Bump upstream SNAPSHOTS
[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.containsString;
12 import static org.hamcrest.CoreMatchers.instanceOf;
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.hamcrest.Matchers.equalTo;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertThrows;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assume.assumeTrue;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doAnswer;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.verify;
28
29 import akka.actor.ActorRef;
30 import akka.actor.ActorSelection;
31 import akka.actor.ActorSystem;
32 import akka.actor.Address;
33 import akka.actor.AddressFromURIString;
34 import akka.cluster.Cluster;
35 import akka.cluster.Member;
36 import akka.dispatch.Futures;
37 import akka.pattern.Patterns;
38 import akka.testkit.javadsl.TestKit;
39 import com.google.common.base.Stopwatch;
40 import com.google.common.base.Throwables;
41 import com.google.common.collect.ImmutableMap;
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.Optional;
52 import java.util.concurrent.ExecutionException;
53 import java.util.concurrent.ExecutorService;
54 import java.util.concurrent.Executors;
55 import java.util.concurrent.TimeUnit;
56 import java.util.concurrent.atomic.AtomicBoolean;
57 import java.util.concurrent.atomic.AtomicLong;
58 import org.junit.After;
59 import org.junit.Before;
60 import org.junit.Test;
61 import org.junit.runner.RunWith;
62 import org.junit.runners.Parameterized;
63 import org.junit.runners.Parameterized.Parameter;
64 import org.junit.runners.Parameterized.Parameters;
65 import org.mockito.stubbing.Answer;
66 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
67 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
68 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
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.exceptions.NoShardLeaderException;
76 import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
77 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
78 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
79 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
80 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
81 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
82 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
83 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
84 import org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata;
85 import org.opendaylight.controller.cluster.datastore.persisted.FrontendShardDataTreeSnapshotMetadata;
86 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
87 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
88 import org.opendaylight.controller.cluster.datastore.utils.UnsignedLongBitmap;
89 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
90 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
91 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
92 import org.opendaylight.controller.cluster.raft.client.messages.Shutdown;
93 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
94 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
95 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
96 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
97 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
98 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
99 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
100 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
101 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
102 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
103 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
104 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
105 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
106 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
107 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
108 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
109 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
110 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
111 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
112 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
113 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
114 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
115 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
116 import org.opendaylight.yangtools.yang.common.Uint64;
117 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
118 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
119 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
120 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
121 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
122 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
123 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
124 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
125 import org.opendaylight.yangtools.yang.data.tree.api.ConflictingModificationAppliedException;
126 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
127 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
128 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
129 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
130 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
131 import scala.collection.Set;
132 import scala.concurrent.Await;
133 import scala.concurrent.Future;
134 import scala.concurrent.duration.FiniteDuration;
135
136 /**
137  * End-to-end distributed data store tests that exercise remote shards and transactions.
138  *
139  * @author Thomas Pantelis
140  */
141 @RunWith(Parameterized.class)
142 public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
143
144     @Parameters(name = "{0}")
145     public static Collection<Object[]> data() {
146         return Arrays.asList(new Object[][] {
147                 { TestDistributedDataStore.class, 7 }, { TestClientBackedDataStore.class, 12 }
148         });
149     }
150
151     @Parameter(0)
152     public Class<? extends AbstractDataStore> testParameter;
153     @Parameter(1)
154     public int commitTimeout;
155
156     private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
157     private static final String[] CARS = {"cars"};
158
159     private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse(
160             "akka://cluster-test@127.0.0.1:2558");
161     private static final Address MEMBER_2_ADDRESS = AddressFromURIString.parse(
162             "akka://cluster-test@127.0.0.1:2559");
163
164     private static final String MODULE_SHARDS_CARS_ONLY_1_2 = "module-shards-cars-member-1-and-2.conf";
165     private static final String MODULE_SHARDS_CARS_PEOPLE_1_2 = "module-shards-member1-and-2.conf";
166     private static final String MODULE_SHARDS_CARS_PEOPLE_1_2_3 = "module-shards-member1-and-2-and-3.conf";
167     private static final String MODULE_SHARDS_CARS_1_2_3 = "module-shards-cars-member-1-and-2-and-3.conf";
168
169     private ActorSystem leaderSystem;
170     private ActorSystem followerSystem;
171     private ActorSystem follower2System;
172
173     private final DatastoreContext.Builder leaderDatastoreContextBuilder =
174             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
175
176     private final DatastoreContext.Builder followerDatastoreContextBuilder =
177             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5)
178                 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
179     private final TransactionIdentifier tx1 = nextTransactionId();
180     private final TransactionIdentifier tx2 = nextTransactionId();
181
182     private AbstractDataStore followerDistributedDataStore;
183     private AbstractDataStore leaderDistributedDataStore;
184     private IntegrationTestKit followerTestKit;
185     private IntegrationTestKit leaderTestKit;
186
187     @Before
188     public void setUp() {
189         InMemoryJournal.clear();
190         InMemorySnapshotStore.clear();
191
192         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
193         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
194
195         followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
196         Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
197
198         follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
199         Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
200     }
201
202     @After
203     public void tearDown() {
204         if (followerDistributedDataStore != null) {
205             leaderDistributedDataStore.close();
206         }
207         if (leaderDistributedDataStore != null) {
208             leaderDistributedDataStore.close();
209         }
210
211         TestKit.shutdownActorSystem(leaderSystem, true);
212         TestKit.shutdownActorSystem(followerSystem, true);
213         TestKit.shutdownActorSystem(follower2System,true);
214
215         InMemoryJournal.clear();
216         InMemorySnapshotStore.clear();
217     }
218
219     private void initDatastoresWithCars(final String type) throws Exception {
220         initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
221     }
222
223     private void initDatastoresWithCarsAndPeople(final String type) throws Exception {
224         initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
225     }
226
227     private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards)
228             throws Exception {
229         initDatastores(type, moduleShardsConfig, shards, leaderDatastoreContextBuilder,
230                 followerDatastoreContextBuilder);
231     }
232
233     private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards,
234             final DatastoreContext.Builder leaderBuilder, final DatastoreContext.Builder followerBuilder)
235                     throws Exception {
236         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderBuilder, commitTimeout);
237
238         leaderDistributedDataStore = leaderTestKit.setupAbstractDataStore(
239                 testParameter, type, moduleShardsConfig, false, shards);
240
241         followerTestKit = new IntegrationTestKit(followerSystem, followerBuilder, commitTimeout);
242         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
243                 testParameter, type, moduleShardsConfig, false, shards);
244
245         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorUtils(), shards);
246
247         leaderTestKit.waitForMembersUp("member-2");
248         followerTestKit.waitForMembersUp("member-1");
249     }
250
251     private static void verifyCars(final DOMStoreReadTransaction readTx, final MapEntryNode... entries)
252             throws Exception {
253         final Optional<NormalizedNode> optional = readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
254         assertTrue("isPresent", optional.isPresent());
255
256         final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listBuilder = ImmutableNodes.mapNodeBuilder(
257                 CarsModel.CAR_QNAME);
258         for (final NormalizedNode entry: entries) {
259             listBuilder.withChild((MapEntryNode) entry);
260         }
261
262         assertEquals("Car list node", listBuilder.build(), optional.get());
263     }
264
265     private static void verifyNode(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path,
266             final NormalizedNode expNode) throws Exception {
267         assertEquals(Optional.of(expNode), readTx.read(path).get(5, TimeUnit.SECONDS));
268     }
269
270     private static void verifyExists(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path)
271             throws Exception {
272         assertEquals("exists", Boolean.TRUE, readTx.exists(path).get(5, TimeUnit.SECONDS));
273     }
274
275     @Test
276     public void testWriteTransactionWithSingleShard() throws Exception {
277         final String testName = "testWriteTransactionWithSingleShard";
278         initDatastoresWithCars(testName);
279
280         final String followerCarShardName = "member-2-shard-cars-" + testName;
281
282         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
283         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
284
285         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
286         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
287
288         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
289         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
290         writeTx.merge(car1Path, car1);
291
292         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
293         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
294         writeTx.merge(car2Path, car2);
295
296         followerTestKit.doCommit(writeTx.ready());
297
298         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
299
300         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
301
302         // Test delete
303
304         writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
305
306         writeTx.delete(car1Path);
307
308         followerTestKit.doCommit(writeTx.ready());
309
310         verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
311
312         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
313
314         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
315
316         // Re-instate the follower member 2 as a single-node to verify replication and recovery.
317
318         // The following is a bit tricky. Before we reinstate the follower we need to ensure it has persisted and
319         // applied and all the log entries from the leader. Since we've verified the car data above we know that
320         // all the transactions have been applied on the leader so we first read and capture its lastAppliedIndex.
321         final AtomicLong leaderLastAppliedIndex = new AtomicLong();
322         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, CARS[0],
323             state -> leaderLastAppliedIndex.set(state.getLastApplied()));
324
325         // Now we need to make sure the follower has persisted the leader's lastAppliedIndex via ApplyJournalEntries.
326         // However we don't know exactly how many ApplyJournalEntries messages there will be as it can differ between
327         // the tell-based and ask-based front-ends. For ask-based there will be exactly 2 ApplyJournalEntries but
328         // tell-based persists additional payloads which could be replicated and applied in a batch resulting in
329         // either 2 or 3 ApplyJournalEntries. To handle this we read the follower's persisted ApplyJournalEntries
330         // until we find the one that encompasses the leader's lastAppliedIndex.
331         Stopwatch sw = Stopwatch.createStarted();
332         boolean done = false;
333         while (!done) {
334             final List<ApplyJournalEntries> entries = InMemoryJournal.get(followerCarShardName,
335                     ApplyJournalEntries.class);
336             for (ApplyJournalEntries aje: entries) {
337                 if (aje.getToIndex() >= leaderLastAppliedIndex.get()) {
338                     done = true;
339                     break;
340                 }
341             }
342
343             assertTrue("Follower did not persist ApplyJournalEntries containing leader's lastAppliedIndex "
344                     + leaderLastAppliedIndex + ". Entries persisted: " + entries, sw.elapsed(TimeUnit.SECONDS) <= 5);
345
346             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
347         }
348
349         TestKit.shutdownActorSystem(leaderSystem, true);
350         TestKit.shutdownActorSystem(followerSystem, true);
351
352         final ActorSystem newSystem = newActorSystem("reinstated-member2", "Member2");
353
354         try (AbstractDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder,
355                 commitTimeout)
356                 .setupAbstractDataStore(testParameter, testName, "module-shards-member2", true, CARS)) {
357             verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
358         }
359     }
360
361     @Test
362     public void testSingleTransactionsWritesInQuickSuccession() throws Exception {
363         initDatastoresWithCars("testSingleTransactionsWritesInQuickSuccession");
364
365         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
366
367         DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
368         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
369         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
370         followerTestKit.doCommit(writeTx.ready());
371
372         int numCars = 5;
373         for (int i = 0; i < numCars; i++) {
374             writeTx = txChain.newWriteOnlyTransaction();
375             writeTx.write(CarsModel.newCarPath("car" + i), CarsModel.newCarEntry("car" + i, Uint64.valueOf(20000)));
376             followerTestKit.doCommit(writeTx.ready());
377
378             try (var tx = txChain.newReadOnlyTransaction()) {
379                 tx.read(CarsModel.BASE_PATH).get();
380             }
381         }
382
383         // wait to let the shard catch up with purged
384         await("Range set leak test").atMost(5, TimeUnit.SECONDS)
385                 .pollInterval(500, TimeUnit.MILLISECONDS)
386                 .untilAsserted(() -> {
387                     final var localShard = leaderDistributedDataStore.getActorUtils().findLocalShard("cars")
388                         .orElseThrow();
389                     final var frontendMetadata =
390                         (FrontendShardDataTreeSnapshotMetadata) leaderDistributedDataStore.getActorUtils()
391                             .executeOperation(localShard, new RequestFrontendMetadata());
392
393                     final var clientMeta = frontendMetadata.getClients().get(0);
394                     if (leaderDistributedDataStore.getActorUtils().getDatastoreContext().isUseTellBasedProtocol()) {
395                         assertTellClientMetadata(clientMeta, numCars * 2);
396                     } else {
397                         assertAskClientMetadata(clientMeta);
398                     }
399                 });
400
401         try (var tx = txChain.newReadOnlyTransaction()) {
402             final var body = tx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS).orElseThrow().body();
403             assertThat(body, instanceOf(Collection.class));
404             assertEquals(numCars, ((Collection<?>) body).size());
405         }
406     }
407
408     private static void assertAskClientMetadata(final FrontendClientMetadata clientMeta) {
409         // ask based should track no metadata
410         assertEquals(List.of(), clientMeta.getCurrentHistories());
411     }
412
413     private static void assertTellClientMetadata(final FrontendClientMetadata clientMeta, final long lastPurged) {
414         final var iterator = clientMeta.getCurrentHistories().iterator();
415         var metadata = iterator.next();
416         while (iterator.hasNext() && metadata.getHistoryId() != 1) {
417             metadata = iterator.next();
418         }
419
420         assertEquals(UnsignedLongBitmap.of(), metadata.getClosedTransactions());
421         assertEquals("[[0.." + lastPurged + "]]", metadata.getPurgedTransactions().ranges().toString());
422     }
423
424     @Test
425     public void testCloseTransactionMetadataLeak() throws Exception {
426         // FIXME: CONTROLLER-2016: ask-based frontend triggers this:
427         //
428         // java.lang.IllegalStateException: Previous transaction
429         //            member-2-datastore-testCloseTransactionMetadataLeak-fe-0-chn-1-txn-1-0 is not ready yet
430         //        at org.opendaylight.controller.cluster.datastore.TransactionChainProxy$Allocated.checkReady()
431         //        at org.opendaylight.controller.cluster.datastore.TransactionChainProxy.newReadOnlyTransaction()
432         assumeTrue(testParameter.isAssignableFrom(ClientBackedDataStore.class));
433
434         initDatastoresWithCars("testCloseTransactionMetadataLeak");
435
436         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
437
438         DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
439         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
440         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
441         followerTestKit.doCommit(writeTx.ready());
442
443         int numCars = 5;
444         for (int i = 0; i < numCars; i++) {
445             try (var tx = txChain.newWriteOnlyTransaction()) {
446                 // Empty on purpose
447             }
448
449             try (var tx = txChain.newReadOnlyTransaction()) {
450                 tx.read(CarsModel.BASE_PATH).get();
451             }
452         }
453
454         // wait to let the shard catch up with purged
455         await("wait for purges to settle").atMost(5, TimeUnit.SECONDS)
456                 .pollInterval(500, TimeUnit.MILLISECONDS)
457                 .untilAsserted(() -> {
458                     final var localShard = leaderDistributedDataStore.getActorUtils().findLocalShard("cars")
459                         .orElseThrow();
460                     final var frontendMetadata =
461                             (FrontendShardDataTreeSnapshotMetadata) leaderDistributedDataStore.getActorUtils()
462                                     .executeOperation(localShard, new RequestFrontendMetadata());
463
464                     final var clientMeta = frontendMetadata.getClients().get(0);
465                     if (leaderDistributedDataStore.getActorUtils().getDatastoreContext().isUseTellBasedProtocol()) {
466                         assertTellClientMetadata(clientMeta, numCars * 2);
467                     } else {
468                         assertAskClientMetadata(clientMeta);
469                     }
470                 });
471     }
472
473     @Test
474     public void testReadWriteTransactionWithSingleShard() throws Exception {
475         initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
476
477         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
478         assertNotNull("newReadWriteTransaction returned null", rwTx);
479
480         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
481         rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
482
483         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
484         rwTx.merge(CarsModel.newCarPath("optima"), car1);
485
486         verifyCars(rwTx, car1);
487
488         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
489         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
490         rwTx.merge(car2Path, car2);
491
492         verifyExists(rwTx, car2Path);
493
494         followerTestKit.doCommit(rwTx.ready());
495
496         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
497     }
498
499     @Test
500     public void testWriteTransactionWithMultipleShards() throws Exception {
501         initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
502
503         final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
504         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
505
506         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
507         final NormalizedNode carsNode = CarsModel.emptyContainer();
508         writeTx.write(carsPath, carsNode);
509
510         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
511         final NormalizedNode peopleNode = PeopleModel.emptyContainer();
512         writeTx.write(peoplePath, peopleNode);
513
514         followerTestKit.doCommit(writeTx.ready());
515
516         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
517
518         verifyNode(readTx, carsPath, carsNode);
519         verifyNode(readTx, peoplePath, peopleNode);
520     }
521
522     @Test
523     public void testReadWriteTransactionWithMultipleShards() throws Exception {
524         initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
525
526         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
527         assertNotNull("newReadWriteTransaction returned null", rwTx);
528
529         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
530         final NormalizedNode carsNode = CarsModel.emptyContainer();
531         rwTx.write(carsPath, carsNode);
532
533         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
534         final NormalizedNode peopleNode = PeopleModel.emptyContainer();
535         rwTx.write(peoplePath, peopleNode);
536
537         followerTestKit.doCommit(rwTx.ready());
538
539         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
540
541         verifyNode(readTx, carsPath, carsNode);
542         verifyNode(readTx, peoplePath, peopleNode);
543     }
544
545     @Test
546     public void testTransactionChainWithSingleShard() throws Exception {
547         initDatastoresWithCars("testTransactionChainWithSingleShard");
548
549         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
550
551         // Add the top-level cars container with write-only.
552
553         final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
554         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
555
556         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
557
558         final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
559
560         // Verify the top-level cars container with read-only.
561
562         verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
563
564         // Perform car operations with read-write.
565
566         final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
567
568         verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
569
570         rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
571
572         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
573         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
574         rwTx.write(car1Path, car1);
575
576         verifyExists(rwTx, car1Path);
577
578         verifyCars(rwTx, car1);
579
580         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(25000));
581         rwTx.merge(CarsModel.newCarPath("sportage"), car2);
582
583         rwTx.delete(car1Path);
584
585         followerTestKit.doCommit(writeTxReady);
586
587         followerTestKit.doCommit(rwTx.ready());
588
589         txChain.close();
590
591         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
592     }
593
594     @Test
595     public void testTransactionChainWithMultipleShards() throws Exception {
596         initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
597
598         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
599
600         DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
601         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
602
603         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
604         writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
605
606         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
607         writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
608
609         followerTestKit.doCommit(writeTx.ready());
610
611         final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
612
613         final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
614         final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
615         readWriteTx.write(carPath, car);
616
617         final MapEntryNode person = PeopleModel.newPersonEntry("jack");
618         final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
619         readWriteTx.merge(personPath, person);
620
621         assertEquals(Optional.of(car), readWriteTx.read(carPath).get(5, TimeUnit.SECONDS));
622         assertEquals(Optional.of(person), readWriteTx.read(personPath).get(5, TimeUnit.SECONDS));
623
624         final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
625
626         writeTx = txChain.newWriteOnlyTransaction();
627
628         writeTx.delete(personPath);
629
630         final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
631
632         followerTestKit.doCommit(cohort2);
633         followerTestKit.doCommit(cohort3);
634
635         txChain.close();
636
637         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
638         verifyCars(readTx, car);
639
640         assertEquals(Optional.empty(), readTx.read(personPath).get(5, TimeUnit.SECONDS));
641     }
642
643     @Test
644     public void testChainedTransactionFailureWithSingleShard() throws Exception {
645         initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
646
647         final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
648                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
649                         LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
650                         MoreExecutors.directExecutor());
651
652         final DOMTransactionChainListener listener = mock(DOMTransactionChainListener.class);
653         final DOMTransactionChain txChain = broker.createTransactionChain(listener);
654
655         final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
656
657         final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
658                 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
659                     .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
660
661         writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
662
663         final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS))
664             .getCause();
665         assertThat(ex, instanceOf(TransactionCommitFailedException.class));
666
667         verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
668
669         txChain.close();
670         broker.close();
671     }
672
673     @Test
674     public void testChainedTransactionFailureWithMultipleShards() throws Exception {
675         initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
676
677         final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
678                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
679                         LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
680                         MoreExecutors.directExecutor());
681
682         final DOMTransactionChainListener listener = mock(DOMTransactionChainListener.class);
683         final DOMTransactionChain txChain = broker.createTransactionChain(listener);
684
685         final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
686
687         writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
688
689         final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
690                 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
691                     .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
692
693         // Note that merge will validate the data and fail but put succeeds b/c deep validation is not
694         // done for put for performance reasons.
695         writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
696
697         final var ex = assertThrows(ExecutionException.class, () -> writeTx.commit().get(5, TimeUnit.SECONDS))
698             .getCause();
699         assertThat(ex, instanceOf(TransactionCommitFailedException.class));
700
701         verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
702
703         txChain.close();
704         broker.close();
705     }
706
707     @Test
708     public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
709         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
710         final String testName = "testSingleShardTransactionsWithLeaderChanges";
711         initDatastoresWithCars(testName);
712
713         final String followerCarShardName = "member-2-shard-cars-" + testName;
714         InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
715
716         // Write top-level car container from the follower so it uses a remote Tx.
717
718         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
719
720         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
721         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
722
723         followerTestKit.doCommit(writeTx.ready());
724
725         InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
726
727         // Switch the leader to the follower
728
729         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
730                 .shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
731
732         TestKit.shutdownActorSystem(leaderSystem, true);
733         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
734
735         followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorUtils(), CARS);
736
737         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
738         Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
739
740         final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder()
741                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
742         IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
743
744         try (AbstractDataStore ds =
745                 newMember1TestKit.setupAbstractDataStore(
746                         testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
747
748             followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), CARS);
749
750             // Write a car entry to the new leader - should switch to local Tx
751
752             writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
753
754             MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
755             YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
756             writeTx.merge(car1Path, car1);
757
758             followerTestKit.doCommit(writeTx.ready());
759
760             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
761         }
762     }
763
764     @Test
765     public void testReadyLocalTransactionForwardedToLeader() throws Exception {
766         initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
767         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
768
769         final Optional<ActorRef> carsFollowerShard =
770                 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
771         assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
772
773         final DataTree dataTree = new InMemoryDataTreeFactory().create(
774             DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
775
776         // Send a tx with immediate commit.
777
778         DataTreeModification modification = dataTree.takeSnapshot().newModification();
779         new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
780         new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
781
782         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
783         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
784         modification.ready();
785
786         ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1 , modification, true, Optional.empty());
787
788         carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
789         Object resp = followerTestKit.expectMsgClass(Object.class);
790         if (resp instanceof akka.actor.Status.Failure) {
791             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
792         }
793
794         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
795
796         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
797
798         // Send another tx without immediate commit.
799
800         modification = dataTree.takeSnapshot().newModification();
801         MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(30000));
802         new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
803         modification.ready();
804
805         readyLocal = new ReadyLocalTransaction(tx2 , modification, false, Optional.empty());
806
807         carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
808         resp = followerTestKit.expectMsgClass(Object.class);
809         if (resp instanceof akka.actor.Status.Failure) {
810             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
811         }
812
813         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
814
815         final ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
816                 ((ReadyTransactionReply)resp).getCohortPath());
817
818         ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(leaderDistributedDataStore.getActorUtils(),
819             List.of(new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor),
820                 () -> DataStoreVersions.CURRENT_VERSION)), tx2);
821         cohort.canCommit().get(5, TimeUnit.SECONDS);
822         cohort.preCommit().get(5, TimeUnit.SECONDS);
823         cohort.commit().get(5, TimeUnit.SECONDS);
824
825         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
826     }
827
828     @Test
829     public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
830         initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
831         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorUtils(), "cars");
832
833         final Optional<ActorRef> carsFollowerShard =
834                 followerDistributedDataStore.getActorUtils().findLocalShard("cars");
835         assertTrue("Cars follower shard found", carsFollowerShard.isPresent());
836
837         carsFollowerShard.get().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
838         final DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
839
840         // Send a tx with immediate commit.
841
842         DataTreeModification modification = dataTree.takeSnapshot().newModification();
843         new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
844         new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
845
846         final MapEntryNode car1 = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
847         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
848
849         ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction(tx1, DataStoreVersions.CURRENT_VERSION,
850             new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx1, modification),
851             true, Optional.empty());
852
853         carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
854         Object resp = followerTestKit.expectMsgClass(Object.class);
855         if (resp instanceof akka.actor.Status.Failure) {
856             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
857         }
858
859         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
860
861         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
862
863         // Send another tx without immediate commit.
864
865         modification = dataTree.takeSnapshot().newModification();
866         MapEntryNode car2 = CarsModel.newCarEntry("sportage", Uint64.valueOf(30000));
867         new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
868
869         forwardedReady = new ForwardedReadyTransaction(tx2, DataStoreVersions.CURRENT_VERSION,
870             new ReadWriteShardDataTreeTransaction(mock(ShardDataTreeTransactionParent.class), tx2, modification),
871             false, Optional.empty());
872
873         carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
874         resp = followerTestKit.expectMsgClass(Object.class);
875         if (resp instanceof akka.actor.Status.Failure) {
876             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
877         }
878
879         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
880
881         ActorSelection txActor = leaderDistributedDataStore.getActorUtils().actorSelection(
882                 ((ReadyTransactionReply)resp).getCohortPath());
883
884         final ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
885             leaderDistributedDataStore.getActorUtils(), List.of(
886                 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor),
887                     () -> DataStoreVersions.CURRENT_VERSION)), tx2);
888         cohort.canCommit().get(5, TimeUnit.SECONDS);
889         cohort.preCommit().get(5, TimeUnit.SECONDS);
890         cohort.commit().get(5, TimeUnit.SECONDS);
891
892         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
893     }
894
895     @Test
896     public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
897         followerDatastoreContextBuilder.shardBatchedModificationCount(2);
898         leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
899         initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
900
901         // Do an initial write to get the primary shard info cached.
902
903         final DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
904         initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
905         initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
906         followerTestKit.doCommit(initialWriteTx.ready());
907
908         // Wait for the commit to be replicated to the follower.
909
910         MemberNode.verifyRaftState(followerDistributedDataStore, "cars",
911             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
912
913         MemberNode.verifyRaftState(followerDistributedDataStore, "people",
914             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
915
916         // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
917         // the leader shard.
918
919         final DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
920         writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
921         writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
922         final DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
923         final ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
924         writeTx1CanCommit.get(5, TimeUnit.SECONDS);
925
926         // Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
927         // in the leader shard.
928
929         final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
930         final LinkedList<MapEntryNode> cars = new LinkedList<>();
931         int carIndex = 1;
932         cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
933         writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
934         carIndex++;
935         NormalizedNode people = ImmutableNodes.mapNodeBuilder(PeopleModel.PERSON_QNAME)
936                 .withChild(PeopleModel.newPersonEntry("Dude")).build();
937         writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
938         final DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
939
940         // Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
941         // tx writes 5 cars so 2 BatchedModidifications messages will be sent initially and cached in the
942         // leader shard (with shardBatchedModificationCount set to 2). The 3rd BatchedModidifications will be
943         // sent on ready.
944
945         final DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
946         for (int i = 1; i <= 5; i++, carIndex++) {
947             cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
948             writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
949         }
950
951         // Prepare another WO that writes to a single shard. This will send a single BatchedModidifications
952         // message on ready.
953
954         final DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
955         cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
956         writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
957         carIndex++;
958
959         // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaciton message to the
960         // leader shard on ready.
961
962         final DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
963         cars.add(CarsModel.newCarEntry("car" + carIndex, Uint64.valueOf(carIndex)));
964         readWriteTx.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
965
966         // FIXME: CONTROLLER-2017: ClientBackedDataStore reports only 4 transactions
967         assumeTrue(DistributedDataStore.class.isAssignableFrom(testParameter));
968         IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
969             stats -> assertEquals("getReadWriteTransactionCount", 5, stats.getReadWriteTransactionCount()));
970
971         // Disable elections on the leader so it switches to follower.
972
973         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
974                 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
975                 .shardElectionTimeoutFactor(10));
976
977         leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorUtils(), "cars");
978
979         // Submit all tx's - the messages should get queued for retry.
980
981         final ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
982         final DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
983         final DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
984         final DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
985
986         // Enable elections on the other follower so it becomes the leader, at which point the
987         // tx's should get forwarded from the previous leader to the new leader to complete the commits.
988
989         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
990                 .customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
991         IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorUtils(), "cars")
992                 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
993         IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorUtils(), "people")
994                 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
995
996         followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
997         followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
998         followerTestKit.doCommit(writeTx3Cohort);
999         followerTestKit.doCommit(writeTx4Cohort);
1000         followerTestKit.doCommit(rwTxCohort);
1001
1002         DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
1003         verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
1004         verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
1005     }
1006
1007     @Test
1008     public void testLeadershipTransferOnShutdown() throws Exception {
1009         leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
1010         followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
1011         final String testName = "testLeadershipTransferOnShutdown";
1012         initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
1013
1014         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
1015                 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500),
1016                 commitTimeout);
1017         try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
1018                 testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
1019
1020             followerTestKit.waitForMembersUp("member-3");
1021             follower2TestKit.waitForMembersUp("member-1", "member-2");
1022
1023             // Create and submit a couple tx's so they're pending.
1024
1025             DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
1026             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1027             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
1028             writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
1029             final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
1030
1031             final var usesCohorts = DistributedDataStore.class.isAssignableFrom(testParameter);
1032             if (usesCohorts) {
1033                 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1034                     stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
1035             }
1036
1037             writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
1038             final MapEntryNode car = CarsModel.newCarEntry("optima", Uint64.valueOf(20000));
1039             writeTx.write(CarsModel.newCarPath("optima"), car);
1040             final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
1041
1042             if (usesCohorts) {
1043                 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
1044                     stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
1045             }
1046
1047             // Gracefully stop the leader via a Shutdown message.
1048
1049             sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1050                 .shardElectionTimeoutFactor(100));
1051
1052             final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
1053             final Future<ActorRef> future = leaderDistributedDataStore.getActorUtils().findLocalShardAsync("cars");
1054             final ActorRef leaderActor = Await.result(future, duration);
1055
1056             final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
1057
1058             // Commit the 2 transactions. They should finish and succeed.
1059
1060             followerTestKit.doCommit(cohort1);
1061             followerTestKit.doCommit(cohort2);
1062
1063             // Wait for the leader actor stopped.
1064
1065             final Boolean stopped = Await.result(stopFuture, duration);
1066             assertEquals("Stopped", Boolean.TRUE, stopped);
1067
1068             // Verify leadership was transferred by reading the committed data from the other nodes.
1069
1070             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
1071             verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
1072         }
1073     }
1074
1075     @Test
1076     public void testTransactionWithIsolatedLeader() throws Exception {
1077         // Set the isolated leader check interval high so we can control the switch to IsolatedLeader.
1078         leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(10000000);
1079         final String testName = "testTransactionWithIsolatedLeader";
1080         initDatastoresWithCars(testName);
1081
1082         // Tx that is submitted after the follower is stopped but before the leader transitions to IsolatedLeader.
1083         final DOMStoreWriteTransaction preIsolatedLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1084         preIsolatedLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1085
1086         // Tx that is submitted after the leader transitions to IsolatedLeader.
1087         final DOMStoreWriteTransaction noShardLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1088         noShardLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1089
1090         // Tx that is submitted after the follower is reinstated.
1091         final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1092         successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1093
1094         // Stop the follower
1095         followerTestKit.watch(followerDistributedDataStore.getActorUtils().getShardManager());
1096         followerDistributedDataStore.close();
1097         followerTestKit.expectTerminated(followerDistributedDataStore.getActorUtils().getShardManager());
1098
1099         // Submit the preIsolatedLeaderWriteTx so it's pending
1100         final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
1101
1102         // Change the isolated leader check interval low so it changes to IsolatedLeader.
1103         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1104                 .shardIsolatedLeaderCheckIntervalInMillis(200));
1105
1106         MemberNode.verifyRaftState(leaderDistributedDataStore, "cars",
1107             raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
1108
1109         final var noShardLeaderCohort = noShardLeaderWriteTx.ready();
1110         final ListenableFuture<Boolean> canCommit;
1111
1112         // There is difference in behavior here:
1113         if (!leaderDistributedDataStore.getActorUtils().getDatastoreContext().isUseTellBasedProtocol()) {
1114             // ask-based canCommit() times out and aborts
1115             final var ex = assertThrows(ExecutionException.class,
1116                 () -> leaderTestKit.doCommit(noShardLeaderCohort)).getCause();
1117             assertThat(ex, instanceOf(NoShardLeaderException.class));
1118             assertThat(ex.getMessage(), containsString(
1119                 "Shard member-1-shard-cars-testTransactionWithIsolatedLeader currently has no leader."));
1120             canCommit = null;
1121         } else {
1122             // tell-based canCommit() does not have a real timeout and hence continues
1123             canCommit = noShardLeaderCohort.canCommit();
1124             Uninterruptibles.sleepUninterruptibly(commitTimeout, TimeUnit.SECONDS);
1125             assertFalse(canCommit.isDone());
1126         }
1127
1128         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
1129                 .shardElectionTimeoutFactor(100));
1130
1131         final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
1132
1133         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
1134                 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
1135
1136         leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
1137         leaderTestKit.doCommit(successTxCohort);
1138
1139         // continuation of tell-based protocol: readied transaction will complete commit, but will report an OLFE
1140         if (canCommit != null) {
1141             final var ex = assertThrows(ExecutionException.class,
1142                 () -> canCommit.get(commitTimeout, TimeUnit.SECONDS)).getCause();
1143             assertThat(ex, instanceOf(OptimisticLockFailedException.class));
1144             assertEquals("Optimistic lock failed for path " + CarsModel.BASE_PATH, ex.getMessage());
1145             final var cause = ex.getCause();
1146             assertThat(cause, instanceOf(ConflictingModificationAppliedException.class));
1147             final var cmae = (ConflictingModificationAppliedException) cause;
1148             assertEquals("Node was created by other transaction.", cmae.getMessage());
1149             assertEquals(CarsModel.BASE_PATH, cmae.getPath());
1150         }
1151     }
1152
1153     @Test
1154     public void testTransactionWithShardLeaderNotResponding() throws Exception {
1155         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1156         followerDatastoreContextBuilder.shardElectionTimeoutFactor(50);
1157         initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
1158
1159         // Do an initial read to get the primary shard info cached.
1160
1161         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1162         readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1163
1164         // Shutdown the leader and try to create a new tx.
1165
1166         TestKit.shutdownActorSystem(leaderSystem, true);
1167
1168         followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
1169         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
1170
1171         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1172
1173         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1174
1175         final var ex = assertThrows(ExecutionException.class, () -> followerTestKit.doCommit(rwTx.ready()));
1176         final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(ex.getCause());
1177         if (DistributedDataStore.class.isAssignableFrom(testParameter)) {
1178             assertTrue(msg, Throwables.getRootCause(ex) instanceof NoShardLeaderException
1179                 || ex.getCause() instanceof ShardLeaderNotRespondingException);
1180         } else {
1181             assertThat(msg, Throwables.getRootCause(ex), instanceOf(RequestTimeoutException.class));
1182         }
1183     }
1184
1185     @Test
1186     public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
1187         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1188         initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
1189
1190         // Do an initial read to get the primary shard info cached.
1191
1192         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1193         readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1194
1195         // Shutdown the leader and try to create a new tx.
1196
1197         TestKit.shutdownActorSystem(leaderSystem, true);
1198
1199         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1200
1201         Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1202
1203         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1204                 .operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
1205
1206         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1207
1208         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1209
1210         final var ex = assertThrows(ExecutionException.class, () -> followerTestKit.doCommit(rwTx.ready()));
1211         final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(ex.getCause());
1212         if (DistributedDataStore.class.isAssignableFrom(testParameter)) {
1213             assertThat(msg, Throwables.getRootCause(ex), instanceOf(NoShardLeaderException.class));
1214         } else {
1215             assertThat(msg, Throwables.getRootCause(ex), instanceOf(RequestTimeoutException.class));
1216         }
1217     }
1218
1219     @Test
1220     public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
1221         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
1222         String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
1223         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1224
1225         final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1226                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1227         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1228                 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1229
1230         try (AbstractDataStore ds =
1231                 follower2TestKit.setupAbstractDataStore(
1232                         testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
1233
1234             followerTestKit.waitForMembersUp("member-1", "member-3");
1235             follower2TestKit.waitForMembersUp("member-1", "member-2");
1236
1237             // Do an initial read to get the primary shard info cached.
1238
1239             final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1240             readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1241
1242             // Shutdown the leader and try to create a new tx.
1243
1244             TestKit.shutdownActorSystem(leaderSystem, true);
1245
1246             Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1247
1248             sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1249                 .operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
1250
1251             final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1252
1253             rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1254
1255             followerTestKit.doCommit(rwTx.ready());
1256         }
1257     }
1258
1259     @Test
1260     public void testSemiReachableCandidateNotDroppingLeader() throws Exception {
1261         final String testName = "testSemiReachableCandidateNotDroppingLeader";
1262         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1263
1264         final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1265                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1266         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1267                 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1268
1269         final AbstractDataStore ds2 =
1270                      follower2TestKit.setupAbstractDataStore(
1271                              testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS);
1272
1273         followerTestKit.waitForMembersUp("member-1", "member-3");
1274         follower2TestKit.waitForMembersUp("member-1", "member-2");
1275
1276         // behavior is controlled by akka.coordinated-shutdown.run-by-actor-system-terminate configuration option
1277         TestKit.shutdownActorSystem(follower2System, true);
1278
1279         ActorRef cars = leaderDistributedDataStore.getActorUtils().findLocalShard("cars").get();
1280         final OnDemandRaftState initialState = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1281                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1282
1283         Cluster leaderCluster = Cluster.get(leaderSystem);
1284         Cluster followerCluster = Cluster.get(followerSystem);
1285         Cluster follower2Cluster = Cluster.get(follower2System);
1286
1287         Member follower2Member = follower2Cluster.readView().self();
1288
1289         await().atMost(10, TimeUnit.SECONDS)
1290                 .until(() -> containsUnreachable(leaderCluster, follower2Member));
1291         await().atMost(10, TimeUnit.SECONDS)
1292                 .until(() -> containsUnreachable(followerCluster, follower2Member));
1293
1294         ActorRef followerCars = followerDistributedDataStore.getActorUtils().findLocalShard("cars").get();
1295
1296         // to simulate a follower not being able to receive messages, but still being able to send messages and becoming
1297         // candidate, we can just send a couple of RequestVotes to both leader and follower.
1298         cars.tell(new RequestVote(initialState.getCurrentTerm() + 1, "member-3-shard-cars", -1, -1), null);
1299         followerCars.tell(new RequestVote(initialState.getCurrentTerm() + 1, "member-3-shard-cars", -1, -1), null);
1300         cars.tell(new RequestVote(initialState.getCurrentTerm() + 3, "member-3-shard-cars", -1, -1), null);
1301         followerCars.tell(new RequestVote(initialState.getCurrentTerm() + 3, "member-3-shard-cars", -1, -1), null);
1302
1303         OnDemandRaftState stateAfter = (OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1304                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1305         OnDemandRaftState followerState = (OnDemandRaftState) followerDistributedDataStore.getActorUtils()
1306                 .executeOperation(cars, GetOnDemandRaftState.INSTANCE);
1307
1308         assertEquals(initialState.getCurrentTerm(), stateAfter.getCurrentTerm());
1309         assertEquals(initialState.getCurrentTerm(), followerState.getCurrentTerm());
1310
1311         ds2.close();
1312     }
1313
1314     private static Boolean containsUnreachable(final Cluster cluster, final Member member) {
1315         // unreachableMembers() returns scala.collection.immutable.Set, but we are using scala.collection.Set to fix JDT
1316         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=468276#c32
1317         final Set<Member> members = cluster.readView().unreachableMembers();
1318         return members.contains(member);
1319     }
1320
1321     @Test
1322     public void testInstallSnapshot() throws Exception {
1323         final String testName = "testInstallSnapshot";
1324         final String leaderCarShardName = "member-1-shard-cars-" + testName;
1325         final String followerCarShardName = "member-2-shard-cars-" + testName;
1326
1327         // Setup a saved snapshot on the leader. The follower will startup with no data and the leader should
1328         // install a snapshot to sync the follower.
1329
1330         DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
1331             SchemaContextHelper.full());
1332
1333         final ContainerNode carsNode = CarsModel.newCarsNode(
1334                 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", Uint64.valueOf(20000))));
1335         AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
1336
1337         final NormalizedNode snapshotRoot = AbstractShardTest.readStore(tree, YangInstanceIdentifier.empty());
1338         final Snapshot initialSnapshot = Snapshot.create(
1339                 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(snapshotRoot)),
1340                 Collections.emptyList(), 5, 1, 5, 1, 1, null, null);
1341         InMemorySnapshotStore.addSnapshot(leaderCarShardName, initialSnapshot);
1342
1343         InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
1344         InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
1345
1346         initDatastoresWithCars(testName);
1347
1348         assertEquals(Optional.of(carsNode), leaderDistributedDataStore.newReadOnlyTransaction().read(
1349             CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS));
1350
1351         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class),
1352                 initialSnapshot, snapshotRoot);
1353
1354         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class),
1355                 initialSnapshot, snapshotRoot);
1356     }
1357
1358     @Test
1359     public void testReadWriteMessageSlicing() throws Exception {
1360         // The slicing is only implemented for tell-based protocol
1361         assumeTrue(ClientBackedDataStore.class.isAssignableFrom(testParameter));
1362
1363         leaderDatastoreContextBuilder.maximumMessageSliceSize(100);
1364         followerDatastoreContextBuilder.maximumMessageSliceSize(100);
1365         initDatastoresWithCars("testLargeReadReplySlicing");
1366
1367         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1368
1369         final NormalizedNode carsNode = CarsModel.create();
1370         rwTx.write(CarsModel.BASE_PATH, carsNode);
1371
1372         verifyNode(rwTx, CarsModel.BASE_PATH, carsNode);
1373     }
1374
1375     @SuppressWarnings("IllegalCatch")
1376     @Test
1377     public void testRaftCallbackDuringLeadershipDrop() throws Exception {
1378         final String testName = "testRaftCallbackDuringLeadershipDrop";
1379         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1380
1381         final ExecutorService executor = Executors.newSingleThreadExecutor();
1382
1383         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
1384                 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(500)
1385                         .shardLeaderElectionTimeoutInSeconds(3600),
1386                 commitTimeout);
1387
1388         final DOMStoreWriteTransaction initialWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1389         initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1390         leaderTestKit.doCommit(initialWriteTx.ready());
1391
1392         try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
1393                 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false)) {
1394
1395             final ActorRef member3Cars = ((LocalShardStore) follower2DistributedDataStore).getLocalShards()
1396                     .getLocalShards().get("cars").getActor();
1397             final ActorRef member2Cars = ((LocalShardStore)followerDistributedDataStore).getLocalShards()
1398                     .getLocalShards().get("cars").getActor();
1399             member2Cars.tell(new StartDropMessages(AppendEntries.class), null);
1400             member3Cars.tell(new StartDropMessages(AppendEntries.class), null);
1401
1402             final DOMStoreWriteTransaction newTx = leaderDistributedDataStore.newWriteOnlyTransaction();
1403             newTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
1404             final AtomicBoolean submitDone = new AtomicBoolean(false);
1405             executor.submit(() -> {
1406                 try {
1407                     leaderTestKit.doCommit(newTx.ready());
1408                     submitDone.set(true);
1409                 } catch (Exception e) {
1410                     throw new RuntimeException(e);
1411                 }
1412             });
1413             final ActorRef leaderCars = ((LocalShardStore) leaderDistributedDataStore).getLocalShards()
1414                     .getLocalShards().get("cars").getActor();
1415             await().atMost(10, TimeUnit.SECONDS)
1416                     .until(() -> ((OnDemandRaftState) leaderDistributedDataStore.getActorUtils()
1417                             .executeOperation(leaderCars, GetOnDemandRaftState.INSTANCE)).getLastIndex() >= 1);
1418
1419             final OnDemandRaftState raftState = (OnDemandRaftState)leaderDistributedDataStore.getActorUtils()
1420                     .executeOperation(leaderCars, GetOnDemandRaftState.INSTANCE);
1421
1422             // Simulate a follower not receiving heartbeats but still being able to send messages ie RequestVote with
1423             // new term(switching to candidate after election timeout)
1424             leaderCars.tell(new RequestVote(raftState.getCurrentTerm() + 1,
1425                     "member-3-shard-cars-testRaftCallbackDuringLeadershipDrop", -1,
1426                             -1), member3Cars);
1427
1428             member2Cars.tell(new StopDropMessages(AppendEntries.class), null);
1429             member3Cars.tell(new StopDropMessages(AppendEntries.class), null);
1430
1431             await("Is tx stuck in COMMIT_PENDING")
1432                     .atMost(10, TimeUnit.SECONDS).untilAtomic(submitDone, equalTo(true));
1433
1434         }
1435
1436         executor.shutdownNow();
1437     }
1438
1439     @Test
1440     public void testSnapshotOnRootOverwrite() throws Exception {
1441         initDatastores("testSnapshotOnRootOverwrite", "module-shards-default-cars-member1-and-2.conf",
1442             new String[] {"cars", "default"},
1443             leaderDatastoreContextBuilder.snapshotOnRootOverwrite(true),
1444             followerDatastoreContextBuilder.snapshotOnRootOverwrite(true));
1445
1446         leaderTestKit.waitForMembersUp("member-2");
1447         final ContainerNode rootNode = ImmutableContainerNodeBuilder.create()
1448                 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifier.create(SchemaContext.NAME))
1449                 .withChild(CarsModel.create())
1450                 .build();
1451
1452         leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.empty(), rootNode);
1453
1454         // FIXME: CONTROLLER-2020: ClientBackedDatastore does not have stable indexes/term,
1455         //                         the snapshot index seems to fluctuate
1456         assumeTrue(DistributedDataStore.class.isAssignableFrom(testParameter));
1457         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1458             state -> assertEquals(1, state.getSnapshotIndex()));
1459
1460         IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1461             state -> assertEquals(1, state.getSnapshotIndex()));
1462
1463         verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 1);
1464         verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 1);
1465
1466         for (int i = 0; i < 10; i++) {
1467             leaderTestKit.testWriteTransaction(leaderDistributedDataStore, CarsModel.newCarPath("car " + i),
1468                     CarsModel.newCarEntry("car " + i, Uint64.ONE));
1469         }
1470
1471         // fake snapshot causes the snapshotIndex to move
1472         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1473             state -> assertEquals(10, state.getSnapshotIndex()));
1474         IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1475             state -> assertEquals(10, state.getSnapshotIndex()));
1476
1477         // however the real snapshot still has not changed and was taken at index 1
1478         verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 1);
1479         verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 1);
1480
1481         // root overwrite so expect a snapshot
1482         leaderTestKit.testWriteTransaction(leaderDistributedDataStore, YangInstanceIdentifier.empty(), rootNode);
1483
1484         // this was a real snapshot so everything should be in it(1(DisableTrackingPayload) + 1 + 10 + 1)
1485         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, "cars",
1486             state -> assertEquals(12, state.getSnapshotIndex()));
1487         IntegrationTestKit.verifyShardState(followerDistributedDataStore, "cars",
1488             state -> assertEquals(12, state.getSnapshotIndex()));
1489
1490         verifySnapshot("member-1-shard-cars-testSnapshotOnRootOverwrite", 12);
1491         verifySnapshot("member-2-shard-cars-testSnapshotOnRootOverwrite", 12);
1492     }
1493
1494     private static void verifySnapshot(final String persistenceId, final long lastAppliedIndex) {
1495         await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
1496                 List<Snapshot> snap = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
1497                 assertEquals(1, snap.size());
1498                 assertEquals(lastAppliedIndex, snap.get(0).getLastAppliedIndex());
1499             }
1500         );
1501     }
1502
1503     private static void verifySnapshot(final Snapshot actual, final Snapshot expected,
1504                                        final NormalizedNode expRoot) {
1505         assertEquals("Snapshot getLastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
1506         assertEquals("Snapshot getLastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
1507         assertEquals("Snapshot getLastTerm", expected.getLastTerm(), actual.getLastTerm());
1508         assertEquals("Snapshot getLastIndex", expected.getLastIndex(), actual.getLastIndex());
1509         assertEquals("Snapshot state type", ShardSnapshotState.class, actual.getState().getClass());
1510         MetadataShardDataTreeSnapshot shardSnapshot =
1511                 (MetadataShardDataTreeSnapshot) ((ShardSnapshotState)actual.getState()).getSnapshot();
1512         assertEquals("Snapshot root node", expRoot, shardSnapshot.getRootNode().get());
1513     }
1514
1515     private static void sendDatastoreContextUpdate(final AbstractDataStore dataStore, final Builder builder) {
1516         final Builder newBuilder = DatastoreContext.newBuilderFrom(builder.build());
1517         final DatastoreContextFactory mockContextFactory = mock(DatastoreContextFactory.class);
1518         final Answer<DatastoreContext> answer = invocation -> newBuilder.build();
1519         doAnswer(answer).when(mockContextFactory).getBaseDatastoreContext();
1520         doAnswer(answer).when(mockContextFactory).getShardDatastoreContext(anyString());
1521         dataStore.onDatastoreContextUpdated(mockContextFactory);
1522     }
1523 }