{"action":"created","comment":{"url":"https://api.github.com/repos/prestodb/presto/pulls/comments/67968539","id":67968539,"diff_hunk":"@@ -0,0 +1,261 @@\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+package com.facebook.presto.raptor.storage.organization;\n+\n+import com.facebook.presto.raptor.metadata.ForMetadata;\n+import com.facebook.presto.raptor.metadata.MetadataDao;\n+import com.facebook.presto.raptor.metadata.ShardManager;\n+import com.facebook.presto.raptor.metadata.ShardMetadata;\n+import com.facebook.presto.raptor.metadata.Table;\n+import com.facebook.presto.raptor.storage.StorageManagerConfig;\n+import com.facebook.presto.spi.NodeManager;\n+import com.google.common.annotations.VisibleForTesting;\n+import com.google.common.collect.ImmutableSet;\n+import com.google.common.collect.ListMultimap;\n+import com.google.common.collect.Multimaps;\n+import com.google.inject.Inject;\n+import io.airlift.log.Logger;\n+import io.airlift.units.Duration;\n+import org.skife.jdbi.v2.IDBI;\n+\n+import javax.annotation.PostConstruct;\n+import javax.annotation.PreDestroy;\n+\n+import java.util.Collection;\n+import java.util.HashSet;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Optional;\n+import java.util.Set;\n+import java.util.concurrent.ScheduledExecutorService;\n+import java.util.concurrent.ThreadLocalRandom;\n+import java.util.concurrent.atomic.AtomicBoolean;\n+import java.util.stream.Collectors;\n+\n+import static com.facebook.presto.raptor.storage.organization.ShardOrganizerUtil.createOrganizationSet;\n+import static com.facebook.presto.raptor.storage.organization.ShardOrganizerUtil.getShardsByDaysBuckets;\n+import static com.facebook.presto.raptor.storage.organization.ShardOrganizerUtil.toShardIndexInfo;\n+import static com.facebook.presto.raptor.util.DatabaseUtil.onDemandDao;\n+import static io.airlift.concurrent.Threads.daemonThreadsNamed;\n+import static java.util.Objects.requireNonNull;\n+import static java.util.concurrent.Executors.newScheduledThreadPool;\n+import static java.util.concurrent.TimeUnit.MILLISECONDS;\n+import static java.util.concurrent.TimeUnit.SECONDS;\n+\n+public class ShardOrganizationManager\n+{\n+    private static final Logger log = Logger.get(ShardOrganizationManager.class);\n+\n+    private final ScheduledExecutorService discoveryService = newScheduledThreadPool(1, daemonThreadsNamed(\"shard-organization-discovery\"));\n+    private final AtomicBoolean started = new AtomicBoolean();\n+    private final AtomicBoolean shutdown = new AtomicBoolean();\n+\n+    private final IDBI dbi;\n+    private final MetadataDao metadataDao;\n+    private final ShardOrganizerDao shardOrganizerDao;\n+    private final ShardManager shardManager;\n+\n+    private final boolean enabled;\n+    private final Duration discoveryInterval;\n+\n+    private final String currentNodeIdentifier;\n+    private final ShardOrganizer organizer;\n+\n+    @Inject\n+    public ShardOrganizationManager(@ForMetadata IDBI dbi, NodeManager nodeManager, ShardManager shardManager, ShardOrganizer organizer, StorageManagerConfig config)\n+    {\n+        this(dbi,\n+                nodeManager.getCurrentNode().getNodeIdentifier(),\n+                shardManager,\n+                organizer,\n+                config.isOrganizationEnabled(),\n+                config.getOrganizationInterval());\n+    }\n+\n+    public ShardOrganizationManager(\n+            IDBI dbi,\n+            String currentNodeIdentifier,\n+            ShardManager shardManager,\n+            ShardOrganizer organizer,\n+            boolean enabled,\n+            Duration discoveryInterval)\n+    {\n+        this.dbi = requireNonNull(dbi, \"dbi is null\");\n+        this.metadataDao = onDemandDao(dbi, MetadataDao.class);\n+        this.shardOrganizerDao = onDemandDao(dbi, ShardOrganizerDao.class);\n+\n+        this.organizer = requireNonNull(organizer, \"organizer is null\");\n+        this.shardManager = requireNonNull(shardManager, \"shardManager is null\");\n+        this.currentNodeIdentifier = requireNonNull(currentNodeIdentifier, \"currentNodeIdentifier is null\");\n+\n+        this.enabled = enabled;\n+        this.discoveryInterval = requireNonNull(discoveryInterval, \"discoveryInterval is null\");\n+    }\n+\n+    @PostConstruct\n+    public void start()\n+    {\n+        if (!enabled) {\n+            return;\n+        }\n+\n+        if (!started.getAndSet(true)) {\n+            startDiscovery();\n+        }\n+    }\n+\n+    @PreDestroy\n+    public void shutdown()\n+    {\n+        shutdown.set(true);\n+        discoveryService.shutdown();\n+    }\n+\n+    private void startDiscovery()\n+    {\n+        long startDelay = getStartDelay();\n+\n+        discoveryService.scheduleWithFixedDelay(() -> {\n+            if (shutdown.get()) {\n+                return;\n+            }\n+\n+            try {\n+                // jitter to avoid overloading database\n+                long interval = (long) discoveryInterval.convertTo(SECONDS).getValue();","path":"presto-raptor/src/main/java/com/facebook/presto/raptor/storage/organization/ShardOrganizationManager.java","position":136,"original_position":136,"commit_id":"56c9ce2776a39ad53e34418eb28abe2aff8fc2a3","original_commit_id":"867bad4a23fbfc4846bb2ed6d48f7cc73b9beb65","user":{"login":"electrum","id":9230,"avatar_url":"https://avatars.githubusercontent.com/u/9230?v=3","gravatar_id":"","url":"https://api.github.com/users/electrum","html_url":"https://github.com/electrum","followers_url":"https://api.github.com/users/electrum/followers","following_url":"https://api.github.com/users/electrum/following{/other_user}","gists_url":"https://api.github.com/users/electrum/gists{/gist_id}","starred_url":"https://api.github.com/users/electrum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/electrum/subscriptions","organizations_url":"https://api.github.com/users/electrum/orgs","repos_url":"https://api.github.com/users/electrum/repos","events_url":"https://api.github.com/users/electrum/events{/privacy}","received_events_url":"https://api.github.com/users/electrum/received_events","type":"User","site_admin":false},"body":"Using the full interval as the jitter doesn't make sense with the default interval of 7 days, since that would mean things actually run every 10.5 days on average. How about a fixed jitter of `min(1 hour, interval)`?","created_at":"2016-06-21T23:04:49Z","updated_at":"2016-06-21T23:04:49Z","html_url":"https://github.com/prestodb/presto/pull/5450#discussion_r67968539","pull_request_url":"https://api.github.com/repos/prestodb/presto/pulls/5450","_links":{"self":{"href":"https://api.github.com/repos/prestodb/presto/pulls/comments/67968539"},"html":{"href":"https://github.com/prestodb/presto/pull/5450#discussion_r67968539"},"pull_request":{"href":"https://api.github.com/repos/prestodb/presto/pulls/5450"}}},"pull_request":{"url":"https://api.github.com/repos/prestodb/presto/pulls/5450","id":73822378,"html_url":"https://github.com/prestodb/presto/pull/5450","diff_url":"https://github.com/prestodb/presto/pull/5450.diff","patch_url":"https://github.com/prestodb/presto/pull/5450.patch","issue_url":"https://api.github.com/repos/prestodb/presto/issues/5450","number":5450,"state":"open","locked":false,"title":"Add shard organization in Raptor","user":{"login":"nileema","id":1034143,"avatar_url":"https://avatars.githubusercontent.com/u/1034143?v=3","gravatar_id":"","url":"https://api.github.com/users/nileema","html_url":"https://github.com/nileema","followers_url":"https://api.github.com/users/nileema/followers","following_url":"https://api.github.com/users/nileema/following{/other_user}","gists_url":"https://api.github.com/users/nileema/gists{/gist_id}","starred_url":"https://api.github.com/users/nileema/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nileema/subscriptions","organizations_url":"https://api.github.com/users/nileema/orgs","repos_url":"https://api.github.com/users/nileema/repos","events_url":"https://api.github.com/users/nileema/events{/privacy}","received_events_url":"https://api.github.com/users/nileema/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-06-14T22:08:10Z","updated_at":"2016-06-21T23:04:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d70d87e63e7012fdbda41706276d7601c3f8ecb9","assignee":{"login":"electrum","id":9230,"avatar_url":"https://avatars.githubusercontent.com/u/9230?v=3","gravatar_id":"","url":"https://api.github.com/users/electrum","html_url":"https://github.com/electrum","followers_url":"https://api.github.com/users/electrum/followers","following_url":"https://api.github.com/users/electrum/following{/other_user}","gists_url":"https://api.github.com/users/electrum/gists{/gist_id}","starred_url":"https://api.github.com/users/electrum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/electrum/subscriptions","organizations_url":"https://api.github.com/users/electrum/orgs","repos_url":"https://api.github.com/users/electrum/repos","events_url":"https://api.github.com/users/electrum/events{/privacy}","received_events_url":"https://api.github.com/users/electrum/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/prestodb/presto/pulls/5450/commits","review_comments_url":"https://api.github.com/repos/prestodb/presto/pulls/5450/comments","review_comment_url":"https://api.github.com/repos/prestodb/presto/pulls/comments{/number}","comments_url":"https://api.github.com/repos/prestodb/presto/issues/5450/comments","statuses_url":"https://api.github.com/repos/prestodb/presto/statuses/56c9ce2776a39ad53e34418eb28abe2aff8fc2a3","head":{"label":"nileema:organization","ref":"organization","sha":"56c9ce2776a39ad53e34418eb28abe2aff8fc2a3","user":{"login":"nileema","id":1034143,"avatar_url":"https://avatars.githubusercontent.com/u/1034143?v=3","gravatar_id":"","url":"https://api.github.com/users/nileema","html_url":"https://github.com/nileema","followers_url":"https://api.github.com/users/nileema/followers","following_url":"https://api.github.com/users/nileema/following{/other_user}","gists_url":"https://api.github.com/users/nileema/gists{/gist_id}","starred_url":"https://api.github.com/users/nileema/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nileema/subscriptions","organizations_url":"https://api.github.com/users/nileema/orgs","repos_url":"https://api.github.com/users/nileema/repos","events_url":"https://api.github.com/users/nileema/events{/privacy}","received_events_url":"https://api.github.com/users/nileema/received_events","type":"User","site_admin":false},"repo":{"id":12574341,"name":"presto","full_name":"nileema/presto","owner":{"login":"nileema","id":1034143,"avatar_url":"https://avatars.githubusercontent.com/u/1034143?v=3","gravatar_id":"","url":"https://api.github.com/users/nileema","html_url":"https://github.com/nileema","followers_url":"https://api.github.com/users/nileema/followers","following_url":"https://api.github.com/users/nileema/following{/other_user}","gists_url":"https://api.github.com/users/nileema/gists{/gist_id}","starred_url":"https://api.github.com/users/nileema/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nileema/subscriptions","organizations_url":"https://api.github.com/users/nileema/orgs","repos_url":"https://api.github.com/users/nileema/repos","events_url":"https://api.github.com/users/nileema/events{/privacy}","received_events_url":"https://api.github.com/users/nileema/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nileema/presto","description":"","fork":true,"url":"https://api.github.com/repos/nileema/presto","forks_url":"https://api.github.com/repos/nileema/presto/forks","keys_url":"https://api.github.com/repos/nileema/presto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nileema/presto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nileema/presto/teams","hooks_url":"https://api.github.com/repos/nileema/presto/hooks","issue_events_url":"https://api.github.com/repos/nileema/presto/issues/events{/number}","events_url":"https://api.github.com/repos/nileema/presto/events","assignees_url":"https://api.github.com/repos/nileema/presto/assignees{/user}","branches_url":"https://api.github.com/repos/nileema/presto/branches{/branch}","tags_url":"https://api.github.com/repos/nileema/presto/tags","blobs_url":"https://api.github.com/repos/nileema/presto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nileema/presto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nileema/presto/git/refs{/sha}","trees_url":"https://api.github.com/repos/nileema/presto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nileema/presto/statuses/{sha}","languages_url":"https://api.github.com/repos/nileema/presto/languages","stargazers_url":"https://api.github.com/repos/nileema/presto/stargazers","contributors_url":"https://api.github.com/repos/nileema/presto/contributors","subscribers_url":"https://api.github.com/repos/nileema/presto/subscribers","subscription_url":"https://api.github.com/repos/nileema/presto/subscription","commits_url":"https://api.github.com/repos/nileema/presto/commits{/sha}","git_commits_url":"https://api.github.com/repos/nileema/presto/git/commits{/sha}","comments_url":"https://api.github.com/repos/nileema/presto/comments{/number}","issue_comment_url":"https://api.github.com/repos/nileema/presto/issues/comments{/number}","contents_url":"https://api.github.com/repos/nileema/presto/contents/{+path}","compare_url":"https://api.github.com/repos/nileema/presto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nileema/presto/merges","archive_url":"https://api.github.com/repos/nileema/presto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nileema/presto/downloads","issues_url":"https://api.github.com/repos/nileema/presto/issues{/number}","pulls_url":"https://api.github.com/repos/nileema/presto/pulls{/number}","milestones_url":"https://api.github.com/repos/nileema/presto/milestones{/number}","notifications_url":"https://api.github.com/repos/nileema/presto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nileema/presto/labels{/name}","releases_url":"https://api.github.com/repos/nileema/presto/releases{/id}","deployments_url":"https://api.github.com/repos/nileema/presto/deployments","created_at":"2013-09-03T20:40:19Z","updated_at":"2014-08-13T18:36:54Z","pushed_at":"2016-06-21T18:10:30Z","git_url":"git://github.com/nileema/presto.git","ssh_url":"git@github.com:nileema/presto.git","clone_url":"https://github.com/nileema/presto.git","svn_url":"https://github.com/nileema/presto","homepage":null,"size":54398,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"prestodb:master","ref":"master","sha":"be5008358edc7f5218fc757bcd8dee597b7f6830","user":{"login":"prestodb","id":6882181,"avatar_url":"https://avatars.githubusercontent.com/u/6882181?v=3","gravatar_id":"","url":"https://api.github.com/users/prestodb","html_url":"https://github.com/prestodb","followers_url":"https://api.github.com/users/prestodb/followers","following_url":"https://api.github.com/users/prestodb/following{/other_user}","gists_url":"https://api.github.com/users/prestodb/gists{/gist_id}","starred_url":"https://api.github.com/users/prestodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prestodb/subscriptions","organizations_url":"https://api.github.com/users/prestodb/orgs","repos_url":"https://api.github.com/users/prestodb/repos","events_url":"https://api.github.com/users/prestodb/events{/privacy}","received_events_url":"https://api.github.com/users/prestodb/received_events","type":"Organization","site_admin":false},"repo":{"id":5349565,"name":"presto","full_name":"prestodb/presto","owner":{"login":"prestodb","id":6882181,"avatar_url":"https://avatars.githubusercontent.com/u/6882181?v=3","gravatar_id":"","url":"https://api.github.com/users/prestodb","html_url":"https://github.com/prestodb","followers_url":"https://api.github.com/users/prestodb/followers","following_url":"https://api.github.com/users/prestodb/following{/other_user}","gists_url":"https://api.github.com/users/prestodb/gists{/gist_id}","starred_url":"https://api.github.com/users/prestodb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prestodb/subscriptions","organizations_url":"https://api.github.com/users/prestodb/orgs","repos_url":"https://api.github.com/users/prestodb/repos","events_url":"https://api.github.com/users/prestodb/events{/privacy}","received_events_url":"https://api.github.com/users/prestodb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/prestodb/presto","description":"Distributed SQL query engine for big data","fork":false,"url":"https://api.github.com/repos/prestodb/presto","forks_url":"https://api.github.com/repos/prestodb/presto/forks","keys_url":"https://api.github.com/repos/prestodb/presto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/prestodb/presto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/prestodb/presto/teams","hooks_url":"https://api.github.com/repos/prestodb/presto/hooks","issue_events_url":"https://api.github.com/repos/prestodb/presto/issues/events{/number}","events_url":"https://api.github.com/repos/prestodb/presto/events","assignees_url":"https://api.github.com/repos/prestodb/presto/assignees{/user}","branches_url":"https://api.github.com/repos/prestodb/presto/branches{/branch}","tags_url":"https://api.github.com/repos/prestodb/presto/tags","blobs_url":"https://api.github.com/repos/prestodb/presto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/prestodb/presto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/prestodb/presto/git/refs{/sha}","trees_url":"https://api.github.com/repos/prestodb/presto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/prestodb/presto/statuses/{sha}","languages_url":"https://api.github.com/repos/prestodb/presto/languages","stargazers_url":"https://api.github.com/repos/prestodb/presto/stargazers","contributors_url":"https://api.github.com/repos/prestodb/presto/contributors","subscribers_url":"https://api.github.com/repos/prestodb/presto/subscribers","subscription_url":"https://api.github.com/repos/prestodb/presto/subscription","commits_url":"https://api.github.com/repos/prestodb/presto/commits{/sha}","git_commits_url":"https://api.github.com/repos/prestodb/presto/git/commits{/sha}","comments_url":"https://api.github.com/repos/prestodb/presto/comments{/number}","issue_comment_url":"https://api.github.com/repos/prestodb/presto/issues/comments{/number}","contents_url":"https://api.github.com/repos/prestodb/presto/contents/{+path}","compare_url":"https://api.github.com/repos/prestodb/presto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/prestodb/presto/merges","archive_url":"https://api.github.com/repos/prestodb/presto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/prestodb/presto/downloads","issues_url":"https://api.github.com/repos/prestodb/presto/issues{/number}","pulls_url":"https://api.github.com/repos/prestodb/presto/pulls{/number}","milestones_url":"https://api.github.com/repos/prestodb/presto/milestones{/number}","notifications_url":"https://api.github.com/repos/prestodb/presto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/prestodb/presto/labels{/name}","releases_url":"https://api.github.com/repos/prestodb/presto/releases{/id}","deployments_url":"https://api.github.com/repos/prestodb/presto/deployments","created_at":"2012-08-09T01:03:37Z","updated_at":"2016-06-21T14:07:25Z","pushed_at":"2016-06-21T22:50:18Z","git_url":"git://github.com/prestodb/presto.git","ssh_url":"git@github.com:prestodb/presto.git","clone_url":"https://github.com/prestodb/presto.git","svn_url":"https://github.com/prestodb/presto","homepage":"https://prestodb.io","size":155341,"stargazers_count":4840,"watchers_count":4840,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1447,"mirror_url":null,"open_issues_count":551,"forks":1447,"open_issues":551,"watchers":4840,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/prestodb/presto/pulls/5450"},"html":{"href":"https://github.com/prestodb/presto/pull/5450"},"issue":{"href":"https://api.github.com/repos/prestodb/presto/issues/5450"},"comments":{"href":"https://api.github.com/repos/prestodb/presto/issues/5450/comments"},"review_comments":{"href":"https://api.github.com/repos/prestodb/presto/pulls/5450/comments"},"review_comment":{"href":"https://api.github.com/repos/prestodb/presto/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/prestodb/presto/pulls/5450/commits"},"statuses":{"href":"https://api.github.com/repos/prestodb/presto/statuses/56c9ce2776a39ad53e34418eb28abe2aff8fc2a3"}}}}
{"actor":{"display_login":"electrum"}}