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.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
20 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
36 extends AbstractDOMBrokerTransaction<T> implements DOMDataWriteTransaction {
38 @SuppressWarnings("rawtypes")
39 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, AbstractDOMTransactionFactory> IMPL_UPDATER =
40 AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, AbstractDOMTransactionFactory.class, "commitImpl");
41 @SuppressWarnings("rawtypes")
42 private static final AtomicReferenceFieldUpdater<AbstractDOMBrokerWriteTransaction, Future> FUTURE_UPDATER =
43 AtomicReferenceFieldUpdater.newUpdater(AbstractDOMBrokerWriteTransaction.class, Future.class, "commitFuture");
44 private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBrokerWriteTransaction.class);
45 private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
48 * Implementation of real commit. It also acts as an indication that
49 * the transaction is running -- which we flip atomically using
50 * {@link #IMPL_UPDATER}.
52 private volatile AbstractDOMTransactionFactory<?> commitImpl;
55 * Future task of transaction commit. It starts off as null, but is
56 * set appropriately on {@link #submit()} and {@link #cancel()} via
57 * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
59 * Lazy set is safe for use because it is only referenced to in the
60 * {@link #cancel()} slow path, where we will busy-wait for it. The
61 * fast path gets the benefit of a store-store barrier instead of the
62 * usual store-load barrier.
64 private volatile Future<?> commitFuture;
66 protected AbstractDOMBrokerWriteTransaction(final Object identifier,
67 final Map<LogicalDatastoreType, ? extends DOMStoreTransactionFactory> storeTxFactories, final AbstractDOMTransactionFactory<?> commitImpl) {
68 super(identifier, storeTxFactories);
69 this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
73 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
74 checkRunning(commitImpl);
75 checkInstanceIdentifierReferencesData(path,data);
76 getSubtransaction(store).write(path, data);
79 private static void checkInstanceIdentifierReferencesData(final YangInstanceIdentifier path,
80 final NormalizedNode<?, ?> data) {
81 final PathArgument lastArg = path.getLastPathArgument();
82 Preconditions.checkArgument(
83 (lastArg == data.getIdentifier()) || (lastArg != null && lastArg.equals(data.getIdentifier())),
84 "Instance identifier references %s but data identifier is %s", lastArg, data);
88 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
89 checkRunning(commitImpl);
90 getSubtransaction(store).delete(path);
94 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
95 checkRunning(commitImpl);
96 checkInstanceIdentifierReferencesData(path, data);
97 getSubtransaction(store).merge(path, data);
101 public boolean cancel() {
102 final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
104 LOG.trace("Transaction {} cancelled before submit", getIdentifier());
105 FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
106 closeSubtransactions();
110 // The transaction is in process of being submitted or cancelled. Busy-wait
111 // for the corresponding future.
114 future = commitFuture;
115 } while (future == null);
117 return future.cancel(false);
122 public ListenableFuture<RpcResult<TransactionStatus>> commit() {
123 return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
127 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
128 final AbstractDOMTransactionFactory<?> 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 T 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 AbstractDOMTransactionFactory<?> impl) {
145 Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());