Skip to content

Commit

Permalink
[RatisConsensus] add ut for force snapshot (#12560)
Browse files Browse the repository at this point in the history
* add ut for forcesnapshot

* add ut for forcesnapshot
  • Loading branch information
SzyWilliam authored May 23, 2024
1 parent 0ebac6b commit b2c5680
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand All @@ -32,9 +31,7 @@
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 {
Expand Down Expand Up @@ -90,32 +87,13 @@ private void testSnapshotImpl(boolean periodic, boolean threshold) throws Except

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ public void transferSnapshot() throws Exception {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand All @@ -54,6 +55,7 @@
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;
Expand Down Expand Up @@ -441,6 +443,25 @@ DataSet readThrough(int serverIndex) throws ConsensusException {
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 {
Expand Down

0 comments on commit b2c5680

Please sign in to comment.