diff --git a/.env.example b/.env.example index d32b9be0..b85ec288 100644 --- a/.env.example +++ b/.env.example @@ -10,4 +10,18 @@ DATABASE_URL_FILE=/run/secrets/database_url SESSION_STORE=postgres CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 +# Per-replica CPU limit. Series A keeps replicas * API_CPU_LIMIT = 1.0. +API_CPU_LIMIT=0.50 +# Per-replica memory. Track 1 keeps replicas * API_MEM_LIMIT = 512m. +API_MEM_LIMIT=256m +# Base profile for docker compose up. scripts/run_track1.py passes the same defaults. +HTTP_WORKERS=8 +DB_POOL_SIZE=8 +NGINX_WORKERS=1 +# PostgreSQL cgroup CPU limit. +DB_CPU_LIMIT=1.0 +# Nginx cgroup CPU limit. 0.25 saturated in the track-1 plateau runs. +LB_CPU_LIMIT=0.25 IDEMPOTENCY_TTL_SECONDS=86400 +# Seconds /health/ready stays 503 after SIGTERM before the listen socket closes. +DRAIN_SECONDS=2 diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..9b380646 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.16) +project(mtusi_messenger CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(Threads REQUIRED) +find_package(PkgConfig REQUIRED) + +pkg_check_modules(PQXX REQUIRED IMPORTED_TARGET libpqxx>=7.9) +pkg_check_modules(HIREDIS REQUIRED IMPORTED_TARGET hiredis) +pkg_check_modules(SODIUM REQUIRED IMPORTED_TARGET libsodium) + +add_executable(messenger + src/main.cpp + src/db.cpp + src/auth.cpp + src/user_repo.cpp + src/chat_repo.cpp + src/message_repo.cpp + src/redis_pool.cpp + src/session_repo.cpp + src/pg_session_repo.cpp +) + +target_include_directories(messenger PRIVATE src) + +target_link_libraries(messenger PRIVATE + Threads::Threads + PkgConfig::PQXX + PkgConfig::HIREDIS + PkgConfig::SODIUM +) \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4683e4f6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +FROM ubuntu:25.10 AS builder + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + cmake \ + pkg-config \ + libpqxx-dev \ + libpq-dev \ + libhiredis-dev \ + libsodium-dev \ + nlohmann-json3-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +COPY CMakeLists.txt ./ +COPY src ./src +COPY migrations ./migrations + +RUN cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \ + && cmake --build build -j + +FROM ubuntu:25.10 + +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + libpqxx-7.10 \ + libhiredis1.1.0 \ + libsodium23 \ + && rm -rf /var/lib/apt/lists/* \ + && useradd --system --no-create-home --uid 10001 app + +WORKDIR /app +COPY --from=builder /src/build/messenger /app/messenger +COPY --from=builder /src/migrations /app/migrations + +RUN chown -R app:app /app + +USER 10001 + +ENV APP_HOST=0.0.0.0 +ENV APP_PORT=8080 + +EXPOSE 8080 + +HEALTHCHECK --interval=5s --timeout=3s --start-period=20s --retries=3 \ + CMD curl -fsS http://127.0.0.1:8080/health/live || exit 1 + +CMD ["/app/messenger"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index c2679005..edba236f 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,149 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Никитин Андрей Александрович БВТ2301 +- Целевая оценка: 5. Трек 1 (цена масштабирования) +- Commit/PR: не публиковался +- Источники и помощь: контракт и публичные тесты курса; PostgreSQL 16, Valkey, Nginx, libpqxx, libsodium, hiredis, nlohmann/json. Помощь ИИ: DeepSeek, Cursor, GigaCode. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Хост этого прогона: 12th Gen Intel Core i5-12450H, 12 потоков, 14 GiB RAM. Образы: `postgres:16.15-alpine`, `valkey/valkey:8.1.10-alpine`, `nginx:1.28.0-alpine`, приложение собирается из `ubuntu:25.10` (g++ 15.2, libpqxx 7.10, libsodium 1.0.18, hiredis 1.2.0). + +Лимиты: db 1 CPU / 512 MiB, cache 0.5 CPU / 256 MiB, каждая реплика api 0.5 CPU / 256 MiB, lb 0.25 CPU / 128 MiB. Проверено через `docker inspect` (`NanoCpus` / `Memory`). + +```sh +cp .env.example .env +# В .env задать POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, SESSION_STORE=redis +mkdir -p secrets +printf 'postgresql://USER:PASSWORD@db:5432/DB\n' > secrets/database_url +printf 'redis://cache:6379/0\n' > secrets/cache_url +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +# Дождаться healthy у lb, затем: +make test +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +docker compose --env-file .env stop +``` + +Хосты в секретах — имена сервисов Compose (`db`, `cache`), не loopback: внутри контейнера loopback не является базой. Секреты и `.env` не коммитятся. Остановка без удаления данных: `docker compose --env-file .env stop`. Удаление учебных volumes — только явно: `docker compose --env-file .env down -v`. + +Миграции применяет процесс API при старте, под сессионным `pg_advisory_lock`. Отдельный шаг миграции не нужен. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Клиент ходит только на `lb:8080`. Две реплики `api` порты наружу не публикуют. Общая PostgreSQL хранит пользователей, чаты, сообщения и `idempotency_keys`. Сессии — Valkey (`SESSION_STORE=redis`, AOF `everysec`, volume `valkeydata`). Sticky session нет. + +`X-Instance-Id` — `INSTANCE_ID`, иначе `HOSTNAME` контейнера, иначе 8 hex. В Compose `HOSTNAME` совпадает с `Config.Hostname` (короткий id контейнера). Nginx этот заголовок и `X-Request-Id` не подменяет. + +Идемпотентность `POST /api/v1/chats/{id}/messages`: в одной транзакции `INSERT messages` и `INSERT idempotency_keys ... ON CONFLICT (user_id, chat_id, key) DO NOTHING RETURNING`. Проигравшая транзакция откатывает своё сообщение и читает уже зафиксированную строку. Тот же семантический текст — тот же `message_id` и 201. Другой текст — 409. Другой чат того же пользователя — независимый ключ. Хеш — SHA-256 текста; в контракте lab3 у тела одно поле `text`, отсутствующий `attachment_ids` в хеше считается пустым. TTL — `IDEMPOTENCY_TTL_SECONDS` (86400). 5xx до COMMIT не записывается. + +`/health/live` — процесс жив. `/health/ready` — PostgreSQL и Valkey отвечают. По SIGTERM ready сразу 503, слушающий сокет закрывается через `DRAIN_SECONDS` (2). `stop_grace_period` API — 15 с. Docker healthcheck образа — `/health/live`, `--start-period=20s`. Healthcheck Compose у api — `/health/ready`, `start_period: 20s` (он подменяет HEALTHCHECK образа при запуске через Compose). + +Nginx 1.28: `resolver 127.0.0.11 valid=2s`, `server api:8080 resolve`, `max_fails=1 fail_timeout=5s`, `keepalive 32`, `proxy_next_upstream error timeout http_502 http_503 http_504`, `proxy_connect_timeout 2s`, `proxy_read_timeout 10s`. Активного health_check, как в Nginx Plus, в этом образе нет: недоступный backend уходит с маршрута после ошибки соединения или 502/503/504. Пока реплика дренируется и ещё отвечает 201, трафик на неё возможен; после закрытия сокета следующий запрос уходит на вторую реплику. + +Из lab2 сохранены регистрация, Basic → Bearer, Argon2id, чаты, keyset-пагинация, logout, пул PostgreSQL с `statement_timeout` и `is_alive()`. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +| Критерий | Команда | Наблюдение | Файл | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Контракт и синтаксис | `make check` | PASS | вывод команды | +| OpenAPI 3.1 | `make validate PYTHON=.venv/bin/python` | PASS | вывод команды | +| Compose | `docker compose --env-file .env config --quiet` | код 0 | — | +| Две реплики и один вход | `docker compose --env-file .env ps` | api ×2 без публикации порта; наружу только `lb` `0.0.0.0:8080` | — | +| Разные реплики, одна сессия | 20× `GET /health/live` через :8080 | `X-Instance-Id`: `a54079f2595c` и `f9a279e85405` | — | +| Smoke | `make test` | PASS, lab3 | — | +| Идемпотентность, гонка, другой чат | `python3 student-tests/idempotency_parallel.py` | 8 ответов, 1 message id, 2 instance id, другой чат — другой id, другой body — 409 | — | +| Общий ключ переживает реплики | тот же скрипт, ответы с двух instance id | один id | — | +| Отказ одной реплики | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | два прогона, оба остановили api-2: до отказа 2 instance; успехи 128 и 128; ошибок 0; первое успешное после возврата hook 3.381 с и 3.369 с | — | +| Вторая реплика тем же скриптом | `TARGET_INSTANCE_ID=a54079f2595c scripts/stop-one.sh` во время записей | hook 0 за окно 15 с; успехи 130; ошибок 0; first recovery 3.134 с; последние id только `f9a279e85405`; контейнер api-1 `Exited (0)` | — | +| Сохранность | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS. После hook все пять контейнеров снова Up (healthy), volumes не удалялись | — | +| SIGTERM и ready | `docker kill --signal=TERM` одной реплики, `curl` из контейнера | сразу ready=503, live=200, сокет ещё принимает; через ~3 с процесс `Exited (0)`, в логе drain 2 с, затем `shutting down` | — | +| Миграции один раз | логи двух реплик при старте | у обеих `migration advisory lock acquired`; строка `applied migration 4` одна | — | +| Лимиты | `docker inspect` | api 5e8 / 268435456, db 1e9 / 536870912, cache 5e8 / 268435456, lb 2.5e8 / 134217728 | — | +| Поочерёдная замена под нагрузкой | `python3 tests/load.py --duration 36 --concurrency 4 --output evidence/load-rolling.json` и `docker stop`/`start` каждой реплики внутри окна | 36.028 с, concurrency 4, успехов 7398, ошибок 0, successful RPS 205.34, p50 11.798 мс, p95 55.641 мс, p99 87.623 мс. Это closed-loop без retry, латентность только успешных | `evidence/load-rolling.json` | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Период проверки Compose: interval 5 с, timeout 3 с. У Nginx обнаружение мёртвого peer — первый отказ соединения (connect timeout до 2 с) и `fail_timeout` 5 с. Наблюдаемое время от старта `stop-one.sh` до следующей успешной записи после возврата скрипта — около 3.1–3.4 с; в эти окна ошибок записи не было. Скрипт сам занимает ~3 с, потому что ждёт `docker stop -t 8`, а процесс ещё 2 с отдаёт ready=503. ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Нагрузка — публичный `tests/load.py`, один прогон, не серия. Цифры ниже сняты на этом хосте 2026-10-23 и не являются пределом машины. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +| Воздействие | Что ожидали | Что наблюдали | Восстановление | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `stop-one.sh` для api-2, публичный failover, два раза | ≥2 реплики до отказа, запись ≤10 с, остановленная реплика не в хвосте ответов, без дублей | 2 реплики; ошибок 0; 128 успешных записей в каждом прогоне | 3.381 с и 3.369 с от старта hook | +| Тот же скрипт для hostname api-1 | то же | ошибок 0; 130 успешных; хвост только вторая реплика | 3.134 с | +| Поочерёдный stop/start обеих реплик во время load.py 36 с | краткие ошибки допустимы | ошибок 0; RPS 205.34; p50/p95/p99 11.798/55.641/87.623 мс | отдельный таймер восстановления внутри этого прогона не выделялся | +| SIGTERM одной реплики | ready перестаёт быть 200 до закрытия сокета | ready 503 при ещё живом live и сокете; выход 0 | процесс завершился примерно за 3 с | +| `restart.sh` db, cache, api, lb | прежний токен и сообщения, volumes на месте | `make test-persistence` PASS, все сервисы снова healthy | hook уложился в лимит теста (90 с) | + +## Трек 1. Цена масштабирования + +Гипотеза. При суммарном CPU API = 1.0 и суммарной RAM API = 512 MiB дополнительные реплики не поднимают RPS: каждая реплика упирается в свой кусок квоты. Пока квота PostgreSQL заполнена, лишний CPU базы поднимает RPS; когда квота API и Nginx уже полные, дальнейший CPU базы даёт меньше. + +Методика. Публичный `tests/load.py`, closed-loop `POST /messages`, `--duration 30 --concurrency 50`. Перед каждым зачётным окном отброшенный прогрев 10 с с теми же 50 воркерами. Каждый запуск создаёт свой чат. `docker stats` снимался примерно раз в 3 с только во время 30 с; в выводе Docker 100% — это одно ядро. Скрипт `scripts/run_track1.py` на каждую точку делает `docker compose down` и `docker compose up -d --build`, передаёт переменные окружения и до нагрузки сверяет `docker inspect`. + +Методика воспроизводима: суммарные ресурсы фиксировались через переменные окружения и проверялись через docker inspect. Базовые значения (8 потоков, пул 8, один процесс Nginx) восстановлены в коде по умолчанию. `HTTP_WORKERS` и `DB_POOL_SIZE` читаются процессом API, `NGINX_WORKERS` подставляется в `lb/nginx.conf.template`. Без этих переменных `docker compose up` поднимает тот же профиль 8/8/1 и 256 MiB на реплику. В сериях A и B суммарная RAM API всегда 512 MiB: 1×512m, 2×256m, 4×128m. Скрипт записывает прогон только если `HostConfig.NanoCpus` и `HostConfig.Memory` совпали с этими значениями, сумма Memory API равна 536870912, в логе каждой реплики есть `pg pool ready (8 connections)` и `http workers: 8`, а `nginx -T` показывает `worker_processes 1;`. + +Фиксировано во всех точках серий A и B: `shared_buffers=256MB`, `synchronous_commit=off`, `max_connections=200`, память db 512 MiB, cache 0.5 CPU, квота lb 0.25 CPU, пул PostgreSQL 8, 8 потоков HTTP на реплику, Nginx `worker_processes 1` и `worker_connections 1024`. Хост: Intel Core i5-12450H, 12 потоков, 14 GiB RAM. Сырые JSON: `evidence/track1-A-*.json` и `evidence/track1-B-*.json`. + +### Серия A. Реплики API, суммарный CPU = 1.0, суммарная RAM = 512 MiB, CPU базы = 2.0 + +| Реплики | CPU на реплику | RAM на реплику | Успешный RPS | p50, мс | p95, мс | p99, мс | Ошибки | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 1 | 1.0 | 512 MiB | 378.703 | 90.288 | 204.67 | 1124.75 | 21 | +| 2 | 0.5 | 256 MiB | 346.098 | 128.325 | 269.126 | 380.157 | 0 | +| 4 | 0.25 | 128 MiB | 327.486 | 102.151 | 487.734 | 626.914 | 0 | + +Сумма RAM по inspect на каждой точке — 536870912 байт. NanoCpus реплик: 1000000000, 500000000 и 250000000. От 1 к 2 репликам RPS −8.6%, от 1 к 4 — −13.5%. На одной реплике API держал максимум 105% при квоте 1 ядро, база — 212% при квоте 2 ядра, Nginx — 28% при квоте 0.25 ядра. В этом окне 21 неуспешный запрос из 11649 (error ratio 0.0018); успешный RPS считается только по 11628 ответам. На четырёх репликах максимум API по контейнеру был 27.2% при квоте 0.25 ядра, ошибок не было. + +![Серия A: RPS и p99 от числа реплик](evidence/api_scaling.png) + +Рост числа реплик при том же суммарном CPU и той же суммарной RAM API не помогает. Запрос по-прежнему ограничен долей ядра на процесс. p99 одной реплики (1125 мс) — длинный хвост в том же окне. + +### Серия B. CPU PostgreSQL, API зафиксирован: 4 × 0.25 CPU и 4 × 128 MiB + +| CPU базы | Успешный RPS | p50, мс | p95, мс | p99, мс | Ошибки | Кто упёрся в квоту | +| --- | --- | --- | --- | --- | --- | --- | +| 1.0 | 178.949 | 286.653 | 480.072 | 538.062 | 0 | база, max 103% | +| 2.0 | 334.227 | 117.124 | 366.356 | 455.007 | 0 | база max 191%; API max 28.2%; lb max 25.8% | +| 4.0 | 348.996 | 110.449 | 394.75 | 479.704 | 0 | API max 27.9%; lb max 26.0%; база max 204% из 400% | + +На всех трёх точках inspect показал 4 контейнера API по NanoCpus 250000000 и Memory 134217728, сумма 536870912. NanoCpus базы: 1000000000, 2000000000, 4000000000. + +Второе ядро базы относительно первого: 334.227 / 178.949 − 1 = **+86.8% RPS**. Это подтверждённое улучшение: при 1.0 CPU база была на квоте, API (max 19%) и Nginx (max 12%) — нет. Четвёртое ядро относительно двух: 348.996 / 334.227 − 1 = **+4.4%**. База при лимите 4.0 использовала максимум около 2.0 ядра, а API и Nginx уже стояли на своих квотах. Поэтому дальнейший CPU базы почти не поднимает RPS. + +![Серия B: RPS и p99 от CPU базы](evidence/db_scaling.png) + +### Тюнинг Nginx и пула + +Строки ниже сняты до параметризации: память была 256 MiB на реплику, а пул и потоки были зашиты в бинарник. Текущий `scripts/run_track1.py` эти точки не повторяет. Зачётные серии — таблицы A и B выше. + +Проверялось ровно то, что предлагалось как снятие узкого места: `worker_processes auto`, `worker_connections 4096`, пул `PgPool(..., 32)`. В первом таком прогоне потоков HTTP было 8, поэтому из 32 соединений реплика одновременно занимала не больше восьми. + +Сразу после серии B тот же тюнинг вместе с квотой lb 2.0 CPU дал 336.611 RPS. Повтор исходной точки серии B (пул 8, один процесс Nginx, lb 0.25, база 4.0 CPU), сделанный позже, дал уже не 417.528, а **344.846** RPS (p50 129.553, p95 307.781, p99 397.828). Абсолютные 417.528 в конце сессии не воспроизвелись: долгий прогон на одном ноутбуке смешал сравнение. Зачётный вывод по тюнингу — только парная пара, снятая подряд: + +| Конфигурация | RPS | p50 | p95 | p99 | +| --- | --- | --- | --- | --- | +| Повтор плато: пул 8, Nginx 1 процесс, 8 потоков HTTP, lb 0.25 CPU | 344.846 | 129.553 | 307.781 | 397.828 | +| Сразу после: пул 32, `worker_processes auto`, 8 потоков HTTP, lb 0.25 CPU | 350.038 | 120.943 | 334.087 | 396.982 | +| Та же квота, пул 32, Nginx `auto`, **32 потока HTTP** | 273.854 | 176.149 | 373.138 | 519.185 | + +Разница пула и Nginx при 8 потоках: (350.038 − 344.846) / 344.846 = **+1.5%**. p99 397.828 и 396.982. Это не прирост пропускной способности. Числа 384 RPS в этих прогонах не было; сравнивать тюнинг с ним нельзя. + +Снятие потолка потоков (`workers_n` 8 → 32) при том же пуле 32, Nginx `auto`, 4 × 0.25 CPU API и 4.0 CPU базы: (273.854 − 350.038) / 350.038 = **−21.8% RPS**. p50 120.943 → 176.149 мс, p99 396.982 → 519.185 мс, ошибок 0. База при этом занята меньше (среднее 110% и максимум 179% против 130% и 204% при 8 потоках), то есть лишние соединения пула не начали работать. Квота API по-прежнему выбрана: максимум 27.6% при лимите 0.25 ядра на реплику. 32 потока делят четверть ядра и добавляют переключения и ожидание на мьютексах, а CPU, которым можно было бы держать больше запросов к базе, не появляется. Ограничение в 8 потоков не было причиной простоя пула. + +Плато воспроизводимой серии B снимается вторым ядром PostgreSQL (+86.8%, от 178.949 до 334.227). Квота API в 1.0 CPU на все реплики при этом не менялась. Более ранний тюнинг пула и числа процессов Nginx на незафиксированной RAM дал +1.5% и −21.8% и зачётным результатом не является. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Отказ одного процесса API выдерживается: запись продолжается через вторую реплику, подтверждённые сообщения остаются. Не выдерживается потеря этого Docker-хоста, единственной PostgreSQL, Valkey или Nginx. Несколько API не делают базу и балансировщик высокодоступными. + +Nginx OSS не опрашивает `/health/ready` сам. Готовность для LB — это 502/503/504 и обрыв соединения плюс `max_fails`. Окно drain (2 с) реплика ещё может ответить 201 со своим `X-Instance-Id`. + +`tests/load.py` — closed-loop, без прозрачных retry, процентили только успешных запросов. Серия трека 1 — отдельные окна по 30 с при concurrency 50; один ранний прогон на 36 с в таблице отказов к ней не относится. В воспроизводимой серии A точка с одной репликой дала 21 неуспешный запрос; остальные пять окон серии A и B — без ошибок. + +Сессии живут в Valkey с `appendfsync everysec`: теоретически последняя секунда записи может не попасть на диск при жёстком kill cache. `restart.sh` шлёт обычный SIGTERM, persistence-тест с этим прошёл. + +Поле `attachment_ids` контракт lab3 запрещает (`additionalProperties: false`). Когда оно появится в lab4, хеш идемпотентности нужно расширить, не меняя смысл уже сохранённых ключей. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..fa813393 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,106 @@ +services: + db: + image: postgres:16.15-alpine + command: postgres -c max_connections=200 -c shared_buffers=256MB -c synchronous_commit=off + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] + interval: 5s + timeout: 3s + retries: 20 + start_period: 10s + cpus: ${DB_CPU_LIMIT:-1.0} + mem_limit: 512m + stop_grace_period: 20s + + cache: + image: valkey/valkey:8.1.10-alpine + command: ["valkey-server", "--appendonly", "yes", "--appendfsync", "everysec"] + volumes: + - valkeydata:/data + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 20 + start_period: 5s + cpus: "0.50" + mem_limit: 256m + stop_grace_period: 15s + + api: + build: . + image: mtusi2026lab3-api:local + depends_on: + db: + condition: service_healthy + cache: + condition: service_healthy + environment: + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + SESSION_STORE: ${SESSION_STORE:-redis} + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + DRAIN_SECONDS: ${DRAIN_SECONDS:-2} + HTTP_WORKERS: ${HTTP_WORKERS:-8} + DB_POOL_SIZE: ${DB_POOL_SIZE:-8} + secrets: + - database_url + - cache_url + healthcheck: + test: ["CMD", "curl", "-fsS", "http://127.0.0.1:8080/health/ready"] + interval: 5s + timeout: 3s + retries: 12 + start_period: 20s + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: ${API_CPU_LIMIT:-0.50} + memory: ${API_MEM_LIMIT:-256m} + cpus: ${API_CPU_LIMIT:-0.50} # для совместимости с docker compose up + mem_limit: ${API_MEM_LIMIT:-256m} + stop_grace_period: 15s + + lb: + image: nginx:1.28.0-alpine + depends_on: + api: + condition: service_healthy + ports: + - "8080:8080" + environment: + NGINX_WORKERS: ${NGINX_WORKERS:-1} + NGINX_ENVSUBST_TEMPLATE_DIR: /etc/nginx/templates + NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx + NGINX_ENVSUBST_FILTER: "^NGINX_" + volumes: + - ./lb/nginx.conf.template:/etc/nginx/templates/nginx.conf.template:ro + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"] + interval: 5s + timeout: 3s + retries: 12 + start_period: 10s + cpus: ${LB_CPU_LIMIT:-0.25} + mem_limit: 128m + stop_grace_period: 10s + +secrets: + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + +volumes: + pgdata: {} + valkeydata: {} diff --git a/evidence/api_scaling.png b/evidence/api_scaling.png new file mode 100644 index 00000000..0977f050 Binary files /dev/null and b/evidence/api_scaling.png differ diff --git a/evidence/db_scaling.png b/evidence/db_scaling.png new file mode 100644 index 00000000..b2c5b5cf Binary files /dev/null and b/evidence/db_scaling.png differ diff --git a/evidence/load-rolling.json b/evidence/load-rolling.json new file mode 100644 index 00000000..f7a99916 --- /dev/null +++ b/evidence/load-rolling.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 36.0, + "elapsed_seconds": 36.028, + "concurrency": 4, + "successful_requests": 7398, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 205.34, + "successful_latency_ms": { + "p50": 11.798, + "p95": 55.641, + "p99": 87.623 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.json b/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.json new file mode 100644 index 00000000..cb96c15f --- /dev/null +++ b/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.json @@ -0,0 +1,75 @@ +{ + "series": "A", + "api_replicas": 1, + "api_cpu_limit": 1.0, + "api_mem_limit": "512m", + "api_mem_limit_bytes": 536870912, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 2.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 1000000000, + "memory": 536870912, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 2000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 6, + "mean_cpu_percent": 26.55, + "max_cpu_percent": 28.44 + }, + "api": { + "samples": 6, + "mean_cpu_percent": 97.93, + "max_cpu_percent": 105.29 + }, + "db": { + "samples": 6, + "mean_cpu_percent": 198.84, + "max_cpu_percent": 212.46 + }, + "cache": { + "samples": 6, + "mean_cpu_percent": 9.22, + "max_cpu_percent": 17.24 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.705, + "concurrency": 50, + "successful_requests": 11628, + "failed_requests": 21, + "error_ratio": 0.001803, + "successful_rps": 378.703, + "successful_latency_ms": { + "p50": 90.288, + "p95": 204.67, + "p99": 1124.75 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.load.json b/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.load.json new file mode 100644 index 00000000..9a1c5086 --- /dev/null +++ b/evidence/track1-A-r1-apicpu1.0-dbcpu2.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.705, + "concurrency": 50, + "successful_requests": 11628, + "failed_requests": 21, + "error_ratio": 0.001803, + "successful_rps": 378.703, + "successful_latency_ms": { + "p50": 90.288, + "p95": 204.67, + "p99": 1124.75 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.json b/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.json new file mode 100644 index 00000000..9e1be4a6 --- /dev/null +++ b/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.json @@ -0,0 +1,82 @@ +{ + "series": "A", + "api_replicas": 2, + "api_cpu_limit": 0.5, + "api_mem_limit": "256m", + "api_mem_limit_bytes": 268435456, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 2.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 500000000, + "memory": 268435456, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-2", + "nano_cpus": 500000000, + "memory": 268435456, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 2000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 6, + "mean_cpu_percent": 24.03, + "max_cpu_percent": 26.81 + }, + "api": { + "samples": 12, + "mean_cpu_percent": 43.88, + "max_cpu_percent": 53.96 + }, + "db": { + "samples": 6, + "mean_cpu_percent": 166.58, + "max_cpu_percent": 192.08 + }, + "cache": { + "samples": 6, + "mean_cpu_percent": 7.98, + "max_cpu_percent": 16.26 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.231, + "concurrency": 50, + "successful_requests": 10463, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 346.098, + "successful_latency_ms": { + "p50": 128.325, + "p95": 269.126, + "p99": 380.157 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.load.json b/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.load.json new file mode 100644 index 00000000..a9c7c7a5 --- /dev/null +++ b/evidence/track1-A-r2-apicpu0.5-dbcpu2.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.231, + "concurrency": 50, + "successful_requests": 10463, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 346.098, + "successful_latency_ms": { + "p50": 128.325, + "p95": 269.126, + "p99": 380.157 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-A-r4-apicpu0.25-dbcpu2.0.json b/evidence/track1-A-r4-apicpu0.25-dbcpu2.0.json new file mode 100644 index 00000000..eac65335 --- /dev/null +++ b/evidence/track1-A-r4-apicpu0.25-dbcpu2.0.json @@ -0,0 +1,96 @@ +{ + "series": "A", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "api_mem_limit": "128m", + "api_mem_limit_bytes": 134217728, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 2.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-2", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-3", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-4", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 2000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 17.02, + "max_cpu_percent": 25.95 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 20.06, + "max_cpu_percent": 27.19 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 139.19, + "max_cpu_percent": 210.12 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 10.01, + "max_cpu_percent": 17.01 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.276, + "concurrency": 50, + "successful_requests": 9915, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 327.486, + "successful_latency_ms": { + "p50": 102.151, + "p95": 487.734, + "p99": 626.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-A-r4-apicpu0.25-dbcpu2.0.load.json b/evidence/track1-A-r4-apicpu0.25-dbcpu2.0.load.json new file mode 100644 index 00000000..afb70d92 --- /dev/null +++ b/evidence/track1-A-r4-apicpu0.25-dbcpu2.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.276, + "concurrency": 50, + "successful_requests": 9915, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 327.486, + "successful_latency_ms": { + "p50": 102.151, + "p95": 487.734, + "p99": 626.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-B-r4-apicpu0.25-dbcpu1.0.json b/evidence/track1-B-r4-apicpu0.25-dbcpu1.0.json new file mode 100644 index 00000000..ecb2e1b5 --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu1.0.json @@ -0,0 +1,96 @@ +{ + "series": "B", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "api_mem_limit": "128m", + "api_mem_limit_bytes": 134217728, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 1.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-2", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-3", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-4", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 1000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 8.62, + "max_cpu_percent": 11.8 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 11.86, + "max_cpu_percent": 19.43 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 75.17, + "max_cpu_percent": 102.55 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 6.74, + "max_cpu_percent": 11.3 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.333, + "concurrency": 50, + "successful_requests": 5428, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 178.949, + "successful_latency_ms": { + "p50": 286.653, + "p95": 480.072, + "p99": 538.062 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-B-r4-apicpu0.25-dbcpu1.0.load.json b/evidence/track1-B-r4-apicpu0.25-dbcpu1.0.load.json new file mode 100644 index 00000000..d596aed6 --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu1.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.333, + "concurrency": 50, + "successful_requests": 5428, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 178.949, + "successful_latency_ms": { + "p50": 286.653, + "p95": 480.072, + "p99": 538.062 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.json b/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.json new file mode 100644 index 00000000..33815e42 --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.json @@ -0,0 +1,96 @@ +{ + "series": "B", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "api_mem_limit": "128m", + "api_mem_limit_bytes": 134217728, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 2.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-2", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-3", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-4", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 2000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 16.89, + "max_cpu_percent": 25.75 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 20.09, + "max_cpu_percent": 28.18 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 131.47, + "max_cpu_percent": 190.53 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 9.64, + "max_cpu_percent": 19.24 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.491, + "concurrency": 50, + "successful_requests": 10191, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 334.227, + "successful_latency_ms": { + "p50": 117.124, + "p95": 366.356, + "p99": 455.007 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.load.json b/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.load.json new file mode 100644 index 00000000..d6946a15 --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu2.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.491, + "concurrency": 50, + "successful_requests": 10191, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 334.227, + "successful_latency_ms": { + "p50": 117.124, + "p95": 366.356, + "p99": 455.007 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..40d3b83f --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,96 @@ +{ + "series": "B", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "api_mem_limit": "128m", + "api_mem_limit_bytes": 134217728, + "api_mem_total_bytes": 536870912, + "db_cpu_limit": 4.0, + "http_workers": 8, + "pg_pool": 8, + "nginx_worker_processes": "1", + "lb_cpu_limit": 0.25, + "warmup_seconds": 10, + "inspect": { + "verified": true, + "api": [ + { + "name": "mtusi2026lab3-api-1", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-2", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-3", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + }, + { + "name": "mtusi2026lab3-api-4", + "nano_cpus": 250000000, + "memory": 134217728, + "http_workers": "8", + "db_pool_size": "8" + } + ], + "api_memory_sum": 536870912, + "db": { + "name": "mtusi2026lab3-db-1", + "nano_cpus": 4000000000 + }, + "lb": { + "name": "mtusi2026lab3-lb-1", + "nano_cpus": 250000000, + "nginx_worker_processes": "worker_processes 1;" + } + }, + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 17.63, + "max_cpu_percent": 25.98 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 19.88, + "max_cpu_percent": 27.94 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 138.77, + "max_cpu_percent": 203.66 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 10.56, + "max_cpu_percent": 17.54 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.31, + "concurrency": 50, + "successful_requests": 10578, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 348.996, + "successful_latency_ms": { + "p50": 110.449, + "p95": 394.75, + "p99": 479.704 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..e7f1a607 --- /dev/null +++ b/evidence/track1-B-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.31, + "concurrency": 50, + "successful_requests": 10578, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 348.996, + "successful_latency_ms": { + "p50": 110.449, + "p95": 394.75, + "p99": 479.704 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..458173fd --- /dev/null +++ b/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "C", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 32, + "nginx_worker_processes": "auto", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-4 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 2000000000 map[com.docker.compose.config-hash:ee74ad04250c4d0cecb5a9de33f8114c96c67a0f78b3ffe89a116b43dcd81889 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 20.96, + "max_cpu_percent": 31.95 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 18.62, + "max_cpu_percent": 28.28 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 126.42, + "max_cpu_percent": 194.27 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 7.12, + "max_cpu_percent": 14.51 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.67, + "concurrency": 50, + "successful_requests": 10324, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 336.611, + "successful_latency_ms": { + "p50": 63.068, + "p95": 484.84, + "p99": 646.969 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..7ca51b42 --- /dev/null +++ b/evidence/track1-C-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.67, + "concurrency": 50, + "successful_requests": 10324, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 336.611, + "successful_latency_ms": { + "p50": 63.068, + "p95": 484.84, + "p99": 646.969 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..11164f31 --- /dev/null +++ b/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "L", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 32, + "nginx_worker_processes": "1", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-4 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 2000000000 map[com.docker.compose.config-hash:ee74ad04250c4d0cecb5a9de33f8114c96c67a0f78b3ffe89a116b43dcd81889 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "api": { + "samples": 28, + "mean_cpu_percent": 20.7, + "max_cpu_percent": 28.12 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 144.54, + "max_cpu_percent": 213.13 + }, + "lb": { + "samples": 7, + "mean_cpu_percent": 20.99, + "max_cpu_percent": 32.27 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 10.94, + "max_cpu_percent": 15.58 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.397, + "concurrency": 50, + "successful_requests": 10394, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 341.943, + "successful_latency_ms": { + "p50": 102.179, + "p95": 395.06, + "p99": 469.992 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..80428de3 --- /dev/null +++ b/evidence/track1-L-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.397, + "concurrency": 50, + "successful_requests": 10394, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 341.943, + "successful_latency_ms": { + "p50": 102.179, + "p95": 395.06, + "p99": 469.992 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..1fab218d --- /dev/null +++ b/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "M", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 8, + "nginx_worker_processes": "1", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-4 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 2000000000 map[com.docker.compose.config-hash:ee74ad04250c4d0cecb5a9de33f8114c96c67a0f78b3ffe89a116b43dcd81889 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "api": { + "samples": 28, + "mean_cpu_percent": 18.97, + "max_cpu_percent": 26.66 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 122.03, + "max_cpu_percent": 207.51 + }, + "lb": { + "samples": 7, + "mean_cpu_percent": 23.57, + "max_cpu_percent": 37.27 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 7.21, + "max_cpu_percent": 8.48 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.564, + "concurrency": 50, + "successful_requests": 10770, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 352.371, + "successful_latency_ms": { + "p50": 61.017, + "p95": 464.208, + "p99": 548.228 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..6dc5e406 --- /dev/null +++ b/evidence/track1-M-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.564, + "concurrency": 50, + "successful_requests": 10770, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 352.371, + "successful_latency_ms": { + "p50": 61.017, + "p95": 464.208, + "p99": 548.228 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..d0f397b3 --- /dev/null +++ b/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "P", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 32, + "nginx_worker_processes": "auto", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:4f45fcaea2d11a10950043c988f2a695fd303df9eb5fb23b59b4c1a0649d3e01 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-4 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 250000000 map[com.docker.compose.config-hash:8afd1a6fe04af86c6e4c4bc0c6be3e01db2e2c692a325086cc477df1a110aebb com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "api": { + "samples": 28, + "mean_cpu_percent": 19.25, + "max_cpu_percent": 28.2 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 130.03, + "max_cpu_percent": 203.72 + }, + "lb": { + "samples": 7, + "mean_cpu_percent": 16.92, + "max_cpu_percent": 26.19 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 10.12, + "max_cpu_percent": 16.62 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.254, + "concurrency": 50, + "successful_requests": 10590, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 350.038, + "successful_latency_ms": { + "p50": 120.943, + "p95": 334.087, + "p99": 396.982 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..bcebbd68 --- /dev/null +++ b/evidence/track1-P-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.254, + "concurrency": 50, + "successful_requests": 10590, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 350.038, + "successful_latency_ms": { + "p50": 120.943, + "p95": 334.087, + "p99": 396.982 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..9d9ddf46 --- /dev/null +++ b/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "R", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 8, + "nginx_worker_processes": "1", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:a1ec572a54fa12c0ab467a444062c9a59e7bc804b20247d743295cee651b6fdc com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-4 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 250000000 map[com.docker.compose.config-hash:8afd1a6fe04af86c6e4c4bc0c6be3e01db2e2c692a325086cc477df1a110aebb com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "lb": { + "samples": 7, + "mean_cpu_percent": 16.95, + "max_cpu_percent": 25.67 + }, + "api": { + "samples": 28, + "mean_cpu_percent": 19.34, + "max_cpu_percent": 28.21 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 131.5, + "max_cpu_percent": 199.27 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 6.82, + "max_cpu_percent": 8.9 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.382, + "concurrency": 50, + "successful_requests": 10477, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 344.846, + "successful_latency_ms": { + "p50": 129.553, + "p95": 307.781, + "p99": 397.828 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..4c4b126e --- /dev/null +++ b/evidence/track1-R-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.382, + "concurrency": 50, + "successful_requests": 10477, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 344.846, + "successful_latency_ms": { + "p50": 129.553, + "p95": 307.781, + "p99": 397.828 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.json b/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.json new file mode 100644 index 00000000..9c3d5a9b --- /dev/null +++ b/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.json @@ -0,0 +1,57 @@ +{ + "series": "W", + "api_replicas": 4, + "api_cpu_limit": 0.25, + "db_cpu_limit": 4.0, + "pg_pool": 32, + "nginx_worker_processes": "auto", + "warmup_seconds": 10, + "inspect": [ + "/mtusi2026lab3-api-1 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:d988ea1050459c3d472f843911e369dfc75cd07559a35c5dad61ec255718dfec com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-1 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-2 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:2 com.docker.compose.depends_on: com.docker.compose.image:sha256:d988ea1050459c3d472f843911e369dfc75cd07559a35c5dad61ec255718dfec com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:api-2 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-3 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:3 com.docker.compose.depends_on: com.docker.compose.image:sha256:d988ea1050459c3d472f843911e369dfc75cd07559a35c5dad61ec255718dfec com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-api-4 250000000 map[com.docker.compose.config-hash:baa6c518a5d0d2ce1a2d38ddcfa206234dfa8023729d82b99b3f7264efaf99b4 com.docker.compose.container-number:4 com.docker.compose.depends_on: com.docker.compose.image:sha256:d988ea1050459c3d472f843911e369dfc75cd07559a35c5dad61ec255718dfec com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:api com.docker.compose.version:5.5.1 org.opencontainers.image.version:25.10]", + "/mtusi2026lab3-cache-1 500000000 map[com.docker.compose.config-hash:a7d737a55154622d18c1f35f33d3767c8335e00fb23f230f1afca917e900252d com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:32627109abf6f741121096b45c732f758876803efd7b2e1018ebc0350d117119 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.service:cache com.docker.compose.version:5.5.1 org.opencontainers.image.source:https://github.com/valkey-io/valkey]", + "/mtusi2026lab3-db-1 4000000000 map[com.docker.compose.config-hash:00262326412a2d2388e383afd1f1c5d3453baeebf2e459788f9a579b8340f994 com.docker.compose.container-number:1 com.docker.compose.depends_on: com.docker.compose.image:sha256:cbf42f0edc946c0bc1b8eeed62403734e1f25dc67a3491455d07a92fdb558b28 com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:db-1 com.docker.compose.service:db com.docker.compose.version:5.5.1]", + "/mtusi2026lab3-lb-1 250000000 map[com.docker.compose.config-hash:8afd1a6fe04af86c6e4c4bc0c6be3e01db2e2c692a325086cc477df1a110aebb com.docker.compose.container-number:1 com.docker.compose.depends_on:api:service_healthy:false com.docker.compose.image:sha256:09ab424a8c788f8d0fe3a64429f6d19dfa526885c8609b748d0943a75dcb9f8c com.docker.compose.oneoff:False com.docker.compose.project:mtusi2026lab3 com.docker.compose.project.config_files:/home/andrey/mtusi2026lab3/compose.yaml com.docker.compose.project.environment_file:/home/andrey/mtusi2026lab3/.env com.docker.compose.project.working_dir:/home/andrey/mtusi2026lab3 com.docker.compose.replace:lb-1 com.docker.compose.service:lb com.docker.compose.version:5.5.1 maintainer:NGINX Docker Maintainers ]" + ], + "cpu": { + "api": { + "samples": 28, + "mean_cpu_percent": 17.31, + "max_cpu_percent": 27.56 + }, + "db": { + "samples": 7, + "mean_cpu_percent": 110.05, + "max_cpu_percent": 178.78 + }, + "lb": { + "samples": 7, + "mean_cpu_percent": 19.02, + "max_cpu_percent": 26.49 + }, + "cache": { + "samples": 7, + "mean_cpu_percent": 5.58, + "max_cpu_percent": 12.24 + } + }, + "load": { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.301, + "concurrency": 50, + "successful_requests": 8298, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 273.854, + "successful_latency_ms": { + "p50": 176.149, + "p95": 373.138, + "p99": 519.185 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." + } +} diff --git a/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.load.json b/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.load.json new file mode 100644 index 00000000..17a1b963 --- /dev/null +++ b/evidence/track1-W-r4-apicpu0.25-dbcpu4.0.load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.301, + "concurrency": 50, + "successful_requests": 8298, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 273.854, + "successful_latency_ms": { + "p50": 176.149, + "p95": 373.138, + "p99": 519.185 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/lb/nginx.conf.template b/lb/nginx.conf.template new file mode 100644 index 00000000..a4843605 --- /dev/null +++ b/lb/nginx.conf.template @@ -0,0 +1,39 @@ +worker_processes ${NGINX_WORKERS}; +error_log /dev/stderr warn; +pid /tmp/nginx.pid; + +events { + worker_connections 1024; +} + +http { + access_log off; + resolver 127.0.0.11 valid=2s ipv6=off; + resolver_timeout 2s; + + upstream api_upstream { + zone api_upstream 64k; + server api:8080 resolve max_fails=1 fail_timeout=5s; + keepalive 32; + } + + server { + listen 8080; + + location / { + proxy_pass http://api_upstream; + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-Host $host; + proxy_next_upstream error timeout http_502 http_503 http_504; + proxy_next_upstream_tries 2; + proxy_next_upstream_timeout 10s; + proxy_connect_timeout 2s; + proxy_read_timeout 10s; + proxy_send_timeout 10s; + } + } +} diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 00000000..6c67477d --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,33 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE + CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX chat_members_user_idx ON chat_members(user_id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users(id), + text text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE INDEX messages_chat_keyset_idx + ON messages (chat_id, created_at, id); \ No newline at end of file diff --git a/migrations/002_sessions.sql b/migrations/002_sessions.sql new file mode 100644 index 00000000..2ee49852 --- /dev/null +++ b/migrations/002_sessions.sql @@ -0,0 +1,6 @@ +CREATE TABLE sessions ( + token text PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at timestamptz NOT NULL +); +CREATE INDEX sessions_expires_at_idx ON sessions (expires_at); \ No newline at end of file diff --git a/migrations/003_idempotency.sql b/migrations/003_idempotency.sql new file mode 100644 index 00000000..2c847072 --- /dev/null +++ b/migrations/003_idempotency.sql @@ -0,0 +1,11 @@ +CREATE TABLE idempotency_keys ( + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'), + request_hash text NOT NULL, + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, chat_id, key) +); + +CREATE INDEX idempotency_keys_created_at_idx ON idempotency_keys (created_at); \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt index 6b103ddb..e22616c5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ # Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib. openapi-spec-validator==0.7.2 +matplotlib==3.11.2 diff --git a/scripts/plot_graphs.py b/scripts/plot_graphs.py new file mode 100644 index 00000000..aaca19d4 --- /dev/null +++ b/scripts/plot_graphs.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Plot track-1 JSON files written by scripts/run_track1.py.""" +import json +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt + +ROOT = Path(__file__).resolve().parents[1] +EVIDENCE = ROOT / "evidence" + + +def load_series(letter): + rows = [] + for path in sorted(EVIDENCE.glob("track1-*.json")): + if path.name.endswith(".load.json"): + continue + data = json.loads(path.read_text()) + if data.get("series") != letter: + continue + load = data["load"] + lat = load["successful_latency_ms"] + rows.append( + { + "replicas": data["api_replicas"], + "db_cpu": data["db_cpu_limit"], + "pool": data["pg_pool"], + "nginx": str(data["nginx_worker_processes"]), + "rps": load["successful_rps"], + "p50": lat["p50"], + "p95": lat["p95"], + "p99": lat["p99"], + "errors": load["failed_requests"], + } + ) + return rows + + +def dual_axis(ax, xs, rps, p99, xlabel, title): + ax.plot(xs, rps, marker="o", color="#1f4e79", label="successful RPS") + ax.set_xlabel(xlabel) + ax.set_ylabel("successful RPS") + ax.set_title(title) + twin = ax.twinx() + twin.plot(xs, p99, marker="s", color="#b85c38", label="p99, ms") + twin.set_ylabel("p99, ms") + lines = ax.get_lines() + twin.get_lines() + ax.legend(lines, [line.get_label() for line in lines], loc="best") + ax.grid(True, axis="y", alpha=0.3) + return twin + + +def main(): + series_a = sorted(load_series("A"), key=lambda row: row["replicas"]) + series_b = sorted(load_series("B"), key=lambda row: row["db_cpu"]) + if not series_a or not series_b: + raise SystemExit("need series A and B JSON in evidence/") + + fig, ax = plt.subplots(figsize=(7.2, 4.4)) + dual_axis( + ax, + [row["replicas"] for row in series_a], + [row["rps"] for row in series_a], + [row["p99"] for row in series_a], + "API replicas (total CPU = 1.0, total RAM = 512 MiB, DB CPU = 2.0)", + "Серия A: реплики API при фиксированных суммарных CPU и RAM", + ) + fig.tight_layout() + fig.savefig(EVIDENCE / "api_scaling.png", dpi=140) + plt.close(fig) + + fig, ax = plt.subplots(figsize=(7.2, 4.4)) + dual_axis( + ax, + [row["db_cpu"] for row in series_b], + [row["rps"] for row in series_b], + [row["p99"] for row in series_b], + "PostgreSQL CPU limit (4 API × 0.25 CPU, total API RAM = 512 MiB)", + "Серия B: RPS и p99 от CPU базы", + ) + fig.tight_layout() + fig.savefig(EVIDENCE / "db_scaling.png", dpi=140) + plt.close(fig) + print("wrote evidence/api_scaling.png evidence/db_scaling.png") + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..784aa589 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,38 @@ +#!/bin/sh +set -eu + +cd "$(dirname "$0")/.." + +compose() { + docker compose --env-file .env "$@" +} + +deadline=$(( $(date +%s) + 70 )) + +wait_healthy() { + svc=$1 + want=1 + if [ "$svc" = "api" ]; then + want=${API_REPLICAS:-2} + fi + while [ "$(date +%s)" -lt "$deadline" ]; do + states=$(compose ps "$svc" --format '{{.Health}}' || true) + n=$(printf '%s\n' "$states" | grep -cx healthy || true) + if [ "$n" -ge "$want" ]; then + return 0 + fi + sleep 1 + done + echo "timed out waiting for $svc" >&2 + return 1 +} + +compose restart db cache +wait_healthy db +wait_healthy cache +compose start api +compose restart api +wait_healthy api +compose restart lb +wait_healthy lb +exit 0 diff --git a/scripts/run_track1.py b/scripts/run_track1.py new file mode 100755 index 00000000..d789acf5 --- /dev/null +++ b/scripts/run_track1.py @@ -0,0 +1,374 @@ +#!/usr/bin/env python3 +"""Series A and B with a fixed total API RAM budget and the base 8/8/1 profile. + +The profile is applied through Compose environment, then checked with docker inspect +before the load window. Nothing secret is printed. +""" +import json +import os +import subprocess +import sys +import threading +import time +import urllib.error +import urllib.request +from decimal import Decimal +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +HTTP_WORKERS = "8" +DB_POOL_SIZE = "8" +NGINX_WORKERS = "1" +LB_CPU_LIMIT = "0.25" +TOTAL_API_RAM = 512 * 1024 * 1024 + + +def mem_bytes(spec): + text = str(spec).strip() + units = {"b": 1, "k": 1024, "m": 1024**2, "g": 1024**3} + unit = text[-1].lower() + if unit not in units: + raise SystemExit(f"bad memory spec: {spec}") + return int(text[:-1]) * units[unit] + + +def cpu_nanos(spec): + return int(Decimal(str(spec)) * Decimal(1_000_000_000)) + + +def series_points(): + points = [] + for replicas, api_cpu in ((1, "1.0"), (2, "0.5"), (4, "0.25")): + per = TOTAL_API_RAM // replicas // (1024 * 1024) + points.append(("A", replicas, api_cpu, f"{per}m", "2.0")) + for db_cpu in ("1.0", "2.0", "4.0"): + per = TOTAL_API_RAM // 4 // (1024 * 1024) + points.append(("B", 4, "0.25", f"{per}m", db_cpu)) + return points + + +def env_for(replicas, api_cpu, api_mem, db_cpu): + env = os.environ.copy() + env["API_REPLICAS"] = str(replicas) + env["API_CPU_LIMIT"] = str(api_cpu) + env["API_MEM_LIMIT"] = api_mem + env["DB_CPU_LIMIT"] = str(db_cpu) + env["HTTP_WORKERS"] = HTTP_WORKERS + env["DB_POOL_SIZE"] = DB_POOL_SIZE + env["NGINX_WORKERS"] = NGINX_WORKERS + env["LB_CPU_LIMIT"] = LB_CPU_LIMIT + return env + + +def compose(args, env, capture=False): + result = subprocess.run( + ["docker", "compose", "--env-file", str(ROOT / ".env"), *args], + cwd=ROOT, + env=env, + check=False, + text=True, + capture_output=capture, + ) + if result.returncode != 0 and capture: + sys.stderr.write(result.stderr or "") + sys.stderr.write(result.stdout or "") + return result + + +def wait_stack(env, replicas, timeout=180): + deadline = time.monotonic() + timeout + last = "" + while time.monotonic() < deadline: + ps = compose(["ps", "api", "--format", "{{.Health}}"], env, capture=True) + health = [line.strip() for line in ps.stdout.splitlines() if line.strip()] + ready = False + try: + with urllib.request.urlopen("http://127.0.0.1:8080/health/ready", timeout=2) as resp: + ready = resp.status == 200 + except (urllib.error.URLError, TimeoutError, OSError): + ready = False + last = f"api_health={health} ready={ready}" + if len(health) == replicas and all(item == "healthy" for item in health) and ready: + return + time.sleep(2) + raise SystemExit("stack did not become ready: " + last) + + +def container_ids(env, service): + result = compose(["ps", "-q", service], env, capture=True) + if result.returncode != 0: + raise SystemExit(f"compose ps {service} failed") + return [line for line in result.stdout.split() if line] + + +def inspect_container(cid): + raw = subprocess.check_output(["docker", "inspect", cid], text=True) + return json.loads(raw)[0] + + +def env_map(info): + found = {} + for item in info["Config"].get("Env") or []: + key, _, value = item.partition("=") + found[key] = value + return found + + +def verify_limits(env, replicas, api_cpu, api_mem, db_cpu): + expect_api_nanos = cpu_nanos(api_cpu) + expect_api_mem = mem_bytes(api_mem) + expect_db_nanos = cpu_nanos(db_cpu) + expect_lb_nanos = cpu_nanos(LB_CPU_LIMIT) + api_ids = container_ids(env, "api") + if len(api_ids) != replicas: + raise SystemExit(f"expected {replicas} api containers, found {len(api_ids)}") + + api_rows = [] + for cid in api_ids: + info = inspect_container(cid) + nanos = info["HostConfig"]["NanoCpus"] + memory = info["HostConfig"]["Memory"] + present = env_map(info) + name = info["Name"].lstrip("/") + if nanos != expect_api_nanos or memory != expect_api_mem: + raise SystemExit( + f"{name}: NanoCpus={nanos} Memory={memory}, " + f"expected {expect_api_nanos} and {expect_api_mem}" + ) + if present.get("HTTP_WORKERS") != HTTP_WORKERS or present.get("DB_POOL_SIZE") != DB_POOL_SIZE: + raise SystemExit( + f"{name}: HTTP_WORKERS={present.get('HTTP_WORKERS')} " + f"DB_POOL_SIZE={present.get('DB_POOL_SIZE')}" + ) + api_rows.append( + { + "name": name, + "nano_cpus": nanos, + "memory": memory, + "http_workers": present["HTTP_WORKERS"], + "db_pool_size": present["DB_POOL_SIZE"], + } + ) + total_mem = sum(row["memory"] for row in api_rows) + if total_mem != TOTAL_API_RAM: + raise SystemExit(f"api RAM sum {total_mem} != {TOTAL_API_RAM}") + + db_ids = container_ids(env, "db") + if len(db_ids) != 1: + raise SystemExit(f"expected 1 db container, found {len(db_ids)}") + db_info = inspect_container(db_ids[0]) + db_nanos = db_info["HostConfig"]["NanoCpus"] + if db_nanos != expect_db_nanos: + raise SystemExit(f"db NanoCpus={db_nanos}, expected {expect_db_nanos}") + + lb_ids = container_ids(env, "lb") + if len(lb_ids) != 1: + raise SystemExit(f"expected 1 lb container, found {len(lb_ids)}") + lb_info = inspect_container(lb_ids[0]) + lb_nanos = lb_info["HostConfig"]["NanoCpus"] + lb_env = env_map(lb_info) + if lb_nanos != expect_lb_nanos or lb_env.get("NGINX_WORKERS") != NGINX_WORKERS: + raise SystemExit( + f"lb NanoCpus={lb_nanos} NGINX_WORKERS={lb_env.get('NGINX_WORKERS')}" + ) + rendered = compose(["exec", "-T", "lb", "nginx", "-T"], env, capture=True) + if rendered.returncode != 0: + raise SystemExit("nginx -T failed") + directive = "" + rendered_text = (rendered.stdout or "") + "\n" + (rendered.stderr or "") + for line in rendered_text.splitlines(): + stripped = line.strip() + if stripped.startswith("worker_processes"): + directive = stripped + break + expected_directive = f"worker_processes {NGINX_WORKERS};" + if directive != expected_directive: + raise SystemExit(f"nginx config has {directive!r}, expected {expected_directive!r}") + + logs = compose(["logs", "api"], env, capture=True) + if logs.returncode != 0: + raise SystemExit("api logs failed") + pool_line = f"pg pool ready ({DB_POOL_SIZE} connections)" + worker_line = f"http workers: {HTTP_WORKERS}" + if logs.stdout.count(pool_line) < replicas or logs.stdout.count(worker_line) < replicas: + raise SystemExit("api logs do not show the base pool and worker profile on every replica") + + return { + "verified": True, + "api": api_rows, + "api_memory_sum": total_mem, + "db": {"name": db_info["Name"].lstrip("/"), "nano_cpus": db_nanos}, + "lb": { + "name": lb_info["Name"].lstrip("/"), + "nano_cpus": lb_nanos, + "nginx_worker_processes": directive, + }, + } + + +def sample_stats(stop, bucket): + while not stop.is_set(): + proc = subprocess.run( + ["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"], + text=True, + capture_output=True, + check=False, + ) + stamp = time.time() + rows = [] + for line in proc.stdout.splitlines(): + parts = line.split("\t") + if len(parts) != 3: + continue + name, cpu, mem = parts + try: + cpu_num = float(cpu.strip().rstrip("%")) + except ValueError: + continue + rows.append({"name": name, "cpu_percent": cpu_num, "mem": mem.strip()}) + if rows: + bucket.append({"t": stamp, "rows": rows}) + stop.wait(3) + + +def summarize_cpu(samples): + acc = {} + for sample in samples: + for row in sample["rows"]: + name = row["name"].lstrip("/") + service = "other" + for prefix in ("api", "db", "cache", "lb"): + if f"-{prefix}-" in name or name.endswith(prefix): + service = prefix + break + acc.setdefault(service, []).append(row["cpu_percent"]) + out = {} + for service, values in acc.items(): + out[service] = { + "samples": len(values), + "mean_cpu_percent": round(sum(values) / len(values), 2), + "max_cpu_percent": round(max(values), 2), + } + return out + + +def run_point(series, replicas, api_cpu, api_mem, db_cpu): + env = env_for(replicas, api_cpu, api_mem, db_cpu) + label = f"track1-{series}-r{replicas}-apicpu{api_cpu}-dbcpu{db_cpu}" + raw = ROOT / "evidence" / f"{label}.load.json" + dest = ROOT / "evidence" / f"{label}.json" + print( + f"point series={series} replicas={replicas} api_cpu={api_cpu} " + f"api_mem={api_mem} db_cpu={db_cpu} http_workers={HTTP_WORKERS} " + f"db_pool={DB_POOL_SIZE} nginx_workers={NGINX_WORKERS}", + file=sys.stderr, + ) + down = compose(["down"], env) + if down.returncode != 0: + raise SystemExit(down.returncode) + up = compose( + ["up", "-d", "--build", "--scale", f"api={replicas}"], + env, + ) + if up.returncode != 0: + raise SystemExit(up.returncode) + wait_stack(env, replicas) + limits = verify_limits(env, replicas, api_cpu, api_mem, db_cpu) + print( + f"inspect ok api_sum_memory={limits['api_memory_sum']} " + f"api_nano={limits['api'][0]['nano_cpus']} db_nano={limits['db']['nano_cpus']} " + f"nginx={limits['lb']['nginx_worker_processes']}", + file=sys.stderr, + ) + + warmup = subprocess.run( + [ + sys.executable, + str(ROOT / "tests" / "load.py"), + "--duration", + "10", + "--concurrency", + "50", + "--output", + f"/tmp/{label}-warmup.json", + ], + cwd=ROOT, + text=True, + capture_output=True, + ) + if warmup.returncode != 0: + sys.stderr.write(warmup.stdout) + sys.stderr.write(warmup.stderr) + raise SystemExit("warmup failed") + + samples = [] + stop = threading.Event() + thread = threading.Thread(target=sample_stats, args=(stop, samples)) + thread.start() + try: + measured = subprocess.run( + [ + sys.executable, + str(ROOT / "tests" / "load.py"), + "--duration", + "30", + "--concurrency", + "50", + "--output", + str(raw), + ], + cwd=ROOT, + text=True, + capture_output=True, + ) + finally: + stop.set() + thread.join() + if measured.returncode != 0: + sys.stderr.write(measured.stdout) + sys.stderr.write(measured.stderr) + raise SystemExit("measured run failed") + load = json.loads(raw.read_text()) + report = { + "series": series, + "api_replicas": replicas, + "api_cpu_limit": float(api_cpu), + "api_mem_limit": api_mem, + "api_mem_limit_bytes": mem_bytes(api_mem), + "api_mem_total_bytes": TOTAL_API_RAM, + "db_cpu_limit": float(db_cpu), + "http_workers": int(HTTP_WORKERS), + "pg_pool": int(DB_POOL_SIZE), + "nginx_worker_processes": NGINX_WORKERS, + "lb_cpu_limit": float(LB_CPU_LIMIT), + "warmup_seconds": 10, + "inspect": limits, + "cpu": summarize_cpu(samples), + "load": load, + } + dest.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n") + lat = load["successful_latency_ms"] + summary = { + "file": str(dest.relative_to(ROOT)), + "rps": load["successful_rps"], + "p50": lat["p50"], + "p95": lat["p95"], + "p99": lat["p99"], + "errors": load["failed_requests"], + "error_ratio": load["error_ratio"], + "api_mem_total_bytes": TOTAL_API_RAM, + "cpu": report["cpu"], + } + print(json.dumps(summary, ensure_ascii=False)) + return summary + + +def main(): + if len(sys.argv) != 1: + raise SystemExit("usage: python3 scripts/run_track1.py") + for series, replicas, api_cpu, api_mem, db_cpu in series_points(): + run_point(series, replicas, api_cpu, api_mem, db_cpu) + + +if __name__ == "__main__": + main() diff --git a/scripts/run_track1.sh b/scripts/run_track1.sh new file mode 100755 index 00000000..49909817 --- /dev/null +++ b/scripts/run_track1.sh @@ -0,0 +1,53 @@ +#!/bin/bash +set -e + +# Переходим в корень проекта +cd "$(dirname "$0")/.." + +# Проверяем, что .env существует +if [ ! -f .env ]; then + echo "Ошибка: .env не найден" + exit 1 +fi + +mkdir -p evidence + +# Функция для запуска одного теста +run_test() { + local replicas=$1 + local cpu_limit=$2 + + echo "==================================================" + echo "ТЕСТ: $replicas реплик (лимит CPU на реплику: $cpu_limit, суммарно 1.0)" + echo "==================================================" + + # Меняем переменные окружения для compose + export API_REPLICAS=$replicas + export API_CPU_LIMIT=$cpu_limit + + # Перезапускаем стек + echo "Перезапуск стека..." + docker compose --env-file .env down + docker compose --env-file .env up -d --build + + echo "Ожидание готовности LB (10 секунд)..." + sleep 10 + + # Запускаем нагрузку + echo "Запуск нагрузки (30 секунд, 20 воркеров)..." + python3 tests/load.py --duration 30 --concurrency 20 --output "evidence/load_${replicas}rep.json" + + # Снимаем метрики CPU контейнеров + echo "Сбор метрик CPU..." + docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" > "evidence/docker_stats_${replicas}rep.txt" + + echo "Тест завершен." + echo "" +} + +# Запускаем 3 конфигурации +run_test 1 "1.0" +run_test 2 "0.5" +run_test 4 "0.25" + +echo "Все тесты завершены! Результаты в папке evidence/" \ No newline at end of file diff --git a/scripts/run_track1_improvement.sh b/scripts/run_track1_improvement.sh new file mode 100755 index 00000000..01657f9e --- /dev/null +++ b/scripts/run_track1_improvement.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e +cd "$(dirname "$0")/.." +mkdir -p evidence + +export API_REPLICAS=4 +export API_CPU_LIMIT=0.25 +export DB_CPU_LIMIT=1.0 + +echo "ТЕСТ: 4 реплики API, БД 1.0 CPU + УВЕЛИЧЕННЫЕ ЛИМИТЫ (tuning)" +docker compose --env-file .env down +docker compose --env-file .env up -d --build +sleep 10 + +python3 tests/load.py --duration 30 --concurrency 50 --output "evidence/load_improved.json" \ No newline at end of file diff --git a/scripts/run_track1_scaling.sh b/scripts/run_track1_scaling.sh new file mode 100755 index 00000000..a5319977 --- /dev/null +++ b/scripts/run_track1_scaling.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -e +cd "$(dirname "$0")/.." +mkdir -p evidence + +export API_REPLICAS=4 +export API_CPU_LIMIT=0.25 + +run_db_test() { + local db_cpu=$1 + echo "==================================================" + echo "ТЕСТ: 4 реплики API, лимит CPU для БД: $db_cpu" + echo "==================================================" + + export DB_CPU_LIMIT=$db_cpu + docker compose --env-file .env down + docker compose --env-file .env up -d --build + sleep 10 + + echo "Нагрузка: 50 воркеров, 30 секунд..." + python3 tests/load.py --duration 30 --concurrency 50 --output "evidence/load_db_${db_cpu}cpu.json" + echo "" +} + +run_db_test "1.0" +run_db_test "2.0" +run_db_test "4.0" + +echo "Готово! Результаты в evidence/" \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..52592c6d --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,24 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" + +cd "$(dirname "$0")/.." + +found="" +ids=$(docker compose --env-file .env ps -q api) +for id in $ids; do + host=$(docker inspect -f '{{.Config.Hostname}}' "$id") + if [ "$host" = "$TARGET_INSTANCE_ID" ]; then + found=$id + break + fi +done + +if [ -z "$found" ]; then + echo "api replica not found" >&2 + exit 1 +fi + +docker stop -t 8 "$found" >/dev/null +exit 0 diff --git a/src/auth.cpp b/src/auth.cpp new file mode 100644 index 00000000..d6d8cf95 --- /dev/null +++ b/src/auth.cpp @@ -0,0 +1,39 @@ +#include "auth.hpp" + +#include +#include + + +namespace { +struct SodiumInit { + SodiumInit() { + if (sodium_init() < 0) { + throw std::runtime_error("sodium_init failed"); + } + } +}; + +const SodiumInit g_sodium_init; +} + + +std::string hash_password(const std::string& plain) { + char hashed[crypto_pwhash_STRBYTES]; + if (crypto_pwhash_str( + hashed, + plain.data(), plain.size(), + crypto_pwhash_OPSLIMIT_INTERACTIVE, + crypto_pwhash_MEMLIMIT_INTERACTIVE) != 0) { + throw std::runtime_error("crypto_pwhash_str failed (out of memory?)"); + } + return std::string(hashed); +} + +bool verify_password(const std::string& plain, const std::string& hash) { + if (hash.empty() || hash.size() >= crypto_pwhash_STRBYTES) { + return false; + } + return crypto_pwhash_str_verify( + hash.c_str(), + plain.data(), plain.size()) == 0; +} \ No newline at end of file diff --git a/src/auth.hpp b/src/auth.hpp new file mode 100644 index 00000000..639b0336 --- /dev/null +++ b/src/auth.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include + +std::string hash_password(const std::string& plain); + +bool verify_password(const std::string& plain, const std::string& hash); \ No newline at end of file diff --git a/src/chat_repo.cpp b/src/chat_repo.cpp new file mode 100644 index 00000000..580fc71b --- /dev/null +++ b/src/chat_repo.cpp @@ -0,0 +1,186 @@ +#include "chat_repo.hpp" +#include "cursor.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +std::string make_uuid() { + static std::mt19937_64 rng{std::random_device{}()}; + std::uniform_int_distribution d16(0, 0xffff); + std::uniform_int_distribution d32(0, 0xffffffffu); + std::ostringstream out; + out << std::hex << std::setfill('0'); + out << std::setw(8) << d32(rng); + out << '-' << std::setw(4) << d16(rng); + out << '-' << std::setw(4) << ((d16(rng) & 0x0fff) | 0x4000); + out << '-' << std::setw(4) << ((d16(rng) & 0x3fff) | 0x8000); + out << '-' << std::setw(4) << d16(rng) << std::setw(8) << d32(rng); + return out.str(); +} + +std::string epoch_to_rfc3339(long long epoch) { + const std::time_t t = static_cast(epoch); + std::tm tm{}; + gmtime_r(&t, &tm); + std::ostringstream out; + out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); + return out.str(); +} + +std::string now_epoch_str() { + const auto now = std::chrono::system_clock::now(); + const auto secs = std::chrono::duration_cast( + now.time_since_epoch()).count(); + return std::to_string(secs); +} + +Chat row_to_chat(const pqxx::row& r) { + Chat c; + c.id = r["id"].as(); + c.title = r["title"].as(); + c.created_at = epoch_to_rfc3339(r["created_at_epoch"].as()); + return c; +} + +std::vector load_member_ids(pqxx::work& tx, + const std::string& chat_id) { + std::vector ids; + auto r = tx.exec( + "SELECT user_id FROM chat_members WHERE chat_id = $1 ORDER BY user_id", + pqxx::params{chat_id}); + ids.reserve(r.size()); + for (const auto& row : r) { + ids.push_back(row["user_id"].as()); + } + return ids; +} + +constexpr const char* kChatSelect = + "id, title, " + "EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch"; + +} + +std::optional ChatRepo::create(const std::string& title, + const std::vector& member_ids) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + + const std::string chat_id = make_uuid(); + const std::string created = now_epoch_str(); + + try { + tx.exec( + "INSERT INTO chats (id, title, created_at) " + "VALUES ($1, $2, to_timestamp($3::bigint))", + pqxx::params{chat_id, title, created}); + + for (const auto& uid : member_ids) { + tx.exec( + "INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)", + pqxx::params{chat_id, uid}); + } + } catch (const pqxx::foreign_key_violation&) { + tx.abort(); + return std::nullopt; + } catch (const pqxx::unique_violation&) { + tx.abort(); + return std::nullopt; + } + + auto r = tx.exec( + "SELECT " + std::string(kChatSelect) + " FROM chats WHERE id = $1", + pqxx::params{chat_id}); + Chat c = row_to_chat(r[0]); + c.member_ids = load_member_ids(tx, chat_id); + tx.commit(); + return c; +} + +std::optional ChatRepo::find_by_id(const std::string& id) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto r = tx.exec( + "SELECT " + std::string(kChatSelect) + " FROM chats WHERE id = $1", + pqxx::params{id}); + if (r.empty()) { + tx.commit(); + return std::nullopt; + } + Chat c = row_to_chat(r[0]); + c.member_ids = load_member_ids(tx, id); + tx.commit(); + return c; +} + + +std::optional ChatRepo::list_for_user( + const std::string& user_id, + int limit, + const std::optional& cursor) { + + std::optional cur; + if (cursor) { + cur = parse_cursor(*cursor); + if (!cur) { + return std::nullopt; + } + } + + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + + if (cur) { + auto owner_check = tx.exec( + "SELECT 1 FROM chats WHERE id = $1 AND id IN " + "(SELECT chat_id FROM chat_members WHERE user_id = $2)", + pqxx::params{cur->id, user_id}); + if (owner_check.empty()) { + tx.commit(); + return std::nullopt; + } + } + + std::string sql = + "SELECT " + std::string(kChatSelect) + ", " + kCreatedUsSql + " " + "FROM chats " + "WHERE id IN (SELECT chat_id FROM chat_members WHERE user_id = $1) "; + if (cur) { + sql += "AND (created_at, id) > (" + std::string(kCursorTimestampSql) + ", $4::uuid) "; + } + sql += "ORDER BY created_at ASC, id ASC LIMIT $2"; + + pqxx::result r; + if (cur) { + r = tx.exec(sql, pqxx::params{user_id, limit + 1, cur->created_us, cur->id}); + } else { + r = tx.exec(sql, pqxx::params{user_id, limit + 1}); + } + + ChatPage page; + const bool has_more = (static_cast(r.size()) > limit); + const int take = has_more ? limit : static_cast(r.size()); + page.items.reserve(take); + for (int i = 0; i < take; ++i) { + Chat c = row_to_chat(r[static_cast(i)]); + c.member_ids = load_member_ids(tx, c.id); + page.items.push_back(std::move(c)); + } + + if (has_more && take > 0) { + const auto& last = r[static_cast(take - 1)]; + page.next_cursor = make_cursor(last["created_us"].as(), + last["id"].as()); + } + + tx.commit(); + return page; +} \ No newline at end of file diff --git a/src/chat_repo.hpp b/src/chat_repo.hpp new file mode 100644 index 00000000..279e2661 --- /dev/null +++ b/src/chat_repo.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "db.hpp" +#include "models.hpp" + +#include +#include +#include + +class ChatRepo { +public: + explicit ChatRepo(PgPool& pool) : pool_(pool) {} + + std::optional create(const std::string& title, + const std::vector& member_ids); + + std::optional find_by_id(const std::string& id); + + std::optional list_for_user(const std::string& user_id, int limit, const std::optional& cursor); + +private: + PgPool& pool_; +}; \ No newline at end of file diff --git a/src/cursor.hpp b/src/cursor.hpp new file mode 100644 index 00000000..08f08504 --- /dev/null +++ b/src/cursor.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include +#include + +// Непрозрачный для клиента курсор keyset-пагинации: "|". +// Микросекунды сохраняют точный created_at строки, даже если в JSON время округлено до секунд. +struct Cursor { + std::string created_us; + std::string id; +}; + +// Выражение SQL, восстанавливающее timestamptz из created_us без потери точности. +constexpr const char* kCursorTimestampSql = + "(timestamptz 'epoch' + $3::bigint * interval '1 microsecond')"; + +constexpr const char* kCreatedUsSql = + "(EXTRACT(EPOCH FROM created_at) * 1000000)::bigint AS created_us"; + +inline bool is_uuid(const std::string& s) { + static const std::regex re( + "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-" + "[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); + return std::regex_match(s, re); +} + +inline std::optional parse_cursor(const std::string& raw) { + const auto bar = raw.find('|'); + if (bar == std::string::npos) { + return std::nullopt; + } + Cursor c{raw.substr(0, bar), raw.substr(bar + 1)}; + const bool digits = !c.created_us.empty() && c.created_us.size() <= 18 && + std::all_of(c.created_us.begin(), c.created_us.end(), + [](unsigned char ch) { return std::isdigit(ch); }); + if (!digits || !is_uuid(c.id)) { + return std::nullopt; + } + return c; +} + +inline std::string make_cursor(const std::string& created_us, const std::string& id) { + return created_us + "|" + id; +} diff --git a/src/db.cpp b/src/db.cpp new file mode 100644 index 00000000..97312c40 --- /dev/null +++ b/src/db.cpp @@ -0,0 +1,360 @@ +#include "db.hpp" +#include "errors.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + + +namespace { + +constexpr int kConnectTimeoutSeconds = 2; +constexpr int kTcpUserTimeoutMs = 5000; +constexpr int kStatementTimeoutMs = 5000; +constexpr int kClientIoTimeoutSeconds = 5; +constexpr auto kAcquireTimeout = std::chrono::seconds(3); + +std::string add_conn_param(std::string conninfo, const std::string& key, + const std::string& value) { + if (conninfo.find(key + "=") != std::string::npos) { + return conninfo; + } + const bool is_url = conninfo.rfind("postgres://", 0) == 0 || + conninfo.rfind("postgresql://", 0) == 0; + if (is_url) { + conninfo += (conninfo.find('?') == std::string::npos ? "?" : "&"); + } else { + conninfo += " "; + } + return conninfo + key + "=" + value; +} + +bool is_alive(pqxx::connection* conn) { + std::promise done; + auto fut = done.get_future(); + std::thread th([conn, &done]() { + try { + pqxx::nontransaction tx(*conn); + tx.exec("SELECT 1"); + done.set_value(); + } catch (...) { + done.set_exception(std::current_exception()); + } + }); + if (fut.wait_for(std::chrono::seconds(kClientIoTimeoutSeconds)) != + std::future_status::ready) { + const int fd = conn->sock(); + if (fd >= 0) { + ::shutdown(fd, SHUT_RDWR); + } + th.join(); + return false; + } + th.join(); + try { + fut.get(); + return true; + } catch (...) { + return false; + } +} + +struct Migration { + int version; + std::string path; +}; + +constexpr int kMigrationsAdvisoryLockKey = 0x6d747573; + + +Migration parse_migration_name(const fs::path& p) { + const std::string name = p.filename().string(); + const auto dot = name.find('.'); + + if (dot == std::string::npos || name.substr(dot) != ".sql") { + throw std::runtime_error("migration file must end with .sql: " + name); + } + + const auto us = name.find('_'); + if (us == std::string::npos || us == 0) { + throw std::runtime_error( + "migration file must be named NNN_name.sql: " + name); + } + + const std::string num = name.substr(0, us); + if (!std::all_of(num.begin(), num.end(), + [](unsigned char c){ return std::isdigit(c); })) { + throw std::runtime_error( + "migration file prefix must be digits: " + name); + } + + Migration m; + m.version = std::stoi(num); + m.path = p.string(); + return m; +}} + + +void apply_migrations(pqxx::connection& conn, const std::string& dir) { + { + pqxx::work tx(conn); + tx.exec("SELECT pg_advisory_lock($1)", + pqxx::params{kMigrationsAdvisoryLockKey}); + tx.commit(); + } + std::clog << "migration advisory lock acquired\n"; + struct Unlock { + pqxx::connection& c; + ~Unlock() { + try { + pqxx::work tx(c); + tx.exec("SELECT pg_advisory_unlock($1)", + pqxx::params{kMigrationsAdvisoryLockKey}); + tx.commit(); + } catch (...) {} + } + } unlock_guard{conn}; + + { + pqxx::work tx(conn); + tx.exec( + "CREATE TABLE IF NOT EXISTS schema_migrations (" + " version int PRIMARY KEY," + " applied_at timestamptz NOT NULL DEFAULT now()" + ")"); + tx.commit(); + } + + if (!fs::exists(dir) || !fs::is_directory(dir)) { + throw std::runtime_error("migrations dir not found: " + dir); + } + std::vector files; + for (const auto& entry : fs::directory_iterator(dir)) { + if (entry.is_regular_file()) { + files.push_back(parse_migration_name(entry.path())); + } + } + std::sort(files.begin(), files.end(), + [](const Migration& a, const Migration& b){ + return a.version < b.version; + }); + + for (std::size_t i = 1; i < files.size(); ++i) { + if (files[i].version == files[i-1].version) { + throw std::runtime_error( + "duplicate migration version: " + std::to_string(files[i].version)); + } + } + + std::set applied; + { + pqxx::work tx(conn); + for (const auto& row : tx.exec("SELECT version FROM schema_migrations")) { + applied.insert(row[0].as()); + } + tx.commit(); + } + + for (const auto& m : files) { + if (applied.count(m.version)) { + continue; + } + + std::ifstream f(m.path, std::ios::binary); + if (!f) { + throw std::runtime_error("cannot open migration: " + m.path); + } + std::stringstream buf; + buf << f.rdbuf(); + const std::string sql = buf.str(); + + pqxx::work tx(conn); + tx.exec(sql); + tx.exec("INSERT INTO schema_migrations(version) VALUES ($1)", + m.version); + tx.commit(); + + std::clog << "applied migration " << m.version + << " (" << m.path << ")\n"; + } +} + + +std::string read_secret_file(const char* env_name) { + const char* path = std::getenv(env_name); + + if (path == nullptr || *path == '\0') { + throw std::runtime_error( + std::string("environment variable not set: ") + env_name); + } + + std::ifstream f(path, std::ios::binary); + if (!f) { + throw std::runtime_error( + std::string("cannot open secret file: ") + path); + } + + std::string s; + std::getline(f, s); + if (!s.empty() && s.back() == '\r') { + s.pop_back(); + } + + if (s.empty()) { + throw std::runtime_error( + std::string("secret file is empty: ") + path); + } + + return s; +} + + +bool is_dependency_failure(const std::exception& e) { + if (dynamic_cast(&e) != nullptr || + dynamic_cast(&e) != nullptr || + dynamic_cast(&e) != nullptr) { + return true; + } + if (const auto* sql = dynamic_cast(&e)) { + // 08xxx — ошибки соединения, 57xxx — отмена (statement_timeout) и остановка сервера. + const std::string& state = sql->sqlstate(); + return state.rfind("08", 0) == 0 || state.rfind("57", 0) == 0; + } + return false; +} + + +std::unique_ptr open_pg_connection(const std::string& conninfo, + bool statement_timeout) { + std::string full = add_conn_param(conninfo, "connect_timeout", + std::to_string(kConnectTimeoutSeconds)); + full = add_conn_param(full, "tcp_user_timeout", std::to_string(kTcpUserTimeoutMs)); + auto conn = std::make_unique(full); + const int fd = conn->sock(); + if (fd >= 0) { + timeval tv{}; + tv.tv_sec = kClientIoTimeoutSeconds; + tv.tv_usec = 0; + ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + ::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + } + if (statement_timeout) { + pqxx::nontransaction tx(*conn); + tx.exec("SET statement_timeout = " + std::to_string(kStatementTimeoutMs)); + } + return conn; +} + + +PgPool::PgPool(std::string conninfo, std::size_t n) + : conninfo_(std::move(conninfo)) { + if (n == 0) { + throw std::runtime_error("PgPool: n must be > 0"); + } + all_.reserve(n); + idle_.reserve(n); + for (std::size_t i = 0; i < n; ++i) { + auto conn = open_pg_connection(conninfo_, true); + idle_.push_back(conn.get()); + all_.push_back(std::move(conn)); + } +} + + +PgPool::~PgPool() { + { + std::lock_guard lk(mu_); + shutting_down_ = true; + } + cv_.notify_all(); +} + + +pqxx::connection* PgPool::take() { + std::unique_lock lk(mu_); + if (!cv_.wait_for(lk, kAcquireTimeout, + [&]() { return shutting_down_ || !idle_.empty(); })) { + throw DependencyUnavailable("postgres pool: acquire timeout"); + } + if (shutting_down_) { + throw DependencyUnavailable("postgres pool is shutting down"); + } + pqxx::connection* conn = idle_.back(); + idle_.pop_back(); + lk.unlock(); + + if (is_alive(conn)) { + return conn; + } + + std::unique_ptr fresh; + try { + fresh = open_pg_connection(conninfo_, true); + } catch (const std::exception& e) { + // Мёртвое соединение возвращается в пул: следующий take() снова + // попробует переподключиться, и пул не теряет слот. + give_back(conn); + throw DependencyUnavailable(std::string("postgres reconnect failed: ") + e.what()); + } + + std::lock_guard lk2(mu_); + for (auto& up : all_) { + if (up.get() == conn) { + up = std::move(fresh); + return up.get(); + } + } + throw std::logic_error("PgPool: connection not owned by pool"); +} + + +void PgPool::give_back(pqxx::connection* conn) { + { + std::lock_guard lk(mu_); + idle_.push_back(conn); + } + cv_.notify_one(); +} + + +PgPool::Lease::Lease(PgPool& pool) : pool_(&pool), conn_(pool.take()) {} + +PgPool::Lease::~Lease() { + if (pool_ != nullptr && conn_ != nullptr) { + pool_->give_back(conn_); + } +} + +PgPool::Lease::Lease(Lease&& other) noexcept + : pool_(other.pool_), conn_(other.conn_) { + other.pool_ = nullptr; + other.conn_ = nullptr; +} + +PgPool::Lease PgPool::acquire() { + return Lease(*this); +} + +bool PgPool::ping() { + try { + auto lease = acquire(); + pqxx::work tx(*lease); + tx.exec("SELECT 1"); + tx.commit(); + return true; + } catch (...) { + return false; + } +} \ No newline at end of file diff --git a/src/db.hpp b/src/db.hpp new file mode 100644 index 00000000..bacfa92b --- /dev/null +++ b/src/db.hpp @@ -0,0 +1,62 @@ +#pragma once +#include + +#include + +#include +#include +#include +#include +#include + + +std::string read_secret_file(const char* env_name); + +std::unique_ptr open_pg_connection(const std::string& conninfo, + bool statement_timeout); + + +void apply_migrations(pqxx::connection& conn, const std::string& dir); + + +class PgPool { +public: + bool ping(); + PgPool(std::string conninfo, std::size_t n); + ~PgPool(); + + PgPool(const PgPool&) = delete; + PgPool& operator=(const PgPool&) = delete; + + class Lease { + public: + explicit Lease(PgPool& pool); + ~Lease(); + Lease(Lease&& other) noexcept; + Lease& operator=(Lease&&) = delete; + Lease(const Lease&) = delete; + Lease& operator=(const Lease&) = delete; + + pqxx::connection& operator*() const { return *conn_; } + pqxx::connection* operator->() const { return conn_; } + + private: + PgPool* pool_; + pqxx::connection* conn_; + }; + + Lease acquire(); + +private: + friend class Lease; + + pqxx::connection* take(); + void give_back(pqxx::connection* conn); + + std::string conninfo_; + std::vector> all_; + std::vector idle_; + std::mutex mu_; + std::condition_variable cv_; + bool shutting_down_ = false; +}; \ No newline at end of file diff --git a/src/errors.hpp b/src/errors.hpp new file mode 100644 index 00000000..4f9f3c5a --- /dev/null +++ b/src/errors.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +// PostgreSQL или Valkey недоступны либо не ответили вовремя: клиент получает 503. +struct DependencyUnavailable : std::runtime_error { + using std::runtime_error::runtime_error; +}; + +bool is_dependency_failure(const std::exception& e); diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 00000000..c35f6ea0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,1395 @@ +#include "models.hpp" +#include "db.hpp" +#include "auth.hpp" +#include "user_repo.hpp" +#include "chat_repo.hpp" +#include "message_repo.hpp" +#include "redis_pool.hpp" +#include "session_repo.hpp" +#include "pg_session_repo.hpp" +#include "session_repo_base.hpp" +#include "errors.hpp" + + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static std::string g_instance_id; + +static std::string make_instance_id() { + const char* env = std::getenv("INSTANCE_ID"); + if (env && *env) { + return std::string(env); + } + const char* host = std::getenv("HOSTNAME"); + if (host && *host) { + return std::string(host); + } + static const char hex[] = "0123456789abcdef"; + std::random_device rd; + std::string s(8, '0'); + for (auto& c : s) c = hex[rd() & 0xf]; + return s; +} + + +static void send_all(int fd, const std::string& data) { + size_t sent = 0; + while (sent < data.size()) { + const ssize_t n = ::write(fd, data.data() + sent, data.size() - sent); + if (n <= 0) { + return; + } + sent += static_cast(n); + } +} + + +static std::string http_response(int status, const char* reason, + const std::string& request_id, + const std::string& body) { + std::string out; + out += "HTTP/1.1 " + std::to_string(status) + " " + reason + "\r\n"; + out += "Content-Type: application/json\r\n"; + out += "X-Request-Id: " + request_id + "\r\n"; + out += "X-Instance-Id: " + g_instance_id + "\r\n"; + out += "Content-Length: " + std::to_string(body.size()) + "\r\n"; + out += "Connection: close\r\n"; + out += "\r\n"; + out += body; + return out; +} + +static std::string http_no_content(const std::string& request_id) { + std::string out; + out += "HTTP/1.1 204 No Content\r\n"; + out += "X-Request-Id: " + request_id + "\r\n"; + out += "X-Instance-Id: " + g_instance_id + "\r\n"; + out += "Content-Length: 0\r\n"; + out += "Connection: close\r\n"; + out += "\r\n"; + return out; +} + + +static const char* status_reason(int status) { + switch (status) { + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 404: return "Not Found"; + case 409: return "Conflict"; + case 413: return "Payload Too Large"; + case 415: return "Unsupported Media Type"; + case 500: return "Internal Server Error"; + case 503: return "Service Unavailable"; + default: return "Error"; + } +} + + +static std::string error_response(int status, + const std::string& code, + const std::string& message, + const std::string& request_id, + const std::string& www_authenticate = "") { + nlohmann::json j; + j["error"]["code"] = code; + j["error"]["message"] = message; + j["error"]["request_id"] = request_id; + const std::string body = j.dump(); + + std::string head = + "HTTP/1.1 " + std::to_string(status) + " " + + status_reason(status) + "\r\n" + "Content-Type: application/json\r\n" + "X-Request-Id: " + request_id + "\r\n" + + "X-Instance-Id: " + g_instance_id + "\r\n"; + if (status == 401 && !www_authenticate.empty()) { + head += "WWW-Authenticate: " + www_authenticate + "\r\n"; + } + head += "Content-Length: " + std::to_string(body.size()) + "\r\n"; + head += "Connection: close\r\n\r\n"; + return head + body; +} + + +static std::string failure_response(const std::exception& e, const char* what, + const std::string& request_id) { + std::clog << what << " failed: " << e.what() << "\n"; + if (is_dependency_failure(e)) { + return error_response(503, "unavailable", "service unavailable", request_id); + } + return error_response(500, "internal_error", "internal error", request_id); +} + + +static std::optional parse_json_object(const std::string& body) { + nlohmann::json j; + try { + j = nlohmann::json::parse(body); + } catch (const nlohmann::json::exception&) { + return std::nullopt; + } + + if (!j.is_object()) { + return std::nullopt; + } + return j; +} + + +static std::optional find_unexpected_key( + const nlohmann::json& j, + std::initializer_list allowed) { + for (const auto& [key, value] : j.items()) { + bool ok = false; + for (const char* a : allowed) { + if (key == a) { ok = true; break; } + } + + if (!ok) { + return key; + } + } + return std::nullopt; +} + + +static std::optional get_string_field( + const nlohmann::json& j, const char* key) { + auto it = j.find(key); + if (it == j.end() || !it->is_string()) { + return std::nullopt; + } + return it->get(); +} + + +static std::optional> get_string_array( + const nlohmann::json& j, const char* key) { + auto it = j.find(key); + if (it == j.end() || !it->is_array()) { + return std::nullopt; + } + std::vector out; + out.reserve(it->size()); + for (const auto& item : *it) { + if (!item.is_string()) { + return std::nullopt; + } + out.push_back(item.get()); + } + return out; +} + +static bool parse_request_line(const std::string& first_line, + std::string& method, + std::string& path, + std::string& query) { + query.clear(); + const auto sp1 = first_line.find(' '); + if (sp1 == std::string::npos) { + return false; + } + + const auto sp2 = first_line.find(' ', sp1 + 1); + if (sp2 == std::string::npos) { + return false; + } + + method = first_line.substr(0, sp1); + path = first_line.substr(sp1 + 1, sp2 - sp1 - 1); + const auto q = path.find('?'); + if (q != std::string::npos) { + query = path.substr(q + 1); + path.resize(q); + } + return !method.empty() && !path.empty() && path[0] == '/'; +} + +static bool match_id_path(const std::string& path, + const std::string& prefix, + std::string& id) { + if (path.compare(0, prefix.size(), prefix) != 0) { + return false; + } + id = path.substr(prefix.size()); + return !id.empty() && id.find('/') == std::string::npos; +} + + +static bool match_chat_messages(const std::string& path, std::string& chat_id) { + const std::string prefix = "/api/v1/chats/"; + const std::string suffix = "/messages"; + if (path.size() <= prefix.size() + suffix.size()) { + return false; + } + if (path.compare(0, prefix.size(), prefix) != 0) { + return false; + } + if (path.compare(path.size() - suffix.size(), suffix.size(), suffix) != 0) { + return false; + } + chat_id = path.substr(prefix.size(), + path.size() - prefix.size() - suffix.size()); + return !chat_id.empty() && chat_id.find('/') == std::string::npos; +} + + +static std::string to_lower(std::string s) { + for (char& c : s) { + c = static_cast(std::tolower(static_cast(c))); + } + return s; +} + + +static std::optional utf8_cps(const std::string& s) { + size_t i = 0; + size_t n = 0; + while (i < s.size()) { + const unsigned char c = static_cast(s[i]); + size_t w = 1; + if (c >= 0x80) { + if ((c & 0xE0) == 0xC0) w = 2; + else if ((c & 0xF0) == 0xE0) w = 3; + else if ((c & 0xF8) == 0xF0) w = 4; + else return std::nullopt; + if (i + w > s.size()) return std::nullopt; + for (size_t k = 1; k < w; ++k) { + if ((static_cast(s[i + k]) & 0xC0) != 0x80) { + return std::nullopt; + } + } + } + ++n; + i += w; + } + return n; +} + + +static bool cps_in_range(const std::string& s, size_t lo, size_t hi) { + const auto n = utf8_cps(s); + return n && *n >= lo && *n <= hi; +} + + +static std::unordered_map parse_headers( + const std::string& request, std::string::size_type after_first_line) { + std::unordered_map headers; + auto pos = after_first_line; + if (pos < request.size() && request.compare(pos, 2, "\r\n") == 0) { + pos += 2; + } + for (;;) { + const auto eol = request.find("\r\n", pos); + if (eol == std::string::npos || eol == pos) { + break; + } + const std::string line = request.substr(pos, eol - pos); + pos = eol + 2; + const auto colon = line.find(':'); + if (colon == std::string::npos) { + continue; + } + std::string name = to_lower(line.substr(0, colon)); + std::string value = line.substr(colon + 1); + while (!value.empty() && (value[0] == ' ' || value[0] == '\t')) { + value.erase(0, 1); + } + headers[name] = value; + } + return headers; +} + + +static std::string user_json(const User& user) { + nlohmann::json j; + j["id"] = user.id; + j["username"] = user.username; + j["display_name"] = user.display_name; + j["created_at"] = user.created_at; + return j.dump(); +} + + +static std::string message_json(const Message& msg) { + nlohmann::json j; + j["id"] = msg.id; + j["chat_id"] = msg.chat_id; + j["sender_id"] = msg.sender_id; + j["text"] = msg.text; + j["created_at"] = msg.created_at; + return j.dump(); +} + + +static std::string chat_json(const Chat& chat) { + nlohmann::json j; + j["id"] = chat.id; + j["title"] = chat.title; + j["member_ids"] = chat.member_ids; + j["created_at"] = chat.created_at; + return j.dump(); +} + + +static std::string messages_page_json(const MessagePage& msgs) { + nlohmann::json j; + j["items"] = nlohmann::json::array(); + for (const auto& m : msgs.items) { + j["items"].push_back(nlohmann::json::parse(message_json(m))); + } + if (msgs.next_cursor) { + j["next_cursor"] = *msgs.next_cursor; + } else { + j["next_cursor"] = nullptr; + } + return j.dump(); +} + + +static std::string chats_page_json(const ChatPage& page) { + nlohmann::json j; + j["items"] = nlohmann::json::array(); + for (const auto& c : page.items) { + j["items"].push_back(nlohmann::json::parse(chat_json(c))); + } + if (page.next_cursor) { + j["next_cursor"] = *page.next_cursor; + } else { + j["next_cursor"] = nullptr; + } + return j.dump(); +} + + +static bool is_json_content_type(const std::string& raw) { + std::string s = to_lower(raw); + const auto sc = s.find(';'); + if (sc != std::string::npos) { + s.resize(sc); + } + while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) { + s.pop_back(); + } + size_t a = 0; + while (a < s.size() && (s[a] == ' ' || s[a] == '\t')) { + ++a; + } + return s.substr(a) == "application/json"; +} + + +static int hex_val(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + + +static bool json_content_type_ok( + const std::unordered_map& headers) { + const auto it = headers.find("content-type"); + return it != headers.end() && is_json_content_type(it->second); +} + + +static bool contains_nul(const std::string& s) { + return s.find('\0') != std::string::npos; +} + +static bool is_unicode_space(uint32_t cp) { + if (cp <= 0x7F) { + return std::isspace(static_cast(cp)) != 0; + } + if (cp >= 0x2000 && cp <= 0x200A) { + return true; + } + switch (cp) { + case 0x00A0: + case 0x1680: + case 0x2028: + case 0x2029: + case 0x202F: + case 0x205F: + case 0x3000: + case 0xFEFF: + return true; + default: + return false; + } +} + +static bool is_blank(const std::string& s) { + size_t i = 0; + while (i < s.size()) { + const unsigned char c = static_cast(s[i]); + uint32_t cp = c; + size_t w = 1; + if (c >= 0x80) { + if ((c & 0xE0) == 0xC0 && i + 1 < s.size()) { + w = 2; + cp = (static_cast(c & 0x1F) << 6) + | (static_cast(s[i + 1]) & 0x3F); + } else if ((c & 0xF0) == 0xE0 && i + 2 < s.size()) { + w = 3; + cp = (static_cast(c & 0x0F) << 12) + | ((static_cast(s[i + 1]) & 0x3F) << 6) + | (static_cast(s[i + 2]) & 0x3F); + } else if ((c & 0xF8) == 0xF0 && i + 3 < s.size()) { + w = 4; + cp = (static_cast(c & 0x07) << 18) + | ((static_cast(s[i + 1]) & 0x3F) << 12) + | ((static_cast(s[i + 2]) & 0x3F) << 6) + | (static_cast(s[i + 3]) & 0x3F); + } else { + return false; + } + } + if (!is_unicode_space(cp)) { + return false; + } + i += w; + } + return true; +} + + +static bool valid_username(const std::string& s) { + static const std::regex re("^[a-z0-9_]{3,32}$"); + return std::regex_match(s, re); +} + + +static bool valid_uuid(const std::string& s) { + static const std::regex re( + "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-" + "[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); + return std::regex_match(s, re); +} + +static bool valid_idempotency_key(const std::string& s) { + static const std::regex re("^[A-Za-z0-9._:-]{1,128}$"); + return std::regex_match(s, re); +} + + +static bool has_duplicate_ids(const std::vector& ids) { + std::unordered_set seen; + for (const auto& id : ids) { + if (!seen.insert(to_lower(id)).second) { + return true; + } + } + return false; +} + + +static std::string url_decode(const std::string& s) { + std::string out; + for (size_t i = 0; i < s.size(); ++i) { + if (s[i] == '%' && i + 2 < s.size()) { + const int hi = hex_val(s[i + 1]); + const int lo = hex_val(s[i + 2]); + if (hi >= 0 && lo >= 0) { + out.push_back(static_cast((hi << 4) | lo)); + i += 2; + continue; + } + } + out.push_back(s[i] == '+' ? ' ' : s[i]); + } + return out; +} + + +static std::unordered_map parse_query( + const std::string& query) { + std::unordered_map out; + size_t pos = 0; + while (pos < query.size()) { + auto amp = query.find('&', pos); + if (amp == std::string::npos) { + amp = query.size(); + } + const auto pair = query.substr(pos, amp - pos); + const auto eq = pair.find('='); + if (eq != std::string::npos) { + out[url_decode(pair.substr(0, eq))] = + url_decode(pair.substr(eq + 1)); + } + pos = amp + 1; + } + return out; +} + + +static std::atomic g_listen{-1}; +static std::atomic g_stop{false}; +static std::atomic g_draining{false}; +static std::mutex g_live_mu; +static std::unordered_set g_live; + +static void set_timeouts(int fd) { + timeval tv{}; + tv.tv_sec = 2; + tv.tv_usec = 0; + ::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + ::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); +} + +static void live_add(int fd) { + std::lock_guard lk(g_live_mu); + g_live.insert(fd); +} + +static void live_del(int fd) { + std::lock_guard lk(g_live_mu); + g_live.erase(fd); +} + +static std::string read_request(int client, bool& too_large) { + too_large = false; + std::string request; + char buf[4096]; + constexpr std::size_t kMaxBody = 1048576; + for (;;) { + const ssize_t n = ::read(client, buf, sizeof(buf)); + if (n < 0) { + if (errno == EINTR) { + continue; + } + return {}; + } + if (n == 0) { + return request; + } + request.append(buf, static_cast(n)); + const auto hdr = request.find("\r\n\r\n"); + if (hdr == std::string::npos) { + continue; + } + std::size_t content_length = 0; + const auto cl = to_lower(request).find("content-length:"); + if (cl != std::string::npos && cl < hdr) { + const auto line_end = request.find("\r\n", cl); + const auto colon = request.find(':', cl); + if (line_end != std::string::npos && colon < line_end) { + content_length = static_cast( + std::atoi(request.c_str() + colon + 1)); + } + } + if (content_length > kMaxBody) { + too_large = true; + request.resize(hdr + 4); + return request; + } + const auto need = hdr + 4 + content_length; + if (request.size() >= need) { + request.resize(need); + return request; + } + } +} + +static std::optional> parse_basic_auth( + const std::string& header_value) { + + static const std::string prefix = "Basic "; + if (header_value.size() <= prefix.size() || + header_value.compare(0, prefix.size(), prefix) != 0) { + return std::nullopt; + } + const std::string b64 = header_value.substr(prefix.size()); + + auto b64_val = [](char c) -> int { + if (c >= 'A' && c <= 'Z') return c - 'A'; + if (c >= 'a' && c <= 'z') return c - 'a' + 26; + if (c >= '0' && c <= '9') return c - '0' + 52; + if (c == '+') return 62; + if (c == '/') return 63; + return -1; + }; + + std::string decoded; + int buf = 0, bits = 0; + for (char c : b64) { + if (c == '=') break; + const int v = b64_val(c); + if (v < 0) return std::nullopt; + buf = (buf << 6) | v; + bits += 6; + if (bits >= 8) { + bits -= 8; + decoded.push_back(static_cast((buf >> bits) & 0xFF)); + } + } + + const auto colon = decoded.find(':'); + if (colon == std::string::npos) { + return std::nullopt; + } + return std::make_pair(decoded.substr(0, colon), + decoded.substr(colon + 1)); +} + +static void handle_client(int client, + std::atomic& request_n, + UserRepo& users, + ChatRepo& chats, + MessageRepo& messages, + ISessionRepo& sessions, + PgPool& pg, + RedisPool* redis) { + bool too_large = false; + const std::string request = read_request(client, too_large); + const unsigned nreq = ++request_n; + const std::string request_id = std::to_string(nreq); + + if (too_large) { + send_all(client, error_response(413, "payload_too_large", + "payload too large", request_id)); + live_del(client); + ::close(client); + return; + } + if (request.empty()) { + live_del(client); + ::close(client); + return; + } + + const auto line_end = request.find("\r\n"); + const std::string first_line = request.substr(0, line_end); + std::string method; + std::string path; + std::string query; + if (!parse_request_line(first_line, method, path, query)) { + send_all(client, error_response(400, "invalid_request", + "bad request line", request_id)); + live_del(client); + ::close(client); + return; + } + + const auto headers = parse_headers(request, line_end); + std::string user_id; + std::string bearer_token; + bool auth_backend_error = false; + { + auto it = headers.find("authorization"); + if (it != headers.end()) { + static const std::string prefix = "Bearer "; + const std::string& auth = it->second; + if (auth.size() > prefix.size() && + auth.compare(0, prefix.size(), prefix) == 0) { + bearer_token = auth.substr(prefix.size()); + } + } + if (!bearer_token.empty()) { + try { + auto found = sessions.lookup(bearer_token); + if (found) { + user_id = *found; + } + } catch (const std::exception& e) { + std::clog << "session lookup failed: " << e.what() << "\n"; + auth_backend_error = true; + } + } + } + auto send_auth_error = [&]() -> bool { + if (auth_backend_error) { + send_all(client, error_response(503, "unavailable", + "service unavailable", request_id)); + return true; + } + if (user_id.empty()) { + send_all(client, error_response(401, "unauthorized", + "unauthorized", request_id, + "Bearer realm=\"messenger\"")); + return true; + } + return false; + }; + // Сессия может пережить пользователя; недоступная БД — это 503, а не 401. + auto send_user_error = [&]() -> bool { + try { + if (users.find_by_id(user_id)) { + return false; + } + send_all(client, error_response(401, "unauthorized", + "unauthorized", request_id, + "Bearer realm=\"messenger\"")); + } catch (const std::exception& e) { + send_all(client, failure_response(e, "find_user", request_id)); + } + return true; + }; + + std::string chat_id; + + if (method == "GET" && path == "/health/live") { + send_all(client, http_response(200, "OK", request_id, "{\"status\":\"ok\"}")); + } + else if (method == "GET" && path == "/health/ready") { + if (g_draining.load()) { + send_all(client, error_response(503, "unavailable", + "shutting down", request_id)); + } else { + const bool pg_ok = pg.ping(); + const bool redis_ok = redis == nullptr || redis->ping(); + if (pg_ok && redis_ok) { + send_all(client, http_response(200, "OK", request_id, "{\"status\":\"ok\"}")); + } else { + std::clog << "readiness failed: pg=" << pg_ok + << " redis=" << redis_ok << "\n"; + send_all(client, error_response(503, "unavailable", + "service unavailable", request_id)); + } + } + } + else if (method == "POST" && path == "/api/v1/auth/sessions") { + const auto body_at = request.find("\r\n\r\n"); + const std::string session_body = + body_at == std::string::npos ? "" : request.substr(body_at + 4); + if (!session_body.empty()) { + send_all(client, error_response(400, "invalid_request", + "body not allowed", request_id)); + } else { + auto it = headers.find("authorization"); + std::optional> creds; + if (it != headers.end()) { + creds = parse_basic_auth(it->second); + } + + const auto send_basic_401 = [&]() { + send_all(client, error_response( + 401, "unauthorized", "unauthorized", request_id, + "Basic realm=\"messenger\", charset=\"UTF-8\"")); + }; + + if (!creds) { + send_basic_401(); + } else { + const auto& username = creds->first; + const auto& password = creds->second; + try { + const auto user = users.find_by_username(username); + if (!user || !verify_password(password, user->password_hash)) { + send_basic_401(); + } else { + const auto created = sessions.create(user->id); + nlohmann::json j; + j["token"] = created.token; + j["token_type"] = "Bearer"; + const std::time_t t = static_cast( + created.expires_at); + std::tm tm{}; + gmtime_r(&t, &tm); + std::ostringstream out; + out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); + j["expires_at"] = out.str(); + j["user"] = nlohmann::json::parse(user_json(*user)); + send_all(client, http_response(201, "Created", + request_id, j.dump())); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "create_session", request_id)); + } + } + } + } + else if (method == "GET" && path == "/api/v1/users/me") { + if (send_auth_error()) { + } else { + try { + const auto user = users.find_by_id(user_id); + if (!user) { + send_all(client, error_response(401, "unauthorized", + "unauthorized", request_id, + "Bearer realm=\"messenger\"")); + } else { + send_all(client, http_response(200, "OK", request_id, + user_json(*user))); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "find_user", request_id)); + } + } + } + else if (method == "POST" && path == "/api/v1/users") { + if (!json_content_type_ok(headers)) { + send_all(client, error_response(415, "unsupported_media_type", + "unsupported media type", request_id)); + } else { + const auto body_at = request.find("\r\n\r\n"); + const std::string body = + body_at == std::string::npos ? "" : request.substr(body_at + 4); + + const auto j = parse_json_object(body); + if (!j) { + send_all(client, error_response(400, "invalid_request", + "invalid json", request_id)); + } else if (auto bad = find_unexpected_key( + *j, {"username", "display_name", "password"})) { + send_all(client, error_response(400, "invalid_request", + "unexpected field: " + *bad, request_id)); + } else { + const auto username = get_string_field(*j, "username"); + const auto display_name = get_string_field(*j, "display_name"); + const auto password = get_string_field(*j, "password"); + if (!username || !display_name || !password) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else if (!valid_username(*username) || + contains_nul(*display_name) || + is_blank(*display_name) || + !cps_in_range(*display_name, 1, 100)) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else if (contains_nul(*password) || + !cps_in_range(*password, 12, 128)) { + send_all(client, error_response(400, "invalid_request", + "invalid password", request_id)); + } else { + std::string hash; + try { + hash = hash_password(*password); + } catch (const std::exception& e) { + send_all(client, failure_response(e, "hash_password", request_id)); + } + if (!hash.empty()) { + try { + const auto created = users.create(*username, + *display_name, hash); + if (!created) { + send_all(client, error_response(409, "conflict", + "username taken", request_id)); + } else { + send_all(client, http_response(201, "Created", request_id, + user_json(*created))); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "create_user", request_id)); + } + } + } + } + } + } + else if (method == "POST" && path == "/api/v1/chats") { + if (send_auth_error()) { + // ответ отправлен + } else if (send_user_error()) { + // ответ отправлен + } else if (!json_content_type_ok(headers)) { + send_all(client, error_response(415, "unsupported_media_type", + "unsupported media type", request_id)); + } else { + const auto body_at = request.find("\r\n\r\n"); + const std::string body = + body_at == std::string::npos ? "" : request.substr(body_at + 4); + + const auto j = parse_json_object(body); + if (!j) { + send_all(client, error_response(400, "invalid_request", + "invalid json", request_id)); + } else if (auto bad = find_unexpected_key(*j, {"title", "member_ids"})) { + send_all(client, error_response(400, "invalid_request", + "unexpected field: " + *bad, request_id)); + } else { + const auto title = get_string_field(*j, "title"); + const auto member_ids = get_string_array(*j, "member_ids"); + if (!title || !member_ids) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else if (contains_nul(*title) || is_blank(*title) || + !cps_in_range(*title, 1, 100)) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else if (member_ids->empty() || member_ids->size() > 100) { + send_all(client, error_response(400, "invalid_request", + "invalid members", request_id)); + } else if (std::find(member_ids->begin(), member_ids->end(), + user_id) == member_ids->end()) { + send_all(client, error_response(400, "invalid_request", + "caller not in members", request_id)); + } else if (!std::all_of(member_ids->begin(), member_ids->end(), + valid_uuid)) { + send_all(client, error_response(400, "invalid_request", + "invalid members", request_id)); + } else if (has_duplicate_ids(*member_ids)) { + send_all(client, error_response(400, "invalid_request", + "duplicate members", request_id)); + } else { + try { + const auto created = chats.create(*title, *member_ids); + if (!created) { + send_all(client, error_response(400, "invalid_request", + "invalid members", request_id)); + } else { + send_all(client, http_response(201, "Created", request_id, + chat_json(*created))); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "create_chat", request_id)); + } + } + } + } + } + else if (method == "POST" && match_chat_messages(path, chat_id)) { + if (send_auth_error()) { + // ответ отправлен + } else if (send_user_error()) { + // ответ отправлен + } else if (!valid_uuid(chat_id)) { + send_all(client, error_response(400, "invalid_request", + "invalid chat id", request_id)); + } else { + try { + const auto chat = chats.find_by_id(chat_id); + const bool is_member = chat && + (std::find(chat->member_ids.begin(), + chat->member_ids.end(), + user_id) != chat->member_ids.end()); + if (!is_member) { + send_all(client, error_response(404, "not_found", + "not found", request_id)); + } else if (!json_content_type_ok(headers)) { + send_all(client, error_response(415, "unsupported_media_type", + "unsupported media type", request_id)); + } else { + const auto body_at = request.find("\r\n\r\n"); + const std::string body = + body_at == std::string::npos ? "" : request.substr(body_at + 4); + + const auto j = parse_json_object(body); + if (!j) { + send_all(client, error_response(400, "invalid_request", + "invalid json", request_id)); + } else if (auto bad = find_unexpected_key(*j, {"text"})) { + send_all(client, error_response(400, "invalid_request", + "unexpected field: " + *bad, request_id)); + } else { + const auto text = get_string_field(*j, "text"); + if (!text) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else if (contains_nul(*text) || is_blank(*text) || + !cps_in_range(*text, 1, 4000)) { + send_all(client, error_response(400, "invalid_request", + "invalid fields", request_id)); + } else { + const auto idem = headers.find("idempotency-key"); + if (idem == headers.end() || + !valid_idempotency_key(idem->second)) { + send_all(client, error_response( + 400, "invalid_request", + "invalid idempotency key", request_id)); + } else { + const auto created = messages.create_idempotent( + chat_id, user_id, *text, idem->second); + if (created.status == CreateResult::Status::NotFound) { + send_all(client, error_response(404, "not_found", + "not found", request_id)); + } else if (created.status == + CreateResult::Status::KeyConflict) { + send_all(client, error_response( + 409, "conflict", + "idempotency key reused", request_id)); + } else if (!created.message) { + send_all(client, error_response( + 500, "internal_error", + "internal error", request_id)); + } else { + send_all(client, http_response( + 201, "Created", request_id, + message_json(*created.message))); + } + } + } + } + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "post_message", request_id)); + } + } + } + else if (method == "GET" && match_chat_messages(path, chat_id)) { + if (send_auth_error()) { + } else if (send_user_error()) { + // ответ отправлен + } else if (!valid_uuid(chat_id)) { + send_all(client, error_response(400, "invalid_request", + "invalid chat id", request_id)); + } else { + try { + const auto chat = chats.find_by_id(chat_id); + const bool is_member = chat && + (std::find(chat->member_ids.begin(), + chat->member_ids.end(), + user_id) != chat->member_ids.end()); + if (!is_member) { + send_all(client, error_response(404, "not_found", + "not found", request_id)); + } else { + const auto params = parse_query(query); + int limit = 50; + bool bad_limit = false; + if (params.count("limit")) { + try { + std::size_t consumed = 0; + limit = std::stoi(params.at("limit"), &consumed); + if (consumed != params.at("limit").size() || + limit < 1 || limit > 100) { + bad_limit = true; + } + } catch (...) { + bad_limit = true; + } + } + std::optional cursor; + if (params.count("cursor")) { + cursor = params.at("cursor"); + } + if (bad_limit || (cursor && cursor->empty())) { + send_all(client, error_response(400, "invalid_request", + "bad query", request_id)); + } else { + const auto page = messages.list_page(chat_id, limit, cursor); + if (!page) { + send_all(client, error_response(400, "invalid_request", + "bad cursor", request_id)); + } else { + send_all(client, http_response(200, "OK", + request_id, + messages_page_json(*page))); + } + } + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "list_messages", request_id)); + } + } + } + else if (method == "GET" && path == "/api/v1/chats") { + if (send_auth_error()) { + // ответ отправлен + } else if (send_user_error()) { + // ответ отправлен + } else { + const auto params = parse_query(query); + int limit = 50; + bool bad_limit = false; + if (params.count("limit")) { + try { + std::size_t consumed = 0; + limit = std::stoi(params.at("limit"), &consumed); + if (consumed != params.at("limit").size() || + limit < 1 || limit > 100) { + bad_limit = true; + } + } catch (...) { + bad_limit = true; + } + } + std::optional cursor; + if (params.count("cursor")) { + cursor = params.at("cursor"); + } + if (bad_limit || (cursor && cursor->empty())) { + send_all(client, error_response(400, "invalid_request", + "bad query", request_id)); + } else { + try { + const auto page = chats.list_for_user(user_id, limit, cursor); + if (!page) { + send_all(client, error_response(400, "invalid_request", + "bad cursor", request_id)); + } else { + send_all(client, http_response(200, "OK", request_id, + chats_page_json(*page))); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "list_chats", request_id)); + } + } + } + } + else if (method == "GET" && match_id_path(path, "/api/v1/chats/", chat_id)) { + if (send_auth_error()) { + // ответ отправлен + } else if (send_user_error()) { + // ответ отправлен + } else if (!valid_uuid(chat_id)) { + send_all(client, error_response(400, "invalid_request", + "invalid chat id", request_id)); + } else { + try { + const auto chat = chats.find_by_id(chat_id); + const bool is_member = chat && + (std::find(chat->member_ids.begin(), + chat->member_ids.end(), + user_id) != chat->member_ids.end()); + if (!is_member) { + send_all(client, error_response(404, "not_found", + "not found", request_id)); + } else { + send_all(client, http_response(200, "OK", request_id, + chat_json(*chat))); + } + } catch (const std::exception& e) { + send_all(client, failure_response(e, "find_chat", request_id)); + } + } + } else if (method == "DELETE" && path == "/api/v1/auth/sessions/current") { + if (send_auth_error()) { + } else { + try { + sessions.revoke(bearer_token); + send_all(client, http_no_content(request_id)); + } catch (const std::exception& e) { + send_all(client, failure_response(e, "revoke_session", request_id)); + } + } + } + else { + send_all(client, error_response(404, "not_found", "not found", request_id)); + } + + live_del(client); + ::close(client); +} + +int env_or_default(const char* name, int fallback) { + const char* raw = std::getenv(name); + if (raw == nullptr || raw[0] == '\0') { + return fallback; + } + char* end = nullptr; + errno = 0; + const long value = std::strtol(raw, &end, 10); + if (end == raw || *end != '\0' || errno != 0 || value < 1 || value > 4096) { + return fallback; + } + return static_cast(value); +} + +int main() { + const char* host_env = std::getenv("APP_HOST"); + const char* port_env = std::getenv("APP_PORT"); + const char* host = host_env ? host_env : "0.0.0.0"; + const int port = port_env ? std::atoi(port_env) : 8080; + g_instance_id = make_instance_id(); + std::clog << "instance id: " << g_instance_id << "\n"; + + sigset_t set; + sigemptyset(&set); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGINT); + if (pthread_sigmask(SIG_BLOCK, &set, nullptr) != 0) { + std::cerr << "pthread_sigmask failed" << std::endl; + return 1; + } + + const std::string db_url = read_secret_file("DATABASE_URL_FILE"); + std::clog << "connecting to db...\n"; + { + auto conn = open_pg_connection(db_url, false); + std::clog << "applying migrations...\n"; + apply_migrations(*conn, "./migrations"); + std::clog << "migrations ok\n"; + } + + const int pool_n = env_or_default("DB_POOL_SIZE", 8); + PgPool pool(db_url, pool_n); + std::clog << "pg pool ready (" << pool_n << " connections)\n"; + + UserRepo users(pool); + ChatRepo chats(pool); + MessageRepo messages(pool); + + const char* ttl_env = std::getenv("SESSION_TTL_SECONDS"); + const int ttl_seconds = ttl_env ? std::atoi(ttl_env) : 3600; + + const char* store_env = std::getenv("SESSION_STORE"); + const std::string store = store_env ? store_env : "redis"; + + std::unique_ptr redis_pool; + std::unique_ptr sessions; + if (store == "postgres") { + sessions = std::make_unique(pool, ttl_seconds); + std::clog << "session store: postgres\n"; + } else if (store == "redis") { + redis_pool = std::make_unique(read_secret_file("CACHE_URL_FILE"), 4); + std::clog << "redis pool ready (4 connections)\n"; + sessions = std::make_unique(*redis_pool, ttl_seconds); + std::clog << "session store: redis\n"; + } else { + throw std::runtime_error("unknown SESSION_STORE: " + store); + } + std::clog << "session ttl: " << ttl_seconds << "s\n"; + + const int fd = ::socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + std::cerr << "socket failed" << std::endl; + return 1; + } + + const int yes = 1; + ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(static_cast(port)); + if (::inet_pton(AF_INET, host, &addr.sin_addr) != 1) { + std::cerr << "invalid APP_HOST" << std::endl; + ::close(fd); + return 1; + } + + if (::bind(fd, reinterpret_cast(&addr), sizeof(addr)) != 0) { + std::cerr << "bind failed on " << host << ":" << port << std::endl; + ::close(fd); + return 1; + } + + if (::listen(fd, 16) != 0) { + std::cerr << "listen failed" << std::endl; + ::close(fd); + return 1; + } + + g_listen.store(fd); + + std::clog << "listening on " << host << ":" << port << std::endl; + + std::atomic request_n{0}; + std::mutex q_mu; + std::condition_variable q_cv; + std::queue q; + const int workers_n = env_or_default("HTTP_WORKERS", 8); + std::clog << "http workers: " << workers_n << "\n"; + std::vector workers; + for (int i = 0; i < workers_n; ++i) { + workers.emplace_back([&]() { + for (;;) { + int client = -1; + { + std::unique_lock lk(q_mu); + q_cv.wait(lk, [&]() { return g_stop.load() || !q.empty(); }); + if (g_stop.load() && q.empty()) { + return; + } + client = q.front(); + q.pop(); + } + try { + handle_client(client, request_n, users, chats, messages, *sessions, pool, redis_pool.get()); + } catch (const std::exception& e) { + std::clog << "unhandled request error: " << e.what() << "\n"; + send_all(client, error_response(500, "internal_error", + "internal error", + std::to_string(request_n.load()))); + live_del(client); + ::close(client); + } catch (...) { + live_del(client); + ::close(client); + } + } + }); + } + + std::thread signal_thread([&]() { + int sig = 0; + if (sigwait(&set, &sig) != 0) { + return; + } + g_draining.store(true); + int drain_seconds = 2; + if (const char* env = std::getenv("DRAIN_SECONDS")) { + if (*env) { + drain_seconds = std::atoi(env); + if (drain_seconds < 0) { + drain_seconds = 0; + } else if (drain_seconds > 30) { + drain_seconds = 30; + } + } + } + std::clog << "draining: /health/ready is 503 for " + << drain_seconds << "s before listen socket closes\n"; + if (drain_seconds > 0) { + ::sleep(static_cast(drain_seconds)); + } + g_stop.store(true); + const int lfd = g_listen.load(); + if (lfd >= 0) { + ::shutdown(lfd, SHUT_RDWR); + } + }); + + for (;;) { + const int client = ::accept(fd, nullptr, nullptr); + if (client < 0) { + if (g_stop.load()) { + std::clog << "shutting down\n"; + break; + } + std::cerr << "accept failed\n"; + break; + } + set_timeouts(client); + live_add(client); + { + std::lock_guard lk(q_mu); + q.push(client); + } + q_cv.notify_one(); + } + { + std::lock_guard lk(q_mu); + g_stop.store(true); + } + q_cv.notify_all(); + for (auto& t : workers) { + t.join(); + } + signal_thread.detach(); + + ::close(fd); + std::clog << "stopped\n"; + return 0; +} \ No newline at end of file diff --git a/src/message_repo.cpp b/src/message_repo.cpp new file mode 100644 index 00000000..5adb1235 --- /dev/null +++ b/src/message_repo.cpp @@ -0,0 +1,297 @@ +#include "message_repo.hpp" +#include "cursor.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +std::string make_uuid() { + static std::mutex rng_mu; + static std::mt19937_64 rng{std::random_device{}()}; + std::uniform_int_distribution d16(0, 0xffff); + std::uniform_int_distribution d32(0, 0xffffffffu); + std::lock_guard lock(rng_mu); + std::ostringstream out; + out << std::hex << std::setfill('0'); + out << std::setw(8) << d32(rng); + out << '-' << std::setw(4) << d16(rng); + out << '-' << std::setw(4) << ((d16(rng) & 0x0fff) | 0x4000); + out << '-' << std::setw(4) << ((d16(rng) & 0x3fff) | 0x8000); + out << '-' << std::setw(4) << d16(rng) << std::setw(8) << d32(rng); + return out.str(); +} + +std::string sha256_hex(const std::string& s) { + unsigned char out[crypto_hash_sha256_BYTES]; + crypto_hash_sha256(out, reinterpret_cast(s.data()), s.size()); + static const char hex[] = "0123456789abcdef"; + std::string r; + r.resize(sizeof(out) * 2); + for (std::size_t i = 0; i < sizeof(out); ++i) { + r[i * 2] = hex[out[i] >> 4]; + r[i * 2 + 1] = hex[out[i] & 0x0f]; + } + return r; +} + +std::string epoch_to_rfc3339(long long epoch) { + const std::time_t t = static_cast(epoch); + std::tm tm{}; + gmtime_r(&t, &tm); + std::ostringstream out; + out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); + return out.str(); +} + +std::string now_epoch_str() { + const auto now = std::chrono::system_clock::now(); + const auto secs = std::chrono::duration_cast( + now.time_since_epoch()).count(); + return std::to_string(secs); +} + +Message row_to_message(const pqxx::row& r) { + Message m; + m.id = r["id"].as(); + m.chat_id = r["chat_id"].as(); + m.sender_id = r["sender_id"].as(); + m.text = r["text"].as(); + m.created_at = epoch_to_rfc3339(r["created_at_epoch"].as()); + return m; +} + +constexpr const char* kMessageSelect = + "id, chat_id, sender_id, text, " + "EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch"; + +// Lab3 принимает только text. Отсутствующий attachment_ids равен []. +// Порядок ключей JSON в хеш не входит: сравнивается семантика тела. +std::string canonical_message_body(const std::string& text) { + return "text=" + text + "\nattachment_ids="; +} + +int idempotency_ttl_seconds() { + const char* env = std::getenv("IDEMPOTENCY_TTL_SECONDS"); + if (env == nullptr || *env == '\0') { + return 86400; + } + try { + const int v = std::stoi(env); + if (v <= 0) { + return 86400; + } + return v; + } catch (...) { + return 86400; + } +} + +Message insert_message(pqxx::work& tx, + const std::string& chat_id, + const std::string& sender_id, + const std::string& text) { + const std::string id = make_uuid(); + const std::string created = now_epoch_str(); + auto r = tx.exec( + "INSERT INTO messages (id, chat_id, sender_id, text, created_at) " + "VALUES ($1, $2, $3, $4, to_timestamp($5::bigint)) " + "RETURNING " + std::string(kMessageSelect), + pqxx::params{id, chat_id, sender_id, text, created}); + return row_to_message(r[0]); +} + +bool insert_idempotency_key(pqxx::work& tx, + const std::string& user_id, + const std::string& chat_id, + const std::string& idem_key, + const std::string& request_hash, + const std::string& message_id) { + auto inserted = tx.exec( + "INSERT INTO idempotency_keys " + "(user_id, chat_id, key, request_hash, message_id) " + "VALUES ($1, $2, $3, $4, $5) " + "ON CONFLICT (user_id, chat_id, key) DO NOTHING " + "RETURNING message_id", + pqxx::params{user_id, chat_id, idem_key, request_hash, message_id}); + return !inserted.empty(); +} + +Message load_message(pqxx::work& tx, const std::string& message_id) { + auto r = tx.exec( + "SELECT " + std::string(kMessageSelect) + " FROM messages WHERE id = $1", + pqxx::params{message_id}); + if (r.empty()) { + throw std::runtime_error("idempotent message row is missing"); + } + return row_to_message(r[0]); +} + +} + +std::optional MessageRepo::create(const std::string& chat_id, + const std::string& sender_id, + const std::string& text) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + + auto member_check = tx.exec( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + pqxx::params{chat_id, sender_id}); + if (member_check.empty()) { + tx.commit(); + return std::nullopt; + } + + const std::string id = make_uuid(); + const std::string created = now_epoch_str(); + + auto r = tx.exec( + "INSERT INTO messages (id, chat_id, sender_id, text, created_at) " + "VALUES ($1, $2, $3, $4, to_timestamp($5::bigint)) " + "RETURNING " + std::string(kMessageSelect), + pqxx::params{id, chat_id, sender_id, text, created}); + + tx.commit(); + return row_to_message(r[0]); +} + +std::optional MessageRepo::list_page( + const std::string& chat_id, + int limit, + const std::optional& cursor) { + + std::optional cur; + if (cursor) { + cur = parse_cursor(*cursor); + if (!cur) { + return std::nullopt; + } + } + + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + + if (cur) { + auto owner_check = tx.exec( + "SELECT 1 FROM messages WHERE id = $1 AND chat_id = $2", + pqxx::params{cur->id, chat_id}); + if (owner_check.empty()) { + tx.commit(); + return std::nullopt; + } + } + + std::string sql = + "SELECT " + std::string(kMessageSelect) + ", " + kCreatedUsSql + " " + "FROM messages " + "WHERE chat_id = $1 "; + if (cur) { + sql += "AND (created_at, id) > (" + std::string(kCursorTimestampSql) + ", $4::uuid) "; + } + sql += "ORDER BY created_at ASC, id ASC LIMIT $2"; + + pqxx::result r; + if (cur) { + r = tx.exec(sql, pqxx::params{chat_id, limit + 1, cur->created_us, cur->id}); + } else { + r = tx.exec(sql, pqxx::params{chat_id, limit + 1}); + } + + MessagePage page; + const bool has_more = (static_cast(r.size()) > limit); + const int take = has_more ? limit : static_cast(r.size()); + page.items.reserve(take); + for (int i = 0; i < take; ++i) { + page.items.push_back(row_to_message(r[static_cast(i)])); + } + + if (has_more && take > 0) { + const auto& last = r[static_cast(take - 1)]; + page.next_cursor = make_cursor(last["created_us"].as(), + last["id"].as()); + } + + tx.commit(); + return page; +} + +CreateResult MessageRepo::create_idempotent(const std::string& chat_id, + const std::string& sender_id, + const std::string& text, + const std::string& idem_key) { + const int ttl = idempotency_ttl_seconds(); + const std::string hash = sha256_hex(canonical_message_body(text)); + + for (int attempt = 0; attempt < 3; ++attempt) { + { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + + auto member_check = tx.exec( + "SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2", + pqxx::params{chat_id, sender_id}); + if (member_check.empty()) { + tx.commit(); + return CreateResult{CreateResult::Status::NotFound, std::nullopt}; + } + + const Message created = insert_message(tx, chat_id, sender_id, text); + if (insert_idempotency_key(tx, sender_id, chat_id, idem_key, hash, + created.id)) { + tx.commit(); + return CreateResult{CreateResult::Status::Created, created}; + } + } + + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto existing = tx.exec( + "SELECT request_hash, message_id, " + "EXTRACT(EPOCH FROM (now() - created_at))::bigint AS age_seconds " + "FROM idempotency_keys " + "WHERE user_id = $1 AND chat_id = $2 AND key = $3 " + "FOR UPDATE", + pqxx::params{sender_id, chat_id, idem_key}); + if (existing.empty()) { + tx.commit(); + continue; + } + + const std::string existing_hash = existing[0]["request_hash"].as(); + const long long age = existing[0]["age_seconds"].as(); + const std::string message_id = existing[0]["message_id"].as(); + if (age < ttl) { + if (existing_hash != hash) { + tx.commit(); + return CreateResult{CreateResult::Status::KeyConflict, std::nullopt}; + } + const Message replayed = load_message(tx, message_id); + tx.commit(); + return CreateResult{CreateResult::Status::Replayed, replayed}; + } + + tx.exec( + "DELETE FROM idempotency_keys " + "WHERE user_id = $1 AND chat_id = $2 AND key = $3", + pqxx::params{sender_id, chat_id, idem_key}); + const Message created = insert_message(tx, chat_id, sender_id, text); + if (insert_idempotency_key(tx, sender_id, chat_id, idem_key, hash, + created.id)) { + tx.commit(); + return CreateResult{CreateResult::Status::Created, created}; + } + } + + throw std::runtime_error("idempotency retry exhausted"); +} \ No newline at end of file diff --git a/src/message_repo.hpp b/src/message_repo.hpp new file mode 100644 index 00000000..932a1dbb --- /dev/null +++ b/src/message_repo.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "db.hpp" +#include "models.hpp" + +#include +#include +#include + + +struct CreateResult { + enum class Status { Created, Replayed, NotFound, KeyConflict }; + Status status; + std::optional message; +}; + +class MessageRepo { +public: + explicit MessageRepo(PgPool& pool) : pool_(pool) {} + + std::optional create(const std::string& chat_id, + const std::string& sender_id, + const std::string& text); + + std::optional list_page( + const std::string& chat_id, + int limit, + const std::optional& cursor); + + CreateResult create_idempotent(const std::string& chat_id, + const std::string& sender_id, + const std::string& text, + const std::string& idem_key); + +private: + PgPool& pool_; +}; \ No newline at end of file diff --git a/src/models.hpp b/src/models.hpp new file mode 100644 index 00000000..87c3fe30 --- /dev/null +++ b/src/models.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + +struct User { + std::string id; + std::string username; + std::string display_name; + std::string password_hash; + std::string created_at; +}; + +struct Chat { + std::string id; + std::string title; + std::vector member_ids; + std::string created_at; +}; + +struct Message { + std::string id; + std::string chat_id; + std::string sender_id; + std::string text; + std::string created_at; +}; + +struct MessagePage { + std::vector items; + std::optional next_cursor; +}; + +struct ChatPage { + std::vector items; + std::optional next_cursor; +}; \ No newline at end of file diff --git a/src/pg_session_repo.cpp b/src/pg_session_repo.cpp new file mode 100644 index 00000000..507f2ddd --- /dev/null +++ b/src/pg_session_repo.cpp @@ -0,0 +1,66 @@ +#include "pg_session_repo.hpp" + +#include +#include + +#include +#include +#include + +namespace { + +std::string random_token_hex() { + unsigned char buf[32]; + randombytes_buf(buf, sizeof(buf)); + static const char hex[] = "0123456789abcdef"; + std::string out; + out.resize(sizeof(buf) * 2); + for (std::size_t i = 0; i < sizeof(buf); ++i) { + out[i * 2] = hex[buf[i] >> 4]; + out[i * 2 + 1] = hex[buf[i] & 0x0f]; + } + return out; +} + +std::int64_t now_unix() { + return static_cast(std::time(nullptr)); +} + +} + +ISessionRepo::Created PgSessionRepo::create(const std::string& user_id) { + const std::string token = random_token_hex(); + const std::int64_t exp = now_unix() + ttl_; + + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + tx.exec( + "INSERT INTO sessions (token, user_id, expires_at) " + "VALUES ($1, $2, to_timestamp($3::bigint))", + pqxx::params{token, user_id, exp}); + tx.commit(); + + return Created{token, exp}; +} + +std::optional PgSessionRepo::lookup(const std::string& token) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto r = tx.exec( + "SELECT user_id FROM sessions " + "WHERE token = $1 AND expires_at > now()", + pqxx::params{token}); + tx.commit(); + if (r.empty()) return std::nullopt; + return r[0]["user_id"].as(); +} + +bool PgSessionRepo::revoke(const std::string& token) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto r = tx.exec( + "DELETE FROM sessions WHERE token = $1", + pqxx::params{token}); + tx.commit(); + return r.affected_rows() > 0; +} \ No newline at end of file diff --git a/src/pg_session_repo.hpp b/src/pg_session_repo.hpp new file mode 100644 index 00000000..ed005fec --- /dev/null +++ b/src/pg_session_repo.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "db.hpp" +#include "session_repo_base.hpp" + +class PgSessionRepo : public ISessionRepo { +public: + PgSessionRepo(PgPool& pool, int ttl_seconds) + : pool_(pool), ttl_(ttl_seconds) {} + + Created create(const std::string& user_id) override; + std::optional lookup(const std::string& token) override; + bool revoke(const std::string& token) override; + +private: + PgPool& pool_; + int ttl_; +}; \ No newline at end of file diff --git a/src/redis_pool.cpp b/src/redis_pool.cpp new file mode 100644 index 00000000..035b5f20 --- /dev/null +++ b/src/redis_pool.cpp @@ -0,0 +1,216 @@ +#include "redis_pool.hpp" +#include "errors.hpp" + +#include +#include +#include +#include + +namespace { + +constexpr timeval kConnectTimeout{2, 0}; +constexpr timeval kCommandTimeout{2, 0}; +constexpr auto kAcquireTimeout = std::chrono::seconds(3); + +struct RedisUrl { + std::string host = "127.0.0.1"; + int port = 6379; + std::string password; + int db = 0; +}; + +RedisUrl parse_redis_url(const std::string& url) { + RedisUrl out; + const std::string prefix = "redis://"; + if (url.compare(0, prefix.size(), prefix) != 0) { + throw std::runtime_error("redis url must start with redis://"); + } + std::string rest = url.substr(prefix.size()); + + const auto slash = rest.find('/'); + if (slash != std::string::npos) { + const std::string db_str = rest.substr(slash + 1); + rest.resize(slash); + if (!db_str.empty()) { + out.db = std::atoi(db_str.c_str()); + } + } + + const auto at = rest.find('@'); + std::string hostport = rest; + if (at != std::string::npos) { + const std::string creds = rest.substr(0, at); + hostport = rest.substr(at + 1); + const auto colon = creds.find(':'); + if (colon != std::string::npos) { + out.password = creds.substr(colon + 1); + } else { + out.password = creds; + } + } + + const auto colon = hostport.find(':'); + if (colon == std::string::npos) { + out.host = hostport; + } else { + out.host = hostport.substr(0, colon); + out.port = std::atoi(hostport.substr(colon + 1).c_str()); + } + + return out; +} + +redisContext* make_connection(const RedisUrl& u) { + redisContext* c = redisConnectWithTimeout(u.host.c_str(), u.port, kConnectTimeout); + if (c == nullptr) { + throw std::runtime_error("redisConnect: allocation failed"); + } + if (c->err) { + const std::string err = c->errstr; + redisFree(c); + throw DependencyUnavailable("redisConnect failed: " + err); + } + if (redisSetTimeout(c, kCommandTimeout) != REDIS_OK) { + redisFree(c); + throw DependencyUnavailable("redisSetTimeout failed"); + } + if (!u.password.empty()) { + redisReply* r = static_cast( + redisCommand(c, "AUTH %s", u.password.c_str())); + if (r == nullptr) { + redisFree(c); + throw std::runtime_error("redis AUTH: no reply"); + } + const bool ok = (r->type == REDIS_REPLY_STATUS); + freeReplyObject(r); + if (!ok) { + redisFree(c); + throw std::runtime_error("redis AUTH failed"); + } + } + if (u.db != 0) { + redisReply* r = static_cast( + redisCommand(c, "SELECT %d", u.db)); + if (r == nullptr) { + redisFree(c); + throw std::runtime_error("redis SELECT: no reply"); + } + const bool ok = (r->type == REDIS_REPLY_STATUS); + freeReplyObject(r); + if (!ok) { + redisFree(c); + throw std::runtime_error("redis SELECT failed"); + } + } + return c; +} + +} + +RedisPool::RedisPool(std::string url, std::size_t n) + : url_(std::move(url)) { + if (n == 0) { + throw std::runtime_error("RedisPool: n must be > 0"); + } + const RedisUrl parsed = parse_redis_url(url_); + all_.reserve(n); + idle_.reserve(n); + for (std::size_t i = 0; i < n; ++i) { + ConnPtr c(make_connection(parsed)); + idle_.push_back(c.get()); + all_.push_back(std::move(c)); + } +} + +RedisPool::~RedisPool() { + { + std::lock_guard lk(mu_); + shutting_down_ = true; + } + cv_.notify_all(); +} + +RedisPool::Lease::Lease(RedisPool& pool) + : pool_(&pool), ctx_(pool.take()) {} + +RedisPool::Lease::~Lease() { + if (pool_ != nullptr && ctx_ != nullptr) { + pool_->give_back(ctx_); + } +} + +RedisPool::Lease::Lease(Lease&& other) noexcept + : pool_(other.pool_), ctx_(other.ctx_) { + other.pool_ = nullptr; + other.ctx_ = nullptr; +} + +RedisPool::Lease RedisPool::acquire() { + return Lease(*this); +} + +redisContext* RedisPool::take() { + std::unique_lock lk(mu_); + if (!cv_.wait_for(lk, kAcquireTimeout, + [&]() { return shutting_down_ || !idle_.empty(); })) { + throw DependencyUnavailable("redis pool: acquire timeout"); + } + if (shutting_down_) { + throw DependencyUnavailable("redis pool is shutting down"); + } + redisContext* c = idle_.back(); + idle_.pop_back(); + lk.unlock(); + + bool alive = false; + if (!c->err) { + redisReply* r = static_cast(redisCommand(c, "PING")); + if (r != nullptr && r->type == REDIS_REPLY_STATUS) { + alive = true; + } + if (r != nullptr) freeReplyObject(r); + } + if (alive) { + return c; + } + + ConnPtr fresh; + try { + fresh.reset(make_connection(parse_redis_url(url_))); + } catch (const std::exception& e) { + // Слот возвращается в пул, иначе после нескольких неудач пул опустеет навсегда. + give_back(c); + throw DependencyUnavailable(std::string("redis reconnect failed: ") + e.what()); + } + + std::lock_guard lk2(mu_); + for (auto& up : all_) { + if (up.get() == c) { + up = std::move(fresh); + return up.get(); + } + } + throw std::logic_error("RedisPool: connection not owned by pool"); +} + +void RedisPool::give_back(redisContext* c) { + { + std::lock_guard lk(mu_); + idle_.push_back(c); + } + cv_.notify_one(); +} + +bool RedisPool::ping() { + try { + auto lease = acquire(); + redisContext* c = lease.ctx(); + redisReply* r = static_cast(redisCommand(c, "PING")); + if (r == nullptr) return false; + const bool ok = (r->type == REDIS_REPLY_STATUS); + freeReplyObject(r); + return ok; + } catch (...) { + return false; + } +} \ No newline at end of file diff --git a/src/redis_pool.hpp b/src/redis_pool.hpp new file mode 100644 index 00000000..00323151 --- /dev/null +++ b/src/redis_pool.hpp @@ -0,0 +1,59 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +class RedisPool { +public: + RedisPool(std::string url, std::size_t n); + ~RedisPool(); + + RedisPool(const RedisPool&) = delete; + RedisPool& operator=(const RedisPool&) = delete; + + class Lease { + public: + explicit Lease(RedisPool& pool); + ~Lease(); + Lease(Lease&& other) noexcept; + Lease& operator=(Lease&&) = delete; + Lease(const Lease&) = delete; + Lease& operator=(const Lease&) = delete; + + redisContext* ctx() const { return ctx_; } + + private: + RedisPool* pool_; + redisContext* ctx_; + }; + + Lease acquire(); + + bool ping(); + +private: + friend class Lease; + + struct RedisDeleter { + void operator()(redisContext* c) const noexcept { + if (c != nullptr) ::redisFree(c); + } + }; + using ConnPtr = std::unique_ptr; + + redisContext* take(); + void give_back(redisContext* c); + + std::string url_; + std::vector all_; + std::vector idle_; + std::mutex mu_; + std::condition_variable cv_; + bool shutting_down_ = false; +}; \ No newline at end of file diff --git a/src/session_repo.cpp b/src/session_repo.cpp new file mode 100644 index 00000000..28f1edbb --- /dev/null +++ b/src/session_repo.cpp @@ -0,0 +1,87 @@ +#include "session_repo.hpp" +#include "errors.hpp" + +#include + +#include +#include +#include +#include + +namespace { + +std::string random_token_hex() { + unsigned char buf[32]; + randombytes_buf(buf, sizeof(buf)); + static const char hex[] = "0123456789abcdef"; + std::string out; + out.resize(sizeof(buf) * 2); + for (std::size_t i = 0; i < sizeof(buf); ++i) { + out[i * 2] = hex[buf[i] >> 4]; + out[i * 2 + 1] = hex[buf[i] & 0x0f]; + } + return out; +} + +std::int64_t now_unix() { + return static_cast(std::time(nullptr)); +} + +redisReply* check_reply(redisContext* c, redisReply* r) { + if (r == nullptr) { + const std::string err = c->err ? c->errstr : "no reply"; + throw DependencyUnavailable("redis command failed: " + err); + } + return r; +} + +} + +ISessionRepo::Created SessionRepo::create(const std::string& user_id) { + const std::string token = random_token_hex(); + const std::int64_t exp = now_unix() + ttl_; + + auto lease = pool_.acquire(); + redisContext* c = lease.ctx(); + + redisReply* r = static_cast( + redisCommand(c, "SET session:%s %s EX %d", + token.c_str(), user_id.c_str(), ttl_)); + check_reply(c, r); + const bool ok = (r->type == REDIS_REPLY_STATUS); + freeReplyObject(r); + if (!ok) { + throw std::runtime_error("redis SET returned non-status"); + } + + return Created{token, exp}; +} + +std::optional SessionRepo::lookup(const std::string& token) { + auto lease = pool_.acquire(); + redisContext* c = lease.ctx(); + + redisReply* r = static_cast( + redisCommand(c, "GET session:%s", token.c_str())); + check_reply(c, r); + + std::optional out; + if (r->type == REDIS_REPLY_STRING) { + out = std::string(r->str, r->len); + } + freeReplyObject(r); + return out; +} + +bool SessionRepo::revoke(const std::string& token) { + auto lease = pool_.acquire(); + redisContext* c = lease.ctx(); + + redisReply* r = static_cast( + redisCommand(c, "DEL session:%s", token.c_str())); + check_reply(c, r); + + const bool existed = (r->type == REDIS_REPLY_INTEGER && r->integer == 1); + freeReplyObject(r); + return existed; +} \ No newline at end of file diff --git a/src/session_repo.hpp b/src/session_repo.hpp new file mode 100644 index 00000000..550eae6b --- /dev/null +++ b/src/session_repo.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "redis_pool.hpp" +#include "session_repo_base.hpp" + +class SessionRepo : public ISessionRepo { +public: + SessionRepo(RedisPool& pool, int ttl_seconds) + : pool_(pool), ttl_(ttl_seconds) {} + + Created create(const std::string& user_id) override; + std::optional lookup(const std::string& token) override; + bool revoke(const std::string& token) override; + +private: + RedisPool& pool_; + int ttl_; +}; \ No newline at end of file diff --git a/src/session_repo_base.hpp b/src/session_repo_base.hpp new file mode 100644 index 00000000..4a685ba2 --- /dev/null +++ b/src/session_repo_base.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + + +class ISessionRepo { +public: + virtual ~ISessionRepo() = default; + + struct Created { + std::string token; + std::int64_t expires_at; // unix-время в секундах UTC + }; + + virtual Created create(const std::string& user_id) = 0; + + virtual std::optional lookup(const std::string& token) = 0; + + virtual bool revoke(const std::string& token) = 0; +}; \ No newline at end of file diff --git a/src/user_repo.cpp b/src/user_repo.cpp new file mode 100644 index 00000000..2cc4bc9c --- /dev/null +++ b/src/user_repo.cpp @@ -0,0 +1,100 @@ +#include "user_repo.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +namespace { + +std::string make_uuid() { + static std::mt19937_64 rng{std::random_device{}()}; + std::uniform_int_distribution d16(0, 0xffff); + std::uniform_int_distribution d32(0, 0xffffffffu); + std::ostringstream out; + out << std::hex << std::setfill('0'); + out << std::setw(8) << d32(rng); + out << '-' << std::setw(4) << d16(rng); + out << '-' << std::setw(4) << ((d16(rng) & 0x0fff) | 0x4000); + out << '-' << std::setw(4) << ((d16(rng) & 0x3fff) | 0x8000); + out << '-' << std::setw(4) << d16(rng) << std::setw(8) << d32(rng); + return out.str(); +} + +std::string epoch_to_rfc3339(long long epoch) { + const std::time_t t = static_cast(epoch); + std::tm tm{}; + gmtime_r(&t, &tm); + std::ostringstream out; + out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); + return out.str(); +} + +std::string now_epoch_str() { + const auto now = std::chrono::system_clock::now(); + const auto secs = std::chrono::duration_cast( + now.time_since_epoch()).count(); + return std::to_string(secs); +} + +User row_to_user(const pqxx::row& r) { + User u; + u.id = r["id"].as(); + u.username = r["username"].as(); + u.display_name = r["display_name"].as(); + u.password_hash = r["password_hash"].as(); + u.created_at = epoch_to_rfc3339(r["created_at_epoch"].as()); + return u; +} + +constexpr const char* kUserColumns = + "id, username, display_name, password_hash, " + "EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch"; + +} + +std::optional UserRepo::create(const std::string& username, + const std::string& display_name, + const std::string& password_hash) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + try { + const std::string id = make_uuid(); + auto r = tx.exec( + "INSERT INTO users (id, username, display_name, password_hash, created_at) " + "VALUES ($1, $2, $3, $4, to_timestamp($5::bigint)) " + "RETURNING " + std::string(kUserColumns), + pqxx::params{id, username, display_name, password_hash, now_epoch_str()}); + tx.commit(); + return row_to_user(r[0]); + } catch (const pqxx::unique_violation&) { + tx.abort(); + return std::nullopt; + } +} + +std::optional UserRepo::find_by_id(const std::string& id) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto r = tx.exec( + "SELECT " + std::string(kUserColumns) + " FROM users WHERE id = $1", + pqxx::params{id}); + tx.commit(); + if (r.empty()) return std::nullopt; + return row_to_user(r[0]); +} + +std::optional UserRepo::find_by_username(const std::string& username) { + auto lease = pool_.acquire(); + pqxx::work tx(*lease); + auto r = tx.exec( + "SELECT " + std::string(kUserColumns) + " FROM users WHERE username = $1", + pqxx::params{username}); + tx.commit(); + if (r.empty()) return std::nullopt; + return row_to_user(r[0]); +} \ No newline at end of file diff --git a/src/user_repo.hpp b/src/user_repo.hpp new file mode 100644 index 00000000..b8cbada2 --- /dev/null +++ b/src/user_repo.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "db.hpp" +#include "models.hpp" + +#include +#include + +class UserRepo { +public: + explicit UserRepo(PgPool& pool) : pool_(pool) {} + + std::optional create(const std::string& username, + const std::string& display_name, + const std::string& password_hash); + + std::optional find_by_id(const std::string& id); + std::optional find_by_username(const std::string& username); + +private: + PgPool& pool_; +}; \ No newline at end of file diff --git a/student-tests/idempotency_parallel.py b/student-tests/idempotency_parallel.py new file mode 100644 index 00000000..32ffc8cb --- /dev/null +++ b/student-tests/idempotency_parallel.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +"""Same Idempotency-Key from several threads, then the same key in another chat.""" +import base64 +import json +import os +import threading +import urllib.error +import urllib.request +import uuid + +BASE = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/") + + +def call(method, path, expected, body=None, headers=None): + h = {"Accept": "application/json"} + raw = None + if body is not None: + raw = json.dumps(body, ensure_ascii=False).encode() + h["Content-Type"] = "application/json" + h.update(headers or {}) + req = urllib.request.Request(BASE + path, data=raw, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=10) + except urllib.error.HTTPError as exc: + resp = exc + with resp: + data = resp.read() + payload = json.loads(data) if data else None + if resp.code != expected: + raise SystemExit( + method + " " + path.split("?")[0] + ": expected " + + str(expected) + ", got " + str(resp.code) + ) + return payload, resp.headers.get("X-Instance-Id") + + +def main(): + username = "u_par_" + uuid.uuid4().hex[:12] + password = "T3st-" + uuid.uuid4().hex + user, _ = call( + "POST", "/api/v1/users", 201, + {"username": username, "display_name": "Параллельный", "password": password}, + ) + basic = base64.b64encode((username + ":" + password).encode()).decode() + session, _ = call( + "POST", "/api/v1/auth/sessions", 201, + headers={"Authorization": "Basic " + basic}, + ) + auth = {"Authorization": "Bearer " + session["token"]} + chat_a, _ = call( + "POST", "/api/v1/chats", 201, + {"title": "A", "member_ids": [user["id"]]}, + auth, + ) + chat_b, _ = call( + "POST", "/api/v1/chats", 201, + {"title": "B", "member_ids": [user["id"]]}, + auth, + ) + key = "same-" + uuid.uuid4().hex + text = "одно сообщение " + key + results = [] + errors = [] + lock = threading.Lock() + + def post(): + try: + payload, instance = call( + "POST", "/api/v1/chats/" + chat_a["id"] + "/messages", 201, + {"text": text}, + {**auth, "Idempotency-Key": key}, + ) + with lock: + results.append((payload["id"], instance)) + except SystemExit as exc: + with lock: + errors.append(str(exc)) + + threads = [threading.Thread(target=post) for _ in range(8)] + for t in threads: + t.start() + for t in threads: + t.join() + if errors: + raise SystemExit("parallel post failed: " + "; ".join(errors)) + ids = {item[0] for item in results} + instances = {item[1] for item in results} + if len(ids) != 1: + raise SystemExit("parallel retry created " + str(len(ids)) + " messages") + other, _ = call( + "POST", "/api/v1/chats/" + chat_b["id"] + "/messages", 201, + {"text": text}, + {**auth, "Idempotency-Key": key}, + ) + if other["id"] == next(iter(ids)): + raise SystemExit("same key in another chat reused the first message") + conflict, _ = call( + "POST", "/api/v1/chats/" + chat_a["id"] + "/messages", 409, + {"text": "другое тело"}, + {**auth, "Idempotency-Key": key}, + ) + if conflict["error"]["code"] != "conflict": + raise SystemExit("conflict code mismatch") + print(json.dumps({ + "parallel_responses": len(results), + "unique_message_ids": len(ids), + "instances_seen": len(instances), + "other_chat_independent": True, + "different_body": 409, + }, ensure_ascii=False)) + + +if __name__ == "__main__": + main()