Skip to content
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

[Refactor] Refactor the DynamicConfiguration interface #5339 #5340

Merged
merged 26 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e5c50fc
Polish apache/dubbo#5306 : [Migration] Upgrade the @since tags in Jav…
mercyblitz Nov 12, 2019
b1fe4f6
Polish apache/dubbo#5306 : [Migration] Upgrade the @since tags in Jav…
mercyblitz Nov 12, 2019
6176bf0
Polish apache/dubbo#5309 : [ISSURE] The beans of Dubbo's Config can't…
mercyblitz Nov 12, 2019
d4c8956
Polish apache/dubbo#5312 : Resolve the demos' issues of zookeeper and…
mercyblitz Nov 12, 2019
4278dc1
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 12, 2019
3734800
Polish apache/dubbo#5313 : [Migration] migrate the code in common mod…
mercyblitz Nov 12, 2019
5d74025
Polish apache/dubbo#5316 : [Refactor] Replace @EnableDubboConfigBindi…
mercyblitz Nov 13, 2019
2f56570
Polish apache/dubbo#5317 : [Refactor] Refactor ReferenceAnnotationBea…
mercyblitz Nov 13, 2019
5afb058
Polish apache/dubbo#5321 : Remove BeanFactoryUtils
mercyblitz Nov 13, 2019
92af95e
Polish apache/dubbo#5321 : Remove AnnotatedBeanDefinitionRegistryUtils
mercyblitz Nov 13, 2019
2bbabf6
Polish apache/dubbo#5321 : Remove AnnotationUtils
mercyblitz Nov 13, 2019
8596072
Polish apache/dubbo#5321 : Remove ClassUtils
mercyblitz Nov 13, 2019
256ee6e
Polish apache/dubbo#5321 : Remove BeanRegistrar
mercyblitz Nov 13, 2019
96276f4
Polish apache/dubbo#5321 : Remove ObjectUtils
mercyblitz Nov 13, 2019
ed0f920
Polish apache/dubbo#5321 : Remove PropertySourcesUtils
mercyblitz Nov 13, 2019
0da7e27
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 14, 2019
e97abee
Sync commit
mercyblitz Nov 14, 2019
14c116d
Polish apache/dubbo#5325 : [Migration] To migrate dubbo-metadata-api …
mercyblitz Nov 14, 2019
b1eb630
Polish apache/dubbo#5326 : [Migration] To migrate dubbo-metadata-proc…
mercyblitz Nov 14, 2019
4c72e2c
Sync
mercyblitz Nov 14, 2019
2827278
Polish apache/dubbo#5329 : [Feature] To add the default metadata into…
mercyblitz Nov 14, 2019
8f39074
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 14, 2019
348e31d
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 15, 2019
60ba484
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 15, 2019
b669ec0
Merge remote-tracking branch 'upstream/master' into cloud-native-2.7.5
mercyblitz Nov 18, 2019
2eeee77
Polish apache/dubbo#5339 : [Refactor] Refactor the DynamicConfigurati…
mercyblitz Nov 18, 2019
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 @@ -130,7 +130,7 @@ protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation,
if (CollectionUtils.isEmpty(invokers)) {
return null;
}
String methodName = invocation == null ? StringUtils.EMPTY : invocation.getMethodName();
String methodName = invocation == null ? StringUtils.EMPTY_STRING : invocation.getMethodName();

