Repository navigation
fix(storage): run_blob_gc unlinks sharded path + dry-run + history (#1190) - #1318
Conversation
Problem
- ``run_blob_gc`` unlinked ``{blob_dir}/{full_hash}`` rather than the
sharded ``{blob_dir}/{prefix}/{remainder}`` where blobs actually live.
- ``missing_ok=True`` swallowed the inevitable ENOENT and the ``deleted``
counter still incremented, so GC reports lied: counts said reclaimed,
orphan blobs stayed on disk.
Solution
- ``polylogue/storage/blob_gc.py``: unlink the sharded path; treat
FileNotFoundError as ``skipped_missing`` (do NOT bump deleted);
separate PermissionError / OSError as ``skipped_unlink_error``.
- New ``GCRunEvidence`` dataclass captures
inspected/deleted/skipped_referenced/skipped_leased/skipped_missing/
skipped_unlink_error/dry_run/max_batch/previous_generation/
deleted_hashes and is persisted as JSON in a new
``gc_generations.evidence`` column.
- New ``dry_run=True`` kwarg: counts would-be deletions without
touching disk and without consuming a generation slot.
- New ``read_gc_history(db_path, limit)`` returns parsed history rows
newest-first; rows without evidence (pre-#1190 or crashed passes)
surface with ``evidence=None``.
- ``polylogue maintenance gc-history`` CLI surfaces the evidence rows
(plain or JSON) so operators don't need bespoke SQLite tooling.
- Schema bumped to v2 (``gc_generations.evidence TEXT``); fresh-first
policy applies — mismatch triggers re-ingest, no migration chain.
Tests
- New tests in ``tests/unit/storage/test_blob_gc.py``:
``test_run_blob_gc_unlinks_sharded_path_and_increments_counter`` and
``test_run_blob_gc_does_not_increment_when_file_already_missing``
cover the AC pair from #1190; plus dry-run, evidence persistence,
and history surface tests.
- Updated the existing concurrency test's pre-thread setup with a
sentinel lease — the prior code's no-op unlink masked a race window
between thread spawn and the writer's first ``acquire_blob_leases``.
Verification
devtools verify --quick # exit 0
pytest tests/unit/storage/test_blob_gc.py \
tests/unit/storage/test_blob_gc_concurrency.py \
tests/unit/storage/test_blob_store_contracts.py
# 46 passed
Closes #1190
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (9)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
run_blob_gcnow unlinks the correct sharded blob path(
{root}/{prefix}/{remainder}), incrementsdeletedonly when anunlinkactually removed a file, persists per-pass structured evidenceinto a new
gc_generations.evidencecolumn, supportsdry_run=True,and exposes the evidence through a
polylogue maintenance gc-historyoperator surface.
Problem
The previous implementation called
(blob_path / blob_hash).unlink(missing_ok=True)— i.e. it targeted{blob_dir}/{full_hash}. Blobs live at the sharded path{blob_dir}/{prefix}/{remainder}(seeBlobStore.blob_path), so theflat path never exists for a real blob.
missing_ok=Truesilentlyswallowed the
ENOENTand thedeletedcounter bumped anyway, so:This was observed during PR #1189 implementation and tracked in #1190.
Solution
polylogue/storage/blob_gc.py:_sharded_blob_path(blob_root, blob_hash)mirroringBlobStore.blob_pathand use it as the unlink target.unlink(missing_ok=True)to explicitunlink()withFileNotFoundErrorhandling: missing files nowbump
skipped_missinginstead ofdeleted.PermissionError/OSErrorfailures into a separateskipped_unlink_errorcounter; thedeletedcounter only bumps ona successful
unlink()call.GCRunEvidencedataclass capturing inspected, deleted,per-reason skip counts (
skipped_referenced,skipped_leased,skipped_missing,skipped_unlink_error),dry_run,max_batch,previous_generation, and the list ofdeleted_hashes. Serialisedas sorted JSON.
dry_run: bool = Falsekwarg: counts would-be deletions ondisk-present candidates, touches no file, and does NOT write a
gc_generationsrow (no generation slot consumed by previews).read_gc_history(db_path, limit)returning parsed historyrows newest-first; pre-fix(storage): run_blob_gc unlinks wrong path, deleted counter inflated #1190 rows surface with
evidence=Nonesothey remain visible to operators.
polylogue/storage/sqlite/schema_ddl_archive.py/polylogue/storage/sqlite/schema_ddl.py:evidence TEXTcolumn togc_generations.SCHEMA_VERSION1 → 2. Per the no-migration-chain policythis is a deletes-then-defines edit: a mismatched DB triggers
re-ingest (
polylogue reset --database && polylogued run), noupgrade helper is added. The added column is nullable so existing
in-place generations slot in cleanly under the new schema.
polylogue/cli/commands/maintenance.py:polylogue maintenance gc-historysubcommand with--limitand
--output-format {plain,json}. Readsgc_generations.evidencevia
read_gc_history; plain output shows generation, completiontime, and the per-reason skip tallies.
Tests:
tests/unit/storage/test_blob_gc.py:test_run_blob_gc_unlinks_sharded_path_and_increments_counterplants an orphan via
BlobStore.write_from_bytes(sharded path),backdates its mtime past
MIN_AGE_S, runs GC, asserts thesharded file is gone AND counter == 1.
test_run_blob_gc_does_not_increment_when_file_already_missingpatches
_candidate_blobsto report a hash whose file wasalready removed; counter stays 0 and evidence records
skipped_missing >= 1.test_run_blob_gc_dry_run_does_not_delete_or_record_generationasserts dry-run preserves disk and consumes no generation slot.
test_run_blob_gc_records_structured_evidencevalidates theJSON shape, the deleted-hash list, and
skipped_referencedaccounting.
test_read_gc_history_returns_recent_passes_newest_firstrunsthree GC passes and asserts newest-first ordering.
tests/unit/storage/test_blob_gc_concurrency.py,tests/unit/storage/test_blob_store_contracts.py: schema fixturesupdated to include the new
evidencecolumn; the threadedacquire/commit/GC test gained a pre-thread sentinel lease — the
prior code's no-op unlink masked a real startup race between
thread spawn and the writer's first
acquire_blob_leasescall.Alternatives rejected:
missing_ok=Trueand post-hoc checkingtarget.exists()before/after would still race with concurrent reclaimers. Using
the
unlink()return-status idiom (FileNotFoundError ⇒ no-op) isatomic and matches POSIX semantics directly.
schema surface for no benefit; the evidence row is 1:1 with the
generation and the JSON shape evolves more cheaply than columns.
Verification
Pre-existing CLI status test failures (
test_status_first_run_hint*,test_status_subprocess_no_archive) reproduce on master without thesechanges — they read the host operator's 48.9 GB live archive instead
of an isolated tmp workspace, unrelated to GC.
Closes #1190