Search in sources :

Example 1 with RaftState

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

the class InitLeadershipTransferTask method run.

@Override
public void run() {
    if (!raftNode.getCommittedMembers().contains(targetEndpoint)) {
        resultFuture.completeExceptionally(new IllegalArgumentException("Cannot transfer leadership to " + targetEndpoint + " because it is not in the committed group member list!"));
        return;
    }
    if (raftNode.getStatus() != RaftNodeStatus.ACTIVE) {
        resultFuture.completeExceptionally(new IllegalStateException("Cannot transfer leadership to " + targetEndpoint + " because the status is " + raftNode.getStatus()));
        return;
    }
    RaftState state = raftNode.state();
    LeaderState leaderState = state.leaderState();
    if (leaderState == null) {
        resultFuture.completeExceptionally(new IllegalStateException("Cannot transfer leadership to " + targetEndpoint + " because I am not the leader!"));
        return;
    }
    if (raftNode.getLocalMember().equals(targetEndpoint)) {
        raftNode.getLogger(getClass()).warning("I am already the leader... There is no leadership transfer to myself.");
        resultFuture.complete(null);
        return;
    }
    if (state.initLeadershipTransfer(targetEndpoint, resultFuture)) {
        new LeadershipTransferTask(raftNode, LEADERSHIP_TRANSFER_RETRY_COUNT).run();
    }
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Example 2 with RaftState

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

the class MembershipChangeTask method isValidGroupMemberCommitIndex.

private boolean isValidGroupMemberCommitIndex() {
    if (groupMembersCommitIndex != null) {
        RaftState state = raftNode.state();
        RaftGroupMembers groupMembers = state.committedGroupMembers();
        if (groupMembers.index() != groupMembersCommitIndex) {
            logger.severe("Cannot " + membershipChangeMode + " " + member + " because expected members commit index: " + groupMembersCommitIndex + " is different than group members commit index: " + groupMembers.index());
            Exception e = new MismatchingGroupMembersCommitIndexException(groupMembers.index(), groupMembers.members());
            resultFuture.completeExceptionally(e);
            return false;
        }
    }
    return true;
}
Also used : MismatchingGroupMembersCommitIndexException(com.hazelcast.cp.internal.raft.exception.MismatchingGroupMembersCommitIndexException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftGroupMembers(com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers) MemberDoesNotExistException(com.hazelcast.cp.internal.raft.exception.MemberDoesNotExistException) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) MemberAlreadyExistsException(com.hazelcast.cp.internal.raft.exception.MemberAlreadyExistsException) MismatchingGroupMembersCommitIndexException(com.hazelcast.cp.internal.raft.exception.MismatchingGroupMembersCommitIndexException)

Example 3 with RaftState

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

the class ReplicateTask 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 (!raftNode.canReplicateNewEntry(operation)) {
            resultFuture.completeExceptionally(new CannotReplicateException(raftNode.getLocalMember()));
            return;
        }
        if (logger.isFineEnabled()) {
            logger.fine("Replicating: " + operation + " in term: " + state.term());
        }
        RaftLog log = state.log();
        if (!log.checkAvailableCapacity(1)) {
            resultFuture.completeExceptionally(new IllegalStateException("Not enough capacity in RaftLog!"));
            return;
        }
        long newEntryLogIndex = log.lastLogOrSnapshotIndex() + 1;
        raftNode.registerFuture(newEntryLogIndex, resultFuture);
        log.appendEntries(new LogEntry(state.term(), newEntryLogIndex, operation));
        preApplyRaftGroupCmd(newEntryLogIndex, operation);
        raftNode.broadcastAppendRequest();
    } catch (Throwable t) {
        logger.severe(operation + " could not be replicated to leader: " + raftNode.getLocalMember(), t);
        RaftEndpoint leader = raftNode.getLeader();
        UUID leaderUuid = leader != null ? leader.getUuid() : null;
        resultFuture.completeExceptionally(new CPSubsystemException("Internal failure", t, leaderUuid));
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) UUID(java.util.UUID) CPSubsystemException(com.hazelcast.cp.exception.CPSubsystemException) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Example 4 with RaftState

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

the class LeaderElectionTask method innerRun.

@Override
protected void innerRun() {
    RaftState state = raftNode.state();
    if (state.leader() != null) {
        logger.warning("No new election round, we already have a LEADER: " + state.leader());
        return;
    }
    VoteRequest request = state.toCandidate(disruptive);
    logger.info("Leader election started for term: " + request.term() + ", last log index: " + request.lastLogIndex() + ", last log term: " + request.lastLogTerm());
    raftNode.printMemberState();
    for (RaftEndpoint endpoint : state.remoteMembers()) {
        raftNode.send(request, endpoint);
    }
    scheduleLeaderElectionTimeout();
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) VoteRequest(com.hazelcast.cp.internal.raft.impl.dto.VoteRequest)

Example 5 with RaftState

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

the class LeadershipTransferTask method run.

@Override
public void run() {
    ILogger logger = raftNode.getLogger(getClass());
    RaftState state = raftNode.state();
    LeaderState leaderState = state.leaderState();
    if (leaderState == null) {
        logger.fine("Not retrying leadership transfer since not leader...");
        return;
    }
    LeadershipTransferState leadershipTransferState = state.leadershipTransferState();
    checkTrue(leadershipTransferState != null, "No leadership transfer state!");
    if (retryCount == maxRetryCount) {
        String msg = "Leadership transfer to " + leadershipTransferState.endpoint() + " timed out!";
        logger.warning(msg);
        state.completeLeadershipTransfer(new IllegalStateException(msg));
        return;
    }
    RaftEndpoint targetEndpoint = leadershipTransferState.endpoint();
    if (state.commitIndex() < state.log().lastLogOrSnapshotIndex()) {
        logger.warning("Waiting until all appended entries to be committed before transferring leadership to " + targetEndpoint);
        reschedule();
        return;
    }
    if (retryCount > 0) {
        logger.fine("Retrying leadership transfer to " + leadershipTransferState.endpoint());
    } else {
        logger.info("Transferring leadership to " + leadershipTransferState.endpoint());
    }
    leaderState.getFollowerState(targetEndpoint).appendRequestAckReceived();
    raftNode.sendAppendRequest(targetEndpoint);
    LogEntry entry = state.log().lastLogOrSnapshotEntry();
    raftNode.send(new TriggerLeaderElection(raftNode.getLocalMember(), state.term(), entry.term(), entry.index()), targetEndpoint);
    reschedule();
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ILogger(com.hazelcast.logging.ILogger) LeadershipTransferState(com.hazelcast.cp.internal.raft.impl.state.LeadershipTransferState) TriggerLeaderElection(com.hazelcast.cp.internal.raft.impl.dto.TriggerLeaderElection) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

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