Class SnapshotManager
java.lang.Object
com.loomcache.server.persistence.SnapshotManager
Manages snapshots for WAL log compaction.
Provides:
- Snapshot creation with CRC32 integrity verification
- Snapshot loading with corruption detection
- Metadata-only loading (without full data)
- Automatic cleanup to maintain bounded snapshot storage
Snapshot format on disk: [lastIncludedIndex(8)][lastIncludedTerm(8)][timestamp(8)][dataLength(4)][data(variable)][checksum(4 CRC32)]
Snapshots are named: snapshot-{nodeId}-{index}.bin
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordSnapshot record containing index, term, timestamp, checksum, and data.static final recordSnapshot metadata without full data payload. -
Constructor Summary
ConstructorsConstructorDescriptionSnapshotManager(String nodeId, Path snapshotDirectory) Initialize the SnapshotManager. -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanup(int maxSnapshots) Clean up old snapshots, keeping only the latest N.voidcreateSnapshot(long lastIncludedIndex, long lastIncludedTerm, byte[] data) Create a snapshot of the current state.voiddeleteSnapshot(long index) Delete a specific snapshot by index.Get metadata for the latest snapshot without loading the full data.Load the latest snapshot from disk.
-
Constructor Details
-
SnapshotManager
Initialize the SnapshotManager.- Parameters:
nodeId- The node identifier (must not be null or empty)snapshotDirectory- The directory path for snapshot files (must not be null)- Throws:
IOException- If directory creation failsIllegalArgumentException- If parameters are invalid
-
-
Method Details
-
createSnapshot
public void createSnapshot(long lastIncludedIndex, long lastIncludedTerm, byte[] data) throws IOException Create a snapshot of the current state.The snapshot is written atomically to disk with CRC32 checksum for corruption detection on load.
- Parameters:
lastIncludedIndex- The index of the last log entry included in this snapshotlastIncludedTerm- The term of lastIncludedIndexdata- The snapshot data (state machine state)- Throws:
IOException- If write failsIllegalArgumentException- If indices or term are invalid
-
loadLatestSnapshot
Load the latest snapshot from disk.Validates CRC32 checksum and returns the snapshot if valid. Returns empty if no valid snapshot exists.
- Returns:
- Optional containing the latest snapshot if found and valid
- Throws:
IOException- If read fails
-
getSnapshotInfo
Get metadata for the latest snapshot without loading the full data.- Returns:
- Optional containing snapshot metadata if available
- Throws:
IOException- If read fails
-
cleanup
Clean up old snapshots, keeping only the latest N.Useful for managing disk space by removing snapshots that are no longer needed.
- Parameters:
maxSnapshots- The maximum number of snapshots to keep- Throws:
IOException- If file operations fail
-
deleteSnapshot
Delete a specific snapshot by index.- Parameters:
index- The snapshot index to delete- Throws:
IOException- If deletion fails
-