use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry 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);
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class FollowerTest method testHandleAppendEntriesCorrectReceiverLogEntries.
/**
* This test verifies that when a new AppendEntries message is received with
* new entries and the logs of the sender and receiver are out-of-sync that
* the log is first corrected by removing the out of sync entries from the
* log and then adding in the new entries sent with the AppendEntries message.
*/
@Test
public void testHandleAppendEntriesCorrectReceiverLogEntries() {
logStart("testHandleAppendEntriesCorrectReceiverLogEntries");
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);
follower = createBehavior(context);
RaftActorBehavior newBehavior = follower.handleMessage(leaderActor, appendEntries);
Assert.assertSame(follower, newBehavior);
// The entry at index 2 will be found out-of-sync with the leader
// and will be removed
// Then the two new entries will be added to the log
// Thus making the log to have 4 entries
assertEquals("Next index", 4, log.last().getIndex() + 1);
// assertEquals("Entry 2", entries.get(0), log.get(2));
assertEquals("Entry 1 data", "one", log.get(1).getData().toString());
// Check that the entry at index 2 has the new data
assertEquals("Entry 2", entries.get(0), log.get(2));
assertEquals("Entry 3", entries.get(1), log.get(3));
expectAndVerifyAppendEntriesReply(2, true, context.getId(), 2, 3);
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testHandleAppendEntriesReplyFailureWithFollowersLogBehindTheLeader.
@Test
public void testHandleAppendEntriesReplyFailureWithFollowersLogBehindTheLeader() {
logStart("testHandleAppendEntriesReplyFailureWithFollowersLogBehindTheLeader");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
long leaderCommitIndex = 2;
leaderActorContext.setCommitIndex(leaderCommitIndex);
leaderActorContext.setLastApplied(leaderCommitIndex);
final ReplicatedLogEntry leadersSecondLogEntry = leaderActorContext.getReplicatedLog().get(1);
final ReplicatedLogEntry leadersThirdLogEntry = leaderActorContext.getReplicatedLog().get(2);
MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 1, 1).build());
followerActorContext.setCommitIndex(0);
followerActorContext.setLastApplied(0);
Follower follower = new Follower(followerActorContext);
followerActor.underlyingActor().setBehavior(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.
assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
assertEquals("Log entries size", 0, appendEntries.getEntries().size());
assertEquals("getPrevLogIndex", 1, appendEntries.getPrevLogIndex());
leaderActor.underlyingActor().setBehavior(leader);
leader.handleMessage(followerActor, appendEntriesReply);
MessageCollectorActor.expectMatching(leaderActor, AppendEntriesReply.class, 1);
appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 1, appendEntries.getEntries().get(0).getIndex());
assertEquals("First entry data", leadersSecondLogEntry.getData(), appendEntries.getEntries().get(0).getData());
assertEquals("Second entry index", 2, appendEntries.getEntries().get(1).getIndex());
assertEquals("Second entry data", leadersThirdLogEntry.getData(), appendEntries.getEntries().get(1).getData());
FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
assertEquals("getNextIndex", 3, followerInfo.getNextIndex());
List<ApplyState> applyStateList = MessageCollectorActor.expectMatching(followerActor, ApplyState.class, 2);
ApplyState applyState = applyStateList.get(0);
assertEquals("Follower's first ApplyState index", 1, applyState.getReplicatedLogEntry().getIndex());
assertEquals("Follower's first ApplyState term", 1, applyState.getReplicatedLogEntry().getTerm());
assertEquals("Follower's first ApplyState data", leadersSecondLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
applyState = applyStateList.get(1);
assertEquals("Follower's second ApplyState index", 2, applyState.getReplicatedLogEntry().getIndex());
assertEquals("Follower's second ApplyState term", 1, applyState.getReplicatedLogEntry().getTerm());
assertEquals("Follower's second ApplyState data", leadersThirdLogEntry.getData(), applyState.getReplicatedLogEntry().getData());
assertEquals("Follower's commit index", 2, followerActorContext.getCommitIndex());
assertEquals("Follower's lastIndex", 2, followerActorContext.getReplicatedLog().lastIndex());
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class LeaderTest method testHandleAppendEntriesReplyFailureWithFollowersLogEmpty.
@Test
public void testHandleAppendEntriesReplyFailureWithFollowersLogEmpty() {
logStart("testHandleAppendEntriesReplyFailureWithFollowersLogEmpty");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 1).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().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 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());
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", 1, 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", 1, 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());
}
use of org.opendaylight.controller.cluster.raft.ReplicatedLogEntry in project controller by opendaylight.
the class AbstractRaftActorBehaviorTest method testHandleAppendEntriesAddSameEntryToLog.
@Test
public void testHandleAppendEntriesAddSameEntryToLog() {
MockRaftActorContext context = createActorContext();
context.getTermInformation().update(2, "test");
// Prepare the receivers log
MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
setLastLogEntry(context, 2, 0, payload);
List<ReplicatedLogEntry> entries = new ArrayList<>();
entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short) 0);
behavior = createBehavior(context);
assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
RaftState expected = behavior.state();
// Check that the behavior does not handle unknwon message
assertNull(behavior.handleMessage(behaviorActor, "unknown"));
RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
assertEquals("Raft state", expected, raftBehavior.state());
assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
}
Aggregations