diff --git a/.dockerignore b/.dockerignore index bc16e556..ddd2c738 100644 --- a/.dockerignore +++ b/.dockerignore @@ -17,4 +17,5 @@ backups __pycache__ node_modules evidence +/messenger .DS_Store diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 00000000..68445fcd --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,87 @@ +name: CI + +on: + pull_request: + branches: [main] + push: + branches: [main] + workflow_dispatch: + +concurrency: + group: ci-${{ github.repository }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + verify: + runs-on: ubuntu-latest + timeout-minutes: 20 + env: + COMPOSE_PROJECT_NAME: mtusi2026lab3-${{ github.run_id }} + API_BIND_ADDRESS: 127.0.0.1:0 + SWAGGER_BIND_ADDRESS: 127.0.0.1:0 + PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0 + GRAFANA_BIND_ADDRESS: 127.0.0.1:0 + steps: + - name: Checkout exact commit + run: | + git init . + git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git + git fetch --depth=1 origin ${{ github.sha }} + git checkout --detach FETCH_HEAD + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: false + + - name: Check contract + run: make check + + - name: Validate OpenAPI + run: | + python3 -m venv .venv + .venv/bin/python -m pip install -r requirements-dev.txt + make validate PYTHON=.venv/bin/python + + - name: Check formatting + run: test -z "$(gofmt -l cmd internal migrations)" + + - name: Lint + uses: golangci/golangci-lint-action@v7 + with: + version: v2.13.2 + args: --timeout=5m + skip-cache: true + + - name: Unit tests and build + run: | + go test -race ./... + go build ./cmd/... + + - name: Prepare local stack + run: | + cp lab3.env.example .env + docker compose config --quiet + docker compose up -d --build --scale api=2 --wait --wait-timeout 120 + network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)") + docker network connect "$network" "$HOSTNAME" + printf '%s\n' "$network" > .ci-network + + - name: Integration checks + run: | + make test BASE_URL=http://nginx:8080 + docker compose --profile tests run --rm --build tests + go run ./cmd/dependencycheck -base-url=http://nginx:8080 + make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080 + make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080 + docker compose up -d --scale api=2 --wait --wait-timeout 90 + + - name: Container logs + if: failure() + run: if [ -f .env ]; then docker compose logs --no-color --tail=200; fi + + - name: Stop local stack + if: always() + run: | + if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + if [ -f .env ]; then docker compose --profile tests --profile research down -v --remove-orphans; fi diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 00000000..55344f4e --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,117 @@ +name: Deploy production + +on: + workflow_dispatch: + +concurrency: + group: production + cancel-in-progress: false + +jobs: + verify: + name: Verify selected commit + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + timeout-minutes: 20 + env: + COMPOSE_PROJECT_NAME: mtusi2026lab3-deploy-${{ github.run_id }} + API_BIND_ADDRESS: 127.0.0.1:0 + SWAGGER_BIND_ADDRESS: 127.0.0.1:0 + PROMETHEUS_BIND_ADDRESS: 127.0.0.1:0 + GRAFANA_BIND_ADDRESS: 127.0.0.1:0 + steps: + - name: Checkout exact commit + run: | + git init . + git remote add origin https://gitea.privcloud.tech/${{ github.repository }}.git + git fetch --depth=1 origin ${{ github.sha }} + git checkout --detach FETCH_HEAD + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: false + + - name: Check contract + run: make check + + - name: Validate OpenAPI + run: | + python3 -m venv .venv + .venv/bin/python -m pip install -r requirements-dev.txt + make validate PYTHON=.venv/bin/python + + - name: Check formatting + run: test -z "$(gofmt -l cmd internal migrations)" + + - name: Lint + uses: golangci/golangci-lint-action@v7 + with: + version: v2.13.2 + args: --timeout=5m + skip-cache: true + + - name: Unit tests and build + run: | + go test -race ./... + go build ./cmd/... + + - name: Prepare local stack + run: | + cp lab3.env.example .env + docker compose config --quiet + docker compose up -d --build --scale api=2 --wait --wait-timeout 120 + network=$(docker inspect --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{end}}' "$(docker compose ps -q nginx)") + docker network connect "$network" "$HOSTNAME" + printf '%s\n' "$network" > .ci-network + + - name: Integration checks + run: | + make test BASE_URL=http://nginx:8080 + docker compose --profile tests run --rm --build tests + go run ./cmd/dependencycheck -base-url=http://nginx:8080 + make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://nginx:8080 + make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://nginx:8080 + + - name: Container logs + if: failure() + run: if [ -f .env ]; then docker compose logs --no-color --tail=200; fi + + - name: Stop local stack + if: always() + run: | + if [ -f .ci-network ]; then docker network disconnect "$(cat .ci-network)" "$HOSTNAME"; fi + if [ -f .env ]; then docker compose --profile tests --profile research down -v --remove-orphans; fi + + deploy: + name: Deploy selected commit + needs: verify + runs-on: ubuntu-latest + environment: production + env: + DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }} + DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }} + DEPLOY_SHA: ${{ github.sha }} + steps: + - name: Configure SSH + run: | + umask 077 + mkdir -p ~/.ssh + printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519 + printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > ~/.ssh/known_hosts + + - name: Deploy and verify + run: | + ssh "$DEPLOY_USER@$DEPLOY_HOST" " + set -eu + cd ~/mtusi2026lab3 + git fetch origin main + git cat-file -e '$DEPLOY_SHA^{commit}' + git reset --hard '$DEPLOY_SHA' + git clean -fd + scripts/rolling-update.sh + test \"\$(git rev-parse HEAD)\" = '$DEPLOY_SHA' + docker compose ps + " diff --git a/.gitignore b/.gitignore index b5a71896..37e0ed35 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ dist/ build/ target/ bin/ +/messenger obj/ coverage/ *.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..39cf6e77 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +FROM golang:1.26.5-alpine3.24 AS build + +WORKDIR /src +COPY . . +RUN go build -o /out/messenger ./cmd/messenger +RUN go build -o /out/lab3experiment ./cmd/lab3experiment +RUN go build -o /out/lab3summary ./cmd/lab3summary + +FROM build AS test + +CMD ["go", "test", "./internal/postgres", "./internal/session"] + +FROM swaggerapi/swagger-ui:v5.31.2 AS swagger + +COPY contracts/openapi.json /spec/openapi.json +ENV SWAGGER_JSON=/spec/openapi.json + +FROM prom/prometheus:v3.13.3 AS prometheus + +COPY monitoring/prometheus.yml /etc/prometheus/prometheus.yml + +FROM grafana/grafana:13.1.6 AS grafana + +COPY monitoring/grafana/provisioning /etc/grafana/provisioning +COPY monitoring/grafana/dashboards /etc/grafana/dashboards + +FROM nginx:1.30.5-alpine AS loadbalancer + +COPY nginx/nginx.conf /etc/nginx/nginx.conf + +FROM alpine:3.24.2 + +RUN addgroup -S app && adduser -S -G app -u 10001 app +COPY --from=build --chown=app:app /out/messenger /app/messenger +COPY --from=build --chown=app:app /out/lab3experiment /app/lab3experiment +COPY --from=build --chown=app:app /out/lab3summary /app/lab3summary + +USER app +WORKDIR /app +HEALTHCHECK --interval=10s --timeout=2s --start-period=5s --retries=3 \ + CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1 +CMD ["/app/messenger"] diff --git a/REPORT.md b/REPORT.md index c2679005..66e69021 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,197 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Пластинин Денис, БВТ2303 +- Целевая оценка: 5 +- Выбранный трек: 1 — цена масштабирования +- Ветка/PR: `solution/bvt2303-plastinin`, PR #1 +- Среда эксперимента: Docker, `linux/arm64`, Go 1.26.5 ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требуются Git, Docker Engine с Docker Compose v2 и свободные локальные порты 8080, 8081, 9090 и 3000. Все внешние образы закреплены по версиям. Локальные пароли из примера предназначены только для изолированного стенда. Compose передаёт каждому контейнеру только нужные ему переменные: API получает строки подключения PostgreSQL/Valkey без отдельных исходных паролей и пароля Grafana, а `migrate` — только настройки PostgreSQL и миграций. + +```sh +cp lab3.env.example .env +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build --scale api=2 --wait --wait-timeout 120 +``` + +Compose сначала ожидает PostgreSQL, затем однократно запускает контейнер `migrate`. API-реплики стартуют только после успешного завершения миграций. Единственная публичная точка API — Nginx на `http://localhost:8080`; порты отдельных API-контейнеров на хост не публикуются. + +Доступные интерфейсы: + +- Swagger UI: `http://localhost:8081`; +- Prometheus: `http://localhost:9090`; +- Grafana: `http://localhost:3000`, логин `admin`, локальный пароль берётся из `.env`; +- Nginx API: `http://localhost:8080`. + +Проверки запущенного стенда: + +```sh +make check +make test +docker compose --profile tests run --rm --build tests +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +``` + +Безопасная остановка сохраняет данные в named volumes: + +```sh +docker compose stop +``` + +Повторный запуск выполняется командой `docker compose up -d --scale api=2 --wait`. Намеренное удаление всех учебных данных: + +```sh +docker compose down -v +``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +```mermaid +flowchart LR + C[Клиент] -->|HTTP :8080| N[Nginx] + N --> A1[API replica 1] + N --> A2[API replica 2] + N -. scale .-> AN[API replica N] + A1 --> P[(PostgreSQL)] + A2 --> P + AN --> P + A1 --> V[(Valkey)] + A2 --> V + AN --> V + M[Однократный migrate] --> P + PR[Prometheus] --> A1 + PR --> A2 + PR --> NE[Nginx exporter] + PR --> PE[PostgreSQL exporter] + PR --> VE[Valkey exporter] + G[Grafana] --> PR +``` + +PostgreSQL является источником истины для пользователей, чатов, сообщений и записей идемпотентности. Valkey хранит общие сессии, поэтому запросы с одним bearer token могут обслуживаться разными API-репликами. В Valkey ключ имеет вид `session:`, а значением служит JSON с `user_id` и сроком действия. Исходный случайный 256-битный токен возвращается клиенту один раз и не записывается в хранилище. + +API остаётся stateless относительно пользовательских данных. Локально в процессе находятся только конфигурация, подключения, метрики конкретной реплики и флаг readiness. Каждый HTTP-ответ содержит `X-Instance-Id`, равный hostname контейнера. + +### Балансировка и отказ реплики + +Nginx OSS 1.30.5 использует round-robin, Docker DNS `127.0.0.11`, повторное разрешение имени `api` каждые 2 секунды и общий upstream zone. Keep-alive соединения ограничены 32. Таймаут подключения равен 1 секунде, таймауты отправки и чтения — 10 секунд. Ошибка соединения или ответ 502/503/504 временно исключают backend на 3 секунды; небезопасные POST автоматически не повторяются. + +Docker проверяет `/health/ready` каждой API-реплики раз в 2 секунды с таймаутом 1 секунду. Readiness обращается к PostgreSQL и Valkey. При SIGTERM приложение сначала снимает readiness, затем вызывает `http.Server.Shutdown` с лимитом 5 секунд; Compose даёт контейнеру 8 секунд до принудительной остановки. + +Nginx OSS в этом стенде использует пассивное исключение backend. Docker healthcheck не передаёт своё состояние напрямую в Nginx. Поэтому верхняя практическая граница обнаружения отказа складывается из connect timeout и fail timeout; в проверках восстановление произошло примерно за 1.4 секунды, что ниже требуемых 10 секунд. + +### Общая идемпотентность + +Для `POST /api/v1/chats/{chat_id}/messages` обязателен один заголовок `Idempotency-Key` длиной до 128 символов. Область ключа — `(user_id, chat_id, key)`, поэтому одинаковая строка независима для другого пользователя или чата. Минимальный TTL равен 24 часам. + +Запись сообщения и запись идемпотентности создаются в одной транзакции PostgreSQL: + +1. Проверяется участие пользователя в чате. +2. Из текста считается SHA-256 для выявления повторного использования ключа с другим запросом. +3. Строка идемпотентности блокируется через `SELECT ... FOR UPDATE`; конкурентные вставки разрешаются первичным ключом и `ON CONFLICT DO NOTHING`. +4. Победившая транзакция резервирует UUID сообщения и вставляет сообщение. Отложенный внешний ключ проверяется при commit. +5. Повтор с тем же телом читает исходное сообщение и возвращает тот же объект. Повтор с другим телом получает `409 Conflict`. + +Это позволяет безопасно повторить POST после timeout: подтверждённая ранее запись не дублируется, даже если повтор попал на другую реплику. Каждый успешный запрос также удаляет до 100 истёкших записей по индексу `expires_at`; `SKIP LOCKED` не даёт параллельным репликам ждать одну и ту же пачку. + +### Миграции, обновление и развёртывание + +API не выполняет DDL при старте. Отдельная команда `/app/messenger migrate` берёт advisory lock и применяет versioned SQL один раз. Миграция идемпотентности только добавляет таблицу и индекс, поэтому совместима с уже работающей предыдущей версией. + +`scripts/rolling-update.sh` собирает образы, запускает миграции, временно увеличивает число API с двух до трёх и поочерёдно заменяет старые контейнеры. Скрипт ждёт health каждого состава и затем возвращает две реплики. Workflow deploy запускается только вручную из `main` через `workflow_dispatch`; перед подключением к серверу он повторяет check, formatting, Go tests и build для SHA запуска. На сервере выполняется reset именно на этот SHA, даже если `main` успел измениться. Для SSH используются environment secrets `DEPLOY_HOST`, `DEPLOY_USER`, `DEPLOY_SSH_KEY` и заранее проверенный `DEPLOY_KNOWN_HOSTS`; изменяемых actions для SSH нет. + +## Наблюдаемость + +HTTP middleware каждой реплики публикует число запросов по route/status class, histogram latency, in-flight gauge, число replay идемпотентности, Go runtime и process metrics. Prometheus использует DNS service discovery имени `api` с периодом обновления 2 секунды и отдельно опрашивает Nginx, PostgreSQL и Valkey exporters каждые 5 секунд. Данные хранятся в Prometheus TSDB с retention 7 суток в named volume. + +Provisioned dashboard Grafana показывает общий RPS, RPS и p95 по репликам, классы HTTP-ответов, число healthy replicas, in-flight, heap, replay rate, активные соединения и запросы Nginx, соединения PostgreSQL и состояние Valkey. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` через Nginx | PASS: регистрация, сессия, чат, пагинация, доступ и идемпотентность | `evidence/lab3-verification.txt` | +| Общие сессии и ≥2 реплики | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | Одна сессия обслужена двумя instance ID | `evidence/lab3-verification.txt` | +| Конкурентная идемпотентность | Go black-box через Nginx и database integration suite | 16 одновременных HTTP-запросов обработаны минимум двумя instance ID и вернули один message ID; история содержит одну запись | `evidence/lab3-verification.txt` | +| Сохранность | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | API, PostgreSQL и Valkey перезапущены; старые данные и token доступны | `evidence/lab3-verification.txt` | +| Отказ API | Два запуска failover с остановкой разных реплик | 125–131 успешная запись; 0–1 переходная ошибка; восстановление 1.35–1.44 с; дублей нет | `evidence/lab3-verification.txt` | +| Rolling update | 60 секунд Go-нагрузки и `scripts/rolling-update.sh` | 134894 подтверждения, 2248.16 RPS, 0 ошибок, p95 13.12 ms | `evidence/lab3-rolling-update.json` | +| CPU/RAM-лимиты | `docker compose config` и scale experiment | Лимиты заданы всем сервисам; при 128 MiB API не хватает памяти для Argon2id, поэтому рабочий минимум эксперимента — 256 MiB | `evidence/lab3-verification.txt` | +| Трек 1 | `scripts/run-scale-experiment.sh` | 1/2/4 реплики, два бюджета, concurrency 8/32/64, три повтора | `evidence/lab3-scale-results.json`, `evidence/lab3-scale-rps.svg` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### Методика исследования масштабирования + +Гипотеза: при фиксированном суммарном бюджете дробление ресурсов между большим числом процессов сначала улучшит распределение конкурентных запросов, но затем проиграет из-за меньших CPU-квот и пулов каждой реплики. При добавлении одинаковых ресурсов на реплику RPS должен расти до насыщения PostgreSQL или Nginx. + +Используется Go-клиент `cmd/lab3experiment`. Он создаёт отдельного пользователя, сессию и чат, прогревает стенд 2 секунды, затем 6 секунд выполняет closed-loop POST сообщений через Nginx. Для каждого запроса генерируется уникальный idempotency key. Считаются только HTTP-подтверждения; автоматических retry измеряемого трафика нет. Для каждой точки используются concurrency 8, 32 и 64 и три изолированных повтора с новой базой. + +CPU и Nginx RPS считаются по разности монотонных `process_cpu_seconds_total` и `nginx_http_requests_total`, прочитанных непосредственно до и после измеряемого интервала. Поэтому bootstrap, warmup и простой между точками в них не входят. Resident memory и число healthy replicas снимаются после нагрузки с `/metrics` каждой реплики, соединения PostgreSQL — instant query к Prometheus. Короткая контрольная точка дала 2355.97 client RPS и 2359.64 RPS по Nginx-счётчику, расхождение 0.16%. + +Фиксированная серия имеет общий бюджет API 2 CPU и 1 GiB RAM: `1×(2 CPU, 1 GiB)`, `2×(1 CPU, 512 MiB)`, `4×(0.5 CPU, 256 MiB)`. Пулы PostgreSQL соответственно равны 16, 8 и 4 соединениям на реплику. В растущей серии каждая реплика получает 1 CPU, 256 MiB и пул 8, поэтому суммарный бюджет растёт вместе с числом процессов. + +Предварительный прогон с общим лимитом 512 MiB показал, что вариант `4×128 MiB` не выполняет регистрацию: базовое потребление процесса около 71 MiB, а Argon2id временно требует ещё 64 MiB. Cgroup завершает реплику, Nginx наблюдает premature upstream close. Этот результат определил минимальный рабочий лимит 256 MiB и не включён в сравнение throughput. + +Медианные результаты фиксированного суммарного бюджета: + +| Concurrency | 1 реплика: RPS / p95 ms | 2 реплики: RPS / p95 ms | 4 реплики: RPS / p95 ms | +| ---: | ---: | ---: | ---: | +| 8 | 2412 / 5.86 | 2484 / 5.59 | 2242 / 6.35 | +| 32 | 2731 / 18.63 | 2365 / 24.93 | 2013 / 34.58 | +| 64 | 2733 / 32.99 | 2198 / 55.06 | 2103 / 67.89 | + +При фиксированных 2 CPU и 1 GiB увеличение числа процессов не даёт линейного выигрыша. Две реплики дают небольшой выигрыш только при concurrency 8, одна быстрее при 32 и 64, а четыре проигрывают во всех профилях из-за квоты 0.5 CPU на процесс и расходов четырёх runtime. Для рабочего стенда две реплики остаются осознанным компромиссом ради отказоустойчивости, дальнейшее дробление бюджета не оправдано. + +Медианные результаты серии с 1 CPU и 256 MiB на каждую реплику: + +| Concurrency | 1 реплика: RPS / p95 ms | 2 реплики: RPS / p95 ms | 4 реплики: RPS / p95 ms | +| ---: | ---: | ---: | ---: | +| 8 | 2712 / 4.95 | 2554 / 5.28 | 2185 / 7.05 | +| 32 | 2818 / 15.34 | 2471 / 21.94 | 2130 / 24.14 | +| 64 | 2044 / 61.44 | 2239 / 57.76 | 1859 / 67.42 | + +Даже при росте суммарного CPU практический предел не превышает двух реплик. При concurrency 64 переход `1→2` повышает медианный RPS на 9.6%, а `2→4` снижает его на 17.0%. Одновременно число соединений PostgreSQL растёт `9→17→33`, суммарная resident memory — примерно `36→65→397 MiB`, а p95 у четырёх реплик достигает 67.42 ms. Точная средняя загрузка API составляет примерно `0.43→0.52→0.54 core` при доступных `1→2→4 core`. API не упирается в CPU-квоту. Вместе с ростом DB connections и tail latency это указывает на насыщение общего пути PostgreSQL/Nginx/network и расходы дополнительных процессов, но данный эксперимент не разделяет вклад этих компонентов. + +Во всех 54 основных точках было 0 HTTP-ошибок; каждая конфигурация наблюдала все заявленные реплики. Диапазоны трёх повторов заметно пересекаются и доступны в raw JSON, поэтому небольшие отличия нельзя считать точной оценкой абсолютной производительности. + +Для настройки пула конфигурация `4×0.5 CPU, 256 MiB` при concurrency 64 дополнительно сравнивалась с 10 и 4 соединениями PostgreSQL на реплику: + +| Пул на реплику | Median RPS | p50 / p95 / p99, ms | Соединения PostgreSQL | Ошибки | +| ---: | ---: | ---: | ---: | ---: | +| 10 | 1918 | 30.00 / 59.52 / 101.70 | 41 | 0 | +| 4 | 2179 | 23.61 / 66.76 / 80.91 | 17 | 0 | + +Уменьшение пула `10→4` сократило число соединений на 58.5%, повысило медианный RPS на 13.6%, уменьшило p50 на 21.3% и p99 на 20.4%. p95 вырос на 12.2%, то есть распределение задержек изменилось неравномерно. Для данного стенда выбран пул 4: он соответствует CPU-квоте 0.5 core и заметно снижает давление на общую БД. При строгом SLO на tail latency значение следует подбирать повторно на более длинном open-loop тесте. + +Итоговый график находится в `evidence/lab3-scale-rps.svg`, обезличенные исходные точки, медианы и диапазоны — в `evidence/lab3-scale-results.json`. + +### Отказ и обновление | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| SIGTERM одной API-реплики | Nginx исключает backend, другая реплика продолжает работу | 0–1 переходная ошибка, старый instance ID исчез из последующих ответов | 1.35–1.44 с; подтверждённые сообщения сохранены, дублей нет | +| Полный restart API, PostgreSQL и Valkey без удаления volumes | Данные и сессия сохраняются | Старый bearer token и созданные сущности доступны после запуска | В пределах 90-секундного окна теста; потерь не обнаружено | +| Rolling update API 2→3→2 | Во время замены остаются healthy backend | 134894 успешных POST, 0 ошибок; наблюдались старые и новые instance ID | Перерыва по подтверждённым ответам не зафиксировано | +| Остановка PostgreSQL или Valkey | Readiness становится отрицательной | Все API теряют готовность, потому что хранилища общие | Автоматический failover хранилищ не реализован | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Все компоненты работают на одном Docker-хосте. Отказ хоста, Nginx, PostgreSQL или Valkey останавливает сервис; выполнен отказ процесса API, а не отказ узла. +- Nginx является одной публичной точкой и SPOF. Для эксплуатации потребуются несколько LB с внешним virtual IP или управляемый балансировщик. +- PostgreSQL и Valkey не реплицированы. AOF Valkey с `appendfsync everysec` ограничивает возможную потерю примерно последней секундой при аварии хранилища, но этот сценарий в работе не заявляется как защищённый. +- Нагрузочный клиент closed-loop и подвержен coordinated omission. Результаты сравнивают конфигурации на одной машине и не являются абсолютной производительностью production-системы. +- Все реплики и клиент делят ресурсы одного хоста; дельта process CPU показывает потребление API, а не полную стоимость Docker/network/storage. +- Для эксплуатации нужны TLS, внешнее управление secrets, репликация хранилищ, алерты и длительный soak test; при большом потоке следует отдельно измерить стоимость пакетной очистки idempotency records. + +## Источники и инструменты + +- Nginx upstream и динамическое DNS: +- Docker Compose services и healthcheck: +- PostgreSQL explicit locking: +- Valkey persistence: +- Argon2: RFC 9106, +- Go packages: `pgx/v5`, `valkey-go`, `prometheus/client_golang`, `google/uuid`, `viper`, `x/crypto/argon2`. +- OpenAI Codex использовался для подготовки кода, конфигурации и текста; реализация и результаты проверены запуском тестов и экспериментов. diff --git a/cmd/dependencycheck/main.go b/cmd/dependencycheck/main.go new file mode 100644 index 00000000..eaa3d643 --- /dev/null +++ b/cmd/dependencycheck/main.go @@ -0,0 +1,330 @@ +package main + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "io" + "net/http" + "os" + "os/exec" + "strings" + "sync" + "time" + + "github.com/google/uuid" +) + +type apiClient struct { + baseURL string + client *http.Client + token string + userID string +} + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + fmt.Println("PASS: replica idempotency and dependency recovery") +} + +func run() error { + baseURL := flag.String("base-url", "http://localhost:8080", "API base URL") + flag.Parse() + client := &apiClient{baseURL: *baseURL, client: &http.Client{Timeout: 10 * time.Second}} + if err := client.registerAndLogin(); err != nil { + return err + } + if err := client.expect(http.MethodGet, "/api/v1/users/me", http.StatusOK, true); err != nil { + return err + } + if err := client.verifyConcurrentIdempotency(); err != nil { + return err + } + if err := verifyOutage(client, "valkey"); err != nil { + return err + } + return verifyOutage(client, "postgres") +} + +func (c *apiClient) registerAndLogin() error { + username := "dep_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16] + password := "T3st-" + uuid.NewString() + body := map[string]string{"username": username, "display_name": "Dependency check", "password": password} + payload, err := json.Marshal(body) + if err != nil { + return err + } + request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/users", bytes.NewReader(payload)) + if err != nil { + return err + } + request.Header.Set("Content-Type", "application/json") + response, err := c.client.Do(request) + if err != nil { + return fmt.Errorf("register: %w", err) + } + registrationBody := response.Body + defer func() { _ = registrationBody.Close() }() + if response.StatusCode != http.StatusCreated { + return fmt.Errorf("register status = %d", response.StatusCode) + } + var user struct { + ID string `json:"id"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&user); err != nil { + return fmt.Errorf("decode user: %w", err) + } + if user.ID == "" { + return fmt.Errorf("register returned an empty user id") + } + c.userID = user.ID + + request, err = http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", nil) + if err != nil { + return err + } + request.SetBasicAuth(username, password) + response, err = c.client.Do(request) + if err != nil { + return fmt.Errorf("login: %w", err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusCreated { + return fmt.Errorf("login status = %d", response.StatusCode) + } + var session struct { + Token string `json:"token"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil { + return fmt.Errorf("decode session: %w", err) + } + if session.Token == "" { + return fmt.Errorf("login returned an empty token") + } + c.token = session.Token + return nil +} + +type messageResult struct { + messageID string + instanceID string + err error +} + +func (c *apiClient) verifyConcurrentIdempotency() error { + chatID, err := c.createChat() + if err != nil { + return err + } + + const workers = 16 + var ( + key = uuid.NewString() + start = make(chan struct{}) + results = make(chan messageResult, workers) + group sync.WaitGroup + instances = make(map[string]struct{}) + messageID string + ) + transport := &http.Transport{DisableKeepAlives: true, MaxConnsPerHost: workers} + client := &http.Client{Transport: transport, Timeout: 10 * time.Second} + defer transport.CloseIdleConnections() + + for range workers { + group.Add(1) + go func() { + defer group.Done() + <-start + id, instance, requestErr := c.createMessage(client, chatID, key, "concurrent message") + results <- messageResult{messageID: id, instanceID: instance, err: requestErr} + }() + } + close(start) + group.Wait() + close(results) + + for result := range results { + if result.err != nil { + return result.err + } + if result.instanceID == "" { + return fmt.Errorf("message response has an empty X-Instance-Id") + } + instances[result.instanceID] = struct{}{} + if messageID == "" { + messageID = result.messageID + } + if result.messageID != messageID { + return fmt.Errorf("idempotent responses returned different message ids") + } + } + if len(instances) < 2 { + return fmt.Errorf("requests reached %d API instance, want at least 2", len(instances)) + } + return c.verifyMessageHistory(chatID, messageID) +} + +func (c *apiClient) createChat() (string, error) { + payload, err := json.Marshal(map[string]any{ + "title": "Replica check", "member_ids": []string{c.userID}, + }) + if err != nil { + return "", err + } + request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/chats", bytes.NewReader(payload)) + if err != nil { + return "", err + } + request.Header.Set("Authorization", "Bearer "+c.token) + request.Header.Set("Content-Type", "application/json") + response, err := c.client.Do(request) + if err != nil { + return "", fmt.Errorf("create chat: %w", err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusCreated { + return "", fmt.Errorf("create chat status = %d", response.StatusCode) + } + var chat struct { + ID string `json:"id"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&chat); err != nil { + return "", fmt.Errorf("decode chat: %w", err) + } + return chat.ID, nil +} + +func (c *apiClient) createMessage(client *http.Client, chatID, key, text string) (string, string, error) { + payload, err := json.Marshal(map[string]string{"text": text}) + if err != nil { + return "", "", err + } + request, err := http.NewRequest( + http.MethodPost, c.baseURL+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(payload), + ) + if err != nil { + return "", "", err + } + request.Header.Set("Authorization", "Bearer "+c.token) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Idempotency-Key", key) + response, err := client.Do(request) + if err != nil { + return "", "", fmt.Errorf("create message: %w", err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusCreated { + return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("create message status = %d", response.StatusCode) + } + var message struct { + ID string `json:"id"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&message); err != nil { + return "", response.Header.Get("X-Instance-Id"), fmt.Errorf("decode message: %w", err) + } + return message.ID, response.Header.Get("X-Instance-Id"), nil +} + +func (c *apiClient) verifyMessageHistory(chatID, messageID string) error { + request, err := http.NewRequest(http.MethodGet, c.baseURL+"/api/v1/chats/"+chatID+"/messages?limit=100", nil) + if err != nil { + return err + } + request.Header.Set("Authorization", "Bearer "+c.token) + response, err := c.client.Do(request) + if err != nil { + return fmt.Errorf("list messages: %w", err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusOK { + return fmt.Errorf("list messages status = %d", response.StatusCode) + } + var page struct { + Items []struct { + ID string `json:"id"` + } `json:"items"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&page); err != nil { + return fmt.Errorf("decode messages: %w", err) + } + if len(page.Items) != 1 || page.Items[0].ID != messageID { + return fmt.Errorf("message history contains %d items with an unexpected id", len(page.Items)) + } + return nil +} + +func verifyOutage(client *apiClient, service string) (returnErr error) { + if err := compose("stop", service); err != nil { + return err + } + defer func() { + if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); returnErr == nil && err != nil { + returnErr = err + } + }() + if err := client.expectEventually("/health/ready", http.StatusServiceUnavailable, false); err != nil { + return fmt.Errorf("%s readiness outage: %w", service, err) + } + if err := client.expectEventually("/api/v1/users/me", http.StatusServiceUnavailable, true); err != nil { + return fmt.Errorf("%s business outage: %w", service, err) + } + if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); err != nil { + return err + } + if err := client.expectEventually("/health/ready", http.StatusOK, false); err != nil { + return fmt.Errorf("%s readiness recovery: %w", service, err) + } + if err := client.expectEventually("/api/v1/users/me", http.StatusOK, true); err != nil { + return fmt.Errorf("%s session recovery: %w", service, err) + } + return nil +} + +func compose(arguments ...string) error { + command := exec.Command("docker", append([]string{"compose"}, arguments...)...) + if output, err := command.CombinedOutput(); err != nil { + return fmt.Errorf("docker compose %v: %w: %s", arguments, err, bytes.TrimSpace(output)) + } + return nil +} + +func (c *apiClient) expectEventually(path string, status int, authenticated bool) error { + deadline := time.Now().Add(20 * time.Second) + var lastErr error + for time.Now().Before(deadline) { + lastErr = c.expect(http.MethodGet, path, status, authenticated) + if lastErr == nil { + return nil + } + time.Sleep(200 * time.Millisecond) + } + return lastErr +} + +func (c *apiClient) expect(method, path string, status int, authenticated bool) error { + request, err := http.NewRequest(method, c.baseURL+path, nil) + if err != nil { + return err + } + if authenticated { + request.Header.Set("Authorization", "Bearer "+c.token) + } + response, err := c.client.Do(request) + if err != nil { + return err + } + return closeResponse(response, status) +} + +func closeResponse(response *http.Response, status int) error { + defer func() { _ = response.Body.Close() }() + _, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20)) + if response.StatusCode != status { + return fmt.Errorf("status = %d, want %d", response.StatusCode, status) + } + return nil +} diff --git a/cmd/lab3experiment/main.go b/cmd/lab3experiment/main.go new file mode 100644 index 00000000..efc40d07 --- /dev/null +++ b/cmd/lab3experiment/main.go @@ -0,0 +1,522 @@ +package main + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "net" + "net/http" + "net/url" + "os" + "path/filepath" + "runtime" + "sort" + "strconv" + "strings" + "sync" + "sync/atomic" + "time" + + "github.com/google/uuid" +) + +type configuration struct { + baseURL string + prometheusURL string + label string + duration time.Duration + warmup time.Duration + concurrency int + replicas int + apiCPUs string + apiMemory string + dbMaxConns int + output string +} + +type apiClient struct { + baseURL string + client *http.Client + token string + chatID string +} + +type collector struct { + mu sync.Mutex + latencies []time.Duration + errors map[string]int + instances map[string]struct{} +} + +type result struct { + Label string `json:"label"` + Timestamp string `json:"timestamp"` + GOOS string `json:"goos"` + GOARCH string `json:"goarch"` + DurationSeconds float64 `json:"duration_seconds"` + WarmupSeconds float64 `json:"warmup_seconds"` + Concurrency int `json:"concurrency"` + Replicas int `json:"replicas"` + APICPUs string `json:"api_cpus_per_replica"` + APIMemory string `json:"api_memory_per_replica"` + DBMaxConns int `json:"db_max_connections_per_replica"` + SuccessfulRequests int `json:"successful_requests"` + SuccessfulRPS float64 `json:"successful_rps"` + Errors map[string]int `json:"errors"` + LatencyMS latencySummary `json:"latency_ms"` + ObservedInstances int `json:"observed_instances"` + Prometheus map[string]float64 `json:"prometheus"` +} + +type latencySummary struct { + P50 float64 `json:"p50"` + P95 float64 `json:"p95"` + P99 float64 `json:"p99"` +} + +type runtimeSnapshot struct { + apiCPUSeconds float64 + apiResident float64 + healthyReplicas float64 + nginxRequests float64 +} + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run() error { + settings := parseFlags() + client := &apiClient{ + baseURL: strings.TrimRight(settings.baseURL, "/"), + client: &http.Client{Transport: &http.Transport{ + MaxIdleConns: settings.concurrency * 2, + MaxIdleConnsPerHost: settings.concurrency, + MaxConnsPerHost: settings.concurrency, + IdleConnTimeout: 30 * time.Second, + }, Timeout: 10 * time.Second}, + } + if err := client.waitForReplicas(settings.replicas); err != nil { + return err + } + if err := client.bootstrap(); err != nil { + return err + } + if settings.warmup > 0 { + warmup := newCollector() + if err := executeLoad(client, settings.warmup, settings.concurrency, warmup); err != nil { + return fmt.Errorf("warmup: %w", err) + } + } + + var before runtimeSnapshot + if settings.prometheusURL != "" { + snapshot, err := collectRuntimeSnapshot() + if err != nil { + return fmt.Errorf("collect metrics before load: %w", err) + } + before = snapshot + } + measurements := newCollector() + started := time.Now() + if err := executeLoad(client, settings.duration, settings.concurrency, measurements); err != nil { + return err + } + elapsed := time.Since(started) + summary := measurements.result(settings, elapsed) + if settings.prometheusURL != "" { + after, err := collectRuntimeSnapshot() + if err != nil { + return fmt.Errorf("collect metrics after load: %w", err) + } + summary.Prometheus, err = prometheusMeasurement(settings.prometheusURL, before, after, elapsed) + if err != nil { + return err + } + } + return writeResult(settings.output, summary) +} + +func (c *apiClient) waitForReplicas(expected int) error { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + instances := make(map[string]struct{}, expected) + for ctx.Err() == nil { + request, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/health/ready", http.NoBody) + if err != nil { + return err + } + response, err := c.client.Do(request) + if err == nil { + _, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20)) + _ = response.Body.Close() + if response.StatusCode == http.StatusOK { + if instanceID := response.Header.Get("X-Instance-Id"); instanceID != "" { + instances[instanceID] = struct{}{} + } + if len(instances) == expected { + return nil + } + } + } + time.Sleep(100 * time.Millisecond) + } + return fmt.Errorf("nginx discovered %d of %d API replicas: %w", len(instances), expected, ctx.Err()) +} + +func parseFlags() configuration { + var settings configuration + flag.StringVar(&settings.baseURL, "base-url", "http://localhost:8080", "public Nginx base URL") + flag.StringVar(&settings.prometheusURL, "prometheus-url", "", "Prometheus base URL") + flag.StringVar(&settings.label, "label", "manual", "experiment point label") + flag.DurationVar(&settings.duration, "duration", 30*time.Second, "measurement duration") + flag.DurationVar(&settings.warmup, "warmup", 5*time.Second, "warmup duration") + flag.IntVar(&settings.concurrency, "concurrency", 16, "concurrent clients") + flag.IntVar(&settings.replicas, "replicas", 2, "API replica count") + flag.StringVar(&settings.apiCPUs, "api-cpus", "1", "CPU limit per API replica") + flag.StringVar(&settings.apiMemory, "api-memory", "256m", "memory limit per API replica") + flag.IntVar(&settings.dbMaxConns, "db-max-conns", 10, "database pool limit per API replica") + flag.StringVar(&settings.output, "output", "", "JSON output path; stdout when empty") + flag.Parse() + if settings.duration <= 0 || settings.warmup < 0 || settings.concurrency < 1 || settings.replicas < 1 || settings.dbMaxConns < 1 { + fmt.Fprintln(os.Stderr, "duration, warmup and concurrency must be positive") + os.Exit(2) + } + return settings +} + +func (c *apiClient) bootstrap() error { + username := "scale_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20] + password := "Synthetic-" + uuid.NewString() + var user struct { + ID string `json:"id"` + } + if err := c.doJSON(http.MethodPost, "/api/v1/users", "", "", map[string]string{ + "username": username, "display_name": "Scale experiment", "password": password, + }, &user, http.StatusCreated); err != nil { + return fmt.Errorf("register: %w", err) + } + + request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", http.NoBody) + if err != nil { + return err + } + request.SetBasicAuth(username, password) + response, err := c.client.Do(request) + if err != nil { + return fmt.Errorf("login: %w", err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusCreated { + return responseError("login", response) + } + var session struct { + Token string `json:"token"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil { + return fmt.Errorf("decode session: %w", err) + } + c.token = session.Token + + var chat struct { + ID string `json:"id"` + } + if err := c.doJSON(http.MethodPost, "/api/v1/chats", c.token, "", map[string]any{ + "title": "Scale experiment", "member_ids": []string{user.ID}, + }, &chat, http.StatusCreated); err != nil { + return fmt.Errorf("create chat: %w", err) + } + c.chatID = chat.ID + return nil +} + +func executeLoad(client *apiClient, duration time.Duration, concurrency int, measurements *collector) error { + ctx, cancel := context.WithTimeout(context.Background(), duration) + defer cancel() + var sequence atomic.Uint64 + var group sync.WaitGroup + for range concurrency { + group.Add(1) + go func() { + defer group.Done() + for ctx.Err() == nil { + number := sequence.Add(1) + started := time.Now() + instanceID, status, err := client.createMessage(ctx, number) + if err != nil && ctx.Err() != nil { + return + } + measurements.observe(time.Since(started), instanceID, status, err) + } + }() + } + group.Wait() + if len(measurements.latencies) == 0 { + return errors.New("no successful requests") + } + return nil +} + +func (c *apiClient) createMessage(ctx context.Context, sequence uint64) (string, int, error) { + payload, err := json.Marshal(map[string]string{"text": fmt.Sprintf("scale message %d", sequence)}) + if err != nil { + return "", 0, err + } + request, err := http.NewRequestWithContext( + ctx, http.MethodPost, c.baseURL+"/api/v1/chats/"+c.chatID+"/messages", bytes.NewReader(payload), + ) + if err != nil { + return "", 0, err + } + request.Header.Set("Authorization", "Bearer "+c.token) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Idempotency-Key", uuid.NewString()) + response, err := c.client.Do(request) + if err != nil { + return "", 0, err + } + defer func() { _ = response.Body.Close() }() + _, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20)) + if response.StatusCode != http.StatusCreated { + return response.Header.Get("X-Instance-Id"), response.StatusCode, fmt.Errorf("unexpected status") + } + return response.Header.Get("X-Instance-Id"), response.StatusCode, nil +} + +func newCollector() *collector { + return &collector{errors: make(map[string]int), instances: make(map[string]struct{})} +} + +func (c *collector) observe(latency time.Duration, instanceID string, status int, err error) { + c.mu.Lock() + defer c.mu.Unlock() + if err != nil { + key := "network" + if status != 0 { + key = strconv.Itoa(status) + } + c.errors[key]++ + return + } + c.latencies = append(c.latencies, latency) + if instanceID != "" { + c.instances[instanceID] = struct{}{} + } +} + +func (c *collector) result(settings configuration, elapsed time.Duration) result { + sort.Slice(c.latencies, func(i, j int) bool { return c.latencies[i] < c.latencies[j] }) + return result{ + Label: settings.label, Timestamp: time.Now().UTC().Format(time.RFC3339), + GOOS: runtime.GOOS, GOARCH: runtime.GOARCH, + DurationSeconds: elapsed.Seconds(), WarmupSeconds: settings.warmup.Seconds(), + Concurrency: settings.concurrency, Replicas: settings.replicas, + APICPUs: settings.apiCPUs, APIMemory: settings.apiMemory, DBMaxConns: settings.dbMaxConns, + SuccessfulRequests: len(c.latencies), + SuccessfulRPS: float64(len(c.latencies)) / elapsed.Seconds(), Errors: c.errors, + LatencyMS: latencySummary{ + P50: percentile(c.latencies, 0.50), P95: percentile(c.latencies, 0.95), P99: percentile(c.latencies, 0.99), + }, + ObservedInstances: len(c.instances), + } +} + +func percentile(values []time.Duration, percentile float64) float64 { + if len(values) == 0 { + return 0 + } + index := int(float64(len(values)-1) * percentile) + return float64(values[index].Microseconds()) / 1000 +} + +func collectRuntimeSnapshot() (runtimeSnapshot, error) { + hosts, err := net.LookupHost("api") + if err != nil { + return runtimeSnapshot{}, fmt.Errorf("resolve API replicas: %w", err) + } + uniqueHosts := make(map[string]struct{}, len(hosts)) + var snapshot runtimeSnapshot + for _, host := range hosts { + if _, duplicate := uniqueHosts[host]; duplicate { + continue + } + uniqueHosts[host] = struct{}{} + metrics, err := scrapeMetrics("http://" + net.JoinHostPort(host, "8080") + "/metrics") + if err != nil { + return runtimeSnapshot{}, fmt.Errorf("scrape API %s: %w", host, err) + } + cpuSeconds, err := requiredMetric(metrics, "process_cpu_seconds_total") + if err != nil { + return runtimeSnapshot{}, err + } + resident, err := requiredMetric(metrics, "process_resident_memory_bytes") + if err != nil { + return runtimeSnapshot{}, err + } + snapshot.apiCPUSeconds += cpuSeconds + snapshot.apiResident += resident + snapshot.healthyReplicas++ + } + nginxMetrics, err := scrapeMetrics("http://nginx-exporter:9113/metrics") + if err != nil { + return runtimeSnapshot{}, fmt.Errorf("scrape Nginx exporter: %w", err) + } + snapshot.nginxRequests, err = requiredMetric(nginxMetrics, "nginx_http_requests_total") + if err != nil { + return runtimeSnapshot{}, err + } + return snapshot, nil +} + +func scrapeMetrics(endpoint string) (map[string]float64, error) { + client := &http.Client{Timeout: 5 * time.Second} + response, err := client.Get(endpoint) + if err != nil { + return nil, err + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusOK { + return nil, fmt.Errorf("status %d", response.StatusCode) + } + metrics := make(map[string]float64) + scanner := bufio.NewScanner(io.LimitReader(response.Body, 8<<20)) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + fields := strings.Fields(line) + if len(fields) < 2 { + continue + } + name, _, _ := strings.Cut(fields[0], "{") + value, err := strconv.ParseFloat(fields[len(fields)-1], 64) + if err != nil { + return nil, fmt.Errorf("parse metric %s: %w", name, err) + } + metrics[name] += value + } + if err := scanner.Err(); err != nil { + return nil, err + } + return metrics, nil +} + +func requiredMetric(metrics map[string]float64, name string) (float64, error) { + value, found := metrics[name] + if !found { + return 0, fmt.Errorf("metric %s not found", name) + } + return value, nil +} + +func prometheusMeasurement(baseURL string, before, after runtimeSnapshot, elapsed time.Duration) (map[string]float64, error) { + postgresConnections, err := prometheusQuery(baseURL, `sum(pg_stat_activity_count{datname="messenger"})`) + if err != nil { + return nil, fmt.Errorf("query PostgreSQL connections: %w", err) + } + seconds := elapsed.Seconds() + return map[string]float64{ + "api_cpu_cores": (after.apiCPUSeconds - before.apiCPUSeconds) / seconds, + "api_resident_bytes": after.apiResident, + "healthy_replicas": after.healthyReplicas, + "nginx_rps": (after.nginxRequests - before.nginxRequests) / seconds, + "postgres_connections": postgresConnections, + }, nil +} + +func prometheusQuery(baseURL, query string) (float64, error) { + endpoint := strings.TrimRight(baseURL, "/") + "/api/v1/query?query=" + url.QueryEscape(query) + client := &http.Client{Timeout: 5 * time.Second} + response, err := client.Get(endpoint) + if err != nil { + return 0, err + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusOK { + return 0, fmt.Errorf("prometheus status %d", response.StatusCode) + } + var payload struct { + Data struct { + Result []struct { + Value []json.RawMessage `json:"value"` + } `json:"result"` + } `json:"data"` + } + if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&payload); err != nil { + return 0, err + } + var total float64 + for _, item := range payload.Data.Result { + if len(item.Value) != 2 { + continue + } + var encoded string + if err := json.Unmarshal(item.Value[1], &encoded); err != nil { + return 0, err + } + value, err := strconv.ParseFloat(encoded, 64) + if err != nil { + return 0, err + } + total += value + } + return total, nil +} + +func (c *apiClient) doJSON(method, path, token, idempotencyKey string, body, target any, expected int) error { + payload, err := json.Marshal(body) + if err != nil { + return err + } + request, err := http.NewRequest(method, c.baseURL+path, bytes.NewReader(payload)) + if err != nil { + return err + } + request.Header.Set("Content-Type", "application/json") + if token != "" { + request.Header.Set("Authorization", "Bearer "+token) + } + if idempotencyKey != "" { + request.Header.Set("Idempotency-Key", idempotencyKey) + } + response, err := c.client.Do(request) + if err != nil { + return err + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != expected { + return responseError(method+" "+path, response) + } + return json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(target) +} + +func responseError(action string, response *http.Response) error { + body, _ := io.ReadAll(io.LimitReader(response.Body, 1<<20)) + return fmt.Errorf("%s status=%d body=%s", action, response.StatusCode, bytes.TrimSpace(body)) +} + +func writeResult(path string, value result) error { + data, err := json.MarshalIndent(value, "", " ") + if err != nil { + return err + } + data = append(data, '\n') + if path == "" { + _, err = os.Stdout.Write(data) + return err + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + return os.WriteFile(path, data, 0o644) +} diff --git a/cmd/lab3summary/main.go b/cmd/lab3summary/main.go new file mode 100644 index 00000000..8a3b6c41 --- /dev/null +++ b/cmd/lab3summary/main.go @@ -0,0 +1,366 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "html" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "time" +) + +type latency struct { + P50 float64 `json:"p50"` + P95 float64 `json:"p95"` + P99 float64 `json:"p99"` +} + +type inputResult struct { + Label string `json:"label"` + Timestamp string `json:"timestamp"` + DurationSeconds float64 `json:"duration_seconds"` + WarmupSeconds float64 `json:"warmup_seconds"` + Concurrency int `json:"concurrency"` + Replicas int `json:"replicas"` + APICPUs string `json:"api_cpus_per_replica"` + APIMemory string `json:"api_memory_per_replica"` + DBMaxConns int `json:"db_max_connections_per_replica"` + SuccessfulRequests int `json:"successful_requests"` + SuccessfulRPS float64 `json:"successful_rps"` + Errors map[string]int `json:"errors"` + LatencyMS latency `json:"latency_ms"` + Instances []string `json:"instances"` + ObservedInstances int `json:"observed_instances"` + Prometheus map[string]float64 `json:"prometheus"` +} + +type rawResult struct { + Label string `json:"label"` + Timestamp string `json:"timestamp"` + DurationSeconds float64 `json:"duration_seconds"` + WarmupSeconds float64 `json:"warmup_seconds"` + Concurrency int `json:"concurrency"` + Replicas int `json:"replicas"` + APICPUs string `json:"api_cpus_per_replica"` + APIMemory string `json:"api_memory_per_replica"` + DBMaxConns int `json:"db_max_connections_per_replica"` + SuccessfulRequests int `json:"successful_requests"` + SuccessfulRPS float64 `json:"successful_rps"` + Errors map[string]int `json:"errors"` + LatencyMS latency `json:"latency_ms"` + ObservedInstances int `json:"observed_instances"` + Prometheus map[string]float64 `json:"prometheus"` +} + +type aggregate struct { + Label string `json:"label"` + Repeats int `json:"repeats"` + Concurrency int `json:"concurrency"` + Replicas int `json:"replicas"` + APICPUs string `json:"api_cpus_per_replica"` + APIMemory string `json:"api_memory_per_replica"` + DBMaxConns int `json:"db_max_connections_per_replica"` + SuccessfulRPSMedian float64 `json:"successful_rps_median"` + SuccessfulRPSMin float64 `json:"successful_rps_min"` + SuccessfulRPSMax float64 `json:"successful_rps_max"` + LatencyP50MedianMS float64 `json:"latency_p50_median_ms"` + LatencyP95MedianMS float64 `json:"latency_p95_median_ms"` + LatencyP99MedianMS float64 `json:"latency_p99_median_ms"` + TotalErrors int `json:"total_errors"` + ObservedInstancesMin int `json:"observed_instances_min"` + APICPUCoresMedian float64 `json:"api_cpu_cores_median"` + APIResidentBytesMedian float64 `json:"api_resident_bytes_median"` + PostgresConnectionsMedian float64 `json:"postgres_connections_median"` + PrometheusHealthyMin float64 `json:"prometheus_healthy_replicas_min"` +} + +type report struct { + GeneratedAt string `json:"generated_at"` + Method string `json:"method"` + Raw []rawResult `json:"raw"` + Summary []aggregate `json:"summary"` +} + +func main() { + inputDirectory := flag.String("input", "evidence", "directory with experiment JSON files") + outputPath := flag.String("output", "evidence/lab3-scale-results.json", "combined JSON output") + chartPath := flag.String("chart", "evidence/lab3-scale-rps.svg", "SVG chart output") + flag.Parse() + + results, err := readResults(*inputDirectory) + if err != nil { + fatal(err) + } + aggregates := summarize(results) + if err := writeJSON(*outputPath, report{ + GeneratedAt: time.Now().UTC().Format(time.RFC3339), + Method: "closed-loop HTTP through Nginx; median of three isolated repeats", + Raw: sanitize(results), + Summary: aggregates, + }); err != nil { + fatal(err) + } + if err := writeChart(*chartPath, aggregates); err != nil { + fatal(err) + } +} + +func readResults(directory string) ([]inputResult, error) { + var paths []string + for _, pattern := range []string{"lab3-fixed-*.json", "lab3-growing-*.json", "lab3-pool-*.json"} { + matches, err := filepath.Glob(filepath.Join(directory, pattern)) + if err != nil { + return nil, err + } + paths = append(paths, matches...) + } + if len(paths) == 0 { + return nil, fmt.Errorf("no experiment results in %s", directory) + } + + results := make([]inputResult, 0, len(paths)) + for _, path := range paths { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var result inputResult + if err := json.Unmarshal(data, &result); err != nil { + return nil, fmt.Errorf("decode %s: %w", path, err) + } + results = append(results, result) + } + sort.Slice(results, func(i, j int) bool { return results[i].Label < results[j].Label }) + return results, nil +} + +func sanitize(results []inputResult) []rawResult { + raw := make([]rawResult, 0, len(results)) + for _, value := range results { + raw = append(raw, rawResult{ + Label: value.Label, Timestamp: value.Timestamp, + DurationSeconds: value.DurationSeconds, WarmupSeconds: value.WarmupSeconds, + Concurrency: value.Concurrency, Replicas: value.Replicas, + APICPUs: value.APICPUs, APIMemory: value.APIMemory, DBMaxConns: value.DBMaxConns, + SuccessfulRequests: value.SuccessfulRequests, SuccessfulRPS: value.SuccessfulRPS, + Errors: value.Errors, LatencyMS: value.LatencyMS, + ObservedInstances: instanceCount(value), Prometheus: value.Prometheus, + }) + } + return raw +} + +func summarize(results []inputResult) []aggregate { + groups := make(map[string][]inputResult) + for _, value := range results { + groups[withoutRepeat(value.Label)] = append(groups[withoutRepeat(value.Label)], value) + } + + labels := make([]string, 0, len(groups)) + for label := range groups { + labels = append(labels, label) + } + sort.Strings(labels) + + aggregates := make([]aggregate, 0, len(labels)) + for _, label := range labels { + values := groups[label] + first := values[0] + rps := numbers(values, func(value inputResult) float64 { return value.SuccessfulRPS }) + p50 := numbers(values, func(value inputResult) float64 { return value.LatencyMS.P50 }) + p95 := numbers(values, func(value inputResult) float64 { return value.LatencyMS.P95 }) + p99 := numbers(values, func(value inputResult) float64 { return value.LatencyMS.P99 }) + cpu := metricNumbers(values, "api_cpu_cores") + memory := metricNumbers(values, "api_resident_bytes") + connections := metricNumbers(values, "postgres_connections") + healthy := metricNumbers(values, "healthy_replicas") + aggregate := aggregate{ + Label: label, Repeats: len(values), Concurrency: first.Concurrency, Replicas: first.Replicas, + APICPUs: first.APICPUs, APIMemory: first.APIMemory, DBMaxConns: first.DBMaxConns, + SuccessfulRPSMedian: median(rps), SuccessfulRPSMin: minimum(rps), SuccessfulRPSMax: maximum(rps), + LatencyP50MedianMS: median(p50), LatencyP95MedianMS: median(p95), LatencyP99MedianMS: median(p99), + ObservedInstancesMin: instanceCount(first), APICPUCoresMedian: median(cpu), + APIResidentBytesMedian: median(memory), PostgresConnectionsMedian: median(connections), + PrometheusHealthyMin: minimum(healthy), + } + for _, value := range values { + if count := instanceCount(value); count < aggregate.ObservedInstancesMin { + aggregate.ObservedInstancesMin = count + } + for _, count := range value.Errors { + aggregate.TotalErrors += count + } + } + aggregates = append(aggregates, aggregate) + } + return aggregates +} + +func instanceCount(value inputResult) int { + if value.ObservedInstances > 0 { + return value.ObservedInstances + } + return len(value.Instances) +} + +func withoutRepeat(label string) string { + index := strings.LastIndex(label, "-n") + if index == -1 { + return label + } + if _, err := strconv.Atoi(label[index+2:]); err != nil { + return label + } + return label[:index] +} + +func numbers(values []inputResult, value func(inputResult) float64) []float64 { + result := make([]float64, 0, len(values)) + for _, item := range values { + result = append(result, value(item)) + } + return result +} + +func metricNumbers(values []inputResult, name string) []float64 { + result := make([]float64, 0, len(values)) + for _, item := range values { + if value, ok := item.Prometheus[name]; ok { + result = append(result, value) + } + } + return result +} + +func median(values []float64) float64 { + if len(values) == 0 { + return 0 + } + values = append([]float64(nil), values...) + sort.Float64s(values) + middle := len(values) / 2 + if len(values)%2 == 1 { + return values[middle] + } + return (values[middle-1] + values[middle]) / 2 +} + +func minimum(values []float64) float64 { + if len(values) == 0 { + return 0 + } + minimum := values[0] + for _, value := range values[1:] { + if value < minimum { + minimum = value + } + } + return minimum +} + +func maximum(values []float64) float64 { + if len(values) == 0 { + return 0 + } + maximum := values[0] + for _, value := range values[1:] { + if value > maximum { + maximum = value + } + } + return maximum +} + +func writeJSON(path string, value any) error { + data, err := json.MarshalIndent(value, "", " ") + if err != nil { + return err + } + return os.WriteFile(path, append(data, '\n'), 0o644) +} + +func writeChart(path string, values []aggregate) error { + const width, height = 960, 560 + const left, right, top, bottom = 85.0, 240.0, 45.0, 70.0 + plotWidth := width - left - right + plotHeight := height - top - bottom + + var selected []aggregate + var maxRPS float64 + for _, value := range values { + if strings.HasPrefix(value.Label, "fixed-") || strings.HasPrefix(value.Label, "growing-") { + selected = append(selected, value) + if value.SuccessfulRPSMedian > maxRPS { + maxRPS = value.SuccessfulRPSMedian + } + } + } + if maxRPS == 0 { + return fmt.Errorf("no scale results for chart") + } + maxRPS *= 1.1 + + var output strings.Builder + fmt.Fprintf(&output, ``, width, height, width, height) + output.WriteString(``) + fmt.Fprintf(&output, `Median successful RPS by replica count`, left) + + for step := 0; step <= 5; step++ { + y := top + plotHeight*float64(5-step)/5 + value := maxRPS * float64(step) / 5 + fmt.Fprintf(&output, `%.0f`, left, y, left+plotWidth, y, left-10, y+4, value) + } + fmt.Fprintf(&output, ``, left, top, left, top+plotHeight, left, top+plotHeight, left+plotWidth, top+plotHeight) + for index, replicas := range []int{1, 2, 4} { + x := left + plotWidth*float64(index)/2 + fmt.Fprintf(&output, `%d`, x, top+plotHeight+25, replicas) + } + fmt.Fprintf(&output, `API replicas`, left+plotWidth/2, height-15) + + colors := []string{"#1a73e8", "#d93025", "#188038", "#9334e6", "#f29900", "#00897b"} + type series struct { + name string + values []aggregate + } + groups := make(map[string][]aggregate) + for _, value := range selected { + name := strings.Split(value.Label, "-r")[0] + ", c=" + strconv.Itoa(value.Concurrency) + groups[name] = append(groups[name], value) + } + var names []string + for name := range groups { + names = append(names, name) + } + sort.Strings(names) + allSeries := make([]series, 0, len(names)) + for _, name := range names { + points := groups[name] + sort.Slice(points, func(i, j int) bool { return points[i].Replicas < points[j].Replicas }) + allSeries = append(allSeries, series{name: name, values: points}) + } + for index, current := range allSeries { + color := colors[index%len(colors)] + var coordinates []string + for _, point := range current.values { + xIndex := map[int]float64{1: 0, 2: 1, 4: 2}[point.Replicas] + x := left + plotWidth*xIndex/2 + y := top + plotHeight*(1-point.SuccessfulRPSMedian/maxRPS) + coordinates = append(coordinates, fmt.Sprintf("%g,%g", x, y)) + fmt.Fprintf(&output, ``, x, y, color) + } + fmt.Fprintf(&output, ``, strings.Join(coordinates, " "), color) + legendY := top + 20 + float64(index*28) + fmt.Fprintf(&output, `%s`, left+plotWidth+25, legendY, left+plotWidth+50, legendY, color, left+plotWidth+58, legendY+4, html.EscapeString(current.name)) + } + output.WriteString(``) + return os.WriteFile(path, []byte(output.String()), 0o644) +} + +func fatal(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} diff --git a/cmd/messenger/main.go b/cmd/messenger/main.go new file mode 100644 index 00000000..aefd2bcd --- /dev/null +++ b/cmd/messenger/main.go @@ -0,0 +1,132 @@ +package main + +import ( + "context" + "fmt" + "log/slog" + "net" + "net/http" + "os" + "os/signal" + "syscall" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/config" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/database" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/logger" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/messenger" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/password" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/session" +) + +func main() { + appLogger := logger.New() + if err := run(appLogger, os.Args[1:]); err != nil { + appLogger.Error("server stopped", "error", err) + os.Exit(1) + } +} + +func run(logger *slog.Logger, arguments []string) error { + if len(arguments) == 0 { + return runServer(logger) + } + if len(arguments) == 1 && arguments[0] == "migrate" { + return runMigrations() + } + return fmt.Errorf("usage: messenger [migrate]") +} + +func runServer(logger *slog.Logger) error { + settings, err := config.Load() + if err != nil { + return err + } + instanceID, err := os.Hostname() + if err != nil { + return err + } + pool, err := database.Open(context.Background(), database.Config{ + URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns, + MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle, + HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout, + }) + if err != nil { + return err + } + defer pool.Close() + sessions, err := session.New(settings.CacheURL) + if err != nil { + return err + } + defer func() { _ = sessions.Close() }() + passwords, err := password.New(password.Config{ + MemoryKiB: settings.PasswordMemoryKiB, Iterations: settings.PasswordIterations, + Parallelism: settings.PasswordParallelism, SaltLength: settings.PasswordSaltLength, + KeyLength: settings.PasswordKeyLength, + }) + if err != nil { + return err + } + app := messenger.NewServer( + logger, + postgres.New(pool), + sessions, + passwords, + settings.SessionTTL, + settings.IdempotencyTTL, + settings.RequestTimeout, + instanceID, + settings.SwaggerPort, + ) + server := &http.Server{ + Addr: settings.Address, + Handler: app.Handler(), + ReadHeaderTimeout: settings.HTTPReadHeaderTimeout, + ReadTimeout: settings.HTTPReadTimeout, + WriteTimeout: settings.HTTPWriteTimeout, + IdleTimeout: settings.HTTPIdleTimeout, + } + listener, err := net.Listen("tcp", server.Addr) + if err != nil { + return err + } + serveResult := make(chan error, 1) + go func() { + serveResult <- server.Serve(listener) + }() + logger.Info("server started", "address", server.Addr) + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + select { + case err := <-serveResult: + return err + case <-ctx.Done(): + logger.Info("shutdown requested") + app.SetReady(false) + shutdownCtx, cancel := context.WithTimeout(context.Background(), settings.HTTPShutdownTimeout) + defer cancel() + if err := server.Shutdown(shutdownCtx); err != nil { + _ = server.Close() + return err + } + return nil + } +} + +func runMigrations() error { + settings, err := config.LoadMigration() + if err != nil { + return err + } + pool, err := database.Open(context.Background(), database.Config{ + URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns, + MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle, + HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout, + }) + if err != nil { + return err + } + defer pool.Close() + return database.Migrate(context.Background(), pool, settings.MigrationTimeout) +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..e8288df0 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,259 @@ +services: + postgres: + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] + interval: 5s + timeout: 3s + retries: 20 + cpus: ${POSTGRES_CPUS} + mem_limit: ${POSTGRES_MEMORY} + restart: unless-stopped + + valkey: + image: valkey/valkey:8.1.4-alpine + command: ["valkey-server", "--appendonly", "yes", "--appendfsync", "everysec", "--requirepass", "${VALKEY_PASSWORD}"] + environment: + VALKEY_PASSWORD: ${VALKEY_PASSWORD} + volumes: + - valkey_data:/data + healthcheck: + test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASSWORD\" ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 20 + cpus: ${VALKEY_CPUS} + mem_limit: ${VALKEY_MEMORY} + restart: unless-stopped + + migrate: + image: ${API_IMAGE:-mtusi-messenger:lab3} + command: ["/app/messenger", "migrate"] + environment: + DATABASE_URL: ${DATABASE_URL} + DB_MIN_CONNS: ${DB_MIN_CONNS} + DB_MAX_CONNS: ${DB_MAX_CONNS} + DB_MAX_CONNECTION_AGE_SECONDS: ${DB_MAX_CONNECTION_AGE_SECONDS} + DB_MAX_CONNECTION_IDLE_SECONDS: ${DB_MAX_CONNECTION_IDLE_SECONDS} + DB_HEALTH_CHECK_SECONDS: ${DB_HEALTH_CHECK_SECONDS} + DEPENDENCY_TIMEOUT_SECONDS: ${DEPENDENCY_TIMEOUT_SECONDS} + MIGRATION_TIMEOUT_SECONDS: ${MIGRATION_TIMEOUT_SECONDS} + depends_on: + postgres: + condition: service_healthy + healthcheck: + disable: true + cpus: ${MIGRATE_CPUS} + mem_limit: ${MIGRATE_MEMORY} + restart: "no" + + api: + build: . + image: ${API_IMAGE:-mtusi-messenger:lab3} + environment: + APP_HOST: ${APP_HOST} + APP_PORT: ${APP_PORT} + DATABASE_URL: ${DATABASE_URL} + CACHE_URL: ${CACHE_URL} + SESSION_STORE: ${SESSION_STORE} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS} + DB_MIN_CONNS: ${DB_MIN_CONNS} + DB_MAX_CONNS: ${DB_MAX_CONNS} + DB_MAX_CONNECTION_AGE_SECONDS: ${DB_MAX_CONNECTION_AGE_SECONDS} + DB_MAX_CONNECTION_IDLE_SECONDS: ${DB_MAX_CONNECTION_IDLE_SECONDS} + DB_HEALTH_CHECK_SECONDS: ${DB_HEALTH_CHECK_SECONDS} + DEPENDENCY_TIMEOUT_SECONDS: ${DEPENDENCY_TIMEOUT_SECONDS} + REQUEST_TIMEOUT_SECONDS: ${REQUEST_TIMEOUT_SECONDS} + HTTP_READ_HEADER_TIMEOUT_SECONDS: ${HTTP_READ_HEADER_TIMEOUT_SECONDS} + HTTP_READ_TIMEOUT_SECONDS: ${HTTP_READ_TIMEOUT_SECONDS} + HTTP_WRITE_TIMEOUT_SECONDS: ${HTTP_WRITE_TIMEOUT_SECONDS} + HTTP_IDLE_TIMEOUT_SECONDS: ${HTTP_IDLE_TIMEOUT_SECONDS} + HTTP_SHUTDOWN_TIMEOUT_SECONDS: ${HTTP_SHUTDOWN_TIMEOUT_SECONDS} + PASSWORD_MEMORY_KIB: ${PASSWORD_MEMORY_KIB} + PASSWORD_ITERATIONS: ${PASSWORD_ITERATIONS} + PASSWORD_PARALLELISM: ${PASSWORD_PARALLELISM} + PASSWORD_SALT_LENGTH: ${PASSWORD_SALT_LENGTH} + PASSWORD_KEY_LENGTH: ${PASSWORD_KEY_LENGTH} + SWAGGER_PORT: ${SWAGGER_PORT} + expose: + - "8080" + depends_on: + migrate: + condition: service_completed_successfully + valkey: + condition: service_healthy + healthcheck: + test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:${APP_PORT}/health/ready || exit 1"] + interval: 2s + timeout: 1s + start_period: 5s + retries: 2 + deploy: + replicas: ${API_REPLICAS:-2} + cpus: ${API_CPUS} + mem_limit: ${API_MEMORY} + stop_grace_period: 8s + restart: unless-stopped + + nginx: + build: + context: . + target: loadbalancer + image: ${NGINX_IMAGE:-mtusi-nginx:lab3} + ports: + - "${API_BIND_ADDRESS}:8080" + depends_on: + api: + condition: service_healthy + healthcheck: + test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8081/stub_status || exit 1"] + interval: 2s + timeout: 1s + retries: 10 + cpus: ${NGINX_CPUS} + mem_limit: ${NGINX_MEMORY} + restart: unless-stopped + + swagger: + build: + context: . + target: swagger + image: ${SWAGGER_IMAGE:-mtusi-swagger:lab3} + ports: + - "${SWAGGER_BIND_ADDRESS}:8080" + depends_on: + nginx: + condition: service_healthy + cpus: ${SWAGGER_CPUS} + mem_limit: ${SWAGGER_MEMORY} + restart: unless-stopped + + tests: + profiles: ["tests"] + build: + context: . + target: test + environment: + TEST_DATABASE_URL: ${DATABASE_URL} + TEST_CACHE_URL: ${CACHE_URL} + depends_on: + migrate: + condition: service_completed_successfully + valkey: + condition: service_healthy + cpus: ${TEST_CPUS} + mem_limit: ${TEST_MEMORY} + + experiment: + profiles: ["research"] + image: ${API_IMAGE:-mtusi-messenger:lab3} + command: + - /app/lab3experiment + - -base-url=http://nginx:8080 + - -prometheus-url=http://prometheus:9090 + - -output=/output/lab3-scale.json + volumes: + - ./evidence:/output + depends_on: + nginx: + condition: service_healthy + prometheus: + condition: service_healthy + cpus: ${TEST_CPUS} + mem_limit: ${TEST_MEMORY} + + postgres-exporter: + image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1 + environment: + DATA_SOURCE_URI: postgres:5432/${POSTGRES_DB}?sslmode=disable + DATA_SOURCE_USER: ${POSTGRES_USER} + DATA_SOURCE_PASS: ${POSTGRES_PASSWORD} + PG_EXPORTER_COLLECTION_TIMEOUT: 5s + depends_on: + postgres: + condition: service_healthy + cpus: ${EXPORTER_CPUS} + mem_limit: ${EXPORTER_MEMORY} + restart: unless-stopped + + valkey-exporter: + image: oliver006/redis_exporter:v1.89.0-alpine + environment: + REDIS_ADDR: redis://valkey:6379 + REDIS_USER: default + REDIS_PASSWORD: ${VALKEY_PASSWORD} + depends_on: + valkey: + condition: service_healthy + cpus: ${EXPORTER_CPUS} + mem_limit: ${EXPORTER_MEMORY} + restart: unless-stopped + + nginx-exporter: + image: nginx/nginx-prometheus-exporter:1.5.1 + command: ["--nginx.scrape-uri=http://nginx:8081/stub_status"] + depends_on: + nginx: + condition: service_healthy + cpus: ${EXPORTER_CPUS} + mem_limit: ${EXPORTER_MEMORY} + restart: unless-stopped + + prometheus: + build: + context: . + target: prometheus + image: ${PROMETHEUS_IMAGE:-mtusi-prometheus:lab3} + command: + - --config.file=/etc/prometheus/prometheus.yml + - --storage.tsdb.path=/prometheus + - --storage.tsdb.retention.time=7d + volumes: + - prometheus_data:/prometheus + ports: + - "${PROMETHEUS_BIND_ADDRESS}:9090" + healthcheck: + test: ["CMD", "promtool", "query", "instant", "http://127.0.0.1:9090", "up"] + interval: 5s + timeout: 3s + retries: 20 + depends_on: + - api + - nginx-exporter + - postgres-exporter + - valkey-exporter + cpus: ${PROMETHEUS_CPUS} + mem_limit: ${PROMETHEUS_MEMORY} + restart: unless-stopped + + grafana: + build: + context: . + target: grafana + image: ${GRAFANA_IMAGE:-mtusi-grafana:lab3} + environment: + GF_SECURITY_ADMIN_USER: admin + GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD} + GF_USERS_ALLOW_SIGN_UP: "false" + volumes: + - grafana_data:/var/lib/grafana + ports: + - "${GRAFANA_BIND_ADDRESS}:3000" + depends_on: + - prometheus + cpus: ${GRAFANA_CPUS} + mem_limit: ${GRAFANA_MEMORY} + restart: unless-stopped + +volumes: + postgres_data: + valkey_data: + prometheus_data: + grafana_data: diff --git a/evidence/lab3-rolling-update.json b/evidence/lab3-rolling-update.json new file mode 100644 index 00000000..b85d4dfe --- /dev/null +++ b/evidence/lab3-rolling-update.json @@ -0,0 +1,29 @@ +{ + "label": "rolling-update", + "timestamp": "2026-10-02T09:54:15Z", + "goos": "linux", + "goarch": "arm64", + "duration_seconds": 60.001996238, + "warmup_seconds": 3, + "concurrency": 16, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 10, + "successful_requests": 134894, + "successful_rps": 2248.158535675018, + "errors": {}, + "latency_ms": { + "p50": 6.41, + "p95": 13.12, + "p99": 22.006 + }, + "observed_instances": 5, + "prometheus": { + "api_cpu_cores": 0.4491430158747135, + "api_resident_bytes": 58523648, + "healthy_replicas": 2, + "nginx_rps": 2253.547396138092, + "postgres_connections": 21 + } +} diff --git a/evidence/lab3-scale-results.json b/evidence/lab3-scale-results.json new file mode 100644 index 00000000..79054b92 --- /dev/null +++ b/evidence/lab3-scale-results.json @@ -0,0 +1,2048 @@ +{ + "generated_at": "2026-10-02T11:34:39Z", + "method": "closed-loop HTTP through Nginx; median of three isolated repeats", + "raw": [ + { + "label": "fixed-r1-c32-n1", + "timestamp": "2026-10-02T11:13:52Z", + "duration_seconds": 6.001228475, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 16389, + "successful_rps": 2730.940851239629, + "errors": {}, + "latency_ms": { + "p50": 10.785, + "p95": 18.628, + "p99": 34.675 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42657932632701506, + "api_resident_bytes": 36622336, + "healthy_replicas": 1, + "nginx_rps": 2736.939623016103, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c32-n2", + "timestamp": "2026-10-02T11:20:24Z", + "duration_seconds": 6.001659422, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 15981, + "successful_rps": 2662.7635585950115, + "errors": {}, + "latency_ms": { + "p50": 11.032, + "p95": 19.337, + "p99": 29.03 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4198838725773999, + "api_resident_bytes": 33845248, + "healthy_replicas": 1, + "nginx_rps": 2668.9285202161873, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c32-n3", + "timestamp": "2026-10-02T11:28:48Z", + "duration_seconds": 6.000657423, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 18345, + "successful_rps": 3057.1650248996393, + "errors": {}, + "latency_ms": { + "p50": 10.149, + "p95": 14.379, + "p99": 20.03 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.426619921708535, + "api_resident_bytes": 34050048, + "healthy_replicas": 1, + "nginx_rps": 3063.331015955583, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c64-n1", + "timestamp": "2026-10-02T11:14:03Z", + "duration_seconds": 6.004250916, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 16412, + "successful_rps": 2733.3967599964303, + "errors": {}, + "latency_ms": { + "p50": 21.54, + "p95": 32.991, + "p99": 66.967 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.41970264655075573, + "api_resident_bytes": 35110912, + "healthy_replicas": 1, + "nginx_rps": 2744.555520837264, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c64-n2", + "timestamp": "2026-10-02T11:20:35Z", + "duration_seconds": 6.002174937, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 17519, + "successful_rps": 2918.775307931349, + "errors": {}, + "latency_ms": { + "p50": 20.995, + "p95": 29.421, + "p99": 36.314 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4281781232595221, + "api_resident_bytes": 35639296, + "healthy_replicas": 1, + "nginx_rps": 2930.2711408126356, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c64-n3", + "timestamp": "2026-10-02T11:28:59Z", + "duration_seconds": 6.005813394, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 13572, + "successful_rps": 2259.810471893593, + "errors": {}, + "latency_ms": { + "p50": 25.128, + "p95": 47.38, + "p99": 80.951 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.422923563115954, + "api_resident_bytes": 35971072, + "healthy_replicas": 1, + "nginx_rps": 2271.2993403404435, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r1-c8-n1", + "timestamp": "2026-10-02T11:13:40Z", + "duration_seconds": 6.000388895, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 14474, + "successful_rps": 2412.1769860718337, + "errors": {}, + "latency_ms": { + "p50": 2.981, + "p95": 5.859, + "p99": 9.624 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42497245505618175, + "api_resident_bytes": 30003200, + "healthy_replicas": 1, + "nginx_rps": 2414.1768564485683, + "postgres_connections": 10 + } + }, + { + "label": "fixed-r1-c8-n2", + "timestamp": "2026-10-02T11:20:12Z", + "duration_seconds": 6.000665371, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 9954, + "successful_rps": 1658.816045318185, + "errors": {}, + "latency_ms": { + "p50": 3.644, + "p95": 12.672, + "p99": 20.831 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4366182478133057, + "api_resident_bytes": 30031872, + "healthy_replicas": 1, + "nginx_rps": 1660.8158235524445, + "postgres_connections": 10 + } + }, + { + "label": "fixed-r1-c8-n3", + "timestamp": "2026-10-02T11:28:37Z", + "duration_seconds": 6.000879206, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_requests": 17507, + "successful_rps": 2917.4058332145005, + "errors": {}, + "latency_ms": { + "p50": 2.655, + "p95": 4.172, + "p99": 5.742 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42660415451128814, + "api_resident_bytes": 29147136, + "healthy_replicas": 1, + "nginx_rps": 2919.5721824366283, + "postgres_connections": 10 + } + }, + { + "label": "fixed-r2-c32-n1", + "timestamp": "2026-10-02T11:14:45Z", + "duration_seconds": 6.002851707, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 14197, + "successful_rps": 2365.0425985777233, + "errors": {}, + "latency_ms": { + "p50": 12.159, + "p95": 24.925, + "p99": 44.989 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.514755344763342, + "api_resident_bytes": 62197760, + "healthy_replicas": 2, + "nginx_rps": 2371.039748225451, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c32-n2", + "timestamp": "2026-10-02T11:21:17Z", + "duration_seconds": 6.002198827, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 15653, + "successful_rps": 2607.877621378903, + "errors": {}, + "latency_ms": { + "p50": 11.861, + "p95": 20.571, + "p99": 43.978 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.49981683154262496, + "api_resident_bytes": 62091264, + "healthy_replicas": 2, + "nginx_rps": 2613.7088177469, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c32-n3", + "timestamp": "2026-10-02T11:29:42Z", + "duration_seconds": 6.001806643, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 13823, + "successful_rps": 2303.139841421246, + "errors": {}, + "latency_ms": { + "p50": 12.392, + "p95": 26.935, + "p99": 49.468 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5165111414603097, + "api_resident_bytes": 62054400, + "healthy_replicas": 2, + "nginx_rps": 2309.3046518193205, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c64-n1", + "timestamp": "2026-10-02T11:14:57Z", + "duration_seconds": 6.00093491, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 13188, + "successful_rps": 2197.657564661037, + "errors": {}, + "latency_ms": { + "p50": 28.424, + "p95": 55.062, + "p99": 78.884 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5149197660602521, + "api_resident_bytes": 65277952, + "healthy_replicas": 2, + "nginx_rps": 2209.155773029373, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c64-n2", + "timestamp": "2026-10-02T11:21:29Z", + "duration_seconds": 6.001721202, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 15112, + "successful_rps": 2517.944351524378, + "errors": {}, + "latency_ms": { + "p50": 24.691, + "p95": 49.451, + "p99": 72.134 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5165184945556889, + "api_resident_bytes": 64376832, + "healthy_replicas": 2, + "nginx_rps": 2529.607672369184, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c64-n3", + "timestamp": "2026-10-02T11:29:54Z", + "duration_seconds": 6.004428561, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 11522, + "successful_rps": 1918.9169931736324, + "errors": {}, + "latency_ms": { + "p50": 30.021, + "p95": 67.901, + "p99": 111.126 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5179510370395763, + "api_resident_bytes": 64458752, + "healthy_replicas": 2, + "nginx_rps": 1930.4085113587548, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r2-c8-n1", + "timestamp": "2026-10-02T11:14:34Z", + "duration_seconds": 6.001018858, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 14010, + "successful_rps": 2334.603561747381, + "errors": {}, + "latency_ms": { + "p50": 3.026, + "p95": 6.321, + "p99": 11.643 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.4949159584860614, + "api_resident_bytes": 59621376, + "healthy_replicas": 2, + "nginx_rps": 2336.6032221857085, + "postgres_connections": 16 + } + }, + { + "label": "fixed-r2-c8-n2", + "timestamp": "2026-10-02T11:21:06Z", + "duration_seconds": 6.000636999, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 15770, + "successful_rps": 2628.0543220041563, + "errors": {}, + "latency_ms": { + "p50": 2.814, + "p95": 5.05, + "p99": 7.998 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5032799018676317, + "api_resident_bytes": 58634240, + "healthy_replicas": 2, + "nginx_rps": 2630.2207586678246, + "postgres_connections": 14 + } + }, + { + "label": "fixed-r2-c8-n3", + "timestamp": "2026-10-02T11:29:31Z", + "duration_seconds": 6.000844108, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_requests": 14908, + "successful_rps": 2484.31716133493, + "errors": {}, + "latency_ms": { + "p50": 2.934, + "p95": 5.586, + "p99": 8.643 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.4949303708857487, + "api_resident_bytes": 58679296, + "healthy_replicas": 2, + "nginx_rps": 2486.3168800051753, + "postgres_connections": 15 + } + }, + { + "label": "fixed-r4-c32-n1", + "timestamp": "2026-10-02T11:15:41Z", + "duration_seconds": 6.0029428, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 12085, + "successful_rps": 2013.1792693410307, + "errors": {}, + "latency_ms": { + "p50": 12.487, + "p95": 34.58, + "p99": 68.218 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5613913229358108, + "api_resident_bytes": 369168384, + "healthy_replicas": 4, + "nginx_rps": 2019.1763279836684, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c32-n2", + "timestamp": "2026-10-02T11:22:13Z", + "duration_seconds": 6.003303354, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 11491, + "successful_rps": 1914.112834618552, + "errors": {}, + "latency_ms": { + "p50": 13.572, + "p95": 34.597, + "p99": 69.614 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5763493523435897, + "api_resident_bytes": 474034176, + "healthy_replicas": 4, + "nginx_rps": 1920.2761080395671, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c32-n3", + "timestamp": "2026-10-02T11:30:38Z", + "duration_seconds": 6.00495133, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13473, + "successful_rps": 2243.64849265148, + "errors": {}, + "latency_ms": { + "p50": 12.734, + "p95": 27.775, + "p99": 41.779 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5795217660823239, + "api_resident_bytes": 374923264, + "healthy_replicas": 4, + "nginx_rps": 2249.6435454040557, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c64-n1", + "timestamp": "2026-10-02T11:15:54Z", + "duration_seconds": 6.003245223, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13699, + "successful_rps": 2281.9324367286467, + "errors": {}, + "latency_ms": { + "p50": 24.118, + "p95": 60.359, + "p99": 87.286 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5680261047700327, + "api_resident_bytes": 437239808, + "healthy_replicas": 4, + "nginx_rps": 2293.259643509985, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c64-n2", + "timestamp": "2026-10-02T11:22:25Z", + "duration_seconds": 6.001440594, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 12619, + "successful_rps": 2102.6618196664267, + "errors": {}, + "latency_ms": { + "p50": 26.308, + "p95": 67.889, + "p99": 121.945 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5698631764212044, + "api_resident_bytes": 438214656, + "healthy_replicas": 4, + "nginx_rps": 2114.159059190714, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c64-n3", + "timestamp": "2026-10-02T11:30:50Z", + "duration_seconds": 6.0011934, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 11940, + "successful_rps": 1989.6042677111523, + "errors": {}, + "latency_ms": { + "p50": 26.312, + "p95": 77.357, + "p99": 93.368 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5665539790802274, + "api_resident_bytes": 365211648, + "healthy_replicas": 4, + "nginx_rps": 2001.2686143392746, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c8-n1", + "timestamp": "2026-10-02T11:15:29Z", + "duration_seconds": 6.000357282, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13452, + "successful_rps": 2241.8665035753115, + "errors": {}, + "latency_ms": { + "p50": 3.164, + "p95": 6.348, + "p99": 9.942 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5682994928034352, + "api_resident_bytes": 615251968, + "healthy_replicas": 4, + "nginx_rps": 2244.0330412311605, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c8-n2", + "timestamp": "2026-10-02T11:22:01Z", + "duration_seconds": 6.001054705, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 9964, + "successful_rps": 1660.3747990662587, + "errors": {}, + "latency_ms": { + "p50": 4.097, + "p95": 9.858, + "p99": 18.19 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5632343256567598, + "api_resident_bytes": 587333632, + "healthy_replicas": 4, + "nginx_rps": 1662.2078101852599, + "postgres_connections": 17 + } + }, + { + "label": "fixed-r4-c8-n3", + "timestamp": "2026-10-02T11:30:26Z", + "duration_seconds": 6.00084231, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 15560, + "successful_rps": 2592.9693193354383, + "errors": {}, + "latency_ms": { + "p50": 2.877, + "p95": 5.636, + "p99": 7.487 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5615878281594106, + "api_resident_bytes": 127115264, + "healthy_replicas": 4, + "nginx_rps": 2595.1356818772997, + "postgres_connections": 17 + } + }, + { + "label": "growing-r1-c32-n1", + "timestamp": "2026-10-02T11:16:36Z", + "duration_seconds": 6.003394308, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 17867, + "successful_rps": 2976.1496718932494, + "errors": {}, + "latency_ms": { + "p50": 10.134, + "p95": 14.455, + "p99": 21.076 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42142825711590765, + "api_resident_bytes": 33411072, + "healthy_replicas": 1, + "nginx_rps": 2982.3128519380275, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c32-n2", + "timestamp": "2026-10-02T11:23:08Z", + "duration_seconds": 6.001408915, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 13653, + "successful_rps": 2274.9657944279575, + "errors": {}, + "latency_ms": { + "p50": 11.761, + "p95": 23.312, + "p99": 55.308 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4382304284293216, + "api_resident_bytes": 32964608, + "healthy_replicas": 1, + "nginx_rps": 2281.1310133830466, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c32-n3", + "timestamp": "2026-10-02T11:31:33Z", + "duration_seconds": 6.001539678, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 16912, + "successful_rps": 2817.9435457195686, + "errors": {}, + "latency_ms": { + "p50": 10.857, + "p95": 15.335, + "p99": 21.528 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4265572065422242, + "api_resident_bytes": 31309824, + "healthy_replicas": 1, + "nginx_rps": 2823.775382527763, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c64-n1", + "timestamp": "2026-10-02T11:16:47Z", + "duration_seconds": 6.003172059, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 12270, + "successful_rps": 2043.9194278306127, + "errors": {}, + "latency_ms": { + "p50": 26.551, + "p95": 61.444, + "p99": 102.98 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42644121721649303, + "api_resident_bytes": 34877440, + "healthy_replicas": 1, + "nginx_rps": 2055.5799298638763, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c64-n2", + "timestamp": "2026-10-02T11:23:19Z", + "duration_seconds": 6.002412878, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 9446, + "successful_rps": 1573.7004754573632, + "errors": {}, + "latency_ms": { + "p50": 28.822, + "p95": 96.357, + "p99": 222.405 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.43315913997344313, + "api_resident_bytes": 36052992, + "healthy_replicas": 1, + "nginx_rps": 1585.362452302802, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c64-n3", + "timestamp": "2026-10-02T11:31:45Z", + "duration_seconds": 6.003872011, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15085, + "successful_rps": 2512.545232870055, + "errors": {}, + "latency_ms": { + "p50": 23.32, + "p95": 36.688, + "p99": 58.614 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.4263915012361512, + "api_resident_bytes": 35610624, + "healthy_replicas": 1, + "nginx_rps": 2523.87125712164, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c8-n1", + "timestamp": "2026-10-02T11:16:25Z", + "duration_seconds": 6.000515988, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15415, + "successful_rps": 2568.9457424707057, + "errors": {}, + "latency_ms": { + "p50": 2.94, + "p95": 5.009, + "p99": 7.398 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.42496345399288354, + "api_resident_bytes": 29818880, + "healthy_replicas": 1, + "nginx_rps": 2571.112222824395, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c8-n2", + "timestamp": "2026-10-02T11:22:56Z", + "duration_seconds": 6.000646429, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 16486, + "successful_rps": 2747.370669987529, + "errors": {}, + "latency_ms": { + "p50": 2.63, + "p95": 4.946, + "p99": 9.012 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.41995475484463013, + "api_resident_bytes": 30105600, + "healthy_replicas": 1, + "nginx_rps": 2749.537103246648, + "postgres_connections": 9 + } + }, + { + "label": "growing-r1-c8-n3", + "timestamp": "2026-10-02T11:31:22Z", + "duration_seconds": 6.000635097, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 16276, + "successful_rps": 2712.3795626461506, + "errors": {}, + "latency_ms": { + "p50": 2.735, + "p95": 4.824, + "p99": 8.061 + }, + "observed_instances": 1, + "prometheus": { + "api_cpu_cores": 0.41662256737622116, + "api_resident_bytes": 30113792, + "healthy_replicas": 1, + "nginx_rps": 2714.545999996507, + "postgres_connections": 9 + } + }, + { + "label": "growing-r2-c32-n1", + "timestamp": "2026-10-02T11:17:29Z", + "duration_seconds": 6.00069083, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15746, + "successful_rps": 2624.0312067535733, + "errors": {}, + "latency_ms": { + "p50": 11.67, + "p95": 21.115, + "p99": 31.725 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5066083366271347, + "api_resident_bytes": 62099456, + "healthy_replicas": 2, + "nginx_rps": 2630.030516003105, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c32-n2", + "timestamp": "2026-10-02T11:24:02Z", + "duration_seconds": 6.002365388, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 14830, + "successful_rps": 2470.6926422120705, + "errors": {}, + "latency_ms": { + "p50": 11.857, + "p95": 21.935, + "p99": 34.466 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5031349817586277, + "api_resident_bytes": 62484480, + "healthy_replicas": 2, + "nginx_rps": 2476.85687874355, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c32-n3", + "timestamp": "2026-10-02T11:32:27Z", + "duration_seconds": 6.003222172, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 14108, + "successful_rps": 2350.071277688505, + "errors": {}, + "latency_ms": { + "p50": 12.949, + "p95": 24.988, + "p99": 42.291 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5080604902856491, + "api_resident_bytes": 62377984, + "healthy_replicas": 2, + "nginx_rps": 2356.234634455904, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c64-n1", + "timestamp": "2026-10-02T11:17:41Z", + "duration_seconds": 6.006687453, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 14149, + "successful_rps": 2355.541238113426, + "errors": {}, + "latency_ms": { + "p50": 24.56, + "p95": 57.759, + "p99": 83.657 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5194210660056459, + "api_resident_bytes": 64593920, + "healthy_replicas": 2, + "nginx_rps": 2366.861953654575, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c64-n2", + "timestamp": "2026-10-02T11:24:13Z", + "duration_seconds": 6.014530011, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 12614, + "successful_rps": 2097.2544782269274, + "errors": {}, + "latency_ms": { + "p50": 28.563, + "p95": 57.99, + "p99": 89.912 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5104305730265314, + "api_resident_bytes": 65654784, + "healthy_replicas": 2, + "nginx_rps": 2108.5604322874497, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c64-n3", + "timestamp": "2026-10-02T11:32:39Z", + "duration_seconds": 6.001685646, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 13439, + "successful_rps": 2239.2042490524, + "errors": {}, + "latency_ms": { + "p50": 28.242, + "p95": 55.31, + "p99": 82.937 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5198539517109522, + "api_resident_bytes": 64917504, + "healthy_replicas": 2, + "nginx_rps": 2250.7010191383156, + "postgres_connections": 17 + } + }, + { + "label": "growing-r2-c8-n1", + "timestamp": "2026-10-02T11:17:18Z", + "duration_seconds": 6.000737232, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15448, + "successful_rps": 2574.3503510903274, + "errors": {}, + "latency_ms": { + "p50": 2.815, + "p95": 5.282, + "p99": 10.559 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5049379572633153, + "api_resident_bytes": 58855424, + "healthy_replicas": 2, + "nginx_rps": 2576.516751567035, + "postgres_connections": 15 + } + }, + { + "label": "growing-r2-c8-n2", + "timestamp": "2026-10-02T11:23:50Z", + "duration_seconds": 6.001116768, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15263, + "successful_rps": 2543.3599428338935, + "errors": {}, + "latency_ms": { + "p50": 2.932, + "p95": 5.415, + "p99": 7.726 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5132378054070886, + "api_resident_bytes": 59572224, + "healthy_replicas": 2, + "nginx_rps": 2545.69284194938, + "postgres_connections": 14 + } + }, + { + "label": "growing-r2-c8-n3", + "timestamp": "2026-10-02T11:32:16Z", + "duration_seconds": 6.000150243, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 15324, + "successful_rps": 2553.9360481643857, + "errors": {}, + "latency_ms": { + "p50": 2.891, + "p95": 5.208, + "p99": 8.66 + }, + "observed_instances": 2, + "prometheus": { + "api_cpu_cores": 0.5149871044654106, + "api_resident_bytes": 58920960, + "healthy_replicas": 2, + "nginx_rps": 2555.93599808464, + "postgres_connections": 15 + } + }, + { + "label": "growing-r4-c32-n1", + "timestamp": "2026-10-02T11:18:24Z", + "duration_seconds": 6.001886675, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 14778, + "successful_rps": 2462.2257633679833, + "errors": {}, + "latency_ms": { + "p50": 12.519, + "p95": 20.369, + "p99": 27.493 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5364979671163151, + "api_resident_bytes": 423374848, + "healthy_replicas": 4, + "nginx_rps": 2468.390491561556, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c32-n2", + "timestamp": "2026-10-02T11:24:57Z", + "duration_seconds": 6.003686343, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 10176, + "successful_rps": 1694.9586335176737, + "errors": {}, + "latency_ms": { + "p50": 16.525, + "p95": 33.496, + "p99": 66.101 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5496622927091513, + "api_resident_bytes": 492273664, + "healthy_replicas": 4, + "nginx_rps": 1701.2880781003853, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c32-n3", + "timestamp": "2026-10-02T11:33:22Z", + "duration_seconds": 6.001351862, + "warmup_seconds": 2, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 12781, + "successful_rps": 2129.686826217956, + "errors": {}, + "latency_ms": { + "p50": 13.923, + "p95": 24.14, + "p99": 39.802 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5415446510608212, + "api_resident_bytes": 369414144, + "healthy_replicas": 4, + "nginx_rps": 2135.3522164136693, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c64-n1", + "timestamp": "2026-10-02T11:18:36Z", + "duration_seconds": 6.006332231, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 11165, + "successful_rps": 1858.871532675962, + "errors": {}, + "latency_ms": { + "p50": 30.388, + "p95": 67.422, + "p99": 98.596 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5410956096011533, + "api_resident_bytes": 397496320, + "healthy_replicas": 4, + "nginx_rps": 1870.3594086951864, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c64-n2", + "timestamp": "2026-10-02T11:25:09Z", + "duration_seconds": 6.040332097, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 10524, + "successful_rps": 1742.288309814433, + "errors": {}, + "latency_ms": { + "p50": 26.708, + "p95": 81.814, + "p99": 125.924 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5512941915319329, + "api_resident_bytes": 414949376, + "healthy_replicas": 4, + "nginx_rps": 1753.7115227921217, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c64-n3", + "timestamp": "2026-10-02T11:33:34Z", + "duration_seconds": 6.006393765, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 13269, + "successful_rps": 2209.1458734057874, + "errors": {}, + "latency_ms": { + "p50": 24.93, + "p95": 59.356, + "p99": 84.385 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5410900662121342, + "api_resident_bytes": 376426496, + "healthy_replicas": 4, + "nginx_rps": 2220.6336317345986, + "postgres_connections": 33 + } + }, + { + "label": "growing-r4-c8-n1", + "timestamp": "2026-10-02T11:18:13Z", + "duration_seconds": 6.000820517, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 13215, + "successful_rps": 2202.198843068647, + "errors": {}, + "latency_ms": { + "p50": 3.125, + "p95": 7.049, + "p99": 13.356 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5615898676610925, + "api_resident_bytes": 614596608, + "healthy_replicas": 4, + "nginx_rps": 2204.365213478022, + "postgres_connections": 19 + } + }, + { + "label": "growing-r4-c8-n2", + "timestamp": "2026-10-02T11:24:45Z", + "duration_seconds": 6.000744805, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 12122, + "successful_rps": 2020.082572066652, + "errors": {}, + "latency_ms": { + "p50": 3.487, + "p95": 7.328, + "p99": 13.364 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5749286317134096, + "api_resident_bytes": 605945856, + "healthy_replicas": 4, + "nginx_rps": 2022.2489698093402, + "postgres_connections": 17 + } + }, + { + "label": "growing-r4-c8-n3", + "timestamp": "2026-10-02T11:33:10Z", + "duration_seconds": 6.000546553, + "warmup_seconds": 2, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_requests": 13110, + "successful_rps": 2184.8009817448374, + "errors": {}, + "latency_ms": { + "p50": 3.294, + "p95": 6.916, + "p99": 10.948 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.561615507893219, + "api_resident_bytes": 614023168, + "healthy_replicas": 4, + "nginx_rps": 2186.96745106312, + "postgres_connections": 19 + } + }, + { + "label": "pool-baseline-r4-c64-n1", + "timestamp": "2026-10-02T11:19:09Z", + "duration_seconds": 6.003512334, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 10, + "successful_requests": 12010, + "successful_rps": 2000.495598548728, + "errors": {}, + "latency_ms": { + "p50": 27.976, + "p95": 56.907, + "p99": 100.328 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5463468412356225, + "api_resident_bytes": 612368384, + "healthy_replicas": 4, + "nginx_rps": 2011.9888705137455, + "postgres_connections": 41 + } + }, + { + "label": "pool-baseline-r4-c64-n2", + "timestamp": "2026-10-02T11:25:42Z", + "duration_seconds": 6.006350196, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 10, + "successful_requests": 9128, + "successful_rps": 1519.7249081611826, + "errors": {}, + "latency_ms": { + "p50": 33.678, + "p95": 92.261, + "p99": 165.149 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5460887049483655, + "api_resident_bytes": 586547200, + "healthy_replicas": 4, + "nginx_rps": 1531.0462593613315, + "postgres_connections": 41 + } + }, + { + "label": "pool-baseline-r4-c64-n3", + "timestamp": "2026-10-02T11:34:06Z", + "duration_seconds": 6.002334631, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 10, + "successful_requests": 11511, + "successful_rps": 1917.7537920911027, + "errors": {}, + "latency_ms": { + "p50": 29.998, + "p95": 59.521, + "p99": 101.701 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5247957992430696, + "api_resident_bytes": 610664448, + "healthy_replicas": 4, + "nginx_rps": 1929.415920963171, + "postgres_connections": 41 + } + }, + { + "label": "pool-tuned-r4-c64-n1", + "timestamp": "2026-10-02T11:19:41Z", + "duration_seconds": 6.002401553, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13059, + "successful_rps": 2175.6291852005656, + "errors": {}, + "latency_ms": { + "p50": 22.884, + "p95": 71.837, + "p99": 93.913 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.5681059439110139, + "api_resident_bytes": 441999360, + "healthy_replicas": 4, + "nginx_rps": 2187.124584065627, + "postgres_connections": 17 + } + }, + { + "label": "pool-tuned-r4-c64-n2", + "timestamp": "2026-10-02T11:28:06Z", + "duration_seconds": 6.002283087, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13783, + "successful_rps": 2296.2928939242815, + "errors": {}, + "latency_ms": { + "p50": 23.605, + "p95": 66.761, + "p99": 80.909 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.564785091083459, + "api_resident_bytes": 566091776, + "healthy_replicas": 4, + "nginx_rps": 2307.955122943704, + "postgres_connections": 17 + } + }, + { + "label": "pool-tuned-r4-c64-n3", + "timestamp": "2026-10-02T11:34:38Z", + "duration_seconds": 6.00190551, + "warmup_seconds": 2, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_requests": 13078, + "successful_rps": 2178.974657000223, + "errors": {}, + "latency_ms": { + "p50": 26.267, + "p95": 63.481, + "p99": 75.128 + }, + "observed_instances": 4, + "prometheus": { + "api_cpu_cores": 0.56981903402208, + "api_resident_bytes": 536719360, + "healthy_replicas": 4, + "nginx_rps": 2190.3043921796097, + "postgres_connections": 17 + } + } + ], + "summary": [ + { + "label": "fixed-r1-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_rps_median": 2730.940851239629, + "successful_rps_min": 2662.7635585950115, + "successful_rps_max": 3057.1650248996393, + "latency_p50_median_ms": 10.785, + "latency_p95_median_ms": 18.628, + "latency_p99_median_ms": 29.03, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.42657932632701506, + "api_resident_bytes_median": 34050048, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "fixed-r1-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_rps_median": 2733.3967599964303, + "successful_rps_min": 2259.810471893593, + "successful_rps_max": 2918.775307931349, + "latency_p50_median_ms": 21.54, + "latency_p95_median_ms": 32.991, + "latency_p99_median_ms": 66.967, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.422923563115954, + "api_resident_bytes_median": 35639296, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "fixed-r1-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "2", + "api_memory_per_replica": "1g", + "db_max_connections_per_replica": 16, + "successful_rps_median": 2412.1769860718337, + "successful_rps_min": 1658.816045318185, + "successful_rps_max": 2917.4058332145005, + "latency_p50_median_ms": 2.981, + "latency_p95_median_ms": 5.859, + "latency_p99_median_ms": 9.624, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.42660415451128814, + "api_resident_bytes_median": 30003200, + "postgres_connections_median": 10, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "fixed-r2-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2365.0425985777233, + "successful_rps_min": 2303.139841421246, + "successful_rps_max": 2607.877621378903, + "latency_p50_median_ms": 12.159, + "latency_p95_median_ms": 24.925, + "latency_p99_median_ms": 44.989, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.514755344763342, + "api_resident_bytes_median": 62091264, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "fixed-r2-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2197.657564661037, + "successful_rps_min": 1918.9169931736324, + "successful_rps_max": 2517.944351524378, + "latency_p50_median_ms": 28.424, + "latency_p95_median_ms": 55.062, + "latency_p99_median_ms": 78.884, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.5165184945556889, + "api_resident_bytes_median": 64458752, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "fixed-r2-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "512m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2484.31716133493, + "successful_rps_min": 2334.603561747381, + "successful_rps_max": 2628.0543220041563, + "latency_p50_median_ms": 2.934, + "latency_p95_median_ms": 5.586, + "latency_p99_median_ms": 8.643, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.4949303708857487, + "api_resident_bytes_median": 58679296, + "postgres_connections_median": 15, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "fixed-r4-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_rps_median": 2013.1792693410307, + "successful_rps_min": 1914.112834618552, + "successful_rps_max": 2243.64849265148, + "latency_p50_median_ms": 12.734, + "latency_p95_median_ms": 34.58, + "latency_p99_median_ms": 68.218, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5763493523435897, + "api_resident_bytes_median": 374923264, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "fixed-r4-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_rps_median": 2102.6618196664267, + "successful_rps_min": 1989.6042677111523, + "successful_rps_max": 2281.9324367286467, + "latency_p50_median_ms": 26.308, + "latency_p95_median_ms": 67.889, + "latency_p99_median_ms": 93.368, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5680261047700327, + "api_resident_bytes_median": 437239808, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "fixed-r4-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_rps_median": 2241.8665035753115, + "successful_rps_min": 1660.3747990662587, + "successful_rps_max": 2592.9693193354383, + "latency_p50_median_ms": 3.164, + "latency_p95_median_ms": 6.348, + "latency_p99_median_ms": 9.942, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5632343256567598, + "api_resident_bytes_median": 587333632, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "growing-r1-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2817.9435457195686, + "successful_rps_min": 2274.9657944279575, + "successful_rps_max": 2976.1496718932494, + "latency_p50_median_ms": 10.857, + "latency_p95_median_ms": 15.335, + "latency_p99_median_ms": 21.528, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.4265572065422242, + "api_resident_bytes_median": 32964608, + "postgres_connections_median": 9, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "growing-r1-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2043.9194278306127, + "successful_rps_min": 1573.7004754573632, + "successful_rps_max": 2512.545232870055, + "latency_p50_median_ms": 26.551, + "latency_p95_median_ms": 61.444, + "latency_p99_median_ms": 102.98, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.42644121721649303, + "api_resident_bytes_median": 35610624, + "postgres_connections_median": 9, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "growing-r1-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 1, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2712.3795626461506, + "successful_rps_min": 2568.9457424707057, + "successful_rps_max": 2747.370669987529, + "latency_p50_median_ms": 2.735, + "latency_p95_median_ms": 4.946, + "latency_p99_median_ms": 8.061, + "total_errors": 0, + "observed_instances_min": 1, + "api_cpu_cores_median": 0.41995475484463013, + "api_resident_bytes_median": 30105600, + "postgres_connections_median": 9, + "prometheus_healthy_replicas_min": 1 + }, + { + "label": "growing-r2-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2470.6926422120705, + "successful_rps_min": 2350.071277688505, + "successful_rps_max": 2624.0312067535733, + "latency_p50_median_ms": 11.857, + "latency_p95_median_ms": 21.935, + "latency_p99_median_ms": 34.466, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.5066083366271347, + "api_resident_bytes_median": 62377984, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "growing-r2-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2239.2042490524, + "successful_rps_min": 2097.2544782269274, + "successful_rps_max": 2355.541238113426, + "latency_p50_median_ms": 28.242, + "latency_p95_median_ms": 57.759, + "latency_p99_median_ms": 83.657, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.5194210660056459, + "api_resident_bytes_median": 64917504, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "growing-r2-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 2, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2553.9360481643857, + "successful_rps_min": 2543.3599428338935, + "successful_rps_max": 2574.3503510903274, + "latency_p50_median_ms": 2.891, + "latency_p95_median_ms": 5.282, + "latency_p99_median_ms": 8.66, + "total_errors": 0, + "observed_instances_min": 2, + "api_cpu_cores_median": 0.5132378054070886, + "api_resident_bytes_median": 58920960, + "postgres_connections_median": 15, + "prometheus_healthy_replicas_min": 2 + }, + { + "label": "growing-r4-c32", + "repeats": 3, + "concurrency": 32, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2129.686826217956, + "successful_rps_min": 1694.9586335176737, + "successful_rps_max": 2462.2257633679833, + "latency_p50_median_ms": 13.923, + "latency_p95_median_ms": 24.14, + "latency_p99_median_ms": 39.802, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5415446510608212, + "api_resident_bytes_median": 423374848, + "postgres_connections_median": 33, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "growing-r4-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 1858.871532675962, + "successful_rps_min": 1742.288309814433, + "successful_rps_max": 2209.1458734057874, + "latency_p50_median_ms": 26.708, + "latency_p95_median_ms": 67.422, + "latency_p99_median_ms": 98.596, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5410956096011533, + "api_resident_bytes_median": 397496320, + "postgres_connections_median": 33, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "growing-r4-c8", + "repeats": 3, + "concurrency": 8, + "replicas": 4, + "api_cpus_per_replica": "1", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 8, + "successful_rps_median": 2184.8009817448374, + "successful_rps_min": 2020.082572066652, + "successful_rps_max": 2202.198843068647, + "latency_p50_median_ms": 3.294, + "latency_p95_median_ms": 7.049, + "latency_p99_median_ms": 13.356, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.561615507893219, + "api_resident_bytes_median": 614023168, + "postgres_connections_median": 19, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "pool-baseline-r4-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 10, + "successful_rps_median": 1917.7537920911027, + "successful_rps_min": 1519.7249081611826, + "successful_rps_max": 2000.495598548728, + "latency_p50_median_ms": 29.998, + "latency_p95_median_ms": 59.521, + "latency_p99_median_ms": 101.701, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5460887049483655, + "api_resident_bytes_median": 610664448, + "postgres_connections_median": 41, + "prometheus_healthy_replicas_min": 4 + }, + { + "label": "pool-tuned-r4-c64", + "repeats": 3, + "concurrency": 64, + "replicas": 4, + "api_cpus_per_replica": "0.5", + "api_memory_per_replica": "256m", + "db_max_connections_per_replica": 4, + "successful_rps_median": 2178.974657000223, + "successful_rps_min": 2175.6291852005656, + "successful_rps_max": 2296.2928939242815, + "latency_p50_median_ms": 23.605, + "latency_p95_median_ms": 66.761, + "latency_p99_median_ms": 80.909, + "total_errors": 0, + "observed_instances_min": 4, + "api_cpu_cores_median": 0.5681059439110139, + "api_resident_bytes_median": 536719360, + "postgres_connections_median": 17, + "prometheus_healthy_replicas_min": 4 + } + ] +} diff --git a/evidence/lab3-scale-rps.svg b/evidence/lab3-scale-rps.svg new file mode 100644 index 00000000..71244973 --- /dev/null +++ b/evidence/lab3-scale-rps.svg @@ -0,0 +1 @@ +Median successful RPS by replica count06201240186024803100124API replicasfixed, c=32fixed, c=64fixed, c=8growing, c=32growing, c=64growing, c=8 \ No newline at end of file diff --git a/evidence/lab3-verification.txt b/evidence/lab3-verification.txt new file mode 100644 index 00000000..97710ec8 --- /dev/null +++ b/evidence/lab3-verification.txt @@ -0,0 +1,51 @@ +Lab 3 verification +Date: 2026-10-02 +Environment: Docker linux/arm64, Go 1.26.5 + +Contract and build +- make check: PASS +- docker compose --env-file .env config --quiet: PASS +- go test ./cmd/lab3experiment: PASS +- go test ./internal/messenger ./internal/model ./internal/password: PASS + +Integration +- docker compose --profile tests run --rm --build tests: PASS +- PostgreSQL concurrent idempotency: 8 calls through two Store instances, one message ID and one row +- black-box replica idempotency: 16 concurrent calls through Nginx, at least two instance IDs, one message ID and one history item +- PostgreSQL and Valkey stop/recovery: PASS +- persistence after restarting API, PostgreSQL and Valkey: PASS; the original session and data remained valid +- public smoke through Nginx: PASS + +API replica failover +- run 1: 2 instances observed, 131 successful writes, 0 transition errors, first recovered write after 1.353 s +- run 2: 2 instances observed, 125 successful writes, 1 transition error, first recovered write after 1.442 s +- confirmed messages remained readable and idempotent retries did not create duplicates + +Rolling update under Go load +- duration: 60.002 s +- concurrency: 16 +- successful requests: 134894 +- successful RPS: 2248.16 +- errors: 0 +- latency p50/p95/p99: 6.41/13.12/22.01 ms +- healthy API replicas after update: 2 + +Resource boundary +- 4 API replicas with 128 MiB each restarted while Argon2id processed registration +- idle process memory was about 71 MiB; Argon2id memory parameter was 64 MiB +- the fixed-budget experiment therefore uses 256 MiB per replica as its minimum + +Scale experiment +- command: DURATION=6s WARMUP=2s REPEATS=3 CONCURRENCIES='8 32 64' scripts/run-scale-experiment.sh +- configurations: fixed total API resources and growing per-replica resources, 1/2/4 replicas +- completed points: 60; HTTP errors: 0; every point observed all configured replicas +- CPU and Nginx RPS use counter deltas read immediately before and after measured load +- maximum difference between client RPS and Nginx counter RPS across 60 points: 0.75% +- reducing the pool from 10 to 4 connections per replica changed median RPS from 1918 to 2179 and PostgreSQL connections from 41 to 17 +- combined sanitized raw data and median aggregates: evidence/lab3-scale-results.json +- comparison chart: evidence/lab3-scale-rps.svg + +Configuration scope +- API receives only its application settings and PostgreSQL/Valkey connection URLs +- migrate receives only PostgreSQL pool, dependency and migration settings +- neither container receives the Grafana password or separate source passwords of unrelated services diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..68b77ae1 --- /dev/null +++ b/go.mod @@ -0,0 +1,43 @@ +module gitea.privcloud.tech/dizi/mtusi2026lab3 + +go 1.26.0 + +require ( + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.11.0 + github.com/prometheus/client_golang v1.23.2 + github.com/spf13/viper v1.21.0 + github.com/stretchr/testify v1.11.1 + github.com/valkey-io/valkey-go v1.0.78 + golang.org/x/crypto v0.57.0 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.9.0 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect + github.com/sagikazarmark/locafero v0.11.0 // indirect + github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect + github.com/spf13/afero v1.15.0 // indirect + github.com/spf13/cast v1.10.0 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + google.golang.org/protobuf v1.36.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..e55a574b --- /dev/null +++ b/go.sum @@ -0,0 +1,96 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= +github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= +github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/onsi/gomega v1.42.1 h1:iN1rCUX+44NZ1Dc97MPoeFYbFR0vh8zxoxMFwKdyZ6I= +github.com/onsi/gomega v1.42.1/go.mod h1:REff/hsDsodHoKlWsP2mAPhu1+5/6hVYNf9rIEBpeSg= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= +github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= +github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U= +github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I= +github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg= +github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= +github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= +github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/valkey-io/valkey-go v1.0.78 h1:1Ce8hRaaNXpKfqmqZQRhnbVh1eW8MGR873pdXVvE4y8= +github.com/valkey-io/valkey-go v1.0.78/go.mod h1:gvC/r2m3eW4Hbj0YnjogTzNtFdPSM/D+NCqen+5OABM= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..44571418 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,262 @@ +package config + +import ( + "errors" + "fmt" + "net" + "strconv" + "strings" + "time" + + "github.com/spf13/viper" +) + +type Config struct { + Address string + DatabaseURL string + CacheURL string + SessionTTL time.Duration + IdempotencyTTL time.Duration + DBMaxConns int32 + DBMinConns int32 + DBMaxConnectionAge time.Duration + DBMaxConnectionIdle time.Duration + DBHealthCheckPeriod time.Duration + DependencyTimeout time.Duration + RequestTimeout time.Duration + HTTPReadHeaderTimeout time.Duration + HTTPReadTimeout time.Duration + HTTPWriteTimeout time.Duration + HTTPIdleTimeout time.Duration + HTTPShutdownTimeout time.Duration + PasswordMemoryKiB uint32 + PasswordIterations uint32 + PasswordParallelism uint8 + PasswordSaltLength uint32 + PasswordKeyLength uint32 + SwaggerPort string +} + +type MigrationConfig struct { + DatabaseURL string + DBMaxConns int32 + DBMinConns int32 + DBMaxConnectionAge time.Duration + DBMaxConnectionIdle time.Duration + DBHealthCheckPeriod time.Duration + DependencyTimeout time.Duration + MigrationTimeout time.Duration +} + +type databaseConfig struct { + url string + maxConns int + minConns int + maxConnectionAge time.Duration + maxConnectionIdle time.Duration + healthCheckPeriod time.Duration + dependencyTimeout time.Duration +} + +func Load() (Config, error) { + values := viper.New() + values.AutomaticEnv() + + host, err := requiredString(values, "APP_HOST") + if err != nil { + return Config{}, err + } + port, err := requiredPort(values, "APP_PORT") + if err != nil { + return Config{}, err + } + database, err := loadDatabaseConfig(values) + if err != nil { + return Config{}, err + } + cacheURL, err := requiredString(values, "CACHE_URL") + if err != nil { + return Config{}, err + } + store, err := requiredString(values, "SESSION_STORE") + if err != nil { + return Config{}, err + } + if store != "valkey" { + return Config{}, errors.New("SESSION_STORE must be valkey") + } + + sessionTTL, err := seconds(values, "SESSION_TTL_SECONDS", 1, 86400*30) + if err != nil { + return Config{}, err + } + idempotencyTTL, err := seconds(values, "IDEMPOTENCY_TTL_SECONDS", 86400, 86400*30) + if err != nil { + return Config{}, err + } + requestTimeout, err := seconds(values, "REQUEST_TIMEOUT_SECONDS", 1, 300) + if err != nil { + return Config{}, err + } + readHeaderTimeout, err := seconds(values, "HTTP_READ_HEADER_TIMEOUT_SECONDS", 1, 300) + if err != nil { + return Config{}, err + } + readTimeout, err := seconds(values, "HTTP_READ_TIMEOUT_SECONDS", 1, 300) + if err != nil { + return Config{}, err + } + writeTimeout, err := seconds(values, "HTTP_WRITE_TIMEOUT_SECONDS", 1, 300) + if err != nil { + return Config{}, err + } + idleTimeout, err := seconds(values, "HTTP_IDLE_TIMEOUT_SECONDS", 1, 3600) + if err != nil { + return Config{}, err + } + shutdownTimeout, err := seconds(values, "HTTP_SHUTDOWN_TIMEOUT_SECONDS", 1, 300) + if err != nil { + return Config{}, err + } + passwordMemory, err := integer(values, "PASSWORD_MEMORY_KIB", 8*1024, 1024*1024) + if err != nil { + return Config{}, err + } + passwordIterations, err := integer(values, "PASSWORD_ITERATIONS", 1, 20) + if err != nil { + return Config{}, err + } + passwordParallelism, err := integer(values, "PASSWORD_PARALLELISM", 1, 255) + if err != nil { + return Config{}, err + } + passwordSaltLength, err := integer(values, "PASSWORD_SALT_LENGTH", 8, 1024) + if err != nil { + return Config{}, err + } + passwordKeyLength, err := integer(values, "PASSWORD_KEY_LENGTH", 16, 1024) + if err != nil { + return Config{}, err + } + swaggerPort, err := requiredPort(values, "SWAGGER_PORT") + if err != nil { + return Config{}, err + } + + return Config{ + Address: net.JoinHostPort(host, port), DatabaseURL: database.url, CacheURL: cacheURL, + SessionTTL: sessionTTL, IdempotencyTTL: idempotencyTTL, + DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns), + DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle, + DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout, + RequestTimeout: requestTimeout, + HTTPReadHeaderTimeout: readHeaderTimeout, HTTPReadTimeout: readTimeout, + HTTPWriteTimeout: writeTimeout, HTTPIdleTimeout: idleTimeout, + HTTPShutdownTimeout: shutdownTimeout, PasswordMemoryKiB: uint32(passwordMemory), + PasswordIterations: uint32(passwordIterations), PasswordParallelism: uint8(passwordParallelism), + PasswordSaltLength: uint32(passwordSaltLength), PasswordKeyLength: uint32(passwordKeyLength), + SwaggerPort: swaggerPort, + }, nil +} + +func LoadMigration() (MigrationConfig, error) { + values := viper.New() + values.AutomaticEnv() + database, err := loadDatabaseConfig(values) + if err != nil { + return MigrationConfig{}, err + } + migrationTimeout, err := seconds(values, "MIGRATION_TIMEOUT_SECONDS", 1, 600) + if err != nil { + return MigrationConfig{}, err + } + return MigrationConfig{ + DatabaseURL: database.url, DBMaxConns: int32(database.maxConns), DBMinConns: int32(database.minConns), + DBMaxConnectionAge: database.maxConnectionAge, DBMaxConnectionIdle: database.maxConnectionIdle, + DBHealthCheckPeriod: database.healthCheckPeriod, DependencyTimeout: database.dependencyTimeout, + MigrationTimeout: migrationTimeout, + }, nil +} + +func loadDatabaseConfig(values *viper.Viper) (databaseConfig, error) { + databaseURL, err := requiredString(values, "DATABASE_URL") + if err != nil { + return databaseConfig{}, err + } + maxConns, err := integer(values, "DB_MAX_CONNS", 1, 100) + if err != nil { + return databaseConfig{}, err + } + minConns, err := integer(values, "DB_MIN_CONNS", 0, maxConns) + if err != nil { + return databaseConfig{}, err + } + maxConnectionAge, err := seconds(values, "DB_MAX_CONNECTION_AGE_SECONDS", 1, 86400) + if err != nil { + return databaseConfig{}, err + } + maxConnectionIdle, err := seconds(values, "DB_MAX_CONNECTION_IDLE_SECONDS", 1, 86400) + if err != nil { + return databaseConfig{}, err + } + healthCheckPeriod, err := seconds(values, "DB_HEALTH_CHECK_SECONDS", 1, 3600) + if err != nil { + return databaseConfig{}, err + } + dependencyTimeout, err := seconds(values, "DEPENDENCY_TIMEOUT_SECONDS", 1, 60) + if err != nil { + return databaseConfig{}, err + } + return databaseConfig{ + url: databaseURL, maxConns: maxConns, minConns: minConns, + maxConnectionAge: maxConnectionAge, maxConnectionIdle: maxConnectionIdle, + healthCheckPeriod: healthCheckPeriod, dependencyTimeout: dependencyTimeout, + }, nil +} + +func requiredString(values *viper.Viper, name string) (string, error) { + value := strings.TrimSpace(values.GetString(name)) + if value == "" { + return "", fmt.Errorf("%s is required", name) + } + return value, nil +} + +func requiredPort(values *viper.Viper, name string) (string, error) { + value, err := requiredString(values, name) + if err != nil { + return "", err + } + if _, err := integerValue(value, 1, 65535); err != nil { + return "", fmt.Errorf("%s: %w", name, err) + } + return value, nil +} + +func seconds(values *viper.Viper, name string, minimum, maximum int) (time.Duration, error) { + value, err := integer(values, name, minimum, maximum) + if err != nil { + return 0, err + } + return time.Duration(value) * time.Second, nil +} + +func integer(values *viper.Viper, name string, minimum, maximum int) (int, error) { + value, err := requiredString(values, name) + if err != nil { + return 0, err + } + parsed, err := integerValue(value, minimum, maximum) + if err != nil { + return 0, fmt.Errorf("%s: %w", name, err) + } + return parsed, nil +} + +func integerValue(value string, minimum, maximum int) (int, error) { + parsed, err := strconv.Atoi(value) + if err != nil || parsed < minimum || parsed > maximum { + return 0, fmt.Errorf("must be between %d and %d", minimum, maximum) + } + return parsed, nil +} diff --git a/internal/database/database.go b/internal/database/database.go new file mode 100644 index 00000000..a02ff7ce --- /dev/null +++ b/internal/database/database.go @@ -0,0 +1,93 @@ +package database + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/migrations" +) + +var ( + errURLRequired = errors.New("database URL is required") + errMinConnections = errors.New("minimum database connections must not be negative") + errMaxConnections = errors.New("maximum database connections must be positive") + errConnectionRange = errors.New("minimum database connections exceed maximum") + errMaxConnectionAge = errors.New("maximum database connection age must be positive") + errMaxConnectionIdle = errors.New("maximum database connection idle time must be positive") + errHealthCheckPeriod = errors.New("database health check period must be positive") + errConnectionTimeout = errors.New("database connection timeout must be positive") +) + +type Config struct { + URL string + MinConnections int32 + MaxConnections int32 + MaxConnectionAge time.Duration + MaxConnectionIdle time.Duration + HealthCheckPeriod time.Duration + ConnectTimeout time.Duration +} + +func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) { + if err := validateConfig(config); err != nil { + return nil, err + } + poolConfig, err := pgxpool.ParseConfig(config.URL) + if err != nil { + return nil, fmt.Errorf("parse database configuration: %w", err) + } + poolConfig.MinConns = config.MinConnections + poolConfig.MaxConns = config.MaxConnections + poolConfig.MaxConnLifetime = config.MaxConnectionAge + poolConfig.MaxConnIdleTime = config.MaxConnectionIdle + poolConfig.HealthCheckPeriod = config.HealthCheckPeriod + + connectCtx, cancel := context.WithTimeout(ctx, config.ConnectTimeout) + defer cancel() + pool, err := pgxpool.NewWithConfig(connectCtx, poolConfig) + if err != nil { + return nil, fmt.Errorf("create database pool: %w", err) + } + if err := pool.Ping(connectCtx); err != nil { + pool.Close() + return nil, fmt.Errorf("ping database: %w", err) + } + + return pool, nil +} + +func Migrate(ctx context.Context, pool *pgxpool.Pool, timeout time.Duration) error { + if timeout <= 0 { + return errors.New("database migration timeout must be positive") + } + migrationCtx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + return applyMigrations(migrationCtx, pool, migrations.FS) +} + +func validateConfig(config Config) error { + switch { + case config.URL == "": + return errURLRequired + case config.MinConnections < 0: + return errMinConnections + case config.MaxConnections < 1: + return errMaxConnections + case config.MinConnections > config.MaxConnections: + return errConnectionRange + case config.MaxConnectionAge <= 0: + return errMaxConnectionAge + case config.MaxConnectionIdle <= 0: + return errMaxConnectionIdle + case config.HealthCheckPeriod <= 0: + return errHealthCheckPeriod + case config.ConnectTimeout <= 0: + return errConnectionTimeout + default: + return nil + } +} diff --git a/internal/database/migrations.go b/internal/database/migrations.go new file mode 100644 index 00000000..095c93d1 --- /dev/null +++ b/internal/database/migrations.go @@ -0,0 +1,150 @@ +package database + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io/fs" + "path/filepath" + "regexp" + "sort" + "strconv" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" +) + +const ( + migrationLockID int64 = 0x4d54555349324c42 + migrationUnlockTimeout = 5 * time.Second +) + +var migrationNamePattern = regexp.MustCompile(`^(\d+)_([a-z0-9_]+)\.sql$`) + +type migration struct { + version int64 + name string + checksum string + sql string +} + +func applyMigrations(ctx context.Context, pool *pgxpool.Pool, migrationFS fs.FS) (returnErr error) { + migrations, err := readMigrations(migrationFS) + if err != nil { + return err + } + connection, err := pool.Acquire(ctx) + if err != nil { + return fmt.Errorf("acquire connection for migrations: %w", err) + } + defer connection.Release() + if _, err := connection.Exec(ctx, "SELECT pg_advisory_lock($1)", migrationLockID); err != nil { + return fmt.Errorf("lock migrations: %w", err) + } + defer func() { + unlockCtx, cancel := context.WithTimeout(context.Background(), migrationUnlockTimeout) + defer cancel() + if _, err := connection.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", migrationLockID); err != nil { + _ = connection.Conn().Close(unlockCtx) + if returnErr == nil { + returnErr = fmt.Errorf("unlock migrations: %w", err) + } + } + }() + + if _, err := connection.Exec(ctx, ` +CREATE TABLE IF NOT EXISTS schema_migrations ( + version BIGINT PRIMARY KEY, + name TEXT NOT NULL, + checksum TEXT NOT NULL, + applied_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp() +)`); err != nil { + return fmt.Errorf("create migration history: %w", err) + } + for _, migration := range migrations { + if err := applyMigration(ctx, connection, migration); err != nil { + return err + } + } + return nil +} + +func readMigrations(migrationFS fs.FS) ([]migration, error) { + entries, err := fs.ReadDir(migrationFS, ".") + if err != nil { + return nil, fmt.Errorf("read embedded migrations: %w", err) + } + migrations := make([]migration, 0, len(entries)) + versions := make(map[int64]string) + for _, entry := range entries { + if entry.IsDir() || filepath.Ext(entry.Name()) != ".sql" { + continue + } + matches := migrationNamePattern.FindStringSubmatch(entry.Name()) + if matches == nil { + return nil, fmt.Errorf("invalid migration filename %q", entry.Name()) + } + version, err := strconv.ParseInt(matches[1], 10, 64) + if err != nil || version < 1 { + return nil, fmt.Errorf("invalid migration version in %q", entry.Name()) + } + if previous, exists := versions[version]; exists { + return nil, fmt.Errorf("migration version %d is used by %q and %q", version, previous, entry.Name()) + } + data, err := fs.ReadFile(migrationFS, entry.Name()) + if err != nil { + return nil, fmt.Errorf("read migration %q: %w", entry.Name(), err) + } + hash := sha256.Sum256(data) + versions[version] = entry.Name() + migrations = append(migrations, migration{ + version: version, + name: entry.Name(), + checksum: hex.EncodeToString(hash[:]), + sql: string(data), + }) + } + if len(migrations) == 0 { + return nil, errors.New("no database migrations found") + } + sort.Slice(migrations, func(i, j int) bool { + return migrations[i].version < migrations[j].version + }) + return migrations, nil +} + +func applyMigration(ctx context.Context, connection *pgxpool.Conn, migration migration) error { + var checksum string + err := connection.QueryRow(ctx, "SELECT checksum FROM schema_migrations WHERE version = $1", migration.version).Scan(&checksum) + switch { + case err == nil && checksum != migration.checksum: + return fmt.Errorf("migration %q checksum does not match applied version", migration.name) + case err == nil: + return nil + case !errors.Is(err, pgx.ErrNoRows): + return fmt.Errorf("read migration %q state: %w", migration.name, err) + } + + transaction, err := connection.Begin(ctx) + if err != nil { + return fmt.Errorf("begin migration %q: %w", migration.name, err) + } + defer func() { + _ = transaction.Rollback(context.WithoutCancel(ctx)) + }() + if _, err := transaction.Exec(ctx, migration.sql, pgx.QueryExecModeSimpleProtocol); err != nil { + return fmt.Errorf("apply migration %q: %w", migration.name, err) + } + if _, err := transaction.Exec(ctx, ` +INSERT INTO schema_migrations (version, name, checksum) +VALUES ($1, $2, $3)`, migration.version, migration.name, migration.checksum); err != nil { + return fmt.Errorf("record migration %q: %w", migration.name, err) + } + if err := transaction.Commit(ctx); err != nil { + return fmt.Errorf("commit migration %q: %w", migration.name, err) + } + return nil +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 00000000..ab813660 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,10 @@ +package logger + +import ( + "log/slog" + "os" +) + +func New() *slog.Logger { + return slog.New(slog.NewJSONHandler(os.Stdout, nil)) +} diff --git a/internal/messenger/auth.go b/internal/messenger/auth.go new file mode 100644 index 00000000..5c6eb576 --- /dev/null +++ b/internal/messenger/auth.go @@ -0,0 +1,113 @@ +package messenger + +import ( + "errors" + "io" + "net/http" + "strings" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/session" +) + +func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodPost { + return methodNotAllowed(w, http.MethodPost) + } + body, err := io.ReadAll(io.LimitReader(r.Body, 1)) + if err != nil || len(body) != 0 { + return invalidRequest() + } + username, passwordValue, ok := singleBasicAuth(r) + if !ok { + return unauthorized() + } + credentials, err := s.messengerStore.UserByUsername(r.Context(), username) + if errors.Is(err, postgres.ErrNotFound) { + if _, verifyErr := s.passwords.Verify(passwordValue, s.passwords.DummyHash()); verifyErr != nil { + return internalError() + } + return unauthorized() + } + if err != nil { + return unavailable() + } + verified, err := s.passwords.Verify(passwordValue, credentials.PasswordHash) + if err != nil { + return internalError() + } + if !verified { + return unauthorized() + } + token, expiresAt, err := s.sessions.Create(r.Context(), credentials.ID, s.sessionTTL) + if err != nil { + return unavailable() + } + writeJSON(w, http.StatusCreated, struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User User `json:"user"` + }{token, "Bearer", model.FormatTime(expiresAt), credentials.User}) + return nil +} + +func singleBasicAuth(r *http.Request) (string, string, bool) { + if len(r.Header.Values("Authorization")) != 1 { + return "", "", false + } + username, passwordValue, ok := r.BasicAuth() + return username, passwordValue, ok && username != "" +} + +func (s *Server) revokeSession(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodDelete { + return methodNotAllowed(w, http.MethodDelete) + } + _, token, authErr := s.authenticate(r) + if authErr != nil { + return authErr + } + if err := s.sessions.Delete(r.Context(), token); errors.Is(err, session.ErrNotFound) { + return unauthorized() + } else if err != nil { + return unavailable() + } + w.WriteHeader(http.StatusNoContent) + return nil +} + +func (s *Server) authenticate(r *http.Request) (User, string, *apiError) { + values := r.Header.Values("Authorization") + if len(values) != 1 { + return User{}, "", unauthorized() + } + scheme, token, found := strings.Cut(values[0], " ") + if !found || !strings.EqualFold(scheme, "Bearer") || token == "" || strings.ContainsAny(token, " \t\r\n") { + return User{}, "", unauthorized() + } + userID, err := s.sessions.UserID(r.Context(), token) + if errors.Is(err, session.ErrNotFound) || errors.Is(err, session.ErrInvalidToken) { + return User{}, "", unauthorized() + } + if err != nil { + return User{}, "", unavailable() + } + user, err := s.messengerStore.UserByID(r.Context(), userID) + if errors.Is(err, postgres.ErrNotFound) { + _ = s.sessions.Delete(r.Context(), token) + return User{}, "", unauthorized() + } + if err != nil { + return User{}, "", unavailable() + } + return user, token, nil +} + +func authenticationChallenge(r *http.Request) string { + if r.URL.Path == "/api/v1/auth/sessions" { + return `Basic realm="messenger", charset="UTF-8"` + } + return `Bearer realm="messenger"` +} diff --git a/internal/messenger/errors.go b/internal/messenger/errors.go new file mode 100644 index 00000000..88d29705 --- /dev/null +++ b/internal/messenger/errors.go @@ -0,0 +1,60 @@ +package messenger + +import ( + "encoding/json" + "net/http" +) + +const ( + errorCodeInvalidRequest = "invalid_request" + errorCodeUnauthorized = "unauthorized" + errorCodeNotFound = "not_found" + errorCodeInternal = "internal_error" + errorCodeUnsupportedMediaType = "unsupported_media_type" + errorCodePayloadTooLarge = "payload_too_large" + errorCodeUnavailable = "unavailable" + errorCodeConflict = "conflict" +) + +type apiError struct { + status int + code string + message string +} + +func methodNotAllowed(w http.ResponseWriter, allowed string) *apiError { + w.Header().Set("Allow", allowed) + return &apiError{http.StatusMethodNotAllowed, errorCodeInvalidRequest, "method not allowed"} +} + +func invalidRequest() *apiError { + return &apiError{http.StatusBadRequest, errorCodeInvalidRequest, "invalid request"} +} + +func unauthorized() *apiError { + return &apiError{http.StatusUnauthorized, errorCodeUnauthorized, "authentication required"} +} + +func notFound() *apiError { + return &apiError{http.StatusNotFound, errorCodeNotFound, "resource not found"} +} + +func internalError() *apiError { + return &apiError{http.StatusInternalServerError, errorCodeInternal, "internal server error"} +} + +func unavailable() *apiError { + return &apiError{http.StatusServiceUnavailable, errorCodeUnavailable, "service unavailable"} +} + +func writeError(w http.ResponseWriter, requestID string, err *apiError) { + writeJSON(w, err.status, map[string]any{"error": map[string]string{ + "code": err.code, "message": err.message, "request_id": requestID, + }}) +} + +func writeJSON(w http.ResponseWriter, status int, value any) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(value) +} diff --git a/internal/messenger/handlers.go b/internal/messenger/handlers.go new file mode 100644 index 00000000..e9ad0fdc --- /dev/null +++ b/internal/messenger/handlers.go @@ -0,0 +1,273 @@ +package messenger + +import ( + "context" + "errors" + "net/http" + "regexp" + "strings" + "time" + "unicode/utf8" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres" +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +func (s *Server) live(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet) + } + writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) + return nil +} + +func (s *Server) readiness(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet) + } + if !s.ready.Load() { + return unavailable() + } + ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) + defer cancel() + if err := s.messengerStore.Ping(ctx); err != nil { + return unavailable() + } + if err := s.sessions.Ping(ctx); err != nil { + return unavailable() + } + writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) + return nil +} + +func (s *Server) users(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodPost { + return methodNotAllowed(w, http.MethodPost) + } + var input struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` + } + if err := readJSON(w, r, &input); err != nil { + return err + } + if !usernamePattern.MatchString(input.Username) || !validText(input.DisplayName, 100) || !validPassword(input.Password) { + return invalidRequest() + } + passwordHash, err := s.passwords.Hash(input.Password) + if err != nil { + return internalError() + } + user, err := s.messengerStore.CreateUser(r.Context(), input.Username, input.DisplayName, passwordHash) + if errors.Is(err, postgres.ErrConflict) { + return &apiError{http.StatusConflict, errorCodeConflict, "username already exists"} + } + if err != nil { + return unavailable() + } + writeJSON(w, http.StatusCreated, user) + return nil +} + +func validPassword(value string) bool { + length := utf8.RuneCountInString(value) + return utf8.ValidString(value) && strings.TrimSpace(value) != "" && length >= 12 && length <= 128 +} + +func (s *Server) me(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet) + } + user, _, err := s.authenticate(r) + if err != nil { + return err + } + writeJSON(w, http.StatusOK, user) + return nil +} + +func (s *Server) chats(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodPost && r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet+", "+http.MethodPost) + } + user, _, authErr := s.authenticate(r) + if authErr != nil { + return authErr + } + if r.Method == http.MethodGet { + return s.listChats(w, r, user) + } + var input struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + } + if err := readJSON(w, r, &input); err != nil { + return err + } + if !validText(input.Title, 100) || len(input.MemberIDs) < 1 || len(input.MemberIDs) > 100 { + return invalidRequest() + } + members := make(map[string]struct{}, len(input.MemberIDs)) + for i, memberID := range input.MemberIDs { + canonical, valid := canonicalID(memberID) + if !valid { + return invalidRequest() + } + if _, duplicate := members[canonical]; duplicate { + return invalidRequest() + } + members[canonical] = struct{}{} + input.MemberIDs[i] = canonical + } + if _, included := members[user.ID]; !included { + return invalidRequest() + } + chat, err := s.messengerStore.CreateChat(r.Context(), input.Title, input.MemberIDs, user.ID) + if errors.Is(err, postgres.ErrInvalidMembers) { + return invalidRequest() + } + if err != nil { + return unavailable() + } + writeJSON(w, http.StatusCreated, chat) + return nil +} + +func (s *Server) listChats(w http.ResponseWriter, r *http.Request, user User) *apiError { + limit, cursor, err := pageParams(r, "chat", user.ID) + if err != nil { + return err + } + items, hasNext, listErr := s.messengerStore.ListChats(r.Context(), user.ID, cursorPosition(cursor), limit) + if errors.Is(listErr, postgres.ErrInvalidCursor) { + return invalidRequest() + } + if listErr != nil { + return unavailable() + } + if items == nil { + items = []Chat{} + } + var next *string + if hasNext { + last := items[len(items)-1] + value := encodeCursor("chat", user.ID, last.CreatedAt, last.ID) + next = &value + } + writeJSON(w, http.StatusOK, struct { + Items []Chat `json:"items"` + NextCursor *string `json:"next_cursor"` + }{items, next}) + return nil +} + +func (s *Server) chat(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet) + } + user, _, authErr := s.authenticate(r) + if authErr != nil { + return authErr + } + chatID, valid := canonicalID(strings.Split(r.URL.Path, "/")[4]) + if !valid { + return invalidRequest() + } + chat, err := s.messengerStore.ChatForUser(r.Context(), chatID, user.ID) + if errors.Is(err, postgres.ErrNotFound) { + return notFound() + } + if err != nil { + return unavailable() + } + writeJSON(w, http.StatusOK, chat) + return nil +} + +func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError { + if r.Method != http.MethodPost && r.Method != http.MethodGet { + return methodNotAllowed(w, http.MethodGet+", "+http.MethodPost) + } + user, _, authErr := s.authenticate(r) + if authErr != nil { + return authErr + } + chatID, valid := canonicalID(strings.Split(r.URL.Path, "/")[4]) + if !valid { + return invalidRequest() + } + if r.Method == http.MethodGet { + return s.listMessages(w, r, user, chatID) + } + key, valid := idempotencyKey(r) + if !valid { + return invalidRequest() + } + var input struct { + Text string `json:"text"` + } + if err := readJSON(w, r, &input); err != nil { + return err + } + if !validText(input.Text, 4000) { + return invalidRequest() + } + message, replay, err := s.messengerStore.CreateMessage( + r.Context(), chatID, user.ID, input.Text, key, s.idempotencyTTL, + ) + if errors.Is(err, postgres.ErrNotFound) { + return notFound() + } + if errors.Is(err, postgres.ErrIdempotencyConflict) { + return &apiError{http.StatusConflict, errorCodeConflict, "idempotency key conflicts with previous request"} + } + if err != nil { + return unavailable() + } + if replay { + s.metrics.replays.Inc() + } + writeJSON(w, http.StatusCreated, message) + return nil +} + +func (s *Server) listMessages(w http.ResponseWriter, r *http.Request, user User, chatID string) *apiError { + limit, cursor, err := pageParams(r, "message", chatID) + if err != nil { + return err + } + items, hasNext, listErr := s.messengerStore.ListMessages(r.Context(), chatID, user.ID, cursorPosition(cursor), limit) + if errors.Is(listErr, postgres.ErrNotFound) { + return notFound() + } + if errors.Is(listErr, postgres.ErrInvalidCursor) { + return invalidRequest() + } + if listErr != nil { + return unavailable() + } + if items == nil { + items = []Message{} + } + var next *string + if hasNext { + last := items[len(items)-1] + value := encodeCursor("message", chatID, last.CreatedAt, last.ID) + next = &value + } + writeJSON(w, http.StatusOK, struct { + Items []Message `json:"items"` + NextCursor *string `json:"next_cursor"` + }{items, next}) + return nil +} + +func cursorPosition(cursor *pageCursor) *model.Position { + if cursor == nil { + return nil + } + return &model.Position{CreatedAt: cursor.Time, ID: cursor.ID} +} diff --git a/internal/messenger/messenger_interface.go b/internal/messenger/messenger_interface.go new file mode 100644 index 00000000..1761254e --- /dev/null +++ b/internal/messenger/messenger_interface.go @@ -0,0 +1,33 @@ +package messenger + +import ( + "context" + "time" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" +) + +type MessengerStore interface { + Ping(context.Context) error + CreateUser(context.Context, string, string, string) (model.User, error) + UserByID(context.Context, string) (model.User, error) + UserByUsername(context.Context, string) (model.UserCredentials, error) + CreateChat(context.Context, string, []string, string) (model.Chat, error) + ChatForUser(context.Context, string, string) (model.Chat, error) + ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error) + CreateMessage(context.Context, string, string, string, string, time.Duration) (model.Message, bool, error) + ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error) +} + +type SessionStore interface { + Ping(context.Context) error + Create(context.Context, string, time.Duration) (string, time.Time, error) + UserID(context.Context, string) (string, error) + Delete(context.Context, string) error +} + +type PasswordService interface { + Hash(string) (string, error) + Verify(string, string) (bool, error) + DummyHash() string +} diff --git a/internal/messenger/metrics.go b/internal/messenger/metrics.go new file mode 100644 index 00000000..8073542e --- /dev/null +++ b/internal/messenger/metrics.go @@ -0,0 +1,83 @@ +package messenger + +import ( + "net/http" + "strconv" + "strings" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +type metrics struct { + requests *prometheus.CounterVec + duration *prometheus.HistogramVec + inFlight prometheus.Gauge + replays prometheus.Counter + handler http.Handler +} + +func newMetrics() *metrics { + registry := prometheus.NewRegistry() + result := &metrics{ + requests: prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: "messenger", + Subsystem: "http", + Name: "requests_total", + Help: "Completed HTTP requests.", + }, []string{"method", "route", "status_class"}), + duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "messenger", + Subsystem: "http", + Name: "request_duration_seconds", + Help: "HTTP request duration in seconds.", + Buckets: prometheus.DefBuckets, + }, []string{"method", "route"}), + inFlight: prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: "messenger", + Subsystem: "http", + Name: "requests_in_flight", + Help: "HTTP requests currently being served.", + }), + replays: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: "messenger", + Subsystem: "http", + Name: "idempotency_replays_total", + Help: "Message requests served from an idempotency record.", + }), + } + registry.MustRegister( + result.requests, + result.duration, + result.inFlight, + result.replays, + collectors.NewGoCollector(), + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + ) + result.handler = promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) + return result +} + +func (m *metrics) observe(r *http.Request, status int, started time.Time) { + route := routeTemplate(r.URL.Path) + m.requests.WithLabelValues(r.Method, route, strconv.Itoa(status/100)+"xx").Inc() + m.duration.WithLabelValues(r.Method, route).Observe(time.Since(started).Seconds()) +} + +func routeTemplate(path string) string { + switch path { + case "/health/live", "/health/ready", "/metrics", "/api/v1/users", "/api/v1/users/me", + "/api/v1/auth/sessions", "/api/v1/auth/sessions/current", "/api/v1/chats": + return path + } + parts := strings.Split(strings.Trim(path, "/"), "/") + if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" { + return "/api/v1/chats/{chat_id}" + } + if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" { + return "/api/v1/chats/{chat_id}/messages" + } + return "unknown" +} diff --git a/internal/messenger/model.go b/internal/messenger/model.go new file mode 100644 index 00000000..acbe99d8 --- /dev/null +++ b/internal/messenger/model.go @@ -0,0 +1,29 @@ +package messenger + +import ( + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" + "github.com/google/uuid" +) + +type User = model.User +type Chat = model.Chat +type Message = model.Message + +func newID() (string, error) { + id, err := uuid.NewRandom() + if err != nil { + return "", err + } + return id.String(), nil +} + +func canonicalID(value string) (string, bool) { + if len(value) != 36 { + return "", false + } + id, err := uuid.Parse(value) + if err != nil { + return "", false + } + return id.String(), true +} diff --git a/internal/messenger/request.go b/internal/messenger/request.go new file mode 100644 index 00000000..da264ece --- /dev/null +++ b/internal/messenger/request.go @@ -0,0 +1,127 @@ +package messenger + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "io" + "mime" + "net/http" + "net/url" + "regexp" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +const maxRequestBytes = 1 << 20 + +var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError { + mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) { + return &apiError{http.StatusUnsupportedMediaType, errorCodeUnsupportedMediaType, "expected application/json"} + } + r.Body = http.MaxBytesReader(w, r.Body, maxRequestBytes) + data, err := io.ReadAll(r.Body) + if err != nil { + var tooLarge *http.MaxBytesError + if errors.As(err, &tooLarge) { + return &apiError{http.StatusRequestEntityTooLarge, errorCodePayloadTooLarge, "request body too large"} + } + return invalidRequest() + } + if !utf8.Valid(data) { + return invalidRequest() + } + var object map[string]json.RawMessage + if err := json.Unmarshal(data, &object); err != nil || object == nil { + return invalidRequest() + } + normalized, err := json.Marshal(object) + if err != nil { + return invalidRequest() + } + decoder := json.NewDecoder(bytes.NewReader(normalized)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(target); err != nil { + return invalidRequest() + } + return nil +} + +func validText(value string, maxRunes int) bool { + return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= maxRunes +} + +func idempotencyKey(r *http.Request) (string, bool) { + values := r.Header.Values("Idempotency-Key") + if len(values) != 1 || !idempotencyKeyPattern.MatchString(values[0]) { + return "", false + } + return values[0], true +} + +type pageCursor struct { + Kind string `json:"kind"` + Resource string `json:"resource"` + Time time.Time `json:"time"` + ID string `json:"id"` +} + +func pageParams(r *http.Request, kind, resource string) (int, *pageCursor, *apiError) { + query, err := url.ParseQuery(r.URL.RawQuery) + if err != nil { + return 0, nil, invalidRequest() + } + for key := range query { + if key != "limit" && key != "cursor" { + return 0, nil, invalidRequest() + } + if len(query[key]) != 1 { + return 0, nil, invalidRequest() + } + } + limit := 50 + if value, present := query["limit"]; present { + parsed, err := strconv.Atoi(value[0]) + if err != nil || parsed < 1 || parsed > 100 { + return 0, nil, invalidRequest() + } + limit = parsed + } + if value, present := query["cursor"]; present { + cursor, valid := decodeCursor(value[0], kind, resource) + if !valid { + return 0, nil, invalidRequest() + } + return limit, cursor, nil + } + return limit, nil, nil +} + +func encodeCursor(kind, resource string, createdAt time.Time, id string) string { + data, _ := json.Marshal(pageCursor{kind, resource, createdAt, id}) + return base64.RawURLEncoding.EncodeToString(data) +} + +func decodeCursor(value, kind, resource string) (*pageCursor, bool) { + data, err := base64.RawURLEncoding.DecodeString(value) + if err != nil { + return nil, false + } + var cursor pageCursor + if err := json.Unmarshal(data, &cursor); err != nil || cursor.Kind != kind || cursor.Resource != resource { + return nil, false + } + if cursor.Time.IsZero() { + return nil, false + } + if _, valid := canonicalID(cursor.ID); !valid { + return nil, false + } + return &cursor, true +} diff --git a/internal/messenger/routes.go b/internal/messenger/routes.go new file mode 100644 index 00000000..bb698d0b --- /dev/null +++ b/internal/messenger/routes.go @@ -0,0 +1,41 @@ +package messenger + +import ( + "net/http" + "strings" +) + +type requestHandler func(http.ResponseWriter, *http.Request) *apiError + +func (s *Server) route(w http.ResponseWriter, r *http.Request) *apiError { + var handler requestHandler + switch r.URL.Path { + case "/health/live": + handler = s.live + case "/health/ready": + handler = s.readiness + case "/metrics": + s.metrics.handler.ServeHTTP(w, r) + return nil + case "/api/v1/users": + handler = s.users + case "/api/v1/users/me": + handler = s.me + case "/api/v1/auth/sessions": + handler = s.createSession + case "/api/v1/auth/sessions/current": + handler = s.revokeSession + case "/api/v1/chats": + handler = s.chats + default: + parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/") + if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" { + handler = s.chat + } else if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" { + handler = s.messages + } else { + return notFound() + } + } + return handler(w, r) +} diff --git a/internal/messenger/server.go b/internal/messenger/server.go new file mode 100644 index 00000000..3cc2ba00 --- /dev/null +++ b/internal/messenger/server.go @@ -0,0 +1,112 @@ +package messenger + +import ( + "context" + "log/slog" + "net/http" + "sync/atomic" + "time" +) + +type Server struct { + messengerStore MessengerStore + sessions SessionStore + passwords PasswordService + sessionTTL time.Duration + idempotencyTTL time.Duration + requestTTL time.Duration + instanceID string + logger *slog.Logger + swaggerPort string + metrics *metrics + ready atomic.Bool +} + +type statusWriter struct { + http.ResponseWriter + status int +} + +func (w *statusWriter) WriteHeader(status int) { + w.status = status + w.ResponseWriter.WriteHeader(status) +} + +func NewServer( + logger *slog.Logger, + messages MessengerStore, + sessions SessionStore, + passwords PasswordService, + sessionTTL time.Duration, + idempotencyTTL time.Duration, + requestTTL time.Duration, + instanceID string, + swaggerPort string, +) *Server { + s := &Server{ + messengerStore: messages, sessions: sessions, passwords: passwords, + sessionTTL: sessionTTL, idempotencyTTL: idempotencyTTL, requestTTL: requestTTL, + instanceID: instanceID, logger: logger, swaggerPort: swaggerPort, metrics: newMetrics(), + } + s.ready.Store(true) + return s +} + +func (s *Server) SetReady(ready bool) { + s.ready.Store(ready) +} + +func (s *Server) Handler() http.Handler { + return http.HandlerFunc(s.serve) +} + +func (s *Server) serve(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", s.instanceID) + requestID, err := newID() + if err != nil { + http.Error(w, "server unavailable", http.StatusServiceUnavailable) + return + } + w.Header().Set("X-Request-Id", requestID) + w.Header().Set("Cache-Control", "no-store") + wrapped := &statusWriter{ResponseWriter: w, status: http.StatusOK} + started := time.Now() + s.metrics.inFlight.Inc() + defer func() { + s.metrics.inFlight.Dec() + if recovered := recover(); recovered != nil { + s.logger.Error("request panic", "request_id", requestID, "method", r.Method, "path", r.URL.Path) + if wrapped.status == http.StatusOK { + writeError(wrapped, requestID, internalError()) + } + } + s.metrics.observe(r, wrapped.status, started) + s.logger.Info("request", "request_id", requestID, "method", r.Method, "path", r.URL.Path, "status", wrapped.status, "duration_ms", time.Since(started).Milliseconds()) + }() + if s.allowSwaggerOrigin(wrapped, r) && r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" { + wrapped.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS") + wrapped.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Idempotency-Key") + wrapped.WriteHeader(http.StatusNoContent) + return + } + ctx, cancel := context.WithTimeout(r.Context(), s.requestTTL) + defer cancel() + r = r.WithContext(ctx) + if apiErr := s.route(wrapped, r); apiErr != nil { + if apiErr.status == http.StatusUnauthorized { + wrapped.Header().Set("WWW-Authenticate", authenticationChallenge(r)) + } + writeError(wrapped, requestID, apiErr) + } +} + +func (s *Server) allowSwaggerOrigin(w http.ResponseWriter, r *http.Request) bool { + origin := r.Header.Get("Origin") + if origin != "http://localhost:"+s.swaggerPort && origin != "http://127.0.0.1:"+s.swaggerPort { + return false + } + w.Header().Set("Access-Control-Allow-Origin", origin) + w.Header().Set("Access-Control-Expose-Headers", "X-Request-Id, X-Instance-Id") + w.Header().Add("Vary", "Origin") + return true +} diff --git a/internal/messenger/server_test.go b/internal/messenger/server_test.go new file mode 100644 index 00000000..011fb0fe --- /dev/null +++ b/internal/messenger/server_test.go @@ -0,0 +1,257 @@ +package messenger + +import ( + "context" + "errors" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/suite" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres" +) + +var errDependency = errors.New("dependency unavailable") + +type MessengerSuite struct { + suite.Suite + logger *slog.Logger +} + +func TestMessengerSuite(t *testing.T) { + suite.Run(t, new(MessengerSuite)) +} + +func (s *MessengerSuite) SetupTest() { + s.logger = slog.New(slog.NewTextHandler(io.Discard, nil)) +} + +type messengerStoreStub struct { + pingErr error + userByID model.User + userByIDErr error + credentials model.UserCredentials + credentialsErr error + message model.Message + messageReplay bool + messageErr error +} + +func (s messengerStoreStub) Ping(context.Context) error { return s.pingErr } +func (messengerStoreStub) CreateUser(context.Context, string, string, string) (model.User, error) { + return model.User{}, nil +} +func (s messengerStoreStub) UserByID(context.Context, string) (model.User, error) { + return s.userByID, s.userByIDErr +} +func (s messengerStoreStub) UserByUsername(context.Context, string) (model.UserCredentials, error) { + return s.credentials, s.credentialsErr +} +func (messengerStoreStub) CreateChat(context.Context, string, []string, string) (model.Chat, error) { + return model.Chat{}, nil +} +func (messengerStoreStub) ChatForUser(context.Context, string, string) (model.Chat, error) { + return model.Chat{}, nil +} +func (messengerStoreStub) ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error) { + return nil, false, nil +} +func (s messengerStoreStub) CreateMessage(context.Context, string, string, string, string, time.Duration) (model.Message, bool, error) { + return s.message, s.messageReplay, s.messageErr +} +func (messengerStoreStub) ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error) { + return nil, false, nil +} + +type sessionStub struct { + pingErr error + userID string + userIDErr error +} + +func (s sessionStub) Ping(context.Context) error { return s.pingErr } +func (sessionStub) Create(context.Context, string, time.Duration) (string, time.Time, error) { + return "token", time.Now().Add(time.Hour), nil +} +func (s sessionStub) UserID(context.Context, string) (string, error) { return s.userID, s.userIDErr } +func (sessionStub) Delete(context.Context, string) error { return nil } + +type passwordsStub struct { + verified bool + err error +} + +func (passwordsStub) Hash(string) (string, error) { return "hash", nil } +func (passwordsStub) DummyHash() string { return "hash" } +func (s passwordsStub) Verify(string, string) (bool, error) { + return s.verified, s.err +} + +func (s *MessengerSuite) TestReadinessReturnsUnavailableForDependencyFailure() { + t := s.T() + tests := []struct { + name string + store messengerStoreStub + sessions sessionStub + }{ + {name: "postgres", store: messengerStoreStub{pingErr: errDependency}}, + {name: "valkey", sessions: sessionStub{pingErr: errDependency}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + server := s.newTestServer(tt.store, tt.sessions, passwordsStub{}) + response := serveRequest(server, http.MethodGet, "/health/ready", "") + if response.Code != http.StatusServiceUnavailable { + t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable) + } + }) + } +} + +func (s *MessengerSuite) TestBusinessRequestReturnsUnavailableForDependencyFailure() { + t := s.T() + tests := []struct { + name string + store messengerStoreStub + sessions sessionStub + }{ + {name: "valkey", sessions: sessionStub{userIDErr: errDependency}}, + {name: "postgres", store: messengerStoreStub{userByIDErr: errDependency}, sessions: sessionStub{userID: "user-id"}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + server := s.newTestServer(tt.store, tt.sessions, passwordsStub{}) + request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil) + request.Header.Set("Authorization", "Bearer valid-token") + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + if response.Code != http.StatusServiceUnavailable { + t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable) + } + }) + } +} + +func (s *MessengerSuite) TestAuthenticationChallenges() { + t := s.T() + server := s.newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{}) + bearer := serveRequest(server, http.MethodGet, "/api/v1/users/me", "") + if bearer.Code != http.StatusUnauthorized || bearer.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` { + t.Fatalf("business challenge = %d %q", bearer.Code, bearer.Header().Get("WWW-Authenticate")) + } + + store := messengerStoreStub{credentials: model.UserCredentials{PasswordHash: "hash"}} + server = s.newTestServer(store, sessionStub{}, passwordsStub{verified: false}) + request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil) + request.SetBasicAuth("user", "wrong-password") + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + if response.Code != http.StatusUnauthorized || response.Header().Get("WWW-Authenticate") != `Basic realm="messenger", charset="UTF-8"` { + t.Fatalf("session challenge = %d %q", response.Code, response.Header().Get("WWW-Authenticate")) + } +} + +func (s *MessengerSuite) TestSessionCreationRejectsChunkedBody() { + t := s.T() + server := s.newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{}) + request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", strings.NewReader("{}")) + request.ContentLength = -1 + request.SetBasicAuth("user", "password") + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + if response.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest) + } +} + +func (s *MessengerSuite) TestRegistrationRejectsWhitespacePassword() { + t := s.T() + server := s.newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{}) + response := serveRequest(server, http.MethodPost, "/api/v1/users", `{"username":"student","display_name":"Student","password":" "}`) + if response.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest) + } +} + +func (s *MessengerSuite) TestInstanceHeaderIsSetOnSuccessAndError() { + t := s.T() + server := s.newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{}) + for _, response := range []*httptest.ResponseRecorder{ + serveRequest(server, http.MethodGet, "/health/live", ""), + serveRequest(server, http.MethodGet, "/api/v1/users/me", ""), + } { + if value := response.Header().Get("X-Instance-Id"); value != "test-instance" { + t.Fatalf("X-Instance-Id = %q, want test-instance", value) + } + } +} + +func (s *MessengerSuite) TestMessageCreationValidatesIdempotencyKey() { + t := s.T() + store := messengerStoreStub{userByID: model.User{ID: "user-id"}} + server := s.newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{}) + tests := []struct { + name string + key string + }{ + {name: "missing"}, + {name: "invalid", key: "invalid key"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + request := httptest.NewRequest(http.MethodPost, "/api/v1/chats/00000000-0000-0000-0000-000000000001/messages", strings.NewReader(`{"text":"message"}`)) + request.Header.Set("Authorization", "Bearer valid-token") + request.Header.Set("Content-Type", "application/json") + if tt.key != "" { + request.Header.Set("Idempotency-Key", tt.key) + } + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + if response.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest) + } + }) + } +} + +func (s *MessengerSuite) TestMessageCreationReturnsConflictForReusedKeyWithDifferentBody() { + t := s.T() + store := messengerStoreStub{ + userByID: model.User{ID: "user-id"}, + messageErr: fmt.Errorf("create message: %w", postgres.ErrIdempotencyConflict), + } + server := s.newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{}) + request := httptest.NewRequest(http.MethodPost, "/api/v1/chats/00000000-0000-0000-0000-000000000001/messages", strings.NewReader(`{"text":"different"}`)) + request.Header.Set("Authorization", "Bearer valid-token") + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Idempotency-Key", "same-key") + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + if response.Code != http.StatusConflict { + t.Fatalf("status = %d, want %d", response.Code, http.StatusConflict) + } +} + +func (s *MessengerSuite) newTestServer(messages MessengerStore, sessions SessionStore, passwords PasswordService) *Server { + return NewServer(s.logger, messages, sessions, passwords, time.Hour, 24*time.Hour, time.Second, "test-instance", "8081") +} + +func serveRequest(server *Server, method, path, body string) *httptest.ResponseRecorder { + request := httptest.NewRequest(method, path, strings.NewReader(body)) + if body != "" { + request.Header.Set("Content-Type", "application/json") + } + response := httptest.NewRecorder() + server.Handler().ServeHTTP(response, request) + return response +} diff --git a/internal/model/model.go b/internal/model/model.go new file mode 100644 index 00000000..57a247e8 --- /dev/null +++ b/internal/model/model.go @@ -0,0 +1,82 @@ +package model + +import ( + "encoding/json" + "time" +) + +const timestampLayout = "2006-01-02T15:04:05.000000Z" + +type User struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt time.Time `json:"created_at"` +} + +type UserCredentials struct { + User + PasswordHash string `json:"-"` +} + +type Chat struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type Message struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt time.Time `json:"created_at"` +} + +type Position struct { + CreatedAt time.Time + ID string +} + +type userJSON struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt string `json:"created_at"` +} + +type chatJSON struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt string `json:"created_at"` +} + +type messageJSON struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt string `json:"created_at"` +} + +func (u User) MarshalJSON() ([]byte, error) { + return json.Marshal(userJSON{u.ID, u.Username, u.DisplayName, formatTime(u.CreatedAt)}) +} + +func (c Chat) MarshalJSON() ([]byte, error) { + return json.Marshal(chatJSON{c.ID, c.Title, c.MemberIDs, formatTime(c.CreatedAt)}) +} + +func (m Message) MarshalJSON() ([]byte, error) { + return json.Marshal(messageJSON{m.ID, m.ChatID, m.SenderID, m.Text, formatTime(m.CreatedAt)}) +} + +func FormatTime(value time.Time) string { + return formatTime(value) +} + +func formatTime(value time.Time) string { + return value.UTC().Format(timestampLayout) +} diff --git a/internal/model/model_test.go b/internal/model/model_test.go new file mode 100644 index 00000000..d3a54d9c --- /dev/null +++ b/internal/model/model_test.go @@ -0,0 +1,24 @@ +package model + +import ( + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/suite" +) + +type ModelSuite struct { + suite.Suite +} + +func TestModelSuite(t *testing.T) { + suite.Run(t, new(ModelSuite)) +} + +func (s *ModelSuite) TestUserTimestampHasFixedMicroseconds() { + createdAt := time.Date(2026, time.September, 23, 21, 28, 15, 423030000, time.UTC) + data, err := json.Marshal(User{ID: "id", Username: "user", DisplayName: "User", CreatedAt: createdAt}) + s.Require().NoError(err) + s.Equal(`{"id":"id","username":"user","display_name":"User","created_at":"2026-09-23T21:28:15.423030Z"}`, string(data)) +} diff --git a/internal/password/password.go b/internal/password/password.go new file mode 100644 index 00000000..5dd8b1b1 --- /dev/null +++ b/internal/password/password.go @@ -0,0 +1,133 @@ +package password + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strconv" + "strings" + + "golang.org/x/crypto/argon2" +) + +var ( + ErrInvalidConfig = errors.New("invalid password configuration") + ErrInvalidHash = errors.New("invalid password hash") +) + +type Config struct { + MemoryKiB uint32 + Iterations uint32 + Parallelism uint8 + SaltLength uint32 + KeyLength uint32 +} + +type Hasher struct { + config Config + dummyHash string +} + +func New(config Config) (*Hasher, error) { + if err := validateConfig(config); err != nil { + return nil, err + } + salt := make([]byte, config.SaltLength) + digest := argon2.IDKey([]byte("invalid-user-password"), salt, config.Iterations, config.MemoryKiB, config.Parallelism, config.KeyLength) + return &Hasher{config: config, dummyHash: encode(config, salt, digest)}, nil +} + +func validateConfig(config Config) error { + if config.MemoryKiB < 8*1024 || config.MemoryKiB > 1024*1024 || + config.Iterations < 1 || config.Iterations > 20 || + config.Parallelism < 1 || + config.SaltLength < 8 || config.SaltLength > 1024 || + config.KeyLength < 16 || config.KeyLength > 1024 { + return ErrInvalidConfig + } + return nil +} + +func (h *Hasher) Hash(value string) (string, error) { + salt := make([]byte, h.config.SaltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("generate password salt: %w", err) + } + digest := argon2.IDKey([]byte(value), salt, h.config.Iterations, h.config.MemoryKiB, h.config.Parallelism, h.config.KeyLength) + return encode(h.config, salt, digest), nil +} + +func (h *Hasher) Verify(value, encodedHash string) (bool, error) { + config, salt, digest, err := decode(encodedHash) + if err != nil { + return false, err + } + actual := argon2.IDKey([]byte(value), salt, config.Iterations, config.MemoryKiB, config.Parallelism, config.KeyLength) + return subtle.ConstantTimeCompare(actual, digest) == 1, nil +} + +func (h *Hasher) DummyHash() string { + return h.dummyHash +} + +func encode(config Config, salt, digest []byte) string { + return fmt.Sprintf( + "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, + config.MemoryKiB, + config.Iterations, + config.Parallelism, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(digest), + ) +} + +func decode(encodedHash string) (Config, []byte, []byte, error) { + parts := strings.Split(encodedHash, "$") + if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) { + return Config{}, nil, nil, ErrInvalidHash + } + + parameters := strings.Split(parts[3], ",") + if len(parameters) != 3 { + return Config{}, nil, nil, ErrInvalidHash + } + memory, err := parameter(parameters[0], "m=", 32) + if err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + iterations, err := parameter(parameters[1], "t=", 32) + if err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + parallelism, err := parameter(parameters[2], "p=", 8) + if err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + + salt, err := base64.RawStdEncoding.Strict().DecodeString(parts[4]) + if err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + digest, err := base64.RawStdEncoding.Strict().DecodeString(parts[5]) + if err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + config := Config{ + MemoryKiB: uint32(memory), Iterations: uint32(iterations), Parallelism: uint8(parallelism), + SaltLength: uint32(len(salt)), KeyLength: uint32(len(digest)), + } + if err := validateConfig(config); err != nil { + return Config{}, nil, nil, ErrInvalidHash + } + return config, salt, digest, nil +} + +func parameter(value, prefix string, bitSize int) (uint64, error) { + if !strings.HasPrefix(value, prefix) { + return 0, ErrInvalidHash + } + return strconv.ParseUint(strings.TrimPrefix(value, prefix), 10, bitSize) +} diff --git a/internal/password/password_test.go b/internal/password/password_test.go new file mode 100644 index 00000000..8e88875a --- /dev/null +++ b/internal/password/password_test.go @@ -0,0 +1,44 @@ +package password + +import ( + "testing" + + "github.com/stretchr/testify/suite" +) + +type PasswordSuite struct { + suite.Suite + hasher *Hasher +} + +func TestPasswordSuite(t *testing.T) { + suite.Run(t, new(PasswordSuite)) +} + +func (s *PasswordSuite) SetupTest() { + hasher, err := New(testConfig()) + s.Require().NoError(err) + s.hasher = hasher +} + +func (s *PasswordSuite) TestHasher() { + hash, err := s.hasher.Hash("T3st-password-123") + s.Require().NoError(err) + + verified, err := s.hasher.Verify("T3st-password-123", hash) + s.Require().NoError(err) + s.True(verified) + + verified, err = s.hasher.Verify("wrong-password", hash) + s.Require().NoError(err) + s.False(verified) +} + +func (s *PasswordSuite) TestVerifyRejectsMalformedHash() { + _, err := s.hasher.Verify("password", "invalid") + s.Error(err) +} + +func testConfig() Config { + return Config{MemoryKiB: 8 * 1024, Iterations: 1, Parallelism: 1, SaltLength: 16, KeyLength: 32} +} diff --git a/internal/postgres/errors.go b/internal/postgres/errors.go new file mode 100644 index 00000000..4e0c3780 --- /dev/null +++ b/internal/postgres/errors.go @@ -0,0 +1,11 @@ +package postgres + +import "errors" + +var ( + ErrConflict = errors.New("conflict") + ErrNotFound = errors.New("not found") + ErrInvalidMembers = errors.New("invalid members") + ErrInvalidCursor = errors.New("invalid cursor") + ErrIdempotencyConflict = errors.New("idempotency conflict") +) diff --git a/internal/postgres/idempotency.go b/internal/postgres/idempotency.go new file mode 100644 index 00000000..a4843ad7 --- /dev/null +++ b/internal/postgres/idempotency.go @@ -0,0 +1,285 @@ +package postgres + +import ( + "bytes" + "context" + "crypto/sha256" + "errors" + "fmt" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" +) + +const expiredIdempotencyCleanupBatch = 100 + +func (s *Store) CreateMessage( + ctx context.Context, + chatID string, + senderID string, + text string, + idempotencyKey string, + idempotencyTTL time.Duration, +) (model.Message, bool, error) { + var ( + tx pgx.Tx + err error + visible bool + requestHash [sha256.Size]byte + message model.Message + replay bool + ) + + tx, err = s.pool.BeginTx(ctx, pgx.TxOptions{}) + if err != nil { + return model.Message{}, false, fmt.Errorf("begin create message transaction: %w", err) + } + defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() + + visible, err = chatVisibleInTransaction(ctx, tx, chatID, senderID) + if err != nil { + return model.Message{}, false, err + } + if !visible { + return model.Message{}, false, ErrNotFound + } + if err = deleteExpiredIdempotencyRecords(ctx, tx, expiredIdempotencyCleanupBatch); err != nil { + return model.Message{}, false, err + } + + requestHash = sha256.Sum256([]byte(text)) + message, replay, err = createIdempotentMessage( + ctx, tx, chatID, senderID, text, idempotencyKey, requestHash[:], idempotencyTTL, + ) + if err != nil { + return model.Message{}, false, err + } + if err := tx.Commit(ctx); err != nil { + return model.Message{}, false, fmt.Errorf("commit create message transaction: %w", err) + } + return message, replay, nil +} + +func chatVisibleInTransaction(ctx context.Context, tx pgx.Tx, chatID, userID string) (bool, error) { + const query = ` + SELECT EXISTS ( + SELECT 1 + FROM chat_members + WHERE chat_id = $1 AND user_id = $2 + )` + + var visible bool + if err := tx.QueryRow(ctx, query, chatID, userID).Scan(&visible); err != nil { + return false, fmt.Errorf("check chat visibility: %w", err) + } + return visible, nil +} + +func createIdempotentMessage( + ctx context.Context, + tx pgx.Tx, + chatID string, + senderID string, + text string, + idempotencyKey string, + requestHash []byte, + idempotencyTTL time.Duration, +) (model.Message, bool, error) { + for { + var ( + record idempotencyRecord + found bool + err error + messageID string + reserved bool + message model.Message + ) + + record, found, err = lockIdempotencyRecord(ctx, tx, senderID, chatID, idempotencyKey) + if err != nil { + return model.Message{}, false, err + } + if found && record.active { + if !bytes.Equal(record.requestHash, requestHash) { + return model.Message{}, false, ErrIdempotencyConflict + } + message, err = messageByID(ctx, tx, record.messageID) + return message, true, err + } + if found { + if err := deleteIdempotencyRecord(ctx, tx, senderID, chatID, idempotencyKey); err != nil { + return model.Message{}, false, err + } + } + + messageID = uuid.NewString() + reserved, err = reserveIdempotencyKey( + ctx, tx, senderID, chatID, idempotencyKey, requestHash, messageID, idempotencyTTL, + ) + if err != nil { + return model.Message{}, false, err + } + if !reserved { + continue + } + + message, err = insertMessage(ctx, tx, messageID, chatID, senderID, text) + return message, false, err + } +} + +func deleteExpiredIdempotencyRecords(ctx context.Context, tx pgx.Tx, limit int) error { + const query = ` + WITH expired AS ( + SELECT ctid + FROM message_idempotency + WHERE expires_at <= statement_timestamp() + ORDER BY expires_at + LIMIT $1 + FOR UPDATE SKIP LOCKED + ) + DELETE FROM message_idempotency records + USING expired + WHERE records.ctid = expired.ctid` + + if _, err := tx.Exec(ctx, query, limit); err != nil { + return fmt.Errorf("delete expired idempotency records: %w", err) + } + return nil +} + +type idempotencyRecord struct { + requestHash []byte + messageID string + active bool +} + +func lockIdempotencyRecord( + ctx context.Context, + tx pgx.Tx, + userID string, + chatID string, + key string, +) (idempotencyRecord, bool, error) { + const query = ` + SELECT request_hash, message_id, expires_at > statement_timestamp() + FROM message_idempotency + WHERE user_id = $1 AND chat_id = $2 AND key = $3 + FOR UPDATE` + + var ( + record idempotencyRecord + err error + ) + err = tx.QueryRow(ctx, query, userID, chatID, key).Scan( + &record.requestHash, + &record.messageID, + &record.active, + ) + if errors.Is(err, pgx.ErrNoRows) { + return idempotencyRecord{}, false, nil + } + if err != nil { + return idempotencyRecord{}, false, fmt.Errorf("lock idempotency key: %w", err) + } + return record, true, nil +} + +func deleteIdempotencyRecord(ctx context.Context, tx pgx.Tx, userID, chatID, key string) error { + const query = ` + DELETE FROM message_idempotency + WHERE user_id = $1 AND chat_id = $2 AND key = $3` + if _, err := tx.Exec(ctx, query, userID, chatID, key); err != nil { + return fmt.Errorf("delete expired idempotency key: %w", err) + } + return nil +} + +func reserveIdempotencyKey( + ctx context.Context, + tx pgx.Tx, + userID string, + chatID string, + key string, + requestHash []byte, + messageID string, + ttl time.Duration, +) (bool, error) { + const query = ` + INSERT INTO message_idempotency (user_id, chat_id, key, request_hash, message_id, expires_at) + VALUES ($1, $2, $3, $4, $5, statement_timestamp() + $6 * INTERVAL '1 second') + ON CONFLICT (user_id, chat_id, key) DO NOTHING + RETURNING true` + + var ( + inserted bool + err error + ) + err = tx.QueryRow(ctx, query, userID, chatID, key, requestHash, messageID, int64(ttl/time.Second)).Scan(&inserted) + if errors.Is(err, pgx.ErrNoRows) { + return false, nil + } + if err != nil { + return false, fmt.Errorf("reserve idempotency key: %w", err) + } + return inserted, nil +} + +func insertMessage( + ctx context.Context, + tx pgx.Tx, + messageID string, + chatID string, + senderID string, + text string, +) (model.Message, error) { + const query = ` + INSERT INTO messages (id, chat_id, sender_id, text) + VALUES ($1, $2, $3, $4) + RETURNING id, chat_id, sender_id, text, created_at` + + var ( + message model.Message + err error + ) + err = tx.QueryRow(ctx, query, messageID, chatID, senderID, text).Scan( + &message.ID, + &message.ChatID, + &message.SenderID, + &message.Text, + &message.CreatedAt, + ) + if err != nil { + return model.Message{}, fmt.Errorf("insert message: %w", err) + } + return message, nil +} + +func messageByID(ctx context.Context, tx pgx.Tx, messageID string) (model.Message, error) { + const query = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE id = $1` + + var ( + message model.Message + err error + ) + err = tx.QueryRow(ctx, query, messageID).Scan( + &message.ID, + &message.ChatID, + &message.SenderID, + &message.Text, + &message.CreatedAt, + ) + if errors.Is(err, pgx.ErrNoRows) { + return model.Message{}, fmt.Errorf("idempotency result message not found") + } + if err != nil { + return model.Message{}, fmt.Errorf("get idempotency result message: %w", err) + } + return message, nil +} diff --git a/internal/postgres/store.go b/internal/postgres/store.go new file mode 100644 index 00000000..2e072579 --- /dev/null +++ b/internal/postgres/store.go @@ -0,0 +1,435 @@ +package postgres + +import ( + "context" + "errors" + "fmt" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" +) + +type Store struct { + pool *pgxpool.Pool +} + +func New(pool *pgxpool.Pool) *Store { + return &Store{pool: pool} +} + +func (s *Store) Ping(ctx context.Context) error { + return s.pool.Ping(ctx) +} + +func (s *Store) CreateUser( + ctx context.Context, + username string, + displayName string, + passwordHash string, +) (model.User, error) { + const query = ` + INSERT INTO users (id, username, display_name, password_hash) + VALUES ($1, $2, $3, $4) + RETURNING id, username, display_name, created_at` + + var user model.User + err := s.pool.QueryRow(ctx, query, uuid.NewString(), username, displayName, passwordHash).Scan( + &user.ID, + &user.Username, + &user.DisplayName, + &user.CreatedAt, + ) + if isUniqueViolation(err) { + return model.User{}, ErrConflict + } + if err != nil { + return model.User{}, fmt.Errorf("create user: %w", err) + } + + return user, nil +} + +func (s *Store) UserByID(ctx context.Context, id string) (model.User, error) { + const query = ` + SELECT id, username, display_name, created_at + FROM users + WHERE id = $1` + + var user model.User + err := s.pool.QueryRow(ctx, query, id).Scan( + &user.ID, + &user.Username, + &user.DisplayName, + &user.CreatedAt, + ) + if errors.Is(err, pgx.ErrNoRows) { + return model.User{}, ErrNotFound + } + if err != nil { + return model.User{}, fmt.Errorf("get user by id: %w", err) + } + + return user, nil +} + +func (s *Store) UserByUsername(ctx context.Context, username string) (model.UserCredentials, error) { + const query = ` + SELECT id, username, display_name, created_at, password_hash + FROM users + WHERE username = $1` + + var credentials model.UserCredentials + err := s.pool.QueryRow(ctx, query, username).Scan( + &credentials.ID, + &credentials.Username, + &credentials.DisplayName, + &credentials.CreatedAt, + &credentials.PasswordHash, + ) + if errors.Is(err, pgx.ErrNoRows) { + return model.UserCredentials{}, ErrNotFound + } + if err != nil { + return model.UserCredentials{}, fmt.Errorf("get user by username: %w", err) + } + + return credentials, nil +} + +func (s *Store) CreateChat( + ctx context.Context, + title string, + memberIDs []string, + callerID string, +) (model.Chat, error) { + if !validMembers(memberIDs, callerID) { + return model.Chat{}, ErrInvalidMembers + } + + tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) + if err != nil { + return model.Chat{}, fmt.Errorf("begin create chat transaction: %w", err) + } + defer func() { _ = tx.Rollback(context.WithoutCancel(ctx)) }() + + valid, err := allUsersExist(ctx, tx, memberIDs) + if err != nil { + return model.Chat{}, err + } + if !valid { + return model.Chat{}, ErrInvalidMembers + } + + chat, err := insertChat(ctx, tx, title) + if err != nil { + return model.Chat{}, err + } + if err := insertMembers(ctx, tx, chat.ID, memberIDs); err != nil { + return model.Chat{}, err + } + if err := tx.Commit(ctx); err != nil { + return model.Chat{}, fmt.Errorf("commit create chat transaction: %w", err) + } + + chat.MemberIDs = append([]string(nil), memberIDs...) + return chat, nil +} + +func validMembers(memberIDs []string, callerID string) bool { + callerUUID, err := uuid.Parse(callerID) + if err != nil { + return false + } + + seen := make(map[uuid.UUID]struct{}, len(memberIDs)) + callerIncluded := false + for _, memberID := range memberIDs { + memberUUID, err := uuid.Parse(memberID) + if err != nil { + return false + } + if _, exists := seen[memberUUID]; exists { + return false + } + + seen[memberUUID] = struct{}{} + callerIncluded = callerIncluded || memberUUID == callerUUID + } + + return callerIncluded +} + +func allUsersExist(ctx context.Context, tx pgx.Tx, memberIDs []string) (bool, error) { + const query = ` + SELECT count(*) + FROM users + WHERE id::text = ANY($1)` + + var count int + if err := tx.QueryRow(ctx, query, memberIDs).Scan(&count); err != nil { + return false, fmt.Errorf("check chat members: %w", err) + } + + return count == len(memberIDs), nil +} + +func insertChat(ctx context.Context, tx pgx.Tx, title string) (model.Chat, error) { + const query = ` + INSERT INTO chats (id, title) + VALUES ($1, $2) + RETURNING id, title, created_at` + + var chat model.Chat + if err := tx.QueryRow(ctx, query, uuid.NewString(), title).Scan(&chat.ID, &chat.Title, &chat.CreatedAt); err != nil { + return model.Chat{}, fmt.Errorf("insert chat: %w", err) + } + + return chat, nil +} + +func insertMembers(ctx context.Context, tx pgx.Tx, chatID string, memberIDs []string) error { + const query = ` + INSERT INTO chat_members (chat_id, user_id, position) + VALUES ($1, $2, $3)` + + batch := &pgx.Batch{} + for position, memberID := range memberIDs { + batch.Queue(query, chatID, memberID, position) + } + + results := tx.SendBatch(ctx, batch) + defer func() { _ = results.Close() }() + for range memberIDs { + if _, err := results.Exec(); err != nil { + return fmt.Errorf("insert chat member: %w", err) + } + } + if err := results.Close(); err != nil { + return fmt.Errorf("close chat members batch: %w", err) + } + + return nil +} + +func (s *Store) ChatForUser(ctx context.Context, chatID, userID string) (model.Chat, error) { + const query = ` + SELECT c.id, c.title, c.created_at, + array_agg(cm.user_id::text ORDER BY cm.position) + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE c.id = $1 + AND EXISTS ( + SELECT 1 + FROM chat_members visible + WHERE visible.chat_id = c.id AND visible.user_id = $2 + ) + GROUP BY c.id, c.title, c.created_at` + + var chat model.Chat + err := s.pool.QueryRow(ctx, query, chatID, userID).Scan( + &chat.ID, + &chat.Title, + &chat.CreatedAt, + &chat.MemberIDs, + ) + if errors.Is(err, pgx.ErrNoRows) { + return model.Chat{}, ErrNotFound + } + if err != nil { + return model.Chat{}, fmt.Errorf("get chat for user: %w", err) + } + + return chat, nil +} + +func (s *Store) ListChats( + ctx context.Context, + userID string, + after *model.Position, + limit int, +) ([]model.Chat, bool, error) { + if after != nil { + valid, err := s.visibleChatPositionExists(ctx, userID, *after) + if err != nil { + return nil, false, err + } + if !valid { + return nil, false, ErrInvalidCursor + } + } + + const query = ` + SELECT c.id, c.title, c.created_at, + array_agg(all_members.user_id::text ORDER BY all_members.position) + FROM chats c + JOIN chat_members visible ON visible.chat_id = c.id AND visible.user_id = $1 + JOIN chat_members all_members ON all_members.chat_id = c.id + WHERE ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2, $3::uuid)) + GROUP BY c.id, c.title, c.created_at + ORDER BY c.created_at, c.id + LIMIT $4` + + var afterTime any + var afterID any + if after != nil { + afterTime = after.CreatedAt + afterID = after.ID + } + + rows, err := s.pool.Query(ctx, query, userID, afterTime, afterID, limit+1) + if err != nil { + return nil, false, fmt.Errorf("list chats: %w", err) + } + defer rows.Close() + + chats, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Chat](func(row pgx.CollectableRow) (model.Chat, error) { + var chat model.Chat + err := row.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs) + return chat, err + })) + if err != nil { + return nil, false, fmt.Errorf("scan chats: %w", err) + } + + return page(chats, limit) +} + +func (s *Store) visibleChatPositionExists( + ctx context.Context, + userID string, + position model.Position, +) (bool, error) { + const query = ` + SELECT EXISTS ( + SELECT 1 + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE cm.user_id = $1 AND c.created_at = $2 AND c.id = $3 + )` + + var exists bool + if err := s.pool.QueryRow(ctx, query, userID, position.CreatedAt, position.ID).Scan(&exists); err != nil { + return false, fmt.Errorf("validate chat cursor: %w", err) + } + + return exists, nil +} + +func (s *Store) ListMessages( + ctx context.Context, + chatID string, + userID string, + after *model.Position, + limit int, +) ([]model.Message, bool, error) { + visible, err := s.chatVisible(ctx, chatID, userID) + if err != nil { + return nil, false, err + } + if !visible { + return nil, false, ErrNotFound + } + if after != nil { + valid, err := s.messagePositionExists(ctx, chatID, *after) + if err != nil { + return nil, false, err + } + if !valid { + return nil, false, ErrInvalidCursor + } + } + + const query = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1 + AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid)) + ORDER BY created_at, id + LIMIT $4` + + var afterTime any + var afterID any + if after != nil { + afterTime = after.CreatedAt + afterID = after.ID + } + + rows, err := s.pool.Query(ctx, query, chatID, afterTime, afterID, limit+1) + if err != nil { + return nil, false, fmt.Errorf("list messages: %w", err) + } + defer rows.Close() + + messages, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Message](func(row pgx.CollectableRow) (model.Message, error) { + var message model.Message + err := row.Scan( + &message.ID, + &message.ChatID, + &message.SenderID, + &message.Text, + &message.CreatedAt, + ) + return message, err + })) + if err != nil { + return nil, false, fmt.Errorf("scan messages: %w", err) + } + + return page(messages, limit) +} + +func (s *Store) chatVisible(ctx context.Context, chatID, userID string) (bool, error) { + const query = ` + SELECT EXISTS ( + SELECT 1 + FROM chat_members + WHERE chat_id = $1 AND user_id = $2 + )` + + var exists bool + if err := s.pool.QueryRow(ctx, query, chatID, userID).Scan(&exists); err != nil { + return false, fmt.Errorf("check chat visibility: %w", err) + } + + return exists, nil +} + +func (s *Store) messagePositionExists( + ctx context.Context, + chatID string, + position model.Position, +) (bool, error) { + const query = ` + SELECT EXISTS ( + SELECT 1 + FROM messages + WHERE chat_id = $1 AND created_at = $2 AND id = $3 + )` + + var exists bool + if err := s.pool.QueryRow(ctx, query, chatID, position.CreatedAt, position.ID).Scan(&exists); err != nil { + return false, fmt.Errorf("validate message cursor: %w", err) + } + + return exists, nil +} + +func page[T any](items []T, limit int) ([]T, bool, error) { + if limit < 1 { + return nil, false, fmt.Errorf("limit must be positive") + } + if len(items) <= limit { + return items, false, nil + } + + return items[:limit], true, nil +} + +func isUniqueViolation(err error) bool { + var postgresError *pgconn.PgError + return errors.As(err, &postgresError) && postgresError.Code == "23505" +} diff --git a/internal/postgres/store_integration_test.go b/internal/postgres/store_integration_test.go new file mode 100644 index 00000000..469f5a2e --- /dev/null +++ b/internal/postgres/store_integration_test.go @@ -0,0 +1,310 @@ +package postgres_test + +import ( + "context" + "errors" + "os" + "strings" + "sync" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/stretchr/testify/suite" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/database" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/model" + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/postgres" +) + +type PostgresSuite struct { + suite.Suite + pool *pgxpool.Pool + store *postgres.Store +} + +func TestPostgresSuite(t *testing.T) { + suite.Run(t, new(PostgresSuite)) +} + +func (s *PostgresSuite) SetupSuite() { + s.pool = openTestDatabase(s.T()) + s.store = postgres.New(s.pool) +} + +func (s *PostgresSuite) TearDownSuite() { + if s.pool != nil { + s.pool.Close() + } +} + +func (s *PostgresSuite) TestConcurrentUsernameCreation() { + t := s.T() + store := s.store + username := "race_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16] + + const workers = 8 + results := make(chan error, workers) + var group sync.WaitGroup + for range workers { + group.Add(1) + go func() { + defer group.Done() + _, err := store.CreateUser(context.Background(), username, "Race user", "password-hash") + results <- err + }() + } + group.Wait() + close(results) + + var created, conflicts int + for err := range results { + switch { + case err == nil: + created++ + case errors.Is(err, postgres.ErrConflict): + conflicts++ + default: + t.Fatalf("CreateUser() error = %v", err) + } + } + if created != 1 || conflicts != workers-1 { + t.Fatalf("created = %d, conflicts = %d", created, conflicts) + } +} + +func (s *PostgresSuite) TestCreateChatRollsBackForUnknownMember() { + t := s.T() + pool := s.pool + store := s.store + owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash") + if err != nil { + t.Fatalf("CreateUser() error = %v", err) + } + + before := chatCount(t, pool) + _, err = store.CreateChat(context.Background(), "Invalid chat", []string{owner.ID, uuid.NewString()}, owner.ID) + if !errors.Is(err, postgres.ErrInvalidMembers) { + t.Fatalf("CreateChat() error = %v, want ErrInvalidMembers", err) + } + if after := chatCount(t, pool); after != before { + t.Fatalf("chat count changed from %d to %d", before, after) + } +} + +func (s *PostgresSuite) TestMessageIdempotencyReplayAndConflict() { + t := s.T() + pool := s.pool + store := s.store + owner, chatID := createOwnerAndChat(t, store) + key := uuid.NewString() + + first, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", key, 24*time.Hour) + if err != nil || replay { + t.Fatalf("first CreateMessage() = (%v, %t, %v)", first, replay, err) + } + second, replay, err := store.CreateMessage(context.Background(), chatID, owner.ID, "same body", key, 24*time.Hour) + if err != nil || !replay { + t.Fatalf("replay CreateMessage() = (%v, %t, %v)", second, replay, err) + } + if first != second { + t.Fatalf("replay message = %v, want %v", second, first) + } + if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "different body", key, 24*time.Hour); !errors.Is(err, postgres.ErrIdempotencyConflict) { + t.Fatalf("conflicting CreateMessage() error = %v, want ErrIdempotencyConflict", err) + } + if count := messageCount(t, pool, chatID); count != 1 { + t.Fatalf("message count = %d, want 1", count) + } +} + +func (s *PostgresSuite) TestConcurrentMessageIdempotency() { + t := s.T() + pool := s.pool + stores := []*postgres.Store{postgres.New(pool), postgres.New(pool)} + owner, chatID := createOwnerAndChat(t, stores[0]) + key := uuid.NewString() + + const workers = 8 + type result struct { + message model.Message + replay bool + err error + } + results := make(chan result, workers) + var group sync.WaitGroup + for worker := range workers { + group.Add(1) + go func() { + defer group.Done() + message, replay, err := stores[worker%len(stores)].CreateMessage( + context.Background(), chatID, owner.ID, "concurrent body", key, 24*time.Hour, + ) + results <- result{message: message, replay: replay, err: err} + }() + } + group.Wait() + close(results) + + var messageID string + var created int + for result := range results { + if result.err != nil { + t.Fatalf("CreateMessage() error = %v", result.err) + } + if messageID == "" { + messageID = result.message.ID + } + if result.message.ID != messageID { + t.Fatalf("message ID = %s, want %s", result.message.ID, messageID) + } + if !result.replay { + created++ + } + } + if created != 1 { + t.Fatalf("new message results = %d, want 1", created) + } + if count := messageCount(t, pool, chatID); count != 1 { + t.Fatalf("message count = %d, want 1", count) + } +} + +func (s *PostgresSuite) TestMessageIdempotencyScopeAndExpiry() { + t := s.T() + pool := s.pool + store := s.store + owner, chatID := createOwnerAndChat(t, store) + second, err := store.CreateUser(context.Background(), uniqueUsername(), "Second", "password-hash") + if err != nil { + t.Fatalf("CreateUser() error = %v", err) + } + chat, err := store.CreateChat(context.Background(), "Shared", []string{owner.ID, second.ID}, owner.ID) + if err != nil { + t.Fatalf("CreateChat() error = %v", err) + } + key := uuid.NewString() + + if _, _, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "owner", key, 24*time.Hour); err != nil { + t.Fatalf("owner CreateMessage() error = %v", err) + } + if _, _, err := store.CreateMessage(context.Background(), chat.ID, second.ID, "second", key, 24*time.Hour); err != nil { + t.Fatalf("second user CreateMessage() error = %v", err) + } + if _, _, err := store.CreateMessage(context.Background(), chatID, owner.ID, "other chat", key, 24*time.Hour); err != nil { + t.Fatalf("other chat CreateMessage() error = %v", err) + } + if _, err := pool.Exec(context.Background(), ` + UPDATE message_idempotency + SET created_at = clock_timestamp() - INTERVAL '2 days', + expires_at = clock_timestamp() - INTERVAL '1 day' + WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chat.ID, key); err != nil { + t.Fatalf("expire idempotency key: %v", err) + } + message, replay, err := store.CreateMessage(context.Background(), chat.ID, owner.ID, "after expiry", key, 24*time.Hour) + if err != nil || replay || message.Text != "after expiry" { + t.Fatalf("expired CreateMessage() = (%v, %t, %v)", message, replay, err) + } +} + +func (s *PostgresSuite) TestExpiredIdempotencyRecordsAreCleanedOnNewMessages() { + t := s.T() + owner, chatID := createOwnerAndChat(t, s.store) + expiredKey := uuid.NewString() + if _, _, err := s.store.CreateMessage(context.Background(), chatID, owner.ID, "expired", expiredKey, 24*time.Hour); err != nil { + t.Fatalf("create expired message: %v", err) + } + if _, err := s.pool.Exec(context.Background(), ` + UPDATE message_idempotency + SET created_at = clock_timestamp() - INTERVAL '2 days', + expires_at = clock_timestamp() - INTERVAL '1 day' + WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey); err != nil { + t.Fatalf("expire idempotency key: %v", err) + } + if _, _, err := s.store.CreateMessage( + context.Background(), chatID, owner.ID, "new message", uuid.NewString(), 24*time.Hour, + ); err != nil { + t.Fatalf("create new message: %v", err) + } + + var remaining int + if err := s.pool.QueryRow(context.Background(), ` + SELECT count(*) + FROM message_idempotency + WHERE user_id = $1 AND chat_id = $2 AND key = $3`, owner.ID, chatID, expiredKey).Scan(&remaining); err != nil { + t.Fatalf("count expired idempotency records: %v", err) + } + if remaining != 0 { + t.Fatalf("expired idempotency records = %d, want 0", remaining) + } +} + +func createOwnerAndChat(t *testing.T, store *postgres.Store) (model.User, string) { + t.Helper() + owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash") + if err != nil { + t.Fatalf("CreateUser() error = %v", err) + } + chat, err := store.CreateChat(context.Background(), "Chat", []string{owner.ID}, owner.ID) + if err != nil { + t.Fatalf("CreateChat() error = %v", err) + } + return owner, chat.ID +} + +func messageCount(t *testing.T, pool *pgxpool.Pool, chatID string) int { + t.Helper() + var count int + if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM messages WHERE chat_id = $1", chatID).Scan(&count); err != nil { + t.Fatalf("count messages: %v", err) + } + return count +} + +func openTestDatabase(t *testing.T) *pgxpool.Pool { + t.Helper() + url := testSecret(t, "TEST_DATABASE_URL", "TEST_DATABASE_URL_FILE") + pool, err := database.Open(context.Background(), database.Config{ + URL: url, MinConnections: 0, MaxConnections: 10, + MaxConnectionAge: time.Minute, MaxConnectionIdle: time.Minute, + HealthCheckPeriod: time.Minute, ConnectTimeout: 5 * time.Second, + }) + if err != nil { + t.Fatalf("database.Open() error = %v", err) + } + if err := database.Migrate(context.Background(), pool, 30*time.Second); err != nil { + pool.Close() + t.Fatalf("database.Migrate() error = %v", err) + } + return pool +} + +func testSecret(t *testing.T, valueName, fileName string) string { + t.Helper() + if value := strings.TrimSpace(os.Getenv(valueName)); value != "" { + return value + } + path := strings.TrimSpace(os.Getenv(fileName)) + if path == "" { + t.Skipf("set %s or %s to run integration tests", valueName, fileName) + } + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v", fileName, err) + } + return strings.TrimSpace(string(data)) +} + +func uniqueUsername() string { + return "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20] +} + +func chatCount(t *testing.T, pool *pgxpool.Pool) int { + t.Helper() + var count int + if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&count); err != nil { + t.Fatalf("count chats: %v", err) + } + return count +} diff --git a/internal/session/session.go b/internal/session/session.go new file mode 100644 index 00000000..4f40fc53 --- /dev/null +++ b/internal/session/session.go @@ -0,0 +1,161 @@ +package session + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "strings" + "time" + + "github.com/valkey-io/valkey-go" +) + +const ( + tokenLength = 32 + keyPrefix = "session:" +) + +var ( + ErrNotFound = errors.New("session not found") + ErrInvalidToken = errors.New("invalid session token") + ErrInvalidRecord = errors.New("invalid session record") + ErrInvalidTTL = errors.New("invalid session TTL") + ErrTokenGeneration = errors.New("session token generation failed") + ErrStore = errors.New("session store failure") +) + +type record struct { + UserID string `json:"user_id"` + ExpiresAt time.Time `json:"expires_at"` +} + +type Store struct { + client valkey.Client +} + +func New(rawURL string) (*Store, error) { + option, err := valkey.ParseURL(rawURL) + if err != nil { + return nil, fmt.Errorf("%w: parse Valkey URL: %w", ErrStore, err) + } + + client, err := valkey.NewClient(option) + if err != nil { + return nil, fmt.Errorf("%w: create Valkey client: %w", ErrStore, err) + } + + return &Store{client: client}, nil +} + +func (s *Store) Create(ctx context.Context, userID string, ttl time.Duration) (string, time.Time, error) { + if strings.TrimSpace(userID) == "" { + return "", time.Time{}, ErrInvalidRecord + } + if ttl < time.Second { + return "", time.Time{}, ErrInvalidTTL + } + + token, err := newToken() + if err != nil { + return "", time.Time{}, err + } + + record := record{ + UserID: userID, + ExpiresAt: time.Now().UTC().Add(ttl).Truncate(time.Microsecond), + } + payload, err := json.Marshal(record) + if err != nil { + return "", time.Time{}, fmt.Errorf("%w: encode session: %w", ErrStore, err) + } + + command := s.client.B().Set().Key(s.key(token)).Value(valkey.BinaryString(payload)).Ex(ttl).Build() + if err := s.client.Do(ctx, command).Error(); err != nil { + return "", time.Time{}, fmt.Errorf("%w: create session: %w", ErrStore, err) + } + + return token, record.ExpiresAt, nil +} + +func newToken() (string, error) { + random := make([]byte, tokenLength) + if _, err := rand.Read(random); err != nil { + return "", fmt.Errorf("%w: %v", ErrTokenGeneration, err) + } + return base64.RawURLEncoding.EncodeToString(random), nil +} + +func (s *Store) UserID(ctx context.Context, token string) (string, error) { + key, err := s.sessionKey(token) + if err != nil { + return "", err + } + + payload, err := s.client.Do(ctx, s.client.B().Get().Key(key).Build()).AsBytes() + if valkey.IsValkeyNil(err) { + return "", ErrNotFound + } + if err != nil { + return "", fmt.Errorf("%w: get session: %w", ErrStore, err) + } + + var record record + if err := json.Unmarshal(payload, &record); err != nil { + return "", fmt.Errorf("%w: decode session: %w", ErrStore, err) + } + if strings.TrimSpace(record.UserID) == "" || record.ExpiresAt.IsZero() { + return "", fmt.Errorf("%w: %w", ErrStore, ErrInvalidRecord) + } + if !record.ExpiresAt.After(time.Now()) { + return "", ErrNotFound + } + + return record.UserID, nil +} + +func (s *Store) Delete(ctx context.Context, token string) error { + key, err := s.sessionKey(token) + if err != nil { + return err + } + + deleted, err := s.client.Do(ctx, s.client.B().Del().Key(key).Build()).AsInt64() + if err != nil { + return fmt.Errorf("%w: delete session: %w", ErrStore, err) + } + if deleted == 0 { + return ErrNotFound + } + + return nil +} + +func (s *Store) Ping(ctx context.Context) error { + if err := s.client.Do(ctx, s.client.B().Ping().Build()).Error(); err != nil { + return fmt.Errorf("%w: ping Valkey: %w", ErrStore, err) + } + return nil +} + +func (s *Store) Close() error { + s.client.Close() + return nil +} + +func (s *Store) sessionKey(token string) (string, error) { + decoded, err := base64.RawURLEncoding.Strict().DecodeString(token) + if err != nil || len(decoded) != tokenLength { + return "", ErrInvalidToken + } + return s.key(token), nil +} + +func (s *Store) key(token string) string { + digest := sha256.Sum256([]byte(token)) + return keyPrefix + hex.EncodeToString(digest[:]) +} diff --git a/internal/session/session_integration_test.go b/internal/session/session_integration_test.go new file mode 100644 index 00000000..e22a63dd --- /dev/null +++ b/internal/session/session_integration_test.go @@ -0,0 +1,72 @@ +package session_test + +import ( + "context" + "errors" + "os" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/suite" + + "gitea.privcloud.tech/dizi/mtusi2026lab3/internal/session" +) + +type SessionSuite struct { + suite.Suite + store *session.Store +} + +func TestSessionSuite(t *testing.T) { + suite.Run(t, new(SessionSuite)) +} + +func (s *SessionSuite) SetupSuite() { + rawURL := strings.TrimSpace(os.Getenv("TEST_CACHE_URL")) + if rawURL == "" { + path := strings.TrimSpace(os.Getenv("TEST_CACHE_URL_FILE")) + if path == "" { + s.T().Skip("set TEST_CACHE_URL or TEST_CACHE_URL_FILE to run integration tests") + } + data, err := os.ReadFile(path) + s.Require().NoError(err) + rawURL = strings.TrimSpace(string(data)) + } + store, err := session.New(rawURL) + s.Require().NoError(err) + s.store = store +} + +func (s *SessionSuite) TearDownSuite() { + if s.store != nil { + s.Require().NoError(s.store.Close()) + } +} + +func (s *SessionSuite) TestSessionLifecycle() { + userID := uuid.NewString() + token, expiresAt, err := s.store.Create(context.Background(), userID, time.Minute) + s.Require().NoError(err) + s.NotEmpty(token) + s.True(expiresAt.After(time.Now())) + + actualUserID, err := s.store.UserID(context.Background(), token) + s.Require().NoError(err) + s.Equal(userID, actualUserID) + s.Require().NoError(s.store.Delete(context.Background(), token)) + + _, err = s.store.UserID(context.Background(), token) + s.ErrorIs(err, session.ErrNotFound) +} + +func (s *SessionSuite) TestSessionExpires() { + token, _, err := s.store.Create(context.Background(), uuid.NewString(), time.Second) + s.Require().NoError(err) + + s.Eventually(func() bool { + _, err = s.store.UserID(context.Background(), token) + return errors.Is(err, session.ErrNotFound) + }, 3*time.Second, 50*time.Millisecond) +} diff --git a/lab3.env.example b/lab3.env.example new file mode 100644 index 00000000..447b3b72 --- /dev/null +++ b/lab3.env.example @@ -0,0 +1,79 @@ +# Адрес и внутренний порт HTTP-сервера. +APP_HOST=0.0.0.0 +APP_PORT=8080 + +# Адреса публикации сервисов на локальной машине. +API_BIND_ADDRESS=127.0.0.1:8080 +SWAGGER_BIND_ADDRESS=127.0.0.1:8081 +PROMETHEUS_BIND_ADDRESS=127.0.0.1:9090 +GRAFANA_BIND_ADDRESS=127.0.0.1:3000 + +# Время жизни сессии, идемпотентности и используемое хранилище. +SESSION_TTL_SECONDS=3600 +IDEMPOTENCY_TTL_SECONDS=86400 +SESSION_STORE=valkey + +# Число API-реплик за Nginx. +API_REPLICAS=2 + +# Учётные данные локального PostgreSQL и строка подключения API. +POSTGRES_USER=messenger +POSTGRES_DB=messenger +POSTGRES_PASSWORD=local-postgres-password +DATABASE_URL=postgres://messenger:local-postgres-password@postgres:5432/messenger?sslmode=disable + +# Пароль локального Valkey и строка подключения API. +VALKEY_PASSWORD=local-valkey-password +CACHE_URL=redis://default:local-valkey-password@valkey:6379/0 + +# Размер пула и обслуживание соединений PostgreSQL. +DB_MIN_CONNS=1 +DB_MAX_CONNS=10 +DB_MAX_CONNECTION_AGE_SECONDS=1800 +DB_MAX_CONNECTION_IDLE_SECONDS=300 +DB_HEALTH_CHECK_SECONDS=30 + +# Таймауты зависимостей, миграций и HTTP-запросов. +DEPENDENCY_TIMEOUT_SECONDS=5 +MIGRATION_TIMEOUT_SECONDS=30 +REQUEST_TIMEOUT_SECONDS=5 +HTTP_READ_HEADER_TIMEOUT_SECONDS=5 +HTTP_READ_TIMEOUT_SECONDS=10 +HTTP_WRITE_TIMEOUT_SECONDS=10 +HTTP_IDLE_TIMEOUT_SECONDS=60 +HTTP_SHUTDOWN_TIMEOUT_SECONDS=5 + +# Параметры Argon2id для новых хешей паролей. +PASSWORD_MEMORY_KIB=65536 +PASSWORD_ITERATIONS=3 +PASSWORD_PARALLELISM=2 +PASSWORD_SALT_LENGTH=16 +PASSWORD_KEY_LENGTH=32 + +# Порт Swagger внутри конфигурации CORS API. +SWAGGER_PORT=8081 + +# Ограничения ресурсов контейнеров. +API_CPUS=1 +API_MEMORY=256m +MIGRATE_CPUS=0.5 +MIGRATE_MEMORY=128m +NGINX_CPUS=0.5 +NGINX_MEMORY=128m +SWAGGER_CPUS=0.25 +SWAGGER_MEMORY=128m +TEST_CPUS=2 +TEST_MEMORY=512m +EXPORTER_CPUS=0.25 +EXPORTER_MEMORY=128m +PROMETHEUS_CPUS=1 +PROMETHEUS_MEMORY=512m +GRAFANA_CPUS=0.5 +GRAFANA_MEMORY=256m +POSTGRES_CPUS=2 +POSTGRES_MEMORY=1g +VALKEY_CPUS=1 +VALKEY_MEMORY=256m + +# Пароль локального администратора Grafana. +GRAFANA_ADMIN_PASSWORD=local-grafana-password diff --git a/migrations/0001_initial.sql b/migrations/0001_initial.sql new file mode 100644 index 00000000..2f9b90b4 --- /dev/null +++ b/migrations/0001_initial.sql @@ -0,0 +1,50 @@ +CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), + CONSTRAINT users_username_unique UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100), + CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '[^[:space:]]'), + CONSTRAINT users_password_hash_length CHECK (char_length(password_hash) BETWEEN 1 AND 1024), + CONSTRAINT users_password_hash_not_blank CHECK (password_hash ~ '[^[:space:]]') +); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), + CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100), + CONSTRAINT chats_title_not_blank CHECK (title ~ '[^[:space:]]') +); + +CREATE INDEX chats_created_at_id_idx ON chats (created_at, id); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL, + user_id UUID NOT NULL, + position SMALLINT NOT NULL, + CONSTRAINT chat_members_pk PRIMARY KEY (chat_id, user_id), + CONSTRAINT chat_members_chat_position_unique UNIQUE (chat_id, position), + CONSTRAINT chat_members_position_range CHECK (position BETWEEN 0 AND 99), + CONSTRAINT chat_members_chat_fk FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE, + CONSTRAINT chat_members_user_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE +); + +CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL, + sender_id UUID NOT NULL, + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), + CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000), + CONSTRAINT messages_text_not_blank CHECK (text ~ '[^[:space:]]'), + CONSTRAINT messages_sender_membership_fk FOREIGN KEY (chat_id, sender_id) + REFERENCES chat_members (chat_id, user_id) ON DELETE CASCADE +); + +CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id); diff --git a/migrations/0002_message_idempotency.sql b/migrations/0002_message_idempotency.sql new file mode 100644 index 00000000..91443a48 --- /dev/null +++ b/migrations/0002_message_idempotency.sql @@ -0,0 +1,19 @@ +CREATE TABLE message_idempotency ( + user_id UUID NOT NULL, + chat_id UUID NOT NULL, + key TEXT NOT NULL, + request_hash BYTEA NOT NULL, + message_id UUID NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(), + expires_at TIMESTAMPTZ NOT NULL, + CONSTRAINT message_idempotency_pk PRIMARY KEY (user_id, chat_id, key), + CONSTRAINT message_idempotency_user_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_chat_fk FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE, + CONSTRAINT message_idempotency_message_fk FOREIGN KEY (message_id) REFERENCES messages (id) + ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED, + CONSTRAINT message_idempotency_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'), + CONSTRAINT message_idempotency_request_hash_length CHECK (octet_length(request_hash) = 32), + CONSTRAINT message_idempotency_expiry CHECK (expires_at > created_at) +); + +CREATE INDEX message_idempotency_expires_at_idx ON message_idempotency (expires_at); diff --git a/migrations/embed.go b/migrations/embed.go new file mode 100644 index 00000000..08bbf60c --- /dev/null +++ b/migrations/embed.go @@ -0,0 +1,8 @@ +package migrations + +import "embed" + +// FS contains database migrations applied by the application at startup. +// +//go:embed *.sql +var FS embed.FS diff --git a/monitoring/grafana/dashboards/messenger.json b/monitoring/grafana/dashboards/messenger.json new file mode 100644 index 00000000..ad0b521a --- /dev/null +++ b/monitoring/grafana/dashboards/messenger.json @@ -0,0 +1,99 @@ +{ + "annotations": {"list": []}, + "editable": false, + "graphTooltip": 1, + "panels": [ + { + "type": "timeseries", "title": "API requests per second", "id": 1, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}, + "targets": [{"expr": "sum(rate(messenger_http_requests_total[1m]))", "legendFormat": "total", "refId": "A"}] + }, + { + "type": "timeseries", "title": "API RPS by replica", "id": 2, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0}, + "targets": [{"expr": "sum by (instance) (rate(messenger_http_requests_total[1m]))", "legendFormat": "{{instance}}", "refId": "A"}] + }, + { + "type": "timeseries", "title": "API responses by status class", "id": 3, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}, + "targets": [{"expr": "sum by (status_class) (rate(messenger_http_requests_total[1m]))", "legendFormat": "{{status_class}}", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Healthy API replicas", "id": 4, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8}, + "targets": [{"expr": "sum(up{job=\"messenger\"})", "legendFormat": "replicas", "refId": "A"}] + }, + { + "type": "timeseries", "title": "API latency p95 by replica and route", "id": 5, + "gridPos": {"h": 8, "w": 24, "x": 0, "y": 16}, + "fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}, + "targets": [{"expr": "histogram_quantile(0.95, sum by (le, instance, route) (rate(messenger_http_request_duration_seconds_bucket[5m])))", "legendFormat": "{{instance}} {{route}}", "refId": "A"}] + }, + { + "type": "timeseries", "title": "API in flight", "id": 6, + "gridPos": {"h": 8, "w": 8, "x": 0, "y": 24}, + "targets": [{"expr": "messenger_http_requests_in_flight", "legendFormat": "{{instance}}", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Go heap by replica", "id": 7, + "gridPos": {"h": 8, "w": 8, "x": 8, "y": 24}, + "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, + "targets": [{"expr": "go_memstats_heap_alloc_bytes{job=\"messenger\"}", "legendFormat": "{{instance}}", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Idempotency replays", "id": 8, + "gridPos": {"h": 8, "w": 8, "x": 16, "y": 24}, + "targets": [{"expr": "sum(rate(messenger_http_idempotency_replays_total[1m]))", "legendFormat": "replays/s", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Nginx connections and requests", "id": 9, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 32}, + "targets": [ + {"expr": "nginx_connections_active", "legendFormat": "active connections", "refId": "A"}, + {"expr": "rate(nginx_http_requests_total[1m])", "legendFormat": "requests/s", "refId": "B"} + ] + }, + { + "type": "timeseries", "title": "PostgreSQL connections", "id": 10, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 32}, + "targets": [{"expr": "sum(pg_stat_activity_count{datname=\"messenger\"})", "legendFormat": "connections", "refId": "A"}] + }, + { + "type": "timeseries", "title": "PostgreSQL transactions", "id": 11, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 40}, + "targets": [ + {"expr": "rate(pg_stat_database_xact_commit{datname=\"messenger\"}[1m])", "legendFormat": "commit/s", "refId": "A"}, + {"expr": "rate(pg_stat_database_xact_rollback{datname=\"messenger\"}[1m])", "legendFormat": "rollback/s", "refId": "B"} + ] + }, + { + "type": "timeseries", "title": "PostgreSQL database size", "id": 12, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 40}, + "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, + "targets": [{"expr": "pg_database_size_bytes{datname=\"messenger\"}", "legendFormat": "database", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Valkey memory", "id": 13, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 48}, + "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, + "targets": [{"expr": "redis_memory_used_bytes", "legendFormat": "used", "refId": "A"}] + }, + { + "type": "timeseries", "title": "Valkey keys and commands", "id": 14, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 48}, + "targets": [ + {"expr": "sum(redis_db_keys)", "legendFormat": "keys", "refId": "A"}, + {"expr": "rate(redis_commands_processed_total[1m])", "legendFormat": "commands/s", "refId": "B"} + ] + } + ], + "refresh": "5s", + "schemaVersion": 41, + "tags": ["highload", "lab3", "nginx", "failover"], + "templating": {"list": []}, + "time": {"from": "now-15m", "to": "now"}, + "timezone": "browser", + "title": "Messenger lab3 overview", + "uid": "messenger-overview", + "version": 2 +} diff --git a/monitoring/grafana/provisioning/dashboards/messenger.yml b/monitoring/grafana/provisioning/dashboards/messenger.yml new file mode 100644 index 00000000..fa7934cb --- /dev/null +++ b/monitoring/grafana/provisioning/dashboards/messenger.yml @@ -0,0 +1,10 @@ +apiVersion: 1 + +providers: + - name: Messenger + folder: Highload + type: file + disableDeletion: true + editable: false + options: + path: /etc/grafana/dashboards diff --git a/monitoring/grafana/provisioning/datasources/prometheus.yml b/monitoring/grafana/provisioning/datasources/prometheus.yml new file mode 100644 index 00000000..8704d9e5 --- /dev/null +++ b/monitoring/grafana/provisioning/datasources/prometheus.yml @@ -0,0 +1,10 @@ +apiVersion: 1 + +datasources: + - name: Prometheus + type: prometheus + uid: prometheus + access: proxy + url: http://prometheus:9090 + isDefault: true + editable: false diff --git a/monitoring/prometheus.yml b/monitoring/prometheus.yml new file mode 100644 index 00000000..2c0881ea --- /dev/null +++ b/monitoring/prometheus.yml @@ -0,0 +1,23 @@ +global: + scrape_interval: 5s + evaluation_interval: 5s + +scrape_configs: + - job_name: messenger + dns_sd_configs: + - names: ["api"] + type: A + port: 8080 + refresh_interval: 2s + + - job_name: nginx + static_configs: + - targets: ["nginx-exporter:9113"] + + - job_name: postgres + static_configs: + - targets: ["postgres-exporter:9187"] + + - job_name: valkey + static_configs: + - targets: ["valkey-exporter:9121"] diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 00000000..9dcebe8f --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,55 @@ +user nginx; +worker_processes auto; +error_log /dev/stderr notice; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + +http { + resolver 127.0.0.11 valid=2s ipv6=off; + + log_format structured escape=json + '{"time":"$time_iso8601","request_id":"$http_x_request_id",' + '"method":"$request_method","uri":"$request_uri","status":$status,' + '"request_time":$request_time,"upstream":"$upstream_addr",' + '"upstream_status":"$upstream_status","upstream_time":"$upstream_response_time"}'; + + access_log /dev/stdout structured; + + upstream messenger { + zone messenger 64k; + server api:8080 resolve max_fails=1 fail_timeout=3s; + keepalive 32; + } + + server { + listen 8080; + + location / { + proxy_pass http://messenger; + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + proxy_connect_timeout 1s; + proxy_send_timeout 10s; + proxy_read_timeout 10s; + proxy_next_upstream error timeout http_502 http_503 http_504; + proxy_next_upstream_tries 2; + proxy_next_upstream_timeout 3s; + } + } + + server { + listen 8081; + access_log off; + + location = /stub_status { + stub_status; + } + } +} diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..cacb5215 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +docker compose stop nginx api valkey postgres +docker compose start postgres valkey +docker compose run --rm migrate +docker compose up -d --wait --wait-timeout 90 diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..e32e2973 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +set -eu + +wait_for_replicas() { + expected=$1 + deadline=$(($(date +%s) + 90)) + + while [ "$(date +%s)" -lt "$deadline" ]; do + containers=$(docker compose ps -q api) + count=0 + healthy=0 + for container in $containers; do + count=$((count + 1)) + status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container") + if [ "$status" = "healthy" ]; then + healthy=$((healthy + 1)) + fi + done + if [ "$count" -eq "$expected" ] && [ "$healthy" -eq "$expected" ]; then + return 0 + fi + sleep 1 + done + + printf '%s\n' "API replicas did not become healthy: expected $expected" >&2 + return 1 +} + +old_containers=$(docker compose ps -q api) +old_count=0 +for container in $old_containers; do + old_count=$((old_count + 1)) +done + +docker compose build +docker compose up -d postgres valkey +docker compose run --rm migrate + +if [ "$old_count" -lt 2 ]; then + docker compose up -d --scale api=2 --wait --wait-timeout 120 + exit 0 +fi + +docker compose up -d --no-deps --no-recreate --scale api=3 api +wait_for_replicas 3 + +for container in $old_containers; do + docker stop --time 6 "$container" >/dev/null + docker rm "$container" >/dev/null + docker compose up -d --no-deps --no-recreate --scale api=3 api + wait_for_replicas 3 +done + +docker compose up -d --no-deps --no-recreate --scale api=2 api +wait_for_replicas 2 +docker compose up -d --no-build --scale api=2 --wait --wait-timeout 120 diff --git a/scripts/run-scale-experiment.sh b/scripts/run-scale-experiment.sh new file mode 100755 index 00000000..092713ca --- /dev/null +++ b/scripts/run-scale-experiment.sh @@ -0,0 +1,104 @@ +#!/bin/sh + +set -eu + +duration=${DURATION:-15s} +warmup=${WARMUP:-3s} +concurrencies=${CONCURRENCIES:-"8 32 64"} +repeats=${REPEATS:-3} +current_project= + +restore_default_stack() { + if [ -n "$current_project" ]; then + env COMPOSE_PROJECT_NAME="$current_project" docker compose --profile research down -v --remove-orphans || true + fi + docker compose up -d --scale api=2 --wait --wait-timeout 120 +} + +run_configuration() { + series=$1 + replicas=$2 + api_cpus=$3 + api_memory=$4 + db_max_conns=$5 + repeat=$6 + point_concurrencies=$7 + project="lab3exp_${series}_r${replicas}_n${repeat}" + + pending=false + for concurrency in $point_concurrencies; do + label="${series}-r${replicas}-c${concurrency}-n${repeat}" + if [ ! -s "evidence/lab3-${label}.json" ]; then + pending=true + fi + done + if [ "$pending" = false ]; then + echo "Skipping completed configuration ${series}-r${replicas}-n${repeat}" + return + fi + + current_project=$project + + compose() { + env \ + COMPOSE_PROJECT_NAME="$project" \ + API_REPLICAS="$replicas" \ + API_CPUS="$api_cpus" \ + API_MEMORY="$api_memory" \ + DB_MIN_CONNS=0 \ + DB_MAX_CONNS="$db_max_conns" \ + docker compose "$@" + } + + compose up -d --scale api="$replicas" --wait --wait-timeout 120 \ + postgres valkey migrate api nginx postgres-exporter valkey-exporter nginx-exporter prometheus + for concurrency in $point_concurrencies; do + label="${series}-r${replicas}-c${concurrency}-n${repeat}" + output="lab3-${label}.json" + if [ -s "evidence/$output" ]; then + echo "Skipping completed point $label" + continue + fi + compose --profile research run --rm experiment \ + /app/lab3experiment \ + -base-url=http://nginx:8080 \ + -prometheus-url=http://prometheus:9090 \ + -label="$label" \ + -duration="$duration" \ + -warmup="$warmup" \ + -concurrency="$concurrency" \ + -replicas="$replicas" \ + -api-cpus="$api_cpus" \ + -api-memory="$api_memory" \ + -db-max-conns="$db_max_conns" \ + -output="/output/$output" + done + compose --profile research down -v --remove-orphans + current_project= +} + +trap restore_default_stack EXIT INT TERM +docker compose build api nginx swagger prometheus grafana +docker compose stop + +repeat=1 +while [ "$repeat" -le "$repeats" ]; do + run_configuration fixed 1 2 1g 16 "$repeat" "$concurrencies" + run_configuration fixed 2 1 512m 8 "$repeat" "$concurrencies" + run_configuration fixed 4 0.5 256m 4 "$repeat" "$concurrencies" + + run_configuration growing 1 1 256m 8 "$repeat" "$concurrencies" + run_configuration growing 2 1 256m 8 "$repeat" "$concurrencies" + run_configuration growing 4 1 256m 8 "$repeat" "$concurrencies" + + run_configuration pool-baseline 4 0.5 256m 10 "$repeat" "64" + run_configuration pool-tuned 4 0.5 256m 4 "$repeat" "64" + repeat=$((repeat + 1)) +done + +docker compose --profile research run --no-deps --rm experiment \ + /app/lab3summary \ + -input=/output \ + -output=/output/lab3-scale-results.json \ + -chart=/output/lab3-scale-rps.svg +rm -f evidence/lab3-fixed-*.json evidence/lab3-growing-*.json evidence/lab3-pool-*.json diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..5327251e --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +set -eu + +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" + +for container in $(docker compose ps -q api); do + instance=$(docker inspect --format '{{.Config.Hostname}}' "$container") + if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then + docker stop --time 6 "$container" >/dev/null + exit 0 + fi +done + +printf '%s\n' "API instance not found: $TARGET_INSTANCE_ID" >&2 +exit 1