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/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f3e695  Fixes [ISSUE #2377] clean up optimize [shenyu-loadbalancer] 
(#2380)
4f3e695 is described below

commit 4f3e6950230b86222f116c784182a15e2ff4dbd3
Author: haibo.duan <[email protected]>
AuthorDate: Wed Nov 17 14:12:51 2021 +0800

    Fixes [ISSUE #2377] clean up optimize [shenyu-loadbalancer] (#2380)
---
 .../loadbalancer/cache/UpstreamCacheManager.java   | 42 ++++++++++++----------
 .../loadbalancer/cache/UpstreamCheckTask.java      | 13 ++++---
 .../loadbalancer/cache/UpstreamWithSelectorId.java | 12 +++----
 .../shenyu/loadbalancer/entity/Upstream.java       |  2 +-
 .../loadbalancer/spi/AbstractLoadBalancer.java     |  4 ++-
 .../shenyu/loadbalancer/spi/HashLoadBalancer.java  | 11 +++---
 .../loadbalancer/spi/RandomLoadBalancer.java       | 24 +++++++++----
 .../loadbalancer/spi/RoundRobinLoadBalancer.java   | 19 ++++++----
 8 files changed, 78 insertions(+), 49 deletions(-)

diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java
index 8f9605f..0b2a4d4 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java
@@ -44,7 +44,9 @@ public final class UpstreamCacheManager {
 
     private UpstreamCheckTask task;
 
-    // health check parameters
+    /**
+     * health check parameters.
+     */
     private Boolean checkEnable;
 
     private int checkTimeout;
@@ -55,7 +57,9 @@ public final class UpstreamCacheManager {
 
     private int unhealthyThreshold;
 
-    // healthy upstream print parameters
+    /**
+     * healthy upstream print parameters.
+     */
     private Boolean printEnable;
 
     private Integer printInterval;
@@ -96,7 +100,7 @@ public final class UpstreamCacheManager {
             }
         }
     }
-    
+
     /**
      * Gets instance.
      *
@@ -105,7 +109,7 @@ public final class UpstreamCacheManager {
     public static UpstreamCacheManager getInstance() {
         return INSTANCE;
     }
-    
+
     /**
      * Find upstream list by selector id list.
      *
@@ -115,7 +119,7 @@ public final class UpstreamCacheManager {
     public List<Upstream> findUpstreamListBySelectorId(final String 
selectorId) {
         return task.getHealthyUpstream().get(selectorId);
     }
-    
+
     /**
      * Remove by key.
      *
@@ -125,7 +129,7 @@ public final class UpstreamCacheManager {
         UPSTREAM_MAP.remove(key);
         task.triggerRemoveAll(key);
     }
-    
+
     /**
      * Submit.
      *
@@ -135,19 +139,19 @@ public final class UpstreamCacheManager {
     public void submit(final String selectorId, final List<Upstream> 
upstreamList) {
         if (CollectionUtils.isNotEmpty(upstreamList)) {
             List<Upstream> existUpstream = 
UPSTREAM_MAP.computeIfAbsent(selectorId, k -> Lists.newArrayList());
-            // check upstream delete
-            for (Upstream upstream : existUpstream) {
-                if (!upstreamList.contains(upstream)) {
-                    task.triggerRemoveOne(selectorId, upstream);
-                }
-            }
-            // check upstream add
-            for (Upstream upstream : upstreamList) {
-                if (!existUpstream.contains(upstream)) {
-                    task.triggerAddOne(selectorId, upstream);
-                }
-            }
-            // replace upstream
+            /**
+             * check upstream delete.
+             */
+            existUpstream.stream().filter(upstream -> 
!upstreamList.contains(upstream))
+                    .forEach(upstream -> task.triggerRemoveOne(selectorId, 
upstream));
+            /**
+             * check upstream add.
+             */
+            upstreamList.stream().filter(upstream -> 
!existUpstream.contains(upstream))
+                    .forEach(upstream -> task.triggerAddOne(selectorId, 
upstream));
+            /**
+             * replace upstream
+             */
             UPSTREAM_MAP.put(selectorId, upstreamList);
         } else {
             UPSTREAM_MAP.remove(selectorId);
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java
index 6930a3e..9b2c37d 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java
@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -129,8 +130,10 @@ public final class UpstreamCheckTask implements Runnable {
 
     private void healthCheck() {
         try {
-            // If there is no synchronized. when check is done and all 
upstream check result is in the futures list.
-            // In the same time, triggerRemoveAll() called before 
waitFinish(), there will be dirty data stay in map.
+            /**
+             * If there is no synchronized. when check is done and all 
upstream check result is in the futures list.
+             * In the same time, triggerRemoveAll() called before 
waitFinish(), there will be dirty data stay in map.
+             */
             synchronized (lock) {
                 if (tryStartHealthCheck()) {
                     doHealthCheck();
@@ -229,7 +232,7 @@ public final class UpstreamCheckTask implements Runnable {
     public void triggerAddOne(final String selectorId, final Upstream 
upstream) {
         putToMap(healthyUpstream, selectorId, upstream);
     }
-    
+
     /**
      * Remove a specific upstream via selectorId.
      *
@@ -276,7 +279,7 @@ public final class UpstreamCheckTask implements Runnable {
      */
     public void printHealthyUpstream() {
         healthyUpstream.forEach((k, v) -> {
-            if (v != null) {
+            if (Objects.nonNull(v)) {
                 List<String> list = 
v.stream().map(Upstream::getUrl).collect(Collectors.toList());
                 LOG.info("[Health Check] currently healthy upstream: {}", 
GsonUtils.getInstance().toJson(list));
             }
@@ -288,7 +291,7 @@ public final class UpstreamCheckTask implements Runnable {
      */
     public void printUnhealthyUpstream() {
         unhealthyUpstream.forEach((k, v) -> {
-            if (v != null) {
+            if (Objects.nonNull(v)) {
                 List<String> list = 
v.stream().map(Upstream::getUrl).collect(Collectors.toList());
                 LOG.info("[Health Check] currently unhealthy upstream: {}", 
GsonUtils.getInstance().toJson(list));
             }
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamWithSelectorId.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamWithSelectorId.java
index 493c0f8..3d861b4 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamWithSelectorId.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamWithSelectorId.java
@@ -29,7 +29,7 @@ public class UpstreamWithSelectorId {
     private String selectorId;
 
     private Upstream upstream;
-    
+
     /**
      * all args constructor.
      *
@@ -40,7 +40,7 @@ public class UpstreamWithSelectorId {
         this.selectorId = selectorId;
         this.upstream = upstream;
     }
-    
+
     /**
      * get selectorId.
      *
@@ -49,7 +49,7 @@ public class UpstreamWithSelectorId {
     public String getSelectorId() {
         return selectorId;
     }
-    
+
     /**
      * set selectorId.
      *
@@ -58,7 +58,7 @@ public class UpstreamWithSelectorId {
     public void setSelectorId(final String selectorId) {
         this.selectorId = selectorId;
     }
-    
+
     /**
      * get upstream.
      *
@@ -67,7 +67,7 @@ public class UpstreamWithSelectorId {
     public Upstream getUpstream() {
         return upstream;
     }
-    
+
     /**
      * set upstream.
      *
@@ -82,7 +82,7 @@ public class UpstreamWithSelectorId {
         if (this == o) {
             return true;
         }
-        if (o == null || getClass() != o.getClass()) {
+        if (Objects.isNull(o) || getClass() != o.getClass()) {
             return false;
         }
         UpstreamWithSelectorId that = (UpstreamWithSelectorId) o;
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
index 1d7b58d..eb61c41 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
@@ -40,7 +40,7 @@ public final class Upstream {
     private int weight;
 
     /**
-     * false close/ true open.
+     * false close, true open.
      */
     private boolean status;
 
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/AbstractLoadBalancer.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/AbstractLoadBalancer.java
index eb57990..b5ab0f7 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/AbstractLoadBalancer.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/AbstractLoadBalancer.java
@@ -18,8 +18,10 @@
 package org.apache.shenyu.loadbalancer.spi;
 
 import org.apache.shenyu.loadbalancer.entity.Upstream;
+import org.apache.commons.collections4.CollectionUtils;
 
 import java.util.List;
+import java.util.Objects;
 
 /**
  * The type Abstract load balancer.
@@ -37,7 +39,7 @@ public abstract class AbstractLoadBalancer implements 
LoadBalancer {
 
     @Override
     public Upstream select(final List<Upstream> upstreamList, final String ip) 
{
-        if (upstreamList == null || upstreamList.isEmpty()) {
+        if (Objects.isNull(upstreamList) || 
CollectionUtils.isEmpty(upstreamList)) {
             return null;
         }
         if (upstreamList.size() == 1) {
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalancer.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalancer.java
index 4bbc9da..a678c77 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalancer.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/HashLoadBalancer.java
@@ -26,6 +26,7 @@ import java.security.NoSuchAlgorithmException;
 import java.util.List;
 import java.util.SortedMap;
 import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.stream.IntStream;
 
 /**
  * hash algorithm impl.
@@ -38,13 +39,13 @@ public class HashLoadBalancer extends AbstractLoadBalancer {
     @Override
     public Upstream doSelect(final List<Upstream> upstreamList, final String 
ip) {
         final ConcurrentSkipListMap<Long, Upstream> treeMap = new 
ConcurrentSkipListMap<>();
-        for (Upstream upstream : upstreamList) {
-            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
+        upstreamList.stream().forEach(upstream -> {
+            IntStream.range(0, VIRTUAL_NODE_NUM).forEach(i -> {
                 long addressHash = hash("SHENYU-" + upstream.getUrl() + 
"-HASH-" + i);
                 treeMap.put(addressHash, upstream);
-            }
-        }
-        long hash = hash(String.valueOf(ip));
+            });
+        });
+        long hash = hash(ip);
         SortedMap<Long, Upstream> lastRing = treeMap.tailMap(hash);
         if (!lastRing.isEmpty()) {
             return lastRing.get(lastRing.firstKey());
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 da1e17a..44b7976 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
@@ -38,7 +38,9 @@ public class RandomLoadBalancer extends AbstractLoadBalancer {
         if (totalWeight > 0 && !sameWeight) {
             return random(totalWeight, upstreamList);
         }
-        // If the weights are the same or the weights are 0 then random
+        /**
+         * If the weights are the same or the weights are 0 then random.
+         */
         return random(upstreamList);
     }
 
@@ -48,7 +50,9 @@ public class RandomLoadBalancer extends AbstractLoadBalancer {
         for (int i = 0; i < length; i++) {
             int weight = getWeight(upstreamList.get(i));
             if (i > 0 && weight != getWeight(upstreamList.get(i - 1))) {
-                // Calculate whether the weight of ownership is the same
+                /**
+                 * Calculate whether the weight of ownership is the same.
+                 */
                 sameWeight = false;
                 break;
             }
@@ -57,20 +61,28 @@ public class RandomLoadBalancer extends 
AbstractLoadBalancer {
     }
 
     private int calculateTotalWeight(final List<Upstream> upstreamList) {
-        // total weight
+        /**
+         * total weight.
+         */
         int totalWeight = 0;
         for (Upstream divideUpstream : upstreamList) {
             int weight = getWeight(divideUpstream);
-            // Cumulative total weight
+            /**
+             * Cumulative total weight.
+             */
             totalWeight += weight;
         }
         return totalWeight;
     }
 
     private Upstream random(final int totalWeight, 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
+        /**
+         * 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
+        /**
+         * Determine which segment the random value falls on
+         */
         for (Upstream divideUpstream : upstreamList) {
             offset -= getWeight(divideUpstream);
             if (offset < 0) {
diff --git 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
index 907f35b..0d16a1a 100644
--- 
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
+++ 
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalancer.java
@@ -21,6 +21,7 @@ import org.apache.shenyu.loadbalancer.entity.Upstream;
 import org.apache.shenyu.spi.Join;
 
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -42,7 +43,7 @@ public class RoundRobinLoadBalancer extends 
AbstractLoadBalancer {
     public Upstream doSelect(final List<Upstream> upstreamList, final String 
ip) {
         String key = upstreamList.get(0).getUrl();
         ConcurrentMap<String, WeightedRoundRobin> map = 
methodWeightMap.get(key);
-        if (map == null) {
+        if (Objects.isNull(map)) {
             methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<>(16));
             map = methodWeightMap.get(key);
         }
@@ -55,13 +56,15 @@ public class RoundRobinLoadBalancer extends 
AbstractLoadBalancer {
             String rKey = upstream.getUrl();
             WeightedRoundRobin weightedRoundRobin = map.get(rKey);
             int weight = getWeight(upstream);
-            if (weightedRoundRobin == null) {
+            if (Objects.isNull(weightedRoundRobin)) {
                 weightedRoundRobin = new WeightedRoundRobin();
                 weightedRoundRobin.setWeight(weight);
                 map.putIfAbsent(rKey, weightedRoundRobin);
             }
             if (weight != weightedRoundRobin.getWeight()) {
-                //weight changed
+                /**
+                 * weight changed.
+                 */
                 weightedRoundRobin.setWeight(weight);
             }
             long cur = weightedRoundRobin.increaseCurrent();
@@ -75,7 +78,9 @@ public class RoundRobinLoadBalancer extends 
AbstractLoadBalancer {
         }
         if (!updateLock.get() && upstreamList.size() != map.size() && 
updateLock.compareAndSet(false, true)) {
             try {
-                // copy -> modify -> update reference
+                /**
+                 * copy -> modify -> update reference.
+                 */
                 ConcurrentMap<String, WeightedRoundRobin> newMap = new 
ConcurrentHashMap<>(map);
                 newMap.entrySet().removeIf(item -> now - 
item.getValue().getLastUpdate() > recyclePeriod);
                 methodWeightMap.put(key, newMap);
@@ -83,11 +88,13 @@ public class RoundRobinLoadBalancer extends 
AbstractLoadBalancer {
                 updateLock.set(false);
             }
         }
-        if (selectedInvoker != null) {
+        if (Objects.nonNull(selectedInvoker)) {
             selectedWRR.sel(totalWeight);
             return selectedInvoker;
         }
-        // should not happen here
+        /**
+         * should not happen here.
+         */
         return upstreamList.get(0);
     }
 

Reply via email to