Search in sources :

Example 11 with RaftDataService

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

the class SnapshotTest method when_snapshotIsTaken_then_nextEntryIsCommitted.

@Test
public void when_snapshotIsTaken_then_nextEntryIsCommitted() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i < entryCount; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(entryCount, getCommitIndex(raftNode));
            assertEquals(entryCount, getSnapshotEntry(raftNode).index());
        }
    });
    leader.replicate(new ApplyRaftRunnable("valFinal")).get();
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(entryCount + 1, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(entryCount + 1, service.size());
            for (int i = 0; i < entryCount; i++) {
                assertEquals(("val" + i), service.get(i + 1));
            }
            assertEquals("valFinal", service.get(51));
        }
    });
}
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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with RaftDataService

use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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 13 with RaftDataService

use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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 14 with RaftDataService

use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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 15 with RaftDataService

use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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)30 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)30 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)25 QuickTest (com.hazelcast.test.annotation.QuickTest)25 Test (org.junit.Test)25 RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)13 RestoredRaftState (com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState)6 InMemoryRaftStateStore (com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore)6 LocalRaftGroupBuilder (com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder)6 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)5 ArrayList (java.util.ArrayList)4 RaftGroupMembers (com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers)3 Future (java.util.concurrent.Future)3 TimeoutException (java.util.concurrent.TimeoutException)2 LeaderDemotedException (com.hazelcast.cp.exception.LeaderDemotedException)1 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)1 StaleAppendRequestException (com.hazelcast.cp.exception.StaleAppendRequestException)1 List (java.util.List)1