Search in sources :

Example 1 with RaftEndpoint

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

the class MetadataRaftGroupTest method when_metadataClusterNodeFallsFarBehind_then_itInstallsSnapshot.

@Test
public void when_metadataClusterNodeFallsFarBehind_then_itInstallsSnapshot() {
    int nodeCount = 3;
    int commitCountToSnapshot = 5;
    Config config = createConfig(nodeCount, nodeCount);
    config.getCPSubsystemConfig().getRaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitCountToSnapshot);
    HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        instances[i] = factory.newHazelcastInstance(config);
    }
    waitUntilCPDiscoveryCompleted(instances);
    waitAllForLeaderElection(instances, getMetadataGroupId(instances[0]));
    RaftEndpoint leaderEndpoint = getLeaderMember(getRaftNode(instances[0], getMetadataGroupId(instances[0])));
    HazelcastInstance leader = getInstance(leaderEndpoint);
    HazelcastInstance follower = getRandomFollowerInstance(instances, getMetadataGroupId(instances[0]));
    dropOperationsBetween(leader, follower, RaftServiceDataSerializerHook.F_ID, singletonList(APPEND_REQUEST_OP));
    List<CPGroupId> groupIds = new ArrayList<>();
    for (int i = 0; i < commitCountToSnapshot; i++) {
        CPGroupId groupId = createNewRaftGroup(leader, "id" + i, nodeCount);
        groupIds.add(groupId);
    }
    assertTrueEventually(() -> assertTrue(getSnapshotEntry(getRaftNode(leader, getMetadataGroupId(leader))).index() > 0));
    assertTrueEventually(() -> {
        for (CPGroupId groupId : groupIds) {
            assertNotNull(getRaftNode(follower, groupId));
        }
    });
}
Also used : CPGroupId(com.hazelcast.cp.CPGroupId) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ArrayList(java.util.ArrayList) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with RaftEndpoint

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

the class HazelcastRaftTestSupport method getLeaderInstance.

protected static HazelcastInstance getLeaderInstance(HazelcastInstance[] instances, CPGroupId groupId) {
    RaftNodeImpl[] raftNodeRef = new RaftNodeImpl[1];
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftNodeImpl raftNode = getRaftNode(instance, groupId);
            if (raftNode != null) {
                raftNodeRef[0] = raftNode;
                return;
            }
        }
        fail();
    });
    RaftNodeImpl raftNode = raftNodeRef[0];
    waitUntilLeaderElected(raftNode);
    RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
    assertNotNull(leaderEndpoint);
    for (HazelcastInstance instance : instances) {
        CPMember cpMember = instance.getCPSubsystem().getLocalCPMember();
        if (cpMember != null && leaderEndpoint.getUuid().equals(cpMember.getUuid())) {
            return instance;
        }
    }
    throw new AssertionError();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CPMember(com.hazelcast.cp.CPMember)

Example 3 with RaftEndpoint

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

the class RaftState method updateGroupMembers.

/**
 * Initializes the last applied group members with the members and logIndex.
 * This method expects there's no uncommitted membership changes, committed members are the same as
 * the last applied members.
 *
 * Leader state is updated for the members which don't exist in committed members and committed members
 * those don't exist in latest applied members are removed.
 *
 * @param logIndex log index of membership change
 * @param members latest applied members
 */
public void updateGroupMembers(long logIndex, Collection<RaftEndpoint> members) {
    assert committedGroupMembers == lastGroupMembers : "Cannot update group members to: " + members + " at log index: " + logIndex + " because last group members: " + lastGroupMembers + " is different than committed group members: " + committedGroupMembers;
    assert lastGroupMembers.index() < logIndex : "Cannot update group members to: " + members + " at log index: " + logIndex + " because last group members: " + lastGroupMembers + " has a bigger log index.";
    RaftGroupMembers newGroupMembers = new RaftGroupMembers(logIndex, members, localEndpoint);
    committedGroupMembers = lastGroupMembers;
    lastGroupMembers = newGroupMembers;
    if (leaderState != null) {
        for (RaftEndpoint endpoint : members) {
            if (!committedGroupMembers.isKnownMember(endpoint)) {
                leaderState.add(endpoint, log.lastLogOrSnapshotIndex());
            }
        }
        for (RaftEndpoint endpoint : committedGroupMembers.remoteMembers()) {
            if (!members.contains(endpoint)) {
                leaderState.remove(endpoint);
            }
        }
    }
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 4 with RaftEndpoint

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

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

the class CreateRaftNodeOp method readInternal.

@Override
protected void readInternal(ObjectDataInput in) throws IOException {
    super.readInternal(in);
    groupId = in.readObject();
    int count = in.readInt();
    initialMembers = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        RaftEndpoint member = in.readObject();
        initialMembers.add(member);
    }
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

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