Skip to content

Commit

Permalink
[Refactor] Refactor the DynamicConfiguration interface #5339 (#5340)
Browse files Browse the repository at this point in the history
* Polish #5306 : [Migration] Upgrade the @SInCE tags in Javadoc migration cloud native to master

* Polish #5306 : [Migration] Upgrade the @SInCE tags in Javadoc migration cloud native to master

* Polish #5309 : [ISSURE] The beans of Dubbo's Config can't be found on the ReferenceBean's initialization

* Polish #5312 : Resolve the demos' issues of zookeeper and nacos

* Polish #5313 : [Migration] migrate the code in common module from cloud-native branch to master

* Polish #5316 : [Refactor] Replace @EnableDubboConfigBinding Using spring-context-support

* Polish #5317 : [Refactor] Refactor ReferenceAnnotationBeanPostProcessor using Alibaba spring-context-suuport API

* Polish #5321 : Remove BeanFactoryUtils

* Polish #5321 : Remove AnnotatedBeanDefinitionRegistryUtils

* Polish #5321 : Remove AnnotationUtils

* Polish #5321 : Remove ClassUtils

* Polish #5321 : Remove BeanRegistrar

* Polish #5321 : Remove ObjectUtils

* Polish #5321 : Remove PropertySourcesUtils

* Polish #5325 : [Migration] To migrate dubbo-metadata-api from cloud-native branch

* Polish #5326 : [Migration] To migrate dubbo-metadata-processor from cloud-native branch

* Polish #5329 : [Feature] To add the default metadata into ServiceInstance

* Polish #5339 : [Refactor] Refactor the DynamicConfiguration interface
  • Loading branch information
mercyblitz committed Nov 18, 2019
1 parent c42b0a8 commit ec16ba4
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 84 deletions.
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

0 comments on commit ec16ba4

Please sign in to comment.