Skip to content

Commit d52b0f9

Browse files
zabetakcnauroth
authored andcommitted
HADOOP-8865. Log warning message when loading deprecated properties
The goal is to emit the warning message when deprecated properties are loaded from XML configuration files to inform users that obsolete properties are in use. Before this patch we only inform users when a set/get operation is performed on a deprecated property but this is not sufficient cause if the users/apps are using the new key to obtain the value they will never see the warning. As a result when the property is finally removed or stops taking effect the application will fail or showcase different behavior. With this patch deprecated properties may be logged twice: * when a deprecated prop is loaded from a resource * when a deprecated prop is accessed via get/set; if the deprecated key is not accessed no additional log is generated Closes #7737 Signed-off-by: Chris Nauroth <[email protected]>
1 parent c8ea305 commit d52b0f9

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3433,6 +3433,7 @@ void handleEndProperty() {
34333433
deprecations.getDeprecatedKeyMap().get(confName);
34343434

34353435
if (keyInfo != null) {
3436+
logDeprecation(keyInfo.getWarningMessage(confName, wrapper.toString()));
34363437
keyInfo.clearAccessed();
34373438
for (String key : keyInfo.newKeys) {
34383439
// update new keys with deprecated key's value

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.apache.log4j.AppenderSkeleton;
8888
import org.apache.log4j.Logger;
8989
import org.apache.log4j.spi.LoggingEvent;
90+
import org.junit.jupiter.api.io.TempDir;
9091
import org.mockito.Mockito;
9192

9293
public class TestConfiguration {
@@ -120,7 +121,7 @@ public class TestConfiguration {
120121

121122
@BeforeEach
122123
public void setUp() throws Exception {
123-
conf = new Configuration();
124+
conf = new Configuration(false);
124125
}
125126

126127
@AfterEach
@@ -356,6 +357,33 @@ public void testFinalWarningsMultipleOverride() throws Exception {
356357
}
357358
}
358359

360+
@Test
361+
public void testDeprecatedPropertyInXMLFileGeneratesLogMessage(@TempDir java.nio.file.Path tmp) throws IOException {
362+
String oldProp = "test.deprecation.old.conf.a";
363+
String newProp = "test.deprecation.new.conf.a";
364+
Configuration.addDeprecation(oldProp, newProp);
365+
java.nio.file.Path confFile = Files.createFile(tmp.resolve("TestConfiguration.xml"));
366+
String confXml = "<configuration><property><name>" + oldProp + "</name><value>a</value></property></configuration>";
367+
Files.write(confFile, confXml.getBytes());
368+
369+
TestAppender appender = new TestAppender();
370+
Logger deprecationLogger = Logger.getLogger("org.apache.hadoop.conf.Configuration.deprecation");
371+
deprecationLogger.addAppender(appender);
372+
373+
try {
374+
conf.addResource(new Path(confFile.toUri()));
375+
// Properties are lazily initialized so access them to trigger the loading of the resource
376+
conf.getProps();
377+
} finally {
378+
deprecationLogger.removeAppender(appender);
379+
}
380+
381+
Pattern deprecationMsgPattern = Pattern.compile(oldProp + " in file:" + confFile + " is deprecated");
382+
boolean hasDeprecationMessage = appender.log.stream().map(LoggingEvent::getRenderedMessage)
383+
.anyMatch(msg -> deprecationMsgPattern.matcher(msg).find());
384+
assertTrue(hasDeprecationMessage);
385+
}
386+
359387
/**
360388
* A simple appender for white box testing.
361389
*/
@@ -845,8 +873,7 @@ public void testToString() throws IOException {
845873
conf.addResource(fileResource);
846874

847875
String expectedOutput =
848-
"Configuration: core-default.xml, core-site.xml, " +
849-
fileResource.toString();
876+
"Configuration: " + fileResource;
850877
assertEquals(expectedOutput, conf.toString());
851878
}
852879

0 commit comments

Comments
 (0)