BUG-8618: eliminate SimpleShardDataTreeCohort subclasses
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / SimpleShardDataTreeCohort.java
index 7da174fec583bd1f2ac998383fc8f3677d019712..cf9948cf03e0407f47103e4e2085d33a58d0346d 100644 (file)
@@ -8,14 +8,14 @@
 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;
@@ -29,7 +29,7 @@ import scala.concurrent.Future;
 
 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;
@@ -48,6 +48,15 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
         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;
@@ -59,7 +68,6 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
     }
 
     @Override
-
     DataTreeModification getDataTreeModification() {
         return transaction;
     }
@@ -77,7 +85,12 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
         checkState(State.READY);
         this.callback = Preconditions.checkNotNull(newCallback);
         state = State.CAN_COMMIT_PENDING;
-        dataTree.startCanCommit(this);
+
+        if (nextFailure == null) {
+            dataTree.startCanCommit(this);
+        } else {
+            failedCanCommit(nextFailure);
+        }
     }
 
     @Override
@@ -94,33 +107,37 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
     }
 
     @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();
+        final Optional<List<Future<Object>>> maybeAborts = userCohorts.abort();
         if (!maybeAborts.isPresent()) {
-            return VOID_FUTURE;
+            abortCallback.onSuccess(null);
+            return;
         }
 
-        final Future<Iterable<Object>> aborts = maybeAborts.get();
+        final Future<Iterable<Object>> aborts = Futures.sequence(maybeAborts.get(), ExecutionContexts.global());
         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
@@ -128,7 +145,12 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
         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) {
@@ -140,6 +162,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);
     }
@@ -158,6 +185,7 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
      */
     // FIXME: this should be asynchronous
     void userPreCommit(final DataTreeCandidate dataTreeCandidate) throws ExecutionException, TimeoutException {
+        userCohorts.reset();
         userCohorts.canCommit(dataTreeCandidate);
         userCohorts.preCommit();
     }
@@ -207,11 +235,20 @@ final class SimpleShardDataTreeCohort extends ShardDataTreeCohort {
     }
 
     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);
+    }
 }