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

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


The following commit(s) were added to refs/heads/3.3 by this push:
     new c8a8946f89 Fix the issue of high CPU load caused by continuously 
creating new ConsistentHashSelector instances under high concurrency (#15497)
c8a8946f89 is described below

commit c8a8946f89ef7f1d3bb9725f754465c6bb675834
Author: DustinQiao <[email protected]>
AuthorDate: Thu Jun 26 22:29:32 2025 +0800

    Fix the issue of high CPU load caused by continuously creating new 
ConsistentHashSelector instances under high concurrency (#15497)
---
 .../rpc/cluster/loadbalance/ConsistentHashLoadBalance.java   | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
index 82421e460f..b776dbe3a7 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalance.java
@@ -53,13 +53,13 @@ public class ConsistentHashLoadBalance extends 
AbstractLoadBalance {
     protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, 
Invocation invocation) {
         String methodName = RpcUtils.getMethodName(invocation);
         String key = invokers.get(0).getUrl().getServiceKey() + "." + 
methodName;
-        // using the hashcode of list to compute the hash only pay attention 
to the elements in the list
         int invokersHashCode = invokers.hashCode();
-        ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) 
selectors.get(key);
-        if (selector == null || selector.identityHashCode != invokersHashCode) 
{
-            selectors.put(key, new ConsistentHashSelector<T>(invokers, 
methodName, invokersHashCode));
-            selector = (ConsistentHashSelector<T>) selectors.get(key);
-        }
+        // using the hashcode of invoker list to create consistent selector by 
atomic computation.
+        ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) 
selectors.compute(
+                key,
+                (k, oldSelector) -> (oldSelector == null || 
oldSelector.identityHashCode != invokersHashCode)
+                        ? new ConsistentHashSelector<>(invokers, methodName, 
invokersHashCode)
+                        : oldSelector);
         return selector.select(invocation);
     }
 

Reply via email to