Expose completion future from WriteOperations
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMForwardedWriteTransaction.java
index c74bce66fb070788eca01fa2834c631bbff8de95..70b1e4043fb0e142eda63629144a6da4675838ca 100644 (file)
@@ -7,19 +7,19 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
+
 import com.google.common.util.concurrent.FluentFuture;
-import com.google.common.util.concurrent.Futures;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.Future;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
-import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.mdsal.dom.spi.UncancellableListenableFuture;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -28,10 +28,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Read-Write Transaction, which is composed of several
- * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
- * {@link LogicalDatastoreType} type parameter in:
- *
+ * Read-Write Transaction, which is composed of several {@link DOMStoreWriteTransaction} transactions. A sub-transaction
+ * is selected by {@link LogicalDatastoreType} type parameter in:
  * <ul>
  * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
  * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
@@ -41,115 +39,118 @@ import org.slf4j.LoggerFactory;
  * <p>
  * {@link #submit()} will result in invocation of
  * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
- * invocation with all
- * {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for
- * underlying transactions.
+ * invocation with all {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for underlying
+ * transactions.
  *
  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
  */
-class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
-        AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataTreeWriteTransaction {
+class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction>
+        extends AbstractDOMForwardedTransaction<T> implements DOMDataTreeWriteTransaction {
     @SuppressWarnings("rawtypes")
     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
         AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
                 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
     @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
+    private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, FluentFuture> FUTURE_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, FluentFuture.class,
+                "commitFuture");
     private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
-    private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
 
-    /**
-     * Implementation of real commit. It also acts as an indication that
-     * the transaction is running -- which we flip atomically using
-     * {@link #IMPL_UPDATER}.
+    private final @NonNull SettableFuture<@NonNull CommitInfo> settableCompletion = SettableFuture.create();
+    private final @NonNull FluentFuture<? extends @NonNull CommitInfo> completionFuture = FluentFuture.from(
+        new UncancellableListenableFuture<>(settableCompletion));
+
+    /*
+     * Implementation of real commit. It also acts as an indication that the transaction is running -- which we flip
+     * atomically using {@link #IMPL_UPDATER}.
      */
     private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
 
-    /**
-     * Future task of transaction commit. It starts off as null, but is
-     * set appropriately on {@link #submit()} and {@link #cancel()} via
-     * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
+    /*
+     * Future task of transaction commit. It starts off as null, but is set appropriately on {@link #submit()} and
+     * {@link #cancel()} via {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
      *
-     *<p>
-     * Lazy set is safe for use because it is only referenced to in the
-     * {@link #cancel()} slow path, where we will busy-wait for it. The
-     * fast path gets the benefit of a store-store barrier instead of the
-     * usual store-load barrier.
+     * Lazy set is safe for use because it is only referenced to in the {@link #cancel()} slow path, where we will
+     * busy-wait for it. The fast path gets the benefit of a store-store barrier instead of the usual store-load
+     * barrier.
      */
-    private volatile Future<?> commitFuture;
+    private volatile FluentFuture<?> commitFuture;
 
     protected DOMForwardedWriteTransaction(final Object identifier,
-            final Map<LogicalDatastoreType, T> backingTxs, final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
-        super(identifier, backingTxs);
-        this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
+            final Function<LogicalDatastoreType, T> backingTxFactory,
+            final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
+        super(identifier, backingTxFactory);
+        this.commitImpl = requireNonNull(commitImpl, "commitImpl must not be null.");
     }
 
     @Override
-    public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
-            final NormalizedNode<?, ?> data) {
+    public final void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
+            final NormalizedNode data) {
         checkRunning(commitImpl);
         getSubtransaction(store).write(path, data);
     }
 
     @Override
-    public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
+    public final void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         checkRunning(commitImpl);
         getSubtransaction(store).delete(path);
     }
 
     @Override
-    public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
-            final NormalizedNode<?, ?> data) {
+    public final void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
+            final NormalizedNode data) {
         checkRunning(commitImpl);
         getSubtransaction(store).merge(path, data);
     }
 
     @Override
-    public boolean cancel() {
+    public final boolean cancel() {
         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
         if (impl != null) {
             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
-            FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
+            FUTURE_UPDATER.lazySet(this, FluentFutures.immediateCancelledFluentFuture());
             closeSubtransactions();
             return true;
         }
 
-        // The transaction is in process of being submitted or cancelled. Busy-wait
-        // for the corresponding future.
-        Future<?> future;
+        // The transaction is in process of being submitted or cancelled. Busy-wait for the corresponding future.
+        FluentFuture<?> future;
         do {
             future = commitFuture;
         } while (future == null);
 
-        return future.cancel(false);
+        return future.isCancelled();
     }
 
     @Override
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
+    public final FluentFuture<? extends CommitInfo> commit() {
         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
         checkRunning(impl);
 
-        final Collection<T> txns = getSubtransactions();
-        final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
-
         FluentFuture<? extends CommitInfo> ret;
-        try {
-            for (final DOMStoreWriteTransaction txn : txns) {
-                cohorts.add(txn.ready());
+        final var tx = getSubtransaction();
+        if (tx == null) {
+            ret = CommitInfo.emptyFluentFuture();
+        } else {
+            try {
+                ret = impl.commit(this, tx.ready());
+            } catch (RuntimeException e) {
+                ret = immediateFailedFluentFuture(TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
             }
-
-            ret = impl.commit(this, cohorts);
-        } catch (RuntimeException e) {
-            ret = FluentFutures.immediateFailedFluentFuture(
-                    TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
         }
+
+        settableCompletion.setFuture(ret);
         FUTURE_UPDATER.lazySet(this, ret);
-        return ret;
+        return completionFuture;
+    }
+
+    @Override
+    public final FluentFuture<?> completionFuture() {
+        return completionFuture;
     }
 
     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
-        Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
+        checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
     }
 }