From c43d3490a07ad939e4e4748863589b73c84ad05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BD=D0=B5=D0=BA?= <88394552+Lunatuk@users.noreply.github.com> Date: Thu, 5 Nov 2026 19:30:42 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=204=20=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + .github/workflows/application.yml | 6 +- REPORT.md | 203 ++- compose.yaml | 76 +- docs/schema.md | 34 +- evidence/lab4-checks.json | 2154 ++++++++++++++++++++++++++++ evidence/lab4-checks.txt | 531 +++++++ infra/haproxy.cfg | 4 +- migrations/003_attachments.sql | 34 + requirements.txt | 7 + scripts/check-docker.sh | 2 +- scripts/init_local.py | 30 +- scripts/migrate.sh | 4 +- scripts/restart.sh | 2 +- scripts/rolling-update.sh | 1 - scripts/start.sh | 2 +- src/app.py | 32 +- src/attachments.py | 147 ++ src/bootstrap_s3.py | 38 + src/cleanup.py | 60 + src/models.py | 23 +- src/object_store.py | 57 + src/storage.py | 35 +- student-tests/attachment_checks.py | 275 ++++ student-tests/docker_lifecycle.py | 18 +- student-tests/test_attachments.py | 133 ++ 26 files changed, 3835 insertions(+), 74 deletions(-) create mode 100644 evidence/lab4-checks.json create mode 100644 evidence/lab4-checks.txt create mode 100644 migrations/003_attachments.sql create mode 100644 src/attachments.py create mode 100644 src/bootstrap_s3.py create mode 100644 src/cleanup.py create mode 100644 src/object_store.py create mode 100644 student-tests/attachment_checks.py create mode 100644 student-tests/test_attachments.py diff --git a/.env.example b/.env.example index ebbf681..e441d33 100644 --- a/.env.example +++ b/.env.example @@ -19,3 +19,4 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 +UPLOAD_CLEANUP_GRACE_SECONDS=3600 diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml index 6784a25..4527c4a 100644 --- a/.github/workflows/application.yml +++ b/.github/workflows/application.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/upload-artifact@v4 if: always() with: - name: lab3-evidence + name: lab4-evidence path: | - evidence/lab3-checks.txt - evidence/lab3-checks.json + evidence/lab4-checks.txt + evidence/lab4-checks.json diff --git a/REPORT.md b/REPORT.md index 9ad2b84..0fc22be 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,198 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Савельев Иван, БВТ2304. +- Целевая оценка: **4** — все требования уровней 3 и 4. Исследовательские треки оценки 5 не заявлены. +- Стенд: Docker Compose, HAProxy, две реплики FastAPI, PostgreSQL, Valkey, SeaweedFS с приватным S3 bucket. +- Commit/PR: после публикации. Evidence содержит source_commit и working_tree=true: проверялась рабочая версия поверх этого commit. +- Источники и помощь: официальные материалы перечислены в конце; код и проверки подготовлены с помощью Codex, результаты получены запуском Docker-стенда. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Engine с Compose v2, Python 3.11, Bash и make; для demo.sh — curl и jq. Проверенная среда записана в evidence/lab4-checks.json. Все команды выполняются из корня этого репозитория, внешние установленные БД или S3 не нужны. -## Архитектура и решения +```sh +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements.txt -r requirements-dev.txt +make check validate PYTHON=.venv/bin/python +./scripts/start.sh +./scripts/check.sh +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Адрес API: http://localhost:8080. start.sh генерирует локальные .env и secrets/, собирает образ, поднимает хранилища, выполняет миграции и bootstrap bucket, запускает API, LB и cleaner. Возврат из скрипта означает готовность обеих API-реплик через LB. Существующие пароли и настройки не перезаписываются. Локальный IAM-конфиг S3 собирается из тех же secret-файлов. Каталог secrets имеет права 0700; файлы 0444 позволяют UID 10001 контейнера читать смонтированные Docker secrets. .env, secrets и .venv игнорируются Git; Docker build context содержит только необходимые исходники. -## Доказательства критериев +Если 8080 занят другим стендом, сохраните HOST_PORT=8081 в .env и используйте BASE_URL=http://localhost:8081 для HTTP-проверок. Либо передайте HOST_PORT=8081 в окружении start.sh. Compose project mtusi-lab4 и его volumes отделены от lab3. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +```sh +docker compose ps -a +./scripts/demo.sh +./scripts/restart.sh # перезапуск всех постоянных процессов; volumes сохраняются +./scripts/stop.sh # удаление контейнеров/сетей; volumes сохраняются +./scripts/start.sh # повторный запуск с прежними данными -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +# Только для намеренного удаления учебных данных текущего Compose project: +docker compose down --volumes +``` -## Эксперименты и отказы +Закреплены Python 3.11.15-slim-bookworm с digest в Dockerfile, PostgreSQL 17.6-bookworm, Valkey 8.1.3, HAProxy 3.2.6-alpine, SeaweedFS 3.85 с digest sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a. Версии Python-библиотек зафиксированы в requirements.txt; добавлены MinIO Python SDK 7.2.20 и Pillow 13.0.0. SDK работает с S3 API SeaweedFS; сервер MinIO не требуется. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +## Архитектура и перенос из lab3 -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +```text +Клиент → HAProxy :8080 → API × 2 → PostgreSQL (метаданные, сообщения, идемпотентность) + → Valkey (серверные Bearer-сессии) + → S3 gateway :9000 → SeaweedFS storage → s3_data +Одноразовые migrate → PostgreSQL; s3-bootstrap → приватный bucket +Постоянный cleanup → журнал PostgreSQL + DELETE незавершённых объектов S3 +``` + +Из lab3 сохранены пользователи, Argon2id, Basic-вход и Bearer-сессии, ACL чатов, текстовые сообщения, keyset-пагинация, X-Request-Id/X-Instance-Id, общая идемпотентность, миграции, LB, ограничения ресурсов и скрипты отказа/замены реплик. Предыдущие отчёты — REPORT.lab2.md и REPORT.lab3.md. Архивные evidence/lab3-* относятся к прежней работе. + +Добавлена миграция 003: attachments, message_attachments, attachment_uploads и request_attachment_ids в таблице идемпотентности. Миграции 001/002 не изменены. Старые сообщения читаются с attachment_ids=[]. Новый POST сообщения принимает до 10 уникальных attachment_ids; чужой/неизвестный ID или ID другого чата даёт 404. Весь список проверяется до вставки сообщения в одной транзакции. Пустой/пробельный text допустим только при наличии вложений. Любой участник может использовать вложение своего чата. Порядок списка сохраняется; при повторе ключа сравниваются text и список UUID. Пропущенный список и [] равнозначны. + +Бинарного содержимого в PostgreSQL нет. Случайный object_key вида attachments/ назначает сервер; filename используется только для отображения и Content-Disposition. В API нет endpoint удаления/изменения вложения. Ready-метаданные и связи сообщений постоянны, локальный файл API для чтения оригинала не нужен. Детали FK, индексов и транзакций — в docs/schema.md. + +| Настройка .env.example | Реальное использование | +| --- | --- | +| APP_HOST, APP_PORT | 0.0.0.0:8080 внутри API | +| HOST_PORT | Публичный порт LB на 127.0.0.1 | +| APP_ENV | Учебная метка, не переключает поведение | +| SESSION_STORE, SESSION_TTL_SECONDS | Valkey; TTL сессии, по умолчанию 3600 s | +| DATABASE_URL_FILE, CACHE_URL_FILE | Пути Docker secrets PostgreSQL и Valkey | +| API_REPLICAS | Две реплики по умолчанию; LB рассчитан на 10 | +| IDEMPOTENCY_TTL_SECONDS | Не меньше 86400 s; одинаковый ответ при повторе | +| S3_ENDPOINT, S3_REGION, S3_BUCKET | http://s3:9000, us-east-1, messenger-attachments | +| S3_ACCESS_KEY_ID_FILE, S3_SECRET_ACCESS_KEY_FILE | Только ограниченные credentials приложения | +| MAX_UPLOAD_BYTES, MAX_IMAGE_PIXELS | 10485760 bytes, 40000000 pixels; повышение выше контракта запрещено | +| UPLOAD_CLEANUP_GRACE_SECONDS | 3600 s, минимум 60 s; защита свежих/незавершённых загрузок | + +## Приватный bucket и bootstrap + +Выбран один S3-совместимый сервер — SeaweedFS. Два сервиса одного продукта разделяют его S3 gateway и внутренние master/volume/filer. s3-storage и его административные HTTP/gRPC-интерфейсы находятся только в закрытой сети s3-internal; API к ней не подключены. Только S3 gateway соединяет эту сеть с сетью приложения. Наружу опубликован лишь LB: ни 9000, ни UI, ни внутренние API-порты не опубликованы. + +init_local.py создаёт отдельные случайные root и application credentials. S3 загружает статическую IAM-конфигурацию из secret s3_config, без anonymous identity. Администратор используется только одноразовым s3-bootstrap; тот проверяет наличие bucket и создаёт его при отсутствии. Повторный bootstrap сохраняет bucket и объекты. Root secrets/IAM-файл не монтируются в API или cleaner. + +Приложению разрешены Read и List только своего bucket и Write только prefix attachments/*. В терминах SeaweedFS Write включает PUT/DELETE объектов; административных действий и записи вне prefix нет. Тест напрямую, вне API приложения, проверяет anonymous GET=403, запрет создания другого bucket и записи вне prefix, недоступность административных портов и изоляцию storage-сети. Приватность S3 дополняется ACL приложения: запрос без Bearer даёт 401, посторонний участнику чата пользователь — 404 как для metadata, так и для content. + +## Приём, проверка и скачивание + +POST /api/v1/chats/{id}/attachments?filename=... принимает сырые bytes PNG/JPEG. Сначала выполняются аутентификация и проверка чата, затем валидация имени и MIME. Запрещены slash, backslash, NUL, управляющие символы, пустое/пробельное имя и длина более 255 code points. + +Content-Length свыше лимита отклоняется до чтения тела. Независимо от заголовка, включая chunked upload, счётчик проверяется при каждом полученном фрагменте до записи в TemporaryFile: больше 10 MiB — 413. Тело не собирается через request.body(). Временный файл закрывается и удаляется при успехе, ошибке, прерывании клиента и завершении процесса; он не является постоянным хранилищем оригинала. + +Pillow открывает только PNG/JPEG, проверяет реальный формат и число пикселей до load(), затем verify() и полное декодирование обнаруживают повреждения. Подмена MIME/чужой формат/повреждённое изображение — 415; превышение pixels — 413. DecompressionBombWarning считается ошибкой. Оригинал не перекодируется, SHA-256 считается по исходному потоку. + +На одну API-реплику приходится одна активная загрузка/декодирование; остальные запросы ждут слот до 20 s с обратным давлением ASGI, далее получают 503. Приём тела ограничен 30 s. S3 имеет connect timeout 1 s, read timeout 3 s и отключённые автоматические повторы. Один оригинал ≤10 MiB отправляется одним PUT, без multipart и его незавершённых частей. SDK может держать до 10 MiB для PUT; декодирование ограничено 40 млн pixels и одним изображением. Каждый API имеет лимит 384 MiB и 1 CPU; количество загрузок не умножает число полных декодирований на реплике. + +201 ready отправляется только после сохранения S3 и COMMIT metadata. variants=[], error_code=null. GET metadata/content снова проверяет членство в чате. Content идёт через API без redirect, порциями 64 KiB с истинным Content-Type, Content-Length, безопасным Content-Disposition и nosniff. Файл не читается целиком в RAM при скачивании; S3-соединение закрывается в finally. SHA-256 и size_bytes описывают исходные bytes. + +/health/ready проверяет PostgreSQL, Valkey и существование S3 bucket подписанным HEAD. /health/live проверяет процесс. При отказе S3 обязательные upload/download получают 503; LB исключает неготовые API. После отправки части download последующий сетевой обрыв означает неполный HTTP download: статус уже не заменяется, клиент может повторить безопасный GET. + +## Согласованность S3 и PostgreSQL на уровне 4 + +1. После приёма и проверки файла отдельный COMMIT сохраняет attachment_uploads с ID и object_key. +2. Следующая транзакция берёт advisory lock object_key, выполняет S3 PUT, вставляет attachments и удаляет журнал. Только после COMMIT отправляется 201. +3. При обрыве клиентского тела до проверки S3/журнал ещё не затрагиваются. При ошибке PUT или отказе БД после PUT сохраняется долговечное намерение. Если исход COMMIT неизвестен, немедленного DELETE нет: ready-метаданные могли сохраниться. +4. Cleaner каждые 30 s выбирает до 100 записей старше grace period, пробует ту же advisory lock и повторно проверяет возраст и наличие ready-метаданных. Активный lock означает пропуск. Если ready существует, очищается только журнал. Иначе выполняются идемпотентный S3 DELETE и удаление журнала. +5. Ошибка S3/БД откладывает проход. Если DELETE уже прошёл, а COMMIT удаления журнала нет, повторный DELETE безопасен. При восстановлении зависимостей worker продолжает работу. + +По умолчанию grace=1 час существенно больше таймаутов загрузки; минимальное значение — 60 s. Транзакция записи имеет idle timeout 30 s. Свежие и активные загрузки защищены возрастом и блокировкой, ready-объекты не удаляются даже без привязки к сообщению. После восстановления сервисов журнал не остаётся без обработчика. + +```sh +# Ручной дополнительный проход; постоянный cleanup-service уже работает: +docker compose run --rm --no-deps cleanup python -m src.cleanup --once +``` + +Это ограниченная компенсация операций приложения. Полный reconciler, presigned upload, варианты изображений и фоновая обработка из следующих лабораторных не добавлялись. + +## Команды проверки и доказательства критериев + +```sh +make check validate PYTHON=.venv/bin/python +./scripts/check.sh +make test-persistence PYTHON=.venv/bin/python ACTION_SCRIPT=scripts/restart.sh +make test-failover PYTHON=.venv/bin/python ACTION_SCRIPT=scripts/stop-one.sh +docker compose start api +./scripts/wait-ready.sh + +# Полный повторяемый прогон уровня 4 в отдельном временном Compose project: +./scripts/check-docker.sh +``` + +check-docker.sh выбирает отдельное имя lab4-check- и свободный порт, создаёт только синтетические данные. В finally удаляет только свои контейнеры и volumes; обычный mtusi-lab4 остаётся доступным. Моделирование отказа БД использует временный trigger в тестовой БД; trigger удаляется в finally. Рабочий API не содержит fault-injection endpoint или специального заголовка. + +| Критерий README | Команда/сценарий | Наблюдение и evidence | +| --- | --- | --- | +| Сохранены возможности lab3, две реплики, общая сессия/идемпотентность | check.sh, повтор ключа через реплики, повтор после рестарта, SIGTERM, rolling update под нагрузкой | api_tests, persistence, sigterm, failovers, rolling_update | +| S3 в общем стенде; metadata в PostgreSQL, bytes в приватном bucket | Docker inspect, прямой S3 GET, проверки прав | containers, attachments.private_bucket | +| 201 ready, Message с attachment_ids, SHA-256 и побайтный download | Публичный smoke и test_attachments | lab4-checks.txt; api_tests | +| Реальные PNG/JPEG, MIME, 10 MiB и 40 MP, опасные filename | test_png_and_jpeg_originals, corruption_and_mime_spoof, pixel_limit_before_decode, filename_rejected_before_storage | 39 собственных тестов; lab4-checks.txt | +| Upload → остановка загрузившей реплики → download другой | Сопоставление X-Instance-Id и Docker ID, реальная stop-one | attachments.upload_replica_failover | +| Restart сохраняет metadata/bytes/действующую сессию | Публичный persistence, StartedAt всех процессов до/после | persistence | +| Слишком большой файл, повреждение, подмена MIME, чужой ID/другой чат не дают 500 | Отдельные негативные HTTP-тесты, включая chunked без Content-Length | test_attachments; lab4-checks.txt | +| Прерывание загрузки и сбой между S3/БД не оставляют вечные неучтённые объекты | Обрыв сокета; trigger отказывает после PUT; затем ручной и автоматический cleaner | attachments.interrupted_body, compensation | +| Безопасный grace period, защита активных/готовых объектов, повторная очистка | Свежий журнал, backdate, удержание advisory lock, ready-ссылка, повтор DELETE | compensation.fresh_kept/active_locked_kept/ready_kept/repeat_safe/automatic_worker | +| Ограничены RAM/таймауты; ≥10 одновременных загрузок с замером памяти | Barrier на 10 клиентах, настоящие PNG 1600×1200, cgroup каждые 20 ms, SHA-256 всех downloads | attachments.parallel_uploads | +| Идемпотентный bootstrap и ограниченные S3 credentials; закрытая администрация | Два bootstrap, запрещённые S3 операции, TCP-пробы из API | attachments.private_bucket | +| Недоступность S3 и восстановление | Остановка настоящего s3-service, запросы внутри API в обход исключившего backend LB | attachments.s3_outage; dependency_readiness | + +Все актуальные результаты — [evidence/lab4-checks.txt](evidence/lab4-checks.txt) и [evidence/lab4-checks.json](evidence/lab4-checks.json). Токены, пароли и исходные тела запросов туда не сохраняются. PASS публичного smoke сам по себе не считается доказательством всех критериев. + +## Эксперимент памяти и отказы + +Гипотеза: десять одновременных загрузок не вызывают десять полных декодирований на API; слот и лимиты удерживают RAM ниже контейнерного лимита, оригиналы сохраняются без изменений. Методика: один случайный несжимаемый RGB PNG 1600×1200 (~5.5 MiB), десять потоков с общим barrier, две API-реплики; после каждого 201 второй участник скачивает оригинал, проверяется SHA-256. Монитор в каждой API читает cgroup v2 memory.current каждые 20 ms. Сохраняются исходные временные ряды, memory.peak за всю жизнь контейнера и лимит, duration, successful RPS, p50/p95, число ошибок и OOMKilled. Файл генерируется заново при каждом запуске; его точный размер/checksum записаны в JSON. + +Замер включает Python-процесс монитора и файловый cache cgroup; это консервативная оценка контейнерной памяти, а не чистый RSS приложения. Пик выборки может пропустить события короче 20 ms; memory.peak дополнительно показывает пик за жизнь контейнера. Это один ограниченный учебный прогон с 10 клиентами, без поиска предельной производительности. p50/p95 относятся к успешным HTTP upload; coordinated omission и влияние клиента остаются ограничениями. + +### Результат фактического прогона 05.11.2026 + +Среда: Darwin arm64; Docker 29.0.1 aarch64 CPUs=10 RAM=8217448448; Compose 2.40.3-desktop.1. Две API-реплики, по 1 CPU и 384 MiB. Прогрев — полный smoke и 39 тестов до эксперимента; накопившаяся память отражена в baseline. Временный project: lab4-check-753ba80cb3. + +| Показатель | Наблюдение | +| --- | --- | +| Параллельные uploads | 10 из 10, ошибок 0 | +| Один PNG | 5765613 bytes, 1600×1200 RGB | +| Длительность / успешные upload/s | 0.805 s / 12.426 | +| p50 / p95 HTTP upload | 596.684 / 798.322 ms | +| Оригиналы | 10 download, все SHA-256 совпали | +| OOM | Нет | + +| API instance | Baseline cgroup, MiB | Пик выборки во время uploads, MiB | Пик cgroup за жизнь контейнера, MiB | Лимит, MiB | +| --- | ---: | ---: | ---: | ---: | +| 887ffd1465a8 | 179.97 | 194.26 | 261.37 | 384 | +| 8f41208a6406 | 63.74 | 86.14 | 88.71 | 384 | + +Различие baseline между репликами возникло после предыдущих проверок и аллокаций; сравнение не трактуется как чистое измерение стоимости одного декодирования. Пик за жизнь контейнера включает более ранние тесты. Гипотеза о соблюдении лимита в этом профиле подтверждена; универсальная производительность или поведение при произвольном числе клиентов не выводятся. + +| Воздействие | Наблюдение | Восстановление / потеря подтверждённых данных | +| --- | --- | --- | +| Остановлена загрузившая API-реплика 8f41208a6406 | Download обслужила 887ffd1465a8; исходный SHA-256 совпал | 3.589 s от начала stop; потерь нет | +| Прервано HTTP-тело | Metadata и журнал не созданы; S3 PUT не начинался | Очистка временного файла; ready-объектов не затрагивает | +| БД отказала после S3 PUT | HTTP 503; объект существует вместе с журналом; ready-metadata отсутствует | Свежий/активный объект сохранён; после искусственного старения безопасно удалён; worker проверен отдельно | +| S3 service остановлен | live=200, ready/upload/download=503 в едином формате API | После старта прежний токен и оригинал работают; downtime отдельно не измерялся | +| Restart всего стенда с volumes | StartedAt всех постоянных процессов изменились; сессия, metadata, bytes и повтор сообщения сохранены | Уложилось в бюджет ожидания 90 s; потерь нет | +| SIGTERM с незавершённым POST | Новые соединения прекращены за 0.162 s; exit code 0, без OOM | Выход за 5.448 s, меньше 8 s | + +Дополнительно сохранены результаты отказа каждой реплики и поочерёдной замены под нагрузкой: + +| Сценарий | Длительность, s | Успешные HTTP/s | Переходные ошибки | Макс. пауза POST / GET, s | Потери / дубли | +| --- | ---: | ---: | ---: | --- | --- | +| stop 887ffd1465a8 | 16.012 | 92.43 | 4 | 2.042 / 1.984 | 0 / 0 | +| stop 8f41208a6406 | 16.026 | 91.85 | 4 | 1.986 / 1.99 | 0 / 0 | +| sequential API replacement | 22.153 | 88.47 | 8 | 1.048 / 1.031 | 0 / 0 | + +Этот профиль из lab3 использует четыре закрытых клиента: POST сообщения и GET истории, пауза 50 ms. При неопределённом POST повторяется тот же ключ/тело. Длительность включает 1 s прогрева и воздействие; исходные посекундные счётчики есть в JSON. Это проверка восстановления и отсутствия потерь, а не поиск предельного RPS. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Гарантируется сохранность при проверенном обычном restart с volumes и отказе одной API-реплики при доступных зависимостях. PostgreSQL, Valkey, S3 и LB остаются отдельными точками отказа; HA хранилищ, отказ хоста/электропитания и backup этой работы не заявлены. HEAD bucket в readiness не является тестом записи каждого объекта. Потеря или удаление volumes уничтожает соответствующие данные. + +После 201 данные доступны всем участникам через любую готовую реплику. Неопределённый ответ upload может привести к ещё одному готовому вложению при повторе: идемпотентность upload контрактом не предусмотрена. Cleaner удаляет только незавершённые журналируемые операции; потеря одновременно БД/журнала или ручная запись посторонних объектов требует работы оператора. При длительной недоступности S3/БД очистка откладывается до восстановления. + +Слоты и ресурсные лимиты ограничивают память, но не обещают отсутствие 503 при произвольной нагрузке. Временный spool хранится на ephemeral disk API только во время запроса. Для удалённого стенда нужен TLS; текущий HTTP привязан к localhost и использует синтетические учебные данные. + +## Первичные источники + +- [SeaweedFS 3.85: команды сервера](https://github.com/seaweedfs/seaweedfs/blob/3.85/weed/command/server.go). +- [SeaweedFS 3.85: IAM и ограничения действий](https://github.com/seaweedfs/seaweedfs/blob/3.85/weed/s3api/auth_credentials.go). +- [MinIO Python SDK](https://minio-py.min.io/). +- [Pillow Image: open, verify, load и лимиты](https://pillow.readthedocs.io/en/stable/reference/Image.html). diff --git a/compose.yaml b/compose.yaml index ab60a4e..3835171 100644 --- a/compose.yaml +++ b/compose.yaml @@ -2,10 +2,9 @@ name: mtusi-lab4 x-application: &application image: mtusi-messenger:lab4 - build: . env_file: .env cpus: 1 - mem_limit: 256m + mem_limit: 384m x-postgres: &postgres image: postgres:17.6-bookworm @@ -41,15 +40,18 @@ services: api: <<: *application + build: . deploy: replicas: ${API_REPLICAS:-2} environment: SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} - secrets: [database_url, cache_url] + secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key] depends_on: postgres: {condition: service_healthy} valkey: {condition: service_healthy} migrate: {condition: service_completed_successfully} + s3: {condition: service_healthy} + s3-bootstrap: {condition: service_completed_successfully} stop_grace_period: 8s migrate: @@ -61,6 +63,59 @@ services: depends_on: postgres: {condition: service_healthy} + s3: + image: chrislusf/seaweedfs:3.85@sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a + command: ["s3", "-filer=s3-storage:8888", "-ip.bind=0.0.0.0", "-port=9000", "-config=/run/secrets/s3_config"] + secrets: [s3_config] + networks: [default, s3-internal] + depends_on: + s3-storage: {condition: service_healthy} + cpus: 0.5 + mem_limit: 256m + healthcheck: + test: ["CMD", "nc", "-z", "127.0.0.1", "9000"] + interval: 2s + timeout: 2s + retries: 30 + + s3-storage: + image: chrislusf/seaweedfs:3.85@sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a + command: ["server", "-dir=/data", "-ip=s3-storage", "-ip.bind=0.0.0.0", "-filer", "-master.volumeSizeLimitMB=64", "-volume.max=64", "-volume.preStopSeconds=0"] + volumes: [s3_data:/data] + networks: [s3-internal] + cpus: 1 + mem_limit: 512m + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8888/"] + interval: 2s + timeout: 2s + retries: 30 + + s3-bootstrap: + <<: *application + command: ["python", "-m", "src.bootstrap_s3"] + environment: + S3_ROOT_USER_FILE: /run/secrets/s3_root_user + S3_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password + secrets: [s3_root_user, s3_root_password] + cpus: 0.5 + mem_limit: 128m + healthcheck: + disable: true + depends_on: + s3: {condition: service_healthy} + + cleanup: + <<: *application + command: ["python", "-m", "src.cleanup"] + secrets: [database_url, s3_access_key_id, s3_secret_access_key] + restart: unless-stopped + healthcheck: + disable: true + depends_on: + migrate: {condition: service_completed_successfully} + s3-bootstrap: {condition: service_completed_successfully} + lb: image: haproxy:3.2.6-alpine ports: ["127.0.0.1:${HOST_PORT:-8080}:8080"] @@ -85,6 +140,11 @@ volumes: postgres_data: valkey_data: restore_data: + s3_data: + +networks: + s3-internal: + internal: true secrets: postgres_password: @@ -93,3 +153,13 @@ secrets: file: ./secrets/database_url cache_url: file: ./secrets/cache_url + s3_root_user: + file: ./secrets/s3_root_user + s3_root_password: + file: ./secrets/s3_root_password + s3_access_key_id: + file: ./secrets/s3_access_key_id + s3_secret_access_key: + file: ./secrets/s3_secret_access_key + s3_config: + file: ./secrets/s3_config diff --git a/docs/schema.md b/docs/schema.md index 3cf4052..52143c8 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -1,25 +1,33 @@ # Схема и транзакции -Лабораторная 3, оценка 4: HTTP → HAProxy → FastAPI × 2 → PostgreSQL; -Bearer-сессии находятся в общем Valkey. Схема lab2 сохранена. +Лабораторная 4, оценка 4: клиент → HAProxy → FastAPI × 2 → PostgreSQL + Valkey + приватный S3 (SeaweedFS). | Таблица PostgreSQL | Назначение и ограничения | | --- | --- | -| users | UUID PK; username UNIQUE; display_name; password_hash Argon2id; created_at timestamptz | -| chats | UUID PK; title; created_at timestamptz | +| users | UUID PK; username UNIQUE; password_hash Argon2id; display_name; created_at | +| chats | UUID PK; title; created_at | | chat_members | PK (chat_id, user_id); FK на chats и users | -| messages | UUID PK; FK chat_id на chats; составной FK (chat_id, sender_id) на chat_members; text; created_at timestamptz | -| schema_migrations | Имя и SHA-256 применённой SQL-миграции; время применения | -| message_idempotency | PK (user_id, chat_id, key); FK на пользователя, чат и сообщение; request_text; expires_at | +| messages | UUID PK; FK на чат и членство отправителя; text до 4000 code points; created_at | +| message_attachments | FK на messages и attachments; до 10 позиций; UNIQUE(message_id, position); PK(message_id, attachment_id) | +| attachments | UUID PK; FK chat_id и (chat_id, owner_id); UNIQUE object_key; filename, content_type, size_bytes, sha256, created_at | +| attachment_uploads | UUID PK, UNIQUE object_key, created_at; долговечный журнал незавершённых PUT | +| message_idempotency | PK (user_id, chat_id, key); request_text, request_attachment_ids, message_id FK, expires_at | +| schema_migrations | Имя и SHA-256 применённой миграции; applied_at | -Индексы: users(username) создаётся UNIQUE-ограничением; chat_members(user_id, chat_id) нужен для списка чатов пользователя; messages(chat_id, created_at, id) нужен для истории с keyset cursor. PK служат индексами поиска по UUID. +В PostgreSQL нет bytes оригинала. `object_key=attachments/<случайный UUID>` назначает сервер; filename не участвует в ключе. В таблицу attachments попадают только ready-объекты; status=ready, variants=[], error_code=null формируются API. updated_at совпадает с created_at, поскольку API lab4 не изменяет вложения. -Каждая операция хранилища выполняется в одной транзакции: контекст соединения Psycopg делает COMMIT при успехе и ROLLBACK при исключении. Создание чата сначала вставляет chats, затем участников. Неизвестный участник вызывает FK violation и откатывает весь чат. Проверка уникальности username выполняется ограничением БД, включая конкурентные запросы. Создание сообщения проверяет доступ и записывает сообщение в одной транзакции; составной FK дополнительно запрещает автора вне чата. +Индексы: UNIQUE users(username); chat_members(user_id, chat_id); messages(chat_id, created_at, id); attachments(chat_id); attachment_uploads(created_at). Первичные/уникальные ключи индексируют поиск ID и object_key. -Идемпотентная отправка проверяет членство, берёт общую транзакционную advisory lock для пользователя/метода/пути/ключа и проверяет предыдущий результат. Тот же text даёт прежнее Message, другой — 409. Сообщение и строка message_idempotency фиксируются одной транзакцией; при откате не сохраняется ни одно. TTL не меньше 24 часов. Истёкшая строка заменяется при повторном использовании ключа; старое сообщение сохраняется. +Создание чата и его участников атомарно. Неизвестный участник откатывает весь чат. Сообщение проверяет членство и все attachment_ids в том же чате, затем одной транзакцией записывает messages, message_attachments и результат идемпотентности. Составной FK дополнительно запрещает отправителя вне чата. Пустой/пробельный text разрешён только с вложением: это проверяет модель запроса; FK таблицы message_attachments запрещают несуществующие ссылки. Удаления вложений в HTTP API нет. -Миграции выполняет отдельный одноразовый Compose-service migrate под advisory transaction lock. API только проверяют версии и checksum. Повторное применение пропускает уже записанную версию, изменение применённого SQL выявляется по checksum. Схема и запись версии фиксируются вместе. Миграция 002 только добавляет таблицу; существующая 001 не изменялась. +Общая advisory transaction lock сериализует область пользователь + POST + путь чата + Idempotency-Key. Сравниваются text и упорядоченный нормализованный список UUID; отсутствие attachment_ids эквивалентно []. Совпадение возвращает исходный 201 и Message, отличие — 409. TTL не меньше 24 часов. Чтение истории использует keyset (created_at,id), без кэша; на следующем запросе видны COMMIT любой реплики. При конкурентных вставках snapshot между страницами нет; запись с более ранними часами может оказаться до уже пройденного cursor. -Valkey — источник истины только для сессий: ключ session:SHA256(token), значение — user_id и абсолютное expires_at, TTL задаётся SESSION_TTL_SECONDS. Сырой токен выдаётся клиенту один раз и не записывается в логи. Включены AOF, appendfsync always и именованный volume. Logout удаляет ключ; исчезнувшая или просроченная сессия даёт 401. При недоступности Valkey — 503 без обхода аутентификации. При потере PostgreSQL — 503. /health/live остаётся доступным, /health/ready проверяет оба сервиса. +Upload сначала полностью принимает ограниченный поток во временный файл и проверяет PNG/JPEG. Затем отдельной транзакцией фиксирует attachment_uploads. В следующей транзакции берётся advisory lock object_key, выполняется S3 PUT, вставляются ready-метаданные и удаляется журнал. 201 отправляется после COMMIT. S3 и PostgreSQL не образуют распределённую транзакцию. -Исследование индекса и backup/restore в research/ перенесено из lab2. Для оценки 4 текущей лабораторной оно не требуется; результаты отказов и замены реплик описаны в [REPORT.md](../REPORT.md). +Если PUT прошёл, а COMMIT нет, остаётся журнал. Если исход COMMIT неизвестен, немедленного удаления нет: ready-метаданные могли сохраниться. Cleaner раз в 30 секунд берёт до 100 записей старше grace period (по умолчанию 3600 s, минимум 60 s), пробует ту же advisory lock и повторно проверяет запись. Активная загрузка пропускается; при наличии ready-метаданных удаляется только запись журнала; иначе удаляется S3 object и затем журнал. DELETE объекта идемпотентен. Сбой S3/БД откладывает обработку; следующий проход повторяет её. Это компенсация журналируемых загрузок уровня 4, без полного обхода bucket. + +Миграции 001 и 002 сохранены. Миграция 003 добавляет вложения, связи, журнал и список вложений в ключе повтора; старые сообщения имеют пустой список. DDL выполняет один migrate-service под общей advisory lock; API проверяют версии и checksum. + +Valkey хранит только серверные сессии: session:SHA256(token) → user_id, expires_at, TTL. Включены AOF appendfsync always и постоянный volume. Сырой токен не записывается в evidence. PostgreSQL, Valkey и S3 bucket проверяются /health/ready; /health/live не обращается к зависимостям. + +Исходные отчёты и результаты lab2/lab3 сохранены отдельно. Актуальные команды, измерения и ограничения — в [REPORT.md](../REPORT.md). diff --git a/evidence/lab4-checks.json b/evidence/lab4-checks.json new file mode 100644 index 0000000..9104484 --- /dev/null +++ b/evidence/lab4-checks.json @@ -0,0 +1,2154 @@ +{ + "utc": "2026-11-05T16:13:18.569036+00:00", + "project": "lab4-check-753ba80cb3", + "base_url": "http://127.0.0.1:65032", + "host": "Darwin arm64", + "docker": "29.0.1 aarch64 CPUs=10 RAM=8217448448", + "profile": { + "workers": 4, + "pause_seconds": 0.05, + "operation": "POST message then GET history, same-key retry after uncertain POST" + }, + "compose_version": "2.40.3-desktop.1", + "source_commit": "5c9b3e206a07c0346c9765923cd9560358c3a420", + "working_tree": true, + "containers": [ + { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:46.833090004Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:46.727185879Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "10143cbf3e42de96c7a3078ea5b42d24bbf9702a4f46e57d4a9aa2a7ffbbab61", + "instance": "10143cbf3e42", + "service": "cleanup", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:46.726778921Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "aecf8751126b66c807155750bf349fa6af90aa2a322bf078bd903bae7fba5b53", + "instance": "aecf8751126b", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-11-05T16:13:52.440463757Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "65032" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "5274aa99b987b7af4cc7680c634cb6a8d9c38d05f6ff0f49dac61c48d7844629", + "instance": "5274aa99b987", + "service": "migrate", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:23.022573091Z", + "running": false, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "1d892ce3140664f694767e9737f67ab691ca4e25b8129874d2e2fe1f20bbddda", + "instance": "1d892ce31406", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-11-05T16:13:20.380429756Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab4-check-753ba80cb3_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "3c9d7b1f26816060cb94a7bcf246d06c04c1fa3c6cf104f38e051eca1c11bfe1", + "instance": "3c9d7b1f2681", + "service": "s3", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:13:43.008405419Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "5884fdd395ed5d5bb60b03bbb71fdd685825042d009cda0a137b77e42fef1ebf" + } + ] + }, + { + "id": "1615ae74805ae0ca3d9bd98fe25ea40ef8cd2db8fe7cb8c6938f8a427e3bc2db", + "instance": "1615ae74805a", + "service": "s3-bootstrap", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:45.622155504Z", + "running": false, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_root_user", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_root_password", + "name": null + } + ] + }, + { + "id": "b403c79e680014fba72123d64d7742d70a57a4a08b4c7cd6f036eb752a63456e", + "instance": "b403c79e6800", + "service": "s3-storage", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:13:20.382611881Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_s3_data" + } + ] + }, + { + "id": "030c8bacc201f114875911d3e3ddd3ee159a0a266d849104f775b2615b941937", + "instance": "030c8bacc201", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-11-05T16:13:20.381988089Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_valkey_data" + } + ] + } + ], + "api_tests": "public smoke + student unittest suite passed", + "attachments": { + "private_bucket": { + "anonymous_get": 403, + "create_bucket": "AccessDenied", + "write_outside_prefix": "AccessDenied", + "admin_ports_unreachable": [ + 9333, + 8888, + 8080 + ], + "storage_network_isolated": true, + "bootstrap_runs": 2 + }, + "upload_replica_failover": { + "uploaded_by": "8f41208a6406", + "stopped_container": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "downloaded_by": "887ffd1465a8", + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "recovery_seconds": 3.589 + }, + "interrupted_body": { + "metadata_or_journal_created": false + }, + "compensation": { + "failure_after_put": 503, + "orphan_recorded": true, + "fresh_kept": true, + "active_locked_kept": true, + "expired_removed": true, + "ready_kept": true, + "repeat_safe": true, + "automatic_worker": true + }, + "s3_outage": { + "live": 200, + "ready": 503, + "download": 503, + "upload": 503, + "original_preserved_after_recovery": true + }, + "parallel_uploads": { + "concurrency": 10, + "successful": 10, + "errors": 0, + "dimensions": [ + 1600, + 1200 + ], + "size_bytes": 5765613, + "sha256": "79199b764c56a75da074e2cc1157cc471d53d23dc1cb141edba352d25e376ac3", + "duration_seconds": 0.805, + "successful_rps": 12.426, + "p50_ms": 596.684, + "p95_ms": 798.322, + "memory_sample_interval_seconds": 0.02, + "memory_includes_monitor_process": true, + "memory": [ + { + "baseline_bytes": 188715008, + "sample_peak_bytes": 203698176, + "lifetime_peak_bytes": 274067456, + "samples": [ + [ + 0.0, + 188715008 + ], + [ + 0.021, + 188608512 + ], + [ + 0.042, + 188723200 + ], + [ + 0.063, + 188723200 + ], + [ + 0.084, + 188723200 + ], + [ + 0.105, + 188731392 + ], + [ + 0.127, + 188727296 + ], + [ + 0.149, + 188723200 + ], + [ + 0.17, + 188874752 + ], + [ + 0.191, + 190881792 + ], + [ + 0.212, + 192102400 + ], + [ + 0.233, + 193662976 + ], + [ + 0.254, + 193961984 + ], + [ + 0.275, + 194826240 + ], + [ + 0.296, + 195448832 + ], + [ + 0.317, + 196419584 + ], + [ + 0.338, + 197005312 + ], + [ + 0.359, + 198537216 + ], + [ + 0.38, + 198995968 + ], + [ + 0.401, + 201351168 + ], + [ + 0.422, + 202309632 + ], + [ + 0.443, + 202698752 + ], + [ + 0.464, + 203296768 + ], + [ + 0.486, + 203456512 + ], + [ + 0.507, + 203698176 + ], + [ + 0.528, + 203694080 + ], + [ + 0.549, + 203694080 + ], + [ + 0.57, + 203694080 + ], + [ + 0.591, + 203689984 + ], + [ + 0.613, + 203694080 + ], + [ + 0.635, + 203411456 + ], + [ + 0.656, + 203218944 + ], + [ + 0.677, + 203214848 + ], + [ + 0.698, + 203214848 + ], + [ + 0.72, + 202133504 + ], + [ + 0.744, + 201838592 + ], + [ + 0.764, + 201822208 + ], + [ + 0.785, + 195977216 + ], + [ + 0.806, + 198823936 + ], + [ + 0.827, + 198758400 + ], + [ + 0.848, + 198758400 + ], + [ + 0.869, + 198762496 + ], + [ + 0.89, + 196005888 + ], + [ + 0.911, + 195731456 + ], + [ + 0.932, + 195731456 + ] + ], + "instance": "887ffd1465a8", + "limit_bytes": 402653184 + }, + { + "baseline_bytes": 66834432, + "sample_peak_bytes": 90324992, + "lifetime_peak_bytes": 93024256, + "samples": [ + [ + 0.0, + 66834432 + ], + [ + 0.02, + 67706880 + ], + [ + 0.041, + 69509120 + ], + [ + 0.062, + 70037504 + ], + [ + 0.083, + 71856128 + ], + [ + 0.104, + 72515584 + ], + [ + 0.125, + 73310208 + ], + [ + 0.146, + 73994240 + ], + [ + 0.167, + 75051008 + ], + [ + 0.188, + 75055104 + ], + [ + 0.21, + 77008896 + ], + [ + 0.23, + 77328384 + ], + [ + 0.251, + 80379904 + ], + [ + 0.272, + 82636800 + ], + [ + 0.293, + 82620416 + ], + [ + 0.314, + 83005440 + ], + [ + 0.336, + 83689472 + ], + [ + 0.356, + 87851008 + ], + [ + 0.378, + 89878528 + ], + [ + 0.398, + 89747456 + ], + [ + 0.419, + 89747456 + ], + [ + 0.44, + 87896064 + ], + [ + 0.462, + 88268800 + ], + [ + 0.483, + 90234880 + ], + [ + 0.503, + 90218496 + ], + [ + 0.524, + 89128960 + ], + [ + 0.545, + 90324992 + ], + [ + 0.567, + 90263552 + ], + [ + 0.588, + 90259456 + ], + [ + 0.609, + 85856256 + ], + [ + 0.63, + 86413312 + ], + [ + 0.65, + 86913024 + ], + [ + 0.671, + 86097920 + ], + [ + 0.693, + 86142976 + ], + [ + 0.713, + 83120128 + ], + [ + 0.734, + 83083264 + ], + [ + 0.756, + 83079168 + ], + [ + 0.776, + 77225984 + ], + [ + 0.798, + 77201408 + ], + [ + 0.818, + 77135872 + ] + ], + "instance": "8f41208a6406", + "limit_bytes": 402653184 + } + ], + "all_original_checksums_verified": true, + "oom_killed": false + } + }, + "persistence": { + "before": [ + { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:13:46.833090004Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:14:24.166448674Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "10143cbf3e42de96c7a3078ea5b42d24bbf9702a4f46e57d4a9aa2a7ffbbab61", + "instance": "10143cbf3e42", + "service": "cleanup", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:14:50.281562506Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "aecf8751126b66c807155750bf349fa6af90aa2a322bf078bd903bae7fba5b53", + "instance": "aecf8751126b", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-11-05T16:13:52.440463757Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "65032" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "1d892ce3140664f694767e9737f67ab691ca4e25b8129874d2e2fe1f20bbddda", + "instance": "1d892ce31406", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-11-05T16:13:20.380429756Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab4-check-753ba80cb3_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "3c9d7b1f26816060cb94a7bcf246d06c04c1fa3c6cf104f38e051eca1c11bfe1", + "instance": "3c9d7b1f2681", + "service": "s3", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:14:43.155925128Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "5884fdd395ed5d5bb60b03bbb71fdd685825042d009cda0a137b77e42fef1ebf" + } + ] + }, + { + "id": "b403c79e680014fba72123d64d7742d70a57a4a08b4c7cd6f036eb752a63456e", + "instance": "b403c79e6800", + "service": "s3-storage", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:13:20.382611881Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_s3_data" + } + ] + }, + { + "id": "030c8bacc201f114875911d3e3ddd3ee159a0a266d849104f775b2615b941937", + "instance": "030c8bacc201", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-11-05T16:13:20.381988089Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_valkey_data" + } + ] + } + ], + "after": [ + { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:14:58.905051635Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:14:59.190444177Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "10143cbf3e42de96c7a3078ea5b42d24bbf9702a4f46e57d4a9aa2a7ffbbab61", + "instance": "10143cbf3e42", + "service": "cleanup", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:15:06.604429847Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "aecf8751126b66c807155750bf349fa6af90aa2a322bf078bd903bae7fba5b53", + "instance": "aecf8751126b", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-11-05T16:14:58.790557135Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "65032" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "1d892ce3140664f694767e9737f67ab691ca4e25b8129874d2e2fe1f20bbddda", + "instance": "1d892ce31406", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-11-05T16:14:58.812168052Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab4-check-753ba80cb3_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "3c9d7b1f26816060cb94a7bcf246d06c04c1fa3c6cf104f38e051eca1c11bfe1", + "instance": "3c9d7b1f2681", + "service": "s3", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:14:58.840776635Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "5884fdd395ed5d5bb60b03bbb71fdd685825042d009cda0a137b77e42fef1ebf" + } + ] + }, + { + "id": "b403c79e680014fba72123d64d7742d70a57a4a08b4c7cd6f036eb752a63456e", + "instance": "b403c79e6800", + "service": "s3-storage", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:15:06.569589264Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_s3_data" + } + ] + }, + { + "id": "030c8bacc201f114875911d3e3ddd3ee159a0a266d849104f775b2615b941937", + "instance": "030c8bacc201", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-11-05T16:14:58.861777302Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_valkey_data" + } + ] + } + ], + "passed": true, + "idempotency_replay_after_restart": true + }, + "sigterm": { + "instance": "887ffd1465a8", + "unfinished_request": true, + "shutdown_seconds": 5.448, + "new_connections_refused_seconds": 0.162, + "exit_code": 0, + "oom_killed": false + }, + "dependency_readiness": [ + { + "dependency": "valkey", + "api_live": 200, + "api_ready": 503, + "lb_ready": 503 + }, + { + "dependency": "postgres", + "api_live": 200, + "api_ready": 503, + "lb_ready": 503 + }, + { + "dependency": "s3", + "api_live": 200, + "api_ready": 503, + "lb_ready": 503 + } + ], + "public_failover": { + "passed": true, + "containers_after": [ + { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:15:39.329511417Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:14:59.190444177Z", + "running": false, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + } + ] + }, + "failovers": [ + { + "label": "stop 887ffd1465a8", + "duration_seconds": 16.012, + "successful_requests": 1480, + "successful_writes": 739, + "errors": 4, + "successful_rps": 92.43, + "first_success_after_action_seconds": { + "POST": 0.006, + "GET": 0.008 + }, + "maximum_success_gap_seconds": { + "POST": 2.042, + "GET": 1.984 + }, + "action_start_seconds": 1.001, + "action_end_seconds": 1.708, + "p50_ms": 9.065, + "p95_ms": 18.196, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "887ffd1465a8", + "8f41208a6406" + ], + "per_second": [ + { + "second": 0, + "successes": 117, + "errors": 0 + }, + { + "second": 1, + "successes": 38, + "errors": 0 + }, + { + "second": 2, + "successes": 0, + "errors": 0 + }, + { + "second": 3, + "successes": 62, + "errors": 4 + }, + { + "second": 4, + "successes": 107, + "errors": 0 + }, + { + "second": 5, + "successes": 104, + "errors": 0 + }, + { + "second": 6, + "successes": 105, + "errors": 0 + }, + { + "second": 7, + "successes": 112, + "errors": 0 + }, + { + "second": 8, + "successes": 97, + "errors": 0 + }, + { + "second": 9, + "successes": 111, + "errors": 0 + }, + { + "second": 10, + "successes": 105, + "errors": 0 + }, + { + "second": 11, + "successes": 109, + "errors": 0 + }, + { + "second": 12, + "successes": 99, + "errors": 0 + }, + { + "second": 13, + "successes": 104, + "errors": 0 + }, + { + "second": 14, + "successes": 107, + "errors": 0 + }, + { + "second": 15, + "successes": 101, + "errors": 0 + }, + { + "second": 16, + "successes": 2, + "errors": 0 + } + ], + "error_samples": [ + [ + 3.325078833993757, + "POST", + "POST /api/v1/chats/90558f6b-bac8-4d25-9200-0b6550fc7907/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.3309984169900417, + "GET", + "GET /api/v1/chats/90558f6b-bac8-4d25-9200-0b6550fc7907/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.3486054169916315, + "POST", + "POST /api/v1/chats/90558f6b-bac8-4d25-9200-0b6550fc7907/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.414436291990569, + "POST", + "POST /api/v1/chats/90558f6b-bac8-4d25-9200-0b6550fc7907/messages: connection/TLS failure (TimeoutError)" + ] + ], + "action": { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:15:39.329511417Z", + "running": false, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + } + }, + { + "label": "stop 8f41208a6406", + "duration_seconds": 16.026, + "successful_requests": 1472, + "successful_writes": 735, + "errors": 4, + "successful_rps": 91.85, + "first_success_after_action_seconds": { + "POST": 0.029, + "GET": 0.038 + }, + "maximum_success_gap_seconds": { + "POST": 1.986, + "GET": 1.99 + }, + "action_start_seconds": 1.011, + "action_end_seconds": 1.806, + "p50_ms": 9.112, + "p95_ms": 20.767, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "887ffd1465a8", + "8f41208a6406" + ], + "per_second": [ + { + "second": 0, + "successes": 112, + "errors": 0 + }, + { + "second": 1, + "successes": 45, + "errors": 0 + }, + { + "second": 2, + "successes": 0, + "errors": 0 + }, + { + "second": 3, + "successes": 53, + "errors": 4 + }, + { + "second": 4, + "successes": 107, + "errors": 0 + }, + { + "second": 5, + "successes": 107, + "errors": 0 + }, + { + "second": 6, + "successes": 107, + "errors": 0 + }, + { + "second": 7, + "successes": 111, + "errors": 0 + }, + { + "second": 8, + "successes": 99, + "errors": 0 + }, + { + "second": 9, + "successes": 108, + "errors": 0 + }, + { + "second": 10, + "successes": 100, + "errors": 0 + }, + { + "second": 11, + "successes": 107, + "errors": 0 + }, + { + "second": 12, + "successes": 112, + "errors": 0 + }, + { + "second": 13, + "successes": 99, + "errors": 0 + }, + { + "second": 14, + "successes": 103, + "errors": 0 + }, + { + "second": 15, + "successes": 98, + "errors": 0 + }, + { + "second": 16, + "successes": 4, + "errors": 0 + } + ], + "error_samples": [ + [ + 3.379369415997644, + "POST", + "POST /api/v1/chats/4f88aa82-8938-466a-95a0-d514336024ea/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.3918063749879366, + "POST", + "POST /api/v1/chats/4f88aa82-8938-466a-95a0-d514336024ea/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.399761665990809, + "POST", + "POST /api/v1/chats/4f88aa82-8938-466a-95a0-d514336024ea/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.473328790991218, + "GET", + "GET /api/v1/chats/4f88aa82-8938-466a-95a0-d514336024ea/messages: connection/TLS failure (TimeoutError)" + ] + ], + "action": { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:16:18.896292297Z", + "running": false, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + } + } + ], + "rolling_update": { + "label": "sequential API replacement", + "duration_seconds": 22.153, + "successful_requests": 1960, + "successful_writes": 979, + "errors": 8, + "successful_rps": 88.47, + "first_success_after_action_seconds": { + "POST": 0.035, + "GET": 0.041 + }, + "maximum_success_gap_seconds": { + "POST": 1.048, + "GET": 1.031 + }, + "action_start_seconds": 1.004, + "action_end_seconds": 19.149, + "p50_ms": 8.859, + "p95_ms": 17.468, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "4479505e52f5", + "887ffd1465a8", + "8f41208a6406", + "9be50a7967ed" + ], + "per_second": [ + { + "second": 0, + "successes": 120, + "errors": 0 + }, + { + "second": 1, + "successes": 105, + "errors": 0 + }, + { + "second": 2, + "successes": 110, + "errors": 0 + }, + { + "second": 3, + "successes": 102, + "errors": 0 + }, + { + "second": 4, + "successes": 111, + "errors": 0 + }, + { + "second": 5, + "successes": 52, + "errors": 0 + }, + { + "second": 6, + "successes": 3, + "errors": 4 + }, + { + "second": 7, + "successes": 51, + "errors": 0 + }, + { + "second": 8, + "successes": 102, + "errors": 0 + }, + { + "second": 9, + "successes": 110, + "errors": 0 + }, + { + "second": 10, + "successes": 108, + "errors": 0 + }, + { + "second": 11, + "successes": 113, + "errors": 0 + }, + { + "second": 12, + "successes": 30, + "errors": 0 + }, + { + "second": 13, + "successes": 43, + "errors": 4 + }, + { + "second": 14, + "successes": 28, + "errors": 0 + }, + { + "second": 15, + "successes": 108, + "errors": 0 + }, + { + "second": 16, + "successes": 106, + "errors": 0 + }, + { + "second": 17, + "successes": 112, + "errors": 0 + }, + { + "second": 18, + "successes": 106, + "errors": 0 + }, + { + "second": 19, + "successes": 109, + "errors": 0 + }, + { + "second": 20, + "successes": 103, + "errors": 0 + }, + { + "second": 21, + "successes": 112, + "errors": 0 + }, + { + "second": 22, + "successes": 16, + "errors": 0 + } + ], + "error_samples": [ + [ + 6.467671833001077, + "POST", + "POST /api/v1/chats/07035b84-deb6-4ad8-ba1e-5b435fcf227e/messages: expected [201], received 503" + ], + [ + 6.489907541996217, + "POST", + "POST /api/v1/chats/07035b84-deb6-4ad8-ba1e-5b435fcf227e/messages: expected [201], received 503" + ], + [ + 6.499776625001687, + "GET", + "GET /api/v1/chats/07035b84-deb6-4ad8-ba1e-5b435fcf227e/messages: expected [200], received 503" + ], + [ + 6.534639249992324, + "GET", + "GET /api/v1/chats/07035b84-deb6-4ad8-ba1e-5b435fcf227e/messages: expected [200], received 503" + ], + [ + 13.272986249998212, + "POST", + "POST /api/v1/chats/07035b84-deb6-4ad8-ba1e-5b435fcf227e/messages: expected [201], received 503" + ] + ], + "action": { + "before": [ + { + "id": "887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710", + "instance": "887ffd1465a8", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:16:43.393904378Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158", + "instance": "8f41208a6406", + "service": "api", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:17:07.789637667Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "10143cbf3e42de96c7a3078ea5b42d24bbf9702a4f46e57d4a9aa2a7ffbbab61", + "instance": "10143cbf3e42", + "service": "cleanup", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:15:06.604429847Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "aecf8751126b66c807155750bf349fa6af90aa2a322bf078bd903bae7fba5b53", + "instance": "aecf8751126b", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-11-05T16:14:58.790557135Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "65032" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "1d892ce3140664f694767e9737f67ab691ca4e25b8129874d2e2fe1f20bbddda", + "instance": "1d892ce31406", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-11-05T16:15:52.946969591Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab4-check-753ba80cb3_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "3c9d7b1f26816060cb94a7bcf246d06c04c1fa3c6cf104f38e051eca1c11bfe1", + "instance": "3c9d7b1f2681", + "service": "s3", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:15:58.478104926Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "5884fdd395ed5d5bb60b03bbb71fdd685825042d009cda0a137b77e42fef1ebf" + } + ] + }, + { + "id": "b403c79e680014fba72123d64d7742d70a57a4a08b4c7cd6f036eb752a63456e", + "instance": "b403c79e6800", + "service": "s3-storage", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:15:06.569589264Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_s3_data" + } + ] + }, + { + "id": "030c8bacc201f114875911d3e3ddd3ee159a0a266d849104f775b2615b941937", + "instance": "030c8bacc201", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-11-05T16:15:46.860700088Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_valkey_data" + } + ] + } + ], + "after": [ + { + "id": "9be50a7967ed59707098e73e42c6f0b054bf4d60ee4dad0a9f8f0ed6afc148e6", + "instance": "9be50a7967ed", + "service": "api", + "image": "sha256:413691a40751a490bab701b9498431365ae10bfd081d55decba718e5e90a0f5d", + "started_at": "2026-11-05T16:17:20.086358506Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "4479505e52f5bf17b2ceb2d0d799a4616bb846086cbbcc99d100aa1dc69571a7", + "instance": "4479505e52f5", + "service": "api", + "image": "sha256:413691a40751a490bab701b9498431365ae10bfd081d55decba718e5e90a0f5d", + "started_at": "2026-11-05T16:17:26.864185551Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + } + ] + }, + { + "id": "10143cbf3e42de96c7a3078ea5b42d24bbf9702a4f46e57d4a9aa2a7ffbbab61", + "instance": "10143cbf3e42", + "service": "cleanup", + "image": "sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5", + "started_at": "2026-11-05T16:15:06.604429847Z", + "running": true, + "exit_code": 0, + "memory_bytes": 402653184, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_access_key_id", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_secret_access_key", + "name": null + } + ] + }, + { + "id": "aecf8751126b66c807155750bf349fa6af90aa2a322bf078bd903bae7fba5b53", + "instance": "aecf8751126b", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-11-05T16:14:58.790557135Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "65032" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "1d892ce3140664f694767e9737f67ab691ca4e25b8129874d2e2fe1f20bbddda", + "instance": "1d892ce31406", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-11-05T16:15:52.946969591Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab4-check-753ba80cb3_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "3c9d7b1f26816060cb94a7bcf246d06c04c1fa3c6cf104f38e051eca1c11bfe1", + "instance": "3c9d7b1f2681", + "service": "s3", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:15:58.478104926Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "5884fdd395ed5d5bb60b03bbb71fdd685825042d009cda0a137b77e42fef1ebf" + } + ] + }, + { + "id": "b403c79e680014fba72123d64d7742d70a57a4a08b4c7cd6f036eb752a63456e", + "instance": "b403c79e6800", + "service": "s3-storage", + "image": "sha256:49312939c00c01e5ee6afbd7d728b18027821d3764c35a797a72acd4fdf3296a", + "started_at": "2026-11-05T16:15:06.569589264Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_s3_data" + } + ] + }, + { + "id": "030c8bacc201f114875911d3e3ddd3ee159a0a266d849104f775b2615b941937", + "instance": "030c8bacc201", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-11-05T16:15:46.860700088Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab4-check-753ba80cb3_valkey_data" + } + ] + } + ] + } + }, + "passed": true +} diff --git a/evidence/lab4-checks.txt b/evidence/lab4-checks.txt new file mode 100644 index 0000000..3eb74f0 --- /dev/null +++ b/evidence/lab4-checks.txt @@ -0,0 +1,531 @@ +Command: ./scripts/check-docker.sh; project: lab4-check-753ba80cb3 +Docker: 29.0.1 aarch64 CPUs=10 RAM=8217448448 +Local .env and secrets are ready; values are not printed +#1 [internal] load local bake definitions +#1 reading from stdin 564B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 733B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 167B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 1.01kB done +#5 DONE 0.0s + +#6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done +#6 DONE 0.0s + +#7 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#7 CACHED + +#8 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#8 CACHED + +#9 [2/8] WORKDIR /app +#9 CACHED + +#10 [7/8] COPY migrations/ ./migrations/ +#10 CACHED + +#11 [3/8] COPY requirements.txt . +#11 CACHED + +#12 [6/8] COPY src/ ./src/ +#12 CACHED + +#13 [8/8] COPY research/ ./research/ +#13 CACHED + +#14 exporting to image +#14 exporting layers done +#14 exporting manifest sha256:1c6cb19983cdfb308e1ec43f59338922d2fec797ff8fdc9636526f9de20a0693 done +#14 exporting config sha256:bc7c1361d0a40f2beb749094a98a091632f75a255c5841fcdf80a33b166720eb done +#14 exporting attestation manifest sha256:ee8ad230ca1748e0ae7559c1966adbe734edbc78280e59f8553b3f6ad1139713 done +#14 exporting manifest list sha256:cb6318e7f06fd815219d3324008ff58c6b6ebb44fb9ea9b76e43546a8b0f78d5 done +#14 naming to docker.io/library/mtusi-messenger:lab4 done +#14 unpacking to docker.io/library/mtusi-messenger:lab4 done +#14 DONE 0.0s + +#15 resolving provenance for metadata file +#15 DONE 0.0s + mtusi-messenger:lab4 Built + Network lab4-check-753ba80cb3_default Creating + Network lab4-check-753ba80cb3_default Created + Network lab4-check-753ba80cb3_s3-internal Creating + Network lab4-check-753ba80cb3_s3-internal Created + Volume lab4-check-753ba80cb3_s3_data Creating + Volume lab4-check-753ba80cb3_s3_data Created + Volume lab4-check-753ba80cb3_postgres_data Creating + Volume lab4-check-753ba80cb3_postgres_data Created + Volume lab4-check-753ba80cb3_valkey_data Creating + Volume lab4-check-753ba80cb3_valkey_data Created + Container lab4-check-753ba80cb3-postgres-1 Creating + Container lab4-check-753ba80cb3-valkey-1 Creating + Container lab4-check-753ba80cb3-s3-storage-1 Creating + Container lab4-check-753ba80cb3-valkey-1 Created + Container lab4-check-753ba80cb3-s3-storage-1 Created + Container lab4-check-753ba80cb3-s3-1 Creating + Container lab4-check-753ba80cb3-postgres-1 Created + Container lab4-check-753ba80cb3-migrate-1 Creating + Container lab4-check-753ba80cb3-s3-1 Created + Container lab4-check-753ba80cb3-s3-bootstrap-1 Creating + Container lab4-check-753ba80cb3-migrate-1 Created + Container lab4-check-753ba80cb3-s3-bootstrap-1 Created + Container lab4-check-753ba80cb3-api-2 Creating + Container lab4-check-753ba80cb3-api-1 Creating + Container lab4-check-753ba80cb3-cleanup-1 Creating + Container lab4-check-753ba80cb3-api-1 Created + Container lab4-check-753ba80cb3-api-2 Created + Container lab4-check-753ba80cb3-lb-1 Creating + Container lab4-check-753ba80cb3-cleanup-1 Created + Container lab4-check-753ba80cb3-lb-1 Created + Container lab4-check-753ba80cb3-s3-storage-1 Starting + Container lab4-check-753ba80cb3-valkey-1 Starting + Container lab4-check-753ba80cb3-postgres-1 Starting + Container lab4-check-753ba80cb3-s3-storage-1 Started + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Started + Container lab4-check-753ba80cb3-postgres-1 Started + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Starting + Container lab4-check-753ba80cb3-s3-1 Started + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-2 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-cleanup-1 Starting + Container lab4-check-753ba80cb3-cleanup-1 Started + Container lab4-check-753ba80cb3-api-2 Started + Container lab4-check-753ba80cb3-api-1 Starting + Container lab4-check-753ba80cb3-api-1 Started + Container lab4-check-753ba80cb3-api-2 Waiting + Container lab4-check-753ba80cb3-api-1 Waiting + Container lab4-check-753ba80cb3-api-2 Healthy + Container lab4-check-753ba80cb3-api-1 Healthy + Container lab4-check-753ba80cb3-lb-1 Starting + Container lab4-check-753ba80cb3-lb-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +PASS: 2 real API containers, CPU/RAM limits, API non-root and no public ports; only LB published +Проверяем сервер: http://127.0.0.1:65032 +/Users/vanek/Desktop/VNPlabs/VNPlab4/mtusi2026lab4/.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +/Users/vanek/Desktop/VNPlabs/VNPlab4/mtusi2026lab4/.venv/bin/python tests/smoke.py --base-url "http://127.0.0.1:65032" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. +test_chat_access (test_api.ApiTests.test_chat_access) ... ok +test_chat_pagination_and_cursor_owner (test_api.ApiTests.test_chat_pagination_and_cursor_owner) ... ok +test_cursor_cannot_be_used_for_another_chat_or_changed (test_api.ApiTests.test_cursor_cannot_be_used_for_another_chat_or_changed) ... ok +test_default_page_size (test_api.ApiTests.test_default_page_size) ... ok +test_duplicate_username (test_api.ApiTests.test_duplicate_username) ... ok +test_framework_errors_and_request_ids (test_api.ApiTests.test_framework_errors_and_request_ids) ... ok +test_identification_and_authentication_order (test_api.ApiTests.test_identification_and_authentication_order) ... ok +test_invalid_chat_members (test_api.ApiTests.test_invalid_chat_members) ... ok +test_invalid_chat_title (test_api.ApiTests.test_invalid_chat_title) ... ok +test_invalid_limits_and_cursors (test_api.ApiTests.test_invalid_limits_and_cursors) ... ok +test_invalid_message_fields (test_api.ApiTests.test_invalid_message_fields) ... ok +test_invalid_user_fields (test_api.ApiTests.test_invalid_user_fields) ... ok +test_malformed_json_and_encoding (test_api.ApiTests.test_malformed_json_and_encoding) ... ok +test_message_pagination_and_changing_limit (test_api.ApiTests.test_message_pagination_and_changing_limit) ... ok +test_unicode_and_text_preservation (test_api.ApiTests.test_unicode_and_text_preservation) ... ok +test_wrong_content_type (test_api.ApiTests.test_wrong_content_type) ... ok +test_access_and_authentication (test_attachments.AttachmentTests.test_access_and_authentication) ... ok +test_attachment_only_message_and_other_member (test_attachments.AttachmentTests.test_attachment_only_message_and_other_member) ... ok +test_chunked_limit_without_content_length (test_attachments.AttachmentTests.test_chunked_limit_without_content_length) ... ok +test_content_length_limit_before_body (test_attachments.AttachmentTests.test_content_length_limit_before_body) ... ok +test_corruption_and_mime_spoof (test_attachments.AttachmentTests.test_corruption_and_mime_spoof) ... ok +test_filename_rejected_before_storage (test_attachments.AttachmentTests.test_filename_rejected_before_storage) ... ok +test_foreign_and_unknown_attachment_ids (test_attachments.AttachmentTests.test_foreign_and_unknown_attachment_ids) ... ok +test_idempotency_includes_ordered_attachment_ids (test_attachments.AttachmentTests.test_idempotency_includes_ordered_attachment_ids) ... ok +test_invalid_attachment_lists (test_attachments.AttachmentTests.test_invalid_attachment_lists) ... ok +test_pixel_limit_before_decode (test_attachments.AttachmentTests.test_pixel_limit_before_decode) ... ok +test_png_and_jpeg_originals (test_attachments.AttachmentTests.test_png_and_jpeg_originals) ... ok +test_simultaneous_registration_of_one_username (test_concurrency.ConcurrencyTests.test_simultaneous_registration_of_one_username) ... ok +test_ten_clients_send_one_hundred_messages (test_concurrency.ConcurrencyTests.test_ten_clients_send_one_hundred_messages) ... ok +test_conflict_and_semantic_json_replay (test_idempotency.IdempotencyTests.test_conflict_and_semantic_json_replay) ... ok +test_key_format_and_missing_key (test_idempotency.IdempotencyTests.test_key_format_and_missing_key) ... ok +test_parallel_replay_across_replicas (test_idempotency.IdempotencyTests.test_parallel_replay_across_replicas) ... ok +test_replay_rechecks_session_and_access (test_idempotency.IdempotencyTests.test_replay_rechecks_session_and_access) ... ok +test_scope_is_user_and_chat (test_idempotency.IdempotencyTests.test_scope_is_user_and_chat) ... ok +test_logout_revokes_only_one_session (test_sessions.SessionTests.test_logout_revokes_only_one_session) ... ok +test_password_validation (test_sessions.SessionTests.test_password_validation) ... ok +test_sql_like_message_is_plain_text (test_sessions.SessionTests.test_sql_like_message_is_plain_text) ... ok +test_unicode_password_with_colon_and_private_response (test_sessions.SessionTests.test_unicode_password_with_colon_and_private_response) ... ok +test_wrong_password_and_basic_only_for_login (test_sessions.SessionTests.test_wrong_password_and_basic_only_for_login) ... ok + +---------------------------------------------------------------------- +Ran 39 tests in 9.473s + +OK +PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages. + Container lab4-check-753ba80cb3-postgres-1 Running + Container lab4-check-753ba80cb3-postgres-1 Running +PASS: migrations only in one-shot service; repeated execution preserves data + Container lab4-check-753ba80cb3-cleanup-1 Stopping + Container lab4-check-753ba80cb3-cleanup-1 Stopped + Container lab4-check-753ba80cb3-s3-storage-1 Running + Container lab4-check-753ba80cb3-s3-1 Running + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-storage-1 Running + Container lab4-check-753ba80cb3-s3-1 Running + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy +PASS: private S3, limited credentials, isolated administration, repeated bootstrap +Stopped API: 8f41208a6406 container=8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158 +API ready through LB: 887ffd1465a8 + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-2 Starting + Container lab4-check-753ba80cb3-api-2 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +PASS: original downloaded through another replica after stopping uploader +PASS: post-PUT DB failure recorded; cleanup protects fresh, active and ready uploads + Container lab4-check-753ba80cb3-s3-1 Stopping + Container lab4-check-753ba80cb3-s3-1 Stopped + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Starting + Container lab4-check-753ba80cb3-s3-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +PASS: S3 outage gives API 503; recovery preserves original and session + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-cleanup-1 Starting + Container lab4-check-753ba80cb3-cleanup-1 Started +PASS: 10 concurrent real PNG uploads; all checksums match; memory peaks: [203698176, 90324992] + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited +/Users/vanek/Desktop/VNPlabs/VNPlab4/mtusi2026lab4/.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:65032" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +PASS: both API, LB, PostgreSQL, Valkey, S3 and cleaner actually restarted; data and session preserved +PASS: SIGTERM with unfinished request: {"instance": "887ffd1465a8", "unfinished_request": true, "shutdown_seconds": 5.448, "new_connections_refused_seconds": 0.162, "exit_code": 0, "oom_killed": false} + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-1 Starting + Container lab4-check-753ba80cb3-api-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 + Container lab4-check-753ba80cb3-valkey-1 Stopping + Container lab4-check-753ba80cb3-valkey-1 Stopped +PASS: valkey outage: API live=200, ready=503, LB ready=503 + Container lab4-check-753ba80cb3-valkey-1 Starting + Container lab4-check-753ba80cb3-valkey-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 + Container lab4-check-753ba80cb3-postgres-1 Stopping + Container lab4-check-753ba80cb3-postgres-1 Stopped +PASS: postgres outage: API live=200, ready=503, LB ready=503 + Container lab4-check-753ba80cb3-postgres-1 Starting + Container lab4-check-753ba80cb3-postgres-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 + Container lab4-check-753ba80cb3-s3-1 Stopping + Container lab4-check-753ba80cb3-s3-1 Stopped +PASS: s3 outage: API live=200, ready=503, LB ready=503 + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Starting + Container lab4-check-753ba80cb3-s3-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +/Users/vanek/Desktop/VNPlabs/VNPlab4/mtusi2026lab4/.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:65032" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 111, "failed_attempts": 1, "first_recovery_seconds": 2.469} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-2 Starting + Container lab4-check-753ba80cb3-api-2 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +Stopped API: 887ffd1465a8 container=887ffd1465a8191971a9f04cb3deb79994889e288fbe4bc7224052fa3094a710 +{"label": "stop 887ffd1465a8", "duration_seconds": 16.012, "successful_requests": 1480, "successful_writes": 739, "errors": 4, "successful_rps": 92.43, "first_success_after_action_seconds": {"POST": 0.006, "GET": 0.008}, "maximum_success_gap_seconds": {"POST": 2.042, "GET": 1.984}, "action_start_seconds": 1.001, "action_end_seconds": 1.708, "p50_ms": 9.065, "p95_ms": 18.196, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["887ffd1465a8", "8f41208a6406"]} + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-1 Starting + Container lab4-check-753ba80cb3-api-1 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +Stopped API: 8f41208a6406 container=8f41208a64062523e85a27c5d759029d6c9940df78417691c85900dcb8e6f158 +{"label": "stop 8f41208a6406", "duration_seconds": 16.026, "successful_requests": 1472, "successful_writes": 735, "errors": 4, "successful_rps": 91.85, "first_success_after_action_seconds": {"POST": 0.029, "GET": 0.038}, "maximum_success_gap_seconds": {"POST": 1.986, "GET": 1.99}, "action_start_seconds": 1.011, "action_end_seconds": 1.806, "p50_ms": 9.112, "p95_ms": 20.767, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["887ffd1465a8", "8f41208a6406"]} + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Waiting + Container lab4-check-753ba80cb3-s3-storage-1 Healthy + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Starting + Container lab4-check-753ba80cb3-migrate-1 Started + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Starting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Started + Container lab4-check-753ba80cb3-s3-1 Waiting + Container lab4-check-753ba80cb3-s3-bootstrap-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Waiting + Container lab4-check-753ba80cb3-valkey-1 Waiting + Container lab4-check-753ba80cb3-migrate-1 Waiting + Container lab4-check-753ba80cb3-postgres-1 Healthy + Container lab4-check-753ba80cb3-migrate-1 Exited + Container lab4-check-753ba80cb3-valkey-1 Healthy + Container lab4-check-753ba80cb3-s3-1 Healthy + Container lab4-check-753ba80cb3-s3-bootstrap-1 Exited + Container lab4-check-753ba80cb3-api-2 Starting + Container lab4-check-753ba80cb3-api-2 Started +API ready through LB: 887ffd1465a8, 8f41208a6406 +Local .env and secrets are ready; values are not printed +#1 [internal] load local bake definitions +#1 reading from stdin 564B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 733B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 167B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 1.01kB done +#5 DONE 0.0s + +#6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done +#6 DONE 0.0s + +#7 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#7 CACHED + +#8 [7/8] COPY migrations/ ./migrations/ +#8 CACHED + +#9 [3/8] COPY requirements.txt . +#9 CACHED + +#10 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#10 CACHED + +#11 [6/8] COPY src/ ./src/ +#11 CACHED + +#12 [2/8] WORKDIR /app +#12 CACHED + +#13 [8/8] COPY research/ ./research/ +#13 CACHED + +#14 exporting to image +#14 exporting layers done +#14 exporting manifest sha256:1c6cb19983cdfb308e1ec43f59338922d2fec797ff8fdc9636526f9de20a0693 done +#14 exporting config sha256:bc7c1361d0a40f2beb749094a98a091632f75a255c5841fcdf80a33b166720eb done +#14 exporting attestation manifest sha256:d2cddc9cbcfc350b5ccf03a93a9a2ea63dc1b623c099006f4187f3782a500abc 0.0s done +#14 exporting manifest list sha256:413691a40751a490bab701b9498431365ae10bfd081d55decba718e5e90a0f5d done +#14 naming to docker.io/library/mtusi-messenger:lab4 done +#14 unpacking to docker.io/library/mtusi-messenger:lab4 done +#14 DONE 0.1s + +#15 resolving provenance for metadata file +#15 DONE 0.0s + mtusi-messenger:lab4 Built +Local .env and secrets are ready; values are not printed + Container lab4-check-753ba80cb3-postgres-1 Running +Migrations: already applied +API ready through LB: 887ffd1465a8, 8f41208a6406 +Replacing API: 887ffd1465a8 + Container lab4-check-753ba80cb3-api-2 Running + Container lab4-check-753ba80cb3-api-3 Creating + Container lab4-check-753ba80cb3-api-3 Created + Container lab4-check-753ba80cb3-api-3 Starting + Container lab4-check-753ba80cb3-api-3 Started +API ready through LB: 8f41208a6406, 9be50a7967ed +Replacing API: 8f41208a6406 + Container lab4-check-753ba80cb3-api-3 Running + Container lab4-check-753ba80cb3-api-4 Creating + Container lab4-check-753ba80cb3-api-4 Created + Container lab4-check-753ba80cb3-api-4 Starting + Container lab4-check-753ba80cb3-api-4 Started +API ready through LB: 4479505e52f5, 9be50a7967ed +PASS: all API containers replaced sequentially; stores and LB kept running +{"label": "sequential API replacement", "duration_seconds": 22.153, "successful_requests": 1960, "successful_writes": 979, "errors": 8, "successful_rps": 88.47, "first_success_after_action_seconds": {"POST": 0.035, "GET": 0.041}, "maximum_success_gap_seconds": {"POST": 1.048, "GET": 1.031}, "action_start_seconds": 1.004, "action_end_seconds": 19.149, "p50_ms": 8.859, "p95_ms": 17.468, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["4479505e52f5", "887ffd1465a8", "8f41208a6406", "9be50a7967ed"]} +PASS: all lab4 grade-4 checks completed + Container lab4-check-753ba80cb3-lb-1 Stopping + Container lab4-check-753ba80cb3-cleanup-1 Stopping + Container lab4-check-753ba80cb3-lb-1 Stopped + Container lab4-check-753ba80cb3-lb-1 Removing + Container lab4-check-753ba80cb3-lb-1 Removed + Container lab4-check-753ba80cb3-api-3 Stopping + Container lab4-check-753ba80cb3-api-4 Stopping + Container lab4-check-753ba80cb3-api-4 Stopped + Container lab4-check-753ba80cb3-api-4 Removing + Container lab4-check-753ba80cb3-api-4 Removed + Container lab4-check-753ba80cb3-api-3 Stopped + Container lab4-check-753ba80cb3-api-3 Removing + Container lab4-check-753ba80cb3-api-3 Removed + Container lab4-check-753ba80cb3-valkey-1 Stopping + Container lab4-check-753ba80cb3-valkey-1 Stopped + Container lab4-check-753ba80cb3-valkey-1 Removing + Container lab4-check-753ba80cb3-valkey-1 Removed + Container lab4-check-753ba80cb3-cleanup-1 Stopped + Container lab4-check-753ba80cb3-cleanup-1 Removing + Container lab4-check-753ba80cb3-cleanup-1 Removed + Container lab4-check-753ba80cb3-s3-bootstrap-1 Stopping + Container lab4-check-753ba80cb3-migrate-1 Stopping + Container lab4-check-753ba80cb3-s3-bootstrap-1 Stopped + Container lab4-check-753ba80cb3-migrate-1 Stopped + Container lab4-check-753ba80cb3-migrate-1 Removing + Container lab4-check-753ba80cb3-s3-bootstrap-1 Removing + Container lab4-check-753ba80cb3-migrate-1 Removed + Container lab4-check-753ba80cb3-postgres-1 Stopping + Container lab4-check-753ba80cb3-s3-bootstrap-1 Removed + Container lab4-check-753ba80cb3-s3-1 Stopping + Container lab4-check-753ba80cb3-postgres-1 Stopped + Container lab4-check-753ba80cb3-postgres-1 Removing + Container lab4-check-753ba80cb3-postgres-1 Removed + Container lab4-check-753ba80cb3-s3-1 Stopped + Container lab4-check-753ba80cb3-s3-1 Removing + Container lab4-check-753ba80cb3-s3-1 Removed + Container lab4-check-753ba80cb3-s3-storage-1 Stopping + Container lab4-check-753ba80cb3-s3-storage-1 Stopped + Container lab4-check-753ba80cb3-s3-storage-1 Removing + Container lab4-check-753ba80cb3-s3-storage-1 Removed + Network lab4-check-753ba80cb3_s3-internal Removing + Volume lab4-check-753ba80cb3_postgres_data Removing + Volume lab4-check-753ba80cb3_s3_data Removing + Network lab4-check-753ba80cb3_default Removing + Volume lab4-check-753ba80cb3_valkey_data Removing + Volume lab4-check-753ba80cb3_s3_data Removed + Volume lab4-check-753ba80cb3_postgres_data Removed + Volume lab4-check-753ba80cb3_valkey_data Removed + Network lab4-check-753ba80cb3_s3-internal Removed + Network lab4-check-753ba80cb3_default Removed +Removed only temporary test containers and volumes: lab4-check-753ba80cb3 diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg index 72fffda..c39b8d5 100644 --- a/infra/haproxy.cfg +++ b/infra/haproxy.cfg @@ -8,8 +8,8 @@ defaults option dontlognull option http-server-close timeout connect 1s - timeout client 10s - timeout server 8s + timeout client 40s + timeout server 60s timeout http-request 5s timeout check 1s retries 1 diff --git a/migrations/003_attachments.sql b/migrations/003_attachments.sql new file mode 100644 index 0000000..5a9db48 --- /dev/null +++ b/migrations/003_attachments.sql @@ -0,0 +1,34 @@ +-- Только добавления к lab3; старые сообщения и ключи сохраняются. +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL, + object_key text NOT NULL UNIQUE, + filename varchar(255) NOT NULL, + content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 char(64) NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id) +); +CREATE INDEX attachments_chat_idx ON attachments(chat_id); + +ALTER TABLE messages DROP CONSTRAINT messages_text_check; +ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) <= 4000); +CREATE TABLE message_attachments ( + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id uuid NOT NULL REFERENCES attachments(id), + position integer NOT NULL CHECK (position BETWEEN 0 AND 9), + PRIMARY KEY (message_id, attachment_id), + UNIQUE (message_id, position) +); +ALTER TABLE message_idempotency + ADD COLUMN request_attachment_ids uuid[] NOT NULL DEFAULT '{}'; + +-- Фиксируется ДО S3 PUT. После COMMIT ready-метаданных удаляется атомарно с ними. +CREATE TABLE attachment_uploads ( + id uuid PRIMARY KEY, + object_key text NOT NULL UNIQUE, + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE INDEX attachment_uploads_created_idx ON attachment_uploads(created_at); diff --git a/requirements.txt b/requirements.txt index 82eccad..2bb42d3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,3 +27,10 @@ psycopg-pool==3.3.3 argon2-cffi-bindings==26.1.0 cffi==2.1.1 pycparser==3.11 + +# S3 и проверка настоящих PNG/JPEG (без изменения оригинала) +minio==7.2.20 +Pillow==13.0.0 +certifi==2026.10.23 +pycryptodome==4.3.0 +urllib3==2.8.0 diff --git a/scripts/check-docker.sh b/scripts/check-docker.sh index 438b6a3..435b9f8 100755 --- a/scripts/check-docker.sh +++ b/scripts/check-docker.sh @@ -8,4 +8,4 @@ if [[ ! -x .venv/bin/python ]]; then fi ./scripts/build.sh mkdir -p evidence -.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | sed -E 's/[[:blank:]]+$//' | tee evidence/lab3-checks.txt +.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | sed -E 's/[[:blank:]]+$//' | tee evidence/lab4-checks.txt diff --git a/scripts/init_local.py b/scripts/init_local.py index 230c15e..f970193 100644 --- a/scripts/init_local.py +++ b/scripts/init_local.py @@ -1,5 +1,8 @@ """Создаёт локальное окружение и secrets; существующие значения сохраняет.""" from pathlib import Path +import json +import os +import re import secrets import shutil from urllib.parse import quote @@ -18,12 +21,37 @@ password = password_file.read_text().strip() values = { "database_url": f"postgresql://messenger:{quote(password, safe='')}@postgres:5432/messenger", "cache_url": "redis://valkey:6379/0", + "s3_root_user": "root-" + secrets.token_hex(8), + "s3_root_password": secrets.token_urlsafe(32), + "s3_access_key_id": "api-" + secrets.token_hex(8), + "s3_secret_access_key": secrets.token_urlsafe(32), } for name, value in values.items(): path = directory / name if not path.exists(): path.write_text(value) +dotenv = dict(line.split("=", 1) for line in (root / ".env").read_text().splitlines() + if line.strip() and not line.lstrip().startswith("#") and "=" in line) +bucket = os.environ.get("S3_BUCKET", dotenv.get("S3_BUCKET", "messenger-attachments")) +if re.fullmatch(r"[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]", bucket) is None: + raise ValueError("Недопустимое имя S3 bucket") +def local_secret(name): + return (directory / name).read_text().strip() +config = {"accounts": [{"id": "messenger-api", "displayName": "messenger-api"}], "identities": [ + {"name": "bootstrap", "credentials": [{"accessKey": local_secret("s3_root_user"), + "secretKey": local_secret("s3_root_password")}], "actions": ["Admin"]}, + {"name": "messenger-api", "account": {"id": "messenger-api", "displayName": "messenger-api"}, + "credentials": [{"accessKey": local_secret("s3_access_key_id"), "secretKey": local_secret("s3_secret_access_key")}], + "actions": ["Read:" + bucket, "List:" + bucket, "Write:" + bucket + "/attachments/*"]}, +]} +config_file = directory / "s3_config" +encoded_config = json.dumps(config) +if not config_file.exists() or config_file.read_text() != encoded_config: + temporary = directory / "s3_config.tmp" + temporary.write_text(encoded_config) + temporary.chmod(0o444) + temporary.replace(config_file) # Каталог 0700 защищает файлы на хосте; 0444 позволяет USER 10001 читать bind-mounted secret. -for name in ("postgres_password", "database_url", "cache_url"): +for name in ("postgres_password", "s3_config", *values): (directory / name).chmod(0o444) print("Local .env and secrets are ready; values are not printed") diff --git a/scripts/migrate.sh b/scripts/migrate.sh index a1874ea..9f0bb21 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -2,5 +2,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" -python3 scripts/init_local.py -docker compose run --rm --build migrate +./scripts/build.sh +docker compose run --rm migrate diff --git a/scripts/restart.sh b/scripts/restart.sh index 5375d64..5b9c85b 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -2,5 +2,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" -docker compose restart --timeout 8 postgres valkey api lb +docker compose restart --timeout 8 postgres valkey s3-storage s3 api lb cleanup ./scripts/wait-ready.sh diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh index 7693a96..b5b225f 100755 --- a/scripts/rolling-update.sh +++ b/scripts/rolling-update.sh @@ -3,6 +3,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" # Сначала собираем образ и применяем только обратно совместимые миграции. -./scripts/build.sh ./scripts/migrate.sh exec python3 scripts/replicas.py rolling diff --git a/scripts/start.sh b/scripts/start.sh index a4133c6..653502b 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -3,5 +3,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" python3 scripts/init_local.py -docker compose up -d --build lb +docker compose up -d --build lb cleanup ./scripts/wait-ready.sh diff --git a/src/app.py b/src/app.py index 3155c8e..5c82517 100644 --- a/src/app.py +++ b/src/app.py @@ -6,13 +6,15 @@ import logging import re from time import perf_counter from uuid import UUID, uuid4 +from urllib.parse import quote from fastapi import FastAPI, Request, Response +from fastapi.responses import StreamingResponse from starlette.concurrency import run_in_threadpool from fastapi.exceptions import RequestValidationError from starlette.exceptions import HTTPException -from src import auth, db, migrate, passwords, storage +from src import attachments, auth, db, migrate, object_store, passwords, storage from src.errors import ApiError, error_response from src.models import CreateChat, CreateMessage, CreateUser from src.config import INSTANCE_ID @@ -40,13 +42,14 @@ async def handle_request(request: Request, call_next): try: is_registration = request.method == "POST" and path == "/api/v1/users" is_login = request.method == "POST" and path == "/api/v1/auth/sessions" + is_upload = request.method == "POST" and re.fullmatch(r"/api/v1/chats/[^/]+/attachments", path) if is_login: request.state.user = await run_in_threadpool(auth.basic_user, request.headers.get("Authorization")) if await request.body(): raise ApiError(400, "invalid_request", "Вход через Basic не принимает тело запроса") elif path.startswith("/api/v1/") and not is_registration: request.state.user = await run_in_threadpool(auth.current_user, request.headers.get("Authorization")) - if request.method == "POST" and path.startswith("/api/v1/") and not is_login: + if request.method == "POST" and path.startswith("/api/v1/") and not is_login and not is_upload: content_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower() if content_type != "application/json": raise ApiError(415, "unsupported_media_type", "Ожидается application/json") @@ -101,6 +104,7 @@ def health_live(): def health_ready(): db.ready() auth.ready() + object_store.ready() return {"status": "ok"} @@ -135,7 +139,29 @@ def create_message(request: Request, chat_id: UUID, data: CreateMessage): keys = request.headers.getlist("Idempotency-Key") if len(keys) != 1 or re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", keys[0]) is None: raise ApiError(400, "invalid_request", "Нужен Idempotency-Key: 1–128 символов ASCII [A-Za-z0-9._:-]") - return storage.create_message(request.state.user["id"], str(chat_id), data.text, keys[0]) + return storage.create_message(request.state.user["id"], str(chat_id), data.text, keys[0], data.attachment_ids) + + +@app.post("/api/v1/chats/{chat_id}/attachments", status_code=201) +async def upload_attachment(request: Request, chat_id: UUID, filename: str): + return await attachments.upload(request, str(chat_id), filename) + + +@app.get("/api/v1/attachments/{attachment_id}") +def get_attachment(request: Request, attachment_id: UUID): + return attachments.public_attachment(attachments.find(request.state.user["id"], str(attachment_id))) + + +@app.get("/api/v1/attachments/{attachment_id}/content") +def download_attachment(request: Request, attachment_id: UUID, variant: str = "original"): + row = attachments.find(request.state.user["id"], str(attachment_id)) + if variant != "original": + raise ApiError(404, "not_found", "Вариант вложения не найден") + response = object_store.get(row["object_key"]) + return StreamingResponse(attachments.chunks(response), media_type=row["content_type"], headers={ + "Content-Length": str(row["size_bytes"]), "X-Content-Type-Options": "nosniff", + "Content-Disposition": "attachment; filename*=UTF-8''" + quote(row["filename"], safe=""), + }) @app.get("/api/v1/chats/{chat_id}/messages") diff --git a/src/attachments.py b/src/attachments.py new file mode 100644 index 0000000..d025efa --- /dev/null +++ b/src/attachments.py @@ -0,0 +1,147 @@ +"""Синхронная загрузка: ограниченный поток -> проверка -> журнал -> S3 -> COMMIT.""" +import asyncio +import hashlib +import os +from tempfile import TemporaryFile +import unicodedata +from uuid import UUID, uuid4 +import warnings + +from PIL import Image, UnidentifiedImageError +from starlette.concurrency import run_in_threadpool +from starlette.requests import ClientDisconnect + +from src import db, object_store, storage +from src.errors import ApiError + + +MAX_BYTES = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760")) +MAX_PIXELS = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000")) +if not 1 <= MAX_BYTES <= 10485760 or not 1 <= MAX_PIXELS <= 40000000: + raise ValueError("Лимиты загрузки должны быть положительными и не превышать контракт") +Image.MAX_IMAGE_PIXELS = MAX_PIXELS +warnings.filterwarnings("error", category=Image.DecompressionBombWarning) +# Максимум одно полное декодирование на реплику, независимо от числа клиентов. +upload_slot = asyncio.Semaphore(1) + + +def public_attachment(row): + return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "owner_id": str(row["owner_id"]), + "filename": row["filename"], "content_type": row["content_type"], + "size_bytes": row["size_bytes"], "sha256": row["sha256"], "status": "ready", + "variants": [], "error_code": None, "created_at": storage.utc(row["created_at"]), + "updated_at": storage.utc(row["created_at"])} + + +def validate_filename(filename): + if not 1 <= len(filename) <= 255 or not filename.strip() or any( + char in "/\\" or unicodedata.category(char) == "Cc" for char in filename + ): + raise ApiError(400, "invalid_request", "Недопустимое имя файла") + try: + filename.encode("utf-8") + except UnicodeError: + raise ApiError(400, "invalid_request", "Недопустимое имя файла") from None + + +def validate_image(file, declared_type): + try: + file.seek(0) + with Image.open(file, formats=["PNG", "JPEG"]) as image: + true_type = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format] + if image.width * image.height > MAX_PIXELS: + raise ApiError(413, "payload_too_large", "Слишком много пикселей") + if true_type != declared_type: + raise ApiError(415, "unsupported_media_type", "MIME не совпадает с содержимым") + image.verify() + # verify проверяет структуру/CRC, load дополнительно обнаруживает обрыв pixel data. + file.seek(0) + with Image.open(file, formats=["PNG", "JPEG"]) as image: + image.load() + return true_type + except (Image.DecompressionBombError, Image.DecompressionBombWarning): + raise ApiError(413, "payload_too_large", "Слишком много пикселей") from None + except (UnidentifiedImageError, OSError, ValueError, SyntaxError): + raise ApiError(415, "unsupported_media_type", "Повреждённый PNG/JPEG или другой формат") from None + + +def lock_upload(conn, key, blocking=True): + function = "pg_advisory_xact_lock" if blocking else "pg_try_advisory_xact_lock" + return conn.execute(f"SELECT {function}(hashtextextended(%s, 4)) AS locked", (key,)).fetchone()["locked"] + + +def save(user_id, chat_id, filename, content_type, file, size, sha256): + attachment_id = uuid4() + key = "attachments/" + attachment_id.hex + # Даже при kill -9 после PUT остаётся долговечное намерение для cleaner. + with db.connection() as conn: + storage.require_member(conn, user_id, chat_id) + conn.execute("INSERT INTO attachment_uploads(id, object_key) VALUES (%s, %s)", (attachment_id, key)) + with db.connection() as conn: + conn.execute("SET LOCAL idle_in_transaction_session_timeout='30s'") + lock_upload(conn, key) + if not conn.execute("SELECT 1 FROM attachment_uploads WHERE id=%s", (attachment_id,)).fetchone(): + raise ApiError(503, "unavailable", "Загрузка больше не активна") + object_store.put(key, file, size, content_type) + row = conn.execute("""INSERT INTO attachments(id, chat_id, owner_id, object_key, filename, + content_type, size_bytes, sha256) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING *""", + (attachment_id, UUID(chat_id), UUID(user_id), key, filename, content_type, size, sha256)).fetchone() + conn.execute("DELETE FROM attachment_uploads WHERE id=%s", (attachment_id,)) + result = public_attachment(row) + # Ответ только после успешного COMMIT; при неопределённом исходе ничего не удаляем. + return result + + +async def upload(request, chat_id, filename): + user_id = request.state.user["id"] + await run_in_threadpool(storage.get_chat, user_id, chat_id) + validate_filename(filename) + content_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower() + if content_type not in {"image/png", "image/jpeg"}: + raise ApiError(415, "unsupported_media_type", "Ожидаются сырые bytes PNG/JPEG") + length = request.headers.get("Content-Length") + if length is not None: + if not length.isascii() or not length.isdecimal(): + raise ApiError(400, "invalid_request", "Неверный Content-Length") + if len(length) > 10 or int(length) > MAX_BYTES: + raise ApiError(413, "payload_too_large", "Файл больше допустимого размера") + try: + await asyncio.wait_for(upload_slot.acquire(), timeout=20) + except TimeoutError: + raise ApiError(503, "unavailable", "Загрузки заняты; повторите позднее") from None + try: + with TemporaryFile() as file: + size = 0 + digest = hashlib.sha256() + try: + async with asyncio.timeout(30): + async for chunk in request.stream(): + size += len(chunk) + if size > MAX_BYTES: + raise ApiError(413, "payload_too_large", "Файл больше допустимого размера") + digest.update(chunk) + await run_in_threadpool(file.write, chunk) + except (TimeoutError, ClientDisconnect): + raise ApiError(400, "invalid_request", "Загрузка прервана или истёк таймаут") from None + true_type = await run_in_threadpool(validate_image, file, content_type) + return await run_in_threadpool(save, user_id, chat_id, filename, true_type, file, size, digest.hexdigest()) + finally: + upload_slot.release() + + +def find(user_id, attachment_id): + with db.connection() as conn: + row = conn.execute("""SELECT a.* FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id + WHERE a.id=%s AND m.user_id=%s""", (UUID(attachment_id), UUID(user_id))).fetchone() + if row is None: + raise ApiError(404, "not_found", "Вложение не найдено") + return row + + +def chunks(response): + try: + while chunk := response.read(65536): + yield chunk + finally: + response.close() + response.release_conn() diff --git a/src/bootstrap_s3.py b/src/bootstrap_s3.py new file mode 100644 index 0000000..254c6fe --- /dev/null +++ b/src/bootstrap_s3.py @@ -0,0 +1,38 @@ +"""Идемпотентное создание приватного bucket отдельной административной учёткой.""" +import os +import time +from urllib.parse import urlsplit + +from minio import Minio +from minio.error import MinioException, S3Error +import urllib3 + +from src.config import secret + + +def bootstrap(): + endpoint = urlsplit(os.environ.get("S3_ENDPOINT", "http://s3:9000")) + client = Minio(endpoint.netloc, access_key=secret("S3_ROOT_USER"), secret_key=secret("S3_ROOT_PASSWORD"), + secure=endpoint.scheme == "https", region=os.environ.get("S3_REGION", "us-east-1"), + http_client=urllib3.PoolManager(timeout=urllib3.Timeout(connect=1, read=3), retries=False)) + bucket = os.environ.get("S3_BUCKET", "messenger-attachments") + deadline = time.monotonic() + 60 + while True: + try: + if not client.bucket_exists(bucket): + client.make_bucket(bucket) + print("Private bucket is ready; static IAM permits only authenticated accounts", flush=True) + return + except S3Error as error: + if error.code in {"BucketAlreadyOwnedByYou", "BucketAlreadyExists"}: + return + if time.monotonic() >= deadline: + raise RuntimeError("S3 bootstrap failed") from None + except (MinioException, urllib3.exceptions.HTTPError, OSError): + if time.monotonic() >= deadline: + raise RuntimeError("S3 bootstrap timed out") from None + time.sleep(1) + + +if __name__ == "__main__": + bootstrap() diff --git a/src/cleanup.py b/src/cleanup.py new file mode 100644 index 0000000..482e30f --- /dev/null +++ b/src/cleanup.py @@ -0,0 +1,60 @@ +"""Уровень 4: очистка только журнала незавершённых PUT, без полного обхода S3.""" +import argparse +import json +import logging +import os +import time + +from src import db, object_store +from src.attachments import lock_upload + + +GRACE_SECONDS = int(os.environ.get("UPLOAD_CLEANUP_GRACE_SECONDS", "3600")) +if GRACE_SECONDS < 60: + raise ValueError("Безопасный grace period должен быть не меньше 60 секунд") + + +def cleanup_once(): + result = {"removed": 0, "kept_ready": 0, "skipped_active": 0} + with db.connection() as conn: + candidates = conn.execute("""SELECT id, object_key FROM attachment_uploads + WHERE created_at < clock_timestamp() - %s * interval '1 second' + ORDER BY created_at LIMIT 100""", (GRACE_SECONDS,)).fetchall() + for row in candidates: + with db.connection() as conn: + if not lock_upload(conn, row["object_key"], blocking=False): + result["skipped_active"] += 1 + continue + pending = conn.execute("""SELECT 1 FROM attachment_uploads WHERE id=%s + AND created_at < clock_timestamp() - %s * interval '1 second' FOR UPDATE""", + (row["id"], GRACE_SECONDS)).fetchone() + if not pending: + continue + ready = conn.execute("SELECT 1 FROM attachments WHERE object_key=%s", (row["object_key"],)).fetchone() + if ready: + result["kept_ready"] += 1 + else: + object_store.remove(row["object_key"]) + result["removed"] += 1 + conn.execute("DELETE FROM attachment_uploads WHERE id=%s", (row["id"],)) + return result + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--once", action="store_true") + args = parser.parse_args() + db.pool.open(wait=True, timeout=15) + try: + while True: + try: + print(json.dumps(cleanup_once()), flush=True) + except Exception as error: + if args.once: + raise + logging.error("Cleanup postponed: %s", type(error).__name__) + if args.once: + break + time.sleep(30) + finally: + db.pool.close() diff --git a/src/models.py b/src/models.py index e8f6be4..ee5360b 100644 --- a/src/models.py +++ b/src/models.py @@ -2,7 +2,7 @@ from uuid import UUID -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator, model_validator class RequestModel(BaseModel): @@ -10,9 +10,9 @@ class RequestModel(BaseModel): @field_validator("*") @classmethod - def check_strings(cls, value): + def check_strings(cls, value, info: ValidationInfo): if isinstance(value, str): - if not value.strip(): + if info.field_name != "text" and not value.strip(): raise ValueError("Строка не должна состоять из пробелов") value.encode("utf-8") return value @@ -38,4 +38,19 @@ class CreateChat(RequestModel): class CreateMessage(RequestModel): - text: str = Field(min_length=1, max_length=4000) + text: str = Field(max_length=4000) + attachment_ids: list[str] = Field(default_factory=list, max_length=10) + + @field_validator("attachment_ids") + @classmethod + def check_attachments(cls, values): + ids = [str(UUID(value)) for value in values] + if len(ids) != len(set(ids)): + raise ValueError("Вложения не должны повторяться") + return ids + + @model_validator(mode="after") + def check_content(self): + if not self.text.strip() and not self.attachment_ids: + raise ValueError("Нужен текст или хотя бы одно вложение") + return self diff --git a/src/object_store.py b/src/object_store.py new file mode 100644 index 0000000..3cb4b8c --- /dev/null +++ b/src/object_store.py @@ -0,0 +1,57 @@ +"""Один приватный bucket; короткие таймауты и отсутствие прозрачных повторов PUT.""" +from contextlib import contextmanager +import os +from urllib.parse import urlsplit + +from minio import Minio +from minio.error import MinioException +import urllib3 + +from src.config import secret +from src.errors import ApiError + + +BUCKET = os.environ.get("S3_BUCKET", "messenger-attachments") +endpoint = urlsplit(os.environ.get("S3_ENDPOINT", "http://s3:9000")) +client = Minio( + endpoint.netloc, + access_key=secret("S3_ACCESS_KEY_ID"), + secret_key=secret("S3_SECRET_ACCESS_KEY"), + secure=endpoint.scheme == "https", + region=os.environ.get("S3_REGION", "us-east-1"), + http_client=urllib3.PoolManager( + timeout=urllib3.Timeout(connect=1, read=3), retries=False, maxsize=4, + ), +) + + +@contextmanager +def available(): + try: + yield + except (MinioException, urllib3.exceptions.HTTPError, OSError): + raise ApiError(503, "unavailable", "Хранилище вложений временно недоступно") from None + + +def ready(): + with available(): + if not client.bucket_exists(BUCKET): + raise ApiError(503, "unavailable", "Хранилище вложений не готово") + + +def put(key, file, size, content_type): + with available(): + file.seek(0) + # Весь обязательный диапазон <=10 MiB помещается в ОДИН PUT, без multipart. + client.put_object(BUCKET, key, file, size, content_type=content_type, + part_size=10 * 1024 * 1024, num_parallel_uploads=1) + + +def get(key): + with available(): + return client.get_object(BUCKET, key) + + +def remove(key): + with available(): + client.remove_object(BUCKET, key) diff --git a/src/storage.py b/src/storage.py index 3dbc765..fc76309 100644 --- a/src/storage.py +++ b/src/storage.py @@ -24,7 +24,12 @@ def public_chat(row): def public_message(row): return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), - "text": row["text"], "created_at": utc(row["created_at"])} + "text": row["text"], "created_at": utc(row["created_at"]), + "attachment_ids": [str(value) for value in row.get("attachment_ids", [])]} + + +MESSAGE_ATTACHMENTS = """ARRAY(SELECT attachment_id FROM message_attachments + WHERE message_id=m.id ORDER BY position) AS attachment_ids""" def create_user(username, display_name, password_hash): @@ -98,28 +103,38 @@ def list_chats(user_id, limit, cursor): return pagination.page([public_chat(row) for row in rows], size, scope) -def create_message(user_id, chat_id, text, key): +def create_message(user_id, chat_id, text, key, attachment_ids=()): + attachment_ids = [UUID(value) for value in attachment_ids] with db.connection() as conn: require_member(conn, user_id, chat_id) # Одна блокировка на область ключа во всех API-процессах. # Побочный эффект и результат повтора фиксируются одной транзакцией. scope = user_id + ":POST:/api/v1/chats/" + chat_id + "/messages:" + key conn.execute("SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))", (scope,)) - previous = conn.execute("""SELECT m.*, i.request_text FROM message_idempotency i + previous = conn.execute("SELECT m.*, i.request_text, i.request_attachment_ids, " + MESSAGE_ATTACHMENTS + """ FROM message_idempotency i JOIN messages m ON m.id=i.message_id WHERE i.user_id=%s AND i.chat_id=%s AND i.key=%s AND i.expires_at>clock_timestamp()""", (UUID(user_id), UUID(chat_id), key)).fetchone() if previous: - if previous["request_text"] != text: + if previous["request_text"] != text or previous["request_attachment_ids"] != attachment_ids: raise ApiError(409, "conflict", "Этот ключ уже использован с другим сообщением") return public_message(previous) + if attachment_ids: + available = conn.execute("SELECT id FROM attachments WHERE chat_id=%s AND id=ANY(%s)", + (UUID(chat_id), attachment_ids)).fetchall() + if len(available) != len(attachment_ids): + raise ApiError(404, "not_found", "Вложение не найдено в этом чате") conn.execute("DELETE FROM message_idempotency WHERE user_id=%s AND chat_id=%s AND key=%s", (UUID(user_id), UUID(chat_id), key)) row = conn.execute("""INSERT INTO messages(id, chat_id, sender_id, text) VALUES (%s, %s, %s, %s) RETURNING *""", (uuid4(), UUID(chat_id), UUID(user_id), text)).fetchone() - conn.execute("""INSERT INTO message_idempotency(user_id, chat_id, key, request_text, message_id, expires_at) - VALUES (%s, %s, %s, %s, %s, clock_timestamp() + %s * interval '1 second')""", - (UUID(user_id), UUID(chat_id), key, text, row["id"], IDEMPOTENCY_TTL)) + for position, attachment_id in enumerate(attachment_ids): + conn.execute("INSERT INTO message_attachments(message_id, attachment_id, position) VALUES (%s, %s, %s)", + (row["id"], attachment_id, position)) + conn.execute("""INSERT INTO message_idempotency(user_id, chat_id, key, request_text, request_attachment_ids, message_id, expires_at) + VALUES (%s, %s, %s, %s, %s, %s, clock_timestamp() + %s * interval '1 second')""", + (UUID(user_id), UUID(chat_id), key, text, attachment_ids, row["id"], IDEMPOTENCY_TTL)) + row["attachment_ids"] = attachment_ids result = public_message(row) return result @@ -133,7 +148,7 @@ def list_messages(user_id, chat_id, limit, cursor): found = conn.execute("SELECT 1 FROM messages WHERE chat_id=%s AND (created_at, id)=(%s, %s)", (UUID(chat_id), *anchor)).fetchone() if not found: raise ApiError(400, "invalid_request", "Неверный cursor") - condition = "AND (created_at, id) > (%s, %s)" if anchor else "" - rows = conn.execute("SELECT * FROM messages WHERE chat_id=%s " + condition + - " ORDER BY created_at, id LIMIT %s", (UUID(chat_id), *(anchor or ()), size + 1)).fetchall() + condition = "AND (m.created_at, m.id) > (%s, %s)" if anchor else "" + rows = conn.execute("SELECT m.*, " + MESSAGE_ATTACHMENTS + " FROM messages m WHERE m.chat_id=%s " + condition + + " ORDER BY m.created_at, m.id LIMIT %s", (UUID(chat_id), *(anchor or ()), size + 1)).fetchall() return pagination.page([public_message(row) for row in rows], size, scope) diff --git a/student-tests/attachment_checks.py b/student-tests/attachment_checks.py new file mode 100644 index 0000000..63562be --- /dev/null +++ b/student-tests/attachment_checks.py @@ -0,0 +1,275 @@ +"""Реальные отказы/очистка/память уровня 4; только временный тестовый стенд.""" +from concurrent.futures import ThreadPoolExecutor +import hashlib +import io +import json +import os +import socket +import subprocess +import threading +import time +from urllib.parse import urlsplit +from uuid import uuid4 + +from PIL import Image + +from helpers import Client, ROOT +from client import Failure + + +def run_checks(base, env, compose, apis, script, sql): + result = {} + client = Client(base, timeout=30) + alice, bob, _, chat = client.bootstrap() + png = (ROOT / "tests/fixtures/sample.png").read_bytes() + upload_path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=sample.png" + + def upload(): + return client.call("POST", upload_path, 201, actor=alice, raw=png, + headers={"Content-Type": "image/png"}, schema="Attachment") + + def inside(code, data=None): + completed = subprocess.run(["docker", "exec", "-i", apis()[0]["Id"], "python", "-c", code], + input=json.dumps(data), capture_output=True, text=True, timeout=45, check=True) + return json.loads(completed.stdout) + + def cleanup(): + return inside("from src import db; from src.cleanup import cleanup_once; import json; " + "db.pool.open(wait=True); print(json.dumps(cleanup_once())); db.pool.close()") + + def stat(key): + return inside("import json,sys; from src import object_store; from minio.error import S3Error\n" + "key=json.load(sys.stdin)\n" + "try:\n r=object_store.client.stat_object(object_store.BUCKET,key); print(json.dumps({'size':r.size}))\n" + "except S3Error as e:\n print(json.dumps({'error':e.code}))", key) + + def age(key): + sql("UPDATE attachment_uploads SET created_at=clock_timestamp()-interval '2 hours' WHERE object_key='" + key + "'") + + compose("stop", "cleanup") + try: + for _ in range(2): + compose("run", "--rm", "s3-bootstrap") + ready = upload()["json"] + key = sql("SELECT object_key FROM attachments WHERE id='" + ready["id"] + "'") + backend_id = compose("ps", "-q", "s3-storage") + backend = json.loads(subprocess.check_output(["docker", "inspect", backend_id], text=True))[0] + assert not set(backend["NetworkSettings"]["Networks"]) & set(apis()[0]["NetworkSettings"]["Networks"]) + backend_ip = next(iter(backend["NetworkSettings"]["Networks"].values()))["IPAddress"] + # Вызов S3 напрямую из контейнера клиента, минуя HTTP API приложения. + result["private_bucket"] = inside(""" +import io,json,socket,sys +from urllib.error import HTTPError +from urllib.request import urlopen +from minio.error import S3Error +from src import object_store +data=json.load(sys.stdin); key=data['key'] +try: + urlopen('http://s3:9000/'+object_store.BUCKET+'/'+key,timeout=3) + raise AssertionError('public original') +except HTTPError as e: + assert e.code == 403 +for operation in (lambda: object_store.client.make_bucket('forbidden-bucket'), + lambda: object_store.client.put_object(object_store.BUCKET,'outside-prefix',io.BytesIO(b'x'),1)): + try: + operation() + raise AssertionError('excess application permissions') + except S3Error as e: + assert e.code == 'AccessDenied', e.code +for port in (9333,8888,8080): + with socket.socket() as connection: + connection.settimeout(1) + assert connection.connect_ex(('s3',port)) != 0, 'admin/backend port open' + with socket.socket() as connection: + connection.settimeout(.5) + assert connection.connect_ex((data['backend_ip'],port)) != 0, 'storage network reachable from API' +print(json.dumps({'anonymous_get':403,'create_bucket':'AccessDenied','write_outside_prefix':'AccessDenied', + 'admin_ports_unreachable':[9333,8888,8080],'storage_network_isolated':True,'bootstrap_runs':2})) +""", {"key":key, "backend_ip":backend_ip}) + print("PASS: private S3, limited credentials, isolated administration, repeated bootstrap", flush=True) + + # Останавливаем именно реплику, которая ответила на upload, а затем читаем другой. + uploaded = upload() + target = uploaded["headers"]["X-Instance-Id"] + stopped_at = time.monotonic() + subprocess.run([str(ROOT / "scripts/stop-one.sh")], cwd=ROOT, + env=dict(env, TARGET_INSTANCE_ID=target), check=True, timeout=15) + script("wait-ready.sh") + while True: + try: + response = client.call("GET", "/api/v1/attachments/" + uploaded["json"]["id"] + "/content", actor=bob) + break + except Failure: + assert time.monotonic() - stopped_at < 10, "download failover exceeded 10 seconds" + time.sleep(.1) + assert response["bytes"] == png and response["headers"]["X-Instance-Id"] != target + stopped = next(info for info in apis() if info["Config"]["Hostname"] == target) + assert not stopped["State"]["Running"] + result["upload_replica_failover"] = {"uploaded_by": target, "stopped_container": stopped["Id"], + "downloaded_by": response["headers"]["X-Instance-Id"], "sha256": hashlib.sha256(png).hexdigest(), + "recovery_seconds":round(time.monotonic()-stopped_at,3)} + compose("start", "api") + script("wait-ready.sh") + print("PASS: original downloaded through another replica after stopping uploader", flush=True) + + # Незавершённое HTTP-тело не достигает S3 и не создаёт метаданных/журнала. + before = sql("SELECT (SELECT count(*) FROM attachments)||':'||(SELECT count(*) FROM attachment_uploads)") + address = urlsplit(base) + with socket.create_connection((address.hostname, address.port), timeout=3) as connection: + headers = ("POST " + upload_path + " HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer " + alice["token"] + + "\r\nContent-Type: image/png\r\nContent-Length: 1000000\r\n\r\n").encode() + connection.sendall(headers + png[:30]) + time.sleep(0.5) + assert sql("SELECT (SELECT count(*) FROM attachments)||':'||(SELECT count(*) FROM attachment_uploads)") == before + result["interrupted_body"] = {"metadata_or_journal_created": False} + + # Детерминированный реальный отказ PostgreSQL ПОСЛЕ успешного S3 PUT. + sql("""CREATE FUNCTION lab4_reject_attachment() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN RAISE EXCEPTION 'injected storage failure' USING ERRCODE='08006'; END $$; + CREATE TRIGGER lab4_reject BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION lab4_reject_attachment();""") + try: + failure = client.call("POST", upload_path, 503, actor=alice, raw=png, headers={"Content-Type": "image/png"}) + assert failure["json"]["error"]["code"] == "unavailable" + finally: + sql("DROP TRIGGER lab4_reject ON attachments; DROP FUNCTION lab4_reject_attachment()") + pending_key = sql("SELECT object_key FROM attachment_uploads ORDER BY created_at DESC LIMIT 1") + assert stat(pending_key)["size"] == len(png) + assert sql("SELECT count(*) FROM attachments WHERE object_key='" + pending_key + "'") == "0" + assert cleanup()["removed"] == 0 + age(pending_key) + lock_code = ("from src import db; from src.attachments import lock_upload; import sys\n" + "db.pool.open(wait=True)\nwith db.connection() as c:\n" + " lock_upload(c,'" + pending_key + "'); print('locked',flush=True); sys.stdin.readline()\n" + "db.pool.close()") + holder = subprocess.Popen(["docker", "exec", "-i", apis()[0]["Id"], "python", "-u", "-c", lock_code], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + try: + assert holder.stdout.readline().strip() == "locked" + assert cleanup()["skipped_active"] == 1 + assert stat(pending_key)["size"] == len(png) + finally: + holder.communicate("\n", timeout=10) + assert cleanup()["removed"] == 1 + assert stat(pending_key).get("error") == "NoSuchKey" + assert cleanup()["removed"] == 0 + # Даже устаревшая ошибочная запись журнала не позволяет удалить ready-файл. + sql("INSERT INTO attachment_uploads(id,object_key,created_at) VALUES ('" + str(uuid4()) + "','" + key + + "',clock_timestamp()-interval '2 hours')") + assert cleanup()["kept_ready"] == 1 + assert client.call("GET", "/api/v1/attachments/" + ready["id"] + "/content", actor=bob)["bytes"] == png + result["compensation"] = {"failure_after_put":503, "orphan_recorded":True, "fresh_kept":True, + "active_locked_kept":True, "expired_removed":True, "ready_kept":True, "repeat_safe":True} + print("PASS: post-PUT DB failure recorded; cleanup protects fresh, active and ready uploads", flush=True) + + try: + compose("stop", "--timeout", "8", "s3") + result["s3_outage"] = inside(""" +import json,sys +from urllib.request import Request,urlopen +from urllib.error import HTTPError +data=json.load(sys.stdin) +statuses={} +for name,method,path,body in [('live','GET','/health/live',None),('ready','GET','/health/ready',None), + ('download','GET','/api/v1/attachments/'+data['id']+'/content',None), + ('upload','POST',data['upload'],bytes.fromhex(data['png']))]: + req=Request('http://127.0.0.1:8080'+path,data=body,method=method, + headers={'Authorization':'Bearer '+data['token'],'Content-Type':'image/png'}) + try: response=urlopen(req,timeout=8) + except HTTPError as error: response=error + with response: + statuses[name]=response.status + payload=json.load(response) + if name!='live': + assert response.status==503 and payload['error']['code']=='unavailable' + assert payload['error']['request_id']==response.headers['X-Request-Id'] +assert statuses['live']==200 +print(json.dumps(statuses)) +""", {"id":ready["id"], "token":alice["token"], "upload":upload_path, "png":png.hex()}) + finally: + compose("start", "s3") + script("wait-ready.sh") + assert client.call("GET", "/api/v1/attachments/" + ready["id"] + "/content", actor=bob)["bytes"] == png + sql("UPDATE attachment_uploads SET created_at=clock_timestamp()-interval '2 hours'") + assert cleanup()["removed"] >= 1 + result["s3_outage"]["original_preserved_after_recovery"] = True + print("PASS: S3 outage gives API 503; recovery preserves original and session", flush=True) + + # Доказываем работу постоянного worker, а не только ручного вызова cleanup_once. + inside("import io,json; from src import object_store; " + "object_store.put('attachments/cleanup-worker-test',io.BytesIO(b'x'),1,'application/octet-stream'); print('{}')") + sql("INSERT INTO attachment_uploads(id,object_key,created_at) VALUES ('" + str(uuid4()) + + "','attachments/cleanup-worker-test',clock_timestamp()-interval '2 hours')") + compose("start", "cleanup") + deadline = time.monotonic() + 40 + while sql("SELECT count(*) FROM attachment_uploads WHERE object_key='attachments/cleanup-worker-test'") != "0": + assert time.monotonic() < deadline, "automatic cleanup not running" + time.sleep(0.2) + assert stat('attachments/cleanup-worker-test').get("error") == "NoSuchKey" + result["compensation"]["automatic_worker"] = True + + # 10 одновременно отправляемых реальных PNG ~5.5 MiB; монитор cgroup каждые 20 ms. + image = Image.frombytes("RGB", (1600, 1200), os.urandom(1600 * 1200 * 3)) + buffer = io.BytesIO() + image.save(buffer, format="PNG") + image.close() + data = buffer.getvalue() + expected_sha = hashlib.sha256(data).hexdigest() + monitor_code = """ +from pathlib import Path +import json,sys,threading,time +root=Path('/sys/fs/cgroup'); stop=threading.Event(); samples=[] +threading.Thread(target=lambda:(sys.stdin.readline(),stop.set()),daemon=True).start() +start=time.monotonic(); before=int((root/'memory.current').read_text()) +print('ready',flush=True) +while not stop.is_set(): + samples.append([round(time.monotonic()-start,3),int((root/'memory.current').read_text())]) + stop.wait(.02) +print(json.dumps({'baseline_bytes':before,'sample_peak_bytes':max(s[1] for s in samples), + 'lifetime_peak_bytes':int((root/'memory.peak').read_text()),'samples':samples}),flush=True) +""" + monitors = [] + responses = [] + try: + for info in apis(): + process = subprocess.Popen(["docker", "exec", "-i", info["Id"], "python", "-u", "-c", monitor_code], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + assert process.stdout.readline().strip() == "ready" + monitors.append((info, process)) + barrier = threading.Barrier(10) + def send(_): + concurrent_client = Client(base, timeout=60) + barrier.wait(timeout=15) + return concurrent_client.call("POST", upload_path, 201, actor=alice, raw=data, + headers={"Content-Type":"image/png"}, schema="Attachment") + started = time.monotonic() + with ThreadPoolExecutor(max_workers=10) as pool: + responses = list(pool.map(send, range(10))) + elapsed = time.monotonic() - started + finally: + memory = [] + for info, process in monitors: + output, error = process.communicate("\n", timeout=10) + assert process.returncode == 0, error + measurement = json.loads(output) + measurement.update({"instance": info["Config"]["Hostname"], "limit_bytes": info["HostConfig"]["Memory"]}) + assert measurement["sample_peak_bytes"] < measurement["limit_bytes"] + memory.append(measurement) + assert len(responses) == 10 + assert len({r["headers"]["X-Instance-Id"] for r in responses}) >= 2 + for response in responses: + assert response["json"]["sha256"] == expected_sha and response["json"]["size_bytes"] == len(data) + original = client.call("GET", "/api/v1/attachments/" + response["json"]["id"] + "/content", actor=bob) + assert hashlib.sha256(original["bytes"]).hexdigest() == expected_sha + assert all(not i["State"]["OOMKilled"] for i in apis()) + timings = sorted(r["elapsed"] * 1000 for r in responses) + result["parallel_uploads"] = {"concurrency":10, "successful":10, "errors":0, + "dimensions":[1600,1200], "size_bytes":len(data), "sha256":expected_sha, + "duration_seconds":round(elapsed,3), "successful_rps":round(10/elapsed,3), + "p50_ms":round(timings[4],3), "p95_ms":round(timings[-1],3), + "memory_sample_interval_seconds":0.02, "memory_includes_monitor_process":True, + "memory":memory, "all_original_checksums_verified":True, "oom_killed":False} + print("PASS: 10 concurrent real PNG uploads; all checksums match; memory peaks:", + [m["sample_peak_bytes"] for m in memory], flush=True) + return result + finally: + compose("start", "s3", "api", "cleanup") diff --git a/student-tests/docker_lifecycle.py b/student-tests/docker_lifecycle.py index c5e49e3..75582ee 100644 --- a/student-tests/docker_lifecycle.py +++ b/student-tests/docker_lifecycle.py @@ -16,6 +16,7 @@ from uuid import uuid4 from helpers import Client, ROOT from client import Failure from resilience import history +from attachment_checks import run_checks def docker(*args): @@ -26,7 +27,7 @@ def main(): with socket.socket() as sock: sock.bind(("127.0.0.1", 0)) port = sock.getsockname()[1] - project = "lab3-check-" + uuid4().hex[:10] + project = "lab4-check-" + uuid4().hex[:10] base = f"http://127.0.0.1:{port}" env = dict(os.environ, COMPOSE_PROJECT_NAME=project, HOST_PORT=str(port), API_REPLICAS="2", SESSION_TTL_SECONDS="3600", BASE_URL=base) @@ -202,15 +203,16 @@ time.sleep(30) assert all(m["Destination"].startswith("/run/secrets/") for m in info["Mounts"]) print("PASS: 2 real API containers, CPU/RAM limits, API non-root and no public ports; only LB published", flush=True) subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=env, check=True, timeout=240) - result["api_tests"] = "public smoke + 28 student tests passed" + result["api_tests"] = "public smoke + student unittest suite passed" subprocess.run([str(ROOT / "scripts/demo.sh")], cwd=ROOT, env=env, check=True, stdout=subprocess.DEVNULL, timeout=30) - assert sql("SELECT count(*) FROM schema_migrations") == "2" + assert sql("SELECT count(*) FROM schema_migrations") == "3" count = sql("SELECT count(*) FROM messages") for _ in range(2): compose("run", "--rm", "migrate") - assert sql("SELECT count(*) FROM schema_migrations") == "2" + assert sql("SELECT count(*) FROM schema_migrations") == "3" assert sql("SELECT count(*) FROM messages") == count print("PASS: migrations only in one-shot service; repeated execution preserves data", flush=True) + result["attachments"] = run_checks(base, env, compose, apis, script, sql) replay_client = Client(base) replay_actor, _, _, replay_chat = replay_client.bootstrap() @@ -225,14 +227,14 @@ time.sleep(30) assert replay_client.message(replay_actor, replay_chat, "Повтор после рестарта", key=replay_key)["json"] == saved_message result["persistence"] = {"before": list(before.values()), "after": list(after.values()), "passed": True, "idempotency_replay_after_restart": True} - print("PASS: both API, LB, PostgreSQL, Valkey actually restarted; session and confirmed data preserved", flush=True) + print("PASS: both API, LB, PostgreSQL, Valkey, S3 and cleaner actually restarted; data and session preserved", flush=True) check_shutdown() dependency_client = Client(base) actor = dependency_client.user("deps") result["dependency_readiness"] = [] - for dependency in ("valkey", "postgres"): + for dependency in ("valkey", "postgres", "s3"): try: compose("stop", "--timeout", "8", dependency) probe_id = apis()[0]["Id"] @@ -302,9 +304,9 @@ time.sleep(30) result["rolling_update"] = load_action("sequential API replacement", replace) result["passed"] = True - print("PASS: all lab3 grade-4 checks completed", flush=True) + print("PASS: all lab4 grade-4 checks completed", flush=True) finally: - output = ROOT / "evidence" / "lab3-checks.json" + output = ROOT / "evidence" / "lab4-checks.json" output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n") compose("down", "--volumes", "--timeout", "8") print("Removed only temporary test containers and volumes:", project, flush=True) diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py new file mode 100644 index 0000000..338e7ad --- /dev/null +++ b/student-tests/test_attachments.py @@ -0,0 +1,133 @@ +import hashlib +import http.client +import io +import json +import struct +from urllib.parse import urlencode, urlsplit +from uuid import uuid4 +import zlib + +from PIL import Image + +from helpers import ApiTestCase, ROOT + + +class AttachmentTests(ApiTestCase): + def upload(self, data=None, filename="photo.png", content_type="image/png", expected=201, actor=None): + if data is None: + data = (ROOT / "tests/fixtures/sample.png").read_bytes() + return self.client.call("POST", self.path + "/attachments?" + urlencode({"filename": filename}), + expected, actor=actor or self.alice, raw=data, + headers={"Content-Type": content_type}, schema="Attachment" if expected == 201 else None) + + def test_png_and_jpeg_originals(self): + jpeg = io.BytesIO() + Image.new("RGB", (100, 60), "orange").save(jpeg, format="JPEG") + for data, mime, filename in (((ROOT / "tests/fixtures/sample.png").read_bytes(), "image/png", "Фото 👋.png"), + (jpeg.getvalue(), "image/jpeg", "photo.jpeg")): + with self.subTest(mime=mime): + attachment = self.upload(data, filename, mime)["json"] + self.assertEqual(attachment["filename"], filename) + self.assertEqual(attachment["status"], "ready") + self.assertEqual(attachment["variants"], []) + self.assertIsNone(attachment["error_code"]) + self.assertEqual(attachment["sha256"], hashlib.sha256(data).hexdigest()) + self.assertEqual(attachment["size_bytes"], len(data)) + self.assertEqual(self.client.call("GET", "/api/v1/attachments/" + attachment["id"], + actor=self.bob, schema="Attachment")["json"], attachment) + result = self.client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=self.bob) + self.assertEqual(result["bytes"], data) + self.assertEqual(result["headers"].get_content_type(), mime) + self.assertEqual(result["headers"]["X-Content-Type-Options"], "nosniff") + + def test_corruption_and_mime_spoof(self): + png = (ROOT / "tests/fixtures/sample.png").read_bytes() + for data, mime in ((png, "image/jpeg"), (png[:50], "image/png"), + (b"not an image", "image/png"), (b"", "image/png"), + (b"GIF89a", "image/png"), (png, "application/octet-stream")): + with self.subTest(size=len(data), mime=mime): + result = self.upload(data, content_type=mime, expected=415) + self.assertEqual(result["json"]["error"]["code"], "unsupported_media_type") + + def test_filename_rejected_before_storage(self): + for filename in ("", " ", "../a.png", "a/b.png", "a\\b.png", "a\x00.png", "a\r\n.png", "a\x7f.png", "a" * 256): + with self.subTest(filename=repr(filename)): + result = self.upload(filename=filename, expected=400) + self.assertEqual(result["json"]["error"]["code"], "invalid_request") + self.client.call("POST", self.path + "/attachments", 400, actor=self.alice, raw=b"x", + headers={"Content-Type": "image/png"}) + + def test_content_length_limit_before_body(self): + self.client.call("POST", self.path + "/attachments?filename=a.png", 413, actor=self.alice, + raw=b"x", headers={"Content-Type": "image/png", "Content-Length": str(10485761)}) + + def test_chunked_limit_without_content_length(self): + target = urlsplit(self.client.base) + connection = http.client.HTTPConnection(target.hostname, target.port, timeout=15) + try: + connection.request("POST", self.path + "/attachments?filename=large.png", + body=iter([b"x" * 65536] * 160 + [b"x"]), + headers={"Authorization": "Bearer " + self.alice["token"], "Content-Type": "image/png"}, + encode_chunked=True) + response = connection.getresponse() + self.assertEqual(response.status, 413) + self.assertEqual(json.loads(response.read())["error"]["code"], "payload_too_large") + finally: + connection.close() + + def test_pixel_limit_before_decode(self): + def chunk(kind, data): + return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) + # Размеры в корректном IHDR превышают лимит; pixel data вообще не выделяется. + data = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 10000, 4001, 8, 2, 0, 0, 0)) + data += chunk(b"IDAT", b"") + chunk(b"IEND", b"") + self.upload(data, expected=413) + + def test_access_and_authentication(self): + attachment = self.upload()["json"] + for suffix in ("", "/content"): + path = "/api/v1/attachments/" + attachment["id"] + suffix + self.assert_error("GET", path, 401, "unauthorized") + self.assert_error("GET", path, 404, "not_found", actor=self.eve) + self.assert_error("GET", "/api/v1/attachments/" + str(uuid4()) + suffix, + 404, "not_found", actor=self.eve) + self.upload(expected=404, actor=self.eve) + self.client.call("POST", self.path + "/attachments?filename=../bad", 401, + raw=b"x", headers={"Content-Type": "image/png"}) + self.assert_error("GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail", + 404, "not_found", actor=self.alice) + + def test_attachment_only_message_and_other_member(self): + attachment = self.upload()["json"] + message = self.client.message(self.bob, self.chat, "", attachments=[attachment["id"]])["json"] + self.assertEqual(message["attachment_ids"], [attachment["id"]]) + self.assertEqual(message["sender_id"], self.bob["id"]) + self.assertEqual(self.read_all(self.path + "/messages", self.alice, "MessagePage"), [message]) + + def test_foreign_and_unknown_attachment_ids(self): + attachment = self.upload()["json"] + other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, schema="Chat", + body={"title": "Другой чат", "member_ids": [self.alice["id"]]})["json"] + for chat_id, ids in ((self.chat["id"], [str(uuid4())]), (other["id"], [attachment["id"]])): + self.assert_error("POST", "/api/v1/chats/" + chat_id + "/messages", 404, "not_found", actor=self.alice, + body={"text": "", "attachment_ids": ids}) + + def test_invalid_attachment_lists(self): + attachment = self.upload()["json"] + for ids in ([attachment["id"], attachment["id"].upper()], [str(uuid4()) for _ in range(11)], + ["not-a-uuid"], [True], None, "uuid"): + self.assert_error("POST", self.path + "/messages", 400, "invalid_request", actor=self.alice, + body={"text": "Текст", "attachment_ids": ids}) + + def test_idempotency_includes_ordered_attachment_ids(self): + first, second = self.upload()["json"], self.upload()["json"] + key = uuid4().hex + ids = [first["id"], second["id"]] + saved = self.client.message(self.alice, self.chat, "", key=key, attachments=ids)["json"] + self.assertEqual(self.client.message(self.alice, self.chat, "", key=key, attachments=ids)["json"], saved) + for changed in ([second["id"], first["id"]], [first["id"]]): + self.assert_error("POST", self.path + "/messages", 409, "conflict", actor=self.alice, + body={"text": "", "attachment_ids": changed}, headers={"Idempotency-Key": key}) + key = uuid4().hex + saved = self.client.message(self.alice, self.chat, "Без файла", key=key)["json"] + self.assertEqual(self.client.message(self.alice, self.chat, "Без файла", key=key, attachments=[])["json"], saved)