+ @Test
+ public void testLeadershipTransferOnShutdown() throws Exception {
+ leaderDatastoreContextBuilder.shardBatchedModificationCount(1);
+ followerDatastoreContextBuilder.shardElectionTimeoutFactor(10).customRaftPolicyImplementation(null);
+ String testName = "testLeadershipTransferOnShutdown";
+ initDatastores(testName, MODULE_SHARDS_CARS_PEOPLE_1_2_3, CARS_AND_PEOPLE);
+
+ IntegrationTestKit follower2TestKit = new IntegrationTestKit(follower2System, followerDatastoreContextBuilder);
+ DistributedDataStore follower2DistributedDataStore = follower2TestKit.setupDistributedDataStore(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());
+ DOMStoreThreePhaseCommitCohort cohort1 = writeTx.ready();
+
+ IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", new ShardStatsVerifier() {
+ @Override
+ public void verify(ShardStats stats) {
+ assertEquals("getTxCohortCacheSize", 1, stats.getTxCohortCacheSize());
+ }
+ });
+
+ writeTx = followerDistributedDataStore.newWriteOnlyTransaction();
+ MapEntryNode car = CarsModel.newCarEntry("optima", BigInteger.valueOf(20000));
+ writeTx.write(CarsModel.newCarPath("optima"), car);
+ DOMStoreThreePhaseCommitCohort cohort2 = writeTx.ready();
+
+ IntegrationTestKit.verifyShardStats(leaderDistributedDataStore, "cars", new ShardStatsVerifier() {
+ @Override
+ public void verify(ShardStats stats) {
+ assertEquals("getTxCohortCacheSize", 2, stats.getTxCohortCacheSize());
+ }
+ });
+
+ // Gracefully stop the leader via a Shutdown message.
+
+ FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
+ Future<ActorRef> future = leaderDistributedDataStore.getActorContext().findLocalShardAsync("cars");
+ ActorRef leaderActor = Await.result(future, duration);
+
+ Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown());
+
+ // Commit the 2 transactions. They should finish and succeed.
+
+ followerTestKit.doCommit(cohort1);
+ followerTestKit.doCommit(cohort2);
+
+ // Wait for the leader actor stopped.
+
+ 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);
+ }
+