Search in sources :

Example 41 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class LeaderTest method testMultipleReplicateWithReplyShouldResultInAppendEntries.

@Test
public void testMultipleReplicateWithReplyShouldResultInAppendEntries() throws Exception {
    logStart("testMultipleReplicateWithReplyShouldResultInAppendEntries");
    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 < 3; i++) {
        sendReplicate(actorContext, lastIndex + i + 1);
        leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, lastIndex + i + 1, term, (short) 0));
    }
    for (int i = 3; i < 5; i++) {
        sendReplicate(actorContext, lastIndex + i + 1);
    }
    List<AppendEntries> allMessages = MessageCollectorActor.getAllMatching(followerActor, AppendEntries.class);
    // We expect 4 here because the first 3 replicate got a reply and so the 4th entry would
    // get sent to the follower - but not the 5th
    assertEquals("The number of append entries collected should be 4", 4, allMessages.size());
    for (int i = 0; i < 4; i++) {
        long expected = allMessages.get(i).getEntries().get(0).getIndex();
        assertEquals(expected, i + 2);
    }
}
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 42 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class LeaderTest method testHandleReplicateMessageSendAppendEntriesToFollower.

@Test
public void testHandleReplicateMessageSendAppendEntriesToFollower() throws Exception {
    logStart("testHandleReplicateMessageSendAppendEntriesToFollower");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    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();
    RaftActorBehavior raftBehavior = sendReplicate(actorContext, lastIndex + 1);
    // State should not change
    assertTrue(raftBehavior instanceof Leader);
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("getPrevLogIndex", lastIndex, appendEntries.getPrevLogIndex());
    assertEquals("getPrevLogTerm", term, appendEntries.getPrevLogTerm());
    assertEquals("Entries size", 1, appendEntries.getEntries().size());
    assertEquals("Entry getIndex", lastIndex + 1, appendEntries.getEntries().get(0).getIndex());
    assertEquals("Entry getTerm", term, appendEntries.getEntries().get(0).getTerm());
    assertEquals("Entry payload", "foo", appendEntries.getEntries().get(0).getData().toString());
    assertEquals("Commit Index", lastIndex, actorContext.getCommitIndex());
}
Also used : AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 43 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class LeaderTest method testLaggingFollowerStarvation.

@Test
public void testLaggingFollowerStarvation() throws Exception {
    logStart("testLaggingFollowerStarvation");
    String leaderActorId = actorFactory.generateActorId("leader");
    String follower1ActorId = actorFactory.generateActorId("follower");
    String follower2ActorId = actorFactory.generateActorId("follower");
    final ActorRef follower1Actor = actorFactory.createActor(MessageCollectorActor.props(), follower1ActorId);
    final ActorRef follower2Actor = actorFactory.createActor(MessageCollectorActor.props(), follower2ActorId);
    MockRaftActorContext leaderActorContext = new MockRaftActorContext(leaderActorId, getSystem(), leaderActor);
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(200, TimeUnit.MILLISECONDS));
    configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
    leaderActorContext.setConfigParams(configParams);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(1, 5, 1).build());
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put(follower1ActorId, follower1Actor.path().toString());
    peerAddresses.put(follower2ActorId, follower2Actor.path().toString());
    leaderActorContext.setPeerAddresses(peerAddresses);
    leaderActorContext.getTermInformation().update(1, leaderActorId);
    leader = createBehavior(leaderActorContext);
    leaderActor.underlyingActor().setBehavior(leader);
    for (int i = 1; i < 6; i++) {
        // Each AppendEntriesReply could end up rescheduling the heartbeat (without the fix for bug 2733)
        RaftActorBehavior newBehavior = leader.handleMessage(follower1Actor, new AppendEntriesReply(follower1ActorId, 1, true, i, 1, (short) 0));
        assertTrue(newBehavior == leader);
        Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);
    }
    // Check if the leader has been receiving SendHeartbeat messages despite getting AppendEntriesReply
    List<SendHeartBeat> heartbeats = MessageCollectorActor.getAllMatching(leaderActor, SendHeartBeat.class);
    assertTrue(String.format("%s heartbeat(s) is less than expected", heartbeats.size()), heartbeats.size() > 1);
    // Check if follower-2 got AppendEntries during this time and was not starved
    List<AppendEntries> appendEntries = MessageCollectorActor.getAllMatching(follower2Actor, AppendEntries.class);
    assertTrue(String.format("%s append entries is less than expected", appendEntries.size()), appendEntries.size() > 1);
}
Also used : HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) SendHeartBeat(org.opendaylight.controller.cluster.raft.base.messages.SendHeartBeat) Test(org.junit.Test)

Example 44 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class LeaderTest method testLeaderCreatedWithCommitIndexLessThanFollowersCommitIndex.

