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