use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class IsolatedLeaderTest method testHandleMessageFromAnotherLeader.
@Test
public void testHandleMessageFromAnotherLeader() throws Exception {
String followerAddress1 = "akka://test/user/$a";
String followerAddress2 = "akka://test/user/$b";
MockRaftActorContext leaderActorContext = createActorContext();
Map<String, String> peerAddresses = new HashMap<>();
peerAddresses.put("follower-1", followerAddress1);
peerAddresses.put("follower-2", followerAddress2);
leaderActorContext.setPeerAddresses(peerAddresses);
isolatedLeader = new IsolatedLeader(leaderActorContext);
assertEquals("Raft state", RaftState.IsolatedLeader, isolatedLeader.state());
// if an append-entries reply is received by the isolated-leader, and that reply
// has a term > than its own term, then IsolatedLeader switches to Follower
// bowing itself to another leader in the cluster
RaftActorBehavior newBehavior = isolatedLeader.handleMessage(senderActor, new AppendEntriesReply("follower-1", isolatedLeader.lastTerm() + 1, true, isolatedLeader.lastIndex() + 1, isolatedLeader.lastTerm() + 1, (short) 0));
assertEquals("Raft state", RaftState.Follower, newBehavior.state());
newBehavior.close();
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class LeaderTest method testTransferLeadershipWithFollowerInitiallyOutOfSync.
@Test
public void testTransferLeadershipWithFollowerInitiallyOutOfSync() {
logStart("testTransferLeadershipWithFollowerInitiallyOutOfSync");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
leader = new Leader(leaderActorContext);
leaderActorContext.setCurrentBehavior(leader);
// Initial heartbeat
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
MessageCollectorActor.clearMessages(followerActor);
RaftActorLeadershipTransferCohort mockTransferCohort = mock(RaftActorLeadershipTransferCohort.class);
doReturn(Optional.absent()).when(mockTransferCohort).getRequestedFollowerId();
leader.transferLeadership(mockTransferCohort);
verify(mockTransferCohort, never()).transferComplete();
// Sync up the follower.
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, -1, -1, (short) 0));
MessageCollectorActor.clearMessages(followerActor);
Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getHeartBeatInterval().toMillis() + 1, TimeUnit.MILLISECONDS);
leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 1, 1, (short) 0));
// Leader should force an election timeout
MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
verify(mockTransferCohort).transferComplete();
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply in project controller by opendaylight.
the class LeaderTest method testDuplicateAppendEntriesWillBeSentOnHeartBeat.
@Test
public void testDuplicateAppendEntriesWillBeSentOnHeartBeat() throws Exception {
logStart("testDuplicateAppendEntriesWillBeSentOnHeartBeat");
MockRaftActorContext actorContext = createActorContextWithFollower();
actorContext.setConfigParams(new DefaultConfigParamsImpl() {
@Override
public FiniteDuration getHeartBeatInterval() {
return FiniteDuration.apply(500, TimeUnit.MILLISECONDS);
}
});
long term = 1;
actorContext.getTermInformation().update(term, "");
leader = new Leader(actorContext);
// Leader will send an immediate heartbeat - ignore it.
MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
// The follower would normally reply - simulate that explicitly here.
long lastIndex = actorContext.getReplicatedLog().lastIndex();
leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, lastIndex, term, (short) 0));
assertEquals("isFollowerActive", true, leader.getFollower(FOLLOWER_ID).isFollowerActive());
followerActor.underlyingActor().clear();
sendReplicate(actorContext, lastIndex + 1);
// Wait slightly longer than heartbeat duration
Uninterruptibles.sleepUninterruptibly(750, TimeUnit.MILLISECONDS);
leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
List<AppendEntries> allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
assertEquals("The number of append entries collected should be 2", 2, allMessages.size());
assertEquals(1, allMessages.get(0).getEntries().size());
assertEquals(lastIndex + 1, allMessages.get(0).getEntries().get(0).getIndex());
assertEquals(1, allMessages.get(1).getEntries().size());
assertEquals(lastIndex + 1, allMessages.get(0).getEntries().get(0).getIndex());
}
use of org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply 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.messages.AppendEntriesReply 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());
}
Aggregations