Search in sources :

Example 16 with ApplyState

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

the class LeadershipTransferIntegrationTest method sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1.

private void sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1() throws Exception {
    testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 starting");
    clearMessages(leaderNotifierActor);
    clearMessages(follower1NotifierActor);
    clearMessages(follower2NotifierActor);
    clearMessages(follower3NotifierActor);
    // Simulate a delay for follower2 in receiving the LeaderTransitioning message with null leader id.
    final TestRaftActor follower2Instance = follower2Actor.underlyingActor();
    follower2Instance.startDropMessages(LeaderTransitioning.class);
    FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
    final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
    verifyRaftState(follower1Actor, RaftState.Leader);
    Boolean stopped = Await.result(stopFuture, duration);
    assertEquals("Stopped", Boolean.TRUE, stopped);
    // Re-enable LeaderTransitioning messages to follower2.
    final LeaderTransitioning leaderTransitioning = expectFirstMatching(follower2CollectorActor, LeaderTransitioning.class);
    follower2Instance.stopDropMessages(LeaderTransitioning.class);
    follower2Instance.stopDropMessages(AppendEntries.class);
    ApplyState applyState = expectFirstMatching(follower2CollectorActor, ApplyState.class);
    assertEquals("Apply sate index", 0, applyState.getReplicatedLogEntry().getIndex());
    // Now send the LeaderTransitioning to follower2 after it has received AppendEntries from the new leader.
    follower2Actor.tell(leaderTransitioning, ActorRef.noSender());
    verifyLeaderStateChangedMessages(leaderNotifierActor, null, follower1Id);
    verifyLeaderStateChangedMessages(follower1NotifierActor, null, follower1Id);
    // follower2 should only get 1 LeaderStateChanged with the new leaderId - the LeaderTransitioning message
    // should not generate a LeaderStateChanged with null leaderId since it arrived after the new leaderId was set.
    verifyLeaderStateChangedMessages(follower2NotifierActor, follower1Id);
    verifyLeaderStateChangedMessages(follower3NotifierActor, null, follower1Id);
    testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 ending");
}
Also used : FiniteDuration(scala.concurrent.duration.FiniteDuration) LeaderTransitioning(org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState)

Example 17 with ApplyState

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

the class RaftActorServerConfigurationSupportTest method testChangeServersVotingStatus.

@Test
public void testChangeServersVotingStatus() {
    LOG.info("testChangeServersVotingStatus starting");
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    configParams.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    final String follower1ActorId = actorFactory.generateActorId(FOLLOWER_ID);
    final String follower1ActorPath = actorFactory.createTestActorPath(follower1ActorId);
    final String follower2ActorId = actorFactory.generateActorId(FOLLOWER_ID2);
    final String follower2ActorPath = actorFactory.createTestActorPath(follower2ActorId);
    TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, follower1ActorPath, FOLLOWER_ID2, follower2ActorPath), new MockRaftActorContext()).withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID));
    ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor());
    ActorRef follower1Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
    final TestActorRef<CollectingMockRaftActor> follower1RaftActor = actorFactory.createTestActor(CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID2, follower2ActorPath), configParams, NO_PERSISTENCE, follower1Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), follower1ActorId);
    ActorRef follower2Collector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
    final TestActorRef<CollectingMockRaftActor> follower2RaftActor = actorFactory.createTestActor(CollectingMockRaftActor.props(FOLLOWER_ID2, ImmutableMap.of(LEADER_ID, leaderActor.path().toString(), FOLLOWER_ID, follower1ActorPath), configParams, NO_PERSISTENCE, follower2Collector).withDispatcher(Dispatchers.DefaultDispatcherId()), follower2ActorId);
    // Send first ChangeServersVotingStatus message
    leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(FOLLOWER_ID, false, FOLLOWER_ID2, false)), testKit.getRef());
    ServerChangeReply reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class);
    assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus());
    final ApplyState applyState = MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class);
    assertEquals(0L, applyState.getReplicatedLogEntry().getIndex());
    verifyServerConfigurationPayloadEntry(leaderActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), nonVotingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    MessageCollectorActor.expectFirstMatching(follower1Collector, ApplyState.class);
    verifyServerConfigurationPayloadEntry(follower1RaftActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), nonVotingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    MessageCollectorActor.expectFirstMatching(follower2Collector, ApplyState.class);
    verifyServerConfigurationPayloadEntry(follower2RaftActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), nonVotingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    MessageCollectorActor.clearMessages(leaderCollector);
    MessageCollectorActor.clearMessages(follower1Collector);
    MessageCollectorActor.clearMessages(follower2Collector);
    // Send second ChangeServersVotingStatus message
    leaderActor.tell(new ChangeServersVotingStatus(ImmutableMap.of(FOLLOWER_ID, true)), testKit.getRef());
    reply = testKit.expectMsgClass(testKit.duration("5 seconds"), ServerChangeReply.class);
    assertEquals("getStatus", ServerChangeStatus.OK, reply.getStatus());
    MessageCollectorActor.expectFirstMatching(leaderCollector, ApplyState.class);
    verifyServerConfigurationPayloadEntry(leaderActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    MessageCollectorActor.expectFirstMatching(follower1Collector, ApplyState.class);
    verifyServerConfigurationPayloadEntry(follower1RaftActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    MessageCollectorActor.expectFirstMatching(follower2Collector, ApplyState.class);
    verifyServerConfigurationPayloadEntry(follower2RaftActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(LEADER_ID), votingServer(FOLLOWER_ID), nonVotingServer(FOLLOWER_ID2));
    LOG.info("testChangeServersVotingStatus ending");
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) ChangeServersVotingStatus(org.opendaylight.controller.cluster.raft.messages.ChangeServersVotingStatus) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ServerChangeReply(org.opendaylight.controller.cluster.raft.messages.ServerChangeReply) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 18 with ApplyState

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

the class RaftActorServerConfigurationSupportTest method testRemoveServerLeader.

@Test
public void testRemoveServerLeader() {
    LOG.info("testRemoveServerLeader starting");
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    configParams.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    final String followerActorId = actorFactory.generateActorId(FOLLOWER_ID);
    final String followerActorPath = actorFactory.createTestActorPath(followerActorId);
    RaftActorContext initialActorContext = new MockRaftActorContext();
    TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, followerActorPath), initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID));
    final ActorRef leaderCollector = newLeaderCollectorActor(leaderActor.underlyingActor());
    final ActorRef followerCollector = actorFactory.createActor(MessageCollectorActor.props(), actorFactory.generateActorId("collector"));
    actorFactory.createTestActor(CollectingMockRaftActor.props(FOLLOWER_ID, ImmutableMap.of(LEADER_ID, leaderActor.path().toString()), configParams, NO_PERSISTENCE, followerCollector).withDispatcher(Dispatchers.DefaultDispatcherId()), followerActorId);
    leaderActor.tell(new RemoveServer(LEADER_ID), testKit.getRef());
    RemoveServerReply removeServerReply = testKit.expectMsgClass(testKit.duration("5 seconds"), RemoveServerReply.class);
    assertEquals("getStatus", ServerChangeStatus.OK, removeServerReply.getStatus());
    final ApplyState applyState = MessageCollectorActor.expectFirstMatching(followerCollector, ApplyState.class);
    assertEquals(0L, applyState.getReplicatedLogEntry().getIndex());
    verifyServerConfigurationPayloadEntry(leaderActor.underlyingActor().getRaftActorContext().getReplicatedLog(), votingServer(FOLLOWER_ID));
    MessageCollectorActor.expectFirstMatching(leaderCollector, ServerRemoved.class);
    LOG.info("testRemoveServerLeader ending");
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) FiniteDuration(scala.concurrent.duration.FiniteDuration) RemoveServer(org.opendaylight.controller.cluster.raft.messages.RemoveServer) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) RemoveServerReply(org.opendaylight.controller.cluster.raft.messages.RemoveServerReply) Test(org.junit.Test)

