diff --git a/openspec/changes/add-lab4-storage-reconciler/tasks.md b/openspec/changes/add-lab4-storage-reconciler/tasks.md index cbec9e35..b206ab41 100644 --- a/openspec/changes/add-lab4-storage-reconciler/tasks.md +++ b/openspec/changes/add-lab4-storage-reconciler/tasks.md @@ -17,6 +17,7 @@ - [x] 3.2 Платформа: поток журнала настраивается - [x] 3.3 CLI `messenger.attachments.entrypoints.reconcile`: флаги, сборка адаптеров, JSON в stdout, коды выхода - [x] 3.4 Проверка группы: API-тесты сверки против одноразового PostgreSQL (каждый класс в dry-run без изменений, apply и повтор = 0, свежее и используемое сохранены, строка появилась после листинга, карантин и `404`, `healed`, `--deep`, занятая блокировка); тест адаптера против RustFS (порядок листинга, страницы, `HEAD`) +- [x] 3.5 Отказ зависимости посреди прохода завершает его исключением `Unavailable`, а не группой исключений `TaskGroup`: журнал `reconcile.failed`, код `1`; API-тест сбоя листинга на второй странице ## 4. Стенд @@ -30,6 +31,8 @@ - [x] 5.2 `student-tests/tools/seed_storage.py`: наполнение N объектов и строк в образе API - [x] 5.3 `scripts/reconcile_track.py`: `demo` (внедрение, dry-run, apply ×2, безопасность, `healed`, нагрузка) и `cost` (снимок хоста, закрепление CPU, матрица N × режим × 3) - [x] 5.4 Проверка группы: `make check` (Python 3.9, JSON), `make lint` +- [ ] 5.5 Процессорное время прохода в отчёте; `cost`: снимок `docker stats` хоста до и после серии, чужой CPU за окно каждого прогона (во всей VM и на закреплённых ядрах), отбраковка и повтор после тишины, отбракованные — в `cost/disturbed/` +- [ ] 5.6 `demo`: проход при остановленном S3 — код `1` за ограниченное время, PostgreSQL и bucket не меняются ## 6. Измерения diff --git a/src/messenger/attachments/application/reconcile.py b/src/messenger/attachments/application/reconcile.py index 2856d7b9..9de89638 100644 --- a/src/messenger/attachments/application/reconcile.py +++ b/src/messenger/attachments/application/reconcile.py @@ -22,6 +22,7 @@ from messenger.attachments.domain.models import ( AttachmentStatus, Quarantine, ) +from messenger.shared.application.errors import Unavailable from messenger.shared.application.ports import Clock, Logger type UnitOfWorkFactory = Callable[[], AttachmentsUnitOfWork] @@ -104,18 +105,22 @@ class Pass: async def run(self) -> None: objects = aiter(self._objects()) item = await anext(objects, None) - async with asyncio.TaskGroup() as checks: - async for row in self._rows(): - while item is not None and item.key < row.object_key: + try: + async with asyncio.TaskGroup() as checks: + async for row in self._rows(): + while item is not None and item.key < row.object_key: + await self._orphan(item) + item = await anext(objects, None) + matched = item if item is not None and item.key == row.object_key else None + await self._row(row, matched, checks) + if matched is not None: + item = await anext(objects, None) + while item is not None: await self._orphan(item) item = await anext(objects, None) - matched = item if item is not None and item.key == row.object_key else None - await self._row(row, matched, checks) - if matched is not None: - item = await anext(objects, None) - while item is not None: - await self._orphan(item) - item = await anext(objects, None) + except* Unavailable as failed: + error = failed.exceptions[0] + raise error from error.__cause__ async def _timed[ResultT](self, name: str, step: Awaitable[ResultT]) -> ResultT: started = monotonic() diff --git a/student-tests/api/test_reconciler.py b/student-tests/api/test_reconciler.py index c582bcd3..737664cd 100644 --- a/student-tests/api/test_reconciler.py +++ b/student-tests/api/test_reconciler.py @@ -22,6 +22,7 @@ from messenger.attachments.infrastructure.postgres import ( PostgresSingleRun, ) from messenger.migrate import migrate +from messenger.shared.application.errors import Unavailable from messenger.shared.infrastructure.clock import MonotonicUtcClock from messenger.shared.infrastructure.postgres import Database from pictures import SAMPLE @@ -271,3 +272,16 @@ async def test_second_pass_is_skipped_while_one_runs(dsn: str) -> None: assert not busy.found assert not free.skipped assert free.fixed["orphan_stale"] == 1 + + +async def test_storage_failure_mid_pass_ends_it_as_unavailable(dsn: str) -> None: + async with stand(dsn) as s: + await damage(s) + + async def broken() -> None: + raise Unavailable("S3 недоступно") from TimeoutError() + + s.store.after_page = broken + with pytest.raises(Unavailable) as failed: + await s.run(apply=True, deep=True) + assert isinstance(failed.value.__cause__, TimeoutError)