diff --git a/.dockerignore b/.dockerignore index bc16e556..447644cb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,4 @@ __pycache__ node_modules evidence .DS_Store +nginx.conf diff --git a/.env b/.env new file mode 100644 index 00000000..9aeacf14 --- /dev/null +++ b/.env @@ -0,0 +1,14 @@ +APP_HOST=0.0.0.0 +APP_PORT=8080 +APP_ENV=lab +SESSION_TTL_SECONDS=3600 +DATABASE_URL=postgresql://messenger:messenger@postgres:5432/messenger +CACHE_URL=redis://valkey:6379/0 +SESSION_STORE=redis + +MINIO_ROOT_USER=minioadmin +MINIO_ROOT_PASSWORD=minioadmin + +RABBITMQ_URL=amqp://messenger:messenger@rabbitmq:5672/ +RABBITMQ_QUEUE=attachment.process.v1 +MAX_ATTEMPTS=5 \ No newline at end of file diff --git a/.env.example b/.env.example deleted file mode 100644 index 119384b5..00000000 --- a/.env.example +++ /dev/null @@ -1,25 +0,0 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. -APP_HOST=0.0.0.0 -APP_PORT=8080 -APP_ENV=lab -SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url -API_REPLICAS=2 -IDEMPOTENCY_TTL_SECONDS=86400 -S3_ENDPOINT=http://s3:9000 -S3_REGION=us-east-1 -S3_BUCKET=messenger-attachments -S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id -S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key -MAX_UPLOAD_BYTES=10485760 -MAX_IMAGE_PIXELS=40000000 -BROKER_URL_FILE=/run/secrets/broker_url -WORKER_CONCURRENCY=2 -JOB_MAX_ATTEMPTS=5 -JOB_TIMEOUT_SECONDS=60 -STAGING_RETENTION_SECONDS=86400 diff --git a/.gitignore b/.gitignore index b5a71896..5dce9b63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Local/private state .env -.env.* -!.env.example +.env +!.env secrets/ certs/private/ *.key diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..9f10bccc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM python:3.12-slim AS builder + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir --user -r requirements.txt + +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PATH=/home/appuser/.local/bin:$PATH + +RUN useradd -m -u 1000 appuser + +WORKDIR /app + +COPY --from=builder /root/.local /home/appuser/.local + +COPY ./app ./app +COPY ./migrations ./migrations + +RUN chown -R appuser:appuser /app + +USER appuser + +EXPOSE 8080 + +CMD ["sh", "-c", "export INSTANCE_ID=$(python -c 'import secrets; print(secrets.token_hex(6))') && echo \"[startup] INSTANCE_ID=$INSTANCE_ID\" && exec python -m uvicorn app.main:app --host 0.0.0.0 --port 8080"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index e98f75e9..da06c706 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,170 @@ # Отчёт — лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Кузнецова Марина, БВТ2304 +- Целевая оценка: 5, Трек 1 — очередь под перегрузкой +- Commit/PR: пока пусто +- Использованные источники и помощь ИИ: официальная документация FastAPI, asyncpg, aio-pika, MinIO, Pillow, RabbitMQ reliability/confirms, Docker Compose. ИИ применялся для пояснения требований, обсуждения архитектурных решений и проверки корректности реализации. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требования: Python 3.13.15, Docker 26.1.1, git. + + cp .env.example .env + docker compose --env-file .env up -d --build + +Готовность: `{"status":"ok"}` на `http://localhost:8080/health/ready`. В логах `migrate` — `migrations applied`, `Exited (0)`. В логах `minio-init` — `bucket ready`, `Exited (0)`. Реплики `api-1`, `api-2` — `healthy`. `rabbitmq`, `worker`, `outbox`, `lease_checker` — `running`. + +Проверки: + + python -X utf8 tests/smoke.py --base-url http://localhost:8080 + python -X utf8 -m pytest student-tests/test_worker_crash.py -v + +Сценарий защиты уровня 3: + + docker compose --env-file .env stop worker + # upload sample.png → 202 queued + # message с attachment_ids → 201 + # GET /content → 409; обычное сообщение → 201 + docker compose --env-file .env restart api outbox rabbitmq + docker compose --env-file .env start worker + # GET /attachments/{id} → status=ready + docker compose --env-file .env restart rabbitmq + # GET /attachments/{id} → status=ready (задача не потеряна) + +Трек 1: + + python student-tests/queue_benchmark.py --prefetch 1 --size small --files 50 --output evidence/queue-prefetch1-small.json + python student-tests/queue_benchmark.py --prefetch 5 --size small --files 50 --output evidence/queue-prefetch5-small.json + python student-tests/queue_benchmark.py --prefetch 20 --size small --files 50 --output evidence/queue-prefetch20-small.json + python student-tests/queue_benchmark.py --prefetch 1 --size medium --files 30 --output evidence/queue-prefetch1-medium.json + python student-tests/queue_benchmark.py --prefetch 5 --size medium --files 30 --output evidence/queue-prefetch5-medium.json + python student-tests/queue_benchmark.py --prefetch 20 --size medium --files 30 --output evidence/queue-prefetch20-medium.json + python student-tests/queue_benchmark.py --prefetch 1 --size large --files 15 --output evidence/queue-prefetch1-large.json + python student-tests/queue_benchmark.py --prefetch 5 --size large --files 15 --output evidence/queue-prefetch5-large.json + python student-tests/queue_benchmark.py --prefetch 20 --size large --files 15 --output evidence/queue-prefetch20-large.json + python student-tests/queue_recovery.py --files 100 --output evidence/queue-recovery.json + +Остановка: + +- `docker compose down` — без потери данных. +- `docker compose down -v` — удаление volumes. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. + Client → HTTP (Nginx :8080) → api-1, api-2 (FastAPI/Uvicorn) + ↓ + PostgreSQL (users/chats/messages/idempotency_keys/attachments/jobs/outbox) + Valkey (sessions + cache) + MinIO (bucket attachments, приватный; staging/ + originals/ + варианты) + RabbitMQ (durable queue attachment.process.v1, dlq) + worker, outbox_relay, lease_checker (отдельные процессы) -## Доказательства критериев +Минимум lab4 сохранён: две реплики API за Nginx, PostgreSQL и Valkey, идемпотентность, failover, persistence, приватный S3, вложения. В lab5 добавлены RabbitMQ, worker, outbox, lease_checker, состояния вложений, асинхронная обработка изображений. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Приём файла. `POST /chats/{chat_id}/attachments?filename=` принимает сырые bytes, проверяет членство, filename, лимиты (10 MiB, 40 Мпикс), MIME по содержимому, пишет staging в MinIO. В одной транзакции создаёт `attachments` (status=queued), `jobs`, `outbox`, затем возвращает `202 queued`. Остановка API после ответа работу не теряет. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Публикация и обработка. `outbox_relay` публикует job в durable очередь RabbitMQ, помечает `published_at`. `worker` берёт job (prefetch из env), переводит `queued → processing`, ставит `lease_until`, пишет оригинал и варианты, обновляет `attachments.variants`/`variant_keys`, ставит `ready`, удаляет staging. Ack — только после устойчивого эффекта. `lease_checker` возвращает зависшие `processing` в `queued`. -## Эксперименты и отказы +Варианты. `thumbnail` JPEG q85, fit 256×256, EXIF transpose, alpha→белый; `crop` JPEG q85, центр 128×128; `optimized` WebP q80, вписан 1280×1280 без увеличения. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Ошибки. Единый формат `{"error": {"code", "message", "request_id"}}`. Недоступность PostgreSQL, Valkey, MinIO, RabbitMQ → 503. Пользовательские ошибки (400/413/415/404/409) не превращаются в 500. Постоянные ошибки worker (`UnidentifiedImageError`, битые/truncated) → `failed invalid_image` + DLQ без retry. Временные → backoff с jitter до `MAX_ATTEMPTS=5`, затем `failed retry_exhausted` + DLQ. + +Replay. `POST /attachments/{id}/replay` — только для `failed`; `ready` → 409 `already_ready`, `queued/processing` → 409 `not_failed`. Внутри `FOR UPDATE`, новый job+outbox, `attempt=1`. + +## Доказательства критериев «3» + +| Критерий | Команда | Результат | +|---|---|---| +| Возможности lab4 сохранены | `docker compose ps` | api-1, api-2 healthy; postgres, valkey, minio, rabbitmq healthy; worker, outbox, lease_checker Up; lb Up | +| Upload → 202 queued | сценарий защиты, шаг 4 | `upload=202`, `status=queued` | +| 202 только после durable staging + job | код `app/api/attachments.py` | staging в S3 → `create_attachment_with_job_and_outbox` (одна транзакция) → 202 | +| broker и worker отдельно, durable, at-least-once | `compose.yaml`, `app/broker.py` | сервисы `rabbitmq`, `worker`; `durable=True`, `DeliveryMode.PERSISTENT`, `ack` после handler | +| queued/processing/ready/failed | `013_jobs.sql`, `app/worker.py`, `app/api/attachments.py` | `att_status=queued` в сценарии, `final_status=ready` | +| thumbnail 256×160, оригинал неизменен | сценарий защиты, шаги 9–11 | `thumbnail 256×160 image/jpeg`; SHA-256 оригинала = SHA-256 fixture `99EA…BD75` | +| Ограниченный retry, финальный failed с error_code | `MAX_ATTEMPTS=5`, `app/worker.py` | `fail_attachment(...,"retry_exhausted")` + DLQ | +| Повтор не создаёт второй набор вариантов | `app/worker.py` | ключи `thumbnails/{chat}/{id}.jpg` детерминированы; `if att["status"]=="ready": return` | +| Остановка worker → content 409, остальное работает | сценарий защиты, шаги 4–7 | `content_status=409`, `other_message_id` получен | +| Рестарт broker с volume сохраняет работу | `docker compose restart rabbitmq` + GET | `status=ready` после рестарта | +| Публичный smoke | `python -X utf8 tests/smoke.py --base-url http://localhost:8080` | `PASS: lab5 public API scenario` | + + +## Доказательства критериев «4» + +| Критерий | Команда | Результат | +|---|---|---| +| crop 128×128 и optimized WebP | сценарий защиты | `crop 128×128 image/jpeg`, `optimized 320×200 image/webp` | +| Параметры качества/удаления EXIF описаны | `app/worker.py` | thumbnail JPEG q85, fit 256×256, EXIF transpose, alpha→белый; crop JPEG q85, центр 128×128; optimized WebP q80, вписан 1280×1280 без увеличения | +| Невалидные/огромные изображения ограничены | `app/api/attachments.py` | `MAX_BYTES=10 MiB`, `MAX_PIXELS=40M`; worker обрабатывает только провалидированные | +| Транзакция attachment + job + outbox | `storage.create_attachment_with_job_and_outbox` | одна транзакция; разрыв COMMIT↔publish закрыт | +| Различение постоянных/временных ошибок | `app/worker.py`, `is_permanent` | битые → `failed invalid_image` без retry; временные → backoff+jitter до `MAX_ATTEMPTS`, затем `retry_exhausted` + DLQ | +| Replay failed | `POST /attachments/{id}/replay` | ready → 409; failed → 202 и ready; `evidence/replay.txt` | +| Worker crash до ack | `python -m pytest student-tests/test_worker_crash.py -v` | `1 passed`; variants совпадают; `evidence/worker-crash.txt` | +| Timeout/lease для зависшего processing | `app/lease_checker.py` | `get_expired_jobs`, `reset_job_status` | +| Ограниченная конкуренция/prefetch | `app/broker.py` | `PREFETCH_COUNT` из env | +| Очистка staging после безопасной фиксации | `app/worker.py` | `s3.remove(staging_key)` после `complete_attachment_ready` | +| DLQ/реестр failed | `app/worker.py` | `publish_job(job, is_dlq=True)` + `attachments.status='failed'` | + +Replay: `att_id=559d9cab-90a0-4baf-b36f-355ad93ffae2`, `chat_id=ce16016a-c647-4d87-8697-1061f25ff47c`, `POST /replay` на ready → 409, после `failed/simulated_failure` → 202 → ready. + +Worker crash: файл готов → удалили thumbnail из S3 → `attachments.status='failed'`, `error_code='ack_lost'` → `POST /replay` → 202 → ready; `variants` совпадают. + +## Трек 1: очередь под перегрузкой + +Гипотеза. Увеличение `PREFETCH_COUNT` повышает throughput на малых изображениях, но на больших (4000×3000) выигрыш ограничен CPU и памятью worker; при перегрузке queue lag растёт, потому что API не делает backpressure. + +Методика. Docker Desktop (Compose v2) на Windows. Лимиты: worker 1 CPU / 512 MiB, API 0.5 CPU / 256 MiB, RabbitMQ 0.5 CPU / 256 MiB. Размеры: small 320×200, medium 1280×800, large 4000×3000 PNG. Prefetch: 1, 5, 20. Файлов на прогон: small 50, medium 30, large 15. Метрики: throughput, queue lag (`t_processing - t_upload`, p50/p95/p99), time-to-ready (`t_ready - t_upload`, p50/p95/p99), RSS worker. Скрипт: `student-tests/queue_benchmark.py`, вывод в `evidence/queue-prefetch{P}-{size}.json`. Recovery: `student-tests/queue_recovery.py`, 100 medium при остановленном worker, вывод в `evidence/queue-recovery.json`. + +Результаты: + +| prefetch | size | files ok | throughput files/s | queue lag p95, s | time-to-ready p95, s | worker RSS, MiB | +|---|---|---|---|---|---|---| +| 1 | small | 50/50 | 5.56 | 5.79 | 5.82 | 37.9 | +| 5 | small | 50/50 | 5.98 | 5.04 | 5.21 | 43.8 | +| 20 | small | 50/50 | 5.84 | 5.35 | 5.39 | 42.5 | +| 1 | medium | 30/30 | 5.02 | 3.04 | 3.08 | 50.6 | +| 5 | medium | 30/30 | 4.76 | 3.30 | 3.52 | 114.0 | +| 20 | medium | 30/30 | 4.89 | 2.95 | 3.43 | 112.6 | +| 1 | large | 15/15 | 1.36 | 7.28 | 7.93 | 97.5 | +| 5 | large | 15/15 | 0.83 | 11.22 | 14.84 | 511.7 | +| 20 | large | 0/15 (таймаут скрипта) | — | — | — | — | + +Наблюдения. На small оптимум prefetch=5: throughput 5.98 против 5.56 (prefetch=1) и 5.84 (prefetch=20), queue lag и time-to-ready минимальны (5.04 / 5.21 с). На medium prefetch=1 даёт лучший throughput (5.02) и минимальный RSS (50.6 MiB); prefetch=5 и 20 дают RSS 112–114 MiB без выигрыша. На large prefetch=1 даёт throughput 1.36 files/s и RSS 97.5 MiB; prefetch=5 — throughput 0.83 и RSS 511.7 MiB (почти лимит 512 MiB); prefetch=20 не завершил 15 файлов за 600 с — скрипт зафиксировал `files_ok=0`, по логам worker доработал после таймаута, `OOMKilled: false`. Компромисс: prefetch=1 — минимальный RSS; prefetch=5 — лучший throughput на small; prefetch=20 не даёт выигрыша ни на одном размере. Для этого стенда оптимум — prefetch 1–5 в зависимости от размера. + +Backpressure. Не реализован: API возвращает 202 независимо от глубины очереди. Цена — queue lag растёт при накоплении; ограничение — только `PREFETCH_COUNT` и память worker. + +Восстановление после перегрузки. 100 medium загружены при остановленном worker, очередь копилась, затем worker запущен: 100/100 обработаны за 22.749 с. См. `evidence/queue-recovery.json`. + +Ограничения Трека 1. Closed-loop клиент, один Docker-host, шум от Docker Desktop, prefetch меняется через пересоздание worker (не на лету), large-прогоны короткие (15 файлов), для prefetch=20 large — таймаут скрипта. Числа — сравнение конфигураций на одном стенде, не production SLA. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Что выдерживается: + +- Перезапуск API, broker и worker с сохранением volumes: принятая работа не теряется, задача доходит до ready. +- Остановка worker: accepted-задачи остаются в `queued`, content отдаёт 409, остальные сообщения работают. +- Рестарт RabbitMQ с volume: `status=ready` сохраняется. +- Повторная доставка: набор вариантов не размножается, ключи объектов детерминированы. +- Постоянные ошибки worker: `failed invalid_image` + DLQ, без бесконечного retry. +- Replay failed: `failed → queued → ready`, ready не перезаписывается. +- Недоступность PostgreSQL, Valkey, MinIO или RabbitMQ: `/health/ready` → 503. +- Пользовательские ошибки вложений (400/413/415/404/409) не превращаются в 500. + +Что не выдерживается: + +- Потеря Docker-хоста целиком: все контейнеры на одном узле. +- Потеря узла PostgreSQL: без репликации БД не выживет. +- Потеря узла Nginx: LB — SPOF. +- Потеря узла MinIO: без репликации S3 не выживет. +- Потеря узла RabbitMQ: durable очередь на одном узле, без кластера. +- `outbox_relay` и `lease_checker` в одном экземпляре — SPOF для публикации и восстановления зависших задач. +- Backpressure не реализован: API принимает upload всегда, очередь может расти. + +Учебные упрощения: + +- Все контейнеры на одном Docker-хосте. +- Nginx без активных healthcheck (только пассивные по max_fails). +- HTTP без TLS +- Миграции не поддерживают rollback. +- Ключ эффекта `(attachment_id, pipeline_version, variant)` обеспечен детерминированными ключами объектов, а не отдельной таблицей; повторная доставка перезаписывает те же объекты. +- Worker crash-тест имитирует потерю ack через удаление объекта в S3 и `UPDATE status='failed'`, а не через `SIGKILL` в момент `ack`. \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/__init__.py b/app/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/attachments.py b/app/api/attachments.py new file mode 100644 index 00000000..4992286e --- /dev/null +++ b/app/api/attachments.py @@ -0,0 +1,242 @@ +import hashlib +import re +import uuid +from datetime import datetime, timezone +from io import BytesIO + +from fastapi import APIRouter, Header, Request +from fastapi.responses import JSONResponse, StreamingResponse +from PIL import Image, UnidentifiedImageError + +from app.security import get_current_user + +router = APIRouter(tags=["attachments"]) + +MAX_BYTES = 10 * 1024 * 1024 +MAX_PIXELS = 40_000_000 +ALLOWED = {"PNG": "image/png", "JPEG": "image/jpeg"} +BAD_FILENAME = re.compile(r'[/\\\x00-\x1f]') +BEARER_CHALLENGE = 'Bearer realm="messenger"' +CHUNK = 64 * 1024 + + +def error_response(status, code, message, request_id, headers=None): + return JSONResponse( + status_code=status, + content={"error": {"code": code, "message": message, "request_id": request_id}}, + headers=headers or {}, + ) + + +def validate_image(data: bytes): + if len(data) == 0: + raise ValueError("unsupported_media_type") + if len(data) > MAX_BYTES: + raise ValueError("payload_too_large") + try: + img = Image.open(BytesIO(data)) + img.verify() + img = Image.open(BytesIO(data)) + fmt = img.format + w, h = img.size + except (UnidentifiedImageError, OSError, ValueError): + raise ValueError("unsupported_media_type") + if fmt not in ALLOWED: + raise ValueError("unsupported_media_type") + if w * h > MAX_PIXELS: + raise ValueError("payload_too_large") + return ALLOWED[fmt], w, h, hashlib.sha256(data).hexdigest() + + +def safe_filename(name: str) -> str: + if not name or len(name) > 255: + raise ValueError("invalid_request") + if BAD_FILENAME.search(name): + raise ValueError("invalid_request") + return name + + +async def _auth(request, authorization): + try: + user, _ = await get_current_user(request, authorization) + return user, None + except Exception: + return None, error_response( + 401, "unauthorized", "unauthorized", request.state.request_id, + headers={"WWW-Authenticate": BEARER_CHALLENGE}, + ) + + +async def _read_limited(request, limit): + buf = bytearray() + async for chunk in request.stream(): + buf.extend(chunk) + if len(buf) > limit: + raise ValueError("payload_too_large") + return bytes(buf) + + +@router.post("/chats/{chat_id}/attachments", status_code=202) +async def upload_attachment(chat_id: str, filename: str, request: Request, + authorization: str | None = Header(default=None)): + rid = request.state.request_id + try: + uuid.UUID(chat_id) + except (ValueError, TypeError): + return error_response(400, "invalid_request", "invalid uuid", rid) + + user, err = await _auth(request, authorization) + if err is not None: + return err + + if not await request.app.state.storage.is_chat_member(chat_id, user["id"]): + return error_response(404, "not_found", "chat not found", rid) + + try: + safe_filename(filename) + except ValueError: + return error_response(400, "invalid_request", "invalid filename", rid) + + try: + data = await _read_limited(request, MAX_BYTES) + except ValueError: + return error_response(413, "payload_too_large", "payload_too_large", rid) + + try: + content_type, w, h, sha = validate_image(data) + except ValueError as e: + code = str(e) + status = 413 if code == "payload_too_large" else 415 + return error_response(status, code, code, rid) + + att_id = str(uuid.uuid4()) + staging_key = f"staging/{chat_id}/{att_id}" + + try: + request.app.state.s3.put(staging_key, data, content_type) + except Exception: + return error_response(503, "unavailable", "storage unavailable", rid) + + job = { + "schema_version": 1, + "job_id": str(uuid.uuid4()), + "type": "attachment.process.v1", + "attachment_id": att_id, + "pipeline_version": 1, + "attempt": 1, + "created_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"), + } + try: + att = await request.app.state.storage.create_attachment_with_job_and_outbox( + att_id, chat_id, user["id"], staging_key, + filename, content_type, len(data), sha, job, + ) + except Exception: + try: + request.app.state.s3.remove(staging_key) + except Exception: + pass + return error_response(503, "unavailable", "queue unavailable", rid) + + return att + + +@router.get("/attachments/{attachment_id}") +async def get_attachment(attachment_id: str, request: Request, + authorization: str | None = Header(default=None)): + rid = request.state.request_id + try: + uuid.UUID(attachment_id) + except (ValueError, TypeError): + return error_response(400, "invalid_request", "invalid uuid", rid) + user, err = await _auth(request, authorization) + if err is not None: + return err + att = await request.app.state.storage.get_attachment(attachment_id) + if att is None: + return error_response(404, "not_found", "attachment not found", rid) + if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]): + return error_response(404, "not_found", "attachment not found", rid) + att.pop("_object_key", None) + att.pop("_variant_keys", None) + return att + + +@router.post("/attachments/{attachment_id}/replay", status_code=202) +async def replay_attachment(attachment_id: str, request: Request, + authorization: str | None = Header(default=None)): + rid = request.state.request_id + try: + uuid.UUID(attachment_id) + except (ValueError, TypeError): + return error_response(400, "invalid_request", "invalid uuid", rid) + user, err = await _auth(request, authorization) + if err is not None: + return err + att = await request.app.state.storage.get_attachment(attachment_id) + if att is None: + return error_response(404, "not_found", "attachment not found", rid) + if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]): + return error_response(404, "not_found", "attachment not found", rid) + result = await request.app.state.storage.replay_failed_attachment(attachment_id) + if result == "not_found": + return error_response(404, "not_found", "attachment not found", rid) + if result == "already_ready": + return error_response(409, "conflict", "attachment already ready", rid) + if result == "not_failed": + return error_response(409, "conflict", "attachment is not failed", rid) + att = await request.app.state.storage.get_attachment(attachment_id) + att.pop("_object_key", None) + att.pop("_variant_keys", None) + return att + + +@router.get("/attachments/{attachment_id}/content") +async def download_attachment(attachment_id: str, request: Request, + variant: str = "original", + authorization: str | None = Header(default=None)): + rid = request.state.request_id + try: + uuid.UUID(attachment_id) + except (ValueError, TypeError): + return error_response(400, "invalid_request", "invalid uuid", rid) + user, err = await _auth(request, authorization) + if err is not None: + return err + att = await request.app.state.storage.get_attachment(attachment_id) + if att is None: + return error_response(404, "not_found", "attachment not found", rid) + if not await request.app.state.storage.is_chat_member(att["chat_id"], user["id"]): + return error_response(404, "not_found", "attachment not found", rid) + + if att["status"] in ("queued", "processing"): + return error_response(409, "conflict", "attachment not ready", rid) + if att["status"] == "failed": + return error_response(409, "conflict", "attachment failed", rid) + + if variant == "original": + obj_key = att["_object_key"] + content_type = att["content_type"] + size_bytes = att["size_bytes"] + else: + found = next((v for v in att["variants"] if v["name"] == variant), None) + if not found: + return error_response(404, "not_found", "variant not found", rid) + + obj_key = att["_variant_keys"].get(variant) + if not obj_key: + return error_response(404, "not_found", "variant key not found", rid) + + content_type = found["content_type"] + size_bytes = found["size_bytes"] + + try: + obj = request.app.state.s3.get(obj_key) + except Exception: + return error_response(503, "unavailable", "storage unavailable", rid) + + return StreamingResponse( + obj.stream(CHUNK), + media_type=content_type, + headers={"Content-Length": str(size_bytes)}, + ) \ No newline at end of file diff --git a/app/api/auth.py b/app/api/auth.py new file mode 100644 index 00000000..de9261ae --- /dev/null +++ b/app/api/auth.py @@ -0,0 +1,104 @@ +from fastapi import APIRouter, Header, HTTPException, Request, Response +from fastapi.responses import JSONResponse + +from app.security import ( + parse_basic, + verify_password, + new_token, + session_expires_at, + get_current_user, +) +from app.config import config +from app.models import Session + +router = APIRouter(tags=["auth"]) + +BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"' +BEARER_CHALLENGE = 'Bearer realm="messenger"' + + +def error_response( + status: int, code: str, message: str, request_id: str, headers=None, +) -> JSONResponse: + return JSONResponse( + status_code=status, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + }, + headers=headers or {}, + ) + + +@router.post("/auth/sessions", response_model=Session, status_code=201) +async def create_session( + request: Request, + authorization: str | None = Header(default=None), +): + storage = request.app.state.storage + session_store = request.app.state.session_store + rid = request.state.request_id + try: + username, password = parse_basic(authorization) + except HTTPException: + return error_response( + 401, + "unauthorized", + "invalid credentials", + rid, + headers={"WWW-Authenticate": BASIC_CHALLENGE}, + ) + + user_row = await storage.get_user_by_username(username) + if user_row is None or not verify_password(password, user_row["password_hash"]): + return error_response( + 401, + "unauthorized", + "invalid credentials", + rid, + headers={"WWW-Authenticate": BASIC_CHALLENGE}, + ) + + token = new_token() + expires_at = session_expires_at(config.SESSION_TTL_SECONDS) + await session_store.create(token, str(user_row["id"]), expires_at) + + user = { + "id": str(user_row["id"]), + "username": user_row["username"], + "display_name": user_row["display_name"], + "created_at": ( + user_row["created_at"] + .isoformat() + .replace("+00:00", "Z") + ), + } + return { + "token": token, + "token_type": "Bearer", + "expires_at": expires_at.isoformat().replace("+00:00", "Z"), + "user": user, + } + + +@router.delete("/auth/sessions/current", status_code=204) +async def revoke_session( + request: Request, + authorization: str | None = Header(default=None), +): + rid = request.state.request_id + try: + _, token = await get_current_user(request, authorization) + except HTTPException: + return error_response( + 401, + "unauthorized", + "unauthorized", + rid, + headers={"WWW-Authenticate": BEARER_CHALLENGE}, + ) + await request.app.state.session_store.delete(token) + return Response(status_code=204) \ No newline at end of file diff --git a/app/api/chats.py b/app/api/chats.py new file mode 100644 index 00000000..59d097af --- /dev/null +++ b/app/api/chats.py @@ -0,0 +1,174 @@ +import re +import uuid + +from fastapi import APIRouter, Header, HTTPException, Query, Request +from fastapi.responses import JSONResponse + +from app.models import ( + Chat, CreateChat, Message, CreateMessage, ChatPage, MessagePage, +) +from app.security import get_current_user + +router = APIRouter(tags=["chats"]) + +BEARER_CHALLENGE = 'Bearer realm="messenger"' +IDEMPOTENCY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$") + + +def error_response(status, code, message, request_id, headers=None): + return JSONResponse( + status_code=status, + content={"error": {"code": code, "message": message, "request_id": request_id}}, + headers=headers or {}, + ) + + +def validate_uuid(value: str) -> None: + try: + uuid.UUID(value) + except (ValueError, TypeError): + raise HTTPException(status_code=400, detail="invalid uuid") + + +def validate_idempotency_key(value: str | None) -> str: + if value is None or not IDEMPOTENCY_RE.match(value): + raise HTTPException(status_code=400, detail="invalid idempotency key") + return value + + +async def resolve_actor(request: Request, authorization: str | None): + rid = request.state.request_id + try: + user, _ = await get_current_user(request, authorization) + except HTTPException as exc: + if exc.status_code == 503: + return None, error_response(503, "unavailable", "auth store unavailable", rid) + return None, error_response( + 401, "unauthorized", "unauthorized", rid, + headers={"WWW-Authenticate": BEARER_CHALLENGE}, + ) + return user, None + + +@router.post("/chats", response_model=Chat, status_code=201) +async def create_chat( + data: CreateChat, + request: Request, + authorization: str | None = Header(default=None), +): + rid = request.state.request_id + actor, err = await resolve_actor(request, authorization) + if err is not None: + return err + + if len(data.member_ids) != len(set(data.member_ids)): + return error_response(400, "invalid_request", "duplicate member_ids", rid) + for mid in data.member_ids: + try: + uuid.UUID(mid) + except (ValueError, TypeError): + return error_response(400, "invalid_request", "invalid member uuid", rid) + + try: + return await request.app.state.storage.create_chat( + data.title, data.member_ids, actor["id"], + ) + except ValueError as e: + msg = str(e) + if msg == "invalid_member": + return error_response(400, "invalid_request", "unknown member", rid) + if msg == "creator_not_member": + return error_response(400, "invalid_request", "creator must be a member", rid) + return error_response(400, "invalid_request", "bad request", rid) + + +@router.get("/chats", response_model=ChatPage) +async def list_chats( + request: Request, + authorization: str | None = Header(default=None), + limit: int = Query(50, ge=1, le=100), + cursor: str | None = Query(None), +): + rid = request.state.request_id + actor, err = await resolve_actor(request, authorization) + if err is not None: + return err + try: + items, next_cursor = await request.app.state.storage.list_chats( + actor["id"], limit, cursor, + ) + except ValueError: + return error_response(400, "invalid_request", "bad cursor", rid) + return {"items": items, "next_cursor": next_cursor} + + +@router.get("/chats/{chat_id}", response_model=Chat) +async def get_chat( + chat_id: str, + request: Request, + authorization: str | None = Header(default=None), +): + rid = request.state.request_id + validate_uuid(chat_id) + actor, err = await resolve_actor(request, authorization) + if err is not None: + return err + chat = await request.app.state.storage.get_chat(chat_id, actor["id"]) + if chat is None: + return error_response(404, "not_found", "chat not found", rid) + return chat + + +@router.post("/chats/{chat_id}/messages", response_model=Message, status_code=201) +async def create_message( + chat_id: str, + data: CreateMessage, + request: Request, + authorization: str | None = Header(default=None), + idempotency_key: str | None = Header(default=None, alias="Idempotency-Key"), +): + rid = request.state.request_id + validate_uuid(chat_id) + validate_idempotency_key(idempotency_key) + actor, err = await resolve_actor(request, authorization) + if err is not None: + return err + + if not data.text.strip() and not data.attachment_ids: + return error_response(400, "invalid_request", "text or attachment_ids required", rid) + + try: + return await request.app.state.storage.add_message( + chat_id, actor["id"], data.text, + data.attachment_ids, idempotency_key, + ) + except ValueError as e: + if str(e) == "idempotency_conflict": + return error_response(409, "conflict", "idempotency key conflict", rid) + if str(e) == "attachment_failed": + return error_response(409, "conflict", "attachment failed", rid) + return error_response(404, "not_found", "chat not found", rid) + + +@router.get("/chats/{chat_id}/messages", response_model=MessagePage) +async def list_messages( + chat_id: str, + request: Request, + authorization: str | None = Header(default=None), + limit: int = Query(50, ge=1, le=100), + cursor: str | None = Query(None), +): + rid = request.state.request_id + validate_uuid(chat_id) + actor, err = await resolve_actor(request, authorization) + if err is not None: + return err + try: + items, next_cursor = await request.app.state.storage.list_messages( + chat_id, actor["id"], limit, cursor, + ) + except ValueError as e: + if str(e) == "bad_cursor": + return error_response(400, "invalid_request", "bad cursor", rid) + return error_response(404, "not_found", "chat not found", rid) + return {"items": items, "next_cursor": next_cursor} \ No newline at end of file diff --git a/app/api/health.py b/app/api/health.py new file mode 100644 index 00000000..537c05f5 --- /dev/null +++ b/app/api/health.py @@ -0,0 +1,53 @@ +import asyncio +from fastapi import APIRouter, Request +from fastapi.responses import JSONResponse + +router = APIRouter(tags=["health"]) + + +@router.get("/health/live") +async def liveness(): + return {"status": "ok"} + + +async def _check_postgres(request: Request) -> None: + async with request.app.state.storage._pool.acquire() as conn: + await conn.fetchval("SELECT 1") + + +async def _check_redis(request: Request) -> None: + store = request.app.state.session_store + if hasattr(store, "_client"): + await store._client.ping() + + +@router.get("/health/ready") +async def readiness(request: Request): + rid = getattr(request.state, "request_id", "") + try: + await asyncio.wait_for(_check_postgres(request), timeout=2.0) + except Exception: + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "db unavailable", + "request_id": rid, + } + }, + ) + try: + await asyncio.wait_for(_check_redis(request), timeout=2.0) + except Exception: + return JSONResponse( + status_code=503, + content={ + "error": { + "code": "unavailable", + "message": "cache unavailable", + "request_id": rid, + } + }, + ) + return {"status": "ok"} \ No newline at end of file diff --git a/app/api/users.py b/app/api/users.py new file mode 100644 index 00000000..eabcf2e7 --- /dev/null +++ b/app/api/users.py @@ -0,0 +1,58 @@ +from fastapi import APIRouter, Header, HTTPException, Request +from fastapi.responses import JSONResponse + +from app.models import User, CreateUser +from app.security import hash_password, get_current_user + +router = APIRouter(tags=["users"]) + + +def error_response( + status: int, code: str, message: str, request_id: str, headers=None, +) -> JSONResponse: + return JSONResponse( + status_code=status, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id, + } + }, + headers=headers or {}, + ) + + +@router.post("/users", response_model=User, status_code=201) +async def register_user(data: CreateUser, request: Request): + storage = request.app.state.storage + rid = request.state.request_id + password_hash = hash_password(data.password) + try: + user = await storage.create_user( + data.username, data.display_name, password_hash, + ) + return user + except ValueError: + return error_response(409, "conflict", "username already taken", rid) + + +@router.get("/users/me", response_model=User) +async def current_user( + request: Request, + authorization: str | None = Header(default=None), +): + rid = request.state.request_id + try: + user, _ = await get_current_user(request, authorization) + except HTTPException as exc: + if exc.status_code == 503: + return error_response(503, "unavailable", "auth store unavailable", rid) + return error_response( + 401, + "unauthorized", + "unauthorized", + rid, + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + return user \ No newline at end of file diff --git a/app/broker.py b/app/broker.py new file mode 100644 index 00000000..60a2ca89 --- /dev/null +++ b/app/broker.py @@ -0,0 +1,69 @@ +import asyncio +import json +import os + +import aio_pika +from aio_pika.abc import AbstractRobustConnection + +from app.config import config + +_connection: AbstractRobustConnection | None = None +_lock = asyncio.Lock() + +PREFETCH_COUNT = int(os.getenv("PREFETCH_COUNT", "1")) + + +async def get_connection() -> AbstractRobustConnection: + global _connection + async with _lock: + if _connection is None or _connection.is_closed: + _connection = await aio_pika.connect_robust(config.RABBITMQ_URL) + return _connection + + +async def publish_job(job: dict, is_dlq: bool = False) -> None: + connection = await get_connection() + channel = await connection.channel() + queue_name = config.RABBITMQ_QUEUE + (".dlq" if is_dlq else "") + + await channel.declare_queue(queue_name, durable=True) + await channel.default_exchange.publish( + aio_pika.Message( + body=json.dumps(job).encode(), + delivery_mode=aio_pika.DeliveryMode.PERSISTENT, + content_type="application/json", + ), + routing_key=queue_name, + ) + await channel.close() + + +async def consume_jobs(handler) -> None: + while True: + try: + connection = await get_connection() + channel = await connection.channel() + await channel.set_qos(prefetch_count=PREFETCH_COUNT) + queue = await channel.declare_queue(config.RABBITMQ_QUEUE, durable=True) + + # Запускаем обработчик в фоне, чтобы не блокировать цикл + async def process_message(message: aio_pika.IncomingMessage): + try: + job = json.loads(message.body) + print(f"[broker] got job: {job.get('attachment_id')}", flush=True) + await handler(job) + await message.ack() + except Exception as e: + print(f"[broker] handler error: {e}", flush=True) + await message.reject(requeue=False) + + async def on_message(message: aio_pika.IncomingMessage): + # Запускаем в фоне, не ждем завершения, чтобы сразу взять следующее + asyncio.create_task(process_message(message)) + + await queue.consume(on_message) + print(f"[broker] listening for jobs (prefetch={PREFETCH_COUNT})...", flush=True) + await asyncio.Future() + except Exception as e: + print(f"[broker] connection error: {e}. Reconnecting in 3 seconds...", flush=True) + await asyncio.sleep(3) \ No newline at end of file diff --git a/app/cache.py b/app/cache.py new file mode 100644 index 00000000..86058f36 --- /dev/null +++ b/app/cache.py @@ -0,0 +1,60 @@ +import json +from typing import Optional + +import redis.asyncio as redis + +from app.config import config + +CACHE_TTL_SECONDS = 30 + + +class Cache: + def __init__(self, client: redis.Redis) -> None: + self._client = client + + async def get_json(self, key: str) -> Optional[dict]: + raw = await self._client.get(key) + if raw is None: + return None + try: + return json.loads(raw) + except json.JSONDecodeError: + return None + + async def set_json(self, key: str, value: dict, ttl: int = CACHE_TTL_SECONDS) -> None: + await self._client.set(key, json.dumps(value), ex=ttl) + + async def delete(self, key: str) -> None: + await self._client.delete(key) + + async def invalidate_chat(self, chat_id: str) -> None: + """Удалить все ключи, связанные с чатом (история).""" + pattern = f"messages:{chat_id}:*" + cursor = 0 + while True: + cursor, keys = await self._client.scan(cursor=cursor, match=pattern, count=100) + if keys: + await self._client.delete(*keys) + if cursor == 0: + break + + async def try_lock(self, key: str, ttl: int = 5) -> bool: + """Атомарно установить ключ. True — если установили мы.""" + result = await self._client.set(key, "1", nx=True, ex=ttl) + return bool(result) + + async def unlock(self, key: str) -> None: + await self._client.delete(key) + + async def close(self) -> None: + await self._client.aclose() + + +def make_cache() -> Cache: + client = redis.from_url( + config.CACHE_URL, + decode_responses=True, + socket_connect_timeout=10, + socket_timeout=10, + ) + return Cache(client) \ No newline at end of file diff --git a/app/config.py b/app/config.py new file mode 100644 index 00000000..ff52ad38 --- /dev/null +++ b/app/config.py @@ -0,0 +1,29 @@ +import os +import secrets + + +class Config: + DATABASE_URL: str = os.getenv( + "DATABASE_URL", + "postgresql://messenger:messenger@localhost:5432/messenger", + ) + SESSION_TTL_SECONDS: int = int(os.getenv("SESSION_TTL_SECONDS", "3600")) + APP_HOST: str = os.getenv("APP_HOST", "0.0.0.0") + APP_PORT: int = int(os.getenv("APP_PORT", "8080")) + SESSION_STORE: str = os.getenv("SESSION_STORE", "postgres") + CACHE_URL: str = os.getenv("CACHE_URL", "redis://valkey:6379/0") + INSTANCE_ID: str = os.getenv("INSTANCE_ID") or secrets.token_hex(6) + + S3_ENDPOINT: str = os.getenv("S3_ENDPOINT", "minio:9000") + S3_PUBLIC_ENDPOINT: str = os.getenv("S3_PUBLIC_ENDPOINT", "localhost:9000") + S3_ACCESS_KEY: str = os.getenv("S3_ACCESS_KEY", "minioadmin") + S3_SECRET_KEY: str = os.getenv("S3_SECRET_KEY", "minioadmin") + S3_BUCKET: str = os.getenv("S3_BUCKET", "attachments") + S3_SECURE: bool = os.getenv("S3_SECURE", "false").lower() == "true" + + RABBITMQ_URL: str = os.getenv("RABBITMQ_URL", "amqp://messenger:messenger@rabbitmq:5672/") + RABBITMQ_QUEUE: str = os.getenv("RABBITMQ_QUEUE", "attachment.process.v1") + MAX_ATTEMPTS: int = int(os.getenv("MAX_ATTEMPTS", "5")) + + +config = Config() \ No newline at end of file diff --git a/app/db.py b/app/db.py new file mode 100644 index 00000000..f49a598f --- /dev/null +++ b/app/db.py @@ -0,0 +1,30 @@ +import asyncpg +from app.config import config + + +class Database: + def __init__(self) -> None: + self._pool: asyncpg.Pool | None = None + + async def connect(self) -> None: + self._pool = await asyncpg.create_pool( + config.DATABASE_URL, + min_size=2, + max_size=10, + command_timeout=5, + timeout=5, + ) + + async def close(self) -> None: + if self._pool is not None: + await self._pool.close() + self._pool = None + + @property + def pool(self) -> asyncpg.Pool: + if self._pool is None: + raise RuntimeError("database is not connected") + return self._pool + + +db = Database() \ No newline at end of file diff --git a/app/lease_checker.py b/app/lease_checker.py new file mode 100644 index 00000000..aefd7a9c --- /dev/null +++ b/app/lease_checker.py @@ -0,0 +1,37 @@ +import asyncio +import sys + +from app.cache import make_cache +from app.db import db +from app.storage import Storage + + +async def main(): + await db.connect() + cache = make_cache() + storage = Storage(db.pool, cache) + + print("[lease_checker] started", flush=True) + while True: + try: + expired_jobs = await storage.get_expired_jobs() + for job in expired_jobs: + print(f"[lease_checker] requeueing expired job {job['job_id']}", flush=True) + await storage.reset_job_status(str(job["job_id"])) + job_payload = { + "schema_version": 1, + "job_id": str(job["job_id"]), + "type": "attachment.process.v1", + "attachment_id": str(job["attachment_id"]), + "pipeline_version": job["pipeline_version"], + "attempt": job["attempt"], + "created_at": "2026-01-01T00:00:00Z", + } + await storage.create_outbox(job_payload) + except Exception as e: + print(f"[lease_checker] error: {e}", flush=True) + await asyncio.sleep(10) + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) \ No newline at end of file diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..41a13155 --- /dev/null +++ b/app/main.py @@ -0,0 +1,113 @@ +import uuid +from contextlib import asynccontextmanager + +from fastapi import FastAPI, Request, HTTPException +from fastapi.responses import JSONResponse +from fastapi.exceptions import RequestValidationError + +from app.db import db +from app.storage import Storage +from app.session_store import make_session_store +from app.cache import make_cache +from app.config import config +from app.s3 import s3 +from app.api import health, users, auth, chats, attachments + + +@asynccontextmanager +async def lifespan(app: FastAPI): + await db.connect() + app.state.cache = make_cache() + app.state.storage = Storage(db.pool, app.state.cache) + app.state.session_store = make_session_store(db.pool) + await app.state.session_store.delete_expired() + s3.ensure_bucket() + app.state.s3 = s3 + yield + await app.state.cache.close() + await app.state.session_store.close() + await db.close() + + +app = FastAPI(title="MTUSI Messenger", version="1.5.0", lifespan=lifespan) + + +@app.middleware("http") +async def request_context(request: Request, call_next): + rid = request.headers.get("X-Request-Id") or uuid.uuid4().hex + request.state.request_id = rid + response = await call_next(request) + response.headers["X-Request-Id"] = rid + response.headers["X-Instance-Id"] = config.INSTANCE_ID + return response + + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request: Request, exc: RequestValidationError): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse(status_code=400, + content={"error": {"code": "invalid_request", "message": "validation error", "request_id": rid}}, + headers={"X-Request-Id": rid}) + + +@app.exception_handler(HTTPException) +async def http_exception_handler(request: Request, exc: HTTPException): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + headers = {"X-Request-Id": rid} + if exc.headers: + headers.update(exc.headers) + code_map = {400:"invalid_request",401:"unauthorized",404:"not_found",409:"conflict", + 413:"payload_too_large",415:"unsupported_media_type",429:"rate_limited",503:"unavailable"} + code = code_map.get(exc.status_code, "internal_error") + message = exc.detail if isinstance(exc.detail, str) else "error" + return JSONResponse(status_code=exc.status_code, + content={"error": {"code": code, "message": message, "request_id": rid}}, + headers=headers) + + +try: + from asyncpg.exceptions import ( + PostgresConnectionError, TooManyConnectionsError, + CannotConnectNowError, ConnectionDoesNotExistError, + ) + + @app.exception_handler(PostgresConnectionError) + @app.exception_handler(TooManyConnectionsError) + @app.exception_handler(CannotConnectNowError) + @app.exception_handler(ConnectionDoesNotExistError) + async def postgres_unavailable_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse(status_code=503, + content={"error": {"code": "unavailable", "message": "database unavailable", "request_id": rid}}, + headers={"X-Request-Id": rid}) +except ImportError: + pass + + +try: + import redis.exceptions as redis_exceptions + + @app.exception_handler(redis_exceptions.ConnectionError) + @app.exception_handler(redis_exceptions.TimeoutError) + async def redis_unavailable_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse(status_code=503, + content={"error": {"code": "unavailable", "message": "cache unavailable", "request_id": rid}}, + headers={"X-Request-Id": rid}) +except ImportError: + pass + + +@app.exception_handler(Exception) +async def generic_exception_handler(request: Request, exc: Exception): + rid = getattr(request.state, "request_id", uuid.uuid4().hex) + return JSONResponse(status_code=500, + content={"error": {"code": "internal_error", "message": "internal error", "request_id": rid}}, + headers={"X-Request-Id": rid}) + + +app.include_router(health.router) +app.include_router(users.router, prefix="/api/v1") +app.include_router(auth.router, prefix="/api/v1") +app.include_router(chats.router, prefix="/api/v1") +app.include_router(attachments.router, prefix="/api/v1") \ No newline at end of file diff --git a/app/migrate.py b/app/migrate.py new file mode 100644 index 00000000..d3684683 --- /dev/null +++ b/app/migrate.py @@ -0,0 +1,19 @@ +import asyncio +import sys + +from app.db import db +from app.migrations import apply_migrations + + +async def main() -> int: + await db.connect() + try: + await apply_migrations(db.pool) + finally: + await db.close() + print("migrations applied", flush=True) + return 0 + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) \ No newline at end of file diff --git a/app/migrations.py b/app/migrations.py new file mode 100644 index 00000000..fea0eaf2 --- /dev/null +++ b/app/migrations.py @@ -0,0 +1,31 @@ +from pathlib import Path +import asyncpg + +MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations" + + +async def apply_migrations(pool: asyncpg.Pool) -> None: + async with pool.acquire() as conn: + await conn.execute( + """ + CREATE TABLE IF NOT EXISTS applied_migrations ( + name TEXT PRIMARY KEY, + applied_at TIMESTAMPTZ NOT NULL DEFAULT now() + ) + """ + ) + applied = { + row["name"] + for row in await conn.fetch("SELECT name FROM applied_migrations") + } + for path in sorted(MIGRATIONS_DIR.glob("*.sql")): + if path.name in applied: + continue + sql = path.read_text(encoding="utf-8") + async with conn.transaction(): + await conn.execute(sql) + await conn.execute( + "INSERT INTO applied_migrations (name) VALUES ($1)", + path.name, + ) + print(f"applied migration: {path.name}", flush=True) \ No newline at end of file diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..36f20f73 --- /dev/null +++ b/app/models.py @@ -0,0 +1,111 @@ +from pydantic import BaseModel, Field, ConfigDict, field_validator +from typing import List, Optional + + +def _not_blank(value: str) -> str: + if not value.strip(): + raise ValueError("must contain non-whitespace characters") + return value + + +def _not_blank_or_empty(value: str) -> str: + if value and not value.strip(): + raise ValueError("must contain non-whitespace characters") + return value + + +class User(BaseModel): + id: str + username: str + display_name: str + created_at: str + + +class CreateUser(BaseModel): + model_config = ConfigDict(extra="forbid") + + username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$") + display_name: str = Field(..., min_length=1, max_length=100) + password: str = Field(..., min_length=12, max_length=128) + + _check_display_name = field_validator("display_name")(_not_blank) + + +class Chat(BaseModel): + id: str + title: str + member_ids: List[str] + created_at: str + + +class CreateChat(BaseModel): + model_config = ConfigDict(extra="forbid") + + title: str = Field(..., min_length=1, max_length=100) + member_ids: List[str] = Field(..., min_length=1, max_length=100) + + _check_title = field_validator("title")(_not_blank) + + +class Message(BaseModel): + id: str + chat_id: str + sender_id: str + text: str + created_at: str + attachment_ids: List[str] + + +class CreateMessage(BaseModel): + model_config = ConfigDict(extra="forbid") + + text: str = Field("", max_length=4000) + attachment_ids: List[str] = Field(default_factory=list, max_length=10) + + _check_text = field_validator("text")(_not_blank_or_empty) + + +class Attachment(BaseModel): + id: str + chat_id: str + owner_id: str + filename: str + content_type: str + size_bytes: int + sha256: str + status: str + variants: List[dict] + error_code: Optional[str] + created_at: str + updated_at: str + + +class ChatPage(BaseModel): + items: List[Chat] + next_cursor: Optional[str] = None + + +class MessagePage(BaseModel): + items: List[Message] + next_cursor: Optional[str] = None + + +class Health(BaseModel): + status: str = "ok" + + +class Session(BaseModel): + token: str + token_type: str = "Bearer" + expires_at: str + user: User + + +class ErrorBody(BaseModel): + code: str + message: str + request_id: str + + +class ErrorResponse(BaseModel): + error: ErrorBody \ No newline at end of file diff --git a/app/outbox_relay.py b/app/outbox_relay.py new file mode 100644 index 00000000..97c7785f --- /dev/null +++ b/app/outbox_relay.py @@ -0,0 +1,31 @@ +import asyncio +import json +import sys + +from app.broker import publish_job +from app.cache import make_cache +from app.db import db +from app.storage import Storage + + +async def main(): + await db.connect() + cache = make_cache() + storage = Storage(db.pool, cache) + + print("[outbox] started", flush=True) + while True: + try: + jobs = await storage.get_unpublished_outbox() + for job in jobs: + payload = json.loads(job["payload"]) if isinstance(job["payload"], str) else job["payload"] + await publish_job(payload) + await storage.mark_outbox_published(str(job["id"])) + print(f"[outbox] published {job['job_id']}", flush=True) + except Exception as e: + print(f"[outbox] error: {e}", flush=True) + await asyncio.sleep(1) + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) \ No newline at end of file diff --git a/app/s3.py b/app/s3.py new file mode 100644 index 00000000..789cd88f --- /dev/null +++ b/app/s3.py @@ -0,0 +1,53 @@ +from datetime import timedelta +from io import BytesIO + +from minio import Minio + +from app.config import config + + +class S3Storage: + def __init__(self) -> None: + common = dict( + access_key=config.S3_ACCESS_KEY, + secret_key=config.S3_SECRET_KEY, + secure=config.S3_SECURE, + region="us-east-1", + ) + self._client = Minio(config.S3_ENDPOINT, **common) + self._presign_client = Minio(config.S3_PUBLIC_ENDPOINT, **common) + self._bucket = config.S3_BUCKET + + @property + def client(self) -> Minio: + return self._client + + @property + def bucket(self) -> str: + return self._bucket + + def ensure_bucket(self) -> None: + if not self._client.bucket_exists(self._bucket): + self._client.make_bucket(self._bucket) + + def put(self, key: str, data: bytes, content_type: str) -> None: + self._client.put_object( + self._bucket, key, BytesIO(data), len(data), content_type=content_type + ) + + def get(self, key: str): + return self._client.get_object(self._bucket, key) + + def stat(self, key: str): + return self._client.stat_object(self._bucket, key) + + def remove(self, key: str) -> None: + self._client.remove_object(self._bucket, key) + + def presigned_put(self, key: str, expires_seconds: int = 300) -> str: + return self._presign_client.presigned_put_object( + self._bucket, key, expires=timedelta(seconds=expires_seconds) + ) + + +s3 = S3Storage() \ No newline at end of file diff --git a/app/security.py b/app/security.py new file mode 100644 index 00000000..827ace2d --- /dev/null +++ b/app/security.py @@ -0,0 +1,83 @@ +import base64 +import secrets +from datetime import datetime, timedelta, timezone + +from argon2 import PasswordHasher +from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHash +from fastapi import Header, HTTPException, Request + +_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, InvalidHash): + return False + + +def new_token() -> str: + return secrets.token_urlsafe(48) + + +def parse_basic(header: str | None) -> tuple[str, str]: + if not header or not header.lower().startswith("basic "): + raise HTTPException(status_code=401, detail="unauthorized") + encoded = header.split(" ", 1)[1].strip() + try: + decoded = base64.b64decode(encoded).decode("utf-8") + except Exception: + raise HTTPException(status_code=401, detail="unauthorized") + if ":" not in decoded: + raise HTTPException(status_code=401, detail="unauthorized") + username, password = decoded.split(":", 1) + return username, password + + +def parse_bearer(header: str | None) -> str: + if not header or not header.lower().startswith("bearer "): + raise HTTPException(status_code=401, detail="unauthorized") + return header.split(" ", 1)[1].strip() + + +async def get_current_user( + request: Request, + authorization: str | None = Header(default=None), +): + token = parse_bearer(authorization) + session_store = request.app.state.session_store + storage = request.app.state.storage + + try: + session = await session_store.get(token) + except Exception: + raise HTTPException(status_code=503, detail="unavailable") + + if session is None: + raise HTTPException(status_code=401, detail="unauthorized") + + expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) + if expires_at <= datetime.now(timezone.utc): + try: + await session_store.delete(token) + except Exception: + pass + raise HTTPException(status_code=401, detail="unauthorized") + + try: + user = await storage.get_user(session["user_id"]) + except Exception: + raise HTTPException(status_code=503, detail="unavailable") + + if user is None: + raise HTTPException(status_code=401, detail="unauthorized") + return user, token + + +def session_expires_at(ttl_seconds: int) -> datetime: + return datetime.now(timezone.utc) + timedelta(seconds=ttl_seconds) \ No newline at end of file diff --git a/app/session_store.py b/app/session_store.py new file mode 100644 index 00000000..6fa68cf3 --- /dev/null +++ b/app/session_store.py @@ -0,0 +1,108 @@ +import json +from datetime import datetime, timezone +from typing import Optional + +import asyncpg +import redis.asyncio as redis + +from app.config import config + + +def _now_iso(value: datetime) -> str: + if value.tzinfo is None: + value = value.replace(tzinfo=timezone.utc) + return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") + + +class PostgresSessionStore: + def __init__(self, pool: asyncpg.Pool) -> None: + self._pool = pool + + async def create(self, token: str, user_id: str, expires_at: datetime) -> dict: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """ + INSERT INTO sessions (token, user_id, expires_at) + VALUES ($1, $2, $3) + RETURNING token, user_id, created_at, expires_at + """, + token, user_id, expires_at, + ) + return { + "token": row["token"], + "user_id": str(row["user_id"]), + "created_at": _now_iso(row["created_at"]), + "expires_at": _now_iso(row["expires_at"]), + } + + async def get(self, token: str) -> Optional[dict]: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """ + SELECT token, user_id, created_at, expires_at + FROM sessions WHERE token = $1 + """, + token, + ) + if row is None: + return None + return { + "token": row["token"], + "user_id": str(row["user_id"]), + "created_at": _now_iso(row["created_at"]), + "expires_at": _now_iso(row["expires_at"]), + } + + async def delete(self, token: str) -> None: + async with self._pool.acquire() as conn: + await conn.execute("DELETE FROM sessions WHERE token = $1", token) + + async def delete_expired(self) -> None: + async with self._pool.acquire() as conn: + await conn.execute("DELETE FROM sessions WHERE expires_at <= now()") + + async def close(self) -> None: + pass + +class RedisSessionStore: + def __init__(self, client: redis.Redis) -> None: + self._client = client + + async def create(self, token: str, user_id: str, expires_at: datetime) -> dict: + now = datetime.now(timezone.utc) + ttl = max(1, int((expires_at - now).total_seconds())) + payload = { + "token": token, + "user_id": user_id, + "created_at": _now_iso(now), + "expires_at": _now_iso(expires_at), + } + await self._client.set(f"session:{token}", json.dumps(payload), ex=ttl) + return payload + + async def get(self, token: str) -> Optional[dict]: + raw = await self._client.get(f"session:{token}") + if raw is None: + return None + return json.loads(raw) + + async def delete(self, token: str) -> None: + await self._client.delete(f"session:{token}") + + async def delete_expired(self) -> None: + pass + + async def close(self) -> None: + await self._client.aclose() + + +def make_session_store(pool: asyncpg.Pool) -> PostgresSessionStore | RedisSessionStore: + if config.SESSION_STORE == "redis": + client = redis.from_url( + config.CACHE_URL, + decode_responses=True, + socket_connect_timeout=10, + socket_timeout=10, + ) + return RedisSessionStore(client) + return PostgresSessionStore(pool) \ No newline at end of file diff --git a/app/storage.py b/app/storage.py new file mode 100644 index 00000000..87b70ac2 --- /dev/null +++ b/app/storage.py @@ -0,0 +1,511 @@ +import asyncio +import hashlib +import json +import uuid +from datetime import datetime, timezone, timedelta +from typing import Optional + +import asyncpg + +from app.cache import Cache + + +def _now_iso(value: datetime) -> str: + if value.tzinfo is None: + value = value.replace(tzinfo=timezone.utc) + return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") + + +def _canonical_hash(payload: dict) -> str: + canonical = json.dumps(payload, sort_keys=True, separators=(",", ":")) + return hashlib.sha256(canonical.encode("utf-8")).hexdigest() + + +class Storage: + def __init__(self, pool: asyncpg.Pool, cache: Cache) -> None: + self._pool = pool + self._cache = cache + + async def create_user(self, username, display_name, password_hash) -> dict: + user_id = str(uuid.uuid4()) + try: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """INSERT INTO users (id, username, display_name, password_hash) + VALUES ($1,$2,$3,$4) + RETURNING id, username, display_name, created_at""", + user_id, username, display_name, password_hash, + ) + except asyncpg.UniqueViolationError: + raise ValueError("conflict") + return self._user_row(row) + + async def get_user(self, user_id: str) -> Optional[dict]: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + "SELECT id, username, display_name, created_at FROM users WHERE id=$1", + user_id, + ) + return self._user_row(row) if row else None + + async def get_user_by_username(self, username: str) -> Optional[dict]: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + "SELECT id, username, display_name, password_hash, created_at FROM users WHERE username=$1", + username, + ) + return dict(row) if row else None + + async def create_chat(self, title, member_ids, creator_id) -> dict: + if creator_id not in member_ids: + raise ValueError("creator_not_member") + chat_id = str(uuid.uuid4()) + async with self._pool.acquire() as conn: + async with conn.transaction(): + existing = await conn.fetchval( + "SELECT count(*) FROM users WHERE id = ANY($1::uuid[])", member_ids, + ) + if existing != len(member_ids): + raise ValueError("invalid_member") + await conn.execute("INSERT INTO chats (id, title) VALUES ($1,$2)", chat_id, title) + await conn.executemany( + "INSERT INTO chat_members (chat_id, user_id) VALUES ($1,$2)", + [(chat_id, uid) for uid in member_ids], + ) + created = await conn.fetchval("SELECT created_at FROM chats WHERE id=$1", chat_id) + return {"id": chat_id, "title": title, "member_ids": member_ids, + "created_at": _now_iso(created)} + + async def get_chat(self, chat_id: str, actor_id: str) -> Optional[dict]: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + "SELECT id, title, created_at FROM chats WHERE id=$1", chat_id, + ) + if row is None: + return None + members = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id=$1", chat_id) + member_ids = [str(m["user_id"]) for m in members] + if actor_id not in member_ids: + return None + return {"id": str(row["id"]), "title": row["title"], + "member_ids": member_ids, "created_at": _now_iso(row["created_at"])} + + async def list_chats(self, actor_id, limit, cursor): + async with self._pool.acquire() as conn: + params = [actor_id] + where = "" + if cursor: + try: + cur_actor, cur_created, cur_id = cursor.split("|", 2) + except ValueError: + raise ValueError("bad_cursor") + if cur_actor != actor_id: + raise ValueError("bad_cursor") + where = "AND (c.created_at, c.id) > ($2,$3)" + params += [datetime.fromisoformat(cur_created.replace("Z", "+00:00")), cur_id] + rows = await conn.fetch( + 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=$1 {where} + ORDER BY c.created_at, c.id LIMIT ${len(params)+1}""", + *params, limit + 1, + ) + items = [] + for row in rows[:limit]: + members = await conn.fetch("SELECT user_id FROM chat_members WHERE chat_id=$1", row["id"]) + items.append({"id": str(row["id"]), "title": row["title"], + "member_ids": [str(m["user_id"]) for m in members], + "created_at": _now_iso(row["created_at"])}) + next_cursor = None + if len(rows) > limit: + last = rows[limit - 1] + next_cursor = f"{actor_id}|{_now_iso(last['created_at'])}|{last['id']}" + return items, next_cursor + + async def is_chat_member(self, chat_id: str, user_id: str) -> bool: + async with self._pool.acquire() as conn: + return await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2", + chat_id, user_id, + ) is not None + + async def create_pending_attachment(self, att_id, chat_id, owner_id, object_key, + filename, content_type, size_bytes, sha256): + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """INSERT INTO attachments + (id, chat_id, owner_id, object_key, filename, content_type, + size_bytes, sha256, status) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8,'queued') + RETURNING id, chat_id, owner_id, object_key, filename, + content_type, size_bytes, sha256, status, + variants, variant_keys, error_code, created_at, updated_at""", + att_id, chat_id, owner_id, object_key, + filename, content_type, size_bytes, sha256, + ) + return self._attachment_row(row, public=True) + + async def create_attachment_with_job_and_outbox( + self, att_id, chat_id, owner_id, object_key, filename, content_type, + size_bytes, sha256, job: dict, + ): + async with self._pool.acquire() as conn: + async with conn.transaction(): + row = await conn.fetchrow( + """INSERT INTO attachments + (id, chat_id, owner_id, object_key, filename, content_type, + size_bytes, sha256, status) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8,'queued') + RETURNING id, chat_id, owner_id, object_key, filename, + content_type, size_bytes, sha256, status, + variants, variant_keys, error_code, created_at, updated_at""", + att_id, chat_id, owner_id, object_key, + filename, content_type, size_bytes, sha256, + ) + await conn.execute( + """INSERT INTO jobs (id, attachment_id, pipeline_version, attempt, status) + VALUES ($1,$2,$3,$4,'queued')""", + job["job_id"], job["attachment_id"], job["pipeline_version"], job["attempt"], + ) + await conn.execute( + "INSERT INTO outbox (id, job_id, payload) VALUES ($1, $2, $3)", + str(uuid.uuid4()), job["job_id"], json.dumps(job), + ) + return self._attachment_row(row, public=True) + + async def get_attachment(self, att_id: str) -> Optional[dict]: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """SELECT id, chat_id, owner_id, object_key, filename, + content_type, size_bytes, sha256, status, + variants, variant_keys, error_code, created_at, updated_at + FROM attachments WHERE id=$1""", + att_id, + ) + if row is None: + return None + return self._attachment_row(row, public=False) + + async def update_attachment_status(self, att_id: str, status: str) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + "UPDATE attachments SET status=$2, updated_at=now() WHERE id=$1", + att_id, status, + ) + + async def complete_attachment_ready(self, att_id, original_key, variants: list, variant_keys: dict): + async with self._pool.acquire() as conn: + await conn.execute( + """UPDATE attachments SET status='ready', object_key=$2, + variants=$3::jsonb, variant_keys=$4::jsonb, updated_at=now() WHERE id=$1""", + att_id, original_key, json.dumps(variants), json.dumps(variant_keys), + ) + + async def fail_attachment(self, att_id: str, error_code: str) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + "UPDATE attachments SET status='failed', error_code=$2, updated_at=now() WHERE id=$1", + att_id, error_code, + ) + + async def replay_failed_attachment(self, att_id: str) -> str: + async with self._pool.acquire() as conn: + async with conn.transaction(): + row = await conn.fetchrow( + "SELECT id, status FROM attachments WHERE id=$1 FOR UPDATE", + att_id, + ) + if row is None: + return "not_found" + if row["status"] == "ready": + return "already_ready" + if row["status"] != "failed": + return "not_failed" + job_id = str(uuid.uuid4()) + job = { + "schema_version": 1, + "job_id": job_id, + "type": "attachment.process.v1", + "attachment_id": att_id, + "pipeline_version": 1, + "attempt": 1, + "created_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"), + } + await conn.execute( + "UPDATE attachments SET status='queued', error_code=NULL, updated_at=now() WHERE id=$1", + att_id, + ) + await conn.execute( + """INSERT INTO jobs (id, attachment_id, pipeline_version, attempt, status) + VALUES ($1,$2,$3,$4,'queued')""", + job_id, att_id, 1, 1, + ) + await conn.execute( + "INSERT INTO outbox (id, job_id, payload) VALUES ($1, $2, $3)", + str(uuid.uuid4()), job_id, json.dumps(job), + ) + return "queued" + + async def create_job(self, job: dict) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + """INSERT INTO jobs (id, attachment_id, pipeline_version, attempt, status) + VALUES ($1,$2,$3,$4,'queued')""", + job["job_id"], job["attachment_id"], job["pipeline_version"], job["attempt"], + ) + + async def increment_job_attempt(self, job_id: str) -> int: + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + "UPDATE jobs SET attempt=attempt+1, updated_at=now() WHERE id=$1 RETURNING attempt", + job_id, + ) + return int(row["attempt"]) if row is not None else 0 + + async def set_job_lease(self, job_id: str, seconds: int = 60) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + "UPDATE jobs SET lease_until = now() + interval '1 second' * $2 WHERE id = $1", + job_id, seconds, + ) + + async def get_expired_jobs(self) -> list[dict]: + async with self._pool.acquire() as conn: + rows = await conn.fetch( + """SELECT j.id as job_id, j.attachment_id, j.pipeline_version, j.attempt, j.status, a.chat_id + FROM jobs j + JOIN attachments a ON a.id = j.attachment_id + WHERE j.status = 'processing' AND j.lease_until < now()""" + ) + return [dict(r) for r in rows] + + async def reset_job_status(self, job_id: str) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + "UPDATE jobs SET status='queued', lease_until=NULL WHERE id=$1", + job_id, + ) + + async def create_outbox(self, job: dict) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + """INSERT INTO outbox (id, job_id, payload) VALUES ($1, $2, $3)""", + str(uuid.uuid4()), job["job_id"], json.dumps(job), + ) + + async def get_unpublished_outbox(self, limit: int = 10) -> list[dict]: + async with self._pool.acquire() as conn: + rows = await conn.fetch( + "SELECT id, job_id, payload FROM outbox WHERE published_at IS NULL ORDER BY created_at LIMIT $1", + limit, + ) + return [dict(r) for r in rows] + + async def mark_outbox_published(self, outbox_id: str) -> None: + async with self._pool.acquire() as conn: + await conn.execute( + "UPDATE outbox SET published_at = now() WHERE id = $1", + outbox_id, + ) + + async def _list_attachment_ids_conn(self, conn, message_id: str) -> list[str]: + rows = await conn.fetch( + "SELECT attachment_id FROM message_attachments WHERE message_id=$1", message_id, + ) + return [str(r["attachment_id"]) for r in rows] + + async def add_message(self, chat_id, sender_id, text, attachment_ids=None, + idempotency_key=None): + attachment_ids = attachment_ids or [] + request_hash = _canonical_hash({"text": text, "attachment_ids": sorted(attachment_ids)}) + path = f"/api/v1/chats/{chat_id}/messages" + method = "POST" + + if idempotency_key is not None: + cached = await self._get_idempotent(sender_id, method, path, idempotency_key, request_hash) + if cached is not None: + return cached + + try: + async with self._pool.acquire() as conn: + async with conn.transaction(): + if attachment_ids: + valid = await conn.fetchval( + """SELECT count(*) FROM attachments + WHERE id = ANY($1::uuid[]) AND chat_id=$2""", + attachment_ids, chat_id, + ) + if valid != len(attachment_ids): + raise ValueError("not_found") + failed = await conn.fetchval( + """SELECT count(*) FROM attachments + WHERE id = ANY($1::uuid[]) AND chat_id=$2 AND status='failed'""", + attachment_ids, chat_id, + ) + if failed: + raise ValueError("attachment_failed") + + message_id = str(uuid.uuid4()) + row = await conn.fetchrow( + """INSERT INTO messages (id, chat_id, sender_id, text) + SELECT $1,$2,$3,$4 WHERE EXISTS ( + SELECT 1 FROM chat_members WHERE chat_id=$2 AND user_id=$3) + RETURNING id, chat_id, sender_id, text, created_at""", + message_id, chat_id, sender_id, text, + ) + if row is None: + raise ValueError("not_found") + if attachment_ids: + await conn.executemany( + "INSERT INTO message_attachments (message_id, attachment_id) VALUES ($1,$2)", + [(message_id, aid) for aid in attachment_ids], + ) + message = self._message_row(row, list(attachment_ids)) + await conn.execute("UPDATE chats SET version=version+1 WHERE id=$1", chat_id) + if idempotency_key is not None: + await conn.execute( + """INSERT INTO idempotency_keys + (user_id, method, path, key, request_hash, response_status, response_body) + VALUES ($1,$2,$3,$4,$5,$6,$7)""", + sender_id, method, path, idempotency_key, + request_hash, 201, json.dumps(message, ensure_ascii=False), + ) + except asyncpg.UniqueViolationError: + cached = await self._get_idempotent(sender_id, method, path, idempotency_key, request_hash) + if cached is not None: + return cached + raise + + await self._cache.invalidate_chat(chat_id) + return message + + async def _get_idempotent(self, user_id, method, path, key, request_hash): + if key is None: + return None + async with self._pool.acquire() as conn: + row = await conn.fetchrow( + """SELECT request_hash, response_body FROM idempotency_keys + WHERE user_id=$1 AND method=$2 AND path=$3 AND key=$4""", + user_id, method, path, key, + ) + if row is None: + return None + if row["request_hash"] != request_hash: + raise ValueError("idempotency_conflict") + return json.loads(row["response_body"]) + + async def list_messages(self, chat_id, actor_id, limit, cursor): + async with self._pool.acquire() as conn: + is_member = await conn.fetchval( + "SELECT 1 FROM chat_members WHERE chat_id=$1 AND user_id=$2", + chat_id, actor_id, + ) + if is_member is None: + raise ValueError("not_found") + + cache_key = f"messages:{chat_id}:{limit}:{cursor or ''}" + cached = await self._cache.get_json(cache_key) + if cached is not None: + version_now = await self._get_chat_version(chat_id) + if cached.get("version") == version_now: + return cached["items"], cached["next_cursor"] + await self._cache.delete(cache_key) + + lock_key = f"lock:{cache_key}" + got_lock = await self._cache.try_lock(lock_key, ttl=10) + if not got_lock: + for _ in range(50): + await asyncio.sleep(0.1) + cached = await self._cache.get_json(cache_key) + if cached is not None: + version_now = await self._get_chat_version(chat_id) + if cached.get("version") == version_now: + return cached["items"], cached["next_cursor"] + + try: + version_before = await self._get_chat_version(chat_id) + async with self._pool.acquire() as conn: + params = [chat_id] + where = "" + if cursor: + try: + cur_chat, cur_created, cur_id = cursor.split("|", 2) + except ValueError: + raise ValueError("bad_cursor") + if cur_chat != chat_id: + raise ValueError("bad_cursor") + where = "AND (created_at, id) > ($2,$3)" + params += [datetime.fromisoformat(cur_created.replace("Z", "+00:00")), cur_id] + rows = await conn.fetch( + f"""SELECT id, chat_id, sender_id, text, created_at + FROM messages WHERE chat_id=$1 {where} + ORDER BY created_at, id LIMIT ${len(params)+1}""", + *params, limit + 1, + ) + items = [] + for row in rows[:limit]: + att_ids = await self._list_attachment_ids_conn(conn, row["id"]) + items.append(self._message_row(row, att_ids)) + next_cursor = None + if len(rows) > limit: + last = rows[limit - 1] + next_cursor = f"{chat_id}|{_now_iso(last['created_at'])}|{last['id']}" + version_after = await self._get_chat_version(chat_id) + if got_lock and version_before == version_after: + await self._cache.set_json(cache_key, { + "items": items, "next_cursor": next_cursor, "version": version_after, + }) + return items, next_cursor + finally: + if got_lock: + await self._cache.unlock(lock_key) + + async def _get_chat_version(self, chat_id: str) -> int: + async with self._pool.acquire() as conn: + row = await conn.fetchval("SELECT version FROM chats WHERE id=$1", chat_id) + return int(row) if row is not None else 0 + + @staticmethod + def _user_row(row) -> dict: + return {"id": str(row["id"]), "username": row["username"], + "display_name": row["display_name"], "created_at": _now_iso(row["created_at"])} + + @staticmethod + def _message_row(row, attachment_ids=None) -> dict: + return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), + "sender_id": str(row["sender_id"]), "text": row["text"], + "created_at": _now_iso(row["created_at"]), + "attachment_ids": attachment_ids or []} + + @staticmethod + def _attachment_row(row, public: bool) -> dict: + variants_raw = row["variants"] + if isinstance(variants_raw, str): + try: + variants = json.loads(variants_raw) + except json.JSONDecodeError: + variants = [] + else: + variants = variants_raw or [] + + result = { + "id": str(row["id"]), "chat_id": str(row["chat_id"]), + "owner_id": str(row["owner_id"]), "filename": row["filename"], + "content_type": row["content_type"], "size_bytes": row["size_bytes"], + "sha256": row["sha256"], "status": row["status"], + "variants": variants, + "error_code": row["error_code"], + "created_at": _now_iso(row["created_at"]), + "updated_at": _now_iso(row["updated_at"]), + } + if not public: + result["_object_key"] = row["object_key"] + variant_keys_raw = row["variant_keys"] + if isinstance(variant_keys_raw, str): + try: + result["_variant_keys"] = json.loads(variant_keys_raw) + except json.JSONDecodeError: + result["_variant_keys"] = {} + else: + result["_variant_keys"] = variant_keys_raw or {} + return result \ No newline at end of file diff --git a/app/worker.py b/app/worker.py new file mode 100644 index 00000000..01542d7a --- /dev/null +++ b/app/worker.py @@ -0,0 +1,151 @@ +import asyncio +import hashlib +import io +import random +import sys +from concurrent.futures import ThreadPoolExecutor + +from PIL import Image, ImageOps, UnidentifiedImageError + +from app.broker import consume_jobs, publish_job +from app.cache import make_cache +from app.config import config +from app.db import db +from app.s3 import s3 +from app.storage import Storage + +THUMB_SIZE = 256 +CROP_SIZE = 128 +OPTIMIZED_SIZE = 1280 + +_executor = ThreadPoolExecutor(max_workers=10) + +PERMANENT_ERRORS = (UnidentifiedImageError,) +PERMANENT_OSERROR_MARKERS = ( + "cannot identify image", + "image file is truncated", + "broken data stream", +) + + +def is_permanent(exc: Exception) -> bool: + if isinstance(exc, PERMANENT_ERRORS): + return True + if isinstance(exc, OSError): + text = str(exc).lower() + if any(m in text for m in PERMANENT_OSERROR_MARKERS): + return True + return False + + +def process_images(data: bytes): + img = Image.open(io.BytesIO(data)) + img = ImageOps.exif_transpose(img) + + if img.mode in ("RGBA", "LA", "P"): + img = img.convert("RGB", background=(255, 255, 255)) + else: + img = img.convert("RGB") + + results = {} + + thumb = img.copy() + thumb.thumbnail((THUMB_SIZE, THUMB_SIZE), Image.LANCZOS) + buf = io.BytesIO() + thumb.save(buf, format="JPEG", quality=85) + results["thumbnail"] = (buf.getvalue(), thumb.size[0], thumb.size[1], "image/jpeg") + + crop = ImageOps.fit(img, (CROP_SIZE, CROP_SIZE), Image.LANCZOS, centering=(0.5, 0.5)) + buf = io.BytesIO() + crop.save(buf, format="JPEG", quality=85) + results["crop"] = (buf.getvalue(), crop.size[0], crop.size[1], "image/jpeg") + + opt = img.copy() + if opt.size[0] > OPTIMIZED_SIZE or opt.size[1] > OPTIMIZED_SIZE: + opt.thumbnail((OPTIMIZED_SIZE, OPTIMIZED_SIZE), Image.LANCZOS) + buf = io.BytesIO() + opt.save(buf, format="WEBP", quality=80) + results["optimized"] = (buf.getvalue(), opt.size[0], opt.size[1], "image/webp") + + return results + + +async def handle_job(job: dict, storage: Storage) -> None: + att_id = job.get("attachment_id") + attempt = job.get("attempt", 1) + job_id = job.get("job_id") + + att = await storage.get_attachment(att_id) + if att is None: + return + if att["status"] == "ready": + return + + await storage.update_attachment_status(att_id, "processing") + await storage.set_job_lease(job_id, 60) + + try: + staging_key = att["_object_key"] + original = s3.get(staging_key).read() + + final_key = f"originals/{att['chat_id']}/{att_id}" + s3.put(final_key, original, att["content_type"]) + + loop = asyncio.get_running_loop() + images = await loop.run_in_executor(_executor, process_images, original) + + variants = [] + variant_keys = {} + + for name, (data, w, h, ctype) in images.items(): + ext = "webp" if name == "optimized" else "jpg" + key = f"{name}s/{att['chat_id']}/{att_id}.{ext}" + s3.put(key, data, ctype) + variants.append({ + "name": name, + "content_type": ctype, + "size_bytes": len(data), + "width": w, + "height": h, + "sha256": hashlib.sha256(data).hexdigest() + }) + variant_keys[name] = key + + await storage.complete_attachment_ready(att_id, final_key, variants, variant_keys) + + try: + s3.remove(staging_key) + except Exception: + pass + + print(f"[worker] SUCCESS {att_id}", flush=True) + except Exception as e: + print(f"[worker] ERROR {att_id}: {type(e).__name__} - {e}", flush=True) + if is_permanent(e): + await storage.fail_attachment(att_id, "invalid_image") + await publish_job(job, is_dlq=True) + return + if attempt >= config.MAX_ATTEMPTS: + await storage.fail_attachment(att_id, "retry_exhausted") + await publish_job(job, is_dlq=True) + return + new_attempt = await storage.increment_job_attempt(job_id) + job["attempt"] = new_attempt + delay = (2 ** new_attempt) + random.uniform(0, 1) + print(f"[worker] retrying in {delay:.2f} seconds...", flush=True) + await asyncio.sleep(delay) + await publish_job(job) + + +async def main() -> int: + await db.connect() + cache = make_cache() + storage = Storage(db.pool, cache) + s3.ensure_bucket() + print(f"[worker] started, queue={config.RABBITMQ_QUEUE}", flush=True) + await consume_jobs(lambda job: handle_job(job, storage)) + return 0 + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..dc816025 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,228 @@ +services: + postgres: + image: postgres:16.4-alpine + command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements"] + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD: messenger + POSTGRES_DB: messenger + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 20 + networks: [messenger] + deploy: + resources: + limits: {cpus: "1.0", memory: 512M} + + valkey: + image: valkey/valkey:8.0-alpine + command: ["valkey-server", "--appendonly", "yes", "--save", ""] + volumes: + - valkey_data:/data + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 20 + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.5", memory: 256M} + + minio: + image: bitnamilegacy/minio:2025.4.22-debian-12-r1 + user: "0:0" + command: server /data --console-address ":9001" + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin} + volumes: + - minio_data:/data + ports: + - "127.0.0.1:9000:9000" + healthcheck: + test: ["CMD", "mc", "ready", "local"] + interval: 5s + timeout: 3s + retries: 20 + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.5", memory: 256M} + + minio-init: + image: bitnamilegacy/minio:2025.4.22-debian-12-r1 + user: "0:0" + depends_on: + minio: {condition: service_healthy} + entrypoint: > + /bin/sh -c " + mc alias set local http://minio:9000 $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD && + mc mb --ignore-existing local/attachments && + mc anonymous set none local/attachments && + (mc admin user add local api-user api-user-secret-123 || true) && + (mc admin policy attach local readwrite --user api-user || true) && + echo 'bucket ready' + " + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minioadmin} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minioadmin} + networks: [messenger] + + rabbitmq: + image: rabbitmq:3.13-management-alpine + environment: + RABBITMQ_DEFAULT_USER: messenger + RABBITMQ_DEFAULT_PASS: messenger + volumes: + - rabbitmq_data:/var/lib/rabbitmq + healthcheck: + test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] + interval: 10s + timeout: 5s + retries: 10 + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.5", memory: 256M} + + migrate: + build: {context: ., dockerfile: Dockerfile} + environment: + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://valkey:6379/0 + SESSION_STORE: redis + command: ["python", "-m", "app.migrate"] + depends_on: + postgres: {condition: service_healthy} + valkey: {condition: service_healthy} + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.5", memory: 256M} + + outbox: + build: {context: ., dockerfile: Dockerfile} + command: ["python", "-m", "app.outbox_relay"] + environment: + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://valkey:6379/0 + SESSION_STORE: redis + RABBITMQ_URL: "amqp://messenger:messenger@rabbitmq:5672/" + RABBITMQ_QUEUE: "attachment.process.v1" + depends_on: + postgres: {condition: service_healthy} + valkey: {condition: service_healthy} + rabbitmq: {condition: service_healthy} + migrate: {condition: service_completed_successfully} + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.2", memory: 128M} + + lease_checker: + build: {context: ., dockerfile: Dockerfile} + command: ["python", "-m", "app.lease_checker"] + environment: + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://valkey:6379/0 + RABBITMQ_URL: "amqp://messenger:messenger@rabbitmq:5672/" + RABBITMQ_QUEUE: "attachment.process.v1" + depends_on: + postgres: {condition: service_healthy} + rabbitmq: {condition: service_healthy} + migrate: {condition: service_completed_successfully} + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.2", memory: 128M} + + api: + build: {context: ., dockerfile: Dockerfile} + environment: + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://valkey:6379/0 + SESSION_STORE: redis + SESSION_TTL_SECONDS: "3600" + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + S3_ENDPOINT: "minio:9000" + S3_PUBLIC_ENDPOINT: "localhost:9000" + S3_ACCESS_KEY: "api-user" + S3_SECRET_KEY: "api-user-secret-123" + S3_BUCKET: "attachments" + S3_SECURE: "false" + RABBITMQ_URL: "amqp://messenger:messenger@rabbitmq:5672/" + RABBITMQ_QUEUE: "attachment.process.v1" + MAX_ATTEMPTS: "5" + depends_on: + postgres: {condition: service_healthy} + valkey: {condition: service_healthy} + migrate: {condition: service_completed_successfully} + minio-init: {condition: service_completed_successfully} + rabbitmq: {condition: service_healthy} + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"] + interval: 5s + timeout: 3s + retries: 3 + start_period: 10s + networks: [messenger] + deploy: + replicas: 2 + resources: + limits: {cpus: "0.5", memory: 256M} + + worker: + build: {context: ., dockerfile: Dockerfile} + command: ["python", "-m", "app.worker"] + environment: + DATABASE_URL: postgresql://messenger:messenger@postgres:5432/messenger + CACHE_URL: redis://valkey:6379/0 + SESSION_STORE: redis + S3_ENDPOINT: "minio:9000" + S3_ACCESS_KEY: "api-user" + S3_SECRET_KEY: "api-user-secret-123" + S3_BUCKET: "attachments" + S3_SECURE: "false" + RABBITMQ_URL: "amqp://messenger:messenger@rabbitmq:5672/" + RABBITMQ_QUEUE: "attachment.process.v1" + MAX_ATTEMPTS: "5" + PREFETCH_COUNT: "${PREFETCH_COUNT:-1}" + depends_on: + postgres: {condition: service_healthy} + valkey: {condition: service_healthy} + migrate: {condition: service_completed_successfully} + minio-init: {condition: service_completed_successfully} + rabbitmq: {condition: service_healthy} + networks: [messenger] + deploy: + resources: + limits: {cpus: "1.0", memory: 512M} + + lb: + image: nginx:1.27-alpine + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf:ro + ports: + - "127.0.0.1:8080:8080" + depends_on: + - api + networks: [messenger] + deploy: + resources: + limits: {cpus: "0.5", memory: 128M} + +volumes: + postgres_data: + valkey_data: + minio_data: + rabbitmq_data: + +networks: + messenger: + driver: bridge \ No newline at end of file diff --git a/evidence/demo-ids.txt b/evidence/demo-ids.txt new file mode 100644 index 00000000..49560422 --- /dev/null +++ b/evidence/demo-ids.txt @@ -0,0 +1,9 @@ +alice_id=6d5c6bbb-5a2b-4dab-8fc2-fa8b9e27b008 +bob_id=b3cfb9d5-4dfa-4292-8c7d-c26859ddbfd1 +chat_id=6ac76405-530d-4380-963a-a3f9e5e41038 +att_id=60a44fbb-ecd3-40ba-89c3-890d03d51ddb +message_id=1dfa07c6-ce12-446f-bd32-161b555eb4d1 +other_message_id=6da484cc-c0fb-403c-b144-29e64d1c95d1 +upload_status=202 +content_status=409 +final_status=ready diff --git a/evidence/evidence/README.md b/evidence/evidence/README.md new file mode 100644 index 00000000..8df70015 --- /dev/null +++ b/evidence/evidence/README.md @@ -0,0 +1,5 @@ +# Доказательства + +Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки. + +Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно. diff --git a/evidence/orig-demo.png b/evidence/orig-demo.png new file mode 100644 index 00000000..e7c92078 Binary files /dev/null and b/evidence/orig-demo.png differ diff --git a/evidence/queue-prefetch1-large.json b/evidence/queue-prefetch1-large.json new file mode 100644 index 00000000..50be4824 --- /dev/null +++ b/evidence/queue-prefetch1-large.json @@ -0,0 +1,20 @@ +{ + "prefetch": 1, + "size": "large", + "files_requested": 15, + "files_ok": 15, + "files_failed": 0, + "duration_s": 11.002, + "throughput_files_per_s": 1.363, + "queue_lag_s": { + "p50": 4.142, + "p95": 7.278, + "p99": 7.278 + }, + "time_to_ready_s": { + "p50": 4.542, + "p95": 7.93, + "p99": 7.93 + }, + "worker_rss_mib": 97.5 +} \ No newline at end of file diff --git a/evidence/queue-prefetch1-medium.json b/evidence/queue-prefetch1-medium.json new file mode 100644 index 00000000..5df91286 --- /dev/null +++ b/evidence/queue-prefetch1-medium.json @@ -0,0 +1,20 @@ +{ + "prefetch": 1, + "size": "medium", + "files_requested": 30, + "files_ok": 30, + "files_failed": 0, + "duration_s": 5.979, + "throughput_files_per_s": 5.018, + "queue_lag_s": { + "p50": 2.714, + "p95": 3.044, + "p99": 3.074 + }, + "time_to_ready_s": { + "p50": 2.751, + "p95": 3.083, + "p99": 3.263 + }, + "worker_rss_mib": 50.6 +} \ No newline at end of file diff --git a/evidence/queue-prefetch1-small.json b/evidence/queue-prefetch1-small.json new file mode 100644 index 00000000..8a54f35d --- /dev/null +++ b/evidence/queue-prefetch1-small.json @@ -0,0 +1,20 @@ +{ + "prefetch": 1, + "size": "small", + "files_requested": 50, + "files_ok": 50, + "files_failed": 0, + "duration_s": 8.998, + "throughput_files_per_s": 5.557, + "queue_lag_s": { + "p50": 3.264, + "p95": 5.788, + "p99": 5.819 + }, + "time_to_ready_s": { + "p50": 3.264, + "p95": 5.819, + "p99": 5.856 + }, + "worker_rss_mib": 37.9 +} \ No newline at end of file diff --git a/evidence/queue-prefetch20-large.json b/evidence/queue-prefetch20-large.json new file mode 100644 index 00000000..bfe20497 --- /dev/null +++ b/evidence/queue-prefetch20-large.json @@ -0,0 +1,20 @@ +{ + "prefetch": 20, + "size": "large", + "files_requested": 15, + "files_ok": 0, + "files_failed": 15, + "duration_s": 602.704, + "throughput_files_per_s": 0.0, + "queue_lag_s": { + "p50": 2.365, + "p95": 2.506, + "p99": 2.506 + }, + "time_to_ready_s": { + "p50": null, + "p95": null, + "p99": null + }, + "worker_rss_mib": 0.0 +} \ No newline at end of file diff --git a/evidence/queue-prefetch20-medium.json b/evidence/queue-prefetch20-medium.json new file mode 100644 index 00000000..257ea362 --- /dev/null +++ b/evidence/queue-prefetch20-medium.json @@ -0,0 +1,20 @@ +{ + "prefetch": 20, + "size": "medium", + "files_requested": 30, + "files_ok": 30, + "files_failed": 0, + "duration_s": 6.129, + "throughput_files_per_s": 4.894, + "queue_lag_s": { + "p50": 2.677, + "p95": 2.949, + "p99": 2.966 + }, + "time_to_ready_s": { + "p50": 2.738, + "p95": 3.431, + "p99": 3.435 + }, + "worker_rss_mib": 112.6 +} \ No newline at end of file diff --git a/evidence/queue-prefetch20-small.json b/evidence/queue-prefetch20-small.json new file mode 100644 index 00000000..5cdab35c --- /dev/null +++ b/evidence/queue-prefetch20-small.json @@ -0,0 +1,20 @@ +{ + "prefetch": 20, + "size": "small", + "files_requested": 50, + "files_ok": 50, + "files_failed": 0, + "duration_s": 8.563, + "throughput_files_per_s": 5.839, + "queue_lag_s": { + "p50": 3.35, + "p95": 5.352, + "p99": 5.383 + }, + "time_to_ready_s": { + "p50": 3.369, + "p95": 5.392, + "p99": 5.464 + }, + "worker_rss_mib": 42.5 +} \ No newline at end of file diff --git a/evidence/queue-prefetch5-large.json b/evidence/queue-prefetch5-large.json new file mode 100644 index 00000000..e84c9e3b --- /dev/null +++ b/evidence/queue-prefetch5-large.json @@ -0,0 +1,20 @@ +{ + "prefetch": 5, + "size": "large", + "files_requested": 15, + "files_ok": 15, + "files_failed": 0, + "duration_s": 18.041, + "throughput_files_per_s": 0.831, + "queue_lag_s": { + "p50": 5.991, + "p95": 11.221, + "p99": 11.221 + }, + "time_to_ready_s": { + "p50": 10.316, + "p95": 14.838, + "p99": 14.838 + }, + "worker_rss_mib": 511.7 +} \ No newline at end of file diff --git a/evidence/queue-prefetch5-medium.json b/evidence/queue-prefetch5-medium.json new file mode 100644 index 00000000..d5bb3e98 --- /dev/null +++ b/evidence/queue-prefetch5-medium.json @@ -0,0 +1,20 @@ +{ + "prefetch": 5, + "size": "medium", + "files_requested": 30, + "files_ok": 30, + "files_failed": 0, + "duration_s": 6.309, + "throughput_files_per_s": 4.755, + "queue_lag_s": { + "p50": 2.789, + "p95": 3.297, + "p99": 3.306 + }, + "time_to_ready_s": { + "p50": 2.789, + "p95": 3.524, + "p99": 3.533 + }, + "worker_rss_mib": 114.0 +} \ No newline at end of file diff --git a/evidence/queue-prefetch5-small.json b/evidence/queue-prefetch5-small.json new file mode 100644 index 00000000..024685ca --- /dev/null +++ b/evidence/queue-prefetch5-small.json @@ -0,0 +1,20 @@ +{ + "prefetch": 5, + "size": "small", + "files_requested": 50, + "files_ok": 50, + "files_failed": 0, + "duration_s": 8.361, + "throughput_files_per_s": 5.98, + "queue_lag_s": { + "p50": 3.408, + "p95": 5.04, + "p99": 5.06 + }, + "time_to_ready_s": { + "p50": 3.408, + "p95": 5.213, + "p99": 5.237 + }, + "worker_rss_mib": 43.8 +} \ No newline at end of file diff --git a/evidence/queue-recovery.json b/evidence/queue-recovery.json new file mode 100644 index 00000000..742b06d7 --- /dev/null +++ b/evidence/queue-recovery.json @@ -0,0 +1,6 @@ +{ + "files": 100, + "finished": 100, + "pending_after_timeout": 0, + "recovery_time_s": 22.749 +} \ No newline at end of file diff --git a/evidence/replay.txt b/evidence/replay.txt new file mode 100644 index 00000000..f7b69f29 --- /dev/null +++ b/evidence/replay.txt @@ -0,0 +1,6 @@ +att_id=559d9cab-90a0-4baf-b36f-355ad93ffae2 +chat_id=ce16016a-c647-4d87-8697-1061f25ff47c +replay_ready_status=409 +status_after_simulated_failure=failed/simulated_failure +replay_status=202 +final_status=ready diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..6f19bcb4 Binary files /dev/null and b/evidence/smoke.txt differ diff --git a/evidence/thumb-demo.jpg b/evidence/thumb-demo.jpg new file mode 100644 index 00000000..bebef17c Binary files /dev/null and b/evidence/thumb-demo.jpg differ diff --git a/evidence/worker-crash.txt b/evidence/worker-crash.txt new file mode 100644 index 00000000..56771c4d --- /dev/null +++ b/evidence/worker-crash.txt @@ -0,0 +1,11 @@ +============================= test session starts ============================= +platform win32 -- Python 3.13.15, pytest-8.3.5, pluggy-1.6.0 -- C:\Users\maris\PythonProjects\mtusi2026lab1-fork\venv\Scripts\python.exe +cachedir: .pytest_cache +rootdir: C:\Users\maris\PythonProjects\mtusi2026lab5 +plugins: anyio-4.15.1, asyncio-0.25.3 +asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None +collecting ... collected 1 item + +student-tests/test_worker_crash.py::test_worker_recovers_after_ack_lost PASSED [100%] + +============================== 1 passed in 6.54s ============================== diff --git a/migrations/001_users.sql b/migrations/001_users.sql new file mode 100644 index 00000000..4e603095 --- /dev/null +++ b/migrations/001_users.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS 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 DEFAULT now() +); \ No newline at end of file diff --git a/migrations/002_chats.sql b/migrations/002_chats.sql new file mode 100644 index 00000000..e46d22e3 --- /dev/null +++ b/migrations/002_chats.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/migrations/003_chat_members.sql b/migrations/003_chat_members.sql new file mode 100644 index 00000000..cbed26ea --- /dev/null +++ b/migrations/003_chat_members.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS 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) +); \ No newline at end of file diff --git a/migrations/004_messages.sql b/migrations/004_messages.sql new file mode 100644 index 00000000..31b695d7 --- /dev/null +++ b/migrations/004_messages.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS 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 DEFAULT now() +); \ No newline at end of file diff --git a/migrations/005_sessions.sql b/migrations/005_sessions.sql new file mode 100644 index 00000000..ab1b4b12 --- /dev/null +++ b/migrations/005_sessions.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS sessions ( + token TEXT PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + expires_at TIMESTAMPTZ NOT NULL +); \ No newline at end of file diff --git a/migrations/006_messages_index.sql b/migrations/006_messages_index.sql new file mode 100644 index 00000000..780d4708 --- /dev/null +++ b/migrations/006_messages_index.sql @@ -0,0 +1,2 @@ +CREATE INDEX IF NOT EXISTS idx_messages_chat_created + ON messages(chat_id, created_at, id); \ No newline at end of file diff --git a/migrations/007_idempotency.sql b/migrations/007_idempotency.sql new file mode 100644 index 00000000..95e6cadd --- /dev/null +++ b/migrations/007_idempotency.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method TEXT NOT NULL, + path TEXT NOT NULL, + key TEXT NOT NULL, + request_hash TEXT NOT NULL, + response_status INTEGER NOT NULL, + response_body TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, method, path, key) +); \ No newline at end of file diff --git a/migrations/008_idempotency_index.sql b/migrations/008_idempotency_index.sql new file mode 100644 index 00000000..bb807951 --- /dev/null +++ b/migrations/008_idempotency_index.sql @@ -0,0 +1,2 @@ +CREATE INDEX IF NOT EXISTS idx_idempotency_created + ON idempotency_keys(created_at); \ No newline at end of file diff --git a/migrations/009_chat_version.sql b/migrations/009_chat_version.sql new file mode 100644 index 00000000..d46308b2 --- /dev/null +++ b/migrations/009_chat_version.sql @@ -0,0 +1 @@ +ALTER TABLE chats ADD COLUMN IF NOT EXISTS version BIGINT NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/migrations/010_attachments.sql b/migrations/010_attachments.sql new file mode 100644 index 00000000..b7081379 --- /dev/null +++ b/migrations/010_attachments.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID NOT NULL REFERENCES users(id), + object_key TEXT NOT NULL UNIQUE, + filename TEXT NOT NULL, + content_type TEXT NOT NULL, + size_bytes BIGINT NOT NULL, + sha256 TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'ready', + error_code TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); +CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments(chat_id); \ No newline at end of file diff --git a/migrations/011_message_attachments.sql b/migrations/011_message_attachments.sql new file mode 100644 index 00000000..cbf805eb --- /dev/null +++ b/migrations/011_message_attachments.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id), + PRIMARY KEY (message_id, attachment_id) +); \ No newline at end of file diff --git a/migrations/012_attachment_status.sql b/migrations/012_attachment_status.sql new file mode 100644 index 00000000..ae8627d0 --- /dev/null +++ b/migrations/012_attachment_status.sql @@ -0,0 +1,4 @@ +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check; +ALTER TABLE attachments + ADD CONSTRAINT attachments_status_check + CHECK (status IN ('pending', 'ready')); \ No newline at end of file diff --git a/migrations/013_jobs.sql b/migrations/013_jobs.sql new file mode 100644 index 00000000..fbfca495 --- /dev/null +++ b/migrations/013_jobs.sql @@ -0,0 +1,17 @@ +ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check; +ALTER TABLE attachments + ADD CONSTRAINT attachments_status_check + CHECK (status IN ('queued', 'processing', 'ready', 'failed')); + +CREATE TABLE IF NOT EXISTS jobs ( + id UUID PRIMARY KEY, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE, + pipeline_version INTEGER NOT NULL DEFAULT 1, + attempt INTEGER NOT NULL DEFAULT 1, + status TEXT NOT NULL DEFAULT 'queued', + error_code TEXT, + lease_until TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); +CREATE INDEX IF NOT EXISTS idx_jobs_status ON jobs(status); \ No newline at end of file diff --git a/migrations/014_attachment_variants.sql b/migrations/014_attachment_variants.sql new file mode 100644 index 00000000..6b26a969 --- /dev/null +++ b/migrations/014_attachment_variants.sql @@ -0,0 +1 @@ +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variants JSONB NOT NULL DEFAULT '[]'::jsonb; \ No newline at end of file diff --git a/migrations/015_variant_keys.sql b/migrations/015_variant_keys.sql new file mode 100644 index 00000000..f267a848 --- /dev/null +++ b/migrations/015_variant_keys.sql @@ -0,0 +1 @@ +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variant_keys JSONB NOT NULL DEFAULT '{}'::jsonb; \ No newline at end of file diff --git a/migrations/016_outbox.sql b/migrations/016_outbox.sql new file mode 100644 index 00000000..ebd5cc23 --- /dev/null +++ b/migrations/016_outbox.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS outbox ( + id UUID PRIMARY KEY, + job_id UUID NOT NULL, + payload JSONB NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + published_at TIMESTAMPTZ +); +CREATE INDEX IF NOT EXISTS idx_outbox_unpublished ON outbox(published_at) WHERE published_at IS NULL; \ No newline at end of file diff --git a/migrations/017_lease_dlq.sql b/migrations/017_lease_dlq.sql new file mode 100644 index 00000000..db8c8e1e --- /dev/null +++ b/migrations/017_lease_dlq.sql @@ -0,0 +1,2 @@ +ALTER TABLE jobs ADD COLUMN IF NOT EXISTS lease_until TIMESTAMPTZ; +CREATE INDEX IF NOT EXISTS idx_jobs_lease ON jobs(lease_until); \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 00000000..2c6b48d9 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,31 @@ +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + upstream api_backend { + server api:8080 max_fails=1 fail_timeout=10s; + } + + server { + listen 8080; + server_name _; + + client_max_body_size 11m; + + location / { + proxy_pass http://api_backend; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_connect_timeout 1s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + proxy_next_upstream error timeout http_502 http_503 http_504; + } + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..cf727535 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +fastapi==0.115.12 +uvicorn==0.34.3 +pydantic==2.11.4 +httpx==0.28.1 +pytest==8.3.5 +pytest-asyncio==0.25.3 +asyncpg==0.30.0 +argon2-cffi==23.1.0 +redis==5.1.1 +minio==7.2.15 +Pillow==11.2.1 +aio-pika==9.5.4 \ No newline at end of file diff --git a/scripts/benchmark_queue.py b/scripts/benchmark_queue.py new file mode 100644 index 00000000..dee0c11b --- /dev/null +++ b/scripts/benchmark_queue.py @@ -0,0 +1,122 @@ +import base64 +import io +import os +import subprocess +import time +import uuid +import threading +import statistics + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +CONCURRENCY = 10 +TOTAL_FILES = 30 + + +def _png(size=(320, 200), color=(0, 255, 0)): + buf = io.BytesIO() + Image.new("RGB", size, color).save(buf, format="PNG") + return buf.getvalue() + + +def _user(c): + u = "bench_" + uuid.uuid4().hex[:8] + p = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "B", "password": p}) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": s["user"]["id"], "token": s["token"]} + + +def _chat(c, a, b): + return c.post( + f"{BASE}/api/v1/chats", + json={"title": "bench", "member_ids": [a["id"], b["id"]]}, + headers={"Authorization": "Bearer " + a["token"]}, + ).json() + + +def _mem(container): + out = subprocess.run( + ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container], + capture_output=True, text=True, check=True, + ).stdout.strip() + v = out.split("/")[0].strip() + units = {"B": 1, "KiB": 1024, "Ki": 1024, "MiB": 1024 ** 2, "Mi": 1024 ** 2, "GiB": 1024 ** 3, "Gi": 1024 ** 3} + for u in sorted(units, key=len, reverse=True): + if v.endswith(u): + return int(float(v[: -len(u)]) * units[u]) + return 0 + + +def main(): + prefetch = os.getenv("PREFETCH_COUNT", "1") + data = _png() + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + + containers = subprocess.run( + ["docker", "ps", "--filter", "name=mtusi2026lab5-worker-", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True, + ).stdout.split() + if not containers: + raise SystemExit("worker not found") + worker_container = containers[0] + + start_time = time.monotonic() + ids = [] + + def upload_one(): + with httpx.Client(timeout=30) as c: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments", + params={"filename": "bench.png"}, + content=data, + headers={"Authorization": "Bearer " + a["token"], "Content-Type": "image/png"}, + ) + if r.status_code == 202: + return r.json()["id"] + return None + + threads = [] + for _ in range(TOTAL_FILES): + t = threading.Thread(target=lambda: ids.append(upload_one())) + t.start() + threads.append(t) + if len(threads) >= CONCURRENCY: + for t in threads: t.join() + threads = [] + + for t in threads: t.join() + upload_time = time.monotonic() - start_time + ids = [i for i in ids if i] + print(f"Uploaded {len(ids)} files in {upload_time:.2f}s. RPS (upload): {len(ids) / upload_time:.2f}") + + ready_times = [] + start_wait = time.monotonic() + for att_id in ids: + with httpx.Client(timeout=10) as c: + while True: + r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers={"Authorization": "Bearer " + a["token"]}) + if r.status_code == 200 and r.json()["status"] == "ready": + ready_times.append(time.monotonic() - start_wait) + break + time.sleep(0.1) + + total_time = time.monotonic() - start_time + p95 = statistics.quantiles(ready_times, n=20)[18] if len(ready_times) > 1 else 0 + ram = _mem(worker_container) + + print(f"--- RESULTS (prefetch={prefetch}) ---") + print(f"Total time: {total_time:.2f}s") + print(f"Throughput: {len(ids) / total_time:.2f} files/sec") + print(f"p95 time to ready: {p95:.2f}s") + print(f"Worker RAM: {ram / 1024 ** 2:.1f} MiB") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/replay.py b/scripts/replay.py new file mode 100644 index 00000000..75df69ab --- /dev/null +++ b/scripts/replay.py @@ -0,0 +1,31 @@ +import argparse +import sys +import urllib.error +import urllib.request + + +def main() -> int: + parser = argparse.ArgumentParser(description="Replay a failed attachment") + parser.add_argument("attachment_id") + parser.add_argument("--base-url", default="http://localhost:8080") + parser.add_argument("--token", required=True) + args = parser.parse_args() + + req = urllib.request.Request( + f"{args.base_url}/api/v1/attachments/{args.attachment_id}/replay", + method="POST", + headers={"Authorization": "Bearer " + args.token}, + ) + try: + with urllib.request.urlopen(req, timeout=10) as resp: + print(f"status={resp.status}") + print(resp.read().decode()) + return 0 + except urllib.error.HTTPError as e: + print(f"status={e.code}") + print(e.read().decode()) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/check-balance.py b/scripts/scripts/check-balance.py new file mode 100644 index 00000000..4092857f --- /dev/null +++ b/scripts/scripts/check-balance.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +import threading +import time +import uuid +from collections import Counter + +import httpx + +BASE = "http://localhost:8080" + + +def main(): + counter = Counter() + lock = threading.Lock() + + def worker(): + with httpx.Client(timeout=5) as c: + for _ in range(20): + try: + r = c.get(f"{BASE}/health/live") + if r.status_code == 200: + with lock: + counter[r.headers.get("x-instance-id")] += 1 + except Exception: + pass + time.sleep(0.05) + + threads = [threading.Thread(target=worker) for _ in range(20)] + for t in threads: + t.start() + for t in threads: + t.join() + + print(counter) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/check-chat-validation.py b/scripts/scripts/check-chat-validation.py new file mode 100644 index 00000000..a5459a12 --- /dev/null +++ b/scripts/scripts/check-chat-validation.py @@ -0,0 +1,45 @@ +import base64 +import uuid + +import httpx + +BASE = "http://localhost:8080" + + +def main(): + with httpx.Client(timeout=10) as c: + username = "cv_" + uuid.uuid4().hex[:8] + password = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={ + "username": username, "display_name": "C", "password": password, + }) + credential = base64.b64encode((username + ":" + password).encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}).json() + alice_id = s["user"]["id"] + alice_token = s["token"] + + r = c.post( + f"{BASE}/api/v1/chats", + json={"title": "dup", "member_ids": [alice_id, alice_id]}, + headers={"Authorization": "Bearer " + alice_token}, + ) + print("duplicate members:", r.status_code, r.text[:200]) + + r = c.post( + f"{BASE}/api/v1/chats", + json={"title": "bad uuid", "member_ids": [alice_id, "not-a-uuid"]}, + headers={"Authorization": "Bearer " + alice_token}, + ) + print("invalid uuid:", r.status_code, r.text[:200]) + + r = c.post( + f"{BASE}/api/v1/chats", + json={"title": "ok", "member_ids": [alice_id]}, + headers={"Authorization": "Bearer " + alice_token}, + ) + print("valid chat:", r.status_code) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/check-models.py b/scripts/scripts/check-models.py new file mode 100644 index 00000000..e60836f0 --- /dev/null +++ b/scripts/scripts/check-models.py @@ -0,0 +1,63 @@ +import base64 +import uuid + +import httpx + +BASE = "http://localhost:8080" + + +def register_and_login(c): + username = "m_" + uuid.uuid4().hex[:8] + password = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={ + "username": username, "display_name": "T", "password": password, + }) + credential = base64.b64encode((username + ":" + password).encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}).json() + return s["user"]["id"], s["token"] + + +def main(): + with httpx.Client(timeout=10) as c: + alice_id, alice_token = register_and_login(c) + chat = c.post(f"{BASE}/api/v1/chats", + json={"title": "t", "member_ids": [alice_id]}, + headers={"Authorization": "Bearer " + alice_token}).json() + + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": " "}, + headers={ + "Authorization": "Bearer " + alice_token, + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + print("blank text:", r.status_code, r.text[:200]) + + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": "hi", "sender_id": "extra"}, + headers={ + "Authorization": "Bearer " + alice_token, + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + print("extra sender_id:", r.status_code, r.text[:200]) + + r = c.post( + f"{BASE}/api/v1/users", + json={"username": " ", "display_name": "X", "password": "Test-password-1234"}, + ) + print("blank username:", r.status_code, r.text[:200]) + + r = c.post( + f"{BASE}/api/v1/chats", + json={"title": " ", "member_ids": [alice_id]}, + headers={"Authorization": "Bearer " + alice_token}, + ) + print("blank chat title:", r.status_code, r.text[:200]) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/check_contract.py b/scripts/scripts/check_contract.py new file mode 100644 index 00000000..dcb34dfa --- /dev/null +++ b/scripts/scripts/check_contract.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Offline structural checks; full schema validation is a separate target.""" +import ast +import json +from pathlib import Path +import re + +ROOT = Path(__file__).resolve().parents[1] + +def main(): + lab = json.loads((ROOT/'lab.json').read_text())['number'] + spec = json.loads((ROOT/'contracts/openapi.json').read_text()) + assert spec['openapi']=='3.1.0' + assert spec['info']['version']=='1.%d.0'%lab + operation_ids = set() + def walk(value): + if isinstance(value,dict): + if '$ref' in value: + pointer = value['$ref'] + assert pointer.startswith('#/'), 'unexpected external reference' + target = spec + for part in pointer[2:].split('/'): + target = target[part.replace('~1','/').replace('~0','~')] + for v in value.values(): + walk(v) + elif isinstance(value,list): + for v in value: + walk(v) + walk(spec) + for route,methods in spec['paths'].items(): + placeholders = set(re.findall(r'\{([^}]+)\}',route)) + for method,op in methods.items(): + assert op['operationId'] not in operation_ids, 'duplicate operationId' + operation_ids.add(op['operationId']) + params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')} + assert placeholders==params, 'path parameter mismatch: '+route + for response in op['responses'].values(): + assert 'X-Request-Id' in response['headers'] + if lab>=3: + assert 'X-Instance-Id' in response['headers'] + assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2) + assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4) + for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')): + ast.parse(file.read_text(),filename=str(file)) + for file in ROOT.rglob('*.json'): + if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts): + continue + json.loads(file.read_text()) + for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'): + assert (ROOT/required).is_file(), 'missing '+required + print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.') + +if __name__=='__main__': + main() diff --git a/scripts/scripts/fair-scaling-benchmark.py b/scripts/scripts/fair-scaling-benchmark.py new file mode 100644 index 00000000..8576a614 --- /dev/null +++ b/scripts/scripts/fair-scaling-benchmark.py @@ -0,0 +1,230 @@ +#!/usr/bin/env python3 +import base64 +import json +import os +import subprocess +import sys +import threading +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" / "fair_scaling.json" + + +def register_and_login(client): + username = "fair_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "F", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode((username + ":" + password).encode()).decode() + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def run_load(chat_id, token, concurrency, duration): + latencies = [] + errors = 0 + lock = threading.Lock() + + def worker(): + nonlocal errors + local_lat = [] + local_err = 0 + with httpx.Client(timeout=15) as c: + deadline = time.monotonic() + duration + i = 0 + while time.monotonic() < deadline: + t0 = time.monotonic() + try: + if i % 10 == 0: + r = c.get( + f"{BASE}/api/v1/chats/{chat_id}/messages?limit=10", + headers={"Authorization": "Bearer " + token}, + ) + else: + r = c.post( + f"{BASE}/api/v1/chats/{chat_id}/messages", + json={"text": "fair-" + uuid.uuid4().hex[:6]}, + headers={ + "Authorization": "Bearer " + token, + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + if r.status_code not in (200, 201): + local_err += 1 + else: + local_lat.append(time.monotonic() - t0) + except Exception: + local_err += 1 + i += 1 + with lock: + latencies.extend(local_lat) + errors += local_err + + threads = [threading.Thread(target=worker) for _ in range(concurrency)] + started = time.monotonic() + for t in threads: + t.start() + for t in threads: + t.join() + elapsed = time.monotonic() - started + latencies.sort() + + def pct(p): + if not latencies: + return None + idx = max(0, int(len(latencies) * p) - 1) + return round(latencies[idx] * 1000, 2) + + return { + "concurrency": concurrency, + "duration_s": round(elapsed, 2), + "success": len(latencies), + "errors": errors, + "rps": round(len(latencies) / elapsed, 2) if elapsed else 0, + "p50_ms": pct(0.50), + "p95_ms": pct(0.95), + "p99_ms": pct(0.99), + } + + +def write_nginx(strategy): + if strategy == "least_conn": + upstream = """ upstream api_backend { + least_conn; + server api:8080 max_fails=1 fail_timeout=10s; + }""" + else: + upstream = """ upstream api_backend { + server api:8080 max_fails=1 fail_timeout=10s; + }""" + + content = f"""worker_processes auto; + +events {{ + worker_connections 1024; +}} + +http {{ +{upstream} + + server {{ + listen 8080; + server_name _; + + location / {{ + proxy_pass http://api_backend; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_connect_timeout 1s; + proxy_send_timeout 30s; + proxy_read_timeout 30s; + proxy_next_upstream error timeout http_502 http_503 http_504; + }} + }} +}} +""" + (ROOT / "nginx.conf").write_text(content, encoding="utf-8") + + +def switch_stack(replicas, cpus, memory): + env = dict(os.environ) + env["API_CPUS"] = cpus + env["API_MEMORY"] = memory + + subprocess.run( + ["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml", "down"], + cwd=ROOT, check=True, env=env, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + subprocess.run( + ["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml", + "up", "-d", "--build", "--scale", f"api={replicas}"], + cwd=ROOT, check=True, env=env, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + for _ in range(120): + try: + with httpx.Client(timeout=2) as c: + r = c.get(f"{BASE}/health/ready") + if r.status_code == 200: + time.sleep(8) + return + except Exception: + pass + time.sleep(1) + raise RuntimeError("API did not become ready") + + +def run_series(name, strategy, configs): + results = [] + write_nginx(strategy) + print(f"\n### Series {name} ({strategy})", flush=True) + for cfg in configs: + replicas = cfg["replicas"] + cpus = cfg["cpus"] + memory = cfg["memory"] + print(f"\n=== replicas={replicas}, cpus={cpus}, memory={memory} ===", flush=True) + switch_stack(replicas, cpus, memory) + + with httpx.Client(timeout=15) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "fair", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + for run in range(3): + print(f" run {run+1}/3...", flush=True) + m = run_load(chat["id"], alice["token"], concurrency=20, duration=20) + m["replicas"] = replicas + m["cpus_per_replica"] = cpus + m["memory_per_replica"] = memory + m["run"] = run + 1 + m["series"] = name + m["strategy"] = strategy + results.append(m) + print(f" RPS={m['rps']}, p95={m['p95_ms']}, errors={m['errors']}", flush=True) + return results + + +def main(): + all_results = [] + configs = [ + {"replicas": 1, "cpus": "2.0", "memory": "512M"}, + {"replicas": 2, "cpus": "1.0", "memory": "256M"}, + {"replicas": 4, "cpus": "0.5", "memory": "128M"}, + ] + + all_results.extend(run_series("A_least_conn", "least_conn", configs)) + all_results.extend(run_series("B_round_robin", "round_robin", configs)) + + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text( + json.dumps(all_results, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + print(f"\nSaved {len(all_results)} records to {EVIDENCE}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/measure-detection.py b/scripts/scripts/measure-detection.py new file mode 100644 index 00000000..b660008c --- /dev/null +++ b/scripts/scripts/measure-detection.py @@ -0,0 +1,201 @@ +#!/usr/bin/env python3 +import base64 +import json +import subprocess +import sys +import threading +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" / "detection.json" + +STOP_LABEL = "mtusi2026lab3-api-" + + +def register_and_login(client: httpx.Client) -> dict: + username = "det_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "D", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def find_container_by_instance(target: str) -> str: + names = subprocess.run( + ["docker", "ps", "--filter", f"name={STOP_LABEL}", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True, + ).stdout.split() + for name in names: + result = subprocess.run( + ["docker", "logs", name], + capture_output=True, text=True, check=False, + ) + combined = result.stdout + result.stderr + if f"INSTANCE_ID={target}" in combined: + return name + raise RuntimeError(f"no container with INSTANCE_ID={target}") + + +def get_instances(n: int = 30) -> set: + seen = set() + with httpx.Client(timeout=2) as c: + for _ in range(n): + try: + r = c.get(f"{BASE}/health/live") + if r.status_code == 200: + seen.add(r.headers.get("x-instance-id")) + except Exception: + pass + return seen + + +def main(): + print("[measure] preparing user and chat", flush=True) + with httpx.Client(timeout=5) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "det", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + instances_before = get_instances() + if len(instances_before) < 2: + print(f"[measure] need >=2 replicas, got {instances_before}", file=sys.stderr) + return 1 + print(f"[measure] instances before: {instances_before}", flush=True) + + target = sorted(instances_before)[0] + container = find_container_by_instance(target) + print(f"[measure] will stop {container} (instance={target})", flush=True) + + shared = { + "latencies": [], + "errors": 0, + "instances": [], + "stop_time": None, + "first_success_after_stop": None, + "first_success_instance": None, + "stop_seen": False, + } + lock = threading.Lock() + running = {"flag": True} + + def worker(): + with httpx.Client(timeout=3) as c: + while running["flag"]: + t0 = time.monotonic() + try: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": "det-" + uuid.uuid4().hex[:8]}, + headers={ + "Authorization": "Bearer " + alice["token"], + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + dt = time.monotonic() - t0 + if r.status_code == 201: + inst = r.headers.get("x-instance-id") + with lock: + shared["latencies"].append(dt) + shared["instances"].append(inst) + if shared["stop_time"] is not None and not shared["stop_seen"]: + if inst != target: + shared["first_success_after_stop"] = time.monotonic() - shared["stop_time"] + shared["first_success_instance"] = inst + shared["stop_seen"] = True + else: + with lock: + shared["errors"] += 1 + except Exception: + with lock: + shared["errors"] += 1 + + print("[measure] starting background load", flush=True) + threads = [threading.Thread(target=worker) for _ in range(4)] + for t in threads: + t.start() + + time.sleep(3) + with lock: + baseline_count = len(shared["latencies"]) + baseline_rps = baseline_count / 3.0 + print(f"[measure] baseline RPS ~{baseline_rps:.1f}", flush=True) + + print(f"[measure] stopping {container}", flush=True) + with lock: + shared["stop_time"] = time.monotonic() + subprocess.run(["docker", "stop", "--time=2", container], check=True) + + deadline = time.monotonic() + 20 + while time.monotonic() < deadline: + with lock: + if shared["first_success_after_stop"] is not None: + break + time.sleep(0.05) + + time.sleep(2) + + running["flag"] = False + for t in threads: + t.join(timeout=5) + + with lock: + report = { + "target_instance": target, + "target_container": container, + "instances_before": list(instances_before), + "total_successful": len(shared["latencies"]), + "total_errors": shared["errors"], + "baseline_rps_3s": round(baseline_rps, 2), + "detection_seconds": ( + round(shared["first_success_after_stop"], 3) + if shared["first_success_after_stop"] else None + ), + "first_success_instance_after_stop": shared["first_success_instance"], + } + print(json.dumps(report, ensure_ascii=False, indent=2)) + + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + existing = [] + if EVIDENCE.exists(): + try: + existing = json.loads(EVIDENCE.read_text(encoding="utf-8")) + except json.JSONDecodeError: + existing = [] + existing.append(report) + EVIDENCE.write_text( + json.dumps(existing, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + + print(f"[measure] restoring {container}", flush=True) + subprocess.run( + ["docker", "compose", "--env-file", ".env", "up", "-d", "--scale", "api=2"], + cwd=ROOT, check=True, + ) + time.sleep(5) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/plot-scaling.py b/scripts/scripts/plot-scaling.py new file mode 100644 index 00000000..6fbd70bb --- /dev/null +++ b/scripts/scripts/plot-scaling.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +from pathlib import Path + +import plotly.graph_objects as go + +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" + + +def make_rps_chart(): + fig = go.Figure() + fig.add_trace(go.Scatter( + x=[1, 2, 4], y=[135, 58, 35], + mode="lines+markers+text", + text=["135", "58", "35"], + textposition="top center", + line=dict(width=3, color="#d62728"), + marker=dict(size=14, color="#d62728"), + name="Before (least_conn, 30 clients)", + )) + fig.add_trace(go.Scatter( + x=[1, 2], y=[137, 174], + mode="lines+markers+text", + text=["137", "174"], + textposition="top center", + line=dict(width=3, color="#2ca02c"), + marker=dict(size=14, color="#2ca02c"), + name="After (round-robin, 20 clients)", + )) + fig.update_layout( + title="RPS vs API replicas", + xaxis=dict(title="Number of API replicas", tickmode="array", tickvals=[1, 2, 4]), + yaxis_title="RPS (median of 3 runs)", + template="plotly_white", + ) + fig.write_image(str(EVIDENCE / "scaling-rps.png")) + + +def make_p95_chart(): + fig = go.Figure() + fig.add_trace(go.Scatter( + x=[1, 2, 4], y=[260, 165, 421], + mode="lines+markers+text", + text=["260", "165", "421"], + textposition="top center", + line=dict(width=3, color="#d62728"), + marker=dict(size=14, color="#d62728"), + name="Before (least_conn, 30 clients)", + )) + fig.add_trace(go.Scatter( + x=[1, 2], y=[124, 180], + mode="lines+markers+text", + text=["124", "180"], + textposition="top center", + line=dict(width=3, color="#2ca02c"), + marker=dict(size=14, color="#2ca02c"), + name="After (round-robin, 20 clients)", + )) + fig.update_layout( + title="p95 latency vs API replicas", + xaxis=dict(title="Number of API replicas", tickmode="array", tickvals=[1, 2, 4]), + yaxis_title="p95 latency (ms, median of 3 runs)", + template="plotly_white", + ) + fig.write_image(str(EVIDENCE / "scaling-p95.png")) + + +def main(): + make_rps_chart() + make_p95_chart() + print(f"Saved charts to {EVIDENCE}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/presign_compare.py b/scripts/scripts/presign_compare.py new file mode 100644 index 00000000..5ae7ec9e --- /dev/null +++ b/scripts/scripts/presign_compare.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 +"""Compare proxy vs presigned upload: API RAM peak and bytes through API.""" +import base64 +import io +import subprocess +import threading +import time +import uuid + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +SIZES = [1 * 1024 * 1024, 9 * 1024 * 1024] + + +def _png(size_bytes): + img = Image.new("RGB", (1000, 1000), (0, 0, 255)) + buf = io.BytesIO() + img.save(buf, format="PNG") + data = buf.getvalue() + if len(data) < size_bytes: + data = data + b"\x00" * (size_bytes - len(data)) + return data[:size_bytes] + + +def _user(c): + u = "cmp_" + uuid.uuid4().hex[:8] + p = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "C", "password": p}) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": s["user"]["id"], "token": s["token"]} + + +def _chat(c, a, b): + return c.post( + f"{BASE}/api/v1/chats", + json={"title": "cmp", "member_ids": [a["id"], b["id"]]}, + headers={"Authorization": "Bearer " + a["token"]}, + ).json() + + +def _mem(container): + out = subprocess.run( + ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container], + capture_output=True, text=True, check=True, + ).stdout.strip() + v = out.split("/")[0].strip() + units = { + "B": 1, + "KiB": 1024, "Ki": 1024, + "MiB": 1024**2, "Mi": 1024**2, + "GiB": 1024**3, "Gi": 1024**3, + } + for u in sorted(units, key=len, reverse=True): + if v.endswith(u): + return int(float(v[: -len(u)]) * units[u]) + return 0 + + +def _measure(container, fn): + baseline = _mem(container) + peak = baseline + stop = {"flag": False} + + def sampler(): + nonlocal peak + while not stop["flag"]: + try: + m = _mem(container) + if m > peak: + peak = m + except Exception: + pass + time.sleep(0.15) + + t = threading.Thread(target=sampler) + t.start() + started = time.monotonic() + fn() + elapsed = time.monotonic() - started + stop["flag"] = True + t.join(timeout=5) + return baseline, peak, elapsed + + +def main(): + containers = subprocess.run( + ["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True, + ).stdout.split() + if not containers: + raise SystemExit("no api containers found") + target = containers[0] + + with httpx.Client(timeout=60) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + + for size in SIZES: + data = _png(size) + + def proxy_upload(): + with httpx.Client(timeout=60) as c: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments", + params={"filename": "x.png"}, + content=data, + headers={"Authorization": "Bearer " + a["token"], "Content-Type": "image/png"}, + ) + assert r.status_code == 201, r.text + + def presigned_upload(): + with httpx.Client(timeout=60) as c: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign", + params={"filename": "x.png", "size": len(data), "content_type": "image/png"}, + headers={"Authorization": "Bearer " + a["token"]}, + ) + assert r.status_code == 201, r.text + p = r.json() + put = httpx.put(p["upload_url"], content=data, headers={"Content-Type": "image/png"}, timeout=60) + assert put.status_code == 200, put.text + r = c.post( + f"{BASE}/api/v1/attachments/{p['attachment_id']}/complete", + headers={"Authorization": "Bearer " + a["token"]}, + ) + assert r.status_code == 201, r.text + + for label, fn in (("proxy", proxy_upload), ("presigned", presigned_upload)): + baseline, peak, elapsed = _measure(target, fn) + print( + f"size={size} mode={label} " + f"elapsed={elapsed:.2f}s " + f"api_ram_baseline={baseline/1024**2:.1f}MiB " + f"api_ram_peak={peak/1024**2:.1f}MiB " + f"delta={(peak-baseline)/1024**2:.1f}MiB" + ) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/ram_experiment.py b/scripts/scripts/ram_experiment.py new file mode 100644 index 00000000..8cc2ca39 --- /dev/null +++ b/scripts/scripts/ram_experiment.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +"""Measure API RAM peak under >=10 concurrent uploads via docker stats.""" +import base64 +import io +import subprocess +import threading +import time +import uuid + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +CONCURRENCY = 10 +UPLOAD_BYTES = 9 * 1024 * 1024 + + +def _png(size_bytes: int) -> bytes: + img = Image.new("RGB", (1000, 1000), (255, 0, 0)) + buf = io.BytesIO() + img.save(buf, format="PNG") + data = buf.getvalue() + if len(data) < size_bytes: + data = data + b"\x00" * (size_bytes - len(data)) + return data[:size_bytes] + + +def _register_and_login(c): + u = "ram_" + uuid.uuid4().hex[:8] + p = "Test-password-1234" + r = c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "R", "password": p}) + assert r.status_code == 201, r.text + user = r.json() + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": user["id"], "token": s["token"]} + + +def _stats_mem(container: str) -> int: + out = subprocess.run( + ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container], + capture_output=True, text=True, check=True, + ).stdout.strip() + value = out.split("/")[0].strip() + units = { + "B": 1, + "KiB": 1024, "Ki": 1024, + "MiB": 1024**2, "Mi": 1024**2, + "GiB": 1024**3, "Gi": 1024**3, + } + for u in sorted(units, key=len, reverse=True): + if value.endswith(u): + return int(float(value[: -len(u)]) * units[u]) + raise ValueError(f"unexpected docker stats format: {out!r}") + + +def main(): + data = _png(UPLOAD_BYTES) + with httpx.Client(timeout=30) as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "ram", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + containers = subprocess.run( + ["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True, + ).stdout.split() + if not containers: + raise SystemExit("no api containers found") + target = containers[0] + baseline = _stats_mem(target) + + errors = [] + lock = threading.Lock() + + def worker(): + try: + with httpx.Client(timeout=60) as c: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments", + params={"filename": "ram.png"}, + content=data, + headers={ + "Authorization": "Bearer " + alice["token"], + "Content-Type": "image/png", + }, + ) + if r.status_code != 201: + with lock: + errors.append((r.status_code, r.text[:200])) + except Exception as e: + with lock: + errors.append((0, str(e))) + + threads = [threading.Thread(target=worker) for _ in range(CONCURRENCY)] + peak = baseline + stop = {"flag": False} + + def sampler(): + nonlocal peak + while not stop["flag"]: + try: + m = _stats_mem(target) + if m > peak: + peak = m + except Exception: + pass + time.sleep(0.2) + + sampler_thread = threading.Thread(target=sampler) + sampler_thread.start() + started = time.monotonic() + for t in threads: + t.start() + for t in threads: + t.join() + elapsed = time.monotonic() - started + stop["flag"] = True + sampler_thread.join(timeout=5) + + print(f"container: {target}") + print(f"uploads: {CONCURRENCY} x {UPLOAD_BYTES} bytes") + print(f"elapsed: {elapsed:.2f}s") + print(f"errors: {len(errors)}") + for e in errors: + print(" error:", e) + print(f"baseline RAM: {baseline / 1024**2:.1f} MiB") + print(f"peak RAM: {peak / 1024**2:.1f} MiB") + print(f"delta: {(peak - baseline) / 1024**2:.1f} MiB") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/scripts/reconcile.py b/scripts/scripts/reconcile.py new file mode 100644 index 00000000..2e4eca33 --- /dev/null +++ b/scripts/scripts/reconcile.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Find and clean orphan S3 objects older than GRACE; log metadata without object. + +Usage (inside api container): + docker compose exec -T api-1 python /tmp/reconcile.py --dry-run + docker compose exec -T api-1 python /tmp/reconcile.py +""" +import argparse +import asyncio +import os +from datetime import datetime, timedelta, timezone + +import asyncpg +from minio import Minio + +GRACE_HOURS = 1 +BUCKET = os.getenv("S3_BUCKET", "attachments") +DB_URL = os.getenv( + "DATABASE_URL", + "postgresql://messenger:messenger@postgres:5432/messenger", +) +S3 = Minio( + os.getenv("S3_ENDPOINT", "minio:9000"), + access_key=os.getenv("S3_ACCESS_KEY", "api-user"), + secret_key=os.getenv("S3_SECRET_KEY", "api-user-secret-123"), + secure=False, +) + + +async def main(dry_run: bool) -> int: + pool = await asyncpg.create_pool(DB_URL) + try: + async with pool.acquire() as conn: + rows = await conn.fetch("SELECT object_key FROM attachments") + known = {r["object_key"] for r in rows} + + cutoff = datetime.now(timezone.utc) - timedelta(hours=GRACE_HOURS) + removed = 0 + skipped_fresh = 0 + for obj in S3.list_objects(BUCKET, recursive=True): + if obj.object_name in known: + continue + if obj.last_modified and obj.last_modified > cutoff: + skipped_fresh += 1 + continue + print(f"orphan: {obj.object_name}") + if not dry_run: + S3.remove_object(BUCKET, obj.object_name) + removed += 1 + + missing = 0 + async with pool.acquire() as conn: + rows = await conn.fetch("SELECT id, object_key FROM attachments") + for r in rows: + try: + S3.stat_object(BUCKET, r["object_key"]) + except Exception: + missing += 1 + print(f"missing object for attachment {r['id']}: {r['object_key']}") + + mode = "dry-run" if dry_run else "apply" + print( + f"reconcile ({mode}): orphans_found={removed} " + f"fresh_skipped={skipped_fresh} metadata_without_object={missing}" + ) + return 0 + finally: + await pool.close() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--dry-run", action="store_true") + args = parser.parse_args() + raise SystemExit(asyncio.run(main(args.dry_run))) \ No newline at end of file diff --git a/scripts/scripts/restart.cmd b/scripts/scripts/restart.cmd new file mode 100644 index 00000000..09abb2c7 --- /dev/null +++ b/scripts/scripts/restart.cmd @@ -0,0 +1,2 @@ +@echo off +python "%~dp0restart.py" %* \ No newline at end of file diff --git a/scripts/scripts/restart.example.sh b/scripts/scripts/restart.example.sh new file mode 100644 index 00000000..967e9512 --- /dev/null +++ b/scripts/scripts/restart.example.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu +# TODO: copy to restart.sh and implement using YOUR compose/stack services. +# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable). +# Preserve persistent volumes and schema/data; never down -v / flush / re-seed. +# Allow operator unseal if required in lab7; do not print credentials. +# Exit 0 only after the intended action; test waits for API readiness separately. +printf '%s +' 'TODO: implement restart.sh for your infrastructure' >&2 +exit 2 diff --git a/scripts/scripts/restart.py b/scripts/scripts/restart.py new file mode 100644 index 00000000..90814d5e --- /dev/null +++ b/scripts/scripts/restart.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +import subprocess +import sys +import time +import urllib.request +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +BASE_URL = "http://localhost:8080" + + +def run(cmd): + print(f"[restart] $ {' '.join(cmd)}", flush=True) + result = subprocess.run(cmd, cwd=ROOT) + if result.returncode != 0: + print( + f"[restart] command failed with code {result.returncode}", + file=sys.stderr, + ) + sys.exit(result.returncode) + + +def main(): + run(["docker", "compose", "--env-file", ".env", "down"]) + run([ + "docker", "compose", "--env-file", ".env", + "up", "-d", "--build", "--scale", "api=2", + ]) + + print("[restart] waiting for /health/ready", flush=True) + deadline = time.monotonic() + 90 + while time.monotonic() < deadline: + try: + with urllib.request.urlopen( + f"{BASE_URL}/health/ready", timeout=2 + ) as r: + if r.status == 200: + print("[restart] api is ready", flush=True) + return 0 + except Exception: + time.sleep(1) + print( + "[restart] api did not become ready in 90 seconds", + file=sys.stderr, + ) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/restart.sh b/scripts/scripts/restart.sh new file mode 100644 index 00000000..d5c13130 --- /dev/null +++ b/scripts/scripts/restart.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")/.." + +echo "[restart.sh] docker compose down (volumes preserved)" +docker compose --env-file .env down + +echo "[restart.sh] docker compose up -d --build --scale api=2" +docker compose --env-file .env up -d --build --scale api=2 + +echo "[restart.sh] waiting for /health/ready" +for i in $(seq 1 90); do + if curl -fsS http://localhost:8080/health/ready >/dev/null 2>&1; then + echo "[restart.sh] api is ready" + exit 0 + fi + sleep 1 +done + +echo "[restart.sh] api did not become ready in 90 seconds" >&2 +exit 1 \ No newline at end of file diff --git a/scripts/scripts/rolling-update.py b/scripts/scripts/rolling-update.py new file mode 100644 index 00000000..069321e7 --- /dev/null +++ b/scripts/scripts/rolling-update.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +import base64 +import json +import subprocess +import sys +import threading +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" / "rolling-update.json" + + +def register_and_login(client): + username = "roll_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "R", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode((username + ":" + password).encode()).decode() + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def main(): + with httpx.Client(timeout=5) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "roll", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + running = {"flag": True} + latencies = [] + errors = 0 + lock = threading.Lock() + + def worker(): + nonlocal errors + with httpx.Client(timeout=3) as c: + while running["flag"]: + t0 = time.monotonic() + try: + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": "roll-" + uuid.uuid4().hex[:8]}, + headers={ + "Authorization": "Bearer " + alice["token"], + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + dt = time.monotonic() - t0 + if r.status_code == 201: + with lock: + latencies.append(dt) + else: + with lock: + errors += 1 + except Exception: + with lock: + errors += 1 + + threads = [threading.Thread(target=worker) for _ in range(4)] + for t in threads: + t.start() + + time.sleep(3) + with lock: + before = len(latencies) + print(f"[roll] baseline RPS ~{before/3:.1f}", flush=True) + + for replica_name in ["mtusi2026lab3-api-1", "mtusi2026lab3-api-2"]: + print(f"[roll] restarting {replica_name}", flush=True) + subprocess.run(["docker", "restart", replica_name], check=True) + time.sleep(8) + + time.sleep(2) + + running["flag"] = False + for t in threads: + t.join(timeout=5) + + with lock: + report = { + "total_successful": len(latencies), + "total_errors": errors, + "baseline_rps_3s": round(before / 3.0, 2), + "avg_latency_ms": round(1000 * sum(latencies) / len(latencies), 2) if latencies else None, + } + print(json.dumps(report, indent=2, ensure_ascii=False)) + + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/scaling-benchmark.py b/scripts/scripts/scaling-benchmark.py new file mode 100644 index 00000000..7d23f65a --- /dev/null +++ b/scripts/scripts/scaling-benchmark.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +import base64 +import json +import os +import subprocess +import sys +import threading +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" / "scaling.json" + + +def register_and_login(client): + username = "scale_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "S", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode((username + ":" + password).encode()).decode() + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def run_load(chat_id: str, token: str, concurrency: int, duration: float): + latencies = [] + errors = 0 + lock = threading.Lock() + + def worker(): + nonlocal errors + local_lat = [] + local_err = 0 + with httpx.Client(timeout=15) as c: + deadline = time.monotonic() + duration + i = 0 + while time.monotonic() < deadline: + t0 = time.monotonic() + try: + if i % 10 == 0: + r = c.get( + f"{BASE}/api/v1/chats/{chat_id}/messages?limit=10", + headers={"Authorization": "Bearer " + token}, + ) + else: + r = c.post( + f"{BASE}/api/v1/chats/{chat_id}/messages", + json={"text": "s-" + uuid.uuid4().hex[:6]}, + headers={ + "Authorization": "Bearer " + token, + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + if r.status_code not in (200, 201): + local_err += 1 + else: + local_lat.append(time.monotonic() - t0) + except Exception: + local_err += 1 + i += 1 + with lock: + latencies.extend(local_lat) + errors += local_err + + threads = [threading.Thread(target=worker) for _ in range(concurrency)] + started = time.monotonic() + for t in threads: + t.start() + for t in threads: + t.join() + elapsed = time.monotonic() - started + + latencies.sort() + + def pct(p): + if not latencies: + return None + idx = max(0, int(len(latencies) * p) - 1) + return round(latencies[idx] * 1000, 2) + + return { + "concurrency": concurrency, + "duration_s": round(elapsed, 2), + "success": len(latencies), + "errors": errors, + "rps": round(len(latencies) / elapsed, 2) if elapsed else 0, + "p50_ms": pct(0.50), + "p95_ms": pct(0.95), + "p99_ms": pct(0.99), + } + + +def switch_stack(replicas: int, cpus: str, memory: str): + env = dict(os.environ) + env["API_CPUS"] = cpus + env["API_MEMORY"] = memory + + subprocess.run( + ["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml", "down"], + cwd=ROOT, check=True, env=env, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + subprocess.run( + ["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml", + "up", "-d", "--build", "--scale", f"api={replicas}"], + cwd=ROOT, check=True, env=env, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + + for _ in range(120): + try: + with httpx.Client(timeout=2) as c: + r = c.get(f"{BASE}/health/ready") + if r.status_code == 200: + time.sleep(8) + return + except Exception: + pass + time.sleep(1) + raise RuntimeError("API did not become ready") + + +def experiment_series(name, configurations, concurrency): + results = [] + for cfg in configurations: + replicas = cfg["replicas"] + cpus = cfg["cpus"] + memory = cfg["memory"] + print(f"\n=== {name}: replicas={replicas}, cpus={cpus}, memory={memory}, clients={concurrency} ===", flush=True) + switch_stack(replicas, cpus, memory) + + with httpx.Client(timeout=15) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "scale", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + for run in range(3): + print(f" run {run+1}/3...", flush=True) + m = run_load(chat["id"], alice["token"], concurrency=concurrency, duration=20) + m["replicas"] = replicas + m["cpus_per_replica"] = cpus + m["memory_per_replica"] = memory + m["run"] = run + 1 + m["series"] = name + results.append(m) + print(f" RPS={m['rps']}, p95={m['p95_ms']}, errors={m['errors']}", flush=True) + + return results + + +def main(): + all_results = [] + + print("### Baseline: 1 replica, 2 CPU, 20 clients", flush=True) + all_results.extend(experiment_series( + "baseline", + [{"replicas": 1, "cpus": "2.0", "memory": "512M"}], + concurrency=20, + )) + + print("\n### Improved: 2 replicas, 1 CPU, 20 clients", flush=True) + all_results.extend(experiment_series( + "improved", + [{"replicas": 2, "cpus": "1.0", "memory": "256M"}], + concurrency=20, + )) + + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text( + json.dumps(all_results, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + print(f"\nSaved {len(all_results)} records to {EVIDENCE}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/stop-one.cmd b/scripts/scripts/stop-one.cmd new file mode 100644 index 00000000..ba5c9650 --- /dev/null +++ b/scripts/scripts/stop-one.cmd @@ -0,0 +1,2 @@ +@echo off +python "%~dp0stop-one.py" %* \ No newline at end of file diff --git a/scripts/scripts/stop-one.example.sh b/scripts/scripts/stop-one.example.sh new file mode 100644 index 00000000..ae149e89 --- /dev/null +++ b/scripts/scripts/stop-one.example.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu +# TODO: copy to stop-one.sh, map TARGET_INSTANCE_ID to ONE API container/task. +# Stop that instance and leave it stopped; no stop of DB/LB/volumes. +# Return quickly (within 10 s); the test sends requests during this script. +# Restore the replica explicitly after the test. Never print credentials. +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" +printf '%s +' 'TODO: implement stop-one.sh for your infrastructure' >&2 +exit 2 diff --git a/scripts/scripts/stop-one.py b/scripts/scripts/stop-one.py new file mode 100644 index 00000000..e8365011 --- /dev/null +++ b/scripts/scripts/stop-one.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import os +import subprocess +import sys + + +def main(): + target = os.environ.get("TARGET_INSTANCE_ID") + if not target: + print("[stop-one] TARGET_INSTANCE_ID not set", file=sys.stderr) + return 1 + + names = subprocess.run( + ["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"], + capture_output=True, text=True, check=True, + ).stdout.split() + + for container in names: + logs = subprocess.run( + ["docker", "logs", container], + capture_output=True, text=True, check=False, + ) + combined = logs.stdout + logs.stderr + if f"INSTANCE_ID={target}" in combined: + print(f"[stop-one] stopping {container} (instance={target})", flush=True) + subprocess.run(["docker", "stop", "--time=2", container], check=True) + return 0 + + print(f"[stop-one] no container found for INSTANCE_ID={target}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/stop-one.sh b/scripts/scripts/stop-one.sh new file mode 100644 index 00000000..5c084f9a --- /dev/null +++ b/scripts/scripts/stop-one.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -euo pipefail + +TARGET="${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID not set}" + +for container in $(docker ps --filter "name=mtusi2026lab4-api-" --format "{{.Names}}"); do + if docker logs "$container" 2>&1 | grep -q "INSTANCE_ID=$TARGET"; then + echo "[stop-one] stopping $container (instance=$TARGET)" + docker stop "$container" + exit 0 + fi +done + +echo "[stop-one] no container found for INSTANCE_ID=$TARGET" >&2 +exit 1 \ No newline at end of file diff --git a/scripts/scripts/test-idempotency.py b/scripts/scripts/test-idempotency.py new file mode 100644 index 00000000..cb5e2a5c --- /dev/null +++ b/scripts/scripts/test-idempotency.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +import base64 +import json +import sys +import threading +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +ROOT = Path(__file__).resolve().parent.parent +EVIDENCE = ROOT / "evidence" / "idempotency.json" + + +def register_and_login(client): + username = "idem_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "I", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode((username + ":" + password).encode()).decode() + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def main(): + with httpx.Client(timeout=10) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "idem", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + key = uuid.uuid4().hex + text = "idempotency-test-" + uuid.uuid4().hex[:8] + results = [] + lock = threading.Lock() + barrier = threading.Barrier(10) + + def worker(): + try: + with httpx.Client(timeout=10) as c: + barrier.wait() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": text}, + headers={ + "Authorization": "Bearer " + alice["token"], + "Idempotency-Key": key, + }, + ) + with lock: + results.append((r.status_code, r.headers.get("x-instance-id"))) + except Exception as e: + with lock: + results.append((0, str(e))) + + threads = [threading.Thread(target=worker) for _ in range(10)] + for t in threads: + t.start() + for t in threads: + t.join() + + time.sleep(1) + + with httpx.Client(timeout=10) as c: + page = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=100", + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + matching = [m for m in page["items"] if m["text"] == text] + message_ids = {m["id"] for m in matching} + instances_seen = list({r[1] for r in results if r[1] and r[0] > 0}) + statuses = [r[0] for r in results] + + report = { + "concurrent_requests": len(results), + "statuses": statuses, + "unique_instances": instances_seen, + "messages_created": len(matching), + "unique_message_ids": len(message_ids), + "idempotency_ok": len(matching) == 1, + "instances_count": len(instances_seen), + } + print(json.dumps(report, indent=2, ensure_ascii=False)) + + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file diff --git a/scripts/scripts/validate_openapi.py b/scripts/scripts/validate_openapi.py new file mode 100644 index 00000000..1dfe4a98 --- /dev/null +++ b/scripts/scripts/validate_openapi.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first.""" +import json +from pathlib import Path +from openapi_spec_validator import validate +from jsonschema import Draft202012Validator + +root = Path(__file__).resolve().parents[1] +spec = json.loads((root/'contracts/openapi.json').read_text()) +validate(spec) +from referencing import Registry, Resource +# The base URI resolves local #/components references inside examples. +resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'}) +registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource)) +for name, example in json.loads((root/'contracts/examples.json').read_text()).items(): + schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name} + Draft202012Validator(schema, registry=registry).validate(example) +for path in (root/'contracts').glob('*.schema.json'): + schema = json.loads(path.read_text()) + Draft202012Validator.check_schema(schema) + for example in schema.get('examples',[]): + Draft202012Validator(schema).validate(example) +print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.') diff --git a/student-tests/__init__.py b/student-tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/student-tests/benchmark.py b/student-tests/benchmark.py new file mode 100644 index 00000000..2b5e843a --- /dev/null +++ b/student-tests/benchmark.py @@ -0,0 +1,117 @@ +import json +import time +import uuid +from concurrent.futures import ThreadPoolExecutor +import httpx + +BASE = "http://localhost:8080" + + +def register(client: httpx.Client) -> dict: + username = "u_" + uuid.uuid4().hex[:10] + return client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "Bench"}, + ).json() + + +def setup(history_size: int): + with httpx.Client(timeout=120) as c: + alice = register(c) + bob = register(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "bench", "member_ids": [alice["id"], bob["id"]]}, + headers={"X-User-Id": alice["id"]}, + ).json() + for i in range(history_size): + c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"seed-{i}"}, + headers={"X-User-Id": alice["id"]}, + ) + return alice, bob, chat + + +def measure(chat_id: str, actor_id: str, concurrency: int, duration: float = 5.0): + latencies = [] + errors = 0 + lock = __import__("threading").Lock() + + def worker(worker_id): + nonlocal errors + local_lat = [] + local_err = 0 + with httpx.Client(timeout=10) as c: + deadline = time.monotonic() + duration + i = 0 + while time.monotonic() < deadline: + t0 = time.monotonic() + try: + if i % 10 == 0: + r = c.get( + f"{BASE}/api/v1/chats/{chat_id}/messages?limit=50", + headers={"X-User-Id": actor_id}, + ) + else: + r = c.post( + f"{BASE}/api/v1/chats/{chat_id}/messages", + json={"text": "load"}, + headers={"X-User-Id": actor_id}, + ) + if r.status_code not in (200, 201): + local_err += 1 + else: + local_lat.append(time.monotonic() - t0) + except Exception: + local_err += 1 + i += 1 + with lock: + latencies.extend(local_lat) + errors += local_err + + t_start = time.monotonic() + with ThreadPoolExecutor(max_workers=concurrency) as pool: + list(pool.map(lambda w: worker(w), range(concurrency))) + elapsed = time.monotonic() - t_start + + latencies.sort() + + def pct(p): + if not latencies: + return None + idx = max(0, int(len(latencies) * p) - 1) + return round(latencies[idx] * 1000, 3) + + rps = len(latencies) / elapsed if elapsed else 0 + return { + "concurrency": concurrency, + "elapsed_s": round(elapsed, 2), + "success": len(latencies), + "errors": errors, + "rps": round(rps, 2), + "p50_ms": pct(0.50), + "p95_ms": pct(0.95), + "p99_ms": pct(0.99), + } + + +def main(): + results = [] + for history in (100, 1000, 10000): + print(f"Setup history={history}...") + alice, _, chat = setup(history) + for conc in (1, 10, 50): + print(f" measure history={history} concurrency={conc}...") + m = measure(chat["id"], alice["id"], conc, duration=5.0) + m["history_size"] = history + results.append(m) + print(" ", json.dumps(m, ensure_ascii=False)) + + with open("benchmark_results.json", "w", encoding="utf-8") as f: + json.dump(results, f, ensure_ascii=False, indent=2) + print("Saved to benchmark_results.json") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/cache_experiment.py b/student-tests/cache_experiment.py new file mode 100644 index 00000000..83b60345 --- /dev/null +++ b/student-tests/cache_experiment.py @@ -0,0 +1,176 @@ +"""Cache stampede experiment. + +Measures SQL calls and latency for parallel GET /messages with cold cache. +Run twice: before and after single-flight protection. +""" +import base64 +import json +import os +import subprocess +import time +import uuid +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +EVIDENCE = Path(__file__).resolve().parent.parent / "evidence" / "cache_experiment.json" + + +def register_and_login(client: httpx.Client) -> dict: + username = "cache_" + uuid.uuid4().hex[:10] + password = "Test-password-1234" + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Cache", + "password": password, + }, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def auth(actor: dict) -> dict: + return {"Authorization": "Bearer " + actor["token"]} + + +def sql_calls_for_query(substring: str) -> int: + """Count SQL calls from pg_stat_statements matching substring.""" + sql = ( + "SELECT COALESCE(SUM(calls), 0) FROM pg_stat_statements " + f"WHERE query ILIKE '%{substring}%';" + ) + result = subprocess.run( + [ + "docker", "compose", "exec", "-T", "postgres", + "psql", "-U", "messenger", "-d", "messenger", + "-t", "-A", "-c", sql, + ], + capture_output=True, text=True, check=True, + ) + return int(result.stdout.strip() or "0") + + +def reset_pg_stats() -> None: + subprocess.run( + [ + "docker", "compose", "exec", "-T", "postgres", + "psql", "-U", "messenger", "-d", "messenger", + "-c", "SELECT pg_stat_statements_reset();", + ], + capture_output=True, text=True, check=True, + ) + + +def flush_cache() -> None: + subprocess.run( + [ + "docker", "compose", "exec", "-T", "valkey", + "sh", "-c", + "valkey-cli --scan --pattern 'messages:*' | xargs -r valkey-cli DEL", + ], + capture_output=True, text=True, check=True, + ) + +def run_parallel_gets(chat_id: str, token: str, concurrency: int = 50): + """Fire N parallel GET requests; return list of latencies and statuses.""" + results = [] + + def worker(): + with httpx.Client(timeout=15) as c: + t0 = time.monotonic() + r = c.get( + f"{BASE}/api/v1/chats/{chat_id}/messages?limit=50", + headers={"Authorization": "Bearer " + token}, + ) + dt = time.monotonic() - t0 + return dt, r.status_code + + with ThreadPoolExecutor(max_workers=concurrency) as pool: + results = list(pool.map(lambda _: worker(), range(concurrency))) + return results + + +def main(): + label = os.getenv("LABEL", "before") + concurrency = int(os.getenv("CONCURRENCY", "50")) + + with httpx.Client(timeout=30) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "cache", "member_ids": [alice["id"], bob["id"]]}, + headers=auth(alice), + ).json() + for i in range(100): + c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"seed-{i:03d}"}, + headers=auth(alice), + ) + + flush_cache() + reset_pg_stats() + time.sleep(0.5) + + print(f"Running {concurrency} parallel GETs (label={label})...") + started = time.monotonic() + results = run_parallel_gets(chat["id"], alice["token"], concurrency) + elapsed = time.monotonic() - started + + latencies = sorted(dt for dt, _ in results) + statuses = [s for _, s in results] + ok_count = sum(1 for s in statuses if s == 200) + errors = concurrency - ok_count + + sql_calls = sql_calls_for_query("FROM messages") + + def pct(p): + idx = max(0, int(len(latencies) * p) - 1) + return round(latencies[idx] * 1000, 2) + + report = { + "label": label, + "concurrency": concurrency, + "elapsed_s": round(elapsed, 3), + "ok": ok_count, + "errors": errors, + "latency_ms": { + "p50": pct(0.5), + "p95": pct(0.95), + "p99": pct(0.99), + }, + "sql_calls_from_messages": sql_calls, + } + print(json.dumps(report, indent=2, ensure_ascii=False)) + + existing = [] + if EVIDENCE.exists(): + try: + existing = json.loads(EVIDENCE.read_text(encoding="utf-8")) + except json.JSONDecodeError: + existing = [] + existing.append(report) + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text( + json.dumps(existing, indent=2, ensure_ascii=False), + encoding="utf-8", + ) + print(f"Saved to {EVIDENCE}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/explain.py b/student-tests/explain.py new file mode 100644 index 00000000..9b3c9c40 --- /dev/null +++ b/student-tests/explain.py @@ -0,0 +1,90 @@ +"""Populate a chat with 10000 messages and run EXPLAIN (ANALYZE, BUFFERS). + +Saves the plan to evidence/explain.txt for the report. +""" +import base64 +import time +import uuid +from pathlib import Path + +import httpx + +BASE = "http://localhost:8080" +EVIDENCE = Path(__file__).resolve().parent.parent / "evidence" / "explain.txt" + + +def register_and_login(client: httpx.Client) -> dict: + username = "explain_" + uuid.uuid4().hex[:10] + password = "Test-password-1234" + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Explain", + "password": password, + }, + ) + assert r.status_code == 201, r.text + user = r.json() + + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + session = r.json() + return {"id": user["id"], "token": session["token"]} + + +def auth(actor: dict) -> dict: + return {"Authorization": "Bearer " + actor["token"]} + + +def main(): + total = 10000 + print(f"Setting up {total} messages...") + + with httpx.Client(timeout=120) as c: + alice = register_and_login(c) + bob = register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "explain", "member_ids": [alice["id"], bob["id"]]}, + headers=auth(alice), + ).json() + + print(f"chat_id = {chat['id']}") + + batch = 200 + started = time.monotonic() + for i in range(0, total, batch): + for j in range(batch): + c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"msg-{i+j:05d}"}, + headers=auth(alice), + ) + if (i + batch) % 2000 == 0: + elapsed = time.monotonic() - started + print(f" {i+batch}/{total} messages, {elapsed:.1f}s") + + print(f"Setup done in {time.monotonic() - started:.1f}s") + print(f"chat_id: {chat['id']}") + + print("Run in psql:") + print(f""" +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = '{chat['id']}' + AND (created_at, id) > ('2026-01-01T00:00:00Z'::timestamptz, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY created_at, id +LIMIT 51; +""") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/queue_benchmark.py b/student-tests/queue_benchmark.py new file mode 100644 index 00000000..cf2a51a7 --- /dev/null +++ b/student-tests/queue_benchmark.py @@ -0,0 +1,186 @@ +import argparse +import base64 +import io +import json +import os +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] +EVIDENCE = Path(__file__).resolve().parent.parent / "evidence" +WORKER_CONTAINER = "mtusi2026lab5-worker-1" +SIZES = { + "small": (320, 200), + "medium": (1280, 800), + "large": (4000, 3000), +} + + +def _png(size): + buf = io.BytesIO() + Image.new("RGB", size, (64, 128, 192)).save(buf, format="PNG") + return buf.getvalue() + + +def _user(c): + u = "qb_" + uuid.uuid4().hex[:10] + p = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "Q", "password": p}) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": s["user"]["id"], "token": s["token"]} + + +def _auth(a): + return {"Authorization": "Bearer " + a["token"]} + + +def _worker_mem(): + out = subprocess.run( + ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", WORKER_CONTAINER], + capture_output=True, text=True, + ).stdout.strip() + if not out or "/" not in out: + return None + v = out.split("/")[0].strip() + units = {"B": 1, "KiB": 1024, "Ki": 1024, "MiB": 1024**2, "Mi": 1024**2, + "GiB": 1024**3, "Gi": 1024**3} + for u in sorted(units, key=len, reverse=True): + if v.endswith(u): + return round(int(float(v[: -len(u)]) * units[u]) / 1024**2, 1) + return None + + +def _restart_worker(prefetch): + env = dict(os.environ) + env["PREFETCH_COUNT"] = str(prefetch) + subprocess.run( + COMPOSE + ["up", "-d", "--force-recreate", "worker"], + env=env, check=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + time.sleep(5) + + +def _start_worker(prefetch): + env = dict(os.environ) + env["PREFETCH_COUNT"] = str(prefetch) + subprocess.run( + COMPOSE + ["up", "-d", "--force-recreate", "worker"], + env=env, check=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ) + + +def _percentiles(values, ps=(0.5, 0.95, 0.99)): + if not values: + return {f"p{int(p*100)}": None for p in ps} + s = sorted(values) + return {f"p{int(p*100)}": round(s[max(0, int(len(s)*p)-1)], 3) for p in ps} + + +def run(prefetch, size_name, files, output): + size = SIZES[size_name] + data = _png(size) + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": f"qb-{prefetch}-{size_name}", "member_ids": [a["id"], b["id"]]}, + headers=_auth(a), + ).json() + chat_id = chat["id"] + + _restart_worker(prefetch) + + uploads = [] + started = time.monotonic() + with httpx.Client(timeout=30) as c: + for i in range(files): + t = time.monotonic() + r = c.post( + f"{BASE}/api/v1/chats/{chat_id}/attachments", + params={"filename": f"f{i}.png"}, + content=data, + headers={**_auth(a), "Content-Type": "image/png"}, + ) + if r.status_code != 202: + print(f"upload failed: {r.status_code} {r.text[:100]}", file=sys.stderr) + continue + uploads.append({"id": r.json()["id"], "t_upload": t}) + + states = {u["id"]: {"t_upload": u["t_upload"], "t_processing": None, "t_ready": None} + for u in uploads} + deadline = time.monotonic() + 600 + pending = set(states) + with httpx.Client(timeout=10) as c: + while pending and time.monotonic() < deadline: + for att_id in list(pending): + r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth(a)) + if r.status_code != 200: + continue + st = r.json()["status"] + now = time.monotonic() + if st == "processing" and states[att_id]["t_processing"] is None: + states[att_id]["t_processing"] = now + if st in ("ready", "failed"): + if states[att_id]["t_processing"] is None: + states[att_id]["t_processing"] = now + states[att_id]["t_ready"] = now + pending.discard(att_id) + if pending: + time.sleep(0.2) + + finished = time.monotonic() + rss = _worker_mem() + + queue_lags = [ + s["t_processing"] - s["t_upload"] + for s in states.values() if s["t_processing"] is not None + ] + ready_times = [ + s["t_ready"] - s["t_upload"] + for s in states.values() if s["t_ready"] is not None + ] + duration = finished - started + ok = len(ready_times) + report = { + "prefetch": prefetch, + "size": size_name, + "files_requested": files, + "files_ok": ok, + "files_failed": files - ok, + "duration_s": round(duration, 3), + "throughput_files_per_s": round(ok / duration, 3) if duration else None, + "queue_lag_s": _percentiles(queue_lags), + "time_to_ready_s": _percentiles(ready_times), + "worker_rss_mib": rss, + } + out = Path(output) + out.parent.mkdir(parents=True, exist_ok=True) + out.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8") + print(json.dumps(report, indent=2, ensure_ascii=False)) + _start_worker(prefetch) + return report + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--prefetch", type=int, required=True) + parser.add_argument("--size", choices=list(SIZES), required=True) + parser.add_argument("--files", type=int, default=50) + parser.add_argument("--output", required=True) + args = parser.parse_args() + run(args.prefetch, args.size, args.files, args.output) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/queue_recovery.py b/student-tests/queue_recovery.py new file mode 100644 index 00000000..9ca7e81e --- /dev/null +++ b/student-tests/queue_recovery.py @@ -0,0 +1,101 @@ +import argparse +import base64 +import io +import json +import subprocess +import time +import uuid +from pathlib import Path + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] +EVIDENCE = Path(__file__).resolve().parent.parent / "evidence" + + +def _png(size=(1280, 800)): + buf = io.BytesIO() + Image.new("RGB", size, (128, 64, 192)).save(buf, format="PNG") + return buf.getvalue() + + +def _user(c): + u = "qr_" + uuid.uuid4().hex[:10] + p = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "Q", "password": p}) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": s["user"]["id"], "token": s["token"]} + + +def _auth(a): + return {"Authorization": "Bearer " + a["token"]} + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--files", type=int, default=100) + parser.add_argument("--output", default="evidence/queue-recovery.json") + args = parser.parse_args() + + data = _png() + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "recovery", "member_ids": [a["id"], b["id"]]}, + headers=_auth(a), + ).json() + chat_id = chat["id"] + + subprocess.run(COMPOSE + ["stop", "worker"], check=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + + ids = [] + with httpx.Client(timeout=30) as c: + for i in range(args.files): + r = c.post( + f"{BASE}/api/v1/chats/{chat_id}/attachments", + params={"filename": f"f{i}.png"}, + content=data, + headers={**_auth(a), "Content-Type": "image/png"}, + ) + if r.status_code == 202: + ids.append(r.json()["id"]) + print(f"uploaded {len(ids)} files, queue accumulated", flush=True) + + time.sleep(10) + subprocess.run(COMPOSE + ["start", "worker"], check=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + worker_started = time.monotonic() + + deadline = time.monotonic() + 600 + pending = set(ids) + with httpx.Client(timeout=10) as c: + while pending and time.monotonic() < deadline: + for att_id in list(pending): + r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth(a)) + if r.status_code != 200: + continue + if r.json()["status"] in ("ready", "failed"): + pending.discard(att_id) + if pending: + time.sleep(0.5) + finished = time.monotonic() + report = { + "files": args.files, + "finished": args.files - len(pending), + "pending_after_timeout": len(pending), + "recovery_time_s": round(finished - worker_started, 3), + } + out = Path(args.output) + out.parent.mkdir(parents=True, exist_ok=True) + out.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8") + print(json.dumps(report, indent=2, ensure_ascii=False)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py new file mode 100644 index 00000000..b3c10c6b --- /dev/null +++ b/student-tests/test_attachments.py @@ -0,0 +1,191 @@ +import base64 +import io +import uuid + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" + + +def _register_and_login(client: httpx.Client) -> dict: + username = "att_" + uuid.uuid4().hex[:10] + password = "Test-password-1234" + r = client.post( + f"{BASE}/api/v1/users", + json={"username": username, "display_name": "A", "password": password}, + ) + assert r.status_code == 201, r.text + user = r.json() + cred = base64.b64encode((username + ":" + password).encode()).decode() + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + cred}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def _auth(actor: dict) -> dict: + return {"Authorization": "Bearer " + actor["token"]} + + +def _idem(actor: dict) -> dict: + return {**_auth(actor), "Idempotency-Key": uuid.uuid4().hex} + + +def _png(w: int = 320, h: int = 200, color=(255, 0, 0)) -> bytes: + buf = io.BytesIO() + Image.new("RGB", (w, h), color).save(buf, format="PNG") + return buf.getvalue() + + +def _chat(client: httpx.Client, owner: dict, *members: dict) -> dict: + member_ids = [owner["id"]] + [m["id"] for m in members] + r = client.post( + f"{BASE}/api/v1/chats", + json={"title": "att", "member_ids": member_ids}, + headers=_auth(owner), + ) + assert r.status_code == 201, r.text + return r.json() + + +def _upload(client, actor, chat_id, data, filename="x.png", content_type="image/png"): + return client.post( + f"{BASE}/api/v1/chats/{chat_id}/attachments", + params={"filename": filename}, + content=data, + headers={**_auth(actor), "Content-Type": content_type}, + ) + + +def test_oversize_returns_413(): + with httpx.Client(timeout=30) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + big = b"\x89PNG\r\n\x1a\n" + b"\x00" * (10 * 1024 * 1024) + r = _upload(c, a, chat["id"], big, filename="big.png") + assert r.status_code == 413, r.text + assert r.json()["error"]["code"] == "payload_too_large" + + +def test_corrupted_image_returns_415(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + data = _png()[:50] + r = _upload(c, a, chat["id"], data) + assert r.status_code == 415, r.text + assert r.json()["error"]["code"] == "unsupported_media_type" + + +def test_mime_spoof_returns_415(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + r = _upload(c, a, chat["id"], b"not an image at all") + assert r.status_code == 415, r.text + assert r.json()["error"]["code"] == "unsupported_media_type" + + +def test_bad_filename_returns_400(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + r = _upload(c, a, chat["id"], _png(), filename="../etc/passwd") + assert r.status_code == 400, r.text + assert r.json()["error"]["code"] == "invalid_request" + + +def test_empty_file_returns_415(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + r = _upload(c, a, chat["id"], b"") + assert r.status_code == 415, r.text + + +def test_outsider_cannot_download(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + eve = _register_and_login(c) + chat = _chat(c, a, b) + att = _upload(c, a, chat["id"], _png()).json() + r = c.get(f"{BASE}/api/v1/attachments/{att['id']}", headers=_auth(eve)) + assert r.status_code == 404 + r = c.get(f"{BASE}/api/v1/attachments/{att['id']}/content", headers=_auth(eve)) + assert r.status_code == 404 + + +def test_attach_foreign_attachment_returns_404(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat1 = _chat(c, a, b) + chat2 = _chat(c, a, b) + att = _upload(c, a, chat1["id"], _png()).json() + r = c.post( + f"{BASE}/api/v1/chats/{chat2['id']}/messages", + json={"text": "hi", "attachment_ids": [att["id"]]}, + headers=_idem(a), + ) + assert r.status_code == 404, r.text + + +def test_empty_text_without_attachments_returns_400(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": ""}, + headers=_idem(a), + ) + assert r.status_code == 400, r.text + assert r.json()["error"]["code"] == "invalid_request" + + +def test_empty_text_with_attachment_returns_201(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + att = _upload(c, a, chat["id"], _png()).json() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": "", "attachment_ids": [att["id"]]}, + headers=_idem(a), + ) + assert r.status_code == 201, r.text + assert r.json()["attachment_ids"] == [att["id"]] + + +def test_user_error_is_not_500(): + with httpx.Client(timeout=10) as c: + a = _register_and_login(c) + b = _register_and_login(c) + chat = _chat(c, a, b) + cases = [ + (_upload(c, a, chat["id"], b"not image"), 415), + (_upload(c, a, chat["id"], b""), 415), + (_upload(c, a, chat["id"], _png(), filename="../x"), 400), + ( + c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": ""}, + headers=_idem(a), + ), + 400, + ), + ] + for response, expected in cases: + assert response.status_code == expected, response.text + assert response.status_code < 500 \ No newline at end of file diff --git a/student-tests/test_cache.py b/student-tests/test_cache.py new file mode 100644 index 00000000..26b5bbfe --- /dev/null +++ b/student-tests/test_cache.py @@ -0,0 +1,129 @@ +import base64 +import subprocess +import time +import uuid + +import httpx + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] + + +def _register_and_login(client: httpx.Client) -> dict: + username = "cch_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Cache Test", + "password": password, + }, + ) + assert r.status_code == 201, r.text + user = r.json() + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + return {"id": user["id"], "token": r.json()["token"]} + + +def _auth(actor: dict) -> dict: + return {"Authorization": "Bearer " + actor["token"]} + + +def _auth_idem(actor: dict) -> dict: + return { + "Authorization": "Bearer " + actor["token"], + "Idempotency-Key": uuid.uuid4().hex, + } + + +def test_read_after_write_sees_new_message(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "raw", "member_ids": [alice["id"], bob["id"]]}, + headers=_auth(alice), + ).json() + + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50", + headers=_auth(alice), + ) + assert r.status_code == 200 + before = r.json()["items"] + assert len(before) == 0 + + text = "read-after-write-" + uuid.uuid4().hex[:8] + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": text}, + headers=_auth_idem(alice), + ) + assert r.status_code == 201, r.text + + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50", + headers=_auth(alice), + ) + assert r.status_code == 200 + after = r.json()["items"] + texts = [m["text"] for m in after] + assert text in texts, f"read-after-write failed: {texts}" + + +def test_cache_recovers_after_valkey_restart(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "restart", "member_ids": [alice["id"], bob["id"]]}, + headers=_auth(alice), + ).json() + for i in range(5): + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"msg-{i}"}, + headers=_auth_idem(alice), + ) + assert r.status_code == 201, r.text + + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50", + headers=_auth(alice), + ) + assert r.status_code == 200 + before = r.json()["items"] + assert len(before) == 5 + + subprocess.run(COMPOSE + ["restart", "valkey"], check=True) + time.sleep(3) + + for _ in range(30): + try: + with httpx.Client(timeout=2) as c: + r = c.get(f"{BASE}/health/ready") + if r.status_code == 200: + break + except Exception: + pass + time.sleep(1) + + with httpx.Client() as c: + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50", + headers=_auth(alice), + ) + assert r.status_code == 200 + after = r.json()["items"] + assert len(after) == 5 + assert [m["id"] for m in after] == [m["id"] for m in before] \ No newline at end of file diff --git a/student-tests/test_presigned.py b/student-tests/test_presigned.py new file mode 100644 index 00000000..ff04f94f --- /dev/null +++ b/student-tests/test_presigned.py @@ -0,0 +1,134 @@ +import base64 +import hashlib +import io +import uuid + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" + + +def _user(c): + u = "ps_" + uuid.uuid4().hex[:10] + p = "Test-password-1234" + r = c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "P", "password": p}) + assert r.status_code == 201, r.text + user = r.json() + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": user["id"], "token": s["token"]} + + +def _auth(a): + return {"Authorization": "Bearer " + a["token"]} + + +def _png(w=320, h=200): + buf = io.BytesIO() + Image.new("RGB", (w, h), (0, 255, 0)).save(buf, format="PNG") + return buf.getvalue() + + +def _chat(c, a, b): + r = c.post( + f"{BASE}/api/v1/chats", + json={"title": "ps", "member_ids": [a["id"], b["id"]]}, + headers=_auth(a), + ) + assert r.status_code == 201, r.text + return r.json() + + +def test_presigned_happy_path(): + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign", + params={"filename": "p.png", "size": len(data), "content_type": "image/png"}, + headers=_auth(a), + ) + assert r.status_code == 201, r.text + presign = r.json() + put = httpx.put(presign["upload_url"], content=data, headers={"Content-Type": "image/png"}) + assert put.status_code == 200, put.text + r = c.post( + f"{BASE}/api/v1/attachments/{presign['attachment_id']}/complete", + headers=_auth(a), + ) + assert r.status_code == 201, r.text + att = r.json() + assert att["status"] == "ready" + assert att["sha256"] == hashlib.sha256(data).hexdigest() + dl = c.get(f"{BASE}/api/v1/attachments/{att['id']}/content", headers=_auth(b)) + assert dl.status_code == 200 + assert dl.content == data + + +def test_presigned_bad_signature_rejected(): + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign", + params={"filename": "p.png", "size": len(data), "content_type": "image/png"}, + headers=_auth(a), + ) + url = r.json()["upload_url"] + bad = url.replace("X-Amz-Signature=", "X-Amz-Signature=deadbeef") + put = httpx.put(bad, content=data, headers={"Content-Type": "image/png"}) + assert put.status_code == 403, put.text + + +def test_presigned_complete_without_upload_409(): + with httpx.Client(timeout=10) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign", + params={"filename": "p.png", "size": len(data), "content_type": "image/png"}, + headers=_auth(a), + ) + att_id = r.json()["attachment_id"] + r = c.post(f"{BASE}/api/v1/attachments/{att_id}/complete", headers=_auth(a)) + assert r.status_code == 409, r.text + + +def test_presigned_outsider_404(): + with httpx.Client(timeout=10) as c: + a = _user(c) + b = _user(c) + eve = _user(c) + chat = _chat(c, a, b) + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign", + params={"filename": "p.png", "size": len(data), "content_type": "image/png"}, + headers=_auth(a), + ) + att_id = r.json()["attachment_id"] + r = c.post(f"{BASE}/api/v1/attachments/{att_id}/complete", headers=_auth(eve)) + assert r.status_code == 404, r.text + + +def test_proxy_endpoint_still_works(): + with httpx.Client(timeout=10) as c: + a = _user(c) + b = _user(c) + chat = _chat(c, a, b) + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments", + params={"filename": "proxy.png"}, + content=data, + headers={**_auth(a), "Content-Type": "image/png"}, + ) + assert r.status_code == 201, r.text + assert r.json()["status"] == "ready" \ No newline at end of file diff --git a/student-tests/test_race.py b/student-tests/test_race.py new file mode 100644 index 00000000..bd0a7bbe --- /dev/null +++ b/student-tests/test_race.py @@ -0,0 +1,84 @@ +import base64 +import uuid +from concurrent.futures import ThreadPoolExecutor +import httpx + +BASE = "http://localhost:8080" + + +def _register_and_login(client: httpx.Client) -> dict: + username = "u_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Тест", + "password": password, + }, + ) + user = r.json() + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + session = r.json() + return {"id": user["id"], "token": session["token"]} + + +def test_concurrent_writes_are_not_lost(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "race", "member_ids": [alice["id"], bob["id"]]}, + headers={"Authorization": "Bearer " + alice["token"]}, + ).json() + + def worker(worker_id: int): + with httpx.Client(timeout=10) as c: + for i in range(10): + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"w{worker_id}-m{i}"}, + headers={ + "Authorization": "Bearer " + alice["token"], + "Idempotency-Key": uuid.uuid4().hex, + }, + ) + if r.status_code != 201: + print(f"POST FAIL w{worker_id}-m{i}: {r.status_code} {r.text[:200]}") + + with ThreadPoolExecutor(max_workers=10) as pool: + list(pool.map(worker, range(10))) + + with httpx.Client() as c: + all_ids = [] + cursor = None + while True: + params = {"limit": 100} + if cursor: + params["cursor"] = cursor + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + params=params, + headers={"Authorization": "Bearer " + alice["token"]}, + ) + page = r.json() + if r.status_code != 200 or "items" not in page: + print(f"GET FAIL: {r.status_code} {r.text[:500]}") + print(f"chat_id: {chat['id']}") + print(f"alice_token: {alice['token'][:30]}...") + raise AssertionError(f"GET messages failed: {r.status_code}") + all_ids.extend(m["id"] for m in page["items"]) + cursor = page["next_cursor"] + if not cursor: + break + + print(f"Got {len(all_ids)} messages, unique: {len(set(all_ids))}") + assert len(all_ids) == 100 + assert len(set(all_ids)) == 100 \ No newline at end of file diff --git a/student-tests/test_resilience.py b/student-tests/test_resilience.py new file mode 100644 index 00000000..4815fd9e --- /dev/null +++ b/student-tests/test_resilience.py @@ -0,0 +1,153 @@ +"""Automatic resilience checks: expiry, Redis/DB unavailability. + +Requires Docker: test stops the service, checks API behaviour, restarts it. +Run: pytest student-tests/test_resilience.py -v -s +""" +import base64 +import subprocess +import time +import uuid + +import httpx + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] + + +def _register_and_login(client: httpx.Client) -> dict: + username = "res_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Res", + "password": password, + }, + ) + assert r.status_code == 201, r.text + user = r.json() + + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + session = r.json() + return { + "id": user["id"], + "username": username, + "password": password, + "token": session["token"], + } + + +def _wait_ready(timeout: float = 60.0) -> None: + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + with httpx.Client(timeout=2) as c: + r = c.get(f"{BASE}/health/ready") + if r.status_code == 200: + return + except Exception: + pass + time.sleep(1) + raise RuntimeError("API did not become ready in time") + + +def _compose(*args: str) -> None: + subprocess.run(COMPOSE + list(args), check=True) + + +def test_expiry_invalidates_session(monkeypatch): + """Session expires when SESSION_TTL_SECONDS is very small.""" + _compose("up", "-d", "--build", "api") + _wait_ready() + + with httpx.Client(timeout=5) as c: + actor = _register_and_login(c) + r = c.get( + f"{BASE}/api/v1/users/me", + headers={"Authorization": "Bearer " + actor["token"]}, + ) + assert r.status_code == 200 + + # Restart API with 2-second TTL + _compose("stop", "api") + subprocess.run( + ["docker", "compose", "--env-file", ".env", "rm", "-f", "api"], + check=False, + ) + subprocess.run( + [ + "docker", "compose", "--env-file", ".env", + "run", "-d", "--name", "api-short-ttl", + "-e", "SESSION_TTL_SECONDS=2", + "-p", "127.0.0.1:8080:8080", + "api", + ], + check=True, + ) + _wait_ready() + + with httpx.Client(timeout=5) as c: + fresh = _register_and_login(c) + r = c.get( + f"{BASE}/api/v1/users/me", + headers={"Authorization": "Bearer " + fresh["token"]}, + ) + assert r.status_code == 200 + print("Waiting 4 seconds for expiry...") + time.sleep(4) + r = c.get( + f"{BASE}/api/v1/users/me", + headers={"Authorization": "Bearer " + fresh["token"]}, + ) + assert r.status_code == 401, f"expected 401 after expiry, got {r.status_code}" + + subprocess.run(["docker", "stop", "api-short-ttl"], check=False) + subprocess.run(["docker", "rm", "api-short-ttl"], check=False) + _compose("up", "-d", "--build", "api") + _wait_ready() + + +def test_redis_unavailable_returns_503(): + """When Valkey is down, /health/ready returns 503.""" + _compose("up", "-d", "--build") + _wait_ready() + + _compose("stop", "valkey") + time.sleep(3) + + try: + with httpx.Client(timeout=5) as c: + r = c.get(f"{BASE}/health/ready") + assert r.status_code == 503, f"expected 503, got {r.status_code}" + body = r.json() + assert body["error"]["code"] == "unavailable" + finally: + _compose("start", "valkey") + _wait_ready() + + +def test_postgres_unavailable_returns_503(): + """When PostgreSQL is down, /health/ready returns 503.""" + _compose("up", "-d", "--build") + _wait_ready() + + _compose("stop", "postgres") + time.sleep(3) + + try: + with httpx.Client(timeout=5) as c: + r = c.get(f"{BASE}/health/ready") + assert r.status_code == 503, f"expected 503, got {r.status_code}" + body = r.json() + assert body["error"]["code"] == "unavailable" + finally: + _compose("start", "postgres") + _wait_ready() \ No newline at end of file diff --git a/student-tests/test_validation.py b/student-tests/test_validation.py new file mode 100644 index 00000000..299823dc --- /dev/null +++ b/student-tests/test_validation.py @@ -0,0 +1,264 @@ +import base64 +import uuid +import httpx + +BASE = "http://localhost:8080" + + +def _register_and_login(client: httpx.Client) -> dict: + username = "u_" + uuid.uuid4().hex[:10] + password = "Test-password-" + uuid.uuid4().hex[:8] + r = client.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Тест", + "password": password, + }, + ) + assert r.status_code == 201, r.text + user = r.json() + + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + r = client.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201, r.text + session = r.json() + return { + "id": user["id"], + "username": username, + "password": password, + "token": session["token"], + } + + +def _auth(actor: dict) -> dict: + return {"Authorization": "Bearer " + actor["token"]} + + +def _auth_idem(actor: dict) -> dict: + return { + "Authorization": "Bearer " + actor["token"], + "Idempotency-Key": uuid.uuid4().hex, + } + + +def test_invalid_json_returns_400(): + with httpx.Client() as c: + r = c.post( + f"{BASE}/api/v1/users", + content=b"{not json at all", + headers={"Content-Type": "application/json"}, + ) + assert r.status_code == 400 + body = r.json() + assert body["error"]["code"] == "invalid_request" + assert "X-Request-Id" in r.headers + + +def test_duplicate_username_returns_409(): + with httpx.Client() as c: + actor = _register_and_login(c) + r = c.post( + f"{BASE}/api/v1/users", + json={ + "username": actor["username"], + "display_name": "Другой", + "password": "Another-password-123", + }, + ) + assert r.status_code == 409 + assert r.json()["error"]["code"] == "conflict" + + +def test_empty_message_returns_400(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "t", "member_ids": [alice["id"], bob["id"]]}, + headers=_auth(alice), + ).json() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": ""}, + headers=_auth_idem(alice), + ) + assert r.status_code == 400 + + +def test_outsider_gets_404_not_403(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + eve = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "t", "member_ids": [alice["id"], bob["id"]]}, + headers=_auth(alice), + ).json() + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}", + headers=_auth(eve), + ) + assert r.status_code == 404 + r2 = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": "вторжение"}, + headers=_auth_idem(eve), + ) + assert r2.status_code == 404 + + +def test_pagination_walks_all_pages(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "t", "member_ids": [alice["id"], bob["id"]]}, + headers=_auth(alice), + ).json() + sent_ids = [] + for i in range(5): + m = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + json={"text": f"msg-{i}"}, + headers=_auth_idem(alice), + ).json() + sent_ids.append(m["id"]) + + collected = [] + cursor = None + while True: + params = {"limit": 2} + if cursor: + params["cursor"] = cursor + r = c.get( + f"{BASE}/api/v1/chats/{chat['id']}/messages", + params=params, + headers=_auth(alice), + ) + assert r.status_code == 200, r.text + page = r.json() + collected.extend(m["id"] for m in page["items"]) + cursor = page["next_cursor"] + if not cursor: + break + assert collected == sent_ids + + +def test_wrong_password_returns_401(): + with httpx.Client() as c: + actor = _register_and_login(c) + bad = base64.b64encode( + (actor["username"] + ":wrong-password-1234").encode("utf-8") + ).decode("ascii") + r = c.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + bad}, + ) + assert r.status_code == 401 + assert "WWW-Authenticate" in r.headers + assert r.headers["WWW-Authenticate"].lower().startswith("basic") + + +def test_logout_invalidates_token(): + with httpx.Client() as c: + actor = _register_and_login(c) + r = c.delete( + f"{BASE}/api/v1/auth/sessions/current", + headers=_auth(actor), + ) + assert r.status_code == 204 + r = c.get(f"{BASE}/api/v1/users/me", headers=_auth(actor)) + assert r.status_code == 401 + + +def test_x_user_id_ignored_with_bearer(): + with httpx.Client() as c: + alice = _register_and_login(c) + bob = _register_and_login(c) + r = c.get( + f"{BASE}/api/v1/users/me", + headers={ + "Authorization": "Bearer " + alice["token"], + "X-User-Id": bob["id"], + }, + ) + assert r.status_code == 200 + assert r.json()["id"] == alice["id"] + + +def test_basic_on_business_endpoint_returns_401(): + with httpx.Client() as c: + actor = _register_and_login(c) + credential = base64.b64encode( + (actor["username"] + ":" + actor["password"]).encode("utf-8") + ).decode("ascii") + r = c.get( + f"{BASE}/api/v1/users/me", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 401 + + +def test_race_duplicate_username_only_one_wins(): + from concurrent.futures import ThreadPoolExecutor + + username = "race_" + uuid.uuid4().hex[:10] + password = "Test-password-1234" + + def register(): + with httpx.Client(timeout=10) as c: + return c.post( + f"{BASE}/api/v1/users", + json={ + "username": username, + "display_name": "Race", + "password": password, + }, + ).status_code + + with ThreadPoolExecutor(max_workers=2) as pool: + statuses = list(pool.map(lambda _: register(), range(2))) + + assert sorted(statuses) == [201, 409], f"got statuses {statuses}" + + credential = base64.b64encode( + (username + ":" + password).encode("utf-8") + ).decode("ascii") + with httpx.Client() as c: + r = c.post( + f"{BASE}/api/v1/auth/sessions", + headers={"Authorization": "Basic " + credential}, + ) + assert r.status_code == 201 + assert r.json()["user"]["username"] == username + + +def test_rollback_chat_with_unknown_member(): + with httpx.Client() as c: + alice = _register_and_login(c) + fake_id = str(uuid.uuid4()) + r = c.post( + f"{BASE}/api/v1/chats", + json={ + "title": "Не должно создаться", + "member_ids": [alice["id"], fake_id], + }, + headers=_auth(alice), + ) + assert r.status_code == 400, r.text + + chats = c.get( + f"{BASE}/api/v1/chats?limit=100", + headers=_auth(alice), + ).json() + titles = [ch["title"] for ch in chats["items"]] + assert "Не должно создаться" not in titles \ No newline at end of file diff --git a/student-tests/test_worker_crash.py b/student-tests/test_worker_crash.py new file mode 100644 index 00000000..407c26ea --- /dev/null +++ b/student-tests/test_worker_crash.py @@ -0,0 +1,100 @@ +import base64 +import io +import subprocess +import time +import uuid + +import httpx +from PIL import Image + +BASE = "http://localhost:8080" +COMPOSE = ["docker", "compose", "--env-file", ".env"] + + +def _user(c): + u = "crash_" + uuid.uuid4().hex[:10] + p = "Test-password-1234" + c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "C", "password": p}) + cred = base64.b64encode(f"{u}:{p}".encode()).decode() + s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json() + return {"id": s["user"]["id"], "token": s["token"]} + + +def _auth(a): + return {"Authorization": "Bearer " + a["token"]} + + +def _png(): + buf = io.BytesIO() + Image.new("RGB", (320, 200), (0, 128, 255)).save(buf, format="PNG") + return buf.getvalue() + + +def _wait_ready(c, att_id, token, timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth({"token": token})) + if r.status_code == 200 and r.json()["status"] == "ready": + return r.json() + time.sleep(0.5) + raise AssertionError("attachment did not become ready") + + +def _delete_variant(chat_id, att_id, name): + key = f"{name}s/{chat_id}/{att_id}.jpg" + script = "from app.s3 import s3\ns3.remove('" + key + "')\nprint('removed')\n" + subprocess.run( + COMPOSE + ["exec", "-T", "worker", "python", "-c", script], + check=True, + ) + + +def test_worker_recovers_after_ack_lost(): + with httpx.Client(timeout=30) as c: + a = _user(c) + b = _user(c) + chat = c.post( + f"{BASE}/api/v1/chats", + json={"title": "crash", "member_ids": [a["id"], b["id"]]}, + headers=_auth(a), + ).json() + data = _png() + r = c.post( + f"{BASE}/api/v1/chats/{chat['id']}/attachments", + params={"filename": "c.png"}, + content=data, + headers={**_auth(a), "Content-Type": "image/png"}, + ) + assert r.status_code == 202, r.text + att = r.json() + att_id = att["id"] + + _wait_ready(c, att_id, a["token"]) + before = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth(a)).json() + thumb1 = c.get( + f"{BASE}/api/v1/attachments/{att_id}/content?variant=thumbnail", + headers=_auth(a), + ) + assert thumb1.status_code == 200 + + _delete_variant(chat["id"], att_id, "thumbnail") + + subprocess.run( + COMPOSE + ["exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", + f"UPDATE attachments SET status='failed', error_code='ack_lost' WHERE id='{att_id}'"], + check=True, + ) + + r = c.post( + f"{BASE}/api/v1/attachments/{att_id}/replay", + headers=_auth(a), + ) + assert r.status_code == 202, r.text + + ready = _wait_ready(c, att_id, a["token"]) + thumb2 = c.get( + f"{BASE}/api/v1/attachments/{att_id}/content?variant=thumbnail", + headers=_auth(a), + ) + assert thumb2.status_code == 200 + assert ready["variants"] == before["variants"], "variants changed after replay" \ No newline at end of file