Search in sources :

Example 86 with ActorRef

use of akka.actor.ActorRef in project controller by opendaylight.

the class DistributedEntityOwnershipIntegrationTest method testLeaderAndFollowerEntityOwnersReassignedAfterShutdown.

@Test
public void testLeaderAndFollowerEntityOwnersReassignedAfterShutdown() throws Exception {
    followerDatastoreContextBuilder.shardElectionTimeoutFactor(5).customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
    String name = "testLeaderAndFollowerEntityOwnersReassignedAfterShutdown";
    final MemberNode leaderNode = MemberNode.builder(memberNodes).akkaConfig("Member1").useAkkaArtery(false).testName(name).moduleShardsConfig(MODULE_SHARDS_5_NODE_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(leaderDatastoreContextBuilder).build();
    final MemberNode follower1Node = MemberNode.builder(memberNodes).akkaConfig("Member2").useAkkaArtery(false).testName(name).moduleShardsConfig(MODULE_SHARDS_5_NODE_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    final MemberNode follower2Node = MemberNode.builder(memberNodes).akkaConfig("Member3").useAkkaArtery(false).testName(name).moduleShardsConfig(MODULE_SHARDS_5_NODE_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    final MemberNode follower3Node = MemberNode.builder(memberNodes).akkaConfig("Member4").useAkkaArtery(false).testName(name).moduleShardsConfig(MODULE_SHARDS_5_NODE_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    final MemberNode follower4Node = MemberNode.builder(memberNodes).akkaConfig("Member5").useAkkaArtery(false).testName(name).moduleShardsConfig(MODULE_SHARDS_5_NODE_CONFIG).schemaContext(SCHEMA_CONTEXT).createOperDatastore(false).datastoreContextBuilder(followerDatastoreContextBuilder).build();
    AbstractDataStore leaderDistributedDataStore = leaderNode.configDataStore();
    leaderDistributedDataStore.waitTillReady();
    follower1Node.configDataStore().waitTillReady();
    follower2Node.configDataStore().waitTillReady();
    follower3Node.configDataStore().waitTillReady();
    follower4Node.configDataStore().waitTillReady();
    leaderNode.waitForMembersUp("member-2", "member-3", "member-4", "member-5");
    follower1Node.waitForMembersUp("member-1", "member-3", "member-4", "member-5");
    final DOMEntityOwnershipService leaderEntityOwnershipService = newOwnershipService(leaderDistributedDataStore);
    final DOMEntityOwnershipService follower1EntityOwnershipService = newOwnershipService(follower1Node.configDataStore());
    final DOMEntityOwnershipService follower2EntityOwnershipService = newOwnershipService(follower2Node.configDataStore());
    final DOMEntityOwnershipService follower3EntityOwnershipService = newOwnershipService(follower3Node.configDataStore());
    newOwnershipService(follower4Node.configDataStore());
    leaderNode.kit().waitUntilLeader(leaderNode.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    // Register follower1 candidate for entity1 and verify it becomes owner
    follower1EntityOwnershipService.registerCandidate(ENTITY1);
    verifyOwner(leaderDistributedDataStore, ENTITY1, "member-2");
    // Register leader candidate for entity1
    leaderEntityOwnershipService.registerCandidate(ENTITY1);
    verifyCandidates(leaderDistributedDataStore, ENTITY1, "member-2", "member-1");
    verifyOwner(leaderDistributedDataStore, ENTITY1, "member-2");
    // Register leader candidate for entity2 and verify it becomes owner
    leaderEntityOwnershipService.registerCandidate(ENTITY2);
    verifyOwner(leaderDistributedDataStore, ENTITY2, "member-1");
    // Register follower2 candidate for entity2
    follower2EntityOwnershipService.registerCandidate(ENTITY2);
    verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-1", "member-3");
    verifyOwner(leaderDistributedDataStore, ENTITY2, "member-1");
    // Register follower3 as a candidate for entity2 as well
    follower3EntityOwnershipService.registerCandidate(ENTITY2);
    verifyCandidates(leaderDistributedDataStore, ENTITY2, "member-1", "member-3", "member-4");
    verifyOwner(leaderDistributedDataStore, ENTITY2, "member-1");
    // Re-enable elections on all remaining followers so one becomes the new leader
    ActorRef follower1Shard = IntegrationTestKit.findLocalShard(follower1Node.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    follower1Shard.tell(DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
    ActorRef follower2Shard = IntegrationTestKit.findLocalShard(follower2Node.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    follower2Shard.tell(DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
    ActorRef follower4Shard = IntegrationTestKit.findLocalShard(follower4Node.configDataStore().getActorContext(), ENTITY_OWNERSHIP_SHARD_NAME);
    follower4Shard.tell(DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).customRaftPolicyImplementation(null).build(), ActorRef.noSender());
    // Shutdown the leader and follower3
    leaderNode.cleanup();
    follower3Node.cleanup();
    follower1Node.waitForMemberDown("member-1");
    follower1Node.waitForMemberDown("member-4");
    follower2Node.waitForMemberDown("member-1");
    follower2Node.waitForMemberDown("member-4");
    follower4Node.waitForMemberDown("member-1");
    follower4Node.waitForMemberDown("member-4");
    // Verify the prior leader's and follower3 entity owners are re-assigned.
    verifyCandidates(follower1Node.configDataStore(), ENTITY1, "member-2", "member-1");
    verifyCandidates(follower1Node.configDataStore(), ENTITY2, "member-1", "member-3", "member-4");
    verifyOwner(follower1Node.configDataStore(), ENTITY1, "member-2");
    verifyOwner(follower1Node.configDataStore(), ENTITY2, "member-3");
}
Also used : DOMEntityOwnershipService(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService) MemberNode(org.opendaylight.controller.cluster.datastore.MemberNode) ActorRef(akka.actor.ActorRef) DisableElectionsRaftPolicy(org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy) AbstractDataStore(org.opendaylight.controller.cluster.datastore.AbstractDataStore) Test(org.junit.Test)

Example 87 with ActorRef

use of akka.actor.ActorRef in project controller by opendaylight.

the class EntityOwnershipListenerSupportTest method testNotifyEntityOwnershipListeners.

@Test
public void testNotifyEntityOwnershipListeners() {
    EntityOwnershipListenerSupport support = new EntityOwnershipListenerSupport(actorContext, "test");
    DOMEntityOwnershipListener mockListener1 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1");
    DOMEntityOwnershipListener mockListener2 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener2");
    DOMEntityOwnershipListener mockListener12 = mock(DOMEntityOwnershipListener.class, "EntityOwnershipListener1_2");
    String entityType1 = "type1";
    String entityType2 = "type2";
    final DOMEntity entity1 = new DOMEntity(entityType1, YangInstanceIdentifier.of(QName.create("test", "id1")));
    final DOMEntity entity2 = new DOMEntity(entityType2, YangInstanceIdentifier.of(QName.create("test", "id2")));
    final DOMEntity entity3 = new DOMEntity("noListener", YangInstanceIdentifier.of(QName.create("test", "id5")));
    // Add EntityOwnershipListener registrations.
    support.addEntityOwnershipListener(entityType1, mockListener1);
    // register again - should be noop
    support.addEntityOwnershipListener(entityType1, mockListener1);
    support.addEntityOwnershipListener(entityType1, mockListener12);
    support.addEntityOwnershipListener(entityType2, mockListener2);
    // Notify entity1 changed and verify appropriate listeners are notified.
    support.notifyEntityOwnershipListeners(entity1, false, true, true);
    verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
    verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, true, true));
    Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
    verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    assertEquals("# of listener actors", 2, actorContext.children().size());
    reset(mockListener1, mockListener2, mockListener12);
    // Notify entity2 changed and verify appropriate listeners are notified.
    support.notifyEntityOwnershipListeners(entity2, false, true, true);
    verify(mockListener2, timeout(5000)).ownershipChanged(ownershipChange(entity2, false, true, true));
    Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
    verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    assertEquals("# of listener actors", 3, actorContext.children().size());
    reset(mockListener1, mockListener2, mockListener12);
    // Notify entity3 changed and verify no listeners are notified.
    support.notifyEntityOwnershipListeners(entity3, true, false, true);
    Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
    verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    reset(mockListener1, mockListener2, mockListener12);
    Iterable<ActorRef> listenerActors = actorContext.children();
    assertEquals("# of listener actors", 3, listenerActors.size());
    // Unregister mockListener1, issue a change for entity1 and verify only remaining listeners are notified.
    support.removeEntityOwnershipListener(entityType1, mockListener1);
    support.notifyEntityOwnershipListeners(entity1, true, false, true);
    verify(mockListener12, timeout(5000)).ownershipChanged(ownershipChange(entity1, true, false, true));
    Uninterruptibles.sleepUninterruptibly(300, TimeUnit.MILLISECONDS);
    verify(mockListener1, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    reset(mockListener1, mockListener2, mockListener12);
    // Unregister all listeners and verify their listener actors are destroyed.
    List<TestKit> watchers = new ArrayList<>();
    for (Iterator<ActorRef> iter = listenerActors.iterator(); iter.hasNext(); ) {
        TestKit kit = new TestKit(getSystem());
        kit.watch(iter.next());
        watchers.add(kit);
    }
    support.removeEntityOwnershipListener(entityType1, mockListener12);
    // un-register again - should be noop
    support.removeEntityOwnershipListener(entityType1, mockListener12);
    support.removeEntityOwnershipListener(entityType2, mockListener2);
    Iterator<ActorRef> iter = listenerActors.iterator();
    for (TestKit kit : watchers) {
        kit.expectTerminated(kit.duration("3 seconds"), iter.next());
    }
    assertEquals("# of listener actors", 0, actorContext.children().size());
    // Re-register mockListener1 and verify it is notified.
    reset(mockListener1, mockListener2);
    support.addEntityOwnershipListener(entityType1, mockListener1);
    support.notifyEntityOwnershipListeners(entity1, false, false, true);
    verify(mockListener1, timeout(5000)).ownershipChanged(ownershipChange(entity1, false, false, true));
    verify(mockListener12, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    verify(mockListener2, never()).ownershipChanged(any(DOMEntityOwnershipChange.class));
    // Quickly register and unregister mockListener2 - expecting no exceptions.
    support.addEntityOwnershipListener(entityType1, mockListener2);
    support.removeEntityOwnershipListener(entityType1, mockListener2);
}
Also used : DOMEntityOwnershipListener(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener) DOMEntityOwnershipChange(org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) ArrayList(java.util.ArrayList) DOMEntity(org.opendaylight.mdsal.eos.dom.api.DOMEntity) TestKit(akka.testkit.javadsl.TestKit) Test(org.junit.Test)

Example 88 with ActorRef

use of akka.actor.ActorRef in project controller by opendaylight.

the class ForwardingDataTreeChangeListenerTest method testOnDataChanged.

@Test
public void testOnDataChanged() throws Exception {
    final ActorRef actorRef = getSystem().actorOf(MessageCollectorActor.props());
    ForwardingDataTreeChangeListener forwardingListener = new ForwardingDataTreeChangeListener(getSystem().actorSelection(actorRef.path()));
    Collection<DataTreeCandidate> expected = Arrays.asList(Mockito.mock(DataTreeCandidate.class));
    forwardingListener.onDataTreeChanged(expected);
    DataTreeChanged actual = MessageCollectorActor.expectFirstMatching(actorRef, DataTreeChanged.class, 5000);
    Assert.assertSame(expected, actual.getChanges());
}
Also used : DataTreeCandidate(org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate) DataTreeChanged(org.opendaylight.controller.cluster.datastore.messages.DataTreeChanged) ActorRef(akka.actor.ActorRef) Test(org.junit.Test)

Example 89 with ActorRef

use of akka.actor.ActorRef in project controller by opendaylight.

the class IntegrationTestKit method waitUntilNoLeader.

public void waitUntilNoLeader(final ActorContext actorContext, final String... shardNames) {
    for (String shardName : shardNames) {
        ActorRef shard = findLocalShard(actorContext, shardName);
        assertNotNull("No local shard found for " + shardName, shard);
        waitUntilNoLeader(shard);
    }
}
Also used : ActorRef(akka.actor.ActorRef)

Example 90 with ActorRef

use of akka.actor.ActorRef in project controller by opendaylight.

the class IntegrationTestKit method verifyShardStats.

public static void verifyShardStats(final AbstractDataStore datastore, final String shardName, final ShardStatsVerifier verifier) throws Exception {
    ActorContext actorContext = datastore.getActorContext();
    Future<ActorRef> future = actorContext.findLocalShardAsync(shardName);
    ActorRef shardActor = Await.result(future, Duration.create(10, TimeUnit.SECONDS));
    AssertionError lastError = null;
    Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= 5) {
        ShardStats shardStats = (ShardStats) actorContext.executeOperation(shardActor, Shard.GET_SHARD_MBEAN_MESSAGE);
        try {
            verifier.verify(shardStats);
            return;
        } catch (AssertionError e) {
            lastError = e;
            Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
        }
    }
    throw lastError;
}
Also used : ShardStats(org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats) ActorRef(akka.actor.ActorRef) Stopwatch(com.google.common.base.Stopwatch) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext)

Aggregations

ActorRef (akka.actor.ActorRef)383 Test (org.junit.Test)253 TestActorRef (akka.testkit.TestActorRef)124 TestKit (akka.testkit.javadsl.TestKit)84 ActorSystem (akka.actor.ActorSystem)55 FiniteDuration (scala.concurrent.duration.FiniteDuration)53 Props (akka.actor.Props)46 Timeout (akka.util.Timeout)43 Configuration (org.apache.flink.configuration.Configuration)42 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)38 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)37 AkkaActorGateway (org.apache.flink.runtime.instance.AkkaActorGateway)33 ActorGateway (org.apache.flink.runtime.instance.ActorGateway)30 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)26 NormalizedNodeAggregatorTest (org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregatorTest)26 AddressFromURIString (akka.actor.AddressFromURIString)24 ArrayList (java.util.ArrayList)22 JobID (org.apache.flink.api.common.JobID)22 IOException (java.io.IOException)21 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)20