Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B872110943 for ; Wed, 19 Nov 2014 03:24:42 +0000 (UTC) Received: (qmail 88533 invoked by uid 500); 19 Nov 2014 03:24:42 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 88500 invoked by uid 500); 19 Nov 2014 03:24:42 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 88491 invoked by uid 99); 19 Nov 2014 03:24:42 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Nov 2014 03:24:42 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A60259AA0CD; Wed, 19 Nov 2014 03:24:41 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cammckenzie@apache.org To: commits@curator.apache.org Message-Id: <8ee717a639ed42ac974f395ae2903c1a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: curator git commit: 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 Date: Wed, 19 Nov 2014 03:24:41 +0000 (UTC) Repository: curator Updated Branches: refs/heads/CURATOR-167 [created] 44c3891e3 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/44c3891e Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/44c3891e Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/44c3891e Branch: refs/heads/CURATOR-167 Commit: 44c3891e3e1633e456c530165513fff50a52a8b4 Parents: ef2ca57 Author: Cameron McKenzie Authored: Wed Nov 19 14:23:24 2014 +1100 Committer: Cameron McKenzie Committed: Wed Nov 19 14:23:24 2014 +1100 ---------------------------------------------------------------------- .../framework/recipes/cache/NodeCache.java | 42 ++++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/44c3891e/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 fa0df51..e745af7 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,24 +22,27 @@ 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.EnsurePath; 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; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; + import org.apache.curator.utils.PathUtils; /** @@ -62,7 +65,7 @@ public class NodeCache implements Closeable private final AtomicReference state = new AtomicReference(State.LATENT); private final ListenerContainer listeners = new ListenerContainer(); 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) @@ -88,12 +91,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); + } } }; @@ -173,8 +183,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; + } } /** @@ -315,4 +333,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); + } }