Skip to content

Commit

Permalink
bugFix/fix is available timeout (#12796)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xieshouyu authored Jul 29, 2023
1 parent 97227e4 commit 2488b08
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ public Set<String> getServices() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand Down Expand Up @@ -94,6 +95,13 @@ default long getDelay() {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public ServiceDiscovery getServiceDiscovery() {
*/
protected ServiceDiscovery createServiceDiscovery(URL registryURL) {
return getServiceDiscovery(registryURL.addParameter(INTERFACE_KEY, ServiceDiscovery.class.getName())
.removeParameter(REGISTRY_TYPE_KEY));
.removeParameter(REGISTRY_TYPE_KEY));
}

/**
Expand Down Expand Up @@ -278,11 +278,8 @@ public List<URL> lookup(URL url) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -179,6 +180,18 @@ public void removeServiceInstancesChangedListener(ServiceInstancesChangedListene
});
}

@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));
Expand Down

0 comments on commit 2488b08

Please sign in to comment.