Search in sources :

Example 31 with MockRaftActorContext

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

the class LeaderTest method setupIsolatedLeaderCheckTestWithTwoFollowers.

private RaftActorBehavior setupIsolatedLeaderCheckTestWithTwoFollowers(final RaftPolicy raftPolicy) {
    ActorRef followerActor1 = getSystem().actorOf(MessageCollectorActor.props(), "follower-1");
    ActorRef followerActor2 = getSystem().actorOf(MessageCollectorActor.props(), "follower-2");
    MockRaftActorContext leaderActorContext = createActorContext();
    Map<String, String> peerAddresses = new HashMap<>();
    peerAddresses.put("follower-1", followerActor1.path().toString());
    peerAddresses.put("follower-2", followerActor2.path().toString());
    leaderActorContext.setPeerAddresses(peerAddresses);
    leaderActorContext.setRaftPolicy(raftPolicy);
    leader = new Leader(leaderActorContext);
    leader.markFollowerActive("follower-1");
    leader.markFollowerActive("follower-2");
    RaftActorBehavior newBehavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
    assertTrue("Behavior not instance of Leader when all followers are active", newBehavior instanceof Leader);
    // kill 1 follower and verify if that got killed
    final TestKit probe = new TestKit(getSystem());
    probe.watch(followerActor1);
    followerActor1.tell(PoisonPill.getInstance(), ActorRef.noSender());
    final Terminated termMsg1 = probe.expectMsgClass(Terminated.class);
    assertEquals(termMsg1.getActor(), followerActor1);
    leader.markFollowerInActive("follower-1");
    leader.markFollowerActive("follower-2");
    newBehavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
    assertTrue("Behavior not instance of Leader when majority of followers are active", newBehavior instanceof Leader);
    // kill 2nd follower and leader should change to Isolated leader
    followerActor2.tell(PoisonPill.getInstance(), null);
    probe.watch(followerActor2);
    followerActor2.tell(PoisonPill.getInstance(), ActorRef.noSender());
    final Terminated termMsg2 = probe.expectMsgClass(Terminated.class);
    assertEquals(termMsg2.getActor(), followerActor2);
    leader.markFollowerInActive("follower-2");
    return leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
}
Also used : HashMap(java.util.HashMap) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) TestKit(akka.testkit.javadsl.TestKit) Terminated(akka.actor.Terminated)

Example 32 with MockRaftActorContext

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

the class LeaderTest method testForceInstallSnapshot.

@Test
public void testForceInstallSnapshot() throws Exception {
    logStart("testForceInstallSnapshot");
    final MockRaftActorContext actorContext = createActorContextWithFollower();
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    final int lastAppliedIndex = 3;
    final int snapshotIndex = -1;
    final int snapshotTerm = -1;
    final int currentTerm = 2;
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    actorContext.setCommitIndex(lastAppliedIndex);
    actorContext.setLastApplied(lastAppliedIndex);
    leader = new Leader(actorContext);
    // Initial heartbeat.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(-1);
    byte[] bytes = toByteString(leadersSnapshot).toByteArray();
    Snapshot snapshot = Snapshot.create(ByteState.of(bytes), Collections.<ReplicatedLogEntry>emptyList(), lastAppliedIndex, snapshotTerm, lastAppliedIndex, snapshotTerm, -1, null, null);
    RaftActorBehavior raftBehavior = leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bytes)));
    assertTrue(raftBehavior instanceof Leader);
    // check if installsnapshot gets called with the correct values.
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertNotNull(installSnapshot.getData());
    assertEquals(lastAppliedIndex, installSnapshot.getLastIncludedIndex());
    assertEquals(snapshotTerm, installSnapshot.getLastIncludedTerm());
    assertEquals(currentTerm, installSnapshot.getTerm());
}
Also used : Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 33 with MockRaftActorContext

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

the class LeaderTest method testLargePayloadSlicingExpiration.

@Test
public void testLargePayloadSlicingExpiration() {
    logStart("testLargePayloadSlicingExpiration");
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setElectionTimeoutFactor(1);
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setSnapshotChunkSize(10);
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
    leaderActorContext.setCommitIndex(-1);
    leaderActorContext.setLastApplied(-1);
    final long term = leaderActorContext.getTermInformation().getCurrentTerm();
    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);
    sendReplicate(leaderActorContext, term, 0, new MockRaftActorContext.MockPayload("large", leaderActorContext.getConfigParams().getSnapshotChunkSize() + 1));
    MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
    // Sleep for at least 3 * election timeout so the slicing state expires.
    Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getElectionTimeOutInterval().toMillis() * 3 + 50, TimeUnit.MILLISECONDS);
    MessageCollectorActor.clearMessages(followerActor);
    leader.handleMessage(leaderActor, SendHeartBeat.INSTANCE);
    AppendEntries appendEntries = MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    assertEquals("getLeaderCommit", -1, appendEntries.getLeaderCommit());
    assertEquals("Entries size", 0, appendEntries.getEntries().size());
    MessageCollectorActor.assertNoneMatching(followerActor, MessageSlice.class, 300);
    MessageCollectorActor.clearMessages(followerActor);
    // Send an AppendEntriesReply - this should restart the slicing.
    Uninterruptibles.sleepUninterruptibly(leaderActorContext.getConfigParams().getHeartBeatInterval().toMillis() + 50, TimeUnit.MILLISECONDS);
    leader.handleMessage(followerActor, new AppendEntriesReply(FOLLOWER_ID, term, true, -1, term, (short) 0));
    MessageCollectorActor.expectFirstMatching(followerActor, MessageSlice.class);
}
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 34 with MockRaftActorContext

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

the class LeaderTest method createActorContext.

private MockRaftActorContext createActorContext(final String id, final ActorRef actorRef) {
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(50, TimeUnit.MILLISECONDS));
    configParams.setElectionTimeoutFactor(100000);
    MockRaftActorContext context = new MockRaftActorContext(id, getSystem(), actorRef);
    context.setConfigParams(configParams);
    context.setPayloadVersion(payloadVersion);
    return context;
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)

Example 35 with MockRaftActorContext

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

the class LeaderTest method testHandleAppendEntriesReplyWithNewerTermWhenElectionsAreDisabled.

@Test
public void testHandleAppendEntriesReplyWithNewerTermWhenElectionsAreDisabled() {
    logStart("testHandleAppendEntriesReplyWithNewerTermWhenElectionsAreDisabled");
    MockRaftActorContext leaderActorContext = createActorContext();
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(10000, TimeUnit.SECONDS));
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 2).build());
    leaderActorContext.setRaftPolicy(createRaftPolicy(false, false));
    leader = new Leader(leaderActorContext);
    leaderActor.underlyingActor().setBehavior(leader);
    leaderActor.tell(new AppendEntriesReply("foo", 20, false, 1000, 10, (short) 1), ActorRef.noSender());
    AppendEntriesReply appendEntriesReply = MessageCollectorActor.expectFirstMatching(leaderActor, AppendEntriesReply.class);
    assertEquals(false, appendEntriesReply.isSuccess());
    assertEquals(RaftState.Leader, leaderActor.underlyingActor().getFirstBehaviorChange().state());
    MessageCollectorActor.clearMessages(leaderActor);
}
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) 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