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.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.slf4j.LoggerFactory;
import scala.concurrent.Future;
-final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
+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> VOID_FUTURE = Futures.immediateFuture(null);
+
private final DataTreeModification transaction;
private final ShardDataTree dataTree;
private final TransactionIdentifier transactionId;
this.dataTree = Preconditions.checkNotNull(dataTree);
this.transaction = Preconditions.checkNotNull(transaction);
this.transactionId = Preconditions.checkNotNull(transactionId);
- this.userCohorts = Preconditions.checkNotNull(userCohorts);
+ this.userCohorts = userCohorts;
}
@Override
}
@Override
-
DataTreeModification getDataTreeModification() {
return transaction;
}
}
@Override
- public ListenableFuture<Void> abort() {
- dataTree.startAbort(this);
+ public void abort(final FutureCallback<Void> abortCallback) {
+ if (!dataTree.startAbort(this)) {
+ abortCallback.onSuccess(null);
+ return;
+ }
+
+ candidate = null;
state = State.ABORTED;
final Optional<Future<Iterable<Object>>> maybeAborts = userCohorts.abort();
if (!maybeAborts.isPresent()) {
- return VOID_FUTURE;
+ abortCallback.onSuccess(null);
+ return;
}
final Future<Iterable<Object>> aborts = maybeAborts.get();
if (aborts.isCompleted()) {
- return VOID_FUTURE;
+ abortCallback.onSuccess(null);
+ return;
}
- final SettableFuture<Void> ret = SettableFuture.create();
aborts.onComplete(new OnComplete<Iterable<Object>>() {
@Override
public void onComplete(final Throwable failure, final Iterable<Object> objs) {
if (failure != null) {
- ret.setException(failure);
+ abortCallback.onFailure(failure);
} else {
- ret.set(null);
+ abortCallback.onSuccess(null);
}
}
}, ExecutionContexts.global());
-
- return ret;
}
@Override
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 <T> FutureCallback<T> switchState(final State newState) {
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);
}
*/
// FIXME: this should be asynchronous
void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
+ userCohorts.reset();
userCohorts.canCommit(dataTreeCandidate);
userCohorts.preCommit();
}
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;