ЛР 3 — БПИ2303 — Морару — на 5: API на TypeScript/Node, HAProxy, failover, трек 1

This commit is contained in:
celis
2026-10-29 20:59:24 +03:00
parent 0ab3a341aa
commit c4f9eb7648
83 changed files with 1862 additions and 19 deletions
+3
View File
@@ -18,3 +18,6 @@ __pycache__
node_modules
evidence
.DS_Store
research
student-tests
tests
+6
View File
@@ -11,3 +11,9 @@ SESSION_STORE=postgres
CACHE_URL_FILE=/run/secrets/cache_url
API_REPLICAS=2
IDEMPOTENCY_TTL_SECONDS=86400
# Implementation knobs (compose.yaml interpolation), see REPORT.md:
DB_POOL_MAX=8
API_CPUS=1
API_MEMORY=256m
DB_CPUS=2
SHUTDOWN_DRAIN_MS=2500
+1
View File
@@ -30,3 +30,4 @@ coverage/
.vscode/
# Keep small, sanitized evidence summaries in evidence/ under version control.
# Inspect git diff --cached: ignore rules are not a secret scanner.
node_modules/
+11
View File
@@ -0,0 +1,11 @@
# Node 24 runs .ts directly (type stripping), so there is no build step; `npm run typecheck` checks types.
FROM node:24.22.2-alpine3.24
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY src ./src
USER node
EXPOSE 8080
# node is PID 1 and handles SIGTERM itself (graceful drain in src/main.ts).
CMD ["node", "src/main.ts"]
+60 -19
View File
@@ -1,37 +1,78 @@
# Отчёт — лабораторная 3
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
- Автор, группа: Морару С. С., БПИ2303
- Целевая оценка: **5**, трек 1 «Цена масштабирования»
- Commit/PR: ветка `solution/bpi2303-moraru`, https://gitea.privcloud.tech/s.s.moraru/mtusi2026lab3
- Источники и ИИ: Node.js 24 (`node:http`, `node:crypto`), `pg` 8.23.1, HAProxy 3.2, k6 1.8.2. Код и отчёт подготовлены с помощью Claude Code (Anthropic), проверены запуском тестов.
## Запуск из чистого clone
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
Prerequisites: Docker Engine + Compose v2, Python 3.9+ (тесты), OpenSSL. Образы: `node:24.22.2-alpine3.24`, `postgres:18.6-alpine`, `haproxy:3.2.26-alpine`, `grafana/k6:1.8.2`.
```sh
scripts/init-secrets.sh # .env из .env.example + случайный пароль БД в secrets/ (вне Git)
docker compose --env-file .env up -d --build # postgres -> migrate (one-shot) -> api x2 -> lb
make test # LB на 127.0.0.1:${LB_PORT:-8080}
make test-persistence ACTION_SCRIPT=scripts/restart.sh
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
docker compose start api # восстановить остановленную реплику
python3 student-tests/idempotency_race.py --base-url http://localhost:8080
docker compose stop # безопасная остановка; удаление учебных данных: docker compose down -v
```
Типы: `npm ci && npm run typecheck` (Node 24 исполняет `.ts` напрямую через type stripping, сборки нет).
На стенде автора 8080 был занят, использовался `LB_PORT=8090` в `.env`.
## Архитектура и решения
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
Клиент → `lb` (HAProxy, единственный опубликованный порт) → `api` × N (`deploy.replicas`, без `container_name`, без опубликованных портов) → `postgres` (volume `pgdata`).
- Источник истины — PostgreSQL: пользователи, чаты, сообщения, **сессии** (хранится только sha256 токена) и ключи идемпотентности. В API нет локального состояния → любая реплика обслуживает любой запрос, sticky sessions нет.
- Миграции: отдельный one-shot сервис `migrate` (`depends_on: service_completed_successfully`), `pg_advisory_lock` защищает от гонки при ручном повторе; таблица `schema_migrations`.
- Идемпотентность: ключ (user + POST + путь чата + key) и сообщение вставляются **одним SQL-оператором** (CTE) → атомарно. Конкурентный дубль на другой реплике блокируется на PK ключа до COMMIT первого, затем отдаёт сохранённое сообщение (тот же 201) или 409 при другом теле. Ошибка БД ничего не фиксирует → повтор с тем же ключом безопасен.
- Пароли: scrypt (N=2^14, r=8, p=1) из `node:crypto`; токен 256 бит.
- `X-Instance-Id` = hostname контейнера (12 hex ID), `scripts/stop-one.sh` сопоставляет его с контейнером.
- Readiness: `/health/ready` = процесс не завершается + `SELECT 1` к PostgreSQL (единственная критичная зависимость).
- SIGTERM: readiness → 503, `Connection: close`, 2.5 с drain (больше времени обнаружения LB), затем `server.close()` дожидается текущих запросов.
- HAProxy: check `/health/ready` каждые 1 с (0.5 с при смене состояния), `fall 2 rise 2`, `timeout check 1s`; отказ соединения на живом трафике → DOWN сразу (`observe layer4`) + `retry-on conn-failure` с `redispatch` (повторяются только не дошедшие до backend запросы). Backends через Docker DNS (`server-template api 8 api:8080`), масштабирование без правки конфига. **Макс. время обнаружения**: graceful stop ≤ 1.5 с; падение процесса — первый же refused connect; зависание — ≤ 3.5 с.
- Таймауты: HAProxy connect 1 с / server 10 с; Node headers 5 с, request 15 с; pg acquire 2 с (→ 503), statement_timeout 5 с.
- Лимиты: api 1 CPU/256 MiB, postgres 2 CPU/1 GiB, lb 1 CPU/128 MiB. Сеть `backend` internal: API и БД недоступны с хоста. Секрет `postgres_password` только у postgres, `database_url` только у api/migrate.
- `.env.example`: `SESSION_TTL_SECONDS`, `API_REPLICAS`, `DATABASE_URL_FILE` используются как есть; `SESSION_STORE=postgres`; `CACHE_URL_FILE` не используется (Redis нет); `IDEMPOTENCY_TTL_SECONDS` — ключи хранятся бессрочно (≥ 24 ч выполнено, очистка — ограничение).
## Доказательства критериев
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
| Критерий | Команда | Результат |
| --- | --- | --- |
| Smoke | `make check`, `make test` | PASS lab3 public API scenario |
| Persistence | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; `StartedAt` всех контейнеров (lb, api-1, api-2, postgres) изменился, токен прежний |
| Failover | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | PASS дважды: 2 реплики до отказа, 131/132 успешных записей, **0 ошибок**, запись продолжилась через ~3 с (длительность drain); `docker compose ps -a` показывает остановленный контейнер, в логах LB `Server api/apiN is DOWN ... 503` |
| Каждая реплика по очереди под нагрузкой | `scripts/failover-each.sh` | k6 300 RPS, 20 с: api-1 и api-2 остановлены по очереди (stop ≈ 3 с), **0 ошибок** в обоих, 266/280 успешных RPS, p50 ≈ 4–6 мс; dropped iterations 407–678 из-за задержек p95 до 0.6–1.9 с в момент переключения. Вывод: `evidence/level4/failover-each.txt` |
| Конкурентная идемпотентность | `student-tests/idempotency_race.py --parallel 64` | 64 запроса с одним ключом через 2 реплики: 32×201 (одно и то же сообщение), 32×409 (другое тело), в истории ровно 1 сообщение |
| Rolling update | `scripts/rolling-update.sh` под k6 300 RPS, 40 с | обе реплики заменены (api-1/2 → api-3/4), **0 ошибок**, 293.5 успешных RPS, p50 4.7 мс, p99 533 мс. Миграции выполняются один раз до замены и только добавляют схему (expand-only), поэтому совместимы с работающей версией. Вывод: `evidence/level4/rolling-update.txt` |
| Лимиты / порты / секреты | `compose.yaml` | см. выше |
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
## Трек 1. Цена масштабирования
## Эксперименты и отказы
**Гипотеза.** Node.js исполняет JS в одном потоке, поэтому одна реплика не использует >1 CPU; при фиксированном суммарном CPU API (2 CPU / 512 MiB) 2–4 реплики дадут больше, чем 1×2 CPU. Дальше рост реплик упрётся в PostgreSQL (2 CPU).
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
**Методика** (`research/bench.sh`, `research/k6/messenger.js`, `research/report.py`): k6 open model (constant-arrival-rate, без coordinated omission), 50% POST сообщения с новым ключом / 50% GET истории (limit 20), 20 пользователей в 10 чатах, ступени 400–2400 RPS по 15 с, CPU контейнеров — `docker stats` на 8-й секунде. DB 2 CPU, LB 1 CPU, k6 3 CPU, pool 8 на реплику. Hardware: Apple M3 Pro, 11 ядер, 18 GiB, OrbStack. Raw: `evidence/track1/baseline/`, `evidence/track1/prepared/`, таблицы `*.md`, график `evidence/track1/throughput.svg`.
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
**Результаты (baseline, максимум успешных RPS):**
| Конфигурация | Макс. успешные RPS | CPU postgres на максимуме |
| --- | --- | --- |
| 1 × 2 CPU | ~870 (api ≈ 120% — один event loop) | ~165% |
| 2 × 1 CPU | ~1140 | ~200% |
| 4 × 0.5 CPU | ~1220 | ~217% (лимит) |
| 4 × 1 CPU (рост ресурсов) | ~1130 | ~240% (лимит) |
- 1 реплика не может съесть 2 CPU — гипотеза подтверждена (+30–40% при переходе на 2–4 реплики при тех же ресурсах).
- **Saturation point — PostgreSQL**: от 2 реплик CPU БД на лимите 2 CPU; удвоение CPU API (4×0.5 → 4×1) не увеличило пропускную способность. LB загружен ≤ 80% от 1 CPU и не узкое место.
**Подтверждённое улучшение: именованные prepared statements** на горячем пути (`auth`, `member`, `send`, `history`) — PostgreSQL не парсит/планирует запрос каждый раз. 2 × 1 CPU: максимум **~1140 → ~1390 RPS (+22%)**; при 1200 RPS p50 **121 → 4.4 мс**, p95 543 → 137 мс.
**Ограничения.** Один прогон на ступень (нет повторов и доверительных интервалов); на той же машине работали другие Docker-стенды пользователя — заметен шум (выброс 4×1 на 1200–1600 RPS, 4×0.5 после улучшения); k6 и стенд делят CPU; fsync на диск VM OrbStack. Вывод о DB-bottleneck устойчив (CPU БД на лимите во всех конфигурациях ≥ 2 реплик), абсолютные числа — нет.
## Ограничения и следующие шаги
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
Выдерживается отказ одного процесса API. Не выдерживаются: отказ LB, PostgreSQL, хоста (один Docker-host, БД без реплики). Ключи идемпотентности не чистятся. Порядок истории по `clock_timestamp()` вставки: сообщение с меньшим временем может закоммититься позже уже прочитанной страницы. Дальше: повторы прогонов на изолированной машине, вынос чтений на реплику БД.
+86
View File
@@ -0,0 +1,86 @@
# Client -> lb (HAProxy) -> api x N -> postgres (users, chats, messages, sessions, idempotency keys).
# Start: scripts/init-secrets.sh && docker compose --env-file .env up -d --build (see REPORT.md)
name: messenger-lab3
x-api: &api
build: .
image: messenger-api:lab3
environment:
DATABASE_URL_FILE: /run/secrets/database_url
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
DB_POOL_MAX: ${DB_POOL_MAX:-8}
SHUTDOWN_DRAIN_MS: ${SHUTDOWN_DRAIN_MS:-2500}
secrets: [database_url]
networks: [backend]
services:
postgres:
image: postgres:18.6-alpine
command: ["postgres", "-c", "max_connections=200", "-c", "shared_buffers=256MB"]
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets: [postgres_password]
volumes: [pgdata:/var/lib/postgresql]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 2s
timeout: 2s
retries: 15
deploy:
resources:
limits: { cpus: "${DB_CPUS:-2}", memory: 1g }
stop_grace_period: 30s
restart: unless-stopped
networks: [backend]
# One-shot: schema migrations run once before any replica starts (advisory lock guards manual reruns).
migrate:
<<: *api
command: ["node", "src/main.ts", "migrate"]
restart: "no"
depends_on:
postgres: { condition: service_healthy }
api:
<<: *api
depends_on:
migrate: { condition: service_completed_successfully }
healthcheck:
test: ["CMD", "wget", "-qO", "/dev/null", "http://127.0.0.1:8080/health/ready"]
interval: 2s
timeout: 2s
retries: 3
start_period: 10s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits: { cpus: "${API_CPUS:-1}", memory: "${API_MEMORY:-256m}" }
stop_grace_period: 15s
restart: unless-stopped # a crashed replica comes back; `docker stop` keeps it stopped
lb:
image: haproxy:3.2.26-alpine
volumes: ["./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro"]
ports:
- "127.0.0.1:${LB_PORT:-8080}:8080"
- "127.0.0.1:${LB_STATS_PORT:-8404}:8404" # HAProxy stats/metrics for the operator, not the API
depends_on:
api: { condition: service_healthy }
deploy:
resources:
limits: { cpus: "${LB_CPUS:-1}", memory: 128m }
restart: unless-stopped
networks: [frontend, backend]
networks:
frontend: {}
backend: { internal: true } # api and postgres are unreachable from the host and have no egress
volumes:
pgdata: {}
secrets:
database_url: { file: ./secrets/database_url }
postgres_password: { file: ./secrets/postgres_password }
+4
View File
@@ -0,0 +1,4 @@
stopped /messenger-lab3-api-1 in 3 s: {"offered_rps":300,"write_share":0.5,"duration_s":20,"requests":5321,"failed":0,"error_ratio":0,"successful_rps":266.1,"dropped_iterations":678,"latency_ms":{"avg":285.8580549864688,"p50":5.886506,"p95":1887.179839,"p99":2480.7312331999997,"max":2824.470612},"write_latency_ms":{"avg":301.3330675666296,"p50":7.970291,"p95":1916.7441439999998,"p99":2534.75885108,"max":2824.470612},"read_latency_ms":{"avg":269.80835778905055,"p50":3.976541,"p95":1768.326860899999,"p99":2424.8058585,"max":2700.834139},"max_vus":405}
restored /messenger-lab3-api-1
stopped /messenger-lab3-api-2 in 3 s: {"offered_rps":300,"write_share":0.5,"duration_s":20,"requests":5603,"failed":0,"error_ratio":0,"successful_rps":280.1,"dropped_iterations":407,"latency_ms":{"avg":76.73040731965024,"p50":3.778679,"p95":594.9778335999997,"p99":996.2148922199999,"max":1531.615854},"write_latency_ms":{"avg":81.54697441930875,"p50":4.562875,"p95":627.9609838999987,"p99":1017.97884274,"max":1531.615854},"read_latency_ms":{"avg":71.89489092167379,"p50":2.43511,"p95":567.8789274999997,"p99":971.2050595999999,"max":1384.910329},"max_vus":164}
restored /messenger-lab3-api-2
+5
View File
@@ -0,0 +1,5 @@
rolling-update.sh:
messenger-lab3-api-3 dbad184102d7 Up 30 seconds (healthy)
messenger-lab3-api-4 29c3788db998 Up 19 seconds (healthy)
k6 during update:
{"offered_rps":300,"write_share":0.5,"duration_s":40,"requests":11740,"failed":0,"error_ratio":0,"successful_rps":293.5,"dropped_iterations":261,"latency_ms":{"avg":35.639212581005154,"p50":4.673601,"p95":191.03081429999946,"p99":533.1527323999996,"max":1231.547582},"write_latency_ms":{"avg":39.883953559633134,"p50":5.463844,"p95":217.158911,"p99":569.9043230499994,"max":1189.504971},"read_latency_ms":{"avg":31.37126837188255,"p50":3.7131305,"p95":164.84850394999953,"p99":462.7135553499999,"max":1231.547582},"max_vus":140}
+22
View File
@@ -0,0 +1,22 @@
| Конфигурация API | Offered RPS | Успешные RPS | Ошибки | Dropped | p50 мс | p95 мс | p99 мс | CPU api % | CPU postgres % | CPU lb % |
|---|---|---|---|---|---|---|---|---|---|---|
| 1×2 CPU, pool 8 | 400 | 396.5 | 0 | 53 | 6.3 | 226.6 | 363.0 | 82 | 74 | 12 |
| 1×2 CPU, pool 8 | 800 | 695.9 | 0 | 1561 | 488.7 | 1240.5 | 1516.7 | 115 | 167 | 31 |
| 1×2 CPU, pool 8 | 1200 | 871.3 | 0 | 4932 | 1486.0 | 2542.1 | 2577.8 | 121 | 165 | 36 |
| 1×2 CPU, pool 8 | 1600 | 866.5 | 0 | 11003 | 2143.4 | 2637.7 | 2671.0 | 123 | 147 | 41 |
| 1×2 CPU, pool 8 | 2400 | 869.8 | 0 | 22954 | 2320.2 | 2775.9 | 2925.4 | 120 | 133 | 41 |
| 2×1 CPU, pool 8 | 400 | 395.9 | 0 | 63 | 8.4 | 182.8 | 332.6 | 151 | 118 | 18 |
| 2×1 CPU, pool 8 | 800 | 785 | 0 | 228 | 13.1 | 325.2 | 429.8 | 119 | 142 | 20 |
| 2×1 CPU, pool 8 | 1200 | 1138.4 | 0 | 924 | 121.6 | 542.7 | 623.5 | 108 | 196 | 22 |
| 2×1 CPU, pool 8 | 1600 | 927.3 | 346 | 9622 | 1029.3 | 4371.6 | 4831.8 | 172 | 205 | 54 |
| 2×1 CPU, pool 8 | 2400 | 813.9 | 5003 | 18790 | 1464.3 | 3354.6 | 4409.3 | 225 | 225 | 83 |
| 4×0.5 CPU, pool 8 | 400 | 400 | 0 | 0 | 3.9 | 17.1 | 92.5 | 74 | 59 | 9 |
| 4×0.5 CPU, pool 8 | 800 | 797.8 | 0 | 35 | 8.2 | 113.9 | 258.4 | 204 | 212 | 62 |
| 4×0.5 CPU, pool 8 | 1200 | 1143.9 | 0 | 842 | 22.8 | 628.1 | 693.1 | 145 | 191 | 23 |
| 4×0.5 CPU, pool 8 | 1600 | 1219.1 | 0 | 5723 | 525.1 | 1929.8 | 2428.3 | 164 | 217 | 37 |
| 4×0.5 CPU, pool 8 | 2400 | 1192.5 | 0 | 18126 | 1479.5 | 2613.4 | 3394.7 | 199 | 217 | 48 |
| 4×1 CPU, pool 8 | 400 | 395.7 | 0 | 65 | 4.7 | 155.4 | 377.0 | 86 | 73 | 10 |
| 4×1 CPU, pool 8 | 800 | 745.7 | 0 | 817 | 6.9 | 537.2 | 920.8 | 134 | 138 | 18 |
| 4×1 CPU, pool 8 | 1200 | 67.2 | 2099 | 15083 | 3651.7 | 5933.6 | 7313.1 | 77 | 52 | 2 |
| 4×1 CPU, pool 8 | 1600 | 500.4 | 1892 | 14517 | 2231.7 | 6222.7 | 7129.5 | 58 | 33 | 5 |
| 4×1 CPU, pool 8 | 2400 | 1128.6 | 0 | 19076 | 1699.5 | 2868.3 | 2985.5 | 253 | 240 | 72 |
@@ -0,0 +1,3 @@
messenger-lab3-api-1 120.87%
messenger-lab3-lb-1 36.39%
messenger-lab3-postgres-1 164.61%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":13069,"failed":0,"error_ratio":0,"successful_rps":871.3,"dropped_iterations":4932,"latency_ms":{"avg":1377.7500002243426,"p50":1485.999735,"p95":2542.1442652,"p99":2577.7934696800003,"max":2612.851945},"write_latency_ms":{"avg":1375.8530888596276,"p50":1489.948772,"p95":2536.98453465,"p99":2575.80878484,"max":2612.851945},"read_latency_ms":{"avg":1379.6292860607723,"p50":1483.663271,"p95":2544.5906878,"p99":2578.4755604,"max":2607.574566},"max_vus":1894}
@@ -0,0 +1,3 @@
messenger-lab3-api-1 122.60%
messenger-lab3-lb-1 41.40%
messenger-lab3-postgres-1 147.04%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":12998,"failed":0,"error_ratio":0,"successful_rps":866.5,"dropped_iterations":11003,"latency_ms":{"avg":1952.7481274139907,"p50":2143.3979435,"p95":2637.65673135,"p99":2671.0399932600003,"max":2698.663353},"write_latency_ms":{"avg":1956.361136338029,"p50":2149.440146,"p95":2642.0307936,"p99":2674.0917056,"max":2698.663353},"read_latency_ms":{"avg":1949.1016078182058,"p50":2140.713775,"p95":2633.4779611999998,"p99":2664.8991942800003,"max":2690.353308},"max_vus":2000}
@@ -0,0 +1,3 @@
messenger-lab3-api-1 120.46%
messenger-lab3-lb-1 41.12%
messenger-lab3-postgres-1 133.29%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":13047,"failed":0,"error_ratio":0,"successful_rps":869.8,"dropped_iterations":22954,"latency_ms":{"avg":2234.377719246645,"p50":2320.227715,"p95":2775.9377184,"p99":2925.3860481399997,"max":2954.934704},"write_latency_ms":{"avg":2238.8293657670206,"p50":2323.0659445,"p95":2777.29427745,"p99":2926.6174346400003,"max":2954.934704},"read_latency_ms":{"avg":2229.8897579247373,"p50":2318.015956,"p95":2774.1182498,"p99":2923.36437288,"max":2953.242062},"max_vus":2000}
@@ -0,0 +1,3 @@
messenger-lab3-api-1 82.23%
messenger-lab3-lb-1 12.42%
messenger-lab3-postgres-1 73.51%
@@ -0,0 +1 @@
{"offered_rps":400,"write_share":0.5,"duration_s":15,"requests":5948,"failed":0,"error_ratio":0,"successful_rps":396.5,"dropped_iterations":53,"latency_ms":{"avg":37.30886019435115,"p50":6.2835745,"p95":226.61944324999996,"p99":363.01610857,"max":443.196583},"write_latency_ms":{"avg":39.32523198772798,"p50":7.852552,"p95":229.50429669999968,"p99":363.7971908399999,"max":443.196583},"read_latency_ms":{"avg":35.23611523798162,"p50":4.712598,"p95":221.98558579999994,"p99":360.2773956,"max":441.000032},"max_vus":122}
@@ -0,0 +1,3 @@
messenger-lab3-api-1 114.76%
messenger-lab3-lb-1 30.58%
messenger-lab3-postgres-1 166.62%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":10439,"failed":0,"error_ratio":0,"successful_rps":695.9,"dropped_iterations":1561,"latency_ms":{"avg":504.85043385688255,"p50":488.693167,"p95":1240.5209084999997,"p99":1516.7086683399998,"max":1614.135596},"write_latency_ms":{"avg":509.62484525402834,"p50":500.254162,"p95":1245.61459115,"p99":1515.5776794499998,"max":1614.135596},"read_latency_ms":{"avg":500.0897236594604,"p50":475.014662,"p95":1235.3024072,"p99":1518.0933346199997,"max":1611.651424},"max_vus":706}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 54.84%
messenger-lab3-api-2 53.29%
messenger-lab3-lb-1 22.16%
messenger-lab3-postgres-1 195.98%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":17076,"failed":0,"error_ratio":0,"successful_rps":1138.4,"dropped_iterations":924,"latency_ms":{"avg":184.2536581855231,"p50":121.619543,"p95":542.6821342499998,"p99":623.4931734999996,"max":823.491375},"write_latency_ms":{"avg":185.86037619524322,"p50":124.615581,"p95":544.25632,"p99":630.495987,"max":823.491375},"read_latency_ms":{"avg":182.67935396521722,"p50":118.660919,"p95":540.5566415999999,"p99":617.0685706799998,"max":782.02136},"max_vus":501}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 83.80%
messenger-lab3-api-2 88.65%
messenger-lab3-lb-1 53.84%
messenger-lab3-postgres-1 205.07%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":14255,"failed":346,"error_ratio":0.024272,"successful_rps":927.3,"dropped_iterations":9622,"latency_ms":{"avg":1423.6164610485453,"p50":1029.329677,"p95":4371.5977618,"p99":4831.7629368200005,"max":4909.452839},"write_latency_ms":{"avg":1422.105342264009,"p50":1030.4731815,"p95":4375.034157099999,"p99":4849.20119662,"max":4909.452839},"read_latency_ms":{"avg":1425.1328894563628,"p50":1026.789964,"p95":4369.677181799999,"p99":4816.34788194,"max":4876.835819},"max_vus":2000}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 120.01%
messenger-lab3-api-2 104.70%
messenger-lab3-lb-1 83.28%
messenger-lab3-postgres-1 224.53%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":17211,"failed":5003,"error_ratio":0.290686,"successful_rps":813.9,"dropped_iterations":18790,"latency_ms":{"avg":1346.0662245085723,"p50":1464.311398,"p95":3354.5784405,"p99":4409.2749650000005,"max":4651.754674},"write_latency_ms":{"avg":1346.641000310546,"p50":1464.063506,"p95":3321.7923219,"p99":4401.064470740001,"max":4651.754674},"read_latency_ms":{"avg":1345.493781671108,"p50":1464.371189,"p95":3390.2804482,"p99":4423.77663752,"max":4651.456012},"max_vus":2000}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 77.33%
messenger-lab3-api-2 73.85%
messenger-lab3-lb-1 18.05%
messenger-lab3-postgres-1 118.08%
@@ -0,0 +1 @@
{"offered_rps":400,"write_share":0.5,"duration_s":15,"requests":5938,"failed":0,"error_ratio":0,"successful_rps":395.9,"dropped_iterations":63,"latency_ms":{"avg":31.627892203604016,"p50":8.429459999999999,"p95":182.8400829,"p99":332.61143422999993,"max":463.600216},"write_latency_ms":{"avg":36.50790030020285,"p50":11.2187725,"p95":191.8529304,"p99":344.4485094799999,"max":463.600216},"read_latency_ms":{"avg":26.78391101241602,"p50":5.7194780000000005,"p95":168.9671546999999,"p99":321.85990316,"max":443.320723},"max_vus":112}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 60.06%
messenger-lab3-api-2 59.21%
messenger-lab3-lb-1 20.13%
messenger-lab3-postgres-1 142.00%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":11775,"failed":0,"error_ratio":0,"successful_rps":785,"dropped_iterations":228,"latency_ms":{"avg":57.9596839032698,"p50":13.07989,"p95":325.21779899999984,"p99":429.84890614,"max":509.217544},"write_latency_ms":{"avg":62.90194856092592,"p50":17.258579,"p95":341.7304343999999,"p99":441.45039312,"max":509.217544},"read_latency_ms":{"avg":52.96764225435298,"p50":9.017846500000001,"p95":313.9240739,"p99":414.1343786899999,"max":484.466617},"max_vus":261}
@@ -0,0 +1,6 @@
messenger-lab3-api-1 38.60%
messenger-lab3-api-2 36.98%
messenger-lab3-api-4 34.84%
messenger-lab3-api-3 34.56%
messenger-lab3-lb-1 22.83%
messenger-lab3-postgres-1 191.06%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":17159,"failed":0,"error_ratio":0,"successful_rps":1143.9,"dropped_iterations":842,"latency_ms":{"avg":153.60984741820616,"p50":22.798665,"p95":628.0566369,"p99":693.09573986,"max":792.158463},"write_latency_ms":{"avg":161.29379987323688,"p50":29.636897,"p95":636.9124772,"p99":699.38446408,"max":775.45075},"read_latency_ms":{"avg":145.93395129729709,"p50":16.7221915,"p95":618.6731082,"p99":686.4067532899999,"max":792.158463},"max_vus":567}
@@ -0,0 +1,6 @@
messenger-lab3-api-1 40.58%
messenger-lab3-api-2 40.88%
messenger-lab3-api-4 40.97%
messenger-lab3-api-3 41.75%
messenger-lab3-lb-1 37.37%
messenger-lab3-postgres-1 216.85%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":18286,"failed":0,"error_ratio":0,"successful_rps":1219.1,"dropped_iterations":5723,"latency_ms":{"avg":744.6361151525203,"p50":525.125727,"p95":1929.8387959999998,"p99":2428.2790683,"max":2549.334759},"write_latency_ms":{"avg":754.1291711846636,"p50":535.979109,"p95":1937.72611545,"p99":2434.47376465,"max":2549.334759},"read_latency_ms":{"avg":735.3586237577854,"p50":516.0065285000001,"p95":1912.6174244999997,"p99":2418.40500141,"max":2533.009541},"max_vus":1711}
@@ -0,0 +1,6 @@
messenger-lab3-api-1 48.81%
messenger-lab3-api-2 49.85%
messenger-lab3-api-4 49.74%
messenger-lab3-api-3 50.84%
messenger-lab3-lb-1 47.89%
messenger-lab3-postgres-1 216.85%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":17888,"failed":0,"error_ratio":0,"successful_rps":1192.5,"dropped_iterations":18126,"latency_ms":{"avg":1444.9100745602047,"p50":1479.4854885,"p95":2613.3593831499998,"p99":3394.6668168300002,"max":3546.522578},"write_latency_ms":{"avg":1450.9141503404742,"p50":1486.042705,"p95":2625.6198941999996,"p99":3401.90025736,"max":3546.522578},"read_latency_ms":{"avg":1438.9421396886635,"p50":1474.221296,"p95":2606.9951395,"p99":3391.2363225999998,"max":3498.470702},"max_vus":2000}
@@ -0,0 +1,6 @@
messenger-lab3-api-1 16.68%
messenger-lab3-api-2 24.28%
messenger-lab3-api-4 17.33%
messenger-lab3-api-3 15.63%
messenger-lab3-lb-1 9.14%
messenger-lab3-postgres-1 59.12%
@@ -0,0 +1 @@
{"offered_rps":400,"write_share":0.5,"duration_s":15,"requests":6000,"failed":0,"error_ratio":0,"successful_rps":400,"dropped_iterations":0,"latency_ms":{"avg":7.420179008500009,"p50":3.9336095,"p95":17.081804899999987,"p99":92.46334012999995,"max":360.704023},"write_latency_ms":{"avg":9.50113648481848,"p50":4.899553,"p95":22.063917149999995,"p99":121.44334558999981,"max":313.108723},"read_latency_ms":{"avg":5.297181987205394,"p50":2.7496885,"p95":11.306022999999989,"p99":60.82138993999997,"max":360.704023},"max_vus":100}
@@ -0,0 +1,6 @@
messenger-lab3-api-1 50.57%
messenger-lab3-api-2 51.71%
messenger-lab3-api-4 49.69%
messenger-lab3-api-3 51.84%
messenger-lab3-lb-1 61.66%
messenger-lab3-postgres-1 211.61%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":11967,"failed":0,"error_ratio":0,"successful_rps":797.8,"dropped_iterations":35,"latency_ms":{"avg":24.131353696582163,"p50":8.234712,"p95":113.87341099999995,"p99":258.42533341999996,"max":364.434301},"write_latency_ms":{"avg":28.535250372752785,"p50":10.995005,"p95":123.95958339999999,"p99":282.87352169999997,"max":364.434301},"read_latency_ms":{"avg":19.6088561444784,"p50":5.880955,"p95":102.82208639999993,"p99":225.59189529,"max":327.944839},"max_vus":212}
@@ -0,0 +1,6 @@
messenger-lab3-api-4 12.05%
messenger-lab3-api-3 2.19%
messenger-lab3-api-1 15.04%
messenger-lab3-api-2 47.38%
messenger-lab3-lb-1 1.61%
messenger-lab3-postgres-1 52.06%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":3107,"failed":2099,"error_ratio":0.675571,"successful_rps":67.2,"dropped_iterations":15083,"latency_ms":{"avg":3290.0812545532694,"p50":3651.666168,"p95":5933.551933999999,"p99":7313.10876462,"max":7736.283848},"write_latency_ms":{"avg":3308.1679607397637,"p50":3651.666168,"p95":5948.200961,"p99":7307.31234666,"max":7736.283848},"read_latency_ms":{"avg":3271.197305396704,"p50":3651.6673135,"p95":5871.715363049999,"p99":7318.383750839999,"max":7715.776877},"max_vus":900}
@@ -0,0 +1,6 @@
messenger-lab3-api-4 5.23%
messenger-lab3-api-3 12.18%
messenger-lab3-api-1 13.07%
messenger-lab3-api-2 27.95%
messenger-lab3-lb-1 5.14%
messenger-lab3-postgres-1 33.14%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":9398,"failed":1892,"error_ratio":0.201319,"successful_rps":500.4,"dropped_iterations":14517,"latency_ms":{"avg":2633.6900294437078,"p50":2231.738601,"p95":6222.724926949998,"p99":7129.46403511,"max":8004.776274},"write_latency_ms":{"avg":2666.4717723044837,"p50":2265.146152,"p95":6173.1245407999995,"p99":7096.90545708,"max":8004.776274},"read_latency_ms":{"avg":2600.203151223488,"p50":2203.614473,"p95":6294.8318078,"p99":7206.22669572,"max":7630.69649},"max_vus":2000}
@@ -0,0 +1,6 @@
messenger-lab3-api-4 71.16%
messenger-lab3-api-3 63.88%
messenger-lab3-api-1 53.50%
messenger-lab3-api-2 64.43%
messenger-lab3-lb-1 72.29%
messenger-lab3-postgres-1 240.35%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":16929,"failed":0,"error_ratio":0,"successful_rps":1128.6,"dropped_iterations":19076,"latency_ms":{"avg":1619.458252642272,"p50":1699.486104,"p95":2868.3080628,"p99":2985.51171728,"max":3066.451479},"write_latency_ms":{"avg":1639.3613475642373,"p50":1709.17144,"p95":2884.62945375,"p99":2991.60039657,"max":3066.451479},"read_latency_ms":{"avg":1598.9463908731018,"p50":1690.429029,"p95":2840.9941818,"p99":2978.4581778399997,"max":3057.87173},"max_vus":2000}
@@ -0,0 +1,6 @@
messenger-lab3-api-4 27.06%
messenger-lab3-api-3 18.52%
messenger-lab3-api-1 22.07%
messenger-lab3-api-2 18.84%
messenger-lab3-lb-1 10.45%
messenger-lab3-postgres-1 73.37%
@@ -0,0 +1 @@
{"offered_rps":400,"write_share":0.5,"duration_s":15,"requests":5936,"failed":0,"error_ratio":0,"successful_rps":395.7,"dropped_iterations":65,"latency_ms":{"avg":26.209319683793797,"p50":4.7016805,"p95":155.4244929999999,"p99":377.0095952,"max":657.381321},"write_latency_ms":{"avg":31.579317840482517,"p50":5.743040000000001,"p95":189.40907999999982,"p99":398.3498036099999,"max":657.381321},"read_latency_ms":{"avg":20.781110164972887,"p50":3.3078265,"p95":121.39735594999979,"p99":359.59089234999993,"max":564.214151},"max_vus":130}
@@ -0,0 +1,6 @@
messenger-lab3-api-4 33.12%
messenger-lab3-api-3 36.13%
messenger-lab3-api-1 31.70%
messenger-lab3-api-2 32.95%
messenger-lab3-lb-1 18.50%
messenger-lab3-postgres-1 137.61%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":11185,"failed":0,"error_ratio":0,"successful_rps":745.7,"dropped_iterations":817,"latency_ms":{"avg":90.66115125176574,"p50":6.921315,"p95":537.1561139999998,"p99":920.78826352,"max":1164.049243},"write_latency_ms":{"avg":98.25650529074275,"p50":8.260836999999999,"p95":565.4327361999999,"p99":928.79544672,"max":1164.049243},"read_latency_ms":{"avg":82.81873772287844,"p50":5.242964,"p95":501.57621429999995,"p99":902.47582684,"max":1150.578527},"max_vus":338}
+10
View File
@@ -0,0 +1,10 @@
| Конфигурация API | Offered RPS | Успешные RPS | Ошибки | Dropped | p50 мс | p95 мс | p99 мс | CPU api % | CPU postgres % | CPU lb % |
|---|---|---|---|---|---|---|---|---|---|---|
| 2×1 CPU, pool 8 | 800 | 768.5 | 0 | 472 | 4.4 | 374.3 | 838.2 | 124 | 141 | 39 |
| 2×1 CPU, pool 8 | 1200 | 1188.9 | 0 | 167 | 4.4 | 136.7 | 292.5 | 86 | 104 | 18 |
| 2×1 CPU, pool 8 | 1600 | 1388.3 | 0 | 3170 | 324.2 | 1000.2 | 1477.6 | 201 | 178 | 78 |
| 2×1 CPU, pool 8 | 2400 | 1145.8 | 1955 | 16860 | 1027.2 | 3487.9 | 3976.7 | 206 | 164 | 53 |
| 4×0.5 CPU, pool 8 | 800 | 665.3 | 0 | 2019 | 7.3 | 1396.3 | 2159.9 | 113 | 69 | 14 |
| 4×0.5 CPU, pool 8 | 1200 | 780.8 | 809 | 5483 | 286.0 | 4721.9 | 7044.8 | 231 | 169 | 45 |
| 4×0.5 CPU, pool 8 | 1600 | 1356.3 | 0 | 3660 | 193.8 | 1035.6 | 1843.0 | 79 | 18 | 5 |
| 4×0.5 CPU, pool 8 | 2400 | 1156.9 | 0 | 18645 | 1066.7 | 3587.6 | 4230.0 | 221 | 182 | 55 |
@@ -0,0 +1,4 @@
messenger-lab3-api-1 43.43%
messenger-lab3-api-2 43.03%
messenger-lab3-lb-1 18.46%
messenger-lab3-postgres-1 104.09%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":17834,"failed":0,"error_ratio":0,"successful_rps":1188.9,"dropped_iterations":167,"latency_ms":{"avg":20.921610246383203,"p50":4.412207,"p95":136.69577139999993,"p99":292.46994078999995,"max":544.214272},"write_latency_ms":{"avg":22.72791040006645,"p50":5.214812,"p95":144.35042399999998,"p99":301.59520879999997,"max":544.214272},"read_latency_ms":{"avg":19.068526446779618,"p50":3.545224,"p95":130.33164199999996,"p99":271.73105446,"max":538.0541},"max_vus":345}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 105.83%
messenger-lab3-api-2 95.30%
messenger-lab3-lb-1 78.33%
messenger-lab3-postgres-1 177.67%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":20824,"failed":0,"error_ratio":0,"successful_rps":1388.3,"dropped_iterations":3170,"latency_ms":{"avg":335.36963824942376,"p50":324.176089,"p95":1000.2192829999998,"p99":1477.5644545,"max":1577.135807},"write_latency_ms":{"avg":337.9801489117481,"p50":326.7511195,"p95":1007.3138522499997,"p99":1477.1426540999998,"max":1574.528277},"read_latency_ms":{"avg":332.7470651581636,"p50":321.6143715,"p95":986.0951228999985,"p99":1477.63397172,"max":1577.135807},"max_vus":1041}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 103.51%
messenger-lab3-api-2 102.28%
messenger-lab3-lb-1 53.48%
messenger-lab3-postgres-1 164.37%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":19142,"failed":1955,"error_ratio":0.102131,"successful_rps":1145.8,"dropped_iterations":16860,"latency_ms":{"avg":1332.421539354669,"p50":1027.1938555,"p95":3487.9020753,"p99":3976.6542119399996,"max":4324.674698},"write_latency_ms":{"avg":1343.3896662825866,"p50":1028.012816,"p95":3492.4464788,"p99":3974.24478452,"max":4324.674698},"read_latency_ms":{"avg":1321.3196688417981,"p50":1026.006801,"p95":3483.9109485999998,"p99":3984.7329752399996,"max":4314.995552},"max_vus":2000}
@@ -0,0 +1,4 @@
messenger-lab3-api-1 64.50%
messenger-lab3-api-2 59.72%
messenger-lab3-lb-1 39.37%
messenger-lab3-postgres-1 141.05%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":11528,"failed":0,"error_ratio":0,"successful_rps":768.5,"dropped_iterations":472,"latency_ms":{"avg":56.78879948794237,"p50":4.353184499999999,"p95":374.2802499999996,"p99":838.24780902,"max":1004.819627},"write_latency_ms":{"avg":58.65329885660307,"p50":5.29644,"p95":375.0482877999996,"p99":835.28789092,"max":1004.819627},"read_latency_ms":{"avg":54.88573553987723,"p50":3.33551,"p95":373.6549276,"p99":842.29507112,"max":966.977821},"max_vus":303}
@@ -0,0 +1,6 @@
messenger-lab3-api-2 58.50%
messenger-lab3-api-3 55.17%
messenger-lab3-api-1 60.07%
messenger-lab3-api-4 57.13%
messenger-lab3-lb-1 44.73%
messenger-lab3-postgres-1 168.82%
@@ -0,0 +1 @@
{"offered_rps":1200,"write_share":0.5,"duration_s":15,"requests":12521,"failed":809,"error_ratio":0.064611,"successful_rps":780.8,"dropped_iterations":5483,"latency_ms":{"avg":741.4503653119542,"p50":285.958837,"p95":4721.851902,"p99":7044.8102502,"max":7740.427043},"write_latency_ms":{"avg":743.6836619626586,"p50":297.588503,"p95":4666.9570128,"p99":7084.30263824,"max":7730.000483},"read_latency_ms":{"avg":739.250702646956,"p50":273.6498335,"p95":4778.857987349997,"p99":7010.0369199199995,"max":7740.427043},"max_vus":909}
@@ -0,0 +1,6 @@
messenger-lab3-api-2 9.09%
messenger-lab3-api-3 32.14%
messenger-lab3-api-1 17.14%
messenger-lab3-api-4 20.47%
messenger-lab3-lb-1 4.76%
messenger-lab3-postgres-1 17.53%
@@ -0,0 +1 @@
{"offered_rps":1600,"write_share":0.5,"duration_s":15,"requests":20344,"failed":0,"error_ratio":0,"successful_rps":1356.3,"dropped_iterations":3660,"latency_ms":{"avg":291.0591323767692,"p50":193.7862415,"p95":1035.5979208999993,"p99":1842.9859081599993,"max":2148.889561},"write_latency_ms":{"avg":298.78921722150903,"p50":203.9106295,"p95":1058.3603921499996,"p99":1868.56213664,"max":2148.889561},"read_latency_ms":{"avg":283.37148721352975,"p50":182.41178150000002,"p95":1021.8262150999999,"p99":1797.4362670099997,"max":2096.906865},"max_vus":908}
@@ -0,0 +1,6 @@
messenger-lab3-api-2 55.31%
messenger-lab3-api-3 57.99%
messenger-lab3-api-1 53.50%
messenger-lab3-api-4 54.21%
messenger-lab3-lb-1 54.83%
messenger-lab3-postgres-1 181.88%
@@ -0,0 +1 @@
{"offered_rps":2400,"write_share":0.5,"duration_s":15,"requests":17353,"failed":0,"error_ratio":0,"successful_rps":1156.9,"dropped_iterations":18645,"latency_ms":{"avg":1436.4086833729054,"p50":1066.74967,"p95":3587.636085599999,"p99":4230.04881144,"max":4404.794374},"write_latency_ms":{"avg":1440.5476515174832,"p50":1078.225876,"p95":3532.7582081,"p99":4230.5450839,"max":4404.794374},"read_latency_ms":{"avg":1432.2787689233264,"p50":1051.1480040000001,"p95":3625.999405749997,"p99":4229.9558742,"max":4355.997285},"max_vus":2000}
@@ -0,0 +1,6 @@
messenger-lab3-api-2 27.75%
messenger-lab3-api-3 28.01%
messenger-lab3-api-1 28.52%
messenger-lab3-api-4 28.57%
messenger-lab3-lb-1 14.20%
messenger-lab3-postgres-1 69.16%
@@ -0,0 +1 @@
{"offered_rps":800,"write_share":0.5,"duration_s":15,"requests":9979,"failed":0,"error_ratio":0,"successful_rps":665.3,"dropped_iterations":2019,"latency_ms":{"avg":292.61199650355775,"p50":7.334198,"p95":1396.2831122,"p99":2159.91119692,"max":2552.326753},"write_latency_ms":{"avg":300.24812809827523,"p50":8.542014,"p95":1432.9697039999999,"p99":2199.1567824999997,"max":2552.326753},"read_latency_ms":{"avg":284.9865704808731,"p50":5.694481,"p95":1334.2283064,"p99":2129.5101201999996,"max":2467.373359},"max_vus":559}
+22
View File
@@ -0,0 +1,22 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 380" font-family="sans-serif" font-size="12">
<rect width="100%" height="100%" fill="white"/>
<line x1="50" y1="330" x2="590" y2="330" stroke="black"/><line x1="50" y1="50" x2="50" y2="330" stroke="black"/>
<line x1="50.0" y1="330.0" x2="590.0" y2="50.0" stroke="#aaa" stroke-dasharray="4"/>
<text x="320.0" y="368" text-anchor="middle">offered RPS</text>
<text x="14" y="190.0" transform="rotate(-90 14 190.0)" text-anchor="middle">successful RPS</text>
<text x="50.0" y="346" text-anchor="middle">0</text><text x="44" y="334.0" text-anchor="end">0</text>
<text x="140.0" y="346" text-anchor="middle">400</text><text x="44" y="287.3333333333333" text-anchor="end">400</text>
<text x="230.0" y="346" text-anchor="middle">800</text><text x="44" y="240.66666666666669" text-anchor="end">800</text>
<text x="320.0" y="346" text-anchor="middle">1200</text><text x="44" y="194.0" text-anchor="end">1200</text>
<text x="410.0" y="346" text-anchor="middle">1600</text><text x="44" y="147.33333333333334" text-anchor="end">1600</text>
<text x="500.0" y="346" text-anchor="middle">2000</text><text x="44" y="100.66666666666666" text-anchor="end">2000</text>
<text x="590.0" y="346" text-anchor="middle">2400</text><text x="44" y="54.0" text-anchor="end">2400</text>
<polyline fill="none" stroke="#2a6fdb" stroke-width="2" points="140,284 230,249 320,228 410,229 590,229"/>
<text x="60" y="50" fill="#2a6fdb">1×2 CPU, pool 8</text>
<polyline fill="none" stroke="#d9480f" stroke-width="2" points="140,284 230,238 320,197 410,222 590,235"/>
<text x="60" y="66" fill="#d9480f">2×1 CPU, pool 8</text>
<polyline fill="none" stroke="#2b8a3e" stroke-width="2" points="140,283 230,237 320,197 410,188 590,191"/>
<text x="60" y="82" fill="#2b8a3e">4×0.5 CPU, pool 8</text>
<polyline fill="none" stroke="#862e9c" stroke-width="2" points="140,284 230,243 320,322 410,272 590,198"/>
<text x="60" y="98" fill="#862e9c">4×1 CPU, pool 8</text>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

