Search in sources :

Example 11 with RaftEndpoint

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

the class UpdateRaftGroupMembersCmd method readData.

@Override
public void readData(ObjectDataInput in) throws IOException {
    int count = in.readInt();
    Collection<RaftEndpoint> members = new LinkedHashSet<RaftEndpoint>();
    for (int i = 0; i < count; i++) {
        RaftEndpoint member = in.readObject();
        members.add(member);
    }
    this.members = members;
    this.member = in.readObject();
    this.mode = MembershipChangeMode.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 12 with RaftEndpoint

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

the class SnapshotEntry method writeData.

@Override
public void writeData(ObjectDataOutput out) throws IOException {
    super.writeData(out);
    out.writeLong(groupMembersLogIndex);
    out.writeInt(groupMembers.size());
    for (RaftEndpoint endpoint : groupMembers) {
        out.writeObject(endpoint);
    }
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 13 with RaftEndpoint

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

the class SnapshotEntry method readData.

@Override
public void readData(ObjectDataInput in) throws IOException {
    super.readData(in);
    groupMembersLogIndex = in.readLong();
    int count = in.readInt();
    groupMembers = new LinkedHashSet<RaftEndpoint>(count);
    for (int i = 0; i < count; i++) {
        RaftEndpoint endpoint = in.readObject();
        groupMembers.add(endpoint);
    }
    groupMembers = unmodifiableCollection(groupMembers);
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 14 with RaftEndpoint

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

the class VoteRequestHandlerTask method innerRun.

@Override
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" })
protected // Justification: It is easier to follow the RequestVoteRPC logic in a single method
void innerRun() {
    RaftState state = raftNode.state();
    RaftEndpoint localMember = localMember();
    // Reply false if last AppendEntries call was received less than election timeout ago (leader stickiness)
    // (Raft thesis - Section 4.2.3) This check conflicts with the leadership transfer mechanism,
    // in which a server legitimately starts an election without waiting an election timeout.
    // Those VoteRequest objects are marked with a special flag ("disruptive") to bypass leader stickiness.
    // Also if request comes from the current leader, then stickiness check is skipped.
    // Since current leader may have restarted by recovering its persistent state.
    long leaderElectionTimeoutDeadline = Clock.currentTimeMillis() - raftNode.getLeaderElectionTimeoutInMillis();
    if (!req.isDisruptive() && raftNode.lastAppendEntriesTimestamp() > leaderElectionTimeoutDeadline && !req.candidate().equals(state.leader())) {
        logger.info("Rejecting " + req + " since received append entries recently.");
        raftNode.send(new VoteResponse(localMember, state.term(), false), req.candidate());
        return;
    }
    // Reply false if term < currentTerm (§5.1)
    if (state.term() > req.term()) {
        logger.info("Rejecting " + req + " since current term: " + state.term() + " is bigger");
        raftNode.send(new VoteResponse(localMember, state.term(), false), req.candidate());
        return;
    }
    if (state.term() < req.term()) {
        // If RPC request or response contains term T > currentTerm: set currentTerm = T, convert to follower (§5.1)
        if (state.role() != FOLLOWER) {
            logger.info("Demoting to FOLLOWER after " + req + " since current term: " + state.term() + " is smaller");
        } else {
            logger.info("Moving to new term: " + req.term() + " from current term: " + state.term() + " after " + req);
        }
        raftNode.toFollower(req.term());
    }
    if (state.leader() != null && !req.candidate().equals(state.leader())) {
        logger.warning("Rejecting " + req + " since we have a leader: " + state.leader());
        raftNode.send(new VoteResponse(localMember, req.term(), false), req.candidate());
        return;
    }
    if (state.votedFor() != null) {
        boolean granted = (req.candidate().equals(state.votedFor()));
        if (granted) {
            logger.info("Vote granted for duplicate" + req);
        } else {
            logger.info("Duplicate " + req + ". currently voted-for: " + state.votedFor());
        }
        raftNode.send(new VoteResponse(localMember, req.term(), granted), 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 VoteResponse(localMember, req.term(), 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 VoteResponse(localMember, req.term(), false), req.candidate());
        return;
    }
    logger.info("Granted vote for " + req);
    state.persistVote(req.term(), req.candidate());
    raftNode.send(new VoteResponse(localMember, req.term(), true), req.candidate());
}
Also used : VoteResponse(com.hazelcast.cp.internal.raft.impl.dto.VoteResponse) RaftState(com.hazelcast.cp.internal.raft.impl.state.RaftState) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftLog(com.hazelcast.cp.internal.raft.impl.log.RaftLog)

Example 15 with RaftEndpoint

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

the class CPGroupSummary method writeData.

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

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