curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [01/12] 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.
Date Wed, 05 Feb 2014 18:41:55 GMT
Updated Branches:
  refs/heads/master 09f04e9c9 -> 0a82f4eaa


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/master
Commit: a937dfacf68869b1f6a860ffd02993108da99382
Parents: 262d57e
Author: randgalt <randgalt@apache.org>
Authored: Sun Nov 17 12:46:55 2013 -0800
Committer: randgalt <randgalt@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


Mime
View raw message