Skip to content

Commit

Permalink
RATIS-1862 Add the parameter whether to take Snapshot when stopping t…
Browse files Browse the repository at this point in the history
…o adapt to different services (#896)
  • Loading branch information
OneSizeFitsQuorum authored Jul 28, 2023
1 parent 45772bb commit 0b79689
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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_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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -103,6 +105,7 @@ enum State {
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);
Expand Down Expand Up @@ -317,7 +320,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;
Expand Down

0 comments on commit 0b79689

Please sign in to comment.