You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/09/27 11:59:07 UTC

[GitHub] [dubbo] AlbumenJ commented on a change in pull request #8916: [Dubbo-5961] Improve consistent hashing load balancing with a new algorithm #5989

AlbumenJ commented on a change in pull request #8916:
URL: https://github.com/apache/dubbo/pull/8916#discussion_r716620179



##########
File path: dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
##########
@@ -111,13 +149,49 @@ private String toKey(Object[] args) {
         }
 
         private Invoker<T> selectForKey(long hash) {
+            ++totalRequestCount;
             Map.Entry<Long, Invoker<T>> entry = virtualInvokers.ceilingEntry(hash);
             if (entry == null) {
                 entry = virtualInvokers.firstEntry();
             }
+            String serverAddress = entry.getValue().getUrl().getAddress();
+            double overloadThread = ((double) totalRequestCount / (double) serverCount) * overloadRatioAllowed;
+
+            /**
+             * Find a valid server node:
+             * 1. Not have accept request yet
+             * or
+             * 2. Not have overloaded (request count already accept < thread (average request count * overloadRatioAllowed ))
+             */
+            while (serverRequestCountMap.containsKey(serverAddress)
+                    && serverRequestCountMap.get(serverAddress) >= overloadThread) {
+                /**
+                 * If server node is not valid, get next node
+                 */
+                entry = virtualInvokers.higherEntry(entry.getKey());
+                if(entry == null){
+                    entry = virtualInvokers.firstEntry();
+                }
+                serverAddress = entry.getValue().getUrl().getAddress();
+            }
+
+            if (!serverRequestCountMap.containsKey(serverAddress)) {
+                //
+                serverRequestCountMap.put(serverAddress, 1L);
+            } else {
+                serverRequestCountMap.put(serverAddress, serverRequestCountMap.get(entry.getValue().getUrl().getAddress()) + 1L);

Review comment:
       Use ActomicLong.increase to prevent concurrent problem

##########
File path: dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
##########
@@ -72,6 +84,28 @@
 
         private final int[] argumentIndex;
 
+        /**
+         * key: server(invoker) address
+         * value: count of requests accept by certain server
+         */
+        private Map<String, Long> serverRequestCountMap = new HashMap<>();

Review comment:
       Change to ConcurrentHashMap to prevent concurrent problem




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org