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/24 07:21:44 UTC

curator git commit: Instead of closing the ZK instance and setting LOST directly, use the new testing API in 3.5 injectSessionExpiration(). This causes the ZK instance to close and LOST to get posted but has the benefit of sending a session expiration to

Repository: curator
Updated Branches:
  refs/heads/CURATOR-247 dd788163a -> 81bab455c


Instead of closing the ZK instance and setting LOST directly, use the new testing API in 3.5 injectSessionExpiration(). This causes the ZK instance to close and LOST to get posted but has the benefit of sending a session expiration to all watchers


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

Branch: refs/heads/CURATOR-247
Commit: 81bab455cf259f3b45d1fa0f7e26d78127f06d61
Parents: dd78816
Author: randgalt <ra...@apache.org>
Authored: Mon Aug 24 00:21:35 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon Aug 24 00:21:35 2015 -0500

----------------------------------------------------------------------
 .../framework/state/ConnectionStateManager.java | 10 ++++---
 .../imps/TestEnabledSessionExpiredState.java    | 28 ++++++++++++++++++++
 2 files changed, 34 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/81bab455/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java b/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
index de71264..daa33f6 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/state/ConnectionStateManager.java
@@ -296,16 +296,18 @@ public class ConnectionStateManager implements Closeable
             int useSessionTimeoutMs = (lastNegotiatedSessionTimeoutMs > 0) ? lastNegotiatedSessionTimeoutMs : sessionTimeoutMs;
             if ( elapsedMs >= useSessionTimeoutMs )
             {
-                log.warn(String.format("Session timeout has elapsed while SUSPENDED. Posting LOST event and resetting the connection. Elapsed ms: %d. Session Timeout ms: %d", elapsedMs, useSessionTimeoutMs));
+                log.warn(String.format("Session timeout has elapsed while SUSPENDED. Injecting a session expiration. Elapsed ms: %d. Session Timeout ms: %d", elapsedMs, useSessionTimeoutMs));
                 try
                 {
-                    client.getZookeeperClient().reset();
+                    // LOL - this method was proposed by me (JZ) in 2013 for totally unrelated reasons
+                    // it got added to ZK 3.5 and now does exactly what we need
+                    // https://issues.apache.org/jira/browse/ZOOKEEPER-1730
+                    client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
                 }
                 catch ( Exception e )
                 {
-                    log.error("Could not reset the connection", e);
+                    log.error("Could not inject session expiration", e);
                 }
-                addStateChange(ConnectionState.LOST);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/curator/blob/81bab455/curator-framework/src/test/java/org/apache/curator/framework/imps/TestEnabledSessionExpiredState.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestEnabledSessionExpiredState.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestEnabledSessionExpiredState.java
index a41d581..eff899d 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestEnabledSessionExpiredState.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestEnabledSessionExpiredState.java
@@ -29,11 +29,14 @@ import org.apache.curator.test.KillSession;
 import org.apache.curator.test.Timing;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 public class TestEnabledSessionExpiredState extends BaseClassForTests
@@ -79,6 +82,31 @@ public class TestEnabledSessionExpiredState extends BaseClassForTests
     }
 
     @Test
+    public void testInjectedWatchedEvent() throws Exception
+    {
+        Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        Watcher watcher = new Watcher()
+        {
+            @Override
+            public void process(WatchedEvent event)
+            {
+                if ( event.getType() == Event.EventType.None )
+                {
+                    if ( event.getState() == Event.KeeperState.Expired )
+                    {
+                        latch.countDown();
+                    }
+                }
+            }
+        };
+        client.checkExists().usingWatcher(watcher).forPath("/");
+        server.stop();
+        Assert.assertTrue(timing.forSessionSleep().awaitLatch(latch));
+    }
+
+    @Test
     public void testKillSession() throws Exception
     {
         Assert.assertEquals(states.poll(timing.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);