Index: hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java =================================================================== --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java (revision 1613350) +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/ApplicationCLI.java (working copy) @@ -40,7 +40,9 @@ import org.apache.hadoop.yarn.api.records.ApplicationReport; 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; @@ -149,13 +151,22 @@ printUsage(title, opts); return exitCode; } - if (args[0].equalsIgnoreCase(APPLICATION)) { - printApplicationReport(cliParser.getOptionValue(STATUS_CMD)); - } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { - printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD)); - } else if (args[0].equalsIgnoreCase(CONTAINER)) { - printContainerReport(cliParser.getOptionValue(STATUS_CMD)); - } + + try { + if (args[0].equalsIgnoreCase(APPLICATION)) { + printApplicationReport(cliParser.getOptionValue(STATUS_CMD)); + } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) { + printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD)); + } else if (args[0].equalsIgnoreCase(CONTAINER)) { + printContainerReport(cliParser.getOptionValue(STATUS_CMD)); + } + } catch (ApplicationNotFoundException e) { + return exitCode; + } catch (ApplicationAttemptNotFoundException e) { + return exitCode; + } catch (ContainerNotFoundException e) { + return exitCode; + } } else if (cliParser.hasOption(LIST_CMD)) { if (args[0].equalsIgnoreCase(APPLICATION)) { allAppStates = false; @@ -254,9 +265,19 @@ */ private void 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 History Server."); + throw e; + } catch (ApplicationAttemptNotFoundException e) { + sysout.println("Application Attempt with id '" + applicationAttemptId + + "' doesn't exist in RM or History Server."); + throw e; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter appAttemptReportStr = new PrintWriter(baos); @@ -294,8 +315,23 @@ */ private void 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 History Server."); + throw e; + } catch (ApplicationAttemptNotFoundException e) { + sysout.println("Application Attempt for Container with id '" + + containerId + "' doesn't exist in RM or History Server."); + throw e; + } catch (ContainerNotFoundException e) { + sysout.println("Container with id '" + containerId + + "' doesn't exist in RM or History Server."); + throw e; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter containerReportStr = new PrintWriter(baos); @@ -425,8 +461,15 @@ */ private void 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 History Server."); + throw e; + } // Use PrintWriter.println, which uses correct platform line ending. ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter appReportStr = new PrintWriter(baos); Index: 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 (revision 1613350) +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestYarnCLI.java (working copy) @@ -58,7 +58,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.util.Records; import org.junit.Before; import org.junit.Test; @@ -289,16 +291,126 @@ 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 History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ApplicationNotFoundException appEx) { + Assert.fail("application -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); } } + + @Test + public void testGetApplicationAttemptReportException() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + ApplicationId applicationId = ApplicationId.newInstance(5678, 1); + ApplicationAttemptId attemptId1 = ApplicationAttemptId.newInstance( + applicationId, 1); + when(client.getApplicationAttemptReport(attemptId1)).thenThrow( + new ApplicationNotFoundException("History file for application" + + applicationId + " is not found")); + try { + 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 History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ApplicationNotFoundException appEx) { + Assert.fail("applicationattempt -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); + } + ApplicationAttemptId attemptId2 = ApplicationAttemptId.newInstance( + applicationId, 2); + when(client.getApplicationAttemptReport(attemptId2)).thenThrow( + new ApplicationAttemptNotFoundException( + "History file for application attempt" + attemptId2 + + " is not found")); + try { + int exitCode = cli.run(new String[]{"applicationattempt", "-status", + attemptId2.toString()}); + verify(sysOut).println( + "Application Attempt with id '" + attemptId2 + + "' doesn't exist in RM or History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ApplicationAttemptNotFoundException appEx) { + Assert.fail("applicationattempt -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); + } + } + @Test + public void testGetContainerReportException() throws Exception { + ApplicationCLI cli = createAndGetAppCLI(); + ApplicationId applicationId = ApplicationId.newInstance(1234, 5); + ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance( + applicationId, 1); + ContainerId containerId1 = ContainerId.newInstance(attemptId, 1); + when(client.getContainerReport(containerId1)).thenThrow( + new ApplicationNotFoundException("History file for application" + + applicationId + " is not found")); + try { + 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 History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ApplicationNotFoundException appEx) { + Assert.fail("container -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); + } + + ContainerId containerId2 = ContainerId.newInstance(attemptId, 2); + when(client.getContainerReport(containerId2)).thenThrow( + new ApplicationAttemptNotFoundException( + "History file for application attempt" + attemptId + + " is not found")); + try { + int 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 History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ApplicationAttemptNotFoundException appEx) { + Assert.fail("container -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); + } + + ContainerId containerId3 = ContainerId.newInstance(attemptId, 3); + when(client.getContainerReport(containerId3)).thenThrow( + new ContainerNotFoundException("History file for container" + + containerId3 + " is not found")); + try { + int exitCode = cli.run(new String[]{"container", "-status", + containerId3.toString()}); + verify(sysOut).println( + "Container with id '" + containerId3 + + "' doesn't exist in RM or History Server."); + Assert.assertNotSame("should return non-zero exit code.", 0, exitCode); + } catch (ContainerNotFoundException appEx) { + Assert.fail("container -status should not throw" + + "ApplicationNotFoundException. " + appEx); + } catch (Exception e) { + Assert.fail("Unexpected exception: " + e); + } + } + + @Test public void testGetApplications() throws Exception { ApplicationCLI cli = createAndGetAppCLI(); ApplicationId applicationId = ApplicationId.newInstance(1234, 5);