diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java index ebd5428810e..d28c77cdec7 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java @@ -274,7 +274,7 @@ public class LogsCLI extends Configured implements Tool { formatter.printHelp("general options are:", options); } - private List getAMContainerInfoForRMWebService( + protected List getAMContainerInfoForRMWebService( Configuration conf, String appId) throws ClientHandlerException, UniformInterfaceException, JSONException { Client webServiceClient = Client.create(); @@ -538,6 +538,11 @@ public class LogsCLI extends Configured implements Tool { if (amContainerId <= requests.size()) { outputAMContainerLogs(requests.get(amContainerId - 1), conf, logCliHelper); + } else { + System.err.println(String.format("ERROR: Specified AM containerId" + + " (%s) exceeds the number of AM containers (%s).", + amContainerId, requests.size())); + return -1; } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java index 10047e38333..df1c6b7dd07 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/cli/TestLogsCLI.java @@ -71,6 +71,7 @@ import org.apache.hadoop.yarn.logaggregation.AggregatedLogFormat; import org.apache.hadoop.yarn.logaggregation.ContainerLogsRequest; import org.apache.hadoop.yarn.logaggregation.LogAggregationUtils; import org.apache.hadoop.yarn.logaggregation.LogCLIHelpers; +import org.codehaus.jettison.json.JSONObject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -131,6 +132,28 @@ public class TestLogsCLI { assertTrue(sysErrStream.toString().startsWith("Invalid ApplicationId specified")); } + @Test(timeout = 5000L) + public void testInvalidAMContainerId() throws Exception { + Configuration conf = new YarnConfiguration(); + conf.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, true); + YarnClient mockYarnClient = + createMockYarnClient(YarnApplicationState.FINISHED, + UserGroupInformation.getCurrentUser().getShortUserName()); + LogsCLI cli = spy(new LogsCLIForTest(mockYarnClient)); + List list = Arrays.asList(new JSONObject()); + doReturn(list).when(cli) + .getAMContainerInfoForRMWebService(any(Configuration.class), + any(String.class)); + cli.setConf(conf); + + int exitCode = cli.run( + new String[] {"-applicationId", "application_1465862913885_0027", + "-am", "1000" }); + assertTrue(exitCode == -1); + assertTrue(sysErrStream.toString() + .contains("exceeds the number of AM containers")); + } + @Test(timeout = 5000l) public void testUnknownApplicationId() throws Exception { Configuration conf = new YarnConfiguration();