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=64b9086c250c16f759a417003d0cefc9839f688b;hb=81d69b64c20a8e17279a57920beab854922c668c;hp=d79cd6f69f4b0e3e4f1171a332b45c36adbbd515;hpb=df894b365aa4f958a031cf453a028c947d5f7f8d;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 d79cd6f69f..64b9086c25 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 @@ -18,11 +18,11 @@ import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.util.concurrent.CheckedFuture; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import javax.annotation.concurrent.GuardedBy; +import org.opendaylight.controller.cluster.datastore.compat.PreLithiumTransactionContextImpl; import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException; import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction; @@ -164,7 +165,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { * PhantomReference. */ private List remoteTransactionActors; - private AtomicBoolean remoteTransactionActorsMB; + private volatile AtomicBoolean remoteTransactionActorsMB; /** * Stores the create transaction results per shard. @@ -177,8 +178,10 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { private final String transactionChainId; private final SchemaContext schemaContext; private boolean inReadyState; - private final Semaphore operationLimiter; - private final OperationCompleter operationCompleter; + + private volatile boolean initialized; + private Semaphore operationLimiter; + private OperationCompleter operationCompleter; public TransactionProxy(ActorContext actorContext, TransactionType transactionType) { this(actorContext, transactionType, ""); @@ -199,25 +202,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { memberName = "UNKNOWN-MEMBER"; } - this.identifier = TransactionIdentifier.builder().memberName(memberName).counter( - counter.getAndIncrement()).build(); - - if(transactionType == TransactionType.READ_ONLY) { - // Read-only Tx's aren't explicitly closed by the client so we create a PhantomReference - // to close the remote Tx's when this instance is no longer in use and is garbage - // collected. - - remoteTransactionActors = Lists.newArrayList(); - remoteTransactionActorsMB = new AtomicBoolean(); - - TransactionProxyCleanupPhantomReference cleanup = - new TransactionProxyCleanupPhantomReference(this); - phantomReferenceCache.put(cleanup, cleanup); - } - - // Note : Currently mailbox-capacity comes from akka.conf and not from the config-subsystem - this.operationLimiter = new Semaphore(actorContext.getTransactionOutstandingOperationLimit()); - this.operationCompleter = new OperationCompleter(operationLimiter); + this.identifier = new TransactionIdentifier(memberName, counter.getAndIncrement()); LOG.debug("Created txn {} of type {} on chain {}", identifier, transactionType, transactionChainId); } @@ -257,14 +242,17 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { throttleOperation(); + final SettableFuture>> proxyFuture = SettableFuture.create(); + TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path); - return txFutureCallback.enqueueReadOperation(new ReadOperation>>() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override - public CheckedFuture>, ReadFailedException> invoke( - TransactionContext transactionContext) { - return transactionContext.readData(path); + public void invoke(TransactionContext transactionContext) { + transactionContext.readData(path, proxyFuture); } }); + + return MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER); } @Override @@ -277,15 +265,18 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { throttleOperation(); + final SettableFuture proxyFuture = SettableFuture.create(); + TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path); - return txFutureCallback.enqueueReadOperation(new ReadOperation() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override - public CheckedFuture invoke(TransactionContext transactionContext) { - return transactionContext.dataExists(path); + public void invoke(TransactionContext transactionContext) { + transactionContext.dataExists(path, proxyFuture); } }); - } + return MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER); + } private void checkModificationState() { Preconditions.checkState(transactionType != TransactionType.READ_ONLY, @@ -299,8 +290,19 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { } private void throttleOperation(int acquirePermits) { + if(!initialized) { + // Note : Currently mailbox-capacity comes from akka.conf and not from the config-subsystem + operationLimiter = new Semaphore(actorContext.getTransactionOutstandingOperationLimit()); + operationCompleter = new OperationCompleter(operationLimiter); + + // Make sure we write this last because it's volatile and will also publish the non-volatile writes + // above as well so they'll be visible to other threads. + initialized = true; + } + try { - if(!operationLimiter.tryAcquire(acquirePermits, actorContext.getDatastoreContext().getOperationTimeoutInSeconds(), TimeUnit.SECONDS)){ + if(!operationLimiter.tryAcquire(acquirePermits, + actorContext.getDatastoreContext().getOperationTimeoutInSeconds(), TimeUnit.SECONDS)){ LOG.warn("Failed to acquire operation permit for transaction {}", getIdentifier()); } } catch (InterruptedException e) { @@ -323,7 +325,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { throttleOperation(); TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path); - txFutureCallback.enqueueModifyOperation(new TransactionOperation() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override public void invoke(TransactionContext transactionContext) { transactionContext.writeData(path, data); @@ -341,7 +343,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { throttleOperation(); TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path); - txFutureCallback.enqueueModifyOperation(new TransactionOperation() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override public void invoke(TransactionContext transactionContext) { transactionContext.mergeData(path, data); @@ -359,7 +361,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { throttleOperation(); TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path); - txFutureCallback.enqueueModifyOperation(new TransactionOperation() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override public void invoke(TransactionContext transactionContext) { transactionContext.deleteData(path); @@ -372,13 +374,18 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { checkModificationState(); - throttleOperation(txFutureCallbackMap.size()); - inReadyState = true; LOG.debug("Tx {} Readying {} transactions for commit", identifier, txFutureCallbackMap.size()); + if(txFutureCallbackMap.size() == 0) { + onTransactionReady(Collections.>emptyList()); + return NoOpDOMStoreThreePhaseCommitCohort.INSTANCE; + } + + throttleOperation(txFutureCallbackMap.size()); + List> cohortFutures = Lists.newArrayList(); for(TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) { @@ -386,12 +393,21 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { LOG.debug("Tx {} Readying transaction for shard {} chain {}", identifier, txFutureCallback.getShardName(), transactionChainId); - Future future = txFutureCallback.enqueueFutureOperation(new FutureOperation() { - @Override - public Future invoke(TransactionContext transactionContext) { - return transactionContext.readyTransaction(); - } - }); + final TransactionContext transactionContext = txFutureCallback.getTransactionContext(); + final Future future; + if (transactionContext != null) { + // avoid the creation of a promise and a TransactionOperation + future = transactionContext.readyTransaction(); + } else { + final Promise promise = akka.dispatch.Futures.promise(); + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { + @Override + public void invoke(TransactionContext transactionContext) { + promise.completeWith(transactionContext.readyTransaction()); + } + }); + future = promise.future(); + } cohortFutures.add(future); } @@ -410,18 +426,6 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { protected void onTransactionReady(List> cohortFutures) { } - /** - * Method called to send a CreateTransaction message to a shard. - * - * @param shard the shard actor to send to - * @param serializedCreateMessage the serialized message to send - * @return the response Future - */ - protected Future sendCreateTransaction(ActorSelection shard, - Object serializedCreateMessage) { - return actorContext.executeOperationAsync(shard, serializedCreateMessage); - } - @Override public Object getIdentifier() { return this.identifier; @@ -430,7 +434,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { @Override public void close() { for (TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) { - txFutureCallback.enqueueModifyOperation(new TransactionOperation() { + txFutureCallback.enqueueTransactionOperation(new TransactionOperation() { @Override public void invoke(TransactionContext transactionContext) { transactionContext.closeTransaction(); @@ -440,7 +444,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { txFutureCallbackMap.clear(); - if(transactionType == TransactionType.READ_ONLY) { + if(remoteTransactionActorsMB != null) { remoteTransactionActors.clear(); remoteTransactionActorsMB.set(true); } @@ -450,14 +454,17 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { return ShardStrategyFactory.getStrategy(path).findShard(path); } + protected Future sendFindPrimaryShardAsync(String shardName) { + return actorContext.findPrimaryShardAsync(shardName); + } + private TransactionFutureCallback getOrCreateTxFutureCallback(YangInstanceIdentifier path) { String shardName = shardNameFromIdentifier(path); TransactionFutureCallback txFutureCallback = txFutureCallbackMap.get(shardName); if(txFutureCallback == null) { - Future findPrimaryFuture = actorContext.findPrimaryShardAsync(shardName); + Future findPrimaryFuture = sendFindPrimaryShardAsync(shardName); - final TransactionFutureCallback newTxFutureCallback = - new TransactionFutureCallback(shardName); + final TransactionFutureCallback newTxFutureCallback = new TransactionFutureCallback(shardName); txFutureCallback = newTxFutureCallback; txFutureCallbackMap.put(shardName, txFutureCallback); @@ -471,7 +478,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { newTxFutureCallback.setPrimaryShard(primaryShard); } } - }, actorContext.getActorSystem().dispatcher()); + }, actorContext.getClientDispatcher()); } return txFutureCallback; @@ -492,20 +499,6 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { void invoke(TransactionContext transactionContext); } - /** - * This interface returns a Guava Future - */ - private static interface ReadOperation { - CheckedFuture invoke(TransactionContext transactionContext); - } - - /** - * This interface returns a Scala Future - */ - private static interface FutureOperation { - Future invoke(TransactionContext transactionContext); - } - /** * Implements a Future OnComplete callback for a CreateTransaction message. This class handles * retries, up to a limit, if the shard doesn't have a leader yet. This is done by scheduling a @@ -582,64 +575,7 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { } } - - Future enqueueFutureOperation(final FutureOperation op) { - - Future future; - - if (transactionContext != null) { - future = op.invoke(transactionContext); - } else { - // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future - // callback to be executed after the Tx is created. - final Promise promise = akka.dispatch.Futures.promise(); - addTxOperationOnComplete(new TransactionOperation() { - @Override - public void invoke(TransactionContext transactionContext) { - promise.completeWith(op.invoke(transactionContext)); - } - }); - - future = promise.future(); - } - - return future; - } - - CheckedFuture enqueueReadOperation(final ReadOperation op) { - - CheckedFuture future; - - if (transactionContext != null) { - future = op.invoke(transactionContext); - } else { - // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future - // callback to be executed after the Tx is created. - final SettableFuture proxyFuture = SettableFuture.create(); - addTxOperationOnComplete(new TransactionOperation() { - @Override - public void invoke(TransactionContext transactionContext) { - Futures.addCallback(op.invoke(transactionContext), new FutureCallback() { - @Override - public void onSuccess(T data) { - proxyFuture.set(data); - } - - @Override - public void onFailure(Throwable t) { - proxyFuture.setException(t); - } - }); - } - }); - - future = MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER); - } - - return future; - } - - void enqueueModifyOperation(final TransactionOperation op) { + void enqueueTransactionOperation(final TransactionOperation op) { if (transactionContext != null) { op.invoke(transactionContext); @@ -654,12 +590,13 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { * Performs a CreateTransaction try async. */ private void tryCreateTransaction() { - Future createTxFuture = sendCreateTransaction(primaryShard, - new CreateTransaction(identifier.toString(), - TransactionProxy.this.transactionType.ordinal(), - getTransactionChainId()).toSerializable()); + Object serializedCreateMessage = new CreateTransaction(identifier.toString(), + TransactionProxy.this.transactionType.ordinal(), + getTransactionChainId()).toSerializable(); + + Future createTxFuture = actorContext.executeOperationAsync(primaryShard, serializedCreateMessage); - createTxFuture.onComplete(this, actorContext.getActorSystem().dispatcher()); + createTxFuture.onComplete(this, actorContext.getClientDispatcher()); } @Override @@ -679,11 +616,16 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { public void run() { tryCreateTransaction(); } - }, actorContext.getActorSystem().dispatcher()); + }, actorContext.getClientDispatcher()); return; } } + // Mainly checking for state violation here to perform a volatile read of "initialized" to + // ensure updates to operationLimter et al are visible to this thread (ie we're doing + // "piggy-back" synchronization here). + Preconditions.checkState(initialized, "Tx was not propertly initialized."); + // Create the TransactionContext from the response or failure. Store the new // TransactionContext locally until we've completed invoking the // TransactionOperations. This avoids thread timing issues which could cause @@ -747,11 +689,24 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { private TransactionContext createValidTransactionContext(CreateTransactionReply reply) { String transactionPath = reply.getTransactionPath(); - LOG.debug("Tx {} Received transaction actor path {}", identifier, transactionPath); + LOG.debug("Tx {} Received {}", identifier, reply); ActorSelection transactionActor = actorContext.actorSelection(transactionPath); if (transactionType == TransactionType.READ_ONLY) { + // Read-only Tx's aren't explicitly closed by the client so we create a PhantomReference + // to close the remote Tx's when this instance is no longer in use and is garbage + // collected. + + if(remoteTransactionActorsMB == null) { + remoteTransactionActors = Lists.newArrayList(); + remoteTransactionActorsMB = new AtomicBoolean(); + + TransactionProxyCleanupPhantomReference cleanup = + new TransactionProxyCleanupPhantomReference(TransactionProxy.this); + phantomReferenceCache.put(cleanup, cleanup); + } + // Add the actor to the remoteTransactionActors list for access by the // cleanup PhantonReference. remoteTransactionActors.add(transactionActor); @@ -765,8 +720,45 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { // Check if TxActor is created in the same node boolean isTxActorLocal = actorContext.isPathLocal(transactionPath); - return new TransactionContextImpl(transactionPath, transactionActor, identifier, - actorContext, schemaContext, isTxActorLocal, reply.getVersion(), operationCompleter); + if(reply.getVersion() >= DataStoreVersions.LITHIUM_VERSION) { + return new TransactionContextImpl(transactionPath, transactionActor, identifier, + actorContext, schemaContext, isTxActorLocal, reply.getVersion(), operationCompleter); + } else { + return new PreLithiumTransactionContextImpl(transactionPath, transactionActor, identifier, + actorContext, schemaContext, isTxActorLocal, reply.getVersion(), operationCompleter); + } + } + } + + private static class NoOpDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort { + static NoOpDOMStoreThreePhaseCommitCohort INSTANCE = new NoOpDOMStoreThreePhaseCommitCohort(); + + private static final ListenableFuture IMMEDIATE_VOID_SUCCESS = + com.google.common.util.concurrent.Futures.immediateFuture(null); + private static final ListenableFuture IMMEDIATE_BOOLEAN_SUCCESS = + com.google.common.util.concurrent.Futures.immediateFuture(Boolean.TRUE); + + private NoOpDOMStoreThreePhaseCommitCohort() { + } + + @Override + public ListenableFuture canCommit() { + return IMMEDIATE_BOOLEAN_SUCCESS; + } + + @Override + public ListenableFuture preCommit() { + return IMMEDIATE_VOID_SUCCESS; + } + + @Override + public ListenableFuture abort() { + return IMMEDIATE_VOID_SUCCESS; + } + + @Override + public ListenableFuture commit() { + return IMMEDIATE_VOID_SUCCESS; } } }