Search in sources :

Example 6 with MockDataChangeListener

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

the class ShardTest method testChangeListenerNotifiedWhenNotTheLeaderOnRegistration.

@SuppressWarnings("serial")
@Test
public void testChangeListenerNotifiedWhenNotTheLeaderOnRegistration() throws Exception {
    // This test tests the timing window in which a change listener is registered before the
    // shard becomes the leader. We verify that the listener is registered and notified of the
    // existing data when the shard becomes the leader.
    // For this test, we want to send the RegisterChangeListener message after the shard
    // has recovered from persistence and before it becomes the leader. So we subclass
    // Shard to override onReceiveCommand and, when the first ElectionTimeout is received,
    // we know that the shard has been initialized to a follower and has started the
    // election process. The following 2 CountDownLatches are used to coordinate the
    // ElectionTimeout with the sending of the RegisterChangeListener message.
    final CountDownLatch onFirstElectionTimeout = new CountDownLatch(1);
    final CountDownLatch onChangeListenerRegistered = new CountDownLatch(1);
    final Creator<Shard> creator = new Creator<Shard>() {

        boolean firstElectionTimeout = true;

        @Override
        public Shard create() throws Exception {
            // it does do a persist)
            return new Shard(newShardBuilder()) {

                @Override
                public void handleCommand(final Object message) {
                    if (message instanceof ElectionTimeout && firstElectionTimeout) {
                        // Got the first ElectionTimeout. We don't forward it to the
                        // base Shard yet until we've sent the RegisterChangeListener
                        // message. So we signal the onFirstElectionTimeout latch to tell
                        // the main thread to send the RegisterChangeListener message and
                        // start a thread to wait on the onChangeListenerRegistered latch,
                        // which the main thread signals after it has sent the message.
                        // After the onChangeListenerRegistered is triggered, we send the
                        // original ElectionTimeout message to proceed with the election.
                        firstElectionTimeout = false;
                        final ActorRef self = getSelf();
                        new Thread(() -> {
                            Uninterruptibles.awaitUninterruptibly(onChangeListenerRegistered, 5, TimeUnit.SECONDS);
                            self.tell(message, self);
                        }).start();
                        onFirstElectionTimeout.countDown();
                    } else {
                        super.handleCommand(message);
                    }
                }
            };
        }
    };
    setupInMemorySnapshotStore();
    final YangInstanceIdentifier path = TestModel.TEST_PATH;
    final MockDataChangeListener listener = new MockDataChangeListener(1);
    final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, path), "testRegisterChangeListenerWhenNotLeaderInitially-DataChangeListener");
    final TestActorRef<Shard> shard = actorFactory.createTestActor(Props.create(new DelegatingShardCreator(creator)).withDispatcher(Dispatchers.DefaultDispatcherId()), "testRegisterChangeListenerWhenNotLeaderInitially");
    new ShardTestKit(getSystem()) {

        {
            // Wait until the shard receives the first ElectionTimeout
            // message.
            assertEquals("Got first ElectionTimeout", true, onFirstElectionTimeout.await(5, TimeUnit.SECONDS));
            // Now send the RegisterChangeListener and wait for the reply.
            shard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.SUBTREE, false), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
            // Sanity check - verify the shard is not the leader yet.
            shard.tell(FindLeader.INSTANCE, getRef());
            final FindLeaderReply findLeadeReply = expectMsgClass(duration("5 seconds"), FindLeaderReply.class);
            assertFalse("Expected the shard not to be the leader", findLeadeReply.getLeaderActor().isPresent());
            // Signal the onChangeListenerRegistered latch to tell the
            // thread above to proceed
            // with the election process.
            onChangeListenerRegistered.countDown();
            // Wait for the shard to become the leader and notify our
            // listener with the existing
            // data in the store.
            listener.waitForChangeEvents(path);
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) ElectionTimeout(org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) Creator(akka.japi.Creator) CountDownLatch(java.util.concurrent.CountDownLatch) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) FindLeaderReply(org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) Test(org.junit.Test)

Example 7 with MockDataChangeListener

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

the class ShardTest method testClusteredDataChangeListenerRegistration.

