laijianbin closed pull request #793: [SCB-718] If auto discovery failed , will
cause a dead cycle
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/793
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
index 0d5db0a6b..99ccfbc17 100644
---
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
+++
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/IpPortManager.java
@@ -23,6 +23,11 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.servicecomb.foundation.common.net.IpPort;
@@ -50,8 +55,18 @@
private boolean autoDiscoveryInited = false;
+ private ScheduledThreadPoolExecutor taskPool;
+
+ private InstanceCache cache;
+
private int maxRetryTimes;
+ private InstanceCacheTask instanceCacheTask = new InstanceCacheTask();
+
+ public void setAutoDiscoveryInited(boolean autoDiscoveryInited) {
+ this.autoDiscoveryInited = autoDiscoveryInited;
+ }
+
public int getMaxRetryTimes() {
return maxRetryTimes;
}
@@ -73,11 +88,45 @@ public IpPortManager(ServiceRegistryConfig
serviceRegistryConfig, InstanceCacheM
// we have to do this operation after the first time setup has already done
public void initAutoDiscovery() {
if (!autoDiscoveryInited &&
this.serviceRegistryConfig.isRegistryAutoDiscovery()) {
- instanceCacheManager.getOrCreate(REGISTRY_APP_ID,
+ cache = instanceCacheManager.getOrCreate(REGISTRY_APP_ID,
+ REGISTRY_SERVICE_NAME,
+ DefinitionConst.VERSION_RULE_LATEST);
+ if (cache.getInstanceMap().size() > 0) {
+ setAutoDiscoveryInited(true);
+ } else {
+ setAutoDiscoveryInited(false);
+ taskPool = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable task) {
+ return new Thread(task, "Instance Cache Task");
+ }
+ }, new RejectedExecutionHandler() {
+ @Override
+ public void rejectedExecution(Runnable task, ThreadPoolExecutor
executor) {
+ LOGGER.warn("Too many pending tasks, reject " +
task.getClass().getName());
+ }
+ });
+ taskPool.scheduleAtFixedRate(instanceCacheTask,
+ ServiceRegistryConfig.getInstanceCacheInterval(),
+ ServiceRegistryConfig.getInstanceCacheInterval(),
+ TimeUnit.SECONDS);
+ }
+ }
+ }
+
+ class InstanceCacheTask implements Runnable {
+
+ @Override
+ public void run() {
+ cache = instanceCacheManager.getOrCreate(REGISTRY_APP_ID,
REGISTRY_SERVICE_NAME,
DefinitionConst.VERSION_RULE_LATEST);
- autoDiscoveryInited = true;
+ if (cache.getInstanceMap().size() > 0) {
+ setAutoDiscoveryInited(true);
+ taskPool.shutdownNow();
+ }
}
+
}
public IpPort getNextAvailableAddress(IpPort failedIpPort) {
diff --git
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
index 23824a800..89f11045f 100644
---
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
+++
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/config/ServiceRegistryConfig.java
@@ -51,6 +51,8 @@
private static final int DEFAULT_REQUEST_HEARTBEAT_TIMEOUT_IN_MS = 3000;
private static final int DEFAULT_CHECK_INTERVAL_IN_S = 30;
+
+ private static final int DEFAULT_Cache_INTERVAL_IN_S = 30;
private static final int DEFAULT_CHECK_TIMES = 3;
@@ -197,6 +199,15 @@ public int getHeartbeatInterval() {
int interval = property.get();
return interval < 0 ? DEFAULT_CHECK_INTERVAL_IN_S : interval;
}
+
+ public static int getInstanceCacheInterval() {
+ DynamicIntProperty property =
+ DynamicPropertyFactory.getInstance()
+ .getIntProperty("servicecomb.service.registry.cache.interval",
+ DEFAULT_Cache_INTERVAL_IN_S);
+ int interval = property.get();
+ return interval < 0 ? DEFAULT_Cache_INTERVAL_IN_S : interval;
+ }
public int getInstancePullInterval() {
DynamicIntProperty property =
diff --git
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
index 6e3b933a5..d29b09357 100644
---
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
+++
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java
@@ -26,7 +26,7 @@
import
org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl;
import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig;
import
org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition;
-import org.apache.servicecomb.serviceregistry.task.MicroserviceRegisterTask;
+import
org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask;
import org.apache.servicecomb.serviceregistry.task.event.PeriodicPullEvent;
import
org.apache.servicecomb.serviceregistry.task.event.PullMicroserviceVersionsInstancesEvent;
import org.apache.servicecomb.serviceregistry.task.event.ShutdownEvent;
@@ -96,7 +96,7 @@ public void
onPullMicroserviceVersionsInstancesEvent(PullMicroserviceVersionsIns
}
@Subscribe
- public void onMicroserviceRegistryTask(MicroserviceRegisterTask event) {
+ public void onMicroserviceRegistryTask(MicroserviceInstanceRegisterTask
event) {
if (event.isRegistered()) {
ipPortManager.initAutoDiscovery();
}
diff --git
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 363668286..cea91464d 100644
---
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -176,6 +176,7 @@ private boolean registerSchemas() {
checkRemainingSchema(scSchemaMap);
schemaIdSetMatch = true;
+ this.registered = true;
return true;
}
diff --git
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
index 771b2d9e7..24006e822 100644
---
a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
+++
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/TestIpPortManager.java
@@ -60,7 +60,7 @@ public void setup() {
manager = serviceRegistry.getIpPortManager();
}
- @Test
+ @Test
public void testGetAvailableAddress(@Injectable ServiceRegistryConfig config,
@Injectable InstanceCacheManager cacheManager,
@Injectable InstanceCache cache) {
@@ -122,6 +122,8 @@ public void testGetAvailableAddress(@Injectable
ServiceRegistryConfig config,
};
manager.initAutoDiscovery();
+ manager.setAutoDiscoveryInited(true);
+
IpPort address4 = manager.getNextAvailableAddress(address3);
if (address1.getPort() == 9980) {
Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
@@ -136,7 +138,7 @@ public void testGetAvailableAddress(@Injectable
ServiceRegistryConfig config,
Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
Assert.assertEquals(9980, address5.getPort());
}
-
+
@Test
public void testCreateServiceRegistryCacheWithInstanceCache() {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services