Skip to content

fix(blob): MongoDB sweep reclaims each key only up to its newest expired version - #98

Merged
M3gA-Mind merged 3 commits into
tinyhumansai:mainfrom
M3gA-Mind:fix/mongo-blob-sweep-order
Nov 11, 2026
Merged

M3gA-Mind merged 3 commits into
tinyhumansai:mainfrom
M3gA-Mind:fix/mongo-blob-sweep-order

Conversation

@M3gA-Mind

@M3gA-Mind M3gA-Mind commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

MongoBlobStore::sweep_expired collected expired keys in one pass, then re-queried each key by filename alone and deleted every version it found, newest first. That had two problems:

  • A version written after the sweep began (a replacement) was deleted along with the expired ones.
  • A failure partway could leave an older, superseded version as the key's newest, so it became readable again.

The sweep now takes, per key, the newest version that was expired in its first pass, and deletes only versions up to it, oldest first. Versions written since are newer and stay. A sweep that fails partway never exposes a superseded version.

This also corrects the "Blob lifetimes" section of docs/specs/storage-bus.md:

  • It said file and MongoDB reclaim on read, but no driver does; reads only treat a lapsed blob as absent.
  • It named sweep_expired_blobs / sweep_expired_blobs_generation, which don't exist.
  • It now says which sweeps are atomic (memory, SQLite), which are bounded (MongoDB), and which can still race a re-put (file and S3 check, then delete).

Closes #38

API or behavior changes

No API change. Behaviour: the MongoDB sweep no longer deletes versions written during the sweep.

Validation

On a Linux build host, merged onto main 05acdd9 (and merging cleanly onto v2.1.3, f5a3302):

  • cargo test --locked -p tinystoragedrivers-blob --features mongodb against a live replica set and a live standalone mongod: all pass, no skipped live tests, on both. That includes the three sweep tests.
  • Revert check: with the previous mongodb.rs and the new test, the new test fails on both servers (left: 4, right: 3, because the old sweep also deletes the newer version).
  • cargo test --workspace --all-features --locked: 857 passed, 0 failed, 1 ignored (on v2.1.3 main).
  • clippy -D warnings (0 items), cargo fmt --check, rustdoc -D warnings.

Tests

  • mongodb_sweep_keeps_a_blob_rewritten_between_its_passes (live MongoDB): The sweep pauses after its first pass, the key is written again, and the sweep resumes. The fresh blob survives.
    • On the previous sweep it is deleted: pass 2 re-queried by filename (called Option::unwrap() on a None value). That fails on both a replica set and a standalone.
    • A test-only pause hook between the passes makes the race deterministic.

mongodb_sweep_reclaims_only_up_to_the_newest_expired_version (live MongoDB). It plants versions directly in GridFS, the state a failed cleanup or a racing writer leaves, and checks two things:

  • an expired newest version over an older live one removes both, so the key stays absent;
  • a version newer than the expired one survives the sweep.

…red version

sweep_expired re-queried every expired key by filename alone and deleted
all of its versions newest first. A version written after the sweep
began was deleted with them, and a failure partway could leave an older,
superseded version as the newest.

The sweep now takes, per key, the newest version that was expired in its
first pass and deletes only versions up to it, oldest first. Versions
written since are newer and stay; a sweep that fails partway never
exposes a superseded version.

The storage-bus spec section on blob lifetimes is corrected: no driver
reclaims on read, it named sweep functions that do not exist, and it now
says which sweeps can still race a re-put (file, S3).

Closes tinyhumansai#38
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e65cb272-8153-450f-8c82-c692f0626247
Pre-merge checks: Passed 1 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Title check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
Passed Passed checks: 1 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@tinysweeper

tinysweeper Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Tiny Sweeper review

Reviewed head: b141c3729fa96dee7587f5c424832cd218334c10

Verdict: Ready for maintainer review · Priority: none
Coverage: Reviewed 3 of 3 files.
Pull request purpose (title): fix(blob): MongoDB s…

Diagnostics and supporting details

Full summary

Pull request purpose (title): fix(blob): MongoDB sweep reclaims each key only up to its newest expired version. Tiny Sweeper completed its review; deterministic results follow.

Review snapshot

Change surface Files Review signal Count
Production 1 Active findings 0
Tests 1 Noted findings 0
Documentation 1 Resolved findings 0
Configuration 0 Pending checks/questions 0

Completeness: Complete
Test assessment: No supported feature-to-test mapping was available; this does not mean tests are absent or passed.

What changed

`sweep_expired` in `MongoBlobStore` now collects the first file seen per key from the expiry query — the newest expired version, given the shared `files()` ordering of `filename` ascending and `_id` descending — instead of a bare set of expired keys. It then re-queries with both the filename and `_id <= bound` and deletes the returned versions in reverse (oldest first), so a version written since the sweep began is past the bound and survives, and a partial failure never leaves a superseded version as the newest. A `#[cfg(test)]` `sweep_pause` field on the store, set through `pause_sweep_for_test`, pauses the sweep between its two passes via a started/resume notify pair. The *Blob lifetimes* section of `docs/specs/storage-bus.md` is rewritten to state that no driver reclaims storage on the read that notices a lapsed blob, to drop the non-existent `sweep_expired_blobs` / `sweep_expired_blobs_generation` names, and to distinguish atomic (memory, SQLite), bounded (MongoDB) and check-then-delete (file, S3) sweeps. Two live-MongoDB tests cover the bounded reclaim and the between-passes rewrite.

