Teach AbstractDOMDataBroker about commit cohorts
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / DOMForwardedWriteTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.dom.spi;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
13
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import java.util.function.Function;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
23 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Read-Write Transaction, which is composed of several {@link DOMStoreWriteTransaction} transactions. A sub-transaction
31  * is selected by {@link LogicalDatastoreType} type parameter in:
32  * <ul>
33  * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
34  * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
35  * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
36  * </ul>
37  *
38  * <p>
39  * {@link #submit()} will result in invocation of
40  * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
41  * invocation with all {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for underlying
42  * transactions.
43  *
44  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
45  */
46 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction>
47         extends AbstractDOMForwardedTransaction<T> implements DOMDataTreeWriteTransaction {
48     @SuppressWarnings("rawtypes")
49     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
50         AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
51                 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
52     @SuppressWarnings("rawtypes")
53     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, FluentFuture> FUTURE_UPDATER =
54             AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, FluentFuture.class,
55                 "commitFuture");
56     private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
57
58     private final @NonNull SettableFuture<@NonNull CommitInfo> settableCompletion = SettableFuture.create();
59     private final @NonNull FluentFuture<? extends @NonNull CommitInfo> completionFuture = FluentFuture.from(
60         new UncancellableListenableFuture<>(settableCompletion));
61
62     /*
63      * Implementation of real commit. It also acts as an indication that the transaction is running -- which we flip
64      * atomically using {@link #IMPL_UPDATER}.
65      */
66     private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
67
68     /*
69      * Future task of transaction commit. It starts off as null, but is set appropriately on {@link #submit()} and
70      * {@link #cancel()} via {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
71      *
72      * Lazy set is safe for use because it is only referenced to in the {@link #cancel()} slow path, where we will
73      * busy-wait for it. The fast path gets the benefit of a store-store barrier instead of the usual store-load
74      * barrier.
75      */
76     private volatile FluentFuture<?> commitFuture;
77
78     protected DOMForwardedWriteTransaction(final Object identifier,
79             final Function<LogicalDatastoreType, T> backingTxFactory,
80             final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
81         super(identifier, backingTxFactory);
82         this.commitImpl = requireNonNull(commitImpl, "commitImpl must not be null.");
83     }
84
85     @Override
86     public final void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
87             final NormalizedNode data) {
88         checkRunning(commitImpl);
89         getSubtransaction(store).write(path, data);
90     }
91
92     @Override
93     public final void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
94         checkRunning(commitImpl);
95         getSubtransaction(store).delete(path);
96     }
97
98     @Override
99     public final void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
100             final NormalizedNode data) {
101         checkRunning(commitImpl);
102         getSubtransaction(store).merge(path, data);
103     }
104
105     @Override
106     public final boolean cancel() {
107         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
108         if (impl != null) {
109             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
110             FUTURE_UPDATER.lazySet(this, FluentFutures.immediateCancelledFluentFuture());
111             closeSubtransactions();
112             return true;
113         }
114
115         // The transaction is in process of being submitted or cancelled. Busy-wait for the corresponding future.
116         FluentFuture<?> future;
117         do {
118             future = commitFuture;
119         } while (future == null);
120
121         return future.isCancelled();
122     }
123
124     @Override
125     @SuppressWarnings("checkstyle:IllegalCatch")
126     public final FluentFuture<? extends CommitInfo> commit() {
127         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
128         checkRunning(impl);
129
130         FluentFuture<? extends CommitInfo> ret;
131         final var tx = getSubtransaction();
132         if (tx == null) {
133             ret = CommitInfo.emptyFluentFuture();
134         } else {
135             try {
136                 ret = impl.commit(this, tx.ready());
137             } catch (RuntimeException e) {
138                 ret = immediateFailedFluentFuture(TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
139             }
140         }
141
142         settableCompletion.setFuture(ret);
143         FUTURE_UPDATER.lazySet(this, ret);
144         return completionFuture;
145     }
146
147     @Override
148     public final FluentFuture<?> completionFuture() {
149         return completionFuture;
150     }
151
152     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
153         checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
154     }
155 }