Search in sources :

Example 16 with ApplyRaftRunnable

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

the class LinearizableQueryTest method when_newCommitIsDoneWhileThereAreMultipleQueries_then_allQueriesRunAfterCommit.

@Test(timeout = 300_000)
public void when_newCommitIsDoneWhileThereAreMultipleQueries_then_allQueriesRunAfterCommit() 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 queryFuture1 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    InternalCompletableFuture queryFuture2 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    group.resetAllRulesFrom(leader.getLocalMember());
    replicateFuture.get();
    assertEquals("value2", queryFuture1.get());
    assertEquals("value2", queryFuture2.get());
}
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 17 with ApplyRaftRunnable

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

the class LinearizableQueryTest method when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails.

@Test(timeout = 300_000)
public void when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails() throws Exception {
    group = newGroup();
    group.start();
    RaftNodeImpl oldLeader = group.waitUntilLeaderElected();
    final int[] split = group.createMajoritySplitIndexes(false);
    group.split(split);
    InternalCompletableFuture queryFuture = oldLeader.query(new QueryRaftRunnable(), LINEARIZABLE);
    assertTrueEventually(() -> {
        for (int ix : split) {
            RaftEndpoint newLeader = getLeaderMember(group.getNode(ix));
            assertNotNull(newLeader);
            assertNotEquals(oldLeader.getLocalMember(), newLeader);
        }
    });
    RaftNodeImpl newLeader = group.getNode(getLeaderMember(group.getNode(split[0])));
    newLeader.replicate(new ApplyRaftRunnable("value1")).get();
    group.merge();
    group.waitUntilLeaderElected();
    assertEquals(oldLeader.getLeader(), newLeader.getLocalMember());
    try {
        queryFuture.joinInternal();
        fail();
    } catch (LeaderDemotedException ignored) {
    }
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) 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 18 with ApplyRaftRunnable

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

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

the class LocalRaftTest method when_thereAreTooManyInflightAppendedEntries_then_newAppendsAreRejected.

@Test
public void when_thereAreTooManyInflightAppendedEntries_then_newAppendsAreRejected() {
    int uncommittedEntryCount = 10;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setUncommittedEntryCountToRejectNewAppends(uncommittedEntryCount);
    group = newGroup(2, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl follower = group.getAnyFollowerNode();
    group.terminateNode(follower.getLocalMember());
    for (int i = 0; i < uncommittedEntryCount; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i));
    }
    try {
        leader.replicate(new ApplyRaftRunnable("valFinal")).joinInternal();
        fail();
    } catch (CannotReplicateException ignored) {
    }
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) 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 20 with ApplyRaftRunnable

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

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