2 * Copyright (c) 2015 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;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSelection;
20 import akka.actor.ActorSystem;
21 import akka.actor.Address;
22 import akka.actor.AddressFromURIString;
23 import akka.cluster.Cluster;
24 import akka.dispatch.Futures;
25 import akka.pattern.AskTimeoutException;
26 import akka.pattern.Patterns;
27 import akka.testkit.JavaTestKit;
28 import com.google.common.base.Optional;
29 import com.google.common.base.Supplier;
30 import com.google.common.collect.ImmutableMap;
31 import com.google.common.util.concurrent.ListenableFuture;
32 import com.google.common.util.concurrent.MoreExecutors;
33 import com.google.common.util.concurrent.Uninterruptibles;
34 import com.typesafe.config.ConfigFactory;
35 import java.math.BigInteger;
36 import java.util.Arrays;
37 import java.util.LinkedList;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.TimeUnit;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.mockito.invocation.InvocationOnMock;
45 import org.mockito.stubbing.Answer;
46 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
47 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
48 import org.opendaylight.controller.cluster.datastore.IntegrationTestKit.ShardStatsVerifier;
49 import org.opendaylight.controller.cluster.datastore.MemberNode.RaftStateVerifier;
50 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
51 import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
52 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
53 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
54 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
55 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
56 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
57 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
58 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
59 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
60 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
61 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
62 import org.opendaylight.controller.cluster.raft.client.messages.Shutdown;
63 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
64 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
65 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
66 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
67 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
68 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
69 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
70 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
71 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
72 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
73 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
74 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
75 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
76 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
77 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
78 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
79 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
80 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
81 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
83 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
84 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
85 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
86 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
87 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
88 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
89 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
90 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
91 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
92 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
93 import scala.concurrent.Await;
94 import scala.concurrent.Future;
95 import scala.concurrent.duration.FiniteDuration;
98 * End-to-end distributed data store tests that exercise remote shards and transactions.
100 * @author Thomas Pantelis
102 public class DistributedDataStoreRemotingIntegrationTest {
104 private static final String[] CARS_AND_PEOPLE = {"cars", "people"};
105 private static final String[] CARS = {"cars"};
107 private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
108 private static final Address MEMBER_2_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2559");
110 private static final String MODULE_SHARDS_CARS_ONLY_1_2 = "module-shards-cars-member-1-and-2.conf";
111 private static final String MODULE_SHARDS_CARS_PEOPLE_1_2 = "module-shards-member1-and-2.conf";
112 private static final String MODULE_SHARDS_CARS_PEOPLE_1_2_3 = "module-shards-member1-and-2-and-3.conf";
114 private ActorSystem leaderSystem;
115 private ActorSystem followerSystem;
116 private ActorSystem follower2System;
118 private final DatastoreContext.Builder leaderDatastoreContextBuilder =
119 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(2);
121 private final DatastoreContext.Builder followerDatastoreContextBuilder =
122 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5).
123 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName());
125 private DistributedDataStore followerDistributedDataStore;
126 private DistributedDataStore leaderDistributedDataStore;
127 private IntegrationTestKit followerTestKit;
128 private IntegrationTestKit leaderTestKit;
131 public void setUp() {
132 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
133 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
135 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
136 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
138 follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
139 Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
143 public void tearDown() {
144 if (followerDistributedDataStore != null) {
145 leaderDistributedDataStore.close();
147 if (leaderDistributedDataStore != null) {
148 leaderDistributedDataStore.close();
151 JavaTestKit.shutdownActorSystem(leaderSystem);
152 JavaTestKit.shutdownActorSystem(followerSystem);
153 JavaTestKit.shutdownActorSystem(follower2System);
156 private void initDatastoresWithCars(String type) {
157 initDatastores(type, MODULE_SHARDS_CARS_ONLY_1_2, CARS);
160 private void initDatastoresWithCarsAndPeople(String type) {
161 initDatastores(type, MODULE_SHARDS_CARS_PEOPLE_1_2, CARS_AND_PEOPLE);
164 private void initDatastores(String type, String moduleShardsConfig, String[] shards) {
165 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
167 leaderDistributedDataStore = leaderTestKit.setupDistributedDataStore(type, moduleShardsConfig, false, shards);
169 followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder);
170 followerDistributedDataStore = followerTestKit.setupDistributedDataStore(type, moduleShardsConfig, false, shards);
172 leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), shards);
175 private static void verifyCars(DOMStoreReadTransaction readTx, MapEntryNode... entries) throws Exception {
176 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
177 assertEquals("isPresent", true, optional.isPresent());
179 CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = ImmutableNodes.mapNodeBuilder(CarsModel.CAR_QNAME);
180 for(NormalizedNode<?, ?> entry: entries) {
181 listBuilder.withChild((MapEntryNode) entry);
184 assertEquals("Car list node", listBuilder.build(), optional.get());
187 private static void verifyNode(DOMStoreReadTransaction readTx, YangInstanceIdentifier path, NormalizedNode<?, ?> expNode)
189 Optional<NormalizedNode<?, ?>> optional = readTx.read(path).get(5, TimeUnit.SECONDS);
190 assertEquals("isPresent", true, optional.isPresent());
191 assertEquals("Data node", expNode, optional.get());
194 private static void verifyExists(DOMStoreReadTransaction readTx, YangInstanceIdentifier path) throws Exception {
195 Boolean exists = readTx.exists(path).get(5, TimeUnit.SECONDS);
196 assertEquals("exists", true, exists);
200 public void testWriteTransactionWithSingleShard() throws Exception {
201 String testName = "testWriteTransactionWithSingleShard";
202 initDatastoresWithCars(testName);
204 String followerCarShardName = "member-2-shard-cars-" + testName;
205 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 2, ApplyJournalEntries.class );
207 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
208 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
210 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
211 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
213 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
214 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
215 writeTx.merge(car1Path, car1);
217 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
218 YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
219 writeTx.merge(car2Path, car2);
221 followerTestKit.doCommit(writeTx.ready());
223 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
225 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
229 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
231 writeTx.delete(car1Path);
233 followerTestKit.doCommit(writeTx.ready());
235 verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
237 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
239 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
241 // Re-instate the follower member 2 as a single-node to verify replication and recovery.
243 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
245 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
246 JavaTestKit.shutdownActorSystem(followerSystem, null, true);
248 ActorSystem newSystem = ActorSystem.create("reinstated-member2", ConfigFactory.load().getConfig("Member2"));
250 try (DistributedDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder).
251 setupDistributedDataStore(testName, "module-shards-member2", true, CARS_AND_PEOPLE)) {
252 verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
255 JavaTestKit.shutdownActorSystem(newSystem);
259 public void testReadWriteTransactionWithSingleShard() throws Exception {
260 initDatastoresWithCars("testReadWriteTransactionWithSingleShard");
262 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
263 assertNotNull("newReadWriteTransaction returned null", rwTx);
265 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
266 rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
268 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
269 rwTx.merge(CarsModel.newCarPath("optima"), car1);
271 verifyCars(rwTx, car1);
273 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
274 YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
275 rwTx.merge(car2Path, car2);
277 verifyExists(rwTx, car2Path);
279 followerTestKit.doCommit(rwTx.ready());
281 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
285 public void testWriteTransactionWithMultipleShards() throws Exception {
286 initDatastoresWithCarsAndPeople("testWriteTransactionWithMultipleShards");
288 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
289 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
291 YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
292 NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
293 writeTx.write(carsPath, carsNode);
295 YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
296 NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
297 writeTx.write(peoplePath, peopleNode);
299 followerTestKit.doCommit(writeTx.ready());
301 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
303 verifyNode(readTx, carsPath, carsNode);
304 verifyNode(readTx, peoplePath, peopleNode);
308 public void testReadWriteTransactionWithMultipleShards() throws Exception {
309 initDatastoresWithCarsAndPeople("testReadWriteTransactionWithMultipleShards");
311 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
312 assertNotNull("newReadWriteTransaction returned null", rwTx);
314 YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
315 NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
316 rwTx.write(carsPath, carsNode);
318 YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
319 NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
320 rwTx.write(peoplePath, peopleNode);
322 followerTestKit.doCommit(rwTx.ready());
324 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
326 verifyNode(readTx, carsPath, carsNode);
327 verifyNode(readTx, peoplePath, peopleNode);
331 public void testTransactionChainWithSingleShard() throws Exception {
332 initDatastoresWithCars("testTransactionChainWithSingleShard");
334 DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
336 // Add the top-level cars container with write-only.
338 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
339 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
341 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
345 // Verify the top-level cars container with read-only.
347 verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
349 // Perform car operations with read-write.
351 DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
353 verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
355 rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
357 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
358 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
359 rwTx.write(car1Path, car1);
361 verifyExists(rwTx, car1Path);
363 verifyCars(rwTx, car1);
365 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
366 rwTx.merge(CarsModel.newCarPath("sportage"), car2);
368 rwTx.delete(car1Path);
370 followerTestKit.doCommit(rwTx.ready());
374 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
378 public void testTransactionChainWithMultipleShards() throws Exception{
379 initDatastoresWithCarsAndPeople("testTransactionChainWithMultipleShards");
381 DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
383 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
384 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
386 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
387 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
389 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
390 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
392 followerTestKit.doCommit(writeTx.ready());
394 DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
396 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
397 YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
398 readWriteTx.write(carPath, car);
400 MapEntryNode person = PeopleModel.newPersonEntry("jack");
401 YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
402 readWriteTx.merge(personPath, person);
404 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
405 assertEquals("isPresent", true, optional.isPresent());
406 assertEquals("Data node", car, optional.get());
408 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
409 assertEquals("isPresent", true, optional.isPresent());
410 assertEquals("Data node", person, optional.get());
412 DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
414 writeTx = txChain.newWriteOnlyTransaction();
416 writeTx.delete(personPath);
418 DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
420 followerTestKit.doCommit(cohort2);
421 followerTestKit.doCommit(cohort3);
425 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
426 verifyCars(readTx, car);
428 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
429 assertEquals("isPresent", false, optional.isPresent());
433 public void testChainedTransactionFailureWithSingleShard() throws Exception {
434 initDatastoresWithCars("testChainedTransactionFailureWithSingleShard");
436 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
437 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
438 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
439 MoreExecutors.directExecutor());
441 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
442 DOMTransactionChain txChain = broker.createTransactionChain(listener);
444 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
446 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
447 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME)).
448 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
450 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
453 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
454 fail("Expected TransactionCommitFailedException");
455 } catch (TransactionCommitFailedException e) {
459 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
466 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
467 initDatastoresWithCarsAndPeople("testChainedTransactionFailureWithMultipleShards");
469 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
470 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
471 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
472 MoreExecutors.directExecutor());
474 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
475 DOMTransactionChain txChain = broker.createTransactionChain(listener);
477 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
479 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
481 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
482 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME)).
483 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
485 // Note that merge will validate the data and fail but put succeeds b/c deep validation is not
486 // done for put for performance reasons.
487 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
490 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
491 fail("Expected TransactionCommitFailedException");
492 } catch (TransactionCommitFailedException e) {
496 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
503 public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
504 String testName = "testSingleShardTransactionsWithLeaderChanges";
505 initDatastoresWithCars(testName);
507 String followerCarShardName = "member-2-shard-cars-" + testName;
508 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class );
510 // Write top-level car container from the follower so it uses a remote Tx.
512 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
514 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
515 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
517 followerTestKit.doCommit(writeTx.ready());
519 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
521 // Switch the leader to the follower
523 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.
524 shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
526 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
528 followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), CARS);
530 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
531 Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
533 DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder().
534 shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
535 IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder);
537 try (DistributedDataStore ds =
538 newMember1TestKit.setupDistributedDataStore(testName, MODULE_SHARDS_CARS_ONLY_1_2, false, CARS)) {
540 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), CARS);
542 // Write a car entry to the new leader - should switch to local Tx
544 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
546 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
547 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
548 writeTx.merge(car1Path, car1);
550 followerTestKit.doCommit(writeTx.ready());
552 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
556 @SuppressWarnings("unchecked")
558 public void testReadyLocalTransactionForwardedToLeader() throws Exception {
559 initDatastoresWithCars("testReadyLocalTransactionForwardedToLeader");
560 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
562 Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars");
563 assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
565 TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
566 dataTree.setSchemaContext(SchemaContextHelper.full());
568 // Send a tx with immediate commit.
570 DataTreeModification modification = dataTree.takeSnapshot().newModification();
571 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
572 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
574 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
575 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
576 modification.ready();
578 ReadyLocalTransaction readyLocal = new ReadyLocalTransaction("tx-1" , modification, true);
580 carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
581 Object resp = followerTestKit.expectMsgClass(Object.class);
582 if(resp instanceof akka.actor.Status.Failure) {
583 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
586 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
588 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
590 // Send another tx without immediate commit.
592 modification = dataTree.takeSnapshot().newModification();
593 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
594 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
595 modification.ready();
597 readyLocal = new ReadyLocalTransaction("tx-2" , modification, false);
599 carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
600 resp = followerTestKit.expectMsgClass(Object.class);
601 if(resp instanceof akka.actor.Status.Failure) {
602 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
605 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
607 ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
608 ((ReadyTransactionReply)resp).getCohortPath());
610 Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
611 Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
612 ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
613 leaderDistributedDataStore.getActorContext(), Arrays.asList(
614 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), "tx-2");
615 cohort.canCommit().get(5, TimeUnit.SECONDS);
616 cohort.preCommit().get(5, TimeUnit.SECONDS);
617 cohort.commit().get(5, TimeUnit.SECONDS);
619 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
622 @SuppressWarnings("unchecked")
624 public void testForwardedReadyTransactionForwardedToLeader() throws Exception {
625 initDatastoresWithCars("testForwardedReadyTransactionForwardedToLeader");
626 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
628 Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars");
629 assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
631 carsFollowerShard.get().tell(GetShardDataTree.INSTANCE, followerTestKit.getRef());
632 DataTree dataTree = followerTestKit.expectMsgClass(DataTree.class);
634 // Send a tx with immediate commit.
636 DataTreeModification modification = dataTree.takeSnapshot().newModification();
637 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
638 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
640 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
641 new WriteModification(CarsModel.newCarPath("optima"), car1).apply(modification);
643 ForwardedReadyTransaction forwardedReady = new ForwardedReadyTransaction("tx-1",
644 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
645 Mockito.mock(ShardDataTreeTransactionParent.class), "tx-1", modification), true);
647 carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
648 Object resp = followerTestKit.expectMsgClass(Object.class);
649 if(resp instanceof akka.actor.Status.Failure) {
650 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
653 assertEquals("Response type", CommitTransactionReply.class, resp.getClass());
655 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1);
657 // Send another tx without immediate commit.
659 modification = dataTree.takeSnapshot().newModification();
660 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(30000));
661 new WriteModification(CarsModel.newCarPath("sportage"), car2).apply(modification);
663 forwardedReady = new ForwardedReadyTransaction("tx-2",
664 DataStoreVersions.CURRENT_VERSION, new ReadWriteShardDataTreeTransaction(
665 Mockito.mock(ShardDataTreeTransactionParent.class), "tx-2", modification), false);
667 carsFollowerShard.get().tell(forwardedReady, followerTestKit.getRef());
668 resp = followerTestKit.expectMsgClass(Object.class);
669 if(resp instanceof akka.actor.Status.Failure) {
670 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
673 assertEquals("Response type", ReadyTransactionReply.class, resp.getClass());
675 ActorSelection txActor = leaderDistributedDataStore.getActorContext().actorSelection(
676 ((ReadyTransactionReply)resp).getCohortPath());
678 Supplier<Short> versionSupplier = Mockito.mock(Supplier.class);
679 Mockito.doReturn(DataStoreVersions.CURRENT_VERSION).when(versionSupplier).get();
680 ThreePhaseCommitCohortProxy cohort = new ThreePhaseCommitCohortProxy(
681 leaderDistributedDataStore.getActorContext(), Arrays.asList(
682 new ThreePhaseCommitCohortProxy.CohortInfo(Futures.successful(txActor), versionSupplier)), "tx-2");
683 cohort.canCommit().get(5, TimeUnit.SECONDS);
684 cohort.preCommit().get(5, TimeUnit.SECONDS);
685 cohort.commit().get(5, TimeUnit.SECONDS);
687 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
691 public void testTransactionForwardedToLeaderAfterRetry() throws Exception {
692 followerDatastoreContextBuilder.shardBatchedModificationCount(2);
693 leaderDatastoreContextBuilder.shardBatchedModificationCount(2);
694 initDatastoresWithCarsAndPeople("testTransactionForwardedToLeaderAfterRetry");
696 // Do an initial write to get the primary shard info cached.
698 DOMStoreWriteTransaction initialWriteTx = followerDistributedDataStore.newWriteOnlyTransaction();
699 initialWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
700 initialWriteTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
701 followerTestKit.doCommit(initialWriteTx.ready());
703 // Wait for the commit to be replicated to the follower.
705 MemberNode.verifyRaftState(followerDistributedDataStore, "cars", new RaftStateVerifier() {
707 public void verify(OnDemandRaftState raftState) {
708 assertEquals("getLastApplied", 0, raftState.getLastApplied());
712 // Prepare, ready and canCommit a WO tx that writes to 2 shards. This will become the current tx in
715 DOMStoreWriteTransaction writeTx1 = followerDistributedDataStore.newWriteOnlyTransaction();
716 writeTx1.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
717 writeTx1.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
718 DOMStoreThreePhaseCommitCohort writeTx1Cohort = writeTx1.ready();
719 ListenableFuture<Boolean> writeTx1CanCommit = writeTx1Cohort.canCommit();
720 writeTx1CanCommit.get(5, TimeUnit.SECONDS);
722 // Prepare and ready another WO tx that writes to 2 shards but don't canCommit yet. This will be queued
723 // in the leader shard.
725 DOMStoreWriteTransaction writeTx2 = followerDistributedDataStore.newWriteOnlyTransaction();
726 LinkedList<MapEntryNode> cars = new LinkedList<>();
728 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
729 writeTx2.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
731 NormalizedNode<?, ?> people = PeopleModel.newPersonMapNode();
732 writeTx2.write(PeopleModel.PERSON_LIST_PATH, people);
733 DOMStoreThreePhaseCommitCohort writeTx2Cohort = writeTx2.ready();
735 // Prepare another WO that writes to a single shard and thus will be directly committed on ready. This
736 // tx writes 5 cars so 2 BatchedModidifications messages will be sent initially and cached in the
737 // leader shard (with shardBatchedModificationCount set to 2). The 3rd BatchedModidifications will be
740 DOMStoreWriteTransaction writeTx3 = followerDistributedDataStore.newWriteOnlyTransaction();
741 for(int i = 1; i <= 5; i++, carIndex++) {
742 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
743 writeTx3.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
746 // Prepare another WO that writes to a single shard. This will send a single BatchedModidifications
749 DOMStoreWriteTransaction writeTx4 = followerDistributedDataStore.newWriteOnlyTransaction();
750 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
751 writeTx4.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
754 // Prepare a RW tx that will create a tx actor and send a ForwardedReadyTransaciton message to the
755 // leader shard on ready.
757 DOMStoreReadWriteTransaction readWriteTx = followerDistributedDataStore.newReadWriteTransaction();
758 cars.add(CarsModel.newCarEntry("car" + carIndex, BigInteger.valueOf(carIndex)));
759 readWriteTx.write(CarsModel.newCarPath("car" + carIndex), cars.getLast());
761 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", new ShardStatsVerifier() {
763 public void verify(ShardStats stats) {
764 assertEquals("getReadWriteTransactionCount", 1, stats.getReadWriteTransactionCount());
768 // Disable elections on the leader so it switches to follower.
770 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.
771 customRaftPolicyImplementation(DisableElectionsRaftPolicy.class.getName()).
772 shardElectionTimeoutFactor(10));
774 leaderTestKit.waitUntilNoLeader(leaderDistributedDataStore.getActorContext(), "cars");
776 // Submit all tx's - the messages should get queued for retry.
778 ListenableFuture<Boolean> writeTx2CanCommit = writeTx2Cohort.canCommit();
779 DOMStoreThreePhaseCommitCohort writeTx3Cohort = writeTx3.ready();
780 DOMStoreThreePhaseCommitCohort writeTx4Cohort = writeTx4.ready();
781 DOMStoreThreePhaseCommitCohort rwTxCohort = readWriteTx.ready();
783 // Enable elections on the other follower so it becomes the leader, at which point the
784 // tx's should get forwarded from the previous leader to the new leader to complete the commits.
786 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.
787 customRaftPolicyImplementation(null).shardElectionTimeoutFactor(1));
789 followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
790 followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
791 followerTestKit.doCommit(writeTx3Cohort);
792 followerTestKit.doCommit(writeTx4Cohort);
793 followerTestKit.doCommit(rwTxCohort);
795 DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
796 verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
797 verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
801 public void testLeadershipTransferOnShutdown() throws Exception {
802 leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
803 followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
804 String testName = "testLeadershipTransferOnShutdown";
805 initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
807 IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, followerDatastoreContextBuilder);
808 try (DistributedDataStore follower2DistributedDataStore = follower2TestKit.setupDistributedDataStore(testName,
809 MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
811 // Create and submit a couple tx's so they're pending.
813 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
814 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
815 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
816 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
817 DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
819 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", new ShardStatsVerifier() {
821 public void verify(ShardStats stats) {
822 assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize());
826 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
827 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
828 writeTx.write(CarsModel.newCarPath("optima"), car);
829 DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
831 IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", new ShardStatsVerifier() {
833 public void verify(ShardStats stats) {
834 assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize());
838 // Gracefully stop the leader via a Shutdown message.
840 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.
841 shardElectionTimeoutFactor(100));
843 FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
844 Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
845 ActorRef leaderActor = Await.result(future, duration);
847 Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
849 // Commit the 2 transactions. They should finish and succeed.
851 followerTestKit.doCommit(cohort1);
852 followerTestKit.doCommit(cohort2);
854 // Wait for the leader actor stopped.
856 Boolean stopped = Await.result(stopFuture, duration);
857 assertEquals("Stopped", Boolean.TRUE, stopped);
859 // Verify leadership was transferred by reading the committed data from the other nodes.
861 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
862 verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
867 public void testTransactionWithIsolatedLeader() throws Throwable {
868 leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(200);
869 String testName = "testTransactionWithIsolatedLeader";
870 initDatastoresWithCars(testName);
872 DOMStoreWriteTransaction failWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
873 failWriteTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
875 DOMStoreWriteTransaction successWriteTx = leaderDistributedDataStore.newWriteOnlyTransaction();
876 successWriteTx.merge(CarsModel.BASE_PATH, CarsModel.emptyContainer());
878 followerTestKit.watch(followerDistributedDataStore.getActorContext().getShardManager());
879 followerDistributedDataStore.close();
880 followerTestKit.expectTerminated(followerDistributedDataStore.getActorContext().getShardManager());
882 MemberNode.verifyRaftState(leaderDistributedDataStore, "cars", new RaftStateVerifier() {
884 public void verify(OnDemandRaftState raftState) {
885 assertEquals("getRaftState", "IsolatedLeader", raftState.getRaftState());
890 leaderTestKit.doCommit(failWriteTx.ready());
891 fail("Expected NoShardLeaderException");
892 } catch (ExecutionException e) {
893 assertEquals("getCause", NoShardLeaderException.class, e.getCause().getClass());
896 sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder.
897 shardElectionTimeoutFactor(100));
899 DOMStoreThreePhaseCommitCohort writeTxCohort = successWriteTx.ready();
901 followerDistributedDataStore = followerTestKit.setupDistributedDataStore(testName,
902 MODULE_SHARDS_CARS_ONLY_1_2, false, CARS);
904 leaderTestKit.doCommit(writeTxCohort);
907 @Test(expected=AskTimeoutException.class)
908 public void testTransactionWithShardLeaderNotResponding() throws Throwable {
909 initDatastoresWithCars("testTransactionWithShardLeaderNotResponding");
911 // Do an initial read to get the primary shard info cached.
913 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
914 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
916 // Shutdown the leader and try to create a new tx.
918 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
920 followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
921 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder);
923 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
925 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
928 followerTestKit.doCommit(rwTx.ready());
929 } catch (ExecutionException e) {
930 assertTrue("Expected ShardLeaderNotRespondingException cause. Actual: " + e.getCause(),
931 e.getCause() instanceof ShardLeaderNotRespondingException);
932 assertNotNull("Expected a nested cause", e.getCause().getCause());
933 throw e.getCause().getCause();
937 @Test(expected=NoShardLeaderException.class)
938 public void testTransactionWithCreateTxFailureDueToNoLeader() throws Throwable {
939 initDatastoresWithCars("testTransactionWithCreateTxFailureDueToNoLeader");
941 // Do an initial read to get the primary shard info cached.
943 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
944 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
946 // Shutdown the leader and try to create a new tx.
948 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
950 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
952 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.
953 operationTimeoutInMillis(10).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
955 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
957 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
960 followerTestKit.doCommit(rwTx.ready());
961 } catch (ExecutionException e) {
967 public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
968 String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
969 initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS);
971 DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder().
972 shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
973 IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, follower2DatastoreContextBuilder);
975 try (DistributedDataStore ds =
976 follower2TestKit.setupDistributedDataStore(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false, CARS)) {
978 followerTestKit.waitForMembersUp("member-1", "member-3");
979 follower2TestKit.waitForMembersUp("member-1", "member-2");
981 // Do an initial read to get the primary shard info cached.
983 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
984 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
986 // Shutdown the leader and try to create a new tx.
988 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
990 sendDatastoreContextUpdate(followerDistributedDataStore, followerDatastoreContextBuilder.
991 operationTimeoutInMillis(500).shardElectionTimeoutFactor(1).customRaftPolicyImplementation(null));
993 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
995 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
997 followerTestKit.doCommit(rwTx.ready());
1001 private static void sendDatastoreContextUpdate(DistributedDataStore dataStore, final Builder builder) {
1002 final Builder newBuilder = DatastoreContext.newBuilderFrom(builder.build());
1003 DatastoreContextFactory mockContextFactory = Mockito.mock(DatastoreContextFactory.class);
1004 Answer<DatastoreContext> answer = new Answer<DatastoreContext>() {
1006 public DatastoreContext answer(InvocationOnMock invocation) {
1007 return newBuilder.build();
1010 Mockito.doAnswer(answer).when(mockContextFactory).getBaseDatastoreContext();
1011 Mockito.doAnswer(answer).when(mockContextFactory).getShardDatastoreContext(Mockito.anyString());
1012 dataStore.onDatastoreContextUpdated(mockContextFactory);