Search in sources :

Example 11 with LocalRaftGroupBuilder

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

the class MembershipChangeTest method when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate.

@Test
public void when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate() throws ExecutionException, InterruptedException {
    group = new LocalRaftGroupBuilder(3).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val1")).get();
    long oldLeaderCommitIndexBeforeMemberhipChange = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl follower : followers) {
            assertEquals(oldLeaderCommitIndexBeforeMemberhipChange, getCommitIndex(follower));
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    RaftNodeImpl newRaftNode = group.createNewRaftNode();
    leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD);
    assertTrueEventually(() -> {
        long leaderLastLogIndex = getLastLogOrSnapshotEntry(leader).index();
        assertTrue(leaderLastLogIndex > oldLeaderCommitIndexBeforeMemberhipChange);
        assertEquals(leaderLastLogIndex, getLastLogOrSnapshotEntry(newRaftNode).index());
    });
    group.dropMessagesToAll(newRaftNode.getLocalMember(), PreVoteRequest.class);
    group.dropMessagesToAll(newRaftNode.getLocalMember(), VoteRequest.class);
    group.terminateNode(leader.getLocalMember());
    assertTrueEventually(() -> {
        assertNotNull(followers[0].getLeader());
        assertNotNull(followers[1].getLeader());
        assertNotEquals(leader.getLocalMember(), followers[0].getLeader());
        assertNotEquals(leader.getLocalMember(), followers[1].getLeader());
        assertEquals(followers[0].getLeader(), followers[1].getLeader());
    });
    RaftNodeImpl newLeader = group.getNode(followers[0].getLeader());
    newLeader.replicate(new ApplyRaftRunnable("val1")).get();
    newLeader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(newLeader), getCommitIndex(newRaftNode));
        assertEquals(getCommittedGroupMembers(newLeader).index(), getCommittedGroupMembers(newRaftNode).index());
    });
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) 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 12 with LocalRaftGroupBuilder

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

the class MembershipChangeTest method when_appendNopEntryOnLeaderElection_then_canMakeMemberChangeAfterNopEntryCommitted.

@Test
public void when_appendNopEntryOnLeaderElection_then_canMakeMemberChangeAfterNopEntryCommitted() {
    // https://groups.google.com/forum/#!msg/raft-dev/t4xj6dJTP6E/d2D9LrWRza8J
    group = new LocalRaftGroupBuilder(3).setAppendNopEntryOnLeaderElection(true).build();
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    assertTrueEventually(() -> {
        // may fail until nop-entry is committed
        try {
            leader.replicateMembershipChange(leader.getLocalMember(), MembershipChangeMode.REMOVE).get();
        } catch (CannotReplicateException e) {
            fail(e.getMessage());
        }
    });
}
Also used : 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 13 with LocalRaftGroupBuilder

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

the class PersistenceTest method when_followerIsRestarted_then_itRestoresItsRaftStateWithSnapshot.

@Test
public void when_followerIsRestarted_then_itRestoresItsRaftStateWithSnapshot() 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();
    final RaftNodeImpl leader = group.waitUntilLeaderElected();
    for (int i = 0; i <= committedEntryCountToSnapshot; i++) {
        leader.replicate(new ApplyRaftRunnable("val" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl node : group.getNodes()) {
            assertTrue(getSnapshotEntry(node).index() > 0);
        }
    });
    RaftNodeImpl terminatedFollower = group.getAnyFollowerNode();
    RaftEndpoint terminatedEndpoint = terminatedFollower.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(terminatedFollower);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    group.terminateNode(terminatedEndpoint);
    leader.replicate(new ApplyRaftRunnable("val" + (committedEntryCountToSnapshot + 1))).get();
    final RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    assertEquals(new ArrayList<>(getCommittedGroupMembers(leader).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
    assertEquals(new ArrayList<>(getLastGroupMembers(leader).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    assertTrueEventually(() -> {
        assertEquals(leader.getLocalMember(), restartedNode.getLeader());
        assertEquals(getTerm(leader), getTerm(restartedNode));
        assertEquals(getCommitIndex(leader), getCommitIndex(restartedNode));
        assertEquals(getLastApplied(leader), getLastApplied(restartedNode));
        RaftDataService service = group.getService(restartedNode);
        Object[] values = service.valuesArray();
        assertThat(values, arrayWithSize(committedEntryCountToSnapshot + 2));
        for (int i = 0; i <= committedEntryCountToSnapshot + 1; 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 14 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_itBecomesLeaderAndAppliesPreviouslyCommittedMemberListViaSnapshot.

@Test
public void when_leaderIsRestarted_then_itBecomesLeaderAndAppliesPreviouslyCommittedMemberListViaSnapshot() throws ExecutionException, InterruptedException {
    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();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl removedFollower = followers[0];
    final RaftNodeImpl runningFollower = followers[1];
    group.terminateNode(removedFollower.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val")).get();
    leader.replicateMembershipChange(removedFollower.getLocalMember(), REMOVE).get();
    while (getSnapshotEntry(leader).index() == 0) {
        leader.replicate(new ApplyRaftRunnable("val")).get();
    }
    RaftEndpoint terminatedEndpoint = leader.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(leader);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    // Block voting between followers
    // to avoid a leader election before leader restarts.
    blockVotingBetweenFollowers();
    group.terminateNode(terminatedEndpoint);
    RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    assertSame(restartedNode, newLeader);
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(runningFollower), getCommitIndex(restartedNode));
        assertEquals(new ArrayList<>(getCommittedGroupMembers(runningFollower).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
        assertEquals(new ArrayList<>(getLastGroupMembers(runningFollower).members()), new ArrayList<>(getLastGroupMembers(restartedNode).members()));
    });
}
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) 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 15 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_itRestoresItsRaftStateAndBecomesLeader.

@Test
public void when_leaderIsRestarted_then_itRestoresItsRaftStateAndBecomesLeader() 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();
    }
    final int term = getTerm(leader);
    final long commitIndex = getCommitIndex(leader);
    RaftEndpoint terminatedEndpoint = leader.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(leader);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    // Block voting between followers
    // to avoid a leader election before leader restarts.
    blockVotingBetweenFollowers();
    group.terminateNode(terminatedEndpoint);
    RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    assertSame(newLeader, restartedNode);
    assertTrueEventually(() -> {
        assertTrue(getTerm(restartedNode) > term);
        assertEquals(commitIndex + 1, getCommitIndex(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)

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