Reconcile attachment metadata with the bucket in one merge pass

StorageReconciler walks the sorted bucket listing and keyset pages of
attachments ordered by object key side by side, so a pass is linear
and keeps one page of each side in memory. An object without a row is
deleted only when it is older than grace plus skew and a lookup by key
right before the delete still finds no row; a ready row whose object
is missing, has another size or, with the deep check, another digest
is put in quarantine after a HEAD recheck and is never deleted; a stale
intent is claimed with SKIP LOCKED and dropped object first; a
quarantined row whose object matches again is released. Deep checks
hash objects in a bounded task group, each action uses its own unit of
work, and failures of single actions are counted instead of aborting.
An advisory lock lets one pass run at a time. Content of a quarantined
attachment answers 404 without touching S3, its metadata is unchanged.

The storage port gains listing pages and HEAD, the repository keyset
pages, lookup by key, claim and quarantine marking; the platform logger
can write to another stream. The one-off entry point
python -m messenger.attachments.entrypoints.reconcile is a dry run
unless --apply is given, assembles the adapters with the API key of S3
and prints a JSON report with counts per class, samples and the cost
of the pass to stdout while the journal goes to stderr.

Tests run the reconciler against its own migrated database with every
class, apply and repeat, the row that appears after the listing, the
busy lock and the release, and check paged byte-order listing and HEAD
against RustFS.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
This commit is contained in:
Artyom
2026-10-29 03:12:10 +03:00
co-authored by Claude Opus 5.5
parent 62986245a2
commit 91731fd317
13 changed files with 920 additions and 23 deletions
+26 -1
View File
@@ -71,6 +71,27 @@ async def test_round_trip_of_the_largest_object(rustfs: str) -> None:
await objects.delete(key)
async def test_listing_is_paged_in_byte_order_and_stat_reads_size(rustfs: str) -> None:
prefix = f"reconcile-{uuid4().hex}/"
keys = [f"{prefix}{name}" for name in ("b", "a", "Z", "a-1", "é")]
async with storage(rustfs) as objects:
for size, key in enumerate(keys, start=1):
await objects.put(key, b"x" * size, "image/png")
pages = [page async for page in objects.listing(prefix, 2)]
assert [len(page) for page in pages] == [2, 2, 1]
listed = [item.key for page in pages for item in page]
assert listed == sorted(keys, key=str.encode)
sizes = {item.key: item.size for page in pages for item in page}
assert sizes == {key: size for size, key in enumerate(keys, start=1)}
found = await objects.stat(keys[0])
assert found is not None
assert found.size == 1
assert await objects.stat(f"{prefix}absent") is None
for key in keys:
await objects.delete(key)
assert not [item async for page in objects.listing(prefix, 2) for item in page]
async def test_unreachable_endpoint_is_unavailable_in_bounded_time() -> None:
started = time.monotonic()
async with storage_without_bucket("http://127.0.0.1:1") as objects:
@@ -78,7 +99,11 @@ async def test_unreachable_endpoint_is_unavailable_in_bounded_time() -> None:
await objects.put("attachments/x", b"x", "image/png")
with pytest.raises(Unavailable):
await objects.open("attachments/x")
assert time.monotonic() - started < 8
with pytest.raises(Unavailable):
await objects.stat("attachments/x")
with pytest.raises(Unavailable):
await anext(aiter(objects.listing("attachments/", 10)))
assert time.monotonic() - started < 12
@asynccontextmanager