pub struct CompactionManager { /* private fields */ }Expand description
Manages Parquet file compaction for optimal storage and query performance.
Step 4 of the sustainable data strategy moved compaction from a
global-stream pass to a per-tenant pass. Each invocation iterates
the tenants discovered under <storage_dir>/<tenant>/... and
emits a snapshot.<tenant>.<from>-<to>.parquet per qualifying
chunk. Snapshot files are written atomically (tmp + rename) and
their constituent raw files are removed only after the rename
succeeds — so a mid-compaction crash leaves data intact.
Implementations§
Source§impl CompactionManager
impl CompactionManager
Sourcepub fn new(storage_dir: impl Into<PathBuf>, config: CompactionConfig) -> Self
pub fn new(storage_dir: impl Into<PathBuf>, config: CompactionConfig) -> Self
Create a new compaction manager
Sourcepub fn last_compaction(&self) -> Option<DateTime<Utc>>
pub fn last_compaction(&self) -> Option<DateTime<Utc>>
When the last sweep finished, across restarts.
Sourcepub fn last_sweep_paused(&self) -> bool
pub fn last_sweep_paused(&self) -> bool
Whether the last sweep left tenants unvisited, across restarts.
Sourcepub fn delay_until_next_sweep(&self, settle: Duration) -> Duration
pub fn delay_until_next_sweep(&self, settle: Duration) -> Duration
How long to wait before the next sweep.
An overdue or never-run sweep is scheduled after settle rather than
after a full interval, so a process restarting more often than its
interval still compacts. settle keeps a boot-time sweep off WAL
recovery’s disk.
Sourcepub fn should_compact(&self) -> bool
pub fn should_compact(&self) -> bool
Check if compaction should run
Sourcepub fn compact(&self) -> Result<CompactionResult>
pub fn compact(&self) -> Result<CompactionResult>
Perform compaction across every discovered tenant.
Iterates the tenants under <storage_dir>/<tenant>/..., calls
compact_tenant for each, and aggregates the results. Step 4
of the sustainable data strategy: per-tenant compaction
instead of global, keyed off the per-tenant directory tree
Step 1 introduced.
Errors compacting one tenant are logged but don’t abort the pass — other tenants still get compacted. The aggregate result reflects what actually completed.
The sweep stops starting work once max_pass_seconds is spent and
resumes on the next tick. Tenants are visited in a stable order, so a
sweep that runs out of budget always leaves the same tail — the
per-pass bound is on time, and progress comes from the files each
group removes.
Sourcepub fn sweep_cursor(&self) -> Option<String>
pub fn sweep_cursor(&self) -> Option<String>
Where the next sweep will start, or None before the first one.
Diagnostic: a cursor that never moves off one tenant means that tenant’s backlog is larger than a single pass’s budget.
Sourcepub fn compact_until(
&self,
deadline: Option<Instant>,
) -> Result<CompactionResult>
pub fn compact_until( &self, deadline: Option<Instant>, ) -> Result<CompactionResult>
Self::compact with an explicit deadline in place of the one
max_pass_seconds derives.
Sourcepub fn compact_tenant(&self, tenant_id: &str) -> Result<CompactionResult>
pub fn compact_tenant(&self, tenant_id: &str) -> Result<CompactionResult>
Compact one tenant’s event files into snapshot files under that tenant’s partition.
Per-tenant pipeline:
- List
<storage>/<tenant>/...*.parquet, keeping an existingsnapshot.*only while it is still undersmall_file_thresholdso partial passes merge upward. - Apply the configured strategy (size / time / full) to pick candidates.
- Split the candidates into groups bounded by
max_input_bytes_per_snapshotandmax_input_files_per_snapshot, and compact each group on its own. One group’s events are resident at a time, so a tenant’s size sets how many snapshots a pass writes, never how much memory it needs.
Each group is independently crash-safe: its snapshot is renamed into place before any of its own inputs is deleted, and a crash between groups leaves the earlier groups compacted and the later ones untouched.
Sourcepub fn compact_tenant_until(
&self,
tenant_id: &str,
deadline: Option<Instant>,
) -> Result<CompactionResult>
pub fn compact_tenant_until( &self, tenant_id: &str, deadline: Option<Instant>, ) -> Result<CompactionResult>
compact_tenant with a sweep deadline. Checked between groups, so the
tenant’s remaining groups are left for the next tick rather than
abandoned mid-group — a group either publishes its snapshot and
deletes its inputs or does neither.
Sourcepub fn stats(&self) -> CompactionStats
pub fn stats(&self) -> CompactionStats
Get compaction statistics
Sourcepub fn config(&self) -> &CompactionConfig
pub fn config(&self) -> &CompactionConfig
Get configuration
Sourcepub fn compact_now(&self) -> Result<CompactionResult>
pub fn compact_now(&self) -> Result<CompactionResult>
Trigger manual compaction