ЛР 3 — БПИ2303 — Любченко — на 4 #115
+4
-12
@@ -1,13 +1,5 @@
|
||||
# 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
|
||||
# cp .env.example .env; python3 scripts/bootstrap.py
|
||||
PUBLIC_PORT=8080
|
||||
API_IMAGE_TAG=local
|
||||
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
|
||||
# Shared credentials and CURSOR_SECRET are generated in secrets/ (never in Git).
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
#
|
||||
# MTUSI Messenger, lab3 — PostgreSQL + Redis/Valkey HTTP API.
|
||||
# Base image is pinned by tag *and* digest (multi-arch manifest list), so a rebuild
|
||||
# on another machine with the same platform yields the same layers.
|
||||
#
|
||||
# Runtime dependencies: psycopg2-binary for PostgreSQL, redis for the session cache.
|
||||
# Development-only tooling lives in requirements-dev.txt and never enters this image.
|
||||
FROM python:3.12-slim-bookworm@sha256:392307d22300de8b5986851a12d9176dfc0fc073e65bf6523ebd7dcbeb23564e
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONPATH=/app/src
|
||||
|
||||
# Pinned runtime dependencies (versions fixed for reproducible builds).
|
||||
RUN pip install --no-cache-dir \
|
||||
psycopg2-binary==2.9.11 \
|
||||
redis==7.0.1 \
|
||||
bcrypt==4.0.1
|
||||
|
||||
# Unprivileged runtime account; the image's default root user is never used at runtime.
|
||||
RUN groupadd --gid 10001 app \
|
||||
&& useradd --uid 10001 --gid 10001 --create-home --shell /usr/sbin/nologin app
|
||||
|
||||
ARG APP_REVISION=local
|
||||
ENV APP_REVISION=$APP_REVISION
|
||||
|
||||
WORKDIR /app
|
||||
COPY --chown=10001:10001 src/ /app/src/
|
||||
COPY --chown=10001:10001 migrations/ /app/migrations/
|
||||
|
||||
USER 10001:10001
|
||||
|
||||
# Configuration comes from the environment; secrets are read from files at runtime.
|
||||
ENV APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_ENV=lab \
|
||||
LOG_LEVEL=info \
|
||||
SHUTDOWN_GRACE_SECONDS=8 \
|
||||
SESSION_TTL_SECONDS=3600 \
|
||||
SESSION_STORE=redis \
|
||||
DB_POOL_MIN=2 \
|
||||
DB_POOL_MAX=10 \
|
||||
DB_CONNECT_TIMEOUT=5 \
|
||||
DB_STATEMENT_TIMEOUT=10
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
# Python (not curl) keeps the image slim; /health/ready is the synchronous-path probe.
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \
|
||||
CMD ["python", "-c", "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:%s/health/ready' % os.environ.get('APP_PORT', '8080'), timeout=2)"]
|
||||
|
||||
STOPSIGNAL SIGTERM
|
||||
|
||||
# The HTTP process is PID 1 and handles SIGTERM itself (graceful drain, exit code 0).
|
||||
CMD ["python", "-m", "messenger"]
|
||||
@@ -1,5 +1,6 @@
|
||||
PYTHON ?= python3
|
||||
BASE_URL ?= http://localhost:8080
|
||||
PUBLIC_PORT ?= $(or $(shell sed -n 's/^PUBLIC_PORT=//p' .env 2>/dev/null),8080)
|
||||
BASE_URL ?= http://localhost:$(PUBLIC_PORT)
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
@@ -28,3 +29,52 @@ 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: unit-test student-test concurrency lab3-test
|
||||
unit-test:
|
||||
docker compose run --rm --no-deps -v "$(CURDIR)/student-tests:/app/student-tests:ro" api-a python /app/student-tests/unit_test.py
|
||||
|
||||
student-test:
|
||||
$(PYTHON) student-tests/run.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
concurrency:
|
||||
$(PYTHON) student-tests/concurrency.py --base-url "$(BASE_URL)" --clients 10 --messages-per-client 10 --output evidence/concurrency.json $(TLS_ARGS)
|
||||
|
||||
lab3-test:
|
||||
$(PYTHON) student-tests/lab3.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
.PHONY: bootstrap up restore rolling-update test-dependencies test-expiry test-readiness test-failover-load test-rolling-load
|
||||
bootstrap:
|
||||
$(PYTHON) scripts/bootstrap.py
|
||||
|
||||
up:
|
||||
docker compose --env-file .env up -d --build
|
||||
|
||||
restore:
|
||||
$(PYTHON) scripts/manage.py restore
|
||||
|
||||
rolling-update:
|
||||
scripts/rolling-update.sh
|
||||
|
||||
test-dependencies:
|
||||
$(PYTHON) student-tests/dependencies.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-expiry:
|
||||
SESSION_TTL_SECONDS=2 docker compose up -d --no-deps --force-recreate api-a api-b
|
||||
$(PYTHON) scripts/manage.py restore
|
||||
@status=0; $(PYTHON) student-tests/dependencies.py --mode expiry --base-url "$(BASE_URL)" $(TLS_ARGS) || status=$$?; \
|
||||
docker compose up -d --no-deps --force-recreate api-a api-b; \
|
||||
$(PYTHON) scripts/manage.py restore; exit $$status
|
||||
|
||||
test-readiness:
|
||||
$(PYTHON) student-tests/readiness.py
|
||||
|
||||
test-failover-load:
|
||||
$(PYTHON) student-tests/experiments.py --base-url "$(BASE_URL)" --scenario failover $(TLS_ARGS)
|
||||
|
||||
test-rolling-load:
|
||||
$(PYTHON) student-tests/experiments.py --base-url "$(BASE_URL)" --scenario rolling $(TLS_ARGS)
|
||||
|
||||
.PHONY: test-storage
|
||||
test-storage:
|
||||
docker compose run --rm --no-deps -v "$(CURDIR)/student-tests:/app/student-tests:ro" api-a python /app/student-tests/storage.py
|
||||
|
||||
@@ -1,37 +1,203 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Любченко Никита, БПИ2303.
|
||||
- Целевая оценка: **4** — весь уровень 3 и все пункты уровня 4. Исследовательский трек оценки 5 не выбирался.
|
||||
- Commit/PR: автор добавит после проверки и публикации; команды Git при реализации не выполнялись.
|
||||
- Использованные источники, библиотеки и помощь ИИ: собственная реализация lab2; контракт `contracts/openapi.json` и `contracts/README.md`; Python 3.12, `psycopg2-binary==2.9.11`, `redis==7.0.1`, `bcrypt==4.0.1`. ИИ использовался для реализации, регрессионных тестов, проверки кода и подготовки отчёта. Результаты ниже получены реальными прогонами синтетического стенда, не перенесены из отчёта lab2.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны работающий Docker Engine с Compose v2+, `make`, Python 3.9+ на хосте для тестов и операторских скриптов. Сервер и его зависимости работают внутри образа. Публичный маршрут — через Nginx.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
python3 scripts/bootstrap.py
|
||||
make check
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
python3 scripts/manage.py restore
|
||||
make test
|
||||
make unit-test
|
||||
make student-test
|
||||
make lab3-test
|
||||
make concurrency
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
python3 scripts/manage.py restore
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
python3 scripts/manage.py restore
|
||||
```
|
||||
|
||||
`bootstrap.py` создаёт `secrets/` с доступом только владельцу каталога и случайные значения PostgreSQL/password, URL и общего ключа курсоров. Существующие файлы не перезаписываются. `.env` и `secrets/` исключены из Git и Docker build context. Не копировать секреты из lab2. API запускается непривилегированным UID 10001. Docker Desktop позволяет API читать смонтированные Compose secrets; на иной платформе необходимо обеспечить чтение файлов этим UID, не публикуя их в репозитории.
|
||||
|
||||
По умолчанию `.env.example` задаёт `PUBLIC_PORT=8080`. В проверенном окружении lab2 уже занимала 8080, поэтому локальный `.env` lab3 содержит `PUBLIC_PORT=18080`: адрес стенда — `http://localhost:18080`. Makefile читает `PUBLIC_PORT` из `.env`; `BASE_URL` можно переопределить явно. Стенд lab2 не останавливался, его файлы не менялись.
|
||||
|
||||
Для полной валидации OpenAPI и проверки KDF в том же runtime, что у сервера:
|
||||
|
||||
```sh
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
docker run --rm -v "$PWD/student-tests:/app/student-tests:ro" \
|
||||
mtusi-lab3-api:local python /app/student-tests/unit_test.py
|
||||
```
|
||||
|
||||
`make unit-test` запускает все пять проверок внутри API-образа, поэтому runtime-зависимости не требуется устанавливать на хост. При ручном запуске `unit_test.py` хостовым Python без `hashlib.scrypt` тест нативного scrypt пропускается. Локальная OpenAPI-валидация прошла с предупреждением urllib3 о LibreSSL хостового Python; это предупреждение инструмента проверки, не отказ сервера.
|
||||
|
||||
Безопасная остановка: `docker compose down` — volumes остаются. `scripts/restart.sh` перезапускает обе API, Nginx, PostgreSQL и Redis, ждёт готовности, не удаляет данные и не запускает migration-service повторно. Намеренное удаление **только учебных данных этой lab3**: `docker compose down -v`. При удалении только `secrets/` с сохранением PostgreSQL volume новый пароль не совпадёт со старым: сохранять секреты вместе с данными либо осознанно пересоздавать оба.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```text
|
||||
HTTP-клиент → Nginx :8080 → api-a ─┬→ PostgreSQL
|
||||
→ api-b ─┤ users/chats/members/messages/sessions/idempotency
|
||||
└→ Redis (кэш сессий с TTL)
|
||||
|
||||
migrate → PostgreSQL (отдельный завершающийся процесс перед API)
|
||||
healthwatch внутри Nginx → /health/ready каждой API → upstream + reload
|
||||
```
|
||||
|
||||
Сохранены структура `src/messenger/`, Basic-вход с opaque Bearer-сессиями, password KDF, PostgreSQL, Redis-кэш, ключевая пагинация, транзакции и структурные JSON-логи lab2. Новое в lab3: `X-Instance-Id`, общая идемпотентность, отдельные версионируемые миграции, Nginx, управление репликами и эксперименты. При переносе исправлены HTTP `Content-Length`, учёт активного запроса до завершения отправки ответа, обнаружение нативного scrypt, формат разбора scrypt-хеша и чтение bcrypt-хешей на runtime с scrypt.
|
||||
|
||||
### Роли компонентов
|
||||
|
||||
| Роль | Service | Версия / фиксация | CPU / RAM | Хранилище / сеть |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| API | `api-a`, `api-b` | Python 3.12 slim bookworm, digest в Dockerfile | по 0.50 CPU / 256 MiB | локальное состояние не определяет корректность, только `backend` |
|
||||
| БД | `postgres` | PostgreSQL 15.19, digest в Compose | 1 CPU / 512 MiB | `pg_data`, только `backend` |
|
||||
| Кэш | `redis` | Redis 7.4.12, digest в Compose | 0.25 CPU / 128 MiB | `redis_data`, AOF, maxmemory 96 MiB, только `backend` |
|
||||
| Балансировщик | `nginx` | Nginx 1.28.2 alpine, digest в Compose | 0.50 CPU / 128 MiB | `frontend` + `backend`, единственный опубликованный порт |
|
||||
| Миграции | `migrate` | тот же API-образ | 0.50 CPU / 256 MiB | временный процесс, только `backend` |
|
||||
|
||||
API не имеют `ports`; `container_name` отсутствует. Выбраны два именованных сервиса API для явной поочерёдной замены. Добавление третьего сервиса требует изменения upstream и контроллера, автоматическое обнаружение произвольного числа сервисов не заявляется. Nginx использует round robin, без sticky sessions; DNS Docker `127.0.0.11` обновляется с `valid=1s`, `resolver_timeout=1s`. Общее имя сервиса после пересоздания получает новый адрес без ручной перезагрузки всего стенда.
|
||||
|
||||
### Конфигурация и таймауты
|
||||
|
||||
| Настройка | Значение / назначение |
|
||||
| --- | --- |
|
||||
| `PUBLIC_PORT` | внешний порт Nginx; 8080 по умолчанию, 18080 в измерениях |
|
||||
| `API_IMAGE_TAG` | тег собираемого API; `local` по умолчанию, отдельный тег при обновлении |
|
||||
| `SESSION_TTL_SECONDS` | 3600; для smoke/persistence минимум 300, expiry-тест временно задаёт 2 |
|
||||
| `DATABASE_URL_FILE` / `CACHE_URL_FILE` | общие файлы секретов для API и миграций |
|
||||
| `CURSOR_SECRET_FILE` | общий постоянный HMAC-ключ, курсор работает на другой реплике и после рестарта |
|
||||
| `DB_POOL_MIN/MAX` | 2 / 10 на API; две реплики дают максимум 20 соединений API |
|
||||
| `DB_CONNECT_TIMEOUT` / `DB_STATEMENT_TIMEOUT` | 1 / 4 s |
|
||||
| `/health/ready` | лёгкий `SELECT 1`; БД обязательна, Redis необязателен |
|
||||
| `/health/live` | жизнь API-процесса, независимо от доступности БД |
|
||||
| API Docker healthcheck | интервал 2 s, timeout 2 s, retries 2, start_period 3 s |
|
||||
| Активные проверки LB | параллельные `/health/ready`, пауза между циклами 1 s, timeout пробы 1 s |
|
||||
| Пассивное исключение Nginx | `max_fails=1`, `fail_timeout=2s` |
|
||||
| Nginx connect / read / send timeout | 1 / 5 / 5 s; максимум 2 попытки upstream, бюджет переключения 6 s |
|
||||
| Завершение API | SIGTERM запрещает новые запросы, drain до 8 s; Compose stop grace 9 s |
|
||||
| Старые worker Nginx | `worker_shutdown_timeout=6s`; плановая замена ждёт 6.5 s после подтверждения drain |
|
||||
|
||||
`healthwatch.sh` — небольшой контроллер внутри контейнера Nginx, без Docker socket. После проб он читает управляющее поколение `serve`/`drain`, атомарно заменяет рабочий upstream, выполняет `nginx -t` и reload, затем подтверждает применённое поколение. `manage.py` ждёт именно это подтверждение. Таким образом, Docker healthcheck отдельно показывает состояние контейнера, а активная readiness реально влияет на маршрутизацию.
|
||||
|
||||
При свободном CPU номинальное обнаружение неготовности — до 3 s: до 1 s завершения уже начатого параллельного цикла, пауза 1 s и до 1 s следующей пробы, плюс накладные расходы reload. С учётом уже начатых запросов и завершения старых worker принят бюджет до 10 s для продолжения новых чтений/записей; реальные результаты ниже. Это не гарантия реального времени при перегруженном хосте.
|
||||
|
||||
API добавляет `X-Instance-Id` из hostname контейнера во все свои ответы; значение стабильно в течение жизни процесса и сопоставимо с реальным контейнером. Ошибки самого Nginx могут не содержать этот заголовок. При отказе PostgreSQL API выдаёт 503 на readiness/бизнес-запросы и 200 на liveness; LB после исключения обеих API может отвечать 502. Это отказ общей БД, не отказ одной API.
|
||||
|
||||
### Источник истины и идемпотентность
|
||||
|
||||
PostgreSQL — источник истины для данных, сессий и результатов идемпотентности. Redis хранит только кэш сессий с TTL. Каждый cache hit дополнительно проверяет действительность сессии в PostgreSQL: устаревший ключ после logout не даёт доступ. При недоступности Redis API использует БД. Как и в перенесённой реализации lab2, после сетевой ошибки Redis-клиент остаётся отключённым до перезапуска API; автоматический повтор подключения пока не реализован.
|
||||
|
||||
`POST /api/v1/chats/{chat_id}/messages` требует `Idempotency-Key` формата `[A-Za-z0-9._:-]{1,128}`. Сначала проверяются сессия, членство и валидность тела. Полная область уникальности — `(user_id, method, path, key)`. Нормализованное допустимое тело lab3 содержит `text`; порядок полей и внешние пробелы JSON не влияют на результат.
|
||||
|
||||
В одной транзакции PostgreSQL берётся advisory lock от области ключа, проверяется сохранённый результат, вставляется сообщение и JSON исходного ответа. Полный ключ защищён составным первичным ключом; коллизия хеша блокировки лишь последовательно выполнит независимые операции. Повтор одинакового тела возвращает исходный 201 и тот же Message; другое тело — 409. Ошибка до COMMIT откатывает и сообщение, и результат; потерянный после COMMIT ответ безопасно восстанавливается повтором ключа. Транзиентный 5xx не сохраняется.
|
||||
|
||||
Результаты идемпотентности в учебном стенде хранятся бессрочно — требование минимум 24 часов выполнено, но таблица растёт. Вложения и `attachment_ids` появляются только в lab4 и в текущий контракт lab3 не входят. Nginx не включает `proxy_next_upstream non_idempotent`: автоматический повтор переданного POST регистрации/создания чата опасен. Неопределённый результат POST сообщения повторяет клиент с исходным ключом и телом.
|
||||
|
||||
### Миграции и поочерёдная замена
|
||||
|
||||
`python -m messenger.migrate` — единственная точка применения схемы. Миграции защищены transaction advisory lock, а выполненные имена записываются в `schema_migrations`. `0001_init.sql` сохраняет схему lab2; `0002_idempotency.sql` только добавляет таблицу. API при старте миграции не запускает. Текущая миграция совместима с работающей предыдущей схемой; поддержка произвольных разрушительных миграций без отдельного плана не заявляется.
|
||||
|
||||
Команды обновления на новый образ:
|
||||
|
||||
```sh
|
||||
API_IMAGE_TAG=updated docker compose build api-a
|
||||
API_IMAGE_TAG=updated make test-rolling-load
|
||||
# Или отдельно, если нагрузка уже запущена:
|
||||
API_IMAGE_TAG=updated scripts/rolling-update.sh
|
||||
```
|
||||
|
||||
Скрипт один раз запускает migration-service, затем для каждой API подтверждает готовность и включение другой реплики в LB, применяет drain выбранной, ждёт завершения старых worker, пересоздаёт API, ждёт Docker health и возврата через readiness. При неготовой новой версии скрипт прекращает обновление, оставляя другую реплику обслуживать запросы. После исправления версии `python3 scripts/manage.py restore` возвращает только готовые backend. Откат образа автоматически не выполняется.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
### Оценка «3»: работающий минимум
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Критерий README | Команда / сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Воспроизводимый стенд, две API, общая БД и сессии, один внешний LB | `docker compose up -d --build`; `python3 scripts/manage.py status` | два реальных API-контейнера, наружу опубликован только Nginx | `compose.yaml`, `evidence/final-status.jsonl` |
|
||||
| Одна сессия работает на разных репликах | `make lab3-test` | разные `X-Instance-Id`, повторный вход не нужен; курсор переносится между репликами | `evidence/lab3-tests.txt` |
|
||||
| Чтение/запись продолжаются после остановки любой одной API за ≤10 s | `make test-failover-load` | отдельный отказ каждой API, предыдущая восстановлена перед следующим опытом | `evidence/stop-api-a.json`, `evidence/stop-api-b.json` |
|
||||
| Подтверждённые данные сохранены, неопределённый POST безопасно повторяется | нагрузочные сценарии, полный просмотр истории после воздействия | нет потерь подтверждений и дублей текста; старый ключ возвращает исходный Message | JSON экспериментов, `evidence/dependencies.txt` |
|
||||
| Общая идемпотентность, включая конкурентный повтор | `make lab3-test`; SQL fault injection | 12 запросов через обе API создают одну запись; rollback при ошибке между двумя INSERT | `evidence/lab3-tests.txt`, `evidence/storage.txt` |
|
||||
| Публичные smoke, persistence и failover | `make test`; `make test-persistence ACTION_SCRIPT=scripts/restart.sh`; `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | PASS | `evidence/smoke.txt`, `evidence/persistence.txt`, `evidence/public-failover.txt` |
|
||||
| Сохранён уровень lab2 | регрессионные, SQL, dependency и expiry-тесты | 13 регрессий; KDF; гонка username; rollback; expiry/logout; индекс на 10000+ сообщениях | `evidence/student-tests.txt`, `evidence/unit-tests.txt`, `evidence/storage.txt`, `evidence/dependencies.txt`, `evidence/expiry.txt` |
|
||||
|
||||
### Оценка «4»: уровень стажёра / джуна
|
||||
|
||||
| Критерий README | Команда / сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Согласованы readiness, timeout, SIGTERM и исключение backend | `make test-readiness`; отказ API; анализ конфигурации | замороженный живой процесс исключён; после разморозки возвращён; активные пробы связаны с Nginx | `evidence/readiness.txt`, `infra/healthwatch.sh`, `infra/nginx.conf` |
|
||||
| Остановлена каждая реплика, собраны RPS, ошибки, восстановление | `make test-failover-load` | обе реплики испытаны отдельно, в JSON есть реальные ID и состояния до/после | `evidence/failover-load.txt`, `evidence/stop-api-a.json`, `evidence/stop-api-b.json` |
|
||||
| Один ключ параллельно через разные реплики | `make lab3-test` | исходный 201 и один Message для всех 12 запросов, ≥2 реальных instance ID | `evidence/lab3-tests.txt` |
|
||||
| CPU/RAM-лимиты, приватные API, разумное размещение секретов и stores, нет container_name | `docker compose config --quiet`; `python3 scripts/manage.py status` | лимиты заданы; PortBindings у API отсутствуют; секреты не входят в исходники/образ | `compose.yaml`, `evidence/final-status.jsonl` |
|
||||
| Поочерёдная замена под нагрузкой | сборка нового тега; `API_IMAGE_TAG=updated make test-rolling-load` | изменились container ID и image ID обеих API; во время обновления продолжаются чтения/записи | `evidence/rolling-update.json`, `evidence/rolling-update-action.txt` |
|
||||
| Миграции однократно и совместимы | `migrate`; повтор SQL migration runner | две записи schema_migrations; повтор не меняет данные; API не применяет миграции | `evidence/storage.txt`, `src/messenger/migrate.py`, `migrations/0002_idempotency.sql` |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Измерения: 6 ноября 2026 года, macOS 27.0.1 arm64, Docker Engine 29.5.3, Compose 5.1.4. Docker VM: 10 CPU и 8 321 515 520 bytes RAM. Точная модель CPU и RAM физического хоста не прочитаны из sandbox и не подставлены из lab2. Паспорт — `evidence/environment.json` (сбор: `python3 scripts/collect-evidence.py`); действующие лимиты — `evidence/final-status.jsonl`. Во время опытов отдельно работала lab2, поэтому фоновые процессы влияют на цифры.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
Профиль `student-tests/experiments.py`: четыре клиента, каждый чередует POST сообщения и GET первой страницы истории; прогрев 3 s; затем воздействие при продолжающихся запросах. После остановки API нагрузка идёт ещё 12 s, после rolling — 5 s. Для каждого воздействия создаётся новый синтетический чат. Длительность включает наблюдение и финальную сверку; число подтверждений, записей истории, RPS и latency содержатся в исходных JSON. CPU/RAM снимаются `docker stats` во время нагрузки.
|
||||
|
||||
| Воздействие | Успешных HTTP | Общие RPS | POST RPS | Ошибки | p95 успешных HTTP, ms | Чтение / запись после hook, s |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| Остановка api-a | 4624 | 257.288 | 128.644 | 0 | 49.281 | 0.488 / 0.504 |
|
||||
| Остановка api-b | 5247 | 291.629 | 145.787 | 1 | 44.871 | 0.632 / 0.634 |
|
||||
| Поочерёдная замена | 16044 | 355.419 | 177.71 | 0 | 47.279 | обновление 35.255 s; без ошибок |
|
||||
|
||||
Во всех трёх опытах потерянных/изменённых подтверждений и дублей — 0. При поочерёдной замене максимальный промежуток между успешными чтениями — 0.108 s, между записями — 0.178 s. При остановке api-b зафиксирована одна временная ошибка POST; повтор с исходным ключом не создал дубль.
|
||||
|
||||
Восстановление в failover JSON — первый успешный запрос после завершения hook, отсчитанный от его запуска. Это верхняя наблюдаемая граница возобновления, а не длительность полной недоступности: исправная реплика могла обслуживать запросы непрерывно. В rolling длительность действия означает время поочерёдной замены обеих API, а не простой. Дополнительно тест требует успешных чтений и записей в каждом трёхсекундном окне и максимальный промежуток между успехами ≤3 s.
|
||||
|
||||
| Воздействие | Ожидание | Наблюдение / доказательство |
|
||||
| --- | --- | --- |
|
||||
| Остановка `api-a`, затем восстановление и остановка `api-b` | другая API обслуживает пользователя с прежним токеном | отдельные JSON показывают остановленный контейнер и отсутствие потерь/дублей |
|
||||
| Заморозка `api-a` через Docker pause | живая TCP-точка не считается готовой API | `evidence/readiness.txt`: активное исключение и возврат после unpause |
|
||||
| Полный restart без удаления volumes | прежние данные, сессии и ключи живы | public persistence и собственный replay после рестарта — PASS |
|
||||
| Остановка Redis | безопасная деградация на PostgreSQL | аутентифицированная запись работает, logout остаётся действительным |
|
||||
| Остановка PostgreSQL | API отказывает ограниченно | внутри API live=200, ready/business=503; LB может выдать 502 при исключении всех backend |
|
||||
| `SESSION_TTL_SECONDS=2` | истёкшая сессия не принимается ни одной API | 401 на повторных запросах; после теста восстановлен TTL 3600 |
|
||||
| Ошибка между INSERT сообщения и INSERT результата | ни одной частичной записи | SQL trigger fault injection: 0 сообщений после rollback, тот же ключ затем успешно используется |
|
||||
| Повтор запуска миграций | нет повторного применения и потери данных | в synthetic SQL-чате остаётся 10001 сообщение |
|
||||
|
||||
Закрытая нагрузка зависит от производительности клиента и имеет coordinated omission. p50/p95/p99 относятся только к успешным HTTP-запросам; ошибки учитываются отдельно. Суммарные RPS включают чтения и записи, POST RPS приведены отдельно. Один прогон каждого сценария доказывает воспроизводимый пример поведения, не статистическую гарантию и не линейное масштабирование.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Выдерживается остановка одной API, потеря ответа сообщения с безопасным повтором, перезапуск с сохранением volumes и плановая поочерёдная замена. Общие PostgreSQL, Nginx, Docker-host и Redis не реплицированы. Потеря общей БД/LB/хоста не переживается; Redis необязателен для корректности сессий. Отказ узла и исследование масштабирования относятся к трекам оценки 5 и не заявлены.
|
||||
|
||||
Учебные ограничения: бессрочное хранение idempotency-результатов; нет автоматического отката образа; фиксированные имена двух сервисов в контроллере; Redis восстанавливает кэширование после перезапуска API; нет TLS, rate limiting и вложений следующих лабораторных. Пользовательские часы между запросами не дают snapshot-изоляцию истории: сохраняется семантика keyset-пагинации lab2. Долгий запрос сверх бюджета graceful shutdown может потерять ответ; POST сообщения повторяется с тем же ключом.
|
||||
|
||||
Контракт, README задания и публичные `tests/` не ослаблялись. Секреты создаются локально; Git-команды, коммиты и push оставлены автору.
|
||||
|
||||
## Черновик текста PR
|
||||
|
||||
- Лабораторная 3 — Любченко Никита, БПИ2303; целевая оценка **4**.
|
||||
- Перенесён мессенджер lab2; добавлены две API за Nginx, общие сессии/курсоры, PostgreSQL-идемпотентность, отдельные миграции, readiness и сценарии остановки/поочерёдной замены.
|
||||
- Запуск: `.env.example` → `.env`, `python3 scripts/bootstrap.py`, `docker compose up -d --build`, `python3 scripts/manage.py restore`, `make test`.
|
||||
- Проверки и реальные артефакты перечислены в таблицах выше. Исследовательский трек оценки 5 не заявлен.
|
||||
- [x] Уровень 3 и все пункты уровня 4 покрыты реализацией и отдельными проверками.
|
||||
- [x] Контракт и публичные проверки сохранены.
|
||||
- [x] Секреты и токены не входят в публикуемые файлы.
|
||||
- [ ] Автор проверил файлы, создал commit и добавил ссылку на PR.
|
||||
|
||||
## Источники
|
||||
|
||||
- [Nginx upstream: resolve, shared zone, max_fails и fail_timeout](https://nginx.org/en/docs/http/ngx_http_upstream_module.html).
|
||||
- [Nginx proxy: таймауты и ограничения повторов non-idempotent запросов](https://nginx.org/en/docs/http/ngx_http_proxy_module.html).
|
||||
- [Docker Compose services](https://docs.docker.com/reference/compose-file/services/).
|
||||
- [PostgreSQL explicit locking](https://www.postgresql.org/docs/15/explicit-locking.html).
|
||||
- [PostgreSQL transactions](https://www.postgresql.org/docs/15/tutorial-transactions.html).
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
name: mtusi-lab3
|
||||
|
||||
x-api: &api
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
APP_REVISION: ${API_IMAGE_TAG:-local}
|
||||
image: mtusi-lab3-api:${API_IMAGE_TAG:-local}
|
||||
environment: &api-env
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: '8080'
|
||||
APP_ENV: lab3
|
||||
CURSOR_SECRET_FILE: /run/secrets/cursor_secret
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
SESSION_STORE: redis
|
||||
SHUTDOWN_GRACE_SECONDS: '8'
|
||||
DB_POOL_MIN: '2'
|
||||
DB_POOL_MAX: '10'
|
||||
DB_CONNECT_TIMEOUT: '1'
|
||||
DB_STATEMENT_TIMEOUT: '4'
|
||||
secrets: [database_url, cache_url, cursor_secret]
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
networks: [backend]
|
||||
cpus: 0.50
|
||||
mem_limit: 256m
|
||||
stop_grace_period: 9s
|
||||
healthcheck:
|
||||
test: [CMD, python, -c, "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=1)"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 2
|
||||
start_period: 3s
|
||||
# Deliberately no auto-restart: failover hook must leave its target stopped.
|
||||
restart: 'no'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15-alpine@sha256:54e0dd8e8cddcdbb9156c2486eed5a43eccb7214d7c6828097d41ce049fb3984
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets: [postgres_password]
|
||||
volumes: [pg_data:/var/lib/postgresql/data]
|
||||
networks: [backend]
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
|
||||
redis:
|
||||
image: redis:7-alpine@sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f
|
||||
command: [redis-server, --appendonly, 'yes', --maxmemory, 96mb, --maxmemory-policy, allkeys-lru]
|
||||
volumes: [redis_data:/data]
|
||||
networks: [backend]
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
healthcheck:
|
||||
test: [CMD, redis-cli, ping]
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 10
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
APP_REVISION: ${API_IMAGE_TAG:-local}
|
||||
image: mtusi-lab3-api:${API_IMAGE_TAG:-local}
|
||||
command: [python, -m, messenger.migrate]
|
||||
environment: *api-env
|
||||
secrets: [database_url, cache_url, cursor_secret]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
networks: [backend]
|
||||
cpus: 0.50
|
||||
mem_limit: 256m
|
||||
restart: 'no'
|
||||
healthcheck:
|
||||
disable: true
|
||||
|
||||
api-a:
|
||||
<<: *api
|
||||
api-b:
|
||||
<<: *api
|
||||
|
||||
nginx:
|
||||
image: nginx:1.28.2-alpine@sha256:5b4900b042ccfa8b0a73df622c3a60f2322faeb2be800cbee5aa7b44d241649e
|
||||
entrypoint: [/bin/sh, -ec]
|
||||
command:
|
||||
- 'cp /etc/nginx/lab/upstreams.conf /etc/nginx/upstreams.conf; /bin/sh /etc/nginx/lab/healthwatch.sh & exec nginx -g "daemon off;"'
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra:/etc/nginx/lab:ro
|
||||
ports: ['127.0.0.1:${PUBLIC_PORT:-8080}:8080']
|
||||
networks: [frontend, backend]
|
||||
depends_on:
|
||||
api-a:
|
||||
condition: service_healthy
|
||||
api-b:
|
||||
condition: service_healthy
|
||||
cpus: 0.50
|
||||
mem_limit: 128m
|
||||
stop_grace_period: 10s
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'kill -0 $$(cat /tmp/healthwatch.pid) && wget -T 1 -q -O /dev/null http://127.0.0.1:8080/health/ready']
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
|
||||
networks:
|
||||
frontend:
|
||||
backend:
|
||||
internal: true
|
||||
volumes:
|
||||
pg_data:
|
||||
redis_data:
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
cursor_secret:
|
||||
file: ./secrets/cursor_secret
|
||||
+22
-3
@@ -1,5 +1,24 @@
|
||||
# Доказательства
|
||||
# Доказательства — лабораторная 3
|
||||
|
||||
Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки.
|
||||
Результаты получены на синтетических данных через Nginx. Паспорт среды — `environment.json`, конфигурация и ограничения измерений — в [REPORT.md](../REPORT.md). Commit не создавался: работу с Git выполняет автор после проверки файлов.
|
||||
|
||||
Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно.
|
||||
| Артефакт | Что проверяет |
|
||||
| --- | --- |
|
||||
| `check.txt`, `validate.txt` | структура skeleton и схема OpenAPI |
|
||||
| `unit-tests.txt` | KDF, HTTP-framing и формат ключа |
|
||||
| `smoke.txt`, `student-tests.txt`, `lab3-tests.txt` | публичный сценарий, регрессии lab2, идемпотентность между репликами |
|
||||
| `concurrency.json` | 100 независимых сообщений от 10 клиентов |
|
||||
| `storage.txt` | настоящий rollback составных транзакций, индекс истории на 10000+ сообщениях и повтор миграций |
|
||||
| `persistence.txt`, `before-persistence.jsonl`, `after-persistence.jsonl` | полный рестарт с сохранением данных и сессии, реальные состояния контейнеров |
|
||||
| `dependencies.txt`, `expiry.txt` | отказы Redis/БД, отзыв сессии, короткий TTL и сохранность ключа после рестарта |
|
||||
| `readiness.txt` | исключение живой, но замороженной API и возврат после разморозки |
|
||||
| `public-failover.txt` | публичная проверка остановки выбранной реплики |
|
||||
| `stop-api-a.json`, `stop-api-b.json` | отдельная остановка каждой реплики под нагрузкой |
|
||||
| `rolling-update.json` | замена обеих реплик на другой образ под нагрузкой |
|
||||
| `*-action.txt` | действия оператора, ID контейнеров и проверки конфигурации при экспериментах |
|
||||
| `protected-files.txt`, `source-image-check.txt` | сохранность файлов задания и совпадение работающего API с исходниками |
|
||||
| `source-sha256.json` | контрольные суммы исходников, конфигурации и собственных тестов проверенной версии |
|
||||
|
||||
RPS считаются по успешным HTTP-запросам, отдельно для чтения и записи. Процентили включают только успешные запросы. Нагрузка замкнутая (closed-loop), клиент влияет на результат; это проверка поведения стенда при отказе, не исследование максимальной пропускной способности. Таймаут записи может скрывать COMMIT, поэтому повтор сохраняет исходные ключ и тело, а история после воздействия сверяется с подтверждёнными сообщениями.
|
||||
|
||||
Секреты, `.env`, токены, пароли и дампы БД в доказательства не включаются. Старые измерения lab2 не выдаются за результаты lab3.
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{"service": "postgres", "id": "a1902a1cda2d", "instance": "a1902a1cda2d", "running": true, "started_at": "2026-11-06T17:11:53.05322338Z", "health": "healthy", "image_id": "sha256:54e0dd8e8cddcdbb9156c2486eed5a43eccb7214d7c6828097d41ce049fb3984", "cpu_limit": 1.0, "memory_limit_bytes": 536870912, "published_ports": {}}
|
||||
{"service": "redis", "id": "c99cd0f56994", "instance": "c99cd0f56994", "running": true, "started_at": "2026-11-06T17:11:53.078491338Z", "health": "healthy", "image_id": "sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", "cpu_limit": 0.25, "memory_limit_bytes": 134217728, "published_ports": {}}
|
||||
{"service": "api-a", "id": "6c3d78ba978d", "instance": "6c3d78ba978d", "running": true, "started_at": "2026-11-06T17:11:53.161237463Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "api-b", "id": "b845543d6cc9", "instance": "b845543d6cc9", "running": true, "started_at": "2026-11-06T17:11:53.249185255Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "nginx", "id": "f453c77da6cf", "instance": "f453c77da6cf", "running": true, "started_at": "2026-11-06T17:11:53.283876297Z", "health": "healthy", "image_id": "sha256:5b4900b042ccfa8b0a73df622c3a60f2322faeb2be800cbee5aa7b44d241649e", "cpu_limit": 0.5, "memory_limit_bytes": 134217728, "published_ports": {"8080/tcp": [{"HostIp": "127.0.0.1", "HostPort": "18080"}]}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{"service": "postgres", "id": "a1902a1cda2d", "instance": "a1902a1cda2d", "running": true, "started_at": "2026-11-06T17:05:33.937069802Z", "health": "healthy", "image_id": "sha256:54e0dd8e8cddcdbb9156c2486eed5a43eccb7214d7c6828097d41ce049fb3984", "cpu_limit": 1.0, "memory_limit_bytes": 536870912, "published_ports": {}}
|
||||
{"service": "redis", "id": "c99cd0f56994", "instance": "c99cd0f56994", "running": true, "started_at": "2026-11-06T17:05:34.10158426Z", "health": "healthy", "image_id": "sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", "cpu_limit": 0.25, "memory_limit_bytes": 134217728, "published_ports": {}}
|
||||
{"service": "api-a", "id": "6c3d78ba978d", "instance": "6c3d78ba978d", "running": true, "started_at": "2026-11-06T17:09:17.177321044Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "api-b", "id": "b845543d6cc9", "instance": "b845543d6cc9", "running": true, "started_at": "2026-11-06T17:09:34.25306626Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "nginx", "id": "f453c77da6cf", "instance": "f453c77da6cf", "running": true, "started_at": "2026-11-06T17:09:01.648462509Z", "health": "healthy", "image_id": "sha256:5b4900b042ccfa8b0a73df622c3a60f2322faeb2be800cbee5aa7b44d241649e", "cpu_limit": 0.5, "memory_limit_bytes": 134217728, "published_ports": {"8080/tcp": [{"HostIp": "127.0.0.1", "HostPort": "18080"}]}}
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
@@ -0,0 +1,82 @@
|
||||
{
|
||||
"lab": 3,
|
||||
"workload": "closed-loop parallel POST messages from independent clients",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"expected_messages": 100,
|
||||
"acknowledged_201": 100,
|
||||
"stored_in_history": 100,
|
||||
"unique_stored_ids": 100,
|
||||
"unique_stored_texts": 100,
|
||||
"duplicates": 0,
|
||||
"missing_texts": [],
|
||||
"failed_writes": 0,
|
||||
"write_errors": [],
|
||||
"history_pages": 1,
|
||||
"history_order_strictly_increasing": true,
|
||||
"writers_elapsed_seconds": 0.455,
|
||||
"successful_rps": 219.78,
|
||||
"per_client": [
|
||||
{
|
||||
"client_index": 0,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.412
|
||||
},
|
||||
{
|
||||
"client_index": 1,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.41
|
||||
},
|
||||
{
|
||||
"client_index": 2,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.405
|
||||
},
|
||||
{
|
||||
"client_index": 3,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.401
|
||||
},
|
||||
{
|
||||
"client_index": 4,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.348
|
||||
},
|
||||
{
|
||||
"client_index": 5,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.269
|
||||
},
|
||||
{
|
||||
"client_index": 6,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.352
|
||||
},
|
||||
{
|
||||
"client_index": 7,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.338
|
||||
},
|
||||
{
|
||||
"client_index": 8,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.411
|
||||
},
|
||||
{
|
||||
"client_index": 9,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.403
|
||||
}
|
||||
],
|
||||
"note": "Client-observed timings: closed-loop, single Docker host, no retries. Latency percentiles are intentionally absent here (see make load)."
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
python3 student-tests/concurrency.py --base-url "http://localhost:18080" --clients 10 --messages-per-client 10 --output evidence/concurrency.json
|
||||
{
|
||||
"lab": 3,
|
||||
"workload": "closed-loop parallel POST messages from independent clients",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"expected_messages": 100,
|
||||
"acknowledged_201": 100,
|
||||
"stored_in_history": 100,
|
||||
"unique_stored_ids": 100,
|
||||
"unique_stored_texts": 100,
|
||||
"duplicates": 0,
|
||||
"missing_texts": [],
|
||||
"failed_writes": 0,
|
||||
"write_errors": [],
|
||||
"history_pages": 1,
|
||||
"history_order_strictly_increasing": true,
|
||||
"writers_elapsed_seconds": 0.455,
|
||||
"successful_rps": 219.78,
|
||||
"per_client": [
|
||||
{
|
||||
"client_index": 0,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.412
|
||||
},
|
||||
{
|
||||
"client_index": 1,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.41
|
||||
},
|
||||
{
|
||||
"client_index": 2,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.405
|
||||
},
|
||||
{
|
||||
"client_index": 3,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.401
|
||||
},
|
||||
{
|
||||
"client_index": 4,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.348
|
||||
},
|
||||
{
|
||||
"client_index": 5,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.269
|
||||
},
|
||||
{
|
||||
"client_index": 6,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.352
|
||||
},
|
||||
{
|
||||
"client_index": 7,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.338
|
||||
},
|
||||
{
|
||||
"client_index": 8,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.411
|
||||
},
|
||||
{
|
||||
"client_index": 9,
|
||||
"acknowledged": 10,
|
||||
"errors": 0,
|
||||
"duration_seconds": 0.403
|
||||
}
|
||||
],
|
||||
"note": "Client-observed timings: closed-loop, single Docker host, no retries. Latency percentiles are intentionally absent here (see make load)."
|
||||
}
|
||||
PASS: 100 parallel messages from 10 clients stored exactly once
|
||||
@@ -0,0 +1,22 @@
|
||||
Container mtusi-lab3-redis-1 Stopping
|
||||
Container mtusi-lab3-redis-1 Stopped
|
||||
c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229
|
||||
Container mtusi-lab3-redis-1 Stopping
|
||||
Container mtusi-lab3-redis-1 Stopped
|
||||
c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229
|
||||
Container mtusi-lab3-postgres-1 Stopping
|
||||
Container mtusi-lab3-postgres-1 Stopped
|
||||
PASS: API liveness 200, readiness/business 503 while PostgreSQL is down
|
||||
a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9
|
||||
c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229
|
||||
Container mtusi-lab3-api-a-1 Restarting
|
||||
Container mtusi-lab3-postgres-1 Restarting
|
||||
Container mtusi-lab3-redis-1 Restarting
|
||||
Container mtusi-lab3-api-b-1 Restarting
|
||||
Container mtusi-lab3-nginx-1 Restarting
|
||||
Container mtusi-lab3-postgres-1 Started
|
||||
Container mtusi-lab3-redis-1 Started
|
||||
Container mtusi-lab3-api-b-1 Started
|
||||
Container mtusi-lab3-api-a-1 Started
|
||||
Container mtusi-lab3-nginx-1 Started
|
||||
PASS: registration race, invalid chat rollback, Redis fallback, logout, DB 503, restart replay with original token/key
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"date": "2026-11-06T20:15:33.923576+03:00",
|
||||
"timezone": "Europe/Moscow",
|
||||
"os": "macOS-27.0.1-arm64-arm-64bit",
|
||||
"architecture": "arm64",
|
||||
"cpu": "Недоступно в текущем окружении",
|
||||
"logical_cpus": "Недоступно в текущем окружении",
|
||||
"host_memory_bytes": "Недоступно в текущем окружении",
|
||||
"docker_version": "29.5.3",
|
||||
"compose_version": "5.1.4",
|
||||
"docker_resources": {
|
||||
"cpus": 10,
|
||||
"memory_bytes": 8321515520
|
||||
},
|
||||
"commit": "Не фиксировался: Git оставлен пользователю",
|
||||
"note": "lab2 remained running separately on port 8080; lab3 measured on 18080. Shared-host background work affects timings."
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
SESSION_TTL_SECONDS=2 docker compose up -d --no-deps --force-recreate api-a api-b
|
||||
Container mtusi-lab3-api-a-1 Recreate
|
||||
Container mtusi-lab3-api-b-1 Recreate
|
||||
Container mtusi-lab3-api-a-1 Recreated
|
||||
Container mtusi-lab3-api-b-1 Recreated
|
||||
Container mtusi-lab3-api-a-1 Starting
|
||||
Container mtusi-lab3-api-b-1 Starting
|
||||
Container mtusi-lab3-api-a-1 Started
|
||||
Container mtusi-lab3-api-b-1 Started
|
||||
python3 scripts/manage.py restore
|
||||
db7745dff8b7c8cdb1a19828a6e2f91e686256b4dec55f4a31aad42df791eedd
|
||||
9cd64cac047e9e838780f2dd989bec0d1c8910138036c7ef6d58f4d196f1d5ee
|
||||
PASS: short SESSION_TTL_SECONDS expiry across replicas
|
||||
Container mtusi-lab3-api-b-1 Recreate
|
||||
Container mtusi-lab3-api-a-1 Recreate
|
||||
Container mtusi-lab3-api-b-1 Recreated
|
||||
Container mtusi-lab3-api-a-1 Recreated
|
||||
Container mtusi-lab3-api-a-1 Starting
|
||||
Container mtusi-lab3-api-b-1 Starting
|
||||
Container mtusi-lab3-api-b-1 Started
|
||||
Container mtusi-lab3-api-a-1 Started
|
||||
d26cdb2d282e9c035c52a3491ff8b1001869fbd0ce96365f0cedeb6543e3c2cb
|
||||
1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26
|
||||
@@ -0,0 +1,11 @@
|
||||
python3 student-tests/experiments.py --base-url "http://localhost:18080" --scenario failover
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8
|
||||
{"scenario": "stop-api-a", "concurrency": 4, "warmup_seconds": 3, "elapsed_seconds": 17.972, "action_start_seconds": 3.153, "action_done_seconds": 3.64, "first_success_after_action_from_action_start_seconds": {"read": 0.488, "write": 0.504}, "http": {"successful_requests": 4624, "failed_requests": 0, "successful_rps": 257.288, "error_ratio": 0.0, "successful_latency_ms": {"p50": 7.7, "p95": 49.281, "p99": 91.316}}, "read": {"successful_requests": 2312, "failed_requests": 0, "successful_rps": 128.644, "error_ratio": 0.0, "successful_latency_ms": {"p50": 7.057, "p95": 46.452, "p99": 91.316}}, "write": {"successful_requests": 2312, "failed_requests": 0, "successful_rps": 128.644, "error_ratio": 0.0, "successful_latency_ms": {"p50": 8.64, "p95": 51.717, "p99": 90.447}}, "during_action": {"successful_requests": 89, "failed_requests": 0, "successful_rps": 182.746, "error_ratio": 0.0, "successful_latency_ms": {"p50": 11.397, "p95": 55.55, "p99": 62.607}}, "confirmed_messages": 2312, "history_messages": 2313, "missing_or_changed_confirmations": 0, "duplicate_texts": 0, "maximum_gap_between_successes_during_action_seconds": {"read": 0.052, "write": 0.054}}
|
||||
PASS: stop-api-a
|
||||
{"scenario": "stop-api-b", "concurrency": 4, "warmup_seconds": 3, "elapsed_seconds": 17.992, "action_start_seconds": 3.157, "action_done_seconds": 3.785, "first_success_after_action_from_action_start_seconds": {"read": 0.632, "write": 0.634}, "http": {"successful_requests": 5247, "failed_requests": 1, "successful_rps": 291.629, "error_ratio": 0.000191, "successful_latency_ms": {"p50": 6.107, "p95": 44.871, "p99": 88.255}}, "read": {"successful_requests": 2624, "failed_requests": 0, "successful_rps": 145.842, "error_ratio": 0.0, "successful_latency_ms": {"p50": 5.332, "p95": 44.494, "p99": 86.918}}, "write": {"successful_requests": 2623, "failed_requests": 1, "successful_rps": 145.787, "error_ratio": 0.000381, "successful_latency_ms": {"p50": 6.502, "p95": 45.353, "p99": 89.938}}, "during_action": {"successful_requests": 250, "failed_requests": 1, "successful_rps": 398.334, "error_ratio": 0.003984, "successful_latency_ms": {"p50": 5.302, "p95": 43.575, "p99": 88.408}}, "confirmed_messages": 2623, "history_messages": 2624, "missing_or_changed_confirmations": 0, "duplicate_texts": 0, "maximum_gap_between_successes_during_action_seconds": {"read": 0.091, "write": 0.074}}
|
||||
PASS: stop-api-b
|
||||
@@ -0,0 +1,5 @@
|
||||
{"service": "postgres", "id": "a1902a1cda2d", "instance": "a1902a1cda2d", "running": true, "started_at": "2026-11-06T17:11:53.05322338Z", "health": "healthy", "image_id": "sha256:54e0dd8e8cddcdbb9156c2486eed5a43eccb7214d7c6828097d41ce049fb3984", "cpu_limit": 1.0, "memory_limit_bytes": 536870912, "published_ports": {}}
|
||||
{"service": "redis", "id": "c99cd0f56994", "instance": "c99cd0f56994", "running": true, "started_at": "2026-11-06T17:11:53.078491338Z", "health": "healthy", "image_id": "sha256:8da19c7399dca900c6822a1cf30f9d2285585e2c4b457ff4095b0f4500054d3f", "cpu_limit": 0.25, "memory_limit_bytes": 134217728, "published_ports": {}}
|
||||
{"service": "api-a", "id": "6c3d78ba978d", "instance": "6c3d78ba978d", "running": true, "started_at": "2026-11-06T17:13:20.78945042Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "api-b", "id": "b845543d6cc9", "instance": "b845543d6cc9", "running": true, "started_at": "2026-11-06T17:14:31.400012259Z", "health": "healthy", "image_id": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855", "cpu_limit": 0.5, "memory_limit_bytes": 268435456, "published_ports": {}}
|
||||
{"service": "nginx", "id": "f453c77da6cf", "instance": "f453c77da6cf", "running": true, "started_at": "2026-11-06T17:11:53.283876297Z", "health": "healthy", "image_id": "sha256:5b4900b042ccfa8b0a73df622c3a60f2322faeb2be800cbee5aa7b44d241649e", "cpu_limit": 0.5, "memory_limit_bytes": 134217728, "published_ports": {"8080/tcp": [{"HostIp": "127.0.0.1", "HostPort": "18080"}]}}
|
||||
@@ -0,0 +1,3 @@
|
||||
python3 student-tests/lab3.py --base-url "http://localhost:18080"
|
||||
{"parallel_requests": 12, "parallel_replicas": ["6c3d78ba978d", "b845543d6cc9"], "unique_parallel_messages": 1, "pagination_messages": 7, "pagination_replicas": ["6c3d78ba978d", "b845543d6cc9"]}
|
||||
PASS: cross-replica idempotency, isolation, pagination and logout
|
||||
@@ -0,0 +1,3 @@
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:18080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1 @@
|
||||
PASS: assignment README, COURSE, CONTRIBUTING, lab.json, contracts/ and public tests/ match their original SHA-256
|
||||
@@ -0,0 +1,4 @@
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:18080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 131, "failed_attempts": 0, "first_recovery_seconds": 0.79}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,5 @@
|
||||
python3 student-tests/readiness.py
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
PASS: frozen api-a excluded in 1.422s
|
||||
PASS: healthy backend restored in 1.072s
|
||||
@@ -0,0 +1,5 @@
|
||||
python3 student-tests/experiments.py --base-url "http://localhost:18080" --scenario rolling
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8
|
||||
{"scenario": "rolling-update", "concurrency": 4, "warmup_seconds": 3, "elapsed_seconds": 45.141, "action_start_seconds": 3.162, "action_done_seconds": 38.417, "first_success_after_action_from_action_start_seconds": {"read": 35.303, "write": 35.305}, "http": {"successful_requests": 16044, "failed_requests": 0, "successful_rps": 355.419, "error_ratio": 0.0, "successful_latency_ms": {"p50": 5.179, "p95": 47.279, "p99": 87.176}}, "read": {"successful_requests": 8022, "failed_requests": 0, "successful_rps": 177.71, "error_ratio": 0.0, "successful_latency_ms": {"p50": 4.568, "p95": 47.245, "p99": 86.249}}, "write": {"successful_requests": 8022, "failed_requests": 0, "successful_rps": 177.71, "error_ratio": 0.0, "successful_latency_ms": {"p50": 5.692, "p95": 47.286, "p99": 87.835}}, "during_action": {"successful_requests": 10790, "failed_requests": 0, "successful_rps": 306.05, "error_ratio": 0.0, "successful_latency_ms": {"p50": 6.377, "p95": 51.398, "p99": 89.359}}, "confirmed_messages": 8022, "history_messages": 8023, "missing_or_changed_confirmations": 0, "duplicate_texts": 0, "maximum_gap_between_successes_during_action_seconds": {"read": 0.108, "write": 0.178}}
|
||||
PASS: rolling-update
|
||||
@@ -0,0 +1,13 @@
|
||||
PASS: migrations applied
|
||||
{"service": "api-a", "before": ["37a5489260f9f51c80ed0c14587a03f4a8781e4b9399fc7c8f42dea71a60ace1"], "after": ["6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee"]}
|
||||
{"service": "api-b", "before": ["1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26"], "after": ["b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8"]}
|
||||
Container mtusi-lab3-migrate-run-8e24e956ce89 Creating
|
||||
Container mtusi-lab3-migrate-run-8e24e956ce89 Created
|
||||
Container mtusi-lab3-api-a-1 Recreate
|
||||
Container mtusi-lab3-api-a-1 Recreated
|
||||
Container mtusi-lab3-api-a-1 Starting
|
||||
Container mtusi-lab3-api-a-1 Started
|
||||
Container mtusi-lab3-api-b-1 Recreate
|
||||
Container mtusi-lab3-api-b-1 Recreated
|
||||
Container mtusi-lab3-api-b-1 Starting
|
||||
Container mtusi-lab3-api-b-1 Started
|
||||
@@ -0,0 +1,383 @@
|
||||
{
|
||||
"scenario": "rolling-update",
|
||||
"concurrency": 4,
|
||||
"warmup_seconds": 3,
|
||||
"elapsed_seconds": 45.141,
|
||||
"action_start_seconds": 3.162,
|
||||
"action_done_seconds": 38.417,
|
||||
"first_success_after_action_from_action_start_seconds": {
|
||||
"read": 35.303,
|
||||
"write": 35.305
|
||||
},
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "37a5489260f9",
|
||||
"instance": "37a5489260f9",
|
||||
"running": true,
|
||||
"image": "sha256:1cc5e3b74ed08cae9e055ef2f5910d768cb2c719f6904466cb5be955e8c4b9e7"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "1eee05dbaba0",
|
||||
"instance": "1eee05dbaba0",
|
||||
"running": true,
|
||||
"image": "sha256:1cc5e3b74ed08cae9e055ef2f5910d768cb2c719f6904466cb5be955e8c4b9e7"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "6c3d78ba978d",
|
||||
"instance": "6c3d78ba978d",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "b845543d6cc9",
|
||||
"instance": "b845543d6cc9",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
}
|
||||
],
|
||||
"http": {
|
||||
"successful_requests": 16044,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 355.419,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 5.179,
|
||||
"p95": 47.279,
|
||||
"p99": 87.176
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"successful_requests": 8022,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 177.71,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 4.568,
|
||||
"p95": 47.245,
|
||||
"p99": 86.249
|
||||
}
|
||||
},
|
||||
"write": {
|
||||
"successful_requests": 8022,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 177.71,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 5.692,
|
||||
"p95": 47.286,
|
||||
"p99": 87.835
|
||||
}
|
||||
},
|
||||
"during_action": {
|
||||
"successful_requests": 10790,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 306.05,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 6.377,
|
||||
"p95": 51.398,
|
||||
"p99": 89.359
|
||||
}
|
||||
},
|
||||
"confirmed_messages": 8022,
|
||||
"history_messages": 8023,
|
||||
"missing_or_changed_confirmations": 0,
|
||||
"duplicate_texts": 0,
|
||||
"resources": [
|
||||
{
|
||||
"at": 2.101,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "42.17%",
|
||||
"Container": "37a5489260f9f51c80ed0c14587a03f4a8781e4b9399fc7c8f42dea71a60ace1",
|
||||
"ID": "37a5489260f9",
|
||||
"MemPerc": "16.00%",
|
||||
"MemUsage": "40.96MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "2.84MB / 3.1MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "44.03%",
|
||||
"Container": "1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26",
|
||||
"ID": "1eee05dbaba0",
|
||||
"MemPerc": "22.57%",
|
||||
"MemUsage": "57.79MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "18MB / 19.1MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 49.2kB",
|
||||
"CPUPerc": "10.48%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.12%",
|
||||
"MemUsage": "3.992MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "2.35MB / 2.39MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.61MB",
|
||||
"CPUPerc": "50.99%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "7.78%",
|
||||
"MemUsage": "39.84MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "48.8MB / 51.6MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "36.9kB / 20.5kB",
|
||||
"CPUPerc": "2.35%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.44%",
|
||||
"MemUsage": "9.52MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "2.84MB / 1.86MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 5.12,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "46.88%",
|
||||
"Container": "37a5489260f9f51c80ed0c14587a03f4a8781e4b9399fc7c8f42dea71a60ace1",
|
||||
"ID": "37a5489260f9",
|
||||
"MemPerc": "16.33%",
|
||||
"MemUsage": "41.8MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "6.49MB / 7.02MB",
|
||||
"PIDs": "3"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.31%",
|
||||
"Container": "1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26",
|
||||
"ID": "1eee05dbaba0",
|
||||
"MemPerc": "22.57%",
|
||||
"MemUsage": "57.77MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "21.7MB / 23MB",
|
||||
"PIDs": "3"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 135kB",
|
||||
"CPUPerc": "14.47%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.02%",
|
||||
"MemUsage": "3.871MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "5.88MB / 5.97MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 2.56MB",
|
||||
"CPUPerc": "61.58%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "7.86%",
|
||||
"MemUsage": "40.25MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "54.2MB / 57.1MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "36.9kB / 20.5kB",
|
||||
"CPUPerc": "2.18%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.44%",
|
||||
"MemUsage": "9.52MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "3.16MB / 2.09MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 8.155,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "14.78%",
|
||||
"Container": "37a5489260f9f51c80ed0c14587a03f4a8781e4b9399fc7c8f42dea71a60ace1",
|
||||
"ID": "37a5489260f9",
|
||||
"MemPerc": "16.06%",
|
||||
"MemUsage": "41.12MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "6.93MB / 7.49MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.98%",
|
||||
"Container": "1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26",
|
||||
"ID": "1eee05dbaba0",
|
||||
"MemPerc": "26.21%",
|
||||
"MemUsage": "67.1MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "24.5MB / 26MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 188kB",
|
||||
"CPUPerc": "4.14%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.91%",
|
||||
"MemUsage": "3.73MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "7.3MB / 7.41MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 2.56MB",
|
||||
"CPUPerc": "53.09%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "7.85%",
|
||||
"MemUsage": "40.17MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "56.6MB / 59.6MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "36.9kB / 20.5kB",
|
||||
"CPUPerc": "0.73%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.43%",
|
||||
"MemUsage": "9.516MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "3.27MB / 2.15MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 11.172,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "9.96%",
|
||||
"Container": "37a5489260f9f51c80ed0c14587a03f4a8781e4b9399fc7c8f42dea71a60ace1",
|
||||
"ID": "37a5489260f9",
|
||||
"MemPerc": "16.06%",
|
||||
"MemUsage": "41.12MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "6.94MB / 7.5MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "49.88%",
|
||||
"Container": "1eee05dbaba086be490c7047dd58f56137c8573221639bd3ce870b4d6b562d26",
|
||||
"ID": "1eee05dbaba0",
|
||||
"MemPerc": "27.51%",
|
||||
"MemUsage": "70.42MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "26.9MB / 28.5MB",
|
||||
"PIDs": "6"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 238kB",
|
||||
"CPUPerc": "2.60%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.01%",
|
||||
"MemUsage": "3.852MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "8.33MB / 8.46MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 2.56MB",
|
||||
"CPUPerc": "39.28%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "8.57%",
|
||||
"MemUsage": "43.89MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "58.5MB / 61.5MB",
|
||||
"PIDs": "12"
|
||||
},
|
||||
{
|
||||
"BlockIO": "36.9kB / 20.5kB",
|
||||
"CPUPerc": "0.56%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.504MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "3.41MB / 2.24MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 14.186,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 17.217,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 20.269,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 23.306,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 26.331,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 29.374,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 32.392,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 35.437,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 38.46,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 41.48,
|
||||
"containers": []
|
||||
},
|
||||
{
|
||||
"at": 44.5,
|
||||
"containers": []
|
||||
}
|
||||
],
|
||||
"maximum_gap_between_successes_during_action_seconds": {
|
||||
"read": 0.108,
|
||||
"write": 0.178
|
||||
},
|
||||
"note": "Closed-loop, four writers alternate POST and GET. Percentiles cover successful HTTP requests only; retries preserve key/body. Recovery is first success after hook completion measured from hook start; not an outage duration. One Docker host; no node/DB/LB HA claim."
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 tests/smoke.py --base-url "http://localhost:18080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1 @@
|
||||
PASS: running api-a and api-b Python sources match src/ SHA-256 exactly
|
||||
@@ -0,0 +1,65 @@
|
||||
{
|
||||
".dockerignore": "62c0534ad74d9f0e2982e97d1fbadb66d80c493ed237df5bcb9fbb797062eb38",
|
||||
".env.example": "bed4678ce67f72f3dd17848bec5254d288061c4ef9d13535178fe0277ff18592",
|
||||
".github/pull_request_template.md": "1d5f1cb0fd7b64b019350beffa76c0c8a942328a68a5f6aa8f045a2560b890a0",
|
||||
".gitignore": "c26ab45b3a778a5376f6db4f624a51c5fdbf1a404f9a26df8613449277d95438",
|
||||
"CONTRIBUTING.md": "2c01dd698cc9653dece49a94c9fd3f58d36e84764aaa643b46e26d7e599dc12e",
|
||||
"COURSE.md": "c34d53cd082d194aa4b58f504c4e0348d25b6afec3d6a9c0963dd01c9c81e971",
|
||||
"Dockerfile": "29ef500604b349e5dcf1dffd89046a61552d7758c7c097deed37c096fd1e2254",
|
||||
"MAINTAINERS.md": "7bbe72f2e1f33aac4ec6cc3b80d60def85a3a64a8da9a02a0ef91455457148f0",
|
||||
"Makefile": "37ead57907e8d5eeae94544802704fcbab0bac289b344214b2f5f0ed9aded006",
|
||||
"README.md": "7622c956ad4508d10d1807b4190461e3a7911a22759108ecf812d5bc477ff7a2",
|
||||
"REPORT.md": "deed2abe6a84a1e29cd1d8cc02303b940fe3c7707ef6e9d03d56511f879d0ae7",
|
||||
"compose.example.yaml": "41522570e101e67a4889e9bfd290027f363831781d4ce401874d771ec116fef6",
|
||||
"compose.yaml": "01516bbc86c5eae9af945e32b70b965c217f84ab225f23daa883a15d54395121",
|
||||
"contracts/README.md": "72df27a559f88ffa3fd8c3c79d3c32f6db2c5986a0602677c51994c434a3fc4f",
|
||||
"contracts/examples.json": "0a71cec72903af556df85f939c7e347fc73a86a1a4ffc39fb9c8ded169b7df45",
|
||||
"contracts/infrastructure.json": "99320becab20381c3d31983336dd4e0aef4bca992d672538f049b6684714a577",
|
||||
"contracts/openapi.json": "9c3787e5d08a60c59ae67eeb429e9d75fbf134992a4e1f0954e926db49e111be",
|
||||
"examples/requests.http": "bddb95ebba97431ee956256e677b0724485d571d54438aca07d54df72227098d",
|
||||
"infra/README.md": "cd855eddfca0f9ef9171232a7433697455d4ff8bb3a64adc7bc537b346e1bd68",
|
||||
"infra/healthwatch.sh": "a93baa0c3fe3f7bb9627bb94d6d72cea3fe4f31ab47a4a518ac7c8d86d44a3cc",
|
||||
"infra/nginx.conf": "1d337dcbeb5a41bf57dc01b157452a6bb94e0df22f346934bca74204e0f62dfd",
|
||||
"infra/upstreams.conf": "16e94efebafd58a9f9bfee6191352c9c7aeefc8dcb215503bcc89c4cedb1d6a4",
|
||||
"lab.json": "acf873f5132bbd0af78dc878852baeb49dbfa026c072932f8bd70b958114e938",
|
||||
"migrations/0001_init.sql": "e5907428ecc5d3eb450103604a5873ad3b2753011fc28266e90a60cc1f16dd8e",
|
||||
"migrations/0002_idempotency.sql": "0d307ffd7ebc44266eb874c321729e5ecb635d6c7c1c99d00a6270ec7dc7405f",
|
||||
"requirements-dev.txt": "4cefdb67007caf026d1613be7c519c01b4c8343db7c4f4b618677e01cf113525",
|
||||
"scripts/bootstrap.py": "a930eca1761aa44f6541f309cfc26bedc234a60d796d434c291962ae85e2fc1b",
|
||||
"scripts/check_contract.py": "7c4402c2a6ab80d3b4676e1866d6ecc87b6daea14c080bcb9be9a7e8b959d671",
|
||||
"scripts/collect-evidence.py": "40c73eda69cf582b194520e2162fd243696baa0309728e1ef56173520b09fdd4",
|
||||
"scripts/manage.py": "17101d03b5fa70f71c1a7d21d6b7c95b215e00d8baf1e0af4ea663c2b32ca90c",
|
||||
"scripts/restart.example.sh": "0328246ba75defbced992104e986fde973f63e3830d83251eced5953263e944b",
|
||||
"scripts/restart.sh": "557f6dd6ae19e653cbb165d93c1786e8929a9f3868cf214a25b3d4ad3591dabf",
|
||||
"scripts/rolling-update.sh": "1b732e6fba159b67d67bb59ac4e2b032739a6f0f635f92eef8402c1559a98328",
|
||||
"scripts/stop-one.example.sh": "1bcf34394520b89d95b7605ec56675be799eea920e83cfbba306d5dc0a6d9811",
|
||||
"scripts/stop-one.sh": "f972201a985487657488ffc89d40f608c5eca8e6c5b3b48d8a2a7e895b68538b",
|
||||
"scripts/validate_openapi.py": "4e981acbfc10dac58817bf922d5896e996798551fedc1b2385130e1cddb13d54",
|
||||
"src/messenger/__init__.py": "587b1c0b38e456d49884b911cc2f1fd8ef7a83e90de7462ef39927d1cb78e5cd",
|
||||
"src/messenger/__main__.py": "de48dcf73574cb8b9e16edea75688544d84215b155a7f8951b05ee2f7f3054ef",
|
||||
"src/messenger/api.py": "cbf3ff4140314328aac9a4a0d8bda8063562e804af9e944d0a6d0e99cfa28d54",
|
||||
"src/messenger/cache.py": "2099eaf58a068f533281c4251a952c4c56f06647dfdffbad856498616ba7957b",
|
||||
"src/messenger/config.py": "9bfbe631a1bb37a27f5e6b5a342fcc0e375422a21063385a1994d9c00d0af72f",
|
||||
"src/messenger/cursor.py": "5ce4d9ecc2b5fea4b6fb1a7c59877801e3e364d6326c682ac2d4bfc343c24bfe",
|
||||
"src/messenger/db.py": "9c2d1c29fc61e76e65d73467ac8966803d6bf3fe18e1efb3697bfc0a403e4d0c",
|
||||
"src/messenger/errors.py": "165b237ad93185398adafba7dd215ef49fe2a9690cdfa9f76d853bfe717c73c6",
|
||||
"src/messenger/logging_utils.py": "a436d4c83a5e760a3fd6caa98eb54002eb2db95d6a1630b1ce8e407d2ebd4b0f",
|
||||
"src/messenger/migrate.py": "0ef8407dca3aac0aaf6749aa13c373ba61f95b37fecd660dbcaa3a9695ddce6f",
|
||||
"src/messenger/security.py": "1e22c3501c4b022d8ef3140aeb78c85f33297ac55e6aa59743aff406a54e5c15",
|
||||
"src/messenger/server.py": "983a972cf2e1eee4bb5dc8d6e011ae15af61b6a8f1a00b82e7e93434e1885668",
|
||||
"src/messenger/store.py": "1202ad070521271e09d007d85b835752a12eed0b89b73560d39496bd4411b823",
|
||||
"src/messenger/validation.py": "2e6bce61c9c7d61a48c82495811bfbaec521c2dd9bac2e3fb2fe3eb60a05ca72",
|
||||
"student-tests/concurrency.py": "93d7228b3e74aea0d61d8b0f8184c76e5ff281b7ef3ee7395ee5361a3ce54773",
|
||||
"student-tests/dependencies.py": "8032167aabe3fe7bd36de5f654df7af75fe61b627d6b73f8e7bbf01f3aaaf05c",
|
||||
"student-tests/experiments.py": "7574cb8bde0c05448bc70d0ef2c4e32a719bdda4667858495703433294768aa1",
|
||||
"student-tests/lab3.py": "2a5cf0af4b6f96977733dbc6b69c44ee24de3fca5dc4dd9436cb69b9d336e981",
|
||||
"student-tests/readiness.py": "450b0c2a1855e5b77b37d78c7d9cae37a64b9c7b199968a7239ab05cd50f8b69",
|
||||
"student-tests/run.py": "5f0b2965229adcb57e7f4280c1cde9300b2b9935ef2d5bf6fe58ef549db492b9",
|
||||
"student-tests/storage.py": "72827da48b8421020f2f4f202089185692a25908fe9adc265d7bb2d5453b4fc8",
|
||||
"student-tests/unit_test.py": "513591429afbdeca56db9825684f8cc0655e055d5b04d02cbb0e116e0961485a",
|
||||
"tests/README.md": "e0bfa7e196797f6a5bf6e44d8ad2ffeb82651927ff5d8484eb1c2dda1ec7dec5",
|
||||
"tests/client.py": "8d50976fc7c7bbb81e88a152f613c61ab25eaf53dba33c6a9dfab19f868d546d",
|
||||
"tests/load.py": "6d3511ea719c7a7e841083e73d435c86dcbfd9f4e5c2a07d62eb9614b01d9933",
|
||||
"tests/resilience.py": "1f96663a174b3978d24eff66f2741f5f8c9b788d3242261d37a972720c52788b",
|
||||
"tests/smoke.py": "52ac38eb343ff7d43e7543588e1da58ca06c9fce74b2b0d0bf0e4982bc23c1db"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Stopping API replica: api-a 6c3d78ba978d
|
||||
Flag --time has been deprecated, use --timeout instead
|
||||
6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee
|
||||
@@ -0,0 +1,459 @@
|
||||
{
|
||||
"scenario": "stop-api-a",
|
||||
"concurrency": 4,
|
||||
"warmup_seconds": 3,
|
||||
"elapsed_seconds": 17.972,
|
||||
"action_start_seconds": 3.153,
|
||||
"action_done_seconds": 3.64,
|
||||
"first_success_after_action_from_action_start_seconds": {
|
||||
"read": 0.488,
|
||||
"write": 0.504
|
||||
},
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "6c3d78ba978d",
|
||||
"instance": "6c3d78ba978d",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "b845543d6cc9",
|
||||
"instance": "b845543d6cc9",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "6c3d78ba978d",
|
||||
"instance": "6c3d78ba978d",
|
||||
"running": false,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "b845543d6cc9",
|
||||
"instance": "b845543d6cc9",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
}
|
||||
],
|
||||
"http": {
|
||||
"successful_requests": 4624,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 257.288,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 7.7,
|
||||
"p95": 49.281,
|
||||
"p99": 91.316
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"successful_requests": 2312,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 128.644,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 7.057,
|
||||
"p95": 46.452,
|
||||
"p99": 91.316
|
||||
}
|
||||
},
|
||||
"write": {
|
||||
"successful_requests": 2312,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 128.644,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 8.64,
|
||||
"p95": 51.717,
|
||||
"p99": 90.447
|
||||
}
|
||||
},
|
||||
"during_action": {
|
||||
"successful_requests": 89,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 182.746,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 11.397,
|
||||
"p95": 55.55,
|
||||
"p99": 62.607
|
||||
}
|
||||
},
|
||||
"confirmed_messages": 2312,
|
||||
"history_messages": 2313,
|
||||
"missing_or_changed_confirmations": 0,
|
||||
"duplicate_texts": 0,
|
||||
"resources": [
|
||||
{
|
||||
"at": 2.5,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "48.06%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "15.93%",
|
||||
"MemUsage": "40.78MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "1.57MB / 1.71MB",
|
||||
"PIDs": "3"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "49.35%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "15.97%",
|
||||
"MemUsage": "40.89MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "3.05MB / 3.29MB",
|
||||
"PIDs": "3"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.13MB",
|
||||
"CPUPerc": "8.40%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.59%",
|
||||
"MemUsage": "3.312MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "1.56MB / 1.54MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 442kB",
|
||||
"CPUPerc": "62.72%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.06%",
|
||||
"MemUsage": "31.04MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "4.49MB / 4.53MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "1.53%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.496MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "139kB / 92.3kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 5.518,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.26%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "19.73%",
|
||||
"MemUsage": "50.51MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "5.96MB / 6.36MB",
|
||||
"PIDs": "6"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.2MB",
|
||||
"CPUPerc": "3.82%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.69%",
|
||||
"MemUsage": "3.449MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "4.74MB / 4.78MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "48.60%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "5.94%",
|
||||
"MemUsage": "30.4MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "7.41MB / 7.54MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "0.63%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.41%",
|
||||
"MemUsage": "9.488MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "443kB / 294kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 8.562,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.60%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "16.24%",
|
||||
"MemUsage": "41.58MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "7.63MB / 8.11MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.25MB",
|
||||
"CPUPerc": "3.56%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.97%",
|
||||
"MemUsage": "3.805MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "5.77MB / 5.83MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "40.80%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.01%",
|
||||
"MemUsage": "30.79MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "9.27MB / 9.45MB",
|
||||
"PIDs": "9"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "1.25%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.61%",
|
||||
"MemUsage": "9.738MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "545kB / 359kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 11.599,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.75%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "16.37%",
|
||||
"MemUsage": "41.91MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "10.9MB / 11.5MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.28MB",
|
||||
"CPUPerc": "3.88%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.08%",
|
||||
"MemUsage": "3.941MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "6.57MB / 6.65MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "34.94%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.14%",
|
||||
"MemUsage": "31.42MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "11.1MB / 11.4MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "0.56%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.5MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "647kB / 423kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 14.615,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "49.79%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "16.43%",
|
||||
"MemUsage": "42.07MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "13.2MB / 13.9MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.34MB",
|
||||
"CPUPerc": "3.67%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.93%",
|
||||
"MemUsage": "3.75MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "7.74MB / 7.85MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "41.40%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "5.61%",
|
||||
"MemUsage": "28.73MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "12.9MB / 13.2MB",
|
||||
"PIDs": "8"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "1.33%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.5MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "742kB / 483kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 17.675,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.37%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "16.28%",
|
||||
"MemUsage": "41.68MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "14.1MB / 14.9MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.38MB",
|
||||
"CPUPerc": "1.10%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "2.89%",
|
||||
"MemUsage": "3.703MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "8.14MB / 8.26MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "0.13%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "5.55%",
|
||||
"MemUsage": "28.4MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "13.6MB / 13.9MB",
|
||||
"PIDs": "8"
|
||||
},
|
||||
{
|
||||
"BlockIO": "12.3kB / 0B",
|
||||
"CPUPerc": "0.82%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.61%",
|
||||
"MemUsage": "9.746MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "782kB / 508kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"maximum_gap_between_successes_during_action_seconds": {
|
||||
"read": 0.052,
|
||||
"write": 0.054
|
||||
},
|
||||
"note": "Closed-loop, four writers alternate POST and GET. Percentiles cover successful HTTP requests only; retries preserve key/body. Recovery is first success after hook completion measured from hook start; not an outage duration. One Docker host; no node/DB/LB HA claim."
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Stopping API replica: api-b b845543d6cc9
|
||||
Flag --time has been deprecated, use --timeout instead
|
||||
b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8
|
||||
@@ -0,0 +1,459 @@
|
||||
{
|
||||
"scenario": "stop-api-b",
|
||||
"concurrency": 4,
|
||||
"warmup_seconds": 3,
|
||||
"elapsed_seconds": 17.992,
|
||||
"action_start_seconds": 3.157,
|
||||
"action_done_seconds": 3.785,
|
||||
"first_success_after_action_from_action_start_seconds": {
|
||||
"read": 0.632,
|
||||
"write": 0.634
|
||||
},
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "6c3d78ba978d",
|
||||
"instance": "6c3d78ba978d",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "b845543d6cc9",
|
||||
"instance": "b845543d6cc9",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"id": "6c3d78ba978d",
|
||||
"instance": "6c3d78ba978d",
|
||||
"running": true,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"id": "b845543d6cc9",
|
||||
"instance": "b845543d6cc9",
|
||||
"running": false,
|
||||
"image": "sha256:5afb35dc1fe1012dfc2d33544dca759dd2b80c72335f7672adb562976c052855"
|
||||
}
|
||||
],
|
||||
"http": {
|
||||
"successful_requests": 5247,
|
||||
"failed_requests": 1,
|
||||
"successful_rps": 291.629,
|
||||
"error_ratio": 0.000191,
|
||||
"successful_latency_ms": {
|
||||
"p50": 6.107,
|
||||
"p95": 44.871,
|
||||
"p99": 88.255
|
||||
}
|
||||
},
|
||||
"read": {
|
||||
"successful_requests": 2624,
|
||||
"failed_requests": 0,
|
||||
"successful_rps": 145.842,
|
||||
"error_ratio": 0.0,
|
||||
"successful_latency_ms": {
|
||||
"p50": 5.332,
|
||||
"p95": 44.494,
|
||||
"p99": 86.918
|
||||
}
|
||||
},
|
||||
"write": {
|
||||
"successful_requests": 2623,
|
||||
"failed_requests": 1,
|
||||
"successful_rps": 145.787,
|
||||
"error_ratio": 0.000381,
|
||||
"successful_latency_ms": {
|
||||
"p50": 6.502,
|
||||
"p95": 45.353,
|
||||
"p99": 89.938
|
||||
}
|
||||
},
|
||||
"during_action": {
|
||||
"successful_requests": 250,
|
||||
"failed_requests": 1,
|
||||
"successful_rps": 398.334,
|
||||
"error_ratio": 0.003984,
|
||||
"successful_latency_ms": {
|
||||
"p50": 5.302,
|
||||
"p95": 43.575,
|
||||
"p99": 88.408
|
||||
}
|
||||
},
|
||||
"confirmed_messages": 2623,
|
||||
"history_messages": 2624,
|
||||
"missing_or_changed_confirmations": 0,
|
||||
"duplicate_texts": 0,
|
||||
"resources": [
|
||||
{
|
||||
"at": 2.486,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "40.55%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "16.05%",
|
||||
"MemUsage": "41.1MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "3.09MB / 3.31MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "36.71%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "22.57%",
|
||||
"MemUsage": "57.78MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "17.8MB / 18.9MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.59MB",
|
||||
"CPUPerc": "9.22%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.63%",
|
||||
"MemUsage": "4.648MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "11.8MB / 11.9MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "46.31%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.60%",
|
||||
"MemUsage": "33.79MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "18.2MB / 19.1MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "1.47%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.62%",
|
||||
"MemUsage": "9.754MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "929kB / 607kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 5.518,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "51.02%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "16.39%",
|
||||
"MemUsage": "41.96MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "6.35MB / 6.77MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.65MB",
|
||||
"CPUPerc": "3.49%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.35%",
|
||||
"MemUsage": "4.293MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "14MB / 14.2MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "52.66%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.61%",
|
||||
"MemUsage": "33.82MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "21.8MB / 22.7MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "1.26%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.43%",
|
||||
"MemUsage": "9.508MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "1.23MB / 810kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 8.556,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.29%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "16.35%",
|
||||
"MemUsage": "41.86MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "9.23MB / 9.78MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.7MB",
|
||||
"CPUPerc": "3.99%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.41%",
|
||||
"MemUsage": "4.363MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "15.2MB / 15.4MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "55.45%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.63%",
|
||||
"MemUsage": "33.95MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "23.9MB / 25MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "0.69%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.43%",
|
||||
"MemUsage": "9.512MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "1.35MB / 888kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 11.595,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "51.79%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "20.79%",
|
||||
"MemUsage": "53.23MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "11.9MB / 12.6MB",
|
||||
"PIDs": "6"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.74MB",
|
||||
"CPUPerc": "3.26%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.50%",
|
||||
"MemUsage": "4.484MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "16.4MB / 16.6MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "43.10%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.72%",
|
||||
"MemUsage": "34.39MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "26MB / 27.1MB",
|
||||
"PIDs": "10"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "1.22%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.5MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "1.47MB / 964kB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 14.634,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "50.19%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "16.53%",
|
||||
"MemUsage": "42.31MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "13.5MB / 14.3MB",
|
||||
"PIDs": "5"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.79MB",
|
||||
"CPUPerc": "3.30%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.76%",
|
||||
"MemUsage": "4.816MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "17.3MB / 17.6MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "36.98%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.79%",
|
||||
"MemUsage": "34.75MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "27.7MB / 28.8MB",
|
||||
"PIDs": "9"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "1.17%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.42%",
|
||||
"MemUsage": "9.504MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "1.6MB / 1.05MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"at": 17.664,
|
||||
"containers": [
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "12.09%",
|
||||
"Container": "6c3d78ba978dc3181aaec1a6021c04ee88387979fcad6e8af9b6b1dfb3dc13ee",
|
||||
"ID": "6c3d78ba978d",
|
||||
"MemPerc": "16.40%",
|
||||
"MemUsage": "41.99MiB / 256MiB",
|
||||
"Name": "mtusi-lab3-api-a-1",
|
||||
"NetIO": "15.5MB / 16.4MB",
|
||||
"PIDs": "1"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 0B",
|
||||
"CPUPerc": "0.00%",
|
||||
"Container": "b845543d6cc90451cdef8b5ba6128ef9c7a69255ef1922b4b80bb4bce45fc7c8",
|
||||
"ID": "b845543d6cc9",
|
||||
"MemPerc": "0.00%",
|
||||
"MemUsage": "0B / 0B",
|
||||
"Name": "mtusi-lab3-api-b-1",
|
||||
"NetIO": "0B / 0B",
|
||||
"PIDs": "0"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 1.84MB",
|
||||
"CPUPerc": "0.55%",
|
||||
"Container": "f453c77da6cf433723d727f47aff349fa1a2f15f362067ac0f323fb73e55dd9b",
|
||||
"ID": "f453c77da6cf",
|
||||
"MemPerc": "3.78%",
|
||||
"MemUsage": "4.836MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-nginx-1",
|
||||
"NetIO": "17.9MB / 18.2MB",
|
||||
"PIDs": "4"
|
||||
},
|
||||
{
|
||||
"BlockIO": "0B / 606kB",
|
||||
"CPUPerc": "0.08%",
|
||||
"Container": "a1902a1cda2dcc1664058dd1337874a6d2c82b403c49c1836fcdb4c15131e0a9",
|
||||
"ID": "a1902a1cda2d",
|
||||
"MemPerc": "6.26%",
|
||||
"MemUsage": "32.05MiB / 512MiB",
|
||||
"Name": "mtusi-lab3-postgres-1",
|
||||
"NetIO": "28.7MB / 29.9MB",
|
||||
"PIDs": "8"
|
||||
},
|
||||
{
|
||||
"BlockIO": "20.5kB / 0B",
|
||||
"CPUPerc": "0.57%",
|
||||
"Container": "c99cd0f569942ee8e95bda2009b4283bacfd382a5e7bf8ab5a2e8c097edc6229",
|
||||
"ID": "c99cd0f56994",
|
||||
"MemPerc": "7.61%",
|
||||
"MemUsage": "9.746MiB / 128MiB",
|
||||
"Name": "mtusi-lab3-redis-1",
|
||||
"NetIO": "1.66MB / 1.09MB",
|
||||
"PIDs": "6"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"maximum_gap_between_successes_during_action_seconds": {
|
||||
"read": 0.091,
|
||||
"write": 0.074
|
||||
},
|
||||
"note": "Closed-loop, four writers alternate POST and GET. Percentiles cover successful HTTP requests only; retries preserve key/body. Recovery is first success after hook completion measured from hook start; not an outage duration. One Docker host; no node/DB/LB HA claim."
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
Container mtusi-lab3-api-a-run-58a0efacd2d1 Creating
|
||||
Container mtusi-lab3-api-a-run-58a0efacd2d1 Created
|
||||
PASS: actual multi-statement chat transaction rollback
|
||||
PASS: message/result atomic rollback; same key usable after transient failure
|
||||
Limit (cost=0.41..9.99 rows=51 width=85) (actual time=0.006..0.029 rows=51 loops=1)
|
||||
Buffers: shared hit=55
|
||||
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.41..1879.29 rows=10001 width=85) (actual time=0.006..0.026 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'ccabcfa1-0126-4cf1-9c51-9f544add2f10'::uuid)
|
||||
Buffers: shared hit=55
|
||||
Planning:
|
||||
Buffers: shared hit=48
|
||||
Planning Time: 0.105 ms
|
||||
Execution Time: 0.036 ms
|
||||
PASS: history index on at least 10000 messages
|
||||
PASS: repeated versioned migrations preserved existing messages
|
||||
@@ -0,0 +1,16 @@
|
||||
python3 student-tests/run.py --base-url "http://localhost:18080"
|
||||
student-tests: 13 cases against http://localhost:18080
|
||||
PASS health_and_request_id
|
||||
PASS unknown_route_and_method_are_json_404
|
||||
PASS malformed_json_is_rejected
|
||||
PASS duplicate_username_is_409
|
||||
PASS user_payload_validation
|
||||
PASS basic_login_issues_bearer_session
|
||||
PASS bearer_required_for_business_endpoints
|
||||
PASS logout_revokes_session
|
||||
PASS foreign_chat_is_invisible
|
||||
PASS message_payload_validation
|
||||
PASS text_limits_and_unicode_roundtrip
|
||||
PASS multi_page_traversal_is_exact_and_stable
|
||||
PASS default_page_limit_is_fifty
|
||||
13 passed, 0 failed
|
||||
@@ -0,0 +1,8 @@
|
||||
docker compose run --rm --no-deps -v "/Users/atmose/Documents/Labs_and_etc/ВП/mtusi2026lab3-fork/student-tests:/app/student-tests:ro" api-a python /app/student-tests/unit_test.py
|
||||
Container mtusi-lab3-api-a-run-9b35a939ed13 Creating
|
||||
Container mtusi-lab3-api-a-run-9b35a939ed13 Created
|
||||
.....
|
||||
----------------------------------------------------------------------
|
||||
Ran 5 tests in 0.902s
|
||||
|
||||
OK
|
||||
@@ -0,0 +1,4 @@
|
||||
/Users/atmose/Documents/Codex/2026-11-06/new-chat/work/lab3-venv/bin/python scripts/validate_openapi.py
|
||||
/Users/atmose/Documents/Codex/2026-11-06/new-chat/work/lab3-venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
|
||||
warnings.warn(
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
+14
-4
@@ -1,7 +1,17 @@
|
||||
# Инфраструктура студента
|
||||
# Инфраструктура — лабораторная 3
|
||||
|
||||
Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли.
|
||||
Стенд: Docker Compose, Nginx, две API-реплики, PostgreSQL и Redis. Все зависимости описаны в [compose.yaml](../compose.yaml); команды запуска и результаты — в [REPORT.md](../REPORT.md).
|
||||
|
||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
||||
| Файл | Назначение |
|
||||
| --- | --- |
|
||||
| `nginx.conf` | единая точка входа, таймауты, DNS, пассивное исключение недоступных backend |
|
||||
| `upstreams.conf` | исходный список `api-a` и `api-b`, без привязки пользователя к реплике |
|
||||
| `healthwatch.sh` | параллельные проверки `/health/ready`, обновление рабочего upstream и reload Nginx |
|
||||
|
||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
||||
Контроллер readiness работает внутри контейнера Nginx. Он не получает Docker socket и секреты. Проверки двух API выполняются параллельно: пауза между циклами — 1 s, timeout каждой проверки — 1 s. При изменении готовности рабочая конфигурация `/etc/nginx/upstreams.conf` обновляется и проверяется через `nginx -t`, затем выполняется reload. Исходные файлы репозитория смонтированы только для чтения.
|
||||
|
||||
`docker healthcheck` показывает состояние контейнера, а `healthwatch.sh` отдельно связывает readiness API с маршрутизацией. При плановом обновлении `scripts/manage.py` устанавливает управляющую запись `drain:<generation>` в `/tmp/control-api-a` или `/tmp/control-api-b`; контроллер исключает эту реплику независимо от её readiness. После записи `serve:<generation>` реплика возвращается только при успешной проверке.
|
||||
|
||||
Плановая замена ждёт исключения backend и 6.5 s завершения старых worker Nginx (`worker_shutdown_timeout=6s`). Затем пересоздаётся одна API, проверяется Docker health и readiness контроллера, после чего можно заменять следующую. Если новая версия не готова, скрипт останавливается, сохраняя вторую реплику. Исправить образ и вернуть обе готовые реплики: `python3 scripts/manage.py restore`. Эта команда также снимает режим drain и ждёт возврата backend в Nginx. Контроллер подтверждает конкретное поколение управляющей записи только после применения конфигурации; рабочий файл заменяется атомарно. Перед остановкой выбранной реплики rolling-скрипт отдельно подтверждает включение другой реплики в LB.
|
||||
|
||||
Внутренние порты API, PostgreSQL и Redis наружу не публикуются. Наружу доступен только `127.0.0.1:${PUBLIC_PORT}:8080` у Nginx. `container_name` не используется. Два отдельных API-service выбраны для явной поочерёдной замены; изменение их числа требует согласованного изменения upstream и контроллера.
|
||||
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#!/bin/sh
|
||||
# Active readiness bridge for open-source Nginx. No Docker socket or credentials.
|
||||
set -eu
|
||||
probe() {
|
||||
if wget -T 1 -q -O /dev/null "http://$1:8080/health/ready" 2>/dev/null; then
|
||||
echo up > "/tmp/$1.health"
|
||||
else
|
||||
echo down > "/tmp/$1.health"
|
||||
fi
|
||||
}
|
||||
control() { cat "/tmp/control-$1" 2>/dev/null || echo serve:boot; }
|
||||
echo $$ > /tmp/healthwatch.pid
|
||||
while :; do
|
||||
sleep 1
|
||||
probe api-a &
|
||||
first=$!
|
||||
probe api-b &
|
||||
second=$!
|
||||
wait "$first"
|
||||
wait "$second"
|
||||
# Read desired state AFTER probes. A generation is acknowledged only after
|
||||
# the matching upstream configuration has been applied, never from a stale probe.
|
||||
a_control=$(control api-a)
|
||||
b_control=$(control api-b)
|
||||
a_state=$(cat /tmp/api-a.health)
|
||||
b_state=$(cat /tmp/api-b.health)
|
||||
case "$a_control" in drain:*) a_state=down;; esac
|
||||
case "$b_control" in drain:*) b_state=down;; esac
|
||||
: > /tmp/upstreams.next
|
||||
for service in api-a api-b; do
|
||||
if [ "$service" = api-a ]; then state=$a_state; else state=$b_state; fi
|
||||
suffix=''
|
||||
[ "$state" = up ] || suffix=' down'
|
||||
printf 'server %s:8080 resolve max_fails=1 fail_timeout=2s%s;\n' "$service" "$suffix" >> /tmp/upstreams.next
|
||||
done
|
||||
if ! cmp -s /tmp/upstreams.next /etc/nginx/upstreams.conf; then
|
||||
cp /etc/nginx/upstreams.conf /tmp/upstreams.previous
|
||||
# Rename on the same filesystem: readers never see missing/partial config.
|
||||
cp /tmp/upstreams.next /etc/nginx/upstreams.next
|
||||
mv -f /etc/nginx/upstreams.next /etc/nginx/upstreams.conf
|
||||
if nginx -t && nginx -s reload; then
|
||||
echo 'readiness upstream set updated'
|
||||
else
|
||||
cp /tmp/upstreams.previous /etc/nginx/upstreams.next
|
||||
mv -f /etc/nginx/upstreams.next /etc/nginx/upstreams.conf
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
printf '%s %s\n' "$a_control" "$a_state" > /tmp/applied-api-a.next
|
||||
mv -f /tmp/applied-api-a.next /tmp/applied-api-a
|
||||
printf '%s %s\n' "$b_control" "$b_state" > /tmp/applied-api-b.next
|
||||
mv -f /tmp/applied-api-b.next /tmp/applied-api-b
|
||||
done
|
||||
@@ -0,0 +1,35 @@
|
||||
worker_processes 1;
|
||||
worker_shutdown_timeout 6s;
|
||||
events { worker_connections 1024; }
|
||||
http {
|
||||
resolver 127.0.0.11 valid=1s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
log_format lab '$time_iso8601 $request_method $uri $status upstream=$upstream_addr '
|
||||
'instance=$upstream_http_x_instance_id time=$request_time';
|
||||
access_log /dev/stdout lab;
|
||||
error_log /dev/stderr warn;
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
include /etc/nginx/upstreams.conf;
|
||||
}
|
||||
server {
|
||||
listen 8080;
|
||||
client_max_body_size 1m;
|
||||
client_body_timeout 5s;
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection close;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Request-Id $http_x_request_id;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 5s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 6s;
|
||||
# Do not enable non_idempotent: uncertain POST is retried by the
|
||||
# client with the ORIGINAL key; chats and registration are unsafe.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
server api-a:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
server api-b:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
@@ -0,0 +1,51 @@
|
||||
-- Migration 0001: initial schema for lab2.
|
||||
-- Idempotent: safe to re-run on an existing database.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE,
|
||||
password_hash text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Keyset pagination index: history by (chat_id, created_at, id) avoids
|
||||
-- full table scan on every page read.
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id
|
||||
ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_chat_id
|
||||
ON messages (chat_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash text PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id
|
||||
ON sessions (user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at
|
||||
ON sessions (expires_at);
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Additive migration: compatible with the lab2 schema and existing messages.
|
||||
CREATE TABLE IF NOT EXISTS message_idempotency (
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method text NOT NULL,
|
||||
path text NOT NULL,
|
||||
key text NOT NULL,
|
||||
request_hash text NOT NULL,
|
||||
response_status integer NOT NULL,
|
||||
response_body jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, method, path, key)
|
||||
);
|
||||
-- Results are retained indefinitely in this lab (therefore at least 24 hours).
|
||||
-- There is intentionally no independent cache expiry that could allow duplicates.
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate local credentials once, never print them or overwrite existing files."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import secrets
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
folder = root / 'secrets'
|
||||
folder.mkdir(mode=0o700, exist_ok=True)
|
||||
password_file = folder / 'postgres_password'
|
||||
|
||||
def save(name, value):
|
||||
path = folder / name
|
||||
if path.exists():
|
||||
return
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
with os.fdopen(fd, 'w') as handle:
|
||||
handle.write(value + '\n')
|
||||
|
||||
save('postgres_password', secrets.token_hex(24))
|
||||
password = password_file.read_text().strip()
|
||||
save('database_url', 'postgresql://messenger:%s@postgres:5432/messenger' % password)
|
||||
save('cache_url', 'redis://redis:6379/0')
|
||||
save('cursor_secret', secrets.token_hex(32))
|
||||
print('PASS: local secret files ready (existing values preserved)')
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Collect sanitized environment/source fingerprints, without Git or credentials."""
|
||||
import ast
|
||||
import hashlib
|
||||
import json
|
||||
import platform
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import datetime as dt
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
OUT = ROOT / 'evidence'
|
||||
|
||||
|
||||
def output(*cmd):
|
||||
result = subprocess.run(cmd, cwd=ROOT, text=True, capture_output=True)
|
||||
return result.stdout.strip() if result.returncode == 0 else 'Недоступно в текущем окружении'
|
||||
|
||||
|
||||
def main():
|
||||
OUT.mkdir(exist_ok=True)
|
||||
report = {
|
||||
'date': dt.datetime.now(dt.timezone(dt.timedelta(hours=3))).isoformat(),
|
||||
'timezone': 'Europe/Moscow', 'os': platform.platform(), 'architecture': platform.machine(),
|
||||
'cpu': output('sysctl', '-n', 'machdep.cpu.brand_string'),
|
||||
'logical_cpus': output('sysctl', '-n', 'hw.logicalcpu'),
|
||||
'host_memory_bytes': output('sysctl', '-n', 'hw.memsize'),
|
||||
'docker_version': output('docker', 'version', '--format', '{{.Server.Version}}'),
|
||||
'compose_version': output('docker', 'compose', 'version', '--short'),
|
||||
'docker_resources': json.loads(output('docker', 'info', '--format',
|
||||
'{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}')),
|
||||
'commit': 'Не фиксировался: Git оставлен пользователю',
|
||||
'note': 'lab2 remained running separately on port 8080; lab3 measured on 18080. Shared-host background work affects timings.',
|
||||
}
|
||||
(OUT / 'environment.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n')
|
||||
sources = {}
|
||||
for file in sorted(ROOT.rglob('*')):
|
||||
name = file.relative_to(ROOT)
|
||||
if not file.is_file() or any(p in {'.git', '.venv', '__pycache__', 'evidence', 'secrets'} for p in name.parts):
|
||||
continue
|
||||
if file.name == '.env' or file.name.startswith('.env.') and file.name != '.env.example':
|
||||
continue
|
||||
if file.suffix not in {'.py', '.sql', '.yaml', '.json', '.md', '.sh', '.conf', '.txt', '.http'} and file.name not in {'Dockerfile', 'Makefile', '.dockerignore', '.gitignore', '.env.example'}:
|
||||
continue
|
||||
if file.suffix == '.py':
|
||||
ast.parse(file.read_text(), filename=str(name))
|
||||
sources[str(name)] = hashlib.sha256(file.read_bytes()).hexdigest()
|
||||
(OUT / 'source-sha256.json').write_text(json.dumps(sources, ensure_ascii=False, indent=2) + '\n')
|
||||
script = "import hashlib,json,pathlib; p=pathlib.Path('/app/src'); print(json.dumps({str(f.relative_to(p)):hashlib.sha256(f.read_bytes()).hexdigest() for f in p.rglob('*.py')}))"
|
||||
expected = {str(p.relative_to(ROOT / 'src')): hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
for p in (ROOT / 'src').rglob('*.py')}
|
||||
for service in ('api-a', 'api-b'):
|
||||
actual = json.loads(output('docker', 'compose', 'exec', '-T', service, 'python', '-c', script))
|
||||
assert actual == expected, 'running image differs from source: ' + service
|
||||
(OUT / 'source-image-check.txt').write_text('PASS: running api-a and api-b Python sources match src/ SHA-256 exactly\n')
|
||||
print('PASS: source syntax, running image/source fingerprints, sanitized environment')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,157 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit, bounded operator actions for this lab's Compose project only."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SERVICES = ('api-a', 'api-b')
|
||||
|
||||
|
||||
def command(args, capture=False):
|
||||
result = subprocess.run(args, cwd=ROOT, check=True, text=True,
|
||||
stdout=subprocess.PIPE if capture else None)
|
||||
return result.stdout.strip() if capture else None
|
||||
|
||||
|
||||
def compose(*args, capture=False):
|
||||
return command(['docker', 'compose', '--env-file', '.env', *args], capture)
|
||||
|
||||
|
||||
def inspect(container):
|
||||
return json.loads(command(['docker', 'inspect', container], True))[0]
|
||||
|
||||
|
||||
def containers(service):
|
||||
return compose('ps', '-a', '-q', service, capture=True).splitlines()
|
||||
|
||||
|
||||
def start_services(*services):
|
||||
for service in services:
|
||||
for container in containers(service):
|
||||
command(['docker', 'start', container])
|
||||
|
||||
|
||||
def wait_healthy(service, timeout=90):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
ids = containers(service)
|
||||
if ids and all(inspect(c)['State'].get('Health', {}).get('Status') == 'healthy' for c in ids):
|
||||
return
|
||||
time.sleep(.5)
|
||||
raise RuntimeError('%s did not become healthy' % service)
|
||||
|
||||
|
||||
def target_service(target):
|
||||
if not target:
|
||||
raise RuntimeError('TARGET_INSTANCE_ID is required')
|
||||
for service in SERVICES:
|
||||
for container in containers(service):
|
||||
data = inspect(container)
|
||||
if data['Config']['Hostname'] == target:
|
||||
return service, container
|
||||
raise RuntimeError('Unknown API instance alias')
|
||||
|
||||
|
||||
def set_backend(service, enabled):
|
||||
# Fixed allowlist; shell receives no untrusted instance or credential input.
|
||||
if service not in SERVICES:
|
||||
raise ValueError('Unknown service')
|
||||
token = ('serve:' if enabled else 'drain:') + uuid.uuid4().hex
|
||||
path = '/tmp/control-' + service
|
||||
compose('exec', '-T', 'nginx', 'sh', '-ec',
|
||||
"printf '%s\\n' '" + token + "' > " + path + '.next; mv -f ' + path + '.next ' + path)
|
||||
deadline = time.monotonic() + 10
|
||||
expected = token + (' up' if enabled else ' down')
|
||||
while time.monotonic() < deadline:
|
||||
ack = compose('exec', '-T', 'nginx', 'sh', '-c',
|
||||
'cat /tmp/applied-' + service + ' 2>/dev/null || true', capture=True)
|
||||
if ack == expected:
|
||||
return
|
||||
time.sleep(.2)
|
||||
raise RuntimeError('Readiness controller did not acknowledge backend state for ' + service)
|
||||
|
||||
|
||||
def stop_one():
|
||||
service, container = target_service(os.environ.get('TARGET_INSTANCE_ID'))
|
||||
running = [c for name in SERVICES for c in containers(name)
|
||||
if inspect(c)['State'].get('Health', {}).get('Status') == 'healthy'
|
||||
and inspect(c)['State']['Running']]
|
||||
if len(running) < 2:
|
||||
raise RuntimeError('Restore the other replica before this experiment')
|
||||
print('Stopping API replica:', service, container[:12], flush=True)
|
||||
command(['docker', 'stop', '--time', '8', container])
|
||||
if inspect(container)['State']['Running']:
|
||||
raise RuntimeError('Target is still running')
|
||||
|
||||
|
||||
def restart():
|
||||
# Never start the one-shot migration service as part of persistence testing.
|
||||
compose('restart', 'postgres', 'redis', *SERVICES, 'nginx')
|
||||
for service in (*SERVICES, 'nginx'):
|
||||
wait_healthy(service)
|
||||
|
||||
|
||||
def rolling_update():
|
||||
# Run additive migrations once before replacing either API version.
|
||||
compose('run', '--rm', '--no-deps', 'migrate')
|
||||
for service in SERVICES:
|
||||
other = SERVICES[1] if service == SERVICES[0] else SERVICES[0]
|
||||
wait_healthy(other)
|
||||
set_backend(other, True)
|
||||
before = containers(service)
|
||||
set_backend(service, False)
|
||||
# The old nginx worker can retain the old upstream table until it exits.
|
||||
# worker_shutdown_timeout=6s bounds that drain; no new work then reaches it.
|
||||
time.sleep(6.5)
|
||||
compose('up', '-d', '--no-deps', '--force-recreate', '--no-build', service)
|
||||
wait_healthy(service)
|
||||
set_backend(service, True)
|
||||
time.sleep(2) # DNS valid=1s + readiness before moving to the next replica.
|
||||
after = containers(service)
|
||||
if before == after:
|
||||
raise RuntimeError('Replica was not replaced')
|
||||
print(json.dumps({'service': service, 'before': before, 'after': after}), flush=True)
|
||||
|
||||
|
||||
def restore():
|
||||
start_services(*SERVICES)
|
||||
for service in SERVICES:
|
||||
wait_healthy(service)
|
||||
for service in SERVICES:
|
||||
set_backend(service, True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=('stop-one', 'restart', 'restore', 'rolling-update', 'status'))
|
||||
args = parser.parse_args()
|
||||
if args.action == 'stop-one':
|
||||
stop_one()
|
||||
elif args.action == 'restart':
|
||||
restart()
|
||||
elif args.action == 'rolling-update':
|
||||
rolling_update()
|
||||
elif args.action == 'restore':
|
||||
restore()
|
||||
else:
|
||||
for service in ('postgres', 'redis', *SERVICES, 'nginx'):
|
||||
for container in containers(service):
|
||||
data = inspect(container)
|
||||
print(json.dumps({'service': service, 'id': data['Id'][:12],
|
||||
'instance': data['Config']['Hostname'],
|
||||
'running': data['State']['Running'],
|
||||
'started_at': data['State']['StartedAt'],
|
||||
'health': data['State'].get('Health', {}).get('Status'),
|
||||
'image_id': data['Image'],
|
||||
'cpu_limit': data['HostConfig']['NanoCpus'] / 1e9,
|
||||
'memory_limit_bytes': data['HostConfig']['Memory'],
|
||||
'published_ports': data['HostConfig'].get('PortBindings')}))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
|
||||
exec python3 scripts/manage.py restart
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
|
||||
exec python3 scripts/manage.py rolling-update
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
|
||||
exec python3 scripts/manage.py stop-one
|
||||
@@ -0,0 +1,3 @@
|
||||
"""MTUSI Messenger lab1: in-memory HTTP API (standard library only)."""
|
||||
|
||||
__version__ = '1.0.0'
|
||||
@@ -0,0 +1,7 @@
|
||||
"""Entry point: python -m messenger."""
|
||||
import sys
|
||||
|
||||
from .server import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,252 @@
|
||||
"""Domain logic and routing for the lab2 API: PostgreSQL state, Bearer sessions."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import datetime as dt
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from . import cursor as cursor_codec
|
||||
from . import security
|
||||
from . import validation
|
||||
from .db import Database
|
||||
from .errors import ApiError, Conflict, InvalidRequest, NotFound, Unauthorized, Unavailable, UnsupportedMediaType
|
||||
from .store import Store, utc_now, rfc3339
|
||||
|
||||
SCOPE_CHATS = 'chats'
|
||||
SCOPE_MESSAGES = 'chat/{chat_id}/messages'
|
||||
|
||||
|
||||
class Request:
|
||||
"""One parsed HTTP request; the API layer never touches sockets."""
|
||||
|
||||
__slots__ = ('method', 'path', 'query', 'headers', 'body', 'request_id', 'client')
|
||||
|
||||
def __init__(self, method, path, query, headers, body, request_id, client):
|
||||
self.method = method
|
||||
self.path = path
|
||||
self.query = query
|
||||
self.headers = headers
|
||||
self.body = body
|
||||
self.request_id = request_id
|
||||
self.client = client
|
||||
|
||||
|
||||
class Api:
|
||||
ROUTES = (
|
||||
('GET', re.compile(r'^/health/live$'), 'health_live'),
|
||||
('GET', re.compile(r'^/health/ready$'), 'health_ready'),
|
||||
('POST', re.compile(r'^/api/v1/users$'), 'create_user'),
|
||||
('GET', re.compile(r'^/api/v1/users/me$'), 'current_user'),
|
||||
('GET', re.compile(r'^/api/v1/chats$'), 'list_chats'),
|
||||
('POST', re.compile(r'^/api/v1/chats$'), 'create_chat'),
|
||||
('GET', re.compile(r'^/api/v1/chats/(?P<chat_id>[^/]+)$'), 'get_chat'),
|
||||
('GET', re.compile(r'^/api/v1/chats/(?P<chat_id>[^/]+)/messages$'), 'list_messages'),
|
||||
('POST', re.compile(r'^/api/v1/chats/(?P<chat_id>[^/]+)/messages$'), 'post_message'),
|
||||
('POST', re.compile(r'^/api/v1/auth/sessions$'), 'create_session'),
|
||||
('DELETE', re.compile(r'^/api/v1/auth/sessions/current$'), 'revoke_session'),
|
||||
)
|
||||
|
||||
def __init__(self, config, store: Store, database: Database):
|
||||
self.config = config
|
||||
self.store = store
|
||||
self.database = database
|
||||
|
||||
def dispatch(self, request):
|
||||
"""Return (status, payload). Only ApiError and payloads leave this method."""
|
||||
path_matched = False
|
||||
for method, pattern, handler in self.ROUTES:
|
||||
match = pattern.match(request.path)
|
||||
if match:
|
||||
path_matched = True
|
||||
if method == request.method:
|
||||
return getattr(self, handler)(request, match.groupdict())
|
||||
raise NotFound('Маршрут не найден' if not path_matched else 'Метод не поддерживается')
|
||||
|
||||
# ------------------------------------------------------------- health
|
||||
def health_live(self, request, path_params):
|
||||
return 200, {'status': 'ok'}
|
||||
|
||||
def health_ready(self, request, path_params):
|
||||
try:
|
||||
self.database.ping()
|
||||
except Exception:
|
||||
raise Unavailable('База данных недоступна')
|
||||
return 200, {'status': 'ok'}
|
||||
|
||||
# -------------------------------------------------------------- users
|
||||
def create_user(self, request, path_params):
|
||||
payload = self._json_object(request)
|
||||
validation.require_fields(payload, ('username', 'display_name', 'password'),
|
||||
('username', 'display_name', 'password'))
|
||||
username = validation.require_username(payload)
|
||||
display_name = validation.require_text_field(
|
||||
payload, 'display_name', 1, self.config.max_display_name_code_points)
|
||||
password = validation.require_text_field(
|
||||
payload, 'password', 12, 128, strip_check=False)
|
||||
password_hash = security.hash_password(password)
|
||||
return 201, self.store.create_user(username, display_name, password_hash)
|
||||
|
||||
def current_user(self, request, path_params):
|
||||
return 200, self._authenticate(request)
|
||||
|
||||
# -------------------------------------------------------------- chats
|
||||
def create_chat(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
payload = self._json_object(request)
|
||||
validation.require_fields(payload, ('title', 'member_ids'), ('title', 'member_ids'))
|
||||
title = validation.require_text_field(payload, 'title', 1,
|
||||
self.config.max_title_code_points)
|
||||
members = validation.require_member_ids(payload, 100)
|
||||
if actor['id'] not in members:
|
||||
raise InvalidRequest('Создатель чата должен быть указан в member_ids')
|
||||
if not self.store.users_exist(members):
|
||||
raise InvalidRequest('member_ids содержит неизвестных пользователей')
|
||||
return 201, self.store.create_chat(title, members)
|
||||
|
||||
def list_chats(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
limit, after_key = self._pagination(request, SCOPE_CHATS)
|
||||
items, has_more = self.store.page_chats(actor['id'], after_key, limit)
|
||||
for item in items:
|
||||
item['member_ids'] = self._chat_member_ids(item['id'])
|
||||
return 200, {'items': items, 'next_cursor': self._next_cursor(SCOPE_CHATS, items, has_more)}
|
||||
|
||||
def get_chat(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
chat = self._member_chat(actor, path_params['chat_id'])
|
||||
chat['member_ids'] = self._chat_member_ids(chat['id'])
|
||||
return 200, chat
|
||||
|
||||
# ----------------------------------------------------------- messages
|
||||
def post_message(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
chat = self._member_chat(actor, path_params['chat_id'])
|
||||
key = validation.require_idempotency_key(request.headers.get('Idempotency-Key'))
|
||||
payload = self._json_object(request)
|
||||
validation.require_fields(payload, ('text',), ('text',))
|
||||
text = validation.require_text_field(payload, 'text', 1,
|
||||
self.config.max_text_code_points)
|
||||
return 201, self.store.add_message(chat['id'], actor['id'], text, key)
|
||||
|
||||
def list_messages(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
chat = self._member_chat(actor, path_params['chat_id'])
|
||||
scope = SCOPE_MESSAGES.format(chat_id=chat['id'])
|
||||
limit, after_key = self._pagination(request, scope)
|
||||
items, has_more = self.store.page_messages(chat['id'], after_key, limit)
|
||||
return 200, {'items': items, 'next_cursor': self._next_cursor(scope, items, has_more)}
|
||||
|
||||
# ----------------------------------------------------------- sessions
|
||||
def create_session(self, request, path_params):
|
||||
"""Login: only Authorization: Basic username:password, no JSON body."""
|
||||
authorization = request.headers.get('Authorization') or ''
|
||||
if not authorization.lower().startswith('basic '):
|
||||
raise Unauthorized('Требуется авторизация Basic')
|
||||
try:
|
||||
raw = base64.b64decode(authorization[len('basic '):].strip()).decode('utf-8')
|
||||
except Exception:
|
||||
raise Unauthorized('Некорректные учётные данные') from None
|
||||
if ':' not in raw:
|
||||
raise Unauthorized('Некорректные учётные данные')
|
||||
username, password = raw.split(':', 1)
|
||||
row = self.store.user_by_username(username)
|
||||
if row is None or not security.verify_password(password, row['password_hash']):
|
||||
raise Unauthorized('Неверное имя пользователя или пароль', www_authenticate='Basic realm="messenger", charset="UTF-8"')
|
||||
token = security.generate_token()
|
||||
token_hash = security.hash_token(token)
|
||||
expires_at = utc_now() + dt.timedelta(seconds=self.config.session_ttl_seconds)
|
||||
self.store.create_session(token_hash, row['id'], expires_at)
|
||||
user = self.store._public_user({
|
||||
'id': row['id'],
|
||||
'username': row['username'],
|
||||
'display_name': row['display_name'],
|
||||
'created_at': row['created_at'],
|
||||
})
|
||||
return 201, {
|
||||
'token': token,
|
||||
'token_type': 'Bearer',
|
||||
'expires_at': rfc3339(expires_at),
|
||||
'user': user,
|
||||
}
|
||||
|
||||
def revoke_session(self, request, path_params):
|
||||
actor = self._authenticate(request)
|
||||
token = self._bearer_token(request)
|
||||
self.store.delete_session(security.hash_token(token))
|
||||
return 204, None
|
||||
|
||||
# ------------------------------------------------------------- helpers
|
||||
def _authenticate(self, request):
|
||||
"""Lab2 identity: only Authorization: Bearer <token> identifies the actor.
|
||||
|
||||
X-User-Id is ignored: it does not change the session owner and alone
|
||||
grants no access. Basic on business endpoints is 401.
|
||||
"""
|
||||
token = self._bearer_token(request)
|
||||
if token is None:
|
||||
raise Unauthorized('Требуется идентификация пользователя',
|
||||
www_authenticate='Bearer realm="messenger"')
|
||||
user_id = self.store.get_session(security.hash_token(token))
|
||||
if user_id is None:
|
||||
raise Unauthorized('Сессия недействительна или истекла',
|
||||
www_authenticate='Bearer realm="messenger"')
|
||||
user = self.store.user(user_id)
|
||||
if user is None:
|
||||
raise Unauthorized('Пользователь не найден',
|
||||
www_authenticate='Bearer realm="messenger"')
|
||||
return user
|
||||
|
||||
def _bearer_token(self, request):
|
||||
authorization = request.headers.get('Authorization') or ''
|
||||
if not authorization.lower().startswith('bearer '):
|
||||
return None
|
||||
return authorization[len('bearer '):].strip()
|
||||
|
||||
def _chat_member_ids(self, chat_id):
|
||||
with self.database.connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT user_id FROM chat_members WHERE chat_id = %s', (str(chat_id),))
|
||||
return [str(row['user_id']) for row in cur.fetchall()]
|
||||
|
||||
def _member_chat(self, actor, raw_chat_id):
|
||||
"""Foreign and missing chats are indistinguishable: both are 404."""
|
||||
chat_id = validation.require_uuid_string(raw_chat_id, 'chat_id')
|
||||
chat = self.store.chat(chat_id)
|
||||
if chat is None or not self.store.is_member(chat, actor['id']):
|
||||
raise NotFound('Чат не найден')
|
||||
return chat
|
||||
|
||||
def _json_object(self, request):
|
||||
content_type = request.headers.get('Content-Type') or ''
|
||||
if content_type.split(';')[0].strip().lower() != 'application/json':
|
||||
raise UnsupportedMediaType('Ожидается Content-Type: application/json')
|
||||
if not request.body:
|
||||
raise InvalidRequest('Требуется JSON-тело запроса')
|
||||
try:
|
||||
payload = json.loads(request.body.decode('utf-8'))
|
||||
except (UnicodeError, ValueError):
|
||||
raise InvalidRequest('Тело запроса не является корректным JSON') from None
|
||||
return validation.require_object(payload)
|
||||
|
||||
def _pagination(self, request, scope):
|
||||
raw_limit = (request.query.get('limit') or [None])[0]
|
||||
limit = validation.parse_limit(raw_limit, self.config.default_page_limit,
|
||||
self.config.max_page_limit)
|
||||
raw_cursor = validation.parse_cursor((request.query.get('cursor') or [None])[0])
|
||||
if raw_cursor is None:
|
||||
return limit, None
|
||||
try:
|
||||
stamp, identifier = cursor_codec.decode(raw_cursor, scope, self.config.cursor_secret)
|
||||
except cursor_codec.InvalidCursor:
|
||||
raise InvalidRequest('cursor некорректен или относится к другому ресурсу') from None
|
||||
return limit, (stamp, identifier)
|
||||
|
||||
def _next_cursor(self, scope, items, has_more):
|
||||
if not has_more or not items:
|
||||
return None
|
||||
last = items[-1]
|
||||
return cursor_codec.encode(scope, last['created_at'], last['id'],
|
||||
self.config.cursor_secret)
|
||||
@@ -0,0 +1,95 @@
|
||||
"""Redis session cache with TTL and graceful degradation.
|
||||
|
||||
PostgreSQL is the source of truth for sessions. Redis is a look-aside cache:
|
||||
- write: session is persisted in PostgreSQL first, then copied to Redis with TTL
|
||||
- read: Redis first (fast); on miss, read PostgreSQL and backfill Redis
|
||||
- when Redis is unavailable: fall back to PostgreSQL only, never treat an
|
||||
anonymous request as trusted
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import redis
|
||||
|
||||
from .config import Config
|
||||
|
||||
logger = logging.getLogger('messenger.cache')
|
||||
|
||||
|
||||
class SessionCache:
|
||||
"""Look-aside cache for session tokens in Redis/Valkey."""
|
||||
|
||||
def __init__(self, config: Config):
|
||||
self._config = config
|
||||
self._client = None
|
||||
self._available = False
|
||||
|
||||
def open(self):
|
||||
if not self._config.cache_url:
|
||||
self._available = False
|
||||
return
|
||||
try:
|
||||
self._client = redis.Redis.from_url(
|
||||
self._config.cache_url,
|
||||
socket_connect_timeout=2,
|
||||
socket_timeout=2,
|
||||
socket_keepalive=True,
|
||||
health_check_interval=30,
|
||||
decode_responses=True,
|
||||
)
|
||||
self._client.ping()
|
||||
self._available = True
|
||||
logger.info('cache_connected', extra={'event': 'cache_connected'})
|
||||
except Exception as exc: # noqa: BLE001 - Redis is optional, degrade safely
|
||||
self._available = False
|
||||
self._client = None
|
||||
logger.warning('cache_unavailable', extra={
|
||||
'event': 'cache_unavailable', 'detail': 'falling back to PostgreSQL'})
|
||||
|
||||
def close(self):
|
||||
if self._client is not None:
|
||||
try:
|
||||
self._client.close()
|
||||
except Exception:
|
||||
pass
|
||||
self._client = None
|
||||
self._available = False
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
return self._available and self._client is not None
|
||||
|
||||
def get(self, token_hash):
|
||||
"""Return cached user_id or None (miss or unavailable)."""
|
||||
if not self.available:
|
||||
return None
|
||||
try:
|
||||
value = self._client.get('session:%s' % token_hash)
|
||||
return value
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning('cache_read_failed', extra={
|
||||
'event': 'cache_read_failed', 'detail': 'falling back to PostgreSQL'})
|
||||
self._available = False
|
||||
return None
|
||||
|
||||
def set(self, token_hash, user_id, ttl_seconds):
|
||||
"""Cache a session with TTL. Failure is non-fatal."""
|
||||
if not self.available:
|
||||
return
|
||||
try:
|
||||
self._client.setex('session:%s' % token_hash, ttl_seconds, user_id)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning('cache_write_failed', extra={
|
||||
'event': 'cache_write_failed', 'detail': 'continuing without cache'})
|
||||
self._available = False
|
||||
|
||||
def delete(self, token_hash):
|
||||
"""Remove a session from the cache (logout). Failure is non-fatal."""
|
||||
if not self.available:
|
||||
return
|
||||
try:
|
||||
self._client.delete('session:%s' % token_hash)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning('cache_delete_failed', extra={
|
||||
'event': 'cache_delete_failed'})
|
||||
@@ -0,0 +1,149 @@
|
||||
"""Runtime configuration. Every value is read from the environment."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import secrets
|
||||
|
||||
# Contract limits: they cannot be raised by configuration.
|
||||
CONTRACT_MAX_TEXT_CODE_POINTS = 4000
|
||||
CONTRACT_MAX_PAGE_LIMIT = 100
|
||||
CONTRACT_SESSION_TTL_MIN = 1
|
||||
CONTRACT_SESSION_TTL_MAX = 86400
|
||||
|
||||
|
||||
class ConfigError(ValueError):
|
||||
"""Raised when an environment value cannot be used."""
|
||||
|
||||
|
||||
def _str(environ, name, default):
|
||||
value = environ.get(name)
|
||||
if value is None or value == '':
|
||||
return default
|
||||
return value
|
||||
|
||||
|
||||
def _int(environ, name, default, minimum, maximum):
|
||||
raw = environ.get(name)
|
||||
if raw is None or raw == '':
|
||||
return default
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError:
|
||||
raise ConfigError('%s must be an integer, got %r' % (name, raw)) from None
|
||||
if not minimum <= value <= maximum:
|
||||
raise ConfigError('%s must be in %d..%d, got %d' % (name, minimum, maximum, value))
|
||||
return value
|
||||
|
||||
|
||||
def _log_level(environ, name, default):
|
||||
value = _str(environ, name, default).upper()
|
||||
if value not in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
|
||||
raise ConfigError('%s must be one of DEBUG, INFO, WARNING, ERROR, CRITICAL' % name)
|
||||
return value
|
||||
|
||||
|
||||
def _secret_from_file(environ, name, default=None):
|
||||
"""Читает секрет из файла, путь к которому задаёт переменная окружения.
|
||||
|
||||
Секреты не лежат в переменных окружения напрямую: контейнер монтирует
|
||||
файл (например, Docker secret), а приложение читает его содержимое.
|
||||
Если файл не задан или не существует, используется значение переменной
|
||||
окружения без суффикса _FILE (удобно для локального запуска без секретов).
|
||||
"""
|
||||
path_name = name + '_FILE'
|
||||
path = environ.get(path_name)
|
||||
if path:
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as handle:
|
||||
return handle.read().strip()
|
||||
except OSError:
|
||||
pass
|
||||
return environ.get(name) or default
|
||||
|
||||
|
||||
class Config:
|
||||
"""Effective configuration of one process instance."""
|
||||
|
||||
def __init__(self, host, port, env, log_level, cursor_secret, shutdown_grace_seconds,
|
||||
max_text_code_points, max_display_name_code_points, max_title_code_points,
|
||||
default_page_limit, max_page_limit, max_body_bytes,
|
||||
database_url, cache_url, session_ttl_seconds, session_store,
|
||||
pool_min, pool_max, connect_timeout, statement_timeout):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.env = env
|
||||
self.log_level = log_level
|
||||
self.cursor_secret = cursor_secret
|
||||
self.shutdown_grace_seconds = shutdown_grace_seconds
|
||||
self.max_text_code_points = max_text_code_points
|
||||
self.max_display_name_code_points = max_display_name_code_points
|
||||
self.max_title_code_points = max_title_code_points
|
||||
self.default_page_limit = default_page_limit
|
||||
self.max_page_limit = max_page_limit
|
||||
self.max_body_bytes = max_body_bytes
|
||||
self.database_url = database_url
|
||||
self.cache_url = cache_url
|
||||
self.session_ttl_seconds = session_ttl_seconds
|
||||
self.session_store = session_store
|
||||
self.pool_min = pool_min
|
||||
self.pool_max = pool_max
|
||||
self.connect_timeout = connect_timeout
|
||||
self.statement_timeout = statement_timeout
|
||||
|
||||
@classmethod
|
||||
def from_env(cls, environ=None):
|
||||
environ = os.environ if environ is None else environ
|
||||
secret = _secret_from_file(environ, 'CURSOR_SECRET', '')
|
||||
if not secret:
|
||||
secret = secrets.token_hex(32)
|
||||
session_store = _str(environ, 'SESSION_STORE', 'postgres')
|
||||
if session_store not in ('postgres', 'redis'):
|
||||
raise ConfigError('SESSION_STORE должен быть postgres или redis, got %r' % session_store)
|
||||
return cls(
|
||||
host=_str(environ, 'APP_HOST', '0.0.0.0'),
|
||||
port=_int(environ, 'APP_PORT', 8080, 1, 65535),
|
||||
env=_str(environ, 'APP_ENV', 'lab'),
|
||||
log_level=_log_level(environ, 'LOG_LEVEL', 'INFO'),
|
||||
cursor_secret=secret.encode('utf-8'),
|
||||
shutdown_grace_seconds=_int(environ, 'SHUTDOWN_GRACE_SECONDS', 8, 1, 10),
|
||||
max_text_code_points=_int(environ, 'MAX_TEXT_CODE_POINTS', CONTRACT_MAX_TEXT_CODE_POINTS,
|
||||
1, CONTRACT_MAX_TEXT_CODE_POINTS),
|
||||
max_display_name_code_points=_int(environ, 'MAX_DISPLAY_NAME_CODE_POINTS', 100, 1, 100),
|
||||
max_title_code_points=_int(environ, 'MAX_TITLE_CODE_POINTS', 100, 1, 100),
|
||||
default_page_limit=_int(environ, 'DEFAULT_PAGE_LIMIT', 50, 1, CONTRACT_MAX_PAGE_LIMIT),
|
||||
max_page_limit=_int(environ, 'MAX_PAGE_LIMIT', CONTRACT_MAX_PAGE_LIMIT, 1,
|
||||
CONTRACT_MAX_PAGE_LIMIT),
|
||||
max_body_bytes=_int(environ, 'MAX_BODY_BYTES', 1048576, 1024, 16 * 1048576),
|
||||
database_url=_secret_from_file(environ, 'DATABASE_URL', None),
|
||||
cache_url=_secret_from_file(environ, 'CACHE_URL', None),
|
||||
session_ttl_seconds=_int(environ, 'SESSION_TTL_SECONDS', 3600,
|
||||
CONTRACT_SESSION_TTL_MIN, CONTRACT_SESSION_TTL_MAX),
|
||||
session_store=session_store,
|
||||
pool_min=_int(environ, 'DB_POOL_MIN', 2, 1, 50),
|
||||
pool_max=_int(environ, 'DB_POOL_MAX', 10, 1, 100),
|
||||
connect_timeout=_int(environ, 'DB_CONNECT_TIMEOUT', 5, 1, 60),
|
||||
statement_timeout=_int(environ, 'DB_STATEMENT_TIMEOUT', 10, 1, 300),
|
||||
)
|
||||
|
||||
def as_log_fields(self):
|
||||
"""Configuration summary safe for logs (no secrets)."""
|
||||
return {
|
||||
'app_env': self.env,
|
||||
'host': self.host,
|
||||
'port': self.port,
|
||||
'log_level': self.log_level,
|
||||
'shutdown_grace_seconds': self.shutdown_grace_seconds,
|
||||
'default_page_limit': self.default_page_limit,
|
||||
'max_page_limit': self.max_page_limit,
|
||||
'max_text_code_points': self.max_text_code_points,
|
||||
'max_body_bytes': self.max_body_bytes,
|
||||
'cursor_secret_configured': bool(os.environ.get('CURSOR_SECRET')),
|
||||
'database_url_configured': bool(self.database_url),
|
||||
'cache_url_configured': bool(self.cache_url),
|
||||
'session_ttl_seconds': self.session_ttl_seconds,
|
||||
'session_store': self.session_store,
|
||||
'db_pool_min': self.pool_min,
|
||||
'db_pool_max': self.pool_max,
|
||||
'db_connect_timeout': self.connect_timeout,
|
||||
'db_statement_timeout': self.statement_timeout,
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Opaque keyset cursors: HMAC-protected (created_at, id) plus a resource scope."""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import uuid
|
||||
|
||||
VERSION = 1
|
||||
MAC_BYTES = 16
|
||||
|
||||
|
||||
class InvalidCursor(ValueError):
|
||||
"""Cursor is malformed, forged, expired or belongs to another resource."""
|
||||
|
||||
|
||||
def _key(secret):
|
||||
return hashlib.sha256(b'mtusi-messenger-cursor-v1' + secret).digest()
|
||||
|
||||
|
||||
def encode(scope, created_at, object_id, secret):
|
||||
"""Serialize a keyset position bound to one resource scope."""
|
||||
payload = json.dumps(
|
||||
{'v': VERSION, 'scope': scope, 'created_at': created_at, 'id': str(object_id)},
|
||||
separators=(',', ':'), sort_keys=True).encode('utf-8')
|
||||
mac = hmac.new(_key(secret), payload, hashlib.sha256).digest()[:MAC_BYTES]
|
||||
return base64.urlsafe_b64encode(payload + mac).decode('ascii').rstrip('=')
|
||||
|
||||
|
||||
def decode(token, scope, secret):
|
||||
"""Return (created_at datetime, uuid) or raise InvalidCursor."""
|
||||
if not isinstance(token, str) or not token:
|
||||
raise InvalidCursor('empty cursor')
|
||||
padding = '=' * (-len(token) % 4)
|
||||
try:
|
||||
raw = base64.urlsafe_b64decode(token + padding)
|
||||
except (binascii.Error, ValueError):
|
||||
raise InvalidCursor('cursor is not base64url') from None
|
||||
if len(raw) <= MAC_BYTES:
|
||||
raise InvalidCursor('cursor is too short')
|
||||
payload, mac = raw[:-MAC_BYTES], raw[-MAC_BYTES:]
|
||||
if not hmac.compare_digest(mac, hmac.new(_key(secret), payload, hashlib.sha256).digest()[:MAC_BYTES]):
|
||||
raise InvalidCursor('cursor signature mismatch')
|
||||
try:
|
||||
data = json.loads(payload.decode('utf-8'))
|
||||
except (ValueError, UnicodeError):
|
||||
raise InvalidCursor('cursor payload is not JSON') from None
|
||||
if not isinstance(data, dict) or data.get('v') != VERSION:
|
||||
raise InvalidCursor('unsupported cursor version')
|
||||
if data.get('scope') != scope:
|
||||
raise InvalidCursor('cursor belongs to another resource')
|
||||
created_at = data.get('created_at')
|
||||
object_id = data.get('id')
|
||||
if not isinstance(created_at, str) or not isinstance(object_id, str):
|
||||
raise InvalidCursor('cursor payload types')
|
||||
try:
|
||||
stamp = dt.datetime.fromisoformat(created_at.replace('Z', '+00:00'))
|
||||
identifier = uuid.UUID(object_id)
|
||||
except ValueError:
|
||||
raise InvalidCursor('cursor payload values') from None
|
||||
if stamp.tzinfo is None:
|
||||
raise InvalidCursor('cursor timestamp has no timezone')
|
||||
return stamp.astimezone(dt.timezone.utc), identifier
|
||||
@@ -0,0 +1,97 @@
|
||||
"""PostgreSQL connection pool and schema migrations."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from psycopg2 import pool, OperationalError, InterfaceError
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
from .config import Config
|
||||
from .errors import Unavailable
|
||||
|
||||
_ROOT = Path(__file__).resolve().parents[2]
|
||||
_MIGRATIONS_DIR = _ROOT / 'migrations'
|
||||
|
||||
|
||||
class Database:
|
||||
"""Thread-safe connection pool with prepared statement timeouts.
|
||||
|
||||
All queries are parameterised (%s). Connections are returned to the pool
|
||||
after every operation; the pool itself is closed on shutdown.
|
||||
"""
|
||||
|
||||
def __init__(self, config: Config):
|
||||
self._config = config
|
||||
self._pool = None
|
||||
|
||||
def open(self):
|
||||
if self._pool is not None:
|
||||
return
|
||||
try:
|
||||
self._pool = pool.ThreadedConnectionPool(
|
||||
minconn=self._config.pool_min,
|
||||
maxconn=self._config.pool_max,
|
||||
dsn=self._config.database_url,
|
||||
connect_timeout=self._config.connect_timeout,
|
||||
cursor_factory=RealDictCursor,
|
||||
)
|
||||
except Exception as exc: # psycopg2.Error or configuration error
|
||||
raise Unavailable('Не удалось подключиться к PostgreSQL: %s' % exc) from None
|
||||
|
||||
def close(self):
|
||||
if self._pool is not None:
|
||||
self._pool.closeall()
|
||||
self._pool = None
|
||||
|
||||
@contextmanager
|
||||
def connection(self):
|
||||
"""Yield a connection; rollback on exception, return to pool on exit."""
|
||||
if self._pool is None:
|
||||
raise Unavailable('База данных не инициализирована')
|
||||
try:
|
||||
conn = self._pool.getconn()
|
||||
except (pool.PoolError, OperationalError, InterfaceError):
|
||||
raise Unavailable('Пул соединений PostgreSQL недоступен') from None
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SET statement_timeout = %s',
|
||||
(self._config.statement_timeout * 1000,))
|
||||
yield conn
|
||||
except Exception as exc:
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
pass
|
||||
if isinstance(exc, (OperationalError, InterfaceError)):
|
||||
raise Unavailable('PostgreSQL недоступен') from None
|
||||
raise
|
||||
finally:
|
||||
self._pool.putconn(conn, close=bool(conn.closed))
|
||||
|
||||
@contextmanager
|
||||
def cursor(self):
|
||||
"""Yield a cursor inside a connection; commit on success, rollback on failure."""
|
||||
with self.connection() as conn:
|
||||
with conn.cursor() as cur:
|
||||
yield cur
|
||||
conn.commit()
|
||||
|
||||
def ping(self):
|
||||
with self.cursor() as cur:
|
||||
cur.execute('SELECT 1')
|
||||
cur.fetchone()
|
||||
|
||||
def run_migrations(self):
|
||||
"""Apply versioned migrations once, under a transaction advisory lock."""
|
||||
with self.cursor() as cur:
|
||||
cur.execute('SELECT pg_advisory_xact_lock(3072026)')
|
||||
cur.execute('CREATE TABLE IF NOT EXISTS schema_migrations '
|
||||
'(name text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())')
|
||||
for path in sorted(_MIGRATIONS_DIR.glob('*.sql')):
|
||||
cur.execute('SELECT 1 FROM schema_migrations WHERE name = %s', (path.name,))
|
||||
if cur.fetchone() is not None:
|
||||
continue
|
||||
cur.execute(path.read_text(encoding='utf-8'))
|
||||
cur.execute('INSERT INTO schema_migrations(name) VALUES (%s)', (path.name,))
|
||||
@@ -0,0 +1,73 @@
|
||||
"""API error taxonomy; every message is safe to return to a client."""
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
"""Base class for errors that map onto the public error payload."""
|
||||
|
||||
status = 500
|
||||
code = 'internal_error'
|
||||
default_message = 'Внутренняя ошибка сервера'
|
||||
|
||||
def __init__(self, message=None):
|
||||
self.message = message or self.default_message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class InvalidRequest(ApiError):
|
||||
status = 400
|
||||
code = 'invalid_request'
|
||||
default_message = 'Некорректный запрос'
|
||||
|
||||
|
||||
class Unauthorized(ApiError):
|
||||
status = 401
|
||||
code = 'unauthorized'
|
||||
default_message = 'Требуется идентификация пользователя'
|
||||
www_authenticate = None
|
||||
|
||||
def __init__(self, message=None, www_authenticate=None):
|
||||
self.message = message or self.default_message
|
||||
self.www_authenticate = www_authenticate
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class NotFound(ApiError):
|
||||
status = 404
|
||||
code = 'not_found'
|
||||
default_message = 'Объект не найден'
|
||||
|
||||
|
||||
class Conflict(ApiError):
|
||||
status = 409
|
||||
code = 'conflict'
|
||||
default_message = 'Конфликт состояния'
|
||||
|
||||
|
||||
class PayloadTooLarge(ApiError):
|
||||
status = 413
|
||||
code = 'payload_too_large'
|
||||
default_message = 'Тело запроса слишком велико'
|
||||
|
||||
|
||||
class UnsupportedMediaType(ApiError):
|
||||
status = 415
|
||||
code = 'unsupported_media_type'
|
||||
default_message = 'Поддерживается только application/json'
|
||||
|
||||
|
||||
class RateLimited(ApiError):
|
||||
status = 429
|
||||
code = 'rate_limited'
|
||||
default_message = 'Слишком много запросов'
|
||||
|
||||
|
||||
class Unavailable(ApiError):
|
||||
status = 503
|
||||
code = 'unavailable'
|
||||
default_message = 'Сервис временно недоступен'
|
||||
|
||||
|
||||
class InternalError(ApiError):
|
||||
status = 500
|
||||
code = 'internal_error'
|
||||
default_message = 'Внутренняя ошибка сервера'
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Structured stdout logging without payload contents."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
|
||||
_FIELDS = ('event', 'request_id', 'method', 'route', 'status', 'duration_ms', 'actor_id',
|
||||
'error_code', 'client', 'outcome', 'count', 'detail', 'signal', 'version')
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
"""One JSON object per line; message text of API payloads is never logged."""
|
||||
|
||||
def format(self, record):
|
||||
payload = {
|
||||
'ts': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime(record.created)) +
|
||||
'.%03dZ' % int(record.msecs),
|
||||
'level': record.levelname.lower(),
|
||||
'logger': record.name,
|
||||
'message': record.getMessage(),
|
||||
}
|
||||
for field in _FIELDS:
|
||||
value = getattr(record, field, None)
|
||||
if value is not None:
|
||||
payload[field] = value
|
||||
if record.exc_info and record.levelno >= logging.ERROR:
|
||||
payload['exception'] = record.exc_info[0].__name__
|
||||
return json.dumps(payload, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def configure(level_name):
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(JsonFormatter())
|
||||
root = logging.getLogger()
|
||||
for existing in list(root.handlers):
|
||||
root.removeHandler(existing)
|
||||
root.addHandler(handler)
|
||||
root.setLevel(getattr(logging, level_name))
|
||||
return logging.getLogger('messenger')
|
||||
|
||||
|
||||
def access_logger():
|
||||
return logging.getLogger('messenger.access')
|
||||
@@ -0,0 +1,17 @@
|
||||
"""One-shot schema migration entry point; API processes never migrate."""
|
||||
from .config import Config
|
||||
from .db import Database
|
||||
|
||||
|
||||
def main():
|
||||
database = Database(Config.from_env())
|
||||
try:
|
||||
database.open()
|
||||
database.run_migrations()
|
||||
print('PASS: migrations applied')
|
||||
finally:
|
||||
database.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Password hashing and session tokens.
|
||||
|
||||
Passwords are hashed with a library password KDF: scrypt from the standard
|
||||
library when available, otherwise bcrypt with a SHA-256 pre-hash so that
|
||||
passwords longer than bcrypt's 72-byte limit are still supported.
|
||||
Session tokens are opaque random values (>= 256 bits of entropy); only their
|
||||
SHA-256 is stored in the database.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import secrets
|
||||
|
||||
import bcrypt
|
||||
|
||||
try: # scrypt is part of hashlib on OpenSSL-enabled builds
|
||||
hashlib.scrypt(b'x', salt=b's', n=16, r=8, p=1, dklen=16)
|
||||
_HAS_SCRYPT = True
|
||||
except Exception:
|
||||
_HAS_SCRYPT = False
|
||||
|
||||
if not _HAS_SCRYPT:
|
||||
import bcrypt # type: ignore
|
||||
|
||||
SCRYPT_N = 16384
|
||||
SCRYPT_R = 8
|
||||
SCRYPT_P = 1
|
||||
SCRYPT_DKLEN = 32
|
||||
SALT_BYTES = 16
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
"""Hash a UTF-8 password with a library password KDF.
|
||||
|
||||
Returns a single storable string:
|
||||
scrypt:$n:$r:$p:$salt_b64:$hash_b64
|
||||
or
|
||||
bcrypt:$salt_b64:$hash_b64
|
||||
"""
|
||||
raw = password.encode('utf-8')
|
||||
if _HAS_SCRYPT:
|
||||
salt = secrets.token_bytes(SALT_BYTES)
|
||||
dk = hashlib.scrypt(
|
||||
raw,
|
||||
salt=salt,
|
||||
n=SCRYPT_N,
|
||||
r=SCRYPT_R,
|
||||
p=SCRYPT_P,
|
||||
dklen=SCRYPT_DKLEN,
|
||||
)
|
||||
return 'scrypt:%d:%d:%d:%s:%s' % (
|
||||
SCRYPT_N, SCRYPT_R, SCRYPT_P,
|
||||
base64.b64encode(salt).decode('ascii'),
|
||||
base64.b64encode(dk).decode('ascii'),
|
||||
)
|
||||
# bcrypt limits input to 72 bytes; pre-hash with SHA-256 so long passwords
|
||||
# (up to 128 code points per contract) are fully covered.
|
||||
digest = hashlib.sha256(raw).digest()
|
||||
hashed = bcrypt.hashpw(digest, bcrypt.gensalt(rounds=12))
|
||||
return 'bcrypt:%s' % hashed.decode('ascii')
|
||||
|
||||
|
||||
def verify_password(password: str, password_hash: str) -> bool:
|
||||
"""Verify a password against a KDF hash string. Constant-time compare."""
|
||||
try:
|
||||
raw = password.encode('utf-8')
|
||||
if password_hash.startswith('scrypt:'):
|
||||
parts = password_hash.split(':')
|
||||
if len(parts) != 6 or parts[0] != 'scrypt':
|
||||
return False
|
||||
_, n, r, p, salt_b64, hash_b64 = parts
|
||||
salt = base64.b64decode(salt_b64)
|
||||
expected = base64.b64decode(hash_b64)
|
||||
dk = hashlib.scrypt(
|
||||
raw,
|
||||
salt=salt,
|
||||
n=int(n), r=int(r), p=int(p),
|
||||
dklen=len(expected),
|
||||
)
|
||||
return hmac.compare_digest(dk, expected)
|
||||
if password_hash.startswith('bcrypt:'):
|
||||
stored = password_hash[len('bcrypt:'):].encode('ascii')
|
||||
digest = hashlib.sha256(raw).digest()
|
||||
return bcrypt.checkpw(digest, stored)
|
||||
return False
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def generate_token() -> str:
|
||||
"""Opaque random bearer token, at least 256 bits of entropy."""
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def hash_token(token: str) -> str:
|
||||
"""SHA-256 of a bearer token, stored in the database instead of the token itself."""
|
||||
return hashlib.sha256(token.encode('utf-8')).hexdigest()
|
||||
@@ -0,0 +1,263 @@
|
||||
"""HTTP/1.1 front end: request ids, structured logs, graceful SIGTERM shutdown."""
|
||||
from __future__ import annotations
|
||||
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
from . import __version__, logging_utils
|
||||
from .api import Api, Request
|
||||
from .cache import SessionCache
|
||||
from .config import Config, ConfigError
|
||||
from .db import Database
|
||||
from .errors import ApiError, InvalidRequest, PayloadTooLarge, Unavailable
|
||||
from .store import Store
|
||||
|
||||
MAX_REQUEST_ID_LENGTH = 128
|
||||
MAX_LOGGED_LINE = 200
|
||||
|
||||
|
||||
def sanitize_request_id(raw):
|
||||
"""Echo a safe client id, otherwise generate one. Never trust raw input."""
|
||||
if raw and len(raw) <= MAX_REQUEST_ID_LENGTH and all(0x21 <= ord(ch) <= 0x7E for ch in raw):
|
||||
return raw
|
||||
return uuid.uuid4().hex
|
||||
|
||||
|
||||
def route_template(path):
|
||||
for _method, pattern, _handler in Api.ROUTES:
|
||||
if pattern.match(path):
|
||||
return pattern.pattern.replace('[^/]+', '{chat_id}').strip('^$')
|
||||
return 'unmatched'
|
||||
|
||||
|
||||
def encode_payload(payload):
|
||||
return json.dumps(payload, ensure_ascii=False, separators=(',', ':')).encode('utf-8')
|
||||
|
||||
|
||||
class ApiServer(ThreadingHTTPServer):
|
||||
"""Threaded server that tracks in-flight requests and stops accepting on demand."""
|
||||
|
||||
daemon_threads = True
|
||||
allow_reuse_address = True
|
||||
request_queue_size = 128
|
||||
|
||||
def __init__(self, address, api, config, logger, database):
|
||||
self.api = api
|
||||
self.config = config
|
||||
self.logger = logger
|
||||
self.database = database
|
||||
self.instance_id = os.environ.get("HOSTNAME") or uuid.uuid4().hex
|
||||
self.shutting_down = False
|
||||
self._active = 0
|
||||
self._idle = threading.Condition()
|
||||
super().__init__(address, ApiHandler)
|
||||
|
||||
def enter(self):
|
||||
with self._idle:
|
||||
self._active += 1
|
||||
|
||||
def leave(self):
|
||||
with self._idle:
|
||||
self._active -= 1
|
||||
if self._active == 0:
|
||||
self._idle.notify_all()
|
||||
|
||||
def wait_idle(self, timeout):
|
||||
deadline = time.monotonic() + timeout
|
||||
with self._idle:
|
||||
while self._active > 0:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
return False
|
||||
self._idle.wait(remaining)
|
||||
return True
|
||||
|
||||
|
||||
class ApiHandler(BaseHTTPRequestHandler):
|
||||
protocol_version = 'HTTP/1.1'
|
||||
server_version = 'mtusi-messenger/' + __version__
|
||||
sys_version = ''
|
||||
timeout = 30
|
||||
|
||||
# --------------------------------------------------------- entry points
|
||||
def do_GET(self):
|
||||
self._handle()
|
||||
|
||||
do_HEAD = do_POST = do_PUT = do_PATCH = do_DELETE = do_OPTIONS = do_GET
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
"""Base-class chatter stays debug-only; access logs are structured."""
|
||||
self.server.logger.debug('http_note', extra={
|
||||
'event': 'http_note', 'detail': (fmt % args)[:MAX_LOGGED_LINE]})
|
||||
|
||||
# ------------------------------------------------------------- pipeline
|
||||
def _handle(self):
|
||||
started = time.monotonic()
|
||||
server = self.server
|
||||
request_id = sanitize_request_id(self.headers.get('X-Request-Id'))
|
||||
head_only = self.command == 'HEAD'
|
||||
path = urlsplit(self.path).path
|
||||
status, error_code = 500, None
|
||||
self._last_exc = None
|
||||
server.enter()
|
||||
try:
|
||||
if server.shutting_down:
|
||||
raise Unavailable('Сервер завершает работу')
|
||||
body = self._read_body()
|
||||
query = parse_qs(urlsplit(self.path).query, keep_blank_values=True)
|
||||
request = Request('GET' if head_only else self.command, path, query, self.headers,
|
||||
body, request_id, self.client_address[0])
|
||||
status, payload = server.api.dispatch(request)
|
||||
encoded = encode_payload(payload)
|
||||
except ApiError as exc:
|
||||
self._last_exc = exc
|
||||
status, error_code = exc.status, exc.code
|
||||
encoded = self._error_payload(exc, request_id)
|
||||
except Exception: # last resort: log privately, answer generically
|
||||
server.logger.exception('unhandled_error', extra={
|
||||
'event': 'unhandled_error', 'request_id': request_id,
|
||||
'method': self.command, 'route': route_template(path)})
|
||||
status, error_code = 500, 'internal_error'
|
||||
encoded = self._error_payload(ApiError(), request_id)
|
||||
finally:
|
||||
server.logger.info('http_request', extra={
|
||||
'event': 'http_request', 'request_id': request_id, 'method': self.command,
|
||||
'route': route_template(path), 'status': status,
|
||||
'duration_ms': round(1000 * (time.monotonic() - started), 3),
|
||||
'error_code': error_code, 'client': self.client_address[0]})
|
||||
try:
|
||||
self._send(status, encoded, request_id, head_only)
|
||||
finally:
|
||||
server.leave()
|
||||
|
||||
def _read_body(self):
|
||||
if self.headers.get('Transfer-Encoding'):
|
||||
self.close_connection = True
|
||||
raise InvalidRequest('Transfer-Encoding не поддерживается')
|
||||
raw_length = self.headers.get('Content-Length')
|
||||
if raw_length is None:
|
||||
return b''
|
||||
try:
|
||||
length = int(raw_length.strip())
|
||||
except ValueError:
|
||||
raise InvalidRequest('Некорректный Content-Length') from None
|
||||
if length < 0:
|
||||
raise InvalidRequest('Некорректный Content-Length')
|
||||
if length > self.server.config.max_body_bytes:
|
||||
self.close_connection = True
|
||||
raise PayloadTooLarge('Тело запроса больше %d байт' % self.server.config.max_body_bytes)
|
||||
if length == 0:
|
||||
return b''
|
||||
return self.rfile.read(length)
|
||||
|
||||
@staticmethod
|
||||
def _error_payload(exc, request_id):
|
||||
return encode_payload({'error': {
|
||||
'code': exc.code, 'message': exc.message, 'request_id': request_id}})
|
||||
|
||||
def _send(self, status, encoded, request_id, head_only):
|
||||
try:
|
||||
self.send_response(status)
|
||||
self.send_header('Content-Type', 'application/json; charset=utf-8')
|
||||
self.send_header('X-Request-Id', request_id)
|
||||
self.send_header('X-Instance-Id', self.server.instance_id)
|
||||
self.send_header('Cache-Control', 'no-store')
|
||||
if status == 204:
|
||||
self.send_header('Content-Length', '0')
|
||||
if status == 503:
|
||||
self.send_header('Retry-After', '1')
|
||||
self.end_headers()
|
||||
return
|
||||
self.send_header('Content-Length', str(len(encoded)))
|
||||
if status == 503:
|
||||
self.send_header('Retry-After', '1')
|
||||
if status == 401 and self._last_exc is not None:
|
||||
www_auth = getattr(self._last_exc, 'www_authenticate', None)
|
||||
if www_auth:
|
||||
self.send_header('WWW-Authenticate', www_auth)
|
||||
if self.close_connection:
|
||||
self.send_header('Connection', 'close')
|
||||
self.end_headers()
|
||||
if not head_only and encoded:
|
||||
self.wfile.write(encoded)
|
||||
except (BrokenPipeError, ConnectionResetError):
|
||||
self.close_connection = True
|
||||
|
||||
|
||||
def build_server(config, logger, api, database):
|
||||
"""Create the listening server; raises OSError when the port is taken."""
|
||||
return ApiServer((config.host, config.port), api, config, logger, database)
|
||||
|
||||
|
||||
def run(config, logger, database, cache):
|
||||
"""Serve until SIGTERM/SIGINT, then drain in-flight work within the grace period."""
|
||||
api = Api(config, Store(config, database, cache), database)
|
||||
try:
|
||||
server = build_server(config, logger, api, database)
|
||||
except OSError as exc:
|
||||
logger.error('listen_failed', extra={'event': 'listen_failed', 'detail': str(exc)})
|
||||
return 1
|
||||
stop = threading.Event()
|
||||
|
||||
def handle_signal(signum, _frame):
|
||||
if stop.is_set():
|
||||
return
|
||||
stop.set()
|
||||
server.shutting_down = True
|
||||
logger.warning('shutdown_signal', extra={
|
||||
'event': 'shutdown_signal', 'signal': signum,
|
||||
'detail': 'stop accepting new requests'})
|
||||
threading.Thread(target=server.shutdown, name='shutdown', daemon=True).start()
|
||||
|
||||
for name in ('SIGTERM', 'SIGINT'):
|
||||
if hasattr(signal, name):
|
||||
signal.signal(getattr(signal, name), handle_signal)
|
||||
|
||||
logger.info('server_started', extra=dict(
|
||||
{'event': 'server_started', 'version': __version__,
|
||||
'detail': 'pid %d, python %s' % (os.getpid(), platform.python_version())},
|
||||
**config.as_log_fields()))
|
||||
started = time.monotonic()
|
||||
try:
|
||||
server.serve_forever(poll_interval=0.2)
|
||||
finally:
|
||||
server.server_close()
|
||||
drained = server.wait_idle(config.shutdown_grace_seconds)
|
||||
database.close()
|
||||
logger.info('server_stopped', extra={
|
||||
'event': 'server_stopped',
|
||||
'outcome': 'graceful' if drained else 'grace_period_exceeded',
|
||||
'duration_ms': round(1000 * (time.monotonic() - started), 3)})
|
||||
return 0
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
try:
|
||||
config = Config.from_env()
|
||||
except ConfigError as exc:
|
||||
print('configuration error: %s' % exc, file=sys.stderr)
|
||||
return 2
|
||||
logger = logging_utils.configure(config.log_level)
|
||||
database = Database(config)
|
||||
cache = SessionCache(config)
|
||||
try:
|
||||
database.open()
|
||||
cache.open()
|
||||
except Exception as exc: # noqa: BLE001 - startup must fail loud and clear
|
||||
logger.error('database_start_failed', extra={
|
||||
'event': 'database_start_failed', 'detail': str(exc)})
|
||||
database.close()
|
||||
cache.close()
|
||||
return 1
|
||||
try:
|
||||
return run(config, logger, database, cache)
|
||||
finally:
|
||||
cache.close()
|
||||
@@ -0,0 +1,297 @@
|
||||
"""PostgreSQL-backed state store. All writes go through parameterised queries
|
||||
inside explicit transactions; reads use keyset pagination over indexed columns."""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
from psycopg2.extras import Json
|
||||
from psycopg2.errors import UniqueViolation, ForeignKeyViolation
|
||||
import uuid
|
||||
|
||||
from . import cursor as cursor_codec
|
||||
from . import security
|
||||
from .cache import SessionCache
|
||||
from .config import Config
|
||||
from .db import Database
|
||||
from .errors import Conflict, InvalidRequest, NotFound
|
||||
|
||||
|
||||
def utc_now():
|
||||
return dt.datetime.now(dt.timezone.utc)
|
||||
|
||||
|
||||
def rfc3339(stamp):
|
||||
"""RFC 3339 UTC with fixed microsecond precision and the Z suffix."""
|
||||
return stamp.astimezone(dt.timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
|
||||
|
||||
|
||||
class Store:
|
||||
"""PostgreSQL state: users, chats, membership, messages, sessions."""
|
||||
|
||||
def __init__(self, config: Config, database: Database, cache: SessionCache):
|
||||
self._config = config
|
||||
self._db = database
|
||||
self._cache = cache
|
||||
|
||||
# ---------------------------------------------------------------- reads
|
||||
def stats(self):
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute('SELECT (SELECT count(*) FROM users) AS users,'
|
||||
' (SELECT count(*) FROM chats) AS chats,'
|
||||
' (SELECT count(*) FROM messages) AS messages')
|
||||
row = cur.fetchone()
|
||||
return {
|
||||
'users': row['users'],
|
||||
'chats': row['chats'],
|
||||
'messages': row['messages'],
|
||||
}
|
||||
|
||||
def user(self, user_id):
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT id, username, display_name, created_at FROM users WHERE id = %s',
|
||||
(str(user_id),))
|
||||
row = cur.fetchone()
|
||||
return self._public_user(row) if row else None
|
||||
|
||||
def user_by_username(self, username):
|
||||
"""Return user row including password_hash, or None."""
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT id, username, password_hash, display_name, created_at FROM users '
|
||||
'WHERE username = %s', (username,))
|
||||
return cur.fetchone()
|
||||
|
||||
def users_exist(self, identifiers):
|
||||
if not identifiers:
|
||||
return True
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT count(*) AS c FROM users WHERE id = ANY(%s::uuid[])',
|
||||
([str(i) for i in identifiers],))
|
||||
row = cur.fetchone()
|
||||
return row['c'] == len(identifiers)
|
||||
|
||||
# --------------------------------------------------------------- writes
|
||||
def create_user(self, username, display_name, password_hash):
|
||||
with self._db.cursor() as cur:
|
||||
try:
|
||||
cur.execute(
|
||||
'INSERT INTO users (id, username, password_hash, display_name, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s) '
|
||||
'RETURNING id, username, display_name, created_at',
|
||||
(str(uuid.uuid4()), username, password_hash, display_name, utc_now()))
|
||||
except UniqueViolation:
|
||||
# Unique violation on username -> conflict, rolled back by cursor context.
|
||||
raise Conflict('username уже занят') from None
|
||||
row = cur.fetchone()
|
||||
return self._public_user(row)
|
||||
|
||||
def create_chat(self, title, member_ids):
|
||||
"""Atomically create a chat and its membership. Rollback on any failure."""
|
||||
identifier = str(uuid.uuid4())
|
||||
stamp = utc_now()
|
||||
with self._db.cursor() as cur:
|
||||
try:
|
||||
cur.execute(
|
||||
'INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)',
|
||||
(identifier, title, stamp))
|
||||
for member_id in member_ids:
|
||||
cur.execute(
|
||||
'INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)',
|
||||
(identifier, str(member_id)))
|
||||
except ForeignKeyViolation:
|
||||
raise InvalidRequest('member_ids содержит неизвестных пользователей') from None
|
||||
return {
|
||||
'id': identifier,
|
||||
'title': title,
|
||||
'member_ids': list(member_ids),
|
||||
'created_at': rfc3339(stamp),
|
||||
}
|
||||
|
||||
def add_message(self, chat_id, sender_id, text, key):
|
||||
"""One transaction owns both the message and its replayable result.
|
||||
|
||||
The advisory lock serializes only the same user/method/path/key across
|
||||
processes. Hash collisions serialize extra requests, never alias keys:
|
||||
the complete key is still checked against the primary key in the table.
|
||||
A crash before COMMIT rolls back both records. A lost reply after COMMIT
|
||||
is recovered by replay. Transient failures are never recorded.
|
||||
"""
|
||||
path = '/api/v1/chats/%s/messages' % chat_id
|
||||
body_hash = hashlib.sha256(json.dumps({'text': text}, ensure_ascii=False,
|
||||
sort_keys=True).encode('utf-8')).hexdigest()
|
||||
scope = json.dumps([str(sender_id), 'POST', path, key], separators=(',', ':'))
|
||||
lock_id = int.from_bytes(hashlib.sha256(scope.encode()).digest()[:8], 'big', signed=True)
|
||||
identifier = str(uuid.uuid4())
|
||||
stamp = utc_now()
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute('SELECT pg_advisory_xact_lock(%s)', (lock_id,))
|
||||
cur.execute('SELECT request_hash, response_body FROM message_idempotency '
|
||||
'WHERE user_id=%s AND method=%s AND path=%s AND key=%s',
|
||||
(str(sender_id), 'POST', path, key))
|
||||
replay = cur.fetchone()
|
||||
if replay is not None:
|
||||
if replay['request_hash'] != body_hash:
|
||||
raise Conflict('Idempotency-Key уже использован с другим телом запроса')
|
||||
return replay['response_body']
|
||||
cur.execute(
|
||||
'INSERT INTO messages (id, chat_id, sender_id, text, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s) '
|
||||
'RETURNING id, chat_id, sender_id, text, created_at',
|
||||
(identifier, str(chat_id), str(sender_id), text, stamp))
|
||||
result = self._public_message(cur.fetchone())
|
||||
cur.execute('INSERT INTO message_idempotency '
|
||||
'(user_id, method, path, key, request_hash, response_status, response_body) '
|
||||
'VALUES (%s,%s,%s,%s,%s,201,%s)',
|
||||
(str(sender_id), 'POST', path, key, body_hash, Json(result)))
|
||||
return result
|
||||
|
||||
# --------------------------------------------------------------- pages
|
||||
def page_messages(self, chat_id, after_key, limit):
|
||||
"""Return (public messages, has_more) for a keyset window."""
|
||||
with self._db.cursor() as cur:
|
||||
if after_key is None:
|
||||
cur.execute(
|
||||
'SELECT id, chat_id, sender_id, text, created_at FROM messages '
|
||||
'WHERE chat_id = %s '
|
||||
'ORDER BY created_at, id '
|
||||
'LIMIT %s', (str(chat_id), limit + 1))
|
||||
else:
|
||||
stamp, identifier = after_key
|
||||
cur.execute(
|
||||
'SELECT id, chat_id, sender_id, text, created_at FROM messages '
|
||||
'WHERE chat_id = %s '
|
||||
'AND (created_at, id) > (%s, %s) '
|
||||
'ORDER BY created_at, id '
|
||||
'LIMIT %s',
|
||||
(str(chat_id), stamp, str(identifier), limit + 1))
|
||||
rows = cur.fetchall()
|
||||
has_more = len(rows) > limit
|
||||
items = [self._public_message(dict(row)) for row in rows[:limit]]
|
||||
return items, has_more
|
||||
|
||||
def page_chats(self, user_id, after_key, limit):
|
||||
"""Return (chats visible to the user, has_more) ordered by (created_at, id)."""
|
||||
with self._db.cursor() as cur:
|
||||
if after_key is None:
|
||||
cur.execute(
|
||||
'SELECT c.id, c.title, c.created_at FROM chats c '
|
||||
'JOIN chat_members cm ON cm.chat_id = c.id '
|
||||
'WHERE cm.user_id = %s '
|
||||
'ORDER BY c.created_at, c.id '
|
||||
'LIMIT %s', (str(user_id), limit + 1))
|
||||
else:
|
||||
stamp, identifier = after_key
|
||||
cur.execute(
|
||||
'SELECT c.id, c.title, c.created_at FROM chats c '
|
||||
'JOIN chat_members cm ON cm.chat_id = c.id '
|
||||
'WHERE cm.user_id = %s '
|
||||
'AND (c.created_at, c.id) > (%s, %s) '
|
||||
'ORDER BY c.created_at, c.id '
|
||||
'LIMIT %s',
|
||||
(str(user_id), stamp, str(identifier), limit + 1))
|
||||
rows = cur.fetchall()
|
||||
has_more = len(rows) > limit
|
||||
items = [self._public_chat(dict(row)) for row in rows[:limit]]
|
||||
return items, has_more
|
||||
|
||||
# ----------------------------------------------------------- projection
|
||||
def chat(self, chat_id):
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT id, title, created_at FROM chats WHERE id = %s', (str(chat_id),))
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
chat = self._public_chat(dict(row))
|
||||
cur.execute(
|
||||
'SELECT user_id FROM chat_members WHERE chat_id = %s', (str(chat_id),))
|
||||
chat['member_ids'] = [str(r['user_id']) for r in cur.fetchall()]
|
||||
return chat
|
||||
|
||||
@staticmethod
|
||||
def is_member(chat, user_id):
|
||||
return str(user_id) in chat['member_ids']
|
||||
|
||||
@staticmethod
|
||||
def _public_user(row):
|
||||
return {
|
||||
'id': str(row['id']),
|
||||
'username': row['username'],
|
||||
'display_name': row['display_name'],
|
||||
'created_at': rfc3339(row['created_at']),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _public_chat(row):
|
||||
return {
|
||||
'id': str(row['id']),
|
||||
'title': row['title'],
|
||||
'member_ids': [], # filled by API when needed
|
||||
'created_at': rfc3339(row['created_at']),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _public_message(row):
|
||||
return {
|
||||
'id': str(row['id']),
|
||||
'chat_id': str(row['chat_id']),
|
||||
'sender_id': str(row['sender_id']),
|
||||
'text': row['text'],
|
||||
'created_at': rfc3339(row['created_at']),
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------- sessions
|
||||
def create_session(self, token_hash, user_id, expires_at):
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'INSERT INTO sessions (token_hash, user_id, expires_at, created_at) '
|
||||
'VALUES (%s, %s, %s, %s)',
|
||||
(token_hash, str(user_id), expires_at, utc_now()))
|
||||
ttl = max(1, int((expires_at - utc_now()).total_seconds()))
|
||||
self._cache.set(token_hash, str(user_id), ttl)
|
||||
|
||||
def get_session(self, token_hash):
|
||||
"""Return user_id for a valid non-expired session, or None.
|
||||
|
||||
The cache is consulted first, but every cache hit is re-validated
|
||||
against PostgreSQL so that a revoked or expired session is never
|
||||
trusted from a stale Redis key. On miss, read PostgreSQL and
|
||||
backfill the cache. Expired sessions found during lookup are deleted.
|
||||
"""
|
||||
cached = self._cache.get(token_hash)
|
||||
if cached is not None:
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT 1 FROM sessions WHERE token_hash = %s AND expires_at > %s',
|
||||
(token_hash, utc_now()))
|
||||
if cur.fetchone() is None:
|
||||
self._cache.delete(token_hash)
|
||||
return None
|
||||
return cached
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute(
|
||||
'SELECT user_id, expires_at FROM sessions WHERE token_hash = %s',
|
||||
(token_hash,))
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
if row['expires_at'] <= utc_now():
|
||||
cur.execute('DELETE FROM sessions WHERE token_hash = %s', (token_hash,))
|
||||
return None
|
||||
user_id = str(row['user_id'])
|
||||
ttl = max(1, int((row['expires_at'] - utc_now()).total_seconds()))
|
||||
self._cache.set(token_hash, user_id, ttl)
|
||||
return user_id
|
||||
|
||||
def delete_session(self, token_hash):
|
||||
self._cache.delete(token_hash)
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute('DELETE FROM sessions WHERE token_hash = %s', (token_hash,))
|
||||
|
||||
def cleanup_expired_sessions(self):
|
||||
with self._db.cursor() as cur:
|
||||
cur.execute('DELETE FROM sessions WHERE expires_at <= %s', (utc_now(),))
|
||||
@@ -0,0 +1,132 @@
|
||||
"""Strict request payload validation; any deviation is 400 invalid_request."""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from .errors import InvalidRequest
|
||||
|
||||
USERNAME_PATTERN = re.compile(r'[a-z0-9_]{3,32}')
|
||||
DIGITS_PATTERN = re.compile(r'[0-9]+')
|
||||
|
||||
|
||||
def safe_label(value):
|
||||
"""Render untrusted text for an error message without breaking its encoding.
|
||||
|
||||
Field names come straight from the client JSON, so they may contain the same
|
||||
lone surrogates that the payload itself is rejected for.
|
||||
"""
|
||||
text = value if isinstance(value, str) else repr(value)
|
||||
return text.encode('utf-8', 'replace').decode('utf-8')
|
||||
|
||||
|
||||
def require_encodable(value, name):
|
||||
"""Reject text that cannot survive a UTF-8 round trip.
|
||||
|
||||
`json.loads` happily produces lone surrogates from `\ud800` escapes. Such a
|
||||
string cannot be UTF-8 encoded, so it must be rejected before it is stored;
|
||||
otherwise the write is committed and the response encoding fails afterwards.
|
||||
"""
|
||||
try:
|
||||
value.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise InvalidRequest(
|
||||
'Поле %s содержит недопустимые символы Unicode' % name) from None
|
||||
return value
|
||||
|
||||
|
||||
def require_object(payload):
|
||||
if not isinstance(payload, dict):
|
||||
raise InvalidRequest('Тело запроса должно быть JSON-объектом')
|
||||
return payload
|
||||
|
||||
|
||||
def require_fields(payload, required, allowed):
|
||||
"""Reject missing required fields and any unknown field."""
|
||||
missing = [name for name in required if name not in payload]
|
||||
if missing:
|
||||
raise InvalidRequest('Отсутствуют обязательные поля: ' + ', '.join(sorted(missing)))
|
||||
extra = [name for name in payload if name not in allowed]
|
||||
if extra:
|
||||
raise InvalidRequest('Неизвестные поля: ' + ', '.join(sorted(safe_label(n) for n in extra)))
|
||||
return payload
|
||||
|
||||
|
||||
def require_text_field(payload, name, minimum, maximum, strip_check=True):
|
||||
value = payload.get(name)
|
||||
if not isinstance(value, str):
|
||||
raise InvalidRequest('Поле %s должно быть строкой' % name)
|
||||
require_encodable(value, name)
|
||||
if strip_check and not value.strip():
|
||||
raise InvalidRequest('Поле %s не может быть пустым или состоять из пробелов' % name)
|
||||
if len(value) < minimum:
|
||||
raise InvalidRequest('Поле %s короче %d символов' % (name, minimum))
|
||||
if len(value) > maximum:
|
||||
raise InvalidRequest('Поле %s длиннее %d символов' % (name, maximum))
|
||||
return value
|
||||
|
||||
|
||||
def require_username(payload):
|
||||
username = payload.get('username')
|
||||
# fullmatch, not match: a trailing "$" would also accept a final newline.
|
||||
if not isinstance(username, str) or USERNAME_PATTERN.fullmatch(username) is None:
|
||||
raise InvalidRequest('username должен соответствовать ^[a-z0-9_]{3,32}$')
|
||||
return require_encodable(username, 'username')
|
||||
|
||||
|
||||
def require_uuid_string(value, name):
|
||||
if not isinstance(value, str):
|
||||
raise InvalidRequest('Поле %s должно быть строкой UUID' % name)
|
||||
try:
|
||||
return str(uuid.UUID(value))
|
||||
except ValueError:
|
||||
raise InvalidRequest('Поле %s не является корректным UUID' % name) from None
|
||||
|
||||
|
||||
def require_member_ids(payload, maximum, minimum=1):
|
||||
value = payload.get('member_ids')
|
||||
if not isinstance(value, list):
|
||||
raise InvalidRequest('member_ids должен быть массивом UUID')
|
||||
if not minimum <= len(value) <= maximum:
|
||||
raise InvalidRequest('member_ids должен содержать от %d до %d участников' % (minimum, maximum))
|
||||
identifiers = [require_uuid_string(item, 'member_ids') for item in value]
|
||||
if len(set(identifiers)) != len(identifiers):
|
||||
raise InvalidRequest('member_ids содержит повторяющихся участников')
|
||||
return identifiers
|
||||
|
||||
|
||||
def parse_limit(raw, default, maximum):
|
||||
"""Parse a limit query value strictly: absent means default, anything else must be a plain integer.
|
||||
|
||||
An empty value is a client error rather than an absent parameter, and the
|
||||
digit run is length-bounded before int() because Python refuses to convert
|
||||
strings longer than its integer-string limit, which would surface as a 500.
|
||||
"""
|
||||
if raw is None:
|
||||
return default
|
||||
if not DIGITS_PATTERN.fullmatch(raw):
|
||||
raise InvalidRequest('limit должен быть целым числом от 1 до %d' % maximum)
|
||||
# int() refuses absurdly long digit strings (ValueError -> 500). Comparing the
|
||||
# significant digits against the maximum first rejects them without converting.
|
||||
significant = raw.lstrip('0') or '0'
|
||||
if len(significant) > len(str(maximum)):
|
||||
raise InvalidRequest('limit должен быть в диапазоне 1..%d' % maximum)
|
||||
value = int(significant)
|
||||
if not 1 <= value <= maximum:
|
||||
raise InvalidRequest('limit должен быть в диапазоне 1..%d' % maximum)
|
||||
return value
|
||||
|
||||
|
||||
def parse_cursor(raw):
|
||||
if raw is None:
|
||||
return None
|
||||
if not isinstance(raw, str) or not raw:
|
||||
raise InvalidRequest('cursor должен быть непустой строкой')
|
||||
return raw
|
||||
|
||||
|
||||
def require_idempotency_key(value):
|
||||
"""Only the contract's bounded ASCII token is accepted."""
|
||||
if not isinstance(value, str) or re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', value) is None:
|
||||
raise InvalidRequest('Требуется корректный Idempotency-Key (1–128 ASCII символов)')
|
||||
return value
|
||||
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Concurrency evidence for level 4: N clients write M messages in parallel.
|
||||
|
||||
Command (see REPORT.md and `make concurrency`):
|
||||
python3 student-tests/concurrency.py --clients 10 --messages-per-client 10 \
|
||||
--output evidence/concurrency.json
|
||||
|
||||
Expectation: every acknowledged message is stored exactly once, the full history
|
||||
contains exactly clients*messages_per_client unique messages and the order over
|
||||
pages is strictly increasing by (created_at, id). No retries are hidden: any
|
||||
failed write is reported and fails the run.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
|
||||
from client import Client, Failure, LAB, arguments, check, timestamp # noqa: E402
|
||||
|
||||
MESSAGES_PATH = '/api/v1/chats/%s/messages'
|
||||
|
||||
|
||||
def build_fixture(base_url, ca_file, clients):
|
||||
"""One user per client plus a single chat everybody belongs to."""
|
||||
master = Client(base_url, ca_file, timeout=15)
|
||||
master.call('GET', '/health/ready', schema='Health')
|
||||
actors = [master.user('conc%d' % index) for index in range(clients)]
|
||||
chat = master.call('POST', '/api/v1/chats', 201, actor=actors[0],
|
||||
body={'title': 'Конкурентная запись',
|
||||
'member_ids': [actor['id'] for actor in actors]},
|
||||
schema='Chat')['json']
|
||||
check(set(chat['member_ids']) == {actor['id'] for actor in actors},
|
||||
'chat membership mismatch')
|
||||
return master, actors, chat
|
||||
|
||||
|
||||
def write_phase(base_url, ca_file, actors, chat, messages_per_client, prefix):
|
||||
"""Each client writes its own slice of messages, all starting at once."""
|
||||
barrier = threading.Barrier(len(actors))
|
||||
|
||||
def worker(index):
|
||||
client = Client(base_url, ca_file, timeout=15)
|
||||
actor = actors[index]
|
||||
barrier.wait()
|
||||
acknowledged, errors = [], []
|
||||
started = time.monotonic()
|
||||
for message_index in range(messages_per_client):
|
||||
text = '%s-%03d-%03d' % (prefix, index, message_index)
|
||||
try:
|
||||
message = client.message(actor, chat, text)['json']
|
||||
acknowledged.append((text, message['id']))
|
||||
except Failure as exc:
|
||||
errors.append('%s: %s' % (text, exc))
|
||||
return {'client_index': index, 'acknowledged': acknowledged, 'errors': errors,
|
||||
'duration_seconds': round(time.monotonic() - started, 3)}
|
||||
|
||||
started = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=len(actors)) as pool:
|
||||
results = list(pool.map(worker, range(len(actors))))
|
||||
return results, round(time.monotonic() - started, 3)
|
||||
|
||||
|
||||
def read_whole_history(client, actor, chat):
|
||||
items, cursor, pages = [], None, 0
|
||||
while True:
|
||||
query = '?limit=100' + ('' if cursor is None else '&cursor=' + quote(cursor, safe=''))
|
||||
page = client.call('GET', MESSAGES_PATH % chat['id'] + query, actor=actor,
|
||||
schema='MessagePage')['json']
|
||||
items.extend(page['items'])
|
||||
pages += 1
|
||||
cursor = page['next_cursor']
|
||||
check(pages < 100, 'pagination did not terminate')
|
||||
if cursor is None:
|
||||
return items, pages
|
||||
|
||||
|
||||
def main():
|
||||
def configure(parser):
|
||||
parser.add_argument('--clients', type=int, default=10)
|
||||
parser.add_argument('--messages-per-client', type=int, default=10)
|
||||
parser.add_argument('--output', default=None,
|
||||
help='Optional JSON report path, no session data inside')
|
||||
|
||||
args = arguments('Concurrent writes from independent HTTP clients', configure)
|
||||
check(1 <= args.clients <= 64, 'clients must be 1..64')
|
||||
check(1 <= args.messages_per_client <= 200, 'messages-per-client must be 1..200')
|
||||
expected = args.clients * args.messages_per_client
|
||||
prefix = 'conc-' + uuid.uuid4().hex[:8]
|
||||
|
||||
master, actors, chat = build_fixture(args.base_url, args.ca_file, args.clients)
|
||||
results, elapsed = write_phase(args.base_url, args.ca_file, actors, chat,
|
||||
args.messages_per_client, prefix)
|
||||
acknowledged = [pair for result in results for pair in result['acknowledged']]
|
||||
errors = [error for result in results for error in result['errors']]
|
||||
history, pages = read_whole_history(master, actors[0], chat)
|
||||
|
||||
stored_texts = [message['text'] for message in history]
|
||||
stored_ids = [message['id'] for message in history]
|
||||
expected_texts = {'%s-%03d-%03d' % (prefix, client, index)
|
||||
for client in range(args.clients)
|
||||
for index in range(args.messages_per_client)}
|
||||
missing = sorted(expected_texts.difference(stored_texts))
|
||||
keys = [(timestamp(message['created_at']), uuid.UUID(message['id']).int)
|
||||
for message in history]
|
||||
ordered_correctly = all(keys[index] < keys[index + 1] for index in range(len(keys) - 1))
|
||||
|
||||
report = {
|
||||
'lab': LAB,
|
||||
'workload': 'closed-loop parallel POST messages from independent clients',
|
||||
'clients': args.clients,
|
||||
'messages_per_client': args.messages_per_client,
|
||||
'expected_messages': expected,
|
||||
'acknowledged_201': len(acknowledged),
|
||||
'stored_in_history': len(history),
|
||||
'unique_stored_ids': len(set(stored_ids)),
|
||||
'unique_stored_texts': len(set(stored_texts)),
|
||||
'duplicates': len(stored_ids) - len(set(stored_ids)),
|
||||
'missing_texts': missing,
|
||||
'failed_writes': len(errors),
|
||||
'write_errors': errors[:10],
|
||||
'history_pages': pages,
|
||||
'history_order_strictly_increasing': ordered_correctly,
|
||||
'writers_elapsed_seconds': elapsed,
|
||||
'successful_rps': round(len(acknowledged) / elapsed, 3) if elapsed else None,
|
||||
'per_client': [{'client_index': result['client_index'],
|
||||
'acknowledged': len(result['acknowledged']),
|
||||
'errors': len(result['errors']),
|
||||
'duration_seconds': result['duration_seconds']}
|
||||
for result in sorted(results, key=lambda item: item['client_index'])],
|
||||
'note': ('Client-observed timings: closed-loop, single Docker host, no retries. '
|
||||
'Latency percentiles are intentionally absent here (see make load).'),
|
||||
}
|
||||
text = json.dumps(report, ensure_ascii=False, indent=2)
|
||||
if args.output:
|
||||
destination = Path(args.output)
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(text + '\n')
|
||||
print(text)
|
||||
check(not errors, 'some parallel writes failed')
|
||||
check(len(acknowledged) == expected, 'not every write was acknowledged')
|
||||
check(len(history) == expected, 'history holds %d messages instead of %d'
|
||||
% (len(history), expected))
|
||||
check(not missing, 'messages missing from the history')
|
||||
check(len(set(stored_ids)) == expected, 'duplicate message ids in the history')
|
||||
check(ordered_correctly, 'history is not strictly ordered by (created_at, id)')
|
||||
print('PASS: %d parallel messages from %d clients stored exactly once' %
|
||||
(expected, args.clients))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (Failure, ValueError, OSError) as exc:
|
||||
print('FAIL:', exc)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Previous-lab regressions: durable replay, expiry, rollback and dependency failures."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
sys.path.insert(0, str(ROOT / 'scripts'))
|
||||
from client import Client, arguments, check, run
|
||||
import manage
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--mode', choices=('storage', 'expiry'), default='storage')
|
||||
args = arguments(__doc__, configure)
|
||||
c = Client(args.base_url, args.ca_file, timeout=10)
|
||||
if args.mode == 'expiry':
|
||||
alice = c.user('expiry')
|
||||
c.call('GET', '/api/v1/users/me', actor=alice)
|
||||
time.sleep(3)
|
||||
for _ in range(8):
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
print('PASS: short SESSION_TTL_SECONDS expiry across replicas')
|
||||
return
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
# Unique constraint must settle simultaneous registration.
|
||||
body = dict(alice['registration'], username='race_' + uuid.uuid4().hex[:20])
|
||||
def register(_):
|
||||
return Client(args.base_url,args.ca_file).call('POST','/api/v1/users',[201,409],body=body)['status']
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
check(sorted(pool.map(register,range(2)))==[201,409], 'registration race corrupted uniqueness')
|
||||
before = c.call('GET','/api/v1/chats?limit=100',actor=alice,schema='ChatPage')['json']
|
||||
c.call('POST','/api/v1/chats',400,actor=alice,
|
||||
body={'title':'Rollback','member_ids':[alice['id'],str(uuid.uuid4())]})
|
||||
after = c.call('GET','/api/v1/chats?limit=100',actor=alice,schema='ChatPage')['json']
|
||||
check(before==after, 'invalid chat left a visible partial object')
|
||||
key = uuid.uuid4().hex
|
||||
original = c.message(alice,chat,'Replay after full restart',key=key)['json']
|
||||
try:
|
||||
manage.compose('stop','redis')
|
||||
check(c.message(alice,chat,'Redis down')['status']==201, 'Redis failure broke writes')
|
||||
c.call('GET','/api/v1/users/me',401)
|
||||
manage.start_services('redis')
|
||||
manage.wait_healthy('redis')
|
||||
# Revoking with Redis unavailable must still invalidate the authoritative DB.
|
||||
temp = c.user('revoke')
|
||||
manage.compose('stop','redis')
|
||||
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=temp)
|
||||
manage.start_services('redis')
|
||||
manage.wait_healthy('redis')
|
||||
for _ in range(4):
|
||||
c.call('GET','/api/v1/users/me',401,actor=temp)
|
||||
manage.compose('stop','postgres')
|
||||
# LB may answer 502 when every unready backend is excluded. Verify
|
||||
# API's own dependency semantics inside its private network separately.
|
||||
source = """import json,sys,urllib.request,urllib.error
|
||||
actor=json.load(sys.stdin)
|
||||
for path,want in [('/health/live',200),('/health/ready',503),('/api/v1/users/me',503)]:
|
||||
req=urllib.request.Request('http://127.0.0.1:8080'+path,headers={'Authorization':'Bearer '+actor['token']})
|
||||
try:
|
||||
response=urllib.request.urlopen(req,timeout=8)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response=exc
|
||||
assert response.code==want,(path,response.code,want)
|
||||
response.close()
|
||||
print('PASS: API liveness 200, readiness/business 503 while PostgreSQL is down')
|
||||
"""
|
||||
subprocess.run(['docker','compose','exec','-T','api-a','python','-c',source],
|
||||
cwd=ROOT,input=json.dumps({'token':alice['token']}),text=True,check=True)
|
||||
try:
|
||||
response=urllib.request.urlopen(c.base+'/api/v1/users/me',timeout=8)
|
||||
except urllib.error.HTTPError as exc:
|
||||
response=exc
|
||||
check(response.code in (401,502,503,504), 'LB must reject unavailable/unauthenticated request')
|
||||
response.close()
|
||||
finally:
|
||||
manage.start_services('postgres','redis')
|
||||
for s in ('postgres','redis',*manage.SERVICES):
|
||||
manage.wait_healthy(s)
|
||||
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
||||
# Stronger persistence check than public smoke: replay an OLD key as well.
|
||||
manage.restart()
|
||||
check(c.message(alice,chat,original['text'],key=key)['json']==original, 'idempotency lost on restart')
|
||||
print('PASS: registration race, invalid chat rollback, Redis fallback, logout, DB 503, restart replay with original token/key')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,207 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Repeatable read/write load with explicit stop/update and sanitized evidence."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
sys.path.insert(0, str(ROOT / 'scripts'))
|
||||
from client import Client, Failure, arguments, check, run
|
||||
import manage
|
||||
|
||||
|
||||
def whole_history(client, actor, chat):
|
||||
items, cursor = [], None
|
||||
while True:
|
||||
path = '/api/v1/chats/%s/messages?limit=100' % chat['id']
|
||||
if cursor:
|
||||
path += '&cursor=' + quote(cursor, safe='')
|
||||
page = client.call('GET', path, actor=actor, schema='MessagePage')['json']
|
||||
items.extend(page['items'])
|
||||
cursor = page['next_cursor']
|
||||
if cursor is None:
|
||||
return items
|
||||
|
||||
|
||||
def scenario(args, service=None):
|
||||
c = Client(args.base_url, args.ca_file, timeout=2)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
seed_key = uuid.uuid4().hex
|
||||
seed = c.message(alice, chat, 'До воздействия', key=seed_key)['json']
|
||||
events, samples, confirmations = [], [], {}
|
||||
lock = threading.Lock()
|
||||
started = time.monotonic()
|
||||
end = threading.Event()
|
||||
workload_started = threading.Barrier(5)
|
||||
|
||||
def load(worker_id):
|
||||
local = Client(args.base_url, args.ca_file, timeout=2)
|
||||
key = uuid.uuid4().hex
|
||||
workload_started.wait()
|
||||
while not end.is_set():
|
||||
text = 'experiment-%d-%s' % (worker_id, key)
|
||||
start = time.monotonic()
|
||||
try:
|
||||
reply = local.message(alice, chat, text, key=key)
|
||||
with lock:
|
||||
confirmations[text] = reply['json']
|
||||
event = {'kind': 'write', 'ok': True, 'instance': reply['headers']['X-Instance-Id']}
|
||||
key = uuid.uuid4().hex
|
||||
except Failure:
|
||||
event = {'kind': 'write', 'ok': False}
|
||||
# Keep the same body/key after uncertain response.
|
||||
event.update(at=time.monotonic() - started, elapsed=time.monotonic() - start)
|
||||
with lock:
|
||||
events.append(event)
|
||||
start = time.monotonic()
|
||||
try:
|
||||
local.call('GET', '/api/v1/chats/%s/messages?limit=1' % chat['id'], actor=bob, schema='MessagePage')
|
||||
ok = True
|
||||
except Failure:
|
||||
ok = False
|
||||
with lock:
|
||||
events.append({'kind': 'read', 'ok': ok, 'at': time.monotonic()-started,
|
||||
'elapsed': time.monotonic()-start})
|
||||
|
||||
def stats():
|
||||
ids = [x for name in (*manage.SERVICES, 'nginx', 'postgres', 'redis') for x in manage.containers(name)]
|
||||
while not end.is_set():
|
||||
proc = subprocess.run(['docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids],
|
||||
text=True, capture_output=True, timeout=10)
|
||||
with lock:
|
||||
samples.append({'at': round(time.monotonic()-started, 3),
|
||||
'containers': [json.loads(x) for x in proc.stdout.splitlines() if x]})
|
||||
end.wait(1)
|
||||
|
||||
def status():
|
||||
return [{'service': s, 'id': data['Id'][:12], 'instance': data['Config']['Hostname'],
|
||||
'running': data['State']['Running'], 'image': data['Image']}
|
||||
for s in manage.SERVICES for data in map(manage.inspect, manage.containers(s))]
|
||||
|
||||
before = status()
|
||||
monitor = threading.Thread(target=stats, daemon=True)
|
||||
action_at = action_done = None
|
||||
hook_output = ''
|
||||
action_error = None
|
||||
with ThreadPoolExecutor(max_workers=4) as pool:
|
||||
jobs = [pool.submit(load, i) for i in range(4)]
|
||||
workload_started.wait()
|
||||
monitor.start()
|
||||
try:
|
||||
time.sleep(3) # Same warm-up for each scenario.
|
||||
action_at = time.monotonic()-started
|
||||
if service:
|
||||
container = manage.containers(service)[0]
|
||||
target = manage.inspect(container)['Config']['Hostname']
|
||||
env = dict(os.environ, TARGET_INSTANCE_ID=target)
|
||||
proc = subprocess.run([str(ROOT/'scripts/stop-one.sh')], cwd=ROOT, env=env,
|
||||
capture_output=True, text=True, timeout=10)
|
||||
else:
|
||||
proc = subprocess.run([str(ROOT/'scripts/rolling-update.sh')], cwd=ROOT,
|
||||
capture_output=True, text=True, timeout=180)
|
||||
action_done = time.monotonic()-started
|
||||
hook_output = proc.stdout + proc.stderr
|
||||
check(proc.returncode == 0, 'action failed; inspect sanitized action log')
|
||||
time.sleep(12 if service else 5)
|
||||
except Exception as exc:
|
||||
action_error = exc
|
||||
finally:
|
||||
end.set()
|
||||
for job in jobs:
|
||||
job.result()
|
||||
monitor.join(timeout=15)
|
||||
after = status()
|
||||
history = whole_history(c, bob, chat)
|
||||
by_text = {}
|
||||
for item in history:
|
||||
by_text.setdefault(item['text'], []).append(item)
|
||||
missing = [text for text, msg in confirmations.items() if by_text.get(text) != [msg]]
|
||||
duplicate_texts = sum(len(items)-1 for items in by_text.values())
|
||||
check(by_text.get(seed['text']) == [seed], 'pre-action confirmed message lost')
|
||||
check(c.message(alice, chat, seed['text'], key=seed_key)['json'] == seed, 'replay across action differs')
|
||||
|
||||
def summary(selected, elapsed):
|
||||
successes = [e for e in selected if e['ok']]
|
||||
latencies = sorted(e['elapsed'] * 1000 for e in successes)
|
||||
def percentile(p):
|
||||
return round(latencies[max(0, math.ceil(len(latencies)*p)-1)], 3) if latencies else None
|
||||
return {'successful_requests': len(successes), 'failed_requests': len(selected)-len(successes),
|
||||
'successful_rps': round(len(successes)/elapsed, 3) if elapsed else None,
|
||||
'error_ratio': round(1-len(successes)/len(selected), 6) if selected else None,
|
||||
'successful_latency_ms': {'p50': percentile(.5), 'p95': percentile(.95), 'p99': percentile(.99)}}
|
||||
|
||||
elapsed = time.monotonic()-started
|
||||
recovery = {}
|
||||
for kind in ('read', 'write'):
|
||||
times = [e['at']-action_at for e in events if e['ok'] and e['kind']==kind and e['at']>=action_done]
|
||||
recovery[kind] = round(min(times), 3) if times else None
|
||||
success_gaps = {}
|
||||
for kind in ('read', 'write'):
|
||||
timeline = sorted(e['at'] for e in events if e['ok'] and e['kind']==kind)
|
||||
# Include boundaries of the action, so silence during the entire action
|
||||
# cannot be hidden by success only before or after it.
|
||||
timeline = [action_at] + [t for t in timeline if action_at<=t<=(action_done or elapsed)] + [action_done or elapsed]
|
||||
success_gaps[kind] = round(max(b-a for a,b in zip(timeline,timeline[1:])), 3)
|
||||
report = {'scenario': 'stop-'+service if service else 'rolling-update', 'concurrency': 4,
|
||||
'warmup_seconds': 3, 'elapsed_seconds': round(elapsed, 3),
|
||||
'action_start_seconds': round(action_at, 3), 'action_done_seconds': round(action_done, 3) if action_done else None,
|
||||
'first_success_after_action_from_action_start_seconds': recovery,
|
||||
'before': before, 'after': after,
|
||||
'http': summary(events, elapsed),
|
||||
'read': summary([e for e in events if e['kind']=='read'], elapsed),
|
||||
'write': summary([e for e in events if e['kind']=='write'], elapsed),
|
||||
'during_action': summary([e for e in events if action_at<=e['at']<=(action_done or elapsed)], (action_done or elapsed)-action_at),
|
||||
'confirmed_messages': len(confirmations), 'history_messages': len(history),
|
||||
'missing_or_changed_confirmations': len(missing), 'duplicate_texts': duplicate_texts,
|
||||
'resources': samples, 'maximum_gap_between_successes_during_action_seconds': success_gaps,
|
||||
'note': 'Closed-loop, four writers alternate POST and GET. Percentiles cover successful HTTP requests only; retries preserve key/body. Recovery is first success after hook completion measured from hook start; not an outage duration. One Docker host; no node/DB/LB HA claim.'}
|
||||
name = report['scenario']
|
||||
(ROOT/'evidence'/('%s.json' % name)).write_text(json.dumps(report, ensure_ascii=False, indent=2)+'\n')
|
||||
(ROOT/'evidence'/('%s-action.txt' % name)).write_text(hook_output)
|
||||
check(action_error is None, 'action failed: %s' % action_error)
|
||||
check(not missing and not duplicate_texts, 'lost/duplicated messages')
|
||||
if service:
|
||||
check(all(x is not None and x<=10 for x in recovery.values()), 'read/write recovery exceeded 10s')
|
||||
target_entry = next(e for e in after if e['service']==service)
|
||||
check(not target_entry['running'], 'target replica is still running')
|
||||
recent_instances = {e.get('instance') for e in events if e['kind']=='write' and e['ok'] and e['at']>action_done+3}
|
||||
check(target_entry['instance'] not in recent_instances, 'stopped target still serves writes')
|
||||
else:
|
||||
check(all(x<=3 for x in success_gaps.values()), 'rolling read/write availability gap exceeded 3 seconds')
|
||||
for kind in ('read', 'write'):
|
||||
for left in range(int(action_at), int(action_done), 3):
|
||||
right = min(left+3, action_done)
|
||||
check(any(e['ok'] and e['kind']==kind and left<=e['at']<=right for e in events),
|
||||
'no successful %s during rolling window' % kind)
|
||||
check(all(a['id']!=b['id'] for a,b in zip(before, after)), 'not every replica was replaced')
|
||||
check(all(a['image']!=b['image'] for a,b in zip(before, after)), 'new image was not deployed')
|
||||
print(json.dumps({k:v for k,v in report.items() if k not in ('resources','before','after','note')}, ensure_ascii=False))
|
||||
print('PASS:', name)
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--scenario', choices=('failover', 'rolling'), required=True)
|
||||
args = arguments(__doc__, configure)
|
||||
try:
|
||||
if args.scenario == 'failover':
|
||||
for service in manage.SERVICES:
|
||||
manage.restore()
|
||||
scenario(args, service)
|
||||
else:
|
||||
scenario(args)
|
||||
finally:
|
||||
manage.restore()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Cross-replica idempotency, key isolation, sessions and pagination."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments(__doc__)
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = '/api/v1/chats/%s/messages' % chat['id']
|
||||
observed = {c.call('GET', '/api/v1/users/me', actor=alice)['headers']['X-Instance-Id'] for _ in range(20)}
|
||||
check(len(observed) >= 2, 'one session must work through at least two replicas')
|
||||
key = uuid.uuid4().hex
|
||||
barrier = threading.Barrier(12)
|
||||
|
||||
def worker(_):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
barrier.wait()
|
||||
return local.message(alice, chat, 'Один ключ — одно сообщение 👋', key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as pool:
|
||||
replies = list(pool.map(worker, range(12)))
|
||||
instances = {r['headers']['X-Instance-Id'] for r in replies}
|
||||
check(len(instances) >= 2, 'parallel key replay did not reach both replicas')
|
||||
first = replies[0]['json']
|
||||
check(all(r['json'] == first and r['status'] == 201 for r in replies), 'replays differ')
|
||||
page = c.call('GET', path, actor=alice, schema='MessagePage')['json']
|
||||
check(page['items'] == [first], 'parallel replay stored duplicate messages')
|
||||
c.call('POST', path, 409, actor=alice, body={'text': 'другое'}, headers={'Idempotency-Key': key})
|
||||
for bad in (None, '', 'bad key', 'x' * 129, 'bad/key'):
|
||||
c.call('POST', path, 400, actor=alice, body={'text': 'invalid'},
|
||||
headers={} if bad is None else {'Idempotency-Key': bad})
|
||||
c.call('POST', path, 404, actor=eve, body={'text': first['text']}, headers={'Idempotency-Key': key})
|
||||
other_user = c.message(bob, chat, first['text'], key=key)['json']
|
||||
check(other_user['id'] != first['id'], 'different user key was aliased')
|
||||
second_chat = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Другой чат', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
other_chat = c.message(alice, second_chat, first['text'], key=key)['json']
|
||||
check(other_chat['id'] != first['id'], 'different chat key was aliased')
|
||||
for index in range(5):
|
||||
c.message(alice, chat, 'page-%d' % index)
|
||||
result, cursor, page_instances = [], None, set()
|
||||
while True:
|
||||
url = path + '?limit=1' + (('&cursor=' + quote(cursor, safe='')) if cursor else '')
|
||||
reply = c.call('GET', url, actor=alice, schema='MessagePage')
|
||||
page_instances.add(reply['headers']['X-Instance-Id'])
|
||||
result.extend(reply['json']['items'])
|
||||
cursor = reply['json']['next_cursor']
|
||||
if cursor is None:
|
||||
break
|
||||
check(len(result) == len({m['id'] for m in result}) == 7, 'pagination lost/duplicated messages')
|
||||
check(len(page_instances) >= 2, 'cursor did not cross replicas')
|
||||
# Revocation is observed on every API, even with an existing Redis entry.
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
for _ in range(8):
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
c.call('POST', path, 401, actor=alice, body={'text': first['text']}, headers={'Idempotency-Key': key})
|
||||
print(json.dumps({'parallel_requests': 12, 'parallel_replicas': sorted(instances),
|
||||
'unique_parallel_messages': 1, 'pagination_messages': 7,
|
||||
'pagination_replicas': sorted(page_instances)}, ensure_ascii=False))
|
||||
print('PASS: cross-replica idempotency, isolation, pagination and logout')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A live but frozen API must be removed by active readiness, then restored."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0,str(ROOT/'scripts'))
|
||||
import manage
|
||||
|
||||
container=manage.containers('api-a')[0]
|
||||
started=time.monotonic()
|
||||
try:
|
||||
manage.command(['docker','pause',container])
|
||||
while True:
|
||||
config=manage.compose('exec','-T','nginx','cat','/etc/nginx/upstreams.conf',capture=True)
|
||||
if any('api-a:' in line and ' down;' in line for line in config.splitlines()):
|
||||
break
|
||||
assert time.monotonic()-started < 5, 'readiness did not remove frozen backend within 5s'
|
||||
time.sleep(.2)
|
||||
print('PASS: frozen api-a excluded in %.3fs' % (time.monotonic()-started))
|
||||
finally:
|
||||
manage.command(['docker','unpause',container])
|
||||
started=time.monotonic()
|
||||
while True:
|
||||
config=manage.compose('exec','-T','nginx','cat','/etc/nginx/upstreams.conf',capture=True)
|
||||
if not any('api-a:' in line and ' down;' in line for line in config.splitlines()):
|
||||
break
|
||||
assert time.monotonic()-started<10, 'healthy backend did not return'
|
||||
time.sleep(.2)
|
||||
print('PASS: healthy backend restored in %.3fs' % (time.monotonic()-started))
|
||||
|
||||
manage.wait_healthy('api-a')
|
||||
@@ -0,0 +1,354 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Student-owned lab3 regression tests: sessions, validation, pagination.
|
||||
|
||||
One command: make student-test
|
||||
Direct call: python3 student-tests/run.py --base-url http://localhost:8080
|
||||
|
||||
The suite complements tests/smoke.py and never weakens it. Every run creates
|
||||
synthetic users, chats and messages only; no public check is imported or patched.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import datetime as dt
|
||||
import http.client
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote, urlsplit
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
|
||||
from client import Client, Failure, LAB, arguments, check, timestamp # noqa: E402
|
||||
|
||||
if LAB != 3:
|
||||
print('FAIL: student tests target lab3, but lab.json declares lab%d' % LAB)
|
||||
raise SystemExit(2)
|
||||
|
||||
CHAT_PATH = '/api/v1/chats/%s'
|
||||
class StudentClient(Client):
|
||||
def call(self, method, path, *args, **kwargs):
|
||||
if method == 'POST' and path.endswith('/messages'):
|
||||
headers = dict(kwargs.get('headers') or {})
|
||||
headers.setdefault('Idempotency-Key', uuid.uuid4().hex)
|
||||
kwargs['headers'] = headers
|
||||
return super().call(method, path, *args, **kwargs)
|
||||
|
||||
|
||||
MESSAGES_PATH = CHAT_PATH + '/messages'
|
||||
MAX_PAGES = 200
|
||||
|
||||
TESTS = []
|
||||
|
||||
|
||||
def test(func):
|
||||
TESTS.append(func)
|
||||
return func
|
||||
|
||||
|
||||
class Context:
|
||||
"""Shared synthetic fixtures: three users, one chat between two of them."""
|
||||
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
self.alice, self.bob, self.eve, self.chat = client.bootstrap()
|
||||
|
||||
def new_chat(self, title, members):
|
||||
return self.client.call('POST', '/api/v1/chats', 201, actor=members[0],
|
||||
body={'title': title,
|
||||
'member_ids': [member['id'] for member in members]},
|
||||
schema='Chat')['json']
|
||||
|
||||
def send(self, actor, chat, text):
|
||||
return self.client.call('POST', MESSAGES_PATH % chat['id'], 201, actor=actor,
|
||||
body={'text': text}, schema='Message')['json']
|
||||
|
||||
def page(self, actor, chat, limit, cursor=None):
|
||||
query = '?limit=%d' % limit
|
||||
if cursor is not None:
|
||||
query += '&cursor=' + quote(cursor, safe='')
|
||||
return self.client.call('GET', MESSAGES_PATH % chat['id'] + query, actor=actor,
|
||||
schema='MessagePage')['json']
|
||||
|
||||
def traverse(self, actor, chat, limits):
|
||||
"""Walk the whole history, changing the page size between requests."""
|
||||
items, cursor, pages = [], None, 0
|
||||
while True:
|
||||
limit = limits[min(pages, len(limits) - 1)]
|
||||
page = self.page(actor, chat, limit, cursor)
|
||||
items.extend(page['items'])
|
||||
cursor = page['next_cursor']
|
||||
pages += 1
|
||||
check(pages < MAX_PAGES, 'pagination did not terminate')
|
||||
if cursor is None:
|
||||
break
|
||||
return items, pages
|
||||
|
||||
|
||||
def ordered(records):
|
||||
"""Contract order: (created_at, id) ascending, exactly as the client sorts."""
|
||||
return sorted(records, key=lambda item: (timestamp(item['created_at']),
|
||||
uuid.UUID(item['id']).int))
|
||||
|
||||
|
||||
def traversal_order(records):
|
||||
return [item['id'] for item in records]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------- health
|
||||
@test
|
||||
def health_and_request_id(ctx):
|
||||
for path in ('/health/live', '/health/ready'):
|
||||
result = ctx.client.call('GET', path, schema='Health')
|
||||
check(result['json'] == {'status': 'ok'},
|
||||
'%s must return exactly {"status":"ok"}' % path)
|
||||
check(len(result['headers']['X-Request-Id']) >= 8, 'request id looks too short')
|
||||
marker = 'student-' + uuid.uuid4().hex
|
||||
echoed = ctx.client.call('GET', '/health/live', headers={'X-Request-Id': marker})
|
||||
check(echoed['headers']['X-Request-Id'] == marker, 'client request id was not echoed')
|
||||
error = ctx.client.call('GET', '/api/v1/users/me', 401, headers={'X-Request-Id': marker})
|
||||
check(error['headers']['X-Request-Id'] == marker, 'error response lost the request id')
|
||||
check(error['json']['error']['request_id'] == marker, 'error body request_id mismatch')
|
||||
|
||||
|
||||
# -------------------------------------------------------------- unknown route
|
||||
@test
|
||||
def unknown_route_and_method_are_json_404(ctx):
|
||||
ctx.client.call('GET', '/api/v1/unknown', 404)
|
||||
ctx.client.call('DELETE', '/api/v1/chats', 404, actor=ctx.alice)
|
||||
ctx.client.call('GET', '/api/v1/chats/', 404, actor=ctx.alice)
|
||||
|
||||
|
||||
# --------------------------------------------------------------- user payload
|
||||
@test
|
||||
def malformed_json_is_rejected(ctx):
|
||||
json_headers = {'Content-Type': 'application/json'}
|
||||
ctx.client.call('POST', '/api/v1/users', 400, raw=b'{"username": "broken",', headers=json_headers)
|
||||
ctx.client.call('POST', '/api/v1/users', 400, raw=b'[1, 2, 3]', headers=json_headers)
|
||||
ctx.client.call('POST', '/api/v1/users', 400, raw=b'"just a string"', headers=json_headers)
|
||||
ctx.client.call('POST', '/api/v1/users', 400, raw=b'', headers=json_headers)
|
||||
ctx.client.call('POST', '/api/v1/users', 415, raw=b'{"username": "x"}',
|
||||
headers={'Content-Type': 'text/plain'})
|
||||
ctx.client.call('POST', '/api/v1/users', 415, raw=b'{"username": "x"}',
|
||||
headers={'Content-Type': 'application/x-www-form-urlencoded'})
|
||||
|
||||
|
||||
@test
|
||||
def duplicate_username_is_409(ctx):
|
||||
name = 'dup_' + uuid.uuid4().hex[:12]
|
||||
body = {'username': name, 'display_name': 'Дубликат',
|
||||
'password': 'T3st-' + uuid.uuid4().hex}
|
||||
created = ctx.client.call('POST', '/api/v1/users', 201, body=body, schema='User')['json']
|
||||
check(created['username'] == body['username'], 'username changed on creation')
|
||||
conflict = ctx.client.call('POST', '/api/v1/users', 409, body=body)
|
||||
check(conflict['json']['error']['code'] == 'conflict', 'duplicate username must be conflict')
|
||||
|
||||
|
||||
@test
|
||||
def user_payload_validation(ctx):
|
||||
name = 'valid_' + uuid.uuid4().hex[:8]
|
||||
for bad in (
|
||||
{'username': name, 'display_name': 'Имя', 'password': 'ok', 'extra': 1},
|
||||
{'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': name, 'password': 'ok'},
|
||||
{'username': name, 'display_name': ' ', 'password': 'ok'},
|
||||
{'username': name, 'display_name': '', 'password': 'ok'},
|
||||
{'username': name, 'display_name': 42, 'password': 'ok'},
|
||||
{'username': name, 'display_name': None, 'password': 'ok'},
|
||||
{'username': name, 'display_name': 'д' * 101, 'password': 'ok'},
|
||||
{'username': 'AB', 'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': 'with space', 'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': 'юзер123', 'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': 'x' * 33, 'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': '', 'display_name': 'Имя', 'password': 'ok'},
|
||||
{'username': 123, 'display_name': 'Имя', 'password': 'ok'},
|
||||
# password too short
|
||||
{'username': name, 'display_name': 'Имя', 'password': 'short'},
|
||||
# password too long
|
||||
{'username': name, 'display_name': 'Имя', 'password': 'д' * 129},
|
||||
):
|
||||
ctx.client.call('POST', '/api/v1/users', 400, body=bad)
|
||||
ok = ctx.client.call('POST', '/api/v1/users', 201,
|
||||
body={'username': name, 'display_name': 'Студент Юникод 👋',
|
||||
'password': 'T3st-' + uuid.uuid4().hex},
|
||||
schema='User')['json']
|
||||
check(ok['display_name'] == 'Студент Юникод 👋', 'unicode display_name changed')
|
||||
check('password' not in ok and 'token' not in ok, 'user projection leaks credentials')
|
||||
|
||||
|
||||
# ------------------------------------------------------------- sessions
|
||||
@test
|
||||
def basic_login_issues_bearer_session(ctx):
|
||||
reg = ctx.alice['registration']
|
||||
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
||||
session = ctx.client.call('POST', '/api/v1/auth/sessions', 201,
|
||||
headers={'Authorization': 'Basic ' + credential},
|
||||
schema='Session')['json']
|
||||
check(session['user'] == ctx.alice['user'], 'session user mismatch')
|
||||
check(session['token_type'] == 'Bearer', 'token type must be Bearer')
|
||||
check(len(session['token']) >= 32, 'token must be opaque and long enough')
|
||||
check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),
|
||||
'session already expired')
|
||||
ctx.alice['token'] = session['token']
|
||||
|
||||
# wrong password -> 401
|
||||
bad = base64.b64encode((reg['username'] + ':wrong-password').encode()).decode()
|
||||
ctx.client.call('POST', '/api/v1/auth/sessions', 401,
|
||||
headers={'Authorization': 'Basic ' + bad})
|
||||
# unknown user -> 401
|
||||
bad = base64.b64encode(('nonexistent-user:' + reg['password']).encode()).decode()
|
||||
ctx.client.call('POST', '/api/v1/auth/sessions', 401,
|
||||
headers={'Authorization': 'Basic ' + bad})
|
||||
|
||||
|
||||
@test
|
||||
def bearer_required_for_business_endpoints(ctx):
|
||||
# no auth -> 401
|
||||
ctx.client.call('GET', '/api/v1/users/me', 401)
|
||||
# Basic on business endpoint -> 401
|
||||
reg = ctx.alice['registration']
|
||||
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
||||
ctx.client.call('GET', '/api/v1/users/me', 401,
|
||||
headers={'Authorization': 'Basic ' + credential})
|
||||
# X-User-Id ignored -> 401
|
||||
ctx.client.call('GET', '/api/v1/users/me', 401, headers={'X-User-Id': ctx.alice['id']})
|
||||
# Bearer works
|
||||
me = ctx.client.call('GET', '/api/v1/users/me', 200, actor=ctx.alice,
|
||||
schema='User')['json']
|
||||
check(me == ctx.alice['user'], 'current user mismatch')
|
||||
|
||||
|
||||
@test
|
||||
def logout_revokes_session(ctx):
|
||||
reg = ctx.alice['registration']
|
||||
credential = base64.b64encode((reg['username'] + ':' + reg['password']).encode()).decode()
|
||||
session = ctx.client.call('POST', '/api/v1/auth/sessions', 201,
|
||||
headers={'Authorization': 'Basic ' + credential},
|
||||
schema='Session')['json']
|
||||
token = session['token']
|
||||
ctx.client.call('DELETE', '/api/v1/auth/sessions/current', 204,
|
||||
headers={'Authorization': 'Bearer ' + token})
|
||||
# revoked token -> 401
|
||||
ctx.client.call('GET', '/api/v1/users/me', 401,
|
||||
headers={'Authorization': 'Bearer ' + token})
|
||||
ctx.client.call('DELETE', '/api/v1/auth/sessions/current', 401,
|
||||
headers={'Authorization': 'Bearer ' + token})
|
||||
|
||||
|
||||
# -------------------------------------------------------------- message rules
|
||||
@test
|
||||
def foreign_chat_is_invisible(ctx):
|
||||
other = ctx.new_chat('Чат без Евы', [ctx.alice, ctx.bob])
|
||||
ctx.client.call('GET', CHAT_PATH % other['id'], 404, actor=ctx.eve)
|
||||
ctx.client.call('GET', MESSAGES_PATH % other['id'], 404, actor=ctx.eve)
|
||||
ctx.client.call('POST', MESSAGES_PATH % other['id'], 404, actor=ctx.eve,
|
||||
body={'text': 'чужое сообщение'})
|
||||
listed = ctx.client.call('GET', '/api/v1/chats?limit=100', actor=ctx.eve,
|
||||
schema='ChatPage')['json']
|
||||
check(all(item['id'] != other['id'] for item in listed['items']),
|
||||
'foreign chat leaked into the list of another user')
|
||||
member_view = ctx.client.call('GET', '/api/v1/chats?limit=100', actor=ctx.bob,
|
||||
schema='ChatPage')['json']
|
||||
check(any(item['id'] == other['id'] for item in member_view['items']),
|
||||
'member cannot see the own chat')
|
||||
ctx.client.call('GET', CHAT_PATH % uuid.uuid4(), 404, actor=ctx.alice)
|
||||
ctx.client.call('GET', '/api/v1/chats/not-a-uuid', 400, actor=ctx.alice)
|
||||
ctx.client.call('GET', MESSAGES_PATH % 'not-a-uuid', 400, actor=ctx.alice)
|
||||
|
||||
|
||||
@test
|
||||
def message_payload_validation(ctx):
|
||||
"""Empty and whitespace-only text is rejected."""
|
||||
for bad in (
|
||||
{'text': ''},
|
||||
{'text': ' '},
|
||||
{'text': '\u00a0\t\n'},
|
||||
{},
|
||||
{'text': 123},
|
||||
{'text': None},
|
||||
{'text': []},
|
||||
{'text': 'ок', 'sender_id': ctx.bob['id']},
|
||||
{'text': 'д' * 4001},
|
||||
):
|
||||
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 400, actor=ctx.alice, body=bad)
|
||||
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 401, body={'text': 'ок'})
|
||||
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 400, actor=ctx.alice, raw=b'{"text":}',
|
||||
headers={'Content-Type': 'application/json'})
|
||||
ctx.client.call('POST', MESSAGES_PATH % ctx.chat['id'], 415, actor=ctx.alice,
|
||||
raw=b'{"text":"ok"}', headers={'Content-Type': 'application/xml'})
|
||||
|
||||
|
||||
@test
|
||||
def text_limits_and_unicode_roundtrip(ctx):
|
||||
longest = 'д' * 3999 + '👋'
|
||||
check(len(longest) == 4000, 'fixture must be exactly 4000 code points')
|
||||
created = ctx.send(ctx.bob, ctx.chat, longest)
|
||||
check(created['text'] == longest, '4000 code points text changed')
|
||||
check(created['sender_id'] == ctx.bob['id'], 'sender must be derived from actor')
|
||||
mixed = 'Привет 👋🏽 \u00a0 e\u0301 — астральные 🚀🧪 и кавычки «»'
|
||||
echo = ctx.send(ctx.alice, ctx.chat, mixed)
|
||||
check(echo['text'] == mixed, 'mixed unicode text changed in transit')
|
||||
check(echo['chat_id'] == ctx.chat['id'], 'message bound to the wrong chat')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- pagination
|
||||
@test
|
||||
def multi_page_traversal_is_exact_and_stable(ctx):
|
||||
chat = ctx.new_chat('Пагинация', [ctx.bob, ctx.alice])
|
||||
sent = []
|
||||
for index in range(7):
|
||||
actor = ctx.alice if index % 2 == 0 else ctx.bob
|
||||
sent.append(ctx.send(actor, chat, 'страница-%d-👋' % index))
|
||||
expected = traversal_order(ordered(sent))
|
||||
walked, pages = ctx.traverse(ctx.bob, chat, [2])
|
||||
check(pages >= 4, 'seven messages with limit=2 must take at least four pages')
|
||||
check(traversal_order(walked) == expected, 'traversal lost, duplicated or reordered messages')
|
||||
check(len(set(traversal_order(walked))) == len(walked), 'traversal returned duplicates')
|
||||
switched, _ = ctx.traverse(ctx.alice, chat, [2, 3, 1])
|
||||
check(traversal_order(switched) == expected, 'changing limit mid-traversal changed the order')
|
||||
singles, _ = ctx.traverse(ctx.alice, chat, [1])
|
||||
check(len(singles) == 7, 'limit=1 traversal must return all seven messages')
|
||||
first = ctx.page(ctx.alice, chat, 2)
|
||||
check(len(first['items']) == 2 and first['next_cursor'], 'first page shape is wrong')
|
||||
check(first['items'][0]['id'] == expected[0], 'first page must start with the oldest message')
|
||||
|
||||
|
||||
@test
|
||||
def default_page_limit_is_fifty(ctx):
|
||||
chat = ctx.new_chat('Предел страницы по умолчанию', [ctx.alice, ctx.bob])
|
||||
for index in range(51):
|
||||
ctx.send(ctx.alice, chat, 'default-limit-%d' % index)
|
||||
page = ctx.client.call('GET', MESSAGES_PATH % chat['id'], actor=ctx.alice,
|
||||
schema='MessagePage')['json']
|
||||
check(len(page['items']) == 50, 'default limit must be 50, got %d' % len(page['items']))
|
||||
check(page['next_cursor'], 'a page that is not the last one needs a cursor')
|
||||
tail = ctx.page(ctx.alice, chat, 100, page['next_cursor'])
|
||||
check(len(tail['items']) == 1, 'the remaining page must hold exactly one message')
|
||||
check(tail['next_cursor'] is None, 'last page must have next_cursor null')
|
||||
merged = traversal_order(page['items']) + traversal_order(tail['items'])
|
||||
check(len(set(merged)) == 51, 'page boundaries duplicated or dropped messages')
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Student-owned lab3 regression tests: sessions, validation, pagination')
|
||||
client = StudentClient(args.base_url, args.ca_file)
|
||||
ctx = Context(client)
|
||||
print('student-tests: %d cases against %s' % (len(TESTS), client.base))
|
||||
failures = []
|
||||
for func in TESTS:
|
||||
try:
|
||||
func(ctx)
|
||||
except (Failure, ValueError, OSError) as exc:
|
||||
failures.append(func.__name__)
|
||||
print('FAIL %-42s %s' % (func.__name__, exc))
|
||||
else:
|
||||
print('PASS %-42s' % func.__name__)
|
||||
print('%d passed, %d failed' % (len(TESTS) - len(failures), len(failures)))
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run inside API image: real SQL rollback, idempotency atomicity and index evidence."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, '/app/src')
|
||||
from messenger.cache import SessionCache
|
||||
from messenger.config import Config
|
||||
from messenger.db import Database
|
||||
from messenger.errors import InvalidRequest
|
||||
from messenger.security import hash_password
|
||||
from messenger.store import Store
|
||||
|
||||
|
||||
def main():
|
||||
config = Config.from_env()
|
||||
db = Database(config)
|
||||
db.open()
|
||||
store = Store(config, db, SessionCache(config))
|
||||
suffix = uuid.uuid4().hex[:10]
|
||||
actor = store.create_user('sql_' + suffix, 'SQL synthetic', hash_password('test-password-123'))
|
||||
try:
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT count(*) AS n FROM chats')
|
||||
before = cur.fetchone()['n']
|
||||
try:
|
||||
store.create_chat('Must rollback', [actor['id'], str(uuid.uuid4())])
|
||||
raise AssertionError('unknown member was accepted')
|
||||
except InvalidRequest:
|
||||
pass
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT count(*) AS n FROM chats')
|
||||
assert cur.fetchone()['n'] == before, 'partial chat survived rollback'
|
||||
print('PASS: actual multi-statement chat transaction rollback')
|
||||
chat = store.create_chat('SQL test', [actor['id']])
|
||||
function = 'test_reject_' + suffix
|
||||
key = 'rollback-' + suffix
|
||||
# Simulate failure between message INSERT and idempotency result INSERT.
|
||||
# Unique function/key isolate this synthetic test from every other request.
|
||||
with db.cursor() as cur:
|
||||
cur.execute("CREATE FUNCTION " + function + "() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN "
|
||||
"IF NEW.key = '" + key + "' THEN RAISE EXCEPTION 'synthetic rollback'; END IF; RETURN NEW; END $$")
|
||||
cur.execute('CREATE TRIGGER ' + function + ' BEFORE INSERT ON message_idempotency '
|
||||
'FOR EACH ROW EXECUTE FUNCTION ' + function + '()')
|
||||
try:
|
||||
try:
|
||||
store.add_message(chat['id'], actor['id'], 'must rollback', key)
|
||||
raise AssertionError('injected failure did not occur')
|
||||
except Exception as exc:
|
||||
assert 'synthetic rollback' in str(exc), type(exc).__name__
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT count(*) AS n FROM messages WHERE chat_id=%s', (chat['id'],))
|
||||
assert cur.fetchone()['n'] == 0, 'message committed without replay record'
|
||||
finally:
|
||||
with db.cursor() as cur:
|
||||
cur.execute('DROP TRIGGER ' + function + ' ON message_idempotency')
|
||||
cur.execute('DROP FUNCTION ' + function + '()')
|
||||
message = store.add_message(chat['id'], actor['id'], 'must rollback', key)
|
||||
assert store.add_message(chat['id'], actor['id'], 'must rollback', key) == message
|
||||
print('PASS: message/result atomic rollback; same key usable after transient failure')
|
||||
with db.cursor() as cur:
|
||||
cur.execute('INSERT INTO messages(id,chat_id,sender_id,text,created_at) '
|
||||
"SELECT gen_random_uuid(),%s,%s,'index-'||g, '2026-01-01'::timestamptz+g*interval '1 second' "
|
||||
'FROM generate_series(1,10000) AS g', (chat['id'], actor['id']))
|
||||
cur.execute('ANALYZE messages')
|
||||
cur.execute('EXPLAIN (ANALYZE, BUFFERS) SELECT id,chat_id,sender_id,text,created_at '
|
||||
'FROM messages WHERE chat_id=%s ORDER BY created_at,id LIMIT 51', (chat['id'],))
|
||||
plan = '\n'.join(row['QUERY PLAN'] for row in cur.fetchall())
|
||||
assert 'idx_messages_chat_created_id' in plan and 'Index Scan' in plan, plan
|
||||
print(plan)
|
||||
print('PASS: history index on at least 10000 messages')
|
||||
db.run_migrations()
|
||||
db.run_migrations()
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT name FROM schema_migrations ORDER BY name')
|
||||
assert [r['name'] for r in cur.fetchall()] == ['0001_init.sql', '0002_idempotency.sql']
|
||||
cur.execute('SELECT count(*) AS n FROM messages WHERE chat_id=%s', (chat['id'],))
|
||||
assert cur.fetchone()['n'] == 10001
|
||||
print('PASS: repeated versioned migrations preserved existing messages')
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Regression tests for password compatibility and HTTP framing/drain."""
|
||||
import hashlib
|
||||
import http.client
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src'))
|
||||
from messenger import security
|
||||
from messenger.config import Config
|
||||
from messenger.server import ApiServer
|
||||
from messenger.validation import require_idempotency_key
|
||||
from messenger.errors import InvalidRequest
|
||||
|
||||
|
||||
class IdempotencyValidationTests(unittest.TestCase):
|
||||
def test_key_format(self):
|
||||
for value in ('abc', 'A-Z_0.1:key', 'x' * 128):
|
||||
self.assertEqual(require_idempotency_key(value), value)
|
||||
for value in (None, '', 'x' * 129, 'a b', 'ключ', 'a\n'):
|
||||
with self.assertRaises(InvalidRequest):
|
||||
require_idempotency_key(value)
|
||||
|
||||
|
||||
class PasswordTests(unittest.TestCase):
|
||||
@unittest.skipUnless(hasattr(hashlib, 'scrypt'), 'host Python lacks scrypt; run in API image')
|
||||
def test_native_scrypt_is_used_and_can_be_verified(self):
|
||||
encoded = security.hash_password('test-password-123')
|
||||
self.assertTrue(encoded.startswith('scrypt:'), encoded.split(':')[0])
|
||||
self.assertTrue(security.verify_password('test-password-123', encoded))
|
||||
|
||||
def test_password_roundtrip(self):
|
||||
for password in ('test-password-123', 'Я' * 100):
|
||||
hashed = security.hash_password(password)
|
||||
self.assertTrue(security.verify_password(password, hashed))
|
||||
self.assertFalse(security.verify_password(password + 'x', hashed))
|
||||
|
||||
def test_bcrypt_hash_remains_readable_when_scrypt_available(self):
|
||||
import bcrypt
|
||||
encoded = 'bcrypt:' + bcrypt.hashpw(hashlib.sha256(b'test-password-123').digest(), bcrypt.gensalt(4)).decode()
|
||||
with patch.object(security, '_HAS_SCRYPT', True):
|
||||
self.assertTrue(security.verify_password('test-password-123', encoded))
|
||||
|
||||
|
||||
class HTTPTests(unittest.TestCase):
|
||||
def test_persistent_response_has_length_and_instance_id(self):
|
||||
class API:
|
||||
def dispatch(self, request):
|
||||
return 200, {'status': 'ok'}
|
||||
config = Config.from_env({'CURSOR_SECRET': 'shared'})
|
||||
server = ApiServer(('127.0.0.1', 0), API(), config, logging.getLogger('test'), None)
|
||||
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
||||
thread.start()
|
||||
try:
|
||||
conn = http.client.HTTPConnection(*server.server_address, timeout=1)
|
||||
conn.request('GET', '/health/live')
|
||||
response = conn.getresponse()
|
||||
self.assertIsNotNone(response.getheader('Content-Length'))
|
||||
instance = response.getheader('X-Instance-Id')
|
||||
self.assertTrue(instance)
|
||||
self.assertEqual(response.read(), b'{"status":"ok"}')
|
||||
conn.request('GET', '/health/live')
|
||||
response = conn.getresponse()
|
||||
self.assertEqual(response.getheader('X-Instance-Id'), instance)
|
||||
response.read()
|
||||
conn.close()
|
||||
finally:
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
thread.join()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user