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.IOException;
34 import java.math.BigInteger;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.ExecutionException;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.atomic.AtomicReference;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.junit.runners.Parameterized;
49 import org.junit.runners.Parameterized.Parameter;
50 import org.junit.runners.Parameterized.Parameters;
51 import org.mockito.Mockito;
52 import org.opendaylight.controller.cluster.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.MockDataChangeListener;
62 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
63 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
64 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
65 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
66 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
67 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
68 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
69 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
70 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
71 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
72 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
73 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
74 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
75 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
76 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
77 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
78 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
79 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
80 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
81 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
82 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
83 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
84 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
85 import org.opendaylight.yangtools.concepts.ListenerRegistration;
86 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
87 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
88 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
89 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
90 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
91 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
92 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
93 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
94 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
95 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
97 @RunWith(Parameterized.class)
98 public class DistributedDataStoreIntegrationTest {
100 @Parameters(name = "{0}")
101 public static Collection<Object[]> data() {
102 return Arrays.asList(new Object[][] {
103 { DistributedDataStore.class }, { ClientBackedDataStore.class }
108 public Class<? extends AbstractDataStore> testParameter;
110 private ActorSystem system;
112 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
113 .shardHeartbeatIntervalInMillis(100);
116 public void setUp() throws IOException {
117 InMemorySnapshotStore.clear();
118 InMemoryJournal.clear();
119 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
120 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
121 Cluster.get(system).join(member1Address);
125 public void tearDown() throws IOException {
126 JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
130 protected ActorSystem getSystem() {
135 public void testWriteTransactionWithSingleShard() throws Exception {
136 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
138 try (AbstractDataStore dataStore = setupAbstractDataStore(
139 testParameter, "transactionIntegrationTest", "test-1")) {
141 testWriteTransaction(dataStore, TestModel.TEST_PATH,
142 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
144 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
145 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
152 public void testWriteTransactionWithMultipleShards() throws Exception {
153 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
155 try (AbstractDataStore dataStore = setupAbstractDataStore(
156 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
158 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
159 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
161 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
162 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
164 doCommit(writeTx.ready());
166 writeTx = dataStore.newWriteOnlyTransaction();
168 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
169 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
171 doCommit(writeTx.ready());
173 writeTx = dataStore.newWriteOnlyTransaction();
175 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
176 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
177 writeTx.write(carPath, car);
179 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
180 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
181 writeTx.write(personPath, person);
183 doCommit(writeTx.ready());
185 // Verify the data in the store
186 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
188 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
189 assertEquals("isPresent", true, optional.isPresent());
190 assertEquals("Data node", car, optional.get());
192 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
193 assertEquals("isPresent", true, optional.isPresent());
194 assertEquals("Data node", person, optional.get());
201 public void testReadWriteTransactionWithSingleShard() throws Exception {
202 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
204 try (AbstractDataStore dataStore = setupAbstractDataStore(
205 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
207 // 1. Create a read-write Tx
208 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
209 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
211 // 2. Write some data
212 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
213 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
214 readWriteTx.write(nodePath, nodeToWrite);
216 // 3. Read the data from Tx
217 final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
218 assertEquals("exists", true, exists);
220 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
221 assertEquals("isPresent", true, optional.isPresent());
222 assertEquals("Data node", nodeToWrite, optional.get());
224 // 4. Ready the Tx for commit
225 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
230 // 6. Verify the data in the store
231 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
233 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
234 assertEquals("isPresent", true, optional.isPresent());
235 assertEquals("Data node", nodeToWrite, optional.get());
242 public void testReadWriteTransactionWithMultipleShards() throws Exception {
243 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
245 try (AbstractDataStore dataStore = setupAbstractDataStore(
246 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
248 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
249 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
251 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
252 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
254 doCommit(readWriteTx.ready());
256 readWriteTx = dataStore.newReadWriteTransaction();
258 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
259 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
261 doCommit(readWriteTx.ready());
263 readWriteTx = dataStore.newReadWriteTransaction();
265 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
266 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
267 readWriteTx.write(carPath, car);
269 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
270 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
271 readWriteTx.write(personPath, person);
273 final Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
274 assertEquals("exists", true, exists);
276 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
277 assertEquals("isPresent", true, optional.isPresent());
278 assertEquals("Data node", car, optional.get());
280 doCommit(readWriteTx.ready());
282 // Verify the data in the store
283 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
285 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
286 assertEquals("isPresent", true, optional.isPresent());
287 assertEquals("Data node", car, optional.get());
289 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
290 assertEquals("isPresent", true, optional.isPresent());
291 assertEquals("Data node", person, optional.get());
299 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
300 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
302 try (AbstractDataStore dataStore = setupAbstractDataStore(
303 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
305 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
307 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
308 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
309 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
310 doCommit(writeTx.ready());
312 writeTx = txChain.newWriteOnlyTransaction();
315 for (int i = 0; i < numCars; i++) {
316 writeTx.write(CarsModel.newCarPath("car" + i),
317 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
320 doCommit(writeTx.ready());
322 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
323 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
324 assertEquals("isPresent", true, optional.isPresent());
325 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
331 @SuppressWarnings("checkstyle:IllegalCatch")
332 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
334 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
336 final String shardName = "test-1";
338 // Setup the InMemoryJournal to block shard recovery to ensure
340 // initialized until we create and submit the write the Tx.
341 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
342 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
343 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
345 try (AbstractDataStore dataStore = setupAbstractDataStore(
346 testParameter, testName, false, shardName)) {
348 // Create the write Tx
349 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
350 : dataStore.newReadWriteTransaction();
351 assertNotNull("newReadWriteTransaction returned null", writeTx);
353 // Do some modification operations and ready the Tx on a
355 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
356 .builder(TestModel.OUTER_LIST_PATH)
357 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
359 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
360 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
361 final CountDownLatch txReady = new CountDownLatch(1);
362 final Thread txThread = new Thread(() -> {
364 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
366 writeTx.merge(TestModel.OUTER_LIST_PATH,
367 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
369 writeTx.write(listEntryPath,
370 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
372 writeTx.delete(listEntryPath);
374 txCohort.set(writeTx.ready());
375 } catch (Exception e) {
384 // Wait for the Tx operations to complete.
385 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
386 if (caughtEx.get() != null) {
387 throw caughtEx.get();
390 assertEquals("Tx ready", true, done);
392 // At this point the Tx operations should be waiting for the
393 // shard to initialize so
394 // trigger the latch to let the shard recovery to continue.
395 blockRecoveryLatch.countDown();
397 // Wait for the Tx commit to complete.
398 doCommit(txCohort.get());
400 // Verify the data in the store
401 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
403 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
404 assertEquals("isPresent", true, optional.isPresent());
406 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
407 assertEquals("isPresent", true, optional.isPresent());
409 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
410 assertEquals("isPresent", false, optional.isPresent());
417 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
418 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
419 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
423 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
424 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
428 @SuppressWarnings("checkstyle:IllegalCatch")
429 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
430 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
432 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
433 final String shardName = "test-1";
435 // Setup the InMemoryJournal to block shard recovery to ensure
437 // initialized until we create the Tx.
438 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
439 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
440 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
442 try (AbstractDataStore dataStore = setupAbstractDataStore(
443 testParameter, testName, false, shardName)) {
445 // Create the read-write Tx
446 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
447 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
449 // Do some reads on the Tx on a separate thread.
450 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
451 new AtomicReference<>();
452 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
453 txReadFuture = new AtomicReference<>();
454 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
455 final CountDownLatch txReadsDone = new CountDownLatch(1);
456 final Thread txThread = new Thread(() -> {
458 readWriteTx.write(TestModel.TEST_PATH,
459 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
461 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
463 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
464 } catch (Exception e) {
467 txReadsDone.countDown();
473 // Wait for the Tx operations to complete.
474 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
475 if (caughtEx.get() != null) {
476 throw caughtEx.get();
479 assertEquals("Tx reads done", true, done);
481 // At this point the Tx operations should be waiting for the
482 // shard to initialize so
483 // trigger the latch to let the shard recovery to continue.
484 blockRecoveryLatch.countDown();
486 // Wait for the reads to complete and verify.
487 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
488 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
496 @Test(expected = NotInitializedException.class)
497 @SuppressWarnings("checkstyle:IllegalCatch")
498 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
499 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
501 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
502 final String shardName = "test-1";
504 // Set the shard initialization timeout low for the test.
505 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
507 // Setup the InMemoryJournal to block shard recovery
509 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
510 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
511 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
513 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
515 final AbstractDataStore dataStore =
516 setupAbstractDataStore(testParameter, testName, false, shardName);
518 // Create the write Tx
519 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
520 assertNotNull("newReadWriteTransaction returned null", writeTx);
522 // Do some modifications and ready the Tx on a separate
524 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
525 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
526 final CountDownLatch txReady = new CountDownLatch(1);
527 final Thread txThread = new Thread(() -> {
529 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
531 txCohort.set(writeTx.ready());
532 } catch (Exception e) {
541 // Wait for the Tx operations to complete.
542 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
543 if (caughtEx.get() != null) {
544 throw caughtEx.get();
547 assertEquals("Tx ready", true, done);
549 // Wait for the commit to complete. Since the shard never
550 // initialized, the Tx should
551 // have timed out and throw an appropriate exception cause.
553 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
554 fail("Expected NotInitializedException");
555 } catch (final Exception e) {
556 Throwables.propagate(Throwables.getRootCause(e));
558 blockRecoveryLatch.countDown();
564 @Test(expected = NotInitializedException.class)
565 @SuppressWarnings("checkstyle:IllegalCatch")
566 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
567 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
569 final String testName = "testTransactionReadFailureWithShardNotInitialized";
570 final String shardName = "test-1";
572 // Set the shard initialization timeout low for the test.
573 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
575 // Setup the InMemoryJournal to block shard recovery
577 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
578 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
579 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
581 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
583 try (AbstractDataStore dataStore = setupAbstractDataStore(
584 testParameter, testName, false, shardName)) {
586 // Create the read-write Tx
587 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
588 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
590 // Do a read on the Tx on a separate thread.
591 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
592 txReadFuture = new AtomicReference<>();
593 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
594 final CountDownLatch txReadDone = new CountDownLatch(1);
595 final Thread txThread = new Thread(() -> {
597 readWriteTx.write(TestModel.TEST_PATH,
598 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
600 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
603 } catch (Exception e) {
606 txReadDone.countDown();
612 // Wait for the Tx operations to complete.
613 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
614 if (caughtEx.get() != null) {
615 throw caughtEx.get();
618 assertEquals("Tx read done", true, done);
620 // Wait for the read to complete. Since the shard never
621 // initialized, the Tx should
622 // have timed out and throw an appropriate exception cause.
624 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
625 fail("Expected NotInitializedException");
626 } catch (final ReadFailedException e) {
627 Throwables.propagate(Throwables.getRootCause(e));
629 blockRecoveryLatch.countDown();
636 @SuppressWarnings("checkstyle:IllegalCatch")
637 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
639 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
641 final String shardName = "default";
643 // We don't want the shard to become the leader so prevent shard
645 datastoreContextBuilder.customRaftPolicyImplementation(
646 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
648 // The ShardManager uses the election timeout for FindPrimary so
649 // reset it low so it will timeout quickly.
650 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
651 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
653 try (AbstractDataStore dataStore = setupAbstractDataStore(
654 testParameter, testName, false, shardName)) {
656 final Object result = dataStore.getActorContext().executeOperation(
657 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
658 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
660 // Create the write Tx.
661 try (DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
662 : dataStore.newReadWriteTransaction()) {
663 assertNotNull("newReadWriteTransaction returned null", writeTx);
665 // Do some modifications and ready the Tx on a separate
667 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
668 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
669 final CountDownLatch txReady = new CountDownLatch(1);
670 final Thread txThread = new Thread(() -> {
672 writeTx.write(TestModel.JUNK_PATH,
673 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
675 txCohort.set(writeTx.ready());
676 } catch (Exception e) {
685 // Wait for the Tx operations to complete.
686 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
687 if (caughtEx.get() != null) {
688 throw caughtEx.get();
691 assertEquals("Tx ready", true, done);
693 // Wait for the commit to complete. Since no shard
694 // leader was elected in time, the Tx
695 // should have timed out and throw an appropriate
698 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
699 fail("Expected NoShardLeaderException");
700 } catch (final ExecutionException e) {
701 Throwables.propagate(Throwables.getRootCause(e));
709 @Test(expected = NoShardLeaderException.class)
710 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
711 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
712 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
715 @Test(expected = NoShardLeaderException.class)
716 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
717 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
721 public void testTransactionAbort() throws Exception {
722 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
724 try (AbstractDataStore dataStore = setupAbstractDataStore(
725 testParameter, "transactionAbortIntegrationTest", "test-1")) {
727 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
728 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
730 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
732 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
734 cohort.canCommit().get(5, TimeUnit.SECONDS);
736 cohort.abort().get(5, TimeUnit.SECONDS);
738 testWriteTransaction(dataStore, TestModel.TEST_PATH,
739 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
746 @SuppressWarnings("checkstyle:IllegalCatch")
747 public void testTransactionChainWithSingleShard() throws Exception {
748 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
750 try (AbstractDataStore dataStore = setupAbstractDataStore(
751 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
753 // 1. Create a Tx chain and write-only Tx
754 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
756 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
757 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
759 // 2. Write some data
760 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
761 writeTx.write(TestModel.TEST_PATH, testNode);
763 // 3. Ready the Tx for commit
764 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
766 // 4. Commit the Tx on another thread that first waits for
767 // the second read Tx.
768 final CountDownLatch continueCommit1 = new CountDownLatch(1);
769 final CountDownLatch commit1Done = new CountDownLatch(1);
770 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
773 continueCommit1.await();
775 } catch (Exception e) {
778 commit1Done.countDown();
782 // 5. Create a new read Tx from the chain to read and verify
783 // the data from the first
784 // Tx is visible after being readied.
785 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
786 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
787 assertEquals("isPresent", true, optional.isPresent());
788 assertEquals("Data node", testNode, optional.get());
790 // 6. Create a new RW Tx from the chain, write more data,
792 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
793 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
794 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
796 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
798 // 7. Create a new read Tx from the chain to read the data
799 // from the last RW Tx to
800 // verify it is visible.
801 readTx = txChain.newReadWriteTransaction();
802 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
803 assertEquals("isPresent", true, optional.isPresent());
804 assertEquals("Data node", outerNode, optional.get());
806 // 8. Wait for the 2 commits to complete and close the
808 continueCommit1.countDown();
809 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
811 if (commit1Error.get() != null) {
812 throw commit1Error.get();
819 // 9. Create a new read Tx from the data store and verify
821 readTx = dataStore.newReadOnlyTransaction();
822 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
823 assertEquals("isPresent", true, optional.isPresent());
824 assertEquals("Data node", outerNode, optional.get());
831 public void testTransactionChainWithMultipleShards() throws Exception {
832 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
834 try (AbstractDataStore dataStore = setupAbstractDataStore(
835 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
837 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
839 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
840 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
842 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
843 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
845 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
846 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
848 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
850 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
852 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
853 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
854 readWriteTx.write(carPath, car);
856 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
857 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
858 readWriteTx.merge(personPath, person);
860 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
861 assertEquals("isPresent", true, optional.isPresent());
862 assertEquals("Data node", car, optional.get());
864 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
865 assertEquals("isPresent", true, optional.isPresent());
866 assertEquals("Data node", person, optional.get());
868 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
870 writeTx = txChain.newWriteOnlyTransaction();
872 writeTx.delete(carPath);
874 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
876 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
877 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
879 doCommit(canCommit1, cohort1);
880 doCommit(canCommit2, cohort2);
885 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
887 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
888 assertEquals("isPresent", false, optional.isPresent());
890 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
891 assertEquals("isPresent", true, optional.isPresent());
892 assertEquals("Data node", person, optional.get());
899 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
900 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
902 try (AbstractDataStore dataStore = setupAbstractDataStore(
903 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
905 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
906 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
907 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
908 MoreExecutors.directExecutor());
910 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
911 DOMTransactionChain txChain = broker.createTransactionChain(listener);
913 final List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
915 final DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
916 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
917 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
918 futures.add(writeTx.submit());
921 for (int i = 0; i < numCars; i++) {
922 final DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
924 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
925 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
927 futures.add(rwTx.submit());
930 for (final CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
934 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
935 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
936 assertEquals("isPresent", true, optional.isPresent());
937 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
948 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
949 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
951 try (AbstractDataStore dataStore = setupAbstractDataStore(
952 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
954 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
956 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
960 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
962 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
963 5, TimeUnit.SECONDS);
964 assertEquals("isPresent", false, optional.isPresent());
973 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
974 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
976 try (AbstractDataStore dataStore = setupAbstractDataStore(
977 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
979 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
981 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
982 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
984 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
986 // Try to create another Tx of each type - each should fail
987 // b/c the previous Tx wasn't
989 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
996 public void testCreateChainedTransactionAfterClose() throws Exception {
997 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
999 try (AbstractDataStore dataStore = setupAbstractDataStore(
1000 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1002 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1005 // Try to create another Tx of each type - should fail b/c
1006 // the previous Tx was closed.
1007 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1014 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1015 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1017 try (AbstractDataStore dataStore = setupAbstractDataStore(
1018 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1020 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1022 // Create a write tx and submit.
1023 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1024 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1025 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1027 // Create read-only tx's and issue a read.
1028 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1029 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1031 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1032 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1034 // Create another write tx and issue the write.
1035 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1036 writeTx2.write(TestModel.OUTER_LIST_PATH,
1037 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1039 // Ensure the reads succeed.
1041 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1042 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1044 // Ensure the writes succeed.
1045 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1050 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1051 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1058 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1059 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1061 try (AbstractDataStore dataStore = setupAbstractDataStore(
1062 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1064 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1065 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1066 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1067 MoreExecutors.directExecutor());
1069 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1070 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1072 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1074 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1075 PeopleModel.emptyContainer());
1077 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1078 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1079 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1081 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1084 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1085 fail("Expected TransactionCommitFailedException");
1086 } catch (final TransactionCommitFailedException e) {
1090 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1091 any(Throwable.class));
1101 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1102 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1104 try (AbstractDataStore dataStore = setupAbstractDataStore(
1105 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1107 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1108 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1109 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1110 MoreExecutors.directExecutor());
1112 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1113 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1115 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1117 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1118 PeopleModel.emptyContainer());
1120 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1121 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1122 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1124 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1126 // Note that merge will validate the data and fail but put
1127 // succeeds b/c deep validation is not
1128 // done for put for performance reasons.
1130 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1131 fail("Expected TransactionCommitFailedException");
1132 } catch (final TransactionCommitFailedException e) {
1136 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1137 any(Throwable.class));
1147 public void testChangeListenerRegistration() throws Exception {
1148 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1150 try (AbstractDataStore dataStore = setupAbstractDataStore(
1151 testParameter, "testChangeListenerRegistration", "test-1")) {
1153 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1154 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1156 final MockDataChangeListener listener = new MockDataChangeListener(1);
1158 final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1159 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1161 assertNotNull("registerChangeListener returned null", listenerReg);
1163 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1164 state -> assertEquals("getDataChangeListenerActors", 1,
1165 state.getDataChangeListenerActors().size()));
1167 // Wait for the initial notification
1168 listener.waitForChangeEvents(TestModel.TEST_PATH);
1172 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1173 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1175 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1176 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1177 testWriteTransaction(dataStore, listPath,
1178 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1180 // Wait for the 2 updates.
1181 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1182 listenerReg.close();
1184 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1185 state -> assertEquals("getDataChangeListenerActors", 0,
1186 state.getDataChangeListenerActors().size()));
1188 testWriteTransaction(dataStore,
1189 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1190 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1191 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1193 listener.expectNoMoreChanges("Received unexpected change after close");
1200 public void testDataTreeChangeListenerRegistration() throws Exception {
1201 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1203 try (AbstractDataStore dataStore = setupAbstractDataStore(
1204 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
1206 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1207 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1209 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1211 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
1212 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
1214 assertNotNull("registerTreeChangeListener returned null", listenerReg);
1216 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1217 state -> assertEquals("getTreeChangeListenerActors", 1,
1218 state.getTreeChangeListenerActors().size()));
1220 // Wait for the initial notification
1221 listener.waitForChangeEvents(TestModel.TEST_PATH);
1225 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1226 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1228 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1229 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1230 testWriteTransaction(dataStore, listPath,
1231 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1233 // Wait for the 2 updates.
1234 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1235 listenerReg.close();
1237 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1238 state -> assertEquals("getTreeChangeListenerActors", 0,
1239 state.getTreeChangeListenerActors().size()));
1241 testWriteTransaction(dataStore,
1242 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1243 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1244 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1246 listener.expectNoMoreChanges("Received unexpected change after close");
1253 public void testRestoreFromDatastoreSnapshot() throws Exception {
1254 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1256 final String name = "transactionIntegrationTest";
1258 final ContainerNode carsNode = CarsModel.newCarsNode(
1259 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1260 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1262 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1263 dataTree.setSchemaContext(SchemaContextHelper.full());
1264 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1265 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1267 final Snapshot carsSnapshot = Snapshot.create(
1268 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1269 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1271 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1272 dataTree.setSchemaContext(SchemaContextHelper.full());
1274 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1275 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1277 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1279 final Snapshot peopleSnapshot = Snapshot.create(
1280 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1281 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1283 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1284 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1285 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1287 try (AbstractDataStore dataStore = setupAbstractDataStore(
1288 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1290 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1293 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1294 assertEquals("isPresent", true, optional.isPresent());
1295 assertEquals("Data node", carsNode, optional.get());
1297 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1298 assertEquals("isPresent", true, optional.isPresent());
1299 assertEquals("Data node", peopleNode, optional.get());