in twill-zookeeper/src/main/java/org/apache/twill/internal/zookeeper/LeaderElection.java [376:412]
public void process(WatchedEvent event) {
switch (event.getState()) {
case Disconnected:
disconnected = true;
LOG.info("Disconnected from ZK: {} for {}", zkClient.getConnectString(), zkFolderPath);
if (state == State.LEADER) {
// becomeFollower has to be called in disconnect so that no two active leader is possible.
LOG.info("Stepping down as leader due to disconnect: {} for {}", zkClient.getConnectString(), zkFolderPath);
becomeFollower();
}
break;
case SyncConnected:
boolean runElection = disconnected && !expired && state != State.IN_PROGRESS;
boolean runRegister = disconnected && expired && state != State.IN_PROGRESS;
disconnected = false;
expired = false;
if (runElection) {
// If the state is cancelled (meaning a cancel happens between disconnect and connect),
// still runElection() so that it has chance to delete the node (as it's not expired, the node stays
// after reconnection).
if (state != State.CANCELLED) {
state = State.IN_PROGRESS;
}
LOG.info("Connected to ZK, running election: {} for {}", zkClient.getConnectString(), zkFolderPath);
runElection();
} else if (runRegister && state != State.CANCELLED) {
LOG.info("Connected to ZK, registering: {} for {}", zkClient.getConnectString(), zkFolderPath);
register();
}
break;
case Expired:
LOG.info("ZK session expired: {} for {}", zkClient.getConnectString(), zkFolderPath);
expired = true;
break;
}
}