From 740be9ca0d9a3d44e0ce9ab5c26e173305b5ae97 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Mon, 7 Jan 2019 16:04:59 +0800 Subject: [PATCH 1/7] fix client reconnect offline provider. --- .../remoting/transport/AbstractClient.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index ed913d2a0f7..4c052f92d6b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -152,6 +152,25 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { + + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return; + } + if (!isConnected()) { connect(); } else { @@ -344,14 +363,14 @@ public void reconnect() throws RemotingException { @Override public void close() { try { - if (executor != null) { - ExecutorUtil.shutdownNow(executor, 100); - } + super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { - super.close(); + if (executor != null) { + ExecutorUtil.shutdownNow(executor, 100); + } } catch (Throwable e) { logger.warn(e.getMessage(), e); } From 5fdd75b6932f8192bb2bc0ead13e54a4e0acc5c2 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Tue, 8 Jan 2019 00:12:30 +0800 Subject: [PATCH 2/7] refactor cancel future. --- .../remoting/transport/AbstractClient.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index 4c052f92d6b..cfb3772d79e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -152,24 +152,7 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { - - /** - * If the provider service is detected offline, - * the client should not attempt to connect again. - * - * issue: https://github.com/apache/incubator-dubbo/issues/3158 - */ - if(isClosed()) { - ScheduledFuture future = reconnectExecutorFuture; - if(future != null && !future.isCancelled()){ - /** - * Client has been destroyed and - * scheduled task should be cancelled. - */ - future.cancel(true); - } - return; - } + if (cancelFutureIfOffline()) return; if (!isConnected()) { connect(); @@ -191,7 +174,29 @@ public void run() { } } } + + private boolean cancelFutureIfOffline() { + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return true; + } + return false; + } }; + reconnectExecutorFuture = reconnectExecutorService.scheduleWithFixedDelay(connectStatusCheckCommand, reconnect, reconnect, TimeUnit.MILLISECONDS); } } From 1f2c3d62f38475ec1ffed91d306f17fc7461d5c5 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Mon, 7 Jan 2019 16:04:59 +0800 Subject: [PATCH 3/7] fix client reconnect offline provider. --- .../remoting/transport/AbstractClient.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index ed913d2a0f7..4c052f92d6b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -152,6 +152,25 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { + + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return; + } + if (!isConnected()) { connect(); } else { @@ -344,14 +363,14 @@ public void reconnect() throws RemotingException { @Override public void close() { try { - if (executor != null) { - ExecutorUtil.shutdownNow(executor, 100); - } + super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { - super.close(); + if (executor != null) { + ExecutorUtil.shutdownNow(executor, 100); + } } catch (Throwable e) { logger.warn(e.getMessage(), e); } From 13792107d703aaffcbea90d80bc8ef00b57b0311 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Tue, 8 Jan 2019 00:12:30 +0800 Subject: [PATCH 4/7] refactor cancel future. --- .../remoting/transport/AbstractClient.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index 4c052f92d6b..cfb3772d79e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -152,24 +152,7 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { - - /** - * If the provider service is detected offline, - * the client should not attempt to connect again. - * - * issue: https://github.com/apache/incubator-dubbo/issues/3158 - */ - if(isClosed()) { - ScheduledFuture future = reconnectExecutorFuture; - if(future != null && !future.isCancelled()){ - /** - * Client has been destroyed and - * scheduled task should be cancelled. - */ - future.cancel(true); - } - return; - } + if (cancelFutureIfOffline()) return; if (!isConnected()) { connect(); @@ -191,7 +174,29 @@ public void run() { } } } + + private boolean cancelFutureIfOffline() { + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return true; + } + return false; + } }; + reconnectExecutorFuture = reconnectExecutorService.scheduleWithFixedDelay(connectStatusCheckCommand, reconnect, reconnect, TimeUnit.MILLISECONDS); } } From 506d90ca03a471176ec5b6c0e50e6feb7a95adb2 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Mon, 7 Jan 2019 16:04:59 +0800 Subject: [PATCH 5/7] fix client reconnect offline provider. --- .../remoting/transport/AbstractClient.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index 9b1c9ba8daa..e2f72b986e8 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -153,6 +153,25 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { + + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return; + } + if (!isConnected()) { connect(); } else { @@ -345,14 +364,14 @@ public void reconnect() throws RemotingException { @Override public void close() { try { - if (executor != null) { - ExecutorUtil.shutdownNow(executor, 100); - } + super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { - super.close(); + if (executor != null) { + ExecutorUtil.shutdownNow(executor, 100); + } } catch (Throwable e) { logger.warn(e.getMessage(), e); } From 29ef3ad1af443bad30ab9ba15efcce88745405bf Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Tue, 8 Jan 2019 00:12:30 +0800 Subject: [PATCH 6/7] refactor cancel future. --- .../remoting/transport/AbstractClient.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java index e2f72b986e8..7280b508f5e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractClient.java @@ -153,24 +153,7 @@ private synchronized void initConnectStatusCheckCommand() { @Override public void run() { try { - - /** - * If the provider service is detected offline, - * the client should not attempt to connect again. - * - * issue: https://github.com/apache/incubator-dubbo/issues/3158 - */ - if(isClosed()) { - ScheduledFuture future = reconnectExecutorFuture; - if(future != null && !future.isCancelled()){ - /** - * Client has been destroyed and - * scheduled task should be cancelled. - */ - future.cancel(true); - } - return; - } + if (cancelFutureIfOffline()) return; if (!isConnected()) { connect(); @@ -192,7 +175,29 @@ public void run() { } } } + + private boolean cancelFutureIfOffline() { + /** + * If the provider service is detected offline, + * the client should not attempt to connect again. + * + * issue: https://github.com/apache/incubator-dubbo/issues/3158 + */ + if(isClosed()) { + ScheduledFuture future = reconnectExecutorFuture; + if(future != null && !future.isCancelled()){ + /** + * Client has been destroyed and + * scheduled task should be cancelled. + */ + future.cancel(true); + } + return true; + } + return false; + } }; + reconnectExecutorFuture = reconnectExecutorService.scheduleWithFixedDelay(connectStatusCheckCommand, reconnect, reconnect, TimeUnit.MILLISECONDS); } } From d671365bd5720c3d1b1874baa1d4730fef688942 Mon Sep 17 00:00:00 2001 From: zonghaishang Date: Mon, 21 Jan 2019 23:36:40 +0800 Subject: [PATCH 7/7] fix unregister when client destroyed --- .../integration/RegistryDirectory.java | 21 ++++++++++++++++++- .../integration/RegistryProtocol.java | 6 ++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java index 41b55b2d59e..8ec3bcbee1f 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java @@ -23,10 +23,10 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.Assert; +import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.UrlUtils; -import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.configcenter.DynamicConfiguration; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.Registry; @@ -90,6 +90,8 @@ public class RegistryDirectory extends AbstractDirectory implements Notify private volatile URL overrideDirectoryUrl; // Initialization at construction time, assertion not null, and always assign non null value + private volatile URL registeredConsumerUrl; + /** * override rules * Priority: override>-D>consumer>provider @@ -158,6 +160,15 @@ public void destroy() { if (isDestroyed()) { return; } + + // unregister. + try { + if (getRegisteredConsumerUrl() != null && registry != null && registry.isAvailable()) { + registry.unregister(getRegisteredConsumerUrl()); + } + } catch (Throwable t) { + logger.warn("unexpected error when unregister service " + serviceKey + "from registry" + registry.getUrl(), t); + } // unsubscribe. try { if (getConsumerUrl() != null && registry != null && registry.isAvailable()) { @@ -565,6 +576,14 @@ public URL getUrl() { return this.overrideDirectoryUrl; } + public URL getRegisteredConsumerUrl() { + return registeredConsumerUrl; + } + + public void setRegisteredConsumerUrl(URL registeredConsumerUrl) { + this.registeredConsumerUrl = registeredConsumerUrl; + } + @Override public boolean isAvailable() { if (isDestroyed()) { diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java index 4cb5efd6f41..ddedadcbc39 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java @@ -374,7 +374,8 @@ private Invoker doRefer(Cluster cluster, Registry registry, Class type Map parameters = new HashMap(directory.getUrl().getParameters()); URL subscribeUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters); if (!ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(REGISTER_KEY, true)) { - registry.register(getRegisteredConsumerUrl(subscribeUrl, url)); + directory.setRegisteredConsumerUrl(getRegisteredConsumerUrl(subscribeUrl, url)); + registry.register(directory.getRegisteredConsumerUrl()); } directory.buildRouterChain(subscribeUrl); directory.subscribe(subscribeUrl.addParameter(CATEGORY_KEY, @@ -385,7 +386,7 @@ private Invoker doRefer(Cluster cluster, Registry registry, Class type return invoker; } - private URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) { + public URL getRegisteredConsumerUrl(final URL consumerUrl, URL registryUrl) { if (!registryUrl.getParameter(SIMPLE_CONSUMER_CONFIG_KEY, false)) { return consumerUrl.addParameters(CATEGORY_KEY, CONSUMERS_CATEGORY, CHECK_KEY, String.valueOf(false)); @@ -598,6 +599,7 @@ protected void notifyOverrides() { overrideListeners.values().forEach(listener -> ((OverrideListener) listener).doOverrideIfNecessary()); } } + /** * exporter proxy, establish the corresponding relationship between the returned exporter and the exporter * exported by the protocol, and can modify the relationship at the time of override.