tisonkun commented on code in PR #500:
URL: https://github.com/apache/curator/pull/500#discussion_r1596382445


##########
curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java:
##########
@@ -548,43 +548,58 @@ private void checkLeadership(List<String> children) 
throws Exception {
         log.debug("checkLeadership with id: {}, ourPath: {}, children: {}", 
id, localOurPath, sortedChildren);
 
         if (ourIndex < 0) {
-            log.error("Can't find our node. Resetting. Index: " + ourIndex);
+            log.error("Can't find our node. Resetting. Index: {}", ourIndex);
             reset();
-        } else if (ourIndex == 0) {
-            lastPathIsLeader.set(localOurPath);
-            setLeadership(true);
-        } else {
-            setLeadership(false);
-            String watchPath = sortedChildren.get(ourIndex - 1);
-            Watcher watcher = new Watcher() {
-                @Override
-                public void process(WatchedEvent event) {
-                    if (state.get() == State.STARTED && event.getType() == 
Event.EventType.NodeDeleted) {
-                        try {
-                            getChildren();
-                        } catch (Exception ex) {
-                            ThreadUtils.checkInterrupted(ex);
-                            log.error("An error occurred checking the 
leadership.", ex);
+            return;
+        }
+
+        if (ourIndex == 0) {
+            client.getData()
+                    .inBackground((client, event) -> {
+                        final long ephemeralOwner =
+                                event.getStat() != null ? 
event.getStat().getEphemeralOwner() : -1;
+                        final long thisSessionId =
+                                
client.getZookeeperClient().getZooKeeper().getSessionId();
+                        if (ephemeralOwner != thisSessionId) {
+                            // this node is gone - reset
+                            reset();
+                        } else {
+                            lastPathIsLeader.set(localOurPath);
+                            setLeadership(true);
                         }

Review Comment:
   This is the major fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@curator.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to