diff --git a/.env.example b/.env.example index 714c8076..c3822416 100644 --- a/.env.example +++ b/.env.example @@ -7,5 +7,5 @@ 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 +SESSION_STORE=redis CACHE_URL_FILE=/run/secrets/cache_url diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..99aff457 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM python:3.13.15-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY alembic.ini . +COPY migrations ./migrations +COPY src ./src + +RUN useradd --create-home --uid 10001 appuser + +USER appuser + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" || exit 1 + +CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index e42f2b05..63a52475 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,186 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- **Автор, группа:** Травкин С.А., БВТ2304. +- **Целевая оценка; выбранный трек 5, если нужен:** 5, трек 2 — исследование индексов и восстановление PostgreSQL. +- **Commit/PR:** указать после финального push. +- **Использованные источники, библиотеки и помощь ИИ:** README и API-контракт лабораторной, документация FastAPI, SQLAlchemy, Alembic, PostgreSQL, Redis и Docker Compose. Использованы Python, FastAPI, SQLAlchemy, asyncpg, Alembic, PostgreSQL 17.6, Redis 8.2.2, Argon2id. ChatGPT применялся для разбора задания, разработки, тестирования и оформления отчёта. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Необходимы Docker Engine, Docker Compose, Git и Python 3.9+ для публичных тестов. + +Создать локальную конфигурацию: + +```bash +cp .env.example .env +mkdir -p secrets +chmod 700 secrets +``` + +Создать файлы секретов: + +```bash +python3 - <<'PY' +from pathlib import Path +import secrets + +directory = Path("secrets") +password = secrets.token_hex(24) + +values = { + "postgres_password": password, + "database_url": f"postgresql+asyncpg://lab2:{password}@postgres:5432/messenger", + "cache_url": "redis://redis:6379/0", +} + +for name, value in values.items(): + path = directory / name + path.write_text(value, encoding="utf-8") + path.chmod(0o600) +PY +``` + +Запустить приложение: + +```bash +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +docker compose ps -a +curl -i http://localhost:8080/health/ready +``` + +Миграции выполняются автоматически сервисом `migrate` после готовности PostgreSQL. Ожидаемый ответ `/health/ready` — HTTP 200, `{"status":"ok"}`. + +Основные проверки: + +```bash +make check +make test +python3 -m venv .venv +.venv/bin/python -m pip install -r requirements-dev.txt +make validate PYTHON=.venv/bin/python +make test-persistence ACTION_SCRIPT=scripts/restart.sh +``` + +Дополнительные тесты: + +```bash +python tests_student/test_auth.py +python tests_student/test_race.py +./scripts/test_short_ttl.sh +./scripts/test_failures.sh +``` + +Проверка отката транзакции: + +```bash +docker compose run --rm --no-deps \ + -e PYTHONPATH=/app \ + -v "$PWD/tests_student:/app/tests_student:ro" \ + api python tests_student/test_rollback.py +``` + +Остановка без удаления данных — `docker compose down`. Команда `docker compose down -v` дополнительно удаляет volumes с учебными данными. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Архитектура: **клиент → FastAPI → PostgreSQL / Redis**. + +PostgreSQL хранит пользователей, чаты, участников и сообщения. Redis используется для хранения сессий с TTL. Данные размещаются в Docker named volumes. + +В PostgreSQL созданы таблицы `users`, `chats`, `chat_members`, `messages`. Используются UUID, первичные и внешние ключи, уникальность `username`. Для истории сообщений создан составной индекс `(chat_id, created_at, id)` и реализована keyset-пагинация. + +Создание чата и добавление участников выполняются транзакционно. При ошибке изменения откатываются. + +Пароли хешируются с Argon2id. Вход выполняется через Basic, дальнейшая авторизация — по случайному Bearer-токену. Токены хранятся в Redis, имеют TTL и удаляются при logout. Авторизация через `X-User-Id` не используется. + +Для Redis включён AOF с `appendfsync everysec`. Для PostgreSQL настроен пул соединений `pool_size=5`, `max_overflow=5`, `pool_timeout=5` секунд и таймаут подключения 5 секунд. Для Redis заданы таймауты подключения и операций. + +Конфигурация передаётся через `.env` и Compose. Строки подключения и пароль PostgreSQL читаются из файлов Docker secrets. Миграции Alembic запускаются отдельным сервисом `migrate`. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий | Результат | Доказательство | +| --- | --- | --- | +| Публичный API и проверки | `make check`, `make validate`, `make test` — PASS | `evidence/grade4_checks.txt` | +| Постоянное хранение | Данные и действующий токен сохранились после перезапуска | `evidence/grade4_resilience.txt` | +| PostgreSQL и миграции | Схема создаётся на пустой базе | `migrations/versions/f7c51e1e945b_initial_schema.py` | +| Транзакции | Ошибка составной операции приводит к rollback | `evidence/grade4_checks.txt` | +| Авторизация | Basic, Bearer, logout, неверный пароль проверены | `evidence/grade4_checks.txt` | +| Конкурентная регистрация | Один запрос — 201, второй — 409 | `evidence/grade4_checks.txt` | +| TTL сессии | После истечения TTL получен 401 | `evidence/grade4_resilience.txt` | +| Отказы зависимостей | При отказе PostgreSQL или Redis — 503, после восстановления — 200 | `evidence/failure_timings.txt` | +| Индекс и пагинация | Keyset читает 100 строк индекса против 15 100 у OFFSET | `evidence/explain_history.txt` | +| Трек 2: набор данных | 120 000 сообщений в четырёх чатах | `evidence/grade5_dataset.txt` | +| Трек 2: сравнение индексов | Сравнены обычный и покрывающий индексы | `evidence/grade5_indexes.txt` | +| Трек 2: стоимость записи | По 5 измерений вставки 10 000 сообщений | `evidence/grade5_write_repeat.txt` | +| Трек 2: восстановление | Backup восстановлен в отдельный PostgreSQL volume за 1,03 с | `evidence/grade5_restore_time.txt` | +| Трек 2: сверка данных | Количество, выбранные сообщения и контрольная сумма совпали | `evidence/grade5_verify_source.txt`, `evidence/grade5_verify_restored.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +**1. Пагинация истории сообщений.** -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +На 20 000 сообщениях сравнивались OFFSET и keyset-пагинация с `EXPLAIN (ANALYZE, BUFFERS)`. + +| Показатель | OFFSET | Keyset | +| --- | ---: | ---: | +| Execution Time | 1,908 мс | 0,053 мс | +| Shared buffer hits | 435 | 7 | +| Просмотрено строк индекса | 15 100 | 100 | + +В данном измерении keyset оказался примерно в 36 раз быстрее. + +**2. Проверки отказов.** + +При остановке Redis и PostgreSQL `/health/ready` возвращал 503, `/health/live` — 200. Защищённые запросы возвращали 503 без предоставления доступа. После восстановления зависимостей запросы снова выполнялись успешно. + +Время отказного запроса `/users/me`: Redis — 4,006 и 8,000 с; PostgreSQL — 4,006 и 4,008 с. Точное время восстановления зависимостей не измерялось. Проверки сохранения данных и сессий после штатного перезапуска пройдены. + +**3. Трек 2 — сравнение индексов.** + +В отдельной базе `lab2_benchmark` подготовлено 120 000 синтетических сообщений с неравномерным распределением по чатам: 90 000, 20 000, 8 000 и 2 000. + +Сравнивались два индекса: + +- A — `(chat_id, created_at, id)`; +- B — `(chat_id, created_at, id) INCLUDE (sender_id, text)`. + +| Показатель | Индекс A | Индекс B | +| --- | ---: | ---: | +| Размер | 6928 КБ | 13 МБ | +| План чтения | Index Scan | Index Only Scan | +| Большой чат, 100 сообщений | 0,059 мс | 0,074 мс | +| Малый чат, 100 сообщений | 0,068 мс | 0,085 мс | +| Медиана вставки 10 000 сообщений | 24,310 мс | 23,397 мс | +| WAL при вставке 10 000 сообщений | 2 895 528 байт | 3 857 376 байт | + +Чтение проверялось после `VACUUM (ANALYZE)`. Покрывающий индекс обеспечил `Index Only Scan` с `Heap Fetches: 0`, но в выполненных измерениях не показал преимущества по времени. + +Стоимость записи измерялась пять раз для каждого варианта на свежих копиях таблиц с чередованием порядка тестов. Использовался `EXPLAIN (ANALYZE, BUFFERS, WAL)`. В одном прогоне индекса A зафиксирован выброс 93,088 мс, причина которого не установлена. + +Индекс B создавал примерно на 33,2% больше WAL-байтов и занимал больше места. Устойчивого преимущества по времени не обнаружено. Для приложения сохранён исходный индекс A. + +**4. Трек 2 — backup и восстановление.** + +Для базы `lab2_benchmark` создан резервный архив `pg_dump -Fc` размером около 4,0 МБ. Восстановление выполнено через `pg_restore` в отдельный контейнер PostgreSQL 17.6 с новым Docker volume `lab2_grade5_restore_data`. + +- Время команды восстановления — 1,03 с. +- Код завершения `pg_restore` — 0. +- В исходной и восстановленной базах по 120 000 записей в каждой из таблиц `messages`, `bench_index_a`, `bench_index_b`. +- Распределение сообщений по чатам совпало. +- Десять выбранных сообщений совпали. +- Контрольная сумма содержимого всех 120 000 сообщений совпала: `16c1cb06c01f4ec4f161e45b59a5dce7`. +- `diff` результатов сверки завершился с кодом 0. + +Восстановление прошло успешно, расхождений в проверенных данных не обнаружено. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Эксперименты выполнены локально в Docker на синтетических данных. Чтение для каждого варианта измерялось однократно, запись — пять раз. Результаты зависят от состояния кэша и фоновой нагрузки. Полноценное нагрузочное тестирование с RPS, p95/p99 и использованием CPU/RAM не проводилось. + +Измеренные 1,03 с — время команды восстановления, а не гарантированное RTO. Полное восстановление сервиса дополнительно требует обнаружения сбоя, подготовки инфраструктуры и переключения приложения. + +При восстановлении из полного дампа возможна потеря изменений, сделанных после его создания. Нулевой RPO не обеспечивается. Для уменьшения потерь необходимы регулярные резервные копии и архивирование WAL с возможностью PITR. + +Автоматическое переключение на резервную базу и восстановление с заданными RPO/RTO не реализованы. Дальнейшие улучшения — автоматизация backup, настройка PITR и расширение нагрузочных испытаний. \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..017e41ad --- /dev/null +++ b/alembic.ini @@ -0,0 +1,149 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts. +# this is typically a path given in POSIX (e.g. forward slashes) +# format, relative to the token %(here)s which refers to the location of this +# ini file +script_location = %(here)s/migrations + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s +# Or organize into date-based subdirectories (requires recursive_version_locations = true) +# file_template = %%(year)d/%%(month).2d/%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. for multiple paths, the path separator +# is defined by "path_separator" below. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the tzdata library which can be installed by adding +# `alembic[tz]` to the pip requirements. +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to /versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "path_separator" +# below. +# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions + +# path_separator; This indicates what character is used to split lists of file +# paths, including version_locations and prepend_sys_path within configparser +# files such as alembic.ini. +# The default rendered in new alembic.ini files is "os", which uses os.pathsep +# to provide os-dependent path splitting. +# +# Note that in order to support legacy alembic.ini files, this default does NOT +# take place if path_separator is not present in alembic.ini. If this +# option is omitted entirely, fallback logic is as follows: +# +# 1. Parsing of the version_locations option falls back to using the legacy +# "version_path_separator" key, which if absent then falls back to the legacy +# behavior of splitting on spaces and/or commas. +# 2. Parsing of the prepend_sys_path option falls back to the legacy +# behavior of splitting on spaces, commas, or colons. +# +# Valid values for path_separator are: +# +# path_separator = : +# path_separator = ; +# path_separator = space +# path_separator = newline +# +# Use os.pathsep. Default configuration used for new projects. +path_separator = os + + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +# database URL. This is consumed by the user-maintained env.py script only. +# other means of configuring database URLs may be customized within the env.py +# file. +sqlalchemy.url = + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module +# hooks = ruff +# ruff.type = module +# ruff.module = ruff +# ruff.options = check --fix REVISION_SCRIPT_FILENAME + +# Alternatively, use the exec runner to execute a binary found on your PATH +# hooks = ruff +# ruff.type = exec +# ruff.executable = ruff +# ruff.options = check --fix REVISION_SCRIPT_FILENAME + +# Logging configuration. This is also consumed by the user-maintained +# env.py script only. +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARNING +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARNING +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..8bed7491 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,85 @@ +services: + postgres: + image: postgres:17.6 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: lab2 + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U lab2 -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + + redis: + image: redis:8.2.2 + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + + migrate: + build: + context: . + dockerfile: Dockerfile + command: ["alembic", "upgrade", "head"] + environment: + DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url} + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + restart: "no" + + api: + build: + context: . + dockerfile: Dockerfile + environment: + APP_HOST: ${APP_HOST:-0.0.0.0} + APP_PORT: ${APP_PORT:-8080} + APP_ENV: ${APP_ENV:-lab} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url} + SESSION_STORE: ${SESSION_STORE:-redis} + CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url} + secrets: + - database_url + - cache_url + depends_on: + migrate: + condition: service_completed_successfully + redis: + condition: service_healthy + ports: + - "8080:8080" + +secrets: + postgres_password: + file: ./secrets/postgres_password + + database_url: + file: ./secrets/database_url + + cache_url: + file: ./secrets/cache_url + +volumes: + postgres_data: + redis_data: \ No newline at end of file diff --git a/evidence/explain_history.txt b/evidence/explain_history.txt new file mode 100644 index 00000000..3123db5b --- /dev/null +++ b/evidence/explain_history.txt @@ -0,0 +1,42 @@ +=== Lab 2 Grade 4: message history index experiment === +=== Preparing synthetic benchmark user === +INSERT 0 0 +=== Preparing test chat === +INSERT 0 0 +INSERT 0 0 +=== Preparing 20000 messages === +INSERT 0 0 +=== Test data size === + message_count +--------------- + 20000 +(1 row) + +=== Refreshing PostgreSQL statistics === +ANALYZE +=== OFFSET pagination after 15000 messages === + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=2129.70..2143.90 rows=100 width=82) (actual time=1.883..1.896 rows=100 loops=1) + Buffers: shared hit=435 + -> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..2839.47 rows=20000 width=82) (actual time=0.020..1.535 rows=15100 loops=1) + Index Cond: (chat_id = '22222222-2222-4222-8222-222222222222'::uuid) + Buffers: shared hit=435 + Planning: + Buffers: shared hit=21 + Planning Time: 0.094 ms + Execution Time: 1.908 ms +(9 rows) + +=== KEYSET pagination after message 15000 === + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.41..30.01 rows=100 width=82) (actual time=0.008..0.031 rows=100 loops=1) + Buffers: shared hit=7 + -> Index Scan using ix_messages_chat_created_id on messages (cost=0.41..1481.99 rows=5006 width=82) (actual time=0.007..0.022 rows=100 loops=1) + Index Cond: ((chat_id = '22222222-2222-4222-8222-222222222222'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:15+00'::timestamp with time zone, '00003a98-0000-4000-8000-000000003a98'::uuid))) + Buffers: shared hit=7 + Planning Time: 0.048 ms + Execution Time: 0.053 ms +(7 rows) + diff --git a/evidence/failure_timings.txt b/evidence/failure_timings.txt new file mode 100644 index 00000000..387b0b50 --- /dev/null +++ b/evidence/failure_timings.txt @@ -0,0 +1,39 @@ +Waiting for API readiness before testing... +Creating a test user and session... +Test session created. + +Checking baseline... +PASS: /health/live returned 200 in 0.001683s +PASS: /health/ready returned 200 in 0.003160s +PASS: /api/v1/users/me returned 200 in 0.002653s + +=== Redis failure === + Container mtusi2026lab2-redis-1 Stopping + Container mtusi2026lab2-redis-1 Stopped +PASS: /health/live returned 200 in 0.001606s +PASS: /health/ready returned 503 in 4.006407s +PASS: /api/v1/users/me returned 503 in 4.006094s +Restarting Redis... + Container mtusi2026lab2-redis-1 Starting + Container mtusi2026lab2-redis-1 Started +PASS: /health/ready returned 200 in 0.002260s +PASS: /api/v1/users/me returned 200 in 0.002382s + +=== PostgreSQL failure === + Container mtusi2026lab2-postgres-1 Stopping + Container mtusi2026lab2-postgres-1 Stopped +PASS: /health/live returned 200 in 0.001882s +PASS: /health/ready returned 503 in 4.007958s +PASS: /api/v1/users/me returned 503 in 4.006373s +Restarting PostgreSQL... + Container mtusi2026lab2-postgres-1 Starting + Container mtusi2026lab2-postgres-1 Started +PASS: /health/ready returned 200 in 0.002324s +PASS: /api/v1/users/me returned 200 in 0.012524s + +PASS: dependency failure and recovery tests completed. +Restoring PostgreSQL and Redis... + Container mtusi2026lab2-postgres-1 Running + Container mtusi2026lab2-redis-1 Running +Waiting for API readiness... +Services restored. diff --git a/evidence/grade4_checks.txt b/evidence/grade4_checks.txt new file mode 100644 index 00000000..691be8f7 --- /dev/null +++ b/evidence/grade4_checks.txt @@ -0,0 +1,22 @@ +=== make check === +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +=== make validate === +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. + +=== make test === +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. + +=== test_auth === +PASS: wrong password returned 401; valid session worked; logout revoked the token. + +=== test_race === +PASS: concurrent registration produced exactly one 201 and one 409. + +=== test_rollback === + Container mtusi2026lab2-api-run-8eb3c54add45 Creating + Container mtusi2026lab2-api-run-8eb3c54add45 Created +PASS: failed compound transaction rolled back chat and membership inserts. diff --git a/evidence/grade4_resilience.txt b/evidence/grade4_resilience.txt new file mode 100644 index 00000000..5653d85c --- /dev/null +++ b/evidence/grade4_resilience.txt @@ -0,0 +1,66 @@ +=== persistence === +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +=== short TTL === +Starting API with SESSION_TTL_SECONDS=2... + Container mtusi2026lab2-api-1 Recreate + Container mtusi2026lab2-api-1 Recreated + Container mtusi2026lab2-api-1 Starting + Container mtusi2026lab2-api-1 Started +Checking API configuration... +Waiting for API readiness... +Running short TTL test... +PASS: token works immediately after login. +PASS: expired token returned 401. +PASS: short session TTL test completed. +Restoring API with SESSION_TTL_SECONDS=3600... + Container mtusi2026lab2-api-1 Recreate + Container mtusi2026lab2-api-1 Recreated + Container mtusi2026lab2-api-1 Starting + Container mtusi2026lab2-api-1 Started + +=== dependency failures === +Waiting for API readiness before testing... +Creating a test user and session... +Test session created. + +Checking baseline... +PASS: /health/live returned 200 in 0.002339s +PASS: /health/ready returned 200 in 0.003825s +PASS: /api/v1/users/me returned 200 in 0.004731s + +=== Redis failure === + Container mtusi2026lab2-redis-1 Stopping + Container mtusi2026lab2-redis-1 Stopped +PASS: /health/live returned 200 in 0.002183s +PASS: /health/ready returned 503 in 4.007097s +PASS: /api/v1/users/me returned 503 in 8.000377s +Restarting Redis... + Container mtusi2026lab2-redis-1 Starting + Container mtusi2026lab2-redis-1 Started +PASS: /health/ready returned 200 in 0.002200s +PASS: /api/v1/users/me returned 200 in 0.003519s + +=== PostgreSQL failure === + Container mtusi2026lab2-postgres-1 Stopping + Container mtusi2026lab2-postgres-1 Stopped +PASS: /health/live returned 200 in 0.002847s +PASS: /health/ready returned 503 in 4.006891s +PASS: /api/v1/users/me returned 503 in 4.008468s +Restarting PostgreSQL... + Container mtusi2026lab2-postgres-1 Starting + Container mtusi2026lab2-postgres-1 Started +PASS: /health/ready returned 200 in 0.003164s +PASS: /api/v1/users/me returned 200 in 0.006318s + +PASS: dependency failure and recovery tests completed. +Restoring PostgreSQL and Redis... + Container mtusi2026lab2-postgres-1 Running + Container mtusi2026lab2-redis-1 Running +Waiting for API readiness... +Services restored. + +=== final readiness === +{"status":"ok"} diff --git a/evidence/grade5_dataset.txt b/evidence/grade5_dataset.txt new file mode 100644 index 00000000..39fabfbf --- /dev/null +++ b/evidence/grade5_dataset.txt @@ -0,0 +1,23 @@ +=== Grade 5: preparing skewed dataset === +BEGIN +INSERT 0 1 +INSERT 0 4 +INSERT 0 4 +INSERT 0 120000 +COMMIT +ANALYZE +=== Dataset distribution === + title | message_count +-----------------------+--------------- + Benchmark large chat | 90000 + Benchmark medium chat | 20000 + Benchmark small chat | 8000 + Benchmark tiny chat | 2000 +(4 rows) + +=== Total messages === + total_messages +---------------- + 120000 +(1 row) + diff --git a/evidence/grade5_indexes.txt b/evidence/grade5_indexes.txt new file mode 100644 index 00000000..1ce1cb84 --- /dev/null +++ b/evidence/grade5_indexes.txt @@ -0,0 +1,86 @@ +Timing is on. +=== Grade 5: index comparison === +NOTICE: table "bench_index_a" does not exist, skipping +DROP TABLE +Time: 1.402 ms +DROP TABLE +Time: 0.122 ms +NOTICE: table "bench_index_b" does not exist, skipping +SELECT 120000 +Time: 100.016 ms +SELECT 120000 +Time: 146.729 ms +CREATE INDEX +Time: 44.119 ms +CREATE INDEX +Time: 86.764 ms +VACUUM +Time: 139.424 ms +VACUUM +Time: 140.120 ms +=== Table and index sizes === + variant | index_size +-------------------+------------ + A: regular index | 6928 kB + B: covering index | 13 MB +(2 rows) + +Time: 0.846 ms +=== Index A: large chat, deep page === + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.42..19.65 rows=100 width=87) (actual time=0.030..0.046 rows=100 loops=1) + Buffers: shared hit=2 read=4 + -> Index Scan using bench_a_history_idx on bench_index_a (cost=0.42..4303.33 rows=22377 width=87) (actual time=0.030..0.041 rows=100 loops=1) + Index Cond: ((chat_id = '66666666-6666-4666-8666-666666666661'::uuid) AND (created_at > '2026-01-01 00:01:00+00'::timestamp with time zone)) + Buffers: shared hit=2 read=4 + Planning: + Buffers: shared hit=32 + Planning Time: 0.091 ms + Execution Time: 0.059 ms +(9 rows) + +Time: 1.254 ms +=== Index B: large chat, deep page === + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=0.42..8.07 rows=100 width=87) (actual time=0.038..0.064 rows=100 loops=1) + Buffers: shared hit=1 read=5 + -> Index Only Scan using bench_b_history_idx on bench_index_b (cost=0.42..1730.46 rows=22596 width=87) (actual time=0.037..0.058 rows=100 loops=1) + Index Cond: ((chat_id = '66666666-6666-4666-8666-666666666661'::uuid) AND (created_at > '2026-01-01 00:01:00+00'::timestamp with time zone)) + Heap Fetches: 0 + Buffers: shared hit=1 read=5 + Planning: + Buffers: shared hit=18 + Planning Time: 0.079 ms + Execution Time: 0.074 ms +(10 rows) + +Time: 0.315 ms +=== Index A: small chat === + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=0.42..44.76 rows=100 width=87) (actual time=0.037..0.059 rows=100 loops=1) + Buffers: shared hit=3 read=3 + -> Index Scan using bench_a_history_idx on bench_index_a (cost=0.42..3453.96 rows=7788 width=87) (actual time=0.037..0.052 rows=100 loops=1) + Index Cond: (chat_id = '66666666-6666-4666-8666-666666666663'::uuid) + Buffers: shared hit=3 read=3 + Planning Time: 0.043 ms + Execution Time: 0.068 ms +(7 rows) + +Time: 0.263 ms +=== Index B: small chat === + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.42..8.02 rows=100 width=87) (actual time=0.023..0.074 rows=100 loops=1) + Buffers: shared hit=2 read=4 + -> Index Only Scan using bench_b_history_idx on bench_index_b (cost=0.42..614.82 rows=8080 width=87) (actual time=0.022..0.067 rows=100 loops=1) + Index Cond: (chat_id = '66666666-6666-4666-8666-666666666663'::uuid) + Heap Fetches: 0 + Buffers: shared hit=2 read=4 + Planning Time: 0.042 ms + Execution Time: 0.085 ms +(8 rows) + +Time: 0.308 ms diff --git a/evidence/grade5_restore_time.txt b/evidence/grade5_restore_time.txt new file mode 100644 index 00000000..053a7554 --- /dev/null +++ b/evidence/grade5_restore_time.txt @@ -0,0 +1 @@ +RESTORE_ELAPSED_SECONDS=1.03 diff --git a/evidence/grade5_verify_restored.txt b/evidence/grade5_verify_restored.txt new file mode 100644 index 00000000..73044de0 --- /dev/null +++ b/evidence/grade5_verify_restored.txt @@ -0,0 +1,38 @@ +=== Row counts by table === + table_name | rows_count +---------------+------------ + bench_index_a | 120000 + bench_index_b | 120000 + messages | 120000 +(3 rows) + +=== Message distribution by chat === + chat_id | message_count +--------------------------------------+--------------- + 66666666-6666-4666-8666-666666666661 | 90000 + 66666666-6666-4666-8666-666666666662 | 20000 + 66666666-6666-4666-8666-666666666663 | 8000 + 66666666-6666-4666-8666-666666666664 | 2000 +(4 rows) + +=== Deterministic sample of messages === + id | chat_id | sender_id | text | created_at +--------------------------------------+--------------------------------------+--------------------------------------+------------------------------+---------------------------- + 00000001-0000-4000-8000-000000000001 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 1 | 2026-01-01 00:00:00.001+00 + 00000002-0000-4000-8000-000000000002 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 2 | 2026-01-01 00:00:00.002+00 + 00000003-0000-4000-8000-000000000003 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 3 | 2026-01-01 00:00:00.003+00 + 00000004-0000-4000-8000-000000000004 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 4 | 2026-01-01 00:00:00.004+00 + 00000005-0000-4000-8000-000000000005 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 5 | 2026-01-01 00:00:00.005+00 + 00000006-0000-4000-8000-000000000006 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 6 | 2026-01-01 00:00:00.006+00 + 00000007-0000-4000-8000-000000000007 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 7 | 2026-01-01 00:00:00.007+00 + 00000008-0000-4000-8000-000000000008 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 8 | 2026-01-01 00:00:00.008+00 + 00000009-0000-4000-8000-000000000009 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 9 | 2026-01-01 00:00:00.009+00 + 0000000a-0000-4000-8000-00000000000a | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 10 | 2026-01-01 00:00:00.01+00 +(10 rows) + +=== Message content checksum === + message_count | content_checksum +---------------+---------------------------------- + 120000 | 16c1cb06c01f4ec4f161e45b59a5dce7 +(1 row) + diff --git a/evidence/grade5_verify_source.txt b/evidence/grade5_verify_source.txt new file mode 100644 index 00000000..73044de0 --- /dev/null +++ b/evidence/grade5_verify_source.txt @@ -0,0 +1,38 @@ +=== Row counts by table === + table_name | rows_count +---------------+------------ + bench_index_a | 120000 + bench_index_b | 120000 + messages | 120000 +(3 rows) + +=== Message distribution by chat === + chat_id | message_count +--------------------------------------+--------------- + 66666666-6666-4666-8666-666666666661 | 90000 + 66666666-6666-4666-8666-666666666662 | 20000 + 66666666-6666-4666-8666-666666666663 | 8000 + 66666666-6666-4666-8666-666666666664 | 2000 +(4 rows) + +=== Deterministic sample of messages === + id | chat_id | sender_id | text | created_at +--------------------------------------+--------------------------------------+--------------------------------------+------------------------------+---------------------------- + 00000001-0000-4000-8000-000000000001 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 1 | 2026-01-01 00:00:00.001+00 + 00000002-0000-4000-8000-000000000002 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 2 | 2026-01-01 00:00:00.002+00 + 00000003-0000-4000-8000-000000000003 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 3 | 2026-01-01 00:00:00.003+00 + 00000004-0000-4000-8000-000000000004 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 4 | 2026-01-01 00:00:00.004+00 + 00000005-0000-4000-8000-000000000005 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 5 | 2026-01-01 00:00:00.005+00 + 00000006-0000-4000-8000-000000000006 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 6 | 2026-01-01 00:00:00.006+00 + 00000007-0000-4000-8000-000000000007 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 7 | 2026-01-01 00:00:00.007+00 + 00000008-0000-4000-8000-000000000008 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 8 | 2026-01-01 00:00:00.008+00 + 00000009-0000-4000-8000-000000000009 | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 9 | 2026-01-01 00:00:00.009+00 + 0000000a-0000-4000-8000-00000000000a | 66666666-6666-4666-8666-666666666661 | 55555555-5555-4555-8555-555555555555 | Grade 5 synthetic message 10 | 2026-01-01 00:00:00.01+00 +(10 rows) + +=== Message content checksum === + message_count | content_checksum +---------------+---------------------------------- + 120000 | 16c1cb06c01f4ec4f161e45b59a5dce7 +(1 row) + diff --git a/evidence/grade5_write_repeat.txt b/evidence/grade5_write_repeat.txt new file mode 100644 index 00000000..f1caf3fd --- /dev/null +++ b/evidence/grade5_write_repeat.txt @@ -0,0 +1,97 @@ +Timing is on. +=== Grade 5: repeated write benchmark === +=== PostgreSQL 17.6; 10000 inserted rows per run === +CREATE PROCEDURE +Time: 5.749 ms +NOTICE: === RUN 1, VARIANT A === +NOTICE: Insert on "grade5_write_A_1" (cost=0.00..525.00 rows=0 width=0) (actual time=22.080..22.081 rows=0 loops=1) +NOTICE: Buffers: shared hit=40536 read=6 dirtied=278 written=274 +NOTICE: WAL: records=20129 bytes=2895528 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.484..5.930 rows=10000 loops=1) +NOTICE: Planning: +NOTICE: Buffers: shared hit=12 +NOTICE: Planning Time: 0.153 ms +NOTICE: Execution Time: 22.278 ms +CALL +Time: 301.986 ms +NOTICE: === RUN 1, VARIANT B === +NOTICE: Insert on "grade5_write_B_1" (cost=0.00..525.00 rows=0 width=0) (actual time=22.852..22.852 rows=0 loops=1) +NOTICE: Buffers: shared hit=40770 read=6 dirtied=395 written=391 +NOTICE: WAL: records=20246 bytes=3857376 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.351..5.782 rows=10000 loops=1) +NOTICE: Planning Time: 0.054 ms +NOTICE: Execution Time: 22.963 ms +CALL +Time: 324.262 ms +NOTICE: === RUN 2, VARIANT B === +NOTICE: Insert on "grade5_write_B_2" (cost=0.00..525.00 rows=0 width=0) (actual time=21.701..21.701 rows=0 loops=1) +NOTICE: Buffers: shared hit=40770 read=6 dirtied=395 written=391 +NOTICE: WAL: records=20246 bytes=3857376 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.425..5.720 rows=10000 loops=1) +NOTICE: Planning Time: 0.069 ms +NOTICE: Execution Time: 21.789 ms +CALL +Time: 422.448 ms +NOTICE: === RUN 2, VARIANT A === +NOTICE: Insert on "grade5_write_A_2" (cost=0.00..525.00 rows=0 width=0) (actual time=22.510..22.510 rows=0 loops=1) +NOTICE: Buffers: shared hit=40536 read=6 dirtied=278 written=274 +NOTICE: WAL: records=20129 bytes=2895528 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.424..6.208 rows=10000 loops=1) +NOTICE: Planning Time: 0.057 ms +NOTICE: Execution Time: 22.596 ms +CALL +Time: 294.527 ms +NOTICE: === RUN 3, VARIANT A === +NOTICE: Insert on "grade5_write_A_3" (cost=0.00..525.00 rows=0 width=0) (actual time=24.216..24.217 rows=0 loops=1) +NOTICE: Buffers: shared hit=40536 read=6 dirtied=278 written=274 +NOTICE: WAL: records=20129 bytes=2895528 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.752..6.839 rows=10000 loops=1) +NOTICE: Planning Time: 0.064 ms +NOTICE: Execution Time: 24.310 ms +CALL +Time: 279.345 ms +NOTICE: === RUN 3, VARIANT B === +NOTICE: Insert on "grade5_write_B_3" (cost=0.00..525.00 rows=0 width=0) (actual time=24.158..24.159 rows=0 loops=1) +NOTICE: Buffers: shared hit=40770 read=6 dirtied=395 written=391 +NOTICE: WAL: records=20246 bytes=3857376 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.425..6.257 rows=10000 loops=1) +NOTICE: Planning Time: 0.057 ms +NOTICE: Execution Time: 24.248 ms +CALL +Time: 393.958 ms +NOTICE: === RUN 4, VARIANT B === +NOTICE: Insert on "grade5_write_B_4" (cost=0.00..525.00 rows=0 width=0) (actual time=23.306..23.307 rows=0 loops=1) +NOTICE: Buffers: shared hit=40770 read=6 dirtied=395 written=391 +NOTICE: WAL: records=20246 bytes=3857376 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.440..6.001 rows=10000 loops=1) +NOTICE: Planning Time: 0.060 ms +NOTICE: Execution Time: 23.397 ms +CALL +Time: 351.346 ms +NOTICE: === RUN 4, VARIANT A === +NOTICE: Insert on "grade5_write_A_4" (cost=0.00..525.00 rows=0 width=0) (actual time=92.972..92.973 rows=0 loops=1) +NOTICE: Buffers: shared hit=40536 read=6 dirtied=278 written=274 +NOTICE: WAL: records=20129 bytes=2895528 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.489..5.808 rows=10000 loops=1) +NOTICE: Planning Time: 0.071 ms +NOTICE: Execution Time: 93.088 ms +CALL +Time: 354.370 ms +NOTICE: === RUN 5, VARIANT A === +NOTICE: Insert on "grade5_write_A_5" (cost=0.00..525.00 rows=0 width=0) (actual time=24.264..24.264 rows=0 loops=1) +NOTICE: Buffers: shared hit=40536 read=6 dirtied=278 written=274 +NOTICE: WAL: records=20129 bytes=2895528 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.634..6.771 rows=10000 loops=1) +NOTICE: Planning Time: 0.052 ms +NOTICE: Execution Time: 24.388 ms +CALL +Time: 328.367 ms +NOTICE: === RUN 5, VARIANT B === +NOTICE: Insert on "grade5_write_B_5" (cost=0.00..525.00 rows=0 width=0) (actual time=26.081..26.082 rows=0 loops=1) +NOTICE: Buffers: shared hit=40770 read=6 dirtied=395 written=391 +NOTICE: WAL: records=20246 bytes=3857376 +NOTICE: -> Function Scan on generate_series g (cost=0.00..525.00 rows=10000 width=572) (actual time=0.408..6.748 rows=10000 loops=1) +NOTICE: Planning Time: 0.071 ms +NOTICE: Execution Time: 26.206 ms +CALL +Time: 383.466 ms diff --git a/evidence/grade5_writes.txt b/evidence/grade5_writes.txt new file mode 100644 index 00000000..67256b24 --- /dev/null +++ b/evidence/grade5_writes.txt @@ -0,0 +1,24 @@ +Timing is on. +=== Grade 5: write overhead comparison === +=== Index A: insert 10000 messages === +BEGIN +Time: 0.099 ms +INSERT 0 10000 +Time: 27.003 ms +ROLLBACK +Time: 0.142 ms +=== Index B: insert 10000 messages === +BEGIN +Time: 0.357 ms +INSERT 0 10000 +Time: 25.934 ms +ROLLBACK +Time: 0.179 ms +=== Verify unchanged row counts === + variant | count +---------+-------- + A | 120000 + B | 120000 +(2 rows) + +Time: 11.135 ms diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..e0d0858f --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration with an async dbapi. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..7125d778 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,50 @@ +import asyncio + +from alembic import context +from sqlalchemy import pool +from sqlalchemy.ext.asyncio import async_engine_from_config + + +config = context.config + +DATABASE_URL_FILE = "/run/secrets/database_url" + + +def read_database_url() -> str: + with open(DATABASE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +config.set_main_option("sqlalchemy.url", read_database_url()) + +target_metadata = None + + +def do_run_migrations(connection) -> None: + context.configure( + connection=connection, + target_metadata=target_metadata, + ) + + with context.begin_transaction(): + context.run_migrations() + + +async def run_async_migrations() -> None: + connectable = async_engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + async with connectable.connect() as connection: + await connection.run_sync(do_run_migrations) + + await connectable.dispose() + + +def run_migrations_online() -> None: + asyncio.run(run_async_migrations()) + + +run_migrations_online() \ No newline at end of file diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..11016301 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,28 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + """Upgrade schema.""" + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + """Downgrade schema.""" + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/f7c51e1e945b_initial_schema.py b/migrations/versions/f7c51e1e945b_initial_schema.py new file mode 100644 index 00000000..5bc1512b --- /dev/null +++ b/migrations/versions/f7c51e1e945b_initial_schema.py @@ -0,0 +1,110 @@ +"""initial schema + +Revision ID: f7c51e1e945b +Revises: +Create Date: 2026-10-07 18:33:37.771593 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "f7c51e1e945b" +down_revision: Union[str, Sequence[str], None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.create_table( + "users", + sa.Column("id", sa.Uuid(), nullable=False), + sa.Column("username", sa.String(length=32), nullable=False), + sa.Column("display_name", sa.String(length=100), nullable=False), + sa.Column("password_hash", sa.Text(), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("username", name="uq_users_username"), + ) + + op.create_table( + "chats", + sa.Column("id", sa.Uuid(), nullable=False), + sa.Column("title", sa.String(length=100), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.PrimaryKeyConstraint("id"), + ) + + op.create_table( + "chat_members", + sa.Column("chat_id", sa.Uuid(), nullable=False), + sa.Column("user_id", sa.Uuid(), nullable=False), + sa.ForeignKeyConstraint( + ["chat_id"], + ["chats.id"], + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["user_id"], + ["users.id"], + ondelete="CASCADE", + ), + sa.PrimaryKeyConstraint("chat_id", "user_id"), + ) + + op.create_table( + "messages", + sa.Column("id", sa.Uuid(), nullable=False), + sa.Column("chat_id", sa.Uuid(), nullable=False), + sa.Column("sender_id", sa.Uuid(), nullable=False), + sa.Column("text", sa.String(length=4000), nullable=False), + sa.Column( + "created_at", + sa.DateTime(timezone=True), + server_default=sa.text("CURRENT_TIMESTAMP"), + nullable=False, + ), + sa.ForeignKeyConstraint( + ["chat_id"], + ["chats.id"], + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["sender_id"], + ["users.id"], + ondelete="RESTRICT", + ), + sa.PrimaryKeyConstraint("id"), + ) + + op.create_index( + "ix_messages_chat_created_id", + "messages", + ["chat_id", "created_at", "id"], + unique=False, + ) + + +def downgrade() -> None: + op.drop_index( + "ix_messages_chat_created_id", + table_name="messages", + ) + op.drop_table("messages") + op.drop_table("chat_members") + op.drop_table("chats") + op.drop_table("users") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..2f27f572 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +click==8.5.0 +fastapi==0.141.1 +h11==0.16.0 +idna==3.20 +pydantic==2.13.5 +pydantic_core==2.46.5 +starlette==1.7.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +uvicorn==0.53.0 +SQLAlchemy==2.1.4 +asyncpg==0.32.0 +alembic==1.20.0 +greenlet==3.5.6 + +argon2-cffi==25.1.0 +redis==7.0.1 diff --git a/scripts/explain_history.sql b/scripts/explain_history.sql new file mode 100644 index 00000000..7536aa2f --- /dev/null +++ b/scripts/explain_history.sql @@ -0,0 +1,113 @@ +\set ON_ERROR_STOP on + +\echo '=== Lab 2 Grade 4: message history index experiment ===' + +-- Fixed identifiers make the experiment repeatable. +\set chat_id '22222222-2222-4222-8222-222222222222' +\set user_id '33333333-3333-4333-8333-333333333333' + +\echo '=== Preparing synthetic benchmark user ===' + +INSERT INTO users ( + id, + username, + display_name, + password_hash, + created_at +) +VALUES ( + :'user_id'::uuid, + 'explain_benchmark_user', + 'EXPLAIN benchmark user', + 'benchmark-only-not-a-real-password-hash', + CURRENT_TIMESTAMP +) +ON CONFLICT (id) DO NOTHING; + +\echo '=== Preparing test chat ===' + +INSERT INTO chats (id, title) +VALUES ( + :'chat_id'::uuid, + 'EXPLAIN test 20000 messages' +) +ON CONFLICT (id) DO NOTHING; + +INSERT INTO chat_members (chat_id, user_id) +VALUES ( + :'chat_id'::uuid, + :'user_id'::uuid +) +ON CONFLICT DO NOTHING; + + +\echo '=== Preparing 20000 messages ===' + +INSERT INTO messages ( + id, + chat_id, + sender_id, + text, + created_at +) +SELECT + ( + lpad(to_hex(g), 8, '0') || + '-0000-4000-8000-' || + lpad(to_hex(g), 12, '0') + )::uuid, + :'chat_id'::uuid, + :'user_id'::uuid, + 'EXPLAIN test message ' || g, + '2026-01-01 00:00:00+00'::timestamptz + + g * interval '1 millisecond' +FROM generate_series(1, 20000) AS g +ON CONFLICT (id) DO NOTHING; + + +\echo '=== Test data size ===' + +SELECT COUNT(*) AS message_count +FROM messages +WHERE chat_id = :'chat_id'::uuid; + + +\echo '=== Refreshing PostgreSQL statistics ===' + +ANALYZE messages; + + +\echo '=== OFFSET pagination after 15000 messages ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT + id, + chat_id, + sender_id, + text, + created_at +FROM messages +WHERE chat_id = :'chat_id'::uuid +ORDER BY created_at, id +OFFSET 15000 +LIMIT 100; + + +\echo '=== KEYSET pagination after message 15000 ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT + id, + chat_id, + sender_id, + text, + created_at +FROM messages +WHERE chat_id = :'chat_id'::uuid + AND (created_at, id) > + ( + '2026-01-01 00:00:15+00'::timestamptz, + '00003a98-0000-4000-8000-000000003a98'::uuid + ) +ORDER BY created_at, id +LIMIT 100; \ No newline at end of file diff --git a/scripts/grade5_dataset.sql b/scripts/grade5_dataset.sql new file mode 100644 index 00000000..c339ab6c --- /dev/null +++ b/scripts/grade5_dataset.sql @@ -0,0 +1,78 @@ + +\set ON_ERROR_STOP on + +\echo '=== Grade 5: preparing skewed dataset ===' + +BEGIN; + + +INSERT INTO users ( + id, username, display_name, password_hash +) +VALUES ( + '55555555-5555-4555-8555-555555555555', + 'grade5_benchmark_user', + 'Grade 5 benchmark user', + 'synthetic-benchmark-only' +); + + +INSERT INTO chats (id, title) +VALUES + ('66666666-6666-4666-8666-666666666661', 'Benchmark large chat'), + ('66666666-6666-4666-8666-666666666662', 'Benchmark medium chat'), + ('66666666-6666-4666-8666-666666666663', 'Benchmark small chat'), + ('66666666-6666-4666-8666-666666666664', 'Benchmark tiny chat'); + + +INSERT INTO chat_members (chat_id, user_id) +SELECT + id, + '55555555-5555-4555-8555-555555555555'::uuid +FROM chats +WHERE title LIKE 'Benchmark % chat'; + + +WITH chat_sizes(chat_id, message_count, offset_id) AS ( + VALUES + ('66666666-6666-4666-8666-666666666661'::uuid, 90000, 0), + ('66666666-6666-4666-8666-666666666662'::uuid, 20000, 90000), + ('66666666-6666-4666-8666-666666666663'::uuid, 8000, 110000), + ('66666666-6666-4666-8666-666666666664'::uuid, 2000, 118000) +) +INSERT INTO messages ( + id, chat_id, sender_id, text, created_at +) +SELECT + ( + lpad(to_hex(s.offset_id + g.n), 8, '0') || + '-0000-4000-8000-' || + lpad(to_hex(s.offset_id + g.n), 12, '0') + )::uuid, + s.chat_id, + '55555555-5555-4555-8555-555555555555'::uuid, + 'Grade 5 synthetic message ' || g.n, + '2026-01-01 00:00:00+00'::timestamptz + + g.n * interval '1 millisecond' +FROM chat_sizes AS s +CROSS JOIN LATERAL generate_series(1, s.message_count) AS g(n); + +COMMIT; + +ANALYZE messages; + +\echo '=== Dataset distribution ===' + +SELECT + c.title, + COUNT(m.id) AS message_count +FROM chats AS c +LEFT JOIN messages AS m ON m.chat_id = c.id +WHERE c.title LIKE 'Benchmark % chat' +GROUP BY c.id, c.title +ORDER BY message_count DESC; + +\echo '=== Total messages ===' + +SELECT COUNT(*) AS total_messages +FROM messages; diff --git a/scripts/grade5_indexes.sql b/scripts/grade5_indexes.sql new file mode 100644 index 00000000..30097eeb --- /dev/null +++ b/scripts/grade5_indexes.sql @@ -0,0 +1,71 @@ +\set ON_ERROR_STOP on +\timing on + +\echo '=== Grade 5: index comparison ===' + +DROP TABLE IF EXISTS bench_index_a; +DROP TABLE IF EXISTS bench_index_b; + +CREATE TABLE bench_index_a AS +SELECT * FROM messages; + +CREATE TABLE bench_index_b AS +SELECT * FROM messages; + +CREATE INDEX bench_a_history_idx +ON bench_index_a (chat_id, created_at, id); + +CREATE INDEX bench_b_history_idx +ON bench_index_b (chat_id, created_at, id) +INCLUDE (sender_id, text); + +VACUUM (ANALYZE) bench_index_a; +VACUUM (ANALYZE) bench_index_b; + +\echo '=== Table and index sizes ===' + +SELECT + 'A: regular index' AS variant, + pg_size_pretty(pg_relation_size('bench_a_history_idx')) AS index_size +UNION ALL +SELECT + 'B: covering index', + pg_size_pretty(pg_relation_size('bench_b_history_idx')); + +\echo '=== Index A: large chat, deep page ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM bench_index_a +WHERE chat_id = '66666666-6666-4666-8666-666666666661' + AND created_at > '2026-01-01 00:01:00+00' +ORDER BY created_at, id +LIMIT 100; + +\echo '=== Index B: large chat, deep page ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM bench_index_b +WHERE chat_id = '66666666-6666-4666-8666-666666666661' + AND created_at > '2026-01-01 00:01:00+00' +ORDER BY created_at, id +LIMIT 100; + +\echo '=== Index A: small chat ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM bench_index_a +WHERE chat_id = '66666666-6666-4666-8666-666666666663' +ORDER BY created_at, id +LIMIT 100; + +\echo '=== Index B: small chat ===' + +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM bench_index_b +WHERE chat_id = '66666666-6666-4666-8666-666666666663' +ORDER BY created_at, id +LIMIT 100; \ No newline at end of file diff --git a/scripts/grade5_verify_restore.sql b/scripts/grade5_verify_restore.sql new file mode 100644 index 00000000..5bd33f71 --- /dev/null +++ b/scripts/grade5_verify_restore.sql @@ -0,0 +1,55 @@ + +\set ON_ERROR_STOP on + +\echo '=== Row counts by table ===' + +SELECT 'messages' AS table_name, COUNT(*) AS rows_count +FROM messages +UNION ALL +SELECT 'bench_index_a', COUNT(*) FROM bench_index_a +UNION ALL +SELECT 'bench_index_b', COUNT(*) FROM bench_index_b +ORDER BY table_name; + +\echo '=== Message distribution by chat ===' + +SELECT + chat_id, + COUNT(*) AS message_count +FROM messages +GROUP BY chat_id +ORDER BY chat_id; + +\echo '=== Deterministic sample of messages ===' + +SELECT + id, + chat_id, + sender_id, + text, + created_at +FROM messages +ORDER BY id +LIMIT 10; + +\echo '=== Message content checksum ===' + +SELECT + COUNT(*) AS message_count, + md5( + string_agg( + md5( + id::text || '|' || + chat_id::text || '|' || + sender_id::text || '|' || + length(text)::text || ':' || text || '|' || + to_char( + created_at AT TIME ZONE 'UTC', + 'YYYY-MM-DD"T"HH24:MI:SS.US' + ) + ), + '' + ORDER BY id + ) + ) AS content_checksum +FROM messages; diff --git a/scripts/grade5_write_repeat.sql b/scripts/grade5_write_repeat.sql new file mode 100644 index 00000000..664c5fea --- /dev/null +++ b/scripts/grade5_write_repeat.sql @@ -0,0 +1,92 @@ + +\set ON_ERROR_STOP on +\timing on + +\echo '=== Grade 5: repeated write benchmark ===' +\echo '=== PostgreSQL 17.6; 10000 inserted rows per run ===' + + + +CREATE OR REPLACE PROCEDURE pg_temp.run_write_benchmark( + variant text, + run_number integer +) +LANGUAGE plpgsql +AS $$ +DECLARE + table_name text; + index_name text; + plan_line text; +BEGIN + table_name := format('grade5_write_%s_%s', variant, run_number); + index_name := table_name || '_idx'; + + EXECUTE format( + 'CREATE TABLE %I AS SELECT * FROM messages', + table_name + ); + + IF variant = 'A' THEN + EXECUTE format( + 'CREATE INDEX %I ON %I (chat_id, created_at, id)', + index_name, table_name + ); + ELSIF variant = 'B' THEN + EXECUTE format( + 'CREATE INDEX %I ON %I (chat_id, created_at, id) + INCLUDE (sender_id, text)', + index_name, table_name + ); + ELSE + RAISE EXCEPTION 'Unknown variant: %', variant; + END IF; + + EXECUTE format('ANALYZE %I', table_name); + + RAISE NOTICE '=== RUN %, VARIANT % ===', run_number, variant; + + + FOR plan_line IN + EXECUTE format( + $query$ + EXPLAIN (ANALYZE, BUFFERS, WAL) + INSERT INTO %I ( + id, chat_id, sender_id, text, created_at + ) + SELECT + ( + lpad(to_hex(200000 + g), 8, '0') || + '-0000-4000-8000-' || + lpad(to_hex(200000 + g), 12, '0') + )::uuid, + '66666666-6666-4666-8666-666666666661'::uuid, + '55555555-5555-4555-8555-555555555555'::uuid, + 'Grade 5 write benchmark ' || g, + '2026-02-01 00:00:00+00'::timestamptz + + g * interval '1 millisecond' + FROM generate_series(1, 10000) AS g + $query$, + table_name + ) + LOOP + RAISE NOTICE '%', plan_line; + END LOOP; + + EXECUTE format('DROP TABLE %I', table_name); +END; +$$; + +CALL pg_temp.run_write_benchmark('A', 1); +CALL pg_temp.run_write_benchmark('B', 1); + +CALL pg_temp.run_write_benchmark('B', 2); +CALL pg_temp.run_write_benchmark('A', 2); + +CALL pg_temp.run_write_benchmark('A', 3); +CALL pg_temp.run_write_benchmark('B', 3); + +CALL pg_temp.run_write_benchmark('B', 4); +CALL pg_temp.run_write_benchmark('A', 4); + +CALL pg_temp.run_write_benchmark('A', 5); +CALL pg_temp.run_write_benchmark('B', 5); \ No newline at end of file diff --git a/scripts/grade5_writes.sql b/scripts/grade5_writes.sql new file mode 100644 index 00000000..9782cdf8 --- /dev/null +++ b/scripts/grade5_writes.sql @@ -0,0 +1,57 @@ + +\set ON_ERROR_STOP on +\timing on + +\echo '=== Grade 5: write overhead comparison ===' + + + +\echo '=== Index A: insert 10000 messages ===' + +BEGIN; + +INSERT INTO bench_index_a ( + id, chat_id, sender_id, text, created_at +) +SELECT + ( + lpad(to_hex(200000 + g), 8, '0') || + '-0000-4000-8000-' || + lpad(to_hex(200000 + g), 12, '0') + )::uuid, + '66666666-6666-4666-8666-666666666661'::uuid, + '55555555-5555-4555-8555-555555555555'::uuid, + 'Grade 5 write benchmark ' || g, + '2026-02-01 00:00:00+00'::timestamptz + + g * interval '1 millisecond' +FROM generate_series(1, 10000) AS g; + +ROLLBACK; + +\echo '=== Index B: insert 10000 messages ===' + +BEGIN; + +INSERT INTO bench_index_b ( + id, chat_id, sender_id, text, created_at +) +SELECT + ( + lpad(to_hex(200000 + g), 8, '0') || + '-0000-4000-8000-' || + lpad(to_hex(200000 + g), 12, '0') + )::uuid, + '66666666-6666-4666-8666-666666666661'::uuid, + '55555555-5555-4555-8555-555555555555'::uuid, + 'Grade 5 write benchmark ' || g, + '2026-02-01 00:00:00+00'::timestamptz + + g * interval '1 millisecond' +FROM generate_series(1, 10000) AS g; + +ROLLBACK; + +\echo '=== Verify unchanged row counts ===' + +SELECT 'A' AS variant, COUNT(*) FROM bench_index_a +UNION ALL +SELECT 'B', COUNT(*) FROM bench_index_b; diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..798281ab --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +set -euo pipefail + +echo "Stopping application and stores..." +docker compose down + +echo "Starting application and stores..." +docker compose up -d + +echo "Waiting for services to become healthy..." + +for attempt in $(seq 1 60); do + postgres_status=$( + docker compose ps postgres --format json \ + | python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))' + ) + + redis_status=$( + docker compose ps redis --format json \ + | python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))' + ) + + api_status=$( + docker compose ps api --format json \ + | python -c 'import sys, json; data=json.load(sys.stdin); print(data.get("Health", ""))' + ) + + if [[ "$postgres_status" == "healthy" \ + && "$redis_status" == "healthy" \ + && "$api_status" == "healthy" ]]; then + echo "All services are healthy." + exit 0 + fi + + sleep 1 +done + +echo "Services did not become healthy in time." >&2 +docker compose ps +exit 1 \ No newline at end of file diff --git a/scripts/test_failures.sh b/scripts/test_failures.sh new file mode 100755 index 00000000..47fd5b38 --- /dev/null +++ b/scripts/test_failures.sh @@ -0,0 +1,189 @@ +#!/usr/bin/env bash + +set -euo pipefail + +BASE_URL="http://localhost:8080" +TOKEN_FILE=$(mktemp) + +restore_services() { + echo "Restoring PostgreSQL and Redis..." + docker compose up -d postgres redis + + echo "Waiting for API readiness..." + for attempt in $(seq 1 60); do + if curl --silent --fail \ + "$BASE_URL/health/ready" > /dev/null; then + rm -f "$TOKEN_FILE" + echo "Services restored." + return + fi + sleep 1 + done + + rm -f "$TOKEN_FILE" + echo "WARNING: API did not become ready after recovery." >&2 +} + +trap restore_services EXIT + +echo "Waiting for API readiness before testing..." + +api_ready=false + +for attempt in $(seq 1 60); do + if curl --silent --fail --max-time 3 \ + "$BASE_URL/health/ready" > /dev/null; then + api_ready=true + break + fi + + sleep 1 +done + +if [[ "$api_ready" != "true" ]]; then + echo "FAIL: API did not become ready before testing." >&2 + exit 1 +fi + +check_status() { + local expected="$1" + local path="$2" + local authorization="${3:-}" + + local result actual_status elapsed + + if [[ -n "$authorization" ]]; then + result=$(curl --silent --output /dev/null \ + --max-time 12 \ + --write-out "%{http_code} %{time_total}" \ + -H "Authorization: Bearer $authorization" \ + "$BASE_URL$path") + else + result=$(curl --silent --output /dev/null \ + --max-time 12 \ + --write-out "%{http_code} %{time_total}" \ + "$BASE_URL$path") + fi + + read -r actual_status elapsed <<< "$result" + + if [[ "$actual_status" != "$expected" ]]; then + echo "FAIL: $path expected $expected, got $actual_status" >&2 + exit 1 + fi + + echo "PASS: $path returned $actual_status in ${elapsed}s" +} + +echo "Creating a test user and session..." + +python - "$TOKEN_FILE" <<'PY' +import base64 +import json +import sys +import urllib.request +import uuid + +base = "http://localhost:8080" +username = "failure_" + uuid.uuid4().hex[:16] +password = "correct-password-123" + +def post(path, data=None, authorization=None): + headers = {} + body = None + + if data is not None: + headers["Content-Type"] = "application/json" + body = json.dumps(data).encode("utf-8") + + if authorization: + headers["Authorization"] = authorization + + request = urllib.request.Request( + base + path, + data=body, + headers=headers, + method="POST", + ) + + with urllib.request.urlopen(request, timeout=10) as response: + assert response.status == 201 + return json.load(response) + +post("/api/v1/users", { + "username": username, + "display_name": "Failure Test User", + "password": password, +}) + +credentials = base64.b64encode( + f"{username}:{password}".encode() +).decode() + +result = post( + "/api/v1/auth/sessions", + authorization=f"Basic {credentials}", +) + +with open(sys.argv[1], "w", encoding="utf-8") as file: + file.write(result["token"]) + +print("Test session created.") +PY + +TOKEN=$(cat "$TOKEN_FILE") + +echo +echo "Checking baseline..." +check_status 200 "/health/live" +check_status 200 "/health/ready" +check_status 200 "/api/v1/users/me" "$TOKEN" + +echo +echo "=== Redis failure ===" + +docker compose stop redis + +check_status 200 "/health/live" +check_status 503 "/health/ready" +check_status 503 "/api/v1/users/me" "$TOKEN" + +echo "Restarting Redis..." +docker compose start redis + +for attempt in $(seq 1 60); do + if curl --silent --fail \ + "$BASE_URL/health/ready" > /dev/null; then + break + fi + sleep 1 +done + +check_status 200 "/health/ready" +check_status 200 "/api/v1/users/me" "$TOKEN" + +echo +echo "=== PostgreSQL failure ===" + +docker compose stop postgres + +check_status 200 "/health/live" +check_status 503 "/health/ready" +check_status 503 "/api/v1/users/me" "$TOKEN" + +echo "Restarting PostgreSQL..." +docker compose start postgres + +for attempt in $(seq 1 60); do + if curl --silent --fail \ + "$BASE_URL/health/ready" > /dev/null; then + break + fi + sleep 1 +done + +check_status 200 "/health/ready" +check_status 200 "/api/v1/users/me" "$TOKEN" + +echo +echo "PASS: dependency failure and recovery tests completed." \ No newline at end of file diff --git a/scripts/test_short_ttl.sh b/scripts/test_short_ttl.sh new file mode 100755 index 00000000..6723184b --- /dev/null +++ b/scripts/test_short_ttl.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -euo pipefail + +restore_api() { + echo "Restoring API with SESSION_TTL_SECONDS=3600..." + + SESSION_TTL_SECONDS=3600 \ + docker compose up -d --force-recreate --no-deps api +} + +trap restore_api EXIT + +echo "Starting API with SESSION_TTL_SECONDS=2..." + +SESSION_TTL_SECONDS=2 \ + docker compose up -d --force-recreate --no-deps api + +echo "Checking API configuration..." + +actual_ttl=$(docker compose exec -T api printenv SESSION_TTL_SECONDS) + +if [[ "$actual_ttl" != "2" ]]; then + echo "Expected SESSION_TTL_SECONDS=2, got $actual_ttl" >&2 + exit 1 +fi + +echo "Waiting for API readiness..." + +ready=false + +for attempt in $(seq 1 30); do + if curl --silent --fail \ + http://localhost:8080/health/ready > /dev/null; then + ready=true + break + fi + + sleep 1 +done + +if [[ "$ready" != "true" ]]; then + echo "API did not become ready." >&2 + exit 1 +fi + +echo "Running short TTL test..." + +python tests_student/test_short_ttl.py + +echo "PASS: short session TTL test completed." \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cursor.py b/src/cursor.py new file mode 100644 index 00000000..16702905 --- /dev/null +++ b/src/cursor.py @@ -0,0 +1,60 @@ +import base64 +import binascii +import json +from datetime import datetime +from uuid import UUID + + +def encode_cursor( + chat_id: UUID, + created_at: datetime, + message_id: UUID, +) -> str: + data = { + "chat_id": str(chat_id), + "created_at": created_at.isoformat(), + "message_id": str(message_id), + } + + raw = json.dumps( + data, + separators=(",", ":"), + ).encode("utf-8") + + return base64.urlsafe_b64encode(raw).decode("ascii") + + +def decode_cursor(cursor: str) -> tuple[UUID, datetime, UUID]: + try: + raw = base64.urlsafe_b64decode(cursor.encode("ascii")) + + data = json.loads(raw.decode("utf-8")) + + if not isinstance(data, dict): + raise ValueError("Invalid cursor") + + if set(data.keys()) != { + "chat_id", + "created_at", + "message_id", + }: + raise ValueError("Invalid cursor") + + chat_id = UUID(data["chat_id"]) + created_at = datetime.fromisoformat(data["created_at"]) + message_id = UUID(data["message_id"]) + + if created_at.tzinfo is None or created_at.utcoffset() is None: + raise ValueError("Invalid cursor") + + return chat_id, created_at, message_id + + except ( + ValueError, + KeyError, + TypeError, + UnicodeError, + binascii.Error, + json.JSONDecodeError, + ) as exc: + raise ValueError("Invalid cursor") from exc \ No newline at end of file diff --git a/src/database.py b/src/database.py new file mode 100644 index 00000000..95b01ffb --- /dev/null +++ b/src/database.py @@ -0,0 +1,57 @@ +from collections.abc import AsyncIterator + +import socket + +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.ext.asyncio import ( + AsyncSession, + async_sessionmaker, + create_async_engine, +) + + +DATABASE_URL_FILE = "/run/secrets/database_url" + +POOL_SIZE = 5 +MAX_OVERFLOW = 5 +POOL_TIMEOUT_SECONDS = 5 +DB_CONNECT_TIMEOUT_SECONDS = 5 + + +def read_database_url() -> str: + with open(DATABASE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +database_url = read_database_url() + +engine = create_async_engine( + database_url, + pool_size=POOL_SIZE, + max_overflow=MAX_OVERFLOW, + pool_timeout=POOL_TIMEOUT_SECONDS, + connect_args={ + "timeout": DB_CONNECT_TIMEOUT_SECONDS, + }, + pool_pre_ping=True, +) + +SessionFactory = async_sessionmaker( + bind=engine, + expire_on_commit=False, +) + + +async def get_db_session() -> AsyncIterator[AsyncSession]: + async with SessionFactory() as session: + yield session + + +async def ping_database() -> bool: + try: + async with engine.connect() as connection: + await connection.execute(text("SELECT 1")) + return True + except (SQLAlchemyError, OSError): + return False \ No newline at end of file diff --git a/src/errors.py b/src/errors.py new file mode 100644 index 00000000..8b853205 --- /dev/null +++ b/src/errors.py @@ -0,0 +1,12 @@ +class APIError(Exception): + def __init__( + self, + status_code: int, + code: str, + message: str, + headers: dict[str, str] | None = None, + ): + self.status_code = status_code + self.code = code + self.message = message + self.headers = headers \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 00000000..96b8be14 --- /dev/null +++ b/src/main.py @@ -0,0 +1,483 @@ +from datetime import datetime, timedelta, timezone +from uuid import UUID, uuid4 +import socket + +from fastapi import Depends, FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from fastapi.security import ( + HTTPAuthorizationCredentials, + HTTPBasic, + HTTPBasicCredentials, + HTTPBearer, +) +from sqlalchemy.exc import IntegrityError, SQLAlchemyError +from sqlalchemy.ext.asyncio import AsyncSession +from starlette.exceptions import HTTPException as StarletteHTTPException + +from src.database import get_db_session, ping_database +from src.errors import APIError +from src.models import CreateUser, SessionResponse, User, CreateChat, Chat, ChatPage, CreateMessage, Message, MessagePage +from src.repositories import ( + create_chat, + list_chats, + create_user as create_user_in_db, + get_user_by_id, + get_chat_by_id, + get_user_credentials_by_username, + create_message, + get_messages, +) +from src.security import verify_password +from src.sessions import ( + create_session, + delete_session, + get_session_user_id, + ping_session_store, +) + + +app = FastAPI() + +basic_security = HTTPBasic(auto_error=False) +bearer_security = HTTPBearer(auto_error=False) + + +async def get_current_user( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security), + session: AsyncSession = Depends(get_db_session), +) -> User: + if credentials is None or credentials.scheme.lower() != "bearer": + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + user_id = await get_session_user_id(credentials.credentials) + + if user_id is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + user = await get_user_by_id(session, user_id) + + if user is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": "Bearer", + }, + ) + + return user + + +@app.middleware("http") +async def request_middleware(request: Request, call_next): + request_id = str(uuid4()) + request.state.request_id = request_id + + if request.method in {"POST", "PUT", "PATCH"}: + content_type = request.headers.get("content-type") + + if content_type is not None: + media_type = content_type.split(";", 1)[0].strip().lower() + + if media_type != "application/json": + return JSONResponse( + status_code=415, + content={ + "error": { + "code": "unsupported_media_type", + "message": "Unsupported media type", + "request_id": request_id, + } + }, + headers={ + "X-Request-Id": request_id, + }, + ) + + response = await call_next(request) + + response.headers["X-Request-Id"] = request_id + + return response + + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler( + request: Request, + exc: RequestValidationError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=400, + content={ + "error": { + "code": "invalid_request", + "message": "Invalid request", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(APIError) +async def api_error_handler(request: Request, exc: APIError): + request_id = request.state.request_id + + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "code": exc.code, + "message": exc.message, + "request_id": request_id, + } + }, + headers=exc.headers, + ) + + +@app.exception_handler(SQLAlchemyError) +async def database_exception_handler( + request: Request, + exc: SQLAlchemyError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "Database unavailable", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(OSError) +async def database_connection_exception_handler( + request: Request, + exc: OSError, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "Database unavailable", + "request_id": request_id, + } + }, + ) + + +@app.exception_handler(StarletteHTTPException) +async def http_exception_handler( + request: Request, + exc: StarletteHTTPException, +): + request_id = request.state.request_id + + if exc.status_code == 404: + code = "not_found" + message = "Not found" + else: + code = "invalid_request" + message = "Invalid request" + + return JSONResponse( + status_code=exc.status_code, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + }, + headers=exc.headers, + ) + + +@app.exception_handler(Exception) +async def unexpected_exception_handler( + request: Request, + exc: Exception, +): + request_id = request.state.request_id + + return JSONResponse( + status_code=500, + content={ + "error": { + "code": "internal_error", + "message": "Internal server error", + "request_id": request_id, + } + }, + ) + + +@app.get("/health/live") +async def health_live(): + return {"status": "ok"} + + +@app.get("/health/ready") +async def health_ready(): + database_ready = await ping_database() + session_store_ready = await ping_session_store() + + if not database_ready or not session_store_ready: + raise APIError( + status_code=503, + code="unavailable", + message="Service dependencies unavailable", + ) + + return {"status": "ok"} + + +@app.post("/api/v1/users", response_model=User, status_code=201) +async def create_user( + data: CreateUser, + session: AsyncSession = Depends(get_db_session), +): + try: + async with session.begin(): + return await create_user_in_db(session, data) + + except IntegrityError as exc: + raise APIError( + status_code=409, + code="conflict", + message="Username already exists", + ) from exc + + +@app.get("/api/v1/users/me", response_model=User) +async def get_me( + current_user: User = Depends(get_current_user), +): + return current_user + + +@app.post( + "/api/v1/auth/sessions", + response_model=SessionResponse, + status_code=201, +) +async def create_auth_session( + credentials: HTTPBasicCredentials | None = Depends(basic_security), + session: AsyncSession = Depends(get_db_session), +): + if credentials is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + user_credentials = await get_user_credentials_by_username( + session, + credentials.username, + ) + + if user_credentials is None: + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + if not verify_password( + user_credentials.password_hash, + credentials.password, + ): + raise APIError( + status_code=401, + code="unauthorized", + message="Unauthorized", + headers={ + "WWW-Authenticate": 'Basic realm="messenger"', + }, + ) + + user = User( + id=user_credentials.id, + username=user_credentials.username, + display_name=user_credentials.display_name, + created_at=user_credentials.created_at, + ) + + token, ttl = await create_session(user.id) + + expires_at = datetime.now(timezone.utc) + timedelta(seconds=ttl) + + return SessionResponse( + token=token, + token_type="Bearer", + expires_at=expires_at, + user=user, + ) + + +@app.delete( + "/api/v1/auth/sessions/current", + status_code=204, +) +async def delete_current_session( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security), + current_user: User = Depends(get_current_user), +): + await delete_session(credentials.credentials) + + +@app.post( + "/api/v1/chats", + response_model=Chat, + status_code=201, +) +async def create_chat_endpoint( + data: CreateChat, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Chat: + try: + chat = await create_chat( + session=session, + data=data, + current_user_id=current_user.id, + ) + await session.commit() + except Exception: + await session.rollback() + raise + + return chat + + +@app.get( + "/api/v1/chats", + response_model=ChatPage, +) +async def get_chats( + limit: int = 50, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> ChatPage: + if limit < 1 or limit > 100: + raise APIError( + status_code=400, + code="invalid_request", + message="Limit must be between 1 and 100", + ) + + return await list_chats( + session=session, + current_user_id=current_user.id, + limit=limit, + ) + + +@app.get( + "/api/v1/chats/{chat_id}", + response_model=Chat, +) +async def get_chat_endpoint( + chat_id: UUID, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Chat: + chat = await get_chat_by_id( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + ) + + if chat is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + return chat + + +@app.post( + "/api/v1/chats/{chat_id}/messages", + response_model=Message, + status_code=201, +) +async def create_message_endpoint( + chat_id: UUID, + data: CreateMessage, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> Message: + try: + message = await create_message( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + data=data, + ) + await session.commit() + except Exception: + await session.rollback() + raise + + return message + + +@app.get( + "/api/v1/chats/{chat_id}/messages", + response_model=MessagePage, +) +async def get_messages_endpoint( + chat_id: UUID, + limit: int = 50, + cursor: str | None = None, + current_user: User = Depends(get_current_user), + session: AsyncSession = Depends(get_db_session), +) -> MessagePage: + if limit < 1 or limit > 100: + raise APIError( + status_code=400, + code="invalid_request", + message="Limit must be between 1 and 100", + ) + + return await get_messages( + session=session, + chat_id=chat_id, + current_user_id=current_user.id, + limit=limit, + cursor=cursor, + ) \ No newline at end of file diff --git a/src/models.py b/src/models.py new file mode 100644 index 00000000..5f2c2991 --- /dev/null +++ b/src/models.py @@ -0,0 +1,118 @@ +from datetime import datetime +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateUser(BaseModel): + model_config = ConfigDict(extra="forbid") + + username: str = Field( + min_length=3, + max_length=32, + pattern=r"^[a-z0-9_]+$", + ) + + display_name: str = Field( + min_length=1, + max_length=100, + ) + + password: str = Field( + min_length=12, + max_length=128, + ) + + @field_validator("display_name") + @classmethod + def validate_display_name(cls, value: str) -> str: + if not value.strip(): + raise ValueError("Display name must not be blank") + + return value + + +class User(BaseModel): + id: UUID + username: str + display_name: str + created_at: datetime + + +class SessionResponse(BaseModel): + token: str + token_type: str + expires_at: datetime + user: User + + +class CreateChat(BaseModel): + model_config = ConfigDict(extra="forbid") + + title: str = Field( + min_length=1, + max_length=100, + ) + + member_ids: list[UUID] = Field( + min_length=1, + max_length=100, + ) + + @field_validator("title") + @classmethod + def validate_title(cls, value: str) -> str: + if not value.strip(): + raise ValueError("Chat title must not be blank") + + return value + + @field_validator("member_ids") + @classmethod + def validate_member_ids(cls, value: list[UUID]) -> list[UUID]: + if len(value) != len(set(value)): + raise ValueError("Member IDs must be unique") + + return value + + +class Chat(BaseModel): + id: UUID + title: str + member_ids: list[UUID] + created_at: datetime + + +class ChatPage(BaseModel): + items: list[Chat] + next_cursor: str | None + + +class CreateMessage(BaseModel): + model_config = ConfigDict(extra="forbid") + + text: str = Field( + min_length=1, + max_length=4000, + ) + + @field_validator("text") + @classmethod + def validate_text(cls, value: str) -> str: + if not value.strip(): + raise ValueError("Message text must not be blank") + + return value + + +class Message(BaseModel): + id: UUID + chat_id: UUID + sender_id: UUID + text: str + created_at: datetime + + +class MessagePage(BaseModel): + items: list[Message] + next_cursor: str | None \ No newline at end of file diff --git a/src/repositories.py b/src/repositories.py new file mode 100644 index 00000000..8fc10312 --- /dev/null +++ b/src/repositories.py @@ -0,0 +1,535 @@ +from uuid import UUID, uuid4 + +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession + +from src.models import Chat, ChatPage, CreateChat, CreateUser, User, CreateMessage, Message, MessagePage +from src.security import hash_password +from src.errors import APIError +from src.cursor import decode_cursor, encode_cursor + + +async def create_user( + session: AsyncSession, + data: CreateUser, +) -> User: + user_id = uuid4() + password_hash = hash_password(data.password) + + result = await session.execute( + text( + """ + INSERT INTO users ( + id, + username, + display_name, + password_hash + ) + VALUES ( + :id, + :username, + :display_name, + :password_hash + ) + RETURNING + id, + username, + display_name, + created_at + """ + ), + { + "id": user_id, + "username": data.username, + "display_name": data.display_name, + "password_hash": password_hash, + }, + ) + + row = result.one() + + return User( + id=UUID(str(row.id)), + username=row.username, + display_name=row.display_name, + created_at=row.created_at, + ) + +async def get_user_credentials_by_username( + session: AsyncSession, + username: str, +): + result = await session.execute( + text( + """ + SELECT + id, + username, + display_name, + password_hash, + created_at + FROM users + WHERE username = :username + """ + ), + { + "username": username, + }, + ) + + return result.one_or_none() + + +async def get_user_by_id( + session: AsyncSession, + user_id: UUID, +) -> User | None: + result = await session.execute( + text( + """ + SELECT + id, + username, + display_name, + created_at + FROM users + WHERE id = :user_id + """ + ), + { + "user_id": user_id, + }, + ) + + row = result.one_or_none() + + if row is None: + return None + + return User( + id=UUID(str(row.id)), + username=row.username, + display_name=row.display_name, + created_at=row.created_at, + ) + + +async def create_chat( + session: AsyncSession, + data: CreateChat, + current_user_id: UUID, +) -> Chat: + if current_user_id not in data.member_ids: + raise APIError( + status_code=400, + code="invalid_request", + message="Current user must be a chat member", + ) + + users_result = await session.execute( + text( + """ + SELECT id + FROM users + WHERE id = ANY(:member_ids) + """ + ), + { + "member_ids": data.member_ids, + }, + ) + + existing_user_ids = { + UUID(str(row.id)) + for row in users_result + } + + if existing_user_ids != set(data.member_ids): + raise APIError( + status_code=400, + code="invalid_request", + message="Unknown chat member", + ) + + chat_id = uuid4() + + chat_result = await session.execute( + text( + """ + INSERT INTO chats ( + id, + title + ) + VALUES ( + :id, + :title + ) + RETURNING + id, + title, + created_at + """ + ), + { + "id": chat_id, + "title": data.title, + }, + ) + + chat_row = chat_result.one() + + for member_id in data.member_ids: + await session.execute( + text( + """ + INSERT INTO chat_members ( + chat_id, + user_id + ) + VALUES ( + :chat_id, + :user_id + ) + """ + ), + { + "chat_id": chat_id, + "user_id": member_id, + }, + ) + + return Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=data.member_ids, + created_at=chat_row.created_at, + ) + + +async def get_chat_by_id( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, +) -> Chat | None: + chat_result = await session.execute( + text( + """ + SELECT + c.id, + c.title, + c.created_at + FROM chats AS c + JOIN chat_members AS cm + ON cm.chat_id = c.id + WHERE c.id = :chat_id + AND cm.user_id = :current_user_id + """ + ), + { + "chat_id": chat_id, + "current_user_id": current_user_id, + }, + ) + + chat_row = chat_result.one_or_none() + + if chat_row is None: + return None + + members_result = await session.execute( + text( + """ + SELECT user_id + FROM chat_members + WHERE chat_id = :chat_id + ORDER BY user_id + """ + ), + { + "chat_id": chat_id, + }, + ) + + member_ids = [ + UUID(str(row.user_id)) + for row in members_result + ] + + return Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=member_ids, + created_at=chat_row.created_at, + ) + + +async def list_chats( + session: AsyncSession, + current_user_id: UUID, + limit: int, +) -> ChatPage: + chats_result = await session.execute( + text( + """ + SELECT + c.id, + c.title, + c.created_at + FROM chats AS c + JOIN chat_members AS cm + ON cm.chat_id = c.id + WHERE cm.user_id = :current_user_id + ORDER BY c.created_at, c.id + LIMIT :limit + """ + ), + { + "current_user_id": current_user_id, + "limit": limit, + }, + ) + + chat_rows = chats_result.all() + + items = [] + + for chat_row in chat_rows: + members_result = await session.execute( + text( + """ + SELECT user_id + FROM chat_members + WHERE chat_id = :chat_id + ORDER BY user_id + """ + ), + { + "chat_id": chat_row.id, + }, + ) + + member_ids = [ + UUID(str(row.user_id)) + for row in members_result + ] + + items.append( + Chat( + id=UUID(str(chat_row.id)), + title=chat_row.title, + member_ids=member_ids, + created_at=chat_row.created_at, + ) + ) + + return ChatPage( + items=items, + next_cursor=None, + ) + + +async def create_message( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, + data: CreateMessage, +) -> Message: + membership_result = await session.execute( + text( + """ + SELECT 1 + FROM chat_members + WHERE chat_id = :chat_id + AND user_id = :current_user_id + """ + ), + { + "chat_id": chat_id, + "current_user_id": current_user_id, + }, + ) + + if membership_result.scalar_one_or_none() is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + message_id = uuid4() + + message_result = await session.execute( + text( + """ + INSERT INTO messages ( + id, + chat_id, + sender_id, + text + ) + VALUES ( + :id, + :chat_id, + :sender_id, + :text + ) + RETURNING + id, + chat_id, + sender_id, + text, + created_at + """ + ), + { + "id": message_id, + "chat_id": chat_id, + "sender_id": current_user_id, + "text": data.text, + }, + ) + + row = message_result.one() + + return Message( + id=UUID(str(row.id)), + chat_id=UUID(str(row.chat_id)), + sender_id=UUID(str(row.sender_id)), + text=row.text, + created_at=row.created_at, + ) + + +async def get_messages( + session: AsyncSession, + chat_id: UUID, + current_user_id: UUID, + limit: int, + cursor: str | None, +) -> MessagePage: + membership_result = await session.execute( + text( + """ + SELECT 1 + FROM chat_members + WHERE chat_id = :chat_id + AND user_id = :current_user_id + """ + ), + { + "chat_id": chat_id, + "current_user_id": current_user_id, + }, + ) + + if membership_result.scalar_one_or_none() is None: + raise APIError( + status_code=404, + code="not_found", + message="Chat not found", + ) + + cursor_created_at = None + cursor_message_id = None + + if cursor is not None: + try: + ( + cursor_chat_id, + cursor_created_at, + cursor_message_id, + ) = decode_cursor(cursor) + except ValueError: + raise APIError( + status_code=400, + code="invalid_request", + message="Invalid cursor", + ) + + if cursor_chat_id != chat_id: + raise APIError( + status_code=400, + code="invalid_request", + message="Cursor belongs to another chat", + ) + + if cursor is None: + messages_result = await session.execute( + text( + """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE chat_id = :chat_id + ORDER BY created_at, id + LIMIT :fetch_limit + """ + ), + { + "chat_id": chat_id, + "fetch_limit": limit + 1, + }, + ) + else: + messages_result = await session.execute( + text( + """ + SELECT + id, + chat_id, + sender_id, + text, + created_at + FROM messages + WHERE chat_id = :chat_id + AND (created_at, id) > ( + :cursor_created_at, + :cursor_message_id + ) + ORDER BY created_at, id + LIMIT :fetch_limit + """ + ), + { + "chat_id": chat_id, + "cursor_created_at": cursor_created_at, + "cursor_message_id": cursor_message_id, + "fetch_limit": limit + 1, + }, + ) + + rows = messages_result.all() + + has_more = len(rows) > limit + rows = rows[:limit] + + items = [ + Message( + id=UUID(str(row.id)), + chat_id=UUID(str(row.chat_id)), + sender_id=UUID(str(row.sender_id)), + text=row.text, + created_at=row.created_at, + ) + for row in rows + ] + + next_cursor = None + + if has_more: + last_message = items[-1] + + next_cursor = encode_cursor( + chat_id=chat_id, + created_at=last_message.created_at, + message_id=last_message.id, + ) + + return MessagePage( + items=items, + next_cursor=next_cursor, + ) \ No newline at end of file diff --git a/src/security.py b/src/security.py new file mode 100644 index 00000000..0afbbf1a --- /dev/null +++ b/src/security.py @@ -0,0 +1,16 @@ +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError + + +password_hasher = PasswordHasher() + + +def hash_password(password: str) -> str: + return password_hasher.hash(password) + + +def verify_password(password_hash: str, password: str) -> bool: + try: + return password_hasher.verify(password_hash, password) + except VerifyMismatchError: + return False \ No newline at end of file diff --git a/src/sessions.py b/src/sessions.py new file mode 100644 index 00000000..6c522cbe --- /dev/null +++ b/src/sessions.py @@ -0,0 +1,87 @@ +import os +import secrets +from uuid import UUID + +from redis.asyncio import Redis +from redis.exceptions import RedisError + +from src.errors import APIError + + +CACHE_URL_FILE = "/run/secrets/cache_url" +DEFAULT_SESSION_TTL_SECONDS = 3600 + + +def read_cache_url() -> str: + with open(CACHE_URL_FILE, encoding="utf-8") as file: + return file.read().strip() + + +def get_session_ttl_seconds() -> int: + return int( + os.getenv( + "SESSION_TTL_SECONDS", + str(DEFAULT_SESSION_TTL_SECONDS), + ) + ) + + +redis_client = Redis.from_url( + read_cache_url(), + decode_responses=True, + socket_connect_timeout=5, + socket_timeout=5, +) + + +def session_store_unavailable() -> APIError: + return APIError( + status_code=503, + code="unavailable", + message="Session store unavailable", + ) + + +async def create_session(user_id: UUID) -> tuple[str, int]: + token = secrets.token_urlsafe(32) + ttl = get_session_ttl_seconds() + + try: + await redis_client.set( + f"session:{token}", + str(user_id), + ex=ttl, + ) + except RedisError as exc: + raise session_store_unavailable() from exc + + return token, ttl + + +async def get_session_user_id(token: str) -> UUID | None: + try: + value = await redis_client.get(f"session:{token}") + except RedisError as exc: + raise session_store_unavailable() from exc + + if value is None: + return None + + try: + return UUID(value) + except ValueError: + return None + + +async def delete_session(token: str) -> None: + try: + await redis_client.delete(f"session:{token}") + except RedisError as exc: + raise session_store_unavailable() from exc + + +async def ping_session_store() -> bool: + try: + return bool(await redis_client.ping()) + except RedisError: + return False \ No newline at end of file diff --git a/tests_student/test_auth.py b/tests_student/test_auth.py new file mode 100644 index 00000000..3d81a635 --- /dev/null +++ b/tests_student/test_auth.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 + +import base64 +import json +import urllib.error +import urllib.request +import uuid + + +BASE_URL = "http://localhost:8080" +PASSWORD = "correct-password-123" + + +def call( + method: str, + path: str, + body: dict | None = None, + headers: dict[str, str] | None = None, +) -> tuple[int, dict | None, dict]: + request_headers = dict(headers or {}) + + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + request_headers["Content-Type"] = "application/json" + + request = urllib.request.Request( + BASE_URL + path, + data=data, + headers=request_headers, + method=method, + ) + + try: + with urllib.request.urlopen(request, timeout=10) as response: + raw = response.read() + response_body = json.loads(raw) if raw else None + return response.status, response_body, dict(response.headers) + except urllib.error.HTTPError as error: + raw = error.read() + response_body = json.loads(raw) if raw else None + return error.code, response_body, dict(error.headers) + + +def basic_header(username: str, password: str) -> str: + credentials = f"{username}:{password}".encode("utf-8") + encoded = base64.b64encode(credentials).decode("ascii") + return f"Basic {encoded}" + + +def main() -> None: + username = "auth_" + uuid.uuid4().hex[:16] + + # 1. Создаём отдельного пользователя для этого теста. + status, body, _ = call( + "POST", + "/api/v1/users", + { + "username": username, + "display_name": "Auth Test User", + "password": PASSWORD, + }, + ) + + if status != 201: + raise AssertionError( + f"registration: expected 201, got {status}: {body}" + ) + + # 2. Неверный пароль не должен создавать сессию. + status, body, headers = call( + "POST", + "/api/v1/auth/sessions", + headers={ + "Authorization": basic_header( + username, + "definitely-wrong-password", + ) + }, + ) + + if status != 401: + raise AssertionError( + f"wrong password: expected 401, got {status}: {body}" + ) + + if body is None or body.get("error", {}).get("code") != "unauthorized": + raise AssertionError( + f"wrong password: unexpected response body: {body}" + ) + + # 3. С правильным паролем получаем Bearer-сессию. + status, body, _ = call( + "POST", + "/api/v1/auth/sessions", + headers={ + "Authorization": basic_header(username, PASSWORD) + }, + ) + + if status != 201: + raise AssertionError( + f"login: expected 201, got {status}: {body}" + ) + + if body is None: + raise AssertionError("login: empty response body") + + token = body.get("token") + + if not token: + raise AssertionError("login: token is missing") + + if body.get("token_type") != "Bearer": + raise AssertionError( + f"login: expected token_type Bearer, got {body.get('token_type')}" + ) + + bearer = {"Authorization": f"Bearer {token}"} + + # 4. До logout токен обязан работать. + status, body, _ = call( + "GET", + "/api/v1/users/me", + headers=bearer, + ) + + if status != 200: + raise AssertionError( + f"token before logout: expected 200, got {status}: {body}" + ) + + # 5. Отзываем текущую сессию. + status, body, _ = call( + "DELETE", + "/api/v1/auth/sessions/current", + headers=bearer, + ) + + if status != 204: + raise AssertionError( + f"logout: expected 204, got {status}: {body}" + ) + + # 6. Тот же токен после logout больше не принимается. + status, body, _ = call( + "GET", + "/api/v1/users/me", + headers=bearer, + ) + + if status != 401: + raise AssertionError( + f"token after logout: expected 401, got {status}: {body}" + ) + + # 7. Повторный logout с уже отозванным токеном тоже не авторизован. + status, body, _ = call( + "DELETE", + "/api/v1/auth/sessions/current", + headers=bearer, + ) + + if status != 401: + raise AssertionError( + f"repeated logout: expected 401, got {status}: {body}" + ) + + print( + "PASS: wrong password returned 401; " + "valid session worked; logout revoked the token." + ) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tests_student/test_race.py b/tests_student/test_race.py new file mode 100644 index 00000000..0bd35e4a --- /dev/null +++ b/tests_student/test_race.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +import json +import threading +import urllib.error +import urllib.request +import uuid + + +BASE_URL = "http://localhost:8080" + + +def register(username: str, barrier: threading.Barrier) -> tuple[int, dict]: + body = json.dumps( + { + "username": username, + "display_name": "Race User", + "password": "correct-password-123", + } + ).encode("utf-8") + + request = urllib.request.Request( + BASE_URL + "/api/v1/users", + data=body, + headers={"Content-Type": "application/json"}, + method="POST", + ) + + # Оба потока доходят сюда и только потом одновременно продолжают. + barrier.wait() + + try: + with urllib.request.urlopen(request, timeout=10) as response: + return response.status, json.loads(response.read()) + except urllib.error.HTTPError as error: + return error.code, json.loads(error.read()) + + +def main() -> None: + username = "race_" + uuid.uuid4().hex[:16] + barrier = threading.Barrier(2) + results: list[tuple[int, dict] | None] = [None, None] + + def worker(index: int) -> None: + results[index] = register(username, barrier) + + threads = [ + threading.Thread(target=worker, args=(0,)), + threading.Thread(target=worker, args=(1,)), + ] + + for thread in threads: + thread.start() + + for thread in threads: + thread.join() + + statuses = sorted(result[0] for result in results if result is not None) + + if statuses != [201, 409]: + raise AssertionError( + f"expected exactly one 201 and one 409, got {statuses}" + ) + + created = [ + result + for result in results + if result is not None and result[0] == 201 + ] + + conflicts = [ + result + for result in results + if result is not None and result[0] == 409 + ] + + if len(created) != 1 or created[0][1]["username"] != username: + raise AssertionError("unexpected successful registration response") + + if ( + len(conflicts) != 1 + or conflicts[0][1]["error"]["code"] != "conflict" + ): + raise AssertionError("unexpected conflict response") + + print( + "PASS: concurrent registration produced exactly " + "one 201 and one 409." + ) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tests_student/test_rollback.py b/tests_student/test_rollback.py new file mode 100644 index 00000000..f33cabae --- /dev/null +++ b/tests_student/test_rollback.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 + +import asyncio +from uuid import uuid4 + +from sqlalchemy import text +from sqlalchemy.exc import IntegrityError + +from src.database import SessionFactory + + +async def main() -> None: + chat_id = uuid4() + + async with SessionFactory() as session: + # Берём реального существующего пользователя. + result = await session.execute( + text( + """ + SELECT id + FROM users + ORDER BY created_at + LIMIT 1 + """ + ) + ) + user_id = result.scalar_one_or_none() + + if user_id is None: + raise AssertionError("test requires at least one existing user") + + try: + # Шаг 1: успешно создаём чат. + await session.execute( + text( + """ + INSERT INTO chats (id, title) + VALUES (:chat_id, :title) + """ + ), + { + "chat_id": chat_id, + "title": "Rollback automated test", + }, + ) + + # Шаг 2: успешно добавляем реального участника. + await session.execute( + text( + """ + INSERT INTO chat_members (chat_id, user_id) + VALUES (:chat_id, :user_id) + """ + ), + { + "chat_id": chat_id, + "user_id": user_id, + }, + ) + + # Шаг 3: намеренно нарушаем внешний ключ. + await session.execute( + text( + """ + INSERT INTO chat_members (chat_id, user_id) + VALUES (:chat_id, :user_id) + """ + ), + { + "chat_id": chat_id, + "user_id": uuid4(), + }, + ) + + raise AssertionError("expected foreign key violation") + + except IntegrityError: + await session.rollback() + + # После rollback начинаем новую транзакцию и проверяем БД. + chat_result = await session.execute( + text( + """ + SELECT COUNT(*) + FROM chats + WHERE id = :chat_id + """ + ), + {"chat_id": chat_id}, + ) + + chat_count = chat_result.scalar_one() + + member_result = await session.execute( + text( + """ + SELECT COUNT(*) + FROM chat_members + WHERE chat_id = :chat_id + """ + ), + {"chat_id": chat_id}, + ) + + member_count = member_result.scalar_one() + + if chat_count != 0: + raise AssertionError( + f"chat survived rollback: count={chat_count}" + ) + + if member_count != 0: + raise AssertionError( + f"chat members survived rollback: count={member_count}" + ) + + print( + "PASS: failed compound transaction rolled back " + "chat and membership inserts." + ) + + +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file diff --git a/tests_student/test_short_ttl.py b/tests_student/test_short_ttl.py new file mode 100644 index 00000000..3f469bb9 --- /dev/null +++ b/tests_student/test_short_ttl.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import base64 +import json +import time +import urllib.error +import urllib.request +import uuid + + +BASE_URL = "http://localhost:8080" +PASSWORD = "correct-password-123" + + +def call(method, path, body=None, headers=None): + request_headers = dict(headers or {}) + data = None + + if body is not None: + data = json.dumps(body).encode("utf-8") + request_headers["Content-Type"] = "application/json" + + request = urllib.request.Request( + BASE_URL + path, + data=data, + headers=request_headers, + method=method, + ) + + try: + with urllib.request.urlopen(request, timeout=10) as response: + raw = response.read() + return response.status, json.loads(raw) if raw else None + except urllib.error.HTTPError as error: + raw = error.read() + return error.code, json.loads(raw) if raw else None + + +def main(): + username = "ttl_" + uuid.uuid4().hex[:16] + + status, body = call( + "POST", + "/api/v1/users", + { + "username": username, + "display_name": "TTL Test User", + "password": PASSWORD, + }, + ) + assert status == 201, f"registration: {status}, {body}" + + credentials = base64.b64encode( + f"{username}:{PASSWORD}".encode("utf-8") + ).decode("ascii") + + status, body = call( + "POST", + "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {credentials}"}, + ) + assert status == 201, f"login: {status}, {body}" + + token = body["token"] + bearer = {"Authorization": f"Bearer {token}"} + + status, body = call("GET", "/api/v1/users/me", headers=bearer) + assert status == 200, f"before expiry: {status}, {body}" + + print("PASS: token works immediately after login.") + + time.sleep(4) + + status, body = call("GET", "/api/v1/users/me", headers=bearer) + assert status == 401, f"after expiry: {status}, {body}" + + print("PASS: expired token returned 401.") + + +if __name__ == "__main__": + main() \ No newline at end of file