Class SnapshotStore
java.lang.Object
com.loomcache.server.persistence.SnapshotStore
Snapshot storage for Raft log compaction.
Saves/loads snapshots to disk: snapshot-{nodeId}-{index}.bin Format: [snapshotIndex(8)][snapshotTerm(8)][dataLength(4)][data][sha256Checksum(32)]
Records snapshot metadata: index, term, data, and SHA-256 checksum for validation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordSnapshot metadata record with SHA-256 checksum for validation.static final record -
Constructor Summary
ConstructorsConstructorDescriptionSnapshotStore(String nodeId, Path snapshotDirectory) Initialize snapshot store.SnapshotStore(String nodeId, Path snapshotDirectory, @Nullable LoomMetrics metrics) Initialize snapshot store with metrics. -
Method Summary
Modifier and TypeMethodDescriptionvoiddiscardStagedSnapshot(@Nullable SnapshotStore.StagedSnapshot stagedSnapshot) Load the latest snapshot from disk.loadLatestSnapshot(boolean allowFallbackToOlderSnapshot) Load the latest snapshot, optionally refusing fallback when the newest snapshot exists but fails validation.voidpromoteStagedSnapshot(SnapshotStore.StagedSnapshot stagedSnapshot) voidsaveSnapshot(long index, long term, byte[] data) Save a snapshot to disk with SHA-256 checksum.stageSnapshot(long index, long term, byte[] data)
-
Constructor Details
-
SnapshotStore
Initialize snapshot store.- Parameters:
nodeId- The node identifiersnapshotDirectory- The directory path for snapshots- Throws:
IOException- If directory creation fails
-
SnapshotStore
public SnapshotStore(String nodeId, Path snapshotDirectory, @Nullable LoomMetrics metrics) throws IOException Initialize snapshot store with metrics.- Parameters:
nodeId- The node identifiersnapshotDirectory- The directory path for snapshotsmetrics- Optional metrics tracker- Throws:
IOException- If directory creation fails
-
-
Method Details
-
saveSnapshot
Save a snapshot to disk with SHA-256 checksum.Format: [snapshotIndex(8)][snapshotTerm(8)][dataLength(4)][data][sha256Checksum(32)]
- Parameters:
index- The snapshot indexterm- The snapshot termdata- The snapshot data (must not be null; use an empty array for a zero-length snapshot)- Throws:
NullPointerException- If data is nullIOException- If write fails
-
stageSnapshot
public SnapshotStore.StagedSnapshot stageSnapshot(long index, long term, byte[] data) throws IOException - Throws:
IOException
-
promoteStagedSnapshot
- Throws:
IOException
-
discardStagedSnapshot
-
loadLatestSnapshot
Load the latest snapshot from disk.Finds the snapshot file with the highest index for this node. Validates SHA-256 checksum if present.
- Returns:
- Optional containing SnapshotMetadata if found and valid, empty otherwise
- Throws:
IOException- If read fails
-
loadLatestSnapshot
public Optional<SnapshotStore.SnapshotMetadata> loadLatestSnapshot(boolean allowFallbackToOlderSnapshot) throws IOException Load the latest snapshot, optionally refusing fallback when the newest snapshot exists but fails validation.- Parameters:
allowFallbackToOlderSnapshot- whether older snapshots may be used after the latest candidate is invalid- Returns:
- Optional containing SnapshotMetadata if found and valid, empty otherwise
- Throws:
IOException- If read fails or fallback is disabled and the latest candidate is invalid
-