2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.datastore;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.verify;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.AddressFromURIString;
23 import akka.cluster.Cluster;
24 import akka.testkit.JavaTestKit;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Throwables;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.util.concurrent.CheckedFuture;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import com.google.common.util.concurrent.MoreExecutors;
31 import com.google.common.util.concurrent.Uninterruptibles;
32 import com.typesafe.config.ConfigFactory;
33 import java.io.ByteArrayOutputStream;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import java.io.ObjectOutputStream;
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.concurrent.CountDownLatch;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.TimeUnit;
46 import java.util.concurrent.atomic.AtomicReference;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.junit.runners.Parameterized;
52 import org.junit.runners.Parameterized.Parameter;
53 import org.junit.runners.Parameterized.Parameters;
54 import org.mockito.Mockito;
55 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
56 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
57 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
58 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
59 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
60 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
61 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
62 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
63 import org.opendaylight.controller.cluster.datastore.persisted.PayloadVersion;
64 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
65 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
66 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
67 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
68 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
69 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
70 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
71 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
72 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
73 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
74 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
75 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
76 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
77 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
78 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
79 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
80 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
81 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
82 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
83 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
84 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
85 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
86 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
87 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
88 import org.opendaylight.yangtools.concepts.ListenerRegistration;
89 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
90 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
91 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
92 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
93 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
94 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
95 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
96 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
97 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
98 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
100 @RunWith(Parameterized.class)
101 public class DistributedDataStoreIntegrationTest {
103 @Parameters(name = "{0}")
104 public static Collection<Object[]> data() {
105 return Arrays.asList(new Object[][] {
106 { DistributedDataStore.class }, { ClientBackedDataStore.class }
111 public Class<? extends AbstractDataStore> testParameter;
113 private ActorSystem system;
115 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
116 .shardHeartbeatIntervalInMillis(100);
119 public void setUp() throws IOException {
120 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
121 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
122 Cluster.get(system).join(member1Address);
126 public void tearDown() throws IOException {
127 JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
131 protected ActorSystem getSystem() {
136 public void testWriteTransactionWithSingleShard() throws Exception {
137 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
139 try (final AbstractDataStore dataStore = setupAbstractDataStore(
140 testParameter, "transactionIntegrationTest", "test-1")) {
142 testWriteTransaction(dataStore, TestModel.TEST_PATH,
143 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
145 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
146 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
153 public void testWriteTransactionWithMultipleShards() throws Exception {
154 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
156 try (final AbstractDataStore dataStore = setupAbstractDataStore(
157 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
159 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
160 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
162 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
163 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
165 doCommit(writeTx.ready());
167 writeTx = dataStore.newWriteOnlyTransaction();
169 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
170 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
172 doCommit(writeTx.ready());
174 writeTx = dataStore.newWriteOnlyTransaction();
176 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
177 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
178 writeTx.write(carPath, car);
180 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
181 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
182 writeTx.write(personPath, person);
184 doCommit(writeTx.ready());
186 // Verify the data in the store
187 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
189 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
190 assertEquals("isPresent", true, optional.isPresent());
191 assertEquals("Data node", car, optional.get());
193 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
194 assertEquals("isPresent", true, optional.isPresent());
195 assertEquals("Data node", person, optional.get());
202 public void testReadWriteTransactionWithSingleShard() throws Exception {
203 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
205 try (final AbstractDataStore dataStore = setupAbstractDataStore(
206 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
208 // 1. Create a read-write Tx
209 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
210 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
212 // 2. Write some data
213 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
214 final NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
215 readWriteTx.write(nodePath, nodeToWrite);
217 // 3. Read the data from Tx
218 final Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
219 assertEquals("exists", true, exists);
221 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
222 assertEquals("isPresent", true, optional.isPresent());
223 assertEquals("Data node", nodeToWrite, optional.get());
225 // 4. Ready the Tx for commit
226 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
231 // 6. Verify the data in the store
232 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
234 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
235 assertEquals("isPresent", true, optional.isPresent());
236 assertEquals("Data node", nodeToWrite, optional.get());
243 public void testReadWriteTransactionWithMultipleShards() throws Exception {
244 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
246 try (final AbstractDataStore dataStore = setupAbstractDataStore(
247 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
249 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
250 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
252 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
253 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
255 doCommit(readWriteTx.ready());
257 readWriteTx = dataStore.newReadWriteTransaction();
259 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
260 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
262 doCommit(readWriteTx.ready());
264 readWriteTx = dataStore.newReadWriteTransaction();
266 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
267 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
268 readWriteTx.write(carPath, car);
270 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
271 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
272 readWriteTx.write(personPath, person);
274 final Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
275 assertEquals("exists", true, exists);
277 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
278 assertEquals("isPresent", true, optional.isPresent());
279 assertEquals("Data node", car, optional.get());
281 doCommit(readWriteTx.ready());
283 // Verify the data in the store
284 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
286 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
287 assertEquals("isPresent", true, optional.isPresent());
288 assertEquals("Data node", car, optional.get());
290 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
291 assertEquals("isPresent", true, optional.isPresent());
292 assertEquals("Data node", person, optional.get());
300 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
301 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
303 try (final AbstractDataStore dataStore = setupAbstractDataStore(
304 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
306 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
308 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
309 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
310 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
311 doCommit(writeTx.ready());
313 writeTx = txChain.newWriteOnlyTransaction();
316 for (int i = 0; i < numCars; i++) {
317 writeTx.write(CarsModel.newCarPath("car" + i),
318 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
321 doCommit(writeTx.ready());
323 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
324 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
325 assertEquals("isPresent", true, optional.isPresent());
326 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
332 @SuppressWarnings("checkstyle:IllegalCatch")
333 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
335 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
337 final String shardName = "test-1";
339 // Setup the InMemoryJournal to block shard recovery to ensure
341 // initialized until we create and submit the write the Tx.
342 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
343 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
344 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
346 try (final AbstractDataStore dataStore = setupAbstractDataStore(
347 testParameter, testName, false, shardName)) {
349 // Create the write Tx
350 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
351 : dataStore.newReadWriteTransaction();
352 assertNotNull("newReadWriteTransaction returned null", writeTx);
354 // Do some modification operations and ready the Tx on a
356 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
357 .builder(TestModel.OUTER_LIST_PATH)
358 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
360 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
361 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
362 final CountDownLatch txReady = new CountDownLatch(1);
363 final Thread txThread = new Thread() {
367 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
369 writeTx.merge(TestModel.OUTER_LIST_PATH,
370 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
372 writeTx.write(listEntryPath,
373 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
375 writeTx.delete(listEntryPath);
377 txCohort.set(writeTx.ready());
378 } catch (Exception e) {
388 // Wait for the Tx operations to complete.
389 final boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
390 if (caughtEx.get() != null) {
391 throw caughtEx.get();
394 assertEquals("Tx ready", true, done);
396 // At this point the Tx operations should be waiting for the
397 // shard to initialize so
398 // trigger the latch to let the shard recovery to continue.
399 blockRecoveryLatch.countDown();
401 // Wait for the Tx commit to complete.
402 doCommit(txCohort.get());
404 // Verify the data in the store
405 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
407 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
408 assertEquals("isPresent", true, optional.isPresent());
410 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
411 assertEquals("isPresent", true, optional.isPresent());
413 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
414 assertEquals("isPresent", false, optional.isPresent());
421 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
422 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
423 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
427 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
428 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
432 @SuppressWarnings("checkstyle:IllegalCatch")
433 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
434 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
436 final String testName = "testTransactionReadsWithShardNotInitiallyReady";
437 final String shardName = "test-1";
439 // Setup the InMemoryJournal to block shard recovery to ensure
441 // initialized until we create the Tx.
442 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
443 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
444 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
446 try (final AbstractDataStore dataStore = setupAbstractDataStore(
447 testParameter, testName, false, shardName)) {
449 // Create the read-write Tx
450 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
451 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
453 // Do some reads on the Tx on a separate thread.
454 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
455 new AtomicReference<>();
456 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
457 txReadFuture = new AtomicReference<>();
458 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
459 final CountDownLatch txReadsDone = new CountDownLatch(1);
460 final Thread txThread = new Thread() {
464 readWriteTx.write(TestModel.TEST_PATH,
465 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
467 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
469 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
470 } catch (Exception e) {
473 txReadsDone.countDown();
480 // Wait for the Tx operations to complete.
481 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
482 if (caughtEx.get() != null) {
483 throw caughtEx.get();
486 assertEquals("Tx reads done", true, done);
488 // At this point the Tx operations should be waiting for the
489 // shard to initialize so
490 // trigger the latch to let the shard recovery to continue.
491 blockRecoveryLatch.countDown();
493 // Wait for the reads to complete and verify.
494 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
495 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
503 @Test(expected = NotInitializedException.class)
504 @SuppressWarnings("checkstyle:IllegalCatch")
505 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
506 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
508 final String testName = "testTransactionCommitFailureWithShardNotInitialized";
509 final String shardName = "test-1";
511 // Set the shard initialization timeout low for the test.
512 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
514 // Setup the InMemoryJournal to block shard recovery
516 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
517 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
518 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
520 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
522 final AbstractDataStore dataStore =
523 setupAbstractDataStore(testParameter, testName, false, shardName);
525 // Create the write Tx
526 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
527 assertNotNull("newReadWriteTransaction returned null", writeTx);
529 // Do some modifications and ready the Tx on a separate
531 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
532 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
533 final CountDownLatch txReady = new CountDownLatch(1);
534 final Thread txThread = new Thread(() -> {
536 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
538 txCohort.set(writeTx.ready());
539 } catch (Exception e) {
548 // Wait for the Tx operations to complete.
549 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
550 if (caughtEx.get() != null) {
551 throw caughtEx.get();
554 assertEquals("Tx ready", true, done);
556 // Wait for the commit to complete. Since the shard never
557 // initialized, the Tx should
558 // have timed out and throw an appropriate exception cause.
560 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
561 fail("Expected NotInitializedException");
562 } catch (final Exception e) {
563 Throwables.propagate(Throwables.getRootCause(e));
565 blockRecoveryLatch.countDown();
571 @Test(expected = NotInitializedException.class)
572 @SuppressWarnings("checkstyle:IllegalCatch")
573 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
574 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
576 final String testName = "testTransactionReadFailureWithShardNotInitialized";
577 final String shardName = "test-1";
579 // Set the shard initialization timeout low for the test.
580 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
582 // Setup the InMemoryJournal to block shard recovery
584 final String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
585 final CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
586 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
588 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
590 try (final AbstractDataStore dataStore = setupAbstractDataStore(
591 testParameter, testName, false, shardName)) {
593 // Create the read-write Tx
594 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
595 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
597 // Do a read on the Tx on a separate thread.
598 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
599 txReadFuture = new AtomicReference<>();
600 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
601 final CountDownLatch txReadDone = new CountDownLatch(1);
602 final Thread txThread = new Thread(() -> {
604 readWriteTx.write(TestModel.TEST_PATH,
605 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
607 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
610 } catch (Exception e) {
613 txReadDone.countDown();
619 // Wait for the Tx operations to complete.
620 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
621 if (caughtEx.get() != null) {
622 throw caughtEx.get();
625 assertEquals("Tx read done", true, done);
627 // Wait for the read to complete. Since the shard never
628 // initialized, the Tx should
629 // have timed out and throw an appropriate exception cause.
631 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
632 fail("Expected NotInitializedException");
633 } catch (final ReadFailedException e) {
634 Throwables.propagate(Throwables.getRootCause(e));
636 blockRecoveryLatch.countDown();
643 @SuppressWarnings("checkstyle:IllegalCatch")
644 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
646 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
648 final String shardName = "default";
650 // We don't want the shard to become the leader so prevent shard
652 datastoreContextBuilder.customRaftPolicyImplementation(
653 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
655 // The ShardManager uses the election timeout for FindPrimary so
656 // reset it low so it will timeout quickly.
657 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
658 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
660 try (final AbstractDataStore dataStore = setupAbstractDataStore(
661 testParameter, testName, false, shardName)) {
663 final Object result = dataStore.getActorContext().executeOperation(
664 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
665 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
667 // Create the write Tx.
668 try (final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
669 : dataStore.newReadWriteTransaction()) {
670 assertNotNull("newReadWriteTransaction returned null", writeTx);
672 // Do some modifications and ready the Tx on a separate
674 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
675 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
676 final CountDownLatch txReady = new CountDownLatch(1);
677 final Thread txThread = new Thread(() -> {
679 writeTx.write(TestModel.JUNK_PATH,
680 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
682 txCohort.set(writeTx.ready());
683 } catch (Exception e) {
692 // Wait for the Tx operations to complete.
693 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
694 if (caughtEx.get() != null) {
695 throw caughtEx.get();
698 assertEquals("Tx ready", true, done);
700 // Wait for the commit to complete. Since no shard
701 // leader was elected in time, the Tx
702 // should have timed out and throw an appropriate
705 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
706 fail("Expected NoShardLeaderException");
707 } catch (final ExecutionException e) {
708 Throwables.propagate(Throwables.getRootCause(e));
716 @Test(expected = NoShardLeaderException.class)
717 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
718 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
719 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
722 @Test(expected = NoShardLeaderException.class)
723 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
724 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
728 public void testTransactionAbort() throws Exception {
729 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
731 try (final AbstractDataStore dataStore = setupAbstractDataStore(
732 testParameter, "transactionAbortIntegrationTest", "test-1")) {
734 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
735 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
737 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
739 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
741 cohort.canCommit().get(5, TimeUnit.SECONDS);
743 cohort.abort().get(5, TimeUnit.SECONDS);
745 testWriteTransaction(dataStore, TestModel.TEST_PATH,
746 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
753 @SuppressWarnings("checkstyle:IllegalCatch")
754 public void testTransactionChainWithSingleShard() throws Exception {
755 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
757 try (final AbstractDataStore dataStore = setupAbstractDataStore(
758 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
760 // 1. Create a Tx chain and write-only Tx
761 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
763 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
764 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
766 // 2. Write some data
767 final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
768 writeTx.write(TestModel.TEST_PATH, testNode);
770 // 3. Ready the Tx for commit
771 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
773 // 4. Commit the Tx on another thread that first waits for
774 // the second read Tx.
775 final CountDownLatch continueCommit1 = new CountDownLatch(1);
776 final CountDownLatch commit1Done = new CountDownLatch(1);
777 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
780 continueCommit1.await();
782 } catch (Exception e) {
785 commit1Done.countDown();
789 // 5. Create a new read Tx from the chain to read and verify
790 // the data from the first
791 // Tx is visible after being readied.
792 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
793 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
794 assertEquals("isPresent", true, optional.isPresent());
795 assertEquals("Data node", testNode, optional.get());
797 // 6. Create a new RW Tx from the chain, write more data,
799 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
800 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
801 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
803 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
805 // 7. Create a new read Tx from the chain to read the data
806 // from the last RW Tx to
807 // verify it is visible.
808 readTx = txChain.newReadWriteTransaction();
809 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
810 assertEquals("isPresent", true, optional.isPresent());
811 assertEquals("Data node", outerNode, optional.get());
813 // 8. Wait for the 2 commits to complete and close the
815 continueCommit1.countDown();
816 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
818 if (commit1Error.get() != null) {
819 throw commit1Error.get();
826 // 9. Create a new read Tx from the data store and verify
828 readTx = dataStore.newReadOnlyTransaction();
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());
838 public void testTransactionChainWithMultipleShards() throws Exception {
839 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
841 try (final AbstractDataStore dataStore = setupAbstractDataStore(
842 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
844 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
846 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
847 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
849 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
850 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
852 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
853 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
855 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
857 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
859 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
860 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
861 readWriteTx.write(carPath, car);
863 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
864 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
865 readWriteTx.merge(personPath, person);
867 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
868 assertEquals("isPresent", true, optional.isPresent());
869 assertEquals("Data node", car, optional.get());
871 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
872 assertEquals("isPresent", true, optional.isPresent());
873 assertEquals("Data node", person, optional.get());
875 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
877 writeTx = txChain.newWriteOnlyTransaction();
879 writeTx.delete(carPath);
881 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
883 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
884 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
886 doCommit(canCommit1, cohort1);
887 doCommit(canCommit2, cohort2);
892 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
894 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
895 assertEquals("isPresent", false, optional.isPresent());
897 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
898 assertEquals("isPresent", true, optional.isPresent());
899 assertEquals("Data node", person, optional.get());
906 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
907 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
909 try (final AbstractDataStore dataStore = setupAbstractDataStore(
910 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
912 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
913 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
914 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
915 MoreExecutors.directExecutor());
917 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
918 DOMTransactionChain txChain = broker.createTransactionChain(listener);
920 final List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
922 final DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
923 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
924 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
925 futures.add(writeTx.submit());
928 for (int i = 0; i < numCars; i++) {
929 final DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
931 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
932 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
934 futures.add(rwTx.submit());
937 for (final CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
941 final Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
942 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
943 assertEquals("isPresent", true, optional.isPresent());
944 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
955 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
956 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
958 try (final AbstractDataStore dataStore = setupAbstractDataStore(
959 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
961 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
963 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
967 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
969 final Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(
970 5, TimeUnit.SECONDS);
971 assertEquals("isPresent", false, optional.isPresent());
980 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
981 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
983 try (final AbstractDataStore dataStore = setupAbstractDataStore(
984 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
986 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
988 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
989 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
991 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
993 // Try to create another Tx of each type - each should fail
994 // b/c the previous Tx wasn't
996 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1003 public void testCreateChainedTransactionAfterClose() throws Exception {
1004 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1006 try (AbstractDataStore dataStore = setupAbstractDataStore(
1007 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
1009 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1012 // Try to create another Tx of each type - should fail b/c
1013 // the previous Tx was closed.
1014 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1021 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1022 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1024 try (final AbstractDataStore dataStore = setupAbstractDataStore(
1025 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1027 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1029 // Create a write tx and submit.
1030 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1031 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1032 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1034 // Create read-only tx's and issue a read.
1035 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1036 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1038 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1039 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1041 // Create another write tx and issue the write.
1042 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1043 writeTx2.write(TestModel.OUTER_LIST_PATH,
1044 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1046 // Ensure the reads succeed.
1048 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1049 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1051 // Ensure the writes succeed.
1052 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1057 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1058 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1065 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1066 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1068 try (final AbstractDataStore dataStore = setupAbstractDataStore(
1069 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1071 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1072 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1073 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1074 MoreExecutors.directExecutor());
1076 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1077 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1079 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1081 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1082 PeopleModel.emptyContainer());
1084 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1085 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1086 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1088 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1091 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1092 fail("Expected TransactionCommitFailedException");
1093 } catch (final TransactionCommitFailedException e) {
1097 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1098 any(Throwable.class));
1108 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1109 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1111 try (final AbstractDataStore dataStore = setupAbstractDataStore(
1112 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1114 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1115 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1116 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1117 MoreExecutors.directExecutor());
1119 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1120 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
1122 final DOMDataReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
1124 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1125 PeopleModel.emptyContainer());
1127 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1128 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1129 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1131 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1133 // Note that merge will validate the data and fail but put
1134 // succeeds b/c deep validation is not
1135 // done for put for performance reasons.
1137 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1138 fail("Expected TransactionCommitFailedException");
1139 } catch (final TransactionCommitFailedException e) {
1143 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1144 any(Throwable.class));
1154 public void testChangeListenerRegistration() throws Exception {
1155 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1157 try (final AbstractDataStore dataStore = setupAbstractDataStore(
1158 testParameter, "testChangeListenerRegistration", "test-1")) {
1160 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1161 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1163 final MockDataChangeListener listener = new MockDataChangeListener(1);
1165 final ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1166 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1168 assertNotNull("registerChangeListener returned null", listenerReg);
1170 // Wait for the initial notification
1171 listener.waitForChangeEvents(TestModel.TEST_PATH);
1175 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1176 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1178 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1179 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1180 testWriteTransaction(dataStore, listPath,
1181 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1183 // Wait for the 2 updates.
1184 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1185 listenerReg.close();
1187 testWriteTransaction(dataStore,
1188 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1189 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1190 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1192 listener.expectNoMoreChanges("Received unexpected change after close");
1199 public void testRestoreFromDatastoreSnapshot() throws Exception {
1200 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1202 final String name = "transactionIntegrationTest";
1204 final ContainerNode carsNode = CarsModel.newCarsNode(
1205 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1206 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1208 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1209 dataTree.setSchemaContext(SchemaContextHelper.full());
1210 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1211 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1213 final Snapshot carsSnapshot = Snapshot.create(
1214 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1215 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1217 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1218 dataTree.setSchemaContext(SchemaContextHelper.full());
1220 final NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1221 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1223 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1225 final Snapshot peopleSnapshot = Snapshot.create(
1226 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1227 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1229 restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
1230 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
1231 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
1233 try (final AbstractDataStore dataStore = setupAbstractDataStore(
1234 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
1236 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1239 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1240 assertEquals("isPresent", true, optional.isPresent());
1241 assertEquals("Data node", carsNode, optional.get());
1243 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1244 assertEquals("isPresent", true, optional.isPresent());
1245 assertEquals("Data node", peopleNode, optional.get());
1253 public void testRecoveryFromPreCarbonSnapshot() throws Exception {
1254 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1256 final String name = "testRecoveryFromPreCarbonSnapshot";
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 MetadataShardDataTreeSnapshot shardSnapshot = new MetadataShardDataTreeSnapshot(root);
1268 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
1269 try (final DataOutputStream dos = new DataOutputStream(bos)) {
1270 PayloadVersion.BORON.writeTo(dos);
1271 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
1272 oos.writeObject(shardSnapshot);
1276 final org.opendaylight.controller.cluster.raft.Snapshot snapshot =
1277 org.opendaylight.controller.cluster.raft.Snapshot.create(bos.toByteArray(),
1278 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1280 InMemorySnapshotStore.addSnapshot("member-1-shard-cars-" + name, snapshot);
1282 try (AbstractDataStore dataStore = setupAbstractDataStore(
1283 testParameter, name, "module-shards-member1.conf", true, "cars")) {
1285 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1287 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1288 assertEquals("isPresent", true, optional.isPresent());
1289 assertEquals("Data node", carsNode, optional.get());