Skip to content

Commit

Permalink
exception for misconfigured zookeepercoordinator options will now con…
Browse files Browse the repository at this point in the history
…tain all the missing options first. still would be nice to validate data types at some point.
  • Loading branch information
bryanduxbury committed Apr 21, 2011
1 parent 860a7f1 commit 83be927
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions src/java/com/rapleaf/hank/coordinator/zk/ZooKeeperCoordinator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.rapleaf.hank.coordinator.zk;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -48,6 +49,52 @@
* removal of domains, domain groups, ring groups, or hosts.
*/
public class ZooKeeperCoordinator extends ZooKeeperConnection implements Coordinator, DomainGroupChangeListener, RingGroupChangeListener {
private static final Logger LOG = Logger.getLogger(ZooKeeperCoordinator.class);

/**
* Used to instantiate a ZooKeeperCoordinator generically.
*/
public static final class Factory implements CoordinatorFactory {
private static final String RING_GROUPS_ROOT_KEY = "ring_groups_root";
private static final String DOMAIN_GROUPS_ROOT_KEY = "domain_groups_root";
private static final String DOMAINS_ROOT_KEY = "domains_root";
private static final String SESSION_TIMEOUT_KEY = "session_timeout";
private static final String CONNECT_STRING_KEY = "connect_string";
private static final List<String> REQUIRED_KEYS = Arrays.asList(
RING_GROUPS_ROOT_KEY,
DOMAIN_GROUPS_ROOT_KEY,
DOMAINS_ROOT_KEY,
SESSION_TIMEOUT_KEY,
CONNECT_STRING_KEY
);

@Override
public Coordinator getCoordinator(Map<String, Object> options) {
validateOptions(options);
try {
return new ZooKeeperCoordinator((String)options.get(CONNECT_STRING_KEY),
(Integer)options.get(SESSION_TIMEOUT_KEY),
(String)options.get(DOMAINS_ROOT_KEY),
(String)options.get(DOMAIN_GROUPS_ROOT_KEY),
(String)options.get(RING_GROUPS_ROOT_KEY));
} catch (Exception e) {
throw new RuntimeException("Couldn't make a ZooKeeperCoordinator from options " + options, e);
}
}

private void validateOptions(Map<String, Object> options) {
Set<String> missingKeys = new HashSet<String>();
for (String requiredKey : REQUIRED_KEYS) {
if (!options.containsKey(requiredKey)) {
missingKeys.add(requiredKey);
}
}
if (!missingKeys.isEmpty()) {
throw new RuntimeException("Options for ZooKeeperCoordinator was missing required keys: " + missingKeys);
}
}
}

private final class WatchForNewDomainGroups extends HankWatcher {
public WatchForNewDomainGroups() throws KeeperException, InterruptedException {
super();
Expand Down Expand Up @@ -77,23 +124,6 @@ public void realProcess(WatchedEvent event) {
}
}

private static final Logger LOG = Logger.getLogger(ZooKeeperCoordinator.class);

public static final class Factory implements CoordinatorFactory {
@Override
public Coordinator getCoordinator(Map<String, Object> options) {
try {
return new ZooKeeperCoordinator((String)options.get("connect_string"),
(Integer)options.get("session_timeout"),
(String)options.get("domains_root"),
(String)options.get("domain_groups_root"),
(String)options.get("ring_groups_root"));
} catch (Exception e) {
throw new RuntimeException("Couldn't make a ZooKeeperCoordinator from options " + options, e);
}
}
}

/**
* We save our watchers so that we can reregister them in case of session
* expiry.
Expand Down

0 comments on commit 83be927

Please sign in to comment.