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.ImmutableBiMap;
15 import com.google.common.collect.ImmutableBiMap.Builder;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Multimap;
19 import java.util.Collection;
20 import java.util.Collections;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
38 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
40 private final Set<DOMDataTreeIdentifier> subtrees;
41 private final ShardedDOMDataTree dataTree;
43 private BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer = ImmutableBiMap.of();
44 private Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
46 private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
47 CURRENT_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
48 ShardedDOMDataTreeWriteTransaction.class, "currentTx");
49 @SuppressWarnings("unused")
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 = Collections.emptyMap();
65 private boolean closed;
68 private ShardedDOMDataTreeListenerContext<?> attachedListener;
70 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree,
71 final Collection<DOMDataTreeIdentifier> subtrees,
72 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap,
73 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
74 this.dataTree = Preconditions.checkNotNull(dataTree);
75 if (!shardToId.isEmpty()) {
76 this.idToProducer = mapIdsToProducer(shardToId);
78 idToShard = ImmutableMap.copyOf(shardMap);
79 this.subtrees = ImmutableSet.copyOf(subtrees);
82 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree,
83 final Collection<DOMDataTreeIdentifier> subtrees,
84 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
85 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
86 // map which identifier belongs to which shard
87 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
88 shardToIdentifiers.put(entry.getValue(), entry.getKey());
91 return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
94 void subshardAdded(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
95 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
96 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToIdentifiers = ArrayListMultimap.create();
97 // map which identifier belongs to which shard
98 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> entry : shardMap.entrySet()) {
99 shardToIdentifiers.put(entry.getValue(), entry.getKey());
101 this.idToProducer = mapIdsToProducer(shardToIdentifiers);
102 idToShard = ImmutableMap.copyOf(shardMap);
105 private static BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> mapIdsToProducer(
106 final Multimap<DOMDataTreeShard, DOMDataTreeIdentifier> shardToId) {
107 final Builder<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducerBuilder = ImmutableBiMap.builder();
108 for (final Entry<DOMDataTreeShard, Collection<DOMDataTreeIdentifier>> entry : shardToId.asMap().entrySet()) {
109 if (entry.getKey() instanceof WriteableDOMDataTreeShard) {
110 //create a single producer for all prefixes in a single shard
111 final DOMDataTreeShardProducer producer = ((WriteableDOMDataTreeShard) entry.getKey())
112 .createProducer(entry.getValue());
113 // id mapped to producers
114 for (final DOMDataTreeIdentifier id : entry.getValue()) {
115 idToProducerBuilder.put(id, producer);
118 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
122 return idToProducerBuilder.build();
126 public synchronized DOMDataTreeCursorAwareTransaction createTransaction(final boolean isolated) {
127 Preconditions.checkState(!closed, "Producer is already closed");
128 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
130 LOG.debug("Creating transaction from producer");
131 final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
132 final ShardedDOMDataTreeWriteTransaction ret;
134 // Isolated case. If we have a previous transaction, submit it before returning this one.
135 if (current != null) {
136 submitTransaction(current);
138 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children, true);
140 // Non-isolated case, see if we can reuse the transaction
141 if (current != null) {
142 LOG.debug("Reusing previous transaction {} since there is still a transaction inflight",
143 current.getIdentifier());
146 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children, false);
150 final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
151 Verify.verify(success);
155 private void submitTransaction(final ShardedDOMDataTreeWriteTransaction current) {
157 current.doSubmit(this::transactionSuccessful, this::transactionFailed);
161 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
162 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
163 if (i.contains(subtree)) {
172 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier domDataTreeIdentifier) {
173 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
174 if (e.getKey().contains(domDataTreeIdentifier)) {
183 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
184 Preconditions.checkState(!closed, "Producer is already closed");
185 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
187 for (final DOMDataTreeIdentifier s : subtrees) {
188 // Check if the subtree was visible at any time
189 Preconditions.checkArgument(haveSubtree(s), "Subtree %s was never available in producer %s", s, this);
190 // Check if the subtree has not been delegated to a child
191 final DOMDataTreeProducer child = lookupChild(s);
192 Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
194 // Check if part of the requested subtree is not delegated to a child.
195 for (final DOMDataTreeIdentifier c : children.keySet()) {
197 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is"
198 + " superset of already-delegated %s", s, c));
203 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
204 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
206 for (final DOMDataTreeIdentifier s : subtrees) {
210 children = cb.build();
214 boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
215 for (final DOMDataTreeIdentifier c : children.keySet()) {
216 if (c.contains(path)) {
225 public synchronized void close() throws DOMDataTreeProducerException {
227 if (openTx != null) {
228 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
232 dataTree.destroyProducer(this);
236 protected Set<DOMDataTreeIdentifier> getSubtrees() {
240 void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
241 final boolean success = OPEN_UPDATER.compareAndSet(this, transaction, null);
243 LOG.debug("Transaction {} cancelled", transaction);
245 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
249 void processTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
250 final boolean wasOpen = OPEN_UPDATER.compareAndSet(this, transaction, null);
251 Verify.verify(wasOpen);
253 if (lastTx != null) {
254 final boolean success = CURRENT_UPDATER.compareAndSet(this, null, transaction);
255 Verify.verify(success);
256 if (lastTx == null) {
257 // Dispatch after requeue
258 processCurrentTransaction();
261 submitTransaction(transaction);
265 void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx, final Void result) {
266 LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
268 tx.onTransactionSuccess(result);
269 processNextTransaction(tx);
272 void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
273 LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
275 tx.onTransactionFailure(throwable);
276 processNextTransaction(tx);
279 private void processCurrentTransaction() {
280 final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
281 if (current != null) {
282 submitTransaction(current);
286 private void processNextTransaction(final ShardedDOMDataTreeWriteTransaction tx) {
287 final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
289 processCurrentTransaction();
293 synchronized void boundToListener(final ShardedDOMDataTreeListenerContext<?> listener) {
294 // FIXME: Add option to detach
295 Preconditions.checkState(this.attachedListener == null, "Producer %s is already attached to other listener.",
296 listener.getListener());
297 this.attachedListener = listener;