From 74aa2242515a05192eceb4e3838e3eb4f032f397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BD=D0=B5=D0=BA?= <88394552+Lunatuk@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:00:25 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=202=20=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 31 +- .env.example | 5 +- .github/workflows/application.yml | 27 + Dockerfile | 28 + REPORT.lab2.md | 202 +++ compose.yaml | 66 + docs/schema.md | 22 + evidence/docker-checks.txt | 66 + evidence/query-plans.json | 2621 +++++++++++++++++++++++++++++ evidence/research.json | 1114 ++++++++++++ evidence/research.txt | 32 + migrations/001_initial.sql | 30 + requirements.txt | 29 + research/README.md | 12 + research/__init__.py | 0 research/benchmark.py | 168 ++ research/run.py | 99 ++ scripts/build.sh | 6 + scripts/check-docker.sh | 11 + scripts/check.sh | 17 + scripts/demo.sh | 27 + scripts/init_local.py | 29 + scripts/migrate.sh | 5 + scripts/research.sh | 7 + scripts/restart.sh | 6 + scripts/run-local.sh | 5 + scripts/start.sh | 7 + scripts/stop.sh | 6 + scripts/wait-ready.sh | 13 + src/__init__.py | 0 src/__main__.py | 16 + src/app.py | 148 ++ src/auth.py | 95 ++ src/config.py | 14 + src/db.py | 35 + src/errors.py | 27 + src/healthcheck.py | 15 + src/migrate.py | 39 + src/models.py | 41 + src/pagination.py | 39 + src/passwords.py | 22 + src/storage.py | 121 ++ student-tests/docker_lifecycle.py | 201 +++ student-tests/helpers.py | 39 + student-tests/test_api.py | 164 ++ student-tests/test_concurrency.py | 47 + student-tests/test_sessions.py | 51 + 47 files changed, 5783 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/application.yml create mode 100644 Dockerfile create mode 100644 REPORT.lab2.md create mode 100644 compose.yaml create mode 100644 docs/schema.md create mode 100644 evidence/docker-checks.txt create mode 100644 evidence/query-plans.json create mode 100644 evidence/research.json create mode 100644 evidence/research.txt create mode 100644 migrations/001_initial.sql create mode 100644 requirements.txt create mode 100644 research/README.md create mode 100644 research/__init__.py create mode 100644 research/benchmark.py create mode 100644 research/run.py create mode 100755 scripts/build.sh create mode 100755 scripts/check-docker.sh create mode 100755 scripts/check.sh create mode 100755 scripts/demo.sh create mode 100644 scripts/init_local.py create mode 100755 scripts/migrate.sh create mode 100755 scripts/research.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/run-local.sh create mode 100755 scripts/start.sh create mode 100755 scripts/stop.sh create mode 100755 scripts/wait-ready.sh create mode 100644 src/__init__.py create mode 100644 src/__main__.py create mode 100644 src/app.py create mode 100644 src/auth.py create mode 100644 src/config.py create mode 100644 src/db.py create mode 100644 src/errors.py create mode 100644 src/healthcheck.py create mode 100644 src/migrate.py create mode 100644 src/models.py create mode 100644 src/pagination.py create mode 100644 src/passwords.py create mode 100644 src/storage.py create mode 100644 student-tests/docker_lifecycle.py create mode 100644 student-tests/helpers.py create mode 100644 student-tests/test_api.py create mode 100644 student-tests/test_concurrency.py create mode 100644 student-tests/test_sessions.py diff --git a/.dockerignore b/.dockerignore index bc16e556..40d419ed 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 -evidence -.DS_Store +* +!Dockerfile +!requirements.txt +!src/ +!src/** +!migrations/ +!migrations/** +!research/ +!research/** +**/__pycache__/ +**/*.py[cod] diff --git a/.env.example b/.env.example index d32b9be0..49918d8a 100644 --- a/.env.example +++ b/.env.example @@ -3,11 +3,12 @@ APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +HOST_PORT=8080 SESSION_TTL_SECONDS=3600 # Choose credentials locally. File paths contain no secret values. DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres +# Imported implementation stores shared sessions in Valkey. +SESSION_STORE=valkey CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 IDEMPOTENCY_TTL_SECONDS=86400 diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 00000000..9d149345 --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,27 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read + +jobs: + docker: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - run: python -m venv .venv + - run: .venv/bin/python -m pip install -r requirements.txt + - run: ./scripts/check-docker.sh + - run: ./scripts/research.sh + - uses: actions/upload-artifact@v4 + if: always() + with: + name: lab2-evidence + path: | + evidence/docker-checks.txt + evidence/research.txt + evidence/research.json + evidence/query-plans.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..64af5489 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 + +WORKDIR /app + +COPY requirements.txt . +RUN python -m pip install --no-cache-dir -r requirements.txt \ + && python -m pip check + +RUN groupadd --gid 10001 app \ + && useradd --uid 10001 --gid app --no-create-home app + +COPY src/ ./src/ +COPY migrations/ ./migrations/ +COPY research/ ./research/ + +USER 10001:10001 +EXPOSE 8080 +STOPSIGNAL SIGTERM + +HEALTHCHECK --interval=2s --timeout=3s --start-period=5s --retries=3 \ + CMD ["python", "-m", "src.healthcheck"] + +CMD ["python", "-m", "src"] diff --git a/REPORT.lab2.md b/REPORT.lab2.md new file mode 100644 index 00000000..f3e185db --- /dev/null +++ b/REPORT.lab2.md @@ -0,0 +1,202 @@ +# Отчёт — лабораторная 2 + +- Автор, группа: Савельев Иван БВТ2304 +- Целевая оценка: **5**, уровень 3 + весь уровень 4 + **трек 2 «План запроса и восстановление»**. +- Commit/PR: TODO после публикации. Сохранённые проверки относятся к рабочим изменениям поверх коммита, указанного в evidence. +- Стек: Python 3.11.15 в образе, FastAPI 0.142.4, Uvicorn 0.54.0, Pydantic 2.13.5, Psycopg 3.3.6, PostgreSQL 17.6, Valkey 8.1.3, Argon2id. Прямые и транзитивные зависимости приложения закреплены в requirements.txt. + +## Запуск из чистого clone + +Нужны Docker Engine с Compose v2, Python 3.11, Bash и make. Для demo.sh также нужны curl и jq. Все компоненты запускаются из этого репозитория. Команды выполнять из корня: + +```sh +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements.txt +./scripts/start.sh +./scripts/check.sh +./scripts/demo.sh +``` + +start.sh создаёт .env и secrets через scripts/init_local.py, собирает API, запускает PostgreSQL и Valkey, затем ждёт готовности API. Пустая БД мигрируется при старте приложения. POST /users теперь требует password; бизнес-запросы принимают Authorization: Bearer. Адрес по умолчанию — http://localhost:8080. + +Локальный пароль PostgreSQL создаётся случайно и сохраняется в secrets/postgres_password; URL соединений — в secrets/database_url и secrets/cache_url. Значения не печатаются. Существующие файлы не перезаписываются. Каталог secrets имеет права 0700; файлы 0444, чтобы непривилегированный пользователь контейнера мог читать примонтированные secrets. Содержимое каталога исключено из Git. .env содержит настройки и пути к secrets. Его и .venv в образе нет. + +| Настройка | Использование | +| --- | --- | +| APP_HOST, APP_PORT | Адрес Uvicorn; внутри контейнера оставить 0.0.0.0:8080 | +| HOST_PORT | Порт публикации на 127.0.0.1 компьютера | +| SESSION_TTL_SECONDS | Срок новой сессии; по умолчанию 3600 секунд | +| SESSION_STORE | В этой реализации valkey | +| DATABASE_URL_FILE | Файл с PostgreSQL DSN | +| CACHE_URL_FILE | Файл с URL Valkey, который хранит сессии | +| APP_ENV | Метка учебного окружения; поведение API не переключает | + +Другой внешний порт и команды управления: + +```sh +HOST_PORT=8081 ./scripts/start.sh +BASE_URL=http://localhost:8081 ./scripts/check.sh +BASE_URL=http://localhost:8081 ./scripts/demo.sh + +docker compose logs --tail 30 api +./scripts/migrate.sh # повторное применение безопасно +./scripts/restart.sh # перезапускает API, PostgreSQL и Valkey +./scripts/stop.sh # удаляет контейнеры и сеть, сохраняет volumes +``` + +Для рестарта с нестандартным внешним портом передать то же HOST_PORT или записать его в .env. Команда `docker compose down --volumes` намеренно удаляет учебные данные текущего Compose project, включая сессии; для обычной остановки она не нужна. scripts/run-local.sh вызывает start.sh: локальный стенд lab2 включает все три сервиса. + +PostgreSQL и Valkey не публикуют порты на хост. API запускается от UID/GID 10001 и является PID 1. Лимиты: API — 1 CPU / 256 MiB, PostgreSQL — 1 CPU / 512 MiB, Valkey — 0.5 CPU / 128 MiB. Именованные volumes: postgres_data и valkey_data. Секреты API примонтированы файлами, состояние в API-контейнере не хранится. + +## Архитектура и транзакции + +Сохранены обработчики и модель пользователей/чатов/сообщений из lab1, строгая валидация, единые ошибки, X-Request-Id, пагинация и проверки доступа. Вместо словарей используется PostgreSQL, вместо X-User-Id — Bearer. Контракт, публичные tests, README и COURSE второй работы не изменялись. + +Схема спроектирована перед SQL-реализацией в [docs/schema.md](docs/schema.md). Миграция — migrations/001_initial.sql: + +| Таблица | Содержимое и ограничения | +| --- | --- | +| users | UUID PK, username UNIQUE, display_name, password_hash, created_at | +| chats | UUID PK, title, created_at | +| chat_members | PK (chat_id, user_id); FK на пользователей и чаты | +| messages | UUID PK; chat_id; sender_id; text; created_at; составной FK на участника чата | +| schema_migrations | Версия, SHA-256 и время применения миграции | + +Одна функция записи в storage.py — одна транзакция. Контекст соединения делает COMMIT при успехе и ROLLBACK при исключении. Ответ 201 формируется после выхода из транзакции. Уникальность username гарантирует ограничение PostgreSQL. Создание чата сначала вставляет чат, затем всех участников: FK violation откатывает и чат, и уже вставленных участников. Проверка доступа и запись сообщения находятся в одной транзакции; составной FK дополнительно требует членства отправителя. + +Все значения клиента передаются параметрами `%s`, без подстановки в SQL. Динамические имена индексов в исследовании выбираются только из фиксированных вариантов программы. Пул API: от 1 до 5 соединений, не более 20 ожидающих; ожидание пула — 2 с, connect_timeout — 2 с, statement_timeout — 3 с, lock_timeout — 2 с, idle_in_transaction_session_timeout — 5 с. При недоступности PostgreSQL возвращается общий 503. + +Миграции выполняются в транзакции под advisory lock. Повторная версия пропускается, checksum обнаруживает редактирование уже применённого SQL. Повторный запуск не создаёт новые пользовательские данные. Новое изменение схемы оформляется следующим SQL-файлом. + +## Пароли и сессии + +password имеет длину 12–128 Unicode code points и не возвращается в User. В БД сохраняется Argon2id с собственной случайной солью: memory_cost=19456 KiB, time_cost=2, parallelism=1. Это конфигурация 19 MiB / 2 / 1 из [OWASP Password Storage](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html). Не более двух вычислений хеша одновременно ограничивают расход памяти. + +POST /api/v1/auth/sessions принимает только Basic без тела. Пароль декодируется как UTF-8; разделение по первому двоеточию позволяет использовать двоеточие в пароле. Неверный пароль и неизвестное имя дают одинаковый 401 с WWW-Authenticate: Basic. Бизнес-запросы требуют Bearer и при 401 возвращают WWW-Authenticate: Bearer. X-User-Id игнорируется и сам по себе доступа не даёт. + +Токен содержит 32 случайных байта, представленных непрозрачной строкой. Valkey хранит session:SHA256(token) → user_id и expires_at с TTL. Токен не является username/UUID; его исходное значение не сохраняется в ключе. Чтение сессии не продлевает срок. Абсолютный expires_at дополнительно проверяется приложением. DELETE /auth/sessions/current удаляет только текущую сессию, возвращает пустой 204; повторное использование — 401. + +Valkey — источник истины для сессий. Включены AOF, appendfsync always, постоянный volume и noeviction. Поэтому обычный рестарт не сбрасывает ещё действующие сессии. При недоступности Valkey возвращается 503, перехода на доверие к X-User-Id или анонимному запросу нет. Ошибка подключения ограничена таймаутом 0.5 с, автоматические повторы отключены. Истечение TTL и logout не требуют инвалидации отдельного кэша. + +/health/live показывает доступность процесса; /health/ready проверяет PostgreSQL и Valkey. Docker healthcheck проверяет HTTP 200 и JSON status=ok. При остановке зависимости live остаётся 200, ready возвращает 503. + +## Пагинация и индекс истории + +Запрос истории выбирает из БД только `limit + 1` записей после пары `(created_at, id)`, упорядоченных по той же паре. Индекс messages_history_idx(chat_id, created_at, id) соответствует равенству по чату и диапазону по ключу. Наличие опорной записи курсора проверяется отдельным индексным запросом; полная история в Python не загружается. + +Курсор содержит scope, время и UUID в Base64. Scope привязан к чату или списку чатов пользователя. Чужой, повреждённый или несуществующий anchor даёт 400. limit — 1..100, по умолчанию 50; изменение limit при продолжении допустимо. Неучастник и отсутствующий чат возвращают одинаковый 404. + +Каждая страница отражает отдельное чтение. Общего snapshot между HTTP-запросами нет. Неизменная история обходится без повторов; конкурентная запись с ключом меньше уже выданного курсора может не попасть на следующие страницы. Время берётся из часов PostgreSQL, поэтому остаётся ограничение при переводе системных часов назад. + +## Проверки уровней 3 и 4 + +```sh +# На работающем стенде: структура, публичный smoke и 23 собственных теста +./scripts/check.sh + +# Явный persistence-тест с прежним токеном; сохраняет volumes +make test-persistence PYTHON=.venv/bin/python ACTION_SCRIPT=scripts/restart.sh + +# Полный прогон в отдельном временном Compose project, включая отказы и короткий TTL +./scripts/check-docker.sh + +# Полная валидация исходного OpenAPI +.venv/bin/python -m pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python +``` + +check-docker.sh собирает образ и запускает student-tests/docker_lifecycle.py. Проверка использует случайный порт и свой Compose project; в конце удаляет только его контейнеры и тестовые volumes. Пользовательский стенд не перезапускается. Вывод автоматически сохраняется в evidence/docker-checks.txt; вывод сборки остаётся в терминале. + +| Критерий | Наблюдение | Доказательство | +| --- | --- | --- | +| API lab2 и объявленный переход от lab1 | Публичный smoke прошёл; Basic/Bearer, Unicode, доступ и ошибки проверены | evidence/docker-checks.txt | +| Собственные отрицательные тесты и пагинация | 23 теста прошли, включая неверный JSON, поля, страницы и чужой cursor | evidence/docker-checks.txt | +| Конкурентная запись | 10 клиентов × 10 сообщений; ровно 100 уникальных подтверждённых записей | evidence/docker-checks.txt | +| Гонка регистрации | Одинаковый username: одна 201, девять 409 | test_concurrency.py, evidence/docker-checks.txt | +| Откат составной операции | После FK violation нет строки chats с проверяемым title | evidence/docker-checks.txt | +| Миграции с нуля и повторно | Пустой volume; повторный запуск дважды, одна версия и прежнее число сообщений | evidence/docker-checks.txt | +| Полный рестарт с прежней сессией | Публичный persistence прошёл; StartedAt изменился у всех трёх сервисов | evidence/docker-checks.txt | +| Срок сессии, logout, неверный пароль | 2-секундная сессия истекла; отзыв одной сессии не затронул другую; Basic с неверным паролем — 401 | test_sessions.py, evidence/docker-checks.txt | +| Недоступность Valkey и PostgreSQL | Доверенный запрос — 503, анонимный — 401, live 200 / ready 503; после восстановления прежний токен работает | evidence/docker-checks.txt | +| Индекс истории на ≥10000 сообщений | EXPLAIN на 100000: Index Scan по составному индексу, без полного сканирования каждой страницы | evidence/query-plans.json | +| Условия Docker из lab1 | non-root, PID 1, healthcheck, логи без паролей/токенов/сообщений; завершение по SIGTERM <10 с | evidence/docker-checks.txt | + +SIGTERM проверяется с незавершённым HTTP-запросом. Новые соединения должны перестать приниматься до выхода процесса. Uvicorn ждёт текущие запросы до 5 с; весь тест требует <10 с. Конкретные времена сохранены в evidence и зависят от машины. + +## Трек 5: план запроса и восстановление + +**Гипотеза.** Индекс только по chat_id не обеспечивает порядок истории: PostgreSQL приходится отбирать и сортировать множество записей. Индекс (chat_id, created_at, id) позволяет сразу перейти к anchor и прочитать небольшую страницу; ожидаем выигрыш чтения ценой большего индекса и дополнительных затрат записи. + +Повторить весь эксперимент: + +```sh +./scripts/research.sh +``` + +Никакой соседний репозиторий и подготовленная БД не требуются. Скрипт создаёт отдельный Compose project с двумя PostgreSQL-контейнерами и разными новыми volumes. Основной стенд и его данные не используются. SQL-измерения выполняет research/benchmark.py внутри API-образа; управление контейнерами, backup и restore — research/run.py. Прямой запуск benchmark требует специальной отметки из драйвера и пустой БД. + +Методика: + +1. Создать схему из миграции, 10 синтетических пользователей, 4 чата и 100000 сообщений с распределением **70000 / 20000 / 9000 / 1000**. Данные и UUID детерминированы; хеш пароля синтетических аккаунтов создаётся Argon2id, пароль не выводится. +2. Сравнить индекс A `(chat_id)` и индекс B `(chat_id, created_at, id)`. PK сообщений и остальные ограничения одинаковы. В каждом из трёх повторов таблица сообщений очищается только в исследовательской БД, затем индекс создаётся до записи. Порядок A/B чередуется. +3. Измерить один INSERT SELECT на 100000 строк с COMMIT, объём WAL и размер исследуемого индекса. Генерация UUID, текстов и проверки FK входят в стоимость записи обоих вариантов. +4. После VACUUM ANALYZE для каждого чата прочитать страницы около 10%, 50% и 90% истории. На каждый случай — один прогрев и пять измерений; страница 51 строка, как API для limit=50 с проверкой продолжения. Всего 180 измерений на вариант. SQL параметризован, OFFSET в исследуемом запросе нет. +5. Сохранить EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) для 24 случаев первого повтора и времена остальных повторов. +6. Сделать pg_dump в custom format. После завершения dump записать ещё одно сообщение в источник. Восстановить backup через pg_restore --exit-on-error --single-transaction в другой контейнер с новым volume. +7. Сравнить счётчики всех таблиц, 12 выбранных сообщений и SHA-256 всех полей всех 100000 сообщений в порядке UUID. Подтвердить отсутствие записи, сделанной после backup. + +### Результаты реального прогона + +Проверено 22.10.2026: macOS arm64, Docker Engine 29.0.1, Linux/aarch64, PostgreSQL 17.6. Docker VM: 10 CPU, 8217448448 байт RAM. Для каждой БД установлен одинаковый лимит 1 CPU / 512 MiB, для Python-клиента — 1 CPU / 256 MiB. Нагрузка — последовательные SQL-запросы с concurrency=1 и прогретыми страницами. Ошибок измеряемых запросов — 0. + +| Метрика | Только chat_id | chat_id, created_at, id | +| --- | ---: | ---: | +| p50 чтения, мс | 1.685 | 0.226 | +| p95 чтения, мс | 7.566 | 0.297 | +| p99 чтения, мс | 46.402 | 0.338 | +| Медиана записи 100000, мс | 1386.76 | 1404.49 | +| Размер индекса, байт | 671744 | 7315456 | +| Медиана WAL записи, байт | 38822560 | 43108512 | +| Успешных последовательных SQL/с | 282.0 | 4322.8 | + +Медиана чтения уменьшилась примерно в 7.45 раза. Время массовой записи отличается на 1.28% в этом прогоне; трёх повторов недостаточно для универсального вывода о стоимости записи. Составной индекс заметно больше, и WAL записи также увеличился. + +Для чата на 70000 сообщений около середины истории план A использовал Parallel Seq Scan и сортировку, 2443 shared hit blocks; план B — Index Scan messages_history_idx, 6 shared hit blocks. Время EXPLAIN было 7.042 мс против 0.019 мс. Это отдельный пример плана, а не медиана всех измерений. + +В research.json сохранён снимок docker stats после SQL-измерений: PostgreSQL занимал около 63.24 MiB из 512 MiB. Это посленагрузочный снимок, не измерение пикового потребления. p50/p95/p99 объединяют страницы разных размеров чатов и позиций; подробные значения сохранены отдельно. successful_sql_rps вычислен по сумме времени успешных последовательных SQL-запросов, без прогревов и EXPLAIN: это не HTTP throughput и не предел производительности системы. + +### Backup и восстановление + +Custom dump размером 3067615 байт создан за 0.54 с. pg_restore завершился за 0.68 с; запуск проверки восстановленных данных и сверка заняли ещё 2.34 с. Весь сценарий, включая создание исследовательских контейнеров и SQL-измерения, до cleanup занял 22.90 с. + +Источник и приёмник имели разные container ID и разные volume, что записано в research.json. Восстановлены 10 пользователей, 4 чата, 8 участников, 100000 сообщений и одна миграция. Совпали все счётчики, все поля 12 образцов и SHA-256 всей истории. В источнике после backup было 100001 сообщение; дополнительная запись отсутствует в восстановленной БД, как и ожидается для снимка. + +Граница потери данных: pg_dump сохраняет согласованный снимок на момент начала backup. Записи после этого снимка без WAL-архива/репликации не восстанавливаются. Показанная потеря одной последующей записи — явный пример этой границы. COMMIT и стандартные fsync/synchronous_commit PostgreSQL обеспечивают локальную фиксацию в пределах гарантий диска и Docker VM; один host не даёт защиты от потери самого host. + +Измеренное время pg_restore относится к уже работающему пустому PostgreSQL. Полный RTO включает также создание среды, проверки и переключение API. Переключение рабочего API на восстановленную БД в эксперименте не выполнялось. Сессии находятся в отдельном Valkey AOF-volume и не входят в SQL dump; для полного аварийного восстановления сервиса их резервирование требуется отдельно. Проверка обычного рестарта Valkey с сохранённым volume выполнена в основном прогоне. + +### Файлы доказательств и повторяемость + +- evidence/docker-checks.txt — автоматически перезаписывается check-docker.sh; публичные и собственные тесты, рестарты, отказы, TTL и SIGTERM. +- evidence/research.txt — краткий реальный stdout исследования, автоматически перезаписывается research.sh. +- evidence/research.json — все повторы, латентности, стоимость записи, счётчики, SHA-256, ID контейнеров/volumes и времена backup/restore. +- evidence/query-plans.json — реальные JSON-планы PostgreSQL. +- backups/*.dump — локальные dump-файлы, исключённые из Git; содержат только синтетическую исследовательскую БД. Контейнеры и volumes временного research project удаляются в finally. Рабочие volumes обычного стенда сохраняются. + +При повторе числа могут измениться. PostgreSQL работает в Docker VM, исследуются прогретые данные на одной машине. Холодный старт диска, сетевые задержки между машинами, высокая конкуренция и другие распределения данных не измерялись. Гипотеза подтверждена для проверенных страниц и распределения, универсального ускорения для любых запросов не заявляется. + +## CI и дальнейшее развитие + +Исходный workflow contracts.yml проверяет контракт. application.yml собирает приложение, выполняет check-docker.sh и research.sh, затем сохраняет evidence как артефакт. Локальные команды выполнены; успешный удалённый запуск CI не заявлен без отдельного результата GitHub Actions. + +Код остаётся разделён по назначению: app.py — HTTP, models.py — поля, storage.py — SQL, db.py — пул, auth.py — сессии, passwords.py — Argon2id, pagination.py — курсор, migrate.py — миграции. Исследование находится отдельно в research/. HTTP-контракт и публичные проверки не ослаблялись. + +Ограничения: одна API-реплика, один PostgreSQL и один Valkey без HA; нет лимита количества сообщений; сессии теряются при намеренном удалении их volume; сроки жизни зависят от системных часов. Для следующих лабораторных можно сохранить SQL-хранилище, сессии и API, добавляя требуемые контрактом возможности. + +## Источники + +- [Пул и транзакции Psycopg](https://www.psycopg.org/psycopg3/docs/advanced/pool.html). +- [Составные индексы PostgreSQL](https://www.postgresql.org/docs/17/indexes-multicolumn.html). +- [Согласованный SQL dump и восстановление PostgreSQL](https://www.postgresql.org/docs/17/backup-dump.html). +- [Valkey persistence](https://valkey.io/topics/persistence/) и [TTL](https://valkey.io/commands/expire/). +- [Argon2-cffi](https://argon2-cffi.readthedocs.io/en/stable/howto.html) и [параметры password KDF OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html). diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..1d392383 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,66 @@ +name: mtusi-lab3 + +x-postgres: &postgres + image: postgres:17.6-bookworm + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: [postgres_password] + cpus: 1 + mem_limit: 512m + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 2s + timeout: 3s + retries: 30 + +services: + postgres: + <<: *postgres + volumes: [postgres_data:/var/lib/postgresql/data] + + valkey: + image: valkey/valkey:8.1.3 + command: ["valkey-server", "--appendonly", "yes", "--appendfsync", "always", "--save", "", "--maxmemory", "64mb", "--maxmemory-policy", "noeviction"] + volumes: [valkey_data:/data] + cpus: 0.5 + mem_limit: 128m + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 2s + timeout: 3s + retries: 30 + + api: + image: mtusi-messenger:lab3 + build: . + env_file: .env + environment: + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + secrets: [database_url, cache_url] + ports: ["127.0.0.1:${HOST_PORT:-8080}:8080"] + cpus: 1 + mem_limit: 256m + depends_on: + postgres: {condition: service_healthy} + valkey: {condition: service_healthy} + stop_grace_period: 10s + + postgres-restore: + <<: *postgres + profiles: [research] + volumes: [restore_data:/var/lib/postgresql/data] + +volumes: + postgres_data: + valkey_data: + restore_data: + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url diff --git a/docs/schema.md b/docs/schema.md new file mode 100644 index 00000000..c7d08fb0 --- /dev/null +++ b/docs/schema.md @@ -0,0 +1,22 @@ +# Схема и транзакции lab2 + +Архитектура: HTTP → FastAPI → PostgreSQL; Bearer-сессии находятся в Valkey. +Выбран трек 2 на оценку 5: план запроса и восстановление. + +| Таблица PostgreSQL | Назначение и ограничения | +| --- | --- | +| users | UUID PK; username UNIQUE; display_name; password_hash Argon2id; created_at timestamptz | +| chats | UUID PK; title; created_at timestamptz | +| chat_members | PK (chat_id, user_id); FK на chats и users | +| messages | UUID PK; FK chat_id на chats; составной FK (chat_id, sender_id) на chat_members; text; created_at timestamptz | +| schema_migrations | Имя и SHA-256 применённой SQL-миграции; время применения | + +Индексы: users(username) создаётся UNIQUE-ограничением; chat_members(user_id, chat_id) нужен для списка чатов пользователя; messages(chat_id, created_at, id) нужен для истории с keyset cursor. PK служат индексами поиска по UUID. + +Каждая операция хранилища выполняется в одной транзакции: контекст соединения Psycopg делает COMMIT при успехе и ROLLBACK при исключении. Создание чата сначала вставляет chats, затем участников. Неизвестный участник вызывает FK violation и откатывает весь чат. Проверка уникальности username выполняется ограничением БД, включая конкурентные запросы. Создание сообщения проверяет доступ и записывает сообщение в одной транзакции; составной FK дополнительно запрещает автора вне чата. + +Миграции выполняются под advisory transaction lock. Повторное применение пропускает уже записанную версию, изменение применённого SQL выявляется по checksum. Схема и запись версии фиксируются вместе. + +Valkey — источник истины только для сессий: ключ session:SHA256(token), значение — user_id и абсолютное expires_at, TTL задаётся SESSION_TTL_SECONDS. Сырой токен выдаётся клиенту один раз и не записывается в логи. Включены AOF, appendfsync always и именованный volume. Logout удаляет ключ; исчезнувшая или просроченная сессия даёт 401. При недоступности Valkey — 503 без обхода аутентификации. При потере PostgreSQL — 503. /health/live остаётся доступным, /health/ready проверяет оба сервиса. + +Для трека 5 отдельно исследуются индекс только по chat_id и составной индекс на одинаковых 100000 сообщениях с распределением 70000/20000/9000/1000. Измеряются чтение страниц, стоимость записи и размер индекса. Backup восстанавливается в другой контейнер PostgreSQL с новым volume; сравниваются счётчики, выбранные записи и контрольная сумма всей истории. diff --git a/evidence/docker-checks.txt b/evidence/docker-checks.txt new file mode 100644 index 00000000..0ab4442a --- /dev/null +++ b/evidence/docker-checks.txt @@ -0,0 +1,66 @@ +UTC: 2026-10-22T11:17:22.562634+00:00 +Host: Darwin arm64 Python 3.11.9 +Docker: 29.0.1 aarch64 CPUs=10 RAM=8217448448 +Commit: 9961362 + working tree +Command: ./scripts/check-docker.sh; project: lab2-check-ae280d00db +PASS: start.sh built and started the stack, readiness reached +api image=sha256:0b3dc5056cced642eea5ade11d235dce9f69f0d9e6d63956ce9479f2deaa10a0 started=2026-10-22T11:17:27.057932632Z memory=268435456 nano_cpus=1000000000 +postgres image=sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3 started=2026-10-22T11:17:24.411773881Z memory=536870912 nano_cpus=1000000000 +valkey image=sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571 started=2026-10-22T11:17:24.406509756Z memory=134217728 nano_cpus=500000000 +PASS: non-root UID 10001; Python PID 1; healthcheck; named volumes; no API data volume +Проверяем сервер: http://127.0.0.1:61900 +/Users/vanek/Desktop/VNPlabs/VNPlab2/mtusi2026lab2/.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +/Users/vanek/Desktop/VNPlabs/VNPlab2/mtusi2026lab2/.venv/bin/python tests/smoke.py --base-url "http://127.0.0.1:61900" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. +test_chat_access (test_api.ApiTests.test_chat_access) ... ok +test_chat_pagination_and_cursor_owner (test_api.ApiTests.test_chat_pagination_and_cursor_owner) ... ok +test_cursor_cannot_be_used_for_another_chat_or_changed (test_api.ApiTests.test_cursor_cannot_be_used_for_another_chat_or_changed) ... ok +test_default_page_size (test_api.ApiTests.test_default_page_size) ... ok +test_duplicate_username (test_api.ApiTests.test_duplicate_username) ... ok +test_framework_errors_and_request_ids (test_api.ApiTests.test_framework_errors_and_request_ids) ... ok +test_identification_and_authentication_order (test_api.ApiTests.test_identification_and_authentication_order) ... ok +test_invalid_chat_members (test_api.ApiTests.test_invalid_chat_members) ... ok +test_invalid_chat_title (test_api.ApiTests.test_invalid_chat_title) ... ok +test_invalid_limits_and_cursors (test_api.ApiTests.test_invalid_limits_and_cursors) ... ok +test_invalid_message_fields (test_api.ApiTests.test_invalid_message_fields) ... ok +test_invalid_user_fields (test_api.ApiTests.test_invalid_user_fields) ... ok +test_malformed_json_and_encoding (test_api.ApiTests.test_malformed_json_and_encoding) ... ok +test_message_pagination_and_changing_limit (test_api.ApiTests.test_message_pagination_and_changing_limit) ... ok +test_unicode_and_text_preservation (test_api.ApiTests.test_unicode_and_text_preservation) ... ok +test_wrong_content_type (test_api.ApiTests.test_wrong_content_type) ... ok +test_simultaneous_registration_of_one_username (test_concurrency.ConcurrencyTests.test_simultaneous_registration_of_one_username) ... ok +test_ten_clients_send_one_hundred_messages (test_concurrency.ConcurrencyTests.test_ten_clients_send_one_hundred_messages) ... ok +test_logout_revokes_only_one_session (test_sessions.SessionTests.test_logout_revokes_only_one_session) ... ok +test_password_validation (test_sessions.SessionTests.test_password_validation) ... ok +test_sql_like_message_is_plain_text (test_sessions.SessionTests.test_sql_like_message_is_plain_text) ... ok +test_unicode_password_with_colon_and_private_response (test_sessions.SessionTests.test_unicode_password_with_colon_and_private_response) ... ok +test_wrong_password_and_basic_only_for_login (test_sessions.SessionTests.test_wrong_password_and_basic_only_for_login) ... ok + +---------------------------------------------------------------------- +Ran 23 tests in 5.743s + +OK +PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages. +PASS: demo.sh registered users, logged in and read two messages +PASS: Argon2id in PostgreSQL; logs do not contain message text, passwords or session tokens +PASS: failed membership insert rolled back chat; migrations run twice without duplicate data +/Users/vanek/Desktop/VNPlabs/VNPlab2/mtusi2026lab2/.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:61900" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +Restart api 2026-10-22T11:17:27.057932632Z -> 2026-10-22T11:17:45.460071626Z +Restart postgres 2026-10-22T11:17:24.411773881Z -> 2026-10-22T11:17:44.637353001Z +Restart valkey 2026-10-22T11:17:24.406509756Z -> 2026-10-22T11:17:44.670095293Z +PASS: API, PostgreSQL and Valkey really restarted; previous token still works +PASS: valkey unavailable -> 503 in 0.11s; anonymous -> 401; readiness 503, liveness 200 +API ready +PASS: valkey recovered without a new login +PASS: postgres unavailable -> 503 in 2.01s; anonymous -> 401; readiness 503, liveness 200 +API ready +PASS: postgres recovered without a new login +API ready +PASS: SESSION_TTL_SECONDS=2; valid token expires without logout +PASS: SIGTERM with unfinished request: 5.54s; new requests refused after 0.16s; exit=0; OOM=false +PASS: all lab2 Docker checks completed +PASS: stop.sh removed containers and preserved named volumes +Removed only temporary Compose project and its test volumes: lab2-check-ae280d00db diff --git a/evidence/query-plans.json b/evidence/query-plans.json new file mode 100644 index 00000000..25f478d6 --- /dev/null +++ b/evidence/query-plans.json @@ -0,0 +1,2621 @@ +[ + { + "variant": "chat_only", + "chat_size": 70000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5732.59, + "Total Cost": 5738.45, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 7.421, + "Actual Total Time": 8.522, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5732.59, + "Total Cost": 10128.12, + "Plan Rows": 38222, + "Plan Width": 160, + "Actual Startup Time": 7.421, + "Actual Total Time": 8.508, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 1, + "Workers Launched": 1, + "Shared Hit Blocks": 2443, + "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": 4732.58, + "Total Cost": 4828.13, + "Plan Rows": 38222, + "Plan Width": 160, + "Actual Startup Time": 6.621, + "Actual Total Time": 6.624, + "Actual Rows": 51, + "Actual Loops": 2, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3457.41, + "Plan Rows": 38222, + "Plan Width": 160, + "Actual Startup Time": 0.215, + "Actual Total Time": 4.164, + "Actual Rows": 31500, + "Actual Loops": 2, + "Filter": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.007+00'::timestamp with time zone, '9cc0c1db-51d1-5bf2-e5a4-83cb93303974'::uuid)))", + "Rows Removed by Filter": 18500, + "Shared Hit Blocks": 2428, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.031, + "Triggers": [], + "Execution Time": 8.532 + } + }, + { + "variant": "chat_only", + "chat_size": 70000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5354.06, + "Total Cost": 5359.93, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 5.792, + "Actual Total Time": 7.033, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 5354.06, + "Total Cost": 8444.8, + "Plan Rows": 26876, + "Plan Width": 160, + "Actual Startup Time": 5.792, + "Actual Total Time": 7.028, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 1, + "Workers Launched": 1, + "Shared Hit Blocks": 2443, + "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": 4354.05, + "Total Cost": 4421.24, + "Plan Rows": 26876, + "Plan Width": 160, + "Actual Startup Time": 4.884, + "Actual Total Time": 4.886, + "Actual Rows": 40, + "Actual Loops": 2, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3457.41, + "Plan Rows": 26876, + "Plan Width": 160, + "Actual Startup Time": 1.017, + "Actual Total Time": 3.623, + "Actual Rows": 17500, + "Actual Loops": 2, + "Filter": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.035+00'::timestamp with time zone, '169ee7a7-ca0d-aa05-bd8c-a1eb275708e7'::uuid)))", + "Rows Removed by Filter": 32500, + "Shared Hit Blocks": 2428, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.038, + "Triggers": [], + "Execution Time": 7.042 + } + }, + { + "variant": "chat_only", + "chat_size": 70000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4967.83, + "Total Cost": 4973.69, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 25.942, + "Actual Total Time": 27.248, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Gather Merge", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 4967.83, + "Total Cost": 6727.21, + "Plan Rows": 15299, + "Plan Width": 160, + "Actual Startup Time": 25.942, + "Actual Total Time": 27.243, + "Actual Rows": 51, + "Actual Loops": 1, + "Workers Planned": 1, + "Workers Launched": 1, + "Shared Hit Blocks": 2443, + "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": 3967.82, + "Total Cost": 4006.07, + "Plan Rows": 15299, + "Plan Width": 160, + "Actual Startup Time": 14.755, + "Actual Total Time": 14.763, + "Actual Rows": 26, + "Actual Loops": 2, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "top-N heapsort", + "Sort Space Used": 38, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 2443, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [ + { + "Worker Number": 0, + "Sort Method": "quicksort", + "Sort Space Used": 25, + "Sort Space Type": "Memory" + } + ], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Parallel Aware": true, + "Async Capable": false, + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.0, + "Total Cost": 3457.41, + "Plan Rows": 15299, + "Plan Width": 160, + "Actual Startup Time": 13.398, + "Actual Total Time": 14.487, + "Actual Rows": 3500, + "Actual Loops": 2, + "Filter": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.063+00'::timestamp with time zone, '987963b5-9cc6-d246-e263-52373e7f4b96'::uuid)))", + "Rows Removed by Filter": 46500, + "Shared Hit Blocks": 2428, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Workers": [] + } + ] + } + ] + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.047, + "Triggers": [], + "Execution Time": 27.257 + } + }, + { + "variant": "chat_only", + "chat_size": 20000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 3191.11, + "Total Cost": 3191.24, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 3.535, + "Actual Total Time": 3.542, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 506, + "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": 3191.11, + "Total Cost": 3205.3, + "Plan Rows": 5677, + "Plan Width": 160, + "Actual Startup Time": 3.534, + "Actual Total Time": 3.538, + "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": 506, + "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": 220.91, + "Total Cost": 3001.71, + "Plan Rows": 5677, + "Plan Width": 160, + "Actual Startup Time": 0.442, + "Actual Total Time": 2.23, + "Actual Rows": 18000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.072+00'::timestamp with time zone, '97b900c6-4a0c-f323-4d98-d97c50488136'::uuid))", + "Rows Removed by Filter": 2000, + "Exact Heap Blocks": 489, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 506, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 219.49, + "Plan Rows": 20160, + "Plan Width": 0, + "Actual Startup Time": 0.29, + "Actual Total Time": 0.291, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Shared Hit Blocks": 17, + "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.043, + "Triggers": [], + "Execution Time": 3.549 + } + }, + { + "variant": "chat_only", + "chat_size": 20000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 3136.93, + "Total Cost": 3137.05, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 2.648, + "Actual Total Time": 2.654, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 506, + "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": 3136.93, + "Total Cost": 3147.09, + "Plan Rows": 4065, + "Plan Width": 160, + "Actual Startup Time": 2.647, + "Actual Total Time": 2.65, + "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": 506, + "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": 220.51, + "Total Cost": 3001.31, + "Plan Rows": 4065, + "Plan Width": 160, + "Actual Startup Time": 0.94, + "Actual Total Time": 1.93, + "Actual Rows": 10000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.08+00'::timestamp with time zone, '9042bcb2-df86-667b-9e54-e6e6c0c7923a'::uuid))", + "Rows Removed by Filter": 10000, + "Exact Heap Blocks": 489, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 506, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 219.49, + "Plan Rows": 20160, + "Plan Width": 0, + "Actual Startup Time": 0.276, + "Actual Total Time": 0.276, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Shared Hit Blocks": 17, + "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.03, + "Triggers": [], + "Execution Time": 2.66 + } + }, + { + "variant": "chat_only", + "chat_size": 20000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 3081.5, + "Total Cost": 3081.63, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 1.813, + "Actual Total Time": 1.819, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 506, + "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": 3081.5, + "Total Cost": 3087.54, + "Plan Rows": 2416, + "Plan Width": 160, + "Actual Startup Time": 1.813, + "Actual Total Time": 1.816, + "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": 506, + "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": 220.1, + "Total Cost": 3000.9, + "Plan Rows": 2416, + "Plan Width": 160, + "Actual Startup Time": 1.459, + "Actual Total Time": 1.667, + "Actual Rows": 2000, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.088+00'::timestamp with time zone, '81028714-cb0a-8e0f-6d74-4c379351efc2'::uuid))", + "Rows Removed by Filter": 18000, + "Exact Heap Blocks": 489, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 506, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 219.49, + "Plan Rows": 20160, + "Plan Width": 0, + "Actual Startup Time": 0.277, + "Actual Total Time": 0.277, + "Actual Rows": 20000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid)", + "Shared Hit Blocks": 17, + "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.027, + "Triggers": [], + "Execution Time": 1.827 + } + }, + { + "variant": "chat_only", + "chat_size": 9000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2710.71, + "Total Cost": 2710.84, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 1.58, + "Actual Total Time": 1.586, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 230, + "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": 2710.71, + "Total Cost": 2712.76, + "Plan Rows": 818, + "Plan Width": 160, + "Actual Startup Time": 1.58, + "Actual Total Time": 1.582, + "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": 230, + "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": 99.37, + "Total Cost": 2683.42, + "Plan Rows": 818, + "Plan Width": 160, + "Actual Startup Time": 0.192, + "Actual Total Time": 1.001, + "Actual Rows": 8100, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0909+00'::timestamp with time zone, 'b4d17acc-f297-d5d5-746e-2ef777eaa364'::uuid))", + "Rows Removed by Filter": 900, + "Exact Heap Blocks": 221, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 230, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 99.17, + "Plan Rows": 8917, + "Plan Width": 0, + "Actual Startup Time": 0.121, + "Actual Total Time": 0.121, + "Actual Rows": 9000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Shared Hit Blocks": 9, + "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.021, + "Triggers": [], + "Execution Time": 1.592 + } + }, + { + "variant": "chat_only", + "chat_size": 9000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2699.82, + "Total Cost": 2699.95, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 1.21, + "Actual Total Time": 1.216, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 230, + "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": 2699.82, + "Total Cost": 2701.06, + "Plan Rows": 494, + "Plan Width": 160, + "Actual Startup Time": 1.209, + "Actual Total Time": 1.212, + "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": 230, + "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": 99.29, + "Total Cost": 2683.34, + "Plan Rows": 494, + "Plan Width": 160, + "Actual Startup Time": 0.437, + "Actual Total Time": 0.889, + "Actual Rows": 4500, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0945+00'::timestamp with time zone, '64cc6094-35b0-52bc-2c33-655d810cbabc'::uuid))", + "Rows Removed by Filter": 4500, + "Exact Heap Blocks": 221, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 230, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 99.17, + "Plan Rows": 8917, + "Plan Width": 0, + "Actual Startup Time": 0.121, + "Actual Total Time": 0.121, + "Actual Rows": 9000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Shared Hit Blocks": 9, + "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.02, + "Triggers": [], + "Execution Time": 1.222 + } + }, + { + "variant": "chat_only", + "chat_size": 9000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 2689.0, + "Total Cost": 2689.13, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.834, + "Actual Total Time": 0.84, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 230, + "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": 2689.0, + "Total Cost": 2689.43, + "Plan Rows": 172, + "Plan Width": 160, + "Actual Startup Time": 0.834, + "Actual Total Time": 0.836, + "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": 230, + "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": 99.21, + "Total Cost": 2683.26, + "Plan Rows": 172, + "Plan Width": 160, + "Actual Startup Time": 0.661, + "Actual Total Time": 0.766, + "Actual Rows": 900, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0981+00'::timestamp with time zone, '8de6f8f9-25ec-97e3-e3b7-59eac1d2fd4c'::uuid))", + "Rows Removed by Filter": 8100, + "Exact Heap Blocks": 221, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 230, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 99.17, + "Plan Rows": 8917, + "Plan Width": 0, + "Actual Startup Time": 0.12, + "Actual Total Time": 0.12, + "Actual Rows": 9000, + "Actual Loops": 1, + "Index Cond": "(chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid)", + "Shared Hit Blocks": 9, + "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.035, + "Triggers": [], + "Execution Time": 0.846 + } + }, + { + "variant": "chat_only", + "chat_size": 1000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1993.42, + "Total Cost": 1993.45, + "Plan Rows": 11, + "Plan Width": 160, + "Actual Startup Time": 0.192, + "Actual Total Time": 0.198, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "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": 1993.42, + "Total Cost": 1993.45, + "Plan Rows": 11, + "Plan Width": 160, + "Actual Startup Time": 0.192, + "Actual Total Time": 0.194, + "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": 27, + "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": 12.62, + "Total Cost": 1993.23, + "Plan Rows": 11, + "Plan Width": 160, + "Actual Startup Time": 0.024, + "Actual Total Time": 0.125, + "Actual Rows": 900, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0991+00'::timestamp with time zone, 'a1580a3c-2826-e768-e4ea-f6c5a0e7171b'::uuid))", + "Rows Removed by Filter": 100, + "Exact Heap Blocks": 25, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 27, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 12.62, + "Plan Rows": 1110, + "Plan Width": 0, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.015, + "Actual Rows": 1000, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Shared Hit Blocks": 2, + "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.02, + "Triggers": [], + "Execution Time": 0.204 + } + }, + { + "variant": "chat_only", + "chat_size": 1000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1993.31, + "Total Cost": 1993.32, + "Plan Rows": 6, + "Plan Width": 160, + "Actual Startup Time": 0.139, + "Actual Total Time": 0.145, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "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": 1993.31, + "Total Cost": 1993.32, + "Plan Rows": 6, + "Plan Width": 160, + "Actual Startup Time": 0.139, + "Actual Total Time": 0.141, + "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": 27, + "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": 12.62, + "Total Cost": 1993.23, + "Plan Rows": 6, + "Plan Width": 160, + "Actual Startup Time": 0.051, + "Actual Total Time": 0.1, + "Actual Rows": 500, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0995+00'::timestamp with time zone, '86263544-3ca9-88ac-6a18-01fe3ed3d579'::uuid))", + "Rows Removed by Filter": 500, + "Exact Heap Blocks": 25, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 27, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 12.62, + "Plan Rows": 1110, + "Plan Width": 0, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.015, + "Actual Rows": 1000, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Shared Hit Blocks": 2, + "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.019, + "Triggers": [], + "Execution Time": 0.151 + } + }, + { + "variant": "chat_only", + "chat_size": 1000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 1993.24, + "Total Cost": 1993.24, + "Plan Rows": 1, + "Plan Width": 160, + "Actual Startup Time": 0.095, + "Actual Total Time": 0.101, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 27, + "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": 1993.24, + "Total Cost": 1993.24, + "Plan Rows": 1, + "Plan Width": 160, + "Actual Startup Time": 0.095, + "Actual Total Time": 0.097, + "Actual Rows": 51, + "Actual Loops": 1, + "Sort Key": [ + "created_at", + "id" + ], + "Sort Method": "quicksort", + "Sort Space Used": 50, + "Sort Space Type": "Memory", + "Shared Hit Blocks": 27, + "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": 12.62, + "Total Cost": 1993.23, + "Plan Rows": 1, + "Plan Width": 160, + "Actual Startup Time": 0.077, + "Actual Total Time": 0.086, + "Actual Rows": 100, + "Actual Loops": 1, + "Recheck Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Rows Removed by Index Recheck": 0, + "Filter": "(ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, 'dc2ec078-4b31-5bfc-d9ac-d4e6671f761b'::uuid))", + "Rows Removed by Filter": 900, + "Exact Heap Blocks": 25, + "Lossy Heap Blocks": 0, + "Shared Hit Blocks": 27, + "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_chat_idx", + "Startup Cost": 0.0, + "Total Cost": 12.62, + "Plan Rows": 1110, + "Plan Width": 0, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.015, + "Actual Rows": 1000, + "Actual Loops": 1, + "Index Cond": "(chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid)", + "Shared Hit Blocks": 2, + "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.02, + "Triggers": [], + "Execution Time": 0.106 + } + }, + { + "variant": "history", + "chat_size": 70000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 10.63, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 13039.29, + "Plan Rows": 65139, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.007+00'::timestamp with time zone, '9cc0c1db-51d1-5bf2-e5a4-83cb93303974'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.021 + } + }, + { + "variant": "history", + "chat_size": 70000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 13.82, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 11922.0, + "Plan Rows": 45372, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.035+00'::timestamp with time zone, '169ee7a7-ca0d-aa05-bd8c-a1eb275708e7'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "variant": "history", + "chat_size": 70000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 21.74, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 10814.04, + "Plan Rows": 25860, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.01, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.063+00'::timestamp with time zone, '987963b5-9cc6-d246-e263-52373e7f4b96'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "variant": "history", + "chat_size": 20000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 88.93, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.014, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 9665.82, + "Plan Rows": 5569, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.072+00'::timestamp with time zone, '97b900c6-4a0c-f323-4d98-d97c50488136'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.018 + } + }, + { + "variant": "history", + "chat_size": 20000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 111.12, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.013, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 8668.14, + "Plan Rows": 3993, + "Plan Width": 160, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.08+00'::timestamp with time zone, '9042bcb2-df86-667b-9e54-e6e6c0c7923a'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.016, + "Triggers": [], + "Execution Time": 0.018 + } + }, + { + "variant": "history", + "chat_size": 20000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 134.87, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.013, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 6322.47, + "Plan Rows": 2398, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '096d532a-b923-0f13-8dc9-243f47b54ac3'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.088+00'::timestamp with time zone, '81028714-cb0a-8e0f-6d74-4c379351efc2'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.018 + } + }, + { + "variant": "history", + "chat_size": 9000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 171.67, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.013, + "Actual Rows": 51, + "Actual Loops": 1, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_history_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 2737.17, + "Plan Rows": 815, + "Plan Width": 160, + "Actual Startup Time": 0.003, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0909+00'::timestamp with time zone, 'b4d17acc-f297-d5d5-746e-2ef777eaa364'::uuid)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 6, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + }, + "Planning": { + "Shared Hit Blocks": 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.017, + "Triggers": [], + "Execution Time": 0.018 + } + }, + { + "variant": "history", + "chat_size": 9000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 182.11, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.013, + "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_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 1767.48, + "Plan Rows": 496, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0945+00'::timestamp with time zone, '64cc6094-35b0-52bc-2c33-655d810cbabc'::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.019, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "variant": "history", + "chat_size": 9000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 194.46, + "Plan Rows": 51, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.013, + "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_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 635.83, + "Plan Rows": 167, + "Plan Width": 160, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = '35bbad77-7634-ca61-4fd8-5d37539c23b8'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0981+00'::timestamp with time zone, '8de6f8f9-25ec-97e3-e3b7-59eac1d2fd4c'::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.021, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "variant": "history", + "chat_size": 1000, + "position": 0.1, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 39.4, + "Plan Rows": 9, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "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_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 39.4, + "Plan Rows": 9, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0991+00'::timestamp with time zone, 'a1580a3c-2826-e768-e4ea-f6c5a0e7171b'::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.02, + "Triggers": [], + "Execution Time": 0.019 + } + }, + { + "variant": "history", + "chat_size": 1000, + "position": 0.5, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 23.92, + "Plan Rows": 5, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.014, + "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_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 23.92, + "Plan Rows": 5, + "Plan Width": 160, + "Actual Startup Time": 0.005, + "Actual Total Time": 0.009, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0995+00'::timestamp with time zone, '86263544-3ca9-88ac-6a18-01fe3ed3d579'::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.018, + "Triggers": [], + "Execution Time": 0.018 + } + }, + { + "variant": "history", + "chat_size": 1000, + "position": 0.9, + "plan": { + "Plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 160, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.012, + "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_idx", + "Relation Name": "messages", + "Alias": "messages", + "Startup Cost": 0.42, + "Total Cost": 8.44, + "Plan Rows": 1, + "Plan Width": 160, + "Actual Startup Time": 0.004, + "Actual Total Time": 0.008, + "Actual Rows": 51, + "Actual Loops": 1, + "Index Cond": "((chat_id = 'a3c16613-13f1-93e3-af30-521782e6eb21'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.0999+00'::timestamp with time zone, 'dc2ec078-4b31-5bfc-d9ac-d4e6671f761b'::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.017, + "Triggers": [], + "Execution Time": 0.017 + } + } +] \ No newline at end of file diff --git a/evidence/research.json b/evidence/research.json new file mode 100644 index 00000000..5074a7a0 --- /dev/null +++ b/evidence/research.json @@ -0,0 +1,1114 @@ +{ + "postgres": "PostgreSQL 17.6 (Debian 17.6-2.pgdg12+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14+deb12u1) 12.2.0, 64-bit", + "messages": 100000, + "chat_sizes": [ + 70000, + 20000, + 9000, + 1000 + ], + "repeats": 3, + "warmup_per_case": 1, + "measurements_per_case": 5, + "page_size": 51, + "query": "SELECT * FROM messages WHERE chat_id=%s AND (created_at, id)>(%s, %s)\n ORDER BY created_at, id LIMIT %s", + "summary": { + "chat_only": { + "insert_100000_median_ms": 1386.7587500000127, + "index_bytes": 671744, + "wal_bytes_median": 38822560, + "queries": 180, + "errors": 0, + "concurrency": 1, + "p50_ms": 1.6852079999978287, + "p95_ms": 7.566457999985232, + "p99_ms": 46.40233399999261, + "successful_sql_rps": 282.01364182715855 + }, + "history": { + "insert_100000_median_ms": 1404.4910420000178, + "index_bytes": 7315456, + "wal_bytes_median": 43108512, + "queries": 180, + "errors": 0, + "concurrency": 1, + "p50_ms": 0.22610399997802233, + "p95_ms": 0.29704099995342403, + "p99_ms": 0.3384169999662845, + "successful_sql_rps": 4322.810483886352 + } + }, + "trials": [ + { + "variant": "chat_only", + "repeat": 1, + "insert_100000_ms": 1386.7587500000127, + "wal_bytes": 38822584, + "index_bytes": 671744, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 6.914874999949916, + 6.668166999986624, + 6.523834000006445, + 6.556250000016917, + 6.811083000002327 + ], + "explain_execution_ms": 8.532 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 46.40233399999261, + 6.385249999993903, + 6.055750000030002, + 7.862708000004659, + 7.713500000022577 + ], + "explain_execution_ms": 7.042 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 6.462749999968764, + 6.372040999963247, + 6.377625000027365, + 6.360333999964496, + 6.293291999952544 + ], + "explain_execution_ms": 27.257 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 2.6799170000231243, + 2.6539589999856616, + 2.6237499999979264, + 2.6159999999890715, + 2.5921669999888763 + ], + "explain_execution_ms": 3.549 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 2.2941670000022896, + 2.2702500000377768, + 2.252708000014536, + 2.271042000018042, + 2.3105829999963134 + ], + "explain_execution_ms": 2.66 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 1.9595829999730086, + 1.9207080000001042, + 2.059125000016593, + 2.0110829999566704, + 2.0071669999879305 + ], + "explain_execution_ms": 1.827 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 1.3095409999550611, + 1.3007080000306814, + 1.288000000045031, + 1.280042000018966, + 1.2995419999697333 + ], + "explain_execution_ms": 1.592 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 1.156374999993659, + 1.1407089999693198, + 1.144167000006746, + 1.1433750000264808, + 1.1405409999838412 + ], + "explain_execution_ms": 1.222 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 1.0064999999599422, + 1.003166999964833, + 1.002000000028147, + 0.9964999999851898, + 0.9997909999697185 + ], + "explain_execution_ms": 0.846 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.3318340000078024, + 0.34162500003276364, + 0.3277920000073209, + 0.32820799998489747, + 0.33399999995253893 + ], + "explain_execution_ms": 0.204 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.31758300002593387, + 0.31320800002276883, + 0.3052920000072845, + 0.32208299995772904, + 0.31487500001503577 + ], + "explain_execution_ms": 0.151 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.29320900000584516, + 0.29254200001105346, + 0.30541699999275806, + 0.2934999999979482, + 0.2922080000189453 + ], + "explain_execution_ms": 0.106 + } + ] + }, + { + "variant": "history", + "repeat": 1, + "insert_100000_ms": 1404.4910420000178, + "wal_bytes": 43108512, + "index_bytes": 7315456, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 0.3173750000087239, + 0.29504200000474157, + 0.28329200000598576, + 0.3037079999899106, + 0.31616600000461403 + ], + "explain_execution_ms": 0.021 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 0.2926249999859465, + 0.21762500000477303, + 0.19337500003757668, + 0.21075000000791988, + 0.23008300001947646 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 0.2326670000343256, + 0.22991600002342238, + 0.25349999998525163, + 0.2272909999874173, + 0.2270420000058948 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 0.2562920000173108, + 0.23475000000416912, + 0.22645799998599614, + 0.22308400002657436, + 0.2378749999820684 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 0.22491599997920275, + 0.25087500000609, + 0.22708300002705073, + 0.2274999999940519, + 0.22325000003320383 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 0.2279589999716336, + 0.22745899997289598, + 0.22599999999783904, + 0.2297500000167929, + 0.22641599997541562 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 0.24199999995744292, + 0.2252499999713109, + 0.22495799998978327, + 0.22799999999278953, + 0.2394169999888618 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 0.2292909999823678, + 0.3384169999662845, + 0.28125000000045475, + 0.24829200003750884, + 0.23558299994874687 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 0.2306659999931071, + 0.2268749999529973, + 0.29704099995342403, + 0.24354200002107973, + 0.23233300004221746 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.2479999999991378, + 0.2283750000060536, + 0.22741699996231546, + 0.2274999999940519, + 0.24287499996944462 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.22462499998709973, + 0.24941599997418962, + 0.2247919999831538, + 0.22466699999768025, + 0.2243339999949967 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.22287500001993976, + 0.2249590000360513, + 0.22291699997367687, + 0.2342500000054315, + 0.23004100000889594 + ], + "explain_execution_ms": 0.017 + } + ] + }, + { + "variant": "history", + "repeat": 2, + "insert_100000_ms": 1417.234876000009, + "wal_bytes": 43108512, + "index_bytes": 7315456, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 0.3070409999850199, + 0.2651249999985339, + 0.2804580000201895, + 0.2466660000095544, + 0.4540420000012091 + ], + "explain_execution_ms": 0.021 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 0.30112500002132947, + 0.23020800000495, + 0.24920800001382304, + 0.26024999999663123, + 0.23162500002626984 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 0.2297500000167929, + 0.22941700001410936, + 0.2268339999886848, + 0.2509999999915635, + 0.2265840000177377 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 0.24954200000593119, + 0.22820899999942412, + 0.22675000002436718, + 0.22470900000826077, + 0.22833299999547307 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 0.2232080000226233, + 0.22866699998758122, + 0.23858299999801602, + 0.22420799996325513, + 0.22699999999531428 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 0.2425420000236045, + 0.22604199995157614, + 0.22554200000968194, + 0.22283300000935924, + 0.24158299999044175 + ], + "explain_execution_ms": 0.025 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 0.23279099997353114, + 0.23029199996926764, + 0.32120800000257077, + 0.26729199998953845, + 0.2517920000286722 + ], + "explain_execution_ms": 0.019 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 0.23408300000937743, + 0.2288339999836353, + 0.23687499998459316, + 0.2249590000360513, + 0.2247919999831538 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 0.2243749999593092, + 0.22408299997778158, + 0.22312500004773028, + 0.23924999999280772, + 0.22150000000920045 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.22354100002530686, + 0.2302089999943746, + 0.22079199999325283, + 0.2222910000000411, + 0.2202919999945152 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.22287500001993976, + 0.22287499996309634, + 0.22041700003683218, + 0.2310830000169517, + 0.22616600000446851 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.22241599998551465, + 0.22308299998030634, + 0.22195799999735755, + 0.21995800000240706, + 0.21950000001424996 + ], + "explain_execution_ms": 0.017 + } + ] + }, + { + "variant": "chat_only", + "repeat": 2, + "insert_100000_ms": 1390.132249999965, + "wal_bytes": 38822560, + "index_bytes": 671744, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 7.372583000005761, + 6.758875000002718, + 6.808458000023165, + 6.906250000042746, + 7.094250000022839 + ], + "explain_execution_ms": 40.541 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 6.415666999998848, + 6.257875000017066, + 6.178666999971938, + 8.023291000029076, + 7.650417000036214 + ], + "explain_execution_ms": 7.198 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 42.71466599999485, + 6.7938749999711945, + 6.601541000009092, + 6.471875000045202, + 6.461500000000342 + ], + "explain_execution_ms": 5.685 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 2.8667090000453754, + 2.8354170000284284, + 2.8501250000090295, + 2.787916999977824, + 2.8024999999729516 + ], + "explain_execution_ms": 3.787 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 2.3539160000041193, + 2.3988750000398795, + 2.3642079999603993, + 2.349291000030007, + 2.33629199999541 + ], + "explain_execution_ms": 2.82 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 1.9656659999895965, + 1.9535420000238446, + 1.9440419999909864, + 1.940166999986559, + 1.9436249999671418 + ], + "explain_execution_ms": 1.83 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 1.3667079999777343, + 1.36754199996858, + 1.3556669999843507, + 1.3793749999990723, + 1.357499999983247 + ], + "explain_execution_ms": 1.715 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 1.1639999999601969, + 1.1638330000209862, + 1.1650000000145155, + 1.1809580000203823, + 1.1649159999933545 + ], + "explain_execution_ms": 1.275 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 0.9753749999958927, + 0.9995409999987714, + 1.014125000040167, + 0.9871670000052291, + 0.9951249999744505 + ], + "explain_execution_ms": 0.854 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.3327910000052725, + 0.3331660000185366, + 0.3437500000131877, + 0.3285419999770056, + 0.3382080000164933 + ], + "explain_execution_ms": 0.22 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.3201250000302025, + 0.33404200001996287, + 0.31708299997035283, + 0.31666699999277625, + 0.3300420000300619 + ], + "explain_execution_ms": 0.156 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.3005840000014359, + 0.28595899999572794, + 0.2849999999625652, + 0.2990409999483745, + 0.2847920000021986 + ], + "explain_execution_ms": 0.106 + } + ] + }, + { + "variant": "chat_only", + "repeat": 3, + "insert_100000_ms": 1330.3022509999778, + "wal_bytes": 38822560, + "index_bytes": 671744, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 48.38283300000512, + 7.059540999989622, + 6.774750000033691, + 7.226040999967154, + 7.049875000006978 + ], + "explain_execution_ms": 8.497 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 5.816499999980351, + 5.940875000021606, + 52.47504099997968, + 7.940541999971629, + 7.566457999985232 + ], + "explain_execution_ms": 6.953 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 6.405041999983041, + 6.38737500003117, + 6.312415999957466, + 6.295207999983177, + 6.2736250000057225 + ], + "explain_execution_ms": 5.548 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 2.6689579999583657, + 2.6040839999836862, + 2.6041659999691547, + 2.593499999989035, + 2.596417000006568 + ], + "explain_execution_ms": 11.285 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 2.3674579999806156, + 2.497582999978931, + 2.4268750000260297, + 2.543584000022747, + 2.3092499999961547 + ], + "explain_execution_ms": 2.655 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 1.982666000003519, + 1.9526250000012624, + 1.9421659999920848, + 2.0057080000128735, + 1.990875000046799 + ], + "explain_execution_ms": 1.878 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 1.4241659999925105, + 1.449707999995553, + 1.3514999999983957, + 1.3168329999757589, + 1.3098750000040127 + ], + "explain_execution_ms": 1.607 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 1.1759579999761627, + 1.1762500000145337, + 1.1795839999990676, + 1.1871670000118684, + 1.1549170000080267 + ], + "explain_execution_ms": 1.221 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 0.9988330000396672, + 1.0168339999836462, + 1.0056670000153645, + 0.9815419999767983, + 0.9922919999780788 + ], + "explain_execution_ms": 0.831 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.33158299999058727, + 0.32433300003731347, + 0.3312079999773232, + 0.3266250000137916, + 0.3368750000163345 + ], + "explain_execution_ms": 0.194 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.3318330000183778, + 0.3219579999722555, + 0.3167920000350932, + 0.317624999979671, + 0.3145420000123522 + ], + "explain_execution_ms": 0.154 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.29337500001247463, + 0.29699999998911153, + 0.3068749999783904, + 0.309750000042186, + 0.29841700001043137 + ], + "explain_execution_ms": 0.107 + } + ] + }, + { + "variant": "history", + "repeat": 3, + "insert_100000_ms": 1366.9856260000302, + "wal_bytes": 43108536, + "index_bytes": 7315456, + "queries": [ + { + "chat_size": 70000, + "position": 0.1, + "latency_ms": [ + 0.29145799999241717, + 0.25487500005283437, + 0.24249999995618055, + 0.24029200000086348, + 0.40070800002922624 + ], + "explain_execution_ms": 0.02 + }, + { + "chat_size": 70000, + "position": 0.5, + "latency_ms": [ + 0.24608299997908034, + 0.2166659999716103, + 0.19137499998578278, + 0.20683299999291194, + 0.18729199996414536 + ], + "explain_execution_ms": 0.034 + }, + { + "chat_size": 70000, + "position": 0.9, + "latency_ms": [ + 0.1954580000074202, + 0.19033400002399503, + 0.1846659999955591, + 0.20504200000459605, + 0.1987920000487975 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 20000, + "position": 0.1, + "latency_ms": [ + 0.1877920000197264, + 0.2019580000478527, + 0.1839579999796115, + 0.18112499998323983, + 0.18541700001151185 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 20000, + "position": 0.5, + "latency_ms": [ + 0.19970799996826827, + 0.18504199999824777, + 0.18249999999397915, + 0.17937500001607987, + 0.1814999999965039 + ], + "explain_execution_ms": 0.016 + }, + { + "chat_size": 20000, + "position": 0.9, + "latency_ms": [ + 0.18754100000251128, + 0.18725000001040826, + 0.18091699996602983, + 0.22283300000935924, + 0.20837499999970532 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 9000, + "position": 0.1, + "latency_ms": [ + 0.1893749999908323, + 0.20050000000537693, + 0.18170799995687048, + 0.1799160000359734, + 0.19962499999337524 + ], + "explain_execution_ms": 0.018 + }, + { + "chat_size": 9000, + "position": 0.5, + "latency_ms": [ + 0.2502920000324593, + 0.22045800000114468, + 0.21791599999687605, + 0.2180409999823496, + 0.23691699999517368 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 9000, + "position": 0.9, + "latency_ms": [ + 0.21720799998092843, + 0.21795899999688118, + 0.21658399998614186, + 0.2168750000350883, + 0.21812500000351065 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 1000, + "position": 0.1, + "latency_ms": [ + 0.21791700004314407, + 0.2171250000060354, + 0.21858299999166775, + 0.21612500000856016, + 0.22783299999673545 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 1000, + "position": 0.5, + "latency_ms": [ + 0.2180000000180371, + 0.21766600002592895, + 0.21808299999293013, + 0.2169169999888254, + 0.2186660000234042 + ], + "explain_execution_ms": 0.017 + }, + { + "chat_size": 1000, + "position": 0.9, + "latency_ms": [ + 0.21783400001140762, + 0.21633299996892674, + 0.2234579999935704, + 0.21850000001677472, + 0.2274999999940519 + ], + "explain_execution_ms": 0.017 + } + ] + } + ], + "snapshot": { + "counts": { + "users": 10, + "chats": 4, + "chat_members": 8, + "messages": 100000, + "schema_migrations": 1 + }, + "messages_sha256": "36958977cf7f00bc537cb8e2b0052fd83cc8358d0d44d72304d0856d1cf43870", + "samples": [ + { + "chat_id": "fb372790-4b73-5a45-c97c-7e4287fa05be", + "created_at": "2026-01-01 00:00:00.000001+00:00", + "id": "0406791b-bf11-c4be-113d-e56c41008a52", + "sender_id": "fbb798c2-5241-0201-2e7b-d22eef00e52d", + "text": "Synthetic message 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "fb372790-4b73-5a45-c97c-7e4287fa05be", + "created_at": "2026-01-01 00:00:00.035000+00:00", + "id": "169ee7a7-ca0d-aa05-bd8c-a1eb275708e7", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 35000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "fb372790-4b73-5a45-c97c-7e4287fa05be", + "created_at": "2026-01-01 00:00:00.070000+00:00", + "id": "dbf177a7-e559-4eb5-2f16-d7594a0c375b", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 70000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "096d532a-b923-0f13-8dc9-243f47b54ac3", + "created_at": "2026-01-01 00:00:00.070001+00:00", + "id": "042eb2e1-c673-28b2-57e6-bbc531f9f886", + "sender_id": "fbb798c2-5241-0201-2e7b-d22eef00e52d", + "text": "Synthetic message 70001 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "096d532a-b923-0f13-8dc9-243f47b54ac3", + "created_at": "2026-01-01 00:00:00.080000+00:00", + "id": "9042bcb2-df86-667b-9e54-e6e6c0c7923a", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 80000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "096d532a-b923-0f13-8dc9-243f47b54ac3", + "created_at": "2026-01-01 00:00:00.090000+00:00", + "id": "5409a1ee-b7e3-f2fa-1b87-173c59924f20", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 90000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "35bbad77-7634-ca61-4fd8-5d37539c23b8", + "created_at": "2026-01-01 00:00:00.090001+00:00", + "id": "8a98c150-4c98-8f1a-478f-2d01e8725584", + "sender_id": "fbb798c2-5241-0201-2e7b-d22eef00e52d", + "text": "Synthetic message 90001 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "35bbad77-7634-ca61-4fd8-5d37539c23b8", + "created_at": "2026-01-01 00:00:00.094500+00:00", + "id": "64cc6094-35b0-52bc-2c33-655d810cbabc", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 94500 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "35bbad77-7634-ca61-4fd8-5d37539c23b8", + "created_at": "2026-01-01 00:00:00.099000+00:00", + "id": "26f56eb2-d617-f971-efa9-fc332ae1272b", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 99000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "a3c16613-13f1-93e3-af30-521782e6eb21", + "created_at": "2026-01-01 00:00:00.099001+00:00", + "id": "7d565f77-f911-6eb9-62a9-dcf414afb5f3", + "sender_id": "fbb798c2-5241-0201-2e7b-d22eef00e52d", + "text": "Synthetic message 99001 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "a3c16613-13f1-93e3-af30-521782e6eb21", + "created_at": "2026-01-01 00:00:00.099500+00:00", + "id": "86263544-3ca9-88ac-6a18-01fe3ed3d579", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 99500 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + }, + { + "chat_id": "a3c16613-13f1-93e3-af30-521782e6eb21", + "created_at": "2026-01-01 00:00:00.100000+00:00", + "id": "103bbef2-c217-19c4-d209-a1c1cdd3a1e2", + "sender_id": "bdb1dd10-5679-979c-a82b-28edd1c8ccd2", + "text": "Synthetic message 100000 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + } + ] + }, + "utc": "2026-10-22T11:09:43.745407+00:00", + "project": "lab2-research-85021a09d9", + "command": "./scripts/research.sh", + "host": "Darwin arm64", + "commit": "9961362 + working tree", + "postgres_limits": { + "cpus": 1, + "memory_mib": 512 + }, + "api_runner_limits": { + "cpus": 1, + "memory_mib": 256 + }, + "source": { + "container": "c0b5e13dc5d2fd8bbd488a9b227eb62e4fa6a3896f6b28cf464b94e3d8f9032f", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "volumes": [ + "lab2-research-85021a09d9_postgres_data" + ] + }, + "restored": { + "container": "1c23bc29d590048481d8e2d18dfe340627309f20f004d4ffdb8892c3582fb8f1", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "volumes": [ + "lab2-research-85021a09d9_restore_data" + ] + }, + "docker": "29.0.1 aarch64 CPUs=10 RAM=8217448448", + "stats_after_queries": "{\"BlockIO\":\"131kB / 583MB\",\"CPUPerc\":\"0.05%\",\"Container\":\"c0b5e13dc5d2fd8bbd488a9b227eb62e4fa6a3896f6b28cf464b94e3d8f9032f\",\"ID\":\"c0b5e13dc5d2\",\"MemPerc\":\"12.35%\",\"MemUsage\":\"63.24MiB / 512MiB\",\"Name\":\"lab2-research-85021a09d9-postgres-1\",\"NetIO\":\"378kB / 33.3MB\",\"PIDs\":\"6\"}", + "backup": { + "file": "backups/lab2-research-85021a09d9.dump", + "bytes": 3067615, + "seconds": 0.5438075830461457 + }, + "restore": { + "seconds": 0.6752373329363763, + "verification_seconds": 2.342134208069183, + "verification": { + "counts": { + "users": 10, + "chats": 4, + "chat_members": 8, + "messages": 100000, + "schema_migrations": 1 + }, + "messages_sha256": "36958977cf7f00bc537cb8e2b0052fd83cc8358d0d44d72304d0856d1cf43870", + "samples_verified": 12, + "all_messages_match": true, + "after_backup_message_missing_in_restore": true, + "source_messages": 100001 + }, + "post_backup_insert": { + "id": "9b10784e-e462-01b4-6520-1c572ae115d9", + "source_messages": 100001 + } + }, + "total_seconds": 22.895743250031956 +} \ No newline at end of file diff --git a/evidence/research.txt b/evidence/research.txt new file mode 100644 index 00000000..ef6bb5e7 --- /dev/null +++ b/evidence/research.txt @@ -0,0 +1,32 @@ +Research project: lab2-research-85021a09d9 +Measuring two indexes on the same 100000 messages, three repeats... +{ + "chat_only": { + "insert_100000_median_ms": 1386.7587500000127, + "index_bytes": 671744, + "wal_bytes_median": 38822560, + "queries": 180, + "errors": 0, + "concurrency": 1, + "p50_ms": 1.6852079999978287, + "p95_ms": 7.566457999985232, + "p99_ms": 46.40233399999261, + "successful_sql_rps": 282.01364182715855 + }, + "history": { + "insert_100000_median_ms": 1404.4910420000178, + "index_bytes": 7315456, + "wal_bytes_median": 43108512, + "queries": 180, + "errors": 0, + "concurrency": 1, + "p50_ms": 0.22610399997802233, + "p95_ms": 0.29704099995342403, + "p99_ms": 0.3384169999662845, + "successful_sql_rps": 4322.810483886352 + } +} +PASS: backup 0.54s; restore 0.68s; 100000 messages, all hashes and 12 samples match +PASS: restored into a different container and fresh volume; one post-backup commit is absent as expected +Results: evidence/research.json, evidence/query-plans.json; dump stays outside Git in backups/ +Removed only temporary research project: lab2-research-85021a09d9 diff --git a/migrations/001_initial.sql b/migrations/001_initial.sql new file mode 100644 index 00000000..9dfbb863 --- /dev/null +++ b/migrations/001_initial.sql @@ -0,0 +1,30 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name varchar(100) NOT NULL CHECK (char_length(display_name) >= 1), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title varchar(100) NOT NULL CHECK (char_length(title) >= 1), + created_at timestamptz NOT NULL DEFAULT clock_timestamp() +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id), + PRIMARY KEY (chat_id, user_id) +); +CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id uuid NOT NULL, + text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000 AND text !~ '^\s*$'), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id) +); +CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id); diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..82eccad4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,29 @@ +# Основные библиотеки +fastapi==0.142.4 +uvicorn==0.54.0 +pydantic==2.13.5 + +# Их зависимости +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +click==8.5.0 +h11==0.16.0 +idna==3.20 +opentelemetry-api==1.45.1 +pydantic-core==2.46.5 +starlette==1.7.0 +typing-extensions==4.16.0 +typing-inspection==0.4.4 + +# PostgreSQL, сессии и Argon2id +psycopg[binary,pool]==3.3.6 +redis==8.1.0 +argon2-cffi==25.1.0 + +# Транзитивные зависимости новых библиотек +psycopg-binary==3.3.6 +psycopg-pool==3.3.3 +argon2-cffi-bindings==26.1.0 +cffi==2.1.1 +pycparser==3.11 diff --git a/research/README.md b/research/README.md new file mode 100644 index 00000000..956721c4 --- /dev/null +++ b/research/README.md @@ -0,0 +1,12 @@ +# Трек 2 на оценку 5 + +Запуск из корня: `./scripts/research.sh`. Нужны Docker Compose, Python 3 и интернет для первой сборки. Скрипт собирает API-образ и использует отдельный Compose project с новыми volumes. Текущий стенд не участвует. + +- run.py управляет контейнерами, pg_dump, pg_restore, файлами evidence и cleanup. +- benchmark.py выполняет SQL и сверку внутри API-образа. Перед заполнением требует пустую БД. Исследовательский флаг устанавливает только драйвер. + +Датасет: 100000 сообщений в 4 чатах (70000/20000/9000/1000). Сравниваются индекс chat_id и индекс (chat_id,created_at,id), по 3 повтора с одинаковыми ограничениями и записями. На каждый чат — страницы около 10/50/90%, один прогрев и пять измерений, 51 строка. Измеряется INSERT SELECT с COMMIT, WAL и размер индекса. EXPLAIN и основные цифры сохраняются в evidence. + +После dump создаётся ещё одно сообщение. Backup восстанавливается в другой контейнер и свежий volume. Сравниваются все счётчики, 12 полных записей и SHA-256 всех сообщений. Последующая запись ожидаемо отсутствует в восстановленном снимке. + +Файлы research.json, query-plans.json и research.txt обновляются автоматически. Dump остаётся в игнорируемом backups/. Временные исследовательские volumes удаляются после проверки. Методика, фактические результаты и ограничения лабораторной 2 описаны в [REPORT.lab2.md](../REPORT.lab2.md). diff --git a/research/__init__.py b/research/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/research/benchmark.py b/research/benchmark.py new file mode 100644 index 00000000..93b51fe4 --- /dev/null +++ b/research/benchmark.py @@ -0,0 +1,168 @@ +"""SQL-эксперимент запускается только в отдельном research Compose project.""" +from datetime import datetime, timedelta, timezone +import hashlib +import json +import os +import secrets +import statistics +import sys +import time +from uuid import UUID + +import psycopg +from psycopg.conninfo import make_conninfo +from psycopg.rows import dict_row + +from src import db, migrate, passwords +from src.config import secret + + +SIZES = [70000, 20000, 9000, 1000] +START = datetime(2026, 1, 1, tzinfo=timezone.utc) +QUERY = """SELECT * FROM messages WHERE chat_id=%s AND (created_at, id)>(%s, %s) + ORDER BY created_at, id LIMIT %s""" + + +def identifier(value): + return UUID(hashlib.md5(value.encode(), usedforsecurity=False).hexdigest()) + + +def connect(restored=False): + url = secret("DATABASE_URL") + if restored: + url = make_conninfo(url, host="postgres-restore") + return psycopg.connect(url, autocommit=True, row_factory=dict_row, + options="-c timezone=UTC -c statement_timeout=120000") + + +def snapshot(conn): + counts = {} + for table in ("users", "chats", "chat_members", "messages", "schema_migrations"): + # Имена таблиц — фиксированный список выше, не входные данные клиента. + counts[table] = conn.execute("SELECT count(*) AS n FROM " + table).fetchone()["n"] + digest = hashlib.sha256() + for row in conn.execute("SELECT * FROM messages ORDER BY id"): + digest.update((json.dumps(row, default=str, sort_keys=True, ensure_ascii=False) + "\n").encode()) + samples = [] + offset = 0 + for size in SIZES: + for number in (offset + 1, offset + size // 2, offset + size): + row = conn.execute("SELECT * FROM messages WHERE id=%s", (identifier(f"message:{number}"),)).fetchone() + samples.append(json.loads(json.dumps(row, default=str, sort_keys=True, ensure_ascii=False))) + offset += size + return {"counts": counts, "messages_sha256": digest.hexdigest(), "samples": samples} + + +def benchmark(): + db.pool.open(wait=True, timeout=15) + try: + migrate.apply() + finally: + db.pool.close() + results, plans = [], [] + with connect() as conn: + assert conn.execute("SELECT count(*) AS n FROM users").fetchone()["n"] == 0, "Research requires an empty database" + encoded = passwords.hash_password(secrets.token_urlsafe(24)) + for n in range(1, 11): + conn.execute("INSERT INTO users(id,username,display_name,password_hash) VALUES(%s,%s,%s,%s)", + (identifier(f"user:{n}"), f"research_{n}", "Synthetic research user", encoded)) + for n in range(4): + chat = identifier(f"chat:{n}") + conn.execute("INSERT INTO chats(id,title) VALUES(%s,%s)", (chat, f"Research chat {n}")) + for user in (1, 2): + conn.execute("INSERT INTO chat_members(chat_id,user_id) VALUES(%s,%s)", (chat, identifier(f"user:{user}"))) + variants = [("chat_only", "chat_id"), ("history", "chat_id, created_at, id")] + for repeat in range(3): + order = variants if repeat % 2 == 0 else variants[::-1] + for variant, columns in order: + conn.execute("TRUNCATE messages") + conn.execute("DROP INDEX IF EXISTS messages_history_idx") + conn.execute("DROP INDEX IF EXISTS messages_chat_idx") + index = "messages_history_idx" if variant == "history" else "messages_chat_idx" + conn.execute("CREATE INDEX " + index + " ON messages (" + columns + ")") + lsn = conn.execute("SELECT pg_current_wal_insert_lsn() AS lsn").fetchone()["lsn"] + started = time.perf_counter() + conn.execute("""INSERT INTO messages(id,chat_id,sender_id,text,created_at) + SELECT md5('message:'||n)::uuid, + md5('chat:'||(CASE WHEN n<=70000 THEN 0 WHEN n<=90000 THEN 1 WHEN n<=99000 THEN 2 ELSE 3 END))::uuid, + md5('user:'||(1+n%2))::uuid, + 'Synthetic message '||n||' '||repeat('x',80), + timestamptz '2026-01-01 00:00:00+00' + n*interval '1 microsecond' + FROM generate_series(1,100000) AS n""") + insert_ms = (time.perf_counter() - started) * 1000 + wal = conn.execute("SELECT pg_wal_lsn_diff(pg_current_wal_insert_lsn(), %s) AS bytes", (lsn,)).fetchone()["bytes"] + conn.execute("VACUUM (ANALYZE) messages") + index_bytes = conn.execute("SELECT pg_relation_size(%s::regclass) AS bytes", (index,)).fetchone()["bytes"] + result = {"variant": variant, "repeat": repeat + 1, "insert_100000_ms": insert_ms, + "wal_bytes": int(wal), "index_bytes": index_bytes, "queries": []} + offset = 0 + for chat, size in enumerate(SIZES): + for fraction in (0.1, 0.5, 0.9): + number = offset + int(size * fraction) + params = (identifier(f"chat:{chat}"), START + timedelta(microseconds=number), identifier(f"message:{number}"), 51) + conn.execute(QUERY, params).fetchall() # один прогрев именно этой страницы + times = [] + for _ in range(5): + started = time.perf_counter() + rows = conn.execute(QUERY, params).fetchall() + times.append((time.perf_counter() - started) * 1000) + assert len(rows) == 51 + plan = conn.execute("EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) " + QUERY, params).fetchone()["QUERY PLAN"][0] + result["queries"].append({"chat_size": size, "position": fraction, "latency_ms": times, + "explain_execution_ms": plan["Execution Time"]}) + if repeat == 0: + plans.append({"variant": variant, "chat_size": size, "position": fraction, "plan": plan}) + offset += size + results.append(result) + # Последняя итерация заканчивается production-индексом history. + assert conn.execute("SELECT to_regclass('messages_history_idx') AS name").fetchone()["name"] + baseline = snapshot(conn) + assert baseline["counts"]["messages"] == 100000 + summary = {} + for variant, _ in variants: + trials = [row for row in results if row["variant"] == variant] + latencies = sorted(t for row in trials for q in row["queries"] for t in q["latency_ms"]) + summary[variant] = { + "insert_100000_median_ms": statistics.median(row["insert_100000_ms"] for row in trials), + "index_bytes": trials[-1]["index_bytes"], + "wal_bytes_median": statistics.median(row["wal_bytes"] for row in trials), + "queries": len(latencies), "errors": 0, "concurrency": 1, + "p50_ms": statistics.median(latencies), + "p95_ms": latencies[int(0.95 * (len(latencies) - 1))], + "p99_ms": latencies[int(0.99 * (len(latencies) - 1))], + "successful_sql_rps": len(latencies) / (sum(latencies) / 1000), + } + return {"postgres": conn.execute("SELECT version() AS version").fetchone()["version"], + "messages": 100000, "chat_sizes": SIZES, "repeats": 3, "warmup_per_case": 1, + "measurements_per_case": 5, "page_size": 51, "query": QUERY, + "summary": summary, "trials": results, "plans": plans, "snapshot": baseline} + + +def after_backup(): + with connect() as conn: + message_id = identifier("after-backup") + conn.execute("INSERT INTO messages(id,chat_id,sender_id,text) VALUES(%s,%s,%s,%s)", + (message_id, identifier("chat:0"), identifier("user:1"), "Committed AFTER backup")) + return {"id": str(message_id), "source_messages": conn.execute("SELECT count(*) AS n FROM messages").fetchone()["n"]} + + +def verify(): + expected = json.load(sys.stdin) + with connect(restored=True) as restored, connect() as source: + actual = snapshot(restored) + assert actual == expected, "Restored data differs from the snapshot" + marker = (identifier("after-backup"),) + assert restored.execute("SELECT 1 FROM messages WHERE id=%s", marker).fetchone() is None + assert source.execute("SELECT 1 FROM messages WHERE id=%s", marker).fetchone() is not None + return {"counts": actual["counts"], "messages_sha256": actual["messages_sha256"], + "samples_verified": len(actual["samples"]), "all_messages_match": True, + "after_backup_message_missing_in_restore": True, + "source_messages": source.execute("SELECT count(*) AS n FROM messages").fetchone()["n"]} + + +if __name__ == "__main__": + if os.environ.get("LAB2_RESEARCH") != "1": + raise SystemExit("Use ./scripts/research.sh: an isolated research database is required") + operation = sys.argv[1] if len(sys.argv) > 1 else "benchmark" + result = {"benchmark": benchmark, "after-backup": after_backup, "verify": verify}[operation]() + print(json.dumps(result, ensure_ascii=False, default=str)) diff --git a/research/run.py b/research/run.py new file mode 100644 index 00000000..33e0f0e3 --- /dev/null +++ b/research/run.py @@ -0,0 +1,99 @@ +"""Создаёт изолированный стенд, измеряет SQL, делает dump и restore; пишет реальные результаты.""" +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import platform +import subprocess +import sys +import time +from uuid import uuid4 + + +ROOT = Path(__file__).resolve().parents[1] +project = "lab2-research-" + uuid4().hex[:10] +env = dict(os.environ, COMPOSE_PROJECT_NAME=project) +command = ["docker", "compose", "--profile", "research"] + + +def compose(*args, stdin=None, stdout=subprocess.PIPE): + return subprocess.run([*command, *args], cwd=ROOT, env=env, input=stdin, + stdout=stdout, stderr=subprocess.PIPE, check=True, timeout=600) + + +def run_module(operation, stdin=None): + result = compose("run", "--rm", "--no-deps", "-T", "-e", "LAB2_RESEARCH=1", "api", "python", "-m", "research.benchmark", operation, stdin=stdin) + return json.loads(result.stdout) + + +def wait_database(service): + for _ in range(60): + cid = compose("ps", "-q", service).stdout.decode().strip() + state = subprocess.run(["docker", "inspect", "--format", "{{.State.Health.Status}}", cid], capture_output=True, text=True, check=True) + if state.stdout.strip() == "healthy": + return + time.sleep(1) + raise RuntimeError(service + " did not become healthy") + + +def main(): + evidence = ROOT / "evidence" + evidence.mkdir(exist_ok=True) + backups = ROOT / "backups" + backups.mkdir(exist_ok=True) + dump = backups / (project + ".dump") + metadata = {"utc": datetime.now(timezone.utc).isoformat(), "project": project, + "command": "./scripts/research.sh", "host": platform.system() + " " + platform.machine(), + "commit": subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip() + " + working tree", + "postgres_limits": {"cpus": 1, "memory_mib": 512}, "api_runner_limits": {"cpus": 1, "memory_mib": 256}} + started = time.perf_counter() + try: + print("Research project:", project, flush=True) + compose("up", "-d", "postgres", "postgres-restore") + wait_database("postgres") + wait_database("postgres-restore") + print("Measuring two indexes on the same 100000 messages, three repeats...", flush=True) + result = run_module("benchmark") + plans = result.pop("plans") + (evidence / "query-plans.json").write_text(json.dumps(plans, ensure_ascii=False, indent=2)) + source_id = compose("ps", "-q", "postgres").stdout.decode().strip() + restore_id = compose("ps", "-q", "postgres-restore").stdout.decode().strip() + assert source_id != restore_id + for label, cid in (("source", source_id), ("restored", restore_id)): + inspected = json.loads(subprocess.run(["docker", "inspect", cid], capture_output=True, check=True).stdout)[0] + metadata[label] = {"container": cid, "image": inspected["Image"], + "volumes": [m["Name"] for m in inspected["Mounts"] if m["Type"] == "volume"]} + assert metadata["source"]["volumes"] != metadata["restored"]["volumes"] + metadata["docker"] = subprocess.run(["docker", "info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}"], capture_output=True, text=True, check=True).stdout.strip() + metadata["stats_after_queries"] = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{json .}}", source_id], capture_output=True, text=True, check=True).stdout.strip() + backup_started = time.perf_counter() + with dump.open("wb") as output: + compose("exec", "-T", "postgres", "pg_dump", "-U", "messenger", "-d", "messenger", "--format=custom", stdout=output) + backup_seconds = time.perf_counter() - backup_started + marker = run_module("after-backup") + restore_started = time.perf_counter() + # Dump подаётся через stdin, без вывода данных или credentials в журнал. + with dump.open("rb") as input_file: + subprocess.run([*command, "exec", "-T", "postgres-restore", "pg_restore", "-U", "messenger", "-d", "messenger", + "--exit-on-error", "--single-transaction", "--no-owner"], cwd=ROOT, env=env, stdin=input_file, + stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, check=True, timeout=300) + restore_seconds = time.perf_counter() - restore_started + verification_started = time.perf_counter() + verification = run_module("verify", stdin=json.dumps(result["snapshot"]).encode()) + result.update(metadata) + result["backup"] = {"file": "backups/" + dump.name, "bytes": dump.stat().st_size, "seconds": backup_seconds} + result["restore"] = {"seconds": restore_seconds, "verification_seconds": time.perf_counter() - verification_started, + "verification": verification, "post_backup_insert": marker} + result["total_seconds"] = time.perf_counter() - started + (evidence / "research.json").write_text(json.dumps(result, ensure_ascii=False, indent=2)) + print(json.dumps(result["summary"], ensure_ascii=False, indent=2), flush=True) + print(f"PASS: backup {backup_seconds:.2f}s; restore {restore_seconds:.2f}s; 100000 messages, all hashes and 12 samples match", flush=True) + print("PASS: restored into a different container and fresh volume; one post-backup commit is absent as expected", flush=True) + print("Results: evidence/research.json, evidence/query-plans.json; dump stays outside Git in backups/", flush=True) + finally: + compose("down", "--volumes", "--timeout", "10") + print("Removed only temporary research project:", project, flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 00000000..0a0b9999 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +python3 scripts/init_local.py +docker compose build "$@" api diff --git a/scripts/check-docker.sh b/scripts/check-docker.sh new file mode 100755 index 00000000..048ad8d1 --- /dev/null +++ b/scripts/check-docker.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +if [[ ! -x .venv/bin/python ]]; then + echo "Создай .venv и установи requirements.txt по REPORT.md" >&2 + exit 1 +fi +./scripts/build.sh +mkdir -p evidence +.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | tee evidence/docker-checks.txt diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 00000000..90a608d1 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" + +if [[ ! -x .venv/bin/python ]]; then + echo "Не хватает .venv" >&2 + echo "python3 -m venv .venv" >&2 + echo ".venv/bin/python -m pip install -r requirements.txt" >&2 + exit 1 +fi + +export BASE_URL="${BASE_URL:-http://localhost:8080}" +echo "Проверяем сервер: $BASE_URL" +make check test PYTHON="$project_dir/.venv/bin/python" BASE_URL="$BASE_URL" +.venv/bin/python -m unittest discover -s student-tests -v diff --git a/scripts/demo.sh b/scripts/demo.sh new file mode 100755 index 00000000..73f7a3c8 --- /dev/null +++ b/scripts/demo.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail +base_url="${BASE_URL:-http://localhost:8080}" +suffix="$(date +%s)_$RANDOM" +# Только синтетические пароли; токены остаются в памяти shell и не выводятся. +password="Demo-password-$suffix" +register() { + local username="$1" display_name="$2" + jq -nc --arg u "$username" --arg d "$display_name" --arg p "$password" \ + '{username:$u, display_name:$d, password:$p}' | + curl -fsS -H 'Content-Type: application/json' --data-binary @- "$base_url/api/v1/users" | jq -er '.id' +} +login() { + curl -fsS -X POST -u "$1:$password" "$base_url/api/v1/auth/sessions" | jq -er '.token' +} +alice_id="$(register "alice_$suffix" Алиса)" +bob_id="$(register "bob_$suffix" Боб)" +alice_token="$(login "alice_$suffix")" +bob_token="$(login "bob_$suffix")" +chat_id="$(jq -nc --arg a "$alice_id" --arg b "$bob_id" '{title:"Учебный чат",member_ids:[$a,$b]}' | + curl -fsS -H "Authorization: Bearer $alice_token" -H 'Content-Type: application/json' \ + --data-binary @- "$base_url/api/v1/chats" | jq -er '.id')" +for token in "$alice_token" "$bob_token"; do + curl -fsS -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \ + -d '{"text":"Привет из lab2! 👋"}' "$base_url/api/v1/chats/$chat_id/messages" | jq . +done +curl -fsS -H "Authorization: Bearer $bob_token" "$base_url/api/v1/chats/$chat_id/messages" | jq . diff --git a/scripts/init_local.py b/scripts/init_local.py new file mode 100644 index 00000000..230c15ef --- /dev/null +++ b/scripts/init_local.py @@ -0,0 +1,29 @@ +"""Создаёт локальное окружение и secrets; существующие значения сохраняет.""" +from pathlib import Path +import secrets +import shutil +from urllib.parse import quote + + +root = Path(__file__).resolve().parents[1] +if not (root / ".env").exists(): + shutil.copyfile(root / ".env.example", root / ".env") +directory = root / "secrets" +directory.mkdir(exist_ok=True, mode=0o700) +directory.chmod(0o700) +password_file = directory / "postgres_password" +if not password_file.exists(): + password_file.write_text(secrets.token_hex(24)) +password = password_file.read_text().strip() +values = { + "database_url": f"postgresql://messenger:{quote(password, safe='')}@postgres:5432/messenger", + "cache_url": "redis://valkey:6379/0", +} +for name, value in values.items(): + path = directory / name + if not path.exists(): + path.write_text(value) +# Каталог 0700 защищает файлы на хосте; 0444 позволяет USER 10001 читать bind-mounted secret. +for name in ("postgres_password", "database_url", "cache_url"): + (directory / name).chmod(0o444) +print("Local .env and secrets are ready; values are not printed") diff --git a/scripts/migrate.sh b/scripts/migrate.sh new file mode 100755 index 00000000..22da4961 --- /dev/null +++ b/scripts/migrate.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +docker compose exec -T api python -m src.migrate diff --git a/scripts/research.sh b/scripts/research.sh new file mode 100755 index 00000000..b4f8e34e --- /dev/null +++ b/scripts/research.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +./scripts/build.sh +mkdir -p evidence +python3 -u research/run.py 2>&1 | tee evidence/research.txt diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..23c94603 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +docker compose restart --timeout 10 postgres valkey api +./scripts/wait-ready.sh diff --git a/scripts/run-local.sh b/scripts/run-local.sh new file mode 100755 index 00000000..19337594 --- /dev/null +++ b/scripts/run-local.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +# В lab2 локальный запуск включает API, PostgreSQL и Valkey. +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +exec "$project_dir/scripts/start.sh" diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100755 index 00000000..464d62bb --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +python3 scripts/init_local.py +docker compose up -d --build api +./scripts/wait-ready.sh diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100755 index 00000000..7dfffac9 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +docker compose down --timeout 10 +echo "Контейнеры удалены; именованные volumes сохранены" diff --git a/scripts/wait-ready.sh b/scripts/wait-ready.sh new file mode 100755 index 00000000..7ea7a709 --- /dev/null +++ b/scripts/wait-ready.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +for ((attempt = 0; attempt < 60; attempt++)); do + if docker compose exec -T api python -m src.healthcheck >/dev/null 2>&1; then + echo "API ready" + exit 0 + fi + sleep 1 +done +echo "API не готов за 60 секунд. Проверь docker compose logs api" >&2 +exit 1 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/__main__.py b/src/__main__.py new file mode 100644 index 00000000..420cbd44 --- /dev/null +++ b/src/__main__.py @@ -0,0 +1,16 @@ +"""Общий запуск""" + +import os + +import uvicorn + + +if __name__ == "__main__": + uvicorn.run( + "src.app:app", + host=os.environ.get("APP_HOST", "0.0.0.0"), + port=int(os.environ.get("APP_PORT", "8080")), + workers=1, + access_log=False, + timeout_graceful_shutdown=5, + ) diff --git a/src/app.py b/src/app.py new file mode 100644 index 00000000..84d9f41c --- /dev/null +++ b/src/app.py @@ -0,0 +1,148 @@ +"""HTTP-адреса, определение пользователя и обработка запросов""" + +from contextlib import asynccontextmanager +import json +import logging +from time import perf_counter +from uuid import UUID, uuid4 + +from fastapi import FastAPI, Request, Response +from starlette.concurrency import run_in_threadpool +from fastapi.exceptions import RequestValidationError +from starlette.exceptions import HTTPException + +from src import auth, db, migrate, passwords, storage +from src.errors import ApiError, error_response +from src.models import CreateChat, CreateMessage, CreateUser + + +@asynccontextmanager +async def lifespan(app): + await run_in_threadpool(db.pool.open, wait=True, timeout=15) + try: + await run_in_threadpool(migrate.apply) + yield + finally: + await run_in_threadpool(db.pool.close) + + +app = FastAPI(title="Учебный мессенджер", redirect_slashes=False, lifespan=lifespan) +logger = logging.getLogger("uvicorn.error") + + +@app.middleware("http") +async def handle_request(request: Request, call_next): + request.state.request_id = str(uuid4()) + started = perf_counter() + path = request.url.path + try: + is_registration = request.method == "POST" and path == "/api/v1/users" + is_login = request.method == "POST" and path == "/api/v1/auth/sessions" + if is_login: + request.state.user = await run_in_threadpool(auth.basic_user, request.headers.get("Authorization")) + if await request.body(): + raise ApiError(400, "invalid_request", "Вход через Basic не принимает тело запроса") + elif path.startswith("/api/v1/") and not is_registration: + request.state.user = await run_in_threadpool(auth.current_user, request.headers.get("Authorization")) + if request.method == "POST" and path.startswith("/api/v1/") and not is_login: + content_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower() + if content_type != "application/json": + raise ApiError(415, "unsupported_media_type", "Ожидается application/json") + try: + json.loads((await request.body()).decode("utf-8")) + except (ValueError, RecursionError): + raise ApiError(400, "invalid_request", "Ожидается корректный JSON в UTF-8") + response = await call_next(request) + except ApiError as error: + response = error_response(request, error) + except Exception as error: + # Не печатаем тело запроса или текст исключения: там могут быть сообщения. + logger.error("%s request_id=%s", type(error).__name__, request.state.request_id) + response = error_response(request, ApiError(500, "internal_error", "Внутренняя ошибка сервера")) + response.headers["X-Request-Id"] = request.state.request_id + logger.info( + "%s %s status=%s duration_ms=%.2f request_id=%s", + request.method, path, response.status_code, + (perf_counter() - started) * 1000, request.state.request_id, + ) + return response + + +@app.exception_handler(ApiError) +def handle_api_error(request: Request, error: ApiError): + return error_response(request, error) + + +@app.exception_handler(RequestValidationError) +def handle_validation_error(request: Request, error: RequestValidationError): + # Контракт требует 400 и наш формат ошибки, а не стандартный ответ FastAPI. + return error_response(request, ApiError(400, "invalid_request", "Неверные поля или параметры запроса")) + + +@app.exception_handler(HTTPException) +def handle_http_error(request: Request, error: HTTPException): + code = "not_found" if error.status_code == 404 else "invalid_request" + message = "Адрес не найден" if error.status_code == 404 else "Недопустимый HTTP-запрос" + response = error_response(request, ApiError(error.status_code, code, message)) + if error.headers: + response.headers.update(error.headers) + return response + + +@app.get("/health/live") +def health_live(): + return {"status": "ok"} + + +@app.get("/health/ready") +def health_ready(): + db.ready() + auth.ready() + return {"status": "ok"} + + +@app.post("/api/v1/users", status_code=201) +def create_user(data: CreateUser): + return storage.create_user(data.username, data.display_name, passwords.hash_password(data.password)) + + +@app.get("/api/v1/users/me") +def current_user(request: Request): + return request.state.user + + +@app.post("/api/v1/chats", status_code=201) +def create_chat(request: Request, data: CreateChat): + return storage.create_chat(request.state.user["id"], data.title, data.member_ids) + + +@app.get("/api/v1/chats") +def list_chats(request: Request, limit: str = "50", cursor: str | None = None): + user_id = request.state.user["id"] + return storage.list_chats(user_id, limit, cursor) + + +@app.get("/api/v1/chats/{chat_id}") +def get_chat(request: Request, chat_id: UUID): + return storage.get_chat(request.state.user["id"], str(chat_id)) + + +@app.post("/api/v1/chats/{chat_id}/messages", status_code=201) +def create_message(request: Request, chat_id: UUID, data: CreateMessage): + return storage.create_message(request.state.user["id"], str(chat_id), data.text) + + +@app.get("/api/v1/chats/{chat_id}/messages") +def list_messages(request: Request, chat_id: UUID, limit: str = "50", cursor: str | None = None): + return storage.list_messages(request.state.user["id"], str(chat_id), limit, cursor) + + +@app.post("/api/v1/auth/sessions", status_code=201) +def create_session(request: Request): + return auth.create_session(request.state.user) + + +@app.delete("/api/v1/auth/sessions/current", status_code=204) +def delete_session(request: Request): + auth.logout(request.headers.get("Authorization")) + return Response(status_code=204) diff --git a/src/auth.py b/src/auth.py new file mode 100644 index 00000000..a8db643d --- /dev/null +++ b/src/auth.py @@ -0,0 +1,95 @@ +"""Basic только для входа; Bearer-сессии с TTL и AOF находятся в Valkey.""" +import base64 +import binascii +from contextlib import contextmanager +from datetime import datetime, timedelta, timezone +import hashlib +import json +import secrets + +import redis + +from src import passwords, storage +from src.config import SESSION_TTL, secret +from src.errors import ApiError + + +sessions = redis.Redis.from_url( + secret("CACHE_URL"), decode_responses=True, + socket_connect_timeout=0.5, socket_timeout=0.5, max_connections=10, + retry=redis.retry.Retry(redis.backoff.NoBackoff(), 0), +) + + +@contextmanager +def available(): + try: + yield + except redis.RedisError: + raise ApiError(503, "unavailable", "Valkey временно недоступен") from None + + +def unauthorized(basic=False): + challenge = 'Basic realm="messenger", charset="UTF-8"' if basic else 'Bearer realm="messenger"' + return ApiError(401, "unauthorized", "Неверная или отсутствующая идентификация", {"WWW-Authenticate": challenge}) + + +def credential(header, scheme): + parts = (header or "").split() + if len(parts) != 2 or parts[0].lower() != scheme.lower(): + raise unauthorized(scheme == "Basic") + return parts[1] + + +def basic_user(header): + value = credential(header, "Basic") + try: + decoded = base64.b64decode(value, validate=True).decode("utf-8") + username, password = decoded.split(":", 1) + except (ValueError, UnicodeError, binascii.Error): + raise unauthorized(True) from None + user = storage.find_login(username) + if user is None or not passwords.matches(user["password_hash"], password): + raise unauthorized(True) + return storage.public_user(user) + + +def session_key(token): + return "session:" + hashlib.sha256(token.encode()).hexdigest() + + +def create_session(user): + token = secrets.token_urlsafe(32) + expires = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL) + value = {"user_id": user["id"], "expires_at": storage.utc(expires)} + with available(): + sessions.set(session_key(token), json.dumps(value), ex=SESSION_TTL) + return {"token": token, "token_type": "Bearer", "expires_at": value["expires_at"], "user": user} + + +def current_user(header): + token = credential(header, "Bearer") + with available(): + value = sessions.get(session_key(token)) + if value is None: + raise unauthorized() + data = json.loads(value) + if datetime.fromisoformat(data["expires_at"].replace("Z", "+00:00")) <= datetime.now(timezone.utc): + raise unauthorized() + user = storage.find_user(data["user_id"]) + if user is None: + raise unauthorized() + return user + + +def logout(header): + token = credential(header, "Bearer") + with available(): + removed = sessions.delete(session_key(token)) + if not removed: + raise unauthorized() + + +def ready(): + with available(): + sessions.ping() diff --git a/src/config.py b/src/config.py new file mode 100644 index 00000000..427fdb3e --- /dev/null +++ b/src/config.py @@ -0,0 +1,14 @@ +"""Настройки и чтение локальных Docker secrets.""" +import os +from pathlib import Path + + +def secret(name): + return Path(os.environ[name + "_FILE"]).read_text().strip() + + +SESSION_TTL = int(os.environ.get("SESSION_TTL_SECONDS", "3600")) +if SESSION_TTL < 1: + raise ValueError("SESSION_TTL_SECONDS должен быть положительным") +if os.environ.get("SESSION_STORE", "valkey") != "valkey": + raise ValueError("В этой реализации SESSION_STORE=valkey") diff --git a/src/db.py b/src/db.py new file mode 100644 index 00000000..8580eca0 --- /dev/null +++ b/src/db.py @@ -0,0 +1,35 @@ +"""Ограниченный пул; каждый with connection() — отдельная транзакция.""" +from contextlib import contextmanager + +import psycopg +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool, PoolTimeout, TooManyRequests + +from src.config import secret +from src.errors import ApiError + + +pool = ConnectionPool( + conninfo=secret("DATABASE_URL"), open=False, + min_size=1, max_size=5, max_waiting=20, timeout=2, + check=ConnectionPool.check_connection, + kwargs={ + "row_factory": dict_row, + "connect_timeout": 2, + "options": "-c timezone=UTC -c statement_timeout=3000 -c lock_timeout=2000 -c idle_in_transaction_session_timeout=5000", + }, +) + + +@contextmanager +def connection(): + try: + with pool.connection() as conn: + yield conn + except (psycopg.OperationalError, PoolTimeout, TooManyRequests): + raise ApiError(503, "unavailable", "PostgreSQL временно недоступен") from None + + +def ready(): + with connection() as conn: + conn.execute("SELECT 1") diff --git a/src/errors.py b/src/errors.py new file mode 100644 index 00000000..09e0cdf1 --- /dev/null +++ b/src/errors.py @@ -0,0 +1,27 @@ +"""общий формат ошибок""" + +from fastapi import Request +from fastapi.responses import JSONResponse + + +class ApiError(Exception): + def __init__(self, status: int, code: str, message: str, headers=None): + super().__init__(message) + self.status = status + self.code = code + self.message = message + self.headers = headers or {} + + +def error_response(request: Request, error: ApiError): + return JSONResponse( + status_code=error.status, + content={ + "error": { + "code": error.code, + "message": error.message, + "request_id": request.state.request_id, + } + }, + headers={"X-Request-Id": request.state.request_id, **error.headers}, + ) diff --git a/src/healthcheck.py b/src/healthcheck.py new file mode 100644 index 00000000..dae4d1d5 --- /dev/null +++ b/src/healthcheck.py @@ -0,0 +1,15 @@ +"""healthcheck докера""" + +import json +import os +from urllib.request import urlopen + + +if __name__ == "__main__": + port = os.environ.get("APP_PORT", "8080") + try: + with urlopen(f"http://127.0.0.1:{port}/health/ready", timeout=2) as response: + ready = response.status == 200 and json.load(response) == {"status": "ok"} + raise SystemExit(0 if ready else 1) + except (OSError, ValueError): + raise SystemExit(1) diff --git a/src/migrate.py b/src/migrate.py new file mode 100644 index 00000000..0c1410b6 --- /dev/null +++ b/src/migrate.py @@ -0,0 +1,39 @@ +"""Версионируемые миграции с проверкой checksum и блокировкой.""" +import hashlib +from pathlib import Path + +from src import db + + +DIRECTORY = Path(__file__).resolve().parents[1] / "migrations" + + +def apply(): + applied = [] + with db.connection() as conn: + conn.execute("SELECT pg_advisory_xact_lock(202602)") + conn.execute(""" + CREATE TABLE IF NOT EXISTS schema_migrations ( + version text PRIMARY KEY, checksum text NOT NULL, + applied_at timestamptz NOT NULL DEFAULT clock_timestamp() + ) + """) + for path in sorted(DIRECTORY.glob("*.sql")): + checksum = hashlib.sha256(path.read_bytes()).hexdigest() + existing = conn.execute("SELECT checksum FROM schema_migrations WHERE version=%s", (path.name,)).fetchone() + if existing: + if existing["checksum"] != checksum: + raise RuntimeError("Изменена применённая миграция: " + path.name) + continue + conn.execute(path.read_text(), prepare=False) + conn.execute("INSERT INTO schema_migrations(version, checksum) VALUES (%s, %s)", (path.name, checksum)) + applied.append(path.name) + return applied + + +if __name__ == "__main__": + db.pool.open(wait=True, timeout=15) + try: + print("Migrations:", ", ".join(apply()) or "already applied") + finally: + db.pool.close() diff --git a/src/models.py b/src/models.py new file mode 100644 index 00000000..e8f6be41 --- /dev/null +++ b/src/models.py @@ -0,0 +1,41 @@ +"""допустимые поля и проверка данных""" + +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class RequestModel(BaseModel): + model_config = ConfigDict(extra="forbid", strict=True) + + @field_validator("*") + @classmethod + def check_strings(cls, value): + if isinstance(value, str): + if not value.strip(): + raise ValueError("Строка не должна состоять из пробелов") + value.encode("utf-8") + return value + + +class CreateUser(RequestModel): + username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") + display_name: str = Field(min_length=1, max_length=100) + password: str = Field(min_length=12, max_length=128) + + +class CreateChat(RequestModel): + title: str = Field(min_length=1, max_length=100) + member_ids: list[str] = Field(min_length=1, max_length=100) + + @field_validator("member_ids") + @classmethod + def check_members(cls, values): + member_ids = [str(UUID(value)) for value in values] + if len(set(member_ids)) != len(member_ids): + raise ValueError("Участники не должны повторяться") + return member_ids + + +class CreateMessage(RequestModel): + text: str = Field(min_length=1, max_length=4000) diff --git a/src/pagination.py b/src/pagination.py new file mode 100644 index 00000000..dcac20cc --- /dev/null +++ b/src/pagination.py @@ -0,0 +1,39 @@ +"""Курсор — scope, время и UUID; сами страницы выбирает PostgreSQL.""" +import base64 +import binascii +from datetime import datetime +import json +import re +from uuid import UUID + +from src.errors import ApiError + + +def parameters(limit, cursor, scope): + try: + if not re.fullmatch(r"[0-9]+", limit): + raise ValueError + size = int(limit) + if not 1 <= size <= 100: + raise ValueError + anchor = None + if cursor is not None: + raw = base64.b64decode(cursor, altchars=b"-_", validate=True) + data = json.loads(raw.decode("utf-8")) + if not isinstance(data, dict) or set(data) != {"scope", "created_at", "id"}: + raise ValueError + if data["scope"] != scope or not isinstance(data["created_at"], str) or not data["created_at"].endswith("Z"): + raise ValueError + anchor = (datetime.fromisoformat(data["created_at"].replace("Z", "+00:00")), UUID(data["id"])) + return size, anchor + except (ValueError, TypeError, AttributeError, binascii.Error, RecursionError): + raise ApiError(400, "invalid_request", "Неверный limit или cursor") from None + + +def page(items, size, scope): + cursor = None + if len(items) > size: + last = items[size - 1] + value = {"scope": scope, "created_at": last["created_at"], "id": last["id"]} + cursor = base64.urlsafe_b64encode(json.dumps(value).encode()).decode() + return {"items": items[:size], "next_cursor": cursor} diff --git a/src/passwords.py b/src/passwords.py new file mode 100644 index 00000000..bc980660 --- /dev/null +++ b/src/passwords.py @@ -0,0 +1,22 @@ +"""Argon2id. Одновременно считаются не более двух хешей.""" +from threading import Semaphore + +from argon2 import PasswordHasher +from argon2.exceptions import VerificationError + + +hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1) +slots = Semaphore(2) + + +def hash_password(password): + with slots: + return hasher.hash(password) + + +def matches(encoded, password): + with slots: + try: + return hasher.verify(encoded, password) + except VerificationError: + return False diff --git a/src/storage.py b/src/storage.py new file mode 100644 index 00000000..e4335208 --- /dev/null +++ b/src/storage.py @@ -0,0 +1,121 @@ +"""Параметризованный SQL. Одна функция записи — одна транзакция.""" +from uuid import UUID, uuid4 + +from psycopg.errors import ForeignKeyViolation, UniqueViolation + +from src import db, pagination +from src.errors import ApiError + + +def utc(value): + return value.isoformat(timespec="microseconds").replace("+00:00", "Z") + + +def public_user(row): + return {"id": str(row["id"]), "username": row["username"], + "display_name": row["display_name"], "created_at": utc(row["created_at"])} + + +def public_chat(row): + return {"id": str(row["id"]), "title": row["title"], + "member_ids": [str(value) for value in row["member_ids"]], "created_at": utc(row["created_at"])} + + +def public_message(row): + return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]), + "text": row["text"], "created_at": utc(row["created_at"])} + + +def create_user(username, display_name, password_hash): + try: + with db.connection() as conn: + row = conn.execute("""INSERT INTO users(id, username, display_name, password_hash) + VALUES (%s, %s, %s, %s) RETURNING *""", (uuid4(), username, display_name, password_hash)).fetchone() + result = public_user(row) + return result + except UniqueViolation: + raise ApiError(409, "conflict", "Имя пользователя уже занято") from None + + +def find_login(username): + with db.connection() as conn: + return conn.execute("SELECT * FROM users WHERE username=%s", (username,)).fetchone() + + +def find_user(user_id): + with db.connection() as conn: + row = conn.execute("SELECT * FROM users WHERE id=%s", (UUID(user_id),)).fetchone() + return public_user(row) if row else None + + +def create_chat(user_id, title, member_ids): + if user_id not in member_ids: + raise ApiError(400, "invalid_request", "Создатель должен входить в member_ids") + try: + with db.connection() as conn: + row = conn.execute("INSERT INTO chats(id, title) VALUES (%s, %s) RETURNING *", (uuid4(), title)).fetchone() + with conn.cursor() as cur: + cur.executemany("INSERT INTO chat_members(chat_id, user_id) VALUES (%s, %s)", + [(row["id"], UUID(member)) for member in member_ids]) + row["member_ids"] = member_ids + result = public_chat(row) + return result + except ForeignKeyViolation: + raise ApiError(400, "invalid_request", "Все участники должны существовать") from None + + +def require_member(conn, user_id, chat_id): + row = conn.execute("SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s", (UUID(chat_id), UUID(user_id))).fetchone() + if row is None: + raise ApiError(404, "not_found", "Чат не найден") + + +def get_chat(user_id, chat_id): + with db.connection() as conn: + require_member(conn, user_id, chat_id) + row = conn.execute("""SELECT c.*, ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) AS member_ids + FROM chats c WHERE c.id=%s""", (UUID(chat_id),)).fetchone() + return public_chat(row) + + +def list_chats(user_id, limit, cursor): + scope = "chats:" + user_id + size, anchor = pagination.parameters(limit, cursor, scope) + with db.connection() as conn: + if anchor: + found = conn.execute("""SELECT 1 FROM chats c JOIN chat_members m ON m.chat_id=c.id + WHERE m.user_id=%s AND (c.created_at, c.id)=(%s, %s)""", (UUID(user_id), *anchor)).fetchone() + if not found: + raise ApiError(400, "invalid_request", "Неверный cursor") + condition = "AND (c.created_at, c.id) > (%s, %s)" if anchor else "" + params = (UUID(user_id), *(anchor or ()), size + 1) + rows = conn.execute("""WITH selected AS ( + SELECT c.* FROM chats c JOIN chat_members own ON own.chat_id=c.id + WHERE own.user_id=%s """ + condition + """ ORDER BY c.created_at, c.id LIMIT %s + ) SELECT c.*, ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id) AS member_ids + FROM selected c ORDER BY c.created_at, c.id""", params).fetchall() + return pagination.page([public_chat(row) for row in rows], size, scope) + + +def create_message(user_id, chat_id, text): + with db.connection() as conn: + require_member(conn, user_id, chat_id) + row = conn.execute("""INSERT INTO messages(id, chat_id, sender_id, text) VALUES (%s, %s, %s, %s) + RETURNING *""", (uuid4(), UUID(chat_id), UUID(user_id), text)).fetchone() + result = public_message(row) + return result + + +def list_messages(user_id, chat_id, limit, cursor): + scope = "messages:" + chat_id + size, anchor = pagination.parameters(limit, cursor, scope) + with db.connection() as conn: + require_member(conn, user_id, chat_id) + if anchor: + found = conn.execute("SELECT 1 FROM messages WHERE chat_id=%s AND (created_at, id)=(%s, %s)", (UUID(chat_id), *anchor)).fetchone() + if not found: + raise ApiError(400, "invalid_request", "Неверный cursor") + condition = "AND (created_at, id) > (%s, %s)" if anchor else "" + rows = conn.execute("SELECT * FROM messages WHERE chat_id=%s " + condition + + " ORDER BY created_at, id LIMIT %s", (UUID(chat_id), *(anchor or ()), size + 1)).fetchall() + return pagination.page([public_message(row) for row in rows], size, scope) diff --git a/student-tests/docker_lifecycle.py b/student-tests/docker_lifecycle.py new file mode 100644 index 00000000..bff249f5 --- /dev/null +++ b/student-tests/docker_lifecycle.py @@ -0,0 +1,201 @@ +"""Полный прогон lab2 в собственном Compose project; основные данные не затрагивает.""" +from datetime import datetime, timezone +import json +import os +import platform +import socket +import subprocess +import sys +import time +from urllib.error import URLError +from urllib.request import urlopen +from uuid import uuid4 + +from helpers import Client, ROOT + + +def docker(*args): + return subprocess.run(["docker", *args], check=True, capture_output=True, text=True, timeout=120).stdout.strip() + + +def inspect(container): + return json.loads(docker("inspect", container))[0] + + +def check_shutdown(container, base_url, port): + with socket.create_connection(("127.0.0.1", port), timeout=2) as pending: + pending.sendall( + b"POST /api/v1/users HTTP/1.1\r\nHost: localhost\r\n" + b"Content-Type: application/json\r\nContent-Length: 1000\r\n" + b"Expect: 100-continue\r\nConnection: close\r\n\r\n" + ) + assert b"100 Continue" in pending.recv(1024), "Сервер не начал читать тело запроса" + pending.sendall(b'{"username":') + started = time.monotonic() + docker("kill", "--signal=TERM", container) + first_refusal = None + while inspect(container)["State"]["Running"]: + assert time.monotonic() - started < 10, "SIGTERM: превышены 10 секунд" + try: + with urlopen(base_url + "/health/live", timeout=0.3): + pass + except (URLError, OSError): + if first_refusal is None: + first_refusal = time.monotonic() - started + time.sleep(0.05) + elapsed = time.monotonic() - started + + assert elapsed < 10, "SIGTERM: превышены 10 секунд" + assert first_refusal is not None and first_refusal < elapsed, "Не показан отказ новых запросов при остановке" + state = inspect(container)["State"] + assert not state["OOMKilled"] and state["ExitCode"] in (0, 143), state + + result = subprocess.run(["docker", "logs", container], capture_output=True, text=True, check=True, timeout=10) + logs = result.stdout + result.stderr + assert "Application shutdown complete" in logs, "Нет подтверждения корректного завершения" + try: + with urlopen(base_url + "/health/live", timeout=0.5): + raise AssertionError("Остановленный сервер отвечает") + except (URLError, OSError): + pass + print(f"PASS: SIGTERM with unfinished request: {elapsed:.2f}s; new requests refused after {first_refusal:.2f}s; exit={state['ExitCode']}; OOM=false") + + +def main(): + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + port = sock.getsockname()[1] + project = "lab2-check-" + uuid4().hex[:10] + env = dict(os.environ, COMPOSE_PROJECT_NAME=project, HOST_PORT=str(port), SESSION_TTL_SECONDS="3600") + base_url = f"http://127.0.0.1:{port}" + + def compose(*args, capture=True, stdin=None): + return subprocess.run(["docker", "compose", *args], cwd=ROOT, env=env, input=stdin, + text=True, capture_output=capture, check=True, timeout=180) + + def ready(): + subprocess.run([str(ROOT / "scripts/start.sh")], cwd=ROOT, env=env, + check=True, capture_output=True, text=True, timeout=180) + print("PASS: start.sh built and started the stack, readiness reached") + + def states(): + return {service: inspect(compose("ps", "-q", service).stdout.strip()) for service in ("api", "postgres", "valkey")} + + def scalar(query, params=()): + code = """import json,sys +from src import db +data=json.load(sys.stdin) +db.pool.open(wait=True,timeout=15) +try: + with db.connection() as conn: + row=conn.execute(data['query'], data['params']).fetchone() + print(json.dumps(next(iter(row.values())))) +finally: + db.pool.close() +""" + return json.loads(compose("exec", "-T", "api", "python", "-c", code, + stdin=json.dumps({"query": query, "params": params})).stdout) + + print("UTC:", datetime.now(timezone.utc).isoformat()) + print("Host:", platform.system(), platform.machine(), "Python", platform.python_version()) + print("Docker:", docker("info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}")) + print("Commit:", subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip(), "+ working tree") + print("Command: ./scripts/check-docker.sh; project:", project) + try: + ready() + before = states() + for service, info in before.items(): + print(service, "image=" + info["Image"], "started=" + info["State"]["StartedAt"], + "memory=" + str(info["HostConfig"]["Memory"]), "nano_cpus=" + str(info["HostConfig"]["NanoCpus"])) + api = compose("ps", "-q", "api").stdout.strip() + assert docker("exec", api, "id", "-u") == "10001" + assert before["api"]["Config"]["Cmd"] == ["python", "-m", "src"] + for service in ("postgres", "valkey"): + assert any(mount["Type"] == "volume" for mount in before[service]["Mounts"]) + assert all(mount["Destination"].startswith("/run/secrets/") for mount in before["api"]["Mounts"]) + bad_health = subprocess.run(["docker", "exec", "-e", "APP_PORT=1", api, "python", "-m", "src.healthcheck"], capture_output=True) + assert bad_health.returncode == 1 + print("PASS: non-root UID 10001; Python PID 1; healthcheck; named volumes; no API data volume") + + subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url), check=True, timeout=240) + demo = subprocess.run([str(ROOT / "scripts/demo.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url), + check=True, capture_output=True, text=True, timeout=30) + assert "Привет из lab2!" in demo.stdout + print("PASS: demo.sh registered users, logged in and read two messages") + client = Client(base_url) + alice, bob, eve, chat = client.bootstrap() + marker = "private-" + uuid4().hex + client.message(alice, chat, marker) + logs = docker("logs", api) + assert marker not in logs and alice["token"] not in logs and alice["registration"]["password"] not in logs + logs_result = subprocess.run(["docker", "logs", api], capture_output=True, text=True, check=True) + assert "request_id=" in logs_result.stderr + logs_result.stdout + assert marker not in logs_result.stderr + logs_result.stdout + assert alice["token"] not in logs_result.stderr + logs_result.stdout + assert alice["registration"]["password"] not in logs_result.stderr + logs_result.stdout + assert scalar("SELECT bool_and(password_hash LIKE %s) FROM users", ("$argon2id$%",)) + print("PASS: Argon2id in PostgreSQL; logs do not contain message text, passwords or session tokens") + + title = "rollback_" + uuid4().hex + client.call("POST", "/api/v1/chats", 400, actor=alice, + body={"title": title, "member_ids": [alice["id"], str(uuid4())]}) + assert scalar("SELECT count(*) FROM chats WHERE title=%s", (title,)) == 0 + count = scalar("SELECT count(*) FROM messages") + compose("exec", "-T", "api", "python", "-m", "src.migrate") + compose("exec", "-T", "api", "python", "-m", "src.migrate") + assert scalar("SELECT count(*) FROM schema_migrations") == 1 + assert scalar("SELECT count(*) FROM messages") == count + print("PASS: failed membership insert rolled back chat; migrations run twice without duplicate data") + + before = states() + subprocess.run(["make", "test-persistence", "PYTHON=" + sys.executable, + "ACTION_SCRIPT=scripts/restart.sh", "BASE_URL=" + base_url], cwd=ROOT, env=env, check=True, timeout=240) + after = states() + for service in before: + assert before[service]["State"]["StartedAt"] != after[service]["State"]["StartedAt"] + print("Restart", service, before[service]["State"]["StartedAt"], "->", after[service]["State"]["StartedAt"]) + client.call("GET", "/api/v1/users/me", actor=alice, schema="User") + print("PASS: API, PostgreSQL and Valkey really restarted; previous token still works") + + for service in ("valkey", "postgres"): + try: + compose("stop", "--timeout", "10", service) + started = time.monotonic() + client.call("GET", "/api/v1/users/me", 503, actor=alice) + elapsed = time.monotonic() - started + assert elapsed < 6, elapsed + client.call("GET", "/api/v1/users/me", 401) + client.call("GET", "/health/ready", 503) + client.call("GET", "/health/live", schema="Health") + print(f"PASS: {service} unavailable -> 503 in {elapsed:.2f}s; anonymous -> 401; readiness 503, liveness 200") + finally: + compose("start", service) + subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90) + client.call("GET", "/api/v1/users/me", actor=alice, schema="User") + print("PASS:", service, "recovered without a new login") + + env["SESSION_TTL_SECONDS"] = "2" + compose("up", "-d", "--force-recreate", "api") + subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90) + short = client.user("short_ttl") + client.call("GET", "/api/v1/users/me", actor=short, schema="User") + time.sleep(2.2) + client.call("GET", "/api/v1/users/me", 401, actor=short) + print("PASS: SESSION_TTL_SECONDS=2; valid token expires without logout") + api = compose("ps", "-q", "api").stdout.strip() + check_shutdown(api, base_url, port) + print("PASS: all lab2 Docker checks completed") + finally: + try: + subprocess.run([str(ROOT / "scripts/stop.sh")], cwd=ROOT, env=env, + check=True, capture_output=True, text=True, timeout=90) + volumes = docker("volume", "ls", "--quiet", "--filter", "label=com.docker.compose.project=" + project) + if volumes: + print("PASS: stop.sh removed containers and preserved named volumes") + finally: + compose("down", "--volumes", "--timeout", "10") + print("Removed only temporary Compose project and its test volumes:", project) + + +if __name__ == "__main__": + main() diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 00000000..33a9a94d --- /dev/null +++ b/student-tests/helpers.py @@ -0,0 +1,39 @@ +from pathlib import Path +import sys +import unittest +from urllib.parse import urlencode + + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) +from client import Client + + +class ApiTestCase(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 assert_error(self, method, path, status, code, **kwargs): + result = self.client.call(method, path, status, **kwargs) + self.assertEqual(result["json"]["error"]["code"], code) + return result["json"]["error"] + + def read_all(self, path, actor, schema, limit=3): + items = [] + cursor = None + seen_cursors = set() + while True: + query = {"limit": limit} + if cursor is not None: + query["cursor"] = cursor + page = self.client.call("GET", path + "?" + urlencode(query), actor=actor, schema=schema)["json"] + self.assertLessEqual(len(page["items"]), limit) + items.extend(page["items"]) + cursor = page["next_cursor"] + if cursor is None: + return items + self.assertTrue(page["items"], "Пустая страница не должна вести к следующей") + self.assertNotIn(cursor, seen_cursors, "Зацикливание пагинации") + seen_cursors.add(cursor) diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 00000000..08940ae1 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,164 @@ +import base64 +import json +from urllib.parse import urlencode +from uuid import uuid4 + +from helpers import ApiTestCase + + +class ApiTests(ApiTestCase): + def test_duplicate_username(self): + self.assert_error("POST", "/api/v1/users", 409, "conflict", body=self.alice["registration"]) + + def test_invalid_user_fields(self): + invalid_bodies = [ + {}, [], None, + {"username": "ab", "display_name": "Имя"}, + {"username": "UPPER", "display_name": "Имя"}, + {"username": "name\n", "display_name": "Имя"}, + {"username": "язык", "display_name": "Имя"}, + {"username": "a" * 33, "display_name": "Имя"}, + {"username": 123, "display_name": "Имя"}, + {"username": "valid_name", "display_name": " \t\n"}, + {"username": "valid_name", "display_name": "👋" * 101}, + {"username": "valid_name", "display_name": False}, + {"username": "valid_name", "display_name": "Имя", "extra": 1}, + ] + for body in invalid_bodies: + if isinstance(body, dict): + body = {**body, "password": "Synthetic-password-123"} + with self.subTest(body=body): + self.assert_error("POST", "/api/v1/users", 400, "invalid_request", + raw=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) + + def test_malformed_json_and_encoding(self): + invalid_bodies = [ + b'{"username":', b'', b'not JSON', b'\xff', + '{"username":"valid_name","display_name":"Имя"}'.encode("utf-16"), + b'{"username":"valid_name","display_name":"\\ud800"}', + ] + for body in invalid_bodies: + with self.subTest(body=body): + self.assert_error("POST", "/api/v1/users", 400, "invalid_request", + raw=body, headers={"Content-Type": "application/json"}) + + def test_wrong_content_type(self): + self.assert_error("POST", "/api/v1/users", 415, "unsupported_media_type", + raw=b'{}', headers={"Content-Type": "text/plain"}) + + def test_identification_and_authentication_order(self): + self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized") + self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", + headers={"X-User-Id": self.alice["id"]}) + self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", + headers={"Authorization": "Bearer unknown-session"}) + self.assert_error("POST", self.path + "/messages", 401, "unauthorized", + raw=b'{', headers={"Content-Type": "application/json"}) + me = self.client.call("GET", "/api/v1/users/me", schema="User", actor=self.alice, + headers={"X-User-Id": self.bob["id"]})["json"] + self.assertEqual(me, self.alice["user"]) + + def test_invalid_chat_members(self): + invalid_members = [ + [], [self.bob["id"]], [self.alice["id"], str(uuid4())], + [self.alice["id"], self.alice["id"]], + [self.alice["id"], self.alice["id"].upper()], + [self.alice["id"], "not-a-uuid"], [True], self.alice["id"], + [self.alice["id"]] + [str(uuid4()) for _ in range(100)], + ] + for members in invalid_members: + with self.subTest(members=members): + self.assert_error("POST", "/api/v1/chats", 400, "invalid_request", actor=self.alice, + body={"title": "Чат", "member_ids": members}) + chats = self.read_all("/api/v1/chats", self.alice, "ChatPage") + self.assertEqual([chat["id"] for chat in chats], [self.chat["id"]]) + + def test_invalid_chat_title(self): + for title in ("", " \n", "a" * 101, 123, None): + with self.subTest(title=title): + self.assert_error("POST", "/api/v1/chats", 400, "invalid_request", actor=self.alice, + body={"title": title, "member_ids": [self.alice["id"]]}) + + def test_chat_access(self): + outsider = self.assert_error("GET", self.path, 404, "not_found", actor=self.eve) + missing = self.assert_error("GET", "/api/v1/chats/" + str(uuid4()), 404, "not_found", actor=self.eve) + self.assertEqual(outsider["message"], missing["message"]) + self.assert_error("GET", self.path + "/messages", 404, "not_found", actor=self.eve) + self.assert_error("POST", self.path + "/messages", 404, "not_found", actor=self.eve, body={"text": "Чужое"}) + self.assertEqual(self.read_all("/api/v1/chats", self.eve, "ChatPage"), []) + self.assert_error("GET", "/api/v1/chats/not-a-uuid", 400, "invalid_request", actor=self.alice) + + def test_invalid_message_fields(self): + for text in ("", " \t\n", "👋" * 4001, 123, True, None, []): + with self.subTest(text_type=type(text).__name__): + self.assert_error("POST", self.path + "/messages", 400, "invalid_request", + actor=self.alice, body={"text": text}) + self.assert_error("POST", self.path + "/messages", 400, "invalid_request", actor=self.alice, + body={"text": "Подмена автора", "sender_id": self.bob["id"]}) + self.assertEqual(self.read_all(self.path + "/messages", self.alice, "MessagePage"), []) + + def test_unicode_and_text_preservation(self): + for text in (" Привет, мир 👋\n", "👋" * 4000): + message = self.client.message(self.bob, self.chat, text)["json"] + self.assertEqual(message["text"], text) + self.assertEqual(message["sender_id"], self.bob["id"]) + items = self.read_all(self.path + "/messages", self.alice, "MessagePage") + self.assertEqual([item["text"] for item in items], [" Привет, мир 👋\n", "👋" * 4000]) + + def test_message_pagination_and_changing_limit(self): + sent = [self.client.message(self.alice, self.chat, f"Сообщение {i}")["json"] for i in range(8)] + items = self.read_all(self.path + "/messages", self.bob, "MessagePage", limit=3) + self.assertEqual(items, sorted(sent, key=lambda item: (item["created_at"], item["id"]))) + first = self.client.call("GET", self.path + "/messages?limit=1", actor=self.alice, schema="MessagePage")["json"] + query = urlencode({"limit": 100, "cursor": first["next_cursor"]}) + rest = self.client.call("GET", self.path + "/messages?" + query, actor=self.alice, schema="MessagePage")["json"] + self.assertEqual(first["items"] + rest["items"], items) + self.assertIsNone(rest["next_cursor"]) + + def test_default_page_size(self): + for i in range(51): + self.client.message(self.alice, self.chat, f"Сообщение {i}") + page = self.client.call("GET", self.path + "/messages", actor=self.alice, schema="MessagePage")["json"] + self.assertEqual(len(page["items"]), 50) + self.assertIsNotNone(page["next_cursor"]) + + def test_chat_pagination_and_cursor_owner(self): + for i in range(4): + self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, + body={"title": f"Чат {i}", "member_ids": [self.alice["id"], self.bob["id"]]}) + chats = self.read_all("/api/v1/chats", self.alice, "ChatPage", limit=2) + self.assertEqual(len(chats), 5) + self.assertEqual(len({chat["id"] for chat in chats}), 5) + self.assertEqual(chats, sorted(chats, key=lambda item: (item["created_at"], item["id"]))) + first = self.client.call("GET", "/api/v1/chats?limit=1", actor=self.alice, schema="ChatPage")["json"] + query = urlencode({"cursor": first["next_cursor"]}) + self.assert_error("GET", "/api/v1/chats?" + query, 400, "invalid_request", actor=self.bob) + + def test_invalid_limits_and_cursors(self): + bad_queries = [{"limit": value} for value in ("0", "101", "-1", "1.5", "1.0", "abc", "")] + bad_queries += [{"cursor": value} for value in ("", "not-base64!", "e30=", "W10=")] + for path in ("/api/v1/chats", self.path + "/messages"): + for query in bad_queries: + with self.subTest(path=path, query=query): + self.assert_error("GET", path + "?" + urlencode(query), 400, "invalid_request", actor=self.alice) + + def test_cursor_cannot_be_used_for_another_chat_or_changed(self): + for i in range(2): + self.client.message(self.alice, self.chat, f"Сообщение {i}") + first = self.client.call("GET", self.path + "/messages?limit=1", actor=self.alice, schema="MessagePage")["json"] + cursor = first["next_cursor"] + other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, schema="Chat", + body={"title": "Другой чат", "member_ids": [self.alice["id"]]})["json"] + path = "/api/v1/chats/" + other["id"] + "/messages?" + urlencode({"cursor": cursor}) + self.assert_error("GET", path, 400, "invalid_request", actor=self.alice) + data = json.loads(base64.urlsafe_b64decode(cursor)) + data["id"] = str(uuid4()) + changed = base64.urlsafe_b64encode(json.dumps(data).encode()).decode() + self.assert_error("GET", self.path + "/messages?" + urlencode({"cursor": changed}), + 400, "invalid_request", actor=self.alice) + + def test_framework_errors_and_request_ids(self): + first = self.assert_error("GET", "/unknown", 404, "not_found") + second = self.assert_error("GET", "/unknown", 404, "not_found") + self.assertNotEqual(first["request_id"], second["request_id"]) + self.assert_error("POST", "/health/live", 405, "invalid_request") diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..abebb619 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,47 @@ +from concurrent.futures import ThreadPoolExecutor +import threading +from uuid import uuid4 + +from helpers import ApiTestCase, Client + + +class ConcurrencyTests(ApiTestCase): + def test_ten_clients_send_one_hundred_messages(self): + barrier = threading.Barrier(10) + + def send_messages(worker): + client = Client() + actor = self.alice if worker % 2 == 0 else self.bob + barrier.wait(timeout=10) + sent = [] + for index in range(10): + message = client.message(actor, self.chat, f"parallel-{worker}-{index}")["json"] + self.assertEqual(message["sender_id"], actor["id"]) + sent.append(message) + return sent + + with ThreadPoolExecutor(max_workers=10) as pool: + batches = list(pool.map(send_messages, range(10))) + sent = [message for batch in batches for message in batch] + history = self.read_all(self.path + "/messages", self.alice, "MessagePage", limit=17) + self.assertEqual(len(history), 100) + self.assertEqual(len({message["id"] for message in history}), 100) + self.assertEqual(history, sorted(sent, key=lambda item: (item["created_at"], item["id"]))) + self.assertEqual({message["text"] for message in history}, + {f"parallel-{worker}-{index}" for worker in range(10) for index in range(10)}) + print("PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages.") + + def test_simultaneous_registration_of_one_username(self): + username = "race_" + uuid4().hex[:20] + barrier = threading.Barrier(10) + + def register(worker): + client = Client() + barrier.wait(timeout=10) + return client.call("POST", "/api/v1/users", [201, 409], + body={"username": username, "display_name": f"Клиент {worker}", "password": "Synthetic-password-123"})["status"] + + with ThreadPoolExecutor(max_workers=10) as pool: + statuses = list(pool.map(register, range(10))) + self.assertEqual(statuses.count(201), 1) + self.assertEqual(statuses.count(409), 9) diff --git a/student-tests/test_sessions.py b/student-tests/test_sessions.py new file mode 100644 index 00000000..d95c4d4b --- /dev/null +++ b/student-tests/test_sessions.py @@ -0,0 +1,51 @@ +import base64 +from uuid import uuid4 + +from helpers import ApiTestCase + + +class SessionTests(ApiTestCase): + def basic(self, username, password): + value = base64.b64encode((username + ":" + password).encode()).decode() + return {"Authorization": "Basic " + value} + + def test_wrong_password_and_basic_only_for_login(self): + registration = self.alice["registration"] + self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized", + headers=self.basic(registration["username"], "wrong-password")) + self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized") + self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized", + headers={"Authorization": "Basic not-base64!"}) + self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", + headers=self.basic(registration["username"], registration["password"])) + + def test_logout_revokes_only_one_session(self): + registration = self.alice["registration"] + second = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session", + headers=self.basic(registration["username"], registration["password"]))["json"] + self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice) + self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", actor=self.alice) + self.assert_error("DELETE", "/api/v1/auth/sessions/current", 401, "unauthorized", actor=self.alice) + self.client.call("GET", "/api/v1/users/me", schema="User", headers={"Authorization": "Bearer " + second["token"]}) + + def test_password_validation(self): + for password in ("short", "x" * 129, 123, None, " " * 12): + body = {"username": "pw_" + uuid4().hex[:20], "display_name": "Пароль", "password": password} + self.assert_error("POST", "/api/v1/users", 400, "invalid_request", body=body) + + def test_unicode_password_with_colon_and_private_response(self): + username = "pw_" + uuid4().hex[:20] + password = "Пароль:с:двоеточием-123" + user = self.client.call("POST", "/api/v1/users", 201, schema="User", + body={"username": username, "display_name": "Пароль", "password": password})["json"] + session = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session", + headers=self.basic(username, password))["json"] + self.assertEqual(session["user"], user) + self.assertEqual(set(user), {"id", "username", "display_name", "created_at"}) + self.assertGreaterEqual(len(session["token"]), 32) + + def test_sql_like_message_is_plain_text(self): + text = "'; DROP TABLE messages; -- Привет" + message = self.client.message(self.alice, self.chat, text)["json"] + self.assertEqual(message["text"], text) + self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [message]) -- 2.54.0 From 64ea89d04046a0d4eb761c8774d0b33cdb363bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BD=D0=B5=D0=BA?= <88394552+Lunatuk@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:51:19 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 +- .github/pull_request_template.md | 41 +- .github/workflows/application.yml | 9 +- REPORT.md | 188 +++- compose.yaml | 41 +- docs/schema.md | 13 +- evidence/lab3-checks.json | 1209 ++++++++++++++++++++++++ evidence/lab3-checks.txt | 403 ++++++++ infra/README.md | 6 + infra/haproxy.cfg | 40 + migrations/002_message_idempotency.sql | 10 + scripts/check-docker.sh | 2 +- scripts/demo.sh | 3 +- scripts/migrate.sh | 3 +- scripts/replicas.py | 90 ++ scripts/restart.sh | 2 +- scripts/rolling-update.sh | 8 + scripts/run-local.sh | 2 +- scripts/start.sh | 2 +- scripts/stop-one.sh | 5 + scripts/wait-ready.sh | 10 +- src/app.py | 10 +- src/config.py | 6 + src/migrate.py | 9 + src/storage.py | 20 +- student-tests/docker_lifecycle.py | 451 +++++---- student-tests/helpers.py | 3 + student-tests/test_idempotency.py | 62 ++ 28 files changed, 2411 insertions(+), 243 deletions(-) create mode 100644 evidence/lab3-checks.json create mode 100644 evidence/lab3-checks.txt create mode 100644 infra/haproxy.cfg create mode 100644 migrations/002_message_idempotency.sql create mode 100644 scripts/replicas.py create mode 100755 scripts/rolling-update.sh create mode 100755 scripts/stop-one.sh create mode 100644 student-tests/test_idempotency.py diff --git a/.env.example b/.env.example index 49918d8a..ca9d0120 100644 --- a/.env.example +++ b/.env.example @@ -1,14 +1,14 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. +# init_local.py copies this template to local .env and creates secrets separately. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab HOST_PORT=8080 SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. +# File paths contain no secret values. DATABASE_URL_FILE=/run/secrets/database_url # Imported implementation stores shared sessions in Valkey. SESSION_STORE=valkey CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 +# Contract requires at least 24 hours (86400 seconds). IDEMPOTENCY_TTL_SECONDS=86400 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index e6e63eb2..832e783c 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,31 @@ ## Лабораторная 3 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Савельев Иван, БВТ2304 +Целевая оценка (3/4/5): 4 +Трек 5, если заявлен: не заявлен -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Перенесены API, PostgreSQL, Valkey-сессии и миграции lab2. Добавлены HAProxy, +две API-реплики, X-Instance-Id и общая атомарная идемпотентность сообщений. +Миграции выполняются отдельным сервисом; реализованы остановка выбранной +реплики и поочерёдная замена под нагрузкой. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +Запуск: создать .venv, установить requirements.txt и requirements-dev.txt, +выполнить ./scripts/start.sh и ./scripts/check.sh. Полный отдельный тестовый +стенд: ./scripts/check-docker.sh. + +Проверки: make check/validate, публичные smoke/persistence/failover, +28 собственных тестов, SIGTERM, readiness при отказах зависимостей, +отказ каждой API-реплики и поочерёдная замена с RPS/ошибками/временем восстановления. +Команды, результаты и ограничения — в [REPORT.md](../REPORT.md), +вывод и исходные счётчики — evidence/lab3-checks.txt и evidence/lab3-checks.json. + +Ограничения: один хост; PostgreSQL, Valkey и LB без HA; возможны краткие +переходные ошибки; миграции перед заменой должны быть обратно совместимыми. +Использована помощь Codex; первичные источники перечислены в REPORT.md. + +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] make check и make test выполнены; приложен реальный вывод. +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml index 9d149345..6784a25c 100644 --- a/.github/workflows/application.yml +++ b/.github/workflows/application.yml @@ -15,13 +15,10 @@ jobs: - run: python -m venv .venv - run: .venv/bin/python -m pip install -r requirements.txt - run: ./scripts/check-docker.sh - - run: ./scripts/research.sh - uses: actions/upload-artifact@v4 if: always() with: - name: lab2-evidence + name: lab3-evidence path: | - evidence/docker-checks.txt - evidence/research.txt - evidence/research.json - evidence/query-plans.json + evidence/lab3-checks.txt + evidence/lab3-checks.json diff --git a/REPORT.md b/REPORT.md index c2679005..3eaa8924 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,181 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Савельев Иван, БВТ2304. +- Целевая оценка: **4**, весь уровень 3 и все пункты уровня 4. Треки оценки 5 не заявлены. +- Решение: Docker Compose + HAProxy, две реплики FastAPI, общий PostgreSQL и Valkey. +- Commit/PR: после публикации. Локальные проверки относятся к рабочим изменениям поверх `source_commit` из `evidence/lab3-checks.json`. + ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Engine с Compose v2, Python 3.11, Bash и make. Для демонстрации `demo.sh` также нужны curl и jq. Проверено на Docker Engine 29.0.1, Compose 2.40.3, macOS arm64. Команды выполнять из корня репозитория: -## Архитектура и решения +```sh +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements.txt -r requirements-dev.txt +./scripts/start.sh +./scripts/check.sh +./scripts/demo.sh +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +`start.sh` создаёт локальные `.env` и `secrets/`, собирает образ, поднимает PostgreSQL и Valkey, выполняет отдельный сервис `migrate`, запускает две API-реплики и HAProxy. API ожидают успешного завершения миграций. Скрипт возвращает управление после проверки обеих реплик через LB. Основной адрес — `http://localhost:8080`. -## Доказательства критериев +Пароль PostgreSQL генерируется случайно в `secrets/postgres_password`; DSN и URL Valkey находятся в `secrets/database_url` и `secrets/cache_url`. Существующие значения не перезаписываются. Каталог имеет права 0700, файлы — 0444 для чтения пользователем контейнера 10001. `.env`, `secrets/`, `.venv` исключены из Git и Docker build context. `.env.example` содержит настройки и пути, без значений секретов. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +| Переменная | Использование | +| --- | --- | +| APP_HOST, APP_PORT | Адрес API внутри контейнера: 0.0.0.0:8080 | +| HOST_PORT | Порт единственного публичного входа HAProxy на 127.0.0.1, по умолчанию 8080 | +| API_REPLICAS | Количество реплик сервиса api, по умолчанию 2; конфигурация LB содержит 10 мест | +| SESSION_STORE | valkey, общее хранилище серверных сессий | +| SESSION_TTL_SECONDS | TTL новой сессии, по умолчанию 3600; рестарт TTL не продлевает | +| DATABASE_URL_FILE, CACHE_URL_FILE | Пути к Docker secrets | +| IDEMPOTENCY_TTL_SECONDS | Срок результата повтора, по умолчанию 86400, меньше 24 часов запрещено | +| APP_ENV | Учебная метка окружения; поведение приложения не переключает | -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Если порт занят, например стендом lab2: -## Эксперименты и отказы +```sh +HOST_PORT=8081 ./scripts/start.sh +BASE_URL=http://localhost:8081 ./scripts/check.sh +``` -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Для дальнейших команд с нестандартным портом сохранить `HOST_PORT=8081` в `.env` или передавать его в окружении. Для HTTP-проверок передавать соответствующий `BASE_URL`. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +```sh +docker compose ps -a +docker compose logs --tail 30 api lb +./scripts/restart.sh # API, PostgreSQL, Valkey и LB; volumes сохраняются +./scripts/stop.sh # удаление контейнеров и сети с сохранением volumes -## Ограничения и следующие шаги +# Только для намеренного удаления учебных данных текущего Compose project: +docker compose down --volumes +``` -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Compose project `mtusi-lab3` отделён от lab2. Тестовый сценарий использует своё случайное имя и порт; в конце удаляет только свои тестовые контейнеры и volumes. + +## Архитектура и перенос из lab2 + +```text +Клиент → HAProxy :8080 → api × 2 → PostgreSQL (данные и результаты повторов) + ↘ Valkey (общие Bearer-сессии) +Отдельный migrate → PostgreSQL; завершается до первого запуска API +``` + +Сохранены пользователи, Argon2id, Basic-вход, Bearer-сессии с TTL и отзывом, чаты, проверки доступа, сообщения, единые ошибки, X-Request-Id и keyset-пагинация. PostgreSQL остаётся источником истины для данных. Valkey хранит SHA-256 токена → пользователя и время окончания, с AOF `appendfsync always`, `noeviction` и постоянным volume. Подтверждённые данные и действующие сессии переживают обычный рестарт. Локального состояния, от которого зависит корректность, в API нет. + +Добавлены `X-Instance-Id`, общая идемпотентность, LB и управление репликами. `X-Instance-Id` равен hostname контейнера и стабилен в течение жизни процесса. В доказательствах он сопоставлен с настоящим container ID; заголовок не подменяется балансировщиком. Любой ответ, сформированный API, включая health и ошибки, содержит оба служебных заголовка. + +HAProxy использует round robin без sticky session. `server-template` получает адреса масштабируемого сервиса `api` из Docker DNS `127.0.0.11`; записи переопрашиваются, устаревшие адреса удаляются. `hold valid/obsolete=1s`, повтор разрешения — 1s. Новые IP после пересоздания обнаруживаются без рестарта LB. `wait-ready.sh` проверяет Docker health всех работающих API и наблюдает каждую через LB. + +API не публикуют порты на хост, работают от UID/GID 10001, без volumes данных и без `container_name`. Только LB публикует порт. Секрет PostgreSQL доступен БД; DSN — API и мигратору; URL Valkey — только API. LB не получает секреты. PostgreSQL и Valkey используют отдельные именованные volumes. + +| Контейнер | CPU | RAM | +| --- | ---: | ---: | +| Каждый API | 1 | 256 MiB | +| HAProxy | 0.5 | 64 MiB | +| PostgreSQL | 1 | 512 MiB | +| Valkey | 0.5 | 128 MiB | +| Одноразовый migrate | 1 | 256 MiB | + +## Готовность, таймауты и SIGTERM + +`/health/live` проверяет жизнь процесса. `/health/ready` проверяет PostgreSQL и Valkey: обе зависимости критичны для обязательного синхронного пути. При отказе любой зависимости live остаётся 200, ready становится 503. При старте API также проверяет наличие и checksum всех миграций, не выполняя DDL. + +HAProxy проверяет `/health/ready` каждую 1 секунду. Два последовательных неуспеха исключают backend (`fall 2`), два успеха возвращают (`rise 2`). Таймаут подключения — 1s, ответа healthcheck — 1s. Консервативная верхняя оценка обнаружения отказа по этим настройкам — **6 секунд**: ожидание ближайшей проверки, два подключения/ответа и интервал между ними; задержки планирования ОС в оценку не входят. Отказ с немедленным connection refused обычно обнаруживается быстрее. Это укладывается в требование восстановления за 10 секунд. + +Остальные таймауты HAProxy: server 8s, client 10s, чтение HTTP-заголовков 5s. `retries 1` относится к установлению backend-соединения; повторов уже отправленных HTTP POST по статусам не включено. Клиент повторяет неопределённый POST сообщения с исходным ключом. + +API является PID 1. При SIGTERM Uvicorn прекращает приём новых соединений, даёт текущим запросам до 5 секунд и закрывает пул. Docker даёт 8 секунд до принудительного завершения. Проверка с незавершённым телом POST измеряет отказ новых соединений и выход до 8 секунд; проверяется `Application shutdown complete`, exit code 0 и отсутствие OOM. Переходные ошибки до исключения backend из LB учитываются в нагрузочном сценарии. + +## Общая идемпотентность + +Для POST сообщения требуется ровно один `Idempotency-Key` по шаблону `[A-Za-z0-9._:-]{1,128}`. Отсутствие и неверный формат дают 400. Область ключа — пользователь, POST, путь конкретного чата, ключ. Метод и тип пути фиксированы единственным обработчиком; таблица хранит `(user_id, chat_id, key)` как первичный ключ. + +В одной транзакции PostgreSQL: + +1. Проверяется членство в чате; сессия повторно проверяется middleware при каждом запросе. +2. Берётся транзакционная advisory lock для области ключа. Она общая для всех реплик; возможная коллизия хеша лишь сериализует независимые запросы. +3. При действующем результате сравнивается значение `text`, а не исходные JSON-байты. Совпадение возвращает прежний 201 и Message; другой текст даёт 409. +4. Для нового или истёкшего ключа создаются сообщение и строка результата с TTL, затем выполняется COMMIT. Ответ отправляется после COMMIT. + +Порядок/пробелы JSON и Unicode escaping не меняют результат. В контракте lab3 поле сообщения только `text`; вложения появляются с lab4. Ключи разных пользователей и чатов независимы. 5xx не сохраняется как результат; откат убирает и сообщение, и строку идемпотентности. При обрыве после COMMIT повтор на другой реплике читает прежнее сообщение. Результаты хранятся минимум 24 часа, переживают рестарт и замену API. Истёкшая строка заменяется при следующем использовании того же ключа; сообщения остаются в истории. + +## Миграции и поочерёдная замена + +`migrate` — один отдельный Compose-service, выполняющий SQL под общей advisory lock и сохраняющий SHA-256 версий. API только проверяют схему. Ручная команда — `./scripts/migrate.sh`; повтор безопасен и не создаёт повторных данных. + +Миграция `001_initial.sql` не изменялась. `002_message_idempotency.sql` только добавляет таблицу и не изменяет существующие поля. Она совместима с уже работающей версией приложения. При последующих изменениях разрешены только обратно совместимые добавления перед rolling update; удаление/переименование полей потребует отдельного перехода или окна обслуживания. Автоматическая совместимость произвольных миграций не заявляется. + +```sh +./scripts/rolling-update.sh +``` + +Скрипт сначала собирает образ и запускает миграции, затем для каждого исходного API-контейнера: проверяет наличие другой здоровой реплики, останавливает и удаляет выбранный контейнер, создаёт только недостающую реплику через `up --no-deps --no-recreate --scale`, ждёт её готовности через LB и переходит к следующей. PostgreSQL, Valkey и HAProxy не перезапускаются. Это поочерёдная замена с сохранением хотя бы одной готовой реплики; краткие переходные ошибки измеряются, нулевые ошибки не обещаются. + +## Команды проверки и доказательства + +```sh +make check validate PYTHON=.venv/bin/python +./scripts/check.sh +make test-persistence PYTHON=.venv/bin/python ACTION_SCRIPT=scripts/restart.sh +make test-failover PYTHON=.venv/bin/python ACTION_SCRIPT=scripts/stop-one.sh +docker compose start api +./scripts/wait-ready.sh + +# Полный воспроизводимый прогон в отдельном тестовом стенде: +./scripts/check-docker.sh +``` + +Для ручной остановки конкретной реплики задать `TARGET_INSTANCE_ID` из её `X-Instance-Id` и вызвать `./scripts/stop-one.sh`. Скрипт сопоставляет hostname контейнерам только текущего Compose project, требует минимум две работающие реплики, останавливает ровно выбранную и оставляет её остановленной. `docker compose ps -a` показывает фактическое состояние. Восстановление — `docker compose start api` и `./scripts/wait-ready.sh`. Полный сценарий последовательно повторяет отказ для каждой из двух реплик, восстанавливая предыдущую. + +| Критерий ТЗ | Проверка | Доказательство | +| --- | --- | --- | +| Воспроизводимый стенд, две реальные API-реплики и единый вход | start.sh, Docker inspect, общий Compose-service api | compose.yaml; containers в lab3-checks.json | +| Общая сессия, запросы через разные реплики | 20 параллельных повторов с одним Bearer-токеном | test_parallel_replay_across_replicas; lab3-checks.txt | +| Публичный контракт, включая ошибки и идемпотентность | make test, 28 собственных тестов | lab3-checks.txt | +| Рестарт всех компонентов, прежняя сессия и сообщения | make test-persistence, StartedAt до/после; повтор ключа после рестарта | persistence в lab3-checks.json | +| Публичный отказ одной выбранной реплики, восстановление ≤10s | make test-failover, реально остановленный container ID | public_failover; lab3-checks.txt | +| Отказ каждой API-реплики, чтения и записи под нагрузкой | Два отдельных load_action, восстановление предыдущей между ними | failovers в lab3-checks.json | +| Параллельная общая идемпотентность, нет дублей | 20 одновременных запросов через ≥2 реплики; сверка истории после отказов | test_idempotency.py; duplicate_messages=0 | +| Readiness, таймауты и SIGTERM | Отказ PostgreSQL/Valkey; незавершённый запрос + TERM | dependency_readiness и sigterm | +| CPU/RAM-лимиты, отсутствие обхода LB и container_name | Проверки Docker inspect для всех контейнеров | containers; compose.yaml | +| Поочерёдная замена под нагрузкой | rolling-update.sh, новые ID обоих API; неизменный StartedAt остальных | rolling_update в lab3-checks.json | +| Отдельные согласованные миграции | Одноразовый сервис, повторный запуск дважды без изменения данных | lab3-checks.txt; миграции 001/002 | + +Файлы текущего прогона: [evidence/lab3-checks.txt](evidence/lab3-checks.txt) и [evidence/lab3-checks.json](evidence/lab3-checks.json). Идентификаторы контейнеров, ресурсы, времена, ошибки и посекундные исходные счётчики сохранены без паролей и токенов. `evidence/docker-checks.txt`, `research.*`, `query-plans.json` и `REPORT.lab2.md` относятся ко второй лабораторной. + +## Измерения + +Дата прогона — 29.10.2026. Среда: macOS arm64, Docker VM Linux aarch64, 10 CPU, 8217448448 байт RAM. Лимиты контейнеров приведены выше. Профиль — четыре закрытых клиента: POST сообщения и GET истории с существующими сессиями, пауза 50ms между парами запросов. Для неопределённого POST сохраняются прежние ключ и тело. При отказе нагрузка длится 15 секунд после начала воздействия; при замене — весь action и минимум 3 секунды после. Перед воздействием есть 1 секунда прогрева, которая входит в общие счётчики. + +Численные результаты последнего успешного прогона приведены ниже. + +| Воздействие | Длительность, с | Успешные HTTP/с | Ошибки | Макс. пауза POST / GET, с | Потеря подтверждённых / дубли | +| --- | ---: | ---: | ---: | --- | --- | +| Остановка 7f6a80b69e32 | 16.009 | 98.44 | 4 | 1.957 / 2.004 | 0 / 0 | +| Остановка f973eaec4d52 | 16.015 | 98.28 | 4 | 2.006 / 2.006 | 0 / 0 | +| Поочерёдная замена обоих API | 23.774 | 89.64 | 11 | 1.059 / 2.034 | 0 / 0 | + +SIGTERM с незавершённым POST: новые соединения перестали приниматься за 0.167 с, выход за 5.393 с, exit code 0, OOM отсутствует. При остановке Valkey и PostgreSQL по отдельности подтверждены API live=200, API ready=503 и LB ready=503; после восстановления прежний токен продолжил работать. + +RPS считается по всем успешным HTTP-запросам чтения и записи за полную длительность сценария; ошибки считаются отдельно. «Первый успех» показывает продолжение обслуживания после начала action, а максимальный промежуток между успешными запросами дополнительно показывает паузы во всём сценарии. После каждого воздействия проверяются все подтверждённые сообщения, исходное сообщение до отказа, отсутствие повторяющихся ID/текстов и работа прежней сессии. + +Это короткие проверки отказоустойчивости для оценки 4. Нагрузка закрытая, присутствует coordinated omission; p50/p95 относятся только к успешным ответам. Предельная производительность и масштабирование 1/2/4 здесь не исследуются. + +## Ограничения + +Выдерживается отказ одной API-реплики при работающей другой. PostgreSQL, Valkey, HAProxy и сам хост остаются точками отказа; отказ узла и HA хранилищ не заявлены. Остановка БД или Valkey делает обязательный API неготовым. При полном рестарте допустимо окно недоступности, данные и действующие сессии сохраняются в volumes. Удаление volumes намеренно уничтожает данные. + +Нет автоматического TTL-удаления всех давно неиспользуемых ключей идемпотентности: они удаляются при повторном использовании ключа. Срок сессий зависит от часов; keyset-пагинация сохраняет ограничения lab2 при конкурентных записях и переводе часов. Поочерёдная замена останавливается при отсутствии здоровой резервной реплики, автоматический rollback образа не реализован. + +## Источники + +- [Docker Compose services, replicas, secrets и depends_on](https://docs.docker.com/reference/compose-file/services/). +- [Compose up: scale и no-recreate](https://docs.docker.com/reference/cli/docker/compose/up/). +- [HAProxy: DNS и server-template](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/proxying-essentials/dns-resolution/). +- [HAProxy: active health checks, inter/fall/rise](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/reliability/health-checks/). +- [PostgreSQL: advisory locks](https://www.postgresql.org/docs/17/explicit-locking.html#ADVISORY-LOCKS). +- [Uvicorn: graceful shutdown timeout](https://uvicorn.dev/settings/#timeouts). + +Стек приложения и закреплённые зависимости перенесены из lab2 (`requirements.txt`): Python 3.11.15, FastAPI 0.142.4, Uvicorn 0.54.0, Psycopg 3.3.6, PostgreSQL 17.6, Valkey 8.1.3; добавлен HAProxy 3.2.6-alpine. README задания, OpenAPI и публичные tests не изменялись. CI запускает контрактные и Docker-проверки; успешный удалённый запуск GitHub Actions без отдельного результата не заявлен. diff --git a/compose.yaml b/compose.yaml index 1d392383..0dad0dc8 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,5 +1,12 @@ name: mtusi-lab3 +x-application: &application + image: mtusi-messenger:lab3 + build: . + env_file: .env + cpus: 1 + mem_limit: 256m + x-postgres: &postgres image: postgres:17.6-bookworm environment: @@ -33,19 +40,41 @@ services: retries: 30 api: - image: mtusi-messenger:lab3 - build: . - env_file: .env + <<: *application + deploy: + replicas: ${API_REPLICAS:-2} environment: SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} secrets: [database_url, cache_url] - ports: ["127.0.0.1:${HOST_PORT:-8080}:8080"] - cpus: 1 - mem_limit: 256m depends_on: postgres: {condition: service_healthy} valkey: {condition: service_healthy} + migrate: {condition: service_completed_successfully} + stop_grace_period: 8s + + migrate: + <<: *application + command: ["python", "-m", "src.migrate"] + secrets: [database_url] + healthcheck: + disable: true + depends_on: + postgres: {condition: service_healthy} + + lb: + image: haproxy:3.2.6-alpine + ports: ["127.0.0.1:${HOST_PORT:-8080}:8080"] + volumes: [./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro] + cpus: 0.5 + mem_limit: 64m + depends_on: + api: {condition: service_healthy} stop_grace_period: 10s + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"] + interval: 2s + timeout: 2s + retries: 3 postgres-restore: <<: *postgres diff --git a/docs/schema.md b/docs/schema.md index c7d08fb0..3cf4052c 100644 --- a/docs/schema.md +++ b/docs/schema.md @@ -1,7 +1,7 @@ -# Схема и транзакции lab2 +# Схема и транзакции -Архитектура: HTTP → FastAPI → PostgreSQL; Bearer-сессии находятся в Valkey. -Выбран трек 2 на оценку 5: план запроса и восстановление. +Лабораторная 3, оценка 4: HTTP → HAProxy → FastAPI × 2 → PostgreSQL; +Bearer-сессии находятся в общем Valkey. Схема lab2 сохранена. | Таблица PostgreSQL | Назначение и ограничения | | --- | --- | @@ -10,13 +10,16 @@ | chat_members | PK (chat_id, user_id); FK на chats и users | | messages | UUID PK; FK chat_id на chats; составной FK (chat_id, sender_id) на chat_members; text; created_at timestamptz | | schema_migrations | Имя и SHA-256 применённой SQL-миграции; время применения | +| message_idempotency | PK (user_id, chat_id, key); FK на пользователя, чат и сообщение; request_text; expires_at | Индексы: users(username) создаётся UNIQUE-ограничением; chat_members(user_id, chat_id) нужен для списка чатов пользователя; messages(chat_id, created_at, id) нужен для истории с keyset cursor. PK служат индексами поиска по UUID. Каждая операция хранилища выполняется в одной транзакции: контекст соединения Psycopg делает COMMIT при успехе и ROLLBACK при исключении. Создание чата сначала вставляет chats, затем участников. Неизвестный участник вызывает FK violation и откатывает весь чат. Проверка уникальности username выполняется ограничением БД, включая конкурентные запросы. Создание сообщения проверяет доступ и записывает сообщение в одной транзакции; составной FK дополнительно запрещает автора вне чата. -Миграции выполняются под advisory transaction lock. Повторное применение пропускает уже записанную версию, изменение применённого SQL выявляется по checksum. Схема и запись версии фиксируются вместе. +Идемпотентная отправка проверяет членство, берёт общую транзакционную advisory lock для пользователя/метода/пути/ключа и проверяет предыдущий результат. Тот же text даёт прежнее Message, другой — 409. Сообщение и строка message_idempotency фиксируются одной транзакцией; при откате не сохраняется ни одно. TTL не меньше 24 часов. Истёкшая строка заменяется при повторном использовании ключа; старое сообщение сохраняется. + +Миграции выполняет отдельный одноразовый Compose-service migrate под advisory transaction lock. API только проверяют версии и checksum. Повторное применение пропускает уже записанную версию, изменение применённого SQL выявляется по checksum. Схема и запись версии фиксируются вместе. Миграция 002 только добавляет таблицу; существующая 001 не изменялась. Valkey — источник истины только для сессий: ключ session:SHA256(token), значение — user_id и абсолютное expires_at, TTL задаётся SESSION_TTL_SECONDS. Сырой токен выдаётся клиенту один раз и не записывается в логи. Включены AOF, appendfsync always и именованный volume. Logout удаляет ключ; исчезнувшая или просроченная сессия даёт 401. При недоступности Valkey — 503 без обхода аутентификации. При потере PostgreSQL — 503. /health/live остаётся доступным, /health/ready проверяет оба сервиса. -Для трека 5 отдельно исследуются индекс только по chat_id и составной индекс на одинаковых 100000 сообщениях с распределением 70000/20000/9000/1000. Измеряются чтение страниц, стоимость записи и размер индекса. Backup восстанавливается в другой контейнер PostgreSQL с новым volume; сравниваются счётчики, выбранные записи и контрольная сумма всей истории. +Исследование индекса и backup/restore в research/ перенесено из lab2. Для оценки 4 текущей лабораторной оно не требуется; результаты отказов и замены реплик описаны в [REPORT.md](../REPORT.md). diff --git a/evidence/lab3-checks.json b/evidence/lab3-checks.json new file mode 100644 index 00000000..8c497137 --- /dev/null +++ b/evidence/lab3-checks.json @@ -0,0 +1,1209 @@ +{ + "utc": "2026-10-29T10:13:09.874202+00:00", + "project": "lab3-check-a31f544685", + "base_url": "http://127.0.0.1:58474", + "host": "Darwin arm64", + "docker": "29.0.1 aarch64 CPUs=10 RAM=8217448448", + "profile": { + "workers": 4, + "pause_seconds": 0.05, + "operation": "POST message then GET history, same-key retry after uncertain POST" + }, + "compose_version": "2.40.3-desktop.1", + "source_commit": "74aa2242515a05192eceb4e3838e3eb4f032f397", + "working_tree": true, + "containers": [ + { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:15.412188555Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:15.325770597Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", + "instance": "2abbb539f04c", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-10-29T10:13:21.014548544Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "58474" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "a0a498f6df2fa9b5b03b24cb1ef2f3786139a8f744f71a6803e414867c391f94", + "instance": "a0a498f6df2f", + "service": "migrate", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:14.212270221Z", + "running": false, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", + "instance": "653f44c2806d", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-10-29T10:13:11.598535928Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-a31f544685_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", + "instance": "4aca2308d7a0", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-10-29T10:13:11.59306872Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab3-check-a31f544685_valkey_data" + } + ] + } + ], + "api_tests": "public smoke + 28 student tests passed", + "persistence": { + "before": [ + { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:15.412188555Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:15.325770597Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", + "instance": "2abbb539f04c", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-10-29T10:13:21.014548544Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "58474" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", + "instance": "653f44c2806d", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-10-29T10:13:11.598535928Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-a31f544685_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", + "instance": "4aca2308d7a0", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-10-29T10:13:11.59306872Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab3-check-a31f544685_valkey_data" + } + ] + } + ], + "after": [ + { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:35.473955634Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:35.398920009Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", + "instance": "2abbb539f04c", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-10-29T10:13:35.312310009Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "58474" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", + "instance": "653f44c2806d", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-10-29T10:13:35.342063384Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-a31f544685_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", + "instance": "4aca2308d7a0", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-10-29T10:13:35.37293605Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab3-check-a31f544685_valkey_data" + } + ] + } + ], + "passed": true, + "idempotency_replay_after_restart": true + }, + "sigterm": { + "instance": "7f6a80b69e32", + "unfinished_request": true, + "shutdown_seconds": 5.393, + "new_connections_refused_seconds": 0.167, + "exit_code": 0, + "oom_killed": false + }, + "dependency_readiness": [ + { + "dependency": "valkey", + "api_live": 200, + "api_ready": 503, + "lb_ready": 503 + }, + { + "dependency": "postgres", + "api_live": 200, + "api_ready": 503, + "lb_ready": 503 + } + ], + "public_failover": { + "passed": true, + "containers_after": [ + { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:48.981612751Z", + "running": false, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:35.398920009Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + } + ] + }, + "failovers": [ + { + "label": "stop 7f6a80b69e32", + "duration_seconds": 16.009, + "successful_requests": 1576, + "successful_writes": 788, + "errors": 4, + "successful_rps": 98.44, + "first_success_after_action_seconds": { + "POST": 0.014, + "GET": 0.007 + }, + "maximum_success_gap_seconds": { + "POST": 1.957, + "GET": 2.004 + }, + "action_start_seconds": 1.003, + "action_end_seconds": 1.666, + "p50_ms": 7.813, + "p95_ms": 16.393, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "7f6a80b69e32", + "f973eaec4d52" + ], + "per_second": [ + { + "second": 0, + "successes": 121, + "errors": 0 + }, + { + "second": 1, + "successes": 43, + "errors": 0 + }, + { + "second": 2, + "successes": 0, + "errors": 0 + }, + { + "second": 3, + "successes": 73, + "errors": 4 + }, + { + "second": 4, + "successes": 109, + "errors": 0 + }, + { + "second": 5, + "successes": 113, + "errors": 0 + }, + { + "second": 6, + "successes": 116, + "errors": 0 + }, + { + "second": 7, + "successes": 113, + "errors": 0 + }, + { + "second": 8, + "successes": 118, + "errors": 0 + }, + { + "second": 9, + "successes": 112, + "errors": 0 + }, + { + "second": 10, + "successes": 102, + "errors": 0 + }, + { + "second": 11, + "successes": 102, + "errors": 0 + }, + { + "second": 12, + "successes": 110, + "errors": 0 + }, + { + "second": 13, + "successes": 116, + "errors": 0 + }, + { + "second": 14, + "successes": 112, + "errors": 0 + }, + { + "second": 15, + "successes": 116, + "errors": 0 + }, + { + "second": 16, + "successes": 0, + "errors": 0 + } + ], + "error_samples": [ + [ + 3.329044791999877, + "GET", + "GET /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.363858082999741, + "POST", + "POST /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.367696166999849, + "POST", + "POST /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.4346232499997313, + "GET", + "GET /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" + ] + ], + "action": { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:14:23.665074878Z", + "running": false, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + } + }, + { + "label": "stop f973eaec4d52", + "duration_seconds": 16.015, + "successful_requests": 1574, + "successful_writes": 786, + "errors": 4, + "successful_rps": 98.28, + "first_success_after_action_seconds": { + "POST": 0.002, + "GET": 0.008 + }, + "maximum_success_gap_seconds": { + "POST": 2.006, + "GET": 2.006 + }, + "action_start_seconds": 1.005, + "action_end_seconds": 1.713, + "p50_ms": 7.98, + "p95_ms": 17.217, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "7f6a80b69e32", + "f973eaec4d52" + ], + "per_second": [ + { + "second": 0, + "successes": 112, + "errors": 0 + }, + { + "second": 1, + "successes": 43, + "errors": 0 + }, + { + "second": 2, + "successes": 0, + "errors": 0 + }, + { + "second": 3, + "successes": 71, + "errors": 4 + }, + { + "second": 4, + "successes": 117, + "errors": 0 + }, + { + "second": 5, + "successes": 115, + "errors": 0 + }, + { + "second": 6, + "successes": 109, + "errors": 0 + }, + { + "second": 7, + "successes": 116, + "errors": 0 + }, + { + "second": 8, + "successes": 109, + "errors": 0 + }, + { + "second": 9, + "successes": 118, + "errors": 0 + }, + { + "second": 10, + "successes": 115, + "errors": 0 + }, + { + "second": 11, + "successes": 115, + "errors": 0 + }, + { + "second": 12, + "successes": 109, + "errors": 0 + }, + { + "second": 13, + "successes": 105, + "errors": 0 + }, + { + "second": 14, + "successes": 113, + "errors": 0 + }, + { + "second": 15, + "successes": 104, + "errors": 0 + }, + { + "second": 16, + "successes": 3, + "errors": 0 + } + ], + "error_samples": [ + [ + 3.3455931250000503, + "POST", + "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.349944458000209, + "POST", + "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.3524864160003744, + "POST", + "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.41803337500005, + "GET", + "GET /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + ] + ], + "action": { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:13:35.398920009Z", + "running": false, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + } + } + ], + "rolling_update": { + "label": "sequential API replacement", + "duration_seconds": 23.774, + "successful_requests": 2131, + "successful_writes": 1063, + "errors": 11, + "successful_rps": 89.64, + "first_success_after_action_seconds": { + "POST": 0.018, + "GET": 0.004 + }, + "maximum_success_gap_seconds": { + "POST": 1.059, + "GET": 2.034 + }, + "action_start_seconds": 1.005, + "action_end_seconds": 19.63, + "p50_ms": 7.723, + "p95_ms": 13.565, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "244bf0b910eb", + "7f6a80b69e32", + "ca9c7a52da66", + "f973eaec4d52" + ], + "per_second": [ + { + "second": 0, + "successes": 121, + "errors": 0 + }, + { + "second": 1, + "successes": 116, + "errors": 0 + }, + { + "second": 2, + "successes": 109, + "errors": 0 + }, + { + "second": 3, + "successes": 104, + "errors": 0 + }, + { + "second": 4, + "successes": 114, + "errors": 0 + }, + { + "second": 5, + "successes": 33, + "errors": 0 + }, + { + "second": 6, + "successes": 60, + "errors": 4 + }, + { + "second": 7, + "successes": 105, + "errors": 0 + }, + { + "second": 8, + "successes": 117, + "errors": 0 + }, + { + "second": 9, + "successes": 119, + "errors": 0 + }, + { + "second": 10, + "successes": 116, + "errors": 0 + }, + { + "second": 11, + "successes": 115, + "errors": 0 + }, + { + "second": 12, + "successes": 28, + "errors": 0 + }, + { + "second": 13, + "successes": 5, + "errors": 4 + }, + { + "second": 14, + "successes": 74, + "errors": 0 + }, + { + "second": 15, + "successes": 120, + "errors": 0 + }, + { + "second": 16, + "successes": 119, + "errors": 0 + }, + { + "second": 17, + "successes": 115, + "errors": 0 + }, + { + "second": 18, + "successes": 114, + "errors": 0 + }, + { + "second": 19, + "successes": 120, + "errors": 0 + }, + { + "second": 20, + "successes": 116, + "errors": 0 + }, + { + "second": 21, + "successes": 88, + "errors": 0 + }, + { + "second": 22, + "successes": 0, + "errors": 0 + }, + { + "second": 23, + "successes": 3, + "errors": 3 + } + ], + "error_samples": [ + [ + 6.263843457999883, + "POST", + "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" + ], + [ + 6.297545500000069, + "POST", + "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" + ], + [ + 6.300835125000049, + "POST", + "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" + ], + [ + 6.369535666000047, + "GET", + "GET /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [200], received 503" + ], + [ + 13.222746333000032, + "POST", + "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" + ] + ], + "action": { + "before": [ + { + "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", + "instance": "7f6a80b69e32", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:14:47.987597625Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", + "instance": "f973eaec4d52", + "service": "api", + "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", + "started_at": "2026-10-29T10:15:11.976704845Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + } + ] + }, + { + "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", + "instance": "2abbb539f04c", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-10-29T10:13:35.312310009Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "58474" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", + "instance": "653f44c2806d", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-10-29T10:14:01.922362174Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-a31f544685_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", + "instance": "4aca2308d7a0", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-10-29T10:13:56.000628546Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab3-check-a31f544685_valkey_data" + } + ] + } + ], + "after": [ + { + "id": "ca9c7a52da66ca3c5ffad5cc6668a0d6d60876862683967b6d5f8e438eb630e0", + "instance": "ca9c7a52da66", + "service": "api", + "image": "sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8", + "started_at": "2026-10-29T10:15:24.374360503Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "244bf0b910eb2fa37937f8f0756a1162e9b339df076f09f4cff90b5c357037a6", + "instance": "244bf0b910eb", + "service": "api", + "image": "sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8", + "started_at": "2026-10-29T10:15:31.325585882Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", + "instance": "2abbb539f04c", + "service": "lb", + "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", + "started_at": "2026-10-29T10:13:35.312310009Z", + "running": true, + "exit_code": 0, + "memory_bytes": 67108864, + "nano_cpus": 500000000, + "published_ports": { + "8080/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "58474" + } + ] + }, + "mounts": [ + { + "type": "bind", + "destination": "/usr/local/etc/haproxy/haproxy.cfg", + "name": null + } + ] + }, + { + "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", + "instance": "653f44c2806d", + "service": "postgres", + "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", + "started_at": "2026-10-29T10:14:01.922362174Z", + "running": true, + "exit_code": 0, + "memory_bytes": 536870912, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-a31f544685_postgres_data" + }, + { + "type": "bind", + "destination": "/run/secrets/postgres_password", + "name": null + } + ] + }, + { + "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", + "instance": "4aca2308d7a0", + "service": "valkey", + "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", + "started_at": "2026-10-29T10:13:56.000628546Z", + "running": true, + "exit_code": 0, + "memory_bytes": 134217728, + "nano_cpus": 500000000, + "published_ports": {}, + "mounts": [ + { + "type": "volume", + "destination": "/data", + "name": "lab3-check-a31f544685_valkey_data" + } + ] + } + ] + } + }, + "passed": true +} diff --git a/evidence/lab3-checks.txt b/evidence/lab3-checks.txt new file mode 100644 index 00000000..27ec14ee --- /dev/null +++ b/evidence/lab3-checks.txt @@ -0,0 +1,403 @@ +Command: ./scripts/check-docker.sh; project: lab3-check-a31f544685 +Docker: 29.0.1 aarch64 CPUs=10 RAM=8217448448 +Local .env and secrets are ready; values are not printed +#1 [internal] load local bake definitions +#1 reading from stdin 1.05kB done +#1 DONE 0.0s + +#2 [migrate internal] load build definition from Dockerfile +#2 transferring dockerfile: 733B done +#2 DONE 0.0s + +#3 [api internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 DONE 0.0s + +#4 [api internal] load .dockerignore +#4 transferring context: 167B done +#4 DONE 0.0s + +#5 [migrate internal] load build context +#5 transferring context: 809B done +#5 DONE 0.0s + +#6 [migrate 1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 done +#6 DONE 0.0s + +#7 [migrate 2/8] WORKDIR /app +#7 CACHED + +#8 [migrate 3/8] COPY requirements.txt . +#8 CACHED + +#9 [migrate 4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#9 CACHED + +#10 [migrate 6/8] COPY src/ ./src/ +#10 CACHED + +#11 [migrate 7/8] COPY migrations/ ./migrations/ +#11 CACHED + +#12 [migrate 5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#12 CACHED + +#13 [migrate 8/8] COPY research/ ./research/ +#13 CACHED + +#14 [api] exporting to image +#14 exporting layers done +#14 exporting manifest sha256:06163e45d3676c7cf4f7a1c7bfe108ed3b98559ebb485c7a09d1e87e6f418d17 done +#14 exporting config sha256:3fed7460e9510eef7b2928f8d600d06d5d65329f74d820beac13809476b11398 done +#14 exporting attestation manifest sha256:352319cde788733f2f237a2a78204630c542e7ca906cff14c902046e8c1d2e5b 0.0s done +#14 exporting manifest list sha256:848e0c76f1678f35dd1b483e7c54552466999551ba7e77c0062bf346671e2902 done +#14 naming to docker.io/library/mtusi-messenger:lab3 done +#14 unpacking to docker.io/library/mtusi-messenger:lab3 done +#14 DONE 0.1s + +#15 [migrate] exporting to image +#15 exporting layers done +#15 exporting manifest sha256:a59568d34e2c5e8b4d23308342601fdaf8c07e78ec3065878679a067c082e952 done +#15 exporting config sha256:b67b805ba1b7dd90f0a3e8444455488fe8c7f4118f14d671501dd63e85b59c97 done +#15 exporting attestation manifest sha256:b1853c07ddac46c4b398b475052f334aa2753c2fe971e0e4fb2517b0e3e13656 0.0s done +#15 exporting manifest list sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb done +#15 naming to docker.io/library/mtusi-messenger:lab3 done +#15 unpacking to docker.io/library/mtusi-messenger:lab3 done +#15 DONE 0.1s + +#16 [api] resolving provenance for metadata file +#16 DONE 0.0s + +#17 [migrate] resolving provenance for metadata file +#17 DONE 0.0s + mtusi-messenger:lab3 Built + mtusi-messenger:lab3 Built + Network lab3-check-a31f544685_default Creating + Network lab3-check-a31f544685_default Created + Volume lab3-check-a31f544685_postgres_data Creating + Volume lab3-check-a31f544685_postgres_data Created + Volume lab3-check-a31f544685_valkey_data Creating + Volume lab3-check-a31f544685_valkey_data Created + Container lab3-check-a31f544685-valkey-1 Creating + Container lab3-check-a31f544685-postgres-1 Creating + Container lab3-check-a31f544685-valkey-1 Created + Container lab3-check-a31f544685-postgres-1 Created + Container lab3-check-a31f544685-migrate-1 Creating + Container lab3-check-a31f544685-migrate-1 Created + Container lab3-check-a31f544685-api-2 Creating + Container lab3-check-a31f544685-api-1 Creating + Container lab3-check-a31f544685-api-1 Created + Container lab3-check-a31f544685-api-2 Created + Container lab3-check-a31f544685-lb-1 Creating + Container lab3-check-a31f544685-lb-1 Created + Container lab3-check-a31f544685-valkey-1 Starting + Container lab3-check-a31f544685-postgres-1 Starting + Container lab3-check-a31f544685-valkey-1 Started + Container lab3-check-a31f544685-postgres-1 Started + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Starting + Container lab3-check-a31f544685-migrate-1 Started + Container lab3-check-a31f544685-migrate-1 Waiting + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-valkey-1 Waiting + Container lab3-check-a31f544685-valkey-1 Healthy + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Exited + Container lab3-check-a31f544685-api-2 Starting + Container lab3-check-a31f544685-api-2 Started + Container lab3-check-a31f544685-api-1 Starting + Container lab3-check-a31f544685-api-1 Started + Container lab3-check-a31f544685-api-2 Waiting + Container lab3-check-a31f544685-api-1 Waiting + Container lab3-check-a31f544685-api-2 Healthy + Container lab3-check-a31f544685-api-1 Healthy + Container lab3-check-a31f544685-lb-1 Starting + Container lab3-check-a31f544685-lb-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 +PASS: 2 real API containers, CPU/RAM limits, API non-root and no public ports; only LB published +Проверяем сервер: http://127.0.0.1:58474 +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/smoke.py --base-url "http://127.0.0.1:58474" +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. +test_chat_access (test_api.ApiTests.test_chat_access) ... ok +test_chat_pagination_and_cursor_owner (test_api.ApiTests.test_chat_pagination_and_cursor_owner) ... ok +test_cursor_cannot_be_used_for_another_chat_or_changed (test_api.ApiTests.test_cursor_cannot_be_used_for_another_chat_or_changed) ... ok +test_default_page_size (test_api.ApiTests.test_default_page_size) ... ok +test_duplicate_username (test_api.ApiTests.test_duplicate_username) ... ok +test_framework_errors_and_request_ids (test_api.ApiTests.test_framework_errors_and_request_ids) ... ok +test_identification_and_authentication_order (test_api.ApiTests.test_identification_and_authentication_order) ... ok +test_invalid_chat_members (test_api.ApiTests.test_invalid_chat_members) ... ok +test_invalid_chat_title (test_api.ApiTests.test_invalid_chat_title) ... ok +test_invalid_limits_and_cursors (test_api.ApiTests.test_invalid_limits_and_cursors) ... ok +test_invalid_message_fields (test_api.ApiTests.test_invalid_message_fields) ... ok +test_invalid_user_fields (test_api.ApiTests.test_invalid_user_fields) ... ok +test_malformed_json_and_encoding (test_api.ApiTests.test_malformed_json_and_encoding) ... ok +test_message_pagination_and_changing_limit (test_api.ApiTests.test_message_pagination_and_changing_limit) ... ok +test_unicode_and_text_preservation (test_api.ApiTests.test_unicode_and_text_preservation) ... ok +test_wrong_content_type (test_api.ApiTests.test_wrong_content_type) ... ok +test_simultaneous_registration_of_one_username (test_concurrency.ConcurrencyTests.test_simultaneous_registration_of_one_username) ... ok +test_ten_clients_send_one_hundred_messages (test_concurrency.ConcurrencyTests.test_ten_clients_send_one_hundred_messages) ... ok +test_conflict_and_semantic_json_replay (test_idempotency.IdempotencyTests.test_conflict_and_semantic_json_replay) ... ok +test_key_format_and_missing_key (test_idempotency.IdempotencyTests.test_key_format_and_missing_key) ... ok +test_parallel_replay_across_replicas (test_idempotency.IdempotencyTests.test_parallel_replay_across_replicas) ... ok +test_replay_rechecks_session_and_access (test_idempotency.IdempotencyTests.test_replay_rechecks_session_and_access) ... ok +test_scope_is_user_and_chat (test_idempotency.IdempotencyTests.test_scope_is_user_and_chat) ... ok +test_logout_revokes_only_one_session (test_sessions.SessionTests.test_logout_revokes_only_one_session) ... ok +test_password_validation (test_sessions.SessionTests.test_password_validation) ... ok +test_sql_like_message_is_plain_text (test_sessions.SessionTests.test_sql_like_message_is_plain_text) ... ok +test_unicode_password_with_colon_and_private_response (test_sessions.SessionTests.test_unicode_password_with_colon_and_private_response) ... ok +test_wrong_password_and_basic_only_for_login (test_sessions.SessionTests.test_wrong_password_and_basic_only_for_login) ... ok + +---------------------------------------------------------------------- +Ran 28 tests in 6.783s + +OK +PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages. + Container lab3-check-a31f544685-postgres-1 Running + Container lab3-check-a31f544685-postgres-1 Running +PASS: migrations only in one-shot service; repeated execution preserves data +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:58474" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +PASS: both API, LB, PostgreSQL, Valkey actually restarted; session and confirmed data preserved +PASS: SIGTERM with unfinished request: {"instance": "7f6a80b69e32", "unfinished_request": true, "shutdown_seconds": 5.393, "new_connections_refused_seconds": 0.167, "exit_code": 0, "oom_killed": false} + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Starting + Container lab3-check-a31f544685-migrate-1 Started + Container lab3-check-a31f544685-valkey-1 Waiting + Container lab3-check-a31f544685-migrate-1 Waiting + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-valkey-1 Healthy + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Exited + Container lab3-check-a31f544685-api-1 Starting + Container lab3-check-a31f544685-api-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 + Container lab3-check-a31f544685-valkey-1 Stopping + Container lab3-check-a31f544685-valkey-1 Stopped +PASS: valkey outage: API live=200, ready=503, LB ready=503 + Container lab3-check-a31f544685-valkey-1 Starting + Container lab3-check-a31f544685-valkey-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 + Container lab3-check-a31f544685-postgres-1 Stopping + Container lab3-check-a31f544685-postgres-1 Stopped +PASS: postgres outage: API live=200, ready=503, LB ready=503 + Container lab3-check-a31f544685-postgres-1 Starting + Container lab3-check-a31f544685-postgres-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:58474" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 113, "failed_attempts": 1, "first_recovery_seconds": 2.452} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Starting + Container lab3-check-a31f544685-migrate-1 Started + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-valkey-1 Waiting + Container lab3-check-a31f544685-migrate-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-valkey-1 Healthy + Container lab3-check-a31f544685-migrate-1 Exited + Container lab3-check-a31f544685-api-1 Starting + Container lab3-check-a31f544685-api-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 +Stopped API: 7f6a80b69e32 container=7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6 +{"label": "stop 7f6a80b69e32", "duration_seconds": 16.009, "successful_requests": 1576, "successful_writes": 788, "errors": 4, "successful_rps": 98.44, "first_success_after_action_seconds": {"POST": 0.014, "GET": 0.007}, "maximum_success_gap_seconds": {"POST": 1.957, "GET": 2.004}, "action_start_seconds": 1.003, "action_end_seconds": 1.666, "p50_ms": 7.813, "p95_ms": 16.393, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["7f6a80b69e32", "f973eaec4d52"]} + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Starting + Container lab3-check-a31f544685-migrate-1 Started + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-valkey-1 Waiting + Container lab3-check-a31f544685-migrate-1 Waiting + Container lab3-check-a31f544685-valkey-1 Healthy + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Exited + Container lab3-check-a31f544685-api-1 Starting + Container lab3-check-a31f544685-api-1 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 +Stopped API: f973eaec4d52 container=f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b +{"label": "stop f973eaec4d52", "duration_seconds": 16.015, "successful_requests": 1574, "successful_writes": 786, "errors": 4, "successful_rps": 98.28, "first_success_after_action_seconds": {"POST": 0.002, "GET": 0.008}, "maximum_success_gap_seconds": {"POST": 2.006, "GET": 2.006}, "action_start_seconds": 1.005, "action_end_seconds": 1.713, "p50_ms": 7.98, "p95_ms": 17.217, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["7f6a80b69e32", "f973eaec4d52"]} + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Starting + Container lab3-check-a31f544685-migrate-1 Started + Container lab3-check-a31f544685-postgres-1 Waiting + Container lab3-check-a31f544685-valkey-1 Waiting + Container lab3-check-a31f544685-migrate-1 Waiting + Container lab3-check-a31f544685-valkey-1 Healthy + Container lab3-check-a31f544685-postgres-1 Healthy + Container lab3-check-a31f544685-migrate-1 Exited + Container lab3-check-a31f544685-api-2 Starting + Container lab3-check-a31f544685-api-2 Started +API ready through LB: 7f6a80b69e32, f973eaec4d52 +Local .env and secrets are ready; values are not printed +#1 [internal] load local bake definitions +#1 reading from stdin 1.03kB done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 733B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 167B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 809B done +#5 DONE 0.0s + +#6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done +#6 DONE 0.0s + +#7 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#7 CACHED + +#8 [3/8] COPY requirements.txt . +#8 CACHED + +#9 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#9 CACHED + +#10 [6/8] COPY src/ ./src/ +#10 CACHED + +#11 [2/8] WORKDIR /app +#11 CACHED + +#12 [7/8] COPY migrations/ ./migrations/ +#12 CACHED + +#13 [8/8] COPY research/ ./research/ +#13 CACHED + +#14 exporting to image +#14 exporting layers done +#14 exporting manifest sha256:06163e45d3676c7cf4f7a1c7bfe108ed3b98559ebb485c7a09d1e87e6f418d17 done +#14 exporting config sha256:3fed7460e9510eef7b2928f8d600d06d5d65329f74d820beac13809476b11398 done +#14 exporting attestation manifest sha256:3a88ce0975ddcbab53c3ecb185da08ef114e818e9db952983e997cd47add67bf done +#14 exporting manifest list sha256:433a8a3293202a92f092574052b474e8373c0d58649cbcf1c7ebc1af17933d3c done +#14 naming to docker.io/library/mtusi-messenger:lab3 done +#14 unpacking to docker.io/library/mtusi-messenger:lab3 done +#14 DONE 0.1s + +#15 resolving provenance for metadata file +#15 DONE 0.0s + mtusi-messenger:lab3 Built +Local .env and secrets are ready; values are not printed + Container lab3-check-a31f544685-postgres-1 Running +#1 [internal] load local bake definitions +#1 reading from stdin 576B done +#1 DONE 0.0s + +#2 [internal] load build definition from Dockerfile +#2 transferring dockerfile: 733B done +#2 DONE 0.0s + +#3 [internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 DONE 0.0s + +#4 [internal] load .dockerignore +#4 transferring context: 167B done +#4 DONE 0.0s + +#5 [internal] load build context +#5 transferring context: 809B done +#5 DONE 0.0s + +#6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 done +#6 DONE 0.0s + +#7 [3/8] COPY requirements.txt . +#7 CACHED + +#8 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#8 CACHED + +#9 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#9 CACHED + +#10 [6/8] COPY src/ ./src/ +#10 CACHED + +#11 [7/8] COPY migrations/ ./migrations/ +#11 CACHED + +#12 [2/8] WORKDIR /app +#12 CACHED + +#13 [8/8] COPY research/ ./research/ +#13 CACHED + +#14 exporting to image +#14 exporting layers done +#14 exporting manifest sha256:a59568d34e2c5e8b4d23308342601fdaf8c07e78ec3065878679a067c082e952 done +#14 exporting config sha256:b67b805ba1b7dd90f0a3e8444455488fe8c7f4118f14d671501dd63e85b59c97 done +#14 exporting attestation manifest sha256:25ff7934ca5161da9b6c345af61b6a10b452580e4d349b35939d32feb66664ea done +#14 exporting manifest list sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8 done +#14 naming to docker.io/library/mtusi-messenger:lab3 done +#14 unpacking to docker.io/library/mtusi-messenger:lab3 done +#14 DONE 0.0s + +#15 resolving provenance for metadata file +#15 DONE 0.0s +Migrations: already applied +API ready through LB: 7f6a80b69e32, f973eaec4d52 +Replacing API: 7f6a80b69e32 + Container lab3-check-a31f544685-api-2 Running + Container lab3-check-a31f544685-api-3 Creating + Container lab3-check-a31f544685-api-3 Created + Container lab3-check-a31f544685-api-3 Starting + Container lab3-check-a31f544685-api-3 Started +API ready through LB: ca9c7a52da66, f973eaec4d52 +Replacing API: f973eaec4d52 + Container lab3-check-a31f544685-api-3 Running + Container lab3-check-a31f544685-api-4 Creating + Container lab3-check-a31f544685-api-4 Created + Container lab3-check-a31f544685-api-4 Starting + Container lab3-check-a31f544685-api-4 Started +API ready through LB: 244bf0b910eb, ca9c7a52da66 +PASS: all API containers replaced sequentially; stores and LB kept running +{"label": "sequential API replacement", "duration_seconds": 23.774, "successful_requests": 2131, "successful_writes": 1063, "errors": 11, "successful_rps": 89.64, "first_success_after_action_seconds": {"POST": 0.018, "GET": 0.004}, "maximum_success_gap_seconds": {"POST": 1.059, "GET": 2.034}, "action_start_seconds": 1.005, "action_end_seconds": 19.63, "p50_ms": 7.723, "p95_ms": 13.565, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["244bf0b910eb", "7f6a80b69e32", "ca9c7a52da66", "f973eaec4d52"]} +PASS: all lab3 grade-4 checks completed + Container lab3-check-a31f544685-lb-1 Stopping + Container lab3-check-a31f544685-lb-1 Stopped + Container lab3-check-a31f544685-lb-1 Removing + Container lab3-check-a31f544685-lb-1 Removed + Container lab3-check-a31f544685-api-3 Stopping + Container lab3-check-a31f544685-api-4 Stopping + Container lab3-check-a31f544685-api-4 Stopped + Container lab3-check-a31f544685-api-4 Removing + Container lab3-check-a31f544685-api-4 Removed + Container lab3-check-a31f544685-api-3 Stopped + Container lab3-check-a31f544685-api-3 Removing + Container lab3-check-a31f544685-api-3 Removed + Container lab3-check-a31f544685-migrate-1 Stopping + Container lab3-check-a31f544685-valkey-1 Stopping + Container lab3-check-a31f544685-migrate-1 Stopped + Container lab3-check-a31f544685-migrate-1 Removing + Container lab3-check-a31f544685-migrate-1 Removed + Container lab3-check-a31f544685-postgres-1 Stopping + Container lab3-check-a31f544685-valkey-1 Stopped + Container lab3-check-a31f544685-valkey-1 Removing + Container lab3-check-a31f544685-valkey-1 Removed + Container lab3-check-a31f544685-postgres-1 Stopped + Container lab3-check-a31f544685-postgres-1 Removing + Container lab3-check-a31f544685-postgres-1 Removed + Volume lab3-check-a31f544685_valkey_data Removing + Network lab3-check-a31f544685_default Removing + Volume lab3-check-a31f544685_postgres_data Removing + Volume lab3-check-a31f544685_valkey_data Removed + Volume lab3-check-a31f544685_postgres_data Removed + Network lab3-check-a31f544685_default Removed +Removed only temporary test containers and volumes: lab3-check-a31f544685 diff --git a/infra/README.md b/infra/README.md index 5a39e862..c5e025b7 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,5 +1,11 @@ # Инфраструктура студента +Решение lab3: [HAProxy](haproxy.cfg) → масштабируемый сервис `api` +(две реплики) → общие PostgreSQL и Valkey. Отдельный `migrate` выполняется +до запуска API. Публичный порт имеет только HAProxy; round robin без +sticky session, readiness раз в секунду, актуальные адреса из Docker DNS. +Команды запуска, лимиты, таймауты и результаты проверок — в [REPORT.md](../REPORT.md). + Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..72fffda1 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,40 @@ +global + log stdout format raw local0 + maxconn 256 + +defaults + mode http + log global + option dontlognull + option http-server-close + timeout connect 1s + timeout client 10s + timeout server 8s + timeout http-request 5s + timeout check 1s + retries 1 + +resolvers docker + nameserver dns 127.0.0.11:53 + accepted_payload_size 8192 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 1s + hold obsolete 1s + hold nx 1s + hold timeout 1s + hold refused 1s + hold other 1s + +frontend public + bind :8080 + default_backend api + +backend api + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host localhost + http-check expect status 200 + default-server inter 1s fastinter 1s downinter 1s fall 2 rise 2 + server-template replica 10 api:8080 check resolvers docker resolve-prefer ipv4 init-addr none diff --git a/migrations/002_message_idempotency.sql b/migrations/002_message_idempotency.sql new file mode 100644 index 00000000..375b58c4 --- /dev/null +++ b/migrations/002_message_idempotency.sql @@ -0,0 +1,10 @@ +-- Добавочная миграция: существующие данные и таблицы lab2 сохраняются. +CREATE TABLE message_idempotency ( + user_id uuid NOT NULL REFERENCES users(id), + chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + key varchar(128) NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'), + request_text text NOT NULL, + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + expires_at timestamptz NOT NULL, + PRIMARY KEY (user_id, chat_id, key) +); diff --git a/scripts/check-docker.sh b/scripts/check-docker.sh index 048ad8d1..438b6a3d 100755 --- a/scripts/check-docker.sh +++ b/scripts/check-docker.sh @@ -8,4 +8,4 @@ if [[ ! -x .venv/bin/python ]]; then fi ./scripts/build.sh mkdir -p evidence -.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | tee evidence/docker-checks.txt +.venv/bin/python -u student-tests/docker_lifecycle.py 2>&1 | sed -E 's/[[:blank:]]+$//' | tee evidence/lab3-checks.txt diff --git a/scripts/demo.sh b/scripts/demo.sh index 73f7a3c8..5d511280 100755 --- a/scripts/demo.sh +++ b/scripts/demo.sh @@ -22,6 +22,7 @@ chat_id="$(jq -nc --arg a "$alice_id" --arg b "$bob_id" '{title:"Учебный --data-binary @- "$base_url/api/v1/chats" | jq -er '.id')" for token in "$alice_token" "$bob_token"; do curl -fsS -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \ - -d '{"text":"Привет из lab2! 👋"}' "$base_url/api/v1/chats/$chat_id/messages" | jq . + -H "Idempotency-Key: demo-$RANDOM-$RANDOM" \ + -d '{"text":"Привет из lab3! 👋"}' "$base_url/api/v1/chats/$chat_id/messages" | jq . done curl -fsS -H "Authorization: Bearer $bob_token" "$base_url/api/v1/chats/$chat_id/messages" | jq . diff --git a/scripts/migrate.sh b/scripts/migrate.sh index 22da4961..a1874eaa 100755 --- a/scripts/migrate.sh +++ b/scripts/migrate.sh @@ -2,4 +2,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" -docker compose exec -T api python -m src.migrate +python3 scripts/init_local.py +docker compose run --rm --build migrate diff --git a/scripts/replicas.py b/scripts/replicas.py new file mode 100644 index 00000000..e9cfe3e6 --- /dev/null +++ b/scripts/replicas.py @@ -0,0 +1,90 @@ +"""Управление репликами только текущего Compose project через Docker CLI.""" +import argparse +import json +import os +from pathlib import Path +import subprocess +import time +from urllib.request import urlopen + +ROOT = Path(__file__).resolve().parents[1] + + +def command(*args): + return subprocess.check_output(args, cwd=ROOT, text=True, timeout=120).strip() + + +def replicas(): + ids = command("docker", "compose", "ps", "-q", "api").split() + return json.loads(command("docker", "inspect", *ids)) if ids else [] + + +def base_url(): + config = json.loads(command("docker", "compose", "config", "--format", "json")) + port = config["services"]["lb"]["ports"][0]["published"] + return os.environ.get("BASE_URL", "http://127.0.0.1:" + str(port)).rstrip("/") + + +def wait_ready(): + url = base_url() + "/health/ready" + deadline = time.monotonic() + 90 + seen = set() + while time.monotonic() < deadline: + current = replicas() + expected = {info["Config"]["Hostname"] for info in current} + if expected and all(info["State"].get("Health", {}).get("Status") == "healthy" for info in current): + try: + with urlopen(url, timeout=2) as response: + if response.status == 200: + seen.add(response.headers.get("X-Instance-Id")) + if expected <= seen: + print("API ready through LB:", ", ".join(sorted(expected)), flush=True) + return + except OSError: + pass + time.sleep(0.2) + raise RuntimeError("Не все работающие API-реплики готовы через LB за 90 секунд") + + +def stop_one(): + target = os.environ.get("TARGET_INSTANCE_ID") + if not target: + raise RuntimeError("Задайте TARGET_INSTANCE_ID из X-Instance-Id") + current = replicas() + matches = [info for info in current if info["Config"]["Hostname"] == target] + if len(current) < 2 or len(matches) != 1: + raise RuntimeError("Нужны минимум две работающие реплики и ровно одна выбранная") + selected = matches[0] + command("docker", "stop", "--time", "8", selected["Id"]) + print("Stopped API:", target, "container=" + selected["Id"], flush=True) + + +def rolling(): + wait_ready() + originals = replicas() + count = len(originals) + if count < 2: + raise RuntimeError("Для поочерёдной замены нужны минимум две API-реплики") + for old in originals: + active = replicas() + healthy = [info for info in active if info["Id"] != old["Id"] + and info["State"].get("Health", {}).get("Status") == "healthy"] + if not healthy: + raise RuntimeError("Нет готовой реплики, которая продолжит обслуживать запросы") + print("Replacing API:", old["Config"]["Hostname"], flush=True) + command("docker", "stop", "--time", "8", old["Id"]) + command("docker", "rm", old["Id"]) + # --no-recreate сохраняет остальные процессы; Compose создаёт только недостающий. + command("docker", "compose", "up", "-d", "--no-deps", "--no-recreate", "--scale", f"api={count}", "api") + wait_ready() + assert len(replicas()) == count + final = replicas() + assert not ({info["Id"] for info in originals} & {info["Id"] for info in final}) + print("PASS: all API containers replaced sequentially; stores and LB kept running", flush=True) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("action", choices=["wait", "stop", "rolling"]) + action = parser.parse_args().action + {"wait": wait_ready, "stop": stop_one, "rolling": rolling}[action]() diff --git a/scripts/restart.sh b/scripts/restart.sh index 23c94603..5375d646 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -2,5 +2,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" -docker compose restart --timeout 10 postgres valkey api +docker compose restart --timeout 8 postgres valkey api lb ./scripts/wait-ready.sh diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..7693a96e --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +# Сначала собираем образ и применяем только обратно совместимые миграции. +./scripts/build.sh +./scripts/migrate.sh +exec python3 scripts/replicas.py rolling diff --git a/scripts/run-local.sh b/scripts/run-local.sh index 19337594..1c0b0e7c 100755 --- a/scripts/run-local.sh +++ b/scripts/run-local.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash set -euo pipefail -# В lab2 локальный запуск включает API, PostgreSQL и Valkey. +# Локальный стенд: HAProxy, две API-реплики, PostgreSQL и Valkey. project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" exec "$project_dir/scripts/start.sh" diff --git a/scripts/start.sh b/scripts/start.sh index 464d62bb..a4133c69 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -3,5 +3,5 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" python3 scripts/init_local.py -docker compose up -d --build api +docker compose up -d --build lb ./scripts/wait-ready.sh diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..b77ec3f0 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +exec python3 scripts/replicas.py stop diff --git a/scripts/wait-ready.sh b/scripts/wait-ready.sh index 7ea7a709..da085770 100755 --- a/scripts/wait-ready.sh +++ b/scripts/wait-ready.sh @@ -2,12 +2,4 @@ set -euo pipefail project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$project_dir" -for ((attempt = 0; attempt < 60; attempt++)); do - if docker compose exec -T api python -m src.healthcheck >/dev/null 2>&1; then - echo "API ready" - exit 0 - fi - sleep 1 -done -echo "API не готов за 60 секунд. Проверь docker compose logs api" >&2 -exit 1 +exec python3 scripts/replicas.py wait diff --git a/src/app.py b/src/app.py index 84d9f41c..3155c8e4 100644 --- a/src/app.py +++ b/src/app.py @@ -3,6 +3,7 @@ from contextlib import asynccontextmanager import json import logging +import re from time import perf_counter from uuid import UUID, uuid4 @@ -14,13 +15,14 @@ from starlette.exceptions import HTTPException from src import auth, db, migrate, passwords, storage from src.errors import ApiError, error_response from src.models import CreateChat, CreateMessage, CreateUser +from src.config import INSTANCE_ID @asynccontextmanager async def lifespan(app): await run_in_threadpool(db.pool.open, wait=True, timeout=15) try: - await run_in_threadpool(migrate.apply) + await run_in_threadpool(migrate.check) yield finally: await run_in_threadpool(db.pool.close) @@ -60,6 +62,7 @@ async def handle_request(request: Request, call_next): logger.error("%s request_id=%s", type(error).__name__, request.state.request_id) response = error_response(request, ApiError(500, "internal_error", "Внутренняя ошибка сервера")) response.headers["X-Request-Id"] = request.state.request_id + response.headers["X-Instance-Id"] = INSTANCE_ID logger.info( "%s %s status=%s duration_ms=%.2f request_id=%s", request.method, path, response.status_code, @@ -129,7 +132,10 @@ def get_chat(request: Request, chat_id: UUID): @app.post("/api/v1/chats/{chat_id}/messages", status_code=201) def create_message(request: Request, chat_id: UUID, data: CreateMessage): - return storage.create_message(request.state.user["id"], str(chat_id), data.text) + keys = request.headers.getlist("Idempotency-Key") + if len(keys) != 1 or re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", keys[0]) is None: + raise ApiError(400, "invalid_request", "Нужен Idempotency-Key: 1–128 символов ASCII [A-Za-z0-9._:-]") + return storage.create_message(request.state.user["id"], str(chat_id), data.text, keys[0]) @app.get("/api/v1/chats/{chat_id}/messages") diff --git a/src/config.py b/src/config.py index 427fdb3e..c86331ce 100644 --- a/src/config.py +++ b/src/config.py @@ -1,5 +1,6 @@ """Настройки и чтение локальных Docker secrets.""" import os +import socket from pathlib import Path @@ -12,3 +13,8 @@ if SESSION_TTL < 1: raise ValueError("SESSION_TTL_SECONDS должен быть положительным") if os.environ.get("SESSION_STORE", "valkey") != "valkey": raise ValueError("В этой реализации SESSION_STORE=valkey") + +INSTANCE_ID = socket.gethostname() +IDEMPOTENCY_TTL = int(os.environ.get("IDEMPOTENCY_TTL_SECONDS", "86400")) +if IDEMPOTENCY_TTL < 86400: + raise ValueError("IDEMPOTENCY_TTL_SECONDS должен быть не меньше 86400") diff --git a/src/migrate.py b/src/migrate.py index 0c1410b6..c4bf5ee8 100644 --- a/src/migrate.py +++ b/src/migrate.py @@ -8,6 +8,15 @@ from src import db DIRECTORY = Path(__file__).resolve().parents[1] / "migrations" +def check(): + """API только проверяет схему; DDL выполняет отдельный migrate-service.""" + with db.connection() as conn: + for path in sorted(DIRECTORY.glob("*.sql")): + row = conn.execute("SELECT checksum FROM schema_migrations WHERE version=%s", (path.name,)).fetchone() + if row is None or row["checksum"] != hashlib.sha256(path.read_bytes()).hexdigest(): + raise RuntimeError("Сначала выполните миграции: " + path.name) + + def apply(): applied = [] with db.connection() as conn: diff --git a/src/storage.py b/src/storage.py index e4335208..3dbc765d 100644 --- a/src/storage.py +++ b/src/storage.py @@ -5,6 +5,7 @@ from psycopg.errors import ForeignKeyViolation, UniqueViolation from src import db, pagination from src.errors import ApiError +from src.config import IDEMPOTENCY_TTL def utc(value): @@ -97,11 +98,28 @@ def list_chats(user_id, limit, cursor): return pagination.page([public_chat(row) for row in rows], size, scope) -def create_message(user_id, chat_id, text): +def create_message(user_id, chat_id, text, key): with db.connection() as conn: require_member(conn, user_id, chat_id) + # Одна блокировка на область ключа во всех API-процессах. + # Побочный эффект и результат повтора фиксируются одной транзакцией. + scope = user_id + ":POST:/api/v1/chats/" + chat_id + "/messages:" + key + conn.execute("SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))", (scope,)) + previous = conn.execute("""SELECT m.*, i.request_text FROM message_idempotency i + JOIN messages m ON m.id=i.message_id + WHERE i.user_id=%s AND i.chat_id=%s AND i.key=%s AND i.expires_at>clock_timestamp()""", + (UUID(user_id), UUID(chat_id), key)).fetchone() + if previous: + if previous["request_text"] != text: + raise ApiError(409, "conflict", "Этот ключ уже использован с другим сообщением") + return public_message(previous) + conn.execute("DELETE FROM message_idempotency WHERE user_id=%s AND chat_id=%s AND key=%s", + (UUID(user_id), UUID(chat_id), key)) row = conn.execute("""INSERT INTO messages(id, chat_id, sender_id, text) VALUES (%s, %s, %s, %s) RETURNING *""", (uuid4(), UUID(chat_id), UUID(user_id), text)).fetchone() + conn.execute("""INSERT INTO message_idempotency(user_id, chat_id, key, request_text, message_id, expires_at) + VALUES (%s, %s, %s, %s, %s, clock_timestamp() + %s * interval '1 second')""", + (UUID(user_id), UUID(chat_id), key, text, row["id"], IDEMPOTENCY_TTL)) result = public_message(row) return result diff --git a/student-tests/docker_lifecycle.py b/student-tests/docker_lifecycle.py index bff249f5..c5e49e38 100644 --- a/student-tests/docker_lifecycle.py +++ b/student-tests/docker_lifecycle.py @@ -1,4 +1,5 @@ -"""Полный прогон lab2 в собственном Compose project; основные данные не затрагивает.""" +"""Проверки уровня 4 в отдельном Compose project с синтетическими данными.""" +from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timezone import json import os @@ -6,195 +7,307 @@ import platform import socket import subprocess import sys +import threading import time -from urllib.error import URLError +from urllib.error import HTTPError from urllib.request import urlopen from uuid import uuid4 from helpers import Client, ROOT +from client import Failure +from resilience import history def docker(*args): - return subprocess.run(["docker", *args], check=True, capture_output=True, text=True, timeout=120).stdout.strip() - - -def inspect(container): - return json.loads(docker("inspect", container))[0] - - -def check_shutdown(container, base_url, port): - with socket.create_connection(("127.0.0.1", port), timeout=2) as pending: - pending.sendall( - b"POST /api/v1/users HTTP/1.1\r\nHost: localhost\r\n" - b"Content-Type: application/json\r\nContent-Length: 1000\r\n" - b"Expect: 100-continue\r\nConnection: close\r\n\r\n" - ) - assert b"100 Continue" in pending.recv(1024), "Сервер не начал читать тело запроса" - pending.sendall(b'{"username":') - started = time.monotonic() - docker("kill", "--signal=TERM", container) - first_refusal = None - while inspect(container)["State"]["Running"]: - assert time.monotonic() - started < 10, "SIGTERM: превышены 10 секунд" - try: - with urlopen(base_url + "/health/live", timeout=0.3): - pass - except (URLError, OSError): - if first_refusal is None: - first_refusal = time.monotonic() - started - time.sleep(0.05) - elapsed = time.monotonic() - started - - assert elapsed < 10, "SIGTERM: превышены 10 секунд" - assert first_refusal is not None and first_refusal < elapsed, "Не показан отказ новых запросов при остановке" - state = inspect(container)["State"] - assert not state["OOMKilled"] and state["ExitCode"] in (0, 143), state - - result = subprocess.run(["docker", "logs", container], capture_output=True, text=True, check=True, timeout=10) - logs = result.stdout + result.stderr - assert "Application shutdown complete" in logs, "Нет подтверждения корректного завершения" - try: - with urlopen(base_url + "/health/live", timeout=0.5): - raise AssertionError("Остановленный сервер отвечает") - except (URLError, OSError): - pass - print(f"PASS: SIGTERM with unfinished request: {elapsed:.2f}s; new requests refused after {first_refusal:.2f}s; exit={state['ExitCode']}; OOM=false") + return subprocess.check_output(["docker", *args], text=True, timeout=180).strip() def main(): with socket.socket() as sock: sock.bind(("127.0.0.1", 0)) port = sock.getsockname()[1] - project = "lab2-check-" + uuid4().hex[:10] - env = dict(os.environ, COMPOSE_PROJECT_NAME=project, HOST_PORT=str(port), SESSION_TTL_SECONDS="3600") - base_url = f"http://127.0.0.1:{port}" + project = "lab3-check-" + uuid4().hex[:10] + base = f"http://127.0.0.1:{port}" + env = dict(os.environ, COMPOSE_PROJECT_NAME=project, HOST_PORT=str(port), + API_REPLICAS="2", SESSION_TTL_SECONDS="3600", BASE_URL=base) + result = {"utc": datetime.now(timezone.utc).isoformat(), "project": project, + "base_url": base, "host": platform.system() + " " + platform.machine(), + "docker": docker("info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}"), + "commit": docker("compose", "version", "--short"), + "profile": {"workers": 4, "pause_seconds": 0.05, "operation": "POST message then GET history, same-key retry after uncertain POST"}} + result["compose_version"] = result.pop("commit") + result["source_commit"] = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip() + result["working_tree"] = True - def compose(*args, capture=True, stdin=None): - return subprocess.run(["docker", "compose", *args], cwd=ROOT, env=env, input=stdin, - text=True, capture_output=capture, check=True, timeout=180) + def compose(*args): + return subprocess.check_output(["docker", "compose", *args], cwd=ROOT, env=env, text=True, timeout=180).strip() - def ready(): - subprocess.run([str(ROOT / "scripts/start.sh")], cwd=ROOT, env=env, - check=True, capture_output=True, text=True, timeout=180) - print("PASS: start.sh built and started the stack, readiness reached") + def script(name): + subprocess.run([str(ROOT / "scripts" / name)], cwd=ROOT, env=env, check=True, timeout=240) - def states(): - return {service: inspect(compose("ps", "-q", service).stdout.strip()) for service in ("api", "postgres", "valkey")} + def inspect_all(): + ids = compose("ps", "-a", "-q").split() + return json.loads(docker("inspect", *ids)) - def scalar(query, params=()): - code = """import json,sys -from src import db -data=json.load(sys.stdin) -db.pool.open(wait=True,timeout=15) -try: - with db.connection() as conn: - row=conn.execute(data['query'], data['params']).fetchone() - print(json.dumps(next(iter(row.values())))) -finally: - db.pool.close() + def apis(): + return [i for i in inspect_all() if i["Config"]["Labels"]["com.docker.compose.service"] == "api"] + + def metadata(info): + return {"id": info["Id"], "instance": info["Config"]["Hostname"], + "service": info["Config"]["Labels"]["com.docker.compose.service"], + "image": info["Image"], "started_at": info["State"]["StartedAt"], + "running": info["State"]["Running"], "exit_code": info["State"]["ExitCode"], + "memory_bytes": info["HostConfig"]["Memory"], "nano_cpus": info["HostConfig"]["NanoCpus"], + "published_ports": info["HostConfig"]["PortBindings"], + "mounts": [{"type": m["Type"], "destination": m["Destination"], "name": m.get("Name")} for m in info["Mounts"]]} + + def sql(query): + return compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-At", "-c", query) + + def check_shutdown(): + info = apis()[0] + # Внутреннее подключение через docker exec не публикует порт API на хост. + pending_code = """import socket,time +s=socket.create_connection(('127.0.0.1',8080),timeout=2) +s.sendall(b'POST /api/v1/users HTTP/1.1\\r\\nHost: localhost\\r\\nContent-Type: application/json\\r\\nContent-Length: 1000\\r\\nExpect: 100-continue\\r\\n\\r\\n') +assert b'100 Continue' in s.recv(1024) +s.sendall(b'{"username":') +print('pending',flush=True) +time.sleep(30) """ - return json.loads(compose("exec", "-T", "api", "python", "-c", code, - stdin=json.dumps({"query": query, "params": params})).stdout) - - print("UTC:", datetime.now(timezone.utc).isoformat()) - print("Host:", platform.system(), platform.machine(), "Python", platform.python_version()) - print("Docker:", docker("info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}")) - print("Commit:", subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip(), "+ working tree") - print("Command: ./scripts/check-docker.sh; project:", project) - try: - ready() - before = states() - for service, info in before.items(): - print(service, "image=" + info["Image"], "started=" + info["State"]["StartedAt"], - "memory=" + str(info["HostConfig"]["Memory"]), "nano_cpus=" + str(info["HostConfig"]["NanoCpus"])) - api = compose("ps", "-q", "api").stdout.strip() - assert docker("exec", api, "id", "-u") == "10001" - assert before["api"]["Config"]["Cmd"] == ["python", "-m", "src"] - for service in ("postgres", "valkey"): - assert any(mount["Type"] == "volume" for mount in before[service]["Mounts"]) - assert all(mount["Destination"].startswith("/run/secrets/") for mount in before["api"]["Mounts"]) - bad_health = subprocess.run(["docker", "exec", "-e", "APP_PORT=1", api, "python", "-m", "src.healthcheck"], capture_output=True) - assert bad_health.returncode == 1 - print("PASS: non-root UID 10001; Python PID 1; healthcheck; named volumes; no API data volume") - - subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url), check=True, timeout=240) - demo = subprocess.run([str(ROOT / "scripts/demo.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url), - check=True, capture_output=True, text=True, timeout=30) - assert "Привет из lab2!" in demo.stdout - print("PASS: demo.sh registered users, logged in and read two messages") - client = Client(base_url) - alice, bob, eve, chat = client.bootstrap() - marker = "private-" + uuid4().hex - client.message(alice, chat, marker) - logs = docker("logs", api) - assert marker not in logs and alice["token"] not in logs and alice["registration"]["password"] not in logs - logs_result = subprocess.run(["docker", "logs", api], capture_output=True, text=True, check=True) - assert "request_id=" in logs_result.stderr + logs_result.stdout - assert marker not in logs_result.stderr + logs_result.stdout - assert alice["token"] not in logs_result.stderr + logs_result.stdout - assert alice["registration"]["password"] not in logs_result.stderr + logs_result.stdout - assert scalar("SELECT bool_and(password_hash LIKE %s) FROM users", ("$argon2id$%",)) - print("PASS: Argon2id in PostgreSQL; logs do not contain message text, passwords or session tokens") - - title = "rollback_" + uuid4().hex - client.call("POST", "/api/v1/chats", 400, actor=alice, - body={"title": title, "member_ids": [alice["id"], str(uuid4())]}) - assert scalar("SELECT count(*) FROM chats WHERE title=%s", (title,)) == 0 - count = scalar("SELECT count(*) FROM messages") - compose("exec", "-T", "api", "python", "-m", "src.migrate") - compose("exec", "-T", "api", "python", "-m", "src.migrate") - assert scalar("SELECT count(*) FROM schema_migrations") == 1 - assert scalar("SELECT count(*) FROM messages") == count - print("PASS: failed membership insert rolled back chat; migrations run twice without duplicate data") - - before = states() - subprocess.run(["make", "test-persistence", "PYTHON=" + sys.executable, - "ACTION_SCRIPT=scripts/restart.sh", "BASE_URL=" + base_url], cwd=ROOT, env=env, check=True, timeout=240) - after = states() - for service in before: - assert before[service]["State"]["StartedAt"] != after[service]["State"]["StartedAt"] - print("Restart", service, before[service]["State"]["StartedAt"], "->", after[service]["State"]["StartedAt"]) - client.call("GET", "/api/v1/users/me", actor=alice, schema="User") - print("PASS: API, PostgreSQL and Valkey really restarted; previous token still works") - - for service in ("valkey", "postgres"): - try: - compose("stop", "--timeout", "10", service) - started = time.monotonic() - client.call("GET", "/api/v1/users/me", 503, actor=alice) - elapsed = time.monotonic() - started - assert elapsed < 6, elapsed - client.call("GET", "/api/v1/users/me", 401) - client.call("GET", "/health/ready", 503) - client.call("GET", "/health/live", schema="Health") - print(f"PASS: {service} unavailable -> 503 in {elapsed:.2f}s; anonymous -> 401; readiness 503, liveness 200") - finally: - compose("start", service) - subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90) - client.call("GET", "/api/v1/users/me", actor=alice, schema="User") - print("PASS:", service, "recovered without a new login") - - env["SESSION_TTL_SECONDS"] = "2" - compose("up", "-d", "--force-recreate", "api") - subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90) - short = client.user("short_ttl") - client.call("GET", "/api/v1/users/me", actor=short, schema="User") - time.sleep(2.2) - client.call("GET", "/api/v1/users/me", 401, actor=short) - print("PASS: SESSION_TTL_SECONDS=2; valid token expires without logout") - api = compose("ps", "-q", "api").stdout.strip() - check_shutdown(api, base_url, port) - print("PASS: all lab2 Docker checks completed") - finally: + pending = subprocess.Popen(["docker", "exec", info["Id"], "python", "-u", "-c", pending_code], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) try: - subprocess.run([str(ROOT / "scripts/stop.sh")], cwd=ROOT, env=env, - check=True, capture_output=True, text=True, timeout=90) - volumes = docker("volume", "ls", "--quiet", "--filter", "label=com.docker.compose.project=" + project) - if volumes: - print("PASS: stop.sh removed containers and preserved named volumes") + assert pending.stdout.readline().strip() == "pending" + started = time.monotonic() + docker("kill", "--signal=TERM", info["Id"]) + first_refusal = None + while True: + state = json.loads(docker("inspect", info["Id"]))[0]["State"] + if not state["Running"]: + break + assert time.monotonic() - started < 8 + probe = subprocess.run(["docker", "exec", info["Id"], "python", "-c", + "import socket; s=socket.socket(); s.settimeout(.2); raise SystemExit(s.connect_ex(('127.0.0.1',8080)))"], + capture_output=True, timeout=3) + if probe.returncode and first_refusal is None: + first_refusal = time.monotonic() - started + time.sleep(0.1) + elapsed = time.monotonic() - started + assert first_refusal is not None and first_refusal < elapsed < 8 + assert state["ExitCode"] == 0 and not state["OOMKilled"] + logs = subprocess.run(["docker", "logs", info["Id"]], capture_output=True, text=True, check=True) + assert "Application shutdown complete" in logs.stdout + logs.stderr + result["sigterm"] = {"instance": info["Config"]["Hostname"], "unfinished_request": True, + "shutdown_seconds": round(elapsed, 3), "new_connections_refused_seconds": round(first_refusal, 3), + "exit_code": state["ExitCode"], "oom_killed": state["OOMKilled"]} + print("PASS: SIGTERM with unfinished request:", json.dumps(result["sigterm"]), flush=True) finally: - compose("down", "--volumes", "--timeout", "10") - print("Removed only temporary Compose project and its test volumes:", project) + pending.communicate(timeout=10) + compose("start", "api") + script("wait-ready.sh") + + def load_action(label, action, target=None): + client = Client(base, timeout=2) + alice, bob, _, chat = client.bootstrap() + original = client.message(alice, chat, "До воздействия")["json"] + stop = threading.Event() + lock = threading.Lock() + successes, errors, confirmed = [], [], {} + started = time.monotonic() + + def worker(index): + c = Client(base, timeout=2) + key = uuid4().hex + while not stop.is_set(): + for method in ("POST", "GET"): + try: + if method == "POST": + response = c.message(alice, chat, "load-" + key, key=key) + with lock: + confirmed[response["json"]["id"]] = response["json"] + key = uuid4().hex + else: + response = c.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", + actor=bob, schema="MessagePage") + with lock: + successes.append((time.monotonic() - started, method, + response["headers"]["X-Instance-Id"], response["elapsed"])) + except Failure as exc: + with lock: + errors.append((time.monotonic() - started, method, str(exc))) + stop.wait(0.05) + + with ThreadPoolExecutor(max_workers=4) as pool: + futures = [pool.submit(worker, i) for i in range(4)] + time.sleep(1) + action_start = time.monotonic() - started + try: + action_info = action() + action_end = time.monotonic() - started + # Охватывает обнаружение отказа и устойчивую работу после него. + time.sleep(max(3, 15 - (action_end - action_start))) + finally: + stop.set() + for future in futures: + future.result() + elapsed = time.monotonic() - started + items = history(client, bob, chat) + by_id = {m["id"]: m for m in items} + assert len(items) == len(by_id) == len({m["text"] for m in items}), "Повтор создал дубль" + assert by_id.get(original["id"]) == original + assert all(by_id.get(mid) == msg for mid, msg in confirmed.items()), "Потеря подтверждённого сообщения" + after = [s for s in successes if s[0] >= action_end] + assert {s[1] for s in after} == {"POST", "GET"}, "Нет чтений/записей после воздействия" + recovery = {method: round(min(s[0] for s in successes if s[0] >= action_start and s[1] == method) - action_start, 3) + for method in ("POST", "GET")} + assert max(recovery.values()) <= 10, recovery + max_gap = {} + for method in ("POST", "GET"): + moments = sorted(s[0] for s in successes if s[1] == method) + max_gap[method] = round(max(b - a for a, b in zip(moments, moments[1:])), 3) + assert max(max_gap.values()) <= 10, max_gap + if target: + assert target not in {s[2] for s in after[-20:]}, "Остановленная реплика ещё отвечает" + buckets = [] + for second in range(int(elapsed) + 1): + buckets.append({"second": second, "successes": sum(second <= s[0] < second + 1 for s in successes), + "errors": sum(second <= e[0] < second + 1 for e in errors)}) + latencies = sorted(s[3] * 1000 for s in successes) + summary = {"label": label, "duration_seconds": round(elapsed, 3), "successful_requests": len(successes), + "successful_writes": len(confirmed), "errors": len(errors), "successful_rps": round(len(successes) / elapsed, 2), + "first_success_after_action_seconds": recovery, "maximum_success_gap_seconds": max_gap, + "action_start_seconds": round(action_start, 3), "action_end_seconds": round(action_end, 3), + "p50_ms": round(latencies[len(latencies) // 2], 3), + "p95_ms": round(latencies[int(len(latencies) * .95)], 3), + "confirmed_data_preserved": True, "duplicate_messages": 0, + "instances": sorted({s[2] for s in successes}), "per_second": buckets, + "error_samples": errors[:5], "action": action_info} + print(json.dumps({k: v for k, v in summary.items() if k not in ("per_second", "error_samples", "action")}, ensure_ascii=False), flush=True) + return summary + + print("Command: ./scripts/check-docker.sh; project:", project, flush=True) + print("Docker:", result["docker"], flush=True) + try: + script("start.sh") + initial = inspect_all() + result["containers"] = [metadata(info) for info in initial] + assert len(apis()) == 2 + for info in initial: + assert info["HostConfig"]["Memory"] > 0 and info["HostConfig"]["NanoCpus"] > 0 + if info["Config"]["Labels"]["com.docker.compose.service"] != "lb": + assert not info["HostConfig"]["PortBindings"] + for info in apis(): + assert docker("exec", info["Id"], "id", "-u") == "10001" + assert all(m["Destination"].startswith("/run/secrets/") for m in info["Mounts"]) + print("PASS: 2 real API containers, CPU/RAM limits, API non-root and no public ports; only LB published", flush=True) + subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=env, check=True, timeout=240) + result["api_tests"] = "public smoke + 28 student tests passed" + subprocess.run([str(ROOT / "scripts/demo.sh")], cwd=ROOT, env=env, check=True, stdout=subprocess.DEVNULL, timeout=30) + assert sql("SELECT count(*) FROM schema_migrations") == "2" + count = sql("SELECT count(*) FROM messages") + for _ in range(2): + compose("run", "--rm", "migrate") + assert sql("SELECT count(*) FROM schema_migrations") == "2" + assert sql("SELECT count(*) FROM messages") == count + print("PASS: migrations only in one-shot service; repeated execution preserves data", flush=True) + + replay_client = Client(base) + replay_actor, _, _, replay_chat = replay_client.bootstrap() + replay_key = uuid4().hex + saved_message = replay_client.message(replay_actor, replay_chat, "Повтор после рестарта", key=replay_key)["json"] + before = {i["Id"]: metadata(i) for i in inspect_all() if i["State"]["Running"]} + subprocess.run(["make", "test-persistence", "PYTHON=" + sys.executable, "ACTION_SCRIPT=scripts/restart.sh"], + cwd=ROOT, env=env, check=True, timeout=180) + after = {i["Id"]: metadata(i) for i in inspect_all() if i["State"]["Running"]} + assert before.keys() == after.keys() + assert all(before[cid]["started_at"] != after[cid]["started_at"] for cid in before) + assert replay_client.message(replay_actor, replay_chat, "Повтор после рестарта", key=replay_key)["json"] == saved_message + result["persistence"] = {"before": list(before.values()), "after": list(after.values()), "passed": True, + "idempotency_replay_after_restart": True} + print("PASS: both API, LB, PostgreSQL, Valkey actually restarted; session and confirmed data preserved", flush=True) + + check_shutdown() + + dependency_client = Client(base) + actor = dependency_client.user("deps") + result["dependency_readiness"] = [] + for dependency in ("valkey", "postgres"): + try: + compose("stop", "--timeout", "8", dependency) + probe_id = apis()[0]["Id"] + readiness = subprocess.run(["docker", "exec", probe_id, "python", "-m", "src.healthcheck"], capture_output=True, timeout=10) + assert readiness.returncode == 1 + live = docker("exec", probe_id, "python", "-c", + "from urllib.request import urlopen; print(urlopen('http://127.0.0.1:8080/health/live').status)") + assert live == "200" + deadline = time.monotonic() + 8 + while True: + try: + urlopen(base + "/health/ready", timeout=2).close() + except HTTPError as error: + if error.code == 503: + break + raise + except OSError: + # Уже отправленный запрос может истечь до исключения backend. + pass + assert time.monotonic() < deadline + time.sleep(0.1) + result["dependency_readiness"].append({"dependency": dependency, "api_live": 200, "api_ready": 503, "lb_ready": 503}) + print("PASS:", dependency, "outage: API live=200, ready=503, LB ready=503", flush=True) + finally: + compose("start", dependency) + script("wait-ready.sh") + dependency_client.call("GET", "/api/v1/users/me", actor=actor, schema="User") + + subprocess.run(["make", "test-failover", "PYTHON=" + sys.executable, "ACTION_SCRIPT=scripts/stop-one.sh"], + cwd=ROOT, env=env, check=True, timeout=90) + assert sum(i["State"]["Running"] for i in apis()) == 1 + result["public_failover"] = {"passed": True, "containers_after": [metadata(i) for i in apis()]} + compose("start", "api") + script("wait-ready.sh") + + result["failovers"] = [] + for info in apis(): + target = info["Config"]["Hostname"] + + def stop_target(): + subprocess.run([str(ROOT / "scripts/stop-one.sh")], cwd=ROOT, + env=dict(env, TARGET_INSTANCE_ID=target), check=True, timeout=10) + stopped = json.loads(docker("inspect", info["Id"]))[0] + assert not stopped["State"]["Running"] + assert not stopped["State"]["OOMKilled"] and stopped["State"]["ExitCode"] == 0 + # Uvicorn пишет в stderr; получаем оба потока без вывода запросов. + logs_result = subprocess.run(["docker", "logs", info["Id"]], capture_output=True, text=True, check=True) + assert "Application shutdown complete" in logs_result.stdout + logs_result.stderr + return metadata(stopped) + + result["failovers"].append(load_action("stop " + target, stop_target, target)) + compose("start", "api") + script("wait-ready.sh") + + before = {i["Id"]: metadata(i) for i in inspect_all() if i["State"]["Running"]} + + def replace(): + script("rolling-update.sh") + after = {i["Id"]: metadata(i) for i in inspect_all() if i["State"]["Running"]} + old_api = {i["id"] for i in before.values() if i["service"] == "api"} + new_api = {i["id"] for i in after.values() if i["service"] == "api"} + assert len(new_api) == 2 and not old_api & new_api + for cid, info in before.items(): + if info["service"] != "api": + assert after[cid]["started_at"] == info["started_at"] + return {"before": list(before.values()), "after": list(after.values())} + + result["rolling_update"] = load_action("sequential API replacement", replace) + result["passed"] = True + print("PASS: all lab3 grade-4 checks completed", flush=True) + finally: + output = ROOT / "evidence" / "lab3-checks.json" + output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n") + compose("down", "--volumes", "--timeout", "8") + print("Removed only temporary test containers and volumes:", project, flush=True) if __name__ == "__main__": diff --git a/student-tests/helpers.py b/student-tests/helpers.py index 33a9a94d..d44e6fd0 100644 --- a/student-tests/helpers.py +++ b/student-tests/helpers.py @@ -2,6 +2,7 @@ from pathlib import Path import sys import unittest from urllib.parse import urlencode +from uuid import uuid4 ROOT = Path(__file__).resolve().parents[1] @@ -16,6 +17,8 @@ class ApiTestCase(unittest.TestCase): self.path = "/api/v1/chats/" + self.chat["id"] def assert_error(self, method, path, status, code, **kwargs): + if method == "POST" and path.endswith("/messages"): + kwargs["headers"] = {"Idempotency-Key": uuid4().hex, **kwargs.get("headers", {})} result = self.client.call(method, path, status, **kwargs) self.assertEqual(result["json"]["error"]["code"], code) return result["json"]["error"] diff --git a/student-tests/test_idempotency.py b/student-tests/test_idempotency.py new file mode 100644 index 00000000..a53fd30b --- /dev/null +++ b/student-tests/test_idempotency.py @@ -0,0 +1,62 @@ +from concurrent.futures import ThreadPoolExecutor +import json +import threading +from uuid import uuid4 + +from helpers import ApiTestCase, Client + + +class IdempotencyTests(ApiTestCase): + def test_parallel_replay_across_replicas(self): + key = uuid4().hex + barrier = threading.Barrier(20) + + def send(_): + client = Client() + barrier.wait(timeout=10) + return client.message(self.alice, self.chat, "Одно сообщение 👋", key=key) + + with ThreadPoolExecutor(max_workers=20) as pool: + responses = list(pool.map(send, range(20))) + self.assertGreaterEqual(len({r["headers"]["X-Instance-Id"] for r in responses}), 2) + self.assertTrue(all(r["json"] == responses[0]["json"] for r in responses)) + self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [responses[0]["json"]]) + + def test_key_format_and_missing_key(self): + for key in (None, "", " ", "a b", "a/b", "a" * 129): + with self.subTest(key=key): + headers = {} if key is None else {"Idempotency-Key": key} + self.client.call("POST", self.path + "/messages", 400, actor=self.alice, + body={"text": "Текст"}, headers=headers) + for key in ("A", "a" * 128, "ABCdef012._:-"): + self.client.message(self.alice, self.chat, key, key=key) + + def test_conflict_and_semantic_json_replay(self): + key = uuid4().hex + first = self.client.message(self.alice, self.chat, "Текст 👋", key=key)["json"] + replay = self.client.call("POST", self.path + "/messages", 201, actor=self.alice, + raw=json.dumps({"text": "Текст 👋"}, ensure_ascii=True, indent=2).encode(), + headers={"Content-Type": "application/json", "Idempotency-Key": key}, + schema="Message")["json"] + self.assertEqual(first, replay) + self.assert_error("POST", self.path + "/messages", 409, "conflict", actor=self.alice, + body={"text": "Другой"}, headers={"Idempotency-Key": key}) + self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [first]) + + def test_scope_is_user_and_chat(self): + key = uuid4().hex + first = self.client.message(self.alice, self.chat, "Алиса", key=key)["json"] + second = self.client.message(self.bob, self.chat, "Боб", key=key)["json"] + other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, schema="Chat", + body={"title": "Другой чат", "member_ids": [self.alice["id"]]})["json"] + third = self.client.message(self.alice, other, "Другой чат", key=key)["json"] + self.assertEqual(len({first["id"], second["id"], third["id"]}), 3) + + def test_replay_rechecks_session_and_access(self): + key = uuid4().hex + self.client.message(self.alice, self.chat, "Текст", key=key) + self.client.call("POST", self.path + "/messages", 404, actor=self.eve, + body={"text": "Текст"}, headers={"Idempotency-Key": key}) + self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice) + self.client.call("POST", self.path + "/messages", 401, actor=self.alice, + body={"text": "Текст"}, headers={"Idempotency-Key": key}) -- 2.54.0 From 7f848bbc421bfb23b2f6c6a9b84993c6a9966e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BD=D0=B5=D0=BA?= <88394552+Lunatuk@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:46:06 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 27 +- evidence/lab3-checks.json | 933 ++++++++++++++-------------- evidence/lab3-checks.txt | 432 ++++++------- evidence/postgres-pause-before.json | 10 + src/db.py | 16 + student-tests/docker_lifecycle.py | 3 + student-tests/postgres_pause.py | 81 +++ 7 files changed, 823 insertions(+), 679 deletions(-) create mode 100644 evidence/postgres-pause-before.json create mode 100644 student-tests/postgres_pause.py diff --git a/REPORT.md b/REPORT.md index 3eaa8924..53f416cf 100644 --- a/REPORT.md +++ b/REPORT.md @@ -86,6 +86,23 @@ HAProxy проверяет `/health/ready` каждую 1 секунду. Два Остальные таймауты HAProxy: server 8s, client 10s, чтение HTTP-заголовков 5s. `retries 1` относится к установлению backend-соединения; повторов уже отправленных HTTP POST по статусам не включено. Клиент повторяет неопределённый POST сообщения с исходным ключом. +Ожидание сетевого ответа PostgreSQL ограничено на клиенте 2 секундами: +`TimeoutConnection.wait` в `src/db.py` используется всеми соединениями пула, +включая проверку перед выдачей, SQL и завершение транзакций. При OperationalError +соединение сразу закрывается и исключается из повторного использования, чтобы +не зависнуть ещё и на rollback/cancel. Приложение возвращает контрактный +`503 unavailable` с X-Request-Id и X-Instance-Id раньше таймаута HAProxy. +Серверный statement_timeout=3s сохранён, но сам по себе он не помогает при +`docker compose pause postgres`: остановленный процесс БД не исполняет таймер. + +Регрессия `student-tests/postgres_pause.py` входит в `./scripts/check-docker.sh`. +До исправления прогретый запрос истории после паузы получил 504 за 8.008s +([исходное наблюдение](evidence/postgres-pause-before.json)). После исправления — +JSON 503 за 2.008s. Отдельно приостановлен PostgreSQL во время уже начавшегося +`pg_sleep`: клиент завершил SQL за 2.002s и закрыл соединение. После unpause +подтверждены прежняя сессия, сохранность сообщения и новая запись без рестарта API. +Результат сохранён в `postgres_pause` файла `evidence/lab3-checks.json`. + API является PID 1. При SIGTERM Uvicorn прекращает приём новых соединений, даёт текущим запросам до 5 секунд и закрывает пул. Docker даёт 8 секунд до принудительного завершения. Проверка с незавершённым телом POST измеряет отказ новых соединений и выход до 8 секунд; проверяется `Application shutdown complete`, exit code 0 и отсутствие OOM. Переходные ошибки до исключения backend из LB учитываются в нагрузочном сценарии. ## Общая идемпотентность @@ -147,17 +164,17 @@ docker compose start api ## Измерения -Дата прогона — 29.10.2026. Среда: macOS arm64, Docker VM Linux aarch64, 10 CPU, 8217448448 байт RAM. Лимиты контейнеров приведены выше. Профиль — четыре закрытых клиента: POST сообщения и GET истории с существующими сессиями, пауза 50ms между парами запросов. Для неопределённого POST сохраняются прежние ключ и тело. При отказе нагрузка длится 15 секунд после начала воздействия; при замене — весь action и минимум 3 секунды после. Перед воздействием есть 1 секунда прогрева, которая входит в общие счётчики. +Дата повторного прогона после исправления таймаута БД — 06.11.2026. Среда: macOS arm64, Docker VM Linux aarch64, 10 CPU, 8217448448 байт RAM. Лимиты контейнеров приведены выше. Профиль — четыре закрытых клиента: POST сообщения и GET истории с существующими сессиями, пауза 50ms между парами запросов. Для неопределённого POST сохраняются прежние ключ и тело. При отказе нагрузка длится 15 секунд после начала воздействия; при замене — весь action и минимум 3 секунды после. Перед воздействием есть 1 секунда прогрева, которая входит в общие счётчики. Численные результаты последнего успешного прогона приведены ниже. | Воздействие | Длительность, с | Успешные HTTP/с | Ошибки | Макс. пауза POST / GET, с | Потеря подтверждённых / дубли | | --- | ---: | ---: | ---: | --- | --- | -| Остановка 7f6a80b69e32 | 16.009 | 98.44 | 4 | 1.957 / 2.004 | 0 / 0 | -| Остановка f973eaec4d52 | 16.015 | 98.28 | 4 | 2.006 / 2.006 | 0 / 0 | -| Поочерёдная замена обоих API | 23.774 | 89.64 | 11 | 1.059 / 2.034 | 0 / 0 | +| Остановка 8e14050120c6 | 16.014 | 90.67 | 4 | 2.009 / 2.003 | 0 / 0 | +| Остановка 998bd7b19b89 | 16.015 | 93.91 | 4 | 1.968 / 1.972 | 0 / 0 | +| Поочерёдная замена обоих API | 22.757 | 91.05 | 8 | 1.007 / 1.074 | 0 / 0 | -SIGTERM с незавершённым POST: новые соединения перестали приниматься за 0.167 с, выход за 5.393 с, exit code 0, OOM отсутствует. При остановке Valkey и PostgreSQL по отдельности подтверждены API live=200, API ready=503 и LB ready=503; после восстановления прежний токен продолжил работать. +SIGTERM с незавершённым POST: новые соединения перестали приниматься за 0.161 с, выход за 5.454 с, exit code 0, OOM отсутствует. При остановке Valkey и PostgreSQL по отдельности подтверждены API live=200, API ready=503 и LB ready=503; после восстановления прежний токен продолжил работать. RPS считается по всем успешным HTTP-запросам чтения и записи за полную длительность сценария; ошибки считаются отдельно. «Первый успех» показывает продолжение обслуживания после начала action, а максимальный промежуток между успешными запросами дополнительно показывает паузы во всём сценарии. После каждого воздействия проверяются все подтверждённые сообщения, исходное сообщение до отказа, отсутствие повторяющихся ID/текстов и работа прежней сессии. diff --git a/evidence/lab3-checks.json b/evidence/lab3-checks.json index 8c497137..8028f12d 100644 --- a/evidence/lab3-checks.json +++ b/evidence/lab3-checks.json @@ -1,7 +1,7 @@ { - "utc": "2026-10-29T10:13:09.874202+00:00", - "project": "lab3-check-a31f544685", - "base_url": "http://127.0.0.1:58474", + "utc": "2026-11-06T07:34:22.361174+00:00", + "project": "lab3-check-2418557caa", + "base_url": "http://127.0.0.1:64150", "host": "Darwin arm64", "docker": "29.0.1 aarch64 CPUs=10 RAM=8217448448", "profile": { @@ -10,39 +10,15 @@ "operation": "POST message then GET history, same-key retry after uncertain POST" }, "compose_version": "2.40.3-desktop.1", - "source_commit": "74aa2242515a05192eceb4e3838e3eb4f032f397", + "source_commit": "64ea89d04046a0d4eb761c8774d0b33cdb363bb0", "working_tree": true, "containers": [ { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:15.412188555Z", - "running": true, - "exit_code": 0, - "memory_bytes": 268435456, - "nano_cpus": 1000000000, - "published_ports": {}, - "mounts": [ - { - "type": "bind", - "destination": "/run/secrets/cache_url", - "name": null - }, - { - "type": "bind", - "destination": "/run/secrets/database_url", - "name": null - } - ] - }, - { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", - "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:15.325770597Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:27.834668252Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -62,11 +38,35 @@ ] }, { - "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", - "instance": "2abbb539f04c", + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", + "service": "api", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:27.940669294Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "28c0b15ec1e6fa82db624d0742fdeae73aaff4b60bac2ea7ab49a02ff0922f6c", + "instance": "28c0b15ec1e6", "service": "lb", "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", - "started_at": "2026-10-29T10:13:21.014548544Z", + "started_at": "2026-11-06T07:34:33.562351255Z", "running": true, "exit_code": 0, "memory_bytes": 67108864, @@ -75,7 +75,7 @@ "8080/tcp": [ { "HostIp": "127.0.0.1", - "HostPort": "58474" + "HostPort": "64150" } ] }, @@ -88,11 +88,11 @@ ] }, { - "id": "a0a498f6df2fa9b5b03b24cb1ef2f3786139a8f744f71a6803e414867c391f94", - "instance": "a0a498f6df2f", + "id": "b6926643ca9f5b8537a02918d004adcaad6353a61f53bc821311e251f15299a1", + "instance": "b6926643ca9f", "service": "migrate", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:14.212270221Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:26.723215668Z", "running": false, "exit_code": 0, "memory_bytes": 268435456, @@ -107,11 +107,11 @@ ] }, { - "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", - "instance": "653f44c2806d", + "id": "c4dc7a588242d47cc065defca1612a0b818eae805c3643de642f0672ff6e2de4", + "instance": "c4dc7a588242", "service": "postgres", "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", - "started_at": "2026-10-29T10:13:11.598535928Z", + "started_at": "2026-11-06T07:34:24.113683417Z", "running": true, "exit_code": 0, "memory_bytes": 536870912, @@ -121,7 +121,7 @@ { "type": "volume", "destination": "/var/lib/postgresql/data", - "name": "lab3-check-a31f544685_postgres_data" + "name": "lab3-check-2418557caa_postgres_data" }, { "type": "bind", @@ -131,11 +131,11 @@ ] }, { - "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", - "instance": "4aca2308d7a0", + "id": "5e34f9349f45e6b95cb35a0f6cf50bce63c8158369c91715140436f2394aaa09", + "instance": "5e34f9349f45", "service": "valkey", "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", - "started_at": "2026-10-29T10:13:11.59306872Z", + "started_at": "2026-11-06T07:34:24.1050395Z", "running": true, "exit_code": 0, "memory_bytes": 134217728, @@ -145,44 +145,34 @@ { "type": "volume", "destination": "/data", - "name": "lab3-check-a31f544685_valkey_data" + "name": "lab3-check-2418557caa_valkey_data" } ] } ], "api_tests": "public smoke + 28 student tests passed", + "postgres_pause": { + "history_status": 503, + "history_seconds": 2.008, + "error_code": "unavailable", + "api_instance": "8e14050120c6", + "same_session_and_message_after_unpause": true, + "active_query": { + "status": 503, + "code": "unavailable", + "seconds": 2.002, + "connection_closed": true + }, + "read_write_after_recovery": true + }, "persistence": { "before": [ { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:15.412188555Z", - "running": true, - "exit_code": 0, - "memory_bytes": 268435456, - "nano_cpus": 1000000000, - "published_ports": {}, - "mounts": [ - { - "type": "bind", - "destination": "/run/secrets/cache_url", - "name": null - }, - { - "type": "bind", - "destination": "/run/secrets/database_url", - "name": null - } - ] - }, - { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", - "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:15.325770597Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:27.834668252Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -202,11 +192,35 @@ ] }, { - "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", - "instance": "2abbb539f04c", + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", + "service": "api", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:27.940669294Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "28c0b15ec1e6fa82db624d0742fdeae73aaff4b60bac2ea7ab49a02ff0922f6c", + "instance": "28c0b15ec1e6", "service": "lb", "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", - "started_at": "2026-10-29T10:13:21.014548544Z", + "started_at": "2026-11-06T07:34:33.562351255Z", "running": true, "exit_code": 0, "memory_bytes": 67108864, @@ -215,7 +229,7 @@ "8080/tcp": [ { "HostIp": "127.0.0.1", - "HostPort": "58474" + "HostPort": "64150" } ] }, @@ -228,11 +242,11 @@ ] }, { - "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", - "instance": "653f44c2806d", + "id": "c4dc7a588242d47cc065defca1612a0b818eae805c3643de642f0672ff6e2de4", + "instance": "c4dc7a588242", "service": "postgres", "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", - "started_at": "2026-10-29T10:13:11.598535928Z", + "started_at": "2026-11-06T07:34:24.113683417Z", "running": true, "exit_code": 0, "memory_bytes": 536870912, @@ -242,7 +256,7 @@ { "type": "volume", "destination": "/var/lib/postgresql/data", - "name": "lab3-check-a31f544685_postgres_data" + "name": "lab3-check-2418557caa_postgres_data" }, { "type": "bind", @@ -252,11 +266,11 @@ ] }, { - "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", - "instance": "4aca2308d7a0", + "id": "5e34f9349f45e6b95cb35a0f6cf50bce63c8158369c91715140436f2394aaa09", + "instance": "5e34f9349f45", "service": "valkey", "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", - "started_at": "2026-10-29T10:13:11.59306872Z", + "started_at": "2026-11-06T07:34:24.1050395Z", "running": true, "exit_code": 0, "memory_bytes": 134217728, @@ -266,42 +280,18 @@ { "type": "volume", "destination": "/data", - "name": "lab3-check-a31f544685_valkey_data" + "name": "lab3-check-2418557caa_valkey_data" } ] } ], "after": [ { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:35.473955634Z", - "running": true, - "exit_code": 0, - "memory_bytes": 268435456, - "nano_cpus": 1000000000, - "published_ports": {}, - "mounts": [ - { - "type": "bind", - "destination": "/run/secrets/cache_url", - "name": null - }, - { - "type": "bind", - "destination": "/run/secrets/database_url", - "name": null - } - ] - }, - { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", - "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:35.398920009Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:55.237104501Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -321,11 +311,35 @@ ] }, { - "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", - "instance": "2abbb539f04c", + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", + "service": "api", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:55.196763626Z", + "running": true, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + }, + { + "id": "28c0b15ec1e6fa82db624d0742fdeae73aaff4b60bac2ea7ab49a02ff0922f6c", + "instance": "28c0b15ec1e6", "service": "lb", "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", - "started_at": "2026-10-29T10:13:35.312310009Z", + "started_at": "2026-11-06T07:34:55.112661542Z", "running": true, "exit_code": 0, "memory_bytes": 67108864, @@ -334,7 +348,7 @@ "8080/tcp": [ { "HostIp": "127.0.0.1", - "HostPort": "58474" + "HostPort": "64150" } ] }, @@ -347,35 +361,35 @@ ] }, { - "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", - "instance": "653f44c2806d", + "id": "c4dc7a588242d47cc065defca1612a0b818eae805c3643de642f0672ff6e2de4", + "instance": "c4dc7a588242", "service": "postgres", "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", - "started_at": "2026-10-29T10:13:35.342063384Z", + "started_at": "2026-11-06T07:34:55.144460376Z", "running": true, "exit_code": 0, "memory_bytes": 536870912, "nano_cpus": 1000000000, "published_ports": {}, "mounts": [ - { - "type": "volume", - "destination": "/var/lib/postgresql/data", - "name": "lab3-check-a31f544685_postgres_data" - }, { "type": "bind", "destination": "/run/secrets/postgres_password", "name": null + }, + { + "type": "volume", + "destination": "/var/lib/postgresql/data", + "name": "lab3-check-2418557caa_postgres_data" } ] }, { - "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", - "instance": "4aca2308d7a0", + "id": "5e34f9349f45e6b95cb35a0f6cf50bce63c8158369c91715140436f2394aaa09", + "instance": "5e34f9349f45", "service": "valkey", "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", - "started_at": "2026-10-29T10:13:35.37293605Z", + "started_at": "2026-11-06T07:34:55.173245167Z", "running": true, "exit_code": 0, "memory_bytes": 134217728, @@ -385,7 +399,7 @@ { "type": "volume", "destination": "/data", - "name": "lab3-check-a31f544685_valkey_data" + "name": "lab3-check-2418557caa_valkey_data" } ] } @@ -394,10 +408,10 @@ "idempotency_replay_after_restart": true }, "sigterm": { - "instance": "7f6a80b69e32", + "instance": "8e14050120c6", "unfinished_request": true, - "shutdown_seconds": 5.393, - "new_connections_refused_seconds": 0.167, + "shutdown_seconds": 5.454, + "new_connections_refused_seconds": 0.161, "exit_code": 0, "oom_killed": false }, @@ -419,12 +433,12 @@ "passed": true, "containers_after": [ { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:48.981612751Z", - "running": false, + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:35:09.013094632Z", + "running": true, "exit_code": 0, "memory_bytes": 268435456, "nano_cpus": 1000000000, @@ -432,23 +446,23 @@ "mounts": [ { "type": "bind", - "destination": "/run/secrets/cache_url", + "destination": "/run/secrets/database_url", "name": null }, { "type": "bind", - "destination": "/run/secrets/database_url", + "destination": "/run/secrets/cache_url", "name": null } ] }, { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:35.398920009Z", - "running": true, + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:34:55.196763626Z", + "running": false, "exit_code": 0, "memory_bytes": 268435456, "nano_cpus": 1000000000, @@ -470,188 +484,29 @@ }, "failovers": [ { - "label": "stop 7f6a80b69e32", - "duration_seconds": 16.009, - "successful_requests": 1576, - "successful_writes": 788, + "label": "stop 8e14050120c6", + "duration_seconds": 16.014, + "successful_requests": 1452, + "successful_writes": 726, "errors": 4, - "successful_rps": 98.44, + "successful_rps": 90.67, "first_success_after_action_seconds": { - "POST": 0.014, - "GET": 0.007 + "POST": 0.033, + "GET": 0.044 }, "maximum_success_gap_seconds": { - "POST": 1.957, - "GET": 2.004 + "POST": 2.009, + "GET": 2.003 }, - "action_start_seconds": 1.003, - "action_end_seconds": 1.666, - "p50_ms": 7.813, - "p95_ms": 16.393, + "action_start_seconds": 1.001, + "action_end_seconds": 1.722, + "p50_ms": 9.064, + "p95_ms": 20.562, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": [ - "7f6a80b69e32", - "f973eaec4d52" - ], - "per_second": [ - { - "second": 0, - "successes": 121, - "errors": 0 - }, - { - "second": 1, - "successes": 43, - "errors": 0 - }, - { - "second": 2, - "successes": 0, - "errors": 0 - }, - { - "second": 3, - "successes": 73, - "errors": 4 - }, - { - "second": 4, - "successes": 109, - "errors": 0 - }, - { - "second": 5, - "successes": 113, - "errors": 0 - }, - { - "second": 6, - "successes": 116, - "errors": 0 - }, - { - "second": 7, - "successes": 113, - "errors": 0 - }, - { - "second": 8, - "successes": 118, - "errors": 0 - }, - { - "second": 9, - "successes": 112, - "errors": 0 - }, - { - "second": 10, - "successes": 102, - "errors": 0 - }, - { - "second": 11, - "successes": 102, - "errors": 0 - }, - { - "second": 12, - "successes": 110, - "errors": 0 - }, - { - "second": 13, - "successes": 116, - "errors": 0 - }, - { - "second": 14, - "successes": 112, - "errors": 0 - }, - { - "second": 15, - "successes": 116, - "errors": 0 - }, - { - "second": 16, - "successes": 0, - "errors": 0 - } - ], - "error_samples": [ - [ - 3.329044791999877, - "GET", - "GET /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" - ], - [ - 3.363858082999741, - "POST", - "POST /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" - ], - [ - 3.367696166999849, - "POST", - "POST /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" - ], - [ - 3.4346232499997313, - "GET", - "GET /api/v1/chats/76736023-9461-41c6-87f7-2286c6b1b7b3/messages: connection/TLS failure (TimeoutError)" - ] - ], - "action": { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", - "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:14:23.665074878Z", - "running": false, - "exit_code": 0, - "memory_bytes": 268435456, - "nano_cpus": 1000000000, - "published_ports": {}, - "mounts": [ - { - "type": "bind", - "destination": "/run/secrets/cache_url", - "name": null - }, - { - "type": "bind", - "destination": "/run/secrets/database_url", - "name": null - } - ] - } - }, - { - "label": "stop f973eaec4d52", - "duration_seconds": 16.015, - "successful_requests": 1574, - "successful_writes": 786, - "errors": 4, - "successful_rps": 98.28, - "first_success_after_action_seconds": { - "POST": 0.002, - "GET": 0.008 - }, - "maximum_success_gap_seconds": { - "POST": 2.006, - "GET": 2.006 - }, - "action_start_seconds": 1.005, - "action_end_seconds": 1.713, - "p50_ms": 7.98, - "p95_ms": 17.217, - "confirmed_data_preserved": true, - "duplicate_messages": 0, - "instances": [ - "7f6a80b69e32", - "f973eaec4d52" + "8e14050120c6", + "998bd7b19b89" ], "per_second": [ { @@ -661,7 +516,7 @@ }, { "second": 1, - "successes": 43, + "successes": 40, "errors": 0 }, { @@ -671,62 +526,62 @@ }, { "second": 3, - "successes": 71, + "successes": 68, "errors": 4 }, { "second": 4, - "successes": 117, + "successes": 106, "errors": 0 }, { "second": 5, - "successes": 115, + "successes": 96, "errors": 0 }, { "second": 6, - "successes": 109, + "successes": 90, "errors": 0 }, { "second": 7, - "successes": 116, + "successes": 104, "errors": 0 }, { "second": 8, - "successes": 109, + "successes": 96, "errors": 0 }, { "second": 9, - "successes": 118, + "successes": 107, "errors": 0 }, { "second": 10, - "successes": 115, + "successes": 102, "errors": 0 }, { "second": 11, - "successes": 115, + "successes": 111, "errors": 0 }, { "second": 12, - "successes": 109, + "successes": 96, "errors": 0 }, { "second": 13, - "successes": 105, + "successes": 106, "errors": 0 }, { "second": 14, - "successes": 113, + "successes": 112, "errors": 0 }, { @@ -736,38 +591,197 @@ }, { "second": 16, - "successes": 3, + "successes": 2, "errors": 0 } ], "error_samples": [ [ - 3.3455931250000503, + 3.3445420839998405, "POST", - "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + "POST /api/v1/chats/016772af-0bbf-4860-98c2-6a847c3590dd/messages: connection/TLS failure (TimeoutError)" ], [ - 3.349944458000209, + 3.3737433339993004, "POST", - "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + "POST /api/v1/chats/016772af-0bbf-4860-98c2-6a847c3590dd/messages: connection/TLS failure (TimeoutError)" ], [ - 3.3524864160003744, - "POST", - "POST /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" - ], - [ - 3.41803337500005, + 3.38191112500499, "GET", - "GET /api/v1/chats/aba5162e-1b67-4157-9f13-85a2427d7f80/messages: connection/TLS failure (TimeoutError)" + "GET /api/v1/chats/016772af-0bbf-4860-98c2-6a847c3590dd/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.4136469999939436, + "GET", + "GET /api/v1/chats/016772af-0bbf-4860-98c2-6a847c3590dd/messages: connection/TLS failure (TimeoutError)" ] ], "action": { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:13:35.398920009Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:35:09.013094632Z", + "running": false, + "exit_code": 0, + "memory_bytes": 268435456, + "nano_cpus": 1000000000, + "published_ports": {}, + "mounts": [ + { + "type": "bind", + "destination": "/run/secrets/database_url", + "name": null + }, + { + "type": "bind", + "destination": "/run/secrets/cache_url", + "name": null + } + ] + } + }, + { + "label": "stop 998bd7b19b89", + "duration_seconds": 16.015, + "successful_requests": 1504, + "successful_writes": 751, + "errors": 4, + "successful_rps": 93.91, + "first_success_after_action_seconds": { + "POST": 0.015, + "GET": 0.021 + }, + "maximum_success_gap_seconds": { + "POST": 1.968, + "GET": 1.972 + }, + "action_start_seconds": 1.011, + "action_end_seconds": 1.701, + "p50_ms": 8.418, + "p95_ms": 16.635, + "confirmed_data_preserved": true, + "duplicate_messages": 0, + "instances": [ + "8e14050120c6", + "998bd7b19b89" + ], + "per_second": [ + { + "second": 0, + "successes": 118, + "errors": 0 + }, + { + "second": 1, + "successes": 41, + "errors": 0 + }, + { + "second": 2, + "successes": 0, + "errors": 0 + }, + { + "second": 3, + "successes": 59, + "errors": 4 + }, + { + "second": 4, + "successes": 113, + "errors": 0 + }, + { + "second": 5, + "successes": 105, + "errors": 0 + }, + { + "second": 6, + "successes": 108, + "errors": 0 + }, + { + "second": 7, + "successes": 109, + "errors": 0 + }, + { + "second": 8, + "successes": 102, + "errors": 0 + }, + { + "second": 9, + "successes": 105, + "errors": 0 + }, + { + "second": 10, + "successes": 103, + "errors": 0 + }, + { + "second": 11, + "successes": 109, + "errors": 0 + }, + { + "second": 12, + "successes": 108, + "errors": 0 + }, + { + "second": 13, + "successes": 108, + "errors": 0 + }, + { + "second": 14, + "successes": 112, + "errors": 0 + }, + { + "second": 15, + "successes": 104, + "errors": 0 + }, + { + "second": 16, + "successes": 0, + "errors": 0 + } + ], + "error_samples": [ + [ + 3.3396142910060007, + "POST", + "POST /api/v1/chats/d6b57d5f-7fde-4c24-a854-44f01806a1f6/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.3734452909993706, + "POST", + "POST /api/v1/chats/d6b57d5f-7fde-4c24-a854-44f01806a1f6/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.383555000007618, + "POST", + "POST /api/v1/chats/d6b57d5f-7fde-4c24-a854-44f01806a1f6/messages: connection/TLS failure (TimeoutError)" + ], + [ + 3.4469545410102, + "GET", + "GET /api/v1/chats/d6b57d5f-7fde-4c24-a854-44f01806a1f6/messages: connection/TLS failure (TimeoutError)" + ] + ], + "action": { + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", + "service": "api", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:35:43.846525885Z", "running": false, "exit_code": 0, "memory_bytes": 268435456, @@ -790,188 +804,183 @@ ], "rolling_update": { "label": "sequential API replacement", - "duration_seconds": 23.774, - "successful_requests": 2131, - "successful_writes": 1063, - "errors": 11, - "successful_rps": 89.64, + "duration_seconds": 22.757, + "successful_requests": 2072, + "successful_writes": 1037, + "errors": 8, + "successful_rps": 91.05, "first_success_after_action_seconds": { - "POST": 0.018, - "GET": 0.004 + "POST": 0.019, + "GET": 0.025 }, "maximum_success_gap_seconds": { - "POST": 1.059, - "GET": 2.034 + "POST": 1.007, + "GET": 1.074 }, - "action_start_seconds": 1.005, - "action_end_seconds": 19.63, - "p50_ms": 7.723, - "p95_ms": 13.565, + "action_start_seconds": 1.008, + "action_end_seconds": 19.74, + "p50_ms": 8.068, + "p95_ms": 14.446, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": [ - "244bf0b910eb", - "7f6a80b69e32", - "ca9c7a52da66", - "f973eaec4d52" + "1dde8575f8f4", + "27f47ac686fc", + "8e14050120c6", + "998bd7b19b89" ], "per_second": [ { "second": 0, - "successes": 121, + "successes": 112, "errors": 0 }, { "second": 1, - "successes": 116, + "successes": 106, "errors": 0 }, { "second": 2, - "successes": 109, + "successes": 108, "errors": 0 }, { "second": 3, - "successes": 104, + "successes": 107, "errors": 0 }, { "second": 4, - "successes": 114, + "successes": 97, "errors": 0 }, { "second": 5, - "successes": 33, - "errors": 0 - }, - { - "second": 6, - "successes": 60, + "successes": 10, "errors": 4 }, + { + "second": 6, + "successes": 22, + "errors": 0 + }, { "second": 7, - "successes": 105, + "successes": 86, "errors": 0 }, { "second": 8, - "successes": 117, + "successes": 115, "errors": 0 }, { "second": 9, - "successes": 119, + "successes": 116, "errors": 0 }, { "second": 10, - "successes": 116, + "successes": 108, "errors": 0 }, { "second": 11, - "successes": 115, + "successes": 111, "errors": 0 }, { "second": 12, - "successes": 28, + "successes": 19, "errors": 0 }, { "second": 13, - "successes": 5, + "successes": 3, "errors": 4 }, { "second": 14, - "successes": 74, + "successes": 91, "errors": 0 }, { "second": 15, - "successes": 120, + "successes": 111, "errors": 0 }, { "second": 16, - "successes": 119, + "successes": 112, "errors": 0 }, { "second": 17, - "successes": 115, + "successes": 113, "errors": 0 }, { "second": 18, - "successes": 114, + "successes": 108, "errors": 0 }, { "second": 19, - "successes": 120, + "successes": 111, "errors": 0 }, { "second": 20, - "successes": 116, + "successes": 112, "errors": 0 }, { "second": 21, - "successes": 88, + "successes": 109, "errors": 0 }, { "second": 22, - "successes": 0, + "successes": 85, "errors": 0 - }, - { - "second": 23, - "successes": 3, - "errors": 3 } ], "error_samples": [ [ - 6.263843457999883, - "POST", - "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" - ], - [ - 6.297545500000069, - "POST", - "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" - ], - [ - 6.300835125000049, - "POST", - "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" - ], - [ - 6.369535666000047, + 5.859570375003386, "GET", - "GET /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [200], received 503" + "GET /api/v1/chats/7e626360-a780-4ddc-b215-907e3e6b85af/messages: expected [200], received 503" ], [ - 13.222746333000032, + 5.876960459005204, + "GET", + "GET /api/v1/chats/7e626360-a780-4ddc-b215-907e3e6b85af/messages: expected [200], received 503" + ], + [ + 5.907214917009696, + "GET", + "GET /api/v1/chats/7e626360-a780-4ddc-b215-907e3e6b85af/messages: expected [200], received 503" + ], + [ + 5.91258333400765, + "GET", + "GET /api/v1/chats/7e626360-a780-4ddc-b215-907e3e6b85af/messages: expected [200], received 503" + ], + [ + 13.1466267920041, "POST", - "POST /api/v1/chats/6c896f9d-2f46-4c63-85f9-59d67053f53b/messages: expected [201], received 503" + "POST /api/v1/chats/7e626360-a780-4ddc-b215-907e3e6b85af/messages: expected [201], received 503" ] ], "action": { "before": [ { - "id": "7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6", - "instance": "7f6a80b69e32", + "id": "8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560", + "instance": "8e14050120c6", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:14:47.987597625Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:36:08.104300215Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -980,22 +989,22 @@ "mounts": [ { "type": "bind", - "destination": "/run/secrets/cache_url", + "destination": "/run/secrets/database_url", "name": null }, { "type": "bind", - "destination": "/run/secrets/database_url", + "destination": "/run/secrets/cache_url", "name": null } ] }, { - "id": "f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b", - "instance": "f973eaec4d52", + "id": "998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190", + "instance": "998bd7b19b89", "service": "api", - "image": "sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb", - "started_at": "2026-10-29T10:15:11.976704845Z", + "image": "sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f", + "started_at": "2026-11-06T07:36:32.161170421Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -1004,22 +1013,22 @@ "mounts": [ { "type": "bind", - "destination": "/run/secrets/cache_url", + "destination": "/run/secrets/database_url", "name": null }, { "type": "bind", - "destination": "/run/secrets/database_url", + "destination": "/run/secrets/cache_url", "name": null } ] }, { - "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", - "instance": "2abbb539f04c", + "id": "28c0b15ec1e6fa82db624d0742fdeae73aaff4b60bac2ea7ab49a02ff0922f6c", + "instance": "28c0b15ec1e6", "service": "lb", "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", - "started_at": "2026-10-29T10:13:35.312310009Z", + "started_at": "2026-11-06T07:34:55.112661542Z", "running": true, "exit_code": 0, "memory_bytes": 67108864, @@ -1028,7 +1037,7 @@ "8080/tcp": [ { "HostIp": "127.0.0.1", - "HostPort": "58474" + "HostPort": "64150" } ] }, @@ -1041,11 +1050,11 @@ ] }, { - "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", - "instance": "653f44c2806d", + "id": "c4dc7a588242d47cc065defca1612a0b818eae805c3643de642f0672ff6e2de4", + "instance": "c4dc7a588242", "service": "postgres", "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", - "started_at": "2026-10-29T10:14:01.922362174Z", + "started_at": "2026-11-06T07:35:22.154406263Z", "running": true, "exit_code": 0, "memory_bytes": 536870912, @@ -1055,7 +1064,7 @@ { "type": "volume", "destination": "/var/lib/postgresql/data", - "name": "lab3-check-a31f544685_postgres_data" + "name": "lab3-check-2418557caa_postgres_data" }, { "type": "bind", @@ -1065,11 +1074,11 @@ ] }, { - "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", - "instance": "4aca2308d7a0", + "id": "5e34f9349f45e6b95cb35a0f6cf50bce63c8158369c91715140436f2394aaa09", + "instance": "5e34f9349f45", "service": "valkey", "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", - "started_at": "2026-10-29T10:13:56.000628546Z", + "started_at": "2026-11-06T07:35:16.191112511Z", "running": true, "exit_code": 0, "memory_bytes": 134217728, @@ -1079,18 +1088,18 @@ { "type": "volume", "destination": "/data", - "name": "lab3-check-a31f544685_valkey_data" + "name": "lab3-check-2418557caa_valkey_data" } ] } ], "after": [ { - "id": "ca9c7a52da66ca3c5ffad5cc6668a0d6d60876862683967b6d5f8e438eb630e0", - "instance": "ca9c7a52da66", + "id": "1dde8575f8f47901f0885407b1ee7ea14e05413b974ea309a6a6117bbc5d46ee", + "instance": "1dde8575f8f4", "service": "api", - "image": "sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8", - "started_at": "2026-10-29T10:15:24.374360503Z", + "image": "sha256:6b3faf75764ac8f93967166e18995765c3c9899adae08c10b0df375a8a871db1", + "started_at": "2026-11-06T07:36:44.022233593Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -1110,11 +1119,11 @@ ] }, { - "id": "244bf0b910eb2fa37937f8f0756a1162e9b339df076f09f4cff90b5c357037a6", - "instance": "244bf0b910eb", + "id": "27f47ac686fc84ad11a443c49cb04a8e540da3698b7fac770ef83d96bea54747", + "instance": "27f47ac686fc", "service": "api", - "image": "sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8", - "started_at": "2026-10-29T10:15:31.325585882Z", + "image": "sha256:6b3faf75764ac8f93967166e18995765c3c9899adae08c10b0df375a8a871db1", + "started_at": "2026-11-06T07:36:51.294172721Z", "running": true, "exit_code": 0, "memory_bytes": 268435456, @@ -1134,11 +1143,11 @@ ] }, { - "id": "2abbb539f04ceb95a2d783c1326fd65d7083216f5846e73ca3f253237fa3c6e1", - "instance": "2abbb539f04c", + "id": "28c0b15ec1e6fa82db624d0742fdeae73aaff4b60bac2ea7ab49a02ff0922f6c", + "instance": "28c0b15ec1e6", "service": "lb", "image": "sha256:3a819392f5a6af2a9203d1c63ff0feba74f0c940623a558511be06d7d85e361c", - "started_at": "2026-10-29T10:13:35.312310009Z", + "started_at": "2026-11-06T07:34:55.112661542Z", "running": true, "exit_code": 0, "memory_bytes": 67108864, @@ -1147,7 +1156,7 @@ "8080/tcp": [ { "HostIp": "127.0.0.1", - "HostPort": "58474" + "HostPort": "64150" } ] }, @@ -1160,11 +1169,11 @@ ] }, { - "id": "653f44c2806d6c0dce3f9695ea8cde24648091b2516976aace0bece9906b064d", - "instance": "653f44c2806d", + "id": "c4dc7a588242d47cc065defca1612a0b818eae805c3643de642f0672ff6e2de4", + "instance": "c4dc7a588242", "service": "postgres", "image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3", - "started_at": "2026-10-29T10:14:01.922362174Z", + "started_at": "2026-11-06T07:35:22.154406263Z", "running": true, "exit_code": 0, "memory_bytes": 536870912, @@ -1174,7 +1183,7 @@ { "type": "volume", "destination": "/var/lib/postgresql/data", - "name": "lab3-check-a31f544685_postgres_data" + "name": "lab3-check-2418557caa_postgres_data" }, { "type": "bind", @@ -1184,11 +1193,11 @@ ] }, { - "id": "4aca2308d7a06f00722910ca2c010a9f9a55d6c9c8a2a02af1c7502ad8cafe2e", - "instance": "4aca2308d7a0", + "id": "5e34f9349f45e6b95cb35a0f6cf50bce63c8158369c91715140436f2394aaa09", + "instance": "5e34f9349f45", "service": "valkey", "image": "sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571", - "started_at": "2026-10-29T10:13:56.000628546Z", + "started_at": "2026-11-06T07:35:16.191112511Z", "running": true, "exit_code": 0, "memory_bytes": 134217728, @@ -1198,7 +1207,7 @@ { "type": "volume", "destination": "/data", - "name": "lab3-check-a31f544685_valkey_data" + "name": "lab3-check-2418557caa_valkey_data" } ] } diff --git a/evidence/lab3-checks.txt b/evidence/lab3-checks.txt index 27ec14ee..051aaa25 100644 --- a/evidence/lab3-checks.txt +++ b/evidence/lab3-checks.txt @@ -1,4 +1,4 @@ -Command: ./scripts/check-docker.sh; project: lab3-check-a31f544685 +Command: ./scripts/check-docker.sh; project: lab3-check-2418557caa Docker: 29.0.1 aarch64 CPUs=10 RAM=8217448448 Local .env and secrets are ready; values are not printed #1 [internal] load local bake definitions @@ -9,117 +9,118 @@ Local .env and secrets are ready; values are not printed #2 transferring dockerfile: 733B done #2 DONE 0.0s -#3 [api internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#3 [migrate internal] load metadata for docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 #3 DONE 0.0s -#4 [api internal] load .dockerignore +#4 [migrate internal] load .dockerignore #4 transferring context: 167B done #4 DONE 0.0s -#5 [migrate internal] load build context +#5 [api internal] load build context #5 transferring context: 809B done #5 DONE 0.0s -#6 [migrate 1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 -#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 done +#6 [api 1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done #6 DONE 0.0s -#7 [migrate 2/8] WORKDIR /app +#7 [api 6/8] COPY src/ ./src/ #7 CACHED -#8 [migrate 3/8] COPY requirements.txt . +#8 [api 3/8] COPY requirements.txt . #8 CACHED -#9 [migrate 4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#9 [api 7/8] COPY migrations/ ./migrations/ #9 CACHED -#10 [migrate 6/8] COPY src/ ./src/ +#10 [api 4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check #10 CACHED -#11 [migrate 7/8] COPY migrations/ ./migrations/ +#11 [api 2/8] WORKDIR /app #11 CACHED -#12 [migrate 5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#12 [api 5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app #12 CACHED -#13 [migrate 8/8] COPY research/ ./research/ +#13 [api 8/8] COPY research/ ./research/ #13 CACHED #14 [api] exporting to image #14 exporting layers done -#14 exporting manifest sha256:06163e45d3676c7cf4f7a1c7bfe108ed3b98559ebb485c7a09d1e87e6f418d17 done -#14 exporting config sha256:3fed7460e9510eef7b2928f8d600d06d5d65329f74d820beac13809476b11398 done -#14 exporting attestation manifest sha256:352319cde788733f2f237a2a78204630c542e7ca906cff14c902046e8c1d2e5b 0.0s done -#14 exporting manifest list sha256:848e0c76f1678f35dd1b483e7c54552466999551ba7e77c0062bf346671e2902 done +#14 exporting manifest sha256:f5b0ba5ed30baa51240061b64983bd1e0be184515995717d9d42c0f303b28386 0.0s done +#14 exporting config sha256:ba8834f54a12be5f335a7d34fccbeb70b0c1b7f1535c33e1a8bf0510bedb4d72 done +#14 exporting attestation manifest sha256:fc530f9da54cb9b04d0e2eb260225df8d61746a14f47eca74e1035ba073c23a6 0.0s done +#14 exporting manifest list sha256:2af1c545ac849373a709167b3fda775bb89e3be8b0efd868844ed36ca154b1e6 done +#14 naming to docker.io/library/mtusi-messenger:lab3 #14 naming to docker.io/library/mtusi-messenger:lab3 done #14 unpacking to docker.io/library/mtusi-messenger:lab3 done #14 DONE 0.1s #15 [migrate] exporting to image #15 exporting layers done -#15 exporting manifest sha256:a59568d34e2c5e8b4d23308342601fdaf8c07e78ec3065878679a067c082e952 done -#15 exporting config sha256:b67b805ba1b7dd90f0a3e8444455488fe8c7f4118f14d671501dd63e85b59c97 done -#15 exporting attestation manifest sha256:b1853c07ddac46c4b398b475052f334aa2753c2fe971e0e4fb2517b0e3e13656 0.0s done -#15 exporting manifest list sha256:eff1abdd662eb60e2741e6ce6b8f364467d2817dafeef9754518247124a780eb done +#15 exporting manifest sha256:b9475efa88de4b71344df73089a66ddd577d05384dce92c62b755b04f74934cc 0.0s done +#15 exporting config sha256:18b572ee6ee932396009b6354f40403917b55bd3d69920671450baff5304f07f done +#15 exporting attestation manifest sha256:a9debc4af71e6876f412c529b350ec078d267c239bd643e509597224309139ed 0.0s done +#15 exporting manifest list sha256:5d4b6704cc0e11813a73dc8a21d040fd1fa0a7155e40e221bd7ff35297bd729f done #15 naming to docker.io/library/mtusi-messenger:lab3 done #15 unpacking to docker.io/library/mtusi-messenger:lab3 done #15 DONE 0.1s -#16 [api] resolving provenance for metadata file +#16 [migrate] resolving provenance for metadata file #16 DONE 0.0s -#17 [migrate] resolving provenance for metadata file +#17 [api] resolving provenance for metadata file #17 DONE 0.0s mtusi-messenger:lab3 Built mtusi-messenger:lab3 Built - Network lab3-check-a31f544685_default Creating - Network lab3-check-a31f544685_default Created - Volume lab3-check-a31f544685_postgres_data Creating - Volume lab3-check-a31f544685_postgres_data Created - Volume lab3-check-a31f544685_valkey_data Creating - Volume lab3-check-a31f544685_valkey_data Created - Container lab3-check-a31f544685-valkey-1 Creating - Container lab3-check-a31f544685-postgres-1 Creating - Container lab3-check-a31f544685-valkey-1 Created - Container lab3-check-a31f544685-postgres-1 Created - Container lab3-check-a31f544685-migrate-1 Creating - Container lab3-check-a31f544685-migrate-1 Created - Container lab3-check-a31f544685-api-2 Creating - Container lab3-check-a31f544685-api-1 Creating - Container lab3-check-a31f544685-api-1 Created - Container lab3-check-a31f544685-api-2 Created - Container lab3-check-a31f544685-lb-1 Creating - Container lab3-check-a31f544685-lb-1 Created - Container lab3-check-a31f544685-valkey-1 Starting - Container lab3-check-a31f544685-postgres-1 Starting - Container lab3-check-a31f544685-valkey-1 Started - Container lab3-check-a31f544685-postgres-1 Started - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Starting - Container lab3-check-a31f544685-migrate-1 Started - Container lab3-check-a31f544685-migrate-1 Waiting - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-valkey-1 Waiting - Container lab3-check-a31f544685-valkey-1 Healthy - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Exited - Container lab3-check-a31f544685-api-2 Starting - Container lab3-check-a31f544685-api-2 Started - Container lab3-check-a31f544685-api-1 Starting - Container lab3-check-a31f544685-api-1 Started - Container lab3-check-a31f544685-api-2 Waiting - Container lab3-check-a31f544685-api-1 Waiting - Container lab3-check-a31f544685-api-2 Healthy - Container lab3-check-a31f544685-api-1 Healthy - Container lab3-check-a31f544685-lb-1 Starting - Container lab3-check-a31f544685-lb-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 + Network lab3-check-2418557caa_default Creating + Network lab3-check-2418557caa_default Created + Volume lab3-check-2418557caa_postgres_data Creating + Volume lab3-check-2418557caa_postgres_data Created + Volume lab3-check-2418557caa_valkey_data Creating + Volume lab3-check-2418557caa_valkey_data Created + Container lab3-check-2418557caa-valkey-1 Creating + Container lab3-check-2418557caa-postgres-1 Creating + Container lab3-check-2418557caa-valkey-1 Created + Container lab3-check-2418557caa-postgres-1 Created + Container lab3-check-2418557caa-migrate-1 Creating + Container lab3-check-2418557caa-migrate-1 Created + Container lab3-check-2418557caa-api-2 Creating + Container lab3-check-2418557caa-api-1 Creating + Container lab3-check-2418557caa-api-2 Created + Container lab3-check-2418557caa-api-1 Created + Container lab3-check-2418557caa-lb-1 Creating + Container lab3-check-2418557caa-lb-1 Created + Container lab3-check-2418557caa-valkey-1 Starting + Container lab3-check-2418557caa-postgres-1 Starting + Container lab3-check-2418557caa-postgres-1 Started + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Started + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Starting + Container lab3-check-2418557caa-migrate-1 Started + Container lab3-check-2418557caa-valkey-1 Waiting + Container lab3-check-2418557caa-migrate-1 Waiting + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Healthy + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Exited + Container lab3-check-2418557caa-api-1 Starting + Container lab3-check-2418557caa-api-1 Started + Container lab3-check-2418557caa-api-2 Starting + Container lab3-check-2418557caa-api-2 Started + Container lab3-check-2418557caa-api-1 Waiting + Container lab3-check-2418557caa-api-2 Waiting + Container lab3-check-2418557caa-api-1 Healthy + Container lab3-check-2418557caa-api-2 Healthy + Container lab3-check-2418557caa-lb-1 Starting + Container lab3-check-2418557caa-lb-1 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 PASS: 2 real API containers, CPU/RAM limits, API non-root and no public ports; only LB published -Проверяем сервер: http://127.0.0.1:58474 +Проверяем сервер: http://127.0.0.1:64150 /Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python scripts/check_contract.py PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. -/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/smoke.py --base-url "http://127.0.0.1:58474" +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/smoke.py --base-url "http://127.0.0.1:64150" PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. test_chat_access (test_api.ApiTests.test_chat_access) ... ok test_chat_pagination_and_cursor_owner (test_api.ApiTests.test_chat_pagination_and_cursor_owner) ... ok @@ -151,90 +152,97 @@ test_unicode_password_with_colon_and_private_response (test_sessions.SessionTest test_wrong_password_and_basic_only_for_login (test_sessions.SessionTests.test_wrong_password_and_basic_only_for_login) ... ok ---------------------------------------------------------------------- -Ran 28 tests in 6.783s +Ran 28 tests in 6.931s OK PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages. - Container lab3-check-a31f544685-postgres-1 Running - Container lab3-check-a31f544685-postgres-1 Running + Container lab3-check-2418557caa-postgres-1 Running + Container lab3-check-2418557caa-postgres-1 Running PASS: migrations only in one-shot service; repeated execution preserves data -/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:58474" + Container lab3-check-2418557caa-postgres-1 Paused + Container lab3-check-2418557caa-postgres-1 Unpaused +API ready through LB: 8e14050120c6, 998bd7b19b89 + Container lab3-check-2418557caa-postgres-1 Paused + Container lab3-check-2418557caa-postgres-1 Unpaused +API ready through LB: 8e14050120c6, 998bd7b19b89 +PASS: PostgreSQL pause: {"history_status": 503, "history_seconds": 2.008, "error_code": "unavailable", "api_instance": "8e14050120c6", "same_session_and_message_after_unpause": true, "active_query": {"status": 503, "code": "unavailable", "seconds": 2.002, "connection_closed": true}, "read_write_after_recovery": true} +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://127.0.0.1:64150" Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. PASS: both API, LB, PostgreSQL, Valkey actually restarted; session and confirmed data preserved -PASS: SIGTERM with unfinished request: {"instance": "7f6a80b69e32", "unfinished_request": true, "shutdown_seconds": 5.393, "new_connections_refused_seconds": 0.167, "exit_code": 0, "oom_killed": false} - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Starting - Container lab3-check-a31f544685-migrate-1 Started - Container lab3-check-a31f544685-valkey-1 Waiting - Container lab3-check-a31f544685-migrate-1 Waiting - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-valkey-1 Healthy - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Exited - Container lab3-check-a31f544685-api-1 Starting - Container lab3-check-a31f544685-api-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 - Container lab3-check-a31f544685-valkey-1 Stopping - Container lab3-check-a31f544685-valkey-1 Stopped +PASS: SIGTERM with unfinished request: {"instance": "8e14050120c6", "unfinished_request": true, "shutdown_seconds": 5.454, "new_connections_refused_seconds": 0.161, "exit_code": 0, "oom_killed": false} + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Starting + Container lab3-check-2418557caa-migrate-1 Started + Container lab3-check-2418557caa-migrate-1 Waiting + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-valkey-1 Healthy + Container lab3-check-2418557caa-migrate-1 Exited + Container lab3-check-2418557caa-api-1 Starting + Container lab3-check-2418557caa-api-1 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 + Container lab3-check-2418557caa-valkey-1 Stopping + Container lab3-check-2418557caa-valkey-1 Stopped PASS: valkey outage: API live=200, ready=503, LB ready=503 - Container lab3-check-a31f544685-valkey-1 Starting - Container lab3-check-a31f544685-valkey-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 - Container lab3-check-a31f544685-postgres-1 Stopping - Container lab3-check-a31f544685-postgres-1 Stopped + Container lab3-check-2418557caa-valkey-1 Starting + Container lab3-check-2418557caa-valkey-1 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 + Container lab3-check-2418557caa-postgres-1 Stopping + Container lab3-check-2418557caa-postgres-1 Stopped PASS: postgres outage: API live=200, ready=503, LB ready=503 - Container lab3-check-a31f544685-postgres-1 Starting - Container lab3-check-a31f544685-postgres-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 -/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:58474" + Container lab3-check-2418557caa-postgres-1 Starting + Container lab3-check-2418557caa-postgres-1 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 +/Users/vanek/Desktop/VNPlabs/VNPlab3/mtusi2026lab3/.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://127.0.0.1:64150" Executing explicit failover hook; hook output suppressed to avoid leaking secrets. -{"observed_instances_before": 2, "successful_writes": 113, "failed_attempts": 1, "first_recovery_seconds": 2.452} +{"observed_instances_before": 2, "successful_writes": 112, "failed_attempts": 1, "first_recovery_seconds": 2.459} PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Starting - Container lab3-check-a31f544685-migrate-1 Started - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-valkey-1 Waiting - Container lab3-check-a31f544685-migrate-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-valkey-1 Healthy - Container lab3-check-a31f544685-migrate-1 Exited - Container lab3-check-a31f544685-api-1 Starting - Container lab3-check-a31f544685-api-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 -Stopped API: 7f6a80b69e32 container=7f6a80b69e3297a4de72e756a6c9d867a108930c1ca0e57922b3821c26fd03e6 -{"label": "stop 7f6a80b69e32", "duration_seconds": 16.009, "successful_requests": 1576, "successful_writes": 788, "errors": 4, "successful_rps": 98.44, "first_success_after_action_seconds": {"POST": 0.014, "GET": 0.007}, "maximum_success_gap_seconds": {"POST": 1.957, "GET": 2.004}, "action_start_seconds": 1.003, "action_end_seconds": 1.666, "p50_ms": 7.813, "p95_ms": 16.393, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["7f6a80b69e32", "f973eaec4d52"]} - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Starting - Container lab3-check-a31f544685-migrate-1 Started - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-valkey-1 Waiting - Container lab3-check-a31f544685-migrate-1 Waiting - Container lab3-check-a31f544685-valkey-1 Healthy - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Exited - Container lab3-check-a31f544685-api-1 Starting - Container lab3-check-a31f544685-api-1 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 -Stopped API: f973eaec4d52 container=f973eaec4d5290eb73cd96949877ccb8df1213523fce0ec05c6b40c1136bb31b -{"label": "stop f973eaec4d52", "duration_seconds": 16.015, "successful_requests": 1574, "successful_writes": 786, "errors": 4, "successful_rps": 98.28, "first_success_after_action_seconds": {"POST": 0.002, "GET": 0.008}, "maximum_success_gap_seconds": {"POST": 2.006, "GET": 2.006}, "action_start_seconds": 1.005, "action_end_seconds": 1.713, "p50_ms": 7.98, "p95_ms": 17.217, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["7f6a80b69e32", "f973eaec4d52"]} - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Starting - Container lab3-check-a31f544685-migrate-1 Started - Container lab3-check-a31f544685-postgres-1 Waiting - Container lab3-check-a31f544685-valkey-1 Waiting - Container lab3-check-a31f544685-migrate-1 Waiting - Container lab3-check-a31f544685-valkey-1 Healthy - Container lab3-check-a31f544685-postgres-1 Healthy - Container lab3-check-a31f544685-migrate-1 Exited - Container lab3-check-a31f544685-api-2 Starting - Container lab3-check-a31f544685-api-2 Started -API ready through LB: 7f6a80b69e32, f973eaec4d52 + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Starting + Container lab3-check-2418557caa-migrate-1 Started + Container lab3-check-2418557caa-migrate-1 Waiting + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Waiting + Container lab3-check-2418557caa-valkey-1 Healthy + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Exited + Container lab3-check-2418557caa-api-2 Starting + Container lab3-check-2418557caa-api-2 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 +Stopped API: 8e14050120c6 container=8e14050120c607ce540e41301256a08292ff7b832d7b32682871ad3c1facf560 +{"label": "stop 8e14050120c6", "duration_seconds": 16.014, "successful_requests": 1452, "successful_writes": 726, "errors": 4, "successful_rps": 90.67, "first_success_after_action_seconds": {"POST": 0.033, "GET": 0.044}, "maximum_success_gap_seconds": {"POST": 2.009, "GET": 2.003}, "action_start_seconds": 1.001, "action_end_seconds": 1.722, "p50_ms": 9.064, "p95_ms": 20.562, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["8e14050120c6", "998bd7b19b89"]} + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Starting + Container lab3-check-2418557caa-migrate-1 Started + Container lab3-check-2418557caa-migrate-1 Waiting + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-valkey-1 Healthy + Container lab3-check-2418557caa-migrate-1 Exited + Container lab3-check-2418557caa-api-1 Starting + Container lab3-check-2418557caa-api-1 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 +Stopped API: 998bd7b19b89 container=998bd7b19b8910cbf3e823a15144b6bdbe42a64e15bb9ae602cdbc30a7e3e190 +{"label": "stop 998bd7b19b89", "duration_seconds": 16.015, "successful_requests": 1504, "successful_writes": 751, "errors": 4, "successful_rps": 93.91, "first_success_after_action_seconds": {"POST": 0.015, "GET": 0.021}, "maximum_success_gap_seconds": {"POST": 1.968, "GET": 1.972}, "action_start_seconds": 1.011, "action_end_seconds": 1.701, "p50_ms": 8.418, "p95_ms": 16.635, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["8e14050120c6", "998bd7b19b89"]} + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-migrate-1 Starting + Container lab3-check-2418557caa-migrate-1 Started + Container lab3-check-2418557caa-migrate-1 Waiting + Container lab3-check-2418557caa-postgres-1 Waiting + Container lab3-check-2418557caa-valkey-1 Waiting + Container lab3-check-2418557caa-postgres-1 Healthy + Container lab3-check-2418557caa-valkey-1 Healthy + Container lab3-check-2418557caa-migrate-1 Exited + Container lab3-check-2418557caa-api-2 Starting + Container lab3-check-2418557caa-api-2 Started +API ready through LB: 8e14050120c6, 998bd7b19b89 Local .env and secrets are ready; values are not printed #1 [internal] load local bake definitions #1 reading from stdin 1.03kB done @@ -256,25 +264,25 @@ Local .env and secrets are ready; values are not printed #5 DONE 0.0s #6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 -#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 done #6 DONE 0.0s -#7 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#7 [2/8] WORKDIR /app #7 CACHED -#8 [3/8] COPY requirements.txt . +#8 [7/8] COPY migrations/ ./migrations/ #8 CACHED -#9 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#9 [3/8] COPY requirements.txt . #9 CACHED -#10 [6/8] COPY src/ ./src/ +#10 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app #10 CACHED -#11 [2/8] WORKDIR /app +#11 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check #11 CACHED -#12 [7/8] COPY migrations/ ./migrations/ +#12 [6/8] COPY src/ ./src/ #12 CACHED #13 [8/8] COPY research/ ./research/ @@ -282,10 +290,10 @@ Local .env and secrets are ready; values are not printed #14 exporting to image #14 exporting layers done -#14 exporting manifest sha256:06163e45d3676c7cf4f7a1c7bfe108ed3b98559ebb485c7a09d1e87e6f418d17 done -#14 exporting config sha256:3fed7460e9510eef7b2928f8d600d06d5d65329f74d820beac13809476b11398 done -#14 exporting attestation manifest sha256:3a88ce0975ddcbab53c3ecb185da08ef114e818e9db952983e997cd47add67bf done -#14 exporting manifest list sha256:433a8a3293202a92f092574052b474e8373c0d58649cbcf1c7ebc1af17933d3c done +#14 exporting manifest sha256:f5b0ba5ed30baa51240061b64983bd1e0be184515995717d9d42c0f303b28386 done +#14 exporting config sha256:ba8834f54a12be5f335a7d34fccbeb70b0c1b7f1535c33e1a8bf0510bedb4d72 done +#14 exporting attestation manifest sha256:4d6c5d31589d68559a42b790b99b4a58122d4f0ef087bacafbc8c14138d20153 0.0s done +#14 exporting manifest list sha256:fb863cfc900482faa092f947ff3814cc5dc7a270ffadc707e7fd0d1de642873a done #14 naming to docker.io/library/mtusi-messenger:lab3 done #14 unpacking to docker.io/library/mtusi-messenger:lab3 done #14 DONE 0.1s @@ -294,7 +302,7 @@ Local .env and secrets are ready; values are not printed #15 DONE 0.0s mtusi-messenger:lab3 Built Local .env and secrets are ready; values are not printed - Container lab3-check-a31f544685-postgres-1 Running + Container lab3-check-2418557caa-postgres-1 Running #1 [internal] load local bake definitions #1 reading from stdin 576B done #1 DONE 0.0s @@ -315,25 +323,25 @@ Local .env and secrets are ready; values are not printed #5 DONE 0.0s #6 [1/8] FROM docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 -#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 done +#6 resolve docker.io/library/python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 0.0s done #6 DONE 0.0s -#7 [3/8] COPY requirements.txt . +#7 [6/8] COPY src/ ./src/ #7 CACHED -#8 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check +#8 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app #8 CACHED -#9 [5/8] RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app +#9 [7/8] COPY migrations/ ./migrations/ #9 CACHED -#10 [6/8] COPY src/ ./src/ +#10 [4/8] RUN python -m pip install --no-cache-dir -r requirements.txt && python -m pip check #10 CACHED -#11 [7/8] COPY migrations/ ./migrations/ +#11 [2/8] WORKDIR /app #11 CACHED -#12 [2/8] WORKDIR /app +#12 [3/8] COPY requirements.txt . #12 CACHED #13 [8/8] COPY research/ ./research/ @@ -341,63 +349,63 @@ Local .env and secrets are ready; values are not printed #14 exporting to image #14 exporting layers done -#14 exporting manifest sha256:a59568d34e2c5e8b4d23308342601fdaf8c07e78ec3065878679a067c082e952 done -#14 exporting config sha256:b67b805ba1b7dd90f0a3e8444455488fe8c7f4118f14d671501dd63e85b59c97 done -#14 exporting attestation manifest sha256:25ff7934ca5161da9b6c345af61b6a10b452580e4d349b35939d32feb66664ea done -#14 exporting manifest list sha256:3e102cd9a2c1e81e95aed4a0d4de1ac94352370595fced350257813f7a07bef8 done +#14 exporting manifest sha256:b9475efa88de4b71344df73089a66ddd577d05384dce92c62b755b04f74934cc done +#14 exporting config sha256:18b572ee6ee932396009b6354f40403917b55bd3d69920671450baff5304f07f done +#14 exporting attestation manifest sha256:82c4ebc3bd535d871d1e2415f644fa0c987421cbe6f7bd05afd95854da34e4bc done +#14 exporting manifest list sha256:6b3faf75764ac8f93967166e18995765c3c9899adae08c10b0df375a8a871db1 done #14 naming to docker.io/library/mtusi-messenger:lab3 done #14 unpacking to docker.io/library/mtusi-messenger:lab3 done -#14 DONE 0.0s +#14 DONE 0.1s #15 resolving provenance for metadata file #15 DONE 0.0s Migrations: already applied -API ready through LB: 7f6a80b69e32, f973eaec4d52 -Replacing API: 7f6a80b69e32 - Container lab3-check-a31f544685-api-2 Running - Container lab3-check-a31f544685-api-3 Creating - Container lab3-check-a31f544685-api-3 Created - Container lab3-check-a31f544685-api-3 Starting - Container lab3-check-a31f544685-api-3 Started -API ready through LB: ca9c7a52da66, f973eaec4d52 -Replacing API: f973eaec4d52 - Container lab3-check-a31f544685-api-3 Running - Container lab3-check-a31f544685-api-4 Creating - Container lab3-check-a31f544685-api-4 Created - Container lab3-check-a31f544685-api-4 Starting - Container lab3-check-a31f544685-api-4 Started -API ready through LB: 244bf0b910eb, ca9c7a52da66 +API ready through LB: 8e14050120c6, 998bd7b19b89 +Replacing API: 8e14050120c6 + Container lab3-check-2418557caa-api-2 Running + Container lab3-check-2418557caa-api-3 Creating + Container lab3-check-2418557caa-api-3 Created + Container lab3-check-2418557caa-api-3 Starting + Container lab3-check-2418557caa-api-3 Started +API ready through LB: 1dde8575f8f4, 998bd7b19b89 +Replacing API: 998bd7b19b89 + Container lab3-check-2418557caa-api-3 Running + Container lab3-check-2418557caa-api-4 Creating + Container lab3-check-2418557caa-api-4 Created + Container lab3-check-2418557caa-api-4 Starting + Container lab3-check-2418557caa-api-4 Started +API ready through LB: 1dde8575f8f4, 27f47ac686fc PASS: all API containers replaced sequentially; stores and LB kept running -{"label": "sequential API replacement", "duration_seconds": 23.774, "successful_requests": 2131, "successful_writes": 1063, "errors": 11, "successful_rps": 89.64, "first_success_after_action_seconds": {"POST": 0.018, "GET": 0.004}, "maximum_success_gap_seconds": {"POST": 1.059, "GET": 2.034}, "action_start_seconds": 1.005, "action_end_seconds": 19.63, "p50_ms": 7.723, "p95_ms": 13.565, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["244bf0b910eb", "7f6a80b69e32", "ca9c7a52da66", "f973eaec4d52"]} +{"label": "sequential API replacement", "duration_seconds": 22.757, "successful_requests": 2072, "successful_writes": 1037, "errors": 8, "successful_rps": 91.05, "first_success_after_action_seconds": {"POST": 0.019, "GET": 0.025}, "maximum_success_gap_seconds": {"POST": 1.007, "GET": 1.074}, "action_start_seconds": 1.008, "action_end_seconds": 19.74, "p50_ms": 8.068, "p95_ms": 14.446, "confirmed_data_preserved": true, "duplicate_messages": 0, "instances": ["1dde8575f8f4", "27f47ac686fc", "8e14050120c6", "998bd7b19b89"]} PASS: all lab3 grade-4 checks completed - Container lab3-check-a31f544685-lb-1 Stopping - Container lab3-check-a31f544685-lb-1 Stopped - Container lab3-check-a31f544685-lb-1 Removing - Container lab3-check-a31f544685-lb-1 Removed - Container lab3-check-a31f544685-api-3 Stopping - Container lab3-check-a31f544685-api-4 Stopping - Container lab3-check-a31f544685-api-4 Stopped - Container lab3-check-a31f544685-api-4 Removing - Container lab3-check-a31f544685-api-4 Removed - Container lab3-check-a31f544685-api-3 Stopped - Container lab3-check-a31f544685-api-3 Removing - Container lab3-check-a31f544685-api-3 Removed - Container lab3-check-a31f544685-migrate-1 Stopping - Container lab3-check-a31f544685-valkey-1 Stopping - Container lab3-check-a31f544685-migrate-1 Stopped - Container lab3-check-a31f544685-migrate-1 Removing - Container lab3-check-a31f544685-migrate-1 Removed - Container lab3-check-a31f544685-postgres-1 Stopping - Container lab3-check-a31f544685-valkey-1 Stopped - Container lab3-check-a31f544685-valkey-1 Removing - Container lab3-check-a31f544685-valkey-1 Removed - Container lab3-check-a31f544685-postgres-1 Stopped - Container lab3-check-a31f544685-postgres-1 Removing - Container lab3-check-a31f544685-postgres-1 Removed - Volume lab3-check-a31f544685_valkey_data Removing - Network lab3-check-a31f544685_default Removing - Volume lab3-check-a31f544685_postgres_data Removing - Volume lab3-check-a31f544685_valkey_data Removed - Volume lab3-check-a31f544685_postgres_data Removed - Network lab3-check-a31f544685_default Removed -Removed only temporary test containers and volumes: lab3-check-a31f544685 + Container lab3-check-2418557caa-lb-1 Stopping + Container lab3-check-2418557caa-lb-1 Stopped + Container lab3-check-2418557caa-lb-1 Removing + Container lab3-check-2418557caa-lb-1 Removed + Container lab3-check-2418557caa-api-4 Stopping + Container lab3-check-2418557caa-api-3 Stopping + Container lab3-check-2418557caa-api-3 Stopped + Container lab3-check-2418557caa-api-3 Removing + Container lab3-check-2418557caa-api-3 Removed + Container lab3-check-2418557caa-api-4 Stopped + Container lab3-check-2418557caa-api-4 Removing + Container lab3-check-2418557caa-api-4 Removed + Container lab3-check-2418557caa-migrate-1 Stopping + Container lab3-check-2418557caa-valkey-1 Stopping + Container lab3-check-2418557caa-migrate-1 Stopped + Container lab3-check-2418557caa-migrate-1 Removing + Container lab3-check-2418557caa-migrate-1 Removed + Container lab3-check-2418557caa-postgres-1 Stopping + Container lab3-check-2418557caa-valkey-1 Stopped + Container lab3-check-2418557caa-valkey-1 Removing + Container lab3-check-2418557caa-valkey-1 Removed + Container lab3-check-2418557caa-postgres-1 Stopped + Container lab3-check-2418557caa-postgres-1 Removing + Container lab3-check-2418557caa-postgres-1 Removed + Volume lab3-check-2418557caa_valkey_data Removing + Volume lab3-check-2418557caa_postgres_data Removing + Network lab3-check-2418557caa_default Removing + Volume lab3-check-2418557caa_valkey_data Removed + Volume lab3-check-2418557caa_postgres_data Removed + Network lab3-check-2418557caa_default Removed +Removed only temporary test containers and volumes: lab3-check-2418557caa diff --git a/evidence/postgres-pause-before.json b/evidence/postgres-pause-before.json new file mode 100644 index 00000000..0a9e1689 --- /dev/null +++ b/evidence/postgres-pause-before.json @@ -0,0 +1,10 @@ +{ + "status": 504, + "elapsed_seconds": 8.008, + "json_error": null, + "x_request_id": null, + "x_instance_id": null, + "date": "2026-11-06", + "commit": "64ea89d04046a0d4eb761c8774d0b33cdb363bb0", + "scenario": "Authenticated history GET immediately after pausing PostgreSQL; existing connections warmed" +} diff --git a/src/db.py b/src/db.py index 8580eca0..a4c143bd 100644 --- a/src/db.py +++ b/src/db.py @@ -9,8 +9,24 @@ from src.config import secret from src.errors import ApiError +IO_TIMEOUT_SECONDS = 2 + + +class TimeoutConnection(psycopg.Connection): + def wait(self, gen, interval=0.1, timeout=None): + # statement_timeout не работает, пока процесс PostgreSQL приостановлен. + limit = IO_TIMEOUT_SECONDS if timeout is None else min(timeout, IO_TIMEOUT_SECONDS) + try: + return super().wait(gen, interval=interval, timeout=limit) + except psycopg.OperationalError: + # Закрываем сразу: ожидание ROLLBACK/cancel тоже может зависнуть. + self.close() + raise + + pool = ConnectionPool( conninfo=secret("DATABASE_URL"), open=False, + connection_class=TimeoutConnection, min_size=1, max_size=5, max_waiting=20, timeout=2, check=ConnectionPool.check_connection, kwargs={ diff --git a/student-tests/docker_lifecycle.py b/student-tests/docker_lifecycle.py index c5e49e38..2b50c802 100644 --- a/student-tests/docker_lifecycle.py +++ b/student-tests/docker_lifecycle.py @@ -16,6 +16,7 @@ from uuid import uuid4 from helpers import Client, ROOT from client import Failure from resilience import history +from postgres_pause import check_postgres_pause def docker(*args): @@ -212,6 +213,8 @@ time.sleep(30) assert sql("SELECT count(*) FROM messages") == count print("PASS: migrations only in one-shot service; repeated execution preserves data", flush=True) + result["postgres_pause"] = check_postgres_pause(compose, base, lambda: script("wait-ready.sh")) + replay_client = Client(base) replay_actor, _, _, replay_chat = replay_client.bootstrap() replay_key = uuid4().hex diff --git a/student-tests/postgres_pause.py b/student-tests/postgres_pause.py new file mode 100644 index 00000000..c6ee1fc0 --- /dev/null +++ b/student-tests/postgres_pause.py @@ -0,0 +1,81 @@ +"""Регрессия: замершая БД должна давать API 503 раньше timeout server 8s.""" +import json +import subprocess +import time + +from helpers import Client, ROOT + + +def check_postgres_pause(compose, base_url, wait_ready): + client = Client(base_url, timeout=12) + alice, bob, _, chat = client.bootstrap() + saved = client.message(alice, chat, "До приостановки PostgreSQL")["json"] + path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100" + # Прогреваем соединения обеих реплик: пауза должна затронуть открытые TCP. + for _ in range(8): + client.call("GET", path, actor=bob, schema="MessagePage") + api_id = compose("ps", "-q", "api").split()[0] + compose("pause", "postgres") + try: + started = time.monotonic() + response = client.call("GET", path, 503, actor=bob) + elapsed = time.monotonic() - started + assert elapsed < 6, "API должен завершить ожидание до HAProxy timeout server 8s" + assert response["json"]["error"]["code"] == "unavailable" + # Client проверяет JSON Error, X-Request-Id, X-Instance-Id и совпадение request_id. + report = {"history_status": 503, "history_seconds": round(elapsed, 3), + "error_code": "unavailable", "api_instance": response["headers"]["X-Instance-Id"]} + finally: + compose("unpause", "postgres") + wait_ready() + page = client.call("GET", path, actor=bob, schema="MessagePage")["json"] + assert saved in page["items"], "Подтверждённое сообщение потеряно" + report["same_session_and_message_after_unpause"] = True + + # Отдельно проверяем уже выполняющийся SQL, а не только check_connection пула. + code = """import json,time +from src import db +from src.errors import ApiError +db.pool.open(wait=True,timeout=15) +conn=None +started=None +try: + with db.connection() as conn: + conn.execute("SET application_name='lab3-pause-active-query'") + started=time.monotonic() + conn.execute('SELECT pg_sleep(30)') + raise AssertionError('SQL unexpectedly completed') +except ApiError as error: + print(json.dumps({'status':error.status,'code':error.code,'seconds':round(time.monotonic()-started,3),'connection_closed':conn.closed}),flush=True) +finally: + db.pool.close() +""" + worker = subprocess.Popen(["docker", "exec", api_id, "python", "-u", "-c", code], + cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + deadline = time.monotonic() + 5 + while True: + active = compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-At", "-c", + "SELECT count(*) FROM pg_stat_activity WHERE application_name='lab3-pause-active-query' AND state='active' AND query='SELECT pg_sleep(30)'") + if active == "1": + break + assert time.monotonic() < deadline, "SQL не начал выполняться" + time.sleep(0.05) + compose("pause", "postgres") + stdout, _ = worker.communicate(timeout=6) + assert worker.returncode == 0, "Проверка активного SQL завершилась с ошибкой" + query = json.loads(stdout) + assert query["status"] == 503 and query["code"] == "unavailable" + assert query["connection_closed"] and query["seconds"] < 6 + report["active_query"] = query + finally: + compose("unpause", "postgres") + if worker.poll() is None: + worker.kill() + worker.communicate(timeout=5) + wait_ready() + assert saved in client.call("GET", path, actor=bob, schema="MessagePage")["json"]["items"] + client.message(alice, chat, "Запись после unpause") + report["read_write_after_recovery"] = True + print("PASS: PostgreSQL pause:", json.dumps(report, ensure_ascii=False), flush=True) + return report -- 2.54.0