chickenlj closed pull request #1369: [Dubbo-unsubscribe failed] fixed zookeeper 
unsubscribe failed.
URL: https://github.com/apache/incubator-dubbo/pull/1369
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistry.java
 
b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistry.java
index dcce7e4d49..e687ed93fc 100644
--- 
a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistry.java
+++ 
b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistry.java
@@ -191,7 +191,10 @@ protected void doUnsubscribe(URL url, NotifyListener 
listener) {
         if (listeners != null) {
             ChildListener zkListener = listeners.get(listener);
             if (zkListener != null) {
-                zkClient.removeChildListener(toUrlPath(url), zkListener);
+                // maybe url has many subscribe path
+                for(String path : toUnsubscribedPath(url)){
+                    zkClient.removeChildListener(path, zkListener);
+                }
             }
         }
     }
@@ -256,6 +259,23 @@ private String toUrlPath(URL url) {
         return toCategoryPath(url) + Constants.PATH_SEPARATOR + 
URL.encode(url.toFullString());
     }
 
+    protected List<String> toUnsubscribedPath(URL url){
+        List<String> categories = new ArrayList<String>();
+        if(Constants.ANY_VALUE.equals(url.getServiceInterface())) {
+            String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
+            if (!group.startsWith(Constants.PATH_SEPARATOR)) {
+                group = Constants.PATH_SEPARATOR + group;
+            }
+            categories.add(group);
+            return categories;
+        }else{
+            for (String path : toCategoriesPath(url)) {
+                categories.add(path);
+            }
+        }
+        return categories;
+    }
+
     private List<URL> toUrlsWithoutEmpty(URL consumer, List<String> providers) 
{
         List<URL> urls = new ArrayList<URL>();
         if (providers != null && !providers.isEmpty()) {
diff --git 
a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
 
b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
index 6b37e97965..1f74a5b242 100644
--- 
a/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
+++ 
b/dubbo-registry/dubbo-registry-zookeeper/src/test/java/com/alibaba/dubbo/registry/zookeeper/ZookeeperRegistryTest.java
@@ -17,23 +17,34 @@
 package com.alibaba.dubbo.registry.zookeeper;
 
 import com.alibaba.dubbo.common.URL;
+import com.alibaba.dubbo.common.extension.ExtensionLoader;
+import com.alibaba.dubbo.common.utils.NetUtils;
+import com.alibaba.dubbo.registry.NotifyListener;
+import com.alibaba.dubbo.registry.RegistryFactory;
 
 import junit.framework.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
 /**
  * ZookeeperRegistryTest
  *
  */
 public class ZookeeperRegistryTest {
 
-    String service = "com.alibaba.dubbo.test.injvmServie";
-    URL registryUrl = URL.valueOf("zookeeper://239.255.255.255/");
-    URL serviceUrl = URL.valueOf("zookeeper://zookeeper/" + service
-            + "?notify=false&methods=test1,test2");
-    URL consumerUrl = URL.valueOf("zookeeper://consumer/" + service + 
"?notify=false&methods=test1,test2");
+//    String service = "com.alibaba.dubbo.test.injvmServie";
+//    URL registryUrl = URL.valueOf("zookeeper://239.255.255.255/");
+//    URL serviceUrl = URL.valueOf("zookeeper://zookeeper/" + service
+//            + "?notify=false&methods=test1,test2");
+//    URL consumerUrl = URL.valueOf("zookeeper://consumer/" + service + 
"?notify=false&methods=test1,test2");
     // ZookeeperRegistry registry    = new ZookeeperRegistry(registryUrl);
 
     /**
@@ -105,4 +116,46 @@ public void notify(List<URL> urls) {
 
     }
 
+    @Test
+    @Ignore public void test_unsubscribe() throws InterruptedException {
+
+        registry.unregister(serviceUrl);
+
+        Assert.assertTrue(registry.getRegistered().size() == 0);
+        Assert.assertTrue(registry.getSubscribed().size() == 0);
+
+        final CountDownLatch notNotified = new CountDownLatch(2);
+
+        final AtomicReference<URL> notifiedUrl = new AtomicReference<URL>();
+
+        NotifyListener listener = new NotifyListener() {
+            public void notify(List<URL> urls) {
+                if(urls != null) {
+                    for(Iterator<URL> iterator = urls.iterator(); 
iterator.hasNext();) {
+                        URL url = iterator.next();
+                        if(!url.getProtocol().equals("empty")){
+                            notifiedUrl.set(url);
+                            notNotified.countDown();
+                        }
+                    }
+                }
+            }
+        };
+        registry.subscribe(consumerUrl, listener);
+        registry.unsubscribe(consumerUrl, listener);
+
+        registry.register(serviceUrl);
+
+        Assert.assertFalse(notNotified.await(2, TimeUnit.SECONDS));
+        // expect nothing happen
+        Assert.assertTrue(notifiedUrl.get() == null);
+    }
+
+    String service = "com.alibaba.dubbo.internal.test.DemoServie";
+    URL serviceUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + "/" + 
service + "?methods=test1,test2");
+    URL consumerUrl = URL.valueOf("dubbo://" + NetUtils.getLocalHost() + 
":2018" + "/" + service + "?methods=test1,test2");
+    // should change zookeeper address if not equals 192.168.47.102:2181
+    URL registryUrl = 
URL.valueOf("zookeeper://192.168.47.102:2181/com.alibaba.dubbo.registry.RegistryService");
+    RegistryFactory registryFactory = 
ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
+    ZookeeperRegistry registry = (ZookeeperRegistry) 
registryFactory.getRegistry(registryUrl);
 }
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org
For additional commands, e-mail: notifications-h...@dubbo.apache.org

Reply via email to