curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dragonsi...@apache.org
Subject [1/6] git commit: CURATOR-144: TreeCache.Builder to configure advanced options
Date Mon, 06 Oct 2014 23:52:24 GMT
Repository: curator
Updated Branches:
  refs/heads/master 86299ede1 -> 861ba15b7


CURATOR-144: TreeCache.Builder to configure advanced options


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/a8ff472c
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/a8ff472c
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/a8ff472c

Branch: refs/heads/master
Commit: a8ff472cb0c5e63b7f87a9ebfd22c2c9660d55e5
Parents: 7d30d3d
Author: Scott Blum <scottb@squareup.com>
Authored: Fri Aug 22 16:00:20 2014 -0400
Committer: Scott Blum <scottb@squareup.com>
Committed: Fri Aug 22 16:34:55 2014 -0400

----------------------------------------------------------------------
 .../framework/recipes/cache/TreeCache.java      | 159 ++++++++++++++-----
 .../recipes/cache/BaseTestTreeCache.java        |   9 +-
 .../framework/recipes/cache/TestTreeCache.java  |  30 ++--
 .../recipes/cache/TestTreeCacheRandomTree.java  |   2 +-
 4 files changed, 140 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index a4c252b..3de3a04 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -30,6 +30,7 @@ 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.CloseableExecutorService;
+import org.apache.curator.utils.PathUtils;
 import org.apache.curator.utils.ThreadUtils;
 import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.KeeperException;
@@ -50,7 +51,6 @@ import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
-import org.apache.curator.utils.PathUtils;
 
 /**
  * <p>A utility that attempts to keep all data from all children of a ZK path locally
cached. This class
@@ -65,6 +65,106 @@ public class TreeCache implements Closeable
 {
     private static final Logger LOG = LoggerFactory.getLogger(TreeCache.class);
 
+    public static final class Builder {
+        private final CuratorFramework client;
+        private final String path;
+        private boolean cacheData = true;
+        private boolean dataIsCompressed = false;
+        private CloseableExecutorService executorService = null;
+
+        private Builder(CuratorFramework client, String path) {
+            this.client = client;
+            this.path = path;
+        }
+
+        /**
+         * Builds the {@link TreeCache} based on configured values.
+         */
+        public TreeCache build()
+        {
+            CloseableExecutorService executor = executorService;
+            if ( executor == null )
+            {
+                executor = new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory));
+            }
+            return new TreeCache(client, path, cacheData, dataIsCompressed, executor);
+        }
+
+        /**
+         * Builds the {@link TreeCache} based on configured values, and starts it.
+         */
+        public TreeCache buildAndStart() throws Exception
+        {
+            TreeCache treeCache = build();
+            treeCache.start();
+            return treeCache;
+        }
+
+        /**
+         * Sets whether or not to cache byte data per node; default {@code true}.
+         */
+        public Builder setCacheData(boolean cacheData)
+        {
+            this.cacheData = cacheData;
+            return this;
+        }
+
+        /**
+         * Sets whether or to decompress node data; default {@code false}.
+         */
+        public Builder setDataIsCompressed(boolean dataIsCompressed)
+        {
+            this.dataIsCompressed = dataIsCompressed;
+            return this;
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not
specified.
+         */
+        public Builder setExecutor(ThreadFactory threadFactory)
+        {
+            return setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory)));
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not
specified.
+         */
+        public Builder setExecutor(ExecutorService executorService)
+        {
+            if (executorService instanceof CloseableExecutorService) {
+                return setExecutor((CloseableExecutorService) executorService);
+            } else {
+                return setExecutor(new CloseableExecutorService(executorService));
+            }
+        }
+
+        /**
+         * Sets the executor to publish events; a default executor will be created if not
specified.
+         */
+        public Builder setExecutor(CloseableExecutorService executorService)
+        {
+            this.executorService = executorService;
+            return this;
+        }
+    }
+
+    /**
+     * Create a TreeCache builder for the given client and path to configure advanced options.
+     *
+     * If the client is namespaced, all operations on the resulting TreeCache will be in
terms of
+     * the namespace, including all published events.  The given path is the root at which
the
+     * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache
will
+     * be initially empty.
+     *
+     * @param client the client to use; may be namespaced
+     * @param path the path to the root node to watch/explore; this path need not actually
exist on
+     *             the server
+     * @return a new builder
+     */
+    public static Builder newBuilder(CuratorFramework client, String path) {
+        return new Builder(client, path);
+    }
+
     private enum NodeState
     {
         PENDING, LIVE, DEAD
@@ -356,51 +456,24 @@ public class TreeCache implements Closeable
         }
     };
 
-    private static final ThreadFactory defaultThreadFactory = ThreadUtils.newThreadFactory("TreeCache");
-
-    /**
-     * @param client    the client
-     * @param path      path to watch
-     * @param cacheData if true, node contents are cached in addition to the stat
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData)
-    {
-        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory),
true));
-    }
-
-    /**
-     * @param client        the client
-     * @param path          path to watch
-     * @param cacheData     if true, node contents are cached in addition to the stat
-     * @param threadFactory factory to use when creating internal threads
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, ThreadFactory
threadFactory)
-    {
-        this(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory),
true));
-    }
+    static final ThreadFactory defaultThreadFactory = ThreadUtils.newThreadFactory("TreeCache");
 
     /**
-     * @param client           the client
-     * @param path             path to watch
-     * @param cacheData        if true, node contents are cached in addition to the stat
-     * @param dataIsCompressed if true, data in the path is compressed
-     * @param threadFactory    factory to use when creating internal threads
-     */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed,
ThreadFactory threadFactory)
-    {
-        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(Executors.newSingleThreadExecutor(threadFactory),
true));
-    }
-
-    /**
-     * @param client           the client
-     * @param path             path to watch
-     * @param cacheData        if true, node contents are cached in addition to the stat
-     * @param dataIsCompressed if true, data in the path is compressed
-     * @param executorService  ExecutorService to use for the TreeCache's background thread
+     * Create a TreeCache for the given client and path with default options.
+     *
+     * If the client is namespaced, all operations on the resulting TreeCache will be in
terms of
+     * the namespace, including all published events.  The given path is the root at which
the
+     * TreeCache will watch and explore.  If no node exists at the given path, the TreeCache
will
+     * be initially empty.
+     *
+     * @see #newBuilder(CuratorFramework, String)
+     * @param client the client to use; may be namespaced
+     * @param path the path to the root node to watch/explore; this path need not actually
exist on
+     *             the server
      */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed,
