Search in sources :

Example 6 with RaftDataService

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

the class SnapshotTest method when_followerMissesTheLastEntryThatGoesIntoTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot.

@Test
public void when_followerMissesTheLastEntryThatGoesIntoTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl slowFollower = followers[1];
    for (int i = 0; i < entryCount - 1; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl follower : group.getNodesExcept(leader.getLocalMember())) {
            assertEquals(entryCount - 1, getMatchIndex(leader, follower.getLocalMember()));
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
    leader.replicate(new ApplyRaftRunnable("val" + (entryCount - 1))).get();
    assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
    leader.replicate(new ApplyRaftRunnable("valFinal")).get();
    group.allowMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    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 7 with RaftDataService

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

the class LocalRaftTest method testSingleCommitEntry.

private void testSingleCommitEntry(final int nodeCount) throws Exception {
    group = newGroup(nodeCount);
    group.start();
    group.waitUntilLeaderElected();
    Object val = "val";
    Object result = group.getLeaderNode().replicate(new ApplyRaftRunnable(val)).get();
    assertEquals(result, val);
    int commitIndex = 1;
    assertTrueEventually(() -> {
        for (int i = 0; i < nodeCount; i++) {
            RaftNodeImpl node = group.getNode(i);
            long index = getCommitIndex(node);
            assertEquals(commitIndex, index);
            RaftDataService service = group.getIntegration(i).getService();
            Object actual = service.get(commitIndex);
            assertEquals(val, actual);
        }
    });
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)

Example 8 with RaftDataService

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

the class LocalRaftTest method testReplicateEntriesConcurrently.

private void testReplicateEntriesConcurrently(int nodeCount) throws ExecutionException, InterruptedException {
    final int entryCount = 100;
    group = newGroup(nodeCount, newRaftConfigWithNoSnapshotting(entryCount));
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    List<Future> futures = new ArrayList<>(entryCount);
    for (int i = 0; i < entryCount; i++) {
        Object val = "val" + i;
        futures.add(leader.replicate(new ApplyRaftRunnable(val)));
    }
    for (Future f : futures) {
        f.get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(entryCount, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(100, service.size());
            Set<Object> values = service.values();
            for (int i = 0; i < entryCount; i++) {
                Object val = "val" + i;
                assertTrue(values.contains(val));
            }
        }
    });
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) ArrayList(java.util.ArrayList) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future)

Example 9 with RaftDataService

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

the class LocalRaftTest method when_leaderCrashes_then_theFollowerWithLongestLogMayNotBecomeLeaderIfItsLogIsNotMajority.

@Test
public void when_leaderCrashes_then_theFollowerWithLongestLogMayNotBecomeLeaderIfItsLogIsNotMajority() throws Exception {
    group = newGroup(5);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val1")).get();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl followerWithLongestLog = followers[0];
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(commitIndex, getCommitIndex(raftNode));
        }
    });
    for (int i = 1; i < followers.length; i++) {
        group.dropMessagesToMember(leader.getLocalMember(), followers[i].getLocalMember(), AppendRequest.class);
    }
    leader.replicate(new ApplyRaftRunnable("val2"));
    assertTrueEventually(() -> assertTrue(getLastLogOrSnapshotEntry(followerWithLongestLog).index() > commitIndex));
    assertTrueAllTheTime(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(commitIndex, getCommitIndex(raftNode));
        }
    }, 10);
    group.dropMessagesToMember(followerWithLongestLog.getLocalMember(), followers[1].getLocalMember(), VoteRequest.class);
    group.dropMessagesToMember(followerWithLongestLog.getLocalMember(), followers[2].getLocalMember(), VoteRequest.class);
    group.terminateNode(leader.getLocalMember());
    RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    // followerWithLongestLog has 2 entries, other 3 followers have 1 entry
    // and those 3 followers will elect a leader among themselves
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            RaftEndpoint newLeader1 = getLeaderMember(raftNode);
            assertNotEquals(leader.getLocalMember(), newLeader1);
            assertNotEquals(followerWithLongestLog.getLocalMember(), newLeader1);
        }
    });
    for (int i = 1; i < followers.length; i++) {
        assertEquals(commitIndex, getCommitIndex(followers[i]));
        assertEquals(commitIndex, getLastLogOrSnapshotEntry(followers[i]).index());
    }
    // followerWithLongestLog does not truncate its extra log entry until the new leader appends a new entry
    assertTrue(getLastLogOrSnapshotEntry(followerWithLongestLog).index() > commitIndex);
    newLeader.replicate(new ApplyRaftRunnable("val3")).get();
    assertTrueEventually(() -> {
        for (RaftNodeImpl follower : followers) {
            assertEquals(2, getCommitIndex(follower));
            RaftDataService service = group.getService(follower);
            assertEquals(2, service.size());
            assertEquals("val1", service.get(1));
            assertEquals("val3", service.get(2));
        }
    });
    assertEquals(2, getLastLogOrSnapshotEntry(followerWithLongestLog).index());
}
Also used : 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 10 with RaftDataService

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

the class SnapshotTest method when_followersMatchIndexIsUnknown_then_itInstallsSnapshot.

@Test
public void when_followersMatchIndexIsUnknown_then_itInstallsSnapshot() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl slowFollower = followers[1];
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
    for (int i = 0; i < entryCount; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
    leader.replicate(new ApplyRaftRunnable("valFinal")).get();
    group.allowMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
    assertTrueEventually(() -> assertEquals(entryCount, getCommitIndex(slowFollower)));
    group.resetAllRulesFrom(leader.getLocalMember());
    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)

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