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.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
20 import java.util.concurrent.atomic.AtomicLong;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.mdsal.common.api.CommitInfo;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
29 import org.opendaylight.yangtools.yang.common.Empty;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * NormalizedNode implementation of {@link org.opendaylight.mdsal.common.api.TransactionChain} which is backed
35 * by several {@link DOMStoreTransactionChain} differentiated by provided
36 * {@link org.opendaylight.mdsal.common.api.LogicalDatastoreType} type.
39 final class DOMDataBrokerTransactionChainImpl extends AbstractDOMForwardedTransactionFactory<DOMStoreTransactionChain>
40 implements DOMTransactionChain {
48 private static final AtomicIntegerFieldUpdater<DOMDataBrokerTransactionChainImpl> COUNTER_UPDATER =
49 AtomicIntegerFieldUpdater.newUpdater(DOMDataBrokerTransactionChainImpl.class, "counter");
50 private static final AtomicReferenceFieldUpdater<DOMDataBrokerTransactionChainImpl, State> STATE_UPDATER =
51 AtomicReferenceFieldUpdater.newUpdater(DOMDataBrokerTransactionChainImpl.class, State.class, "state");
52 private static final Logger LOG = LoggerFactory.getLogger(DOMDataBrokerTransactionChainImpl.class);
54 private final @NonNull SettableFuture<Empty> future = SettableFuture.create();
55 private final AtomicLong txNum = new AtomicLong();
56 private final AbstractDOMDataBroker broker;
57 private final long chainId;
59 private volatile State state = State.RUNNING;
60 private volatile int counter = 0;
62 DOMDataBrokerTransactionChainImpl(final long chainId,
63 final Map<LogicalDatastoreType, DOMStoreTransactionChain> chains, final AbstractDOMDataBroker broker) {
65 this.chainId = chainId;
66 this.broker = requireNonNull(broker);
69 private void checkNotFailed() {
70 Preconditions.checkState(state != State.FAILED, "Transaction chain has failed");
74 public ListenableFuture<Empty> future() {
79 protected Object newTransactionIdentifier() {
80 return "DOM-CHAIN-" + chainId + "-" + txNum.getAndIncrement();
84 protected FluentFuture<? extends CommitInfo> commit(final DOMDataTreeWriteTransaction transaction,
85 final DOMStoreThreePhaseCommitCohort cohort) {
89 final var ret = broker.commit(transaction, cohort);
90 COUNTER_UPDATER.incrementAndGet(this);
92 ret.addCallback(new FutureCallback<CommitInfo>() {
94 public void onSuccess(final CommitInfo result) {
95 transactionCompleted();
99 public void onFailure(final Throwable throwable) {
100 transactionFailed(transaction, throwable);
102 }, MoreExecutors.directExecutor());
108 public void close() {
109 final boolean success = STATE_UPDATER.compareAndSet(this, State.RUNNING, State.CLOSING);
111 LOG.debug("Chain {} is no longer running", this);
116 for (DOMStoreTransactionChain subChain : getTxFactories().values()) {
125 private void finishClose() {
126 state = State.CLOSED;
127 future.set(Empty.value());
130 private void transactionCompleted() {
131 if (COUNTER_UPDATER.decrementAndGet(this) == 0 && state == State.CLOSING) {
136 private void transactionFailed(final DOMDataTreeWriteTransaction tx, final Throwable cause) {
137 state = State.FAILED;
138 LOG.debug("Transaction chain {} failed.", this, cause);
139 future.setException(cause);