Repository: curator Updated Branches: refs/heads/CURATOR-358 3478aca7e -> ee5d65463
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/CURATOR-358 Commit: ee5d65463b73abb2a2b732fb02729239b183519d Parents: 3478aca Author: Cam McKenzie <cammcken...@apache.org> Authored: Mon Nov 21 16:06:17 2016 +1100 Committer: Cam McKenzie <cammcken...@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