Search in sources :

Example 6 with ReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.

the class FollowerTest method testInitialSyncUpWithHandleInstallSnapshotFollowedByAppendEntries.

@Test
public void testInitialSyncUpWithHandleInstallSnapshotFollowedByAppendEntries() {
    logStart("testInitialSyncUpWithHandleInstallSnapshot");
    MockRaftActorContext context = createActorContext();
    context.setCommitIndex(-1);
    follower = createBehavior(context);
    ByteString bsSnapshot = createSnapshot();
    int offset = 0;
    int snapshotLength = bsSnapshot.size();
    int chunkSize = 50;
    int totalChunks = snapshotLength / chunkSize + (snapshotLength % chunkSize > 0 ? 1 : 0);
    int lastIncludedIndex = 1;
    int chunkIndex = 1;
    InstallSnapshot lastInstallSnapshot = null;
    for (int i = 0; i < totalChunks; i++) {
        byte[] chunkData = getNextChunk(bsSnapshot, offset, chunkSize);
        lastInstallSnapshot = new InstallSnapshot(1, "leader", lastIncludedIndex, 1, chunkData, chunkIndex, totalChunks);
        follower.handleMessage(leaderActor, lastInstallSnapshot);
        offset = offset + 50;
        lastIncludedIndex++;
        chunkIndex++;
    }
    FollowerInitialSyncUpStatus syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    assertFalse(syncStatus.isInitialSyncDone());
    // Clear all the messages
    MessageCollectorActor.clearMessages(followerActor);
    context.setLastApplied(101);
    context.setCommitIndex(101);
    setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload(""));
    List<ReplicatedLogEntry> entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
    // The new commitIndex is 101
    AppendEntries appendEntries = new AppendEntries(2, "leader", 101, 1, entries, 102, 101, (short) 0);
    follower.handleMessage(leaderActor, appendEntries);
    syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    assertTrue(syncStatus.isInitialSyncDone());
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) Test(org.junit.Test)

Example 7 with ReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesWhenOutOfSyncLogDetectedRequestForceInstallSnapshot.

@Test
public void testHandleAppendEntriesWhenOutOfSyncLogDetectedRequestForceInstallSnapshot() {
    logStart("testHandleAppendEntriesWhenOutOfSyncLogDetectedRequestForceInstallSnapshot");
    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(2, 2, "two-1"));
    entries.add(newReplicatedLogEntry(2, 3, "three"));
    // 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
    AppendEntries appendEntries = new AppendEntries(2, "leader", 1, 1, entries, 3, -1, (short) 0);
    context.setRaftPolicy(createRaftPolicy(false, true));
    follower = createBehavior(context);
    RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
    Assert.assertSame(follower, newBehavior);
    expectAndVerifyAppendEntriesReply(2, false, context.getId(), 1, 2, true);
}
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) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 8 with ReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesAfterInstallingSnapshot.

@Test
public void testHandleAppendEntriesAfterInstallingSnapshot() {
    logStart("testHandleAppendAfterInstallingSnapshot");
    MockRaftActorContext context = createActorContext();
    // Prepare the receivers log
    MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
    // Set up a log as if it has been snapshotted
    log.setSnapshotIndex(3);
    log.setSnapshotTerm(1);
    context.setReplicatedLog(log);
    // Prepare the entries to be sent with AppendEntries
    List<ReplicatedLogEntry> entries = new ArrayList<>();
    entries.add(newReplicatedLogEntry(1, 4, "four"));
    AppendEntries appendEntries = new AppendEntries(1, "leader", 3, 1, entries, 4, 3, (short) 0);
    follower = createBehavior(context);
    RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
    Assert.assertSame(follower, newBehavior);
    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) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 9 with ReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesLeaderChangedAfterSyncUpComplete.

@Test
public void testHandleAppendEntriesLeaderChangedAfterSyncUpComplete() {
    logStart("testHandleAppendEntriesLeaderChangedAfterSyncUpComplete");
    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(101);
    context.setCommitIndex(101);
    setLastLogEntry(context, 1, 101, new MockRaftActorContext.MockPayload(""));
    entries = Arrays.asList(newReplicatedLogEntry(2, 101, "foo"));
    // The new commitIndex is 101
    appendEntries = new AppendEntries(2, "leader-1", 101, 1, entries, 102, 101, (short) 0);
    follower.handleMessage(leaderActor, appendEntries);
    syncStatus = MessageCollectorActor.expectFirstMatching(followerActor, FollowerInitialSyncUpStatus.class);
    assertTrue(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 10 with ReplicatedLogEntry

use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.

the class FollowerTest method testHandleAppendEntriesWithNewerCommitIndex.

/**
 * This test verifies that when an AppendEntries RPC is received by a RaftActor
 * with a commitIndex that is greater than what has been applied to the
 * state machine of the RaftActor, the RaftActor applies the state and
 * sets it current applied state to the commitIndex of the sender.
 */
@Test
public void testHandleAppendEntriesWithNewerCommitIndex() {
    logStart("testHandleAppendEntriesWithNewerCommitIndex");
    MockRaftActorContext context = createActorContext();
    context.setLastApplied(100);
    setLastLogEntry(context, 1, 100, new MockRaftActorContext.MockPayload(""));
    context.getReplicatedLog().setSnapshotIndex(99);
    List<ReplicatedLogEntry> entries = Arrays.<ReplicatedLogEntry>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);
    assertEquals("getLastApplied", 101L, context.getLastApplied());
}
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) Test(org.junit.Test)

Aggregations

ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)38 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)31 Test (org.junit.Test)30 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)30 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)24 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)15 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 ArrayList (java.util.ArrayList)8 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)7 ByteString (com.google.protobuf.ByteString)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)5 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)5 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)5 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4 FiniteDuration (scala.concurrent.duration.FiniteDuration)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockRaftActor (org.opendaylight.controller.cluster.raft.MockRaftActor)3 Builder (org.opendaylight.controller.cluster.raft.MockRaftActor.Builder)3 ActorSelection (akka.actor.ActorSelection)2