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 2013/11/17 21:47:04 UTC

[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 <ra...@apache.org>
Authored: Sun Nov 17 12:46:55 2013 -0800
Committer: randgalt <ra...@apache.org>
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<ConnectionState> lastState = new AtomicReference<ConnectionState>();
+            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