Class SnapshotScheduler

java.lang.Object
com.loomcache.server.persistence.SnapshotScheduler

public class SnapshotScheduler extends Object
Schedules and manages state machine snapshots for Raft log compaction.

Automatically triggers snapshots after a configured number of committed entries, using virtual threads to avoid blocking the Raft processing thread.

Maintains bounded storage by retaining only the latest N snapshots.

  • Constructor Details

    • SnapshotScheduler

      public SnapshotScheduler(StateMachineSnapshotManager snapshotManager, long snapshotIntervalEntries, int maxSnapshotsToRetain, int maxDeltasBetweenFullSnapshots)
      Create a snapshot scheduler with custom configuration.
      Parameters:
      snapshotManager - the underlying snapshot manager
      snapshotIntervalEntries - trigger snapshot after this many committed entries (default: 10000)
      maxSnapshotsToRetain - keep at most this many snapshots (default: 3)
      maxDeltasBetweenFullSnapshots - max delta snapshots before forcing a full snapshot (default: 3)
    • SnapshotScheduler

      public SnapshotScheduler(StateMachineSnapshotManager snapshotManager, long snapshotIntervalEntries, int maxSnapshotsToRetain)
      Create a snapshot scheduler with custom configuration and default delta count (3).
      Parameters:
      snapshotManager - the underlying snapshot manager
      snapshotIntervalEntries - trigger snapshot after this many committed entries
      maxSnapshotsToRetain - keep at most this many snapshots
    • SnapshotScheduler

      public SnapshotScheduler(StateMachineSnapshotManager snapshotManager)
      Create a snapshot scheduler with default configuration. Default: snapshot every 10,000 committed entries, retain 3 snapshots, max 3 deltas.
      Parameters:
      snapshotManager - the underlying snapshot manager
  • Method Details

    • maybeSnapshot

      public void maybeSnapshot(long currentCommittedIndex, long currentTerm, Map<String, ? extends Snapshotable> dataStructures)
      Check if a snapshot should be triggered based on the committed index. If needed, trigger snapshot asynchronously using a virtual thread.
      Parameters:
      currentCommittedIndex - the current Raft committed log index
      currentTerm - the current Raft term
      dataStructures - the data structures to snapshot
    • getStats

      Get current scheduler statistics.
      Returns:
      SnapshotSchedulerStats with current metrics
    • getLastSnapshotIndex

      public long getLastSnapshotIndex()
      Get the index of the last snapshot taken.
      Returns:
      the last snapshot index, or -1 if no snapshots taken yet
    • setLastSnapshotIndex

      public void setLastSnapshotIndex(long index)
      Set the last snapshot index (useful for recovery).
      Parameters:
      index - the index to set