From e191d6936d2f676cddcc383c0d0f8f2ea9ebeb56 Mon Sep 17 00:00:00 2001 From: OneSizeFitQuorum Date: Fri, 28 Jul 2023 13:28:55 +0800 Subject: [PATCH 1/2] finish Signed-off-by: OneSizeFitQuorum --- .../apache/ratis/server/RaftServerConfigKeys.java | 12 ++++++++++++ .../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 2fc025535d..fc237165c6 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 @@ -586,6 +586,18 @@ static void setAutoTriggerEnabled(RaftProperties properties, boolean autoTrigger setBoolean(properties::setBoolean, AUTO_TRIGGER_ENABLED_KEY, autoTriggerEnabled); } + /** whether trigger snapshot when stop raft server */ + String TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY = PREFIX + ".trigger.snapshot.when.stop.enabled"; + /** by default let the state machine to trigger snapshot when stop */ + boolean TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_DEFAULT = true; + static boolean triggerSnapshotWhenStopEnabled(RaftProperties properties) { + return getBoolean(properties::getBoolean, + TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY, TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_DEFAULT, getDefaultLog()); + } + static void setTriggerSnapshotWhenStopEnabled(RaftProperties properties, boolean triggerSnapshotWhenStopEnabled) { + setBoolean(properties::setBoolean, TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY, triggerSnapshotWhenStopEnabled); + } + /** 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 3027052ea7..41d94c3672 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 @@ -72,6 +72,8 @@ enum State { private final RaftServerImpl server; private final RaftLog raftLog; + private final boolean triggerSnapshotWhenStopEnabled; + private final Long autoSnapshotThreshold; private final boolean purgeUptoSnapshotIndex; @@ -103,6 +105,7 @@ enum State { this.appliedIndex = new RaftLogIndex("appliedIndex", lastAppliedIndex); this.snapshotIndex = new RaftLogIndex("snapshotIndex", lastAppliedIndex); + this.triggerSnapshotWhenStopEnabled = RaftServerConfigKeys.Snapshot.triggerSnapshotWhenStopEnabled(properties); final boolean autoSnapshot = RaftServerConfigKeys.Snapshot.autoTriggerEnabled(properties); this.autoSnapshotThreshold = autoSnapshot? RaftServerConfigKeys.Snapshot.autoTriggerThreshold(properties): null; final int numSnapshotFilesRetained = RaftServerConfigKeys.Snapshot.retentionFileNum(properties); @@ -315,7 +318,7 @@ private boolean shouldTakeSnapshot() { 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; From ea4b55b7679bccdc4de9af4190162f866a2e2279 Mon Sep 17 00:00:00 2001 From: OneSizeFitQuorum Date: Fri, 28 Jul 2023 14:33:36 +0800 Subject: [PATCH 2/2] fix review Signed-off-by: OneSizeFitQuorum --- .../apache/ratis/server/RaftServerConfigKeys.java | 12 ++++++------ .../ratis/server/impl/StateMachineUpdater.java | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) 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 fc237165c6..211edd7960 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 @@ -587,15 +587,15 @@ static void setAutoTriggerEnabled(RaftProperties properties, boolean autoTrigger } /** whether trigger snapshot when stop raft server */ - String TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY = PREFIX + ".trigger.snapshot.when.stop.enabled"; + String TRIGGER_WHEN_STOP_ENABLED_KEY = PREFIX + ".trigger-when-stop.enabled"; /** by default let the state machine to trigger snapshot when stop */ - boolean TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_DEFAULT = true; - static boolean triggerSnapshotWhenStopEnabled(RaftProperties properties) { + boolean TRIGGER_WHEN_STOP_ENABLED_DEFAULT = true; + static boolean triggerWhenStopEnabled(RaftProperties properties) { return getBoolean(properties::getBoolean, - TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY, TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_DEFAULT, getDefaultLog()); + TRIGGER_WHEN_STOP_ENABLED_KEY, TRIGGER_WHEN_STOP_ENABLED_DEFAULT, getDefaultLog()); } - static void setTriggerSnapshotWhenStopEnabled(RaftProperties properties, boolean triggerSnapshotWhenStopEnabled) { - setBoolean(properties::setBoolean, TRIGGER_SNAPSHOT_WHEN_STOP_ENABLED_KEY, triggerSnapshotWhenStopEnabled); + 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. */ 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 41d94c3672..cb73b285ef 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 @@ -105,7 +105,7 @@ enum State { this.appliedIndex = new RaftLogIndex("appliedIndex", lastAppliedIndex); this.snapshotIndex = new RaftLogIndex("snapshotIndex", lastAppliedIndex); - this.triggerSnapshotWhenStopEnabled = RaftServerConfigKeys.Snapshot.triggerSnapshotWhenStopEnabled(properties); + 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);