BUG-7033: Fix commit exception due to pipe-lining
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SimpleShardDataTreeCohort.java
index 5fac0abe6b0a221fafcf676a9764dfa66955e0be..197c90a60bd62ea15aff24b23b1e17d9a47c5cfa 100644 (file)
@@ -13,14 +13,10 @@ 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.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 +24,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import scala.concurrent.Future;
 
-final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Identifiable<TransactionIdentifier> {
+final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
     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;
@@ -60,7 +56,6 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide
     }
 
     @Override
-
     DataTreeModification getDataTreeModification() {
         return transaction;
     }
@@ -70,21 +65,21 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide
     }
 
     @Override
-    public void canCommit(final FutureCallback<Void> callback) {
-        if(state == State.CAN_COMMIT_PENDING) {
+    public void canCommit(final FutureCallback<Void> 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);
     }
 
     @Override
-    public void preCommit(final FutureCallback<DataTreeCandidate> callback) {
+    public void preCommit(final FutureCallback<DataTreeCandidate> 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 +90,50 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide
     }
 
     @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
-    public void commit(final FutureCallback<UnsignedLong> callback) {
+    public void commit(final FutureCallback<UnsignedLong> 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 <T> FutureCallback<T> switchState(final State newState) {
@@ -141,6 +145,11 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide
         return ret;
     }
 
+    void setNewCandidate(DataTreeCandidateTip dataTreeCandidate) {
+        checkState(State.PRE_COMMIT_COMPLETE);
+        this.candidate = Verify.verifyNotNull(dataTreeCandidate);
+    }
+
     void successfulCanCommit() {
         switchState(State.CAN_COMMIT_COMPLETE).onSuccess(null);
     }
@@ -153,20 +162,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) {
@@ -202,6 +212,12 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort implements Ide
         switchState(State.FAILED).onFailure(cause);
     }
 
+    void finishCommitPending() {
+        checkState(State.COMMIT_PENDING);
+        // We want to switch the state but keep the callback.
+        callback = switchState(State.FINISH_COMMIT_PENDING);
+    }
+
     @Override
     public State getState() {
         return state;