BUG-3128: Extract abstract shard implementations.
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
23
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;
29 import java.util.Map;
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;
65
66 public class ShardedDOMDataTreeTest {
67
68     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
69
70     private static SchemaContext schemaContext = null;
71
72     static {
73         try {
74             schemaContext = TestModel.createTestContext();
75         } catch (final ReactorException e) {
76             LOG.error("Unable to create schema context for TestModel", e);
77         }
78     }
79
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);
84
85     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
86             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.INNER_CONTAINER_PATH);
87
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);
92
93     private InMemoryDOMDataTreeShard rootShard;
94
95     private ShardedDOMDataTree dataTreeService;
96     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
97
98     private final ExecutorService executor = Executors.newSingleThreadExecutor();
99
100     @Captor
101     private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
102     @Captor
103     private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
104
105     private final ContainerNode crossShardContainer = createCrossShardContainer();
106
107     @Before
108     public void setUp() throws Exception {
109         MockitoAnnotations.initMocks(this);
110
111         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
112         rootShard.onGlobalContextUpdated(schemaContext);
113
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();
118
119         dataTreeService = dataTree;
120     }
121
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));
126     }
127
128     @Test
129     public void testShardRegistrationClose() throws Exception {
130         rootShardReg.close();
131
132         final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
133         newRootShard.onGlobalContextUpdated(schemaContext);
134         final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
135
136         final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
137                 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
138         shardRegProducer.close();
139
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);
146
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);
153
154         cursor.close();
155         tx.cancel();
156         shardRegProducer2.close();
157
158         innerShardReg.close();
159         newRootShardReg.close();
160     }
161
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);
167     }
168
169     @Test
170     public void testSingleShardWrite() throws Exception {
171         final DOMDataTreeListener mockedDataTreeListener = mock(DOMDataTreeListener.class);
172         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
173
174         dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID),
175                 true, Collections.emptyList());
176
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);
181
182         cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
183
184         try {
185             tx.submit().checkedGet();
186             fail("There's still an open cursor");
187         } catch (final IllegalStateException e) {
188             assertTrue(e.getMessage().contains("cursor open"));
189         }
190
191         cursor.close();
192         tx.submit().checkedGet();
193
194         tx = producer.createTransaction(false);
195         cursor = tx.createCursor(TEST_ID);
196         assertNotNull(cursor);
197
198         cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
199         cursor.close();
200         tx.submit().checkedGet();
201
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);
206
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);
212
213         verifyNoMoreInteractions(mockedDataTreeListener);
214     }
215
216     @Test
217     public void testMultipleWritesIntoSingleMapEntry() throws Exception {
218
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);
222
223         final DOMDataTreeProducer shardProducer = dataTreeService.createProducer(
224                 Collections.singletonList(outerListPath));
225         final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(outerListPath, executor, 1000);
226         outerListShard.onGlobalContextUpdated(schemaContext);
227
228         final ListenerRegistration<InMemoryDOMDataTreeShard> oid1ShardRegistration =
229                 dataTreeService.registerDataTreeShard(outerListPath, outerListShard, shardProducer);
230
231         final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
232         final DOMDataTreeWriteCursor cursor =
233                 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
234         assertNotNull(cursor);
235
236         MapNode innerList = ImmutableMapNodeBuilder
237                 .create()
238                 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
239                 .build();
240
241         cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
242         cursor.close();
243         tx.submit().checkedGet();
244
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);
254                 cursor1.close();
255                 futures.add(tx1.submit());
256             }
257         }
258
259         futures.get(futures.size() - 1).checkedGet();
260
261     }
262
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))
273                     .build());
274         }
275
276         return ret;
277     }
278
279     @Test
280     public void testMultipleProducerCursorCreation() throws Exception {
281
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();
288
289         try {
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"));
294         }
295
296         assertTrue(rootTx.cancel());
297
298         final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
299                 Collections.singletonList(INNER_CONTAINER_ID));
300
301         rootTx = rootProducer.createTransaction(false);
302         try {
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"));
307         }
308
309         rootTxCursor = rootTx.createCursor(TEST_ID);
310         assertNotNull(rootTxCursor);
311         try {
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"));
316         }
317
318         try {
319             rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
320                     ImmutableContainerNodeBuilder.create()
321                             .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
322                             .build());
323             fail("Cursor should not have this subtree available");
324         } catch (final IllegalArgumentException e) {
325             assertTrue(e.getMessage().contains("not available to this cursor"));
326         }
327
328         final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
329         final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
330         assertNotNull(innerShardCursor);
331     }
332
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();
340
341
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")
347                         .build())
348                 .build();
349
350         final ContainerNode containerNode =
351                 ImmutableContainerNodeBuilder.create()
352                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
353                         .withChild(shardedValue1)
354                         .withChild(shardedValue2)
355                         .withChild(lowerShardContainer)
356                         .build();
357
358         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
359                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
360                 .withChild(containerNode)
361                 .build();
362
363         return testContainer;
364     }
365
366     //test multiple vertical levels between the shards
367     @Test
368     public void testLargerSubshardSpace() throws Exception {
369
370         final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(OUTER_LIST_ID, executor, 1, 1);
371         outerListShard.onGlobalContextUpdated(schemaContext);
372
373         try (final DOMDataTreeProducer producer =
374                      dataTreeService.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
375             dataTreeService.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
376         }
377
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);
381
382         assertNotNull(cursor);
383         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer2());
384         cursor.close();
385
386         tx.submit().checkedGet();
387
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());
394
395         verify(listener, times(2)).onDataTreeChanged(any(), any());
396
397
398     }
399
400     private static ContainerNode createCrossShardContainer2() {
401
402         final MapEntryNode
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"))
407                 .build();
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"))
412                 .build();
413
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();
418
419         final MapEntryNode outerListEntry1 = ImmutableNodes
420                 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1)
421                 .withChild(innerList1)
422                 .build();
423         final MapEntryNode outerListEntry2 = ImmutableNodes
424                 .mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2)
425                 .withChild(innerList2)
426                 .build();
427
428         final MapNode outerList = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
429                 .withChild(outerListEntry1)
430                 .withChild(outerListEntry2)
431                 .build();
432
433         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
434                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
435                 .withChild(outerList)
436                 .build();
437
438         return testContainer;
439     }
440 }