ЛР3 - БВТ2301 - Никитин - на 5 #38
@@ -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
|
||||
|
||||
@@ -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
|
||||
)
|
||||
+49
@@ -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"]
|
||||
@@ -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 ядра, ошибок не было.
|
||||
|
||||

|
||||
|
||||
Рост числа реплик при том же суммарном 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.
|
||||
|
||||

|
||||
|
||||
### Тюнинг 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, хеш идемпотентности нужно расширить, не меняя смысл уже сохранённых ключей.
|
||||
|
||||
+106
@@ -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: {}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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 <docker-maint@nginx.com>]"
|
||||
],
|
||||
"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."
|
||||
}
|
||||
}
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
Executable
+38
@@ -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
|
||||
Executable
+374
@@ -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()
|
||||
Executable
+53
@@ -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/"
|
||||
Executable
+15
@@ -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"
|
||||
Executable
+29
@@ -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/"
|
||||
Executable
+24
@@ -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
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "auth.hpp"
|
||||
|
||||
#include <sodium.h>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
std::string hash_password(const std::string& plain);
|
||||
|
||||
bool verify_password(const std::string& plain, const std::string& hash);
|
||||
@@ -0,0 +1,186 @@
|
||||
#include "chat_repo.hpp"
|
||||
#include "cursor.hpp"
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string make_uuid() {
|
||||
static std::mt19937_64 rng{std::random_device{}()};
|
||||
std::uniform_int_distribution<uint32_t> d16(0, 0xffff);
|
||||
std::uniform_int_distribution<uint32_t> 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<std::time_t>(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<std::chrono::seconds>(
|
||||
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<std::string>();
|
||||
c.title = r["title"].as<std::string>();
|
||||
c.created_at = epoch_to_rfc3339(r["created_at_epoch"].as<long long>());
|
||||
return c;
|
||||
}
|
||||
|
||||
std::vector<std::string> load_member_ids(pqxx::work& tx,
|
||||
const std::string& chat_id) {
|
||||
std::vector<std::string> 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<std::string>());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
constexpr const char* kChatSelect =
|
||||
"id, title, "
|
||||
"EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch";
|
||||
|
||||
}
|
||||
|
||||
std::optional<Chat> ChatRepo::create(const std::string& title,
|
||||
const std::vector<std::string>& 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<Chat> 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<ChatPage> ChatRepo::list_for_user(
|
||||
const std::string& user_id,
|
||||
int limit,
|
||||
const std::optional<std::string>& cursor) {
|
||||
|
||||
std::optional<Cursor> 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<int>(r.size()) > limit);
|
||||
const int take = has_more ? limit : static_cast<int>(r.size());
|
||||
page.items.reserve(take);
|
||||
for (int i = 0; i < take; ++i) {
|
||||
Chat c = row_to_chat(r[static_cast<std::size_t>(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<std::size_t>(take - 1)];
|
||||
page.next_cursor = make_cursor(last["created_us"].as<std::string>(),
|
||||
last["id"].as<std::string>());
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return page;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "db.hpp"
|
||||
#include "models.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class ChatRepo {
|
||||
public:
|
||||
explicit ChatRepo(PgPool& pool) : pool_(pool) {}
|
||||
|
||||
std::optional<Chat> create(const std::string& title,
|
||||
const std::vector<std::string>& member_ids);
|
||||
|
||||
std::optional<Chat> find_by_id(const std::string& id);
|
||||
|
||||
std::optional<ChatPage> list_for_user(const std::string& user_id, int limit, const std::optional<std::string>& cursor);
|
||||
|
||||
private:
|
||||
PgPool& pool_;
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
// Непрозрачный для клиента курсор keyset-пагинации: "<created_at в мкс от epoch>|<uuid>".
|
||||
// Микросекунды сохраняют точный 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<Cursor> 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;
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
#include "db.hpp"
|
||||
#include "errors.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<void> 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<Migration> 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<int> applied;
|
||||
{
|
||||
pqxx::work tx(conn);
|
||||
for (const auto& row : tx.exec("SELECT version FROM schema_migrations")) {
|
||||
applied.insert(row[0].as<int>());
|
||||
}
|
||||
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<const DependencyUnavailable*>(&e) != nullptr ||
|
||||
dynamic_cast<const pqxx::broken_connection*>(&e) != nullptr ||
|
||||
dynamic_cast<const pqxx::in_doubt_error*>(&e) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
if (const auto* sql = dynamic_cast<const pqxx::sql_error*>(&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<pqxx::connection> 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<pqxx::connection>(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<std::mutex> lk(mu_);
|
||||
shutting_down_ = true;
|
||||
}
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
|
||||
pqxx::connection* PgPool::take() {
|
||||
std::unique_lock<std::mutex> 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<pqxx::connection> 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<std::mutex> 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<std::mutex> 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;
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
#include <condition_variable>
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
std::string read_secret_file(const char* env_name);
|
||||
|
||||
std::unique_ptr<pqxx::connection> 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<std::unique_ptr<pqxx::connection>> all_;
|
||||
std::vector<pqxx::connection*> idle_;
|
||||
std::mutex mu_;
|
||||
std::condition_variable cv_;
|
||||
bool shutting_down_ = false;
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
// PostgreSQL или Valkey недоступны либо не ответили вовремя: клиент получает 503.
|
||||
struct DependencyUnavailable : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
bool is_dependency_failure(const std::exception& e);
|
||||
+1395
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,297 @@
|
||||
#include "message_repo.hpp"
|
||||
#include "cursor.hpp"
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sodium.h>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string make_uuid() {
|
||||
static std::mutex rng_mu;
|
||||
static std::mt19937_64 rng{std::random_device{}()};
|
||||
std::uniform_int_distribution<uint32_t> d16(0, 0xffff);
|
||||
std::uniform_int_distribution<uint32_t> d32(0, 0xffffffffu);
|
||||
std::lock_guard<std::mutex> 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<const unsigned char*>(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<std::time_t>(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<std::chrono::seconds>(
|
||||
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<std::string>();
|
||||
m.chat_id = r["chat_id"].as<std::string>();
|
||||
m.sender_id = r["sender_id"].as<std::string>();
|
||||
m.text = r["text"].as<std::string>();
|
||||
m.created_at = epoch_to_rfc3339(r["created_at_epoch"].as<long long>());
|
||||
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<Message> 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<MessagePage> MessageRepo::list_page(
|
||||
const std::string& chat_id,
|
||||
int limit,
|
||||
const std::optional<std::string>& cursor) {
|
||||
|
||||
std::optional<Cursor> 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<int>(r.size()) > limit);
|
||||
const int take = has_more ? limit : static_cast<int>(r.size());
|
||||
page.items.reserve(take);
|
||||
for (int i = 0; i < take; ++i) {
|
||||
page.items.push_back(row_to_message(r[static_cast<std::size_t>(i)]));
|
||||
}
|
||||
|
||||
if (has_more && take > 0) {
|
||||
const auto& last = r[static_cast<std::size_t>(take - 1)];
|
||||
page.next_cursor = make_cursor(last["created_us"].as<std::string>(),
|
||||
last["id"].as<std::string>());
|
||||
}
|
||||
|
||||
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<std::string>();
|
||||
const long long age = existing[0]["age_seconds"].as<long long>();
|
||||
const std::string message_id = existing[0]["message_id"].as<std::string>();
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "db.hpp"
|
||||
#include "models.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
struct CreateResult {
|
||||
enum class Status { Created, Replayed, NotFound, KeyConflict };
|
||||
Status status;
|
||||
std::optional<Message> message;
|
||||
};
|
||||
|
||||
class MessageRepo {
|
||||
public:
|
||||
explicit MessageRepo(PgPool& pool) : pool_(pool) {}
|
||||
|
||||
std::optional<Message> create(const std::string& chat_id,
|
||||
const std::string& sender_id,
|
||||
const std::string& text);
|
||||
|
||||
std::optional<MessagePage> list_page(
|
||||
const std::string& chat_id,
|
||||
int limit,
|
||||
const std::optional<std::string>& 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_;
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string> 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<Message> items;
|
||||
std::optional<std::string> next_cursor;
|
||||
};
|
||||
|
||||
struct ChatPage {
|
||||
std::vector<Chat> items;
|
||||
std::optional<std::string> next_cursor;
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "pg_session_repo.hpp"
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
#include <sodium.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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::int64_t>(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<std::string> 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<std::string>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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<std::string> lookup(const std::string& token) override;
|
||||
bool revoke(const std::string& token) override;
|
||||
|
||||
private:
|
||||
PgPool& pool_;
|
||||
int ttl_;
|
||||
};
|
||||
@@ -0,0 +1,216 @@
|
||||
#include "redis_pool.hpp"
|
||||
#include "errors.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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<redisReply*>(
|
||||
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<redisReply*>(
|
||||
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<std::mutex> 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<std::mutex> 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<redisReply*>(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<std::mutex> 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<std::mutex> lk(mu_);
|
||||
idle_.push_back(c);
|
||||
}
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
bool RedisPool::ping() {
|
||||
try {
|
||||
auto lease = acquire();
|
||||
redisContext* c = lease.ctx();
|
||||
redisReply* r = static_cast<redisReply*>(redisCommand(c, "PING"));
|
||||
if (r == nullptr) return false;
|
||||
const bool ok = (r->type == REDIS_REPLY_STATUS);
|
||||
freeReplyObject(r);
|
||||
return ok;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <hiredis/hiredis.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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, RedisDeleter>;
|
||||
|
||||
redisContext* take();
|
||||
void give_back(redisContext* c);
|
||||
|
||||
std::string url_;
|
||||
std::vector<ConnPtr> all_;
|
||||
std::vector<redisContext*> idle_;
|
||||
std::mutex mu_;
|
||||
std::condition_variable cv_;
|
||||
bool shutting_down_ = false;
|
||||
};
|
||||
@@ -0,0 +1,87 @@
|
||||
#include "session_repo.hpp"
|
||||
#include "errors.hpp"
|
||||
|
||||
#include <sodium.h>
|
||||
|
||||
#include <ctime>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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::int64_t>(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<redisReply*>(
|
||||
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<std::string> SessionRepo::lookup(const std::string& token) {
|
||||
auto lease = pool_.acquire();
|
||||
redisContext* c = lease.ctx();
|
||||
|
||||
redisReply* r = static_cast<redisReply*>(
|
||||
redisCommand(c, "GET session:%s", token.c_str()));
|
||||
check_reply(c, r);
|
||||
|
||||
std::optional<std::string> 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<redisReply*>(
|
||||
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;
|
||||
}
|
||||
@@ -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<std::string> lookup(const std::string& token) override;
|
||||
bool revoke(const std::string& token) override;
|
||||
|
||||
private:
|
||||
RedisPool& pool_;
|
||||
int ttl_;
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
|
||||
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<std::string> lookup(const std::string& token) = 0;
|
||||
|
||||
virtual bool revoke(const std::string& token) = 0;
|
||||
};
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "user_repo.hpp"
|
||||
|
||||
#include <pqxx/pqxx>
|
||||
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string make_uuid() {
|
||||
static std::mt19937_64 rng{std::random_device{}()};
|
||||
std::uniform_int_distribution<uint32_t> d16(0, 0xffff);
|
||||
std::uniform_int_distribution<uint32_t> 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<std::time_t>(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<std::chrono::seconds>(
|
||||
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<std::string>();
|
||||
u.username = r["username"].as<std::string>();
|
||||
u.display_name = r["display_name"].as<std::string>();
|
||||
u.password_hash = r["password_hash"].as<std::string>();
|
||||
u.created_at = epoch_to_rfc3339(r["created_at_epoch"].as<long long>());
|
||||
return u;
|
||||
}
|
||||
|
||||
constexpr const char* kUserColumns =
|
||||
"id, username, display_name, password_hash, "
|
||||
"EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch";
|
||||
|
||||
}
|
||||
|
||||
std::optional<User> 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<User> 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<User> 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]);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "db.hpp"
|
||||
#include "models.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
class UserRepo {
|
||||
public:
|
||||
explicit UserRepo(PgPool& pool) : pool_(pool) {}
|
||||
|
||||
std::optional<User> create(const std::string& username,
|
||||
const std::string& display_name,
|
||||
const std::string& password_hash);
|
||||
|
||||
std::optional<User> find_by_id(const std::string& id);
|
||||
std::optional<User> find_by_username(const std::string& username);
|
||||
|
||||
private:
|
||||
PgPool& pool_;
|
||||
};
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user