2 * Copyright (c) 2015, 2017 Brocade Communications 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.hamcrest.Matchers.equalTo;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertThrows;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assume.assumeTrue;
20 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.timeout;
26 import static org.mockito.Mockito.verify;
28 import akka.actor.ActorRef;
29 import akka.actor.ActorSelection;
30 import akka.actor.ActorSystem;
31 import akka.actor.Address;
32 import akka.actor.AddressFromURIString;
33 import akka.cluster.Cluster;
34 import akka.cluster.Member;
35 import akka.dispatch.Futures;
36 import akka.pattern.Patterns;
37 import akka.testkit.javadsl.TestKit;
38 import com.google.common.base.Stopwatch;
39 import com.google.common.base.Throwables;
40 import com.google.common.collect.ImmutableMap;
41 import com.google.common.util.concurrent.FutureCallback;
42 import com.google.common.util.concurrent.ListenableFuture;
43 import com.google.common.util.concurrent.MoreExecutors;
44 import com.google.common.util.concurrent.Uninterruptibles;
45 import com.typesafe.config.ConfigFactory;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.LinkedList;
50 import java.util.List;
52 import java.util.Optional;
53 import java.util.concurrent.ExecutionException;
54 import java.util.concurrent.ExecutorService;
55 import java.util.concurrent.Executors;
56 import java.util.concurrent.TimeUnit;
57 import java.util.concurrent.atomic.AtomicBoolean;
58 import java.util.concurrent.atomic.AtomicLong;
59 import org.junit.After;
60 import org.junit.Before;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63 import org.junit.runners.Parameterized;
64 import org.junit.runners.Parameterized.Parameter;
65 import org.junit.runners.Parameterized.Parameters;
66 import org.mockito.stubbing.Answer;
67 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
68 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
69 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
70 import org.opendaylight.controller.cluster.databroker.TestClientBackedDataStore;
71 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
72 import org.opendaylight.controller.cluster.datastore.TestShard.RequestFrontendMetadata;
73 import org.opendaylight.controller.cluster.datastore.TestShard.StartDropMessages;
74 import org.opendaylight.controller.cluster.datastore.TestShard.StopDropMessages;
75 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
76 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
77 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
78 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
79 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
80 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
81 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
82 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
83 import org.opendaylight.controller.cluster.datastore.persisted.FrontendShardDataTreeSnapshotMetadata;
84 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
85 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
86 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongBitmap;
87 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
88 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
89 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
90 import org.opendaylight.controller.cluster.raft.client.messages.Shutdown;
91 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
92 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
93 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
94 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
95 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
96 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
97 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
98 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
99 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
100 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
101 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
102 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
103 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
104 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
105 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
106 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
107 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
108 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
109 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
110 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
111 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
112 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
113 import org.opendaylight.yangtools.yang.common.Uint64;
114 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
115 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
116 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
117 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
118 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
119 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
120 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
121 import org.opendaylight.yangtools.yang.data.tree.api.ConflictingModificationAppliedException;
122 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
123 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
124 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
125 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
126 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
127 import scala.collection.Set;
128 import scala.concurrent.Await;
129 import scala.concurrent.Future;
130 import scala.concurrent.duration.FiniteDuration;
133 * End-to-end distributed data store tests that exercise remote shards and transactions.
135 * @author Thomas Pantelis
137 @RunWith(Parameterized.class)
138 public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
140 @Parameters(name = "{0}")
141 public static Collection<Object[]> data() {
142 return Arrays.asList(new Object[][] {
143 { TestClientBackedDataStore.class, 12 }
148 public Class<? extends AbstractDataStore> testParameter;
150 public int commitTimeout;
152 private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
153 private static final String[] CARS = {"cars"};
155 private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse(
156 "akka://cluster-test@127.0.0.1:2558");
157 private static final Address MEMBER_2_ADDRESS = AddressFromURIString.parse(
158 "akka://cluster-test@127.0.0.1:2559");
160 private static final String MODULE_SHARDS_CARS_ONLY_1_2 = "module-shards-cars-member-1-and-2.conf";
161 private static final String MODULE_SHARDS_CARS_PEOPLE_1_2 = "module-shards-member1-and-2.conf";
162 private static final String MODULE_SHARDS_CARS_PEOPLE_1_2_3 = "module-shards-member1-and-2-and-3.conf";
163 private static final String MODULE_SHARDS_CARS_1_2_3 = "module-shards-cars-member-1-and-2-and-3.conf";
165 private ActorSystem leaderSystem;
166 private ActorSystem followerSystem;
167 private ActorSystem follower2System;
169 private final DatastoreContext.Builder leaderDatastoreContextBuilder =
170 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
172 private final DatastoreContext.Builder followerDatastoreContextBuilder =
173 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5)
174 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
175 private final TransactionIdentifier tx1 = nextTransactionId();
176 private final TransactionIdentifier tx2 = nextTransactionId();
178 private AbstractDataStore followerDistributedDataStore;
179 private AbstractDataStore leaderDistributedDataStore;
180 private IntegrationTestKit followerTestKit;
181 private IntegrationTestKit leaderTestKit;
184 public void setUp() {
185 InMemoryJournal.clear();
186 InMemorySnapshotStore.clear();
188 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
189 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
191 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
192 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
194 follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
195 Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
199 public void tearDown() {
200 if (followerDistributedDataStore != null) {
201 leaderDistributedDataStore.close();
203 if (leaderDistributedDataStore != null) {
204 leaderDistributedDataStore.close();
207 TestKit.shutdownActorSystem(leaderSystem, true);
208 TestKit.shutdownActorSystem(followerSystem, true);
209 TestKit.shutdownActorSystem(follower2System,true);
211 InMemoryJournal.clear();
212 InMemorySnapshotStore.clear();
215 private void initDatastoresWithCars(final String type) throws Exception {
216 initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
219 private void initDatastoresWithCarsAndPeople(final String type) throws Exception {
220 initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
223 private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards)
225 initDatastores(type, moduleShardsConfig, shards, leaderDatastoreContextBuilder,
226 followerDatastoreContextBuilder);
229 private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards,
230 final DatastoreContext.Builder leaderBuilder, final DatastoreContext.Builder followerBuilder)
232 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderBuilder, commitTimeout);
234 leaderDistributedDataStore = leaderTestKit.setupAbstractDataStore(
235 testParameter, type, moduleShardsConfig, false, shards);
237 followerTestKit = new IntegrationTestKit(followerSystem, followerBuilder, commitTimeout);
238 followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
239 testParameter, type, moduleShardsConfig, false, shards);
241 leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(), shards);
243 leaderTestKit.waitForMembersUp("member-2");
244 followerTestKit.waitForMembersUp("member-1");
247 private static void verifyCars(final DOMStoreReadTransaction readTx, final MapEntryNode... entries)
249 assertEquals("Car list node",
250 Optional.of(ImmutableNodes.mapNodeBuilder(CarsModel.CAR_QNAME).withValue(Arrays.asList(entries)).build()),
251 readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS));
254 private static void verifyNode(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path,
255 final NormalizedNode expNode) throws Exception {
256 assertEquals(Optional.of(expNode), readTx.read(path).get(5, TimeUnit.SECONDS));
259 private static void verifyExists(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path)
261 assertEquals("exists", Boolean.TRUE, readTx.exists(path).get(5, TimeUnit.SECONDS));
265 public void testWriteTransactionWithSingleShard() throws Exception {
266 final String testName = "testWriteTransactionWithSingleShard";
267 initDatastoresWithCars(testName);
269 final String followerCarShardName = "member-2-shard-cars-" + testName;
271 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
272 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
274 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
275 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
277 final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
278 final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
279 writeTx.merge(car1Path, car1);
281 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
282 final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
283 writeTx.merge(car2Path, car2);
285 followerTestKit.doCommit(writeTx.ready());
287 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
289 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
293 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
295 writeTx.delete(car1Path);
297 followerTestKit.doCommit(writeTx.ready());
299 verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
301 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
303 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
305 // Re-instate the follower member 2 as a single-node to verify replication and recovery.
307 // The following is a bit tricky. Before we reinstate the follower we need to ensure it has persisted and
308 // applied and all the log entries from the leader. Since we've verified the car data above we know that
309 // all the transactions have been applied on the leader so we first read and capture its lastAppliedIndex.
310 final AtomicLong leaderLastAppliedIndex = new AtomicLong();
311 IntegrationTestKit.verifyShardState(leaderDistributedDataStore, CARS[0],
312 state -> leaderLastAppliedIndex.set(state.getLastApplied()));
314 // Now we need to make sure the follower has persisted the leader's lastAppliedIndex via ApplyJournalEntries.
315 // However we don't know exactly how many ApplyJournalEntries messages there will be as it can differ between
316 // the tell-based and ask-based front-ends. For ask-based there will be exactly 2 ApplyJournalEntries but
317 // tell-based persists additional payloads which could be replicated and applied in a batch resulting in
318 // either 2 or 3 ApplyJournalEntries. To handle this we read the follower's persisted ApplyJournalEntries
319 // until we find the one that encompasses the leader's lastAppliedIndex.
320 Stopwatch sw = Stopwatch.createStarted();
321 boolean done = false;
323 final List<ApplyJournalEntries> entries = InMemoryJournal.get(followerCarShardName,
324 ApplyJournalEntries.class);
325 for (ApplyJournalEntries aje: entries) {
326 if (aje.getToIndex() >= leaderLastAppliedIndex.get()) {
332 assertTrue("Follower did not persist ApplyJournalEntries containing leader's lastAppliedIndex "
333 + leaderLastAppliedIndex + ". Entries persisted: " + entries, sw.elapsed(TimeUnit.SECONDS) <= 5);
335 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
338 TestKit.shutdownActorSystem(leaderSystem, true);
339 TestKit.shutdownActorSystem(followerSystem, true);
341 final ActorSystem newSystem = newActorSystem("reinstated-member2", "Member2");
343 try (AbstractDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder,
345 .setupAbstractDataStore(testParameter, testName, "module-shards-member2", true, CARS)) {
346 verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
351 public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
352 initDatastoresWithCars("testSingleTransactionsWritesInQuickSuccession");
354 final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
356 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
357 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
358 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
359 followerTestKit.doCommit(writeTx.ready());
362 for (int i = 0; i < numCars; i++) {
363 writeTx = txChain.newWriteOnlyTransaction();
364 writeTx.write(CarsModel.newCarPath("car" + i), CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000)));
365 followerTestKit.doCommit(writeTx.ready());
367 try (var tx = txChain.newReadOnlyTransaction()) {
368 tx.read(CarsModel.BASE_PATH).get();
372 // wait to let the shard catch up with purged
373 await("Range set leak test").atMost(5, TimeUnit.SECONDS)
374 .pollInterval(500, TimeUnit.MILLISECONDS)
375 .untilAsserted(() -> {
376 final var localShard = leaderDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
377 final var frontendMetadata =
378 (FrontendShardDataTreeSnapshotMetadata) leaderDistributedDataStore.getActorUtils()
379 .executeOperation(localShard, new RequestFrontendMetadata());
381 assertClientMetadata(frontendMetadata.getClients().get(0), numCars * 2);
384 try (var tx = txChain.newReadOnlyTransaction()) {
385 final var body = assertInstanceOf(Collection.class,
386 tx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS).orElseThrow().body());
387 assertEquals(numCars, ((Collection<?>) body).size());
391 private static void assertClientMetadata(final FrontendClientMetadata clientMeta, final long lastPurged) {
392 final var iterator = clientMeta.getCurrentHistories().iterator();
393 var metadata = iterator.next();
394 while (iterator.hasNext() && metadata.getHistoryId() != 1) {
395 metadata = iterator.next();
398 assertEquals(UnsignedLongBitmap.of(), metadata.getClosedTransactions());
399 assertEquals("[[0.." + lastPurged + "]]", metadata.getPurgedTransactions().ranges().toString());
403 public void testCloseTransactionMetadataLeak() throws Exception {
404 initDatastoresWithCars("testCloseTransactionMetadataLeak");
406 final var txChain = followerDistributedDataStore.createTransactionChain();
408 var writeTx = txChain.newWriteOnlyTransaction();
409 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
410 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
411 followerTestKit.doCommit(writeTx.ready());
414 for (int i = 0; i < numCars; i++) {
415 try (var tx = txChain.newWriteOnlyTransaction()) {
419 try (var tx = txChain.newReadOnlyTransaction()) {
420 tx.read(CarsModel.BASE_PATH).get();
424 // wait to let the shard catch up with purged
425 await("wait for purges to settle").atMost(5, TimeUnit.SECONDS)
426 .pollInterval(500, TimeUnit.MILLISECONDS)
427 .untilAsserted(() -> {
428 final var localShard = leaderDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
429 final var frontendMetadata =
430 (FrontendShardDataTreeSnapshotMetadata) leaderDistributedDataStore.getActorUtils()
431 .executeOperation(localShard, new RequestFrontendMetadata());
433 assertClientMetadata(frontendMetadata.getClients().get(0), numCars * 2);
438 public void testReadWriteTransactionWithSingleShard() throws Exception {
439 initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
441 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
442 assertNotNull("newReadWriteTransaction returned null", rwTx);
444 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
445 rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
447 final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
448 rwTx.merge(CarsModel.newCarPath("optima"), car1);
450 verifyCars(rwTx, car1);
452 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
453 final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
454 rwTx.merge(car2Path, car2);
456 verifyExists(rwTx, car2Path);
458 followerTestKit.doCommit(rwTx.ready());
460 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
464 public void testWriteTransactionWithMultipleShards() throws Exception {
465 initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
467 final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
468 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
470 final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
471 final NormalizedNode carsNode = CarsModel.emptyContainer();
472 writeTx.write(carsPath, carsNode);
474 final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
475 final NormalizedNode peopleNode = PeopleModel.emptyContainer();
476 writeTx.write(peoplePath, peopleNode);
478 followerTestKit.doCommit(writeTx.ready());
480 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
482 verifyNode(readTx, carsPath, carsNode);
483 verifyNode(readTx, peoplePath, peopleNode);
487 public void testReadWriteTransactionWithMultipleShards() throws Exception {
488 initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
490 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
491 assertNotNull("newReadWriteTransaction returned null", rwTx);
493 final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
494 final NormalizedNode carsNode = CarsModel.emptyContainer();
495 rwTx.write(carsPath, carsNode);
497 final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
498 final NormalizedNode peopleNode = PeopleModel.emptyContainer();
499 rwTx.write(peoplePath, peopleNode);
501 followerTestKit.doCommit(rwTx.ready());
503 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
505 verifyNode(readTx, carsPath, carsNode);
506 verifyNode(readTx, peoplePath, peopleNode);
510 public void testTransactionChainWithSingleShard() throws Exception {
511 initDatastoresWithCars("testTransactionChainWithSingleShard");
513 final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
515 // Add the top-level cars container with write-only.
517 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
518 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
520 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
522 final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
524 // Verify the top-level cars container with read-only.
526 verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
528 // Perform car operations with read-write.
530 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
532 verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
534 rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
536 final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
537 final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
538 rwTx.write(car1Path, car1);
540 verifyExists(rwTx, car1Path);
542 verifyCars(rwTx, car1);
544 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
545 rwTx.merge(CarsModel.newCarPath("sportage"), car2);
547 rwTx.delete(car1Path);
549 followerTestKit.doCommit(writeTxReady);
551 followerTestKit.doCommit(rwTx.ready());
555 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
559 public void testTransactionChainWithMultipleShards() throws Exception {
560 initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
562 final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
564 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
565 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
567 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
568 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
570 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
571 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
573 followerTestKit.doCommit(writeTx.ready());
575 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
577 final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
578 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
579 readWriteTx.write(carPath, car);
581 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
582 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
583 readWriteTx.merge(personPath, person);
585 assertEquals(Optional.of(car), readWriteTx.read(carPath).get(5, TimeUnit.SECONDS));
586 assertEquals(Optional.of(person), readWriteTx.read(personPath).get(5, TimeUnit.SECONDS));
588 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
590 writeTx = txChain.newWriteOnlyTransaction();
592 writeTx.delete(personPath);
594 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
596 followerTestKit.doCommit(cohort2);
597 followerTestKit.doCommit(cohort3);
601 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
602 verifyCars(readTx, car);
604 assertEquals(Optional.empty(), readTx.read(personPath).get(5, TimeUnit.SECONDS));
608 public void testChainedTransactionFailureWithSingleShard() throws Exception {
609 initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
611 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
612 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
613 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
614 MoreExecutors.directExecutor());
616 final var listener = mock(FutureCallback.class);
617 final DOMTransactionChain txChain = broker.createTransactionChain();
618 txChain.addCallback(listener);
620 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
622 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, Builders.containerBuilder()
623 .withNodeIdentifier(new NodeIdentifier(CarsModel.BASE_QNAME))
624 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk"))
627 final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS));
628 assertInstanceOf(TransactionCommitFailedException.class, ex.getCause());
630 verify(listener, timeout(5000)).onFailure(any());
637 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
638 initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
640 try (var broker = new ConcurrentDOMDataBroker(
641 Map.of(LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore), MoreExecutors.directExecutor())) {
643 final var listener = mock(FutureCallback.class);
644 final DOMTransactionChain txChain = broker.createTransactionChain();
645 txChain.addCallback(listener);
647 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
649 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
651 // Note that merge will validate the data and fail but put succeeds b/c deep validation is not
652 // done for put for performance reasons.
653 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, Builders.containerBuilder()
654 .withNodeIdentifier(new NodeIdentifier(CarsModel.BASE_QNAME))
655 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk"))
658 final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS))
660 assertThat(ex, instanceOf(TransactionCommitFailedException.class));
662 verify(listener, timeout(5000)).onFailure(any());
669 public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
670 followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
671 final String testName = "testSingleShardTransactionsWithLeaderChanges";
672 initDatastoresWithCars(testName);
674 final String followerCarShardName = "member-2-shard-cars-" + testName;
675 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
677 // Write top-level car container from the follower so it uses a remote Tx.
679 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
681 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
682 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
684 followerTestKit.doCommit(writeTx.ready());
686 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
688 // Switch the leader to the follower
690 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
691 .shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
693 TestKit.shutdownActorSystem(leaderSystem, true);
694 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
696 followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorUtils(), CARS);
698 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
699 Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
701 final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder()
702 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
703 IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
705 try (AbstractDataStore ds =
706 newMember1TestKit.setupAbstractDataStore(
707 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
709 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), CARS);
711 // Write a car entry to the new leader - should switch to local Tx
713 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
715 MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
716 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
717 writeTx.merge(car1Path, car1);
719 followerTestKit.doCommit(writeTx.ready());
721 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
726 public void testReadyLocalTransactionForwardedToLeader() throws Exception {
727 initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
728 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
730 final Optional<ActorRef> carsFollowerShard =
731 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
732 assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
734 final DataTree dataTree = new InMemoryDataTreeFactory().create(
735 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
737 // Send a tx with immediate commit.
739 DataTreeModification modification = dataTree.takeSnapshot().newModification();
740 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
741 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
743 final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
744 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
745 modification.ready();
747 ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1 , modification, true, Optional.empty());
749 carsFollowerShard.orElseThrow().tell(readyLocal, followerTestKit.getRef());
750 Object resp = followerTestKit.expectMsgClass(Object.class);
751 if (resp instanceof akka.actor.Status.Failure) {
752 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
755 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
757 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
759 // Send another tx without immediate commit.
761 modification = dataTree.takeSnapshot().newModification();
762 MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(30000));
763 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
764 modification.ready();
766 readyLocal = new ReadyLocalTransaction(tx2 , modification, false, Optional.empty());
768 carsFollowerShard.orElseThrow().tell(readyLocal, followerTestKit.getRef());
769 resp = followerTestKit.expectMsgClass(Object.class);
770 if (resp instanceof akka.actor.Status.Failure) {
771 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
774 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
776 final ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
777 ((ReadyTransactionReply)resp).getCohortPath());
779 ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(leaderDistributedDataStore.getActorUtils(),
780 List.of(new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor),
781 () -> DataStoreVersions.CURRENT_VERSION)), tx2);
782 cohort.canCommit().get(5, TimeUnit.SECONDS);
783 cohort.preCommit().get(5, TimeUnit.SECONDS);
784 cohort.commit().get(5, TimeUnit.SECONDS);
786 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
790 public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
791 initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
792 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
794 final Optional<ActorRef> carsFollowerShard =
795 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
796 assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
798 carsFollowerShard.orElseThrow().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
799 final DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
801 // Send a tx with immediate commit.
803 DataTreeModification modification = dataTree.takeSnapshot().newModification();
804 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
805 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
807 final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
808 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
810 ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction(tx1, DataStoreVersions.CURRENT_VERSION,
811 new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx1, modification),
812 true, Optional.empty());
814 carsFollowerShard.orElseThrow().tell(forwardedReady, followerTestKit.getRef());
815 Object resp = followerTestKit.expectMsgClass(Object.class);
816 if (resp instanceof akka.actor.Status.Failure) {
817 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
820 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
822 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
824 // Send another tx without immediate commit.
826 modification = dataTree.takeSnapshot().newModification();
827 MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(30000));
828 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
830 forwardedReady = new ForwardedReadyTransaction(tx2, DataStoreVersions.CURRENT_VERSION,
831 new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx2, modification),
832 false, Optional.empty());
834 carsFollowerShard.orElseThrow().tell(forwardedReady, followerTestKit.getRef());
835 resp = followerTestKit.expectMsgClass(Object.class);
836 if (resp instanceof akka.actor.Status.Failure) {
837 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
840 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
842 ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
843 ((ReadyTransactionReply)resp).getCohortPath());
845 final ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
846 leaderDistributedDataStore.getActorUtils(), List.of(
847 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor),
848 () -> DataStoreVersions.CURRENT_VERSION)), tx2);
849 cohort.canCommit().get(5, TimeUnit.SECONDS);
850 cohort.preCommit().get(5, TimeUnit.SECONDS);
851 cohort.commit().get(5, TimeUnit.SECONDS);
853 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
857 public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
858 followerDatastoreContextBuilder.shardBatchedModificationCount(2);
859 leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
860 initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
862 // Verify backend statistics on start
863 verifyCarsReadWriteTransactions(leaderDistributedDataStore, 0);
864 verifyCarsReadWriteTransactions(followerDistributedDataStore, 0);
866 // Do an initial write to get the primary shard info cached.
868 final DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
869 initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
870 initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
871 followerTestKit.doCommit(initialWriteTx.ready());
873 // Wait for the commit to be replicated to the follower.
875 MemberNode.verifyRaftState(followerDistributedDataStore, "cars",
876 raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
878 MemberNode.verifyRaftState(followerDistributedDataStore, "people",
879 raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
881 // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
884 final DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
885 writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
886 writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
887 final DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
888 final ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
889 writeTx1CanCommit.get(5, TimeUnit.SECONDS);
891 // Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
892 // in the leader shard.
894 final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
895 final LinkedList<MapEntryNode> cars = new LinkedList<>();
897 cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
898 writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
900 NormalizedNode people = ImmutableNodes.mapNodeBuilder(PeopleModel.PERSON_QNAME)
901 .withChild(PeopleModel.newPersonEntry("Dude")).build();
902 writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
903 final DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
905 // At this point only leader should see the transactions
906 verifyCarsReadWriteTransactions(leaderDistributedDataStore, 2);
907 verifyCarsReadWriteTransactions(followerDistributedDataStore, 0);
909 // Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
910 // tx writes 5 cars so 2 BatchedModifications messages will be sent initially and cached in the leader shard
911 // (with shardBatchedModificationCount set to 2). The 3rd BatchedModifications will be sent on ready.
913 final DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
914 for (int i = 1; i <= 5; i++, carIndex++) {
915 cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
916 writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
919 // Prepare another WO that writes to a single shard. This will send a single BatchedModifications message
922 final DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
923 cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
924 writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
927 // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaction message to the leader shard
930 final DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
931 cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
932 final YangInstanceIdentifier carPath = CarsModel.newCarPath("car" + carIndex);
933 readWriteTx.write(carPath, cars.getLast());
935 // There is a difference here between implementations: tell-based protocol enforces batching on per-transaction
936 // level whereas ask-based protocol has a global limit towards a shard -- and hence flushes out last two
937 // transactions eagerly.
938 verifyCarsReadWriteTransactions(leaderDistributedDataStore, 3);
939 verifyCarsReadWriteTransactions(followerDistributedDataStore, 0);
941 // Disable elections on the leader so it switches to follower.
943 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
944 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
945 .shardElectionTimeoutFactor(10));
947 leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorUtils(), "cars");
949 // Submit all tx's - the messages should get queued for retry.
951 final ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
952 final DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
953 final DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
954 final DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
956 // Enable elections on the other follower so it becomes the leader, at which point the
957 // tx's should get forwarded from the previous leader to the new leader to complete the commits.
959 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
960 .customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
961 IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorUtils(), "cars")
962 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
963 IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorUtils(), "people")
964 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
966 followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
967 followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
968 followerTestKit.doCommit(writeTx3Cohort);
969 followerTestKit.doCommit(writeTx4Cohort);
970 followerTestKit.doCommit(rwTxCohort);
972 // At this point everything is committed and the follower datastore should see 5 transactions, but leader should
973 // only see the initial transactions
974 verifyCarsReadWriteTransactions(leaderDistributedDataStore, 3);
975 verifyCarsReadWriteTransactions(followerDistributedDataStore, 5);
977 DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
978 verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
979 verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
982 private static void verifyCarsReadWriteTransactions(final AbstractDataStore datastore, final int expected)
984 IntegrationTestKit.verifyShardStats(datastore, "cars",
985 stats -> assertEquals("getReadWriteTransactionCount", expected, stats.getReadWriteTransactionCount()));
989 public void testLeadershipTransferOnShutdown() throws Exception {
990 leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
991 followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
992 final String testName = "testLeadershipTransferOnShutdown";
993 initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
995 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
996 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500),
998 try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
999 testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
1001 followerTestKit.waitForMembersUp("member-3");
1002 follower2TestKit.waitForMembersUp("member-1", "member-2");
1004 // Create and submit a couple tx's so they're pending.
1006 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
1007 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1008 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
1009 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
1010 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1012 // FIXME: this assertion should be made in an explicit Shard test
1013 // IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1014 // stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
1016 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
1017 final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
1018 writeTx.write(CarsModel.newCarPath("optima"), car);
1019 final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
1021 // FIXME: this assertion should be made in an explicit Shard test
1022 // IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1023 // stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
1025 // Gracefully stop the leader via a Shutdown message.
1027 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1028 .shardElectionTimeoutFactor(100));
1030 final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
1031 final Future<ActorRef> future = leaderDistributedDataStore.getActorUtils().findLocalShardAsync("cars");
1032 final ActorRef leaderActor = Await.result(future, duration);
1034 final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
1036 // Commit the 2 transactions. They should finish and succeed.
1038 followerTestKit.doCommit(cohort1);
1039 followerTestKit.doCommit(cohort2);
1041 // Wait for the leader actor stopped.
1043 final Boolean stopped = Await.result(stopFuture, duration);
1044 assertEquals("Stopped", Boolean.TRUE, stopped);
1046 // Verify leadership was transferred by reading the committed data from the other nodes.
1048 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
1049 verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
1054 public void testTransactionWithIsolatedLeader() throws Exception {
1055 // Set the isolated leader check interval high so we can control the switch to IsolatedLeader.
1056 leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(10000000);
1057 final String testName = "testTransactionWithIsolatedLeader";
1058 initDatastoresWithCars(testName);
1060 // Tx that is submitted after the follower is stopped but before the leader transitions to IsolatedLeader.
1061 final DOMStoreWriteTransaction preIsolatedLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1062 preIsolatedLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1064 // Tx that is submitted after the leader transitions to IsolatedLeader.
1065 final DOMStoreWriteTransaction noShardLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1066 noShardLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1068 // Tx that is submitted after the follower is reinstated.
1069 final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1070 successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1072 // Stop the follower
1073 followerTestKit.watch(followerDistributedDataStore.getActorUtils().getShardManager());
1074 followerDistributedDataStore.close();
1075 followerTestKit.expectTerminated(followerDistributedDataStore.getActorUtils().getShardManager());
1077 // Submit the preIsolatedLeaderWriteTx so it's pending
1078 final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
1080 // Change the isolated leader check interval low so it changes to IsolatedLeader.
1081 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1082 .shardIsolatedLeaderCheckIntervalInMillis(200));
1084 MemberNode.verifyRaftState(leaderDistributedDataStore, "cars",
1085 raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
1087 final var noShardLeaderCohort = noShardLeaderWriteTx.ready();
1088 // tell-based canCommit() does not have a real timeout and hence continues
1089 final var canCommit = noShardLeaderCohort.canCommit();
1090 Uninterruptibles.sleepUninterruptibly(commitTimeout, TimeUnit.SECONDS);
1091 assertFalse(canCommit.isDone());
1093 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1094 .shardElectionTimeoutFactor(100));
1096 final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
1098 followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
1099 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
1101 leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
1102 leaderTestKit.doCommit(successTxCohort);
1104 // continuation of canCommit(): readied transaction will complete commit, but will report an OLFE
1105 final var ex = assertThrows(ExecutionException.class,
1106 () -> canCommit.get(commitTimeout, TimeUnit.SECONDS)).getCause();
1107 assertThat(ex, instanceOf(OptimisticLockFailedException.class));
1108 assertEquals("Optimistic lock failed for path " + CarsModel.BASE_PATH, ex.getMessage());
1109 final var cause = ex.getCause();
1110 assertThat(cause, instanceOf(ConflictingModificationAppliedException.class));
1111 final var cmae = (ConflictingModificationAppliedException) cause;
1112 assertEquals("Node was created by other transaction.", cmae.getMessage());
1113 assertEquals(CarsModel.BASE_PATH, cmae.getPath());
1117 public void testTransactionWithShardLeaderNotResponding() throws Exception {
1118 followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1119 followerDatastoreContextBuilder.shardElectionTimeoutFactor(50);
1120 initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
1122 // Do an initial read to get the primary shard info cached.
1124 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1125 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1127 // Shutdown the leader and try to create a new tx.
1129 TestKit.shutdownActorSystem(leaderSystem, true);
1131 followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
1132 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
1134 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1136 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1138 final var ex = assertThrows(ExecutionException.class, () -> followerTestKit.doCommit(rwTx.ready()));
1139 assertThat("Unexpected exception: " + Throwables.getStackTraceAsString(ex.getCause()),
1140 Throwables.getRootCause(ex), instanceOf(RequestTimeoutException.class));
1144 public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
1145 followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1146 initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
1148 // Do an initial read to get the primary shard info cached.
1150 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1151 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1153 // Shutdown the leader and try to create a new tx.
1155 TestKit.shutdownActorSystem(leaderSystem, true);
1157 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1159 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1161 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1162 .operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
1164 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1166 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1168 final var ex = assertThrows(ExecutionException.class, () -> followerTestKit.doCommit(rwTx.ready()));
1169 assertThat("Unexpected exception: " + Throwables.getStackTraceAsString(ex.getCause()),
1170 Throwables.getRootCause(ex), instanceOf(RequestTimeoutException.class));
1174 public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
1175 followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
1176 String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
1177 initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1179 final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1180 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1181 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1182 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1184 try (AbstractDataStore ds =
1185 follower2TestKit.setupAbstractDataStore(
1186 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
1188 followerTestKit.waitForMembersUp("member-1", "member-3");
1189 follower2TestKit.waitForMembersUp("member-1", "member-2");
1191 // Do an initial read to get the primary shard info cached.
1193 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1194 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1196 // Shutdown the leader and try to create a new tx.
1198 TestKit.shutdownActorSystem(leaderSystem, true);
1200 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1202 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1203 .operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
1205 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1207 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1209 followerTestKit.doCommit(rwTx.ready());
1214 public void testSemiReachableCandidateNotDroppingLeader() throws Exception {
1215 final String testName = "testSemiReachableCandidateNotDroppingLeader";
1216 initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1218 final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1219 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1220 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1221 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1223 final AbstractDataStore ds2 =
1224 follower2TestKit.setupAbstractDataStore(
1225 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS);
1227 followerTestKit.waitForMembersUp("member-1", "member-3");
1228 follower2TestKit.waitForMembersUp("member-1", "member-2");
1230 // behavior is controlled by akka.coordinated-shutdown.run-by-actor-system-terminate configuration option
1231 TestKit.shutdownActorSystem(follower2System, true);
1233 ActorRef cars = leaderDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
1234 final OnDemandRaftState initialState = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1235 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1237 Cluster leaderCluster = Cluster.get(leaderSystem);
1238 Cluster followerCluster = Cluster.get(followerSystem);
1239 Cluster follower2Cluster = Cluster.get(follower2System);
1241 Member follower2Member = follower2Cluster.readView().self();
1243 await().atMost(10, TimeUnit.SECONDS)
1244 .until(() -> containsUnreachable(leaderCluster, follower2Member));
1245 await().atMost(10, TimeUnit.SECONDS)
1246 .until(() -> containsUnreachable(followerCluster, follower2Member));
1248 ActorRef followerCars = followerDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
1250 // to simulate a follower not being able to receive messages, but still being able to send messages and becoming
1251 // candidate, we can just send a couple of RequestVotes to both leader and follower.
1252 cars.tell(new RequestVote(initialState.getCurrentTerm() + 1, "member-3-shard-cars", -1, -1), null);
1253 followerCars.tell(new RequestVote(initialState.getCurrentTerm() + 1, "member-3-shard-cars", -1, -1), null);
1254 cars.tell(new RequestVote(initialState.getCurrentTerm() + 3, "member-3-shard-cars", -1, -1), null);
1255 followerCars.tell(new RequestVote(initialState.getCurrentTerm() + 3, "member-3-shard-cars", -1, -1), null);
1257 OnDemandRaftState stateAfter = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1258 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1259 OnDemandRaftState followerState = (OnDemandRaftState) followerDistributedDataStore.getActorUtils()
1260 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1262 assertEquals(initialState.getCurrentTerm(), stateAfter.getCurrentTerm());
1263 assertEquals(initialState.getCurrentTerm(), followerState.getCurrentTerm());
1268 private static Boolean containsUnreachable(final Cluster cluster, final Member member) {
1269 // unreachableMembers() returns scala.collection.immutable.Set, but we are using scala.collection.Set to fix JDT
1270 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=468276#c32
1271 final Set<Member> members = cluster.readView().unreachableMembers();
1272 return members.contains(member);
1276 public void testInstallSnapshot() throws Exception {
1277 final String testName = "testInstallSnapshot";
1278 final String leaderCarShardName = "member-1-shard-cars-" + testName;
1279 final String followerCarShardName = "member-2-shard-cars-" + testName;
1281 // Setup a saved snapshot on the leader. The follower will startup with no data and the leader should
1282 // install a snapshot to sync the follower.
1284 DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
1285 SchemaContextHelper.full());
1287 final ContainerNode carsNode = CarsModel.newCarsNode(
1288 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", Uint64.valueOf(20000))));
1289 AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
1291 final NormalizedNode snapshotRoot = AbstractShardTest.readStore(tree, YangInstanceIdentifier.of());
1292 final Snapshot initialSnapshot = Snapshot.create(
1293 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(snapshotRoot)),
1294 Collections.emptyList(), 5, 1, 5, 1, 1, null, null);
1295 InMemorySnapshotStore.addSnapshot(leaderCarShardName, initialSnapshot);
1297 InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
1298 InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
1300 initDatastoresWithCars(testName);
1302 assertEquals(Optional.of(carsNode), leaderDistributedDataStore.newReadOnlyTransaction().read(
1303 CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS));
1305 verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class),
1306 initialSnapshot, snapshotRoot);
1308 verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class),
1309 initialSnapshot, snapshotRoot);
1313 public void testReadWriteMessageSlicing() throws Exception {
1314 leaderDatastoreContextBuilder.maximumMessageSliceSize(100);
1315 followerDatastoreContextBuilder.maximumMessageSliceSize(100);
1316 initDatastoresWithCars("testLargeReadReplySlicing");
1318 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1320 final NormalizedNode carsNode = CarsModel.create();
1321 rwTx.write(CarsModel.BASE_PATH, carsNode);
1323 verifyNode(rwTx, CarsModel.BASE_PATH, carsNode);
1326 @SuppressWarnings("IllegalCatch")
1328 public void testRaftCallbackDuringLeadershipDrop() throws Exception {
1329 final String testName = "testRaftCallbackDuringLeadershipDrop";
1330 initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1332 final ExecutorService executor = Executors.newSingleThreadExecutor();
1334 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
1335 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500)
1336 .shardLeaderElectionTimeoutInSeconds(3600),
1339 final DOMStoreWriteTransaction initialWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1340 initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1341 leaderTestKit.doCommit(initialWriteTx.ready());
1343 try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
1344 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false)) {
1346 final ActorRef member3Cars = ((LocalShardStore) follower2DistributedDataStore).getLocalShards()
1347 .getLocalShards().get("cars").getActor();
1348 final ActorRef member2Cars = ((LocalShardStore)followerDistributedDataStore).getLocalShards()
1349 .getLocalShards().get("cars").getActor();
1350 member2Cars.tell(new StartDropMessages<>(AppendEntries.class), null);
1351 member3Cars.tell(new StartDropMessages<>(AppendEntries.class), null);
1353 final DOMStoreWriteTransaction newTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1354 newTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
1355 final AtomicBoolean submitDone = new AtomicBoolean(false);
1356 executor.submit(() -> {
1358 leaderTestKit.doCommit(newTx.ready());
1359 submitDone.set(true);
1360 } catch (Exception e) {
1361 throw new RuntimeException(e);
1364 final ActorRef leaderCars = ((LocalShardStore) leaderDistributedDataStore).getLocalShards()
1365 .getLocalShards().get("cars").getActor();
1366 await().atMost(10, TimeUnit.SECONDS)
1367 .until(() -> ((OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1368 .executeOperation(leaderCars, GetOnDemandRaftState.INSTANCE)).getLastIndex() >= 1);
1370 final OnDemandRaftState raftState = (OnDemandRaftState)leaderDistributedDataStore.getActorUtils()
1371 .executeOperation(leaderCars, GetOnDemandRaftState.INSTANCE);
1373 // Simulate a follower not receiving heartbeats but still being able to send messages ie RequestVote with
1374 // new term(switching to candidate after election timeout)
1375 leaderCars.tell(new RequestVote(raftState.getCurrentTerm() + 1,
1376 "member-3-shard-cars-testRaftCallbackDuringLeadershipDrop", -1,
1379 member2Cars.tell(new StopDropMessages<>(AppendEntries.class), null);
1380 member3Cars.tell(new StopDropMessages<>(AppendEntries.class), null);
1382 await("Is tx stuck in COMMIT_PENDING")
1383 .atMost(10, TimeUnit.SECONDS).untilAtomic(submitDone, equalTo(true));
1387 executor.shutdownNow();
1391 public void testSnapshotOnRootOverwrite() throws Exception {
1392 initDatastores("testSnapshotOnRootOverwrite", "module-shards-default-cars-member1-and-2.conf",
1393 new String[] {"cars", "default"},
1394 leaderDatastoreContextBuilder.snapshotOnRootOverwrite(true),
1395 followerDatastoreContextBuilder.snapshotOnRootOverwrite(true));
1397 leaderTestKit.waitForMembersUp("member-2");
1398 final ContainerNode rootNode = Builders.containerBuilder()
1399 .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
1400 .withChild(CarsModel.create())
1403 leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.of(), rootNode);
1405 // FIXME: CONTROLLER-2020: ClientBackedDatastore does not have stable indexes/term,
1406 // the snapshot index seems to fluctuate
1408 IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1409 state -> assertEquals(1, state.getSnapshotIndex()));
1411 IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1412 state -> assertEquals(1, state.getSnapshotIndex()));
1414 verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 1);
1415 verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 1);
1417 for (int i = 0; i < 10; i++) {
1418 leaderTestKit.testWriteTransaction(leaderDistributedDataStore, CarsModel.newCarPath("car " + i),
1419 CarsModel.newCarEntry("car " + i, Uint64.ONE));
1422 // fake snapshot causes the snapshotIndex to move
1423 IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1424 state -> assertEquals(10, state.getSnapshotIndex()));
1425 IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1426 state -> assertEquals(10, state.getSnapshotIndex()));
1428 // however the real snapshot still has not changed and was taken at index 1
1429 verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 1);
1430 verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 1);
1432 // root overwrite so expect a snapshot
1433 leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.of(), rootNode);
1435 // this was a real snapshot so everything should be in it(1(DisableTrackingPayload) + 1 + 10 + 1)
1436 IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1437 state -> assertEquals(12, state.getSnapshotIndex()));
1438 IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1439 state -> assertEquals(12, state.getSnapshotIndex()));
1441 verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 12);
1442 verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 12);
1445 private static void verifySnapshot(final String persistenceId, final long lastAppliedIndex) {
1446 await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
1447 List<Snapshot> snap = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
1448 assertEquals(1, snap.size());
1449 assertEquals(lastAppliedIndex, snap.get(0).getLastAppliedIndex());
1454 private static void verifySnapshot(final Snapshot actual, final Snapshot expected,
1455 final NormalizedNode expRoot) {
1456 assertEquals("Snapshot getLastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
1457 assertEquals("Snapshot getLastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
1458 assertEquals("Snapshot getLastTerm", expected.getLastTerm(), actual.getLastTerm());
1459 assertEquals("Snapshot getLastIndex", expected.getLastIndex(), actual.getLastIndex());
1460 assertEquals("Snapshot state type", ShardSnapshotState.class, actual.getState().getClass());
1461 MetadataShardDataTreeSnapshot shardSnapshot =
1462 (MetadataShardDataTreeSnapshot) ((ShardSnapshotState)actual.getState()).getSnapshot();
1463 assertEquals("Snapshot root node", expRoot, shardSnapshot.getRootNode().orElseThrow());
1466 private static void sendDatastoreContextUpdate(final AbstractDataStore dataStore, final Builder builder) {
1467 final Builder newBuilder = DatastoreContext.newBuilderFrom(builder.build());
1468 final DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class);
1469 final Answer<DatastoreContext> answer = invocation -> newBuilder.build();
1470 doAnswer(answer).when(mockContextFactory).getBaseDatastoreContext();
1471 doAnswer(answer).when(mockContextFactory).getShardDatastoreContext(anyString());
1472 dataStore.onDatastoreContextUpdated(mockContextFactory);