You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2016/12/31 21:41:20 UTC
[2/5] curator git commit: Added a composite cache
Added a composite cache
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/313fd7d4
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/313fd7d4
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/313fd7d4
Branch: refs/heads/persistent-watch
Commit: 313fd7d46ccede6bbc9ac1feb0b5a2099fce7a6d
Parents: 5b0a9f5
Author: randgalt <ra...@apache.org>
Authored: Fri Dec 30 15:12:04 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Fri Dec 30 15:12:04 2016 -0500
----------------------------------------------------------------------
.../framework/recipes/watch/CacheFilters.java | 18 ++
.../recipes/watch/CompositeCountDownLatch.java | 82 ++++++
.../recipes/watch/CompositeCuratorCache.java | 282 +++++++++++++++++++
.../watch/CompositeCuratorCacheProxy.java | 53 ++++
.../framework/recipes/watch/CuratorCache.java | 11 +-
.../recipes/watch/CuratorCacheBase.java | 5 +-
.../recipes/watch/CuratorCacheBuilder.java | 8 +
.../recipes/watch/InternalCuratorCache.java | 4 +-
.../framework/recipes/watch/RefreshFilters.java | 18 ++
9 files changed, 468 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CacheFilters.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CacheFilters.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CacheFilters.java
index 171dea4..6d7a119 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CacheFilters.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CacheFilters.java
@@ -1,3 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
package org.apache.curator.framework.recipes.watch;
public class CacheFilters
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCountDownLatch.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCountDownLatch.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCountDownLatch.java
new file mode 100644
index 0000000..5c75622
--- /dev/null
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCountDownLatch.java
@@ -0,0 +1,82 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.recipes.watch;
+
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+class CompositeCountDownLatch extends CountDownLatch
+{
+ private final Collection<CountDownLatch> latches;
+
+ CompositeCountDownLatch(Collection<CountDownLatch> latches)
+ {
+ super(0);
+ this.latches = ImmutableList.copyOf(latches);
+ }
+
+ @Override
+ public void await() throws InterruptedException
+ {
+ for ( CountDownLatch latch : latches )
+ {
+ latch.await();
+ }
+ }
+
+ @Override
+ public boolean await(final long timeout, TimeUnit unit) throws InterruptedException
+ {
+ long timeoutMs = unit.toMillis(timeout);
+ for ( CountDownLatch latch : latches )
+ {
+ if ( timeoutMs < 0 )
+ {
+ return false;
+ }
+ long start = System.currentTimeMillis();
+ if ( !latch.await(timeoutMs, TimeUnit.MILLISECONDS) )
+ {
+ return false;
+ }
+ long elapsed = System.currentTimeMillis() - start;
+ timeoutMs -= elapsed;
+ }
+ return true;
+ }
+
+ @Override
+ public void countDown()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public long getCount()
+ {
+ long count = 0;
+ for ( CountDownLatch latch : latches )
+ {
+ count += latch.getCount();
+ }
+ return count;
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCache.java
new file mode 100644
index 0000000..c0163c5
--- /dev/null
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCache.java
@@ -0,0 +1,282 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.recipes.watch;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import org.apache.curator.framework.listen.Listenable;
+import org.apache.curator.framework.listen.ListenerContainer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+
+public class CompositeCuratorCache implements CuratorCache
+{
+ private final Map<String, CuratorCache> caches;
+ private final ListenerContainer<CacheListener> listeners = new ListenerContainer<>();
+ private final CacheListener listener = new CacheListener()
+ {
+ @Override
+ public void process(final CacheEvent event, final String path, final CachedNode affectedNode)
+ {
+ Function<CacheListener, Void> proc = new Function<CacheListener, Void>()
+ {
+ @Override
+ public Void apply(CacheListener listener)
+ {
+ listener.process(event, path, affectedNode);
+ return null;
+ }
+ };
+ listeners.forEach(proc);
+ }
+ };
+
+ public CompositeCuratorCache(CuratorCache... caches)
+ {
+ this(toMap(Arrays.asList(caches)));
+ }
+
+ public CompositeCuratorCache(Collection<CuratorCache> caches)
+ {
+ this(toMap(caches));
+ }
+
+ private static Map<String, CuratorCache> toMap(Collection<CuratorCache> caches)
+ {
+ Map<String, CuratorCache> map = new HashMap<>();
+ for ( CuratorCache cache : caches )
+ {
+ map.put(Integer.toString(map.size()), cache);
+ }
+ return map;
+ }
+
+ public CompositeCuratorCache(Map<String, CuratorCache> caches)
+ {
+ this.caches = ImmutableMap.copyOf(caches);
+ }
+
+ public Iterable<String> cacheKeys()
+ {
+ return caches.keySet();
+ }
+
+ public CuratorCache getCache(String key)
+ {
+ return caches.get(key);
+ }
+
+ @Override
+ public CountDownLatch start()
+ {
+ List<CountDownLatch> latches = new ArrayList<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ latches.add(cache.start());
+ }
+ return new CompositeCountDownLatch(latches);
+ }
+
+ @Override
+ public void close()
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ cache.getListenable().removeListener(listener);
+ cache.close();
+ }
+ }
+
+ @Override
+ public Listenable<CacheListener> getListenable()
+ {
+ return listeners;
+ }
+
+ @Override
+ public CountDownLatch refreshAll()
+ {
+ List<CountDownLatch> latches = new ArrayList<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ latches.add(cache.refreshAll());
+ }
+ return new CompositeCountDownLatch(latches);
+ }
+
+ @Override
+ public CountDownLatch refresh(String path)
+ {
+ List<CountDownLatch> latches = new ArrayList<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ latches.add(cache.refresh(path));
+ }
+ return new CompositeCountDownLatch(latches);
+ }
+
+ @Override
+ public boolean clear(String path)
+ {
+ boolean cleared = false;
+ for ( CuratorCache cache : caches.values() )
+ {
+ if ( cache.clear(path) )
+ {
+ cleared = true;
+ }
+ }
+ return cleared;
+ }
+
+ @Override
+ public void clearAll()
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ cache.clearAll();
+ }
+ }
+
+ @Override
+ public boolean exists(String path)
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ if ( cache.exists(path) )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public Set<String> paths()
+ {
+ Set<String> paths = new HashSet<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ cache.clearAll();
+ }
+ return paths;
+ }
+
+ @Override
+ public CachedNode get(String path)
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ CachedNode node = cache.get(path);
+ if ( node != null )
+ {
+ return node;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Iterable<CachedNode> getAll()
+ {
+ List<Iterable<CachedNode>> nodes = new ArrayList<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ nodes.add(cache.getAll());
+ }
+ return Iterables.concat(nodes);
+ }
+
+ @Override
+ public Iterable<Map.Entry<String, CachedNode>> entries()
+ {
+ List<Iterable<Map.Entry<String, CachedNode>>> nodes = new ArrayList<>();
+ for ( CuratorCache cache : caches.values() )
+ {
+ nodes.add(cache.entries());
+ }
+ return Iterables.concat(nodes);
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ if ( !cache.isEmpty() )
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public int size()
+ {
+ long size = 0;
+ for ( CuratorCache cache : caches.values() )
+ {
+ size += cache.size();
+ }
+ return (int)Math.min(size, Integer.MAX_VALUE);
+ }
+
+ @Override
+ public void clearDataBytes(String path)
+ {
+ for ( CuratorCache cache : caches.values() )
+ {
+ cache.clearDataBytes(path);
+ }
+ }
+
+ @Override
+ public boolean clearDataBytes(String path, int ifVersion)
+ {
+ boolean cleared = false;
+ for ( CuratorCache cache : caches.values() )
+ {
+ if ( cache.clearDataBytes(path, ifVersion) )
+ {
+ cleared = true;
+ }
+ }
+ return cleared;
+ }
+
+ @Override
+ public long refreshCount()
+ {
+ long count = 0;
+ for ( CuratorCache cache : caches.values() )
+ {
+ count = Math.min(count, cache.refreshCount());
+ }
+ return count;
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCacheProxy.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCacheProxy.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCacheProxy.java
new file mode 100644
index 0000000..17ca449
--- /dev/null
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CompositeCuratorCacheProxy.java
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.recipes.watch;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+
+public class CompositeCuratorCacheProxy extends CompositeCuratorCache
+{
+ public CompositeCuratorCacheProxy(CuratorCache... caches)
+ {
+ super(caches);
+ }
+
+ public CompositeCuratorCacheProxy(Collection<CuratorCache> caches)
+ {
+ super(caches);
+ }
+
+ public CompositeCuratorCacheProxy(Map<String, CuratorCache> caches)
+ {
+ super(caches);
+ }
+
+ @Override
+ public CountDownLatch start()
+ {
+ return new CountDownLatch(0);
+ }
+
+ @Override
+ public void close()
+ {
+ // NOP
+ }
+}
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCache.java
index c8a6ea2..164ce29 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCache.java
@@ -20,7 +20,6 @@ package org.apache.curator.framework.recipes.watch;
import org.apache.curator.framework.listen.Listenable;
import java.io.Closeable;
-import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -104,20 +103,18 @@ public interface CuratorCache extends Closeable
CachedNode get(String path);
/**
- * Returns the collection of node values in the cache. The returned set behaves in the same manner
- * as {@link ConcurrentHashMap#values()}
+ * Returns the collection of node values in the cache.
*
* @return node values
*/
- Collection<CachedNode> getAll();
+ Iterable<CachedNode> getAll();
/**
- * Returns the collection of node entries in the cache. The returned set behaves in the same manner
- * as {@link ConcurrentHashMap#entrySet()}
+ * Returns the collection of node entries in the cache.
*
* @return node entries
*/
- Set<Map.Entry<String, CachedNode>> entries();
+ Iterable<Map.Entry<String, CachedNode>> entries();
/**
* Returns true if the cache is currently empty. Use the result only as a reference. Concurrent
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBase.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBase.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBase.java
index 4803e69..3075c3b 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBase.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBase.java
@@ -23,7 +23,6 @@ import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import org.apache.curator.framework.listen.Listenable;
import org.apache.curator.framework.listen.ListenerContainer;
-import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -107,13 +106,13 @@ abstract class CuratorCacheBase implements CuratorCache
}
@Override
- public final Collection<CachedNode> getAll()
+ public final Iterable<CachedNode> getAll()
{
return cache.asMap().values();
}
@Override
- public final Set<Map.Entry<String, CachedNode>> entries()
+ public final Iterable<Map.Entry<String, CachedNode>> entries()
{
return cache.asMap().entrySet();
}
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBuilder.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBuilder.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBuilder.java
index 341ac23..df068c0 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBuilder.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/CuratorCacheBuilder.java
@@ -75,6 +75,14 @@ public class CuratorCacheBuilder
return this;
}
+ public CuratorCacheBuilder forFull(CacheAction cacheAction)
+ {
+ singleNode = false;
+ refreshFilter = RefreshFilters.tree();
+ cacheFilter = CacheFilters.full(cacheAction);
+ return this;
+ }
+
public CuratorCacheBuilder usingWeakValues()
{
cacheBuilder = cacheBuilder.weakValues();
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/InternalCuratorCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/InternalCuratorCache.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/InternalCuratorCache.java
index 303ebb1..950c336 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/InternalCuratorCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/InternalCuratorCache.java
@@ -124,9 +124,7 @@ class InternalCuratorCache extends CuratorCacheBase implements Watcher
@Override
public CountDownLatch refresh(String path)
{
- Preconditions.checkArgument(path.startsWith(basePath), "Path is not this cache's tree: " + path);
-
- if ( isStarted() )
+ if ( isStarted() && path.startsWith(basePath) )
{
CountDownLatch latch = new CountDownLatch(1);
Refresher refresher = new Refresher(this, path, latch);
http://git-wip-us.apache.org/repos/asf/curator/blob/313fd7d4/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/RefreshFilters.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/RefreshFilters.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/RefreshFilters.java
index c1581c4..9ab1ca7 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/RefreshFilters.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/watch/RefreshFilters.java
@@ -1,3 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
package org.apache.curator.framework.recipes.watch;
public class RefreshFilters