2 * Copyright (c) 2016 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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.anyCollection;
15 import static org.mockito.Matchers.anyMap;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Captor;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
42 import org.opendaylight.mdsal.dom.broker.util.TestModel;
43 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
55 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
59 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
60 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 public class ShardedDOMDataTreeTest {
66 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
68 private static SchemaContext schemaContext = null;
72 schemaContext = TestModel.createTestContext();
73 } catch (final ReactorException e) {
74 LOG.error("Unable to create schema context for TestModel", e);
78 private static final DOMDataTreeIdentifier ROOT_ID =
79 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
80 private static final DOMDataTreeIdentifier TEST_ID =
81 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.TEST_PATH);
83 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
84 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
86 private InMemoryDOMDataTreeShard rootShard;
88 private ShardedDOMDataTree dataTreeService;
89 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
91 private final ExecutorService executor = Executors.newSingleThreadExecutor();
94 private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
96 private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
98 private final ContainerNode crossShardContainer = createCrossShardContainer();
101 public void setUp() throws Exception {
102 MockitoAnnotations.initMocks(this);
104 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
105 rootShard.onGlobalContextUpdated(schemaContext);
107 final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
108 final DOMDataTreeProducer shardRegProducer = dataTree.createProducer(Collections.singletonList(ROOT_ID));
109 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
110 shardRegProducer.close();
112 dataTreeService = dataTree;
115 @Test(expected = IllegalArgumentException.class)
116 public void testProducerPathContention() throws Exception {
117 dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
118 dataTreeService.createProducer(Collections.singletonList(TEST_ID));
122 public void testShardRegistrationClose() throws Exception {
123 rootShardReg.close();
125 final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
126 newRootShard.onGlobalContextUpdated(schemaContext);
127 final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
129 final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
130 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
131 shardRegProducer.close();
133 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1);
134 innerShard.onGlobalContextUpdated(schemaContext);
135 final DOMDataTreeProducer shardRegProducer2 =
136 dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
137 ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg =
138 dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
140 innerShardReg.close();
141 // try to register the shard again
142 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
143 final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
144 final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
145 assertNotNull(cursor);
149 shardRegProducer2.close();
151 innerShardReg.close();
152 newRootShardReg.close();
156 public void testSingleShardWrite() throws Exception {
157 final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
158 doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
160 dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID),
161 true, Collections.emptyList());
163 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
164 DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
165 DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
166 assertNotNull(cursor);
168 cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
171 tx.submit().checkedGet();
172 fail("There's still an open cursor");
173 } catch (final IllegalStateException e) {
174 assertTrue(e.getMessage().contains("cursor open"));
178 tx.submit().checkedGet();
180 tx = producer.createTransaction(false);
181 cursor = tx.createCursor(TEST_ID);
182 assertNotNull(cursor);
184 cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
186 tx.submit().checkedGet();
188 verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(),
189 captorForSubtrees.capture());
190 final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
191 assertTrue(capturedValue.size() == 3);
193 final ContainerNode capturedChange =
194 (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
195 final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(
196 TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
197 assertEquals(innerContainerVerify, capturedChange);
199 verifyNoMoreInteractions(mockedDataTreeListener);
203 public void testMultipleWritesIntoSingleMapEntry() throws Exception {
205 final YangInstanceIdentifier oid1 = TestModel.OUTER_LIST_PATH.node(new NodeIdentifierWithPredicates(
206 TestModel.OUTER_LIST_QNAME, QName.create(TestModel.OUTER_LIST_QNAME, "id"), 0));
207 final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
209 final DOMDataTreeProducer shardProducer = dataTreeService.createProducer(
210 Collections.singletonList(outerListPath));
211 final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(outerListPath, executor, 1000);
212 outerListShard.onGlobalContextUpdated(schemaContext);
214 final ListenerRegistration<InMemoryDOMDataTreeShard> oid1ShardRegistration =
215 dataTreeService.registerDataTreeShard(outerListPath, outerListShard, shardProducer);
217 final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
218 final DOMDataTreeWriteCursor cursor =
219 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
220 assertNotNull(cursor);
222 MapNode innerList = ImmutableMapNodeBuilder
224 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
227 cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
229 tx.submit().checkedGet();
231 final ArrayList<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
232 for (int i = 0; i < 1000; i++) {
233 final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
234 for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
235 final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
236 final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(
237 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
238 oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
239 cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
241 futures.add(tx1.submit());
245 futures.get(futures.size() - 1).checkedGet();
249 private Collection<MapEntryNode> createInnerListMapEntries(int amount, String valuePrefix) {
250 final Collection<MapEntryNode> ret = new ArrayList<>();
251 for (int i = 0; i < amount; i++) {
252 ret.add(ImmutableNodes.mapEntryBuilder()
253 .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.INNER_LIST_QNAME,
254 QName.create(TestModel.OUTER_LIST_QNAME, "name"), Integer.toString(i)))
255 .withChild(ImmutableNodes
256 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "name"), Integer.toString(i)))
257 .withChild(ImmutableNodes
258 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "value"), valuePrefix + "-" + i))
266 public void testMultipleProducerCursorCreation() throws Exception {
268 final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
269 DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
270 //check if we can create cursor where the new producer will be
271 DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
272 assertNotNull(rootTxCursor);
273 rootTxCursor.close();
276 rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
277 fail("Should've failed there is still a tx open");
278 } catch (final IllegalStateException e) {
279 assertTrue(e.getMessage().contains("open"));
282 assertTrue(rootTx.cancel());
284 final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
285 Collections.singletonList(INNER_CONTAINER_ID));
287 rootTx = rootProducer.createTransaction(false);
289 rootTx.createCursor(INNER_CONTAINER_ID);
290 fail("Subtree should not be available to this producer");
291 } catch (final IllegalArgumentException e) {
292 assertTrue(e.getMessage().contains("delegated to child producer"));
295 rootTxCursor = rootTx.createCursor(TEST_ID);
296 assertNotNull(rootTxCursor);
298 rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
299 fail("Cursor should not have this subtree available");
300 } catch (final IllegalArgumentException e) {
301 assertTrue(e.getMessage().contains("not available to this cursor"));
305 rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
306 ImmutableContainerNodeBuilder.create()
307 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
309 fail("Cursor should not have this subtree available");
310 } catch (final IllegalArgumentException e) {
311 assertTrue(e.getMessage().contains("not available to this cursor"));
314 final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
315 final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
316 assertNotNull(innerShardCursor);
319 private ContainerNode createCrossShardContainer() {
320 final LeafNode<String> shardedValue1 =
321 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
322 TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
323 final LeafNode<String> shardedValue2 =
324 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
325 TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
328 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
329 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
330 .withChild(ImmutableLeafNodeBuilder.create()
331 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
332 .withValue("testing-value")
336 final ContainerNode containerNode =
337 ImmutableContainerNodeBuilder.create()
338 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
339 .withChild(shardedValue1)
340 .withChild(shardedValue2)
341 .withChild(lowerShardContainer)
344 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
345 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
346 .withChild(containerNode)
349 return testContainer;