Improve ShardedDOMDataTreeWriteTransaction performance
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeProducer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
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;
37
38 class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
39     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
40
41     private final Set<DOMDataTreeIdentifier> subtrees;
42     private final ShardedDOMDataTree dataTree;
43
44     private BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer = ImmutableBiMap.of();
45     private Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
46
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;
52
53     private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
54         OPEN_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
55             ShardedDOMDataTreeWriteTransaction.class, "openTx");
56     private volatile ShardedDOMDataTreeWriteTransaction openTx;
57
58     private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
59         LAST_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
60             ShardedDOMDataTreeWriteTransaction.class, "lastTx");
61     private volatile ShardedDOMDataTreeWriteTransaction lastTx;
62
63     @GuardedBy("this")
64     private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = ImmutableMap.of();
65     @GuardedBy("this")
66     private Set<YangInstanceIdentifier> childRoots = ImmutableSet.of();
67     @GuardedBy("this")
68     private boolean closed;
69
70     @GuardedBy("this")
71     private ShardedDOMDataTreeListenerContext<?> attachedListener;
72
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);
80         }
81         idToShard = ImmutableMap.copyOf(shardMap);
82         this.subtrees = ImmutableSet.copyOf(subtrees);
83     }
84
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());
92         }
93
94         return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
95     }
96
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());
103         }
104         this.idToProducer = mapIdsToProducer(shardToIdentifiers);
105         idToShard = ImmutableMap.copyOf(shardMap);
106     }
107
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);
119                 }
120             } else {
121                 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
122             }
123         }
124
125         return idToProducerBuilder.build();
126     }
127
128     @Override
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);
132
133         LOG.debug("Creating transaction from producer");
134         final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
135         final ShardedDOMDataTreeWriteTransaction ret;
136         if (isolated) {
137             // Isolated case. If we have a previous transaction, submit it before returning this one.
138             if (current != null) {
139                 submitTransaction(current);
140             }
141             ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
142         } else {
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());
147                 ret = current;
148             } else {
149                 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
150             }
151         }
152
153         final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
154         Verify.verify(success);
155         return ret;
156     }
157
158     private void submitTransaction(final ShardedDOMDataTreeWriteTransaction current) {
159         lastTx = current;
160         current.doSubmit(this::transactionSuccessful, this::transactionFailed);
161     }
162
163     @GuardedBy("this")
164     private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
165         for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
166             if (i.contains(subtree)) {
167                 return true;
168             }
169         }
170
171         return false;
172     }
173
174     @GuardedBy("this")
175     private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier domDataTreeIdentifier) {
176         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
177             if (e.getKey().contains(domDataTreeIdentifier)) {
178                 return e.getValue();
179             }
180         }
181
182         return null;
183     }
184
185     @Override
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);
189
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);
196
197             // Check if part of the requested subtree is not delegated to a child.
198             for (final DOMDataTreeIdentifier c : children.keySet()) {
199                 if (s.contains(c)) {
200                     throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is"
201                             + " superset of already-delegated %s", s, c));
202                 }
203             }
204         }
205
206         final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
207         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
208         cb.putAll(children);
209         for (final DOMDataTreeIdentifier s : subtrees) {
210             cb.put(s, ret);
211         }
212
213         children = cb.build();
214         childRoots = ImmutableSet.copyOf(Collections2.transform(children.keySet(),
215             DOMDataTreeIdentifier::getRootIdentifier));
216         return ret;
217     }
218
219     boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
220         for (final DOMDataTreeIdentifier c : children.keySet()) {
221             if (c.contains(path)) {
222                 return true;
223             }
224         }
225         return false;
226     }
227
228
229     @Override
230     public synchronized void close() throws DOMDataTreeProducerException {
231         if (!closed) {
232             if (openTx != null) {
233                 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
234             }
235
236             closed = true;
237             dataTree.destroyProducer(this);
238         }
239     }
240
241     protected Set<DOMDataTreeIdentifier> getSubtrees() {
242         return subtrees;
243     }
244
245     void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
246         final boolean success = OPEN_UPDATER.compareAndSet(this, transaction, null);
247         if (success) {
248             LOG.debug("Transaction {} cancelled", transaction);
249         } else {
250             LOG.warn("Transaction {} is not open in producer {}", transaction, this);
251         }
252     }
253
254     void processTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
255         final boolean wasOpen = OPEN_UPDATER.compareAndSet(this, transaction, null);
256         Verify.verify(wasOpen);
257
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();
264             }
265         } else {
266             submitTransaction(transaction);
267         }
268     }
269
270     void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx) {
271         LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
272
273         tx.onTransactionSuccess(null);
274         processNextTransaction(tx);
275     }
276
277     void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
278         LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
279
280         tx.onTransactionFailure(throwable);
281         processNextTransaction(tx);
282     }
283
284     private void processCurrentTransaction() {
285         final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
286         if (current != null) {
287             submitTransaction(current);
288         }
289     }
290
291     private synchronized void processNextTransaction(final ShardedDOMDataTreeWriteTransaction tx) {
292         final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
293         if (wasLast) {
294             processCurrentTransaction();
295         }
296     }
297
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;
303     }
304 }