2 * Copyright (c) 2015, 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.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;
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.Stopwatch;
29 import com.google.common.base.Supplier;
30 import com.google.common.base.Throwables;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.util.concurrent.ListenableFuture;
33 import com.google.common.util.concurrent.MoreExecutors;
34 import com.google.common.util.concurrent.Uninterruptibles;
35 import com.typesafe.config.ConfigFactory;
36 import java.math.BigInteger;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Optional;
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;
110 * End-to-end distributed data store tests that exercise remote shards and transactions.
112 * @author Thomas Pantelis
114 @RunWith(Parameterized.class)
115 public class DistributedDataStoreRemotingIntegrationTest extends AbstractTest {
117 @Parameters(name = "{0}")
118 public static Collection<Object[]> data() {
119 return Arrays.asList(new Object[][] {
120 { DistributedDataStore.class, 7}, { ClientBackedDataStore.class, 12 }
125 public Class<? extends AbstractDataStore> testParameter;
127 public int commitTimeout;
129 private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
130 private static final String[] CARS = {"cars"};
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");
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";
142 private ActorSystem leaderSystem;
143 private ActorSystem followerSystem;
144 private ActorSystem follower2System;
146 private final DatastoreContext.Builder leaderDatastoreContextBuilder =
147 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
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();
155 private AbstractDataStore followerDistributedDataStore;
156 private AbstractDataStore leaderDistributedDataStore;
157 private IntegrationTestKit followerTestKit;
158 private IntegrationTestKit leaderTestKit;
161 public void setUp() {
162 InMemoryJournal.clear();
163 InMemorySnapshotStore.clear();
165 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
166 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
168 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
169 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
171 follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
172 Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
176 public void tearDown() {
177 if (followerDistributedDataStore != null) {
178 leaderDistributedDataStore.close();
180 if (leaderDistributedDataStore != null) {
181 leaderDistributedDataStore.close();
184 TestKit.shutdownActorSystem(leaderSystem);
185 TestKit.shutdownActorSystem(followerSystem);
186 TestKit.shutdownActorSystem(follower2System);
188 InMemoryJournal.clear();
189 InMemorySnapshotStore.clear();
192 private void initDatastoresWithCars(final String type) throws Exception {
193 initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
196 private void initDatastoresWithCarsAndPeople(final String type) throws Exception {
197 initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
200 private void initDatastores(final String type, final String moduleShardsConfig, final String[] shards)
202 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder, commitTimeout);
204 leaderDistributedDataStore = leaderTestKit.setupAbstractDataStore(
205 testParameter, type, moduleShardsConfig, false, shards);
207 followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder, commitTimeout);
208 followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
209 testParameter, type, moduleShardsConfig, false, shards);
211 leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), shards);
213 leaderTestKit.waitForMembersUp("member-2");
214 followerTestKit.waitForMembersUp("member-1");
217 private static void verifyCars(final DOMStoreReadTransaction readTx, final MapEntryNode... entries)
219 final Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
220 assertEquals("isPresent", true, optional.isPresent());
222 final CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = ImmutableNodes.mapNodeBuilder(
223 CarsModel.CAR_QNAME);
224 for (final NormalizedNode<?, ?> entry: entries) {
225 listBuilder.withChild((MapEntryNode) entry);
228 assertEquals("Car list node", listBuilder.build(), optional.get());
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());
238 private static void verifyExists(final DOMStoreReadTransaction readTx, final YangInstanceIdentifier path)
240 final Boolean exists = readTx.exists(path).get(5, TimeUnit.SECONDS);
241 assertEquals("exists", true, exists);
245 public void testWriteTransactionWithSingleShard() throws Exception {
246 final String testName = "testWriteTransactionWithSingleShard";
247 initDatastoresWithCars(testName);
249 final String followerCarShardName = "member-2-shard-cars-" + testName;
251 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
252 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
254 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
255 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
257 final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
258 final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
259 writeTx.merge(car1Path, car1);
261 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
262 final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
263 writeTx.merge(car2Path, car2);
265 followerTestKit.doCommit(writeTx.ready());
267 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
269 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
273 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
275 writeTx.delete(car1Path);
277 followerTestKit.doCommit(writeTx.ready());
279 verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
281 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
283 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
285 // Re-instate the follower member 2 as a single-node to verify replication and recovery.
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()));
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;
303 final List<ApplyJournalEntries> entries = InMemoryJournal.get(followerCarShardName,
304 ApplyJournalEntries.class);
305 for (ApplyJournalEntries aje: entries) {
306 if (aje.getToIndex() >= leaderLastAppliedIndex.get()) {
312 assertTrue("Follower did not persist ApplyJournalEntries containing leader's lastAppliedIndex "
313 + leaderLastAppliedIndex + ". Entries persisted: " + entries, sw.elapsed(TimeUnit.SECONDS) <= 5);
315 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
318 TestKit.shutdownActorSystem(leaderSystem, Boolean.TRUE);
319 TestKit.shutdownActorSystem(followerSystem, Boolean.TRUE);
321 final ActorSystem newSystem = newActorSystem("reinstated-member2", "Member2");
323 try (AbstractDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder,
325 .setupAbstractDataStore(testParameter, testName, "module-shards-member2", true, CARS)) {
326 verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
331 public void testReadWriteTransactionWithSingleShard() throws Exception {
332 initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
334 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
335 assertNotNull("newReadWriteTransaction returned null", rwTx);
337 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
338 rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
340 final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
341 rwTx.merge(CarsModel.newCarPath("optima"), car1);
343 verifyCars(rwTx, car1);
345 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
346 final YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
347 rwTx.merge(car2Path, car2);
349 verifyExists(rwTx, car2Path);
351 followerTestKit.doCommit(rwTx.ready());
353 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
357 public void testWriteTransactionWithMultipleShards() throws Exception {
358 initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
360 final DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
361 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
363 final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
364 final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
365 writeTx.write(carsPath, carsNode);
367 final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
368 final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
369 writeTx.write(peoplePath, peopleNode);
371 followerTestKit.doCommit(writeTx.ready());
373 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
375 verifyNode(readTx, carsPath, carsNode);
376 verifyNode(readTx, peoplePath, peopleNode);
380 public void testReadWriteTransactionWithMultipleShards() throws Exception {
381 initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
383 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
384 assertNotNull("newReadWriteTransaction returned null", rwTx);
386 final YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
387 final NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
388 rwTx.write(carsPath, carsNode);
390 final YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
391 final NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
392 rwTx.write(peoplePath, peopleNode);
394 followerTestKit.doCommit(rwTx.ready());
396 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
398 verifyNode(readTx, carsPath, carsNode);
399 verifyNode(readTx, peoplePath, peopleNode);
403 public void testTransactionChainWithSingleShard() throws Exception {
404 initDatastoresWithCars("testTransactionChainWithSingleShard");
406 final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
408 // Add the top-level cars container with write-only.
410 final DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
411 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
413 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
415 final DOMStoreThreePhaseCommitCohort writeTxReady = writeTx.ready();
417 // Verify the top-level cars container with read-only.
419 verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
421 // Perform car operations with read-write.
423 final DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
425 verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
427 rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
429 final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
430 final YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
431 rwTx.write(car1Path, car1);
433 verifyExists(rwTx, car1Path);
435 verifyCars(rwTx, car1);
437 final MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
438 rwTx.merge(CarsModel.newCarPath("sportage"), car2);
440 rwTx.delete(car1Path);
442 followerTestKit.doCommit(writeTxReady);
444 followerTestKit.doCommit(rwTx.ready());
448 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
452 public void testTransactionChainWithMultipleShards() throws Exception {
453 initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
455 final DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
457 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
458 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
460 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
461 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
463 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
464 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
466 followerTestKit.doCommit(writeTx.ready());
468 final DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
470 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
471 final YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
472 readWriteTx.write(carPath, car);
474 final MapEntryNode person = PeopleModel.newPersonEntry("jack");
475 final YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
476 readWriteTx.merge(personPath, person);
478 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
479 assertEquals("isPresent", true, optional.isPresent());
480 assertEquals("Data node", car, optional.get());
482 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
483 assertEquals("isPresent", true, optional.isPresent());
484 assertEquals("Data node", person, optional.get());
486 final DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
488 writeTx = txChain.newWriteOnlyTransaction();
490 writeTx.delete(personPath);
492 final DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
494 followerTestKit.doCommit(cohort2);
495 followerTestKit.doCommit(cohort3);
499 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
500 verifyCars(readTx, car);
502 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
503 assertEquals("isPresent", false, optional.isPresent());
507 public void testChainedTransactionFailureWithSingleShard() throws Exception {
508 initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
510 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
511 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
512 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
513 MoreExecutors.directExecutor());
515 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
516 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
518 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
520 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
521 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
522 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
524 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
527 writeTx.commit().get(5, TimeUnit.SECONDS);
528 fail("Expected TransactionCommitFailedException");
529 } catch (final ExecutionException e) {
533 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
540 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
541 initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
543 final ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
544 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
545 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
546 MoreExecutors.directExecutor());
548 final TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
549 final DOMTransactionChain txChain = broker.createTransactionChain(listener);
551 final DOMDataTreeWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
553 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
555 final ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
556 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME))
557 .withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
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);
564 writeTx.commit().get(5, TimeUnit.SECONDS);
565 fail("Expected TransactionCommitFailedException");
566 } catch (final ExecutionException e) {
570 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
577 public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
578 followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
579 final String testName = "testSingleShardTransactionsWithLeaderChanges";
580 initDatastoresWithCars(testName);
582 final String followerCarShardName = "member-2-shard-cars-" + testName;
583 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class);
585 // Write top-level car container from the follower so it uses a remote Tx.
587 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
589 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
590 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
592 followerTestKit.doCommit(writeTx.ready());
594 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
596 // Switch the leader to the follower
598 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
599 .shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
601 TestKit.shutdownActorSystem(leaderSystem, true);
602 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
604 followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), CARS);
606 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
607 Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
609 final DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder()
610 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
611 IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder, commitTimeout);
613 try (AbstractDataStore ds =
614 newMember1TestKit.setupAbstractDataStore(
615 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
617 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), CARS);
619 // Write a car entry to the new leader - should switch to local Tx
621 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
623 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
624 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
625 writeTx.merge(car1Path, car1);
627 followerTestKit.doCommit(writeTx.ready());
629 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
633 @SuppressWarnings("unchecked")
635 public void testReadyLocalTransactionForwardedToLeader() throws Exception {
636 initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
637 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
639 final com.google.common.base.Optional<ActorRef> carsFollowerShard =
640 followerDistributedDataStore.getActorContext().findLocalShard("cars");
641 assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
643 final DataTree dataTree = new InMemoryDataTreeFactory().create(
644 DataTreeConfiguration.DEFAULT_OPERATIONAL, SchemaContextHelper.full());
646 // Send a tx with immediate commit.
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);
652 final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
653 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
654 modification.ready();
656 ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(tx1 , modification, true,
657 java.util.Optional.empty());
659 carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
660 Object resp = followerTestKit.expectMsgClass(Object.class);
661 if (resp instanceof akka.actor.Status.Failure) {
662 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
665 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
667 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
669 // Send another tx without immediate commit.
671 modification = dataTree.takeSnapshot().newModification();
672 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
673 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
674 modification.ready();
676 readyLocal = new ReadyLocalTransaction(tx2 , modification, false, java.util.Optional.empty());
678 carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
679 resp = followerTestKit.expectMsgClass(Object.class);
680 if (resp instanceof akka.actor.Status.Failure) {
681 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
684 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
686 final ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
687 ((ReadyTransactionReply)resp).getCohortPath());
689 final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
690 Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
691 ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
692 leaderDistributedDataStore.getActorContext(), Arrays.asList(
693 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
694 cohort.canCommit().get(5, TimeUnit.SECONDS);
695 cohort.preCommit().get(5, TimeUnit.SECONDS);
696 cohort.commit().get(5, TimeUnit.SECONDS);
698 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
701 @SuppressWarnings("unchecked")
703 public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
704 initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
705 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
707 final com.google.common.base.Optional<ActorRef> carsFollowerShard =
708 followerDistributedDataStore.getActorContext().findLocalShard("cars");
709 assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
711 carsFollowerShard.get().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
712 final DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
714 // Send a tx with immediate commit.
716 DataTreeModification modification = dataTree.takeSnapshot().newModification();
717 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
718 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
720 final MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
721 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
723 ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction(tx1,
724 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
725 Mockito.mock(ShardDataTreeTransactionParent.class), tx1, modification), true,
726 java.util.Optional.empty());
728 carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
729 Object resp = followerTestKit.expectMsgClass(Object.class);
730 if (resp instanceof akka.actor.Status.Failure) {
731 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
734 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
736 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
738 // Send another tx without immediate commit.
740 modification = dataTree.takeSnapshot().newModification();
741 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
742 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
744 forwardedReady = new ForwardedReadyTransaction(tx2,
745 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
746 Mockito.mock(ShardDataTreeTransactionParent.class), tx2, modification), false,
747 java.util.Optional.empty());
749 carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
750 resp = followerTestKit.expectMsgClass(Object.class);
751 if (resp instanceof akka.actor.Status.Failure) {
752 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
755 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
757 ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
758 ((ReadyTransactionReply)resp).getCohortPath());
760 final Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
761 Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
762 final ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
763 leaderDistributedDataStore.getActorContext(), Arrays.asList(
764 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), tx2);
765 cohort.canCommit().get(5, TimeUnit.SECONDS);
766 cohort.preCommit().get(5, TimeUnit.SECONDS);
767 cohort.commit().get(5, TimeUnit.SECONDS);
769 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
773 public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
774 //TODO remove when test passes also for ClientBackedDataStore
775 Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
776 followerDatastoreContextBuilder.shardBatchedModificationCount(2);
777 leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
778 initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
780 // Do an initial write to get the primary shard info cached.
782 final DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
783 initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
784 initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
785 followerTestKit.doCommit(initialWriteTx.ready());
787 // Wait for the commit to be replicated to the follower.
789 MemberNode.verifyRaftState(followerDistributedDataStore, "cars",
790 raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
792 MemberNode.verifyRaftState(followerDistributedDataStore, "people",
793 raftState -> assertEquals("getLastApplied", 1, raftState.getLastApplied()));
795 // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
798 final DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
799 writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
800 writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
801 final DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
802 final ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
803 writeTx1CanCommit.get(5, TimeUnit.SECONDS);
805 // Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
806 // in the leader shard.
808 final DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
809 final LinkedList<MapEntryNode> cars = new LinkedList<>();
811 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
812 writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
814 NormalizedNode<?, ?> people = PeopleModel.newPersonMapNode();
815 writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
816 final DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
818 // Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
819 // tx writes 5 cars so 2 BatchedModidifications messages will be sent initially and cached in the
820 // leader shard (with shardBatchedModificationCount set to 2). The 3rd BatchedModidifications will be
823 final DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
824 for (int i = 1; i <= 5; i++, carIndex++) {
825 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
826 writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
829 // Prepare another WO that writes to a single shard. This will send a single BatchedModidifications
832 final DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
833 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
834 writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
837 // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaciton message to the
838 // leader shard on ready.
840 final DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
841 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
842 readWriteTx.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
844 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
845 stats -> assertEquals("getReadWriteTransactionCount", 1, stats.getReadWriteTransactionCount()));
847 // Disable elections on the leader so it switches to follower.
849 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
850 .customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName())
851 .shardElectionTimeoutFactor(10));
853 leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorContext(), "cars");
855 // Submit all tx's - the messages should get queued for retry.
857 final ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
858 final DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
859 final DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
860 final DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
862 // Enable elections on the other follower so it becomes the leader, at which point the
863 // tx's should get forwarded from the previous leader to the new leader to complete the commits.
865 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
866 .customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
867 IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "cars")
868 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
869 IntegrationTestKit.findLocalShard(followerDistributedDataStore.getActorContext(), "people")
870 .tell(TimeoutNow.INSTANCE, ActorRef.noSender());
872 followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
873 followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
874 followerTestKit.doCommit(writeTx3Cohort);
875 followerTestKit.doCommit(writeTx4Cohort);
876 followerTestKit.doCommit(rwTxCohort);
878 DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
879 verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
880 verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
884 public void testLeadershipTransferOnShutdown() throws Exception {
885 //TODO remove when test passes also for ClientBackedDataStore
886 Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
887 leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
888 followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
889 final String testName = "testLeadershipTransferOnShutdown";
890 initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
892 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
893 DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(100),
895 try (AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
896 testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
898 followerTestKit.waitForMembersUp("member-3");
899 follower2TestKit.waitForMembersUp("member-1", "member-2");
901 // Create and submit a couple tx's so they're pending.
903 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
904 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
905 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
906 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
907 final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
909 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
910 stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
912 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
913 final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
914 writeTx.write(CarsModel.newCarPath("optima"), car);
915 final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
917 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
918 stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
920 // Gracefully stop the leader via a Shutdown message.
922 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
923 .shardElectionTimeoutFactor(100));
925 final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
926 final Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
927 final ActorRef leaderActor = Await.result(future, duration);
929 final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
931 // Commit the 2 transactions. They should finish and succeed.
933 followerTestKit.doCommit(cohort1);
934 followerTestKit.doCommit(cohort2);
936 // Wait for the leader actor stopped.
938 final Boolean stopped = Await.result(stopFuture, duration);
939 assertEquals("Stopped", Boolean.TRUE, stopped);
941 // Verify leadership was transferred by reading the committed data from the other nodes.
943 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
944 verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
949 public void testTransactionWithIsolatedLeader() throws Exception {
950 //TODO remove when test passes also for ClientBackedDataStore
951 Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
952 // Set the isolated leader check interval high so we can control the switch to IsolatedLeader.
953 leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(10000000);
954 final String testName = "testTransactionWithIsolatedLeader";
955 initDatastoresWithCars(testName);
957 // Tx that is submitted after the follower is stopped but before the leader transitions to IsolatedLeader.
958 final DOMStoreWriteTransaction preIsolatedLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
959 preIsolatedLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
961 // Tx that is submitted after the leader transitions to IsolatedLeader.
962 final DOMStoreWriteTransaction noShardLeaderWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
963 noShardLeaderWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
965 // Tx that is submitted after the follower is reinstated.
966 final DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
967 successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
970 followerTestKit.watch(followerDistributedDataStore.getActorContext().getShardManager());
971 followerDistributedDataStore.close();
972 followerTestKit.expectTerminated(followerDistributedDataStore.getActorContext().getShardManager());
974 // Submit the preIsolatedLeaderWriteTx so it's pending
975 final DOMStoreThreePhaseCommitCohort preIsolatedLeaderTxCohort = preIsolatedLeaderWriteTx.ready();
977 // Change the isolated leader check interval low so it changes to IsolatedLeader.
978 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
979 .shardIsolatedLeaderCheckIntervalInMillis(200));
981 MemberNode.verifyRaftState(leaderDistributedDataStore, "cars",
982 raftState -> assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState()));
985 leaderTestKit.doCommit(noShardLeaderWriteTx.ready());
986 fail("Expected NoShardLeaderException");
987 } catch (final ExecutionException e) {
988 assertEquals("getCause", NoShardLeaderException.class, Throwables.getRootCause(e).getClass());
991 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
992 .shardElectionTimeoutFactor(100));
994 final DOMStoreThreePhaseCommitCohort successTxCohort = successWriteTx.ready();
996 followerDistributedDataStore = followerTestKit.setupAbstractDataStore(
997 testParameter, testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
999 leaderTestKit.doCommit(preIsolatedLeaderTxCohort);
1000 leaderTestKit.doCommit(successTxCohort);
1004 public void testTransactionWithShardLeaderNotResponding() throws Exception {
1005 followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1006 followerDatastoreContextBuilder.shardElectionTimeoutFactor(50);
1007 initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
1009 // Do an initial read to get the primary shard info cached.
1011 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1012 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1014 // Shutdown the leader and try to create a new tx.
1016 TestKit.shutdownActorSystem(leaderSystem, true);
1018 followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
1019 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
1021 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1023 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1026 followerTestKit.doCommit(rwTx.ready());
1027 fail("Exception expected");
1028 } catch (final ExecutionException e) {
1029 final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
1030 if (DistributedDataStore.class.equals(testParameter)) {
1031 assertTrue(msg, Throwables.getRootCause(e) instanceof NoShardLeaderException
1032 || e.getCause() instanceof ShardLeaderNotRespondingException);
1034 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
1040 public void testTransactionWithCreateTxFailureDueToNoLeader() throws Exception {
1041 followerDatastoreContextBuilder.frontendRequestTimeoutInSeconds(2);
1042 initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
1044 // Do an initial read to get the primary shard info cached.
1046 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1047 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1049 // Shutdown the leader and try to create a new tx.
1051 TestKit.shutdownActorSystem(leaderSystem, true);
1053 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1055 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
1057 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1058 .operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
1060 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1062 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1065 followerTestKit.doCommit(rwTx.ready());
1066 fail("Exception expected");
1067 } catch (final ExecutionException e) {
1068 final String msg = "Unexpected exception: " + Throwables.getStackTraceAsString(e.getCause());
1069 if (DistributedDataStore.class.equals(testParameter)) {
1070 assertTrue(msg, Throwables.getRootCause(e) instanceof NoShardLeaderException);
1072 assertTrue(msg, Throwables.getRootCause(e) instanceof RequestTimeoutException);
1078 public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
1079 followerDatastoreContextBuilder.backendAlivenessTimerIntervalInSeconds(2);
1080 String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
1081 initDatastores(testName, MODULE_SHARDS_CARS_1_2_3, CARS);
1083 final DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder()
1084 .shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(10);
1085 final IntegrationTestKit follower2TestKit = new IntegrationTestKit(
1086 follower2System, follower2DatastoreContextBuilder, commitTimeout);
1088 try (AbstractDataStore ds =
1089 follower2TestKit.setupAbstractDataStore(
1090 testParameter, testName, MODULE_SHARDS_CARS_1_2_3, false, CARS)) {
1092 followerTestKit.waitForMembersUp("member-1", "member-3");
1093 follower2TestKit.waitForMembersUp("member-1", "member-2");
1095 // Do an initial read to get the primary shard info cached.
1097 final DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
1098 readTx.read(CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1100 // Shutdown the leader and try to create a new tx.
1102 TestKit.shutdownActorSystem(leaderSystem, true);
1104 Cluster.get(followerSystem).leave(MEMBER_1_ADDRESS);
1106 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder
1107 .operationTimeoutInMillis(500).shardElectionTimeoutFactor(5).customRaftPolicyImplementation(null));
1109 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1111 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
1113 followerTestKit.doCommit(rwTx.ready());
1118 public void testInstallSnapshot() throws Exception {
1119 final String testName = "testInstallSnapshot";
1120 final String leaderCarShardName = "member-1-shard-cars-" + testName;
1121 final String followerCarShardName = "member-2-shard-cars-" + testName;
1123 // Setup a saved snapshot on the leader. The follower will startup with no data and the leader should
1124 // install a snapshot to sync the follower.
1126 DataTree tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
1127 SchemaContextHelper.full());
1129 final ContainerNode carsNode = CarsModel.newCarsNode(
1130 CarsModel.newCarsMapNode(CarsModel.newCarEntry("optima", BigInteger.valueOf(20000))));
1131 AbstractShardTest.writeToStore(tree, CarsModel.BASE_PATH, carsNode);
1133 final NormalizedNode<?, ?> snapshotRoot = AbstractShardTest.readStore(tree, YangInstanceIdentifier.EMPTY);
1134 final Snapshot initialSnapshot = Snapshot.create(
1135 new ShardSnapshotState(new MetadataShardDataTreeSnapshot(snapshotRoot)),
1136 Collections.emptyList(), 5, 1, 5, 1, 1, null, null);
1137 InMemorySnapshotStore.addSnapshot(leaderCarShardName, initialSnapshot);
1139 InMemorySnapshotStore.addSnapshotSavedLatch(leaderCarShardName);
1140 InMemorySnapshotStore.addSnapshotSavedLatch(followerCarShardName);
1142 initDatastoresWithCars(testName);
1144 final Optional<NormalizedNode<?, ?>> readOptional = leaderDistributedDataStore.newReadOnlyTransaction().read(
1145 CarsModel.BASE_PATH).get(5, TimeUnit.SECONDS);
1146 assertEquals("isPresent", true, readOptional.isPresent());
1147 assertEquals("Node", carsNode, readOptional.get());
1149 verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(leaderCarShardName, Snapshot.class),
1150 initialSnapshot, snapshotRoot);
1152 verifySnapshot(InMemorySnapshotStore.waitForSavedSnapshot(followerCarShardName, Snapshot.class),
1153 initialSnapshot, snapshotRoot);
1157 public void testReadWriteMessageSlicing() throws Exception {
1158 // The slicing is only implemented for tell-based protocol
1159 Assume.assumeTrue(testParameter.equals(ClientBackedDataStore.class));
1161 leaderDatastoreContextBuilder.maximumMessageSliceSize(100);
1162 followerDatastoreContextBuilder.maximumMessageSliceSize(100);
1163 initDatastoresWithCars("testLargeReadReplySlicing");
1165 final DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
1167 final NormalizedNode<?, ?> carsNode = CarsModel.create();
1168 rwTx.write(CarsModel.BASE_PATH, carsNode);
1170 verifyNode(rwTx, CarsModel.BASE_PATH, carsNode);
1173 private static void verifySnapshot(final Snapshot actual, final Snapshot expected,
1174 final NormalizedNode<?, ?> expRoot) {
1175 assertEquals("Snapshot getLastAppliedTerm", expected.getLastAppliedTerm(), actual.getLastAppliedTerm());
1176 assertEquals("Snapshot getLastAppliedIndex", expected.getLastAppliedIndex(), actual.getLastAppliedIndex());
1177 assertEquals("Snapshot getLastTerm", expected.getLastTerm(), actual.getLastTerm());
1178 assertEquals("Snapshot getLastIndex", expected.getLastIndex(), actual.getLastIndex());
1179 assertEquals("Snapshot state type", ShardSnapshotState.class, actual.getState().getClass());
1180 MetadataShardDataTreeSnapshot shardSnapshot =
1181 (MetadataShardDataTreeSnapshot) ((ShardSnapshotState)actual.getState()).getSnapshot();
1182 assertEquals("Snapshot root node", expRoot, shardSnapshot.getRootNode().get());
1185 private static void sendDatastoreContextUpdate(final AbstractDataStore dataStore, final Builder builder) {
1186 final Builder newBuilder = DatastoreContext.newBuilderFrom(builder.build());
1187 final DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
1188 final Answer<DatastoreContext> answer = invocation -> newBuilder.build();
1189 Mockito.doAnswer(answer).when(mockContextFactory).getBaseDatastoreContext();
1190 Mockito.doAnswer(answer).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
1191 dataStore.onDatastoreContextUpdated(mockContextFactory);