From: Tom Pantelis Date: Thu, 1 Sep 2016 16:14:07 +0000 (-0400) Subject: Notify listeners on applySnapshot X-Git-Tag: release/carbon~494 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=4f1f2ae598588f6aa5aac59b2206d97ad402a193 Notify listeners on applySnapshot When a snapshot is applied from the leader, we weren't notifying data change listeners. We should. Change-Id: If721c2ce7e6f27aa01f7babc0a0ad3c4468840c1 Signed-off-by: Tom Pantelis --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java index a990b50a32..bf3b200825 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java @@ -208,7 +208,10 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { final DataTreeModification unwrapped = unwrap(mod); dataTree.validate(unwrapped); - dataTree.commit(dataTree.prepare(unwrapped)); + DataTreeCandidateTip candidate = dataTree.prepare(unwrapped); + dataTree.commit(candidate); + notifyListeners(candidate); + LOG.debug("{}: state snapshot applied in %s", logContext, elapsed); } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java index 5d27224398..9ae74bb47c 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/ShardDataTreeTest.java @@ -10,28 +10,42 @@ package org.opendaylight.controller.cluster.datastore; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.reset; import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCanCommit; import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediateCommit; import static org.opendaylight.controller.cluster.datastore.ShardDataTreeMocking.immediatePreCommit; import com.google.common.base.Optional; import com.google.common.base.Ticker; +import com.google.common.collect.Maps; import java.math.BigInteger; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutionException; +import java.util.function.Consumer; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats; import org.opendaylight.controller.md.cluster.datastore.model.CarsModel; import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel; import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper; +import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; +import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -136,6 +150,61 @@ public class ShardDataTreeTest extends AbstractTest { assertEquals(expected, actual); } + @Test + public void testListenerNotifiedOnApplySnapshot() throws Exception { + final ShardDataTree shardDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL); + + DOMDataTreeChangeListener listener = mock(DOMDataTreeChangeListener.class); + shardDataTree.registerTreeChangeListener(CarsModel.CAR_LIST_PATH.node(CarsModel.CAR_QNAME), listener); + + addCar(shardDataTree, "optima"); + + verifyOnDataTreeChanged(listener, dtc -> { + assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType()); + assertEquals("getRootPath", CarsModel.newCarPath("optima"), dtc.getRootPath()); + }); + + addCar(shardDataTree, "sportage"); + + verifyOnDataTreeChanged(listener, dtc -> { + assertEquals("getModificationType", ModificationType.WRITE, dtc.getRootNode().getModificationType()); + assertEquals("getRootPath", CarsModel.newCarPath("sportage"), dtc.getRootPath()); + }); + + ShardDataTree newDataTree = new ShardDataTree(mockShard, fullSchema, TreeType.OPERATIONAL); + addCar(newDataTree, "optima"); + addCar(newDataTree, "murano"); + + shardDataTree.applySnapshot(newDataTree.takeStateSnapshot()); + + Map expChanges = Maps.newHashMap(); + expChanges.put(CarsModel.newCarPath("optima"), ModificationType.WRITE); + expChanges.put(CarsModel.newCarPath("murano"), ModificationType.WRITE); + expChanges.put(CarsModel.newCarPath("sportage"), ModificationType.DELETE); + verifyOnDataTreeChanged(listener, dtc -> { + ModificationType expType = expChanges.remove(dtc.getRootPath()); + assertNotNull("Got unexpected change for " + dtc.getRootPath(), expType); + assertEquals("getModificationType", expType, dtc.getRootNode().getModificationType()); + }); + + if(!expChanges.isEmpty()) { + fail("Missing change notifications: " + expChanges); + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private static void verifyOnDataTreeChanged(DOMDataTreeChangeListener listener, Consumer callback) { + ArgumentCaptor changes = ArgumentCaptor.forClass(Collection.class); + verify(listener, atLeastOnce()).onDataTreeChanged(changes.capture()); + for(Collection list: changes.getAllValues()) { + for(Object dtc: list) { + callback.accept((DataTreeCandidate)dtc); + } + } + + reset(listener); + } + private static NormalizedNode getCars(final ShardDataTree shardDataTree) { final ReadOnlyShardDataTreeTransaction readOnlyShardDataTreeTransaction = shardDataTree.newReadOnlyTransaction(nextTransactionId()); final DataTreeSnapshot snapshot1 = readOnlyShardDataTreeTransaction.getSnapshot(); @@ -148,10 +217,14 @@ public class ShardDataTreeTest extends AbstractTest { } private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree) throws ExecutionException, InterruptedException { + return addCar(shardDataTree, "altima"); + } + + private static DataTreeCandidateTip addCar(final ShardDataTree shardDataTree, String name) throws ExecutionException, InterruptedException { return doTransaction(shardDataTree, snapshot -> { snapshot.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer()); snapshot.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()); - snapshot.write(CarsModel.newCarPath("altima"), CarsModel.newCarEntry("altima", new BigInteger("100"))); + snapshot.write(CarsModel.newCarPath(name), CarsModel.newCarEntry(name, new BigInteger("100"))); }); } @@ -196,4 +269,4 @@ public class ShardDataTreeTest extends AbstractTest { return candidate; } -} \ No newline at end of file +}