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.Throwables;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.common.util.concurrent.FluentFuture;
28 import com.google.common.util.concurrent.ListenableFuture;
29 import com.google.common.util.concurrent.MoreExecutors;
30 import com.google.common.util.concurrent.Uninterruptibles;
31 import com.typesafe.config.ConfigFactory;
32 import java.math.BigInteger;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Optional;
39 import java.util.concurrent.CountDownLatch;
40 import java.util.concurrent.ExecutionException;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.atomic.AtomicReference;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.junit.runners.Parameterized;
48 import org.junit.runners.Parameterized.Parameter;
49 import org.junit.runners.Parameterized.Parameters;
50 import org.mockito.Mockito;
51 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
52 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
53 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
54 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
55 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
56 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
57 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
58 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
59 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
60 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
61 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
62 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
63 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
64 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
65 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
66 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
67 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
68 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
69 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
70 import org.opendaylight.mdsal.common.api.ReadFailedException;
71 import org.opendaylight.mdsal.common.api.TransactionChainClosedException;
72 import org.opendaylight.mdsal.common.api.TransactionChainListener;
73 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
74 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
75 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
76 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
77 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
78 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
79 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
80 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
81 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
82 import org.opendaylight.yangtools.concepts.ListenerRegistration;
83 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
84 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
89 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
90 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
91 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
92 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
94 @RunWith(Parameterized.class)
95 public class DistributedDataStoreIntegrationTest {
97 @Parameters(name = "{0}")
98 public static Collection<Object[]> data() {
99 return Arrays.asList(new Object[][] {
100 { DistributedDataStore.class }, { ClientBackedDataStore.class }
105 public Class<? extends AbstractDataStore> testParameter;
107 private ActorSystem system;
109 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
110 .shardHeartbeatIntervalInMillis(100);
113 public void setUp() {
114 InMemorySnapshotStore.clear();
115 InMemoryJournal.clear();
116 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
117 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
118 Cluster.get(system).join(member1Address);
122 public void tearDown() {
123 TestKit.shutdownActorSystem(system, Boolean.TRUE);
127 protected ActorSystem getSystem() {
132 public void testWriteTransactionWithSingleShard() throws Exception {
133 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
135 try (AbstractDataStore dataStore = setupAbstractDataStore(
136 testParameter, "transactionIntegrationTest", "test-1")) {
138 testWriteTransaction(dataStore, TestModel.TEST_PATH,
139 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
141 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
142 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
149 public void testWriteTransactionWithMultipleShards() throws Exception {
150 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
152 try (AbstractDataStore dataStore = setupAbstractDataStore(
153 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
155 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
156 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
158 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
159 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
161 doCommit(writeTx.ready());
163 writeTx = dataStore.newWriteOnlyTransaction();
165 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
166 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
168 doCommit(writeTx.ready());
170 writeTx = dataStore.newWriteOnlyTransaction();
172 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
173 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
174 writeTx.write(carPath, car);
176 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
177 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
178 writeTx.write(personPath, person);
180 doCommit(writeTx.ready());
182 // Verify the data in the store
183 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
185 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
186 assertEquals("isPresent", true, optional.isPresent());
187 assertEquals("Data node", car, optional.get());
189 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
190 assertEquals("isPresent", true, optional.isPresent());
191 assertEquals("Data node", person, optional.get());
198 public void testReadWriteTransactionWithSingleShard() throws Exception {
199 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
201 try (AbstractDataStore dataStore = setupAbstractDataStore(
202 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
204 // 1. Create a read-write Tx
205 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
206 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
208 // 2. Write some data
209 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
210 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
211 readWriteTx.write(nodePath, nodeToWrite);
213 // 3. Read the data from Tx
214 final Boolean exists = readWriteTx.exists(nodePath).get(5, TimeUnit.SECONDS);
215 assertEquals("exists", true, exists);
217 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
218 assertEquals("isPresent", true, optional.isPresent());
219 assertEquals("Data node", nodeToWrite, optional.get());
221 // 4. Ready the Tx for commit
222 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
227 // 6. Verify the data in the store
228 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
230 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
231 assertEquals("isPresent", true, optional.isPresent());
232 assertEquals("Data node", nodeToWrite, optional.get());
239 public void testReadWriteTransactionWithMultipleShards() throws Exception {
240 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
242 try (AbstractDataStore dataStore = setupAbstractDataStore(
243 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
245 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
246 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
248 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
249 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
251 doCommit(readWriteTx.ready());
253 readWriteTx = dataStore.newReadWriteTransaction();
255 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
256 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
258 doCommit(readWriteTx.ready());
260 readWriteTx = dataStore.newReadWriteTransaction();
262 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
263 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
264 readWriteTx.write(carPath, car);
266 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
267 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
268 readWriteTx.write(personPath, person);
270 final Boolean exists = readWriteTx.exists(carPath).get(5, TimeUnit.SECONDS);
271 assertEquals("exists", true, exists);
273 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
274 assertEquals("isPresent", true, optional.isPresent());
275 assertEquals("Data node", car, optional.get());
277 doCommit(readWriteTx.ready());
279 // Verify the data in the store
280 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
282 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
283 assertEquals("isPresent", true, optional.isPresent());
284 assertEquals("Data node", car, optional.get());
286 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
287 assertEquals("isPresent", true, optional.isPresent());
288 assertEquals("Data node", person, optional.get());
296 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
297 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
299 try (AbstractDataStore dataStore = setupAbstractDataStore(
300 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
302 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
304 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
305 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
306 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
307 doCommit(writeTx.ready());
309 writeTx = txChain.newWriteOnlyTransaction();
312 for (int i = 0; i < numCars; i++) {
313 writeTx.write(CarsModel.newCarPath("car" + i),
314 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
317 doCommit(writeTx.ready());
319 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
320 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
321 assertEquals("isPresent", true, optional.isPresent());
322 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
328 @SuppressWarnings("checkstyle:IllegalCatch")
329 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
331 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
333 final String shardName = "test-1";
335 // Setup the InMemoryJournal to block shard recovery to ensure
337 // initialized until we create and submit the write the Tx.
338 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
339 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
340 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
342 try (AbstractDataStore dataStore = setupAbstractDataStore(
343 testParameter, testName, false, shardName)) {
345 // Create the write Tx
346 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
347 : dataStore.newReadWriteTransaction();
348 assertNotNull("newReadWriteTransaction returned null", writeTx);
350 // Do some modification operations and ready the Tx on a
352 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
353 .builder(TestModel.OUTER_LIST_PATH)
354 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
356 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
357 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
358 final CountDownLatch txReady = new CountDownLatch(1);
359 final Thread txThread = new Thread(() -> {
361 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
363 writeTx.merge(TestModel.OUTER_LIST_PATH,
364 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
366 writeTx.write(listEntryPath,
367 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
369 writeTx.delete(listEntryPath);
371 txCohort.set(writeTx.ready());
372 } catch (Exception e) {
381 // Wait for the Tx operations to complete.
382 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
383 if (caughtEx.get() != null) {
384 throw caughtEx.get();
387 assertEquals("Tx ready", true, done);
389 // At this point the Tx operations should be waiting for the
390 // shard to initialize so
391 // trigger the latch to let the shard recovery to continue.
392 blockRecoveryLatch.countDown();
394 // Wait for the Tx commit to complete.
395 doCommit(txCohort.get());
397 // Verify the data in the store
398 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
400 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
401 assertEquals("isPresent", true, optional.isPresent());
403 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
404 assertEquals("isPresent", true, optional.isPresent());
406 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
407 assertEquals("isPresent", false, optional.isPresent());
414 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
415 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
416 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
420 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
421 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
425 @SuppressWarnings("checkstyle:IllegalCatch")
426 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
427 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
429 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
430 final String shardName = "test-1";
432 // Setup the InMemoryJournal to block shard recovery to ensure
434 // initialized until we create the Tx.
435 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
436 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
437 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
439 try (AbstractDataStore dataStore = setupAbstractDataStore(
440 testParameter, testName, false, shardName)) {
442 // Create the read-write Tx
443 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
444 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
446 // Do some reads on the Tx on a separate thread.
447 final AtomicReference<FluentFuture<Boolean>> txExistsFuture = new AtomicReference<>();
448 final AtomicReference<FluentFuture<Optional<NormalizedNode<?, ?>>>>
449 txReadFuture = new AtomicReference<>();
450 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
451 final CountDownLatch txReadsDone = new CountDownLatch(1);
452 final Thread txThread = new Thread(() -> {
454 readWriteTx.write(TestModel.TEST_PATH,
455 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
457 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
459 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
460 } catch (Exception e) {
463 txReadsDone.countDown();
469 // Wait for the Tx operations to complete.
470 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
471 if (caughtEx.get() != null) {
472 throw caughtEx.get();
475 assertEquals("Tx reads done", true, done);
477 // At this point the Tx operations should be waiting for the
478 // shard to initialize so
479 // trigger the latch to let the shard recovery to continue.
480 blockRecoveryLatch.countDown();
482 // Wait for the reads to complete and verify.
483 assertEquals("exists", true, txExistsFuture.get().get(5, TimeUnit.SECONDS));
484 assertEquals("read", true, txReadFuture.get().get(5, TimeUnit.SECONDS).isPresent());
492 @Test(expected = NotInitializedException.class)
493 @SuppressWarnings("checkstyle:IllegalCatch")
494 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
495 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
497 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
498 final String shardName = "test-1";
500 // Set the shard initialization timeout low for the test.
501 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
503 // Setup the InMemoryJournal to block shard recovery
505 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
506 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
507 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
509 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
511 final AbstractDataStore dataStore =
512 setupAbstractDataStore(testParameter, testName, false, shardName);
514 // Create the write Tx
515 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
516 assertNotNull("newReadWriteTransaction returned null", writeTx);
518 // Do some modifications and ready the Tx on a separate
520 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
521 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
522 final CountDownLatch txReady = new CountDownLatch(1);
523 final Thread txThread = new Thread(() -> {
525 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
527 txCohort.set(writeTx.ready());
528 } catch (Exception e) {
537 // Wait for the Tx operations to complete.
538 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
539 if (caughtEx.get() != null) {
540 throw caughtEx.get();
543 assertEquals("Tx ready", true, done);
545 // Wait for the commit to complete. Since the shard never
546 // initialized, the Tx should
547 // have timed out and throw an appropriate exception cause.
549 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
550 fail("Expected NotInitializedException");
551 } catch (final Exception e) {
552 final Throwable root = Throwables.getRootCause(e);
553 Throwables.throwIfUnchecked(root);
554 throw new RuntimeException(root);
556 blockRecoveryLatch.countDown();
562 @Test(expected = NotInitializedException.class)
563 @SuppressWarnings("checkstyle:IllegalCatch")
564 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
565 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
567 final String testName = "testTransactionReadFailureWithShardNotInitialized";
568 final String shardName = "test-1";
570 // Set the shard initialization timeout low for the test.
571 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
573 // Setup the InMemoryJournal to block shard recovery
575 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
576 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
577 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
579 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
581 try (AbstractDataStore dataStore = setupAbstractDataStore(
582 testParameter, testName, false, shardName)) {
584 // Create the read-write Tx
585 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
586 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
588 // Do a read on the Tx on a separate thread.
589 final AtomicReference<FluentFuture<Optional<NormalizedNode<?, ?>>>>
590 txReadFuture = new AtomicReference<>();
591 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
592 final CountDownLatch txReadDone = new CountDownLatch(1);
593 final Thread txThread = new Thread(() -> {
595 readWriteTx.write(TestModel.TEST_PATH,
596 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
598 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
601 } catch (Exception e) {
604 txReadDone.countDown();
610 // Wait for the Tx operations to complete.
611 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
612 if (caughtEx.get() != null) {
613 throw caughtEx.get();
616 assertEquals("Tx read done", true, done);
618 // Wait for the read to complete. Since the shard never
619 // initialized, the Tx should
620 // have timed out and throw an appropriate exception cause.
622 txReadFuture.get().get(5, TimeUnit.SECONDS);
623 } catch (ExecutionException e) {
624 assertTrue("Expected ReadFailedException cause: " + e.getCause(),
625 e.getCause() instanceof ReadFailedException);
626 final Throwable root = Throwables.getRootCause(e);
627 Throwables.throwIfUnchecked(root);
628 throw new RuntimeException(root);
630 blockRecoveryLatch.countDown();
637 @SuppressWarnings("checkstyle:IllegalCatch")
638 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
640 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
642 final String shardName = "default";
644 // We don't want the shard to become the leader so prevent shard
646 datastoreContextBuilder.customRaftPolicyImplementation(
647 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
649 // The ShardManager uses the election timeout for FindPrimary so
650 // reset it low so it will timeout quickly.
651 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
652 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS).frontendRequestTimeoutInSeconds(2);
654 try (AbstractDataStore dataStore = setupAbstractDataStore(
655 testParameter, testName, false, shardName)) {
657 final Object result = dataStore.getActorContext().executeOperation(
658 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
659 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
661 // Create the write Tx.
662 DOMStoreWriteTransaction writeTxToClose = null;
664 writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction()
665 : dataStore.newReadWriteTransaction();
666 final DOMStoreWriteTransaction writeTx = writeTxToClose;
667 assertNotNull("newReadWriteTransaction returned null", writeTx);
669 // Do some modifications and ready the Tx on a separate
671 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
672 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
673 final CountDownLatch txReady = new CountDownLatch(1);
674 final Thread txThread = new Thread(() -> {
676 writeTx.write(TestModel.JUNK_PATH,
677 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
679 txCohort.set(writeTx.ready());
680 } catch (Exception e) {
689 // Wait for the Tx operations to complete.
690 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
691 if (caughtEx.get() != null) {
692 throw caughtEx.get();
695 assertEquals("Tx ready", true, done);
697 // Wait for the commit to complete. Since no shard
698 // leader was elected in time, the Tx
699 // should have timed out and throw an appropriate
702 txCohort.get().canCommit().get(10, TimeUnit.SECONDS);
703 fail("Expected NoShardLeaderException");
704 } catch (final ExecutionException e) {
705 final String msg = "Unexpected exception: "
706 + Throwables.getStackTraceAsString(e.getCause());
707 if (DistributedDataStore.class.equals(testParameter)) {
708 assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
710 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
715 if (writeTxToClose != null) {
716 writeTxToClose.close();
718 } catch (Exception e) {
719 // FIXME TransactionProxy.close throws IllegalStateException:
720 // Transaction is ready, it cannot be closed
729 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
730 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
731 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
735 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
736 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
740 public void testTransactionAbort() throws Exception {
741 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
743 try (AbstractDataStore dataStore = setupAbstractDataStore(
744 testParameter, "transactionAbortIntegrationTest", "test-1")) {
746 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
747 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
749 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
751 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
753 cohort.canCommit().get(5, TimeUnit.SECONDS);
755 cohort.abort().get(5, TimeUnit.SECONDS);
757 testWriteTransaction(dataStore, TestModel.TEST_PATH,
758 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
765 @SuppressWarnings("checkstyle:IllegalCatch")
766 public void testTransactionChainWithSingleShard() throws Exception {
767 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
769 try (AbstractDataStore dataStore = setupAbstractDataStore(
770 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
772 // 1. Create a Tx chain and write-only Tx
773 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
775 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
776 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
778 // 2. Write some data
779 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
780 writeTx.write(TestModel.TEST_PATH, testNode);
782 // 3. Ready the Tx for commit
783 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
785 // 4. Commit the Tx on another thread that first waits for
786 // the second read Tx.
787 final CountDownLatch continueCommit1 = new CountDownLatch(1);
788 final CountDownLatch commit1Done = new CountDownLatch(1);
789 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
792 continueCommit1.await();
794 } catch (Exception e) {
797 commit1Done.countDown();
801 // 5. Create a new read Tx from the chain to read and verify
802 // the data from the first
803 // Tx is visible after being readied.
804 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
805 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
806 assertEquals("isPresent", true, optional.isPresent());
807 assertEquals("Data node", testNode, optional.get());
809 // 6. Create a new RW Tx from the chain, write more data,
811 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
812 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
813 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
815 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
817 // 7. Create a new read Tx from the chain to read the data
818 // from the last RW Tx to
819 // verify it is visible.
820 readTx = txChain.newReadWriteTransaction();
821 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
822 assertEquals("isPresent", true, optional.isPresent());
823 assertEquals("Data node", outerNode, optional.get());
825 // 8. Wait for the 2 commits to complete and close the
827 continueCommit1.countDown();
828 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
830 if (commit1Error.get() != null) {
831 throw commit1Error.get();
838 // 9. Create a new read Tx from the data store and verify
840 readTx = dataStore.newReadOnlyTransaction();
841 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
842 assertEquals("isPresent", true, optional.isPresent());
843 assertEquals("Data node", outerNode, optional.get());
850 public void testTransactionChainWithMultipleShards() throws Exception {
851 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
853 try (AbstractDataStore dataStore = setupAbstractDataStore(
854 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
856 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
858 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
859 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
861 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
862 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
864 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
865 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
867 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
869 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
871 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
872 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
873 readWriteTx.write(carPath, car);
875 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
876 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
877 readWriteTx.merge(personPath, person);
879 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
880 assertEquals("isPresent", true, optional.isPresent());
881 assertEquals("Data node", car, optional.get());
883 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
884 assertEquals("isPresent", true, optional.isPresent());
885 assertEquals("Data node", person, optional.get());
887 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
889 writeTx = txChain.newWriteOnlyTransaction();
891 writeTx.delete(carPath);
893 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
895 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
896 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
898 doCommit(canCommit1, cohort1);
899 doCommit(canCommit2, cohort2);
904 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
906 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
907 assertEquals("isPresent", false, optional.isPresent());
909 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
910 assertEquals("isPresent", true, optional.isPresent());
911 assertEquals("Data node", person, optional.get());
918 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
919 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
921 try (AbstractDataStore dataStore = setupAbstractDataStore(
922 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
924 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
925 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
926 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
927 MoreExecutors.directExecutor());
929 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
930 DOMTransactionChain txChain = broker.createTransactionChain(listener);
932 final List<ListenableFuture<?>> futures = new ArrayList<>();
934 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
935 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
936 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
937 futures.add(writeTx.commit());
940 for (int i = 0; i < numCars; i++) {
941 final DOMDataTreeReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
943 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
944 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
946 futures.add(rwTx.commit());
949 for (final ListenableFuture<?> f : futures) {
950 f.get(5, TimeUnit.SECONDS);
953 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
954 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
955 assertEquals("isPresent", true, optional.isPresent());
956 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
967 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
968 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
970 try (AbstractDataStore dataStore = setupAbstractDataStore(
971 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
973 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
975 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
979 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
981 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
982 5, TimeUnit.SECONDS);
983 assertEquals("isPresent", false, optional.isPresent());
992 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
993 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
995 try (AbstractDataStore dataStore = setupAbstractDataStore(
996 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
998 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1000 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1001 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
1003 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1005 // Try to create another Tx of each type - each should fail
1006 // b/c the previous Tx wasn't
1008 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1015 public void testCreateChainedTransactionAfterClose() throws Exception {
1016 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1018 try (AbstractDataStore dataStore = setupAbstractDataStore(
1019 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1021 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1024 // Try to create another Tx of each type - should fail b/c
1025 // the previous Tx was closed.
1026 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1033 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1034 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1036 try (AbstractDataStore dataStore = setupAbstractDataStore(
1037 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1039 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1041 // Create a write tx and submit.
1042 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1043 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1044 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1046 // Create read-only tx's and issue a read.
1047 FluentFuture<Optional<NormalizedNode<?, ?>>> readFuture1 = txChain
1048 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1050 FluentFuture<Optional<NormalizedNode<?, ?>>> readFuture2 = txChain
1051 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1053 // Create another write tx and issue the write.
1054 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1055 writeTx2.write(TestModel.OUTER_LIST_PATH,
1056 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1058 // Ensure the reads succeed.
1060 assertEquals("isPresent", true, readFuture1.get(5, TimeUnit.SECONDS).isPresent());
1061 assertEquals("isPresent", true, readFuture2.get(5, TimeUnit.SECONDS).isPresent());
1063 // Ensure the writes succeed.
1064 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1069 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1070 .get(5, TimeUnit.SECONDS).isPresent());
1077 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1078 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1080 try (AbstractDataStore dataStore = setupAbstractDataStore(
1081 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1083 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1084 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1085 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1086 MoreExecutors.directExecutor());
1088 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1089 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1091 final DOMDataTreeReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1093 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1094 PeopleModel.emptyContainer());
1096 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1097 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1098 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1100 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1103 writeTx.commit().get(5, TimeUnit.SECONDS);
1104 fail("Expected TransactionCommitFailedException");
1105 } catch (final ExecutionException e) {
1109 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1110 any(Throwable.class));
1120 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1121 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1123 try (AbstractDataStore dataStore = setupAbstractDataStore(
1124 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1126 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1127 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1128 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1129 MoreExecutors.directExecutor());
1131 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1132 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1134 final DOMDataTreeWriteTransaction writeTx = txChain.newReadWriteTransaction();
1136 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1137 PeopleModel.emptyContainer());
1139 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1140 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1141 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1143 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1145 // Note that merge will validate the data and fail but put
1146 // succeeds b/c deep validation is not
1147 // done for put for performance reasons.
1149 writeTx.commit().get(5, TimeUnit.SECONDS);
1150 fail("Expected TransactionCommitFailedException");
1151 } catch (final ExecutionException e) {
1155 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1156 any(Throwable.class));
1166 public void testDataTreeChangeListenerRegistration() throws Exception {
1167 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1169 try (AbstractDataStore dataStore = setupAbstractDataStore(
1170 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
1172 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1173 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1175 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1177 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
1178 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
1180 assertNotNull("registerTreeChangeListener returned null", listenerReg);
1182 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1183 state -> assertEquals("getTreeChangeListenerActors", 1,
1184 state.getTreeChangeListenerActors().size()));
1186 // Wait for the initial notification
1187 listener.waitForChangeEvents(TestModel.TEST_PATH);
1191 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1192 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1194 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1195 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1196 testWriteTransaction(dataStore, listPath,
1197 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1199 // Wait for the 2 updates.
1200 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1201 listenerReg.close();
1203 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1204 state -> assertEquals("getTreeChangeListenerActors", 0,
1205 state.getTreeChangeListenerActors().size()));
1207 testWriteTransaction(dataStore,
1208 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1209 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1210 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1212 listener.expectNoMoreChanges("Received unexpected change after close");
1219 public void testRestoreFromDatastoreSnapshot() throws Exception {
1220 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1222 final String name = "transactionIntegrationTest";
1224 final ContainerNode carsNode = CarsModel.newCarsNode(
1225 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1226 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1228 DataTree dataTree = new InMemoryDataTreeFactory().create(
1229 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
1230 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1231 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1233 final Snapshot carsSnapshot = Snapshot.create(
1234 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1235 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1237 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
1238 SchemaContextHelper.full());
1240 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1241 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1243 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1245 final Snapshot peopleSnapshot = Snapshot.create(
1246 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1247 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1249 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1250 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1251 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1253 try (AbstractDataStore dataStore = setupAbstractDataStore(
1254 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1256 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1259 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1260 assertEquals("isPresent", true, optional.isPresent());
1261 assertEquals("Data node", carsNode, optional.get());
1263 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1264 assertEquals("isPresent", true, optional.isPresent());
1265 assertEquals("Data node", peopleNode, optional.get());