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.JavaTestKit;
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.ByteArrayOutputStream;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import java.io.ObjectOutputStream;
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.concurrent.CountDownLatch;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.TimeUnit;
46 import java.util.concurrent.atomic.AtomicReference;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.junit.runners.Parameterized;
52 import org.junit.runners.Parameterized.Parameter;
53 import org.junit.runners.Parameterized.Parameters;
54 import org.mockito.Mockito;
55 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
56 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
57 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
58 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
59 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
60 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
61 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
62 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
63 import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
64 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
65 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
66 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
67 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
68 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
69 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
70 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
71 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
72 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
73 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
74 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
75 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
76 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
77 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
78 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
79 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
80 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
81 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
82 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
83 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
84 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
85 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
86 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
87 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
88 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
89 import org.opendaylight.yangtools.concepts.ListenerRegistration;
90 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
91 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
92 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
93 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
94 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
95 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
96 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
97 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
98 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
99 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
101 @RunWith(Parameterized.class)
102 public class DistributedDataStoreIntegrationTest {
104 @Parameters(name = "{0}")
105 public static Collection<Object[]> data() {
106 return Arrays.asList(new Object[][] {
107 { DistributedDataStore.class }, { ClientBackedDataStore.class }
112 public Class<? extends AbstractDataStore> testParameter;
114 private ActorSystem system;
116 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
117 .shardHeartbeatIntervalInMillis(100);
120 public void setUp() throws IOException {
121 InMemorySnapshotStore.clear();
122 InMemoryJournal.clear();
123 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
124 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
125 Cluster.get(system).join(member1Address);
129 public void tearDown() throws IOException {
130 JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
134 protected ActorSystem getSystem() {
139 public void testWriteTransactionWithSingleShard() throws Exception {
140 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
142 try (AbstractDataStore dataStore = setupAbstractDataStore(
143 testParameter, "transactionIntegrationTest", "test-1")) {
145 testWriteTransaction(dataStore, TestModel.TEST_PATH,
146 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
148 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
149 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
156 public void testWriteTransactionWithMultipleShards() throws Exception {
157 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
159 try (AbstractDataStore dataStore = setupAbstractDataStore(
160 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
162 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
163 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
165 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
166 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
168 doCommit(writeTx.ready());
170 writeTx = dataStore.newWriteOnlyTransaction();
172 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
173 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
175 doCommit(writeTx.ready());
177 writeTx = dataStore.newWriteOnlyTransaction();
179 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
180 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
181 writeTx.write(carPath, car);
183 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
184 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
185 writeTx.write(personPath, person);
187 doCommit(writeTx.ready());
189 // Verify the data in the store
190 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
192 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
193 assertEquals("isPresent", true, optional.isPresent());
194 assertEquals("Data node", car, optional.get());
196 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
197 assertEquals("isPresent", true, optional.isPresent());
198 assertEquals("Data node", person, optional.get());
205 public void testReadWriteTransactionWithSingleShard() throws Exception {
206 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
208 try (AbstractDataStore dataStore = setupAbstractDataStore(
209 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
211 // 1. Create a read-write Tx
212 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
213 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
215 // 2. Write some data
216 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
217 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
218 readWriteTx.write(nodePath, nodeToWrite);
220 // 3. Read the data from Tx
221 final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
222 assertEquals("exists", true, exists);
224 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
225 assertEquals("isPresent", true, optional.isPresent());
226 assertEquals("Data node", nodeToWrite, optional.get());
228 // 4. Ready the Tx for commit
229 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
234 // 6. Verify the data in the store
235 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
237 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
238 assertEquals("isPresent", true, optional.isPresent());
239 assertEquals("Data node", nodeToWrite, optional.get());
246 public void testReadWriteTransactionWithMultipleShards() throws Exception {
247 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
249 try (AbstractDataStore dataStore = setupAbstractDataStore(
250 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
252 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
253 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
255 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
256 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
258 doCommit(readWriteTx.ready());
260 readWriteTx = dataStore.newReadWriteTransaction();
262 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
263 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
265 doCommit(readWriteTx.ready());
267 readWriteTx = dataStore.newReadWriteTransaction();
269 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
270 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
271 readWriteTx.write(carPath, car);
273 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
274 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
275 readWriteTx.write(personPath, person);
277 final Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
278 assertEquals("exists", true, exists);
280 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
281 assertEquals("isPresent", true, optional.isPresent());
282 assertEquals("Data node", car, optional.get());
284 doCommit(readWriteTx.ready());
286 // Verify the data in the store
287 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
289 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
290 assertEquals("isPresent", true, optional.isPresent());
291 assertEquals("Data node", car, optional.get());
293 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
294 assertEquals("isPresent", true, optional.isPresent());
295 assertEquals("Data node", person, optional.get());
303 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
304 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
306 try (AbstractDataStore dataStore = setupAbstractDataStore(
307 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
309 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
311 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
312 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
313 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
314 doCommit(writeTx.ready());
316 writeTx = txChain.newWriteOnlyTransaction();
319 for (int i = 0; i < numCars; i++) {
320 writeTx.write(CarsModel.newCarPath("car" + i),
321 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
324 doCommit(writeTx.ready());
326 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
327 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
328 assertEquals("isPresent", true, optional.isPresent());
329 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
335 @SuppressWarnings("checkstyle:IllegalCatch")
336 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
338 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
340 final String shardName = "test-1";
342 // Setup the InMemoryJournal to block shard recovery to ensure
344 // initialized until we create and submit the write the Tx.
345 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
346 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
347 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
349 try (AbstractDataStore dataStore = setupAbstractDataStore(
350 testParameter, testName, false, shardName)) {
352 // Create the write Tx
353 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
354 : dataStore.newReadWriteTransaction();
355 assertNotNull("newReadWriteTransaction returned null", writeTx);
357 // Do some modification operations and ready the Tx on a
359 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
360 .builder(TestModel.OUTER_LIST_PATH)
361 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
363 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
364 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
365 final CountDownLatch txReady = new CountDownLatch(1);
366 final Thread txThread = new Thread() {
370 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
372 writeTx.merge(TestModel.OUTER_LIST_PATH,
373 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
375 writeTx.write(listEntryPath,
376 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
378 writeTx.delete(listEntryPath);
380 txCohort.set(writeTx.ready());
381 } catch (Exception e) {
391 // Wait for the Tx operations to complete.
392 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
393 if (caughtEx.get() != null) {
394 throw caughtEx.get();
397 assertEquals("Tx ready", true, done);
399 // At this point the Tx operations should be waiting for the
400 // shard to initialize so
401 // trigger the latch to let the shard recovery to continue.
402 blockRecoveryLatch.countDown();
404 // Wait for the Tx commit to complete.
405 doCommit(txCohort.get());
407 // Verify the data in the store
408 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
410 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
411 assertEquals("isPresent", true, optional.isPresent());
413 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
414 assertEquals("isPresent", true, optional.isPresent());
416 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
417 assertEquals("isPresent", false, optional.isPresent());
424 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
425 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
426 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
430 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
431 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
435 @SuppressWarnings("checkstyle:IllegalCatch")
436 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
437 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
439 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
440 final String shardName = "test-1";
442 // Setup the InMemoryJournal to block shard recovery to ensure
444 // initialized until we create the Tx.
445 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
446 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
447 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
449 try (AbstractDataStore dataStore = setupAbstractDataStore(
450 testParameter, testName, false, shardName)) {
452 // Create the read-write Tx
453 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
454 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
456 // Do some reads on the Tx on a separate thread.
457 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
458 new AtomicReference<>();
459 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
460 txReadFuture = new AtomicReference<>();
461 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
462 final CountDownLatch txReadsDone = new CountDownLatch(1);
463 final Thread txThread = new Thread() {
467 readWriteTx.write(TestModel.TEST_PATH,
468 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
470 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
472 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
473 } catch (Exception e) {
476 txReadsDone.countDown();
483 // Wait for the Tx operations to complete.
484 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
485 if (caughtEx.get() != null) {
486 throw caughtEx.get();
489 assertEquals("Tx reads done", true, done);
491 // At this point the Tx operations should be waiting for the
492 // shard to initialize so
493 // trigger the latch to let the shard recovery to continue.
494 blockRecoveryLatch.countDown();
496 // Wait for the reads to complete and verify.
497 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
498 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
506 @Test(expected = NotInitializedException.class)
507 @SuppressWarnings("checkstyle:IllegalCatch")
508 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
509 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
511 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
512 final String shardName = "test-1";
514 // Set the shard initialization timeout low for the test.
515 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
517 // Setup the InMemoryJournal to block shard recovery
519 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
520 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
521 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
523 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
525 final AbstractDataStore dataStore =
526 setupAbstractDataStore(testParameter, testName, false, shardName);
528 // Create the write Tx
529 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
530 assertNotNull("newReadWriteTransaction returned null", writeTx);
532 // Do some modifications and ready the Tx on a separate
534 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
535 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
536 final CountDownLatch txReady = new CountDownLatch(1);
537 final Thread txThread = new Thread(() -> {
539 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
541 txCohort.set(writeTx.ready());
542 } catch (Exception e) {
551 // Wait for the Tx operations to complete.
552 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
553 if (caughtEx.get() != null) {
554 throw caughtEx.get();
557 assertEquals("Tx ready", true, done);
559 // Wait for the commit to complete. Since the shard never
560 // initialized, the Tx should
561 // have timed out and throw an appropriate exception cause.
563 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
564 fail("Expected NotInitializedException");
565 } catch (final Exception e) {
566 Throwables.propagate(Throwables.getRootCause(e));
568 blockRecoveryLatch.countDown();
574 @Test(expected = NotInitializedException.class)
575 @SuppressWarnings("checkstyle:IllegalCatch")
576 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
577 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
579 final String testName = "testTransactionReadFailureWithShardNotInitialized";
580 final String shardName = "test-1";
582 // Set the shard initialization timeout low for the test.
583 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
585 // Setup the InMemoryJournal to block shard recovery
587 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
588 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
589 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
591 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
593 try (AbstractDataStore dataStore = setupAbstractDataStore(
594 testParameter, testName, false, shardName)) {
596 // Create the read-write Tx
597 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
598 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
600 // Do a read on the Tx on a separate thread.
601 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
602 txReadFuture = new AtomicReference<>();
603 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
604 final CountDownLatch txReadDone = new CountDownLatch(1);
605 final Thread txThread = new Thread(() -> {
607 readWriteTx.write(TestModel.TEST_PATH,
608 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
610 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
613 } catch (Exception e) {
616 txReadDone.countDown();
622 // Wait for the Tx operations to complete.
623 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
624 if (caughtEx.get() != null) {
625 throw caughtEx.get();
628 assertEquals("Tx read done", true, done);
630 // Wait for the read to complete. Since the shard never
631 // initialized, the Tx should
632 // have timed out and throw an appropriate exception cause.
634 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
635 fail("Expected NotInitializedException");
636 } catch (final ReadFailedException e) {
637 Throwables.propagate(Throwables.getRootCause(e));
639 blockRecoveryLatch.countDown();
646 @SuppressWarnings("checkstyle:IllegalCatch")
647 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
649 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
651 final String shardName = "default";
653 // We don't want the shard to become the leader so prevent shard
655 datastoreContextBuilder.customRaftPolicyImplementation(
656 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
658 // The ShardManager uses the election timeout for FindPrimary so
659 // reset it low so it will timeout quickly.
660 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
661 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
663 try (AbstractDataStore dataStore = setupAbstractDataStore(
664 testParameter, testName, false, shardName)) {
666 final Object result = dataStore.getActorContext().executeOperation(
667 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
668 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
670 // Create the write Tx.
671 try (DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
672 : dataStore.newReadWriteTransaction()) {
673 assertNotNull("newReadWriteTransaction returned null", writeTx);
675 // Do some modifications and ready the Tx on a separate
677 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
678 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
679 final CountDownLatch txReady = new CountDownLatch(1);
680 final Thread txThread = new Thread(() -> {
682 writeTx.write(TestModel.JUNK_PATH,
683 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
685 txCohort.set(writeTx.ready());
686 } catch (Exception e) {
695 // Wait for the Tx operations to complete.
696 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
697 if (caughtEx.get() != null) {
698 throw caughtEx.get();
701 assertEquals("Tx ready", true, done);
703 // Wait for the commit to complete. Since no shard
704 // leader was elected in time, the Tx
705 // should have timed out and throw an appropriate
708 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
709 fail("Expected NoShardLeaderException");
710 } catch (final ExecutionException e) {
711 Throwables.propagate(Throwables.getRootCause(e));
719 @Test(expected = NoShardLeaderException.class)
720 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
721 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
722 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
725 @Test(expected = NoShardLeaderException.class)
726 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
727 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
731 public void testTransactionAbort() throws Exception {
732 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
734 try (AbstractDataStore dataStore = setupAbstractDataStore(
735 testParameter, "transactionAbortIntegrationTest", "test-1")) {
737 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
738 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
740 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
742 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
744 cohort.canCommit().get(5, TimeUnit.SECONDS);
746 cohort.abort().get(5, TimeUnit.SECONDS);
748 testWriteTransaction(dataStore, TestModel.TEST_PATH,
749 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
756 @SuppressWarnings("checkstyle:IllegalCatch")
757 public void testTransactionChainWithSingleShard() throws Exception {
758 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
760 try (AbstractDataStore dataStore = setupAbstractDataStore(
761 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
763 // 1. Create a Tx chain and write-only Tx
764 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
766 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
767 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
769 // 2. Write some data
770 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
771 writeTx.write(TestModel.TEST_PATH, testNode);
773 // 3. Ready the Tx for commit
774 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
776 // 4. Commit the Tx on another thread that first waits for
777 // the second read Tx.
778 final CountDownLatch continueCommit1 = new CountDownLatch(1);
779 final CountDownLatch commit1Done = new CountDownLatch(1);
780 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
783 continueCommit1.await();
785 } catch (Exception e) {
788 commit1Done.countDown();
792 // 5. Create a new read Tx from the chain to read and verify
793 // the data from the first
794 // Tx is visible after being readied.
795 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
796 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
797 assertEquals("isPresent", true, optional.isPresent());
798 assertEquals("Data node", testNode, optional.get());
800 // 6. Create a new RW Tx from the chain, write more data,
802 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
803 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
804 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
806 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
808 // 7. Create a new read Tx from the chain to read the data
809 // from the last RW Tx to
810 // verify it is visible.
811 readTx = txChain.newReadWriteTransaction();
812 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
813 assertEquals("isPresent", true, optional.isPresent());
814 assertEquals("Data node", outerNode, optional.get());
816 // 8. Wait for the 2 commits to complete and close the
818 continueCommit1.countDown();
819 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
821 if (commit1Error.get() != null) {
822 throw commit1Error.get();
829 // 9. Create a new read Tx from the data store and verify
831 readTx = dataStore.newReadOnlyTransaction();
832 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
833 assertEquals("isPresent", true, optional.isPresent());
834 assertEquals("Data node", outerNode, optional.get());
841 public void testTransactionChainWithMultipleShards() throws Exception {
842 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
844 try (AbstractDataStore dataStore = setupAbstractDataStore(
845 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
847 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
849 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
850 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
852 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
853 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
855 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
856 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
858 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
860 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
862 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
863 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
864 readWriteTx.write(carPath, car);
866 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
867 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
868 readWriteTx.merge(personPath, person);
870 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
871 assertEquals("isPresent", true, optional.isPresent());
872 assertEquals("Data node", car, optional.get());
874 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
875 assertEquals("isPresent", true, optional.isPresent());
876 assertEquals("Data node", person, optional.get());
878 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
880 writeTx = txChain.newWriteOnlyTransaction();
882 writeTx.delete(carPath);
884 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
886 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
887 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
889 doCommit(canCommit1, cohort1);
890 doCommit(canCommit2, cohort2);
895 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
897 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
898 assertEquals("isPresent", false, optional.isPresent());
900 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
901 assertEquals("isPresent", true, optional.isPresent());
902 assertEquals("Data node", person, optional.get());
909 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
910 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
912 try (AbstractDataStore dataStore = setupAbstractDataStore(
913 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
915 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
916 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
917 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
918 MoreExecutors.directExecutor());
920 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
921 DOMTransactionChain txChain = broker.createTransactionChain(listener);
923 final List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
925 final DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
926 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
927 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
928 futures.add(writeTx.submit());
931 for (int i = 0; i < numCars; i++) {
932 final DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
934 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
935 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
937 futures.add(rwTx.submit());
940 for (final CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
944 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
945 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
946 assertEquals("isPresent", true, optional.isPresent());
947 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
958 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
959 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
961 try (AbstractDataStore dataStore = setupAbstractDataStore(
962 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
964 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
966 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
970 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
972 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
973 5, TimeUnit.SECONDS);
974 assertEquals("isPresent", false, optional.isPresent());
983 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
984 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
986 try (AbstractDataStore dataStore = setupAbstractDataStore(
987 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
989 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
991 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
992 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
994 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
996 // Try to create another Tx of each type - each should fail
997 // b/c the previous Tx wasn't
999 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1006 public void testCreateChainedTransactionAfterClose() throws Exception {
1007 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1009 try (AbstractDataStore dataStore = setupAbstractDataStore(
1010 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1012 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1015 // Try to create another Tx of each type - should fail b/c
1016 // the previous Tx was closed.
1017 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1024 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1025 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1027 try (AbstractDataStore dataStore = setupAbstractDataStore(
1028 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1030 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1032 // Create a write tx and submit.
1033 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1034 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1035 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1037 // Create read-only tx's and issue a read.
1038 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1039 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1041 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1042 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1044 // Create another write tx and issue the write.
1045 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1046 writeTx2.write(TestModel.OUTER_LIST_PATH,
1047 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1049 // Ensure the reads succeed.
1051 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1052 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1054 // Ensure the writes succeed.
1055 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1060 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1061 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1068 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1069 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1071 try (AbstractDataStore dataStore = setupAbstractDataStore(
1072 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1074 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1075 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1076 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1077 MoreExecutors.directExecutor());
1079 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1080 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1082 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1084 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1085 PeopleModel.emptyContainer());
1087 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1088 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1089 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1091 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1094 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1095 fail("Expected TransactionCommitFailedException");
1096 } catch (final TransactionCommitFailedException e) {
1100 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1101 any(Throwable.class));
1111 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1112 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1114 try (AbstractDataStore dataStore = setupAbstractDataStore(
1115 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1117 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1118 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1119 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1120 MoreExecutors.directExecutor());
1122 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1123 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1125 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1127 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1128 PeopleModel.emptyContainer());
1130 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1131 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1132 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1134 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1136 // Note that merge will validate the data and fail but put
1137 // succeeds b/c deep validation is not
1138 // done for put for performance reasons.
1140 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1141 fail("Expected TransactionCommitFailedException");
1142 } catch (final TransactionCommitFailedException e) {
1146 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1147 any(Throwable.class));
1157 public void testChangeListenerRegistration() throws Exception {
1158 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1160 try (AbstractDataStore dataStore = setupAbstractDataStore(
1161 testParameter, "testChangeListenerRegistration", "test-1")) {
1163 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1164 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1166 final MockDataChangeListener listener = new MockDataChangeListener(1);
1168 final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1169 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1171 assertNotNull("registerChangeListener returned null", listenerReg);
1173 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1174 state -> assertEquals("getDataChangeListenerActors", 1,
1175 state.getDataChangeListenerActors().size()));
1177 // Wait for the initial notification
1178 listener.waitForChangeEvents(TestModel.TEST_PATH);
1182 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1183 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1185 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1186 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1187 testWriteTransaction(dataStore, listPath,
1188 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1190 // Wait for the 2 updates.
1191 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1192 listenerReg.close();
1194 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1195 state -> assertEquals("getDataChangeListenerActors", 0,
1196 state.getDataChangeListenerActors().size()));
1198 testWriteTransaction(dataStore,
1199 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1200 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1201 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1203 listener.expectNoMoreChanges("Received unexpected change after close");
1210 public void testDataTreeChangeListenerRegistration() throws Exception {
1211 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1213 try (AbstractDataStore dataStore = setupAbstractDataStore(
1214 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
1216 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1217 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1219 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1221 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
1222 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
1224 assertNotNull("registerTreeChangeListener returned null", listenerReg);
1226 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1227 state -> assertEquals("getTreeChangeListenerActors", 1,
1228 state.getTreeChangeListenerActors().size()));
1230 // Wait for the initial notification
1231 listener.waitForChangeEvents(TestModel.TEST_PATH);
1235 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1236 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1238 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1239 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1240 testWriteTransaction(dataStore, listPath,
1241 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1243 // Wait for the 2 updates.
1244 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1245 listenerReg.close();
1247 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1248 state -> assertEquals("getTreeChangeListenerActors", 0,
1249 state.getTreeChangeListenerActors().size()));
1251 testWriteTransaction(dataStore,
1252 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1253 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1254 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1256 listener.expectNoMoreChanges("Received unexpected change after close");
1263 public void testRestoreFromDatastoreSnapshot() throws Exception {
1264 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1266 final String name = "transactionIntegrationTest";
1268 final ContainerNode carsNode = CarsModel.newCarsNode(
1269 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1270 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1272 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1273 dataTree.setSchemaContext(SchemaContextHelper.full());
1274 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1275 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1277 final Snapshot carsSnapshot = Snapshot.create(
1278 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1279 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1281 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1282 dataTree.setSchemaContext(SchemaContextHelper.full());
1284 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1285 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1287 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1289 final Snapshot peopleSnapshot = Snapshot.create(
1290 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1291 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1293 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1294 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1295 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1297 try (AbstractDataStore dataStore = setupAbstractDataStore(
1298 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1300 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1303 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1304 assertEquals("isPresent", true, optional.isPresent());
1305 assertEquals("Data node", carsNode, optional.get());
1307 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1308 assertEquals("isPresent", true, optional.isPresent());
1309 assertEquals("Data node", peopleNode, optional.get());
1317 public void testRecoveryFromPreCarbonSnapshot() throws Exception {
1318 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1320 final String name = "testRecoveryFromPreCarbonSnapshot";
1322 final ContainerNode carsNode = CarsModel.newCarsNode(
1323 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1324 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1326 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1327 dataTree.setSchemaContext(SchemaContextHelper.full());
1328 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1329 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1331 MetadataShardDataTreeSnapshot shardSnapshot = new MetadataShardDataTreeSnapshot(root);
1332 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
1333 try (DataOutputStream dos = new DataOutputStream(bos)) {
1334 PayloadVersion.BORON.writeTo(dos);
1335 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
1336 oos.writeObject(shardSnapshot);
1340 final org.opendaylight.controller.cluster.raft.Snapshot snapshot =
1341 org.opendaylight.controller.cluster.raft.Snapshot.create(bos.toByteArray(),
1342 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1344 InMemorySnapshotStore.addSnapshot("member-1-shard-cars-" + name, snapshot);
1346 try (AbstractDataStore dataStore = setupAbstractDataStore(
1347 testParameter, name, "module-shards-member1.conf", true, "cars")) {
1349 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1351 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1352 assertEquals("isPresent", true, optional.isPresent());
1353 assertEquals("Data node", carsNode, optional.get());