@Test
public void testClusteredDataChangeListenerRegistration() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final String testName = "testClusteredDataChangeListenerRegistration";
            final ShardIdentifier followerShardID = ShardIdentifier.create("inventory", MemberName.forName(actorFactory.generateActorId(testName + "-follower")), "config");
            final ShardIdentifier leaderShardID = ShardIdentifier.create("inventory", MemberName.forName(actorFactory.generateActorId(testName + "-leader")), "config");
            final TestActorRef<Shard> followerShard = actorFactory.createTestActor(Shard.builder().id(followerShardID).datastoreContext(dataStoreContextBuilder.shardElectionTimeoutFactor(1000).build()).peerAddresses(Collections.singletonMap(leaderShardID.toString(), "akka://test/user/" + leaderShardID.toString())).schemaContextProvider(() -> SCHEMA_CONTEXT).props().withDispatcher(Dispatchers.DefaultDispatcherId()), followerShardID.toString());
            final TestActorRef<Shard> leaderShard = actorFactory.createTestActor(Shard.builder().id(leaderShardID).datastoreContext(newDatastoreContext()).peerAddresses(Collections.singletonMap(followerShardID.toString(), "akka://test/user/" + followerShardID.toString())).schemaContextProvider(() -> SCHEMA_CONTEXT).props().withDispatcher(Dispatchers.DefaultDispatcherId()), leaderShardID.toString());
            leaderShard.tell(TimeoutNow.INSTANCE, ActorRef.noSender());
            final String leaderPath = waitUntilLeader(followerShard);
            assertEquals("Shard leader path", leaderShard.path().toString(), leaderPath);
            final YangInstanceIdentifier path = TestModel.TEST_PATH;
            final MockDataChangeListener listener = new MockDataChangeListener(1);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, path), actorFactory.generateActorId(testName + "-DataChangeListener"));
            followerShard.tell(new RegisterChangeListener(path, dclActor, AsyncDataBroker.DataChangeScope.BASE, true), getRef());
            final RegisterDataTreeNotificationListenerReply reply = expectMsgClass(duration("5 seconds"), RegisterDataTreeNotificationListenerReply.class);
            assertNotNull("getListenerRegistrationPath", reply.getListenerRegistrationPath());
            writeToStore(followerShard, path, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
            listener.waitForChangeEvents();
        }
    };
}
Also used : RegisterDataTreeNotificationListenerReply(org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply) ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) ShardIdentifier(org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 8 with MockDataChangeListener

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

the class DistributedDataStoreIntegrationTest method testChangeListenerRegistration.

@Test
public void testChangeListenerRegistration() throws Exception {
    new IntegrationTestKit(getSystem(), datastoreContextBuilder) {

        {
            try (AbstractDataStore dataStore = setupAbstractDataStore(testParameter, "testChangeListenerRegistration", "test-1")) {
                testWriteTransaction(dataStore, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
                final MockDataChangeListener listener = new MockDataChangeListener(1);
                final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore.registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
                assertNotNull("registerChangeListener returned null", listenerReg);
                IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("getDataChangeListenerActors", 1, state.getDataChangeListenerActors().size()));
                // Wait for the initial notification
                listener.waitForChangeEvents(TestModel.TEST_PATH);
                listener.reset(2);
                // Write 2 updates.
                testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
                YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
                testWriteTransaction(dataStore, listPath, ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
                // Wait for the 2 updates.
                listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
                listenerReg.close();
                IntegrationTestKit.verifyShardState(dataStore, "test-1", state -> assertEquals("getDataChangeListenerActors", 0, state.getDataChangeListenerActors().size()));
                testWriteTransaction(dataStore, YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(), ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
                listener.expectNoMoreChanges("Received unexpected change after close");
            }
        }
    };
}
Also used : MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 9 with MockDataChangeListener

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

the class DataChangeListenerSupportTest method testInitialChangeListenerEventWithNestedWildcardedListsPath.

@Test
public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testInitialChangeListenerEventWithNestedWildcardedListsPath");
            waitUntilLeader(actor);
            final Shard shard = actor.underlyingActor();
            mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
            final MockDataChangeListener listener = new MockDataChangeListener(1);
            final YangInstanceIdentifier path = OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, path), "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener");
            final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
            support.onMessage(new RegisterChangeListener(path, dclActor, DataChangeScope.ONE, false), true, true);
            listener.waitForChangeEvents();
            listener.verifyCreatedData(0, innerEntryPath(1, "one"));
            listener.verifyCreatedData(0, innerEntryPath(1, "two"));
            listener.verifyCreatedData(0, innerEntryPath(2, "three"));
            listener.verifyCreatedData(0, innerEntryPath(2, "four"));
            // Register for a specific outer list entry
            final MockDataChangeListener listener2 = new MockDataChangeListener(1);
            final YangInstanceIdentifier path2 = OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME);
            final ActorRef dclActor2 = actorFactory.createActor(DataChangeListener.props(listener2, path2), "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener2");
            final DataChangeListenerSupport support2 = new DataChangeListenerSupport(shard);
            support2.onMessage(new RegisterChangeListener(path2, dclActor2, DataChangeScope.ONE, false), true, true);
            listener2.waitForChangeEvents();
            listener2.verifyCreatedData(0, innerEntryPath(1, "one"));
            listener2.verifyCreatedData(0, innerEntryPath(1, "two"));
            listener2.verifyNoCreatedData(0, innerEntryPath(2, "three"));
            listener2.verifyNoCreatedData(0, innerEntryPath(2, "four"));
        }
    };
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 10 with MockDataChangeListener

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

the class DataChangeListenerSupportTest method testInitialChangeListenerEventWithListPath.

@Test
public void testInitialChangeListenerEventWithListPath() throws Exception {
    new ShardTestKit(getSystem()) {

        {
            final TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()), "testInitialChangeListenerEventWithListPath");
            waitUntilLeader(actor);
            final Shard shard = actor.underlyingActor();
            mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
            final MockDataChangeListener listener = new MockDataChangeListener(1);
            final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener, OUTER_LIST_PATH), "testInitialChangeListenerEventWithListPath-DataChangeListener");
            final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
            support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH, dclActor, DataChangeScope.ONE, false), true, true);
            listener.waitForChangeEvents();
            assertEquals("Outer entry 1 present", true, NormalizedNodes.findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(1)).isPresent());
            assertEquals("Outer entry 2 present", true, NormalizedNodes.findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(2)).isPresent());
        }
    };
}
Also used : ActorRef(akka.actor.ActorRef) TestActorRef(akka.testkit.TestActorRef) MockDataChangeListener(org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener) RegisterChangeListener(org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 MockDataChangeListener (org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener)11 ActorRef (akka.actor.ActorRef)10 TestActorRef (akka.testkit.TestActorRef)10 RegisterChangeListener (org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener)10 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)8 RegisterDataTreeNotificationListenerReply (org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeNotificationListenerReply)4 Creator (akka.japi.Creator)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ShardIdentifier (org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier)1 UpdateSchemaContext (org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext)1 ElectionTimeout (org.opendaylight.controller.cluster.raft.base.messages.ElectionTimeout)1 FindLeaderReply (org.opendaylight.controller.cluster.raft.client.messages.FindLeaderReply)1 DisableElectionsRaftPolicy (org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy)1