Search in sources :

Example 26 with ApplyRaftRunnable

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

the class MembershipChangeTest method when_newRaftNodeJoins_then_itAppendsMissingEntries.

@Test
public void when_newRaftNodeJoins_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val")).get();
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
    RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
    assertTrueEventually(() -> {
        RaftNodeImpl[] nodes = group.getNodes();
        for (RaftNodeImpl raftNode : nodes) {
            assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
            assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
            assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
        }
    });
    RaftDataService service = group.getService(newRaftNode);
    assertEquals(1, service.size());
    assertTrue(service.values().contains("val"));
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) RaftGroupMembers(com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 27 with ApplyRaftRunnable

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

the class MembershipChangeTest method when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate.

@Test
public void when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate() throws ExecutionException, InterruptedException {
    group = new LocalRaftGroupBuilder(3).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val1")).get();
    long oldLeaderCommitIndexBeforeMemberhipChange = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl follower : followers) {
            assertEquals(oldLeaderCommitIndexBeforeMemberhipChange, getCommitIndex(follower));
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD);
    assertTrueEventually(() -> {
        long leaderLastLogIndex = getLastLogOrSnapshotEntry(leader).index();
        assertTrue(leaderLastLogIndex > oldLeaderCommitIndexBeforeMemberhipChange);
        assertEquals(leaderLastLogIndex, getLastLogOrSnapshotEntry(newRaftNode).index());
    });
    group.dropMessagesToAll(newRaftNode.getLocalMember(), PreVoteRequest.class);
    group.dropMessagesToAll(newRaftNode.getLocalMember(), VoteRequest.class);
    group.terminateNode(leader.getLocalMember());
    assertTrueEventually(() -> {
        assertNotNull(followers[0].getLeader());
        assertNotNull(followers[1].getLeader());
        assertNotEquals(leader.getLocalMember(), followers[0].getLeader());
        assertNotEquals(leader.getLocalMember(), followers[1].getLeader());
        assertEquals(followers[0].getLeader(), followers[1].getLeader());
    });
    RaftNodeImpl newLeader = group.getNode(followers[0].getLeader());
    newLeader.replicate(new ApplyRaftRunnable("val1")).get();
    newLeader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(newLeader), getCommitIndex(newRaftNode));
        assertEquals(getCommittedGroupMembers(newLeader).index(), getCommittedGroupMembers(newRaftNode).index());
    });
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 28 with ApplyRaftRunnable

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

the class MembershipChangeTest method when_newRaftNodeJoinsAfterAnotherNodeLeaves_then_itAppendsMissingEntries.

@Test
public void when_newRaftNodeJoinsAfterAnotherNodeLeaves_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val")).get();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl leavingFollower = followers[0];
    RaftNodeImpl stayingFollower = followers[1];
    leader.replicateMembershipChange(leavingFollower.getLocalMember(), REMOVE).get();
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
    RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : asList(leader, stayingFollower, newRaftNode)) {
            assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
            assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
            assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
            assertFalse(getLastGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
            assertFalse(getCommittedGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
        }
    });
    RaftDataService service = group.getService(newRaftNode);
    assertEquals(1, service.size());
    assertTrue(service.values().contains("val"));
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) RaftGroupMembers(com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 29 with ApplyRaftRunnable

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

the class MembershipChangeTest method when_newRaftNodeJoinsAfterAnotherNodeLeavesAndSnapshotIsTaken_then_itAppendsMissingEntries.

@Test
public void when_newRaftNodeJoinsAfterAnotherNodeLeavesAndSnapshotIsTaken_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
    int commitIndexAdvanceCountToSnapshot = 10;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitIndexAdvanceCountToSnapshot);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val")).get();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl leavingFollower = followers[0];
    RaftNodeImpl stayingFollower = followers[1];
    leader.replicateMembershipChange(leavingFollower.getLocalMember(), REMOVE).get();
    for (int i = 0; i < commitIndexAdvanceCountToSnapshot; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> assertTrue(getSnapshotEntry(leader).index() > 0));
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
    RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : asList(leader, stayingFollower, newRaftNode)) {
            assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
            assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
            assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
            assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
            assertFalse(getLastGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
            assertFalse(getCommittedGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
        }
    });
    RaftDataService service = group.getService(newRaftNode);
    assertEquals(commitIndexAdvanceCountToSnapshot + 1, service.size());
    assertTrue(service.values().contains("val"));
    for (int i = 0; i < commitIndexAdvanceCountToSnapshot; i++) {
        assertTrue(service.values().contains("val" + i));
    }
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) RaftGroupMembers(com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 30 with ApplyRaftRunnable

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

the class LocalRaftTest method testReplicateEntriesSequentially.

private void testReplicateEntriesSequentially(int nodeCount) throws ExecutionException, InterruptedException {
    final int entryCount = 100;
    group = newGroup(nodeCount, newRaftConfigWithNoSnapshotting(entryCount));
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i < entryCount; i++) {
        Object val = "val" + i;
        leader.replicate(new ApplyRaftRunnable(val)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(entryCount, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(100, service.size());
            for (int i = 0; i < entryCount; i++) {
                int commitIndex = i + 1;
                Object val = "val" + i;
                assertEquals(val, service.get(commitIndex));
            }
        }
    });
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)

Aggregations

ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)55 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)50 QuickTest (com.hazelcast.test.annotation.QuickTest)50 Test (org.junit.Test)50 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)31 RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)23 LocalRaftGroupBuilder (com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder)15 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)15 RestoredRaftState (com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState)14 InMemoryRaftStateStore (com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore)10 Future (java.util.concurrent.Future)8 ArrayList (java.util.ArrayList)5 QueryRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable)4 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)4 RaftGroupMembers (com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers)4 StaleAppendRequestException (com.hazelcast.cp.exception.StaleAppendRequestException)3 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)2 LeaderDemotedException (com.hazelcast.cp.exception.LeaderDemotedException)2 DestroyRaftGroupCmd (com.hazelcast.cp.internal.raft.command.DestroyRaftGroupCmd)2 RaftUtil.getSnapshotEntry (com.hazelcast.cp.internal.raft.impl.RaftUtil.getSnapshotEntry)2