boolean sticky = invokers.get(0).getUrl()
.getMethodParameter(methodName, CLUSTER_STICKY_KEY, DEFAULT_CLUSTER_STICKY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public interface DynamicConfiguration extends Configuration, AutoCloseable {

String DEFAULT_GROUP = "dubbo";

String DEFAULT_MAPPING_GROUP = "mapping";

/**
* {@link #addListener(String, String, ConfigurationListener)}
*
Expand Down Expand Up @@ -138,7 +136,7 @@ default String getProperties(String key, String group, long timeout) throws Ille
* @since 2.7.5
*/
default boolean publishConfig(String key, String content) throws UnsupportedOperationException {
return publishConfig(key, DEFAULT_MAPPING_GROUP, content);
return publishConfig(key, DEFAULT_GROUP, content);
}

/**
Expand All @@ -159,12 +157,11 @@ default boolean publishConfig(String key, String group, String content) throws U
* Get the config keys by the specified group
*
* @param group the specified group
* @param rootKey the
* @return the read-only non-null sorted {@link Set set} of config keys
* @throws UnsupportedOperationException If the under layer does not support
* @since 2.7.5
*/
default SortedSet<String> getConfigKeys(String group, String rootKey) throws UnsupportedOperationException {
default SortedSet<String> getConfigKeys(String group) throws UnsupportedOperationException {
throw new UnsupportedOperationException("No support");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public boolean publishConfig(String key, String group, String content) {
}

@Override
public SortedSet<String> getConfigKeys(String group, String key) {
public SortedSet<String> getConfigKeys(String group) {
File[] files = groupDirectory(group).listFiles(File::isFile);
if (files == null) {
return new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean publishConfig(String key, String group, String content) {
* @since 2.7.5
*/
@Override
public SortedSet<String> getConfigKeys(String group, String key) {
public SortedSet<String> getConfigKeys(String group) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public boolean publishConfig(String key, String group, String content) throws Un

@Override
@SuppressWarnings("unchecked")
public SortedSet<String> getConfigKeys(String group, String key) throws UnsupportedOperationException {
return (SortedSet<String>) iterateConfigOperation(configuration -> configuration.getConfigKeys(group, key));
public SortedSet<String> getConfigKeys(String group) throws UnsupportedOperationException {
return (SortedSet<String>) iterateConfigOperation(configuration -> configuration.getConfigKeys(group));
}

private void iterateListenerOperation(Consumer<DynamicConfiguration> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

public final class StringUtils {

public static final String EMPTY = "";
public static final String EMPTY_STRING = "";
public static final int INDEX_NOT_FOUND = -1;
public static final String[] EMPTY_STRING_ARRAY = new String[0];

Expand Down Expand Up @@ -82,10 +82,9 @@ public final class StringUtils {

public static final String SLASH = valueOf(SLASH_CHAR);

/**
* The empty value
*/
public static final String EMPTY_VALUE = "";
public static final char HYPHEN_CHAR = '-';

public static final String HYPHEN = valueOf(HYPHEN_CHAR);

private StringUtils() {
}
Expand Down Expand Up @@ -127,7 +126,7 @@ public static String repeat(final String str, final int repeat) {
return null;
}
if (repeat <= 0) {
return EMPTY;
return EMPTY_STRING;
}
final int inputLength = str.length();
if (repeat == 1 || inputLength == 0) {
Expand Down Expand Up @@ -653,7 +652,7 @@ public static String[] split(String str, char ch) {
*/
public static String join(String[] array) {
if (ArrayUtils.isEmpty(array)) {
return EMPTY;
return EMPTY_STRING;
}
StringBuilder sb = new StringBuilder();
for (String s : array) {
Expand All @@ -671,7 +670,7 @@ public static String join(String[] array) {
*/
public static String join(String[] array, char split) {
if (ArrayUtils.isEmpty(array)) {
return EMPTY;
return EMPTY_STRING;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
Expand All @@ -692,7 +691,7 @@ public static String join(String[] array, char split) {
*/
public static String join(String[] array, String split) {
if (ArrayUtils.isEmpty(array)) {
return EMPTY;
return EMPTY_STRING;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
Expand All @@ -706,7 +705,7 @@ public static String join(String[] array, String split) {

public static String join(Collection<String> coll, String split) {
if (CollectionUtils.isEmpty(coll)) {
return EMPTY;
return EMPTY_STRING;
}

StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -858,7 +857,7 @@ public static boolean isAllUpperCase(String str) {
if (str != null && !isEmpty(str)) {
int sz = str.length();

for(int i = 0; i < sz; ++i) {
for (int i = 0; i < sz; ++i) {
if (!Character.isUpperCase(str.charAt(i))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testPublishConfig() {

@Test
public void testGetConfigKeys() {
assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigKeys(null, null), "No support");
assertThrows(UnsupportedOperationException.class, () -> configuration.getConfigKeys(null), "No support");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import static org.apache.dubbo.common.config.configcenter.Constants.CONFIG_NAMESPACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
import static org.apache.dubbo.common.utils.StringUtils.EMPTY_STRING;

/**
* config center implementation for consul
Expand Down Expand Up @@ -95,9 +96,9 @@ public String getConfig(String key, String group, long timeout) throws IllegalSt
}

@Override
public SortedSet<String> getConfigKeys(String group, String key) throws UnsupportedOperationException {
public SortedSet<String> getConfigKeys(String group) throws UnsupportedOperationException {
SortedSet<String> configKeys = new TreeSet<>();
String normalizedKey = convertKey(group, key);
String normalizedKey = convertKey(group, EMPTY_STRING);
List<String> keys = kvClient.getKeys(normalizedKey);
if (CollectionUtils.isNotEmpty(keys)) {
keys.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
import static org.apache.dubbo.common.utils.StringUtils.HYPHEN_CHAR;
import static org.apache.dubbo.common.utils.StringUtils.SLASH_CHAR;

/**
* The nacos implementation of {@link DynamicConfiguration}
Expand Down Expand Up @@ -175,31 +177,35 @@ private NacosConfigListener createTargetListener(String key, String group) {

@Override
public void addListener(String key, String group, ConfigurationListener listener) {
NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(key, k -> createTargetListener(key, group));
String resolvedGroup = resolveGroup(group);
String listenerKey = buildListenerKey(key, group);
NacosConfigListener nacosConfigListener = watchListenerMap.computeIfAbsent(listenerKey, k -> createTargetListener(key, resolvedGroup));
nacosConfigListener.addListener(listener);
try {
configService.addListener(key, group, nacosConfigListener);
configService.addListener(key, resolvedGroup, nacosConfigListener);
} catch (NacosException e) {
logger.error(e.getMessage());
}
}

@Override
public void removeListener(String key, String group, ConfigurationListener listener) {
NacosConfigListener eventListener = watchListenerMap.get(key);
String listenerKey = buildListenerKey(key, group);
NacosConfigListener eventListener = watchListenerMap.get(listenerKey);
if (eventListener != null) {
eventListener.removeListener(listener);
}
}

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
String resolvedGroup = resolveGroup(group);
try {
long nacosTimeout = timeout < 0 ? DEFAULT_TIMEOUT : timeout;
if (StringUtils.isEmpty(group)) {
group = DEFAULT_GROUP;
if (StringUtils.isEmpty(resolvedGroup)) {
resolvedGroup = DEFAULT_GROUP;
}
return configService.getConfig(key, group, nacosTimeout);
return configService.getConfig(key, resolvedGroup, nacosTimeout);
} catch (NacosException e) {
logger.error(e.getMessage());
}
Expand All @@ -219,12 +225,13 @@ public Object getInternalProperty(String key) {
@Override
public boolean publishConfig(String key, String group, String content) {
boolean published = false;
String resolvedGroup = resolveGroup(group);
try {
String value = configService.getConfig(key, group, -1L);
String value = configService.getConfig(key, resolvedGroup, -1L);
if (StringUtils.isNotEmpty(value)) {
content = value + "," + content;
}
published = configService.publishConfig(key, group, content);
published = configService.publishConfig(key, resolvedGroup, content);
} catch (NacosException e) {
logger.error(e.getErrMsg());
}
Expand All @@ -234,19 +241,19 @@ public boolean publishConfig(String key, String group, String content) {
/**
* TODO Nacos does not support atomic update of the value mapped to a key.
*
* @param group the specified group
* @param key
* @param group the specified group
* @return
*/
@Override
public SortedSet<String> getConfigKeys(String group, String key) {
public SortedSet<String> getConfigKeys(String group) {
// TODO use Nacos Client API to replace HTTP Open API
SortedSet<String> keys = new TreeSet<>();
try {
List<String> paramsValues = asList(
"search", "accurate",
"dataId", "",
"group", group,
"group", resolveGroup(group),
"pageNo", "1",
"pageSize", String.valueOf(Integer.MAX_VALUE)
);
Expand All @@ -260,17 +267,6 @@ public SortedSet<String> getConfigKeys(String group, String key) {
}
}
return keys;

// SortedSet<String> configKeys = new TreeSet<>();
// try {
// String value = configService.getConfig(key, group, -1L);
// if (value != null) {
// Collections.addAll(configKeys, value.split(","));
// }
// } catch (NacosException e) {
// logger.error(e.getErrMsg());
// }
// return configKeys;
}

private Stream<String> toKeysStream(String content) {
Expand Down Expand Up @@ -336,4 +332,12 @@ private ConfigChangeType getChangeType(String configInfo, String oldValue) {
return ConfigChangeType.MODIFIED;
}
}

protected String buildListenerKey(String key, String group) {
return key + HYPHEN_CHAR + resolveGroup(group);
}

protected String resolveGroup(String group) {
return group.replace(SLASH_CHAR, HYPHEN_CHAR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testGetConfigKeys() {
put("key1", "a");
put("key2", "b");

SortedSet<String> keys = config.getConfigKeys(DynamicConfiguration.DEFAULT_GROUP, null);
SortedSet<String> keys = config.getConfigKeys(DynamicConfiguration.DEFAULT_GROUP);

Assertions.assertFalse(keys.isEmpty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
import static org.apache.dubbo.common.config.configcenter.Constants.CONFIG_NAMESPACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
import static org.apache.dubbo.common.utils.StringUtils.EMPTY_STRING;

/**
*
*/
public class ZookeeperDynamicConfiguration implements DynamicConfiguration {

private static final String EMPTY_STRING = "";

private static final Logger logger = LoggerFactory.getLogger(ZookeeperDynamicConfiguration.class);

private Executor executor;
Expand Down Expand Up @@ -118,8 +117,8 @@ public boolean publishConfig(String key, String group, String content) {
}

@Override
public SortedSet<String> getConfigKeys(String group, String key) {
String path = getPathKey(group, key);
public SortedSet<String> getConfigKeys(String group) {
String path = getPathKey(group, EMPTY_STRING);
List<String> nodes = zkClient.getChildren(path);
return isEmpty(nodes) ? emptySortedSet() : unmodifiableSortedSet(new TreeSet<>(nodes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void testGetConfigKeysAndContents() {
assertTrue(configuration.publishConfig(key, group, content));
assertTrue(configuration.publishConfig(key2, group, content));

Set<String> configKeys = configuration.getConfigKeys(group, key);
Set<String> configKeys = configuration.getConfigKeys(group);

assertEquals(new TreeSet(asList(content)), configKeys);
}
Expand Down
Loading