Skip to content

Commit f93aff5

Browse files
authored
HADOOP-19597. Log warning message on every set/get of a deprecated configuration property (#7766) Contributed by Stamatis Zampetakis.
* HADOOP-19597. Log warning message on every set/get of a deprecated configuration property Reviewed-by: Shilun Fan <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 950d876 commit f93aff5

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,6 @@ private static class DeprecatedKeyInfo {
379379
this.customMessage = customMessage;
380380
}
381381

382-
private final String getWarningMessage(String key) {
383-
return getWarningMessage(key, null);
384-
}
385-
386382
/**
387383
* Method to provide the warning message. It gives the custom message if
388384
* non-null, and default message otherwise.
@@ -412,12 +408,9 @@ private String getWarningMessage(String key, String source) {
412408
return warningMessage;
413409
}
414410

415-
boolean getAndSetAccessed() {
416-
return accessed.getAndSet(true);
417-
}
418-
419-
public void clearAccessed() {
420-
accessed.set(false);
411+
void logDeprecation(String name, String source) {
412+
LOG_DEPRECATION.info(getWarningMessage(name, source));
413+
this.accessed.set(true);
421414
}
422415
}
423416

@@ -728,12 +721,10 @@ private String[] handleDeprecation(DeprecationContext deprecations,
728721
}
729722
// Initialize the return value with requested name
730723
String[] names = new String[]{name};
731-
// Deprecated keys are logged once and an updated names are returned
724+
// Deprecated keys are logged and updated names are returned
732725
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
733726
if (keyInfo != null) {
734-
if (!keyInfo.getAndSetAccessed()) {
735-
logDeprecation(keyInfo.getWarningMessage(name));
736-
}
727+
keyInfo.logDeprecation(name, null);
737728
// Override return value for deprecated keys
738729
names = keyInfo.newKeys;
739730
}
@@ -1462,13 +1453,6 @@ void logDeprecation(String message) {
14621453
LOG_DEPRECATION.info(message);
14631454
}
14641455

1465-
void logDeprecationOnce(String name, String source) {
1466-
DeprecatedKeyInfo keyInfo = getDeprecatedKeyInfo(name);
1467-
if (keyInfo != null && !keyInfo.getAndSetAccessed()) {
1468-
LOG_DEPRECATION.info(keyInfo.getWarningMessage(name, source));
1469-
}
1470-
}
1471-
14721456
/**
14731457
* Unset a previously set property.
14741458
* @param name the property name
@@ -2448,7 +2432,10 @@ private CredentialEntry getCredentialEntry(CredentialProvider provider,
24482432
if (oldName != null) {
24492433
entry = provider.getCredentialEntry(oldName);
24502434
if (entry != null) {
2451-
logDeprecationOnce(oldName, provider.toString());
2435+
DeprecatedKeyInfo ki = getDeprecatedKeyInfo(oldName);
2436+
if (ki != null) {
2437+
ki.logDeprecation(oldName, provider.toString());
2438+
}
24522439
return entry;
24532440
}
24542441
}
@@ -2459,7 +2446,7 @@ private CredentialEntry getCredentialEntry(CredentialProvider provider,
24592446
for (String newName : keyInfo.newKeys) {
24602447
entry = provider.getCredentialEntry(newName);
24612448
if (entry != null) {
2462-
logDeprecationOnce(name, null);
2449+
keyInfo.logDeprecation(name, null);
24632450
return entry;
24642451
}
24652452
}
@@ -3433,8 +3420,7 @@ void handleEndProperty() {
34333420
deprecations.getDeprecatedKeyMap().get(confName);
34343421

34353422
if (keyInfo != null) {
3436-
logDeprecation(keyInfo.getWarningMessage(confName, wrapper.toString()));
3437-
keyInfo.clearAccessed();
3423+
keyInfo.logDeprecation(confName, wrapper.toString());
34383424
for (String key : keyInfo.newKeys) {
34393425
// update new keys with deprecated key's value
34403426
results.add(new ParsedItem(

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,36 @@ public void testDeprecatedPropertyInXMLFileGeneratesLogMessage(@TempDir java.nio
384384
assertTrue(hasDeprecationMessage);
385385
}
386386

387+
@Test
388+
public void testDeprecatedPropertyLogsWarningOnEveryUse(){
389+
String oldProp = "test.deprecation.old.conf.b";
390+
String newProp = "test.deprecation.new.conf.b";
391+
Configuration.addDeprecation(oldProp, newProp);
392+
393+
TestAppender appender = new TestAppender();
394+
Logger deprecationLogger = Logger.getLogger("org.apache.hadoop.conf.Configuration.deprecation");
395+
deprecationLogger.addAppender(appender);
396+
397+
try {
398+
conf.set(oldProp, "b1");
399+
conf.get(oldProp);
400+
conf.set(oldProp, "b2");
401+
conf.get(oldProp);
402+
// Using the new property should not log a warning
403+
conf.set(newProp, "b3");
404+
conf.get(newProp);
405+
conf.set(newProp, "b4");
406+
conf.get(newProp);
407+
} finally {
408+
deprecationLogger.removeAppender(appender);
409+
}
410+
411+
Pattern deprecationMsgPattern = Pattern.compile(oldProp + " is deprecated");
412+
long count = appender.log.stream().map(LoggingEvent::getRenderedMessage)
413+
.filter(msg -> deprecationMsgPattern.matcher(msg).find()).count();
414+
assertEquals(4, count, "Expected exactly four warnings for deprecated property usage");
415+
}
416+
387417
/**
388418
* A simple appender for white box testing.
389419
*/

0 commit comments

Comments
 (0)