2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
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;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.mdsal.dom.spi.store.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;
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:
34 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
35 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
36 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
40 * {@link #submit()} will result in invocation of
41 * {@link DOMDataCommitImplementation#submit(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction, Iterable)}
43 * {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for
44 * underlying transactions.
46 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
48 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
49 AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataTreeWriteTransaction {
50 @SuppressWarnings("rawtypes")
51 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
52 AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
53 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
54 @SuppressWarnings("rawtypes")
55 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER =
56 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
57 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
58 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
61 * Implementation of real commit. It also acts as an indication that
62 * the transaction is running -- which we flip atomically using
63 * {@link #IMPL_UPDATER}.
65 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
68 * Future task of transaction commit. It starts off as null, but is
69 * set appropriately on {@link #submit()} and {@link #cancel()} via
70 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
73 * Lazy set is safe for use because it is only referenced to in the
74 * {@link #cancel()} slow path, where we will busy-wait for it. The
75 * fast path gets the benefit of a store-store barrier instead of the
76 * usual store-load barrier.
78 private volatile Future<?> commitFuture;
80 protected DOMForwardedWriteTransaction(final Object identifier,
81 final Map<LogicalDatastoreType, T> backingTxs, final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
82 super(identifier, backingTxs);
83 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
87 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
88 final NormalizedNode<?, ?> data) {
89 checkRunning(commitImpl);
90 getSubtransaction(store).write(path, data);
94 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
95 checkRunning(commitImpl);
96 getSubtransaction(store).delete(path);
100 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
101 final NormalizedNode<?, ?> data) {
102 checkRunning(commitImpl);
103 getSubtransaction(store).merge(path, data);
107 public boolean cancel() {
108 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
110 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
111 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
112 closeSubtransactions();
116 // The transaction is in process of being submitted or cancelled. Busy-wait
117 // for the corresponding future.
120 future = commitFuture;
121 } while (future == null);
123 return future.cancel(false);
127 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
128 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
131 final Collection<T> txns = getSubtransactions();
132 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
134 // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
135 for (final DOMStoreWriteTransaction txn : txns) {
136 cohorts.add(txn.ready());
139 final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
140 FUTURE_UPDATER.lazySet(this, ret);
144 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
145 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());