diff --git a/.env.example b/.env.example index 714c807..6313637 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,7 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# Generate .env and secret files with python scripts/bootstrap.py. +# The placeholder is not a usable password. Never commit local .env. +POSTGRES_PASSWORD=GENERATED_BY_BOOTSTRAP +HOST_PORT=8080 APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab @@ -9,3 +11,8 @@ 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_MAX_CONNECTIONS=8 +DB_TIMEOUT_MS=2000 +KDF_CONCURRENCY=4 +MAX_IN_FLIGHT=64 +MAX_BODY_BYTES=1048576 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8ceefa1..d796dd2 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,22 @@ ## Лабораторная 2 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Брезгунова Анжелика / БВТ2303 +Целевая оценка (3/4/5): 5 +Трек 5: 2 — план запроса и восстановление -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Перенесён собственный Go API первой лабы. Пользователи, чаты, членство, сообщения и сессии сохраняются в PostgreSQL; пароль хешируется Argon2id, Basic выдаёт opaque Bearer-сессию с expiry/logout. Redis кэширует неизменяемые профили и допускает fallback в БД. Добавлены миграции, Compose, restart hooks и собственные проверки. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +Запуск: `python scripts/bootstrap.py`, `docker compose --env-file .env up -d --build`, `python scripts/wait_ready.py --base-url http://127.0.0.1:8080`. Полная инструкция — [REPORT.md](../REPORT.md). + +PASS: Go tests/vet, offline check, публичные smoke/persistence, гонка регистрации, rollback, Unicode KDF, Redis hit/loss/outage, PostgreSQL outage, migration replay, expiry/logout. Исследование 100 000 сообщений: сравнение двух индексов и вставки; backup восстановлен в другой контейнер/volume, все сообщения сверены. Raw результаты — `evidence/`. + +Ограничения: одна API-реплика/БД; локальный HTTP; warm-cache измерения; INSERT измерен без COMMIT, с rollback. Backup без WAL/PITR теряет изменения после dump snapshot. Redis доверен внутри закрытой Compose-сети. Подробности и результаты — в отчёте. + +Источники: собственная lab1, контракт lab2, lib/pq, go-redis, x/crypto/argon2. Использована помощь Codex в реализации и проверках. + +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] Прямые эквиваленты make check/test выполнены; приложен реальный вывод (локального make на Windows нет). +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Secrets исключены из Git/build context; доказательства не содержат tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 0000000..030bd43 --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,26 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read +jobs: + integration: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python scripts/bootstrap.py + - run: docker compose --env-file .env up -d --build + - run: python scripts/wait_ready.py + - run: make check test PYTHON=python + - run: chmod +x scripts/restart.sh && make test-persistence PYTHON=python ACTION_SCRIPT=scripts/restart.sh + - run: make test-extra research PYTHON=python + - uses: actions/upload-artifact@v4 + if: always() + with: + name: sanitized-evidence + path: evidence/*.json + - if: always() + run: docker compose --env-file .env --profile research down diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4bfc4eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.24.7-alpine3.22 AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN go test ./... && go vet ./... +RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/messenger . + +FROM alpine:3.22.1 +RUN addgroup -S app && adduser -S -G app app +COPY --from=build /out/messenger /usr/local/bin/messenger +USER app +EXPOSE 8080 +ENTRYPOINT ["messenger"] diff --git a/Makefile b/Makefile index 9ecb223..31a6313 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,26 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence bootstrap up down test-extra research unit + +bootstrap: + $(PYTHON) scripts/bootstrap.py + +up: bootstrap + docker compose --env-file .env up -d --build + $(PYTHON) scripts/wait_ready.py --base-url "$(BASE_URL)" + +down: + docker compose --env-file .env down + +unit: + go test ./... + +test-extra: + $(PYTHON) student-tests/integration.py + +research: + $(PYTHON) student-tests/research.py help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" diff --git a/REPORT.md b/REPORT.md index e42f2b0..612f2d2 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,187 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Брезгунова Анжелика, БВТ2303. +- Целевая оценка: 5. Трек 2 — план запроса и восстановление. +- Рабочая ветка: `solution/BVT2303-Брезгунова-на5`. Commit/PR появятся при публикации работы. +- Реализация: Go; PostgreSQL как источник истины; Redis как кэш неизменяемых профилей. +- Использована помощь Codex для реализации, тестов и оформления. Основа HTTP API, модели и ограничения запросов перенесены из собственной первой лабы. Контракт, задание и публичные тесты второй лабы сохранены. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Engine с Compose v2+ и Python 3.9+. Локальный Go и make необязательны: Dockerfile выполняет `go test ./...`, `go vet ./...` и сборку. Образы закреплены: Go `1.24.7-alpine3.22`, Alpine `3.22.1`, PostgreSQL `17.6-alpine3.22`, Redis `7.4.5-alpine3.21`. Приложение имеет локальный тег `mtusi-messenger:lab2`. + +Из каталога репозитория, в PowerShell или POSIX shell: + +```sh +python scripts/bootstrap.py +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +python scripts/wait_ready.py --base-url http://127.0.0.1:8080 +python scripts/check_contract.py +python tests/smoke.py --base-url http://127.0.0.1:8080 +``` + +Bootstrap генерирует случайный локальный пароль, `.env` и `secrets/database_url`, `secrets/cache_url`. При повторе пароль не меняется. Эти файлы игнорируются Git и исключены из build context. На Linux `.env` имеет права 0600, каталог secrets — 0700; отдельные URL-файлы имеют 0644 для чтения непривилегированным API после bind mount. Не копировать сюда secrets или volume первой лабы. Изменение пароля в `.env` не меняет пароль уже инициализированной БД. + +API доступен на `127.0.0.1:8080`; порт хоста меняется через `HOST_PORT` в `.env`. PostgreSQL и Redis не публикуют порты. В Windows выбран IPv4-адрес, чтобы исключить наблюдавшуюся задержку разрешения `localhost`. Если меняете порт, передайте соответствующий `--base-url`, а для собственного integration-теста задайте `BASE_URL`. + +При наличии make те же проверки: + +```sh +make up PYTHON=python +make check test PYTHON=python BASE_URL=http://127.0.0.1:8080 +chmod +x scripts/restart.sh +make test-persistence PYTHON=python ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8080 +make test-extra research PYTHON=python +``` + +Для Windows hook запускается непосредственно публичным тестом: + +```powershell +python tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080 +``` + +Полный последовательный прогон с сохранением доказательств: + +```sh +python student-tests/verify.py +python student-tests/profile_load.py +``` + +`verify.py` создаёт синтетические данные, перезапускает только сервисы этой лабы, временно останавливает Redis/PostgreSQL и восстанавливает их в `finally`. Для expiry временно пересоздаёт API с TTL=2, затем возвращает значение из `.env`. Исследование работает в собственных уникальных БД и удаляет только созданные этим запуском БД; учебные данные приложения не меняет. Backup остаётся в игнорируемом каталоге `backups/`. Исследовательский сервис `restore` после эксперимента остановлен; его отдельный volume сохранён. + +Безопасная остановка с сохранением всех данных: + +```sh +docker compose --env-file .env --profile research down +``` + +Намеренное удаление **всех учебных данных этого Compose-проекта**, только когда они больше не нужны: + +```sh +docker compose --env-file .env --profile research down --volumes +``` + +Для полной проверки OpenAPI установите `requirements-dev.txt` в venv и выполните `python scripts/validate_openapi.py`. CI skeleton выполняет эту проверку отдельно. Собственный workflow `application.yml` собирает приложение и запускает smoke, persistence, дополнительные тесты и исследование; он не использует секреты аккаунта. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +```text +Клиент -> API :8080 -> PostgreSQL :5432 + \-> Redis :6379 (необязательный кэш профилей) +Исследование -> pg_dump -> отдельный PostgreSQL restore -> отдельный volume +``` + +Приложение работает от непривилегированного пользователя, с read-only файловой системой и без Linux capabilities. Состояние API не хранится локально. Основные volumes: `postgres_data`, `redis_data`; для эксперимента — `restore_data`. Redis не содержит обязательных данных и не требует persistence. Его volume не используется как доказательство сохранности сессий. + +### Схема, ограничения и транзакционные границы + +Схема задаётся миграцией `migrations/001_initial.sql`: + +| Таблица | Содержимое и ограничения | Индексы | +| --- | --- | --- | +| users | UUID PK, уникальный username, display_name, password_hash, время БД; CHECK формата username и длины display_name | PK, UNIQUE username | +| chats | UUID PK, title с ограничением длины, время БД | PK, `(created_at,id)` | +| chat_members | FK на chat/user, PK `(chat_id,user_id)`, уникальный порядок участника в чате | PK, UNIQUE `(chat_id,position)`, `(user_id,chat_id)` | +| messages | UUID PK, FK на чат; составной FK `(chat_id,sender_id)` на членство; text 1–4000 code points, время БД | PK, `(chat_id,created_at,id)` | +| sessions | SHA-256 случайного токена как bytea PK длиной 32; FK user; абсолютный expires_at больше created_at | PK, expires_at для очистки | +| schema_migrations | Имя миграции, контрольная сумма SHA-256, время применения | PK имени | + +Создание чата и всех его участников — одна SQL-транзакция. Неизвестный второй участник вызывает FK-ошибку уже после вставки чата и первого участника; rollback удаляет все изменения. Регистрация — один INSERT; UNIQUE в PostgreSQL разрешает гонку одинаковых username. Запись сообщения и создание/удаление сессии — отдельные атомарные SQL-операции. Ответ 201/204 отправляется после успешного выполнения/COMMIT. Используются штатные настройки PostgreSQL `fsync`, `synchronous_commit`, `full_page_writes`; рестарт контейнера проверен, отказ диска/хоста этим не моделируется. + +Миграции встроены в бинарник. При старте advisory lock сериализует запуск, а миграции и запись их контрольных сумм коммитятся вместе. Повторный старт пропускает применённые миграции; изменение уже применённого файла приводит к ошибке старта. Новые изменения схемы нужно оформлять новым нумерованным файлом. Миграции не выполняют seed приложения. + +Все пользовательские значения передаются параметрами `$1…`; SQL не собирается из полей HTTP. Пул ограничен 8 соединениями, контекст HTTP/БД — 2 секунды, подключение — 2 секунды, SQL statement timeout — 2 секунды, lock timeout — 1 секунда. HTTP имеет ограничения размера тела (1 MiB), заголовков и таймауты чтения/записи. Admission gate — 64 бизнес-запроса; одновременно работают максимум 4 password KDF. При заполнении gate возвращается 429 с Retry-After. Health остаётся доступен при заполнении gate. + +### Аутентификация и доступ + +Регистрация принимает пароль из 12–128 Unicode code points. Argon2id v19: память 32 MiB, 3 итерации, 2 lanes, случайная соль 16 bytes, результат 32 bytes. Сравнение ключей выполняется в постоянное время. Пароли не усекутся на 72 bytes; проверен пароль длиной 128 Unicode-символов с двоеточием. Пустые и состоящие только из пробелов значения отклоняются. + +`POST /api/v1/auth/sessions` принимает Basic без тела. Неверный пароль и неизвестный пользователь возвращают одинаковый 401 и Basic challenge; для неизвестного пользователя также выполняется dummy KDF. Сессия получает 32 случайных bytes (256 бит), закодированных base64url. Это opaque токен; в PostgreSQL сохраняется только его SHA-256. SHA-256 здесь служит индексом случайного высокоэнтропийного токена, а не password KDF. + +Защищённые запросы принимают Bearer; `X-User-Id` игнорируется, Basic на бизнес-эндпоинтах не принимается. Авторизация всегда проверяет сессию в PostgreSQL с `expires_at > clock_timestamp()`. TTL задаёт `SESSION_TTL_SECONDS`, по умолчанию 3600; абсолютное время не изменяется из-за рестарта. Logout атомарно удаляет действительную сессию. Повторный logout и запрос после expiry — 401 с Bearer challenge. Фоновая очистка удаляет истёкшие сессии раз в минуту; действительность не зависит от запуска этой очистки. + +Далее проверяется членство пользователя в чате: постороннему возвращается 404. Sender определяется по сессии, клиентское поле sender_id отклоняется. User никогда не содержит password/hash/token. Регистрация, вход и health публичны; `/metrics` — локальное диагностическое расширение. + +### Redis, expiry и инвалидация + +Redis действительно кэширует ответы профиля `GET /users/me` по ключу `profile:v1:`, TTL 300 секунд. Источник истины — users в PostgreSQL. API текущей лабы не меняет и не удаляет профиль, поэтому инвалидация при записи не нужна: кэш заполняется только из успешно сохранённого User. При добавлении редактирования понадобится явная инвалидация/версионирование. История чата не кэшируется: следующее чтение после подтверждённой записи сразу идёт в PostgreSQL. + +Сессия проверяется в PostgreSQL **до** обращения к Redis; кэш не может обойти logout, expiry или отказ БД. При miss/ошибке/потере Redis профиль читается из PostgreSQL. GET/SET кэша ограничены 150 ms каждый, retry выключен; cache hit/miss/failure видны в `/metrics`. Redis ограничен 64 MiB, политика allkeys-lru. Недоступность Redis не блокирует readiness. Недоступность PostgreSQL приводит к 503 readiness и защищённых запросов; liveness остаётся 200. Приложение не принимает анонимного пользователя как доверенного. + +### Пагинация и порядок + +История читает максимум `limit+1` записей по предикату `chat_id=$1 AND (created_at,id)>($2,$3)`, сортировке `(created_at,id)` и индексу `(chat_id,created_at,id)`. OFFSET и чтение всей истории в память отсутствуют. Дополнительная запись определяет next_cursor; последний элемент страницы не повторяется. Cursor содержит ресурс, время и UUID; cursor чужого чата или списка другого пользователя отклоняется. Допускается смена limit, диапазон 1–100. + +Время назначается PostgreSQL с точностью microseconds; UUID разрешает равенство времён. Между HTTP-страницами нет snapshot: конкурентная вставка с ключом раньше уже пройденного cursor может не попасть в этот обход. Обратный ход системных часов также может создать такую запись. Для неизменяемой истории обход полный и без повторов. Следующее чтение с начала видит все подтверждённые записи. + +### Конфигурация + +`APP_HOST`, `APP_PORT`, `SESSION_TTL_SECONDS`, `SESSION_STORE`, `DATABASE_URL_FILE`, `CACHE_URL_FILE`, `DB_MAX_CONNECTIONS`, `DB_TIMEOUT_MS`, `KDF_CONCURRENCY`, `MAX_IN_FLIGHT`, `MAX_BODY_BYTES` читаются приложением. `_FILE` имеет приоритет над прямым URL; SESSION_STORE допускает только postgres. `POSTGRES_PASSWORD` и `HOST_PORT` используются Compose, `APP_ENV=lab` в примере — информационная метка. HTTP допустим здесь для локального стенда с синтетическими данными; Basic кодирует, но не шифрует пароль. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Фактические результаты сохраняются в `evidence/verification.json`, `integration.json`, `research.json`, `load.json`, `resources.json`, `build.txt`. Команды внутри JSON позволяют повторить проверку. Публичные Python-команды — прямые эквиваленты соответствующих targets Makefile; локальный make на проверяемой Windows-машине отсутствует. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий | Команда/сценарий | Доказательство | +| --- | --- | --- | +| Контракт и сборка | `python scripts/check_contract.py`; Docker build с go test и go vet | verification.json; build.txt | +| Обязательный API, неверный пароль, Basic/Bearer, доступ, logout | `python tests/smoke.py --base-url http://127.0.0.1:8080` | verification.json | +| Данные и действующие сессии после рестарта всех сервисов | публичный resilience с restart.cmd/restart.sh | verification.json: PASS, StartedAt всех трёх контейнеров до/после | +| Миграции с нуля и повторный запуск | первая инициализация volume; restart API и сравнение migration/user/message counts | build.txt; integration.json | +| Username race, атомарность чата, параметризованный SQL | 4 конкурентные регистрации; чат с неизвестным вторым участником; чтение SQL-результатов | integration.json; database.go | +| Password KDF, Unicode, безопасное хранение токена | проверки префикса hash/digest в БД, Unicode login, Go-тест | integration.json; build.txt | +| Реальная роль Redis | рост cache_hits; stop Redis и FLUSHDB; тот же токен читает данные | integration.json | +| Expiry, logout, отказ Redis/БД | TTL=2; 401/503/200 по сценарию | integration.json | +| Ограниченный пул/таймауты | конфигурация приложения, bounded outage assertions | main.go; compose.yaml; integration.json | +| Индекс на ≥10 000 сообщений | EXPLAIN ANALYZE BUFFERS на 100 000 | research.json | +| Трек 2 целиком | два индекса, чтение/запись, dump/restore в другой контейнер/volume, сверка | research.json | -## Эксперименты и отказы +## Эксперимент: план запроса и восстановление -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Гипотеза: индекс `(chat_id,created_at,id)` ограничивает работу на keyset-страницу независимо от длины истории чата, но увеличивает объём индекса и стоимость вставки относительно `(chat_id)`. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +Стенд: Windows, Docker Desktop Linux/WSL2; Intel Core i7-12700H, 14 физических ядер / 20 логических процессоров, RAM около 16 GiB. Compose лимиты: API 1 CPU / 512 MiB, PostgreSQL 1 CPU / 512 MiB, Redis 0.5 CPU / 128 MiB. Restore PostgreSQL — отдельный контейнер 1 CPU / 512 MiB. Образы и зависимости закреплены в Dockerfile/compose/go.mod/go.sum. -## Ограничения и следующие шаги +Методика `python student-tests/research.py`: отдельная исследовательская БД, 100 000 сообщений в 10 чатах: 80 000, 15 000 и 8×625. В большом чате text имеет 200 символов, в остальных 40. Сравниваются `(chat_id)` и `(chat_id,created_at,id)`. Три страницы большого чата: начало, после 40 000, после 79 000; LIMIT 51, два прогрева и пять измерений каждой страницы. В raw JSON сохранены execution times, actual rows и BUFFERS. Для каждого индекса — пять INSERT по 2000 строк под EXPLAIN ANALYZE, затем rollback. Это сравнение работы вставки с FK/triggers, без измерения COMMIT; rollback может оставить dead tuples, что явно ограничивает точность сравнения. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +После измерений выбранный составной индекс сохраняется в схеме. `pg_dump -Fc` создаёт snapshot; `pg_restore --exit-on-error` восстанавливает его в пустую БД отдельного PostgreSQL-контейнера на отдельном named volume. Проверяются counts users/chats/members/messages, digest всех сообщений в устойчивом порядке и 10 полных сообщений. Исследовательские БД после успешной сверки удаляются, backup исключён из Git. + +### Результаты 10 октября 2026 года + +Полный последовательный прогон прошёл. Smoke и persistence вернули PASS; `verification.json` дополнительно подтверждает изменение StartedAt API, PostgreSQL и Redis. Гонка регистрации дала ровно один 201 и три 409; составной чат после FK-ошибки отсутствовал. При отказе Redis сценарий fallback занял 0,375 с; последовательная проверка отказа PostgreSQL заняла 4,031 с (два запроса к БД с таймаутом 2 с, liveness и отказ анонимному пользователю). TTL=2 и logout дали ожидаемые 401. + +| Измерение, медиана пяти повторов | `(chat_id)` | `(chat_id,created_at,id)` | +| --- | ---: | ---: | +| Страница в начале, ms | 18,687 | 0,158 | +| Страница после 40 000, ms | 14,587 | 0,170 | +| Страница после 79 000, ms | 12,460 | 0,149 | +| INSERT 2000 строк с последующим rollback, ms | 125,699 | 116,631 | +| Размер индекса, bytes | 720 896 | 5 914 624 | +| Shared hit blocks средней страницы, выбранный третий повтор | 3224 | 7 | + +План `(chat_id)` на большом чате выбрал parallel sequential scan и top-N sort. Составной индекс дал Index Scan с соответствующим Index Cond и 51 фактической строкой под Limit; полной сортировки/сканирования истории нет. Средняя страница ускорилась примерно в 85,8 раза на этом стенде. Цена по размеру индекса — около 8,2 раза. Предположение о замедлении INSERT в этом коротком опыте не подтвердилось: составной индекс оказался немного быстрее. Нельзя выдавать это за отсутствие стоимости индекса; ограничения порядка вариантов, прогрева, rollback и доминирующих FK/triggers не позволяют сделать такой вывод. Уверенно подтверждены выигрыш чтения и рост размера индекса. + +Backup: 2 882 994 bytes, создание 1,500 с, восстановление 1,734 с. В исходной и восстановленной БД совпали 1 пользователь, 10 чатов, 10 membership и 100 000 сообщений; digest всех сообщений `be1222f55df222fef96c1d71910ec87b`, а также 10 полных выбранных сообщений совпали. Эти данные сохранены вместе с raw планами в `evidence/research.json`. Restore выполнен в отдельном контейнере и отдельном volume, без повторного seed. + +Первый HTTP-прогон: 30,125 с, concurrency=4, 9873 успешных запроса, 0 ошибок, 327,734 successful RPS; p50=15, p95=31, p99=32 ms. Его вывод сохранён в `verification.json`. + +Повтор на финальном образе с одновременными CPU/RAM-сэмплами: 30,093 с, 10 435 успешных запросов, 0 ошибок, 346,758 successful RPS; p50=15, p95=31, p99=32 ms. В `resources.json` сохранены шесть сэмплов Docker stats во время этого прогона; `load.json` содержит итог. HTTP-прогоны начинаются после readiness/bootstrap; отдельный временной прогрев HTTP-нагрузки не выделялся. Максимум среди этих сэмплов (это не непрерывный peak): API 72,90% CPU и 73,16 MiB RAM; PostgreSQL 74,11% и 84,70 MiB; Redis 6,00% и 6,496 MiB. CPU-процент Docker относится к одному логическому CPU. Изменение размера истории и фоновые операции/измерения входят в профиль; разница RPS между повторами не интерпретируется как отдельное доказательство оптимизации. + +## Ограничения + +Одна API-реплика и одна основная БД: отказ PostgreSQL не скрывается, HA не заявляется. Redis — доверенный внутренний сервис локальной Compose-сети, без TLS/ACL; он не опубликован наружу. Пользователь БД в учебном стенде управляет миграциями; разделение migration/runtime ролей оставлено для последующих лабораторных. Secrets Compose — файлы локального оператора, внешний secret manager не используется. + +Перезапуск с volumes проверяет сохранность контейнерного стенда, а не внезапное отключение питания или повреждение диска. Отдельные request contexts ограничивают время ожидания; 201 может быть потерян на сети после COMMIT, поэтому прозрачный retry POST в lab2 не добавлен. Идемпотентность появляется по контракту lab3. + +Backup включает данные на snapshot начала dump. Сообщения, подтверждённые позже этого snapshot, в backup не входят: без WAL archiving/PITR предел потери данных равен времени с последнего backup. Измеренное время восстановления относится к restore уже подготовленного локального контейнера и не включает обнаружение аварии и provisioning. + +Нагрузочный генератор closed-loop, concurrency=4, 30 секунд; latency — только успешных запросов, есть coordinated omission. Это успешные HTTP-подтверждения, а не универсальная максимальная пропускная способность или точный счётчик COMMIT. Индексный эксперимент — пять последовательных повторов на прогретом локальном buffer cache; порядок вариантов фиксирован, статистическая универсальность не заявляется. + +Из факультативной первой лабы сохранены лимит тела, admission gate и метрики; in-memory лимит числа сообщений заменён устойчивым хранением без искусственного ограничения истории. Grafana/Prometheus не входят в обязательный минимум второй лабы, `/metrics` позволяет подключить их отдельно. Следующие шаги: несколько API-реплик и идемпотентность по lab3, разделение DB-ролей, TLS и управление secrets в соответствующих лабораторных. + +## Источники + +- Задание этой лабораторной, `COURSE.md`, `contracts/README.md`, `contracts/openapi.json`, публичные тесты. +- Собственная реализация первой лабораторной: HTTP API, модели, метрики и admission gate. +- Библиотеки `github.com/lib/pq`, `github.com/redis/go-redis/v9`, `golang.org/x/crypto/argon2`; точные версии в go.mod/go.sum. Код SQL, KDF и cache integration написан под контракт этой лабы. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..7ac2484 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,93 @@ +name: mtusi-lab2 +services: + postgres: + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Run python scripts/bootstrap.py} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: [CMD-SHELL, 'pg_isready -U messenger -d messenger'] + interval: 2s + timeout: 2s + retries: 30 + cpus: 1.0 + mem_limit: 512m + restart: unless-stopped + redis: + image: redis:7.4.5-alpine3.21 + command: [redis-server, /etc/redis/redis.conf] + volumes: + - redis_data:/data + - ./infra/redis.conf:/etc/redis/redis.conf:ro + healthcheck: + test: [CMD, redis-cli, ping] + interval: 2s + timeout: 2s + retries: 30 + cpus: 0.5 + mem_limit: 128m + restart: unless-stopped + restore: + profiles: [research] + image: postgres:17.6-alpine3.22 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: restore_validation + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Run python scripts/bootstrap.py} + volumes: + - restore_data:/var/lib/postgresql/data + healthcheck: + test: [CMD-SHELL, 'pg_isready -U messenger -d restore_validation'] + interval: 2s + timeout: 2s + retries: 30 + cpus: 1.0 + mem_limit: 512m + api: + image: mtusi-messenger:lab2 + build: . + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_STORE: postgres + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + DB_MAX_CONNECTIONS: 8 + DB_TIMEOUT_MS: 2000 + MAX_IN_FLIGHT: 64 + MAX_BODY_BYTES: 1048576 + KDF_CONCURRENCY: 4 + ports: + - '127.0.0.1:${HOST_PORT:-8080}:8080' + secrets: + - database_url + - cache_url + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: [CMD, wget, -q, -O, /dev/null, 'http://127.0.0.1:8080/health/ready'] + interval: 2s + timeout: 3s + retries: 30 + cpus: 1.0 + mem_limit: 512m + read_only: true + cap_drop: [ALL] + security_opt: [no-new-privileges:true] + restart: unless-stopped +volumes: + postgres_data: + redis_data: + restore_data: +secrets: + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url diff --git a/database.go b/database.go new file mode 100644 index 0000000..6ffaf0c --- /dev/null +++ b/database.go @@ -0,0 +1,180 @@ +package main + +import ( + "context" + "crypto/sha256" + "database/sql" + "embed" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/lib/pq" + "github.com/redis/go-redis/v9" + "mtusi2026lab2/models" +) + +//go:embed migrations/*.sql +var migrations embed.FS + +type store struct { + db *sql.DB + cache *redis.Client + metrics *cacheMetrics +} + +// A transaction-scoped advisory lock serializes migrations across starting API processes. +func migrate(ctx context.Context, db *sql.DB) error { + tx, err := db.BeginTx(ctx, nil) + if err != nil { + return err + } + defer tx.Rollback() + if _, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(202602)`); err != nil { + return err + } + if _, err = tx.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations (name text PRIMARY KEY, checksum text NOT NULL, applied_at timestamptz NOT NULL DEFAULT clock_timestamp())`); err != nil { + return err + } + entries, err := migrations.ReadDir("migrations") + if err != nil { + return err + } + for _, entry := range entries { + data, err := migrations.ReadFile("migrations/" + entry.Name()) + if err != nil { + return err + } + sum := sha256.Sum256(data) + checksum := hex.EncodeToString(sum[:]) + var previous string + err = tx.QueryRowContext(ctx, `SELECT checksum FROM schema_migrations WHERE name=$1`, entry.Name()).Scan(&previous) + if err == nil { + if previous != checksum { + return fmt.Errorf("changed migration %s", entry.Name()) + } + continue + } + if !errors.Is(err, sql.ErrNoRows) { + return err + } + if _, err = tx.ExecContext(ctx, string(data)); err != nil { + return err + } + if _, err = tx.ExecContext(ctx, `INSERT INTO schema_migrations(name,checksum) VALUES($1,$2)`, entry.Name(), checksum); err != nil { + return err + } + } + return tx.Commit() +} + +func scanUser(row interface{ Scan(...any) error }) (models.User, error) { + var u models.User + err := row.Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt) + u.CreatedAt = u.CreatedAt.UTC() + return u, err +} + +func (s *store) user(ctx context.Context, id models.UUID) (models.User, error) { + key := "profile:v1:" + string(id) + if s.cache != nil { + cacheCtx, cancel := context.WithTimeout(ctx, 150*time.Millisecond) + data, err := s.cache.Get(cacheCtx, key).Bytes() + cancel() + var u models.User + if err == nil && json.Unmarshal(data, &u) == nil && u.ID == id { + s.metrics.hits.Add(1) + return u, nil + } + s.metrics.misses.Add(1) + if err != nil && !errors.Is(err, redis.Nil) { + s.metrics.failures.Add(1) + } + } + u, err := scanUser(s.db.QueryRowContext(ctx, `SELECT id,username,display_name,created_at FROM users WHERE id=$1`, id)) + if err == nil && s.cache != nil { + data, _ := json.Marshal(u) + cacheCtx, cancel := context.WithTimeout(ctx, 150*time.Millisecond) + if s.cache.Set(cacheCtx, key, data, 5*time.Minute).Err() != nil { + s.metrics.failures.Add(1) + } + cancel() + } + return u, err +} + +func (s *store) createChat(ctx context.Context, title string, members []models.UUID) (models.Chat, error) { + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + return models.Chat{}, err + } + defer tx.Rollback() + chat := models.Chat{ID: models.UUID(newID()), Title: title, MemberIDs: members} + err = tx.QueryRowContext(ctx, `INSERT INTO chats(id,title) VALUES($1,$2) RETURNING created_at`, chat.ID, title).Scan(&chat.CreatedAt) + if err != nil { + return chat, err + } + for i, id := range members { + if _, err = tx.ExecContext(ctx, `INSERT INTO chat_members(chat_id,user_id,position) VALUES($1,$2,$3)`, chat.ID, id, i); err != nil { + return chat, err + } + } + err = tx.Commit() + chat.CreatedAt = chat.CreatedAt.UTC() + return chat, err +} + +func scanChat(row interface{ Scan(...any) error }) (models.Chat, error) { + var c models.Chat + var members pq.StringArray + err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &members) + c.CreatedAt = c.CreatedAt.UTC() + c.MemberIDs = make([]models.UUID, len(members)) + for i, m := range members { + c.MemberIDs[i] = models.UUID(m) + } + return c, err +} + +const chatColumns = `c.id,c.title,c.created_at,ARRAY(SELECT user_id::text FROM chat_members WHERE chat_id=c.id ORDER BY position)` + +func (s *store) chat(ctx context.Context, id, user models.UUID) (models.Chat, error) { + return scanChat(s.db.QueryRowContext(ctx, `SELECT `+chatColumns+` FROM chats c WHERE c.id=$1 AND EXISTS(SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=$2)`, id, user)) +} + +func (s *store) chats(ctx context.Context, user models.UUID, p cursor, limit int) ([]models.Chat, error) { + rows, err := s.db.QueryContext(ctx, `SELECT `+chatColumns+` FROM chats c WHERE EXISTS(SELECT 1 FROM chat_members WHERE chat_id=c.id AND user_id=$1) AND (c.created_at,c.id)>($2,$3::uuid) ORDER BY c.created_at,c.id LIMIT $4`, user, p.CreatedAt, p.ID, limit+1) + if err != nil { + return nil, err + } + defer rows.Close() + items := []models.Chat{} + for rows.Next() { + c, err := scanChat(rows) + if err != nil { + return nil, err + } + items = append(items, c) + } + return items, rows.Err() +} + +func (s *store) messages(ctx context.Context, chat models.UUID, p cursor, limit int) ([]models.Message, error) { + rows, err := s.db.QueryContext(ctx, `SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid) ORDER BY created_at,id LIMIT $4`, chat, p.CreatedAt, p.ID, limit+1) + if err != nil { + return nil, err + } + defer rows.Close() + items := []models.Message{} + for rows.Next() { + var m models.Message + if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil { + return nil, err + } + m.CreatedAt = m.CreatedAt.UTC() + items = append(items, m) + } + return items, rows.Err() +} diff --git a/degradation.go b/degradation.go new file mode 100644 index 0000000..62dd098 --- /dev/null +++ b/degradation.go @@ -0,0 +1,94 @@ +package main + +import ( + "fmt" + "os" + "runtime" + runtimemetrics "runtime/metrics" + "strconv" + "strings" + "sync/atomic" +) + +type limits struct { + maxInFlight int + maxBodyBytes int64 +} + +func loadLimits() limits { + return limits{ + maxInFlight: envInt("MAX_IN_FLIGHT", 64, 1), + maxBodyBytes: int64(envInt("MAX_BODY_BYTES", 1<<20, 1024)), + } +} + +func envInt(name string, fallback, minimum int) int { + value, err := strconv.Atoi(os.Getenv(name)) + if err != nil || value < minimum { + return fallback + } + return value +} + +type metrics struct { + requests atomic.Uint64 + inFlight atomic.Int64 + status413 atomic.Uint64 + status429 atomic.Uint64 + status503 atomic.Uint64 + accepted atomic.Uint64 +} + +func (metrics *metrics) snapshot() string { + var memory runtime.MemStats + runtime.ReadMemStats(&memory) + processCPU := processCPUSeconds() + residentMemory := residentMemoryBytes() + return fmt.Sprintf("# HELP messenger_requests_total Total HTTP requests.\n"+ + "# TYPE messenger_requests_total counter\n"+ + "messenger_requests_total %d\n"+ + "# HELP messenger_responses_total Responses rejected by overload protection.\n"+ + "# TYPE messenger_responses_total counter\n"+ + "messenger_responses_total{status=\"413\"} %d\n"+ + "messenger_responses_total{status=\"429\"} %d\n"+ + "messenger_responses_total{status=\"503\"} %d\n"+ + "# HELP messenger_messages_accepted_total Confirmed message writes.\n"+ + "# TYPE messenger_messages_accepted_total counter\n"+ + "messenger_messages_accepted_total %d\n"+ + "# HELP messenger_in_flight Current requests inside the admission gate.\n"+ + "# TYPE messenger_in_flight gauge\n"+"messenger_in_flight %d\n"+ + "# HELP messenger_memory_alloc_bytes Go heap bytes currently allocated.\n"+ + "# TYPE messenger_memory_alloc_bytes gauge\n"+"messenger_memory_alloc_bytes %d\n"+ + "# HELP process_cpu_seconds_total CPU time consumed by the Go process.\n"+ + "# TYPE process_cpu_seconds_total counter\n"+"process_cpu_seconds_total %.6f\n"+ + "# HELP process_resident_memory_bytes Resident memory used by the Go process.\n"+ + "# TYPE process_resident_memory_bytes gauge\n"+"process_resident_memory_bytes %d\n", + metrics.requests.Load(), metrics.status413.Load(), metrics.status429.Load(), metrics.status503.Load(), + metrics.accepted.Load(), metrics.inFlight.Load(), memory.Alloc, + processCPU, residentMemory) +} + +func processCPUSeconds() float64 { + samples := []runtimemetrics.Sample{{Name: "/cpu/classes/total:cpu-seconds"}} + runtimemetrics.Read(samples) + if samples[0].Value.Kind() != runtimemetrics.KindFloat64 { + return 0 + } + return samples[0].Value.Float64() +} + +func residentMemoryBytes() int64 { + data, err := os.ReadFile("/proc/self/statm") + if err != nil { + return 0 + } + fields := strings.Fields(string(data)) + if len(fields) < 2 { + return 0 + } + residentPages, err := strconv.ParseInt(fields[1], 10, 64) + if err != nil { + return 0 + } + return residentPages * int64(os.Getpagesize()) +} diff --git a/evidence/build.txt b/evidence/build.txt new file mode 100644 index 0000000..7f3b1a7 --- /dev/null +++ b/evidence/build.txt @@ -0,0 +1,81 @@ + Image mtusi-messenger:lab2 Building +#1 [internal] load local bake definitions +#1 reading from stdin 573B 0.1s done +#1 DONE 0.1s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 419B 0.0s done +#2 DONE 0.0s + +#3 [auth] library/golang:pull token for registry-1.docker.io +#3 DONE 0.0s + +#4 [auth] library/alpine:pull token for registry-1.docker.io +#4 DONE 0.0s + +#5 [internal] load metadata for docker.io/library/golang:1.24.7-alpine3.22 +#5 ... + +#6 [internal] load metadata for docker.io/library/alpine:3.22.1 +#6 DONE 1.0s + +#5 [internal] load metadata for docker.io/library/golang:1.24.7-alpine3.22 +#5 DONE 1.1s + +#7 [internal] load .dockerignore +#7 transferring context: 216B done +#7 DONE 0.0s + +#8 [build 1/7] FROM docker.io/library/golang:1.24.7-alpine3.22@sha256:fc2cff6625f3c1c92e6c85938ac5bd09034ad0d4bc2dfb08278020b68540dbb5 +#8 resolve docker.io/library/golang:1.24.7-alpine3.22@sha256:fc2cff6625f3c1c92e6c85938ac5bd09034ad0d4bc2dfb08278020b68540dbb5 0.1s done +#8 DONE 0.1s + +#9 [stage-1 1/3] FROM docker.io/library/alpine:3.22.1@sha256:4bcff63911fcb4448bd4fdacec207030997caf25e9bea4045fa6c8c44de311d1 +#9 resolve docker.io/library/alpine:3.22.1@sha256:4bcff63911fcb4448bd4fdacec207030997caf25e9bea4045fa6c8c44de311d1 0.1s done +#9 DONE 0.1s + +#10 [internal] load build context +#10 transferring context: 58.65kB 0.0s done +#10 DONE 0.0s + +#11 [build 2/7] WORKDIR /src +#11 CACHED + +#12 [build 3/7] COPY go.mod go.sum ./ +#12 CACHED + +#13 [build 4/7] RUN go mod download +#13 CACHED + +#14 [build 5/7] COPY . . +#14 DONE 0.1s + +#15 [build 6/7] RUN go test ./... && go vet ./... +#15 14.10 ok mtusi2026lab2 0.237s +#15 14.10 ? mtusi2026lab2/models [no test files] +#15 DONE 15.9s + +#16 [build 7/7] RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/messenger . +#16 DONE 12.7s + +#17 [stage-1 2/3] RUN addgroup -S app && adduser -S -G app app +#17 CACHED + +#18 [stage-1 3/3] COPY --from=build /out/messenger /usr/local/bin/messenger +#18 DONE 0.1s + +#19 exporting to image +#19 exporting layers +#19 exporting layers 0.6s done +#19 exporting manifest sha256:e8c811054d3254708949e94dcb384e201823a951dfb50590c69df24fdc7586a0 0.0s done +#19 exporting config sha256:8ed4ff1295869d76d500ce5a74c63c939d1ca103d72fd643d9e9d2714fcf6324 0.0s done +#19 exporting attestation manifest sha256:368d2846dd6d36cc83b3d7607dc5a9cea986cfe8ecd7d2085b2e64f4c9bec07e 0.0s done +#19 exporting manifest list sha256:4561c35282d7c428edfa00f1f12a04c21c70ca2eaa0b1a462d4e1d9fc69ab764 0.0s done +#19 naming to docker.io/library/mtusi-messenger:lab2 +#19 naming to docker.io/library/mtusi-messenger:lab2 done +#19 unpacking to docker.io/library/mtusi-messenger:lab2 0.1s done +#19 DONE 0.9s + +#20 resolving provenance for metadata file +#20 DONE 0.0s + Image mtusi-messenger:lab2 Built diff --git a/evidence/integration.json b/evidence/integration.json new file mode 100644 index 0000000..5687807 --- /dev/null +++ b/evidence/integration.json @@ -0,0 +1,25 @@ +{ + "registration_race": [ + 409, + 409, + 409, + 201 + ], + "rollback": "PASS: chat and membership inserts rolled back", + "credential_storage": "Argon2id; 32-byte session digests", + "contract_edges": "PASS: cursor scope, no sender spoof, 128 Unicode password characters including colon", + "redis_down": { + "fallback": "PASS", + "seconds": 0.329 + }, + "cache_loss": "PASS: cache rebuild; original session/data valid", + "postgres_down": { + "protected_request": 503, + "readiness": 503, + "liveness": 200, + "seconds": 4.011 + }, + "migration_replay": "PASS: same migration/user/message counts", + "expiry": "PASS: SESSION_TTL_SECONDS=2 rejects existing token after expiry", + "logout": "PASS: revoked token rejected" +} diff --git a/evidence/load.json b/evidence/load.json new file mode 100644 index 0000000..0a97550 --- /dev/null +++ b/evidence/load.json @@ -0,0 +1,17 @@ +{ + "lab": 2, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.065, + "concurrency": 4, + "successful_requests": 8904, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 296.157, + "successful_latency_ms": { + "p50": 5.602, + "p95": 30.814, + "p99": 119.181 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/research.json b/evidence/research.json new file mode 100644 index 0000000..eaa46cb --- /dev/null +++ b/evidence/research.json @@ -0,0 +1,1114 @@ +{ + "generated_at_utc": "2026-10-22T10:24:30.857161+00:00", + "hypothesis": "Index (chat_id,created_at,id) bounds keyset page work independently of total chat history, at a measurable write/index-space cost.", + "host": "Windows-11-10.0.26200-SP0", + "limits": { + "postgres_cpu": 1, + "postgres_memory_mb": 512 + }, + "messages": 100000, + "distribution": [ + 80000, + 15000, + 625, + 625, + 625, + 625, + 625, + 625, + 625, + 625 + ], + "warmup_reads": 2, + "measured_repetitions": 5, + "variants": { + "chat_only": { + "index_bytes": 720896, + "pages": { + "0": { + "execution_ms": [ + 10.364, + 10.573, + 11.867, + 10.955, + 10.857 + ], + "median_ms": 10.857, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 6139.88, + "Total Cost": 6145.83, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 9.259, + "Actual Total Time": 11.759, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 6139.88, + "Total Cost": 13921.39, + "Plan Rows": 66694, + "Plan Width": 227, + "Actual Startup Time": 9.258, + "Actual Total Time": 11.745, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 2, + "Workers Launched": 2, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5139.86, + "Total Cost": 5223.23, + "Plan Rows": 33347, + "Plan Width": 227, + "Actual Startup Time": 6.124, + "Actual Total Time": 6.129, + "Actual Rows": 51, + "Actual Loops": 3, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + }, + { + "Worker Number": 1, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 4027.33, + "Plan Rows": 33347, + "Plan Width": 227, + "Actual Startup Time": 0.008, + "Actual Total Time": 4.187, + "Actual Rows": 26667, + "Actual Loops": 3, + "Filter": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '00:00:00'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Filter": 6667, + "Shared Hit Blocks": 3194, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 95, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.579, + "Triggers": [], + "Execution Time": 11.867 + } + }, + "40000": { + "execution_ms": [ + 8.986, + 8.554, + 8.792, + 9.158, + 9.034 + ], + "median_ms": 8.986, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5471.34, + "Total Cost": 5477.29, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 6.912, + "Actual Total Time": 8.695, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5471.34, + "Total Cost": 8576.76, + "Plan Rows": 26616, + "Plan Width": 227, + "Actual Startup Time": 6.911, + "Actual Total Time": 8.69, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 2, + "Workers Launched": 2, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4471.32, + "Total Cost": 4504.59, + "Plan Rows": 13308, + "Plan Width": 227, + "Actual Startup Time": 4.566, + "Actual Total Time": 4.568, + "Actual Rows": 37, + "Actual Loops": 3, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + }, + { + "Worker Number": 1, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 4027.33, + "Plan Rows": 13308, + "Plan Width": 227, + "Actual Startup Time": 1.456, + "Actual Total Time": 3.588, + "Actual Rows": 13333, + "Actual Loops": 3, + "Filter": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '11:06:40'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Filter": 20000, + "Shared Hit Blocks": 3194, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 95, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.548, + "Triggers": [], + "Execution Time": 8.792 + } + }, + "79000": { + "execution_ms": [ + 8.468, + 7.935, + 8.093, + 8.911, + 7.61 + ], + "median_ms": 8.093, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5038.9, + "Total Cost": 5044.85, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 5.863, + "Actual Total Time": 8.001, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5038.9, + "Total Cost": 5119.64, + "Plan Rows": 692, + "Plan Width": 227, + "Actual Startup Time": 5.862, + "Actual Total Time": 7.984, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 2, + "Workers Launched": 2, + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4038.88, + "Total Cost": 4039.74, + "Plan Rows": 346, + "Plan Width": 227, + "Actual Startup Time": 3.495, + "Actual Total Time": 3.497, + "Actual Rows": 50, + "Actual Loops": 3, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 3224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + }, + { + "Worker Number": 1, + "Sort Method": "top-N heapsort", + "Sort Space Used": 50, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 4027.33, + "Plan Rows": 346, + "Plan Width": 227, + "Actual Startup Time": 2.97, + "Actual Total Time": 3.399, + "Actual Rows": 333, + "Actual Loops": 3, + "Filter": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '21:56:40'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Filter": 33000, + "Shared Hit Blocks": 3194, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 95, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.49, + "Triggers": [], + "Execution Time": 8.093 + } + } + }, + "write_2000": { + "execution_ms": [ + 58.665, + 65.9, + 61.239, + 61.952, + 60.61 + ], + "median_ms": 61.239, + "representative_plan": { + "Plan": { + "Node Type": "ModifyTable", + "Operation": "Insert", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 65.0, + "Plan Rows": 0, + "Plan Width": 0, + "Actual Startup Time": 12.184, + "Actual Total Time": 12.185, + "Actual Rows": 0, + "Actual Loops": 1, + "Shared Hit Blocks": 12224, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 84, + "Shared Written Blocks": 84, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Function Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Function Name": "generate_series", + "Alias": "n", + "Startup Cost": 0.0, + "Total Cost": 65.0, + "Plan Rows": 2000, + "Plan Width": 88, + "Actual Startup Time": 0.308, + "Actual Total Time": 3.175, + "Actual Rows": 2000, + "Actual Loops": 1, + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 21, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.186, + "Triggers": [ + { + "Trigger Name": "RI_ConstraintTrigger_c_24720", + "Constraint Name": "messages_chat_id_fkey", + "Relation": "messages", + "Time": 24.438, + "Calls": 2000 + }, + { + "Trigger Name": "RI_ConstraintTrigger_c_24725", + "Constraint Name": "messages_chat_id_sender_id_fkey", + "Relation": "messages", + "Time": 24.317, + "Calls": 2000 + } + ], + "Execution Time": 61.239 + } + } + }, + "keyset": { + "index_bytes": 5914624, + "pages": { + "0": { + "execution_ms": [ + 0.091, + 0.081, + 0.085, + 0.119, + 0.088 + ], + "median_ms": 0.088, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 9.19, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.029, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 13740.4, + "Plan Rows": 79875, + "Plan Width": 227, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.025, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '00:00:00'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 106, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.535, + "Triggers": [], + "Execution Time": 0.085 + } + }, + "40000": { + "execution_ms": [ + 0.097, + 0.094, + 0.094, + 0.103, + 0.098 + ], + "median_ms": 0.097, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 17.55, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 0.023, + "Actual Total Time": 0.038, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 7, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 10735.69, + "Plan Rows": 31966, + "Plan Width": 227, + "Actual Startup Time": 0.023, + "Actual Total Time": 0.035, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '11:06:40'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 7, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 106, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.501, + "Triggers": [], + "Execution Time": 0.094 + } + }, + "79000": { + "execution_ms": [ + 0.091, + 0.092, + 0.086, + 0.102, + 0.087 + ], + "median_ms": 0.091, + "representative_plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 115.08, + "Plan Rows": 51, + "Plan Width": 227, + "Actual Startup Time": 0.023, + "Actual Total Time": 0.033, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1976.68, + "Plan Rows": 879, + "Plan Width": 227, + "Actual Startup Time": 0.022, + "Actual Total Time": 0.029, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW(('2026-01-01 00:00:00+00'::timestamp with time zone + '21:56:40'::interval), 'ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 106, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.522, + "Triggers": [], + "Execution Time": 0.086 + } + } + }, + "write_2000": { + "execution_ms": [ + 66.689, + 67.817, + 61.934, + 64.02, + 71.374 + ], + "median_ms": 66.689, + "representative_plan": { + "Plan": { + "Node Type": "ModifyTable", + "Operation": "Insert", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 65.0, + "Plan Rows": 0, + "Plan Width": 0, + "Actual Startup Time": 13.263, + "Actual Total Time": 13.263, + "Actual Rows": 0, + "Actual Loops": 1, + "Shared Hit Blocks": 14270, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 102, + "Shared Written Blocks": 102, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Function Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Function Name": "generate_series", + "Alias": "n", + "Startup Cost": 0.0, + "Total Cost": 65.0, + "Plan Rows": 2000, + "Plan Width": 88, + "Actual Startup Time": 0.358, + "Actual Total Time": 3.008, + "Actual Rows": 2000, + "Actual Loops": 1, + "Shared Hit Blocks": 0, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 21, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + }, + "Planning Time": 0.17, + "Triggers": [ + { + "Trigger Name": "RI_ConstraintTrigger_c_24720", + "Constraint Name": "messages_chat_id_fkey", + "Relation": "messages", + "Time": 23.991, + "Calls": 2000 + }, + { + "Trigger Name": "RI_ConstraintTrigger_c_24725", + "Constraint Name": "messages_chat_id_sender_id_fkey", + "Relation": "messages", + "Time": 24.41, + "Calls": 2000 + } + ], + "Execution Time": 61.934 + } + } + } + }, + "postgres_version": "PostgreSQL 17.6 on x86_64-pc-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit", + "backup_restore": { + "backup_bytes": 2882987, + "backup_seconds": 0.574, + "restore_seconds": 0.779, + "source": { + "users": 1, + "chats": 10, + "members": 10, + "messages": 100000, + "content_digest": "be1222f55df222fef96c1d71910ec87b", + "selected": [ + { + "id": "0000882b-fe35-c255-54a7-1cd1579d9006", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T18:14:34+00:00" + }, + { + "id": "0002cfad-a6bc-ef0f-c84d-4e3988f17764", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T02:26:02+00:00" + }, + { + "id": "00030608-be2c-f10c-2a5b-4cbd0d4c3a85", + "chat_id": "8f75de3d-47bf-55ab-9411-7165997387e6", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T01:24:23+00:00" + }, + { + "id": "0003781e-a0d4-f9dd-89a4-575d8b1bbf96", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T08:24:03+00:00" + }, + { + "id": "00042c63-b5a0-9a2a-091e-45dc4ccceca6", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T09:42:31+00:00" + }, + { + "id": "00043264-f2db-3704-4f99-9077c288fb38", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T15:40:34+00:00" + }, + { + "id": "0004722b-bb4a-57ad-8008-a171cdb39ea4", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T19:48:18+00:00" + }, + { + "id": "0004b9f5-173e-4e3d-0164-420727828f54", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T12:35:59+00:00" + }, + { + "id": "0004e195-adcb-7438-1b13-78cc7c25275e", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T14:15:32+00:00" + }, + { + "id": "0004f30a-b6a3-be35-aaf7-6f145ae3fb54", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T05:45:26+00:00" + } + ] + }, + "restored": { + "users": 1, + "chats": 10, + "members": 10, + "messages": 100000, + "content_digest": "be1222f55df222fef96c1d71910ec87b", + "selected": [ + { + "id": "0000882b-fe35-c255-54a7-1cd1579d9006", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T18:14:34+00:00" + }, + { + "id": "0002cfad-a6bc-ef0f-c84d-4e3988f17764", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T02:26:02+00:00" + }, + { + "id": "00030608-be2c-f10c-2a5b-4cbd0d4c3a85", + "chat_id": "8f75de3d-47bf-55ab-9411-7165997387e6", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T01:24:23+00:00" + }, + { + "id": "0003781e-a0d4-f9dd-89a4-575d8b1bbf96", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T08:24:03+00:00" + }, + { + "id": "00042c63-b5a0-9a2a-091e-45dc4ccceca6", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T09:42:31+00:00" + }, + { + "id": "00043264-f2db-3704-4f99-9077c288fb38", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T15:40:34+00:00" + }, + { + "id": "0004722b-bb4a-57ad-8008-a171cdb39ea4", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T19:48:18+00:00" + }, + { + "id": "0004b9f5-173e-4e3d-0164-420727828f54", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T12:35:59+00:00" + }, + { + "id": "0004e195-adcb-7438-1b13-78cc7c25275e", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T14:15:32+00:00" + }, + { + "id": "0004f30a-b6a3-be35-aaf7-6f145ae3fb54", + "chat_id": "22222222-2222-4222-8222-222222222222", + "sender_id": "11111111-1111-4111-8111-111111111111", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T05:45:26+00:00" + } + ] + }, + "equal": true, + "scope": "empty database on separate PostgreSQL container and separate named volume; all counts, ordered digest of every message, and 10 complete messages match" + }, + "limitations": [ + "Warm buffer cache, 5 sequential trials, local single-host containers; no claim of max production throughput.", + "Write timing is EXPLAIN ANALYZE of 2000 inserts followed by rollback; includes constraints/triggers and excludes durable COMMIT latency. Aborted inserts can leave dead tuples.", + "Logical backup captures the dump snapshot. Writes committed after that snapshot are not included; no WAL archiving/PITR. Restore time excludes operator detection and provisioning.", + "Research fixtures use SQL bulk insert; HTTP validation/authentication cost is measured separately by the public load tool." + ] +} diff --git a/evidence/resources.json b/evidence/resources.json new file mode 100644 index 0000000..e8e22b4 --- /dev/null +++ b/evidence/resources.json @@ -0,0 +1,237 @@ +{ + "sampled_at_utc": "2026-10-22T10:26:34.875518+00:00", + "command": "python tests/load.py --base-url http://127.0.0.1:8080 --duration 30 --concurrency 4 --output evidence/load.json", + "exit_code": 0, + "stdout": "{\n \"lab\": 2,\n \"workload\": \"closed-loop POST messages; no automatic retries\",\n \"requested_duration_seconds\": 30.0,\n \"elapsed_seconds\": 30.065,\n \"concurrency\": 4,\n \"successful_requests\": 8904,\n \"failed_requests\": 0,\n \"error_ratio\": 0.0,\n \"successful_rps\": 296.157,\n \"successful_latency_ms\": {\n \"p50\": 5.602,\n \"p95\": 30.814,\n \"p99\": 119.181\n },\n \"note\": \"Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.\"\n}", + "stderr": "", + "samples": [ + { + "seconds": 2.516, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "71.77%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "14.88%", + "MemUsage": "76.17MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "26.4MB / 31.1MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 187MB", + "CPUPerc": "53.25%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "16.52%", + "MemUsage": "84.58MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "19.8MB / 15.7MB", + "PIDs": "10" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.34%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "3.03%", + "MemUsage": "3.879MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + }, + { + "seconds": 7.661, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "37.50%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "19.40%", + "MemUsage": "99.31MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "29.5MB / 34.9MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 200MB", + "CPUPerc": "40.26%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "16.88%", + "MemUsage": "86.4MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "22.1MB / 17.5MB", + "PIDs": "11" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "2.75%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "3.03%", + "MemUsage": "3.879MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + }, + { + "seconds": 12.737, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "42.46%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "2.44%", + "MemUsage": "12.48MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "33.4MB / 39.4MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 212MB", + "CPUPerc": "41.65%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "17.25%", + "MemUsage": "88.34MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "24MB / 19MB", + "PIDs": "12" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.34%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "3.08%", + "MemUsage": "3.938MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + }, + { + "seconds": 17.838, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "15.68%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "2.25%", + "MemUsage": "11.51MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "35.1MB / 41.5MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 223MB", + "CPUPerc": "12.26%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "16.77%", + "MemUsage": "85.88MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "25.9MB / 20.5MB", + "PIDs": "11" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.28%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "3.35%", + "MemUsage": "4.293MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + }, + { + "seconds": 22.904, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "14.09%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "2.30%", + "MemUsage": "11.78MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "36.2MB / 42.7MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 227MB", + "CPUPerc": "34.57%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "16.89%", + "MemUsage": "86.49MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "26.5MB / 21MB", + "PIDs": "11" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.34%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "2.84%", + "MemUsage": "3.629MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + }, + { + "seconds": 27.971, + "containers": [ + { + "BlockIO": "0B / 0B", + "CPUPerc": "51.72%", + "Container": "7a8813e9a5256badf4587e977fa252bed25ab8f7263d8eaabd0b5439873dcb46", + "ID": "7a8813e9a525", + "MemPerc": "2.55%", + "MemUsage": "13.05MiB / 512MiB", + "Name": "mtusi-lab2-api-1", + "NetIO": "39MB / 46MB", + "PIDs": "14" + }, + { + "BlockIO": "11.2MB / 236MB", + "CPUPerc": "55.88%", + "Container": "1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be", + "ID": "1543f104c8a8", + "MemPerc": "16.93%", + "MemUsage": "86.68MiB / 512MiB", + "Name": "mtusi-lab2-postgres-1", + "NetIO": "27.9MB / 22.2MB", + "PIDs": "11" + }, + { + "BlockIO": "0B / 0B", + "CPUPerc": "0.39%", + "Container": "1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853", + "ID": "1e84e689b424", + "MemPerc": "3.03%", + "MemUsage": "3.879MiB / 128MiB", + "Name": "mtusi-lab2-redis-1", + "NetIO": "6.38kB / 1.57kB", + "PIDs": "6" + } + ] + } + ] +} diff --git a/evidence/verification.json b/evidence/verification.json new file mode 100644 index 0000000..a6b151a --- /dev/null +++ b/evidence/verification.json @@ -0,0 +1,60 @@ +{ + "timestamp_utc": "2026-10-22T10:24:03.697760+00:00", + "host": "Windows-11-10.0.26200-SP0", + "base_url": "http://127.0.0.1:8080", + "checks": [ + { + "command": "scripts/check_contract.py", + "exit_code": 0, + "seconds": 0.106, + "stdout": "PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.", + "stderr": "" + }, + { + "command": "tests/smoke.py --base-url http://127.0.0.1:8080", + "exit_code": 0, + "seconds": 0.829, + "stdout": "PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.", + "stderr": "" + }, + { + "command": "tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080", + "exit_code": 0, + "seconds": 5.869, + "stdout": "Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.\nPASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.", + "stderr": "" + }, + { + "command": "student-tests/integration.py", + "exit_code": 0, + "seconds": 19.568, + "stdout": "PASS: races, rollback, KDF, cache hit/loss/outage, DB outage, migration replay, expiry, logout", + "stderr": "" + }, + { + "command": "student-tests/research.py", + "exit_code": 0, + "seconds": 27.163, + "stdout": "PASS: 100000 messages, two indexes/read-write measurements, backup restored to new database with matching content\n{\"read_middle_ms\": {\"chat_only\": 8.986, \"keyset\": 0.097}, \"restore_seconds\": 0.779}", + "stderr": "" + }, + { + "command": "tests/load.py --base-url http://127.0.0.1:8080 --duration 30 --concurrency 4 --output evidence/load.json", + "exit_code": 0, + "seconds": 30.647, + "stdout": "{\n \"lab\": 2,\n \"workload\": \"closed-loop POST messages; no automatic retries\",\n \"requested_duration_seconds\": 30.0,\n \"elapsed_seconds\": 30.135,\n \"concurrency\": 4,\n \"successful_requests\": 15594,\n \"failed_requests\": 0,\n \"error_ratio\": 0.0,\n \"successful_rps\": 517.47,\n \"successful_latency_ms\": {\n \"p50\": 4.73,\n \"p95\": 24.602,\n \"p99\": 29.299\n },\n \"note\": \"Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.\"\n}", + "stderr": "" + } + ], + "restart_before": [ + "\"/mtusi-lab2-api-1\" \"848d079a208b3cfc9983752a913cf731e723739703108ebc5e46f8a47c2cca8d\" \"2026-10-22T10:23:03.052015698Z\" \"running\"", + "\"/mtusi-lab2-postgres-1\" \"1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be\" \"2026-10-22T10:22:46.693936115Z\" \"running\"", + "\"/mtusi-lab2-redis-1\" \"1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853\" \"2026-10-22T10:22:46.359922669Z\" \"running\"" + ], + "restart_after": [ + "\"/mtusi-lab2-api-1\" \"848d079a208b3cfc9983752a913cf731e723739703108ebc5e46f8a47c2cca8d\" \"2026-10-22T10:24:10.361747033Z\" \"running\"", + "\"/mtusi-lab2-postgres-1\" \"1543f104c8a8e6c5f198f5b942014c0e5ef95bf627210975c01d7f36b76cc7be\" \"2026-10-22T10:24:06.279071946Z\" \"running\"", + "\"/mtusi-lab2-redis-1\" \"1e84e689b4244d86e68d64e517d57a2548955aa2739db83146c3d302833bb853\" \"2026-10-22T10:24:06.140891322Z\" \"running\"" + ], + "restart_scope_verified": true +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bfb4674 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module mtusi2026lab2 + +go 1.24.0 + +require ( + github.com/lib/pq v1.10.9 + github.com/redis/go-redis/v9 v9.12.1 + golang.org/x/crypto v0.41.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + golang.org/x/sys v0.35.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9690e1f --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/redis/go-redis/v9 v9.12.1 h1:k5iquqv27aBtnTm2tIkROUDp8JBXhXZIVu1InSgvovg= +github.com/redis/go-redis/v9 v9.12.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw= +golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4= +golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/infra/redis.conf b/infra/redis.conf new file mode 100644 index 0000000..052c1f1 --- /dev/null +++ b/infra/redis.conf @@ -0,0 +1,8 @@ +bind 0.0.0.0 +protected-mode no +# Only reachable on the private Compose network; no host port. +appendonly no +save "" +maxmemory 64mb +maxmemory-policy allkeys-lru +# Profiles are disposable; PostgreSQL holds all durable data and sessions. diff --git a/main.go b/main.go new file mode 100644 index 0000000..b8e89f0 --- /dev/null +++ b/main.go @@ -0,0 +1,571 @@ +package main + +import ( + "bytes" + "context" + "crypto/rand" + "crypto/sha256" + "database/sql" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net" + "net/http" + "os" + "os/signal" + "regexp" + "strconv" + "strings" + "sync/atomic" + "syscall" + "time" + "unicode/utf8" + + "github.com/lib/pq" + "github.com/redis/go-redis/v9" + "mtusi2026lab2/models" +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) +var uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) + +type cacheMetrics struct{ hits, misses, failures atomic.Uint64 } +type app struct { + store *store + limits limits + metrics metrics + cacheMetrics cacheMetrics + gate, kdfGate chan struct{} + ttl time.Duration + timeout time.Duration + dummyHash string +} +type createUser struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} +type createChat struct { + Title string `json:"title"` + MemberIDs []models.UUID `json:"member_ids"` +} +type createMessage struct { + Text string `json:"text"` +} +type cursor struct { + Resource string `json:"resource"` + CreatedAt time.Time `json:"created_at"` + ID models.UUID `json:"id"` +} + +func setting(name, fallback string) string { + if value := os.Getenv(name); value != "" { + return value + } + return fallback +} +func secret(name string) string { + if path := os.Getenv(name + "_FILE"); path != "" { + data, err := os.ReadFile(path) + if err != nil { + log.Fatalf("cannot read %s_FILE", name) + } + return strings.TrimSpace(string(data)) + } + return os.Getenv(name) +} + +func main() { + if setting("SESSION_STORE", "postgres") != "postgres" { + log.Fatal("this implementation uses PostgreSQL sessions") + } + db, err := sql.Open("postgres", secret("DATABASE_URL")) + if err != nil { + log.Fatal("invalid database configuration") + } + defer db.Close() + pool := envInt("DB_MAX_CONNECTIONS", 8, 1) + db.SetMaxOpenConns(pool) + db.SetMaxIdleConns(pool) + db.SetConnMaxLifetime(30 * time.Minute) + bootCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + if err = migrate(bootCtx, db); err != nil { + cancel() + log.Fatal("database migration failed: ", err) + } + cancel() + configuration := loadLimits() + a := &app{store: &store{db: db}, limits: configuration, gate: make(chan struct{}, configuration.maxInFlight), kdfGate: make(chan struct{}, envInt("KDF_CONCURRENCY", 4, 1)), ttl: time.Duration(envInt("SESSION_TTL_SECONDS", 3600, 1)) * time.Second, timeout: time.Duration(envInt("DB_TIMEOUT_MS", 2000, 100)) * time.Millisecond, dummyHash: hashPassword("dummy-never-a-login-password")} + if url := secret("CACHE_URL"); url != "" { + options, err := redis.ParseURL(url) + if err != nil { + log.Fatal("invalid cache configuration") + } + options.PoolSize = pool + options.MaxRetries = -1 + options.DialTimeout = 150 * time.Millisecond + options.ReadTimeout = 150 * time.Millisecond + options.WriteTimeout = 150 * time.Millisecond + options.PoolTimeout = 150 * time.Millisecond + options.ContextTimeoutEnabled = true + a.store.cache = redis.NewClient(options) + a.store.metrics = &a.cacheMetrics + defer a.store.cache.Close() + } + rootCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + go func() { + ticker := time.NewTicker(time.Minute) + defer ticker.Stop() + for { + select { + case <-rootCtx.Done(): + return + case <-ticker.C: + ctx, cancel := context.WithTimeout(rootCtx, a.timeout) + _, _ = db.ExecContext(ctx, `DELETE FROM sessions WHERE expires_at<=clock_timestamp()`) + cancel() + } + } + }() + server := &http.Server{Addr: net.JoinHostPort(setting("APP_HOST", "0.0.0.0"), setting("APP_PORT", "8080")), Handler: a.routes(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 15 * time.Second, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 16 * 1024} + serverErrors := make(chan error, 1) + go func() { log.Printf("server listening on %s", server.Addr); serverErrors <- server.ListenAndServe() }() + select { + case err := <-serverErrors: + if err != nil && err != http.ErrServerClosed { + log.Fatal(err) + } + case <-rootCtx.Done(): + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := server.Shutdown(ctx); err != nil { + _ = server.Close() + } + } +} + +func (a *app) routes() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("GET /health/live", a.health) + mux.HandleFunc("GET /health/ready", a.health) + mux.HandleFunc("GET /metrics", a.metricsHandler) + mux.HandleFunc("POST /api/v1/users", a.users) + mux.HandleFunc("POST /api/v1/auth/sessions", a.login) + mux.HandleFunc("DELETE /api/v1/auth/sessions/current", a.logout) + mux.HandleFunc("GET /api/v1/users/me", a.currentUser) + mux.HandleFunc("/api/v1/chats", a.chats) + mux.HandleFunc("/api/v1/chats/", a.chatResource) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + a.error(w, 404, "not_found", "Объект не найден") + }) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("X-Request-Id", newID()) + a.metrics.requests.Add(1) + if r.ContentLength > a.limits.maxBodyBytes { + a.metrics.status413.Add(1) + a.error(w, 413, "payload_too_large", "Размер запроса превышает лимит") + return + } + // Health and metrics remain observable while the business admission gate is full. + if !strings.HasPrefix(r.URL.Path, "/health/") && r.URL.Path != "/metrics" { + select { + case a.gate <- struct{}{}: + a.metrics.inFlight.Add(1) + defer func() { <-a.gate; a.metrics.inFlight.Add(-1) }() + default: + a.rateLimited(w) + return + } + } + defer func() { + if recover() != nil { + a.error(w, 500, "internal_error", "Внутренняя ошибка") + } + }() + ctx, cancel := context.WithTimeout(r.Context(), a.timeout) + defer cancel() + mux.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +func (a *app) error(w http.ResponseWriter, status int, code, message string) { + writeJSON(w, status, map[string]any{"error": map[string]string{"code": code, "message": message, "request_id": w.Header().Get("X-Request-Id")}}) +} +func (a *app) unavailable(w http.ResponseWriter) { + a.metrics.status503.Add(1) + a.error(w, 503, "unavailable", "Хранилище временно недоступно") +} +func (a *app) unauthorized(w http.ResponseWriter, basic bool) { + scheme := `Bearer realm="messenger"` + if basic { + scheme = `Basic realm="messenger", charset="UTF-8"` + } + w.Header().Set("WWW-Authenticate", scheme) + a.error(w, 401, "unauthorized", "Нет действительной аутентификации") +} +func (a *app) rateLimited(w http.ResponseWriter) { + w.Header().Set("Retry-After", "1") + a.metrics.status429.Add(1) + a.error(w, 429, "rate_limited", "Слишком много запросов") +} +func (a *app) kdf(w http.ResponseWriter) bool { + select { + case a.kdfGate <- struct{}{}: + return true + default: + a.rateLimited(w) + return false + } +} +func (a *app) decode(w http.ResponseWriter, r *http.Request, target any) bool { + data, err := io.ReadAll(http.MaxBytesReader(w, r.Body, a.limits.maxBodyBytes)) + if err != nil { + var large *http.MaxBytesError + if errors.As(err, &large) { + a.metrics.status413.Add(1) + a.error(w, 413, "payload_too_large", "Размер запроса превышает лимит") + } else { + a.error(w, 400, "invalid_request", "Неверный JSON") + } + return false + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + var extra any + if !utf8.Valid(data) || decoder.Decode(target) != nil || decoder.Decode(&extra) != io.EOF { + a.error(w, 400, "invalid_request", "Неверный JSON") + return false + } + return true +} + +func (a *app) health(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/health/ready" && a.store.db.PingContext(r.Context()) != nil { + a.unavailable(w) + return + } + writeJSON(w, 200, map[string]string{"status": "ok"}) +} +func (a *app) metricsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; version=0.0.4") + _, _ = fmt.Fprint(w, a.metrics.snapshot()) + _, _ = fmt.Fprintf(w, "messenger_profile_cache_hits_total %d\nmessenger_profile_cache_misses_total %d\nmessenger_profile_cache_failures_total %d\nmessenger_db_connections %d\n", a.cacheMetrics.hits.Load(), a.cacheMetrics.misses.Load(), a.cacheMetrics.failures.Load(), a.store.db.Stats().OpenConnections) +} + +func (a *app) users(w http.ResponseWriter, r *http.Request) { + var input createUser + if !a.decode(w, r, &input) { + return + } + if !usernamePattern.MatchString(input.Username) || !validText(input.DisplayName, 100) || !validPassword(input.Password) { + a.error(w, 400, "invalid_request", "Неверные поля пользователя") + return + } + if !a.kdf(w) { + return + } + hash := hashPassword(input.Password) + <-a.kdfGate + user, err := scanUser(a.store.db.QueryRowContext(r.Context(), `INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4) RETURNING id,username,display_name,created_at`, newID(), input.Username, input.DisplayName, hash)) + if err != nil { + var pg *pq.Error + if errors.As(err, &pg) && pg.Code == "23505" { + a.error(w, 409, "conflict", "Имя пользователя уже занято") + } else { + a.unavailable(w) + } + return + } + writeJSON(w, 201, user) +} + +func (a *app) login(w http.ResponseWriter, r *http.Request) { + data, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1)) + if err != nil || len(data) != 0 { + a.error(w, 400, "invalid_request", "Вход не принимает тело запроса") + return + } + username, password, ok := r.BasicAuth() + if !ok || !usernamePattern.MatchString(username) || !validPassword(password) { + a.unauthorized(w, true) + return + } + var user models.User + var hash string + err = a.store.db.QueryRowContext(r.Context(), `SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=$1`, username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash) + missing := errors.Is(err, sql.ErrNoRows) + if err != nil && !missing { + a.unavailable(w) + return + } + if missing { + hash = a.dummyHash + } + if !a.kdf(w) { + return + } + valid := verifyPassword(hash, password) + <-a.kdfGate + if !valid || missing { + a.unauthorized(w, true) + return + } + data = make([]byte, 32) + if _, err = rand.Read(data); err != nil { + panic(err) + } + token := base64.RawURLEncoding.EncodeToString(data) + digest := sha256.Sum256([]byte(token)) + var expires time.Time + err = a.store.db.QueryRowContext(r.Context(), `INSERT INTO sessions(token_hash,user_id,expires_at) VALUES($1,$2,clock_timestamp()+$3*interval '1 second') RETURNING expires_at`, digest[:], user.ID, int(a.ttl/time.Second)).Scan(&expires) + if err != nil { + a.unavailable(w) + return + } + user.CreatedAt = user.CreatedAt.UTC() + writeJSON(w, 201, map[string]any{"token": token, "token_type": "Bearer", "expires_at": expires.UTC(), "user": user}) +} + +func bearer(r *http.Request) string { + parts := strings.Fields(r.Header.Get("Authorization")) + if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") || len(parts[1]) != 43 { + return "" + } + data, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil || len(data) != 32 { + return "" + } + return parts[1] +} +func (a *app) authenticate(w http.ResponseWriter, r *http.Request) (models.UUID, bool) { + token := bearer(r) + if token == "" { + a.unauthorized(w, false) + return "", false + } + digest := sha256.Sum256([]byte(token)) + var user models.UUID + err := a.store.db.QueryRowContext(r.Context(), `SELECT user_id FROM sessions WHERE token_hash=$1 AND expires_at>clock_timestamp()`, digest[:]).Scan(&user) + if errors.Is(err, sql.ErrNoRows) { + a.unauthorized(w, false) + return "", false + } + if err != nil { + a.unavailable(w) + return "", false + } + return user, true +} +func (a *app) logout(w http.ResponseWriter, r *http.Request) { + token := bearer(r) + if token == "" { + a.unauthorized(w, false) + return + } + digest := sha256.Sum256([]byte(token)) + var id models.UUID + err := a.store.db.QueryRowContext(r.Context(), `DELETE FROM sessions WHERE token_hash=$1 AND expires_at>clock_timestamp() RETURNING user_id`, digest[:]).Scan(&id) + if errors.Is(err, sql.ErrNoRows) { + a.unauthorized(w, false) + return + } + if err != nil { + a.unavailable(w) + return + } + w.WriteHeader(204) +} +func (a *app) currentUser(w http.ResponseWriter, r *http.Request) { + id, ok := a.authenticate(w, r) + if !ok { + return + } + u, err := a.store.user(r.Context(), id) + if err != nil { + a.unavailable(w) + return + } + writeJSON(w, 200, u) +} + +func (a *app) chats(w http.ResponseWriter, r *http.Request) { + user, ok := a.authenticate(w, r) + if !ok { + return + } + if r.Method == http.MethodPost { + var input createChat + if !a.decode(w, r, &input) { + return + } + if !validText(input.Title, 100) || len(input.MemberIDs) < 1 || len(input.MemberIDs) > 100 { + a.error(w, 400, "invalid_request", "Неверные поля чата") + return + } + seen := map[models.UUID]bool{} + for i, id := range input.MemberIDs { + id = models.UUID(strings.ToLower(string(id))) + if !isUUID(string(id)) || seen[id] { + a.error(w, 400, "invalid_request", "Неверные участники") + return + } + seen[id] = true + input.MemberIDs[i] = id + } + if !seen[user] { + a.error(w, 400, "invalid_request", "Создатель должен быть участником") + return + } + chat, err := a.store.createChat(r.Context(), input.Title, input.MemberIDs) + if err != nil { + var pg *pq.Error + if errors.As(err, &pg) && pg.Code == "23503" { + a.error(w, 400, "invalid_request", "Неизвестный участник") + } else { + a.unavailable(w) + } + return + } + writeJSON(w, 201, chat) + return + } + if r.Method != http.MethodGet { + a.error(w, 404, "not_found", "Объект не найден") + return + } + resource := "chats:" + string(user) + p, limit, ok := a.position(w, r, resource) + if !ok { + return + } + items, err := a.store.chats(r.Context(), user, p, limit) + if err != nil { + a.unavailable(w) + return + } + var next any + if len(items) > limit { + items = items[:limit] + last := items[len(items)-1] + next = encodeCursor(cursor{resource, last.CreatedAt, last.ID}) + } + writeJSON(w, 200, map[string]any{"items": items, "next_cursor": next}) +} + +func (a *app) chatResource(w http.ResponseWriter, r *http.Request) { + user, ok := a.authenticate(w, r) + if !ok { + return + } + parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/") + if !isUUID(parts[0]) { + a.error(w, 400, "invalid_request", "Неверный UUID") + return + } + chatID := models.UUID(strings.ToLower(parts[0])) + chat, err := a.store.chat(r.Context(), chatID, user) + if errors.Is(err, sql.ErrNoRows) { + a.error(w, 404, "not_found", "Объект не найден") + return + } + if err != nil { + a.unavailable(w) + return + } + if len(parts) == 1 && r.Method == http.MethodGet { + writeJSON(w, 200, chat) + return + } + if len(parts) != 2 || parts[1] != "messages" { + a.error(w, 404, "not_found", "Объект не найден") + return + } + if r.Method == http.MethodPost { + var input createMessage + if !a.decode(w, r, &input) { + return + } + if !validText(input.Text, 4000) { + a.error(w, 400, "invalid_request", "Неверный текст") + return + } + m := models.Message{ID: models.UUID(newID()), ChatID: chatID, SenderID: user, Text: input.Text} + err = a.store.db.QueryRowContext(r.Context(), `INSERT INTO messages(id,chat_id,sender_id,text) VALUES($1,$2,$3,$4) RETURNING created_at`, m.ID, chatID, user, input.Text).Scan(&m.CreatedAt) + if err != nil { + a.unavailable(w) + return + } + m.CreatedAt = m.CreatedAt.UTC() + a.metrics.accepted.Add(1) + writeJSON(w, 201, m) + return + } + if r.Method != http.MethodGet { + a.error(w, 404, "not_found", "Объект не найден") + return + } + resource := "messages:" + string(chatID) + p, limit, ok := a.position(w, r, resource) + if !ok { + return + } + items, err := a.store.messages(r.Context(), chatID, p, limit) + if err != nil { + a.unavailable(w) + return + } + var next any + if len(items) > limit { + items = items[:limit] + last := items[len(items)-1] + next = encodeCursor(cursor{resource, last.CreatedAt, last.ID}) + } + writeJSON(w, 200, map[string]any{"items": items, "next_cursor": next}) +} + +func (a *app) position(w http.ResponseWriter, r *http.Request, resource string) (cursor, int, bool) { + limit := 50 + var err error + if value := r.URL.Query().Get("limit"); value != "" { + limit, err = strconv.Atoi(value) + } + p := cursor{Resource: resource, CreatedAt: time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), ID: "00000000-0000-0000-0000-000000000000"} + if limit < 1 || limit > 100 || err != nil { + a.error(w, 400, "invalid_request", "Неверный limit") + return p, 0, false + } + if value := r.URL.Query().Get("cursor"); value != "" { + data, e := base64.RawURLEncoding.DecodeString(value) + if len(value) > 1024 || e != nil || json.Unmarshal(data, &p) != nil || p.Resource != resource || !isUUID(string(p.ID)) || p.CreatedAt.IsZero() { + a.error(w, 400, "invalid_request", "Неверный cursor") + return p, 0, false + } + } + return p, limit, true +} +func encodeCursor(p cursor) string { + data, _ := json.Marshal(p) + return base64.RawURLEncoding.EncodeToString(data) +} +func validText(value string, max int) bool { + return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= max +} +func isUUID(value string) bool { return uuidPattern.MatchString(value) } +func writeJSON(w http.ResponseWriter, status int, value any) { + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(value) +} +func newID() string { + data := make([]byte, 16) + if _, err := rand.Read(data); err != nil { + panic(err) + } + data[6] = (data[6] & 0x0f) | 0x40 + data[8] = (data[8] & 0x3f) | 0x80 + return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", data[:4], data[4:6], data[6:8], data[8:10], data[10:]) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..3bb4427 --- /dev/null +++ b/main_test.go @@ -0,0 +1,90 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func TestPasswordUnicodeAndNoTruncation(t *testing.T) { + password := strings.Repeat("🔑", 128) + if !validPassword(password) || validPassword(password+"x") || validPassword("too-short") || validPassword(strings.Repeat(" ", 12)) { + t.Fatal("password code point boundaries") + } + hash := hashPassword(password) + if !verifyPassword(hash, password) || verifyPassword(hash, password[:len(password)-4]+"x") || verifyPassword("corrupt", password) { + t.Fatal("password verification/truncation") + } +} + +func TestCursorScopeAndValidation(t *testing.T) { + a := &app{} + now := time.Now().UTC() + p := cursor{"messages:chat-a", now, "11111111-1111-4111-8111-111111111111"} + cases := []struct { + query string + valid bool + }{ + {"limit=1&cursor=" + encodeCursor(p), true}, + {"limit=100&cursor=" + encodeCursor(p), true}, + {"limit=0", false}, {"limit=101", false}, {"limit=1.2", false}, {"cursor=garbage", false}, + {"cursor=" + encodeCursor(cursor{"messages:chat-b", now, p.ID}), false}, + {"cursor=" + encodeCursor(cursor{p.Resource, now, "bad-id"}), false}, + {"cursor=" + encodeCursor(cursor{p.Resource, time.Time{}, p.ID}), false}, + } + for _, c := range cases { + r := httptest.NewRequest("GET", "/messages?"+c.query, nil) + w := httptest.NewRecorder() + _, _, ok := a.position(w, r, p.Resource) + if ok != c.valid { + t.Errorf("cursor query %s: valid=%v", c.query, ok) + } + } +} + +func TestStrictJSONAndBodyLimit(t *testing.T) { + a := &app{limits: limits{maxBodyBytes: 128}} + for _, data := range []string{`{"text":12}`, `{"text":"ok","sender_id":"spoof"}`, `{"text":"ok"} {}`, `{`, "{\"text\":\"\xff\"}"} { + w := httptest.NewRecorder() + var input createMessage + if a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(data)), &input) || w.Code != 400 { + t.Errorf("accepted invalid JSON %q", data) + } + } + w := httptest.NewRecorder() + var input createMessage + if a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(strings.Repeat("x", 129))), &input) || w.Code != 413 { + t.Fatal("body limit") + } +} + +func TestBearerDoesNotTrustUserHeader(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + r.Header.Set("X-User-Id", newID()) + if bearer(r) != "" { + t.Fatal("trusted X-User-Id") + } + r.Header.Set("Authorization", "Basic dXNlcjpwYXNz") + if bearer(r) != "" { + t.Fatal("accepted Basic") + } + r.Header.Set("Authorization", "Bearer "+strings.Repeat("A", 43)) + if bearer(r) == "" { + t.Fatal("rejected valid 256 bit token encoding") + } + r.Header.Set("Authorization", "Bearer "+strings.Repeat("!", 43)) + if bearer(r) != "" { + t.Fatal("accepted malformed token") + } +} + +func TestLogoutMissingTokenChallenge(t *testing.T) { + a := &app{} + w := httptest.NewRecorder() + a.logout(w, httptest.NewRequest(http.MethodDelete, "/api/v1/auth/sessions/current", nil)) + if w.Code != 401 || w.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` { + t.Fatal("logout challenge") + } +} diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 0000000..3304988 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,37 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id), + user_id uuid NOT NULL REFERENCES users(id), + position smallint NOT NULL CHECK (position BETWEEN 0 AND 99), + PRIMARY KEY (chat_id, user_id), + UNIQUE (chat_id, position) +); +CREATE INDEX chat_members_user ON chat_members(user_id, chat_id); +CREATE INDEX chats_history ON chats(created_at, id); +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + sender_id uuid NOT NULL, + text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) +); +CREATE INDEX messages_history ON messages(chat_id, created_at, id); +CREATE TABLE sessions ( + token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32), + user_id uuid NOT NULL REFERENCES users(id), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + expires_at timestamptz NOT NULL CHECK (expires_at > created_at) +); +CREATE INDEX sessions_expiry ON sessions(expires_at); diff --git a/models/models.go b/models/models.go new file mode 100644 index 0000000..93df35e --- /dev/null +++ b/models/models.go @@ -0,0 +1,27 @@ +package models + +import "time" + +type UUID string + +type User struct { + ID UUID `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt time.Time `json:"created_at"` +} + +type Chat struct { + ID UUID `json:"id"` + Title string `json:"title"` + MemberIDs []UUID `json:"member_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type Message struct { + ID UUID `json:"id"` + ChatID UUID `json:"chat_id"` + SenderID UUID `json:"sender_id"` + Text string `json:"text"` + CreatedAt time.Time `json:"created_at"` +} diff --git a/password.go b/password.go new file mode 100644 index 0000000..bbbe6e8 --- /dev/null +++ b/password.go @@ -0,0 +1,43 @@ +package main + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "fmt" + "strings" + "unicode/utf8" + + "golang.org/x/crypto/argon2" +) + +// Argon2id: 32 MiB, three iterations, two lanes. Limit simultaneous KDFs in app. +func hashPassword(password string) string { + salt := make([]byte, 16) + if _, err := rand.Read(salt); err != nil { + panic(err) + } + key := argon2.IDKey([]byte(password), salt, 3, 32*1024, 2, 32) + return fmt.Sprintf("$argon2id$v=19$m=32768,t=3,p=2$%s$%s", base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(key)) +} + +func verifyPassword(encoded, password string) bool { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" || parts[2] != "v=19" || parts[3] != "m=32768,t=3,p=2" { + return false + } + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil || len(salt) != 16 { + return false + } + key, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil || len(key) != 32 { + return false + } + actual := argon2.IDKey([]byte(password), salt, 3, 32*1024, 2, 32) + return subtle.ConstantTimeCompare(key, actual) == 1 +} + +func validPassword(password string) bool { + return utf8.ValidString(password) && strings.TrimSpace(password) != "" && utf8.RuneCountInString(password) >= 12 && utf8.RuneCountInString(password) <= 128 +} diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py new file mode 100644 index 0000000..8db856c --- /dev/null +++ b/scripts/bootstrap.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +"""Generate private LOCAL configuration once; never display credentials.""" +from pathlib import Path +import secrets + +ROOT = Path(__file__).resolve().parents[1] + +def main(): + env = ROOT / '.env' + folder = ROOT / 'secrets' + folder.mkdir(exist_ok=True) + if not env.exists(): + password = secrets.token_hex(24) + env.write_text('POSTGRES_PASSWORD='+password+'\nSESSION_TTL_SECONDS=3600\nHOST_PORT=8080\n', encoding='utf-8') + settings = dict(line.split('=', 1) for line in env.read_text(encoding='utf-8').splitlines() if line and not line.startswith('#') and '=' in line) + password = settings.get('POSTGRES_PASSWORD') + if not password or password == 'GENERATED_BY_BOOTSTRAP': + raise SystemExit('Use a local generated POSTGRES_PASSWORD in .env or remove an unused example .env before bootstrap.') + from urllib.parse import quote + url = 'postgres://messenger:'+quote(password, safe='')+'@postgres:5432/messenger?sslmode=disable&connect_timeout=2&statement_timeout=2000&lock_timeout=1000' + (folder / 'database_url').write_text(url+'\n', encoding='utf-8') + (folder / 'cache_url').write_text('redis://redis:6379/0\n', encoding='utf-8') + # File-backed Compose secrets keep host permissions on Linux. The private + # directory protects them on the host; 0644 lets the unprivileged API read + # individual bind-mounted files inside its container. + folder.chmod(0o700) + env.chmod(0o600) + for path in [folder / 'database_url', folder / 'cache_url']: + path.chmod(0o644) + print('Local configuration ready. Credentials are kept in ignored .env and secrets/.') + +if __name__ == '__main__': + main() diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 0000000..28b0c94 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,3 @@ +@echo off +cd /d "%~dp0.." +docker compose --env-file .env restart postgres redis api diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 0000000..7585581 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +docker compose --env-file .env restart postgres redis api diff --git a/scripts/wait_ready.py b/scripts/wait_ready.py new file mode 100644 index 0000000..d1175cb --- /dev/null +++ b/scripts/wait_ready.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +import argparse +import time +from urllib.request import urlopen + +p = argparse.ArgumentParser() +p.add_argument('--base-url', default='http://localhost:8080') +args = p.parse_args() +deadline = time.monotonic()+90 +while True: + try: + with urlopen(args.base_url+'/health/ready', timeout=3) as response: + if response.status == 200: + print('API ready') + break + except OSError: + pass + if time.monotonic() >= deadline: + raise SystemExit('API did not become ready in 90 seconds') + time.sleep(0.5) diff --git a/student-tests/integration.py b/student-tests/integration.py new file mode 100644 index 0000000..8312c95 --- /dev/null +++ b/student-tests/integration.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +"""Extra assertions on the student's local Compose stack; restores stopped services/TTL.""" +import base64 +from concurrent.futures import ThreadPoolExecutor +import json +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, check + +COMPOSE = ['docker', 'compose', '--env-file', '.env'] +BASE = os.environ.get('BASE_URL', 'http://127.0.0.1:8080') + +def command(args, **kwargs): + result = subprocess.run(args, cwd=ROOT, capture_output=True, **kwargs) + if result.returncode: + raise AssertionError('Infrastructure command failed: '+args[0]+' '+args[1]) + return result.stdout.decode().strip() + +def compose(*args, env=None): + return command(COMPOSE+list(args), env=env) + +def sql(query): + return command(COMPOSE+['exec', '-T', 'postgres', 'psql', '-X', '-v', 'ON_ERROR_STOP=1', '-U', 'messenger', '-d', 'messenger', '-At', '-c', query]) + +def ready(c): + deadline = time.monotonic()+90 + while time.monotonic()before_hits, 'Redis profile cache unused') + compose('stop','redis') + try: + start = time.monotonic() + c.call('GET','/api/v1/users/me',actor=alice,schema='User') + c.call('GET','/health/ready',schema='Health') + c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':alice['id']}) + check(metrics(c)['messenger_profile_cache_failures_total']>0, 'missing cache fallback metric') + results['redis_down'] = {'fallback':'PASS','seconds':round(time.monotonic()-start,3)} + finally: + compose('start','redis') + ready(c) + # A lost cache is harmless: existing sessions and confirmed messages are in PostgreSQL. + compose('exec','-T','redis','redis-cli','FLUSHDB') + c.call('GET','/api/v1/users/me',actor=alice,schema='User') + c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=bob,schema='MessagePage') + results['cache_loss'] = 'PASS: cache rebuild; original session/data valid' + compose('stop','postgres') + try: + start = time.monotonic() + c.call('GET','/api/v1/users/me',503,actor=alice) + c.call('GET','/health/ready',503) + c.call('GET','/health/live',schema='Health') + c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':alice['id']}) + elapsed = time.monotonic()-start + check(elapsed<7, 'database outage unbounded') + results['postgres_down'] = {'protected_request':503,'readiness':503,'liveness':200,'seconds':round(elapsed,3)} + finally: + compose('start','postgres') + ready(c) + # Re-running migrations on startup changes neither schema version nor data. + counts = sql("SELECT (SELECT count(*) FROM schema_migrations)||':'||(SELECT count(*) FROM users)||':'||(SELECT count(*) FROM messages)") + compose('restart','api');ready(c) + check(sql("SELECT (SELECT count(*) FROM schema_migrations)||':'||(SELECT count(*) FROM users)||':'||(SELECT count(*) FROM messages)")==counts, 'migration replay modified data') + results['migration_replay'] = 'PASS: same migration/user/message counts' + # Dedicated short TTL run. Restore normal configuration even on assertion failure. + short_env = dict(os.environ, SESSION_TTL_SECONDS='2') + try: + compose('up','-d','--no-deps','api',env=short_env);ready(c) + short = c.user('short') + c.call('GET','/api/v1/users/me',actor=short,schema='User') + time.sleep(2.2) + c.call('GET','/api/v1/users/me',401,actor=short) + results['expiry'] = 'PASS: SESSION_TTL_SECONDS=2 rejects existing token after expiry' + finally: + compose('up','-d','--no-deps','api');ready(c) + c.call('GET','/api/v1/users/me',401,actor=short) + c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice) + c.call('GET','/api/v1/users/me',401,actor=alice) + results['logout'] = 'PASS: revoked token rejected' + (ROOT/'evidence'/'integration.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') + print('PASS: races, rollback, KDF, cache hit/loss/outage, DB outage, migration replay, expiry, logout') + +if __name__=='__main__': + main() diff --git a/student-tests/profile_load.py b/student-tests/profile_load.py new file mode 100644 index 0000000..e26dc20 --- /dev/null +++ b/student-tests/profile_load.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""Public closed-loop load with contemporaneous Docker CPU/RAM samples.""" +import datetime as dt +import json +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] + +def main(): + samples = [] + ids = subprocess.run(['docker','compose','ps','-q','api','postgres','redis'],cwd=ROOT,capture_output=True,text=True,check=True).stdout.split() + started = time.monotonic() + proc = subprocess.Popen([sys.executable,'tests/load.py','--base-url','http://127.0.0.1:8080','--duration','30','--concurrency','4','--output','evidence/load.json'],cwd=ROOT,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,encoding='utf-8',errors='replace') + while proc.poll() is None: + result = subprocess.run(['docker','stats','--no-stream','--format','{{json .}}']+ids,cwd=ROOT,capture_output=True,text=True) + samples.append({'seconds':round(time.monotonic()-started,3),'containers':[json.loads(line) for line in result.stdout.splitlines()]}) + time.sleep(3) + out,err = proc.communicate() + evidence = {'sampled_at_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'command':'python tests/load.py --base-url http://127.0.0.1:8080 --duration 30 --concurrency 4 --output evidence/load.json', 'exit_code':proc.returncode,'stdout':out.strip(),'stderr':err.strip(),'samples':samples} + (ROOT/'evidence'/'resources.json').write_text(json.dumps(evidence,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') + print(out.strip()) + if proc.returncode: + print(err) + raise SystemExit(proc.returncode) + +if __name__=='__main__': + main() diff --git a/student-tests/research.py b/student-tests/research.py new file mode 100644 index 0000000..c844df1 --- /dev/null +++ b/student-tests/research.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +"""Track 2: isolated 100k dataset, index/write comparison, dump -> NEW DB -> checks. + +Uses only synthetic data in uniquely named research databases; never changes app tables. +""" +import datetime as dt +import json +from pathlib import Path +import platform +import re +import statistics +import subprocess +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +COMPOSE = ['docker', 'compose', '--env-file', '.env'] +USER = '11111111-1111-4111-8111-111111111111' +HOT = '22222222-2222-4222-8222-222222222222' + +def command(args, data=None): + result = subprocess.run(args, cwd=ROOT, input=data, capture_output=True) + if result.returncode: + raise RuntimeError('Research infrastructure command failed: '+args[0]+' '+args[1]+'; '+result.stderr.decode(errors='replace')[:500]) + return result.stdout + +def sql(database, query, service='postgres'): + return command(COMPOSE+['exec','-T',service,'psql','-X','-v','ON_ERROR_STOP=1','-U','messenger','-d',database,'-At','-c',query]).decode().strip() + +def plan(database, query, rollback=False): + query = 'EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) '+query + if rollback: + query = 'BEGIN; '+query+'; ROLLBACK;' + output = sql(database, query) + start = output.index('[') + value, _ = json.JSONDecoder().raw_decode(output[start:]) + return value[0] + +def fingerprint(database, service='postgres'): + query = """SELECT json_build_object( + 'users',(SELECT count(*) FROM users), + 'chats',(SELECT count(*) FROM chats), + 'members',(SELECT count(*) FROM chat_members), + 'messages',(SELECT count(*) FROM messages), + 'content_digest',(SELECT md5(string_agg(md5(row(id,chat_id,sender_id,text,created_at)::text),'' ORDER BY id)) FROM messages), + 'selected',(SELECT json_agg(x) FROM (SELECT * FROM messages ORDER BY id LIMIT 10) x))""" + return json.loads(sql(database, query, service)) + +def main(): + suffix = uuid.uuid4().hex[:12] + database, restored = 'research_'+suffix, 'research_restore_'+suffix + created = [] + results = {'generated_at_utc':dt.datetime.now(dt.timezone.utc).isoformat(), + 'hypothesis':'Index (chat_id,created_at,id) bounds keyset page work independently of total chat history, at a measurable write/index-space cost.', + 'host':platform.platform(),'limits':{'postgres_cpu':1,'postgres_memory_mb':512}, + 'messages':100000,'distribution':[80000,15000]+[625]*8,'warmup_reads':2,'measured_repetitions':5,'variants':{}} + try: + command(COMPOSE+['--profile','research','up','-d','--wait','restore']) + for name, service, admin in [(database,'postgres','messenger'),(restored,'restore','restore_validation')]: + assert re.fullmatch(r'research_(restore_)?[a-f0-9]{12}',name) + sql(admin,'CREATE DATABASE '+name,service) + created.append((name, service, admin)) + sql(database,(ROOT/'migrations'/'001_initial.sql').read_text(encoding='utf-8')) + sql(database,"INSERT INTO users(id,username,display_name,password_hash) VALUES('"+USER+"','research_user','Synthetic researcher','research-fixture-not-a-login')") + for chat_index, count in enumerate(results['distribution']): + chat_id = HOT if chat_index==0 else str(uuid.uuid5(uuid.NAMESPACE_DNS,'lab2-research-chat-'+str(chat_index))) + sql(database,f"INSERT INTO chats(id,title) VALUES('{chat_id}','Research {chat_index}'); INSERT INTO chat_members(chat_id,user_id,position) VALUES('{chat_id}','{USER}',0)") + sql(database,f"INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('seed-{chat_index}-'||n)::uuid,'{chat_id}','{USER}',repeat('x',{200 if chat_index==0 else 40}),'2026-01-01T00:00:00Z'::timestamptz+n*interval '1 second' FROM generate_series(1,{count}) n") + sql(database,'ANALYZE messages') + results['postgres_version'] = sql(database,'SELECT version()') + variants = {'chat_only':'CREATE INDEX research_chat_only ON messages(chat_id)', + 'keyset':'CREATE INDEX messages_history ON messages(chat_id,created_at,id)'} + for variant,ddl in variants.items(): + sql(database,'DROP INDEX IF EXISTS messages_history; DROP INDEX IF EXISTS research_chat_only; '+ddl+'; ANALYZE messages') + index_name = 'research_chat_only' if variant=='chat_only' else 'messages_history' + measurements = {'index_bytes':int(sql(database,"SELECT pg_relation_size('"+index_name+"')")), 'pages':{}} + for position in [0,40000,79000]: + query = f"SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id='{HOT}' AND (created_at,id)>('2026-01-01T00:00:00Z'::timestamptz+{position}*interval '1 second','ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid) ORDER BY created_at,id LIMIT 51" + for _ in range(2): + plan(database,query) + plans = [plan(database,query) for _ in range(5)] + measurements['pages'][str(position)] = {'execution_ms':[p['Execution Time'] for p in plans], + 'median_ms':statistics.median(p['Execution Time'] for p in plans),'representative_plan':plans[2]} + writes = [] + for repetition in range(5): + # Roll back after EACH measured insertion, so both variants see the same logical dataset. + query = f"INSERT INTO messages(id,chat_id,sender_id,text,created_at) SELECT md5('write-{variant}-{repetition}-'||n)::uuid,'{HOT}','{USER}',repeat('w',200),'2026-02-01T00:00:00Z'::timestamptz+n*interval '1 second' FROM generate_series(1,2000) n" + writes.append(plan(database,query,rollback=True)) + measurements['write_2000'] = {'execution_ms':[p['Execution Time'] for p in writes], + 'median_ms':statistics.median(p['Execution Time'] for p in writes),'representative_plan':writes[2]} + results['variants'][variant] = measurements + # The selected production index remains in the restored schema. + source = fingerprint(database) + check_count = source['messages']==100000 and source['chats']==10 + if not check_count: + raise AssertionError('Research dataset count mismatch') + folder = ROOT/'backups' + folder.mkdir(exist_ok=True) + backup = folder/(database+'.dump') + start = time.monotonic() + dump = command(COMPOSE+['exec','-T','postgres','pg_dump','-U','messenger','-d',database,'-Fc']) + backup.write_bytes(dump) + backup_seconds = time.monotonic()-start + start = time.monotonic() + command(COMPOSE+['exec','-T','restore','pg_restore','--exit-on-error','--no-owner','-U','messenger','-d',restored], data=dump) + restore_seconds = time.monotonic()-start + target = fingerprint(restored,'restore') + if source!=target: + raise AssertionError('Restored data differs from snapshot') + results['backup_restore'] = {'backup_bytes':len(dump),'backup_seconds':round(backup_seconds,3), + 'restore_seconds':round(restore_seconds,3),'source':source,'restored':target,'equal':True, + 'scope':'empty database on separate PostgreSQL container and separate named volume; all counts, ordered digest of every message, and 10 complete messages match'} + results['limitations'] = ['Warm buffer cache, 5 sequential trials, local single-host containers; no claim of max production throughput.', + 'Write timing is EXPLAIN ANALYZE of 2000 inserts followed by rollback; includes constraints/triggers and excludes durable COMMIT latency. Aborted inserts can leave dead tuples.', + 'Logical backup captures the dump snapshot. Writes committed after that snapshot are not included; no WAL archiving/PITR. Restore time excludes operator detection and provisioning.', + 'Research fixtures use SQL bulk insert; HTTP validation/authentication cost is measured separately by the public load tool.'] + (ROOT/'evidence'/'research.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') + print('PASS: 100000 messages, two indexes/read-write measurements, backup restored to new database with matching content') + print(json.dumps({'read_middle_ms':{name:value['pages']['40000']['median_ms'] for name,value in results['variants'].items()},'restore_seconds':results['backup_restore']['restore_seconds']},ensure_ascii=False)) + finally: + for name, service, admin in reversed(created): + # Only drop names generated AND created by this invocation, never application DB. + assert re.fullmatch(r'research_(restore_)?[a-f0-9]{12}',name) + sql(admin,'DROP DATABASE '+name+' WITH (FORCE)', service) + command(COMPOSE+['--profile','research','stop','restore']) + +if __name__=='__main__': + main() diff --git a/student-tests/verify.py b/student-tests/verify.py new file mode 100644 index 0000000..667496d --- /dev/null +++ b/student-tests/verify.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Run local checks serially and keep sanitized output + restart scope evidence.""" +import datetime as dt +import json +import os +from pathlib import Path +import platform +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +COMPOSE = ['docker','compose','--env-file','.env'] + +def command(args): + result = subprocess.run(args,cwd=ROOT,capture_output=True,text=True,encoding='utf-8',errors='replace') + return result + +def snapshot(): + ids = command(COMPOSE+['ps','-q','api','postgres','redis']) + if ids.returncode: + raise RuntimeError('Cannot inspect local lab stack') + result = command(['docker','inspect','--format','{{json .Name}} {{json .Id}} {{json .State.StartedAt}} {{json .State.Status}}']+ids.stdout.split()) + if result.returncode: + raise RuntimeError('Cannot inspect restart scope') + # Narrow format intentionally excludes environment/credentials. + return result.stdout.strip().splitlines() + +def main(): + results = {'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'host':platform.platform(),'base_url':'http://127.0.0.1:8080','checks':[]} + hook = 'scripts/restart.cmd' if os.name=='nt' else 'scripts/restart.sh' + if os.name!='nt': + (ROOT/hook).chmod(0o755) + commands = [ + [sys.executable,'scripts/check_contract.py'], + [sys.executable,'tests/smoke.py','--base-url',results['base_url']], + [sys.executable,'tests/resilience.py','--mode','persistence','--action-script',hook,'--base-url',results['base_url']], + [sys.executable,'student-tests/integration.py'], + [sys.executable,'student-tests/research.py'], + [sys.executable,'tests/load.py','--base-url',results['base_url'],'--duration','30','--concurrency','4','--output','evidence/load.json'], + ] + try: + for args in commands: + persistence = 'tests/resilience.py' in args + if persistence: + results['restart_before'] = snapshot() + start = time.monotonic() + completed = command(args) + entry = {'command':' '.join(args[1:]),'exit_code':completed.returncode,'seconds':round(time.monotonic()-start,3),'stdout':completed.stdout.strip(),'stderr':completed.stderr.strip()} + results['checks'].append(entry) + print(entry['stdout'],flush=True) + if persistence: + results['restart_after'] = snapshot() + if completed.returncode==0: + before = {line.split()[0]:line.split()[2] for line in results['restart_before']} + after = {line.split()[0]:line.split()[2] for line in results['restart_after']} + if before.keys()!=after.keys() or any(before[key]==after[key] for key in before): + raise AssertionError('Not every API/state-store process restarted') + results['restart_scope_verified'] = True + if completed.returncode: + print(entry['stderr'],flush=True) + raise SystemExit(completed.returncode) + finally: + (ROOT/'evidence'/'verification.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n',encoding='utf-8') + +if __name__=='__main__': + main()