+56
View File
@@ -0,0 +1,56 @@
# Single public entrypoint. Backends are discovered through Docker DNS (service name "api"),
# so `docker compose up --scale api=N` needs no config change (up to 8 replicas).
global
log stdout format raw local0 info
maxconn 4096
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 2
timeout resolve 1s
timeout retry 500ms
hold valid 1s
hold other 1s
hold refused 1s
hold nx 1s
hold timeout 1s
hold obsolete 1s
defaults
mode http
log global
option httplog
option dontlog-normal # log only errors and server state changes
timeout connect 1s
timeout client 30s
timeout server 10s
timeout http-request 10s
timeout http-keep-alive 10s
timeout check 1s
# A request that never reached a backend (connect refused/timeout) is retried, the last time on
# another server. Requests already sent are NOT retried: POST /users and /chats are not idempotent.
retries 2
retry-on conn-failure
option redispatch 1
frontend public
bind :8080
default_backend api
backend api
balance roundrobin
option httpchk
http-check send meth GET uri /health/ready
http-check expect status 200
# Active check every 1 s (0.5 s while state is changing): 2 failures -> DOWN, 2 successes -> UP.
# A refused connection on live traffic marks the server DOWN immediately (observe layer4).
default-server check inter 1s fastinter 500ms downinter 1s fall 2 rise 2 observe layer4 error-limit 1 on-error mark-down resolvers docker init-addr none
server-template api 8 api:8080
# Operator view (bound to 127.0.0.1 on the host by compose): /stats and Prometheus /metrics.
frontend stats
bind :8404
http-request use-service prometheus-exporter if { path /metrics }
stats enable
stats uri /stats
stats refresh 2s
+571
View File
@@ -0,0 +1,571 @@
{
"name": "messenger-lab3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "messenger-lab3",
"dependencies": {
"pg": "8.23.1"
},
"devDependencies": {
"@types/node": "24.19.2",
"@types/pg": "8.23.1",
"typescript": "7.0.2"
},
"engines": {
"node": ">=24"
}
},
"node_modules/@types/node": {
"version": "24.19.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.19.2.tgz",
"integrity": "sha512-93d49wCWJkCyN87MxLpNN6KXPCwFw7bVUg79mGUV1OS40JGWwVCr8n61DXCSqz7CV6/VfPkb7xec7uP2M+QHWQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": ">=7.24.0 <7.24.7"
}
},
"node_modules/@types/pg": {
"version": "8.23.1",
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.23.1.tgz",
"integrity": "sha512-fKVHpikPdg4GKks3JuLEhvwSyvwzF23hnabPy6DD8ljVbC7+6J5dQzdv4arV6jqq57djnMgs1HKBxX4P8aBI3A==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/node": "*",
"pg-protocol": "*",
"pg-types": "^2.2.0"
}
},
"node_modules/@typescript/typescript-aix-ppc64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz",
"integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"aix"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-darwin-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz",
"integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-darwin-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz",
"integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-freebsd-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz",
"integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-freebsd-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz",
"integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-arm": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz",
"integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==",
"cpu": [
"arm"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz",
"integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-loong64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz",
"integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==",
"cpu": [
"loong64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-mips64el": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz",
"integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==",
"cpu": [
"mips64el"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-ppc64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz",
"integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-riscv64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz",
"integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==",
"cpu": [
"riscv64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-s390x": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz",
"integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==",
"cpu": [
"s390x"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-linux-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz",
"integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-netbsd-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz",
"integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-netbsd-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz",
"integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-openbsd-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz",
"integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-openbsd-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz",
"integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-sunos-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz",
"integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"sunos"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-win32-arm64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz",
"integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/@typescript/typescript-win32-x64": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz",
"integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==",
"cpu": [
"x64"
],
"dev": true,
"license": "Apache-2.0",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=16.20.0"
}
},
"node_modules/pg": {
"version": "8.23.1",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.23.1.tgz",
"integrity": "sha512-aL96AHANtWjPLDOLqnhx+ngp9+UK7ETEU8VJrDCGvsSSi/mGLcWYsS6Herg7lmaBJe4uwrfqsa7gTEFaSizDoQ==",
"license": "MIT",
"dependencies": {
"pg-connection-string": "^2.14.1",
"pg-pool": "^3.14.0",
"pg-protocol": "^1.16.1",
"pg-types": "2.2.0",
"pgpass": "1.0.5"
},
"engines": {
"node": ">= 16.0.0"
},
"optionalDependencies": {
"pg-cloudflare": "^1.4.1"
},
"peerDependencies": {
"pg-native": ">=3.0.1"
},
"peerDependenciesMeta": {
"pg-native": {
"optional": true
}
}
},
"node_modules/pg-cloudflare": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.4.1.tgz",
"integrity": "sha512-6PQbsFWZcp9EmJEwy5cGQ2La+AMWpP46lgbb8X+U/XsHIUweYDNCpeuKck5RxL2MdVFi7krbbEi5nX4Zh7JhrQ==",
"license": "MIT",
"optional": true
},
"node_modules/pg-connection-string": {
"version": "2.14.1",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.14.1.tgz",
"integrity": "sha512-qR3kGNPBLpCNtz0evbKA0Y/MRFXwSSdT+pTJvYp/bXTcReZbvX1kzF0IyTc1QnxqF7AZbOeBhNL8R5mYQZV/MA==",
"license": "MIT"
},
"node_modules/pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
"license": "ISC",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/pg-pool": {
"version": "3.14.0",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz",
"integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==",
"license": "MIT",
"peerDependencies": {
"pg": ">=8.0"
}
},
"node_modules/pg-protocol": {
"version": "1.16.1",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.16.1.tgz",
"integrity": "sha512-p9VOFMiHB/ZbJATetbg+99PxssTVSQRnyuPSQ67mN1+1KBOjZaZ83ZQzltnxPhJwSsC3nwVjJ10DVJlerbFzLg==",
"license": "MIT"
},
"node_modules/pg-types": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
"license": "MIT",
"dependencies": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/pgpass": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
"license": "MIT",
"dependencies": {
"split2": "^4.1.0"
}
},
"node_modules/postgres-array": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
"license": "MIT",
"engines": {
"node": ">=4"
}
},
"node_modules/postgres-bytea": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz",
"integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-date": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/postgres-interval": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
"license": "MIT",
"dependencies": {
"xtend": "^4.0.0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/split2": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
"license": "ISC",
"engines": {
"node": ">= 10.x"
}
},
"node_modules/typescript": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz",
"integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc"
},
"engines": {
"node": ">=16.20.0"
},
"optionalDependencies": {
"@typescript/typescript-aix-ppc64": "7.0.2",
"@typescript/typescript-darwin-arm64": "7.0.2",
"@typescript/typescript-darwin-x64": "7.0.2",
"@typescript/typescript-freebsd-arm64": "7.0.2",
"@typescript/typescript-freebsd-x64": "7.0.2",
"@typescript/typescript-linux-arm": "7.0.2",
"@typescript/typescript-linux-arm64": "7.0.2",
"@typescript/typescript-linux-loong64": "7.0.2",
"@typescript/typescript-linux-mips64el": "7.0.2",
"@typescript/typescript-linux-ppc64": "7.0.2",
"@typescript/typescript-linux-riscv64": "7.0.2",
"@typescript/typescript-linux-s390x": "7.0.2",
"@typescript/typescript-linux-x64": "7.0.2",
"@typescript/typescript-netbsd-arm64": "7.0.2",
"@typescript/typescript-netbsd-x64": "7.0.2",
"@typescript/typescript-openbsd-arm64": "7.0.2",
"@typescript/typescript-openbsd-x64": "7.0.2",
"@typescript/typescript-sunos-x64": "7.0.2",
"@typescript/typescript-win32-arm64": "7.0.2",
"@typescript/typescript-win32-x64": "7.0.2"
}
},
"node_modules/undici-types": {
"version": "7.24.6",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.24.6.tgz",
"integrity": "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==",
"dev": true,
"license": "MIT"
},
"node_modules/xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
"license": "MIT",
"engines": {
"node": ">=0.4"
}
}
}
}
+21
View File
@@ -0,0 +1,21 @@
{
"name": "messenger-lab3",
"private": true,
"type": "module",
"engines": {
"node": ">=24"
},
"scripts": {
"start": "node src/main.ts",
"migrate": "node src/main.ts migrate",
"typecheck": "tsc"
},
"dependencies": {
"pg": "8.23.1"
},
"devDependencies": {
"@types/node": "24.19.2",
"@types/pg": "8.23.1",
"typescript": "7.0.2"
}
}
+23
View File
@@ -0,0 +1,23 @@
#!/bin/sh
# Track 1: for each "replicas cpus pool" config, step the offered rate and record k6 summary + docker CPU sample.
# Usage: research/bench.sh OUT_DIR "N CPUS POOL" ... (rates from RATES env)
set -eu
cd "$(dirname "$0")/.."
out=$1; shift
mkdir -p "$out"
RATES=${RATES:-"400 800 1200 1600 2400"}
for cfg in "$@"; do
set -- $cfg
n=$1 cpus=$2 pool=$3
mem=$((512 / n))m
API_REPLICAS=$n API_CPUS=$cpus API_MEMORY=$mem DB_POOL_MAX=$pool docker compose --env-file .env up -d --wait api lb >/dev/null 2>&1
sleep 3
for rate in $RATES; do
tag="n${n}_c${cpus}_p${pool}_r${rate}"
( sleep 8; docker stats --no-stream --format '{{.Name}} {{.CPUPerc}}' | grep messenger-lab3 > "$out/$tag.cpu" ) &
docker run --rm --cpus 3 --network messenger-lab3_frontend -v "$PWD/research/k6:/k6:ro" \
-e RATE="$rate" -e DURATION=15s -e PAIRS=10 grafana/k6:1.8.2 run -q /k6/messenger.js 2>/dev/null | tail -1 > "$out/$tag.json"
wait
echo "$tag $(cat "$out/$tag.json")"
done
done
+108
View File
@@ -0,0 +1,108 @@
// Open-model load profile (constant arrival rate, no coordinated omission):
// WRITE_SHARE of iterations POST a message with a fresh Idempotency-Key, the rest read a history page (limit 20).
// Run through docker on the stand network, see research/README.md.
import http from 'k6/http';
import encoding from 'k6/encoding';
import { Counter } from 'k6/metrics';
const BASE = __ENV.BASE_URL || 'http://lb:8080';
const RATE = Number(__ENV.RATE || 200);
const WRITE_SHARE = Number(__ENV.WRITE_SHARE || 0.5);
const PAIRS = Number(__ENV.PAIRS || 20);
const OUT = __ENV.OUT || '';
const DURATION = __ENV.DURATION || '30s';
const byInstance = new Counter('by_instance');
const failed = new Counter('failed_requests');
export const options = {
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: RATE,
timeUnit: '1s',
duration: DURATION,
preAllocatedVUs: Number(__ENV.VUS || Math.max(50, RATE / 4)),
maxVUs: Number(__ENV.MAX_VUS || 2000),
gracefulStop: '5s',
},
},
setupTimeout: '180s',
// Thresholds only create submetrics, so setup() traffic is excluded from the numbers below.
thresholds: {
'http_reqs{scenario:load}': ['count>=0'],
'http_req_duration{scenario:load}': ['max>=0'],
'http_req_duration{op:write}': ['max>=0'],
'http_req_duration{op:read}': ['max>=0'],
},
summaryTrendStats: ['avg', 'p(50)', 'p(95)', 'p(99)', 'max'],
};
const json = { headers: { 'Content-Type': 'application/json' }, timeout: '10s' };
function user(tag) {
const username = `k6_${tag}_${Math.random().toString(36).slice(2, 12)}`;
const password = 'Bench-' + Math.random().toString(36).slice(2) + 'xx';
const created = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: 'bench ' + tag, password }), json);
if (created.status !== 201) throw new Error('register ' + created.status);
const basic = encoding.b64encode(`${username}:${password}`);
const s = http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: 'Basic ' + basic } });
if (s.status !== 201) throw new Error('login ' + s.status);
return { id: created.json('id'), token: s.json('token') };
}
export function setup() {
const actors = [];
for (let i = 0; i < PAIRS; i++) {
const a = user(`a${i}`);
const b = user(`b${i}`);
const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: 'bench ' + i, member_ids: [a.id, b.id] }),
{ headers: { ...json.headers, Authorization: 'Bearer ' + a.token } });
if (chat.status !== 201) throw new Error('chat ' + chat.status);
const chatId = chat.json('id');
for (let m = 0; m < 20; m++) { // non-empty first history page
http.post(`${BASE}/api/v1/chats/${chatId}/messages`, JSON.stringify({ text: 'seed ' + m }),
{ headers: { ...json.headers, Authorization: 'Bearer ' + a.token, 'Idempotency-Key': `seed-${chatId}-${m}` } });
}
actors.push({ token: a.token, chatId }, { token: b.token, chatId });
}
return { actors };
}
export default function (data) {
const actor = data.actors[Math.floor(Math.random() * data.actors.length)];
const auth = { Authorization: 'Bearer ' + actor.token };
const write = Math.random() < WRITE_SHARE;
const res = write
? http.post(`${BASE}/api/v1/chats/${actor.chatId}/messages`, JSON.stringify({ text: `load ${__VU}-${__ITER}` }),
{ headers: { ...json.headers, ...auth, 'Idempotency-Key': `k6-${__VU}-${__ITER}-${Date.now()}` }, timeout: '10s', tags: { op: 'write' } })
: http.get(`${BASE}/api/v1/chats/${actor.chatId}/messages?limit=20`, { headers: auth, timeout: '10s', tags: { op: 'read' } });
const ok = res.status === (write ? 201 : 200);
if (!ok) failed.add(1, { status: String(res.status) });
byInstance.add(1, { instance: res.headers['X-Instance-Id'] || 'none', ok: String(ok) });
}
export function handleSummary(data) {
const m = data.metrics;
const pick = (v) => ({ avg: v.avg, p50: v['p(50)'], p95: v['p(95)'], p99: v['p(99)'], max: v.max });
const d = m['http_req_duration{scenario:load}'].values;
const reqs = m['http_reqs{scenario:load}'].values.count;
const fails = m.failed_requests ? m.failed_requests.values.count : 0;
const seconds = parseFloat(DURATION); // seconds, e.g. '30s'
const summary = {
offered_rps: RATE,
write_share: WRITE_SHARE,
duration_s: Number(seconds.toFixed(2)),
requests: reqs,
failed: fails,
error_ratio: reqs ? Number((fails / reqs).toFixed(6)) : null,
successful_rps: Number(((reqs - fails) / seconds).toFixed(1)),
dropped_iterations: m.dropped_iterations ? m.dropped_iterations.values.count : 0,
latency_ms: pick(d),
write_latency_ms: pick(m['http_req_duration{op:write}'].values),
read_latency_ms: pick(m['http_req_duration{op:read}'].values),
max_vus: m.vus_max ? m.vus_max.values.max : null,
};
const out = { stdout: JSON.stringify(summary) + '\n' };
if (OUT) out[OUT] = JSON.stringify(summary, null, 2);
return out;
}
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
"""Aggregate evidence/track1/raw/*.json (k6 summaries) + *.cpu into a Markdown table and an SVG chart."""
import json
import re
import sys
from pathlib import Path
raw = Path(sys.argv[1] if len(sys.argv) > 1 else 'evidence/track1/raw')
rows = []
for f in sorted(raw.glob('*.json')):
m = re.match(r'n(\d+)_c([\d.]+)_p(\d+)_r(\d+)', f.stem)
if not m or not f.read_text().strip():
continue
d = json.loads(f.read_text())
cpu = {}
for line in (f.with_suffix('.cpu').read_text().splitlines() if f.with_suffix('.cpu').exists() else []):
name, pct = line.split()
role = name.split('-')[2]
cpu[role] = cpu.get(role, 0) + float(pct.rstrip('%'))
rows.append((f'{m[1]}×{m[2]} CPU, pool {m[3]}', int(m[4]), d, cpu))
rows.sort(key=lambda r: (r[0], r[1]))
print('| Конфигурация API | Offered RPS | Успешные RPS | Ошибки | Dropped | p50 мс | p95 мс | p99 мс | CPU api % | CPU postgres % | CPU lb % |')
print('|---|---|---|---|---|---|---|---|---|---|---|')
for cfg, rate, d, cpu in rows:
l = d['latency_ms']
print(f"| {cfg} | {rate} | {d['successful_rps']} | {d['failed']} | {d['dropped_iterations']} | {l['p50']:.1f} | {l['p95']:.1f} | {l['p99']:.1f} | "
f"{cpu.get('api', 0):.0f} | {cpu.get('postgres', 0):.0f} | {cpu.get('lb', 0):.0f} |")
# SVG: successful RPS vs offered RPS, one line per configuration.
W, H, P = 640, 380, 50
series = {}
for cfg, rate, d, _ in rows:
series.setdefault(cfg, []).append((rate, d['successful_rps']))
xmax = max(r for _, r, _, _ in rows)
ymax = xmax
sx = lambda x: P + x / xmax * (W - 2 * P)
sy = lambda y: H - P - y / ymax * (H - 2 * P)
colors = ['#2a6fdb', '#d9480f', '#2b8a3e', '#862e9c', '#495057']
svg = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" font-family="sans-serif" font-size="12">',
'<rect width="100%" height="100%" fill="white"/>',
f'<line x1="{P}" y1="{H-P}" x2="{W-P}" y2="{H-P}" stroke="black"/><line x1="{P}" y1="{P}" x2="{P}" y2="{H-P}" stroke="black"/>',
f'<line x1="{sx(0)}" y1="{sy(0)}" x2="{sx(xmax)}" y2="{sy(xmax)}" stroke="#aaa" stroke-dasharray="4"/>',
f'<text x="{W/2}" y="{H-12}" text-anchor="middle">offered RPS</text>',
f'<text x="14" y="{H/2}" transform="rotate(-90 14 {H/2})" text-anchor="middle">successful RPS</text>']
for t in range(0, xmax + 1, 400):
svg.append(f'<text x="{sx(t)}" y="{H-P+16}" text-anchor="middle">{t}</text><text x="{P-6}" y="{sy(t)+4}" text-anchor="end">{t}</text>')
for i, (cfg, pts) in enumerate(series.items()):
c = colors[i % len(colors)]
svg.append(f'<polyline fill="none" stroke="{c}" stroke-width="2" points="{" ".join(f"{sx(x):.0f},{sy(y):.0f}" for x, y in pts)}"/>')
svg.append(f'<text x="{P+10}" y="{P+16*i}" fill="{c}">{cfg}</text>')
svg.append('</svg>')
(raw.parent / 'throughput.svg').write_text('\n'.join(svg))
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
# Level 4: under constant k6 load, stop each API replica in turn (restoring the previous one first).
# Prints per-replica k6 summary: successful RPS, errors, latency. Recovery = downtime of the failed replica.
set -eu
cd "$(dirname "$0")/.."
RATE=${RATE:-300}
for cid in $(docker compose ps -q api); do
name=$(docker inspect -f '{{.Name}}' "$cid")
docker run --rm --network messenger-lab3_frontend -v "$PWD/research/k6:/k6:ro" \
-e RATE="$RATE" -e DURATION=20s -e PAIRS=5 grafana/k6:1.8.2 run -q /k6/messenger.js 2>/dev/null | tail -1 > /tmp/failover-each.$$ &
sleep 12 # setup + 5 s of steady load
t0=$(date +%s)
docker stop "$cid" >/dev/null # graceful: readiness 503 -> LB DOWN -> drain
t1=$(date +%s)
wait
echo "stopped $name in $((t1 - t0)) s: $(cat /tmp/failover-each.$$)"
docker start "$cid" >/dev/null
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$cid")" = healthy ]; do sleep 1; done
echo "restored $name"
done
rm -f /tmp/failover-each.$$
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
# Creates local synthetic credentials and .env once. Never overwrites: the pgdata volume keeps the old password.
set -eu
cd "$(dirname "$0")/.."
[ -f .env ] || cp .env.example .env
if [ -f secrets/postgres_password ]; then
echo "secrets/ already exists, keeping it"
exit 0
fi
mkdir -p secrets
chmod 700 secrets
password=$(openssl rand -hex 24)
printf '%s' "$password" > secrets/postgres_password
printf 'postgres://messenger:%s@postgres:5432/messenger' "$password" > secrets/database_url
chmod 644 secrets/postgres_password secrets/database_url # read by container users; the directory stays 700
echo "created secrets/ and .env"
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
# Restarts the LB, every API replica and PostgreSQL; the pgdata volume and the schema are kept as is.
set -eu
cd "$(dirname "$0")/.."
docker compose stop lb api # graceful SIGTERM, replicas drain first
docker compose restart postgres # same container, same volume
docker compose start api lb # migrate is a no-op on an up-to-date schema
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
# Rolling replacement for Compose: rebuild the image, then for each old replica start one new replica,
# wait until it is healthy (HAProxy picks it up via DNS), and only then stop/remove the old one.
# Migrations run before (expand-only, compatible with the running version).
set -eu
cd "$(dirname "$0")/.."
docker compose build api >/dev/null
docker compose run --rm migrate >/dev/null
old=$(docker compose ps -q api)
known=$old
n=$(printf '%s\n' "$old" | grep -c .)
for cid in $old; do
docker compose up -d --no-deps --no-recreate --scale api=$((n + 1)) api >/dev/null 2>&1
new=$(docker compose ps -q api | grep -v -F "$known")
known="$known
$new"
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 1; done
sleep 2 # rise 2 x inter 1 s: LB marks the new replica UP
docker stop "$cid" >/dev/null && docker rm "$cid" >/dev/null
echo "replaced $(echo "$cid" | cut -c1-12) -> $(echo "$new" | cut -c1-12)"
done
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
# Stops exactly the API replica whose X-Instance-Id is TARGET_INSTANCE_ID and leaves it stopped.
# X-Instance-Id = container hostname = first 12 hex chars of the container ID.
# Restore afterwards: docker compose start api
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
cd "$(dirname "$0")/.."
case "$TARGET_INSTANCE_ID" in
*[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;;
esac
ids=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true)
[ "$(printf '%s\n' "$ids" | grep -c .)" -eq 1 ] || { echo "no single running api container for $TARGET_INSTANCE_ID" >&2; exit 1; }
docker stop "$ids" >/dev/null # SIGTERM -> readiness 503 -> drain -> exit (about 3 s)
echo "stopped $(docker inspect -f '{{.Name}}' "$ids") ($TARGET_INSTANCE_ID)"
+334
View File
@@ -0,0 +1,334 @@
import { createHash, randomBytes, randomUUID, scrypt, timingSafeEqual } from 'node:crypto';
import type { IncomingMessage, ServerResponse } from 'node:http';
import type { Pool } from 'pg';
const USERNAME = /^[a-z0-9_]{3,32}$/;
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const IDEMPOTENCY_KEY = /^[A-Za-z0-9._:-]{1,128}$/;
const TIMESTAMP = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}Z$/;
const BODY_LIMIT = 1 << 20;
class HttpError extends Error {
status: number;
code: string;
constructor(status: number, code: string, message: string) {
super(message);
this.status = status;
this.code = code;
}
}
const bad = (msg: string) => new HttpError(400, 'invalid_request', msg);
const notFound = () => new HttpError(404, 'not_found', 'Объект не найден');
const unauthorized = () => new HttpError(401, 'unauthorized', 'Требуется действительная сессия');
type User = { id: string; username: string; display_name: string; created_at: string };
type Ctx = { req: IncomingMessage; url: URL; params: string[] };
type Result = [status: number, body?: unknown];
// Timestamps are formatted by PostgreSQL: JS Date would drop the microseconds the keyset cursor relies on.
const userCols = 'u.id, u.username, u.display_name, rfc3339(u.created_at) AS created_at';
const msgCols = 'id, chat_id, sender_id, text, rfc3339(created_at) AS created_at';
const chatSelect = `SELECT c.id, c.title, rfc3339(c.created_at) AS created_at,
(SELECT array_agg(m.user_id::text ORDER BY m.position) FROM chat_members m WHERE m.chat_id = c.id) AS member_ids
FROM chats c JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $1 `;
const sha256 = (s: string) => createHash('sha256').update(s).digest();
// ---- request parsing ----
async function readJson(req: IncomingMessage): Promise<unknown> {
const type = (req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase();
if (type !== 'application/json') throw new HttpError(415, 'unsupported_media_type', 'Ожидается application/json');
const chunks: Buffer[] = [];
let size = 0;
for await (const chunk of req) {
size += chunk.length;
if (size > BODY_LIMIT) throw new HttpError(413, 'payload_too_large', 'Слишком большое тело запроса');
chunks.push(chunk);
}
try {
return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks)));
} catch {
throw bad('Некорректный JSON');
}
}
// A JSON object with only the allowed keys; extra fields are 400 per contract.
function object(value: unknown, keys: string[]): Record<string, unknown> {
if (typeof value !== 'object' || value === null || Array.isArray(value) || Object.keys(value).some((k) => !keys.includes(k))) {
throw bad('Ожидается JSON-объект без лишних полей');
}
return value as Record<string, unknown>;
}
// Required string: not blank, length counted in Unicode code points.
function isText(v: unknown, min: number, max: number): v is string {
if (typeof v !== 'string' || v.trim() === '') return false;
const n = [...v].length;
return n >= min && n <= max;
}
function limitOf(url: URL): number {
const q = url.searchParams.get('limit');
if (q === null) return 50;
const n = Number(q);
if (!/^\d+$/.test(q) || n < 1 || n > 100) throw bad('limit должен быть целым 1–100');
return n;
}
// Opaque cursor = base64url("kind|scope|created_at|id"); scope binds it to one chat (or user for chat lists).
function encodeCursor(kind: string, scope: string, last: { created_at: string; id: string }): string {
return Buffer.from([kind, scope, last.created_at, last.id].join('|')).toString('base64url');
}
function decodeCursor(url: URL, kind: string, scope: string): [string, string] {
const raw = url.searchParams.get('cursor');
if (raw === null) return ['-infinity', '00000000-0000-0000-0000-000000000000'];
const [k, s, ts, id, ...rest] = Buffer.from(raw, 'base64url').toString().split('|');
if (k !== kind || s !== scope || !TIMESTAMP.test(ts ?? '') || !UUID.test(id ?? '') || rest.length) throw bad('Некорректный cursor');
return [ts, id];
}
function page<T extends { created_at: string; id: string }>(rows: T[], limit: number, kind: string, scope: string) {
const items = rows.slice(0, limit);
return { items, next_cursor: rows.length > limit ? encodeCursor(kind, scope, items[limit - 1]) : null };
}
// ---- passwords: scrypt from node:crypto (N=2^14, r=8, p=1, 16 MiB per hash) ----
function scryptKey(password: string, salt: Buffer, N: number, r: number, p: number): Promise<Buffer> {
return new Promise((resolve, reject) =>
scrypt(password, salt, 32, { N, r, p, maxmem: 64 << 20 }, (err, key) => (err ? reject(err) : resolve(key))));
}
async function hashPassword(password: string): Promise<string> {
const salt = randomBytes(16);
const key = await scryptKey(password, salt, 16384, 8, 1);
return ['scrypt', 16384, 8, 1, salt.toString('base64'), key.toString('base64')].join('$');
}
async function checkPassword(password: string, encoded: string): Promise<boolean> {
const [alg, N, r, p, salt, hash] = encoded.split('$');
if (alg !== 'scrypt') return false;
const want = Buffer.from(hash, 'base64');
const got = await scryptKey(password, Buffer.from(salt, 'base64'), Number(N), Number(r), Number(p));
return timingSafeEqual(got, want);
}
// ---- application ----
export function createApp(db: Pool, instance: string, ttlSeconds: number) {
const dummyHash = hashPassword('dummy-password-for-timing'); // unknown users cost the same as wrong passwords
// Hot-path queries are named: pg prepares them once per connection (Track 1 improvement: no parse/plan per request).
// Bearer session only; Basic and X-User-Id never authenticate business endpoints.
async function auth(c: Ctx): Promise<User> {
const m = /^Bearer +(\S+)$/i.exec(c.req.headers.authorization ?? '');
if (!m) throw unauthorized();
const r = await db.query<User>({ name: 'auth', text: `SELECT ${userCols} FROM sessions s JOIN users u ON u.id = s.user_id
WHERE s.token_hash = $1 AND s.expires_at > now()`, values: [sha256(m[1])] });
if (!r.rowCount) throw unauthorized();
return r.rows[0];
}
function chatIdOf(c: Ctx): string {
if (!UUID.test(c.params[0])) throw bad('Некорректный chat_id');
return c.params[0].toLowerCase();
}
// Strangers get the same 404 as for a missing chat.
async function member(c: Ctx, user: User): Promise<string> {
const chatId = chatIdOf(c);
const r = await db.query({ name: 'member', text: 'SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2', values: [chatId, user.id] });
if (!r.rowCount) throw notFound();
return chatId;
}
async function readiness(): Promise<Result> {
if (!app.ready) throw new HttpError(503, 'unavailable', 'Реплика завершает работу');
try {
await db.query('SELECT 1');
} catch {
throw new HttpError(503, 'unavailable', 'База данных недоступна');
}
return [200, { status: 'ok' }];
}
async function createUser(c: Ctx): Promise<Result> {
const b = object(await readJson(c.req), ['username', 'display_name', 'password']);
if (typeof b.username !== 'string' || !USERNAME.test(b.username) || !isText(b.display_name, 1, 100) || !isText(b.password, 12, 128)) {
throw bad('Неверные username, display_name или password');
}
try {
const r = await db.query<User>(`INSERT INTO users AS u (username, display_name, password_hash) VALUES ($1, $2, $3) RETURNING ${userCols}`,
[b.username, b.display_name, await hashPassword(b.password)]);
return [201, r.rows[0]];
} catch (err) {
if ((err as { code?: string }).code === '23505') throw new HttpError(409, 'conflict', 'Username уже занят');
throw err;
}
}
async function createSession(c: Ctx): Promise<Result> {
const deny = new HttpError(401, 'unauthorized', 'Неверные учётные данные');
const m = /^Basic +([A-Za-z0-9+/]+=*)$/i.exec(c.req.headers.authorization ?? '');
if (!m) throw deny;
const decoded = Buffer.from(m[1], 'base64').toString('utf8');
const colon = decoded.indexOf(':'); // username has no colon; the password may contain one
const username = decoded.slice(0, colon);
const password = decoded.slice(colon + 1);
if (colon < 0 || !USERNAME.test(username)) throw deny;
const r = await db.query<User & { password_hash: string }>(`SELECT ${userCols}, u.password_hash FROM users u WHERE u.username = $1`, [username]);
const row = r.rows[0];
const valid = await checkPassword(password, row?.password_hash ?? await dummyHash);
if (!row || !valid || !isText(password, 12, 128)) throw deny;
const token = randomBytes(32).toString('base64url'); // 256 bits
const s = await db.query(`INSERT INTO sessions (token_hash, user_id, expires_at) VALUES ($1, $2, now() + make_interval(secs => $3))
RETURNING rfc3339(expires_at) AS expires_at`, [sha256(token), row.id, ttlSeconds]);
const { password_hash: _, ...user } = row;
return [201, { token, token_type: 'Bearer', expires_at: s.rows[0].expires_at, user }];
}
async function deleteSession(c: Ctx): Promise<Result> {
await auth(c);
const token = /^Bearer +(\S+)$/i.exec(c.req.headers.authorization ?? '')![1];
await db.query('DELETE FROM sessions WHERE token_hash = $1', [sha256(token)]);
return [204];
}
async function me(c: Ctx): Promise<Result> {
return [200, await auth(c)];
}
async function createChat(c: Ctx): Promise<Result> {
const user = await auth(c);
const b = object(await readJson(c.req), ['title', 'member_ids']);
const ids = b.member_ids;
if (!isText(b.title, 1, 100) || !Array.isArray(ids) || ids.length < 1 || ids.length > 100
|| !ids.every((id) => typeof id === 'string' && UUID.test(id))) {
throw bad('Неверные title или member_ids');
}
const members = ids.map((id: string) => id.toLowerCase());
if (new Set(members).size !== members.length || !members.includes(user.id)) {
throw bad('member_ids должны быть уникальны и включать создателя');
}
try { // one statement = one atomic transaction; an unknown member fails the FK and nothing is created
const r = await db.query(`WITH c AS (INSERT INTO chats (title) VALUES ($1) RETURNING id, title, created_at),
m AS (INSERT INTO chat_members (chat_id, user_id, position) SELECT c.id, t.u::uuid, t.o FROM c, unnest($2::text[]) WITH ORDINALITY AS t(u, o))
SELECT id, title, rfc3339(created_at) AS created_at FROM c`, [b.title, members]);
return [201, { ...r.rows[0], member_ids: members }];
} catch (err) {
if ((err as { code?: string }).code === '23503') throw bad('Неизвестный участник');
throw err;
}
}
async function listChats(c: Ctx): Promise<Result> {
const user = await auth(c);
const limit = limitOf(c.url);
const [ts, id] = decodeCursor(c.url, 'c', user.id);
const r = await db.query(chatSelect + 'WHERE (c.created_at, c.id) > ($2, $3::uuid) ORDER BY c.created_at, c.id LIMIT $4', [user.id, ts, id, limit + 1]);
return [200, page(r.rows, limit, 'c', user.id)];
}
async function getChat(c: Ctx): Promise<Result> {
const user = await auth(c);
const r = await db.query(chatSelect + 'WHERE c.id = $2', [user.id, chatIdOf(c)]);
if (!r.rowCount) throw notFound();
return [200, r.rows[0]];
}
// The key row and the message are inserted by ONE statement, so they commit atomically.
// A concurrent duplicate (any replica) blocks on the key's primary key until the first commits, then replays it.
async function sendMessage(c: Ctx): Promise<Result> {
const user = await auth(c);
const chatId = await member(c, user);
const key = c.req.headers['idempotency-key'];
if (typeof key !== 'string' || !IDEMPOTENCY_KEY.test(key)) throw bad('Нужен Idempotency-Key [A-Za-z0-9._:-]{1,128}');
const b = object(await readJson(c.req), ['text']);
if (!isText(b.text, 1, 4000)) throw bad('text: 1–4000 символов, не только пробелы');
const path = `/api/v1/chats/${chatId}/messages`;
const hash = sha256(b.text); // canonical body: text is the only field
const r = await db.query({ name: 'send', text: `WITH k AS (
INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, message_id)
VALUES ($1, 'POST', $2, $3, $4, gen_random_uuid()) ON CONFLICT DO NOTHING RETURNING message_id)
INSERT INTO messages (id, chat_id, sender_id, text) SELECT message_id, $5, $1, $6 FROM k RETURNING ${msgCols}`,
values: [user.id, path, key, hash, chatId, b.text] });
if (r.rowCount) return [201, r.rows[0]];
const old = await db.query(`SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, rfc3339(m.created_at) AS created_at
FROM idempotency_keys k JOIN messages m ON m.id = k.message_id
WHERE k.user_id = $1 AND k.method = 'POST' AND k.path = $2 AND k.key = $3`, [user.id, path, key]);
const { request_hash, ...message } = old.rows[0];
if (!hash.equals(request_hash)) throw new HttpError(409, 'conflict', 'Idempotency-Key уже использован с другим телом');
return [201, message];
}
async function listMessages(c: Ctx): Promise<Result> {
const user = await auth(c);
const chatId = await member(c, user);
const limit = limitOf(c.url);
const [ts, id] = decodeCursor(c.url, 'm', chatId);
const r = await db.query({ name: 'history', text: `SELECT ${msgCols} FROM messages WHERE chat_id = $1 AND (created_at, id) > ($2, $3::uuid)
ORDER BY created_at, id LIMIT $4`, values: [chatId, ts, id, limit + 1] });
return [200, page(r.rows, limit, 'm', chatId)];
}
const routes: [string, RegExp, (c: Ctx) => Promise<Result>][] = [
['GET', /^\/health\/live$/, async () => [200, { status: 'ok' }]],
['GET', /^\/health\/ready$/, readiness],
['POST', /^\/api\/v1\/users$/, createUser],
['POST', /^\/api\/v1\/auth\/sessions$/, createSession],
['DELETE', /^\/api\/v1\/auth\/sessions\/current$/, deleteSession],
['GET', /^\/api\/v1\/users\/me$/, me],
['POST', /^\/api\/v1\/chats$/, createChat],
['GET', /^\/api\/v1\/chats$/, listChats],
['GET', /^\/api\/v1\/chats\/([^/]+)$/, getChat],
['POST', /^\/api\/v1\/chats\/([^/]+)\/messages$/, sendMessage],
['GET', /^\/api\/v1\/chats\/([^/]+)\/messages$/, listMessages],
];
async function dispatch(req: IncomingMessage): Promise<Result> {
const url = new URL(req.url ?? '/', 'http://api');
for (const [method, pattern, fn] of routes) {
const m = pattern.exec(url.pathname);
if (m && req.method === method) return fn({ req, url, params: m.slice(1) });
}
throw notFound();
}
function send(res: ServerResponse, status: number, body?: unknown) {
if (body === undefined) {
res.writeHead(status).end();
return;
}
const data = JSON.stringify(body);
res.writeHead(status, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) }).end(data);
}
// SQL errors are our bugs (500); connection loss, pool timeout, PG restart/cancel are dependency outages (503).
function toHttpError(err: unknown, rid: string): HttpError {
if (err instanceof HttpError) return err;
const { code = '', message = '' } = err as { code?: string; message?: string };
console.error(`request ${rid}: ${code} ${message}`);
const transient = /^(08|53|57)/.test(code) || /^E[A-Z]+$/.test(code) || /connect|terminated|timeout/i.test(message);
return transient ? new HttpError(503, 'unavailable', 'Сервис временно недоступен') : new HttpError(500, 'internal_error', 'Внутренняя ошибка');
}
const app = {
ready: true,
handle(req: IncomingMessage, res: ServerResponse) {
const rid = randomUUID();
res.setHeader('X-Request-Id', rid);
res.setHeader('X-Instance-Id', instance);
if (!app.ready) res.setHeader('Connection', 'close'); // draining: let the LB drop keep-alive to this replica
dispatch(req).then(([status, body]) => send(res, status, body), (err) => {
const e = toHttpError(err, rid);
if (e.status === 401) {
res.setHeader('WWW-Authenticate', req.url === '/api/v1/auth/sessions' ? 'Basic realm="messenger", charset="UTF-8"' : 'Bearer realm="messenger"');
}
send(res, e.status, { error: { code: e.code, message: e.message, request_id: rid } });
});
},
};
return app;
}
+96
View File
@@ -0,0 +1,96 @@
// Messenger API, lab 3: stateless replica behind a load balancer; all state lives in PostgreSQL.
import { readFileSync, readdirSync } from 'node:fs';
import { createServer } from 'node:http';
import { hostname } from 'node:os';
import pg from 'pg';
import { createApp } from './api.ts';
function env(name: string, def: string): string {
return process.env[name] || def;
}
function envInt(name: string, def: number): number {
const n = Number(env(name, String(def)));
if (!Number.isInteger(n) || n <= 0) throw new Error(`${name} must be a positive integer`);
return n;
}
// NAME_FILE (Docker secret) wins over NAME; secret values are never logged.
function secret(name: string): string {
const file = process.env[name + '_FILE'];
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
if (!value) throw new Error(`${name} or ${name}_FILE is required`);
return value;
}
// Applies src/migrations/*.sql once; the advisory lock serialises concurrent runners.
async function migrate(connectionString: string): Promise<void> {
const client = new pg.Client({ connectionString });
await client.connect();
try {
await client.query('SELECT pg_advisory_lock(3003)');
await client.query('CREATE TABLE IF NOT EXISTS schema_migrations (version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now())');
const dir = new URL('./migrations/', import.meta.url);
for (const file of readdirSync(dir).filter((f) => f.endsWith('.sql')).sort()) {
const done = await client.query('SELECT 1 FROM schema_migrations WHERE version = $1', [file]);
if (done.rowCount) continue;
await client.query('BEGIN');
try {
await client.query(readFileSync(new URL(file, dir), 'utf8'));
await client.query('INSERT INTO schema_migrations (version) VALUES ($1)', [file]);
await client.query('COMMIT');
} catch (err) {
await client.query('ROLLBACK');
throw err;
}
console.log(`applied ${file}`);
}
console.log('migrations up to date');
} finally {
await client.end();
}
}
async function serve(): Promise<void> {
const poolMax = envInt('DB_POOL_MAX', 8);
const db = new pg.Pool({
connectionString: secret('DATABASE_URL'),
max: poolMax,
connectionTimeoutMillis: 2000, // pool exhausted / DB down -> 503 instead of hanging
statement_timeout: 5000,
});
db.on('error', (err) => console.error('idle DB client error:', err.message)); // e.g. PostgreSQL restart
const instance = env('INSTANCE_ID', hostname());
const app = createApp(db, instance, envInt('SESSION_TTL_SECONDS', 3600));
const server = createServer(app.handle);
server.headersTimeout = 5000;
server.requestTimeout = 15000;
server.keepAliveTimeout = 60000;
const port = envInt('APP_PORT', 8080);
server.listen(port, env('APP_HOST', '0.0.0.0'), () => console.log(`instance ${instance} listening on :${port}, pool max ${poolMax}`));
// SIGTERM: fail readiness, keep serving until the LB notices (fall * inter), then finish in-flight requests.
const drainMs = envInt('SHUTDOWN_DRAIN_MS', 2500);
const stop = (signal: string) => {
if (!app.ready) return;
console.log(`${signal}: readiness=503, draining ${drainMs} ms`);
app.ready = false;
setTimeout(() => {
server.close(async () => {
await db.end();
console.log('stopped');
process.exit(0);
});
server.closeIdleConnections();
setTimeout(() => process.exit(1), 10000).unref();
}, drainMs);
};
process.on('SIGTERM', () => stop('SIGTERM'));
process.on('SIGINT', () => stop('SIGINT'));
}
if (process.argv[2] === 'migrate') {
await migrate(secret('DATABASE_URL'));
} else {
await serve();
}
+54
View File
@@ -0,0 +1,54 @@
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username text NOT NULL UNIQUE,
display_name text NOT NULL,
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
-- Only sha256(token) is stored: a DB dump does not contain usable bearer tokens.
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users (id),
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats (id),
user_id uuid NOT NULL REFERENCES users (id),
position int NOT NULL,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats (id),
sender_id uuid NOT NULL REFERENCES users (id),
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
-- Scope: user + method + path + key. message_id is written in the same statement as the message.
-- ponytail: keys are never purged (contract needs >= 24 h); add a periodic DELETE when the table grows.
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL,
method text NOT NULL,
path text NOT NULL,
key text NOT NULL,
request_hash bytea NOT NULL,
message_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (user_id, method, path, key)
);
CREATE FUNCTION rfc3339(timestamptz) RETURNS text LANGUAGE sql STABLE AS
$$ SELECT to_char($1 AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') $$;
+59
View File
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
"""Same Idempotency-Key sent N times in parallel through the LB (so through different replicas).
Half of the requests carry body A, half body B: exactly one message must exist, every 201 must return it,
the other body must get 409. Usage: python3 student-tests/idempotency_race.py --base-url http://localhost:8080"""
from concurrent.futures import ThreadPoolExecutor
import json
from pathlib import Path
import sys
import threading
import uuid
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, check, run # noqa: E402
def main():
args = arguments('Parallel same-key POST race', lambda p: p.add_argument('--parallel', type=int, default=32))
c = Client(args.base_url, args.ca_file)
alice, bob, _, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id'] + '/messages'
key = 'race-' + uuid.uuid4().hex
texts = ['гонка A ' + key, 'гонка B ' + key]
barrier = threading.Barrier(args.parallel)
def send(i):
local = Client(args.base_url, args.ca_file)
barrier.wait()
r = local.call('POST', path, [201, 409], actor=alice, body={'text': texts[i % 2]}, headers={'Idempotency-Key': key}, schema='Message')
return r['status'], r['json'], r['headers']['X-Instance-Id']
with ThreadPoolExecutor(args.parallel) as pool:
results = list(pool.map(send, range(args.parallel)))
created = [body for status, body, _ in results if status == 201]
check(created, 'no request succeeded')
winner = created[0]
check(all(body == winner for body in created), 'replicas returned different messages for one key')
check(all(body['error']['code'] == 'conflict' for status, body, _ in results if status == 409), '409 without conflict code')
statuses = {texts[i % 2]: set() for i in range(2)}
for i, (status, _, _) in enumerate(results):
statuses[texts[i % 2]].add(status)
check(statuses[winner['text']] == {201}, 'winning body must always get 201')
check(statuses[next(t for t in texts if t != winner['text'])] == {409}, 'other body must always get 409')
instances = {inst for _, _, inst in results}
check(len(instances) >= 2, 'race did not reach two replicas')
history = c.call('GET', path + '?limit=100', actor=bob, schema='MessagePage')['json']['items']
stored = [m for m in history if key in m['text']]
check(stored == [winner], 'expected exactly one stored message equal to the replayed one')
# Same key is independent for another user.
other = c.message(bob, chat, texts[0], key=key)['json']
check(other['id'] != winner['id'] and other['sender_id'] == bob['id'], 'key scope leaked across users')
print(json.dumps({'parallel': args.parallel, 'replicas_hit': len(instances), 'created_201': len(created),
'conflict_409': args.parallel - len(created), 'stored_messages': len(stored)}, ensure_ascii=False))
print('PASS: concurrent same-key POSTs through different replicas created exactly one message')
if __name__ == '__main__':
run(main)
+15
View File
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"moduleResolution": "nodenext",
"noEmit": true,
"strict": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true,
"types": ["node"]
},
"include": ["src"]
}