use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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.MockRaftActorContext in project controller by opendaylight.
the class FollowerTest method testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled.
@Test
public void testFollowerSchedulesElectionIfAutomaticElectionsAreDisabled() {
MockRaftActorContext context = createActorContext();
context.setConfigParams(new DefaultConfigParamsImpl() {
@Override
public FiniteDuration getElectionTimeOutInterval() {
return FiniteDuration.apply(100, TimeUnit.MILLISECONDS);
}
});
context.setRaftPolicy(createRaftPolicy(false, false));
follower = createBehavior(context);
TimeoutNow timeoutNow = MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
RaftActorBehavior newBehavior = follower.handleMessage(ActorRef.noSender(), timeoutNow);
assertSame("handleMessage result", follower, newBehavior);
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.
the class FollowerTest method testHandleElectionTimeoutWhenLeaderMessageReceived.
@Test
public void testHandleElectionTimeoutWhenLeaderMessageReceived() {
logStart("testHandleElectionTimeoutWhenLeaderMessageReceived");
MockRaftActorContext context = createActorContext();
((DefaultConfigParamsImpl) context.getConfigParams()).setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
((DefaultConfigParamsImpl) context.getConfigParams()).setElectionTimeoutFactor(4);
follower = new Follower(context);
context.setCurrentBehavior(follower);
Uninterruptibles.sleepUninterruptibly(context.getConfigParams().getElectionTimeOutInterval().toMillis() - 100, TimeUnit.MILLISECONDS);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, (short) 1));
Uninterruptibles.sleepUninterruptibly(130, TimeUnit.MILLISECONDS);
RaftActorBehavior raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE);
assertTrue(raftBehavior instanceof Follower);
Uninterruptibles.sleepUninterruptibly(context.getConfigParams().getElectionTimeOutInterval().toMillis() - 150, TimeUnit.MILLISECONDS);
follower.handleMessage(leaderActor, new AppendEntries(1, "leader", -1, -1, Collections.emptyList(), -1, -1, (short) 1));
Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
raftBehavior = follower.handleMessage(leaderActor, ElectionTimeout.INSTANCE);
assertTrue(raftBehavior instanceof Follower);
}
use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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.MockRaftActorContext in project controller by opendaylight.
the class IsolatedLeaderTest method testHandleMessageWithFiveMembers.
@Test
public void testHandleMessageWithFiveMembers() throws Exception {
String followerAddress1 = "akka://test/user/$a";
String followerAddress2 = "akka://test/user/$b";
String followerAddress3 = "akka://test/user/$c";
String followerAddress4 = "akka://test/user/$d";
final MockRaftActorContext leaderActorContext = createActorContext();
Map<String, String> peerAddresses = new HashMap<>();
peerAddresses.put("follower-1", followerAddress1);
peerAddresses.put("follower-2", followerAddress2);
peerAddresses.put("follower-3", followerAddress3);
peerAddresses.put("follower-4", followerAddress4);
leaderActorContext.setPeerAddresses(peerAddresses);
isolatedLeader = new IsolatedLeader(leaderActorContext);
leaderActorContext.setCurrentBehavior(isolatedLeader);
assertEquals("Raft state", RaftState.IsolatedLeader, isolatedLeader.state());
// in a 5 member cluster, atleast 2 followers need to be active and return a reply
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.IsolatedLeader, newBehavior.state());
newBehavior = isolatedLeader.handleMessage(senderActor, new AppendEntriesReply("follower-2", isolatedLeader.lastTerm() - 1, true, isolatedLeader.lastIndex() - 1, isolatedLeader.lastTerm() - 1, (short) 0));
assertEquals("Raft state", RaftState.Leader, newBehavior.state());
isolatedLeader.close();
isolatedLeader = (AbstractLeader) newBehavior;
newBehavior = isolatedLeader.handleMessage(senderActor, new AppendEntriesReply("follower-3", isolatedLeader.lastTerm() - 1, true, isolatedLeader.lastIndex() - 1, isolatedLeader.lastTerm() - 1, (short) 0));
assertEquals("Raft state", RaftState.Leader, newBehavior.state());
}
Aggregations