Skip to content

Commit 6a1b9ee

Browse files
beiwei30chickenlj
authored andcommitted
Merge pull request apache#2810, code review and refactor for dubbo-configcenter.
1 parent 6140bc2 commit 6a1b9ee

File tree

19 files changed

+183
-83
lines changed

19 files changed

+183
-83
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/RouterChain.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class RouterChain<T> {
5151

5252
public static <T> RouterChain<T> buildChain(DynamicConfiguration dynamicConfiguration, URL url) {
5353
RouterChain<T> routerChain = new RouterChain<>(url);
54-
List<RouterFactory> extensionFactories = ExtensionLoader.getExtensionLoader(RouterFactory.class).getActivateExtension(dynamicConfiguration.getUrl(), (String[]) null);
54+
List<RouterFactory> extensionFactories = ExtensionLoader.getExtensionLoader(RouterFactory.class).getActivateExtension(url, (String[]) null);
5555
List<Router> routers = extensionFactories.stream()
5656
.map(factory -> {
5757
Router router = factory.getRouter(dynamicConfiguration, url);

dubbo-common/src/main/java/org/apache/dubbo/common/config/Configuration.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.dubbo.common.config;
1818

1919
/**
20-
*
20+
* Configuration interface, to fetch the value for the specified key.
2121
*/
2222
public interface Configuration {
2323
/**
@@ -58,6 +58,15 @@ public interface Configuration {
5858
*/
5959
Object getProperty(String key);
6060

61+
/**
62+
* Gets a property from the configuration. The default value will return if the configuration doesn't contain
63+
* the mapping for the specified key.
64+
*
65+
* @param key property to retrieve
66+
* @param defaultValue default value
67+
* @return the value to which this configuration maps the specified key, or default value if the configuration
68+
* contains no mapping for this key.
69+
*/
6170
Object getProperty(String key, Object defaultValue);
6271

6372
/**

dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,6 @@ public Set<String> getLoadedExtensions() {
302302
return Collections.unmodifiableSet(new TreeSet<String>(cachedInstances.keySet()));
303303
}
304304

305-
public Set<Object> getLoadedExtensionInstances() {
306-
return Collections.unmodifiableSet(cachedInstances.values().stream().map(Holder::get).collect(Collectors.toSet()));
307-
}
308-
309305
public Object getLoadedAdaptiveExtensionInstances() {
310306
return cachedAdaptiveInstance.get();
311307
}
@@ -340,6 +336,14 @@ public T getExtension(String name) {
340336
return (T) instance;
341337
}
342338

339+
/**
340+
* Return all available extension instances.
341+
*/
342+
public Set<T> getExtensions() {
343+
return Collections.unmodifiableSet(getSupportedExtensions().stream().map(this::getExtension)
344+
.collect(Collectors.toSet()));
345+
}
346+
343347
/**
344348
* Return default extension, return <code>null</code> if it's not configured.
345349
*/

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected void checkRegistry() {
157157
// For compatibility purpose, use registry as the default config center if the registry protocol is zookeeper and there's no config center specified explicitly.
158158
RegistryConfig registry = registries.get(0);
159159
if (registry.isZookeeperProtocol()) {
160-
Set<Object> loadedConfigurations = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getLoadedExtensionInstances();
160+
Set<DynamicConfiguration> loadedConfigurations = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getExtensions();
161161
// we use the loading status of DynamicConfiguration to decide whether ConfigCenter has been initiated.
162162
if (CollectionUtils.isEmpty(loadedConfigurations)) {
163163
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ConfigCenterConfig.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.dubbo.common.URL;
2121
import org.apache.dubbo.common.config.Environment;
2222
import org.apache.dubbo.common.extension.ExtensionLoader;
23-
import org.apache.dubbo.common.utils.CollectionUtils;
2423
import org.apache.dubbo.common.utils.StringUtils;
2524
import org.apache.dubbo.common.utils.UrlUtils;
2625
import org.apache.dubbo.config.support.Parameter;
@@ -31,7 +30,6 @@
3130
import java.util.HashMap;
3231
import java.util.Map;
3332
import java.util.Properties;
34-
import java.util.Set;
3533

3634
/**
3735
*
@@ -102,16 +100,11 @@ private DynamicConfiguration startDynamicConfiguration() {
102100
// checkConfigCenter();
103101

104102
URL url = toConfigUrl();
105-
Set<Object> loadedConfigurations = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getLoadedExtensionInstances();
106-
if (CollectionUtils.isEmpty(loadedConfigurations)) {
107-
DynamicConfiguration dynamicConfiguration = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getExtension(url.getProtocol());
108-
// TODO, maybe we need a factory to do this?
109-
dynamicConfiguration.setUrl(url);
110-
dynamicConfiguration.init();
111-
return dynamicConfiguration;
112-
}
113103

114-
return (DynamicConfiguration) loadedConfigurations.iterator().next();
104+
DynamicConfiguration dynamicConfiguration = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getExtension(url.getProtocol());
105+
// TODO, maybe we need a factory to do this?
106+
dynamicConfiguration.initWith(url);
107+
return dynamicConfiguration;
115108
}
116109

117110
private URL toConfigUrl() {

dubbo-configcenter/dubbo-configcenter-api/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<artifactId>dubbo-configcenter-api</artifactId>
2626
<packaging>jar</packaging>
2727
<name>${project.artifactId}</name>
28-
<description>The api definition of the service configcenter module</description>
28+
<description>The api definition of the service config-center module</description>
2929
<properties>
3030
<skip_maven_deploy>false</skip_maven_deploy>
3131
</properties>
@@ -37,4 +37,4 @@
3737
<version>${project.parent.version}</version>
3838
</dependency>
3939
</dependencies>
40-
</project>
40+
</project>

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/AbstractDynamicConfiguration.java

+50-22
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,36 @@
2323
import java.util.concurrent.ConcurrentMap;
2424

2525
/**
26+
* Dynamic configuration template class. The concrete implementation needs to provide implementation for three methods.
2627
*
28+
* @see AbstractDynamicConfiguration#getTargetConfig(String, String, long)
29+
* @see AbstractDynamicConfiguration#addListener(String, ConfigurationListener)
30+
* @see AbstractDynamicConfiguration#createTargetListener(String, ConfigurationListener)
2731
*/
28-
public abstract class AbstractDynamicConfiguration<TargetConfigListener> extends AbstractConfiguration implements DynamicConfiguration {
29-
public static final String DEFAULT_GROUP = "dubbo";
32+
public abstract class AbstractDynamicConfiguration<TargetListener> extends AbstractConfiguration
33+
implements DynamicConfiguration {
34+
protected static final String DEFAULT_GROUP = "dubbo";
35+
3036
protected URL url;
31-
/**
32-
* One key can register multiple target listeners, but one target listener only maps to one configuration listener
33-
*/
34-
private ConcurrentMap<String, ConcurrentMap<ConfigurationListener, TargetConfigListener>> listenerToTargetListenerMap = new ConcurrentHashMap<>();
37+
38+
// One key can register multiple target listeners, but one target listener only maps to one configuration listener
39+
private ConcurrentMap<String, ConcurrentMap<ConfigurationListener, TargetListener>> targetListeners =
40+
new ConcurrentHashMap<>();
3541

3642
public AbstractDynamicConfiguration() {
3743
}
3844

45+
@Override
46+
public void initWith(URL url) {
47+
this.url = url;
48+
}
49+
3950
@Override
4051
public void addListener(String key, ConfigurationListener listener) {
41-
ConcurrentMap<ConfigurationListener, TargetConfigListener> listeners = listenerToTargetListenerMap.computeIfAbsent(key, k -> new ConcurrentHashMap<>());
42-
TargetConfigListener targetListener = listeners.computeIfAbsent(listener, k -> createTargetConfigListener(key, listener));
52+
ConcurrentMap<ConfigurationListener, TargetListener> listeners = targetListeners.computeIfAbsent(key,
53+
k -> new ConcurrentHashMap<>());
54+
TargetListener targetListener = listeners.computeIfAbsent(listener,
55+
k -> createTargetListener(key, listener));
4356
addTargetListener(key, targetListener);
4457
}
4558

@@ -60,33 +73,48 @@ public String getConfig(String key, ConfigurationListener listener) {
6073

6174
@Override
6275
public String getConfig(String key, String group, ConfigurationListener listener) {
63-
return getConfig(key, group, 0l, listener);
76+
return getConfig(key, group, listener, 0L);
6477
}
6578

6679
@Override
67-
public String getConfig(String key, String group, long timeout, ConfigurationListener listener) {
80+
public String getConfig(String key, String group, ConfigurationListener listener, long timeout) {
6881
try {
6982
if (listener != null) {
7083
this.addListener(key, listener);
7184
}
72-
return getInternalProperty(key, group, timeout);
85+
return getTargetConfig(key, group, timeout);
7386
} catch (Exception e) {
7487
throw new IllegalStateException(e.getMessage(), e);
7588
}
7689
}
7790

78-
public URL getUrl() {
79-
return url;
80-
}
81-
82-
public void setUrl(URL url) {
83-
this.url = url;
84-
}
85-
86-
protected abstract String getInternalProperty(String key, String group, long timeout);
91+
/**
92+
* Fetch dynamic configuration from backend config storage. If timeout exceeds, exception should be thrown.
93+
*
94+
* @param key property key
95+
* @param group group
96+
* @param timeout timeout
97+
* @return target config value
98+
*/
99+
protected abstract String getTargetConfig(String key, String group, long timeout);
87100

88-
protected abstract void addTargetListener(String key, TargetConfigListener listener);
101+
/**
102+
* Register a native listener to the backend config storage so that Dubbo has chance to get notified when the
103+
* value changes.
104+
*
105+
* @param key property key listener is interested.
106+
* @param listener native listener for the backend config storage
107+
*/
108+
protected abstract void addTargetListener(String key, TargetListener listener);
89109

90-
protected abstract TargetConfigListener createTargetConfigListener(String key, ConfigurationListener listener);
110+
/**
111+
* Create a native listener for the backend config storage, eventually ConfigurationListener will get notified once
112+
* the value changes.
113+
*
114+
* @param key property key the native listener will listen on
115+
* @param listener ConfigurationListener instance
116+
* @return native listener for the backend config storage
117+
*/
118+
protected abstract TargetListener createTargetListener(String key, ConfigurationListener listener);
91119

92120
}

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeEvent.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.apache.dubbo.configcenter;
1818

1919
/**
20+
* Config change event.
2021
*
22+
* @see ConfigChangeType
2123
*/
2224
public class ConfigChangeEvent {
2325
private String key;

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigChangeType.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@
1717
package org.apache.dubbo.configcenter;
1818

1919
/**
20-
*
20+
* Config change event type
2121
*/
2222
public enum ConfigChangeType {
23+
/**
24+
* A config is created.
25+
*/
2326
ADDED,
27+
28+
/**
29+
* A config is updated.
30+
*/
2431
MODIFIED,
32+
33+
/**
34+
* A config is deleted.
35+
*/
2536
DELETED
2637
}

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigType.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
package org.apache.dubbo.configcenter;
1818

1919
/**
20-
*
20+
* Config type
2121
*/
2222
public enum ConfigType {
23+
/**
24+
* For Dubbo dynamic config other than routing rules.
25+
*/
2326
CONFIGURATORS,
27+
28+
/**
29+
* For Dubbo routing rules
30+
*/
2431
ROUTERS
2532
}

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigurationListener.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919
import org.apache.dubbo.common.URL;
2020

2121
/**
22-
*
22+
* Config listener, will get notified when the config it listens on changes.
2323
*/
2424
public interface ConfigurationListener {
2525

26+
/**
27+
* Listener call back method. Listener gets notified by this method once there's any change happens on the config
28+
* the listener listens on.
29+
*
30+
* @param event config change event
31+
*/
2632
void process(ConfigChangeEvent event);
2733

34+
// FIXME: why we need this?
2835
URL getUrl();
2936
}

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigurationUtils.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.concurrent.ConcurrentHashMap;
2929

3030
/**
31-
*
31+
* Utilities for manipulating configurations from different sources
3232
*/
3333
public class ConfigurationUtils {
3434
private static final CompositeConfiguration compositeConfiguration;
@@ -64,17 +64,16 @@ public static CompositeConfiguration getRuntimeCompositeConf(URL url, String met
6464
}
6565

6666
/**
67-
* If user opens DynamicConfig, the extension instance must has been created during the initialization of ConfigCenterConfig with the right extension type user specified.
68-
* If no DynamicConfig presents, NopDynamicConfiguration will be used.
69-
*
70-
* @return
67+
* If user opens DynamicConfig, the extension instance must has been created during the initialization of
68+
* ConfigCenterConfig with the right extension type user specified. If no DynamicConfig presents,
69+
* NopDynamicConfiguration will be used.
7170
*/
7271
public static DynamicConfiguration getDynamicConfiguration() {
73-
Set<Object> configurations = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getLoadedExtensionInstances();
72+
Set<DynamicConfiguration> configurations = ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getExtensions();
7473
if (CollectionUtils.isEmpty(configurations)) {
7574
return ExtensionLoader.getExtensionLoader(DynamicConfiguration.class).getDefaultExtension();
7675
} else {
77-
return (DynamicConfiguration) configurations.iterator().next();
76+
return configurations.iterator().next();
7877
}
7978
}
8079

dubbo-configcenter/dubbo-configcenter-api/src/main/java/org/apache/dubbo/configcenter/ConfigurationWrapper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.apache.dubbo.common.config.Configuration;
2121

2222
/**
23-
*
23+
* A wrapper to fetch a config for the specific key with the different prefix in the specified order.
2424
*/
2525
public class ConfigurationWrapper extends AbstractConfiguration {
2626
private String application;
@@ -36,6 +36,7 @@ public ConfigurationWrapper(String application, String service, String method, C
3636
this.delegate = configuration;
3737
}
3838

39+
// FIXME: I think the order is wrong, service.method.key go first, then service.key, and then application.key
3940
@Override
4041
protected Object getInternalProperty(String key) {
4142
Object value = delegate.getProperty(application + "." + key);

0 commit comments

Comments
 (0)