Search in sources :

Example 56 with MockRaftActorContext

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);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 57 with MockRaftActorContext

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);
}
Also used : TimeoutNow(org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) Test(org.junit.Test)

Example 58 with MockRaftActorContext

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);
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 59 with MockRaftActorContext

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);
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ArrayList(java.util.ArrayList) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 60 with MockRaftActorContext

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());
}
Also used : HashMap(java.util.HashMap) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) Test(org.junit.Test)

Aggregations

MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)100 Test (org.junit.Test)93 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)44 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)40 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)30 FiniteDuration (scala.concurrent.duration.FiniteDuration)29 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)28 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)24 ByteString (com.google.protobuf.ByteString)19 HashMap (java.util.HashMap)16 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)11 RequestVoteReply (org.opendaylight.controller.cluster.raft.messages.RequestVoteReply)11 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 RequestVote (org.opendaylight.controller.cluster.raft.messages.RequestVote)8 ArrayList (java.util.ArrayList)7 AbstractActorTest (org.opendaylight.controller.cluster.raft.AbstractActorTest)7 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)7 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)6 SendInstallSnapshot (org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot)6 InstallSnapshotReply (org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply)6