2 * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.awaitility.Awaitility.await;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.timeout;
21 import static org.mockito.Mockito.verify;
23 import akka.actor.ActorSystem;
24 import com.google.common.base.Throwables;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.util.concurrent.FluentFuture;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.MoreExecutors;
29 import com.google.common.util.concurrent.Uninterruptibles;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.concurrent.CountDownLatch;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.atomic.AtomicReference;
40 import org.junit.Test;
41 import org.junit.runners.Parameterized.Parameter;
42 import org.mockito.Mockito;
43 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
44 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
45 import org.opendaylight.controller.cluster.datastore.TestShard.RequestFrontendMetadata;
46 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
47 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
48 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
49 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
50 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
51 import org.opendaylight.controller.cluster.datastore.persisted.FrontendShardDataTreeSnapshotMetadata;
52 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
53 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
54 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
55 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
56 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
57 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
58 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
59 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
60 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
61 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
62 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
63 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
64 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
65 import org.opendaylight.mdsal.dom.api.DOMTransactionChainClosedException;
66 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
67 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
68 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
69 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
70 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
71 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
72 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
73 import org.opendaylight.yangtools.concepts.ListenerRegistration;
74 import org.opendaylight.yangtools.yang.common.Uint64;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
76 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
77 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
81 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
82 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
83 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
84 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
85 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
86 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
88 public abstract class AbstractDistributedDataStoreIntegrationTest {
91 public Class<? extends AbstractDataStore> testParameter;
93 protected ActorSystem system;
95 protected final DatastoreContext.Builder datastoreContextBuilder = DatastoreContext.newBuilder()
96 .shardHeartbeatIntervalInMillis(100);
98 protected ActorSystem getSystem() {
103 public void testWriteTransactionWithSingleShard() throws Exception {
104 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
105 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
106 testParameter, "transactionIntegrationTest", "test-1")) {
108 testKit.testWriteTransaction(dataStore, TestModel.TEST_PATH,
109 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
111 testKit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
112 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
113 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
119 public void testWriteTransactionWithMultipleShards() throws Exception {
120 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
121 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
122 testParameter, "testWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
124 DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
125 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
127 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
128 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
130 testKit.doCommit(writeTx.ready());
132 writeTx = dataStore.newWriteOnlyTransaction();
134 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
135 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
137 testKit.doCommit(writeTx.ready());
139 writeTx = dataStore.newWriteOnlyTransaction();
141 final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
142 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
143 writeTx.write(carPath, car);
145 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
146 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
147 writeTx.write(personPath, person);
149 testKit.doCommit(writeTx.ready());
151 // Verify the data in the store
152 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
154 assertEquals(Optional.of(car), readTx.read(carPath).get(5, TimeUnit.SECONDS));
155 assertEquals(Optional.of(person), readTx.read(personPath).get(5, TimeUnit.SECONDS));
160 public void testReadWriteTransactionWithSingleShard() throws Exception {
161 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
162 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
163 testParameter, "testReadWriteTransactionWithSingleShard", "test-1")) {
165 // 1. Create a read-write Tx
166 final DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
167 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
169 // 2. Write some data
170 final YangInstanceIdentifier nodePath = TestModel.TEST_PATH;
171 final NormalizedNode nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
172 readWriteTx.write(nodePath, nodeToWrite);
174 // 3. Read the data from Tx
175 final Boolean exists = readWriteTx.exists(nodePath).get(5, TimeUnit.SECONDS);
176 assertEquals("exists", Boolean.TRUE, exists);
178 assertEquals(Optional.of(nodeToWrite), readWriteTx.read(nodePath).get(5, TimeUnit.SECONDS));
180 // 4. Ready the Tx for commit
181 final DOMStoreThreePhaseCommitCohort cohort = readWriteTx.ready();
184 testKit.doCommit(cohort);
186 // 6. Verify the data in the store
187 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
189 assertEquals(Optional.of(nodeToWrite), readTx.read(nodePath).get(5, TimeUnit.SECONDS));
194 public void testReadWriteTransactionWithMultipleShards() throws Exception {
195 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
196 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
197 testParameter, "testReadWriteTransactionWithMultipleShards", "cars-1", "people-1")) {
199 DOMStoreReadWriteTransaction readWriteTx = dataStore.newReadWriteTransaction();
200 assertNotNull("newReadWriteTransaction returned null", readWriteTx);
202 readWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
203 readWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
205 testKit.doCommit(readWriteTx.ready());
207 readWriteTx = dataStore.newReadWriteTransaction();
209 readWriteTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
210 readWriteTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
212 testKit.doCommit(readWriteTx.ready());
214 readWriteTx = dataStore.newReadWriteTransaction();
216 final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
217 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
218 readWriteTx.write(carPath, car);
220 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
221 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
222 readWriteTx.write(personPath, person);
224 final Boolean exists = readWriteTx.exists(carPath).get(5, TimeUnit.SECONDS);
225 assertEquals("exists", Boolean.TRUE, exists);
227 assertEquals("Data node", Optional.of(car), readWriteTx.read(carPath).get(5, TimeUnit.SECONDS));
229 testKit.doCommit(readWriteTx.ready());
231 // Verify the data in the store
232 DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
234 assertEquals(Optional.of(car), readTx.read(carPath).get(5, TimeUnit.SECONDS));
235 assertEquals(Optional.of(person), readTx.read(personPath).get(5, TimeUnit.SECONDS));
240 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
241 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
242 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
243 testParameter, "testSingleTransactionsWritesInQuickSuccession", "cars-1")) {
245 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
247 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
248 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
249 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
250 testKit.doCommit(writeTx.ready());
253 for (int i = 0; i < numCars; i++) {
254 writeTx = txChain.newWriteOnlyTransaction();
255 writeTx.write(CarsModel.newCarPath("car" + i), CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000)));
257 testKit.doCommit(writeTx.ready());
259 try (var tx = txChain.newReadOnlyTransaction()) {
260 tx.read(CarsModel.BASE_PATH).get();
264 // wait to let the shard catch up with purged
265 await("transaction state propagation").atMost(5, TimeUnit.SECONDS)
266 .pollInterval(500, TimeUnit.MILLISECONDS)
267 .untilAsserted(() -> {
268 // verify frontend metadata has no holes in purged transactions causing overtime memory leak
269 final var localShard = dataStore.getActorUtils().findLocalShard("cars-1") .orElseThrow();
270 FrontendShardDataTreeSnapshotMetadata frontendMetadata =
271 (FrontendShardDataTreeSnapshotMetadata) dataStore.getActorUtils()
272 .executeOperation(localShard, new RequestFrontendMetadata());
274 final var clientMeta = frontendMetadata.getClients().get(0);
275 if (dataStore.getActorUtils().getDatastoreContext().isUseTellBasedProtocol()) {
276 assertTellMetadata(clientMeta);
278 assertAskMetadata(clientMeta);
282 final var body = txChain.newReadOnlyTransaction().read(CarsModel.CAR_LIST_PATH)
283 .get(5, TimeUnit.SECONDS)
286 assertThat(body, instanceOf(Collection.class));
287 assertEquals("# cars", numCars, ((Collection<?>) body).size());
291 private static void assertAskMetadata(final FrontendClientMetadata clientMeta) {
292 // ask based should track no metadata
293 assertEquals(List.of(), clientMeta.getCurrentHistories());
296 private static void assertTellMetadata(final FrontendClientMetadata clientMeta) {
297 final var iterator = clientMeta.getCurrentHistories().iterator();
298 var metadata = iterator.next();
299 while (iterator.hasNext() && metadata.getHistoryId() != 1) {
300 metadata = iterator.next();
302 assertEquals("[[0..10]]", metadata.getPurgedTransactions().ranges().toString());
305 @SuppressWarnings("checkstyle:IllegalCatch")
306 private void testTransactionCommitFailureWithNoShardLeader(final boolean writeOnly, final String testName)
308 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
309 final String shardName = "default";
311 // We don't want the shard to become the leader so prevent shard
313 datastoreContextBuilder.customRaftPolicyImplementation(
314 "org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy");
316 // The ShardManager uses the election timeout for FindPrimary so
317 // reset it low so it will timeout quickly.
318 datastoreContextBuilder.shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1)
319 .shardInitializationTimeout(200, TimeUnit.MILLISECONDS).frontendRequestTimeoutInSeconds(2);
321 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(testParameter, testName, false, shardName)) {
323 final Object result = dataStore.getActorUtils().executeOperation(
324 dataStore.getActorUtils().getShardManager(), new FindLocalShard(shardName, true));
325 assertTrue("Expected LocalShardFound. Actual: " + result, result instanceof LocalShardFound);
327 // Create the write Tx.
328 DOMStoreWriteTransaction writeTxToClose = null;
330 writeTxToClose = writeOnly ? dataStore.newWriteOnlyTransaction()
331 : dataStore.newReadWriteTransaction();
332 final DOMStoreWriteTransaction writeTx = writeTxToClose;
333 assertNotNull("newReadWriteTransaction returned null", writeTx);
335 // Do some modifications and ready the Tx on a separate
337 final AtomicReference<DOMStoreThreePhaseCommitCohort> txCohort = new AtomicReference<>();
338 final AtomicReference<Exception> caughtEx = new AtomicReference<>();
339 final CountDownLatch txReady = new CountDownLatch(1);
340 final Thread txThread = new Thread(() -> {
342 writeTx.write(TestModel.JUNK_PATH,
343 ImmutableNodes.containerNode(TestModel.JUNK_QNAME));
345 txCohort.set(writeTx.ready());
346 } catch (Exception e) {
355 // Wait for the Tx operations to complete.
356 boolean done = Uninterruptibles.awaitUninterruptibly(txReady, 5, TimeUnit.SECONDS);
357 if (caughtEx.get() != null) {
358 throw caughtEx.get();
361 assertTrue("Tx ready", done);
363 // Wait for the commit to complete. Since no shard
364 // leader was elected in time, the Tx
365 // should have timed out and throw an appropriate
368 txCohort.get().canCommit().get(10, TimeUnit.SECONDS);
369 fail("Expected NoShardLeaderException");
370 } catch (final ExecutionException e) {
371 final String msg = "Unexpected exception: "
372 + Throwables.getStackTraceAsString(e.getCause());
373 if (DistributedDataStore.class.isAssignableFrom(testParameter)) {
374 assertTrue(Throwables.getRootCause(e) instanceof NoShardLeaderException);
376 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
381 if (writeTxToClose != null) {
382 writeTxToClose.close();
384 } catch (Exception e) {
385 // FIXME TransactionProxy.close throws IllegalStateException:
386 // Transaction is ready, it cannot be closed
393 public void testWriteOnlyTransactionCommitFailureWithNoShardLeader() throws Exception {
394 datastoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
395 testTransactionCommitFailureWithNoShardLeader(true, "testWriteOnlyTransactionCommitFailureWithNoShardLeader");
399 public void testReadWriteTransactionCommitFailureWithNoShardLeader() throws Exception {
400 testTransactionCommitFailureWithNoShardLeader(false, "testReadWriteTransactionCommitFailureWithNoShardLeader");
404 public void testTransactionAbort() throws Exception {
405 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
406 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
407 testParameter, "transactionAbortIntegrationTest", "test-1")) {
409 final DOMStoreWriteTransaction writeTx = dataStore.newWriteOnlyTransaction();
410 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
412 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
414 final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
416 cohort.canCommit().get(5, TimeUnit.SECONDS);
418 cohort.abort().get(5, TimeUnit.SECONDS);
420 testKit.testWriteTransaction(dataStore, TestModel.TEST_PATH,
421 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
426 @SuppressWarnings("checkstyle:IllegalCatch")
427 public void testTransactionChainWithSingleShard() throws Exception {
428 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
429 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
430 testParameter, "testTransactionChainWithSingleShard", "test-1")) {
432 // 1. Create a Tx chain and write-only Tx
433 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
435 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
436 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
438 // 2. Write some data
439 final NormalizedNode testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
440 writeTx.write(TestModel.TEST_PATH, testNode);
442 // 3. Ready the Tx for commit
443 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
445 // 4. Commit the Tx on another thread that first waits for
446 // the second read Tx.
447 final CountDownLatch continueCommit1 = new CountDownLatch(1);
448 final CountDownLatch commit1Done = new CountDownLatch(1);
449 final AtomicReference<Exception> commit1Error = new AtomicReference<>();
452 continueCommit1.await();
453 testKit.doCommit(cohort1);
454 } catch (Exception e) {
457 commit1Done.countDown();
461 // 5. Create a new read Tx from the chain to read and verify
462 // the data from the first
463 // Tx is visible after being readied.
464 DOMStoreReadTransaction readTx = txChain.newReadOnlyTransaction();
465 assertEquals(Optional.of(testNode), readTx.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS));
467 // 6. Create a new RW Tx from the chain, write more data,
469 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
470 final MapNode outerNode = ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
471 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
473 rwTx.write(TestModel.OUTER_LIST_PATH, outerNode);
475 final DOMStoreThreePhaseCommitCohort cohort2 = rwTx.ready();
477 // 7. Create a new read Tx from the chain to read the data
478 // from the last RW Tx to
479 // verify it is visible.
480 readTx = txChain.newReadWriteTransaction();
481 assertEquals(Optional.of(outerNode), readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS));
483 // 8. Wait for the 2 commits to complete and close the
485 continueCommit1.countDown();
486 Uninterruptibles.awaitUninterruptibly(commit1Done, 5, TimeUnit.SECONDS);
488 if (commit1Error.get() != null) {
489 throw commit1Error.get();
492 testKit.doCommit(cohort2);
496 // 9. Create a new read Tx from the data store and verify
498 readTx = dataStore.newReadOnlyTransaction();
499 assertEquals(Optional.of(outerNode), readTx.read(TestModel.OUTER_LIST_PATH).get(5, TimeUnit.SECONDS));
504 public void testTransactionChainWithMultipleShards() throws Exception {
505 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
506 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
507 testParameter, "testTransactionChainWithMultipleShards", "cars-1", "people-1")) {
509 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
511 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
512 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
514 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
515 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
517 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
518 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
520 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
522 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
524 final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
525 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
526 readWriteTx.write(carPath, car);
528 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
529 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
530 readWriteTx.merge(personPath, person);
532 assertEquals(Optional.of(car), readWriteTx.read(carPath).get(5, TimeUnit.SECONDS));
533 assertEquals(Optional.of(person), readWriteTx.read(personPath).get(5, TimeUnit.SECONDS));
535 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
537 writeTx = txChain.newWriteOnlyTransaction();
539 writeTx.delete(carPath);
541 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
543 final ListenableFuture<Boolean> canCommit1 = cohort1.canCommit();
544 final ListenableFuture<Boolean> canCommit2 = cohort2.canCommit();
546 testKit.doCommit(canCommit1, cohort1);
547 testKit.doCommit(canCommit2, cohort2);
548 testKit.doCommit(cohort3);
552 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
554 assertEquals(Optional.empty(), readTx.read(carPath).get(5, TimeUnit.SECONDS));
555 assertEquals(Optional.of(person), readTx.read(personPath).get(5, TimeUnit.SECONDS));
560 public void testCreateChainedTransactionsInQuickSuccession() throws Exception {
561 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
562 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
563 testParameter, "testCreateChainedTransactionsInQuickSuccession", "cars-1")) {
565 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
566 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
567 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
568 MoreExecutors.directExecutor());
570 final DOMTransactionChainListener listener = Mockito.mock(DOMTransactionChainListener.class);
571 DOMTransactionChain txChain = broker.createTransactionChain(listener);
573 final List<ListenableFuture<?>> futures = new ArrayList<>();
575 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
576 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, CarsModel.emptyContainer());
577 writeTx.put(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
578 futures.add(writeTx.commit());
581 for (int i = 0; i < numCars; i++) {
582 final DOMDataTreeReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
584 rwTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.newCarPath("car" + i),
585 CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000)));
587 futures.add(rwTx.commit());
590 for (final ListenableFuture<?> f : futures) {
591 f.get(5, TimeUnit.SECONDS);
594 final Optional<NormalizedNode> optional = txChain.newReadOnlyTransaction()
595 .read(LogicalDatastoreType.CONFIGURATION, CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
596 assertTrue("isPresent", optional.isPresent());
597 assertEquals("# cars", numCars, ((Collection<?>) optional.orElseThrow().body()).size());
606 public void testCreateChainedTransactionAfterEmptyTxReadied() throws Exception {
607 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
608 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
609 testParameter, "testCreateChainedTransactionAfterEmptyTxReadied", "test-1")) {
611 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
613 final DOMStoreReadWriteTransaction rwTx1 = txChain.newReadWriteTransaction();
617 final DOMStoreReadWriteTransaction rwTx2 = txChain.newReadWriteTransaction();
619 final Optional<NormalizedNode> optional = rwTx2.read(TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
620 assertFalse("isPresent", optional.isPresent());
627 public void testCreateChainedTransactionWhenPreviousNotReady() throws Exception {
628 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
629 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
630 testParameter, "testCreateChainedTransactionWhenPreviousNotReady", "test-1")) {
632 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
634 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
635 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
637 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
639 // Try to create another Tx of each type - each should fail
640 // b/c the previous Tx wasn't
642 testKit.assertExceptionOnTxChainCreates(txChain, IllegalStateException.class);
647 public void testCreateChainedTransactionAfterClose() throws Exception {
648 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
649 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
650 testParameter, "testCreateChainedTransactionAfterClose", "test-1")) {
652 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
655 // Try to create another Tx of each type - should fail b/c
656 // the previous Tx was closed.
657 testKit.assertExceptionOnTxChainCreates(txChain, DOMTransactionChainClosedException.class);
662 public void testChainWithReadOnlyTxAfterPreviousReady() throws Exception {
663 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
664 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
665 testParameter, "testChainWithReadOnlyTxAfterPreviousReady", "test-1")) {
667 final DOMStoreTransactionChain txChain = dataStore.createTransactionChain();
669 // Create a write tx and submit.
670 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
671 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
672 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
674 // Create read-only tx's and issue a read.
675 FluentFuture<Optional<NormalizedNode>> readFuture1 = txChain
676 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
678 FluentFuture<Optional<NormalizedNode>> readFuture2 = txChain
679 .newReadOnlyTransaction().read(TestModel.TEST_PATH);
681 // Create another write tx and issue the write.
682 DOMStoreWriteTransaction writeTx2 = txChain.newWriteOnlyTransaction();
683 writeTx2.write(TestModel.OUTER_LIST_PATH,
684 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
685 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
688 // Ensure the reads succeed.
690 assertTrue("isPresent", readFuture1.get(5, TimeUnit.SECONDS).isPresent());
691 assertTrue("isPresent", readFuture2.get(5, TimeUnit.SECONDS).isPresent());
693 // Ensure the writes succeed.
694 DOMStoreThreePhaseCommitCohort cohort2 = writeTx2.ready();
696 testKit.doCommit(cohort1);
697 testKit.doCommit(cohort2);
699 assertTrue("isPresent", txChain.newReadOnlyTransaction().read(TestModel.OUTER_LIST_PATH)
700 .get(5, TimeUnit.SECONDS).isPresent());
705 public void testChainedTransactionFailureWithSingleShard() throws Exception {
706 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
707 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
708 testParameter, "testChainedTransactionFailureWithSingleShard", "cars-1")) {
710 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
711 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
712 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
713 MoreExecutors.directExecutor());
715 final DOMTransactionChainListener listener = Mockito.mock(DOMTransactionChainListener.class);
716 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
718 final DOMDataTreeReadWriteTransaction writeTx = txChain.newReadWriteTransaction();
720 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
721 PeopleModel.emptyContainer());
723 final ContainerNode invalidData = Builders.containerBuilder()
724 .withNodeIdentifier(new NodeIdentifier(CarsModel.BASE_QNAME))
725 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk"))
728 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
731 writeTx.commit().get(5, TimeUnit.SECONDS);
732 fail("Expected TransactionCommitFailedException");
733 } catch (final ExecutionException e) {
737 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
738 any(Throwable.class));
746 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
747 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
748 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
749 testParameter, "testChainedTransactionFailureWithMultipleShards", "cars-1", "people-1")) {
751 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
752 ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
753 .put(LogicalDatastoreType.CONFIGURATION, dataStore).build(),
754 MoreExecutors.directExecutor());
756 final DOMTransactionChainListener listener = Mockito.mock(DOMTransactionChainListener.class);
757 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
759 final DOMDataTreeWriteTransaction writeTx = txChain.newReadWriteTransaction();
761 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH,
762 PeopleModel.emptyContainer());
764 final ContainerNode invalidData = Builders.containerBuilder()
765 .withNodeIdentifier(new NodeIdentifier(CarsModel.BASE_QNAME))
766 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk"))
769 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
771 // Note that merge will validate the data and fail but put
772 // succeeds b/c deep validation is not
773 // done for put for performance reasons.
775 writeTx.commit().get(5, TimeUnit.SECONDS);
776 fail("Expected TransactionCommitFailedException");
777 } catch (final ExecutionException e) {
781 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx),
782 any(Throwable.class));
790 public void testDataTreeChangeListenerRegistration() throws Exception {
791 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
792 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
793 testParameter, "testDataTreeChangeListenerRegistration", "test-1")) {
795 testKit.testWriteTransaction(dataStore, TestModel.TEST_PATH,
796 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
798 final MockDataTreeChangeListener listener = new MockDataTreeChangeListener(1);
800 ListenerRegistration<MockDataTreeChangeListener> listenerReg = dataStore
801 .registerTreeChangeListener(TestModel.TEST_PATH, listener);
803 assertNotNull("registerTreeChangeListener returned null", listenerReg);
805 IntegrationTestKit.verifyShardState(dataStore, "test-1",
806 state -> assertEquals("getTreeChangeListenerActors", 1,
807 state.getTreeChangeListenerActors().size()));
809 // Wait for the initial notification
810 listener.waitForChangeEvents(TestModel.TEST_PATH);
814 testKit.testWriteTransaction(dataStore, TestModel.OUTER_LIST_PATH,
815 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
816 .withChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 42))
819 YangInstanceIdentifier listPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
820 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1).build();
821 testKit.testWriteTransaction(dataStore, listPath,
822 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1));
824 // Wait for the 2 updates.
825 listener.waitForChangeEvents(TestModel.OUTER_LIST_PATH, listPath);
828 IntegrationTestKit.verifyShardState(dataStore, "test-1",
829 state -> assertEquals("getTreeChangeListenerActors", 0,
830 state.getTreeChangeListenerActors().size()));
832 testKit.testWriteTransaction(dataStore,
833 YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
834 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2).build(),
835 ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2));
837 listener.expectNoMoreChanges("Received unexpected change after close");
842 public void testRestoreFromDatastoreSnapshot() throws Exception {
843 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(), datastoreContextBuilder);
844 final String name = "transactionIntegrationTest";
846 final ContainerNode carsNode = CarsModel.newCarsNode(
847 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", Uint64.valueOf(20000)),
848 CarsModel.newCarEntry("sportage", Uint64.valueOf(30000))));
850 DataTree dataTree = new InMemoryDataTreeFactory().create(
851 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
852 AbstractShardTest.writeToStore(dataTree, CarsModel.BASE_PATH, carsNode);
853 NormalizedNode root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.of());
855 final Snapshot carsSnapshot = Snapshot.create(
856 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
857 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
859 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
860 SchemaContextHelper.full());
862 final NormalizedNode peopleNode = PeopleModel.create();
863 AbstractShardTest.writeToStore(dataTree, PeopleModel.BASE_PATH, peopleNode);
865 root = AbstractShardTest.readStore(dataTree, YangInstanceIdentifier.of());
867 final Snapshot peopleSnapshot = Snapshot.create(
868 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(root)),
869 Collections.emptyList(), 2, 1, 2, 1, 1, "member-1", null);
871 testKit.restoreFromSnapshot = new DatastoreSnapshot(name, null, Arrays.asList(
872 new DatastoreSnapshot.ShardSnapshot("cars", carsSnapshot),
873 new DatastoreSnapshot.ShardSnapshot("people", peopleSnapshot)));
875 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
876 testParameter, name, "module-shards-member1.conf", true, "cars", "people")) {
878 final DOMStoreReadTransaction readTx = dataStore.newReadOnlyTransaction();
881 assertEquals(Optional.of(carsNode), readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS));
882 assertEquals(Optional.of(peopleNode), readTx.read(PeopleModel.BASE_PATH).get(5, TimeUnit.SECONDS));
887 public void testSnapshotOnRootOverwrite() throws Exception {
888 if (!DistributedDataStore.class.isAssignableFrom(testParameter)) {
889 // FIXME: ClientBackedDatastore does not have stable indexes/term, the snapshot index seems to fluctuate
893 final IntegrationTestKit testKit = new IntegrationTestKit(getSystem(),
894 datastoreContextBuilder.snapshotOnRootOverwrite(true));
895 try (AbstractDataStore dataStore = testKit.setupAbstractDataStore(
896 testParameter, "testRootOverwrite", "module-shards-default-cars-member1.conf",
897 true, "cars", "default")) {
899 ContainerNode rootNode = Builders.containerBuilder()
900 .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
901 .withChild(CarsModel.create())
904 testKit.testWriteTransaction(dataStore, YangInstanceIdentifier.of(), rootNode);
905 IntegrationTestKit.verifyShardState(dataStore, "cars",
906 state -> assertEquals(1, state.getSnapshotIndex()));
908 // root has been written expect snapshot at index 0
909 verifySnapshot("member-1-shard-cars-testRootOverwrite", 1, 1);
911 for (int i = 0; i < 10; i++) {
912 testKit.testWriteTransaction(dataStore, CarsModel.newCarPath("car " + i),
913 CarsModel.newCarEntry("car " + i, Uint64.ONE));
916 // fake snapshot causes the snapshotIndex to move
917 IntegrationTestKit.verifyShardState(dataStore, "cars",
918 state -> assertEquals(10, state.getSnapshotIndex()));
920 // however the real snapshot still has not changed and was taken at index 0
921 verifySnapshot("member-1-shard-cars-testRootOverwrite", 1, 1);
923 // root overwrite so expect a snapshot
924 testKit.testWriteTransaction(dataStore, YangInstanceIdentifier.of(), rootNode);
926 // this was a real snapshot so everything should be in it(1 + 10 + 1)
927 IntegrationTestKit.verifyShardState(dataStore, "cars",
928 state -> assertEquals(12, state.getSnapshotIndex()));
930 verifySnapshot("member-1-shard-cars-testRootOverwrite", 12, 1);
934 private static void verifySnapshot(final String persistenceId, final long lastAppliedIndex,
935 final long lastAppliedTerm) {
936 await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
937 List<Snapshot> snap = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
938 assertEquals(1, snap.size());
939 assertEquals(lastAppliedIndex, snap.get(0).getLastAppliedIndex());
940 assertEquals(lastAppliedTerm, snap.get(0).getLastAppliedTerm());