HADOOP-18021. Provide a public wrapper of Configuration#substituteVars (#3710)

Contributed by Andras Gyori
This commit is contained in:
Andras Gyori 2021-12-03 17:44:58 +01:00 committed by GitHub
parent dd6b987c93
commit 47ea0d734f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -1099,6 +1099,20 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
return result; return result;
} }
/**
* Provides a public wrapper over substituteVars in order to avoid compatibility issues.
* See HADOOP-18021 for further details.
*
* @param expr the literal value of a config key
* @return null if expr is null, otherwise the value resulting from expanding
* expr using the algorithm above.
* @throws IllegalArgumentException when more than
* {@link Configuration#MAX_SUBST} replacements are required
*/
public String substituteCommonVariables(String expr) {
return substituteVars(expr);
}
/** /**
* Attempts to repeatedly expand the value {@code expr} by replacing the * Attempts to repeatedly expand the value {@code expr} by replacing the
* left-most substring of the form "${var}" in the following precedence order * left-most substring of the form "${var}" in the following precedence order
@ -1120,13 +1134,17 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
* If a cycle is detected then the original expr is returned. Loops * If a cycle is detected then the original expr is returned. Loops
* involving multiple substitutions are not detected. * involving multiple substitutions are not detected.
* *
* In order not to introduce breaking changes (as Oozie for example contains a method with the
* same name and same signature) do not make this method public, use substituteCommonVariables
* in this case.
*
* @param expr the literal value of a config key * @param expr the literal value of a config key
* @return null if expr is null, otherwise the value resulting from expanding * @return null if expr is null, otherwise the value resulting from expanding
* expr using the algorithm above. * expr using the algorithm above.
* @throws IllegalArgumentException when more than * @throws IllegalArgumentException when more than
* {@link Configuration#MAX_SUBST} replacements are required * {@link Configuration#MAX_SUBST} replacements are required
*/ */
public String substituteVars(String expr) { private String substituteVars(String expr) {
if (expr == null) { if (expr == null) {
return null; return null;
} }

View File

@ -444,6 +444,17 @@ public class TestConfiguration {
assertTrue(mock.getInt("my.int", -1) == 42); assertTrue(mock.getInt("my.int", -1) == 42);
} }
/**
* Checks if variable substitution is accessible via a public API.
*/
@Test
public void testCommonVariableSubstitution() {
conf.set("intvar", String.valueOf(42));
String intVar = conf.substituteCommonVariables("${intvar}");
assertEquals("42", intVar);
}
@Test @Test
public void testEnvDefault() throws IOException { public void testEnvDefault() throws IOException {
Configuration mock = Mockito.spy(conf); Configuration mock = Mockito.spy(conf);

View File

@ -54,7 +54,7 @@ public final class UserWeights {
if (m.find()) { if (m.find()) {
String userName = item.getKey().replaceFirst("\\." + USER_WEIGHT, ""); String userName = item.getKey().replaceFirst("\\." + USER_WEIGHT, "");
if (!userName.isEmpty()) { if (!userName.isEmpty()) {
String value = conf.substituteVars(item.getValue()); String value = conf.substituteCommonVariables(item.getValue());
userWeights.data.put(userName, new Float(value)); userWeights.data.put(userName, new Float(value));
} }
} }