servicecomb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] WillemJiang closed pull request #552: [SCB-294]when send a request to a not exist microservice, edge will always try to find instances of it
Date Thu, 01 Jan 1970 00:00:00 GMT
WillemJiang closed pull request #552: [SCB-294]when send a request to a not exist microservice,
edge will always try to find instances of it
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/552
 
 
   

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/consumer/MicroserviceManager.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceManager.java
index 0e9351625..3181fde2b 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceManager.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceManager.java
@@ -41,11 +41,16 @@ public MicroserviceManager(AppManager appManager, String appId) {
   }
 
   public MicroserviceVersions getOrCreateMicroserviceVersions(String microserviceName) {
-    return versionsByName.computeIfAbsent(microserviceName, name -> {
+    MicroserviceVersions microserviceVersions = versionsByName.computeIfAbsent(microserviceName,
name -> {
       MicroserviceVersions instance = new MicroserviceVersions(appManager, appId, microserviceName);
       instance.submitPull();
       return instance;
     });
+    if (!microserviceVersions.isValidated()) {
+      // remove this microservice if it does not exist or not registered in order to get
it back when access it again
+      versionsByName.remove(microserviceName);
+    }
+    return microserviceVersions;
   }
 
   public MicroserviceVersionRule getOrCreateMicroserviceVersionRule(String microserviceName,
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
index f26ee057b..0b818df90 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java
@@ -19,7 +19,6 @@
 
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
@@ -64,6 +63,8 @@
   // only pendingPullCount is 0, then do a real pull 
   private AtomicInteger pendingPullCount = new AtomicInteger();
 
+  private boolean validated = false;
+
   public MicroserviceVersions(AppManager appManager, String appId, String microserviceName)
{
     this.appManager = appManager;
     this.appId = appId;
@@ -76,6 +77,10 @@ public MicroserviceVersions(AppManager appManager, String appId, String
microser
     appManager.getEventBus().register(this);
   }
 
+  public boolean isValidated() {
+    return validated;
+  }
+
   public String getAppId() {
     return appId;
   }
@@ -109,8 +114,6 @@ public void pullInstances() {
         DefinitionConst.VERSION_RULE_ALL,
         revision);
     if (microserviceInstances == null) {
-      // exception happens and try pull again later.
-      postPullInstanceEvent(TimeUnit.SECONDS.toMillis(1));
       return;
     }
     if (!microserviceInstances.isNeedRefresh()) {
@@ -125,13 +128,12 @@ public void pullInstances() {
   protected void safeSetInstances(List<MicroserviceInstance> pulledInstances, String
rev) {
     try {
       setInstances(pulledInstances, rev);
+      validated = true;
     } catch (Throwable e) {
       LOGGER.error("Failed to setInstances, appId={}, microserviceName={}.",
           getAppId(),
           getMicroserviceName(),
           e);
-      // exception happens and try pull again later.
-      postPullInstanceEvent(TimeUnit.SECONDS.toMillis(1));
     }
   }
 
@@ -210,9 +212,9 @@ public void onMicroserviceInstanceChanged(MicroserviceInstanceChangedEvent
chang
     //   if pull 1/2/3, and then delete 3, but "delete 3" received before pull result, will
have wrong 3.
     // EXPIRE::
     //   black/white config in SC changed, we must refresh all data from sc.
-    postPullInstanceEvent(TimeUnit.MILLISECONDS.toMillis(1));
+    postPullInstanceEvent(0);
   }
-  
+
   protected boolean isEventAccept(MicroserviceInstanceChangedEvent changedEvent) {
     return (appId.equals(changedEvent.getKey().getAppId()) &&
         microserviceName.equals(changedEvent.getKey().getServiceName())) ||
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceManager.java
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceManager.java
index 786a2d074..c87504c19 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceManager.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceManager.java
@@ -53,6 +53,8 @@
 
   MicroserviceManager microserviceManager = new MicroserviceManager(appManager, appId);
 
+  Map<String, MicroserviceVersions> cachedVersions = Deencapsulation.getField(microserviceManager,
"versionsByName");
+
   MicroserviceInstances microserviceInstances = null;
 
   FindInstancesResponse findInstancesResponse = null;
@@ -84,6 +86,49 @@ public void getOrCreateMicroserviceVersionRule() {
         microserviceManager.getOrCreateMicroserviceVersionRule(serviceName, versionRule);
     Assert.assertEquals("0.0.0+", microserviceVersionRule.getVersionRule().getVersionRule());
     Assert.assertNull(microserviceVersionRule.getLatestMicroserviceVersion());
+    Assert.assertEquals(1, cachedVersions.size());
+  }
+
+  @Test
+  public void testCreateRuleServiceTooMany() throws Exception {
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.findServiceInstances(appId, anyString, DefinitionConst.VERSION_RULE_ALL,
null);
+        result = microserviceInstances;
+      }
+    };
+
+    for (int i = 0; i < 1005; i++) {
+      MicroserviceVersionRule microserviceVersionRule =
+          microserviceManager.getOrCreateMicroserviceVersionRule(serviceName + i, versionRule);
+      Assert.assertEquals("0.0.0+", microserviceVersionRule.getVersionRule().getVersionRule());
+      Assert.assertNull(microserviceVersionRule.getLatestMicroserviceVersion());
+      if (i == 499) {
+        Thread.sleep(1);
+      }
+    }
+
+    Assert.assertEquals(1005, cachedVersions.size());
+    Assert.assertEquals("msName1004", cachedVersions.get("msName1004").getMicroserviceName());
+    Assert.assertEquals("msName1000", cachedVersions.get("msName1000").getMicroserviceName());
+    Assert.assertEquals("msName500", cachedVersions.get("msName500").getMicroserviceName());
+    Assert.assertEquals("msName0", cachedVersions.get("msName0").getMicroserviceName());
+  }
+
+  @Test
+  public void testCreateRuleServiceNotExists() {
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.findServiceInstances(appId, serviceName, DefinitionConst.VERSION_RULE_ALL,
null);
+        result = null;
+      }
+    };
+
+    MicroserviceVersionRule microserviceVersionRule =
+        microserviceManager.getOrCreateMicroserviceVersionRule(serviceName, versionRule);
+    Assert.assertEquals("0.0.0+", microserviceVersionRule.getVersionRule().getVersionRule());
+    Assert.assertNull(microserviceVersionRule.getLatestMicroserviceVersion());
+    Assert.assertEquals(0, cachedVersions.size());
   }
 
   @Test
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersions.java
b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersions.java
index be1e8c7e1..b336d8847 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersions.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/consumer/TestMicroserviceVersions.java
@@ -291,7 +291,8 @@ void setInstances(List<MicroserviceInstance> pulledInstances, String
rev) {
 
     microserviceVersions.safeSetInstances(null, null);
 
-    Assert.assertEquals(1, pendingPullCount.get());
+    Assert.assertEquals(0, pendingPullCount.get());
+    Assert.assertEquals(microserviceVersions.isValidated(), false);
   }
 
   public void checkIsEventAccept(MicroserviceKey key, boolean expected) {


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message