You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/09/06 01:28:49 UTC
[incubator-servicecomb-java-chassis] branch master updated:
[SCB-888] avoid switch SCBEngine status to up in the wrong time
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 4d14798 [SCB-888] avoid switch SCBEngine status to up in the wrong time
4d14798 is described below
commit 4d1479829eb2d58cbdbcba5128dcc99437787a69
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Sep 4 09:18:32 2018 +0800
[SCB-888] avoid switch SCBEngine status to up in the wrong time
---
.../org/apache/servicecomb/core/SCBEngine.java | 58 ++++++++++++++++++++--
.../org/apache/servicecomb/core/TestSCBEngine.java | 5 +-
.../integration/PojoReferenceIntegrationTest.java | 2 +
.../demo/jaxrs/tests/RawJaxrsIntegrationTest.java | 3 ++
.../jaxrs/tests/JaxrsSpringIntegrationTest.java | 2 +
.../demo/jaxrs/tests/JaxrsSpringMain.java | 3 ++
6 files changed, 68 insertions(+), 5 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 dcd877f..863d839 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
import javax.ws.rs.core.Response.Status;
@@ -51,11 +52,16 @@ import org.springframework.util.StringUtils;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
+import com.netflix.config.DynamicPropertyFactory;
// TODO: should not depend on spring, that will make integration more flexible
public class SCBEngine {
private static final Logger LOGGER = LoggerFactory.getLogger(SCBEngine.class);
+ static final String CFG_KEY_WAIT_UP_TIMEOUT = "servicecomb.boot.waitUp.timeoutInMilliseconds";
+
+ static final long DEFAULT_WAIT_UP_TIMEOUT = 10_000;
+
private ProducerProviderManager producerProviderManager;
private ConsumerProviderManager consumerProviderManager;
@@ -192,8 +198,10 @@ public class SCBEngine {
if (SCBStatus.DOWN.equals(status)) {
try {
doInit();
- status = SCBStatus.UP;
- } catch (Exception e) {
+ waitStatusUp();
+ } catch (TimeoutException e) {
+ LOGGER.warn("{}", e.getMessage());
+ } catch (Throwable e) {
destroy();
status = SCBStatus.FAILED;
throw new IllegalStateException("ServiceComb init failed.", e);
@@ -201,7 +209,6 @@ public class SCBEngine {
}
}
-
private void doInit() throws Exception {
status = SCBStatus.STARTING;
@@ -242,7 +249,7 @@ public class SCBEngine {
* even some step throw exception, must catch it and go on, otherwise shutdown process will be broken.
*/
public synchronized void destroy() {
- if (SCBStatus.UP.equals(status)) {
+ if (SCBStatus.UP.equals(status) || SCBStatus.STARTING.equals(status)) {
LOGGER.info("ServiceComb is closing now...");
doDestroy();
status = SCBStatus.DOWN;
@@ -318,4 +325,47 @@ public class SCBEngine {
public void setProducerMicroserviceMeta(MicroserviceMeta producerMicroserviceMeta) {
this.producerMicroserviceMeta = producerMicroserviceMeta;
}
+
+ /**
+ * better to subscribe EventType.AFTER_REGISTRY by BootListener<br>
+ * but in some simple scenes, just block and wait is enough.
+ */
+ public void waitStatusUp() throws InterruptedException, TimeoutException {
+ long msWait = DynamicPropertyFactory.getInstance().getLongProperty(CFG_KEY_WAIT_UP_TIMEOUT, DEFAULT_WAIT_UP_TIMEOUT)
+ .get();
+ waitStatusUp(msWait);
+ }
+
+ /**
+ * better to subscribe EventType.AFTER_REGISTRY by BootListener<br>
+ * but in some simple scenes, just block and wait is enough.
+ */
+ public void waitStatusUp(long msWait) throws InterruptedException, TimeoutException {
+ if (msWait <= 0) {
+ LOGGER.info("Give up waiting for status up, wait timeout milliseconds={}.", msWait);
+ return;
+ }
+
+ LOGGER.info("Waiting for status up. timeout: {}ms", msWait);
+ long start = System.currentTimeMillis();
+ for (; ; ) {
+ SCBStatus currentStatus = getStatus();
+ switch (currentStatus) {
+ case DOWN:
+ case FAILED:
+ throw new IllegalStateException("Failed to wait status up, real status: " + currentStatus);
+ case UP:
+ LOGGER.info("Status already changed to up.");
+ return;
+ default:
+ break;
+ }
+
+ TimeUnit.MILLISECONDS.sleep(100);
+ if (System.currentTimeMillis() - start > msWait) {
+ throw new TimeoutException(
+ String.format("Timeout to wait status up, timeout: %dms, last status: %s", msWait, currentStatus));
+ }
+ }
+ }
}
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
index ea6371b..fcc0e0f 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
@@ -28,6 +28,7 @@ import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
import org.apache.servicecomb.core.transport.TransportManager;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import org.apache.servicecomb.foundation.vertx.VertxUtils;
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.consumer.AppManager;
@@ -81,9 +82,11 @@ public class TestSCBEngine {
engine.setTransportManager(transportManager);
engine.setSchemaListenerManager(schemaListenerManager);
+ ArchaiusUtils.setProperty(SCBEngine.CFG_KEY_WAIT_UP_TIMEOUT, 0);
engine.init();
+ ArchaiusUtils.updateProperty(SCBEngine.CFG_KEY_WAIT_UP_TIMEOUT, null);
- Assert.assertEquals(SCBStatus.UP, engine.getStatus());
+ Assert.assertEquals(SCBStatus.STARTING, engine.getStatus());
engine.destroy();
diff --git a/demo/demo-pojo/pojo-tests/src/test/java/org/apache/servicecomb/demo/integration/PojoReferenceIntegrationTest.java b/demo/demo-pojo/pojo-tests/src/test/java/org/apache/servicecomb/demo/integration/PojoReferenceIntegrationTest.java
index 0a3c4b2..9706e33 100644
--- a/demo/demo-pojo/pojo-tests/src/test/java/org/apache/servicecomb/demo/integration/PojoReferenceIntegrationTest.java
+++ b/demo/demo-pojo/pojo-tests/src/test/java/org/apache/servicecomb/demo/integration/PojoReferenceIntegrationTest.java
@@ -17,6 +17,7 @@
package org.apache.servicecomb.demo.integration;
+import static org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl.LOCAL_REGISTRY_FILE_KEY;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@@ -31,6 +32,7 @@ public class PojoReferenceIntegrationTest {
@BeforeClass
public static void setUp() throws Exception {
+ System.setProperty(LOCAL_REGISTRY_FILE_KEY, "notExistJustForceLocal");
SomePojoTestMain.main(new String[0]);
}
diff --git a/integration-tests/jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/RawJaxrsIntegrationTest.java b/integration-tests/jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/RawJaxrsIntegrationTest.java
index 179f2e8..74fb18e 100644
--- a/integration-tests/jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/RawJaxrsIntegrationTest.java
+++ b/integration-tests/jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/RawJaxrsIntegrationTest.java
@@ -17,12 +17,15 @@
package org.apache.servicecomb.demo.jaxrs.tests;
+import static org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl.LOCAL_REGISTRY_FILE_KEY;
+
import org.junit.BeforeClass;
public class RawJaxrsIntegrationTest extends JaxrsIntegrationTestBase {
@BeforeClass
public static void setUp() throws Exception {
+ System.setProperty(LOCAL_REGISTRY_FILE_KEY, "notExistJustForceLocal");
JaxrsTestMain.main(new String[0]);
}
}
diff --git a/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringIntegrationTest.java b/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringIntegrationTest.java
index 8bb37bd..834dff5 100644
--- a/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringIntegrationTest.java
+++ b/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringIntegrationTest.java
@@ -17,6 +17,7 @@
package org.apache.servicecomb.demo.jaxrs.tests;
+import static org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl.LOCAL_REGISTRY_FILE_KEY;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
@@ -34,6 +35,7 @@ public class JaxrsSpringIntegrationTest extends JaxrsIntegrationTestBase {
@BeforeClass
public static void setUp() {
System.setProperty("property.test5", "from_system_property");
+ System.setProperty(LOCAL_REGISTRY_FILE_KEY, "notExistJustForceLocal");
}
@AfterClass
diff --git a/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringMain.java b/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringMain.java
index 0eeaf7e..3917fdd 100644
--- a/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringMain.java
+++ b/integration-tests/spring-jaxrs-tests/src/test/java/org/apache/servicecomb/demo/jaxrs/tests/JaxrsSpringMain.java
@@ -17,6 +17,8 @@
package org.apache.servicecomb.demo.jaxrs.tests;
+import static org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl.LOCAL_REGISTRY_FILE_KEY;
+
import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -25,6 +27,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableServiceComb
public class JaxrsSpringMain {
public static void main(final String[] args) throws Exception {
+ System.setProperty(LOCAL_REGISTRY_FILE_KEY, "notExistJustForceLocal");
SpringApplication.run(JaxrsSpringMain.class, args);
}
}