diff --git a/.env.example b/.env.example index d99d26a2..229988d7 100644 --- a/.env.example +++ b/.env.example @@ -1,20 +1,9 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. -APP_HOST=0.0.0.0 -APP_PORT=8080 -APP_ENV=lab +APP_PORT=18089 SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 -IDEMPOTENCY_TTL_SECONDS=86400 -S3_ENDPOINT=http://s3:9000 -S3_REGION=us-east-1 -S3_BUCKET=messenger-attachments -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 +API_CPUS=1 +API_MEMORY=512m +HTTP_WORKERS=16 +DB_POOL_MAX=8 +DB_ACCESS_MODE=optimized +POSTGRES_CPUS=1 diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 00000000..9e15045b --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,26 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read +jobs: + application: + runs-on: ubuntu-latest + timeout-minutes: 15 + env: + BASE_URL: http://127.0.0.1:18089 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: | + python scripts/bootstrap.py + docker compose build api + docker compose up -d --wait + python scripts/wait_ready.py + make check test test-student BASE_URL=$BASE_URL + make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=$BASE_URL + docker compose up -d --wait --no-deps api + make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=$BASE_URL + - if: always() + run: docker compose down diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5e287bd9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 APP_HOST=0.0.0.0 APP_PORT=8080 MALLOC_ARENA_MAX=2 +WORKDIR /app +RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt +COPY src/ ./src/ +COPY migrations/ ./migrations/ +USER 10001:10001 +EXPOSE 8080 +HEALTHCHECK --interval=5s --timeout=3s --start-period=15s --retries=3 CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()"] +STOPSIGNAL SIGTERM +CMD ["python", "src/server.py"] diff --git a/Makefile b/Makefile index 5d86d171..32258f81 100644 --- a/Makefile +++ b/Makefile @@ -28,3 +28,14 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: test-student reconcile reconcile-apply + +test-student: + BASE_URL="$(BASE_URL)" $(PYTHON) -m unittest discover -s student-tests -p 'test_*.py' -v + +reconcile: + docker compose run --rm --no-deps reconciler + +reconcile-apply: + docker compose run --rm --no-deps reconciler python src/reconcile.py --apply diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..4d8bad22 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,218 @@ -# Отчёт — лабораторная 4 +# Лабораторная 4 — S3-хранилище и вложения -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +Автор: **Майорова Софья, БВТ2301**. Целевая оценка: **5**. Выбран **трек 2: согласованность двух хранилищ**. Проверки выполнены 27 октября 2026 года. Окончательную оценку выставляет преподаватель. + +Работа продолжает [мою lab3, PR 55](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab3/pulls/55). Сохранены PostgreSQL, Redis-кэш профиля, сессии, keyset-пагинация, балансировка и транзакционная идемпотентность сообщений на 24 часа. Добавлены приватное S3, бинарные вложения, проверки изображений и повторяемая безопасная сверка S3/БД. Исходные README, COURSE, contracts и публичные tests этой лабораторной сохранены. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Engine с Compose v2 и Python 3.9+ на хосте. Сервер работает на Python 3.12.8 внутри образа; устанавливать серверные библиотеки на Mac не нужно. Для используемого Mac Docker запускается через отдельный профиль Colima: -## Архитектура и решения +```sh +colima start --profile mtusi-labs +export DOCKER_CONTEXT=colima-mtusi-labs +cd "/Users/ssmayorova/Documents/Учеба/Высоконагруженные приложения/lab4" +python3 scripts/bootstrap.py +docker compose build api +docker compose up -d --wait +export BASE_URL=http://127.0.0.1:18089 +python3 scripts/wait_ready.py +make check test test-student BASE_URL=$BASE_URL +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +На другой машине вместо этого `cd` клонировать публичный форк и выбрать ветку `solution/BVT2301-Mayorova`. `build api` идёт перед `up`: одноразовые migrate/bootstrap используют тот же уже построенный образ. `bootstrap.py` создаёт только локальные `.env` и `secrets/`, не печатает значения и не перезаписывает существующие. Пароли случайные; путь секрета — не его значение. Каталог secrets имеет 0700, файлы читаются UID 10001 в контейнере. Секреты, `.env`, runtime/build и данные исключены из Git и Docker build context. -## Доказательства критериев +`migrate` один раз применяет версии 001–004 под advisory lock; API DDL не выполняет. Затем административный `bucket-bootstrap` создаёт bucket. Уже существующий bucket допустим, повторный bootstrap проверен. API стартуют после обоих jobs. Контейнер s3-store хранит **и volume-файлы, и filer metadata** под `/data` на одном persistent volume; PostgreSQL использует другой volume. Redis — восстанавливаемый кэш, сессии находятся в PostgreSQL. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Остановка с сохранением данных: -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +```sh +docker compose down +``` -## Эксперименты и отказы +Только для намеренного удаления **данных этого учебного проекта**, когда они больше не нужны: `docker compose down -v`. Эта команда не используется в restart/failover. Закрытые тестовые volumes отдельного clean-clone стенда удаляются после окончания его проверки. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +`.env.example` содержит реальные настройки моего решения: APP_PORT=18089 — только порт LB на localhost, API_REPLICAS=2, API_CPUS=1, API_MEMORY=512m, HTTP_WORKERS=16, DB_POOL_MAX=8, DB_ACCESS_MODE=optimized, POSTGRES_CPUS=1, SESSION_TTL_SECONDS=3600. Внутренний API слушает 8080. Endpoint/bucket/пути S3 credentials задаются в Compose; ограничения 10 MiB и 40 млн пикселей закреплены в коде по контракту и не увеличиваются переменной окружения. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +## Архитектура и данные -## Ограничения и следующие шаги +```text +Клиент → 127.0.0.1:18089 → HAProxy → API × 2 + ├→ PostgreSQL (общие метаданные/сессии/идемпотентность) + ├→ Redis (необязательный кэш профилей) + └→ S3 gateway → отдельная сеть storage → SeaweedFS mini + └→ s3_data +``` -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Выбрана официальная SeaweedFS **4.51**, digest `sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8`. Образ и реальные параметры проверены запуском. Шлюз `weed s3` принимает только S3; IAM API, Iceberg/Lance отключены. Mini в s3-store запускает master/volume/filer, а его S3, WebDAV и административная консоль отключены. Из API недоступен filer: API не состоит в сети storage. Ни S3, ни PostgreSQL, ни Redis, ни отдельные API-порты на хост не публикуются. Только LB подключён к edge. + +Образы Python 3.12.8, PostgreSQL 17.2-bookworm, Redis 7.4.1-bookworm, HAProxy 3.0.5-alpine также закреплены digest в Dockerfile/Compose. Pillow 13.0.0 проверяет изображения, boto3 1.40.61 обращается к S3; точные прямые и транзитивные Python-зависимости закреплены в requirements.txt. Их выбор — проверенная версия, не обещание постоянного отсутствия уязвимостей. + +| Роль | CPU | Память | Устойчивые данные | +| --- | ---: | ---: | --- | +| API, каждая из двух реплик | 1 | 512 MiB | Нет | +| PostgreSQL | 1 | 512 MiB | postgres_data | +| SeaweedFS master/volume/filer | 1 | 512 MiB | s3_data | +| S3 gateway | 0,5 | 512 MiB, GOMEMLIMIT=384MiB | Нет | +| HAProxy | 0,5 | 64 MiB | Нет | +| Redis | 0,25 | 64 MiB, maxmemory=32mb | Нет | + +CPU-лимиты суммарно превышают 4 vCPU локальной VM; это допускаемый overcommit локального стенда, не выделенные физические ядра каждому сервису. Поэтому измерения не выдаются за максимальную производительность production. + +Миграция 004 создаёт `attachments`: UUID, chat_id, owner_id, filename, случайный object_key, истинный MIME, размер, SHA-256, внутреннее состояние и timestamps. `messages.attachment_ids` сохраняет порядок ссылок; `message_attachments` с FK защищает целостность и запрещает удалить используемое вложение. Пустой текст допустим с вложениями; текст до 4000 Unicode code points, включая U+0000, по-прежнему сохраняется lossless в BYTEA. + +`POST /chats/{id}/attachments?filename=...` принимает сырые PNG/JPEG, не multipart. Сервер создаёт ключ `originals/`, имя файла никогда не является путём. Ответ 201 содержит ready, variants=[], error_code=null, checksum оригинала. Участник другого чата/посторонний получает 404 для metadata/content и при попытке прикрепить чужое вложение. Отсутствие Bearer даёт 401. Download идёт через API без redirect; истинный Content-Type и `nosniff`, `private, no-store`, байты не перекодируются. Ключи S3 и адреса backend не входят в публичные модели. Любой участник своего чата может прикрепить доступное вложение. + +## Границы операции upload + +1. Аутентификация и проверка членства предшествуют приёму тела файла. Проверяются query/filename, один Content-Length, отсутствие Transfer-Encoding, лимит bytes и MIME. +2. Файл читается кусками до 64 KiB в `TemporaryFile` внутри ограниченного tmpfs `/tmp` (128 MiB на API). Это только временный буфер незавершённой операции, не источник хранения подтверждённого файла. При исключении/обрыве контекст закрывает файл; при падении процесса tmpfs исчезает. +3. Вычисляется SHA-256. Pillow открывает только PNG/JPEG, проверяет размеры **до load**, затем `verify()` и полное декодирование первого изображения. MIME сравнивается с форматом по содержимому. Ошибки дают 415, превышение pixels — 413. Управляющие символы, NUL, slash/backslash в filename — 400. Для защиты от сжатых PNG-текстов установлен дополнительный лимит декодированных текстовых метаданных 1 MiB. +4. PostgreSQL фиксирует устойчивый intent со state=uploading **до S3 PUT**. Он уже содержит всю ожидаемую checksum/size и назначенный сервером key. +5. В новой транзакции upload берёт row lock на intent, выполняет S3 PUT, затем обновляет ready. COMMIT завершён до возврата 201. Lock удерживается до COMMIT, поэтому reconciler не может собрать выполняющуюся операцию. +6. Обрыв до intent не создаёт S3-объекта. Отказ после intent/PUT оставляет учтённое состояние uploading, которое безопасно убирается последующей сверкой. Ошибка записи в БД отдаёт 503 без SQL/credentials. Неопределённый результат HTTP upload нельзя прозрачно повторять: upload по контракту не идемпотентен. Сообщения с Idempotency-Key остаются идемпотентными через общую транзакцию PostgreSQL. + +Не обещается распределённая ACID-транзакция между PostgreSQL и S3. Именно окно между двумя хранилищами исследуется в выбранном треке. + +## Ограничение ресурсов и таймауты + +На API максимум 16 выполняющихся HTTP handlers и 32 принятых соединения; заполнение очереди даёт 503. Одновременно принимаются максимум 8 upload на реплику, декодируется **один** файл. Пиксельный буфер явно освобождается `image.close()` до освобождения decoder semaphore; duplicate file descriptor сохраняет исходный временный файл для S3. Это важно: один только выход из контекста Pillow не должен оставлять предыдущий декодированный объект живым, пока следующий запрос уже декодируется. + +Content-Length >10 485 760 отклоняется до чтения файла; dimension check >40 000 000 — до распаковки. Приём тела имеет общий deadline 15 секунд и idle timeout ≤3 секунд; `read1` позволяет пересчитывать общий deadline даже при постоянной подаче очень маленьких порций. Ожидание декодера ограничено 10 секундами. S3 connect timeout 1 секунда, socket read timeout 3 секунды, SDK total_max_attempts=1; автоматического retry upload нет. HTTP LB client/server timeouts 30 секунд. Download также идёт кусками 64 KiB, с ограничением времени, первый кусок читается до 200. Ошибка S3 посреди уже начатого ответа закрывает соединение; клиент обязан считать недополученное тело ошибкой. + +Пул PostgreSQL ≤8 соединений на API, ожидание 2 секунды, statement_timeout=2s, lock_timeout=1,5s. SIGTERM прекращает приём новых соединений, drain ограничен 5 секундами. Readiness проверяет PostgreSQL и S3 bucket — зависимости обязательного синхронного пути; Redis не критичен. Liveness проверяет жизнь процесса. При недоступном S3 LB убирает API из ready backend; ответы самого LB о невозможности соединения могут отличаться от JSON-ошибки API. + +## Приватность bucket и права + +`bootstrap.py` генерирует три разные S3 identities. Административная имеет Admin/Read/Write/List и доступна только bucket-bootstrap и явно запускаемому тестовому audit. API и reconciler получают отдельные пары с Read/Write/List **только messenger-attachments**, без Admin. Write включает удаление объектов этого bucket; API не использует этот метод в публичном endpoint, reconciler использует для компенсации. Это ограничение по bucket, не по одному пользовательскому чату: ACL чата обеспечивает приложение. + +Нет anonymous identity/публичного policy. `storage_checks.py` вне API действительно получает 403 на прямой unauthenticated GET известного key и bucket listing. С реальными ключами API создание другого bucket, listing другого bucket и изменение policy дают 403. Внутренний filer и административный порт также недоступны из сети API. `audit` — только tools profile, не запускается обычным up; его административные секреты никогда не подключаются к API. Bucket bootstrap успешно повторён. + +## Трек 2: повторяемый reconciler + +**Гипотеза:** устойчивый intent перед PUT, row lock и консервативная сверка позволят исправлять частичные записи без удаления свежего или используемого файла; цена полного прохода будет расти примерно линейно с числом метаданных/объектов. + +Оператор сначала запускает dry-run, затем после просмотра — apply: + +```sh +make reconcile +make reconcile-apply +``` + +Обе команды используют отдельные ограниченные credentials reconciler. По умолчанию grace **3600 секунд** и dry-run; команда без `--apply` не меняет ни S3, ни строки БД. Значение grace <3600 отвергается, кроме явно отмеченного флага `--allow-short-test-grace` на синтетическом стенде. Для регулярного обслуживания оператор должен повторять эти команды; скрытого фонового удаления нет. + +Сверка берёт общий advisory lock, чтобы два reconciler не работали одновременно. Cutoff фиксируется в начале прохода. PostgreSQL читается страницами по 100 UUID, S3 list — страницами по 100 ключей; findings ограничены 200 записями, счётчики полные. Внутри каждой операции берётся `FOR UPDATE SKIP LOCKED`: занятая живым upload строка пропускается, а свежая по updated_at защищена grace period. + +| Обнаруженное состояние | Dry-run | Apply и повторный запуск | +| --- | --- | --- | +| Старый uploading, PUT не было | stale_intent | Удаляет intent, S3 delete идемпотентен | +| Старый uploading, PUT уже был, ready/COMMIT сорвался | stale_intent | Удаляет объект, затем intent; при сбое между delete и БД следующий проход завершит очистку | +| Старый объект originals/UUIDv4 без строки БД | orphan | Перед delete повторно проверяет отсутствие строки и свежесть HEAD; ключи никогда не переиспользуются | +| Ready-метаданные, S3 отсутствует/size или metadata checksum не совпадают | missing_or_mismatched | Сохраняет строку и все ссылки, ставит внутренний storage_missing; API возвращает 503 вместо ложного оригинала | +| storage_missing, оригинал восстановлен извне | recoverable | Потоково проверяет **реальные байты** SHA-256/size, только затем возвращает ready | +| Ready и корректный HEAD, даже без сообщения | Защищено | Никогда не удаляет | +| Свежее, занято row lock, неуправляемое имя/prefix | Защищено/пропущено | Не удаляет | + +`storage_missing` — внутреннее состояние, не дополнительное значение публичного enum lab4. Такой ресурс возвращает 503; восстановленная запись снова отдаёт нормальную Attachment со status=ready. Reconciler не может изобрести потерянные bytes: для восстановления нужна внешняя сохранённая копия/backup, в эксперименте это исходный fixture в памяти теста. + +Ключевое доказательство безопасности: корректный API **сначала** фиксирует intent и **потом** создаёт объект с новым UUID. Поэтому старый key без строки не может быть обычным upload в окне до фиксации метаданных. Используемая строка защищена state, row lock и FK; ready-вложения сборщик вообще не удаляет. Если БД/S3 недоступны, проход прерывается без fail-open удаления. Уже выполненные S3 delete не откатываются вместе с PostgreSQL, но устойчивый intent делает повтор безопасным. + +`student-tests/research.py` использует настоящие S3 и PostgreSQL: вводит исключение trigger непосредственно в ready UPDATE после успешного PUT; создаёт intent без PUT, объект без метаданных, удаляет оригинал используемого вложения, удерживает row lock как живой uploader, создаёт свежий intent/объект. Реальная выдержка 3 секунды делает старый orphan старше тестового grace=2, S3 LastModified и часы не подменяются. Dry-run, apply, повтор и восстановление имеют assert на фактические строки, bytes и ссылки. Тестовые grace=2 и SQL fault injection предназначены только для данного синтетического стенда. + +Команды воспроизведения security и исследования (audit получает отдельные локальные тестовые секреты): + +```sh +docker compose run --rm -T --no-deps -v "$PWD:/work:ro" -w /work audit python student-tests/storage_checks.py +docker compose run --rm -T --no-deps -v "$PWD:/work:ro" -w /work audit python student-tests/research.py +``` + +## Измерения + +Стенд: macOS arm64, Colima/Ubuntu Linux, 4 vCPU и примерно 4 GiB RAM. Лимиты контейнеров приведены выше. Остальные учебные стенды не останавливались; это локальный эксперимент без гарантии отсутствия соседней нагрузки. Генерация fixtures и bootstrap выполняются до начала измерения. + +Для стоимости сверки использованы три популяции — 10, 110, 510 строк и столько же S3-объектов, три последовательных dry-run на каждой. Исходная популяция включает данные проверок; каждый этап добавляет соответственно 100 и 400 подтверждённых тестовых ready-объектов. При повторе сценария абсолютная исходная популяция может вырасти: результаты всегда содержат фактические counts, а не предполагаемый N. + +| Метаданных / объектов | Медиана полного обхода | Наблюдаемые HEAD | +| --- | ---: | ---: | +| 10 / 10 | 19,56 мс | 9 | +| 110 / 110 | 136,28 мс | 109 | +| 510 / 510 | 603,06 мс | 509–510 | + +Количество HEAD немного различается из-за grace. Полный проход включает создание S3 client, advisory lock, PostgreSQL-страницы/lookup, S3 list и HEAD. Основные bytes не скачиваются при обычном HEAD; GET/SHA-256 используются только при восстановлении storage_missing. Память прохода ограничена размером страниц. Последовательный алгоритм имеет O(N+M) обход, но **не является snapshot между двумя хранилищами**; пропущенное/изменившееся состояние проверяется следующим запуском. Увеличение 110→510 объектов дало примерно пропорциональный рост времени; это поддерживает гипотезу на данном диапазоне, не прогноз миллиардов объектов. + +На каждом профиле **10 клиентов одновременно** начинают upload, затем скачивают результат с тем же SHA-256. Один прогон на профиль; fixtures детерминированы, подготовлены заранее. Нет скрытых retry. Перед каждым профилем API перезапускаются, поэтому cgroup memory.peak относится к этому запуску. Опрос memory.current/peak/events и cpu.stat идёт дополнительными docker exec; это вносит накладные расходы. Измеряется end-to-end upload+download, не только POST и не предельный RPS. Для десяти запросов p95/p99 по nearest-rank оба равны максимуму. + +| PNG: bytes / pixels | Успех / ошибки | Весь сценарий | p50 / p95=p99 end-to-end | Пик API 1 / API 2 | +| --- | --- | ---: | ---: | ---: | +| 529,848 / 176,400 | 10/10 / 0 | 0.387 с | 141.3 / 149.4 мс | 67.05 / 67.18 MiB | +| 7,563,763 / 2,520,000 | 10/10 / 0 | 2.042 с | 1711.5 / 1743.2 мс | 127.41 / 127.04 MiB | +| 533,859 / 40,000,000 | 10/10 / 0 | 0.831 с | 416.2 / 602.8 мс | 326.36 / 319.54 MiB | + +В финальных трёх профилях 30/30 загрузок вернули 201, скачанные SHA-256 совпали, OOM kill = 0. Лимит каждой API — 512 MiB. Высокий пик третьего профиля связан с 40 млн декодированных RGB-пикселей при небольшом сжатом файле: размер HTTP-тела не равен размеру изображения в RAM. Различие реплик отражает распределение нагрузки и память аллокатора; это не доказательство разницы алгоритмов. + +Средний CPU API за сценарий оценивается по разнице cgroup cpu.stat usage_usec, делённой на время сценария (100% = одно ядро; короткая длительность и docker exec влияют на оценку): +- 176400 pixels: API 1 / API 2 = 45.7% / 43.7%. +- 2520000 pixels: API 1 / API 2 = 36.2% / 35.1%. +- 40000000 pixels: API 1 / API 2 = 77.7% / 79.5%. + +![Сверка и память при загрузках](evidence/research.png) + +Ранние прогоны сохранены, а не скрыты: с лимитом S3 gateway 192 MiB обнаружен его OOM; после изменения gateway на 512 MiB, GOMEMLIMIT=384MiB и ограничения concurrentUploadLimitMB до 32 повтор выявил отдельный OOM API на RGB-изображениях 40 Мп. Причина API устранена освобождением пиксельного буфера **до** освобождения decoder semaphore. Финальные прогоны с исходным поддерживаемым лимитом 512 MiB/API проверяют именно этот случай. Ошибочные запуски не включены в успешные итоговые измерения, их stderr и State с OOMKilled=true оставлены в evidence. + +## Проверки и доказательства + +| Критерий | Команда/сценарий | Результат / evidence | +| --- | --- | --- | +| Оригинальные контракт и tests | make check; git diff относительно skeleton | Не изменены; final-tests.txt | +| Минимум lab3 + вложения | make test | Публичный lab4 smoke PASS; final-tests.txt | +| Дополнительные API/Unicode/сессии/гонки | make test-student | 21 тест PASS; final-tests.txt | +| PNG/JPEG, checksum, приватность, чужой chat/id | test_attachments.py и storage_checks.py | Побайтное совпадение; 400/401/404/413/415; storage-security.json | +| 10 MiB/40 Мп и повреждённый файл | test_bad_images_and_mime, test_valid_pixel_boundary | 413 выше границы, 415 для подмены/повреждения, ровно 40 Мп принято | +| Остановка именно загрузившей API | student-tests/faults.py | Каждая реальная реплика остановлена отдельно, файл скачан другой с прежним Bearer; faults.json | +| Обязательный публичный failover сообщений | make test-failover ACTION_SCRIPT=scripts/stop-one.sh | 2 реплики, 116 подтверждённых записей, 1 транзитная ошибка, восстановление 2,594 с; public-failover.txt | +| Restart API, LB, PostgreSQL, Redis и обоих S3-компонентов | make test-persistence ACTION_SCRIPT=scripts/restart.sh | Прежние сессия, сообщения, attachment metadata/bytes сохранены; persistence.txt, restart-before/after.json | +| Недоступность S3, fallback Redis | student-tests/faults.py | S3: ready/content 503, live 200; после восстановления прежние bytes; Redis fallback; faults.json | +| Обрыв приёма | student-tests/faults.py | Нет intent для прерванного filename, PUT не выполнялся | +| Медленный непрерывный клиент | student-tests/slow_upload.py | 400 около общего deadline 15 с; slow-upload.json | +| Отдельные ограниченные S3 credentials и закрытая сеть | storage_checks.py | Anonymous GET/list 403, три запрещённых операции API 403, filer/admin недоступны; storage-security.json | +| Идемпотентный bootstrap bucket | Повторный compose run bucket-bootstrap | Exit 0; bucket-bootstrap-repeat.txt | +| Потоковое ограничение RAM, ≥10 одновременных файлов | student-tests/uploads.py | uploads.json, график; cgroup peaks и OOM counters | +| Согласованность, свежие/используемые/занятые объекты | research.py | Все 7 assertions-групп true; dry/apply/repeat/recover; reconciliation.json | +| Стоимость обхода | research.py | 9 реальных измерений на трёх популяциях; reconciliation.json | +| Чистый clone/новые volumes | Отдельный проект, порт и секреты | PASS: новые секреты, пустые PostgreSQL/S3 volumes, smoke + 21 тест + полный restart; clean-clone.txt | + +Публичные команды отказов управляют только lab4; после stop-one реплика остаётся остановленной по контракту теста и восстанавливается явно `docker compose up -d --wait --no-deps api`. Дополнительный faults.py сам восстанавливает каждую зависимость в finally. Снимки runtime содержат только разрешённые поля состояния, лимитов, mounts и портов, без environment/credentials. Не сохраняются тела Basic/Bearer или полные session responses. + +```sh +export DOCKER_CONTEXT=colima-mtusi-labs +export BASE_URL=http://127.0.0.1:18089 +make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=$BASE_URL +docker compose up -d --wait --no-deps api +make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=$BASE_URL +python3 student-tests/faults.py +python3 student-tests/slow_upload.py +python3 student-tests/uploads.py +``` + +Для повторной генерации графиков нужен Python 3.11+ и `pip install -r requirements-research.txt`, затем `python student-tests/plot.py`; приложенные PNG/SVG уже готовы. Workflow Application добавлен отдельно от исходной проверки skeleton; hosted CI требует разрешений платформы и здесь не объявляется прошедшим автоматически. + +## Ограничения + +Доказаны отказ API-реплики и restart с сохранёнными volumes; один PostgreSQL, один S3-store, один LB и один host остаются точками отказа. Это не multi-node HA и не гарантия сохранности при отключении питания. TLS отсутствует, поэтому публикуется только localhost с синтетическими данными. Root-доступ к Docker равносилен доступу к секретам стенда. + +Распределённой транзакции нет, до очередного apply старые uploading/orphan могут оставаться. Готовые, но неиспользуемые вложения сборщик намеренно сохраняет, поскольку контракт не содержит удаления. Данные экспериментов/публичных тестов также накапливаются. Для постоянно работающего сервиса нужны расписание обслуживания, backup S3+БД и контроль расходов. + +Обычный проход сверяет HEAD и сохранённую S3 checksum metadata, а не перечитывает каждый корректный объект: одинаково изменённые bytes и metadata сторонним привилегированным оператором так не обнаруживаются. Восстановление missing всегда проверяет реальные bytes. Параллельные изменения БД/S3 и значительный clock skew требуют повторного прохода и аккуратного grace; штатные API не переиспользуют ключи и не изменяют ready-объекты. Grace 3600 ориентирован на значительно более короткую обычную загрузку; уменьшать его на реальных данных без исследования нельзя. + +Проверка изображений декодирует первый кадр, не выполняет антивирус и не создаёт thumbnail; дополнительные PNG-текстовые метаданные ограничены 1 MiB как защита parser RAM. UI, presigned upload и трек 1 не реализуются: выбран законченный трек 2. Показатели маленького локального эксперимента не задают SLA или максимальный RPS. + +## Источники и помощь + +Перенесено собственное решение lab3 (Майорова Софья, PR 55); чужие студенческие решения не использовались. Задание/контракты текущего skeleton — основание обязательного поведения. + +- [SeaweedFS S3 API](https://github.com/seaweedfs/seaweedfs/wiki/Amazon-S3-API), [S3 credentials и bucket-scoped actions](https://github.com/seaweedfs/seaweedfs/wiki/S3-Credentials), параметры mini проверены у закреплённого Docker-образа. +- [Pillow Image: ограничения, verify/load/close](https://pillow.readthedocs.io/en/stable/reference/Image.html). +- [Boto3 put_object](https://docs.aws.amazon.com/boto3/latest/reference/services/s3/client/put_object.html). +- [PostgreSQL row-level/advisory locks](https://www.postgresql.org/docs/17/explicit-locking.html). + +Использован Codex для разработки, подготовки тестов, запуска измерений и оформления. Все приведённые PASS/числа получены реальными командами данного стенда; утверждения ограничены фактически проверенными сценариями. Имя автора в Git — Майорова Софья; генеративная помощь раскрыта явно. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..f8dc59f0 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,185 @@ +name: mtusi-lab4 +x-application: &application + image: mtusi-messenger:lab4 + networks: [backend] + environment: &environment + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + HTTP_WORKERS: ${HTTP_WORKERS:-16} + DB_POOL_MAX: ${DB_POOL_MAX:-8} + DB_ACCESS_MODE: ${DB_ACCESS_MODE:-optimized} + LOG_LEVEL: WARNING + S3_ENDPOINT: http://s3:8333 + S3_BUCKET: messenger-attachments + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_api_key + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_api_secret + secrets: [database_url, cache_url, s3_api_key, s3_api_secret] + tmpfs: ["/tmp:size=128m,mode=1777"] + mem_limit: ${API_MEMORY:-512m} + cpus: ${API_CPUS:-1} + stop_grace_period: 10s +services: + migrate: + <<: *application + command: [python, src/migrate.py] + healthcheck: + disable: true + deploy: + replicas: 1 + depends_on: + postgres: + condition: service_healthy + api: + <<: *application + build: . + deploy: + replicas: ${API_REPLICAS:-2} + depends_on: + migrate: + condition: service_completed_successfully + redis: + condition: service_healthy + bucket-bootstrap: + condition: service_completed_successfully + bucket-bootstrap: + <<: *application + command: [python, src/bucket_bootstrap.py] + environment: + <<: *environment + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_key + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret + secrets: [s3_admin_key, s3_admin_secret] + healthcheck: + disable: true + depends_on: + s3: + condition: service_healthy + reconciler: + <<: *application + profiles: [tools] + command: [python, src/reconcile.py] + environment: + <<: *environment + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_reconciler_key + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_reconciler_secret + secrets: [database_url, s3_reconciler_key, s3_reconciler_secret] + healthcheck: + disable: true + deploy: + replicas: 1 + audit: + <<: *application + profiles: [tools] + environment: + <<: *environment + BASE_URL: http://lb:8080 + secrets: [database_url, cache_url, s3_api_key, s3_api_secret, s3_admin_key, s3_admin_secret, s3_reconciler_key, s3_reconciler_secret] + healthcheck: + disable: true + deploy: + replicas: 1 + s3-store: + image: chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8 + command: [mini, -dir=/data, -ip=s3-store, -s3=false, -admin.ui=false, -webdav=false, -master.telemetry=false, -master.volumeSizeLimitMB=128, -volume.max=16, -volume.fileSizeLimitMB=11, -volume.concurrentUploadLimitMB=64, -volume.concurrentDownloadLimitMB=64] + networks: [storage] + volumes: [s3_data:/data] + mem_limit: 512m + cpus: 1 + stop_grace_period: 20s + healthcheck: + test: [CMD, wget, -q, -O, /dev/null, http://127.0.0.1:8888/] + interval: 2s + timeout: 2s + retries: 45 + s3: + image: chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8 + command: [s3, -filer=s3-store:8888, -port=8333, -config=/run/secrets/s3_config, -iam=false, -port.iceberg=0, -port.lance=0, -concurrentFileUploadLimit=16, -concurrentUploadLimitMB=32, -readerCacheSizeMB=16] + secrets: [s3_config] + networks: [backend, storage] + mem_limit: 512m + environment: + GOMEMLIMIT: 384MiB + cpus: 0.5 + depends_on: + s3-store: + condition: service_healthy + healthcheck: + test: [CMD-SHELL, 'nc -z 127.0.0.1 8333'] + interval: 2s + timeout: 1s + retries: 30 + lb: + image: haproxy:3.0.5-alpine@sha256:43a2591a6a62a41dd22edb4452880e9c70de489fbd7c54ffb52d035cccbfe949 + ports: ["127.0.0.1:${APP_PORT:-18089}:8080"] + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + networks: [edge, backend] + depends_on: + api: + condition: service_healthy + mem_limit: 64m + cpus: 0.5 + stop_grace_period: 12s + healthcheck: + test: [CMD, wget, -q, -O, /dev/null, http://127.0.0.1:8080/health/ready] + interval: 2s + timeout: 2s + retries: 10 + postgres: + image: postgres:17.2-bookworm@sha256:3267c505060a0052e5aa6e5175a7b41ab6b04da2f8c4540fc6e98a37210aa2d3 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: [postgres_password] + volumes: [postgres_data:/var/lib/postgresql/data] + networks: [backend] + healthcheck: + test: [CMD, pg_isready, -h, 127.0.0.1, -U, messenger, -d, messenger] + interval: 2s + timeout: 1s + retries: 30 + mem_limit: 512m + cpus: ${POSTGRES_CPUS:-1} + redis: + image: redis:7.4.1-bookworm@sha256:bb142a9c18ac18a16713c1491d779697b4e107c22a97266616099d288237ef47 + command: [redis-server, --maxmemory, 32mb, --maxmemory-policy, allkeys-lru, --save, "", --appendonly, "no"] + networks: [backend] + healthcheck: + test: [CMD, redis-cli, ping] + interval: 2s + timeout: 1s + retries: 30 + mem_limit: 64m + cpus: 0.25 +volumes: + postgres_data: + s3_data: +networks: + edge: + storage: + internal: true + backend: + internal: true +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + s3_admin_key: + file: ./secrets/s3_admin_key + s3_admin_secret: + file: ./secrets/s3_admin_secret + s3_api_key: + file: ./secrets/s3_api_key + s3_api_secret: + file: ./secrets/s3_api_secret + s3_reconciler_key: + file: ./secrets/s3_reconciler_key + s3_reconciler_secret: + file: ./secrets/s3_reconciler_secret + s3_config: + file: ./secrets/s3_config diff --git a/evidence/bucket-bootstrap-repeat.txt b/evidence/bucket-bootstrap-repeat.txt new file mode 100644 index 00000000..5ff110db --- /dev/null +++ b/evidence/bucket-bootstrap-repeat.txt @@ -0,0 +1,3 @@ + Container mtusi-lab4-bucket-bootstrap-run-d95f463bab66 Creating + Container mtusi-lab4-bucket-bootstrap-run-d95f463bab66 Created +Private bucket ready; static authenticated identities loaded from local secret config. diff --git a/evidence/build.txt b/evidence/build.txt new file mode 100644 index 00000000..ae9bb62f --- /dev/null +++ b/evidence/build.txt @@ -0,0 +1,55 @@ + Image mtusi-messenger:lab4 Building +#1 [internal] load local bake definitions +#1 reading from stdin 650B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 739B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 196B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 6.92kB done +#5 DONE 0.0s + +#6 [1/7] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 resolve docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 done +#6 DONE 0.0s + +#7 [2/7] WORKDIR /app +#7 CACHED + +#8 [3/7] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#8 CACHED + +#9 [4/7] COPY requirements.txt ./ +#9 CACHED + +#10 [5/7] RUN pip install --no-cache-dir -r requirements.txt +#10 CACHED + +#11 [6/7] COPY src/ ./src/ +#11 DONE 0.0s + +#12 [7/7] COPY migrations/ ./migrations/ +#12 DONE 0.0s + +#13 exporting to image +#13 exporting layers 0.0s done +#13 exporting manifest sha256:6cabe9729ea3e45195f623f07f0974303b8b392b2e4e6aac4a3c4bb565dd6a1a done +#13 exporting config sha256:84a7ceda5c86abe367437b43f6f54e6eb16a19abb99bfd2257771bb9119cf759 done +#13 exporting attestation manifest sha256:a206b14cc9c3bfd49e2bf15bb90189e58c9a89871e87fcb4454c483bdde60677 done +#13 exporting manifest list sha256:6de46cd4b5e20a5826fcdf2289f055877f477800dcc27100a470e1fb8c267001 done +#13 naming to docker.io/library/mtusi-messenger:lab4 done +#13 unpacking to docker.io/library/mtusi-messenger:lab4 0.0s done +#13 DONE 0.1s + +#14 resolving provenance for metadata file +#14 DONE 0.0s + Image mtusi-messenger:lab4 Built diff --git a/evidence/clean-clone.txt b/evidence/clean-clone.txt new file mode 100644 index 00000000..7467d6a5 --- /dev/null +++ b/evidence/clean-clone.txt @@ -0,0 +1,220 @@ +Cloning into '/Users/ssmayorova/Documents/Учеба/Высоконагруженные приложения/lab4/build/lab4-clean-6e826b67/repo'... +done. +Local configuration ready; no secrets printed. + Image mtusi-messenger:lab4 Building +#1 [internal] load local bake definitions +#1 reading from stdin 721B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 739B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 196B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 52.94kB done +#5 DONE 0.0s + +#6 [1/7] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 resolve docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 done +#6 DONE 0.0s + +#7 [3/7] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#7 CACHED + +#8 [4/7] COPY requirements.txt ./ +#8 CACHED + +#9 [2/7] WORKDIR /app +#9 CACHED + +#10 [5/7] RUN pip install --no-cache-dir -r requirements.txt +#10 CACHED + +#11 [6/7] COPY src/ ./src/ +#11 CACHED + +#12 [7/7] COPY migrations/ ./migrations/ +#12 CACHED + +#13 exporting to image +#13 exporting layers done +#13 exporting manifest sha256:b9111c9adf12479532b94464933e51b833be4486c559d60f233006a4f2d448a8 done +#13 exporting config sha256:e59e3410c92a9e2663734ff4a9c33edcc65414616230d01ede4285bc1ab1f973 done +#13 exporting attestation manifest sha256:9b07c898e2f46418c89c2514d79a08b01b37144849451e0704173ea0c7c5c8c2 done +#13 exporting manifest list sha256:6db63c0e6e0d2f8d4f650a7ed60998284a8e81d98bc294b41883cef612ba2bce done +#13 naming to docker.io/library/mtusi-messenger:lab4 done +#13 unpacking to docker.io/library/mtusi-messenger:lab4 done +#13 DONE 0.0s + +#14 resolving provenance for metadata file +#14 DONE 0.0s + Image mtusi-messenger:lab4 Built + Network lab4-clean-6e826b67_storage Creating + Network lab4-clean-6e826b67_storage Created + Network lab4-clean-6e826b67_backend Creating + Network lab4-clean-6e826b67_backend Created + Network lab4-clean-6e826b67_edge Creating + Network lab4-clean-6e826b67_edge Created + Volume lab4-clean-6e826b67_s3_data Creating + Volume lab4-clean-6e826b67_s3_data Created + Volume lab4-clean-6e826b67_postgres_data Creating + Volume lab4-clean-6e826b67_postgres_data Created + Container lab4-clean-6e826b67-s3-store-1 Creating + Container lab4-clean-6e826b67-redis-1 Creating + Container lab4-clean-6e826b67-postgres-1 Creating + Container lab4-clean-6e826b67-redis-1 Created + Container lab4-clean-6e826b67-s3-store-1 Created + Container lab4-clean-6e826b67-s3-1 Creating + Container lab4-clean-6e826b67-postgres-1 Created + Container lab4-clean-6e826b67-migrate-1 Creating + Container lab4-clean-6e826b67-s3-1 Created + Container lab4-clean-6e826b67-bucket-bootstrap-1 Creating + Container lab4-clean-6e826b67-migrate-1 Created + Container lab4-clean-6e826b67-bucket-bootstrap-1 Created + Container lab4-clean-6e826b67-api-2 Creating + Container lab4-clean-6e826b67-api-1 Creating + Container lab4-clean-6e826b67-api-1 Created + Container lab4-clean-6e826b67-api-2 Created + Container lab4-clean-6e826b67-lb-1 Creating + Container lab4-clean-6e826b67-lb-1 Created + Container lab4-clean-6e826b67-postgres-1 Starting + Container lab4-clean-6e826b67-s3-store-1 Starting + Container lab4-clean-6e826b67-redis-1 Starting + Container lab4-clean-6e826b67-s3-store-1 Started + Container lab4-clean-6e826b67-s3-store-1 Waiting + Container lab4-clean-6e826b67-postgres-1 Started + Container lab4-clean-6e826b67-postgres-1 Waiting + Container lab4-clean-6e826b67-redis-1 Started + Container lab4-clean-6e826b67-postgres-1 Healthy + Container lab4-clean-6e826b67-migrate-1 Starting + Container lab4-clean-6e826b67-migrate-1 Started + Container lab4-clean-6e826b67-s3-store-1 Healthy + Container lab4-clean-6e826b67-s3-1 Starting + Container lab4-clean-6e826b67-s3-1 Started + Container lab4-clean-6e826b67-s3-1 Waiting + Container lab4-clean-6e826b67-s3-1 Healthy + Container lab4-clean-6e826b67-bucket-bootstrap-1 Starting + Container lab4-clean-6e826b67-bucket-bootstrap-1 Started + Container lab4-clean-6e826b67-migrate-1 Waiting + Container lab4-clean-6e826b67-redis-1 Waiting + Container lab4-clean-6e826b67-bucket-bootstrap-1 Waiting + Container lab4-clean-6e826b67-migrate-1 Exited + Container lab4-clean-6e826b67-redis-1 Healthy + Container lab4-clean-6e826b67-bucket-bootstrap-1 Exited + Container lab4-clean-6e826b67-api-2 Starting + Container lab4-clean-6e826b67-api-2 Started + Container lab4-clean-6e826b67-api-1 Starting + Container lab4-clean-6e826b67-api-1 Started + Container lab4-clean-6e826b67-api-2 Waiting + Container lab4-clean-6e826b67-api-1 Waiting + Container lab4-clean-6e826b67-api-2 Healthy + Container lab4-clean-6e826b67-api-1 Healthy + Container lab4-clean-6e826b67-lb-1 Starting + Container lab4-clean-6e826b67-lb-1 Started + Container lab4-clean-6e826b67-api-2 Waiting + Container lab4-clean-6e826b67-api-1 Waiting + Container lab4-clean-6e826b67-lb-1 Waiting + Container lab4-clean-6e826b67-s3-1 Waiting + Container lab4-clean-6e826b67-bucket-bootstrap-1 Waiting + Container lab4-clean-6e826b67-postgres-1 Waiting + Container lab4-clean-6e826b67-migrate-1 Waiting + Container lab4-clean-6e826b67-s3-store-1 Waiting + Container lab4-clean-6e826b67-redis-1 Waiting + Container lab4-clean-6e826b67-bucket-bootstrap-1 Exited + Container lab4-clean-6e826b67-redis-1 Healthy + Container lab4-clean-6e826b67-s3-store-1 Healthy + Container lab4-clean-6e826b67-s3-1 Healthy + Container lab4-clean-6e826b67-postgres-1 Healthy + Container lab4-clean-6e826b67-migrate-1 Exited + Container lab4-clean-6e826b67-api-2 Healthy + Container lab4-clean-6e826b67-api-1 Healthy + Container lab4-clean-6e826b67-lb-1 Healthy +Ready through load balancer. +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +python3 tests/smoke.py --base-url "http://127.0.0.1:18090" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. +BASE_URL="http://127.0.0.1:18090" python3 -m unittest discover -s student-tests -p 'test_*.py' -v +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_idempotency_concurrent_replicas (test_api.APIContract) ... ok +test_idempotency_conflicting_race (test_api.APIContract) ... ok +test_idempotency_invalid_keys (test_api.APIContract) ... ok +test_idempotency_scope (test_api.APIContract) ... ok +test_idempotency_semantic_body (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_protocol_error_headers (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_nul_round_trip (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok +test_access_and_cross_chat (test_attachments.Attachments) ... ok +test_attachment_message_replay (test_attachments.Attachments) ... ok +test_bad_images_and_mime (test_attachments.Attachments) ... ok +test_binary_roundtrip_and_metadata (test_attachments.Attachments) ... ok +test_dangerous_names_and_query (test_attachments.Attachments) ... ok +test_valid_pixel_boundary (test_attachments.Attachments) ... ok + +---------------------------------------------------------------------- +Ran 21 tests in 5.458s + +OK +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:18090" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + Container lab4-clean-6e826b67-lb-1 Stopping + Container lab4-clean-6e826b67-lb-1 Stopped + Container lab4-clean-6e826b67-lb-1 Removing + Container lab4-clean-6e826b67-lb-1 Removed + Container lab4-clean-6e826b67-api-1 Stopping + Container lab4-clean-6e826b67-api-2 Stopping + Container lab4-clean-6e826b67-api-2 Stopped + Container lab4-clean-6e826b67-api-2 Removing + Container lab4-clean-6e826b67-api-2 Removed + Container lab4-clean-6e826b67-api-1 Stopped + Container lab4-clean-6e826b67-api-1 Removing + Container lab4-clean-6e826b67-api-1 Removed + Container lab4-clean-6e826b67-bucket-bootstrap-1 Stopping + Container lab4-clean-6e826b67-migrate-1 Stopping + Container lab4-clean-6e826b67-redis-1 Stopping + Container lab4-clean-6e826b67-bucket-bootstrap-1 Stopped + Container lab4-clean-6e826b67-bucket-bootstrap-1 Removing + Container lab4-clean-6e826b67-migrate-1 Stopped + Container lab4-clean-6e826b67-migrate-1 Removing + Container lab4-clean-6e826b67-migrate-1 Removed + Container lab4-clean-6e826b67-postgres-1 Stopping + Container lab4-clean-6e826b67-bucket-bootstrap-1 Removed + Container lab4-clean-6e826b67-s3-1 Stopping + Container lab4-clean-6e826b67-postgres-1 Stopped + Container lab4-clean-6e826b67-postgres-1 Removing + Container lab4-clean-6e826b67-postgres-1 Removed + Container lab4-clean-6e826b67-s3-1 Stopped + Container lab4-clean-6e826b67-s3-1 Removing + Container lab4-clean-6e826b67-s3-1 Removed + Container lab4-clean-6e826b67-s3-store-1 Stopping + Container lab4-clean-6e826b67-redis-1 Stopped + Container lab4-clean-6e826b67-redis-1 Removing + Container lab4-clean-6e826b67-redis-1 Removed + Container lab4-clean-6e826b67-s3-store-1 Stopped + Container lab4-clean-6e826b67-s3-store-1 Removing + Container lab4-clean-6e826b67-s3-store-1 Removed + Volume lab4-clean-6e826b67_postgres_data Removing + Network lab4-clean-6e826b67_storage Removing + Volume lab4-clean-6e826b67_s3_data Removing + Network lab4-clean-6e826b67_edge Removing + Network lab4-clean-6e826b67_backend Removing + Volume lab4-clean-6e826b67_postgres_data Removed + Volume lab4-clean-6e826b67_s3_data Removed + Network lab4-clean-6e826b67_storage Removed + Network lab4-clean-6e826b67_edge Removed + Network lab4-clean-6e826b67_backend Removed +PASS: clean clone, new credentials, empty PostgreSQL/S3 volumes, all 21 own tests, full restart and byte persistence. diff --git a/evidence/failover-recovery.txt b/evidence/failover-recovery.txt new file mode 100644 index 00000000..bfc5a1bc --- /dev/null +++ b/evidence/failover-recovery.txt @@ -0,0 +1,7 @@ + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Starting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy diff --git a/evidence/faults-initial-stderr.txt b/evidence/faults-initial-stderr.txt new file mode 100644 index 00000000..17c8ef76 --- /dev/null +++ b/evidence/faults-initial-stderr.txt @@ -0,0 +1,6 @@ +Traceback (most recent call last): + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/faults.py", line 125, in + main() + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/faults.py", line 78, in main + target = urlsplit(c.base_url) +AttributeError: 'Client' object has no attribute 'base_url' diff --git a/evidence/faults-second-stderr.txt b/evidence/faults-second-stderr.txt new file mode 100644 index 00000000..ebf0e41e --- /dev/null +++ b/evidence/faults-second-stderr.txt @@ -0,0 +1,12 @@ +Traceback (most recent call last): + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/faults.py", line 125, in + main() + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/faults.py", line 71, in main + c.message(alice, chat, 'write survives stop') + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/tests/client.py", line 183, in message + return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message') + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/tests/client.py", line 132, in call + check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status)) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/tests/client.py", line 24, in check + raise Failure(message) +client.Failure: POST /api/v1/chats/1b519f28-dbdb-4400-a923-073d940c8952/messages: expected [201], received 503 diff --git a/evidence/faults.json b/evidence/faults.json new file mode 100644 index 00000000..e2406322 --- /dev/null +++ b/evidence/faults.json @@ -0,0 +1,33 @@ +{ + "replicas": [ + { + "container": "bf045a5f2da07f277b60fa4218fad624b82fa3f05c3d4870d379eef6ceb5608a", + "uploader": "410dddf690ef439bbaf15193aa235099", + "downloader": "9e726cd692f7474f856feaf1ebea5577", + "confirmed_stopped": true, + "transient_write_failures": 0, + "recovery_seconds": 1.818, + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "exit_code": 0, + "oom_killed": false + }, + { + "container": "15c86e71f1c1c806a7b585fc3fc5c499f886408f75bc90defe3355c8377e49e8", + "uploader": "9e726cd692f7474f856feaf1ebea5577", + "downloader": "882125d9d2834b7aab25eb78c9f99a5e", + "confirmed_stopped": true, + "transient_write_failures": 0, + "recovery_seconds": 1.846, + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "exit_code": 0, + "oom_killed": false + } + ], + "interrupted_before_validation_leaves_no_intent": true, + "s3_outage": { + "ready": 503, + "content": 503, + "live": 200 + }, + "redis_outage_fallback": true +} diff --git a/evidence/final-tests.txt b/evidence/final-tests.txt new file mode 100644 index 00000000..f5cd996b --- /dev/null +++ b/evidence/final-tests.txt @@ -0,0 +1,31 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +python3 tests/smoke.py --base-url "http://127.0.0.1:18089" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. +BASE_URL="http://127.0.0.1:18089" python3 -m unittest discover -s student-tests -p 'test_*.py' -v +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_idempotency_concurrent_replicas (test_api.APIContract) ... ok +test_idempotency_conflicting_race (test_api.APIContract) ... ok +test_idempotency_invalid_keys (test_api.APIContract) ... ok +test_idempotency_scope (test_api.APIContract) ... ok +test_idempotency_semantic_body (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_protocol_error_headers (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_nul_round_trip (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok +test_access_and_cross_chat (test_attachments.Attachments) ... ok +test_attachment_message_replay (test_attachments.Attachments) ... ok +test_bad_images_and_mime (test_attachments.Attachments) ... ok +test_binary_roundtrip_and_metadata (test_attachments.Attachments) ... ok +test_dangerous_names_and_query (test_attachments.Attachments) ... ok +test_valid_pixel_boundary (test_attachments.Attachments) ... ok + +---------------------------------------------------------------------- +Ran 21 tests in 5.309s + +OK diff --git a/evidence/offline-final.txt b/evidence/offline-final.txt new file mode 100644 index 00000000..9a553306 --- /dev/null +++ b/evidence/offline-final.txt @@ -0,0 +1,2 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/openapi-validation.txt b/evidence/openapi-validation.txt new file mode 100644 index 00000000..248343ee --- /dev/null +++ b/evidence/openapi-validation.txt @@ -0,0 +1 @@ +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..b969d325 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:18089" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/public-failover.txt b/evidence/public-failover.txt new file mode 100644 index 00000000..f381bd88 --- /dev/null +++ b/evidence/public-failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:18089" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 116, "failed_attempts": 1, "first_recovery_seconds": 2.594} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/public-smoke.txt b/evidence/public-smoke.txt new file mode 100644 index 00000000..8305d3b8 --- /dev/null +++ b/evidence/public-smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://127.0.0.1:18089" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/rebuild-startup.txt b/evidence/rebuild-startup.txt new file mode 100644 index 00000000..36215156 --- /dev/null +++ b/evidence/rebuild-startup.txt @@ -0,0 +1,12 @@ + Container mtusi-lab4-api-1 Recreate + Container mtusi-lab4-api-2 Recreate + Container mtusi-lab4-api-2 Recreated + Container mtusi-lab4-api-1 Recreated + Container mtusi-lab4-api-1 Starting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Starting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-2 Healthy diff --git a/evidence/reconciler-default-stderr.txt b/evidence/reconciler-default-stderr.txt new file mode 100644 index 00000000..f7f344a4 --- /dev/null +++ b/evidence/reconciler-default-stderr.txt @@ -0,0 +1,2 @@ + Container mtusi-lab4-reconciler-run-e50d556374d7 Creating + Container mtusi-lab4-reconciler-run-e50d556374d7 Created diff --git a/evidence/reconciler-default.json b/evidence/reconciler-default.json new file mode 100644 index 00000000..4d02a2e7 --- /dev/null +++ b/evidence/reconciler-default.json @@ -0,0 +1,20 @@ +{ + "apply": false, + "grace_seconds": 3600, + "elapsed_seconds": 0.785007, + "counts": { + "metadata": 638, + "objects": 628, + "head_calls": 501, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 765, + "protected_ready": 501, + "protected_foreign_prefix": 0 + }, + "findings": [] +} diff --git a/evidence/reconciliation-stderr.txt b/evidence/reconciliation-stderr.txt new file mode 100644 index 00000000..ac275373 --- /dev/null +++ b/evidence/reconciliation-stderr.txt @@ -0,0 +1,2 @@ + Container mtusi-lab4-audit-run-9e5a49008d08 Creating + Container mtusi-lab4-audit-run-9e5a49008d08 Created diff --git a/evidence/reconciliation.json b/evidence/reconciliation.json new file mode 100644 index 00000000..00d6c3cf --- /dev/null +++ b/evidence/reconciliation.json @@ -0,0 +1,332 @@ +{ + "method": "real S3 and PostgreSQL, per-run fixed cutoff, synthetic data, 2s test grace; production >=3600s", + "checks": { + "dry_run_mutates_nothing": true, + "stale_intents_and_orphans_cleaned": true, + "fresh_and_locked_retained": true, + "used_ready_retained": true, + "missing_reference_retained": true, + "apply_repeat_idempotent": true, + "restore_after_sha256_verification": true + }, + "passes": [ + { + "name": "dry_run", + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.050187, + "counts": { + "metadata": 14, + "objects": 12, + "head_calls": 13, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 1, + "protected_fresh": 2, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [ + { + "kind": "stale_intent", + "id": "a41ec1bd-b7f2-443e-914e-98356fd98a7e" + }, + { + "kind": "missing_or_mismatched", + "id": "b38ed978-f5aa-420b-aa2d-c531364e477d" + }, + { + "kind": "stale_intent", + "id": "ecc30613-bc47-4469-9171-373cc9722ca5" + }, + { + "kind": "orphan", + "id": "f210e333-3f79-4f90-917a-643f33b36f06" + } + ] + }, + { + "name": "apply", + "apply": true, + "grace_seconds": 2, + "elapsed_seconds": 0.044472, + "counts": { + "metadata": 14, + "objects": 11, + "head_calls": 13, + "get_calls": 0, + "deleted_objects": 2, + "deleted_intents": 2, + "marked_missing": 1, + "restored": 0, + "skipped_locked": 1, + "protected_fresh": 2, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [ + { + "kind": "stale_intent", + "id": "a41ec1bd-b7f2-443e-914e-98356fd98a7e" + }, + { + "kind": "missing_or_mismatched", + "id": "b38ed978-f5aa-420b-aa2d-c531364e477d" + }, + { + "kind": "stale_intent", + "id": "ecc30613-bc47-4469-9171-373cc9722ca5" + }, + { + "kind": "orphan", + "id": "f210e333-3f79-4f90-917a-643f33b36f06" + } + ] + }, + { + "name": "repeat", + "apply": true, + "grace_seconds": 2, + "elapsed_seconds": 0.020893, + "counts": { + "metadata": 12, + "objects": 10, + "head_calls": 9, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 1, + "protected_fresh": 3, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "name": "recover", + "apply": true, + "grace_seconds": 2, + "elapsed_seconds": 0.025345, + "counts": { + "metadata": 12, + "objects": 11, + "head_calls": 11, + "get_calls": 1, + "deleted_objects": 0, + "deleted_intents": 1, + "marked_missing": 0, + "restored": 1, + "skipped_locked": 0, + "protected_fresh": 3, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [ + { + "kind": "stale_intent", + "id": "5ad3dfb9-96f5-4c1b-aee7-6fcd572b21ff" + }, + { + "kind": "recoverable", + "id": "b38ed978-f5aa-420b-aa2d-c531364e477d" + } + ] + } + ], + "scan_cost": [ + { + "repetition": 1, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.020025, + "counts": { + "metadata": 10, + "objects": 10, + "head_calls": 9, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 2, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 2, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.019561, + "counts": { + "metadata": 10, + "objects": 10, + "head_calls": 9, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 2, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 3, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.019272, + "counts": { + "metadata": 10, + "objects": 10, + "head_calls": 9, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 2, + "protected_ready": 9, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 1, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.127656, + "counts": { + "metadata": 110, + "objects": 110, + "head_calls": 109, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 102, + "protected_ready": 109, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 2, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.142166, + "counts": { + "metadata": 110, + "objects": 110, + "head_calls": 109, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 102, + "protected_ready": 109, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 3, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.136284, + "counts": { + "metadata": 110, + "objects": 110, + "head_calls": 109, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 102, + "protected_ready": 109, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 1, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.609772, + "counts": { + "metadata": 510, + "objects": 510, + "head_calls": 509, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 447, + "protected_ready": 509, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 2, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.583932, + "counts": { + "metadata": 510, + "objects": 510, + "head_calls": 510, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 229, + "protected_ready": 510, + "protected_foreign_prefix": 0 + }, + "findings": [] + }, + { + "repetition": 3, + "apply": false, + "grace_seconds": 2, + "elapsed_seconds": 0.603062, + "counts": { + "metadata": 510, + "objects": 510, + "head_calls": 510, + "get_calls": 0, + "deleted_objects": 0, + "deleted_intents": 0, + "marked_missing": 0, + "restored": 0, + "skipped_locked": 0, + "protected_fresh": 229, + "protected_ready": 510, + "protected_foreign_prefix": 0 + }, + "findings": [] + } + ] +} diff --git a/evidence/research.png b/evidence/research.png new file mode 100644 index 00000000..97349ae0 Binary files /dev/null and b/evidence/research.png differ diff --git a/evidence/research.svg b/evidence/research.svg new file mode 100644 index 00000000..158bfbe2 --- /dev/null +++ b/evidence/research.svg @@ -0,0 +1,2391 @@ + + + + + + + + 2026-10-27T13:12:03.290731 + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/evidence/restart-after.json b/evidence/restart-after.json new file mode 100644 index 00000000..b8f15252 --- /dev/null +++ b/evidence/restart-after.json @@ -0,0 +1,284 @@ +[ + { + "container": "9d2246f08a86e587ed23ea8ee7bcb63e82fd39dff9cf585e6989ab1820640262", + "name": "/mtusi-lab4-api-1", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:05:36.193553009Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "container": "fdc4019ab865a997e437ed6fb3affefd6f7004fa09287907bd2d142674ffd15d", + "name": "/mtusi-lab4-api-2", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:05:36.14388102Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "container": "c2faba3e2c41441c9a6c17abb0b6b0a2b643dae3c04272965e26ae6014d2da58", + "name": "/mtusi-lab4-bucket-bootstrap-1", + "service": "bucket-bootstrap", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T10:05:35.580074761Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_admin_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_admin_secret", + "name": null + } + ] + }, + { + "container": "0acb2abc71272c11cdf6677f4b6deacc61c9fb0bb1b413c56a9e1b19ef5390cc", + "name": "/mtusi-lab4-lb-1", + "service": "lb", + "image": "haproxy:3.0.5-alpine@sha256:43a2591a6a62a41dd22edb4452880e9c70de489fbd7c54ffb52d035cccbfe949", + "user": "haproxy", + "running": true, + "started_at": "2026-10-27T10:05:41.760433323Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 500000000, + "ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "18089" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "container": "5f6ad41d3c31bba6b9aad4f317144db9beadd9ddac68fafb91bbe6515a53a89b", + "name": "/mtusi-lab4-migrate-1", + "service": "migrate", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T10:05:35.080716141Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "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_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + } + ] + }, + { + "container": "659282c0c330639947545a49529899f3df40bfa59c6760487efe827dd4e5382c", + "name": "/mtusi-lab4-postgres-1", + "service": "postgres", + "image": "postgres:17.2-bookworm@sha256:3267c505060a0052e5aa6e5175a7b41ab6b04da2f8c4540fc6e98a37210aa2d3", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:27.807991429Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "5432/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + }, + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "mtusi-lab4_postgres_data" + } + ] + }, + { + "container": "0c92c3135de8112ca4f9d39f49c72bcf010bd29317b40a78b75ccf05283c53fa", + "name": "/mtusi-lab4-redis-1", + "service": "redis", + "image": "redis:7.4.1-bookworm@sha256:bb142a9c18ac18a16713c1491d779697b4e107c22a97266616099d288237ef47", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:27.855523719Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 250000000, + "ports": { + "6379/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "a871486fd792dc6774ac326c0f8acedca47e3a441e34042bed85d2241be43ba7" + } + ] + }, + { + "container": "8dd6d84d8b8c86b9101ba7c40a547ae5422b365543a33bcae493ed3e6740ed8e", + "name": "/mtusi-lab4-s3-1", + "service": "s3", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:27.820013219Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 500000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "14783007132b79e82933ac71a5d25b4559f3af285106fb42539e7967bef98ffa" + } + ] + }, + { + "container": "4f266847c0bb7c5c863260d12d7bd838accad03738de7333d366bb8739d2cf19", + "name": "/mtusi-lab4-s3-store-1", + "service": "s3-store", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:29.140086899Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "mtusi-lab4_s3_data" + } + ] + } +] diff --git a/evidence/restart-before.json b/evidence/restart-before.json new file mode 100644 index 00000000..7dd01206 --- /dev/null +++ b/evidence/restart-before.json @@ -0,0 +1,284 @@ +[ + { + "container": "9d2246f08a86e587ed23ea8ee7bcb63e82fd39dff9cf585e6989ab1820640262", + "name": "/mtusi-lab4-api-1", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:04:17.903587814Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "container": "fdc4019ab865a997e437ed6fb3affefd6f7004fa09287907bd2d142674ffd15d", + "name": "/mtusi-lab4-api-2", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:05:21.182107229Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "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_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + } + ] + }, + { + "container": "ac813039081b26304bffbeaf125695bddc4fa32ce4dcc67cfb068bfc2ba39fdd", + "name": "/mtusi-lab4-bucket-bootstrap-1", + "service": "bucket-bootstrap", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T09:58:48.19724977Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_admin_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_admin_secret", + "name": null + } + ] + }, + { + "container": "0acb2abc71272c11cdf6677f4b6deacc61c9fb0bb1b413c56a9e1b19ef5390cc", + "name": "/mtusi-lab4-lb-1", + "service": "lb", + "image": "haproxy:3.0.5-alpine@sha256:43a2591a6a62a41dd22edb4452880e9c70de489fbd7c54ffb52d035cccbfe949", + "user": "haproxy", + "running": true, + "started_at": "2026-10-27T09:58:54.973653506Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 500000000, + "ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "18089" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "container": "bafb9b9c7acb36922df0ae5172331cef431a8625b52ef566a7192df0e6a44e40", + "name": "/mtusi-lab4-migrate-1", + "service": "migrate", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T09:58:47.706874791Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "container": "659282c0c330639947545a49529899f3df40bfa59c6760487efe827dd4e5382c", + "name": "/mtusi-lab4-postgres-1", + "service": "postgres", + "image": "postgres:17.2-bookworm@sha256:3267c505060a0052e5aa6e5175a7b41ab6b04da2f8c4540fc6e98a37210aa2d3", + "user": "", + "running": true, + "started_at": "2026-10-27T09:58:40.73770571Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "5432/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "mtusi-lab4_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "container": "0c92c3135de8112ca4f9d39f49c72bcf010bd29317b40a78b75ccf05283c53fa", + "name": "/mtusi-lab4-redis-1", + "service": "redis", + "image": "redis:7.4.1-bookworm@sha256:bb142a9c18ac18a16713c1491d779697b4e107c22a97266616099d288237ef47", + "user": "", + "running": true, + "started_at": "2026-10-27T09:59:46.354988447Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 250000000, + "ports": { + "6379/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "a871486fd792dc6774ac326c0f8acedca47e3a441e34042bed85d2241be43ba7" + } + ] + }, + { + "container": "8dd6d84d8b8c86b9101ba7c40a547ae5422b365543a33bcae493ed3e6740ed8e", + "name": "/mtusi-lab4-s3-1", + "service": "s3", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T10:01:37.331049666Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 500000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "14783007132b79e82933ac71a5d25b4559f3af285106fb42539e7967bef98ffa" + }, + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + } + ] + }, + { + "container": "4f266847c0bb7c5c863260d12d7bd838accad03738de7333d366bb8739d2cf19", + "name": "/mtusi-lab4-s3-store-1", + "service": "s3-store", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T09:58:41.842064577Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "mtusi-lab4_s3_data" + } + ] + } +] diff --git a/evidence/runtime.json b/evidence/runtime.json new file mode 100644 index 00000000..826f4ef2 --- /dev/null +++ b/evidence/runtime.json @@ -0,0 +1,284 @@ +[ + { + "container": "bf045a5f2da07f277b60fa4218fad624b82fa3f05c3d4870d379eef6ceb5608a", + "name": "/mtusi-lab4-api-1", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:18:07.132235089Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "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_api_key", + "name": null + } + ] + }, + { + "container": "15c86e71f1c1c806a7b585fc3fc5c499f886408f75bc90defe3355c8377e49e8", + "name": "/mtusi-lab4-api-2", + "service": "api", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": true, + "started_at": "2026-10-27T10:18:14.915499757Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "8080/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "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_api_key", + "name": null + } + ] + }, + { + "container": "c2faba3e2c41441c9a6c17abb0b6b0a2b643dae3c04272965e26ae6014d2da58", + "name": "/mtusi-lab4-bucket-bootstrap-1", + "service": "bucket-bootstrap", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T10:05:35.580074761Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_admin_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_admin_secret", + "name": null + } + ] + }, + { + "container": "0acb2abc71272c11cdf6677f4b6deacc61c9fb0bb1b413c56a9e1b19ef5390cc", + "name": "/mtusi-lab4-lb-1", + "service": "lb", + "image": "haproxy:3.0.5-alpine@sha256:43a2591a6a62a41dd22edb4452880e9c70de489fbd7c54ffb52d035cccbfe949", + "user": "haproxy", + "running": true, + "started_at": "2026-10-27T10:05:41.760433323Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 500000000, + "ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "18089" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "container": "5f6ad41d3c31bba6b9aad4f317144db9beadd9ddac68fafb91bbe6515a53a89b", + "name": "/mtusi-lab4-migrate-1", + "service": "migrate", + "image": "mtusi-messenger:lab4", + "user": "10001:10001", + "running": false, + "started_at": "2026-10-27T10:05:35.080716141Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_key", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/s3_api_secret", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "container": "659282c0c330639947545a49529899f3df40bfa59c6760487efe827dd4e5382c", + "name": "/mtusi-lab4-postgres-1", + "service": "postgres", + "image": "postgres:17.2-bookworm@sha256:3267c505060a0052e5aa6e5175a7b41ab6b04da2f8c4540fc6e98a37210aa2d3", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:27.807991429Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "5432/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "mtusi-lab4_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "container": "0c92c3135de8112ca4f9d39f49c72bcf010bd29317b40a78b75ccf05283c53fa", + "name": "/mtusi-lab4-redis-1", + "service": "redis", + "image": "redis:7.4.1-bookworm@sha256:bb142a9c18ac18a16713c1491d779697b4e107c22a97266616099d288237ef47", + "user": "", + "running": true, + "started_at": "2026-10-27T10:18:25.256591097Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 67108864, + "nano_cpus": 250000000, + "ports": { + "6379/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "a871486fd792dc6774ac326c0f8acedca47e3a441e34042bed85d2241be43ba7" + } + ] + }, + { + "container": "8dd6d84d8b8c86b9101ba7c40a547ae5422b365543a33bcae493ed3e6740ed8e", + "name": "/mtusi-lab4-s3-1", + "service": "s3", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T10:18:22.341659035Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 500000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/s3_config", + "name": null + }, + { + "type": "volume", + "destination": "/data", + "name": "14783007132b79e82933ac71a5d25b4559f3af285106fb42539e7967bef98ffa" + } + ] + }, + { + "container": "4f266847c0bb7c5c863260d12d7bd838accad03738de7333d366bb8739d2cf19", + "name": "/mtusi-lab4-s3-store-1", + "service": "s3-store", + "image": "chrislusf/seaweedfs:4.51@sha256:f7a49e3f3972ebed5be2ed4befa4a0c68eb7bdddba6e692d42729ceb545134b8", + "user": "", + "running": true, + "started_at": "2026-10-27T10:05:29.140086899Z", + "exit_code": 0, + "oom_killed": false, + "memory_limit": 536870912, + "nano_cpus": 1000000000, + "ports": { + "18080/tcp": null, + "18888/tcp": null, + "19333/tcp": null, + "7333/tcp": null, + "8080/tcp": null, + "8333/tcp": null, + "8888/tcp": null, + "9333/tcp": null + }, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "mtusi-lab4_s3_data" + } + ] + } +] diff --git a/evidence/slow-upload-initial-stderr.txt b/evidence/slow-upload-initial-stderr.txt new file mode 100644 index 00000000..d4282e18 --- /dev/null +++ b/evidence/slow-upload-initial-stderr.txt @@ -0,0 +1,4 @@ +Traceback (most recent call last): + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/slow_upload.py", line 31, in + assert b'X-Instance-Id:' in response and b'X-Request-Id:' in response +AssertionError diff --git a/evidence/slow-upload.json b/evidence/slow-upload.json new file mode 100644 index 00000000..74c82992 --- /dev/null +++ b/evidence/slow-upload.json @@ -0,0 +1 @@ +{"trickle_seconds": 14.862, "http_status": 400, "absolute_deadline_enforced": true} diff --git a/evidence/startup.txt b/evidence/startup.txt new file mode 100644 index 00000000..985af07d --- /dev/null +++ b/evidence/startup.txt @@ -0,0 +1,64 @@ + Container mtusi-lab4-migrate-1 Creating + Container mtusi-lab4-s3-1 Creating + Container mtusi-lab4-migrate-1 Created + Container mtusi-lab4-s3-1 Created + Container mtusi-lab4-bucket-bootstrap-1 Creating + Container mtusi-lab4-bucket-bootstrap-1 Created + Container mtusi-lab4-api-2 Creating + Container mtusi-lab4-api-1 Creating + Container mtusi-lab4-api-2 Created + Container mtusi-lab4-api-1 Created + Container mtusi-lab4-lb-1 Creating + Container mtusi-lab4-lb-1 Created + Container mtusi-lab4-postgres-1 Starting + Container mtusi-lab4-redis-1 Starting + Container mtusi-lab4-s3-store-1 Starting + Container mtusi-lab4-postgres-1 Started + Container mtusi-lab4-postgres-1 Waiting + Container mtusi-lab4-s3-store-1 Started + Container mtusi-lab4-s3-store-1 Waiting + Container mtusi-lab4-redis-1 Started + Container mtusi-lab4-postgres-1 Healthy + Container mtusi-lab4-migrate-1 Starting + Container mtusi-lab4-migrate-1 Started + Container mtusi-lab4-s3-store-1 Healthy + Container mtusi-lab4-s3-1 Starting + Container mtusi-lab4-s3-1 Started + Container mtusi-lab4-s3-1 Waiting + Container mtusi-lab4-s3-1 Healthy + Container mtusi-lab4-bucket-bootstrap-1 Starting + Container mtusi-lab4-bucket-bootstrap-1 Started + Container mtusi-lab4-redis-1 Waiting + Container mtusi-lab4-bucket-bootstrap-1 Waiting + Container mtusi-lab4-migrate-1 Waiting + Container mtusi-lab4-migrate-1 Exited + Container mtusi-lab4-redis-1 Healthy + Container mtusi-lab4-bucket-bootstrap-1 Exited + Container mtusi-lab4-api-2 Starting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Starting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-lb-1 Starting + Container mtusi-lab4-lb-1 Started + Container mtusi-lab4-postgres-1 Waiting + Container mtusi-lab4-redis-1 Waiting + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-s3-1 Waiting + Container mtusi-lab4-lb-1 Waiting + Container mtusi-lab4-bucket-bootstrap-1 Waiting + Container mtusi-lab4-migrate-1 Waiting + Container mtusi-lab4-s3-store-1 Waiting + Container mtusi-lab4-postgres-1 Healthy + Container mtusi-lab4-s3-store-1 Healthy + Container mtusi-lab4-migrate-1 Exited + Container mtusi-lab4-bucket-bootstrap-1 Exited + Container mtusi-lab4-redis-1 Healthy + Container mtusi-lab4-s3-1 Healthy + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-lb-1 Healthy diff --git a/evidence/stopped-instances.jsonl b/evidence/stopped-instances.jsonl new file mode 100644 index 00000000..4e0801af --- /dev/null +++ b/evidence/stopped-instances.jsonl @@ -0,0 +1 @@ +{"instance": "9f224351e7f146f9be39061b5e3250d9", "container": "fdc4019ab865a997e437ed6fb3affefd6f7004fa09287907bd2d142674ffd15d", "running": false, "exit_code": 0, "seconds": 0.634} diff --git a/evidence/storage-security-stderr.txt b/evidence/storage-security-stderr.txt new file mode 100644 index 00000000..64fe620c --- /dev/null +++ b/evidence/storage-security-stderr.txt @@ -0,0 +1,2 @@ + Container mtusi-lab4-audit-run-0bd7cfd77571 Creating + Container mtusi-lab4-audit-run-0bd7cfd77571 Created diff --git a/evidence/storage-security.json b/evidence/storage-security.json new file mode 100644 index 00000000..0905e7b6 --- /dev/null +++ b/evidence/storage-security.json @@ -0,0 +1,14 @@ +{ + "anonymous_statuses": [ + 403, + 403 + ], + "api_credentials_denied": [ + 403, + 403, + 403 + ], + "internal_filer_and_admin_inaccessible": true, + "jpeg_byte_roundtrip": true, + "damaged_jpeg_415": true +} diff --git a/evidence/student-api.txt b/evidence/student-api.txt new file mode 100644 index 00000000..1afc0d90 --- /dev/null +++ b/evidence/student-api.txt @@ -0,0 +1,20 @@ +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_idempotency_concurrent_replicas (test_api.APIContract) ... ok +test_idempotency_conflicting_race (test_api.APIContract) ... ok +test_idempotency_invalid_keys (test_api.APIContract) ... ok +test_idempotency_scope (test_api.APIContract) ... ok +test_idempotency_semantic_body (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_protocol_error_headers (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_nul_round_trip (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok + +---------------------------------------------------------------------- +Ran 15 tests in 3.870s + +OK diff --git a/evidence/student-attachments.txt b/evidence/student-attachments.txt new file mode 100644 index 00000000..2e259411 --- /dev/null +++ b/evidence/student-attachments.txt @@ -0,0 +1,11 @@ +test_access_and_cross_chat (test_attachments.Attachments) ... ok +test_attachment_message_replay (test_attachments.Attachments) ... ok +test_bad_images_and_mime (test_attachments.Attachments) ... ok +test_binary_roundtrip_and_metadata (test_attachments.Attachments) ... ok +test_dangerous_names_and_query (test_attachments.Attachments) ... ok +test_valid_pixel_boundary (test_attachments.Attachments) ... ok + +---------------------------------------------------------------------- +Ran 6 tests in 1.497s + +OK diff --git a/evidence/uploads-initial-s3-state.json b/evidence/uploads-initial-s3-state.json new file mode 100644 index 00000000..074d2ec0 --- /dev/null +++ b/evidence/uploads-initial-s3-state.json @@ -0,0 +1 @@ +{"Status":"exited","Running":false,"Paused":false,"Restarting":false,"OOMKilled":true,"Dead":false,"Pid":0,"ExitCode":137,"Error":"","StartedAt":"2026-10-27T09:59:43.487064787Z","FinishedAt":"2026-10-27T10:00:07.995173442Z","Health":{"Status":"unhealthy","FailingStreak":0,"Log":[{"Start":"2026-10-27T12:59:57.792402761+03:00","End":"2026-10-27T12:59:57.867972258+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T12:59:59.869289789+03:00","End":"2026-10-27T12:59:59.912601342+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:00:01.913345235+03:00","End":"2026-10-27T13:00:01.947306663+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:00:03.948017117+03:00","End":"2026-10-27T13:00:03.977837726+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:00:05.979365523+03:00","End":"2026-10-27T13:00:06.022467377+03:00","ExitCode":0,"Output":""}]}} diff --git a/evidence/uploads-initial-stderr.txt b/evidence/uploads-initial-stderr.txt new file mode 100644 index 00000000..214bb3d2 --- /dev/null +++ b/evidence/uploads-initial-stderr.txt @@ -0,0 +1,41 @@ + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting +container mtusi-lab4-api-2 is unhealthy +Traceback (most recent call last): + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 75, in + main() + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 36, in main + docker('compose', 'up', '-d', '--wait', '--no-deps', 'api') + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 18, in docker + return subprocess.check_output(['docker', *args], text=True).strip() + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 424, in check_output + return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 528, in run + raise CalledProcessError(retcode, process.args, +subprocess.CalledProcessError: Command '['docker', 'compose', 'up', '-d', '--wait', '--no-deps', 'api']' returned non-zero exit status 1. diff --git a/evidence/uploads-second-api-state.json b/evidence/uploads-second-api-state.json new file mode 100644 index 00000000..873f3ac2 --- /dev/null +++ b/evidence/uploads-second-api-state.json @@ -0,0 +1 @@ +{"Status":"exited","Running":false,"Paused":false,"Restarting":false,"OOMKilled":true,"Dead":false,"Pid":0,"ExitCode":137,"Error":"","StartedAt":"2026-10-27T10:02:01.511397321Z","FinishedAt":"2026-10-27T10:02:10.139299032Z","Health":{"Status":"unhealthy","FailingStreak":0,"Log":[{"Start":"2026-10-27T13:01:37.254367338+03:00","End":"2026-10-27T13:01:37.434073622+03:00","ExitCode":1,"Output":"Traceback (most recent call last):\n File \"\", line 1, in \n File \"/usr/local/lib/python3.12/urllib/request.py\", line 215, in urlopen\n return opener.open(url, data, timeout)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/urllib/request.py\", line 521, in open\n response = meth(req, response)\n ^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/urllib/request.py\", line 630, in http_response\n response = self.parent.error(\n ^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/urllib/request.py\", line 559, in error\n return self._call_chain(*args)\n ^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/urllib/request.py\", line 492, in _call_chain\n result = func(*args)\n ^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/urllib/request.py\", line 639, in http_error_default\n raise HTTPError(req.full_url, code, msg, hdrs, fp)\nurllib.error.HTTPError: HTTP Error 503: Service Unavailable\n"},{"Start":"2026-10-27T13:01:46.337329806+03:00","End":"2026-10-27T13:01:46.548565827+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:01:55.696703643+03:00","End":"2026-10-27T13:01:55.854363477+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:02:00.855578299+03:00","End":"2026-10-27T13:02:01.017461147+03:00","ExitCode":0,"Output":""},{"Start":"2026-10-27T13:02:06.61049249+03:00","End":"2026-10-27T13:02:06.793434431+03:00","ExitCode":0,"Output":""}]}} diff --git a/evidence/uploads-second-stderr.txt b/evidence/uploads-second-stderr.txt new file mode 100644 index 00000000..7b88cddb --- /dev/null +++ b/evidence/uploads-second-stderr.txt @@ -0,0 +1,70 @@ + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-1 Healthy +Error response from daemon: container f723dc1c71bdaa44acae07bc4e2692a6eff5c1699db47883ac50d4fd85f164df is not running +Exception in thread Thread-3: +Traceback (most recent call last): + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner + self.run() + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run + self._target(*self._args, **self._kwargs) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 45, in sample + samples.append({'seconds': round(time.monotonic() - started, 4), 'memory': {v: memory(v) for v in containers}}) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 45, in + samples.append({'seconds': round(time.monotonic() - started, 4), 'memory': {v: memory(v) for v in containers}}) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 23, in memory + return json.loads(docker('exec', container, 'python', '-c', code)) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 18, in docker + return subprocess.check_output(['docker', *args], text=True).strip() + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 424, in check_output + return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 528, in run + raise CalledProcessError(retcode, process.args, +subprocess.CalledProcessError: Command '['docker', 'exec', 'f723dc1c71bdaa44acae07bc4e2692a6eff5c1699db47883ac50d4fd85f164df', 'python', '-c', "import json; from pathlib import Path; p=Path('/sys/fs/cgroup'); print(json.dumps({k:(p/k).read_text().strip() for k in ['memory.current','memory.peak','memory.events']}))"]' returned non-zero exit status 1. +Traceback (most recent call last): + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 75, in + main() + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 63, in main + results = list(executor.map(upload, range(10))) + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 608, in result_iterator + yield fs.pop().result() + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 445, in result + return self.__get_result() + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result + raise self._exception + File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 52, in run + result = self.fn(*self.args, **self.kwargs) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/student-tests/uploads.py", line 53, in upload + response = Client(timeout=30).call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=concurrent.png', [201, 503], actor=alice, raw=content, headers={'Content-Type': 'image/png'}) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/tests/client.py", line 132, in call + check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status)) + File "/Users/ssmayorova/Documents/учеба/Высоконагруженные приложения/lab4/tests/client.py", line 24, in check + raise Failure(message) +client.Failure: POST /api/v1/chats/d5f3faf9-30c1-4c21-9c11-c90c8f661698/attachments: expected [201, 503], received 502 diff --git a/evidence/uploads-stderr.txt b/evidence/uploads-stderr.txt new file mode 100644 index 00000000..a435ba09 --- /dev/null +++ b/evidence/uploads-stderr.txt @@ -0,0 +1,30 @@ + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-2 Healthy + Container mtusi-lab4-api-2 Restarting + Container mtusi-lab4-api-1 Restarting + Container mtusi-lab4-api-2 Started + Container mtusi-lab4-api-1 Started + Container mtusi-lab4-api-1 Running + Container mtusi-lab4-api-2 Running + Container mtusi-lab4-api-1 Waiting + Container mtusi-lab4-api-2 Waiting + Container mtusi-lab4-api-1 Healthy + Container mtusi-lab4-api-2 Healthy diff --git a/evidence/uploads.json b/evidence/uploads.json new file mode 100644 index 00000000..5617f61b --- /dev/null +++ b/evidence/uploads.json @@ -0,0 +1,475 @@ +{ + "memory_source": "Linux cgroup v2: memory.current, memory.peak (lifetime peak; API restarted before each case), memory.events; samples include tmpfs and kernel charges, not Python heap alone", + "experiments": [ + { + "width": 420, + "height": 420, + "bytes": 529848, + "pixels": 176400, + "clients": 10, + "before": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "62935040", + "memory.peak": "70307840", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 821121\nuser_usec 772582\nsystem_usec 48539\ncore_sched.force_idle_usec 0\nnr_periods 34\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "62951424", + "memory.peak": "70447104", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 824208\nuser_usec 761585\nsystem_usec 62623\ncore_sched.force_idle_usec 0\nnr_periods 31\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + } + }, + "after": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "64696320", + "memory.peak": "70307840", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 997847\nuser_usec 923635\nsystem_usec 74211\ncore_sched.force_idle_usec 0\nnr_periods 38\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "65744896", + "memory.peak": "70447104", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 993358\nuser_usec 904791\nsystem_usec 88566\ncore_sched.force_idle_usec 0\nnr_periods 35\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + } + }, + "samples": [ + { + "seconds": 0.0001, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "66539520", + "memory.peak": "70307840", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 926703\nuser_usec 862307\nsystem_usec 64396\ncore_sched.force_idle_usec 0\nnr_periods 36\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "65880064", + "memory.peak": "70447104", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 935083\nuser_usec 852368\nsystem_usec 82715\ncore_sched.force_idle_usec 0\nnr_periods 33\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + } + } + } + ], + "results": [ + { + "index": 0, + "status": 201, + "instance": "ec1521f25fc94654a795aa7079670d75", + "elapsed_seconds": 0.141092 + }, + { + "index": 1, + "status": 201, + "instance": "b2ebf9eabec541fabee2a014a635254c", + "elapsed_seconds": 0.147615 + }, + { + "index": 2, + "status": 201, + "instance": "b2ebf9eabec541fabee2a014a635254c", + "elapsed_seconds": 0.137885 + }, + { + "index": 3, + "status": 201, + "instance": "b2ebf9eabec541fabee2a014a635254c", + "elapsed_seconds": 0.135644 + }, + { + "index": 4, + "status": 201, + "instance": "ec1521f25fc94654a795aa7079670d75", + "elapsed_seconds": 0.14879 + }, + { + "index": 5, + "status": 201, + "instance": "b2ebf9eabec541fabee2a014a635254c", + "elapsed_seconds": 0.135081 + }, + { + "index": 6, + "status": 201, + "instance": "b2ebf9eabec541fabee2a014a635254c", + "elapsed_seconds": 0.146117 + }, + { + "index": 7, + "status": 201, + "instance": "ec1521f25fc94654a795aa7079670d75", + "elapsed_seconds": 0.149378 + }, + { + "index": 8, + "status": 201, + "instance": "ec1521f25fc94654a795aa7079670d75", + "elapsed_seconds": 0.137382 + }, + { + "index": 9, + "status": 201, + "instance": "ec1521f25fc94654a795aa7079670d75", + "elapsed_seconds": 0.141443 + } + ], + "seconds": 0.386745 + }, + { + "width": 1800, + "height": 1400, + "bytes": 7563763, + "pixels": 2520000, + "clients": 10, + "before": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "63131648", + "memory.peak": "70373376", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 734874\nuser_usec 692651\nsystem_usec 42222\ncore_sched.force_idle_usec 0\nnr_periods 30\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "63029248", + "memory.peak": "70451200", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 760554\nuser_usec 710452\nsystem_usec 50102\ncore_sched.force_idle_usec 0\nnr_periods 34\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + } + }, + "after": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "95686656", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1473238\nuser_usec 1280945\nsystem_usec 192292\ncore_sched.force_idle_usec 0\nnr_periods 51\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "93851648", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1478013\nuser_usec 1302398\nsystem_usec 175615\ncore_sched.force_idle_usec 0\nnr_periods 55\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + }, + "samples": [ + { + "seconds": 0.0001, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "78561280", + "memory.peak": "79040512", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 843102\nuser_usec 785823\nsystem_usec 57278\ncore_sched.force_idle_usec 0\nnr_periods 32\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "132190208", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 983686\nuser_usec 860092\nsystem_usec 123593\ncore_sched.force_idle_usec 0\nnr_periods 38\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 0.3939, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "133455872", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1038539\nuser_usec 903599\nsystem_usec 134940\ncore_sched.force_idle_usec 0\nnr_periods 36\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "131489792", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1050472\nuser_usec 923848\nsystem_usec 126623\ncore_sched.force_idle_usec 0\nnr_periods 40\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 0.6506, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "118374400", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1111506\nuser_usec 964571\nsystem_usec 146934\ncore_sched.force_idle_usec 0\nnr_periods 38\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "123994112", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1127383\nuser_usec 991773\nsystem_usec 135609\ncore_sched.force_idle_usec 0\nnr_periods 43\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 0.8914, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "110870528", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1184065\nuser_usec 1026924\nsystem_usec 157141\ncore_sched.force_idle_usec 0\nnr_periods 41\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "108843008", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1201834\nuser_usec 1054525\nsystem_usec 147308\ncore_sched.force_idle_usec 0\nnr_periods 45\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 1.151, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "103288832", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1261279\nuser_usec 1093241\nsystem_usec 168037\ncore_sched.force_idle_usec 0\nnr_periods 44\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "93851648", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1283831\nuser_usec 1127662\nsystem_usec 156169\ncore_sched.force_idle_usec 0\nnr_periods 48\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 1.4121, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "103329792", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1332057\nuser_usec 1152319\nsystem_usec 179737\ncore_sched.force_idle_usec 0\nnr_periods 46\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "93888512", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1353665\nuser_usec 1189153\nsystem_usec 164511\ncore_sched.force_idle_usec 0\nnr_periods 51\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 1.6763, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "95698944", + "memory.peak": "133599232", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1412922\nuser_usec 1223799\nsystem_usec 189123\ncore_sched.force_idle_usec 0\nnr_periods 49\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "93876224", + "memory.peak": "133210112", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1419903\nuser_usec 1248255\nsystem_usec 171647\ncore_sched.force_idle_usec 0\nnr_periods 53\nnr_throttled 1\nthrottled_usec 90676\nnr_bursts 0\nburst_usec 0" + } + } + } + ], + "results": [ + { + "index": 0, + "status": 201, + "instance": "c6e48dd0a12748459220da96a8f39c1b", + "elapsed_seconds": 1.604444 + }, + { + "index": 1, + "status": 201, + "instance": "0e06ab6fa85d49b18abd412444127bd4", + "elapsed_seconds": 1.6491 + }, + { + "index": 2, + "status": 201, + "instance": "0e06ab6fa85d49b18abd412444127bd4", + "elapsed_seconds": 1.710992 + }, + { + "index": 3, + "status": 201, + "instance": "c6e48dd0a12748459220da96a8f39c1b", + "elapsed_seconds": 1.733797 + }, + { + "index": 4, + "status": 201, + "instance": "0e06ab6fa85d49b18abd412444127bd4", + "elapsed_seconds": 1.731711 + }, + { + "index": 5, + "status": 201, + "instance": "c6e48dd0a12748459220da96a8f39c1b", + "elapsed_seconds": 1.734976 + }, + { + "index": 6, + "status": 201, + "instance": "c6e48dd0a12748459220da96a8f39c1b", + "elapsed_seconds": 1.743231 + }, + { + "index": 7, + "status": 201, + "instance": "c6e48dd0a12748459220da96a8f39c1b", + "elapsed_seconds": 1.635536 + }, + { + "index": 8, + "status": 201, + "instance": "0e06ab6fa85d49b18abd412444127bd4", + "elapsed_seconds": 1.696447 + }, + { + "index": 9, + "status": 201, + "instance": "0e06ab6fa85d49b18abd412444127bd4", + "elapsed_seconds": 1.712033 + } + ], + "seconds": 2.042275 + }, + { + "width": 10000, + "height": 4000, + "bytes": 533859, + "pixels": 40000000, + "clients": 10, + "before": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "63111168", + "memory.peak": "70426624", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 728470\nuser_usec 673510\nsystem_usec 54960\ncore_sched.force_idle_usec 0\nnr_periods 35\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "64720896", + "memory.peak": "71909376", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 750816\nuser_usec 701763\nsystem_usec 49053\ncore_sched.force_idle_usec 0\nnr_periods 32\nnr_throttled 0\nthrottled_usec 0\nnr_bursts 0\nburst_usec 0" + } + }, + "after": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "182140928", + "memory.peak": "342212608", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1374606\nuser_usec 1178092\nsystem_usec 196514\ncore_sched.force_idle_usec 0\nnr_periods 44\nnr_throttled 5\nthrottled_usec 158446\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "182050816", + "memory.peak": "335065088", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1411374\nuser_usec 1208889\nsystem_usec 202484\ncore_sched.force_idle_usec 0\nnr_periods 41\nnr_throttled 4\nthrottled_usec 226190\nnr_bursts 0\nburst_usec 0" + } + }, + "samples": [ + { + "seconds": 0.0001, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "151093248", + "memory.peak": "151236608", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 841782\nuser_usec 762614\nsystem_usec 79167\ncore_sched.force_idle_usec 0\nnr_periods 38\nnr_throttled 1\nthrottled_usec 52104\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "297021440", + "memory.peak": "297283584", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1030597\nuser_usec 900395\nsystem_usec 130201\ncore_sched.force_idle_usec 0\nnr_periods 35\nnr_throttled 1\nthrottled_usec 108977\nnr_bursts 0\nburst_usec 0" + } + } + }, + { + "seconds": 0.3807, + "memory": { + "e9964da9119aeac107c2a641d7bdbbfc948f888369ceb25a03e8628011999365": { + "memory.current": "225230848", + "memory.peak": "342212608", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1266794\nuser_usec 1077023\nsystem_usec 189770\ncore_sched.force_idle_usec 0\nnr_periods 42\nnr_throttled 5\nthrottled_usec 158446\nnr_bursts 0\nburst_usec 0" + }, + "79f0e5e5984ea9aa66a933bfea7ff32784a9ee2b6ff50efaf9d862ddc01162d4": { + "memory.current": "182067200", + "memory.peak": "335065088", + "memory.events": "low 0\nhigh 0\nmax 0\noom 0\noom_kill 0\noom_group_kill 0", + "cpu.stat": "usage_usec 1351217\nuser_usec 1151890\nsystem_usec 199327\ncore_sched.force_idle_usec 0\nnr_periods 39\nnr_throttled 4\nthrottled_usec 226190\nnr_bursts 0\nburst_usec 0" + } + } + } + ], + "results": [ + { + "index": 0, + "status": 201, + "instance": "759bb767d4ad47d186953da4bd2edb23", + "elapsed_seconds": 0.370876 + }, + { + "index": 1, + "status": 201, + "instance": "53156426e9ba49a6a2919f02c96969ac", + "elapsed_seconds": 0.53464 + }, + { + "index": 2, + "status": 201, + "instance": "53156426e9ba49a6a2919f02c96969ac", + "elapsed_seconds": 0.351175 + }, + { + "index": 3, + "status": 201, + "instance": "53156426e9ba49a6a2919f02c96969ac", + "elapsed_seconds": 0.398946 + }, + { + "index": 4, + "status": 201, + "instance": "759bb767d4ad47d186953da4bd2edb23", + "elapsed_seconds": 0.598276 + }, + { + "index": 5, + "status": 201, + "instance": "53156426e9ba49a6a2919f02c96969ac", + "elapsed_seconds": 0.230657 + }, + { + "index": 6, + "status": 201, + "instance": "759bb767d4ad47d186953da4bd2edb23", + "elapsed_seconds": 0.536612 + }, + { + "index": 7, + "status": 201, + "instance": "759bb767d4ad47d186953da4bd2edb23", + "elapsed_seconds": 0.43354 + }, + { + "index": 8, + "status": 201, + "instance": "53156426e9ba49a6a2919f02c96969ac", + "elapsed_seconds": 0.602782 + }, + { + "index": 9, + "status": 201, + "instance": "759bb767d4ad47d186953da4bd2edb23", + "elapsed_seconds": 0.231471 + } + ], + "seconds": 0.831182 + } + ] +} diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..5495e1e8 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,46 @@ +global + maxconn 1024 + nbthread 1 + hard-stop-after 10s + +defaults + mode http + timeout connect 500ms + timeout check 500ms + timeout queue 1s + timeout client 30s + timeout server 30s + timeout http-request 3s + retries 2 + retry-on conn-failure + option http-server-close + +resolvers docker + nameserver docker_dns 127.0.0.11:53 + accepted_payload_size 8192 + resolve_retries 2 + timeout resolve 1s + timeout retry 1s + hold valid 1s + hold nx 1s + hold refused 1s + hold timeout 1s + hold other 1s + hold obsolete 1s + +frontend public + bind :8080 + default_backend replicas + +backend replicas + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api + http-check expect status 200 + default-server inter 1s fall 2 rise 2 + server-template api 1-4 api:8080 check resolvers docker init-addr none resolve-prefer ipv4 + +listen metrics + bind :8404 + stats enable + stats uri /stats diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 00000000..eeac3c14 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,42 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY + , username TEXT NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$') + , display_name TEXT NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100) + , password_hash TEXT NOT NULL + , created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() +) +; +CREATE TABLE chats ( + id UUID PRIMARY KEY + , title TEXT NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100) + , created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() + , last_message_at TIMESTAMPTZ +) +; +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) + , user_id UUID NOT NULL REFERENCES users(id) + , PRIMARY KEY (chat_id, user_id) +) +; +CREATE INDEX chat_members_user ON chat_members (user_id, chat_id) +; +CREATE TABLE messages ( + id UUID PRIMARY KEY + , chat_id UUID NOT NULL REFERENCES chats(id) + , sender_id UUID NOT NULL REFERENCES users(id) + , text TEXT NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000) + , created_at TIMESTAMPTZ NOT NULL + , FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) +) +; +CREATE INDEX messages_history ON messages (chat_id, created_at, id) +; +CREATE TABLE sessions ( + token_hash TEXT PRIMARY KEY + , user_id UUID NOT NULL REFERENCES users(id) + , expires_at TIMESTAMPTZ NOT NULL +) +; +CREATE INDEX sessions_expiry ON sessions (expires_at) +; diff --git a/migrations/002_unicode_nul.sql b/migrations/002_unicode_nul.sql new file mode 100644 index 00000000..224cda0d --- /dev/null +++ b/migrations/002_unicode_nul.sql @@ -0,0 +1,17 @@ +-- PostgreSQL TEXT cannot contain U+0000. Store validated UTF-8 losslessly in BYTEA. +-- API validation keeps the contract's code-point limits; these bounds cap bytes. +ALTER TABLE users + DROP CONSTRAINT users_display_name_check + , ALTER COLUMN display_name TYPE BYTEA USING convert_to(display_name, 'UTF8') + , ADD CONSTRAINT users_display_name_bytes CHECK (octet_length(display_name) BETWEEN 1 AND 400) +; +ALTER TABLE chats + DROP CONSTRAINT chats_title_check + , ALTER COLUMN title TYPE BYTEA USING convert_to(title, 'UTF8') + , ADD CONSTRAINT chats_title_bytes CHECK (octet_length(title) BETWEEN 1 AND 400) +; +ALTER TABLE messages + DROP CONSTRAINT messages_text_check + , ALTER COLUMN text TYPE BYTEA USING convert_to(text, 'UTF8') + , ADD CONSTRAINT messages_text_bytes CHECK (octet_length(text) BETWEEN 1 AND 16000) +; diff --git a/migrations/003_idempotency.sql b/migrations/003_idempotency.sql new file mode 100644 index 00000000..3ca53a7e --- /dev/null +++ b/migrations/003_idempotency.sql @@ -0,0 +1,12 @@ +CREATE TABLE idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) + , chat_id UUID NOT NULL REFERENCES chats(id) + , key VARCHAR(128) NOT NULL + , request_hash CHAR(64) NOT NULL + , message_id UUID REFERENCES messages(id) + , expires_at TIMESTAMPTZ NOT NULL + , PRIMARY KEY (user_id, chat_id, key) +) +; +CREATE INDEX idempotency_expiry ON idempotency_keys (expires_at) +; diff --git a/migrations/004_attachments.sql b/migrations/004_attachments.sql new file mode 100644 index 00000000..bfd51898 --- /dev/null +++ b/migrations/004_attachments.sql @@ -0,0 +1,30 @@ +ALTER TABLE messages + DROP CONSTRAINT messages_text_bytes + , ADD CONSTRAINT messages_text_bytes CHECK (octet_length(text) BETWEEN 0 AND 16000) + , ADD COLUMN attachment_ids UUID[] NOT NULL DEFAULT '{}' +; +CREATE TABLE attachments ( + id UUID PRIMARY KEY + , chat_id UUID NOT NULL REFERENCES chats(id) + , owner_id UUID NOT NULL REFERENCES users(id) + , filename TEXT NOT NULL + , object_key TEXT NOT NULL UNIQUE + , content_type TEXT NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')) + , size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760) + , sha256 CHAR(64) NOT NULL + , state TEXT NOT NULL CHECK (state IN ('uploading', 'ready', 'storage_missing')) + , created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() + , updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() + , FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id) +) +; +CREATE INDEX attachments_chat ON attachments (chat_id, id) +; +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) + , attachment_id UUID NOT NULL REFERENCES attachments(id) + , PRIMARY KEY (message_id, attachment_id) +) +; +CREATE INDEX message_attachments_attachment ON message_attachments (attachment_id) +; diff --git a/requirements-research.txt b/requirements-research.txt new file mode 100644 index 00000000..5a9a4de1 --- /dev/null +++ b/requirements-research.txt @@ -0,0 +1,2 @@ +# Optional: only for rendering figures (application and tests need no matplotlib). +matplotlib==3.11.2 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..30a631ed --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +psycopg[binary]==3.2.3 +psycopg-pool==3.2.4 +redis==5.2.1 +typing-extensions==4.16.0 +boto3==1.40.61 +Pillow==13.0.0 +botocore==1.40.76 +jmespath==1.1.0 +s3transfer==0.14.0 +python-dateutil==2.9.0.post0 +urllib3==2.8.0 +six==1.17.0 diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py new file mode 100644 index 00000000..c18a2ee1 --- /dev/null +++ b/scripts/bootstrap.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Create local credentials without printing or overwriting existing values.""" +import json +from pathlib import Path +import secrets + +root = Path(__file__).resolve().parents[1] +directory = root / 'secrets' +directory.mkdir(mode=0o700, exist_ok=True) + +def save(name, value): + path = directory / name + if not path.exists(): + path.write_text(value) + path.chmod(0o644) # UID 10001 in Compose; parent is owner-only 0700. + return path.read_text().strip() + +password = save('postgres_password', secrets.token_hex(24)) +save('database_url', 'postgresql://messenger:' + password + '@postgres:5432/messenger') +save('cache_url', 'redis://redis:6379/0') +identities = [] +for role, actions in { + 'admin': ['Admin', 'Read', 'Write', 'List'], + 'api': ['Read:messenger-attachments', 'Write:messenger-attachments', 'List:messenger-attachments'], + 'reconciler': ['Read:messenger-attachments', 'Write:messenger-attachments', 'List:messenger-attachments'], +}.items(): + key = save('s3_' + role + '_key', secrets.token_hex(12)) + secret = save('s3_' + role + '_secret', secrets.token_hex(24)) + identities.append({'name': role, 'credentials': [{'accessKey': key, 'secretKey': secret}], 'actions': actions}) +save('s3_config', json.dumps({'identities': identities})) +env = root / '.env' +if not env.exists(): + env.write_text((root / '.env.example').read_text()) + env.chmod(0o600) +print('Local configuration ready; no secrets printed.') diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..efa8b618 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +python3 scripts/snapshot.py > evidence/restart-before.json +docker compose stop api lb +docker compose restart postgres redis s3-store s3 +docker compose up -d --wait postgres redis s3-store s3 +docker compose up -d --wait api lb +python3 scripts/snapshot.py > evidence/restart-after.json diff --git a/scripts/snapshot.py b/scripts/snapshot.py new file mode 100644 index 00000000..3d0354f3 --- /dev/null +++ b/scripts/snapshot.py @@ -0,0 +1,11 @@ +"""Sanitized process and resource evidence; never dump container environments.""" +import json +import subprocess + +ids = subprocess.check_output(['docker', 'compose', 'ps', '-aq'], text=True).split() +records = [] +for container in ids: + raw = subprocess.check_output(['docker', 'inspect', container], text=True) + data = json.loads(raw)[0] + records.append({'container': data['Id'], 'name': data['Name'], 'service': data['Config']['Labels']['com.docker.compose.service'], 'image': data['Config']['Image'], 'user': data['Config']['User'], 'running': data['State']['Running'], 'started_at': data['State']['StartedAt'], 'exit_code': data['State']['ExitCode'], 'oom_killed': data['State']['OOMKilled'], 'memory_limit': data['HostConfig']['Memory'], 'nano_cpus': data['HostConfig']['NanoCpus'], 'ports': data['NetworkSettings']['Ports'], 'mounts': [{'type': m['Type'], 'destination': m['Destination'], 'name': m.get('Name')} for m in data['Mounts']]}) +print(json.dumps(records, indent=2)) diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..ce9279f2 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/stop_one.py diff --git a/scripts/stop_one.py b/scripts/stop_one.py new file mode 100644 index 00000000..bc495aa5 --- /dev/null +++ b/scripts/stop_one.py @@ -0,0 +1,32 @@ +"""Resolve the requested process alias using each actual container, never a fake header.""" +import json +import os +from pathlib import Path +import subprocess +import time + + +def docker(*args): + return subprocess.check_output(['docker', *args], text=True).strip() + + +def instance(container): + code = "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=1).headers['X-Instance-Id'])" + return docker('exec', container, 'python', '-c', code) + + +if __name__ == '__main__': + target = os.environ['TARGET_INSTANCE_ID'] + started = time.monotonic() + for container in docker('compose', 'ps', '-q', 'api').splitlines(): + if instance(container) == target: + docker('stop', '--time', '8', container) + state = json.loads(docker('inspect', '--format', '{{json .State}}', container)) + assert not state['Running'], 'target must remain stopped' + record = {'instance': target, 'container': container, 'running': state['Running'], 'exit_code': state['ExitCode'], 'seconds': round(time.monotonic() - started, 3)} + with Path('evidence/stopped-instances.jsonl').open('a') as out: + out.write(json.dumps(record) + '\n') + print(json.dumps(record)) + break + else: + raise SystemExit('Requested live API instance not found') diff --git a/scripts/wait_ready.py b/scripts/wait_ready.py new file mode 100644 index 00000000..e635caef --- /dev/null +++ b/scripts/wait_ready.py @@ -0,0 +1,16 @@ +import os +import time +import urllib.request + +base = os.environ.get('BASE_URL', 'http://127.0.0.1:18087') +for attempt in range(120): + try: + with urllib.request.urlopen(base + '/health/ready', timeout=2) as response: + if response.status == 200: + print('Ready through load balancer.') + break + except OSError: + pass + time.sleep(0.5) +else: + raise SystemExit('Readiness timeout') diff --git a/src/bucket_bootstrap.py b/src/bucket_bootstrap.py new file mode 100644 index 00000000..a0a2cbd3 --- /dev/null +++ b/src/bucket_bootstrap.py @@ -0,0 +1,13 @@ +"""Idempotent private bucket bootstrap, executed only with administrative S3 keys.""" +from botocore.exceptions import ClientError +from objects import Objects +from postgres import setting + +objects = Objects(setting) +try: + objects.client.create_bucket(Bucket=objects.bucket) +except ClientError as exc: + if exc.response['Error']['Code'] not in ('BucketAlreadyOwnedByYou', 'BucketAlreadyExists'): + raise RuntimeError('Bucket bootstrap failed') from None +objects.ready() +print('Private bucket ready; static authenticated identities loaded from local secret config.') diff --git a/src/common.py b/src/common.py new file mode 100644 index 00000000..29c87c58 --- /dev/null +++ b/src/common.py @@ -0,0 +1,98 @@ +"""Shared HTTP contract; storage is supplied by each laboratory.""" +import base64 +import datetime as dt +import json +import re +import uuid + + +class APIError(Exception): + def __init__(self, status, code, message): + self.status, self.code, self.message = status, code, message + + +def invalid(): + raise APIError(400, 'invalid_request', 'Неверные параметры запроса') + + +def unauthorized(): + raise APIError(401, 'unauthorized', 'Требуется действительная идентификация') + + +def missing(): + raise APIError(404, 'not_found', 'Объект не найден') + + +def identifier(value): + try: + if not isinstance(value, str): + invalid() + return str(uuid.UUID(value)) + except ValueError: + invalid() + + +def text(value, maximum, minimum=1): + if not isinstance(value, str) or not minimum <= len(value) <= maximum or not value.strip(): + invalid() + try: + value.encode('utf-8') + except UnicodeError: + invalid() + return value + + +def fields(value, required): + if not isinstance(value, dict) or set(value) != set(required): + invalid() + + +def username(value): + if not isinstance(value, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', value): + invalid() + return value + + +def now(): + return dt.datetime.now(dt.timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z') + + +def page_args(query, scope): + if set(query) - {'limit', 'cursor'} or any(len(v) != 1 for v in query.values()): + invalid() + limit = query.get('limit', ['50'])[0] + if not re.fullmatch(r'[0-9]+', limit): + invalid() + try: + limit = int(limit) + except ValueError: + invalid() + if not 1 <= limit <= 100: + invalid() + cursor = None + if 'cursor' in query: + raw = query['cursor'][0] + try: + if not raw or len(raw) > 1024: + invalid() + decoded = json.loads(base64.b64decode(raw + '=' * (-len(raw) % 4), altchars=b'-_', validate=True)) + if not isinstance(decoded, list) or len(decoded) != 3 or decoded[0] != scope: + invalid() + stamp = decoded[1] + if not isinstance(stamp, str) or not stamp.endswith('Z'): + invalid() + stamp = dt.datetime.fromisoformat(stamp[:-1] + '+00:00').isoformat(timespec='microseconds').replace('+00:00', 'Z') + cursor = (stamp, identifier(decoded[2])) + except (ValueError, TypeError, UnicodeError): + invalid() + return limit, cursor + + +def page(items, limit, scope): + more = len(items) > limit + items = items[:limit] + cursor = None + if more: + last = items[-1] + cursor = base64.urlsafe_b64encode(json.dumps([scope, last['created_at'], last['id']]).encode()).decode().rstrip('=') + return {'items': items, 'next_cursor': cursor} diff --git a/src/migrate.py b/src/migrate.py new file mode 100644 index 00000000..ebd85f70 --- /dev/null +++ b/src/migrate.py @@ -0,0 +1,13 @@ +"""One-shot migration job; replicas never execute DDL.""" +from pathlib import Path +import psycopg +from postgres import setting + +with psycopg.connect(setting('DATABASE_URL'), connect_timeout=5) as conn: + conn.execute('SELECT pg_advisory_xact_lock(2301003)') + conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY)') + for path in sorted((Path(__file__).resolve().parents[1] / 'migrations').glob('*.sql')): + if not conn.execute('SELECT version FROM schema_migrations WHERE 1=1 AND version = %s', (path.name,)).fetchone(): + conn.execute(path.read_text()) + conn.execute('INSERT INTO schema_migrations (version) VALUES (%s)', (path.name,)) + print('Applied', path.name) diff --git a/src/objects.py b/src/objects.py new file mode 100644 index 00000000..33c6c552 --- /dev/null +++ b/src/objects.py @@ -0,0 +1,156 @@ +"""Bounded S3 operations. No root credentials or filename-derived object keys.""" +import contextlib +import hashlib +import os +import tempfile +import threading +import time +import unicodedata +import warnings + +import boto3 +from botocore.config import Config +from botocore.exceptions import BotoCoreError, ClientError +from PIL import Image, PngImagePlugin, UnidentifiedImageError + +from common import APIError, invalid + +MAX_BYTES = 10 * 1024 * 1024 +MAX_PIXELS = 40_000_000 +Image.MAX_IMAGE_PIXELS = MAX_PIXELS +PngImagePlugin.MAX_TEXT_CHUNK = 1024 * 1024 +PngImagePlugin.MAX_TEXT_MEMORY = 1024 * 1024 +warnings.filterwarnings('error', category=Image.DecompressionBombWarning) +UPLOAD_SLOTS = threading.BoundedSemaphore(8) +DECODE_SLOTS = threading.BoundedSemaphore(1) + + +def unavailable(): + raise APIError(503, 'unavailable', 'Объектное хранилище временно недоступно') + + +def filename(value): + if not isinstance(value, str) or not 1 <= len(value) <= 255 or not value.strip(): + invalid() + if any(c in '/\\' or unicodedata.category(c) == 'Cc' for c in value): + invalid() + try: + value.encode('utf-8') + except UnicodeError: + invalid() + return value + + +@contextlib.contextmanager +def bounded_upload(): + if not UPLOAD_SLOTS.acquire(blocking=False): + raise APIError(503, 'unavailable', 'Все слоты загрузки заняты') + try: + yield + finally: + UPLOAD_SLOTS.release() + + +@contextlib.contextmanager +def receive(handler): + lengths = handler.headers.get_all('Content-Length', []) + if len(lengths) != 1 or handler.headers.get('Transfer-Encoding'): + invalid() + try: + length = int(lengths[0]) + except ValueError: + invalid() + if length > MAX_BYTES: + raise APIError(413, 'payload_too_large', 'Максимум 10 MiB') + if length <= 0: + invalid() + mime = handler.headers.get_content_type() + if mime not in ('image/png', 'image/jpeg'): + raise APIError(415, 'unsupported_media_type', 'Требуется PNG или JPEG') + deadline = time.monotonic() + 15 + digest = hashlib.sha256() + # Ephemeral bounded spool is never a durable source of a confirmed attachment. + with tempfile.TemporaryFile(mode='w+b') as source: + remaining = length + while remaining: + handler.connection.settimeout(min(3, max(0.01, deadline - time.monotonic()))) + if time.monotonic() >= deadline: + invalid() + try: + chunk = handler.rfile.read1(min(64 * 1024, remaining)) + except (OSError, TimeoutError): + invalid() + if not chunk: + invalid() + digest.update(chunk) + source.write(chunk) + remaining -= len(chunk) + if not DECODE_SLOTS.acquire(timeout=10): + raise APIError(503, 'unavailable', 'Проверка изображений перегружена') + try: + source.flush() + for decode in (False, True): + source.seek(0) + # Pillow.close releases pixel buffers and may close its input file. + # A duplicate descriptor preserves the original for the S3 PUT. + with os.fdopen(os.dup(source.fileno()), 'rb') as image_file: + image = Image.open(image_file, formats=['PNG', 'JPEG']) + try: + if image.width * image.height > MAX_PIXELS: + raise APIError(413, 'payload_too_large', 'Максимум 40 миллионов пикселей') + if Image.MIME[image.format] != mime: + raise APIError(415, 'unsupported_media_type', 'MIME не соответствует изображению') + if decode: + image.load() + else: + image.verify() + finally: + image.close() # Free pixels BEFORE releasing the decoder semaphore. + except (Image.DecompressionBombWarning, Image.DecompressionBombError): + raise APIError(413, 'payload_too_large', 'Максимум 40 миллионов пикселей') from None + except (UnidentifiedImageError, OSError, ValueError, SyntaxError): + raise APIError(415, 'unsupported_media_type', 'Повреждённое изображение') from None + finally: + DECODE_SLOTS.release() + source.seek(0) + yield source, length, mime, digest.hexdigest() + + +class Objects: + def __init__(self, setting): + self.bucket = os.environ.get('S3_BUCKET', 'messenger-attachments') + self.client = boto3.client('s3', endpoint_url=os.environ.get('S3_ENDPOINT', 'http://s3:8333'), region_name='us-east-1', aws_access_key_id=setting('S3_ACCESS_KEY_ID'), aws_secret_access_key=setting('S3_SECRET_ACCESS_KEY'), config=Config(signature_version='s3v4', s3={'addressing_style': 'path'}, connect_timeout=1, read_timeout=3, max_pool_connections=16, retries={'total_max_attempts': 1}, request_checksum_calculation='when_required', response_checksum_validation='when_required')) + + def head(self, key): + try: + return self.client.head_object(Bucket=self.bucket, Key=key) + except ClientError as exc: + if exc.response['Error']['Code'] in ('404', 'NoSuchKey', 'NotFound'): + return None + unavailable() + except BotoCoreError: + unavailable() + + def ready(self): + try: + self.client.head_bucket(Bucket=self.bucket) + except (ClientError, BotoCoreError): + unavailable() + + def put(self, key, source, length, mime, digest): + try: + self.client.put_object(Bucket=self.bucket, Key=key, Body=source, ContentLength=length, ContentType=mime, Metadata={'sha256': digest}) + except (ClientError, BotoCoreError): + unavailable() + + def get(self, key): + try: + return self.client.get_object(Bucket=self.bucket, Key=key) + except (ClientError, BotoCoreError): + unavailable() + + def delete(self, key): + try: + self.client.delete_object(Bucket=self.bucket, Key=key) + except (ClientError, BotoCoreError): + unavailable() diff --git a/src/postgres.py b/src/postgres.py new file mode 100644 index 00000000..70e5fdef --- /dev/null +++ b/src/postgres.py @@ -0,0 +1,399 @@ +import base64 +import contextlib +import datetime as dt +import hashlib +import hmac +import json +import os +import re +from pathlib import Path +import secrets +import threading +import uuid + +import psycopg +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool, PoolTimeout +import redis + +from objects import Objects, unavailable + +from common import APIError, fields, identifier, invalid, missing, page, text, unauthorized, username + +KDF_SLOTS = threading.BoundedSemaphore(2) + + +def setting(name): + path = os.environ.get(name + '_FILE') + return Path(path).read_text().strip() if path else os.environ[name] + + +def public(row): + return {k: (v.decode('utf-8') if isinstance(v, bytes) else [str(x) for x in v] if isinstance(v, list) else str(v) if isinstance(v, uuid.UUID) else v.isoformat(timespec='microseconds').replace('+00:00', 'Z') if isinstance(v, dt.datetime) else v) for k, v in row.items() if k != 'password_hash'} + + +def password_hash(password, salt=None): + salt = salt or secrets.token_bytes(16) + if not KDF_SLOTS.acquire(timeout=1.5): + raise APIError(503, 'unavailable', 'Аутентификация временно перегружена') + try: + digest = hashlib.scrypt(password.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32, maxmem=64 * 1024 * 1024) + finally: + KDF_SLOTS.release() + return 'scrypt$16384$8$1$' + salt.hex() + '$' + digest.hex() + + +class Store: + persistent = True + + def __init__(self): + self.ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600')) + if not 1 <= self.ttl <= 86400: + raise ValueError('SESSION_TTL_SECONDS must be 1..86400') + self.access_mode = os.environ.get('DB_ACCESS_MODE', 'optimized') + if self.access_mode not in ('baseline', 'optimized'): + raise ValueError('DB_ACCESS_MODE must be baseline or optimized') + pool_size = int(os.environ.get('DB_POOL_MAX', '8')) + if not 1 <= pool_size <= 32: + raise ValueError('DB_POOL_MAX must be 1..32') + self.pool = ConnectionPool(setting('DATABASE_URL'), min_size=1, max_size=pool_size, timeout=2, kwargs={'row_factory': dict_row, 'connect_timeout': 2, 'options': '-c statement_timeout=2000 -c lock_timeout=1500'}, open=True) + self.pool.wait(timeout=10) + self.cache = redis.Redis.from_url(setting('CACHE_URL'), socket_connect_timeout=0.15, socket_timeout=0.15, decode_responses=True) + self.objects = Objects(setting) + self.dummy_hash = password_hash('dummy-password-for-unknown-users') + with self.connection() as conn: + if not conn.execute("SELECT version FROM schema_migrations WHERE 1=1 AND version = '004_attachments.sql'").fetchone(): + raise RuntimeError('Run the separate migration service first') + + @contextlib.contextmanager + def connection(self): + try: + with self.pool.connection() as conn: + yield conn + except psycopg.errors.UniqueViolation: + raise APIError(409, 'conflict', 'Имя пользователя занято') from None + except (psycopg.Error, PoolTimeout): + raise APIError(503, 'unavailable', 'Хранилище временно недоступно') from None + + def close(self): + self.pool.close() + self.cache.close() + self.objects.client.close() + + def ready(self): + self.objects.ready() + with self.connection() as conn: + conn.execute('SELECT 1') + + def register(self, body): + fields(body, ('username', 'display_name', 'password')) + name, display = username(body['username']), text(body['display_name'], 100) + # Passwords may contain spaces and colons; only length/UTF-8 are restricted. + password = body['password'] + if not isinstance(password, str) or not 12 <= len(password) <= 128: + invalid() + try: + password.encode('utf-8') + except UnicodeError: + invalid() + hashed = password_hash(password) + with self.connection() as conn: + row = conn.execute(''' + INSERT INTO users (id, username, display_name, password_hash) + VALUES (%s, %s, %s, %s) + RETURNING id, username, display_name, created_at + ''', (uuid.uuid4(), name, display.encode('utf-8'), hashed)).fetchone() + return public(row) + + def login(self, headers): + authorization = headers.get('Authorization', '') + try: + scheme, encoded = authorization.split(' ', 1) + if scheme.lower() != 'basic': + unauthorized() + name, password = base64.b64decode(encoded, validate=True).decode('utf-8').split(':', 1) + except (ValueError, UnicodeError): + unauthorized() + if not re_username(name) or not 12 <= len(password) <= 128: + unauthorized() + with self.connection() as conn: + row = conn.execute('SELECT id, username, display_name, created_at, password_hash FROM users WHERE 1=1 AND username = %s', (name,)).fetchone() + hashed = row['password_hash'] if row else self.dummy_hash + verified = hmac.compare_digest(password_hash(password, bytes.fromhex(hashed.split('$')[4])), hashed) + if row is None or not verified: + unauthorized() + token = secrets.token_urlsafe(32) + with self.connection() as conn: + expiry = conn.execute(''' + INSERT INTO sessions (token_hash, user_id, expires_at) + VALUES (%s, %s, clock_timestamp() + %s * INTERVAL '1 second') + RETURNING expires_at + ''', (hashlib.sha256(token.encode()).hexdigest(), row['id'], self.ttl)).fetchone()['expires_at'] + return {'token': token, 'token_type': 'Bearer', 'expires_at': public({'expires_at': expiry})['expires_at'], 'user': public(row)} + + def token_hash(self, headers): + parts = headers.get('Authorization', '').split(' ') + if len(parts) != 2 or parts[0].lower() != 'bearer' or len(parts[1]) != 43 or not parts[1].isascii(): + unauthorized() + return hashlib.sha256(parts[1].encode()).hexdigest() + + def authenticate(self, headers): + token_hash = self.token_hash(headers) + with self.connection() as conn: + session = conn.execute(''' + SELECT user_id + FROM sessions + WHERE 1=1 + AND token_hash = %s + AND expires_at > clock_timestamp() + ''', (token_hash,)).fetchone() + if session is None: + unauthorized() + key = 'user:' + str(session['user_id']) + try: + cached = self.cache.get(key) + if cached: + return json.loads(cached) + except (redis.RedisError, ValueError): + pass + row = conn.execute('SELECT id, username, display_name, created_at FROM users WHERE 1=1 AND id = %s', (session['user_id'],)).fetchone() + if row is None: + unauthorized() + user = public(row) + try: + self.cache.setex(key, 60, json.dumps(user)) + except redis.RedisError: + pass + return user + + def logout(self, headers): + with self.connection() as conn: + conn.execute('DELETE FROM sessions WHERE 1=1 AND token_hash = %s', (self.token_hash(headers),)) + + def chat_in(self, conn, actor, chat_id): + row = conn.execute(''' + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE 1=1 + AND c.id = %s + AND cm.user_id = %s + ''', (chat_id, actor['id'])).fetchone() + if row is None: + missing() + members = conn.execute('SELECT user_id FROM chat_members WHERE 1=1 AND chat_id = %s ORDER BY user_id', (chat_id,)).fetchall() + return dict(public(row), member_ids=[str(v['user_id']) for v in members]) + + def chat(self, actor, chat_id): + with self.connection() as conn: + return self.chat_in(conn, actor, chat_id) + + def create_chat(self, actor, body): + fields(body, ('title', 'member_ids')) + title = text(body['title'], 100) + members = body['member_ids'] + if not isinstance(members, list) or not 1 <= len(members) <= 100: + invalid() + members = [identifier(v) for v in members] + if len(set(members)) != len(members) or actor['id'] not in members: + invalid() + with self.connection() as conn: + chat_id = uuid.uuid4() + conn.execute('INSERT INTO chats (id, title) VALUES (%s, %s)', (chat_id, title.encode('utf-8'))) + # Checking after INSERT exercises rollback of the whole composite operation. + found = conn.execute('SELECT id FROM users WHERE 1=1 AND id = ANY(%s::uuid[])', (members,)).fetchall() + if len(found) != len(members): + invalid() + with conn.cursor() as cur: + cur.executemany('INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)', [(chat_id, v) for v in members]) + return self.chat_in(conn, actor, chat_id) + + def list_chats(self, actor, limit, cursor, scope): + with self.connection() as conn: + where = 'AND (c.created_at, c.id) > (%s::timestamptz, %s::uuid)' if cursor else '' + params = [actor['id']] + list(cursor or ()) + [limit + 1] + rows = conn.execute(''' + SELECT c.id + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE 1=1 + AND cm.user_id = %s + ''' + where + ''' + ORDER BY c.created_at, c.id + LIMIT %s + ''', params).fetchall() + return page([self.chat_in(conn, actor, v['id']) for v in rows], limit, scope) + + def require_member(self, conn, actor, chat_id): + if self.access_mode == 'baseline': + return self.chat_in(conn, actor, chat_id) + if not conn.execute(''' + SELECT 1 + FROM chat_members + WHERE 1=1 + AND chat_id = %s + AND user_id = %s + ''', (chat_id, actor['id'])).fetchone(): + missing() + + def send(self, actor, chat_id, body, key): + if not isinstance(key, str) or not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', key): + invalid() + if not isinstance(body, dict) or set(body) - {'text', 'attachment_ids'} or 'text' not in body: + invalid() + attachments = body.get('attachment_ids', []) + if not isinstance(attachments, list) or len(attachments) > 10: + invalid() + attachments = [identifier(v) for v in attachments] + if len(set(attachments)) != len(attachments): + invalid() + content = body['text'] + if not isinstance(content, str) or len(content) > 4000 or (not attachments and not content.strip()): + invalid() + try: + content.encode('utf-8') + except UnicodeError: + invalid() + canonical = json.dumps({'text': content, 'attachment_ids': attachments}, ensure_ascii=False, sort_keys=True, separators=(',', ':')) + digest = hashlib.sha256(canonical.encode('utf-8')).hexdigest() + with self.connection() as conn: + self.require_member(conn, actor, chat_id) + # The unique key arbitrates concurrent processes; everything commits together. + claimed = conn.execute(''' + INSERT INTO idempotency_keys (user_id, chat_id, key, request_hash, expires_at) + VALUES (%s, %s, %s, %s, clock_timestamp() + INTERVAL '24 hours') + ON CONFLICT (user_id, chat_id, key) DO UPDATE + SET request_hash = EXCLUDED.request_hash + , message_id = NULL + , expires_at = EXCLUDED.expires_at + WHERE 1=1 + AND idempotency_keys.expires_at <= clock_timestamp() + RETURNING request_hash + ''', (actor['id'], chat_id, key, digest)).fetchone() + if claimed is None: + previous = conn.execute(''' + SELECT ik.request_hash + , m.id + , m.chat_id + , m.sender_id + , m.text + , m.created_at + , m.attachment_ids + FROM idempotency_keys ik + JOIN messages m ON m.id = ik.message_id + WHERE 1=1 + AND ik.user_id = %s + AND ik.chat_id = %s + AND ik.key = %s + ''', (actor['id'], chat_id, key)).fetchone() + if previous['request_hash'] != digest: + raise APIError(409, 'conflict', 'Ключ уже использован для другого сообщения') + previous.pop('request_hash') + return public(previous) + if attachments: + rows = conn.execute(''' + SELECT id, state + FROM attachments + WHERE 1=1 + AND id = ANY(%s::uuid[]) + AND chat_id = %s + ORDER BY id + FOR SHARE + ''', (attachments, chat_id)).fetchall() + if len(rows) != len(attachments): + missing() + if any(v['state'] != 'ready' for v in rows): + raise APIError(409, 'conflict', 'Вложение ещё не готово') + # Per-chat row lock preserves committed history order across replicas/clocks. + stamp = conn.execute(''' + UPDATE chats + SET last_message_at = GREATEST(clock_timestamp(), last_message_at + INTERVAL '1 microsecond') + WHERE 1=1 + AND id = %s + RETURNING last_message_at + ''', (chat_id,)).fetchone()['last_message_at'] + row = conn.execute(''' + INSERT INTO messages (id, chat_id, sender_id, text, created_at, attachment_ids) + VALUES (%s, %s, %s, %s, %s, %s::uuid[]) + RETURNING id, chat_id, sender_id, text, created_at, attachment_ids + ''', (uuid.uuid4(), chat_id, actor['id'], content.encode('utf-8'), stamp, attachments)).fetchone() + if attachments: + with conn.cursor() as cur: + cur.executemany('INSERT INTO message_attachments (message_id, attachment_id) VALUES (%s, %s)', [(row['id'], v) for v in attachments]) + conn.execute(''' + UPDATE idempotency_keys + SET message_id = %s + WHERE 1=1 + AND user_id = %s + AND chat_id = %s + AND key = %s + ''', (row['id'], actor['id'], chat_id, key)) + return public(row) + + def history(self, actor, chat_id, limit, cursor, scope): + with self.connection() as conn: + self.require_member(conn, actor, chat_id) + where = 'AND (created_at, id) > (%s::timestamptz, %s::uuid)' if cursor else '' + params = [chat_id] + list(cursor or ()) + [limit + 1] + rows = conn.execute(''' + SELECT id, chat_id, sender_id, text, created_at, attachment_ids + FROM messages + WHERE 1=1 + AND chat_id = %s + ''' + where + ''' + ORDER BY created_at, id + LIMIT %s + ''', params).fetchall() + return page([public(v) for v in rows], limit, scope) + + def upload(self, actor, chat_id, name, source, length, mime, digest): + attachment_id = uuid.uuid4() + key = 'originals/' + str(attachment_id) + # Durable intent exists BEFORE any S3 write; a crash always leaves a ledger entry. + with self.connection() as conn: + self.require_member(conn, actor, chat_id) + conn.execute(''' + INSERT INTO attachments (id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, state) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, 'uploading') + ''', (attachment_id, chat_id, actor['id'], name, key, mime, length, digest)) + with self.connection() as conn: + # Reconciler uses the same row lock; no in-flight upload can be collected. + if conn.execute('SELECT id FROM attachments WHERE 1=1 AND id = %s FOR UPDATE', (attachment_id,)).fetchone() is None: + unavailable() + self.objects.put(key, source, length, mime, digest) + row = conn.execute(''' + UPDATE attachments + SET state = 'ready' + , updated_at = clock_timestamp() + WHERE 1=1 + AND id = %s + RETURNING * + ''', (attachment_id,)).fetchone() + return self.attachment_public(row) + + def attachment_public(self, row): + result = public({k: v for k, v in row.items() if k not in ('object_key', 'state')}) + return dict(result, status='ready', variants=[], error_code=None) + + def attachment(self, actor, attachment_id): + with self.connection() as conn: + row = conn.execute(''' + SELECT a.* + FROM attachments a + JOIN chat_members cm ON cm.chat_id = a.chat_id + WHERE 1=1 + AND a.id = %s + AND cm.user_id = %s + ''', (attachment_id, actor['id'])).fetchone() + if row is None: + missing() + if row['state'] == 'storage_missing': + unavailable() + if row['state'] != 'ready': + raise APIError(409, 'conflict', 'Вложение ещё не готово') + return row + + +def re_username(value): + import re + return re.fullmatch(r'[a-z0-9_]{3,32}', value) diff --git a/src/reconcile.py b/src/reconcile.py new file mode 100644 index 00000000..663e7516 --- /dev/null +++ b/src/reconcile.py @@ -0,0 +1,129 @@ +"""Conservative two-store reconciler; dry-run by default, no API endpoint.""" +import argparse +import datetime as dt +import hashlib +import json +import re +import time + +import psycopg +from psycopg.rows import dict_row +from botocore.exceptions import BotoCoreError, ClientError + +from objects import Objects +from postgres import setting + + +def reconcile(apply=False, grace=3600): + started = time.monotonic() + # Fixed cutoff for the entire pass: objects born during scanning stay protected. + cutoff = dt.datetime.now(dt.timezone.utc) - dt.timedelta(seconds=grace) + objects = Objects(setting) + counts = dict(metadata=0, objects=0, head_calls=0, get_calls=0, deleted_objects=0, deleted_intents=0, marked_missing=0, restored=0, skipped_locked=0, protected_fresh=0, protected_ready=0, protected_foreign_prefix=0) + findings = [] + def note(kind, attachment_id): + if len(findings) < 200: + findings.append({'kind': kind, 'id': str(attachment_id)}) + with psycopg.connect(setting('DATABASE_URL'), autocommit=True, row_factory=dict_row, connect_timeout=2, options='-c statement_timeout=2000 -c lock_timeout=500') as conn: + if not conn.execute('SELECT pg_try_advisory_lock(2301004) AS locked').fetchone()['locked']: + raise RuntimeError('Another reconciler is running') + last = '00000000-0000-0000-0000-000000000000' + while True: + batch = conn.execute('SELECT id FROM attachments WHERE 1=1 AND id > %s ORDER BY id LIMIT 100', (last,)).fetchall() + if not batch: + break + for item in batch: + last = item['id'] + counts['metadata'] += 1 + with conn.transaction(): + row = conn.execute('SELECT * FROM attachments WHERE 1=1 AND id = %s FOR UPDATE SKIP LOCKED', (last,)).fetchone() + if row is None: + counts['skipped_locked'] += 1 + continue + if row['updated_at'] > cutoff: + counts['protected_fresh'] += 1 + continue + head = objects.head(row['object_key']) + counts['head_calls'] += 1 + if row['state'] == 'uploading': + # FK and state locking also prevent deleting anything referenced. + used = conn.execute('SELECT 1 FROM message_attachments WHERE 1=1 AND attachment_id = %s LIMIT 1', (last,)).fetchone() + if used: + note('unexpected_used_intent_retained', last) + continue + note('stale_intent', last) + if apply: + objects.delete(row['object_key']) + counts['deleted_objects'] += int(head is not None) + conn.execute("DELETE FROM attachments WHERE 1=1 AND id = %s AND state = 'uploading'", (last,)) + counts['deleted_intents'] += 1 + elif head is None or head['ContentLength'] != row['size_bytes'] or head.get('Metadata', {}).get('sha256') != row['sha256']: + note('missing_or_mismatched', last) + if apply and row['state'] != 'storage_missing': + conn.execute("UPDATE attachments SET state = 'storage_missing', updated_at = clock_timestamp() WHERE 1=1 AND id = %s", (last,)) + counts['marked_missing'] += 1 + elif row['state'] == 'storage_missing': + # Recovery needs actual bytes, not merely a claimed metadata hash. + response = objects.get(row['object_key']) + counts['get_calls'] += 1 + digest = hashlib.sha256() + size = 0 + with response['Body'] as stream: + for chunk in iter(lambda: stream.read(64 * 1024), b''): + digest.update(chunk) + size += len(chunk) + if size == row['size_bytes'] and digest.hexdigest() == row['sha256']: + note('recoverable', last) + if apply: + conn.execute("UPDATE attachments SET state = 'ready', updated_at = clock_timestamp() WHERE 1=1 AND id = %s", (last,)) + counts['restored'] += 1 + else: + note('corrupt_recovery_retained', last) + else: + counts['protected_ready'] += 1 + # Paginated listing; never collect all keys in memory. + paginator = objects.client.get_paginator('list_objects_v2') + for result in paginator.paginate(Bucket=objects.bucket, Prefix='originals/', PaginationConfig={'PageSize': 100}): + for item in result.get('Contents', []): + counts['objects'] += 1 + key = item['Key'] + if not re.fullmatch(r'originals/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}', key): + counts['protected_foreign_prefix'] += 1 + continue + if item['LastModified'] > cutoff: + counts['protected_fresh'] += 1 + continue + # A valid upload commits intent BEFORE PUT, and never reuses object keys. + with conn.transaction(): + if conn.execute('SELECT 1 FROM attachments WHERE 1=1 AND object_key = %s', (key,)).fetchone(): + continue + head = objects.head(key) + counts['head_calls'] += 1 + if head is None or head['LastModified'] > cutoff: + continue + note('orphan', key.split('/')[1]) + if apply: + objects.delete(key) + counts['deleted_objects'] += 1 + objects.client.close() + return dict(apply=apply, grace_seconds=grace, elapsed_seconds=round(time.monotonic() - started, 6), counts=counts, findings=findings) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--apply', action='store_true') + parser.add_argument('--grace-seconds', type=int, default=3600) + parser.add_argument('--allow-short-test-grace', action='store_true', help='Only on an isolated synthetic test stand') + args = parser.parse_args() + if args.grace_seconds < (1 if args.allow_short_test_grace else 3600): + parser.error('Production grace must be at least 3600 seconds') + try: + print(json.dumps(reconcile(args.apply, args.grace_seconds), indent=2)) + except Exception: + # Do not serialize library exception details, DSNs, signatures or keys. + print(json.dumps({'error': 'Reconciliation aborted; no fail-open cleanup. Retry after restoring dependencies.'})) + raise SystemExit(1) + + +if __name__ == '__main__': + main() diff --git a/src/server.py b/src/server.py new file mode 100644 index 00000000..6df84627 --- /dev/null +++ b/src/server.py @@ -0,0 +1,290 @@ +"""Bounded threaded API; persistent correctness state is shared across replicas.""" +import concurrent.futures +import http.server +import json +import logging +import os +import time +import signal +import socket +import threading +import uuid +from urllib.parse import parse_qs, urlsplit + +from objects import bounded_upload, receive, filename, unavailable + +from common import APIError, identifier, invalid, missing, page_args + +LOG = logging.getLogger('messenger') + + +def unique_object(pairs): + result = {} + for key, value in pairs: + if key in result: + invalid() + result[key] = value + return result + + +class Handler(http.server.BaseHTTPRequestHandler): + protocol_version = 'HTTP/1.1' + + def setup(self): + super().setup() + self.connection.settimeout(3) + + def send_error(self, code, message=None, explain=None): + # BaseHTTPRequestHandler otherwise emits HTML without contract headers. + self.request_id = uuid.uuid4().hex + self.route = '' + self.close_connection = True + status = 413 if code == 413 else 400 + self.reply(status, {'error': {'code': 'payload_too_large' if status == 413 else 'invalid_request', 'message': 'Некорректный HTTP-запрос', 'request_id': self.request_id}}) + + def log_message(self, *args): + pass + + def reply(self, status, body): + if status == 200 and isinstance(body, tuple): + return self.download(*body) + raw = b'' if status == 204 else json.dumps(body, ensure_ascii=False, allow_nan=False).encode('utf-8') + if status >= 400: + self.close_connection = True + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(raw))) + if self.close_connection: + self.send_header('Connection', 'close') + self.send_header('X-Request-Id', self.request_id) + self.send_header('X-Instance-Id', self.server.instance_id) + if status == 401 and self.server.store.persistent: + self.send_header('WWW-Authenticate', 'Basic realm="messenger", charset="UTF-8"' if self.route == '/api/v1/auth/sessions' else 'Bearer realm="messenger"') + self.end_headers() + if self.command != 'HEAD': + self.wfile.write(raw) + + def download(self, row, response, chunk): + stream = response['Body'] + try: + self.send_response(200) + self.send_header('Content-Type', row['content_type']) + self.send_header('Content-Length', str(row['size_bytes'])) + self.send_header('X-Request-Id', self.request_id) + self.send_header('X-Instance-Id', self.server.instance_id) + self.send_header('X-Content-Type-Options', 'nosniff') + self.send_header('Cache-Control', 'private, no-store') + self.end_headers() + deadline = time.monotonic() + 15 + while chunk: + self.wfile.write(chunk) + if time.monotonic() > deadline: + raise TimeoutError() + chunk = stream.read(64 * 1024) + finally: + stream.close() + + def body(self): + lengths = self.headers.get_all('Content-Length', []) + if len(lengths) != 1 or self.headers.get('Transfer-Encoding'): + invalid() + try: + length = int(lengths[0]) + except ValueError: + invalid() + if length > 32768: + raise APIError(413, 'payload_too_large', 'Максимальный JSON — 32768 байт') + if length <= 0: + invalid() + if self.headers.get_content_type() != 'application/json': + raise APIError(415, 'unsupported_media_type', 'Требуется application/json') + try: + raw = self.rfile.read(length) + if len(raw) != length: + invalid() + return json.loads(raw.decode('utf-8'), object_pairs_hook=unique_object, parse_constant=lambda _: invalid()) + except (ValueError, UnicodeError, RecursionError): + invalid() + + def dispatch(self): + self.request_id = uuid.uuid4().hex + self.route = urlsplit(self.path).path + status = 500 + try: + status, result = self.route_request() + except APIError as exc: + status = exc.status + result = {'error': {'code': exc.code, 'message': exc.message, 'request_id': self.request_id}} + except Exception: + # No bodies, credentials, tokens, SQL or exception details in logs/responses. + LOG.error('request_failed request_id=%s', self.request_id) + result = {'error': {'code': 'internal_error', 'message': 'Внутренняя ошибка', 'request_id': self.request_id}} + try: + self.reply(status, result) + except Exception: + self.close_connection = True + LOG.info('method=%s status=%s request_id=%s', self.command, status, self.request_id) + + def route_request(self): + method, path, store = self.command, self.route, self.server.store + if method == 'GET' and path in ('/health/live', '/health/ready'): + if path.endswith('ready'): + store.ready() + return 200, {'status': 'ok'} + if method == 'POST' and path == '/api/v1/users': + return 201, store.register(self.body()) + if store.persistent and method == 'POST' and path == '/api/v1/auth/sessions': + if self.headers.get('Transfer-Encoding') or self.headers.get('Content-Length', '0') != '0': + invalid() + return 201, store.login(self.headers) + actor = store.authenticate(self.headers) + if store.persistent and method == 'DELETE' and path == '/api/v1/auth/sessions/current': + store.logout(self.headers) + return 204, None + if method == 'GET' and path == '/api/v1/users/me': + return 200, actor + query = parse_qs(urlsplit(self.path).query, keep_blank_values=True) + if path == '/api/v1/chats': + if method == 'POST': + return 201, store.create_chat(actor, self.body()) + if method == 'GET': + scope = 'chats:' + actor['id'] + limit, cursor = page_args(query, scope) + return 200, store.list_chats(actor, limit, cursor, scope) + parts = path.split('/') + if len(parts) in (5, 6) and parts[1:4] == ['api', 'v1', 'attachments'] and method == 'GET': + row = store.attachment(actor, identifier(parts[4])) + if len(parts) == 5: + if query: + invalid() + return 200, store.attachment_public(row) + if parts[5] == 'content': + if set(query) - {'variant'} or any(len(v) != 1 for v in query.values()): + invalid() + if query.get('variant', ['original'])[0] != 'original': + missing() + response = store.objects.get(row['object_key']) + try: + chunk = response['Body'].read(64 * 1024) + if response['ContentLength'] != row['size_bytes'] or not chunk: + unavailable() + except Exception: + response['Body'].close() + unavailable() + return 200, (row, response, chunk) + if len(parts) in (5, 6) and parts[1:4] == ['api', 'v1', 'chats']: + chat_id = identifier(parts[4]) + if store.access_mode == 'baseline': + store.chat(actor, chat_id) + if len(parts) == 5 and method == 'GET': + return 200, store.chat(actor, chat_id) + if len(parts) == 6 and parts[5] == 'attachments' and method == 'POST': + store.chat(actor, chat_id) + if set(query) != {'filename'} or len(query['filename']) != 1: + invalid() + name = filename(query['filename'][0]) + with bounded_upload(): + with receive(self) as (source, length, mime, digest): + return 201, store.upload(actor, chat_id, name, source, length, mime, digest) + if len(parts) == 6 and parts[5] == 'messages': + if method == 'POST': + keys = self.headers.get_all('Idempotency-Key', []) + return 201, store.send(actor, chat_id, self.body(), keys[0] if len(keys) == 1 else None) + if method == 'GET': + scope = 'messages:' + chat_id + limit, cursor = page_args(query, scope) + return 200, store.history(actor, chat_id, limit, cursor, scope) + missing() + + do_GET = do_POST = do_DELETE = do_PUT = do_PATCH = do_OPTIONS = dispatch + + +class Server(http.server.HTTPServer): + request_queue_size = 64 + allow_reuse_address = True + + def __init__(self, address, store): + self.store = store + self.instance_id = uuid.uuid4().hex + workers = int(os.environ.get('HTTP_WORKERS', '16')) + if not 1 <= workers <= 128: + raise ValueError('HTTP_WORKERS must be 1..128') + self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=workers) + self.slots = threading.BoundedSemaphore(workers * 2) + self.connections = set() + self.connections_lock = threading.Lock() + super().__init__(address, Handler) + + def process_request(self, request, address): + if not self.slots.acquire(blocking=False): + rid = uuid.uuid4().hex + raw = json.dumps({'error': {'code': 'unavailable', 'message': 'Очередь запросов заполнена', 'request_id': rid}}).encode() + try: + request.settimeout(0.2) + request.sendall(('HTTP/1.0 503 Service Unavailable\r\nContent-Type: application/json\r\nX-Instance-Id: ' + self.instance_id + '\r\nX-Request-Id: ' + rid + '\r\nContent-Length: ' + str(len(raw)) + '\r\n\r\n').encode() + raw) + except OSError: + pass + self.shutdown_request(request) + return + with self.connections_lock: + self.connections.add(request) + self.executor.submit(self.worker, request, address) + + def worker(self, request, address): + try: + self.finish_request(request, address) + except OSError: + pass + finally: + self.shutdown_request(request) + with self.connections_lock: + self.connections.discard(request) + self.slots.release() + + def close_connections(self): + with self.connections_lock: + connections = list(self.connections) + for connection in connections: + try: + connection.shutdown(socket.SHUT_RDWR) + except OSError: + pass + + def server_close(self): + super().server_close() + # Bound draining even when a slow client keeps an incomplete request alive. + deadline = threading.Timer(5, self.close_connections) + deadline.start() + try: + self.executor.shutdown(wait=True) + finally: + deadline.cancel() + + +def main(): + logging.basicConfig(level=os.environ.get('LOG_LEVEL', 'INFO'), format='%(asctime)s %(levelname)s %(message)s') + from postgres import Store + store = Store() + server = Server((os.environ.get('APP_HOST', '0.0.0.0'), int(os.environ.get('APP_PORT', '8080'))), store) + stopping = threading.Event() + + def stop(signum, frame): + if not stopping.is_set(): + stopping.set() + LOG.info('shutdown_started signal=%s', signum) + threading.Thread(target=server.shutdown, daemon=True).start() + + signal.signal(signal.SIGUSR1, lambda *_: LOG.warning('pool_stats instance=%s stats=%s', server.instance_id, json.dumps(store.pool.get_stats()))) + signal.signal(signal.SIGTERM, stop) + signal.signal(signal.SIGINT, stop) + LOG.info('started port=%s', server.server_address[1]) + try: + server.serve_forever(poll_interval=0.1) + finally: + server.server_close() + store.close() + LOG.info('shutdown_complete') + + +if __name__ == '__main__': + main() diff --git a/student-tests/faults.py b/student-tests/faults.py new file mode 100644 index 00000000..09afb8bc --- /dev/null +++ b/student-tests/faults.py @@ -0,0 +1,134 @@ +"""Actual replica stop after upload, interrupted request, and dependency outages.""" +import hashlib +import json +import os +from pathlib import Path +import socket +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid +from urllib.parse import urlsplit + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure + + +def docker(*args, code=None): + result = subprocess.run(['docker', *args], input=code, text=True, capture_output=True, check=True) + return result.stdout.strip() + + +def instance(container): + return docker('exec', container, 'python', '-c', "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live').headers['X-Instance-Id'])") + + +def wait(c): + deadline = time.monotonic() + 60 + while True: + try: + c.call('GET', '/health/ready', schema='Health') + return + except Failure: + if time.monotonic() > deadline: + raise + time.sleep(.2) + + +def main(): + c = Client() + alice, bob, eve, chat = c.bootstrap() + content = (ROOT / 'tests/fixtures/sample.png').read_bytes() + containers = docker('compose', 'ps', '-q', 'api').splitlines() + assert len(containers) == 2 + report = {'replicas': []} + for container in containers: + alias = instance(container) + for attempt in range(20): + response = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=failover.png', 201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment') + if response['headers']['X-Instance-Id'] == alias: + break + else: + raise AssertionError('Target uploader never observed') + att = response['json'] + message = c.message(alice, chat, 'file before stop', attachments=[att['id']])['json'] + started = time.monotonic() + try: + docker('stop', '--time', '8', container) + state = json.loads(docker('inspect', '--format', '{{json .State}}', container)) + assert not state['Running'] + while True: + try: + data = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob) + assert data['headers']['X-Instance-Id'] != alias and data['bytes'] == content + break + except Failure: + assert time.monotonic() - started < 10 + time.sleep(.1) + key = uuid.uuid4().hex + write_failures = 0 + while True: + try: + c.message(alice, chat, 'write survives stop', key=key) + break + except Failure: + write_failures += 1 + assert time.monotonic() - started < 10 + time.sleep(.1) + report['replicas'].append({'container': container, 'uploader': alias, 'downloader': data['headers']['X-Instance-Id'], 'confirmed_stopped': True, 'transient_write_failures': write_failures, 'recovery_seconds': round(time.monotonic() - started, 3), 'sha256': hashlib.sha256(data['bytes']).hexdigest(), 'exit_code': state['ExitCode'], 'oom_killed': state['OOMKilled']}) + finally: + docker('compose', 'up', '-d', '--wait', '--no-deps', 'api') + wait(c) + # Send only part of a declared upload and terminate the TCP connection. + name = 'interrupted_' + uuid.uuid4().hex + '.png' + target = urlsplit(c.base) + connection = socket.create_connection((target.hostname, target.port), timeout=3) + headers = 'POST /api/v1/chats/' + chat['id'] + '/attachments?filename=' + name + ' HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer ' + alice['token'] + '\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\nConnection: close\r\n\r\n' + connection.sendall(headers.encode() + content[:30]) + connection.shutdown(socket.SHUT_WR) + connection.close() + time.sleep(.5) + rows = docker('compose', 'exec', '-T', 'api', 'python', '-c', "import sys; sys.path.insert(0,'src'); from postgres import setting; import psycopg; c=psycopg.connect(setting('DATABASE_URL')); print(c.execute('SELECT count(*) FROM attachments WHERE 1=1 AND filename = %s', ('" + name + "',)).fetchone()[0])") + assert rows == '0' + report['interrupted_before_validation_leaves_no_intent'] = True + # Observe API directly inside its container after the LB removes unready backends. + probe_code = """import sys, json, urllib.request, urllib.error +sys.path.insert(0,'src') +from postgres import Store +from common import APIError +s=Store() +try: + s.ready(); ready=200 +except APIError as e: ready=e.status +try: + s.objects.get(%r); content_status=200 +except APIError as e: content_status=e.status +live=urllib.request.urlopen('http://127.0.0.1:8080/health/live').status +print(json.dumps({'ready':ready,'content':content_status,'live':live})) +s.close() +""" % ('originals/' + att['id']) + try: + docker('compose', 'stop', 's3') + result = json.loads(docker('compose', 'exec', '-T', 'api', 'python', '-c', probe_code)) + assert result == {'ready': 503, 'content': 503, 'live': 200} + report['s3_outage'] = result + finally: + docker('compose', 'up', '-d', '--wait', '--no-deps', 's3') + wait(c) + assert c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob)['bytes'] == content + try: + docker('compose', 'stop', 'redis') + assert c.call('GET', '/api/v1/users/me', actor=alice, schema='User')['json'] == alice['user'] + c.message(alice, chat, 'Redis fallback') + report['redis_outage_fallback'] = True + finally: + docker('compose', 'up', '-d', '--wait', '--no-deps', 'redis') + wait(c) + print(json.dumps(report, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/student-tests/images.py b/student-tests/images.py new file mode 100644 index 00000000..906356fa --- /dev/null +++ b/student-tests/images.py @@ -0,0 +1,24 @@ +"""Small deterministic PNG helpers; generation happens outside measurement.""" +import random +import struct +import zlib + + +def chunk(name, data): + return struct.pack('!I', len(data)) + name + data + struct.pack('!I', zlib.crc32(name + data) & 0xffffffff) + + +def png(width, height, noise=False, rgb=True): + channels = 3 if rgb else 1 + rand = random.Random(2301) + encoded = zlib.compressobj(1) + parts = [] + for _ in range(height): + line = bytes(rand.getrandbits(8) for _ in range(width * channels)) if noise else b'\x7f' * (width * channels) + parts.append(encoded.compress(b'\0' + line)) + parts.append(encoded.flush()) + return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', width, height, 8, 2 if rgb else 0, 0, 0, 0)) + chunk(b'IDAT', b''.join(parts)) + chunk(b'IEND', b'') + + +def bomb(): + return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', 10000, 4001, 8, 2, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(b'x')) + chunk(b'IEND', b'') diff --git a/student-tests/plot.py b/student-tests/plot.py new file mode 100644 index 00000000..85d0cd7c --- /dev/null +++ b/student-tests/plot.py @@ -0,0 +1,36 @@ +"""Render only measured local evidence; optional matplotlib dependency.""" +import json +from pathlib import Path +import statistics + +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt + +ROOT = Path(__file__).resolve().parents[1] +evidence = ROOT / 'evidence' +plt.rcParams.update({'font.family': 'DejaVu Sans', 'font.size': 10}) +research = json.loads((evidence / 'reconciliation.json').read_text()) +groups = {} +for sample in research['scan_cost']: + groups.setdefault(sample['counts']['metadata'], []).append(sample['elapsed_seconds']) +fig, axes = plt.subplots(1, 2, figsize=(11, 4.2), layout='constrained') +for size, samples in groups.items(): + axes[0].scatter([size] * len(samples), [x * 1000 for x in samples], color='#4e79a7', alpha=.65) +axes[0].plot(sorted(groups), [statistics.median(groups[n]) * 1000 for n in sorted(groups)], color='#4e79a7', label='Медиана 3 прогонов') +axes[0].set(xlabel='Число строк метаданных', ylabel='Полный обход, мс', title='Сверка PostgreSQL + S3') +axes[0].legend() +uploads = json.loads((evidence / 'uploads.json').read_text())['experiments'] +labels = ['0,51 MiB\n0,18 Мп', '7,21 MiB\n2,52 Мп', '0,51 MiB\n40 Мп'] +for i in range(2): + values = [int(list(e['after'].values())[i]['memory.peak']) / 1048576 for e in uploads] + axes[1].bar([n + (i - .5) * .34 for n in range(3)], values, width=.34, label='API ' + str(i + 1)) +axes[1].axhline(512, color='#e15759', linestyle='--', label='Лимит на реплику') +axes[1].set(xticks=range(3), xticklabels=labels, ylabel='cgroup memory.peak, MiB', title='10 одновременных upload + download') +axes[1].legend(fontsize=8) +for ax in axes: + ax.grid(axis='y', alpha=.2) + ax.set_axisbelow(True) +fig.suptitle('ЛР 4 · измерения локального стенда · 27 октября 2026') +fig.savefig(evidence / 'research.png', dpi=160) +fig.savefig(evidence / 'research.svg') diff --git a/student-tests/research.py b/student-tests/research.py new file mode 100644 index 00000000..018f12d3 --- /dev/null +++ b/student-tests/research.py @@ -0,0 +1,123 @@ +"""Track 2 fault injection and measured traversal; use only synthetic lab4 data.""" +import concurrent.futures +import datetime as dt +import hashlib +import io +import json +from pathlib import Path +import sys +import time +import uuid + +import psycopg +from psycopg.rows import dict_row +sys.path[:0] = [str(Path.cwd() / 'tests'), '/app/src'] +from client import Client +from objects import Objects +from postgres import setting, Store +from reconcile import reconcile + + +def main(): + c = Client() + alice, bob, eve, chat = c.bootstrap() + good, content = c.attachment(alice, chat) + message = c.message(alice, chat, '', attachments=[good['id']])['json'] + sha = hashlib.sha256(content).hexdigest() + objects = Objects(setting) + report = {'method': 'real S3 and PostgreSQL, per-run fixed cutoff, synthetic data, 2s test grace; production >=3600s', 'checks': {}, 'passes': []} + with psycopg.connect(setting('DATABASE_URL'), autocommit=True, row_factory=dict_row) as conn: + def row(attachment_id): + return conn.execute('SELECT * FROM attachments WHERE 1=1 AND id = %s', (attachment_id,)).fetchone() + def aged(attachment_id): + conn.execute("UPDATE attachments SET updated_at = clock_timestamp() - INTERVAL '2 hours' WHERE 1=1 AND id = %s", (attachment_id,)) + def intent(state='uploading'): + aid = uuid.uuid4() + conn.execute(''' + INSERT INTO attachments (id, chat_id, owner_id, filename, object_key, content_type, size_bytes, sha256, state, updated_at) + VALUES (%s, %s, %s, 'research.png', %s, 'image/png', %s, %s, %s, clock_timestamp() - INTERVAL '2 hours') + ''', (aid, chat['id'], alice['id'], 'originals/' + str(aid), len(content), sha, state)) + return str(aid) + # Fail precisely after successful S3 PUT, before the ready UPDATE/COMMIT. + broken_name = 'fault_' + uuid.uuid4().hex + '.png' + trigger = 'research_' + uuid.uuid4().hex + try: + from psycopg import sql + conn.execute(sql.SQL('''CREATE FUNCTION {}() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN + IF NEW.state = 'ready' AND NEW.filename = '{}' THEN + RAISE EXCEPTION 'synthetic research failure'; + END IF; + RETURN NEW; + END $$''').format(sql.Identifier(trigger), sql.SQL(broken_name))) + conn.execute(sql.SQL('CREATE TRIGGER {} BEFORE UPDATE ON attachments FOR EACH ROW EXECUTE FUNCTION {}()').format(sql.Identifier(trigger), sql.Identifier(trigger))) + c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + broken_name, 503, actor=alice, raw=content, headers={'Content-Type': 'image/png'}) + failed = conn.execute('SELECT id, state, object_key FROM attachments WHERE 1=1 AND filename = %s', (broken_name,)).fetchone() + assert failed['state'] == 'uploading' and objects.head(failed['object_key']) is not None + aged(failed['id']) + finally: + conn.execute(sql.SQL('DROP TRIGGER IF EXISTS {} ON attachments').format(sql.Identifier(trigger))) + conn.execute(sql.SQL('DROP FUNCTION IF EXISTS {}()').format(sql.Identifier(trigger))) + absent = intent() # DB intent exists, S3 PUT never happened (process died first). + missing, _ = c.attachment(alice, chat) + c.message(alice, chat, 'keep missing reference', attachments=[missing['id']]) + missing_key = 'originals/' + missing['id'] + objects.delete(missing_key) # S3-side loss; referenced metadata must never be erased. + aged(missing['id']) + orphan_key = 'originals/' + str(uuid.uuid4()) + objects.put(orphan_key, io.BytesIO(content), len(content), 'image/png', sha) # no DB row + aged(good['id']) + # Wait real object age; don't forge LastModified or replace the S3 clock. + time.sleep(3) + fresh_key = 'originals/' + str(uuid.uuid4()) + objects.put(fresh_key, io.BytesIO(content), len(content), 'image/png', sha) + fresh_intent = intent() + conn.execute('UPDATE attachments SET updated_at = clock_timestamp() WHERE 1=1 AND id = %s', (fresh_intent,)) + locked = intent() + # Hold the same lock as a live uploader while reconciliation runs. + with psycopg.connect(setting('DATABASE_URL')) as uploading: + uploading.execute('SELECT id FROM attachments WHERE 1=1 AND id = %s FOR UPDATE', (locked,)) + dry = reconcile(False, 2) + assert row(failed['id']) and row(absent) and row(missing['id'])['state'] == 'ready' + assert objects.head(orphan_key) is not None + report['checks']['dry_run_mutates_nothing'] = True + fixed = reconcile(True, 2) + assert row(failed['id']) is None and row(absent) is None + assert objects.head(failed['object_key']) is None and objects.head(orphan_key) is None + assert row(missing['id'])['state'] == 'storage_missing' + assert row(locked) is not None and fixed['counts']['skipped_locked'] >= 1 + assert row(fresh_intent) is not None and objects.head(fresh_key) is not None + assert row(good['id'])['state'] == 'ready' and objects.head('originals/' + good['id']) is not None + report['checks'].update(stale_intents_and_orphans_cleaned=True, fresh_and_locked_retained=True, used_ready_retained=True, missing_reference_retained=True) + repeat = reconcile(True, 2) + assert repeat['counts']['deleted_objects'] == 0 and repeat['counts']['marked_missing'] == 0 + report['checks']['apply_repeat_idempotent'] = True + c.call('GET', '/api/v1/attachments/' + missing['id'] + '/content', 503, actor=bob) + # Restore exact original from the test's external copy; reconciler can't invent lost bytes. + objects.put(missing_key, io.BytesIO(content), len(content), 'image/png', sha) + aged(missing['id']) + recovered = reconcile(True, 2) + assert row(missing['id'])['state'] == 'ready' + assert c.call('GET', '/api/v1/attachments/' + missing['id'] + '/content', actor=bob)['bytes'] == content + report['checks']['restore_after_sha256_verification'] = True + # Verify metadata FK and existing message survive every repair. + assert conn.execute('SELECT 1 FROM message_attachments WHERE 1=1 AND message_id = %s AND attachment_id = %s', (message['id'], good['id'])).fetchone() + report['passes'] = [dict(name=n, **v) for n, v in [('dry_run', dry), ('apply', fixed), ('repeat', repeat), ('recover', recovered)]] + # Clean up only new unattached diagnostic keys/intents; confirmed files remain. + objects.delete(fresh_key) + for aid in [fresh_intent, locked]: + conn.execute("DELETE FROM attachments WHERE 1=1 AND id = %s AND state = 'uploading'", (aid,)) + # Measure bounded scans at three populations; 3 repeats, no writes during scan. + report['scan_cost'] = [] + for extra in [0, 100, 400]: + for _ in range(extra): + aid = intent('ready') + objects.put('originals/' + aid, io.BytesIO(content), len(content), 'image/png', sha) + for repetition in range(3): + scan = reconcile(False, 2) + report['scan_cost'].append({'repetition': repetition + 1, **scan}) + print(json.dumps(report, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/student-tests/slow_upload.py b/student-tests/slow_upload.py new file mode 100644 index 00000000..c17d2b01 --- /dev/null +++ b/student-tests/slow_upload.py @@ -0,0 +1,32 @@ +"""A continuously trickling client still reaches the absolute upload deadline.""" +import json +from pathlib import Path +import select +import socket +import sys +import time +import uuid +from urllib.parse import urlsplit +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client + +c = Client() +alice, bob, eve, chat = c.bootstrap() +target = urlsplit(c.base) +connection = socket.create_connection((target.hostname, target.port), timeout=3) +header = 'POST /api/v1/chats/' + chat['id'] + '/attachments?filename=slow_' + uuid.uuid4().hex + '.png HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer ' + alice['token'] + '\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\nConnection: close\r\n\r\n' +started = time.monotonic() +connection.sendall(header.encode()) +response = b'' +while time.monotonic() - started < 20: + if select.select([connection], [], [], .15)[0]: + response += connection.recv(4096) + if b'\r\n\r\n' in response: + break + connection.sendall(b'x') +connection.close() +elapsed = time.monotonic() - started +assert response.startswith(b'HTTP/1.1 400') or response.startswith(b'HTTP/1.0 400'), response[:100] +assert 14 <= elapsed < 19 +assert b'x-instance-id:' in response.lower() and b'x-request-id:' in response.lower() +print(json.dumps({'trickle_seconds': round(elapsed, 3), 'http_status': 400, 'absolute_deadline_enforced': True})) diff --git a/student-tests/storage_checks.py b/student-tests/storage_checks.py new file mode 100644 index 00000000..7e100d87 --- /dev/null +++ b/student-tests/storage_checks.py @@ -0,0 +1,69 @@ +"""Execute with the isolated tools profile; inject faults only into new test IDs.""" +import hashlib +import io +import json +from pathlib import Path +import socket +import sys +import urllib.error +import urllib.request +import uuid + +sys.path[:0] = [str(Path.cwd() / 'tests'), '/app/src'] +from botocore.exceptions import ClientError +from PIL import Image +from client import Client +from objects import Objects +from postgres import setting + + +def main(): + c = Client() + alice, bob, eve, chat = c.bootstrap() + att, content = c.attachment(alice, chat) + api = Objects(setting) + admin = Objects(lambda key: Path('/run/secrets/s3_admin_' + ('key' if key.endswith('_ID') else 'secret')).read_text().strip()) + key = 'originals/' + att['id'] + assert admin.head(key)['ContentLength'] == len(content) + denied = [] + for path in ('/' + api.bucket + '/' + key, '/' + api.bucket + '?list-type=2'): + try: + urllib.request.urlopen('http://s3:8333' + path, timeout=3) + raise AssertionError('Anonymous S3 access allowed') + except urllib.error.HTTPError as error: + assert error.code == 403 + denied.append(error.code) + # API credentials cannot create a bucket, read another bucket or change policy. + other = 'test-' + uuid.uuid4().hex + admin.client.create_bucket(Bucket=other) + rejected = [] + try: + actions = [lambda: api.client.create_bucket(Bucket='forbidden-' + uuid.uuid4().hex), lambda: api.client.list_objects_v2(Bucket=other), lambda: api.client.put_bucket_policy(Bucket=api.bucket, Policy=json.dumps({'Version': '2012-10-17', 'Statement': []}))] + for action in actions: + try: + action() + raise AssertionError('API credentials have excessive permissions') + except ClientError as error: + assert error.response['ResponseMetadata']['HTTPStatusCode'] == 403 + rejected.append(403) + finally: + admin.client.delete_bucket(Bucket=other) + for host, port in [('s3-store', 8888), ('s3', 12535)]: + try: + connection = socket.create_connection((host, port), timeout=1) + connection.close() + raise AssertionError('Administrative/internal endpoint is exposed') + except OSError: + pass + image = io.BytesIO() + Image.new('RGB', (320, 200), (20, 150, 210)).save(image, format='JPEG') + jpg = image.getvalue() + uploaded = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=photo.jpg', 201, actor=alice, raw=jpg, headers={'Content-Type': 'image/jpeg'}, schema='Attachment')['json'] + downloaded = c.call('GET', '/api/v1/attachments/' + uploaded['id'] + '/content', actor=bob) + assert downloaded['bytes'] == jpg and uploaded['sha256'] == hashlib.sha256(jpg).hexdigest() + c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=bad.jpg', 415, actor=alice, raw=jpg[:200], headers={'Content-Type': 'image/jpeg'}) + print(json.dumps({'anonymous_statuses': denied, 'api_credentials_denied': rejected, 'internal_filer_and_admin_inaccessible': True, 'jpeg_byte_roundtrip': True, 'damaged_jpeg_415': True}, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..45f0d800 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,177 @@ +"""Extra black-box tests, without changing the course's public tests.""" +import concurrent.futures +from pathlib import Path +import sys +import unittest +from urllib.parse import quote +import uuid + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client as PublicClient, LAB + + +class Client(PublicClient): + def call(self, method, path, *args, headers=None, **kwargs): + headers = dict(headers or {}) + if method == 'POST' and path.endswith('/messages'): + headers.setdefault('Idempotency-Key', uuid.uuid4().hex) + return super().call(method, path, *args, headers=headers, **kwargs) + + +class APIContract(unittest.TestCase): + def setUp(self): + self.client = Client() + self.alice, self.bob, self.eve, self.chat = self.client.bootstrap() + self.path = '/api/v1/chats/' + self.chat['id'] + + def test_malformed_json_and_extra_fields(self): + for raw in (b'{', b'[]', b'{"text":"x","text":"y"}', b'{"text":NaN}', b'{"text":"\\ud800"}'): + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, raw=raw, headers={'Content-Type': 'application/json'}) + for body in ({'text': 'ok', 'sender_id': self.eve['id']}, {'text': True}, {'text': ' '}, {'text': ''}, {'text': None}): + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, body=body) + deep = b'{"text":' + b'[' * 1200 + b'0' + b']' * 1200 + b'}' + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, raw=deep, headers={'Content-Type': 'application/json'}) + + def test_unicode_limits(self): + result = self.client.message(self.alice, self.chat, '👩' * 4000)['json'] + self.assertEqual(result['text'], '👩' * 4000) + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, body={'text': '👩' * 4001}) + + def test_unicode_nul_round_trip(self): + import base64 + value = 'A\x00B\\u0000👩' + registration = dict(self.alice['registration'], username='nul_' + uuid.uuid4().hex[:20], display_name=value) + user = self.client.call('POST', '/api/v1/users', 201, body=registration, schema='User')['json'] + self.assertEqual(user['display_name'], value) + credential = base64.b64encode((registration['username'] + ':' + registration['password']).encode()).decode() + session = self.client.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': 'Basic ' + credential}, schema='Session')['json'] + self.assertEqual(session['user'], user) + actor = {'id': user['id'], 'token': session['token']} + # Exercise the database path and then the Redis profile cache. + for _ in range(2): + self.assertEqual(self.client.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'], user) + chat = self.client.call('POST', '/api/v1/chats', 201, actor=actor, body={'title': value, 'member_ids': [actor['id']]}, schema='Chat')['json'] + self.assertEqual(chat['title'], value) + self.assertEqual(self.client.call('GET', '/api/v1/chats/' + chat['id'], actor=actor, schema='Chat')['json'], chat) + contents = [value, '\x00' * 4000, '👩' * 3999 + '\x00'] + for content in contents: + self.assertEqual(self.client.message(actor, chat, content)['json']['text'], content) + history = self.client.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=actor, schema='MessagePage')['json'] + self.assertEqual([message['text'] for message in history['items']], contents) + + def test_foreign_chat(self): + for suffix in ('', '/messages'): + self.client.call('GET', self.path + suffix, 404, actor=self.eve) + self.client.call('POST', self.path + '/messages', 404, actor=self.eve, body={'text': 'hidden'}) + self.client.call('GET', '/api/v1/chats/' + str(uuid.uuid4()), 404, actor=self.alice) + + def test_registration_race(self): + body = dict(self.alice['registration'], username='race_' + uuid.uuid4().hex[:20]) + def register(_): + return Client().call('POST', '/api/v1/users', [201, 409], body=body)['status'] + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + statuses = list(executor.map(register, range(10))) + self.assertEqual(statuses.count(201), 1) + self.assertEqual(statuses.count(409), 9) + + def test_invalid_chat_creation(self): + members = ([self.alice['id'], self.alice['id']], [self.bob['id']], [self.alice['id'], str(uuid.uuid4())], [], [True]) + for member_ids in members: + self.client.call('POST', '/api/v1/chats', 400, actor=self.alice, body={'title': 'rollback', 'member_ids': member_ids}) + + def test_cursor_scope_and_validation(self): + for i in range(3): + self.client.message(self.alice, self.chat, str(i)) + cursor = self.client.call('GET', self.path + '/messages?limit=1', actor=self.alice, schema='MessagePage')['json']['next_cursor'] + other = self.client.call('POST', '/api/v1/chats', 201, actor=self.alice, body={'title': 'other', 'member_ids': [self.alice['id']]}, schema='Chat')['json'] + self.client.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + quote(cursor), 400, actor=self.alice) + for query in ('limit=0', 'limit=101', 'limit=' + '9' * 5000, 'limit=true', 'limit=1&limit=2', 'cursor=', 'cursor=bad', 'cursor=e30=', 'offset=1'): + self.client.call('GET', self.path + '/messages?' + query, 400, actor=self.alice) + + def test_concurrent_messages_and_pagination(self): + def send(i): + return Client().message(self.alice if i % 2 else self.bob, self.chat, 'parallel-' + str(i))['json'] + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + sent = list(executor.map(send, range(100))) + found = [] + cursor = None + while True: + url = self.path + '/messages?limit=7' + ('&cursor=' + quote(cursor) if cursor else '') + page = self.client.call('GET', url, actor=self.alice, schema='MessagePage')['json'] + found.extend(page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + self.assertEqual(len(found), 100) + self.assertEqual(len({v['id'] for v in found}), 100) + self.assertEqual({v['id'] for v in found}, {v['id'] for v in sent}) + self.assertEqual(found, sorted(found, key=lambda v: (v['created_at'], v['id']))) + + def test_idempotency_concurrent_replicas(self): + key = uuid.uuid4().hex + def send(_): + return Client().message(self.alice, self.chat, 'Один раз\x00👩', key=key) + with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: + replies = list(executor.map(send, range(32))) + self.assertGreaterEqual(len({v['headers']['X-Instance-Id'] for v in replies}), 2) + self.assertTrue(all(v['json'] == replies[0]['json'] for v in replies)) + page = self.client.call('GET', self.path + '/messages', actor=self.bob, schema='MessagePage')['json'] + self.assertEqual(page['items'], [replies[0]['json']]) + self.client.call('POST', self.path + '/messages', 409, actor=self.alice, body={'text': 'different'}, headers={'Idempotency-Key': key}) + self.client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=self.alice) + self.client.call('POST', self.path + '/messages', 401, actor=self.alice, body={'text': 'Один раз\x00👩'}, headers={'Idempotency-Key': key}) + + def test_protocol_error_headers(self): + self.client.call('TRACE', '/api/v1/users/me', 400, actor=self.alice) + + def test_idempotency_scope(self): + key = uuid.uuid4().hex + one = self.client.message(self.alice, self.chat, 'same', key=key)['json'] + two = self.client.message(self.bob, self.chat, 'same', key=key)['json'] + other = self.client.call('POST', '/api/v1/chats', 201, actor=self.alice, body={'title': 'scope', 'member_ids': [self.alice['id']]}, schema='Chat')['json'] + three = self.client.message(self.alice, other, 'same', key=key)['json'] + self.assertEqual(len({one['id'], two['id'], three['id']}), 3) + + def test_idempotency_semantic_body(self): + key = uuid.uuid4().hex + one = self.client.message(self.alice, self.chat, 'semantic', key=key)['json'] + two = self.client.call('POST', self.path + '/messages', 201, actor=self.alice, body={'attachment_ids': [], 'text': 'semantic'}, headers={'Idempotency-Key': key}, schema='Message')['json'] + self.assertEqual(one, two) + + def test_idempotency_invalid_keys(self): + for key in (None, '', 'x' * 129, 'space key', 'bad,key', 'slash/key'): + headers = {} if key is None else {'Idempotency-Key': key} + PublicClient().call('POST', self.path + '/messages', 400, actor=self.alice, body={'text': 'valid'}, headers=headers) + for raw in (b'{"text":true}', b'{"text":"ok","extra":1}', b'{"text":"ok","attachment_ids":false}'): + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, raw=raw, headers={'Content-Type': 'application/json'}) + + def test_idempotency_conflicting_race(self): + key = uuid.uuid4().hex + def send(i): + return Client().call('POST', self.path + '/messages', [201, 409], actor=self.alice, body={'text': str(i % 2)}, headers={'Idempotency-Key': key}, schema=None) + with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: + replies = list(executor.map(send, range(32))) + self.assertEqual(sum(v['status'] == 201 for v in replies), 16) + self.assertEqual(sum(v['status'] == 409 for v in replies), 16) + page = self.client.call('GET', self.path + '/messages', actor=self.alice, schema='MessagePage')['json'] + self.assertEqual(len(page['items']), 1) + + @unittest.skipUnless(LAB >= 2, 'sessions start in lab2') + def test_unicode_password_basic_and_logout(self): + import base64 + registration = {'username': 'unicode_' + uuid.uuid4().hex[:16], 'display_name': 'Софья', 'password': '🔐' * 127 + ':'} + self.client.call('POST', '/api/v1/users', 201, body=registration) + encoded = base64.b64encode((registration['username'] + ':' + registration['password']).encode()).decode() + headers = {'Authorization': 'Basic ' + encoded} + session = self.client.call('POST', '/api/v1/auth/sessions', 201, headers=headers, schema='Session')['json'] + actor = {'id': session['user']['id'], 'token': session['token']} + self.client.call('GET', '/api/v1/users/me', actor=actor) + self.client.call('GET', '/api/v1/users/me', 401, headers=headers) + for authorization in ('Basic !!!', 'Bearer wrong', '', 'Basic Zm9v'): + self.client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': authorization}) + self.client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor) + self.client.call('GET', '/api/v1/users/me', 401, actor=actor) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py new file mode 100644 index 00000000..6cd6d017 --- /dev/null +++ b/student-tests/test_attachments.py @@ -0,0 +1,83 @@ +import concurrent.futures +from pathlib import Path +import sys +import unittest +from urllib.parse import quote +import uuid + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client +from images import bomb, png + + +class Attachments(unittest.TestCase): + def setUp(self): + self.c = Client() + self.alice, self.bob, self.eve, self.chat = self.c.bootstrap() + self.path = '/api/v1/chats/' + self.chat['id'] + self.image = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes() + + def upload(self, content=None, name='photo.png', mime='image/png', status=201, actor=None): + return self.c.call('POST', self.path + '/attachments?filename=' + quote(name, safe=''), status, actor=actor or self.alice, raw=self.image if content is None else content, headers={'Content-Type': mime}, schema='Attachment' if status == 201 else None) + + def test_binary_roundtrip_and_metadata(self): + att = self.upload(name='Софья 👩‍💻.png')['json'] + self.assertEqual(att['filename'], 'Софья 👩‍💻.png') + self.assertEqual(att['variants'], []) + self.assertIsNone(att['error_code']) + for _ in range(4): + metadata = self.c.call('GET', '/api/v1/attachments/' + att['id'], actor=self.bob, schema='Attachment')['json'] + self.assertEqual(metadata, att) + response = self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', actor=self.bob) + self.assertEqual(response['bytes'], self.image) + self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 404, actor=self.bob) + + def test_bad_images_and_mime(self): + for raw in (b'not image', b'', self.image[:40], self.image[:-30]): + self.upload(raw, status=415) + self.upload(mime='image/jpeg', status=415) + self.upload(mime='application/octet-stream', status=415) + self.upload(bomb(), status=413) + self.upload(b'x' * (10 * 1024 * 1024 + 1), status=413) + + def test_dangerous_names_and_query(self): + for name in ('', ' ', '../a.png', 'a/b.png', 'a\\b.png', '\0.png', 'a\n.png', 'a\x7f.png', 'a' * 256): + self.upload(name=name, status=400) + for query in ('', '?filename=a&filename=b', '?filename=a&extra=x'): + self.c.call('POST', self.path + '/attachments' + query, 400, actor=self.alice, raw=self.image, headers={'Content-Type': 'image/png'}) + + def test_access_and_cross_chat(self): + att = self.upload()['json'] + for suffix in ('', '/content'): + self.c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 404, actor=self.eve) + self.c.call('GET', '/api/v1/attachments/' + att['id'] + suffix, 401) + self.upload(actor=self.eve, status=404) + other = self.c.call('POST', '/api/v1/chats', 201, actor=self.alice, body={'title': 'other', 'member_ids': [self.alice['id']]}, schema='Chat')['json'] + self.c.call('POST', '/api/v1/chats/' + other['id'] + '/messages', 404, actor=self.alice, body={'text': '', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': uuid.uuid4().hex}) + self.c.call('POST', self.path + '/messages', 404, actor=self.alice, body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex}) + + def test_attachment_message_replay(self): + one, two = self.upload()['json'], self.upload()['json'] + key = uuid.uuid4().hex + def send(_): + return Client().message(self.bob, self.chat, '', key=key, attachments=[one['id'], two['id']])['json'] + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + replies = list(executor.map(send, range(20))) + self.assertTrue(all(r == replies[0] for r in replies)) + self.assertEqual(replies[0]['attachment_ids'], [one['id'], two['id']]) + self.c.call('POST', self.path + '/messages', 409, actor=self.bob, body={'text': '', 'attachment_ids': [two['id'], one['id']]}, headers={'Idempotency-Key': key}) + history = self.c.call('GET', self.path + '/messages', actor=self.alice, schema='MessagePage')['json'] + self.assertEqual(history['items'], [replies[0]]) + for ids in ([one['id']] * 2, [True], 'bad', [str(uuid.uuid4()) for _ in range(11)]): + self.c.call('POST', self.path + '/messages', 400, actor=self.alice, body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex}) + + def test_valid_pixel_boundary(self): + # Exactly 40M decoded grayscale pixels, small compressed upload. + content = png(10000, 4000, rgb=False) + att = self.upload(content)['json'] + response = self.c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=self.bob) + self.assertEqual(response['bytes'], content) + + +if __name__ == '__main__': + unittest.main(verbosity=2) diff --git a/student-tests/uploads.py b/student-tests/uploads.py new file mode 100644 index 00000000..c664743a --- /dev/null +++ b/student-tests/uploads.py @@ -0,0 +1,75 @@ +""">=10 simultaneous uploads with sampled cgroup memory and true cgroup peak.""" +import concurrent.futures +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import threading +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client +from images import png + + +def docker(*args): + return subprocess.check_output(['docker', *args], text=True).strip() + + +def memory(container): + code = "import json; from pathlib import Path; p=Path('/sys/fs/cgroup'); print(json.dumps({k:(p/k).read_text().strip() for k in ['memory.current','memory.peak','memory.events','cpu.stat']}))" + return json.loads(docker('exec', container, 'python', '-c', code)) + + +def main(): + c = Client() + alice, bob, eve, chat = c.bootstrap() + records = [] + for width, height, noise, rgb in [(420, 420, True, True), (1800, 1400, True, True), (10000, 4000, False, True)]: + content = png(width, height, noise=noise, rgb=rgb) + assert len(content) <= 10 * 1024 * 1024 + digest = hashlib.sha256(content).hexdigest() + # Fresh API processes make memory.peak attributable to this one experiment. + docker('compose', 'restart', 'api') + docker('compose', 'up', '-d', '--wait', '--no-deps', 'api') + time.sleep(2) + containers = docker('compose', 'ps', '-q', 'api').splitlines() + before = {v: memory(v) for v in containers} + samples = [] + done = threading.Event() + started = time.monotonic() + def sample(): + while not done.is_set(): + samples.append({'seconds': round(time.monotonic() - started, 4), 'memory': {v: memory(v) for v in containers}}) + done.wait(.05) + thread = threading.Thread(target=sample) + thread.start() + barrier = threading.Barrier(10) + def upload(i): + barrier.wait() + begin = time.monotonic() + response = Client(timeout=30).call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=concurrent.png', [201, 503], actor=alice, raw=content, headers={'Content-Type': 'image/png'}) + if response['status'] == 201: + # Parse because schema omitted to allow a documented admission rejection. + attachment = json.loads(response['bytes']) + assert attachment['sha256'] == digest + downloaded = Client(timeout=30).call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob) + assert hashlib.sha256(downloaded['bytes']).hexdigest() == digest + return {'index': i, 'status': response['status'], 'instance': response['headers']['X-Instance-Id'], 'elapsed_seconds': round(time.monotonic() - begin, 6)} + try: + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + results = list(executor.map(upload, range(10))) + finally: + done.set() + thread.join() + after = {v: memory(v) for v in containers} + for m in after.values(): + assert 'oom_kill 0' in m['memory.events'] + records.append({'width': width, 'height': height, 'bytes': len(content), 'pixels': width * height, 'clients': 10, 'before': before, 'after': after, 'samples': samples, 'results': results, 'seconds': round(time.monotonic() - started, 6)}) + print(json.dumps({'memory_source': 'Linux cgroup v2: memory.current, memory.peak (lifetime peak; API restarted before each case), memory.events; samples include tmpfs and kernel charges, not Python heap alone', 'experiments': records}, indent=2)) + + +if __name__ == '__main__': + main()