You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/09/01 08:56:52 UTC

[shenyu] branch master updated: [type: refactor]Refactor random loadbalancer. (#3890)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b07f1ea1b [type: refactor]Refactor random loadbalancer. (#3890)
b07f1ea1b is described below

commit b07f1ea1b4cd994734514e027f3640a5d4b4832b
Author: Kevin Clair <70...@qq.com>
AuthorDate: Thu Sep 1 16:56:44 2022 +0800

    [type: refactor]Refactor random loadbalancer. (#3890)
    
    * refactor random loadbalancer.
    
    * checkstyle
---
 .../loadbalancer/spi/RandomLoadBalancer.java       | 52 +++++++++-------------
 1 file changed, 21 insertions(+), 31 deletions(-)

diff --git a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RandomLoadBalancer.java b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RandomLoadBalancer.java
index 12670fb08..ca1f2547a 100644
--- a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RandomLoadBalancer.java
+++ b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RandomLoadBalancer.java
@@ -33,51 +33,41 @@ public class RandomLoadBalancer extends AbstractLoadBalancer {
 
     @Override
     public Upstream doSelect(final List<Upstream> upstreamList, final String ip) {
-        int totalWeight = calculateTotalWeight(upstreamList);
-        boolean sameWeight = isAllUpStreamSameWeight(upstreamList);
-        if (totalWeight > 0 && !sameWeight) {
-            return random(totalWeight, upstreamList);
-        }
-        // If the weights are the same or the weights are 0 then random.
-        return random(upstreamList);
-    }
-
-    private boolean isAllUpStreamSameWeight(final List<Upstream> upstreamList) {
-        boolean sameWeight = true;
         int length = upstreamList.size();
-        for (int i = 0; i < length; i++) {
-            int weight = getWeight(upstreamList.get(i));
-            if (i > 0 && weight != getWeight(upstreamList.get(i - 1))) {
+        // every upstream has the same weight?
+        boolean sameWeight = true;
+        // the weight of every upstream
+        int[] weights = new int[length];
+        int firstUpstreamWeight = getWeight(upstreamList.get(0));
+        weights[0] = firstUpstreamWeight;
+        // init the totalWeight
+        int totalWeight = firstUpstreamWeight;
+        for (int i = 1; i < length; i++) {
+            int currentUpstreamWeight = getWeight(upstreamList.get(i));
+            weights[i] = currentUpstreamWeight;
+            totalWeight += currentUpstreamWeight;
+            if (sameWeight && currentUpstreamWeight != firstUpstreamWeight) {
                 // Calculate whether the weight of ownership is the same.
                 sameWeight = false;
-                break;
             }
         }
-        return sameWeight;
-    }
-
-    private int calculateTotalWeight(final List<Upstream> upstreamList) {
-        // total weight.
-        int totalWeight = 0;
-        for (Upstream upstream : upstreamList) {
-            int weight = getWeight(upstream);
-            // Cumulative total weight.
-            totalWeight += weight;
+        if (totalWeight > 0 && !sameWeight) {
+            return random(totalWeight, weights, upstreamList);
         }
-        return totalWeight;
+        return random(upstreamList);
     }
 
-    private Upstream random(final int totalWeight, final List<Upstream> upstreamList) {
+    private Upstream random(final int totalWeight, final int[] weights, final List<Upstream> upstreamList) {
         // If the weights are not the same and the weights are greater than 0, then random by the total number of weights.
         int offset = RANDOM.nextInt(totalWeight);
         // Determine which segment the random value falls on
-        for (Upstream upstream : upstreamList) {
-            offset -= getWeight(upstream);
+        for (int i = 0; i < weights.length; i++) {
+            offset -= weights[i];
             if (offset < 0) {
-                return upstream;
+                return upstreamList.get(i);
             }
         }
-        return upstreamList.get(0);
+        return random(upstreamList);
     }
 
     private Upstream random(final List<Upstream> upstreamList) {