use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method createFollowerActorContextWithLeader.
private MockRaftActorContext createFollowerActorContextWithLeader() {
MockRaftActorContext followerActorContext = createActorContext(FOLLOWER_ID, followerActor);
DefaultConfigParamsImpl followerConfig = new DefaultConfigParamsImpl();
followerConfig.setElectionTimeoutFactor(10000);
followerActorContext.setConfigParams(followerConfig);
followerActorContext.setPeerAddresses(ImmutableMap.of(LEADER_ID, leaderActor.path().toString()));
return followerActorContext;
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method testSendingReplicateImmediatelyAfterHeartbeatDoesReplicate.
@Test
public void testSendingReplicateImmediatelyAfterHeartbeatDoesReplicate() throws Exception {
logStart("testSendingReplicateImmediatelyAfterHeartbeatDoesReplicate");
MockRaftActorContext actorContext = createActorContextWithFollower();
actorContext.setConfigParams(new DefaultConfigParamsImpl() {
@Override
public FiniteDuration getHeartBeatInterval() {
return FiniteDuration.apply(100, 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();
Uninterruptibles.sleepUninterruptibly(150, TimeUnit.MILLISECONDS);
leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
sendReplicate(actorContext, lastIndex + 1);
List<AppendEntries> allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
assertEquals("The number of append entries collected should be 2", 2, allMessages.size());
assertEquals(0, allMessages.get(0).getEntries().size());
assertEquals(1, allMessages.get(1).getEntries().size());
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method testSendSnapshotfromInstallSnapshotReply.
@Test
public void testSendSnapshotfromInstallSnapshotReply() throws Exception {
logStart("testSendSnapshotfromInstallSnapshotReply");
MockRaftActorContext actorContext = createActorContextWithFollower();
final int commitIndex = 3;
final int snapshotIndex = 2;
final int snapshotTerm = 1;
final int currentTerm = 2;
DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl() {
@Override
public int getSnapshotChunkSize() {
return 50;
}
};
configParams.setHeartBeatInterval(new FiniteDuration(9, TimeUnit.SECONDS));
configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
actorContext.setConfigParams(configParams);
actorContext.setCommitIndex(commitIndex);
leader = new Leader(actorContext);
actorContext.setCurrentBehavior(leader);
leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
leader.getFollower(FOLLOWER_ID).setNextIndex(0);
Map<String, String> leadersSnapshot = new HashMap<>();
leadersSnapshot.put("1", "A");
leadersSnapshot.put("2", "B");
leadersSnapshot.put("3", "C");
// set the snapshot variables in replicatedlog
actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
ByteString bs = toByteString(leadersSnapshot);
Snapshot snapshot = Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null);
leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bs.toByteArray())));
InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
assertEquals(1, installSnapshot.getChunkIndex());
assertEquals(3, installSnapshot.getTotalChunks());
followerActor.underlyingActor().clear();
leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
assertEquals(2, installSnapshot.getChunkIndex());
assertEquals(3, installSnapshot.getTotalChunks());
followerActor.underlyingActor().clear();
leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
// Send snapshot reply one more time and make sure that a new snapshot message should not be sent to follower
followerActor.underlyingActor().clear();
leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
installSnapshot = MessageCollectorActor.getFirstMatching(followerActor, InstallSnapshot.class);
assertNull(installSnapshot);
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded.
@Test
public void testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded() {
logStart("testFollowerCatchUpWithAppendEntriesMaxDataSizeExceeded");
MockRaftActorContext leaderActorContext = createActorContextWithFollower();
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setSnapshotChunkSize(2);
leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 4, 1).build());
long leaderCommitIndex = 3;
leaderActorContext.setCommitIndex(leaderCommitIndex);
leaderActorContext.setLastApplied(leaderCommitIndex);
final ReplicatedLogEntry leadersFirstLogEntry = leaderActorContext.getReplicatedLog().get(0);
final ReplicatedLogEntry leadersSecondLogEntry = leaderActorContext.getReplicatedLog().get(1);
final ReplicatedLogEntry leadersThirdLogEntry = leaderActorContext.getReplicatedLog().get(2);
final ReplicatedLogEntry leadersFourthLogEntry = leaderActorContext.getReplicatedLog().get(3);
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", 2, appendEntries.getPrevLogIndex());
leaderActor.underlyingActor().setBehavior(leader);
leaderActorContext.setCurrentBehavior(leader);
leader.handleMessage(followerActor, appendEntriesReply);
List<AppendEntries> appendEntriesList = MessageCollectorActor.expectMatching(followerActor, AppendEntries.class, 2);
MessageCollectorActor.expectMatching(leaderActor, AppendEntriesReply.class, 2);
appendEntries = appendEntriesList.get(0);
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());
appendEntries = appendEntriesList.get(1);
assertEquals("getLeaderCommit", leaderCommitIndex, appendEntries.getLeaderCommit());
assertEquals("getPrevLogIndex", 1, appendEntries.getPrevLogIndex());
assertEquals("Log entries size", 2, appendEntries.getEntries().size());
assertEquals("First entry index", 2, appendEntries.getEntries().get(0).getIndex());
assertEquals("First entry data", leadersThirdLogEntry.getData(), appendEntries.getEntries().get(0).getData());
assertEquals("Second entry index", 3, appendEntries.getEntries().get(1).getIndex());
assertEquals("Second entry data", leadersFourthLogEntry.getData(), appendEntries.getEntries().get(1).getData());
FollowerLogInformation followerInfo = leader.getFollower(FOLLOWER_ID);
assertEquals("getNextIndex", 4, followerInfo.getNextIndex());
MessageCollectorActor.expectMatching(followerActor, ApplyState.class, 4);
assertEquals("Follower's commit index", 3, followerActorContext.getCommitIndex());
assertEquals("Follower's lastIndex", 3, followerActorContext.getReplicatedLog().lastIndex());
}
use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl in project controller by opendaylight.
the class LeaderTest method testMultipleReplicateShouldNotCauseDuplicateAppendEntriesToBeSent.
@Test
public void testMultipleReplicateShouldNotCauseDuplicateAppendEntriesToBeSent() throws Exception {
logStart("testHandleReplicateMessageSendAppendEntriesToFollower");
MockRaftActorContext actorContext = createActorContextWithFollower();
actorContext.setConfigParams(new DefaultConfigParamsImpl() {
@Override
public FiniteDuration getHeartBeatInterval() {
return FiniteDuration.apply(5, TimeUnit.SECONDS);
}
});
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();
for (int i = 0; i < 5; i++) {
sendReplicate(actorContext, lastIndex + i + 1);
}
List<AppendEntries> allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
// We expect only 1 message to be sent because of two reasons,
// - an append entries reply was not received
// - the heartbeat interval has not expired
// In this scenario if multiple messages are sent they would likely be duplicates
assertEquals("The number of append entries collected should be 1", 1, allMessages.size());
}
Aggregations