{"action":"created","comment":{"url":"https://api.github.com/repos/druid-io/druid/pulls/comments/103074930","pull_request_review_id":23854382,"id":103074930,"diff_hunk":"@@ -0,0 +1,202 @@\n+/*\n+ * Licensed to Metamarkets Group Inc. (Metamarkets) under one\n+ * or more contributor license agreements. See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership. Metamarkets licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License. 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,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied. See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package io.druid.concurrent;\n+\n+import java.util.concurrent.locks.AbstractQueuedSynchronizer;\n+\n+/**\n+ * A synchronization tool for lifecycled objects (see {@link io.druid.java.util.common.lifecycle.Lifecycle}, that need\n+ * happens-before between start() and other methods and/or to check that the object was successfully started in other\n+ * methods.\n+ *\n+ * Guarantees in terms of JMM: happens-before between {@link #exitStart()} and {@link #isStarted()},\n+ * exitStart() and {@link #canStop()}, if it returns {@code true}.\n+ *\n+ * Example:\n+ * class ExampleLifecycledClass\n+ * {\n+ *   final LifecycleLock lifecycleLock = new LifecycleLock();\n+ *\n+ *   void start()\n+ *   {\n+ *     if (!lifecycleLock.canStart()) {\n+ *       .. return or throw exception\n+ *     }\n+ *     try {\n+ *       .. do start\n+ *       lifecycleLock.started();\n+ *     }\n+ *     finally {\n+ *       lifecycleLock.release();\n+ *     }\n+ *   }\n+ *\n+ *   void otherMethod()\n+ *   {\n+ *     Preconditions.checkState(lifecycleLock.isStarted());\n+ *     // all actions done in start() are visible here\n+ *     .. do stuff\n+ *   }\n+ *\n+ *   void stop()\n+ *   {\n+ *     if (!lifecycleLock.canStop()) {\n+ *       .. return or throw exception\n+ *     }\n+ *     // all actions done in start() are visible here\n+ *     .. do stop\n+ *   }\n+ * }\n+ */\n+public final class LifecycleLock\n+{\n+  private static class Sync extends AbstractQueuedSynchronizer\n+  {\n+    private static final int NOT_STARTED = 0;\n+    private static final int STARTING = 1;\n+    private static final int STARTED = 2;\n+    private static final int START_EXITED_SUCCESSFUL = 3;\n+    private static final int START_EXITED_FAIL = 4;\n+    private static final int STOPPING = 5;\n+\n+    boolean canStart()\n+    {\n+      return compareAndSetState(NOT_STARTED, STARTING);\n+    }\n+\n+    void started()\n+    {\n+      if (!compareAndSetState(STARTING, STARTED)) {\n+        throw new IllegalMonitorStateException(\"Called started() not in the context of start()\");\n+      }\n+    }\n+\n+    void exitStart()\n+    {\n+      // see tryReleaseShared()\n+      releaseShared(1);\n+    }\n+\n+    @Override\n+    protected boolean tryReleaseShared(int ignore)\n+    {\n+      while (true) {\n+        int state = getState();\n+        if (state == STARTING) {\n+          if (compareAndSetState(STARTING, START_EXITED_FAIL)) {\n+            return true;\n+          }\n+        } else if (state == STARTED) {\n+          if (compareAndSetState(STARTED, START_EXITED_SUCCESSFUL)) {\n+            return true;\n+          }\n+        } else {\n+          throw new IllegalMonitorStateException(\"exitStart() called not in the end of the start() method\");\n+        }\n+      }\n+    }\n+\n+    boolean isStarted()\n+    {\n+      try {\n+        // see tryAcquireShared()\n+        acquireSharedInterruptibly(1);\n+      }\n+      catch (InterruptedException e) {\n+        throw new RuntimeException(e);\n+      }\n+      return getState() == START_EXITED_SUCCESSFUL;\n+    }\n+\n+    @Override\n+    protected int tryAcquireShared(int ignore)\n+    {\n+      return getState() >= STARTED ? 1 : -1;\n+    }\n+\n+    boolean canStop()\n+    {\n+      while (true) {","path":"common/src/main/java/io/druid/concurrent/LifecycleLock.java","position":137,"original_position":136,"commit_id":"08f9cf081535bd37cce292988d74c0756ff40bfd","original_commit_id":"b461c83159642e27a7956ee393f30d6c7201f44c","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":"ok. makes sense. ","created_at":"2017-02-25T08:56:33Z","updated_at":"2017-02-25T08:56:33Z","html_url":"https://github.com/druid-io/druid/pull/3964#discussion_r103074930","pull_request_url":"https://api.github.com/repos/druid-io/druid/pulls/3964","_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments/103074930"},"html":{"href":"https://github.com/druid-io/druid/pull/3964#discussion_r103074930"},"pull_request":{"href":"https://api.github.com/repos/druid-io/druid/pulls/3964"}}},"pull_request":{"url":"https://api.github.com/repos/druid-io/druid/pulls/3964","id":107527858,"html_url":"https://github.com/druid-io/druid/pull/3964","diff_url":"https://github.com/druid-io/druid/pull/3964.diff","patch_url":"https://github.com/druid-io/druid/pull/3964.patch","issue_url":"https://api.github.com/repos/druid-io/druid/issues/3964","number":3964,"state":"open","locked":false,"title":"LifecycleLock for better synchronization in lifecycled objects","user":{"login":"leventov","id":609240,"avatar_url":"https://avatars.githubusercontent.com/u/609240?v=3","gravatar_id":"","url":"https://api.github.com/users/leventov","html_url":"https://github.com/leventov","followers_url":"https://api.github.com/users/leventov/followers","following_url":"https://api.github.com/users/leventov/following{/other_user}","gists_url":"https://api.github.com/users/leventov/gists{/gist_id}","starred_url":"https://api.github.com/users/leventov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leventov/subscriptions","organizations_url":"https://api.github.com/users/leventov/orgs","repos_url":"https://api.github.com/users/leventov/repos","events_url":"https://api.github.com/users/leventov/events{/privacy}","received_events_url":"https://api.github.com/users/leventov/received_events","type":"User","site_admin":false},"body":"**Why**\r\n - Almost lock-free\r\n - Harder to make a mistake than with usual `lock` + `started` flag combination, because user needs to manipulate less entities and LifecycleLock's methods have meaningful names.\r\n - Doesn't allow starting again after stop, for any case.","created_at":"2017-02-23T00:02:53Z","updated_at":"2017-02-25T08:56:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bf6ff9e780e623d3257814374346261eb730f799","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/druid-io/druid/pulls/3964/commits","review_comments_url":"https://api.github.com/repos/druid-io/druid/pulls/3964/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/3964/comments","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/08f9cf081535bd37cce292988d74c0756ff40bfd","head":{"label":"metamx:lifecycle-lock","ref":"lifecycle-lock","sha":"08f9cf081535bd37cce292988d74c0756ff40bfd","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":"2017-02-21T05:00:31Z","pushed_at":"2017-02-24T19:31:16Z","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":120509,"stargazers_count":81,"watchers_count":81,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":25,"mirror_url":null,"open_issues_count":5,"forks":25,"open_issues":5,"watchers":81,"default_branch":"master"}},"base":{"label":"druid-io:master","ref":"master","sha":"02fc625b5f895848a918539b2b183d4e7cb034f3","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":"2017-02-24T20:42:31Z","pushed_at":"2017-02-25T07:02:30Z","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":118113,"stargazers_count":4483,"watchers_count":4483,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1058,"mirror_url":null,"open_issues_count":598,"forks":1058,"open_issues":598,"watchers":4483,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/3964"},"html":{"href":"https://github.com/druid-io/druid/pull/3964"},"issue":{"href":"https://api.github.com/repos/druid-io/druid/issues/3964"},"comments":{"href":"https://api.github.com/repos/druid-io/druid/issues/3964/comments"},"review_comments":{"href":"https://api.github.com/repos/druid-io/druid/pulls/3964/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/3964/commits"},"statuses":{"href":"https://api.github.com/repos/druid-io/druid/statuses/08f9cf081535bd37cce292988d74c0756ff40bfd"}}}}
{"actor":{"display_login":"nishantmonu51"}}