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

yaohaishi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git

commit be1660f50d7965025d79b9389bff6075c88c0f34
Author: yhs0092 <[email protected]>
AuthorDate: Mon Feb 17 01:11:33 2020 +0800

    [SCB-1691] turn instance status to DOWN and wait for a period when shutdown
---
 .../org/apache/servicecomb/core/SCBEngine.java     | 41 ++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java 
b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index e704efe..68903e6 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -48,6 +48,7 @@ import 
org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig
 import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
 import org.apache.servicecomb.core.transport.TransportManager;
 import org.apache.servicecomb.foundation.common.VendorExtensions;
+import 
org.apache.servicecomb.foundation.common.concurrency.SuppressedRunnableWrapper;
 import 
org.apache.servicecomb.foundation.common.event.EnableExceptionPropagation;
 import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.common.log.LogMarkerLeakFixUtils;
@@ -55,6 +56,8 @@ import 
org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.vertx.VertxUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstanceStatus;
 import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions;
 import 
org.apache.servicecomb.serviceregistry.definition.MicroserviceNameParser;
 import org.apache.servicecomb.serviceregistry.swagger.SwaggerLoader;
@@ -78,6 +81,10 @@ public class SCBEngine {
 
   static final long DEFAULT_WAIT_UP_TIMEOUT = 10_000;
 
+  static final String CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC = 
"servicecomb.boot.turnDown.waitInSeconds";
+
+  static final long DEFAULT_TURN_DOWN_STATUS_WAIT_SEC = 0;
+
   private static final Object initializationLock = new Object();
 
   private volatile static SCBEngine INSTANCE;
@@ -315,8 +322,8 @@ public class SCBEngine {
   private void printServiceInfo() {
     StringBuilder serviceInfo = new StringBuilder();
     serviceInfo.append("Service information is shown below:\n");
-    for (int i = 0; i < bootUpInformationCollectors.size(); i++) {
-      
serviceInfo.append(bootUpInformationCollectors.get(i).collect()).append('\n');
+    for (BootUpInformationCollector bootUpInformationCollector : 
bootUpInformationCollectors) {
+      serviceInfo.append(bootUpInformationCollector.collect()).append('\n');
     }
     LOGGER.info(serviceInfo.toString());
   }
@@ -387,6 +394,12 @@ public class SCBEngine {
     if (shutdownHook != null) {
       Runtime.getRuntime().removeShutdownHook(shutdownHook);
     }
+
+    //Step 0: turn down the status of this instance in service center,
+    // so that the consumers can remove this instance record in advance
+    turnDownInstanceStatus();
+    blockShutDownOperationForConsumerRefresh();
+
     //Step 1: notify all component stop invoke via BEFORE_CLOSE Event
     safeTriggerEvent(EventType.BEFORE_CLOSE);
 
@@ -418,6 +431,30 @@ public class SCBEngine {
     safeTriggerEvent(EventType.AFTER_CLOSE);
   }
 
+  private void turnDownInstanceStatus() {
+    RegistryUtils.executeOnEachServiceRegistry(sr -> new 
SuppressedRunnableWrapper(() -> {
+      MicroserviceInstance selfInstance = sr.getMicroserviceInstance();
+      sr.getServiceRegistryClient().updateMicroserviceInstanceStatus(
+          selfInstance.getServiceId(),
+          selfInstance.getInstanceId(),
+          MicroserviceInstanceStatus.DOWN);
+    }).run());
+  }
+
+  private void blockShutDownOperationForConsumerRefresh() {
+    try {
+      long turnDownWaitSeconds = DynamicPropertyFactory.getInstance()
+          .getLongProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC, 
DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)
+          .get();
+      if (turnDownWaitSeconds <= 0) {
+        return;
+      }
+      Thread.sleep(TimeUnit.SECONDS.toMillis(turnDownWaitSeconds));
+    } catch (InterruptedException e) {
+      LOGGER.warn("failed to block the shutdown procedure", e);
+    }
+  }
+
   private void validAllInvocationFinished() throws InterruptedException {
     long start = System.currentTimeMillis();
     while (true) {

Reply via email to