X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FTransactionProxy.java;h=b04dd29a5888ff716fdcd3ba8ad038800a36613e;hb=9b235df8e0b4d8c4c7419419538188cdf7b2bfc2;hp=164dc5ef73370f4811db4b2b590cb3774df2af07;hpb=dea3effede98cfb561c44d66b24c2d71a44b10a3;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java index 164dc5ef73..b04dd29a58 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java @@ -17,14 +17,17 @@ import com.google.common.collect.Iterables; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.messages.AbstractRead; import org.opendaylight.controller.cluster.datastore.messages.DataExists; @@ -35,10 +38,10 @@ import org.opendaylight.controller.cluster.datastore.modification.MergeModificat import org.opendaylight.controller.cluster.datastore.modification.WriteModification; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeAggregator; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; -import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTransaction; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction; -import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture; +import org.opendaylight.mdsal.common.api.MappingCheckedFuture; +import org.opendaylight.mdsal.common.api.ReadFailedException; +import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; 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.DataValidationFailedException; @@ -50,15 +53,17 @@ import scala.concurrent.Promise; /** * A transaction potentially spanning multiple backend shards. */ -public class TransactionProxy extends AbstractDOMStoreTransaction implements DOMStoreReadWriteTransaction { +public class TransactionProxy extends AbstractDOMStoreTransaction + implements DOMStoreReadWriteTransaction { private enum TransactionState { OPEN, READY, CLOSED, } + private static final Logger LOG = LoggerFactory.getLogger(TransactionProxy.class); - private final Map txContextWrappers = new HashMap<>(); + private final Map txContextWrappers = new TreeMap<>(); private final AbstractTransactionContextFactory txContextFactory; private final TransactionType type; private TransactionState state = TransactionState.OPEN; @@ -78,19 +83,19 @@ public class TransactionProxy extends AbstractDOMStoreTransaction CheckedFuture executeRead(String shardName, final AbstractRead readCmd) { - Preconditions.checkState(type != TransactionType.WRITE_ONLY, "Reads from write-only transactions are not allowed"); + private CheckedFuture executeRead(final String shardName, + final AbstractRead readCmd) { + Preconditions.checkState(type != TransactionType.WRITE_ONLY, + "Reads from write-only transactions are not allowed"); - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} {} {}", getIdentifier(), readCmd.getClass().getSimpleName(), readCmd.getPath()); - } + LOG.trace("Tx {} {} {}", getIdentifier(), readCmd.getClass().getSimpleName(), readCmd.getPath()); final SettableFuture proxyFuture = SettableFuture.create(); TransactionContextWrapper contextWrapper = getContextWrapper(shardName); contextWrapper.maybeExecuteTransactionOperation(new TransactionOperation() { @Override - public void invoke(TransactionContext transactionContext) { - transactionContext.executeRead(readCmd, proxyFuture); + public void invoke(final TransactionContext transactionContext, final Boolean havePermit) { + transactionContext.executeRead(readCmd, proxyFuture, havePermit); } }); @@ -99,15 +104,12 @@ public class TransactionProxy extends AbstractDOMStoreTransaction>, ReadFailedException> read(final YangInstanceIdentifier path) { - Preconditions.checkState(type != TransactionType.WRITE_ONLY, "Reads from write-only transactions are not allowed"); - - LOG.debug("Tx {} read {}", getIdentifier(), path); + Preconditions.checkState(type != TransactionType.WRITE_ONLY, + "Reads from write-only transactions are not allowed"); + Preconditions.checkNotNull(path, "path should not be null"); - if (YangInstanceIdentifier.EMPTY.equals(path)) { - return readAllData(); - } else { - return singleShardRead(shardNameFromIdentifier(path), path); - } + LOG.trace("Tx {} read {}", getIdentifier(), path); + return path.isEmpty() ? readAllData() : singleShardRead(shardNameFromIdentifier(path), path); } private CheckedFuture>, ReadFailedException> singleShardRead( @@ -117,7 +119,8 @@ public class TransactionProxy extends AbstractDOMStoreTransaction>, ReadFailedException> readAllData() { final Set allShardNames = txContextFactory.getActorContext().getConfiguration().getAllShardNames(); - final Collection>, ReadFailedException>> futures = new ArrayList<>(allShardNames.size()); + final Collection>, ReadFailedException>> futures = + new ArrayList<>(allShardNames.size()); for (String shardName : allShardNames) { futures.add(singleShardRead(shardName, YangInstanceIdentifier.EMPTY)); @@ -126,9 +129,8 @@ public class TransactionProxy extends AbstractDOMStoreTransaction>>> listFuture = Futures.allAsList(futures); final ListenableFuture>> aggregateFuture; - aggregateFuture = Futures.transform(listFuture, new Function>>, Optional>>() { - @Override - public Optional> apply(final List>> input) { + aggregateFuture = Futures.transform(listFuture, + (Function>>, Optional>>) input -> { try { return NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.EMPTY, input, txContextFactory.getActorContext().getSchemaContext(), @@ -136,8 +138,7 @@ public class TransactionProxy extends AbstractDOMStoreTransaction ret; switch (txContextWrappers.size()) { - case 0: - ret = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE; - break; - case 1: - final Entry e = Iterables.getOnlyElement(txContextWrappers.entrySet()); - ret = createSingleCommitCohort(e.getKey(), e.getValue()); - break; - default: - ret = createMultiCommitCohort(txContextWrappers.entrySet()); + case 0: + ret = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE; + break; + case 1: + final Entry e = Iterables.getOnlyElement( + txContextWrappers.entrySet()); + ret = createSingleCommitCohort(e.getKey(), e.getValue()); + break; + default: + ret = createMultiCommitCohort(); } txContextFactory.onTransactionReady(getIdentifier(), ret.getCohortFutures()); @@ -255,34 +255,36 @@ public class TransactionProxy extends AbstractDOMStoreTransaction getDirectCommitFuture(TransactionContext transactionContext, - OperationCallback.Reference operationCallbackRef) { + private Future getDirectCommitFuture(final TransactionContext transactionContext, + final OperationCallback.Reference operationCallbackRef, final Boolean havePermit) { TransactionRateLimitingCallback rateLimitingCallback = new TransactionRateLimitingCallback( txContextFactory.getActorContext()); operationCallbackRef.set(rateLimitingCallback); rateLimitingCallback.run(); - return transactionContext.directCommit(); + return transactionContext.directCommit(havePermit); } - private AbstractThreePhaseCommitCohort createMultiCommitCohort( - final Set> txContextWrapperEntries) { + private AbstractThreePhaseCommitCohort createMultiCommitCohort() { - final List cohorts = new ArrayList<>(txContextWrapperEntries.size()); - for (Entry e : txContextWrapperEntries) { + final List cohorts = new ArrayList<>(txContextWrappers.size()); + final java.util.Optional> shardNames = + java.util.Optional.of(new TreeSet<>(txContextWrappers.keySet())); + for (Entry e : txContextWrappers.entrySet()) { LOG.debug("Tx {} Readying transaction for shard {}", getIdentifier(), e.getKey()); final TransactionContextWrapper wrapper = e.getValue(); @@ -290,14 +292,10 @@ public class TransactionProxy extends AbstractDOMStoreTransaction txVersionSupplier = new Supplier() { - @Override - public Short get() { - return wrapper.getTransactionContext().getTransactionVersion(); - } - }; + Supplier txVersionSupplier = () -> wrapper.getTransactionContext().getTransactionVersion(); - cohorts.add(new ThreePhaseCommitCohortProxy.CohortInfo(wrapper.readyTransaction(), txVersionSupplier)); + cohorts.add(new ThreePhaseCommitCohortProxy.CohortInfo(wrapper.readyTransaction(shardNames), + txVersionSupplier)); } return new ThreePhaseCommitCohortProxy(txContextFactory.getActorContext(), cohorts, getIdentifier());