diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java index 4f0ddfe..9a992c8 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java @@ -74,7 +74,8 @@ public int run(String[] args) throws Exception { "Supports optional use of -states to filter nodes " + "based on node state, all -all to list all nodes."); Option nodeStateOpt = new Option(NODE_STATE_CMD, true, - "Works with -list to filter nodes based on input comma-separated list of node states."); + "Works with -list to filter nodes based on input comma-separated " + + "list of node states. " + getAllValidNodeStates()); nodeStateOpt.setValueSeparator(','); nodeStateOpt.setArgs(Option.UNLIMITED_VALUES); nodeStateOpt.setArgName("States"); @@ -84,6 +85,14 @@ public int run(String[] args) throws Exception { opts.addOption(allOpt); opts.getOption(STATUS_CMD).setArgName("NodeId"); + if (args != null && args.length > 0) { + for (int i = args.length - 1; i >= 0; i --) { + if (args[i].equalsIgnoreCase("-" + NODE_ALL)) { + args[i] = "-" + NODE_ALL; + } + } + } + int exitCode = -1; CommandLine cliParser = null; try { @@ -111,8 +120,14 @@ public int run(String[] args) throws Exception { if (types != null) { for (String type : types) { if (!type.trim().isEmpty()) { - nodeStates.add(NodeState.valueOf( - org.apache.hadoop.util.StringUtils.toUpperCase(type.trim()))); + try { + nodeStates.add(NodeState.valueOf( + org.apache.hadoop.util.StringUtils.toUpperCase(type.trim()))); + } catch (IllegalArgumentException ex) { + sysout.println("The node state " + type + " is invalid."); + sysout.println(getAllValidNodeStates()); + return exitCode; + } } } } @@ -227,4 +242,14 @@ private void printNodeStatus(String nodeIdStr) throws YarnException, nodeReportStr.close(); sysout.println(baos.toString("UTF-8")); } + + private String getAllValidNodeStates() { + StringBuilder sb = new StringBuilder(); + sb.append("The valid node state can be one of the following: "); + for (NodeState state : NodeState.values()) { + sb.append(state).append(","); + } + String output = sb.toString(); + return output.substring(0, output.length() - 1) + "."; + } } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java index 1013958..a510dd9 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java @@ -928,7 +928,7 @@ public void testListClusterNodes() throws Exception { NodeState[] states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - int result = cli.run(new String[] { "-list", "--states", "NEW" }); + int result = cli.run(new String[] { "-list", "-states", "NEW" }); assertEquals(0, result); verify(client).getNodeReports(states); ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -949,7 +949,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", "RUNNING" }); + result = cli.run(new String[] { "-list", "-states", "RUNNING" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -978,7 +978,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", "UNHEALTHY" }); + result = cli.run(new String[] { "-list", "-states", "UNHEALTHY" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -999,7 +999,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", "DECOMMISSIONED" }); + result = cli.run(new String[] { "-list", "-states", "DECOMMISSIONED" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -1020,7 +1020,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", "REBOOTED" }); + result = cli.run(new String[] { "-list", "-states", "REBOOTED" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -1041,7 +1041,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", "LOST" }); + result = cli.run(new String[] { "-list", "-states", "LOST" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -1065,7 +1065,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", + result = cli.run(new String[] { "-list", "-states", "NEW,RUNNING,LOST,REBOOTED" }); assertEquals(0, result); verify(client).getNodeReports(states); @@ -1097,7 +1097,7 @@ public void testListClusterNodes() throws Exception { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--all" }); + result = cli.run(new String[] { "-list", "-all" }); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -1123,6 +1123,10 @@ public void testListClusterNodes() throws Exception { nodesReportStr = baos.toString("UTF-8"); Assert.assertEquals(nodesReportStr, sysOutStream.toString()); verify(sysOut, times(9)).write(any(byte[].class), anyInt(), anyInt()); + + sysOutStream.reset(); + result = cli.run(new String[] { "-list", "-states", "InvalidState"}); + assertEquals(-1, result); } private List getNodeReports( @@ -1518,7 +1522,10 @@ private String createNodeCLIHelpMessage() throws IOException { pw.println(" -states to filter nodes based on node state, all -all"); pw.println(" to list all nodes."); pw.println(" -states Works with -list to filter nodes based on input"); - pw.println(" comma-separated list of node states."); + pw.println(" comma-separated list of node states. The valid node"); + pw.println(" state can be one of the following:"); + pw.println(" NEW,RUNNING,UNHEALTHY,DECOMMISSIONED,LOST,REBOOTED,DEC"); + pw.println(" OMMISSIONING."); pw.println(" -status Prints the status report of the node."); pw.close(); String nodesHelpStr = baos.toString("UTF-8");