Search in sources :

Example 1 with LeaderState

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

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

Example 3 with LeaderState

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

the class AppendFailureResponseHandlerTask method updateNextIndex.

private boolean updateNextIndex(RaftState state) {
    LeaderState leaderState = state.leaderState();
    FollowerState followerState = leaderState.getFollowerState(resp.follower());
    long nextIndex = followerState.nextIndex();
    long matchIndex = followerState.matchIndex();
    if (resp.expectedNextIndex() == nextIndex) {
        // Received a response for the last append request. Resetting the flag...
        followerState.appendRequestAckReceived();
        // this is the response of the request I have sent for this nextIndex
        nextIndex--;
        if (nextIndex <= matchIndex) {
            logger.severe("Cannot decrement next index: " + nextIndex + " below match index: " + matchIndex + " for follower: " + resp.follower());
            return false;
        }
        if (logger.isFineEnabled()) {
            logger.fine("Updating next index: " + nextIndex + " for follower: " + resp.follower());
        }
        followerState.nextIndex(nextIndex);
        return true;
    }
    return false;
}
Also used : FollowerState(com.hazelcast.cp.internal.raft.impl.state.FollowerState) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Example 4 with LeaderState

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

the class AppendSuccessResponseHandlerTask method updateFollowerIndices.

private boolean updateFollowerIndices(RaftState state) {
    // If successful: update nextIndex and matchIndex for follower (ยง5.3)
    RaftEndpoint follower = resp.follower();
    LeaderState leaderState = state.leaderState();
    FollowerState followerState = leaderState.getFollowerState(follower);
    QueryState queryState = leaderState.queryState();
    if (queryState.tryAck(resp.queryRound(), follower)) {
        if (logger.isFineEnabled()) {
            logger.fine("Ack from " + follower + " for query round: " + resp.queryRound());
        }
    }
    long matchIndex = followerState.matchIndex();
    long followerLastLogIndex = resp.lastLogIndex();
    if (followerLastLogIndex > matchIndex) {
        // Received a response for the last append request. Resetting the flag...
        followerState.appendRequestAckReceived();
        long newNextIndex = followerLastLogIndex + 1;
        followerState.matchIndex(followerLastLogIndex);
        followerState.nextIndex(newNextIndex);
        if (logger.isFineEnabled()) {
            logger.fine("Updated match index: " + followerLastLogIndex + " and next index: " + newNextIndex + " for follower: " + follower);
        }
        return true;
    } else if (followerLastLogIndex == matchIndex) {
        // Received a response for the last append request. Resetting the flag...
        followerState.appendRequestAckReceived();
    } else if (logger.isFineEnabled()) {
        logger.fine("Will not update match index for follower: " + follower + ". follower last log index: " + followerLastLogIndex + ", match index: " + matchIndex);
    }
    return false;
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) QueryState(com.hazelcast.cp.internal.raft.impl.state.QueryState) FollowerState(com.hazelcast.cp.internal.raft.impl.state.FollowerState) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Example 5 with LeaderState

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

the class RaftNodeImpl method toFollower.

/**
 * Switches this node to follower role by clearing the known leader
 * endpoint and (pre) candidate states, and updating the term. If this Raft
 * node was leader before switching to the follower state, it may have some
 * queries waiting to be executed. Those queries are also failed with
 * {@link LeaderDemotedException}. After the state switch,
 * {@link RaftIntegration#onNodeStatusChange(RaftNodeStatus)} is called.
 *
 * @param term the new term to switch
 */
public void toFollower(int term) {
    LeaderState leaderState = state.leaderState();
    if (leaderState != null) {
        for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
            t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
        }
    }
    state.toFollower(term);
    printMemberState();
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Aggregations

LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)9 FollowerState (com.hazelcast.cp.internal.raft.impl.state.FollowerState)4 LeaderDemotedException (com.hazelcast.cp.exception.LeaderDemotedException)3 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)2 AppendRequest (com.hazelcast.cp.internal.raft.impl.dto.AppendRequest)2 InstallSnapshot (com.hazelcast.cp.internal.raft.impl.dto.InstallSnapshot)2 TriggerLeaderElection (com.hazelcast.cp.internal.raft.impl.dto.TriggerLeaderElection)2 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)2 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)2 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)2 RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)1 CPGroupId (com.hazelcast.cp.CPGroupId)1 CPMember (com.hazelcast.cp.CPMember)1 StaleAppendRequestException (com.hazelcast.cp.exception.StaleAppendRequestException)1 MembershipChangeMode (com.hazelcast.cp.internal.raft.MembershipChangeMode)1 QueryPolicy (com.hazelcast.cp.internal.raft.QueryPolicy)1 DestroyRaftGroupCmd (com.hazelcast.cp.internal.raft.command.DestroyRaftGroupCmd)1 RaftGroupCmd (com.hazelcast.cp.internal.raft.command.RaftGroupCmd)1 ACTIVE (com.hazelcast.cp.internal.raft.impl.RaftNodeStatus.ACTIVE)1