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.IOException;
35 import java.math.BigInteger;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.ExecutionException;
43 import java.util.concurrent.TimeUnit;
44 import java.util.concurrent.atomic.AtomicReference;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
50 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
51 import org.opendaylight.controller.cluster.datastore.exceptions.NotInitializedException;
52 import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot;
53 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
54 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
55 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
56 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
57 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
58 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
59 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
60 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
61 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
62 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
63 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
64 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
65 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
66 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
67 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
68 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
69 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
70 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
71 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
72 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
73 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
74 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
75 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
76 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
77 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
78 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
79 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
80 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
81 import org.opendaylight.yangtools.concepts.ListenerRegistration;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
83 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
84 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
86 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
87 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
88 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
89 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
90 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
91 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
93 public class DistributedDataStoreIntegrationTest {
95 private static ActorSystem system;
97 private final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
98 .shardHeartbeatIntervalInMillis(100);
101 public static void setUpClass() throws IOException {
102 system = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
103 Address member1Address = AddressFromURIString.parse("akka://cluster-test@127.0.0.1:2558");
104 Cluster.get(system).join(member1Address);
108 public static void tearDownClass() throws IOException {
109 JavaTestKit.shutdownActorSystem(system);
113 protected ActorSystem getSystem() {
118 public void testWriteTransactionWithSingleShard() throws Exception {
119 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
121 try (AbstractDataStore dataStore = setupDistributedDataStore("transactionIntegrationTest",
124 testWriteTransaction(dataStore, TestModel.TEST_PATH,
125 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
127 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
128 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
135 public void testWriteTransactionWithMultipleShards() throws Exception {
136 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
138 try (AbstractDataStore dataStore = setupDistributedDataStore(
139 "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
141 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
142 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
144 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
145 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
147 doCommit(writeTx.ready());
149 writeTx = dataStore.newWriteOnlyTransaction();
151 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
152 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
154 doCommit(writeTx.ready());
156 writeTx = dataStore.newWriteOnlyTransaction();
158 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
159 YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
160 writeTx.write(carPath, car);
162 MapEntryNode person = PeopleModel.newPersonEntry("jack");
163 YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
164 writeTx.write(personPath, person);
166 doCommit(writeTx.ready());
168 // Verify the data in the store
170 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
172 Optional<NormalizedNode<?, ?>> optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
173 assertEquals("isPresent", true, optional.isPresent());
174 assertEquals("Data node", car, optional.get());
176 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
177 assertEquals("isPresent", true, optional.isPresent());
178 assertEquals("Data node", person, optional.get());
185 public void testReadWriteTransactionWithSingleShard() throws Exception {
186 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
188 try (AbstractDataStore dataStore = setupDistributedDataStore(
189 "testReadWriteTransactionWithSingleShard", "test-1")) {
191 // 1. Create a read-write Tx
193 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
194 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
196 // 2. Write some data
198 YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
199 NormalizedNode<?, ?> nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
200 readWriteTx.write(nodePath, nodeToWrite);
202 // 3. Read the data from Tx
204 Boolean exists = readWriteTx.exists(nodePath).checkedGet(5, TimeUnit.SECONDS);
205 assertEquals("exists", true, exists);
207 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS);
208 assertEquals("isPresent", true, optional.isPresent());
209 assertEquals("Data node", nodeToWrite, optional.get());
211 // 4. Ready the Tx for commit
213 DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
219 // 6. Verify the data in the store
221 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
223 optional = readTx.read(nodePath).get(5, TimeUnit.SECONDS);
224 assertEquals("isPresent", true, optional.isPresent());
225 assertEquals("Data node", nodeToWrite, optional.get());
232 public void testReadWriteTransactionWithMultipleShards() throws Exception {
233 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
235 try (AbstractDataStore dataStore = setupDistributedDataStore(
236 "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
238 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
239 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
241 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
242 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
244 doCommit(readWriteTx.ready());
246 readWriteTx = dataStore.newReadWriteTransaction();
248 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
249 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
251 doCommit(readWriteTx.ready());
253 readWriteTx = dataStore.newReadWriteTransaction();
255 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
256 YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
257 readWriteTx.write(carPath, car);
259 MapEntryNode person = PeopleModel.newPersonEntry("jack");
260 YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
261 readWriteTx.write(personPath, person);
263 Boolean exists = readWriteTx.exists(carPath).checkedGet(5, TimeUnit.SECONDS);
264 assertEquals("exists", true, exists);
266 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
267 assertEquals("isPresent", true, optional.isPresent());
268 assertEquals("Data node", car, optional.get());
270 doCommit(readWriteTx.ready());
272 // Verify the data in the store
274 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
276 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
277 assertEquals("isPresent", true, optional.isPresent());
278 assertEquals("Data node", car, optional.get());
280 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
281 assertEquals("isPresent", true, optional.isPresent());
282 assertEquals("Data node", person, optional.get());
290 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
291 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
293 try (AbstractDataStore dataStore = setupDistributedDataStore(
294 "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
296 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
298 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
299 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
300 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
301 doCommit(writeTx.ready());
303 writeTx = txChain.newWriteOnlyTransaction();
306 for (int i = 0; i < numCars; i++) {
307 writeTx.write(CarsModel.newCarPath("car" + i),
308 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
311 doCommit(writeTx.ready());
313 Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
314 .read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
315 assertEquals("isPresent", true, optional.isPresent());
316 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
322 @SuppressWarnings("checkstyle:IllegalCatch")
323 private void testTransactionWritesWithShardNotInitiallyReady(final String testName, final boolean writeOnly)
325 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
327 String shardName = "test-1";
329 // Setup the InMemoryJournal to block shard recovery to ensure
331 // initialized until we create and submit the write the Tx.
332 String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
333 CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
334 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
336 try (AbstractDataStore dataStore = setupDistributedDataStore(testName, false, shardName)) {
338 // Create the write Tx
340 final DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
341 : dataStore.newReadWriteTransaction();
342 assertNotNull("newReadWriteTransaction returned null", writeTx);
344 // Do some modification operations and ready the Tx on a
347 final YangInstanceIdentifier listEntryPath = YangInstanceIdentifier
348 .builder(TestModel.OUTER_LIST_PATH)
349 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
351 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
352 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
353 final CountDownLatch txReady = new CountDownLatch(1);
354 Thread txThread = new Thread() {
358 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
360 writeTx.merge(TestModel.OUTER_LIST_PATH,
361 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
363 writeTx.write(listEntryPath,
364 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
366 writeTx.delete(listEntryPath);
368 txCohort.set(writeTx.ready());
369 } catch (Exception e) {
380 // Wait for the Tx operations to complete.
382 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
383 if (caughtEx.get() != null) {
384 throw caughtEx.get();
387 assertEquals("Tx ready", true, done);
389 // At this point the Tx operations should be waiting for the
390 // shard to initialize so
391 // trigger the latch to let the shard recovery to continue.
393 blockRecoveryLatch.countDown();
395 // Wait for the Tx commit to complete.
397 doCommit(txCohort.get());
399 // Verify the data in the store
401 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
403 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
404 assertEquals("isPresent", true, optional.isPresent());
406 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
407 assertEquals("isPresent", true, optional.isPresent());
409 optional = readTx.read(listEntryPath).get(5, TimeUnit.SECONDS);
410 assertEquals("isPresent", false, optional.isPresent());
417 public void testWriteOnlyTransactionWithShardNotInitiallyReady() throws Exception {
418 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
419 testTransactionWritesWithShardNotInitiallyReady("testWriteOnlyTransactionWithShardNotInitiallyReady", true);
423 public void testReadWriteTransactionWithShardNotInitiallyReady() throws Exception {
424 testTransactionWritesWithShardNotInitiallyReady("testReadWriteTransactionWithShardNotInitiallyReady", false);
428 @SuppressWarnings("checkstyle:IllegalCatch")
429 public void testTransactionReadsWithShardNotInitiallyReady() throws Exception {
430 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
432 String testName = "testTransactionReadsWithShardNotInitiallyReady";
433 String shardName = "test-1";
435 // Setup the InMemoryJournal to block shard recovery to ensure
437 // initialized until we create the Tx.
438 String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
439 CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
440 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
442 try (AbstractDataStore dataStore = setupDistributedDataStore(testName, false, shardName)) {
444 // Create the read-write Tx
446 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
447 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
449 // Do some reads on the Tx on a separate thread.
451 final AtomicReference<CheckedFuture<Boolean, ReadFailedException>> txExistsFuture =
452 new AtomicReference<>();
453 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
454 txReadFuture = new AtomicReference<>();
455 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
456 final CountDownLatch txReadsDone = new CountDownLatch(1);
457 Thread txThread = new Thread() {
461 readWriteTx.write(TestModel.TEST_PATH,
462 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
464 txExistsFuture.set(readWriteTx.exists(TestModel.TEST_PATH));
466 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
467 } catch (Exception e) {
471 txReadsDone.countDown();
478 // Wait for the Tx operations to complete.
480 boolean done = Uninterruptibles.awaitUninterruptibly(txReadsDone, 5, TimeUnit.SECONDS);
481 if (caughtEx.get() != null) {
482 throw caughtEx.get();
485 assertEquals("Tx reads done", true, done);
487 // At this point the Tx operations should be waiting for the
488 // shard to initialize so
489 // trigger the latch to let the shard recovery to continue.
491 blockRecoveryLatch.countDown();
493 // Wait for the reads to complete and verify.
495 assertEquals("exists", true, txExistsFuture.get().checkedGet(5, TimeUnit.SECONDS));
496 assertEquals("read", true, txReadFuture.get().checkedGet(5, TimeUnit.SECONDS).isPresent());
504 @Test(expected = NotInitializedException.class)
505 @SuppressWarnings("checkstyle:IllegalCatch")
506 public void testTransactionCommitFailureWithShardNotInitialized() throws Exception {
507 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
509 String testName = "testTransactionCommitFailureWithShardNotInitialized";
510 String shardName = "test-1";
512 // Set the shard initialization timeout low for the test.
514 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
516 // Setup the InMemoryJournal to block shard recovery
519 String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
520 CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
521 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
523 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
525 try (AbstractDataStore dataStore = setupDistributedDataStore(testName, false, shardName)) {
527 // Create the write Tx
529 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
530 assertNotNull("newReadWriteTransaction returned null", writeTx);
532 // Do some modifications and ready the Tx on a separate
535 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
536 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
537 final CountDownLatch txReady = new CountDownLatch(1);
538 Thread txThread = new Thread() {
542 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
544 txCohort.set(writeTx.ready());
545 } catch (Exception e) {
556 // Wait for the Tx operations to complete.
558 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
559 if (caughtEx.get() != null) {
560 throw caughtEx.get();
563 assertEquals("Tx ready", true, done);
565 // Wait for the commit to complete. Since the shard never
566 // initialized, the Tx should
567 // have timed out and throw an appropriate exception cause.
570 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
571 } catch (ExecutionException e) {
572 Throwables.propagateIfInstanceOf(e.getCause(), Exception.class);
573 Throwables.propagate(e.getCause());
575 blockRecoveryLatch.countDown();
582 @Test(expected = NotInitializedException.class)
583 @SuppressWarnings("checkstyle:IllegalCatch")
584 public void testTransactionReadFailureWithShardNotInitialized() throws Exception {
585 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
587 String testName = "testTransactionReadFailureWithShardNotInitialized";
588 String shardName = "test-1";
590 // Set the shard initialization timeout low for the test.
592 datastoreContextBuilder.shardInitializationTimeout(300, TimeUnit.MILLISECONDS);
594 // Setup the InMemoryJournal to block shard recovery
597 String persistentID = String.format("member-1-shard-%s-%s", shardName, testName);
598 CountDownLatch blockRecoveryLatch = new CountDownLatch(1);
599 InMemoryJournal.addBlockReadMessagesLatch(persistentID, blockRecoveryLatch);
601 InMemoryJournal.addEntry(persistentID, 1, "Dummy data so akka will read from persistence");
603 try (AbstractDataStore dataStore = setupDistributedDataStore(testName, false, shardName)) {
605 // Create the read-write Tx
607 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
608 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
610 // Do a read on the Tx on a separate thread.
612 final AtomicReference<CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>>
613 txReadFuture = new AtomicReference<>();
614 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
615 final CountDownLatch txReadDone = new CountDownLatch(1);
616 Thread txThread = new Thread() {
620 readWriteTx.write(TestModel.TEST_PATH,
621 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
623 txReadFuture.set(readWriteTx.read(TestModel.TEST_PATH));
626 } catch (Exception e) {
630 txReadDone.countDown();
637 // Wait for the Tx operations to complete.
639 boolean done = Uninterruptibles.awaitUninterruptibly(txReadDone, 5, TimeUnit.SECONDS);
640 if (caughtEx.get() != null) {
641 throw caughtEx.get();
644 assertEquals("Tx read done", true, done);
646 // Wait for the read to complete. Since the shard never
647 // initialized, the Tx should
648 // have timed out and throw an appropriate exception cause.
651 txReadFuture.get().checkedGet(5, TimeUnit.SECONDS);
652 } catch (ReadFailedException e) {
653 Throwables.propagateIfInstanceOf(e.getCause(), Exception.class);
654 Throwables.propagate(e.getCause());
656 blockRecoveryLatch.countDown();
663 @SuppressWarnings("checkstyle:IllegalCatch")
664 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
666 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
668 String shardName = "default";
670 // We don't want the shard to become the leader so prevent shard
672 datastoreContextBuilder.customRaftPolicyImplementation(
673 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
675 // The ShardManager uses the election timeout for FindPrimary so
676 // reset it low so it will timeout quickly.
677 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
678 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS);
680 try (AbstractDataStore dataStore = setupDistributedDataStore(testName, false, shardName)) {
682 Object result = dataStore.getActorContext().executeOperation(
683 dataStore.getActorContext().getShardManager(), new FindLocalShard(shardName, true));
684 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
686 // Create the write Tx.
688 try (DOMStoreWriteTransaction writeTx = writeOnly ? dataStore.newWriteOnlyTransaction()
689 : dataStore.newReadWriteTransaction()) {
690 assertNotNull("newReadWriteTransaction returned null", writeTx);
692 // Do some modifications and ready the Tx on a separate
695 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
696 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
697 final CountDownLatch txReady = new CountDownLatch(1);
698 Thread txThread = new Thread() {
702 writeTx.write(TestModel.JUNK_PATH,
703 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
705 txCohort.set(writeTx.ready());
706 } catch (Exception e) {
717 // Wait for the Tx operations to complete.
719 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
720 if (caughtEx.get() != null) {
721 throw caughtEx.get();
724 assertEquals("Tx ready", true, done);
726 // Wait for the commit to complete. Since no shard
727 // leader was elected in time, the Tx
728 // should have timed out and throw an appropriate
732 txCohort.get().canCommit().get(5, TimeUnit.SECONDS);
733 } catch (ExecutionException e) {
734 Throwables.propagateIfInstanceOf(e.getCause(), Exception.class);
735 Throwables.propagate(e.getCause());
743 @Test(expected = NoShardLeaderException.class)
744 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
745 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
746 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
749 @Test(expected = NoShardLeaderException.class)
750 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
751 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
755 public void testTransactionAbort() throws Exception {
756 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
758 try (AbstractDataStore dataStore = setupDistributedDataStore("transactionAbortIntegrationTest",
761 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
762 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
764 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
766 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
768 cohort.canCommit().get(5, TimeUnit.SECONDS);
770 cohort.abort().get(5, TimeUnit.SECONDS);
772 testWriteTransaction(dataStore, TestModel.TEST_PATH,
773 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
780 @SuppressWarnings("checkstyle:IllegalCatch")
781 public void testTransactionChainWithSingleShard() throws Exception {
782 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
784 try (AbstractDataStore dataStore = setupDistributedDataStore("testTransactionChainWithSingleShard",
787 // 1. Create a Tx chain and write-only Tx
789 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
791 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
792 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
794 // 2. Write some data
796 NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
797 writeTx.write(TestModel.TEST_PATH, testNode);
799 // 3. Ready the Tx for commit
801 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
803 // 4. Commit the Tx on another thread that first waits for
804 // the second read Tx.
806 final CountDownLatch continueCommit1 = new CountDownLatch(1);
807 final CountDownLatch commit1Done = new CountDownLatch(1);
808 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
813 continueCommit1.await();
815 } catch (Exception e) {
818 commit1Done.countDown();
823 // 5. Create a new read Tx from the chain to read and verify
824 // the data from the first
825 // Tx is visible after being readied.
827 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
828 Optional<NormalizedNode<?, ?>> optional = readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
829 assertEquals("isPresent", true, optional.isPresent());
830 assertEquals("Data node", testNode, optional.get());
832 // 6. Create a new RW Tx from the chain, write more data,
835 DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
836 MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build();
837 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
839 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
841 // 7. Create a new read Tx from the chain to read the data
842 // from the last RW Tx to
843 // verify it is visible.
845 readTx = txChain.newReadWriteTransaction();
846 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
847 assertEquals("isPresent", true, optional.isPresent());
848 assertEquals("Data node", outerNode, optional.get());
850 // 8. Wait for the 2 commits to complete and close the
853 continueCommit1.countDown();
854 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
856 if (commit1Error.get() != null) {
857 throw commit1Error.get();
864 // 9. Create a new read Tx from the data store and verify
867 readTx = dataStore.newReadOnlyTransaction();
868 optional = readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS);
869 assertEquals("isPresent", true, optional.isPresent());
870 assertEquals("Data node", outerNode, optional.get());
877 public void testTransactionChainWithMultipleShards() throws Exception {
878 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
880 try (AbstractDataStore dataStore = setupDistributedDataStore(
881 "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
883 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
885 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
886 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
888 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
889 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
891 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
892 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
894 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
896 DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
898 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
899 YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
900 readWriteTx.write(carPath, car);
902 MapEntryNode person = PeopleModel.newPersonEntry("jack");
903 YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
904 readWriteTx.merge(personPath, person);
906 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
907 assertEquals("isPresent", true, optional.isPresent());
908 assertEquals("Data node", car, optional.get());
910 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
911 assertEquals("isPresent", true, optional.isPresent());
912 assertEquals("Data node", person, optional.get());
914 DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
916 writeTx = txChain.newWriteOnlyTransaction();
918 writeTx.delete(carPath);
920 DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
922 ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
923 ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
925 doCommit(canCommit1, cohort1);
926 doCommit(canCommit2, cohort2);
931 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
933 optional = readTx.read(carPath).get(5, TimeUnit.SECONDS);
934 assertEquals("isPresent", false, optional.isPresent());
936 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
937 assertEquals("isPresent", true, optional.isPresent());
938 assertEquals("Data node", person, optional.get());
945 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
946 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
948 try (AbstractDataStore dataStore = setupDistributedDataStore(
949 "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
951 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
952 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
953 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
954 MoreExecutors.directExecutor());
956 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
957 DOMTransactionChain txChain = broker.createTransactionChain(listener);
959 List<CheckedFuture<Void, TransactionCommitFailedException>> futures = new ArrayList<>();
961 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
962 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
963 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
964 futures.add(writeTx.submit());
967 for (int i = 0; i < numCars; i++) {
968 DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
970 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
971 CarsModel.newCarEntry("car" + i, BigInteger.valueOf(20000)));
973 futures.add(rwTx.submit());
976 for (CheckedFuture<Void, TransactionCommitFailedException> f : futures) {
980 Optional<NormalizedNode<?, ?>> optional = txChain.newReadOnlyTransaction()
981 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
982 assertEquals("isPresent", true, optional.isPresent());
983 assertEquals("# cars", numCars, ((Collection<?>) optional.get().getValue()).size());
994 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
995 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
997 try (AbstractDataStore dataStore = setupDistributedDataStore(
998 "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
1000 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1002 DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
1006 DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
1008 Optional<NormalizedNode<?, ?>> optional = rwTx2.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
1009 assertEquals("isPresent", false, optional.isPresent());
1018 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
1019 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1021 try (AbstractDataStore dataStore = setupDistributedDataStore(
1022 "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
1024 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1026 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1027 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
1029 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1031 // Try to create another Tx of each type - each should fail
1032 // b/c the previous Tx wasn't
1035 assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
1042 public void testCreateChainedTransactionAfterClose() throws Exception {
1043 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1045 try (AbstractDataStore dataStore = setupDistributedDataStore(
1046 "testCreateChainedTransactionAfterClose", "test-1")) {
1048 DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1052 // Try to create another Tx of each type - should fail b/c
1053 // the previous Tx was closed.
1055 assertExceptionOnTxChainCreates(txChain, TransactionChainClosedException.class);
1062 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
1063 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1065 try (AbstractDataStore dataStore = setupDistributedDataStore(
1066 "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
1068 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
1070 // Create a write tx and submit.
1072 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1073 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1074 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1076 // Create read-only tx's and issue a read.
1078 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture1 = txChain
1079 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1081 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readFuture2 = txChain
1082 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
1084 // Create another write tx and issue the write.
1086 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
1087 writeTx2.write(TestModel.OUTER_LIST_PATH,
1088 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1090 // Ensure the reads succeed.
1092 assertEquals("isPresent", true, readFuture1.checkedGet(5, TimeUnit.SECONDS).isPresent());
1093 assertEquals("isPresent", true, readFuture2.checkedGet(5, TimeUnit.SECONDS).isPresent());
1095 // Ensure the writes succeed.
1097 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
1102 assertEquals("isPresent", true, txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
1103 .checkedGet(5, TimeUnit.SECONDS).isPresent());
1110 public void testChainedTransactionFailureWithSingleShard() throws Exception {
1111 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1113 try (AbstractDataStore dataStore = setupDistributedDataStore(
1114 "testChainedTransactionFailureWithSingleShard", "cars-1")) {
1116 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1117 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1118 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1119 MoreExecutors.directExecutor());
1121 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1122 DOMTransactionChain txChain = broker.createTransactionChain(listener);
1124 DOMDataReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
1126 ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1127 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1128 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1130 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1133 rwTx.submit().checkedGet(5, TimeUnit.SECONDS);
1134 fail("Expected TransactionCommitFailedException");
1135 } catch (TransactionCommitFailedException e) {
1139 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(rwTx),
1140 any(Throwable.class));
1150 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
1151 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1153 try (AbstractDataStore dataStore = setupDistributedDataStore(
1154 "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
1156 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
1157 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
1158 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
1159 MoreExecutors.directExecutor());
1161 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
1162 DOMTransactionChain txChain = broker.createTransactionChain(listener);
1164 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
1166 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
1167 PeopleModel.emptyContainer());
1169 ContainerNode invalidData = ImmutableContainerNodeBuilder.create()
1170 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
1171 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
1173 // Note that merge will validate the data and fail but put
1174 // succeeds b/c deep validation is not
1175 // done for put for performance reasons.
1176 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
1179 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
1180 fail("Expected TransactionCommitFailedException");
1181 } catch (TransactionCommitFailedException e) {
1185 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
1186 any(Throwable.class));
1196 public void testChangeListenerRegistration() throws Exception {
1197 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1199 try (AbstractDataStore dataStore = setupDistributedDataStore("testChangeListenerRegistration",
1202 testWriteTransaction(dataStore, TestModel.TEST_PATH,
1203 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
1205 MockDataChangeListener listener = new MockDataChangeListener(1);
1207 ListenerRegistration<MockDataChangeListener> listenerReg = dataStore
1208 .registerChangeListener(TestModel.TEST_PATH, listener, DataChangeScope.SUBTREE);
1210 assertNotNull("registerChangeListener returned null", listenerReg);
1212 // Wait for the initial notification
1214 listener.waitForChangeEvents(TestModel.TEST_PATH);
1220 testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
1221 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
1223 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1224 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
1225 testWriteTransaction(dataStore, listPath,
1226 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
1228 // Wait for the 2 updates.
1230 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
1232 listenerReg.close();
1234 testWriteTransaction(dataStore,
1235 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
1236 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
1237 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
1239 listener.expectNoMoreChanges("Received unexpected change after close");
1246 public void testRestoreFromDatastoreSnapshot() throws Exception {
1247 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1249 final String name = "transactionIntegrationTest";
1251 ContainerNode carsNode = CarsModel.newCarsNode(
1252 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1253 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1255 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1256 dataTree.setSchemaContext(SchemaContextHelper.full());
1257 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1258 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1260 final Snapshot carsSnapshot = Snapshot.create(
1261 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1262 Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1264 NormalizedNode<?, ?> peopleNode = PeopleModel.create();
1265 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1266 dataTree.setSchemaContext(SchemaContextHelper.full());
1267 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
1268 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1270 Snapshot peopleSnapshot = Snapshot.create(
1271 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
1272 Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1274 restoreFromSnapshot = new DatastoreSnapshot(name, null,
1276 new DatastoreSnapshot.ShardSnapshot("cars",
1277 org.apache.commons.lang3.SerializationUtils.serialize(carsSnapshot)),
1278 new DatastoreSnapshot.ShardSnapshot("people",
1279 org.apache.commons.lang3.SerializationUtils.serialize(peopleSnapshot))));
1281 try (AbstractDataStore dataStore = setupDistributedDataStore(name, "module-shards-member1.conf",
1282 true, "cars", "people")) {
1284 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1286 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1287 assertEquals("isPresent", true, optional.isPresent());
1288 assertEquals("Data node", carsNode, optional.get());
1290 optional = readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1291 assertEquals("isPresent", true, optional.isPresent());
1292 assertEquals("Data node", peopleNode, optional.get());
1300 public void testRecoveryFromPreCarbonSnapshot() throws Exception {
1301 new IntegrationTestKit(getSystem(), datastoreContextBuilder) {
1303 final String name = "testRecoveryFromPreCarbonSnapshot";
1305 ContainerNode carsNode = CarsModel.newCarsNode(
1306 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000L)),
1307 CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000L))));
1309 DataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
1310 dataTree.setSchemaContext(SchemaContextHelper.full());
1311 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
1312 NormalizedNode<?, ?> root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.EMPTY);
1314 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
1315 new MetadataShardDataTreeSnapshot(root).serialize(bos);
1316 final org.opendaylight.controller.cluster.raft.Snapshot snapshot =
1317 org.opendaylight.controller.cluster.raft.Snapshot.create(bos.toByteArray(),
1318 Collections.<ReplicatedLogEntry>emptyList(), 2, 1, 2, 1, 1, "member-1", null);
1320 InMemorySnapshotStore.addSnapshot("member-1-shard-cars-" + name, snapshot);
1322 try (AbstractDataStore dataStore = setupDistributedDataStore(name, "module-shards-member1.conf",
1325 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
1327 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1328 assertEquals("isPresent", true, optional.isPresent());
1329 assertEquals("Data node", carsNode, optional.get());