Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,6 @@ private static class DeprecatedKeyInfo {
this.customMessage = customMessage;
}

private final String getWarningMessage(String key) {
return getWarningMessage(key, null);
}

/**
* Method to provide the warning message. It gives the custom message if
* non-null, and default message otherwise.
Expand Down Expand Up @@ -412,12 +408,9 @@ private String getWarningMessage(String key, String source) {
return warningMessage;
}

boolean getAndSetAccessed() {
return accessed.getAndSet(true);
}

public void clearAccessed() {
accessed.set(false);
void logDeprecation(String name, String source) {
LOG_DEPRECATION.info(getWarningMessage(name, source));
this.accessed.set(true);
}
}

Expand Down Expand Up @@ -728,12 +721,10 @@ private String[] handleDeprecation(DeprecationContext deprecations,
}
// Initialize the return value with requested name
String[] names = new String[]{name};
// Deprecated keys are logged once and an updated names are returned
// Deprecated keys are logged and updated names are returned
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
if (keyInfo != null) {
if (!keyInfo.getAndSetAccessed()) {
logDeprecation(keyInfo.getWarningMessage(name));
}
keyInfo.logDeprecation(name, null);
// Override return value for deprecated keys
names = keyInfo.newKeys;
}
Expand Down Expand Up @@ -1462,13 +1453,6 @@ void logDeprecation(String message) {
LOG_DEPRECATION.info(message);
}

void logDeprecationOnce(String name, String source) {
DeprecatedKeyInfo keyInfo = getDeprecatedKeyInfo(name);
if (keyInfo != null && !keyInfo.getAndSetAccessed()) {
LOG_DEPRECATION.info(keyInfo.getWarningMessage(name, source));
}
}

/**
* Unset a previously set property.
* @param name the property name
Expand Down Expand Up @@ -2448,7 +2432,10 @@ private CredentialEntry getCredentialEntry(CredentialProvider provider,
if (oldName != null) {
entry = provider.getCredentialEntry(oldName);
if (entry != null) {
logDeprecationOnce(oldName, provider.toString());
DeprecatedKeyInfo ki = getDeprecatedKeyInfo(oldName);
if (ki != null) {
ki.logDeprecation(oldName, provider.toString());
}
return entry;
}
}
Expand All @@ -2459,7 +2446,7 @@ private CredentialEntry getCredentialEntry(CredentialProvider provider,
for (String newName : keyInfo.newKeys) {
entry = provider.getCredentialEntry(newName);
if (entry != null) {
logDeprecationOnce(name, null);
keyInfo.logDeprecation(name, null);
return entry;
}
}
Expand Down Expand Up @@ -3433,8 +3420,7 @@ void handleEndProperty() {
deprecations.getDeprecatedKeyMap().get(confName);

if (keyInfo != null) {
logDeprecation(keyInfo.getWarningMessage(confName, wrapper.toString()));
keyInfo.clearAccessed();
keyInfo.logDeprecation(confName, wrapper.toString());
for (String key : keyInfo.newKeys) {
// update new keys with deprecated key's value
results.add(new ParsedItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,36 @@ public void testDeprecatedPropertyInXMLFileGeneratesLogMessage(@TempDir java.nio
assertTrue(hasDeprecationMessage);
}

@Test
public void testDeprecatedPropertyLogsWarningOnEveryUse(){
String oldProp = "test.deprecation.old.conf.b";
String newProp = "test.deprecation.new.conf.b";
Configuration.addDeprecation(oldProp, newProp);

TestAppender appender = new TestAppender();
Logger deprecationLogger = Logger.getLogger("org.apache.hadoop.conf.Configuration.deprecation");
deprecationLogger.addAppender(appender);

try {
conf.set(oldProp, "b1");
conf.get(oldProp);
conf.set(oldProp, "b2");
conf.get(oldProp);
// Using the new property should not log a warning
conf.set(newProp, "b3");
conf.get(newProp);
conf.set(newProp, "b4");
conf.get(newProp);
} finally {
deprecationLogger.removeAppender(appender);
}

Pattern deprecationMsgPattern = Pattern.compile(oldProp + " is deprecated");
long count = appender.log.stream().map(LoggingEvent::getRenderedMessage)
.filter(msg -> deprecationMsgPattern.matcher(msg).find()).count();
assertEquals(4, count, "Expected exactly four warnings for deprecated property usage");
}

/**
* A simple appender for white box testing.
*/
Expand Down