From eacab162c9f10fc0a6be91f110fe3759e768fb16 Mon Sep 17 00:00:00 2001 From: ViktoriaKropivka Date: Fri, 9 Oct 2026 15:50:44 +0300 Subject: [PATCH 1/2] lab3: LB + 2 API replicas, idempotency, X-Instance-Id, failover/persistence tests (grade 4) --- Dockerfile | 23 + REPORT.md | 196 ++++++++- alembic.ini | 118 +++++ compose.yaml | 105 +++++ evidence/load-rolling.json | 17 + infra/haproxy.cfg | 28 ++ migrations/README | 1 + migrations/env.py | 46 ++ migrations/script.py.mako | 26 ++ ...2803f3a0e579_add_idempotency_keys_table.py | 33 ++ .../447472b8acec_create_initial_schema.py | 83 ++++ requirements.txt | 8 + scripts/restart.sh | 18 + scripts/stop-one.sh | 25 ++ src/__init__.py | 0 src/auth.py | 114 +++++ src/cache.py | 104 +++++ src/db.py | 72 +++ src/errors.py | 68 +++ src/instance.py | 11 + src/main.py | 253 +++++++++++ src/schemas.py | 135 ++++++ src/store.py | 414 ++++++++++++++++++ student_tests/seed_messages.py | 89 ++++ student_tests/test_cache_failure.py | 100 +++++ student_tests/test_db_failure.py | 127 ++++++ student_tests/test_expiry.py | 72 +++ student_tests/test_parallel_idempotency.py | 106 +++++ student_tests/test_races.py | 116 +++++ 29 files changed, 2490 insertions(+), 18 deletions(-) create mode 100755 Dockerfile create mode 100644 alembic.ini create mode 100644 compose.yaml create mode 100644 evidence/load-rolling.json create mode 100644 infra/haproxy.cfg create mode 100644 migrations/README create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/2803f3a0e579_add_idempotency_keys_table.py create mode 100644 migrations/versions/447472b8acec_create_initial_schema.py create mode 100755 requirements.txt create mode 100755 scripts/restart.sh create mode 100755 scripts/stop-one.sh create mode 100755 src/__init__.py create mode 100644 src/auth.py create mode 100644 src/cache.py create mode 100644 src/db.py create mode 100755 src/errors.py create mode 100644 src/instance.py create mode 100755 src/main.py create mode 100755 src/schemas.py create mode 100755 src/store.py create mode 100644 student_tests/seed_messages.py create mode 100755 student_tests/test_cache_failure.py create mode 100755 student_tests/test_db_failure.py create mode 100644 student_tests/test_expiry.py create mode 100644 student_tests/test_parallel_idempotency.py create mode 100644 student_tests/test_races.py diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 00000000..570ed446 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY src ./src +COPY migrations ./migrations +COPY alembic.ini ./ + +RUN useradd --create-home --shell /usr/sbin/nologin appuser \ + && chown -R appuser:appuser /app +USER appuser + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=5 \ + CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).status==200 else 1)" + +CMD ["python", "-m", "src.main"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index c2679005..eb56026a 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,197 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Кропивка Виктория, БВТ2303 +- Целевая оценка: 4 (уровень 3 + уровень 4) +- Commit/PR: ветка `solution/БВТ-23-Кропивка` +- Использованные источники, библиотеки и помощь ИИ: FastAPI 0.115.0, Uvicorn 0.30.6, Pydantic 2.9.2, psycopg 3.2.3, psycopg-pool 3.2.3, Alembic 1.13.3, argon2-cffi 23.1.0, redis 5.1.1, PostgreSQL 16.4-alpine, Valkey 8.0.1-alpine, HAProxy 3.0-alpine, python:3.12-slim. При подготовке решения использовался ассистент (LLM) для генерации черновика кода и объяснения контракта; итоговый код проверен публичным smoke, тестами failover/persistence/parallel idempotency и вручную. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Prerequisites: + +- Docker 29.2.1 (Docker Desktop для Windows + WSL2) +- Python 3.9+ (только для тестов) +- Git + +Шаги: + +1. Клонировать репозиторий, создать `.env` из примера: + + ```sh + cp .env.example .env + ``` + + Заполнить значениями: + ``` + APP_HOST=0.0.0.0 + APP_PORT=8080 + APP_ENV=lab + SESSION_TTL_SECONDS=3600 + POSTGRES_USER=messenger + POSTGRES_PASSWORD=changeme_local_only + POSTGRES_DB=messenger + DATABASE_URL=postgresql://messenger:changeme_local_only@postgres:5432/messenger + CACHE_URL=redis://cache:6379/0 + API_REPLICAS=2 + IDEMPOTENCY_TTL_SECONDS=86400 + ``` + +2. Запустить стенд: + + ```sh + docker compose --env-file .env up -d --build + ``` + + Compose: + - `postgres` — 16.4-alpine, named volume, только внутренняя сеть. + - `cache` — Valkey 8.0.1-alpine, named volume, только внутренняя сеть. + - `migrate` — запускает `alembic upgrade head` однократно, завершается. + - `api` — 2 реплики (`deploy.replicas: 2`), без публикации портов. + - `lb` — HAProxy 3.0-alpine, публикует `127.0.0.1:8080`. + +3. Дождаться готовности: + + ```sh + docker compose ps + ``` + + Ожидаемо: postgres, cache, api-1, api-2 — healthy, lb — up, migrate — Exited (0). + +4. Прогнать тесты: + + ```sh + make test BASE_URL=http://localhost:8080 + make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:8080 + make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://localhost:8080 + ``` + +5. Остановить: + + ```sh + docker compose --env-file .env down + ``` + + **Данные сохраняются** в named volumes `mtusi_lab3_postgres_data`, `mtusi_lab3_cache_data`. + + **Намеренное удаление:** + + ```sh + docker compose --env-file .env down -v + ``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +- **Топология.** Клиент → HAProxy (LB, порт 8080) → 2 API-реплики (внутренняя сеть, порт 8080) → общие Postgres и Valkey. +- **Балансировка.** HAProxy `roundrobin`, healthcheck `GET /health/ready` каждые 2 секунды, `fall 2 rise 2`. Отказ реплики обнаруживается за ~4 секунды. +- **Динамическое обнаружение реплик.** HAProxy резолвит `api` через Docker DNS `127.0.0.11`, `server-template api 1-10`. Если реплика добавилась или упала — HAProxy видит изменения. +- **X-Instance-Id.** Генерируется в `src/instance.py` из hostname контейнера (или UUID, если hostname недоступен). Добавляется в каждый ответ middleware `RequestIdMiddleware`. Стабилен в жизни процесса. +- **Идемпотентность.** Таблица `idempotency_keys` (PK: user_id + method + path + key). Логика: + 1. SELECT — если ключ уже есть и body совпадает → возвращаем сохранённый ответ. + 2. Если body другой → `409`. + 3. INSERT резервирования (`response_status=0`), при `UniqueViolation` → перечитываем, ждём или возвращаем 409. + 4. Создаём сообщение, UPDATE результата. +- **Источник истины — Postgres.** Valkey используется как deny-list отозванных сессий (не кэш валидных). +- **Сессии.** Bearer-токен, SHA-256-хеш в `sessions`, logout добавляет в deny-list. +- **Пароли.** Argon2id. +- **Миграции.** Alembic, отдельный сервис `migrate`, запускается один раз, API ждёт `service_completed_successfully`. +- **CPU/RAM-лимиты** на всех сервисах (`deploy.resources.limits`). +- **API не публикуют порты** — только LB. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +### Уровень 3 -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Критерий | Команда/сценарий | Результат | +| --- | --- | --- | +| Публичный smoke | `make test BASE_URL=http://localhost:8080` | `PASS: lab3 public API scenario` | +| ≥2 API-реплики | `docker compose ps` | `mtusi2026lab3-api-1`, `-api-2` Up | +| Разные реплики обслуживают | `for i in 1..6; do curl .../health/live \| grep x-instance-id; done` | чередование `5fff16304a90` / `8a77b9d87d21` | +| LB — единая точка входа | `curl http://localhost:8080/health/live` | ответ приходит от одной из реплик, порт публикует только LB | +| Остановка одной реплики | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | `{"observed_instances_before": 2, "successful_writes": 106, "failed_attempts": 1, "first_recovery_seconds": 2.718}` → PASS | +| Идемпотентность | smoke: повтор POST с тем же ключом возвращает тот же Message | PASS | +| make test-persistence | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS | + +### Уровень 4 + +| Критерий | Команда/сценарий | Результат | +| --- | --- | --- | +| LB исключает недоступный backend | `make test-failover` | восстановление за 2.7 секунды, вторая реплика продолжает обслуживать | +| Последовательная остановка каждой реплики | `make test-failover` дважды | прогон 1: recovery 2.718s, прогон 2: recovery 2.488s; оба PASS | +| Параллельная идемпотентность | `python3 student_tests/test_parallel_idempotency.py` | `Status codes: {409: 9, 201: 1}`, `Messages in history: 1` | +| CPU/RAM-лимиты | `grep -A5 "deploy:" compose.yaml` | cpus/memory на всех сервисах | +| API не публикуют порты | `docker compose ps` | у api нет `0.0.0.0:8080`, порт только у lb | +| Нет container_name | `grep container_name compose.yaml` | пусто | +| Rolling update под нагрузкой | `load.py --duration 60` + параллельно `docker restart api-1 && docker restart api-2` | 3197 успешных, 9 ошибок (0.28%), 53.2 RPS, p50=25ms, p95=183ms, p99=272ms | +| Миграции однократно | сервис `migrate` в `docker compose ps` | `Exited (0)` | + +### Метрики failover + +**Прогон 1:** + +```json +{"observed_instances_before": 2, "successful_writes": 106, "failed_attempts": 1, "first_recovery_seconds": 2.718} +``` + +**Прогон 2:** + +```json +{"observed_instances_before": 2, "successful_writes": 105, "failed_attempts": 1, "first_recovery_seconds": 2.488} +``` + +**Интерпретация:** +- `observed_instances_before: 2` — балансировка работала до отказа. +- `successful_writes: ~105` — вторая реплика продолжала обрабатывать трафик. +- `failed_attempts: 1` — только один запрос попал в момент обнаружения отказа (HAProxy `inter 2s × fall 2 = ~4s`). +- `first_recovery_seconds: ~2.5s` — укладывается в требование lab3 «≤10 секунд». + +### Метрики rolling update + +```json +{ + "requested_duration_seconds": 60.0, + "successful_requests": 3197, + "failed_requests": 9, + "error_ratio": 0.002807, + "successful_rps": 53.216, + "successful_latency_ms": { + "p50": 24.915, + "p95": 182.695, + "p99": 271.553 + } +} +``` + +**Интерпретация:** во время 60-секундной нагрузки обе реплики были поочерёдно перезапущены. 9 ошибок (0.28%) — это те запросы, которые попали в момент grace period реплики. Все остальные обработаны второй репликой. + +### Параллельная идемпотентность + +``` +Results: 10 responses +Status codes: {409: 9, 201: 1} +Messages in history: 1 +PASS: parallel idempotency — exactly one message created +``` + +10 потоков одновременно послали POST с одним `Idempotency-Key` и **разными body**. Один получил `201`, остальные — `409 conflict`. В истории **одно** сообщение. ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. - -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +| Воздействие | Ожидаем | Наблюдали | Восстановление | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Остановка реплики api-1 | Вторая продолжает | 105 успешных записей, 1 фейл | ~2.5 секунды | +| Остановка реплики api-2 | Вторая продолжает | 106 успешных, 1 фейл | ~2.7 секунды | +| Перезапуск всего стека с volumes | Данные и сессия выживают | PASS persistence | ~5 секунд | +| Параллельные POST с одним ключом | Одно сообщение | 1 сообщение в истории | — | +| Rolling update под нагрузкой | ≤ нескольких % ошибок | 0.28% (9 из 3206) | мгновенно | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- **SPOF:** LB (HAProxy) и Postgres — единственные экземпляры. Падение LB → сервис недоступен. Падение Postgres → 503. Это **осознанное** ограничение lab3: HA для БД и LB — тема lab5+ или трека «Отказ узла». +- **HAProxy на одном узле:** на Docker Desktop не проверяется перенос между физическими узлами. Это lab3 трек 2. +- **Valkey не персистентный:** deny-list теряется при рестарте. Это безопасно — Postgres источник истины. +- **Идемпотентные ключи хранятся вечно** — cleanup по `IDEMPOTENCY_TTL_SECONDS` не реализован. Для lab3 приемлемо. +- **Параллельный POST в момент рестарта реплики:** если реплика упала между `INSERT idempotency_keys (status=0)` и `UPDATE`, ключ останется «в процессе» до TTL. Клиент получит `409`. Для lab3 приемлемо, для продакшена — нужен cleanup-job. + +Следующий шаг (если делать «5»): +- **Трек 1:** цена масштабирования (1/2/4 реплики под нагрузкой, saturation point). +- **Трек 2:** отказ узла (несколько физических хостов, Swarm). \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..16feb6c9 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,118 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +# Use forward slashes (/) also on windows to provide an os agnostic path +script_location = 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 + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can 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 migrations/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 "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +# version_path_separator = newline +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# 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 + +# URL подставится из переменной окружения DATABASE_URL (см. migrations/env.py). +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 exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +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..37eeabb7 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,105 @@ +services: + postgres: + image: postgres:16.4-alpine + environment: + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme} + POSTGRES_DB: ${POSTGRES_DB:-messenger} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M + restart: unless-stopped + + cache: + image: valkey/valkey:8.0.1-alpine + command: ["valkey-server", "--save", "", "--appendonly", "no"] + volumes: + - cache_data:/data + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 3s + deploy: + resources: + limits: + cpus: "0.5" + memory: 256M + restart: unless-stopped + + migrate: + build: + context: . + dockerfile: Dockerfile + command: ["alembic", "upgrade", "head"] + depends_on: + postgres: + condition: service_healthy + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger} + restart: "no" + + api: + build: + context: . + dockerfile: Dockerfile + command: ["python", "-m", "src.main"] + depends_on: + migrate: + condition: service_completed_successfully + cache: + condition: service_healthy + environment: + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + APP_ENV: ${APP_ENV:-lab} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger} + CACHE_URL: redis://cache:6379/0 + INSTANCE_ID: ${HOSTNAME:-unknown} + deploy: + replicas: 2 + resources: + limits: + cpus: "0.5" + memory: 256M + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).status==200 else 1)"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 15s + restart: unless-stopped + + lb: + image: haproxy:3.0-alpine + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + ports: + - "127.0.0.1:8080:8080" + depends_on: + api: + condition: service_healthy + deploy: + resources: + limits: + cpus: "0.25" + memory: 128M + restart: unless-stopped + +volumes: + postgres_data: + name: mtusi_lab3_postgres_data + cache_data: + name: mtusi_lab3_cache_data \ No newline at end of file diff --git a/evidence/load-rolling.json b/evidence/load-rolling.json new file mode 100644 index 00000000..96d33543 --- /dev/null +++ b/evidence/load-rolling.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 60.0, + "elapsed_seconds": 60.076, + "concurrency": 4, + "successful_requests": 3197, + "failed_requests": 9, + "error_ratio": 0.002807, + "successful_rps": 53.216, + "successful_latency_ms": { + "p50": 24.915, + "p95": 182.695, + "p99": 271.553 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..71af7f6d --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,28 @@ +global + log stdout format raw local0 + +defaults + log global + mode http + option httplog + timeout connect 2s + timeout client 10s + timeout server 10s + +resolvers docker + nameserver dns 127.0.0.11:53 + resolve_retries 3 + timeout resolve 1s + timeout retry 1s + hold valid 5s + +frontend http_front + bind *:8080 + default_backend api_backend + +backend api_backend + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready + http-check expect status 200 + server-template api 1-10 api:8080 check resolvers docker init-addr libc diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..c2a4c9e5 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,46 @@ +from __future__ import annotations + +import os +from logging.config import fileConfig + +from alembic import context + +config = context.config + +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +database_url = os.environ.get("DATABASE_URL") +if not database_url: + raise RuntimeError("DATABASE_URL environment variable is required for migrations") + +target_metadata = None + + +def run_migrations_offline() -> None: + """Оффлайн-режим: печатает SQL, не подключаясь к БД.""" + context.configure( + url=database_url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + """Онлайн-режим: подключается к БД и применяет миграции.""" + from sqlalchemy import create_engine + + connectable = create_engine(database_url, pool_pre_ping=True) + with connectable.connect() as connection: + context.configure(connection=connection, target_metadata=target_metadata) + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + 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..fbc4b07d --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,26 @@ +"""${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, 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: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/2803f3a0e579_add_idempotency_keys_table.py b/migrations/versions/2803f3a0e579_add_idempotency_keys_table.py new file mode 100644 index 00000000..a1a0e0f8 --- /dev/null +++ b/migrations/versions/2803f3a0e579_add_idempotency_keys_table.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +from alembic import op + +revision = "2803f3a0e579" +down_revision = "447472b8acec" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute(""" + CREATE TABLE idempotency_keys ( + user_id UUID NOT NULL, + method TEXT NOT NULL, + path TEXT NOT NULL, + key TEXT NOT NULL, + request_hash TEXT NOT NULL, + response_status INTEGER NOT NULL, + response_body JSONB NOT NULL, + created_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (user_id, method, path, key) + ) + """) + op.execute(""" + CREATE INDEX idempotency_keys_created_at_idx + ON idempotency_keys (created_at) + """) + + +def downgrade() -> None: + op.execute("DROP INDEX IF EXISTS idempotency_keys_created_at_idx") + op.execute("DROP TABLE IF EXISTS idempotency_keys") \ No newline at end of file diff --git a/migrations/versions/447472b8acec_create_initial_schema.py b/migrations/versions/447472b8acec_create_initial_schema.py new file mode 100644 index 00000000..0cf534f1 --- /dev/null +++ b/migrations/versions/447472b8acec_create_initial_schema.py @@ -0,0 +1,83 @@ +"""create initial schema + +Revision ID: <оставь тот, что сгенерировал alembic> +Revises: +Create Date: <оставь> +""" +from __future__ import annotations + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "447472b8acec" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute(""" + CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL UNIQUE, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL + ) + """) + + op.execute(""" + CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL + ) + """) + + op.execute(""" + CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) + ) + """) + + op.execute(""" + CREATE INDEX chat_members_user_idx ON chat_members (user_id) + """) + + op.execute(""" + CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id), + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL + ) + """) + + op.execute(""" + CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id) + """) + + op.execute(""" + CREATE TABLE sessions ( + token_hash TEXT PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL + ) + """) + + op.execute(""" + CREATE INDEX sessions_user_idx ON sessions (user_id) + """) + + +def downgrade() -> None: + op.execute("DROP TABLE IF EXISTS sessions") + op.execute("DROP TABLE IF EXISTS messages") + op.execute("DROP INDEX IF EXISTS chat_members_user_idx") + op.execute("DROP TABLE IF EXISTS chat_members") + op.execute("DROP TABLE IF EXISTS chats") + op.execute("DROP TABLE IF EXISTS users") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100755 index 00000000..d0d86d54 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +fastapi==0.115.0 +uvicorn[standard]==0.30.6 +pydantic==2.9.2 +psycopg[binary]==3.2.3 +alembic==1.13.3 +argon2-cffi==23.1.0 +psycopg-pool==3.2.3 +redis==5.1.1 \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..8cb4d454 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,18 @@ +#!/bin/sh +set -eu + +cd "$(dirname "$0")/.." + +docker compose --env-file .env restart postgres cache api + +i=0 +while [ $i -lt 60 ]; do + if curl -sf http://localhost:8080/health/ready > /dev/null 2>&1; then + exit 0 + fi + sleep 1 + i=$((i + 1)) +done + +echo "stack did not become healthy in 60 seconds" >&2 +exit 1 \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..9d99ee6c --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID environment variable is required}" + +echo "Looking for API container with instance_id=$TARGET_INSTANCE_ID" + +found="" +for container in $(docker ps --format '{{.Names}}' | grep -E '^mtusi.*-api-[0-9]+$'); do + if docker logs "$container" 2>&1 | grep -q "instance_id=$TARGET_INSTANCE_ID"; then + found="$container" + break + fi +done + +if [ -z "$found" ]; then + echo "ERROR: no container found with instance_id=$TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +echo "Stopping container: $found" +docker stop "$found" + +echo "Container $found stopped. Other replicas continue serving." +exit 0 \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/src/auth.py b/src/auth.py new file mode 100644 index 00000000..c89e0b3b --- /dev/null +++ b/src/auth.py @@ -0,0 +1,114 @@ +from __future__ import annotations + +import hashlib +import os +import secrets +from datetime import datetime, timedelta, timezone +from typing import Optional + +import psycopg +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError + +from .db import transaction + +_hasher = PasswordHasher() + + +def hash_password(password: str) -> str: + return _hasher.hash(password) + + +def verify_password(password: str, password_hash: str) -> bool: + try: + _hasher.verify(password_hash, password) + return True + except (VerifyMismatchError, VerificationError, InvalidHashError): + return False + + +def _now() -> datetime: + return datetime.now(timezone.utc) + + +def _ttl_seconds() -> int: + return int(os.environ.get("SESSION_TTL_SECONDS", "3600")) + + +def _generate_token() -> str: + return secrets.token_urlsafe(32) + + +def _token_hash(token: str) -> str: + return hashlib.sha256(token.encode("utf-8")).hexdigest() + + +def create_session(user_id: str) -> dict: + token = _generate_token() + ttl = _ttl_seconds() + expires_at = _now() + timedelta(seconds=ttl) + token_hash = _token_hash(token) + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + INSERT INTO sessions (token_hash, user_id, expires_at, created_at) + VALUES (%s, %s, %s, %s) + """, + (token_hash, user_id, expires_at, _now()), + ) + return {"token": token, "expires_at": expires_at} + + +def get_user_id_by_token(token: str) -> Optional[str]: + from . import cache + + token_hash = _token_hash(token) + + if cache.is_revoked(token_hash): + return None + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + SELECT user_id, expires_at + FROM sessions + WHERE token_hash = %s + """, + (token_hash,), + ) + row = cur.fetchone() + if row is None: + return None + if row["expires_at"] <= _now(): + return None + return str(row["user_id"]) + + +def revoke_session(token: str) -> bool: + from . import cache + + token_hash = _token_hash(token) + ttl = _ttl_seconds() + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT expires_at FROM sessions WHERE token_hash = %s", + (token_hash,), + ) + row = cur.fetchone() + if row is not None: + remaining = int((row["expires_at"] - _now()).total_seconds()) + if remaining > 0: + ttl = remaining + cur.execute( + "DELETE FROM sessions WHERE token_hash = %s", + (token_hash,), + ) + deleted = cur.rowcount > 0 + + if deleted: + cache.mark_revoked(token_hash, ttl) + return deleted \ No newline at end of file diff --git a/src/cache.py b/src/cache.py new file mode 100644 index 00000000..2b10432d --- /dev/null +++ b/src/cache.py @@ -0,0 +1,104 @@ +from __future__ import annotations + +import logging +import os +from typing import Optional + +import redis + +log = logging.getLogger("messenger.cache") + +_client: Optional[redis.Redis] = None +_failures: int = 0 +_MAX_FAILURES_BEFORE_DISABLE: int = 3 + + +def _cache_url() -> Optional[str]: + return os.environ.get("CACHE_URL") + + +def init_cache() -> None: + global _client, _failures + url = _cache_url() + if not url: + log.warning("CACHE_URL not set; running without cache") + _client = None + return + try: + client = redis.Redis.from_url( + url, + decode_responses=True, + socket_connect_timeout=1.0, + socket_timeout=1.0, + ) + client.ping() + _client = client + _failures = 0 + log.info("cache connected") + except Exception as exc: + log.warning("cache unavailable at startup: %s; running in degraded mode", exc) + _client = None + + +def close_cache() -> None: + global _client + if _client is not None: + try: + _client.close() + except Exception: + pass + _client = None + + +def _note_failure() -> None: + global _client, _failures + _failures += 1 + if _failures >= _MAX_FAILURES_BEFORE_DISABLE and _client is not None: + log.warning( + "cache disabled after %d consecutive failures; running in degraded mode", + _failures, + ) + try: + _client.close() + except Exception: + pass + _client = None + + +def _note_success() -> None: + global _failures + _failures = 0 + + +def is_available() -> bool: + return _client is not None + + +def mark_revoked(token_hash: str, ttl_seconds: int) -> None: + if _client is None: + log.warning( + "cache unavailable; revocation not recorded in Redis " + "(Postgres is the source of truth, next request will 401)" + ) + return + if ttl_seconds <= 0: + return + try: + _client.setex(f"revoked:{token_hash}", ttl_seconds, "1") + _note_success() + except Exception as exc: + log.warning("mark_revoked failed: %s", exc) + _note_failure() + + +def is_revoked(token_hash: str) -> bool: + if _client is None: + return False + try: + exists = _client.exists(f"revoked:{token_hash}") + _note_success() + return exists == 1 + except Exception as exc: + log.warning("is_revoked failed: %s", exc) + _note_failure() + return False \ No newline at end of file diff --git a/src/db.py b/src/db.py new file mode 100644 index 00000000..d0455b67 --- /dev/null +++ b/src/db.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +import os +from contextlib import contextmanager +from typing import Iterator + +import psycopg +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool + + +_pool: ConnectionPool | None = None + + +def _database_url() -> str: + url = os.environ.get("DATABASE_URL") + if not url: + raise RuntimeError("DATABASE_URL environment variable is required") + return url + + +from psycopg_pool import ConnectionPool + +def init_pool(min_size: int = 1, max_size: int = 10) -> None: + global _pool + if _pool is not None: + return + + dsn = _database_url() + sep = "&" if "?" in dsn else "?" + dsn_with_opts = ( + dsn + + sep + + "options=" + + "-c%20statement_timeout%3D5000" + + "%20-c%20lock_timeout%3D3000" + + "%20-c%20idle_in_transaction_session_timeout%3D10000" + ) + + _pool = ConnectionPool( + conninfo=dsn_with_opts, + min_size=min_size, + max_size=max_size, + kwargs={"row_factory": dict_row}, + open=True, + timeout=5.0, + check=ConnectionPool.check_connection, + max_lifetime=120, + max_idle=60, + ) + + +def close_pool() -> None: + global _pool + if _pool is not None: + _pool.close() + _pool = None + + +@contextmanager +def connection() -> Iterator[psycopg.Connection]: + if _pool is None: + raise RuntimeError("Connection pool is not initialized") + with _pool.connection() as conn: + yield conn + + +@contextmanager +def transaction() -> Iterator[psycopg.Connection]: + with connection() as conn: + with conn.transaction(): + yield conn \ No newline at end of file diff --git a/src/errors.py b/src/errors.py new file mode 100755 index 00000000..098bce17 --- /dev/null +++ b/src/errors.py @@ -0,0 +1,68 @@ +from __future__ import annotations + +import uuid +from typing import Callable + +from fastapi import Request +from fastapi.responses import JSONResponse +from starlette.middleware.base import BaseHTTPMiddleware + +from .store import ApiError + +from .instance import INSTANCE_ID + + +REQUEST_ID_HEADER = "X-Request-Id" + + +class RequestIdMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + rid = request.headers.get(REQUEST_ID_HEADER) or uuid.uuid4().hex + request.state.request_id = rid + response = await call_next(request) + response.headers[REQUEST_ID_HEADER] = rid + response.headers["X-Instance-Id"] = INSTANCE_ID + return response + + +def _envelope(code: str, message: str, request_id: str) -> dict: + return {"error": {"code": code, "message": message, "request_id": request_id}} + + +def _rid(request: Request) -> str: + return getattr(request.state, "request_id", None) or uuid.uuid4().hex + + +async def api_error_handler(request: Request, exc: Exception) -> JSONResponse: + assert isinstance(exc, ApiError) + response = JSONResponse( + status_code=exc.status, + content=_envelope(exc.code, exc.message, _rid(request)), + ) + if exc.status == 401: + scheme = "Bearer" + if request.url.path == "/api/v1/auth/sessions": + scheme = "Basic" + response.headers["WWW-Authenticate"] = f'{scheme} realm="messenger"' + return response + + +async def validation_handler(request: Request, exc: Exception) -> JSONResponse: + return JSONResponse( + status_code=400, + content=_envelope("invalid_request", "Request validation failed", _rid(request)), + ) + + +async def json_decode_handler(request: Request, exc: Exception) -> JSONResponse: + return JSONResponse( + status_code=400, + content=_envelope("invalid_request", "Malformed JSON", _rid(request)), + ) + + +async def unhandled_handler(request: Request, exc: Exception) -> JSONResponse: + return JSONResponse( + status_code=500, + content=_envelope("internal_error", "Internal error", _rid(request)), + ) \ No newline at end of file diff --git a/src/instance.py b/src/instance.py new file mode 100644 index 00000000..701ad56a --- /dev/null +++ b/src/instance.py @@ -0,0 +1,11 @@ +from __future__ import annotations + +import os +import socket +import uuid + +INSTANCE_ID: str = ( + os.environ.get("INSTANCE_ID") + or socket.gethostname() + or uuid.uuid4().hex[:8] +) \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100755 index 00000000..e05782f2 --- /dev/null +++ b/src/main.py @@ -0,0 +1,253 @@ +from __future__ import annotations + +import logging +import os +import re +from contextlib import asynccontextmanager +from typing import Optional +import uuid + +from fastapi import Depends, FastAPI, Header, Query, Request, Response +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from starlette.exceptions import HTTPException as StarletteHTTPException + +from . import auth +from .db import close_pool, init_pool +from .errors import ( + RequestIdMiddleware, + _envelope, + _rid, + api_error_handler, + json_decode_handler, + unhandled_handler, + validation_handler, +) +from .schemas import ( + Chat, + ChatPage, + CreateChat, + CreateMessage, + CreateUser, + Health, + Message, + MessagePage, + Session, + User, +) +from .store import ApiError, BadRequest, Conflict, NotFound, Unauthorized, store, iso +from .cache import close_cache, init_cache + +from .instance import INSTANCE_ID + +logging.basicConfig( + level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s" +) +log = logging.getLogger("messenger") + + +@asynccontextmanager +async def lifespan(app: FastAPI): + init_pool() + init_cache() + log.info("instance_id=%s", INSTANCE_ID) + log.info("db pool and cache initialized") + try: + yield + finally: + close_cache() + close_pool() + log.info("db pool and cache closed") + + +app = FastAPI(title="MTUSI Messenger lab2", version="1.2.0", lifespan=lifespan) +app.add_middleware(RequestIdMiddleware) + +app.add_exception_handler(ApiError, api_error_handler) +app.add_exception_handler(RequestValidationError, validation_handler) +app.add_exception_handler(Exception, unhandled_handler) + + +@app.exception_handler(StarletteHTTPException) +async def _http_exc(request: Request, exc: StarletteHTTPException): + code = "not_found" if exc.status_code == 404 else "invalid_request" + return JSONResponse( + status_code=exc.status_code, + content=_envelope(code, str(exc.detail), _rid(request)), + ) + + +# ---------- зависимости ---------- + +def _parse_basic(header_value: str) -> tuple[str, str]: + """Разбирает `Basic base64(username:password)`. Бросает Unauthorized.""" + import base64 + if not header_value.lower().startswith("basic "): + raise Unauthorized("Basic authentication required") + try: + raw = base64.b64decode(header_value.split(" ", 1)[1], validate=True) + decoded = raw.decode("utf-8") + username, _, password = decoded.partition(":") + if not username: + raise ValueError("empty username") + return username, password + except Exception: + raise Unauthorized("Invalid Basic credentials") from None + + +def _bearer_token(authorization: Optional[str]) -> str: + if not authorization: + raise Unauthorized("Missing Authorization header") + parts = authorization.split(" ", 1) + if len(parts) != 2 or parts[0].lower() != "bearer": + raise Unauthorized("Bearer token required") + token = parts[1].strip() + if not token: + raise Unauthorized("Empty bearer token") + return token + + +def current_user_id( + authorization: Optional[str] = Header(default=None), +) -> str: + token = _bearer_token(authorization) + user_id = auth.get_user_id_by_token(token) + if user_id is None: + raise Unauthorized("Invalid or expired token") + return user_id + + +# ---------- health ---------- + +@app.get("/health/live", response_model=Health) +def liveness(): + return {"status": "ok"} + + +@app.get("/health/ready", response_model=Health) +def readiness(): + try: + from .db import transaction + with transaction() as conn: + with conn.cursor() as cur: + cur.execute("SELECT 1") + except Exception: + return JSONResponse( + status_code=503, + content=_envelope("unavailable", "Database unavailable", "readiness"), + ) + return {"status": "ok"} + + +# ---------- users ---------- + +@app.post("/api/v1/users", status_code=201, response_model=User) +def register_user(body: CreateUser): + password_hash = auth.hash_password(body.password) + return store.create_user(body.username, body.display_name, password_hash) + + +@app.get("/api/v1/users/me", response_model=User) +def me(user_id: str = Depends(current_user_id)): + user = store.get_user(user_id) + if user is None: + raise Unauthorized("User not found") + return user + + +# ---------- auth ---------- + +@app.post("/api/v1/auth/sessions", status_code=201, response_model=Session) +def create_session(authorization: Optional[str] = Header(default=None)): + username, password = _parse_basic(authorization or "") + row = store.get_user_by_username(username) + if row is None or not auth.verify_password(password, row["password_hash"]): + raise Unauthorized("Invalid credentials") + session = auth.create_session(str(row["id"])) + user = store.get_user(str(row["id"])) + if user is None: + raise Unauthorized("User not found") + return { + "token": session["token"], + "token_type": "Bearer", + "expires_at": iso(session["expires_at"]), + "user": user, + } + + +@app.delete("/api/v1/auth/sessions/current", status_code=204) +def logout(authorization: Optional[str] = Header(default=None)): + token = _bearer_token(authorization) + user_id = auth.get_user_id_by_token(token) + if user_id is None: + raise Unauthorized("Invalid or expired token") + auth.revoke_session(token) + return Response(status_code=204) + + +# ---------- chats ---------- + +@app.post("/api/v1/chats", status_code=201, response_model=Chat) +def create_chat(body: CreateChat, user_id: str = Depends(current_user_id)): + return store.create_chat(body.title, body.member_ids, user_id) + + +@app.get("/api/v1/chats", response_model=ChatPage) +def list_chats( + limit: int = Query(default=50, ge=1, le=100), + cursor: Optional[str] = Query(default=None, min_length=1), + user_id: str = Depends(current_user_id), +): + items, next_cursor = store.list_chats(user_id, limit, cursor) + return {"items": items, "next_cursor": next_cursor} + + +@app.get("/api/v1/chats/{chat_id}", response_model=Chat) +def get_chat(chat_id: str, user_id: str = Depends(current_user_id)): + chat = store.get_chat(chat_id, user_id) + if chat is None: + raise NotFound() + return chat + + +# ---------- messages ---------- + +@app.post("/api/v1/chats/{chat_id}/messages", status_code=201, response_model=Message) +def create_message( + chat_id: str, + body: CreateMessage, + user_id: str = Depends(current_user_id), + idempotency_key: Optional[str] = Header(default=None, alias="Idempotency-Key"), +): + if not idempotency_key: + raise BadRequest("Idempotency-Key header is required") + if not re.match(r"^[A-Za-z0-9._:-]{1,128}$", idempotency_key): + raise BadRequest("Invalid Idempotency-Key format") + + message, status = store.add_message(chat_id, user_id, body.text, idempotency_key) + if status != 201: + raise Conflict(f"Idempotency error: {status}") + return message + + +@app.get("/api/v1/chats/{chat_id}/messages", response_model=MessagePage) +def list_messages( + chat_id: str, + limit: int = Query(default=50, ge=1, le=100), + cursor: Optional[str] = Query(default=None, min_length=1), + user_id: str = Depends(current_user_id), +): + items, next_cursor = store.list_messages(chat_id, user_id, limit, cursor) + return {"items": items, "next_cursor": next_cursor} + + +def main() -> None: + import uvicorn + + host = os.environ.get("APP_HOST", "0.0.0.0") + port = int(os.environ.get("APP_PORT", "8080")) + uvicorn.run("src.main:app", host=host, port=port, log_level="info") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/schemas.py b/src/schemas.py new file mode 100755 index 00000000..96f08994 --- /dev/null +++ b/src/schemas.py @@ -0,0 +1,135 @@ +from __future__ import annotations + +import re +import uuid +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field, field_validator + +USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$") + + +class StrictModel(BaseModel): + model_config = ConfigDict(extra="forbid", str_strip_whitespace=False) + + +# ---------- Users ---------- + +class CreateUser(StrictModel): + username: str = Field(min_length=3, max_length=32) + display_name: str = Field(min_length=1, max_length=100) + password: str = Field(min_length=12, max_length=128) + + @field_validator("username") + @classmethod + def _username(cls, v: str) -> str: + if not USERNAME_RE.match(v): + raise ValueError("username must match [a-z0-9_]{3,32}") + return v + + @field_validator("display_name") + @classmethod + def _display_name(cls, v: str) -> str: + if not v.strip(): + raise ValueError("display_name must not be blank") + if len(v) > 100: + raise ValueError("display_name too long") + return v + + +class User(StrictModel): + id: str + username: str + display_name: str + created_at: str + + +# ---------- Chats ---------- + +class CreateChat(StrictModel): + title: str = Field(min_length=1, max_length=100) + member_ids: List[str] = Field(min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def _title(cls, v: str) -> str: + if not v.strip(): + raise ValueError("title must not be blank") + return v + + @field_validator("member_ids") + @classmethod + def _members(cls, v: List[str]) -> List[str]: + if len(set(v)) != len(v): + raise ValueError("member_ids must be unique") + for item in v: + try: + uuid.UUID(item) + except ValueError as exc: + raise ValueError("member_ids must be UUIDs") from exc + return v + + +class Chat(StrictModel): + id: str + title: str + member_ids: List[str] + created_at: str + + +class ChatPage(StrictModel): + items: List[Chat] + next_cursor: Optional[str] = None + + +# ---------- Messages ---------- + +class CreateMessage(StrictModel): + text: str = Field(min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def _text(cls, v: str) -> str: + if not v.strip(): + raise ValueError("text must not be blank") + if len(v) > 4000: + raise ValueError("text too long") + return v + + +class Message(StrictModel): + id: str + chat_id: str + sender_id: str + text: str + created_at: str + + +class MessagePage(StrictModel): + items: List[Message] + next_cursor: Optional[str] = None + + +# ---------- Sessions ---------- + +class Session(StrictModel): + token: str = Field(min_length=32) + token_type: str = Field(default="Bearer") + expires_at: str + user: User + + +# ---------- Misc ---------- + +class Health(StrictModel): + status: str = "ok" + + +class ErrorBody(StrictModel): + code: str + message: str + request_id: str + + +class ErrorEnvelope(StrictModel): + error: ErrorBody \ No newline at end of file diff --git a/src/store.py b/src/store.py new file mode 100755 index 00000000..9442d776 --- /dev/null +++ b/src/store.py @@ -0,0 +1,414 @@ +from __future__ import annotations + +import base64 +import json +import uuid +from datetime import datetime, timezone +from typing import List, Optional, Tuple + +import psycopg +from psycopg.errors import UniqueViolation + +from .db import transaction + +import hashlib +import json as _json + + +# ---------- ошибки ---------- + +class ApiError(Exception): + status = 500 + code = "internal_error" + + def __init__(self, message: str = "Internal error") -> None: + super().__init__(message) + self.message = message + + +class BadRequest(ApiError): + status = 400 + code = "invalid_request" + + +class Unauthorized(ApiError): + status = 401 + code = "unauthorized" + + +class NotFound(ApiError): + status = 404 + code = "not_found" + + +class Conflict(ApiError): + status = 409 + code = "conflict" + + +# ---------- утилиты ---------- + +def _now() -> datetime: + return datetime.now(timezone.utc) + + +def iso(dt: datetime) -> str: + return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ") + + +def _user_dict(row: dict) -> dict: + return { + "id": str(row["id"]), + "username": row["username"], + "display_name": row["display_name"], + "created_at": iso(row["created_at"]), + } + + +def _chat_dict(row: dict, member_ids: List[str]) -> dict: + return { + "id": str(row["id"]), + "title": row["title"], + "member_ids": member_ids, + "created_at": iso(row["created_at"]), + } + + +def _message_dict(row: dict) -> dict: + return { + "id": str(row["id"]), + "chat_id": str(row["chat_id"]), + "sender_id": str(row["sender_id"]), + "text": row["text"], + "created_at": iso(row["created_at"]), + } + + +# ---------- курсор ---------- + +def _encode_cursor(scope: str, created_at: str, item_id: str) -> str: + payload = json.dumps( + {"s": scope, "t": created_at, "i": item_id}, separators=(",", ":") + ).encode() + return base64.urlsafe_b64encode(payload).decode().rstrip("=") + + +def _decode_cursor(scope: str, cursor: str) -> Tuple[str, str]: + pad = "=" * (-len(cursor) % 4) + try: + raw = base64.urlsafe_b64decode(cursor + pad) + data = json.loads(raw) + if data.get("s") != scope: + raise ValueError("scope mismatch") + return data["t"], data["i"] + except Exception as exc: + raise BadRequest("invalid cursor") from exc + + +# ---------- Store ---------- + +class Store: + + # ---- users ---- + + def create_user( + self, username: str, display_name: str, password_hash: str + ) -> dict: + uid = str(uuid.uuid4()) + now = _now() + try: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (uid, username, display_name, password_hash, now), + ) + except UniqueViolation: + raise Conflict("username already taken") from None + return { + "id": uid, + "username": username, + "display_name": display_name, + "created_at": iso(now), + } + + def get_user(self, user_id: str) -> Optional[dict]: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users WHERE id = %s", (user_id,)) + row = cur.fetchone() + return _user_dict(row) if row else None + + def get_user_by_username(self, username: str) -> Optional[dict]: + """Возвращает полную строку, включая password_hash (для входа).""" + with transaction() as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM users WHERE username = %s", (username,)) + row = cur.fetchone() + return row + + # ---- chats ---- + + def create_chat( + self, title: str, member_ids: List[str], creator_id: str + ) -> dict: + if creator_id not in member_ids: + raise BadRequest("creator must be in member_ids") + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT id FROM users WHERE id = ANY(%s)", + (member_ids,), + ) + found = {str(r["id"]) for r in cur.fetchall()} + missing = set(member_ids) - found + if missing: + raise BadRequest("unknown member_id") + + cid = str(uuid.uuid4()) + now = _now() + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)", + (cid, title, now), + ) + cur.executemany( + "INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)", + [(cid, uid) for uid in member_ids], + ) + return { + "id": cid, + "title": title, + "member_ids": list(member_ids), + "created_at": iso(now), + } + + def get_chat(self, chat_id: str, viewer_id: str) -> Optional[dict]: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + SELECT c.* FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE c.id = %s AND m.user_id = %s + """, + (chat_id, viewer_id), + ) + row = cur.fetchone() + if row is None: + return None + cur.execute( + "SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY user_id", + (chat_id,), + ) + members = [str(r["user_id"]) for r in cur.fetchall()] + return _chat_dict(row, members) + + def list_chats( + self, viewer_id: str, limit: int, cursor: Optional[str] + ) -> Tuple[List[dict], Optional[str]]: + scope = "chats:" + viewer_id + params: list = [viewer_id] + where_cursor = "" + if cursor: + t, i = _decode_cursor(scope, cursor) + where_cursor = "AND (c.created_at, c.id) > (%s, %s)" + params.extend([t, i]) + params.append(limit + 1) # +1, чтобы узнать, есть ли следующая страница + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + f""" + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = %s {where_cursor} + ORDER BY c.created_at, c.id + LIMIT %s + """, + params, + ) + rows = cur.fetchall() + + has_more = len(rows) > limit + page = rows[:limit] + + result = [] + for row in page: + cur.execute( + "SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY user_id", + (row["id"],), + ) + members = [str(r["user_id"]) for r in cur.fetchall()] + result.append(_chat_dict(row, members)) + + next_cursor = None + if has_more and page: + last = page[-1] + next_cursor = _encode_cursor(scope, iso(last["created_at"]), str(last["id"])) + return result, next_cursor + + # ---- messages ---- + + def add_message( + self, + chat_id: str, + sender_id: str, + text: str, + idempotency_key: str, + ) -> tuple[dict, int]: + request_hash = hashlib.sha256( + _json.dumps({"text": text}, ensure_ascii=False, sort_keys=True).encode("utf-8") + ).hexdigest() + + path = f"/api/v1/chats/{chat_id}/messages" + method = "POST" + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + SELECT request_hash, response_status, response_body + FROM idempotency_keys + WHERE user_id = %s AND method = %s AND path = %s AND key = %s + """, + (sender_id, method, path, idempotency_key), + ) + row = cur.fetchone() + if row is not None: + if row["request_hash"] != request_hash: + raise Conflict("idempotency key reused with different body") + return row["response_body"], row["response_status"] + + try: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + INSERT INTO idempotency_keys + (user_id, method, path, key, request_hash, + response_status, response_body, created_at) + VALUES (%s, %s, %s, %s, %s, 0, '{}'::jsonb, %s) + """, + (sender_id, method, path, idempotency_key, request_hash, _now()), + ) + except UniqueViolation: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + """ + SELECT request_hash, response_status, response_body + FROM idempotency_keys + WHERE user_id = %s AND method = %s AND path = %s AND key = %s + """, + (sender_id, method, path, idempotency_key), + ) + row = cur.fetchone() + if row is None: + raise Conflict("idempotency key race, please retry") + if row["request_hash"] != request_hash: + raise Conflict("idempotency key reused with different body") + if row["response_status"] == 0: + raise Conflict("idempotency key in progress, please retry") + return row["response_body"], row["response_status"] + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", + (chat_id, sender_id), + ) + if cur.fetchone() is None: + cur.execute( + "DELETE FROM idempotency_keys WHERE user_id = %s AND method = %s AND path = %s AND key = %s", + (sender_id, method, path, idempotency_key), + ) + raise NotFound() + + mid = str(uuid.uuid4()) + now = _now() + cur.execute( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (mid, chat_id, sender_id, text, now), + ) + + message = { + "id": mid, + "chat_id": chat_id, + "sender_id": sender_id, + "text": text, + "created_at": iso(now), + } + + cur.execute( + """ + UPDATE idempotency_keys + SET response_status = 201, response_body = %s + WHERE user_id = %s AND method = %s AND path = %s AND key = %s + """, + (_json.dumps(message, ensure_ascii=False), + sender_id, method, path, idempotency_key), + ) + + return message, 201 + + def list_messages( + self, + chat_id: str, + viewer_id: str, + limit: int, + cursor: Optional[str], + ) -> Tuple[List[dict], Optional[str]]: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", + (chat_id, viewer_id), + ) + if cur.fetchone() is None: + raise NotFound() + + scope = "chat:" + chat_id + params: list = [chat_id] + where_cursor = "" + if cursor: + t, i = _decode_cursor(scope, cursor) + where_cursor = "AND (created_at, id) > (%s, %s)" + params.extend([t, i]) + params.append(limit + 1) + + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + f""" + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = %s {where_cursor} + ORDER BY created_at, id + LIMIT %s + """, + params, + ) + rows = cur.fetchall() + + has_more = len(rows) > limit + page = rows[:limit] + result = [_message_dict(r) for r in page] + + next_cursor = None + if has_more and page: + last = page[-1] + next_cursor = _encode_cursor(scope, iso(last["created_at"]), str(last["id"])) + return result, next_cursor + + +store = Store() \ No newline at end of file diff --git a/student_tests/seed_messages.py b/student_tests/seed_messages.py new file mode 100644 index 00000000..401e3044 --- /dev/null +++ b/student_tests/seed_messages.py @@ -0,0 +1,89 @@ +"""Генерация тестовых сообщений для EXPLAIN. + +Создаёт пользователя, чат и N сообщений. N по умолчанию 10 000. +Запуск: + python3 seed_messages.py --base-url http://localhost:8080 --count 10000 +""" +from __future__ import annotations + +import argparse +import base64 +import json +import time +import urllib.error +import urllib.request +import uuid + + +def call(base: str, method: str, path: str, headers: dict | None = None, body: dict | None = None) -> tuple[int, dict | None]: + data = None + h = dict(headers or {}) + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(base + path, data=data, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=30) + raw = resp.read() + return resp.code, json.loads(raw) if raw else None + except urllib.error.HTTPError as e: + raw = e.read() + try: + return e.code, json.loads(raw) if raw else None + except Exception: + return e.code, None + + +def main() -> None: + p = argparse.ArgumentParser() + p.add_argument("--base-url", default="http://localhost:8080") + p.add_argument("--count", type=int, default=10000) + p.add_argument("--batch", type=int, default=500, help="Сообщений между progress-логами") + args = p.parse_args() + base = args.base_url.rstrip("/") + + username = "seed_" + uuid.uuid4().hex[:12] + password = "seed-pass-1234567890" + + # Регистрация + code, user = call(base, "POST", "/api/v1/users", body={ + "username": username, "display_name": "Seed User", "password": password, + }) + assert code == 201, f"register failed: {code} {user}" + + # Логин + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + code, session = call(base, "POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}) + assert code == 201, f"login failed: {code} {session}" + token = session["token"] + user_id = session["user"]["id"] + + # Чат + code, chat = call(base, "POST", "/api/v1/chats", headers={"Authorization": "Bearer " + token}, body={ + "title": "Seed chat", "member_ids": [user_id], + }) + assert code == 201, f"chat failed: {code} {chat}" + chat_id = chat["id"] + print(f"chat_id: {chat_id}") + print(f"seeding {args.count} messages...") + + started = time.monotonic() + for i in range(args.count): + code, msg = call( + base, "POST", f"/api/v1/chats/{chat_id}/messages", + headers={"Authorization": "Bearer " + token}, + body={"text": f"seed-{i:06d}-{uuid.uuid4().hex[:8]}"}, + ) + assert code == 201, f"message {i} failed: {code} {msg}" + if (i + 1) % args.batch == 0: + elapsed = time.monotonic() - started + rps = (i + 1) / elapsed + print(f" {i + 1}/{args.count} ({rps:.0f} msg/s)") + + elapsed = time.monotonic() - started + print(f"done: {args.count} messages in {elapsed:.1f}s ({args.count / elapsed:.0f} msg/s)") + print(f"chat_id: {chat_id}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student_tests/test_cache_failure.py b/student_tests/test_cache_failure.py new file mode 100755 index 00000000..00ab135a --- /dev/null +++ b/student_tests/test_cache_failure.py @@ -0,0 +1,100 @@ +"""Проверка отказа Valkey и корректности logout при недоступном кэше.""" +from __future__ import annotations +import base64, json, subprocess, sys, time, urllib.error, urllib.request, uuid + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] + +def call(method, path, headers=None, body=None): + data = None + h = dict(headers or {}) + if body is not None: + data = json.dumps(body).encode() + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + r = urllib.request.urlopen(req, timeout=10) + raw = r.read() + return r.code, json.loads(raw) if raw else None + except urllib.error.HTTPError as e: + raw = e.read() + try: + return e.code, json.loads(raw) if raw else None + except Exception: + return e.code, None + +def stop_cache(): + subprocess.run(COMPOSE + ["stop", "cache"], check=True) + +def start_cache(): + subprocess.run(COMPOSE + ["start", "cache"], check=True) + +def restart_api(): + subprocess.run(COMPOSE + ["restart", "api"], check=True) + +def wait_ready(deadline=60): + end = time.monotonic() + deadline + while time.monotonic() < end: + try: + code, _ = call("GET", "/health/ready") + if code == 200: + return + except Exception: + pass + time.sleep(1) + raise SystemExit("API did not become ready") + +def main(): + username = "cf_" + uuid.uuid4().hex[:10] + password = "cache-fail-pass-1234567890" + code, _ = call("POST", "/api/v1/users", body={ + "username": username, "display_name": "Cache Fail", "password": password, + }) + assert code == 201, code + + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + code, session = call("POST", "/api/v1/auth/sessions", + headers={"Authorization": "Basic " + cred}) + assert code == 201, code + token = session["token"] + + # Токен работает + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}) + assert code == 200, code + + # Останавливаем Valkey + stop_cache() + time.sleep(2) + + # API должно работать в degraded mode + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}) + assert code == 200, f"degraded mode failed: {code}" + print("PASS: API works with Valkey down (degraded mode)") + + # Logout при недоступном Valkey должен вернуть 204 + code, _ = call("DELETE", "/api/v1/auth/sessions/current", + headers={"Authorization": "Bearer " + token}) + assert code == 204, f"logout with cache down returned {code}" + print("PASS: logout with cache down returned 204") + + # Токен немедленно недействителен (проверка через БД) + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}) + assert code == 401, f"revoked token still works with cache down: {code}" + print("PASS: revoked token rejected while cache down") + + # Поднимаем Valkey, рестартуем API — критический регресс-тест + start_cache() + time.sleep(2) + restart_api() + wait_ready() + + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}) + assert code == 401, f"REGRESSION: revoked token resurrected after cache restart: {code}" + print("PASS: revoked token still 401 after cache restart (regression fixed)") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student_tests/test_db_failure.py b/student_tests/test_db_failure.py new file mode 100755 index 00000000..4dfe26d5 --- /dev/null +++ b/student_tests/test_db_failure.py @@ -0,0 +1,127 @@ +"""Проверка отказа PostgreSQL: таймаут запроса и восстановление.""" +from __future__ import annotations +import base64, json, subprocess, sys, time, urllib.error, urllib.request, uuid + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] + + +def call(method, path, headers=None, body=None, timeout=10): + data = None + h = dict(headers or {}) + if body is not None: + data = json.dumps(body).encode() + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + r = urllib.request.urlopen(req, timeout=timeout) + raw = r.read() + return r.code, json.loads(raw) if raw else None + except urllib.error.HTTPError as e: + raw = e.read() + try: + return e.code, json.loads(raw) if raw else None + except Exception: + return e.code, None + + +def stop_db(): + """Останавливает контейнер Postgres. TCP-соединения рвутся, psycopg + получает ошибку мгновенно — это честный сценарий 'БД упала'.""" + subprocess.run(COMPOSE + ["stop", "postgres"], check=True) + + +def start_db(): + """Поднимает Postgres. Не падает, если уже запущен.""" + subprocess.run(COMPOSE + ["start", "postgres"], + capture_output=True, check=False) + time.sleep(3) + + +def restart_api(): + """Пересоздаёт API, чтобы пул соединений не держал мёртвые сессии.""" + subprocess.run(COMPOSE + ["restart", "api"], + capture_output=True, check=False) + + +def wait_ready(deadline=60): + end = time.monotonic() + deadline + while time.monotonic() < end: + try: + code, _ = call("GET", "/health/ready", timeout=5) + if code == 200: + return + except Exception: + pass + time.sleep(1) + raise SystemExit("API did not become ready") + + +def ensure_clean_state(): + """Возвращает стек в рабочее состояние, если предыдущий запуск оставил + Postgres остановленным или пул API в плохом состоянии.""" + subprocess.run(COMPOSE + ["start", "postgres"], + capture_output=True, check=False) + time.sleep(3) + # API перезапускаем, потому что после stop Postgres пул мог забиться + # мёртвыми соединениями, и первый запрос упадёт по PoolTimeout. + restart_api() + wait_ready() + + +def main(): + ensure_clean_state() + + # Регистрация и логин — до всякого stop, чтобы тест не падал на подготовке + username = "dbf_" + uuid.uuid4().hex[:10] + password = "db-fail-pass-1234567890" + + code, _ = call("POST", "/api/v1/users", body={ + "username": username, "display_name": "DB Fail", "password": password, + }) + assert code == 201, f"failed to create user before stop: {code}" + + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + code, session = call("POST", "/api/v1/auth/sessions", + headers={"Authorization": "Basic " + cred}) + assert code == 201, f"failed to login before stop: {code}" + token = session["token"] + + stop_db() + try: + time.sleep(0.5) + + # Запрос к БД должен упасть быстро (TCP порван, psycopg сразу + # получает ошибку соединения). Проверяем < 8 секунд. + start = time.monotonic() + try: + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}, timeout=15) + except Exception as exc: + elapsed = time.monotonic() - start + raise SystemExit(f"request hung: {elapsed:.1f}s, {type(exc).__name__}") + elapsed = time.monotonic() - start + assert elapsed < 8, f"request took {elapsed:.1f}s (>8s), DB down not handled fast enough" + assert code in (500, 503), f"expected 500/503 during DB down, got {code}" + print(f"PASS: DB down returned {code} in {elapsed:.1f}s (<8s)") + + # health/ready должен вернуть 503, пока БД недоступна + code, _ = call("GET", "/health/ready", timeout=15) + assert code == 503, f"expected 503 from ready, got {code}" + print("PASS: /health/ready returned 503 during DB down") + finally: + # Гарантированно поднимаем Postgres даже при падении выше. + start_db() + # Перезапускаем API, чтобы пул пересоздал соединения с вернувшейся БД. + restart_api() + wait_ready() + + # Проверяем, что данные и сессия сохранены после восстановления. + code, _ = call("GET", "/api/v1/users/me", + headers={"Authorization": "Bearer " + token}) + assert code == 200, f"session lost after DB recovery: {code}" + print("PASS: session and data preserved after DB recovery") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student_tests/test_expiry.py b/student_tests/test_expiry.py new file mode 100644 index 00000000..68d553c1 --- /dev/null +++ b/student_tests/test_expiry.py @@ -0,0 +1,72 @@ +"""Тест expiry: сессия истекает через SESSION_TTL_SECONDS. + +Запуск: + SESSION_TTL_SECONDS=2 docker compose up -d --build api + python3 test_expiry.py --base-url http://localhost:8080 + +Тест создаёт пользователя, логинится, ждёт TTL+1 секунду, проверяет 401. +""" +from __future__ import annotations + +import base64 +import json +import sys +import time +import urllib.error +import urllib.request +import uuid + +BASE = sys.argv[sys.argv.index("--base-url") + 1] if "--base-url" in sys.argv else "http://localhost:8080" + + +def call(method: str, path: str, headers: dict | None = None, body: dict | None = None) -> tuple[int, dict | None]: + data = None + h = dict(headers or {}) + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=10) + raw = resp.read() + return resp.code, json.loads(raw) if raw else None + except urllib.error.HTTPError as e: + raw = e.read() + try: + return e.code, json.loads(raw) if raw else None + except Exception: + return e.code, None + + +def main() -> None: + username = "exp_" + uuid.uuid4().hex[:12] + password = "expiry-pass-1234567890" + + # Регистрация + code, user = call("POST", "/api/v1/users", body={ + "username": username, "display_name": "Expiry Test", "password": password, + }) + assert code == 201, f"register failed: {code} {user}" + + # Логин + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + code, session = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}) + assert code == 201, f"login failed: {code} {session}" + token = session["token"] + print(f"session expires_at: {session['expires_at']}") + + # Сразу после логина — токен работает + code, me = call("GET", "/api/v1/users/me", headers={"Authorization": "Bearer " + token}) + assert code == 200, f"token should work immediately, got {code}" + print("immediately after login: 200 OK") + + # Ждём истечения + time.sleep(3) + code, err = call("GET", "/api/v1/users/me", headers={"Authorization": "Bearer " + token}) + assert code == 401, f"expired token must give 401, got {code}" + print(f"after TTL: {code} (expected 401)") + print("PASS: expiry — token invalidated after SESSION_TTL_SECONDS") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student_tests/test_parallel_idempotency.py b/student_tests/test_parallel_idempotency.py new file mode 100644 index 00000000..9b371151 --- /dev/null +++ b/student_tests/test_parallel_idempotency.py @@ -0,0 +1,106 @@ +"""Параллельная идемпотентность: N потоков посылают один POST с одним ключом. + +Ожидаем: ровно одно сообщение создано, все ответы — одинаковые, +дубликатов в истории нет. +""" +from __future__ import annotations + +import base64 +import json +import sys +import threading +import urllib.error +import urllib.request +import uuid +from concurrent.futures import ThreadPoolExecutor + +BASE = sys.argv[sys.argv.index("--base-url") + 1] if "--base-url" in sys.argv else "http://localhost:8080" +THREADS = 10 + + +def call(method, path, headers=None, body=None): + data = None + h = dict(headers or {}) + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=15) + raw = resp.read() + return resp.status, json.loads(raw) if raw else None, dict(resp.headers) + except urllib.error.HTTPError as e: + raw = e.read() + try: + return e.code, json.loads(raw) if raw else None, dict(e.headers) + except Exception: + return e.code, None, dict(e.headers) + + +def main(): + username = "pario_" + uuid.uuid4().hex[:10] + password = "parallel-pass-1234567890" + + # Регистрация + code, user, _ = call("POST", "/api/v1/users", body={ + "username": username, "display_name": "Parallel", "password": password, + }) + assert code == 201, f"register failed: {code} {user}" + user_id = user["id"] + + # Логин + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + code, session, _ = call("POST", "/api/v1/auth/sessions", + headers={"Authorization": "Basic " + cred}) + assert code == 201, f"login failed: {code}" + token = session["token"] + + # Чат + code, chat, _ = call("POST", "/api/v1/chats", + headers={"Authorization": "Bearer " + token}, + body={"title": "Parallel test", "member_ids": [user_id]}) + assert code == 201, f"chat failed: {code}" + chat_id = chat["id"] + + # Параллельный POST с одним ключом + idem_key = "parallel-" + uuid.uuid4().hex + barrier = threading.Barrier(THREADS) + results = [] + results_lock = threading.Lock() + + def worker(i): + barrier.wait() # стартуют одновременно + code, body, headers = call( + "POST", f"/api/v1/chats/{chat_id}/messages", + headers={ + "Authorization": "Bearer " + token, + "Idempotency-Key": idem_key, + }, + body={"text": f"parallel-message-{i}"}, # ← ВАЖНО: разные тексты! + ) + with results_lock: + results.append((code, body)) + + with ThreadPoolExecutor(max_workers=THREADS) as pool: + list(pool.map(worker, range(THREADS))) + + # Анализ + print(f"Results: {len(results)} responses") + codes = {} + for code, _ in results: + codes[code] = codes.get(code, 0) + 1 + print(f"Status codes: {codes}") + + # Проверка: ровно одно сообщение создано + code, page, _ = call("GET", f"/api/v1/chats/{chat_id}/messages?limit=100", + headers={"Authorization": "Bearer " + token}) + assert code == 200, f"history failed: {code}" + messages = page["items"] + print(f"Messages in history: {len(messages)}") + + assert len(messages) == 1, f"expected exactly 1 message, got {len(messages)}" + print("PASS: parallel idempotency — exactly one message created") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student_tests/test_races.py b/student_tests/test_races.py new file mode 100644 index 00000000..6087556a --- /dev/null +++ b/student_tests/test_races.py @@ -0,0 +1,116 @@ +"""Тест гонки регистрации: два параллельных POST /users с одним username. + +Ожидаем: один 201, второй 409, в БД ровно один пользователь. +Гонка гарантируется через threading.Barrier — оба запроса стартуют одновременно. +""" +from __future__ import annotations + +import json +import sys +import threading +import urllib.error +import urllib.request +import uuid +from concurrent.futures import ThreadPoolExecutor + +BASE = sys.argv[sys.argv.index("--base-url") + 1] if "--base-url" in sys.argv else "http://localhost:8080" + + +def post_user(username: str, display_name: str, password: str) -> int: + """Отправляет POST /users, возвращает HTTP-код.""" + body = json.dumps( + {"username": username, "display_name": display_name, "password": password}, + ensure_ascii=False, + ).encode("utf-8") + req = urllib.request.Request( + BASE + "/api/v1/users", + data=body, + headers={"Content-Type": "application/json"}, + method="POST", + ) + try: + resp = urllib.request.urlopen(req, timeout=10) + return resp.code + except urllib.error.HTTPError as e: + return e.code + + +def main() -> None: + username = "race_" + uuid.uuid4().hex[:12] + password = "race-pass-1234567890" + barrier = threading.Barrier(2) + + def worker(display_name: str) -> int: + barrier.wait() # старт одновременно + return post_user(username, display_name, password) + + with ThreadPoolExecutor(max_workers=2) as pool: + results = list(pool.map(worker, ["Первый", "Второй"])) + + results.sort() + print(f"HTTP codes: {results}") + + assert results == [201, 409], f"expected [201, 409], got {results}" + print(f"PASS: race — one 201, one 409 for username={username}") + +def test_chat_rollback() -> None: + """Попытка создать чат с несуществующим участником → 400, чат не создан.""" + import base64 + + # Создаём пользователя + username = "rollback_" + uuid.uuid4().hex[:8] + password = "rollback-pass-1234567890" + code = post_user(username, "Rollback", password) + assert code == 201, f"failed to create user: {code}" + + # Логинимся + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + req = urllib.request.Request( + BASE + "/api/v1/auth/sessions", + headers={"Authorization": "Basic " + cred}, + method="POST", + ) + resp = urllib.request.urlopen(req) + session = json.loads(resp.read()) + token = session["token"] + user_id = session["user"]["id"] + + # Пытаемся создать чат с несуществующим UUID + fake_user = "00000000-0000-4000-8000-000000000000" + body = json.dumps( + {"title": "Bad chat", "member_ids": [user_id, fake_user]}, + ensure_ascii=False, + ).encode("utf-8") + req = urllib.request.Request( + BASE + "/api/v1/chats", + data=body, + headers={ + "Content-Type": "application/json", + "Authorization": "Bearer " + token, + }, + method="POST", + ) + try: + urllib.request.urlopen(req, timeout=10) + code = 200 + except urllib.error.HTTPError as e: + code = e.code + + assert code == 400, f"expected 400 for unknown member, got {code}" + + # Проверяем, что чат не создан — list chats не должен его содержать + req = urllib.request.Request( + BASE + "/api/v1/chats?limit=100", + headers={"Authorization": "Bearer " + token}, + ) + resp = urllib.request.urlopen(req) + page = json.loads(resp.read()) + titles = [c["title"] for c in page["items"]] + assert "Bad chat" not in titles, f"chat was created despite failure: {titles}" + + print("PASS: rollback — chat not created when member is unknown") + + +if __name__ == "__main__": + main() + test_chat_rollback() \ No newline at end of file -- 2.54.0 From 11bdc0bf626a77cc2f647bb1a80db078552c27f1 Mon Sep 17 00:00:00 2001 From: ViktoriaKropivka Date: Sat, 10 Oct 2026 08:56:12 +0300 Subject: [PATCH 2/2] =?UTF-8?q?lab3:=20fix=20reviewer=20feedback=20?= =?UTF-8?q?=E2=80=94=20idempotency=20atomicity,=20UUID=20validation,=20err?= =?UTF-8?q?or=20headers,=20instance-id,=20migrate=20limits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 30 +++++ compose.yaml | 6 +- src/errors.py | 35 +++-- src/main.py | 26 +++- src/store.py | 147 +++++++++------------ student_tests/test_parallel_idempotency.py | 11 +- 6 files changed, 149 insertions(+), 106 deletions(-) diff --git a/REPORT.md b/REPORT.md index eb56026a..fc7c0c1c 100644 --- a/REPORT.md +++ b/REPORT.md @@ -184,6 +184,36 @@ PASS: parallel idempotency — exactly one message created | Параллельные POST с одним ключом | Одно сообщение | 1 сообщение в истории | — | | Rolling update под нагрузкой | ≤ нескольких % ошибок | 0.28% (9 из 3206) | мгновенно | +## Исправления по замечаниям ревью + +### Уровень 3 + +1. **Небезопасный повтор после обрыва POST.** Ключ резервировался со `status=0` до создания сообщения — при падении ключ залипал. Исправлено: `INSERT idempotency_keys` и `INSERT messages` в **одной транзакции**, `response_status=201` сразу. При падении — ROLLBACK откатывает всё. + +2. **Конкурентная идемпотентность.** В `except UniqueViolation` возвращался `409` вместо перечитывания. Исправлено: retry loop (5 попыток по 50 мс). При одинаковых запросах все потоки получают **тот же 201** и **то же тело**. + + Проверка: `test_parallel_idempotency.py`, 10 потоков → `Status codes: {201: 10}`, `Unique response bodies: 1`, `Messages in history: 1`. + +3. **ACL при повторе.** Проверка ключа шла до проверки доступа → посторонний видел `409`. Исправлено: проверка `chat_members` **первой**; посторонний всегда получает `404`. + +4. **`X-Instance-Id: unknown`.** В `compose.yaml` было `INSTANCE_ID: ${HOSTNAME:-unknown}` — переменная WSL, не контейнера. Исправлено: строка удалена, `src/instance.py` использует `socket.gethostname()` — у каждой реплики уникальный ID. + +5. **Ошибки контракта.** + - Неверный UUID → `400`: функция `valid_uuid` в `main.py`, применяется в роутах с `{chat_id}`. + - 500 без заголовков: все обработчики ошибок добавляют `X-Request-Id` и `X-Instance-Id` через `_with_headers`. + - Readiness с неправильным `request_id`: берётся из `_rid(request)`, совпадает с заголовком. + +### Уровень 4 + +1. **Конкурентная идемпотентность** — см. п. 2 выше. +2. **Migrate без лимитов** — добавлен `deploy.resources.limits` (cpus: 0.5, memory: 256M). +3. **Healthcheck в отчёте.** Синхронизировано: + - `postgres` — `pg_isready` каждые 5s. + - `cache` — `valkey-cli ping` каждые 5s. + - `api` — `urllib /health/live` каждые 5s, `start_period: 15s`. + - `migrate` — healthcheck **нет** (разовый сервис). + - `lb` — healthcheck **нет** в Compose; HAProxy проверяет бэкенды через `option httpchk GET /health/ready` (inter 2s fall 2 rise 2) в `haproxy.cfg`. + ## Ограничения и следующие шаги - **SPOF:** LB (HAProxy) и Postgres — единственные экземпляры. Падение LB → сервис недоступен. Падение Postgres → 503. Это **осознанное** ограничение lab3: HA для БД и LB — тема lab5+ или трека «Отказ узла». diff --git a/compose.yaml b/compose.yaml index 37eeabb7..6327c991 100644 --- a/compose.yaml +++ b/compose.yaml @@ -48,6 +48,11 @@ services: condition: service_healthy environment: DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger} + deploy: + resources: + limits: + cpus: "0.5" + memory: 256M restart: "no" api: @@ -67,7 +72,6 @@ services: SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-changeme}@postgres:5432/${POSTGRES_DB:-messenger} CACHE_URL: redis://cache:6379/0 - INSTANCE_ID: ${HOSTNAME:-unknown} deploy: replicas: 2 resources: diff --git a/src/errors.py b/src/errors.py index 098bce17..8cb1ab7b 100755 --- a/src/errors.py +++ b/src/errors.py @@ -7,9 +7,8 @@ from fastapi import Request from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware -from .store import ApiError - from .instance import INSTANCE_ID +from .store import ApiError REQUEST_ID_HEADER = "X-Request-Id" @@ -33,13 +32,11 @@ def _rid(request: Request) -> str: return getattr(request.state, "request_id", None) or uuid.uuid4().hex -async def api_error_handler(request: Request, exc: Exception) -> JSONResponse: - assert isinstance(exc, ApiError) - response = JSONResponse( - status_code=exc.status, - content=_envelope(exc.code, exc.message, _rid(request)), - ) - if exc.status == 401: +def _with_headers(response: JSONResponse, request: Request, status: int) -> JSONResponse: + rid = _rid(request) + response.headers[REQUEST_ID_HEADER] = rid + response.headers["X-Instance-Id"] = INSTANCE_ID + if status == 401: scheme = "Bearer" if request.url.path == "/api/v1/auth/sessions": scheme = "Basic" @@ -47,22 +44,34 @@ async def api_error_handler(request: Request, exc: Exception) -> JSONResponse: return response +async def api_error_handler(request: Request, exc: Exception) -> JSONResponse: + assert isinstance(exc, ApiError) + response = JSONResponse( + status_code=exc.status, + content=_envelope(exc.code, exc.message, _rid(request)), + ) + return _with_headers(response, request, exc.status) + + async def validation_handler(request: Request, exc: Exception) -> JSONResponse: - return JSONResponse( + response = JSONResponse( status_code=400, content=_envelope("invalid_request", "Request validation failed", _rid(request)), ) + return _with_headers(response, request, 400) async def json_decode_handler(request: Request, exc: Exception) -> JSONResponse: - return JSONResponse( + response = JSONResponse( status_code=400, content=_envelope("invalid_request", "Malformed JSON", _rid(request)), ) + return _with_headers(response, request, 400) async def unhandled_handler(request: Request, exc: Exception) -> JSONResponse: - return JSONResponse( + response = JSONResponse( status_code=500, content=_envelope("internal_error", "Internal error", _rid(request)), - ) \ No newline at end of file + ) + return _with_headers(response, request, 500) \ No newline at end of file diff --git a/src/main.py b/src/main.py index e05782f2..49bb271e 100755 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,8 @@ import os import re from contextlib import asynccontextmanager from typing import Optional -import uuid +import uuid +from uuid import UUID from fastapi import Depends, FastAPI, Header, Query, Request, Response from fastapi.exceptions import RequestValidationError @@ -76,11 +77,19 @@ async def _http_exc(request: Request, exc: StarletteHTTPException): content=_envelope(code, str(exc.detail), _rid(request)), ) +# ---------- утилиты валидации ---------- + +def valid_uuid(value: str, name: str = "id") -> str: + """Проверяет, что value — валидный UUID. Иначе бросает BadRequest (400).""" + try: + UUID(value) + except (ValueError, TypeError, AttributeError): + raise BadRequest(f"Invalid {name}") + return value # ---------- зависимости ---------- def _parse_basic(header_value: str) -> tuple[str, str]: - """Разбирает `Basic base64(username:password)`. Бросает Unauthorized.""" import base64 if not header_value.lower().startswith("basic "): raise Unauthorized("Basic authentication required") @@ -125,17 +134,21 @@ def liveness(): @app.get("/health/ready", response_model=Health) -def readiness(): +def readiness(request: Request): try: from .db import transaction with transaction() as conn: with conn.cursor() as cur: cur.execute("SELECT 1") except Exception: - return JSONResponse( + rid = _rid(request) + response = JSONResponse( status_code=503, - content=_envelope("unavailable", "Database unavailable", "readiness"), + content=_envelope("unavailable", "Database unavailable", rid), ) + response.headers["X-Request-Id"] = rid + response.headers["X-Instance-Id"] = INSTANCE_ID + return response return {"status": "ok"} @@ -204,6 +217,7 @@ def list_chats( @app.get("/api/v1/chats/{chat_id}", response_model=Chat) def get_chat(chat_id: str, user_id: str = Depends(current_user_id)): + chat_id = valid_uuid(chat_id, "chat_id") chat = store.get_chat(chat_id, user_id) if chat is None: raise NotFound() @@ -219,6 +233,7 @@ def create_message( user_id: str = Depends(current_user_id), idempotency_key: Optional[str] = Header(default=None, alias="Idempotency-Key"), ): + chat_id = valid_uuid(chat_id, "chat_id") if not idempotency_key: raise BadRequest("Idempotency-Key header is required") if not re.match(r"^[A-Za-z0-9._:-]{1,128}$", idempotency_key): @@ -237,6 +252,7 @@ def list_messages( cursor: Optional[str] = Query(default=None, min_length=1), user_id: str = Depends(current_user_id), ): + chat_id = valid_uuid(chat_id, "chat_id") items, next_cursor = store.list_messages(chat_id, user_id, limit, cursor) return {"items": items, "next_cursor": next_cursor} diff --git a/src/store.py b/src/store.py index 9442d776..e2514ac6 100755 --- a/src/store.py +++ b/src/store.py @@ -270,96 +270,71 @@ class Store: path = f"/api/v1/chats/{chat_id}/messages" method = "POST" - with transaction() as conn: - with conn.cursor() as cur: - cur.execute( - """ - SELECT request_hash, response_status, response_body - FROM idempotency_keys - WHERE user_id = %s AND method = %s AND path = %s AND key = %s - """, - (sender_id, method, path, idempotency_key), - ) - row = cur.fetchone() - if row is not None: - if row["request_hash"] != request_hash: - raise Conflict("idempotency key reused with different body") - return row["response_body"], row["response_status"] + import time as _time + for attempt in range(5): + try: + with transaction() as conn: + with conn.cursor() as cur: + cur.execute( + "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", + (chat_id, sender_id), + ) + if cur.fetchone() is None: + raise NotFound() - try: - with transaction() as conn: - with conn.cursor() as cur: - cur.execute( - """ - INSERT INTO idempotency_keys - (user_id, method, path, key, request_hash, - response_status, response_body, created_at) - VALUES (%s, %s, %s, %s, %s, 0, '{}'::jsonb, %s) - """, - (sender_id, method, path, idempotency_key, request_hash, _now()), - ) - except UniqueViolation: - with transaction() as conn: - with conn.cursor() as cur: - cur.execute( - """ - SELECT request_hash, response_status, response_body - FROM idempotency_keys - WHERE user_id = %s AND method = %s AND path = %s AND key = %s - """, - (sender_id, method, path, idempotency_key), - ) - row = cur.fetchone() - if row is None: - raise Conflict("idempotency key race, please retry") - if row["request_hash"] != request_hash: - raise Conflict("idempotency key reused with different body") - if row["response_status"] == 0: - raise Conflict("idempotency key in progress, please retry") - return row["response_body"], row["response_status"] + cur.execute( + """ + SELECT request_hash, response_status, response_body + FROM idempotency_keys + WHERE user_id = %s AND method = %s AND path = %s AND key = %s + """, + (sender_id, method, path, idempotency_key), + ) + row = cur.fetchone() + if row is not None: + if row["request_hash"] != request_hash: + raise Conflict( + "idempotency key reused with different body" + ) + return row["response_body"], row["response_status"] - with transaction() as conn: - with conn.cursor() as cur: - cur.execute( - "SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s", - (chat_id, sender_id), - ) - if cur.fetchone() is None: - cur.execute( - "DELETE FROM idempotency_keys WHERE user_id = %s AND method = %s AND path = %s AND key = %s", - (sender_id, method, path, idempotency_key), - ) - raise NotFound() + mid = str(uuid.uuid4()) + now = _now() + cur.execute( + """ + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES (%s, %s, %s, %s, %s) + """, + (mid, chat_id, sender_id, text, now), + ) + message = { + "id": mid, + "chat_id": chat_id, + "sender_id": sender_id, + "text": text, + "created_at": iso(now), + } - mid = str(uuid.uuid4()) - now = _now() - cur.execute( - """ - INSERT INTO messages (id, chat_id, sender_id, text, created_at) - VALUES (%s, %s, %s, %s, %s) - """, - (mid, chat_id, sender_id, text, now), - ) + cur.execute( + """ + INSERT INTO idempotency_keys + (user_id, method, path, key, request_hash, + response_status, response_body, created_at) + VALUES (%s, %s, %s, %s, %s, 201, %s, %s) + """, + ( + sender_id, method, path, idempotency_key, + request_hash, + _json.dumps(message, ensure_ascii=False), + now, + ), + ) + return message, 201 + except UniqueViolation: + _time.sleep(0.05) + continue - message = { - "id": mid, - "chat_id": chat_id, - "sender_id": sender_id, - "text": text, - "created_at": iso(now), - } - - cur.execute( - """ - UPDATE idempotency_keys - SET response_status = 201, response_body = %s - WHERE user_id = %s AND method = %s AND path = %s AND key = %s - """, - (_json.dumps(message, ensure_ascii=False), - sender_id, method, path, idempotency_key), - ) - - return message, 201 + raise Conflict("idempotency key in progress, please retry") def list_messages( self, diff --git a/student_tests/test_parallel_idempotency.py b/student_tests/test_parallel_idempotency.py index 9b371151..0a5b714c 100644 --- a/student_tests/test_parallel_idempotency.py +++ b/student_tests/test_parallel_idempotency.py @@ -76,7 +76,7 @@ def main(): "Authorization": "Bearer " + token, "Idempotency-Key": idem_key, }, - body={"text": f"parallel-message-{i}"}, # ← ВАЖНО: разные тексты! + body={"text": "parallel-message-same"}, # ← ОДИНАКОВЫЙ текст! ) with results_lock: results.append((code, body)) @@ -101,6 +101,15 @@ def main(): assert len(messages) == 1, f"expected exactly 1 message, got {len(messages)}" print("PASS: parallel idempotency — exactly one message created") + # Проверка: все ответы должны быть одинаковыми + codes = [c for c, _ in results] + bodies = [json.dumps(b, sort_keys=True) for _, b in results if b is not None] + unique_bodies = set(bodies) + print(f"Status codes: {dict((c, codes.count(c)) for c in set(codes))}") + print(f"Unique response bodies: {len(unique_bodies)}") + assert codes.count(201) == THREADS, f"expected all {THREADS} to be 201, got {codes.count(201)}" + assert len(unique_bodies) == 1, f"expected same response body, got {len(unique_bodies)}" + if __name__ == "__main__": main() \ No newline at end of file -- 2.54.0