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.collect.BiMap;
12 import com.google.common.collect.ImmutableBiMap;
13 import com.google.common.collect.ImmutableBiMap.Builder;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.LinkedList;
21 import java.util.Map.Entry;
22 import java.util.Queue;
24 import javax.annotation.concurrent.GuardedBy;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
30 import org.opendaylight.mdsal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
32 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
33 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 final class ShardedDOMDataTreeProducer implements DOMDataTreeProducer {
38 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducer.class);
39 private final BiMap<DOMDataTreeShard, DOMStoreTransactionChain> shardToChain;
40 private final Map<DOMDataTreeIdentifier, DOMDataTreeShard> idToShard;
41 private final ShardedDOMDataTree dataTree;
44 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
46 private DOMDataWriteTransaction openTx;
48 private boolean closed;
51 private ShardedDOMDataTreeListenerContext<?> attachedListener;
53 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap, final Set<DOMDataTreeShard> shards) {
54 this.dataTree = Preconditions.checkNotNull(dataTree);
56 // Create shard -> chain map
57 final Builder<DOMDataTreeShard, DOMStoreTransactionChain> cb = ImmutableBiMap.builder();
58 final Queue<Exception> es = new LinkedList<>();
60 for (final DOMDataTreeShard s : shards) {
61 if (s instanceof DOMStore) {
63 final DOMStoreTransactionChain c = ((DOMStore)s).createTransactionChain();
64 LOG.trace("Using DOMStore chain {} to access shard {}", c, s);
66 } catch (final Exception e) {
67 LOG.error("Failed to instantiate chain for shard {}", s, e);
71 LOG.error("Unhandled shard instance type {}", s.getClass());
74 this.shardToChain = cb.build();
76 // An error was encountered, close chains and report the error
77 if (shardToChain.size() != shards.size()) {
78 for (final DOMStoreTransactionChain c : shardToChain.values()) {
81 } catch (final Exception e) {
82 LOG.warn("Exception raised while closing chain {}", c, e);
86 final IllegalStateException e = new IllegalStateException("Failed to completely allocate contexts", es.poll());
87 while (!es.isEmpty()) {
88 e.addSuppressed(es.poll());
94 idToShard = ImmutableMap.copyOf(shardMap);
98 public synchronized DOMDataWriteTransaction createTransaction(final boolean isolated) {
99 Preconditions.checkState(!closed, "Producer is already closed");
100 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
102 // Allocate backing transactions
103 final Map<DOMDataTreeShard, DOMStoreWriteTransaction> shardToTx = new HashMap<>();
104 for (final Entry<DOMDataTreeShard, DOMStoreTransactionChain> e : shardToChain.entrySet()) {
105 shardToTx.put(e.getKey(), e.getValue().newWriteOnlyTransaction());
108 // Create the ID->transaction map
109 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMStoreWriteTransaction> b = ImmutableMap.builder();
110 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeShard> e : idToShard.entrySet()) {
111 b.put(e.getKey(), shardToTx.get(e.getValue()));
114 final ShardedDOMDataWriteTransaction ret = new ShardedDOMDataWriteTransaction(this, b.build());
120 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
121 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
122 if (i.contains(subtree)) {
131 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
132 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
133 if (e.getKey().contains(s)) {
142 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
143 Preconditions.checkState(!closed, "Producer is already closed");
144 Preconditions.checkState(openTx == null, "Transaction %s is still open", openTx);
146 for (final DOMDataTreeIdentifier s : subtrees) {
147 // Check if the subtree was visible at any time
148 Preconditions.checkArgument(haveSubtree(s), "Subtree %s was never available in producer %s", s, this);
149 // Check if the subtree has not been delegated to a child
150 final DOMDataTreeProducer child = lookupChild(s);
151 Preconditions.checkArgument(child == null, "Subtree %s is delegated to child producer %s", s, child);
153 // Check if part of the requested subtree is not delegated to a child.
154 for (final DOMDataTreeIdentifier c : children.keySet()) {
156 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
161 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
162 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
164 for (final DOMDataTreeIdentifier s : subtrees) {
168 children = cb.build();
172 boolean isDelegatedToChild(DOMDataTreeIdentifier path) {
173 for (final DOMDataTreeIdentifier c : children.keySet()) {
174 if (c.contains(path)) {
183 public synchronized void close() throws DOMDataTreeProducerException {
185 if (openTx != null) {
186 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
190 dataTree.destroyProducer(this);
194 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
196 * FIXME: we do not allow multiple multiple shards in a producer because we do not implement the
197 * synchronization primitives yet
199 final Set<DOMDataTreeShard> shards = ImmutableSet.copyOf(shardMap.values());
200 if (shards.size() > 1) {
201 throw new UnsupportedOperationException("Cross-shard producers are not supported yet");
204 return new ShardedDOMDataTreeProducer(dataTree, shardMap, shards);
207 Set<DOMDataTreeIdentifier> getSubtrees() {
208 return idToShard.keySet();
211 synchronized void cancelTransaction(final ShardedDOMDataWriteTransaction transaction) {
212 if (!openTx.equals(transaction)) {
213 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
217 LOG.debug("Transaction {} cancelled", transaction);
221 synchronized void transactionSubmitted(ShardedDOMDataWriteTransaction transaction) {
222 Preconditions.checkState(openTx.equals(transaction));
226 synchronized void boundToListener(ShardedDOMDataTreeListenerContext<?> listener) {
227 // FIXME: Add option to dettach
228 Preconditions.checkState(this.attachedListener == null,
229 "Producer %s is already attached to other listener.",
230 listener.getListener());
231 this.attachedListener = listener;