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 a847cd5..7570ef5 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 @@ -41,7 +41,9 @@ import org.apache.hadoop.yarn.api.records.ApplicationResourceUsageReport; import org.apache.hadoop.yarn.api.records.ContainerReport; import org.apache.hadoop.yarn.api.records.YarnApplicationState; +import org.apache.hadoop.yarn.exceptions.ApplicationAttemptNotFoundException; import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException; +import org.apache.hadoop.yarn.exceptions.ContainerNotFoundException; import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.util.ConverterUtils; @@ -151,12 +153,14 @@ public int run(String[] args) throws Exception { return exitCode; } if (args[0].equalsIgnoreCase(APPLICATION)) { - printApplicationReport(cliParser.getOptionValue(STATUS_CMD)); + exitCode = printApplicationReport(cliParser.getOptionValue(STATUS_CMD)); } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { - printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD)); + exitCode = printApplicationAttemptReport(cliParser + .getOptionValue(STATUS_CMD)); } else if (args[0].equalsIgnoreCase(CONTAINER)) { - printContainerReport(cliParser.getOptionValue(STATUS_CMD)); + exitCode = printContainerReport(cliParser.getOptionValue(STATUS_CMD)); } + return exitCode; } else if (cliParser.hasOption(LIST_CMD)) { if (args[0].equalsIgnoreCase(APPLICATION)) { allAppStates = false; @@ -251,13 +255,24 @@ void printUsage(String title, Options opts) { * Prints the application attempt report for an application attempt id. * * @param applicationAttemptId + * @return * @throws YarnException */ - private void printApplicationAttemptReport(String applicationAttemptId) + private int printApplicationAttemptReport(String applicationAttemptId) throws YarnException, IOException { - ApplicationAttemptReport appAttemptReport = client - .getApplicationAttemptReport(ConverterUtils - .toApplicationAttemptId(applicationAttemptId)); + ApplicationAttemptReport appAttemptReport = null; + try { + appAttemptReport = client.getApplicationAttemptReport(ConverterUtils + .toApplicationAttemptId(applicationAttemptId)); + } catch (ApplicationNotFoundException e) { + sysout.println("Application for AppAttempt with id '" + + applicationAttemptId + "' doesn't exist in RM or Timeline Server."); + return -1; + } catch (ApplicationAttemptNotFoundException e) { + sysout.println("Application Attempt with id '" + applicationAttemptId + + "' doesn't exist in RM or Timeline Server."); + return -1; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter appAttemptReportStr = new PrintWriter(baos); @@ -281,22 +296,39 @@ private void printApplicationAttemptReport(String applicationAttemptId) appAttemptReportStr.print(appAttemptReport.getDiagnostics()); } else { appAttemptReportStr.print("Application Attempt with id '" - + applicationAttemptId + "' doesn't exist in History Server."); + + applicationAttemptId + "' doesn't exist in Timeline Server."); } appAttemptReportStr.close(); sysout.println(baos.toString("UTF-8")); + return 0; } /** * Prints the container report for an container id. * * @param containerId + * @return * @throws YarnException */ - private void printContainerReport(String containerId) throws YarnException, + private int printContainerReport(String containerId) throws YarnException, IOException { - ContainerReport containerReport = client.getContainerReport((ConverterUtils - .toContainerId(containerId))); + ContainerReport containerReport = null; + try { + containerReport = client.getContainerReport((ConverterUtils + .toContainerId(containerId))); + } catch (ApplicationNotFoundException e) { + sysout.println("Application for Container with id '" + containerId + + "' doesn't exist in RM or Timeline Server."); + return -1; + } catch (ApplicationAttemptNotFoundException e) { + sysout.println("Application Attempt for Container with id '" + + containerId + "' doesn't exist in RM or Timeline Server."); + return -1; + } catch (ContainerNotFoundException e) { + sysout.println("Container with id '" + containerId + + "' doesn't exist in RM or Timeline Server."); + return -1; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter containerReportStr = new PrintWriter(baos); @@ -322,6 +354,7 @@ private void printContainerReport(String containerId) throws YarnException, } containerReportStr.close(); sysout.println(baos.toString("UTF-8")); + return 0; } /** @@ -422,12 +455,20 @@ private void moveApplicationAcrossQueues(String applicationId, String queue) * Prints the application report for an application id. * * @param applicationId + * @return * @throws YarnException */ - private void printApplicationReport(String applicationId) + private int printApplicationReport(String applicationId) throws YarnException, IOException { - ApplicationReport appReport = client.getApplicationReport(ConverterUtils - .toApplicationId(applicationId)); + ApplicationReport appReport = null; + try { + appReport = client.getApplicationReport(ConverterUtils + .toApplicationId(applicationId)); + } catch (ApplicationNotFoundException e) { + sysout.println("Application with id '" + applicationId + + "' doesn't exist in RM or Timeline Server."); + return -1; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter appReportStr = new PrintWriter(baos); @@ -480,6 +521,7 @@ private void printApplicationReport(String applicationId) } appReportStr.close(); sysout.println(baos.toString("UTF-8")); + return 0; } private String getAllValidApplicationStates() { 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 9d9a86a..f21add8 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 @@ -64,7 +64,9 @@ import org.apache.hadoop.yarn.api.records.YarnApplicationAttemptState; import org.apache.hadoop.yarn.api.records.YarnApplicationState; import org.apache.hadoop.yarn.client.api.YarnClient; +import org.apache.hadoop.yarn.exceptions.ApplicationAttemptNotFoundException; import org.apache.hadoop.yarn.exceptions.ApplicationNotFoundException; +import org.apache.hadoop.yarn.exceptions.ContainerNotFoundException; import org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager; import org.apache.hadoop.yarn.util.Records; import org.jboss.netty.logging.CommonsLoggerFactory; @@ -306,14 +308,12 @@ public void testGetApplicationReportException() throws Exception { when(client.getApplicationReport(any(ApplicationId.class))).thenThrow( new ApplicationNotFoundException("History file for application" + applicationId + " is not found")); - try { - cli.run(new String[] { "application", "-status", applicationId.toString() }); - Assert.fail(); - } catch (Exception ex) { - Assert.assertTrue(ex instanceof ApplicationNotFoundException); - Assert.assertEquals("History file for application" - + applicationId + " is not found", ex.getMessage()); - } + int exitCode = cli.run(new String[] { "application", "-status", + applicationId.toString() }); + verify(sysOut).println( + "Application with id '" + applicationId + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); } @Test @@ -1305,6 +1305,80 @@ public void testGetQueueInfoWithNonExistedQueue() throws Exception { Assert.assertEquals(queueInfoStr, sysOutStream.toString()); } + @Test + public void testGetApplicationAttemptReportException() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + ApplicationId applicationId = ApplicationId.newInstance(1234, 5); + ApplicationAttemptId attemptId1 = ApplicationAttemptId.newInstance( + applicationId, 1); + when(client.getApplicationAttemptReport(attemptId1)).thenThrow( + new ApplicationNotFoundException("History file for application" + + applicationId + " is not found")); + + int exitCode = cli.run(new String[] { "applicationattempt", "-status", + attemptId1.toString() }); + verify(sysOut).println( + "Application for AppAttempt with id '" + attemptId1 + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + + ApplicationAttemptId attemptId2 = ApplicationAttemptId.newInstance( + applicationId, 2); + when(client.getApplicationAttemptReport(attemptId2)).thenThrow( + new ApplicationAttemptNotFoundException( + "History file for application attempt" + attemptId2 + + " is not found")); + + exitCode = cli.run(new String[] { "applicationattempt", "-status", + attemptId2.toString() }); + verify(sysOut).println( + "Application Attempt with id '" + attemptId2 + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } + + @Test + public void testGetContainerReportException() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + ApplicationId applicationId = ApplicationId.newInstance(1234, 5); + ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance( + applicationId, 1); + long cntId = 1; + ContainerId containerId1 = ContainerId.newContainerId(attemptId, cntId++); + when(client.getContainerReport(containerId1)).thenThrow( + new ApplicationNotFoundException("History file for application" + + applicationId + " is not found")); + + int exitCode = cli.run(new String[] { "container", "-status", + containerId1.toString() }); + verify(sysOut).println( + "Application for Container with id '" + containerId1 + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + ContainerId containerId2 = ContainerId.newContainerId(attemptId, cntId++); + when(client.getContainerReport(containerId2)).thenThrow( + new ApplicationAttemptNotFoundException( + "History file for application attempt" + attemptId + + " is not found")); + + exitCode = cli.run(new String[] { "container", "-status", + containerId2.toString() }); + verify(sysOut).println( + "Application Attempt for Container with id '" + containerId2 + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + + ContainerId containerId3 = ContainerId.newContainerId(attemptId, cntId++); + when(client.getContainerReport(containerId3)).thenThrow( + new ContainerNotFoundException("History file for container" + + containerId3 + " is not found")); + exitCode = cli.run(new String[] { "container", "-status", + containerId3.toString() }); + verify(sysOut).println( + "Container with id '" + containerId3 + + "' doesn't exist in RM or Timeline Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } private void verifyUsageInfo(YarnCLI cli) throws Exception { cli.setSysErrPrintStream(sysErr); -- 1.9.4.msysgit.1