Search in sources :

Example 26 with RaftDataService

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

the class LocalRaftTest method when_leaderAppendsToMinority_then_itCannotCommit.

@Test
public void when_leaderAppendsToMinority_then_itCannotCommit() throws ExecutionException, InterruptedException {
    group = newGroup(5);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    for (int i = 1; i < followers.length; i++) {
        group.dropMessagesToMember(leader.getLocalMember(), followers[i].getLocalMember(), AppendRequest.class);
    }
    Future f = leader.replicate(new ApplyRaftRunnable("val"));
    assertTrueEventually(() -> {
        assertEquals(1, getLastLogOrSnapshotEntry(leader).index());
        assertEquals(1, getLastLogOrSnapshotEntry(followers[0]).index());
    });
    try {
        f.get(10, TimeUnit.SECONDS);
        fail();
    } catch (TimeoutException ignored) {
    }
    for (RaftNodeImpl raftNode : group.getNodes()) {
        assertEquals(0, getCommitIndex(raftNode));
        RaftDataService service = group.getIntegration(raftNode.getLocalMember()).getService();
        assertEquals(0, service.size());
    }
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) TimeoutException(java.util.concurrent.TimeoutException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 27 with RaftDataService

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

the class LocalRaftTest method when_followerSlowsDown_then_itCatchesLeaderEventually.

@Test
public void when_followerSlowsDown_then_itCatchesLeaderEventually() throws ExecutionException, InterruptedException {
    final int entryCount = 100;
    group = newGroup(3, newRaftConfigWithNoSnapshotting(entryCount));
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl slowFollower = group.getAnyFollowerNode();
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    for (int i = 0; i < entryCount; i++) {
        Object val = "val" + i;
        leader.replicate(new ApplyRaftRunnable(val)).get();
    }
    assertEquals(0, getCommitIndex(slowFollower));
    group.resetAllRulesFrom(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(entryCount, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(entryCount, 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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 28 with RaftDataService

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

the class SnapshotTest method when_followerMissesAFewEntriesBeforeTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot.

@Test
public void when_followerMissesAFewEntriesBeforeTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    // entryCount * 0.1 - 2
    final int missingEntryCountOnSlowFollower = 4;
    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 - missingEntryCountOnSlowFollower; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl follower : group.getNodesExcept(leader.getLocalMember())) {
            assertEquals(entryCount - missingEntryCountOnSlowFollower, getMatchIndex(leader, follower.getLocalMember()));
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
    for (int i = entryCount - missingEntryCountOnSlowFollower; 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(), 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 29 with RaftDataService

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

the class SnapshotTest method when_isolatedLeaderAppendsEntries_then_itInvalidatesTheirFeaturesUponInstallSnapshot.

@Test
public void when_isolatedLeaderAppendsEntries_then_itInvalidatesTheirFeaturesUponInstallSnapshot() throws ExecutionException, InterruptedException {
    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());
    for (int i = 0; i < 40; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(40, getCommitIndex(raftNode));
        }
    });
    group.split(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
            assertNotNull(leaderEndpoint);
            assertNotEquals(leader.getLocalMember(), leaderEndpoint);
        }
    });
    List<InternalCompletableFuture> futures = new ArrayList<>();
    for (int i = 40; i < 45; i++) {
        InternalCompletableFuture f = leader.replicate(new ApplyRaftRunnable("isolated" + i));
        futures.add(f);
    }
    RaftNodeImpl newLeader = group.getNode(getLeaderMember(followers[0]));
    for (int i = 40; i < 51; i++) {
        newLeader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            assertTrue(getSnapshotEntry(raftNode).index() > 0);
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    group.merge();
    for (InternalCompletableFuture f : futures) {
        try {
            f.joinInternal();
            fail();
        } catch (StaleAppendRequestException ignored) {
        }
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(51, getCommitIndex(raftNode));
            RaftDataService service = group.getService(raftNode);
            assertEquals(51, service.size());
            for (int i = 0; i < 51; i++) {
                assertEquals(("val" + i), service.get(i + 1));
            }
        }
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) StaleAppendRequestException(com.hazelcast.cp.exception.StaleAppendRequestException) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) ArrayList(java.util.ArrayList) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 30 with RaftDataService

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

the class SnapshotTest method when_commitLogAdvances_then_snapshotIsTaken.

@Test
public void when_commitLogAdvances_then_snapshotIsTaken() 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());
            RaftDataService service = group.getService(raftNode);
            assertEquals(entryCount, service.size());
            for (int i = 0; i < entryCount; i++) {
                assertEquals(("val" + i), service.get(i + 1));
            }
        }
    });
}
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