Merge "Added .gitreview"
[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 org.opendaylight.mdsal.dom.spi.store.DOMStore;
11 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
12 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
13
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
19 import org.opendaylight.mdsal.dom.api.DOMDataWriteTransaction;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.BiMap;
22 import com.google.common.collect.ImmutableBiMap;
23 import com.google.common.collect.ImmutableBiMap.Builder;
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.ImmutableSet;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Queue;
33 import java.util.Set;
34 import javax.annotation.concurrent.GuardedBy;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 final class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
39     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
40     private final BiMap<DOMDataTreeShard, DOMStoreTransactionChain> shardToChain;
41     private final Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
42     private final ShardedDOMDataTree dataTree;
43
44     @GuardedBy("this")
45     private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
46     @GuardedBy("this")
47     private DOMDataWriteTransaction openTx;
48     @GuardedBy("this")
49     private boolean closed;
50
51     ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap, final Set<DOMDataTreeShard> shards) {
52         this.dataTree = Preconditions.checkNotNull(dataTree);
53
54         // Create shard -> chain map
55         final Builder<DOMDataTreeShard, DOMStoreTransactionChain> cb = ImmutableBiMap.builder();
56         final Queue<Exception> es = new LinkedList<>();
57
58         for (final DOMDataTreeShard s : shards) {
59             if (s instanceof DOMStore) {
60                 try {
61                     final DOMStoreTransactionChain c = ((DOMStore)s).createTransactionChain();
62                     LOG.trace("Using DOMStore chain {} to access shard {}", c, s);
63                     cb.put(s, c);
64                 } catch (final Exception e) {
65                     LOG.error("Failed to instantiate chain for shard {}", s, e);
66                     es.add(e);
67                 }
68             } else {
69                 LOG.error("Unhandled shard instance type {}", s.getClass());
70             }
71         }
72         this.shardToChain = cb.build();
73
74         // An error was encountered, close chains and report the error
75         if (shardToChain.size() != shards.size()) {
76             for (final DOMStoreTransactionChain c : shardToChain.values()) {
77                 try {
78                     c.close();
79                 } catch (final Exception e) {
80                     LOG.warn("Exception raised while closing chain {}", c, e);
81                 }
82             }
83
84             final IllegalStateException e = new IllegalStateException("Failed to completely allocate contexts", es.poll());
85             while (!es.isEmpty()) {
86                 e.addSuppressed(es.poll());
87             }
88
89             throw e;
90         }
91
92         idToShard = ImmutableMap.copyOf(shardMap);
93     }
94
95     @Override
96     public synchronized DOMDataWriteTransaction createTransaction(final boolean isolated) {
97         Preconditions.checkState(!closed, "Producer is already closed");
98         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
99
100         // Allocate backing transactions
101         final Map<DOMDataTreeShard, DOMStoreWriteTransaction> shardToTx = new HashMap<>();
102         for (final Entry<DOMDataTreeShard, DOMStoreTransactionChain> e : shardToChain.entrySet()) {
103             shardToTx.put(e.getKey(), e.getValue().newWriteOnlyTransaction());
104         }
105
106         // Create the ID->transaction map
107         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMStoreWriteTransaction> b = ImmutableMap.builder();
108         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> e : idToShard.entrySet()) {
109             b.put(e.getKey(), shardToTx.get(e.getValue()));
110         }
111
112         final ShardedDOMDataWriteTransaction ret = new ShardedDOMDataWriteTransaction(this, b.build());
113         openTx = ret;
114         return ret;
115     }
116
117     @GuardedBy("this")
118     private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
119         for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
120             if (i.contains(subtree)) {
121                 return true;
122             }
123         }
124
125         return false;
126     }
127
128     @GuardedBy("this")
129     private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
130         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
131             if (e.getKey().contains(s)) {
132                 return e.getValue();
133             }
134         }
135
136         return null;
137     }
138
139     @Override
140     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
141         Preconditions.checkState(!closed, "Producer is already closed");
142         Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
143
144         for (final DOMDataTreeIdentifier s : subtrees) {
145             // Check if the subtree was visible at any time
146             if (!haveSubtree(s)) {
147                 throw new IllegalArgumentException(String.format("Subtree %s was never available in producer %s", s, this));
148             }
149
150             // Check if the subtree has not been delegated to a child
151             final DOMDataTreeProducer child = lookupChild(s);
152             Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
153
154             // Check if part of the requested subtree is not delegated to a child.
155             for (final DOMDataTreeIdentifier c : children.keySet()) {
156                 if (s.contains(c)) {
157                     throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
158                 }
159             }
160         }
161
162         final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
163         final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
164         cb.putAll(children);
165         for (final DOMDataTreeIdentifier s : subtrees) {
166             cb.put(s, ret);
167         }
168
169         children = cb.build();
170         return ret;
171     }
172
173     @Override
174     public synchronized void close() throws DOMDataTreeProducerException {
175         if (!closed) {
176             if (openTx != null) {
177                 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
178             }
179
180             closed = true;
181             dataTree.destroyProducer(this);
182         }
183     }
184
185     static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
186         /*
187          * FIXME: we do not allow multiple multiple shards in a producer because we do not implement the
188          *        synchronization primitives yet
189          */
190         final Set<DOMDataTreeShard> shards = ImmutableSet.copyOf(shardMap.values());
191         if (shards.size() > 1) {
192             throw new UnsupportedOperationException("Cross-shard producers are not supported yet");
193         }
194
195         return new ShardedDOMDataTreeProducer(dataTree, shardMap, shards);
196     }
197
198     Set<DOMDataTreeIdentifier> getSubtrees() {
199         return idToShard.keySet();
200     }
201
202     synchronized void cancelTransaction(final ShardedDOMDataWriteTransaction transaction) {
203         if (!openTx.equals(transaction)) {
204             LOG.warn("Transaction {} is not open in producer {}", transaction, this);
205             return;
206         }
207
208         LOG.debug("Transaction {} cancelled", transaction);
209         openTx = null;
210     }
211 }