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 <cammcken...@apache.org> Authored: Mon Nov 21 10:38:15 2016 +1100 Committer: Cam McKenzie <cammcken...@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; } /**