curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cammcken...@apache.org
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 GMT
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 <cameron@unico.com.au>
Authored: Wed Nov 19 14:23:24 2014 +1100
Committer: Cameron McKenzie <cameron@unico.com.au>
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> 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)
@@ -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);
+    }
 }


Mime
View raw message