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 5d78f9d4c43..f262136fd7b 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 @@ -414,15 +414,12 @@ 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"); - writeLocalWrapperScript(launchDst, pidFile, pout); - } finally { - IOUtils.cleanupWithLogger(LOG, pout, out); + try (DataOutputStream out = + lfs.create(wrapperScriptPath, EnumSet.of(CREATE, OVERWRITE))) { + try (PrintStream pout = + new PrintStream(out, false,"UTF-8")) { + writeLocalWrapperScript(launchDst, pidFile, pout); + } } } @@ -489,22 +486,20 @@ 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"); - // 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 - pout.println("#!/bin/bash"); - pout.println(); - pout.println("echo $$ > " + pidFile.toString() + ".tmp"); - 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); + try (DataOutputStream out = + lfs.create(sessionScriptPath, EnumSet.of(CREATE, OVERWRITE))) { + try (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 + pout.println("#!/bin/bash"); + pout.println(); + pout.println("echo $$ > " + pidFile.toString() + ".tmp"); + 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()); + } } 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 f1c826e00f6..149292a04df 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 @@ -220,15 +220,13 @@ 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 = deriveContainerWorkDir(); recordContainerWorkDir(containerID, containerWorkDir.toString()); String pidFileSubpath = getPidFileSubpath(appIdStr, containerIdStr); - // pid file should be in nm private dir so that it is not + // pid file should be in nm private dir so that it is not // accessible by users pidFilePath = dirsHandler.getLocalPathForWrite(pidFileSubpath); List localDirs = dirsHandler.getLocalDirs(); @@ -243,8 +241,10 @@ public Integer call() { throw new IOException("Most of the disks failed. " + dirsHandler.getDisksHealthReport(false)); } - try { - // /////////// Write out the container-script in the nmPrivate space. + // /////////// Write out the container-script in the nmPrivate space. + try (DataOutputStream containerScriptOutStream = + lfs.create(nmPrivateContainerScriptPath, + EnumSet.of(CREATE, OVERWRITE))) { List appDirs = new ArrayList(localDirs.size()); for (String localDir : localDirs) { Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE); @@ -252,9 +252,6 @@ public Integer call() { 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( @@ -271,18 +268,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) diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/JavaSandboxLinuxContainerRuntime.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/JavaSandboxLinuxContainerRuntime.java index 245b38faaf5..1ab1fc58110 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/JavaSandboxLinuxContainerRuntime.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/JavaSandboxLinuxContainerRuntime.java @@ -231,7 +231,6 @@ public void prepareContainer(ContainerRuntimeContext ctx) throw new ContainerExecutionException("hadoop.tmp.dir not set!"); } - OutputStream policyOutputStream = null; try { String containerID = ctx.getExecutionAttribute(CONTAINER_ID_STR); initializePolicyDir(); @@ -242,19 +241,19 @@ public void prepareContainer(ContainerRuntimeContext ctx) Paths.get(policyFileDir.toString(), containerID + "-" + NMContainerPolicyUtils.POLICY_FILE), POLICY_ATTR); - policyOutputStream = Files.newOutputStream(policyFilePath); - containerPolicies.put(containerID, policyFilePath); + try(OutputStream policyOutputStream = + Files.newOutputStream(policyFilePath)) { - NMContainerPolicyUtils.generatePolicyFile(policyOutputStream, - localDirs, groupPolicyFiles, resources, configuration); - NMContainerPolicyUtils.appendSecurityFlags( - commands, env, policyFilePath, sandboxMode); + containerPolicies.put(containerID, policyFilePath); + NMContainerPolicyUtils.generatePolicyFile(policyOutputStream, + localDirs, groupPolicyFiles, resources, configuration); + NMContainerPolicyUtils.appendSecurityFlags( + commands, env, policyFilePath, sandboxMode); + } } catch (IOException e) { throw new ContainerExecutionException(e); - } finally { - IOUtils.cleanupWithLogger(LOG, policyOutputStream); } } }