use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class FollowerTest method testCaptureSnapshotOnAppendEntriesWithUnapplied.
@Test
public void testCaptureSnapshotOnAppendEntriesWithUnapplied() {
String id = "testCaptureSnapshotOnAppendEntriesWithUnapplied";
logStart(id);
InMemoryJournal.addEntry(id, 1, new UpdateElectionTerm(1, null));
DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
config.setSnapshotBatchCount(1);
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, 0, -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 0 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 0 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", 0, ((ApplyJournalEntries) journalEntries.get(0)).getToIndex());
assertEquals("Snapshot unapplied size", 2, snapshot.getUnAppliedEntries().size());
assertEquals("Snapshot unapplied entry index", 1, snapshot.getUnAppliedEntries().get(0).getIndex());
assertEquals("Snapshot unapplied entry index", 2, snapshot.getUnAppliedEntries().get(1).getIndex());
assertEquals("Snapshot getLastAppliedTerm", 1, snapshot.getLastAppliedTerm());
assertEquals("Snapshot getLastAppliedIndex", 0, snapshot.getLastAppliedIndex());
assertEquals("Snapshot getLastTerm", 1, snapshot.getLastTerm());
assertEquals("Snapshot getLastIndex", 2, snapshot.getLastIndex());
assertEquals("Snapshot state", ImmutableList.of(entries.get(0).getData()), MockRaftActor.fromState(snapshot.getState()));
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent.
@Test
public void testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent() {
logStart("testHandleAppendEntriesReplyFailureWithFollowersLogTermDifferent");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 2).build());
long leaderCommitIndex = 1;
leaderActorContext.setCommitIndex(leaderCommitIndex);
leaderActorContext.setLastApplied(leaderCommitIndex);
final ReplicatedLogEntry leadersFirstLogEntry = leaderActorContext.getReplicatedLog().get(0);
final ReplicatedLogEntry leadersSecondLogEntry = leaderActorContext.getReplicatedLog().get(1);
MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 1, 1).build());
followerActorContext.setCommitIndex(-1);
followerActorContext.setLastApplied(-1);
Follower follower = new Follower(followerActorContext);
followerActor.underlyingActor().setBehavior(follower);
followerActorContext.setCurrentBehavior(follower);
leader = new Leader(leaderActorContext);
AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
final AppendEntriesReply appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
MessageCollectorActor.clearMessages(followerActor);
MessageCollectorActor.clearMessages(leaderActor);
// Verify initial AppendEntries sent with the leader's current commit index.
assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
assertEquals("Log entries size", 0, appendEntries.getEntries().size());
assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
leaderActor.underlyingActor().setBehavior(leader);
leaderActorContext.setCurrentBehavior(leader);
leader.handleMessage(followerActor, appendEntriesReply);
MessageCollectorActor.expectMatching(leaderActor, AppendEntriesReply.class, 1);
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
assertEquals("getPrevLogIndex", -1, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 0, appendEntries.getEntries().get(0).getIndex());
assertEquals("First entry term", 2, appendEntries.getEntries().get(0).getTerm());
assertEquals("First entry data", leadersFirstLogEntry.getData(), appendEntries.getEntries().get(0).getData());
assertEquals("Second entry index", 1, appendEntries.getEntries().get(1).getIndex());
assertEquals("Second entry term", 2, appendEntries.getEntries().get(1).getTerm());
assertEquals("Second entry data", leadersSecondLogEntry.getData(), appendEntries.getEntries().get(1).getData());
FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
assertEquals("getNextIndex", 2, followerInfo.getNextIndex());
List<ApplyState> applyStateList = MessageCollectorActor.expectMatching(followerActor, ApplyState.class, 2);
ApplyState applyState = applyStateList.get(0);
assertEquals("Follower's first ApplyState index", 0, applyState.getReplicatedLogEntry().getIndex());
assertEquals("Follower's first ApplyState term", 2, applyState.getReplicatedLogEntry().getTerm());
assertEquals("Follower's first ApplyState data", leadersFirstLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
applyState = applyStateList.get(1);
assertEquals("Follower's second ApplyState index", 1, applyState.getReplicatedLogEntry().getIndex());
assertEquals("Follower's second ApplyState term", 2, applyState.getReplicatedLogEntry().getTerm());
assertEquals("Follower's second ApplyState data", leadersSecondLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
assertEquals("Follower's commit index", 1, followerActorContext.getCommitIndex());
assertEquals("Follower's lastIndex", 1, followerActorContext.getReplicatedLog().lastIndex());
assertEquals("Follower's lastTerm", 2, followerActorContext.getReplicatedLog().lastTerm());
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded.
@Test
public void testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded() {
logStart("testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setSnapshotChunkSize(2);
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 4, 1).build());
long leaderCommitIndex = 3;
leaderActorContext.setCommitIndex(leaderCommitIndex);
leaderActorContext.setLastApplied(leaderCommitIndex);
final ReplicatedLogEntry leadersFirstLogEntry = leaderActorContext.getReplicatedLog().get(0);
final ReplicatedLogEntry leadersSecondLogEntry = leaderActorContext.getReplicatedLog().get(1);
final ReplicatedLogEntry leadersThirdLogEntry = leaderActorContext.getReplicatedLog().get(2);
final ReplicatedLogEntry leadersFourthLogEntry = leaderActorContext.getReplicatedLog().get(3);
MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
followerActorContext.setCommitIndex(-1);
followerActorContext.setLastApplied(-1);
Follower follower = new Follower(followerActorContext);
followerActor.underlyingActor().setBehavior(follower);
followerActorContext.setCurrentBehavior(follower);
leader = new Leader(leaderActorContext);
AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
final AppendEntriesReply appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
MessageCollectorActor.clearMessages(followerActor);
MessageCollectorActor.clearMessages(leaderActor);
// Verify initial AppendEntries sent with the leader's current commit index.
assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
assertEquals("Log entries size", 0, appendEntries.getEntries().size());
assertEquals("getPrevLogIndex", 2, appendEntries.getPrevLogIndex());
leaderActor.underlyingActor().setBehavior(leader);
leaderActorContext.setCurrentBehavior(leader);
leader.handleMessage(followerActor, appendEntriesReply);
List<AppendEntries> appendEntriesList = MessageCollectorActor.expectMatching(followerActor, AppendEntries.class, 2);
MessageCollectorActor.expectMatching(leaderActor, AppendEntriesReply.class, 2);
appendEntries = appendEntriesList.get(0);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
assertEquals("getPrevLogIndex", -1, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 0, appendEntries.getEntries().get(0).getIndex());
assertEquals("First entry data", leadersFirstLogEntry.getData(), appendEntries.getEntries().get(0).getData());
assertEquals("Second entry index", 1, appendEntries.getEntries().get(1).getIndex());
assertEquals("Second entry data", leadersSecondLogEntry.getData(), appendEntries.getEntries().get(1).getData());
appendEntries = appendEntriesList.get(1);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
assertEquals("getPrevLogIndex", 1, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 2, appendEntries.getEntries().get(0).getIndex());
assertEquals("First entry data", leadersThirdLogEntry.getData(), appendEntries.getEntries().get(0).getData());
assertEquals("Second entry index", 3, appendEntries.getEntries().get(1).getIndex());
assertEquals("Second entry data", leadersFourthLogEntry.getData(), appendEntries.getEntries().get(1).getData());
FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
assertEquals("getNextIndex", 4, followerInfo.getNextIndex());
MessageCollectorActor.expectMatching(followerActor, ApplyState.class, 4);
assertEquals("Follower's commit index", 3, followerActorContext.getCommitIndex());
assertEquals("Follower's lastIndex", 3, followerActorContext.getReplicatedLog().lastIndex());
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testHandleReplicateMessageWhenThereAreNoFollowers.
@Test
public void testHandleReplicateMessageWhenThereAreNoFollowers() throws Exception {
logStart("testHandleReplicateMessageWhenThereAreNoFollowers");
MockRaftActorContext actorContext = createActorContext();
leader = new Leader(actorContext);
actorContext.setLastApplied(0);
long newLogIndex = actorContext.getReplicatedLog().lastIndex() + 1;
long term = actorContext.getTermInformation().getCurrentTerm();
ReplicatedLogEntry newEntry = new SimpleReplicatedLogEntry(newLogIndex, term, new MockRaftActorContext.MockPayload("foo"));
actorContext.getReplicatedLog().append(newEntry);
final Identifier id = new MockIdentifier("state-id");
RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new Replicate(leaderActor, id, newEntry, true));
// State should not change
assertTrue(raftBehavior instanceof Leader);
assertEquals("getCommitIndex", newLogIndex, actorContext.getCommitIndex());
// We should get 2 ApplyState messages - 1 for new log entry and 1 for the previous
// one since lastApplied state is 0.
List<ApplyState> applyStateList = MessageCollectorActor.getAllMatching(leaderActor, ApplyState.class);
assertEquals("ApplyState count", newLogIndex, applyStateList.size());
for (int i = 0; i <= newLogIndex - 1; i++) {
ApplyState applyState = applyStateList.get(i);
assertEquals("getIndex", i + 1, applyState.getReplicatedLogEntry().getIndex());
assertEquals("getTerm", term, applyState.getReplicatedLogEntry().getTerm());
}
ApplyState last = applyStateList.get((int) newLogIndex - 1);
assertEquals("getData", newEntry.getData(), last.getReplicatedLogEntry().getData());
assertEquals("getIdentifier", id, last.getIdentifier());
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class FollowerTest method testHandleAppendEntriesWithExistingLogEntry.
@Test
public void testHandleAppendEntriesWithExistingLogEntry() {
logStart("testHandleAppendEntriesWithExistingLogEntry");
MockRaftActorContext context = createActorContext();
context.getTermInformation().update(1, "test");
// Prepare the receivers log
MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
log.append(newReplicatedLogEntry(1, 0, "zero"));
log.append(newReplicatedLogEntry(1, 1, "one"));
context.setReplicatedLog(log);
// Send the last entry again.
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"));
follower = createBehavior(context);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 1, -1, (short) 0));
assertEquals("Next index", 2, log.last().getIndex() + 1);
assertEquals("Entry 1", entries.get(0), log.get(1));
expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 1);
// Send the last entry again and also a new one.
entries = Arrays.asList(newReplicatedLogEntry(1, 1, "one"), newReplicatedLogEntry(1, 2, "two"));
MessageCollectorActor.clearMessages(leaderActor);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", 0, 1, entries, 2, -1, (short) 0));
assertEquals("Next index", 3, log.last().getIndex() + 1);
assertEquals("Entry 1", entries.get(0), log.get(1));
assertEquals("Entry 2", entries.get(1), log.get(2));
expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 2);
}
Aggregations