Add AbstractDOMBrokerTransaction.toString()
[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.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;
18 import java.util.Map;
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;
36
37 public abstract class AbstractDOMBrokerWriteTransaction<T extends DOMStoreWriteTransaction>
38         extends AbstractDOMBrokerTransaction<T> implements DOMDataWriteTransaction {
39
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,
47                     "commitFuture");
48     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMBrokerWriteTransaction.class);
49     private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
50
51     /**
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}.
55      */
56     private volatile AbstractDOMTransactionFactory<?> commitImpl;
57
58     /**
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)}.
62      * <p/>
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.
67      */
68     private volatile Future<?> commitFuture;
69
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.");
75     }
76
77     @Override
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);
83     }
84
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);
92     }
93
94     @Override
95     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
96         checkRunning(commitImpl);
97         getSubtransaction(store).delete(path);
98     }
99
100     @Override
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);
106     }
107
108     @Override
109     public boolean cancel() {
110         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
111         if (impl != null) {
112             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
113             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
114             closeSubtransactions();
115             return true;
116         }
117
118         // The transaction is in process of being submitted or cancelled. Busy-wait
119         // for the corresponding future.
120         Future<?> future;
121         do {
122             future = commitFuture;
123         }
124         while (future == null);
125
126         return future.cancel(false);
127     }
128
129     @Deprecated
130     @Override
131     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
132         return AbstractDataTransaction.convertToLegacyCommitFuture(submit());
133     }
134
135     @Override
136     @SuppressWarnings("checkstyle:illegalcatch")
137     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
138         final AbstractDOMTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
139         checkRunning(impl);
140
141         final Collection<T> txns = getSubtransactions();
142         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
143
144         CheckedFuture<Void, TransactionCommitFailedException> ret;
145         try {
146             for (final T txn : txns) {
147                 cohorts.add(txn.ready());
148             }
149
150             ret = impl.submit(this, cohorts);
151         } catch (RuntimeException e) {
152             ret = Futures.immediateFailedCheckedFuture(
153                     TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e));
154         }
155         FUTURE_UPDATER.lazySet(this, ret);
156         return ret;
157     }
158
159     private void checkRunning(final AbstractDOMTransactionFactory<?> impl) {
160         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
161     }
162
163     @Override
164     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
165         return super.addToStringAttributes(toStringHelper).add("running", commitImpl == null);
166     }
167 }

©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.