{"action":"created","comment":{"url":"https://api.github.com/repos/druid-io/druid/pulls/comments/61877046","id":61877046,"diff_hunk":"@@ -41,12 +43,24 @@\n   private static final long SEVEN_DAYS_IN_MILLIS = 7 * DAY_IN_MILLIS;\n   private static final long THIRTY_DAYS_IN_MILLIS = 30 * DAY_IN_MILLIS;\n   private final long referenceTimestamp;\n-  private final int threadCount;\n+  private final ListeningExecutorService exec;\n+\n+  public static long gapMillis(Interval interval1, Interval interval2)\n+  {\n+    if (interval1.getStartMillis() > interval2.getEndMillis()) {\n+      return interval1.getStartMillis() - interval2.getEndMillis();\n+    } else if (interval2.getStartMillis() > interval1.getEndMillis()) {\n+      return interval2.getStartMillis() - interval1.getEndMillis();\n+    } else {\n+      return 0;\n+    }\n+  }\n \n   public CostBalancerStrategy(DateTime referenceTimestamp, int threadCount)","path":"server/src/main/java/io/druid/server/coordinator/CostBalancerStrategy.java","position":27,"original_position":27,"commit_id":"deeb8a4f742db65dfca52e5ac1f906127732e26b","original_commit_id":"deeb8a4f742db65dfca52e5ac1f906127732e26b","user":{"login":"nishantmonu51","id":5023786,"avatar_url":"https://avatars.githubusercontent.com/u/5023786?v=3","gravatar_id":"","url":"https://api.github.com/users/nishantmonu51","html_url":"https://github.com/nishantmonu51","followers_url":"https://api.github.com/users/nishantmonu51/followers","following_url":"https://api.github.com/users/nishantmonu51/following{/other_user}","gists_url":"https://api.github.com/users/nishantmonu51/gists{/gist_id}","starred_url":"https://api.github.com/users/nishantmonu51/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nishantmonu51/subscriptions","organizations_url":"https://api.github.com/users/nishantmonu51/orgs","repos_url":"https://api.github.com/users/nishantmonu51/repos","events_url":"https://api.github.com/users/nishantmonu51/events{/privacy}","received_events_url":"https://api.github.com/users/nishantmonu51/received_events","type":"User","site_admin":false},"body":"I initially started with that approach, but the number of balancer threads is tied to coordinator dynamic config, so i tied it to the coordinator run loop instead of lifecycle. ","created_at":"2016-05-03T13:06:45Z","updated_at":"2016-05-03T13:06:45Z","html_url":"https://github.com/druid-io/druid/pull/2910#discussion_r61877046","pull_request_url":"https://api.github.com/repos/druid-io/druid/pulls/2910","_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments/61877046"},"html":{"href":"https://github.com/druid-io/druid/pull/2910#discussion_r61877046"},"pull_request":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2910"}}},"pull_request":{"url":"https://api.github.com/repos/druid-io/druid/pulls/2910","id":68576757,"html_url":"https://github.com/druid-io/druid/pull/2910","diff_url":"https://github.com/druid-io/druid/pull/2910.diff","patch_url":"https://github.com/druid-io/druid/pull/2910.patch","issue_url":"https://api.github.com/repos/druid-io/druid/issues/2910","number":2910,"state":"open","locked":false,"title":"Optimize CostBalancerStrategy","user":{"login":"nishantmonu51","id":5023786,"avatar_url":"https://avatars.githubusercontent.com/u/5023786?v=3","gravatar_id":"","url":"https://api.github.com/users/nishantmonu51","html_url":"https://github.com/nishantmonu51","followers_url":"https://api.github.com/users/nishantmonu51/followers","following_url":"https://api.github.com/users/nishantmonu51/following{/other_user}","gists_url":"https://api.github.com/users/nishantmonu51/gists{/gist_id}","starred_url":"https://api.github.com/users/nishantmonu51/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nishantmonu51/subscriptions","organizations_url":"https://api.github.com/users/nishantmonu51/orgs","repos_url":"https://api.github.com/users/nishantmonu51/repos","events_url":"https://api.github.com/users/nishantmonu51/events{/privacy}","received_events_url":"https://api.github.com/users/nishantmonu51/received_events","type":"User","site_admin":false},"body":"Optimizations to Cost balancer - \r\n1) Avoid creating thread pool on each chooseBestServer call \r\n2) Use custom impl for finding gapMillis between two segments rather than using Joda impl which is slow. \r\n\r\nThese changes speeds up computing segment costs fast by 2X. \r\n\r\nBenchmark results - \r\nBefore (with shared thread pool, was getting oome in benchmarks with creating new thread pool each time)- \r\n```\r\nCostBalancerStrategyBenchmark.testBenchmark[0]: [measured 1000 out of 1010 rounds, threads: 1 (sequential)]\r\n round: 0.02 [+- 0.01], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 7, GC.time: 0.01, time.total: 21.60, time.warmup: 0.29, time.bench: 21.31\r\nCostBalancerStrategyBenchmark.testBenchmark[1]: [measured 1000 out of 1010 rounds, threads: 1 (sequential)]\r\n round: 0.01 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 9, GC.time: 0.01, time.total: 5.93, time.warmup: 0.06, time.bench: 5.87\r\n```\r\nAfter - \r\n```\r\nCostBalancerStrategyBenchmark.testBenchmark[0]: [measured 1000 out of 1010 rounds, threads: 1 (sequential)]\r\n round: 0.01 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 7, GC.time: 0.01, time.total: 8.32, time.warmup: 0.17, time.bench: 8.14\r\nCostBalancerStrategyBenchmark.testBenchmark[1]: [measured 1000 out of 1010 rounds, threads: 1 (sequential)]\r\n round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 9, GC.time: 0.01, time.total: 3.64, time.warmup: 0.04, time.bench: 3.60\r\n```\r\n\r\nJoda gap vs custom timeMillis (10X show improvement)- \r\n```\r\nCostBalancerStrategyBenchmark.testJodaGap[0]: [measured 1000000 out of 1001000 rounds, threads: 1 (sequential)]\r\n round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 118, GC.time: 0.32, time.total: 31.99, time.warmup: 0.04, time.bench: 31.95\r\nCostBalancerStrategyBenchmark.testBalancerGapMillis[0]: [measured 1000000 out of 1001000 rounds, threads: 1 (sequential)]\r\n round: 0.00 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 0, GC.time: 0.00, time.total: 3.32, time.warmup: 0.01, time.bench: 3.32\r\n```\r\n\r\n","created_at":"2016-05-02T15:37:33Z","updated_at":"2016-05-03T13:06:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4daff686c72db285148938f8cd72312c1545a21b","assignee":null,"milestone":{"url":"https://api.github.com/repos/druid-io/druid/milestones/18","html_url":"https://github.com/druid-io/druid/milestones/0.9.1","labels_url":"https://api.github.com/repos/druid-io/druid/milestones/18/labels","id":1526819,"number":18,"title":"0.9.1","description":"","creator":{"login":"fjy","id":428325,"avatar_url":"https://avatars.githubusercontent.com/u/428325?v=3","gravatar_id":"","url":"https://api.github.com/users/fjy","html_url":"https://github.com/fjy","followers_url":"https://api.github.com/users/fjy/followers","following_url":"https://api.github.com/users/fjy/following{/other_user}","gists_url":"https://api.github.com/users/fjy/gists{/gist_id}","starred_url":"https://api.github.com/users/fjy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjy/subscriptions","organizations_url":"https://api.github.com/users/fjy/orgs","repos_url":"https://api.github.com/users/fjy/repos","events_url":"https://api.github.com/users/fjy/events{/privacy}","received_events_url":"https://api.github.com/users/fjy/received_events","type":"User","site_admin":false},"open_issues":22,"closed_issues":217,"state":"open","created_at":"2016-01-20T19:04:22Z","updated_at":"2016-05-03T05:38:13Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/druid-io/druid/pulls/2910/commits","review_comments_url":"https://api.github.com/repos/druid-io/druid/pulls/2910/comments","review_comment_url":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}","comments_url":"https://api.github.com/repos/druid-io/druid/issues/2910/comments","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/deeb8a4f742db65dfca52e5ac1f906127732e26b","head":{"label":"metamx:optimize-cost-balancer","ref":"optimize-cost-balancer","sha":"deeb8a4f742db65dfca52e5ac1f906127732e26b","user":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"repo":{"id":28248872,"name":"druid","full_name":"metamx/druid","owner":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metamx/druid","description":"Real²time Exploratory Analytics on Large Datasets","fork":true,"url":"https://api.github.com/repos/metamx/druid","forks_url":"https://api.github.com/repos/metamx/druid/forks","keys_url":"https://api.github.com/repos/metamx/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metamx/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metamx/druid/teams","hooks_url":"https://api.github.com/repos/metamx/druid/hooks","issue_events_url":"https://api.github.com/repos/metamx/druid/issues/events{/number}","events_url":"https://api.github.com/repos/metamx/druid/events","assignees_url":"https://api.github.com/repos/metamx/druid/assignees{/user}","branches_url":"https://api.github.com/repos/metamx/druid/branches{/branch}","tags_url":"https://api.github.com/repos/metamx/druid/tags","blobs_url":"https://api.github.com/repos/metamx/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metamx/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metamx/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/metamx/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metamx/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/metamx/druid/languages","stargazers_url":"https://api.github.com/repos/metamx/druid/stargazers","contributors_url":"https://api.github.com/repos/metamx/druid/contributors","subscribers_url":"https://api.github.com/repos/metamx/druid/subscribers","subscription_url":"https://api.github.com/repos/metamx/druid/subscription","commits_url":"https://api.github.com/repos/metamx/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/metamx/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/metamx/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/metamx/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/metamx/druid/contents/{+path}","compare_url":"https://api.github.com/repos/metamx/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metamx/druid/merges","archive_url":"https://api.github.com/repos/metamx/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metamx/druid/downloads","issues_url":"https://api.github.com/repos/metamx/druid/issues{/number}","pulls_url":"https://api.github.com/repos/metamx/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/metamx/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/metamx/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metamx/druid/labels{/name}","releases_url":"https://api.github.com/repos/metamx/druid/releases{/id}","deployments_url":"https://api.github.com/repos/metamx/druid/deployments","created_at":"2014-12-19T22:30:25Z","updated_at":"2016-04-28T09:54:43Z","pushed_at":"2016-05-02T19:54:36Z","git_url":"git://github.com/metamx/druid.git","ssh_url":"git@github.com:metamx/druid.git","clone_url":"https://github.com/metamx/druid.git","svn_url":"https://github.com/metamx/druid","homepage":"http://druid.io","size":113287,"stargazers_count":62,"watchers_count":62,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":18,"mirror_url":null,"open_issues_count":1,"forks":18,"open_issues":1,"watchers":62,"default_branch":"master"}},"base":{"label":"druid-io:master","ref":"master","sha":"e1eb3b1d95a948941e1d516b63005263c5b9cc2b","user":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"repo":{"id":6358188,"name":"druid","full_name":"druid-io/druid","owner":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/druid-io/druid","description":"Column oriented distributed data store ideal for powering interactive applications","fork":false,"url":"https://api.github.com/repos/druid-io/druid","forks_url":"https://api.github.com/repos/druid-io/druid/forks","keys_url":"https://api.github.com/repos/druid-io/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/druid-io/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/druid-io/druid/teams","hooks_url":"https://api.github.com/repos/druid-io/druid/hooks","issue_events_url":"https://api.github.com/repos/druid-io/druid/issues/events{/number}","events_url":"https://api.github.com/repos/druid-io/druid/events","assignees_url":"https://api.github.com/repos/druid-io/druid/assignees{/user}","branches_url":"https://api.github.com/repos/druid-io/druid/branches{/branch}","tags_url":"https://api.github.com/repos/druid-io/druid/tags","blobs_url":"https://api.github.com/repos/druid-io/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/druid-io/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/druid-io/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/druid-io/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/druid-io/druid/languages","stargazers_url":"https://api.github.com/repos/druid-io/druid/stargazers","contributors_url":"https://api.github.com/repos/druid-io/druid/contributors","subscribers_url":"https://api.github.com/repos/druid-io/druid/subscribers","subscription_url":"https://api.github.com/repos/druid-io/druid/subscription","commits_url":"https://api.github.com/repos/druid-io/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/druid-io/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/druid-io/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/druid-io/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/druid-io/druid/contents/{+path}","compare_url":"https://api.github.com/repos/druid-io/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/druid-io/druid/merges","archive_url":"https://api.github.com/repos/druid-io/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/druid-io/druid/downloads","issues_url":"https://api.github.com/repos/druid-io/druid/issues{/number}","pulls_url":"https://api.github.com/repos/druid-io/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/druid-io/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/druid-io/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/druid-io/druid/labels{/name}","releases_url":"https://api.github.com/repos/druid-io/druid/releases{/id}","deployments_url":"https://api.github.com/repos/druid-io/druid/deployments","created_at":"2012-10-23T19:08:07Z","updated_at":"2016-05-02T22:49:21Z","pushed_at":"2016-05-03T06:03:58Z","git_url":"git://github.com/druid-io/druid.git","ssh_url":"git@github.com:druid-io/druid.git","clone_url":"https://github.com/druid-io/druid.git","svn_url":"https://github.com/druid-io/druid","homepage":"http://druid.io","size":129495,"stargazers_count":3162,"watchers_count":3162,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":691,"mirror_url":null,"open_issues_count":424,"forks":691,"open_issues":424,"watchers":3162,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2910"},"html":{"href":"https://github.com/druid-io/druid/pull/2910"},"issue":{"href":"https://api.github.com/repos/druid-io/druid/issues/2910"},"comments":{"href":"https://api.github.com/repos/druid-io/druid/issues/2910/comments"},"review_comments":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2910/comments"},"review_comment":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2910/commits"},"statuses":{"href":"https://api.github.com/repos/druid-io/druid/statuses/deeb8a4f742db65dfca52e5ac1f906127732e26b"}}}}