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.controller.md.sal.dom.broker.impl;
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.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;
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:
35 * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
36 * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
37 * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
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.
46 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
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();
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}.
67 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
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)}.
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.
80 private volatile Future<?> commitFuture;
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.");
89 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
90 final NormalizedNode<?, ?> data) {
91 checkRunning(commitImpl);
92 getSubtransaction(store).write(path, data);
96 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97 checkRunning(commitImpl);
98 getSubtransaction(store).delete(path);
102 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
103 final NormalizedNode<?, ?> data) {
104 checkRunning(commitImpl);
105 getSubtransaction(store).merge(path, data);
109 public boolean cancel() {
110 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
112 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
113 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
114 closeSubtransactions();
118 // The transaction is in process of being submitted or cancelled. Busy-wait
119 // for the corresponding future.
122 future = commitFuture;
123 } while (future == null);
125 return future.cancel(false);
129 @SuppressWarnings("checkstyle:illegalcatch")
130 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
131 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
134 final Collection<T> txns = getSubtransactions();
135 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
137 CheckedFuture<Void, TransactionCommitFailedException> ret;
139 for (DOMStoreWriteTransaction txn : txns) {
140 cohorts.add(txn.ready());
143 ret = impl.submit(this, cohorts);
144 } catch (RuntimeException e) {
145 ret = Futures.immediateFailedCheckedFuture(
146 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
148 FUTURE_UPDATER.lazySet(this, ret);
152 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
153 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());