Search in sources :

Example 21 with RaftDataService

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

the class PersistenceTest method when_leaderIsRestarted_then_itBecomesFollowerAndRestoresItsRaftState.

@Test
public void when_leaderIsRestarted_then_itBecomesFollowerAndRestoresItsRaftState() throws ExecutionException, InterruptedException {
    group = new LocalRaftGroupBuilder(3).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    final int count = 10;
    for (int i = 0; i < count; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    RaftEndpoint terminatedEndpoint = leader.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(leader);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    group.terminateNode(terminatedEndpoint);
    final RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    final RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    assertEquals(new ArrayList<>(getCommittedGroupMembers(newLeader).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
    assertEquals(new ArrayList<>(getLastGroupMembers(newLeader).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    assertTrueEventually(() -> {
        assertEquals(newLeader.getLocalMember(), restartedNode.getLeader());
        assertEquals(getTerm(newLeader), getTerm(restartedNode));
        assertEquals(getCommitIndex(newLeader), getCommitIndex(restartedNode));
        assertEquals(getLastApplied(newLeader), getLastApplied(restartedNode));
        RaftDataService service = group.getService(restartedNode);
        Object[] values = service.valuesArray();
        assertThat(values, arrayWithSize(count));
        for (int i = 0; i < count; i++) {
            assertEquals("val" + i, values[i]);
        }
    });
}
Also used : RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) InMemoryRaftStateStore(com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 22 with RaftDataService

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

the class SnapshotTest method when_leaderMissesInstallSnapshotResponse_then_itAdvancesMatchIndexWithNextInstallSnapshotResponse.

@Test
public void when_leaderMissesInstallSnapshotResponse_then_itAdvancesMatchIndexWithNextInstallSnapshotResponse() throws ExecutionException, InterruptedException {
    final int entryCount = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount).setAppendRequestBackoffTimeoutInMillis(1000);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl slowFollower = followers[1];
    // the leader cannot send AppendEntriesRPC to the follower
    group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
    // the follower cannot send append response to the leader after installing the snapshot
    group.dropMessagesToMember(slowFollower.getLocalMember(), leader.getLocalMember(), AppendSuccessResponse.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.resetAllRulesFrom(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodesExcept(slowFollower.getLocalMember())) {
            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));
        }
        assertEquals(entryCount, getCommitIndex(slowFollower));
        RaftDataService service = group.getService(slowFollower);
        assertEquals(entryCount, service.size());
        for (int i = 0; i < entryCount; i++) {
            assertEquals(("val" + i), service.get(i + 1));
        }
    });
    group.resetAllRulesFrom(slowFollower.getLocalMember());
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNode raftNode : group.getNodesExcept(leader.getLocalMember())) {
            assertEquals(commitIndex, getMatchIndex(leader, raftNode.getLocalMember()));
        }
    });
}
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 23 with RaftDataService

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

the class MembershipChangeTest method when_newJoiningNodeFirstReceivesSnapshot_then_itInstallsSnapshot.

@Test
public void when_newJoiningNodeFirstReceivesSnapshot_then_itInstallsSnapshot() throws ExecutionException, InterruptedException {
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(5);
    group = newGroup(3, config);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i < 4; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    group.dropMessagesToMember(leader.getLocalMember(), newRaftNode.getLocalMember(), AppendRequest.class);
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    assertTrueEventually(() -> assertTrue(getSnapshotEntry(leader).index() > 0));
    group.resetAllRulesFrom(leader.getLocalMember());
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(leader), getCommitIndex(newRaftNode));
        assertEquals(getLastGroupMembers(leader).members(), getLastGroupMembers(newRaftNode).members());
        assertEquals(getLastGroupMembers(leader).members(), getCommittedGroupMembers(newRaftNode).members());
        RaftDataService service = group.getService(newRaftNode);
        assertEquals(4, service.size());
    });
}
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 24 with RaftDataService

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

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

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