You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@servicecomb.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/07/03 00:57:00 UTC

[jira] [Commented] (SCB-705) When consumer invoke an unregistered provider, it will never find this provider even the provider is registered later

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

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

liubao68 closed pull request #785: [SCB-705] consumer don't cache ReferenceConfig of an unregistered provider
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/785
 
 
   

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/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ConsumerProviderManager.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ConsumerProviderManager.java
index 5dc8cf8eb..69da5f29f 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ConsumerProviderManager.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/ConsumerProviderManager.java
@@ -25,6 +25,7 @@
 import org.apache.servicecomb.core.ConsumerProvider;
 import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
 import org.apache.servicecomb.serviceregistry.consumer.AppManager;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule;
 import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -52,7 +53,18 @@ public void init() throws Exception {
   }
 
   public ReferenceConfig createReferenceConfig(String microserviceName, String versionRule, String transport) {
-    return new ReferenceConfig(appManager, microserviceName, versionRule, transport);
+    ReferenceConfig referenceConfig = new ReferenceConfig(appManager, microserviceName, versionRule, transport);
+
+    MicroserviceVersionRule microserviceVersionRule = referenceConfig.getMicroserviceVersionRule();
+    if (microserviceVersionRule.getLatestMicroserviceVersion() == null) {
+      throw new IllegalStateException(
+          String.format(
+              "Probably invoke a service before it is registered, or no instance found for it, appId=%s, name=%s",
+              microserviceVersionRule.getAppId(),
+              microserviceVersionRule.getMicroserviceName()));
+    }
+
+    return referenceConfig;
   }
 
   public ReferenceConfig createReferenceConfig(String microserviceName) {
@@ -72,7 +84,7 @@ public ReferenceConfig createReferenceConfig(String microserviceName) {
         .getStringProperty(key + ".transport", defaultTransport)
         .get();
 
-    return new ReferenceConfig(appManager, microserviceName, versionRule, transport);
+    return createReferenceConfig(microserviceName, versionRule, transport);
   }
 
   public ReferenceConfig getReferenceConfig(String microserviceName) {
diff --git a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestConsumerProviderManager.java b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestConsumerProviderManager.java
index bdc23bb80..fabdcb6ad 100644
--- a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestConsumerProviderManager.java
+++ b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestConsumerProviderManager.java
@@ -17,6 +17,8 @@
 
 package org.apache.servicecomb.core.provider.consumer;
 
+import static org.junit.Assert.fail;
+
 import java.util.Collections;
 
 import org.apache.servicecomb.core.Const;
@@ -24,16 +26,21 @@
 import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.consumer.AppManager;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion;
+import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule;
 import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
 import com.google.common.eventbus.EventBus;
 
 import mockit.Expectations;
+import mockit.Mock;
+import mockit.MockUp;
 import mockit.Mocked;
 
 public class TestConsumerProviderManager {
@@ -71,6 +78,13 @@ private ReferenceConfig mockCreateReferenceConfig() {
         result = Collections.emptyList();
       }
     };
+
+    new MockUp<MicroserviceVersionRule>() {
+      @Mock
+      MicroserviceVersion getLatestMicroserviceVersion() {
+        return Mockito.mock(MicroserviceVersion.class);
+      }
+    };
     return consumerProviderManager.createReferenceConfig("app:ms");
   }
 
@@ -96,4 +110,43 @@ public void createReferenceConfig_config() {
     Assert.assertEquals("1.0.0+", referenceConfig.getMicroserviceVersionRule().getVersionRule().getVersionRule());
     Assert.assertEquals(Const.RESTFUL, referenceConfig.getTransport());
   }
+
+  @Test
+  public void createReferenceConfig_ProviderNotFound() {
+    EventBus eventBus = new EventBus();
+    AppManager appManager = new AppManager(eventBus);
+
+    ConsumerProviderManager consumerProviderManager = new ConsumerProviderManager();
+    consumerProviderManager.setAppManager(appManager);
+
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.findServiceInstances(anyString, anyString, DefinitionConst.VERSION_RULE_ALL, null);
+        result = Collections.emptyList();
+      }
+    };
+
+    new MockUp<MicroserviceVersionRule>() {
+      @Mock
+      String getAppId() {
+        return "aId";
+      }
+
+      @Mock
+      String getMicroserviceName() {
+        return "ms";
+      }
+    };
+
+    try {
+      consumerProviderManager.createReferenceConfig("app:ms");
+      fail("an IllegalStateException is expected!");
+    } catch (Exception e) {
+      Assert.assertEquals(IllegalStateException.class, e.getClass());
+      Assert.assertEquals(
+          "Probably invoke a service before it is registered, or no instance found for it, appId=aId, name=ms",
+          e.getMessage());
+      e.printStackTrace();
+    }
+  }
 }


 

----------------------------------------------------------------
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


> When consumer invoke an unregistered provider, it will never find this provider even the provider is registered later
> ---------------------------------------------------------------------------------------------------------------------
>
>                 Key: SCB-705
>                 URL: https://issues.apache.org/jira/browse/SCB-705
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Java-Chassis
>            Reporter: YaoHaishi
>            Assignee: YaoHaishi
>            Priority: Major
>
> Before consumer invoke provider, it will create a ReferenceConfig for this provider. But when the provider is not registered, it will get a wrong ReferenceConfig and cache it, so it will never query the service-center again to get the provider microservice information, and an Exception is always thrown to say that the provider cannot be found.



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