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