curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [30/32] git commit: wip
Date Mon, 22 Jul 2013 23:27:05 GMT
wip


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

Branch: refs/heads/CURATOR-14
Commit: 752b723fb3c45926775dd257d44ea6ab4dd8277a
Parents: 9b81582
Author: randgalt <randgalt@apache.org>
Authored: Mon Jul 15 14:40:08 2013 -0700
Committer: randgalt <randgalt@apache.org>
Committed: Mon Jul 15 14:40:08 2013 -0700

----------------------------------------------------------------------
 .../framework/imps/CuratorFrameworkImpl.java    |  8 +-
 .../framework/imps/ExistsBuilderImpl.java       |  4 +-
 .../framework/imps/GetChildrenBuilderImpl.java  |  4 +-
 .../framework/imps/GetDataBuilderImpl.java      |  4 +-
 .../framework/imps/NamespaceWatcher.java        | 25 ++++++
 .../framework/imps/NamespaceWatcherMap.java     | 90 --------------------
 .../apache/curator/framework/imps/Watching.java | 26 ++++--
 .../framework/imps/TestWatcherIdentity.java     | 68 ---------------
 8 files changed, 55 insertions(+), 174 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
index c3b0992..ed06e3d 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorFrameworkImpl.java
@@ -67,7 +67,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
     private final CompressionProvider                                   compressionProvider;
     private final ACLProvider                                           aclProvider;
     private final NamespaceFacadeCache                                  namespaceFacadeCache;
-    private final NamespaceWatcherMap                                   namespaceWatcherMap
= new NamespaceWatcherMap(this);
+    private final DispatchingWatcher                                    dispatchingWatcher
= new DispatchingWatcher();
 
     private volatile ExecutorService                                    executorService;
 
@@ -282,7 +282,7 @@ public class CuratorFrameworkImpl implements CuratorFramework
             unhandledErrorListeners.clear();
             connectionStateManager.close();
             client.close();
-            namespaceWatcherMap.close();
+            dispatchingWatcher.close();
             if ( executorService != null )
             {
                 executorService.shutdownNow();
@@ -576,9 +576,9 @@ public class CuratorFrameworkImpl implements CuratorFramework
         return namespaceFacadeCache;
     }
 
-    NamespaceWatcherMap getNamespaceWatcherMap()
+    DispatchingWatcher getDispatchingWatcher()
     {
-        return namespaceWatcherMap;
+        return dispatchingWatcher;
     }
 
     private <DATA_TYPE> void sendToBackgroundCallback(OperationAndData<DATA_TYPE>
operationAndData, CuratorEvent event)

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
index be79d58..00c8b65 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
@@ -129,7 +129,7 @@ class ExistsBuilderImpl implements ExistsBuilder, BackgroundOperation<String>
         }
         else
         {
-            client.getZooKeeper().exists(operationAndData.getData(), watching.getWatcher(),
callback, backgrounding.getContext());
+            client.getZooKeeper().exists(operationAndData.getData(), watching.getWatcher(operationAndData.getData()),
callback, backgrounding.getContext());
         }
     }
 
@@ -169,7 +169,7 @@ class ExistsBuilderImpl implements ExistsBuilder, BackgroundOperation<String>
                     }
                     else
                     {
-                        returnStat = client.getZooKeeper().exists(path, watching.getWatcher());
+                        returnStat = client.getZooKeeper().exists(path, watching.getWatcher(path));
                     }
                     return returnStat;
                 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
index 5ea6190..a173288 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
@@ -172,7 +172,7 @@ class GetChildrenBuilderImpl implements GetChildrenBuilder, BackgroundOperation<
         }
         else
         {
-            client.getZooKeeper().getChildren(operationAndData.getData(), watching.getWatcher(),
callback, backgrounding.getContext());
+            client.getZooKeeper().getChildren(operationAndData.getData(), watching.getWatcher(operationAndData.getData()),
callback, backgrounding.getContext());
         }
     }
 
@@ -211,7 +211,7 @@ class GetChildrenBuilderImpl implements GetChildrenBuilder, BackgroundOperation<
                     }
                     else
                     {
-                        children = client.getZooKeeper().getChildren(path, watching.getWatcher(),
responseStat);
+                        children = client.getZooKeeper().getChildren(path, watching.getWatcher(path),
responseStat);
                     }
                     return children;
                 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
