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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.Maps;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Collection;
19 import java.util.Deque;
20 import java.util.LinkedList;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import java.util.concurrent.atomic.AtomicLong;
26 import java.util.function.BiConsumer;
27 import java.util.function.Consumer;
28 import java.util.stream.Collectors;
29 import javax.annotation.Nonnull;
30 import javax.annotation.concurrent.GuardedBy;
31 import javax.annotation.concurrent.NotThreadSafe;
32 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
36 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
37 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardWriteTransaction;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 final class ShardedDOMDataTreeWriteTransaction implements DOMDataTreeCursorAwareTransaction {
46 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeWriteTransaction.class);
47 private static final TransactionCommitFailedExceptionMapper SUBMIT_FAILED_MAPPER =
48 TransactionCommitFailedExceptionMapper.create("submit");
49 private static final AtomicLong COUNTER = new AtomicLong();
51 private final Map<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> idToTransaction;
52 private final Collection<YangInstanceIdentifier> childBoundaries;
53 private final ShardedDOMDataTreeProducer producer;
54 private final String identifier;
56 private final SettableFuture<Void> future = SettableFuture.create();
57 private final CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
58 Futures.makeChecked(future, SUBMIT_FAILED_MAPPER);
61 private boolean closed = false;
64 private DOMDataTreeWriteCursor openCursor;
66 ShardedDOMDataTreeWriteTransaction(final ShardedDOMDataTreeProducer producer,
67 final Map<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer,
68 final Set<YangInstanceIdentifier> childRoots) {
69 this.producer = Preconditions.checkNotNull(producer);
70 this.identifier = "SHARDED-DOM-" + COUNTER.getAndIncrement();
71 idToTransaction = ImmutableMap.copyOf(Maps.transformValues(idToProducer,
72 DOMDataTreeShardProducer::createTransaction));
73 childBoundaries = Preconditions.checkNotNull(childRoots);
74 LOG.debug("Created new transaction {}", identifier);
77 private DOMDataTreeShardWriteTransaction lookup(final DOMDataTreeIdentifier prefix) {
78 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShardWriteTransaction> e : idToTransaction.entrySet()) {
79 if (e.getKey().contains(prefix)) {
80 Preconditions.checkArgument(!producer.isDelegatedToChild(prefix),
81 "Path %s is delegated to child producer.", prefix);
86 throw new IllegalArgumentException(String.format("Path %s is not accessible from transaction %s",
91 public String getIdentifier() {
96 public synchronized boolean cancel() {
101 LOG.debug("Cancelling transaction {}", identifier);
102 if (openCursor != null) {
105 for (final DOMDataTreeShardWriteTransaction tx : idToTransaction.values()) {
110 producer.cancelTransaction(this);
115 public synchronized DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
116 Preconditions.checkState(!closed, "Transaction is closed already");
117 Preconditions.checkState(openCursor == null, "There is still a cursor open");
118 final DOMDataTreeShardWriteTransaction lookup = lookup(prefix);
119 openCursor = new DelegatingCursor(lookup.createCursor(prefix), prefix);
124 public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() {
125 Preconditions.checkState(!closed, "Transaction %s is already closed", identifier);
126 Preconditions.checkState(openCursor == null, "Cannot submit transaction while there is a cursor open");
128 producer.processTransaction(this);
132 CheckedFuture<Void, TransactionCommitFailedException> doSubmit(
133 final Consumer<ShardedDOMDataTreeWriteTransaction> success,
134 final BiConsumer<ShardedDOMDataTreeWriteTransaction, Throwable> failure) {
136 final ListenableFuture<List<Void>> listListenableFuture = Futures.allAsList(
137 idToTransaction.values().stream().map(tx -> {
138 LOG.debug("Readying tx {}", identifier);
141 }).collect(Collectors.toList()));
143 final SettableFuture<Void> ret = SettableFuture.create();
144 Futures.addCallback(listListenableFuture, new FutureCallback<List<Void>>() {
146 public void onSuccess(final List<Void> result) {
147 success.accept(ShardedDOMDataTreeWriteTransaction.this);
152 public void onFailure(final Throwable exp) {
153 failure.accept(ShardedDOMDataTreeWriteTransaction.this, exp);
154 ret.setException(exp);
158 return Futures.makeChecked(ret, SUBMIT_FAILED_MAPPER);
161 void onTransactionSuccess(final Void result) {
165 void onTransactionFailure(final Throwable throwable) {
166 future.setException(throwable);
169 synchronized void cursorClosed() {
173 private class DelegatingCursor implements DOMDataTreeWriteCursor {
175 private final DOMDataTreeWriteCursor delegate;
176 private final DOMDataTreeIdentifier rootPosition;
177 private final Deque<PathArgument> path = new LinkedList<>();
179 DelegatingCursor(final DOMDataTreeWriteCursor delegate, final DOMDataTreeIdentifier rootPosition) {
180 this.delegate = Preconditions.checkNotNull(delegate);
181 this.rootPosition = Preconditions.checkNotNull(rootPosition);
182 path.addAll(rootPosition.getRootIdentifier().getPathArguments());
186 public void enter(@Nonnull final PathArgument child) {
187 checkAvailable(child);
189 delegate.enter(child);
193 public void enter(@Nonnull final PathArgument... path) {
194 for (final PathArgument pathArgument : path) {
200 public void enter(@Nonnull final Iterable<PathArgument> path) {
201 for (final PathArgument pathArgument : path) {
213 public void exit(final int depth) {
214 for (int i = 0; i < depth; i++) {
217 delegate.exit(depth);
221 public void close() {
222 int depthEntered = path.size() - rootPosition.getRootIdentifier().getPathArguments().size();
223 if (depthEntered > 0) {
224 // clean up existing modification cursor in case this tx will be reused for batching
225 delegate.exit(depthEntered);
233 public void delete(final PathArgument child) {
234 checkAvailable(child);
235 delegate.delete(child);
239 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
240 checkAvailable(child);
241 delegate.merge(child, data);
245 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
246 checkAvailable(child);
247 delegate.write(child, data);
250 void checkAvailable(final PathArgument child) {
252 final YangInstanceIdentifier yid = YangInstanceIdentifier.create(path);
253 childBoundaries.forEach(id -> {
254 if (id.contains(yid)) {
256 throw new IllegalArgumentException("Path {" + yid + "} is not available to this cursor"
257 + " since it's already claimed by a child producer");