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%2FAbstractTransactionContextFactory.java;h=b50426a811e3f2fd677f187ba459a2c09653c34c;hp=a8a076f98fcee0c73919d8187ddb64ec2b88718b;hb=d3a97264ecf47e8c60ea11a7caebce41b580e91d;hpb=5133f3d0a92e89d863000858d639baa20bb688d5 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionContextFactory.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionContextFactory.java index a8a076f98f..b50426a811 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionContextFactory.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionContextFactory.java @@ -19,7 +19,9 @@ import javax.annotation.Nonnull; import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; -import org.opendaylight.controller.cluster.datastore.utils.ShardInfoListener; +import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; +import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction; +import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,8 +32,7 @@ import scala.util.Try; * Factory for creating local and remote TransactionContext instances. Maintains a cache of known local * transaction factories. */ -abstract class AbstractTransactionContextFactory - implements ShardInfoListener, AutoCloseable { +abstract class AbstractTransactionContextFactory implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(AbstractTransactionContextFactory.class); protected static final AtomicLong TX_COUNTER = new AtomicLong(); @@ -117,34 +118,16 @@ abstract class AbstractTransactionContextFactory maybeDataTree = primaryShardInfo.getLocalShardDataTree(); if (maybeDataTree.isPresent()) { - knownLocal.put(shardName, factoryForShard(shardName, primaryShardInfo.getPrimaryShardActor(), maybeDataTree.get())); - LOG.debug("Shard {} resolved to local data tree", shardName); - } - } - - @Override - public void onShardInfoUpdated(final String shardName, final PrimaryShardInfo primaryShardInfo) { - final F existing = knownLocal.get(shardName); - if (existing != null) { - if (primaryShardInfo != null) { - final Optional maybeDataTree = primaryShardInfo.getLocalShardDataTree(); - if (maybeDataTree.isPresent()) { - final DataTree newDataTree = maybeDataTree.get(); - final DataTree oldDataTree = dataTreeForFactory(existing); - if (!oldDataTree.equals(newDataTree)) { - final F newChain = factoryForShard(shardName, primaryShardInfo.getPrimaryShardActor(), newDataTree); - knownLocal.replace(shardName, existing, newChain); - LOG.debug("Replaced shard {} local data tree to {}", shardName, newDataTree); - } + if(!knownLocal.containsKey(shardName)) { + LOG.debug("Shard {} resolved to local data tree - adding local factory", shardName); - return; - } - } - if (knownLocal.remove(shardName, existing)) { - LOG.debug("Shard {} invalidated data tree {}", shardName, existing); - } else { - LOG.debug("Shard {} failed to invalidate data tree {} ... strange", shardName, existing); + F factory = factoryForShard(shardName, primaryShardInfo.getPrimaryShardActor(), maybeDataTree.get()); + knownLocal.putIfAbsent(shardName, factory); } + } else if(knownLocal.containsKey(shardName)) { + LOG.debug("Shard {} invalidating local data tree", shardName); + + knownLocal.remove(shardName); } } @@ -184,14 +167,6 @@ abstract class AbstractTransactionContextFactory void onTransactionReady(@Nonnull TransactionIdentifier transaction, @Nonnull Collection> cohortFutures); private static TransactionContext createLocalTransactionContext(final LocalTransactionFactory factory, final TransactionProxy parent) { - return new LocalTransactionContext(parent.getIdentifier(), factory.newReadWriteTransaction(parent.getIdentifier()), parent.getCompleter()); + switch(parent.getType()) { + case READ_ONLY: + final DOMStoreReadTransaction readOnly = factory.newReadOnlyTransaction(parent.getIdentifier()); + return new LocalTransactionContext(parent.getIdentifier(), readOnly, parent.getCompleter()) { + @Override + protected DOMStoreWriteTransaction getWriteDelegate() { + throw new UnsupportedOperationException(); + } + + @Override + protected DOMStoreReadTransaction getReadDelegate() { + return readOnly; + } + }; + case READ_WRITE: + final DOMStoreReadWriteTransaction readWrite = factory.newReadWriteTransaction(parent.getIdentifier()); + return new LocalTransactionContext(parent.getIdentifier(), readWrite, parent.getCompleter()) { + @Override + protected DOMStoreWriteTransaction getWriteDelegate() { + return readWrite; + } + + @Override + protected DOMStoreReadTransaction getReadDelegate() { + return readWrite; + } + }; + case WRITE_ONLY: + final DOMStoreWriteTransaction writeOnly = factory.newWriteOnlyTransaction(parent.getIdentifier()); + return new LocalTransactionContext(parent.getIdentifier(), writeOnly, parent.getCompleter()) { + @Override + protected DOMStoreWriteTransaction getWriteDelegate() { + return writeOnly; + } + + @Override + protected DOMStoreReadTransaction getReadDelegate() { + throw new UnsupportedOperationException(); + } + }; + default: + throw new IllegalArgumentException("Invalid transaction type: " + parent.getType()); + } } }