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=91658a23b70bc48c361d99a974cba0ccd1b22a2b;hp=cf00d5fa39e3d317fe99af05d2f30f112fcb6f22;hb=a3cecfd01d0ef8922530924e3ee9684eb03ee5d6;hpb=ae11ac10dfd3579b1b685455ea642bbb08de68f1 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 cf00d5fa39..91658a23b7 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 @@ -17,12 +17,10 @@ import java.util.List; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import java.util.function.Function; import javax.annotation.Nonnull; -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; @@ -41,8 +39,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. @@ -57,7 +56,7 @@ 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; @@ -98,7 +97,7 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory previousFuture() { return null; @@ -120,11 +119,9 @@ 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; @@ -132,34 +129,29 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory * 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<>(); + private final ConcurrentMap> priorReadOnlyTxPromises = + new ConcurrentHashMap<>(); - TransactionChainProxy(final TransactionContextFactory parent) { - super(parent.getActorContext()); - - transactionChainId = new TransactionChainIdentifier(parent.getActorContext().getCurrentMemberName(), CHAIN_COUNTER.incrementAndGet()); + TransactionChainProxy(final TransactionContextFactory parent, final LocalHistoryIdentifier historyId) { + super(parent.getActorContext(), historyId); this.parent = parent; } - public String getTransactionChainId() { - return transactionChainId.toString(); - } - @Override public DOMStoreReadTransaction newReadOnlyTransaction() { currentState.checkReady(); @@ -186,12 +178,8 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory() { - @Override - public Object apply(Short version) { - return new CloseTransactionChain(transactionChainId.toString(), version).toSerializable(); - } - }); + getActorContext().broadcast(version -> new CloseTransactionChain(getHistoryId(), version).toSerializable(), + CloseTransactionChain.class); } private TransactionProxy allocateWriteTransaction(final TransactionType type) { @@ -204,7 +192,8 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory Future combineFutureWithPossiblePriorReadOnlyTxFutures(final Future future, final TransactionIdentifier txId) { - if(!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) { + if (!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) { Collection>> priorReadOnlyTxPromiseEntries = new ArrayList<>(priorReadOnlyTxPromises.entrySet()); - if(priorReadOnlyTxPromiseEntries.isEmpty()) { + if (priorReadOnlyTxPromiseEntries.isEmpty()) { return future; } List> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size()); - for(Entry> entry: priorReadOnlyTxPromiseEntries) { + for (Entry> entry: priorReadOnlyTxPromiseEntries) { LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey()); priorReadOnlyTxFutures.add(entry.getValue().future()); } @@ -301,11 +290,14 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory 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); + Preconditions.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); + Preconditions.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()) { @@ -333,13 +325,8 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory promise = priorReadOnlyTxPromises.remove(transactionId); - if(promise != null) { + if (promise != null) { promise.success(null); } } - - @Override - protected TransactionIdentifier nextIdentifier() { - return transactionChainId.newTransactionIdentifier(); - } }