@Test
public void testLeaderCreatedWithCommitIndexLessThanFollowersCommitIndex() throws Exception {
    logStart("testLeaderCreatedWithCommitIndexLessThanFollowersCommitIndex");
    final MockRaftActorContext leaderActorContext = createActorContext();
    MockRaftActorContext followerActorContext = createActorContext(FOLLOWER_ID, followerActor);
    followerActorContext.setPeerAddresses(ImmutableMap.of(LEADER_ID, leaderActor.path().toString()));
    Follower follower = new Follower(followerActorContext);
    followerActor.underlyingActor().setBehavior(follower);
    followerActorContext.setCurrentBehavior(follower);
    Map<String, String> leaderPeerAddresses = new HashMap<>();
    leaderPeerAddresses.put(FOLLOWER_ID, followerActor.path().toString());
    leaderActorContext.setPeerAddresses(leaderPeerAddresses);
    leaderActorContext.getReplicatedLog().removeFrom(0);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
    leaderActorContext.setCommitIndex(1);
    followerActorContext.getReplicatedLog().removeFrom(0);
    followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
    // follower has the same log entries but its commit index > leaders commit index
    followerActorContext.setCommitIndex(2);
    leader = new Leader(leaderActorContext);
    // Initial heartbeat
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals(-1, appendEntries.getLeaderCommit());
    assertEquals(0, appendEntries.getEntries().size());
    assertEquals(0, appendEntries.getPrevLogIndex());
    AppendEntriesReply appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertEquals(2, appendEntriesReply.getLogLastIndex());
    assertEquals(1, appendEntriesReply.getLogLastTerm());
    leaderActor.underlyingActor().setBehavior(follower);
    leader.handleMessage(followerActor, appendEntriesReply);
    leaderActor.underlyingActor().clear();
    followerActor.underlyingActor().clear();
    Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getHeartBeatInterval().toMillis(), TimeUnit.MILLISECONDS);
    leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
    appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals(2, appendEntries.getLeaderCommit());
    assertEquals(0, appendEntries.getEntries().size());
    assertEquals(2, appendEntries.getPrevLogIndex());
    appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertEquals(2, appendEntriesReply.getLogLastIndex());
    assertEquals(1, appendEntriesReply.getLogLastTerm());
    assertEquals(2, followerActorContext.getCommitIndex());
    follower.close();
}
Also used : HashMap(java.util.HashMap) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) Test(org.junit.Test)

Example 45 with AppendEntries

use of org.opendaylight.controller.cluster.raft.messages.AppendEntries in project controller by opendaylight.

the class LeaderTest method testReplicationWithPayloadSizeThatExceedsThreshold.

@Test
public void testReplicationWithPayloadSizeThatExceedsThreshold() {
    logStart("testReplicationWithPayloadSizeThatExceedsThreshold");
    final int serializedSize = SerializationUtils.serialize(new AppendEntries(1, LEADER_ID, -1, -1, Arrays.asList(new SimpleReplicatedLogEntry(0, 1, new MockRaftActorContext.MockPayload("large"))), 0, -1, (short) 0)).length;
    final MockRaftActorContext.MockPayload largePayload = new MockRaftActorContext.MockPayload("large", serializedSize);
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(300, TimeUnit.MILLISECONDS));
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setSnapshotChunkSize(serializedSize - 50);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
    leaderActorContext.setCommitIndex(-1);
    leaderActorContext.setLastApplied(-1);
    leader = new Leader(leaderActorContext);
    leaderActorContext.setCurrentBehavior(leader);
    // Send initial heartbeat reply so follower is marked active
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, -1, true, -1, -1, (short) 0));
    MessageCollectorActor.clearMessages(followerActor);
    // Send normal payload first to prime commit index.
    final long term = leaderActorContext.getTermInformation().getCurrentTerm();
    sendReplicate(leaderActorContext, term, 0);
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("Entries size", 1, appendEntries.getEntries().size());
    assertEquals("Entry getIndex", 0, appendEntries.getEntries().get(0).getIndex());
    leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, 0, term, (short) 0));
    assertEquals("getCommitIndex", 0, leaderActorContext.getCommitIndex());
    MessageCollectorActor.clearMessages(followerActor);
    // Now send a large payload that exceeds the maximum size for a single AppendEntries - it should be sliced.
    sendReplicate(leaderActorContext, term, 1, largePayload);
    MessageSlice messageSlice = MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
    assertEquals("getSliceIndex", 1, messageSlice.getSliceIndex());
    assertEquals("getTotalSlices", 2, messageSlice.getTotalSlices());
    final Identifier slicingId = messageSlice.getIdentifier();
    appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("getPrevLogIndex", 0, appendEntries.getPrevLogIndex());
    assertEquals("getPrevLogTerm", term, appendEntries.getPrevLogTerm());
    assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
    assertEquals("Entries size", 0, appendEntries.getEntries().size());
    MessageCollectorActor.clearMessages(followerActor);
    // Initiate a heartbeat - it should send an empty AppendEntries since slicing is in progress.
    // Sleep for the heartbeat interval so AppendEntries is sent.
    Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getHeartBeatInterval().toMillis(), TimeUnit.MILLISECONDS);
    leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
    appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
    assertEquals("Entries size", 0, appendEntries.getEntries().size());
    MessageCollectorActor.clearMessages(followerActor);
    // Simulate the MessageSliceReply's and AppendEntriesReply from the follower.
    leader.handleMessage(followerActor, MessageSliceReply.success(slicingId, 1, followerActor));
    messageSlice = MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
    assertEquals("getSliceIndex", 2, messageSlice.getSliceIndex());
    leader.handleMessage(followerActor, MessageSliceReply.success(slicingId, 2, followerActor));
    leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, 1, term, (short) 0));
    MessageCollectorActor.clearMessages(followerActor);
    // Send another normal payload.
    sendReplicate(leaderActorContext, term, 2);
    appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("Entries size", 1, appendEntries.getEntries().size());
    assertEquals("Entry getIndex", 2, appendEntries.getEntries().get(0).getIndex());
    assertEquals("getLeaderCommit", 1, appendEntries.getLeaderCommit());
}
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) Identifier(org.opendaylight.yangtools.concepts.Identifier) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) AppendEntriesReply(org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply) MessageSlice(org.opendaylight.controller.cluster.messaging.MessageSlice) 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