Features

  • Modified — MongoDB blob sweep bounded to each key's newest expired version: The MongoDB sweep no longer deletes every version of an expired key: it deletes only versions up to the newest one that was expired in its first pass, in oldest-first order, so a concurrently written replacement survives the sweep and an interrupted sweep cannot expose a superseded version as the newest. (crates/tinystoragedrivers-blob/src/mongodb.rs impl BlobStore for MongoBlobStore {)
  • Modified — Storage-bus spec *Blob lifetimes* section corrected: The spec no longer claims file and MongoDB reclaim expired blobs on the read that notices them, no longer cites non-existent sweep entry points, and now documents which sweeps are atomic, which are bounded (MongoDB, with the oldest-first and partial-failure guarantees), and which can still race a re-put (file and S3). (docs/specs/storage-bus.md as absent; a zero lifetime is expired on arrival; a plain `put` of the key)

Tests

No supported feature-to-test mapping was produced. Test execution is not inferred.

Findings

No active actionable findings.

Before merge

None.

Agent review details

critique

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The critique lane answered with no findings: it judged the sweep logic and deletion ordering sound, and recorded only a low-confidence note that the test-side single `notify_one` handshake ordering could not be fully verified, without filing a finding.
  • Lane summary: The change bounds the MongoDB expiry sweep to the newest expired version per key so a writer racing the sweep is not losing fresh data, and adds two live tests with a test-only pause hook. The sweep logic and the deletion ordering look sound; the test-side concurrency handling relies on a single `notify_one` handshake whose ordering I could not fully verify, which is noted in the findings at low confidence. _The code index is behind this pull request (indexed at `f732fb62ccda`), so retrieved context may be out of date._

security

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The security lane answered with no findings, reporting the `_id` bound is a data-derived ObjectId rather than attacker input and that no auth, network, or CI permission changes are involved. One file, `docs/specs/storage-bus.md`, was not security-reviewed as prose.
  • Lane summary: The change narrows `sweep_expired` so it only deletes GridFS versions up to the newest expired `_id` seen in the first pass, and adds test-only pause plumbing plus two live-Mongo tests. The bound is a data-derived ObjectId, not attacker input; no auth, network, or CI permission changes. I found no reportable defect in these two files. _The code index is behind this pull request (indexed at `f732fb62ccda`), so retrieved context may be out of date._

tests

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The tests lane answered with no findings, reporting that both live tests assert concrete outcomes that would fail if the new bound were removed, and that the rewritten spec prose conflicts with nothing the diff introduces.
  • Lane summary: The diff narrows the MongoDB expiry sweep to versions up to each key's newest expired one and adds two live-Mongo tests, one of which drives the between-passes race through a test-only pause hook; both assert concrete outcomes (row counts, surviving bytes) that would fail if the new bound were removed. The spec prose is rewritten to match the new behaviour, and the memory/SQLite/S3 description it now gives conflicts with nothing the diff introduces. No reportable defect found. _The code index is behind this pull request (indexed at `f732fb62ccda`), so retrieved context may be out of date._

commits

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The commits lane answered with no findings, reporting no sensitive content in the committed files.
  • Lane summary: No sensitive content in committed files. _The code index is behind this pull request (indexed at `f732fb62ccda`), so retrieved context may be out of date._

description

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The description lane answered with no findings: the description matches the diff (bounded per-key deletion, oldest-first order, the test-only pause hook, both live tests and the spec correction) and nothing significant in the diff is unmentioned.
  • Lane summary: The diff bounds the MongoDB sweep to the newest expired version per key, deletes oldest-first, and adds two live tests plus a test-only pause hook; the spec text is reworded to match. The description matches the change accurately and I found no defect worth reporting. _The code index is behind this pull request (indexed at `f732fb62ccda`), so retrieved context may be out of date._
Evidence and run details
  • Models: ladder/vectors-oai3, deepseek-v4.1-flash, deepseek-v4-flash
  • Spend: $0.007670
  • Completion usage: incomplete; some provider completion costs are unavailable.
  • Tokens: 253181 input · 19084 output · 43648 cached · 416 embedding
  • Updated: 2026-11-11T12:30:07Z
Head State Pass summary
59be10fc03cc ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-11T12:11:44Z)
b141c3729fa9 ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-11T12:30:07Z)

tinysweeper 0.1.2

@tinysweeper tinysweeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tinysweeper found nothing blocking. Approving.

Diagnostics

Total spend: $0.0173

@M3gA-Mind
M3gA-Mind merged commit 0a05a1b into tinyhumansai:main Nov 11, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Follow-up to #37: blob-expiry reclaim order and driver README updates

1 participant