Convert mdsal submit() calls to commit()
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.verify;
18
19 import akka.actor.ActorRef;
20 import akka.actor.ActorSelection;
21 import akka.actor.ActorSystem;
22 import akka.actor.Address;
23 import akka.actor.AddressFromURIString;
24 import akka.cluster.Cluster;
25 import akka.dispatch.Futures;
26 import akka.pattern.Patterns;
27 import akka.testkit.javadsl.TestKit;
28 import com.google.common.base.Optional;
29 import com.google.common.base.Stopwatch;
30 import com.google.common.base.Supplier;
31 import com.google.common.base.Throwables;
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.util.concurrent.ListenableFuture;
34 import com.google.common.util.concurrent.MoreExecutors;
35 import com.google.common.util.concurrent.Uninterruptibles;
36 import com.typesafe.config.ConfigFactory;
37 import java.math.BigInteger;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.LinkedList;
42 import java.util.List;
43 import java.util.concurrent.ExecutionException;
44 import java.util.concurrent.TimeUnit;
45 import java.util.concurrent.atomic.AtomicLong;
46 import org.junit.After;
47 import org.junit.Assume;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.junit.runners.Parameterized;
52 import org.junit.runners.Parameterized.Parameter;
53 import org.junit.runners.Parameterized.Parameters;
54 import org.mockito.Mockito;
55 import org.mockito.stubbing.Answer;
56 import org.opendaylight.controller.cluster.access.client.RequestTimeoutException;
57 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
58 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
59 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
60 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
61 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
62 import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
63 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
64 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
65 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
66 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
67 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
68 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
69 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
70 import org.opendaylight.controller.cluster.datastore.persisted.MetadataShardDataTreeSnapshot;
71 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
72 import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
73 import org.opendaylight.controller.cluster.raft.client.messages.Shutdown;
74 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
75 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
76 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
77 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
78 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
79 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
80 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
81 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
82 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
83 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
84 import org.opendaylight.mdsal.common.api.TransactionChainListener;
85 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
86 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
87 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
88 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
89 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
90 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
91 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
92 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
93 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
94 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
95 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
96 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
97 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
98 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
99 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
100 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
101 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
102 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
103 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
104 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
105 import scala.concurrent.Await;
106 import scala.concurrent.Future;
107 import scala.concurrent.duration.FiniteDuration;
108
109 /**
110  * End-to-end distributed data store tests that exercise remote shards and transactions.
111  *
112  * @author Thomas Pantelis
113  */
114 @RunWith(Parameterized.class)
115 public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
116
117     @Parameters(name = "{0}")
118     public static Collection<Object[]> data() {
119         return Arrays.asList(new Object[][] {
120                 { DistributedDataStore.class, 7}, { ClientBackedDataStore.class, 12 }
121         });
122     }
123
124     @Parameter(0)
125     public Class<? extends AbstractDataStore> testParameter;
126     @Parameter(1)
127     public int commitTimeout;
128
129     private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
130     private static final String[] CARS = {"cars"};
131
132     private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse(
133             "akka://cluster-test@127.0.0.1:2558");
134     private static final Address MEMBER_2_ADDRESS = AddressFromURIString.parse(
135             "akka://cluster-test@127.0.0.1:2559");
136
137     private static final String MODULE_SHARDS_CARS_ONLY_1_2 = "module-shards-cars-member-1-and-2.conf";
138     private static final String MODULE_SHARDS_CARS_PEOPLE_1_2 = "module-shards-member1-and-2.conf";
139     private static final String MODULE_SHARDS_CARS_PEOPLE_1_2_3 = "module-shards-member1-and-2-and-3.conf";
140     private static final String MODULE_SHARDS_CARS_1_2_3 = "module-shards-cars-member-1-and-2-and-3.conf";
141
142     private ActorSystem leaderSystem;
143     private ActorSystem followerSystem;
144     private ActorSystem follower2System;
145
146     private final DatastoreContext.Builder leaderDatastoreContextBuilder =
147             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
148
149     private final DatastoreContext.Builder followerDatastoreContextBuilder =
150             DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5)
151                 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
152     private final TransactionIdentifier tx1 = nextTransactionId();
153     private final TransactionIdentifier tx2 = nextTransactionId();
154
155     private AbstractDataStore followerDistributedDataStore;
156     private AbstractDataStore leaderDistributedDataStore;
157     private IntegrationTestKit followerTestKit;
158     private IntegrationTestKit leaderTestKit;
159
160     @Before
161     public void setUp() {
162         InMemoryJournal.clear();
163         InMemorySnapshotStore.clear();
164
165         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
166         Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
167
168         followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
169         Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
170
171         follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
172         Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
173     }
174
175     @After
176     public void tearDown() {
177         if (followerDistributedDataStore != null) {
178             leaderDistributedDataStore.close();
179         }
180         if (leaderDistributedDataStore != null) {
181             leaderDistributedDataStore.close();
182         }
183
184         TestKit.shutdownActorSystem(leaderSystem);
185         TestKit.shutdownActorSystem(followerSystem);
186         TestKit.shutdownActorSystem(follower2System);
187
188         InMemoryJournal.clear();
189         InMemorySnapshotStore.clear();
190     }
191
192     private void initDatastoresWithCars(final String type) throws Exception {
193         initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
194     }
195
196     private void initDatastoresWithCarsAndPeople(final String type) throws Exception {
197         initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
198     }
199
200     private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards)
201             throws Exception {
202         leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder, commitTimeout);
203
204         leaderDistributedDataStore = leaderTestKit.setupAbstractDataStore(
205                 testParameter, type, moduleShardsConfig, false, shards);
206
207         followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder, commitTimeout);
208         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
209                 testParameter, type, moduleShardsConfig, false, shards);
210
211         leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), shards);
212
213         leaderTestKit.waitForMembersUp("member-2");
214         followerTestKit.waitForMembersUp("member-1");
215     }
216
217     private static void verifyCars(final DOMStoreReadTransaction readTx, final MapEntryNode... entries)
218             throws Exception {
219         final Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
220         assertEquals("isPresent", true, optional.isPresent());
221
222         final CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = ImmutableNodes.mapNodeBuilder(
223                 CarsModel.CAR_QNAME);
224         for (final NormalizedNode<?, ?> entry: entries) {
225             listBuilder.withChild((MapEntryNode) entry);
226         }
227
228         assertEquals("Car list node", listBuilder.build(), optional.get());
229     }
230
231     private static void verifyNode(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path,
232             final NormalizedNode<?, ?> expNode) throws Exception {
233         final Optional<NormalizedNode<?, ?>> optional = readTx.read(path).get(5, TimeUnit.SECONDS);
234         assertEquals("isPresent", true, optional.isPresent());
235         assertEquals("Data node", expNode, optional.get());
236     }
237
238     private static void verifyExists(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path)
239             throws Exception {
240         final Boolean exists = readTx.exists(path).get(5, TimeUnit.SECONDS);
241         assertEquals("exists", true, exists);
242     }
243
244     @Test
245     public void testWriteTransactionWithSingleShard() throws Exception {
246         final String testName = "testWriteTransactionWithSingleShard";
247         initDatastoresWithCars(testName);
248
249         final String followerCarShardName = "member-2-shard-cars-" + testName;
250
251         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
252         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
253
254         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
255         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
256
257         final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
258         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
259         writeTx.merge(car1Path, car1);
260
261         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
262         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
263         writeTx.merge(car2Path, car2);
264
265         followerTestKit.doCommit(writeTx.ready());
266
267         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
268
269         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
270
271         // Test delete
272
273         writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
274
275         writeTx.delete(car1Path);
276
277         followerTestKit.doCommit(writeTx.ready());
278
279         verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
280
281         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
282
283         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
284
285         // Re-instate the follower member 2 as a single-node to verify replication and recovery.
286
287         // The following is a bit tricky. Before we reinstate the follower we need to ensure it has persisted and
288         // applied and all the log entries from the leader. Since we've verified the car data above we know that
289         // all the transactions have been applied on the leader so we first read and capture its lastAppliedIndex.
290         final AtomicLong leaderLastAppliedIndex = new AtomicLong();
291         IntegrationTestKit.verifyShardState(leaderDistributedDataStore, CARS[0],
292             state -> leaderLastAppliedIndex.set(state.getLastApplied()));
293
294         // Now we need to make sure the follower has persisted the leader's lastAppliedIndex via ApplyJournalEntries.
295         // However we don't know exactly how many ApplyJournalEntries messages there will be as it can differ between
296         // the tell-based and ask-based front-ends. For ask-based there will be exactly 2 ApplyJournalEntries but
297         // tell-based persists additional payloads which could be replicated and applied in a batch resulting in
298         // either 2 or 3 ApplyJournalEntries. To handle this we read the follower's persisted ApplyJournalEntries
299         // until we find the one that encompasses the leader's lastAppliedIndex.
300         Stopwatch sw = Stopwatch.createStarted();
301         boolean done = false;
302         while (!done) {
303             final List<ApplyJournalEntries> entries = InMemoryJournal.get(followerCarShardName,
304                     ApplyJournalEntries.class);
305             for (ApplyJournalEntries aje: entries) {
306                 if (aje.getToIndex() >= leaderLastAppliedIndex.get()) {
307                     done = true;
308                     break;
309                 }
310             }
311
312             assertTrue("Follower did not persist ApplyJournalEntries containing leader's lastAppliedIndex "
313                     + leaderLastAppliedIndex + ". Entries persisted: " + entries, sw.elapsed(TimeUnit.SECONDS) <= 5);
314
315             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
316         }
317
318         TestKit.shutdownActorSystem(leaderSystem, Boolean.TRUE);
319         TestKit.shutdownActorSystem(followerSystem, Boolean.TRUE);
320
321         final ActorSystem newSystem = newActorSystem("reinstated-member2", "Member2");
322
323         try (AbstractDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder,
324                 commitTimeout)
325                 .setupAbstractDataStore(testParameter, testName, "module-shards-member2", true, CARS)) {
326             verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
327         }
328     }
329
330     @Test
331     public void testReadWriteTransactionWithSingleShard() throws Exception {
332         initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
333
334         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
335         assertNotNull("newReadWriteTransaction returned null", rwTx);
336
337         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
338         rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
339
340         final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
341         rwTx.merge(CarsModel.newCarPath("optima"), car1);
342
343         verifyCars(rwTx, car1);
344
345         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
346         final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
347         rwTx.merge(car2Path, car2);
348
349         verifyExists(rwTx, car2Path);
350
351         followerTestKit.doCommit(rwTx.ready());
352
353         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
354     }
355
356     @Test
357     public void testWriteTransactionWithMultipleShards() throws Exception {
358         initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
359
360         final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
361         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
362
363         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
364         final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
365         writeTx.write(carsPath, carsNode);
366
367         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
368         final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
369         writeTx.write(peoplePath, peopleNode);
370
371         followerTestKit.doCommit(writeTx.ready());
372
373         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
374
375         verifyNode(readTx, carsPath, carsNode);
376         verifyNode(readTx, peoplePath, peopleNode);
377     }
378
379     @Test
380     public void testReadWriteTransactionWithMultipleShards() throws Exception {
381         initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
382
383         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
384         assertNotNull("newReadWriteTransaction returned null", rwTx);
385
386         final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
387         final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
388         rwTx.write(carsPath, carsNode);
389
390         final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
391         final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
392         rwTx.write(peoplePath, peopleNode);
393
394         followerTestKit.doCommit(rwTx.ready());
395
396         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
397
398         verifyNode(readTx, carsPath, carsNode);
399         verifyNode(readTx, peoplePath, peopleNode);
400     }
401
402     @Test
403     public void testTransactionChainWithSingleShard() throws Exception {
404         initDatastoresWithCars("testTransactionChainWithSingleShard");
405
406         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
407
408         // Add the top-level cars container with write-only.
409
410         final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
411         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
412
413         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
414
415         final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
416
417         // Verify the top-level cars container with read-only.
418
419         verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
420
421         // Perform car operations with read-write.
422
423         final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
424
425         verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
426
427         rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
428
429         final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
430         final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
431         rwTx.write(car1Path, car1);
432
433         verifyExists(rwTx, car1Path);
434
435         verifyCars(rwTx, car1);
436
437         final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
438         rwTx.merge(CarsModel.newCarPath("sportage"), car2);
439
440         rwTx.delete(car1Path);
441
442         followerTestKit.doCommit(writeTxReady);
443
444         followerTestKit.doCommit(rwTx.ready());
445
446         txChain.close();
447
448         verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
449     }
450
451     @Test
452     public void testTransactionChainWithMultipleShards() throws Exception {
453         initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
454
455         final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
456
457         DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
458         assertNotNull("newWriteOnlyTransaction returned null", writeTx);
459
460         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
461         writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
462
463         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
464         writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
465
466         followerTestKit.doCommit(writeTx.ready());
467
468         final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
469
470         final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
471         final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
472         readWriteTx.write(carPath, car);
473
474         final MapEntryNode person = PeopleModel.newPersonEntry("jack");
475         final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
476         readWriteTx.merge(personPath, person);
477
478         Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
479         assertEquals("isPresent", true, optional.isPresent());
480         assertEquals("Data node", car, optional.get());
481
482         optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
483         assertEquals("isPresent", true, optional.isPresent());
484         assertEquals("Data node", person, optional.get());
485
486         final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
487
488         writeTx = txChain.newWriteOnlyTransaction();
489
490         writeTx.delete(personPath);
491
492         final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
493
494         followerTestKit.doCommit(cohort2);
495         followerTestKit.doCommit(cohort3);
496
497         txChain.close();
498
499         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
500         verifyCars(readTx, car);
501
502         optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
503         assertEquals("isPresent", false, optional.isPresent());
504     }
505
506     @Test
507     public void testChainedTransactionFailureWithSingleShard() throws Exception {
508         initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
509
510         final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
511                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
512                         LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
513                         MoreExecutors.directExecutor());
514
515         final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
516         final DOMTransactionChain txChain = broker.createTransactionChain(listener);
517
518         final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
519
520         final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
521                 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
522                     .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
523
524         writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
525
526         try {
527             writeTx.commit().get(5, TimeUnit.SECONDS);
528             fail("Expected TransactionCommitFailedException");
529         } catch (final ExecutionException e) {
530             // Expected
531         }
532
533         verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
534
535         txChain.close();
536         broker.close();
537     }
538
539     @Test
540     public void testChainedTransactionFailureWithMultipleShards() throws Exception {
541         initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
542
543         final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
544                 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
545                         LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
546                         MoreExecutors.directExecutor());
547
548         final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
549         final DOMTransactionChain txChain = broker.createTransactionChain(listener);
550
551         final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
552
553         writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
554
555         final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
556                 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
557                     .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
558
559         // Note that merge will validate the data and fail but put succeeds b/c deep validation is not
560         // done for put for performance reasons.
561         writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
562
563         try {
564             writeTx.commit().get(5, TimeUnit.SECONDS);
565             fail("Expected TransactionCommitFailedException");
566         } catch (final ExecutionException e) {
567             // Expected
568         }
569
570         verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
571
572         txChain.close();
573         broker.close();
574     }
575
576     @Test
577     public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
578         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
579         final String testName = "testSingleShardTransactionsWithLeaderChanges";
580         initDatastoresWithCars(testName);
581
582         final String followerCarShardName = "member-2-shard-cars-" + testName;
583         InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
584
585         // Write top-level car container from the follower so it uses a remote Tx.
586
587         DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
588
589         writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
590         writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
591
592         followerTestKit.doCommit(writeTx.ready());
593
594         InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
595
596         // Switch the leader to the follower
597
598         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
599                 .shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
600
601         TestKit.shutdownActorSystem(leaderSystem, true);
602         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
603
604         followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), CARS);
605
606         leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
607         Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
608
609         final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder()
610                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
611         IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
612
613         try (AbstractDataStore ds =
614                 newMember1TestKit.setupAbstractDataStore(
615                         testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
616
617             followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), CARS);
618
619             // Write a car entry to the new leader - should switch to local Tx
620
621             writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
622
623             MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
624             YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
625             writeTx.merge(car1Path, car1);
626
627             followerTestKit.doCommit(writeTx.ready());
628
629             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
630         }
631     }
632
633     @SuppressWarnings("unchecked")
634     @Test
635     public void testReadyLocalTransactionForwardedToLeader() throws Exception {
636         initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
637         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
638
639         final Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext()
640                 .findLocalShard("cars");
641         assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
642
643         final DataTree dataTree = new InMemoryDataTreeFactory().create(
644             DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
645
646         // Send a tx with immediate commit.
647
648         DataTreeModification modification = dataTree.takeSnapshot().newModification();
649         new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
650         new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
651
652         final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
653         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
654         modification.ready();
655
656         ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1 , modification, true);
657
658         carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
659         Object resp = followerTestKit.expectMsgClass(Object.class);
660         if (resp instanceof akka.actor.Status.Failure) {
661             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
662         }
663
664         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
665
666         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
667
668         // Send another tx without immediate commit.
669
670         modification = dataTree.takeSnapshot().newModification();
671         MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
672         new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
673         modification.ready();
674
675         readyLocal = new ReadyLocalTransaction(tx2 , modification, false);
676
677         carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
678         resp = followerTestKit.expectMsgClass(Object.class);
679         if (resp instanceof akka.actor.Status.Failure) {
680             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
681         }
682
683         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
684
685         final ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
686                 ((ReadyTransactionReply)resp).getCohortPath());
687
688         final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
689         Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
690         ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
691                 leaderDistributedDataStore.getActorContext(), Arrays.asList(
692                         new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
693         cohort.canCommit().get(5, TimeUnit.SECONDS);
694         cohort.preCommit().get(5, TimeUnit.SECONDS);
695         cohort.commit().get(5, TimeUnit.SECONDS);
696
697         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
698     }
699
700     @SuppressWarnings("unchecked")
701     @Test
702     public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
703         initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
704         followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
705
706         final Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext()
707                 .findLocalShard("cars");
708         assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
709
710         carsFollowerShard.get().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
711         final DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
712
713         // Send a tx with immediate commit.
714
715         DataTreeModification modification = dataTree.takeSnapshot().newModification();
716         new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
717         new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
718
719         final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
720         new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
721
722         ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction(tx1,
723                 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
724                         Mockito.mock(ShardDataTreeTransactionParent.class), tx1, modification), true);
725
726         carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
727         Object resp = followerTestKit.expectMsgClass(Object.class);
728         if (resp instanceof akka.actor.Status.Failure) {
729             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
730         }
731
732         assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
733
734         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
735
736         // Send another tx without immediate commit.
737
738         modification = dataTree.takeSnapshot().newModification();
739         MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
740         new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
741
742         forwardedReady = new ForwardedReadyTransaction(tx2,
743                 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
744                         Mockito.mock(ShardDataTreeTransactionParent.class), tx2, modification), false);
745
746         carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
747         resp = followerTestKit.expectMsgClass(Object.class);
748         if (resp instanceof akka.actor.Status.Failure) {
749             throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
750         }
751
752         assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
753
754         ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
755                 ((ReadyTransactionReply)resp).getCohortPath());
756
757         final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
758         Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
759         final ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
760                 leaderDistributedDataStore.getActorContext(), Arrays.asList(
761                         new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
762         cohort.canCommit().get(5, TimeUnit.SECONDS);
763         cohort.preCommit().get(5, TimeUnit.SECONDS);
764         cohort.commit().get(5, TimeUnit.SECONDS);
765
766         verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
767     }
768
769     @Test
770     public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
771         //TODO remove when test passes also for ClientBackedDataStore
772         Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
773         followerDatastoreContextBuilder.shardBatchedModificationCount(2);
774         leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
775         initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
776
777         // Do an initial write to get the primary shard info cached.
778
779         final DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
780         initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
781         initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
782         followerTestKit.doCommit(initialWriteTx.ready());
783
784         // Wait for the commit to be replicated to the follower.
785
786         MemberNode.verifyRaftState(followerDistributedDataStore, "cars",
787             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
788
789         MemberNode.verifyRaftState(followerDistributedDataStore, "people",
790             raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
791
792         // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
793         // the leader shard.
794
795         final DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
796         writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
797         writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
798         final DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
799         final ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
800         writeTx1CanCommit.get(5, TimeUnit.SECONDS);
801
802         // Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
803         // in the leader shard.
804
805         final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
806         final LinkedList<MapEntryNode> cars = new LinkedList<>();
807         int carIndex = 1;
808         cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
809         writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
810         carIndex++;
811         NormalizedNode<?, ?> people = PeopleModel.newPersonMapNode();
812         writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
813         final DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
814
815         // Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
816         // tx writes 5 cars so 2 BatchedModidifications messages will be sent initially and cached in the
817         // leader shard (with shardBatchedModificationCount set to 2). The 3rd BatchedModidifications will be
818         // sent on ready.
819
820         final DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
821         for (int i = 1; i <= 5; i++, carIndex++) {
822             cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
823             writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
824         }
825
826         // Prepare another WO that writes to a single shard. This will send a single BatchedModidifications
827         // message on ready.
828
829         final DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
830         cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
831         writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
832         carIndex++;
833
834         // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaciton message to the
835         // leader shard on ready.
836
837         final DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
838         cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
839         readWriteTx.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
840
841         IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
842             stats -> assertEquals("getReadWriteTransactionCount", 1, stats.getReadWriteTransactionCount()));
843
844         // Disable elections on the leader so it switches to follower.
845
846         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
847                 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
848                 .shardElectionTimeoutFactor(10));
849
850         leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorContext(), "cars");
851
852         // Submit all tx's - the messages should get queued for retry.
853
854         final ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
855         final DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
856         final DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
857         final DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
858
859         // Enable elections on the other follower so it becomes the leader, at which point the
860         // tx's should get forwarded from the previous leader to the new leader to complete the commits.
861
862         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
863                 .customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
864         IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "cars")
865                 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
866         IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "people")
867                 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
868
869         followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
870         followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
871         followerTestKit.doCommit(writeTx3Cohort);
872         followerTestKit.doCommit(writeTx4Cohort);
873         followerTestKit.doCommit(rwTxCohort);
874
875         DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
876         verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
877         verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
878     }
879
880     @Test
881     public void testLeadershipTransferOnShutdown() throws Exception {
882         //TODO remove when test passes also for ClientBackedDataStore
883         Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
884         leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
885         followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
886         final String testName = "testLeadershipTransferOnShutdown";
887         initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
888
889         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
890                 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(100),
891                 commitTimeout);
892         try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
893                 testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
894
895             followerTestKit.waitForMembersUp("member-3");
896             follower2TestKit.waitForMembersUp("member-1", "member-2");
897
898             // Create and submit a couple tx's so they're pending.
899
900             DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
901             writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
902             writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
903             writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
904             final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
905
906             IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
907                 stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
908
909             writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
910             final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
911             writeTx.write(CarsModel.newCarPath("optima"), car);
912             final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
913
914             IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
915                 stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
916
917             // Gracefully stop the leader via a Shutdown message.
918
919             sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
920                 .shardElectionTimeoutFactor(100));
921
922             final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
923             final Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
924             final ActorRef leaderActor = Await.result(future, duration);
925
926             final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
927
928             // Commit the 2 transactions. They should finish and succeed.
929
930             followerTestKit.doCommit(cohort1);
931             followerTestKit.doCommit(cohort2);
932
933             // Wait for the leader actor stopped.
934
935             final Boolean stopped = Await.result(stopFuture, duration);
936             assertEquals("Stopped", Boolean.TRUE, stopped);
937
938             // Verify leadership was transferred by reading the committed data from the other nodes.
939
940             verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
941             verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
942         }
943     }
944
945     @Test
946     public void testTransactionWithIsolatedLeader() throws Exception {
947         //TODO remove when test passes also for ClientBackedDataStore
948         Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
949         // Set the isolated leader check interval high so we can control the switch to IsolatedLeader.
950         leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(10000000);
951         final String testName = "testTransactionWithIsolatedLeader";
952         initDatastoresWithCars(testName);
953
954         // Tx that is submitted after the follower is stopped but before the leader transitions to IsolatedLeader.
955         final DOMStoreWriteTransaction preIsolatedLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
956         preIsolatedLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
957
958         // Tx that is submitted after the leader transitions to IsolatedLeader.
959         final DOMStoreWriteTransaction noShardLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
960         noShardLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
961
962         // Tx that is submitted after the follower is reinstated.
963         final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
964         successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
965
966         // Stop the follower
967         followerTestKit.watch(followerDistributedDataStore.getActorContext().getShardManager());
968         followerDistributedDataStore.close();
969         followerTestKit.expectTerminated(followerDistributedDataStore.getActorContext().getShardManager());
970
971         // Submit the preIsolatedLeaderWriteTx so it's pending
972         final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
973
974         // Change the isolated leader check interval low so it changes to IsolatedLeader.
975         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
976                 .shardIsolatedLeaderCheckIntervalInMillis(200));
977
978         MemberNode.verifyRaftState(leaderDistributedDataStore, "cars",
979             raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
980
981         try {
982             leaderTestKit.doCommit(noShardLeaderWriteTx.ready());
983             fail("Expected NoShardLeaderException");
984         } catch (final ExecutionException e) {
985             assertEquals("getCause", NoShardLeaderException.class, Throwables.getRootCause(e).getClass());
986         }
987
988         sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
989                 .shardElectionTimeoutFactor(100));
990
991         final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
992
993         followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
994                 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
995
996         leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
997         leaderTestKit.doCommit(successTxCohort);
998     }
999
1000     @Test
1001     public void testTransactionWithShardLeaderNotResponding() throws Exception {
1002         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1003         followerDatastoreContextBuilder.shardElectionTimeoutFactor(50);
1004         initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
1005
1006         // Do an initial read to get the primary shard info cached.
1007
1008         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1009         readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
1010
1011         // Shutdown the leader and try to create a new tx.
1012
1013         TestKit.shutdownActorSystem(leaderSystem, true);
1014
1015         followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
1016         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
1017
1018         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1019
1020         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1021
1022         try {
1023             followerTestKit.doCommit(rwTx.ready());
1024             fail("Exception expected");
1025         } catch (final ExecutionException e) {
1026             final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
1027             if (DistributedDataStore.class.equals(testParameter)) {
1028                 assertTrue(msg, Throwables.getRootCause(e) instanceof NoShardLeaderException
1029                         || e.getCause() instanceof ShardLeaderNotRespondingException);
1030             } else {
1031                 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
1032             }
1033         }
1034     }
1035
1036     @Test
1037     public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
1038         followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1039         initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
1040
1041         // Do an initial read to get the primary shard info cached.
1042
1043         final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1044         readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
1045
1046         // Shutdown the leader and try to create a new tx.
1047
1048         TestKit.shutdownActorSystem(leaderSystem, true);
1049
1050         Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1051
1052         Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1053
1054         sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1055                 .operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
1056
1057         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1058
1059         rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1060
1061         try {
1062             followerTestKit.doCommit(rwTx.ready());
1063             fail("Exception expected");
1064         } catch (final ExecutionException e) {
1065             final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
1066             if (DistributedDataStore.class.equals(testParameter)) {
1067                 assertTrue(msg, Throwables.getRootCause(e) instanceof NoShardLeaderException);
1068             } else {
1069                 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
1070             }
1071         }
1072     }
1073
1074     @Test
1075     public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
1076         followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
1077         String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
1078         initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1079
1080         final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1081                 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1082         final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1083                 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1084
1085         try (AbstractDataStore ds =
1086                 follower2TestKit.setupAbstractDataStore(
1087                         testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
1088
1089             followerTestKit.waitForMembersUp("member-1", "member-3");
1090             follower2TestKit.waitForMembersUp("member-1", "member-2");
1091
1092             // Do an initial read to get the primary shard info cached.
1093
1094             final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1095             readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
1096
1097             // Shutdown the leader and try to create a new tx.
1098
1099             TestKit.shutdownActorSystem(leaderSystem, true);
1100
1101             Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1102
1103             sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1104                 .operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
1105
1106             final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1107
1108             rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1109
1110             followerTestKit.doCommit(rwTx.ready());
1111         }
1112     }
1113
1114     @Test
1115     public void testInstallSnapshot() throws Exception {
1116         final String testName = "testInstallSnapshot";
1117         final String leaderCarShardName = "member-1-shard-cars-" + testName;
1118         final String followerCarShardName = "member-2-shard-cars-" + testName;
1119
1120         // Setup a saved snapshot on the leader. The follower will startup with no data and the leader should
1121         // install a snapshot to sync the follower.
1122
1123         DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
1124             SchemaContextHelper.full());
1125
1126         final ContainerNode carsNode = CarsModel.newCarsNode(
1127                 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000))));
1128         AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
1129
1130         final NormalizedNode<?, ?> snapshotRoot = AbstractShardTest.readStore(tree, YangInstanceIdentifier.EMPTY);
1131         final Snapshot initialSnapshot = Snapshot.create(
1132                 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(snapshotRoot)),
1133                 Collections.emptyList(), 5, 1, 5, 1, 1, null, null);
1134         InMemorySnapshotStore.addSnapshot(leaderCarShardName, initialSnapshot);
1135
1136         InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
1137         InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
1138
1139         initDatastoresWithCars(testName);
1140
1141         final Optional<NormalizedNode<?, ?>> readOptional = leaderDistributedDataStore.newReadOnlyTransaction().read(
1142                 CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
1143         assertEquals("isPresent", true, readOptional.isPresent());
1144         assertEquals("Node", carsNode, readOptional.get());
1145
1146         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class),
1147                 initialSnapshot, snapshotRoot);
1148
1149         verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class),
1150                 initialSnapshot, snapshotRoot);
1151     }
1152
1153     @Test
1154     public void testReadWriteMessageSlicing() throws Exception {
1155         // The slicing is only implemented for tell-based protocol
1156         Assume.assumeTrue(testParameter.equals(ClientBackedDataStore.class));
1157
1158         leaderDatastoreContextBuilder.maximumMessageSliceSize(100);
1159         followerDatastoreContextBuilder.maximumMessageSliceSize(100);
1160         initDatastoresWithCars("testLargeReadReplySlicing");
1161
1162         final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1163
1164         final NormalizedNode<?, ?> carsNode = CarsModel.create();
1165         rwTx.write(CarsModel.BASE_PATH, carsNode);
1166
1167         verifyNode(rwTx, CarsModel.BASE_PATH, carsNode);
1168     }
1169
1170     private static void verifySnapshot(final Snapshot actual, final Snapshot expected,
1171                                        final NormalizedNode<?, ?> expRoot) {
1172         assertEquals("Snapshot getLastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
1173         assertEquals("Snapshot getLastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
1174         assertEquals("Snapshot getLastTerm", expected.getLastTerm(), actual.getLastTerm());
1175         assertEquals("Snapshot getLastIndex", expected.getLastIndex(), actual.getLastIndex());
1176         assertEquals("Snapshot state type", ShardSnapshotState.class, actual.getState().getClass());
1177         MetadataShardDataTreeSnapshot shardSnapshot =
1178                 (MetadataShardDataTreeSnapshot) ((ShardSnapshotState)actual.getState()).getSnapshot();
1179         assertEquals("Snapshot root node", expRoot, shardSnapshot.getRootNode().get());
1180     }
1181
1182     private static void sendDatastoreContextUpdate(final AbstractDataStore dataStore, final Builder builder) {
1183         final Builder newBuilder = DatastoreContext.newBuilderFrom(builder.build());
1184         final DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
1185         final Answer<DatastoreContext> answer = invocation -> newBuilder.build();
1186         Mockito.doAnswer(answer).when(mockContextFactory).getBaseDatastoreContext();
1187         Mockito.doAnswer(answer).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
1188         dataStore.onDatastoreContextUpdated(mockContextFactory);
1189     }
1190 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.