Search in sources :

Example 6 with LocalRaftGroupBuilder

use of com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder in project hazelcast by hazelcast.

the class PersistenceTest method testSnapshotIsPersisted.

@Test
public void testSnapshotIsPersisted() throws ExecutionException, InterruptedException {
    final int committedEntryCountToSnapshot = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(committedEntryCountToSnapshot);
    group = new LocalRaftGroupBuilder(3, config).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    final RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i < committedEntryCountToSnapshot; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        assertEquals(committedEntryCountToSnapshot, getSnapshotEntry(leader).index());
        for (RaftNodeImpl node : group.getNodes()) {
            RestoredRaftState restoredState = getRestoredState(node);
            SnapshotEntry snapshot = restoredState.snapshot();
            assertNotNull(snapshot);
            assertEquals(committedEntryCountToSnapshot, snapshot.index());
        }
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) RaftUtil.getSnapshotEntry(com.hazelcast.cp.internal.raft.impl.RaftUtil.getSnapshotEntry) SnapshotEntry(com.hazelcast.cp.internal.raft.impl.log.SnapshotEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with LocalRaftGroupBuilder

use of com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder in project hazelcast by hazelcast.

the class PersistenceTest method when_leaderIsRestarted_then_itBecomesFollowerAndRestoresItsRaftStateWithSnapshot.

@Test
public void when_leaderIsRestarted_then_itBecomesFollowerAndRestoresItsRaftStateWithSnapshot() throws ExecutionException, InterruptedException {
    final int committedEntryCountToSnapshot = 50;
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(committedEntryCountToSnapshot);
    group = new LocalRaftGroupBuilder(3, config).setAppendNopEntryOnLeaderElection(true).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i <= committedEntryCountToSnapshot; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrue(getSnapshotEntry(leader).index() > 0);
    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(committedEntryCountToSnapshot + 1));
        for (int i = 0; i <= committedEntryCountToSnapshot; i++) {
            assertEquals("val" + i, values[i]);
        }
    });
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) 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 8 with LocalRaftGroupBuilder

use of com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder in project hazelcast by hazelcast.

the class PersistenceTest method testCommittedEntriesArePersisted.

@Test
public void testCommittedEntriesArePersisted() throws ExecutionException, InterruptedException {
    group = new LocalRaftGroupBuilder(3).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).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();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl node : group.getNodes()) {
            RestoredRaftState restoredState = getRestoredState(node);
            LogEntry[] entries = restoredState.entries();
            assertEquals(count, entries.length);
            for (int i = 0; i < count; i++) {
                LogEntry entry = entries[i];
                assertEquals(i + 1, entry.index());
                assertEquals("val" + i, ((ApplyRaftRunnable) entry.operation()).getVal());
            }
        }
    });
}
Also used : RestoredRaftState(com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with LocalRaftGroupBuilder

use of com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder in project hazelcast by hazelcast.

the class LinearizableQueryTest method when_multipleQueryLimitIsReachedBeforeHeartbeatAcks_then_noNewQueryIsAccepted.

@Test(timeout = 300_000)
public void when_multipleQueryLimitIsReachedBeforeHeartbeatAcks_then_noNewQueryIsAccepted() throws Exception {
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setUncommittedEntryCountToRejectNewAppends(1);
    group = new LocalRaftGroupBuilder(5, config).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    assertTrueEventually(() -> assertThat(getCommitIndex(leader), greaterThanOrEqualTo(1L)));
    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 queryFuture1 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    InternalCompletableFuture queryFuture2 = leader.query(new QueryRaftRunnable(), LINEARIZABLE);
    try {
        queryFuture2.joinInternal();
        fail();
    } catch (CannotReplicateException ignored) {
    }
    group.resetAllRulesFrom(leader.getLocalMember());
    queryFuture1.get();
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) QueryRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) CannotReplicateException(com.hazelcast.cp.exception.CannotReplicateException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with LocalRaftGroupBuilder

use of com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder in project hazelcast by hazelcast.

the class LeadershipTransferTest method testCannotTransferLeadershipWhileChangingMemberList.

@Test
public void testCannotTransferLeadershipWhileChangingMemberList() throws Exception {
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setLeaderHeartbeatPeriodInMillis(SECONDS.toMillis(30));
    group = new LocalRaftGroupBuilder(3, config).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new NopEntry()).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);
    leader.replicateMembershipChange(followers[0].getLocalMember(), MembershipChangeMode.REMOVE);
    InternalCompletableFuture f = leader.transferLeadership(followers[0].getLocalMember());
    exceptionRule.expect(IllegalStateException.class);
    f.joinInternal();
}
Also used : RaftAlgorithmConfig(com.hazelcast.config.cp.RaftAlgorithmConfig) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) LocalRaftGroupBuilder(com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder) NopEntry(com.hazelcast.cp.internal.raft.impl.testing.NopEntry) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

LocalRaftGroupBuilder (com.hazelcast.cp.internal.raft.impl.testing.LocalRaftGroup.LocalRaftGroupBuilder)20 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)20 QuickTest (com.hazelcast.test.annotation.QuickTest)20 Test (org.junit.Test)20 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)15 RestoredRaftState (com.hazelcast.cp.internal.raft.impl.persistence.RestoredRaftState)15 InMemoryRaftStateStore (com.hazelcast.cp.internal.raft.impl.testing.InMemoryRaftStateStore)10 RaftAlgorithmConfig (com.hazelcast.config.cp.RaftAlgorithmConfig)9 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)6 CannotReplicateException (com.hazelcast.cp.exception.CannotReplicateException)3 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)3 RaftUtil.getSnapshotEntry (com.hazelcast.cp.internal.raft.impl.RaftUtil.getSnapshotEntry)1 QueryRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable)1 SnapshotEntry (com.hazelcast.cp.internal.raft.impl.log.SnapshotEntry)1 NopEntry (com.hazelcast.cp.internal.raft.impl.testing.NopEntry)1 HashSet (java.util.HashSet)1