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 2015/08/28 20:40:13 UTC

[1/2] curator git commit: Switch to new injectSessionExpiration()

Repository: curator
Updated Branches:
  refs/heads/CURATOR-253 [created] 145da217f


Switch to new injectSessionExpiration()


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

Branch: refs/heads/CURATOR-253
Commit: d1b4cbf070046ace5c047cc46d99c0ae71f749a5
Parents: 0f5668b
Author: randgalt <ra...@apache.org>
Authored: Fri Aug 28 13:39:22 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Fri Aug 28 13:39:22 2015 -0500

----------------------------------------------------------------------
 .../curator/TestSessionFailRetryLoop.java       |  9 +-
 .../framework/recipes/cache/TestTreeCache.java  |  1 -
 .../org/apache/curator/test/KillSession.java    | 87 ++++----------------
 3 files changed, 21 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
----------------------------------------------------------------------
diff --git a/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java b/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
index c17b5bf..937726e 100644
--- a/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
+++ b/curator-client/src/test/java/org/apache/curator/TestSessionFailRetryLoop.java
@@ -18,6 +18,7 @@
  */
 package org.apache.curator;
 
+import org.apache.curator.retry.ExponentialBackoffRetry;
 import org.apache.curator.test.BaseClassForTests;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.curator.retry.RetryOneTime;
@@ -34,7 +35,7 @@ public class TestSessionFailRetryLoop extends BaseClassForTests
     public void     testRetry() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
         retryLoop.start();
         try
@@ -103,7 +104,7 @@ public class TestSessionFailRetryLoop extends BaseClassForTests
     public void     testRetryStatic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.RETRY);
         retryLoop.start();
         try
@@ -175,7 +176,7 @@ public class TestSessionFailRetryLoop extends BaseClassForTests
     public void     testBasic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
         retryLoop.start();
         try
@@ -230,7 +231,7 @@ public class TestSessionFailRetryLoop extends BaseClassForTests
     public void     testBasicStatic() throws Exception
     {
         Timing                          timing = new Timing();
-        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
+        final CuratorZookeeperClient    client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new ExponentialBackoffRetry(100, 3));
         SessionFailRetryLoop            retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
         retryLoop.start();
         try

http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/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 0bccb54..151ea7e 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
@@ -377,7 +377,6 @@ public class TestTreeCache extends BaseTestTreeCache
         assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test/me");
 
         KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
-        assertEvent(TreeCacheEvent.Type.CONNECTION_SUSPENDED);
         assertEvent(TreeCacheEvent.Type.CONNECTION_LOST);
         assertEvent(TreeCacheEvent.Type.CONNECTION_RECONNECTED);
         assertEvent(TreeCacheEvent.Type.NODE_REMOVED, "/test/me");

http://git-wip-us.apache.org/repos/asf/curator/blob/d1b4cbf0/curator-test/src/main/java/org/apache/curator/test/KillSession.java
----------------------------------------------------------------------
diff --git a/curator-test/src/main/java/org/apache/curator/test/KillSession.java b/curator-test/src/main/java/org/apache/curator/test/KillSession.java
index 63b7f2a..ce2b7e6 100644
--- a/curator-test/src/main/java/org/apache/curator/test/KillSession.java
+++ b/curator-test/src/main/java/org/apache/curator/test/KillSession.java
@@ -18,24 +18,12 @@
  */
 package org.apache.curator.test;
 
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
 
 /**
  * <p>
- *     Utility to simulate a ZK session dying. See: <a href="http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4">ZooKeeper FAQ</a>
+ *     Utility to simulate a ZK session dying.
  * </p>
- *
- * <blockquote>
- *     In the case of testing we want to cause a problem, so to explicitly expire a session an
- *     application connects to ZooKeeper, saves the session id and password, creates another
- *     ZooKeeper handle with that id and password, and then closes the new handle. Since both
- *     handles reference the same session, the close on second handle will invalidate the session
- *     causing a SESSION_EXPIRED on the first handle.
- * </blockquote>
  */
 public class KillSession
 {
@@ -43,12 +31,24 @@ public class KillSession
      * Kill the given ZK session
      *
      * @param client the client to kill
+     * @since 3.0.0
+     */
+    public static void     kill(ZooKeeper client)
+    {
+        client.getTestable().injectSessionExpiration();
+    }
+
+    /**
+     * Kill the given ZK session
+     *
+     * @param client the client to kill
      * @param connectString server connection string
      * @throws Exception errors
+     * @deprecated use {@link #kill(ZooKeeper)} instead
      */
     public static void     kill(ZooKeeper client, String connectString) throws Exception
     {
-        kill(client, connectString, new Timing().forWaiting().milliseconds());
+        kill(client);
     }
 
     /**
@@ -58,65 +58,10 @@ public class KillSession
      * @param connectString server connection string
      * @param maxMs max time ms to wait for kill
      * @throws Exception errors
+     * @deprecated use {@link #kill(ZooKeeper)} instead
      */
     public static void     kill(ZooKeeper client, String connectString, int maxMs) throws Exception
     {
-        long                    startTicks = System.currentTimeMillis();
-
-        final CountDownLatch    sessionLostLatch = new CountDownLatch(1);
-        Watcher                 sessionLostWatch = new Watcher()
-        {
-            @Override
-            public void process(WatchedEvent event)
-            {
-                sessionLostLatch.countDown();
-            }
-        };
-        client.exists("/___CURATOR_KILL_SESSION___" + System.nanoTime(), sessionLostWatch);
-
-        final CountDownLatch    connectionLatch = new CountDownLatch(1);
-        Watcher                 connectionWatcher = new Watcher()
-        {
-            @Override
-            public void process(WatchedEvent event)
-            {
-                if ( event.getState() == Event.KeeperState.SyncConnected )
-                {
-                    connectionLatch.countDown();
-                }
-            }
-        };
-        ZooKeeper zk = new ZooKeeper(connectString, maxMs, connectionWatcher, client.getSessionId(), client.getSessionPasswd());
-        try
-        {
-            if ( !connectionLatch.await(maxMs, TimeUnit.MILLISECONDS) )
-            {
-                throw new Exception("KillSession could not establish duplicate session");
-            }
-            try
-            {
-                zk.close();
-            }
-            finally
-            {
-                zk = null;
-            }
-
-            while ( client.getState().isConnected() && !sessionLostLatch.await(100, TimeUnit.MILLISECONDS) )
-            {
-                long        elapsed = System.currentTimeMillis() - startTicks;
-                if ( elapsed > maxMs )
-                {
-                    throw new Exception("KillSession timed out waiting for session to expire");
-                }
-            }
-        }
-        finally
-        {
-            if ( zk != null )
-            {
-                zk.close();
-            }
-        }
+        kill(client);
     }
 }


