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 13E6110715 for ; Sun, 17 Nov 2013 20:47:01 +0000 (UTC) Received: (qmail 93483 invoked by uid 500); 17 Nov 2013 20:47:01 -0000 Delivered-To: apmail-curator-commits-archive@curator.apache.org Received: (qmail 93451 invoked by uid 500); 17 Nov 2013 20:47:00 -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 93428 invoked by uid 99); 17 Nov 2013 20:47:00 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 17 Nov 2013 20:47:00 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 73D081F882; Sun, 17 Nov 2013 20:47:00 +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: Sun, 17 Nov 2013 20:47:04 -0000 Message-Id: <22f688b33fc94a27b59710b9aaf9e175@git.apache.org> In-Reply-To: <34d5b63753e24131863b25b2c261689b@git.apache.org> References: <34d5b63753e24131863b25b2c261689b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [5/5] git commit: Background connection errors would go straight to LOST which is different than foreground connection errors. Changed so that background connection errors go to SUSPENDED first just like foreground connection errors. Background connection errors would go straight to LOST which is different than foreground connection errors. Changed so that background connection errors go to SUSPENDED first just like foreground connection errors. Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/a937dfac Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/a937dfac Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/a937dfac Branch: refs/heads/CURATOR-72 Commit: a937dfacf68869b1f6a860ffd02993108da99382 Parents: 262d57e Author: randgalt Authored: Sun Nov 17 12:46:55 2013 -0800 Committer: randgalt Committed: Sun Nov 17 12:46:55 2013 -0800 ---------------------------------------------------------------------- .../framework/imps/CuratorFrameworkImpl.java | 11 ++- .../TestPersistentEphemeralNodeListener.java | 94 ++++++++++++++++++++ 2 files changed, 102 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/a937dfac/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 d56c9a4..3aa1097 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 @@ -514,7 +514,7 @@ public class CuratorFrameworkImpl implements CuratorFramework if ( e instanceof KeeperException.ConnectionLossException ) { - connectionStateManager.addStateChange(ConnectionState.LOST); + handleKeeperStateDisconnected(); } final String localReason = reason; @@ -745,8 +745,7 @@ public class CuratorFrameworkImpl implements CuratorFramework { if ( curatorEvent.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected ) { - connectionStateManager.addStateChange(ConnectionState.SUSPENDED); - internalSync(this, "/", null); // we appear to have disconnected, force a new ZK event and see if we can connect to another server + handleKeeperStateDisconnected(); } else if ( curatorEvent.getWatchedEvent().getState() == Watcher.Event.KeeperState.Expired ) { @@ -762,4 +761,10 @@ public class CuratorFrameworkImpl implements CuratorFramework } } } + + private void handleKeeperStateDisconnected() + { + connectionStateManager.addStateChange(ConnectionState.SUSPENDED); + internalSync(this, "/", null); // we appear to have disconnected, force a new ZK event and see if we can connect to another server + } } http://git-wip-us.apache.org/repos/asf/curator/blob/a937dfac/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNodeListener.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNodeListener.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNodeListener.java new file mode 100644 index 0000000..b97ba41 --- /dev/null +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/nodes/TestPersistentEphemeralNodeListener.java @@ -0,0 +1,94 @@ +/** + * 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.nodes; + +import com.google.common.collect.Lists; +import com.google.common.io.Closeables; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.BaseClassForTests; +import org.apache.curator.framework.state.ConnectionState; +import org.apache.curator.framework.state.ConnectionStateListener; +import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.test.TestingServer; +import org.apache.curator.test.Timing; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.Test; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +public class TestPersistentEphemeralNodeListener extends BaseClassForTests +{ + @Test + public void testListenersReconnectedIsOK() throws Exception + { + server.close(); + + Timing timing = new Timing(); + CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + try + { + client.start(); + PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes()); + node.start(); + + final CountDownLatch connectedLatch = new CountDownLatch(1); + final CountDownLatch reconnectedLatch = new CountDownLatch(1); + final AtomicReference lastState = new AtomicReference(); + ConnectionStateListener listener = new ConnectionStateListener() + { + @Override + public void stateChanged(CuratorFramework client, ConnectionState newState) + { + lastState.set(newState); + if ( newState == ConnectionState.CONNECTED ) + { + connectedLatch.countDown(); + } + if ( newState == ConnectionState.RECONNECTED ) + { + reconnectedLatch.countDown(); + } + System.out.println("XXXX " + newState); + } + }; + client.getConnectionStateListenable().addListener(listener); + timing.sleepABit(); + server = new TestingServer(server.getPort()); + Assert.assertTrue(timing.awaitLatch(connectedLatch)); + timing.sleepABit(); + Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS)); + server.close(); + timing.sleepABit(); + server = new TestingServer(server.getPort()); + timing.sleepABit(); + Assert.assertTrue(timing.awaitLatch(reconnectedLatch)); + timing.sleepABit(); + Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED); + } + finally + { + Closeables.closeQuietly(client); + } + } +} \ No newline at end of file