Search in sources :

Example 1 with CandidateState

use of com.hazelcast.cp.internal.raft.impl.state.CandidateState in project hazelcast by hazelcast.

the class VoteResponseHandlerTask method handleResponse.

@Override
protected void handleResponse() {
    RaftState state = raftNode.state();
    if (state.role() != CANDIDATE) {
        logger.info("Ignored " + resp + ". We are not CANDIDATE anymore.");
        return;
    }
    if (resp.term() > state.term()) {
        logger.info("Demoting to FOLLOWER from current term: " + state.term() + " to new term: " + resp.term() + " after " + resp);
        raftNode.toFollower(resp.term());
        return;
    }
    if (resp.term() < state.term()) {
        logger.warning("Stale " + resp + " is received, current term: " + state.term());
        return;
    }
    CandidateState candidateState = state.candidateState();
    if (resp.granted() && candidateState.grantVote(resp.voter())) {
        logger.info("Vote granted from " + resp.voter() + " for term: " + state.term() + ", number of votes: " + candidateState.voteCount() + ", majority: " + candidateState.majority());
    }
    if (candidateState.isMajorityGranted()) {
        logger.info("We are the LEADER!");
        raftNode.toLeader();
    }
}
Also used : CandidateState(com.hazelcast.cp.internal.raft.impl.state.CandidateState) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState)

Example 2 with CandidateState

use of com.hazelcast.cp.internal.raft.impl.state.CandidateState in project hazelcast by hazelcast.

the class PreVoteResponseHandlerTask method handleResponse.

@Override
protected void handleResponse() {
    RaftState state = raftNode.state();
    if (state.role() != FOLLOWER) {
        logger.info("Ignored " + resp + ". We are not FOLLOWER anymore.");
        return;
    }
    if (resp.term() < state.term()) {
        logger.warning("Stale " + resp + " is received, current term: " + state.term());
        return;
    }
    CandidateState preCandidateState = state.preCandidateState();
    if (preCandidateState == null) {
        if (logger.isFineEnabled()) {
            logger.fine("Ignoring " + resp + ". We are not interested in pre-votes anymore.");
        }
        return;
    }
    if (resp.granted() && preCandidateState.grantVote(resp.voter())) {
        logger.info("Pre-vote granted from " + resp.voter() + " for term: " + resp.term() + ", number of votes: " + preCandidateState.voteCount() + ", majority: " + preCandidateState.majority());
    }
    if (preCandidateState.isMajorityGranted()) {
        logger.info("We have the majority during pre-vote phase. Let's start real election!");
        new LeaderElectionTask(raftNode, false).run();
    }
}
Also used : CandidateState(com.hazelcast.cp.internal.raft.impl.state.CandidateState) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) LeaderElectionTask(com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask)

Aggregations

CandidateState (com.hazelcast.cp.internal.raft.impl.state.CandidateState)2 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)2 LeaderElectionTask (com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask)1