diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java index 2b3415413ce..ff8142ab79b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java @@ -52,6 +52,7 @@ import org.apache.hadoop.yarn.api.records.SignalContainerCommand; import org.apache.hadoop.yarn.api.records.YarnApplicationState; import org.apache.hadoop.yarn.client.api.AppAdminClient; +import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.exceptions.ApplicationAttemptNotFoundException; import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException; import org.apache.hadoop.yarn.exceptions.ContainerNotFoundException; @@ -113,6 +114,7 @@ public static final String VERSION = "version"; public static final String STATES = "states"; public static final String SHELL_CMD = "shell"; + public static final String CLUSTER_ID_OPTION = "clusterId"; private static String firstArg = null; @@ -278,6 +280,8 @@ public int run(String[] args) throws Exception { "the ability to finalize the upgrade automatically."); opts.addOption(UPGRADE_CANCEL, false, "Works with -upgrade option to " + "cancel current upgrade."); + opts.addOption(CLUSTER_ID_OPTION, true, "ClusterId. " + + "By default, it will take default cluster id from the RM"); opts.getOption(LAUNCH_CMD).setArgName("Application Name> nodeStates = new HashSet(); nodeStates.add(NodeState.NEW); @@ -1545,12 +1540,9 @@ public void testListClusterNodes() throws Exception { @Test public void testNodeStatus() throws Exception { NodeId nodeId = NodeId.newInstance("host0", 0); - NodeCLI cli = new NodeCLI(); when(client.getNodeReports()) .thenReturn(getNodeReports(3, NodeState.RUNNING, false, false, false)); - cli.setClient(client); - cli.setSysOutPrintStream(sysOut); - cli.setSysErrPrintStream(sysErr); + NodeCLI cli = createAndGetNodeCLI(); int result = cli.run(new String[] { "-status", nodeId.toString() }); assertEquals(0, result); verify(client).getNodeReports(); @@ -1583,12 +1575,9 @@ public void testNodeStatus() throws Exception { @Test public void testNodeStatusWithEmptyNodeLabels() throws Exception { NodeId nodeId = NodeId.newInstance("host0", 0); - NodeCLI cli = new NodeCLI(); when(client.getNodeReports()).thenReturn( getNodeReports(3, NodeState.RUNNING)); - cli.setClient(client); - cli.setSysOutPrintStream(sysOut); - cli.setSysErrPrintStream(sysErr); + NodeCLI cli = createAndGetNodeCLI(); int result = cli.run(new String[] { "-status", nodeId.toString() }); assertEquals(0, result); verify(client).getNodeReports(); @@ -1620,12 +1609,9 @@ public void testNodeStatusWithEmptyNodeLabels() throws Exception { @Test public void testNodeStatusWithEmptyResourceUtilization() throws Exception { NodeId nodeId = NodeId.newInstance("host0", 0); - NodeCLI cli = new NodeCLI(); when(client.getNodeReports()) .thenReturn(getNodeReports(3, NodeState.RUNNING, false, true, true)); - cli.setClient(client); - cli.setSysOutPrintStream(sysOut); - cli.setSysErrPrintStream(sysErr); + NodeCLI cli = createAndGetNodeCLI(); int result = cli.run(new String[] { "-status", nodeId.toString() }); assertEquals(0, result); verify(client).getNodeReports(); @@ -1657,12 +1643,10 @@ public void testNodeStatusWithEmptyResourceUtilization() throws Exception { @Test public void testAbsentNodeStatus() throws Exception { NodeId nodeId = NodeId.newInstance("Absenthost0", 0); - NodeCLI cli = new NodeCLI(); + when(client.getNodeReports()).thenReturn( getNodeReports(0, NodeState.RUNNING)); - cli.setClient(client); - cli.setSysOutPrintStream(sysOut); - cli.setSysErrPrintStream(sysErr); + NodeCLI cli = createAndGetNodeCLI(); int result = cli.run(new String[] { "-status", nodeId.toString() }); assertEquals(0, result); verify(client).getNodeReports(); @@ -1702,10 +1686,7 @@ public void testMissingArguments() throws Exception { createContainerCLIHelpMessage()), normalize(sysOutStream.toString())); sysOutStream.reset(); - NodeCLI nodeCLI = new NodeCLI(); - nodeCLI.setClient(client); - nodeCLI.setSysOutPrintStream(sysOut); - nodeCLI.setSysErrPrintStream(sysErr); + NodeCLI nodeCLI = createAndGetNodeCLI(); result = nodeCLI.run(new String[] { "-status" }); Assert.assertEquals(result, -1); Assert.assertEquals(String.format("Missing argument for options%n%1s", @@ -2101,14 +2082,31 @@ private void verifyUsageInfo(YarnCLI cli) throws Exception { } private ApplicationCLI createAndGetAppCLI() { - ApplicationCLI cli = new ApplicationCLI(); + ApplicationCLI cli = new ApplicationCLI() { + @Override protected void createAndStartYarnClient() { + } + }; cli.setClient(client); cli.setSysOutPrintStream(sysOut); return cli; } - + private QueueCLI createAndGetQueueCLI() { - QueueCLI cli = new QueueCLI(); + QueueCLI cli = new QueueCLI() { + @Override protected void createAndStartYarnClient() { + } + }; + cli.setClient(client); + cli.setSysOutPrintStream(sysOut); + cli.setSysErrPrintStream(sysErr); + return cli; + } + + private NodeCLI createAndGetNodeCLI() { + NodeCLI cli = new NodeCLI() { + @Override protected void createAndStartYarnClient() { + } + }; cli.setClient(client); cli.setSysOutPrintStream(sysOut); cli.setSysErrPrintStream(sysErr); @@ -2152,6 +2150,9 @@ private String createApplicationCLIHelpMessage() throws IOException { pw.println(" deprecated, this new command"); pw.println(" 'changeQueue' performs same"); pw.println(" functionality."); + pw.println(" -clusterId ClusterId. By default, it will"); + pw.println(" take default cluster id from the"); + pw.println(" RM"); pw.println(" -component Works with -flex option to"); pw.println(" change the number of"); pw.println(" components/containers running"); @@ -2298,6 +2299,8 @@ private String createApplicationAttemptCLIHelpMessage() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(baos); pw.println("usage: applicationattempt"); + pw.println(" -clusterId ClusterId. By default, it will take"); + pw.println(" default cluster id from the RM"); pw.println(" -fail Fails application attempt."); pw.println(" -help Displays help for all commands."); pw.println(" -list List application attempts for"); @@ -2314,6 +2317,7 @@ private String createContainerCLIHelpMessage() throws IOException { PrintWriter pw = new PrintWriter(baos); pw.println("usage: container"); pw.println(" -appTypes Works with -list to specify the app type when application name is provided."); + pw.println(" -clusterId ClusterId. By default, it will take default cluster id from the RM "); pw.println(" -components Works with -list to filter instances based on input comma-separated list of component names."); pw.println(" -help Displays help for all commands."); pw.println(" -list List containers for application attempt when application attempt ID is provided. When application name is provided, then it finds the instances of the application based on app's own implementation, and -appTypes option must be specified unless it is the default yarn-service type. With app name, it supports optional use of -version to filter instances based on app version, -components to filter instances based on component names, -states to filter instances based on instance state.");