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%2FRemoteTransactionContext.java;h=941c86116cb7926ebd13a6b55b79b32484bdd54e;hb=f41c5e6e6f6e10b36b1e4b1992877e38e718c8fb;hp=af0c8714092ea37e82fb389b828cf415ff8bdf31;hpb=74926cb05f2e5e4937658ca61444f7d7c846eb00;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/RemoteTransactionContext.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/RemoteTransactionContext.java index af0c871409..941c86116c 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/RemoteTransactionContext.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/RemoteTransactionContext.java @@ -9,14 +9,14 @@ package org.opendaylight.controller.cluster.datastore; import akka.actor.ActorSelection; +import akka.dispatch.Futures; import akka.dispatch.OnComplete; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.SettableFuture; -import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; import org.opendaylight.controller.cluster.datastore.messages.AbstractRead; import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications; import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction; -import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage; import org.opendaylight.controller.cluster.datastore.modification.AbstractModification; import org.opendaylight.controller.cluster.datastore.modification.Modification; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; @@ -36,27 +36,26 @@ public class RemoteTransactionContext extends AbstractTransactionContext { private final ActorContext actorContext; private final ActorSelection actor; - private final boolean isTxActorLocal; - private final short remoteTransactionVersion; private final OperationLimiter limiter; private BatchedModifications batchedModifications; private int totalBatchedModificationsSent; + private int batchPermits; + + /** + * We have observed a failed modification batch. This transaction context is effectively doomed, as the backend + * does not have a correct view of the world. If this happens, we do not limit operations but rather short-cut them + * to a either a no-op (modifications) or a failure (reads). Once the transaction is ready, though, we send the + * message to resynchronize with the backend, sharing a 'lost message' failure path. + */ + private volatile Throwable failedModification; protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor, - ActorContext actorContext, boolean isTxActorLocal, - short remoteTransactionVersion, OperationLimiter limiter) { - super(identifier); + ActorContext actorContext, short remoteTransactionVersion, OperationLimiter limiter) { + super(identifier, remoteTransactionVersion); this.limiter = Preconditions.checkNotNull(limiter); this.actor = actor; this.actorContext = actorContext; - this.isTxActorLocal = isTxActorLocal; - this.remoteTransactionVersion = remoteTransactionVersion; - } - - private Future completeOperation(Future operationFuture){ - operationFuture.onComplete(limiter, actorContext.getClientDispatcher()); - return operationFuture; } private ActorSelection getActor() { @@ -67,25 +66,12 @@ public class RemoteTransactionContext extends AbstractTransactionContext { return actorContext; } - protected short getRemoteTransactionVersion() { - return remoteTransactionVersion; - } - - protected Future executeOperationAsync(SerializableMessage msg) { - return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable())); - } - @Override public void closeTransaction() { LOG.debug("Tx {} closeTransaction called", getIdentifier()); TransactionContextCleanup.untrack(this); - actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable()); - } - - @Override - public boolean supportsDirectCommit() { - return true; + actorContext.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable()); } @Override @@ -118,19 +104,23 @@ public class RemoteTransactionContext extends AbstractTransactionContext { } private BatchedModifications newBatchedModifications() { - return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId()); + return new BatchedModifications(getIdentifier(), getTransactionVersion()); } - private void batchModification(Modification modification) { + private void batchModification(Modification modification, boolean havePermit) { incrementModificationCount(); - if(batchedModifications == null) { + if (havePermit) { + ++batchPermits; + } + + if (batchedModifications == null) { batchedModifications = newBatchedModifications(); } batchedModifications.addModification(modification); - if(batchedModifications.getModifications().size() >= - actorContext.getDatastoreContext().getShardBatchedModificationCount()) { + if (batchedModifications.getModifications().size() + >= actorContext.getDatastoreContext().getShardBatchedModificationCount()) { sendBatchedModifications(); } } @@ -141,26 +131,52 @@ public class RemoteTransactionContext extends AbstractTransactionContext { protected Future sendBatchedModifications(boolean ready, boolean doCommitOnReady) { Future sent = null; - if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) { - if(batchedModifications == null) { + if (ready || batchedModifications != null && !batchedModifications.getModifications().isEmpty()) { + if (batchedModifications == null) { batchedModifications = newBatchedModifications(); } - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(), - batchedModifications.getModifications().size(), ready); - } + LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(), + batchedModifications.getModifications().size(), ready); batchedModifications.setReady(ready); batchedModifications.setDoCommitOnReady(doCommitOnReady); batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent); - sent = executeOperationAsync(batchedModifications); - if(ready) { + final BatchedModifications toSend = batchedModifications; + final int permitsToRelease = batchPermits; + batchPermits = 0; + + if (ready) { batchedModifications = null; } else { batchedModifications = newBatchedModifications(); + + final Throwable failure = failedModification; + if (failure != null) { + // We have observed a modification failure, it does not make sense to send this batch. This speeds + // up the time when the application could be blocked due to messages timing out and operation + // limiter kicking in. + LOG.debug("Tx {} modifications previously failed, not sending a non-ready batch", getIdentifier()); + limiter.release(permitsToRelease); + return Futures.failed(failure); + } } + + sent = actorContext.executeOperationAsync(getActor(), toSend.toSerializable(), + actorContext.getTransactionCommitOperationTimeout()); + sent.onComplete(new OnComplete() { + @Override + public void onComplete(Throwable failure, Object success) { + if (failure != null) { + LOG.debug("Tx {} modifications failed", getIdentifier(), failure); + failedModification = failure; + } else { + LOG.debug("Tx {} modifications completed with {}", getIdentifier(), success); + } + limiter.release(permitsToRelease); + } + }, actorContext.getClientDispatcher()); } return sent; @@ -168,60 +184,77 @@ public class RemoteTransactionContext extends AbstractTransactionContext { @Override public void executeModification(AbstractModification modification) { - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass() - .getSimpleName(), modification.getPath()); - } + LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), + modification.getClass().getSimpleName(), modification.getPath()); - acquireOperation(); - batchModification(modification); + final boolean havePermit = failedModification == null && acquireOperation(); + batchModification(modification, havePermit); } @Override public void executeRead(final AbstractRead readCmd, final SettableFuture returnFuture) { - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(), - readCmd.getPath()); + LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(), + readCmd.getPath()); + + final Throwable failure = failedModification; + if (failure != null) { + // If we know there was a previous modification failure, we must not send a read request, as it risks + // returning incorrect data. We check this before acquiring an operation simply because we want the app + // to complete this transaction as soon as possible. + returnFuture.setException(new ReadFailedException("Previous modification failed, cannot " + + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure)); + return; } // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the // public API contract. - acquireOperation(); + final boolean havePermit = acquireOperation(); sendBatchedModifications(); OnComplete onComplete = new OnComplete() { @Override - public void onComplete(Throwable failure, Object response) throws Throwable { - if(failure != null) { - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(), - failure); - } - returnFuture.setException(new ReadFailedException("Error checking " + readCmd.getClass().getSimpleName() - + " for path " + readCmd.getPath(), failure)); + public void onComplete(Throwable failure, Object response) { + // We have previously acquired an operation, now release it, no matter what happened + if (havePermit) { + limiter.release(); + } + + if (failure != null) { + LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(), + failure); + + returnFuture.setException(new ReadFailedException("Error checking " + + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure)); } else { - if(LOG.isDebugEnabled()) { - LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName()); - } + LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName()); readCmd.processResponse(response, returnFuture); } } }; - Future future = executeOperationAsync(readCmd); - + final Future future = actorContext.executeOperationAsync(getActor(), + readCmd.asVersion(getTransactionVersion()).toSerializable(), actorContext.getOperationTimeout()); future.onComplete(onComplete, actorContext.getClientDispatcher()); } /** - * Acquire operation from the limiter if the hand-off has completed. If - * the hand-off is still ongoing, this method does nothing. + * Acquire operation from the limiter if the hand-off has completed. If the hand-off is still ongoing, this method + * does nothing. + * + * @return True if a permit was successfully acquired, false otherwise */ - private final void acquireOperation() { + private boolean acquireOperation() { if (isOperationHandOffComplete()) { - limiter.acquire(); + if (limiter.acquire()) { + return true; + } + + LOG.warn("Failed to acquire execute operation permit for transaction {} on actor {}", getIdentifier(), + actor); } + + return false; } @Override