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