2 * Copyright (c) 2014, 2017 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
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.verify;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.AddressFromURIString;
23 import akka.cluster.Cluster;
24 import akka.testkit.javadsl.TestKit;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Throwables;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.util.concurrent.CheckedFuture;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import com.google.common.util.concurrent.MoreExecutors;
31 import com.google.common.util.concurrent.Uninterruptibles;
32 import com.typesafe.config.ConfigFactory;
33 import java.io.IOException;
34 import java.math.BigInteger;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.ExecutionException;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.atomic.AtomicReference;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.junit.runners.Parameterized;
49 import org.junit.runners.Parameterized.Parameter;
50 import org.junit.runners.Parameterized.Parameters;
51 import org.mockito.Mockito;
52 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
53 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
54 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
55 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
56 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
57 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
58 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
59 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
60 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
61 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
62 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
63 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
64 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
65 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
66 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
67 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
68 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
69 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
70 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
71 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
72 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
73 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
74 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
75 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
76 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
77 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
78 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
79 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
80 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
81 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
82 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
83 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
84 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
85 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
86 import org.opendaylight.yangtools.concepts.ListenerRegistration;
87 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
88 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
89 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
90 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
91 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
92 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
93 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
94 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
95 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
96 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
98 @RunWith(Parameterized.class)
99 public class DistributedDataStoreIntegrationTest {
101 @Parameters(name = "{0}")
102 public static Collection<Object[]> data() {
103 return Arrays.asList(new Object[][] {
104 { DistributedDataStore.class }, { ClientBackedDataStore.class }
109 public Class<? extends AbstractDataStore> testParameter;
111 private ActorSystem system;
113 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
114 .shardHeartbeatIntervalInMillis(100);
117 public void setUp() throws IOException {
118 InMemorySnapshotStore.clear();
119 InMemoryJournal.clear();
120 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
121 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
122 Cluster.get(system).join(member1Address);
126 public void tearDown() throws IOException {
127 TestKit.shutdownActorSystem(system, Boolean.TRUE);
131 protected ActorSystem getSystem() {
136 public void testWriteTransactionWithSingleShard() throws Exception {
137 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
139 try (AbstractDataStore dataStore = setupAbstractDataStore(
140 testParameter, "transactionIntegrationTest", "test-1")) {
142 testWriteTransaction(dataStore, TestModel.TEST_PATH,
143 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
145 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
146 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
153 public void testWriteTransactionWithMultipleShards() throws Exception {
154 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
156 try (AbstractDataStore dataStore = setupAbstractDataStore(
157 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
159 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
160 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
162 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
163 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
165 doCommit(writeTx.ready());
167 writeTx = dataStore.newWriteOnlyTransaction();
169 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
170 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
172 doCommit(writeTx.ready());
174 writeTx = dataStore.newWriteOnlyTransaction();
176 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
177 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
178 writeTx.write(carPath, car);
180 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
181 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
182 writeTx.write(personPath, person);
184 doCommit(writeTx.ready());
186 // Verify the data in the store
187 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
189 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
190 assertEquals("isPresent", true, optional.isPresent());
191 assertEquals("Data node", car, optional.get());
193 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
194 assertEquals("isPresent", true, optional.isPresent());
195 assertEquals("Data node", person, optional.get());
202 public void testReadWriteTransactionWithSingleShard() throws Exception {
203 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
205 try (AbstractDataStore dataStore = setupAbstractDataStore(
206 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
208 // 1. Create a read-write Tx
209 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
210 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
212 // 2. Write some data
213 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
214 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
215 readWriteTx.write(nodePath, nodeToWrite);
217 // 3. Read the data from Tx
218 final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
219 assertEquals("exists", true, exists);
221 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
222 assertEquals("isPresent", true, optional.isPresent());
223 assertEquals("Data node", nodeToWrite, optional.get());
225 // 4. Ready the Tx for commit
226 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
231 // 6. Verify the data in the store
232 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
234 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
235 assertEquals("isPresent", true, optional.isPresent());
236 assertEquals("Data node", nodeToWrite, optional.get());
243 public void testReadWriteTransactionWithMultipleShards() throws Exception {
244 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
246 try (AbstractDataStore dataStore = setupAbstractDataStore(
247 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
249 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
250 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
252 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
253 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
255 doCommit(readWriteTx.ready());
257 readWriteTx = dataStore.newReadWriteTransaction();
259 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
260 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
262 doCommit(readWriteTx.ready());
264 readWriteTx = dataStore.newReadWriteTransaction();
266 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
267 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
268 readWriteTx.write(carPath, car);
270 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
271 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
272 readWriteTx.write(personPath, person);
274 final Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
275 assertEquals("exists", true, exists);
277 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
278 assertEquals("isPresent", true, optional.isPresent());
279 assertEquals("Data node", car, optional.get());
281 doCommit(readWriteTx.ready());
283 // Verify the data in the store
284 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
286 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
287 assertEquals("isPresent", true, optional.isPresent());
288 assertEquals("Data node", car, optional.get());
290 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
291 assertEquals("isPresent", true, optional.isPresent());
292 assertEquals("Data node", person, optional.get());
300 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
301 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
303 try (AbstractDataStore dataStore = setupAbstractDataStore(
304 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
306 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
308 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
309 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
310 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
311 doCommit(writeTx.ready());
313 writeTx = txChain.newWriteOnlyTransaction();
316 for (int i = 0; i < numCars; i++) {
317 writeTx.write(CarsModel.newCarPath("car" + i),
318 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
321 doCommit(writeTx.ready());
323 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
324 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
325 assertEquals("isPresent", true, optional.isPresent());
326 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
332 @SuppressWarnings("checkstyle:IllegalCatch")
333 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
335 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
337 final String shardName = "test-1";
339 // Setup the InMemoryJournal to block shard recovery to ensure
341 // initialized until we create and submit the write the Tx.
342 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
343 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
344 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
346 try (AbstractDataStore dataStore = setupAbstractDataStore(
347 testParameter, testName, false, shardName)) {
349 // Create the write Tx
350 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
351 : dataStore.newReadWriteTransaction();
352 assertNotNull("newReadWriteTransaction returned null", writeTx);
354 // Do some modification operations and ready the Tx on a
356 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
357 .builder(TestModel.OUTER_LIST_PATH)
358 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
360 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
361 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
362 final CountDownLatch txReady = new CountDownLatch(1);
363 final Thread txThread = new Thread(() -> {
365 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
367 writeTx.merge(TestModel.OUTER_LIST_PATH,
368 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
370 writeTx.write(listEntryPath,
371 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
373 writeTx.delete(listEntryPath);
375 txCohort.set(writeTx.ready());
376 } catch (Exception e) {
385 // Wait for the Tx operations to complete.
386 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
387 if (caughtEx.get() != null) {
388 throw caughtEx.get();
391 assertEquals("Tx ready", true, done);
393 // At this point the Tx operations should be waiting for the
394 // shard to initialize so
395 // trigger the latch to let the shard recovery to continue.
396 blockRecoveryLatch.countDown();
398 // Wait for the Tx commit to complete.
399 doCommit(txCohort.get());
401 // Verify the data in the store
402 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
404 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
405 assertEquals("isPresent", true, optional.isPresent());
407 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
408 assertEquals("isPresent", true, optional.isPresent());
410 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
411 assertEquals("isPresent", false, optional.isPresent());
418 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
419 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
420 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
424 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
425 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
429 @SuppressWarnings("checkstyle:IllegalCatch")
430 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
431 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
433 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
434 final String shardName = "test-1";
436 // Setup the InMemoryJournal to block shard recovery to ensure
438 // initialized until we create the Tx.
439 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
440 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
441 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
443 try (AbstractDataStore dataStore = setupAbstractDataStore(
444 testParameter, testName, false, shardName)) {
446 // Create the read-write Tx
447 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
448 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
450 // Do some reads on the Tx on a separate thread.
451 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
452 new AtomicReference<>();
453 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
454 txReadFuture = new AtomicReference<>();
455 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
456 final CountDownLatch txReadsDone = new CountDownLatch(1);
457 final Thread txThread = new Thread(() -> {
459 readWriteTx.write(TestModel.TEST_PATH,
460 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
462 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
464 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
465 } catch (Exception e) {
468 txReadsDone.countDown();
474 // Wait for the Tx operations to complete.
475 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
476 if (caughtEx.get() != null) {
477 throw caughtEx.get();
480 assertEquals("Tx reads done", true, done);
482 // At this point the Tx operations should be waiting for the
483 // shard to initialize so
484 // trigger the latch to let the shard recovery to continue.
485 blockRecoveryLatch.countDown();
487 // Wait for the reads to complete and verify.
488 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
489 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
497 @Test(expected = NotInitializedException.class)
498 @SuppressWarnings("checkstyle:IllegalCatch")
499 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
500 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
502 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
503 final String shardName = "test-1";
505 // Set the shard initialization timeout low for the test.
506 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
508 // Setup the InMemoryJournal to block shard recovery
510 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
511 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
512 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
514 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
516 final AbstractDataStore dataStore =
517 setupAbstractDataStore(testParameter, testName, false, shardName);
519 // Create the write Tx
520 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
521 assertNotNull("newReadWriteTransaction returned null", writeTx);
523 // Do some modifications and ready the Tx on a separate
525 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
526 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
527 final CountDownLatch txReady = new CountDownLatch(1);
528 final Thread txThread = new Thread(() -> {
530 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
532 txCohort.set(writeTx.ready());
533 } catch (Exception e) {
542 // Wait for the Tx operations to complete.
543 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
544 if (caughtEx.get() != null) {
545 throw caughtEx.get();
548 assertEquals("Tx ready", true, done);
550 // Wait for the commit to complete. Since the shard never
551 // initialized, the Tx should
552 // have timed out and throw an appropriate exception cause.
554 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
555 fail("Expected NotInitializedException");
556 } catch (final Exception e) {
557 final Throwable root = Throwables.getRootCause(e);
558 Throwables.throwIfUnchecked(root);
559 throw new RuntimeException(root);
561 blockRecoveryLatch.countDown();
567 @Test(expected = NotInitializedException.class)
568 @SuppressWarnings("checkstyle:IllegalCatch")
569 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
570 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
572 final String testName = "testTransactionReadFailureWithShardNotInitialized";
573 final String shardName = "test-1";
575 // Set the shard initialization timeout low for the test.
576 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
578 // Setup the InMemoryJournal to block shard recovery
580 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
581 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
582 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
584 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
586 try (AbstractDataStore dataStore = setupAbstractDataStore(
587 testParameter, testName, false, shardName)) {
589 // Create the read-write Tx
590 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
591 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
593 // Do a read on the Tx on a separate thread.
594 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
595 txReadFuture = new AtomicReference<>();
596 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
597 final CountDownLatch txReadDone = new CountDownLatch(1);
598 final Thread txThread = new Thread(() -> {
600 readWriteTx.write(TestModel.TEST_PATH,
601 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
603 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
606 } catch (Exception e) {
609 txReadDone.countDown();
615 // Wait for the Tx operations to complete.
616 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
617 if (caughtEx.get() != null) {
618 throw caughtEx.get();
621 assertEquals("Tx read done", true, done);
623 // Wait for the read to complete. Since the shard never
624 // initialized, the Tx should
625 // have timed out and throw an appropriate exception cause.
627 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
628 fail("Expected NotInitializedException");
629 } catch (final ReadFailedException e) {
630 final Throwable root = Throwables.getRootCause(e);
631 Throwables.throwIfUnchecked(root);
632 throw new RuntimeException(root);
634 blockRecoveryLatch.countDown();
641 @SuppressWarnings("checkstyle:IllegalCatch")
642 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
644 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
646 final String shardName = "default";
648 // We don't want the shard to become the leader so prevent shard
650 datastoreContextBuilder.customRaftPolicyImplementation(
651 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
653 // The ShardManager uses the election timeout for FindPrimary so
654 // reset it low so it will timeout quickly.
655 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
656 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS).frontendRequestTimeoutInSeconds(2);
658 try (AbstractDataStore dataStore = setupAbstractDataStore(
659 testParameter, testName, false, shardName)) {
661 final Object result = dataStore.getActorContext().executeOperation(
662 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
663 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
665 // Create the write Tx.
666 DOMStoreWriteTransaction writeTxToClose = null;
668 writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction()
669 : dataStore.newReadWriteTransaction();
670 final DOMStoreWriteTransaction writeTx = writeTxToClose;
671 assertNotNull("newReadWriteTransaction returned null", writeTx);
673 // Do some modifications and ready the Tx on a separate
675 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
676 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
677 final CountDownLatch txReady = new CountDownLatch(1);
678 final Thread txThread = new Thread(() -> {
680 writeTx.write(TestModel.JUNK_PATH,
681 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
683 txCohort.set(writeTx.ready());
684 } catch (Exception e) {
693 // Wait for the Tx operations to complete.
694 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
695 if (caughtEx.get() != null) {
696 throw caughtEx.get();
699 assertEquals("Tx ready", true, done);
701 // Wait for the commit to complete. Since no shard
702 // leader was elected in time, the Tx
703 // should have timed out and throw an appropriate
706 txCohort.get().canCommit().get(10, TimeUnit.SECONDS);
707 fail("Expected NoShardLeaderException");
708 } catch (final ExecutionException e) {
709 final String msg = "Unexpected exception: "
710 + Throwables.getStackTraceAsString(e.getCause());
711 if (DistributedDataStore.class.equals(testParameter)) {
712 assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
714 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
719 if (writeTxToClose != null) {
720 writeTxToClose.close();
722 } catch (Exception e) {
723 // FIXME TransactionProxy.close throws IllegalStateException:
724 // Transaction is ready, it cannot be closed
733 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
734 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
735 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
739 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
740 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
744 public void testTransactionAbort() throws Exception {
745 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
747 try (AbstractDataStore dataStore = setupAbstractDataStore(
748 testParameter, "transactionAbortIntegrationTest", "test-1")) {
750 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
751 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
753 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
755 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
757 cohort.canCommit().get(5, TimeUnit.SECONDS);
759 cohort.abort().get(5, TimeUnit.SECONDS);
761 testWriteTransaction(dataStore, TestModel.TEST_PATH,
762 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
769 @SuppressWarnings("checkstyle:IllegalCatch")
770 public void testTransactionChainWithSingleShard() throws Exception {
771 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
773 try (AbstractDataStore dataStore = setupAbstractDataStore(
774 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
776 // 1. Create a Tx chain and write-only Tx
777 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
779 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
780 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
782 // 2. Write some data
783 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
784 writeTx.write(TestModel.TEST_PATH, testNode);
786 // 3. Ready the Tx for commit
787 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
789 // 4. Commit the Tx on another thread that first waits for
790 // the second read Tx.
791 final CountDownLatch continueCommit1 = new CountDownLatch(1);
792 final CountDownLatch commit1Done = new CountDownLatch(1);
793 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
796 continueCommit1.await();
798 } catch (Exception e) {
801 commit1Done.countDown();
805 // 5. Create a new read Tx from the chain to read and verify
806 // the data from the first
807 // Tx is visible after being readied.
808 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
809 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
810 assertEquals("isPresent", true, optional.isPresent());
811 assertEquals("Data node", testNode, optional.get());
813 // 6. Create a new RW Tx from the chain, write more data,
815 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
816 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
817 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
819 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
821 // 7. Create a new read Tx from the chain to read the data
822 // from the last RW Tx to
823 // verify it is visible.
824 readTx = txChain.newReadWriteTransaction();
825 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
826 assertEquals("isPresent", true, optional.isPresent());
827 assertEquals("Data node", outerNode, optional.get());
829 // 8. Wait for the 2 commits to complete and close the
831 continueCommit1.countDown();
832 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
834 if (commit1Error.get() != null) {
835 throw commit1Error.get();
842 // 9. Create a new read Tx from the data store and verify
844 readTx = dataStore.newReadOnlyTransaction();
845 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
846 assertEquals("isPresent", true, optional.isPresent());
847 assertEquals("Data node", outerNode, optional.get());
854 public void testTransactionChainWithMultipleShards() throws Exception {
855 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
857 try (AbstractDataStore dataStore = setupAbstractDataStore(
858 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
860 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
862 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
863 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
865 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
866 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
868 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
869 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
871 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
873 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
875 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
876 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
877 readWriteTx.write(carPath, car);
879 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
880 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
881 readWriteTx.merge(personPath, person);
883 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
884 assertEquals("isPresent", true, optional.isPresent());
885 assertEquals("Data node", car, optional.get());
887 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
888 assertEquals("isPresent", true, optional.isPresent());
889 assertEquals("Data node", person, optional.get());
891 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
893 writeTx = txChain.newWriteOnlyTransaction();
895 writeTx.delete(carPath);
897 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
899 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
900 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
902 doCommit(canCommit1, cohort1);
903 doCommit(canCommit2, cohort2);
908 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
910 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
911 assertEquals("isPresent", false, optional.isPresent());
913 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
914 assertEquals("isPresent", true, optional.isPresent());
915 assertEquals("Data node", person, optional.get());
922 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
923 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
925 try (AbstractDataStore dataStore = setupAbstractDataStore(
926 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
928 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
929 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
930 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
931 MoreExecutors.directExecutor());
933 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
934 DOMTransactionChain txChain = broker.createTransactionChain(listener);
936 final List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
938 final DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
939 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
940 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
941 futures.add(writeTx.submit());
944 for (int i = 0; i < numCars; i++) {
945 final DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
947 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
948 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
950 futures.add(rwTx.submit());
953 for (final CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
957 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
958 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
959 assertEquals("isPresent", true, optional.isPresent());
960 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
971 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
972 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
974 try (AbstractDataStore dataStore = setupAbstractDataStore(
975 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
977 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
979 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
983 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
985 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
986 5, TimeUnit.SECONDS);
987 assertEquals("isPresent", false, optional.isPresent());
996 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
997 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
999 try (AbstractDataStore dataStore = setupAbstractDataStore(
1000 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
1002 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1004 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1005 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
1007 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1009 // Try to create another Tx of each type - each should fail
1010 // b/c the previous Tx wasn't
1012 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1019 public void testCreateChainedTransactionAfterClose() throws Exception {
1020 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1022 try (AbstractDataStore dataStore = setupAbstractDataStore(
1023 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1025 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1028 // Try to create another Tx of each type - should fail b/c
1029 // the previous Tx was closed.
1030 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1037 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1038 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1040 try (AbstractDataStore dataStore = setupAbstractDataStore(
1041 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1043 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1045 // Create a write tx and submit.
1046 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1047 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1048 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1050 // Create read-only tx's and issue a read.
1051 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1052 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1054 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1055 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1057 // Create another write tx and issue the write.
1058 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1059 writeTx2.write(TestModel.OUTER_LIST_PATH,
1060 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1062 // Ensure the reads succeed.
1064 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1065 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1067 // Ensure the writes succeed.
1068 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1073 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1074 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1081 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1082 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1084 try (AbstractDataStore dataStore = setupAbstractDataStore(
1085 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1087 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1088 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1089 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1090 MoreExecutors.directExecutor());
1092 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1093 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1095 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1097 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1098 PeopleModel.emptyContainer());
1100 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1101 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1102 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1104 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1107 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1108 fail("Expected TransactionCommitFailedException");
1109 } catch (final TransactionCommitFailedException e) {
1113 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1114 any(Throwable.class));
1124 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1125 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1127 try (AbstractDataStore dataStore = setupAbstractDataStore(
1128 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1130 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1131 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1132 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1133 MoreExecutors.directExecutor());
1135 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1136 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1138 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1140 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1141 PeopleModel.emptyContainer());
1143 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1144 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1145 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1147 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1149 // Note that merge will validate the data and fail but put
1150 // succeeds b/c deep validation is not
1151 // done for put for performance reasons.
1153 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1154 fail("Expected TransactionCommitFailedException");
1155 } catch (final TransactionCommitFailedException e) {
1159 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1160 any(Throwable.class));
1170 public void testChangeListenerRegistration() throws Exception {
1171 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1173 try (AbstractDataStore dataStore = setupAbstractDataStore(
1174 testParameter, "testChangeListenerRegistration", "test-1")) {
1176 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1177 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1179 final MockDataChangeListener listener = new MockDataChangeListener(1);
1181 final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1182 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1184 assertNotNull("registerChangeListener returned null", listenerReg);
1186 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1187 state -> assertEquals("getDataChangeListenerActors", 1,
1188 state.getDataChangeListenerActors().size()));
1190 // Wait for the initial notification
1191 listener.waitForChangeEvents(TestModel.TEST_PATH);
1195 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1196 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1198 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1199 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1200 testWriteTransaction(dataStore, listPath,
1201 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1203 // Wait for the 2 updates.
1204 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1205 listenerReg.close();
1207 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1208 state -> assertEquals("getDataChangeListenerActors", 0,
1209 state.getDataChangeListenerActors().size()));
1211 testWriteTransaction(dataStore,
1212 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1213 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1214 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1216 listener.expectNoMoreChanges("Received unexpected change after close");
1223 public void testDataTreeChangeListenerRegistration() throws Exception {
1224 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1226 try (AbstractDataStore dataStore = setupAbstractDataStore(
1227 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
1229 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1230 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1232 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1234 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
1235 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
1237 assertNotNull("registerTreeChangeListener returned null", listenerReg);
1239 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1240 state -> assertEquals("getTreeChangeListenerActors", 1,
1241 state.getTreeChangeListenerActors().size()));
1243 // Wait for the initial notification
1244 listener.waitForChangeEvents(TestModel.TEST_PATH);
1248 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1249 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1251 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1252 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1253 testWriteTransaction(dataStore, listPath,
1254 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1256 // Wait for the 2 updates.
1257 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1258 listenerReg.close();
1260 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1261 state -> assertEquals("getTreeChangeListenerActors", 0,
1262 state.getTreeChangeListenerActors().size()));
1264 testWriteTransaction(dataStore,
1265 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1266 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1267 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1269 listener.expectNoMoreChanges("Received unexpected change after close");
1276 public void testRestoreFromDatastoreSnapshot() throws Exception {
1277 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1279 final String name = "transactionIntegrationTest";
1281 final ContainerNode carsNode = CarsModel.newCarsNode(
1282 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1283 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1285 DataTree dataTree = new InMemoryDataTreeFactory().create(
1286 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
1287 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1288 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1290 final Snapshot carsSnapshot = Snapshot.create(
1291 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1292 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1294 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
1295 SchemaContextHelper.full());
1297 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1298 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1300 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1302 final Snapshot peopleSnapshot = Snapshot.create(
1303 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1304 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1306 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1307 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1308 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1310 try (AbstractDataStore dataStore = setupAbstractDataStore(
1311 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1313 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1316 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1317 assertEquals("isPresent", true, optional.isPresent());
1318 assertEquals("Data node", carsNode, optional.get());
1320 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1321 assertEquals("isPresent", true, optional.isPresent());
1322 assertEquals("Data node", peopleNode, optional.get());