-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support concurrent loading of Config for different namespaces (#29)
- Loading branch information
Showing
1 changed file
with
25 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,21 +30,30 @@ | |
* @author Jason Song([email protected]) | ||
*/ | ||
public class DefaultConfigManager implements ConfigManager { | ||
private ConfigFactoryManager m_factoryManager; | ||
private final ConfigFactoryManager m_factoryManager; | ||
|
||
private Map<String, Config> m_configs = Maps.newConcurrentMap(); | ||
private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); | ||
private final Map<String, Config> m_configs = Maps.newConcurrentMap(); | ||
private final Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap(); | ||
private final Map<String, Object> m_configLocks = Maps.newConcurrentMap(); | ||
private final Map<String, Object> m_configFileLocks = Maps.newConcurrentMap(); | ||
|
||
public DefaultConfigManager() { | ||
m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class); | ||
} | ||
|
||
@Override | ||
public Config getConfig(String namespace) { | ||
String lockKey = buildLockKey(namespace); | ||
Object lock = m_configLocks.get(lockKey); | ||
if (lock == null) { | ||
synchronized (lockKey) { | ||
lock = m_configLocks.computeIfAbsent(lockKey, k -> new Object()); | ||
} | ||
} | ||
Config config = m_configs.get(namespace); | ||
|
||
if (config == null) { | ||
synchronized (this) { | ||
synchronized (lock) { | ||
config = m_configs.get(namespace); | ||
|
||
if (config == null) { | ||
|
@@ -62,10 +71,17 @@ public Config getConfig(String namespace) { | |
@Override | ||
public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) { | ||
String namespaceFileName = String.format("%s.%s", namespace, configFileFormat.getValue()); | ||
String lockKey = buildLockKey(namespaceFileName); | ||
Object lock = m_configFileLocks.get(lockKey); | ||
if (lock == null) { | ||
synchronized (lockKey) { | ||
lock = m_configFileLocks.computeIfAbsent(lockKey, k -> new Object()); | ||
} | ||
} | ||
ConfigFile configFile = m_configFiles.get(namespaceFileName); | ||
|
||
if (configFile == null) { | ||
synchronized (this) { | ||
synchronized (lock) { | ||
configFile = m_configFiles.get(namespaceFileName); | ||
|
||
if (configFile == null) { | ||
|
@@ -79,4 +95,8 @@ public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFor | |
|
||
return configFile; | ||
} | ||
|
||
private String buildLockKey(String factor) { | ||
return (String.format("%s.%s", getClass().getName(), factor)).intern(); | ||
} | ||
} |