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.any;
15 import static org.mockito.Matchers.anyCollection;
16 import static org.mockito.Matchers.anyMap;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.timeout;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Captor;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
42 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
43 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
44 import org.opendaylight.mdsal.dom.broker.util.TestModel;
45 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 public class ShardedDOMDataTreeTest {
68 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
70 private static SchemaContext schemaContext = null;
74 schemaContext = TestModel.createTestContext();
75 } catch (final ReactorException e) {
76 LOG.error("Unable to create schema context for TestModel", e);
80 private static final DOMDataTreeIdentifier ROOT_ID =
81 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
82 private static final DOMDataTreeIdentifier TEST_ID =
83 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
85 private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
86 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.INNER_CONTAINER_PATH);
88 private static final YangInstanceIdentifier OUTER_LIST_YID = TestModel.OUTER_LIST_PATH.node(
89 new NodeIdentifierWithPredicates(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
90 private static final DOMDataTreeIdentifier OUTER_LIST_ID =
91 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, OUTER_LIST_YID);
93 private InMemoryDOMDataTreeShard rootShard;
95 private ShardedDOMDataTree dataTreeService;
96 private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
98 private final ExecutorService executor = Executors.newSingleThreadExecutor();
101 private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
103 private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
105 private final ContainerNode crossShardContainer = createCrossShardContainer();
108 public void setUp() throws Exception {
109 MockitoAnnotations.initMocks(this);
111 rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
112 rootShard.onGlobalContextUpdated(schemaContext);
114 final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
115 final DOMDataTreeProducer shardRegProducer = dataTree.createProducer(Collections.singletonList(ROOT_ID));
116 rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
117 shardRegProducer.close();
119 dataTreeService = dataTree;
122 @Test(expected = IllegalArgumentException.class)
123 public void testProducerPathContention() throws Exception {
124 dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
125 dataTreeService.createProducer(Collections.singletonList(TEST_ID));
129 public void testShardRegistrationClose() throws Exception {
130 rootShardReg.close();
132 final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
133 newRootShard.onGlobalContextUpdated(schemaContext);
134 final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
136 final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
137 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
138 shardRegProducer.close();
140 final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID, executor, 1);
141 innerShard.onGlobalContextUpdated(schemaContext);
142 final DOMDataTreeProducer shardRegProducer2 =
143 dataTreeService.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
144 ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg =
145 dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
147 innerShardReg.close();
148 // try to register the shard again
149 innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard, shardRegProducer2);
150 final DOMDataTreeCursorAwareTransaction tx = shardRegProducer2.createTransaction(false);
151 final DOMDataTreeWriteCursor cursor = tx.createCursor(INNER_CONTAINER_ID);
152 assertNotNull(cursor);
156 shardRegProducer2.close();
158 innerShardReg.close();
159 newRootShardReg.close();
162 @Test(expected = IllegalStateException.class)
163 public void testEmptyShardMapProducer() throws Exception {
164 final ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
165 final DOMDataTreeProducer producer = dataTree.createProducer(Collections.singletonList(ROOT_ID));
166 producer.createTransaction(false);
170 public void testSingleShardWrite() throws Exception {
171 final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
172 doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
174 dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID),
175 true, Collections.emptyList());
177 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
178 DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
179 DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
180 assertNotNull(cursor);
182 cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
185 tx.submit().checkedGet();
186 fail("There's still an open cursor");
187 } catch (final IllegalStateException e) {
188 assertTrue(e.getMessage().contains("cursor open"));
192 tx.submit().checkedGet();
194 tx = producer.createTransaction(false);
195 cursor = tx.createCursor(TEST_ID);
196 assertNotNull(cursor);
198 cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
200 tx.submit().checkedGet();
202 verify(mockedDataTreeListener, timeout(1000).times(3)).onDataTreeChanged(captorForChanges.capture(),
203 captorForSubtrees.capture());
204 final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
205 assertTrue(capturedValue.size() == 3);
207 final ContainerNode capturedChange =
208 (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
209 final ContainerNode innerContainerVerify = (ContainerNode) crossShardContainer.getChild(
210 TestModel.INNER_CONTAINER_PATH.getLastPathArgument()).get();
211 assertEquals(innerContainerVerify, capturedChange);
213 verifyNoMoreInteractions(mockedDataTreeListener);
217 // TODO extract common logic from testSingleSubshardWrite and
218 // testSingleShardWrite tests
219 public void testSingleSubshardWrite() throws Exception {
220 final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
221 doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
223 InMemoryDOMDataTreeShard testShard = InMemoryDOMDataTreeShard.create(TEST_ID, executor, 1);
224 testShard.onGlobalContextUpdated(schemaContext);
226 final DOMDataTreeProducer regProducer = dataTreeService.createProducer(Collections.singleton(TEST_ID));
227 dataTreeService.registerDataTreeShard(TEST_ID, testShard, regProducer);
230 dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(TEST_ID),
231 true, Collections.emptyList());
233 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
234 DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
235 DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
236 assertNotNull(cursor);
238 cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
241 tx.submit().checkedGet();
243 tx = producer.createTransaction(false);
244 cursor = tx.createCursor(TEST_ID);
245 assertNotNull(cursor);
247 cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
249 tx.submit().checkedGet();
251 verify(mockedDataTreeListener, timeout(5000).times(3)).onDataTreeChanged(captorForChanges.capture(),
252 captorForSubtrees.capture());
254 final List<Collection<DataTreeCandidate>> capturedValue = captorForChanges.getAllValues();
255 final ContainerNode capturedChange =
256 (ContainerNode) capturedValue.get(1).iterator().next().getRootNode().getDataAfter().get();
257 final ContainerNode innerContainerVerify = crossShardContainer;
258 assertEquals(innerContainerVerify, capturedChange);
262 public void testMultipleWritesIntoSingleMapEntry() throws Exception {
264 final YangInstanceIdentifier oid1 = TestModel.OUTER_LIST_PATH.node(new NodeIdentifierWithPredicates(
265 TestModel.OUTER_LIST_QNAME, QName.create(TestModel.OUTER_LIST_QNAME, "id"), 0));
266 final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
268 final DOMDataTreeProducer shardProducer = dataTreeService.createProducer(
269 Collections.singletonList(outerListPath));
270 final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(outerListPath, executor, 1000);
271 outerListShard.onGlobalContextUpdated(schemaContext);
273 final ListenerRegistration<InMemoryDOMDataTreeShard> oid1ShardRegistration =
274 dataTreeService.registerDataTreeShard(outerListPath, outerListShard, shardProducer);
276 final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
277 final DOMDataTreeWriteCursor cursor =
278 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
279 assertNotNull(cursor);
281 MapNode innerList = ImmutableMapNodeBuilder
283 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
286 cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
288 tx.submit().checkedGet();
290 final ArrayList<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
291 for (int i = 0; i < 1000; i++) {
292 final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
293 for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
294 final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
295 final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(
296 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
297 oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
298 cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
300 futures.add(tx1.submit());
304 futures.get(futures.size() - 1).checkedGet();
308 private static Collection<MapEntryNode> createInnerListMapEntries(final int amount, final String valuePrefix) {
309 final Collection<MapEntryNode> ret = new ArrayList<>();
310 for (int i = 0; i < amount; i++) {
311 ret.add(ImmutableNodes.mapEntryBuilder()
312 .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.INNER_LIST_QNAME,
313 QName.create(TestModel.OUTER_LIST_QNAME, "name"), Integer.toString(i)))
314 .withChild(ImmutableNodes
315 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "name"), Integer.toString(i)))
316 .withChild(ImmutableNodes
317 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "value"), valuePrefix + "-" + i))
325 public void testMultipleProducerCursorCreation() throws Exception {
327 final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
328 DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
329 //check if we can create cursor where the new producer will be
330 DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
331 assertNotNull(rootTxCursor);
332 rootTxCursor.close();
335 rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
336 fail("Should've failed there is still a tx open");
337 } catch (final IllegalStateException e) {
338 assertTrue(e.getMessage().contains("open"));
341 assertTrue(rootTx.cancel());
343 final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
344 Collections.singletonList(INNER_CONTAINER_ID));
346 rootTx = rootProducer.createTransaction(false);
348 rootTx.createCursor(INNER_CONTAINER_ID);
349 fail("Subtree should not be available to this producer");
350 } catch (final IllegalArgumentException e) {
351 assertTrue(e.getMessage().contains("delegated to child producer"));
354 rootTxCursor = rootTx.createCursor(TEST_ID);
355 assertNotNull(rootTxCursor);
357 rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
358 fail("Cursor should not have this subtree available");
359 } catch (final IllegalArgumentException e) {
360 assertTrue(e.getMessage().contains("not available to this cursor"));
364 rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
365 ImmutableContainerNodeBuilder.create()
366 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
368 fail("Cursor should not have this subtree available");
369 } catch (final IllegalArgumentException e) {
370 assertTrue(e.getMessage().contains("not available to this cursor"));
373 final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
374 final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
375 assertNotNull(innerShardCursor);
378 private static ContainerNode createCrossShardContainer() {
379 final LeafNode<String> shardedValue1 =
380 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
381 TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
382 final LeafNode<String> shardedValue2 =
383 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
384 TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
387 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
388 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
389 .withChild(ImmutableLeafNodeBuilder.create()
390 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
391 .withValue("testing-value")
395 final ContainerNode containerNode =
396 ImmutableContainerNodeBuilder.create()
397 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
398 .withChild(shardedValue1)
399 .withChild(shardedValue2)
400 .withChild(lowerShardContainer)
403 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
404 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
405 .withChild(containerNode)
408 return testContainer;
411 //test multiple vertical levels between the shards
413 public void testLargerSubshardSpace() throws Exception {
415 final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(OUTER_LIST_ID, executor, 1, 1);
416 outerListShard.onGlobalContextUpdated(schemaContext);
418 try (final DOMDataTreeProducer producer =
419 dataTreeService.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
420 dataTreeService.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
423 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
424 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
425 final DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
427 assertNotNull(cursor);
428 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer2());
431 tx.submit().checkedGet();
433 final DOMDataTreeListener listener = mock(DOMDataTreeListener.class);
434 doNothing().when(listener).onDataTreeChanged(any(), any());
435 dataTreeService.registerListener(listener,
436 Collections.singletonList(
437 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)),
438 false, Collections.emptyList());
440 verify(listener, times(2)).onDataTreeChanged(any(), any());
445 private static ContainerNode createCrossShardContainer2() {
448 innerListEntry1 = ImmutableNodes
449 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-1")
450 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-1"))
451 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-1"))
453 final MapEntryNode innerListEntry2 = ImmutableNodes
454 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-2")
455 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-2"))
456 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-2"))
459 final MapNode innerList1 = ImmutableNodes
460 .mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(innerListEntry1).build();
461 final MapNode innerList2 = ImmutableNodes
462 .mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(innerListEntry2).build();
464 final MapEntryNode outerListEntry1 = ImmutableNodes
465 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
466 .withChild(innerList1)
468 final MapEntryNode outerListEntry2 = ImmutableNodes
469 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
470 .withChild(innerList2)
473 final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
474 .withChild(outerListEntry1)
475 .withChild(outerListEntry2)
478 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
479 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
480 .withChild(outerList)
483 return testContainer;