This is an automated email from the ASF dual-hosted git repository. williamsong pushed a commit to branch snapshot-branch2 in repository https://gitbox.apache.org/repos/asf/ratis.git
commit ee5bce0b7362bba63390a354e520c3f8d54b2b1e Author: Potato <[email protected]> AuthorDate: Fri Jul 28 22:44:05 2023 +0800 RATIS-1862 Add the parameter whether to take Snapshot when stopping to adapt to different services (#896) --- .../java/org/apache/ratis/server/RaftServerConfigKeys.java | 12 ++++++++++++ .../org/apache/ratis/server/impl/StateMachineUpdater.java | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java index 6c5aa1212..8665c8874 100644 --- a/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java +++ b/ratis-server-api/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java @@ -571,6 +571,18 @@ public interface RaftServerConfigKeys { setBoolean(properties::setBoolean, AUTO_TRIGGER_ENABLED_KEY, autoTriggerEnabled); } + /** whether trigger snapshot when stop raft server */ + String TRIGGER_WHEN_STOP_ENABLED_KEY = PREFIX + ".trigger-when-stop.enabled"; + /** by default let the state machine to trigger snapshot when stop */ + boolean TRIGGER_WHEN_STOP_ENABLED_DEFAULT = true; + static boolean triggerWhenStopEnabled(RaftProperties properties) { + return getBoolean(properties::getBoolean, + TRIGGER_WHEN_STOP_ENABLED_KEY, TRIGGER_WHEN_STOP_ENABLED_DEFAULT, getDefaultLog()); + } + static void setTriggerWhenStopEnabled(RaftProperties properties, boolean triggerWhenStopEnabled) { + setBoolean(properties::setBoolean, TRIGGER_WHEN_STOP_ENABLED_KEY, triggerWhenStopEnabled); + } + /** The log index gap between to two snapshot creations. */ String CREATION_GAP_KEY = PREFIX + ".creation.gap"; long CREATION_GAP_DEFAULT = 1024; diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java index 18f165198..3cfaea8d1 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java @@ -73,6 +73,8 @@ class StateMachineUpdater implements Runnable { private final RaftServerImpl server; private final RaftLog raftLog; + private final boolean triggerSnapshotWhenStopEnabled; + private final Long autoSnapshotThreshold; private final boolean purgeUptoSnapshotIndex; @@ -104,6 +106,7 @@ class StateMachineUpdater implements Runnable { this.appliedIndex = new RaftLogIndex("appliedIndex", lastAppliedIndex); this.snapshotIndex = new RaftLogIndex("snapshotIndex", lastAppliedIndex); + this.triggerSnapshotWhenStopEnabled = RaftServerConfigKeys.Snapshot.triggerWhenStopEnabled(properties); final boolean autoSnapshot = RaftServerConfigKeys.Snapshot.autoTriggerEnabled(properties); this.autoSnapshotThreshold = autoSnapshot? RaftServerConfigKeys.Snapshot.autoTriggerThreshold(properties): null; final int numSnapshotFilesRetained = RaftServerConfigKeys.Snapshot.retentionFileNum(properties); @@ -318,7 +321,7 @@ class StateMachineUpdater implements Runnable { if (autoSnapshotThreshold == null) { return false; } else if (shouldStop()) { - return getLastAppliedIndex() - snapshotIndex.get() > 0; + return triggerSnapshotWhenStopEnabled && getLastAppliedIndex() - snapshotIndex.get() > 0; } return state == State.RUNNING && getStateMachineLastAppliedIndex() - snapshotIndex.get() >= autoSnapshotThreshold;
