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]);
}
});
}
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()));
}
});
}
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());
});
}
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));
}
}
});
}
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());
}
}
Aggregations