This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch branch-3.1.1_review
in repository https://gitbox.apache.org/repos/asf/ratis.git

commit bd154742c099ff276958e5f873f108a411b436af
Author: Duong Nguyen <[email protected]>
AuthorDate: Fri Aug 2 09:32:22 2024 -0700

    RATIS-2104. Double shutdown in TestLeaderInstallSnapshot. (#1108)
---
 .../ratis/InstallSnapshotFromLeaderTests.java      | 160 ++++++++++-----------
 1 file changed, 76 insertions(+), 84 deletions(-)

diff --git 
a/ratis-server/src/test/java/org/apache/ratis/InstallSnapshotFromLeaderTests.java
 
b/ratis-server/src/test/java/org/apache/ratis/InstallSnapshotFromLeaderTests.java
index b83a7dfdd..16a335324 100644
--- 
a/ratis-server/src/test/java/org/apache/ratis/InstallSnapshotFromLeaderTests.java
+++ 
b/ratis-server/src/test/java/org/apache/ratis/InstallSnapshotFromLeaderTests.java
@@ -95,105 +95,97 @@ public abstract class 
InstallSnapshotFromLeaderTests<CLUSTER extends MiniRaftClu
   }
 
   private void testMultiFileInstallSnapshot(CLUSTER cluster) throws Exception {
-    try {
-      int i = 0;
-      RaftTestUtil.waitForLeader(cluster);
-      final RaftPeerId leaderId = cluster.getLeader().getId();
-
-      try (final RaftClient client = cluster.createClient(leaderId)) {
-        for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
-          RaftClientReply
-              reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + 
i));
-          Assertions.assertTrue(reply.isSuccess());
-        }
-
-        client.getSnapshotManagementApi(leaderId).create(3000);
+    int i = 0;
+    RaftTestUtil.waitForLeader(cluster);
+    final RaftPeerId leaderId = cluster.getLeader().getId();
+
+    try (final RaftClient client = cluster.createClient(leaderId)) {
+      for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
+        RaftClientReply
+            reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
+        Assertions.assertTrue(reply.isSuccess());
       }
 
-      final SnapshotInfo snapshot = 
cluster.getLeader().getStateMachine().getLatestSnapshot();
-      Assertions.assertEquals(3, snapshot.getFiles().size());
-
-      // add two more peers
-      final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
-          true);
-      // trigger setConfiguration
-      cluster.setConfiguration(change.allPeersInNewConf);
-
-      RaftServerTestUtil
-          .waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
-
-      // Check the installed snapshot file number on each Follower matches 
with the
-      // leader snapshot.
-      JavaUtils.attempt(() -> {
-        for (RaftServer.Division follower : cluster.getFollowers()) {
-          final SnapshotInfo info = 
follower.getStateMachine().getLatestSnapshot();
-          Assertions.assertNotNull(info);
-          Assertions.assertEquals(3, info.getFiles().size());
-        }
-      }, 10, ONE_SECOND, "check snapshot", LOG);
-    } finally {
-      cluster.shutdown();
+      client.getSnapshotManagementApi(leaderId).create(3000);
     }
+
+    final SnapshotInfo snapshot = 
cluster.getLeader().getStateMachine().getLatestSnapshot();
+    Assertions.assertEquals(3, snapshot.getFiles().size());
+
+    // add two more peers
+    final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
+        true);
+    // trigger setConfiguration
+    cluster.setConfiguration(change.allPeersInNewConf);
+
+    RaftServerTestUtil
+        .waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
+
+    // Check the installed snapshot file number on each Follower matches with 
the
+    // leader snapshot.
+    JavaUtils.attempt(() -> {
+      for (RaftServer.Division follower : cluster.getFollowers()) {
+        final SnapshotInfo info = 
follower.getStateMachine().getLatestSnapshot();
+        Assertions.assertNotNull(info);
+        Assertions.assertEquals(3, info.getFiles().size());
+      }
+    }, 10, ONE_SECOND, "check snapshot", LOG);
   }
 
   private void testInstallSnapshotDuringLeaderSwitch(CLUSTER cluster) throws 
