X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardDataTree.java;h=edf05f2a25bb9fe5bb108b18dd5fcd10709266e8;hp=be2e6d47fbc776ee58efef233b3a4c73c8e50865;hb=17211c982bcee83306af49b1b75c764dcb504b5d;hpb=43aab07cdbc80eda69e84a26085afe1b37f4002e;ds=sidebyside 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 be2e6d47fb..edf05f2a25 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 @@ -9,18 +9,18 @@ package org.opendaylight.controller.cluster.datastore; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import java.util.AbstractMap.SimpleEntry; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.annotation.concurrent.NotThreadSafe; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener; import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener; import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration; -import org.opendaylight.controller.md.sal.dom.store.impl.ResolveDataChangeEventsTask; -import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree; +import org.opendaylight.yangtools.concepts.Identifier; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -47,25 +47,28 @@ import org.slf4j.LoggerFactory; @NotThreadSafe public class ShardDataTree extends ShardDataTreeTransactionParent { private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class); - private static final YangInstanceIdentifier ROOT_PATH = YangInstanceIdentifier.builder().build(); - private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager(); - private final Map transactionChains = new HashMap<>(); - private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher(); - private final ListenerTree listenerTree = ListenerTree.create(); + + private final Map transactionChains = new HashMap<>(); + private final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher; + private final ShardDataChangeListenerPublisher dataChangeListenerPublisher; private final TipProducingDataTree dataTree; + private final String logContext; private SchemaContext schemaContext; - public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) { + public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType, + final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher, + final ShardDataChangeListenerPublisher dataChangeListenerPublisher, final String logContext) { dataTree = InMemoryDataTreeFactory.getInstance().create(treeType); updateSchemaContext(schemaContext); + + this.treeChangeListenerPublisher = Preconditions.checkNotNull(treeChangeListenerPublisher); + this.dataChangeListenerPublisher = Preconditions.checkNotNull(dataChangeListenerPublisher); + this.logContext = Preconditions.checkNotNull(logContext); } - /** - * @deprecated Use {@link #ShardDataTree(SchemaContext, TreeType)} instead. - */ - @Deprecated - public ShardDataTree(final SchemaContext schemaContext) { - this(schemaContext, TreeType.OPERATIONAL); + public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) { + this(schemaContext, treeType, new DefaultShardDataTreeChangeListenerPublisher(), + new DefaultShardDataChangeListenerPublisher(), ""); } public TipProducingDataTree getDataTree() { @@ -77,66 +80,58 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { } void updateSchemaContext(final SchemaContext schemaContext) { - Preconditions.checkNotNull(schemaContext); - this.schemaContext = schemaContext; dataTree.setSchemaContext(schemaContext); + this.schemaContext = Preconditions.checkNotNull(schemaContext); } - private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) { - ShardDataTreeTransactionChain chain = transactionChains.get(chainId); + private ShardDataTreeTransactionChain ensureTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier) { + ShardDataTreeTransactionChain chain = transactionChains.get(localHistoryIdentifier); if (chain == null) { - chain = new ShardDataTreeTransactionChain(chainId, this); - transactionChains.put(chainId, chain); + chain = new ShardDataTreeTransactionChain(localHistoryIdentifier, this); + transactionChains.put(localHistoryIdentifier, chain); } return chain; } - ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) { - if (Strings.isNullOrEmpty(chainId)) { + ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) { + if (txId.getHistoryId().getHistoryId() == 0) { return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot()); } - return ensureTransactionChain(chainId).newReadOnlyTransaction(txId); + return ensureTransactionChain(txId.getHistoryId()).newReadOnlyTransaction(txId); } - ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) { - if (Strings.isNullOrEmpty(chainId)) { + ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) { + if (txId.getHistoryId().getHistoryId() == 0) { return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot() .newModification()); } - return ensureTransactionChain(chainId).newReadWriteTransaction(txId); + return ensureTransactionChain(txId.getHistoryId()).newReadWriteTransaction(txId); } public void notifyListeners(final DataTreeCandidate candidate) { - LOG.debug("Notifying listeners on candidate {}", candidate); - - // DataTreeChanges first, as they are more light-weight - treeChangePublisher.publishChanges(candidate); - - // DataChanges second, as they are heavier - ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER); + treeChangeListenerPublisher.publishChanges(candidate, logContext); + dataChangeListenerPublisher.publishChanges(candidate, logContext); } - void notifyOfInitialData(DataChangeListenerRegistration>> listenerReg, Optional currentState) { - - if(currentState.isPresent()) { - ListenerTree localListenerTree = ListenerTree.create(); - localListenerTree.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(), + void notifyOfInitialData(final DataChangeListenerRegistration>> listenerReg, final Optional currentState) { + if (currentState.isPresent()) { + ShardDataChangeListenerPublisher localPublisher = dataChangeListenerPublisher.newInstance(); + localPublisher.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(), listenerReg.getScope()); - - ResolveDataChangeEventsTask.create(currentState.get(), localListenerTree).resolve(MANAGER); + localPublisher.publishChanges(currentState.get(), logContext); } } void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener, final Optional currentState) { - if(currentState.isPresent()) { - ShardDataTreeChangePublisher localTreeChangePublisher = new ShardDataTreeChangePublisher(); - localTreeChangePublisher.registerTreeChangeListener(path, listener); - localTreeChangePublisher.publishChanges(currentState.get()); + if (currentState.isPresent()) { + ShardDataTreeChangeListenerPublisher localPublisher = treeChangeListenerPublisher.newInstance(); + localPublisher.registerTreeChangeListener(path, listener); + localPublisher.publishChanges(currentState.get(), logContext); } } @@ -148,12 +143,12 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { transactionChains.clear(); } - void closeTransactionChain(final String transactionChainId) { + void closeTransactionChain(final LocalHistoryIdentifier transactionChainId) { final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId); if (chain != null) { chain.close(); } else { - LOG.debug("Closing non-existent transaction chain {}", transactionChainId); + LOG.debug("{}: Closing non-existent transaction chain {}", logContext, transactionChainId); } } @@ -162,33 +157,33 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { final AsyncDataChangeListener> listener, final DataChangeScope scope) { final DataChangeListenerRegistration>> reg = - listenerTree.registerDataChangeListener(path, listener, scope); + dataChangeListenerPublisher.registerDataChangeListener(path, listener, scope); return new SimpleEntry<>(reg, readCurrentData()); } private Optional readCurrentData() { - final Optional> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH); + final Optional> currentState = dataTree.takeSnapshot().readNode(YangInstanceIdentifier.EMPTY); return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode( - ROOT_PATH, currentState.get())) : Optional.absent(); + YangInstanceIdentifier.EMPTY, currentState.get())) : Optional.absent(); } public Entry, Optional> registerTreeChangeListener( final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) { - final ListenerRegistration reg = treeChangePublisher.registerTreeChangeListener( + final ListenerRegistration reg = treeChangeListenerPublisher.registerTreeChangeListener( path, listener); return new SimpleEntry<>(reg, readCurrentData()); } - void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException { - LOG.debug("Applying foreign transaction {}", identifier); + void applyForeignCandidate(final Identifier identifier, final DataTreeCandidate foreign) throws DataValidationFailedException { + LOG.debug("{}: Applying foreign transaction {}", logContext, identifier); final DataTreeModification mod = dataTree.takeSnapshot().newModification(); DataTreeCandidates.applyToModification(mod, foreign); mod.ready(); - LOG.trace("Applying foreign modification {}", mod); + LOG.trace("{}: Applying foreign modification {}", logContext, mod); dataTree.validate(mod); final DataTreeCandidate candidate = dataTree.prepare(mod); dataTree.commit(candidate); @@ -207,7 +202,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId()); } - public Optional> readNode(YangInstanceIdentifier path) { + public Optional> readNode(final YangInstanceIdentifier path) { return dataTree.takeSnapshot().readNode(path); } @@ -219,7 +214,8 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { return dataTree.takeSnapshot().newModification(); } - public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException { + // FIXME: This should be removed, it violates encapsulation + public DataTreeCandidate commit(final DataTreeModification modification) throws DataValidationFailedException { modification.ready(); dataTree.validate(modification); DataTreeCandidateTip candidate = dataTree.prepare(modification);