Search in sources :

Example 1 with ApplyRaftRunnable

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

the class LocalRaftTest method testNoCommitWhenOnlyLeaderAppends.

private void testNoCommitWhenOnlyLeaderAppends(int nodeCount) throws InterruptedException, ExecutionException {
    group = newGroup(nodeCount);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    group.dropMessagesToAll(leader.getLocalMember(), AppendRequest.class);
    try {
        leader.replicate(new ApplyRaftRunnable("val")).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) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ApplyRaftRunnable

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

the class LocalRaftTest method when_leaderTerminatesInMinority_then_clusterRemainsAvailable.

@Test
public void when_leaderTerminatesInMinority_then_clusterRemainsAvailable() throws Exception {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leaderNode = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leaderNode.getLocalMember());
    int leaderTerm = getTerm(leaderNode);
    group.terminateNode(leaderNode.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            RaftEndpoint newLeader = getLeaderMember(raftNode);
            assertNotNull(newLeader);
            assertNotEquals(leaderNode.getLocalMember(), newLeader);
        }
    });
    RaftNodeImpl newLeader = group.waitUntilLeaderElected();
    assertTrue(getTerm(newLeader) > leaderTerm);
    String value = "value";
    Future future = newLeader.replicate(new ApplyRaftRunnable(value));
    assertEquals(value, future.get());
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ApplyRaftRunnable

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

the class LocalRaftTest method when_followerTerminatesInMinority_then_clusterRemainsAvailable.

@Test
public void when_followerTerminatesInMinority_then_clusterRemainsAvailable() throws Exception {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leaderNode = group.waitUntilLeaderElected();
    int leaderIndex = group.getLeaderIndex();
    for (int i = 0; i < group.size(); i++) {
        if (i != leaderIndex) {
            group.terminateNode(i);
            break;
        }
    }
    String value = "value";
    Future future = leaderNode.replicate(new ApplyRaftRunnable(value));
    assertEquals(value, future.get());
}
Also used : ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with ApplyRaftRunnable

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

the class LocalRaftTest method when_followerBecomesLeaderWithUncommittedEntries_then_thoseEntriesAreCommittedWithANewEntryOfCurrentTerm.

@Test
public void when_followerBecomesLeaderWithUncommittedEntries_then_thoseEntriesAreCommittedWithANewEntryOfCurrentTerm() throws Exception {
    group = newGroup(3);
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val1")).get();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl nextLeader = followers[0];
    long commitIndex = getCommitIndex(leader);
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(commitIndex, getCommitIndex(raftNode));
        }
    });
    group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
    group.dropMessagesToMember(nextLeader.getLocalMember(), leader.getLocalMember(), AppendSuccessResponse.class);
    leader.replicate(new ApplyRaftRunnable("val2"));
    assertTrueEventually(() -> assertTrue(getLastLogOrSnapshotEntry(nextLeader).index() > commitIndex));
    assertTrueAllTheTime(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(commitIndex, getCommitIndex(raftNode));
        }
    }, 10);
    group.terminateNode(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            assertEquals(nextLeader.getLocalMember(), getLeaderMember(raftNode));
        }
    });
    nextLeader.replicate(new ApplyRaftRunnable("val3"));
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            assertEquals(3, getCommitIndex(raftNode));
            assertEquals(3, getLastLogOrSnapshotEntry(raftNode).index());
            RaftDataService service = group.getService(raftNode);
            assertEquals(3, service.size());
            assertEquals("val1", service.get(1));
            assertEquals("val2", service.get(2));
            assertEquals("val3", service.get(3));
        }
    });
}
Also used : 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 5 with ApplyRaftRunnable

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

the class PersistenceTest method when_followerIsRestarted_then_itAppliesPreviouslyCommittedMemberList.

@Test
public void when_followerIsRestarted_then_itAppliesPreviouslyCommittedMemberList() throws ExecutionException, InterruptedException {
    RaftAlgorithmConfig config = new RaftAlgorithmConfig().setLeaderHeartbeatPeriodInMillis(SECONDS.toMillis(30));
    group = new LocalRaftGroupBuilder(3, config).setAppendNopEntryOnLeaderElection(true).setRaftStateStoreFactory(RAFT_STATE_STORE_FACTORY).build();
    group.start();
    final RaftNodeImpl leader = group.waitUntilLeaderElected();
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    RaftNodeImpl removedFollower = followers[0];
    RaftNodeImpl terminatedFollower = followers[1];
    group.terminateNode(removedFollower.getLocalMember());
    leader.replicate(new ApplyRaftRunnable("val")).get();
    leader.replicateMembershipChange(removedFollower.getLocalMember(), REMOVE).get();
    RaftEndpoint terminatedEndpoint = terminatedFollower.getLocalMember();
    InMemoryRaftStateStore stateStore = getRaftStateStore(terminatedFollower);
    RestoredRaftState terminatedState = stateStore.toRestoredRaftState();
    group.terminateNode(terminatedEndpoint);
    final RaftNodeImpl restartedNode = group.createNewRaftNode(terminatedState, stateStore);
    assertTrueEventually(() -> {
        assertEquals(getCommitIndex(leader), getCommitIndex(restartedNode));
        assertEquals(getLastApplied(leader), getLastApplied(restartedNode));
        assertEquals(new ArrayList<>(getCommittedGroupMembers(leader).members()), new ArrayList<>(getCommittedGroupMembers(restartedNode).members()));
        assertEquals(new ArrayList<>(getLastGroupMembers(leader).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)

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