279db4badc095ef9bbd1df0c8688c014d93fbfec .../apache/hadoop/yarn/util/ApplicationUtils.java | 92 ++++++++++++++++++++++ .../java/org/apache/hadoop/yarn/util/Apps.java | 50 +----------- 2 files changed, 93 insertions(+), 49 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ApplicationUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ApplicationUtils.java new file mode 100644 index 0000000..e90bf81 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ApplicationUtils.java @@ -0,0 +1,92 @@ +/** +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package org.apache.hadoop.yarn.util; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.hadoop.classification.InterfaceAudience.Public; +import org.apache.hadoop.util.Shell; +import org.apache.hadoop.util.StringInterner; + +/** + * Yarn application-related utilities for setting the Environment. + */ +@Public +public class ApplicationUtils { + + /** + * Update the given Environment with the variables from the input string. + * + * @param env Environment to be updated with the variables + * @param envString The list of variables separated with @classPathSeparator + * @param classPathSeparator The seperator in the environment. + */ + public static void setEnvFromInputString(Map env, + String envString, String classPathSeparator) { + if (envString != null && envString.length() > 0) { + String childEnvs[] = envString.split(","); + Pattern p = Pattern.compile(Shell.getEnvironmentVariableRegex()); + for (String cEnv : childEnvs) { + String[] parts = cEnv.split("="); // split on '=' + Matcher m = p.matcher(parts[1]); + StringBuffer sb = new StringBuffer(); + while (m.find()) { + String var = m.group(1); + // replace $env with the child's env constructed by tt's + String replace = env.get(var); + // if this key is not configured by the tt for the child .. get it + // from the tt's env + if (replace == null) + replace = System.getenv(var); + // the env key is note present anywhere .. simply set it + if (replace == null) + replace = ""; + m.appendReplacement(sb, Matcher.quoteReplacement(replace)); + } + m.appendTail(sb); + addToEnvironment(env, parts[0], sb.toString(), classPathSeparator); + } + } + } + + + /** + * Update the given Environment with the variable name and value * + * + * @param environment Environment to be updated with the variables + * @param variable The name of the environment variable + * @param value Value of the variable to be update din environment + * @param classPathSeparator The separator in the environment. + */ + + public static void addToEnvironment( + Map environment, + String variable, String value, String classPathSeparator) { + String val = environment.get(variable); + if (val == null) { + val = value; + } else { + val = val + classPathSeparator + value; + } + environment.put(StringInterner.weakIntern(variable), + StringInterner.weakIntern(val)); + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/Apps.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/Apps.java index cf3940f..f834633 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/Apps.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/Apps.java @@ -25,14 +25,8 @@ import java.io.File; import java.util.Iterator; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.apache.hadoop.classification.InterfaceAudience.Private; -import org.apache.hadoop.classification.InterfaceAudience.Public; -import org.apache.hadoop.classification.InterfaceStability.Unstable; -import org.apache.hadoop.util.Shell; -import org.apache.hadoop.util.StringInterner; import org.apache.hadoop.yarn.api.ApplicationConstants; import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; @@ -41,7 +35,7 @@ * Yarn internal application-related utilities */ @Private -public class Apps { +public class Apps extends ApplicationUtils { public static final String APP = "application"; public static final String ID = "ID"; @@ -70,33 +64,6 @@ public static void throwParseException(String name, String s) { throw new YarnRuntimeException(join("Error parsing ", name, ": ", s)); } - public static void setEnvFromInputString(Map env, - String envString, String classPathSeparator) { - if (envString != null && envString.length() > 0) { - String childEnvs[] = envString.split(","); - Pattern p = Pattern.compile(Shell.getEnvironmentVariableRegex()); - for (String cEnv : childEnvs) { - String[] parts = cEnv.split("="); // split on '=' - Matcher m = p.matcher(parts[1]); - StringBuffer sb = new StringBuffer(); - while (m.find()) { - String var = m.group(1); - // replace $env with the child's env constructed by tt's - String replace = env.get(var); - // if this key is not configured by the tt for the child .. get it - // from the tt's env - if (replace == null) - replace = System.getenv(var); - // the env key is note present anywhere .. simply set it - if (replace == null) - replace = ""; - m.appendReplacement(sb, Matcher.quoteReplacement(replace)); - } - m.appendTail(sb); - addToEnvironment(env, parts[0], sb.toString(), classPathSeparator); - } - } - } /** * This older version of this method is kept around for compatibility @@ -109,21 +76,6 @@ public static void setEnvFromInputString(Map env, setEnvFromInputString(env, envString, File.pathSeparator); } - @Public - @Unstable - public static void addToEnvironment( - Map environment, - String variable, String value, String classPathSeparator) { - String val = environment.get(variable); - if (val == null) { - val = value; - } else { - val = val + classPathSeparator + value; - } - environment.put(StringInterner.weakIntern(variable), - StringInterner.weakIntern(val)); - } - /** * This older version of this method is kept around for compatibility * because downstream frameworks like Spark and Tez have been using it.