Search in sources :

Example 41 with RaftEndpoint

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

the class CPGroupInfo method readData.

@Override
public void readData(ObjectDataInput in) throws IOException {
    id = in.readObject();
    int initialMemberCount = in.readInt();
    Set<RaftEndpoint> initialMembers = new LinkedHashSet<>();
    for (int i = 0; i < initialMemberCount; i++) {
        RaftEndpoint member = in.readObject();
        initialMembers.add(member);
    }
    this.initialMembers = unmodifiableSet(initialMembers);
    membersCommitIndex = in.readLong();
    int memberCount = in.readInt();
    members = new LinkedHashSet<>(memberCount);
    for (int i = 0; i < memberCount; i++) {
        RaftEndpoint member = in.readObject();
        members.add(member);
    }
    members = unmodifiableSet(members);
    status = CPGroupStatus.valueOf(in.readString());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 42 with RaftEndpoint

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

the class UpdateRaftGroupMembersCmd method writeData.

@Override
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeInt(members.size());
    for (RaftEndpoint member : members) {
        out.writeObject(member);
    }
    out.writeObject(member);
    out.writeString(mode.name());
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 43 with RaftEndpoint

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

the class AbstractResponseHandlerTask method innerRun.

@Override
protected final void innerRun() {
    RaftEndpoint sender = sender();
    if (!raftNode.state().isKnownMember(sender)) {
        logger.warning("Won't run, since " + sender + " is unknown to us");
        return;
    }
    handleResponse();
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 44 with RaftEndpoint

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

the class PreVoteRequestHandlerTask method innerRun.

@Override
protected void innerRun() {
    RaftState state = raftNode.state();
    RaftEndpoint localEndpoint = localMember();
    // Reply false if term < currentTerm (ยง5.1)
    if (state.term() > req.nextTerm()) {
        logger.info("Rejecting " + req + " since current term: " + state.term() + " is bigger");
        raftNode.send(new PreVoteResponse(localEndpoint, state.term(), false), req.candidate());
        return;
    }
    // Reply false if last AppendEntries call was received less than election timeout ago (leader stickiness)
    if (raftNode.lastAppendEntriesTimestamp() > Clock.currentTimeMillis() - raftNode.getLeaderElectionTimeoutInMillis()) {
        logger.info("Rejecting " + req + " since received append entries recently.");
        raftNode.send(new PreVoteResponse(localEndpoint, state.term(), false), req.candidate());
        return;
    }
    RaftLog raftLog = state.log();
    if (raftLog.lastLogOrSnapshotTerm() > req.lastLogTerm()) {
        logger.info("Rejecting " + req + " since our last log term: " + raftLog.lastLogOrSnapshotTerm() + " is greater");
        raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), false), req.candidate());
        return;
    }
    if (raftLog.lastLogOrSnapshotTerm() == req.lastLogTerm() && raftLog.lastLogOrSnapshotIndex() > req.lastLogIndex()) {
        logger.info("Rejecting " + req + " since our last log index: " + raftLog.lastLogOrSnapshotIndex() + " is greater");
        raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), false), req.candidate());
        return;
    }
    logger.info("Granted pre-vote for " + req);
    raftNode.send(new PreVoteResponse(localEndpoint, req.nextTerm(), true), req.candidate());
}
Also used : RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) PreVoteResponse(com.hazelcast.cp.internal.raft.impl.dto.PreVoteResponse) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Example 45 with RaftEndpoint

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

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