Class StateMachineSnapshotManager
java.lang.Object
com.loomcache.server.persistence.StateMachineSnapshotManager
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFull snapshot of all data structures.static final recordMetadata for a state machine snapshot. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intKryo registration ID forDeltaSnapshot.static final intKryo registration ID forStateMachineSnapshotManager.FullSnapshot. -
Constructor Summary
ConstructorsConstructorDescriptionStateMachineSnapshotManager(String nodeId, Path snapshotDirectory) Create a snapshot manager with a dedicated defaultKryoSerializer.StateMachineSnapshotManager(String nodeId, Path snapshotDirectory, KryoSerializer serializer) Create a snapshot manager that shares the givenKryoSerializerwith the rest of the server. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanup(int maxSnapshots) Clean up old snapshots, keeping only the latest N.Get the latest snapshot without loading full data.loadSnapshot(long snapshotIndex) Load a snapshot by index.static voidregisterWith(KryoSerializer serializer) RegisterStateMachineSnapshotManager.FullSnapshotandDeltaSnapshotwith the given Kryo serializer.voidrestoreFromChain(SnapshotChain chain, Map<String, ? extends Snapshotable> dataStructures) Restore data structures from a snapshot chain (base + deltas).voidrestoreFullSnapshot(long snapshotIndex, Map<String, ? extends Snapshotable> dataStructures) Restore all data structures from a snapshot.takeDeltaSnapshot(long baseSnapshotIndex, long snapshotIndex, long snapshotTerm, Map<String, ? extends Snapshotable> dataStructures) Take a delta (incremental) snapshot of only changed data structures.takeFullSnapshot(long snapshotIndex, long snapshotTerm, Map<String, ? extends Snapshotable> dataStructures) Take a full snapshot of all registered data structures.
-
Field Details
-
FULL_SNAPSHOT_KRYO_ID
public static final int FULL_SNAPSHOT_KRYO_IDKryo registration ID forStateMachineSnapshotManager.FullSnapshot. Above reserved range (10..38).- See Also:
-
DELTA_SNAPSHOT_KRYO_ID
public static final int DELTA_SNAPSHOT_KRYO_IDKryo registration ID forDeltaSnapshot. Above reserved range (10..38).- See Also:
-
-
Constructor Details
-
StateMachineSnapshotManager
Create a snapshot manager with a dedicated defaultKryoSerializer. 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 givenKryoSerializerwith 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 (seeMapSnapshot).- Throws:
IOException
-
-
Method Details
-
registerWith
RegisterStateMachineSnapshotManager.FullSnapshotandDeltaSnapshotwith 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 indexsnapshotTerm- the Raft termdataStructures- 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 restoredataStructures- 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
DeltaSnapshotrecord.- Parameters:
baseSnapshotIndex- the Raft index of the base full snapshot this delta builds onsnapshotIndex- the Raft log index for this deltasnapshotTerm- the current Raft termdataStructures- 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 fromdataStructures- the data structures to restore into- Throws:
IOException- if restoration fails
-
loadSnapshot
Load a snapshot by index.- Parameters:
snapshotIndex- the Raft log index- Returns:
- Optional containing the snapshot if found
- Throws:
IOException- if read fails
-
getLatestSnapshotInfo
Get the latest snapshot without loading full data.- Returns:
- Optional containing SnapshotInfo if available
- Throws:
IOException- if read fails
-
cleanup
Clean up old snapshots, keeping only the latest N.- Parameters:
maxSnapshots- the maximum number of snapshots to keep- Throws:
IOException- if cleanup fails
-