Skip to content

Commit

Permalink
Polish apache#4120 : To add DynamicConfiguration implementation for Z…
Browse files Browse the repository at this point in the history
…ookeeper
  • Loading branch information
mercyblitz committed May 23, 2019
1 parent a5da695 commit 3894142
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static java.util.Collections.emptySortedSet;
import static java.util.Collections.unmodifiableSortedSet;
import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
import static org.apache.dubbo.configcenter.Constants.CONFIG_NAMESPACE_KEY;

/**
*
*/
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 @@ -91,12 +100,53 @@ public void removeListener(String key, String group, ConfigurationListener liste

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
String path = buildPath(key, group);
return (String) getInternalProperty(path);
}

/**
* For zookeeper, {@link #getConfig(String, String, long)} and {@link #getConfigs(String, String, long)} have the same meaning.
*
* @param key
* @param group
* @param timeout
* @return
* @throws IllegalStateException
*/
@Override
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
return getConfig(key, group, timeout);
}

@Override
public boolean publishConfig(String key, String group, String content) {
String path = buildPath(key, group);
zkClient.create(path, content, true);
return true;
}

@Override
public SortedSet<String> getConfigKeys(String group) {
String path = buildPath(group);
List<String> nodes = zkClient.getChildren(path);
return isEmpty(nodes) ? emptySortedSet() : unmodifiableSortedSet(new TreeSet<>(nodes));
}

/**
* Build the config node path by the specified <code>key</code> and <code>group</code>
*
* @param key the key to represent a configuration
* @param group the group where the key belongs to
* @return
*/
protected String buildPath(String key, String group) {
String path = null;
/**
* when group is not null, we are getting startup configs from Config Center, for example:
* group=dubbo, key=dubbo.properties
*/
if (StringUtils.isNotEmpty(group)) {
key = group + "/" + key;
path = group + "/" + key;
}
/**
* when group is null, we are fetching governance rules, for example:
Expand All @@ -105,23 +155,14 @@ public String getConfig(String key, String group, long timeout) throws IllegalSt
*/
else {
int i = key.lastIndexOf(".");
key = key.substring(0, i) + "/" + key.substring(i + 1);
path = key.substring(0, i) + "/" + key.substring(i + 1);
}

return (String) getInternalProperty(rootPath + "/" + key);
return buildPath(path);
}

/**
* For zookeeper, {@link #getConfig(String, String, long)} and {@link #getConfigs(String, String, long)} have the same meaning.
*
* @param key
* @param group
* @param timeout
* @return
* @throws IllegalStateException
*/
@Override
public String getConfigs(String key, String group, long timeout) throws IllegalStateException {
return (String) getConfig(key, group, timeout);
protected String buildPath(String relativePath) {
String path = rootPath + "/" + relativePath;
return path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* TODO refactor using mockito
*/
Expand Down Expand Up @@ -122,6 +128,39 @@ public void testAddListener() throws Exception {
Assertions.assertEquals("new value2", listener4.getValue());
}

@Test
public void testPublishConfig() {
String key = "user-service";
String group = "org.apache.dubbo.service.UserService";
String content = "test";

assertTrue(configuration.publishConfig(key, group, content));
assertEquals("test", configuration.getConfigs(key, group));
}

@Test
public void testGetConfigKeysAndContents() {

String key = "user-service";
String group = "org.apache.dubbo.service.UserService";
String content = "test";

String key2 = "user-service-1";

assertTrue(configuration.publishConfig(key, group, content));
assertTrue(configuration.publishConfig(key2, group, content));

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

assertEquals(new TreeSet(asList(key, key2)), configKeys);

Map<String, String> configs = configuration.getConfigs(group);

assertEquals(configs.keySet(), configKeys);

configs.forEach((k, value) -> assertEquals(content, value));
}

private class TestListener implements ConfigurationListener {
private CountDownLatch latch;
private String value;
Expand Down

0 comments on commit 3894142

Please sign in to comment.