Search in sources :

Example 71 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 72 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 73 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext 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 74 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.

the class LeaderTest method testInitiateInstallSnapshot.

@Test
public void testInitiateInstallSnapshot() throws Exception {
    logStart("testInitiateInstallSnapshot");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    final int followersLastIndex = 2;
    final int snapshotIndex = 3;
    final int newEntryIndex = 4;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.setLastApplied(3);
    actorContext.setCommitIndex(followersLastIndex);
    leader = new Leader(actorContext);
    // Leader will send an immediate heartbeat - ignore it.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    // set the snapshot as absent and check if capture-snapshot is invoked.
    leader.setSnapshotHolder(null);
    // new entry
    SimpleReplicatedLogEntry entry = new SimpleReplicatedLogEntry(newEntryIndex, currentTerm, new MockRaftActorContext.MockPayload("D"));
    actorContext.getReplicatedLog().append(entry);
    // update follower timestamp
    leader.markFollowerActive(FOLLOWER_ID);
    leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry, true));
    assertEquals("isCapturing", true, actorContext.getSnapshotManager().isCapturing());
    CaptureSnapshot cs = actorContext.getSnapshotManager().getCaptureSnapshot();
    assertEquals(3, cs.getLastAppliedIndex());
    assertEquals(1, cs.getLastAppliedTerm());
    assertEquals(4, cs.getLastIndex());
    assertEquals(2, cs.getLastTerm());
    // if an initiate is started again when first is in progress, it shouldnt initiate Capture
    leader.handleMessage(leaderActor, new Replicate(null, new MockIdentifier("state-id"), entry, true));
    assertSame("CaptureSnapshot instance", cs, actorContext.getSnapshotManager().getCaptureSnapshot());
}
Also used : Replicate(org.opendaylight.controller.cluster.raft.base.messages.Replicate) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) Test(org.junit.Test)

Example 75 with MockRaftActorContext

use of org.opendaylight.controller.cluster.raft.MockRaftActorContext in project controller by opendaylight.

the class LeaderTest method testHandleAppendEntriesReplyUnknownFollower.

@Test
public void testHandleAppendEntriesReplyUnknownFollower() {
    logStart("testHandleAppendEntriesReplyUnknownFollower");
    MockRaftActorContext leaderActorContext = createActorContext();
    leader = new Leader(leaderActorContext);
    AppendEntriesReply reply = new AppendEntriesReply("unkown-follower", 1, false, 10, 1, (short) 0);
    RaftActorBehavior raftActorBehavior = leader.handleAppendEntriesReply(followerActor, reply);
    assertEquals(RaftState.Leader, raftActorBehavior.state());
}
Also used : 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