hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cdoug...@apache.org
Subject svn commit: r653540 - in /hadoop/core/trunk: CHANGES.txt src/java/org/apache/hadoop/mapred/JobClient.java src/java/org/apache/hadoop/util/GenericOptionsParser.java src/test/org/apache/hadoop/util/TestGenericsUtil.java
Date Mon, 05 May 2008 17:58:47 GMT
Author: cdouglas
Date: Mon May  5 10:58:46 2008
New Revision: 653540

URL: http://svn.apache.org/viewvc?rev=653540&view=rev
Log:
HADOOP-3101. Prevent JobClient from throwing an exception when printing
usage. Contributed by Edward J. Yoon.


Modified:
    hadoop/core/trunk/CHANGES.txt
    hadoop/core/trunk/src/java/org/apache/hadoop/mapred/JobClient.java
    hadoop/core/trunk/src/java/org/apache/hadoop/util/GenericOptionsParser.java
    hadoop/core/trunk/src/test/org/apache/hadoop/util/TestGenericsUtil.java

Modified: hadoop/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/CHANGES.txt?rev=653540&r1=653539&r2=653540&view=diff
==============================================================================
--- hadoop/core/trunk/CHANGES.txt (original)
+++ hadoop/core/trunk/CHANGES.txt Mon May  5 10:58:46 2008
@@ -142,6 +142,9 @@
     changed Writable serialization of DatanodeInfo. This patch handles it.
     (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
 
   INCOMPATIBLE CHANGES

Modified: hadoop/core/trunk/src/java/org/apache/hadoop/mapred/JobClient.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/java/org/apache/hadoop/mapred/JobClient.java?rev=653540&r1=653539&r2=653540&view=diff
==============================================================================
--- hadoop/core/trunk/src/java/org/apache/hadoop/mapred/JobClient.java (original)
+++ hadoop/core/trunk/src/java/org/apache/hadoop/mapred/JobClient.java Mon May  5 10:58:46
2008
@@ -1199,23 +1199,42 @@
   /**
    * 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 {
+    int exitCode = -1;
+    if (argv.length < 1) {
+      displayUsage("");
+      return exitCode;
+    }    
     // process arguments
+    String cmd = argv[0];
     String submitJobFile = null;
     String jobid = null;
     String taskid = null;
@@ -1231,34 +1250,41 @@
     boolean listAllJobs = false;
     boolean killTask = 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];
-    } 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];
       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];
       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];
       fromEvent = Integer.parseInt(argv[2]);
       nEvents = Integer.parseInt(argv[3]);
       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;
       if (argv.length == 3 && "all".equals(argv[1])) {
          viewAllHistory = true;
@@ -1266,26 +1292,33 @@
       } else {
          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])) {
         listAllJobs = true;
       } else {
         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;
       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;
       taskid = argv[1];
     } else {
-      displayUsage();
+      displayUsage(cmd);
+      return exitCode;
     }
 
     // initialize JobClient
@@ -1298,7 +1331,6 @@
     init(conf);
         
     // Submit the request
-    int exitCode = -1;
     try {
       if (submitJobFile != null) {
         RunningJob job = submitJob(conf);

Modified: hadoop/core/trunk/src/java/org/apache/hadoop/util/GenericOptionsParser.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/java/org/apache/hadoop/util/GenericOptionsParser.java?rev=653540&r1=653539&r2=653540&view=diff
==============================================================================
--- hadoop/core/trunk/src/java/org/apache/hadoop/util/GenericOptionsParser.java (original)
+++ hadoop/core/trunk/src/java/org/apache/hadoop/util/GenericOptionsParser.java Mon May  5
10:58:46 2008
@@ -123,10 +123,11 @@
   /**
    * 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() {
-    return commandLine.getArgs();
+    return (commandLine == null) ? new String[]{} : commandLine.getArgs();
   }
 
   /**

Modified: hadoop/core/trunk/src/test/org/apache/hadoop/util/TestGenericsUtil.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/test/org/apache/hadoop/util/TestGenericsUtil.java?rev=653540&r1=653539&r2=653540&view=diff
==============================================================================
--- hadoop/core/trunk/src/test/org/apache/hadoop/util/TestGenericsUtil.java (original)
+++ hadoop/core/trunk/src/test/org/apache/hadoop/util/TestGenericsUtil.java Mon May  5 10:58:46
2008
@@ -23,6 +23,8 @@
 
 import junit.framework.TestCase;
 
+import org.apache.hadoop.conf.Configuration;
+
 public class TestGenericsUtil extends TestCase {
 
   public void testToArray() {
@@ -97,6 +99,12 @@
     
   }
   
+  public void testGenericOptionsParser() throws Exception {
+     GenericOptionsParser parser = new GenericOptionsParser(
+        new Configuration(), new String[] {"-jt"});
+    assertEquals(parser.getRemainingArgs().length, 0);
+  }
+  
   public void testGetClass() {
     
     //test with Integer



Mime
View raw message