f0eebe331ed203c97594f9f503cd5d20f0cf30ce
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBrokerWriteTransaction.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.databroker;
10
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;
17 import java.util.Map;
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;
34
35 public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
36         extends AbstractDOMBrokerTransaction<T> implements DOMDataWriteTransaction {
37
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();
46
47     /**
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}.
51      */
52     private volatile AbstractDOMTransactionFactory<?> commitImpl;
53
54     /**
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)}.
58      *
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.
63      */
64     private volatile Future<?> commitFuture;
65
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.");
70     }
71
72     @Override
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);
77     }
78
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);
85     }
86
87     @Override
88     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
89         checkRunning(commitImpl);
90         getSubtransaction(store).delete(path);
91     }
92
93     @Override
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);
98     }
99
100     @Override
101     public boolean cancel() {
102         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
103         if (impl != null) {
104             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
105             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
106             closeSubtransactions();
107             return true;
108         }
109
110         // The transaction is in process of being submitted or cancelled. Busy-wait
111         // for the corresponding future.
112         Future<?> future;
113         do {
114             future = commitFuture;
115         } while (future == null);
116
117         return future.cancel(false);
118     }
119
120     @Deprecated
121     @Override
122     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
123         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
124     }
125
126     @Override
127     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
128         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
129         checkRunning(impl);
130
131         final Collection<T> txns = getSubtransactions();
132         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
133
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());
137         }
138
139         final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
140         FUTURE_UPDATER.lazySet(this, ret);
141         return ret;
142     }
143
144     private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
145         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
146     }
147
148 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.