fix: address upload-consistency verify findings

Mark a stale pending row in its own committed transaction before the
cleaner deletes the object, and let mark_ready promote only unmarked
rows, so a cleaner rollback after the object delete can no longer leave
a ready attachment without its object. Add fault checks for an unknown
COMMIT outcome, an upload outliving the grace period and a cleaner
failure after the object delete; fix the cleaner entrypoint in rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 21:27:47 +03:00
co-authored by Claude Opus 5.5
parent f2dc0b368a
commit 87c48b940b
20 changed files with 714 additions and 118 deletions
+111
View File
@@ -575,6 +575,12 @@ def orphan_objects(stand: Stand) -> set[str]:
return stored_ids(stand) - set(rows)
def ready_without_object(stand: Stand) -> set[str]:
"""Готовые вложения без объекта: строки `ready` уборщик не трогает,
поэтому порядок вызовов здесь не важен."""
return ready_ids(attachment_rows(stand)) - stored_ids(stand)
def new_ids(before: dict[str, str], after: dict[str, str]) -> set[str]:
return set(after) - set(before)
@@ -875,6 +881,111 @@ def test_killed_between_put_and_ready(
api.upload(alice, chat["id"], images.sample_png())
@pytest.mark.spec("attachments", "Исход фиксации неизвестен")
def test_ready_commit_outcome_unknown(
api: Api,
stand: Stand,
alice: Actor,
chat: dict[str, Any],
) -> None:
"""Перевод в ready падает на самом COMMIT (отложенный триггер): API не
знает, зафиксировано ли вложение, и объект не трогает (verify W2)."""
filename = fault_filename()
before = attachment_rows(stand)
stand("fail-attachments", "commit", filename)
try:
started = time.monotonic()
resp = upload_raw(
api,
alice,
chat,
images.sample_png(),
filename=filename,
)
assert_error(resp, 503)
finally:
stand("clear-fail-attachments")
after = attachment_rows(stand)
(pending,) = new_ids(before, after)
assert after[pending] == "pending"
# компенсации нет: вложение могло стать ready
assert pending in stored_ids(stand)
assert_invisible(api, alice, chat, pending)
wait_cleaned(stand, pending, since=started)
assert not orphan_objects(stand)
@pytest.mark.spec("storage-consistency", "Загрузка дольше grace")
def test_upload_outlives_grace(
api: Api,
stand: Stand,
alice: Actor,
chat: dict[str, Any],
) -> None:
"""Строка вставлена «на час старше», перевод в ready ждёт в триггере,
пока её не уберёт уборщик стенда: загрузка, пережившая grace, без
заморозки процесса (verify W3)."""
filename = fault_filename()
before = attachment_rows(stand)
stand("fail-attachments", "outlive", filename)
# ожидание в триггере до 4,5 с: срок клиента больше
slow = Api(str(api.client.base_url), timeout=THREAD_CLIENT_TIMEOUT_S)
try:
with slow:
resp = upload_raw(
slow,
alice,
chat,
images.sample_png(),
filename=filename,
)
# не 201 (ready без объекта) и не 500 (дефект ветки)
assert_error(resp, 503)
finally:
stand("clear-fail-attachments")
rows = attachment_rows(stand)
assert not new_ids(before, rows), rows
assert not stored_ids(stand) - set(before)
assert not orphan_objects(stand)
@pytest.mark.spec("storage-consistency", "Сбой уборки после удаления объекта")
def test_cleaner_failure_after_object_delete(
api: Api,
stand: Stand,
alice: Actor,
chat: dict[str, Any],
) -> None:
"""Уборщик удалил объект, а удаление строки упало (08006), пока загрузка
ждёт перевода в ready: строка помечена до удаления объекта и ready уже
не станет (verify W1)."""
filename = fault_filename()
before = attachment_rows(stand)
stand("fail-attachments", "undeletable", filename)
slow = Api(str(api.client.base_url), timeout=THREAD_CLIENT_TIMEOUT_S)
try:
with slow:
resp = upload_raw(
slow,
alice,
chat,
images.sample_png(),
filename=filename,
)
assert_error(resp, 503)
(pending,) = new_ids(before, attachment_rows(stand))
assert attachment_rows(stand).get(pending) == "pending"
assert not ready_without_object(stand)
assert_invisible(api, alice, chat, pending)
finally:
stand("clear-fail-attachments")
# строка старше grace: следующий проход доводит уборку
returned = time.monotonic()
wait_cleaned(stand, pending, since=returned - CLEANER_GRACE_S)
assert not orphan_objects(stand)
assert not ready_without_object(stand)
@pytest.mark.spec("storage-consistency", "S3 недоступен во время уборки")
@pytest.mark.spec("storage-consistency", "БД недоступна во время уборки")
@pytest.mark.spec("storage-consistency", "Готовые вложения не тронуты")