changfubai commented on a change in pull request #8916:
URL: https://github.com/apache/dubbo/pull/8916#discussion_r717169435
##########
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;
Review comment:
change to AtomicInteger to prevent concurrent problem?
##########
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();
Review comment:
why not invoke method `getNextInvokerNode`?
##########
File path:
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java
##########
@@ -18,55 +18,55 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
-import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.RouterChain;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@SuppressWarnings("rawtypes")
public class ConsistentHashLoadBalanceTest extends LoadBalanceBaseTest {
+ /**
+ * Test case for hot key
+ * https://github.com/apache/dubbo/issues/4103
+ * invoke method with same params for 10000 times:
+ * 1. count of request accept by each invoker will not
+ * be higher than (overloadRatioAllowed * average + 1)
+ *
+ */
@Test
public void testConsistentHashLoadBalance() {
int runs = 10000;
- long unHitedInvokerCount = 0;
- Map<Invoker, Long> hitedInvokers = new HashMap<>();
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs,
ConsistentHashLoadBalance.NAME);
- for (Invoker minvoker : counter.keySet()) {
- Long count = counter.get(minvoker).get();
-
- if (count == 0) {
- unHitedInvokerCount++;
- } else {
- hitedInvokers.put(minvoker, count);
- }
+ double overloadRatioAllowed = 1.5F;
+ int serverCount = counter.size();
+ double overloadThread = ((double) runs *
overloadRatioAllowed)/((double) serverCount);
+ for (Invoker invoker : counter.keySet()) {
+ Long count = counter.get(invoker).get();
+ Assertions.assertTrue(count < (overloadThread + 1L),
Review comment:
这里为啥不是小于overloadThread呢?
##########
File path:
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/ConsistentHashLoadBalanceTest.java
##########
@@ -18,55 +18,55 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;
-import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.apache.dubbo.rpc.cluster.RouterChain;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@SuppressWarnings("rawtypes")
public class ConsistentHashLoadBalanceTest extends LoadBalanceBaseTest {
+ /**
+ * Test case for hot key
+ * https://github.com/apache/dubbo/issues/4103
+ * invoke method with same params for 10000 times:
+ * 1. count of request accept by each invoker will not
+ * be higher than (overloadRatioAllowed * average + 1)
+ *
+ */
@Test
public void testConsistentHashLoadBalance() {
int runs = 10000;
- long unHitedInvokerCount = 0;
- Map<Invoker, Long> hitedInvokers = new HashMap<>();
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs,
ConsistentHashLoadBalance.NAME);
- for (Invoker minvoker : counter.keySet()) {
- Long count = counter.get(minvoker).get();
-
- if (count == 0) {
- unHitedInvokerCount++;
- } else {
- hitedInvokers.put(minvoker, count);
- }
+ double overloadRatioAllowed = 1.5F;
+ int serverCount = counter.size();
+ double overloadThread = ((double) runs *
overloadRatioAllowed)/((double) serverCount);
+ for (Invoker invoker : counter.keySet()) {
+ Long count = counter.get(invoker).get();
+ Assertions.assertTrue(count < (overloadThread + 1L),
Review comment:
这里为啥不是小于overloadThread呢?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]