diff --git a/.dockerignore b/.dockerignore index bc16e556..10b156aa 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,4 @@ __pycache__ node_modules evidence .DS_Store +app/tmp diff --git a/.env.example b/.env.example index 714c8076..d6d33e62 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,27 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# compose.yaml reads these values. Only POSTGRES_PASSWORD is a secret. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url +DB_POOL_MAX_CONNS=10 + +# Local PostgreSQL. The API receives a full DATABASE_URL built by compose.yaml. +POSTGRES_USER=messenger +POSTGRES_PASSWORD=replace-with-a-local-password +POSTGRES_DB=messenger + +# Host port for the API. 8081 is used only by the optional short-TTL check. +APP_PORT_PUBLISH=8080 +APP_SHORT_TTL_PORT=8081 + +# Redis is the session store (lab 2 grade 4); PostgreSQL stays the source of +# truth for users, chats, memberships and messages. +SESSION_STORE=redis + +# Optional overrides instead of the compose-generated URLs. A *_FILE value is +# read from a file, which is how Docker/Compose secrets are usually mounted. +# DATABASE_URL=postgres://messenger:password@localhost:5432/messenger?sslmode=disable +# DATABASE_URL_FILE=/run/secrets/database_url +# CACHE_URL=redis://localhost:6379/0 +# CACHE_URL_FILE=/run/secrets/cache_url diff --git a/.gitignore b/.gitignore index b5a71896..a5f180ae 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. + +# Local application builds +app/tmp/ +app/server diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..437ea936 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# syntax=docker/dockerfile:1.7 +# AGENT: Two-stage build. The final image contains only the static binary and a +# CA bundle, runs as a non-root user and keeps the server as PID 1 for signals. +FROM golang:1.27.1-alpine AS build + +WORKDIR /src +COPY app/go.mod app/go.sum ./ +RUN go mod download +COPY app/ ./ +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server + +FROM alpine:3.22 + +RUN addgroup -S -g 10001 app && adduser -S -u 10001 -G app app + +COPY --from=build /out/server /usr/local/bin/server + +USER app +EXPOSE 8080 +ENTRYPOINT ["/usr/local/bin/server"] diff --git a/REPORT.md b/REPORT.md index e42f2b05..e8d0db29 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,182 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Булла Максим, БПИ2301 +- Целевая оценка: 5, трек 2 +- Commit/PR: локальный тег ``31194c0`` +- Использованные источники, библиотеки и помощь ИИ: Go 1.27.1, Fiber v3.5.0, pgx v5.11.0, go-redis v9.23.0, Argon2id из `golang.org/x/crypto`; PostgreSQL 16.10, Redis 7.4.7, `pg_dump`/`pg_restore`/`pgbench`. DeepSeek V4.1 Flash + Opencode Harness для генерации решений; все заявленные результаты получены командами из таблицы ниже и сохранены в `evidence/` и `research/results/`. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требования: Docker с Compose v2, свободный порт `8080` (и `8081` только для проверки короткого TTL). Проверено на Docker 29.3.0 и Compose 5.6.0; для приложения нужен Go только при локальной сборке, в Compose сборка идёт внутри образа `golang:1.27.1-alpine`. + +```sh +git clone && cd mtusi2026lab2 +cp .env.example .env +# заменить POSTGRES_PASSWORD на локальный пароль; .env не коммитится +docker compose --env-file .env up -d --build --wait +curl -i http://localhost:8080/health/ready # ожидается 200 {"status":"ok"} + +make check +make test +make test-persistence ACTION_SCRIPT=scripts/restart.sh +``` + +Если API запущен на другом хосте (например, Docker daemon на хосте, а тесты в отдельной песочнице), адрес передаётся явно, менять `Makefile` не нужно: + +```sh +make test BASE_URL=http://:8080 +make test-persistence BASE_URL=http://:8080 ACTION_SCRIPT=scripts/restart.sh +``` + +Для проверки истечения с коротким TTL поднимается второй экземпляр API с тем же PostgreSQL и Redis: + +```sh +docker compose --env-file .env --profile checks up -d --wait api-short-ttl +python3 scripts/grade4_checks.py \ + --base-url http://localhost:8080 \ + --short-ttl-url http://localhost:8081 +``` + +Сборка и схема: + +- API собирается в два этапа (`Dockerfile`): статический бинарник на `golang:1.27.1-alpine`, запуск от непривилегированного пользователя на `alpine:3.22`. +- Миграции встроены в бинарник и выполняются при старте API под `pg_advisory_lock`; версия пишется в `schema_migrations`. Повторный запуск не дублирует данные, повторное применение с нуля проверено удалением собственных volumes. +- Compose создаёт named volumes `mtusi2026lab2_pgdata` и `mtusi2026lab2_redisdata`. Секреты и volumes не в Git: `.env` и `volumes/` в `.gitignore`, `.env.example` содержит только заполнители. + +Остановка и удаление учебных данных: + +```sh +docker compose --env-file .env down # остановить, volumes сохранить +docker compose --env-file .env down -v # осознанно удалить синтетические данные +``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +```text +клиент --HTTP--> API (Go/Fiber) --SQL--> PostgreSQL 16 + \--RESP--> Redis 7 (sessions, AOF + named volume) +``` + +- **Источник истины для бизнес-данных** — PostgreSQL: `users`, `chats`, `chat_members`, `messages`. В памяти процесса нет пользовательских данных, поэтому API можно перезапускать и (в следующих лабах) реплицировать без потери подтверждённых записей. +- **Источник истины для сессий** — Redis. API не хранит сессии локально. Токен — 32 случайных байта (`crypto/rand`, 256 бит), в Redis лежит только `SHA-256(token) -> user_id` с TTL. По дампу Redis нельзя восстановить действующий bearer-токен. +- **Срок жизни сессии** задаёт `SESSION_TTL_SECONDS` (по умолчанию 3600). При каждом запросе TTL не продлевается. `DELETE /auth/sessions/current` удаляет ключ текущего токена; повторный запрос с ним получает `401`. +- **Перезапуск Redis.** Включён AOF (`appendonly yes`, `appendfsync everysec`) и named volume. Абсолютные сроки жизни ключей сохраняются в AOF, поэтому после graceful-перезапуска действующие сессии остаются валидными, а истёкшие — нет. При жёстком `kill -9` возможна потеря последней секунды записей (не более, чем допускает `everysec`); это ограничение указано в разделе «Ограничения». Если удалить volume Redis, сессии пропадут — приложение не подменяет их анонимным пользователем. +- **Пароли.** Argon2id: `m=65536 KiB, t=2, p=4`, случайная соль 16 байт, сравнение в постоянное время. Хеш и параметры хранятся в `password_hash`; в JSON-ответах поля нет. Argon2id выбран вместо bcrypt, потому что контракт разрешает пароль до 128 Unicode code points, а bcrypt молча обрезает длинные пароли. +- **Basic только на входе.** `POST /api/v1/auth/sessions` — единственный endpoint с Basic. Все бизнес-endpoint принимают только `Authorization: Bearer`. `X-User-Id` нигде не читается и не влияет на владельца сессии. +- **Транзакционные границы.** Создание пользователя — один `INSERT`; уникальность username обеспечивает constraint `users_username_key`, гонка двух регистраций даёт один `201` и остальные `409`. Создание чата вместе со всеми `chat_members` выполняется в одной транзакции: неизвестный участник откатывает и чат, и членство. Отправка сообщения — один `INSERT ... SELECT ... WHERE EXISTS (членство)`, поэтому проверка доступа и запись атомарны. +- **Ключи и индексы.** Первичные ключи UUID, внешние ключи с `ON DELETE RESTRICT/CASCADE`, уникальный индекс по `users.username`. История использует keyset-пагинацию по `(created_at, id)` и индекс `messages_chat_created_id_idx (chat_id, created_at, id)`; запрос не делает `OFFSET` и не читает историю с начала. +- **Пулы и таймауты.** `pgxpool`: не более 10 соединений (`DB_POOL_MAX_CONNS`), `MinConns=1`, connect timeout 3 с, таймаут запроса 5 с. Redis: pool 10, dial/read/write 2 с, без автоматических повторов. HTTP: read/write 10 с, idle 60 с, body limit 1 МиБ. Все обращения к зависимостям ограничены контекстом. +- **Ошибки и health.** Все ошибки идут через `common.AppError`/`SendError` и сохраняют форму `{"error":{"code","message","request_id"}}`; `X-Request-Id` выставляет middleware на каждый ответ, включая 404, 401, 204 и health. `/health/live` отвечает 200, пока жив процесс. `/health/ready` проверяет PostgreSQL и Redis и возвращает 503 в общей форме ошибки, если хотя бы один недоступен. При недоступном Redis защищённый запрос получает ограниченный `503 unavailable`, а не `401` и не доступ без пользователя. + +Переменные окружения: + +| Переменная | Назначение | Значение по умолчанию | +| --- | --- | --- | +| `APP_HOST`, `APP_PORT` | адрес HTTP-сервера | `0.0.0.0`, `8080` | +| `SESSION_TTL_SECONDS` | TTL сессии в секундах | `3600` | +| `DATABASE_URL` / `DATABASE_URL_FILE` | строка подключения к PostgreSQL | обязательна | +| `CACHE_URL` / `CACHE_URL_FILE` | URL Redis | обязательна | +| `SESSION_STORE` | выбранное хранилище сессий, допускается только `redis` | `redis` | +| `DB_POOL_MAX_CONNS` | максимум соединений pgxpool | `10` | +| `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_DB` | параметры локального контейнера PostgreSQL; Compose собирает из них `DATABASE_URL` | заполняются в `.env` | +| `APP_PORT_PUBLISH`, `APP_SHORT_TTL_PORT` | опубликованные порты основного API и проверки TTL | `8080`, `8081` | ## Доказательства критериев +Все команды выполнялись из корня репозитория; для стенда в этой среде использовался адрес `http://172.18.0.1:8080` (sandbox → опубликованный порт Docker-хоста). В обычном локальном запуске достаточно `http://localhost:8080`, поэтому `BASE_URL` в примерах ниже — параметр, а не изменённый default. + | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Структура и JSON репозитория | `make check` | `PASS: offline skeleton structure...` | `evidence/01-make-check.txt` | +| Сборка, vet, unit-тесты | `cd app && go build ./... && go vet ./... && go test ./...` | сборка без ошибок, тесты Argon2id и cursor проходят | `evidence/02-go-build-test.txt` | +| Полная OpenAPI/JSON-Schema валидация | `make validate PYTHON=.venv/bin/python` | `PASS: full OpenAPI 3.1...` | `evidence/03-make-validate.txt` | +| Compose воспроизводим | `docker compose --env-file .env config --quiet` | код 0; services `redis postgres api`, volumes `pgdata redisdata` | `evidence/04-compose.txt` | +| Публичный smoke | `make test BASE_URL=...` | `PASS: lab2 public API scenario` | `evidence/05-smoke.txt` | +| PostgreSQL: ключи, FK, уникальность, check-ограничения | `\d users`, `\d chats`, `\d chat_members`, `\d messages` | PK/FK/unique/check присутствуют | `evidence/11-schema.txt` | +| Миграции с нуля и повторный запуск | `down -v` только для собственного проекта, затем `up`; перезапуски | `schema_migrations` содержит одну версию; данные не дублируются | `evidence/04-compose.txt`, `evidence/11-schema.txt`, `evidence/06-persistence.txt` | +| Атомарное создание чата | `python3 scripts/grade4_checks.py` | неизвестный member → 400, частичного чата нет | `evidence/07-grade4-checks.txt` | +| Пароли — password KDF, не в ответах/logs | unit-тест + smoke + код | Argon2id, поле `password_hash` только в БД | `evidence/02-go-build-test.txt`, `evidence/05-smoke.txt` | +| Basic только для сессии, Bearer для бизнеса | smoke | Basic на `/users/me` → 401; Basic на `/auth/sessions` → 201 | `evidence/05-smoke.txt` | +| `X-User-Id` игнорируется | `grade4_checks.py` | заголовок не меняет пользователя и сам по себе не даёт доступ | `evidence/07-grade4-checks.txt` | +| TTL, logout, неверный пароль | `grade4_checks.py` (TTL=2 с) | истечение → 401; logout → 204, повтор → 401; неверный пароль → 401 + Basic challenge | `evidence/07-grade4-checks.txt` | +| Переживание рестарта API и хранилищ с volumes | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | `PASS`, старый токен читает те же данные; `StartedAt` всех трёх контейнеров изменился, volumes на месте | `evidence/06-persistence.txt` | +| Redis/Valkey реально используется для сессий с TTL | compose + `grade4_checks.py` | ключи с TTL, AOF-файлы в named volume; после остановки/старта Redis старый токен работает | `evidence/06-persistence.txt`, `evidence/07-grade4-checks.txt`, `evidence/10-final-state.txt` | +| Параметризованные запросы, bounded pool, таймауты | ревью `app/internal/store/store.go`, `app/internal/session/store.go`, `app/internal/config/config.go` | `$1..$n`, `MaxConns=10`, контекстные таймауты | `evidence/02-go-build-test.txt` + исходники | +| Гонка регистрации username | `grade4_checks.py` | 12 одновременных запросов → один `201`, одиннадцать `409` | `evidence/07-grade4-checks.txt` | +| Rollback составной операции | `grade4_checks.py` | после ошибки чат с тем же title отсутствует в списке | `evidence/07-grade4-checks.txt` | +| Keyset-пагинация и индекс, доказательство на ≥10 000 сообщений | `./scripts/seed_and_explain.sh` | `Index Scan using messages_chat_created_id_idx`, 5–6 shared buffers, ~0,09–0,10 мс | `evidence/08-explain.txt` | +| Трек 2: выбор индекса при ограниченной памяти | `./research/bench_indexes.sh` | 300k сообщений; без индекса истории `chat_messages` 57,6 мс вместо 0,43 мс; без индекса членства `user_chats` 7,4 мс вместо 1,4 мс | `research/results/indexes.md` | +| Трек 2: backup → restore и проверка | `./research/bench_recovery.sh` + `python3 research/analyze.py` | 100k/200k/300k: dump 1,02/1,92/2,66 с, restore 1,44/2,36/3,28 с; counts и MD5-отпечаток сообщений совпали | `research/results/recovery.csv`, `research/results/recovery.md` | +| Автопроверки expiry/logout/wrong password/Redis/PostgreSQL outage | `grade4_checks.py` | все пункты `PASS` | `evidence/07-grade4-checks.txt` | +| Redis недоступен: live 200, ready 503, protected 503 | `grade4_checks.py` (stop/start Redis) | live=200, ready=503, protected=503; после старта тот же токен и сообщение целы | `evidence/07-grade4-checks.txt` | +| PostgreSQL недоступен: live 200, ready 503, protected 503 | `grade4_checks.py` (stop/start PostgreSQL) | live=200, ready=503, protected=503; сессия и данные пережили отказ | `evidence/07-grade4-checks.txt` | +| Форма ошибок и `X-Request-Id` во всех ответах | проверки `tests/client.py` внутри smoke/grade4 | каждый ответ содержит непустой `X-Request-Id`, `request_id` совпадает с заголовком | `evidence/05-smoke.txt`, `evidence/07-grade4-checks.txt` | +| Конфигурация и версии зафиксированы | `evidence/00-environment.txt`, `compose.yaml`, `Dockerfile` | образы `postgres:16.10-alpine`, `redis:7.4.7-alpine`, `golang:1.27.1-alpine`, `alpine:3.22` | `evidence/00-environment.txt` | +| Секреты и volumes не в Git | (описаны в evidence) | `.env` и volumes отсутствуют в индексе | `evidence/12-git-hygiene.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Аппаратные условия: 4 vCPU, 7,5 GiB RAM, Docker daemon на хосте, один экземпляр API и по одному контейнеру PostgreSQL/Redis. Все цифры — локальный учебный стенд, не production-измерение. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +### План запроса истории + +В отдельном чате создано ровно 10 000 синтетических сообщений (`scripts/seed_10k.sql`). Запрос повторяет SQL API: `WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT 51`. + +| Страница | План | Buffers | Execution time | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Первая (cursor в начале) | `Index Scan using messages_chat_created_id_idx` | shared hit=6 | 0,104 мс | +| Глубокая (cursor на 5000-й секунде) | `Index Scan using messages_chat_created_id_idx` | shared hit=5 | 0,087 мс | + +План не зависит от номера страницы: позиция ищется по индексу, а не перебором предыдущих строк. Сырой вывод — `evidence/08-explain.txt`. + +### Нагрузка + +`tests/load.py` — closed-loop генератор POST-сообщений (client-bound, без retry), поэтому это не доказательство максимальной пропускной способности. + +| Профиль | Успешных | Ошибок | RPS | p50 / p95 / p99 | +| --- | --- | --- | --- | --- | +| 10 с, concurrency 4, один API | 8854 | 0 | 877,6 | 4,06 / 7,84 / 10,65 мс | + +Сырые метрики — `evidence/load.json`, команда и вывод — `evidence/09-load.txt`. + +### Трек 2: индекс и восстановление + +Гипотеза: при 300k сообщений индекс истории полезнее индекса членства, поэтому при жёстком бюджете памяти первым стоит убрать `chat_members_user_idx`. Время восстановления должно расти линейно с числом сообщений. + +Стенд: PostgreSQL 16.10, 5 000 чатов, 2 000 пользователей, 72 300 членств и 300k сообщений с перекошенными размерами чатов (крупнейший чат — около 6% истории). Схема создана миграцией `0001_init.up.sql`. В двух сравниваемых состояниях ровно один из двух индексов; состояние проверяется скриптом, в конце оба индекса возвращаются. Запросы: список чатов пользователя, keyset-страница сообщений одного чата и keyset-лента сообщений по всем чатам пользователя. В таблице — медиана пяти `EXPLAIN (ANALYZE, BUFFERS)`. + +| Состояние | `user_chats`, мс | `chat_messages`, мс | `user_feed`, мс | +| --- | ---: | ---: | ---: | +| оба индекса | 1,38 | 0,43 | 35,40 | +| только `messages_chat_created_id_idx` | 7,36 | 0,47 | 35,22 | +| только `chat_members_user_idx` | 1,40 | 57,63 | 80,40 | + +Размеры: `chat_members_user_idx` — около 3,5 МБ, `messages_chat_created_id_idx` — около 17 МБ. `pgbench` на клонированной таблице (4 клиента, 10 с) дал 2 719 tps с индексом истории и 2 640 tps без него; разница в пределах шума. Без индекса истории страница чата замедляется примерно в 135 раз, а лента — в 2,3 раза; без индекса членства список чатов замедляется примерно в 5 раз. **Вывод: сохранять `messages_chat_created_id_idx`; при необходимости удалять `chat_members_user_idx`.** + +Backup → restore выполнялся с обоими индексами на 100k, 200k и 300k сообщениях; после восстановления сверялись количество сообщений/членств, MD5-отпечаток всех сообщений и наличие обоих индексов. + +| Сообщений | dump, с | restore, с | dump, МиБ | БД, МиБ | потеря при 878 RPS | +| ---: | ---: | ---: | ---: | ---: | ---: | +| 100 000 | 1,02 | 1,44 | 4,9 | 45,2 | ~896 | +| 200 000 | 1,92 | 2,36 | 9,3 | 70,8 | ~1 686 | +| 300 000 | 2,66 | 3,28 | 13,8 | 95,0 | ~2 335 | + +Линейные зависимости (`N` — миллионы сообщений): dump `t = 0,23 + 8,20·N` с, restore `t = 0,52 + 9,20·N` с, dump+restore `t = 0,75 + 17,4·N` с (R² ≥ 0,997). Потеря в таблице — это `dump_time × 878 RPS`; `pg_dump` не блокирует записи, поэтому оценка означает «записи после снимка», а не остановку API. При полном офлайн-восстановлении тот же расчёт по `dump + restore` даёт около 2,2k / 3,7k / 5,2k сообщений. Подробная методика и команды — `research/README.md`. + +### Отказы зависимостей + +| Воздействие | Что ожидали | Что наблюдали | Восстановление / потери | +| --- | --- | --- | --- | +| `docker compose stop redis` | `/health/live` 200; `/health/ready` 503; защищённый запрос — ограниченный 503, не анонимный доступ | именно так; обнаружение отказа за 4,4 с (включая таймауты Redis) | `start redis` → готовность за 4,0 с; старый токен и подтверждённое сообщение на месте; потерь нет | +| `docker compose stop postgres` | то же; сессия остаётся в Redis | live=200, ready=503, protected=503; обнаружение за 2,8 с | `start postgres` → готовность за 0,9 с; старый токен и сообщение на месте; потерь нет | +| `make test-persistence` (рестарт API + PostgreSQL + Redis) | тот же токен, те же данные | PASS; `StartedAt` всех контейнеров изменился; volumes сохранены | данные и сессия сохранены | +| `docker compose down -v` (только для собственного стенда) | чистая БД, миграции с нуля | API стартует, `schema_migrations` = одна строка | данные удалены осознанно | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Один экземпляр API и один Redis, единые точки отказа хранилищ +- AOF с `appendfsync everysec`: при аварийном убийстве процесса возможна потеря до ~1 секунды последних операций с сессиями. Graceful-перезапуск, который использует `scripts/restart.sh`, проходит без потерь. +- Список `member_ids` в ответе чата упорядочен по UUID, а не по порядку запроса; состав участников от этого не меняется. Порядок чатов и сообщений, который важен контракту, соблюдается. +- Нагрузочный отчёт ограничен closed-loop клиентом и не является надёжным измерением ёмкости. +- Трек 2: синтетические перекошенные данные, один узел PostgreSQL, тёплый кэш и отсутствие конкурентной записи во время индексных замеров; dump/restore измерен по одному разу на размер. Оценка потерь использует 878 RPS из closed-loop замера, а не максимум пропускной способности. +- В отчёте не раскрываются токены, пароли и содержимое `.env`; в `evidence/` сохранены только агрегаты и коды ответов. diff --git a/app/.air.toml b/app/.air.toml new file mode 100755 index 00000000..ab919098 --- /dev/null +++ b/app/.air.toml @@ -0,0 +1,59 @@ +#:schema https://json.schemastore.org/any.json + +env_files = [] +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] +args_bin = [] +bin = "./tmp/main" +cmd = "go build -o ./tmp/main ./cmd/server/" +delay = 1000 +entrypoint = ["./tmp/main"] +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +full_bin = "" +ignore_dangerous_root_dir = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +post_cmd = [] +pre_cmd = [] +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false + +[color] +app = "" +build = "yellow" +main = "magenta" +mode = "" +runner = "green" +watcher = "cyan" + +[log] +main_only = false +silent = false +time = false + +[misc] +clean_on_exit = false + +[proxy] +app_port = 0 +app_start_timeout = 0 +enabled = false +proxy_port = 0 + +[screen] +clear_on_rebuild = false +keep_scroll = true diff --git a/app/cmd/server/main.go b/app/cmd/server/main.go new file mode 100755 index 00000000..9bfa9da8 --- /dev/null +++ b/app/cmd/server/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "log" + "net" + "os/signal" + "syscall" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/database" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" +) + +func main() { + cfg, err := config.Load() + if err != nil { + log.Fatalf("configuration: %v", err) + } + + rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + pool, err := database.Connect(rootCtx, cfg.DatabaseURL, cfg.DBMaxConns, cfg.StartupWait) + if err != nil { + log.Fatalf("database: %v", err) + } + defer pool.Close() + + migrateCtx, cancelMigrate := context.WithTimeout(rootCtx, 60*time.Second) + err = database.Migrate(migrateCtx, pool, cfg.DatabaseURL) + cancelMigrate() + if err != nil { + log.Fatalf("migrations: %v", err) + } + + sessions, err := session.New(cfg.CacheURL, cfg.SessionTTL, cfg.RedisTimeout) + if err != nil { + log.Fatalf("session store: %v", err) + } + defer sessions.Close() + + app := internal.New(cfg, pool, sessions) + + addr := net.JoinHostPort(cfg.Host, cfg.Port) + listenErr := make(chan error, 1) + go func() { + listenErr <- app.Listen(addr) + }() + + select { + case err = <-listenErr: + if err != nil { + log.Fatalf("server: %v", err) + } + case <-rootCtx.Done(): + } + + log.Printf("shutting down") + internal.SignalGracefulShutdown() + if err = app.ShutdownWithTimeout(10 * time.Second); err != nil { + log.Printf("shutdown: %v", err) + } +} diff --git a/app/go.mod b/app/go.mod new file mode 100755 index 00000000..3340a241 --- /dev/null +++ b/app/go.mod @@ -0,0 +1,36 @@ +module gitflic.ru/project/bulla-max/mtusi2026lab2 + +go 1.27.1 + +require ( + github.com/gofiber/fiber/v3 v3.5.0 + github.com/golang-migrate/migrate/v4 v4.20.1 + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.11.0 + github.com/redis/go-redis/v9 v9.23.0 + golang.org/x/crypto v0.57.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/gofiber/schema v1.8.8 // indirect + github.com/gofiber/utils/v2 v2.6.1 // indirect + github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // 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/klauspost/compress v1.20.1 // indirect + github.com/mattn/go-colorable v0.1.15 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/molecule-man/go-brrr v1.1.1 // indirect + github.com/philhofer/fwd v1.2.0 // indirect + github.com/tinylib/msgp v1.6.5 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.74.0 // indirect + go.uber.org/atomic v1.12.0 // indirect + golang.org/x/net v0.59.0 // 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 + golang.org/x/tools v0.50.0 // indirect +) diff --git a/app/go.sum b/app/go.sum new file mode 100755 index 00000000..623cc7ee --- /dev/null +++ b/app/go.sum @@ -0,0 +1,131 @@ +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg= +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +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/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= +github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= +github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= +github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dhui/dktest v0.4.6 h1:+DPKyScKSEp3VLtbMDHcUq6V5Lm5zfZZVb0Sk7Ahom4= +github.com/dhui/dktest v0.4.6/go.mod h1:JHTSYDtKkvFNFHJKqCzVzqXecyv+tKt8EzceOmQOgbU= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM= +github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.7.0 h1:6SsRfJddP22WMrCkj19x9WKjEDTB+ahsdiGYf0mN39c= +github.com/docker/go-connections v0.7.0/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= +github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= +github.com/fxamacker/cbor/v2 v2.9.4 h1:xwjVlxEMR3S605oUlgBjKLTTeGFciYPGYCtF/35LKGo= +github.com/fxamacker/cbor/v2 v2.9.4/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/gofiber/fiber/v3 v3.5.0 h1:dk7TOUH6DXJGtOLsN2XEG+0ZML7cznzHILTVozbNEK8= +github.com/gofiber/fiber/v3 v3.5.0/go.mod h1:GOVDTW+gjJvfe0iJyVujbQ1Lnx+JUjFySJRI/9/xX/w= +github.com/gofiber/schema v1.8.8 h1:md5t65+Z9LttOuj7FdMcQ63D2+6GniT2ganLXwvTJM4= +github.com/gofiber/schema v1.8.8/go.mod h1:Hzf0L/fTPP1IwOAwKipKflm9C2722aebnSKJCMOgb1I= +github.com/gofiber/utils/v2 v2.6.1 h1:x3zsS/oUQz0ia2Zx8XXtG/7naR5a+/95FRO8mwQ5QQ4= +github.com/gofiber/utils/v2 v2.6.1/go.mod h1:tc9KqdRAQZRD/u0mN2d1ZHROGheSZv+0FcC9cg5OEdw= +github.com/golang-migrate/migrate/v4 v4.20.1 h1:2N/ToVTKrKl58ynBpgeVJ4In7VcLCjWTZtm4eP1LxhU= +github.com/golang-migrate/migrate/v4 v4.20.1/go.mod h1:DDPgKVb4ovSWc4FwSPfV2Uz1160f4XBiTHTrAJtljmM= +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/pgerrcode v0.0.0-20220416144525-469b46aa5efa h1:s+4MhCQ6YrzisK6hFJUX53drDT4UsSW3DEhKn0ifuHw= +github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds= +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.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ= +github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY= +github.com/mattn/go-colorable v0.1.15/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= +github.com/moby/moby/api v1.54.2 h1:wiat9QAhnDQjA7wk1kh/TqHz2I1uUA7M7t9SAl/JNXg= +github.com/moby/moby/api v1.54.2/go.mod h1:+RQ6wluLwtYaTd1WnPLykIDPekkuyD/ROWQClE83pzs= +github.com/moby/moby/client v0.4.1 h1:DMQgisVoMkmMs7fp3ROSdiBnoAu8+vo3GggFl06M/wY= +github.com/moby/moby/client v0.4.1/go.mod h1:z52C9O2POPOsnxZAy//WtKcQ32P+jT/NGeXu/7nfjGQ= +github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ= +github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= +github.com/molecule-man/go-brrr v1.1.1 h1:KYwusQhjtV3smALnhmafZf4OBrh7vP4p8O47DyUmmuY= +github.com/molecule-man/go-brrr v1.1.1/go.mod h1:7ybW6/7gA3oKY45jOfVNjSJDtrr6ea4tzbsTkjmQDC4= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= +github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s= +github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0= +github.com/shamaton/msgpack/v3 v3.2.3 h1:0r0aZtrXDFOPpwWJTEOpFq+Ih/JmMLm0pqLN5mSilrw= +github.com/shamaton/msgpack/v3 v3.2.3/go.mod h1:sgBYvEiyz8JR1NC3yGRoPVME9xXovpnh3l/plW1nfRo= +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.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/tinylib/msgp v1.6.5 h1:iAH6XTP7BpzErjBZlujJQXxA+GmRi2YWs6M4KdLtNbE= +github.com/tinylib/msgp v1.6.5/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.74.0 h1:wMS9fnO2QTALozYx5pId2Vi7ZwU/epUkY8i/KPWCHoU= +github.com/valyala/fasthttp v1.74.0/go.mod h1:3ARmLamUcw7ElxVtC8PXaGzQ6VEuvnetlkrwIklQBSE= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= +go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= +go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 h1:8tvICD4vSTOOsNrsI4Ljf6C+6UKvpTEH5XY3JMoyPoo= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0/go.mod h1:z9+yiacE0IHRqM4qFfkbt/JYlmYXgss8GY/jXoNuPJI= +go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU= +go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc= +go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc= +go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo= +go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk= +go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE= +go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE= +go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk= +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.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= +golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +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= +golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU= +golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/app/internal/app.go b/app/internal/app.go new file mode 100755 index 00000000..c91bd6bf --- /dev/null +++ b/app/internal/app.go @@ -0,0 +1,72 @@ +package internal + +import ( + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/chats" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/health" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/messages" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/users" + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/recover" + "github.com/gofiber/fiber/v3/middleware/requestid" + "github.com/jackc/pgx/v5/pgxpool" +) + +func SignalGracefulShutdown() { + health.SignalShutdown() +} + +func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App { + st := store.New(pool, cfg.DBTimeout) + + app := fiber.New(fiber.Config{ + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 60 * time.Second, + BodyLimit: 1 * 1024 * 1024, + ErrorHandler: handleError, + }) + + app.Use(recover.New()) + app.Use(requestid.New()) + + health.RegisterRoutes(app.Group("/health"), st, sessions) + + api := app.Group("/api/v1") + users.RegisterPublicRoutes(api, st) + auth.RegisterPublicRoutes(api, st, sessions) + + protected := api.Group("") + protected.Use(auth.BearerAuth(sessions)) + auth.RegisterProtectedRoutes(protected, st, sessions) + users.RegisterProtectedRoutes(protected, st) + chats.RegisterProtectedRoutes(protected, st) + messages.RegisterProtectedRoutes(protected, st) + + return app +} + +// handleError keeps every failure in the contract error envelope, including +// Fiber's own errors (404, 405, 413) and recovered panics. +func handleError(c fiber.Ctx, err error) error { + var appErr *common.AppError + if errors.As(err, &appErr) { + return common.SendError(c, appErr.Status, appErr.Code, appErr.Message) + } + + var fiberErr *fiber.Error + if errors.As(err, &fiberErr) { + return common.SendError(c, fiberErr.Code, + common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message) + } + + return common.SendError(c, fiber.StatusInternalServerError, + common.ErrorCodeInternal, "Internal server error") +} diff --git a/app/internal/auth/handler.go b/app/internal/auth/handler.go new file mode 100644 index 00000000..9e60ddca --- /dev/null +++ b/app/internal/auth/handler.go @@ -0,0 +1,130 @@ +package auth + +import ( + "encoding/base64" + "errors" + "strings" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/log" +) + +const basicChallenge = `Basic realm="messenger", charset="UTF-8"` +const bearerChallenge = `Bearer realm="messenger"` + +type Handler struct { + store *store.Store + sessions *session.Store +} + +func NewHandler(st *store.Store, sessions *session.Store) *Handler { + return &Handler{store: st, sessions: sessions} +} + +// AGENT: CreateSession is the only endpoint that accepts Basic auth. It reads +// no JSON body and returns the same 401 for an unknown user and a wrong +// password, so the response does not reveal which usernames exist. +func (h *Handler) CreateSession(c fiber.Ctx) error { + username, password, ok := parseBasic(c.Get(fiber.HeaderAuthorization)) + if !ok { + return common.SendChallenge(c, basicChallenge, "invalid credentials") + } + + user, err := h.store.GetUserByUsername(c.Context(), username) + if err != nil { + if common.IsCode(err, common.ErrorCodeNotFound) { + return common.SendChallenge(c, basicChallenge, "invalid credentials") + } + return common.SendAppError(c, err) + } + + valid, err := VerifyPassword(user.PasswordHash, password) + if err != nil || !valid { + return common.SendChallenge(c, basicChallenge, "invalid credentials") + } + + token, expiresAt, err := h.sessions.Create(c.Context(), user.ID) + if err != nil { + log.Errorf("create session: %v", err) + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "session store unavailable") + } + + return c.Status(fiber.StatusCreated).JSON(model.Session{ + Token: token, + TokenType: "Bearer", + ExpiresAt: common.RFCFormTime(expiresAt), + User: user, + }) +} + +// RevokeSession deletes the current token. The middleware has already proven +// that the token exists, so a repeated DELETE is a 401, not a 204. +func (h *Handler) RevokeSession(c fiber.Ctx) error { + token, ok := common.SessionToken(c) + if !ok { + return common.SendChallenge(c, bearerChallenge, "authentication required") + } + + if err := h.sessions.Revoke(c.Context(), token); err != nil { + log.Errorf("revoke session: %v", err) + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "session store unavailable") + } + + return c.SendStatus(fiber.StatusNoContent) +} + +// BearerAuth accepts only an opaque Bearer token. X-User-Id is deliberately +// never read: with lab 2 it can neither create nor override an identity. +func BearerAuth(sessions *session.Store) fiber.Handler { + return func(c fiber.Ctx) error { + header := strings.TrimSpace(c.Get(fiber.HeaderAuthorization)) + scheme, token, found := strings.Cut(header, " ") + token = strings.TrimSpace(token) + if !found || !strings.EqualFold(scheme, "Bearer") || token == "" || strings.ContainsAny(token, " \t") { + return common.SendChallenge(c, bearerChallenge, "authentication required") + } + + userID, err := sessions.Lookup(c.Context(), token) + if err != nil { + if errors.Is(err, session.ErrNotFound) { + return common.SendChallenge(c, bearerChallenge, "session expired or revoked") + } + // AGENT: A dead Redis must not degrade to "anonymous allowed". + // Protected calls get a bounded 503 instead of a 401 or a bypass. + log.Errorf("session lookup: %v", err) + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "session store unavailable") + } + + c.Locals(common.KeyUserID, userID) + c.Locals(common.KeySessionToken, token) + return c.Next() + } +} + +func parseBasic(header string) (string, string, bool) { + scheme, encoded, found := strings.Cut(strings.TrimSpace(header), " ") + if !found || !strings.EqualFold(scheme, "Basic") || encoded == "" { + return "", "", false + } + + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + if decoded, err = base64.RawStdEncoding.DecodeString(encoded); err != nil { + return "", "", false + } + } + + // RFC 7617: split at the first colon; a colon is legal inside the password. + username, password, found := strings.Cut(string(decoded), ":") + if !found || username == "" || password == "" { + return "", "", false + } + return username, password, true +} diff --git a/app/internal/auth/password.go b/app/internal/auth/password.go new file mode 100644 index 00000000..1b77afae --- /dev/null +++ b/app/internal/auth/password.go @@ -0,0 +1,72 @@ +// AGENT: Argon2id is used instead of bcrypt because the contract allows 128 +// Unicode code points and bcrypt silently truncates at 72 bytes. Salt and +// parameters are stored inside the encoded hash string. +package auth + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +const ( + argonMemory = 64 * 1024 + argonIterations = 2 + argonParallelism = 4 + argonKeyLength = 32 + argonSaltLength = 16 +) + +var ErrInvalidHash = errors.New("invalid password hash") + +func HashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("generate salt: %w", err) + } + + key := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, argonKeyLength) + + return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, argonMemory, argonIterations, argonParallelism, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(key)), nil +} + +// VerifyPassword recomputes the digest with the stored parameters and compares +// in constant time. +func VerifyPassword(encoded, password string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, ErrInvalidHash + } + + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version { + return false, ErrInvalidHash + } + + var memory uint32 + var iterations uint32 + var parallelism uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil { + return false, ErrInvalidHash + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false, ErrInvalidHash + } + expected, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false, ErrInvalidHash + } + + actual := argon2.IDKey([]byte(password), salt, iterations, memory, parallelism, uint32(len(expected))) + return subtle.ConstantTimeCompare(actual, expected) == 1, nil +} diff --git a/app/internal/auth/password_test.go b/app/internal/auth/password_test.go new file mode 100644 index 00000000..f6a988ff --- /dev/null +++ b/app/internal/auth/password_test.go @@ -0,0 +1,46 @@ +package auth + +import ( + "strings" + "testing" +) + +func TestPasswordRoundTripPreservesFullRange(t *testing.T) { + passwords := []string{ + "simple-password-123", + "colon:inside:password", + "пароль-с-кириллицей-12", + strings.Repeat("😀", 128), + } + for _, password := range passwords { + hash, err := HashPassword(password) + if err != nil { + t.Fatalf("HashPassword: %v", err) + } + ok, err := VerifyPassword(hash, password) + if err != nil || !ok { + t.Fatalf("VerifyPassword rejected the correct password (ok=%v err=%v)", ok, err) + } + ok, err = VerifyPassword(hash, password+"x") + if err != nil || ok { + t.Fatalf("VerifyPassword accepted a different password (ok=%v err=%v)", ok, err) + } + } +} + +func TestPasswordHashIsSalted(t *testing.T) { + first, err := HashPassword("same-password-123") + if err != nil { + t.Fatal(err) + } + second, err := HashPassword("same-password-123") + if err != nil { + t.Fatal(err) + } + if first == second { + t.Fatal("two hashes of the same password are identical") + } + if _, err := VerifyPassword("not-a-hash", "same-password-123"); err == nil { + t.Fatal("invalid encoded hash was accepted") + } +} diff --git a/app/internal/auth/router.go b/app/internal/auth/router.go new file mode 100644 index 00000000..04775b3c --- /dev/null +++ b/app/internal/auth/router.go @@ -0,0 +1,17 @@ +package auth + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +func RegisterPublicRoutes(router fiber.Router, st *store.Store, sessions *session.Store) { + handler := NewHandler(st, sessions) + router.Post("/auth/sessions", handler.CreateSession) +} + +func RegisterProtectedRoutes(router fiber.Router, st *store.Store, sessions *session.Store) { + handler := NewHandler(st, sessions) + router.Delete("/auth/sessions/current", handler.RevokeSession) +} diff --git a/app/internal/chats/handler.go b/app/internal/chats/handler.go new file mode 100644 index 00000000..4fa0c0b8 --- /dev/null +++ b/app/internal/chats/handler.go @@ -0,0 +1,86 @@ +package chats + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +type Handler struct { + store *store.Store +} + +func NewHandler(st *store.Store) *Handler { + return &Handler{store: st} +} + +type CreateChatRequest struct { + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` +} + +func (h *Handler) CreateChat(c fiber.Ctx) error { + userID := common.MustUserID(c) + + var req CreateChatRequest + if err := common.BindJSON(c, &req); err != nil { + return common.SendAppError(c, err) + } + + if msg := common.ValidateChatTitle(req.Title); msg != "" { + return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg) + } + if msg := common.ValidateMemberIDs(req.MemberIDs, userID); msg != "" { + return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg) + } + + chat, err := h.store.CreateChat(c.Context(), req.Title, req.MemberIDs) + if err != nil { + return common.SendAppError(c, err) + } + + return c.Status(fiber.StatusCreated).JSON(chat) +} + +func (h *Handler) ListChats(c fiber.Ctx) error { + userID := common.MustUserID(c) + + limit, err := common.ParseLimit(c, 50) + if err != nil { + return common.SendAppError(c, err) + } + + cursor, err := common.ParseCursor(c) + if err != nil { + return common.SendAppError(c, err) + } + if cursor != nil && cursor.ContextID != userID { + return common.SendError(c, fiber.StatusBadRequest, + common.ErrorCodeInvalidRequest, "bad cursor value") + } + + page, err := h.store.ListChats(c.Context(), userID, limit, cursor) + if err != nil { + return common.SendAppError(c, err) + } + + return c.JSON(page) +} + +func (h *Handler) GetChat(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := uuid.Parse(c.Params("chat_id")) + if err != nil { + return common.SendError(c, fiber.StatusBadRequest, + common.ErrorCodeInvalidRequest, "invalid chat_id format") + } + + chat, err := h.store.GetChatForUser(c.Context(), userID, chatID) + if err != nil { + return common.SendAppError(c, err) + } + + return c.JSON(chat) +} diff --git a/app/internal/chats/router.go b/app/internal/chats/router.go new file mode 100644 index 00000000..3329ac32 --- /dev/null +++ b/app/internal/chats/router.go @@ -0,0 +1,13 @@ +package chats + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +func RegisterProtectedRoutes(router fiber.Router, st *store.Store) { + handler := NewHandler(st) + router.Post("/chats", handler.CreateChat) + router.Get("/chats", handler.ListChats) + router.Get("/chats/:chat_id", handler.GetChat) +} diff --git a/app/internal/common/binding.go b/app/internal/common/binding.go new file mode 100644 index 00000000..f715a729 --- /dev/null +++ b/app/internal/common/binding.go @@ -0,0 +1,44 @@ +package common + +import ( + "bytes" + "encoding/json" + "io" + "strings" + + "github.com/gofiber/fiber/v3" +) + +// AGENT: Field-level validation is deliberately not delegated to a generic +// struct validator. The contract needs specific messages for blank strings, +// Unicode length and duplicate member IDs, so handlers call the Validators in +// validation.go after this strict JSON decoding step. +// +// BindJSON enforces the request format from the contract: JSON content type, +// exactly one JSON value, no unknown fields and correct Go types. Field-level +// rules (non-blank strings, lengths, UUID lists) stay in the handlers so their +// messages can be specific. +func BindJSON(c fiber.Ctx, dst any) error { + if contentType := c.Get(fiber.HeaderContentType); !strings.HasPrefix(contentType, "application/json") { + return UnsupportedMedia("invalid JSON body") + } + + body := c.Body() + if len(body) == 0 { + return BadRequest("request body is empty") + } + + decoder := json.NewDecoder(bytes.NewReader(body)) + decoder.DisallowUnknownFields() + + if err := decoder.Decode(dst); err != nil { + return BadRequest("invalid JSON body") + } + + var extra json.RawMessage + if err := decoder.Decode(&extra); err != io.EOF { + return BadRequest("request body has extra trailing bytes") + } + + return nil +} diff --git a/app/internal/common/error.go b/app/internal/common/error.go new file mode 100644 index 00000000..c4c3fc14 --- /dev/null +++ b/app/internal/common/error.go @@ -0,0 +1,143 @@ +package common + +import ( + "errors" + + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/requestid" +) + +const ( + ErrorCodeInvalidRequest = "invalid_request" + ErrorCodeUnauthorized = "unauthorized" + ErrorCodeNotFound = "not_found" + ErrorCodeConflict = "conflict" + ErrorCodePayloadTooLarge = "payload_too_large" + ErrorCodeUnsupportedMedia = "unsupported_media_type" + ErrorCodeRateLimited = "rate_limited" + ErrorCodeUnavailable = "unavailable" + ErrorCodeInternal = "internal_error" +) + +type APIErrorResponse struct { + Error APIError `json:"error"` +} + +type APIError struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +func NewError(code, message, requestID string) APIErrorResponse { + return APIErrorResponse{ + Error: APIError{ + Code: code, + Message: message, + RequestID: requestID, + }, + } +} + +type AppError struct { + Status int + Code string + Message string +} + +func (e *AppError) Error() string { + return e.Message +} + +func NewAppError(status int, code, message string) *AppError { + return &AppError{ + Status: status, + Code: code, + Message: message, + } +} + +func BadRequest(message string) *AppError { + return NewAppError(fiber.StatusBadRequest, ErrorCodeInvalidRequest, message) +} + +func Unauthorized(message string) *AppError { + return NewAppError(fiber.StatusUnauthorized, ErrorCodeUnauthorized, message) +} + +func NotFound(message string) *AppError { + return NewAppError(fiber.StatusNotFound, ErrorCodeNotFound, message) +} + +func Conflict(message string) *AppError { + return NewAppError(fiber.StatusConflict, ErrorCodeConflict, message) +} + +func UnsupportedMedia(message string) *AppError { + return NewAppError(fiber.StatusUnsupportedMediaType, ErrorCodeUnsupportedMedia, message) +} + +func Internal() *AppError { + return NewAppError(fiber.StatusInternalServerError, ErrorCodeInternal, "Internal server error") +} + +func Unavailable(message string) *AppError { + return NewAppError(fiber.StatusServiceUnavailable, ErrorCodeUnavailable, message) +} + +// IsCode reports whether err is an AppError with the given contract code. +func IsCode(err error, code string) bool { + var appErr *AppError + return errors.As(err, &appErr) && appErr.Code == code +} + +func ErrorCodeFromStatus(status int) string { + switch status { + case fiber.StatusBadRequest: + return ErrorCodeInvalidRequest + case fiber.StatusUnauthorized: + return ErrorCodeUnauthorized + case fiber.StatusNotFound: + return ErrorCodeNotFound + case fiber.StatusConflict: + return ErrorCodeConflict + case fiber.StatusRequestEntityTooLarge: + return ErrorCodePayloadTooLarge + case fiber.StatusUnsupportedMediaType: + return ErrorCodeUnsupportedMedia + case fiber.StatusTooManyRequests: + return ErrorCodeRateLimited + case fiber.StatusServiceUnavailable: + return ErrorCodeUnavailable + default: + return ErrorCodeInternal + } +} + +func getRequestID(c fiber.Ctx) string { + if id := string(c.Response().Header.Peek("X-Request-Id")); id != "" { + return id + } + return requestid.FromContext(c) +} + +func SendError(c fiber.Ctx, status int, code, message string) error { + return c.Status(status).JSON(NewError(code, message, getRequestID(c))) +} + +func SendAppError(c fiber.Ctx, err error) error { + var appErr *AppError + if errors.As(err, &appErr) { + return SendError(c, appErr.Status, appErr.Code, appErr.Message) + } + + return SendError(c, fiber.StatusInternalServerError, + ErrorCodeInternal, "Internal server error") +} + +// SendChallenge emits a 401 with the WWW-Authenticate value required by the +// contract: Basic for login, Bearer for every protected endpoint. +func SendChallenge(c fiber.Ctx, challenge, message string) error { + c.Set(fiber.HeaderWWWAuthenticate, challenge) + return SendError(c, fiber.StatusUnauthorized, ErrorCodeUnauthorized, message) +} diff --git a/app/internal/common/locals.go b/app/internal/common/locals.go new file mode 100644 index 00000000..9f977601 --- /dev/null +++ b/app/internal/common/locals.go @@ -0,0 +1,36 @@ +package common + +import ( + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +const ( + // KeyUserID is the request-scoped key set by the Bearer middleware. + KeyUserID = "userID" + // KeySessionToken is the raw token of the current request. + KeySessionToken = "sessionToken" +) + +// UserID returns the authenticated user ID and whether the middleware set it. +func UserID(c fiber.Ctx) (uuid.UUID, bool) { + userID, ok := c.Locals(KeyUserID).(uuid.UUID) + return userID, ok +} + +// MustUserID is for handlers mounted behind BearerAuth, where the middleware +// guarantees the local. A missing value is a programming error, not a client +// error, so it becomes a recovered 500 instead of an anonymous request. +func MustUserID(c fiber.Ctx) uuid.UUID { + userID, ok := UserID(c) + if !ok { + panic("common: authenticated user ID is missing from the request context") + } + return userID +} + +// SessionToken returns the raw bearer token of the current request. +func SessionToken(c fiber.Ctx) (string, bool) { + token, ok := c.Locals(KeySessionToken).(string) + return token, ok && token != "" +} diff --git a/app/internal/common/paginator.go b/app/internal/common/paginator.go new file mode 100644 index 00000000..e2898666 --- /dev/null +++ b/app/internal/common/paginator.go @@ -0,0 +1,108 @@ +package common + +import ( + "encoding/base64" + "strconv" + "strings" + "time" + + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +// CursorKeyCtx is the opaque keyset position: (created_at, id) plus the +// resource/user the cursor belongs to. ContextID stops a cursor from one chat +// or user list being replayed against another. +type CursorKeyCtx struct { + Time time.Time + ID uuid.UUID + ContextID uuid.UUID +} + +type Page[T any] struct { + Items []T `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func ParseLimit(c fiber.Ctx, defaultLimit int) (int, error) { + limitStr, exists := c.Queries()["limit"] + if !exists { + return defaultLimit, nil + } + if strings.TrimSpace(limitStr) == "" { + return 0, BadRequest("limit param declared but empty") + } + + limit, err := strconv.Atoi(limitStr) + if err != nil || limit < 1 || limit > 100 { + return 0, BadRequest("limit must be an integer between 1 and 100") + } + return limit, nil +} + +func ParseCursor(c fiber.Ctx) (*CursorKeyCtx, error) { + cursorStr, exists := c.Queries()["cursor"] + if !exists { + return nil, nil + } + if strings.TrimSpace(cursorStr) == "" { + return nil, BadRequest("cursor param declared but empty") + } + + cursor, ok := DecodeCursor(cursorStr) + if !ok { + return nil, BadRequest("invalid cursor format") + } + return cursor, nil +} + +func EncodeCursor(key CursorKeyCtx) string { + raw := key.Time.UTC().Format(time.RFC3339Nano) + "|" + key.ID.String() + "|" + key.ContextID.String() + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func DecodeCursor(cursor string) (*CursorKeyCtx, bool) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return nil, false + } + + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return nil, false + } + + parsedTime, err := time.Parse(time.RFC3339Nano, parts[0]) + if err != nil { + return nil, false + } + parsedID, err := uuid.Parse(parts[1]) + if err != nil { + return nil, false + } + parsedContext, err := uuid.Parse(parts[2]) + if err != nil { + return nil, false + } + + return &CursorKeyCtx{Time: parsedTime, ID: parsedID, ContextID: parsedContext}, true +} + +// NextCursor returns a cursor pointing at the last item of a page when more +// rows exist. limit+1 rows are fetched by the store, so a non-nil cursor means +// the next page is known to be non-empty at read time. +func NextCursor[T interface { + GetID() uuid.UUID + GetCreatedAt() time.Time +}](items []T, hasMore bool, contextID uuid.UUID) *string { + if !hasMore || len(items) == 0 { + return nil + } + last := items[len(items)-1] + encoded := EncodeCursor(CursorKeyCtx{ + Time: last.GetCreatedAt(), + ID: last.GetID(), + ContextID: contextID, + }) + return &encoded +} diff --git a/app/internal/common/paginator_test.go b/app/internal/common/paginator_test.go new file mode 100644 index 00000000..3fa7cb0e --- /dev/null +++ b/app/internal/common/paginator_test.go @@ -0,0 +1,49 @@ +package common + +import ( + "testing" + "time" + + "github.com/google/uuid" +) + +func TestCursorRoundTrip(t *testing.T) { + want := CursorKeyCtx{ + Time: time.Date(2026, 10, 20, 12, 30, 45, 123456789, time.UTC), + ID: uuid.MustParse("11111111-1111-4111-8111-111111111111"), + ContextID: uuid.MustParse("22222222-2222-4222-8222-222222222222"), + } + + encoded := EncodeCursor(want) + got, ok := DecodeCursor(encoded) + if !ok { + t.Fatal("cursor did not decode") + } + if !got.Time.Equal(want.Time) || got.ID != want.ID || got.ContextID != want.ContextID { + t.Fatalf("cursor round trip changed the key: %+v", got) + } + + for _, invalid := range []string{"not-base64!", "", "aGVsbG8"} { + if _, ok := DecodeCursor(invalid); ok { + t.Fatalf("invalid cursor %q was accepted", invalid) + } + } +} + +func TestValidatePasswordRange(t *testing.T) { + cases := []struct { + password string + valid bool + }{ + {"short", false}, + {"12345678901", false}, + {"123456789012", true}, + {"пароль-из-двенадцати", true}, + {"😀", false}, + } + for _, tc := range cases { + if got := ValidatePassword(tc.password) == ""; got != tc.valid { + t.Fatalf("ValidatePassword(%q) valid=%v, want %v", tc.password, got, tc.valid) + } + } +} diff --git a/app/internal/common/time.go b/app/internal/common/time.go new file mode 100644 index 00000000..69030698 --- /dev/null +++ b/app/internal/common/time.go @@ -0,0 +1,36 @@ +package common + +import ( + "encoding/json" + "time" +) + +// storageTimePrecision is the maximum precision shared by PostgreSQL +// timestamptz columns and the JSON representation. PostgreSQL stores +// microseconds, so keeping the same boundary in Go prevents a create response +// from showing more precision than a later read can reproduce. +const storageTimePrecision = time.Microsecond + +type RFCFormTime time.Time + +func (t RFCFormTime) MarshalJSON() ([]byte, error) { + // RFC 3339 permits an optional fractional second. Use all six digits that + // PostgreSQL can store, and omit the fraction only for an exact second. + // A fixed six-digit fraction is also accepted by strict consumers that only + // understand 3- or 6-digit fractions (for example Python 3.9's parser). + utc := time.Time(t).UTC().Truncate(storageTimePrecision) + layout := "2006-01-02T15:04:05Z" + if utc.Nanosecond() != 0 { + layout = "2006-01-02T15:04:05.000000Z" + } + return json.Marshal(utc.Format(layout)) +} + +func NowUTC() RFCFormTime { + // AGENT: The API used to truncate timestamps to whole seconds to keep + // keyset ordering consistent with JSON. RFC 3339 supports fractional seconds + // and PostgreSQL has microsecond precision, so use that precision end to end: + // timestamps remain ordered after serialization without discarding sub-second + // information. + return RFCFormTime(time.Now().UTC().Truncate(storageTimePrecision)) +} diff --git a/app/internal/common/time_test.go b/app/internal/common/time_test.go new file mode 100644 index 00000000..a177d48a --- /dev/null +++ b/app/internal/common/time_test.go @@ -0,0 +1,48 @@ +package common + +import ( + "encoding/json" + "testing" + "time" +) + +func TestNowUTCHasMicrosecondPrecision(t *testing.T) { + now := time.Time(NowUTC()) + if now.Nanosecond()%1000 != 0 { + t.Fatalf("NowUTC() kept precision below a microsecond: %v", now) + } + if now.Location() != time.UTC { + t.Fatalf("NowUTC() location = %v, want UTC", now.Location()) + } +} + +func TestRFCFormTimeSerializesFractionalSeconds(t *testing.T) { + cases := []struct { + name string + value time.Time + want string + }{ + { + name: "microseconds", + value: time.Date(2026, 10, 26, 12, 34, 56, 123456789, time.UTC), + want: `"2026-10-26T12:34:56.123456Z"`, + }, + { + name: "exact second", + value: time.Date(2026, 10, 26, 12, 34, 56, 0, time.UTC), + want: `"2026-10-26T12:34:56Z"`, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + encoded, err := json.Marshal(RFCFormTime(tc.value)) + if err != nil { + t.Fatalf("marshal RFC form time: %v", err) + } + if string(encoded) != tc.want { + t.Fatalf("RFC form time = %s, want %s", encoded, tc.want) + } + }) + } +} diff --git a/app/internal/common/validation.go b/app/internal/common/validation.go new file mode 100644 index 00000000..7610417a --- /dev/null +++ b/app/internal/common/validation.go @@ -0,0 +1,99 @@ +package common + +import ( + "regexp" + "strings" + "unicode/utf8" + + "github.com/google/uuid" +) + +var usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +func blank(s string) bool { + return strings.TrimSpace(s) == "" +} + +func strLenBetween(s string, min, max int) bool { + n := utf8.RuneCountInString(s) + return n >= min && n <= max +} + +func ValidateCreateUser(username, displayName string) string { + if !usernameRegex.MatchString(username) { + return "username must match ^[a-z0-9_]{3,32}$" + } + + if blank(displayName) { + return "display_name must not be empty or only whitespace" + } + + if !strLenBetween(displayName, 1, 100) { + return "display_name must be between 1 and 100 Unicode code points" + } + + return "" +} + +func ValidatePassword(password string) string { + if blank(password) { + return "password must not be empty or only whitespace" + } + + if !strLenBetween(password, 12, 128) { + return "password must be between 12 and 128 Unicode code points" + } + + return "" +} + +func ValidateChatTitle(title string) string { + if blank(title) { + return "title must not be empty or only whitespace" + } + + if !strLenBetween(title, 1, 100) { + return "title must be between 1 and 100 Unicode code points" + } + + return "" +} + +func ValidateMessageText(text string) string { + if blank(text) { + return "text must not be empty or only whitespace" + } + + if !strLenBetween(text, 1, 4000) { + return "text must be between 1 and 4000 Unicode code points" + } + + return "" +} + +func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) string { + if len(ids) < 1 || len(ids) > 100 { + return "member_ids must contain between 1 and 100 members" + } + + seen := make(map[uuid.UUID]bool, len(ids)) + callerFound := false + + for _, id := range ids { + if seen[id] { + return "member_ids must not contain duplicates" + } + + seen[id] = true + + if id == callerID { + callerFound = true + } + } + + if !callerFound { + return "caller must be included in member_ids" + } + + return "" +} diff --git a/app/internal/config/config.go b/app/internal/config/config.go new file mode 100644 index 00000000..d546ef4e --- /dev/null +++ b/app/internal/config/config.go @@ -0,0 +1,107 @@ +// AGENT: All runtime configuration is read here so every other package receives +// explicit values instead of calling os.Getenv. Values are compatible with +// .env.example and support the *_FILE convention used by Compose secrets. +package config + +import ( + "fmt" + "os" + "strconv" + "strings" + "time" +) + +type Config struct { + Host string + Port string + SessionTTL time.Duration + DatabaseURL string + CacheURL string + DBMaxConns int32 + DBTimeout time.Duration + RedisTimeout time.Duration + StartupWait time.Duration +} + +func Load() (Config, error) { + cfg := Config{ + Host: envOr("APP_HOST", "0.0.0.0"), + Port: envOr("APP_PORT", "8080"), + DBMaxConns: 10, + DBTimeout: 5 * time.Second, + RedisTimeout: 2 * time.Second, + StartupWait: 30 * time.Second, + } + + ttlSeconds, err := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600")) + if err != nil || ttlSeconds < 1 { + return cfg, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer") + } + cfg.SessionTTL = time.Duration(ttlSeconds) * time.Second + + if value := envOr("DB_POOL_MAX_CONNS", ""); value != "" { + n, convErr := strconv.Atoi(value) + if convErr != nil || n < 1 || n > 100 { + return cfg, fmt.Errorf("DB_POOL_MAX_CONNS must be an integer between 1 and 100") + } + cfg.DBMaxConns = int32(n) + } + + cfg.DatabaseURL, err = secret("DATABASE_URL") + if err != nil { + return cfg, err + } + if cfg.DatabaseURL == "" { + return cfg, fmt.Errorf("DATABASE_URL or DATABASE_URL_FILE is required") + } + + cfg.CacheURL, err = secret("CACHE_URL") + if err != nil { + return cfg, err + } + if cfg.CacheURL == "" { + cfg.CacheURL, err = secret("REDIS_URL") + if err != nil { + return cfg, err + } + } + if cfg.CacheURL == "" { + return cfg, fmt.Errorf("CACHE_URL or CACHE_URL_FILE is required") + } + + if value := strings.ToLower(envOr("SESSION_STORE", "redis")); value != "redis" { + return cfg, fmt.Errorf("SESSION_STORE must be redis (got %q)", value) + } + + return cfg, nil +} + +func envOr(name, fallback string) string { + if value := os.Getenv(name); value != "" { + return value + } + return fallback +} + +func secret(name string) (string, error) { + if value := os.Getenv(name); value != "" { + return value, nil + } + + path := os.Getenv(name + "_FILE") + if path == "" { + return "", nil + } + + data, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("read %s_FILE: %w", name, err) + } + + value := strings.TrimSpace(string(data)) + if value == "" { + return "", fmt.Errorf("%s_FILE is empty", name) + } + + return value, nil +} diff --git a/app/internal/database/database.go b/app/internal/database/database.go new file mode 100644 index 00000000..be4383d5 --- /dev/null +++ b/app/internal/database/database.go @@ -0,0 +1,46 @@ +// Package database owns the PostgreSQL connection pool and schema migrations. +package database + +import ( + "context" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +func Connect(ctx context.Context, url string, maxConns int32, wait time.Duration) (*pgxpool.Pool, error) { + cfg, err := pgxpool.ParseConfig(url) + if err != nil { + return nil, fmt.Errorf("parse DATABASE_URL: %w", err) + } + + cfg.MaxConns = maxConns + cfg.MinConns = 1 + cfg.MaxConnLifetime = 30 * time.Minute + cfg.MaxConnIdleTime = 5 * time.Minute + cfg.HealthCheckPeriod = 30 * time.Second + cfg.ConnConfig.ConnectTimeout = 3 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, fmt.Errorf("create pool: %w", err) + } + + // AGENT: Compose may start the API while PostgreSQL is still booting. A bounded + // retry keeps the container from crash-looping without hiding a permanent error. + deadline := time.Now().Add(wait) + for { + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + err = pool.Ping(pingCtx) + cancel() + if err == nil { + return pool, nil + } + if time.Now().After(deadline) || ctx.Err() != nil { + pool.Close() + return nil, fmt.Errorf("postgres is not reachable: %w", err) + } + time.Sleep(500 * time.Millisecond) + } +} diff --git a/app/internal/database/migrations.go b/app/internal/database/migrations.go new file mode 100644 index 00000000..6c899555 --- /dev/null +++ b/app/internal/database/migrations.go @@ -0,0 +1,162 @@ +// AGENT: golang-migrate now runs the embedded SQL files instead of the previous +// hand-written runner. The legacy table upgrade below keeps existing lab 2 +// volumes usable, so switching engines does not require deleting data. +package database + +import ( + "context" + "embed" + "errors" + "fmt" + "time" + + "github.com/golang-migrate/migrate/v4" + pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5" + "github.com/golang-migrate/migrate/v4/source/iofs" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" +) + +//go:embed migrations/*.sql +var migrationFiles embed.FS + +const ( + // The old in-house runner used this advisory lock. We keep the same key for + // the one-time table upgrade so an upgrade cannot race a still-running old + // instance that is applying the baseline migration. + legacyMigrationLockID int64 = 728_2026 + migrationsTable = "schema_migrations" +) + +// Migrate applies the embedded golang-migrate migrations. golang-migrate takes +// its own PostgreSQL advisory lock, records the current version and dirty flag +// in schema_migrations, and wraps each migration file in one transaction. +func Migrate(ctx context.Context, pool *pgxpool.Pool, databaseURL string) error { + if err := upgradeLegacyMigrationsTable(ctx, pool); err != nil { + return err + } + + source, err := iofs.New(migrationFiles, "migrations") + if err != nil { + return fmt.Errorf("load embedded migrations: %w", err) + } + + connConfig, err := pgx.ParseConfig(databaseURL) + if err != nil { + _ = source.Close() + return fmt.Errorf("parse DATABASE_URL for migrations: %w", err) + } + // golang-migrate acquires its lock with context.Background(). A PostgreSQL + // lock timeout keeps startup bounded if another instance never releases it. + if connConfig.RuntimeParams == nil { + connConfig.RuntimeParams = make(map[string]string) + } + connConfig.RuntimeParams["lock_timeout"] = "30000" + connConfig.ConnectTimeout = 3 * time.Second + + db := stdlib.OpenDB(*connConfig) + db.SetMaxOpenConns(1) + db.SetMaxIdleConns(1) + + driver, err := pgxmigrate.WithInstance(db, &pgxmigrate.Config{ + MigrationsTable: migrationsTable, + StatementTimeout: 30 * time.Second, + }) + if err != nil { + _ = source.Close() + _ = db.Close() + return fmt.Errorf("create migration driver: %w", err) + } + + migrator, err := migrate.NewWithInstance("iofs", source, "pgx5", driver) + if err != nil { + _ = source.Close() + _ = driver.Close() + return fmt.Errorf("create migrator: %w", err) + } + defer func() { + // Close closes both the embedded source and the database connection. + _, _ = migrator.Close() + }() + + if err := migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return fmt.Errorf("apply migrations: %w", err) + } + + return nil +} + +// upgradeLegacyMigrationsTable replaces the table written by the previous +// in-house runner (version text, applied_at) with the golang-migrate schema +// (version bigint, dirty boolean). The schema itself is left untouched and the +// highest applied version is carried over, so a preserved volume does not need +// to be recreated. The migration DDL is idempotent, but preserving the version +// keeps the migration history truthful. +// +// AGENT: This is a one-time compatibility bridge, not a permanent second +// migration format. It runs under the old advisory lock before golang-migrate +// creates its own lock and metadata. +func upgradeLegacyMigrationsTable(ctx context.Context, pool *pgxpool.Pool) error { + conn, err := pool.Acquire(ctx) + if err != nil { + return fmt.Errorf("acquire migration connection: %w", err) + } + defer conn.Release() + + if _, err := conn.Exec(ctx, "SELECT pg_advisory_lock($1)", legacyMigrationLockID); err != nil { + return fmt.Errorf("acquire migration lock: %w", err) + } + defer func() { + unlockCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, _ = conn.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", legacyMigrationLockID) + }() + + var legacy bool + if err := conn.QueryRow(ctx, ` + SELECT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_schema = current_schema() + AND table_name = $1 + AND column_name = 'applied_at' + )`, migrationsTable).Scan(&legacy); err != nil { + return fmt.Errorf("inspect %s: %w", migrationsTable, err) + } + if !legacy { + return nil + } + + tx, err := conn.Begin(ctx) + if err != nil { + return fmt.Errorf("begin migration table upgrade: %w", err) + } + defer func() { _ = tx.Rollback(context.Background()) }() + + const upgrade = ` + DROP TABLE IF EXISTS schema_migrations_migrate_tmp; + + CREATE TABLE schema_migrations_migrate_tmp ( + version bigint NOT NULL PRIMARY KEY, + dirty boolean NOT NULL + ); + + INSERT INTO schema_migrations_migrate_tmp (version, dirty) + SELECT MAX(substring(version FROM '^[0-9]+')::bigint), false + FROM schema_migrations + WHERE version ~ '^[0-9]+' + HAVING COUNT(*) > 0; + + DROP TABLE schema_migrations; + ALTER TABLE schema_migrations_migrate_tmp RENAME TO schema_migrations;` + + if _, err := tx.Exec(ctx, upgrade); err != nil { + return fmt.Errorf("upgrade legacy %s: %w", migrationsTable, err) + } + if err := tx.Commit(ctx); err != nil { + return fmt.Errorf("commit migration table upgrade: %w", err) + } + + return nil +} diff --git a/app/internal/database/migrations/0001_init.down.sql b/app/internal/database/migrations/0001_init.down.sql new file mode 100644 index 00000000..1cd97847 --- /dev/null +++ b/app/internal/database/migrations/0001_init.down.sql @@ -0,0 +1,6 @@ +-- AGENT: Local rollback for the baseline schema. The application only runs Up +-- migrations; this file exists so golang-migrate can also step down in tests. +DROP TABLE IF EXISTS messages; +DROP TABLE IF EXISTS chat_members; +DROP TABLE IF EXISTS chats; +DROP TABLE IF EXISTS users; diff --git a/app/internal/database/migrations/0001_init.up.sql b/app/internal/database/migrations/0001_init.up.sql new file mode 100644 index 00000000..e2578130 --- /dev/null +++ b/app/internal/database/migrations/0001_init.up.sql @@ -0,0 +1,46 @@ +-- AGENT: Single versioned baseline migration for lab 2. The application records +-- applied versions in schema_migrations, so rerunning it never duplicates rows. +-- The unique username constraint is the authoritative defence against the +-- concurrent-registration race, not a SELECT followed by an INSERT. + +CREATE TABLE IF NOT EXISTS users ( + id uuid PRIMARY KEY, + username text NOT NULL, + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT users_username_key UNIQUE (username), + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100) +); + +CREATE TABLE IF NOT EXISTS chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100) +); + +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX IF NOT EXISTS chat_members_user_idx + ON chat_members (user_id, chat_id); + +CREATE TABLE IF NOT EXISTS messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text text NOT NULL, + created_at timestamptz NOT NULL, + CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000) +); + +-- AGENT: This composite index matches the only history predicate and sort order: +-- (chat_id, created_at, id). It lets each keyset page seek directly to the cursor +-- instead of scanning the chat history from the beginning. +CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx + ON messages (chat_id, created_at, id); diff --git a/app/internal/database/migrations_test.go b/app/internal/database/migrations_test.go new file mode 100644 index 00000000..45942fb9 --- /dev/null +++ b/app/internal/database/migrations_test.go @@ -0,0 +1,43 @@ +package database + +import ( + "errors" + "io/fs" + "testing" + + "github.com/golang-migrate/migrate/v4/source/iofs" +) + +func TestEmbeddedMigrationsParse(t *testing.T) { + entries, err := fs.ReadDir(migrationFiles, "migrations") + if err != nil { + t.Fatalf("read embedded migrations: %v", err) + } + + names := make(map[string]bool, len(entries)) + for _, entry := range entries { + names[entry.Name()] = true + } + for _, want := range []string{"0001_init.up.sql", "0001_init.down.sql"} { + if !names[want] { + t.Fatalf("migration %q is missing from the embedded filesystem", want) + } + } + + source, err := iofs.New(migrationFiles, "migrations") + if err != nil { + t.Fatalf("parse migrations with golang-migrate: %v", err) + } + defer func() { _ = source.Close() }() + + version, err := source.First() + if err != nil { + t.Fatalf("read first migration: %v", err) + } + if version != 1 { + t.Fatalf("first migration version = %d, want 1", version) + } + if _, err := source.Next(version); !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("expected the baseline to be the last migration, got %v", err) + } +} diff --git a/app/internal/health/handler.go b/app/internal/health/handler.go new file mode 100644 index 00000000..06304d8b --- /dev/null +++ b/app/internal/health/handler.go @@ -0,0 +1,55 @@ +package health + +import ( + "sync/atomic" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +type Handler struct { + store *store.Store + sessions *session.Store +} + +func NewHandler(st *store.Store, sessions *session.Store) *Handler { + return &Handler{store: st, sessions: sessions} +} + +var shuttingDown atomic.Bool + +func SignalShutdown() { + shuttingDown.Store(true) +} + +type Status struct { + Status string `json:"status"` +} + +func (h *Handler) Live(c fiber.Ctx) error { + return c.JSON(Status{Status: "ok"}) +} + +// Ready answers whether the synchronous mandatory path can be served. Both +// PostgreSQL and Redis are critical here: business data and sessions live in +// them respectively. +func (h *Handler) Ready(c fiber.Ctx) error { + if shuttingDown.Load() { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "service is shutting down") + } + + if err := h.store.Ping(c.Context()); err != nil { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "database unavailable") + } + + if err := h.sessions.Ping(c.Context()); err != nil { + return common.SendError(c, fiber.StatusServiceUnavailable, + common.ErrorCodeUnavailable, "session store unavailable") + } + + return c.JSON(Status{Status: "ok"}) +} diff --git a/app/internal/health/router.go b/app/internal/health/router.go new file mode 100644 index 00000000..776587ff --- /dev/null +++ b/app/internal/health/router.go @@ -0,0 +1,13 @@ +package health + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +func RegisterRoutes(router fiber.Router, st *store.Store, sessions *session.Store) { + handler := NewHandler(st, sessions) + router.Get("/live", handler.Live) + router.Get("/ready", handler.Ready) +} diff --git a/app/internal/messages/handler.go b/app/internal/messages/handler.go new file mode 100644 index 00000000..189a22f3 --- /dev/null +++ b/app/internal/messages/handler.go @@ -0,0 +1,72 @@ +package messages + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" + "github.com/google/uuid" +) + +type Handler struct { + store *store.Store +} + +func NewHandler(st *store.Store) *Handler { + return &Handler{store: st} +} + +type CreateMessageRequest struct { + Text string `json:"text"` +} + +func (h *Handler) CreateMessage(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := uuid.Parse(c.Params("chat_id")) + if err != nil { + return common.SendError(c, fiber.StatusBadRequest, + common.ErrorCodeInvalidRequest, "invalid chat_id format") + } + + var req CreateMessageRequest + if err := common.BindJSON(c, &req); err != nil { + return common.SendAppError(c, err) + } + if msg := common.ValidateMessageText(req.Text); msg != "" { + return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg) + } + + message, err := h.store.CreateMessage(c.Context(), chatID, userID, req.Text) + if err != nil { + return common.SendAppError(c, err) + } + + return c.Status(fiber.StatusCreated).JSON(message) +} + +func (h *Handler) ListMessages(c fiber.Ctx) error { + userID := common.MustUserID(c) + + chatID, err := uuid.Parse(c.Params("chat_id")) + if err != nil { + return common.SendError(c, fiber.StatusBadRequest, + common.ErrorCodeInvalidRequest, "invalid chat_id format") + } + + limit, err := common.ParseLimit(c, 50) + if err != nil { + return common.SendAppError(c, err) + } + + cursor, err := common.ParseCursor(c) + if err != nil { + return common.SendAppError(c, err) + } + + page, err := h.store.ListMessages(c.Context(), userID, chatID, limit, cursor) + if err != nil { + return common.SendAppError(c, err) + } + + return c.JSON(page) +} diff --git a/app/internal/messages/router.go b/app/internal/messages/router.go new file mode 100644 index 00000000..f370f314 --- /dev/null +++ b/app/internal/messages/router.go @@ -0,0 +1,12 @@ +package messages + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +func RegisterProtectedRoutes(router fiber.Router, st *store.Store) { + handler := NewHandler(st) + router.Post("/chats/:chat_id/messages", handler.CreateMessage) + router.Get("/chats/:chat_id/messages", handler.ListMessages) +} diff --git a/app/internal/model/model.go b/app/internal/model/model.go new file mode 100644 index 00000000..d4276282 --- /dev/null +++ b/app/internal/model/model.go @@ -0,0 +1,47 @@ +package model + +import ( + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "github.com/google/uuid" +) + +type User struct { + ID uuid.UUID `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + PasswordHash string `json:"-"` + CreatedAt common.RFCFormTime `json:"created_at"` +} + +func (u User) GetID() uuid.UUID { return u.ID } +func (u User) GetCreatedAt() time.Time { return time.Time(u.CreatedAt) } + +type Chat struct { + ID uuid.UUID `json:"id"` + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` + CreatedAt common.RFCFormTime `json:"created_at"` +} + +func (c Chat) GetID() uuid.UUID { return c.ID } +func (c Chat) GetCreatedAt() time.Time { return time.Time(c.CreatedAt) } + +type Message struct { + ID uuid.UUID `json:"id"` + ChatID uuid.UUID `json:"chat_id"` + SenderID uuid.UUID `json:"sender_id"` + Text string `json:"text"` + CreatedAt common.RFCFormTime `json:"created_at"` +} + +func (m Message) GetID() uuid.UUID { return m.ID } +func (m Message) GetCreatedAt() time.Time { return time.Time(m.CreatedAt) } + +type Session struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt common.RFCFormTime `json:"expires_at"` + User User `json:"user"` +} diff --git a/app/internal/session/store.go b/app/internal/session/store.go new file mode 100644 index 00000000..fe423c8a --- /dev/null +++ b/app/internal/session/store.go @@ -0,0 +1,106 @@ +// AGENT: Redis is the single session store for lab 2 grade 4. PostgreSQL stays +// the source of truth for business data. The token itself is never stored: Redis +// holds SHA-256(token) -> user id with a TTL, so a leaked Redis dump does not +// hand out usable bearer tokens. +package session + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "strings" + "time" + + "github.com/google/uuid" + "github.com/redis/go-redis/v9" +) + +var ErrNotFound = errors.New("session not found") + +const keyPrefix = "messenger:session:" + +type Store struct { + client *redis.Client + ttl time.Duration + timeout time.Duration +} + +func New(rawURL string, ttl, timeout time.Duration) (*Store, error) { + options, err := redis.ParseURL(rawURL) + if err != nil { + return nil, fmt.Errorf("parse CACHE_URL: %w", err) + } + options.PoolSize = 10 + options.DialTimeout = timeout + options.ReadTimeout = timeout + options.WriteTimeout = timeout + options.MaxRetries = 0 + + return &Store{ + client: redis.NewClient(options), + ttl: ttl, + timeout: timeout, + }, nil +} + +func (s *Store) Close() error { + return s.client.Close() +} + +func (s *Store) Ping(ctx context.Context) error { + pingCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return s.client.Ping(pingCtx).Err() +} + +// Create returns the raw token exactly once and stores only its digest. +func (s *Store) Create(ctx context.Context, userID uuid.UUID) (string, time.Time, error) { + raw := make([]byte, 32) + if _, err := rand.Read(raw); err != nil { + return "", time.Time{}, fmt.Errorf("generate token: %w", err) + } + token := base64.RawURLEncoding.EncodeToString(raw) + expiresAt := time.Now().UTC().Add(s.ttl) + + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + if err := s.client.Set(opCtx, keyFor(token), userID.String(), s.ttl).Err(); err != nil { + return "", time.Time{}, err + } + + return token, expiresAt, nil +} + +func (s *Store) Lookup(ctx context.Context, token string) (uuid.UUID, error) { + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + + value, err := s.client.Get(opCtx, keyFor(token)).Result() + if errors.Is(err, redis.Nil) { + return uuid.Nil, ErrNotFound + } + if err != nil { + return uuid.Nil, err + } + + userID, err := uuid.Parse(value) + if err != nil { + return uuid.Nil, ErrNotFound + } + return userID, nil +} + +func (s *Store) Revoke(ctx context.Context, token string) error { + opCtx, cancel := context.WithTimeout(ctx, s.timeout) + defer cancel() + return s.client.Del(opCtx, keyFor(token)).Err() +} + +func keyFor(token string) string { + sum := sha256.Sum256([]byte(strings.TrimSpace(token))) + return keyPrefix + hex.EncodeToString(sum[:]) +} diff --git a/app/internal/store/chats.go b/app/internal/store/chats.go new file mode 100644 index 00000000..b2ff65d4 --- /dev/null +++ b/app/internal/store/chats.go @@ -0,0 +1,148 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +// CreateChat inserts the chat and every membership row in one transaction. +// Any unknown member aborts the whole transaction, so a partially created chat +// can never be observed. +func (s *Store) CreateChat(ctx context.Context, title string, memberIDs []uuid.UUID) (model.Chat, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Chat{}, dbError(err) + } + defer func() { _ = tx.Rollback(context.Background()) }() + + for _, memberID := range memberIDs { + var exists int + err = tx.QueryRow(ctx, `SELECT 1 FROM users WHERE id = $1`, memberID).Scan(&exists) + if errors.Is(err, pgx.ErrNoRows) { + return model.Chat{}, common.BadRequest("member_id refers to non-existent user") + } + if err != nil { + return model.Chat{}, dbError(err) + } + } + + chat := model.Chat{ + ID: uuid.New(), + Title: title, + MemberIDs: append([]uuid.UUID(nil), memberIDs...), + CreatedAt: common.NowUTC(), + } + + if _, err = tx.Exec(ctx, + `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`, + chat.ID, chat.Title, time.Time(chat.CreatedAt)); err != nil { + return model.Chat{}, dbError(err) + } + + for _, memberID := range memberIDs { + if _, err = tx.Exec(ctx, + `INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)`, + chat.ID, memberID); err != nil { + return model.Chat{}, dbError(err) + } + } + + if err = tx.Commit(ctx); err != nil { + return model.Chat{}, dbError(err) + } + + return chat, nil +} + +func (s *Store) GetChatForUser(ctx context.Context, userID, chatID uuid.UUID) (model.Chat, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + chat, err := scanChat(s.pool.QueryRow(ctx, ` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY user_id) + FROM chats c + JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $2 + WHERE c.id = $1`, chatID, userID)) + if errors.Is(err, pgx.ErrNoRows) { + return model.Chat{}, common.NotFound("chat not found") + } + if err != nil { + return model.Chat{}, dbError(err) + } + return chat, nil +} + +func (s *Store) ListChats(ctx context.Context, userID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.Page[model.Chat], error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + cursorTime, cursorID := cursorValues(cursor) + rows, err := s.pool.Query(ctx, ` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT user_id FROM chat_members WHERE chat_id = c.id ORDER BY user_id) + FROM chats c + JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $1 + WHERE (c.created_at, c.id) > ($2, $3) + ORDER BY c.created_at, c.id + LIMIT $4`, userID, cursorTime, cursorID, limit+1) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + chats := make([]model.Chat, 0, limit) + for rows.Next() { + chat, scanErr := scanChat(rows) + if scanErr != nil { + return nil, dbError(scanErr) + } + chats = append(chats, chat) + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + + hasMore := len(chats) > limit + if hasMore { + chats = chats[:limit] + } + + return &common.Page[model.Chat]{Items: chats, NextCursor: common.NextCursor(chats, hasMore, userID)}, nil +} + +func (s *Store) IsMember(ctx context.Context, userID, chatID uuid.UUID) (bool, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + var exists int + err := s.pool.QueryRow(ctx, + `SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`, + chatID, userID).Scan(&exists) + if errors.Is(err, pgx.ErrNoRows) { + return false, nil + } + if err != nil { + return false, dbError(err) + } + return true, nil +} + +func scanChat(row rowScanner) (model.Chat, error) { + var chat model.Chat + var createdAt time.Time + if err := row.Scan(&chat.ID, &chat.Title, &createdAt, &chat.MemberIDs); err != nil { + return model.Chat{}, err + } + chat.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return chat, nil +} diff --git a/app/internal/store/errors.go b/app/internal/store/errors.go new file mode 100644 index 00000000..0f60f264 --- /dev/null +++ b/app/internal/store/errors.go @@ -0,0 +1,44 @@ +package store + +import ( + "context" + "errors" + "net" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +func isUniqueViolation(err error) bool { + var pgErr *pgconn.PgError + return errors.As(err, &pgErr) && pgErr.Code == "23505" +} + +// dbError separates a drained dependency (503) from a real bug (500). SQL +// details never reach the client; callers only emit the AppError message. +func dbError(err error) error { + if err == nil { + return nil + } + + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + switch pgErr.Code { + case "23505": + return common.Conflict("resource already exists") + case "23503", "23514", "22P02": + return common.BadRequest("request conflicts with stored data") + } + } + + var netErr net.Error + if errors.As(err, &netErr) || errors.Is(err, context.DeadlineExceeded) || + errors.Is(err, context.Canceled) || + pgconn.SafeToRetry(err) || + errors.Is(err, pgx.ErrTxClosed) { + return common.Unavailable("database unavailable") + } + + return common.Internal() +} diff --git a/app/internal/store/messages.go b/app/internal/store/messages.go new file mode 100644 index 00000000..5454d593 --- /dev/null +++ b/app/internal/store/messages.go @@ -0,0 +1,104 @@ +package store + +import ( + "context" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model" + "github.com/google/uuid" +) + +// CreateMessage uses INSERT ... SELECT so the membership check and the insert +// are a single atomic statement: no window where the user is removed between +// check and write. Zero affected rows means the chat is missing or foreign. +func (s *Store) CreateMessage(ctx context.Context, chatID, senderID uuid.UUID, text string) (model.Message, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + message := model.Message{ + ID: uuid.New(), + ChatID: chatID, + SenderID: senderID, + Text: text, + CreatedAt: common.NowUTC(), + } + + tag, err := s.pool.Exec(ctx, ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + SELECT $1, $2, $3, $4, $5 + WHERE EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $2 AND user_id = $3)`, + message.ID, message.ChatID, message.SenderID, message.Text, time.Time(message.CreatedAt)) + if err != nil { + return model.Message{}, dbError(err) + } + if tag.RowsAffected() == 0 { + return model.Message{}, common.NotFound("chat not found") + } + + return message, nil +} + +func (s *Store) ListMessages(ctx context.Context, userID, chatID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.Page[model.Message], error) { + isMember, err := s.IsMember(ctx, userID, chatID) + if err != nil { + return nil, err + } + if !isMember { + return nil, common.NotFound("chat not found") + } + // AGENT: Cursor context is checked only after membership, so a foreign + // caller gets 404 and cannot probe cursor validity against someone else's chat. + if cursor != nil && cursor.ContextID != chatID { + return nil, common.BadRequest("bad cursor value") + } + + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + // AGENT: limit+1 is fetched so next_cursor is known to be non-null without a + // second COUNT query; the extra row is never returned to the client. + cursorTime, cursorID := cursorValues(cursor) + rows, err := s.pool.Query(ctx, ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1 AND (created_at, id) > ($2, $3) + ORDER BY created_at, id + LIMIT $4`, chatID, cursorTime, cursorID, limit+1) + if err != nil { + return nil, dbError(err) + } + defer rows.Close() + + messages := make([]model.Message, 0, limit) + for rows.Next() { + message, scanErr := scanMessage(rows) + if scanErr != nil { + return nil, dbError(scanErr) + } + messages = append(messages, message) + } + if err = rows.Err(); err != nil { + return nil, dbError(err) + } + + hasMore := len(messages) > limit + if hasMore { + messages = messages[:limit] + } + + return &common.Page[model.Message]{ + Items: messages, + NextCursor: common.NextCursor(messages, hasMore, chatID), + }, nil +} + +func scanMessage(row rowScanner) (model.Message, error) { + var message model.Message + var createdAt time.Time + if err := row.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &createdAt); err != nil { + return model.Message{}, err + } + message.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return message, nil +} diff --git a/app/internal/store/pagination.go b/app/internal/store/pagination.go new file mode 100644 index 00000000..dd4a342a --- /dev/null +++ b/app/internal/store/pagination.go @@ -0,0 +1,17 @@ +package store + +import ( + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "github.com/google/uuid" +) + +// cursorValues converts an optional page cursor to the two keyset columns used +// by every paginated query. A nil cursor means "start from the beginning". +func cursorValues(cursor *common.CursorKeyCtx) (time.Time, uuid.UUID) { + if cursor == nil { + return time.Time{}, uuid.Nil + } + return cursor.Time, cursor.ID +} diff --git a/app/internal/store/store.go b/app/internal/store/store.go new file mode 100644 index 00000000..102b2ada --- /dev/null +++ b/app/internal/store/store.go @@ -0,0 +1,40 @@ +// Package store contains the PostgreSQL persistence layer. It uses plain +// parameterized SQL, one bounded pool and explicit transaction boundaries. +// Contract errors are returned as common.AppError so HTTP status mapping stays +// in one place. +// +// AGENT: Persistence was split from one large file by aggregate (users, chats, +// messages). Queries, transaction boundaries and error mapping are unchanged; +// only file organization and scan helpers changed. +package store + +import ( + "context" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +type Store struct { + pool *pgxpool.Pool + timeout time.Duration +} + +func New(pool *pgxpool.Pool, timeout time.Duration) *Store { + return &Store{pool: pool, timeout: timeout} +} + +func (s *Store) withTimeout(ctx context.Context) (context.Context, context.CancelFunc) { + return context.WithTimeout(ctx, s.timeout) +} + +func (s *Store) Ping(ctx context.Context) error { + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + return s.pool.Ping(pingCtx) +} + +// rowScanner is the common subset of pgx.Row and pgx.Rows used by scan helpers. +type rowScanner interface { + Scan(dest ...any) error +} diff --git a/app/internal/store/users.go b/app/internal/store/users.go new file mode 100644 index 00000000..e0e50138 --- /dev/null +++ b/app/internal/store/users.go @@ -0,0 +1,72 @@ +package store + +import ( + "context" + "errors" + "time" + + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model" + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +func (s *Store) CreateUser(ctx context.Context, username, displayName, passwordHash string) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + user := model.User{ + ID: uuid.New(), + Username: username, + DisplayName: displayName, + PasswordHash: passwordHash, + CreatedAt: common.NowUTC(), + } + + _, err := s.pool.Exec(ctx, ` + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1, $2, $3, $4, $5)`, + user.ID, user.Username, user.DisplayName, user.PasswordHash, time.Time(user.CreatedAt)) + if err != nil { + // AGENT: The unique index is the real guard. Two concurrent registrations + // with the same username both reach INSERT; exactly one commits, the other + // gets SQLSTATE 23505 and is mapped to 409 instead of a 500. + if isUniqueViolation(err) { + return model.User{}, common.Conflict("username already taken") + } + return model.User{}, dbError(err) + } + + return user, nil +} + +func (s *Store) GetUserByID(ctx context.Context, id uuid.UUID) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + return s.scanUser(s.pool.QueryRow(ctx, ` + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE id = $1`, id)) +} + +func (s *Store) GetUserByUsername(ctx context.Context, username string) (model.User, error) { + ctx, cancel := s.withTimeout(ctx) + defer cancel() + + return s.scanUser(s.pool.QueryRow(ctx, ` + SELECT id, username, display_name, password_hash, created_at + FROM users WHERE username = $1`, username)) +} + +func (s *Store) scanUser(row rowScanner) (model.User, error) { + var user model.User + var createdAt time.Time + if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash, &createdAt); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return model.User{}, common.NotFound("user not found") + } + return model.User{}, dbError(err) + } + user.CreatedAt = common.RFCFormTime(createdAt.UTC()) + return user, nil +} diff --git a/app/internal/users/handler.go b/app/internal/users/handler.go new file mode 100644 index 00000000..ff88cbb9 --- /dev/null +++ b/app/internal/users/handler.go @@ -0,0 +1,62 @@ +package users + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common" + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +type Handler struct { + store *store.Store +} + +func NewHandler(st *store.Store) *Handler { + return &Handler{store: st} +} + +type CreateUserRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} + +func (h *Handler) CreateUser(c fiber.Ctx) error { + var req CreateUserRequest + if err := common.BindJSON(c, &req); err != nil { + return common.SendAppError(c, err) + } + + if msg := common.ValidateCreateUser(req.Username, req.DisplayName); msg != "" { + return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg) + } + if msg := common.ValidatePassword(req.Password); msg != "" { + return common.SendError(c, fiber.StatusBadRequest, common.ErrorCodeInvalidRequest, msg) + } + + hash, err := auth.HashPassword(req.Password) + if err != nil { + return common.SendAppError(c, common.Internal()) + } + + user, err := h.store.CreateUser(c.Context(), req.Username, req.DisplayName, hash) + if err != nil { + return common.SendAppError(c, err) + } + + return c.Status(fiber.StatusCreated).JSON(user) +} + +func (h *Handler) GetCurrentUser(c fiber.Ctx) error { + userID, ok := common.UserID(c) + if !ok { + return common.SendChallenge(c, `Bearer realm="messenger"`, "authentication required") + } + + user, err := h.store.GetUserByID(c.Context(), userID) + if err != nil { + return common.SendAppError(c, err) + } + + return c.JSON(user) +} diff --git a/app/internal/users/router.go b/app/internal/users/router.go new file mode 100644 index 00000000..cf0c6f5f --- /dev/null +++ b/app/internal/users/router.go @@ -0,0 +1,16 @@ +package users + +import ( + "gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store" + "github.com/gofiber/fiber/v3" +) + +func RegisterPublicRoutes(router fiber.Router, st *store.Store) { + handler := NewHandler(st) + router.Post("/users", handler.CreateUser) +} + +func RegisterProtectedRoutes(router fiber.Router, st *store.Store) { + handler := NewHandler(st) + router.Get("/users/me", handler.GetCurrentUser) +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..e4500734 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,96 @@ +# AGENT: Single local stack for lab 2 grade 4. PostgreSQL is the source of +# truth, Redis stores sessions with AOF persistence. Both have named volumes, +# so `docker compose restart` preserves data and `down -v` is never needed. +name: mtusi2026lab2 + +x-api-environment: &api-environment + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + DB_POOL_MAX_CONNS: ${DB_POOL_MAX_CONNS:-10} + DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable + CACHE_URL: redis://redis:6379/0 + +services: + postgres: + image: postgres:16.10-alpine + restart: unless-stopped + environment: + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env} + POSTGRES_DB: ${POSTGRES_DB:-messenger} + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 3s + timeout: 3s + retries: 20 + start_period: 5s + + redis: + image: redis:7.4.7-alpine + restart: unless-stopped + # AOF survives a container restart and keeps absolute key TTLs, which is + # exactly what the session-persistence check needs. + command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec", "--save", ""] + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 20 + start_period: 3s + + api: + build: + context: . + dockerfile: Dockerfile + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + environment: + <<: *api-environment + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + ports: + - "${APP_PORT_PUBLISH:-8080}:8080" + healthcheck: + test: ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"] + interval: 5s + timeout: 4s + retries: 12 + start_period: 15s + stop_grace_period: 15s + + # AGENT: Only for the short-TTL expiry check. It shares PostgreSQL and Redis + # with the main API, so its 2-second sessions are visible to every instance. + api-short-ttl: + profiles: ["checks"] + build: + context: . + dockerfile: Dockerfile + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + environment: + <<: *api-environment + SESSION_TTL_SECONDS: "2" + ports: + - "${APP_SHORT_TTL_PORT:-8081}:8080" + healthcheck: + test: ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"] + interval: 5s + timeout: 4s + retries: 12 + start_period: 15s + stop_grace_period: 15s + +volumes: + pgdata: + redisdata: diff --git a/evidence/00-environment.txt b/evidence/00-environment.txt new file mode 100644 index 00000000..fb9e07d8 --- /dev/null +++ b/evidence/00-environment.txt @@ -0,0 +1,14 @@ +# Environment and fixed versions +date_utc: 2026-10-20T13:35:48Z +kernel: Linux 6.12.73+deb13-amd64 x86_64 +cpus: 4 +memory: 7.5Gi +go_module: go version go1.27.1 linux/amd64 +python: Python 3.12.3 +docker_client: 29.9.1 +docker_server: 29.3.0 +compose: 5.6.0 +images: + postgres:16.10-alpine + redis:7.4.7-alpine + mtusi2026lab2-api diff --git a/evidence/01-make-check.txt b/evidence/01-make-check.txt new file mode 100644 index 00000000..f94b9f01 --- /dev/null +++ b/evidence/01-make-check.txt @@ -0,0 +1,3 @@ +$ make check +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/02-go-build-test.txt b/evidence/02-go-build-test.txt new file mode 100644 index 00000000..ff5ee4bd --- /dev/null +++ b/evidence/02-go-build-test.txt @@ -0,0 +1,14 @@ +$ cd app && go build ./... && go vet ./... && go test ./... +? gitflic.ru/project/bulla-max/mtusi2026lab2/cmd/server [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal [no test files] +ok gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth (cached) +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/chats [no test files] +ok gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common (cached) +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/database [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/health [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/messages [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store [no test files] +? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/users [no test files] diff --git a/evidence/03-make-validate.txt b/evidence/03-make-validate.txt new file mode 100644 index 00000000..9ad62baa --- /dev/null +++ b/evidence/03-make-validate.txt @@ -0,0 +1,3 @@ +$ make validate PYTHON=/tmp/lab2-venv/bin/python +/tmp/lab2-venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/04-compose.txt b/evidence/04-compose.txt new file mode 100644 index 00000000..fe2859a6 --- /dev/null +++ b/evidence/04-compose.txt @@ -0,0 +1,9 @@ +$ docker compose --env-file .env config --quiet +OK: compose file and .env are valid +$ docker compose --env-file .env config --services +redis +postgres +api +$ docker compose --env-file .env config --volumes +pgdata +redisdata diff --git a/evidence/05-smoke.txt b/evidence/05-smoke.txt new file mode 100644 index 00000000..267d2332 --- /dev/null +++ b/evidence/05-smoke.txt @@ -0,0 +1,3 @@ +$ make test BASE_URL=http://172.18.0.1:8080 +python3 tests/smoke.py --base-url "http://172.18.0.1:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/06-persistence.txt b/evidence/06-persistence.txt new file mode 100644 index 00000000..ae5af4c5 --- /dev/null +++ b/evidence/06-persistence.txt @@ -0,0 +1,32 @@ +# Persistence hook scope: real container restarts, volumes preserved +$ docker inspect before +/api started=2026-10-20T13:43:34.744581601Z restarts=0 +/postgres started=2026-10-20T13:45:47.006469682Z restarts=0 +/redis started=2026-10-20T13:45:33.276541726Z restarts=0 + +$ make test-persistence BASE_URL=http://172.18.0.1:8080 ACTION_SCRIPT=scripts/restart.sh +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://172.18.0.1:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +$ docker inspect after +/api started=2026-10-20T13:46:01.985254148Z restarts=0 +/postgres started=2026-10-20T13:46:00.989433719Z restarts=0 +/redis started=2026-10-20T13:46:01.023470617Z restarts=0 + +$ docker compose ps +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab2-api-1 sha256:4e5079fddfd7792f1ccf90891259fc4d737a8486a7a92461dcd72c6b0fd6914d "/usr/local/bin/serv…" api 2 minutes ago Up 5 seconds (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi2026lab2-api-short-ttl-1 mtusi2026lab2-api-short-ttl "/usr/local/bin/serv…" api-short-ttl 2 minutes ago Up 2 minutes (healthy) 0.0.0.0:8081->8080/tcp, [::]:8081->8080/tcp +mtusi2026lab2-postgres-1 postgres:16.10-alpine "docker-entrypoint.s…" postgres 10 minutes ago Up 6 seconds (healthy) 5432/tcp +mtusi2026lab2-redis-1 redis:7.4.7-alpine "docker-entrypoint.s…" redis 10 minutes ago Up 6 seconds (healthy) 6379/tcp + +$ docker volume ls (project volumes) +mtusi2026lab2_pgdata +mtusi2026lab2_redisdata + +$ redis AOF files +total 20 +-rw------- 1 redis redis 88 Oct 20 13:35 appendonly.aof.1.base.rdb +-rw------- 1 redis redis 11185 Oct 20 13:46 appendonly.aof.1.incr.aof +-rw------- 1 redis redis 88 Oct 20 13:35 appendonly.aof.manifest diff --git a/evidence/07-grade4-checks.txt b/evidence/07-grade4-checks.txt new file mode 100644 index 00000000..1c274db3 --- /dev/null +++ b/evidence/07-grade4-checks.txt @@ -0,0 +1,27 @@ +$ docker compose --env-file .env --profile checks up -d --wait api-short-ttl + Container mtusi2026lab2-postgres-1 Running + Container mtusi2026lab2-redis-1 Running + Container mtusi2026lab2-api-short-ttl-1 Running + Container mtusi2026lab2-redis-1 Waiting + Container mtusi2026lab2-postgres-1 Waiting + Container mtusi2026lab2-postgres-1 Healthy + Container mtusi2026lab2-redis-1 Healthy + Container mtusi2026lab2-redis-1 Waiting + Container mtusi2026lab2-api-short-ttl-1 Waiting + Container mtusi2026lab2-postgres-1 Waiting + Container mtusi2026lab2-api-short-ttl-1 Healthy + Container mtusi2026lab2-redis-1 Healthy + Container mtusi2026lab2-postgres-1 Healthy + +$ python3 scripts/grade4_checks.py --base-url http://172.18.0.1:8080 --short-ttl-url http://172.18.0.1:8081 +PASS: wrong password -> 401 with Basic challenge +PASS: logout revoked the current token (repeat -> 401) +PASS: X-User-Id is ignored and grants no access +PASS: 12 concurrent registrations of one username -> one 201, eleven 409 +PASS: failed composite chat creation rolled back completely +PASS: SESSION_TTL_SECONDS=2 session expired and was rejected by the main API +PASS: redis down: live=200, ready=503, protected=503 (bounded unavailable error, no anonymous access; detected in 4.4s) +PASS: redis recovered in 4.0s; same token and confirmed message intact +PASS: postgres down: live=200, ready=503, protected=503 (bounded unavailable error, session retained; detected in 2.8s) +PASS: postgres recovered in 0.9s; same token and confirmed message intact +PASS: grade 4 checks completed diff --git a/evidence/08-explain.txt b/evidence/08-explain.txt new file mode 100644 index 00000000..7c4cb6e8 --- /dev/null +++ b/evidence/08-explain.txt @@ -0,0 +1,35 @@ +$ ./scripts/seed_and_explain.sh +INSERT 0 0 +INSERT 0 0 +INSERT 0 0 +DELETE 10000 +INSERT 0 10000 +VACUUM + seeded_messages +----------------- + 10000 +(1 row) + + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.41..7.03 rows=51 width=70) (actual time=0.018..0.037 rows=51 loops=1) + Buffers: shared hit=6 + -> Index Scan using messages_chat_created_id_idx on messages (cost=0.41..1298.32 rows=9999 width=70) (actual time=0.017..0.032 rows=51 loops=1) + Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'::uuid) AND (ROW(created_at, id) > ROW('0001-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Buffers: shared hit=6 + Planning: + Buffers: shared hit=18 + Planning Time: 0.208 ms + Execution Time: 0.104 ms +(9 rows) + + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.41..7.83 rows=51 width=70) (actual time=0.039..0.054 rows=51 loops=1) + Buffers: shared hit=5 + -> Index Scan using messages_chat_created_id_idx on messages (cost=0.41..1055.85 rows=7259 width=70) (actual time=0.038..0.049 rows=51 loops=1) + Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:23:20+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))) + Buffers: shared hit=5 + Planning Time: 0.073 ms + Execution Time: 0.087 ms +(7 rows) diff --git a/evidence/09-load.txt b/evidence/09-load.txt new file mode 100644 index 00000000..66fa7f6d --- /dev/null +++ b/evidence/09-load.txt @@ -0,0 +1,18 @@ +$ python3 tests/load.py --base-url http://172.18.0.1:8080 --duration 10 --concurrency 4 --output evidence/load.json +{ + "lab": 2, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 10.0, + "elapsed_seconds": 10.089, + "concurrency": 4, + "successful_requests": 8854, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 877.599, + "successful_latency_ms": { + "p50": 4.06, + "p95": 7.835, + "p99": 10.654 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/10-final-state.txt b/evidence/10-final-state.txt new file mode 100644 index 00000000..9e46332b --- /dev/null +++ b/evidence/10-final-state.txt @@ -0,0 +1,17 @@ +$ docker compose --env-file .env --profile checks ps +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab2-api-1 sha256:4e5079fddfd7792f1ccf90891259fc4d737a8486a7a92461dcd72c6b0fd6914d "/usr/local/bin/serv…" api 3 minutes ago Up 22 seconds (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi2026lab2-api-short-ttl-1 mtusi2026lab2-api-short-ttl "/usr/local/bin/serv…" api-short-ttl 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:8081->8080/tcp, [::]:8081->8080/tcp +mtusi2026lab2-postgres-1 postgres:16.10-alpine "docker-entrypoint.s…" postgres 10 minutes ago Up 23 seconds (healthy) 5432/tcp +mtusi2026lab2-redis-1 redis:7.4.7-alpine "docker-entrypoint.s…" redis 10 minutes ago Up 23 seconds (healthy) 6379/tcp + +$ curl -s -o /dev/null -w "%{http_code} %{content_type}\\n" http://172.18.0.1:8080/health/ready +200 application/json; charset=utf-8 + +$ docker volume ls --filter name=mtusi2026lab2 +DRIVER VOLUME NAME +local mtusi2026lab2_pgdata +local mtusi2026lab2_redisdata + +$ redis-cli --scan --pattern "messenger:session:*" | wc -l +46 diff --git a/evidence/11-schema.txt b/evidence/11-schema.txt new file mode 100644 index 00000000..710b5051 --- /dev/null +++ b/evidence/11-schema.txt @@ -0,0 +1,66 @@ +$ docker compose --env-file .env exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\\d users" -c "\\d chats" -c "\\d chat_members" -c "\\d messages" -c "TABLE schema_migrations" + Table "public.users" + Column | Type | Collation | Nullable | Default +---------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + username | text | | not null | + display_name | text | | not null | + password_hash | text | | not null | + created_at | timestamp with time zone | | not null | +Indexes: + "users_pkey" PRIMARY KEY, btree (id) + "users_username_key" UNIQUE CONSTRAINT, btree (username) +Check constraints: + "users_display_name_len" CHECK (char_length(display_name) >= 1 AND char_length(display_name) <= 100) + "users_username_format" CHECK (username ~ '^[a-z0-9_]{3,32}$'::text) +Referenced by: + TABLE "chat_members" CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT + TABLE "messages" CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE RESTRICT + + Table "public.chats" + Column | Type | Collation | Nullable | Default +------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + title | text | | not null | + created_at | timestamp with time zone | | not null | +Indexes: + "chats_pkey" PRIMARY KEY, btree (id) +Check constraints: + "chats_title_len" CHECK (char_length(title) >= 1 AND char_length(title) <= 100) +Referenced by: + TABLE "chat_members" CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + TABLE "messages" CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + + Table "public.chat_members" + Column | Type | Collation | Nullable | Default +---------+------+-----------+----------+--------- + chat_id | uuid | | not null | + user_id | uuid | | not null | +Indexes: + "chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id) + "chat_members_user_idx" btree (user_id, chat_id) +Foreign-key constraints: + "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT + + Table "public.messages" + Column | Type | Collation | Nullable | Default +------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + chat_id | uuid | | not null | + sender_id | uuid | | not null | + text | text | | not null | + created_at | timestamp with time zone | | not null | +Indexes: + "messages_pkey" PRIMARY KEY, btree (id) + "messages_chat_created_id_idx" btree (chat_id, created_at, id) +Check constraints: + "messages_text_len" CHECK (char_length(text) >= 1 AND char_length(text) <= 4000) +Foreign-key constraints: + "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE RESTRICT + + version | applied_at +---------------+----------------------------- + 0001_init.sql | 2026-10-20 13:35:37.6134+00 +(1 row) diff --git a/evidence/12-git-hygiene.txt b/evidence/12-git-hygiene.txt new file mode 100644 index 00000000..fc69a134 --- /dev/null +++ b/evidence/12-git-hygiene.txt @@ -0,0 +1,6 @@ +# Git hygiene: no secrets, volumes or build artifacts tracked +$ git ls-files | grep -E "(^|/)\\.env$|app/tmp|volumes/|(^|/)server$" || echo "OK: none tracked" +OK: none tracked + +$ git check-ignore -v .env +.gitignore:2:.env .env diff --git a/evidence/load.json b/evidence/load.json new file mode 100644 index 00000000..3ff07d0e --- /dev/null +++ b/evidence/load.json @@ -0,0 +1,17 @@ +{ + "lab": 2, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 10.0, + "elapsed_seconds": 10.089, + "concurrency": 4, + "successful_requests": 8854, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 877.599, + "successful_latency_ms": { + "p50": 4.06, + "p95": 7.835, + "p99": 10.654 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/research/README.md b/research/README.md new file mode 100644 index 00000000..cbdf9328 --- /dev/null +++ b/research/README.md @@ -0,0 +1,113 @@ +# Track 2: query plan and recovery + +Hypothesis: at 300k messages the history index is worth keeping even under a +tight memory budget, while the membership index is the cheaper one to remove. +Recovery time should grow linearly with message count, so the dump-loss window +can be estimated from a few points. + +## Dataset + +One PostgreSQL 16.10 database, `messenger_research`: + +- 5,000 chats, 2,000 users, 72,300 memberships. +- 100k / 200k / 300k messages, timestamps spread over 90 days. +- Chat sizes are deliberately skewed (`random()^3`): the largest chat holds + about 6% of all messages, most chats stay small. +- Probe user 1 belongs to the ten largest chats and every 100th chat, so the + chat-list and cross-chat feed queries always return rows. +- The app database `messenger` is not touched. + +Schema is applied from `app/internal/database/migrations/0001_init.up.sql`. +The two compared indexes are: + +- `chat_members_user_idx (user_id, chat_id)` +- `messages_chat_created_id_idx (chat_id, created_at, id)` + +## Index experiment + +`bench_indexes.sh` rebuilds the dataset at 300k, warms each query, then reports +the median of five `EXPLAIN (ANALYZE, BUFFERS)` runs. The comparison states have +exactly one of the two indexes; the script asserts that state and restores both +indexes at the end. + +| state | user_chats ms | chat_messages ms | user_feed ms | +| --- | ---: | ---: | ---: | +| both indexes | 1.38 | 0.43 | 35.40 | +| only `messages_chat_created_id_idx` | 7.36 | 0.47 | 35.22 | +| only `chat_members_user_idx` | 1.40 | 57.63 | 80.40 | + +Query shapes: + +- `user_chats`: `chats JOIN chat_members WHERE user_id = $1 ORDER BY created_at, id LIMIT 50` +- `chat_messages`: keyset page of one chat on `(created_at, id)` +- `user_feed`: `messages JOIN chat_members WHERE user_id = $1` plus a keyset + cursor, ordered globally by `(created_at, id)` + +Index sizes after `REINDEX` (300k messages, both present): + +- `chat_members_user_idx`: about 3.5 MB +- `messages_chat_created_id_idx`: about 17 MB +- `messages` table including indexes: about 75 MB + +Write cost (pgbench, 4 clients, 10 s, message-like inserts on a clone table): +2,719 tps with the messages index, 2,640 tps without it. The difference is +small and within run noise; the read benefit below is much larger. + +## Recovery experiment + +`bench_recovery.sh` recreates the database at each size from the migration, +seeds it, checks that both indexes exist, then runs `pg_dump -Fc` and +`pg_restore` into a fresh database. It verifies message and membership counts, +an MD5 fingerprint of every message row, and both indexes in the restored copy. + +| messages | dump s | restore s | dump MiB | DB MiB | lost msgs at 878 RPS | +| ---: | ---: | ---: | ---: | ---: | ---: | +| 100,000 | 1.02 | 1.44 | 4.9 | 45.2 | 896 | +| 200,000 | 1.92 | 2.36 | 9.3 | 70.8 | 1,686 | +| 300,000 | 2.66 | 3.28 | 13.8 | 95.0 | 2,335 | + +Linear fits (`N` = messages in millions): + +- dump: `t = 0.23 + 8.20 * N` s, R² = 1.00 +- restore: `t = 0.52 + 9.20 * N` s, R² = 1.00 +- dump + restore: `t = 0.75 + 17.4 * N` s, R² = 1.00 + +The loss column uses the closed-loop 878 RPS already measured in +`REPORT.md`. `pg_dump` takes a consistent snapshot and does not block writers; +the estimate means "writes committed after that snapshot are lost if the dump +is the recovery point". Total outage including an offline restore is +`dump + restore` times the write rate (about 2.2k, 3.7k and 5.2k messages at +the three sizes). + +## Decision + +Keep `messages_chat_created_id_idx`. Without it a chat page goes from 0.43 ms +to 57.6 ms and the cross-chat feed from 35.4 ms to 80.4 ms. If exactly one of +the two indexes must be removed, remove `chat_members_user_idx`: it costs about +3.5 MB and saves about 6 ms on the chat list, while the history index costs +about 17 MB and prevents a two-order-of-magnitude regression on history reads. +If the budget allows 21 MB for both, keep both. + +## Reproduce + +Requires Docker Compose and the repository `.env` (`cp .env.example .env`, +then change `POSTGRES_PASSWORD`). Only the `postgres` service is needed. + +```sh +docker compose --env-file .env up -d --wait postgres +./research/bench_indexes.sh +./research/bench_recovery.sh +python3 research/analyze.py | tee research/results/recovery.md +``` + +Outputs: `research/results/indexes.md`, `research/results/recovery.csv`, +`research/results/recovery.md`. + +## Limits + +- Synthetic skewed data, not a production trace. +- Single PostgreSQL node, warm cache, no concurrent writes during the query + benchmark. +- One dump/restore run per size; timings are indicative, not a capacity plan. +- The 878 RPS figure comes from the closed-loop client in `REPORT.md`; it is + not a maximum throughput claim. diff --git a/research/analyze.py b/research/analyze.py new file mode 100755 index 00000000..d9e7bbac --- /dev/null +++ b/research/analyze.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +"""Fit recovery time against message count and estimate the dump-loss window.""" +import csv +import os +from pathlib import Path + +ROOT = Path(__file__).resolve().parent +RPS = float(os.environ.get("RPS", "878")) # closed-loop POST RPS from REPORT.md +rows = list(csv.DictReader((ROOT / "results/recovery.csv").open())) + + +def fit(ys): + # AGENT: three points are enough for the linear trend this lab asks for; + # R^2 is printed so a bad fit is visible instead of silently accepted. + xs = [int(row["messages"]) for row in rows] + n = len(xs) + mx = sum(xs) / n + my = sum(ys) / n + slope = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs) + intercept = my - slope * mx + total = sum((y - my) ** 2 for y in ys) + residual = sum((y - (intercept + slope * x)) ** 2 for x, y in zip(xs, ys)) + r2 = 1 - residual / total if total else 1.0 + return intercept, slope, r2 + + +dump = fit([float(row["dump_s"]) for row in rows]) +restore = fit([float(row["restore_s"]) for row in rows]) +total = fit([float(row["dump_s"]) + float(row["restore_s"]) for row in rows]) + +print("| messages | dump s | restore s | dump MiB | DB MiB | lost msgs at %.0f RPS |" % RPS) +print("| ---: | ---: | ---: | ---: | ---: | ---: |") +for row in rows: + dump_s = float(row["dump_s"]) + print("| {messages} | {dump_s} | {restore_s} | {dump_mib:.1f} | {db_mib:.1f} | {loss:.0f} |".format( + messages=row["messages"], + dump_s=row["dump_s"], + restore_s=row["restore_s"], + dump_mib=int(row["dump_bytes"]) / 2**20, + db_mib=int(row["db_bytes"]) / 2**20, + loss=dump_s * RPS, + )) + +print() +for name, (a, b, r2) in (("dump", dump), ("restore", restore), ("dump+restore", total)): + print(f"{name}: t = {a:.3f} + {b * 1_000_000:.3f} * (messages / 1M) s, R^2={r2:.3f}") diff --git a/research/bench_indexes.sh b/research/bench_indexes.sh new file mode 100755 index 00000000..14989673 --- /dev/null +++ b/research/bench_indexes.sh @@ -0,0 +1,191 @@ +#!/usr/bin/env bash +# Track 2: compare the two supporting indexes on one skewed dataset. +# Exactly one of chat_members_user_idx / messages_chat_created_id_idx is +# present in the two comparison states; both are restored at the end. +set -euo pipefail +cd "$(dirname "$0")/.." + +set -a +. ./.env +set +a + +DB=messenger_research +SIZE=${SIZE:-300000} +CHAT_COUNT=${CHAT_COUNT:-5000} +USER_COUNT=${USER_COUNT:-2000} +RESULTS=research/results/indexes.md +COMPOSE=(docker compose --env-file .env) +ZERO=00000000-0000-0000-0000-000000000000 +mkdir -p research/results + +psqlq() { + "${COMPOSE[@]}" exec -T postgres psql -X -q -A -t \ + -U "$POSTGRES_USER" -d "$DB" -c "$1" +} + +psqlf() { + "${COMPOSE[@]}" exec -T postgres psql -X -q \ + -U "$POSTGRES_USER" -d "$DB" -v ON_ERROR_STOP=1 "$@" +} + +echo "Rebuilding $DB at $SIZE messages..." +"${COMPOSE[@]}" exec -T postgres psql -X -q -U "$POSTGRES_USER" -d postgres \ + -v ON_ERROR_STOP=1 \ + -c "DROP DATABASE IF EXISTS $DB WITH (FORCE);" \ + -c "CREATE DATABASE $DB;" >/dev/null +psqlf -f - < app/internal/database/migrations/0001_init.up.sql >/dev/null +psqlq "CREATE TABLE IF NOT EXISTS schema_migrations (version bigint NOT NULL PRIMARY KEY, dirty boolean NOT NULL); + INSERT INTO schema_migrations VALUES (1, false) ON CONFLICT DO NOTHING;" >/dev/null +psqlf -v target_messages="$SIZE" -v chat_count="$CHAT_COUNT" \ + -v user_count="$USER_COUNT" -f - < research/seed.sql >/dev/null + +# All three queries are built from the API's access patterns: +# user_chats - list the chats of one user +# chat_messages - keyset page of one chat +# user_feed - messages from all of one user's chats, keyset page +# The feed cursor is recent so this is a realistic page, not a full-history +# sort; the query still joins every message to the user's memberships. +Q_USER_CHATS="SELECT c.id, c.title, c.created_at +FROM chats c +JOIN chat_members cm ON cm.chat_id = c.id +WHERE cm.user_id = research_user_uuid(1) +ORDER BY c.created_at, c.id +LIMIT 50;" + +Q_CHAT_MESSAGES="SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = research_chat_uuid(1) + AND (created_at, id) > ('2026-02-15T00:00:00Z'::timestamptz, '$ZERO'::uuid) +ORDER BY created_at, id +LIMIT 51;" + +Q_USER_FEED="SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at +FROM messages m +JOIN chat_members cm ON cm.chat_id = m.chat_id +WHERE cm.user_id = research_user_uuid(1) + AND (m.created_at, m.id) > ('2026-03-01T00:00:00Z'::timestamptz, '$ZERO'::uuid) +ORDER BY m.created_at, m.id +LIMIT 50;" + +ensure_both() { + psqlq "CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id); + CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx ON messages (chat_id, created_at, id);" >/dev/null +} + +messages_only() { + psqlq "CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx ON messages (chat_id, created_at, id); + DROP INDEX IF EXISTS chat_members_user_idx;" >/dev/null +} + +membership_only() { + psqlq "CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id); + DROP INDEX IF EXISTS messages_chat_created_id_idx;" >/dev/null +} + +# AGENT: the comparison is only meaningful if exactly one index is present; +# a leftover second index would hide the regression this experiment measures. +assert_single() { + local want_membership=$1 want_messages=$2 + local count has_membership has_messages + count=$(psqlq "SELECT count(*) FROM pg_indexes + WHERE indexname IN ('chat_members_user_idx', 'messages_chat_created_id_idx');") + has_membership=$(psqlq "SELECT to_regclass('chat_members_user_idx') IS NOT NULL;") + has_messages=$(psqlq "SELECT to_regclass('messages_chat_created_id_idx') IS NOT NULL;") + if [ "$count" != "1" ] || [ "$has_membership" != "$want_membership" ] || [ "$has_messages" != "$want_messages" ]; then + echo "index state is not controlled: count=$count membership=$has_membership messages=$has_messages" >&2 + exit 1 + fi +} + +assert_both() { + local count + count=$(psqlq "SELECT count(*) FROM pg_indexes + WHERE indexname IN ('chat_members_user_idx', 'messages_chat_created_id_idx');") + [ "$count" = "2" ] || { echo "both indexes were not restored" >&2; exit 1; } +} + +bench_query() { + local label=$1 sql=$2 + psqlq "EXPLAIN (ANALYZE, BUFFERS) $sql" >/dev/null + local times=() + local i t + for i in 1 2 3 4 5; do + t=$(psqlq "EXPLAIN (ANALYZE, BUFFERS) $sql" | + sed -n 's/.*Execution Time: \([0-9.]*\) ms/\1/p') + times+=("$t") + done + local median plan + median=$(printf '%s\n' "${times[@]}" | sort -n | + awk '{a[NR]=$1} END {print a[int((NR + 1) / 2)]}') + plan=$(psqlq "EXPLAIN (ANALYZE, BUFFERS) $sql" | + grep -oE '(Index Only Scan|Index Scan|Seq Scan|Bitmap Heap Scan|Sort|Hash Join|Nested Loop|Merge Join)' | + awk '{if ($0 != prev) print; prev = $0}' | + paste -sd '>' -) + printf '| %s | %s | %s |\n' "$label" "$median" "$plan" +} + +bench_write() { + psqlq "DROP TABLE IF EXISTS research_write_bench; + CREATE TABLE research_write_bench ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL, + sender_id uuid NOT NULL, + text text NOT NULL, + created_at timestamptz NOT NULL + );" >/dev/null + if [ "$1" = "yes" ]; then + psqlq "CREATE INDEX research_write_bench_idx + ON research_write_bench (chat_id, created_at, id);" >/dev/null + fi + "${COMPOSE[@]}" cp research/write.sql postgres:/tmp/research_write.sql >/dev/null + local tps + tps=$("${COMPOSE[@]}" exec -T postgres pgbench -n -c 4 -j 2 -T 10 \ + -U "$POSTGRES_USER" -d "$DB" -f /tmp/research_write.sql 2>&1 | + sed -n 's/^tps = \([0-9.]*\).*/\1/p') + psqlq "DROP TABLE research_write_bench;" >/dev/null + echo "$tps" +} + +{ + echo "# Index experiment" + echo + echo "Dataset: $SIZE messages, $CHAT_COUNT chats, $USER_COUNT users." + echo "Median of 5 warm-cache runs; plan is the sequence of access nodes." + echo + echo "| state | query | median ms | plan |" + echo "| --- | --- | ---: | --- |" + + ensure_both + assert_both + bench_query "both | user_chats" "$Q_USER_CHATS" + bench_query "both | chat_messages" "$Q_CHAT_MESSAGES" + bench_query "both | user_feed" "$Q_USER_FEED" + write_with=$(bench_write yes) + + messages_only + assert_single f t + bench_query "messages_only | user_chats" "$Q_USER_CHATS" + bench_query "messages_only | chat_messages" "$Q_CHAT_MESSAGES" + bench_query "messages_only | user_feed" "$Q_USER_FEED" + + membership_only + assert_single t f + bench_query "membership_only | user_chats" "$Q_USER_CHATS" + bench_query "membership_only | chat_messages" "$Q_CHAT_MESSAGES" + bench_query "membership_only | user_feed" "$Q_USER_FEED" + write_without=$(bench_write no) + + ensure_both + assert_both + + echo + echo "Index sizes after restoring both:" + psqlq "SELECT 'chat_members_user_idx=' || pg_size_pretty(pg_relation_size('chat_members_user_idx')) || + ', messages_chat_created_id_idx=' || pg_size_pretty(pg_relation_size('messages_chat_created_id_idx')) || + ', messages_table=' || pg_size_pretty(pg_total_relation_size('messages'));" + echo + echo "pgbench message-like inserts, 4 clients, 10 s: with messages index ${write_with} tps; without ${write_without} tps." + echo "The research DB is left with both indexes." +} | tee "$RESULTS" + +echo "Wrote $RESULTS" diff --git a/research/bench_recovery.sh b/research/bench_recovery.sh new file mode 100755 index 00000000..a42f0be4 --- /dev/null +++ b/research/bench_recovery.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +# Track 2: dump/restore timing at 100k, 200k and 300k messages. +# The schema comes from the repository migration; both indexes are present +# for every dump. The research DB is left at 300k with both indexes. +set -euo pipefail +cd "$(dirname "$0")/.." + +set -a +. ./.env +set +a + +DB=messenger_research +RESTORE_DB=messenger_research_restore +DUMP=/tmp/research_recovery.dump +RESULTS=research/results/recovery.csv +COMPOSE=(docker compose --env-file .env) +mkdir -p research/results + +psql_db() { + local db=$1 sql=$2 + "${COMPOSE[@]}" exec -T postgres psql -X -q -A -t \ + -U "$POSTGRES_USER" -d "$db" -c "$sql" +} + +psql_admin() { + "${COMPOSE[@]}" exec -T postgres psql -X -q -A -t \ + -U "$POSTGRES_USER" -d postgres -v ON_ERROR_STOP=1 -c "$1" +} + +measure() { + local command=$1 + # AGENT: /usr/bin/time runs inside the container so the client-side + # docker exec overhead is not counted as backup or restore time. + if ! "${COMPOSE[@]}" exec -T postgres sh -c \ + "/usr/bin/time -f '%e' -o /tmp/research_time $command >/dev/null 2>&1"; then + echo "timed command failed: $command" >&2 + exit 1 + fi + "${COMPOSE[@]}" exec -T postgres cat /tmp/research_time +} + +ensure_both_indexes() { + psql_db "$DB" "CREATE INDEX IF NOT EXISTS chat_members_user_idx + ON chat_members (user_id, chat_id); + CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx + ON messages (chat_id, created_at, id);" >/dev/null +} + +printf 'messages,dump_s,restore_s,dump_bytes,db_bytes,table_bytes,index_bytes\n' > "$RESULTS" + +for n in 100000 200000 300000; do + echo "Rebuilding $DB at $n messages..." + psql_admin "DROP DATABASE IF EXISTS $RESTORE_DB WITH (FORCE);" >/dev/null + psql_admin "DROP DATABASE IF EXISTS $DB WITH (FORCE);" >/dev/null + psql_admin "CREATE DATABASE $DB;" >/dev/null + "${COMPOSE[@]}" exec -T postgres psql -X -q -U "$POSTGRES_USER" -d "$DB" \ + -v ON_ERROR_STOP=1 -f - < app/internal/database/migrations/0001_init.up.sql >/dev/null + psql_db "$DB" "CREATE TABLE IF NOT EXISTS schema_migrations + (version bigint NOT NULL PRIMARY KEY, dirty boolean NOT NULL); + INSERT INTO schema_migrations VALUES (1, false) + ON CONFLICT DO NOTHING;" >/dev/null + "${COMPOSE[@]}" exec -T postgres psql -X -q -U "$POSTGRES_USER" -d "$DB" \ + -v ON_ERROR_STOP=1 -v target_messages="$n" -v chat_count=5000 \ + -v user_count=2000 -f - < research/seed.sql >/dev/null + ensure_both_indexes + + db_bytes=$(psql_db "$DB" "SELECT pg_database_size(current_database());") + table_bytes=$(psql_db "$DB" "SELECT pg_total_relation_size('messages');") + index_bytes=$(psql_db "$DB" "SELECT pg_relation_size('chat_members_user_idx') + + pg_relation_size('messages_chat_created_id_idx');") + + dump_s=$(measure "pg_dump -U $POSTGRES_USER -d $DB -Fc -f $DUMP") + dump_bytes=$("${COMPOSE[@]}" exec -T postgres stat -c %s "$DUMP") + psql_admin "CREATE DATABASE $RESTORE_DB;" >/dev/null + restore_s=$(measure "pg_restore -U $POSTGRES_USER -d $RESTORE_DB --no-owner --no-privileges $DUMP") + + src_count=$(psql_db "$DB" "SELECT count(*) FROM messages;") + dst_count=$(psql_db "$RESTORE_DB" "SELECT count(*) FROM messages;") + src_members=$(psql_db "$DB" "SELECT count(*) FROM chat_members;") + dst_members=$(psql_db "$RESTORE_DB" "SELECT count(*) FROM chat_members;") + # AGENT: one aggregate fingerprint covers every message row, so the check + # is not limited to whichever row happened to be newest. + src_fingerprint=$(psql_db "$DB" "SELECT md5(string_agg( + id::text || '|' || chat_id::text || '|' || sender_id::text || '|' || + text || '|' || created_at::text, E'\n' ORDER BY id)) FROM messages;") + dst_fingerprint=$(psql_db "$RESTORE_DB" "SELECT md5(string_agg( + id::text || '|' || chat_id::text || '|' || sender_id::text || '|' || + text || '|' || created_at::text, E'\n' ORDER BY id)) FROM messages;") + dst_indexes=$(psql_db "$RESTORE_DB" "SELECT count(*) FROM pg_indexes + WHERE indexname IN ('chat_members_user_idx', + 'messages_chat_created_id_idx');") + + if [ "$src_count" != "$dst_count" ] || [ "$src_members" != "$dst_members" ] || + [ "$src_fingerprint" != "$dst_fingerprint" ] || [ "$dst_indexes" != "2" ]; then + echo "restore verification failed at $n rows" >&2 + exit 1 + fi + + printf '%s,%s,%s,%s,%s,%s,%s\n' \ + "$n" "$dump_s" "$restore_s" "$dump_bytes" "$db_bytes" "$table_bytes" "$index_bytes" \ + >> "$RESULTS" + echo " dump ${dump_s}s, restore ${restore_s}s, ${dump_bytes} bytes; verified" + psql_admin "DROP DATABASE $RESTORE_DB WITH (FORCE);" >/dev/null + "${COMPOSE[@]}" exec -T postgres rm -f "$DUMP" +done + +ensure_both_indexes +echo +echo "Recovery results (CSV: messages,dump_s,restore_s,dump_bytes,db_bytes,table_bytes,index_bytes):" +cat "$RESULTS" +echo +echo "Final research DB: $(psql_db "$DB" "SELECT count(*) FROM messages;") messages, both indexes present." diff --git a/research/results/indexes.md b/research/results/indexes.md new file mode 100644 index 00000000..6cc7d9d3 --- /dev/null +++ b/research/results/indexes.md @@ -0,0 +1,22 @@ +# Index experiment + +Dataset: 300000 messages, 5000 chats, 2000 users. +Median of 5 warm-cache runs; plan is the sequence of access nodes. + +| state | query | median ms | plan | +| --- | --- | ---: | --- | +| both | user_chats | 1.379 | Sort>Hash Join>Seq Scan>Index Only Scan | +| both | chat_messages | 0.428 | Index Scan | +| both | user_feed | 35.396 | Sort>Nested Loop>Index Only Scan>Index Scan | +| messages_only | user_chats | 7.360 | Sort>Hash Join>Seq Scan | +| messages_only | chat_messages | 0.469 | Index Scan | +| messages_only | user_feed | 35.218 | Sort>Nested Loop>Seq Scan>Index Scan | +| membership_only | user_chats | 1.402 | Sort>Hash Join>Seq Scan>Index Only Scan | +| membership_only | chat_messages | 57.634 | Sort>Seq Scan | +| membership_only | user_feed | 80.404 | Sort>Hash Join>Seq Scan>Index Only Scan | + +Index sizes after restoring both: +chat_members_user_idx=3536 kB, messages_chat_created_id_idx=17 MB, messages_table=75 MB + +pgbench message-like inserts, 4 clients, 10 s: with messages index 2718.889742 tps; without 2640.028063 tps. +The research DB is left with both indexes. diff --git a/research/results/recovery.csv b/research/results/recovery.csv new file mode 100644 index 00000000..1a57585a --- /dev/null +++ b/research/results/recovery.csv @@ -0,0 +1,4 @@ +messages,dump_s,restore_s,dump_bytes,db_bytes,table_bytes,index_bytes +100000,1.02,1.44,5109980,47411683,26402816,9535488 +200000,1.92,2.36,9803565,74240483,53231616,15417344 +300000,2.66,3.28,14500027,99627491,78618624,21307392 diff --git a/research/results/recovery.md b/research/results/recovery.md new file mode 100644 index 00000000..980e2fd0 --- /dev/null +++ b/research/results/recovery.md @@ -0,0 +1,9 @@ +| messages | dump s | restore s | dump MiB | DB MiB | lost msgs at 878 RPS | +| ---: | ---: | ---: | ---: | ---: | ---: | +| 100000 | 1.02 | 1.44 | 4.9 | 45.2 | 896 | +| 200000 | 1.92 | 2.36 | 9.3 | 70.8 | 1686 | +| 300000 | 2.66 | 3.28 | 13.8 | 95.0 | 2335 | + +dump: t = 0.227 + 8.200 * (messages / 1M) s, R^2=0.997 +restore: t = 0.520 + 9.200 * (messages / 1M) s, R^2=1.000 +dump+restore: t = 0.747 + 17.400 * (messages / 1M) s, R^2=0.999 diff --git a/research/seed.sql b/research/seed.sql new file mode 100644 index 00000000..acc6038a --- /dev/null +++ b/research/seed.sql @@ -0,0 +1,114 @@ +-- Synthetic, repeatable dataset for the Track 2 experiments. +-- Usage: +-- psql -v target_messages=300000 -v chat_count=5000 -v user_count=2000 -f seed.sql +-- The distribution is deliberately skewed: a few chats hold most messages, +-- which is closer to a real messenger than one evenly filled chat. +\set ON_ERROR_STOP on +\if :{?target_messages} +\else + \set target_messages 300000 +\endif +\if :{?chat_count} +\else + \set chat_count 5000 +\endif +\if :{?user_count} +\else + \set user_count 2000 +\endif + +-- Deterministic, readable UUIDs. The version/variant nibbles are valid. +CREATE OR REPLACE FUNCTION research_user_uuid(i integer) +RETURNS uuid LANGUAGE sql IMMUTABLE AS $$ + SELECT ('00000000-0000-4000-8000-' || lpad(to_hex(i), 12, '0'))::uuid +$$; + +CREATE OR REPLACE FUNCTION research_chat_uuid(i integer) +RETURNS uuid LANGUAGE sql IMMUTABLE AS $$ + SELECT ('10000000-0000-4000-8000-' || lpad(to_hex(i), 12, '0'))::uuid +$$; + +DROP TABLE IF EXISTS research_chat_sender; +TRUNCATE messages, chat_members, chats, users CASCADE; + +-- Reproducible pseudo-random layout for a given PostgreSQL build. +SELECT setseed(0.4242); + +INSERT INTO users (id, username, display_name, password_hash, created_at) +SELECT research_user_uuid(g), + 'u' || lpad(g::text, 4, '0'), + 'User ' || g, + 'not-a-login-hash', + '2026-01-01T00:00:00Z'::timestamptz +FROM generate_series(1, :user_count) AS g; + +INSERT INTO chats (id, title, created_at) +SELECT research_chat_uuid(g), + 'Research chat ' || g, + '2026-01-01T00:00:00Z'::timestamptz + (g || ' seconds')::interval +FROM generate_series(1, :chat_count) AS g; + +-- Each chat gets a small random membership set (5..24 distinct users). +INSERT INTO chat_members (chat_id, user_id) +SELECT research_chat_uuid(c), + research_user_uuid(1 + floor(random() * :user_count)::int) +FROM generate_series(1, :chat_count) AS c +CROSS JOIN generate_series(1, 5 + (c % 20)) AS m +ON CONFLICT DO NOTHING; + +-- Probe user 1 is a member of the ten largest chats and every 100th chat. +-- user_chats and user_feed queries therefore always have rows to return. +INSERT INTO chat_members (chat_id, user_id) +SELECT research_chat_uuid(c), research_user_uuid(1) +FROM generate_series(1, :chat_count) AS c +WHERE c <= 10 OR c % 100 = 0 +ON CONFLICT DO NOTHING; + +-- One valid sender per chat, so every synthetic message has a member sender. +CREATE TABLE research_chat_sender AS +SELECT chat_id, (array_agg(user_id ORDER BY random()))[1] AS sender_id +FROM chat_members +GROUP BY chat_id; +ALTER TABLE research_chat_sender ADD PRIMARY KEY (chat_id); + +-- AGENT: a uniform chat size would make every query look cheap, so sizes are +-- skewed. The probe user is deliberately in the largest chats to exercise the +-- worst realistic case for the cross-chat feed. +-- floor(random()^3 * chat_count) makes chat 1 hold roughly 6% of all +-- messages at 300k, while most chats stay small. Timestamps span 90 days. +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + s.chat_id, + cs.sender_id, + 'research message ' || s.g || ' ' || repeat('x', 20 + (s.g % 60)), + '2026-01-01T00:00:00Z'::timestamptz + interval '90 days' * s.r +FROM ( + SELECT g, + research_chat_uuid(floor(power(random(), 3) * :chat_count)::int + 1) AS chat_id, + random() AS r + FROM generate_series(1, :target_messages) AS g +) AS s +JOIN research_chat_sender cs USING (chat_id); + +-- Rebuild the two indexes so their reported sizes are not inflated by the +-- random insertion order. This keeps the index comparison and the dump size +-- on the same, compact baseline. +REINDEX INDEX chat_members_user_idx; +REINDEX INDEX messages_chat_created_id_idx; +VACUUM (ANALYZE) messages, chat_members, chats, users, research_chat_sender; + +SELECT 'messages' AS item, count(*)::text AS value FROM messages +UNION ALL SELECT 'chat_members', count(*)::text FROM chat_members +UNION ALL SELECT 'chats', count(*)::text FROM chats +UNION ALL SELECT 'users', count(*)::text FROM users +UNION ALL SELECT 'messages_table_bytes', pg_total_relation_size('messages')::text +UNION ALL SELECT 'chat_members_user_idx_bytes', pg_relation_size('chat_members_user_idx')::text +UNION ALL SELECT 'messages_chat_created_id_idx_bytes', pg_relation_size('messages_chat_created_id_idx')::text; + +-- Shape check: largest chats should dominate the message count. +SELECT c.id, c.title, count(m.id) AS messages +FROM chats c +LEFT JOIN messages m ON m.chat_id = c.id +GROUP BY c.id, c.title +ORDER BY messages DESC +LIMIT 5; diff --git a/research/write.sql b/research/write.sql new file mode 100644 index 00000000..526081d9 --- /dev/null +++ b/research/write.sql @@ -0,0 +1,9 @@ +\set g random(1, 1000000) +INSERT INTO research_write_bench (id, chat_id, sender_id, text, created_at) +VALUES ( + gen_random_uuid(), + research_chat_uuid(1 + (:g % 5000)), + research_user_uuid(1 + (:g % 2000)), + 'bench ' || :g, + '2026-01-01T00:00:00Z'::timestamptz + (:g || ' seconds')::interval +); diff --git a/scripts/grade4_checks.py b/scripts/grade4_checks.py new file mode 100755 index 00000000..ecb00821 --- /dev/null +++ b/scripts/grade4_checks.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python3 +"""Grade 4 checks that the public smoke does not cover. + +Covers: concurrent duplicate registration, atomic chat rollback, short TTL +expiry, logout invalidation, wrong password, and Redis/PostgreSQL outage +behaviour. Failure drills use `docker compose stop/start` and never remove +volumes. Tokens, passwords and hook output are not printed. +""" +import argparse +import concurrent.futures +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) + +from client import Client, Failure, check # noqa: E402 + + +def say(message): + print("PASS: " + message, flush=True) + + +def compose(args, *command): + env_file = ROOT / ".env" + check(env_file.is_file(), ".env is missing; copy .env.example to .env") + full = [ + "docker", "compose", + "--env-file", str(env_file), + "-p", args.compose_project, + "-f", str((ROOT / args.compose_file).resolve()), + *command, + ] + result = subprocess.run(full, cwd=ROOT, stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, text=True, timeout=60) + if result.returncode != 0: + # stderr may contain paths, never credentials in normal compose output. + raise Failure("docker compose " + command[0] + " failed: " + result.stderr.strip()[:300]) + + +def readiness(client): + try: + client.call("GET", "/health/ready", 200, schema="Health") + return 200 + except Failure: + try: + client.call("GET", "/health/ready", 503) + return 503 + except Failure: + return None + + +def wait_for(client, expected, seconds=45): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + if readiness(client) == expected: + return + time.sleep(0.5) + raise Failure("/health/ready did not become " + str(expected)) + + +def check_wrong_password(args): + client = Client(args.base_url) + actor = client.user("wrongpw") + registration = actor["registration"] + credential = _basic(registration["username"], "definitely-wrong-password") + response = client.call("POST", "/api/v1/auth/sessions", 401, + headers={"Authorization": "Basic " + credential}) + check(response["headers"].get("WWW-Authenticate", "").lower().startswith("basic"), + "wrong password did not return a Basic challenge") + say("wrong password -> 401 with Basic challenge") + + +def check_logout(args): + client = Client(args.base_url) + actor = client.user("logout") + client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor) + client.call("GET", "/api/v1/users/me", 401, actor=actor) + say("logout revoked the current token (repeat -> 401)") + + +def check_x_user_id(args): + client = Client(args.base_url) + alice = client.user("xalice") + bob = client.user("xbob") + me = client.call("GET", "/api/v1/users/me", actor=alice, + headers={"X-User-Id": bob["id"]}, schema="User")["json"] + check(me["id"] == alice["id"], "X-User-Id overrode bearer identity") + client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]}) + say("X-User-Id is ignored and grants no access") + + +def check_concurrent_registration(args): + username = "race_" + uuid.uuid4().hex[:12] + body = {"username": username, "display_name": "Race", "password": "concurrent-password-123"} + + def register(_): + client = Client(args.base_url) + response = client.call("POST", "/api/v1/users", (201, 409), body=body) + return response["status"] + + with concurrent.futures.ThreadPoolExecutor(max_workers=12) as pool: + statuses = list(pool.map(register, range(12))) + + check(statuses.count(201) == 1, "expected exactly one 201, got " + str(statuses.count(201))) + check(statuses.count(409) == 11, "expected eleven 409, got " + str(statuses.count(409))) + say("12 concurrent registrations of one username -> one 201, eleven 409") + + +def check_chat_rollback(args): + client = Client(args.base_url) + actor = client.user("rollback") + title = "rollback-" + uuid.uuid4().hex + unknown = str(uuid.uuid4()) + client.call("POST", "/api/v1/chats", 400, actor=actor, + body={"title": title, "member_ids": [actor["id"], unknown]}) + page = client.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"] + check(all(chat["title"] != title for chat in page["items"]), + "failed chat creation left a partial chat behind") + say("failed composite chat creation rolled back completely") + + +def check_short_ttl(args): + check(args.short_ttl_url, "--short-ttl-url is required for the expiry check") + short = Client(args.short_ttl_url) + main = Client(args.base_url) + actor = short.user("ttl") + main.call("GET", "/api/v1/users/me", actor=actor, schema="User") + time.sleep(3.5) + main.call("GET", "/api/v1/users/me", 401, actor=actor) + say("SESSION_TTL_SECONDS=2 session expired and was rejected by the main API") + + +def drill(args, client, actor, chat, message, service, expected_message): + started = time.monotonic() + compose(args, "stop", service) + wait_for(client, 503, 45) + detection = time.monotonic() - started + client.call("GET", "/health/live", 200, schema="Health") + client.call("GET", "/health/ready", 503) + client.call("GET", "/api/v1/users/me", 503, actor=actor) + say("%s down: live=200, ready=503, protected=503 (%s; detected in %.1fs)" + % (service, expected_message, detection)) + + started = time.monotonic() + compose(args, "start", service) + wait_for(client, 200, 60) + recovery = time.monotonic() - started + client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + page = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", + actor=actor, schema="MessagePage")["json"] + check(any(item == message for item in page["items"]), + service + " recovery lost a confirmed message") + say("%s recovered in %.1fs; same token and confirmed message intact" % (service, recovery)) + + +def check_failure_drills(args): + client = Client(args.base_url) + actor = client.user("drill") + chat = client.call("POST", "/api/v1/chats", 201, actor=actor, + body={"title": "Failure drill", "member_ids": [actor["id"]]}, + schema="Chat")["json"] + message = client.message(actor, chat, "drill-persist-" + uuid.uuid4().hex)["json"] + drill(args, client, actor, chat, message, "redis", "bounded unavailable error, no anonymous access") + drill(args, client, actor, chat, message, "postgres", "bounded unavailable error, session retained") + + +def _basic(username, password): + import base64 + return base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii") + + +def main(): + parser = argparse.ArgumentParser(description="Lab 2 grade 4 evidence checks") + parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080")) + parser.add_argument("--short-ttl-url", default=os.environ.get("SHORT_TTL_URL", "http://localhost:8081")) + parser.add_argument("--compose-project", default=os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab2")) + parser.add_argument("--compose-file", default="compose.yaml") + parser.add_argument("--skip-failure", action="store_true", help="skip Redis/PostgreSQL stop drills") + args = parser.parse_args() + + check_wrong_password(args) + check_logout(args) + check_x_user_id(args) + check_concurrent_registration(args) + check_chat_rollback(args) + check_short_ttl(args) + if not args.skip_failure: + check_failure_drills(args) + + print("PASS: grade 4 checks completed") + + +if __name__ == "__main__": + try: + main() + except Failure as error: + print("FAIL:", str(error)) + raise SystemExit(1) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..ea9f7838 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# AGENT: Restart hook for tests/resilience.py. It intentionally uses `restart`, +# never `down -v`, `flushall` or a re-seed: PostgreSQL data and the Redis AOF +# stay on their named volumes. Exit 0 means "the restart was requested", the +# test then waits for /health/ready and reuses the existing bearer token. +set -eu + +cd "$(dirname "$0")/.." + +COMPOSE_FILE_PATH="${COMPOSE_FILE:-compose.yaml}" + +if [ ! -f .env ]; then + echo "restart.sh: .env is missing; copy .env.example to .env first" >&2 + exit 2 +fi + +compose() { + docker compose --env-file .env -f "$COMPOSE_FILE_PATH" "$@" +} + +# State stores first, then the API, so the app reconnects to a live database. +# The API itself retries PostgreSQL for up to 30 seconds during startup. +compose restart postgres redis +compose restart api diff --git a/scripts/seed_10k.sql b/scripts/seed_10k.sql new file mode 100644 index 00000000..a4e3e7cf --- /dev/null +++ b/scripts/seed_10k.sql @@ -0,0 +1,53 @@ +-- Synthetic history for the index evidence. Never run this against real data: +-- it deletes only messages of the dedicated seed chat and re-creates them. +\set ON_ERROR_STOP on + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES ('aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', 'seed_10k', 'Seed 10k', + 'not-a-login-hash', '2026-01-01T00:00:00Z') +ON CONFLICT (id) DO NOTHING; + +INSERT INTO chats (id, title, created_at) +VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'Seed 10k history', '2026-01-01T00:00:00Z') +ON CONFLICT (id) DO NOTHING; + +INSERT INTO chat_members (chat_id, user_id) +VALUES ('bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa') +ON CONFLICT DO NOTHING; + +DELETE FROM messages WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'; + +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT gen_random_uuid(), + 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb', + 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa', + 'seed message ' || g, + '2026-01-01T00:00:00Z'::timestamptz + (g || ' seconds')::interval +FROM generate_series(1, 10000) AS g; + +-- Remove index entries left by the previous seed run; without this the plan +-- reads dead entries and the buffer count reflects history, not page depth. +VACUUM (ANALYZE) messages; + +SELECT count(*) AS seeded_messages +FROM messages +WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'; + +-- First page: the same predicate the API sends with an empty cursor. +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb' + AND (created_at, id) > ('0001-01-01T00:00:00Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY created_at, id +LIMIT 51; + +-- Deep page: the cursor points at message 5000, so a correct plan seeks there +-- instead of scanning the first 5000 rows. +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb' + AND (created_at, id) > ('2026-01-01T01:23:20Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY created_at, id +LIMIT 51; diff --git a/scripts/seed_and_explain.sh b/scripts/seed_and_explain.sh new file mode 100755 index 00000000..946d0222 --- /dev/null +++ b/scripts/seed_and_explain.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# AGENT: Seeds exactly 10,000 synthetic messages into a dedicated chat and runs +# EXPLAIN (ANALYZE, BUFFERS) using the API's keyset predicate. Output goes to +# stdout so it can be stored in evidence/explain.txt. +set -eu + +cd "$(dirname "$0")/.." + +if [ ! -f .env ]; then + echo "seed_and_explain.sh: .env is missing" >&2 + exit 2 +fi + +set -a +# shellcheck disable=SC1091 +. ./.env +set +a + +docker compose --env-file .env exec -T postgres \ + psql -v ON_ERROR_STOP=1 -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" \ + < scripts/seed_10k.sql