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%2FSimpleShardDataTreeCohort.java;h=cf9948cf03e0407f47103e4e2085d33a58d0346d;hp=5fac0abe6b0a221fafcf676a9764dfa66955e0be;hb=cd3a0e09db5a1def00c46a4be245dbdf648b539c;hpb=6276a65120a674b545ea787a5e1d9311bcdbf2af diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/SimpleShardDataTreeCohort.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/SimpleShardDataTreeCohort.java index 5fac0abe6b..cf9948cf03 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/SimpleShardDataTreeCohort.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/SimpleShardDataTreeCohort.java @@ -8,19 +8,18 @@ package org.opendaylight.controller.cluster.datastore; import akka.dispatch.ExecutionContexts; +import akka.dispatch.Futures; import akka.dispatch.OnComplete; +import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Preconditions; import com.google.common.base.Verify; import com.google.common.primitives.UnsignedLong; 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.List; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; -import org.opendaylight.yangtools.concepts.Identifiable; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; @@ -28,9 +27,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scala.concurrent.Future; -final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Identifiable { +final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { private static final Logger LOG = LoggerFactory.getLogger(SimpleShardDataTreeCohort.class); - private static final ListenableFuture VOID_FUTURE = Futures.immediateFuture(null); + private final DataTreeModification transaction; private final ShardDataTree dataTree; private final TransactionIdentifier transactionId; @@ -49,6 +48,15 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide this.userCohorts = Preconditions.checkNotNull(userCohorts); } + SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction, + final TransactionIdentifier transactionId, final Exception nextFailure) { + this.dataTree = Preconditions.checkNotNull(dataTree); + this.transaction = Preconditions.checkNotNull(transaction); + this.transactionId = Preconditions.checkNotNull(transactionId); + this.userCohorts = null; + this.nextFailure = Preconditions.checkNotNull(nextFailure); + } + @Override public TransactionIdentifier getIdentifier() { return transactionId; @@ -60,7 +68,6 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide } @Override - DataTreeModification getDataTreeModification() { return transaction; } @@ -70,21 +77,26 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide } @Override - public void canCommit(final FutureCallback callback) { - if(state == State.CAN_COMMIT_PENDING) { + public void canCommit(final FutureCallback newCallback) { + if (state == State.CAN_COMMIT_PENDING) { return; } checkState(State.READY); - this.callback = Preconditions.checkNotNull(callback); + this.callback = Preconditions.checkNotNull(newCallback); state = State.CAN_COMMIT_PENDING; - dataTree.startCanCommit(this); + + if (nextFailure == null) { + dataTree.startCanCommit(this); + } else { + failedCanCommit(nextFailure); + } } @Override - public void preCommit(final FutureCallback callback) { + public void preCommit(final FutureCallback newCallback) { checkState(State.CAN_COMMIT_COMPLETE); - this.callback = Preconditions.checkNotNull(callback); + this.callback = Preconditions.checkNotNull(newCallback); state = State.PRE_COMMIT_PENDING; if (nextFailure == null) { @@ -95,41 +107,50 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide } @Override - public ListenableFuture abort() { - dataTree.startAbort(this); + public void abort(final FutureCallback abortCallback) { + if (!dataTree.startAbort(this)) { + abortCallback.onSuccess(null); + return; + } + + candidate = null; state = State.ABORTED; - final Optional>> maybeAborts = userCohorts.abort(); + final Optional>> maybeAborts = userCohorts.abort(); if (!maybeAborts.isPresent()) { - return VOID_FUTURE; + abortCallback.onSuccess(null); + return; } - final Future> aborts = maybeAborts.get(); + final Future> aborts = Futures.sequence(maybeAborts.get(), ExecutionContexts.global()); if (aborts.isCompleted()) { - return VOID_FUTURE; + abortCallback.onSuccess(null); + return; } - final SettableFuture ret = SettableFuture.create(); aborts.onComplete(new OnComplete>() { @Override public void onComplete(final Throwable failure, final Iterable objs) { if (failure != null) { - ret.setException(failure); + abortCallback.onFailure(failure); } else { - ret.set(null); + abortCallback.onSuccess(null); } } }, ExecutionContexts.global()); - - return ret; } @Override - public void commit(final FutureCallback callback) { + public void commit(final FutureCallback newCallback) { checkState(State.PRE_COMMIT_COMPLETE); - this.callback = Preconditions.checkNotNull(callback); + this.callback = Preconditions.checkNotNull(newCallback); state = State.COMMIT_PENDING; - dataTree.startCommit(this, candidate); + + if (nextFailure == null) { + dataTree.startCommit(this, candidate); + } else { + failedCommit(nextFailure); + } } private FutureCallback switchState(final State newState) { @@ -141,6 +162,11 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide return ret; } + void setNewCandidate(final DataTreeCandidateTip dataTreeCandidate) { + checkState(State.PRE_COMMIT_COMPLETE); + this.candidate = Verify.verifyNotNull(dataTreeCandidate); + } + void successfulCanCommit() { switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null); } @@ -153,20 +179,21 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide * Run user-defined canCommit and preCommit hooks. We want to run these before we initiate persistence so that * any failure to validate is propagated before we record the transaction. * - * @param candidate {@link DataTreeCandidate} under consideration - * @throws ExecutionException - * @throws TimeoutException + * @param dataTreeCandidate {@link DataTreeCandidate} under consideration + * @throws ExecutionException if the operation fails + * @throws TimeoutException if the operation times out */ // FIXME: this should be asynchronous - void userPreCommit(final DataTreeCandidate candidate) throws ExecutionException, TimeoutException { - userCohorts.canCommit(candidate); + void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException { + userCohorts.reset(); + userCohorts.canCommit(dataTreeCandidate); userCohorts.preCommit(); } - void successfulPreCommit(final DataTreeCandidateTip candidate) { - LOG.trace("Transaction {} prepared candidate {}", transaction, candidate); - this.candidate = Verify.verifyNotNull(candidate); - switchState(State.PRE_COMMIT_COMPLETE).onSuccess(candidate); + void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) { + LOG.trace("Transaction {} prepared candidate {}", transaction, dataTreeCandidate); + this.candidate = Verify.verifyNotNull(dataTreeCandidate); + switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate); } void failedPreCommit(final Exception cause) { @@ -208,11 +235,20 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide } void reportFailure(final Exception cause) { - this.nextFailure = Preconditions.checkNotNull(cause); + if (nextFailure == null) { + this.nextFailure = Preconditions.checkNotNull(cause); + } else { + LOG.debug("Transaction {} already has a set failure, not updating it", transactionId, cause); + } } @Override public boolean isFailed() { return state == State.FAILED || nextFailure != null; } + + @Override + ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { + return super.addToStringAttributes(toStringHelper).add("nextFailure", nextFailure); + } }