Add default implementation for AsyncWriteTransaction#commit
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / 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.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
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.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Read-Write Transaction, which is composed of several
30  * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
31  * {@link LogicalDatastoreType} type parameter in:
32  *
33  * <p>
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 #commit()} will result in invocation of
42  * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction,
43  * Iterable)} invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort}
44  * for underlying transactions.
45  *
46  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
47  *            subtransaction.
48  */
49 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
50         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
51     @SuppressWarnings("rawtypes")
52     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
53             AbstractDOMForwardedTransactionFactory>
54             IMPL_UPDATER = AtomicReferenceFieldUpdater
55             .newUpdater(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, final Map<LogicalDatastoreType, T> backingTxs,
83                                            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 CheckedFuture<Void, TransactionCommitFailedException> submit() {
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         CheckedFuture<Void, TransactionCommitFailedException> ret;
138         try {
139             for (DOMStoreWriteTransaction txn : txns) {
140                 cohorts.add(txn.ready());
141             }
142
143             ret = impl.submit(this, cohorts);
144         } catch (RuntimeException e) {
145             ret = Futures.immediateFailedCheckedFuture(
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 }