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/23 03:32:47 UTC

curator git commit: When the connection timeout elapses and there is more than one server in the connection string, reset the connection and try again

Repository: curator
Updated Branches:
  refs/heads/CURATOR-247 235544795 -> 05d241da6


When the connection timeout elapses and there is more than one server in the connection string, reset the connection and try again


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

Branch: refs/heads/CURATOR-247
Commit: 05d241da642c6ba0d16b3ce97557128fad4dfe41
Parents: 2355447
Author: randgalt <ra...@apache.org>
Authored: Sat Aug 22 20:32:41 2015 -0500
Committer: randgalt <ra...@apache.org>
Committed: Sat Aug 22 20:32:41 2015 -0500

----------------------------------------------------------------------
 .../src/main/java/org/apache/curator/RetryLoop.java    |  5 +++++
 .../curator/connection/ConnectionHandlingPolicy.java   |  5 +++++
 .../connection/StandardConnectionHandlingPolicy.java   | 13 ++++++++++++-
 3 files changed, 22 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/05d241da/curator-client/src/main/java/org/apache/curator/RetryLoop.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/RetryLoop.java b/curator-client/src/main/java/org/apache/curator/RetryLoop.java
index 35d55a1..a17cbf3 100644
--- a/curator-client/src/main/java/org/apache/curator/RetryLoop.java
+++ b/curator-client/src/main/java/org/apache/curator/RetryLoop.java
@@ -121,6 +121,11 @@ public class RetryLoop
                         break;
                     }
 
+                    case WAIT_FOR_CONNECTION:
+                    {
+                        break;  // just loop
+                    }
+
                     case EXIT_RETRIES:
                     {
                         retryLoop.markComplete();

http://git-wip-us.apache.org/repos/asf/curator/blob/05d241da/curator-client/src/main/java/org/apache/curator/connection/ConnectionHandlingPolicy.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/connection/ConnectionHandlingPolicy.java b/curator-client/src/main/java/org/apache/curator/connection/ConnectionHandlingPolicy.java
index f3ecce6..7f19159 100644
--- a/curator-client/src/main/java/org/apache/curator/connection/ConnectionHandlingPolicy.java
+++ b/curator-client/src/main/java/org/apache/curator/connection/ConnectionHandlingPolicy.java
@@ -63,6 +63,11 @@ public interface ConnectionHandlingPolicy
         CALL_PROC,
 
         /**
+         * Wait again for connection success or timeout
+         */
+        WAIT_FOR_CONNECTION,
+
+        /**
          * Do not call the procedure and exit the retry loop
          */
         EXIT_RETRIES,

http://git-wip-us.apache.org/repos/asf/curator/blob/05d241da/curator-client/src/main/java/org/apache/curator/connection/StandardConnectionHandlingPolicy.java
----------------------------------------------------------------------
diff --git a/curator-client/src/main/java/org/apache/curator/connection/StandardConnectionHandlingPolicy.java b/curator-client/src/main/java/org/apache/curator/connection/StandardConnectionHandlingPolicy.java
index 06285ca..cbbceac 100644
--- a/curator-client/src/main/java/org/apache/curator/connection/StandardConnectionHandlingPolicy.java
+++ b/curator-client/src/main/java/org/apache/curator/connection/StandardConnectionHandlingPolicy.java
@@ -1,10 +1,15 @@
 package org.apache.curator.connection;
 
+import com.google.common.base.Splitter;
 import org.apache.curator.CuratorZookeeperClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import java.util.concurrent.Callable;
 
 public class StandardConnectionHandlingPolicy implements ConnectionHandlingPolicy
 {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
     @Override
     public boolean isEmulatingClassicHandling()
     {
@@ -24,9 +29,15 @@ public class StandardConnectionHandlingPolicy implements ConnectionHandlingPolic
     @Override
     public PreRetryResult preRetry(CuratorZookeeperClient client) throws Exception
     {
-        // TODO - see if there are other servers to connect to
         if ( !client.isConnected() )
         {
+            int serverCount = Splitter.on(",").omitEmptyStrings().splitToList(client.getCurrentConnectionString()).size();
+            if ( serverCount > 1 )
+            {
+                log.info("Connection timed out and connection string is > 1. Resetting connection and trying again.");
+                client.reset(); // unfortunately, there's no way to guarantee that ZK tries a different server. Internally it calls Collections.shuffle(). Hopefully, this will result in a different server each time.
+                return PreRetryResult.WAIT_FOR_CONNECTION;
+            }
             return PreRetryResult.CONNECTION_TIMEOUT;
         }