From ccb07919044ed4e49a7fd24e4214342200da1fcd Mon Sep 17 00:00:00 2001 From: Jian He Date: Wed, 6 Jul 2016 10:37:44 -0700 Subject: [PATCH] =?UTF-8?q?YARN-5227.=20Yarn=20logs=20command:=20no=20need?= =?UTF-8?q?=20to=20specify=20applicationId=20when=20specifying=20container?= =?UTF-8?q?Id.=20Contributed=20by=20Gergely=20Nov=C3=A1k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit d169f5052fe83debcea7cf2f317dcd990890a857) --- .../hadoop/yarn/client/cli/LogsCLI.java | 47 ++++++++++++------- .../hadoop/yarn/client/cli/TestLogsCLI.java | 26 ++++++++-- 2 files changed, 52 insertions(+), 21 deletions(-) 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 9a520a17c0c..ad4964af6d3 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 @@ -145,18 +145,38 @@ public int run(String[] args) throws Exception { return -1; } - if (appIdStr == null) { - System.err.println("ApplicationId cannot be null!"); + if (appIdStr == null && containerIdStr == null) { + System.err.println("Both applicationId and containerId are missing, " + + " one of them must be specified."); printHelpMessage(printOpts); return -1; } ApplicationId appId = null; - try { - appId = ApplicationId.fromString(appIdStr); - } catch (Exception e) { - System.err.println("Invalid ApplicationId specified"); - return -1; + if (appIdStr != null) { + try { + appId = ApplicationId.fromString(appIdStr); + } catch (Exception e) { + System.err.println("Invalid ApplicationId specified"); + return -1; + } + } + + if (containerIdStr != null) { + try { + ContainerId containerId = ContainerId.fromString(containerIdStr); + if (appId == null) { + appId = containerId.getApplicationAttemptId().getApplicationId(); + } else if (!containerId.getApplicationAttemptId().getApplicationId() + .equals(appId)) { + System.err.println("The Application:" + appId + + " does not have the container:" + containerId); + return -1; + } + } catch (Exception e) { + System.err.println("Invalid ContainerId specified"); + return -1; + } } LogCLIHelpers logCliHelper = new LogCLIHelpers(); @@ -218,13 +238,6 @@ public int run(String[] args) throws Exception { int resultCode = 0; if (containerIdStr != null) { - ContainerId containerId = ContainerId.fromString(containerIdStr); - if (!containerId.getApplicationAttemptId().getApplicationId() - .equals(appId)) { - System.err.println("The Application:" + appId - + " does not have the container:" + containerId); - return -1; - } return fetchContainerLogs(request, logCliHelper); } else { if (nodeAddress == null) { @@ -628,11 +641,11 @@ private Options createCommandOpts() { opts.addOption(HELP_CMD, false, "Displays help for all commands."); Option appIdOpt = new Option(APPLICATION_ID_OPTION, true, "ApplicationId (required)"); - appIdOpt.setRequired(true); opts.addOption(appIdOpt); opts.addOption(CONTAINER_ID_OPTION, true, "ContainerId. " - + "By default, it will only print syslog if the application is runing." - + " Work with -logFiles to get other logs."); + + "By default, it will only print syslog if the application is running." + + " Work with -logFiles to get other logs. If specified, the" + + " applicationId can be omitted"); opts.addOption(NODE_ADDRESS_OPTION, true, "NodeAddress in the format " + "nodename:port"); opts.addOption(APP_OWNER_OPTION, true, 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 85287ea2bd4..34369ef54f2 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 @@ -201,8 +201,9 @@ public void testHelpMessage() throws Exception { pw.println(" not specified)"); pw.println(" -containerId ContainerId. By default, it will only"); pw.println(" print syslog if the application is"); - pw.println(" runing. Work with -logFiles to get other"); - pw.println(" logs."); + pw.println(" running. Work with -logFiles to get other"); + pw.println(" logs. If specified, the applicationId can"); + pw.println(" be omitted"); pw.println(" -help Displays help for all commands."); pw.println(" -list_nodes Show the list of nodes that successfully"); pw.println(" aggregated logs. This option can only be"); @@ -498,6 +499,24 @@ public ContainerReport getContainerReport(String containerIdStr) containerId3 + " on " + LogAggregationUtils.getNodeString(nodeId))); sysOutStream.reset(); + // The same should also work without the applicationId + exitCode = + cli.run(new String[] { "-containerId", containerId3.toString() }); + assertTrue(exitCode == 0); + assertTrue(sysOutStream.toString().contains( + "Hello container_0_0001_01_000003 in syslog!")); + assertTrue(sysOutStream.toString().contains( + "Hello container_0_0001_01_000003 in stdout!")); + assertTrue(sysOutStream.toString().contains( + containerId3 + " on " + LogAggregationUtils.getNodeString(nodeId))); + sysOutStream.reset(); + + exitCode = cli.run(new String[] { "-containerId", "invalid_container" }); + assertTrue(exitCode == -1); + assertTrue(sysErrStream.toString().contains( + "Invalid ContainerId specified")); + sysErrStream.reset(); + fs.delete(new Path(remoteLogRootDir), true); fs.delete(new Path(rootLogDir), true); } @@ -863,8 +882,7 @@ public void testPrintContainerLogMetadata() throws Exception { "-show_meta_info", "-nodeAddress", "localhost", "-containerId", "container_1234" }); assertTrue(sysErrStream.toString().contains( - "The container container_1234 couldn't be found on the node " - + "specified: localhost")); + "Invalid ContainerId specified")); sysErrStream.reset(); fs.delete(new Path(remoteLogRootDir), true);