Return-Path: X-Original-To: apmail-curator-commits-archive@minotaur.apache.org Delivered-To: apmail-curator-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8BE5D18DF1 for ; Tue, 1 Sep 2015 13:27:54 +0000 (UTC) Received: (qmail 31991 invoked by uid 500); 1 Sep 2015 13:27:54 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 31919 invoked by uid 500); 1 Sep 2015 13:27:54 -0000 Mailing-List: contact commits-help@curator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@curator.apache.org Delivered-To: mailing list commits@curator.apache.org Received: (qmail 30915 invoked by uid 99); 1 Sep 2015 13:27:53 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Sep 2015 13:27:53 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E0F7BDFFCD; Tue, 1 Sep 2015 13:27:53 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: randgalt@apache.org To: commits@curator.apache.org Date: Tue, 01 Sep 2015 13:28:19 -0000 Message-Id: <996cf54a2a324801993f8eb104eca303@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [27/45] 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 expir 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-248 Commit: 81bab455cf259f3b45d1fa0f7e26d78127f06d61 Parents: dd78816 Author: randgalt Authored: Mon Aug 24 00:21:35 2015 -0500 Committer: randgalt 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);