use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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());
}
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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));
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class PersistenceTest method when_leaderIsRestarted_then_itRestoresItsRaftStateWithSnapshotAndBecomesLeader.
@Test
public void when_leaderIsRestarted_then_itRestoresItsRaftStateWithSnapshotAndBecomesLeader() 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);
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(restartedNode, newLeader);
assertTrueEventually(() -> {
assertTrue(getTerm(restartedNode) > term);
assertEquals(commitIndex + 1, getCommitIndex(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]);
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService 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]);
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class SnapshotTest method when_followersIsFarBehind_then_itInstallsSnapshot.
@Test
public void when_followersIsFarBehind_then_itInstallsSnapshot() throws ExecutionException, InterruptedException {
final int entryCount = 50;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
group = newGroup(3, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val0")).get();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl slowFollower = followers[1];
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
for (int i = 1; i < entryCount; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
leader.replicate(new ApplyRaftRunnable("valFinal")).get();
group.allowMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
assertTrueEventually(() -> assertEquals(entryCount, getCommitIndex(slowFollower)));
group.resetAllRulesFrom(leader.getLocalMember());
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
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));
}
});
}
Aggregations