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.ActorSystem;
20 import akka.actor.Address;
21 import akka.actor.AddressFromURIString;
22 import akka.cluster.Cluster;
23 import akka.pattern.AskTimeoutException;
24 import akka.testkit.JavaTestKit;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.common.util.concurrent.MoreExecutors;
28 import com.google.common.util.concurrent.Uninterruptibles;
29 import com.typesafe.config.ConfigFactory;
30 import java.math.BigInteger;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
38 import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
39 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
40 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
41 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
42 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
43 import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries;
44 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
45 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
46 import org.opendaylight.controller.md.cluster.datastore.model.PeopleModel;
47 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
48 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
49 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
50 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
51 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
52 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
53 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
54 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
55 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
56 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
57 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
58 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
59 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
66 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
67 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
68 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
70 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
73 * End-to-end distributed data store tests that exercise remote shards and transactions.
75 * @author Thomas Pantelis
77 public class DistributedDataStoreRemotingIntegrationTest {
79 private static final String[] SHARD_NAMES = {"cars", "people"};
81 private static final Address MEMBER_1_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2558");
82 private static final Address MEMBER_2_ADDRESS = AddressFromURIString.parse("akka.tcp://cluster-test@127.0.0.1:2559");
84 private static final String MODULE_SHARDS_CONFIG_2 = "module-shards-member1-and-2.conf";
85 private static final String MODULE_SHARDS_CONFIG_3 = "module-shards-member1-and-2-and-3.conf";
87 private ActorSystem leaderSystem;
88 private ActorSystem followerSystem;
89 private ActorSystem follower2System;
91 private final DatastoreContext.Builder leaderDatastoreContextBuilder =
92 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(1);
94 private final DatastoreContext.Builder followerDatastoreContextBuilder =
95 DatastoreContext.newBuilder().shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
97 private DistributedDataStore followerDistributedDataStore;
98 private DistributedDataStore leaderDistributedDataStore;
99 private IntegrationTestKit followerTestKit;
100 private IntegrationTestKit leaderTestKit;
103 public void setUp() {
104 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
105 Cluster.get(leaderSystem).join(MEMBER_1_ADDRESS);
107 followerSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member2"));
108 Cluster.get(followerSystem).join(MEMBER_1_ADDRESS);
110 follower2System = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member3"));
111 Cluster.get(follower2System).join(MEMBER_1_ADDRESS);
115 public void tearDown() {
116 JavaTestKit.shutdownActorSystem(leaderSystem);
117 JavaTestKit.shutdownActorSystem(followerSystem);
118 JavaTestKit.shutdownActorSystem(follower2System);
121 private void initDatastores(String type) {
122 initDatastores(type, MODULE_SHARDS_CONFIG_2);
125 private void initDatastores(String type, String moduleShardsConfig) {
126 leaderTestKit = new IntegrationTestKit(leaderSystem, leaderDatastoreContextBuilder);
128 leaderDistributedDataStore = leaderTestKit.setupDistributedDataStore(type, moduleShardsConfig, false, SHARD_NAMES);
130 followerTestKit = new IntegrationTestKit(followerSystem, followerDatastoreContextBuilder);
131 followerDistributedDataStore = followerTestKit.setupDistributedDataStore(type, moduleShardsConfig, false, SHARD_NAMES);
133 leaderTestKit.waitUntilLeader(leaderDistributedDataStore.getActorContext(), SHARD_NAMES);
136 private void verifyCars(DOMStoreReadTransaction readTx, MapEntryNode... entries) throws Exception {
137 Optional<NormalizedNode<?, ?>> optional = readTx.read(CarsModel.CAR_LIST_PATH).get(5, TimeUnit.SECONDS);
138 assertEquals("isPresent", true, optional.isPresent());
140 CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder = ImmutableNodes.mapNodeBuilder(CarsModel.CAR_QNAME);
141 for(NormalizedNode<?, ?> entry: entries) {
142 listBuilder.withChild((MapEntryNode) entry);
145 assertEquals("Car list node", listBuilder.build(), optional.get());
148 private void verifyNode(DOMStoreReadTransaction readTx, YangInstanceIdentifier path, NormalizedNode<?, ?> expNode)
150 Optional<NormalizedNode<?, ?>> optional = readTx.read(path).get(5, TimeUnit.SECONDS);
151 assertEquals("isPresent", true, optional.isPresent());
152 assertEquals("Data node", expNode, optional.get());
155 private void verifyExists(DOMStoreReadTransaction readTx, YangInstanceIdentifier path) throws Exception {
156 Boolean exists = readTx.exists(path).get(5, TimeUnit.SECONDS);
157 assertEquals("exists", true, exists);
161 public void testWriteTransactionWithSingleShard() throws Exception {
162 String testName = "testWriteTransactionWithSingleShard";
163 initDatastores(testName);
165 String followerCarShardName = "member-2-shard-cars-" + testName;
166 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 2, ApplyJournalEntries.class );
168 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
169 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
171 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
172 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
174 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
175 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
176 writeTx.merge(car1Path, car1);
178 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
179 YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
180 writeTx.merge(car2Path, car2);
182 followerTestKit.doCommit(writeTx.ready());
184 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
186 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car1, car2);
190 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
192 writeTx.delete(car1Path);
194 followerTestKit.doCommit(writeTx.ready());
196 verifyExists(followerDistributedDataStore.newReadOnlyTransaction(), car2Path);
198 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
200 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car2);
202 // Re-instate the follower member 2 as a single-node to verify replication and recovery.
204 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
206 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
207 JavaTestKit.shutdownActorSystem(followerSystem, null, true);
209 ActorSystem newSystem = ActorSystem.create("reinstated-member2", ConfigFactory.load().getConfig("Member2"));
211 DistributedDataStore member2Datastore = new IntegrationTestKit(newSystem, leaderDatastoreContextBuilder).
212 setupDistributedDataStore(testName, "module-shards-member2", true, SHARD_NAMES);
214 verifyCars(member2Datastore.newReadOnlyTransaction(), car2);
216 JavaTestKit.shutdownActorSystem(newSystem);
220 public void testReadWriteTransactionWithSingleShard() throws Exception {
221 initDatastores("testReadWriteTransactionWithSingleShard");
223 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
224 assertNotNull("newReadWriteTransaction returned null", rwTx);
226 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
227 rwTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
229 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
230 rwTx.merge(CarsModel.newCarPath("optima"), car1);
232 verifyCars(rwTx, car1);
234 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
235 YangInstanceIdentifier car2Path = CarsModel.newCarPath("sportage");
236 rwTx.merge(car2Path, car2);
238 verifyExists(rwTx, car2Path);
240 followerTestKit.doCommit(rwTx.ready());
242 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1, car2);
246 public void testWriteTransactionWithMultipleShards() throws Exception {
247 initDatastores("testWriteTransactionWithMultipleShards");
249 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
250 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
252 YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
253 NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
254 writeTx.write(carsPath, carsNode);
256 YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
257 NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
258 writeTx.write(peoplePath, peopleNode);
260 followerTestKit.doCommit(writeTx.ready());
262 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
264 verifyNode(readTx, carsPath, carsNode);
265 verifyNode(readTx, peoplePath, peopleNode);
269 public void testReadWriteTransactionWithMultipleShards() throws Exception {
270 initDatastores("testReadWriteTransactionWithMultipleShards");
272 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
273 assertNotNull("newReadWriteTransaction returned null", rwTx);
275 YangInstanceIdentifier carsPath = CarsModel.BASE_PATH;
276 NormalizedNode<?, ?> carsNode = CarsModel.emptyContainer();
277 rwTx.write(carsPath, carsNode);
279 YangInstanceIdentifier peoplePath = PeopleModel.BASE_PATH;
280 NormalizedNode<?, ?> peopleNode = PeopleModel.emptyContainer();
281 rwTx.write(peoplePath, peopleNode);
283 followerTestKit.doCommit(rwTx.ready());
285 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
287 verifyNode(readTx, carsPath, carsNode);
288 verifyNode(readTx, peoplePath, peopleNode);
292 public void testTransactionChainWithSingleShard() throws Exception {
293 initDatastores("testTransactionChainWithSingleShard");
295 DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
297 // Add the top-level cars container with write-only.
299 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
300 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
302 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
306 // Verify the top-level cars container with read-only.
308 verifyNode(txChain.newReadOnlyTransaction(), CarsModel.BASE_PATH, CarsModel.emptyContainer());
310 // Perform car operations with read-write.
312 DOMStoreReadWriteTransaction rwTx = txChain.newReadWriteTransaction();
314 verifyNode(rwTx, CarsModel.BASE_PATH, CarsModel.emptyContainer());
316 rwTx.merge(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
318 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
319 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
320 rwTx.write(car1Path, car1);
322 verifyExists(rwTx, car1Path);
324 verifyCars(rwTx, car1);
326 MapEntryNode car2 = CarsModel.newCarEntry("sportage", BigInteger.valueOf(25000));
327 rwTx.merge(CarsModel.newCarPath("sportage"), car2);
329 rwTx.delete(car1Path);
331 followerTestKit.doCommit(rwTx.ready());
335 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car2);
339 public void testTransactionChainWithMultipleShards() throws Exception{
340 initDatastores("testTransactionChainWithMultipleShards");
342 DOMStoreTransactionChain txChain = followerDistributedDataStore.createTransactionChain();
344 DOMStoreWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
345 assertNotNull("newWriteOnlyTransaction returned null", writeTx);
347 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
348 writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
350 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
351 writeTx.write(PeopleModel.PERSON_LIST_PATH, PeopleModel.newPersonMapNode());
353 followerTestKit.doCommit(writeTx.ready());
355 DOMStoreReadWriteTransaction readWriteTx = txChain.newReadWriteTransaction();
357 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
358 YangInstanceIdentifier carPath = CarsModel.newCarPath("optima");
359 readWriteTx.write(carPath, car);
361 MapEntryNode person = PeopleModel.newPersonEntry("jack");
362 YangInstanceIdentifier personPath = PeopleModel.newPersonPath("jack");
363 readWriteTx.merge(personPath, person);
365 Optional<NormalizedNode<?, ?>> optional = readWriteTx.read(carPath).get(5, TimeUnit.SECONDS);
366 assertEquals("isPresent", true, optional.isPresent());
367 assertEquals("Data node", car, optional.get());
369 optional = readWriteTx.read(personPath).get(5, TimeUnit.SECONDS);
370 assertEquals("isPresent", true, optional.isPresent());
371 assertEquals("Data node", person, optional.get());
373 DOMStoreThreePhaseCommitCohort cohort2 = readWriteTx.ready();
375 writeTx = txChain.newWriteOnlyTransaction();
377 writeTx.delete(personPath);
379 DOMStoreThreePhaseCommitCohort cohort3 = writeTx.ready();
381 followerTestKit.doCommit(cohort2);
382 followerTestKit.doCommit(cohort3);
386 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
387 verifyCars(readTx, car);
389 optional = readTx.read(personPath).get(5, TimeUnit.SECONDS);
390 assertEquals("isPresent", false, optional.isPresent());
394 public void testChainedTransactionFailureWithSingleShard() throws Exception {
395 initDatastores("testChainedTransactionFailureWithSingleShard");
397 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
398 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
399 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
400 MoreExecutors.directExecutor());
402 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
403 DOMTransactionChain txChain = broker.createTransactionChain(listener);
405 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
407 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
408 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME)).
409 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
411 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
414 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
415 fail("Expected TransactionCommitFailedException");
416 } catch (TransactionCommitFailedException e) {
420 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
427 public void testChainedTransactionFailureWithMultipleShards() throws Exception {
428 initDatastores("testChainedTransactionFailureWithMultipleShards");
430 ConcurrentDOMDataBroker broker = new ConcurrentDOMDataBroker(
431 ImmutableMap.<LogicalDatastoreType, DOMStore>builder().put(
432 LogicalDatastoreType.CONFIGURATION, followerDistributedDataStore).build(),
433 MoreExecutors.directExecutor());
435 TransactionChainListener listener = Mockito.mock(TransactionChainListener.class);
436 DOMTransactionChain txChain = broker.createTransactionChain(listener);
438 DOMDataWriteTransaction writeTx = txChain.newWriteOnlyTransaction();
440 writeTx.put(LogicalDatastoreType.CONFIGURATION, PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
442 ContainerNode invalidData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
443 new YangInstanceIdentifier.NodeIdentifier(CarsModel.BASE_QNAME)).
444 withChild(ImmutableNodes.leafNode(TestModel.JUNK_QNAME, "junk")).build();
446 // Note that merge will validate the data and fail but put succeeds b/c deep validation is not
447 // done for put for performance reasons.
448 writeTx.merge(LogicalDatastoreType.CONFIGURATION, CarsModel.BASE_PATH, invalidData);
451 writeTx.submit().checkedGet(5, TimeUnit.SECONDS);
452 fail("Expected TransactionCommitFailedException");
453 } catch (TransactionCommitFailedException e) {
457 verify(listener, timeout(5000)).onTransactionChainFailed(eq(txChain), eq(writeTx), any(Throwable.class));
464 public void testSingleShardTransactionsWithLeaderChanges() throws Exception {
465 String testName = "testSingleShardTransactionsWithLeaderChanges";
466 initDatastores(testName);
468 String followerCarShardName = "member-2-shard-cars-" + testName;
469 InMemoryJournal.addWriteMessagesCompleteLatch(followerCarShardName, 1, ApplyJournalEntries.class );
471 // Write top-level car container from the follower so it uses a remote Tx.
473 DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
475 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
476 writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
478 followerTestKit.doCommit(writeTx.ready());
480 InMemoryJournal.waitForWriteMessagesComplete(followerCarShardName);
482 // Switch the leader to the follower
484 followerDatastoreContextBuilder.shardElectionTimeoutFactor(1);
485 followerDistributedDataStore.onDatastoreContextUpdated(followerDatastoreContextBuilder.build());
487 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
489 followerTestKit.waitUntilNoLeader(followerDistributedDataStore.getActorContext(), SHARD_NAMES);
491 leaderSystem = ActorSystem.create("cluster-test", ConfigFactory.load().getConfig("Member1"));
492 Cluster.get(leaderSystem).join(MEMBER_2_ADDRESS);
494 DatastoreContext.Builder newMember1Builder = DatastoreContext.newBuilder().
495 shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
496 IntegrationTestKit newMember1TestKit = new IntegrationTestKit(leaderSystem, newMember1Builder);
497 newMember1TestKit.setupDistributedDataStore(testName, MODULE_SHARDS_CONFIG_2, false, SHARD_NAMES);
499 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), SHARD_NAMES);
501 // Write a car entry to the new leader - should switch to local Tx
503 writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
505 MapEntryNode car1 = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
506 YangInstanceIdentifier car1Path = CarsModel.newCarPath("optima");
507 writeTx.merge(car1Path, car1);
509 followerTestKit.doCommit(writeTx.ready());
511 verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car1);
515 public void testReadyLocalTransactionForwardedToLeader() throws Exception {
516 initDatastores("testReadyLocalTransactionForwardedToLeader");
517 followerTestKit.waitUntilLeader(followerDistributedDataStore.getActorContext(), "cars");
519 Optional<ActorRef> carsFollowerShard = followerDistributedDataStore.getActorContext().findLocalShard("cars");
520 assertEquals("Cars follower shard found", true, carsFollowerShard.isPresent());
522 TipProducingDataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
523 dataTree.setSchemaContext(SchemaContextHelper.full());
524 DataTreeModification modification = dataTree.takeSnapshot().newModification();
526 new WriteModification(CarsModel.BASE_PATH, CarsModel.emptyContainer()).apply(modification);
527 new MergeModification(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode()).apply(modification);
529 MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
530 new WriteModification(CarsModel.newCarPath("optima"), car).apply(modification);
532 String transactionID = "tx-1";
533 ReadyLocalTransaction readyLocal = new ReadyLocalTransaction(transactionID , modification, true);
535 carsFollowerShard.get().tell(readyLocal, followerTestKit.getRef());
536 Object resp = followerTestKit.expectMsgClass(Object.class);
537 if(resp instanceof akka.actor.Status.Failure) {
538 throw new AssertionError("Unexpected failure response", ((akka.actor.Status.Failure)resp).cause());
541 assertTrue("Expected response of type " + CommitTransactionReply.SERIALIZABLE_CLASS,
542 CommitTransactionReply.SERIALIZABLE_CLASS.equals(resp.getClass()));
544 verifyCars(leaderDistributedDataStore.newReadOnlyTransaction(), car);
547 @Test(expected=NoShardLeaderException.class)
548 public void testTransactionWithIsolatedLeader() throws Throwable {
549 leaderDatastoreContextBuilder.shardIsolatedLeaderCheckIntervalInMillis(300);
550 String testName = "testTransactionWithIsolatedLeader";
551 initDatastores(testName);
553 JavaTestKit.shutdownActorSystem(followerSystem, null, true);
555 Uninterruptibles.sleepUninterruptibly(leaderDistributedDataStore.getActorContext().getDatastoreContext()
556 .getShardRaftConfig().getElectionTimeOutInterval().toMillis() * 3, TimeUnit.MILLISECONDS);
558 DOMStoreWriteTransaction writeTx = leaderDistributedDataStore.newWriteOnlyTransaction();
559 writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
562 followerTestKit.doCommit(writeTx.ready());
563 } catch (ExecutionException e) {
568 @Test(expected=AskTimeoutException.class)
569 public void testTransactionWithShardLeaderNotResponding() throws Throwable {
570 followerDatastoreContextBuilder.shardElectionTimeoutFactor(30);
571 initDatastores("testTransactionWithShardLeaderNotResponding");
573 // Do an initial read to get the primary shard info cached.
575 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
576 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
578 // Shutdown the leader and try to create a new tx.
580 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
582 followerDatastoreContextBuilder.operationTimeoutInMillis(50).shardElectionTimeoutFactor(1);
583 followerDistributedDataStore.onDatastoreContextUpdated(followerDatastoreContextBuilder.build());
585 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
587 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
590 followerTestKit.doCommit(rwTx.ready());
591 } catch (ExecutionException e) {
592 assertTrue("Expected ShardLeaderNotRespondingException cause. Actual: " + e.getCause(),
593 e.getCause() instanceof ShardLeaderNotRespondingException);
594 assertNotNull("Expected a nested cause", e.getCause().getCause());
595 throw e.getCause().getCause();
599 @Test(expected=NoShardLeaderException.class)
600 public void testTransactionWithCreateTxFailureDueToNoLeader() throws Throwable {
601 initDatastores("testTransactionWithCreateTxFailureDueToNoLeader");
603 // Do an initial read to get the primary shard info cached.
605 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
606 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
608 // Shutdown the leader and try to create a new tx.
610 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
612 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
614 followerDatastoreContextBuilder.operationTimeoutInMillis(10).shardElectionTimeoutFactor(1);
615 followerDistributedDataStore.onDatastoreContextUpdated(followerDatastoreContextBuilder.build());
617 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
619 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
622 followerTestKit.doCommit(rwTx.ready());
623 } catch (ExecutionException e) {
629 public void testTransactionRetryWithInitialAskTimeoutExOnCreateTx() throws Exception {
630 followerDatastoreContextBuilder.shardElectionTimeoutFactor(30);
631 String testName = "testTransactionRetryWithInitialAskTimeoutExOnCreateTx";
632 initDatastores(testName, MODULE_SHARDS_CONFIG_3);
634 DatastoreContext.Builder follower2DatastoreContextBuilder = DatastoreContext.newBuilder().
635 shardHeartbeatIntervalInMillis(100).shardElectionTimeoutFactor(5);
636 IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, follower2DatastoreContextBuilder);
637 follower2TestKit.setupDistributedDataStore(testName, MODULE_SHARDS_CONFIG_3, false, SHARD_NAMES);
639 // Do an initial read to get the primary shard info cached.
641 DOMStoreReadTransaction readTx = followerDistributedDataStore.newReadOnlyTransaction();
642 readTx.read(CarsModel.BASE_PATH).checkedGet(5, TimeUnit.SECONDS);
644 // Shutdown the leader and try to create a new tx.
646 JavaTestKit.shutdownActorSystem(leaderSystem, null, true);
648 followerDatastoreContextBuilder.operationTimeoutInMillis(500);
649 followerDistributedDataStore.onDatastoreContextUpdated(followerDatastoreContextBuilder.build());
651 DOMStoreReadWriteTransaction rwTx = followerDistributedDataStore.newReadWriteTransaction();
653 rwTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
655 followerTestKit.doCommit(rwTx.ready());