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 private volatile ShardedDOMDataTreeWriteTransaction currentTx;
52 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
53 OPEN_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
54 ShardedDOMDataTreeWriteTransaction.class, "openTx");
55 private volatile ShardedDOMDataTreeWriteTransaction openTx;
57 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
58 LAST_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
59 ShardedDOMDataTreeWriteTransaction.class, "lastTx");
60 private volatile ShardedDOMDataTreeWriteTransaction lastTx;
63 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = ImmutableMap.of();
65 private Set<YangInstanceIdentifier> childRoots = ImmutableSet.of();
67 private boolean closed;
70 private ShardedDOMDataTreeListenerContext<?> attachedListener;
72 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
73 final Collection<DOMDataTreeIdentifier> subtrees,
74 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
75 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
76 this.dataTree = Preconditions.checkNotNull(dataTree);
77 if (!shardToId.isEmpty()) {
78 this.idToProducer = mapIdsToProducer(shardToId);
80 idToShard = ImmutableMap.copyOf(shardMap);
81 this.subtrees = ImmutableSet.copyOf(subtrees);
84 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree,
85 final Collection<DOMDataTreeIdentifier> subtrees,
86 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
87 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
88 // map which identifier belongs to which shard
89 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
90 shardToIdentifiers.put(entry.getValue(), entry.getKey());
93 return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
96 void subshardAdded(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
97 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
98 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
99 // map which identifier belongs to which shard
100 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
101 shardToIdentifiers.put(entry.getValue(), entry.getKey());
103 this.idToProducer = mapIdsToProducer(shardToIdentifiers);
104 idToShard = ImmutableMap.copyOf(shardMap);
107 private static BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> mapIdsToProducer(
108 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
109 final Builder<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducerBuilder = ImmutableBiMap.builder();
110 for (final Entry<DOMDataTreeShard, Collection<DOMDataTreeIdentifier>> entry : shardToId.asMap().entrySet()) {
111 if (entry.getKey() instanceof WriteableDOMDataTreeShard) {
112 //create a single producer for all prefixes in a single shard
113 final DOMDataTreeShardProducer producer = ((WriteableDOMDataTreeShard) entry.getKey())
114 .createProducer(entry.getValue());
115 // id mapped to producers
116 for (final DOMDataTreeIdentifier id : entry.getValue()) {
117 idToProducerBuilder.put(id, producer);
120 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
124 return idToProducerBuilder.build();
128 public synchronized DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
129 Preconditions.checkState(!closed, "Producer is already closed");
130 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
132 LOG.debug("Creating transaction from producer");
133 final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
134 final ShardedDOMDataTreeWriteTransaction ret;
136 // Isolated case. If we have a previous transaction, submit it before returning this one.
137 if (current != null) {
138 submitTransaction(current);
140 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
142 // Non-isolated case, see if we can reuse the transaction
143 if (current != null) {
144 LOG.debug("Reusing previous transaction {} since there is still a transaction inflight",
145 current.getIdentifier());
148 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
152 final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
153 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 // Called when the user submits a transaction
255 void transactionSubmitted(final ShardedDOMDataTreeWriteTransaction transaction) {
256 final boolean wasOpen = OPEN_UPDATER.compareAndSet(this, transaction, null);
257 Preconditions.checkState(wasOpen, "Attempted to submit non-open transaction %s", transaction);
259 if (lastTx == null) {
260 // No transaction outstanding, we need to submit it now
261 synchronized (this) {
262 submitTransaction(transaction);
268 // There is a potentially-racing submitted transaction. Publish the new one, which may end up being
269 // picked up by processNextTransaction.
270 final boolean success = CURRENT_UPDATER.compareAndSet(this, null, transaction);
271 Verify.verify(success);
273 // Now a quick check: if the racing transaction completed in between, it may have missed the current
274 // transaction, hence we need to re-check
275 if (lastTx == null) {
276 submitCurrentTransaction();
280 private void submitCurrentTransaction() {
281 final ShardedDOMDataTreeWriteTransaction current = currentTx;
282 if (current != null) {
283 synchronized (this) {
284 if (CURRENT_UPDATER.compareAndSet(this, current, null)) {
285 submitTransaction(current);
291 private void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx) {
292 LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
294 tx.onTransactionSuccess(null);
295 transactionCompleted(tx);
298 private void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
299 LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
301 tx.onTransactionFailure(throwable);
302 transactionCompleted(tx);
305 private void transactionCompleted(final ShardedDOMDataTreeWriteTransaction tx) {
306 final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
308 submitCurrentTransaction();
312 synchronized void boundToListener(final ShardedDOMDataTreeListenerContext<?> listener) {
313 // FIXME: Add option to detach
314 Preconditions.checkState(this.attachedListener == null, "Producer %s is already attached to other listener.",
315 listener.getListener());
316 this.attachedListener = listener;