YARN-1126. Add validation of users input nodes-states options to nodes CLI. Contributed by Wei Yan.

(cherry picked from commit be34b2a8fd)
This commit is contained in:
Rohith Sharma K S 2016-07-21 12:20:47 +05:30
parent 66f3056090
commit cdcd131d5f
2 changed files with 48 additions and 15 deletions

View File

@ -76,7 +76,8 @@ public class NodeCLI extends YarnCLI {
"based on node state, all -all to list all nodes, " +
"-showDetails to display more details about each node.");
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");
@ -89,6 +90,14 @@ public class NodeCLI extends YarnCLI {
opts.addOption(showDetailsOpt);
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 {
@ -116,8 +125,15 @@ public class NodeCLI extends YarnCLI {
if (types != null) {
for (String type : types) {
if (!type.trim().isEmpty()) {
try {
nodeStates.add(NodeState.valueOf(
org.apache.hadoop.util.StringUtils.toUpperCase(type.trim())));
org.apache.hadoop.util.StringUtils.toUpperCase(
type.trim())));
} catch (IllegalArgumentException ex) {
sysout.println("The node state " + type + " is invalid.");
sysout.println(getAllValidNodeStates());
return exitCode;
}
}
}
}
@ -320,4 +336,14 @@ public class NodeCLI extends YarnCLI {
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) + ".";
}
}

View File

@ -1067,7 +1067,7 @@ public class TestYarnCLI {
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();
@ -1088,7 +1088,7 @@ public class TestYarnCLI {
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();
@ -1146,7 +1146,7 @@ public class TestYarnCLI {
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();
@ -1167,7 +1167,7 @@ public class TestYarnCLI {
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();
@ -1188,7 +1188,7 @@ public class TestYarnCLI {
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();
@ -1209,7 +1209,7 @@ public class TestYarnCLI {
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();
@ -1233,7 +1233,7 @@ public class TestYarnCLI {
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);
@ -1265,7 +1265,7 @@ public class TestYarnCLI {
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();
@ -1291,6 +1291,10 @@ public class TestYarnCLI {
nodesReportStr = baos.toString("UTF-8");
Assert.assertEquals(nodesReportStr, sysOutStream.toString());
verify(sysOut, times(10)).write(any(byte[].class), anyInt(), anyInt());
sysOutStream.reset();
result = cli.run(new String[] { "-list", "-states", "InvalidState"});
assertEquals(-1, result);
}
private List<NodeReport> getNodeReports(
@ -1881,7 +1885,10 @@ public class TestYarnCLI {
pw.println(" details about each node.");
pw.println(" -showDetails Works with -list to show more details about each node.");
pw.println(" -states <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,SHUTDOWN.");
pw.println(" -status <NodeId> Prints the status report of the node.");
pw.close();
String nodesHelpStr = baos.toString("UTF-8");