You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/03/31 03:09:25 UTC

[GitHub] mt-monster closed pull request #625: [SCB-444]try to optimize autoDiscovery function

mt-monster closed pull request #625: [SCB-444]try to optimize autoDiscovery function
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/625
 
 
   

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 b3bb4617d..6b6ef39e1 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
@@ -50,6 +50,12 @@
 
   private boolean autoDiscoveryInited = false;
 
+  private int maxRetryTimes;
+
+  public int getMaxRetryTimes() {
+    return maxRetryTimes;
+  }
+
   public IpPortManager(ServiceRegistryConfig serviceRegistryConfig, InstanceCacheManager instanceCacheManager) {
     this.serviceRegistryConfig = serviceRegistryConfig;
     this.instanceCacheManager = instanceCacheManager;
@@ -61,6 +67,7 @@ public IpPortManager(ServiceRegistryConfig serviceRegistryConfig, InstanceCacheM
     }
     int initialIndex = new Random().nextInt(defaultIpPort.size());
     currentAvailableIndex = new AtomicInteger(initialIndex);
+    maxRetryTimes = defaultIpPort.size() + (getDiscoveredIpPort() == null ? 0 : getDiscoveredIpPort().size());
   }
 
   // we have to do this operation after the first time setup has already done
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RequestContext.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RequestContext.java
index dbc5e5581..91c858da6 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RequestContext.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/RequestContext.java
@@ -33,8 +33,7 @@
 
   private RequestParam params;
 
-  // we can set max retry policies, now only try it twice
-  private boolean retry;
+  private int retryTimes = 0;
 
   public IpPort getIpPort() {
     return ipPort;
@@ -68,11 +67,12 @@ public void setParams(RequestParam params) {
     this.params = params;
   }
 
-  public boolean isRetry() {
-    return retry;
+
+  public int getRetryTimes() {
+    return retryTimes;
   }
 
-  public void setRetry(boolean retry) {
-    this.retry = retry;
+  public void incrementRetryTimes() {
+    ++this.retryTimes;
   }
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index ab7f59751..6173a1725 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -85,7 +85,7 @@ public void init() {
   private void retry(RequestContext requestContext, Handler<RestResponse> responseHandler) {
     LOGGER.warn("invoke service [{}] failed, retry.", requestContext.getUri());
     requestContext.setIpPort(ipPortManager.getNextAvailableAddress(requestContext.getIpPort()));
-    requestContext.setRetry(true);
+    requestContext.incrementRetryTimes();
     RestUtils.httpDo(requestContext, responseHandler);
   }
 
@@ -98,7 +98,7 @@ private void retry(RequestContext requestContext, Handler<RestResponse> response
       HttpClientResponse response = restResponse.getResponse();
       if (response == null) {
         // 请求失败,触发请求SC的其他实例
-        if (!requestContext.isRetry()) {
+        if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
           retry(requestContext, syncHandler(countDownLatch, cls, holder));
         } else {
           countDownLatch.countDown();
@@ -150,7 +150,7 @@ private void retry(RequestContext requestContext, Handler<RestResponse> response
       HttpClientResponse response = restResponse.getResponse();
       if (response == null) {
         // 请求失败,触发请求SC的其他实例
-        if (!requestContext.isRetry()) {
+        if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
           retry(requestContext, syncHandlerEx(countDownLatch, holder));
         } else {
           countDownLatch.countDown();
@@ -176,7 +176,7 @@ private void retry(RequestContext requestContext, Handler<RestResponse> response
       HttpClientResponse response = restResponse.getResponse();
       if (response == null) {
         // 请求失败,触发请求SC的其他实例
-        if (!requestContext.isRetry()) {
+        if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) {
           retry(requestContext, syncHandlerForInstances(countDownLatch, mInstances));
         } else {
           countDownLatch.countDown();


 

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