Bump upstreams
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreRemotingIntegrationTest.java
1 /*
2  * Copyright (c) 2015, 2017 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
27
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;
51 import java.util.Map;
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;
131
132 /**
133  * End-to-end distributed data store tests that exercise remote shards and transactions.
134  *
135  * @author Thomas Pantelis
136  */
137 @RunWith(Parameterized.class)
138 public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
139
140     @Parameters(name = "{0}")
141     public static Collection<Object[]> data() {
142         return Arrays.asList(new Object[][] {
143                 { TestClientBackedDataStore.class, 12 }
144         });
145     }
146
147     @Parameter(0)
148     public Class<? extends AbstractDataStore> testParameter;
149     @Parameter(1)
150     public int commitTimeout;
151
152     private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
153     private static final String[] CARS = {"cars"};
154
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");
159
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";
164
165     private ActorSystem leaderSystem;
166     private ActorSystem followerSystem;
167     private ActorSystem follower2System;
168
169     private final DatastoreContext.Builder leaderDatastoreContextBuilder =
170             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
171
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();
177
178     private AbstractDataStore followerDistributedDataStore;
179     private AbstractDataStore leaderDistributedDataStore;
180     private IntegrationTestKit followerTestKit;
181     private IntegrationTestKit leaderTestKit;
182
183     @Before
184     public void setUp() {
185         InMemoryJournal.clear();
186         InMemorySnapshotStore.clear();
187
188         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
189         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
190
191         followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
192         Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
193
194         follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
195         Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
196     }
197
198     @After
199     public void tearDown() {
200         if (followerDistributedDataStore != null) {
201             leaderDistributedDataStore.close();
202         }
203         if (leaderDistributedDataStore != null) {
204             leaderDistributedDataStore.close();
205         }
206
207         TestKit.shutdownActorSystem(leaderSystem, true);
208         TestKit.shutdownActorSystem(followerSystem, true);
209         TestKit.shutdownActorSystem(follower2System,true);
210
211         InMemoryJournal.clear();
212         InMemorySnapshotStore.clear();
213     }
214
215     private void initDatastoresWithCars(final String type) throws Exception {
216         initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
217     }
218
219     private void initDatastoresWithCarsAndPeople(final String type) throws Exception {
220         initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
221     }
222
223     private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards)
224             throws Exception {
225         initDatastores(type, moduleShardsConfig, shards, leaderDatastoreContextBuilder,
226                 followerDatastoreContextBuilder);
227     }
228
229     private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards,
230             final DatastoreContext.Builder leaderBuilder, final DatastoreContext.Builder followerBuilder)
231                     throws Exception {
232         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderBuilder, commitTimeout);
233
234         leaderDistributedDataStore = leaderTestKit.setupAbstractDataStore(
235                 testParameter, type, moduleShardsConfig, false, shards);
236
237         followerTestKit = new IntegrationTestKit(followerSystem, followerBuilder, commitTimeout);
238         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
239                 testParameter, type, moduleShardsConfig, false, shards);
240
241         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(), shards);
242
243         leaderTestKit.waitForMembersUp("member-2");
244         followerTestKit.waitForMembersUp("member-1");
245     }
246
247     private static void verifyCars(final DOMStoreReadTransaction readTx, final MapEntryNode... entries)
248             throws Exception {
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));
252     }
253
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));
257     }
258
259     private static void verifyExists(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path)
260             throws Exception {
261         assertEquals("exists", Boolean.TRUE, readTx.exists(path).get(5, TimeUnit.SECONDS));
262     }
263
264     @Test
265     public void testWriteTransactionWithSingleShard() throws Exception {
266         final String testName = "testWriteTransactionWithSingleShard";
267         initDatastoresWithCars(testName);
268
269         final String followerCarShardName = "member-2-shard-cars-" + testName;
270
271         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
272         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
273
274         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
275         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
276
277         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
278         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
279         writeTx.merge(car1Path, car1);
280
281         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
282         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
283         writeTx.merge(car2Path, car2);
284
285         followerTestKit.doCommit(writeTx.ready());
286
287         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
288
289         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
290
291         // Test delete
292
293         writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
294
295         writeTx.delete(car1Path);
296
297         followerTestKit.doCommit(writeTx.ready());
298
299         verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
300
301         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
302
303         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
304
305         // Re-instate the follower member 2 as a single-node to verify replication and recovery.
306
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()));
313
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;
322         while (!done) {
323             final List<ApplyJournalEntries> entries = InMemoryJournal.get(followerCarShardName,
324                     ApplyJournalEntries.class);
325             for (ApplyJournalEntries aje: entries) {
326                 if (aje.getToIndex() >= leaderLastAppliedIndex.get()) {
327                     done = true;
328                     break;
329                 }
330             }
331
332             assertTrue("Follower did not persist ApplyJournalEntries containing leader's lastAppliedIndex "
333                     + leaderLastAppliedIndex + ". Entries persisted: " + entries, sw.elapsed(TimeUnit.SECONDS) <= 5);
334
335             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
336         }
337
338         TestKit.shutdownActorSystem(leaderSystem, true);
339         TestKit.shutdownActorSystem(followerSystem, true);
340
341         final ActorSystem newSystem = newActorSystem("reinstated-member2", "Member2");
342
343         try (AbstractDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder,
344                 commitTimeout)
345                 .setupAbstractDataStore(testParameter, testName, "module-shards-member2", true, CARS)) {
346             verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
347         }
348     }
349
350     @Test
351     public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
352         initDatastoresWithCars("testSingleTransactionsWritesInQuickSuccession");
353
354         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
355
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());
360
361         int numCars = 5;
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());
366
367             try (var tx = txChain.newReadOnlyTransaction()) {
368                 tx.read(CarsModel.BASE_PATH).get();
369             }
370         }
371
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());
380
381                 assertClientMetadata(frontendMetadata.getClients().get(0), numCars * 2);
382             });
383
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());
388         }
389     }
390
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();
396         }
397
398         assertEquals(UnsignedLongBitmap.of(), metadata.getClosedTransactions());
399         assertEquals("[[0.." + lastPurged + "]]", metadata.getPurgedTransactions().ranges().toString());
400     }
401
402     @Test
403     public void testCloseTransactionMetadataLeak() throws Exception {
404         initDatastoresWithCars("testCloseTransactionMetadataLeak");
405
406         final var txChain = followerDistributedDataStore.createTransactionChain();
407
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());
412
413         int numCars = 5;
414         for (int i = 0; i < numCars; i++) {
415             try (var tx = txChain.newWriteOnlyTransaction()) {
416                 // Empty on purpose
417             }
418
419             try (var tx = txChain.newReadOnlyTransaction()) {
420                 tx.read(CarsModel.BASE_PATH).get();
421             }
422         }
423
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());
432
433                 assertClientMetadata(frontendMetadata.getClients().get(0), numCars * 2);
434             });
435     }
436
437     @Test
438     public void testReadWriteTransactionWithSingleShard() throws Exception {
439         initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
440
441         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
442         assertNotNull("newReadWriteTransaction returned null", rwTx);
443
444         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
445         rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
446
447         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
448         rwTx.merge(CarsModel.newCarPath("optima"), car1);
449
450         verifyCars(rwTx, car1);
451
452         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
453         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
454         rwTx.merge(car2Path, car2);
455
456         verifyExists(rwTx, car2Path);
457
458         followerTestKit.doCommit(rwTx.ready());
459
460         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
461     }
462
463     @Test
464     public void testWriteTransactionWithMultipleShards() throws Exception {
465         initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
466
467         final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
468         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
469
470         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
471         final NormalizedNode carsNode = CarsModel.emptyContainer();
472         writeTx.write(carsPath, carsNode);
473
474         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
475         final NormalizedNode peopleNode = PeopleModel.emptyContainer();
476         writeTx.write(peoplePath, peopleNode);
477
478         followerTestKit.doCommit(writeTx.ready());
479
480         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
481
482         verifyNode(readTx, carsPath, carsNode);
483         verifyNode(readTx, peoplePath, peopleNode);
484     }
485
486     @Test
487     public void testReadWriteTransactionWithMultipleShards() throws Exception {
488         initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
489
490         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
491         assertNotNull("newReadWriteTransaction returned null", rwTx);
492
493         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
494         final NormalizedNode carsNode = CarsModel.emptyContainer();
495         rwTx.write(carsPath, carsNode);
496
497         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
498         final NormalizedNode peopleNode = PeopleModel.emptyContainer();
499         rwTx.write(peoplePath, peopleNode);
500
501         followerTestKit.doCommit(rwTx.ready());
502
503         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
504
505         verifyNode(readTx, carsPath, carsNode);
506         verifyNode(readTx, peoplePath, peopleNode);
507     }
508
509     @Test
510     public void testTransactionChainWithSingleShard() throws Exception {
511         initDatastoresWithCars("testTransactionChainWithSingleShard");
512
513         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
514
515         // Add the top-level cars container with write-only.
516
517         final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
518         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
519
520         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
521
522         final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
523
524         // Verify the top-level cars container with read-only.
525
526         verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
527
528         // Perform car operations with read-write.
529
530         final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
531
532         verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
533
534         rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
535
536         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
537         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
538         rwTx.write(car1Path, car1);
539
540         verifyExists(rwTx, car1Path);
541
542         verifyCars(rwTx, car1);
543
544         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
545         rwTx.merge(CarsModel.newCarPath("sportage"), car2);
546
547         rwTx.delete(car1Path);
548
549         followerTestKit.doCommit(writeTxReady);
550
551         followerTestKit.doCommit(rwTx.ready());
552
553         txChain.close();
554
555         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
556     }
557
558     @Test
559     public void testTransactionChainWithMultipleShards() throws Exception {
560         initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
561
562         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
563
564         DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
565         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
566
567         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
568         writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
569
570         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
571         writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
572
573         followerTestKit.doCommit(writeTx.ready());
574
575         final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
576
577         final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
578         final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
579         readWriteTx.write(carPath, car);
580
581         final MapEntryNode person = PeopleModel.newPersonEntry("jack");
582         final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
583         readWriteTx.merge(personPath, person);
584
585         assertEquals(Optional.of(car), readWriteTx.read(carPath).get(5, TimeUnit.SECONDS));
586         assertEquals(Optional.of(person), readWriteTx.read(personPath).get(5, TimeUnit.SECONDS));
587
588         final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
589
590         writeTx = txChain.newWriteOnlyTransaction();
591
592         writeTx.delete(personPath);
593
594         final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
595
596         followerTestKit.doCommit(cohort2);
597         followerTestKit.doCommit(cohort3);
598
599         txChain.close();
600
601         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
602         verifyCars(readTx, car);
603
604         assertEquals(Optional.empty(), readTx.read(personPath).get(5, TimeUnit.SECONDS));
605     }
606
607     @Test
608     public void testChainedTransactionFailureWithSingleShard() throws Exception {
609         initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
610
611         final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
612                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
613                         LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
614                         MoreExecutors.directExecutor());
615
616         final var listener = mock(FutureCallback.class);
617         final DOMTransactionChain txChain = broker.createTransactionChain();
618         txChain.addCallback(listener);
619
620         final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
621
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"))
625             .build());
626
627         final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS));
628         assertInstanceOf(TransactionCommitFailedException.class, ex.getCause());
629
630         verify(listener, timeout(5000)).onFailure(any());
631
632         txChain.close();
633         broker.close();
634     }
635
636     @Test
637     public void testChainedTransactionFailureWithMultipleShards() throws Exception {
638         initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
639
640         try (var broker = new ConcurrentDOMDataBroker(
641             Map.of(LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore), MoreExecutors.directExecutor())) {
642
643             final var listener = mock(FutureCallback.class);
644             final DOMTransactionChain txChain = broker.createTransactionChain();
645             txChain.addCallback(listener);
646
647             final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
648
649             writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
650
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"))
656                 .build());
657
658             final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS))
659                 .getCause();
660             assertThat(ex, instanceOf(TransactionCommitFailedException.class));
661
662             verify(listener, timeout(5000)).onFailure(any());
663
664             txChain.close();
665         }
666     }
667
668     @Test
669     public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
670         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
671         final String testName = "testSingleShardTransactionsWithLeaderChanges";
672         initDatastoresWithCars(testName);
673
674         final String followerCarShardName = "member-2-shard-cars-" + testName;
675         InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
676
677         // Write top-level car container from the follower so it uses a remote Tx.
678
679         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
680
681         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
682         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
683
684         followerTestKit.doCommit(writeTx.ready());
685
686         InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
687
688         // Switch the leader to the follower
689
690         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
691                 .shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
692
693         TestKit.shutdownActorSystem(leaderSystem, true);
694         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
695
696         followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorUtils(), CARS);
697
698         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
699         Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
700
701         final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder()
702                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
703         IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
704
705         try (AbstractDataStore ds =
706                 newMember1TestKit.setupAbstractDataStore(
707                         testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
708
709             followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), CARS);
710
711             // Write a car entry to the new leader - should switch to local Tx
712
713             writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
714
715             MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
716             YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
717             writeTx.merge(car1Path, car1);
718
719             followerTestKit.doCommit(writeTx.ready());
720
721             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
722         }
723     }
724
725     @Test
726     public void testReadyLocalTransactionForwardedToLeader() throws Exception {
727         initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
728         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
729
730         final Optional<ActorRef> carsFollowerShard =
731                 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
732         assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
733
734         final DataTree dataTree = new InMemoryDataTreeFactory().create(
735             DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
736
737         // Send a tx with immediate commit.
738
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);
742
743         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
744         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
745         modification.ready();
746
747         ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1 , modification, true, Optional.empty());
748
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());
753         }
754
755         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
756
757         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
758
759         // Send another tx without immediate commit.
760
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();
765
766         readyLocal = new ReadyLocalTransaction(tx2 , modification, false, Optional.empty());
767
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());
772         }
773
774         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
775
776         final ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
777                 ((ReadyTransactionReply)resp).getCohortPath());
778
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);
785
786         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
787     }
788
789     @Test
790     public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
791         initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
792         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
793
794         final Optional<ActorRef> carsFollowerShard =
795                 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
796         assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
797
798         carsFollowerShard.orElseThrow().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
799         final DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
800
801         // Send a tx with immediate commit.
802
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);
806
807         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
808         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
809
810         ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction(tx1, DataStoreVersions.CURRENT_VERSION,
811             new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx1, modification),
812             true, Optional.empty());
813
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());
818         }
819
820         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
821
822         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
823
824         // Send another tx without immediate commit.
825
826         modification = dataTree.takeSnapshot().newModification();
827         MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(30000));
828         new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
829
830         forwardedReady = new ForwardedReadyTransaction(tx2, DataStoreVersions.CURRENT_VERSION,
831             new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx2, modification),
832             false, Optional.empty());
833
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());
838         }
839
840         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
841
842         ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
843                 ((ReadyTransactionReply)resp).getCohortPath());
844
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);
852
853         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
854     }
855
856     @Test
857     public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
858         followerDatastoreContextBuilder.shardBatchedModificationCount(2);
859         leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
860         initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
861
862         // Verify backend statistics on start
863         verifyCarsReadWriteTransactions(leaderDistributedDataStore, 0);
864         verifyCarsReadWriteTransactions(followerDistributedDataStore, 0);
865
866         // Do an initial write to get the primary shard info cached.
867
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());
872
873         // Wait for the commit to be replicated to the follower.
874
875         MemberNode.verifyRaftState(followerDistributedDataStore, "cars",
876             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
877
878         MemberNode.verifyRaftState(followerDistributedDataStore, "people",
879             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
880
881         // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
882         // the leader shard.
883
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);
890
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.
893
894         final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
895         final LinkedList<MapEntryNode> cars = new LinkedList<>();
896         int carIndex = 1;
897         cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
898         writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
899         carIndex++;
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();
904
905         // At this point only leader should see the transactions
906         verifyCarsReadWriteTransactions(leaderDistributedDataStore, 2);
907         verifyCarsReadWriteTransactions(followerDistributedDataStore, 0);
908
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.
912
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());
917         }
918
919         // Prepare another WO that writes to a single shard. This will send a single BatchedModifications message
920         // on ready.
921
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());
925         carIndex++;
926
927         // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaction message to the leader shard
928         // on ready.
929
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());
934
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);
940
941         // Disable elections on the leader so it switches to follower.
942
943         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
944                 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
945                 .shardElectionTimeoutFactor(10));
946
947         leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorUtils(), "cars");
948
949         // Submit all tx's - the messages should get queued for retry.
950
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();
955
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.
958
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());
965
966         followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
967         followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
968         followerTestKit.doCommit(writeTx3Cohort);
969         followerTestKit.doCommit(writeTx4Cohort);
970         followerTestKit.doCommit(rwTxCohort);
971
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);
976
977         DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
978         verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
979         verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
980     }
981
982     private static void verifyCarsReadWriteTransactions(final AbstractDataStore datastore, final int expected)
983             throws Exception {
984         IntegrationTestKit.verifyShardStats(datastore, "cars",
985             stats -> assertEquals("getReadWriteTransactionCount", expected, stats.getReadWriteTransactionCount()));
986     }
987
988     @Test
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);
994
995         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
996                 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500),
997                 commitTimeout);
998         try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
999                 testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
1000
1001             followerTestKit.waitForMembersUp("member-3");
1002             follower2TestKit.waitForMembersUp("member-1", "member-2");
1003
1004             // Create and submit a couple tx's so they're pending.
1005
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();
1011
1012             // FIXME: this assertion should be made in an explicit Shard test
1013             //            IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1014             //                stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
1015
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();
1020
1021             // FIXME: this assertion should be made in an explicit Shard test
1022             //            IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1023             //                stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
1024
1025             // Gracefully stop the leader via a Shutdown message.
1026
1027             sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1028                 .shardElectionTimeoutFactor(100));
1029
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);
1033
1034             final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
1035
1036             // Commit the 2 transactions. They should finish and succeed.
1037
1038             followerTestKit.doCommit(cohort1);
1039             followerTestKit.doCommit(cohort2);
1040
1041             // Wait for the leader actor stopped.
1042
1043             final Boolean stopped = Await.result(stopFuture, duration);
1044             assertEquals("Stopped", Boolean.TRUE, stopped);
1045
1046             // Verify leadership was transferred by reading the committed data from the other nodes.
1047
1048             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
1049             verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
1050         }
1051     }
1052
1053     @Test
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);
1059
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());
1063
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());
1067
1068         // Tx that is submitted after the follower is reinstated.
1069         final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1070         successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1071
1072         // Stop the follower
1073         followerTestKit.watch(followerDistributedDataStore.getActorUtils().getShardManager());
1074         followerDistributedDataStore.close();
1075         followerTestKit.expectTerminated(followerDistributedDataStore.getActorUtils().getShardManager());
1076
1077         // Submit the preIsolatedLeaderWriteTx so it's pending
1078         final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
1079
1080         // Change the isolated leader check interval low so it changes to IsolatedLeader.
1081         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1082                 .shardIsolatedLeaderCheckIntervalInMillis(200));
1083
1084         MemberNode.verifyRaftState(leaderDistributedDataStore, "cars",
1085             raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
1086
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());
1092
1093         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1094                 .shardElectionTimeoutFactor(100));
1095
1096         final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
1097
1098         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
1099                 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
1100
1101         leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
1102         leaderTestKit.doCommit(successTxCohort);
1103
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());
1114     }
1115
1116     @Test
1117     public void testTransactionWithShardLeaderNotResponding() throws Exception {
1118         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1119         followerDatastoreContextBuilder.shardElectionTimeoutFactor(50);
1120         initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
1121
1122         // Do an initial read to get the primary shard info cached.
1123
1124         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1125         readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1126
1127         // Shutdown the leader and try to create a new tx.
1128
1129         TestKit.shutdownActorSystem(leaderSystem, true);
1130
1131         followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
1132         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
1133
1134         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1135
1136         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1137
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));
1141     }
1142
1143     @Test
1144     public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
1145         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1146         initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
1147
1148         // Do an initial read to get the primary shard info cached.
1149
1150         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1151         readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1152
1153         // Shutdown the leader and try to create a new tx.
1154
1155         TestKit.shutdownActorSystem(leaderSystem, true);
1156
1157         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1158
1159         Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1160
1161         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1162                 .operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
1163
1164         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1165
1166         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1167
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));
1171     }
1172
1173     @Test
1174     public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
1175         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
1176         String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
1177         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1178
1179         final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1180                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1181         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1182                 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1183
1184         try (AbstractDataStore ds =
1185                 follower2TestKit.setupAbstractDataStore(
1186                         testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
1187
1188             followerTestKit.waitForMembersUp("member-1", "member-3");
1189             follower2TestKit.waitForMembersUp("member-1", "member-2");
1190
1191             // Do an initial read to get the primary shard info cached.
1192
1193             final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1194             readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1195
1196             // Shutdown the leader and try to create a new tx.
1197
1198             TestKit.shutdownActorSystem(leaderSystem, true);
1199
1200             Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1201
1202             sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1203                 .operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
1204
1205             final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1206
1207             rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1208
1209             followerTestKit.doCommit(rwTx.ready());
1210         }
1211     }
1212
1213     @Test
1214     public void testSemiReachableCandidateNotDroppingLeader() throws Exception {
1215         final String testName = "testSemiReachableCandidateNotDroppingLeader";
1216         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1217
1218         final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1219                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1220         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1221                 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1222
1223         final AbstractDataStore ds2 =
1224                      follower2TestKit.setupAbstractDataStore(
1225                              testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS);
1226
1227         followerTestKit.waitForMembersUp("member-1", "member-3");
1228         follower2TestKit.waitForMembersUp("member-1", "member-2");
1229
1230         // behavior is controlled by akka.coordinated-shutdown.run-by-actor-system-terminate configuration option
1231         TestKit.shutdownActorSystem(follower2System, true);
1232
1233         ActorRef cars = leaderDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
1234         final OnDemandRaftState initialState = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1235                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1236
1237         Cluster leaderCluster = Cluster.get(leaderSystem);
1238         Cluster followerCluster = Cluster.get(followerSystem);
1239         Cluster follower2Cluster = Cluster.get(follower2System);
1240
1241         Member follower2Member = follower2Cluster.readView().self();
1242
1243         await().atMost(10, TimeUnit.SECONDS)
1244                 .until(() -> containsUnreachable(leaderCluster, follower2Member));
1245         await().atMost(10, TimeUnit.SECONDS)
1246                 .until(() -> containsUnreachable(followerCluster, follower2Member));
1247
1248         ActorRef followerCars = followerDistributedDataStore.getActorUtils().findLocalShard("cars").orElseThrow();
1249
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);
1256
1257         OnDemandRaftState stateAfter = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1258                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1259         OnDemandRaftState followerState = (OnDemandRaftState) followerDistributedDataStore.getActorUtils()
1260                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1261
1262         assertEquals(initialState.getCurrentTerm(), stateAfter.getCurrentTerm());
1263         assertEquals(initialState.getCurrentTerm(), followerState.getCurrentTerm());
1264
1265         ds2.close();
1266     }
1267
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);
1273     }
1274
1275     @Test
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;
1280
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.
1283
1284         DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
1285             SchemaContextHelper.full());
1286
1287         final ContainerNode carsNode = CarsModel.newCarsNode(
1288                 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", Uint64.valueOf(20000))));
1289         AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
1290
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);
1296
1297         InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
1298         InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
1299
1300         initDatastoresWithCars(testName);
1301
1302         assertEquals(Optional.of(carsNode), leaderDistributedDataStore.newReadOnlyTransaction().read(
1303             CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS));
1304
1305         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class),
1306                 initialSnapshot, snapshotRoot);
1307
1308         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class),
1309                 initialSnapshot, snapshotRoot);
1310     }
1311
1312     @Test
1313     public void testReadWriteMessageSlicing() throws Exception {
1314         leaderDatastoreContextBuilder.maximumMessageSliceSize(100);
1315         followerDatastoreContextBuilder.maximumMessageSliceSize(100);
1316         initDatastoresWithCars("testLargeReadReplySlicing");
1317
1318         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1319
1320         final NormalizedNode carsNode = CarsModel.create();
1321         rwTx.write(CarsModel.BASE_PATH, carsNode);
1322
1323         verifyNode(rwTx, CarsModel.BASE_PATH, carsNode);
1324     }
1325
1326     @SuppressWarnings("IllegalCatch")
1327     @Test
1328     public void testRaftCallbackDuringLeadershipDrop() throws Exception {
1329         final String testName = "testRaftCallbackDuringLeadershipDrop";
1330         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1331
1332         final ExecutorService executor = Executors.newSingleThreadExecutor();
1333
1334         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
1335                 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500)
1336                         .shardLeaderElectionTimeoutInSeconds(3600),
1337                 commitTimeout);
1338
1339         final DOMStoreWriteTransaction initialWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1340         initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1341         leaderTestKit.doCommit(initialWriteTx.ready());
1342
1343         try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
1344                 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false)) {
1345
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);
1352
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(() -> {
1357                 try {
1358                     leaderTestKit.doCommit(newTx.ready());
1359                     submitDone.set(true);
1360                 } catch (Exception e) {
1361                     throw new RuntimeException(e);
1362                 }
1363             });
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);
1369
1370             final OnDemandRaftState raftState = (OnDemandRaftState)leaderDistributedDataStore.getActorUtils()
1371                     .executeOperation(leaderCars, GetOnDemandRaftState.INSTANCE);
1372
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,
1377                             -1), member3Cars);
1378
1379             member2Cars.tell(new StopDropMessages<>(AppendEntries.class), null);
1380             member3Cars.tell(new StopDropMessages<>(AppendEntries.class), null);
1381
1382             await("Is tx stuck in COMMIT_PENDING")
1383                     .atMost(10, TimeUnit.SECONDS).untilAtomic(submitDone, equalTo(true));
1384
1385         }
1386
1387         executor.shutdownNow();
1388     }
1389
1390     @Test
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));
1396
1397         leaderTestKit.waitForMembersUp("member-2");
1398         final ContainerNode rootNode = Builders.containerBuilder()
1399                 .withNodeIdentifier(NodeIdentifier.create(SchemaContext.NAME))
1400                 .withChild(CarsModel.create())
1401                 .build();
1402
1403         leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.of(), rootNode);
1404
1405         // FIXME: CONTROLLER-2020: ClientBackedDatastore does not have stable indexes/term,
1406         //                         the snapshot index seems to fluctuate
1407         assumeTrue(false);
1408         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1409             state -> assertEquals(1, state.getSnapshotIndex()));
1410
1411         IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1412             state -> assertEquals(1, state.getSnapshotIndex()));
1413
1414         verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 1);
1415         verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 1);
1416
1417         for (int i = 0; i < 10; i++) {
1418             leaderTestKit.testWriteTransaction(leaderDistributedDataStore, CarsModel.newCarPath("car " + i),
1419                     CarsModel.newCarEntry("car " + i, Uint64.ONE));
1420         }
1421
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()));
1427
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);
1431
1432         // root overwrite so expect a snapshot
1433         leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.of(), rootNode);
1434
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()));
1440
1441         verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 12);
1442         verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 12);
1443     }
1444
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());
1450             }
1451         );
1452     }
1453
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());
1464     }
1465
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);
1473     }
1474 }