Browse Source

HADOOP-3101. Prevent JobClient from throwing an exception when printing
usage. Contributed by Edward J. Yoon.



git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@653540 13f79535-47bb-0310-9956-ffa450edef68

Christopher Douglas 17 years ago
parent
commit
81543bcf8d

+ 3 - 0
CHANGES.txt

@@ -142,6 +142,9 @@ Trunk (unreleased changes)
     changed Writable serialization of DatanodeInfo. This patch handles it.
     changed Writable serialization of DatanodeInfo. This patch handles it.
     (Tsz Wo (Nicholas), SZE via rangadi)
     (Tsz Wo (Nicholas), SZE via rangadi)
 
 
+    HADOOP-3101. Prevent JobClient from throwing an exception when printing
+    usage. (Edward J. Yoon via cdouglas)
+
 Release 0.17.0 - Unreleased
 Release 0.17.0 - Unreleased
 
 
   INCOMPATIBLE CHANGES
   INCOMPATIBLE CHANGES

+ 74 - 42
src/java/org/apache/hadoop/mapred/JobClient.java

@@ -1199,23 +1199,42 @@ public class JobClient extends Configured implements MRConstants, Tool  {
   /**
   /**
    * Display usage of the command-line tool and terminate execution
    * Display usage of the command-line tool and terminate execution
    */
    */
-  private void displayUsage() {
-    System.out.printf("JobClient <command> <args>\n");
-    System.out.printf("\t-submit\t<job-file>\n");
-    System.out.printf("\t-status\t<job-id>\n");
-    System.out.printf("\t-kill\t<job-id>\n");
-    System.out.printf("\t-events\t<job-id> <from-event-#> <#-of-events>\n");
-    System.out.printf("\t-history\t<jobOutputDir>\n");
-    System.out.printf("\t-list\n");
-    System.out.printf("\t-list\tall\n");
-    System.out.printf("\t-kill-task <task-id>\n");
-    System.out.printf("\t-fail-task <task-id>\n\n");
-    ToolRunner.printGenericCommandUsage(System.out);
-    throw new RuntimeException("JobClient: bad command-line arguments");
+  private void displayUsage(String cmd) {
+    String prefix = "Usage: JobClient ";
+    if("-submit".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " <job-file>]");
+    } else if ("-status".equals(cmd) || "-kill".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " <job-id>]");
+    } else if ("-events".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " <job-id> <from-event-#> <#-of-events>]");
+    } else if ("-history".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " <jobOutputDir>]");
+    } else if ("-list".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " [all]]");
+    } else if ("-kill-task".equals(cmd) || "-fail-task".equals(cmd)) {
+      System.err.println(prefix + "[" + cmd + " <task-id>]");
+    } else {
+      System.err.printf(prefix + "<command> <args>\n");
+      System.err.printf("\t[-submit <job-file>]\n");
+      System.err.printf("\t[-status <job-id>]\n");
+      System.err.printf("\t[-kill <job-id>]\n");
+      System.err.printf("\t[-events <job-id> <from-event-#> <#-of-events>]\n");
+      System.err.printf("\t[-history <jobOutputDir>]\n");
+      System.err.printf("\t[-list [all]]\n");
+      System.err.printf("\t[-kill-task <task-id>]\n");
+      System.err.printf("\t[-fail-task <task-id>]\n\n");
+      ToolRunner.printGenericCommandUsage(System.out);
+    }
   }
   }
     
     
   public int run(String[] argv) throws Exception {
   public int run(String[] argv) throws Exception {
+    int exitCode = -1;
+    if (argv.length < 1) {
+      displayUsage("");
+      return exitCode;
+    }    
     // process arguments
     // process arguments
+    String cmd = argv[0];
     String submitJobFile = null;
     String submitJobFile = null;
     String jobid = null;
     String jobid = null;
     String taskid = null;
     String taskid = null;
@@ -1231,34 +1250,41 @@ public class JobClient extends Configured implements MRConstants, Tool  {
     boolean listAllJobs = false;
     boolean listAllJobs = false;
     boolean killTask = false;
     boolean killTask = false;
     boolean failTask = false;
     boolean failTask = false;
-    
-    if (argv.length < 1)
-      displayUsage();
 
 
-    if ("-submit".equals(argv[0])) {
-      if (argv.length != 2)
-        displayUsage();
+    if ("-submit".equals(cmd)) {
+      if (argv.length != 2) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       submitJobFile = argv[1];
       submitJobFile = argv[1];
-    } else if ("-status".equals(argv[0])) {
-      if (argv.length != 2)
-        displayUsage();
+    } else if ("-status".equals(cmd)) {
+      if (argv.length != 2) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       jobid = argv[1];
       jobid = argv[1];
       getStatus = true;
       getStatus = true;
-    } else if ("-kill".equals(argv[0])) {
-      if (argv.length != 2)
-        displayUsage();
+    } else if ("-kill".equals(cmd)) {
+      if (argv.length != 2) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       jobid = argv[1];
       jobid = argv[1];
       killJob = true;
       killJob = true;
-    } else if ("-events".equals(argv[0])) {
-      if (argv.length != 4)
-        displayUsage();
+    } else if ("-events".equals(cmd)) {
+      if (argv.length != 4) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       jobid = argv[1];
       jobid = argv[1];
       fromEvent = Integer.parseInt(argv[2]);
       fromEvent = Integer.parseInt(argv[2]);
       nEvents = Integer.parseInt(argv[3]);
       nEvents = Integer.parseInt(argv[3]);
       listEvents = true;
       listEvents = true;
-    } else if ("-history".equals(argv[0])) {
-      if (argv.length != 2 && !(argv.length == 3 && "all".equals(argv[1])))
-         displayUsage();
+    } else if ("-history".equals(cmd)) {
+      if (argv.length != 2 && !(argv.length == 3 && "all".equals(argv[1]))) {
+         displayUsage(cmd);
+         return exitCode;
+      }
       viewHistory = true;
       viewHistory = true;
       if (argv.length == 3 && "all".equals(argv[1])) {
       if (argv.length == 3 && "all".equals(argv[1])) {
          viewAllHistory = true;
          viewAllHistory = true;
@@ -1266,26 +1292,33 @@ public class JobClient extends Configured implements MRConstants, Tool  {
       } else {
       } else {
          outputDir = argv[1];
          outputDir = argv[1];
       }
       }
-    } else if ("-list".equals(argv[0])) {
-      if (argv.length != 1 && !(argv.length == 2 && "all".equals(argv[1])))
-        displayUsage();
+    } else if ("-list".equals(cmd)) {
+      if (argv.length != 1 && !(argv.length == 2 && "all".equals(argv[1]))) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       if (argv.length == 2 && "all".equals(argv[1])) {
       if (argv.length == 2 && "all".equals(argv[1])) {
         listAllJobs = true;
         listAllJobs = true;
       } else {
       } else {
         listJobs = true;
         listJobs = true;
       }
       }
-    } else if("-kill-task".equals(argv[0])) {
-      if(argv.length != 2)
-        displayUsage();
+    } else if("-kill-task".equals(cmd)) {
+      if(argv.length != 2) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       killTask = true;
       killTask = true;
       taskid = argv[1];
       taskid = argv[1];
-    } else if("-fail-task".equals(argv[0])) {
-      if(argv.length != 2)
-        displayUsage();
+    } else if("-fail-task".equals(cmd)) {
+      if(argv.length != 2) {
+        displayUsage(cmd);
+        return exitCode;
+      }
       failTask = true;
       failTask = true;
       taskid = argv[1];
       taskid = argv[1];
     } else {
     } else {
-      displayUsage();
+      displayUsage(cmd);
+      return exitCode;
     }
     }
 
 
     // initialize JobClient
     // initialize JobClient
@@ -1298,7 +1331,6 @@ public class JobClient extends Configured implements MRConstants, Tool  {
     init(conf);
     init(conf);
         
         
     // Submit the request
     // Submit the request
-    int exitCode = -1;
     try {
     try {
       if (submitJobFile != null) {
       if (submitJobFile != null) {
         RunningJob job = submitJob(conf);
         RunningJob job = submitJob(conf);

+ 3 - 2
src/java/org/apache/hadoop/util/GenericOptionsParser.java

@@ -123,10 +123,11 @@ public class GenericOptionsParser {
   /**
   /**
    * Returns an array of Strings containing only application-specific arguments.
    * Returns an array of Strings containing only application-specific arguments.
    * 
    * 
-   * @return array of <code>String</code>s containing the un-parsed arguments.
+   * @return array of <code>String</code>s containing the un-parsed arguments
+   * or <strong>empty array</strong> if commandLine was not defined.
    */
    */
   public String[] getRemainingArgs() {
   public String[] getRemainingArgs() {
-    return commandLine.getArgs();
+    return (commandLine == null) ? new String[]{} : commandLine.getArgs();
   }
   }
 
 
   /**
   /**

+ 8 - 0
src/test/org/apache/hadoop/util/TestGenericsUtil.java

@@ -23,6 +23,8 @@ import java.util.List;
 
 
 import junit.framework.TestCase;
 import junit.framework.TestCase;
 
 
+import org.apache.hadoop.conf.Configuration;
+
 public class TestGenericsUtil extends TestCase {
 public class TestGenericsUtil extends TestCase {
 
 
   public void testToArray() {
   public void testToArray() {
@@ -97,6 +99,12 @@ public class TestGenericsUtil extends TestCase {
     
     
   }
   }
   
   
+  public void testGenericOptionsParser() throws Exception {
+     GenericOptionsParser parser = new GenericOptionsParser(
+        new Configuration(), new String[] {"-jt"});
+    assertEquals(parser.getRemainingArgs().length, 0);
+  }
+  
   public void testGetClass() {
   public void testGetClass() {
     
     
     //test with Integer
     //test with Integer