Search in sources :

Example 6 with RaftEndpoint

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

the class CreateRaftNodeOp method writeInternal.

@Override
protected void writeInternal(ObjectDataOutput out) throws IOException {
    super.writeInternal(out);
    out.writeObject(groupId);
    out.writeInt(initialMembers.size());
    for (RaftEndpoint member : initialMembers) {
        out.writeObject(member);
    }
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 7 with RaftEndpoint

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

the class CreateRaftGroupOp method readData.

@Override
public void readData(ObjectDataInput in) throws IOException {
    groupName = in.readString();
    int len = in.readInt();
    members = new ArrayList<>(len);
    for (int i = 0; i < len; i++) {
        RaftEndpoint member = in.readObject();
        members.add(member);
    }
    groupId = in.readLong();
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 8 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint 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 9 with RaftEndpoint

use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint 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 10 with RaftEndpoint

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

Aggregations

RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)57 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)14 QuickTest (com.hazelcast.test.annotation.QuickTest)14 Test (org.junit.Test)14 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)8 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)7 ArrayList (java.util.ArrayList)7 CPMember (com.hazelcast.cp.CPMember)6 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)6 UUID (java.util.UUID)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 CPGroupId (com.hazelcast.cp.CPGroupId)5 TestRaftEndpoint (com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint)5 LinkedHashSet (java.util.LinkedHashSet)4 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)2 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 CannotCreateRaftGroupException (com.hazelcast.cp.internal.exception.CannotCreateRaftGroupException)2 RaftStateStore (com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2