Search in sources :

Example 1 with LeaderStateChanged

use of org.opendaylight.controller.cluster.notifications.LeaderStateChanged in project controller by opendaylight.

the class RoleChangeListenerActorTest method testOnDataTreeChanged.

@Test
public void testOnDataTreeChanged() {
    final LeaderLocationListener listener = mock(LeaderLocationListener.class);
    doNothing().when(listener).onLeaderLocationChanged(any());
    final Props props = RoleChangeListenerActor.props(getSystem().deadLetters(), listener);
    final ActorRef subject = getSystem().actorOf(props, "testDataTreeChangedChanged");
    subject.tell(new LeaderStateChanged("member-1", null, (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.UNKNOWN));
    subject.tell(new LeaderStateChanged("member-1", "member-1", (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.LOCAL));
    subject.tell(new LeaderStateChanged("member-1", "member-2", (short) 0), noSender());
    verify(listener, timeout(5000)).onLeaderLocationChanged(eq(LeaderLocation.REMOTE));
}
Also used : ActorRef(akka.actor.ActorRef) LeaderLocationListener(org.opendaylight.controller.cluster.dom.api.LeaderLocationListener) Props(akka.actor.Props) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) AbstractActorTest(org.opendaylight.controller.cluster.datastore.AbstractActorTest) Test(org.junit.Test)

Example 2 with LeaderStateChanged

use of org.opendaylight.controller.cluster.notifications.LeaderStateChanged in project controller by opendaylight.

the class NonVotingFollowerIntegrationTest method testFollowerLeaderStateChanges.

@Test
public void testFollowerLeaderStateChanges() {
    testLog.info("testFollowerLeaderStateChanges");
    ActorRef roleChangeNotifier = factory.createActor(MessageCollectorActor.props(), factory.generateActorId("roleChangeNotifier"));
    follower1Builder.roleChangeNotifier(roleChangeNotifier);
    setupLeaderAndNonVotingFollower();
    ((DefaultConfigParamsImpl) follower1Context.getConfigParams()).setElectionTimeoutFactor(2);
    ((DefaultConfigParamsImpl) follower1Context.getConfigParams()).setHeartBeatInterval(FiniteDuration.apply(100, TimeUnit.MILLISECONDS));
    MessageCollectorActor.clearMessages(roleChangeNotifier);
    follower1Actor.tell(ElectionTimeout.INSTANCE, ActorRef.noSender());
    followerInstance.startDropMessages(AppendEntries.class);
    LeaderStateChanged leaderStateChanged = MessageCollectorActor.expectFirstMatching(roleChangeNotifier, LeaderStateChanged.class);
    assertEquals("getLeaderId", null, leaderStateChanged.getLeaderId());
    MessageCollectorActor.clearMessages(roleChangeNotifier);
    followerInstance.stopDropMessages(AppendEntries.class);
    leaderStateChanged = MessageCollectorActor.expectFirstMatching(roleChangeNotifier, LeaderStateChanged.class);
    assertEquals("getLeaderId", leaderId, leaderStateChanged.getLeaderId());
}
Also used : ActorRef(akka.actor.ActorRef) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) Test(org.junit.Test)

Example 3 with LeaderStateChanged

use of org.opendaylight.controller.cluster.notifications.LeaderStateChanged in project controller by opendaylight.

the class RaftActorTest method testLeaderTransitioning.

@Test
public void testLeaderTransitioning() throws Exception {
    TEST_LOG.info("testLeaderTransitioning starting");
    ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
    String persistenceId = factory.generateActorId("test-actor-");
    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).roleChangeNotifier(notifierActor).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();
    mockRaftActor.waitForInitializeBehaviorComplete();
    raftActorRef.tell(new AppendEntries(1L, "leader", 0L, 1L, Collections.<ReplicatedLogEntry>emptyList(), 0L, -1L, (short) 1), ActorRef.noSender());
    LeaderStateChanged leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals("getLeaderId", "leader", leaderStateChange.getLeaderId());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActorRef.tell(new LeaderTransitioning("leader"), ActorRef.noSender());
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals("getMemberId", persistenceId, leaderStateChange.getMemberId());
    assertEquals("getLeaderId", null, leaderStateChange.getLeaderId());
    TEST_LOG.info("testLeaderTransitioning ending");
}
Also used : SimpleReplicatedLogEntry(org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) ByteString(com.google.protobuf.ByteString) AppendEntries(org.opendaylight.controller.cluster.raft.messages.AppendEntries) LeaderTransitioning(org.opendaylight.controller.cluster.raft.base.messages.LeaderTransitioning) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) Test(org.junit.Test)

Example 4 with LeaderStateChanged

use of org.opendaylight.controller.cluster.notifications.LeaderStateChanged in project controller by opendaylight.

the class RaftActorTest method testRaftRoleChangeNotifierWhenRaftActorHasNoPeers.

