in ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java [316:359]
private boolean askForVotes(Phase phase, int round) throws InterruptedException, IOException {
final long electionTerm;
final RaftConfigurationImpl conf;
synchronized (server) {
if (!shouldRun()) {
return false;
}
// If round0 is non-null, we have already called initElection in the constructor,
// reuse round0 to avoid initElection again for the first round
final ConfAndTerm confAndTerm = (round == 0 && round0 != null) ?
round0 : server.getState().initElection(phase);
electionTerm = confAndTerm.getTerm();
conf = confAndTerm.getConf();
}
LOG.info("{} {} round {}: submit vote requests at term {} for {}", this, phase, round, electionTerm, conf);
final ResultAndTerm r = submitRequestAndWaitResult(phase, conf, electionTerm);
LOG.info("{} {} round {}: result {}", this, phase, round, r);
synchronized (server) {
if (!shouldRun(electionTerm)) {
return false; // term already passed or this should not run anymore.
}
switch (r.getResult()) {
case PASSED:
case SINGLE_MODE_PASSED:
return true;
case NOT_IN_CONF:
case SHUTDOWN:
server.close();
server.getStateMachine().event().notifyServerShutdown(server.getRoleInfoProto(), false);
return false;
case TIMEOUT:
return false; // should retry
case REJECTED:
case DISCOVERED_A_NEW_TERM:
final long term = r.maxTerm(server.getState().getCurrentTerm());
server.changeToFollowerAndPersistMetadata(term, false, r);
return false;
default: throw new IllegalArgumentException("Unable to process result " + r.result);
}
}
}