YARN-5227. Yarn logs command: no need to specify applicationId when

specifying containerId. Contributed by Gergely Novák
This commit is contained in:
Jian He 2016-07-06 10:37:44 -07:00
parent 04f6ebb66a
commit d169f5052f
2 changed files with 52 additions and 21 deletions

View File

@ -145,19 +145,39 @@ public class LogsCLI extends Configured implements Tool {
return -1; return -1;
} }
if (appIdStr == null) { if (appIdStr == null && containerIdStr == null) {
System.err.println("ApplicationId cannot be null!"); System.err.println("Both applicationId and containerId are missing, "
+ " one of them must be specified.");
printHelpMessage(printOpts); printHelpMessage(printOpts);
return -1; return -1;
} }
ApplicationId appId = null; ApplicationId appId = null;
if (appIdStr != null) {
try { try {
appId = ApplicationId.fromString(appIdStr); appId = ApplicationId.fromString(appIdStr);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Invalid ApplicationId specified"); System.err.println("Invalid ApplicationId specified");
return -1; 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(); LogCLIHelpers logCliHelper = new LogCLIHelpers();
logCliHelper.setConf(getConf()); logCliHelper.setConf(getConf());
@ -218,13 +238,6 @@ public class LogsCLI extends Configured implements Tool {
int resultCode = 0; int resultCode = 0;
if (containerIdStr != null) { 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); return fetchContainerLogs(request, logCliHelper);
} else { } else {
if (nodeAddress == null) { if (nodeAddress == null) {
@ -629,11 +642,11 @@ public class LogsCLI extends Configured implements Tool {
opts.addOption(HELP_CMD, false, "Displays help for all commands."); opts.addOption(HELP_CMD, false, "Displays help for all commands.");
Option appIdOpt = Option appIdOpt =
new Option(APPLICATION_ID_OPTION, true, "ApplicationId (required)"); new Option(APPLICATION_ID_OPTION, true, "ApplicationId (required)");
appIdOpt.setRequired(true);
opts.addOption(appIdOpt); opts.addOption(appIdOpt);
opts.addOption(CONTAINER_ID_OPTION, true, "ContainerId. " opts.addOption(CONTAINER_ID_OPTION, true, "ContainerId. "
+ "By default, it will only print syslog if the application is runing." + "By default, it will only print syslog if the application is running."
+ " Work with -logFiles to get other logs."); + " Work with -logFiles to get other logs. If specified, the"
+ " applicationId can be omitted");
opts.addOption(NODE_ADDRESS_OPTION, true, "NodeAddress in the format " opts.addOption(NODE_ADDRESS_OPTION, true, "NodeAddress in the format "
+ "nodename:port"); + "nodename:port");
opts.addOption(APP_OWNER_OPTION, true, opts.addOption(APP_OWNER_OPTION, true,

View File

@ -201,8 +201,9 @@ public class TestLogsCLI {
pw.println(" not specified)"); pw.println(" not specified)");
pw.println(" -containerId <Container ID> ContainerId. By default, it will only"); pw.println(" -containerId <Container ID> ContainerId. By default, it will only");
pw.println(" print syslog if the application is"); pw.println(" print syslog if the application is");
pw.println(" runing. Work with -logFiles to get other"); pw.println(" running. Work with -logFiles to get other");
pw.println(" logs."); pw.println(" logs. If specified, the applicationId can");
pw.println(" be omitted");
pw.println(" -help Displays help for all commands."); pw.println(" -help Displays help for all commands.");
pw.println(" -list_nodes Show the list of nodes that successfully"); pw.println(" -list_nodes Show the list of nodes that successfully");
pw.println(" aggregated logs. This option can only be"); pw.println(" aggregated logs. This option can only be");
@ -498,6 +499,24 @@ public class TestLogsCLI {
containerId3 + " on " + LogAggregationUtils.getNodeString(nodeId))); containerId3 + " on " + LogAggregationUtils.getNodeString(nodeId)));
sysOutStream.reset(); 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(remoteLogRootDir), true);
fs.delete(new Path(rootLogDir), true); fs.delete(new Path(rootLogDir), true);
} }
@ -863,8 +882,7 @@ public class TestLogsCLI {
"-show_meta_info", "-nodeAddress", "localhost", "-containerId", "-show_meta_info", "-nodeAddress", "localhost", "-containerId",
"container_1234" }); "container_1234" });
assertTrue(sysErrStream.toString().contains( assertTrue(sysErrStream.toString().contains(
"The container container_1234 couldn't be found on the node " "Invalid ContainerId specified"));
+ "specified: localhost"));
sysErrStream.reset(); sysErrStream.reset();
fs.delete(new Path(remoteLogRootDir), true); fs.delete(new Path(remoteLogRootDir), true);