소스 검색

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 년 전
부모
커밋
c737dfb563
4개의 변경된 파일47개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 0
      CHANGES.txt
  2. 21 0
      src/core/org/apache/hadoop/util/Shell.java
  3. 2 2
      src/mapred/org/apache/hadoop/mapred/TaskRunner.java
  4. 21 0
      src/test/org/apache/hadoop/util/TestShell.java

+ 3 - 0
CHANGES.txt

@@ -175,6 +175,9 @@ Trunk (unreleased changes)
     HADOOP-3780. Remove asynchronous resolution of network topology in the 
     HADOOP-3780. Remove asynchronous resolution of network topology in the 
     JobTracker (Amar Kamat via omalley)
     JobTracker (Amar Kamat via omalley)
 
 
+    HADOOP-3852. Add ShellCommandExecutor.toString method to make nicer
+    error messages. (Steve Loughran via omalley)
+
   OPTIMIZATIONS
   OPTIMIZATIONS
 
 
     HADOOP-3556. Removed lock contention in MD5Hash by changing the 
     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() {
     public String getOutput() {
       return (output == null) ? "" : output.toString();
       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,
                         Map<String, String> env,
                         TaskAttemptID taskid) throws IOException {
                         TaskAttemptID taskid) throws IOException {
 
 
+    shexec = new ShellCommandExecutor(args.toArray(new String[0]), dir, env);
     try {
     try {
-      shexec = new ShellCommandExecutor(args.toArray(new String[0]), dir, env);
       shexec.execute();
       shexec.execute();
     } catch (IOException ioe) {
     } catch (IOException ioe) {
       // do nothing
       // do nothing
@@ -461,7 +461,7 @@ abstract class TaskRunner extends Thread {
           tracker.getTaskTrackerInstrumentation().taskFailedPing(t.getTaskID());
           tracker.getTaskTrackerInstrumentation().taskFailedPing(t.getTaskID());
         }
         }
         throw new IOException("Task process exit with nonzero status of " +
         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
     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 {
   private void testInterval(long interval) throws IOException {
     Command command = new Command(interval);
     Command command = new Command(interval);