You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by yi...@apache.org on 2020/09/30 03:52:46 UTC

[dubbo] branch master updated: update that how to judge the same weight (#6479)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2d9583a  update that how to judge the same weight (#6479)
2d9583a is described below

commit 2d9583adf26a2d8bd6fb646243a9fe80a77e65d5
Author: yangmengle <31...@users.noreply.github.com>
AuthorDate: Wed Sep 30 11:52:03 2020 +0800

    update that how to judge the same weight (#6479)
    
    update that weights and how to judge the index depend on offset
    
    Co-authored-by: yangmengle <yangmengle>
---
 .../rpc/cluster/loadbalance/RandomLoadBalance.java     | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

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 adfd04b..50215d6 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
@@ -49,20 +49,17 @@ public class RandomLoadBalance extends AbstractLoadBalance {
         int length = invokers.size();
         // Every invoker has the same weight?
         boolean sameWeight = true;
-        // the weight of every invokers
+        // the maxWeight of every invokers, the minWeight = 0 or the maxWeight of the last invoker
         int[] weights = new int[length];
-        // the first invoker's weight
-        int firstWeight = getWeight(invokers.get(0), invocation);
-        weights[0] = firstWeight;
         // The sum of weights
-        int totalWeight = firstWeight;
-        for (int i = 1; i < length; i++) {
+        int totalWeight = 0;
+        for (int i = 0; i < length; i++) {
             int weight = getWeight(invokers.get(i), invocation);
-            // save for later use
-            weights[i] = weight;
             // Sum
             totalWeight += weight;
-            if (sameWeight && weight != firstWeight) {
+            // save for later use
+            weights[i] = totalWeight;
+            if (sameWeight && totalWeight != weight * (i + 1)) {
                 sameWeight = false;
             }
         }
@@ -71,8 +68,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 -= weights[i];
-                if (offset < 0) {
+                if (offset < weights[i]) {
                     return invokers.get(i);
                 }
             }