837871c03156addd19e4d5dac364fe38ff794ac8
[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.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;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
38     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
39
40     private final Set<DOMDataTreeIdentifier> subtrees;
41     private final ShardedDOMDataTree dataTree;
42
43     private BiMap<DOMDataTreeIdentifier, DOMDataTreeShardProducer> idToProducer = ImmutableBiMap.of();
44     private Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
45
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;
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 = Collections.emptyMap();
64     @GuardedBy("this")
65     private boolean closed;
66
67     @GuardedBy("this")
68     private ShardedDOMDataTreeListenerContext<?> attachedListener;
69
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);
77         }
78         idToShard = ImmutableMap.copyOf(shardMap);
79         this.subtrees = ImmutableSet.copyOf(subtrees);
80     }
81
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());
89         }
90
91         return new ShardedDOMDataTreeProducer(dataTree, subtrees, shardMap, shardToIdentifiers);
92     }
93
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());
100         }
101         this.idToProducer = mapIdsToProducer(shardToIdentifiers);
102         idToShard = ImmutableMap.copyOf(shardMap);
103     }
104
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);
116                 }
117             } else {
118                 LOG.error("Unable to create a producer for shard that's not a WriteableDOMDataTreeShard");
119             }
120         }
121
122         return idToProducerBuilder.build();
123     }
124
125     @Override
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);
129
130         LOG.debug("Creating transaction from producer");
131         final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
132         final ShardedDOMDataTreeWriteTransaction ret;
133         if (isolated) {
134             // Isolated case. If we have a previous transaction, submit it before returning this one.
135             if (current != null) {
136                 submitTransaction(current);
137             }
138             ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children, true);
139         } else {
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());
144                 ret = current;
145             } else {
146                 ret = new ShardedDOMDataTreeWriteTransaction(this, idToProducer, children, false);
147             }
148         }
149
150         final boolean success = OPEN_UPDATER.compareAndSet(this, null, ret);
151         Verify.verify(success);
152         return ret;
153     }
154
155     private void submitTransaction(final ShardedDOMDataTreeWriteTransaction current) {
156         lastTx = current;
157         current.doSubmit(this::transactionSuccessful, this::transactionFailed);
158     }
159
160     @GuardedBy("this")
161     private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
162         for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
163             if (i.contains(subtree)) {
164                 return true;
165             }
166         }
167
168         return false;
169     }
170
171     @GuardedBy("this")
172     private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier domDataTreeIdentifier) {
173         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
174             if (e.getKey().contains(domDataTreeIdentifier)) {
175                 return e.getValue();
176             }
177         }
178
179         return null;
180     }
181
182     @Override
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);
186
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);
193
194             // Check if part of the requested subtree is not delegated to a child.
195             for (final DOMDataTreeIdentifier c : children.keySet()) {
196                 if (s.contains(c)) {
197                     throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is"
198                             + " superset of already-delegated %s", s, c));
199                 }
200             }
201         }
202
203         final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
204         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
205         cb.putAll(children);
206         for (final DOMDataTreeIdentifier s : subtrees) {
207             cb.put(s, ret);
208         }
209
210         children = cb.build();
211         return ret;
212     }
213
214     boolean isDelegatedToChild(final DOMDataTreeIdentifier path) {
215         for (final DOMDataTreeIdentifier c : children.keySet()) {
216             if (c.contains(path)) {
217                 return true;
218             }
219         }
220         return false;
221     }
222
223
224     @Override
225     public synchronized void close() throws DOMDataTreeProducerException {
226         if (!closed) {
227             if (openTx != null) {
228                 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
229             }
230
231             closed = true;
232             dataTree.destroyProducer(this);
233         }
234     }
235
236     protected Set<DOMDataTreeIdentifier> getSubtrees() {
237         return subtrees;
238     }
239
240     void cancelTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
241         final boolean success = OPEN_UPDATER.compareAndSet(this, transaction, null);
242         if (success) {
243             LOG.debug("Transaction {} cancelled", transaction);
244         } else {
245             LOG.warn("Transaction {} is not open in producer {}", transaction, this);
246         }
247     }
248
249     void processTransaction(final ShardedDOMDataTreeWriteTransaction transaction) {
250         final boolean wasOpen = OPEN_UPDATER.compareAndSet(this, transaction, null);
251         Verify.verify(wasOpen);
252
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();
259             }
260         } else {
261             submitTransaction(transaction);
262         }
263     }
264
265     void transactionSuccessful(final ShardedDOMDataTreeWriteTransaction tx, final Void result) {
266         LOG.debug("Transaction {} completed successfully", tx.getIdentifier());
267
268         tx.onTransactionSuccess(result);
269         processNextTransaction(tx);
270     }
271
272     void transactionFailed(final ShardedDOMDataTreeWriteTransaction tx, final Throwable throwable) {
273         LOG.debug("Transaction {} failed", tx.getIdentifier(), throwable);
274
275         tx.onTransactionFailure(throwable);
276         processNextTransaction(tx);
277     }
278
279     private void processCurrentTransaction() {
280         final ShardedDOMDataTreeWriteTransaction current = CURRENT_UPDATER.getAndSet(this, null);
281         if (current != null) {
282             submitTransaction(current);
283         }
284     }
285
286     private void processNextTransaction(final ShardedDOMDataTreeWriteTransaction tx) {
287         final boolean wasLast = LAST_UPDATER.compareAndSet(this, tx, null);
288         if (wasLast) {
289             processCurrentTransaction();
290         }
291     }
292
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;
298     }
299 }