Browse Source

HADOOP-3852. Add ShellCommandExecutor.toString method to make nicer
error messages. (Steve Loughran via omalley)


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

Owen O'Malley 17 years ago
parent
commit
c737dfb563

+ 3 - 0
CHANGES.txt

@@ -175,6 +175,9 @@ Trunk (unreleased changes)
     HADOOP-3780. Remove asynchronous resolution of network topology in the 
     JobTracker (Amar Kamat via omalley)
 
+    HADOOP-3852. Add ShellCommandExecutor.toString method to make nicer
+    error messages. (Steve Loughran via omalley)
+
   OPTIMIZATIONS
 
     HADOOP-3556. Removed lock contention in MD5Hash by changing the 

+ 21 - 0
src/core/org/apache/hadoop/util/Shell.java

@@ -303,6 +303,27 @@ abstract public class Shell {
     public String getOutput() {
       return (output == null) ? "" : output.toString();
     }
+
+    /**
+     * Returns the commands of this instance.
+     * Arguments with spaces in are presented with quotes round; other
+     * arguments are presented raw
+     *
+     * @return a string representation of the object.
+     */
+    public String toString() {
+      StringBuilder builder = new StringBuilder();
+      String[] args = getExecString();
+      for (String s : args) {
+        if (s.indexOf(' ') >= 0) {
+          builder.append('"').append(s).append('"');
+        } else {
+          builder.append(s);
+        }
+        builder.append(' ');
+      }
+      return builder.toString();
+    }
   }
   
   /** 

+ 2 - 2
src/mapred/org/apache/hadoop/mapred/TaskRunner.java

@@ -447,8 +447,8 @@ abstract class TaskRunner extends Thread {
                         Map<String, String> env,
                         TaskAttemptID taskid) throws IOException {
 
+    shexec = new ShellCommandExecutor(args.toArray(new String[0]), dir, env);
     try {
-      shexec = new ShellCommandExecutor(args.toArray(new String[0]), dir, env);
       shexec.execute();
     } catch (IOException ioe) {
       // do nothing
@@ -461,7 +461,7 @@ abstract class TaskRunner extends Thread {
           tracker.getTaskTrackerInstrumentation().taskFailedPing(t.getTaskID());
         }
         throw new IOException("Task process exit with nonzero status of " +
-                              exit_code + ".");
+                              exit_code + ": "+ shexec);
       }
     }
   }

+ 21 - 0
src/test/org/apache/hadoop/util/TestShell.java

@@ -51,6 +51,27 @@ public class TestShell extends TestCase {
     testInterval(System.currentTimeMillis() / 60000 + 60); // test a very big interval
   }
 
+  /**
+   * Assert that a string has a substring in it
+   * @param string string to search
+   * @param search what to search for it
+   */
+  private void assertInString(String string, String search) {
+    assertNotNull("Empty String", string);
+    if (!string.contains(search)) {
+      fail("Did not find \"" + search + "\" in " + string);
+    }
+  }
+
+  public void testShellCommandExecutorToString() throws Throwable {
+    Shell.ShellCommandExecutor sce=new Shell.ShellCommandExecutor(
+            new String[] { "ls","..","arg 2"});
+    String command = sce.toString();
+    assertInString(command,"ls");
+    assertInString(command, " .. ");
+    assertInString(command, "\"arg 2\"");
+  }
+
   private void testInterval(long interval) throws IOException {
     Command command = new Command(interval);