You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/08/29 23:48:04 UTC

[GitHub] [inlong] rhizoma-atractylodis commented on a diff in pull request #5595: [INLONG-5101][DataProxy] Optimize load balancing for DataProxy

rhizoma-atractylodis commented on code in PR #5595:
URL: https://github.com/apache/inlong/pull/5595#discussion_r957880520


##########
inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/network/ClientMgr.java:
##########
@@ -348,6 +351,90 @@ public synchronized NettyClient getClientByRoundRobin() {
         return client;
     }
 
+    public synchronized NettyClient getClientByRandom() {
+        NettyClient client;
+        if (clientList.isEmpty()) {
+            return null;
+        }
+        int currSize = clientList.size();
+        int maxRetry = this.configure.getMaxRetry();
+        Random random = new Random(System.currentTimeMillis());
+        do {
+            int randomId = random.nextInt();
+            client = clientList.get(randomId % currSize);
+            if (client != null && client.isActive()) {
+                break;
+            }
+            maxRetry--;
+        } while (maxRetry > 0);
+        if (client == null || !client.isActive()) {
+            return null;
+        }
+        return client;
+    }
+
+    public synchronized NettyClient getClientByConsistencyHash(String messageId) {
+        NettyClient client;
+        if (clientList.isEmpty()) {
+            return null;
+        }
+        String hash = ConsistencyHashUtil.hashMurMurHash(messageId);
+        HashRing cluster = HashRing.getInstance();
+        HostInfo info = cluster.getNode(hash);
+        client = this.clientMap.get(info);
+        return client;
+    }
+
+//    public synchronized NettyClient getClientByLeastConnections() {}
+
+    public synchronized NettyClient getClientByWeightRoundRobin() {
+        NettyClient client = null;
+        double maxWeight = Double.MIN_VALUE;
+        int clientId = 0;
+        if (clientList.isEmpty()) {
+            return null;
+        }
+        int currSize = clientList.size();
+        for (int retryTime = 0; retryTime < currSize; retryTime++) {
+            currentIndex = (++currentIndex) % currSize;
+            client = clientList.get(currentIndex);
+            if (client != null && client.isActive() && client.getWeight() > maxWeight) {
+                clientId = currentIndex;
+            }
+        }
+        if (client == null || !client.isActive()) {
+            return null;
+        }
+        return clientList.get(clientId);
+    }
+
+    public synchronized NettyClient getClientByWeightRandom() {
+        NettyClient client;
+        double maxWeight = Double.MIN_VALUE;
+        int clientId = 0;
+        if (clientList.isEmpty()) {
+            return null;
+        }
+        int currSize = clientList.size();
+        int maxRetry = 1000;

Review Comment:
   this was changed yesterday



-- 
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: commits-unsubscribe@inlong.apache.org

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