This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push: new 49d8249 [SCB-444]try to optimize autoDiscovery function 49d8249 is described below commit 49d8249b90c5b303725def283b10b29cdb452f59 Author: m00416667 <mengt...@huawei.com> AuthorDate: Sat Mar 31 15:47:42 2018 +0800 [SCB-444]try to optimize autoDiscovery function --- .../servicecomb/serviceregistry/client/IpPortManager.java | 8 ++++++++ .../serviceregistry/client/http/RequestContext.java | 12 ++++++------ .../client/http/ServiceRegistryClientImpl.java | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) 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 b3bb461..0d5db0a 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 @@ public class IpPortManager { 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 class IpPortManager { } int initialIndex = new Random().nextInt(defaultIpPort.size()); currentAvailableIndex = new AtomicInteger(initialIndex); + maxRetryTimes = defaultIpPort.size(); } // we have to do this operation after the first time setup has already done @@ -98,6 +105,7 @@ public class IpPortManager { currentAvailableIndex.set(0); return defaultIpPort.get(0); } + maxRetryTimes = defaultIpPort.size() + endpoints.size(); CacheEndpoint nextEndpoint = endpoints.get(index - defaultIpPort.size()); return new URIEndpointObject(nextEndpoint.getEndpoint()); } 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 dbc5e55..91c858d 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 @@ public class RequestContext { 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 class RequestContext { 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 ab7f597..6173a17 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 final class ServiceRegistryClientImpl implements ServiceRegistryClient { 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 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { 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 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { 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 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { HttpClientResponse response = restResponse.getResponse(); if (response == null) { // 请求失败,触发请求SC的其他实例 - if (!requestContext.isRetry()) { + if (requestContext.getRetryTimes() <= ipPortManager.getMaxRetryTimes()) { retry(requestContext, syncHandlerForInstances(countDownLatch, mInstances)); } else { countDownLatch.countDown(); -- To stop receiving notification emails like this one, please contact liu...@apache.org.