Search in sources :

Example 16 with RaftState

use of com.hazelcast.cp.internal.raft.impl.state.RaftState 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)

Example 17 with RaftState

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

the class PreVoteTask method innerRun.

@Override
protected void innerRun() {
    RaftState state = raftNode.state();
    if (state.leader() != null) {
        logger.fine("No new pre-vote phase, we already have a LEADER: " + state.leader());
        return;
    } else if (state.term() != term) {
        logger.fine("No new pre-vote phase for term= " + term + " because of new term: " + state.term());
        return;
    }
    Collection<RaftEndpoint> remoteMembers = state.remoteMembers();
    if (remoteMembers.isEmpty()) {
        logger.fine("Remote members is empty. No need for pre-voting.");
        return;
    }
    state.initPreCandidateState();
    int nextTerm = state.term() + 1;
    RaftLog log = state.log();
    PreVoteRequest request = new PreVoteRequest(localMember(), nextTerm, log.lastLogOrSnapshotTerm(), log.lastLogOrSnapshotIndex());
    logger.info("Pre-vote started for next term: " + request.nextTerm() + ", last log index: " + request.lastLogIndex() + ", last log term: " + request.lastLogTerm());
    raftNode.printMemberState();
    for (RaftEndpoint endpoint : remoteMembers) {
        raftNode.send(request, endpoint);
    }
    schedulePreVoteTimeout();
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) PreVoteRequest(com.hazelcast.cp.internal.raft.impl.dto.PreVoteRequest) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Example 18 with RaftState

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

the class QueryTask method handleLinearizableRead.

private void handleLinearizableRead() {
    if (!raftNode.isLinearizableReadOptimizationEnabled()) {
        new ReplicateTask(raftNode, operation, resultFuture).run();
        return;
    }
    RaftState state = raftNode.state();
    if (state.role() != LEADER) {
        resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
        return;
    }
    if (!raftNode.canQueryLinearizable()) {
        resultFuture.completeExceptionally(new CannotReplicateException(state.leader()));
        return;
    }
    long commitIndex = state.commitIndex();
    QueryState queryState = state.leaderState().queryState();
    if (logger.isFineEnabled()) {
        logger.fine("Adding query at commit index: " + commitIndex + ", query round: " + queryState.queryRound());
    }
    if (queryState.addQuery(commitIndex, operation, resultFuture) == 1) {
        raftNode.broadcastAppendRequest();
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) QueryState(com.hazelcast.cp.internal.raft.impl.state.QueryState)

Example 19 with RaftState

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

the class MembershipChangeTask method run.

@Override
public void run() {
    try {
        if (!verifyRaftNodeStatus()) {
            return;
        }
        RaftState state = raftNode.state();
        if (state.role() != LEADER) {
            resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
            return;
        }
        if (!isValidGroupMemberCommitIndex()) {
            return;
        }
        Collection<RaftEndpoint> members = new LinkedHashSet<RaftEndpoint>(state.members());
        boolean memberExists = members.contains(member);
        switch(membershipChangeMode) {
            case ADD:
                if (memberExists) {
                    resultFuture.completeExceptionally(new MemberAlreadyExistsException(member));
                    return;
                }
                members.add(member);
                break;
            case REMOVE:
                if (!memberExists) {
                    resultFuture.completeExceptionally(new MemberDoesNotExistException(member));
                    return;
                }
                members.remove(member);
                break;
            default:
                resultFuture.completeExceptionally(new IllegalArgumentException("Unknown type: " + membershipChangeMode));
                return;
        }
        logger.info("New members after " + membershipChangeMode + " " + member + " -> " + members);
        new ReplicateTask(raftNode, new UpdateRaftGroupMembersCmd(members, member, membershipChangeMode), resultFuture).run();
    } catch (Throwable t) {
        logger.severe(this + " failed", t);
        RaftEndpoint leader = raftNode.getLeader();
        UUID leaderUuid = leader != null ? leader.getUuid() : null;
        resultFuture.completeExceptionally(new CPSubsystemException("Internal failure", t, leaderUuid));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) MemberAlreadyExistsException(com.hazelcast.cp.internal.raft.exception.MemberAlreadyExistsException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) MemberDoesNotExistException(com.hazelcast.cp.internal.raft.exception.MemberDoesNotExistException) UpdateRaftGroupMembersCmd(com.hazelcast.cp.internal.raft.impl.command.UpdateRaftGroupMembersCmd) UUID(java.util.UUID)

Example 20 with RaftState

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

the class PreVoteTimeoutTask method innerRun.

@Override
protected void innerRun() {
    RaftState state = raftNode.state();
    // Remove previously set preCandidateState.
    // Since it's now obsolete,
    // either a new pre-vote round will begin
    // or pre-vote phase will cease.
    state.removePreCandidateState();
    if (state.role() != FOLLOWER) {
        return;
    }
    logger.fine("Pre-vote for term: " + state.term() + " has timed out!");
    new PreVoteTask(raftNode, term).run();
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState)

Aggregations

RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)20 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)7 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)5 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)5 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)4 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)3 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)3 MemberAlreadyExistsException (com.hazelcast.cp.internal.raft.exception.MemberAlreadyExistsException)2 MemberDoesNotExistException (com.hazelcast.cp.internal.raft.exception.MemberDoesNotExistException)2 AppendSuccessResponse (com.hazelcast.cp.internal.raft.impl.dto.AppendSuccessResponse)2 CandidateState (com.hazelcast.cp.internal.raft.impl.state.CandidateState)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2 LeaderElectionTask (com.hazelcast.cp.internal.raft.impl.task.LeaderElectionTask)2 UUID (java.util.UUID)2 MismatchingGroupMembersCommitIndexException (com.hazelcast.cp.internal.raft.exception.MismatchingGroupMembersCommitIndexException)1 UpdateRaftGroupMembersCmd (com.hazelcast.cp.internal.raft.impl.command.UpdateRaftGroupMembersCmd)1 AppendFailureResponse (com.hazelcast.cp.internal.raft.impl.dto.AppendFailureResponse)1 PreVoteRequest (com.hazelcast.cp.internal.raft.impl.dto.PreVoteRequest)1 PreVoteResponse (com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse)1 TriggerLeaderElection (com.hazelcast.cp.internal.raft.impl.dto.TriggerLeaderElection)1