You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/12/17 07:51:29 UTC

[incubator-dubbo] branch master updated: make loadbalance robust, add comments (#2962)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new a5faea2  make loadbalance robust, add comments (#2962)
a5faea2 is described below

commit a5faea2232cc306d4243513038afdaac06f83497
Author: Lu Qi <58...@users.noreply.github.com>
AuthorDate: Mon Dec 17 15:51:21 2018 +0800

    make loadbalance robust, add comments (#2962)
---
 .../cluster/loadbalance/AbstractLoadBalance.java   | 22 +++++++++++++++++++---
 .../loadbalance/LeastActiveLoadBalance.java        | 11 ++++++++---
 .../rpc/cluster/loadbalance/RandomLoadBalance.java | 21 +++++++++++++++------
 .../cluster/loadbalance/RoundRobinLoadBalance.java |  4 +---
 4 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
index 7a8d0aa..feb9202 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
@@ -26,10 +26,17 @@ import java.util.List;
 
 /**
  * AbstractLoadBalance
- *
  */
 public abstract class AbstractLoadBalance implements LoadBalance {
-
+    /**
+     * Calculate the weight according to the uptime proportion of warmup time
+     * the new weight will be within 1(inclusive) to weight(inclusive)
+     *
+     * @param uptime the uptime in milliseconds
+     * @param warmup the warmup time in milliseconds
+     * @param weight the weight of an invoker
+     * @return weight which takes warmup into account
+     */
     static int calculateWarmupWeight(int uptime, int warmup, int weight) {
         int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
         return ww < 1 ? 1 : (ww > weight ? weight : ww);
@@ -48,6 +55,15 @@ public abstract class AbstractLoadBalance implements LoadBalance {
 
     protected abstract <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation);
 
+
+    /**
+     * Get the weight of the invoker's invocation which takes warmup time into account
+     * if the uptime is within the warmup time, the weight will be reduce proportionally
+     *
+     * @param invoker    the invoker
+     * @param invocation the invocation of this invoker
+     * @return weight
+     */
     protected int getWeight(Invoker<?> invoker, Invocation invocation) {
         int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
         if (weight > 0) {
@@ -60,7 +76,7 @@ public abstract class AbstractLoadBalance implements LoadBalance {
                 }
             }
         }
-        return weight;
+        return weight >= 0 ? weight : 0;
     }
 
 }
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
index 860dd3d..7841958 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
@@ -46,6 +46,8 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
         int leastCount = 0;
         // The index of invokers having the same least active value (leastActive)
         int[] leastIndexes = new int[length];
+        // the weight of every invokers
+        int[] weights = new int[length];
         // The sum of the warmup weights of all the least active invokes
         int totalWeight = 0;
         // The weight of the first least active invoke
@@ -53,6 +55,7 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
         // Every least active invoker has the same weight value?
         boolean sameWeight = true;
 
+
         // Filter out all the least active invokers
         for (int i = 0; i < length; i++) {
             Invoker<T> invoker = invokers.get(i);
@@ -60,6 +63,8 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
             int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive();
             // Get the weight of the invoke configuration. The default value is 100.
             int afterWarmup = getWeight(invoker, invocation);
+            // save for later use
+            weights[i] = afterWarmup;
             // If it is the first invoker or the active number of the invoker is less than the current least active number
             if (leastActive == -1 || active < leastActive) {
                 // Reset the active number of the current invoker to the least active number
@@ -95,12 +100,12 @@ public class LeastActiveLoadBalance extends AbstractLoadBalance {
         if (!sameWeight && totalWeight > 0) {
             // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on 
             // totalWeight.
-            int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight) + 1;
+            int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight);
             // Return a invoker based on the random value.
             for (int i = 0; i < leastCount; i++) {
                 int leastIndex = leastIndexes[i];
-                offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
-                if (offsetWeight <= 0) {
+                offsetWeight -= weights[leastIndex];
+                if (offsetWeight < 0) {
                     return invokers.get(leastIndex);
                 }
             }
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
index eea0ca3..88453ad 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
@@ -25,7 +25,6 @@ import java.util.concurrent.ThreadLocalRandom;
 
 /**
  * random load balance.
- *
  */
 public class RandomLoadBalance extends AbstractLoadBalance {
 
@@ -33,13 +32,23 @@ public class RandomLoadBalance extends AbstractLoadBalance {
 
     @Override
     protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
-        int length = invokers.size(); // Number of invokers
-        boolean sameWeight = true; // Every invoker has the same weight?
+        // Number of invokers
+        int length = invokers.size();
+        // Every invoker has the same weight?
+        boolean sameWeight = true;
+        // the weight of every invokers
+        int[] weights = new int[length];
+        // the first invoker's weight
         int firstWeight = getWeight(invokers.get(0), invocation);
-        int totalWeight = firstWeight; // The sum of weights
+        weights[0] = firstWeight;
+        // The sum of weights
+        int totalWeight = firstWeight;
         for (int i = 1; i < length; i++) {
             int weight = getWeight(invokers.get(i), invocation);
-            totalWeight += weight; // Sum
+            // save for later use
+            weights[i] = weight;
+            // Sum
+            totalWeight += weight;
             if (sameWeight && weight != firstWeight) {
                 sameWeight = false;
             }
@@ -49,7 +58,7 @@ public class RandomLoadBalance extends AbstractLoadBalance {
             int offset = ThreadLocalRandom.current().nextInt(totalWeight);
             // Return a invoker based on the random value.
             for (int i = 0; i < length; i++) {
-                offset -= getWeight(invokers.get(i), invocation);
+                offset -= weights[i];
                 if (offset < 0) {
                     return invokers.get(i);
                 }
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
index 242a2ff..d30ebe1 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java
@@ -101,9 +101,7 @@ public class RoundRobinLoadBalance extends AbstractLoadBalance {
             String identifyString = invoker.getUrl().toIdentityString();
             WeightedRoundRobin weightedRoundRobin = map.get(identifyString);
             int weight = getWeight(invoker, invocation);
-            if (weight < 0) {
-                weight = 0;
-            }
+
             if (weightedRoundRobin == null) {
                 weightedRoundRobin = new WeightedRoundRobin();
                 weightedRoundRobin.setWeight(weight);