[2/2] curator git commit: New injectSessionExpiration() operates much faster than previously. It exposes an assumption in the tests. Added a debug hook to work around

Posted by ra...@apache.org.
New injectSessionExpiration() operates much faster than previously. It exposes an assumption in the tests. Added a debug hook to work around


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

Branch: refs/heads/CURATOR-253
Commit: 145da217ff35df0178823da784f4dd1618851c5e
Parents: d1b4cbf
Author: randgalt <ra...@apache.org>
Authored: Fri Aug 28 13:39:54 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Fri Aug 28 13:39:54 2015 -0500

----------------------------------------------------------------------
 .../recipes/nodes/PersistentEphemeralNode.java     | 16 ++++++++++++++++
 .../recipes/nodes/TestPersistentEphemeralNode.java | 17 +++++++++--------
 2 files changed, 25 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/145da217/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
index 0b482ef..e889bd8 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/PersistentEphemeralNode.java
@@ -19,6 +19,7 @@
 
 package org.apache.curator.framework.recipes.nodes;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 import org.apache.curator.framework.CuratorFramework;
@@ -111,11 +112,26 @@ public class PersistentEphemeralNode implements Closeable
         {
             if ( newState == ConnectionState.RECONNECTED )
             {
+                if ( debugReconnectLatch != null )
+                {
+                    try
+                    {
+                        debugReconnectLatch.await();
+                    }
+                    catch ( InterruptedException e )
+                    {
+                        Thread.currentThread().interrupt();
+                        e.printStackTrace();
+                    }
+                }
                 createNode();
             }
         }
     };
 
+    @VisibleForTesting
+    volatile CountDownLatch debugReconnectLatch = null;
+
     private enum State
     {
         LATENT,

http://git-wip-us.apache.org/repos/asf/curator/blob/145da217/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
index c81cc65..3a0d564 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNode.java
@@ -297,6 +297,7 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
         CuratorFramework observer = newCurator();
 
         PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL, PATH, new byte[0]);
+        node.debugReconnectLatch = new CountDownLatch(1);
         node.start();
         try
         {
@@ -307,10 +308,11 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
             Trigger deletedTrigger = Trigger.deleted();
             observer.checkExists().usingWatcher(deletedTrigger).forPath(node.getActualPath());
 
-            killSession(curator);
+            KillSession.kill(curator.getZookeeperClient().getZooKeeper());
 
             // Make sure the node got deleted
             assertTrue(deletedTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+            node.debugReconnectLatch.countDown();
         }
         finally
         {
@@ -325,6 +327,7 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
         CuratorFramework observer = newCurator();
 
         PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL, PATH, new byte[0]);
+        node.debugReconnectLatch = new CountDownLatch(1);
         node.start();
         try
         {
@@ -334,10 +337,11 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
             Trigger deletedTrigger = Trigger.deleted();
             observer.checkExists().usingWatcher(deletedTrigger).forPath(node.getActualPath());
 
-            killSession(curator);
+            KillSession.kill(curator.getZookeeperClient().getZooKeeper());
 
             // Make sure the node got deleted...
             assertTrue(deletedTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+            node.debugReconnectLatch.countDown();
 
             // Check for it to be recreated...
             Trigger createdTrigger = Trigger.created();
@@ -367,14 +371,16 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
             // We should be able to disconnect multiple times and each time the node should be recreated.
             for ( int i = 0; i < 5; i++ )
             {
+                node.debugReconnectLatch = new CountDownLatch(1);
                 Trigger deletionTrigger = Trigger.deleted();
                 observer.checkExists().usingWatcher(deletionTrigger).forPath(path);
 
                 // Kill the session, thus cleaning up the node...
-                killSession(curator);
+                KillSession.kill(curator.getZookeeperClient().getZooKeeper());
 
                 // Make sure the node ended up getting deleted...
                 assertTrue(deletionTrigger.firedWithin(timing.forWaiting().seconds(), TimeUnit.SECONDS));
+                node.debugReconnectLatch.countDown();
 
                 // Now put a watch in the background looking to see if it gets created...
                 Trigger creationTrigger = Trigger.created();
@@ -634,11 +640,6 @@ public class TestPersistentEphemeralNode extends BaseClassForTests
         return client;
     }
 
-    public void killSession(CuratorFramework curator) throws Exception
-    {
-        KillSession.kill(curator.getZookeeperClient().getZooKeeper(), curator.getZookeeperClient().getCurrentConnectionString());
-    }
-
     private static final class Trigger implements Watcher
     {
         private final Event.EventType type;