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%2FSimpleShardDataTreeCohort.java;h=f42af0b88e5847c3201520cfbca632b4b2da4dcd;hb=546cd1fd100dbaa36908b22c2f422320dbd8c4b2;hp=0527d013f246357ba68def42af0b76146b009741;hpb=5fd8e6506248cc34da72281a1662612f6c2b2f9a;p=controller.git 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 0527d013f2..f42af0b88e 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 @@ -7,32 +7,32 @@ */ package org.opendaylight.controller.cluster.datastore; -import akka.dispatch.ExecutionContexts; -import akka.dispatch.OnComplete; +import static java.util.Objects.requireNonNull; + +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 java.util.Optional; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; +import java.util.SortedSet; +import java.util.concurrent.CompletionStage; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import scala.concurrent.Future; 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; private final CompositeDataTreeCohort userCohorts; + private final @Nullable SortedSet participatingShardNames; private State state = State.READY; private DataTreeCandidateTip candidate; @@ -40,11 +40,23 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { private Exception nextFailure; SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction, - final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts) { - this.dataTree = Preconditions.checkNotNull(dataTree); - this.transaction = Preconditions.checkNotNull(transaction); - this.transactionId = Preconditions.checkNotNull(transactionId); - this.userCohorts = Preconditions.checkNotNull(userCohorts); + final TransactionIdentifier transactionId, final CompositeDataTreeCohort userCohorts, + final Optional> participatingShardNames) { + this.dataTree = requireNonNull(dataTree); + this.transaction = requireNonNull(transaction); + this.transactionId = requireNonNull(transactionId); + this.userCohorts = requireNonNull(userCohorts); + this.participatingShardNames = requireNonNull(participatingShardNames).orElse(null); + } + + SimpleShardDataTreeCohort(final ShardDataTree dataTree, final DataTreeModification transaction, + final TransactionIdentifier transactionId, final Exception nextFailure) { + this.dataTree = requireNonNull(dataTree); + this.transaction = requireNonNull(transaction); + this.transactionId = requireNonNull(transactionId); + this.userCohorts = null; + this.participatingShardNames = null; + this.nextFailure = requireNonNull(nextFailure); } @Override @@ -58,13 +70,18 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { } @Override - DataTreeModification getDataTreeModification() { return transaction; } + @Override + Optional> getParticipatingShardNames() { + return Optional.ofNullable(participatingShardNames); + } + private void checkState(final State expected) { - Preconditions.checkState(state == expected, "State %s does not match expected state %s", state, expected); + Preconditions.checkState(state == expected, "State %s does not match expected state %s for %s", + state, expected, getIdentifier()); } @Override @@ -74,15 +91,20 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { } checkState(State.READY); - this.callback = Preconditions.checkNotNull(newCallback); + this.callback = requireNonNull(newCallback); state = State.CAN_COMMIT_PENDING; - dataTree.startCanCommit(this); + + if (nextFailure == null) { + dataTree.startCanCommit(this); + } else { + failedCanCommit(nextFailure); + } } @Override public void preCommit(final FutureCallback newCallback) { checkState(State.CAN_COMMIT_COMPLETE); - this.callback = Preconditions.checkNotNull(newCallback); + this.callback = requireNonNull(newCallback); state = State.PRE_COMMIT_PENDING; if (nextFailure == null) { @@ -92,42 +114,42 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { } } - @Override - public void abort(final FutureCallback callback) { - dataTree.startAbort(this); - state = State.ABORTED; - - final Optional>> maybeAborts = userCohorts.abort(); - if (!maybeAborts.isPresent()) { - callback.onSuccess(null); + public void abort(final FutureCallback abortCallback) { + if (!dataTree.startAbort(this)) { + abortCallback.onSuccess(null); return; } - final Future> aborts = maybeAborts.get(); - if (aborts.isCompleted()) { - callback.onSuccess(null); + candidate = null; + state = State.ABORTED; + + final Optional> maybeAborts = userCohorts.abort(); + if (!maybeAborts.isPresent()) { + abortCallback.onSuccess(null); return; } - aborts.onComplete(new OnComplete>() { - @Override - public void onComplete(final Throwable failure, final Iterable objs) { - if (failure != null) { - callback.onFailure(failure); - } else { - callback.onSuccess(null); - } + maybeAborts.get().whenComplete((noop, failure) -> { + if (failure != null) { + abortCallback.onFailure(failure); + } else { + abortCallback.onSuccess(null); } - }, ExecutionContexts.global()); + }); } @Override public void commit(final FutureCallback newCallback) { checkState(State.PRE_COMMIT_COMPLETE); - this.callback = Preconditions.checkNotNull(newCallback); + this.callback = requireNonNull(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) { @@ -139,6 +161,11 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { 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); } @@ -152,13 +179,40 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { * any failure to validate is propagated before we record the transaction. * * @param dataTreeCandidate {@link DataTreeCandidate} under consideration - * @throws ExecutionException if the operation fails - * @throws TimeoutException if the operation times out + * @param futureCallback the callback to invoke on completion, which may be immediate or async. */ - // FIXME: this should be asynchronous - void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException { - userCohorts.canCommit(dataTreeCandidate); - userCohorts.preCommit(); + void userPreCommit(final DataTreeCandidate dataTreeCandidate, final FutureCallback futureCallback) { + userCohorts.reset(); + + final Optional> maybeCanCommitFuture = userCohorts.canCommit(dataTreeCandidate); + if (!maybeCanCommitFuture.isPresent()) { + doUserPreCommit(futureCallback); + return; + } + + maybeCanCommitFuture.get().whenComplete((noop, failure) -> { + if (failure != null) { + futureCallback.onFailure(failure); + } else { + doUserPreCommit(futureCallback); + } + }); + } + + private void doUserPreCommit(final FutureCallback futureCallback) { + final Optional> maybePreCommitFuture = userCohorts.preCommit(); + if (!maybePreCommitFuture.isPresent()) { + futureCallback.onSuccess(null); + return; + } + + maybePreCommitFuture.get().whenComplete((noop, failure) -> { + if (failure != null) { + futureCallback.onFailure(failure); + } else { + futureCallback.onSuccess(null); + } + }); } void successfulPreCommit(final DataTreeCandidateTip dataTreeCandidate) { @@ -167,7 +221,7 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { switchState(State.PRE_COMMIT_COMPLETE).onSuccess(dataTreeCandidate); } - void failedPreCommit(final Exception cause) { + void failedPreCommit(final Throwable cause) { if (LOG.isTraceEnabled()) { LOG.trace("Transaction {} failed to prepare", transaction, cause); } else { @@ -178,15 +232,25 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { switchState(State.FAILED).onFailure(cause); } - void successfulCommit(final UnsignedLong journalIndex) { - try { - userCohorts.commit(); - } catch (TimeoutException | ExecutionException e) { - // We are probably dead, depending on what the cohorts end up doing - LOG.error("User cohorts failed to commit", e); + void successfulCommit(final UnsignedLong journalIndex, final Runnable onComplete) { + final Optional> maybeCommitFuture = userCohorts.commit(); + if (!maybeCommitFuture.isPresent()) { + finishSuccessfulCommit(journalIndex, onComplete); + return; } + maybeCommitFuture.get().whenComplete((noop, failure) -> { + if (failure != null) { + LOG.error("User cohorts failed to commit", failure); + } + + finishSuccessfulCommit(journalIndex, onComplete); + }); + } + + private void finishSuccessfulCommit(final UnsignedLong journalIndex, final Runnable onComplete) { switchState(State.COMMITTED).onSuccess(journalIndex); + onComplete.run(); } void failedCommit(final Exception cause) { @@ -206,11 +270,20 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort { } void reportFailure(final Exception cause) { - this.nextFailure = Preconditions.checkNotNull(cause); + if (nextFailure == null) { + this.nextFailure = requireNonNull(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); + } }