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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17 import java.util.function.Function;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.UncancellableListenableFuture;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Read-Write Transaction, which is composed of several {@link DOMStoreWriteTransaction} transactions. A sub-transaction
32 * is selected by {@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)}
42 * invocation with all {@link org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort} for underlying
45 * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as subtransaction.
47 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction>
48 extends AbstractDOMForwardedTransaction<T> implements DOMDataTreeWriteTransaction {
49 @SuppressWarnings("rawtypes")
50 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
51 AbstractDOMForwardedTransactionFactory> IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(
52 DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
53 @SuppressWarnings("rawtypes")
54 private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, FluentFuture> FUTURE_UPDATER =
55 AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, FluentFuture.class,
57 private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
59 private final @NonNull SettableFuture<@NonNull CommitInfo> settableCompletion = SettableFuture.create();
60 private final @NonNull FluentFuture<? extends @NonNull CommitInfo> completionFuture = FluentFuture.from(
61 new UncancellableListenableFuture<>(settableCompletion));
64 * Implementation of real commit. It also acts as an indication that the transaction is running -- which we flip
65 * atomically using {@link #IMPL_UPDATER}.
67 private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
70 * Future task of transaction commit. It starts off as null, but is set appropriately on {@link #submit()} and
71 * {@link #cancel()} via {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
73 * Lazy set is safe for use because it is only referenced to in the {@link #cancel()} slow path, where we will
74 * busy-wait for it. The fast path gets the benefit of a store-store barrier instead of the usual store-load
77 private volatile FluentFuture<?> commitFuture;
79 protected DOMForwardedWriteTransaction(final Object identifier,
80 final Function<LogicalDatastoreType, T> backingTxFactory,
81 final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
82 super(identifier, backingTxFactory);
83 this.commitImpl = requireNonNull(commitImpl, "commitImpl must not be null.");
87 public final void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
88 final NormalizedNode data) {
89 checkRunning(commitImpl);
90 getSubtransaction(store).write(path, data);
94 public final void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
95 checkRunning(commitImpl);
96 getSubtransaction(store).delete(path);
100 public final void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
101 final NormalizedNode data) {
102 checkRunning(commitImpl);
103 getSubtransaction(store).merge(path, data);
107 public final boolean cancel() {
108 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
110 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
111 FUTURE_UPDATER.lazySet(this, FluentFutures.immediateCancelledFluentFuture());
112 closeSubtransactions();
116 // The transaction is in process of being submitted or cancelled. Busy-wait for the corresponding future.
117 FluentFuture<?> future;
119 future = commitFuture;
120 } while (future == null);
122 return future.isCancelled();
126 @SuppressWarnings("checkstyle:IllegalCatch")
127 public final FluentFuture<? extends CommitInfo> commit() {
128 final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
131 FluentFuture<? extends CommitInfo> ret;
132 final var tx = getSubtransaction();
134 ret = CommitInfo.emptyFluentFuture();
137 ret = impl.commit(this, tx.ready());
138 } catch (RuntimeException e) {
139 ret = immediateFailedFluentFuture(TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
143 settableCompletion.setFuture(ret);
144 FUTURE_UPDATER.lazySet(this, ret);
145 return completionFuture;
149 public final FluentFuture<?> completionFuture() {
150 return completionFuture;
153 private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
154 checkState(impl != null, "Transaction %s is no longer running", getIdentifier());