@Test
public void testRaftRoleChangeNotifierWhenRaftActorHasNoPeers() throws Exception {
    ActorRef notifierActor = factory.createActor(MessageCollectorActor.props());
    MessageCollectorActor.waitUntilReady(notifierActor);
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    long heartBeatInterval = 100;
    config.setHeartBeatInterval(FiniteDuration.create(heartBeatInterval, TimeUnit.MILLISECONDS));
    config.setElectionTimeoutFactor(20);
    String persistenceId = factory.generateActorId("notifier-");
    final TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config).roleChangeNotifier(notifierActor).dataPersistenceProvider(createProvider()).props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    List<RoleChanged> matches = MessageCollectorActor.expectMatching(notifierActor, RoleChanged.class, 3);
    // check if the notifier got a role change from null to Follower
    RoleChanged raftRoleChanged = matches.get(0);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertNull(raftRoleChanged.getOldRole());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
    // check if the notifier got a role change from Follower to Candidate
    raftRoleChanged = matches.get(1);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Candidate.name(), raftRoleChanged.getNewRole());
    // check if the notifier got a role change from Candidate to Leader
    raftRoleChanged = matches.get(2);
    assertEquals(persistenceId, raftRoleChanged.getMemberId());
    assertEquals(RaftState.Candidate.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Leader.name(), raftRoleChanged.getNewRole());
    LeaderStateChanged leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(raftRoleChanged.getMemberId(), leaderStateChange.getLeaderId());
    assertEquals(MockRaftActor.PAYLOAD_VERSION, leaderStateChange.getLeaderPayloadVersion());
    MessageCollectorActor.clearMessages(notifierActor);
    MockRaftActor raftActor = raftActorRef.underlyingActor();
    final String newLeaderId = "new-leader";
    final short newLeaderVersion = 6;
    Follower follower = new Follower(raftActor.getRaftActorContext()) {

        @Override
        public RaftActorBehavior handleMessage(final ActorRef sender, final Object message) {
            setLeaderId(newLeaderId);
            setLeaderPayloadVersion(newLeaderVersion);
            return this;
        }
    };
    raftActor.newBehavior(follower);
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(persistenceId, leaderStateChange.getMemberId());
    assertEquals(null, leaderStateChange.getLeaderId());
    raftRoleChanged = MessageCollectorActor.expectFirstMatching(notifierActor, RoleChanged.class);
    assertEquals(RaftState.Leader.name(), raftRoleChanged.getOldRole());
    assertEquals(RaftState.Follower.name(), raftRoleChanged.getNewRole());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActor.handleCommand("any");
    leaderStateChange = MessageCollectorActor.expectFirstMatching(notifierActor, LeaderStateChanged.class);
    assertEquals(persistenceId, leaderStateChange.getMemberId());
    assertEquals(newLeaderId, leaderStateChange.getLeaderId());
    assertEquals(newLeaderVersion, leaderStateChange.getLeaderPayloadVersion());
    MessageCollectorActor.clearMessages(notifierActor);
    raftActor.handleCommand("any");
    Uninterruptibles.sleepUninterruptibly(505, TimeUnit.MILLISECONDS);
    leaderStateChange = MessageCollectorActor.getFirstMatching(notifierActor, LeaderStateChanged.class);
    assertNull(leaderStateChange);
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) Follower(org.opendaylight.controller.cluster.raft.behaviors.Follower) Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) RoleChanged(org.opendaylight.controller.cluster.notifications.RoleChanged) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) Test(org.junit.Test)

Example 5 with LeaderStateChanged

use of org.opendaylight.controller.cluster.notifications.LeaderStateChanged in project controller by opendaylight.

the class ShardManagerTest method testOnReceiveFindPrimaryForNonLocalLeaderShardBeforeMemberUp.

@Test
public void testOnReceiveFindPrimaryForNonLocalLeaderShardBeforeMemberUp() throws Exception {
    LOG.info("testOnReceiveFindPrimaryForNonLocalLeaderShardBeforeMemberUp starting");
    new TestKit(getSystem()) {

        {
            final ActorRef shardManager = actorFactory.createActor(newPropsShardMgrWithMockShardActor());
            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager.tell(new ActorInitialized(), mockShardActor);
            String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
            String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
            shardManager.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor);
            shardManager.tell(new LeaderStateChanged(memberId1, memberId2, DataStoreVersions.CURRENT_VERSION), mockShardActor);
            shardManager.tell(new FindPrimary(Shard.DEFAULT_NAME, false), getRef());
            expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
        }
    };
    LOG.info("testOnReceiveFindPrimaryForNonLocalLeaderShardBeforeMemberUp ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) LeaderStateChanged(org.opendaylight.controller.cluster.notifications.LeaderStateChanged) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Aggregations

Test (org.junit.Test)6 LeaderStateChanged (org.opendaylight.controller.cluster.notifications.LeaderStateChanged)6 ActorRef (akka.actor.ActorRef)5 TestActorRef (akka.testkit.TestActorRef)3 TestKit (akka.testkit.javadsl.TestKit)2 ByteString (com.google.protobuf.ByteString)2 AddressFromURIString (akka.actor.AddressFromURIString)1 Props (akka.actor.Props)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 AbstractActorTest (org.opendaylight.controller.cluster.datastore.AbstractActorTest)1 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)1 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)1 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)1 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)1 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)1 LeaderLocationListener (org.opendaylight.controller.cluster.dom.api.LeaderLocationListener)1 RegisterRoleChangeListener (org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener)1 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)1 RoleChangeNotifier (org.opendaylight.controller.cluster.notifications.RoleChangeNotifier)1 RoleChanged (org.opendaylight.controller.cluster.notifications.RoleChanged)1