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

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


The following commit(s) were added to refs/heads/3.0 by this push:
     new 39292a0  Optimize performance & Fix some potential bugs (#8155)
39292a0 is described below

commit 39292a0fd7172c50618cbff2bd894e2816881cb3
Author: Albumen Kevin <[email protected]>
AuthorDate: Mon Jun 28 14:41:32 2021 +0800

    Optimize performance & Fix some potential bugs (#8155)
---
 .../rpc/cluster/loadbalance/RandomLoadBalance.java | 36 ++++++++++++++++++++++
 .../cluster/router/mesh/route/MeshRuleRouter.java  |  4 +++
 .../cluster/support/FailfastClusterInvoker.java    |  2 +-
 .../main/java/org/apache/dubbo/common/Version.java |  7 ++++-
 .../store/InMemoryWritableMetadataService.java     |  5 +++
 .../remoting/transport/netty4/NettyServer.java     |  9 +-----
 .../dubbo/rpc/protocol/dubbo/DubboProtocol.java    |  2 +-
 .../protocol/dubbo/LazyConnectExchangeClient.java  |  4 +--
 8 files changed, 56 insertions(+), 13 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 50215d6..2c9be6c 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:
@@ -47,6 +53,11 @@ 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 maxWeight of every invokers, the minWeight = 0 or the maxWeight 
of the last invoker
@@ -77,4 +88,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;
+    }
+
+
 }
diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/route/MeshRuleRouter.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/route/MeshRuleRouter.java
index cd09996..3ffbd2c 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/route/MeshRuleRouter.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/route/MeshRuleRouter.java
@@ -320,6 +320,10 @@ public class MeshRuleRouter implements Router, 
VsDestinationGroupRuleListener {
         return true;
     }
 
+    @Override
+    public void stop() {
+        MeshRuleManager.unregister(this);
+    }
 
     /**
      * just for test
diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
index 60490ca..a375a1a 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
@@ -52,7 +52,7 @@ public class FailfastClusterInvoker<T> extends 
AbstractClusterInvoker<T> {
             }
             throw new RpcException(e instanceof RpcException ? ((RpcException) 
e).getCode() : 0,
                     "Failfast invoke providers " + invoker.getUrl() + " " + 
loadbalance.getClass().getSimpleName()
-                            + " select from all providers " + invokers + " for 
service " + getInterface().getName()
+                            + " for service " + getInterface().getName()
                             + " method " + invocation.getMethodName() + " on 
consumer " + NetUtils.getLocalHost()
                             + " use dubbo version " + Version.getVersion()
                             + ", but no luck to perform the invocation. Last 
error is: " + e.getMessage(),
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java
index e730c12..9d66014 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java
@@ -191,7 +191,12 @@ public final class Version {
                 return defaultVersion;
             }
 
-            String file = codeSource.getLocation().getFile();
+            URL location = codeSource.getLocation();
+            if (location == null){
+                logger.info("No location for class " + cls.getName() + " when 
getVersion, use default version " + defaultVersion);
+                return defaultVersion;
+            }
+            String file =  location.getFile();
             if (!StringUtils.isEmpty(file) && file.endsWith(".jar")) {
                 version = getFromFile(file);
             }
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
index d0b99a0..3e13031 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/store/InMemoryWritableMetadataService.java
@@ -54,8 +54,10 @@ import java.util.concurrent.locks.ReentrantLock;
 import static java.util.Collections.emptySortedSet;
 import static java.util.Collections.unmodifiableSortedSet;
 import static org.apache.dubbo.common.URL.buildKey;
+import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
 import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
 import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
 import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
 
@@ -210,6 +212,9 @@ public class InMemoryWritableMetadataService implements 
WritableMetadataService
                 String data = gson.toJson(serviceDefinition);
                 serviceDefinitions.put(providerUrl.getServiceKey(), data);
                 return;
+            } else if 
(CONSUMER_SIDE.equalsIgnoreCase(providerUrl.getParameter(SIDE_KEY))) {
+                //to avoid consumer generic invoke style error
+                return;
             }
             logger.error("publishProvider interfaceName is empty . 
providerUrl: " + providerUrl.toFullString());
         } catch (Throwable e) {
diff --git 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
index 7868057..cbcf947 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
@@ -172,15 +172,8 @@ public class NettyServer extends AbstractServer implements 
RemotingServer {
     @Override
     public Collection<Channel> getChannels() {
         Collection<Channel> chs = new ArrayList<>(this.channels.size());
+        // pick channels from NettyServerHandler ( needless to check 
connectivity )
         chs.addAll(this.channels.values());
-        //复用 NettyServerHandler 里面的 channels,所以不再需要检查是否可连
-//        for (Channel channel : this.channels.values()) {
-//            if (channel.isConnected()) {
-//                chs.add(channel);
-//            } else {
-//                
channels.remove(NetUtils.toAddressString(channel.getRemoteAddress()));
-//            }
-//        }
         return chs;
     }
 
diff --git 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
index 0a8345e..de33b91 100644
--- 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
+++ 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
@@ -535,7 +535,7 @@ public class DubboProtocol extends AbstractProtocol {
 
         for (ReferenceCountExchangeClient referenceCountExchangeClient : 
referenceCountExchangeClients) {
             // As long as one client is not available, you need to replace the 
unavailable client with the available one.
-            if (referenceCountExchangeClient == null || 
referenceCountExchangeClient.isClosed()) {
+            if (referenceCountExchangeClient == null || 
referenceCountExchangeClient.getCount() <= 0 || 
referenceCountExchangeClient.isClosed()) {
                 return false;
             }
         }
diff --git 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
index 8af87c8..2947e7e 100644
--- 
a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
+++ 
b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/LazyConnectExchangeClient.java
@@ -35,8 +35,8 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 import static org.apache.dubbo.remoting.Constants.SEND_RECONNECT_KEY;
-import static 
org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_CONNECT_INITIAL_STATE_KEY;
 import static 
org.apache.dubbo.rpc.protocol.dubbo.Constants.DEFAULT_LAZY_CONNECT_INITIAL_STATE;
+import static 
org.apache.dubbo.rpc.protocol.dubbo.Constants.LAZY_CONNECT_INITIAL_STATE_KEY;
 
 /**
  * dubbo protocol support class.
@@ -135,7 +135,7 @@ final class LazyConnectExchangeClient implements 
ExchangeClient {
     private void warning() {
         if (requestWithWarning) {
             if (warningcount.get() % warning_period == 0) {
-                logger.warn(new IllegalStateException("safe guard client , 
should not be called ,must have a bug."));
+                logger.warn(url.getAddress() + " " + url.getServiceKey() + " 
safe guard client , should not be called ,must have a bug.");
             }
             warningcount.incrementAndGet();
         }

Reply via email to