// Beware race conditions: closeLeaderLatch() may be called by another thread at any time.
while (isRunning()) {
try {
// Start attempting to acquire leadership via the Curator leadership latch.
LOG.debug("Attempting to acquire leadership: {}", getId());
LeaderLatch latch = startLeaderLatch();
// Wait until (a) leadership is acquired or (b) the latch is closed by service shutdown or ZK cxn loss.
if (isRunning()) {
try {
latch.await();
} catch (EOFException e) {
// Latch was closed while we were waiting.
checkState(!latch.hasLeadership());
}
}
// If we succeeded in acquiring leadership, start/run the leadership-managed delegate service.
if (isRunning() && latch.hasLeadership()) {
LOG.debug("Leadership acquired: {}", getId());
runAsLeader();
LOG.debug("Leadership released: {}", getId());
}
} finally {