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.TimeoutException;
47 import java.util.concurrent.atomic.AtomicReference;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.junit.runners.Parameterized;
53 import org.junit.runners.Parameterized.Parameter;
54 import org.junit.runners.Parameterized.Parameters;
55 import org.mockito.Mockito;
56 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
57 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
58 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
59 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
60 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
61 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
62 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
63 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
64 import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
65 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
66 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
67 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
68 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
69 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
70 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
71 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
72 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
73 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
74 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
75 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
76 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
77 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
78 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
79 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
80 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
81 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
82 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
83 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
84 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
85 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
86 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
87 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
88 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
89 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
90 import org.opendaylight.yangtools.concepts.ListenerRegistration;
91 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
92 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
93 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
94 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
95 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
96 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
97 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
98 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
99 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
100 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
102 @RunWith(Parameterized.class)
103 public class DistributedDataStoreIntegrationTest {
105 @Parameters(name = "{0}")
106 public static Collection<Object[]> data() {
107 return Arrays.asList(new Object[][] {
108 { DistributedDataStore.class }, { ClientBackedDataStore.class }
113 public Class<? extends AbstractDataStore> testParameter;
115 private ActorSystem system;
117 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
118 .shardHeartbeatIntervalInMillis(100);
121 public void setUp() throws IOException {
122 InMemorySnapshotStore.clear();
123 InMemoryJournal.clear();
124 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
125 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
126 Cluster.get(system).join(member1Address);
130 public void tearDown() throws IOException {
131 JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
135 protected ActorSystem getSystem() {
140 public void testWriteTransactionWithSingleShard() throws Exception {
141 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
143 try (AbstractDataStore dataStore = setupAbstractDataStore(
144 testParameter, "transactionIntegrationTest", "test-1")) {
146 testWriteTransaction(dataStore, TestModel.TEST_PATH,
147 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
149 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
150 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
157 public void testWriteTransactionWithMultipleShards() throws Exception {
158 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
160 try (AbstractDataStore dataStore = setupAbstractDataStore(
161 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
163 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
164 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
166 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
167 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
169 doCommit(writeTx.ready());
171 writeTx = dataStore.newWriteOnlyTransaction();
173 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
174 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
176 doCommit(writeTx.ready());
178 writeTx = dataStore.newWriteOnlyTransaction();
180 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
181 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
182 writeTx.write(carPath, car);
184 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
185 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
186 writeTx.write(personPath, person);
188 doCommit(writeTx.ready());
190 // Verify the data in the store
191 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
193 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
194 assertEquals("isPresent", true, optional.isPresent());
195 assertEquals("Data node", car, optional.get());
197 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
198 assertEquals("isPresent", true, optional.isPresent());
199 assertEquals("Data node", person, optional.get());
206 public void testReadWriteTransactionWithSingleShard() throws Exception {
207 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
209 try (AbstractDataStore dataStore = setupAbstractDataStore(
210 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
212 // 1. Create a read-write Tx
213 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
214 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
216 // 2. Write some data
217 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
218 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
219 readWriteTx.write(nodePath, nodeToWrite);
221 // 3. Read the data from Tx
222 final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
223 assertEquals("exists", true, exists);
225 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
226 assertEquals("isPresent", true, optional.isPresent());
227 assertEquals("Data node", nodeToWrite, optional.get());
229 // 4. Ready the Tx for commit
230 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
235 // 6. Verify the data in the store
236 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
238 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
239 assertEquals("isPresent", true, optional.isPresent());
240 assertEquals("Data node", nodeToWrite, optional.get());
247 public void testReadWriteTransactionWithMultipleShards() throws Exception {
248 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
250 try (AbstractDataStore dataStore = setupAbstractDataStore(
251 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
253 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
254 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
256 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
257 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
259 doCommit(readWriteTx.ready());
261 readWriteTx = dataStore.newReadWriteTransaction();
263 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
264 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
266 doCommit(readWriteTx.ready());
268 readWriteTx = dataStore.newReadWriteTransaction();
270 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
271 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
272 readWriteTx.write(carPath, car);
274 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
275 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
276 readWriteTx.write(personPath, person);
278 final Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
279 assertEquals("exists", true, exists);
281 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
282 assertEquals("isPresent", true, optional.isPresent());
283 assertEquals("Data node", car, optional.get());
285 doCommit(readWriteTx.ready());
287 // Verify the data in the store
288 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
290 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
291 assertEquals("isPresent", true, optional.isPresent());
292 assertEquals("Data node", car, optional.get());
294 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
295 assertEquals("isPresent", true, optional.isPresent());
296 assertEquals("Data node", person, optional.get());
304 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
305 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
307 try (AbstractDataStore dataStore = setupAbstractDataStore(
308 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
310 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
312 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
313 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
314 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
315 doCommit(writeTx.ready());
317 writeTx = txChain.newWriteOnlyTransaction();
320 for (int i = 0; i < numCars; i++) {
321 writeTx.write(CarsModel.newCarPath("car" + i),
322 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
325 doCommit(writeTx.ready());
327 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
328 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
329 assertEquals("isPresent", true, optional.isPresent());
330 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
336 @SuppressWarnings("checkstyle:IllegalCatch")
337 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
339 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
341 final String shardName = "test-1";
343 // Setup the InMemoryJournal to block shard recovery to ensure
345 // initialized until we create and submit the write the Tx.
346 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
347 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
348 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
350 try (AbstractDataStore dataStore = setupAbstractDataStore(
351 testParameter, testName, false, shardName)) {
353 // Create the write Tx
354 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
355 : dataStore.newReadWriteTransaction();
356 assertNotNull("newReadWriteTransaction returned null", writeTx);
358 // Do some modification operations and ready the Tx on a
360 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
361 .builder(TestModel.OUTER_LIST_PATH)
362 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
364 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
365 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
366 final CountDownLatch txReady = new CountDownLatch(1);
367 final Thread txThread = new Thread() {
371 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
373 writeTx.merge(TestModel.OUTER_LIST_PATH,
374 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
376 writeTx.write(listEntryPath,
377 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
379 writeTx.delete(listEntryPath);
381 txCohort.set(writeTx.ready());
382 } catch (Exception e) {
392 // Wait for the Tx operations to complete.
393 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
394 if (caughtEx.get() != null) {
395 throw caughtEx.get();
398 assertEquals("Tx ready", true, done);
400 // At this point the Tx operations should be waiting for the
401 // shard to initialize so
402 // trigger the latch to let the shard recovery to continue.
403 blockRecoveryLatch.countDown();
405 // Wait for the Tx commit to complete.
406 doCommit(txCohort.get());
408 // Verify the data in the store
409 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
411 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
412 assertEquals("isPresent", true, optional.isPresent());
414 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
415 assertEquals("isPresent", true, optional.isPresent());
417 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
418 assertEquals("isPresent", false, optional.isPresent());
425 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
426 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
427 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
431 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
432 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
436 @SuppressWarnings("checkstyle:IllegalCatch")
437 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
438 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
440 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
441 final String shardName = "test-1";
443 // Setup the InMemoryJournal to block shard recovery to ensure
445 // initialized until we create the Tx.
446 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
447 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
448 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
450 try (AbstractDataStore dataStore = setupAbstractDataStore(
451 testParameter, testName, false, shardName)) {
453 // Create the read-write Tx
454 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
455 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
457 // Do some reads on the Tx on a separate thread.
458 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
459 new AtomicReference<>();
460 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
461 txReadFuture = new AtomicReference<>();
462 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
463 final CountDownLatch txReadsDone = new CountDownLatch(1);
464 final Thread txThread = new Thread() {
468 readWriteTx.write(TestModel.TEST_PATH,
469 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
471 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
473 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
474 } catch (Exception e) {
477 txReadsDone.countDown();
484 // Wait for the Tx operations to complete.
485 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
486 if (caughtEx.get() != null) {
487 throw caughtEx.get();
490 assertEquals("Tx reads done", true, done);
492 // At this point the Tx operations should be waiting for the
493 // shard to initialize so
494 // trigger the latch to let the shard recovery to continue.
495 blockRecoveryLatch.countDown();
497 // Wait for the reads to complete and verify.
498 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
499 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
507 @Test(expected = NotInitializedException.class)
508 @SuppressWarnings("checkstyle:IllegalCatch")
509 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
510 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
512 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
513 final String shardName = "test-1";
515 // Set the shard initialization timeout low for the test.
516 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
518 // Setup the InMemoryJournal to block shard recovery
520 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
521 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
522 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
524 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
526 final AbstractDataStore dataStore =
527 setupAbstractDataStore(testParameter, testName, false, shardName);
529 // Create the write Tx
530 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
531 assertNotNull("newReadWriteTransaction returned null", writeTx);
533 // Do some modifications and ready the Tx on a separate
535 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
536 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
537 final CountDownLatch txReady = new CountDownLatch(1);
538 final Thread txThread = new Thread(() -> {
540 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
542 txCohort.set(writeTx.ready());
543 } catch (Exception e) {
552 // Wait for the Tx operations to complete.
553 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
554 if (caughtEx.get() != null) {
555 throw caughtEx.get();
558 assertEquals("Tx ready", true, done);
560 // Wait for the commit to complete. Since the shard never
561 // initialized, the Tx should
562 // have timed out and throw an appropriate exception cause.
564 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
565 fail("Expected NotInitializedException");
566 } catch (final Exception e) {
567 Throwables.propagate(Throwables.getRootCause(e));
569 blockRecoveryLatch.countDown();
575 @Test(expected = NotInitializedException.class)
576 @SuppressWarnings("checkstyle:IllegalCatch")
577 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
578 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
580 final String testName = "testTransactionReadFailureWithShardNotInitialized";
581 final String shardName = "test-1";
583 // Set the shard initialization timeout low for the test.
584 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
586 // Setup the InMemoryJournal to block shard recovery
588 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
589 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
590 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
592 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
594 try (AbstractDataStore dataStore = setupAbstractDataStore(
595 testParameter, testName, false, shardName)) {
597 // Create the read-write Tx
598 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
599 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
601 // Do a read on the Tx on a separate thread.
602 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
603 txReadFuture = new AtomicReference<>();
604 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
605 final CountDownLatch txReadDone = new CountDownLatch(1);
606 final Thread txThread = new Thread(() -> {
608 readWriteTx.write(TestModel.TEST_PATH,
609 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
611 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
614 } catch (Exception e) {
617 txReadDone.countDown();
623 // Wait for the Tx operations to complete.
624 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
625 if (caughtEx.get() != null) {
626 throw caughtEx.get();
629 assertEquals("Tx read done", true, done);
631 // Wait for the read to complete. Since the shard never
632 // initialized, the Tx should
633 // have timed out and throw an appropriate exception cause.
635 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
636 fail("Expected NotInitializedException");
637 } catch (final ReadFailedException e) {
638 Throwables.propagate(Throwables.getRootCause(e));
640 blockRecoveryLatch.countDown();
647 @SuppressWarnings("checkstyle:IllegalCatch")
648 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
650 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
652 final String shardName = "default";
654 // We don't want the shard to become the leader so prevent shard
656 datastoreContextBuilder.customRaftPolicyImplementation(
657 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
659 // The ShardManager uses the election timeout for FindPrimary so
660 // reset it low so it will timeout quickly.
661 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
662 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
664 try (AbstractDataStore dataStore = setupAbstractDataStore(
665 testParameter, testName, false, shardName)) {
667 final Object result = dataStore.getActorContext().executeOperation(
668 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
669 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
671 // Create the write Tx.
672 DOMStoreWriteTransaction writeTxToClose = null;
674 writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction()
675 : dataStore.newReadWriteTransaction();
676 final DOMStoreWriteTransaction writeTx = writeTxToClose;
677 assertNotNull("newReadWriteTransaction returned null", writeTx);
679 // Do some modifications and ready the Tx on a separate
681 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
682 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
683 final CountDownLatch txReady = new CountDownLatch(1);
684 final Thread txThread = new Thread(() -> {
686 writeTx.write(TestModel.JUNK_PATH,
687 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
689 txCohort.set(writeTx.ready());
690 } catch (Exception e) {
699 // Wait for the Tx operations to complete.
700 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
701 if (caughtEx.get() != null) {
702 throw caughtEx.get();
705 assertEquals("Tx ready", true, done);
707 // Wait for the commit to complete. Since no shard
708 // leader was elected in time, the Tx
709 // should have timed out and throw an appropriate
712 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
713 fail("Expected NoShardLeaderException");
714 } catch (final ExecutionException e) {
715 assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
716 assertEquals(DistributedDataStore.class, testParameter);
717 } catch (TimeoutException e) {
718 // ClientBackedDataStore doesn't set cause to ExecutionException, future just time outs
719 assertEquals(ClientBackedDataStore.class, testParameter);
723 if (writeTxToClose != null) {
724 writeTxToClose.close();
726 } catch (Exception e) {
727 // FIXME TransactionProxy.close throws IllegalStateException:
728 // Transaction is ready, it cannot be closed
737 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
738 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
739 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
743 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
744 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
748 public void testTransactionAbort() throws Exception {
749 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
751 try (AbstractDataStore dataStore = setupAbstractDataStore(
752 testParameter, "transactionAbortIntegrationTest", "test-1")) {
754 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
755 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
757 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
759 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
761 cohort.canCommit().get(5, TimeUnit.SECONDS);
763 cohort.abort().get(5, TimeUnit.SECONDS);
765 testWriteTransaction(dataStore, TestModel.TEST_PATH,
766 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
773 @SuppressWarnings("checkstyle:IllegalCatch")
774 public void testTransactionChainWithSingleShard() throws Exception {
775 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
777 try (AbstractDataStore dataStore = setupAbstractDataStore(
778 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
780 // 1. Create a Tx chain and write-only Tx
781 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
783 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
784 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
786 // 2. Write some data
787 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
788 writeTx.write(TestModel.TEST_PATH, testNode);
790 // 3. Ready the Tx for commit
791 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
793 // 4. Commit the Tx on another thread that first waits for
794 // the second read Tx.
795 final CountDownLatch continueCommit1 = new CountDownLatch(1);
796 final CountDownLatch commit1Done = new CountDownLatch(1);
797 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
800 continueCommit1.await();
802 } catch (Exception e) {
805 commit1Done.countDown();
809 // 5. Create a new read Tx from the chain to read and verify
810 // the data from the first
811 // Tx is visible after being readied.
812 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
813 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
814 assertEquals("isPresent", true, optional.isPresent());
815 assertEquals("Data node", testNode, optional.get());
817 // 6. Create a new RW Tx from the chain, write more data,
819 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
820 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
821 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
823 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
825 // 7. Create a new read Tx from the chain to read the data
826 // from the last RW Tx to
827 // verify it is visible.
828 readTx = txChain.newReadWriteTransaction();
829 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
830 assertEquals("isPresent", true, optional.isPresent());
831 assertEquals("Data node", outerNode, optional.get());
833 // 8. Wait for the 2 commits to complete and close the
835 continueCommit1.countDown();
836 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
838 if (commit1Error.get() != null) {
839 throw commit1Error.get();
846 // 9. Create a new read Tx from the data store and verify
848 readTx = dataStore.newReadOnlyTransaction();
849 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
850 assertEquals("isPresent", true, optional.isPresent());
851 assertEquals("Data node", outerNode, optional.get());
858 public void testTransactionChainWithMultipleShards() throws Exception {
859 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
861 try (AbstractDataStore dataStore = setupAbstractDataStore(
862 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
864 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
866 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
867 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
869 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
870 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
872 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
873 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
875 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
877 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
879 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
880 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
881 readWriteTx.write(carPath, car);
883 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
884 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
885 readWriteTx.merge(personPath, person);
887 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
888 assertEquals("isPresent", true, optional.isPresent());
889 assertEquals("Data node", car, optional.get());
891 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
892 assertEquals("isPresent", true, optional.isPresent());
893 assertEquals("Data node", person, optional.get());
895 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
897 writeTx = txChain.newWriteOnlyTransaction();
899 writeTx.delete(carPath);
901 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
903 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
904 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
906 doCommit(canCommit1, cohort1);
907 doCommit(canCommit2, cohort2);
912 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
914 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
915 assertEquals("isPresent", false, optional.isPresent());
917 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
918 assertEquals("isPresent", true, optional.isPresent());
919 assertEquals("Data node", person, optional.get());
926 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
927 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
929 try (AbstractDataStore dataStore = setupAbstractDataStore(
930 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
932 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
933 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
934 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
935 MoreExecutors.directExecutor());
937 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
938 DOMTransactionChain txChain = broker.createTransactionChain(listener);
940 final List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
942 final DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
943 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
944 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
945 futures.add(writeTx.submit());
948 for (int i = 0; i < numCars; i++) {
949 final DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
951 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
952 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
954 futures.add(rwTx.submit());
957 for (final CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
961 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
962 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
963 assertEquals("isPresent", true, optional.isPresent());
964 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
975 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
976 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
978 try (AbstractDataStore dataStore = setupAbstractDataStore(
979 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
981 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
983 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
987 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
989 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
990 5, TimeUnit.SECONDS);
991 assertEquals("isPresent", false, optional.isPresent());
1000 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
1001 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1003 try (AbstractDataStore dataStore = setupAbstractDataStore(
1004 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
1006 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1008 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1009 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
1011 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1013 // Try to create another Tx of each type - each should fail
1014 // b/c the previous Tx wasn't
1016 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1023 public void testCreateChainedTransactionAfterClose() throws Exception {
1024 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1026 try (AbstractDataStore dataStore = setupAbstractDataStore(
1027 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1029 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1032 // Try to create another Tx of each type - should fail b/c
1033 // the previous Tx was closed.
1034 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1041 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1042 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1044 try (AbstractDataStore dataStore = setupAbstractDataStore(
1045 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1047 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1049 // Create a write tx and submit.
1050 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1051 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1052 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1054 // Create read-only tx's and issue a read.
1055 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1056 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1058 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1059 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1061 // Create another write tx and issue the write.
1062 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1063 writeTx2.write(TestModel.OUTER_LIST_PATH,
1064 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1066 // Ensure the reads succeed.
1068 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1069 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1071 // Ensure the writes succeed.
1072 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1077 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1078 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1085 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1086 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1088 try (AbstractDataStore dataStore = setupAbstractDataStore(
1089 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1091 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1092 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1093 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1094 MoreExecutors.directExecutor());
1096 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1097 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1099 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1101 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1102 PeopleModel.emptyContainer());
1104 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1105 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1106 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1108 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1111 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1112 fail("Expected TransactionCommitFailedException");
1113 } catch (final TransactionCommitFailedException e) {
1117 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1118 any(Throwable.class));
1128 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1129 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1131 try (AbstractDataStore dataStore = setupAbstractDataStore(
1132 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1134 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1135 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1136 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1137 MoreExecutors.directExecutor());
1139 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1140 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1142 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1144 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1145 PeopleModel.emptyContainer());
1147 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1148 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1149 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1151 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1153 // Note that merge will validate the data and fail but put
1154 // succeeds b/c deep validation is not
1155 // done for put for performance reasons.
1157 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1158 fail("Expected TransactionCommitFailedException");
1159 } catch (final TransactionCommitFailedException e) {
1163 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1164 any(Throwable.class));
1174 public void testChangeListenerRegistration() throws Exception {
1175 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1177 try (AbstractDataStore dataStore = setupAbstractDataStore(
1178 testParameter, "testChangeListenerRegistration", "test-1")) {
1180 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1181 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1183 final MockDataChangeListener listener = new MockDataChangeListener(1);
1185 final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1186 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1188 assertNotNull("registerChangeListener returned null", listenerReg);
1190 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1191 state -> assertEquals("getDataChangeListenerActors", 1,
1192 state.getDataChangeListenerActors().size()));
1194 // Wait for the initial notification
1195 listener.waitForChangeEvents(TestModel.TEST_PATH);
1199 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1200 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1202 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1203 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1204 testWriteTransaction(dataStore, listPath,
1205 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1207 // Wait for the 2 updates.
1208 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1209 listenerReg.close();
1211 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1212 state -> assertEquals("getDataChangeListenerActors", 0,
1213 state.getDataChangeListenerActors().size()));
1215 testWriteTransaction(dataStore,
1216 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1217 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1218 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1220 listener.expectNoMoreChanges("Received unexpected change after close");
1227 public void testDataTreeChangeListenerRegistration() throws Exception {
1228 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1230 try (AbstractDataStore dataStore = setupAbstractDataStore(
1231 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
1233 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1234 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1236 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
1238 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
1239 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
1241 assertNotNull("registerTreeChangeListener returned null", listenerReg);
1243 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1244 state -> assertEquals("getTreeChangeListenerActors", 1,
1245 state.getTreeChangeListenerActors().size()));
1247 // Wait for the initial notification
1248 listener.waitForChangeEvents(TestModel.TEST_PATH);
1252 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1253 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1255 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1256 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1257 testWriteTransaction(dataStore, listPath,
1258 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1260 // Wait for the 2 updates.
1261 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1262 listenerReg.close();
1264 IntegrationTestKit.verifyShardState(dataStore, "test-1",
1265 state -> assertEquals("getTreeChangeListenerActors", 0,
1266 state.getTreeChangeListenerActors().size()));
1268 testWriteTransaction(dataStore,
1269 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1270 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1271 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1273 listener.expectNoMoreChanges("Received unexpected change after close");
1280 public void testRestoreFromDatastoreSnapshot() throws Exception {
1281 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1283 final String name = "transactionIntegrationTest";
1285 final ContainerNode carsNode = CarsModel.newCarsNode(
1286 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1287 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1289 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1290 dataTree.setSchemaContext(SchemaContextHelper.full());
1291 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1292 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1294 final Snapshot carsSnapshot = Snapshot.create(
1295 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1296 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1298 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1299 dataTree.setSchemaContext(SchemaContextHelper.full());
1301 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1302 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1304 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1306 final Snapshot peopleSnapshot = Snapshot.create(
1307 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1308 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1310 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1311 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1312 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1314 try (AbstractDataStore dataStore = setupAbstractDataStore(
1315 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1317 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1320 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1321 assertEquals("isPresent", true, optional.isPresent());
1322 assertEquals("Data node", carsNode, optional.get());
1324 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1325 assertEquals("isPresent", true, optional.isPresent());
1326 assertEquals("Data node", peopleNode, optional.get());
1334 public void testRecoveryFromPreCarbonSnapshot() throws Exception {
1335 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1337 final String name = "testRecoveryFromPreCarbonSnapshot";
1339 final ContainerNode carsNode = CarsModel.newCarsNode(
1340 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1341 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1343 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1344 dataTree.setSchemaContext(SchemaContextHelper.full());
1345 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1346 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1348 MetadataShardDataTreeSnapshot shardSnapshot = new MetadataShardDataTreeSnapshot(root);
1349 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
1350 try (DataOutputStream dos = new DataOutputStream(bos)) {
1351 PayloadVersion.BORON.writeTo(dos);
1352 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
1353 oos.writeObject(shardSnapshot);
1357 final org.opendaylight.controller.cluster.raft.Snapshot snapshot =
1358 org.opendaylight.controller.cluster.raft.Snapshot.create(bos.toByteArray(),
1359 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1361 InMemorySnapshotStore.addSnapshot("member-1-shard-cars-" + name, snapshot);
1363 try (AbstractDataStore dataStore = setupAbstractDataStore(
1364 testParameter, name, "module-shards-member1.conf", true, "cars")) {
1366 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1368 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1369 assertEquals("isPresent", true, optional.isPresent());
1370 assertEquals("Data node", carsNode, optional.get());