+ followerTestKit.doCommit(writeTx1CanCommit, writeTx1Cohort);
+ followerTestKit.doCommit(writeTx2CanCommit, writeTx2Cohort);
+ followerTestKit.doCommit(writeTx3Cohort);
+ followerTestKit.doCommit(writeTx4Cohort);
+ followerTestKit.doCommit(rwTxCohort);
+
+ DOMStoreReadTransaction readTx = leaderDistributedDataStore.newReadOnlyTransaction();
+ verifyCars(readTx, cars.toArray(new MapEntryNode[cars.size()]));
+ verifyNode(readTx, PeopleModel.PERSON_LIST_PATH, people);
+ }
+
+ @Test
+ public void testLeadershipTransferOnShutdown() throws Exception {
+ //TODO remove when test passes also for ClientBackedDataStore
+ Assume.assumeTrue(testParameter.equals(DistributedDataStore.class));
+ leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
+ followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
+ final String testName = "testLeadershipTransferOnShutdown";
+ initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
+
+ final IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System,
+ DatastoreContext.newBuilderFrom(followerDatastoreContextBuilder.build()).operationTimeoutInMillis(100));
+ try (final AbstractDataStore follower2DistributedDataStore = follower2TestKit.setupAbstractDataStore(
+ testParameter, testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, false)) {
+
+ // Create and submit a couple tx's so they're pending.
+
+ DOMStoreWriteTransaction writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
+ writeTx.write(CarsModel.BASE_PATH, CarsModel.emptyContainer());
+ writeTx.write(CarsModel.CAR_LIST_PATH, CarsModel.newCarMapNode());
+ writeTx.write(PeopleModel.BASE_PATH, PeopleModel.emptyContainer());
+ final DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
+
+ IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
+ stats -> assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize()));
+
+ writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
+ final MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
+ writeTx.write(CarsModel.newCarPath("optima"), car);
+ final DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
+
+ IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars",
+ stats -> assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize()));
+
+ // Gracefully stop the leader via a Shutdown message.
+
+ sendDatastoreContextUpdate(leaderDistributedDataStore, leaderDatastoreContextBuilder
+ .shardElectionTimeoutFactor(100));
+
+ final FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
+ final Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
+ final ActorRef leaderActor = Await.result(future, duration);
+
+ final Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
+
+ // Commit the 2 transactions. They should finish and succeed.
+
+ followerTestKit.doCommit(cohort1);
+ followerTestKit.doCommit(cohort2);
+
+ // Wait for the leader actor stopped.
+
+ final Boolean stopped = Await.result(stopFuture, duration);
+ assertEquals("Stopped", Boolean.TRUE, stopped);
+
+ // Verify leadership was transferred by reading the committed data from the other nodes.
+
+ verifyCars(followerDistributedDataStore.newReadOnlyTransaction(), car);
+ verifyCars(follower2DistributedDataStore.newReadOnlyTransaction(), car);
+ }