{"action":"created","comment":{"url":"https://api.github.com/repos/Netflix/eureka/pulls/comments/40610353","id":40610353,"diff_hunk":"@@ -0,0 +1,141 @@\n+/*\n+ * Copyright 2015 Netflix, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.netflix.discovery.shared.resolver;\n+\n+import java.util.List;\n+import java.util.concurrent.atomic.AtomicReference;\n+\n+import com.netflix.servo.annotations.DataSourceType;\n+import com.netflix.servo.annotations.Monitor;\n+import com.netflix.servo.monitor.Monitors;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import static com.netflix.discovery.EurekaClientNames.METRIC_RESOLVER_PREFIX;\n+\n+/**\n+ * A cluster resolver implementation that periodically creates a new {@link ClusterResolver} instance that\n+ * swaps the previous value. If the new resolver cannot be created or contains empty server list, the previous\n+ * one is used. Failed requests are retried using exponential back-off strategy.\n+ * <br/>\n+ * It provides more insight in form of additional logging and metrics, as it is supposed to be used as a top\n+ * level resolver.\n+ *\n+ * @author Tomasz Bak\n+ */\n+public class ReloadingClusterResolver implements ClusterResolver {\n+\n+    private static final Logger logger = LoggerFactory.getLogger(ReloadingClusterResolver.class);\n+\n+    private static final long MAX_RELOAD_INTERVAL_MULTIPLIER = 5;\n+\n+    private final ClusterResolverFactory factory;\n+    private final long reloadIntervalMs;\n+    private final long maxReloadIntervalMs;\n+\n+    private final AtomicReference<ClusterResolver> delegateRef;\n+\n+    private volatile long lastUpdateTime;\n+    private volatile long currentReloadIntervalMs;\n+\n+    // Metric timestamp, tracking last time when data were effectively changed.\n+    private volatile long lastReloadTimestamp = -1;\n+\n+    public ReloadingClusterResolver(final ClusterResolverFactory factory, final long reloadIntervalMs) {\n+        this.factory = factory;\n+        this.reloadIntervalMs = reloadIntervalMs;\n+        this.maxReloadIntervalMs = MAX_RELOAD_INTERVAL_MULTIPLIER * reloadIntervalMs;\n+        this.delegateRef = new AtomicReference<>(factory.createClusterResolver());\n+\n+        this.lastUpdateTime = System.currentTimeMillis();\n+        this.currentReloadIntervalMs = reloadIntervalMs;\n+\n+        List<EurekaEndpoint> clusterEndpoints = delegateRef.get().getClusterEndpoints();\n+        if (clusterEndpoints.isEmpty()) {\n+            logger.error(\"Empty Eureka server endpoint list during initialization process\");\n+            throw new ClusterResolverException(\"Resolved to an empty endpoint list\");\n+        }\n+\n+        if (logger.isInfoEnabled()) {\n+            logger.info(\"Initiated with delegate resolver of type {}; next reload in {}[sec]. Loaded endpoints={}\",\n+                    delegateRef.get().getClass(), currentReloadIntervalMs / 1000, clusterEndpoints);\n+        }\n+\n+        try {\n+            Monitors.registerObject(this);\n+        } catch (Throwable e) {\n+            logger.warn(\"Cannot register metrics\", e);\n+        }\n+    }\n+\n+    @Override\n+    public String getRegion() {\n+        ClusterResolver delegate = delegateRef.get();\n+        return delegate == null ? null : delegate.getRegion();\n+    }\n+\n+    @Override\n+    public List<EurekaEndpoint> getClusterEndpoints() {\n+        long expiryTime = lastUpdateTime + currentReloadIntervalMs;\n+        if (expiryTime <= System.currentTimeMillis()) {\n+            try {\n+                ClusterResolver newDelegate = reload();","path":"eureka-client/src/main/java/com/netflix/discovery/shared/resolver/ReloadingClusterResolver.java","position":96,"original_position":96,"commit_id":"09d3f0b24014d3a04a5de07d189f2cb5cc98f708","original_commit_id":"09d3f0b24014d3a04a5de07d189f2cb5cc98f708","user":{"login":"qiangdavidliu","id":6243760,"avatar_url":"https://avatars.githubusercontent.com/u/6243760?v=3","gravatar_id":"","url":"https://api.github.com/users/qiangdavidliu","html_url":"https://github.com/qiangdavidliu","followers_url":"https://api.github.com/users/qiangdavidliu/followers","following_url":"https://api.github.com/users/qiangdavidliu/following{/other_user}","gists_url":"https://api.github.com/users/qiangdavidliu/gists{/gist_id}","starred_url":"https://api.github.com/users/qiangdavidliu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qiangdavidliu/subscriptions","organizations_url":"https://api.github.com/users/qiangdavidliu/orgs","repos_url":"https://api.github.com/users/qiangdavidliu/repos","events_url":"https://api.github.com/users/qiangdavidliu/events{/privacy}","received_events_url":"https://api.github.com/users/qiangdavidliu/received_events","type":"User","site_admin":false},"body":"Can this block?","created_at":"2015-09-28T21:51:59Z","updated_at":"2015-09-28T21:51:59Z","html_url":"https://github.com/Netflix/eureka/pull/656#discussion_r40610353","pull_request_url":"https://api.github.com/repos/Netflix/eureka/pulls/656","_links":{"self":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/comments/40610353"},"html":{"href":"https://github.com/Netflix/eureka/pull/656#discussion_r40610353"},"pull_request":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/656"}}},"pull_request":{"url":"https://api.github.com/repos/Netflix/eureka/pulls/656","id":46195791,"html_url":"https://github.com/Netflix/eureka/pull/656","diff_url":"https://github.com/Netflix/eureka/pull/656.diff","patch_url":"https://github.com/Netflix/eureka/pull/656.patch","issue_url":"https://api.github.com/repos/Netflix/eureka/issues/656","number":656,"state":"open","locked":false,"title":"1.x merge Refactorings/transport to features/di-friendly-server","user":{"login":"tbak","id":728768,"avatar_url":"https://avatars.githubusercontent.com/u/728768?v=3","gravatar_id":"","url":"https://api.github.com/users/tbak","html_url":"https://github.com/tbak","followers_url":"https://api.github.com/users/tbak/followers","following_url":"https://api.github.com/users/tbak/following{/other_user}","gists_url":"https://api.github.com/users/tbak/gists{/gist_id}","starred_url":"https://api.github.com/users/tbak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbak/subscriptions","organizations_url":"https://api.github.com/users/tbak/orgs","repos_url":"https://api.github.com/users/tbak/repos","events_url":"https://api.github.com/users/tbak/events{/privacy}","received_events_url":"https://api.github.com/users/tbak/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-28T19:13:20Z","updated_at":"2015-09-28T21:51:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3bef7011c3814b94c7a5247ef5b21aa877840859","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Netflix/eureka/pulls/656/commits","review_comments_url":"https://api.github.com/repos/Netflix/eureka/pulls/656/comments","review_comment_url":"https://api.github.com/repos/Netflix/eureka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Netflix/eureka/issues/656/comments","statuses_url":"https://api.github.com/repos/Netflix/eureka/statuses/09d3f0b24014d3a04a5de07d189f2cb5cc98f708","head":{"label":"tbak:refactorings/transport","ref":"refactorings/transport","sha":"09d3f0b24014d3a04a5de07d189f2cb5cc98f708","user":{"login":"tbak","id":728768,"avatar_url":"https://avatars.githubusercontent.com/u/728768?v=3","gravatar_id":"","url":"https://api.github.com/users/tbak","html_url":"https://github.com/tbak","followers_url":"https://api.github.com/users/tbak/followers","following_url":"https://api.github.com/users/tbak/following{/other_user}","gists_url":"https://api.github.com/users/tbak/gists{/gist_id}","starred_url":"https://api.github.com/users/tbak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbak/subscriptions","organizations_url":"https://api.github.com/users/tbak/orgs","repos_url":"https://api.github.com/users/tbak/repos","events_url":"https://api.github.com/users/tbak/events{/privacy}","received_events_url":"https://api.github.com/users/tbak/received_events","type":"User","site_admin":false},"repo":{"id":21827017,"name":"eureka","full_name":"tbak/eureka","owner":{"login":"tbak","id":728768,"avatar_url":"https://avatars.githubusercontent.com/u/728768?v=3","gravatar_id":"","url":"https://api.github.com/users/tbak","html_url":"https://github.com/tbak","followers_url":"https://api.github.com/users/tbak/followers","following_url":"https://api.github.com/users/tbak/following{/other_user}","gists_url":"https://api.github.com/users/tbak/gists{/gist_id}","starred_url":"https://api.github.com/users/tbak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbak/subscriptions","organizations_url":"https://api.github.com/users/tbak/orgs","repos_url":"https://api.github.com/users/tbak/repos","events_url":"https://api.github.com/users/tbak/events{/privacy}","received_events_url":"https://api.github.com/users/tbak/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tbak/eureka","description":"AWS Service registry for resilient mid-tier load balancing and failover.","fork":true,"url":"https://api.github.com/repos/tbak/eureka","forks_url":"https://api.github.com/repos/tbak/eureka/forks","keys_url":"https://api.github.com/repos/tbak/eureka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tbak/eureka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tbak/eureka/teams","hooks_url":"https://api.github.com/repos/tbak/eureka/hooks","issue_events_url":"https://api.github.com/repos/tbak/eureka/issues/events{/number}","events_url":"https://api.github.com/repos/tbak/eureka/events","assignees_url":"https://api.github.com/repos/tbak/eureka/assignees{/user}","branches_url":"https://api.github.com/repos/tbak/eureka/branches{/branch}","tags_url":"https://api.github.com/repos/tbak/eureka/tags","blobs_url":"https://api.github.com/repos/tbak/eureka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tbak/eureka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tbak/eureka/git/refs{/sha}","trees_url":"https://api.github.com/repos/tbak/eureka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tbak/eureka/statuses/{sha}","languages_url":"https://api.github.com/repos/tbak/eureka/languages","stargazers_url":"https://api.github.com/repos/tbak/eureka/stargazers","contributors_url":"https://api.github.com/repos/tbak/eureka/contributors","subscribers_url":"https://api.github.com/repos/tbak/eureka/subscribers","subscription_url":"https://api.github.com/repos/tbak/eureka/subscription","commits_url":"https://api.github.com/repos/tbak/eureka/commits{/sha}","git_commits_url":"https://api.github.com/repos/tbak/eureka/git/commits{/sha}","comments_url":"https://api.github.com/repos/tbak/eureka/comments{/number}","issue_comment_url":"https://api.github.com/repos/tbak/eureka/issues/comments{/number}","contents_url":"https://api.github.com/repos/tbak/eureka/contents/{+path}","compare_url":"https://api.github.com/repos/tbak/eureka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tbak/eureka/merges","archive_url":"https://api.github.com/repos/tbak/eureka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tbak/eureka/downloads","issues_url":"https://api.github.com/repos/tbak/eureka/issues{/number}","pulls_url":"https://api.github.com/repos/tbak/eureka/pulls{/number}","milestones_url":"https://api.github.com/repos/tbak/eureka/milestones{/number}","notifications_url":"https://api.github.com/repos/tbak/eureka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tbak/eureka/labels{/name}","releases_url":"https://api.github.com/repos/tbak/eureka/releases{/id}","created_at":"2014-07-14T16:37:46Z","updated_at":"2015-03-24T21:12:35Z","pushed_at":"2015-09-25T21:38:53Z","git_url":"git://github.com/tbak/eureka.git","ssh_url":"git@github.com:tbak/eureka.git","clone_url":"https://github.com/tbak/eureka.git","svn_url":"https://github.com/tbak/eureka","homepage":"","size":11002,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Netflix:feature/di-friendly-server","ref":"feature/di-friendly-server","sha":"6a68c706d24fbf0eb2d89e1b7551907a0f2e321e","user":{"login":"Netflix","id":913567,"avatar_url":"https://avatars.githubusercontent.com/u/913567?v=3","gravatar_id":"","url":"https://api.github.com/users/Netflix","html_url":"https://github.com/Netflix","followers_url":"https://api.github.com/users/Netflix/followers","following_url":"https://api.github.com/users/Netflix/following{/other_user}","gists_url":"https://api.github.com/users/Netflix/gists{/gist_id}","starred_url":"https://api.github.com/users/Netflix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Netflix/subscriptions","organizations_url":"https://api.github.com/users/Netflix/orgs","repos_url":"https://api.github.com/users/Netflix/repos","events_url":"https://api.github.com/users/Netflix/events{/privacy}","received_events_url":"https://api.github.com/users/Netflix/received_events","type":"Organization","site_admin":false},"repo":{"id":5198041,"name":"eureka","full_name":"Netflix/eureka","owner":{"login":"Netflix","id":913567,"avatar_url":"https://avatars.githubusercontent.com/u/913567?v=3","gravatar_id":"","url":"https://api.github.com/users/Netflix","html_url":"https://github.com/Netflix","followers_url":"https://api.github.com/users/Netflix/followers","following_url":"https://api.github.com/users/Netflix/following{/other_user}","gists_url":"https://api.github.com/users/Netflix/gists{/gist_id}","starred_url":"https://api.github.com/users/Netflix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Netflix/subscriptions","organizations_url":"https://api.github.com/users/Netflix/orgs","repos_url":"https://api.github.com/users/Netflix/repos","events_url":"https://api.github.com/users/Netflix/events{/privacy}","received_events_url":"https://api.github.com/users/Netflix/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Netflix/eureka","description":"AWS Service registry for resilient mid-tier load balancing and failover.","fork":false,"url":"https://api.github.com/repos/Netflix/eureka","forks_url":"https://api.github.com/repos/Netflix/eureka/forks","keys_url":"https://api.github.com/repos/Netflix/eureka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Netflix/eureka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Netflix/eureka/teams","hooks_url":"https://api.github.com/repos/Netflix/eureka/hooks","issue_events_url":"https://api.github.com/repos/Netflix/eureka/issues/events{/number}","events_url":"https://api.github.com/repos/Netflix/eureka/events","assignees_url":"https://api.github.com/repos/Netflix/eureka/assignees{/user}","branches_url":"https://api.github.com/repos/Netflix/eureka/branches{/branch}","tags_url":"https://api.github.com/repos/Netflix/eureka/tags","blobs_url":"https://api.github.com/repos/Netflix/eureka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Netflix/eureka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Netflix/eureka/git/refs{/sha}","trees_url":"https://api.github.com/repos/Netflix/eureka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Netflix/eureka/statuses/{sha}","languages_url":"https://api.github.com/repos/Netflix/eureka/languages","stargazers_url":"https://api.github.com/repos/Netflix/eureka/stargazers","contributors_url":"https://api.github.com/repos/Netflix/eureka/contributors","subscribers_url":"https://api.github.com/repos/Netflix/eureka/subscribers","subscription_url":"https://api.github.com/repos/Netflix/eureka/subscription","commits_url":"https://api.github.com/repos/Netflix/eureka/commits{/sha}","git_commits_url":"https://api.github.com/repos/Netflix/eureka/git/commits{/sha}","comments_url":"https://api.github.com/repos/Netflix/eureka/comments{/number}","issue_comment_url":"https://api.github.com/repos/Netflix/eureka/issues/comments{/number}","contents_url":"https://api.github.com/repos/Netflix/eureka/contents/{+path}","compare_url":"https://api.github.com/repos/Netflix/eureka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Netflix/eureka/merges","archive_url":"https://api.github.com/repos/Netflix/eureka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Netflix/eureka/downloads","issues_url":"https://api.github.com/repos/Netflix/eureka/issues{/number}","pulls_url":"https://api.github.com/repos/Netflix/eureka/pulls{/number}","milestones_url":"https://api.github.com/repos/Netflix/eureka/milestones{/number}","notifications_url":"https://api.github.com/repos/Netflix/eureka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Netflix/eureka/labels{/name}","releases_url":"https://api.github.com/repos/Netflix/eureka/releases{/id}","created_at":"2012-07-26T22:23:07Z","updated_at":"2015-09-28T12:15:36Z","pushed_at":"2015-09-28T19:13:21Z","git_url":"git://github.com/Netflix/eureka.git","ssh_url":"git@github.com:Netflix/eureka.git","clone_url":"https://github.com/Netflix/eureka.git","svn_url":"https://github.com/Netflix/eureka","homepage":"","size":16572,"stargazers_count":1231,"watchers_count":1231,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":283,"mirror_url":null,"open_issues_count":32,"forks":283,"open_issues":32,"watchers":1231,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/656"},"html":{"href":"https://github.com/Netflix/eureka/pull/656"},"issue":{"href":"https://api.github.com/repos/Netflix/eureka/issues/656"},"comments":{"href":"https://api.github.com/repos/Netflix/eureka/issues/656/comments"},"review_comments":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/656/comments"},"review_comment":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Netflix/eureka/pulls/656/commits"},"statuses":{"href":"https://api.github.com/repos/Netflix/eureka/statuses/09d3f0b24014d3a04a5de07d189f2cb5cc98f708"}}}}