{"action":"created","comment":{"url":"https://api.github.com/repos/mesos/myriad/pulls/comments/40256670","id":40256670,"diff_hunk":"@@ -37,103 +37,124 @@\n public class MyriadOperations {\n     private static final Logger LOGGER = LoggerFactory.getLogger(MyriadOperations.class);\n     private final SchedulerState schedulerState;\n-    private MyriadConfiguration cfg;\n-    private NMProfileManager profileManager;\n     private NodeScaleDownPolicy nodeScaleDownPolicy;\n \n     @Inject\n-    public MyriadOperations(MyriadConfiguration cfg,\n-                            SchedulerState schedulerState,\n-                            NMProfileManager profileManager,\n+    public MyriadOperations(SchedulerState schedulerState,\n                             NodeScaleDownPolicy nodeScaleDownPolicy) {\n-        this.cfg = cfg;\n-        this.schedulerState = schedulerState;\n-        this.profileManager = profileManager;\n-        this.nodeScaleDownPolicy = nodeScaleDownPolicy;\n+      this.schedulerState = schedulerState;\n+      this.nodeScaleDownPolicy = nodeScaleDownPolicy;\n     }\n \n-    public void flexUpCluster(int instances, String profile) {\n+    public void flexUpCluster(NMProfile profile, int instances, Constraint constraint) {\n         Collection<NodeTask> nodes = new HashSet<>();\n         for (int i = 0; i < instances; i++) {\n-            nodes.add(new NodeTask(profileManager.get(profile)));\n+            nodes.add(new NodeTask(profile, constraint));\n         }\n \n-        LOGGER.info(\"Adding {} instances to cluster\", nodes.size());\n         this.schedulerState.addNodes(nodes);\n     }\n \n-    public void flexDownCluster(int numInstancesToScaleDown) {\n-        LOGGER.info(\"About to flex down {} instances\", numInstancesToScaleDown);\n+    public void flexDownCluster(NMProfile profile, Constraint constraint, int numInstancesToScaleDown) {\n+        // Flex down Pending tasks, if any\n+        int numPendingTasksScaledDown = 0;\n+          Set<Protos.TaskID> pendingTasks = Sets.newHashSet(this.schedulerState.getPendingTaskIds());\n \n-        int numScaledDown = 0;\n-        Set<NodeTask> activeTasks = Sets.newHashSet(this.schedulerState.getActiveTasks());\n-        List<String> nodesToScaleDown = nodeScaleDownPolicy.getNodesToScaleDown();\n-        if (activeTasks.size() > nodesToScaleDown.size()) {\n-            LOGGER.info(\"Will skip flexing down {} Node Manager instances that were launched but \" +\n-                    \"have not yet registered with Resource Manager.\", activeTasks.size() - nodesToScaleDown.size());\n-        }\n-        \n-        // If a NM is flexed down it takes time for the RM to realize the NM is no longer up\n-        // We need to make sure we filter out nodes that have already been flexed down\n-        // but have not disappeared from the RM's view of the cluster\n-        for (Iterator<String> iterator = nodesToScaleDown.iterator(); iterator.hasNext();) {\n-            String nodeToScaleDown = iterator.next();\n-            boolean nodePresentInMyriad = false;\n-            for (NodeTask nodeTask : activeTasks) {\n-                if (nodeTask.getHostname().equals(nodeToScaleDown)) {\n-                    nodePresentInMyriad = true;    \n-                    break;\n-                }\n-            }\n-            if (!nodePresentInMyriad) {\n-                iterator.remove();\n+          for (Protos.TaskID taskId : pendingTasks) {\n+            NodeTask nodeTask = schedulerState.getTask(taskId);\n+            if (nodeTask.getProfile().getName().equals(profile.getName()) &&\n+                meetsConstraint(nodeTask, constraint)) {\n+              this.schedulerState.makeTaskKillable(taskId);\n+              numPendingTasksScaledDown++;\n+              if (numPendingTasksScaledDown == numInstancesToScaleDown) {\n+                break;","path":"myriad-scheduler/src/main/java/com/ebay/myriad/scheduler/MyriadOperations.java","position":97,"original_position":97,"commit_id":"0a1375053deb49860640947ab9c3a05434102b72","original_commit_id":"0a1375053deb49860640947ab9c3a05434102b72","user":{"login":"smarella","id":3505177,"avatar_url":"https://avatars.githubusercontent.com/u/3505177?v=3","gravatar_id":"","url":"https://api.github.com/users/smarella","html_url":"https://github.com/smarella","followers_url":"https://api.github.com/users/smarella/followers","following_url":"https://api.github.com/users/smarella/following{/other_user}","gists_url":"https://api.github.com/users/smarella/gists{/gist_id}","starred_url":"https://api.github.com/users/smarella/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarella/subscriptions","organizations_url":"https://api.github.com/users/smarella/orgs","repos_url":"https://api.github.com/users/smarella/repos","events_url":"https://api.github.com/users/smarella/events{/privacy}","received_events_url":"https://api.github.com/users/smarella/received_events","type":"User","site_admin":false},"body":"Ideally (performance wise), it definitely helps. In this case, I just preferred to have the code more readable. Hence, all the counters are accumulated and printed out in LOG towards the end of the method.","created_at":"2015-09-23T20:56:08Z","updated_at":"2015-09-23T20:56:08Z","html_url":"https://github.com/mesos/myriad/pull/129#discussion_r40256670","pull_request_url":"https://api.github.com/repos/mesos/myriad/pulls/129","_links":{"self":{"href":"https://api.github.com/repos/mesos/myriad/pulls/comments/40256670"},"html":{"href":"https://github.com/mesos/myriad/pull/129#discussion_r40256670"},"pull_request":{"href":"https://api.github.com/repos/mesos/myriad/pulls/129"}}},"pull_request":{"url":"https://api.github.com/repos/mesos/myriad/pulls/129","id":45445215,"html_url":"https://github.com/mesos/myriad/pull/129","diff_url":"https://github.com/mesos/myriad/pull/129.diff","patch_url":"https://github.com/mesos/myriad/pull/129.patch","issue_url":"https://api.github.com/repos/mesos/myriad/issues/129","number":129,"state":"open","locked":false,"title":"Flex APIs / NM Placement","user":{"login":"smarella","id":3505177,"avatar_url":"https://avatars.githubusercontent.com/u/3505177?v=3","gravatar_id":"","url":"https://api.github.com/users/smarella","html_url":"https://github.com/smarella","followers_url":"https://api.github.com/users/smarella/followers","following_url":"https://api.github.com/users/smarella/following{/other_user}","gists_url":"https://api.github.com/users/smarella/gists{/gist_id}","starred_url":"https://api.github.com/users/smarella/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarella/subscriptions","organizations_url":"https://api.github.com/users/smarella/orgs","repos_url":"https://api.github.com/users/smarella/repos","events_url":"https://api.github.com/users/smarella/events{/privacy}","received_events_url":"https://api.github.com/users/smarella/received_events","type":"User","site_admin":false},"body":"- Added \"profile\" parameter to flexdown API\r\n- Added \"constraint\" parameter to both flexup/flexdown APIs. Currently supports:\r\n  - only 1 constraint\r\n  - only LIKE operator\r\n  - hostname field\r\n  - Mesos slave attribute field\r\n- Improved the parameter validation of both flexup/down (Continuation of work done by @klucar)\r\n- Changed the behavior of flexdown to pick NMs in \"pending\", \"staging\" states ahead of those in \"active\"\r\n- Updated the API doc.","created_at":"2015-09-18T19:47:14Z","updated_at":"2015-09-23T20:56:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f67837f49d094ad07100ad4e25381fdefd7b8ff7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mesos/myriad/pulls/129/commits","review_comments_url":"https://api.github.com/repos/mesos/myriad/pulls/129/comments","review_comment_url":"https://api.github.com/repos/mesos/myriad/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mesos/myriad/issues/129/comments","statuses_url":"https://api.github.com/repos/mesos/myriad/statuses/0a1375053deb49860640947ab9c3a05434102b72","head":{"label":"smarella:nm_placement","ref":"nm_placement","sha":"0a1375053deb49860640947ab9c3a05434102b72","user":{"login":"smarella","id":3505177,"avatar_url":"https://avatars.githubusercontent.com/u/3505177?v=3","gravatar_id":"","url":"https://api.github.com/users/smarella","html_url":"https://github.com/smarella","followers_url":"https://api.github.com/users/smarella/followers","following_url":"https://api.github.com/users/smarella/following{/other_user}","gists_url":"https://api.github.com/users/smarella/gists{/gist_id}","starred_url":"https://api.github.com/users/smarella/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarella/subscriptions","organizations_url":"https://api.github.com/users/smarella/orgs","repos_url":"https://api.github.com/users/smarella/repos","events_url":"https://api.github.com/users/smarella/events{/privacy}","received_events_url":"https://api.github.com/users/smarella/received_events","type":"User","site_admin":false},"repo":{"id":24730362,"name":"myriad","full_name":"smarella/myriad","owner":{"login":"smarella","id":3505177,"avatar_url":"https://avatars.githubusercontent.com/u/3505177?v=3","gravatar_id":"","url":"https://api.github.com/users/smarella","html_url":"https://github.com/smarella","followers_url":"https://api.github.com/users/smarella/followers","following_url":"https://api.github.com/users/smarella/following{/other_user}","gists_url":"https://api.github.com/users/smarella/gists{/gist_id}","starred_url":"https://api.github.com/users/smarella/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarella/subscriptions","organizations_url":"https://api.github.com/users/smarella/orgs","repos_url":"https://api.github.com/users/smarella/repos","events_url":"https://api.github.com/users/smarella/events{/privacy}","received_events_url":"https://api.github.com/users/smarella/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/smarella/myriad","description":"Myriad is a mesos framework for scaling YARN clusters.","fork":true,"url":"https://api.github.com/repos/smarella/myriad","forks_url":"https://api.github.com/repos/smarella/myriad/forks","keys_url":"https://api.github.com/repos/smarella/myriad/keys{/key_id}","collaborators_url":"https://api.github.com/repos/smarella/myriad/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/smarella/myriad/teams","hooks_url":"https://api.github.com/repos/smarella/myriad/hooks","issue_events_url":"https://api.github.com/repos/smarella/myriad/issues/events{/number}","events_url":"https://api.github.com/repos/smarella/myriad/events","assignees_url":"https://api.github.com/repos/smarella/myriad/assignees{/user}","branches_url":"https://api.github.com/repos/smarella/myriad/branches{/branch}","tags_url":"https://api.github.com/repos/smarella/myriad/tags","blobs_url":"https://api.github.com/repos/smarella/myriad/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/smarella/myriad/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/smarella/myriad/git/refs{/sha}","trees_url":"https://api.github.com/repos/smarella/myriad/git/trees{/sha}","statuses_url":"https://api.github.com/repos/smarella/myriad/statuses/{sha}","languages_url":"https://api.github.com/repos/smarella/myriad/languages","stargazers_url":"https://api.github.com/repos/smarella/myriad/stargazers","contributors_url":"https://api.github.com/repos/smarella/myriad/contributors","subscribers_url":"https://api.github.com/repos/smarella/myriad/subscribers","subscription_url":"https://api.github.com/repos/smarella/myriad/subscription","commits_url":"https://api.github.com/repos/smarella/myriad/commits{/sha}","git_commits_url":"https://api.github.com/repos/smarella/myriad/git/commits{/sha}","comments_url":"https://api.github.com/repos/smarella/myriad/comments{/number}","issue_comment_url":"https://api.github.com/repos/smarella/myriad/issues/comments{/number}","contents_url":"https://api.github.com/repos/smarella/myriad/contents/{+path}","compare_url":"https://api.github.com/repos/smarella/myriad/compare/{base}...{head}","merges_url":"https://api.github.com/repos/smarella/myriad/merges","archive_url":"https://api.github.com/repos/smarella/myriad/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/smarella/myriad/downloads","issues_url":"https://api.github.com/repos/smarella/myriad/issues{/number}","pulls_url":"https://api.github.com/repos/smarella/myriad/pulls{/number}","milestones_url":"https://api.github.com/repos/smarella/myriad/milestones{/number}","notifications_url":"https://api.github.com/repos/smarella/myriad/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/smarella/myriad/labels{/name}","releases_url":"https://api.github.com/repos/smarella/myriad/releases{/id}","created_at":"2014-10-02T18:07:33Z","updated_at":"2015-07-10T22:28:37Z","pushed_at":"2015-09-18T19:39:11Z","git_url":"git://github.com/smarella/myriad.git","ssh_url":"git@github.com:smarella/myriad.git","clone_url":"https://github.com/smarella/myriad.git","svn_url":"https://github.com/smarella/myriad","homepage":"","size":2576,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"phase1"}},"base":{"label":"mesos:phase1","ref":"phase1","sha":"4472fdc183671e48058a17ec36e9c74049c90006","user":{"login":"mesos","id":229272,"avatar_url":"https://avatars.githubusercontent.com/u/229272?v=3","gravatar_id":"","url":"https://api.github.com/users/mesos","html_url":"https://github.com/mesos","followers_url":"https://api.github.com/users/mesos/followers","following_url":"https://api.github.com/users/mesos/following{/other_user}","gists_url":"https://api.github.com/users/mesos/gists{/gist_id}","starred_url":"https://api.github.com/users/mesos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mesos/subscriptions","organizations_url":"https://api.github.com/users/mesos/orgs","repos_url":"https://api.github.com/users/mesos/repos","events_url":"https://api.github.com/users/mesos/events{/privacy}","received_events_url":"https://api.github.com/users/mesos/received_events","type":"Organization","site_admin":false},"repo":{"id":23895033,"name":"myriad","full_name":"mesos/myriad","owner":{"login":"mesos","id":229272,"avatar_url":"https://avatars.githubusercontent.com/u/229272?v=3","gravatar_id":"","url":"https://api.github.com/users/mesos","html_url":"https://github.com/mesos","followers_url":"https://api.github.com/users/mesos/followers","following_url":"https://api.github.com/users/mesos/following{/other_user}","gists_url":"https://api.github.com/users/mesos/gists{/gist_id}","starred_url":"https://api.github.com/users/mesos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mesos/subscriptions","organizations_url":"https://api.github.com/users/mesos/orgs","repos_url":"https://api.github.com/users/mesos/repos","events_url":"https://api.github.com/users/mesos/events{/privacy}","received_events_url":"https://api.github.com/users/mesos/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mesos/myriad","description":"A Mesos framework for scaling a YARN cluster (Incubating into Apache)","fork":true,"url":"https://api.github.com/repos/mesos/myriad","forks_url":"https://api.github.com/repos/mesos/myriad/forks","keys_url":"https://api.github.com/repos/mesos/myriad/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mesos/myriad/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mesos/myriad/teams","hooks_url":"https://api.github.com/repos/mesos/myriad/hooks","issue_events_url":"https://api.github.com/repos/mesos/myriad/issues/events{/number}","events_url":"https://api.github.com/repos/mesos/myriad/events","assignees_url":"https://api.github.com/repos/mesos/myriad/assignees{/user}","branches_url":"https://api.github.com/repos/mesos/myriad/branches{/branch}","tags_url":"https://api.github.com/repos/mesos/myriad/tags","blobs_url":"https://api.github.com/repos/mesos/myriad/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mesos/myriad/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mesos/myriad/git/refs{/sha}","trees_url":"https://api.github.com/repos/mesos/myriad/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mesos/myriad/statuses/{sha}","languages_url":"https://api.github.com/repos/mesos/myriad/languages","stargazers_url":"https://api.github.com/repos/mesos/myriad/stargazers","contributors_url":"https://api.github.com/repos/mesos/myriad/contributors","subscribers_url":"https://api.github.com/repos/mesos/myriad/subscribers","subscription_url":"https://api.github.com/repos/mesos/myriad/subscription","commits_url":"https://api.github.com/repos/mesos/myriad/commits{/sha}","git_commits_url":"https://api.github.com/repos/mesos/myriad/git/commits{/sha}","comments_url":"https://api.github.com/repos/mesos/myriad/comments{/number}","issue_comment_url":"https://api.github.com/repos/mesos/myriad/issues/comments{/number}","contents_url":"https://api.github.com/repos/mesos/myriad/contents/{+path}","compare_url":"https://api.github.com/repos/mesos/myriad/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mesos/myriad/merges","archive_url":"https://api.github.com/repos/mesos/myriad/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mesos/myriad/downloads","issues_url":"https://api.github.com/repos/mesos/myriad/issues{/number}","pulls_url":"https://api.github.com/repos/mesos/myriad/pulls{/number}","milestones_url":"https://api.github.com/repos/mesos/myriad/milestones{/number}","notifications_url":"https://api.github.com/repos/mesos/myriad/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mesos/myriad/labels{/name}","releases_url":"https://api.github.com/repos/mesos/myriad/releases{/id}","created_at":"2014-09-10T22:37:06Z","updated_at":"2015-09-21T13:58:12Z","pushed_at":"2015-09-23T20:36:33Z","git_url":"git://github.com/mesos/myriad.git","ssh_url":"git@github.com:mesos/myriad.git","clone_url":"https://github.com/mesos/myriad.git","svn_url":"https://github.com/mesos/myriad","homepage":"https://issues.apache.org/jira/browse/MYRIAD","size":4907,"stargazers_count":230,"watchers_count":230,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":53,"mirror_url":null,"open_issues_count":59,"forks":53,"open_issues":59,"watchers":230,"default_branch":"phase1"}},"_links":{"self":{"href":"https://api.github.com/repos/mesos/myriad/pulls/129"},"html":{"href":"https://github.com/mesos/myriad/pull/129"},"issue":{"href":"https://api.github.com/repos/mesos/myriad/issues/129"},"comments":{"href":"https://api.github.com/repos/mesos/myriad/issues/129/comments"},"review_comments":{"href":"https://api.github.com/repos/mesos/myriad/pulls/129/comments"},"review_comment":{"href":"https://api.github.com/repos/mesos/myriad/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mesos/myriad/pulls/129/commits"},"statuses":{"href":"https://api.github.com/repos/mesos/myriad/statuses/0a1375053deb49860640947ab9c3a05434102b72"}}}}