Class StateMachineSnapshotManager

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

public class StateMachineSnapshotManager extends Object
Manages state machine snapshots for Raft log compaction.

Provides: - Full snapshot of all registered data structures - Incremental snapshots (only changed entries) - Compression with GZIP - CRC32 integrity verification - Snapshot metadata tracking (index, term, timestamp, etc.) - Concurrent snapshot creation using virtual threads

  • Field Details

  • Constructor Details

    • StateMachineSnapshotManager

      public StateMachineSnapshotManager(String nodeId, Path snapshotDirectory) throws IOException
      Create a snapshot manager with a dedicated default KryoSerializer. Prefer the overload that accepts a shared serializer so the entire server shares one registration registry (Hazelcast-style SerializationService).
      Throws:
      IOException
    • StateMachineSnapshotManager

      public StateMachineSnapshotManager(String nodeId, Path snapshotDirectory, KryoSerializer serializer) throws IOException
      Create a snapshot manager that shares the given KryoSerializer with the rest of the server. This is the recommended entry point: a single serializer with a consistent class registry is shared across all snapshot-producing components (see MapSnapshot).
      Throws:
      IOException
  • Method Details

    • registerWith

      public static void registerWith(KryoSerializer serializer)
      Register StateMachineSnapshotManager.FullSnapshot and DeltaSnapshot with the given Kryo serializer. Idempotent. Call once per serializer at startup so the snapshot manager and any auxiliary readers share the same registrations.
    • takeFullSnapshot

      public StateMachineSnapshotManager.SnapshotMetadata takeFullSnapshot(long snapshotIndex, long snapshotTerm, Map<String, ? extends Snapshotable> dataStructures) throws IOException
      Take a full snapshot of all registered data structures.
      Parameters:
      snapshotIndex - the Raft log index
      snapshotTerm - the Raft term
      dataStructures - map of snapshot ID to Snapshotable instances
      Returns:
      SnapshotMetadata describing the created snapshot
      Throws:
      IOException - if snapshot creation fails
    • restoreFullSnapshot

      public void restoreFullSnapshot(long snapshotIndex, Map<String, ? extends Snapshotable> dataStructures) throws IOException
      Restore all data structures from a snapshot.
      Parameters:
      snapshotIndex - the Raft log index to restore
      dataStructures - map of snapshot ID to Snapshotable instances to restore into
      Throws:
      IOException - if restoration fails
    • takeDeltaSnapshot

      public StateMachineSnapshotManager.SnapshotMetadata takeDeltaSnapshot(long baseSnapshotIndex, long snapshotIndex, long snapshotTerm, Map<String, ? extends Snapshotable> dataStructures) throws IOException
      Take a delta (incremental) snapshot of only changed data structures.

      For structures that support delta snapshots and have dirty state, only the changed entries are serialized. Structures that don't support delta or require a full snapshot are fully serialized. The result is stored as a DeltaSnapshot record.

      Parameters:
      baseSnapshotIndex - the Raft index of the base full snapshot this delta builds on
      snapshotIndex - the Raft log index for this delta
      snapshotTerm - the current Raft term
      dataStructures - map of snapshot ID to Snapshotable instances
      Returns:
      SnapshotMetadata describing the delta snapshot
      Throws:
      IOException - if snapshot creation fails
    • restoreFromChain

      public void restoreFromChain(SnapshotChain chain, Map<String, ? extends Snapshotable> dataStructures) throws IOException
      Restore data structures from a snapshot chain (base + deltas).

      Materializes the chain into a single full snapshot and restores each structure.

      Parameters:
      chain - the snapshot chain to restore from
      dataStructures - the data structures to restore into
      Throws:
      IOException - if restoration fails
    • loadSnapshot

      public Optional<SnapshotManager.Snapshot> loadSnapshot(long snapshotIndex) throws IOException
      Load a snapshot by index.
      Parameters:
      snapshotIndex - the Raft log index
      Returns:
      Optional containing the snapshot if found
      Throws:
      IOException - if read fails
    • getLatestSnapshotInfo

      public Optional<SnapshotManager.SnapshotInfo> getLatestSnapshotInfo() throws IOException
      Get the latest snapshot without loading full data.
      Returns:
      Optional containing SnapshotInfo if available
      Throws:
      IOException - if read fails
    • cleanup

      public void cleanup(int maxSnapshots) throws IOException
      Clean up old snapshots, keeping only the latest N.
      Parameters:
      maxSnapshots - the maximum number of snapshots to keep
      Throws:
      IOException - if cleanup fails