index d9b3907..4a4644c 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
@@ -260,7 +260,7 @@ class GetDataBuilderImpl implements GetDataBuilder, BackgroundOperation<String>
         }
         else
         {
-            client.getZooKeeper().getData(operationAndData.getData(), watching.getWatcher(),
callback, backgrounding.getContext());
+            client.getZooKeeper().getData(operationAndData.getData(), watching.getWatcher(operationAndData.getData()),
callback, backgrounding.getContext());
         }
     }
 
@@ -299,7 +299,7 @@ class GetDataBuilderImpl implements GetDataBuilder, BackgroundOperation<String>
                     }
                     else
                     {
-                        responseData = client.getZooKeeper().getData(path, watching.getWatcher(),
responseStat);
+                        responseData = client.getZooKeeper().getData(path, watching.getWatcher(path),
responseStat);
                     }
                     return responseData;
                 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcher.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcher.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcher.java
index 7a2e4c3..43dfbea 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcher.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcher.java
@@ -61,4 +61,29 @@ class NamespaceWatcher implements Watcher
             }
         }
     }
+
+    @SuppressWarnings("RedundantIfStatement")
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        NamespaceWatcher that = (NamespaceWatcher) o;
+
+        if (actualWatcher != null ? !actualWatcher.equals(that.actualWatcher) : that.actualWatcher
!= null)
+            return false;
+        if (client != null ? !client.equals(that.client) : that.client != null) return false;
+        if (curatorWatcher != null ? !curatorWatcher.equals(that.curatorWatcher) : that.curatorWatcher
!= null)
+            return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = client != null ? client.hashCode() : 0;
+        result = 31 * result + (actualWatcher != null ? actualWatcher.hashCode() : 0);
+        result = 31 * result + (curatorWatcher != null ? curatorWatcher.hashCode() : 0);
+        return result;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcherMap.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcherMap.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcherMap.java
deleted file mode 100644
index 962036b..0000000
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/NamespaceWatcherMap.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/**
- * 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.imps;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.MapMaker;
-import org.apache.curator.framework.api.CuratorWatcher;
-import org.apache.zookeeper.Watcher;
-import java.io.Closeable;
-import java.lang.reflect.Field;
-import java.util.concurrent.ConcurrentMap;
-
-class NamespaceWatcherMap implements Closeable
-{
-    private final ConcurrentMap<Object, NamespaceWatcher> map = new MapMaker()
-        .weakValues()
-        .makeMap();
-    private final CuratorFrameworkImpl client;
-
-    NamespaceWatcherMap(CuratorFrameworkImpl client)
-    {
-        this.client = client;
-    }
-
-    @Override
-    public void close()
-    {
-        map.clear();
-    }
-
-    @VisibleForTesting
-    void drain() throws Exception
-    {
-        Runtime.getRuntime().gc();
-
-        // relies on internals of MapMakerInternalMap (obviously)
-        Class mapMakerInternalMapClass = Class.forName("com.google.common.collect.MapMakerInternalMap");
-        Field drainThresholdField = mapMakerInternalMapClass.getDeclaredField("DRAIN_THRESHOLD");
-        drainThresholdField.setAccessible(true);
-        int drainThreshold = drainThresholdField.getInt(null) + 1;
-        while ( drainThreshold-- > 0 )
-        {
-            map.get(new Object());
-        }
-    }
-
-    @VisibleForTesting
-    NamespaceWatcher get(Object key)
-    {
-        return map.get(key);
-    }
-
-    @VisibleForTesting
-    boolean isEmpty()
-    {
-        return map.isEmpty();
-    }
-
-    NamespaceWatcher    getNamespaceWatcher(Watcher watcher)
-    {
-        return get(watcher, new NamespaceWatcher(client, watcher));
-    }
-
-    NamespaceWatcher    getNamespaceWatcher(CuratorWatcher watcher)
-    {
-        return get(watcher, new NamespaceWatcher(client, watcher));
-    }
-
-    private NamespaceWatcher    get(Object watcher, NamespaceWatcher newNamespaceWatcher)
-    {
-        NamespaceWatcher        existingNamespaceWatcher = map.putIfAbsent(watcher, newNamespaceWatcher);
-        return (existingNamespaceWatcher != null) ? existingNamespaceWatcher : newNamespaceWatcher;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
index a9d0ab1..b320b92 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.curator.framework.imps;
 
 import org.apache.curator.framework.api.CuratorWatcher;
@@ -23,35 +24,48 @@ import org.apache.zookeeper.Watcher;
 
 class Watching
 {
-    private final Watcher       watcher;
-    private final boolean       watched;
+    private final CuratorFrameworkImpl client;
+    private final NamespaceWatcher watcher;
+    private final boolean watched;
 
     Watching(boolean watched)
     {
+        this.client = null;
         this.watcher = null;
         this.watched = watched;
     }
 
     Watching(CuratorFrameworkImpl client, Watcher watcher)
     {
-        this.watcher = (watcher != null) ? client.getNamespaceWatcherMap().getNamespaceWatcher(watcher)
: null;
+        this.client = client;
+        this.watcher = new NamespaceWatcher(client, watcher);
         this.watched = false;
     }
 
-    Watching(CuratorFrameworkImpl client, CuratorWatcher watcher)
+    Watching(CuratorFrameworkImpl client, CuratorWatcher curatorWatcher)
     {
-        this.watcher = (watcher != null) ? client.getNamespaceWatcherMap().getNamespaceWatcher(watcher)
: null;
+        this.client = client;
+        this.watcher = new NamespaceWatcher(client, curatorWatcher);
         this.watched = false;
     }
 
     Watching()
     {
+        client = null;
         watcher = null;
         watched = false;
     }
 
-    Watcher getWatcher()
+    Watcher getWatcher(String path)
     {
+        if ( client == null )
+        {
+            return null;
+        }
+        else if ( watcher != null )
+        {
+            return client.getDispatchingWatcher().addNamespaceWatcher(path, watcher);
+        }
         return watcher;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/752b723f/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
index 9441a51..38fd0b5 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestWatcherIdentity.java
@@ -62,74 +62,6 @@ public class TestWatcherIdentity extends BaseClassForTests
     }
 
     @Test
-    public void testRefExpiration() throws Exception
-    {
-        final int MAX_CHECKS = 10;
-
-        final CuratorFrameworkImpl client = (CuratorFrameworkImpl)CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));
-        try
-        {
-            Assert.assertNull(client.getNamespaceWatcherMap().get(new CountCuratorWatcher()));
-
-            final CountDownLatch latch = new CountDownLatch(1);
-            ExecutorService service = Executors.newSingleThreadExecutor();
-            service.submit
-            (
-                new Callable<Void>()
-                {
-                    @Override
-                    public Void call() throws Exception
-                    {
-                        CountZKWatcher watcher = new CountZKWatcher();
-                        client.getNamespaceWatcherMap().getNamespaceWatcher(watcher);
-                        Assert.assertNotNull(client.getNamespaceWatcherMap().get(watcher));
-                        latch.countDown();
-                        return null;
-                    }
-                }
-            );
-
-            latch.await();
-            service.shutdownNow();
-
-            Timing timing = new Timing();
-            for ( int i = 0; i < MAX_CHECKS; ++i )
-            {
-                Assert.assertTrue((i + 1) < MAX_CHECKS);
-                timing.sleepABit();
-
-                client.getNamespaceWatcherMap().drain();  // just to cause drainReferenceQueues()
to get called
-                if ( client.getNamespaceWatcherMap().isEmpty() )
-                {
-                    break;
-                }
-            }
-        }
-        finally
-        {
-            Closeables.closeQuietly(client);
-        }
-    }
-
-    @Test
-    public void testSimpleId()
-    {
-        CountCuratorWatcher curatorWatcher = new CountCuratorWatcher();
-        CountZKWatcher zkWatcher = new CountZKWatcher();
-        CuratorFrameworkImpl client = (CuratorFrameworkImpl)CuratorFrameworkFactory.newClient(server.getConnectString(),
new RetryOneTime(1));
-        try
-        {
-            Assert.assertSame(client.getNamespaceWatcherMap().getNamespaceWatcher(curatorWatcher),
client.getNamespaceWatcherMap().getNamespaceWatcher(curatorWatcher));
-            Assert.assertSame(client.getNamespaceWatcherMap().getNamespaceWatcher(zkWatcher),
client.getNamespaceWatcherMap().getNamespaceWatcher(zkWatcher));
-            Assert.assertNotSame(client.getNamespaceWatcherMap().getNamespaceWatcher(curatorWatcher),
client.getNamespaceWatcherMap().getNamespaceWatcher(zkWatcher));
-        }
-        finally
-        {
-            Closeables.closeQuietly(client);
-        }
-    }
-
-    @Test
     public void testCuratorWatcher() throws Exception
     {
         Timing timing = new Timing();


Mime
View raw message