in curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderSelector.java [422:467]
void doWork() throws Exception {
taskStarted();
hasLeadership = false;
try {
mutex.acquire();
hasLeadership = true;
try {
if (debugLeadershipLatch != null) {
debugLeadershipLatch.countDown();
}
if (debugLeadershipWaitLatch != null) {
debugLeadershipWaitLatch.await();
}
listener.takeLeadership(client);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
} catch (Throwable e) {
ThreadUtils.checkInterrupted(e);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
} finally {
if (taskDone()) {
boolean wasInterrupted =
Thread.interrupted(); // clear any interrupted status so that mutex.release() works immediately
try {
mutex.release();
} catch (Exception e) {
if (failedMutexReleaseCount != null) {
failedMutexReleaseCount.incrementAndGet();
}
ThreadUtils.checkInterrupted(e);
log.error("The leader threw an exception", e);
// ignore errors - this is just a safety
} finally {
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
}
}
}
}