Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B66A5200B52 for ; Mon, 25 Jul 2016 19:26:02 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B46E5160A94; Mon, 25 Jul 2016 17:26:02 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DAB53160A8F for ; Mon, 25 Jul 2016 19:26:01 +0200 (CEST) Received: (qmail 34580 invoked by uid 500); 25 Jul 2016 17:25:49 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 33572 invoked by uid 99); 25 Jul 2016 17:25:48 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2016 17:25:48 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 875C8E02E4; Mon, 25 Jul 2016 17:25:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: vvasudev@apache.org To: common-commits@hadoop.apache.org Date: Mon, 25 Jul 2016 17:26:12 -0000 Message-Id: <4de5ecaf04f44f918ca8d2f802693745@git.apache.org> In-Reply-To: <52a7d86ef7ac443fa0d5c238213d8430@git.apache.org> References: <52a7d86ef7ac443fa0d5c238213d8430@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [26/50] [abbrv] hadoop git commit: YARN-1126. Add validation of users input nodes-states options to nodes CLI. Contributed by Wei Yan. archived-at: Mon, 25 Jul 2016 17:26:02 -0000 YARN-1126. Add validation of users input nodes-states options to nodes CLI. Contributed by Wei Yan. Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/be34b2a8 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/be34b2a8 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/be34b2a8 Branch: refs/heads/YARN-3926 Commit: be34b2a8fd30a1a0e803ac8b2198c695600a9e46 Parents: 521f343 Author: Rohith Sharma K S Authored: Thu Jul 21 12:20:47 2016 +0530 Committer: Rohith Sharma K S Committed: Thu Jul 21 12:20:47 2016 +0530 ---------------------------------------------------------------------- .../apache/hadoop/yarn/client/cli/NodeCLI.java | 32 ++++++++++++++++++-- .../hadoop/yarn/client/cli/TestYarnCLI.java | 31 +++++++++++-------- 2 files changed, 48 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/be34b2a8/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java ---------------------------------------------------------------------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java index f51fee9..288a5d2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/NodeCLI.java @@ -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()) { - 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; + } } } } @@ -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) + "."; + } } http://git-wip-us.apache.org/repos/asf/hadoop/blob/be34b2a8/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java ---------------------------------------------------------------------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java index 5bef691..6d7aaa7 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java @@ -1070,7 +1070,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(); @@ -1091,7 +1091,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(); @@ -1109,13 +1109,13 @@ public class TestYarnCLI { verify(sysOut, times(2)).write(any(byte[].class), anyInt(), anyInt()); sysOutStream.reset(); - result = cli.run(new String[] { "-list" }); + result = cli.run(new String[] {"-list"}); assertEquals(0, result); Assert.assertEquals(nodesReportStr, sysOutStream.toString()); verify(sysOut, times(3)).write(any(byte[].class), anyInt(), anyInt()); sysOutStream.reset(); - result = cli.run(new String[] { "-list", "-showDetails" }); + result = cli.run(new String[] {"-list", "-showDetails"}); assertEquals(0, result); baos = new ByteArrayOutputStream(); pw = new PrintWriter(baos); @@ -1149,7 +1149,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(); @@ -1170,7 +1170,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(); @@ -1191,7 +1191,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(); @@ -1212,7 +1212,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(); @@ -1236,8 +1236,8 @@ public class TestYarnCLI { states = nodeStates.toArray(new NodeState[0]); when(client.getNodeReports(states)) .thenReturn(getNodeReports(nodeReports, nodeStates)); - result = cli.run(new String[] { "-list", "--states", - "NEW,RUNNING,LOST,REBOOTED" }); + result = cli.run(new String[] {"-list", "-states", + "NEW,RUNNING,LOST,REBOOTED"}); assertEquals(0, result); verify(client).getNodeReports(states); baos = new ByteArrayOutputStream(); @@ -1268,7 +1268,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(); @@ -1294,6 +1294,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 getNodeReports( @@ -1884,7 +1888,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 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 Prints the status report of the node."); pw.close(); String nodesHelpStr = baos.toString("UTF-8"); --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org