Repository navigation
feat(storage): walk-gated recompaction (RFC-009) - #229
Conversation
Greptile SummaryThe PR replaces delete-index-gated recompaction with rate-limited segment liveness walks and uses delete-index credits only for prioritization.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains; the previous audit invalidation races are no longer reachable, and the recompaction metadata CAS preserves concurrent replacements while safely removing the old segment. Important Files Changed
Reviews (6): Last reviewed commit: "fix(storage): publish compaction metadat..." | Re-trigger Greptile |
95aebc4 to
8f5ef1f
Compare
24b927d to
654136d
Compare
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
The delete index — an incremental counter every historical credit bug corrupted permanently — no longer decides recompaction. Each pass walks a budget of candidate segments (hint-prioritized, staleness-rotated) and derives dead bytes from ground truth: footer totals minus the entries whose metadata rows still point at the segment, the same liveness test recompactSegment applies before copying. The derivation gates the rewrite directly; the index is only a prioritization hint, so a wrong hint costs one ~ms walk instead of leaked or rewritten data. Walks are cheap by construction: only objects above the 64 KiB inline threshold reach segments, capping entries per segment at ~4,096 (typically ~256). Strict derivation failure handling: only ErrMetadataNotFound counts dead (transient lookup failures abort the walk — also applied to recompactSegment's copy loop, where the old error-as-deleted pattern could drop a live entry); the iterator reads truncation as clean EOF, so the walk cross-checks its entry count against the footer and aborts on damage. Orphaned dead bytes with no credit at all — the invisible case behind the ORD and customer ENOSPC incidents — are reclaimed within one rotation. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
…a deletes DeleteKey staged reclamation BEFORE its delete committed (a failed write left credit and a queued file for a still-live row) and the TTL cleaner staged it after (a crash between commit and staging lost the credit forever). Both now merge a segment value's delete-index credit in the SAME batch as the metadata delete, while raw files keep post-commit queueing. Credits are prioritization hints under RFC-009, so this is a quality-of-ordering fix, not a correctness dependency. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
… walk I/O Three refinements from CI and review: - Hint growth since a segment's last walk bypasses the re-walk interval, so credited deletions are re-derived on the very next pass (the old gate's reactivity); the interval only bounds re-walks when the hint is unchanged. Fixes the integration recompaction tests, where deletions after a first clean walk waited out the full interval. - A derived-fragmented segment whose rewrite fails is retried next pass instead of being parked for the interval — reclaim matters most under the disk pressure that makes rewrites fail (Cursor Bugbot). - Walk reads draw from the shared compaction I/O limiter (one ~4 KiB page cost per entry visited — header and key share a page), so walks, compaction, and recompaction together stay under the configured budget and a burst of small walk reads is paced instead of landing as an IOPS spike. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
With walk reads charged to the shared compaction I/O limiter, the count cap was redundant for I/O safety: even a whole-fleet pass (first after restart, or hourly re-expiry) is paced by the budget rather than bursty, and hint-priority ordering means reclaim-worthy segments are handled first regardless. Walk volume is governed by two mechanisms instead of three: the re-walk interval (with hint-growth bypass) and the limiter. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
Without a count cap a rate-limited whole-fleet pass can outlast the walk interval; pass-start timestamps would then expire retained derivations prematurely and re-walk the fleet every pass. Flagged by Cursor Bugbot. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
…nal Put copyEntry marshalled the ValueMessage it read at walk time and wrote it with wb.Put at the end of the (rate-limited, minutes-long) segment pass. A Put that replaced the key inside that window was clobbered: the row rolled back to the recompacted copy of the OLD bytes and the newly written value became unreachable — silent data loss on a concurrent overwrite, pre-existing on main and widened by the I/O budget. Publish through the same compare-and-swap merge the file compactor uses for raw -> segment migrations, extended to accept a SEGMENT base whose SegmentPath matches the operand's precondition (closed segments are append-only, so a path match proves the row is unchanged since the copy's read; raw and segment paths live in disjoint directories, so the two preconditions cannot cross-match). On mismatch the operand is dropped, the newer write wins, and the copied bytes become dead space the walk-gated recompactor reclaims. Flagged by Greptile. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9 Signed-off-by: Ovais Tariq <[email protected]>
c8aaa4c to
a37c6db
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a37c6db. Configure here.
A CompactFiles run iterates the whole compaction index with no per-run
byte cap, committing its metadata batch only at the end. Destination
segments are finalized along the way, so under a large backlog and a
rate-limited copy those segments became closed — recompaction- and
walk-eligible — while every entry inside them still had metadata
pointing at a raw file. A liveness derivation reads such a segment as
fully dead, copies nothing, and removes it; the run's later commit then
publishes rows referencing a deleted file. The 2 h age gate does not
bound this: the finalize-to-commit window is the run's duration, which
at 16 MiB/s over a 200 GB backlog is hours.
Commit before finalizing instead, so 'closed segment implies published
metadata' is structural rather than a timing assumption. commit() syncs
the segment before writing metadata, preserving sync-before-publish;
source raw files are queued for deletion only after their replacement
metadata is durable, at rollover as at end of run. Crash recovery also
improves: less work lost and fewer orphaned segment bytes per crash.
TestCompactFiles_PublishesBeforeFinalizingSegment pins the invariant by
observing a rollover mid-run; it fails without this change ('holds 1
entries but only 0 have published metadata'). Flagged by Cursor Bugbot.
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9
Signed-off-by: Ovais Tariq <[email protected]>
|
🎉 This PR is included in version 1.11.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |

Implements RFC-009 (included). Design evolved through review: a scan-based reconciliation and then a separate audit-corrects-index mechanism were both prototyped and rejected in favor of this simpler unification.
The change
Recompaction is no longer gated by the delete index. Each pass walks a budget of candidate segments (default 8/pass, hint-prioritized, staleness-rotated) and derives dead bytes from ground truth — footer totals minus the entries whose metadata rows still point at the segment, the exact liveness test
recompactSegmentalready applies before copying. The derivation gates the rewrite directly, in the same pass.The delete index demotes to a prioritization hint. Its credits order which segments get walked first (hot churn reclaims fast; rotation covers the rest in hours). No correctness depends on it anymore: the historical orphan backlog (the ORD and customer ENOSPC incidents), credits lost to any future bug, and inflated credits from racing writers are all absorbed by the walk within one rotation. A wrong hint costs one ~ms walk instead of leaked or rewritten data.
Why walks are cheap enough to be the gate: only objects above the 64 KiB inline threshold reach segments, so a 256 MiB segment holds ≤ ~4,096 entries (typically ~256 at TAG's 1 MiB blocks) — a header-hop plus that many point lookups is single-digit milliseconds.
Also in this PR
ErrMetadataNotFoundcounts dead; transient lookup failures abort the walk — and the same fix is applied torecompactSegment's copy loop, where the pre-existing error-as-deleted pattern could drop a live entry (Bugbot finding, data-loss-adjacent). Truncated segments are caught by a walk-vs-footer cross-check (the iterator reads truncation as clean EOF).DeleteKey(staged before commit — wrong direction) and the TTL cleaner (staged after — crash window) now merge segment credit in the same batch as the metadata delete, matching fix(storage): reclaim the replaced value's bytes on Put overwrite #228's putLow discipline.Testing
TestWalkGatedRecompaction_ReclaimsWithoutAnyCredit— the headline: dead bytes with no credit at all are derived and reclaimedTestWalkGatedRecompaction_IgnoresInflatedHint— a bogus hint cannot rewrite a live segmentTestWalk_AbortsOnTruncatedSegment,TestWalk_IntervalBoundsRewalks./storage/...11 packages green, vet cleanPairs with #226 + #228 (merged): together they restore end-to-end dead-byte reclaim with no dependence on incremental-counter perfection.
🤖 Generated with Claude Code
https://claude.ai/code/session_01L5DLvDTo9dhD38YQXhHpU9
Note
Cursor Bugbot is generating a summary for commit 3731a07. Configure here.