use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method testReplicationConsensusWithNonVotingFollower.
@Test
public void testReplicationConsensusWithNonVotingFollower() {
logStart("testReplicationConsensusWithNonVotingFollower");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
leaderActorContext.setCommitIndex(-1);
leaderActorContext.setLastApplied(-1);
String nonVotingFollowerId = "nonvoting-follower";
ActorRef nonVotingFollowerActor = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId(nonVotingFollowerId));
leaderActorContext.addToPeers(nonVotingFollowerId, nonVotingFollowerActor.path().toString(), VotingState.NON_VOTING);
leader = new Leader(leaderActorContext);
leaderActorContext.setCurrentBehavior(leader);
// Ignore initial heartbeats
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
MessageCollectorActor.expectFirstMatching(nonVotingFollowerActor, AppendEntries.class);
MessageCollectorActor.clearMessages(followerActor);
MessageCollectorActor.clearMessages(nonVotingFollowerActor);
MessageCollectorActor.clearMessages(leaderActor);
// Send a Replicate message and wait for AppendEntries.
sendReplicate(leaderActorContext, 0);
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
MessageCollectorActor.expectFirstMatching(nonVotingFollowerActor, AppendEntries.class);
// Send reply only from the voting follower and verify consensus via ApplyState.
leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 0, 1, (short) 0));
MessageCollectorActor.expectFirstMatching(leaderActor, ApplyState.class);
leader.handleMessage(leaderActor, new AppendEntriesReply(nonVotingFollowerId, 1, true, 0, 1, (short) 0));
MessageCollectorActor.clearMessages(followerActor);
MessageCollectorActor.clearMessages(nonVotingFollowerActor);
MessageCollectorActor.clearMessages(leaderActor);
// Send another Replicate message
sendReplicate(leaderActorContext, 1);
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(nonVotingFollowerActor, AppendEntries.class);
assertEquals("Log entries size", 1, appendEntries.getEntries().size());
assertEquals("Log entry index", 1, appendEntries.getEntries().get(0).getIndex());
// Send reply only from the non-voting follower and verify no consensus via no ApplyState.
leader.handleMessage(leaderActor, new AppendEntriesReply(nonVotingFollowerId, 1, true, 1, 1, (short) 0));
MessageCollectorActor.assertNoneMatching(leaderActor, ApplyState.class, 500);
// Send reply from the voting follower and verify consensus.
leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 1, 1, (short) 0));
MessageCollectorActor.expectFirstMatching(leaderActor, ApplyState.class);
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class PartitionedCandidateOnStartupElectionScenarioTest method setupPartitionedCandidateMember3AndSendElectionTimeouts.
private void setupPartitionedCandidateMember3AndSendElectionTimeouts() {
testLog.info("setupPartitionedCandidateMember3AndSendElectionTimeouts starting");
// Create member 3's behavior initially as a Candidate.
member3Context = newRaftActorContext("member3", member3ActorRef, ImmutableMap.<String, String>builder().put("member1", member1ActorRef.path().toString()).put("member2", member2ActorRef.path().toString()).build());
DefaultConfigParamsImpl member3ConfigParams = newConfigParams();
member3Context.setConfigParams(member3ConfigParams);
// Initialize the ReplicatedLog and election term info for Candidate member 3. The current term
// will be 2 and the last term will be 1 so it is behind the leader's log.
SimpleReplicatedLog candidateReplicatedLog = new SimpleReplicatedLog();
candidateReplicatedLog.append(new SimpleReplicatedLogEntry(0, 2, new MockPayload("")));
member3Context.setReplicatedLog(candidateReplicatedLog);
member3Context.setCommitIndex(candidateReplicatedLog.lastIndex());
member3Context.setLastApplied(candidateReplicatedLog.lastIndex());
member3Context.getTermInformation().update(2, member1Context.getId());
// The member 3 Candidate will start a new term and send RequestVotes. However it will be
// partitioned from the cluster by having member 1 and 2 drop its RequestVote messages.
candidateElectionTerm = member3Context.getTermInformation().getCurrentTerm() + numCandidateElections;
member1Actor.dropMessagesToBehavior(RequestVote.class, numCandidateElections);
member2Actor.dropMessagesToBehavior(RequestVote.class, numCandidateElections);
member3Actor.self().tell(new SetBehavior(new Candidate(member3Context), member3Context), ActorRef.noSender());
for (int i = 0; i < numCandidateElections - 1; i++) {
member3ActorRef.tell(ElectionTimeout.INSTANCE, ActorRef.noSender());
}
member1Actor.waitForExpectedMessages(RequestVote.class);
member2Actor.waitForExpectedMessages(RequestVote.class);
verifyBehaviorState("member 1", member1Actor, RaftState.Leader);
verifyBehaviorState("member 2", member2Actor, RaftState.Follower);
verifyBehaviorState("member 3", member3Actor, RaftState.Candidate);
assertEquals("member 1 election term", 3, member1Context.getTermInformation().getCurrentTerm());
assertEquals("member 2 election term", 3, member2Context.getTermInformation().getCurrentTerm());
assertEquals("member 3 election term", candidateElectionTerm, member3Context.getTermInformation().getCurrentTerm());
testLog.info("setupPartitionedCandidateMember3AndSendElectionTimeouts ending");
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class CandidateTest method testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers.
@Test
public void testBecomeLeaderOnReceivingMajorityVotesWithNonVotingPeers() {
ElectionTerm mockElectionTerm = Mockito.mock(ElectionTerm.class);
Mockito.doReturn(1L).when(mockElectionTerm).getCurrentTerm();
RaftActorContext raftActorContext = new RaftActorContextImpl(candidateActor, candidateActor.actorContext(), "candidate", mockElectionTerm, -1, -1, setupPeers(4), new DefaultConfigParamsImpl(), new NonPersistentDataProvider(Runnable::run), applyState -> {
}, LOG);
raftActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
raftActorContext.getPeerInfo("peer1").setVotingState(VotingState.NON_VOTING);
raftActorContext.getPeerInfo("peer4").setVotingState(VotingState.NON_VOTING);
candidate = new Candidate(raftActorContext);
MessageCollectorActor.expectFirstMatching(peerActors[1], RequestVote.class);
MessageCollectorActor.expectFirstMatching(peerActors[2], RequestVote.class);
MessageCollectorActor.assertNoneMatching(peerActors[0], RequestVote.class, 300);
MessageCollectorActor.assertNoneMatching(peerActors[3], RequestVote.class, 100);
candidate = candidate.handleMessage(peerActors[1], new RequestVoteReply(1, false));
assertEquals("Behavior", RaftState.Candidate, candidate.state());
candidate = candidate.handleMessage(peerActors[2], new RequestVoteReply(1, true));
assertEquals("Behavior", RaftState.Leader, candidate.state());
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class FollowerTest method testCaptureSnapshotOnMiddleEntryInAppendEntries.
@Test
public void testCaptureSnapshotOnMiddleEntryInAppendEntries() {
String id = "testCaptureSnapshotOnMiddleEntryInAppendEntries";
logStart(id);
InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setSnapshotBatchCount(2);
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
final AtomicReference<MockRaftActor> followerRaftActor = new AtomicReference<>();
RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor);
Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id).peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort);
TestActorRef<MockRaftActor> followerActorRef = actorFactory.createTestActor(builder.props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
followerRaftActor.set(followerActorRef.underlyingActor());
followerRaftActor.get().waitForInitializeBehaviorComplete();
InMemorySnapshotStore.addSnapshotSavedLatch(id);
InMemoryJournal.addDeleteMessagesCompleteLatch(id);
InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class);
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"), newReplicatedLogEntry(1, 2, "three"));
AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 2, -1, (short) 0);
followerActorRef.tell(appendEntries, leaderActor);
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
assertEquals("isSuccess", true, reply.isSuccess());
final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
InMemoryJournal.waitForDeleteMessagesComplete(id);
InMemoryJournal.waitForWriteMessagesComplete(id);
// We expect the ApplyJournalEntries for index 2 to remain in the persisted log b/c it's still queued for
// persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it.
// This is OK - on recovery it will be a no-op since index 2 has already been applied.
List<Object> journalEntries = InMemoryJournal.get(id, Object.class);
assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size());
assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass());
assertEquals("ApplyJournalEntries index", 2, ((ApplyJournalEntries) journalEntries.get(0)).getToIndex());
assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size());
assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
assertEquals("Snapshot getLastAppliedIndex", 2, snapshot.getLastAppliedIndex());
assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex());
assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), entries.get(2).getData()), MockRaftActor.fromState(snapshot.getState()));
assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size());
assertEquals("Snapshot index", 2, followerRaftActor.get().getReplicatedLog().getSnapshotIndex());
// Reinstate the actor from persistence
actorFactory.killActor(followerActorRef, new TestKit(getSystem()));
followerActorRef = actorFactory.createTestActor(builder.props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
followerRaftActor.set(followerActorRef.underlyingActor());
followerRaftActor.get().waitForInitializeBehaviorComplete();
assertEquals("Journal size", 0, followerRaftActor.get().getReplicatedLog().size());
assertEquals("Last index", 2, followerRaftActor.get().getReplicatedLog().lastIndex());
assertEquals("Last applied index", 2, followerRaftActor.get().getRaftActorContext().getLastApplied());
assertEquals("Commit index", 2, followerRaftActor.get().getRaftActorContext().getCommitIndex());
assertEquals("State", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData(), entries.get(2).getData()), followerRaftActor.get().getState());
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class FollowerTest method testCaptureSnapshotOnLastEntryInAppendEntries.
@Test
public void testCaptureSnapshotOnLastEntryInAppendEntries() {
String id = "testCaptureSnapshotOnLastEntryInAppendEntries";
logStart(id);
InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setSnapshotBatchCount(2);
config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
final AtomicReference<MockRaftActor> followerRaftActor = new AtomicReference<>();
RaftActorSnapshotCohort snapshotCohort = newRaftActorSnapshotCohort(followerRaftActor);
Builder builder = MockRaftActor.builder().persistent(Optional.of(true)).id(id).peerAddresses(ImmutableMap.of("leader", "")).config(config).snapshotCohort(snapshotCohort);
TestActorRef<MockRaftActor> followerActorRef = actorFactory.createTestActor(builder.props().withDispatcher(Dispatchers.DefaultDispatcherId()), id);
followerRaftActor.set(followerActorRef.underlyingActor());
followerRaftActor.get().waitForInitializeBehaviorComplete();
InMemorySnapshotStore.addSnapshotSavedLatch(id);
InMemoryJournal.addDeleteMessagesCompleteLatch(id);
InMemoryJournal.addWriteMessagesCompleteLatch(id, 1, ApplyJournalEntries.class);
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(1, 0, "one"), newReplicatedLogEntry(1, 1, "two"));
AppendEntries appendEntries = new AppendEntries(1, "leader", -1, -1, entries, 1, -1, (short) 0);
followerActorRef.tell(appendEntries, leaderActor);
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
assertEquals("isSuccess", true, reply.isSuccess());
final Snapshot snapshot = InMemorySnapshotStore.waitForSavedSnapshot(id, Snapshot.class);
InMemoryJournal.waitForDeleteMessagesComplete(id);
InMemoryJournal.waitForWriteMessagesComplete(id);
// We expect the ApplyJournalEntries for index 1 to remain in the persisted log b/c it's still queued for
// persistence by the time we initiate capture so the last persisted journal sequence number doesn't include it.
// This is OK - on recovery it will be a no-op since index 1 has already been applied.
List<Object> journalEntries = InMemoryJournal.get(id, Object.class);
assertEquals("Persisted journal entries size: " + journalEntries, 1, journalEntries.size());
assertEquals("Persisted journal entry type", ApplyJournalEntries.class, journalEntries.get(0).getClass());
assertEquals("ApplyJournalEntries index", 1, ((ApplyJournalEntries) journalEntries.get(0)).getToIndex());
assertEquals("Snapshot unapplied size", 0, snapshot.getUnAppliedEntries().size());
assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
assertEquals("Snapshot getLastAppliedIndex", 1, snapshot.getLastAppliedIndex());
assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
assertEquals("Snapshot getLastIndex", 1, snapshot.getLastIndex());
assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData(), entries.get(1).getData()), MockRaftActor.fromState(snapshot.getState()));
}
Aggregations