Fix InMemory shard transaction chaining.
[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.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;
20
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;
26 import java.util.Map;
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;
63
64 public class ShardedDOMDataTreeTest {
65
66     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTreeProducerMultiShardTest.class);
67
68     private static SchemaContext schemaContext = null;
69
70     static {
71         try {
72             schemaContext = TestModel.createTestContext();
73         } catch (final ReactorException e) {
74             LOG.error("Unable to create schema context for TestModel", e);
75         }
76     }
77
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);
82
83     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID =
84             new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
85
86     private InMemoryDOMDataTreeShard rootShard;
87
88     private ShardedDOMDataTree dataTreeService;
89     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
90
91     private final ExecutorService executor = Executors.newSingleThreadExecutor();
92
93     @Captor
94     private ArgumentCaptor<Collection<DataTreeCandidate>> captorForChanges;
95     @Captor
96     private ArgumentCaptor<Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>>> captorForSubtrees;
97
98     private final ContainerNode crossShardContainer = createCrossShardContainer();
99
100     @Before
101     public void setUp() throws Exception {
102         MockitoAnnotations.initMocks(this);
103
104         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
105         rootShard.onGlobalContextUpdated(schemaContext);
106
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();
111
112         dataTreeService = dataTree;
113     }
114
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));
119     }
120
121     @Test
122     public void testShardRegistrationClose() throws Exception {
123         rootShardReg.close();
124
125         final InMemoryDOMDataTreeShard newRootShard = InMemoryDOMDataTreeShard.create(ROOT_ID, executor, 1);
126         newRootShard.onGlobalContextUpdated(schemaContext);
127         final DOMDataTreeProducer shardRegProducer = dataTreeService.createProducer(Collections.singletonList(ROOT_ID));
128
129         final ListenerRegistration<InMemoryDOMDataTreeShard> newRootShardReg =
130                 dataTreeService.registerDataTreeShard(ROOT_ID, rootShard, shardRegProducer);
131         shardRegProducer.close();
132
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);
139
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);
146
147         cursor.close();
148         tx.cancel();
149         shardRegProducer2.close();
150
151         innerShardReg.close();
152         newRootShardReg.close();
153     }
154
155     @Test
156     public void testSingleShardWrite() throws Exception {
157         final DOMDataTreeListener mockedDataTreeListener = Mockito.mock(DOMDataTreeListener.class);
158         doNothing().when(mockedDataTreeListener).onDataTreeChanged(anyCollection(), anyMap());
159
160         dataTreeService.registerListener(mockedDataTreeListener, Collections.singletonList(INNER_CONTAINER_ID),
161                 true, Collections.emptyList());
162
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);
167
168         cursor.write(TEST_ID.getRootIdentifier().getLastPathArgument(), crossShardContainer);
169
170         try {
171             tx.submit().checkedGet();
172             fail("There's still an open cursor");
173         } catch (final IllegalStateException e) {
174             assertTrue(e.getMessage().contains("cursor open"));
175         }
176
177         cursor.close();
178         tx.submit().checkedGet();
179
180         tx = producer.createTransaction(false);
181         cursor = tx.createCursor(TEST_ID);
182         assertNotNull(cursor);
183
184         cursor.delete(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
185         cursor.close();
186         tx.submit().checkedGet();
187
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);
192
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);
198
199         verifyNoMoreInteractions(mockedDataTreeListener);
200     }
201
202     @Test
203     public void testMultipleWritesIntoSingleMapEntry() throws Exception {
204
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);
208
209         final DOMDataTreeProducer shardProducer = dataTreeService.createProducer(
210                 Collections.singletonList(outerListPath));
211         final InMemoryDOMDataTreeShard outerListShard = InMemoryDOMDataTreeShard.create(outerListPath, executor, 1000);
212         outerListShard.onGlobalContextUpdated(schemaContext);
213
214         final ListenerRegistration<InMemoryDOMDataTreeShard> oid1ShardRegistration =
215                 dataTreeService.registerDataTreeShard(outerListPath, outerListShard, shardProducer);
216
217         final DOMDataTreeCursorAwareTransaction tx = shardProducer.createTransaction(false);
218         final DOMDataTreeWriteCursor cursor =
219                 tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, oid1));
220         assertNotNull(cursor);
221
222         MapNode innerList = ImmutableMapNodeBuilder
223                 .create()
224                 .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
225                 .build();
226
227         cursor.write(new NodeIdentifier(TestModel.INNER_LIST_QNAME), innerList);
228         cursor.close();
229         tx.submit().checkedGet();
230
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);
240                 cursor1.close();
241                 futures.add(tx1.submit());
242             }
243         }
244
245         futures.get(futures.size() - 1).checkedGet();
246
247     }
248
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))
259                     .build());
260         }
261
262         return ret;
263     }
264
265     @Test
266     public void testMultipleProducerCursorCreation() throws Exception {
267
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();
274
275         try {
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"));
280         }
281
282         assertTrue(rootTx.cancel());
283
284         final DOMDataTreeProducer innerContainerProducer = rootProducer.createProducer(
285                 Collections.singletonList(INNER_CONTAINER_ID));
286
287         rootTx = rootProducer.createTransaction(false);
288         try {
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"));
293         }
294
295         rootTxCursor = rootTx.createCursor(TEST_ID);
296         assertNotNull(rootTxCursor);
297         try {
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"));
302         }
303
304         try {
305             rootTxCursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(),
306                     ImmutableContainerNodeBuilder.create()
307                             .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
308                             .build());
309             fail("Cursor should not have this subtree available");
310         } catch (final IllegalArgumentException e) {
311             assertTrue(e.getMessage().contains("not available to this cursor"));
312         }
313
314         final DOMDataTreeCursorAwareTransaction innerShardTx = innerContainerProducer.createTransaction(false);
315         final DOMDataTreeWriteCursor innerShardCursor = innerShardTx.createCursor(INNER_CONTAINER_ID);
316         assertNotNull(innerShardCursor);
317     }
318
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();
326
327
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")
333                         .build())
334                 .build();
335
336         final ContainerNode containerNode =
337                 ImmutableContainerNodeBuilder.create()
338                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
339                         .withChild(shardedValue1)
340                         .withChild(shardedValue2)
341                         .withChild(lowerShardContainer)
342                         .build();
343
344         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
345                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
346                 .withChild(containerNode)
347                 .build();
348
349         return testContainer;
350     }
351 }