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 2020/02/05 08:12:24 UTC

[dubbo] branch master updated: Remove registry from factory that already destroyed. #5483 (#5608)

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

liujieqin 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 2c9d7f5  Remove registry from factory that already destroyed. #5483 (#5608)
2c9d7f5 is described below

commit 2c9d7f5a0cf1a33e79bb46bf57837079c45d5fe2
Author: GuoHao <gu...@gmail.com>
AuthorDate: Wed Feb 5 16:12:01 2020 +0800

    Remove registry from factory that already destroyed. #5483 (#5608)
---
 .../org/apache/dubbo/registry/support/AbstractRegistry.java   |  1 +
 .../dubbo/registry/support/AbstractRegistryFactory.java       |  9 +++++++++
 .../dubbo/registry/support/AbstractRegistryFactoryTest.java   | 11 ++++++++++-
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
index d6537f5..a005f63 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java
@@ -493,6 +493,7 @@ public abstract class AbstractRegistry implements Registry {
                 }
             }
         }
+        AbstractRegistryFactory.removeDestroyedRegistry(this);
     }
 
     protected boolean acceptable(URL urlToRegistry) {
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 af8ee12..7a3be4a 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
@@ -175,6 +175,15 @@ public abstract class AbstractRegistryFactory implements RegistryFactory {
         }
     };
 
+    public static void removeDestroyedRegistry(Registry toRm){
+        LOCK.lock();
+        try {
+            REGISTRIES.entrySet().removeIf(entry -> entry.getValue().equals(toRm));
+        } finally {
+            LOCK.unlock();
+        }
+    }
+
     // for unit test
     public static void clearRegistryNotDestroy() {
         REGISTRIES.clear();
diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java
index 1fd1e6d..a43583b 100644
--- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java
+++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/support/AbstractRegistryFactoryTest.java
@@ -30,7 +30,6 @@ import java.util.List;
 
 /**
  * AbstractRegistryFactoryTest
- *
  */
 public class AbstractRegistryFactoryTest {
 
@@ -107,10 +106,20 @@ public class AbstractRegistryFactoryTest {
     public void testDestroyAllRegistries() {
         Registry registry1 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":8888?group=xxx"));
         Registry registry2 = registryFactory.getRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":9999?group=yyy"));
+        Registry registry3 = new AbstractRegistry(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":2020?group=yyy")) {
+            @Override
+            public boolean isAvailable() {
+                return true;
+            }
+        };
         Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
         Assertions.assertTrue(registries.contains(registry1));
         Assertions.assertTrue(registries.contains(registry2));
+        registry3.destroy();
+        registries = AbstractRegistryFactory.getRegistries();
+        Assertions.assertFalse(registries.contains(registry3));
         AbstractRegistryFactory.destroyAll();
+        registries = AbstractRegistryFactory.getRegistries();
         Assertions.assertFalse(registries.contains(registry1));
         Assertions.assertFalse(registries.contains(registry2));
     }