You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ca...@apache.org on 2016/11/22 22:10:15 UTC

[1/2] curator git commit: CURATOR-358 - Fixed race condition with getLeader() -If leadership changes between the getParticipantNodes() call and the getLeader() internal call the NoNodeException is now handled and the next child in the list is evaluated.

Repository: curator
Updated Branches:
  refs/heads/master e9bfb4353 -> ee5d65463


CURATOR-358 - Fixed race condition with getLeader()
-If leadership changes between the getParticipantNodes() call and the getLeader() internal call the NoNodeException is now handled and the next child in the list is evaluated.


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

Branch: refs/heads/master
Commit: 3478aca7ed6852484b5574a6082f4bb75c04a1e0
Parents: e9bfb43
Author: Cam McKenzie <ca...@apache.org>
Authored: Mon Nov 21 10:38:15 2016 +1100
Committer: Cam McKenzie <ca...@apache.org>
Committed: Mon Nov 21 10:38:15 2016 +1100

----------------------------------------------------------------------
 .../recipes/leader/LeaderSelector.java          | 35 ++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/3478aca7/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
index c177302..4b4a0b4 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
@@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory;
 import java.io.Closeable;
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.AbstractExecutorService;
 import java.util.concurrent.Callable;
@@ -341,11 +342,41 @@ public class LeaderSelector implements Closeable
 
     static Participant getLeader(CuratorFramework client, Collection<String> participantNodes) throws Exception
     {
+        Participant result = null;
+        
         if ( participantNodes.size() > 0 )
         {
-            return participantForPath(client, participantNodes.iterator().next(), true);
+            Iterator<String> iter = participantNodes.iterator();
+            while ( iter.hasNext() )
+            {
+                
+                try
+                {
+                    result = participantForPath(client, iter.next(), true);
+                }
+                catch( KeeperException.NoNodeException e )
+                {
+                    //See CURATOR-358
+                    //There's a race condition between querying the list of
+                    //leader nodes and then determining the content of the
+                    //actual leader node. If the query fails due to the
+                    //node not existing, then just move to the next
+                    //participant node and try again
+                }
+                
+                if ( result != null )
+                {
+                    break;
+                }
+            }
+        }
+        
+        if( result == null )
+        {
+            result = new Participant();
         }
-        return new Participant();
+        
+        return result;
     }
 
     /**


[2/2] curator git commit: CURATOR-358 - Move NoNode exception handling inside participantsForPath

Posted by ca...@apache.org.
CURATOR-358 - Move NoNode exception handling inside participantsForPath


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

Branch: refs/heads/master
Commit: ee5d65463b73abb2a2b732fb02729239b183519d
Parents: 3478aca
Author: Cam McKenzie <ca...@apache.org>
Authored: Mon Nov 21 16:06:17 2016 +1100
Committer: Cam McKenzie <ca...@apache.org>
Committed: Mon Nov 21 16:06:17 2016 +1100

----------------------------------------------------------------------
 .../recipes/leader/LeaderSelector.java          | 41 ++++++++------------
 1 file changed, 16 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/ee5d6546/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
index 4b4a0b4..6ad1053 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java
@@ -303,17 +303,14 @@ public class LeaderSelector implements Closeable
         boolean isLeader = true;
         for ( String path : participantNodes )
         {
-            try
+            Participant participant = participantForPath(client, path, isLeader);
+            
+            if( participant != null )
             {
-                Participant participant = participantForPath(client, path, isLeader);
                 builder.add(participant);
-            }
-            catch ( KeeperException.NoNodeException ignore )
-            {
-                // ignore
-            }
 
-            isLeader = false;   // by definition the first node is the leader
+                isLeader = false;   // by definition the first node is the leader
+            }
         }
 
         return builder.build();
@@ -349,20 +346,7 @@ public class LeaderSelector implements Closeable
             Iterator<String> iter = participantNodes.iterator();
             while ( iter.hasNext() )
             {
-                
-                try
-                {
-                    result = participantForPath(client, iter.next(), true);
-                }
-                catch( KeeperException.NoNodeException e )
-                {
-                    //See CURATOR-358
-                    //There's a race condition between querying the list of
-                    //leader nodes and then determining the content of the
-                    //actual leader node. If the query fails due to the
-                    //node not existing, then just move to the next
-                    //participant node and try again
-                }
+                result = participantForPath(client, iter.next(), true);
                 
                 if ( result != null )
                 {
@@ -403,9 +387,16 @@ public class LeaderSelector implements Closeable
 
     private static Participant participantForPath(CuratorFramework client, String path, boolean markAsLeader) throws Exception
     {
-        byte[] bytes = client.getData().forPath(path);
-        String thisId = new String(bytes, "UTF-8");
-        return new Participant(thisId, markAsLeader);
+        try
+        {
+            byte[] bytes = client.getData().forPath(path);
+            String thisId = new String(bytes, "UTF-8");
+            return new Participant(thisId, markAsLeader);
+        }
+        catch ( KeeperException.NoNodeException e )
+        {
+            return null;
+        }
     }
 
     @VisibleForTesting