[ 
https://issues.apache.org/jira/browse/SCB-718?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16660236#comment-16660236
 ] 

ASF GitHub Bot commented on SCB-718:
------------------------------------

liubao68 closed pull request #937: [SCB-718]If auto discovery failed, will 
cause a dead cycle
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/937
 
 
   

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..1ff5be47d 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
@@ -52,6 +52,11 @@
 
   private int maxRetryTimes;
 
+
+  public void setAutoDiscoveryInited(boolean autoDiscoveryInited) {
+    this.autoDiscoveryInited = autoDiscoveryInited;
+  }
+
   public int getMaxRetryTimes() {
     return maxRetryTimes;
   }
@@ -73,10 +78,14 @@ 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,
+      InstanceCache cache = instanceCacheManager.getOrCreate(REGISTRY_APP_ID,
           REGISTRY_SERVICE_NAME,
           DefinitionConst.VERSION_RULE_LATEST);
-      autoDiscoveryInited = true;
+      if (cache.getInstanceMap().size() > 0) {
+        setAutoDiscoveryInited(true);
+      } else {
+        setAutoDiscoveryInited(false);
+      }
     }
   }
 
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 8eb4fb261..f9dbb4677 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
@@ -25,7 +25,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.MicroserviceInstanceHeartbeatTask;
 import org.apache.servicecomb.serviceregistry.task.event.PeriodicPullEvent;
 import 
org.apache.servicecomb.serviceregistry.task.event.PullMicroserviceVersionsInstancesEvent;
 import org.apache.servicecomb.serviceregistry.task.event.ShutdownEvent;
@@ -94,8 +94,8 @@ public void 
onPullMicroserviceVersionsInstancesEvent(PullMicroserviceVersionsIns
   }
 
   @Subscribe
-  public void onMicroserviceRegistryTask(MicroserviceRegisterTask event) {
-    if (event.isRegistered()) {
+  public void onMicroserviceHeartbeatTask(MicroserviceInstanceHeartbeatTask 
event) {
+    if (HeartbeatResult.SUCCESS.equals(event.getHeartbeatResult())) {
       ipPortManager.initAutoDiscovery();
     }
   }
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..d619b830c 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
@@ -121,22 +121,32 @@ public void testGetAvailableAddress(@Injectable 
ServiceRegistryConfig config,
       }
     };
 
-    manager.initAutoDiscovery();
+    manager.initAutoDiscovery();  //init result is false at first time
     IpPort address4 = manager.getNextAvailableAddress(address3);
     if (address1.getPort() == 9980) {
       Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
-      Assert.assertEquals(9982, address4.getPort());
+      Assert.assertEquals(9980, address4.getPort());
     } else {
       address4 = manager.getNextAvailableAddress(address1);
       Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
-      Assert.assertEquals(9982, address4.getPort());
+      Assert.assertEquals(9980, address4.getPort());
     }
 
     IpPort address5 = manager.getNextAvailableAddress(address4);
     Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
-    Assert.assertEquals(9980, address5.getPort());
-  }
+    Assert.assertEquals(9981, address5.getPort());
 
+    manager.setAutoDiscoveryInited(true);
+    IpPort address6 = manager.getNextAvailableAddress(address3);
+    if (address1.getPort() == 9980) {
+      Assert.assertEquals("127.0.0.1", address6.getHostOrIp());
+      Assert.assertEquals(9982, address6.getPort());
+    } else {
+      address6 = manager.getNextAvailableAddress(address1);
+      Assert.assertEquals("127.0.0.1", address6.getHostOrIp());
+      Assert.assertEquals(9982, address6.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]


> If auto discovery failed , will cause a dead cycle
> --------------------------------------------------
>
>                 Key: SCB-718
>                 URL: https://issues.apache.org/jira/browse/SCB-718
>             Project: Apache ServiceComb
>          Issue Type: Task
>            Reporter: laijianbin
>            Assignee: laijianbin
>            Priority: Major
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to