773bd27b87170652a385cd4f1dac7ef3c9f8c5e9
[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     private volatile ShardedDOMDataTreeWriteTransaction currentTx;
51
52     private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
53         OPEN_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
54             ShardedDOMDataTreeWriteTransaction.class, "openTx");
55     private volatile ShardedDOMDataTreeWriteTransaction openTx;
56
57     private static final AtomicReferenceFieldUpdater<ShardedDOMDataTreeProducer, ShardedDOMDataTreeWriteTransaction>
58         LAST_UPDATER = AtomicReferenceFieldUpdater.newUpdater(ShardedDOMDataTreeProducer.class,
59             ShardedDOMDataTreeWriteTransaction.class, "lastTx");
60     private volatile ShardedDOMDataTreeWriteTransaction lastTx;
61
62     @GuardedBy("this")
63     private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = ImmutableMap.of();
64     @GuardedBy("this")
65     private Set<YangInstanceIdentifier> childRoots = ImmutableSet.of();
66     @GuardedBy("this")
67     private boolean closed;
68
69     @GuardedBy("this")
70     private ShardedDOMDataTreeListenerContext<?> attachedListener;
71
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);
79         }
80         idToShard = ImmutableMap.copyOf(shardMap);
81         this.subtrees = ImmutableSet.copyOf(subtrees);
82     }
83
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());
91         }
92
93         return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
94     }
95
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());
102         }
103         this.idToProducer = mapIdsToProducer(shardToIdentifiers);
104         idToShard = ImmutableMap.copyOf(shardMap);
105     }
106
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);
118                 }
119             } else {
120                 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
121             }
122         }
123
124         return idToProducerBuilder.build();
125     }
126
127     @Override
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);
131
132         LOG.debug("Creating transaction from producer");
133         final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
134         final ShardedDOMDataTreeWriteTransaction ret;
135         if (isolated) {
136             // Isolated case. If we have a previous transaction, submit it before returning this one.
137             if (current != null) {
138                 submitTransaction(current);
139             }
140             ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
141         } else {
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());
146                 ret = current;
147             } else {
148                 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, childRoots);
149             }
150         }
151
152         final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
153         Verify.verify(success);
154         return ret;
155     }
156
157     @GuardedBy("this")
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     // 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);
258
259         if (lastTx == null) {
260             // No transaction outstanding, we need to submit it now
261             synchronized (this) {
262                 submitTransaction(transaction);
263             }
264
265             return;
266         }
267
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);
272
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();
277         }
278     }
279
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);
286                 }
287             }
288         }
289     }
290
291     private void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx) {
292         LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
293
294         tx.onTransactionSuccess(null);
295         transactionCompleted(tx);
296     }
297
298     private void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
299         LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
300
301         tx.onTransactionFailure(throwable);
302         transactionCompleted(tx);
303     }
304
305     private void transactionCompleted(final ShardedDOMDataTreeWriteTransaction tx) {
306         final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
307         if (wasLast) {
308             submitCurrentTransaction();
309         }
310     }
311
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;
317     }
318 }