[ https://issues.apache.org/jira/browse/SCB-444?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16429696#comment-16429696 ]
ASF GitHub Bot commented on SCB-444: ------------------------------------ liubao68 closed pull request #630: [SCB-444]try to optimize autoDiscovery function URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/630 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..0d5db0a6b 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(); } // we have to do this operation after the first time setup has already done @@ -98,6 +105,7 @@ private IpPort getAvailableAddress(int index) { 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 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: us...@infra.apache.org > try to optimize autodiscovery function > -------------------------------------- > > Key: SCB-444 > URL: https://issues.apache.org/jira/browse/SCB-444 > Project: Apache ServiceComb > Issue Type: Improvement > Components: Java-Chassis > Affects Versions: java-chassis-1.0.0-m2 > Reporter: WeiChao > Assignee: TaoMeng > Priority: Minor > Fix For: java-chassis-1.0.0-m2 > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)