{"action":"created","comment":{"url":"https://api.github.com/repos/apache/hadoop/pulls/comments/87886708","pull_request_review_id":8479850,"id":87886708,"diff_hunk":"@@ -0,0 +1,214 @@\n+/**\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with this\n+ * work for additional information regarding copyright ownership.  The ASF\n+ * licenses this file to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ * <p>\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ * <p>\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+ * License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+package org.apache.hadoop.ozone.container.common.statemachine;\n+\n+import com.google.common.annotations.VisibleForTesting;\n+import com.google.common.util.concurrent.ThreadFactoryBuilder;\n+import org.apache.hadoop.conf.Configuration;\n+import org.apache.hadoop.ozone.OzoneClientUtils;\n+import org.apache.hadoop.ozone.OzoneConfigKeys;\n+import org.apache.hadoop.util.Time;\n+import org.apache.hadoop.util.concurrent.HadoopExecutors;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.io.Closeable;\n+import java.io.IOException;\n+import java.util.concurrent.ExecutionException;\n+import java.util.concurrent.ExecutorService;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.TimeoutException;\n+\n+/**\n+ * State Machine Class.\n+ */\n+public class DatanodeStateMachine implements Closeable {\n+  @VisibleForTesting\n+  static final Logger LOG =\n+      LoggerFactory.getLogger(DatanodeStateMachine.class);\n+  private final ExecutorService executorService;\n+  private final Configuration conf;\n+  private final SCMConnectionManager connectionManager;\n+  private final long taskWaitTime;\n+  private final long heartbeatFrequency;\n+  private StateContext context;\n+\n+  /**\n+   * Constructs a container state machine.\n+   *\n+   * @param conf - Configration.\n+   */\n+  public DatanodeStateMachine(Configuration conf) {\n+    this.conf = conf;\n+    executorService = HadoopExecutors.newScheduledThreadPool(\n+        this.conf.getInt(OzoneConfigKeys.OZONE_SCM_CONTAINER_THREADS,\n+            OzoneConfigKeys.OZONE_SCM_CONTAINER_THREADS_DEFAULT),\n+        new ThreadFactoryBuilder().setDaemon(true)\n+            .setNameFormat(\"Container State Machine Thread - %d\").build());\n+    connectionManager = new SCMConnectionManager(\n+        OzoneClientUtils.getScmRpcTimeOutInMilliseconds(conf),\n+        conf);\n+    context = new StateContext(this.conf, DatanodeStates.getInitState(), this);\n+    taskWaitTime = this.conf.getLong(OzoneConfigKeys.OZONE_CONTAINER_TASK_WAIT,\n+        OzoneConfigKeys.OZONE_CONTAINER_TASK_WAIT_DEFAULT);\n+    heartbeatFrequency = OzoneClientUtils.getScmHeartbeatInterval(conf);\n+  }\n+\n+  /**\n+   * Returns the Connection manager for this state machine.\n+   *\n+   * @return - SCMConnectionManager.\n+   */\n+  public SCMConnectionManager getConnectionManager() {\n+    return connectionManager;\n+  }\n+\n+  /**\n+   * Runs the state machine at a fixed frequency.\n+   */\n+  public void start() throws IOException {\n+    long now = 0;\n+    long nextHB = 0;\n+    while (context.getState() != DatanodeStates.SHUTDOWN) {\n+      try {\n+        nextHB = Time.monotonicNow() + heartbeatFrequency;\n+        context.execute(executorService, taskWaitTime, TimeUnit.SECONDS);\n+        now = Time.monotonicNow();\n+        if (now < nextHB) {\n+          Thread.sleep(nextHB - now);\n+        }\n+      } catch (InterruptedException | ExecutionException | TimeoutException e) {\n+        LOG.error(\"Unable to finish the execution\", e);\n+      }\n+    }\n+  }\n+\n+  /**\n+   * Gets the current context.\n+   *\n+   * @return StateContext\n+   */\n+  public StateContext getContext() {\n+    return context;\n+  }\n+\n+  /**\n+   * Sets the current context.\n+   *\n+   * @param context - Context\n+   */\n+  public void setContext(StateContext context) {\n+    this.context = context;\n+  }\n+\n+  /**\n+   * Closes this stream and releases any system resources associated with it. If\n+   * the stream is already closed then invoking this method has no effect.\n+   * <p>\n+   * <p> As noted in {@link AutoCloseable#close()}, cases where the close may\n+   * fail require careful attention. It is strongly advised to relinquish the\n+   * underlying resources and to internally <em>mark</em> the {@code Closeable}\n+   * as closed, prior to throwing the {@code IOException}.\n+   *\n+   * @throws IOException if an I/O error occurs\n+   */\n+  @Override\n+  public void close() throws IOException {\n+    executorService.shutdown();\n+    try {\n+      if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {\n+        executorService.shutdownNow();\n+      }\n+\n+      if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {\n+        LOG.error(\"Unable to shutdown statemachine properly.\");\n+      }\n+    } catch (InterruptedException e) {\n+      executorService.shutdownNow();\n+      Thread.currentThread().interrupt();","path":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/ozone/container/common/statemachine/DatanodeStateMachine.java","position":142,"original_position":142,"commit_id":"ef7262f58b81e1055ebe6874fb521eb8c16078aa","original_commit_id":"ef7262f58b81e1055ebe6874fb521eb8c16078aa","user":{"login":"anuengineer","id":2831777,"avatar_url":"https://avatars.githubusercontent.com/u/2831777?v=3","gravatar_id":"","url":"https://api.github.com/users/anuengineer","html_url":"https://github.com/anuengineer","followers_url":"https://api.github.com/users/anuengineer/followers","following_url":"https://api.github.com/users/anuengineer/following{/other_user}","gists_url":"https://api.github.com/users/anuengineer/gists{/gist_id}","starred_url":"https://api.github.com/users/anuengineer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anuengineer/subscriptions","organizations_url":"https://api.github.com/users/anuengineer/orgs","repos_url":"https://api.github.com/users/anuengineer/repos","events_url":"https://api.github.com/users/anuengineer/events{/privacy}","received_events_url":"https://api.github.com/users/anuengineer/received_events","type":"User","site_admin":false},"body":"fixed","created_at":"2016-11-14T20:34:33Z","updated_at":"2016-11-14T20:34:33Z","html_url":"https://github.com/apache/hadoop/pull/151#discussion_r87886708","pull_request_url":"https://api.github.com/repos/apache/hadoop/pulls/151","_links":{"self":{"href":"https://api.github.com/repos/apache/hadoop/pulls/comments/87886708"},"html":{"href":"https://github.com/apache/hadoop/pull/151#discussion_r87886708"},"pull_request":{"href":"https://api.github.com/repos/apache/hadoop/pulls/151"}}},"pull_request":{"url":"https://api.github.com/repos/apache/hadoop/pulls/151","id":92256789,"html_url":"https://github.com/apache/hadoop/pull/151","diff_url":"https://github.com/apache/hadoop/pull/151.diff","patch_url":"https://github.com/apache/hadoop/pull/151.patch","issue_url":"https://api.github.com/repos/apache/hadoop/issues/151","number":151,"state":"open","locked":false,"title":"Ozone:SCM: Add support for registerNode in datanode","user":{"login":"anuengineer","id":2831777,"avatar_url":"https://avatars.githubusercontent.com/u/2831777?v=3","gravatar_id":"","url":"https://api.github.com/users/anuengineer","html_url":"https://github.com/anuengineer","followers_url":"https://api.github.com/users/anuengineer/followers","following_url":"https://api.github.com/users/anuengineer/following{/other_user}","gists_url":"https://api.github.com/users/anuengineer/gists{/gist_id}","starred_url":"https://api.github.com/users/anuengineer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anuengineer/subscriptions","organizations_url":"https://api.github.com/users/anuengineer/orgs","repos_url":"https://api.github.com/users/anuengineer/repos","events_url":"https://api.github.com/users/anuengineer/events{/privacy}","received_events_url":"https://api.github.com/users/anuengineer/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-11-03T22:59:56Z","updated_at":"2016-11-14T20:34:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d552f787ad054bf6d57cb93c64c8253e3a376306","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/hadoop/pulls/151/commits","review_comments_url":"https://api.github.com/repos/apache/hadoop/pulls/151/comments","review_comment_url":"https://api.github.com/repos/apache/hadoop/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/hadoop/issues/151/comments","statuses_url":"https://api.github.com/repos/apache/hadoop/statuses/ef7262f58b81e1055ebe6874fb521eb8c16078aa","head":{"label":"anuengineer:HDFS-7240","ref":"HDFS-7240","sha":"ef7262f58b81e1055ebe6874fb521eb8c16078aa","user":{"login":"anuengineer","id":2831777,"avatar_url":"https://avatars.githubusercontent.com/u/2831777?v=3","gravatar_id":"","url":"https://api.github.com/users/anuengineer","html_url":"https://github.com/anuengineer","followers_url":"https://api.github.com/users/anuengineer/followers","following_url":"https://api.github.com/users/anuengineer/following{/other_user}","gists_url":"https://api.github.com/users/anuengineer/gists{/gist_id}","starred_url":"https://api.github.com/users/anuengineer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anuengineer/subscriptions","organizations_url":"https://api.github.com/users/anuengineer/orgs","repos_url":"https://api.github.com/users/anuengineer/repos","events_url":"https://api.github.com/users/anuengineer/events{/privacy}","received_events_url":"https://api.github.com/users/anuengineer/received_events","type":"User","site_admin":false},"repo":{"id":72793201,"name":"hadoop-1","full_name":"anuengineer/hadoop-1","owner":{"login":"anuengineer","id":2831777,"avatar_url":"https://avatars.githubusercontent.com/u/2831777?v=3","gravatar_id":"","url":"https://api.github.com/users/anuengineer","html_url":"https://github.com/anuengineer","followers_url":"https://api.github.com/users/anuengineer/followers","following_url":"https://api.github.com/users/anuengineer/following{/other_user}","gists_url":"https://api.github.com/users/anuengineer/gists{/gist_id}","starred_url":"https://api.github.com/users/anuengineer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anuengineer/subscriptions","organizations_url":"https://api.github.com/users/anuengineer/orgs","repos_url":"https://api.github.com/users/anuengineer/repos","events_url":"https://api.github.com/users/anuengineer/events{/privacy}","received_events_url":"https://api.github.com/users/anuengineer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/anuengineer/hadoop-1","description":"Mirror of Apache Hadoop","fork":true,"url":"https://api.github.com/repos/anuengineer/hadoop-1","forks_url":"https://api.github.com/repos/anuengineer/hadoop-1/forks","keys_url":"https://api.github.com/repos/anuengineer/hadoop-1/keys{/key_id}","collaborators_url":"https://api.github.com/repos/anuengineer/hadoop-1/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/anuengineer/hadoop-1/teams","hooks_url":"https://api.github.com/repos/anuengineer/hadoop-1/hooks","issue_events_url":"https://api.github.com/repos/anuengineer/hadoop-1/issues/events{/number}","events_url":"https://api.github.com/repos/anuengineer/hadoop-1/events","assignees_url":"https://api.github.com/repos/anuengineer/hadoop-1/assignees{/user}","branches_url":"https://api.github.com/repos/anuengineer/hadoop-1/branches{/branch}","tags_url":"https://api.github.com/repos/anuengineer/hadoop-1/tags","blobs_url":"https://api.github.com/repos/anuengineer/hadoop-1/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/anuengineer/hadoop-1/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/anuengineer/hadoop-1/git/refs{/sha}","trees_url":"https://api.github.com/repos/anuengineer/hadoop-1/git/trees{/sha}","statuses_url":"https://api.github.com/repos/anuengineer/hadoop-1/statuses/{sha}","languages_url":"https://api.github.com/repos/anuengineer/hadoop-1/languages","stargazers_url":"https://api.github.com/repos/anuengineer/hadoop-1/stargazers","contributors_url":"https://api.github.com/repos/anuengineer/hadoop-1/contributors","subscribers_url":"https://api.github.com/repos/anuengineer/hadoop-1/subscribers","subscription_url":"https://api.github.com/repos/anuengineer/hadoop-1/subscription","commits_url":"https://api.github.com/repos/anuengineer/hadoop-1/commits{/sha}","git_commits_url":"https://api.github.com/repos/anuengineer/hadoop-1/git/commits{/sha}","comments_url":"https://api.github.com/repos/anuengineer/hadoop-1/comments{/number}","issue_comment_url":"https://api.github.com/repos/anuengineer/hadoop-1/issues/comments{/number}","contents_url":"https://api.github.com/repos/anuengineer/hadoop-1/contents/{+path}","compare_url":"https://api.github.com/repos/anuengineer/hadoop-1/compare/{base}...{head}","merges_url":"https://api.github.com/repos/anuengineer/hadoop-1/merges","archive_url":"https://api.github.com/repos/anuengineer/hadoop-1/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/anuengineer/hadoop-1/downloads","issues_url":"https://api.github.com/repos/anuengineer/hadoop-1/issues{/number}","pulls_url":"https://api.github.com/repos/anuengineer/hadoop-1/pulls{/number}","milestones_url":"https://api.github.com/repos/anuengineer/hadoop-1/milestones{/number}","notifications_url":"https://api.github.com/repos/anuengineer/hadoop-1/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/anuengineer/hadoop-1/labels{/name}","releases_url":"https://api.github.com/repos/anuengineer/hadoop-1/releases{/id}","deployments_url":"https://api.github.com/repos/anuengineer/hadoop-1/deployments","created_at":"2016-11-03T22:39:54Z","updated_at":"2016-11-03T22:40:32Z","pushed_at":"2016-11-03T22:59:09Z","git_url":"git://github.com/anuengineer/hadoop-1.git","ssh_url":"git@github.com:anuengineer/hadoop-1.git","clone_url":"https://github.com/anuengineer/hadoop-1.git","svn_url":"https://github.com/anuengineer/hadoop-1","homepage":null,"size":363695,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"trunk"}},"base":{"label":"apache:HDFS-7240","ref":"HDFS-7240","sha":"eb8f2b2ca0835d756132e56c854685c9c4fa4eab","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":23418517,"name":"hadoop","full_name":"apache/hadoop","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/hadoop","description":"Mirror of Apache Hadoop","fork":false,"url":"https://api.github.com/repos/apache/hadoop","forks_url":"https://api.github.com/repos/apache/hadoop/forks","keys_url":"https://api.github.com/repos/apache/hadoop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/hadoop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/hadoop/teams","hooks_url":"https://api.github.com/repos/apache/hadoop/hooks","issue_events_url":"https://api.github.com/repos/apache/hadoop/issues/events{/number}","events_url":"https://api.github.com/repos/apache/hadoop/events","assignees_url":"https://api.github.com/repos/apache/hadoop/assignees{/user}","branches_url":"https://api.github.com/repos/apache/hadoop/branches{/branch}","tags_url":"https://api.github.com/repos/apache/hadoop/tags","blobs_url":"https://api.github.com/repos/apache/hadoop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/hadoop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/hadoop/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/hadoop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/hadoop/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/hadoop/languages","stargazers_url":"https://api.github.com/repos/apache/hadoop/stargazers","contributors_url":"https://api.github.com/repos/apache/hadoop/contributors","subscribers_url":"https://api.github.com/repos/apache/hadoop/subscribers","subscription_url":"https://api.github.com/repos/apache/hadoop/subscription","commits_url":"https://api.github.com/repos/apache/hadoop/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/hadoop/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/hadoop/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/hadoop/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/hadoop/contents/{+path}","compare_url":"https://api.github.com/repos/apache/hadoop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/hadoop/merges","archive_url":"https://api.github.com/repos/apache/hadoop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/hadoop/downloads","issues_url":"https://api.github.com/repos/apache/hadoop/issues{/number}","pulls_url":"https://api.github.com/repos/apache/hadoop/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/hadoop/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/hadoop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/hadoop/labels{/name}","releases_url":"https://api.github.com/repos/apache/hadoop/releases{/id}","deployments_url":"https://api.github.com/repos/apache/hadoop/deployments","created_at":"2014-08-28T07:00:08Z","updated_at":"2016-11-14T18:56:57Z","pushed_at":"2016-11-14T20:32:16Z","git_url":"git://github.com/apache/hadoop.git","ssh_url":"git@github.com:apache/hadoop.git","clone_url":"https://github.com/apache/hadoop.git","svn_url":"https://github.com/apache/hadoop","homepage":null,"size":428856,"stargazers_count":2540,"watchers_count":2540,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2504,"mirror_url":"git://git.apache.org/hadoop.git","open_issues_count":74,"forks":2504,"open_issues":74,"watchers":2540,"default_branch":"trunk"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/hadoop/pulls/151"},"html":{"href":"https://github.com/apache/hadoop/pull/151"},"issue":{"href":"https://api.github.com/repos/apache/hadoop/issues/151"},"comments":{"href":"https://api.github.com/repos/apache/hadoop/issues/151/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/hadoop/pulls/151/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/hadoop/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/hadoop/pulls/151/commits"},"statuses":{"href":"https://api.github.com/repos/apache/hadoop/statuses/ef7262f58b81e1055ebe6874fb521eb8c16078aa"}}}}
{"actor":{"display_login":"anuengineer"}}