From 400e62ffdc310458560bf953abbf5bcf9186eae5 Mon Sep 17 00:00:00 2001 From: mayorovasonya <112+mayorovasonya@noreply.gitea.privcloud.tech> Date: Fri, 23 Oct 2026 11:13:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A0=202=20=E2=80=94=20=D0=91=D0=92?= =?UTF-8?q?=D0=A22301=20=E2=80=94=20=D0=9C=D0=B0=D0=B9=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=20=E2=80=94=20=D0=BD=D0=B0=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 27 +- .github/workflows/application.yml | 21 + .gitignore | 1 + Dockerfile | 13 + MR_DESCRIPTION.md | 20 + Makefile | 10 + REPORT.md | 162 +- compose.yaml | 57 + evidence/build.txt | 78 + evidence/check.txt | 2 + evidence/clean-clone.txt | 130 ++ evidence/dependencies.json | 9 + evidence/dependencies.txt | 15 + evidence/final-api.txt | 18 + evidence/index-study.json | 3018 +++++++++++++++++++++++++++++ evidence/kdf-regression.json | 8 + evidence/persistence.txt | 3 + evidence/research.txt | 6 + evidence/restart-after.json | 27 + evidence/restart-before.json | 27 + evidence/runtime.json | 91 + evidence/smoke.txt | 2 + evidence/student-tests.txt | 13 + evidence/validation.txt | 2 + migrations/001_initial.sql | 42 + requirements.txt | 4 + scripts/bootstrap.py | 28 + scripts/restart.sh | 10 + scripts/snapshot.py | 11 + src/common.py | 98 + src/postgres.py | 261 +++ src/server.py | 219 +++ student-tests/dependencies.py | 92 + student-tests/index_study.py | 113 ++ student-tests/research.py | 52 + student-tests/test_api.py | 98 + 36 files changed, 4751 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/application.yml create mode 100644 Dockerfile create mode 100644 MR_DESCRIPTION.md create mode 100644 compose.yaml create mode 100644 evidence/build.txt create mode 100644 evidence/check.txt create mode 100644 evidence/clean-clone.txt create mode 100644 evidence/dependencies.json create mode 100644 evidence/dependencies.txt create mode 100644 evidence/final-api.txt create mode 100644 evidence/index-study.json create mode 100644 evidence/kdf-regression.json create mode 100644 evidence/persistence.txt create mode 100644 evidence/research.txt create mode 100644 evidence/restart-after.json create mode 100644 evidence/restart-before.json create mode 100644 evidence/runtime.json create mode 100644 evidence/smoke.txt create mode 100644 evidence/student-tests.txt create mode 100644 evidence/validation.txt create mode 100644 migrations/001_initial.sql create mode 100644 requirements.txt create mode 100644 scripts/bootstrap.py create mode 100755 scripts/restart.sh create mode 100644 scripts/snapshot.py create mode 100644 src/common.py create mode 100644 src/postgres.py create mode 100644 src/server.py create mode 100644 student-tests/dependencies.py create mode 100644 student-tests/index_study.py create mode 100644 student-tests/research.py create mode 100644 student-tests/test_api.py diff --git a/.dockerignore b/.dockerignore index bc16e556..07f6dfaa 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,20 +1,11 @@ .git -.github -.env -.env.* -!.env.example -secrets -certs -volumes -data -backups -*.key -*.pem -*.p12 -*.pfx -*.jks -.venv -__pycache__ -node_modules +.env* +.venv* +**/__pycache__ +tests +student-tests evidence -.DS_Store +scripts +secrets +backups +*.log diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 00000000..1f5e337b --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,21 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read +jobs: + application: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: | + python scripts/bootstrap.py + docker compose up -d --build --wait + make check test test-student + make test-persistence ACTION_SCRIPT=scripts/restart.sh + make test-dependencies + make research + - if: always() + run: docker compose down diff --git a/.gitignore b/.gitignore index b5a71896..7abbc276 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ data/ backups/ # Runtime/build artifacts .venv/ +.venv*/ __pycache__/ *.py[cod] node_modules/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5e287bd9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 APP_HOST=0.0.0.0 APP_PORT=8080 MALLOC_ARENA_MAX=2 +WORKDIR /app +RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt +COPY src/ ./src/ +COPY migrations/ ./migrations/ +USER 10001:10001 +EXPOSE 8080 +HEALTHCHECK --interval=5s --timeout=3s --start-period=15s --retries=3 CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).read()"] +STOPSIGNAL SIGTERM +CMD ["python", "src/server.py"] diff --git a/MR_DESCRIPTION.md b/MR_DESCRIPTION.md new file mode 100644 index 00000000..1f5095f5 --- /dev/null +++ b/MR_DESCRIPTION.md @@ -0,0 +1,20 @@ +ЛР 2 — БВТ2301 — Майорова — на 5 + +Автор: Майорова Софья, БВТ2301. Целевая оценка 5, трек 2 — план запроса и восстановление. + +Собственная реализация lab1 перенесена на PostgreSQL. Добавлены версионируемая миграция, PK/FK/UNIQUE, атомарное создание чата, ограниченный connection pool и таймауты. Регистрация использует scrypt, Basic выдаёт opaque Bearer-сессию; сессии с expiry и logout хранятся в PostgreSQL. Redis реально кэширует публичные профили с TTL; отказ кэша не отменяет проверку сессии в БД и не мешает работе. + +Проверки: `make check validate`, `make test`, восемь собственных тестов, сохранность данных и прежнего токена после рестарта API/PostgreSQL/Redis, expiry=2 с, logout, неправильный пароль, гонка username, rollback, отказы Redis/PostgreSQL и восстановление. Найденный собственными тестами OOM при параллельном scrypt исправлен ограничением KDF, повторный тест проходит при прежнем лимите 256 MiB. + +Исследование на 100000 сообщений в десяти чатах разного размера: два индекса, EXPLAIN ANALYZE BUFFERS, пять повторов чтения, три измерения записи с COMMIT и WAL. Backup восстановлен в другой PostgreSQL-контейнер на новом volume; количества, пять выбранных сообщений и SHA-256 всей истории совпали. Полные планы, результаты и ограничения — в `evidence/index-study.json` и `REPORT.md`. + +Запуск из чистого clone: `python3 scripts/bootstrap.py`, `docker compose up -d --build --wait`, `make check test test-student`, `make test-persistence ACTION_SCRIPT=scripts/restart.sh`, `make test-dependencies`, `make research`. + +- [x] Минимум предыдущей работы сохранён с переходом на сессии. +- [x] Публичные контракты и tests не изменены. +- [x] Фактический вывод проверок приложен. +- [x] Полный restart, ошибки зависимостей и трек 5 проверены. +- [x] Пароли, tokens, volumes и backup находятся вне Git. +- [x] README задания сохранён, REPORT.md заполнен. + +Ограничения: один PostgreSQL; нет HA/PITR; Redis — disposable cache. Локальный HTTP предназначен для синтетического стенда. Codex использован при реализации, экспериментах и составлении отчёта. diff --git a/Makefile b/Makefile index 9ecb223e..9680413e 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,16 @@ test: load: $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) +.PHONY: test-student test-dependencies research +test-student: + BASE_URL="$(BASE_URL)" $(PYTHON) -m unittest discover -s student-tests -p test_api.py -v + +test-dependencies: + $(PYTHON) student-tests/dependencies.py + +research: + $(PYTHON) student-tests/research.py + test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) diff --git a/REPORT.md b/REPORT.md index e42f2b05..f9b8f893 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,161 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +Автор: **Майорова Софья, БВТ2301**. Целевая оценка: **5**, трек 2 «План запроса и восстановление». + +Ветка: `solution/BVT2301-Mayorova`. Заголовок МР: `ЛР 2 — БВТ2301 — Майорова — на 5`. +Текущий commit: `git log -1 --oneline`. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Engine с Compose v2+ и Linux-контейнерами, Python 3.9+, make. Исходники, собственные тесты и HTTP-слой перенесены из своей lab1; контракты и публичные тесты взяты из skeleton **lab2**, без замены на старые версии. -## Архитектура и решения +```sh +python3 scripts/bootstrap.py +docker compose config --quiet +docker compose up -d --build --wait +make check test test-student +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-dependencies +make research +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Bootstrap один раз создаёт `.env` и случайные локальные secrets; повтор не перезаписывает пароль или БД. Никаких вручную установленных PostgreSQL/Redis не требуется. API сам применяет версии миграций при запуске, до открытия HTTP-порта. Для нового порта задайте `APP_PORT=18086 docker compose up -d --wait` и передайте соответствующий `BASE_URL` тестам. Сценарий expiry временно занимает 18083, исследование не публикует порт PostgreSQL. + +Полная валидация OpenAPI: + +```sh +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python +``` + +Остановка с сохранением данных: `docker compose down`; повторный запуск — `docker compose up -d --wait`. +**Намеренное удаление всех учебных данных основного стенда**: `docker compose down -v`. Это не команда restart/persistence-проверки. Файлы backup сохраняются отдельно в игнорируемом каталоге `backups/`. +Исследование дополнительно оставляет отдельный остановленный PostgreSQL и собственный том восстановления; их имена записаны в `evidence/index-study.json`. Для их намеренного удаления используйте `docker rm ` и `docker volume rm ` только после проверки этих имён. Не выполняйте общий `docker system prune`. + +## Архитектура, схема и границы транзакций + +`Клиент → API → PostgreSQL`; Redis кэширует неизменяемые публичные профили `User`. PostgreSQL — источник истины для пользователей, чатов, участников, сообщений и сессий. История сообщений никогда не берётся из Redis: успешный COMMIT виден следующему чтению. Удаления/редактирования профилей в контракте нет, поэтому cache-aside профиля не нарушает read-after-write истории. + +Схема в `migrations/001_initial.sql`: + +- `users`: UUID PK, UNIQUE username, display_name, scrypt hash, server timestamp. +- `chats`: UUID PK, title, created_at, last_message_at. +- `chat_members`: составной PK (chat_id, user_id), FK в chats/users, обратный индекс (user_id, chat_id). +- `messages`: UUID PK, FK в chats/users, составной FK (chat_id, sender_id) в членство; индекс (chat_id, created_at, id). +- `sessions`: PK SHA-256 случайного токена, FK user_id, expiry и индекс expiry. SHA-256 здесь индексирует высокоэнтропийный токен, **не пароль**. +- `schema_migrations`: применённые имена версий. Advisory transaction lock исключает одновременное применение миграции несколькими API. + +Создание чата, проверка всех участников и вставка членства — одна транзакция. Ошибка неизвестного участника после INSERT чата откатывает весь составной объект; собственный тест проверяет отсутствие даже невидимого orphan через SQL COUNT. +Запись сообщения и увеличение `last_message_at` — одна транзакция. UPDATE строки чата сериализует timestamp внутри чата; сервер выбирает max(часы, предыдущий timestamp + 1 мкс). Это сохраняет порядок keyset при конкурентных отправках и переводе часов назад. Уровень изоляции — PostgreSQL READ COMMITTED; snapshot между HTTP-страницами не обещается. + +SQL-параметры передаются отдельно от текста запросов. Connection pool: min=1, max=8, ожидание 2 с; connect timeout 2 с, statement timeout 2 с, lock timeout 1,5 с. Ошибки зависимости ограничены JSON 503, внутренний SQL не возвращается клиенту. Чужие объекты скрыты 404. Readiness зависит от PostgreSQL; liveness — только от процесса. + +## Аутентификация и Redis + +Регистрация принимает 12–128 Unicode code points пароля, включая двоеточие и emoji. KDF — библиотечный `hashlib.scrypt`: N=16384, r=8, p=1, 16-байтовая случайная соль, dklen=32. Хранятся алгоритм/параметры/соль/hash. Сравнение через `hmac.compare_digest`; для неизвестного пользователя при допустимом формате credentials вычисляется dummy hash. Неверные credentials возвращают одинаковый 401 с Basic challenge. Профили не содержат паролей/hash. + +Basic разрешён только на `POST /auth/sessions`, без JSON body. Токен — `secrets.token_urlsafe(32)`, 256 бит энтропии. На бизнес-endpoint принимается Bearer; `X-User-Id` не меняет владельца и не даёт доступ. На **каждом** защищённом запросе PostgreSQL проверяет действительность сессии и expiry. Logout удаляет сессию, поэтому даже тёплый кэш профиля не даёт повторного доступа. Ещё действующие сессии переживают рестарт, TTL не продлевается. + +Redis: cache-aside `user:`, TTL 60 с, лимит 32 MiB и allkeys-lru. При miss профиль читается из PostgreSQL; Redis не хранит bearer token и не определяет аутентификацию. При недоступности Redis с таймаутом 150 мс запрос безопасно идёт в PostgreSQL. После рестарта кэш прогревается повторно. Его persistence намеренно выключен: обязательных данных в Redis нет. Если в будущем появится редактирование User, потребуется инвалидация `user:` после COMMIT. + +Собственный конкурентный тест выявил OOM при 10 параллельных scrypt в контейнере 256 MiB (EXIT 137). Исправлено ограничением KDF до двух одновременных вычислений и `MALLOC_ARENA_MAX=2`; ожидание KDF ограничено 1,5 с, далее 503. Тот же тест под **тем же** лимитом памяти теперь проходит: один 201, девять 409. Доказательства — `evidence/kdf-regression.json`, `evidence/student-tests.txt`. + +## Компоненты и конфигурация + +| Роль / service | Версия | Health / readiness | Хранение / сеть | +| --- | --- | --- | --- | +| API / `api` | Python 3.12.8, digest в Dockerfile | `/health/live`, `/health/ready`, Docker healthcheck | UID 10001; без локального состояния; только 127.0.0.1:8080 опубликован | +| PostgreSQL / `postgres` | 17.2-bookworm, digest в compose.yaml | pg_isready и SQL SELECT 1 через API | именованный `postgres_data`, порт только внутри Compose | +| Redis / `redis` | 7.4.1-bookworm, digest в compose.yaml | redis-cli ping; не критичен для readiness API | только внутренний порт; кэш disposable | + +API ограничен 1 CPU / 256 MiB; PostgreSQL — 1 CPU / 512 MiB; Redis — 0,5 CPU / 64 MiB. Окружение измерения: macOS arm64, Linux Colima VM 4 vCPU / 4 GiB. Python-зависимости и транзитивный typing-extensions закреплены в `requirements.txt`. + +`APP_HOST=0.0.0.0` и внутренний APP_PORT=8080 заданы Dockerfile. `APP_PORT` из `.env` задаёт опубликованный порт Compose. `APP_ENV` — информационная метка skeleton. `SESSION_TTL_SECONDS` реально передаётся API, default 3600; нормальные smoke/persistence выполняются с TTL ≥300. `DATABASE_URL_FILE`/`CACHE_URL_FILE` указывают на Docker secrets. `SESSION_STORE=postgres` из исходного примера соответствует выбранному решению; переключение backend по этой переменной не реализовано. + +`secrets/` имеет host mode 0700; пароль PostgreSQL — 0600. Файлы URL имеют 0644 внутри защищённого host-каталога, чтобы Compose мог смонтировать их для UID 10001. Secrets, `.env`, volumes и backups исключены из Git и build context. В публичном отчёте нет токенов и паролей. Локальный учебный HTTP и доверенная внутренняя сеть не заменяют TLS и ограничение DB-role для реального сервиса. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | +Дополнительно выполнен чистый локальный `git clone` сдаваемого решения: сборка из клона и все применимые smoke/собственные тесты прошли. Для lab2 стенд создан на новом пустом volume, миграция применена один раз и persistence-сценарий пройден с прежним токеном. Вывод: `evidence/clean-clone.txt`. + +| Критерий | Команда / сценарий | Реальный результат | Файл | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Offline / OpenAPI | `make check`, `make validate` | PASS | `evidence/check.txt`, `evidence/validation.txt` | +| Обязательный API | `make test` | PASS lab2 | `evidence/smoke.txt` | +| Негативные сценарии и Unicode | `make test-student` | 8 тестов, все PASS | `evidence/student-tests.txt` | +| Конкурентные сообщения / cursor | тот же набор, 100 POST / 10 клиентов | 100 уникальных ID, без потерь | тот же файл | +| Гонка username | тот же набор | ровно один 201, девять 409 | тот же файл | +| FK/UNIQUE/миграции с нуля | первый `compose up`, повторные старты | таблицы созданы; версия 001 применена один раз | `migrations/001_initial.sql`, `evidence/runtime.json` | +| Атомарность составного чата | `make test-dependencies`, SQL COUNT до/после | rollback, количество chats не изменилось | `evidence/dependencies.json` | +| Сохранность данных и прежнего токена | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS после рестарта API + PostgreSQL + Redis | `evidence/persistence.txt` | +| Реальность restart hook | сравнение StartedAt и volumes всех трёх services | процессы новые по времени старта, том PostgreSQL тот же | `evidence/restart-before.json`, `evidence/restart-after.json` | +| Реальный Redis | DEL профиля, затем GET /users/me и Redis TTL | ключ реально заполнен, TTL 60 с | `evidence/dependencies.json` | +| Expiry | отдельный API, TTL=2, ожидание 2,2 с | прежний Bearer → 401 | тот же файл | +| Logout / неверный пароль / Basic только на login | smoke + собственный тест | ожидаемые 401/204 | smoke и student-tests | +| Отказ Redis | остановка redis, чтение/запись с прежним токеном | работа продолжается, поддельный Bearer → 401 | dependencies | +| Отказ PostgreSQL | остановка postgres | me/ready → 503; live → 200; после возврата прежний токен работает | dependencies | +| Индекс ≥10000 сообщений | EXPLAIN ANALYZE BUFFERS на 100000 | keyset использует составной индекс | `evidence/index-study.json` | +| Трек 5: ≥2 индекса, запись | `make research` | 100000 сообщений, 2 индекса, 5 повторов чтения и 3 записи | тот же файл | +| Трек 5: backup → новое хранилище | pg_dump/pg_restore в отдельный контейнер и новый volume | count, 5 образцов и SHA-256 всей истории совпадают | тот же файл, `evidence/research.txt` | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +## Исследование индекса и восстановления -## Эксперименты и отказы +Гипотеза: индекс только по chat_id заставляет отфильтровывать/сортировать значительную часть большого чата при глубоком cursor. B-tree `(chat_id, created_at, id)` позволит читать limit+1 сразу из нужного диапазона; за это придётся платить размером индекса и записью WAL. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +`student-tests/index_study.py` создаёт **отдельную синтетическую БД**, не заполняет рабочие чаты тестового API. Размеры десяти чатов: **70000, 20000, 5000, 2000, 1000, 800, 500, 300, 200, 200** — суммарно 100000. Текст — 64 символа, UUID случайные, timestamp с шагом 1 мкс. Сравниваются `messages_history(chat_id)` и `messages_history(chat_id, created_at, id)`; PK(id) и прочие ограничения одинаковы. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +Запрос совпадает с API keyset: фильтр chat_id и `(created_at,id)>cursor`, ORDER BY created_at,id, LIMIT 51. Для чатов 70000, 20000 и 200 cursor ставится на 80% истории. Перед замером — ANALYZE и один прогрев; затем пять `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)`, без отключения seqscan и других подсказок планировщику. Сохранены все планы, включая buffers. Поочерёдно меняется один индекс. + +| Индекс | Сообщений в чате | Медиана Execution Time, мс | +| --- | --- | --- | +| chat_only | 70000 | 26.665 | +| chat_only | 20000 | 7.168 | +| chat_only | 200 | 0.073 | +| composite | 70000 | 0.057 | +| composite | 20000 | 0.072 | +| composite | 200 | 0.065 | + +Для большого чата простой индекс приводит к полному/широкому проходу и сортировке, составной — к ограниченному индексному диапазону. Для маленького чата разница близка к шуму. Сам API использует составной индекс из миграции. + +Стоимость записи: для каждого варианта три отдельных COPY по 1000 новых сообщений в большой чат, время от COPY до завершения COMMIT; UUID подготовлены до таймера. После каждого повтора добавленные сообщения удаляются и фиксируются отдельно; cleanup не входит в измерение. WAL — разница `pg_current_wal_insert_lsn()` до/после. Все исходные измерения сохранены. + +| Индекс | Размер индекса, байт | Медиана COPY+COMMIT 1000 строк, мс | Медиана WAL, байт | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| chat_only | 753664 | 88.455 | 245480 | +| composite | 6127616 | 329.823 | 322672 | -## Ограничения и следующие шаги +Большой индекс ускоряет чтение, но увеличивает занимаемое место и WAL. Время записи в этих коротких последовательных прогонах заметно колеблется, на него влияют checkpoint, кэш, COPY, CPU quota и фоновые процессы; три повтора не дают универсального коэффициента замедления. Порядок вариантов фиксирован, cold-cache I/O не исследовался. Это анализ query plan и компромисса индекса, не измерение HTTP RPS всего приложения. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +### Backup и новое хранилище + +`student-tests/research.py`: + +1. Запускает исследование в отдельной БД; делает `pg_dump -Fc` согласованного снимка. +2. Создаёт **другой PostgreSQL-контейнер с новым именованным volume**, тем же закреплённым образом и собственным каталогом данных; порты хоста не публикуются. +3. Выполняет `pg_restore --exit-on-error` в новую пустую БД. +4. Сравнивает количества users/chats/chat_members/messages/sessions, пять выбранных сообщений и SHA-256 **всех 100000 сообщений** в порядке UUID, включая поля сообщений. В research fixture sessions=0; сохранность действующей сессии отдельно доказана persistence-тестом. +5. Сохраняет JSON-сводку, останавливает restore-контейнер; backup и новый volume оставляет для проверки. + +Размер dump: 2655979 байт. Dump: 1.102 с; bootstrap нового PostgreSQL: 2.588 с; собственно pg_restore: 1.742 с. Bootstrap + восстановление: 4.330 с, **без времени последующей проверки checksum**. Это время локального учебного набора, не RTO большого сервиса. + +Исходный volume: `mtusi-lab2_postgres_data`; новый: `mtusi-lab2-restore_70a1c2c75a5c-data`. Различные ID процессов/контейнеров и обе сверки приведены в JSON. Бэкап не коммитится. +Восстановленный SHA-256 истории: `0f90589eece3910789d2eae40d5685c80cdf9f93e414aca19893851e85ffb071`. + +### Отказы и ограничения durability + +При потере Redis обязательных данных не теряется, кэш восстановим. В тесте чтение/запись/проверки при выключенном Redis заняли 0.297 с суммарно. При выключенном PostgreSQL первый защищённый запрос вернул 503 за 0.007 с; анонимный доступ не разрешался. После возврата зависимостей исходная сессия работала. + +Рестарт контейнера с сохранённым volume не теряет подтверждённые данные при обычных настройках PostgreSQL fsync/synchronous_commit. Он не доказывает устойчивость к поломке диска/host или потере volume. Резервная копия даёт состояние на момент snapshot pg_dump: сообщения после этого снимка будут потеряны при восстановлении только из данного backup. PITR/WAL archiving, репликация PostgreSQL и HA не реализованы. Readiness честно становится 503 без БД. + +Учебные ограничения: один PostgreSQL и один API; DB-role bootstrap владеет учебной БД, ограничение его production-прав не сделано. Накопленные истёкшие сессии не обслуживаются, но фоновый cleanup таблицы пока не добавлен. Redis не кэширует историю и не сокращает обязательную проверку сессии в БД. Общий HTTP-слой не предназначен для публичного Internet-deployment без TLS/reverse proxy. + +## Коротко для защиты + +COMMIT подтверждает транзакцию; устойчивость зависит от WAL, fsync, synchronous_commit и сохранности носителя. Аутентификация определяет пользователя по Bearer, членство отдельно определяет доступ к чату. Flush/restart Redis затрагивает только кэш профилей; сессии остаются в PostgreSQL. Password KDF намеренно затратен и использует соль, быстрый checksum для паролей не подходит. + +## Источники и помощь + +Исходный skeleton lab2, его контракты и публичные tests; собственные src/common.py, src/server.py и тесты из lab1. +Документация: [PostgreSQL EXPLAIN](https://www.postgresql.org/docs/17/using-explain.html), [pg_dump](https://www.postgresql.org/docs/17/app-pgdump.html), [pg_restore](https://www.postgresql.org/docs/17/app-pgrestore.html), [psycopg pool](https://www.psycopg.org/psycopg3/docs/advanced/pool.html), [Python scrypt](https://docs.python.org/3.12/library/hashlib.html#hashlib.scrypt), [Redis EXPIRE](https://redis.io/docs/latest/commands/expire/), [RFC 7617](https://www.rfc-editor.org/rfc/rfc7617). +Codex использован для реализации, тестирования, экспериментов и отчёта. Все численные результаты получены локальными запусками; контракты и публичные проверки не ослаблялись. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..947c8bf5 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,57 @@ +name: mtusi-lab2 +services: + api: + build: . + ports: + - "127.0.0.1:${APP_PORT:-8080}:8080" + environment: + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + secrets: + - database_url + - cache_url + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + mem_limit: 256m + cpus: 1 + stop_grace_period: 10s + postgres: + image: postgres:17.2-bookworm@sha256:3267c505060a0052e5aa6e5175a7b41ab6b04da2f8c4540fc6e98a37210aa2d3 + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 3s + timeout: 2s + retries: 20 + mem_limit: 512m + cpus: 1 + redis: + image: redis:7.4.1-bookworm@sha256:bb142a9c18ac18a16713c1491d779697b4e107c22a97266616099d288237ef47 + command: ["redis-server", "--maxmemory", "32mb", "--maxmemory-policy", "allkeys-lru", "--save", "", "--appendonly", "no"] + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 2s + retries: 20 + mem_limit: 64m + cpus: 0.5 +volumes: + postgres_data: +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url diff --git a/evidence/build.txt b/evidence/build.txt new file mode 100644 index 00000000..769af04f --- /dev/null +++ b/evidence/build.txt @@ -0,0 +1,78 @@ + Image mtusi-lab2-api Building +#1 [internal] load local bake definitions +#1 reading from stdin 656B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 739B 0.1s done +#2 DONE 0.1s + +#3 [internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#3 DONE 0.1s + +#4 [internal] load .dockerignore +#4 transferring context: 132B 0.0s done +#4 DONE 0.1s + +#5 [internal] load build context +#5 transferring context: 24.73kB 0.2s done +#5 DONE 0.3s + +#6 [1/7] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 resolve docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 resolve docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 0.4s done +#6 DONE 0.5s + +#7 [3/7] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#7 CACHED + +#8 [4/7] COPY requirements.txt ./ +#8 CACHED + +#9 [2/7] WORKDIR /app +#9 CACHED + +#10 [5/7] RUN pip install --no-cache-dir -r requirements.txt +#10 CACHED + +#11 [6/7] COPY src/ ./src/ +#11 DONE 0.2s + +#12 [7/7] COPY migrations/ ./migrations/ +#12 DONE 0.3s + +#13 exporting to image +#13 exporting layers +#13 exporting layers 1.1s done +#13 exporting manifest sha256:5017231aa6022853d9ca872aa39e5c980e31c40e17c7986757b3afc3651064b5 0.1s done +#13 exporting config sha256:2b594068234b84afae7062ee6b719d9e84a772923d9a4f0d9c2ab431aac7f09c +#13 exporting config sha256:2b594068234b84afae7062ee6b719d9e84a772923d9a4f0d9c2ab431aac7f09c 0.1s done +#13 exporting attestation manifest sha256:3ae47f10c02268299f427294491ad4f25e5485ce4905715abe5868d2d0818ca6 +#13 exporting attestation manifest sha256:3ae47f10c02268299f427294491ad4f25e5485ce4905715abe5868d2d0818ca6 0.2s done +#13 exporting manifest list sha256:90f48f95ec551e4de0cb9ca3c8da3fbac9bfa588a52cd6fd9b21a225ac9e48cd +#13 exporting manifest list sha256:90f48f95ec551e4de0cb9ca3c8da3fbac9bfa588a52cd6fd9b21a225ac9e48cd 0.1s done +#13 naming to docker.io/library/mtusi-lab2-api:latest +#13 naming to docker.io/library/mtusi-lab2-api:latest 0.0s done +#13 unpacking to docker.io/library/mtusi-lab2-api:latest +#13 unpacking to docker.io/library/mtusi-lab2-api:latest 0.2s done +#13 DONE 2.1s + +#14 resolving provenance for metadata file +#14 DONE 0.1s + Image mtusi-lab2-api Built + Container mtusi-lab2-redis-1 Running + Container mtusi-lab2-postgres-1 Running + Container mtusi-lab2-api-1 Recreate + Container mtusi-lab2-api-1 Recreated + Container mtusi-lab2-postgres-1 Waiting + Container mtusi-lab2-redis-1 Waiting + Container mtusi-lab2-redis-1 Healthy + Container mtusi-lab2-postgres-1 Healthy + Container mtusi-lab2-api-1 Starting + Container mtusi-lab2-api-1 Started + Container mtusi-lab2-redis-1 Waiting + Container mtusi-lab2-api-1 Waiting + Container mtusi-lab2-postgres-1 Waiting + Container mtusi-lab2-postgres-1 Healthy + Container mtusi-lab2-redis-1 Healthy + Container mtusi-lab2-api-1 Healthy diff --git a/evidence/check.txt b/evidence/check.txt new file mode 100644 index 00000000..9a553306 --- /dev/null +++ b/evidence/check.txt @@ -0,0 +1,2 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/clean-clone.txt b/evidence/clean-clone.txt new file mode 100644 index 00000000..cdc0674b --- /dev/null +++ b/evidence/clean-clone.txt @@ -0,0 +1,130 @@ +$ git clone --local /Users/ssmayorova/Documents/ChatGPT/Высоконагруженные приложения/mtusi2026lab2 /Users/ssmayorova/Documents/ChatGPT/Высоконагруженные приложения/clean-clone-o_gf7ofj/mtusi2026lab2 +Cloning into '/Users/ssmayorova/Documents/ChatGPT/Высоконагруженные приложения/clean-clone-o_gf7ofj/mtusi2026lab2'... +done. +$ python3 scripts/bootstrap.py +Local configuration ready; secrets were not printed. +$ docker compose up -d --build --wait + Image mtusi-clean-144e2e11-api Building +#1 [internal] load local bake definitions +#1 reading from stdin 718B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 739B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 132B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 24.62kB 0.0s done +#5 DONE 0.0s + +#6 [1/7] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 resolve docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 0.1s done +#6 DONE 0.1s + +#7 [4/7] COPY requirements.txt ./ +#7 CACHED + +#8 [5/7] RUN pip install --no-cache-dir -r requirements.txt +#8 CACHED + +#9 [2/7] WORKDIR /app +#9 CACHED + +#10 [6/7] COPY src/ ./src/ +#10 CACHED + +#11 [3/7] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#11 CACHED + +#12 [7/7] COPY migrations/ ./migrations/ +#12 CACHED + +#13 exporting to image +#13 exporting layers done +#13 exporting manifest sha256:17e1aa1e1ab70d739372aac14340c7082aa2b9d516783f5bb8416e85ac608009 0.0s done +#13 exporting config sha256:08162a60f623a2176f3cf1137372f23710c41dd7f958d7212129d15d5829c02b +#13 exporting config sha256:08162a60f623a2176f3cf1137372f23710c41dd7f958d7212129d15d5829c02b 0.0s done +#13 exporting attestation manifest sha256:ed2d5c62a3502802fd1706fca353486999cc539bd9b0d7f4171309a19a8fabe3 0.0s done +#13 exporting manifest list sha256:f866af8b5e84486fa48b2a6264eb645a81e4e41773a66380f849fb3628f59f49 0.0s done +#13 naming to docker.io/library/mtusi-clean-144e2e11-api:latest done +#13 unpacking to docker.io/library/mtusi-clean-144e2e11-api:latest 0.0s done +#13 DONE 0.2s + +#14 resolving provenance for metadata file +#14 DONE 0.0s + Image mtusi-clean-144e2e11-api Built + Network mtusi-clean-144e2e11_default Creating + Network mtusi-clean-144e2e11_default Created + Volume mtusi-clean-144e2e11_postgres_data Creating + Volume mtusi-clean-144e2e11_postgres_data Created + Container mtusi-clean-144e2e11-redis-1 Creating + Container mtusi-clean-144e2e11-postgres-1 Creating + Container mtusi-clean-144e2e11-postgres-1 Created + Container mtusi-clean-144e2e11-redis-1 Created + Container mtusi-clean-144e2e11-api-1 Creating + Container mtusi-clean-144e2e11-api-1 Created + Container mtusi-clean-144e2e11-redis-1 Starting + Container mtusi-clean-144e2e11-postgres-1 Starting + Container mtusi-clean-144e2e11-redis-1 Started + Container mtusi-clean-144e2e11-postgres-1 Started + Container mtusi-clean-144e2e11-postgres-1 Waiting + Container mtusi-clean-144e2e11-redis-1 Waiting + Container mtusi-clean-144e2e11-redis-1 Healthy + Container mtusi-clean-144e2e11-postgres-1 Healthy + Container mtusi-clean-144e2e11-api-1 Starting + Container mtusi-clean-144e2e11-api-1 Started + Container mtusi-clean-144e2e11-redis-1 Waiting + Container mtusi-clean-144e2e11-api-1 Waiting + Container mtusi-clean-144e2e11-postgres-1 Waiting + Container mtusi-clean-144e2e11-redis-1 Healthy + Container mtusi-clean-144e2e11-postgres-1 Healthy + Container mtusi-clean-144e2e11-api-1 Healthy +$ make check test test-student BASE_URL=http://localhost:18086 +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +python3 tests/smoke.py --base-url "http://localhost:18086" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. +BASE_URL="http://localhost:18086" python3 -m unittest discover -s student-tests -p test_api.py -v +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok + +---------------------------------------------------------------------- +Ran 8 tests in 11.843s + +OK +$ make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:18086 +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:18086" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +$ docker compose exec -T postgres psql -U messenger -d messenger -Atc SELECT count(*) FROM schema_migrations; +1 + Container mtusi-clean-144e2e11-api-1 Stopping + Container mtusi-clean-144e2e11-api-1 Stopped + Container mtusi-clean-144e2e11-api-1 Removing + Container mtusi-clean-144e2e11-api-1 Removed + Container mtusi-clean-144e2e11-postgres-1 Stopping + Container mtusi-clean-144e2e11-redis-1 Stopping + Container mtusi-clean-144e2e11-redis-1 Stopped + Container mtusi-clean-144e2e11-redis-1 Removing + Container mtusi-clean-144e2e11-postgres-1 Stopped + Container mtusi-clean-144e2e11-postgres-1 Removing + Container mtusi-clean-144e2e11-redis-1 Removed + Container mtusi-clean-144e2e11-postgres-1 Removed + Volume mtusi-clean-144e2e11_postgres_data Removing + Network mtusi-clean-144e2e11_default Removing + Volume mtusi-clean-144e2e11_postgres_data Removed + Network mtusi-clean-144e2e11_default Removed +PASS: clean local clone, fresh image and all requested acceptance tests. diff --git a/evidence/dependencies.json b/evidence/dependencies.json new file mode 100644 index 00000000..3a9ac2aa --- /dev/null +++ b/evidence/dependencies.json @@ -0,0 +1,9 @@ +{ + "composite_operation_rollback": true, + "redis_user_cache_ttl_seconds": 60, + "redis_outage_seconds": 0.2969403329999998, + "redis_recovered_without_relogin": true, + "postgres_outage_response_seconds": 0.006768832999999752, + "postgres_recovered_without_relogin": true, + "short_ttl_expired": true +} \ No newline at end of file diff --git a/evidence/dependencies.txt b/evidence/dependencies.txt new file mode 100644 index 00000000..c428d723 --- /dev/null +++ b/evidence/dependencies.txt @@ -0,0 +1,15 @@ + Container mtusi-lab2-redis-1 Stopping + Container mtusi-lab2-redis-1 Stopped + Container mtusi-lab2-redis-1 Starting + Container mtusi-lab2-redis-1 Started + Container mtusi-lab2-redis-1 Waiting + Container mtusi-lab2-redis-1 Healthy + Container mtusi-lab2-postgres-1 Stopping + Container mtusi-lab2-postgres-1 Stopped + Container mtusi-lab2-postgres-1 Starting + Container mtusi-lab2-postgres-1 Started + Container mtusi-lab2-postgres-1 Waiting + Container mtusi-lab2-postgres-1 Healthy + Container mtusi-lab2-expiry-f1a39764 Creating + Container mtusi-lab2-expiry-f1a39764 Created +PASS: rollback, real Redis TTL/cache, Redis outage, PostgreSQL outage/recovery and session expiry. diff --git a/evidence/final-api.txt b/evidence/final-api.txt new file mode 100644 index 00000000..a0071995 --- /dev/null +++ b/evidence/final-api.txt @@ -0,0 +1,18 @@ +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. +BASE_URL="http://localhost:8080" python3 -m unittest discover -s student-tests -p test_api.py -v +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok + +---------------------------------------------------------------------- +Ran 8 tests in 13.427s + +OK diff --git a/evidence/index-study.json b/evidence/index-study.json new file mode 100644 index 00000000..979d39cb --- /dev/null +++ b/evidence/index-study.json @@ -0,0 +1,3018 @@ +{ + "database": "research_e3a4181a6e18", + "postgresql": "PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit", + "sizes": [ + 70000, + 20000, + 5000, + 2000, + 1000, + 800, + 500, + 300, + 200, + 200 + ], + "query": "\n SELECT\n id\n , chat_id\n , sender_id\n , text\n , created_at\n FROM messages\n WHERE 1=1\n AND chat_id = %s\n AND (created_at, id) > (%s, %s)\n ORDER BY created_at, id\n LIMIT 51\n ", + "repeats": 5, + "results": [ + { + "variant": "chat_only", + "index_bytes": 753664, + "reads": [ + { + "chat_messages": 70000, + "median_execution_ms": 26.665, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4041.65, + "Total Cost": 4041.77, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 30.76, + "Actual Total Time": 30.778, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 1968, + "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": 4041.65, + "Total Cost": 4065.9, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 30.757, + "Actual Total Time": 30.766, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 1968, + "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": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3718.0, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 13.769, + "Actual Total Time": 27.723, + "Actual Rows": 14000, + "Actual Loops": 1, + "Filter": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Filter": 86000, + "Shared Hit Blocks": 1968, + "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": 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 Time": 0.36, + "Triggers": [], + "Execution Time": 30.825 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4041.65, + "Total Cost": 4041.77, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 31.086, + "Actual Total Time": 31.104, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 1968, + "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": 4041.65, + "Total Cost": 4065.9, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 31.083, + "Actual Total Time": 31.093, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 1968, + "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": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3718.0, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 15.316, + "Actual Total Time": 27.916, + "Actual Rows": 14000, + "Actual Loops": 1, + "Filter": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Filter": 86000, + "Shared Hit Blocks": 1968, + "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": 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 Time": 0.179, + "Triggers": [], + "Execution Time": 31.149 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4041.65, + "Total Cost": 4041.77, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 26.299, + "Actual Total Time": 26.316, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 1968, + "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": 4041.65, + "Total Cost": 4065.9, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 26.296, + "Actual Total Time": 26.304, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 1968, + "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": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3718.0, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 12.752, + "Actual Total Time": 23.574, + "Actual Rows": 14000, + "Actual Loops": 1, + "Filter": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Filter": 86000, + "Shared Hit Blocks": 1968, + "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": 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 Time": 0.164, + "Triggers": [], + "Execution Time": 26.394 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4041.65, + "Total Cost": 4041.77, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 21.487, + "Actual Total Time": 21.501, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 1968, + "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": 4041.65, + "Total Cost": 4065.9, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 21.484, + "Actual Total Time": 21.492, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 1968, + "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": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3718.0, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 11.832, + "Actual Total Time": 19.56, + "Actual Rows": 14000, + "Actual Loops": 1, + "Filter": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Filter": 86000, + "Shared Hit Blocks": 1968, + "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": 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 Time": 0.175, + "Triggers": [], + "Execution Time": 21.542 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4041.65, + "Total Cost": 4041.77, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 26.592, + "Actual Total Time": 26.628, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 1968, + "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": 4041.65, + "Total Cost": 4065.9, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 26.589, + "Actual Total Time": 26.597, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 1968, + "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": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3718.0, + "Plan Rows": 9701, + "Plan Width": 121, + "Actual Startup Time": 12.442, + "Actual Total Time": 23.672, + "Actual Rows": 14000, + "Actual Loops": 1, + "Filter": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Filter": 86000, + "Shared Hit Blocks": 1968, + "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": 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 Time": 0.163, + "Triggers": [], + "Execution Time": 26.665 + } + ] + }, + { + "chat_messages": 20000, + "median_execution_ms": 7.168, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2929.91, + "Total Cost": 2930.04, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 9.52, + "Actual Total Time": 9.544, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 404, + "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": 2929.91, + "Total Cost": 2958.91, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 9.517, + "Actual Total Time": 9.524, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 404, + "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": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 225.12, + "Total Cost": 2542.94, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 7.18, + "Actual Total Time": 8.888, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 16000, + "Exact Heap Blocks": 385, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 404, + "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": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_history", + "Startup Cost": 0.0, + "Total Cost": 222.22, + "Plan Rows": 19990, + "Plan Width": 0, + "Actual Startup Time": 1.674, + "Actual Total Time": 1.674, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Shared Hit Blocks": 19, + "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": 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 Time": 0.224, + "Triggers": [], + "Execution Time": 9.591 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2929.91, + "Total Cost": 2930.04, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 5.88, + "Actual Total Time": 5.892, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 404, + "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": 2929.91, + "Total Cost": 2958.91, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 5.877, + "Actual Total Time": 5.883, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 404, + "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": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 225.12, + "Total Cost": 2542.94, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 4.382, + "Actual Total Time": 5.336, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 16000, + "Exact Heap Blocks": 385, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 404, + "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": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_history", + "Startup Cost": 0.0, + "Total Cost": 222.22, + "Plan Rows": 19990, + "Plan Width": 0, + "Actual Startup Time": 0.669, + "Actual Total Time": 0.67, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Shared Hit Blocks": 19, + "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": 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 Time": 0.246, + "Triggers": [], + "Execution Time": 6.031 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2929.91, + "Total Cost": 2930.04, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 7.114, + "Actual Total Time": 7.129, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 404, + "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": 2929.91, + "Total Cost": 2958.91, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 7.112, + "Actual Total Time": 7.119, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 404, + "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": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 225.12, + "Total Cost": 2542.94, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 4.52, + "Actual Total Time": 6.217, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 16000, + "Exact Heap Blocks": 385, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 404, + "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": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_history", + "Startup Cost": 0.0, + "Total Cost": 222.22, + "Plan Rows": 19990, + "Plan Width": 0, + "Actual Startup Time": 0.735, + "Actual Total Time": 0.736, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Shared Hit Blocks": 19, + "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": 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 Time": 0.14, + "Triggers": [], + "Execution Time": 7.168 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2929.91, + "Total Cost": 2930.04, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 9.149, + "Actual Total Time": 9.166, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 404, + "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": 2929.91, + "Total Cost": 2958.91, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 9.147, + "Actual Total Time": 9.156, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 404, + "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": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 225.12, + "Total Cost": 2542.94, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 6.961, + "Actual Total Time": 8.357, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 16000, + "Exact Heap Blocks": 385, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 404, + "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": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_history", + "Startup Cost": 0.0, + "Total Cost": 222.22, + "Plan Rows": 19990, + "Plan Width": 0, + "Actual Startup Time": 1.173, + "Actual Total Time": 1.174, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Shared Hit Blocks": 19, + "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": 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 Time": 0.176, + "Triggers": [], + "Execution Time": 9.21 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2929.91, + "Total Cost": 2930.04, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 6.349, + "Actual Total Time": 6.363, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 404, + "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": 2929.91, + "Total Cost": 2958.91, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 6.347, + "Actual Total Time": 6.353, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 404, + "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": "Bitmap Heap Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 225.12, + "Total Cost": 2542.94, + "Plan Rows": 11599, + "Plan Width": 121, + "Actual Startup Time": 4.511, + "Actual Total Time": 5.601, + "Actual Rows": 4000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 16000, + "Exact Heap Blocks": 385, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 404, + "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": "Bitmap Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Index Name": "messages_history", + "Startup Cost": 0.0, + "Total Cost": 222.22, + "Plan Rows": 19990, + "Plan Width": 0, + "Actual Startup Time": 0.831, + "Actual Total Time": 0.831, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid)", + "Shared Hit Blocks": 19, + "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": 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 Time": 0.201, + "Triggers": [], + "Execution Time": 6.401 + } + ] + }, + { + "chat_messages": 200, + "median_execution_ms": 0.073, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.32, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.059, + "Actual Total Time": 0.066, + "Actual Rows": 40, + "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": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.65, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.058, + "Actual Total Time": 0.061, + "Actual Rows": 40, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 35, + "Sort Space Type": "Memory", + "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.29, + "Total Cost": 384.05, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.039, + "Actual Total Time": 0.049, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 160, + "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": 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 Time": 0.08, + "Triggers": [], + "Execution Time": 0.082 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.32, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.053, + "Actual Total Time": 0.06, + "Actual Rows": 40, + "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": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.65, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.052, + "Actual Total Time": 0.055, + "Actual Rows": 40, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 35, + "Sort Space Type": "Memory", + "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.29, + "Total Cost": 384.05, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.036, + "Actual Total Time": 0.044, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 160, + "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": 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 Time": 0.055, + "Triggers": [], + "Execution Time": 0.073 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.32, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.051, + "Actual Total Time": 0.058, + "Actual Rows": 40, + "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": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.65, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.051, + "Actual Total Time": 0.054, + "Actual Rows": 40, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 35, + "Sort Space Type": "Memory", + "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.29, + "Total Cost": 384.05, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.033, + "Actual Total Time": 0.043, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 160, + "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": 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 Time": 0.05, + "Triggers": [], + "Execution Time": 0.128 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.32, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.052, + "Actual Total Time": 0.06, + "Actual Rows": 40, + "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": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.65, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.051, + "Actual Total Time": 0.055, + "Actual Rows": 40, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 35, + "Sort Space Type": "Memory", + "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.29, + "Total Cost": 384.05, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.034, + "Actual Total Time": 0.043, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 160, + "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": 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 Time": 0.059, + "Triggers": [], + "Execution Time": 0.073 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.32, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.049, + "Actual Total Time": 0.056, + "Actual Rows": 40, + "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": "Sort", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 390.19, + "Total Cost": 390.65, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.048, + "Actual Total Time": 0.051, + "Actual Rows": 40, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 35, + "Sort Space Type": "Memory", + "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.29, + "Total Cost": 384.05, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.032, + "Actual Total Time": 0.04, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid))", + "Rows Removed by Filter": 160, + "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": 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 Time": 0.048, + "Triggers": [], + "Execution Time": 0.067 + } + ] + } + ], + "writes": [ + { + "rows": 1000, + "commit_ms": 51.328176000197345, + "wal_bytes": 254192 + }, + { + "rows": 1000, + "commit_ms": 132.15779799998018, + "wal_bytes": 238088 + }, + { + "rows": 1000, + "commit_ms": 88.45494599995618, + "wal_bytes": 245480 + } + ] + }, + { + "variant": "composite", + "index_bytes": 6127616, + "reads": [ + { + "chat_messages": 70000, + "median_execution_ms": 0.057, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 34.54, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.03, + "Actual Total Time": 0.05, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6460.76, + "Plan Rows": 9655, + "Plan Width": 121, + "Actual Startup Time": 0.028, + "Actual Total Time": 0.041, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.19, + "Triggers": [], + "Execution Time": 0.08 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 34.54, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.018, + "Actual Total Time": 0.037, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6460.76, + "Plan Rows": 9655, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.028, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.088, + "Triggers": [], + "Execution Time": 0.057 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 34.54, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.016, + "Actual Total Time": 0.035, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6460.76, + "Plan Rows": 9655, + "Plan Width": 121, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.027, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.089, + "Triggers": [], + "Execution Time": 0.053 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 34.54, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.018, + "Actual Total Time": 0.036, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6460.76, + "Plan Rows": 9655, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.028, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.14, + "Triggers": [], + "Execution Time": 0.055 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 34.54, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.018, + "Actual Total Time": 0.037, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6460.76, + "Plan Rows": 9655, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.028, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '1475545b-0118-40e4-959e-59045703e558'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.056+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.176, + "Triggers": [], + "Execution Time": 0.057 + } + ] + }, + { + "chat_messages": 20000, + "median_execution_ms": 0.072, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 29.48, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.049, + "Actual Total Time": 0.074, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6564.03, + "Plan Rows": 11517, + "Plan Width": 121, + "Actual Startup Time": 0.049, + "Actual Total Time": 0.061, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.153, + "Triggers": [], + "Execution Time": 0.095 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 29.48, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.018, + "Actual Total Time": 0.055, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6564.03, + "Plan Rows": 11517, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.046, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.071, + "Triggers": [], + "Execution Time": 0.072 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 29.48, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.038, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6564.03, + "Plan Rows": 11517, + "Plan Width": 121, + "Actual Startup Time": 0.016, + "Actual Total Time": 0.03, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.061, + "Triggers": [], + "Execution Time": 0.052 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 29.48, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.033, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6564.03, + "Plan Rows": 11517, + "Plan Width": 121, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.025, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.055, + "Triggers": [], + "Execution Time": 0.047 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 29.48, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.018, + "Actual Total Time": 0.143, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 6564.03, + "Plan Rows": 11517, + "Plan Width": 121, + "Actual Startup Time": 0.017, + "Actual Total Time": 0.135, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '38cc764f-0e25-4cd9-8c22-46efb8b690da'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.155, + "Triggers": [], + "Execution Time": 0.161 + } + ] + }, + { + "chat_messages": 200, + "median_execution_ms": 0.065, + "plans": [ + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 147.78, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.027, + "Actual Total Time": 0.045, + "Actual Rows": 40, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 532.08, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.026, + "Actual Total Time": 0.038, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.119, + "Triggers": [], + "Execution Time": 0.065 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 147.78, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.016, + "Actual Total Time": 0.032, + "Actual Rows": 40, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 532.08, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.025, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.1, + "Triggers": [], + "Execution Time": 0.05 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 147.78, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.012, + "Actual Total Time": 0.028, + "Actual Rows": 40, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 532.08, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.011, + "Actual Total Time": 0.021, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.064, + "Triggers": [], + "Execution Time": 0.042 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 147.78, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.016, + "Actual Total Time": 0.032, + "Actual Rows": 40, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 532.08, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.025, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.163, + "Triggers": [], + "Execution Time": 0.368 + }, + { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 147.78, + "Plan Rows": 51, + "Plan Width": 121, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.035, + "Actual Rows": 40, + "Actual Loops": 1, + "Shared Hit Blocks": 5, + "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": 532.08, + "Plan Rows": 184, + "Plan Width": 121, + "Actual Startup Time": 0.013, + "Actual Total Time": 0.023, + "Actual Rows": 40, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'be0dc61b-8735-451c-9bee-09d564986b25'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.00016+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 5, + "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": 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 Time": 0.076, + "Triggers": [], + "Execution Time": 0.079 + } + ] + } + ], + "writes": [ + { + "rows": 1000, + "commit_ms": 186.78137000006245, + "wal_bytes": 326616 + }, + { + "rows": 1000, + "commit_ms": 329.82252800002243, + "wal_bytes": 265320 + }, + { + "rows": 1000, + "commit_ms": 473.14494300007937, + "wal_bytes": 322672 + } + ] + } + ], + "snapshot": { + "counts": { + "users": 1, + "chats": 10, + "chat_members": 10, + "messages": 100000, + "sessions": 0 + }, + "messages_sha256": "0f90589eece3910789d2eae40d5685c80cdf9f93e414aca19893851e85ffb071", + "selected_messages": [ + { + "id": "00022a36-d2df-41d5-ab35-a9b2c71cd633", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.013387+00:00" + }, + { + "id": "0002979a-977b-4045-aa42-dfe668d8a498", + "chat_id": "38cc764f-0e25-4cd9-8c22-46efb8b690da", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.002863+00:00" + }, + { + "id": "7facae94-6022-46c6-9f65-d76df1d46d58", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.042234+00:00" + }, + { + "id": "fffe2b8a-98d0-4c28-ae2a-08845ec95a49", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.035161+00:00" + }, + { + "id": "fffe8eae-d740-4296-b936-4f90a7bc140d", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.002345+00:00" + } + ] + }, + "backup": { + "format": "pg_dump custom", + "bytes": 2655979, + "dump_seconds": 1.1019309590000024, + "bootstrap_seconds": 2.5880723749999994, + "restore_seconds": 1.742141042, + "restored_database": "restore_70a1c2c75a5c", + "source_container_id": "f8be6202d5e4", + "restore_container_id": "2db271910eb2", + "fresh_restore_volume": "mtusi-lab2-restore_70a1c2c75a5c-data", + "source_volume": [ + "mtusi-lab2_postgres_data" + ], + "matching_counts": true, + "matching_all_messages_sha256": true, + "matching_selected_messages": true, + "restored_snapshot": { + "counts": { + "users": 1, + "chats": 10, + "chat_members": 10, + "messages": 100000, + "sessions": 0 + }, + "messages_sha256": "0f90589eece3910789d2eae40d5685c80cdf9f93e414aca19893851e85ffb071", + "selected_messages": [ + { + "id": "00022a36-d2df-41d5-ab35-a9b2c71cd633", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.013387+00:00" + }, + { + "id": "0002979a-977b-4045-aa42-dfe668d8a498", + "chat_id": "38cc764f-0e25-4cd9-8c22-46efb8b690da", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.002863+00:00" + }, + { + "id": "7facae94-6022-46c6-9f65-d76df1d46d58", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.042234+00:00" + }, + { + "id": "fffe2b8a-98d0-4c28-ae2a-08845ec95a49", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.035161+00:00" + }, + { + "id": "fffe8eae-d740-4296-b936-4f90a7bc140d", + "chat_id": "1475545b-0118-40e4-959e-59045703e558", + "sender_id": "0bbe8371-c929-4ca0-9bde-82cfa960a2ac", + "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", + "created_at": "2026-01-01T00:00:00.002345+00:00" + } + ] + } + } +} \ No newline at end of file diff --git a/evidence/kdf-regression.json b/evidence/kdf-regression.json new file mode 100644 index 00000000..cafa0c0b --- /dev/null +++ b/evidence/kdf-regression.json @@ -0,0 +1,8 @@ +{ + "scenario": "Initial own test: 10 concurrent registrations, before bounding KDF concurrency", + "container_memory_mib": 256, + "observed_exit_code": 137, + "observed_oom_killed": true, + "fix": "Limit simultaneous scrypt computations to 2 and set MALLOC_ARENA_MAX=2; rerun the same registration race under unchanged memory limit", + "final_result_source": "student-tests.txt" +} diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..933c59c7 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,3 @@ +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/research.txt b/evidence/research.txt new file mode 100644 index 00000000..60bad357 --- /dev/null +++ b/evidence/research.txt @@ -0,0 +1,6 @@ + Container mtusi-lab2-api-run-3b62fd7eb732 Creating + Container mtusi-lab2-api-run-3b62fd7eb732 Created + Container mtusi-lab2-api-run-0145f8a8aff1 Creating + Container mtusi-lab2-api-run-0145f8a8aff1 Created +PASS: 100000 messages; two indexes; write costs; backup/restore counts, samples and full checksum match. +Backup seconds: 1.102 Restore seconds: 1.742 diff --git a/evidence/restart-after.json b/evidence/restart-after.json new file mode 100644 index 00000000..9169939d --- /dev/null +++ b/evidence/restart-after.json @@ -0,0 +1,27 @@ +[ + { + "service": "api", + "container_id": "03414eeb9af4", + "started_at": "2026-10-23T06:53:58.384822282Z", + "status": "running", + "volumes": [] + }, + { + "service": "postgres", + "container_id": "f8be6202d5e4", + "started_at": "2026-10-23T06:53:51.683479639Z", + "status": "running", + "volumes": [ + "mtusi-lab2_postgres_data" + ] + }, + { + "service": "redis", + "container_id": "5fecf0c5edd1", + "started_at": "2026-10-23T06:53:51.383818051Z", + "status": "running", + "volumes": [ + "b72dab0a9ef82932abd5655e09a614b5c99b3a7cef959426f05ad0a2ed7dbc79" + ] + } +] diff --git a/evidence/restart-before.json b/evidence/restart-before.json new file mode 100644 index 00000000..9d77d458 --- /dev/null +++ b/evidence/restart-before.json @@ -0,0 +1,27 @@ +[ + { + "service": "api", + "container_id": "03414eeb9af4", + "started_at": "2026-10-23T06:53:29.066897456Z", + "status": "running", + "volumes": [] + }, + { + "service": "postgres", + "container_id": "f8be6202d5e4", + "started_at": "2026-10-23T06:53:24.074466953Z", + "status": "running", + "volumes": [ + "mtusi-lab2_postgres_data" + ] + }, + { + "service": "redis", + "container_id": "5fecf0c5edd1", + "started_at": "2026-10-23T06:53:23.920707731Z", + "status": "running", + "volumes": [ + "b72dab0a9ef82932abd5655e09a614b5c99b3a7cef959426f05ad0a2ed7dbc79" + ] + } +] diff --git a/evidence/runtime.json b/evidence/runtime.json new file mode 100644 index 00000000..19fb3e7d --- /dev/null +++ b/evidence/runtime.json @@ -0,0 +1,91 @@ +{ + "docker": { + "Platform": { + "Name": "Docker Engine - Community" + }, + "Version": "29.5.2", + "ApiVersion": "1.54", + "MinAPIVersion": "1.40", + "Os": "linux", + "Arch": "arm64", + "Components": [ + { + "Name": "Engine", + "Version": "29.5.2", + "Details": { + "ApiVersion": "1.54", + "Arch": "arm64", + "BuildTime": "Wed May 20 14:39:25 2026", + "Experimental": "false", + "GitCommit": "568f755", + "GoVersion": "go1.26.3", + "KernelVersion": "6.8.0-117-generic", + "MinAPIVersion": "1.40", + "Module": "github.com/moby/moby/v2", + "ModuleVersion": "v2.0.0+unknown", + "Os": "linux" + } + }, + { + "Name": "containerd", + "Version": "v2.2.4", + "Details": { + "GitCommit": "193637f7ee8ae5f5aa5248f49e7baa3e6164966e" + } + }, + { + "Name": "runc", + "Version": "1.3.5", + "Details": { + "GitCommit": "v1.3.5-0-g488fc13e" + } + }, + { + "Name": "docker-init", + "Version": "0.19.0", + "Details": { + "GitCommit": "de40ad0" + } + } + ], + "GitCommit": "568f755", + "GoVersion": "go1.26.3", + "KernelVersion": "6.8.0-117-generic", + "BuildTime": "2026-05-20T14:39:25.000000000+00:00" + }, + "postgres_settings": { + "fsync": "on", + "synchronous_commit": "on", + "full_page_writes": "on", + "migration_versions": [ + "001_initial.sql" + ] + }, + "api_python": "Python 3.12.8", + "containers": [ + { + "service": "api", + "user": "10001:10001", + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "oom_killed": false, + "health": "healthy" + }, + { + "service": "postgres", + "user": "", + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "oom_killed": false, + "health": "healthy" + }, + { + "service": "redis", + "user": "", + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "oom_killed": false, + "health": "healthy" + } + ] +} \ No newline at end of file diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..c93f720f --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/student-tests.txt b/evidence/student-tests.txt new file mode 100644 index 00000000..6299bfdc --- /dev/null +++ b/evidence/student-tests.txt @@ -0,0 +1,13 @@ +test_concurrent_messages_and_pagination (test_api.APIContract) ... ok +test_cursor_scope_and_validation (test_api.APIContract) ... ok +test_foreign_chat (test_api.APIContract) ... ok +test_invalid_chat_creation (test_api.APIContract) ... ok +test_malformed_json_and_extra_fields (test_api.APIContract) ... ok +test_registration_race (test_api.APIContract) ... ok +test_unicode_limits (test_api.APIContract) ... ok +test_unicode_password_basic_and_logout (test_api.APIContract) ... ok + +---------------------------------------------------------------------- +Ran 8 tests in 11.051s + +OK diff --git a/evidence/validation.txt b/evidence/validation.txt new file mode 100644 index 00000000..db607421 --- /dev/null +++ b/evidence/validation.txt @@ -0,0 +1,2 @@ +../.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 00000000..eeac3c14 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,42 @@ +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() + , last_message_at TIMESTAMPTZ +) +; +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) + , user_id UUID NOT NULL REFERENCES users(id) + , PRIMARY KEY (chat_id, user_id) +) +; +CREATE INDEX chat_members_user ON chat_members (user_id, chat_id) +; +CREATE TABLE messages ( + id UUID PRIMARY KEY + , chat_id UUID NOT NULL REFERENCES chats(id) + , sender_id UUID NOT NULL REFERENCES users(id) + , text TEXT NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000) + , created_at TIMESTAMPTZ NOT NULL + , 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 TEXT PRIMARY KEY + , user_id UUID NOT NULL REFERENCES users(id) + , expires_at TIMESTAMPTZ NOT NULL +) +; +CREATE INDEX sessions_expiry ON sessions (expires_at) +; diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..3a7a3d3a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +psycopg[binary]==3.2.3 +psycopg-pool==3.2.4 +redis==5.2.1 +typing-extensions==4.16.0 diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py new file mode 100644 index 00000000..32e38f84 --- /dev/null +++ b/scripts/bootstrap.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""Create local lab credentials once; never print values or overwrite an existing setup.""" +from pathlib import Path +import secrets + +root = Path(__file__).resolve().parents[1] +directory = root / 'secrets' +directory.mkdir(mode=0o700, exist_ok=True) +password_path = directory / 'postgres_password' +if not password_path.exists(): + password_path.write_text(secrets.token_hex(24)) + password_path.chmod(0o600) +password = password_path.read_text().strip() +for name, value in { + 'database_url': 'postgresql://messenger:' + password + '@postgres:5432/messenger', + 'cache_url': 'redis://redis:6379/0', +}.items(): + path = directory / name + if not path.exists(): + path.write_text(value) + # Docker Compose bind-mounted secrets must be readable by UID 10001. + # The containing host directory is accessible only to its owner. + path.chmod(0o644) +env = root / '.env' +if not env.exists(): + env.write_text((root / '.env.example').read_text()) + env.chmod(0o600) +print('Local configuration ready; secrets were not printed.') diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..d31f0b9b --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +mkdir -p evidence +python3 scripts/snapshot.py > evidence/restart-before.json +docker compose restart postgres redis +docker compose up -d --wait postgres redis +docker compose restart api +docker compose up -d --wait api +python3 scripts/snapshot.py > evidence/restart-after.json diff --git a/scripts/snapshot.py b/scripts/snapshot.py new file mode 100644 index 00000000..5dd00148 --- /dev/null +++ b/scripts/snapshot.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +"""Record restart evidence without env, credentials or machine-specific labels.""" +import json +import subprocess + +ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api', 'postgres', 'redis'], text=True).split() +containers = json.loads(subprocess.check_output(['docker', 'inspect', *ids], text=True)) +result = [] +for container in containers: + result.append(dict(service=container['Config']['Labels']['com.docker.compose.service'], container_id=container['Id'][:12], started_at=container['State']['StartedAt'], status=container['State']['Status'], volumes=[m['Name'] for m in container['Mounts'] if m['Type'] == 'volume'])) +print(json.dumps(sorted(result, key=lambda x: x['service']), indent=2)) diff --git a/src/common.py b/src/common.py new file mode 100644 index 00000000..29c87c58 --- /dev/null +++ b/src/common.py @@ -0,0 +1,98 @@ +"""Shared HTTP contract; storage is supplied by each laboratory.""" +import base64 +import datetime as dt +import json +import re +import uuid + + +class APIError(Exception): + def __init__(self, status, code, message): + self.status, self.code, self.message = status, code, message + + +def invalid(): + raise APIError(400, 'invalid_request', 'Неверные параметры запроса') + + +def unauthorized(): + raise APIError(401, 'unauthorized', 'Требуется действительная идентификация') + + +def missing(): + raise APIError(404, 'not_found', 'Объект не найден') + + +def identifier(value): + try: + if not isinstance(value, str): + invalid() + return str(uuid.UUID(value)) + except ValueError: + invalid() + + +def text(value, maximum, minimum=1): + if not isinstance(value, str) or not minimum <= len(value) <= maximum or not value.strip(): + invalid() + try: + value.encode('utf-8') + except UnicodeError: + invalid() + return value + + +def fields(value, required): + if not isinstance(value, dict) or set(value) != set(required): + invalid() + + +def username(value): + if not isinstance(value, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', value): + invalid() + return value + + +def now(): + return dt.datetime.now(dt.timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z') + + +def page_args(query, scope): + if set(query) - {'limit', 'cursor'} or any(len(v) != 1 for v in query.values()): + invalid() + limit = query.get('limit', ['50'])[0] + if not re.fullmatch(r'[0-9]+', limit): + invalid() + try: + limit = int(limit) + except ValueError: + invalid() + if not 1 <= limit <= 100: + invalid() + cursor = None + if 'cursor' in query: + raw = query['cursor'][0] + try: + if not raw or len(raw) > 1024: + invalid() + decoded = json.loads(base64.b64decode(raw + '=' * (-len(raw) % 4), altchars=b'-_', validate=True)) + if not isinstance(decoded, list) or len(decoded) != 3 or decoded[0] != scope: + invalid() + stamp = decoded[1] + if not isinstance(stamp, str) or not stamp.endswith('Z'): + invalid() + stamp = dt.datetime.fromisoformat(stamp[:-1] + '+00:00').isoformat(timespec='microseconds').replace('+00:00', 'Z') + cursor = (stamp, identifier(decoded[2])) + except (ValueError, TypeError, UnicodeError): + invalid() + return limit, cursor + + +def page(items, limit, scope): + more = len(items) > limit + items = items[:limit] + cursor = None + if more: + last = items[-1] + cursor = base64.urlsafe_b64encode(json.dumps([scope, last['created_at'], last['id']]).encode()).decode().rstrip('=') + return {'items': items, 'next_cursor': cursor} diff --git a/src/postgres.py b/src/postgres.py new file mode 100644 index 00000000..f7b76b84 --- /dev/null +++ b/src/postgres.py @@ -0,0 +1,261 @@ +import base64 +import contextlib +import datetime as dt +import hashlib +import hmac +import json +import os +from pathlib import Path +import secrets +import threading +import uuid + +import psycopg +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool, PoolTimeout +import redis + +from common import APIError, fields, identifier, invalid, missing, page, text, unauthorized, username + +KDF_SLOTS = threading.BoundedSemaphore(2) + + +def setting(name): + path = os.environ.get(name + '_FILE') + return Path(path).read_text().strip() if path else os.environ[name] + + +def public(row): + return {k: (str(v) if isinstance(v, uuid.UUID) else v.isoformat(timespec='microseconds').replace('+00:00', 'Z') if isinstance(v, dt.datetime) else v) for k, v in row.items() if k != 'password_hash'} + + +def password_hash(password, salt=None): + salt = salt or secrets.token_bytes(16) + if not KDF_SLOTS.acquire(timeout=1.5): + raise APIError(503, 'unavailable', 'Аутентификация временно перегружена') + try: + digest = hashlib.scrypt(password.encode('utf-8'), salt=salt, n=16384, r=8, p=1, dklen=32, maxmem=64 * 1024 * 1024) + finally: + KDF_SLOTS.release() + return 'scrypt$16384$8$1$' + salt.hex() + '$' + digest.hex() + + +class Store: + persistent = True + + def __init__(self): + self.ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600')) + if not 1 <= self.ttl <= 86400: + raise ValueError('SESSION_TTL_SECONDS must be 1..86400') + self.pool = ConnectionPool(setting('DATABASE_URL'), min_size=1, max_size=8, timeout=2, kwargs={'row_factory': dict_row, 'connect_timeout': 2, 'options': '-c statement_timeout=2000 -c lock_timeout=1500'}, open=True) + self.pool.wait(timeout=10) + self.cache = redis.Redis.from_url(setting('CACHE_URL'), socket_connect_timeout=0.15, socket_timeout=0.15, decode_responses=True) + self.dummy_hash = password_hash('dummy-password-for-unknown-users') + self.migrate() + + @contextlib.contextmanager + def connection(self): + try: + with self.pool.connection() as conn: + yield conn + except psycopg.errors.UniqueViolation: + raise APIError(409, 'conflict', 'Имя пользователя занято') from None + except (psycopg.Error, PoolTimeout): + raise APIError(503, 'unavailable', 'Хранилище временно недоступно') from None + + def migrate(self): + with self.connection() as conn: + conn.execute('SELECT pg_advisory_xact_lock(2301002)') + conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version TEXT PRIMARY KEY)') + for path in sorted((Path(__file__).resolve().parents[1] / 'migrations').glob('*.sql')): + if not conn.execute('SELECT version FROM schema_migrations WHERE 1=1 AND version = %s', (path.name,)).fetchone(): + conn.execute(path.read_text()) + conn.execute('INSERT INTO schema_migrations (version) VALUES (%s)', (path.name,)) + + def close(self): + self.pool.close() + self.cache.close() + + def ready(self): + with self.connection() as conn: + conn.execute('SELECT 1') + + def register(self, body): + fields(body, ('username', 'display_name', 'password')) + name, display = username(body['username']), text(body['display_name'], 100) + # Passwords may contain spaces and colons; only length/UTF-8 are restricted. + password = body['password'] + if not isinstance(password, str) or not 12 <= len(password) <= 128: + invalid() + try: + password.encode('utf-8') + except UnicodeError: + invalid() + hashed = password_hash(password) + with self.connection() as conn: + row = conn.execute(''' + INSERT INTO users (id, username, display_name, password_hash) + VALUES (%s, %s, %s, %s) + RETURNING id, username, display_name, created_at + ''', (uuid.uuid4(), name, display, hashed)).fetchone() + return public(row) + + def login(self, headers): + authorization = headers.get('Authorization', '') + try: + scheme, encoded = authorization.split(' ', 1) + if scheme.lower() != 'basic': + unauthorized() + name, password = base64.b64decode(encoded, validate=True).decode('utf-8').split(':', 1) + except (ValueError, UnicodeError): + unauthorized() + if not re_username(name) or not 12 <= len(password) <= 128: + unauthorized() + with self.connection() as conn: + row = conn.execute('SELECT id, username, display_name, created_at, password_hash FROM users WHERE 1=1 AND username = %s', (name,)).fetchone() + hashed = row['password_hash'] if row else self.dummy_hash + verified = hmac.compare_digest(password_hash(password, bytes.fromhex(hashed.split('$')[4])), hashed) + if row is None or not verified: + unauthorized() + token = secrets.token_urlsafe(32) + with self.connection() as conn: + expiry = conn.execute(''' + INSERT INTO sessions (token_hash, user_id, expires_at) + VALUES (%s, %s, clock_timestamp() + %s * INTERVAL '1 second') + RETURNING expires_at + ''', (hashlib.sha256(token.encode()).hexdigest(), row['id'], self.ttl)).fetchone()['expires_at'] + return {'token': token, 'token_type': 'Bearer', 'expires_at': public({'expires_at': expiry})['expires_at'], 'user': public(row)} + + def token_hash(self, headers): + parts = headers.get('Authorization', '').split(' ') + if len(parts) != 2 or parts[0].lower() != 'bearer' or len(parts[1]) != 43 or not parts[1].isascii(): + unauthorized() + return hashlib.sha256(parts[1].encode()).hexdigest() + + def authenticate(self, headers): + token_hash = self.token_hash(headers) + with self.connection() as conn: + session = conn.execute(''' + SELECT user_id + FROM sessions + WHERE 1=1 + AND token_hash = %s + AND expires_at > clock_timestamp() + ''', (token_hash,)).fetchone() + if session is None: + unauthorized() + key = 'user:' + str(session['user_id']) + try: + cached = self.cache.get(key) + if cached: + return json.loads(cached) + except (redis.RedisError, ValueError): + pass + row = conn.execute('SELECT id, username, display_name, created_at FROM users WHERE 1=1 AND id = %s', (session['user_id'],)).fetchone() + if row is None: + unauthorized() + user = public(row) + try: + self.cache.setex(key, 60, json.dumps(user)) + except redis.RedisError: + pass + return user + + def logout(self, headers): + with self.connection() as conn: + conn.execute('DELETE FROM sessions WHERE 1=1 AND token_hash = %s', (self.token_hash(headers),)) + + def chat_in(self, conn, actor, chat_id): + row = conn.execute(''' + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE 1=1 + AND c.id = %s + AND cm.user_id = %s + ''', (chat_id, actor['id'])).fetchone() + if row is None: + missing() + members = conn.execute('SELECT user_id FROM chat_members WHERE 1=1 AND chat_id = %s ORDER BY user_id', (chat_id,)).fetchall() + return dict(public(row), member_ids=[str(v['user_id']) for v in members]) + + def chat(self, actor, chat_id): + with self.connection() as conn: + return self.chat_in(conn, actor, chat_id) + + def create_chat(self, actor, body): + fields(body, ('title', 'member_ids')) + title = text(body['title'], 100) + members = body['member_ids'] + if not isinstance(members, list) or not 1 <= len(members) <= 100: + invalid() + members = [identifier(v) for v in members] + if len(set(members)) != len(members) or actor['id'] not in members: + invalid() + with self.connection() as conn: + chat_id = uuid.uuid4() + conn.execute('INSERT INTO chats (id, title) VALUES (%s, %s)', (chat_id, title)) + # Checking after INSERT exercises rollback of the whole composite operation. + found = conn.execute('SELECT id FROM users WHERE 1=1 AND id = ANY(%s::uuid[])', (members,)).fetchall() + if len(found) != len(members): + invalid() + with conn.cursor() as cur: + cur.executemany('INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)', [(chat_id, v) for v in members]) + return self.chat_in(conn, actor, chat_id) + + def list_chats(self, actor, limit, cursor, scope): + with self.connection() as conn: + where = 'AND (c.created_at, c.id) > (%s::timestamptz, %s::uuid)' if cursor else '' + params = [actor['id']] + list(cursor or ()) + [limit + 1] + rows = conn.execute(''' + SELECT c.id + FROM chats c + JOIN chat_members cm ON cm.chat_id = c.id + WHERE 1=1 + AND cm.user_id = %s + ''' + where + ''' + ORDER BY c.created_at, c.id + LIMIT %s + ''', params).fetchall() + return page([self.chat_in(conn, actor, v['id']) for v in rows], limit, scope) + + def send(self, actor, chat_id, body): + fields(body, ('text',)) + content = text(body['text'], 4000) + with self.connection() as conn: + self.chat_in(conn, actor, chat_id) + # Row locking serializes sends per chat, including the timestamp assignment. + stamp = conn.execute(''' + UPDATE chats + SET last_message_at = GREATEST(clock_timestamp(), last_message_at + INTERVAL '1 microsecond') + WHERE 1=1 + AND id = %s + RETURNING last_message_at + ''', (chat_id,)).fetchone()['last_message_at'] + row = conn.execute(''' + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (%s, %s, %s, %s, %s) + RETURNING id, chat_id, sender_id, text, created_at + ''', (uuid.uuid4(), chat_id, actor['id'], content, stamp)).fetchone() + return public(row) + + def history(self, actor, chat_id, limit, cursor, scope): + with self.connection() as conn: + self.chat_in(conn, actor, chat_id) + where = 'AND (created_at, id) > (%s::timestamptz, %s::uuid)' if cursor else '' + params = [chat_id] + list(cursor or ()) + [limit + 1] + rows = conn.execute(''' + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE 1=1 + AND chat_id = %s + ''' + where + ''' + ORDER BY created_at, id + LIMIT %s + ''', params).fetchall() + return page([public(v) for v in rows], limit, scope) + + +def re_username(value): + import re + return re.fullmatch(r'[a-z0-9_]{3,32}', value) diff --git a/src/server.py b/src/server.py new file mode 100644 index 00000000..1bfeb059 --- /dev/null +++ b/src/server.py @@ -0,0 +1,219 @@ +"""Bounded threaded HTTP server, stdlib only in lab1.""" +import concurrent.futures +import http.server +import json +import logging +import os +import signal +import socket +import threading +import uuid +from urllib.parse import parse_qs, urlsplit + +from common import APIError, identifier, invalid, missing, page_args + +LOG = logging.getLogger('messenger') + + +def unique_object(pairs): + result = {} + for key, value in pairs: + if key in result: + invalid() + result[key] = value + return result + + +class Handler(http.server.BaseHTTPRequestHandler): + protocol_version = 'HTTP/1.1' + + def setup(self): + super().setup() + self.connection.settimeout(3) + + def log_message(self, *args): + pass + + def reply(self, status, body): + raw = b'' if status == 204 else json.dumps(body, ensure_ascii=False, allow_nan=False).encode('utf-8') + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(raw))) + self.send_header('X-Request-Id', self.request_id) + if status == 401 and self.server.store.persistent: + self.send_header('WWW-Authenticate', 'Basic realm="messenger", charset="UTF-8"' if self.route == '/api/v1/auth/sessions' else 'Bearer realm="messenger"') + self.end_headers() + self.wfile.write(raw) + + def body(self): + lengths = self.headers.get_all('Content-Length', []) + if len(lengths) != 1 or self.headers.get('Transfer-Encoding'): + invalid() + try: + length = int(lengths[0]) + except ValueError: + invalid() + if length > 32768: + raise APIError(413, 'payload_too_large', 'Максимальный JSON — 32768 байт') + if length <= 0: + invalid() + if self.headers.get_content_type() != 'application/json': + raise APIError(415, 'unsupported_media_type', 'Требуется application/json') + try: + raw = self.rfile.read(length) + if len(raw) != length: + invalid() + return json.loads(raw.decode('utf-8'), object_pairs_hook=unique_object, parse_constant=lambda _: invalid()) + except (ValueError, UnicodeError, RecursionError): + invalid() + + def dispatch(self): + self.request_id = uuid.uuid4().hex + self.route = urlsplit(self.path).path + status = 500 + try: + status, result = self.route_request() + except APIError as exc: + status = exc.status + result = {'error': {'code': exc.code, 'message': exc.message, 'request_id': self.request_id}} + except Exception: + # No bodies, credentials, tokens, SQL or exception details in logs/responses. + LOG.error('request_failed request_id=%s', self.request_id) + result = {'error': {'code': 'internal_error', 'message': 'Внутренняя ошибка', 'request_id': self.request_id}} + try: + self.reply(status, result) + except (BrokenPipeError, ConnectionResetError, TimeoutError): + pass + LOG.info('method=%s status=%s request_id=%s', self.command, status, self.request_id) + + def route_request(self): + method, path, store = self.command, self.route, self.server.store + if method == 'GET' and path in ('/health/live', '/health/ready'): + if path.endswith('ready'): + store.ready() + return 200, {'status': 'ok'} + if method == 'POST' and path == '/api/v1/users': + return 201, store.register(self.body()) + if store.persistent and method == 'POST' and path == '/api/v1/auth/sessions': + if self.headers.get('Transfer-Encoding') or self.headers.get('Content-Length', '0') != '0': + invalid() + return 201, store.login(self.headers) + actor = store.authenticate(self.headers) + if store.persistent and method == 'DELETE' and path == '/api/v1/auth/sessions/current': + store.logout(self.headers) + return 204, None + if method == 'GET' and path == '/api/v1/users/me': + return 200, actor + query = parse_qs(urlsplit(self.path).query, keep_blank_values=True) + if path == '/api/v1/chats': + if method == 'POST': + return 201, store.create_chat(actor, self.body()) + if method == 'GET': + scope = 'chats:' + actor['id'] + limit, cursor = page_args(query, scope) + return 200, store.list_chats(actor, limit, cursor, scope) + parts = path.split('/') + if len(parts) in (5, 6) and parts[1:4] == ['api', 'v1', 'chats']: + chat_id = identifier(parts[4]) + store.chat(actor, chat_id) + if len(parts) == 5 and method == 'GET': + return 200, store.chat(actor, chat_id) + if len(parts) == 6 and parts[5] == 'messages': + if method == 'POST': + return 201, store.send(actor, chat_id, self.body()) + if method == 'GET': + scope = 'messages:' + chat_id + limit, cursor = page_args(query, scope) + return 200, store.history(actor, chat_id, limit, cursor, scope) + missing() + + do_GET = do_POST = do_DELETE = do_PUT = do_PATCH = do_OPTIONS = dispatch + + +class Server(http.server.HTTPServer): + request_queue_size = 64 + allow_reuse_address = True + + def __init__(self, address, store): + self.store = store + workers = int(os.environ.get('HTTP_WORKERS', '16')) + if not 1 <= workers <= 128: + raise ValueError('HTTP_WORKERS must be 1..128') + self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=workers) + self.slots = threading.BoundedSemaphore(workers * 2) + self.connections = set() + self.connections_lock = threading.Lock() + super().__init__(address, Handler) + + def process_request(self, request, address): + if not self.slots.acquire(blocking=False): + rid = uuid.uuid4().hex + raw = json.dumps({'error': {'code': 'unavailable', 'message': 'Очередь запросов заполнена', 'request_id': rid}}).encode() + try: + request.settimeout(0.2) + request.sendall(('HTTP/1.0 503 Service Unavailable\r\nContent-Type: application/json\r\nX-Request-Id: ' + rid + '\r\nContent-Length: ' + str(len(raw)) + '\r\n\r\n').encode() + raw) + except OSError: + pass + self.shutdown_request(request) + return + with self.connections_lock: + self.connections.add(request) + self.executor.submit(self.worker, request, address) + + def worker(self, request, address): + try: + self.finish_request(request, address) + except OSError: + pass + finally: + self.shutdown_request(request) + with self.connections_lock: + self.connections.discard(request) + self.slots.release() + + def close_connections(self): + with self.connections_lock: + connections = list(self.connections) + for connection in connections: + try: + connection.shutdown(socket.SHUT_RDWR) + except OSError: + pass + + def server_close(self): + super().server_close() + # Bound draining even when a slow client keeps an incomplete request alive. + deadline = threading.Timer(5, self.close_connections) + deadline.start() + try: + self.executor.shutdown(wait=True) + finally: + deadline.cancel() + + +def main(): + logging.basicConfig(level=os.environ.get('LOG_LEVEL', 'INFO'), format='%(asctime)s %(levelname)s %(message)s') + from postgres import Store + store = Store() + server = Server((os.environ.get('APP_HOST', '0.0.0.0'), int(os.environ.get('APP_PORT', '8080'))), store) + stopping = threading.Event() + + def stop(signum, frame): + if not stopping.is_set(): + stopping.set() + LOG.info('shutdown_started signal=%s', signum) + threading.Thread(target=server.shutdown, daemon=True).start() + + signal.signal(signal.SIGTERM, stop) + signal.signal(signal.SIGINT, stop) + LOG.info('started port=%s', server.server_address[1]) + try: + server.serve_forever(poll_interval=0.1) + finally: + server.server_close() + store.close() + LOG.info('shutdown_complete') + + +if __name__ == '__main__': + main() diff --git a/student-tests/dependencies.py b/student-tests/dependencies.py new file mode 100644 index 00000000..8b040971 --- /dev/null +++ b/student-tests/dependencies.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Destructive only to this repository's disposable lab services; volumes preserved.""" +import json +import os +from pathlib import Path +import subprocess +import sys +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +os.chdir(ROOT) +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure + + +def compose(*args): + return subprocess.check_output(['docker', 'compose', *args], text=True).strip() + + +def wait_ready(client): + deadline = time.monotonic() + 45 + while True: + try: + client.call('GET', '/health/ready', schema='Health') + return + except Failure: + if time.monotonic() > deadline: + raise + time.sleep(0.3) + + +def main(): + c = Client(timeout=5) + alice, bob, eve, chat = c.bootstrap() + result = {} + query = "SELECT count(*) FROM chats;" + before = compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query) + c.call('POST', '/api/v1/chats', 400, actor=alice, body={'title': 'must rollback', 'member_ids': [alice['id'], str(uuid.uuid4())]}) + after = compose('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query) + assert before == after, 'failed chat left an orphan' + result['composite_operation_rollback'] = True + # Force a miss and observe the actual user cache and TTL, without printing its values. + compose('exec', '-T', 'redis', 'redis-cli', 'DEL', 'user:' + alice['id']) + c.call('GET', '/api/v1/users/me', actor=alice) + ttl = int(compose('exec', '-T', 'redis', 'redis-cli', 'TTL', 'user:' + alice['id'])) + assert 0 < ttl <= 60 + result['redis_user_cache_ttl_seconds'] = ttl + compose('stop', 'redis') + try: + started = time.monotonic() + c.call('GET', '/api/v1/users/me', actor=alice) + c.message(alice, chat, 'Redis unavailable; PostgreSQL remains authoritative') + c.call('GET', '/api/v1/users/me', 401, headers={'Authorization': 'Bearer ' + 'x' * 43}) + c.call('GET', '/health/ready', schema='Health') + result['redis_outage_seconds'] = time.monotonic() - started + finally: + compose('up', '-d', '--wait', 'redis') + c.call('GET', '/api/v1/users/me', actor=alice) + result['redis_recovered_without_relogin'] = True + compose('stop', 'postgres') + try: + started = time.monotonic() + c.call('GET', '/api/v1/users/me', 503, actor=alice) + result['postgres_outage_response_seconds'] = time.monotonic() - started + assert result['postgres_outage_response_seconds'] < 4 + c.call('GET', '/health/ready', 503) + c.call('GET', '/health/live', schema='Health') + finally: + compose('up', '-d', '--wait', 'postgres') + wait_ready(c) + c.call('GET', '/api/v1/users/me', actor=alice) + result['postgres_recovered_without_relogin'] = True + # A separate short-TTL API uses the same authoritative PostgreSQL without changing the main API. + name = 'mtusi-lab2-expiry-' + uuid.uuid4().hex[:8] + compose('run', '-d', '--no-deps', '--name', name, '-e', 'SESSION_TTL_SECONDS=2', '-p', '127.0.0.1:18083:8080', 'api') + try: + short = Client('http://127.0.0.1:18083') + wait_ready(short) + actor = short.user('expiry') + short.call('GET', '/api/v1/users/me', actor=actor) + time.sleep(2.2) + short.call('GET', '/api/v1/users/me', 401, actor=actor) + result['short_ttl_expired'] = True + finally: + subprocess.run(['docker', 'rm', '-f', name], check=True, stdout=subprocess.DEVNULL) + (ROOT / 'evidence/dependencies.json').write_text(json.dumps(result, indent=2)) + print('PASS: rollback, real Redis TTL/cache, Redis outage, PostgreSQL outage/recovery and session expiry.') + + +if __name__ == '__main__': + main() diff --git a/student-tests/index_study.py b/student-tests/index_study.py new file mode 100644 index 00000000..1329a95f --- /dev/null +++ b/student-tests/index_study.py @@ -0,0 +1,113 @@ +"""Runs inside the API image, against a separate synthetic research database.""" +import datetime as dt +import hashlib +import json +import os +from pathlib import Path +import statistics +import sys +import time +import uuid + +import psycopg +from psycopg import sql +from psycopg.conninfo import make_conninfo + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'src')) +from postgres import setting + + +def snapshot(conn): + counts = {} + for table in ('users', 'chats', 'chat_members', 'messages', 'sessions'): + counts[table] = conn.execute(sql.SQL('SELECT count(*) FROM {}').format(sql.Identifier(table))).fetchone()[0] + digest = hashlib.sha256() + selected = [] + with conn.cursor(name='checksum') as cur: + cur.execute('SELECT row_to_json(m)::text FROM messages m ORDER BY id') + for i, (row,) in enumerate(cur): + digest.update(row.encode() + b'\n') + if i in (0, 1, 50000, 99998, 99999): + selected.append(json.loads(row)) + return dict(counts=counts, messages_sha256=digest.hexdigest(), selected_messages=selected) + + +def main(): + url = setting('DATABASE_URL') + if '--verify' in sys.argv: + with psycopg.connect(make_conninfo(url, dbname=os.environ['RESEARCH_DATABASE'], host=os.environ['RESEARCH_HOST'])) as conn: + print(json.dumps(snapshot(conn))) + return + database = 'research_' + uuid.uuid4().hex[:12] + with psycopg.connect(url, autocommit=True) as admin: + admin.execute(sql.SQL('CREATE DATABASE {}').format(sql.Identifier(database))) + conn = psycopg.connect(make_conninfo(url, dbname=database)) + conn.execute((ROOT / 'migrations/001_initial.sql').read_text()) + user = uuid.uuid4() + conn.execute('INSERT INTO users (id, username, display_name, password_hash) VALUES (%s, %s, %s, %s)', (user, 'research_user', 'Synthetic', 'not-an-authenticatable-password')) + sizes = [70000, 20000, 5000, 2000, 1000, 800, 500, 300, 200, 200] + chats = [uuid.uuid4() for _ in sizes] + base = dt.datetime(2026, 1, 1, tzinfo=dt.timezone.utc) + for chat in chats: + conn.execute('INSERT INTO chats (id, title) VALUES (%s, %s)', (chat, 'Synthetic')) + conn.execute('INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)', (chat, user)) + with conn.cursor().copy('COPY messages (id, chat_id, sender_id, text, created_at) FROM STDIN') as copy: + for chat, count in zip(chats, sizes): + for i in range(count): + copy.write_row((uuid.uuid4(), chat, user, 'x' * 64, base + dt.timedelta(microseconds=i))) + conn.commit() + results = [] + query = ''' + SELECT + id + , chat_id + , sender_id + , text + , created_at + FROM messages + WHERE 1=1 + AND chat_id = %s + AND (created_at, id) > (%s, %s) + ORDER BY created_at, id + LIMIT 51 + ''' + for variant, columns in (('chat_only', 'chat_id'), ('composite', 'chat_id, created_at, id')): + conn.execute('DROP INDEX messages_history') + # The two SQL fragments are constants, never request input. + conn.execute('CREATE INDEX messages_history ON messages (' + columns + ')') + conn.execute('ANALYZE messages') + conn.commit() + reads = [] + for position in (0, 1, 9): + params = (chats[position], base + dt.timedelta(microseconds=sizes[position] * 4 // 5), uuid.UUID(int=0)) + conn.execute(query, params).fetchall() # Warm the same page before both variants. + plans = [conn.execute('EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ' + query, params).fetchone()[0][0] for _ in range(5)] + reads.append(dict(chat_messages=sizes[position], median_execution_ms=statistics.median(v['Execution Time'] for v in plans), plans=plans)) + conn.commit() + writes = [] + for repeat in range(3): + ids = [uuid.uuid4() for _ in range(1000)] + before = conn.execute('SELECT pg_current_wal_insert_lsn()').fetchone()[0] + conn.commit() + started = time.perf_counter() + with conn.cursor().copy('COPY messages (id, chat_id, sender_id, text, created_at) FROM STDIN') as copy: + for i, mid in enumerate(ids): + copy.write_row((mid, chats[0], user, 'write-cost', base + dt.timedelta(seconds=1, microseconds=i))) + conn.commit() + elapsed = (time.perf_counter() - started) * 1000 + wal = conn.execute('SELECT pg_wal_lsn_diff(pg_current_wal_insert_lsn(), %s::pg_lsn)', (before,)).fetchone()[0] + writes.append(dict(rows=1000, commit_ms=elapsed, wal_bytes=int(wal))) + conn.execute('DELETE FROM messages WHERE 1=1 AND id = ANY(%s::uuid[])', (ids,)) + conn.commit() + index_size = conn.execute("SELECT pg_relation_size('messages_history')").fetchone()[0] + results.append(dict(variant=variant, index_bytes=index_size, reads=reads, writes=writes)) + captured = snapshot(conn) + assert captured['counts']['messages'] == 100000 + version = conn.execute('SELECT version()').fetchone()[0] + conn.close() + print(json.dumps(dict(database=database, postgresql=version, sizes=sizes, query=query, repeats=5, results=results, snapshot=captured))) + + +if __name__ == '__main__': + main() diff --git a/student-tests/research.py b/student-tests/research.py new file mode 100644 index 00000000..7d845119 --- /dev/null +++ b/student-tests/research.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +"""Index study, pg_dump, restore into a new database, full message checksum.""" +import json +import os +from pathlib import Path +import subprocess +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +os.chdir(ROOT) +COMPOSE = ['docker', 'compose'] +RUN = COMPOSE + ['run', '--rm', '--no-deps', '-T', '-v', str(ROOT / 'student-tests') + ':/app/student-tests:ro'] + +result = json.loads(subprocess.check_output(RUN + ['api', 'python', 'student-tests/index_study.py'], text=True)) +source = result['database'] +restore = 'restore_' + uuid.uuid4().hex[:12] +restore_container = 'mtusi-lab2-' + restore +restore_volume = restore_container + '-data' +directory = ROOT / 'backups' +directory.mkdir(exist_ok=True, mode=0o700) +backup = directory / (source + '.dump') +started = time.perf_counter() +with backup.open('wb') as output: + subprocess.run(COMPOSE + ['exec', '-T', 'postgres', 'pg_dump', '-U', 'messenger', '-Fc', source], stdout=output, check=True) +backup.chmod(0o600) +dump_seconds = time.perf_counter() - started +source_id = subprocess.check_output(COMPOSE + ['ps', '-q', 'postgres'], text=True).strip() +source_info = json.loads(subprocess.check_output(['docker', 'inspect', source_id], text=True))[0] +network = next(iter(source_info['NetworkSettings']['Networks'])) +image = source_info['Config']['Image'] +subprocess.run(['docker', 'volume', 'create', restore_volume], check=True, stdout=subprocess.DEVNULL) +started_bootstrap = time.perf_counter() +restore_id = subprocess.check_output(['docker', 'run', '-d', '--name', restore_container, '--network', network, '--cpus=1', '--memory=512m', '-e', 'POSTGRES_USER=messenger', '-e', 'POSTGRES_DB=' + restore, '-e', 'POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password', '--mount', 'type=volume,src=' + restore_volume + ',dst=/var/lib/postgresql/data', '--mount', 'type=bind,src=' + str(ROOT / 'secrets/postgres_password') + ',dst=/run/secrets/postgres_password,readonly', image], text=True).strip() +try: + deadline = time.monotonic() + 45 + while subprocess.run(['docker', 'exec', restore_container, 'pg_isready', '-U', 'messenger', '-d', restore], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: + assert time.monotonic() < deadline, 'new PostgreSQL did not become ready' + time.sleep(0.3) + bootstrap_seconds = time.perf_counter() - started_bootstrap + started = time.perf_counter() + with backup.open('rb') as data: + subprocess.run(['docker', 'exec', '-i', restore_container, 'pg_restore', '-U', 'messenger', '--no-owner', '--exit-on-error', '-d', restore], stdin=data, check=True) + restore_seconds = time.perf_counter() - started + verified = json.loads(subprocess.check_output(RUN + ['-e', 'RESEARCH_DATABASE=' + restore, '-e', 'RESEARCH_HOST=' + restore_container, 'api', 'python', 'student-tests/index_study.py', '--verify'], text=True)) +finally: + subprocess.run(['docker', 'stop', '-t', '10', restore_container], check=True, stdout=subprocess.DEVNULL) +assert result['snapshot'] == verified, 'restored contents differ' +result['backup'] = dict(format='pg_dump custom', bytes=backup.stat().st_size, dump_seconds=dump_seconds, bootstrap_seconds=bootstrap_seconds, restore_seconds=restore_seconds, restored_database=restore, source_container_id=source_id[:12], restore_container_id=restore_id[:12], fresh_restore_volume=restore_volume, source_volume=[m['Name'] for m in source_info['Mounts'] if m['Type'] == 'volume'], matching_counts=True, matching_all_messages_sha256=True, matching_selected_messages=True, restored_snapshot=verified) +(ROOT / 'evidence/index-study.json').write_text(json.dumps(result, indent=2)) +print('PASS: 100000 messages; two indexes; write costs; backup/restore counts, samples and full checksum match.') +print('Backup seconds:', round(dump_seconds, 3), 'Restore seconds:', round(restore_seconds, 3)) diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..2be98ef5 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,98 @@ +"""Extra black-box tests, without changing the course's public tests.""" +import concurrent.futures +from pathlib import Path +import sys +import unittest +from urllib.parse import quote +import uuid + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client, LAB + + +class APIContract(unittest.TestCase): + def setUp(self): + self.client = Client() + self.alice, self.bob, self.eve, self.chat = self.client.bootstrap() + self.path = '/api/v1/chats/' + self.chat['id'] + + def test_malformed_json_and_extra_fields(self): + for raw in (b'{', b'[]', b'{"text":"x","text":"y"}', b'{"text":NaN}', b'{"text":"\\ud800"}'): + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, raw=raw, headers={'Content-Type': 'application/json'}) + for body in ({'text': 'ok', 'sender_id': self.eve['id']}, {'text': True}, {'text': ' '}, {'text': ''}, {'text': None}): + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, body=body) + deep = b'{"text":' + b'[' * 1200 + b'0' + b']' * 1200 + b'}' + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, raw=deep, headers={'Content-Type': 'application/json'}) + + def test_unicode_limits(self): + result = self.client.message(self.alice, self.chat, '👩' * 4000)['json'] + self.assertEqual(result['text'], '👩' * 4000) + self.client.call('POST', self.path + '/messages', 400, actor=self.alice, body={'text': '👩' * 4001}) + + def test_foreign_chat(self): + for suffix in ('', '/messages'): + self.client.call('GET', self.path + suffix, 404, actor=self.eve) + self.client.call('POST', self.path + '/messages', 404, actor=self.eve, body={'text': 'hidden'}) + self.client.call('GET', '/api/v1/chats/' + str(uuid.uuid4()), 404, actor=self.alice) + + def test_registration_race(self): + body = dict(self.alice['registration'], username='race_' + uuid.uuid4().hex[:20]) + def register(_): + return Client().call('POST', '/api/v1/users', [201, 409], body=body)['status'] + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + statuses = list(executor.map(register, range(10))) + self.assertEqual(statuses.count(201), 1) + self.assertEqual(statuses.count(409), 9) + + def test_invalid_chat_creation(self): + members = ([self.alice['id'], self.alice['id']], [self.bob['id']], [self.alice['id'], str(uuid.uuid4())], [], [True]) + for member_ids in members: + self.client.call('POST', '/api/v1/chats', 400, actor=self.alice, body={'title': 'rollback', 'member_ids': member_ids}) + + def test_cursor_scope_and_validation(self): + for i in range(3): + self.client.message(self.alice, self.chat, str(i)) + cursor = self.client.call('GET', self.path + '/messages?limit=1', actor=self.alice, schema='MessagePage')['json']['next_cursor'] + other = self.client.call('POST', '/api/v1/chats', 201, actor=self.alice, body={'title': 'other', 'member_ids': [self.alice['id']]}, schema='Chat')['json'] + self.client.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' + quote(cursor), 400, actor=self.alice) + for query in ('limit=0', 'limit=101', 'limit=' + '9' * 5000, 'limit=true', 'limit=1&limit=2', 'cursor=', 'cursor=bad', 'cursor=e30=', 'offset=1'): + self.client.call('GET', self.path + '/messages?' + query, 400, actor=self.alice) + + def test_concurrent_messages_and_pagination(self): + def send(i): + return Client().message(self.alice if i % 2 else self.bob, self.chat, 'parallel-' + str(i))['json'] + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + sent = list(executor.map(send, range(100))) + found = [] + cursor = None + while True: + url = self.path + '/messages?limit=7' + ('&cursor=' + quote(cursor) if cursor else '') + page = self.client.call('GET', url, actor=self.alice, schema='MessagePage')['json'] + found.extend(page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + self.assertEqual(len(found), 100) + self.assertEqual(len({v['id'] for v in found}), 100) + self.assertEqual({v['id'] for v in found}, {v['id'] for v in sent}) + self.assertEqual(found, sorted(found, key=lambda v: (v['created_at'], v['id']))) + + @unittest.skipUnless(LAB >= 2, 'sessions start in lab2') + def test_unicode_password_basic_and_logout(self): + import base64 + registration = {'username': 'unicode_' + uuid.uuid4().hex[:16], 'display_name': 'Софья', 'password': '🔐' * 127 + ':'} + self.client.call('POST', '/api/v1/users', 201, body=registration) + encoded = base64.b64encode((registration['username'] + ':' + registration['password']).encode()).decode() + headers = {'Authorization': 'Basic ' + encoded} + session = self.client.call('POST', '/api/v1/auth/sessions', 201, headers=headers, schema='Session')['json'] + actor = {'id': session['user']['id'], 'token': session['token']} + self.client.call('GET', '/api/v1/users/me', actor=actor) + self.client.call('GET', '/api/v1/users/me', 401, headers=headers) + for authorization in ('Basic !!!', 'Bearer wrong', '', 'Basic Zm9v'): + self.client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': authorization}) + self.client.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor) + self.client.call('GET', '/api/v1/users/me', 401, actor=actor) + + +if __name__ == '__main__': + unittest.main(verbosity=2)