Class WalCompactor
java.lang.Object
com.loomcache.server.persistence.WalCompactor
- All Implemented Interfaces:
AutoCloseable
Background WAL compaction service.
Automatically compacts the Write-Ahead Log by:
- Monitoring WAL file size
- Creating snapshots when size exceeds threshold
- Truncating old WAL entries atomically
- Running on a virtual thread for efficient async operation
Thread safety:
- Uses virtual threads for background tasks
- Atomic operations for snapshots and truncation
- Statistics use
LongAdderfor lock-free updates
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordSnapshot boundary and state to use for one automatic compaction.static final recordCompaction statistics record. -
Constructor Summary
ConstructorsConstructorDescriptionWalCompactor(String nodeId, WalWriter walWriter, SnapshotManager snapshotManager, long compactionThreshold, long snapshotIntervalMs) Create a WAL compactor.WalCompactor(String nodeId, WalWriter walWriter, SnapshotManager snapshotManager, long compactionThreshold, long snapshotIntervalMs, @Nullable Supplier<@Nullable WalCompactor.CompactionSnapshot> automaticCompactionSnapshotSupplier) Create a WAL compactor with an optional automatic compaction snapshot callback. -
Method Summary
Modifier and TypeMethodDescriptionvoidDisable scheduled automatic compaction.voidclose()Close the compactor and release resources.voidforceCompaction(long snapshotIndex, long snapshotTerm, Supplier<byte[]> stateSupplier) Manually force a compaction regardless of size threshold.Get current compaction statistics.longGet the compaction threshold in bytes.longGet the snapshot interval in milliseconds.voidsetAutomaticCompactionSnapshotSupplier(Supplier<@Nullable WalCompactor.CompactionSnapshot> supplier) Configure the callback used by scheduled automatic compaction.voidstart(long checkIntervalMs) Start background compaction monitoring.voidstop()Stop background compaction monitoring.
-
Constructor Details
-
WalCompactor
public WalCompactor(String nodeId, WalWriter walWriter, SnapshotManager snapshotManager, long compactionThreshold, long snapshotIntervalMs) Create a WAL compactor.- Parameters:
nodeId- The node ID for loggingwalWriter- The WAL writer instancesnapshotManager- The snapshot manager instancecompactionThreshold- Bytes threshold to trigger compaction (e.g., 100MB)snapshotIntervalMs- Minimum ms between snapshots
-
WalCompactor
public WalCompactor(String nodeId, WalWriter walWriter, SnapshotManager snapshotManager, long compactionThreshold, long snapshotIntervalMs, @Nullable Supplier<@Nullable WalCompactor.CompactionSnapshot> automaticCompactionSnapshotSupplier) Create a WAL compactor with an optional automatic compaction snapshot callback.- Parameters:
nodeId- The node ID for loggingwalWriter- The WAL writer instancesnapshotManager- The snapshot manager instancecompactionThreshold- Bytes threshold to trigger compactionsnapshotIntervalMs- Minimum ms between automatic snapshotsautomaticCompactionSnapshotSupplier- Supplier for safe automatic compaction snapshots, or null to require manual compaction
-
-
Method Details
-
setAutomaticCompactionSnapshotSupplier
public void setAutomaticCompactionSnapshotSupplier(Supplier<@Nullable WalCompactor.CompactionSnapshot> supplier) Configure the callback used by scheduled automatic compaction.The callback must only return a snapshot whose index and term are known committed/applied and whose state bytes match that boundary. Returning
nullskips the current automatic compaction attempt.- Parameters:
supplier- snapshot callback
-
clearAutomaticCompactionSnapshotSupplier
public void clearAutomaticCompactionSnapshotSupplier()Disable scheduled automatic compaction. ManualforceCompaction(long, long, Supplier)remains available. -
start
public void start(long checkIntervalMs) Start background compaction monitoring.Spawns a virtual thread that periodically checks WAL size and triggers compaction when threshold is exceeded.
- Parameters:
checkIntervalMs- Interval between size checks (milliseconds)
-
stop
public void stop()Stop background compaction monitoring. -
forceCompaction
public void forceCompaction(long snapshotIndex, long snapshotTerm, Supplier<byte[]> stateSupplier) throws IOException Manually force a compaction regardless of size threshold.This operation:
- Reads all entries from WAL
- Creates a snapshot of the state
- Atomically truncates WAL
- Parameters:
snapshotIndex- The committed/applied index included in the snapshotsnapshotTerm- The term ofsnapshotIndexstateSupplier- Supplier providing the current state machine state as bytes- Throws:
IOException- If compaction fails
-
getCompactionStats
Get current compaction statistics.- Returns:
- CompactionStats record with current metrics
-
close
Close the compactor and release resources.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- If close fails
-
getCompactionThreshold
public long getCompactionThreshold()Get the compaction threshold in bytes.- Returns:
- The threshold value
-
getSnapshotInterval
public long getSnapshotInterval()Get the snapshot interval in milliseconds.- Returns:
- The interval value
-