Search in sources :

Example 36 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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 37 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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 38 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) 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 39 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) 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) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 40 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries 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());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) 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) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ReplicatedLogEntry(org.opendaylight.controller.cluster.raft.ReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Aggregations

AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)62 Test (org.junit.Test)58 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)44 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)34 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)34 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)30 FiniteDuration (scala.concurrent.duration.FiniteDuration)19 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)17 ByteString (com.google.protobuf.ByteString)15 ActorRef (akka.actor.ActorRef)9 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)9 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)9 TestActorRef (akka.testkit.TestActorRef)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)5 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)4 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)4