forked from mtusi2026/mtusi2026lab7
- narrow the worker S3 user to staging reads and attachments writes - add database rules the worker role cannot bypass: no return to pending, variant keys of its own attachment, known error codes only - let the cleaner remove a ready source only after its own SHA-256 check - add make jobs_audit [REPAIR=1] to name and requeue damaged jobs - add make worker_compromise: isolated before/after stands, one operation table, response windows, job safety, controls and committed hypotheses Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
536 lines
17 KiB
Python
536 lines
17 KiB
Python
"""`AttachmentCleanupService`: два блока уборки, сигнал, очистка staging."""
|
||
|
||
from uuid import uuid4
|
||
|
||
import pytest
|
||
|
||
from builders import NOW, at, attachment_dto
|
||
from fakes import FakeDatabase, FakeObjectStorage, FakeUnitOfWork
|
||
from src.core import exceptions
|
||
from src.core.repos import attachment_object
|
||
from src.core.services import attachment_cleanup
|
||
from src.core.services.attachment_cleanup import (
|
||
INTERRUPTED_BY_DATABASE,
|
||
INTERRUPTED_BY_OBJECT_STORAGE,
|
||
INTERRUPTED_BY_STOP,
|
||
AttachmentCleanupService,
|
||
)
|
||
from src.schemas import dataclasses
|
||
from src.schemas.enums import (
|
||
AttachmentErrorCode,
|
||
AttachmentStatus,
|
||
AttachmentVariantName,
|
||
)
|
||
from src.utils.constants import ATTACHMENT_PIPELINE_VERSION
|
||
|
||
pytestmark = pytest.mark.anyio
|
||
|
||
# байты, чей SHA-256 в метаданных `attachment_dto` по умолчанию
|
||
CONTENT = b"content"
|
||
|
||
|
||
@pytest.fixture
|
||
def service(
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
) -> AttachmentCleanupService:
|
||
return AttachmentCleanupService(uow, storage)
|
||
|
||
|
||
async def _stale(
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
*,
|
||
seconds: float = -1000,
|
||
staging: bool = True,
|
||
) -> dataclasses.AttachmentFullDTO:
|
||
row = attachment_dto(
|
||
chat_id=uuid4(),
|
||
owner_id=uuid4(),
|
||
created_at=at(seconds),
|
||
staging=staging,
|
||
)
|
||
async with uow:
|
||
await uow.attachments.insert_attachment(attachment=row)
|
||
await uow.commit()
|
||
storage.seed(row.staging_key or row.object_key)
|
||
return row
|
||
|
||
|
||
async def _clean(
|
||
service: AttachmentCleanupService,
|
||
*,
|
||
batch_size: int = 10,
|
||
stop_requested=None,
|
||
) -> dataclasses.CleanupPassDTO:
|
||
kwargs = (
|
||
{} if stop_requested is None else {"stop_requested": stop_requested}
|
||
)
|
||
return await service.clean_stale_pending(
|
||
older_than=NOW,
|
||
batch_size=batch_size,
|
||
**kwargs,
|
||
)
|
||
|
||
|
||
async def test_two_blocks_object_before_row(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = await _stale(uow, storage)
|
||
database.events.clear()
|
||
result = await _clean(service)
|
||
assert result.removed == 1
|
||
assert database.events == [
|
||
"enter",
|
||
"attachments.get_stale_pending",
|
||
"exit",
|
||
"enter",
|
||
"attachments.lock_stale_pending",
|
||
"attachments.mark_removing",
|
||
"commit",
|
||
"exit",
|
||
"enter",
|
||
"attachments.lock_stale_pending",
|
||
"storage.delete",
|
||
"attachments.delete_attachment",
|
||
"commit",
|
||
"exit",
|
||
]
|
||
assert database.attachments == {}
|
||
assert row.staging_key not in storage.objects
|
||
|
||
|
||
async def test_old_version_row_deletes_final_object(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = await _stale(uow, storage, staging=False)
|
||
await _clean(service)
|
||
assert row.object_key not in storage.objects
|
||
assert database.attachments == {}
|
||
|
||
|
||
async def test_storage_failure_keeps_mark_and_row(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
"""Сценарий «Порядок уборки»: пометка зафиксирована, второй блок откачен."""
|
||
row = await _stale(uow, storage)
|
||
database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable()
|
||
result = await _clean(service)
|
||
assert result.interrupted_by == INTERRUPTED_BY_OBJECT_STORAGE
|
||
assert result.removed == 0
|
||
kept = database.attachments[row.id]
|
||
assert kept.updated_at > kept.created_at
|
||
assert "attachments.delete_attachment" not in database.events
|
||
# следующий проход убирает
|
||
database.fail_on.clear()
|
||
assert (await _clean(service)).removed == 1
|
||
|
||
|
||
async def test_row_delete_failure_rolls_back_second_block(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = await _stale(uow, storage)
|
||
database.fail_on["attachments.delete_attachment"] = (
|
||
exceptions.ServiceUnavailable()
|
||
)
|
||
result = await _clean(service)
|
||
assert result.interrupted_by == INTERRUPTED_BY_DATABASE
|
||
# объект удалён раньше строки: строка без объекта, а не наоборот
|
||
assert row.staging_key not in storage.objects
|
||
assert row.id in database.attachments
|
||
|
||
|
||
async def test_database_failure_on_candidates(
|
||
service: AttachmentCleanupService,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
database.fail_on["attachments.get_stale_pending"] = (
|
||
exceptions.ServiceUnavailable()
|
||
)
|
||
assert (await _clean(service)).interrupted_by == INTERRUPTED_BY_DATABASE
|
||
|
||
|
||
async def test_held_row_skipped(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = await _stale(uow, storage)
|
||
database.held.add(row.id)
|
||
result = await _clean(service)
|
||
assert (result.removed, result.skipped) == (0, 1)
|
||
assert row.staging_key in storage.objects
|
||
|
||
|
||
async def test_keyset_more_than_one_batch(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
for index in range(5):
|
||
await _stale(uow, storage, seconds=-1000 + index)
|
||
result = await _clean(service, batch_size=2)
|
||
assert result.removed == 5
|
||
assert database.attachments == {}
|
||
|
||
|
||
async def test_stop_before_row(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
await _stale(uow, storage)
|
||
result = await _clean(service, stop_requested=lambda: True)
|
||
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
||
assert "attachments.lock_stale_pending" not in database.events
|
||
|
||
|
||
async def test_stop_between_steps_keeps_mark(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
"""Сигнал между шагами: первый блок зафиксирован, второго нет."""
|
||
row = await _stale(uow, storage)
|
||
calls = 0
|
||
|
||
def stop_requested() -> bool:
|
||
nonlocal calls
|
||
calls += 1
|
||
return calls > 1
|
||
|
||
database.events.clear()
|
||
result = await _clean(service, stop_requested=stop_requested)
|
||
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
||
assert database.events.count("attachments.lock_stale_pending") == 1
|
||
assert database.attachments[row.id].updated_at > row.created_at
|
||
assert row.staging_key in storage.objects
|
||
|
||
|
||
async def _for_staging(
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
*,
|
||
status: AttachmentStatus,
|
||
updated_seconds: float = -1000,
|
||
object_key: str | None = None,
|
||
) -> dataclasses.AttachmentFullDTO:
|
||
row = attachment_dto(
|
||
chat_id=uuid4(),
|
||
owner_id=uuid4(),
|
||
status=AttachmentStatus.QUEUED
|
||
if status == AttachmentStatus.FAILED
|
||
else status,
|
||
created_at=at(updated_seconds),
|
||
object_key=object_key,
|
||
)
|
||
async with uow:
|
||
await uow.attachments.insert_attachment(attachment=row)
|
||
if status == AttachmentStatus.FAILED:
|
||
await uow.attachments.commit_failed(
|
||
attachment_id=row.id,
|
||
error_code=AttachmentErrorCode.INVALID_IMAGE,
|
||
updated_at=at(updated_seconds),
|
||
)
|
||
await uow.commit()
|
||
storage.seed(row.staging_key)
|
||
return row
|
||
|
||
|
||
async def _staging_pass(
|
||
service: AttachmentCleanupService,
|
||
*,
|
||
batch_size: int = 10,
|
||
stop_requested=None,
|
||
) -> dataclasses.StagingCleanupPassDTO:
|
||
kwargs = (
|
||
{} if stop_requested is None else {"stop_requested": stop_requested}
|
||
)
|
||
return await service.clean_staging(
|
||
retention_before=NOW,
|
||
batch_size=batch_size,
|
||
**kwargs,
|
||
)
|
||
|
||
|
||
async def test_ready_staging_removed_at_next_pass(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
# у ready срока нет: даже свежая строка
|
||
row = await _for_staging(
|
||
uow,
|
||
storage,
|
||
status=AttachmentStatus.READY,
|
||
updated_seconds=100,
|
||
)
|
||
storage.seed(row.object_key, CONTENT)
|
||
database.events.clear()
|
||
result = await _staging_pass(service)
|
||
assert result.removed_ready == 1
|
||
assert row.staging_key not in storage.objects
|
||
# конечный оригинал ready не трогается
|
||
assert row.object_key in storage.objects
|
||
stored = database.attachments[row.id]
|
||
assert stored.staging_key is None
|
||
assert stored.updated_at == row.updated_at
|
||
block = database.events[database.events.index("enter", 2) :]
|
||
assert block == [
|
||
"enter",
|
||
"attachments.lock_staging_candidate",
|
||
# сверка результата — под той же блокировкой, до удаления исходника
|
||
"storage.get",
|
||
"storage.delete",
|
||
"attachments.clear_staging_key",
|
||
"commit",
|
||
"exit",
|
||
*block[7:],
|
||
]
|
||
|
||
|
||
async def test_failed_staging_removed_with_variants_after_retention(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
old = await _for_staging(uow, storage, status=AttachmentStatus.FAILED)
|
||
young = await _for_staging(
|
||
uow,
|
||
storage,
|
||
status=AttachmentStatus.FAILED,
|
||
updated_seconds=100,
|
||
)
|
||
storage.seed(old.object_key)
|
||
keys = attachment_object.variant_keys(
|
||
storage,
|
||
attachment_id=old.id,
|
||
pipeline_version=ATTACHMENT_PIPELINE_VERSION,
|
||
)
|
||
for key in keys:
|
||
storage.seed(key)
|
||
result = await _staging_pass(service)
|
||
assert result.removed_failed == 1
|
||
assert old.staging_key not in storage.objects
|
||
assert old.object_key not in storage.objects
|
||
assert all(key not in storage.objects for key in keys)
|
||
assert len(keys) == len(AttachmentVariantName)
|
||
# моложе срока хранения: на месте
|
||
assert young.staging_key in storage.objects
|
||
assert database.attachments[young.id].staging_key is not None
|
||
|
||
|
||
async def test_current_work_not_touched(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
) -> None:
|
||
row = await _for_staging(uow, storage, status=AttachmentStatus.QUEUED)
|
||
result = await _staging_pass(service)
|
||
assert result.removed_ready + result.removed_failed == 0
|
||
assert row.staging_key in storage.objects
|
||
|
||
|
||
async def test_foreign_staging_key_not_removed(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = attachment_dto(
|
||
chat_id=uuid4(),
|
||
owner_id=uuid4(),
|
||
status=AttachmentStatus.READY,
|
||
namespace="other",
|
||
)
|
||
async with uow:
|
||
await uow.attachments.insert_attachment(attachment=row)
|
||
await uow.commit()
|
||
storage.seed(row.staging_key)
|
||
result = await _staging_pass(service)
|
||
assert result.skipped == 1
|
||
assert row.staging_key in storage.objects
|
||
assert database.attachments[row.id].staging_key == row.staging_key
|
||
|
||
|
||
async def test_staging_held_and_storage_failure_and_stop(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
row = await _for_staging(uow, storage, status=AttachmentStatus.READY)
|
||
storage.seed(row.object_key, CONTENT)
|
||
database.held.add(row.id)
|
||
assert (await _staging_pass(service)).skipped == 1
|
||
database.held.clear()
|
||
database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable()
|
||
result = await _staging_pass(service)
|
||
assert result.interrupted_by == INTERRUPTED_BY_OBJECT_STORAGE
|
||
assert database.attachments[row.id].staging_key is not None
|
||
database.fail_on.clear()
|
||
result = await _staging_pass(service, stop_requested=lambda: True)
|
||
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
||
assert database.attachments[row.id].staging_key is not None
|
||
|
||
|
||
async def test_staging_keyset_more_than_one_batch(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
) -> None:
|
||
for index in range(5):
|
||
await _for_staging(
|
||
uow,
|
||
storage,
|
||
status=AttachmentStatus.READY,
|
||
updated_seconds=-1000 + index,
|
||
)
|
||
for row in database.attachments.values():
|
||
storage.seed(row.object_key, CONTENT)
|
||
result = await _staging_pass(service, batch_size=2)
|
||
assert result.removed_ready == 5
|
||
|
||
|
||
class Journal:
|
||
"""Записи сервиса с уровнем: что назвал проход."""
|
||
|
||
def __init__(self) -> None:
|
||
self.records: list[tuple[str, str, dict[str, object]]] = []
|
||
|
||
def bind(self, **_: object) -> "Journal":
|
||
return self
|
||
|
||
def __getattr__(self, level: str) -> object:
|
||
def write(event: str, **fields: object) -> None:
|
||
self.records.append((level, event, fields))
|
||
|
||
return write
|
||
|
||
def errors(self) -> list[dict[str, object]]:
|
||
return [
|
||
fields
|
||
for level, event, fields in self.records
|
||
if (level, event)
|
||
== ("error", "Attachment result does not match its source")
|
||
]
|
||
|
||
|
||
@pytest.fixture
|
||
def journal(monkeypatch: pytest.MonkeyPatch) -> Journal:
|
||
found = Journal()
|
||
monkeypatch.setattr(attachment_cleanup, "logger", found)
|
||
return found
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
("stored", "reason"),
|
||
[
|
||
(None, "missing"),
|
||
(CONTENT + b"!", "size"),
|
||
(b"CONTENT", "checksum"),
|
||
(b"", "size"),
|
||
],
|
||
)
|
||
async def test_ready_source_kept_without_matching_result(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
journal: Journal,
|
||
stored: bytes | None,
|
||
reason: str,
|
||
) -> None:
|
||
"""Слово worker «готово» исходник не удаляет (change
|
||
track1-worker-compromise, решение 7): объекта нет, другой размер, другие
|
||
байты того же размера. Остальные строки прохода убираются; следующие
|
||
проходы — то же самое, пока результат не сойдётся."""
|
||
bad = await _for_staging(
|
||
uow,
|
||
storage,
|
||
status=AttachmentStatus.READY,
|
||
updated_seconds=-1000,
|
||
)
|
||
good = await _for_staging(
|
||
uow,
|
||
storage,
|
||
status=AttachmentStatus.READY,
|
||
updated_seconds=-900,
|
||
)
|
||
storage.seed(good.object_key, CONTENT)
|
||
if stored is not None:
|
||
storage.seed(bad.object_key, stored)
|
||
for expected in (1, 0):
|
||
result = await _staging_pass(service)
|
||
assert result.interrupted_by is None
|
||
assert (result.removed_ready, result.skipped) == (expected, 1)
|
||
assert bad.staging_key in storage.objects
|
||
kept = database.attachments[bad.id]
|
||
assert kept.staging_key == bad.staging_key
|
||
assert good.staging_key not in storage.objects
|
||
assert database.attachments[good.id].staging_key is None
|
||
assert (
|
||
journal.errors()
|
||
== [
|
||
{
|
||
"attachment_id": bad.id,
|
||
"object_key": bad.object_key,
|
||
"reason": reason,
|
||
},
|
||
]
|
||
* 2
|
||
)
|
||
# результат сошёлся — исходник убран
|
||
storage.seed(bad.object_key, CONTENT)
|
||
assert (await _staging_pass(service)).removed_ready == 1
|
||
assert bad.staging_key not in storage.objects
|
||
|
||
|
||
async def test_staging_result_read_failure_interrupts(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
database: FakeDatabase,
|
||
journal: Journal,
|
||
) -> None:
|
||
"""Отказ S3 на чтении оригинала — прерванный проход, а не «не сошлось»."""
|
||
row = await _for_staging(uow, storage, status=AttachmentStatus.READY)
|
||
storage.seed(row.object_key, CONTENT)
|
||
database.fail_on["storage.get"] = exceptions.ObjectStorageUnavailable()
|
||
result = await _staging_pass(service)
|
||
assert result.interrupted_by == INTERRUPTED_BY_OBJECT_STORAGE
|
||
assert row.staging_key in storage.objects
|
||
assert database.attachments[row.id].staging_key is not None
|
||
assert journal.errors() == []
|
||
|
||
|
||
async def test_failed_staging_is_not_compared(
|
||
service: AttachmentCleanupService,
|
||
uow: FakeUnitOfWork,
|
||
storage: FakeObjectStorage,
|
||
) -> None:
|
||
"""У `failed` результата нет и сверять нечего: срок хранения прежний."""
|
||
row = await _for_staging(uow, storage, status=AttachmentStatus.FAILED)
|
||
assert (await _staging_pass(service)).removed_failed == 1
|
||
assert row.staging_key not in storage.objects
|
||
assert storage.calls["get"] == 0
|