Refactor PruningDataTreeModification instantiation 41/87041/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 19 Jan 2020 11:39:58 +0000 (12:39 +0100)
committerRobert Varga <nite@hq.sk>
Sun, 19 Jan 2020 14:35:02 +0000 (14:35 +0000)
Hiding the pruner works against what we want to do, as we need
to be able to control how pruning works. While we are at it, hide
delegate() so that users are forced to hold on to this invariant.

JIRA: CONTROLLER-1923
Change-Id: Iba78b2fd5f775281bf4bc04f449539d185c94f9c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/PruningDataTreeModification.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/PruningDataTreeModificationTest.java

index 43a3f6dc49cb201ea34c167a6d57c5794740b6f8..016b078d5c002f1154aa6908920f4f27c65a2c52 100644 (file)
@@ -51,6 +51,7 @@ import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier
 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActorRegistry.CohortRegistryCommand;
 import org.opendaylight.controller.cluster.datastore.ShardDataTreeCohort.State;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActorRegistry.CohortRegistryCommand;
 import org.opendaylight.controller.cluster.datastore.ShardDataTreeCohort.State;
 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
+import org.opendaylight.controller.cluster.datastore.node.utils.transformer.ReusableNormalizedNodePruner;
 import org.opendaylight.controller.cluster.datastore.persisted.AbortTransactionPayload;
 import org.opendaylight.controller.cluster.datastore.persisted.AbstractIdentifiablePayload;
 import org.opendaylight.controller.cluster.datastore.persisted.CloseLocalHistoryPayload;
 import org.opendaylight.controller.cluster.datastore.persisted.AbortTransactionPayload;
 import org.opendaylight.controller.cluster.datastore.persisted.AbstractIdentifiablePayload;
 import org.opendaylight.controller.cluster.datastore.persisted.CloseLocalHistoryPayload;
@@ -265,7 +266,8 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
             }
         }
 
             }
         }
 
-        final DataTreeModification mod = wrapper.apply(dataTree.takeSnapshot().newModification());
+        final DataTreeModification unwrapped = dataTree.takeSnapshot().newModification();
+        final DataTreeModification mod = wrapper.apply(unwrapped);
         // delete everything first
         mod.delete(YangInstanceIdentifier.empty());
 
         // delete everything first
         mod.delete(YangInstanceIdentifier.empty());
 
@@ -276,7 +278,6 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         }
         mod.ready();
 
         }
         mod.ready();
 
-        final DataTreeModification unwrapped = unwrap(mod);
         dataTree.validate(unwrapped);
         DataTreeCandidateTip candidate = dataTree.prepare(unwrapped);
         dataTree.commit(candidate);
         dataTree.validate(unwrapped);
         DataTreeCandidateTip candidate = dataTree.prepare(unwrapped);
         dataTree.commit(candidate);
@@ -297,14 +298,9 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     }
 
     private PruningDataTreeModification wrapWithPruning(final DataTreeModification delegate) {
     }
 
     private PruningDataTreeModification wrapWithPruning(final DataTreeModification delegate) {
-        return new PruningDataTreeModification(delegate, dataTree, dataSchemaContext);
-    }
-
-    private static DataTreeModification unwrap(final DataTreeModification modification) {
-        if (modification instanceof PruningDataTreeModification) {
-            return ((PruningDataTreeModification)modification).delegate();
-        }
-        return modification;
+        return new PruningDataTreeModification(delegate, dataTree,
+            // TODO: we should be able to reuse the pruner, provided we are not reentrant
+            ReusableNormalizedNodePruner.forDataSchemaContext(dataSchemaContext));
     }
 
     /**
     }
 
     /**
@@ -321,12 +317,11 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     @SuppressWarnings("checkstyle:IllegalCatch")
     private void applyRecoveryCandidate(final CommitTransactionPayload payload) throws IOException {
         final Entry<TransactionIdentifier, DataTreeCandidate> entry = payload.getCandidate();
     @SuppressWarnings("checkstyle:IllegalCatch")
     private void applyRecoveryCandidate(final CommitTransactionPayload payload) throws IOException {
         final Entry<TransactionIdentifier, DataTreeCandidate> entry = payload.getCandidate();
-
-        final PruningDataTreeModification mod = wrapWithPruning(dataTree.takeSnapshot().newModification());
+        final DataTreeModification unwrapped = dataTree.takeSnapshot().newModification();
+        final PruningDataTreeModification mod = wrapWithPruning(unwrapped);
         DataTreeCandidates.applyToModification(mod, entry.getValue());
         mod.ready();
 
         DataTreeCandidates.applyToModification(mod, entry.getValue());
         mod.ready();
 
-        final DataTreeModification unwrapped = mod.delegate();
         LOG.trace("{}: Applying recovery modification {}", logContext, unwrapped);
 
         try {
         LOG.trace("{}: Applying recovery modification {}", logContext, unwrapped);
 
         try {
index f69a22e5b6e96fdb2c5f1a7d474280353b99a022..f62f2c8d114f8853f8a1f4f6bfcc0acfd5544dce 100644 (file)
@@ -23,7 +23,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
-import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,20 +39,15 @@ public class PruningDataTreeModification extends ForwardingObject implements Dat
 
     private DataTreeModification delegate;
 
 
     private DataTreeModification delegate;
 
-    private PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
+    public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
             final ReusableNormalizedNodePruner pruner) {
         this.delegate = requireNonNull(delegate);
         this.dataTree = requireNonNull(dataTree);
         this.pruner = requireNonNull(pruner);
     }
 
             final ReusableNormalizedNodePruner pruner) {
         this.delegate = requireNonNull(delegate);
         this.dataTree = requireNonNull(dataTree);
         this.pruner = requireNonNull(pruner);
     }
 
-    public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
-            final DataSchemaContextTree dataSchemaContext) {
-        this(delegate, dataTree, ReusableNormalizedNodePruner.forDataSchemaContext(dataSchemaContext));
-    }
-
     @Override
     @Override
-    public DataTreeModification delegate() {
+    protected DataTreeModification delegate() {
         return delegate;
     }
 
         return delegate;
     }
 
index 50179c706b1ffda9587990c95b5f5b8650e2a431..9118d8f66f3a050abe5ab9d1b284399e189aecea 100644 (file)
@@ -34,6 +34,7 @@ import org.mockito.Mockito;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.controller.cluster.datastore.Shard;
 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.opendaylight.controller.cluster.datastore.Shard;
 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
+import org.opendaylight.controller.cluster.datastore.node.utils.transformer.ReusableNormalizedNodePruner;
 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.cluster.datastore.model.CarsModel;
 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
@@ -97,7 +98,9 @@ public class PruningDataTreeModificationTest {
             }
         });
 
             }
         });
 
-        pruningDataTreeModification = new PruningDataTreeModification(proxyModification, dataTree, CONTEXT_TREE);
+        pruningDataTreeModification = new PruningDataTreeModification(proxyModification, dataTree,
+            // Cannot reuse with parallel tests
+            ReusableNormalizedNodePruner.forDataSchemaContext(CONTEXT_TREE));
     }
 
     @Test
     }
 
     @Test