{"action":"created","comment":{"url":"https://api.github.com/repos/apache/reef/pulls/comments/66682899","id":66682899,"diff_hunk":"@@ -0,0 +1,113 @@\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.Text;\n+using System.Threading;\n+using Org.Apache.REEF.Common.Tasks.Events;\n+using Org.Apache.REEF.IMRU.OnREEF.Driver;\n+using Org.Apache.REEF.IMRU.OnREEF.Parameters;\n+using Org.Apache.REEF.Tang.Annotations;\n+using Org.Apache.REEF.Utilities.Logging;\n+\n+namespace Org.Apache.REEF.IMRU.OnREEF.IMRUTasks\n+{\n+    /// <summary>\n+    /// This class provides a method to handle Task close event. It is called from TaskCloseEventHandler. \n+    /// It also wraps flags to represent if the task should be closed and if the task has been stopped\n+    /// so that to provide a coordination between the task and the close handler.  \n+    /// </summary>\n+    internal sealed class TaskCloseCoordinator\n+    {\n+        private static readonly Logger Logger = Logger.GetLogger(typeof(TaskCloseCoordinator));\n+\n+        /// <summary>\n+        /// When a close event is received, this variable is set to 1. At the beginning of each task iteration,\n+        /// if this variable is set to 1, the task will break from the loop and return from the Call() method.\n+        /// </summary>\n+        private long _shouldCloseTask = 0;\n+\n+        /// <summary>\n+        /// Before the task is returned, this variable is set to 1.\n+        /// Close handler will check this variable to decide if it needs to throw an exception.\n+        /// </summary>\n+        private long _isTaskStopped = 0;\n+\n+        /// <summary>\n+        /// Waiting time for the task to close by itself\n+        /// </summary>\n+        private readonly int _enforceCloseTimeoutMilliseconds;\n+\n+        /// <summary>\n+        /// An event that will wait in close handler until it is either signaled from Call method or timeout.\n+        /// </summary>\n+        private readonly ManualResetEventSlim _waitToCloseEvent = new ManualResetEventSlim(false);\n+\n+        /// <summary>\n+        /// Handle task close event and manage the states, wait/signal when closing the task\n+        /// </summary>\n+        /// <param name=\"enforceCloseTimeoutMilliseconds\">Timeout in milliseconds to enforce the task to close if receiving task close event</param>\n+        [Inject]\n+        private TaskCloseCoordinator([Parameter(typeof(EnforceCloseTimeoutMilliseconds))] int enforceCloseTimeoutMilliseconds)\n+        {\n+            _enforceCloseTimeoutMilliseconds = enforceCloseTimeoutMilliseconds;\n+        }\n+\n+        /// <summary>\n+        /// Handle Task close event.\n+        /// Set _shouldCloseTask to 1 so that to inform the task to stop at the end of the current iteration.\n+        /// Then waiting for the signal from Call method. Either it is signaled or after _enforceCloseTimeoutMilliseconds,\n+        /// If the closed event is sent from driver, checks if the task has been stopped. If not, throw IMRUTaskSystemException \n+        /// to enforce the task to stop.\n+        /// </summary>\n+        /// <param name=\"closeEvent\"></param>\n+        internal void HandleEvent(ICloseEvent closeEvent)\n+        {\n+            Interlocked.Exchange(ref _shouldCloseTask, 1);\n+            _waitToCloseEvent.Wait(TimeSpan.FromMilliseconds(_enforceCloseTimeoutMilliseconds));\n+\n+            if (closeEvent.Value.IsPresent())\n+            {\n+                var msg = Encoding.UTF8.GetString(closeEvent.Value.Value);\n+                if (msg.Equals(TaskManager.CloseTaskByDriver))\n+                {\n+                    Logger.Log(Level.Info, \"The task received close event with message: {0}.\", msg);\n+\n+                    if (Interlocked.Read(ref _isTaskStopped) == 0)\n+                    {\n+                        throw new IMRUTaskSystemException(TaskManager.TaskKilledByDriver);\n+                    }\n+                }\n+            }\n+        }\n+\n+        internal bool ShouldCloseTask()\n+        {\n+            return Interlocked.Read(ref _shouldCloseTask) == 1;\n+        }\n+\n+        internal void SetTaskStopped()\n+        {\n+            Interlocked.Exchange(ref _isTaskStopped, 1);\n+        }\n+\n+        internal void Signal()","path":"lang/cs/Org.Apache.REEF.IMRU/OnREEF/IMRUTasks/TaskCloseCoordinator.cs","position":108,"original_position":108,"commit_id":"1594bb8b2b502403dd9b90afcb0e6c8bdfa48ad2","original_commit_id":"1594bb8b2b502403dd9b90afcb0e6c8bdfa48ad2","user":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars.githubusercontent.com/u/5666504?v=3","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":" I can combine SetTaskStopped and Signal as they are always called together. _isTaskStopped  is still needed because _waitToCloseEvent.Wait can get out by timeout as well, set is not the only way for it to get out. ","created_at":"2016-06-10T21:21:49Z","updated_at":"2016-06-10T21:21:49Z","html_url":"https://github.com/apache/reef/pull/1032#discussion_r66682899","pull_request_url":"https://api.github.com/repos/apache/reef/pulls/1032","_links":{"self":{"href":"https://api.github.com/repos/apache/reef/pulls/comments/66682899"},"html":{"href":"https://github.com/apache/reef/pull/1032#discussion_r66682899"},"pull_request":{"href":"https://api.github.com/repos/apache/reef/pulls/1032"}}},"pull_request":{"url":"https://api.github.com/repos/apache/reef/pulls/1032","id":72807418,"html_url":"https://github.com/apache/reef/pull/1032","diff_url":"https://github.com/apache/reef/pull/1032.diff","patch_url":"https://github.com/apache/reef/pull/1032.patch","issue_url":"https://api.github.com/repos/apache/reef/issues/1032","number":1032,"state":"open","locked":false,"title":"[REEF-1423] Tasks are not disposed after they are closed","user":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars.githubusercontent.com/u/5666504?v=3","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":"Updated TaskRunTime to ensure the Dispose is called after closing the task\r\nAdded test cases.\r\n\r\nJIRA: [REEF-1423](https://issues.apache.org/jira/browse/REEF-1423)\r\nThis closes","created_at":"2016-06-07T02:33:15Z","updated_at":"2016-06-10T21:21:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"41ef25498955c4f17f8ebe74ce2de2445ab2fcb0","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/reef/pulls/1032/commits","review_comments_url":"https://api.github.com/repos/apache/reef/pulls/1032/comments","review_comment_url":"https://api.github.com/repos/apache/reef/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/reef/issues/1032/comments","statuses_url":"https://api.github.com/repos/apache/reef/statuses/1594bb8b2b502403dd9b90afcb0e6c8bdfa48ad2","head":{"label":"jwang98052:REEF-1423","ref":"REEF-1423","sha":"1594bb8b2b502403dd9b90afcb0e6c8bdfa48ad2","user":{"login":"jwang98052","id":5666504,"avatar_url":"https://avatars.githubusercontent.com/u/5666504?v=3","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://avatars.githubusercontent.com/u/5666504?v=3","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":"2016-06-01T23:24:31Z","pushed_at":"2016-06-10T18:44:33Z","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":37426,"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":"master"}},"base":{"label":"apache:master","ref":"master","sha":"798f2dbb075107330c8be575182a28d8fa19e89f","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":25623941,"name":"reef","full_name":"apache/reef","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/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":"2016-05-26T20:59:10Z","pushed_at":"2016-06-10T20:33:16Z","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":48551,"stargazers_count":51,"watchers_count":51,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":59,"mirror_url":"git://git.apache.org/reef.git","open_issues_count":7,"forks":59,"open_issues":7,"watchers":51,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/reef/pulls/1032"},"html":{"href":"https://github.com/apache/reef/pull/1032"},"issue":{"href":"https://api.github.com/repos/apache/reef/issues/1032"},"comments":{"href":"https://api.github.com/repos/apache/reef/issues/1032/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/reef/pulls/1032/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/reef/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/reef/pulls/1032/commits"},"statuses":{"href":"https://api.github.com/repos/apache/reef/statuses/1594bb8b2b502403dd9b90afcb0e6c8bdfa48ad2"}}}}
{"actor":{"display_login":"jwang98052"}}