Skip to content

Commit 47ea0d7

Browse files
authored
HADOOP-18021. Provide a public wrapper of Configuration#substituteVars (#3710)
Contributed by Andras Gyori
1 parent dd6b987 commit 47ea0d7

File tree

3 files changed

+31
-2
lines changed
  • hadoop-common-project/hadoop-common/src
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity

3 files changed

+31
-2
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/Configuration.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,20 @@ private static int[] findSubVariable(String eval) {
10991099
return result;
11001100
}
11011101

1102+
/**
1103+
* Provides a public wrapper over substituteVars in order to avoid compatibility issues.
1104+
* See HADOOP-18021 for further details.
1105+
*
1106+
* @param expr the literal value of a config key
1107+
* @return null if expr is null, otherwise the value resulting from expanding
1108+
* expr using the algorithm above.
1109+
* @throws IllegalArgumentException when more than
1110+
* {@link Configuration#MAX_SUBST} replacements are required
1111+
*/
1112+
public String substituteCommonVariables(String expr) {
1113+
return substituteVars(expr);
1114+
}
1115+
11021116
/**
11031117
* Attempts to repeatedly expand the value {@code expr} by replacing the
11041118
* left-most substring of the form "${var}" in the following precedence order
@@ -1120,13 +1134,17 @@ private static int[] findSubVariable(String eval) {
11201134
* If a cycle is detected then the original expr is returned. Loops
11211135
* involving multiple substitutions are not detected.
11221136
*
1137+
* In order not to introduce breaking changes (as Oozie for example contains a method with the
1138+
* same name and same signature) do not make this method public, use substituteCommonVariables
1139+
* in this case.
1140+
*
11231141
* @param expr the literal value of a config key
11241142
* @return null if expr is null, otherwise the value resulting from expanding
11251143
* expr using the algorithm above.
11261144
* @throws IllegalArgumentException when more than
11271145
* {@link Configuration#MAX_SUBST} replacements are required
11281146
*/
1129-
public String substituteVars(String expr) {
1147+
private String substituteVars(String expr) {
11301148
if (expr == null) {
11311149
return null;
11321150
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ public void testVariableSubstitution() throws IOException {
444444
assertTrue(mock.getInt("my.int", -1) == 42);
445445
}
446446

447+
/**
448+
* Checks if variable substitution is accessible via a public API.
449+
*/
450+
@Test
451+
public void testCommonVariableSubstitution() {
452+
conf.set("intvar", String.valueOf(42));
453+
String intVar = conf.substituteCommonVariables("${intvar}");
454+
455+
assertEquals("42", intVar);
456+
}
457+
447458
@Test
448459
public void testEnvDefault() throws IOException {
449460
Configuration mock = Mockito.spy(conf);

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/UserWeights.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static UserWeights createByConfig(
5454
if (m.find()) {
5555
String userName = item.getKey().replaceFirst("\\." + USER_WEIGHT, "");
5656
if (!userName.isEmpty()) {
57-
String value = conf.substituteVars(item.getValue());
57+
String value = conf.substituteCommonVariables(item.getValue());
5858
userWeights.data.put(userName, new Float(value));
5959
}
6060
}

0 commit comments

Comments
 (0)