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