Search in sources :

Example 46 with ApplyRaftRunnable

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

the class DestroyRaftGroupTest method when_destroyOpIsAppendedButNotCommitted_then_cannotAppendNewEntry.

@Test
public void when_destroyOpIsAppendedButNotCommitted_then_cannotAppendNewEntry() throws ExecutionException, InterruptedException {
    group = newGroup(2);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl follower = group.getAnyFollowerNode();
    group.dropAllMessagesToMember(leader.getLocalMember(), follower.getLocalMember());
    leader.replicate(new DestroyRaftGroupCmd());
    try {
        leader.replicate(new ApplyRaftRunnable("val")).joinInternal();
        fail();
    } catch (CannotReplicateException ignored) {
    }
}
Also used : DestroyRaftGroupCmd(com.hazelcast.cp.internal.raft.command.DestroyRaftGroupCmd) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 47 with ApplyRaftRunnable

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

the class LinearizableQueryTest method when_newCommitIsDoneWhileThereIsWaitingQuery_then_queryRunsAfterNewCommit.

@Test(timeout = 300_000)
public void when_newCommitIsDoneWhileThereIsWaitingQuery_then_queryRunsAfterNewCommit() throws Exception {
    group = newGroup();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("value1")).get();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[2].getLocalMember(), AppendRequest.class);
    InternalCompletableFuture replicateFuture = leader.replicate(new ApplyRaftRunnable("value2"));
    InternalCompletableFuture queryFuture = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    group.resetAllRulesFrom(leader.getLocalMember());
    replicateFuture.get();
    Object o = queryFuture.get();
    assertEquals("value2", o);
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) QueryRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 48 with ApplyRaftRunnable

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

the class LocalRaftTest method testReplicateEntriesInParallel.

private void testReplicateEntriesInParallel(int nodeCount) throws InterruptedException {
    int threadCount = 10;
    final int opsPerThread = 10;
    group = newGroup(nodeCount, newRaftConfigWithNoSnapshotting(threadCount * opsPerThread));
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++) {
        int start = i * opsPerThread;
        threads[i] = new Thread(() -> {
            List<Future> futures = new ArrayList<>();
            for (int j = start; j < start + opsPerThread; j++) {
                futures.add(leader.replicate(new ApplyRaftRunnable(j)));
            }
            for (Future f : futures) {
                try {
                    f.get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    int entryCount = threadCount * opsPerThread;
    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++) {
                assertTrue(values.contains(i));
            }
        }
    });
}
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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 49 with ApplyRaftRunnable

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

the class LocalRaftTest method when_followerAttemptsToReplicate_then_itFails.

@Test
public void when_followerAttemptsToReplicate_then_itFails() throws ExecutionException, InterruptedException {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    try {
        followers[0].replicate(new ApplyRaftRunnable("val")).joinInternal();
        fail("NotLeaderException should have been thrown");
    } catch (NotLeaderException e) {
        ignore(e);
    }
    for (RaftNodeImpl raftNode : group.getNodes()) {
        RaftDataService service = group.getIntegration(raftNode.getLocalMember()).getService();
        assertEquals(0, service.size());
    }
}
Also used : NotLeaderException(com.hazelcast.cp.exception.NotLeaderException) 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 50 with ApplyRaftRunnable

use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable 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)

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