Example 19 with ApplyState

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

the class RaftActorServerConfigurationSupportTest method testOnApplyState.

@Test
public void testOnApplyState() {
    LOG.info("testOnApplyState starting");
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
    configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
    TestActorRef<MockRaftActor> noLeaderActor = actorFactory.createTestActor(MockRaftActor.builder().id(LEADER_ID).peerAddresses(ImmutableMap.of(FOLLOWER_ID, followerActor.path().toString())).config(configParams).persistent(Optional.of(false)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), actorFactory.generateActorId(LEADER_ID));
    RaftActorServerConfigurationSupport support = new RaftActorServerConfigurationSupport(noLeaderActor.underlyingActor());
    ReplicatedLogEntry serverConfigEntry = new SimpleReplicatedLogEntry(1, 1, new ServerConfigurationPayload(Collections.<ServerInfo>emptyList()));
    boolean handled = support.handleMessage(new ApplyState(null, null, serverConfigEntry), ActorRef.noSender());
    assertEquals("Message handled", true, handled);
    ReplicatedLogEntry nonServerConfigEntry = new SimpleReplicatedLogEntry(1, 1, new MockRaftActorContext.MockPayload("1"));
    handled = support.handleMessage(new ApplyState(null, null, nonServerConfigEntry), ActorRef.noSender());
    assertEquals("Message handled", false, handled);
    LOG.info("testOnApplyState ending");
}
Also used : ServerInfo(org.opendaylight.controller.cluster.raft.persisted.ServerInfo) FiniteDuration(scala.concurrent.duration.FiniteDuration) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ServerConfigurationPayload(org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload) SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ApplyState(org.opendaylight.controller.cluster.raft.base.messages.ApplyState) Test(org.junit.Test)

Example 20 with ApplyState

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

Aggregations

ApplyState (org.opendaylight.controller.cluster.raft.base.messages.ApplyState)26 Test (org.junit.Test)17 SimpleReplicatedLogEntry (org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry)13 MockPayload (org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload)11 FiniteDuration (scala.concurrent.duration.FiniteDuration)9 Snapshot (org.opendaylight.controller.cluster.raft.persisted.Snapshot)6 MockRaftActorContext (org.opendaylight.controller.cluster.raft.MockRaftActorContext)5 ReplicatedLogEntry (org.opendaylight.controller.cluster.raft.ReplicatedLogEntry)5 CaptureSnapshot (org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot)5 InstallSnapshot (org.opendaylight.controller.cluster.raft.messages.InstallSnapshot)5 ApplyJournalEntries (org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries)5 FollowerLogInformation (org.opendaylight.controller.cluster.raft.FollowerLogInformation)4 ApplySnapshot (org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot)4 AppendEntries (org.opendaylight.controller.cluster.raft.messages.AppendEntries)4 AppendEntriesReply (org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply)4 ActorRef (akka.actor.ActorRef)3 TestActorRef (akka.testkit.TestActorRef)3 DefaultConfigParamsImpl (org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl)3 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)3 LeaderTransitioning (org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning)2