final ExecutorService executorService)
+    public TreeCache(CuratorFramework client, String path)
     {
-        this(client, path, cacheData, dataIsCompressed, new CloseableExecutorService(executorService));
+        this(client, path, true, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(defaultThreadFactory),
true));
     }
 
     /**
@@ -410,7 +483,7 @@ public class TreeCache implements Closeable
      * @param dataIsCompressed if true, data in the path is compressed
      * @param executorService  Closeable ExecutorService to use for the TreeCache's background
thread
      */
-    public TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed,
final CloseableExecutorService executorService)
+    TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed,
final CloseableExecutorService executorService)
     {
         this.root = new TreeNode(PathUtils.validatePath(path), null);
         this.client = client;

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
index eb903b5..62edf34 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
@@ -25,11 +25,13 @@ import org.apache.curator.framework.api.UnhandledErrorListener;
 import org.apache.curator.retry.RetryOneTime;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.test.Timing;
+import org.apache.curator.utils.CloseableExecutorService;
 import org.apache.curator.utils.CloseableUtils;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -62,9 +64,14 @@ public class BaseTestTreeCache extends BaseClassForTests
     class MyTreeCache extends TreeCache
     {
 
+        MyTreeCache(CuratorFramework client, String path)
+        {
+            this(client, path, true);
+        }
+
         MyTreeCache(CuratorFramework client, String path, boolean cacheData)
         {
-            super(client, path, cacheData);
+            super(client, path, cacheData, false, new CloseableExecutorService(Executors.newSingleThreadExecutor(TreeCache.defaultThreadFactory),
true));
             getListenable().addListener(eventListener);
         }
 

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
index abdbb8b..4528751 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
@@ -39,7 +39,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/3", "three".getBytes());
         client.create().forPath("/test/2/sub", "two-sub".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1", "one".getBytes());
@@ -58,7 +58,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testStartEmpty() throws Exception
     {
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -73,7 +73,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -87,7 +87,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client, "/", true);
+        cache = new MyTreeCache(client, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
@@ -109,7 +109,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/test", true);
+        cache = new MyTreeCache(client.usingNamespace("outer"), "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/one");
@@ -129,7 +129,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/outer/test");
         client.create().forPath("/outer/test/one", "hey there".getBytes());
 
-        cache = new MyTreeCache(client.usingNamespace("outer"), "/", true);
+        cache = new MyTreeCache(client.usingNamespace("outer"), "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/foo");
@@ -147,7 +147,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testSyncInitialPopulation() throws Exception
     {
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
 
@@ -166,7 +166,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test/2", "2".getBytes());
         client.create().forPath("/test/3", "3".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/1");
@@ -200,7 +200,7 @@ public class TestTreeCache extends BaseTestTreeCache
         client.create().forPath("/test");
         client.create().forPath("/test/foo", "one".getBytes());
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/foo");
@@ -219,7 +219,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -243,7 +243,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -269,7 +269,7 @@ public class TestTreeCache extends BaseTestTreeCache
     @Test
     public void testBasicsOnTwoCaches() throws Exception
     {
-        MyTreeCache cache2 = new MyTreeCache(client, "/test", true);
+        MyTreeCache cache2 = new MyTreeCache(client, "/test");
         cache2.getListenable().removeListener(eventListener);  // Don't listen on the second
cache.
 
         // Just ensures the same event count; enables test flow control on cache2.
@@ -287,7 +287,7 @@ public class TestTreeCache extends BaseTestTreeCache
         {
             client.create().forPath("/test");
 
-            cache = new MyTreeCache(client, "/test", true);
+            cache = new MyTreeCache(client, "/test");
             cache.start();
             cache2.start();
 
@@ -327,7 +327,7 @@ public class TestTreeCache extends BaseTestTreeCache
     {
         client.create().forPath("/test");
 
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);
@@ -357,7 +357,7 @@ public class TestTreeCache extends BaseTestTreeCache
         initCuratorFramework();
 
         // Start the client disconnected.
-        cache = new MyTreeCache(client, "/test", true);
+        cache = new MyTreeCache(client, "/test");
         cache.start();
         assertNoMoreEvents();
 

http://git-wip-us.apache.org/repos/asf/curator/blob/a8ff472c/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
index 304bfe3..0709330 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCacheRandomTree.java
@@ -63,7 +63,7 @@ public class TestTreeCacheRandomTree extends BaseTestTreeCache
     {
         client.create().forPath("/tree", null);
         CuratorFramework cl = client.usingNamespace("tree");
-        cache = new MyTreeCache(cl, "/", true);
+        cache = new MyTreeCache(cl, "/");
         cache.start();
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/");
         assertEvent(TreeCacheEvent.Type.INITIALIZED);


Mime
View raw message