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 org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
11 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
12 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
15 import org.opendaylight.mdsal.dom.api.DOMDataWriteTransaction;
17 import com.google.common.base.Preconditions;
18 import com.google.common.collect.BiMap;
19 import com.google.common.collect.ImmutableBiMap;
20 import com.google.common.collect.ImmutableBiMap.Builder;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.LinkedList;
28 import java.util.Map.Entry;
29 import java.util.Queue;
31 import javax.annotation.concurrent.GuardedBy;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
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;
45 private Map<DOMDataTreeIdentifier, DOMDataTreeProducer> children = Collections.emptyMap();
47 private DOMDataWriteTransaction openTx;
49 private boolean closed;
51 ShardedDOMDataTreeProducer(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap, final Set<DOMDataTreeShard> shards) {
52 this.dataTree = Preconditions.checkNotNull(dataTree);
54 // Create shard -> chain map
55 final Builder<DOMDataTreeShard, DOMStoreTransactionChain> cb = ImmutableBiMap.builder();
56 final Queue<Exception> es = new LinkedList<>();
58 for (final DOMDataTreeShard s : shards) {
59 if (s instanceof DOMStore) {
61 final DOMStoreTransactionChain c = ((DOMStore)s).createTransactionChain();
62 LOG.trace("Using DOMStore chain {} to access shard {}", c, s);
64 } catch (final Exception e) {
65 LOG.error("Failed to instantiate chain for shard {}", s, e);
69 LOG.error("Unhandled shard instance type {}", s.getClass());
72 this.shardToChain = cb.build();
74 // An error was encountered, close chains and report the error
75 if (shardToChain.size() != shards.size()) {
76 for (final DOMStoreTransactionChain c : shardToChain.values()) {
79 } catch (final Exception e) {
80 LOG.warn("Exception raised while closing chain {}", c, e);
84 final IllegalStateException e = new IllegalStateException("Failed to completely allocate contexts", es.poll());
85 while (!es.isEmpty()) {
86 e.addSuppressed(es.poll());
92 idToShard = ImmutableMap.copyOf(shardMap);
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);
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());
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()));
112 final ShardedDOMDataWriteTransaction ret = new ShardedDOMDataWriteTransaction(this, b.build());
118 private boolean haveSubtree(final DOMDataTreeIdentifier subtree) {
119 for (final DOMDataTreeIdentifier i : idToShard.keySet()) {
120 if (i.contains(subtree)) {
129 private DOMDataTreeProducer lookupChild(final DOMDataTreeIdentifier s) {
130 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : children.entrySet()) {
131 if (e.getKey().contains(s)) {
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);
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));
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);
154 // Check if part of the requested subtree is not delegated to a child.
155 for (final DOMDataTreeIdentifier c : children.keySet()) {
157 throw new IllegalArgumentException(String.format("Subtree %s cannot be delegated as it is superset of already-delegated %s", s, c));
162 final DOMDataTreeProducer ret = dataTree.createProducer(this, subtrees);
163 final ImmutableMap.Builder<DOMDataTreeIdentifier, DOMDataTreeProducer> cb = ImmutableMap.builder();
165 for (final DOMDataTreeIdentifier s : subtrees) {
169 children = cb.build();
174 public synchronized void close() throws DOMDataTreeProducerException {
176 if (openTx != null) {
177 throw new DOMDataTreeProducerBusyException(String.format("Transaction %s is still open", openTx));
181 dataTree.destroyProducer(this);
185 static DOMDataTreeProducer create(final ShardedDOMDataTree dataTree, final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
187 * FIXME: we do not allow multiple multiple shards in a producer because we do not implement the
188 * synchronization primitives yet
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");
195 return new ShardedDOMDataTreeProducer(dataTree, shardMap, shards);
198 Set<DOMDataTreeIdentifier> getSubtrees() {
199 return idToShard.keySet();
202 synchronized void cancelTransaction(final ShardedDOMDataWriteTransaction transaction) {
203 if (!openTx.equals(transaction)) {
204 LOG.warn("Transaction {} is not open in producer {}", transaction, this);
208 LOG.debug("Transaction {} cancelled", transaction);