Skip to content

Commit

Permalink
addressing review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
spoon16 committed Jun 12, 2015
1 parent f7edacc commit 8302786
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.netflix.config.WatchedConfigurationSource;
Expand All @@ -18,6 +19,9 @@
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.google.common.collect.Maps.newHashMap;
import static com.netflix.config.WatchedUpdateResult.createIncremental;


/**
* Implementation of the dynamic {@link WatchedConfigurationSource} for Etcd
Expand All @@ -44,51 +48,7 @@ public class EtcdConfigurationSource implements WatchedConfigurationSource {
private final Etcd etcd;
private final String configPath;

private Handler<Response> updateHandler = new Handler<Response>() {
@Override
public void handle(Response updateResponse) {
if (updateResponse.wasError()) {
logger.error("Etcd failed with an error response: %s", updateResponse);
}

final Map<String, Object> create = Maps.newHashMap();
final Map<String, Object> set = Maps.newHashMap();
final Map<String, Object> delete = Maps.newHashMap();

final String action = updateResponse.action().toLowerCase();
final Node node = updateResponse.node();

if (node != null ) {
final String etcdKey = node.key();
final String sourceKey = Iterables.getLast(keySplitter.split(etcdKey));
final String value = node.getValue();
valueCache.put(sourceKey, value);

switch (action) {
case "create":
create.put(sourceKey, value);
break;

case "set":
set.put(sourceKey, value);
break;

case "delete":
delete.put(sourceKey, value);
break;

default:
logger.warn("unrecognized action, response: %s", updateResponse);
break;
}

final WatchedUpdateResult result = WatchedUpdateResult.createIncremental(create, set, delete);
updateConfiguration(result);
}

etcd.waitRecursive(updateHandler, configPath);
}
};
private Handler<Response> updateHandler = new UpdateHandler();

/**
* Initialize EtcdConfigurationSource with property values @ configPath
Expand Down Expand Up @@ -144,4 +104,56 @@ private void updateConfiguration(WatchedUpdateResult result) {
}
}
}

private class UpdateHandler implements Handler<Response> {
@Override
public void handle(Response updateResponse) {
if (updateResponse.wasError()) {
logger.error("Etcd failed with an error response: %s", updateResponse);
etcd.waitRecursive(updateHandler, configPath);
return;
}

logger.debug("Etcd updateResponse: ", updateResponse);
final Node node = updateResponse.node();

if (node != null ) {
final String etcdKey = node.key();
final String sourceKey = Iterables.getLast(keySplitter.split(etcdKey));
final String value = node.getValue();
final String action = getUpdateAction(node, updateResponse.action());

switch (action) {
case "create":
valueCache.put(sourceKey, value);
updateConfiguration(createIncremental(null, ImmutableMap.<String, Object>of(sourceKey, value), null));
break;

case "set":
valueCache.put(sourceKey, value);
updateConfiguration(createIncremental(ImmutableMap.<String, Object>of(sourceKey, value), null, null));
break;

case "delete":
valueCache.remove(sourceKey);
updateConfiguration(createIncremental(null, null, ImmutableMap.<String, Object>of(sourceKey, "")));
break;

default:
logger.warn("unrecognized action, response: %s", updateResponse);
break;
}
}

etcd.waitRecursive(updateHandler, configPath);
}

private String getUpdateAction(Node updateNode, String responseAction) {
final String value = updateNode.getValue();
if (value == null) {
return "delete";
}
return responseAction.toLowerCase();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class EtcdConfigurationSourceTest {
new Node("/config", null, 1378, 1378, 0, true, Lists.newArrayList(
new Node("/config/test.key1", "test.value1-etcd", 19311, 19311, 0, false, null),
new Node("/config/test.key4", "test.value4-etcd", 1388, 1388, 0, false, null),
new Node("/config/test.key6", "test.value6-etcd", 1232, 1232, 0, false, null)
new Node("/config/test.key6", "test.value6-etcd", 1232, 1232, 0, false, null),
new Node("/config/test.key7", "test.value7-etcd", 1234, 1234, 0, false, null)
)));
private static Handler<Response> ETCD_UPDATE_HANDLER;
private static final Answer WITH_ETCD_UPDATE_HANDLER = new Answer() {
Expand Down Expand Up @@ -72,6 +73,7 @@ public static void before() throws Exception {
MAP_CONFIGURATION.addProperty("test.key2", "test.value2-map");
MAP_CONFIGURATION.addProperty("test.key3", "test.value3-map");
MAP_CONFIGURATION.addProperty("test.key4", "test.value4-map");
MAP_CONFIGURATION.addProperty("test.key7", "test.value7-map");
compositeConfig.addConfiguration(MAP_CONFIGURATION, "map configuration");

System.setProperty("test.key4", "test.value4-system");
Expand Down Expand Up @@ -143,9 +145,24 @@ public void testUpdateEtcdProperty() throws Exception {
final String updateValue = "test.value6-etcd-override";
final String initialValue = "test.value6-etcd";

assertEquals(initialValue, DynamicPropertyFactory.getInstance().getStringProperty("test.key6", "default").get());
assertEquals(initialValue, DynamicPropertyFactory.getInstance().getStringProperty(updateProperty, "default").get());

ETCD_UPDATE_HANDLER.handle(new Response("set", 200, new Node(updateKey, updateValue, 19444, 19444, 0, false, null)));
assertEquals(updateValue, DynamicPropertyFactory.getInstance().getStringProperty("test.key6", "default").get());
assertEquals(updateValue, DynamicPropertyFactory.getInstance().getStringProperty(updateProperty, "default").get());
}

/**
* should delete from EtcdConfigurationSource when Etcd client handles a delete event
*/
@Test
public void testDeleteEtcdProperty() throws Exception {
final String deleteProperty = "test.key7";
final String deleteKey = CONFIG_PATH + "/" + deleteProperty;
final String initialValue = "test.value7-etcd";

assertEquals(initialValue, DynamicPropertyFactory.getInstance().getStringProperty(deleteProperty, "default").get());

ETCD_UPDATE_HANDLER.handle(new Response("delete", 200, new Node(deleteKey, null, 12345, 12345, 0, false, null)));
assertEquals("test.value7-map", DynamicPropertyFactory.getInstance().getStringProperty(deleteProperty, "default").get());
}
}

0 comments on commit 8302786

Please sign in to comment.