You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/07/29 12:12:57 UTC

[dubbo] branch 3.2 updated: bugFix/fix is available timeout (#12796)

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

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


The following commit(s) were added to refs/heads/3.2 by this push:
     new 2488b08d3e bugFix/fix is available timeout (#12796)
2488b08d3e is described below

commit 2488b08d3e1dd4131ab94cf46b320307016cde50
Author: xieshouyu <23...@qq.com>
AuthorDate: Sat Jul 29 20:12:51 2023 +0800

    bugFix/fix is available timeout (#12796)
    
    * 1、Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
    2、Move the isAvailable of NopServiceDiscovery into its own implementation class
    
    * 1、Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
    2、Move the isAvailable of NopServiceDiscovery into its own implementation class
    
    * 1、submit empty after format error
    
    * 1、move destory to implementation class
    
    * 1、update comment description
---
 .../apache/dubbo/registry/client/NopServiceDiscovery.java   |  6 ++++++
 .../org/apache/dubbo/registry/client/ServiceDiscovery.java  |  8 ++++++++
 .../dubbo/registry/client/ServiceDiscoveryRegistry.java     |  9 +++------
 .../dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java | 13 +++++++++++++
 4 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/NopServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/NopServiceDiscovery.java
index 7cf8d32930..2df58f9b19 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/NopServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/NopServiceDiscovery.java
@@ -55,4 +55,10 @@ public class NopServiceDiscovery extends AbstractServiceDiscovery {
     public List<ServiceInstance> getInstances(String serviceName) throws NullPointerException {
         return null;
     }
+
+    @Override
+    public boolean isAvailable() {
+        // NopServiceDiscovery is designed for compatibility, check availability is meaningless, just return true
+        return true;
+    }
 }
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java
index 29960b39e9..9f9cc984d5 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscovery.java
@@ -21,6 +21,7 @@ import java.util.Set;
 
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.lang.Prioritized;
+import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.metadata.MetadataInfo;
 import org.apache.dubbo.registry.RegistryService;
 import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
@@ -94,6 +95,13 @@ public interface ServiceDiscovery extends RegistryService, Prioritized {
         return getUrl().getParameter(REGISTRY_DELAY_NOTIFICATION_KEY, 5000);
     }
 
+    /**
+     * Get services is the default way for service discovery to be available
+     */
+    default boolean isAvailable() {
+        return !isDestroy() && CollectionUtils.isNotEmpty(getServices());
+    }
+
     /**
      * A human-readable description of the implementation
      *
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
index d808a05b97..9c6eb314de 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceDiscoveryRegistry.java
@@ -110,7 +110,7 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
      */
     protected ServiceDiscovery createServiceDiscovery(URL registryURL) {
         return getServiceDiscovery(registryURL.addParameter(INTERFACE_KEY, ServiceDiscovery.class.getName())
-            .removeParameter(REGISTRY_TYPE_KEY));
+                .removeParameter(REGISTRY_TYPE_KEY));
     }
 
     /**
@@ -278,11 +278,8 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
 
     @Override
     public boolean isAvailable() {
-        if (serviceDiscovery instanceof NopServiceDiscovery) {
-            // NopServiceDiscovery is designed for compatibility, check availability is meaningless, just return true
-            return true;
-        }
-        return !serviceDiscovery.isDestroy() && !serviceDiscovery.getServices().isEmpty();
+        //serviceDiscovery isAvailable has a default method, which can be used as a reference when implementing
+        return serviceDiscovery.isAvailable();
     }
 
     @Override
diff --git a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
index 1dd2ea8896..34ee84363a 100644
--- a/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
+++ b/dubbo-registry/dubbo-registry-zookeeper/src/main/java/org/apache/dubbo/registry/zookeeper/ZookeeperServiceDiscovery.java
@@ -34,6 +34,7 @@ import org.apache.dubbo.common.function.ThrowableConsumer;
 import org.apache.dubbo.common.function.ThrowableFunction;
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
 import org.apache.dubbo.registry.client.ServiceDiscovery;
 import org.apache.dubbo.registry.client.ServiceInstance;
@@ -179,6 +180,18 @@ public class ZookeeperServiceDiscovery extends AbstractServiceDiscovery {
         });
     }
 
+    @Override
+    public boolean isAvailable() {
+        //Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
+        return !isDestroy() && isConnected() && CollectionUtils.isNotEmpty(getServices());
+    }
+
+    private boolean isConnected() {
+        if (curatorFramework == null || curatorFramework.getZookeeperClient() == null) {
+            return false;
+        }
+        return curatorFramework.getZookeeperClient().isConnected();
+    }
 
     private void doInServiceRegistry(ThrowableConsumer<org.apache.curator.x.discovery.ServiceDiscovery> consumer) {
         ThrowableConsumer.execute(serviceDiscovery, s -> consumer.accept(s));