Skip to content

HADOOP-16062. Add ability to disable Configuration reload registry #6128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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 @@ -320,7 +320,7 @@ private static boolean getRestrictParserDefault(Object resource) {
/**
* Configuration objects.
*/
private static final WeakHashMap<Configuration,Object> REGISTRY =
private static WeakHashMap<Configuration, Object> REGISTRY =
new WeakHashMap<Configuration,Object>();

/**
Expand Down Expand Up @@ -828,9 +828,7 @@ public Configuration() {
public Configuration(boolean loadDefaults) {
this.loadDefaults = loadDefaults;

synchronized(Configuration.class) {
REGISTRY.put(this, null);
}
registerConfiguration(this);
}

/**
Expand Down Expand Up @@ -864,18 +862,43 @@ public Configuration(Configuration other) {
this.propertyTagsMap.putAll(other.propertyTagsMap);
}

synchronized(Configuration.class) {
REGISTRY.put(this, null);
}
registerConfiguration(this);

this.classLoader = other.classLoader;
this.loadDefaults = other.loadDefaults;
setQuietMode(other.getQuietMode());
}

/**
* Registers the configuration to enable reloading.
*/
private static void registerConfiguration(Configuration conf) {
WeakHashMap<Configuration, Object> registry = REGISTRY;
if (registry != null) {
synchronized (Configuration.class) {
registry.put(conf, null);
}
}
}

public static synchronized void setConfigurationRegistryEnabled(boolean enable) {
if (enable) {
if (REGISTRY == null) {
REGISTRY = new WeakHashMap<Configuration, Object>();
}
} else {
REGISTRY = null;
}
}

/**
* Reload existing configuration instances.
*/
public static synchronized void reloadExistingConfigurations() {
if (REGISTRY == null) {
LOG.debug("Configuration registry is disabled!");
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Reloading " + REGISTRY.keySet().size()
+ " existing configurations");
Expand All @@ -891,11 +914,14 @@ public static synchronized void reloadExistingConfigurations() {
* @param name file name. File should be present in the classpath.
*/
public static synchronized void addDefaultResource(String name) {
WeakHashMap<Configuration, Object> registry = REGISTRY;
if(!defaultResources.contains(name)) {
defaultResources.add(name);
for(Configuration conf : REGISTRY.keySet()) {
if(conf.loadDefaults) {
conf.reloadConfiguration();
if (registry != null) {
for (Configuration conf : registry.keySet()) {
if (conf.loadDefaults) {
conf.reloadConfiguration();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import static java.util.concurrent.TimeUnit.*;
Expand All @@ -65,6 +68,7 @@
import static org.junit.Assert.*;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.AtomicInitializer;
import org.apache.hadoop.conf.Configuration.IntegerRanges;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
Expand Down Expand Up @@ -2716,4 +2720,27 @@ public void testConcurrentModificationDuringIteration() throws InterruptedExcept

assertFalse("ConcurrentModificationException occurred", exceptionOccurred.get());
}

@Test
public void testReloadingCanBeDisabled() {
try {
AtomicInteger reloadCount = new AtomicInteger(0);
Configuration config = new Configuration() {
@Override
public synchronized void reloadConfiguration() {
reloadCount.incrementAndGet();
}
};
assertThat(reloadCount.get(), is(0));
Configuration.reloadExistingConfigurations();
assertThat(reloadCount.get(), is(1));
Configuration.setConfigurationRegistryEnabled(false);
assertThat(reloadCount.get(), is(1));
Configuration.reloadExistingConfigurations();
assertThat(reloadCount.get(), is(1));

} finally {
Configuration.setConfigurationRegistryEnabled(true);
}
}
}