diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DefaultContainerExecutor.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DefaultContainerExecutor.java index 2b322349267..b54b7f59664 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DefaultContainerExecutor.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/DefaultContainerExecutor.java @@ -42,7 +42,6 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.UnsupportedFileSystemException; 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.CommandExecutor; import org.apache.hadoop.util.Shell.ExitCodeException; @@ -414,15 +413,11 @@ public Path getWrapperScriptPath() { */ public void writeLocalWrapperScript(Path launchDst, Path pidFile) throws IOException { - DataOutputStream out = null; - PrintStream pout = null; - - try { - out = lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE)); - pout = new PrintStream(out, false, "UTF-8"); + try (DataOutputStream out = + lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE)); + PrintStream pout = + new PrintStream(out, false, "UTF-8")) { writeLocalWrapperScript(launchDst, pidFile, pout); - } finally { - IOUtils.cleanupWithLogger(LOG, pout, out); } } @@ -489,11 +484,10 @@ public void writeLocalWrapperScript(Path launchDst, Path pidFile, private void writeSessionScript(Path launchDst, Path pidFile) throws IOException { - DataOutputStream out = null; - PrintStream pout = null; - try { - out = lfs.create(sessionScriptPath, EnumSet.of(CREATE, OVERWRITE)); - pout = new PrintStream(out, false, "UTF-8"); + try (DataOutputStream out = + lfs.create(sessionScriptPath, EnumSet.of(CREATE, OVERWRITE)); + PrintStream pout = + new PrintStream(out, false, "UTF-8")) { // 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 // hence write pid to tmp file first followed by a mv @@ -503,8 +497,6 @@ private void writeSessionScript(Path launchDst, Path pidFile) pout.println("/bin/mv -f " + pidFile.toString() + ".tmp " + pidFile); String exec = Shell.isSetsidAvailable? "exec setsid" : "exec"; pout.printf("%s /bin/bash \"%s\"", exec, launchDst.toUri().getPath()); - } finally { - IOUtils.cleanupWithLogger(LOG, pout, out); } lfs.setPermission(sessionScriptPath, ContainerExecutor.TASK_LAUNCH_SCRIPT_PERMISSION); diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java index b3c75c2eb1d..4bda24165ef 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java @@ -219,8 +219,6 @@ public Integer call() { containerIdStr)); Path nmPrivateClasspathJarDir = dirsHandler.getLocalPathForWrite( getContainerPrivateDir(appIdStr, containerIdStr)); - DataOutputStream containerScriptOutStream = null; - DataOutputStream tokensOutStream = null; // Select the working directory for the container Path containerWorkDir = @@ -249,25 +247,24 @@ public Integer call() { + dirsHandler.getDisksHealthReport(false)); } - try { - // /////////// Write out the container-script in the nmPrivate space. - List appDirs = new ArrayList(localDirs.size()); - - for (String localDir : localDirs) { - Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE); - Path userdir = new Path(usersdir, user); - Path appsdir = new Path(userdir, ContainerLocalizer.APPCACHE); - appDirs.add(new Path(appsdir, appIdStr)); - } - containerScriptOutStream = - lfs.create(nmPrivateContainerScriptPath, - EnumSet.of(CREATE, OVERWRITE)); - - // Set the token location too. - environment.put( - ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME, - new Path(containerWorkDir, - FINAL_CONTAINER_TOKENS_FILE).toUri().getPath()); + List appDirs = new ArrayList(localDirs.size()); + + for (String localDir : localDirs) { + Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE); + Path userdir = new Path(usersdir, user); + Path appsdir = new Path(userdir, ContainerLocalizer.APPCACHE); + appDirs.add(new Path(appsdir, appIdStr)); + } + // Set the token location too. + environment.put( + ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME, + new Path(containerWorkDir, + FINAL_CONTAINER_TOKENS_FILE).toUri().getPath()); + + // /////////// Write out the container-script in the nmPrivate space. + try (DataOutputStream containerScriptOutStream = + lfs.create(nmPrivateContainerScriptPath, + EnumSet.of(CREATE, OVERWRITE))) { // Sanitize the container's environment sanitizeEnv(environment, containerWorkDir, appDirs, userLocalDirs, containerLogDirs, @@ -277,19 +274,16 @@ public Integer call() { exec.writeLaunchEnv(containerScriptOutStream, environment, localResources, launchContext.getCommands(), 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. - tokensOutStream = - lfs.create(nmPrivateTokensPath, EnumSet.of(CREATE, OVERWRITE)); + // /////////// Write out the container-tokens in the nmPrivate space. + try (DataOutputStream tokensOutStream = + lfs.create(nmPrivateTokensPath, EnumSet.of(CREATE, OVERWRITE))) { Credentials creds = container.getCredentials(); 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() .setContainer(container)