diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 155d5c96847..e0363357b21 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -1069,6 +1069,9 @@ Release 2.8.0 - UNRELEASED YARN-4387. Fix typo in FairScheduler log message. (Xin Wang via ozawa) + YARN-4384. updateNodeResource CLI should not accept negative values for resource. + (Junping Du via wangda) + Release 2.7.3 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java index 21ba7a828a1..a5e53e453ed 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/RMAdminCLI.java @@ -418,11 +418,17 @@ public class RMAdminCLI extends HAAdmin { private int updateNodeResource(String nodeIdStr, int memSize, int cores, int overCommitTimeout) throws IOException, YarnException { + // check resource value first + if (invalidResourceValue(memSize, cores)) { + throw new IllegalArgumentException("Invalid resource value: " + "(" + + memSize + "," + cores + ") for updateNodeResource."); + } // Refresh the nodes ResourceManagerAdministrationProtocol adminProtocol = createAdminProtocol(); UpdateNodeResourceRequest request = recordFactory.newRecordInstance(UpdateNodeResourceRequest.class); NodeId nodeId = ConverterUtils.toNodeId(nodeIdStr); + Resource resource = Resources.createResource(memSize, cores); Map resourceMap = new HashMap(); @@ -433,6 +439,11 @@ public class RMAdminCLI extends HAAdmin { return 0; } + // complain negative value for cpu or memory. + private boolean invalidResourceValue(int memValue, int coreValue) { + return (memValue < 0) || (coreValue < 0); + } + private int getGroups(String[] usernames) throws IOException { // Get groups users belongs to ResourceManagerAdministrationProtocol adminProtocol = createAdminProtocol(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestRMAdminCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestRMAdminCLI.java index 085cf02c8f4..f01441d5098 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestRMAdminCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestRMAdminCLI.java @@ -218,6 +218,20 @@ public class TestRMAdminCLI { resource); } + @Test(timeout=500) + public void testUpdateNodeResourceWithInvalidValue() throws Exception { + String nodeIdStr = "0.0.0.0:0"; + int memSize = -2048; + int cores = 2; + String[] args = { "-updateNodeResource", nodeIdStr, + Integer.toString(memSize), Integer.toString(cores) }; + // execution of command line is expected to be failed + assertEquals(-1, rmAdminCLI.run(args)); + // verify admin protocol never calls. + verify(admin,times(0)).updateNodeResource( + any(UpdateNodeResourceRequest.class)); + } + @Test(timeout=500) public void testRefreshNodes() throws Exception { String[] args = { "-refreshNodes" };