Search in sources :

Example 16 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration in project controller by opendaylight.

the class ShardManagerTest method testShardAvailabilityOnChangeOfMemberReachability.

@Test
public void testShardAvailabilityOnChangeOfMemberReachability() throws Exception {
    LOG.info("testShardAvailabilityOnChangeOfMemberReachability starting");
    String shardManagerID = ShardManagerIdentifier.builder().type(shardMrgIDSuffix).build().toString();
    // Create an ActorSystem ShardManager actor for member-1.
    final ActorSystem system1 = newActorSystem("Member1");
    Cluster.get(system1).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    final ActorRef mockShardActor1 = newMockShardActor(system1, Shard.DEFAULT_NAME, "member-1");
    final TestActorRef<TestShardManager> shardManager1 = TestActorRef.create(system1, newTestShardMgrBuilder().shardActor(mockShardActor1).cluster(new ClusterWrapperImpl(system1)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    // Create an ActorSystem ShardManager actor for member-2.
    final ActorSystem system2 = newActorSystem("Member2");
    Cluster.get(system2).join(AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558"));
    final ActorRef mockShardActor2 = newMockShardActor(system2, Shard.DEFAULT_NAME, "member-2");
    MockConfiguration mockConfig2 = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("default", Arrays.asList("member-1", "member-2")).build());
    final TestActorRef<TestShardManager> shardManager2 = TestActorRef.create(system2, newTestShardMgrBuilder(mockConfig2).shardActor(mockShardActor2).cluster(new ClusterWrapperImpl(system2)).props().withDispatcher(Dispatchers.DefaultDispatcherId()), shardManagerID);
    new TestKit(system1) {

        {
            shardManager1.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager2.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager1.tell(new ActorInitialized(), mockShardActor1);
            shardManager2.tell(new ActorInitialized(), mockShardActor2);
            String memberId2 = "member-2-shard-default-" + shardMrgIDSuffix;
            String memberId1 = "member-1-shard-default-" + shardMrgIDSuffix;
            shardManager1.tell(new ShardLeaderStateChanged(memberId1, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor1);
            shardManager1.tell(new RoleChangeNotification(memberId1, RaftState.Candidate.name(), RaftState.Follower.name()), mockShardActor1);
            shardManager2.tell(new ShardLeaderStateChanged(memberId2, memberId2, mock(DataTree.class), DataStoreVersions.CURRENT_VERSION), mockShardActor2);
            shardManager2.tell(new RoleChangeNotification(memberId2, RaftState.Candidate.name(), RaftState.Leader.name()), mockShardActor2);
            shardManager1.underlyingActor().waitForMemberUp();
            shardManager1.tell(new FindPrimary("default", true), getRef());
            RemotePrimaryShardFound found = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
            String path = found.getPrimaryPath();
            assertTrue("Unexpected primary path " + path, path.contains("member-2-shard-default-config"));
            shardManager1.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            shardManager1.underlyingActor().waitForUnreachableMember();
            PeerDown peerDown = MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerDown.class);
            assertEquals("getMemberName", MEMBER_2, peerDown.getMemberName());
            MessageCollectorActor.clearMessages(mockShardActor1);
            shardManager1.tell(MockClusterWrapper.createMemberRemoved("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerDown.class);
            shardManager1.tell(new FindPrimary("default", true), getRef());
            expectMsgClass(duration("5 seconds"), NoShardLeaderException.class);
            shardManager1.tell(MockClusterWrapper.createReachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            shardManager1.underlyingActor().waitForReachableMember();
            PeerUp peerUp = MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerUp.class);
            assertEquals("getMemberName", MEMBER_2, peerUp.getMemberName());
            MessageCollectorActor.clearMessages(mockShardActor1);
            shardManager1.tell(new FindPrimary("default", true), getRef());
            RemotePrimaryShardFound found1 = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
            String path1 = found1.getPrimaryPath();
            assertTrue("Unexpected primary path " + path1, path1.contains("member-2-shard-default-config"));
            shardManager1.tell(MockClusterWrapper.createMemberUp("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            MessageCollectorActor.expectFirstMatching(mockShardActor1, PeerUp.class);
            // Test FindPrimary wait succeeds after reachable member event.
            shardManager1.tell(MockClusterWrapper.createUnreachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            shardManager1.underlyingActor().waitForUnreachableMember();
            shardManager1.tell(new FindPrimary("default", true), getRef());
            shardManager1.tell(MockClusterWrapper.createReachableMember("member-2", "akka://cluster-test@127.0.0.1:2558"), getRef());
            RemotePrimaryShardFound found2 = expectMsgClass(duration("5 seconds"), RemotePrimaryShardFound.class);
            String path2 = found2.getPrimaryPath();
            assertTrue("Unexpected primary path " + path2, path2.contains("member-2-shard-default-config"));
        }
    };
    LOG.info("testShardAvailabilityOnChangeOfMemberReachability ending");
}
Also used : ActorSystem(akka.actor.ActorSystem) ClusterWrapperImpl(org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl) UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ShardLeaderStateChanged(org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) AddressFromURIString(akka.actor.AddressFromURIString) TestKit(akka.testkit.javadsl.TestKit) RemotePrimaryShardFound(org.opendaylight.controller.cluster.datastore.messages.RemotePrimaryShardFound) FindPrimary(org.opendaylight.controller.cluster.datastore.messages.FindPrimary) PeerDown(org.opendaylight.controller.cluster.datastore.messages.PeerDown) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) PeerUp(org.opendaylight.controller.cluster.datastore.messages.PeerUp) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 17 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration in project controller by opendaylight.

the class ShardManagerTest method testWhenMultipleShardsPresentSyncStatusMustBeTrueForAllShards.

@Test
public void testWhenMultipleShardsPresentSyncStatusMustBeTrueForAllShards() throws Exception {
    LOG.info("testWhenMultipleShardsPresentSyncStatusMustBeTrueForAllShards starting");
    TestShardManager shardManager = newTestShardManager(newShardMgrProps(new MockConfiguration() {

        @Override
        public List<String> getMemberShardNames(final MemberName memberName) {
            return Arrays.asList("default", "astronauts");
        }
    }));
    // Initially will be false
    assertEquals(false, shardManager.getMBean().getSyncStatus());
    // Make default shard leader
    String defaultShardId = "member-1-shard-default-" + shardMrgIDSuffix;
    shardManager.onReceiveCommand(new RoleChangeNotification(defaultShardId, RaftState.Follower.name(), RaftState.Leader.name()));
    // default = Leader, astronauts is unknown so sync status remains false
    assertEquals(false, shardManager.getMBean().getSyncStatus());
    // Make astronauts shard leader as well
    String astronautsShardId = "member-1-shard-astronauts-" + shardMrgIDSuffix;
    shardManager.onReceiveCommand(new RoleChangeNotification(astronautsShardId, RaftState.Follower.name(), RaftState.Leader.name()));
    // Now sync status should be true
    assertEquals(true, shardManager.getMBean().getSyncStatus());
    // Make astronauts a Follower
    shardManager.onReceiveCommand(new RoleChangeNotification(astronautsShardId, RaftState.Leader.name(), RaftState.Follower.name()));
    // Sync status is not true
    assertEquals(false, shardManager.getMBean().getSyncStatus());
    // Make the astronauts follower sync status true
    shardManager.onReceiveCommand(new FollowerInitialSyncUpStatus(true, astronautsShardId));
    // Sync status is now true
    assertEquals(true, shardManager.getMBean().getSyncStatus());
    LOG.info("testWhenMultipleShardsPresentSyncStatusMustBeTrueForAllShards ending");
}
Also used : RoleChangeNotification(org.opendaylight.controller.cluster.notifications.RoleChangeNotification) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) MemberName(org.opendaylight.controller.cluster.access.concepts.MemberName) AddressFromURIString(akka.actor.AddressFromURIString) FollowerInitialSyncUpStatus(org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 18 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration in project controller by opendaylight.

the class ShardManagerTest method testShutDown.

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

        {
            MockConfiguration mockConfig = new MockConfiguration(ImmutableMap.<String, List<String>>builder().put("shard1", Arrays.asList("member-1")).put("shard2", Arrays.asList("member-1")).build());
            String shardId1 = ShardIdentifier.create("shard1", MEMBER_1, shardMrgIDSuffix).toString();
            ActorRef shard1 = actorFactory.createActor(MessageCollectorActor.props(), shardId1);
            String shardId2 = ShardIdentifier.create("shard2", MEMBER_1, shardMrgIDSuffix).toString();
            ActorRef shard2 = actorFactory.createActor(MessageCollectorActor.props(), shardId2);
            ActorRef shardManager = actorFactory.createActor(newTestShardMgrBuilder(mockConfig).addShardActor("shard1", shard1).addShardActor("shard2", shard2).props());
            shardManager.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
            shardManager.tell(new ActorInitialized(), shard1);
            shardManager.tell(new ActorInitialized(), shard2);
            FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
            Future<Boolean> stopFuture = Patterns.gracefulStop(shardManager, duration, Shutdown.INSTANCE);
            MessageCollectorActor.expectFirstMatching(shard1, Shutdown.class);
            MessageCollectorActor.expectFirstMatching(shard2, Shutdown.class);
            try {
                Await.ready(stopFuture, FiniteDuration.create(500, TimeUnit.MILLISECONDS));
                fail("ShardManager actor stopped without waiting for the Shards to be stopped");
            } catch (TimeoutException e) {
            // expected
            }
            actorFactory.killActor(shard1, this);
            actorFactory.killActor(shard2, this);
            Boolean stopped = Await.result(stopFuture, duration);
            assertEquals("Stopped", Boolean.TRUE, stopped);
        }
    };
    LOG.info("testShutDown ending");
}
Also used : UpdateSchemaContext(org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) FiniteDuration(scala.concurrent.duration.FiniteDuration) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) List(java.util.List) ActorInitialized(org.opendaylight.controller.cluster.datastore.messages.ActorInitialized) TestKit(akka.testkit.javadsl.TestKit) AddressFromURIString(akka.actor.AddressFromURIString) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) AbstractShardManagerTest(org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)

Example 19 with MockConfiguration

use of org.opendaylight.controller.cluster.datastore.utils.MockConfiguration in project controller by opendaylight.

the class DataChangeListenerProxyTest method testOnDataChanged.

@Test
public void testOnDataChanged() throws Exception {
    final ActorRef actorRef = getSystem().actorOf(MessageCollectorActor.props());
    DataChangeListenerProxy dataChangeListenerProxy = new DataChangeListenerProxy(getSystem().actorSelection(actorRef.path()));
    dataChangeListenerProxy.onDataChanged(new MockDataChangedEvent());
    // Check if it was received by the remote actor
    ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
    Object messages = testContext.executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
    Assert.assertNotNull(messages);
    Assert.assertTrue(messages instanceof List);
    List<?> listMessages = (List<?>) messages;
    Assert.assertEquals(1, listMessages.size());
    Assert.assertTrue(listMessages.get(0).getClass().equals(DataChanged.class));
}
Also used : DataChanged(org.opendaylight.controller.cluster.datastore.messages.DataChanged) ActorRef(akka.actor.ActorRef) MockClusterWrapper(org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper) MockConfiguration(org.opendaylight.controller.cluster.datastore.utils.MockConfiguration) List(java.util.List) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) Test(org.junit.Test)

Aggregations

MockConfiguration (org.opendaylight.controller.cluster.datastore.utils.MockConfiguration)19 AddressFromURIString (akka.actor.AddressFromURIString)17 Test (org.junit.Test)17 TestKit (akka.testkit.javadsl.TestKit)16 AbstractShardManagerTest (org.opendaylight.controller.cluster.datastore.AbstractShardManagerTest)16 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)14 ActorRef (akka.actor.ActorRef)12 TestActorRef (akka.testkit.TestActorRef)11 ActorInitialized (org.opendaylight.controller.cluster.datastore.messages.ActorInitialized)8 RoleChangeNotification (org.opendaylight.controller.cluster.notifications.RoleChangeNotification)7 ActorSystem (akka.actor.ActorSystem)6 ClusterWrapperImpl (org.opendaylight.controller.cluster.datastore.ClusterWrapperImpl)6 ShardLeaderStateChanged (org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged)6 List (java.util.List)5 FollowerInitialSyncUpStatus (org.opendaylight.controller.cluster.raft.base.messages.FollowerInitialSyncUpStatus)5 Status (akka.actor.Status)4 AddShardReplica (org.opendaylight.controller.cluster.datastore.messages.AddShardReplica)4 ChangeShardMembersVotingStatus (org.opendaylight.controller.cluster.datastore.messages.ChangeShardMembersVotingStatus)4 FindPrimary (org.opendaylight.controller.cluster.datastore.messages.FindPrimary)4 ShardManagerSnapshot (org.opendaylight.controller.cluster.datastore.persisted.ShardManagerSnapshot)4