use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class AbstractRaftActorBehavior method appendEntries.
/**
* Handles the common logic for the AppendEntries message and delegates handling to the derived class.
*
* @param sender the ActorRef that sent the message
* @param appendEntries the message
* @return a new behavior if it was changed or the current behavior
*/
protected RaftActorBehavior appendEntries(ActorRef sender, AppendEntries appendEntries) {
// 1. Reply false if term < currentTerm (ยง5.1)
if (appendEntries.getTerm() < currentTerm()) {
log.info("{}: Cannot append entries because sender's term {} is less than {}", logName(), appendEntries.getTerm(), currentTerm());
sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex(), lastTerm(), context.getPayloadVersion()), actor());
return this;
}
return handleAppendEntries(sender, appendEntries);
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class FollowerTest method testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInLog.
@Test
public void testHandleFirstAppendEntriesWithPrevIndexMinusOneAndReplicatedToAllIndexPresentInLog() {
logStart("testHandleFirstAppendEntries");
MockRaftActorContext context = createActorContext();
context.getReplicatedLog().clear(0, 2);
context.getReplicatedLog().append(newReplicatedLogEntry(1, 100, "bar"));
context.getReplicatedLog().setSnapshotIndex(99);
List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
// The new commitIndex is 101
AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 101, 100, (short) 0);
follower = createBehavior(context);
follower.handleMessage(leaderActor, appendEntries);
FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
assertFalse(syncStatus.isInitialSyncDone());
assertTrue("append entries reply should be true", reply.isSuccess());
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class FollowerTest method handleAppendEntriesAddSameEntryToLogReply.
@Override
protected void handleAppendEntriesAddSameEntryToLogReply(final ActorRef replyActor) {
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(replyActor, AppendEntriesReply.class);
assertEquals("isSuccess", true, reply.isSuccess());
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class FollowerTest method expectAndVerifyAppendEntriesReply.
private void expectAndVerifyAppendEntriesReply(final int expTerm, final boolean expSuccess, final String expFollowerId, final long expLogLastTerm, final long expLogLastIndex, final boolean expForceInstallSnapshot) {
AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
assertEquals("isSuccess", expSuccess, reply.isSuccess());
assertEquals("getTerm", expTerm, reply.getTerm());
assertEquals("getFollowerId", expFollowerId, reply.getFollowerId());
assertEquals("getLogLastTerm", expLogLastTerm, reply.getLogLastTerm());
assertEquals("getLogLastIndex", expLogLastIndex, reply.getLogLastIndex());
assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
assertEquals("isForceInstallSnapshot", expForceInstallSnapshot, reply.isForceInstallSnapshot());
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply 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()));
}
Aggregations