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
This commit is contained in:
Vinod Kumar Vavilapalli 2011-10-27 15:52:43 +00:00
parent 28676b5345
commit 1f42531bce
2 changed files with 160 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}
}
}