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 public void testMultipleWritesIntoSingleMapEntry() throws Exception {
219 final YangInstanceIdentifier oid1 = TestModel.OUTER_LIST_PATH.node(new NodeIdentifierWithPredicates(
220 TestModel.OUTER_LIST_QNAME, QName.create(TestModel.OUTER_LIST_QNAME, "id"), 0));
221 final DOMDataTreeIdentifier outerListPath = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1);
223 final DOMDataTreeProducer shardProducer = dataTreeService.createProducer(
224 Collections.singletonList(outerListPath));
225 final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(outerListPath, executor, 1000);
226 outerListShard.onGlobalContextUpdated(schemaContext);
228 final ListenerRegistration<InMemoryDOMDataTreeShard> oid1ShardRegistration =
229 dataTreeService.registerDataTreeShard(outerListPath, outerListShard, shardProducer);
231 final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
232 final DOMDataTreeWriteCursor cursor =
233 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
234 assertNotNull(cursor);
236 MapNode innerList = ImmutableMapNodeBuilder
238 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
241 cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
243 tx.submit().checkedGet();
245 final ArrayList<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
246 for (int i = 0; i < 1000; i++) {
247 final Collection<MapEntryNode> innerListMapEntries = createInnerListMapEntries(1000, "run-" + i);
248 for (final MapEntryNode innerListMapEntry : innerListMapEntries) {
249 final DOMDataTreeCursorAwareTransaction tx1 = shardProducer.createTransaction(false);
250 final DOMDataTreeWriteCursor cursor1 = tx1.createCursor(
251 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
252 oid1.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))));
253 cursor1.write(innerListMapEntry.getIdentifier(), innerListMapEntry);
255 futures.add(tx1.submit());
259 futures.get(futures.size() - 1).checkedGet();
263 private static Collection<MapEntryNode> createInnerListMapEntries(final int amount, final String valuePrefix) {
264 final Collection<MapEntryNode> ret = new ArrayList<>();
265 for (int i = 0; i < amount; i++) {
266 ret.add(ImmutableNodes.mapEntryBuilder()
267 .withNodeIdentifier(new NodeIdentifierWithPredicates(TestModel.INNER_LIST_QNAME,
268 QName.create(TestModel.OUTER_LIST_QNAME, "name"), Integer.toString(i)))
269 .withChild(ImmutableNodes
270 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "name"), Integer.toString(i)))
271 .withChild(ImmutableNodes
272 .leafNode(QName.create(TestModel.INNER_LIST_QNAME, "value"), valuePrefix + "-" + i))
280 public void testMultipleProducerCursorCreation() throws Exception {
282 final DOMDataTreeProducer rootProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
283 DOMDataTreeCursorAwareTransaction rootTx = rootProducer.createTransaction(false);
284 //check if we can create cursor where the new producer will be
285 DOMDataTreeWriteCursor rootTxCursor = rootTx.createCursor(INNER_CONTAINER_ID);
286 assertNotNull(rootTxCursor);
287 rootTxCursor.close();
290 rootProducer.createProducer(Collections.singletonList(INNER_CONTAINER_ID));
291 fail("Should've failed there is still a tx open");
292 } catch (final IllegalStateException e) {
293 assertTrue(e.getMessage().contains("open"));
296 assertTrue(rootTx.cancel());
298 final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
299 Collections.singletonList(INNER_CONTAINER_ID));
301 rootTx = rootProducer.createTransaction(false);
303 rootTx.createCursor(INNER_CONTAINER_ID);
304 fail("Subtree should not be available to this producer");
305 } catch (final IllegalArgumentException e) {
306 assertTrue(e.getMessage().contains("delegated to child producer"));
309 rootTxCursor = rootTx.createCursor(TEST_ID);
310 assertNotNull(rootTxCursor);
312 rootTxCursor.enter(INNER_CONTAINER_ID.getRootIdentifier().getLastPathArgument());
313 fail("Cursor should not have this subtree available");
314 } catch (final IllegalArgumentException e) {
315 assertTrue(e.getMessage().contains("not available to this cursor"));
319 rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
320 ImmutableContainerNodeBuilder.create()
321 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
323 fail("Cursor should not have this subtree available");
324 } catch (final IllegalArgumentException e) {
325 assertTrue(e.getMessage().contains("not available to this cursor"));
328 final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
329 final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
330 assertNotNull(innerShardCursor);
333 private static ContainerNode createCrossShardContainer() {
334 final LeafNode<String> shardedValue1 =
335 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
336 TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
337 final LeafNode<String> shardedValue2 =
338 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(
339 TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
342 final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
343 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
344 .withChild(ImmutableLeafNodeBuilder.create()
345 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE))
346 .withValue("testing-value")
350 final ContainerNode containerNode =
351 ImmutableContainerNodeBuilder.create()
352 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
353 .withChild(shardedValue1)
354 .withChild(shardedValue2)
355 .withChild(lowerShardContainer)
358 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
359 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
360 .withChild(containerNode)
363 return testContainer;
366 //test multiple vertical levels between the shards
368 public void testLargerSubshardSpace() throws Exception {
370 final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(OUTER_LIST_ID, executor, 1, 1);
371 outerListShard.onGlobalContextUpdated(schemaContext);
373 try (final DOMDataTreeProducer producer =
374 dataTreeService.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
375 dataTreeService.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
378 final DOMDataTreeProducer producer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
379 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
380 final DOMDataTreeWriteCursor cursor = tx.createCursor(ROOT_ID);
382 assertNotNull(cursor);
383 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer2());
386 tx.submit().checkedGet();
388 final DOMDataTreeListener listener = mock(DOMDataTreeListener.class);
389 doNothing().when(listener).onDataTreeChanged(any(), any());
390 dataTreeService.registerListener(listener,
391 Collections.singletonList(
392 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)),
393 false, Collections.emptyList());
395 verify(listener, times(2)).onDataTreeChanged(any(), any());
400 private static ContainerNode createCrossShardContainer2() {
403 innerListEntry1 = ImmutableNodes
404 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-1")
405 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-1"))
406 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-1"))
408 final MapEntryNode innerListEntry2 = ImmutableNodes
409 .mapEntryBuilder(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "name-2")
410 .withChild(ImmutableNodes.leafNode(TestModel.NAME_QNAME, "name-2"))
411 .withChild(ImmutableNodes.leafNode(TestModel.VALUE_QNAME, "value-2"))
414 final MapNode innerList1 = ImmutableNodes
415 .mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(innerListEntry1).build();
416 final MapNode innerList2 = ImmutableNodes
417 .mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(innerListEntry2).build();
419 final MapEntryNode outerListEntry1 = ImmutableNodes
420 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
421 .withChild(innerList1)
423 final MapEntryNode outerListEntry2 = ImmutableNodes
424 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
425 .withChild(innerList2)
428 final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
429 .withChild(outerListEntry1)
430 .withChild(outerListEntry2)
433 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
434 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
435 .withChild(outerList)
438 return testContainer;