Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void isLeader()
}
catch (Exception ex) {
log.makeAlert(ex, "listener becomeLeader() failed. Unable to become leader").emit();

recreateLeaderLatch();
stopAndCreateNewLeaderLatch();
startLeaderLatch();
}
}

Expand All @@ -119,8 +119,10 @@ public void notLeader()
}

leader = false;
// give others a chance to become leader.
stopAndCreateNewLeaderLatch();
listener.stopBeingLeader();
recreateLeaderLatch();
startLeaderLatch();
}
catch (Exception ex) {
log.makeAlert(ex, "listener.stopBeingLeader() failed. Unable to stopBeingLeader").emit();
Expand Down Expand Up @@ -205,15 +207,18 @@ public void unregisterListener()
listenerExecutor.shutdownNow();
}

private void recreateLeaderLatch()
private void stopAndCreateNewLeaderLatch()
{
// give others a chance to become leader.
CloseableUtils.closeAndSuppressExceptions(
createNewLeaderLatchWithListener(),
e -> log.warn("Could not close old leader latch; continuing with new one anyway.")
);

leader = false;
}

private void startLeaderLatch()
{
try {
//Small delay before starting the latch so that others waiting are chosen to become leader.
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
Expand Down