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

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new b2c5680f3d3 [RatisConsensus] add ut for force snapshot (#12560)
b2c5680f3d3 is described below

commit b2c5680f3d3b00b6e67b60f47000b64d692bb3dd
Author: William Song <[email protected]>
AuthorDate: Thu May 23 16:43:46 2024 +0800

    [RatisConsensus] add ut for force snapshot (#12560)
    
    * add ut for forcesnapshot
    
    * add ut for forcesnapshot
---
 .../iotdb/consensus/ratis/DiskGuardianTest.java    | 28 +++-------------------
 .../iotdb/consensus/ratis/RatisConsensusTest.java  | 15 ++++++++++++
 .../apache/iotdb/consensus/ratis/TestUtils.java    | 21 ++++++++++++++++
 3 files changed, 39 insertions(+), 25 deletions(-)

diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/DiskGuardianTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/DiskGuardianTest.java
index 53bf27fb286..9030a522d86 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/DiskGuardianTest.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/DiskGuardianTest.java
@@ -23,7 +23,6 @@ import org.apache.iotdb.commons.consensus.SchemaRegionId;
 import org.apache.iotdb.consensus.common.Peer;
 import org.apache.iotdb.consensus.config.RatisConfig;
 import org.apache.iotdb.consensus.ratis.utils.Retriable;
-import org.apache.iotdb.consensus.ratis.utils.Utils;
 
 import org.apache.ratis.util.TimeDuration;
 import org.junit.After;
@@ -32,9 +31,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.IOException;
 import java.util.List;
-import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 public class DiskGuardianTest {
@@ -90,32 +87,13 @@ public class DiskGuardianTest {
 
     miniCluster.waitUntilActiveLeaderElectedAndReady();
     miniCluster.writeManySerial(0, 10);
-    Assert.assertFalse(hasSnapshot(gid));
+    Assert.assertFalse(miniCluster.hasSnapshot(gid, 0));
     Retriable.attemptUntilTrue(
-        () -> hasSnapshot(gid),
+        () -> miniCluster.hasSnapshot(gid, 0),
         12,
         TimeDuration.valueOf(5, TimeUnit.SECONDS),
         "should take snapshot",
         logger);
-    Assert.assertTrue(hasSnapshot(gid));
-  }
-
-  private boolean hasSnapshot(ConsensusGroupId gid) {
-    try {
-      return Objects.requireNonNull(
-                  miniCluster
-                      .getServer(0)
-                      .getServer()
-                      
.getDivision(Utils.fromConsensusGroupIdToRaftGroupId(gid))
-                      .getRaftStorage()
-                      .getStorageDir()
-                      .getStateMachineDir()
-                      .listFiles())
-              .length
-          != 0;
-    } catch (IOException ioe) {
-      Assert.fail("caught IOException:" + ioe);
-      return false; // required by the compiler
-    }
+    Assert.assertTrue(miniCluster.hasSnapshot(gid, 0));
   }
 }
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
index d164cac4316..6883f782443 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
@@ -275,6 +275,21 @@ public class RatisConsensusTest {
     doConsensus(1, 10, 20);
   }
 
+  @Test
+  public void forceSnapshot() throws Exception {
+    servers.get(0).createLocalPeer(group.getGroupId(), group.getPeers());
+    servers.get(1).createLocalPeer(group.getGroupId(), group.getPeers());
+    servers.get(2).createLocalPeer(group.getGroupId(), group.getPeers());
+
+    doConsensus(0, 1, 1); // not enough logs to auto trigger snapshot
+    try {
+      servers.get(0).triggerSnapshot(group.getGroupId(), true);
+    } catch (ConsensusException e) {
+      Assert.fail(e.getMessage());
+    }
+    Assert.assertTrue(miniCluster.hasSnapshot(group.getGroupId(), 0));
+  }
+
   @Test
   public void parsingAndConstructIDs() throws Exception {
     servers.get(0).createLocalPeer(gid, peers.subList(0, 1));
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java
index 37c9bbb51cd..4aaa5f77d9e 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java
@@ -37,6 +37,7 @@ import org.apache.iotdb.consensus.config.RatisConfig;
 import org.apache.iotdb.consensus.exception.ConsensusException;
 import org.apache.iotdb.consensus.exception.RatisReadUnavailableException;
 import org.apache.iotdb.consensus.ratis.utils.Retriable;
+import org.apache.iotdb.consensus.ratis.utils.Utils;
 
 import org.apache.ratis.thirdparty.com.google.common.base.Preconditions;
 import org.apache.ratis.util.FileUtils;
@@ -54,6 +55,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.Scanner;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
@@ -441,6 +443,25 @@ public class TestUtils {
       final ByteBufferConsensusRequest getReq = 
TestUtils.TestRequest.getRequest();
       return servers.get(serverIndex).read(gid, getReq);
     }
+
+    boolean hasSnapshot(ConsensusGroupId gid, int serverIndex) {
+      try {
+        return Objects.requireNonNull(
+                    servers
+                        .get(serverIndex)
+                        .getServer()
+                        
.getDivision(Utils.fromConsensusGroupIdToRaftGroupId(gid))
+                        .getRaftStorage()
+                        .getStorageDir()
+                        .getStateMachineDir()
+                        .listFiles())
+                .length
+            != 0;
+      } catch (IOException ioe) {
+        logger.error("caught IOException:", ioe);
+        return false; // required by the compiler
+      }
+    }
   }
 
   static class MiniClusterFactory {

Reply via email to