YARN-7595. Container launching code suppresses close exceptions after writes. Contributed by Jim Brennan

This commit is contained in:
Jason Lowe 2017-12-15 15:54:56 -06:00
parent 9ae8d1a8de
commit 0f8de8cd91
2 changed files with 30 additions and 44 deletions

View File

@ -42,7 +42,6 @@ import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.UnsupportedFileSystemException; import org.apache.hadoop.fs.UnsupportedFileSystemException;
import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Shell; import org.apache.hadoop.util.Shell;
import org.apache.hadoop.util.Shell.CommandExecutor; import org.apache.hadoop.util.Shell.CommandExecutor;
import org.apache.hadoop.util.Shell.ExitCodeException; import org.apache.hadoop.util.Shell.ExitCodeException;
@ -414,15 +413,11 @@ public class DefaultContainerExecutor extends ContainerExecutor {
*/ */
public void writeLocalWrapperScript(Path launchDst, Path pidFile) public void writeLocalWrapperScript(Path launchDst, Path pidFile)
throws IOException { throws IOException {
DataOutputStream out = null; try (DataOutputStream out =
PrintStream pout = null; lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE));
PrintStream pout =
try { new PrintStream(out, false, "UTF-8")) {
out = lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE));
pout = new PrintStream(out, false, "UTF-8");
writeLocalWrapperScript(launchDst, pidFile, pout); writeLocalWrapperScript(launchDst, pidFile, pout);
} finally {
IOUtils.cleanupWithLogger(LOG, pout, out);
} }
} }
@ -489,11 +484,10 @@ public class DefaultContainerExecutor extends ContainerExecutor {
private void writeSessionScript(Path launchDst, Path pidFile) private void writeSessionScript(Path launchDst, Path pidFile)
throws IOException { throws IOException {
DataOutputStream out = null; try (DataOutputStream out =
PrintStream pout = null; lfs.create(sessionScriptPath, EnumSet.of(CREATE, OVERWRITE));
try { PrintStream pout =
out = lfs.create(sessionScriptPath, EnumSet.of(CREATE, OVERWRITE)); new PrintStream(out, false, "UTF-8")) {
pout = new PrintStream(out, false, "UTF-8");
// We need to do a move as writing to a file is not atomic // We need to do a move as writing to a file is not atomic
// Process reading a file being written to may get garbled data // Process reading a file being written to may get garbled data
// hence write pid to tmp file first followed by a mv // hence write pid to tmp file first followed by a mv
@ -503,8 +497,6 @@ public class DefaultContainerExecutor extends ContainerExecutor {
pout.println("/bin/mv -f " + pidFile.toString() + ".tmp " + pidFile); pout.println("/bin/mv -f " + pidFile.toString() + ".tmp " + pidFile);
String exec = Shell.isSetsidAvailable? "exec setsid" : "exec"; String exec = Shell.isSetsidAvailable? "exec setsid" : "exec";
pout.printf("%s /bin/bash \"%s\"", exec, launchDst.toUri().getPath()); pout.printf("%s /bin/bash \"%s\"", exec, launchDst.toUri().getPath());
} finally {
IOUtils.cleanupWithLogger(LOG, pout, out);
} }
lfs.setPermission(sessionScriptPath, lfs.setPermission(sessionScriptPath,
ContainerExecutor.TASK_LAUNCH_SCRIPT_PERMISSION); ContainerExecutor.TASK_LAUNCH_SCRIPT_PERMISSION);

View File

@ -219,8 +219,6 @@ public class ContainerLaunch implements Callable<Integer> {
containerIdStr)); containerIdStr));
Path nmPrivateClasspathJarDir = dirsHandler.getLocalPathForWrite( Path nmPrivateClasspathJarDir = dirsHandler.getLocalPathForWrite(
getContainerPrivateDir(appIdStr, containerIdStr)); getContainerPrivateDir(appIdStr, containerIdStr));
DataOutputStream containerScriptOutStream = null;
DataOutputStream tokensOutStream = null;
// Select the working directory for the container // Select the working directory for the container
Path containerWorkDir = Path containerWorkDir =
@ -249,25 +247,24 @@ public class ContainerLaunch implements Callable<Integer> {
+ dirsHandler.getDisksHealthReport(false)); + dirsHandler.getDisksHealthReport(false));
} }
try { List<Path> appDirs = new ArrayList<Path>(localDirs.size());
// /////////// Write out the container-script in the nmPrivate space.
List<Path> appDirs = new ArrayList<Path>(localDirs.size());
for (String localDir : localDirs) { for (String localDir : localDirs) {
Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE); Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE);
Path userdir = new Path(usersdir, user); Path userdir = new Path(usersdir, user);
Path appsdir = new Path(userdir, ContainerLocalizer.APPCACHE); Path appsdir = new Path(userdir, ContainerLocalizer.APPCACHE);
appDirs.add(new Path(appsdir, appIdStr)); appDirs.add(new Path(appsdir, appIdStr));
} }
containerScriptOutStream = // Set the token location too.
lfs.create(nmPrivateContainerScriptPath, environment.put(
EnumSet.of(CREATE, OVERWRITE)); ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME,
new Path(containerWorkDir,
FINAL_CONTAINER_TOKENS_FILE).toUri().getPath());
// Set the token location too. // /////////// Write out the container-script in the nmPrivate space.
environment.put( try (DataOutputStream containerScriptOutStream =
ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME, lfs.create(nmPrivateContainerScriptPath,
new Path(containerWorkDir, EnumSet.of(CREATE, OVERWRITE))) {
FINAL_CONTAINER_TOKENS_FILE).toUri().getPath());
// Sanitize the container's environment // Sanitize the container's environment
sanitizeEnv(environment, containerWorkDir, appDirs, userLocalDirs, sanitizeEnv(environment, containerWorkDir, appDirs, userLocalDirs,
containerLogDirs, containerLogDirs,
@ -277,19 +274,16 @@ public class ContainerLaunch implements Callable<Integer> {
exec.writeLaunchEnv(containerScriptOutStream, environment, exec.writeLaunchEnv(containerScriptOutStream, environment,
localResources, launchContext.getCommands(), localResources, launchContext.getCommands(),
new Path(containerLogDirs.get(0)), user); new Path(containerLogDirs.get(0)), user);
}
// /////////// End of writing out container-script
// /////////// End of writing out container-script // /////////// Write out the container-tokens in the nmPrivate space.
try (DataOutputStream tokensOutStream =
// /////////// Write out the container-tokens in the nmPrivate space. lfs.create(nmPrivateTokensPath, EnumSet.of(CREATE, OVERWRITE))) {
tokensOutStream =
lfs.create(nmPrivateTokensPath, EnumSet.of(CREATE, OVERWRITE));
Credentials creds = container.getCredentials(); Credentials creds = container.getCredentials();
creds.writeTokenStorageToStream(tokensOutStream); creds.writeTokenStorageToStream(tokensOutStream);
// /////////// End of writing out container-tokens
} finally {
IOUtils.cleanupWithLogger(LOG, containerScriptOutStream,
tokensOutStream);
} }
// /////////// End of writing out container-tokens
ret = launchContainer(new ContainerStartContext.Builder() ret = launchContainer(new ContainerStartContext.Builder()
.setContainer(container) .setContainer(container)