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