Search in sources :

Example 11 with DefaultConfigParamsImpl

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

the class LeaderTest method testTransferLeadershipWithFollowerInSync.

@Test
public void testTransferLeadershipWithFollowerInSync() {
    logStart("testTransferLeadershipWithFollowerInSync");
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    leaderActorContext.setLastApplied(-1);
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
    leaderActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
    leader = new Leader(leaderActorContext);
    leaderActorContext.setCurrentBehavior(leader);
    // Initial heartbeat
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, -1, -1, (short) 0));
    MessageCollectorActor.clearMessages(followerActor);
    sendReplicate(leaderActorContext, 0);
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 0, 1, (short) 0));
    MessageCollectorActor.expectFirstMatching(leaderActor, ApplyState.class);
    MessageCollectorActor.clearMessages(followerActor);
    RaftActorLeadershipTransferCohort mockTransferCohort = mock(RaftActorLeadershipTransferCohort.class);
    leader.transferLeadership(mockTransferCohort);
    verify(mockTransferCohort, never()).transferComplete();
    doReturn(Optional.absent()).when(mockTransferCohort).getRequestedFollowerId();
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    leader.handleMessage(leaderActor, new AppendEntriesReply(FOLLOWER_ID, 1, true, 0, 1, (short) 0));
    // Expect a final AppendEntries to ensure the follower's lastApplied index is up-to-date
    MessageCollectorActor.expectMatching(followerActor, AppendEntries.class, 2);
    // Leader should force an election timeout
    MessageCollectorActor.expectFirstMatching(followerActor, TimeoutNow.class);
    verify(mockTransferCohort).transferComplete();
}
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) RaftActorLeadershipTransferCohort(org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort) Test(org.junit.Test)

Example 12 with DefaultConfigParamsImpl

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

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

use of org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl 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)

Example 15 with DefaultConfigParamsImpl

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

the class LeaderTest method testIsolatedLeaderCheckNoVotingFollowers.

@Test
public void testIsolatedLeaderCheckNoVotingFollowers() {
    logStart("testIsolatedLeaderCheckNoVotingFollowers");
    MockRaftActorContext followerActorContext = createFollowerActorContextWithLeader();
    Follower follower = new Follower(followerActorContext);
    followerActor.underlyingActor().setBehavior(follower);
    MockRaftActorContext leaderActorContext = createActorContextWithFollower();
    ((DefaultConfigParamsImpl) leaderActorContext.getConfigParams()).setHeartBeatInterval(new FiniteDuration(1000, TimeUnit.SECONDS));
    leaderActorContext.getPeerInfo(FOLLOWER_ID).setVotingState(VotingState.NON_VOTING);
    leader = new Leader(leaderActorContext);
    leader.getFollower(FOLLOWER_ID).markFollowerActive();
    RaftActorBehavior newBehavior = leader.handleMessage(leaderActor, Leader.ISOLATED_LEADER_CHECK);
    assertTrue("Expected Leader", newBehavior instanceof Leader);
}
Also used : MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) Test(org.junit.Test)

Aggregations

DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)39 Test (org.junit.Test)31 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)31 FiniteDuration (scala.concurrent.duration.FiniteDuration)26 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)22 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)17 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)10 ByteString (com.google.protobuf.ByteString)8 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)7 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)6 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)6 HashMap (java.util.HashMap)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 RaftActorLeadershipTransferCohort (org.opendaylight.controller.cluster.raft.RaftActorLeadershipTransferCohort)4 ActorRef (akka.actor.ActorRef)3 TestActorRef (akka.testkit.TestActorRef)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 MockRaftActor (org.opendaylight.controller.cluster.raft.MockRaftActor)3 Builder (org.opendaylight.controller.cluster.raft.MockRaftActor.Builder)3 RaftActorSnapshotCohort (org.opendaylight.controller.cluster.raft.RaftActorSnapshotCohort)3