2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.databroker;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Collection;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
26 import org.opendaylight.controller.md.sal.dom.broker.impl.TransactionCommitFailedExceptionMapper;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
38 extends AbstractDOMBrokerTransaction<T> implements DOMDataWriteTransaction {
40 @SuppressWarnings("rawtypes")
41 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, AbstractDOMTransactionFactory>
42 IMPL_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class,
43 AbstractDOMTransactionFactory.class, "commitImpl");
44 @SuppressWarnings("rawtypes")
45 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, Future> FUTURE_UPDATER =
46 AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, Future.class,
48 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBrokerWriteTransaction.class);
49 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
52 * Implementation of real commit. It also acts as an indication that
53 * the transaction is running -- which we flip atomically using
54 * {@link #IMPL_UPDATER}.
56 private volatile AbstractDOMTransactionFactory<?> commitImpl;
59 * Future task of transaction commit. It starts off as null, but is
60 * set appropriately on {@link #submit()} and {@link #cancel()} via
61 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
63 * Lazy set is safe for use because it is only referenced to in the
64 * {@link #cancel()} slow path, where we will busy-wait for it. The
65 * fast path gets the benefit of a store-store barrier instead of the
66 * usual store-load barrier.
68 private volatile Future<?> commitFuture;
70 protected AbstractDOMBrokerWriteTransaction(final Object identifier,
71 final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories,
72 final AbstractDOMTransactionFactory<?> commitImpl) {
73 super(identifier, storeTxFactories);
74 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
78 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
79 final NormalizedNode<?, ?> data) {
80 checkRunning(commitImpl);
81 checkInstanceIdentifierReferencesData(path,data);
82 getSubtransaction(store).write(path, data);
85 private static void checkInstanceIdentifierReferencesData(final YangInstanceIdentifier path,
86 final NormalizedNode<?, ?> data) {
87 Preconditions.checkArgument(data != null, "Attempted to store null data at %s", path);
88 final PathArgument lastArg = path.getLastPathArgument();
89 Preconditions.checkArgument(
90 lastArg == data.getIdentifier() || lastArg != null && lastArg.equals(data.getIdentifier()),
91 "Instance identifier references %s but data identifier is %s", lastArg, data);
95 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
96 checkRunning(commitImpl);
97 getSubtransaction(store).delete(path);
101 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
102 final NormalizedNode<?, ?> data) {
103 checkRunning(commitImpl);
104 checkInstanceIdentifierReferencesData(path, data);
105 getSubtransaction(store).merge(path, data);
109 public boolean cancel() {
110 final AbstractDOMTransactionFactory<?> 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;
124 while (future == null);
126 return future.cancel(false);
131 public ListenableFuture<RpcResult<TransactionStatus>> commit() {
132 return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
136 @SuppressWarnings("checkstyle:illegalcatch")
137 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
138 final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
141 final Collection<T> txns = getSubtransactions();
142 final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
144 CheckedFuture<Void, TransactionCommitFailedException> ret;
146 for (final T txn : txns) {
147 cohorts.add(txn.ready());
150 ret = impl.submit(this, cohorts);
151 } catch (RuntimeException e) {
152 ret = Futures.immediateFailedCheckedFuture(
153 TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
155 FUTURE_UPDATER.lazySet(this, ret);
159 private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
160 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
164 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
165 return super.addToStringAttributes(toStringHelper).add("running", commitImpl == null);