You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/11/25 06:20:02 UTC

[dubbo] 16/45: improve:优化权重随机消耗过多 CPU 的问题

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

liujun pushed a commit to branch release/3.0.14-rpccontext-bugfix
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit 21862abbe26947ec59f15f942fa2d18e44faa3a8
Author: 未宇 <li...@alibaba-inc.com>
AuthorDate: Thu Oct 22 15:05:03 2020 +0800

    improve:优化权重随机消耗过多 CPU 的问题
---
 .../rpc/cluster/loadbalance/RandomLoadBalance.java | 42 ++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
index adfd04b..0c11a6b 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalance.java
@@ -17,12 +17,18 @@
 package org.apache.dubbo.rpc.cluster.loadbalance;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
 
 import java.util.List;
 import java.util.concurrent.ThreadLocalRandom;
 
+import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_SERVICE_REFERENCE_PATH;
+import static org.apache.dubbo.rpc.cluster.Constants.WEIGHT_KEY;
+
 /**
  * This class select one provider from multiple providers randomly.
  * You can define weights for each provider:
@@ -37,8 +43,9 @@ public class RandomLoadBalance extends AbstractLoadBalance {
 
     /**
      * Select one invoker between a list using a random criteria
-     * @param invokers List of possible invokers
-     * @param url URL
+     *
+     * @param invokers   List of possible invokers
+     * @param url        URL
      * @param invocation Invocation
      * @param <T>
      * @return The selected invoker
@@ -47,6 +54,12 @@ public class RandomLoadBalance extends AbstractLoadBalance {
     protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
         // Number of invokers
         int length = invokers.size();
+
+        // 不用计算权重,直接返回
+        if (!needWeightLoadBalance(invokers,invocation)){
+            return invokers.get(ThreadLocalRandom.current().nextInt(length));
+        }
+
         // Every invoker has the same weight?
         boolean sameWeight = true;
         // the weight of every invokers
@@ -81,4 +94,29 @@ public class RandomLoadBalance extends AbstractLoadBalance {
         return invokers.get(ThreadLocalRandom.current().nextInt(length));
     }
 
+
+    private <T> boolean needWeightLoadBalance(List<Invoker<T>> invokers, Invocation invocation) {
+
+        Invoker invoker = invokers.get(0);
+        URL invokerUrl = invoker.getUrl();
+        // Multiple registry scenario, load balance among multiple registries.
+        if (REGISTRY_SERVICE_REFERENCE_PATH.equals(invokerUrl.getServiceInterface())) {
+            String weight = invokerUrl.getParameter(REGISTRY_KEY + "." + WEIGHT_KEY);
+            if (StringUtils.isNotEmpty(weight)) {
+                return true;
+            }
+        } else {
+            String weight = invokerUrl.getMethodParameter(invocation.getMethodName(), WEIGHT_KEY);
+            if (StringUtils.isNotEmpty(weight)) {
+                return true;
+            }else {
+                String timeStamp = invoker.getUrl().getParameter(TIMESTAMP_KEY);
+                if (StringUtils.isNotEmpty(timeStamp)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
 }