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%2FTransactionChainProxy.java;h=0eded31e2e54ecc0d447c279ec0675d58db1c220;hp=0946b402fd9bb02b3129d48d5f5236ea605510a5;hb=e84f63ee098fff5b02cbce1281ca0d1208f966fa;hpb=86e398bb1e1a60f7516b398a0f27268bf232049d diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxy.java index 0946b402fd..0eded31e2e 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxy.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionChainProxy.java @@ -7,22 +7,29 @@ */ package org.opendaylight.controller.cluster.datastore; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; + import akka.actor.ActorSelection; +import akka.dispatch.Futures; import akka.dispatch.OnComplete; -import com.google.common.base.Preconditions; +import java.util.ArrayList; import java.util.Collection; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.List; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import org.opendaylight.controller.cluster.datastore.identifiers.TransactionChainIdentifier; -import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain; import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; -import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException; -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.DOMStoreTransactionChain; -import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree; +import org.opendaylight.mdsal.dom.api.DOMTransactionChainClosedException; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain; +import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction; +import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scala.concurrent.Future; @@ -33,8 +40,9 @@ import scala.concurrent.Promise; * at a time. For remote transactions, it also tracks the outstanding readiness requests * towards the shard and unblocks operations only after all have completed. */ -final class TransactionChainProxy extends AbstractTransactionContextFactory implements DOMStoreTransactionChain { - private static abstract class State { +final class TransactionChainProxy extends AbstractTransactionContextFactory + implements DOMStoreTransactionChain { + private abstract static class State { /** * Check if it is okay to allocate a new transaction. * @throws IllegalStateException if a transaction may not be allocated. @@ -49,13 +57,13 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory previousFuture(); } - private static abstract class Pending extends State { + private abstract static class Pending extends State { private final TransactionIdentifier transaction; private final Future previousFuture; Pending(final TransactionIdentifier transaction, final Future previousFuture) { this.previousFuture = previousFuture; - this.transaction = Preconditions.checkNotNull(transaction); + this.transaction = requireNonNull(transaction); } @Override @@ -90,7 +98,7 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory previousFuture() { return null; @@ -107,45 +115,61 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory STATE_UPDATER = AtomicReferenceFieldUpdater.newUpdater(TransactionChainProxy.class, State.class, "currentState"); - private final TransactionChainIdentifier transactionChainId; private final TransactionContextFactory parent; private volatile State currentState = IDLE_STATE; - TransactionChainProxy(final TransactionContextFactory parent) { - super(parent.getActorContext()); + /** + * This map holds Promise instances for each read-only tx. It is used to maintain ordering of tx creates + * wrt to read-only tx's between this class and a LocalTransactionChain since they're bridged by + * asynchronous futures. Otherwise, in the following scenario, eg: + *

+ * 1) Create write tx1 on chain + * 2) do write and submit + * 3) Create read-only tx2 on chain and issue read + * 4) Create write tx3 on chain, do write but do not submit + *

+ * if the sequence/timing is right, tx3 may create its local tx on the LocalTransactionChain before tx2, + * which results in tx2 failing b/c tx3 isn't ready yet. So maintaining ordering prevents this issue + * (see Bug 4774). + *

+ * A Promise is added via newReadOnlyTransaction. When the parent class completes the primary shard + * lookup and creates the TransactionContext (either success or failure), onTransactionContextCreated is + * called which completes the Promise. A write tx that is created prior to completion will wait on the + * Promise's Future via findPrimaryShard. + */ + private final ConcurrentMap> priorReadOnlyTxPromises = + new ConcurrentHashMap<>(); - transactionChainId = new TransactionChainIdentifier(parent.getActorContext().getCurrentMemberName(), CHAIN_COUNTER.incrementAndGet()); + TransactionChainProxy(final TransactionContextFactory parent, final LocalHistoryIdentifier historyId) { + super(parent.getActorUtils(), historyId); this.parent = parent; } - public String getTransactionChainId() { - return transactionChainId.toString(); - } - @Override public DOMStoreReadTransaction newReadOnlyTransaction() { currentState.checkReady(); - return new TransactionProxy(this, TransactionType.READ_ONLY); + TransactionProxy transactionProxy = new TransactionProxy(this, TransactionType.READ_ONLY); + priorReadOnlyTxPromises.put(transactionProxy.getIdentifier(), Futures.promise()); + return transactionProxy; } @Override public DOMStoreReadWriteTransaction newReadWriteTransaction() { - getActorContext().acquireTxCreationPermit(); + getActorUtils().acquireTxCreationPermit(); return allocateWriteTransaction(TransactionType.READ_WRITE); } @Override public DOMStoreWriteTransaction newWriteOnlyTransaction() { - getActorContext().acquireTxCreationPermit(); + getActorUtils().acquireTxCreationPermit(); return allocateWriteTransaction(TransactionType.WRITE_ONLY); } @@ -154,7 +178,9 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory new CloseTransactionChain(getHistoryId(), version).toSerializable(), + CloseTransactionChain.class); } private TransactionProxy allocateWriteTransaction(final TransactionType type) { @@ -167,7 +193,8 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory findPrimaryShard(final String shardName) { + protected Future findPrimaryShard(final String shardName, final TransactionIdentifier txId) { // Read current state atomically final State localState = currentState; // There are no outstanding futures, shortcut - final Future previous = localState.previousFuture(); + Future previous = localState.previousFuture(); if (previous == null) { - return parent.findPrimaryShard(shardName); + return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId); } final String previousTransactionId; - if(localState instanceof Pending){ + if (localState instanceof Pending) { previousTransactionId = ((Pending) localState).getIdentifier().toString(); - LOG.debug("Waiting for ready futures with pending Tx {}", previousTransactionId); + LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId); } else { previousTransactionId = ""; - LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId()); + LOG.debug("Waiting for ready futures on chain {}", getHistoryId()); } + previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId); + // Add a callback for completion of the combined Futures. - final Promise returnPromise = akka.dispatch.Futures.promise(); + final Promise returnPromise = Futures.promise(); final OnComplete onComplete = new OnComplete() { @Override public void onComplete(final Throwable failure, final Object notUsed) { if (failure != null) { // A Ready Future failed so fail the returned Promise. - LOG.error("Ready future failed for Tx {}", previousTransactionId); + LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId); returnPromise.failure(failure); } else { - LOG.debug("Previous Tx {} readied - proceeding to FindPrimaryShard", - previousTransactionId); + LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard", + txId, previousTransactionId); // Send the FindPrimaryShard message and use the resulting Future to complete the // returned Promise. - returnPromise.completeWith(parent.findPrimaryShard(shardName)); + returnPromise.completeWith(parent.findPrimaryShard(shardName, txId)); } } }; - previous.onComplete(onComplete, getActorContext().getClientDispatcher()); + previous.onComplete(onComplete, getActorUtils().getClientDispatcher()); + return returnPromise.future(); + } + + private Future combineFutureWithPossiblePriorReadOnlyTxFutures(final Future future, + final TransactionIdentifier txId) { + return priorReadOnlyTxPromises.isEmpty() || priorReadOnlyTxPromises.containsKey(txId) ? future + // Tough luck, we need do some work + : combineWithPriorReadOnlyTxFutures(future, txId); + } + + // Split out of the common path + private Future combineWithPriorReadOnlyTxFutures(final Future future, final TransactionIdentifier txId) { + // Take a stable snapshot, and check if we raced + final List>> priorReadOnlyTxPromiseEntries = + new ArrayList<>(priorReadOnlyTxPromises.entrySet()); + if (priorReadOnlyTxPromiseEntries.isEmpty()) { + return future; + } + + final List> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size()); + for (Entry> entry: priorReadOnlyTxPromiseEntries) { + LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey()); + priorReadOnlyTxFutures.add(entry.getValue().future()); + } + + final Future> combinedFutures = Futures.sequence(priorReadOnlyTxFutures, + getActorUtils().getClientDispatcher()); + + final Promise returnPromise = Futures.promise(); + final OnComplete> onComplete = new OnComplete<>() { + @Override + public void onComplete(final Throwable failure, final Iterable notUsed) { + LOG.debug("Tx: {} - prior read-only Tx futures complete", txId); + + // Complete the returned Promise with the original Future. + returnPromise.completeWith(future); + } + }; + + combinedFutures.onComplete(onComplete, getActorUtils().getClientDispatcher()); return returnPromise.future(); } @Override - protected void onTransactionReady(final TransactionIdentifier transaction, final Collection> cohortFutures) { + protected void onTransactionReady(final TransactionIdentifier transaction, + final Collection> cohortFutures) { final State localState = currentState; - Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, localState); + checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, + localState); final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier(); - Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, currentTx); + checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, + currentTx); // Transaction ready and we are not waiting for futures -- go to idle if (cohortFutures.isEmpty()) { @@ -238,8 +311,7 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory> combined = akka.dispatch.Futures.sequence( - cohortFutures, getActorContext().getClientDispatcher()); + final Future> combined = Futures.sequence(cohortFutures, getActorUtils().getClientDispatcher()); // Record the we have outstanding futures final State newState = new Submitted(transaction, combined); @@ -252,11 +324,14 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory arg1) { STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE); } - }, getActorContext().getClientDispatcher()); + }, getActorUtils().getClientDispatcher()); } @Override - protected TransactionIdentifier nextIdentifier() { - return transactionChainId.newTransactionIdentifier(); + protected void onTransactionContextCreated(final TransactionIdentifier transactionId) { + Promise promise = priorReadOnlyTxPromises.remove(transactionId); + if (promise != null) { + promise.success(null); + } } }