Search in sources :

Example 31 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete.

@Test
public void testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete() {
    logStart("testHandleAppendEntriesLeaderChangedBeforeSyncUpComplete");
    MockRaftActorContext context = createActorContext();
    List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
    // The new commitIndex is 101
    AppendEntries appendEntries = new AppendEntries(2, "leader-1", 100, 1, entries, 101, 100, (short) 0);
    follower = createBehavior(context);
    follower.handleMessage(leaderActor, appendEntries);
    FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    assertFalse(syncStatus.isInitialSyncDone());
    // Clear all the messages
    MessageCollectorActor.clearMessages(followerActor);
    context.setLastApplied(100);
    setLastLogEntry(context, 1, 100, new MockRaftActorContext.MockPayload(""));
    entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
    // leader-2 is becoming the leader now and it says the commitIndex is 45
    appendEntries = new AppendEntries(2, "leader-2", 45, 1, entries, 46, 100, (short) 0);
    follower.handleMessage(leaderActor, appendEntries);
    syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    // We get a new message saying initial status is not done
    assertFalse(syncStatus.isInitialSyncDone());
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) Test(org.junit.Test)

Example 32 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class FollowerTest method testFirstAppendEntriesWithNoPrevIndexAndReplToAllPresentInSnapshotButCalculatedPrevEntryMissing.

@Test
public void testFirstAppendEntriesWithNoPrevIndexAndReplToAllPresentInSnapshotButCalculatedPrevEntryMissing() {
    logStart("testFirstAppendEntriesWithNoPrevIndexAndReplicatedToAllPresentInSnapshotButCalculatedPrevEntryMissing");
    MockRaftActorContext context = createActorContext();
    context.getReplicatedLog().clear(0, 2);
    context.getReplicatedLog().setSnapshotIndex(100);
    List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 105, "foo"));
    // The new commitIndex is 101
    AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 105, 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());
    assertFalse("append entries reply should be false", reply.isSuccess());
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) Test(org.junit.Test)

Example 33 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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()));
}
Also used : Builder(org.opendaylight.controller.cluster.raft.MockRaftActor.Builder) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) MockRaftActor(org.opendaylight.controller.cluster.raft.MockRaftActor) ApplySnapshot(org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) RaftActorSnapshotCohort(org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) UpdateElectionTerm(org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm) Test(org.junit.Test)

Example 34 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class FollowerTest method testReceivingAppendEntriesDuringInstallSnapshot.

/**
 * Verify that when an AppendEntries is sent to a follower during a snapshot install
 * the Follower short-circuits the processing of the AppendEntries message.
 */
@Test
public void testReceivingAppendEntriesDuringInstallSnapshot() {
    logStart("testReceivingAppendEntriesDuringInstallSnapshot");
    MockRaftActorContext context = createActorContext();
    follower = createBehavior(context);
    ByteString bsSnapshot = createSnapshot();
    int snapshotLength = bsSnapshot.size();
    int chunkSize = 50;
    int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
    int lastIncludedIndex = 1;
    // Check that snapshot installation is not in progress
    assertNull(follower.getSnapshotTracker());
    // Make sure that we have more than 1 chunk to send
    assertTrue(totalChunks > 1);
    // Send an install snapshot with the first chunk to start the process of installing a snapshot
    byte[] chunkData = getNextChunk(bsSnapshot, 0, chunkSize);
    follower.handleMessage(leaderActor, new InstallSnapshot(1, "leader", lastIncludedIndex, 1, chunkData, 1, totalChunks));
    // Check if snapshot installation is in progress now
    assertNotNull(follower.getSnapshotTracker());
    // Send an append entry
    AppendEntries appendEntries = new AppendEntries(1, "leader", 1, 1, Arrays.asList(newReplicatedLogEntry(2, 1, "3")), 2, -1, (short) 1);
    follower.handleMessage(leaderActor, appendEntries);
    AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertEquals("isSuccess", true, reply.isSuccess());
    assertEquals("getLogLastIndex", context.getReplicatedLog().lastIndex(), reply.getLogLastIndex());
    assertEquals("getLogLastTerm", context.getReplicatedLog().lastTerm(), reply.getLogLastTerm());
    assertEquals("getTerm", context.getTermInformation().getCurrentTerm(), reply.getTerm());
    assertNotNull(follower.getSnapshotTracker());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) Test(org.junit.Test)

Example 35 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesAddNewEntries.

/**
 * This test verifies that when a new AppendEntries message is received with
 * new entries and the logs of the sender and receiver match that the new
 * entries get added to the log and the log is incremented by the number of
 * entries received in appendEntries.
 */
@Test
public void testHandleAppendEntriesAddNewEntries() {
    logStart("testHandleAppendEntriesAddNewEntries");
    MockRaftActorContext context = createActorContext();
    // First set the receivers term to lower number
    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"));
    log.append(newReplicatedLogEntry(1, 2, "two"));
    context.setReplicatedLog(log);
    // Prepare the entries to be sent with AppendEntries
    List<ReplicatedLogEntry> entries = new ArrayList<>();
    entries.add(newReplicatedLogEntry(1, 3, "three"));
    entries.add(newReplicatedLogEntry(1, 4, "four"));
    // Send appendEntries with the same term as was set on the receiver
    // before the new behavior was created (1 in this case)
    // This will not work for a Candidate because as soon as a Candidate
    // is created it increments the term
    short leaderPayloadVersion = 10;
    String leaderId = "leader-1";
    AppendEntries appendEntries = new AppendEntries(1, leaderId, 2, 1, entries, 4, -1, leaderPayloadVersion);
    follower = createBehavior(context);
    RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
    Assert.assertSame(follower, newBehavior);
    assertEquals("Next index", 5, log.last().getIndex() + 1);
    assertEquals("Entry 3", entries.get(0), log.get(3));
    assertEquals("Entry 4", entries.get(1), log.get(4));
    assertEquals("getLeaderPayloadVersion", leaderPayloadVersion, newBehavior.getLeaderPayloadVersion());
    assertEquals("getLeaderId", leaderId, newBehavior.getLeaderId());
    expectAndVerifyAppendEntriesReply(1, true, context.getId(), 1, 4);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Aggregations

AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)62 Test (org.junit.Test)58 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)44 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)34 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)34 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)30 FiniteDuration (scala.concurrent.duration.FiniteDuration)19 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)17 ByteString (com.google.protobuf.ByteString)15 ActorRef (akka.actor.ActorRef)9 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)9 TestActorRef (akka.testkit.TestActorRef)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)5 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4