Browse Source

YARN-5083. YARN CLI for AM logs does not give any error message if entered invalid am value. Contributed by Jian He.

Junping Du 9 years ago
parent
commit
e14ee0d3b5

+ 6 - 1
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);
     formatter.printHelp("general options are:", options);
   }
   }
 
 
-  private List<JSONObject> getAMContainerInfoForRMWebService(
+  protected List<JSONObject> getAMContainerInfoForRMWebService(
       Configuration conf, String appId) throws ClientHandlerException,
       Configuration conf, String appId) throws ClientHandlerException,
       UniformInterfaceException, JSONException {
       UniformInterfaceException, JSONException {
     Client webServiceClient = Client.create();
     Client webServiceClient = Client.create();
@@ -538,6 +538,11 @@ public class LogsCLI extends Configured implements Tool {
           if (amContainerId <= requests.size()) {
           if (amContainerId <= requests.size()) {
             outputAMContainerLogs(requests.get(amContainerId - 1), conf,
             outputAMContainerLogs(requests.get(amContainerId - 1), conf,
                 logCliHelper);
                 logCliHelper);
+          } else {
+            System.err.println(String.format("ERROR: Specified AM containerId"
+                + " (%s) exceeds the number of AM containers (%s).",
+                amContainerId, requests.size()));
+            return -1;
           }
           }
         }
         }
       }
       }

+ 23 - 0
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.ContainerLogsRequest;
 import org.apache.hadoop.yarn.logaggregation.LogAggregationUtils;
 import org.apache.hadoop.yarn.logaggregation.LogAggregationUtils;
 import org.apache.hadoop.yarn.logaggregation.LogCLIHelpers;
 import org.apache.hadoop.yarn.logaggregation.LogCLIHelpers;
+import org.codehaus.jettison.json.JSONObject;
 import org.junit.Assert;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.Test;
@@ -131,6 +132,28 @@ public class TestLogsCLI {
     assertTrue(sysErrStream.toString().startsWith("Invalid ApplicationId specified"));
     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<JSONObject> 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)
   @Test(timeout = 5000l)
   public void testUnknownApplicationId() throws Exception {
   public void testUnknownApplicationId() throws Exception {
     Configuration conf = new YarnConfiguration();
     Configuration conf = new YarnConfiguration();