Search in sources :

Example 1 with LeaderElectionTask

use of com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask in project hazelcast by hazelcast.

the class TriggerLeaderElectionHandlerTask method innerRun.

@Override
protected void innerRun() {
    if (logger.isFineEnabled()) {
        logger.fine("Received " + req);
    }
    RaftState state = raftNode.state();
    // with the leader's log.
    if (!(req.term() == state.term() && req.leader().equals(state.leader()))) {
        if (logger.isFineEnabled()) {
            logger.fine("Ignoring " + req + " since term: " + state.term() + " and leader: " + state.leader());
        }
        return;
    }
    // Verify the last log entry
    LogEntry entry = state.log().lastLogOrSnapshotEntry();
    if (!(entry.index() == req.lastLogIndex() && entry.term() == req.lastLogTerm())) {
        if (logger.isFineEnabled()) {
            logger.fine("Could not accept leadership transfer because local Raft log is not same with the current leader. " + "Last log entry: " + entry + ", request: " + req);
        }
        return;
    }
    // I will send a disruptive VoteRequest to bypass leader stickiness
    logger.info("Starting a new leader election since the current leader: " + req.leader() + " in term: " + req.term() + " asked for a leadership transfer!");
    state.leader(null);
    new LeaderElectionTask(raftNode, true).run();
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) LeaderElectionTask(com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry)

Example 2 with LeaderElectionTask

use of com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask 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

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