Browse Source

MAPREDUCE-3240. Fixed NodeManager to be able to forcefully cleanup its containers (process-trees) irrespective of whether the container succeeded, or killed. Contributed by Hitesh Shah.
Added the new files which I missed earlier.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.23@1189815 13f79535-47bb-0310-9956-ffa450edef68

Vinod Kumar Vavilapalli 13 years ago
parent
commit
1f42531bce

+ 74 - 0
hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/util/ProcessIdFileReader.java

@@ -0,0 +1,74 @@
+package org.apache.hadoop.yarn.server.nodemanager.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.fs.Path;
+
+/**
+ * Helper functionality to read the pid from a file.
+ */
+public class ProcessIdFileReader {
+
+  private static final Log LOG = LogFactory.getLog(ProcessIdFileReader.class);
+  
+  /**
+   * Get the process id from specified file path.
+   * Parses each line to find a valid number
+   * and returns the first one found.
+   * @return Process Id if obtained from path specified else null
+   * @throws IOException
+   */
+  public static String getProcessId(Path path) throws IOException {
+    if (path == null) {
+      throw new IOException("Trying to access process id from a null path");
+    }
+    
+    LOG.debug("Accessing pid from pid file " + path);
+    String processId = null;
+    FileReader fileReader = null;
+    BufferedReader bufReader = null;
+
+    try {
+      File file = new File(path.toString());
+      if (file.exists()) {
+        fileReader = new FileReader(file);
+        bufReader = new BufferedReader(fileReader);
+        while (true) {
+          String line = bufReader.readLine();
+          if (line == null) {
+            break;
+          }
+          String temp = line.trim(); 
+          if (!temp.isEmpty()) {
+            try {
+              Long pid = Long.valueOf(temp);
+              if (pid > 0) {
+                processId = temp;
+                break;
+              }
+            } catch (Exception e) {
+              // do nothing
+            }
+          }
+        }
+      }
+    } finally {
+      if (fileReader != null) {
+        fileReader.close();
+      }
+      if (bufReader != null) {
+        bufReader.close();
+      }
+    }
+    LOG.debug("Got pid " 
+        + (processId != null? processId : "null")  
+        + " from path " + path);
+    return processId;
+  }
+
+}

+ 86 - 0
hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/util/TestProcessIdFileReader.java

@@ -0,0 +1,86 @@
+package org.apache.hadoop.yarn.server.nodemanager.util;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import junit.framework.Assert;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.yarn.server.nodemanager.util.ProcessIdFileReader;
+import org.junit.Test;
+
+public class TestProcessIdFileReader {
+
+  
+  @Test
+  public void testNullPath() {
+    String pid = null;
+    try {
+      pid = ProcessIdFileReader.getProcessId(null);
+      fail("Expected an error to be thrown for null path");
+    } catch (Exception e) {
+      // expected
+    }
+    assert(pid == null);
+  }
+  
+  @Test 
+  public void testSimpleGet() throws IOException {
+    String rootDir = new File(System.getProperty(
+        "test.build.data", "/tmp")).getAbsolutePath();
+    File testFile = null;
+    
+    try {
+      testFile = new File(rootDir, "temp.txt");
+      PrintWriter fileWriter = new PrintWriter(testFile);
+      fileWriter.println("56789");
+      fileWriter.close();      
+      String processId = null; 
+                  
+      processId = ProcessIdFileReader.getProcessId(
+          new Path(rootDir + Path.SEPARATOR + "temp.txt"));
+      Assert.assertEquals("56789", processId);      
+      
+    } finally {
+      if (testFile != null
+          && testFile.exists()) {
+        testFile.delete();
+      }
+    }
+  }
+
+    
+  @Test
+  public void testComplexGet() throws IOException {
+    String rootDir = new File(System.getProperty(
+        "test.build.data", "/tmp")).getAbsolutePath();
+    File testFile = null;
+    
+    try {
+      testFile = new File(rootDir, "temp.txt");
+      PrintWriter fileWriter = new PrintWriter(testFile);
+      fileWriter.println("   ");
+      fileWriter.println("");
+      fileWriter.println("abc");
+      fileWriter.println("-123");
+      fileWriter.println("-123 ");
+      fileWriter.println(" 23 ");
+      fileWriter.println("6236");
+      fileWriter.close();      
+      String processId = null; 
+                  
+      processId = ProcessIdFileReader.getProcessId(
+          new Path(rootDir + Path.SEPARATOR + "temp.txt"));
+      Assert.assertEquals("23", processId);      
+      
+    } finally {
+      if (testFile != null
+          && testFile.exists()) {
+        testFile.delete();
+      }
+    }
+  }
+}