Search in sources :

Example 1 with PreVoteResponse

use of com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse in project hazelcast by hazelcast.

the class PreVoteRequestHandlerTask method innerRun.

@Override
protected void innerRun() {
    RaftState state = raftNode.state();
    RaftEndpoint localEndpoint = localMember();
    // Reply false if term < currentTerm (ยง5.1)
    if (state.term() > req.nextTerm()) {
        logger.info("Rejecting " + req + " since current term: " + state.term() + " is bigger");
        raftNode.send(new PreVoteResponse(localEndpoint, state.term(), false), req.candidate());
        return;
    }
    // Reply false if last AppendEntries call was received less than election timeout ago (leader stickiness)
    if (raftNode.lastAppendEntriesTimestamp() > Clock.currentTimeMillis() - raftNode.getLeaderElectionTimeoutInMillis()) {
        logger.info("Rejecting " + req + " since received append entries recently.");
        raftNode.send(new PreVoteResponse(localEndpoint, state.term(), false), req.candidate());
        return;
    }
    RaftLog raftLog = state.log();
    if (raftLog.lastLogOrSnapshotTerm() > req.lastLogTerm()) {
        logger.info("Rejecting " + req + " since our last log term: " + raftLog.lastLogOrSnapshotTerm() + " is greater");
        raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), false), req.candidate());
        return;
    }
    if (raftLog.lastLogOrSnapshotTerm() == req.lastLogTerm() && raftLog.lastLogOrSnapshotIndex() > req.lastLogIndex()) {
        logger.info("Rejecting " + req + " since our last log index: " + raftLog.lastLogOrSnapshotIndex() + " is greater");
        raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), false), req.candidate());
        return;
    }
    logger.info("Granted pre-vote for " + req);
    raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), true), req.candidate());
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) PreVoteResponse(com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Aggregations

RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)1 PreVoteResponse (com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse)1 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)1 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)1