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.base.Verify;
12 import com.google.common.collect.ArrayListMultimap;
13 import com.google.common.collect.BiMap;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableBiMap.Builder;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Multimap;
20 import java.util.Collection;
22 import java.util.Map.Entry;
24 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
25 import javax.annotation.concurrent.GuardedBy;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
32 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
33 import org.opendaylight.mdsal.dom.store.inmemory.WriteableDOMDataTreeShard;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
39 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
41 private final Set<DOMDataTreeIdentifier> subtrees;
42 private final ShardedDOMDataTree dataTree;
44 private BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer = ImmutableBiMap.of();
45 private Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
47 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
48 CURRENT_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
49 ShardedDOMDataTreeWriteTransaction.class, "currentTx");
50 @SuppressWarnings("unused")
51 private volatile ShardedDOMDataTreeWriteTransaction currentTx;
53 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
54 OPEN_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
55 ShardedDOMDataTreeWriteTransaction.class, "openTx");
56 private volatile ShardedDOMDataTreeWriteTransaction openTx;
58 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
59 LAST_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
60 ShardedDOMDataTreeWriteTransaction.class, "lastTx");
61 private volatile ShardedDOMDataTreeWriteTransaction lastTx;
64 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = ImmutableMap.of();
66 private Set<YangInstanceIdentifier> childRoots = ImmutableSet.of();
68 private boolean closed;
71 private ShardedDOMDataTreeListenerContext<?> attachedListener;
73 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
74 final Collection<DOMDataTreeIdentifier> subtrees,
75 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
76 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
77 this.dataTree = Preconditions.checkNotNull(dataTree);
78 if (!shardToId.isEmpty()) {
79 this.idToProducer = mapIdsToProducer(shardToId);
81 idToShard = ImmutableMap.copyOf(shardMap);
82 this.subtrees = ImmutableSet.copyOf(subtrees);
85 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree,
86 final Collection<DOMDataTreeIdentifier> subtrees,
87 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
88 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
89 // map which identifier belongs to which shard
90 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
91 shardToIdentifiers.put(entry.getValue(), entry.getKey());
94 return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
97 void subshardAdded(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
98 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
99 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
100 // map which identifier belongs to which shard
101 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
102 shardToIdentifiers.put(entry.getValue(), entry.getKey());
104 this.idToProducer = mapIdsToProducer(shardToIdentifiers);
105 idToShard = ImmutableMap.copyOf(shardMap);
108 private static BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> mapIdsToProducer(
109 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
110 final Builder<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducerBuilder = ImmutableBiMap.builder();
111 for (final Entry<DOMDataTreeShard, Collection<DOMDataTreeIdentifier>> entry : shardToId.asMap().entrySet()) {
112 if (entry.getKey() instanceof WriteableDOMDataTreeShard) {
113 //create a single producer for all prefixes in a single shard
114 final DOMDataTreeShardProducer producer = ((WriteableDOMDataTreeShard) entry.getKey())
115 .createProducer(entry.getValue());
116 // id mapped to producers
117 for (final DOMDataTreeIdentifier id : entry.getValue()) {
118 idToProducerBuilder.put(id, producer);
121 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
125 return idToProducerBuilder.build();
129 public synchronized DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
130 Preconditions.checkState(!closed, "Producer is already closed");
131 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
133 LOG.debug("Creating transaction from producer");
134 final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
135 final ShardedDOMDataTreeWriteTransaction ret;
137 // Isolated case. If we have a previous transaction, submit it before returning this one.
138 if (current != null) {
139 submitTransaction(current);
141 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
143 // Non-isolated case, see if we can reuse the transaction
144 if (current != null) {
145 LOG.debug("Reusing previous transaction {} since there is still a transaction inflight",
146 current.getIdentifier());
149 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
153 final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
154 Verify.verify(success);
158 private void submitTransaction(final ShardedDOMDataTreeWriteTransaction current) {
160 current.doSubmit(this::transactionSuccessful, this::transactionFailed);
164 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
165 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
166 if (i.contains(subtree)) {
175 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier domDataTreeIdentifier) {
176 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
177 if (e.getKey().contains(domDataTreeIdentifier)) {
186 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
187 Preconditions.checkState(!closed, "Producer is already closed");
188 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
190 for (final DOMDataTreeIdentifier s : subtrees) {
191 // Check if the subtree was visible at any time
192 Preconditions.checkArgument(haveSubtree(s), "Subtree %s was never available in producer %s", s, this);
193 // Check if the subtree has not been delegated to a child
194 final DOMDataTreeProducer child = lookupChild(s);
195 Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
197 // Check if part of the requested subtree is not delegated to a child.
198 for (final DOMDataTreeIdentifier c : children.keySet()) {
200 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is"
201 + " superset of already-delegated %s", s, c));
206 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
207 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
209 for (final DOMDataTreeIdentifier s : subtrees) {
213 children = cb.build();
214 childRoots = ImmutableSet.copyOf(Collections2.transform(children.keySet(),
215 DOMDataTreeIdentifier::getRootIdentifier));
219 boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
220 for (final DOMDataTreeIdentifier c : children.keySet()) {
221 if (c.contains(path)) {
230 public synchronized void close() throws DOMDataTreeProducerException {
232 if (openTx != null) {
233 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
237 dataTree.destroyProducer(this);
241 protected Set<DOMDataTreeIdentifier> getSubtrees() {
245 void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
246 final boolean success = OPEN_UPDATER.compareAndSet(this, transaction, null);
248 LOG.debug("Transaction {} cancelled", transaction);
250 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
254 void processTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
255 final boolean wasOpen = OPEN_UPDATER.compareAndSet(this, transaction, null);
256 Verify.verify(wasOpen);
258 if (lastTx != null) {
259 final boolean success = CURRENT_UPDATER.compareAndSet(this, null, transaction);
260 Verify.verify(success);
261 if (lastTx == null) {
262 // Dispatch after requeue
263 processCurrentTransaction();
266 submitTransaction(transaction);
270 void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx) {
271 LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
273 tx.onTransactionSuccess(null);
274 processNextTransaction(tx);
277 void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
278 LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
280 tx.onTransactionFailure(throwable);
281 processNextTransaction(tx);
284 private void processCurrentTransaction() {
285 final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
286 if (current != null) {
287 submitTransaction(current);
291 private synchronized void processNextTransaction(final ShardedDOMDataTreeWriteTransaction tx) {
292 final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
294 processCurrentTransaction();
298 synchronized void boundToListener(final ShardedDOMDataTreeListenerContext<?> listener) {
299 // FIXME: Add option to detach
300 Preconditions.checkState(this.attachedListener == null, "Producer %s is already attached to other listener.",
301 listener.getListener());
302 this.attachedListener = listener;