From 37b7a3a7431709d483ffb786099fbb577be7f49e Mon Sep 17 00:00:00 2001 From: Jim Carroll Date: Sun, 13 May 2012 09:35:08 -0400 Subject: [PATCH] More consistent reset behavior for a zookeeper cluster. --- .../mpcluster/zookeeper/ZookeeperSession.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib-dempsyimpl/src/main/java/com/nokia/dempsy/mpcluster/zookeeper/ZookeeperSession.java b/lib-dempsyimpl/src/main/java/com/nokia/dempsy/mpcluster/zookeeper/ZookeeperSession.java index 30fcb0a9..f3a30332 100644 --- a/lib-dempsyimpl/src/main/java/com/nokia/dempsy/mpcluster/zookeeper/ZookeeperSession.java +++ b/lib-dempsyimpl/src/main/java/com/nokia/dempsy/mpcluster/zookeeper/ZookeeperSession.java @@ -81,6 +81,14 @@ protected ZooKeeper makeZookeeperInstance(String connectString, int sessionTimeo { return new ZooKeeper(connectString, sessionTimeout, new ZkWatcher()); } + + /** + * This is defined here to be overridden in a test. + */ + protected ZookeeperCluster makeZookeeperCluster(ClusterId clusterId) throws MpClusterException + { + return new ZookeeperCluster(clusterId); + } @Override public MpApplication getApplication(String applicationId) throws MpClusterException @@ -109,7 +117,7 @@ public MpCluster getCluster(ClusterId clusterId) throws MpClusterException if (cluster == null) { // make sure the paths are set up - cluster = new ZookeeperCluster(clusterId); + cluster = makeZookeeperCluster(clusterId); initializeCluster(cluster,false); cachedClusters.put(clusterId, cluster); } @@ -361,14 +369,18 @@ public void process(WatchedEvent event) // event = null means it was called explicitly if (logger.isDebugEnabled() && event != null) logger.debug("CALLBACK:MpContainerCluster for " + idForLogging + " Event:" + event); - + + boolean kickOffProcess = true; if (event != null) { if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) clearState(event); + // when we're not connected we want to reset if (event.getState() != KeeperState.SyncConnected) { + kickOffProcess = false; // no reason to execute process if we're going to reset zookeeper. + clearState(event); if (zkref != null) @@ -376,10 +388,13 @@ public void process(WatchedEvent event) } } - synchronized(processLock) + if (kickOffProcess) { - for(MpClusterWatcher watch: watchers) - watch.process(); + synchronized(processLock) + { + for(MpClusterWatcher watch: watchers) + watch.process(); + } } } } @@ -414,7 +429,7 @@ public class ZookeeperCluster extends WatcherManager implements MpCluster private ZookeeperPath clusterPath; private ZookeeperPath appPath; - private ZookeeperCluster(ClusterId clusterId) throws MpClusterException + protected ZookeeperCluster(ClusterId clusterId) throws MpClusterException { super(clusterId.toString()); this.clusterId = clusterId;