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
8 package org.opendaylight.mdsal.dom.broker;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.util.ArrayDeque;
21 import java.util.Deque;
23 import java.util.Map.Entry;
24 import java.util.concurrent.atomic.AtomicLong;
25 import java.util.function.BiConsumer;
26 import java.util.function.Consumer;
27 import java.util.stream.Collectors;
28 import javax.annotation.concurrent.GuardedBy;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.opendaylight.mdsal.common.api.CommitInfo;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
34 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardWriteTransaction;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAwareTransaction {
41 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeWriteTransaction.class);
42 private static final AtomicLong COUNTER = new AtomicLong();
44 private final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> transactions;
45 private final ShardedDOMDataTreeProducer producer;
46 private final ProducerLayout layout;
47 private final String identifier;
49 private final SettableFuture<CommitInfo> future = SettableFuture.create();
52 private boolean closed = false;
55 private DOMDataTreeWriteCursor openCursor;
57 ShardedDOMDataTreeWriteTransaction(final ShardedDOMDataTreeProducer producer,
58 final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> transactions, final ProducerLayout layout) {
59 this.producer = requireNonNull(producer);
60 this.transactions = ImmutableMap.copyOf(transactions);
61 this.layout = requireNonNull(layout);
62 this.identifier = "SHARDED-DOM-" + COUNTER.getAndIncrement();
63 LOG.debug("Created new transaction {}", identifier);
66 private DOMDataTreeShardWriteTransaction lookup(final DOMDataTreeIdentifier prefix) {
67 final DOMDataTreeShardWriteTransaction fast = transactions.get(prefix);
72 LOG.debug("Prefix {} not found in available subtrees {}, fallback to slow path", prefix, transactions.keySet());
73 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> e : transactions.entrySet()) {
74 if (e.getKey().contains(prefix)) {
83 public String getIdentifier() {
88 public synchronized boolean cancel() {
93 LOG.debug("Cancelling transaction {}", identifier);
94 if (openCursor != null) {
97 for (final DOMDataTreeShardWriteTransaction tx : transactions.values()) {
102 producer.cancelTransaction(this);
107 public synchronized DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
108 Preconditions.checkState(!closed, "Transaction is closed already");
109 Preconditions.checkState(openCursor == null, "There is still a cursor open");
110 Preconditions.checkArgument(!producer.isDelegatedToChild(prefix), "Path %s is delegated to child producer.",
113 final DOMDataTreeShardWriteTransaction lookup = lookup(prefix);
114 Preconditions.checkArgument(lookup != null, "Path %s is not accessible from transaction %s", prefix, this);
116 openCursor = new DelegatingCursor(lookup.createCursor(prefix), prefix);
121 public synchronized FluentFuture<? extends @NonNull CommitInfo> commit() {
122 Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
123 Preconditions.checkState(openCursor == null, "Cannot submit transaction while there is a cursor open");
125 producer.transactionSubmitted(this);
129 void doSubmit(final Consumer<ShardedDOMDataTreeWriteTransaction> success,
130 final BiConsumer<ShardedDOMDataTreeWriteTransaction, Throwable> failure) {
131 LOG.debug("Readying tx {}", identifier);
133 final ListenableFuture<?> internalFuture;
134 switch (transactions.size()) {
136 success.accept(this);
139 final DOMDataTreeShardWriteTransaction tx = transactions.values().iterator().next();
141 internalFuture = tx.submit();
145 internalFuture = Futures.allAsList(transactions.values().stream().map(tx -> {
148 }).collect(Collectors.toList()));
151 Futures.addCallback(internalFuture, new FutureCallback<Object>() {
153 public void onSuccess(final Object result) {
154 success.accept(ShardedDOMDataTreeWriteTransaction.this);
158 public void onFailure(final Throwable exp) {
159 failure.accept(ShardedDOMDataTreeWriteTransaction.this, exp);
161 }, MoreExecutors.directExecutor());
164 void onTransactionSuccess(final CommitInfo commitInfo) {
165 future.set(commitInfo);
168 void onTransactionFailure(final Throwable throwable) {
169 future.setException(throwable);
172 synchronized void cursorClosed() {
176 private class DelegatingCursor implements DOMDataTreeWriteCursor {
177 private final Deque<PathArgument> currentArgs = new ArrayDeque<>();
178 private final DOMDataTreeWriteCursor delegate;
179 private final DOMDataTreeIdentifier rootPosition;
181 DelegatingCursor(final DOMDataTreeWriteCursor delegate, final DOMDataTreeIdentifier rootPosition) {
182 this.delegate = requireNonNull(delegate);
183 this.rootPosition = requireNonNull(rootPosition);
184 currentArgs.addAll(rootPosition.getRootIdentifier().getPathArguments());
188 public void enter(final PathArgument child) {
189 checkAvailable(child);
190 delegate.enter(child);
191 currentArgs.push(child);
195 public void enter(final PathArgument... path) {
196 for (final PathArgument pathArgument : path) {
202 public void enter(final Iterable<PathArgument> path) {
203 for (final PathArgument pathArgument : path) {
215 public void exit(final int depth) {
216 delegate.exit(depth);
217 for (int i = 0; i < depth; i++) {
223 public void close() {
224 int depthEntered = currentArgs.size() - rootPosition.getRootIdentifier().getPathArguments().size();
225 if (depthEntered > 0) {
226 // clean up existing modification cursor in case this tx will be reused for batching
227 delegate.exit(depthEntered);
235 public void delete(final PathArgument child) {
236 checkAvailable(child);
237 delegate.delete(child);
241 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
242 checkAvailable(child);
243 delegate.merge(child, data);
247 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
248 checkAvailable(child);
249 delegate.write(child, data);
252 void checkAvailable(final PathArgument child) {
253 layout.checkAvailable(currentArgs, child);
257 ProducerLayout getLayout() {