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

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


The following commit(s) were added to refs/heads/master by this push:
     new ad82467  improve the code that 
RegistryDirectory.java#destroyUnusedInvokers an… (#7902)
ad82467 is described below

commit ad824676fba13af8e5abdc2bb5a4f8ae969d8b13
Author: zhox <[email protected]>
AuthorDate: Mon May 31 10:12:35 2021 +0800

    improve the code that RegistryDirectory.java#destroyUnusedInvokers an… 
(#7902)
    
    * improve the code that RegistryDirectory.java#destroyUnusedInvokers and 
RegistryDirectory.java#toMergeInvokerList
    
    * 1 fix with RegistryDirectory.java: remove unused imports
    
    Co-authored-by: zhongxiong.zeng <[email protected]>
---
 .../registry/integration/RegistryDirectory.java    | 44 ++++++----------------
 1 file changed, 12 insertions(+), 32 deletions(-)

diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
index 44a938e..a0bd837 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
@@ -39,7 +39,6 @@ import org.apache.dubbo.rpc.protocol.InvokerWrapper;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -127,7 +126,7 @@ public class RegistryDirectory<T> extends 
DynamicDirectory<T> {
         List<AddressListener> supportedListeners = 
addressListenerExtensionLoader.getActivateExtension(getUrl(), (String[]) null);
         if (supportedListeners != null && !supportedListeners.isEmpty()) {
             for (AddressListener addressListener : supportedListeners) {
-                providerURLs = addressListener.notify(providerURLs, 
getConsumerUrl(),this);
+                providerURLs = addressListener.notify(providerURLs, 
getConsumerUrl(), this);
             }
         }
         refreshOverrideAndInvoker(providerURLs);
@@ -211,11 +210,9 @@ public class RegistryDirectory<T> extends 
DynamicDirectory<T> {
             this.invokers = multiGroup ? toMergeInvokerList(newInvokers) : 
newInvokers;
             this.urlInvokerMap = newUrlInvokerMap;
 
-            try {
-                destroyUnusedInvokers(oldUrlInvokerMap, newUrlInvokerMap); // 
Close the unused Invoker
-            } catch (Exception e) {
-                logger.warn("destroyUnusedInvokers error. ", e);
-            }
+            // Close the unused Invoker
+            destroyUnusedInvokers(oldUrlInvokerMap, newUrlInvokerMap);
+
         }
 
         // notify invokers refreshed
@@ -224,23 +221,18 @@ public class RegistryDirectory<T> extends 
DynamicDirectory<T> {
 
     private List<Invoker<T>> toMergeInvokerList(List<Invoker<T>> invokers) {
         List<Invoker<T>> mergedInvokers = new ArrayList<>();
-        Map<String, List<Invoker<T>>> groupMap = new HashMap<>();
-        for (Invoker<T> invoker : invokers) {
-            String group = invoker.getUrl().getParameter(GROUP_KEY, "");
-            groupMap.computeIfAbsent(group, k -> new ArrayList<>());
-            groupMap.get(group).add(invoker);
-        }
+        // group by invoker#url#group
+        Map<String, List<Invoker<T>>> groupMap =
+                invokers.stream().collect(Collectors.groupingBy(x -> 
x.getUrl().getParameter(GROUP_KEY, "")));
 
-        if (groupMap.size() == 1) {
-            mergedInvokers.addAll(groupMap.values().iterator().next());
-        } else if (groupMap.size() > 1) {
+        if (groupMap.size() > 1) {
             for (List<Invoker<T>> groupList : groupMap.values()) {
                 StaticDirectory<T> staticDirectory = new 
StaticDirectory<>(groupList);
                 staticDirectory.buildRouterChain();
                 mergedInvokers.add(CLUSTER.join(staticDirectory));
             }
         } else {
-            mergedInvokers = invokers;
+            mergedInvokers.addAll(invokers);
         }
         return mergedInvokers;
     }
@@ -441,22 +433,10 @@ public class RegistryDirectory<T> extends 
DynamicDirectory<T> {
             return;
         }
         // check deleted invoker
-        List<URL> deleted = null;
         if (oldUrlInvokerMap != null) {
-            for (Map.Entry<URL, Invoker<T>> entry : 
oldUrlInvokerMap.entrySet()) {
-                if (!newUrlInvokerMap.containsKey(entry.getKey())) {
-                    if (deleted == null) {
-                        deleted = new ArrayList<>();
-                    }
-                    deleted.add(entry.getKey());
-                }
-            }
-        }
-
-        if (deleted != null) {
-            for (URL url : deleted) {
-                if (url != null) {
-                    Invoker<T> invoker = oldUrlInvokerMap.get(url);
+            for (URL key : oldUrlInvokerMap.keySet()) {
+                if (null != key && !newUrlInvokerMap.containsKey(key)) {
+                    Invoker<T> invoker = oldUrlInvokerMap.get(key);
                     if (invoker != null) {
                         try {
                             invoker.destroy();

Reply via email to