CURATOR-167 - Partial fix to clean up Curator managed watch objects when the cache closes. A full fix requires the ability to cancel watches in ZK which is not available until ZK 3.5
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/8fae7856 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/8fae7856 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/8fae7856 Branch: refs/heads/CURATOR-248 Commit: 8fae7856edc1a5269fd012c683860e0b150e13b3 Parents: 5dc27c1 Author: Cameron McKenzie <came...@unico.com.au> Authored: Wed Nov 19 14:23:24 2014 +1100 Committer: Cam McKenzie <cammcken...@apache.org> Committed: Tue Aug 25 14:06:35 2015 +1000 ---------------------------------------------------------------------- .../framework/recipes/cache/NodeCache.java | 41 ++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/8fae7856/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/NodeCache.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/NodeCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/NodeCache.java index 72ee5ff..bfc27d8 100644 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/NodeCache.java +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/NodeCache.java @@ -22,19 +22,21 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Preconditions; + import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.api.BackgroundCallback; import org.apache.curator.framework.api.CuratorEvent; -import org.apache.curator.framework.api.CuratorWatcher; import org.apache.curator.framework.listen.ListenerContainer; import org.apache.curator.framework.state.ConnectionState; import org.apache.curator.framework.state.ConnectionStateListener; import org.apache.curator.utils.PathUtils; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.io.Closeable; import java.io.IOException; import java.util.concurrent.Exchanger; @@ -60,7 +62,7 @@ public class NodeCache implements Closeable private final AtomicReference<State> state = new AtomicReference<State>(State.LATENT); private final ListenerContainer<NodeCacheListener> listeners = new ListenerContainer<NodeCacheListener>(); private final AtomicBoolean isConnected = new AtomicBoolean(true); - private final ConnectionStateListener connectionStateListener = new ConnectionStateListener() + private ConnectionStateListener connectionStateListener = new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) @@ -86,12 +88,19 @@ public class NodeCache implements Closeable } }; - private final CuratorWatcher watcher = new CuratorWatcher() + private Watcher watcher = new Watcher() { @Override - public void process(WatchedEvent event) throws Exception + public void process(WatchedEvent event) { - reset(); + try + { + reset(); + } + catch(Exception e) + { + handleException(e); + } } }; @@ -170,8 +179,16 @@ public class NodeCache implements Closeable if ( state.compareAndSet(State.STARTED, State.CLOSED) ) { listeners.clear(); - } - client.getConnectionStateListenable().removeListener(connectionStateListener); + client.clearWatcherReferences(watcher); + client.getConnectionStateListenable().removeListener(connectionStateListener); + + // TODO + // From PathChildrenCache + // This seems to enable even more GC - I'm not sure why yet - it + // has something to do with Guava's cache and circular references + connectionStateListener = null; + watcher = null; + } } /** @@ -312,4 +329,14 @@ public class NodeCache implements Closeable } } } + + /** + * Default behavior is just to log the exception + * + * @param e the exception + */ + protected void handleException(Throwable e) + { + log.error("", e); + } }