bc11f122e63e4fd1c79435d5e6d2ba8460ee8a3e
[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 com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Read-Write Transaction, which is composed of several
34  * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
35  * {@link LogicalDatastoreType} type parameter in:
36  *
37  * <ul>
38  * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
39  * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
40  * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
41  * </ul>
42  * <p>
43  * {@link #commit()} will result in invocation of
44  * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction, Iterable)}
45  * invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort} for underlying
46  * transactions.
47  *
48  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
49  *            subtransaction.
50  */
51 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
52         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
53     @SuppressWarnings("rawtypes")
54     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, AbstractDOMForwardedTransactionFactory> IMPL_UPDATER =
55             AtomicReferenceFieldUpdater.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      * 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, final NormalizedNode<?, ?> data) {
89         checkRunning(commitImpl);
90         getSubtransaction(store).write(path, data);
91     }
92
93     @Override
94     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
95         checkRunning(commitImpl);
96         getSubtransaction(store).delete(path);
97     }
98
99     @Override
100     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
101         checkRunning(commitImpl);
102         getSubtransaction(store).merge(path, data);
103     }
104
105     @Override
106     public boolean cancel() {
107         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
108         if (impl != null) {
109             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
110             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
111             closeSubtransactions();
112             return true;
113         }
114
115         // The transaction is in process of being submitted or cancelled. Busy-wait
116         // for the corresponding future.
117         Future<?> future;
118         do {
119             future = commitFuture;
120         } while (future == null);
121
122         return future.cancel(false);
123     }
124
125     @Deprecated
126     @Override
127     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
128         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
129     }
130
131     @Override
132     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
133         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
134         checkRunning(impl);
135
136         final Collection<T> txns = getSubtransactions();
137         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
138
139         // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
140         for (DOMStoreWriteTransaction txn : txns) {
141             cohorts.add(txn.ready());
142         }
143
144         final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
145         FUTURE_UPDATER.lazySet(this, ret);
146         return ret;
147     }
148
149     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
150         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
151     }
152 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.