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 2019/12/10 10:11:26 UTC

[dubbo] branch master updated: Fix shutdown bug: avoid recreating registry after destroy. (#5450)

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

liujun 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 03227ff  Fix shutdown bug: avoid recreating registry after destroy. (#5450)
03227ff is described below

commit 03227ff131d8366a5687ff52213503a5eec3f5e6
Author: ken.lj <ke...@gmail.com>
AuthorDate: Tue Dec 10 18:11:18 2019 +0800

    Fix shutdown bug: avoid recreating registry after destroy. (#5450)
---
 .../registry/support/AbstractRegistryFactory.java  | 58 ++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java
index 6a307ea..b520631 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistryFactory.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.URLBuilder;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.registry.NotifyListener;
 import org.apache.dubbo.registry.Registry;
 import org.apache.dubbo.registry.RegistryFactory;
 import org.apache.dubbo.registry.RegistryService;
@@ -27,7 +28,9 @@ import org.apache.dubbo.registry.RegistryService;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.ReentrantLock;
 
 import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
@@ -50,6 +53,8 @@ public abstract class AbstractRegistryFactory implements RegistryFactory {
     // Registry Collection Map<RegistryAddress, Registry>
     private static final Map<String, Registry> REGISTRIES = new HashMap<>();
 
+    private static final AtomicBoolean destroyed = new AtomicBoolean(false);
+
     /**
      * Get all registries
      *
@@ -67,6 +72,10 @@ public abstract class AbstractRegistryFactory implements RegistryFactory {
      * Close all created registries
      */
     public static void destroyAll() {
+        if (!destroyed.compareAndSet(false, true)) {
+            return;
+        }
+
         if (LOGGER.isInfoEnabled()) {
             LOGGER.info("Close all registries " + getRegistries());
         }
@@ -89,6 +98,12 @@ public abstract class AbstractRegistryFactory implements RegistryFactory {
 
     @Override
     public Registry getRegistry(URL url) {
+        if (destroyed.get()) {
+            LOGGER.warn("All registry instances have been destroyed, failed to fetch any instance. " +
+                    "Usually, this means no need to try to do unnecessary redundant resource clearance, all registries has been taken care of.");
+            return DEFAULT_NOP_REGISTRY;
+        }
+
         url = URLBuilder.from(url)
                 .setPath(RegistryService.class.getName())
                 .addParameter(INTERFACE_KEY, RegistryService.class.getName())
@@ -117,4 +132,47 @@ public abstract class AbstractRegistryFactory implements RegistryFactory {
 
     protected abstract Registry createRegistry(URL url);
 
+
+    private static Registry DEFAULT_NOP_REGISTRY = new Registry() {
+        @Override
+        public URL getUrl() {
+            return null;
+        }
+
+        @Override
+        public boolean isAvailable() {
+            return false;
+        }
+
+        @Override
+        public void destroy() {
+
+        }
+
+        @Override
+        public void register(URL url) {
+
+        }
+
+        @Override
+        public void unregister(URL url) {
+
+        }
+
+        @Override
+        public void subscribe(URL url, NotifyListener listener) {
+
+        }
+
+        @Override
+        public void unsubscribe(URL url, NotifyListener listener) {
+
+        }
+
+        @Override
+        public List<URL> lookup(URL url) {
+            return null;
+        }
+    };
+
 }