Search in sources :

Example 1 with RequestVoteReplyProto

use of org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto in project incubator-ratis by apache.

the class LeaderElection method waitForResults.

private ResultAndTerm waitForResults(final long electionTerm, final int submitted) throws InterruptedException {
    final Timestamp timeout = new Timestamp().addTimeMs(server.getRandomTimeoutMs());
    final List<RequestVoteReplyProto> responses = new ArrayList<>();
    final List<Exception> exceptions = new ArrayList<>();
    int waitForNum = submitted;
    Collection<RaftPeerId> votedPeers = new ArrayList<>();
    while (waitForNum > 0 && running && server.isCandidate()) {
        final long waitTime = -timeout.elapsedTimeMs();
        if (waitTime <= 0) {
            return logAndReturn(Result.TIMEOUT, responses, exceptions, -1);
        }
        try {
            final Future<RequestVoteReplyProto> future = service.poll(waitTime, TimeUnit.MILLISECONDS);
            if (future == null) {
                // poll timeout, continue to return Result.TIMEOUT
                continue;
            }
            final RequestVoteReplyProto r = future.get();
            responses.add(r);
            if (r.getShouldShutdown()) {
                return logAndReturn(Result.SHUTDOWN, responses, exceptions, -1);
            }
            if (r.getTerm() > electionTerm) {
                return logAndReturn(Result.DISCOVERED_A_NEW_TERM, responses, exceptions, r.getTerm());
            }
            if (r.getServerReply().getSuccess()) {
                votedPeers.add(RaftPeerId.valueOf(r.getServerReply().getReplyId()));
                if (conf.hasMajority(votedPeers, server.getId())) {
                    return logAndReturn(Result.PASSED, responses, exceptions, -1);
                }
            }
        } catch (ExecutionException e) {
            LOG.info("{} got exception when requesting votes: {}", server.getId(), e);
            LOG.trace("TRACE", e);
            exceptions.add(e);
        }
        waitForNum--;
    }
    // received all the responses
    return logAndReturn(Result.REJECTED, responses, exceptions, -1);
}
Also used : ArrayList(java.util.ArrayList) RequestVoteReplyProto(org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Timestamp(org.apache.ratis.util.Timestamp) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 RequestVoteReplyProto (org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto)1 Timestamp (org.apache.ratis.util.Timestamp)1