From 07f848d18cfbd99efcb443a72c862f51d601d078 Mon Sep 17 00:00:00 2001 From: killoq7 Date: Wed, 4 Nov 2026 16:11:13 +0300 Subject: [PATCH] lab3 --- .env.example | 15 +- Dockerfile | 27 + Makefile | 34 +- REPORT.md | 143 +++- app.py | 496 +++++++++++ auth.py | 77 ++ compose.yaml | 126 +++ db.py | 338 ++++++++ evidence/README.md | 2 + evidence/failover-targets.json | 23 + evidence/rolling-restart-load.json | 17 + evidence/rolling-restart.json | 37 + evidence/rolling-update-load.json | 17 + evidence/rolling-update.json | 47 + evidence/track1/benchmark.json | 804 ++++++++++++++++++ evidence/track1/fixed-total-1-run-01.json | 17 + evidence/track1/fixed-total-1-run-02.json | 17 + evidence/track1/fixed-total-1-run-03.json | 17 + evidence/track1/fixed-total-2-run-01.json | 17 + evidence/track1/fixed-total-2-run-02.json | 17 + evidence/track1/fixed-total-2-run-03.json | 17 + evidence/track1/fixed-total-4-run-01.json | 17 + evidence/track1/fixed-total-4-run-02.json | 17 + evidence/track1/fixed-total-4-run-03.json | 17 + evidence/track1/growing-total-1-run-01.json | 17 + evidence/track1/growing-total-1-run-02.json | 17 + evidence/track1/growing-total-1-run-03.json | 17 + evidence/track1/growing-total-2-run-01.json | 17 + evidence/track1/growing-total-2-run-02.json | 17 + evidence/track1/growing-total-2-run-03.json | 17 + evidence/track1/growing-total-4-run-01.json | 17 + evidence/track1/growing-total-4-run-02.json | 17 + evidence/track1/growing-total-4-run-03.json | 17 + .../track1/high-concurrency/benchmark.json | 600 +++++++++++++ .../fixed-total-1-run-01.json | 17 + .../fixed-total-1-run-02.json | 17 + .../fixed-total-2-run-01.json | 17 + .../fixed-total-2-run-02.json | 17 + .../fixed-total-4-run-01.json | 17 + .../fixed-total-4-run-02.json | 17 + .../growing-total-1-run-01.json | 17 + .../growing-total-1-run-02.json | 17 + .../growing-total-2-run-01.json | 17 + .../growing-total-2-run-02.json | 17 + .../growing-total-4-run-01.json | 17 + .../growing-total-4-run-02.json | 17 + .../track1/high-concurrency/p95-latency.svg | 37 + .../high-concurrency/successful-rps.svg | 37 + evidence/track1/p95-latency.svg | 37 + evidence/track1/successful-rps.svg | 37 + infra/README.md | 31 +- infra/nginx.conf | 24 + migrations/001_schema.sql | 34 + migrations/002_message_idempotency.sql | 14 + requirements.txt | 3 + scripts/init-local-secrets.ps1 | 38 + scripts/migrate.py | 21 + scripts/restart.bat | 5 + scripts/restart.sh | 4 + scripts/rolling_restart.py | 141 +++ scripts/rolling_update.py | 241 ++++++ scripts/stop-one.bat | 21 + scripts/stop-one.sh | 16 + scripts/track1_benchmark.py | 524 ++++++++++++ sessions.py | 43 + student-tests/test_lab3.py | 83 ++ 66 files changed, 4660 insertions(+), 44 deletions(-) create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 auth.py create mode 100644 compose.yaml create mode 100644 db.py create mode 100644 evidence/failover-targets.json create mode 100644 evidence/rolling-restart-load.json create mode 100644 evidence/rolling-restart.json create mode 100644 evidence/rolling-update-load.json create mode 100644 evidence/rolling-update.json create mode 100644 evidence/track1/benchmark.json create mode 100644 evidence/track1/fixed-total-1-run-01.json create mode 100644 evidence/track1/fixed-total-1-run-02.json create mode 100644 evidence/track1/fixed-total-1-run-03.json create mode 100644 evidence/track1/fixed-total-2-run-01.json create mode 100644 evidence/track1/fixed-total-2-run-02.json create mode 100644 evidence/track1/fixed-total-2-run-03.json create mode 100644 evidence/track1/fixed-total-4-run-01.json create mode 100644 evidence/track1/fixed-total-4-run-02.json create mode 100644 evidence/track1/fixed-total-4-run-03.json create mode 100644 evidence/track1/growing-total-1-run-01.json create mode 100644 evidence/track1/growing-total-1-run-02.json create mode 100644 evidence/track1/growing-total-1-run-03.json create mode 100644 evidence/track1/growing-total-2-run-01.json create mode 100644 evidence/track1/growing-total-2-run-02.json create mode 100644 evidence/track1/growing-total-2-run-03.json create mode 100644 evidence/track1/growing-total-4-run-01.json create mode 100644 evidence/track1/growing-total-4-run-02.json create mode 100644 evidence/track1/growing-total-4-run-03.json create mode 100644 evidence/track1/high-concurrency/benchmark.json create mode 100644 evidence/track1/high-concurrency/fixed-total-1-run-01.json create mode 100644 evidence/track1/high-concurrency/fixed-total-1-run-02.json create mode 100644 evidence/track1/high-concurrency/fixed-total-2-run-01.json create mode 100644 evidence/track1/high-concurrency/fixed-total-2-run-02.json create mode 100644 evidence/track1/high-concurrency/fixed-total-4-run-01.json create mode 100644 evidence/track1/high-concurrency/fixed-total-4-run-02.json create mode 100644 evidence/track1/high-concurrency/growing-total-1-run-01.json create mode 100644 evidence/track1/high-concurrency/growing-total-1-run-02.json create mode 100644 evidence/track1/high-concurrency/growing-total-2-run-01.json create mode 100644 evidence/track1/high-concurrency/growing-total-2-run-02.json create mode 100644 evidence/track1/high-concurrency/growing-total-4-run-01.json create mode 100644 evidence/track1/high-concurrency/growing-total-4-run-02.json create mode 100644 evidence/track1/high-concurrency/p95-latency.svg create mode 100644 evidence/track1/high-concurrency/successful-rps.svg create mode 100644 evidence/track1/p95-latency.svg create mode 100644 evidence/track1/successful-rps.svg create mode 100644 infra/nginx.conf create mode 100644 migrations/001_schema.sql create mode 100644 migrations/002_message_idempotency.sql create mode 100644 requirements.txt create mode 100644 scripts/init-local-secrets.ps1 create mode 100644 scripts/migrate.py create mode 100644 scripts/restart.bat create mode 100644 scripts/restart.sh create mode 100644 scripts/rolling_restart.py create mode 100644 scripts/rolling_update.py create mode 100644 scripts/stop-one.bat create mode 100644 scripts/stop-one.sh create mode 100644 scripts/track1_benchmark.py create mode 100644 sessions.py create mode 100644 student-tests/test_lab3.py diff --git a/.env.example b/.env.example index d32b9be0..d2812077 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,8 @@ -# 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 +# Copy to .env for the LOCAL synthetic Compose environment; never commit .env. APP_PORT=8080 -APP_ENV=lab 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 +API_CPU_LIMIT=1.0 +API_MEMORY_LIMIT=512M +POSTGRES_CPU_LIMIT=1.0 +POSTGRES_MEMORY_LIMIT=1G diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..b7d4d6a0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.12.11-slim-bookworm + +ARG BUILD_REVISION=local +LABEL org.opencontainers.image.revision=$BUILD_REVISION + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 + +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app.py auth.py db.py sessions.py ./ +COPY migrations ./migrations +COPY scripts/migrate.py ./scripts/migrate.py +COPY contracts/openapi.json ./contracts/openapi.json +RUN useradd --uid 10001 --create-home --shell /usr/sbin/nologin appuser \ + && chown -R 10001:10001 /app +USER 10001:10001 + +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" || exit 1 + +STOPSIGNAL SIGTERM +CMD ["python", "app.py"] diff --git a/Makefile b/Makefile index 5d86d171..0a5be5eb 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,48 @@ +ifeq ($(OS),Windows_NT) +PYTHON ?= py -3 +else PYTHON ?= python3 +endif +PYTHONFLAGS ?= -X utf8 BASE_URL ?= http://localhost:8080 +export BASE_URL CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence test-failover +.PHONY: help check validate test student-test load track1 test-rolling test-update test-persistence test-failover help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: full OpenAPI; test/student-test: running API; load/track1: synthetic writes and scaling experiment" check: - $(PYTHON) scripts/check_contract.py + $(PYTHON) $(PYTHONFLAGS) scripts/check_contract.py validate: - $(PYTHON) scripts/validate_openapi.py + $(PYTHON) $(PYTHONFLAGS) scripts/validate_openapi.py test: - $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) $(PYTHONFLAGS) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +student-test: + $(PYTHON) $(PYTHONFLAGS) -m unittest discover -s student-tests -p "test_*.py" load: - $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) $(PYTHONFLAGS) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) + +track1: + BASE_URL="$(BASE_URL)" $(PYTHON) $(PYTHONFLAGS) scripts/track1_benchmark.py + +test-rolling: + $(PYTHON) $(PYTHONFLAGS) scripts/rolling_restart.py --base-url "$(BASE_URL)" + +test-update: + $(PYTHON) $(PYTHONFLAGS) scripts/rolling_update.py --base-url "$(BASE_URL)" test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) - $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + $(PYTHON) $(PYTHONFLAGS) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) 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) + $(PYTHON) $(PYTHONFLAGS) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) diff --git a/REPORT.md b/REPORT.md index c2679005..93ac7b07 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,136 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Журавлев Алексей, БПИ2301 +- Целевая оценка: 5; трек 1 — цена масштабирования +- Commit/PR: +- Дата проверок: 2026-11-04 +- Источники и ИИ: собственные исходники lab2; skeleton и контракт lab3; официальная документация Docker Compose, Nginx, PostgreSQL и Redis. Использовалась помощь ИИ при разработке эксперимента и проверке ошибок; результаты ниже получены командами проекта. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +### Среда и локальные файлы -## Архитектура и решения +- Windows 11, Docker Desktop с Docker Compose v2; текущая проверенная версия Docker Engine — 29.8.2. +- Docker host сообщает 32 CPU и 7539.719 MiB RAM; эти значения относятся к Docker Desktop, а не к физической памяти/CPU ПК. +- Python 3.9+ для публичных тестов; проверка выполнена Python 3.14.6. В PowerShell используйте `py -3 -X utf8`, чтобы Python на Windows читал UTF-8 JSON/контракт независимо от системной кодировки. Makefile добавляет `-X utf8` автоматически. +- Образы закреплены версиями: `python:3.12.11-slim-bookworm`, `postgres:16.8-alpine`, `redis:7.4.2-alpine`, `nginx:1.27.5-alpine`. API собирается локально из `Dockerfile`. +- GNU Make необязателен. Порты наружу: только `${APP_PORT:-8080}` для Nginx. -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +В PowerShell из каталога проекта: -## Доказательства критериев +```powershell +Set-Location 'C:\VMD3\mtusi2026lab3' +.\scripts\init-local-secrets.ps1 +docker compose config --quiet +docker compose up -d --build +Invoke-RestMethod http://localhost:8080/health/ready +make check +make validate +make test +make student-test +``` -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +`init-local-secrets.ps1` копирует `.env.example` в игнорируемый `.env`, если его ещё нет, и создаёт `secrets/postgres_password` и `secrets/database_url` с криптографически случайным паролем. Скрипт не печатает значения. Если оба файла уже есть, он не меняет их; если набор неполный — завершится ошибкой, чтобы случайно не рассинхронизировать пароль и volume. Не публикуйте `.env` или `secrets/`. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Ожидаемая топология после готовности: два `api` контейнера и по одному `nginx`, `postgres`, `redis`. Проверить её и состояние healthchecks: -## Эксперименты и отказы +```powershell +docker compose ps +docker compose logs --tail 100 migrate api nginx +``` -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +`migrate` — однократный job: ждёт PostgreSQL healthcheck, применяет упорядоченные SQL-файлы, завершает работу. Каждый запуск безопасно проверяет уже применённые версии; API-контейнеры не мигрируют схему параллельно при старте. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +### Проверки отказа и последовательной замены -## Ограничения и следующие шаги +Windows-вариант hook-файлов запускается проверкой напрямую: -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +```powershell +py -3 -X utf8 tests\resilience.py --mode persistence --action-script scripts\restart.bat --base-url http://localhost:8080 +py -3 -X utf8 tests\resilience.py --mode failover --action-script scripts\stop-one.bat --base-url http://localhost:8080 +docker compose up -d --scale api=2 +py -3 -X utf8 tests\resilience.py --mode failover --action-script scripts\stop-one.bat --base-url http://localhost:8080 +docker compose up -d --scale api=2 +py -3 -X utf8 scripts\rolling_restart.py --base-url http://localhost:8080 --duration 40 --concurrency 16 +py -3 -X utf8 scripts\rolling_update.py --base-url http://localhost:8080 --duration 90 --concurrency 16 +``` + +Failover hook оставляет выбранную реплику остановленной; после каждого прогона оператор восстанавливает её командой `docker compose up -d --scale api=2`. POSIX-варианты `scripts/restart.sh` и `scripts/stop-one.sh` предназначены для Linux/macOS; перед запуском задайте им executable bit. Rolling restart последовательно перезапускает каждый работающий API-контейнер во время нагрузки и ждёт его healthcheck. Это подтверждает поведение процесса/балансировщика, но не заменяет отдельный тест деплоя нового бинарного образа. + +### Выключение и очистка + +```powershell +docker compose down +``` + +Команда останавливает контейнеры, но оставляет volumes с учебными данными. Только для намеренного полного сброса этого учебного проекта: + +```powershell +docker compose down -v +``` + +Удаление volumes необратимо стирает пользователей, чаты, сообщения, сессии и AOF Redis. Секреты в `secrets/` удаляются отдельно после остановки проекта. + +## Архитектура + +```text +Клиент + │ единственный опубликованный порт 8080 + ▼ +Nginx 1.27.5 ── DNS discovery / passive fail exclusion + │ + ├── API × 2+ (stateless; один общий image) + │ ├── PostgreSQL 16.8: пользователи, чаты, членство, сообщения, idempotency results + │ └── Redis 7.4.2: opaque sessions с TTL и AOF + └── общий Compose backend network (наружу не опубликована) +``` + +| Роль | Service | Версия / readiness | Volume, сеть, порт | Credentials | +| --- | --- | --- | --- | --- | +| API | `api` | Собирается из Python 3.12.11 image; `/health/ready`, каждые 3 s | Backend only; порт 8080 не публикуется | Только `database_url` Compose secret; `CACHE_URL` указывает на внутренний Redis | +| Load balancer | `nginx` | `nginx:1.27.5-alpine`; собственный readiness через API | Frontend + backend; host port 8080 | Не нужны | +| DB | `postgres` | `postgres:16.8-alpine`; `pg_isready` каждые 5 s | Named `postgres_data`, backend only | Только `postgres_password` Compose secret | +| Session store | `redis` | `redis:7.4.2-alpine`; `PING` каждые 5 s | Named `redis_data`, backend only; AOF `everysec` | Без пароля только внутри изолированной сети | +| Миграции | `migrate` | Тот же локально собранный API image; однократный exit | Backend only, volume не нужен | Только `database_url` Compose secret | + +У `api` нет `container_name`, используются динамические Compose DNS-имена и `deploy.replicas`; масштабирование выполняется `docker compose up -d --scale api=N`. Nginx OSS периодически обновляет DNS через Docker DNS (`valid=5s`) и использует passive health: при одной ошибке соединения/таймауте или ответе 502/503/504 backend исключается на 2 s (`max_fails=1`, `fail_timeout=2s`). Это не активная проверка каждого backend коммерческим Nginx Plus. Docker healthcheck проверяет готовность экземпляра отдельно. Максимум наблюдаемого восстановления при остановке API в тестах был меньше секунды; при DNS-кэше/таймауте консервативная верхняя граница настройки — около 7 s, не включая длительный сбой зависимости. + +В сессиях нет локального состояния API: opaque токен сопоставлен с пользователем в Redis через TTL; Redis AOF и volume переживают перезапуск. Источник истины бизнес-данных — PostgreSQL. `migrations/002_message_idempotency.sql` добавляет уникальную область `(user, method, chat path, key)`, hash запроса, тело исходного ответа и expiry 24 часа. Создание записи и сообщения происходит в одной транзакции. `pg_advisory_xact_lock` сериализует параллельные повторы одного ключа; другой body возвращает 409, совпадающий — прежний Message/201. Транзиентный сбой транзакции не сохраняет результат и не оставляет сообщение-дубль. Старые истёкшие ключи для пользователя очищаются при следующей записи. + +Перенесены из lab2 собственные `app.py`, `auth.py`, `db.py`, `sessions.py`, схема PostgreSQL и библиотеки Argon2/psycopg/Redis. Контрактные `tests/`, `contracts/`, README и миграции skeleton lab3 не заменялись. В lab3 добавлены общий idempotency store, X-Instance-Id, DNS-балансировка, отдельный migration job, failover и масштабный эксперимент. + +## Проверки и фактические результаты + +| Критерий | Команда / доказательство | Наблюдаемый результат | +| --- | --- | --- | +| Offline contract / syntax | `make check` | PASS | +| Полная OpenAPI 3.1 / JSON Schema | `make validate` | PASS | +| Публичный сценарий API | `make test` | PASS: lab3 smoke, включая обязательный ключ и replay | +| Конкурентный idempotency на нескольких репликах | `make student-test` | 4 теста OK: replay, conflict при ином body, обязательность/формат ключа, 16 параллельных повторов; один Message, минимум 2 X-Instance-Id | +| PostgreSQL migration/bootstrap | `docker compose up -d --build`; `docker compose exec -T postgres psql -U messenger -d messenger -c "SELECT version FROM schema_migrations ORDER BY version"` | PASS: `001_schema.sql`, `002_message_idempotency.sql`; `migrate` завершился до запуска API | +| Сохранность при полном restart | `tests/resilience.py --mode persistence --action-script scripts/restart.bat` | PASS: подтверждённое сообщение и прежний Bearer токен доступны после restart API, PostgreSQL и Redis с сохранёнными volumes | +| Отказ одной API-реплики | `tests/resilience.py --mode failover --action-script scripts/stop-one.bat` | Два успешных resilience-прогона: восстановление за 0.634 s и 0.613 s, 0/1 переходных ошибки. Дополнительно обе конкретные реплики поочерёдно адресно остановлены: восстановление записи за 1.094/1.092 s, без ошибок; существующая сессия сохранялась, replay вернул тот же Message, дублей нет. `evidence/failover-targets.json` | +| Последовательный restart под нагрузкой | `scripts/rolling_restart.py --duration 40 --concurrency 16` | Две реплики последовательно восстановили health за 6.126 s и 6.148 s. За 40.161 s подтверждено 7054 POST (175.644 successful RPS), 2 ошибки из-за перехода (0.0283%); успешные p50/p95/p99 — 104.831/196.305/278.076 ms. Файлы `evidence/rolling-restart.json` и `evidence/rolling-restart-load.json` | +| Замена контейнеров новым API image под нагрузкой | `scripts/rolling_update.py --duration 90 --concurrency 16` | PASS: revision `lab3-4ef7756dc6dc`; каждый из двух новых контейнеров стал healthy и получил запрос через Nginx за 7.035/7.043 s. За 90.142 s нагрузки — 16,479 успешных POST, 13 ошибок (0.0788%), p50/p95/p99 успешных запросов — 45.288/195.030/285.894 ms. В конце Compose восстановил 2 здоровых API; `evidence/rolling-update.json` | +| Цена масштабирования, трек 1 | `scripts/track1_benchmark.py --duration 30 --warmup 5 --concurrency 16 --repeats 3` | PASS: все 6 конфигураций, 3 повтора. При фиксированных 4 CPU/2048 MiB: 1/2/4 API дали 161.966/293.019/346.607 успешных RPS; подробности ниже и в `evidence/track1/benchmark.json` | +| Saturation check, concurrency 64 | `scripts/track1_benchmark.py --duration 20 --warmup 5 --concurrency 64 --repeats 2 --output evidence/track1/high-concurrency/benchmark.json` | PASS: шесть конфигураций, по два повтора, ошибок нет. При фиксированном API-бюджете 4 CPU throughput 2→4 вырос только на 9.2%, API достиг пикового агрегированного CPU 376.265%; детали в отдельном JSON | + +## Трек 1 — гипотеза и методика + +**Гипотеза:** при одном и том же общем CPU/RAM увеличение числа API с 1 до 2 может повысить успешный HTTP throughput за счёт параллельного обслуживания запросов, но затем эффект ограничат общий PostgreSQL/однопоточные ресурсы LB или пул соединений. При росте суммарных ресурсов сравниваются те же профили; увеличение API-процессов не должно скрывать фиксированные ограничения PostgreSQL/Nginx. + +`scripts/track1_benchmark.py` выполняет 6 конфигураций: 1/2/4 API при фиксированных 4 CPU и 2048 MiB суммарных лимитов API, затем 1/2/4 API с ростом по 1 CPU и 512 MiB на реплику. На конфигурацию — 5 s прогрева, 3 × 30 s, 16 closed-loop workers; перед тестом сверяет число реально наблюдаемых instance IDs и ждёт обновления DNS-кэша Nginx при необходимости. Между конфигурациями меняется только лимит/число API, PostgreSQL и Nginx остаются с фиксированными лимитами Compose. `docker stats` снимается раз в секунду для API/PostgreSQL/Nginx; JSON включает RPS, ошибки, успешные p50/p95/p99 и пиковые CPU/RAM-сэмплы. После эксперимента Compose возвращается к двум API-репликам. + +Фиксированный бюджет API, 4 CPU/2048 MiB: 1→2 реплики повысили throughput с 161.966 до 293.019 RPS (+80.9%), а 2→4 — до 346.607 RPS (+18.3%); p95 для 1/2/4 составил 121.277/103.484/145.922 ms. Для растущего бюджета 1/2/4 CPU и 512/1024/2048 MiB получено 91.964/178.185/346.725 RPS, без ошибок в трёх повторах каждой конфигурации. Четыре реплики при одинаковом суммарном лимите дали практически одинаковый throughput в обеих сериях (346.607/346.725 RPS). + +В основной серии была одна переходная ошибка в суммарно однорепличной конфигурации с лимитом 4 CPU (средняя доля 0.0068%); в остальных пяти конфигурациях ошибок не было. + +При concurrency=16 насыщение ресурсов не подтверждено: пиковый агрегированный CPU API для фиксированных 4 CPU составил 250.420% при четырёх репликах, PostgreSQL — 67.920%, Nginx — 18.280%. При concurrency=64 throughput для фиксированного бюджета составил 155.580/284.255/310.259 RPS для 1/2/4 реплик; переход 2→4 дал лишь +9.2%, p95 вырос с 274.215 до 458.187 ms, а пиковый CPU API достиг 376.265% из 400% доступных по лимиту. Это указывает на насыщение CPU API и низкую отдачу дальнейшего масштабирования в данной нагрузке; PostgreSQL достиг пика 74.370%, поэтому его насыщение не установлено. Nginx достиг 16.975%. Ошибок в supplemental серии не было. Лимиты памяти API не были близки к исчерпанию. Основные графики находятся в `evidence/track1/`, supplemental-графики и 12 исходных повторов — в `evidence/track1/high-concurrency/`. + +Скрипт нагрузки `tests/load.py` не является open-loop генератором и измеряет HTTP-подтверждения, не SQL COMMIT; он подвержен coordinated omission и overhead клиента. Сэмплирование `docker stats` может пропускать короткие пики. Если серия не достигает saturation, это будет указано как ограничение, а не будет выдуман предел БД. Исходные повторы и автоматически построенные SVG-графики являются частью evidence. + +## Отказы, улучшение и ограничения + +Измеренный failover относится только к процессу API при работающих Nginx, PostgreSQL и Redis на одном Docker host. PostgreSQL, Redis, Nginx, Docker host и host ОС остаются SPOF; их HA и потеря узла не заявляются. Nginx OSS применяет пассивное исключение, поэтому отдельный readiness probe каждого upstream не выполняется активно. Restart Redis с AOF/volume проверен, но это не кластерная репликация и не гарантия потери менее одной секунды при любой аварии. + +Проверенный функциональный результат — общий транзакционный ключ идемпотентности: 16 одновременных повторов через балансировщик дают один и тот же Message, несмотря на распределение между экземплярами. Обе API-реплики адресно проверены как цели отказа; подтверждения и действующая сессия сохранились. Под нагрузкой при поочерёдном рестарте сервера подтверждения продолжались; переходные ошибки учтены, а не скрыты. Основной трек и supplemental saturation check завершены; вывод о насыщении ограничен API CPU при concurrency=64, а не переносится на PostgreSQL или весь стенд. diff --git a/app.py b/app.py new file mode 100644 index 00000000..b16e42ac --- /dev/null +++ b/app.py @@ -0,0 +1,496 @@ +#!/usr/bin/env python3 + +import base64 +import binascii +import json +import logging +import os +import re +import signal +import socket +import sys +import threading +import uuid +from datetime import datetime, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from urllib.parse import parse_qs, urlsplit + +from auth import ( + create_session, + hash_password, + now_iso, + parse_basic_auth, + parse_bearer_token, + validate_password, + verify_password, +) +from db import ApiError, Store +from sessions import SessionStore + +MAX_BODY_BYTES = 1024 * 1024 +USERNAME_PATTERN = re.compile(r'^[a-z0-9_]{3,32}$') +CURSOR_TIMESTAMP_PATTERN = re.compile( + r'^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?Z$' +) +IDEMPOTENCY_KEY_PATTERN = re.compile(r'^[A-Za-z0-9._:-]{1,128}$') +LOG = logging.getLogger('messenger') +OPENAPI_PATH = Path(__file__).resolve().parent / 'contracts' / 'openapi.json' +SWAGGER_HTML = b''' + + + + + MTUSI Messenger API + + + +
+ + + + +''' + + +def cursor_encode(resource, item): + value = json.dumps( + {'resource': resource, 'created_at': item['created_at'], 'id': item['id']}, + separators=(',', ':'), + ).encode('utf-8') + return base64.urlsafe_b64encode(value).rstrip(b'=').decode('ascii') + + +def cursor_decode(cursor, resource): + try: + padding = '=' * (-len(cursor) % 4) + decoded = base64.b64decode(cursor + padding, altchars=b'-_', validate=True) + value = json.loads(decoded.decode('utf-8')) + except (ValueError, UnicodeError, binascii.Error): + raise ApiError(400, 'invalid_request', 'Invalid cursor') from None + if (not isinstance(value, dict) or set(value) != {'resource', 'created_at', 'id'} + or value.get('resource') != resource + or not isinstance(value.get('created_at'), str) + or not CURSOR_TIMESTAMP_PATTERN.fullmatch(value['created_at']) + or not isinstance(value.get('id'), str)): + raise ApiError(400, 'invalid_request', 'Invalid cursor') + try: + created_at = datetime.fromisoformat(value['created_at'][:-1] + '+00:00') + item_id = str(uuid.UUID(value['id'])) + except (ValueError, AttributeError): + raise ApiError(400, 'invalid_request', 'Invalid cursor') from None + if item_id != value['id']: + raise ApiError(400, 'invalid_request', 'Invalid cursor') + return created_at, item_id + + +def required_text(value, name, maximum): + if not isinstance(value, str) or not value.strip() or len(value) > maximum: + raise ApiError(400, 'invalid_request', 'Invalid ' + name) + try: + value.encode('utf-8') + except UnicodeEncodeError: + raise ApiError(400, 'invalid_request', 'Invalid ' + name) from None + return value + + +def parse_uuid(value): + if not isinstance(value, str): + raise ApiError(400, 'invalid_request', 'Invalid UUID') + try: + parsed = uuid.UUID(value) + except (ValueError, AttributeError): + raise ApiError(400, 'invalid_request', 'Invalid UUID') from None + if str(parsed) != value.lower(): + raise ApiError(400, 'invalid_request', 'Invalid UUID') + return str(parsed) + + +class MessengerHandler(BaseHTTPRequestHandler): + server_version = 'MTUSI-Messenger-Lab3' + protocol_version = 'HTTP/1.1' + + def log_message(self, _format, *args): + LOG.info('request method=%s status=%s request_id=%s', self.command, self._status, self._request_id) + + def _send_bytes(self, status, request_id, body, content_type, extra_headers=None): + self._status = status + self.send_response(status) + self.send_header('X-Request-Id', request_id) + self.send_header('Content-Length', str(len(body))) + if status != 204: + self.send_header('Content-Type', content_type) + for name, value in (extra_headers or {}).items(): + self.send_header(name, value) + self.send_header('X-Instance-Id', self.server.instance_id) + self.end_headers() + if body: + self.wfile.write(body) + + def _send(self, status, request_id, payload=None, extra_headers=None): + body = b'' if status == 204 else json.dumps( + payload, ensure_ascii=False, separators=(',', ':') + ).encode('utf-8') + self._send_bytes( + status, request_id, body, 'application/json; charset=utf-8', extra_headers + ) + + def _error(self, request_id, error, extra_headers=None): + self._send(error.status, request_id, { + 'error': { + 'code': error.code, + 'message': error.message, + 'request_id': request_id, + }, + }, extra_headers=extra_headers) + + def _read_json(self): + if self.headers.get_content_type() != 'application/json': + raise ApiError(415, 'unsupported_media_type', 'Expected application/json') + raw_length = self.headers.get('Content-Length') + try: + length = int(raw_length) + except (TypeError, ValueError): + raise ApiError(400, 'invalid_request', 'Invalid Content-Length') from None + if length < 0: + raise ApiError(400, 'invalid_request', 'Invalid Content-Length') + if length > MAX_BODY_BYTES: + raise ApiError(413, 'payload_too_large', 'Request body is too large') + try: + value = json.loads(self.rfile.read(length).decode('utf-8')) + except (ValueError, UnicodeError): + raise ApiError(400, 'invalid_request', 'Malformed JSON') from None + if not isinstance(value, dict): + raise ApiError(400, 'invalid_request', 'Expected a JSON object') + return value + + def _dependencies_ok(self): + try: + self.server.store.ping() + self.server.sessions.ping() + return True + except Exception: + LOG.exception('dependency check failed') + return False + + def _unauthorized_bearer(self, request_id): + self._error( + request_id, + ApiError(401, 'unauthorized', 'Authentication required'), + extra_headers={'WWW-Authenticate': 'Bearer realm="messenger"'}, + ) + + def _unauthorized_basic(self, request_id): + self._error( + request_id, + ApiError(401, 'unauthorized', 'Authentication required'), + extra_headers={'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'}, + ) + + def _authenticated_user(self, request_id): + auth = self.headers.get('Authorization', '') + if auth.startswith('Basic '): + self._unauthorized_bearer(request_id) + return None + token = parse_bearer_token(auth) + if not token: + self._unauthorized_bearer(request_id) + return None + try: + user_id = self.server.sessions.get_user_id(token) + except Exception: + LOG.exception('session lookup failed') + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return None + if not user_id: + self._unauthorized_bearer(request_id) + return None + try: + user = self.server.store.get_user(user_id) + except Exception: + LOG.exception('user lookup failed') + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return None + if user is None: + self._unauthorized_bearer(request_id) + return None + user['_token'] = token + return user + + @staticmethod + def _query(query): + try: + values = parse_qs(query, keep_blank_values=True, strict_parsing=True) + except ValueError: + raise ApiError(400, 'invalid_request', 'Invalid query parameters') from None + if set(values) - {'limit', 'cursor'} or any(len(items) != 1 for items in values.values()): + raise ApiError(400, 'invalid_request', 'Invalid query parameters') + try: + limit = int(values.get('limit', ['50'])[0]) + except ValueError: + raise ApiError(400, 'invalid_request', 'Invalid limit') from None + if not 1 <= limit <= 100: + raise ApiError(400, 'invalid_request', 'Invalid limit') + cursor = values.get('cursor', [''])[0] + if ('cursor' in values and not cursor) or len(cursor) > 2048: + raise ApiError(400, 'invalid_request', 'Invalid cursor') + return limit, cursor + + def _create_session(self, request_id): + credentials = parse_basic_auth(self.headers.get('Authorization')) + if not credentials: + self._unauthorized_basic(request_id) + return + username, password = credentials + if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username): + self._unauthorized_basic(request_id) + return + if not validate_password(password): + self._unauthorized_basic(request_id) + return + try: + row = self.server.store.get_user_with_password(username) + except Exception: + LOG.exception('login lookup failed') + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return + if row is None or not verify_password(row['password_hash'], password): + self._unauthorized_basic(request_id) + return + user = self.server.store.get_user(str(row['id'])) + try: + token, expires_at = create_session(self.server.sessions, user['id']) + except Exception: + LOG.exception('session create failed') + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return + self._send(201, request_id, { + 'token': token, + 'token_type': 'Bearer', + 'expires_at': expires_at, + 'user': user, + }) + + def _logout(self, request_id, user): + try: + self.server.sessions.revoke(user['_token']) + except Exception: + LOG.exception('session revoke failed') + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return + self._send_bytes(204, request_id, b'', 'application/json; charset=utf-8') + + def _register(self, request_id, body): + if set(body) != {'username', 'display_name', 'password'}: + raise ApiError(400, 'invalid_request', 'Invalid user fields') + username = body['username'] + if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username): + raise ApiError(400, 'invalid_request', 'Invalid username') + display_name = required_text(body['display_name'], 'display_name', 100) + password = body['password'] + if not validate_password(password): + raise ApiError(400, 'invalid_request', 'Invalid password') + created_at = now_iso() + password_hash = hash_password(password) + user = self.server.store.register_user(username, display_name, password_hash, created_at) + self._send(201, request_id, user) + + def _dispatch(self): + request_id = str(uuid.uuid4()) + self._request_id = request_id + self._status = 500 + try: + if self.server.stopping: + raise ApiError(503, 'unavailable', 'Server is shutting down') + route = urlsplit(self.path) + path = route.path + if self.command == 'GET' and path in ('/docs', '/docs/'): + self._send_bytes(200, request_id, SWAGGER_HTML, 'text/html; charset=utf-8') + return + if self.command == 'GET' and path == '/openapi.json': + self._send_bytes( + 200, request_id, self.server.openapi_json, 'application/json; charset=utf-8' + ) + return + if path == '/health/live' and self.command == 'GET': + self._send(200, request_id, {'status': 'ok'}) + return + if path == '/health/ready' and self.command == 'GET': + if self._dependencies_ok(): + self._send(200, request_id, {'status': 'ok'}) + else: + self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) + return + if path == '/api/v1/users' and self.command == 'POST': + self._register(request_id, self._read_json()) + return + if path == '/api/v1/auth/sessions' and self.command == 'POST': + self._create_session(request_id) + return + if path.startswith('/api/v1/'): + user = self._authenticated_user(request_id) + if user is None: + return + user_id = user['id'] + if path == '/api/v1/auth/sessions/current' and self.command == 'DELETE': + self._logout(request_id, user) + return + if path == '/api/v1/users/me' and self.command == 'GET': + self._send(200, request_id, { + k: user[k] for k in ('id', 'username', 'display_name', 'created_at') + }) + return + if path == '/api/v1/chats' and self.command == 'POST': + body = self._read_json() + if set(body) != {'title', 'member_ids'}: + raise ApiError(400, 'invalid_request', 'Invalid chat fields') + title = required_text(body['title'], 'title', 100) + member_ids = body['member_ids'] + if (not isinstance(member_ids, list) or not member_ids or len(member_ids) > 100 + or any(not isinstance(value, str) for value in member_ids)): + raise ApiError(400, 'invalid_request', 'Invalid chat members') + normalized = [parse_uuid(value) for value in member_ids] + if len(set(normalized)) != len(normalized) or user_id not in normalized: + raise ApiError(400, 'invalid_request', 'Invalid chat members') + chat = self.server.store.create_chat( + user_id, title, normalized, now_iso() + ) + self._send(201, request_id, chat) + return + if path == '/api/v1/chats' and self.command == 'GET': + limit, cursor = self._query(route.query) + key = cursor_decode(cursor, 'chats') if cursor else None + items, next_key = self.server.store.list_chats(user_id, limit, key) + next_cursor = cursor_encode('chats', items[-1]) if next_key else None + self._send(200, request_id, {'items': items, 'next_cursor': next_cursor}) + return + match = re.fullmatch(r'/api/v1/chats/([^/]+)', path) + if match and self.command == 'GET': + chat_id = parse_uuid(match.group(1)) + self._send(200, request_id, self.server.store.get_chat(user_id, chat_id)) + return + match = re.fullmatch(r'/api/v1/chats/([^/]+)/messages', path) + if match: + chat_id = parse_uuid(match.group(1)) + resource = 'messages:' + chat_id + if self.command == 'POST': + body = self._read_json() + if not isinstance(body, dict) or set(body) != {'text'}: + raise ApiError(400, 'invalid_request', 'Invalid message fields') + idempotency_key = self.headers.get('Idempotency-Key', '') + if not IDEMPOTENCY_KEY_PATTERN.fullmatch(idempotency_key): + raise ApiError(400, 'invalid_request', 'Invalid Idempotency-Key') + text = body['text'] + if not isinstance(text, str) or len(text) > 4000 or not text.strip(): + raise ApiError(400, 'invalid_request', 'Message text must not be empty') + try: + text.encode('utf-8') + except UnicodeEncodeError: + raise ApiError(400, 'invalid_request', 'Invalid message text') from None + message = self.server.store.create_message_idempotent( + user_id, chat_id, text, idempotency_key, now_iso() + ) + self._send(201, request_id, message) + return + if self.command == 'GET': + limit, cursor = self._query(route.query) + key = cursor_decode(cursor, resource) if cursor else None + items, next_key = self.server.store.list_messages(user_id, chat_id, limit, key) + next_cursor = cursor_encode(resource, items[-1]) if next_key else None + self._send(200, request_id, {'items': items, 'next_cursor': next_cursor}) + return + raise ApiError(404, 'not_found', 'Resource not found') + except ApiError as error: + self._error(request_id, error) + except (BrokenPipeError, ConnectionResetError): + LOG.info('request disconnected request_id=%s', request_id) + except Exception: + LOG.exception('request failed request_id=%s', request_id) + self._error(request_id, ApiError(500, 'internal_error', 'Internal server error')) + + def do_GET(self): + self._dispatch() + + def do_POST(self): + self._dispatch() + + def do_PUT(self): + self._dispatch() + + def do_PATCH(self): + self._dispatch() + + def do_DELETE(self): + self._dispatch() + + def do_OPTIONS(self): + self._dispatch() + + +class MessengerServer(ThreadingHTTPServer): + daemon_threads = False + block_on_close = True + request_queue_size = 128 + + def __init__(self, address, store, sessions): + super().__init__(address, MessengerHandler) + self.store = store + self.sessions = sessions + self.stopping = False + self.instance_id = os.environ.get('INSTANCE_ID') or socket.gethostname() + try: + spec = json.loads(OPENAPI_PATH.read_text(encoding='utf-8')) + spec['servers'] = [{'url': '/', 'description': 'Текущий адрес API'}] + self.openapi_json = json.dumps( + spec, ensure_ascii=False, separators=(',', ':') + ).encode('utf-8') + except (OSError, ValueError) as error: + self.server_close() + raise RuntimeError('Unable to load the OpenAPI contract') from error + + +def main(): + host = os.environ.get('APP_HOST', '0.0.0.0') + try: + port = int(os.environ.get('APP_PORT', '8080')) + except ValueError: + raise SystemExit('APP_PORT must be an integer') + if not 1 <= port <= 65535: + raise SystemExit('APP_PORT must be between 1 and 65535') + if os.environ.get('SESSION_STORE', 'redis').lower() != 'redis': + raise SystemExit('SESSION_STORE must be redis for this implementation') + + logging.basicConfig( + level=os.environ.get('LOG_LEVEL', 'INFO').upper(), + format='%(asctime)s %(levelname)s %(name)s %(message)s', + stream=sys.stdout, + ) + store = Store.connect(run_migrations=False) + sessions = SessionStore.from_env() + server = MessengerServer((host, port), store, sessions) + + def stop(_signum, _frame): + server.stopping = True + threading.Thread(target=server.shutdown, name='http-shutdown', daemon=True).start() + + signal.signal(signal.SIGTERM, stop) + signal.signal(signal.SIGINT, stop) + LOG.info('server started host=%s port=%d', host, port) + try: + server.serve_forever(poll_interval=0.2) + finally: + server.server_close() + store.close() + LOG.info('server stopped') + + +if __name__ == '__main__': + main() diff --git a/auth.py b/auth.py new file mode 100644 index 00000000..df4bee95 --- /dev/null +++ b/auth.py @@ -0,0 +1,77 @@ +import base64 +import binascii +import os +import secrets +from datetime import datetime, timedelta, timezone + +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError + +from sessions import SessionStore + +PASSWORD_MIN = 12 +PASSWORD_MAX = 128 +_hasher = PasswordHasher() + + +def now_iso(): + return datetime.now(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z') + + +def validate_password(password): + if not isinstance(password, str): + return False + length = len(password) + return PASSWORD_MIN <= length <= PASSWORD_MAX + + +def hash_password(password): + return _hasher.hash(password) + + +def verify_password(password_hash, password): + try: + return _hasher.verify(password_hash, password) + except VerifyMismatchError: + return False + + +def parse_basic_auth(header_value): + if not header_value or not header_value.startswith('Basic '): + return None + encoded = header_value[6:].strip() + try: + decoded = base64.b64decode(encoded, validate=True).decode('utf-8') + except (ValueError, UnicodeError, binascii.Error): + return None + if ':' not in decoded: + return None + username, password = decoded.split(':', 1) + if not username: + return None + return username, password + + +def parse_bearer_token(header_value): + if not header_value or not header_value.startswith('Bearer '): + return None + token = header_value[7:].strip() + return token or None + + +def session_ttl_seconds(): + try: + ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600')) + except ValueError: + ttl = 3600 + return max(1, ttl) + + +def create_session(store: SessionStore, user_id): + token = secrets.token_urlsafe(32) + ttl = session_ttl_seconds() + expires_at = ( + datetime.now(timezone.utc) + timedelta(seconds=ttl) + ).isoformat(timespec='microseconds').replace('+00:00', 'Z') + store.create(token, user_id, ttl) + return token, expires_at diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..d9718e73 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,126 @@ +services: + postgres: + image: postgres:16.8-alpine + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + networks: + - backend + healthcheck: + test: ['CMD-SHELL', 'pg_isready -U messenger -d messenger'] + interval: 5s + timeout: 3s + retries: 12 + start_period: 5s + restart: unless-stopped + deploy: + resources: + limits: + cpus: '${POSTGRES_CPU_LIMIT:-1.0}' + memory: '${POSTGRES_MEMORY_LIMIT:-1G}' + + redis: + image: redis:7.4.2-alpine + command: ['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec'] + volumes: + - redis_data:/data + networks: + - backend + healthcheck: + test: ['CMD', 'redis-cli', 'ping'] + interval: 5s + timeout: 3s + retries: 12 + restart: unless-stopped + + migrate: + build: . + command: ['python', 'scripts/migrate.py'] + environment: + DATABASE_URL_FILE: /run/secrets/database_url + secrets: + - database_url + networks: + - backend + depends_on: + postgres: + condition: service_healthy + restart: 'no' + + api: + build: . + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + SESSION_STORE: redis + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL: redis://redis:6379/0 + secrets: + - database_url + networks: + - backend + depends_on: + migrate: + condition: service_completed_successfully + redis: + condition: service_healthy + healthcheck: + test: + [ + 'CMD', + 'python', + '-c', + "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)", + ] + interval: 3s + timeout: 3s + retries: 3 + start_period: 10s + stop_grace_period: 15s + restart: unless-stopped + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: '${API_CPU_LIMIT:-1.0}' + memory: '${API_MEMORY_LIMIT:-512M}' + + nginx: + image: nginx:1.27.5-alpine + ports: + - '${APP_PORT:-8080}:8080' + volumes: + - ./infra/nginx.conf:/etc/nginx/conf.d/default.conf:ro + networks: + - frontend + - backend + depends_on: + api: + condition: service_healthy + healthcheck: + test: ['CMD-SHELL', 'wget -q -O /dev/null http://127.0.0.1:8080/health/ready'] + interval: 5s + timeout: 3s + retries: 5 + restart: unless-stopped + +networks: + frontend: + backend: + internal: true + +volumes: + postgres_data: + redis_data: + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url diff --git a/db.py b/db.py new file mode 100644 index 00000000..e3ffed9a --- /dev/null +++ b/db.py @@ -0,0 +1,338 @@ +import hashlib +import json +import os +import uuid +from pathlib import Path + +from psycopg import errors +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool, PoolTimeout +from psycopg.types.json import Jsonb + +MIGRATIONS_DIR = Path(__file__).resolve().parent / 'migrations' + + +class ApiError(Exception): + def __init__(self, status, code, message): + super().__init__(message) + self.status = status + self.code = code + self.message = message + + +def read_database_url(): + path = os.environ.get('DATABASE_URL_FILE') + if path and os.path.isfile(path): + return Path(path).read_text(encoding='utf-8').strip() + url = os.environ.get('DATABASE_URL', '').strip() + if not url: + raise RuntimeError('DATABASE_URL or DATABASE_URL_FILE is required') + return url + + +class Store: + def __init__(self, pool: ConnectionPool): + self.pool = pool + + @classmethod + def connect(cls, run_migrations=True): + pool = ConnectionPool( + conninfo=read_database_url(), + min_size=1, + max_size=10, + timeout=5.0, + kwargs={ + 'row_factory': dict_row, + 'options': '-c statement_timeout=5000 -c lock_timeout=2000', + }, + open=True, + ) + store = cls(pool) + if run_migrations: + store.run_migrations() + return store + + def close(self): + self.pool.close() + + def ping(self): + with self.pool.connection() as conn: + conn.execute('SELECT 1') + + def run_migrations(self): + with self.pool.connection() as conn: + conn.execute( + 'CREATE TABLE IF NOT EXISTS schema_migrations ' + '(version TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW())' + ) + for path in sorted(MIGRATIONS_DIR.glob('*.sql')): + version = path.name + row = conn.execute( + 'SELECT 1 FROM schema_migrations WHERE version = %s', + (version,), + ).fetchone() + if row: + continue + sql = path.read_text(encoding='utf-8') + with conn.transaction(): + conn.execute(sql) + conn.execute( + 'INSERT INTO schema_migrations (version) VALUES (%s)', + (version,), + ) + + @staticmethod + def _user_row(row): + return { + 'id': str(row['id']), + 'username': row['username'], + 'display_name': row['display_name'], + 'created_at': row['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'), + } + + def register_user(self, username, display_name, password_hash, created_at): + user_id = str(uuid.uuid4()) + try: + with self.pool.connection() as conn: + conn.execute( + 'INSERT INTO users (id, username, display_name, password_hash, created_at) ' + 'VALUES (%s, %s, %s, %s, %s)', + (user_id, username, display_name, password_hash, created_at), + ) + except errors.UniqueViolation: + raise ApiError(409, 'conflict', 'Username already exists') from None + except (errors.Error, PoolTimeout): + raise ApiError(503, 'unavailable', 'Service unavailable') from None + return { + 'id': user_id, + 'username': username, + 'display_name': display_name, + 'created_at': created_at, + } + + def get_user(self, user_id): + with self.pool.connection() as conn: + row = conn.execute( + 'SELECT id, username, display_name, created_at FROM users WHERE id = %s', + (user_id,), + ).fetchone() + if row is None: + return None + return self._user_row(row) + + def get_user_with_password(self, username): + with self.pool.connection() as conn: + row = conn.execute( + 'SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s', + (username,), + ).fetchone() + return row + + def create_chat(self, user_id, title, member_ids, created_at): + chat_id = str(uuid.uuid4()) + try: + with self.pool.connection() as conn: + with conn.transaction(): + rows = conn.execute( + 'SELECT id FROM users WHERE id = ANY(%s)', + (member_ids,), + ).fetchall() + if len(rows) != len(set(member_ids)): + raise ApiError(400, 'invalid_request', 'Unknown chat member') + conn.execute( + 'INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)', + (chat_id, title, created_at), + ) + for index, member_id in enumerate(member_ids): + conn.execute( + 'INSERT INTO chat_members (chat_id, user_id, position) VALUES (%s, %s, %s)', + (chat_id, member_id, index), + ) + except ApiError: + raise + except errors.Error: + raise ApiError(500, 'internal_error', 'Internal server error') from None + return { + 'id': chat_id, + 'title': title, + 'member_ids': list(member_ids), + 'created_at': created_at, + } + + def _is_member(self, conn, chat_id, user_id): + row = conn.execute( + 'SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s', + (chat_id, user_id), + ).fetchone() + return row is not None + + def get_chat(self, user_id, chat_id): + with self.pool.connection() as conn: + if not self._is_member(conn, chat_id, user_id): + raise ApiError(404, 'not_found', 'Chat not found') + chat = conn.execute( + 'SELECT id, title, created_at FROM chats WHERE id = %s', + (chat_id,), + ).fetchone() + members = conn.execute( + 'SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY position', + (chat_id,), + ).fetchall() + return { + 'id': str(chat['id']), + 'title': chat['title'], + 'member_ids': [str(row['user_id']) for row in members], + 'created_at': chat['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'), + } + + def list_chats(self, user_id, limit, cursor_key): + query = ( + 'SELECT c.id, c.title, c.created_at ' + 'FROM chats c ' + 'JOIN chat_members cm ON cm.chat_id = c.id AND cm.user_id = %s ' + ) + params = [user_id] + if cursor_key: + query += 'WHERE (c.created_at, c.id) > (%s, %s) ' + params.extend(cursor_key) + query += 'ORDER BY c.created_at ASC, c.id ASC LIMIT %s' + params.append(limit + 1) + with self.pool.connection() as conn: + rows = conn.execute(query, params).fetchall() + has_more = len(rows) > limit + page = rows[:limit] + items = [] + for row in page: + chat_id = str(row['id']) + items.append(self.get_chat(user_id, chat_id)) + next_cursor = None + if has_more: + last = page[-1] + next_cursor = (last['created_at'], str(last['id'])) + return items, next_cursor + + def create_message(self, user_id, chat_id, text, created_at): + message_id = str(uuid.uuid4()) + with self.pool.connection() as conn: + with conn.transaction(): + if not self._is_member(conn, chat_id, user_id): + raise ApiError(404, 'not_found', 'Chat not found') + conn.execute( + 'INSERT INTO messages (id, chat_id, sender_id, text, created_at) ' + 'VALUES (%s, %s, %s, %s, %s)', + (message_id, chat_id, user_id, text, created_at), + ) + return { + 'id': message_id, + 'chat_id': chat_id, + 'sender_id': user_id, + 'text': text, + 'created_at': created_at, + } + + def create_message_idempotent(self, user_id, chat_id, text, idempotency_key, created_at): + resource_path = '/api/v1/chats/' + chat_id + '/messages' + request_hash = hashlib.sha256( + json.dumps( + {'chat_id': chat_id, 'text': text}, + ensure_ascii=False, + sort_keys=True, + separators=(',', ':'), + ).encode('utf-8') + ).hexdigest() + lock_key = '\x1f'.join( + (user_id, 'POST', resource_path, idempotency_key) + ) + try: + with self.pool.connection() as conn: + with conn.transaction(): + if not self._is_member(conn, chat_id, user_id): + raise ApiError(404, 'not_found', 'Chat not found') + conn.execute( + 'SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', + (lock_key,), + ) + row = conn.execute( + 'SELECT request_hash, response FROM message_idempotency ' + 'WHERE user_id = %s AND method = %s AND resource_path = %s ' + 'AND idempotency_key = %s AND expires_at > NOW() FOR UPDATE', + (user_id, 'POST', resource_path, idempotency_key), + ).fetchone() + if row is not None: + if row['request_hash'] != request_hash: + raise ApiError(409, 'conflict', 'Idempotency-Key was used with a different request') + return row['response'] + + conn.execute( + 'DELETE FROM message_idempotency WHERE user_id = %s ' + 'AND method = %s AND resource_path = %s AND idempotency_key = %s', + (user_id, 'POST', resource_path, idempotency_key), + ) + conn.execute( + 'DELETE FROM message_idempotency WHERE user_id = %s AND expires_at <= NOW()', + (user_id,), + ) + conn.execute( + 'INSERT INTO message_idempotency ' + '(user_id, method, resource_path, idempotency_key, request_hash, expires_at) ' + "VALUES (%s, %s, %s, %s, %s, NOW() + INTERVAL '24 hours')", + (user_id, 'POST', resource_path, idempotency_key, request_hash), + ) + message_id = str(uuid.uuid4()) + conn.execute( + 'INSERT INTO messages (id, chat_id, sender_id, text, created_at) ' + 'VALUES (%s, %s, %s, %s, %s)', + (message_id, chat_id, user_id, text, created_at), + ) + message = { + 'id': message_id, + 'chat_id': chat_id, + 'sender_id': user_id, + 'text': text, + 'created_at': created_at, + } + conn.execute( + 'UPDATE message_idempotency SET response = %s ' + 'WHERE user_id = %s AND method = %s AND resource_path = %s ' + 'AND idempotency_key = %s', + (Jsonb(message), user_id, 'POST', resource_path, idempotency_key), + ) + return message + except ApiError: + raise + except (errors.Error, PoolTimeout): + raise ApiError(503, 'unavailable', 'Service unavailable') from None + + def list_messages(self, user_id, chat_id, limit, cursor_key): + with self.pool.connection() as conn: + if not self._is_member(conn, chat_id, user_id): + raise ApiError(404, 'not_found', 'Chat not found') + query = ( + 'SELECT id, chat_id, sender_id, text, created_at FROM messages ' + 'WHERE chat_id = %s ' + ) + params = [chat_id] + if cursor_key: + query += 'AND (created_at, id) > (%s, %s) ' + params.extend(cursor_key) + query += 'ORDER BY created_at ASC, id ASC LIMIT %s' + params.append(limit + 1) + with self.pool.connection() as conn: + rows = conn.execute(query, params).fetchall() + has_more = len(rows) > limit + page = rows[:limit] + items = [ + { + 'id': str(row['id']), + 'chat_id': str(row['chat_id']), + 'sender_id': str(row['sender_id']), + 'text': row['text'], + 'created_at': row['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'), + } + for row in page + ] + next_cursor = None + if has_more: + last = page[-1] + next_cursor = (last['created_at'], str(last['id'])) + return items, next_cursor diff --git a/evidence/README.md b/evidence/README.md index 8df70015..eac1b21a 100644 --- a/evidence/README.md +++ b/evidence/README.md @@ -3,3 +3,5 @@ Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки. Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно. + +Для выбранного трека 1 `scripts/track1_benchmark.py` сохраняет повторяемые JSON-замеры и графики в `evidence/track1/`. `scripts/rolling_restart.py` сохраняет сводку последовательного restart, `scripts/rolling_update.py` — сводку сборки нового image и поочерёдной замены контейнеров под нагрузкой, а `failover-targets.json` — адресные проверки каждой API-реплики. diff --git a/evidence/failover-targets.json b/evidence/failover-targets.json new file mode 100644 index 00000000..0bcb4ffe --- /dev/null +++ b/evidence/failover-targets.json @@ -0,0 +1,23 @@ +{ + "method": "stop each original API instance by exact hostname; confirm write, idempotent replay and existing session through Nginx", + "results": [ + { + "stopped_instance_id": "596747a6ea35", + "serving_instance_id": "914a9545a4af", + "recovery_seconds": 1.094, + "transition_failures": 0, + "confirmed_message_id": "196dd880-c495-44bf-9ac3-1f2a4a19b346", + "idempotent_replay": true, + "existing_session_valid": true + }, + { + "stopped_instance_id": "914a9545a4af", + "serving_instance_id": "596747a6ea35", + "recovery_seconds": 1.092, + "transition_failures": 0, + "confirmed_message_id": "315abec8-16c0-45ce-844e-a3e08da3bcb4", + "idempotent_replay": true, + "existing_session_valid": true + } + ] +} diff --git a/evidence/rolling-restart-load.json b/evidence/rolling-restart-load.json new file mode 100644 index 00000000..db7621f5 --- /dev/null +++ b/evidence/rolling-restart-load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 40.0, + "elapsed_seconds": 40.161, + "concurrency": 16, + "successful_requests": 7054, + "failed_requests": 2, + "error_ratio": 0.000283, + "successful_rps": 175.644, + "successful_latency_ms": { + "p50": 104.831, + "p95": 196.305, + "p99": 278.076 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/rolling-restart.json b/evidence/rolling-restart.json new file mode 100644 index 00000000..396c3e54 --- /dev/null +++ b/evidence/rolling-restart.json @@ -0,0 +1,37 @@ +{ + "operation": "serial API replica restart under load; same image, no schema change", + "duration_seconds": 40, + "concurrency": 16, + "restarted_replicas": [ + { + "instance_id": "4fd03cf9f7bc", + "ready_seconds": 6.126 + }, + { + "instance_id": "52406027111f", + "ready_seconds": 6.148 + } + ], + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 40.0, + "elapsed_seconds": 40.161, + "concurrency": 16, + "successful_requests": 7054, + "failed_requests": 2, + "error_ratio": 0.000283, + "successful_rps": 175.644, + "successful_latency_ms": { + "p50": 104.831, + "p95": 196.305, + "p99": 278.076 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "load_raw_file": "rolling-restart-load.json", + "limitations": [ + "This verifies graceful process replacement, not a new-image rolling deployment.", + "PostgreSQL and Redis are single instances; their failure is not covered." + ] +} diff --git a/evidence/rolling-update-load.json b/evidence/rolling-update-load.json new file mode 100644 index 00000000..bd7e0a19 --- /dev/null +++ b/evidence/rolling-update-load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 90.0, + "elapsed_seconds": 90.142, + "concurrency": 16, + "successful_requests": 16479, + "failed_requests": 13, + "error_ratio": 0.000788, + "successful_rps": 182.812, + "successful_latency_ms": { + "p50": 45.288, + "p95": 195.03, + "p99": 285.894 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/rolling-update.json b/evidence/rolling-update.json new file mode 100644 index 00000000..eed0a22c --- /dev/null +++ b/evidence/rolling-update.json @@ -0,0 +1,47 @@ +{ + "operation": "new API image; serial container replacement with temporary capacity surge", + "duration_seconds": 90, + "concurrency": 16, + "new_image_id": "sha256:c41eef6ca515594133a6851138c8d722ea18c549a41210cf50025b08eb77d52c", + "build_revision": "lab3-4ef7756dc6dc", + "old_image_ids": [ + "sha256:85c537edc11aa524a51d941071734f76ca547df05acde776560f71efccf750e4", + "sha256:b4062eb779bac4cc27b8c72ed8ccb786d04f0f43a99fa786a79001eb34579529" + ], + "replacements": [ + { + "old_instance_id": "a435cb28247d", + "new_instance_id": "93d17447c3ca", + "ready_and_routed_seconds": 7.035, + "sequence": 1 + }, + { + "old_instance_id": "7a73010286ff", + "new_instance_id": "63b283186a7b", + "ready_and_routed_seconds": 7.043, + "sequence": 2 + } + ], + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 90.0, + "elapsed_seconds": 90.142, + "concurrency": 16, + "successful_requests": 16479, + "failed_requests": 13, + "error_ratio": 0.000788, + "successful_rps": 182.812, + "successful_latency_ms": { + "p50": 45.288, + "p95": 195.03, + "p99": 285.894 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "load_raw_file": "rolling-update-load.json", + "limitations": [ + "The schema migration in this exercise is additive and backward-compatible.", + "PostgreSQL and Redis are single instances and remain SPOFs." + ] +} diff --git a/evidence/track1/benchmark.json b/evidence/track1/benchmark.json new file mode 100644 index 00000000..8e5117ed --- /dev/null +++ b/evidence/track1/benchmark.json @@ -0,0 +1,804 @@ +{ + "track": "1 — цена масштабирования", + "method": { + "workload": "tests/load.py closed-loop POST messages through Nginx", + "duration_seconds_per_run": 30, + "warmup_seconds_per_scenario": 5, + "concurrency": 16, + "repeats_per_scenario": 3, + "fixed_total_api_limits": { + "cpus": 4.0, + "memory_mib": 2048 + }, + "growth_per_replica_limits": { + "cpus": 1.0, + "memory_mib": 512 + }, + "host_cpu_count": 32, + "platform": "Windows-11-10.0.26300-SP0", + "docker_server_version": "29.8.2", + "measurement_note": "Successful HTTP throughput, not committed-write throughput. Load generator is closed-loop. Docker stats are one-second samples and can miss short CPU peaks.", + "docker_host": { + "cpus": 32, + "memory_mib": 7539.719, + "operating_system": "Docker Desktop" + } + }, + "scenarios": [ + { + "name": "fixed-total-1", + "api_replicas": 1, + "api_cpu_limit_per_replica": 4.0, + "api_memory_limit_mib_per_replica": 2048, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 1, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.163, + "concurrency": 16, + "successful_requests": 4914, + "failed_requests": 1, + "error_ratio": 0.000203, + "successful_rps": 162.916, + "successful_latency_ms": { + "p50": 96.844, + "p95": 120.582, + "p99": 132.171 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 166.78, + "memory_bytes": 37612421 + }, + "nginx": { + "cpu_percent": 8.84, + "memory_bytes": 33009172 + }, + "postgres": { + "cpu_percent": 27.23, + "memory_bytes": 72404172 + } + }, + "raw_file": "fixed-total-1-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.157, + "concurrency": 16, + "successful_requests": 4888, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 162.084, + "successful_latency_ms": { + "p50": 98.429, + "p95": 120.567, + "p99": 130.875 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 166.1, + "memory_bytes": 37339791 + }, + "nginx": { + "cpu_percent": 6.69, + "memory_bytes": 33764147 + }, + "postgres": { + "cpu_percent": 27.69, + "memory_bytes": 77835796 + } + }, + "raw_file": "fixed-total-1-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.131, + "concurrency": 16, + "successful_requests": 4848, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 160.898, + "successful_latency_ms": { + "p50": 99.68, + "p95": 122.681, + "p99": 133.838 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 165.81, + "memory_bytes": 37371248 + }, + "nginx": { + "cpu_percent": 9.12, + "memory_bytes": 33931919 + }, + "postgres": { + "cpu_percent": 33.48, + "memory_bytes": 99184803 + } + }, + "raw_file": "fixed-total-1-run-03.json" + } + ] + }, + { + "name": "fixed-total-2", + "api_replicas": 2, + "api_cpu_limit_per_replica": 2.0, + "api_memory_limit_mib_per_replica": 1024, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 2, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.15, + "concurrency": 16, + "successful_requests": 8817, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 292.438, + "successful_latency_ms": { + "p50": 52.624, + "p95": 105.861, + "p99": 127.191 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 243.71999999999997, + "memory_bytes": 86622863 + }, + "nginx": { + "cpu_percent": 15.26, + "memory_bytes": 33952890 + }, + "postgres": { + "cpu_percent": 49.97, + "memory_bytes": 114085068 + } + }, + "raw_file": "fixed-total-2-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.126, + "concurrency": 16, + "successful_requests": 8976, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 297.944, + "successful_latency_ms": { + "p50": 54.081, + "p95": 101.443, + "p99": 121.794 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 325.88, + "memory_bytes": 85574287 + }, + "nginx": { + "cpu_percent": 15.01, + "memory_bytes": 34728837 + }, + "postgres": { + "cpu_percent": 51.12, + "memory_bytes": 129603993 + } + }, + "raw_file": "fixed-total-2-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.12, + "concurrency": 16, + "successful_requests": 8695, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 288.675, + "successful_latency_ms": { + "p50": 55.679, + "p95": 103.148, + "p99": 122.651 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 250.14999999999998, + "memory_bytes": 73777806 + }, + "nginx": { + "cpu_percent": 15.59, + "memory_bytes": 33942405 + }, + "postgres": { + "cpu_percent": 50.46, + "memory_bytes": 157915545 + } + }, + "raw_file": "fixed-total-2-run-03.json" + } + ] + }, + { + "name": "fixed-total-4", + "api_replicas": 4, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 4, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.129, + "concurrency": 16, + "successful_requests": 10401, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 345.22, + "successful_latency_ms": { + "p50": 28.408, + "p95": 142.989, + "p99": 227.47 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 243.21, + "memory_bytes": 165423348 + }, + "nginx": { + "cpu_percent": 17.5, + "memory_bytes": 34319892 + }, + "postgres": { + "cpu_percent": 62.05, + "memory_bytes": 189477683 + } + }, + "raw_file": "fixed-total-4-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.128, + "concurrency": 16, + "successful_requests": 10612, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 352.228, + "successful_latency_ms": { + "p50": 26.487, + "p95": 145.879, + "p99": 223.534 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 282.35, + "memory_bytes": 149505964 + }, + "nginx": { + "cpu_percent": 19.67, + "memory_bytes": 34623979 + }, + "postgres": { + "cpu_percent": 74.48, + "memory_bytes": 215587225 + } + }, + "raw_file": "fixed-total-4-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.134, + "concurrency": 16, + "successful_requests": 10317, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 342.372, + "successful_latency_ms": { + "p50": 26.381, + "p95": 148.897, + "p99": 227.129 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 225.7, + "memory_bytes": 147996015 + }, + "nginx": { + "cpu_percent": 17.67, + "memory_bytes": 35316039 + }, + "postgres": { + "cpu_percent": 67.23, + "memory_bytes": 228694425 + } + }, + "raw_file": "fixed-total-4-run-03.json" + } + ] + }, + { + "name": "growing-total-1", + "api_replicas": 1, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 1.0, + "total_api_memory_limit_mib": 512, + "instances_observed": 1, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.215, + "concurrency": 16, + "successful_requests": 2775, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 91.843, + "successful_latency_ms": { + "p50": 172.602, + "p95": 282.816, + "p99": 311.881 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 101.46, + "memory_bytes": 49450844 + }, + "nginx": { + "cpu_percent": 6.14, + "memory_bytes": 36941332 + }, + "postgres": { + "cpu_percent": 18.35, + "memory_bytes": 155923251 + } + }, + "raw_file": "growing-total-1-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.239, + "concurrency": 16, + "successful_requests": 2791, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 92.297, + "successful_latency_ms": { + "p50": 173.744, + "p95": 274.984, + "p99": 304.02 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 102.34, + "memory_bytes": 46441431 + }, + "nginx": { + "cpu_percent": 6.6, + "memory_bytes": 32862371 + }, + "postgres": { + "cpu_percent": 18.53, + "memory_bytes": 160012697 + } + }, + "raw_file": "growing-total-1-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.19, + "concurrency": 16, + "successful_requests": 2770, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 91.753, + "successful_latency_ms": { + "p50": 174.887, + "p95": 274.617, + "p99": 304.192 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 104.05, + "memory_bytes": 50216304 + }, + "nginx": { + "cpu_percent": 7.46, + "memory_bytes": 32600227 + }, + "postgres": { + "cpu_percent": 16.92, + "memory_bytes": 160851558 + } + }, + "raw_file": "growing-total-1-run-03.json" + } + ] + }, + { + "name": "growing-total-2", + "api_replicas": 2, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 2.0, + "total_api_memory_limit_mib": 1024, + "instances_observed": 2, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.258, + "concurrency": 16, + "successful_requests": 5309, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 175.455, + "successful_latency_ms": { + "p50": 75.155, + "p95": 202.93, + "p99": 289.628 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 173.1, + "memory_bytes": 84053852 + }, + "nginx": { + "cpu_percent": 10.59, + "memory_bytes": 32820428 + }, + "postgres": { + "cpu_percent": 32.31, + "memory_bytes": 193776844 + } + }, + "raw_file": "growing-total-2-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.159, + "concurrency": 16, + "successful_requests": 5385, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 178.556, + "successful_latency_ms": { + "p50": 68.368, + "p95": 199.407, + "p99": 283.931 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 153.63, + "memory_bytes": 73389833 + }, + "nginx": { + "cpu_percent": 10.45, + "memory_bytes": 33229373 + }, + "postgres": { + "cpu_percent": 31.43, + "memory_bytes": 200802304 + } + }, + "raw_file": "growing-total-2-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.131, + "concurrency": 16, + "successful_requests": 5440, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 180.543, + "successful_latency_ms": { + "p50": 72.837, + "p95": 198.143, + "p99": 280.041 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 155.19, + "memory_bytes": 84892712 + }, + "nginx": { + "cpu_percent": 10.22, + "memory_bytes": 33449574 + }, + "postgres": { + "cpu_percent": 31.97, + "memory_bytes": 207722905 + } + }, + "raw_file": "growing-total-2-run-03.json" + } + ] + }, + { + "name": "growing-total-4", + "api_replicas": 4, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 4, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.135, + "concurrency": 16, + "successful_requests": 10455, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 346.937, + "successful_latency_ms": { + "p50": 26.072, + "p95": 145.651, + "p99": 228.381 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 231.85, + "memory_bytes": 173067466 + }, + "nginx": { + "cpu_percent": 17.81, + "memory_bytes": 34141634 + }, + "postgres": { + "cpu_percent": 62.88, + "memory_bytes": 260361420 + } + }, + "raw_file": "growing-total-4-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.153, + "concurrency": 16, + "successful_requests": 10355, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 343.41, + "successful_latency_ms": { + "p50": 25.913, + "p95": 151.07, + "p99": 213.914 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 231.97, + "memory_bytes": 153574440 + }, + "nginx": { + "cpu_percent": 18.09, + "memory_bytes": 34802237 + }, + "postgres": { + "cpu_percent": 65.95, + "memory_bytes": 275775488 + } + }, + "raw_file": "growing-total-4-run-02.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.143, + "concurrency": 16, + "successful_requests": 10545, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 349.828, + "successful_latency_ms": { + "p50": 25.788, + "p95": 148.36, + "p99": 209.459 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 240.94, + "memory_bytes": 152483920 + }, + "nginx": { + "cpu_percent": 18.53, + "memory_bytes": 34707865 + }, + "postgres": { + "cpu_percent": 66.11, + "memory_bytes": 277033779 + } + }, + "raw_file": "growing-total-4-run-03.json" + } + ] + } + ], + "summary": [ + { + "name": "fixed-total-1", + "api_replicas": 1, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 161.966, + "error_ratio_mean": 6.8e-05, + "p95_latency_ms_mean": 121.277, + "api_cpu_peak_percent_mean": 166.23, + "api_memory_peak_mib_mean": 35.707, + "postgres_cpu_peak_percent_mean": 29.467, + "postgres_memory_peak_mib_mean": 79.29, + "nginx_cpu_peak_percent_mean": 8.217, + "nginx_memory_peak_mib_mean": 32.013 + }, + { + "name": "fixed-total-2", + "api_replicas": 2, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 293.019, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 103.484, + "api_cpu_peak_percent_mean": 273.25, + "api_memory_peak_mib_mean": 78.193, + "postgres_cpu_peak_percent_mean": 50.517, + "postgres_memory_peak_mib_mean": 127.667, + "nginx_cpu_peak_percent_mean": 15.287, + "nginx_memory_peak_mib_mean": 32.623 + }, + { + "name": "fixed-total-4", + "api_replicas": 4, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 346.607, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 145.922, + "api_cpu_peak_percent_mean": 250.42, + "api_memory_peak_mib_mean": 147.16, + "postgres_cpu_peak_percent_mean": 67.92, + "postgres_memory_peak_mib_mean": 201.467, + "nginx_cpu_peak_percent_mean": 18.28, + "nginx_memory_peak_mib_mean": 33.143 + }, + { + "name": "growing-total-1", + "api_replicas": 1, + "total_api_cpu_limit": 1.0, + "total_api_memory_limit_mib": 512, + "successful_rps_mean": 91.964, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 277.472, + "api_cpu_peak_percent_mean": 102.617, + "api_memory_peak_mib_mean": 46.447, + "postgres_cpu_peak_percent_mean": 17.933, + "postgres_memory_peak_mib_mean": 151.567, + "nginx_cpu_peak_percent_mean": 6.733, + "nginx_memory_peak_mib_mean": 32.553 + }, + { + "name": "growing-total-2", + "api_replicas": 2, + "total_api_cpu_limit": 2.0, + "total_api_memory_limit_mib": 1024, + "successful_rps_mean": 178.185, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 200.16, + "api_cpu_peak_percent_mean": 160.64, + "api_memory_peak_mib_mean": 77.037, + "postgres_cpu_peak_percent_mean": 31.903, + "postgres_memory_peak_mib_mean": 191.467, + "nginx_cpu_peak_percent_mean": 10.42, + "nginx_memory_peak_mib_mean": 31.63 + }, + { + "name": "growing-total-4", + "api_replicas": 4, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 346.725, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 148.36, + "api_cpu_peak_percent_mean": 234.92, + "api_memory_peak_mib_mean": 152.31, + "postgres_cpu_peak_percent_mean": 64.98, + "postgres_memory_peak_mib_mean": 258.5, + "nginx_cpu_peak_percent_mean": 18.143, + "nginx_memory_peak_mib_mean": 32.95 + } + ], + "raw_runs": true, + "charts": [ + "successful-rps.svg", + "p95-latency.svg" + ] +} diff --git a/evidence/track1/fixed-total-1-run-01.json b/evidence/track1/fixed-total-1-run-01.json new file mode 100644 index 00000000..e5a62e62 --- /dev/null +++ b/evidence/track1/fixed-total-1-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.163, + "concurrency": 16, + "successful_requests": 4914, + "failed_requests": 1, + "error_ratio": 0.000203, + "successful_rps": 162.916, + "successful_latency_ms": { + "p50": 96.844, + "p95": 120.582, + "p99": 132.171 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-1-run-02.json b/evidence/track1/fixed-total-1-run-02.json new file mode 100644 index 00000000..b5d2213f --- /dev/null +++ b/evidence/track1/fixed-total-1-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.157, + "concurrency": 16, + "successful_requests": 4888, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 162.084, + "successful_latency_ms": { + "p50": 98.429, + "p95": 120.567, + "p99": 130.875 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-1-run-03.json b/evidence/track1/fixed-total-1-run-03.json new file mode 100644 index 00000000..772b1c12 --- /dev/null +++ b/evidence/track1/fixed-total-1-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.131, + "concurrency": 16, + "successful_requests": 4848, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 160.898, + "successful_latency_ms": { + "p50": 99.68, + "p95": 122.681, + "p99": 133.838 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-2-run-01.json b/evidence/track1/fixed-total-2-run-01.json new file mode 100644 index 00000000..7f3a797d --- /dev/null +++ b/evidence/track1/fixed-total-2-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.15, + "concurrency": 16, + "successful_requests": 8817, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 292.438, + "successful_latency_ms": { + "p50": 52.624, + "p95": 105.861, + "p99": 127.191 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-2-run-02.json b/evidence/track1/fixed-total-2-run-02.json new file mode 100644 index 00000000..cdf52d77 --- /dev/null +++ b/evidence/track1/fixed-total-2-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.126, + "concurrency": 16, + "successful_requests": 8976, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 297.944, + "successful_latency_ms": { + "p50": 54.081, + "p95": 101.443, + "p99": 121.794 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-2-run-03.json b/evidence/track1/fixed-total-2-run-03.json new file mode 100644 index 00000000..9fcb0c66 --- /dev/null +++ b/evidence/track1/fixed-total-2-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.12, + "concurrency": 16, + "successful_requests": 8695, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 288.675, + "successful_latency_ms": { + "p50": 55.679, + "p95": 103.148, + "p99": 122.651 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-4-run-01.json b/evidence/track1/fixed-total-4-run-01.json new file mode 100644 index 00000000..685c6d74 --- /dev/null +++ b/evidence/track1/fixed-total-4-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.129, + "concurrency": 16, + "successful_requests": 10401, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 345.22, + "successful_latency_ms": { + "p50": 28.408, + "p95": 142.989, + "p99": 227.47 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-4-run-02.json b/evidence/track1/fixed-total-4-run-02.json new file mode 100644 index 00000000..117c61f1 --- /dev/null +++ b/evidence/track1/fixed-total-4-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.128, + "concurrency": 16, + "successful_requests": 10612, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 352.228, + "successful_latency_ms": { + "p50": 26.487, + "p95": 145.879, + "p99": 223.534 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/fixed-total-4-run-03.json b/evidence/track1/fixed-total-4-run-03.json new file mode 100644 index 00000000..f42afbe1 --- /dev/null +++ b/evidence/track1/fixed-total-4-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.134, + "concurrency": 16, + "successful_requests": 10317, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 342.372, + "successful_latency_ms": { + "p50": 26.381, + "p95": 148.897, + "p99": 227.129 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-1-run-01.json b/evidence/track1/growing-total-1-run-01.json new file mode 100644 index 00000000..de0e45ee --- /dev/null +++ b/evidence/track1/growing-total-1-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.215, + "concurrency": 16, + "successful_requests": 2775, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 91.843, + "successful_latency_ms": { + "p50": 172.602, + "p95": 282.816, + "p99": 311.881 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-1-run-02.json b/evidence/track1/growing-total-1-run-02.json new file mode 100644 index 00000000..580ba6e3 --- /dev/null +++ b/evidence/track1/growing-total-1-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.239, + "concurrency": 16, + "successful_requests": 2791, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 92.297, + "successful_latency_ms": { + "p50": 173.744, + "p95": 274.984, + "p99": 304.02 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-1-run-03.json b/evidence/track1/growing-total-1-run-03.json new file mode 100644 index 00000000..cefddb79 --- /dev/null +++ b/evidence/track1/growing-total-1-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.19, + "concurrency": 16, + "successful_requests": 2770, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 91.753, + "successful_latency_ms": { + "p50": 174.887, + "p95": 274.617, + "p99": 304.192 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-2-run-01.json b/evidence/track1/growing-total-2-run-01.json new file mode 100644 index 00000000..3df812f7 --- /dev/null +++ b/evidence/track1/growing-total-2-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.258, + "concurrency": 16, + "successful_requests": 5309, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 175.455, + "successful_latency_ms": { + "p50": 75.155, + "p95": 202.93, + "p99": 289.628 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-2-run-02.json b/evidence/track1/growing-total-2-run-02.json new file mode 100644 index 00000000..2c9dcac3 --- /dev/null +++ b/evidence/track1/growing-total-2-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.159, + "concurrency": 16, + "successful_requests": 5385, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 178.556, + "successful_latency_ms": { + "p50": 68.368, + "p95": 199.407, + "p99": 283.931 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-2-run-03.json b/evidence/track1/growing-total-2-run-03.json new file mode 100644 index 00000000..a01432c1 --- /dev/null +++ b/evidence/track1/growing-total-2-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.131, + "concurrency": 16, + "successful_requests": 5440, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 180.543, + "successful_latency_ms": { + "p50": 72.837, + "p95": 198.143, + "p99": 280.041 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-4-run-01.json b/evidence/track1/growing-total-4-run-01.json new file mode 100644 index 00000000..e6e0c269 --- /dev/null +++ b/evidence/track1/growing-total-4-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.135, + "concurrency": 16, + "successful_requests": 10455, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 346.937, + "successful_latency_ms": { + "p50": 26.072, + "p95": 145.651, + "p99": 228.381 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-4-run-02.json b/evidence/track1/growing-total-4-run-02.json new file mode 100644 index 00000000..39bdba77 --- /dev/null +++ b/evidence/track1/growing-total-4-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.153, + "concurrency": 16, + "successful_requests": 10355, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 343.41, + "successful_latency_ms": { + "p50": 25.913, + "p95": 151.07, + "p99": 213.914 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/growing-total-4-run-03.json b/evidence/track1/growing-total-4-run-03.json new file mode 100644 index 00000000..6d8863d4 --- /dev/null +++ b/evidence/track1/growing-total-4-run-03.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.143, + "concurrency": 16, + "successful_requests": 10545, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 349.828, + "successful_latency_ms": { + "p50": 25.788, + "p95": 148.36, + "p99": 209.459 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/benchmark.json b/evidence/track1/high-concurrency/benchmark.json new file mode 100644 index 00000000..822a67ee --- /dev/null +++ b/evidence/track1/high-concurrency/benchmark.json @@ -0,0 +1,600 @@ +{ + "track": "1 — цена масштабирования", + "method": { + "workload": "tests/load.py closed-loop POST messages through Nginx", + "duration_seconds_per_run": 20, + "warmup_seconds_per_scenario": 5, + "concurrency": 64, + "repeats_per_scenario": 2, + "fixed_total_api_limits": { + "cpus": 4.0, + "memory_mib": 2048 + }, + "growth_per_replica_limits": { + "cpus": 1.0, + "memory_mib": 512 + }, + "host_cpu_count": 32, + "platform": "Windows-11-10.0.26300-SP0", + "docker_server_version": "29.8.2", + "docker_host": { + "cpus": 32, + "memory_mib": 7539.719, + "operating_system": "Docker Desktop" + }, + "measurement_note": "Successful HTTP throughput, not committed-write throughput. Load generator is closed-loop. Docker stats are one-second samples and can miss short CPU peaks." + }, + "scenarios": [ + { + "name": "fixed-total-1", + "api_replicas": 1, + "api_cpu_limit_per_replica": 4.0, + "api_memory_limit_mib_per_replica": 2048, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 1, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.667, + "concurrency": 64, + "successful_requests": 3177, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 153.722, + "successful_latency_ms": { + "p50": 404.609, + "p95": 452.023, + "p99": 470.854 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 185.76, + "memory_bytes": 54190407 + }, + "nginx": { + "cpu_percent": 6.96, + "memory_bytes": 32788971 + }, + "postgres": { + "cpu_percent": 28.34, + "memory_bytes": 215901798 + } + }, + "raw_file": "fixed-total-1-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.592, + "concurrency": 64, + "successful_requests": 3242, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 157.438, + "successful_latency_ms": { + "p50": 395.408, + "p95": 444.995, + "p99": 461.263 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 164.2, + "memory_bytes": 42026926 + }, + "nginx": { + "cpu_percent": 9.01, + "memory_bytes": 33386659 + }, + "postgres": { + "cpu_percent": 27.83, + "memory_bytes": 220410675 + } + }, + "raw_file": "fixed-total-1-run-02.json" + } + ] + }, + { + "name": "fixed-total-2", + "api_replicas": 2, + "api_cpu_limit_per_replica": 2.0, + "api_memory_limit_mib_per_replica": 1024, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 2, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.5, + "concurrency": 64, + "successful_requests": 5945, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 290.003, + "successful_latency_ms": { + "p50": 214.786, + "p95": 284.069, + "p99": 320.403 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 333.02, + "memory_bytes": 77972110 + }, + "nginx": { + "cpu_percent": 14.7, + "memory_bytes": 34529607 + }, + "postgres": { + "cpu_percent": 48.13, + "memory_bytes": 243164774 + } + }, + "raw_file": "fixed-total-2-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.552, + "concurrency": 64, + "successful_requests": 5724, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 278.508, + "successful_latency_ms": { + "p50": 223.17, + "p95": 264.361, + "p99": 293.779 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 354.98, + "memory_bytes": 77038878 + }, + "nginx": { + "cpu_percent": 14.73, + "memory_bytes": 34466693 + }, + "postgres": { + "cpu_percent": 52.17, + "memory_bytes": 248197939 + } + }, + "raw_file": "fixed-total-2-run-02.json" + } + ] + }, + { + "name": "fixed-total-4", + "api_replicas": 4, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 4, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.677, + "concurrency": 64, + "successful_requests": 6229, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 301.256, + "successful_latency_ms": { + "p50": 214.172, + "p95": 449.253, + "p99": 570.14 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 337.04, + "memory_bytes": 160222411 + }, + "nginx": { + "cpu_percent": 16.93, + "memory_bytes": 35368468 + }, + "postgres": { + "cpu_percent": 86.05, + "memory_bytes": 318033100 + } + }, + "raw_file": "fixed-total-4-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.657, + "concurrency": 64, + "successful_requests": 6595, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 319.262, + "successful_latency_ms": { + "p50": 183.64, + "p95": 467.121, + "p99": 573.588 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 415.49, + "memory_bytes": 168191589 + }, + "nginx": { + "cpu_percent": 17.02, + "memory_bytes": 35001466 + }, + "postgres": { + "cpu_percent": 62.69, + "memory_bytes": 305030758 + } + }, + "raw_file": "fixed-total-4-run-02.json" + } + ] + }, + { + "name": "growing-total-1", + "api_replicas": 1, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 1.0, + "total_api_memory_limit_mib": 512, + "instances_observed": 1, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.818, + "concurrency": 64, + "successful_requests": 1947, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 93.527, + "successful_latency_ms": { + "p50": 649.887, + "p95": 869.536, + "p99": 896.438 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 101.91, + "memory_bytes": 54693724 + }, + "nginx": { + "cpu_percent": 5.54, + "memory_bytes": 34728837 + }, + "postgres": { + "cpu_percent": 17.79, + "memory_bytes": 242011340 + } + }, + "raw_file": "growing-total-1-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.991, + "concurrency": 64, + "successful_requests": 1951, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 92.945, + "successful_latency_ms": { + "p50": 645.531, + "p95": 873.654, + "p99": 893.081 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 102.7, + "memory_bytes": 51023708 + }, + "nginx": { + "cpu_percent": 6.21, + "memory_bytes": 34173091 + }, + "postgres": { + "cpu_percent": 17.57, + "memory_bytes": 242325913 + } + }, + "raw_file": "growing-total-1-run-02.json" + } + ] + }, + { + "name": "growing-total-2", + "api_replicas": 2, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 2.0, + "total_api_memory_limit_mib": 1024, + "instances_observed": 2, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.688, + "concurrency": 64, + "successful_requests": 3509, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 169.619, + "successful_latency_ms": { + "p50": 358.445, + "p95": 535.394, + "p99": 625.777 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 205.44, + "memory_bytes": 105298001 + }, + "nginx": { + "cpu_percent": 9.89, + "memory_bytes": 33795604 + }, + "postgres": { + "cpu_percent": 34.21, + "memory_bytes": 269274316 + } + }, + "raw_file": "growing-total-2-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.677, + "concurrency": 64, + "successful_requests": 3472, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 167.913, + "successful_latency_ms": { + "p50": 365.436, + "p95": 517.84, + "p99": 591.124 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 201.14, + "memory_bytes": 103054049 + }, + "nginx": { + "cpu_percent": 12.01, + "memory_bytes": 34246492 + }, + "postgres": { + "cpu_percent": 39.19, + "memory_bytes": 270008320 + } + }, + "raw_file": "growing-total-2-run-02.json" + } + ] + }, + { + "name": "growing-total-4", + "api_replicas": 4, + "api_cpu_limit_per_replica": 1.0, + "api_memory_limit_mib_per_replica": 512, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "instances_observed": 4, + "runs": [ + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.706, + "concurrency": 64, + "successful_requests": 6345, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 306.428, + "successful_latency_ms": { + "p50": 207.526, + "p95": 489.341, + "p99": 575.398 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 333.53000000000003, + "memory_bytes": 178782205 + }, + "nginx": { + "cpu_percent": 17.87, + "memory_bytes": 35378954 + }, + "postgres": { + "cpu_percent": 65.21, + "memory_bytes": 364694732 + } + }, + "raw_file": "growing-total-4-run-01.json" + }, + { + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 21.034, + "concurrency": 64, + "successful_requests": 6433, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 305.844, + "successful_latency_ms": { + "p50": 45.486, + "p95": 633.764, + "p99": 760.639 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + }, + "peak_resources": { + "api": { + "cpu_percent": 361.56, + "memory_bytes": 173350582 + }, + "nginx": { + "cpu_percent": 17.9, + "memory_bytes": 34676408 + }, + "postgres": { + "cpu_percent": 65.35, + "memory_bytes": 365428736 + } + }, + "raw_file": "growing-total-4-run-02.json" + } + ] + } + ], + "summary": [ + { + "name": "fixed-total-1", + "api_replicas": 1, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 155.58, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 448.509, + "api_cpu_peak_percent_mean": 174.98, + "api_memory_peak_mib_mean": 45.88, + "postgres_cpu_peak_percent_mean": 28.085, + "postgres_memory_peak_mib_mean": 208.05, + "nginx_cpu_peak_percent_mean": 7.985, + "nginx_memory_peak_mib_mean": 31.555 + }, + { + "name": "fixed-total-2", + "api_replicas": 2, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 284.255, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 274.215, + "api_cpu_peak_percent_mean": 344.0, + "api_memory_peak_mib_mean": 73.915, + "postgres_cpu_peak_percent_mean": 50.15, + "postgres_memory_peak_mib_mean": 234.3, + "nginx_cpu_peak_percent_mean": 14.715, + "nginx_memory_peak_mib_mean": 32.9 + }, + { + "name": "fixed-total-4", + "api_replicas": 4, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 310.259, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 458.187, + "api_cpu_peak_percent_mean": 376.265, + "api_memory_peak_mib_mean": 156.6, + "postgres_cpu_peak_percent_mean": 74.37, + "postgres_memory_peak_mib_mean": 297.1, + "nginx_cpu_peak_percent_mean": 16.975, + "nginx_memory_peak_mib_mean": 33.555 + }, + { + "name": "growing-total-1", + "api_replicas": 1, + "total_api_cpu_limit": 1.0, + "total_api_memory_limit_mib": 512, + "successful_rps_mean": 93.236, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 871.595, + "api_cpu_peak_percent_mean": 102.305, + "api_memory_peak_mib_mean": 50.41, + "postgres_cpu_peak_percent_mean": 17.68, + "postgres_memory_peak_mib_mean": 230.95, + "nginx_cpu_peak_percent_mean": 5.875, + "nginx_memory_peak_mib_mean": 32.855 + }, + { + "name": "growing-total-2", + "api_replicas": 2, + "total_api_cpu_limit": 2.0, + "total_api_memory_limit_mib": 1024, + "successful_rps_mean": 168.766, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 526.617, + "api_cpu_peak_percent_mean": 203.29, + "api_memory_peak_mib_mean": 99.35, + "postgres_cpu_peak_percent_mean": 36.7, + "postgres_memory_peak_mib_mean": 257.15, + "nginx_cpu_peak_percent_mean": 10.95, + "nginx_memory_peak_mib_mean": 32.445 + }, + { + "name": "growing-total-4", + "api_replicas": 4, + "total_api_cpu_limit": 4.0, + "total_api_memory_limit_mib": 2048, + "successful_rps_mean": 306.136, + "error_ratio_mean": 0.0, + "p95_latency_ms_mean": 561.553, + "api_cpu_peak_percent_mean": 347.545, + "api_memory_peak_mib_mean": 167.91, + "postgres_cpu_peak_percent_mean": 65.28, + "postgres_memory_peak_mib_mean": 348.15, + "nginx_cpu_peak_percent_mean": 17.885, + "nginx_memory_peak_mib_mean": 33.405 + } + ], + "raw_runs": true, + "charts": [ + "successful-rps.svg", + "p95-latency.svg" + ] +} diff --git a/evidence/track1/high-concurrency/fixed-total-1-run-01.json b/evidence/track1/high-concurrency/fixed-total-1-run-01.json new file mode 100644 index 00000000..58393fee --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-1-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.667, + "concurrency": 64, + "successful_requests": 3177, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 153.722, + "successful_latency_ms": { + "p50": 404.609, + "p95": 452.023, + "p99": 470.854 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/fixed-total-1-run-02.json b/evidence/track1/high-concurrency/fixed-total-1-run-02.json new file mode 100644 index 00000000..19a60877 --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-1-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.592, + "concurrency": 64, + "successful_requests": 3242, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 157.438, + "successful_latency_ms": { + "p50": 395.408, + "p95": 444.995, + "p99": 461.263 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/fixed-total-2-run-01.json b/evidence/track1/high-concurrency/fixed-total-2-run-01.json new file mode 100644 index 00000000..779d49dd --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-2-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.5, + "concurrency": 64, + "successful_requests": 5945, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 290.003, + "successful_latency_ms": { + "p50": 214.786, + "p95": 284.069, + "p99": 320.403 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/fixed-total-2-run-02.json b/evidence/track1/high-concurrency/fixed-total-2-run-02.json new file mode 100644 index 00000000..6cb1a243 --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-2-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.552, + "concurrency": 64, + "successful_requests": 5724, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 278.508, + "successful_latency_ms": { + "p50": 223.17, + "p95": 264.361, + "p99": 293.779 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/fixed-total-4-run-01.json b/evidence/track1/high-concurrency/fixed-total-4-run-01.json new file mode 100644 index 00000000..d797ece2 --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-4-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.677, + "concurrency": 64, + "successful_requests": 6229, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 301.256, + "successful_latency_ms": { + "p50": 214.172, + "p95": 449.253, + "p99": 570.14 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/fixed-total-4-run-02.json b/evidence/track1/high-concurrency/fixed-total-4-run-02.json new file mode 100644 index 00000000..828479b5 --- /dev/null +++ b/evidence/track1/high-concurrency/fixed-total-4-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.657, + "concurrency": 64, + "successful_requests": 6595, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 319.262, + "successful_latency_ms": { + "p50": 183.64, + "p95": 467.121, + "p99": 573.588 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-1-run-01.json b/evidence/track1/high-concurrency/growing-total-1-run-01.json new file mode 100644 index 00000000..39406130 --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-1-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.818, + "concurrency": 64, + "successful_requests": 1947, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 93.527, + "successful_latency_ms": { + "p50": 649.887, + "p95": 869.536, + "p99": 896.438 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-1-run-02.json b/evidence/track1/high-concurrency/growing-total-1-run-02.json new file mode 100644 index 00000000..e247bc6a --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-1-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.991, + "concurrency": 64, + "successful_requests": 1951, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 92.945, + "successful_latency_ms": { + "p50": 645.531, + "p95": 873.654, + "p99": 893.081 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-2-run-01.json b/evidence/track1/high-concurrency/growing-total-2-run-01.json new file mode 100644 index 00000000..561c5909 --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-2-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.688, + "concurrency": 64, + "successful_requests": 3509, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 169.619, + "successful_latency_ms": { + "p50": 358.445, + "p95": 535.394, + "p99": 625.777 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-2-run-02.json b/evidence/track1/high-concurrency/growing-total-2-run-02.json new file mode 100644 index 00000000..1dddc35c --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-2-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.677, + "concurrency": 64, + "successful_requests": 3472, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 167.913, + "successful_latency_ms": { + "p50": 365.436, + "p95": 517.84, + "p99": 591.124 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-4-run-01.json b/evidence/track1/high-concurrency/growing-total-4-run-01.json new file mode 100644 index 00000000..9749f2ab --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-4-run-01.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 20.706, + "concurrency": 64, + "successful_requests": 6345, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 306.428, + "successful_latency_ms": { + "p50": 207.526, + "p95": 489.341, + "p99": 575.398 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/growing-total-4-run-02.json b/evidence/track1/high-concurrency/growing-total-4-run-02.json new file mode 100644 index 00000000..1aec39df --- /dev/null +++ b/evidence/track1/high-concurrency/growing-total-4-run-02.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 20.0, + "elapsed_seconds": 21.034, + "concurrency": 64, + "successful_requests": 6433, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 305.844, + "successful_latency_ms": { + "p50": 45.486, + "p95": 633.764, + "p99": 760.639 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1/high-concurrency/p95-latency.svg b/evidence/track1/high-concurrency/p95-latency.svg new file mode 100644 index 00000000..6315034d --- /dev/null +++ b/evidence/track1/high-concurrency/p95-latency.svg @@ -0,0 +1,37 @@ + + +Successful request p95 by scale + +0.0 + +217.9 + +435.8 + +653.7 + +871.6 + +448.5 +fixed 1 + +274.2 +fixed 2 + +458.2 +fixed 4 + +871.6 +growth 1 + +526.6 +growth 2 + +561.6 +growth 4 +Milliseconds + +fixed API budget + +growing API budget + diff --git a/evidence/track1/high-concurrency/successful-rps.svg b/evidence/track1/high-concurrency/successful-rps.svg new file mode 100644 index 00000000..0c33b2fe --- /dev/null +++ b/evidence/track1/high-concurrency/successful-rps.svg @@ -0,0 +1,37 @@ + + +Successful HTTP throughput by scale + +0.0 + +77.6 + +155.1 + +232.7 + +310.3 + +155.6 +fixed 1 + +284.3 +fixed 2 + +310.3 +fixed 4 + +93.2 +growth 1 + +168.8 +growth 2 + +306.1 +growth 4 +Successful requests per second + +fixed API budget + +growing API budget + diff --git a/evidence/track1/p95-latency.svg b/evidence/track1/p95-latency.svg new file mode 100644 index 00000000..ce0a6b99 --- /dev/null +++ b/evidence/track1/p95-latency.svg @@ -0,0 +1,37 @@ + + +Successful request p95 by scale + +0.0 + +69.4 + +138.7 + +208.1 + +277.5 + +121.3 +fixed 1 + +103.5 +fixed 2 + +145.9 +fixed 4 + +277.5 +growth 1 + +200.2 +growth 2 + +148.4 +growth 4 +Milliseconds + +fixed API budget + +growing API budget + diff --git a/evidence/track1/successful-rps.svg b/evidence/track1/successful-rps.svg new file mode 100644 index 00000000..1eef642c --- /dev/null +++ b/evidence/track1/successful-rps.svg @@ -0,0 +1,37 @@ + + +Successful HTTP throughput by scale + +0.0 + +86.7 + +173.4 + +260.0 + +346.7 + +162.0 +fixed 1 + +293.0 +fixed 2 + +346.6 +fixed 4 + +92.0 +growth 1 + +178.2 +growth 2 + +346.7 +growth 4 +Successful requests per second + +fixed API budget + +growing API budget + diff --git a/infra/README.md b/infra/README.md index 5a39e862..e5059103 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,7 +1,30 @@ -# Инфраструктура студента +# Выбранная инфраструктура: Compose + Nginx -Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. +Корневая топология находится в `compose.yaml`; конфигурация балансировщика — в `nginx.conf`. +`compose.example.yaml` и `contracts/infrastructure.json` остаются учебными материалами, +а не deployment. -В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. +| Роль | Compose service | Образ/сборка | Состояние и readiness | +| --- | --- | --- | --- | +| API | `api` (2 replicas by default; scale to 1/2/4) | Собирается из `Dockerfile` | `/health/ready`, каждые 3 s, 15 s stop grace | +| Load balancer | `nginx` | `nginx:1.27.5-alpine` | `/health/ready`; DNS refresh 5 s; passive fail exclusion | +| Database | `postgres` | `postgres:16.8-alpine` | `pg_isready`; named volume `postgres_data` | +| Session store | `redis` | `redis:7.4.2-alpine` | `PING`; AOF `everysec`; named volume `redis_data` | +| Migration runner | `migrate` | Собирается из `Dockerfile` | Однократный Compose job до запуска API replicas | -Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику. +PostgreSQL, Redis и API находятся в изолированной internal network. Только Nginx +публикует порт `${APP_PORT:-8080}` на host. Не задавайте `container_name`: Compose +масштабирует `api` как один service. Nginx OSS применяет passive checks: одна ошибка +соединения/таймаут либо 502/503/504 исключает backend на 2 s; backend DNS перечитывается +через Docker DNS каждые 5 s. Healthchecks API проверяют синхронные зависимости. + +Запуск с нуля и измерения описаны в корневом `REPORT.md`. Локальные пароль PostgreSQL +и URL подключения создаются `scripts/init-local-secrets.ps1` и передаются только +нужным контейнерам как Compose secrets. Redis не публикуется наружу и доступен только +в backend network. Все образы закреплены тегами; миграции применяются единственным +`migrate` service, до масштабируемых API. + +`scripts/restart.sh` / `.bat` перезапускает API и хранилища без удаления volumes. +`scripts/stop-one.sh` / `.bat` сопоставляет `TARGET_INSTANCE_ID` с контейнерным +hostname и останавливает только одну API-реплику; восстановление выполняется +оператором отдельно. diff --git a/infra/nginx.conf b/infra/nginx.conf new file mode 100644 index 00000000..83cb89b2 --- /dev/null +++ b/infra/nginx.conf @@ -0,0 +1,24 @@ +resolver 127.0.0.11 valid=5s ipv6=off; + +upstream messenger_api { + zone messenger_api 64k; + server api:8080 resolve max_fails=1 fail_timeout=2s; +} + +server { + listen 8080; + server_name _; + client_max_body_size 1m; + + location / { + proxy_pass http://messenger_api; + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_set_header Host $host; + proxy_connect_timeout 1s; + proxy_send_timeout 5s; + proxy_read_timeout 5s; + proxy_next_upstream error timeout http_502 http_503 http_504; + proxy_next_upstream_tries 2; + } +} diff --git a/migrations/001_schema.sql b/migrations/001_schema.sql new file mode 100644 index 00000000..a6187514 --- /dev/null +++ b/migrations/001_schema.sql @@ -0,0 +1,34 @@ +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +CREATE UNIQUE INDEX IF NOT EXISTS users_username_key ON users (username); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); + +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, + position INT NOT NULL, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX IF NOT EXISTS chat_members_user_id_idx ON chat_members (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 +); + +CREATE INDEX IF NOT EXISTS messages_chat_history_idx ON messages (chat_id, created_at ASC, id ASC); diff --git a/migrations/002_message_idempotency.sql b/migrations/002_message_idempotency.sql new file mode 100644 index 00000000..07e8b08b --- /dev/null +++ b/migrations/002_message_idempotency.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS message_idempotency ( + user_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE, + method TEXT NOT NULL CHECK (method = 'POST'), + resource_path TEXT NOT NULL, + idempotency_key VARCHAR(128) NOT NULL, + request_hash CHAR(64) NOT NULL, + response JSONB, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + expires_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (user_id, method, resource_path, idempotency_key) +); + +CREATE INDEX IF NOT EXISTS message_idempotency_expiry_idx + ON message_idempotency (user_id, expires_at); diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..bb6ebc72 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +argon2-cffi==23.1.0 +psycopg[binary,pool]==3.2.9 +redis==5.2.1 diff --git a/scripts/init-local-secrets.ps1 b/scripts/init-local-secrets.ps1 new file mode 100644 index 00000000..45623fc1 --- /dev/null +++ b/scripts/init-local-secrets.ps1 @@ -0,0 +1,38 @@ +$ErrorActionPreference = 'Stop' +$root = Split-Path -Parent $PSScriptRoot +$secrets = Join-Path $root 'secrets' +$passwordPath = Join-Path $secrets 'postgres_password' +$databaseUrlPath = Join-Path $secrets 'database_url' + +if (-not (Test-Path (Join-Path $root '.env'))) { + Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env') +} + +$hasPassword = Test-Path $passwordPath +$hasDatabaseUrl = Test-Path $databaseUrlPath +if ($hasPassword -or $hasDatabaseUrl) { + if ($hasPassword -and $hasDatabaseUrl) { + Write-Output 'Local secrets already exist; leaving them unchanged.' + exit 0 + } + throw 'The local secret set is incomplete. Restore both files from the same setup; do not rotate a password against an existing volume.' +} + +New-Item -ItemType Directory -Force $secrets | Out-Null +$bytes = New-Object byte[] 32 +$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() +try { + $rng.GetBytes($bytes) +} +finally { + $rng.Dispose() +} +$builder = New-Object System.Text.StringBuilder +foreach ($byte in $bytes) { + [void]$builder.Append($byte.ToString('x2')) +} +$password = $builder.ToString() +[System.IO.File]::WriteAllText($passwordPath, $password, [System.Text.Encoding]::ASCII) +$databaseUrl = 'postgresql://messenger:' + $password + '@postgres:5432/messenger' +[System.IO.File]::WriteAllText($databaseUrlPath, $databaseUrl, [System.Text.Encoding]::ASCII) +Write-Output 'Created local Compose secrets. Their values were not printed.' diff --git a/scripts/migrate.py b/scripts/migrate.py new file mode 100644 index 00000000..6ead0bbf --- /dev/null +++ b/scripts/migrate.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +"""Apply database migrations once before API replicas start.""" +import logging +from pathlib import Path +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from db import Store + + +def main(): + logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s') + store = Store.connect() + try: + logging.info('database migrations are current') + finally: + store.close() + + +if __name__ == '__main__': + main() diff --git a/scripts/restart.bat b/scripts/restart.bat new file mode 100644 index 00000000..4b2fc2be --- /dev/null +++ b/scripts/restart.bat @@ -0,0 +1,5 @@ +@echo off +setlocal +cd /d "%~dp0.." +docker compose restart api postgres redis +exit /b %errorlevel% diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..d29bed84 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +docker compose restart api postgres redis diff --git a/scripts/rolling_restart.py b/scripts/rolling_restart.py new file mode 100644 index 00000000..99fcfae8 --- /dev/null +++ b/scripts/rolling_restart.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 +"""Restart API replicas one at a time while the public load test is running.""" +import argparse +import json +import os +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +os.environ.setdefault('PYTHONUTF8', '1') + + +def run(command, capture=False, timeout=None): + result = subprocess.run( + command, + cwd=ROOT, + text=True, + capture_output=capture, + check=False, + timeout=timeout, + ) + if result.returncode: + detail = (result.stderr or result.stdout or '').strip() + raise RuntimeError('Command failed: ' + ' '.join(command) + '\n' + detail) + return result.stdout.strip() if capture else '' + + +def wait_healthy(container_id, timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + status = run( + [ + 'docker', + 'inspect', + '--format', + '{{.State.Health.Status}}', + container_id, + ], + capture=True, + ) + if status == 'healthy': + return + time.sleep(0.5) + raise TimeoutError('API replica did not become healthy after restart') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--base-url', default='http://localhost:8080') + parser.add_argument('--duration', type=int, default=40) + parser.add_argument('--concurrency', type=int, default=16) + parser.add_argument('--output', default='evidence/rolling-restart.json') + args = parser.parse_args() + if args.duration < 10 or args.concurrency < 1: + parser.error('duration must be at least 10 seconds and concurrency must be positive') + + containers = run( + ['docker', 'compose', 'ps', '-q', 'api'], capture=True + ).splitlines() + if len(containers) < 2: + parser.error('At least two running API replicas are required') + + output = (ROOT / args.output).resolve() + output.parent.mkdir(parents=True, exist_ok=True) + load_output = output.with_name(output.stem + '-load.json') + load_process = subprocess.Popen( + [ + sys.executable, + str(ROOT / 'tests' / 'load.py'), + '--base-url', + args.base_url, + '--duration', + str(args.duration), + '--concurrency', + str(args.concurrency), + '--output', + str(load_output), + ], + cwd=ROOT, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + restarts = [] + try: + time.sleep(2) + for container_id in containers: + instance_id = run( + [ + 'docker', + 'inspect', + '--format', + '{{.Config.Hostname}}', + container_id, + ], + capture=True, + ) + started = time.monotonic() + run(['docker', 'restart', '--timeout', '15', container_id], timeout=30) + wait_healthy(container_id) + restarts.append( + { + 'instance_id': instance_id, + 'ready_seconds': round(time.monotonic() - started, 3), + } + ) + stdout, stderr = load_process.communicate(timeout=max(90, args.duration + 30)) + except Exception: + if load_process.poll() is None: + load_process.terminate() + load_process.wait(timeout=5) + raise + if load_process.returncode: + raise RuntimeError('Load test failed:\n' + (stderr or stdout).strip()) + + report = { + 'operation': 'serial API replica restart under load; same image, no schema change', + 'duration_seconds': args.duration, + 'concurrency': args.concurrency, + 'restarted_replicas': restarts, + 'load': json.loads(load_output.read_text(encoding='utf-8')), + 'load_raw_file': load_output.name, + 'limitations': [ + 'This verifies graceful process replacement, not a new-image rolling deployment.', + 'PostgreSQL and Redis are single instances; their failure is not covered.', + ], + } + output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') + print(json.dumps(report['load'], ensure_ascii=False, indent=2)) + print('Saved serial restart evidence to ' + str(output)) + + +if __name__ == '__main__': + try: + main() + except (OSError, RuntimeError, TimeoutError, ValueError, json.JSONDecodeError) as error: + print('ERROR: ' + str(error), file=sys.stderr) + raise SystemExit(1) diff --git a/scripts/rolling_update.py b/scripts/rolling_update.py new file mode 100644 index 00000000..08542ece --- /dev/null +++ b/scripts/rolling_update.py @@ -0,0 +1,241 @@ +#!/usr/bin/env python3 +"""Build a new API image and replace replicas one at a time under load.""" +import argparse +import json +import os +from pathlib import Path +import shlex +import subprocess +import sys +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +BASE_URL = os.environ.get('BASE_URL', 'http://localhost:8080') +COMPOSE = shlex.split(os.environ.get('COMPOSE', 'docker compose')) +os.environ.setdefault('PYTHONUTF8', '1') + + +def run(args, capture=True, timeout=None): + result = subprocess.run( + args, + cwd=ROOT, + text=True, + capture_output=capture, + check=False, + timeout=timeout, + ) + if result.returncode: + detail = (result.stderr or result.stdout or '').strip() + raise RuntimeError('Operation failed: ' + ' '.join(args[:3]) + '\n' + detail) + return result.stdout.strip() if capture else '' + + +def compose(*args, capture=True, timeout=None): + return run(COMPOSE + ['-f', str(ROOT / 'compose.yaml'), *args], capture, timeout) + + +def inspect(container_id): + return json.loads(run(['docker', 'inspect', container_id]))[0] + + +def wait_healthy(container_id, timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + state = inspect(container_id)['State'] + if state['Status'] == 'exited': + raise RuntimeError('Replacement API container exited before becoming healthy') + if state.get('Health', {}).get('Status') == 'healthy': + return + time.sleep(0.5) + raise TimeoutError('Replacement API container did not become healthy') + + +def wait_routed_to(instance_id, base_url, timeout=20): + sys.path.insert(0, str(ROOT / 'tests')) + from client import Client, Failure + + client = Client(base_url, timeout=5) + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + response = client.call( + 'GET', '/api/v1/users/me', expected=401, schema='Error' + ) + if response['headers'].get('X-Instance-Id') == instance_id: + return + except Failure: + pass + time.sleep(0.1) + raise TimeoutError('Nginx did not route a request to the new API container') + + +def candidate_command(old, image_id, name): + config = old['Config'] + host_config = old['HostConfig'] + networks = old['NetworkSettings']['Networks'] + if len(networks) != 1: + raise RuntimeError('Expected API to use exactly one private network') + network = next(iter(networks)) + args = [ + 'docker', 'run', '--detach', + '--name', name, + '--network', network, + '--network-alias', 'api', + '--restart', 'no', + '--stop-timeout', '15', + ] + if config.get('User'): + args += ['--user', config['User']] + if host_config.get('NanoCpus'): + args += ['--cpus', str(host_config['NanoCpus'] / 1_000_000_000)] + if host_config.get('Memory'): + args += ['--memory', str(host_config['Memory'])] + for env in config.get('Env', []): + args += ['--env', env] + for mount in old.get('Mounts', []): + if mount['Destination'] != '/run/secrets/database_url': + raise RuntimeError('Unexpected API mount; refusing to copy deployment state') + args += [ + '--mount', + 'type=bind,source=%s,target=%s,readonly' + % (mount['Source'], mount['Destination']), + ] + args.append(image_id) + return args + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--base-url', default=BASE_URL) + parser.add_argument('--duration', type=int, default=90) + parser.add_argument('--concurrency', type=int, default=16) + parser.add_argument('--output', default='evidence/rolling-update.json') + args = parser.parse_args() + if args.duration < 30 or args.concurrency < 1: + parser.error('duration must be at least 30 seconds and concurrency must be positive') + + if not (ROOT / 'secrets' / 'database_url').is_file(): + parser.error('Run scripts/init-local-secrets.ps1 before the update') + old_ids = compose('ps', '-q', 'api').splitlines() + if len(old_ids) < 2: + parser.error('At least two running API replicas are required') + + old_image_ids = {inspect(container_id)['Image'] for container_id in old_ids} + image = inspect(old_ids[0])['Config']['Image'] + revision = 'lab3-' + uuid.uuid4().hex[:12] + run( + [ + 'docker', + 'build', + '--build-arg', + 'BUILD_REVISION=' + revision, + '--tag', + image, + '.', + ], + capture=False, + timeout=300, + ) + image_id = run(['docker', 'image', 'inspect', '--format', '{{.Id}}', image]) + if image_id in old_image_ids: + raise RuntimeError('The candidate API image is unchanged; refusing a no-op update') + compose('run', '--rm', 'migrate', capture=False, timeout=120) + + output = (ROOT / args.output).resolve() + output.parent.mkdir(parents=True, exist_ok=True) + load_output = output.with_name(output.stem + '-load.json') + load_process = subprocess.Popen( + [ + sys.executable, + str(ROOT / 'tests' / 'load.py'), + '--base-url', + args.base_url, + '--duration', + str(args.duration), + '--concurrency', + str(args.concurrency), + '--output', + str(load_output), + ], + cwd=ROOT, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + candidates = [] + replacements = [] + try: + time.sleep(2) + for index, old_id in enumerate(old_ids, start=1): + old = inspect(old_id) + instance_id = old['Config']['Hostname'] + candidate_name = 'lab3-api-candidate-' + uuid.uuid4().hex[:10] + started = time.monotonic() + candidate_result = run( + candidate_command(old, image_id, candidate_name), + timeout=60, + ) + candidate_id = candidate_result.splitlines()[0] + candidates.append(candidate_id) + new_instance_id = inspect(candidate_id)['Config']['Hostname'] + wait_healthy(candidate_id) + wait_routed_to(new_instance_id, args.base_url) + run(['docker', 'stop', '--timeout', '15', old_id], timeout=30) + run(['docker', 'rm', old_id]) + replacements.append( + { + 'old_instance_id': instance_id, + 'new_instance_id': new_instance_id, + 'ready_and_routed_seconds': round(time.monotonic() - started, 3), + 'sequence': index, + } + ) + + compose( + 'up', '--detach', '--scale', 'api=2', '--no-deps', 'api', + capture=False, timeout=120, + ) + managed_ids = compose('ps', '-q', 'api').splitlines() + if len(managed_ids) != 2: + raise RuntimeError('Compose did not restore exactly two managed API replicas') + for container_id in managed_ids: + wait_healthy(container_id) + for candidate_id in candidates: + run(['docker', 'rm', '--force', candidate_id]) + stdout, stderr = load_process.communicate(timeout=max(90, args.duration + 30)) + except Exception: + if load_process.poll() is None: + load_process.terminate() + load_process.wait(timeout=5) + raise + if load_process.returncode: + raise RuntimeError('Load test failed:\n' + (stderr or stdout).strip()) + + report = { + 'operation': 'new API image; serial container replacement with temporary capacity surge', + 'duration_seconds': args.duration, + 'concurrency': args.concurrency, + 'new_image_id': image_id, + 'build_revision': revision, + 'old_image_ids': sorted(old_image_ids), + 'replacements': replacements, + 'load': json.loads(load_output.read_text(encoding='utf-8')), + 'load_raw_file': load_output.name, + 'limitations': [ + 'The schema migration in this exercise is additive and backward-compatible.', + 'PostgreSQL and Redis are single instances and remain SPOFs.', + ], + } + output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') + print(json.dumps(report['load'], ensure_ascii=False, indent=2)) + print('Saved rolling update evidence to ' + str(output)) + + +if __name__ == '__main__': + try: + main() + except (OSError, RuntimeError, TimeoutError, ValueError, json.JSONDecodeError) as error: + print('ERROR: ' + str(error), file=sys.stderr) + raise SystemExit(1) diff --git a/scripts/stop-one.bat b/scripts/stop-one.bat new file mode 100644 index 00000000..8746b836 --- /dev/null +++ b/scripts/stop-one.bat @@ -0,0 +1,21 @@ +@echo off +setlocal +cd /d "%~dp0.." +if "%TARGET_INSTANCE_ID%"=="" ( + echo TARGET_INSTANCE_ID is required 1>&2 + exit /b 2 +) +set TARGET_STOPPED= +for /f "delims=" %%i in ('docker compose ps -q api') do if not defined TARGET_STOPPED call :stop_if_target %%i +if defined TARGET_STOPPED exit /b 0 +echo No running API replica matches TARGET_INSTANCE_ID 1>&2 +exit /b 1 + +:stop_if_target +for /f "delims=" %%h in ('docker inspect --format "{{.Config.Hostname}}" %1') do if "%%h"=="%TARGET_INSTANCE_ID%" ( + docker stop --time 2 %1 >nul + if errorlevel 1 exit /b 1 + set TARGET_STOPPED=1 + exit /b 0 +) +exit /b 1 diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..c957ceb3 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" +cd "$(dirname "$0")/.." + +for container_id in $(docker compose ps -q api); do + instance_id=$(docker inspect --format '{{.Config.Hostname}}' "$container_id") + if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then + docker stop --time 2 "$container_id" >/dev/null + exit 0 + fi +done + +printf '%s\n' 'No running API replica matches TARGET_INSTANCE_ID' >&2 +exit 1 diff --git a/scripts/track1_benchmark.py b/scripts/track1_benchmark.py new file mode 100644 index 00000000..9c6f4a14 --- /dev/null +++ b/scripts/track1_benchmark.py @@ -0,0 +1,524 @@ +#!/usr/bin/env python3 +"""Compare API replica counts under fixed and increasing total API resources.""" +import argparse +import json +import os +from pathlib import Path +import platform +import re +import shlex +import subprocess +import sys +import tempfile +import time + +ROOT = Path(__file__).resolve().parents[1] +BASE_URL = os.environ.get('BASE_URL', 'http://localhost:8080') +COMPOSE = shlex.split(os.environ.get('COMPOSE', 'docker compose')) +LOAD_SCRIPT = ROOT / 'tests' / 'load.py' +MEMORY_PATTERN = re.compile(r'([\d.]+)\s*(B|KiB|MiB|GiB|kB|MB|GB)', re.IGNORECASE) +os.environ.setdefault('PYTHONUTF8', '1') + + +def command(args, overlay=None, check=True, capture=False): + argv = COMPOSE + ['--env-file', str(ROOT / '.env')] + if overlay: + argv += ['--env-file', str(overlay)] + argv += ['-f', str(ROOT / 'compose.yaml')] + list(args) + result = subprocess.run( + argv, + cwd=ROOT, + check=False, + text=True, + capture_output=capture, + ) + if check and result.returncode: + detail = (result.stderr or result.stdout or '').strip() + raise RuntimeError('Compose command failed: ' + ' '.join(args) + '\n' + detail) + return result + + +def wait_ready(timeout=90): + from urllib.error import URLError + from urllib.request import urlopen + + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + with urlopen(BASE_URL.rstrip('/') + '/health/ready', timeout=2) as response: + if response.status == 200: + return + except (OSError, URLError): + time.sleep(0.5) + raise TimeoutError('Load balancer did not become ready within %s seconds' % timeout) + + +def sample_instances(minimum, timeout=15): + sys.path.insert(0, str(ROOT / 'tests')) + from client import Client, Failure + + client = Client(BASE_URL, timeout=5) + instances = set() + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + for _ in range(40): + try: + response = client.call( + 'GET', + '/api/v1/users/me', + expected=401, + schema='Error', + ) + instances.add(response['headers']['X-Instance-Id']) + except Failure: + time.sleep(0.1) + if len(instances) >= minimum: + return instances + time.sleep(1) + raise TimeoutError( + 'Expected %d API instance IDs through Nginx; observed %d' + % (minimum, len(instances)) + ) + + +def parse_memory(value): + match = MEMORY_PATTERN.search(value) + if not match: + return 0 + amount = float(match.group(1)) + unit = match.group(2).lower() + factors = { + 'b': 1, + 'kib': 1024, + 'mib': 1024 ** 2, + 'gib': 1024 ** 3, + 'kb': 1000, + 'mb': 1000 ** 2, + 'gb': 1000 ** 3, + } + return int(amount * factors[unit]) + + +def collect_stats(): + result = command(['ps', '-q'], check=True, capture=True) + container_ids = result.stdout.split() + if not container_ids: + return {} + stats = subprocess.run( + [ + 'docker', + 'stats', + '--no-stream', + '--format', + '{{.Name}};{{.CPUPerc}};{{.MemUsage}}', + *container_ids, + ], + cwd=ROOT, + text=True, + capture_output=True, + check=False, + ) + if stats.returncode: + raise RuntimeError('docker stats failed: ' + (stats.stderr or stats.stdout).strip()) + values = {} + for line in stats.stdout.splitlines(): + parts = line.split(';', 2) + if len(parts) != 3: + continue + name, cpu, memory = parts + service = next( + (item for item in ('api', 'postgres', 'nginx') if '-' + item + '-' in name), + None, + ) + if service: + values.setdefault(service, []).append( + { + 'cpu_percent': float(cpu.rstrip('%') or 0), + 'memory_bytes': parse_memory(memory.split('/')[0]), + } + ) + return values + + +def run_load(duration, concurrency, output=None): + args = [ + sys.executable, + str(LOAD_SCRIPT), + '--base-url', + BASE_URL, + '--duration', + str(duration), + '--concurrency', + str(concurrency), + ] + if output: + args += ['--output', str(output)] + return subprocess.run( + args, + cwd=ROOT, + text=True, + capture_output=True, + check=False, + ) + + +def measure(duration, concurrency, output): + process = subprocess.Popen( + [ + sys.executable, + str(LOAD_SCRIPT), + '--base-url', + BASE_URL, + '--duration', + str(duration), + '--concurrency', + str(concurrency), + '--output', + str(output), + ], + cwd=ROOT, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + peaks = {} + try: + while process.poll() is None: + for service, entries in collect_stats().items(): + values = peaks.setdefault( + service, {'cpu_percent': 0.0, 'memory_bytes': 0} + ) + values['cpu_percent'] = max( + values['cpu_percent'], + sum(entry['cpu_percent'] for entry in entries), + ) + values['memory_bytes'] = max( + values['memory_bytes'], + sum(entry['memory_bytes'] for entry in entries), + ) + time.sleep(1) + stdout, stderr = process.communicate() + finally: + if process.poll() is None: + process.terminate() + process.wait(timeout=5) + if process.returncode: + raise RuntimeError('Load run failed:\n' + (stderr or stdout).strip()) + return json.loads(output.read_text(encoding='utf-8')), peaks + + +def run_scenario(name, replicas, cpu_per_replica, memory_per_replica, args, output_dir): + with tempfile.NamedTemporaryFile( + mode='w', + encoding='ascii', + suffix='.env', + prefix='lab3-track1-', + delete=False, + ) as handle: + handle.write('API_REPLICAS=%s\n' % replicas) + handle.write('API_CPU_LIMIT=%s\n' % cpu_per_replica) + handle.write('API_MEMORY_LIMIT=%sM\n' % memory_per_replica) + override = Path(handle.name) + try: + command( + ['up', '--detach', '--build', '--scale', 'api=%d' % replicas], + overlay=override, + capture=True, + ) + wait_ready() + instances = sample_instances(min(2, replicas)) + running = command(['ps', '-q', 'api'], capture=True).stdout.split() + if len(running) != replicas: + raise RuntimeError( + 'Expected %d running API replicas, found %d' % (replicas, len(running)) + ) + + warmup = run_load(args.warmup, args.concurrency) + if warmup.returncode: + raise RuntimeError('Warm-up load failed: ' + warmup.stderr.strip()) + + runs = [] + for repeat in range(1, args.repeats + 1): + raw_path = output_dir / ('%s-run-%02d.json' % (name, repeat)) + metrics, peaks = measure( + args.duration, + args.concurrency, + raw_path, + ) + runs.append({'load': metrics, 'peak_resources': peaks, 'raw_file': raw_path.name}) + return { + 'name': name, + 'api_replicas': replicas, + 'api_cpu_limit_per_replica': cpu_per_replica, + 'api_memory_limit_mib_per_replica': memory_per_replica, + 'total_api_cpu_limit': round(replicas * cpu_per_replica, 3), + 'total_api_memory_limit_mib': replicas * memory_per_replica, + 'instances_observed': len(instances), + 'runs': runs, + } + finally: + override.unlink(missing_ok=True) + + +def mean(values): + return sum(values) / len(values) if values else 0 + + +def summarize(scenario): + runs = scenario['runs'] + return { + 'name': scenario['name'], + 'api_replicas': scenario['api_replicas'], + 'total_api_cpu_limit': scenario['total_api_cpu_limit'], + 'total_api_memory_limit_mib': scenario['total_api_memory_limit_mib'], + 'successful_rps_mean': round( + mean([run['load']['successful_rps'] for run in runs]), 3 + ), + 'error_ratio_mean': round( + mean([run['load']['error_ratio'] or 0 for run in runs]), 6 + ), + 'p95_latency_ms_mean': round( + mean([run['load']['successful_latency_ms']['p95'] or 0 for run in runs]), + 3, + ), + 'api_cpu_peak_percent_mean': round( + mean([run['peak_resources'].get('api', {}).get('cpu_percent', 0) for run in runs]), + 3, + ), + 'api_memory_peak_mib_mean': round( + mean( + [ + run['peak_resources'].get('api', {}).get('memory_bytes', 0) + / (1024 ** 2) + for run in runs + ] + ), + 3, + ), + 'postgres_cpu_peak_percent_mean': round( + mean( + [ + run['peak_resources'].get('postgres', {}).get('cpu_percent', 0) + for run in runs + ] + ), + 3, + ), + 'postgres_memory_peak_mib_mean': round( + mean( + [ + run['peak_resources'].get('postgres', {}).get('memory_bytes', 0) + / (1024 ** 2) + for run in runs + ] + ), + 3, + ), + 'nginx_cpu_peak_percent_mean': round( + mean([run['peak_resources'].get('nginx', {}).get('cpu_percent', 0) for run in runs]), + 3, + ), + 'nginx_memory_peak_mib_mean': round( + mean( + [ + run['peak_resources'].get('nginx', {}).get('memory_bytes', 0) + / (1024 ** 2) + for run in runs + ] + ), + 3, + ), + } + + +def write_chart(path, summaries, value_key, title, unit): + width, height = 980, 430 + left, top, chart_width, chart_height = 70, 55, 850, 275 + maximum = max((item[value_key] for item in summaries), default=1) or 1 + parts = [ + '' + % (width, height, width, height), + '', + '%s' + % (left, title), + ] + for tick in range(5): + y = top + chart_height - chart_height * tick / 4 + value = maximum * tick / 4 + parts.append( + '' + % (left, y, left + chart_width, y) + ) + parts.append( + '%.1f' + % (y + 4, value) + ) + bar_width = 72 + slot = chart_width / len(summaries) + for index, item in enumerate(summaries): + value = item[value_key] + bar_height = chart_height * value / maximum + x = left + slot * index + (slot - bar_width) / 2 + y = top + chart_height - bar_height + color = '#2864a5' if item['name'].startswith('fixed') else '#d47a22' + label = item['name'].replace('fixed-total-', 'fixed ').replace('growing-total-', 'growth ') + parts.append( + '' + % (x, y, bar_width, bar_height, color) + ) + parts.append( + '%.1f' + % (x + bar_width / 2, max(top + 14, y - 6), value) + ) + parts.append( + '%s' + % (x + bar_width / 2, top + chart_height + 22, label) + ) + parts.extend( + [ + '%s' + % (left, height - 14, unit), + '', + 'fixed API budget', + '', + 'growing API budget', + '', + ] + ) + path.write_text('\n'.join(parts) + '\n', encoding='utf-8') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--duration', type=int, default=30) + parser.add_argument('--warmup', type=int, default=5) + parser.add_argument('--concurrency', type=int, default=16) + parser.add_argument('--repeats', type=int, default=3) + parser.add_argument('--fixed-total-cpus', type=float, default=4.0) + parser.add_argument('--fixed-total-memory-mib', type=int, default=2048) + parser.add_argument('--growth-cpus-per-replica', type=float, default=1.0) + parser.add_argument('--growth-memory-mib-per-replica', type=int, default=512) + parser.add_argument('--output', default='evidence/track1/benchmark.json') + args = parser.parse_args() + if ( + args.duration < 1 + or args.warmup < 1 + or args.concurrency < 1 + or args.repeats < 1 + or args.fixed_total_cpus <= 0 + or args.fixed_total_memory_mib < 1 + or args.growth_cpus_per_replica <= 0 + or args.growth_memory_mib_per_replica < 1 + ): + parser.error('duration, warmup, concurrency, repeats and resource limits must be positive') + if not (ROOT / '.env').is_file(): + parser.error('Create .env and local Compose secrets first') + if not all((ROOT / 'secrets' / name).is_file() for name in ('postgres_password', 'database_url')): + parser.error('Run scripts/init-local-secrets.ps1 before benchmarking') + + output = (ROOT / args.output).resolve() + output.parent.mkdir(parents=True, exist_ok=True) + command(['config', '--quiet']) + output_dir = output.parent + scenarios = [] + try: + for replicas in (1, 2, 4): + fixed_cpu = round(args.fixed_total_cpus / replicas, 3) + fixed_memory = max(1, args.fixed_total_memory_mib // replicas) + scenarios.append( + run_scenario( + 'fixed-total-%d' % replicas, + replicas, + fixed_cpu, + fixed_memory, + args, + output_dir, + ) + ) + for replicas in (1, 2, 4): + scenarios.append( + run_scenario( + 'growing-total-%d' % replicas, + replicas, + args.growth_cpus_per_replica, + args.growth_memory_mib_per_replica, + args, + output_dir, + ) + ) + finally: + command(['up', '--detach', '--build', '--scale', 'api=2'], capture=True) + wait_ready() + + docker_version = subprocess.run( + ['docker', 'version', '--format', '{{.Server.Version}}'], + cwd=ROOT, + text=True, + capture_output=True, + check=False, + ) + docker_host = subprocess.run( + ['docker', 'info', '--format', '{{.NCPU}}|{{.MemTotal}}|{{.OperatingSystem}}'], + cwd=ROOT, + text=True, + capture_output=True, + check=False, + ) + docker_host_values = docker_host.stdout.strip().split('|') + if docker_host.returncode or len(docker_host_values) != 3: + raise RuntimeError('Unable to capture Docker host CPU/RAM information') + report = { + 'track': '1 — цена масштабирования', + 'method': { + 'workload': 'tests/load.py closed-loop POST messages through Nginx', + 'duration_seconds_per_run': args.duration, + 'warmup_seconds_per_scenario': args.warmup, + 'concurrency': args.concurrency, + 'repeats_per_scenario': args.repeats, + 'fixed_total_api_limits': { + 'cpus': args.fixed_total_cpus, + 'memory_mib': args.fixed_total_memory_mib, + }, + 'growth_per_replica_limits': { + 'cpus': args.growth_cpus_per_replica, + 'memory_mib': args.growth_memory_mib_per_replica, + }, + 'host_cpu_count': os.cpu_count(), + 'platform': platform.platform(), + 'docker_server_version': docker_version.stdout.strip(), + 'docker_host': { + 'cpus': int(docker_host_values[0]), + 'memory_mib': round(int(docker_host_values[1]) / (1024 ** 2), 3), + 'operating_system': docker_host_values[2], + }, + 'measurement_note': ( + 'Successful HTTP throughput, not committed-write throughput. ' + 'Load generator is closed-loop. Docker stats are one-second samples ' + 'and can miss short CPU peaks.' + ), + }, + 'scenarios': scenarios, + 'summary': [summarize(scenario) for scenario in scenarios], + 'raw_runs': True, + } + summary = [summarize(scenario) for scenario in scenarios] + rps_chart = output_dir / 'successful-rps.svg' + p95_chart = output_dir / 'p95-latency.svg' + write_chart(rps_chart, summary, 'successful_rps_mean', 'Successful HTTP throughput by scale', 'Successful requests per second') + write_chart(p95_chart, summary, 'p95_latency_ms_mean', 'Successful request p95 by scale', 'Milliseconds') + report['summary'] = summary + report['charts'] = [rps_chart.name, p95_chart.name] + output.write_text( + json.dumps(report, ensure_ascii=False, indent=2) + '\n', + encoding='utf-8', + ) + print(json.dumps(summary, ensure_ascii=False, indent=2)) + print('Saved repeatable benchmark evidence to ' + str(output)) + + +if __name__ == '__main__': + try: + main() + except (OSError, RuntimeError, TimeoutError, ValueError, json.JSONDecodeError) as error: + print('ERROR: ' + str(error), file=sys.stderr) + raise SystemExit(1) diff --git a/sessions.py b/sessions.py new file mode 100644 index 00000000..e579fb04 --- /dev/null +++ b/sessions.py @@ -0,0 +1,43 @@ +import os + +import redis + + +class SessionStore: + def __init__(self, url, connect_timeout=2.0, socket_timeout=2.0): + self._url = url + self._client = redis.from_url( + url, + decode_responses=True, + socket_connect_timeout=connect_timeout, + socket_timeout=socket_timeout, + health_check_interval=30, + ) + self._prefix = 'sess:' + + @classmethod + def from_env(cls): + url = _read_config('CACHE_URL_FILE', 'CACHE_URL') + if not url: + raise RuntimeError('CACHE_URL or CACHE_URL_FILE is required when SESSION_STORE=redis') + return cls(url) + + def ping(self): + return self._client.ping() + + def create(self, token, user_id, ttl_seconds): + self._client.setex(self._prefix + token, ttl_seconds, user_id) + + def get_user_id(self, token): + return self._client.get(self._prefix + token) + + def revoke(self, token): + return self._client.delete(self._prefix + token) > 0 + + +def _read_config(file_env, value_env): + path = os.environ.get(file_env) + if path and os.path.isfile(path): + with open(path, encoding='utf-8') as handle: + return handle.read().strip() + return os.environ.get(value_env, '').strip() diff --git a/student-tests/test_lab3.py b/student-tests/test_lab3.py new file mode 100644 index 00000000..e2248e49 --- /dev/null +++ b/student-tests/test_lab3.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +"""Student acceptance checks for shared idempotency across API replicas.""" +from concurrent.futures import ThreadPoolExecutor +import os +import sys +import unittest +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client + + +BASE = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/') + + +class Lab3IdempotencyTests(unittest.TestCase): + def setUp(self): + self.client = Client(BASE) + self.alice, self.bob, _eve, self.chat = self.client.bootstrap() + self.key = 'test-' + uuid.uuid4().hex + + def test_same_key_and_body_replay_same_message(self): + first = self.client.message(self.alice, self.chat, 'replay me', key=self.key) + replay = self.client.message(self.alice, self.chat, 'replay me', key=self.key) + self.assertEqual(first['json'], replay['json']) + + def test_same_key_with_different_body_conflicts(self): + self.client.message(self.alice, self.chat, 'original', key=self.key) + response = self.client.call( + 'POST', + '/api/v1/chats/' + self.chat['id'] + '/messages', + 409, + actor=self.alice, + body={'text': 'different'}, + headers={'Idempotency-Key': self.key}, + ) + self.assertEqual(response['json']['error']['code'], 'conflict') + + def test_key_is_required_and_validated(self): + path = '/api/v1/chats/' + self.chat['id'] + '/messages' + self.client.call( + 'POST', path, 400, actor=self.alice, body={'text': 'missing key'} + ) + self.client.call( + 'POST', + path, + 400, + actor=self.alice, + body={'text': 'invalid key'}, + headers={'Idempotency-Key': 'not valid'}, + ) + + def test_concurrent_retries_create_one_message(self): + text = 'concurrent-' + uuid.uuid4().hex + with ThreadPoolExecutor(max_workers=16) as pool: + results = list(pool.map( + lambda _index: self.client.message( + self.alice, self.chat, text, key=self.key + ), + range(16), + )) + + messages = [result['json'] for result in results] + self.assertTrue(all(message == messages[0] for message in messages)) + instances = {result['headers'].get('X-Instance-Id') for result in results} + self.assertGreaterEqual(len(instances), 2) + + history = self.client.call( + 'GET', + '/api/v1/chats/' + self.chat['id'] + '/messages?limit=100', + actor=self.bob, + schema='MessagePage', + )['json']['items'] + self.assertEqual( + sum(message['text'] == text for message in history), + 1, + ) + + +if __name__ == '__main__': + unittest.main()