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());
}
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);
}
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);
}
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());
}
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());
}
Aggregations