From febedd64e998c70594d84e2dc273cc0a469544e2 Mon Sep 17 00:00:00 2001 From: Vinod Kumar Vavilapalli Date: Thu, 29 Aug 2013 17:55:30 +0000 Subject: [PATCH] YARN-1080. Improved help message for "yarn logs" command. Contributed by Xuan Gong. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1518731 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-yarn-project/CHANGES.txt | 3 ++ .../hadoop/yarn/logaggregation/LogDumper.java | 30 +++++++++---- .../yarn/logaggregation/TestLogDumper.java | 42 +++++++++++++++++++ 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 939aafe0f8..c9e3df0f54 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -63,6 +63,9 @@ Release 2.1.1-beta - UNRELEASED YARN-1034. Remove "experimental" in the Fair Scheduler documentation. (Karthik Kambatla via Sandy Ryza) + YARN-1080. Improved help message for "yarn logs" command. (Xuan Gong via + vinodkv) + OPTIMIZATIONS BUG FIXES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogDumper.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogDumper.java index f9e6c14089..1e7ed44e2d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogDumper.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/logaggregation/LogDumper.java @@ -72,10 +72,18 @@ public int run(String[] args) throws Exception { + "nodename:port (must be specified if container id is specified)"); opts.addOption(APP_OWNER_OPTION, true, "AppOwner (assumed to be current user if not specified)"); + opts.getOption(APPLICATION_ID_OPTION).setArgName("Application ID"); + opts.getOption(CONTAINER_ID_OPTION).setArgName("Container ID"); + opts.getOption(NODE_ADDRESS_OPTION).setArgName("Node Address"); + opts.getOption(APP_OWNER_OPTION).setArgName("Application Owner"); + + Options printOpts = new Options(); + printOpts.addOption(opts.getOption(CONTAINER_ID_OPTION)); + printOpts.addOption(opts.getOption(NODE_ADDRESS_OPTION)); + printOpts.addOption(opts.getOption(APP_OWNER_OPTION)); if (args.length < 1) { - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp("general options are: ", opts); + printHelpMessage(printOpts); return -1; } @@ -92,16 +100,13 @@ public int run(String[] args) throws Exception { appOwner = commandLine.getOptionValue(APP_OWNER_OPTION); } catch (ParseException e) { System.out.println("options parsing failed: " + e.getMessage()); - - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp("general options are: ", opts); + printHelpMessage(printOpts); return -1; } if (appIdStr == null) { System.out.println("ApplicationId cannot be null!"); - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp("general options are: ", opts); + printHelpMessage(printOpts); return -1; } @@ -119,8 +124,7 @@ public int run(String[] args) throws Exception { } else if ((containerIdStr == null && nodeAddress != null) || (containerIdStr != null && nodeAddress == null)) { System.out.println("ContainerId or NodeAddress cannot be null!"); - HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp("general options are: ", opts); + printHelpMessage(printOpts); resultCode = -1; } else { Path remoteRootLogDir = @@ -255,4 +259,12 @@ public static void main(String[] args) throws Exception { int exitCode = logDumper.run(args); System.exit(exitCode); } + + private void printHelpMessage(Options options) { + System.out.println("Retrieve logs for completed YARN applications."); + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("yarn logs -applicationId [OPTIONS]", new Options()); + formatter.setSyntaxPrefix(""); + formatter.printHelp("general options are:", options); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/logaggregation/TestLogDumper.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/logaggregation/TestLogDumper.java index 527a551f2d..683b80c2c5 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/logaggregation/TestLogDumper.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/logaggregation/TestLogDumper.java @@ -19,14 +19,30 @@ package org.apache.hadoop.yarn.logaggregation; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; + +import junit.framework.Assert; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.LocalFileSystem; import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.junit.Before; import org.junit.Test; public class TestLogDumper { + ByteArrayOutputStream sysOutStream; + private PrintStream sysOut; + + @Before + public void setUp() { + sysOutStream = new ByteArrayOutputStream(); + sysOut = new PrintStream(sysOutStream); + System.setOut(sysOut); + } + @Test public void testFailResultCodes() throws Exception { Configuration conf = new YarnConfiguration(); @@ -44,4 +60,30 @@ public void testFailResultCodes() throws Exception { "nonexistentnode:1234", "nobody"); assertTrue("Should return an error code", exitCode != 0); } + + @Test + public void testHelpMessage() throws Exception { + Configuration conf = new YarnConfiguration(); + LogDumper dumper = new LogDumper(); + dumper.setConf(conf); + + int exitCode = dumper.run(new String[]{}); + assertTrue(exitCode == -1); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(baos); + pw.println("Retrieve logs for completed YARN applications."); + pw.println("usage: yarn logs -applicationId [OPTIONS]"); + pw.println(); + pw.println("general options are:"); + pw.println(" -appOwner AppOwner (assumed to be current user if"); + pw.println(" not specified)"); + pw.println(" -containerId ContainerId (must be specified if node"); + pw.println(" address is specified)"); + pw.println(" -nodeAddress NodeAddress in the format nodename:port"); + pw.println(" (must be specified if container id is"); + pw.println(" specified)"); + pw.close(); + String appReportStr = baos.toString("UTF-8"); + Assert.assertEquals(appReportStr, sysOutStream.toString()); + } }