{"action":"created","comment":{"url":"https://api.github.com/repos/Alluxio/alluxio/pulls/comments/86853657","pull_request_review_id":7490112,"id":86853657,"diff_hunk":"@@ -0,0 +1,145 @@\n+/*\n+ * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0\n+ * (the \"License\"). You may not use this work except in compliance with the License, which is\n+ * available at www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * This software is distributed on an \"AS IS\" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\n+ * either express or implied, as more fully set forth in the License.\n+ *\n+ * See the NOTICE file distributed with this work for information regarding copyright ownership.\n+ */\n+\n+package alluxio.master.journal;\n+\n+import alluxio.Constants;\n+import alluxio.underfs.UnderFileSystem;\n+import alluxio.util.UnderFileSystemUtils;\n+\n+import com.google.common.base.Preconditions;\n+import com.google.common.base.Throwables;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.io.IOException;\n+\n+/**\n+ * A class which manages the checkpoint for a journal. The {@link #updateCheckpoint(String)} method\n+ * will update the journal's checkpoint file to a specified checkpoint, and\n+ * {@link recoverCheckpoint()} will recover from any failures that may occur during\n+ * {@link #updateCheckpoint(String)}.\n+ *\n+ * The checkpoint updating process goes\n+ * <pre>\n+ * 1. Write a new checkpoint named checkpoint.data.tmp\n+ * 2. Rename checkpoint.data to checkpoint.data.backup.tmp\n+ * 3. Rename checkpoint.data.backup.tmp to checkpoint.data.backup\n+ * 4. Rename checkpoint.data.tmp to checkpoint.data\n+ * 5. Delete completed logs\n+ * 6. Delete checkpoint.data.backup\n+ * </pre>\n+ */\n+public final class CheckpointManager {\n+  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);\n+\n+  /** The UFS where the journal is being written to. */\n+  private final UnderFileSystem mUfs;\n+  /**\n+   * Absolute path to the checkpoint file. This is where the latest checkpoint is stored. During\n+   * normal operation (when not writing a new checkpoint file), this is the only checkpoint file\n+   * that exists. If this file exists and there is no {@link #mTempBackupCheckpointPath}, it plus\n+   * all completed logs, plus the active log, should represent the full state of the master.\n+   */\n+  private final String mCheckpointPath;\n+  /**\n+   * Absolute path to the backup checkpoint file. The latest checkpoint is saved here while renaming\n+   * the temporary checkpoint so that we can recover in case the rename fails. If this file and\n+   * {@link #mCheckpointPath} both exist, {@link #mCheckpointPath} is the most up to date checkpoint\n+   * and {@link #mBackupCheckpointPath} should be deleted.\n+   */\n+  private final String mBackupCheckpointPath;\n+  /**\n+   * Absolute path to the temporary backup checkpoint file. This path is used as an intermediate\n+   * rename step when backing up {@link #mCheckpointPath} to {@link #mBackupCheckpointPath}. As long\n+   * as this file exists, it supercedes mCheckpointPath as the most up to date checkpoint file.\n+   */\n+  private final String mTempBackupCheckpointPath;\n+  /**\n+   * A journal writer through which this checkpoint manager can delete completed logs when the\n+   * checkpoint is updated.\n+   */\n+  private final JournalWriter mWriter;\n+\n+  /**\n+   * Creates a new instance of {@link CheckpointManager}.\n+   *\n+   * @param ufs the under file system holding the journal\n+   * @param the directory for the journal\n+   */\n+  public CheckpointManager(UnderFileSystem ufs, String checkpointPath, JournalWriter writer) {\n+    mUfs = ufs;\n+    mCheckpointPath = checkpointPath;\n+    mBackupCheckpointPath = mCheckpointPath + \".backup\";\n+    mTempBackupCheckpointPath = mBackupCheckpointPath + \".tmp\";\n+    mWriter = writer;\n+  }\n+\n+  /**\n+   * Recovers the checkpoint file in case the master crashed while updating it previously.\n+   */\n+  public void recoverCheckpoint() {\n+    try {\n+      Preconditions.checkState(\n+          !(mUfs.exists(mCheckpointPath) && mUfs.exists(mTempBackupCheckpointPath)\n+              && mUfs.exists(mBackupCheckpointPath)),\n+          \"checkpoint, temp backup checkpoint, and backup checkpoint should never exist \"\n+              + \"simultaneously\");\n+      if (mUfs.exists(mTempBackupCheckpointPath)) {\n+        // If mCheckpointPath exists, step 2 must have implemented rename as copy + delete, and\n+        // failed during the delete.\n+        UnderFileSystemUtils.deleteIfExists(mUfs, mCheckpointPath);\n+        mUfs.rename(mTempBackupCheckpointPath, mCheckpointPath);\n+      }\n+      if (mUfs.exists(mBackupCheckpointPath)) {\n+        // We must have crashed after step 3\n+        if (mUfs.exists(mCheckpointPath)) {\n+          // We crashed after step 4, so we can finish steps 5 and 6.\n+          mWriter.deleteCompletedLogs();\n+          mUfs.delete(mBackupCheckpointPath, false);\n+        } else {\n+          // We crashed before step 4, so we roll back to backup checkpoint.\n+          mUfs.rename(mBackupCheckpointPath, mCheckpointPath);\n+        }\n+      }\n+    } catch (IOException e) {\n+      throw Throwables.propagate(e);\n+    }\n+  }\n+\n+  /**\n+   * Updates the checkpoint file to the checkpoint at the specified path. The update is done in such\n+   * a way that {@link #recoverCheckpoint()} will recover from any failures that occur during the\n+   * update.\n+   *\n+   * @param newCheckpointPath the path to the new checkpoint file\n+   */\n+  public void updateCheckpoint(String newCheckpointPath) {\n+    try {\n+      if (mUfs.exists(mCheckpointPath)) {\n+        UnderFileSystemUtils.deleteIfExists(mUfs, mTempBackupCheckpointPath);\n+        UnderFileSystemUtils.deleteIfExists(mUfs, mBackupCheckpointPath);\n+        // Rename in two steps so that we never have identical mCheckpointPath and\n+        // mBackupCheckpointPath. This is a concern since UFS may implement rename as copy + delete.\n+        mUfs.rename(mCheckpointPath, mTempBackupCheckpointPath);\n+        mUfs.rename(mTempBackupCheckpointPath, mBackupCheckpointPath);","path":"core/server/src/main/java/alluxio/master/journal/CheckpointManager.java","position":133,"original_position":133,"commit_id":"c0a990826400466ef957ba661dfec6429ced6f27","original_commit_id":"c0a990826400466ef957ba661dfec6429ced6f27","user":{"login":"gpang","id":429032,"avatar_url":"https://avatars.githubusercontent.com/u/429032?v=3","gravatar_id":"","url":"https://api.github.com/users/gpang","html_url":"https://github.com/gpang","followers_url":"https://api.github.com/users/gpang/followers","following_url":"https://api.github.com/users/gpang/following{/other_user}","gists_url":"https://api.github.com/users/gpang/gists{/gist_id}","starred_url":"https://api.github.com/users/gpang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gpang/subscriptions","organizations_url":"https://api.github.com/users/gpang/orgs","repos_url":"https://api.github.com/users/gpang/repos","events_url":"https://api.github.com/users/gpang/events{/privacy}","received_events_url":"https://api.github.com/users/gpang/received_events","type":"User","site_admin":false},"body":"NIT: maybe add a log message after this that the backup of the checkpoint was completed?","created_at":"2016-11-07T19:48:13Z","updated_at":"2016-11-07T19:50:55Z","html_url":"https://github.com/Alluxio/alluxio/pull/4306#discussion_r86853657","pull_request_url":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306","_links":{"self":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/comments/86853657"},"html":{"href":"https://github.com/Alluxio/alluxio/pull/4306#discussion_r86853657"},"pull_request":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306"}}},"pull_request":{"url":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306","id":92252370,"html_url":"https://github.com/Alluxio/alluxio/pull/4306","diff_url":"https://github.com/Alluxio/alluxio/pull/4306.diff","patch_url":"https://github.com/Alluxio/alluxio/pull/4306.patch","issue_url":"https://api.github.com/repos/Alluxio/alluxio/issues/4306","number":4306,"state":"open","locked":false,"title":"[ALLUXIO-2408] Fix potential journal checkpoint failure","user":{"login":"aaudiber","id":3455177,"avatar_url":"https://avatars.githubusercontent.com/u/3455177?v=3","gravatar_id":"","url":"https://api.github.com/users/aaudiber","html_url":"https://github.com/aaudiber","followers_url":"https://api.github.com/users/aaudiber/followers","following_url":"https://api.github.com/users/aaudiber/following{/other_user}","gists_url":"https://api.github.com/users/aaudiber/gists{/gist_id}","starred_url":"https://api.github.com/users/aaudiber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaudiber/subscriptions","organizations_url":"https://api.github.com/users/aaudiber/orgs","repos_url":"https://api.github.com/users/aaudiber/repos","events_url":"https://api.github.com/users/aaudiber/events{/privacy}","received_events_url":"https://api.github.com/users/aaudiber/received_events","type":"User","site_admin":false},"body":"https://alluxio.atlassian.net/browse/ALLUXIO-2408","created_at":"2016-11-03T22:23:31Z","updated_at":"2016-11-07T19:50:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8d405a24f79125c52a4e64ae27f5852cd22cfc0c","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306/commits","review_comments_url":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306/comments","review_comment_url":"https://api.github.com/repos/Alluxio/alluxio/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Alluxio/alluxio/issues/4306/comments","statuses_url":"https://api.github.com/repos/Alluxio/alluxio/statuses/c0a990826400466ef957ba661dfec6429ced6f27","head":{"label":"aaudiber:journal-checkpoint-failure","ref":"journal-checkpoint-failure","sha":"c0a990826400466ef957ba661dfec6429ced6f27","user":{"login":"aaudiber","id":3455177,"avatar_url":"https://avatars.githubusercontent.com/u/3455177?v=3","gravatar_id":"","url":"https://api.github.com/users/aaudiber","html_url":"https://github.com/aaudiber","followers_url":"https://api.github.com/users/aaudiber/followers","following_url":"https://api.github.com/users/aaudiber/following{/other_user}","gists_url":"https://api.github.com/users/aaudiber/gists{/gist_id}","starred_url":"https://api.github.com/users/aaudiber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaudiber/subscriptions","organizations_url":"https://api.github.com/users/aaudiber/orgs","repos_url":"https://api.github.com/users/aaudiber/repos","events_url":"https://api.github.com/users/aaudiber/events{/privacy}","received_events_url":"https://api.github.com/users/aaudiber/received_events","type":"User","site_admin":false},"repo":{"id":52421920,"name":"alluxio","full_name":"aaudiber/alluxio","owner":{"login":"aaudiber","id":3455177,"avatar_url":"https://avatars.githubusercontent.com/u/3455177?v=3","gravatar_id":"","url":"https://api.github.com/users/aaudiber","html_url":"https://github.com/aaudiber","followers_url":"https://api.github.com/users/aaudiber/followers","following_url":"https://api.github.com/users/aaudiber/following{/other_user}","gists_url":"https://api.github.com/users/aaudiber/gists{/gist_id}","starred_url":"https://api.github.com/users/aaudiber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaudiber/subscriptions","organizations_url":"https://api.github.com/users/aaudiber/orgs","repos_url":"https://api.github.com/users/aaudiber/repos","events_url":"https://api.github.com/users/aaudiber/events{/privacy}","received_events_url":"https://api.github.com/users/aaudiber/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aaudiber/alluxio","description":"Memory-Centric Virtual Distributed Storage System","fork":true,"url":"https://api.github.com/repos/aaudiber/alluxio","forks_url":"https://api.github.com/repos/aaudiber/alluxio/forks","keys_url":"https://api.github.com/repos/aaudiber/alluxio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aaudiber/alluxio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aaudiber/alluxio/teams","hooks_url":"https://api.github.com/repos/aaudiber/alluxio/hooks","issue_events_url":"https://api.github.com/repos/aaudiber/alluxio/issues/events{/number}","events_url":"https://api.github.com/repos/aaudiber/alluxio/events","assignees_url":"https://api.github.com/repos/aaudiber/alluxio/assignees{/user}","branches_url":"https://api.github.com/repos/aaudiber/alluxio/branches{/branch}","tags_url":"https://api.github.com/repos/aaudiber/alluxio/tags","blobs_url":"https://api.github.com/repos/aaudiber/alluxio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aaudiber/alluxio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aaudiber/alluxio/git/refs{/sha}","trees_url":"https://api.github.com/repos/aaudiber/alluxio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aaudiber/alluxio/statuses/{sha}","languages_url":"https://api.github.com/repos/aaudiber/alluxio/languages","stargazers_url":"https://api.github.com/repos/aaudiber/alluxio/stargazers","contributors_url":"https://api.github.com/repos/aaudiber/alluxio/contributors","subscribers_url":"https://api.github.com/repos/aaudiber/alluxio/subscribers","subscription_url":"https://api.github.com/repos/aaudiber/alluxio/subscription","commits_url":"https://api.github.com/repos/aaudiber/alluxio/commits{/sha}","git_commits_url":"https://api.github.com/repos/aaudiber/alluxio/git/commits{/sha}","comments_url":"https://api.github.com/repos/aaudiber/alluxio/comments{/number}","issue_comment_url":"https://api.github.com/repos/aaudiber/alluxio/issues/comments{/number}","contents_url":"https://api.github.com/repos/aaudiber/alluxio/contents/{+path}","compare_url":"https://api.github.com/repos/aaudiber/alluxio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aaudiber/alluxio/merges","archive_url":"https://api.github.com/repos/aaudiber/alluxio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aaudiber/alluxio/downloads","issues_url":"https://api.github.com/repos/aaudiber/alluxio/issues{/number}","pulls_url":"https://api.github.com/repos/aaudiber/alluxio/pulls{/number}","milestones_url":"https://api.github.com/repos/aaudiber/alluxio/milestones{/number}","notifications_url":"https://api.github.com/repos/aaudiber/alluxio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aaudiber/alluxio/labels{/name}","releases_url":"https://api.github.com/repos/aaudiber/alluxio/releases{/id}","deployments_url":"https://api.github.com/repos/aaudiber/alluxio/deployments","created_at":"2016-02-24T07:07:15Z","updated_at":"2016-09-23T16:25:24Z","pushed_at":"2016-11-07T18:41:22Z","git_url":"git://github.com/aaudiber/alluxio.git","ssh_url":"git@github.com:aaudiber/alluxio.git","clone_url":"https://github.com/aaudiber/alluxio.git","svn_url":"https://github.com/aaudiber/alluxio","homepage":"http://www.alluxio.org/","size":62248,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":1,"default_branch":"master"}},"base":{"label":"Alluxio:master","ref":"master","sha":"c9d9618b92d84e5b72429bd61188e3f7565ae958","user":{"login":"Alluxio","id":16203694,"avatar_url":"https://avatars.githubusercontent.com/u/16203694?v=3","gravatar_id":"","url":"https://api.github.com/users/Alluxio","html_url":"https://github.com/Alluxio","followers_url":"https://api.github.com/users/Alluxio/followers","following_url":"https://api.github.com/users/Alluxio/following{/other_user}","gists_url":"https://api.github.com/users/Alluxio/gists{/gist_id}","starred_url":"https://api.github.com/users/Alluxio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Alluxio/subscriptions","organizations_url":"https://api.github.com/users/Alluxio/orgs","repos_url":"https://api.github.com/users/Alluxio/repos","events_url":"https://api.github.com/users/Alluxio/events{/privacy}","received_events_url":"https://api.github.com/users/Alluxio/received_events","type":"Organization","site_admin":false},"repo":{"id":7276954,"name":"alluxio","full_name":"Alluxio/alluxio","owner":{"login":"Alluxio","id":16203694,"avatar_url":"https://avatars.githubusercontent.com/u/16203694?v=3","gravatar_id":"","url":"https://api.github.com/users/Alluxio","html_url":"https://github.com/Alluxio","followers_url":"https://api.github.com/users/Alluxio/followers","following_url":"https://api.github.com/users/Alluxio/following{/other_user}","gists_url":"https://api.github.com/users/Alluxio/gists{/gist_id}","starred_url":"https://api.github.com/users/Alluxio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Alluxio/subscriptions","organizations_url":"https://api.github.com/users/Alluxio/orgs","repos_url":"https://api.github.com/users/Alluxio/repos","events_url":"https://api.github.com/users/Alluxio/events{/privacy}","received_events_url":"https://api.github.com/users/Alluxio/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Alluxio/alluxio","description":"Alluxio, formerly Tachyon, Memory Speed Virtual Distributed Storage System","fork":false,"url":"https://api.github.com/repos/Alluxio/alluxio","forks_url":"https://api.github.com/repos/Alluxio/alluxio/forks","keys_url":"https://api.github.com/repos/Alluxio/alluxio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Alluxio/alluxio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Alluxio/alluxio/teams","hooks_url":"https://api.github.com/repos/Alluxio/alluxio/hooks","issue_events_url":"https://api.github.com/repos/Alluxio/alluxio/issues/events{/number}","events_url":"https://api.github.com/repos/Alluxio/alluxio/events","assignees_url":"https://api.github.com/repos/Alluxio/alluxio/assignees{/user}","branches_url":"https://api.github.com/repos/Alluxio/alluxio/branches{/branch}","tags_url":"https://api.github.com/repos/Alluxio/alluxio/tags","blobs_url":"https://api.github.com/repos/Alluxio/alluxio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Alluxio/alluxio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Alluxio/alluxio/git/refs{/sha}","trees_url":"https://api.github.com/repos/Alluxio/alluxio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Alluxio/alluxio/statuses/{sha}","languages_url":"https://api.github.com/repos/Alluxio/alluxio/languages","stargazers_url":"https://api.github.com/repos/Alluxio/alluxio/stargazers","contributors_url":"https://api.github.com/repos/Alluxio/alluxio/contributors","subscribers_url":"https://api.github.com/repos/Alluxio/alluxio/subscribers","subscription_url":"https://api.github.com/repos/Alluxio/alluxio/subscription","commits_url":"https://api.github.com/repos/Alluxio/alluxio/commits{/sha}","git_commits_url":"https://api.github.com/repos/Alluxio/alluxio/git/commits{/sha}","comments_url":"https://api.github.com/repos/Alluxio/alluxio/comments{/number}","issue_comment_url":"https://api.github.com/repos/Alluxio/alluxio/issues/comments{/number}","contents_url":"https://api.github.com/repos/Alluxio/alluxio/contents/{+path}","compare_url":"https://api.github.com/repos/Alluxio/alluxio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Alluxio/alluxio/merges","archive_url":"https://api.github.com/repos/Alluxio/alluxio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Alluxio/alluxio/downloads","issues_url":"https://api.github.com/repos/Alluxio/alluxio/issues{/number}","pulls_url":"https://api.github.com/repos/Alluxio/alluxio/pulls{/number}","milestones_url":"https://api.github.com/repos/Alluxio/alluxio/milestones{/number}","notifications_url":"https://api.github.com/repos/Alluxio/alluxio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Alluxio/alluxio/labels{/name}","releases_url":"https://api.github.com/repos/Alluxio/alluxio/releases{/id}","deployments_url":"https://api.github.com/repos/Alluxio/alluxio/deployments","created_at":"2012-12-21T17:43:46Z","updated_at":"2016-11-07T17:28:37Z","pushed_at":"2016-11-07T18:41:22Z","git_url":"git://github.com/Alluxio/alluxio.git","ssh_url":"git@github.com:Alluxio/alluxio.git","clone_url":"https://github.com/Alluxio/alluxio.git","svn_url":"https://github.com/Alluxio/alluxio","homepage":"http://www.alluxio.org/","size":99245,"stargazers_count":2711,"watchers_count":2711,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1225,"mirror_url":null,"open_issues_count":29,"forks":1225,"open_issues":29,"watchers":2711,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306"},"html":{"href":"https://github.com/Alluxio/alluxio/pull/4306"},"issue":{"href":"https://api.github.com/repos/Alluxio/alluxio/issues/4306"},"comments":{"href":"https://api.github.com/repos/Alluxio/alluxio/issues/4306/comments"},"review_comments":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306/comments"},"review_comment":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Alluxio/alluxio/pulls/4306/commits"},"statuses":{"href":"https://api.github.com/repos/Alluxio/alluxio/statuses/c0a990826400466ef957ba661dfec6429ced6f27"}}}}
{"actor":{"display_login":"gpang"}}