{"action":"created","comment":{"url":"https://api.github.com/repos/apache/reef/pulls/comments/173853729","pull_request_review_id":103102154,"id":173853729,"diff_hunk":"@@ -0,0 +1,128 @@\n+﻿// Licensed to the Apache Software Foundation (ASF) under one\n+// or more contributor license agreements.  See the NOTICE file\n+// distributed with this work for additional information\n+// regarding copyright ownership.  The ASF 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+using System;\n+using System.IO;\n+using System.Linq;\n+using Org.Apache.REEF.IMRU.API;\n+using Org.Apache.REEF.IMRU.OnREEF.IMRUTasks;\n+using Org.Apache.REEF.IO.FileSystem;\n+using Org.Apache.REEF.Tang.Annotations;\n+using Org.Apache.REEF.Utilities.Logging;\n+using Org.Apache.REEF.Wake.Remote;\n+\n+namespace Org.Apache.REEF.IMRU.OnREEF.CheckpointHandler\n+{\n+    /// <summary>\n+    /// Default implementation of IIMRUCheckpointHandler\n+    /// </summary>\n+    public class IMRUCheckpointHandler : IIMRUCheckpointHandler\n+    {\n+        private static readonly Logger Logger = Logger.GetLogger(typeof(IMRUCheckpointHandler));\n+\n+        private readonly IFileSystem _fileSystem;\n+        private readonly string _checkpointFilePath;\n+        private const string StateDir = \"StateDir\";\n+        private const string StateFile = \"StateFile\";\n+        private const string FlagFile = \"FlagFile\";\n+        public const string StateFileExt = \".bin\";\n+        public const string FlagFileExt = \".txt\";\n+\n+        /// <summary>\n+        /// It is for storing and retrieving checkpoint data.\n+        /// </summary>\n+        /// <param name=\"checkpointFilePath\">The file path where the checkpoint data will be stored.</param>\n+        /// <param name=\"fileSystem\">File system to load/upload checkpoint data</param>\n+        [Inject]\n+        private IMRUCheckpointHandler(\n+            [Parameter(typeof(CheckpointFilePath))] string checkpointFilePath,\n+            IFileSystem fileSystem)\n+        {\n+            _fileSystem = fileSystem;\n+            _checkpointFilePath = checkpointFilePath;\n+\n+            Logger.Log(Level.Info, \"State file path: {0}\", checkpointFilePath);\n+        }\n+\n+        /// <summary>\n+        /// Save serialized checkpoint data to remote checkpoint file.\n+        /// </summary>\n+        /// <param name=\"taskState\"></param>\n+        /// <param name=\"codec\"></param>\n+        public void Persistent(ITaskState taskState, ICodec<ITaskState> codec)\n+        {\n+            var localStateFile = Path.GetTempPath() + Guid.NewGuid().ToString(\"N\").Substring(0, 4);\n+            var localFlagfile = Path.GetTempPath() + Guid.NewGuid().ToString(\"N\").Substring(0, 4);\n+\n+            string tick = DateTime.Now.Ticks.ToString();\n+            string stateFileDir = Path.Combine(_checkpointFilePath, StateDir + tick);\n+            string remoteStateFileName = Path.Combine(stateFileDir, StateFile + tick + StateFileExt);\n+            string remoteFlagFileName = Path.Combine(stateFileDir, FlagFile + tick + FlagFileExt);\n+\n+            var stateFileUri = _fileSystem.CreateUriForPath(remoteStateFileName);\n+            var flagFileUri = _fileSystem.CreateUriForPath(remoteFlagFileName);\n+\n+            var data = codec.Encode(taskState);\n+            File.WriteAllBytes(localStateFile, data);\n+            File.WriteAllText(localFlagfile, remoteStateFileName);\n+\n+            _fileSystem.CopyFromLocal(localStateFile, stateFileUri);\n+            _fileSystem.CopyFromLocal(localFlagfile, flagFileUri);\n+\n+            File.Delete(localStateFile);\n+            File.Delete(localFlagfile);\n+        }\n+\n+        /// <summary>\n+        /// Read checkpoint data and deserialize it into ITaskState object.\n+        /// </summary>\n+        /// <param name=\"codec\"></param>\n+        /// <returns></returns>\n+        public ITaskState Restore(ICodec<ITaskState> codec)\n+        {\n+            if (!string.IsNullOrEmpty(_checkpointFilePath))\n+            {\n+                var files = _fileSystem.GetChildren(_fileSystem.CreateUriForPath(_checkpointFilePath));\n+                if (files != null)\n+                {\n+                    var flagFiles = files.Where(f => f.AbsolutePath.Contains(FlagFile));\n+                    var uris = flagFiles.OrderByDescending(ff => _fileSystem.GetFileStatus(ff).ModificationTime);\n+\n+                    Uri latestFlagFile = uris.FirstOrDefault();\n+                    if (latestFlagFile != null)\n+                    {\n+                        var localLatestFlagfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString(\"N\").Substring(0, 4));\n+                        var localLatestStatefile = Path.Combine(Path.GetTempPath() + Guid.NewGuid().ToString(\"N\").Substring(0, 4));\n+\n+                        _fileSystem.CopyToLocal(latestFlagFile, localLatestFlagfile);","path":"lang/cs/Org.Apache.REEF.IMRU/OnREEF/CheckpointHandler/IMRUCheckpointHandler.cs","position":111,"original_position":111,"commit_id":"91895c07656fc40a3940a5f0264cd68d19a13981","original_commit_id":"91895c07656fc40a3940a5f0264cd68d19a13981","user":{"login":"markusweimer","id":502371,"avatar_url":"https://avatars3.githubusercontent.com/u/502371?v=4","gravatar_id":"","url":"https://api.github.com/users/markusweimer","html_url":"https://github.com/markusweimer","followers_url":"https://api.github.com/users/markusweimer/followers","following_url":"https://api.github.com/users/markusweimer/following{/other_user}","gists_url":"https://api.github.com/users/markusweimer/gists{/gist_id}","starred_url":"https://api.github.com/users/markusweimer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markusweimer/subscriptions","organizations_url":"https://api.github.com/users/markusweimer/orgs","repos_url":"https://api.github.com/users/markusweimer/repos","events_url":"https://api.github.com/users/markusweimer/events{/privacy}","received_events_url":"https://api.github.com/users/markusweimer/received_events","type":"User","site_admin":false},"body":"The copy to local should not be necessary.  Can't you just `.Open()` the remote file?","created_at":"2018-03-12T16:10:38Z","updated_at":"2018-03-12T16:16:21Z","html_url":"https://github.com/apache/reef/pull/1429#discussion_r173853729","pull_request_url":"https://api.github.com/repos/apache/reef/pulls/1429","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/reef/pulls/comments/173853729"},"html":{"href":"https://github.com/apache/reef/pull/1429#discussion_r173853729"},"pull_request":{"href":"https://api.github.com/repos/apache/reef/pulls/1429"}}},"pull_request":{"url":"https://api.github.com/repos/apache/reef/pulls/1429","id":166897367,"html_url":"https://github.com/apache/reef/pull/1429","diff_url":"https://github.com/apache/reef/pull/1429.diff","patch_url":"https://github.com/apache/reef/pull/1429.patch","issue_url":"https://api.github.com/repos/apache/reef/issues/1429","number":1429,"state":"open","locked":false,"title":"[REEF-1978] Adding Checkpoint handler for IMRU master","user":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars0.githubusercontent.com/u/5666504?v=4","gravatar_id":"","url":"https://api.github.com/users/jwang98052","html_url":"https://github.com/jwang98052","followers_url":"https://api.github.com/users/jwang98052/followers","following_url":"https://api.github.com/users/jwang98052/following{/other_user}","gists_url":"https://api.github.com/users/jwang98052/gists{/gist_id}","starred_url":"https://api.github.com/users/jwang98052/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwang98052/subscriptions","organizations_url":"https://api.github.com/users/jwang98052/orgs","repos_url":"https://api.github.com/users/jwang98052/repos","events_url":"https://api.github.com/users/jwang98052/events{/privacy}","received_events_url":"https://api.github.com/users/jwang98052/received_events","type":"User","site_admin":false},"body":"* Adding IMRUCheckpointHandler to handle task state persistent\r\n* Added configuration module for IMRUCheckpointHandler\r\n* Update IMRUJobDefination to all client to set checkpoint configuration\r\n* Add UpdateTaskStateCodec implementation\r\n* Update IMRU examples to set checkpoint config and call the check point handler\r\n* Update test cases\r\n\r\nJIRA: [REEF-1978](https://issues.apache.org/jira/browse/REEF-1978)\r\nThis closes  #","created_at":"2018-02-02T20:34:43Z","updated_at":"2018-03-12T16:16:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"74316e929a892f6b484b50364875afe4b3520bf6","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/reef/pulls/1429/commits","review_comments_url":"https://api.github.com/repos/apache/reef/pulls/1429/comments","review_comment_url":"https://api.github.com/repos/apache/reef/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/reef/issues/1429/comments","statuses_url":"https://api.github.com/repos/apache/reef/statuses/91895c07656fc40a3940a5f0264cd68d19a13981","head":{"label":"jwang98052:REEF-1978","ref":"REEF-1978","sha":"91895c07656fc40a3940a5f0264cd68d19a13981","user":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars0.githubusercontent.com/u/5666504?v=4","gravatar_id":"","url":"https://api.github.com/users/jwang98052","html_url":"https://github.com/jwang98052","followers_url":"https://api.github.com/users/jwang98052/followers","following_url":"https://api.github.com/users/jwang98052/following{/other_user}","gists_url":"https://api.github.com/users/jwang98052/gists{/gist_id}","starred_url":"https://api.github.com/users/jwang98052/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwang98052/subscriptions","organizations_url":"https://api.github.com/users/jwang98052/orgs","repos_url":"https://api.github.com/users/jwang98052/repos","events_url":"https://api.github.com/users/jwang98052/events{/privacy}","received_events_url":"https://api.github.com/users/jwang98052/received_events","type":"User","site_admin":false},"repo":{"id":25940156,"name":"reef","full_name":"jwang98052/reef","owner":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars0.githubusercontent.com/u/5666504?v=4","gravatar_id":"","url":"https://api.github.com/users/jwang98052","html_url":"https://github.com/jwang98052","followers_url":"https://api.github.com/users/jwang98052/followers","following_url":"https://api.github.com/users/jwang98052/following{/other_user}","gists_url":"https://api.github.com/users/jwang98052/gists{/gist_id}","starred_url":"https://api.github.com/users/jwang98052/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwang98052/subscriptions","organizations_url":"https://api.github.com/users/jwang98052/orgs","repos_url":"https://api.github.com/users/jwang98052/repos","events_url":"https://api.github.com/users/jwang98052/events{/privacy}","received_events_url":"https://api.github.com/users/jwang98052/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jwang98052/reef","description":"Mirror of Apache REEF (Incubating)","fork":true,"url":"https://api.github.com/repos/jwang98052/reef","forks_url":"https://api.github.com/repos/jwang98052/reef/forks","keys_url":"https://api.github.com/repos/jwang98052/reef/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jwang98052/reef/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jwang98052/reef/teams","hooks_url":"https://api.github.com/repos/jwang98052/reef/hooks","issue_events_url":"https://api.github.com/repos/jwang98052/reef/issues/events{/number}","events_url":"https://api.github.com/repos/jwang98052/reef/events","assignees_url":"https://api.github.com/repos/jwang98052/reef/assignees{/user}","branches_url":"https://api.github.com/repos/jwang98052/reef/branches{/branch}","tags_url":"https://api.github.com/repos/jwang98052/reef/tags","blobs_url":"https://api.github.com/repos/jwang98052/reef/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jwang98052/reef/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jwang98052/reef/git/refs{/sha}","trees_url":"https://api.github.com/repos/jwang98052/reef/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jwang98052/reef/statuses/{sha}","languages_url":"https://api.github.com/repos/jwang98052/reef/languages","stargazers_url":"https://api.github.com/repos/jwang98052/reef/stargazers","contributors_url":"https://api.github.com/repos/jwang98052/reef/contributors","subscribers_url":"https://api.github.com/repos/jwang98052/reef/subscribers","subscription_url":"https://api.github.com/repos/jwang98052/reef/subscription","commits_url":"https://api.github.com/repos/jwang98052/reef/commits{/sha}","git_commits_url":"https://api.github.com/repos/jwang98052/reef/git/commits{/sha}","comments_url":"https://api.github.com/repos/jwang98052/reef/comments{/number}","issue_comment_url":"https://api.github.com/repos/jwang98052/reef/issues/comments{/number}","contents_url":"https://api.github.com/repos/jwang98052/reef/contents/{+path}","compare_url":"https://api.github.com/repos/jwang98052/reef/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jwang98052/reef/merges","archive_url":"https://api.github.com/repos/jwang98052/reef/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jwang98052/reef/downloads","issues_url":"https://api.github.com/repos/jwang98052/reef/issues{/number}","pulls_url":"https://api.github.com/repos/jwang98052/reef/pulls{/number}","milestones_url":"https://api.github.com/repos/jwang98052/reef/milestones{/number}","notifications_url":"https://api.github.com/repos/jwang98052/reef/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jwang98052/reef/labels{/name}","releases_url":"https://api.github.com/repos/jwang98052/reef/releases{/id}","deployments_url":"https://api.github.com/repos/jwang98052/reef/deployments","created_at":"2014-10-29T20:32:40Z","updated_at":"2017-01-27T19:16:05Z","pushed_at":"2018-02-23T03:05:46Z","git_url":"git://github.com/jwang98052/reef.git","ssh_url":"git@github.com:jwang98052/reef.git","clone_url":"https://github.com/jwang98052/reef.git","svn_url":"https://github.com/jwang98052/reef","homepage":null,"size":43906,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"e43019f40d31341818e921c6180e6fd53e5f3df1","user":{"login":"apache","id":47359,"avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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":25623941,"name":"reef","full_name":"apache/reef","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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/reef","description":"Mirror of Apache REEF","fork":false,"url":"https://api.github.com/repos/apache/reef","forks_url":"https://api.github.com/repos/apache/reef/forks","keys_url":"https://api.github.com/repos/apache/reef/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/reef/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/reef/teams","hooks_url":"https://api.github.com/repos/apache/reef/hooks","issue_events_url":"https://api.github.com/repos/apache/reef/issues/events{/number}","events_url":"https://api.github.com/repos/apache/reef/events","assignees_url":"https://api.github.com/repos/apache/reef/assignees{/user}","branches_url":"https://api.github.com/repos/apache/reef/branches{/branch}","tags_url":"https://api.github.com/repos/apache/reef/tags","blobs_url":"https://api.github.com/repos/apache/reef/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/reef/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/reef/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/reef/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/reef/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/reef/languages","stargazers_url":"https://api.github.com/repos/apache/reef/stargazers","contributors_url":"https://api.github.com/repos/apache/reef/contributors","subscribers_url":"https://api.github.com/repos/apache/reef/subscribers","subscription_url":"https://api.github.com/repos/apache/reef/subscription","commits_url":"https://api.github.com/repos/apache/reef/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/reef/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/reef/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/reef/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/reef/contents/{+path}","compare_url":"https://api.github.com/repos/apache/reef/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/reef/merges","archive_url":"https://api.github.com/repos/apache/reef/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/reef/downloads","issues_url":"https://api.github.com/repos/apache/reef/issues{/number}","pulls_url":"https://api.github.com/repos/apache/reef/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/reef/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/reef/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/reef/labels{/name}","releases_url":"https://api.github.com/repos/apache/reef/releases{/id}","deployments_url":"https://api.github.com/repos/apache/reef/deployments","created_at":"2014-10-23T07:00:10Z","updated_at":"2018-03-12T15:49:43Z","pushed_at":"2018-03-10T01:33:19Z","git_url":"git://github.com/apache/reef.git","ssh_url":"git@github.com:apache/reef.git","clone_url":"https://github.com/apache/reef.git","svn_url":"https://github.com/apache/reef","homepage":"","size":39344,"stargazers_count":74,"watchers_count":74,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":99,"mirror_url":null,"archived":false,"open_issues_count":8,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":99,"open_issues":8,"watchers":74,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/reef/pulls/1429"},"html":{"href":"https://github.com/apache/reef/pull/1429"},"issue":{"href":"https://api.github.com/repos/apache/reef/issues/1429"},"comments":{"href":"https://api.github.com/repos/apache/reef/issues/1429/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/reef/pulls/1429/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/reef/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/reef/pulls/1429/commits"},"statuses":{"href":"https://api.github.com/repos/apache/reef/statuses/91895c07656fc40a3940a5f0264cd68d19a13981"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"markusweimer"}}