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=b9e39975e5c869529a31fa2a56dad6e0c0db9378;hp=71eaf7dbc8613837a69020a108e00f3d1781553e;hb=8ed5603b4f3503559f2f85137c42d7605ebfd3e9;hpb=925cb4a228d0fda99c7bfeb432eb25285a223887 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 71eaf7dbc8..b9e39975e5 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,17 @@ package org.opendaylight.controller.cluster.datastore; import akka.dispatch.ExecutionContexts; +import akka.dispatch.Futures; import akka.dispatch.OnComplete; 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 +26,36 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scala.concurrent.Future; -final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Identifiable { +abstract class SimpleShardDataTreeCohort extends ShardDataTreeCohort { + static final class DeadOnArrival extends SimpleShardDataTreeCohort { + private final Exception failure; + + DeadOnArrival(final ShardDataTree dataTree, final DataTreeModification transaction, + final TransactionIdentifier transactionId, final Exception failure) { + super(dataTree, transaction, transactionId, null); + this.failure = Preconditions.checkNotNull(failure); + } + + @Override + void throwCanCommitFailure() throws Exception { + throw failure; + } + } + + static final class Normal extends SimpleShardDataTreeCohort { + Normal(final ShardDataTree dataTree, final DataTreeModification transaction, + final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) { + super(dataTree, transaction, transactionId, Preconditions.checkNotNull(userCohorts)); + } + + @Override + void throwCanCommitFailure() { + // No-op + } + } + 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; @@ -46,7 +71,7 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide this.dataTree = Preconditions.checkNotNull(dataTree); this.transaction = Preconditions.checkNotNull(transaction); this.transactionId = Preconditions.checkNotNull(transactionId); - this.userCohorts = Preconditions.checkNotNull(userCohorts); + this.userCohorts = userCohorts; } @Override @@ -60,7 +85,6 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide } @Override - DataTreeModification getDataTreeModification() { return transaction; } @@ -95,33 +119,37 @@ 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 @@ -129,7 +157,12 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide checkState(State.PRE_COMMIT_COMPLETE); 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 +174,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); } @@ -159,6 +197,7 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide */ // FIXME: this should be asynchronous void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException { + userCohorts.reset(); userCohorts.canCommit(dataTreeCandidate); userCohorts.preCommit(); } @@ -211,6 +250,13 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide this.nextFailure = Preconditions.checkNotNull(cause); } + /** + * If there is an initial failure, throw it so the caller can process it. + * + * @throws Exception reported failure. + */ + abstract void throwCanCommitFailure() throws Exception; + @Override public boolean isFailed() { return state == State.FAILED || nextFailure != null;