Exception {
-    try {
-      RaftTestUtil.waitForLeader(cluster);
-      final RaftPeerId leaderId = cluster.getLeader().getId();
-
-      // perform operations and force all peers to take snapshot
-      try (final RaftClient client = cluster.createClient(leaderId)) {
-        for (int i = 0; i < SNAPSHOT_TRIGGER_THRESHOLD * 2; i++) {
-          final RaftClientReply
-              reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + 
i));
-          Assertions.assertTrue(reply.isSuccess());
-        }
+    RaftTestUtil.waitForLeader(cluster);
+    final RaftPeerId leaderId = cluster.getLeader().getId();
 
-        for (final RaftPeer peer: cluster.getPeers()) {
-          final RaftClientReply snapshotReply = 
client.getSnapshotManagementApi(leaderId).create(3000);
-          Assertions.assertTrue(snapshotReply.isSuccess());
-        }
+    // perform operations and force all peers to take snapshot
+    try (final RaftClient client = cluster.createClient(leaderId)) {
+      for (int i = 0; i < SNAPSHOT_TRIGGER_THRESHOLD * 2; i++) {
+        final RaftClientReply
+            reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
+        Assertions.assertTrue(reply.isSuccess());
       }
-      final SnapshotInfo snapshot = 
cluster.getLeader().getStateMachine().getLatestSnapshot();
-      Assertions.assertNotNull(snapshot);
 
-      // isolate two followers (majority) in old configuration
-      final List<RaftServer.Division> oldFollowers = cluster.getFollowers();
-      for (RaftServer.Division f: oldFollowers) {
-        RaftTestUtil.isolate(cluster, f.getId());
+      for (final RaftPeer peer: cluster.getPeers()) {
+        final RaftClientReply snapshotReply = 
client.getSnapshotManagementApi(leaderId).create(3000);
+        Assertions.assertTrue(snapshotReply.isSuccess());
       }
+    }
+    final SnapshotInfo snapshot = 
cluster.getLeader().getStateMachine().getLatestSnapshot();
+    Assertions.assertNotNull(snapshot);
 
-      // add two more peers and install snapshot from leaders
-      final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
-          true);
-      try (final RaftClient client = cluster.createClient(leaderId, 
RetryPolicies.noRetry())) {
-        Assertions.assertThrows(RaftRetryFailureException.class,
-                 () -> 
client.admin().setConfiguration(change.allPeersInNewConf));
-      }
+    // isolate two followers (majority) in old configuration
+    final List<RaftServer.Division> oldFollowers = cluster.getFollowers();
+    for (RaftServer.Division f: oldFollowers) {
+      RaftTestUtil.isolate(cluster, f.getId());
+    }
 
-      final SnapshotInfo snapshotInfo = 
cluster.getDivision(change.newPeers[0].getId())
-           .getStateMachine().getLatestSnapshot();
-      Assertions.assertNotNull(snapshotInfo);
+    // add two more peers and install snapshot from leaders
+    final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true,
+        true);
+    try (final RaftClient client = cluster.createClient(leaderId, 
RetryPolicies.noRetry())) {
+      Assertions.assertThrows(RaftRetryFailureException.class,
+               () -> 
client.admin().setConfiguration(change.allPeersInNewConf));
+    }
 
-      // recover the old followers and isolate the leader to force leader 
switch
-      RaftTestUtil.isolate(cluster, leaderId);
-      for (RaftServer.Division f: oldFollowers) {
-        RaftTestUtil.deIsolate(cluster, f.getId());
-      }
-      RaftTestUtil.waitForLeader(cluster);
+    final SnapshotInfo snapshotInfo = 
cluster.getDivision(change.newPeers[0].getId())
+         .getStateMachine().getLatestSnapshot();
+    Assertions.assertNotNull(snapshotInfo);
 
-      try (final RaftClient client = 
cluster.createClient(cluster.getLeader().getId())) {
-        // successfully setConfiguration during leader switch
-        final RaftClientReply setConf = 
client.admin().setConfiguration(change.allPeersInNewConf);
-        Assertions.assertTrue(setConf.isSuccess());
+    // recover the old followers and isolate the leader to force leader switch
+    RaftTestUtil.isolate(cluster, leaderId);
+    for (RaftServer.Division f: oldFollowers) {
+      RaftTestUtil.deIsolate(cluster, f.getId());
+    }
+    RaftTestUtil.waitForLeader(cluster);
 
-        RaftTestUtil.deIsolate(cluster, leaderId);
-        final RaftClientReply
-              reply = client.io().send(new 
RaftTestUtil.SimpleMessage("final"));
-        Assertions.assertTrue(reply.isSuccess());
-      }
-    } finally {
-      cluster.shutdown();
+    try (final RaftClient client = 
cluster.createClient(cluster.getLeader().getId())) {
+      // successfully setConfiguration during leader switch
+      final RaftClientReply setConf = 
client.admin().setConfiguration(change.allPeersInNewConf);
+      Assertions.assertTrue(setConf.isSuccess());
+
+      RaftTestUtil.deIsolate(cluster, leaderId);
+      final RaftClientReply
+            reply = client.io().send(new RaftTestUtil.SimpleMessage("final"));
+      Assertions.assertTrue(reply.isSuccess());
     }
   }
 

Reply via email to