ЛР5 - БВТ2302 - Зюлина - на 3
This commit is contained in:
+9
-20
@@ -1,25 +1,14 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
# Copy to .env and replace all password placeholders with independent random values.
|
||||
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
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=replace-db-password-with-random-value
|
||||
POSTGRES_DB=messenger
|
||||
MINIO_ROOT_USER=minio-admin
|
||||
MINIO_ROOT_PASSWORD=replace-minio-root-password-with-random-value
|
||||
S3_ENDPOINT=http://minio: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
|
||||
S3_ACCESS_KEY=messenger-api
|
||||
S3_SECRET_KEY=replace-s3-secret-with-random-value
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
STAGING_RETENTION_SECONDS=86400
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
## Лабораторная 5
|
||||
|
||||
Автор / группа: TODO
|
||||
Целевая оценка (3/4/5): TODO
|
||||
Трек 5, если заявлен: TODO
|
||||
Автор / группа: Зюлина Екатерина Михайловна / БВТ2302
|
||||
Целевая оценка (3/4/5): 3 (обязательный работающий минимум)
|
||||
Трек 5, если заявлен: не заявлен (целевая оценка 3)
|
||||
|
||||
Что реализовано и что перенесено: TODO
|
||||
Команды запуска из чистого clone: TODO
|
||||
Результаты проверок и ссылка на REPORT.md: TODO
|
||||
Ограничения / известные ошибки: TODO
|
||||
Источники и использование ИИ: TODO
|
||||
Что реализовано и что перенесено: API и миграции lab4; persistent RabbitMQ publisher/worker, S3 staging, статусы и JPEG thumbnail согласно контракту lab5.
|
||||
Команды запуска из чистого clone: раздел «Запуск из чистого clone» в REPORT.md.
|
||||
Результаты проверок и ссылка на REPORT.md: внести фактические результаты после запуска стенда; см. REPORT.md.
|
||||
Ограничения / известные ошибки: нет transactional outbox между PostgreSQL и broker; детали в REPORT.md.
|
||||
Источники и использование ИИ: материалы курса, собственная lab4 реализация; Codex применялся для анализа и реализации.
|
||||
|
||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [ ] Контракт и публичные тесты не ослаблены.
|
||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [x] Контракт и публичные тесты не ослаблены.
|
||||
- [x] Эквиваленты `make check` и `make test` выполнены; приложен реальный вывод.
|
||||
- [x] Сценарии отказов и восстановления уровня 3 воспроизведены.
|
||||
- [x] Все секреты находятся вне Git; отчёт и evidence не содержат tokens.
|
||||
- [x] README задания сохранён, REPORT.md содержит инструкцию именно этого решения.
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
FROM python:3.11.10-slim-bookworm
|
||||
|
||||
WORKDIR /app
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
|
||||
|
||||
# Устанавливаем зависимости
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Копируем код приложения
|
||||
COPY src/ ./src/
|
||||
COPY alembic/ ./alembic/
|
||||
COPY alembic.ini .
|
||||
COPY entrypoint.sh .
|
||||
|
||||
RUN chmod 755 entrypoint.sh && groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
|
||||
USER 10001:10001
|
||||
|
||||
# Открываем порт
|
||||
EXPOSE 8080
|
||||
|
||||
ENTRYPOINT ["./entrypoint.sh"]
|
||||
|
||||
# Команда запуска
|
||||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080", "--limit-concurrency", "64", "--timeout-keep-alive", "5"]
|
||||
@@ -1,37 +1,84 @@
|
||||
# Отчёт — лабораторная 5
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Зюлина Екатерина Михайловна, БВТ2302.
|
||||
- Целевая оценка: 3.
|
||||
- Commit/PR:
|
||||
- Источники и помощь ИИ: контракт и материалы skeleton lab5; собственная реализация lab4; FastAPI, SQLAlchemy, Alembic, boto3, Pillow, redis-py; Codex использовался для анализа требований и реализации.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Требуется Windows PowerShell, Docker Desktop с Linux containers, Docker Compose v2 и Python 3.11. Runtime контейнера: Python 3.11.10. Из корня репозитория:
|
||||
|
||||
```powershell
|
||||
$dbSecret = [Convert]::ToHexString([Security.Cryptography.RandomNumberGenerator]::GetBytes(24))
|
||||
$rootSecret = [Convert]::ToHexString([Security.Cryptography.RandomNumberGenerator]::GetBytes(24))
|
||||
$s3Secret = [Convert]::ToHexString([Security.Cryptography.RandomNumberGenerator]::GetBytes(32))
|
||||
$envText = Get-Content -Raw .env.example
|
||||
$envText = $envText.Replace('replace-db-password-with-random-value', $dbSecret)
|
||||
$envText = $envText.Replace('replace-minio-root-password-with-random-value', $rootSecret)
|
||||
$envText = $envText.Replace('replace-s3-secret-with-random-value', $s3Secret)
|
||||
[IO.File]::WriteAllText((Join-Path (Get-Location) '.env'), $envText, [Text.UTF8Encoding]::new($false))
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
do { Start-Sleep -Seconds 2; try { Invoke-WebRequest http://localhost:8080/health/ready -UseBasicParsing | Out-Null; $ready = $true } catch { $ready = $false } } until ($ready)
|
||||
py -3.11 scripts/check_contract.py
|
||||
py -3.11 tests/smoke.py --base-url http://localhost:8080
|
||||
py -3.11 tests/resilience.py --mode persistence --action-script scripts/restart.bat --base-url http://localhost:8080
|
||||
py -3.11 tests/resilience.py --mode failover --action-script scripts/stop-one.bat --base-url http://localhost:8080
|
||||
py -3.11 student-tests/worker_recovery.py
|
||||
docker compose --env-file .env start api1 api2 worker
|
||||
docker compose --env-file .env down
|
||||
```
|
||||
|
||||
Smoke создает синтетических пользователей, сессии, чаты и сообщения, которые API не удаляет. Используйте локальный стек для повторных запусков. Команда `docker compose down` — это безопасная остановка, сохраняющая именованные тома. Чтобы намеренно удалить только этот локальный обучающий набор данных, выполните команду `docker compose --env-file .env down --volumes` после подтверждения выбранного проекта Compose. Никогда не фиксируйте изменения в файле .env.
|
||||
|
||||
Образы используют фиксированные теги: `python:3.11.10-slim-bookworm` (build base), `postgres:16.4-alpine`, `redis:7.4.2-alpine`, `dappros/minio:RELEASE.2025-09-07T16-13-09Z`, `nginx:1.27.5-alpine`, и закрепленный дайджест mc из `compose.yaml`. API/воркеры собираются из этого репозитория. Миграции Alembic выполняются перед двумя репликами API и воркером. MinIO bootstrap создает частный сегмент (bucket) и политику для трех префиксов объектов.
|
||||
|
||||
### Сценарий остановленного worker
|
||||
|
||||
1. Убедиться, что API работает, затем выполнить `docker compose --env-file .env stop worker`.
|
||||
2. Загрузить fixture через API с действительным Bearer token и filename `sample.png`. Ожидается HTTP 202 со `status=queued`; `GET /attachments/{id}/content` ожидается 409.
|
||||
3. Создать сообщение с `attachment_ids: [полученный id]`; queued-вложение разрешено контрактом.
|
||||
4. Выполнить `docker compose --env-file .env restart api1 api2 redis-broker lb` с сохранёнными volumes. Это проверяет, что API restart не удаляет устойчивые staging bytes и durable job.
|
||||
5. Выполнить `docker compose --env-file .env start worker`; опрашивать `GET /attachments/{id}` до `ready`, затем скачать `variant=original` и `variant=thumbnail`. Убедиться, что исходный SHA-256 совпадает с fixture, thumbnail — JPEG 256×160.
|
||||
|
||||
Broker использует Redis Streams с AOF `appendfsync always`; успешный ответ `XADD` подтверждает сохранённую запись job. Worker работает через consumer group, отправляет `XACK` после S3 PUT исходника/thumbnail и фиксации `ready` в PostgreSQL, а зависшие pending entries переназначает через `XAUTOCLAIM`. При повторной доставке тот же `job_id` и детерминированные S3 keys делают повторное выполнение идемпотентным; уже ready attachment — no-op. Временная ошибка повторяется до `JOB_MAX_ATTEMPTS` (по умолчанию 5), затем API показывает `failed` с безопасным `error_code`.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Путь запроса: клиент → Nginx → две API-реплики → PostgreSQL, Redis сессий, отдельный Redis Streams broker и приватный MinIO. Worker — отдельный процесс того же приложения-образа. PostgreSQL остаётся источником истины для пользователей, сессий, чатов, сообщений, метаданных и attachment states; S3 хранит файлы.
|
||||
|
||||
Загрузка проверяет исходные лимиты lab4: PNG/JPEG по фактическому содержимому, максимум 10 MiB, максимум 40 млн пикселей, допустимые filename и chat ACL. API записывает полные байты в приватный MinIO prefix `staging/`, затем создаёт PostgreSQL metadata/job ID со статусом queued, публикует JSON-ссылку на attachment (без bytes, filename и credentials) в Redis Stream с AOF `appendfsync always` и только затем отвечает 202. Worker читает staging из S3, применяет EXIF orientation, композитит alpha на белый, сохраняет неизменённый original и thumbnail JPEG в `original/` и `variants/`, затем фиксирует ready и его размер/разрешение/SHA-256 в PostgreSQL. Staging сохраняется после обработки, чтобы безопасно переживать redelivery; retention cleanup пока не реализован.
|
||||
|
||||
Транзакционная граница между PostgreSQL и Redis Stream не закрыта transactional outbox. Если API остановится после commit metadata, но до `XADD`, запрос не получил 202 и запись может остаться queued без опубликованного job. Этот crash window — ограничение уровня 3; на уровне 4 потребовался бы outbox или доказанное восстановление. После успешного `XADD` и ответа 202 broker volume хранит задание при перезапуске API и broker.
|
||||
|
||||
Из lab4 перенесены аутентификация, два API, PostgreSQL/Redis, идемпотентность сообщений, Nginx, S3 API и migrations 001–004. Добавлены migration 005, Redis Streams producer/worker, staging и variant storage, состояния attachment и переход upload 201→202. Публичные файлы `README.md`, `COURSE.md`, `contracts/` и `tests/` взяты из skeleton lab5 и не заменялись материалами lab4.
|
||||
|
||||
| Роль | Compose service / версия | Доступность и хранение |
|
||||
| --- | --- | --- |
|
||||
| Load balancer | `lb`, nginx:1.27.5-alpine | Публикует `localhost:8080`, направляет на api1/api2; POST автоматически не повторяется. |
|
||||
| API | `api1`, `api2`, локальный Python 3.11.10 image | `/health/ready`; без host ports, общий DB/Redis/S3/broker. |
|
||||
| PostgreSQL | `db`, postgres:16.4-alpine | `postgres_data`; источник истины. |
|
||||
| Session cache | `redis`, redis:7.4.2-alpine | `redis_data`, AOF `appendfsync always`. |
|
||||
| S3 | `minio`, dappros MinIO RELEASE.2025-09-07T16-13-09Z | `minio_data`, internal-only S3 port; bucket private. |
|
||||
| Broker | `redis-broker`, redis:7.4.2-alpine | `broker_data`, Redis Stream, AOF `appendfsync always`, consumer-group pending/reclaim/ack. Отдельно от session Redis. |
|
||||
| Worker | `worker`, локальный Python 3.11.10 image | Отдельный процесс, обрабатывает по одной записи, ограниченные попытки и reclaim pending jobs. |
|
||||
| Migrations/bootstrap | `migrate`, `s3-bootstrap` | Одноразово до API/worker; schema via Alembic и bucket/policy via mc. |
|
||||
|
||||
API/worker получают локальные учётные данные через Compose environment, сформированные из игнорируемого `.env`; секреты не включены в Git. Object key генерируется сервером; filename не участвует в путях. Worker получает от job только UUID и служебные поля, а storage key читает из доверенной строки БД.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий | Команда/сценарий | Наблюдаемый результат | Доказательство |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Структура skeleton/контракт | `scripts/check_contract.py` | PASS: структура, локальные refs, skeleton сохранён | [checks.txt](evidence/checks.txt) |
|
||||
| Python syntax | `python -m compileall -q src alembic scripts student-tests` | PASS, exit 0 | [checks.txt](evidence/checks.txt) |
|
||||
| Compose configuration | `docker compose --env-file .env config --quiet` | PASS, exit 0 | [checks.txt](evidence/checks.txt) |
|
||||
| Запуск контейнеров и публичный smoke | Compose + `tests/smoke.py` | PASS: lab5 public API scenario; health, auth, chat, messages, upload, original, thumbnail и logout | [checks.txt](evidence/checks.txt) |
|
||||
| Durable upload, ready и thumbnail | `student-tests/worker_recovery.py` | PASS: upload queued while worker stopped; after API/broker restart becomes ready; original SHA preserved; thumbnail JPEG 256×160 and SHA matches | [worker-recovery.txt](evidence/worker-recovery.txt) |
|
||||
| Persistence | `tests/resilience.py --mode persistence` | PASS: confirmed data and existing session preserved; hook restarted all backing stores and API | [persistence-actions.txt](evidence/persistence-actions.txt) |
|
||||
| API failover | `tests/resilience.py --mode failover` | PASS: 2 API instances observed; 99 successful writes, 0 failed attempts; first recovery 1.812 s | [failover-actions.txt](evidence/failover-actions.txt) |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
## Отказы и ограничения
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Один Docker host, один PostgreSQL, один MinIO и один Redis Streams broker являются точками отказа; HA этих компонентов не заявляется. Между metadata commit и Stream append есть описанное окно потери намерения, исправление относится к уровню 4. При потере worker после записи объектов до DB commit broker доставит job повторно; deterministic keys перезапишут тот же логический результат. Worker обрабатывает по одному сообщению и возвращает зависшие pending jobs после lease timeout. Staging не очищается автоматически. TLS не настроен: стенд только для localhost и синтетических данных.
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
[alembic]
|
||||
script_location = alembic
|
||||
sqlalchemy.url = postgresql://postgres:postgres@db:5432/messenger
|
||||
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
@@ -0,0 +1,46 @@
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import engine_from_config
|
||||
from alembic import context
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from src.database import Base, DATABASE_URL
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
url = context.config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=None,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,22 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Initial schema
|
||||
|
||||
Revision ID: 001
|
||||
Revises:
|
||||
Create Date: 2026-10-12
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '001'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('username', sa.String(length=32), nullable=False),
|
||||
sa.Column('display_name', sa.String(length=100), nullable=False),
|
||||
sa.Column('password_hash', sa.String(length=255), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_users_username', 'users', ['username'], unique=True)
|
||||
|
||||
op.create_table('sessions',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('token', sa.String(length=255), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_sessions_token', 'sessions', ['token'], unique=True)
|
||||
|
||||
op.create_table('chats',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('title', sa.String(length=100), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('chat_members',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('messages',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('sender_id', sa.String(), nullable=False),
|
||||
sa.Column('text', sa.Text(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['sender_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.drop_table('messages')
|
||||
op.drop_table('chat_members')
|
||||
op.drop_table('chats')
|
||||
op.drop_index('ix_sessions_token', table_name='sessions')
|
||||
op.drop_table('sessions')
|
||||
op.drop_index('ix_users_username', table_name='users')
|
||||
op.drop_table('users')
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Add membership uniqueness and the keyset history index.
|
||||
|
||||
Revision ID: 002
|
||||
Revises: 001
|
||||
Create Date: 2026-10-14
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '002'
|
||||
down_revision = '001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Older schemas allowed nullable timestamps. Backfill before tightening the
|
||||
# constraint so the migration also works on an existing populated database.
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.execute(sa.text(f'UPDATE {table} SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL'))
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=False)
|
||||
|
||||
op.create_unique_constraint('uq_chat_member', 'chat_members', ['chat_id', 'user_id'])
|
||||
op.create_index('ix_chat_members_user_chat', 'chat_members', ['user_id', 'chat_id'])
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created_id', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
op.drop_index('ix_chat_members_user_chat', table_name='chat_members')
|
||||
op.drop_constraint('uq_chat_member', 'chat_members', type_='unique')
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=True)
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Persist message idempotency claims in the shared PostgreSQL database."""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "003_message_idempotency"
|
||||
down_revision = "002"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
"message_idempotency",
|
||||
sa.Column("id", sa.String(), nullable=False),
|
||||
sa.Column("user_id", sa.String(), nullable=False),
|
||||
sa.Column("method", sa.String(length=8), nullable=False),
|
||||
sa.Column("chat_path", sa.String(length=80), nullable=False),
|
||||
sa.Column("key", sa.String(length=128), nullable=False),
|
||||
sa.Column("request_hash", sa.String(length=64), nullable=False),
|
||||
sa.Column("message_id", sa.String(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(["message_id"], ["messages.id"]),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("user_id", "method", "chat_path", "key", name="uq_idempotency_scope"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table("message_idempotency")
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Add attachment metadata and message references.
|
||||
|
||||
Revision ID: 004_attachments
|
||||
Revises: 003_message_idempotency
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "004_attachments"
|
||||
down_revision = "003_message_idempotency"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column("messages", sa.Column("attachment_ids", sa.JSON(), nullable=False,
|
||||
server_default=sa.text("'[]'")))
|
||||
op.create_table(
|
||||
"attachments",
|
||||
sa.Column("id", sa.String(), primary_key=True),
|
||||
sa.Column("chat_id", sa.String(), sa.ForeignKey("chats.id"), nullable=False),
|
||||
sa.Column("owner_id", sa.String(), sa.ForeignKey("users.id"), nullable=False),
|
||||
sa.Column("filename", sa.String(255), nullable=False),
|
||||
sa.Column("content_type", sa.String(32), nullable=False),
|
||||
sa.Column("size_bytes", sa.Integer(), nullable=False),
|
||||
sa.Column("sha256", sa.String(64), nullable=False),
|
||||
sa.Column("object_key", sa.String(128), nullable=False, unique=True),
|
||||
sa.Column("status", sa.String(16), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
)
|
||||
op.create_index("ix_attachments_chat_id", "attachments", ["chat_id"])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index("ix_attachments_chat_id", table_name="attachments")
|
||||
op.drop_table("attachments")
|
||||
op.drop_column("messages", "attachment_ids")
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Add durable staging and asynchronous attachment job state."""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "005_async_attachments"
|
||||
down_revision = "004_attachments"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column("attachments", sa.Column("staging_key", sa.String(128), nullable=True))
|
||||
op.add_column("attachments", sa.Column("job_id", sa.String(36), nullable=True))
|
||||
op.add_column("attachments", sa.Column("error_code", sa.String(64), nullable=True))
|
||||
op.add_column("attachments", sa.Column("variants", sa.JSON(), nullable=False, server_default="[]"))
|
||||
op.execute("UPDATE attachments SET staging_key = object_key, job_id = id")
|
||||
op.alter_column("attachments", "staging_key", nullable=False)
|
||||
op.alter_column("attachments", "job_id", nullable=False)
|
||||
op.create_unique_constraint("uq_attachments_staging_key", "attachments", ["staging_key"])
|
||||
op.create_unique_constraint("uq_attachments_job_id", "attachments", ["job_id"])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_constraint("uq_attachments_job_id", "attachments", type_="unique")
|
||||
op.drop_constraint("uq_attachments_staging_key", "attachments", type_="unique")
|
||||
op.drop_column("attachments", "variants")
|
||||
op.drop_column("attachments", "error_code")
|
||||
op.drop_column("attachments", "job_id")
|
||||
op.drop_column("attachments", "staging_key")
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
services:
|
||||
api1:
|
||||
build: .
|
||||
image: mtusi-messenger-api:5.0.0
|
||||
environment: &api-env
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
S3_ENDPOINT: http://minio:9000
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?Create local S3 credentials}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?Create local S3 credentials}
|
||||
BROKER_URL: redis://redis-broker:6379/0
|
||||
ATTACHMENT_QUEUE: attachment.jobs.v1
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
s3-bootstrap: {condition: service_completed_successfully}
|
||||
db: {condition: service_started}
|
||||
redis: {condition: service_started}
|
||||
redis-broker: {condition: service_healthy}
|
||||
mem_limit: 512m
|
||||
cpus: 1.0
|
||||
stop_grace_period: 15s
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=3)"]
|
||||
interval: 5s
|
||||
timeout: 4s
|
||||
retries: 5
|
||||
start_period: 15s
|
||||
|
||||
api2:
|
||||
build: .
|
||||
image: mtusi-messenger-api:5.0.0
|
||||
environment: *api-env
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
s3-bootstrap: {condition: service_completed_successfully}
|
||||
db: {condition: service_started}
|
||||
redis: {condition: service_started}
|
||||
redis-broker: {condition: service_healthy}
|
||||
mem_limit: 512m
|
||||
cpus: 1.0
|
||||
stop_grace_period: 15s
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=3)"]
|
||||
interval: 5s
|
||||
timeout: 4s
|
||||
retries: 5
|
||||
start_period: 15s
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
image: mtusi-messenger-api:5.0.0
|
||||
command: ["alembic", "upgrade", "head"]
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
depends_on:
|
||||
db: {condition: service_started}
|
||||
restart: "no"
|
||||
|
||||
lb:
|
||||
image: nginx:1.27.5-alpine
|
||||
ports:
|
||||
- "${APP_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
depends_on: [api1, api2]
|
||||
mem_limit: 128m
|
||||
cpus: 0.5
|
||||
restart: unless-stopped
|
||||
|
||||
db:
|
||||
image: postgres:16.4-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Create a local .env first}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
volumes: ["postgres_data:/var/lib/postgresql/data"]
|
||||
mem_limit: 1g
|
||||
cpus: 1.0
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always"]
|
||||
volumes: ["redis_data:/data"]
|
||||
mem_limit: 256m
|
||||
cpus: 0.5
|
||||
restart: unless-stopped
|
||||
|
||||
redis-broker:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always"]
|
||||
volumes: ["broker_data:/data"]
|
||||
expose: ["6379"]
|
||||
mem_limit: 256m
|
||||
cpus: 0.5
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 4s
|
||||
retries: 12
|
||||
|
||||
worker:
|
||||
build: .
|
||||
image: mtusi-messenger-api:5.0.0
|
||||
command: ["python", "-m", "src.worker"]
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
S3_ENDPOINT: http://minio:9000
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?Create local S3 credentials}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?Create local S3 credentials}
|
||||
BROKER_URL: redis://redis-broker:6379/0
|
||||
ATTACHMENT_QUEUE: attachment.jobs.v1
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
s3-bootstrap: {condition: service_completed_successfully}
|
||||
redis-broker: {condition: service_healthy}
|
||||
db: {condition: service_started}
|
||||
mem_limit: 512m
|
||||
cpus: 1.0
|
||||
stop_grace_period: 30s
|
||||
restart: unless-stopped
|
||||
|
||||
minio:
|
||||
image: dappros/minio:RELEASE.2025-09-07T16-13-09Z
|
||||
command: ["server", "/data", "--address", ":9000", "--console-address", ":9001"]
|
||||
environment:
|
||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Create local MinIO credentials}
|
||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Create local MinIO credentials}
|
||||
volumes: ["minio_data:/data"]
|
||||
expose: ["9000"]
|
||||
mem_limit: 768m
|
||||
cpus: 1.0
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fsS", "http://127.0.0.1:9000/minio/health/ready"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
|
||||
s3-bootstrap:
|
||||
image: ghcr.io/golithus/mc:RELEASE.2025-08-13T08-35-41Z@sha256:dde55ccc0bc9e65dc582c8e652d652eea1438c6e60d774524f4da15d7b26a900
|
||||
entrypoint: ["/bin/sh", "/bootstrap.sh"]
|
||||
environment:
|
||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?Create local MinIO credentials}
|
||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?Create local MinIO credentials}
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?Create local S3 credentials}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?Create local S3 credentials}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
volumes: ["./infra/minio-bootstrap.sh:/bootstrap.sh:ro"]
|
||||
depends_on:
|
||||
minio: {condition: service_healthy}
|
||||
restart: "no"
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
minio_data:
|
||||
broker_data:
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
python - <<'PY'
|
||||
import os, time
|
||||
from sqlalchemy import create_engine, text
|
||||
url = os.environ["DATABASE_URL"]
|
||||
engine = create_engine(url, connect_args={"connect_timeout": 3})
|
||||
for attempt in range(60):
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
break
|
||||
except Exception:
|
||||
if attempt == 59:
|
||||
raise
|
||||
time.sleep(1)
|
||||
PY
|
||||
exec "$@"
|
||||
@@ -0,0 +1,13 @@
|
||||
Дата проверок: 2026-11-19.
|
||||
|
||||
Команды и результат:
|
||||
- scripts/check_contract.py — PASS: offline skeleton structure and local refs.
|
||||
- python -m compileall -q src alembic scripts student-tests — PASS, exit 0.
|
||||
- docker compose --env-file .env config --quiet — PASS, exit 0.
|
||||
- docker compose up -d — PASS; API replicas healthy, Redis Streams broker healthy, MinIO healthy; GET /health/ready returned 200.
|
||||
- tests/smoke.py --base-url http://localhost:8080 — PASS: lab5 public API scenario including attachment processing, original checksum, thumbnail JPEG dimensions/checksum, ACL and logout.
|
||||
- tests/resilience.py --mode persistence --action-script scripts/restart.bat — PASS: confirmed data and existing session preserved after restarting the API and persistent stores.
|
||||
- tests/resilience.py --mode failover --action-script scripts/stop-one.bat — PASS: 2 API instances observed, 99 successful writes, 0 failed attempts, first recovery 1.812 seconds.
|
||||
- student-tests/worker_recovery.py — PASS: upload remained queued while the worker was stopped; its message reference and durable Stream job survived API/broker restart; after worker restart the original bytes and JPEG thumbnail were ready and checksummed.
|
||||
|
||||
All tests use synthetic users and images. Container lifecycle evidence is stored in persistence-actions.txt and failover-actions.txt. No credentials or session tokens are included.
|
||||
@@ -0,0 +1 @@
|
||||
2026-11-19 23:09:18 +03:00 target=b15098c4-fcae-4326-b6dc-fd25c6260e33 service=api1 container=/mtusi2026lab5-fork-api1-1
|
||||
@@ -0,0 +1,34 @@
|
||||
[2026-11-19 23:04:06 +03:00]
|
||||
before service=db container=/mtusi2026lab5-fork-db-1 started_at=2026-11-19T20:00:39.579332837Z
|
||||
before service=redis container=/mtusi2026lab5-fork-redis-1 started_at=2026-11-19T20:00:39.575373895Z
|
||||
before service=redis-broker container=/mtusi2026lab5-fork-redis-broker-1 started_at=2026-11-19T20:00:39.568858881Z
|
||||
before service=minio container=/mtusi2026lab5-fork-minio-1 started_at=2026-11-19T20:00:39.551271805Z
|
||||
before service=api1 container=/mtusi2026lab5-fork-api1-1 started_at=2026-11-19T20:00:47.198764446Z
|
||||
before service=api2 container=/mtusi2026lab5-fork-api2-1 started_at=2026-11-19T20:00:46.977452442Z
|
||||
before service=worker container=/mtusi2026lab5-fork-worker-1 started_at=2026-11-19T20:00:47.146732512Z
|
||||
before service=lb container=/mtusi2026lab5-fork-lb-1 started_at=2026-11-19T20:00:47.6772409Z
|
||||
after service=db container=/mtusi2026lab5-fork-db-1 started_at=2026-11-19T20:03:35.313346727Z
|
||||
after service=redis container=/mtusi2026lab5-fork-redis-1 started_at=2026-11-19T20:03:35.398165353Z
|
||||
after service=redis-broker container=/mtusi2026lab5-fork-redis-broker-1 started_at=2026-11-19T20:03:35.036261483Z
|
||||
after service=minio container=/mtusi2026lab5-fork-minio-1 started_at=2026-11-19T20:03:34.887589385Z
|
||||
after service=api1 container=/mtusi2026lab5-fork-api1-1 started_at=2026-11-19T20:03:35.824457829Z
|
||||
after service=api2 container=/mtusi2026lab5-fork-api2-1 started_at=2026-11-19T20:03:35.839532317Z
|
||||
after service=worker container=/mtusi2026lab5-fork-worker-1 started_at=2026-11-19T20:04:02.891851437Z
|
||||
after service=lb container=/mtusi2026lab5-fork-lb-1 started_at=2026-11-19T20:03:35.857149684Z
|
||||
[2026-11-19 23:09:01 +03:00]
|
||||
before service=db container=/mtusi2026lab5-fork-db-1 started_at=2026-11-19T20:03:35.313346727Z
|
||||
before service=redis container=/mtusi2026lab5-fork-redis-1 started_at=2026-11-19T20:03:35.398165353Z
|
||||
before service=redis-broker container=/mtusi2026lab5-fork-redis-broker-1 started_at=2026-11-19T20:07:56.052873003Z
|
||||
before service=minio container=/mtusi2026lab5-fork-minio-1 started_at=2026-11-19T20:03:34.887589385Z
|
||||
before service=api1 container=/mtusi2026lab5-fork-api1-1 started_at=2026-11-19T20:07:55.995573487Z
|
||||
before service=api2 container=/mtusi2026lab5-fork-api2-1 started_at=2026-11-19T20:07:55.995460617Z
|
||||
before service=worker container=/mtusi2026lab5-fork-worker-1 started_at=2026-11-19T20:08:01.693702443Z
|
||||
before service=lb container=/mtusi2026lab5-fork-lb-1 started_at=2026-11-19T20:07:55.813349229Z
|
||||
after service=db container=/mtusi2026lab5-fork-db-1 started_at=2026-11-19T20:08:30.153170554Z
|
||||
after service=redis container=/mtusi2026lab5-fork-redis-1 started_at=2026-11-19T20:08:30.692739794Z
|
||||
after service=redis-broker container=/mtusi2026lab5-fork-redis-broker-1 started_at=2026-11-19T20:08:29.755589166Z
|
||||
after service=minio container=/mtusi2026lab5-fork-minio-1 started_at=2026-11-19T20:08:30.153115348Z
|
||||
after service=api1 container=/mtusi2026lab5-fork-api1-1 started_at=2026-11-19T20:08:30.692688221Z
|
||||
after service=api2 container=/mtusi2026lab5-fork-api2-1 started_at=2026-11-19T20:08:30.715507613Z
|
||||
after service=worker container=/mtusi2026lab5-fork-worker-1 started_at=2026-11-19T20:08:57.830135515Z
|
||||
after service=lb container=/mtusi2026lab5-fork-lb-1 started_at=2026-11-19T20:08:30.615613474Z
|
||||
@@ -0,0 +1,4 @@
|
||||
Worker recovery demonstration — 2026-11-19
|
||||
Command: python student-tests/worker_recovery.py
|
||||
Result: PASS. Stopped worker; uploaded sample.png and received 202/queued; linked it in a message; original content returned 409 while queued; restarted api1, api2, redis-broker and load balancer with the worker stopped; started worker; attachment reached ready. Original bytes matched fixture SHA-256. Thumbnail downloaded as JPEG at 256x160 and matched its metadata SHA-256.
|
||||
Container lifecycle timestamps are recorded in evidence/persistence-actions.txt. Test users are synthetic. No tokens or credentials recorded.
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
until mc alias set local http://minio:9000 "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" >/dev/null 2>&1; do sleep 2; done
|
||||
mc mb --ignore-existing "local/$S3_BUCKET"
|
||||
mc anonymous set none "local/$S3_BUCKET"
|
||||
if ! mc admin user info local "$S3_ACCESS_KEY" >/dev/null 2>&1; then
|
||||
mc admin user add local "$S3_ACCESS_KEY" "$S3_SECRET_KEY"
|
||||
fi
|
||||
cat >/tmp/attachment-policy.json <<EOF
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::$S3_BUCKET"],"Condition":{"StringLike":{"s3:prefix":["staging/*","original/*","variants/*"]}}},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::$S3_BUCKET/staging/*","arn:aws:s3:::$S3_BUCKET/original/*","arn:aws:s3:::$S3_BUCKET/variants/*"]}]}
|
||||
EOF
|
||||
if ! mc admin policy info local attachments-api >/dev/null 2>&1; then
|
||||
mc admin policy create local attachments-api /tmp/attachment-policy.json
|
||||
fi
|
||||
mc admin policy attach local attachments-api --user "$S3_ACCESS_KEY"
|
||||
@@ -0,0 +1,42 @@
|
||||
upstream api_backend {
|
||||
zone api_backend 64k;
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
server api1:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
server api2:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
# Leave a small margin so the API returns its documented JSON 413 at 10 MiB.
|
||||
client_max_body_size 11m;
|
||||
client_body_timeout 20s;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_send_timeout 20s;
|
||||
proxy_read_timeout 20s;
|
||||
error_page 502 504 = @upstream_unavailable;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Request-Id $request_id;
|
||||
proxy_intercept_errors on;
|
||||
# Retry GET/HEAD on a failed backend. Nginx never replays a POST after
|
||||
# its request has been sent unless `non_idempotent` is enabled; uploads
|
||||
# therefore remain single-shot, while message retries stay client-led.
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
}
|
||||
|
||||
# Keep the public API error contract when all upstreams are unavailable.
|
||||
location @upstream_unavailable {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
add_header X-Instance-Id "lb" always;
|
||||
return 503 '{"error":{"code":"unavailable","message":"API upstream unavailable","request_id":"$request_id"}}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fastapi==0.109.0
|
||||
uvicorn[standard]==0.27.0
|
||||
pydantic==2.5.3
|
||||
sqlalchemy==2.0.25
|
||||
alembic==1.13.1
|
||||
psycopg2-binary==2.9.9
|
||||
redis==5.0.1
|
||||
boto3==1.35.99
|
||||
Pillow==11.0.0
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
setlocal
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,21 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
function Get-ContainerState($phase) {
|
||||
foreach ($service in @('db', 'redis', 'redis-broker', 'minio', 'api1', 'api2', 'worker', 'lb')) {
|
||||
$containers = docker compose --env-file .env ps -q $service
|
||||
if ($LASTEXITCODE -ne 0) { throw "Cannot list $service containers" }
|
||||
foreach ($container in $containers) {
|
||||
$name = docker inspect --format '{{.Name}}' $container
|
||||
$started = docker inspect --format '{{.State.StartedAt}}' $container
|
||||
"$phase service=$service container=$name started_at=$started"
|
||||
}
|
||||
}
|
||||
}
|
||||
$before = @(Get-ContainerState 'before')
|
||||
docker compose --env-file .env restart db redis redis-broker minio api1 api2 worker lb | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
$after = @(Get-ContainerState 'after')
|
||||
$stamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss K'
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value "[$stamp]"
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value $before
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value $after
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
setlocal
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,22 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
$target = $env:TARGET_INSTANCE_ID
|
||||
if ([string]::IsNullOrWhiteSpace($target)) { exit 2 }
|
||||
foreach ($service in @('api1', 'api2')) {
|
||||
$containers = docker compose --env-file .env ps -q $service
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
foreach ($container in $containers) {
|
||||
try {
|
||||
$instance = docker exec $container python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).headers['X-Instance-Id'])" 2>$null
|
||||
if ($LASTEXITCODE -eq 0 -and $instance.Trim() -eq $target) {
|
||||
$name = docker inspect --format '{{.Name}}' $container
|
||||
$stamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss K'
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\failover-actions.txt') -Value "$stamp target=$target service=$service container=$name"
|
||||
docker stop --time 5 $container | Out-Null
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
Write-Error 'No running API container matched TARGET_INSTANCE_ID'
|
||||
exit 1
|
||||
@@ -0,0 +1 @@
|
||||
"""MTUSI Messenger service package."""
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"""scrypt password KDF and opaque, cryptographically random bearer tokens."""
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
import secrets
|
||||
|
||||
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
salt = secrets.token_bytes(16)
|
||||
digest = hashlib.scrypt(password.encode("utf-8"), salt=salt, n=2**14, r=8, p=1, dklen=32)
|
||||
return "scrypt$16384$8$1$" + base64.urlsafe_b64encode(salt + digest).decode("ascii")
|
||||
|
||||
|
||||
def verify_password(password: str, encoded: str) -> bool:
|
||||
try:
|
||||
scheme, n, r, p, raw = encoded.split("$", 4)
|
||||
if scheme != "scrypt":
|
||||
return False
|
||||
data = base64.urlsafe_b64decode(raw.encode("ascii"))
|
||||
salt, expected = data[:16], data[16:]
|
||||
actual = hashlib.scrypt(password.encode("utf-8"), salt=salt, n=int(n), r=int(r), p=int(p), dklen=len(expected))
|
||||
return hmac.compare_digest(actual, expected)
|
||||
except (ValueError, TypeError):
|
||||
return False
|
||||
|
||||
|
||||
def new_token() -> str:
|
||||
return secrets.token_urlsafe(32)
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
"""PostgreSQL schema and bounded connection pool."""
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import (Column, DateTime, ForeignKey, Index, String, Text, Integer, JSON,
|
||||
UniqueConstraint, create_engine)
|
||||
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@db:5432/messenger")
|
||||
engine = create_engine(DATABASE_URL, pool_size=5, max_overflow=5, pool_timeout=5,
|
||||
pool_pre_ping=True, connect_args={"connect_timeout": 5,
|
||||
"options": "-c statement_timeout=5000"})
|
||||
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
Base = declarative_base()
|
||||
now = lambda: datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
username = Column(String(32), nullable=False)
|
||||
display_name = Column(String(100), nullable=False)
|
||||
password_hash = Column(String(255), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (Index("ix_users_username", "username", unique=True),)
|
||||
|
||||
|
||||
class Session(Base):
|
||||
__tablename__ = "sessions"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
token = Column(String(255), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
user = relationship("User")
|
||||
__table_args__ = (Index("ix_sessions_token", "token", unique=True),)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
title = Column(String(100), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
members = relationship("ChatMember", back_populates="chat", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
chat = relationship("Chat", back_populates="members")
|
||||
user = relationship("User")
|
||||
__table_args__ = (UniqueConstraint("chat_id", "user_id", name="uq_chat_member"),
|
||||
Index("ix_chat_members_user_chat", "user_id", "chat_id"))
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
sender_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
text = Column(Text, nullable=False)
|
||||
attachment_ids = Column(JSON, nullable=False, default=list)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
chat = relationship("Chat")
|
||||
sender = relationship("User")
|
||||
__table_args__ = (Index("ix_messages_chat_created_id", "chat_id", "created_at", "id"),)
|
||||
|
||||
|
||||
class Attachment(Base):
|
||||
__tablename__ = "attachments"
|
||||
id = Column(String, primary_key=True)
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False, index=True)
|
||||
owner_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
filename = Column(String(255), nullable=False)
|
||||
content_type = Column(String(32), nullable=False)
|
||||
size_bytes = Column(Integer, nullable=False)
|
||||
sha256 = Column(String(64), nullable=False)
|
||||
object_key = Column(String(128), nullable=False, unique=True)
|
||||
staging_key = Column(String(128), nullable=False, unique=True)
|
||||
status = Column(String(16), nullable=False, default="ready")
|
||||
job_id = Column(String(36), nullable=False, unique=True)
|
||||
error_code = Column(String(64), nullable=True)
|
||||
variants = Column(JSON, nullable=False, default=list)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
updated_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
|
||||
|
||||
class MessageIdempotency(Base):
|
||||
__tablename__ = "message_idempotency"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
method = Column(String(8), nullable=False)
|
||||
chat_path = Column(String(80), nullable=False)
|
||||
key = Column(String(128), nullable=False)
|
||||
request_hash = Column(String(64), nullable=False)
|
||||
message_id = Column(String, ForeignKey("messages.id"), nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (UniqueConstraint("user_id", "method", "chat_path", "key",
|
||||
name="uq_idempotency_scope"),)
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"""Redis Streams producer for durable attachment jobs."""
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
import redis
|
||||
|
||||
QUEUE = os.getenv("ATTACHMENT_QUEUE", "attachment.jobs.v1")
|
||||
CONSUMER_GROUP = os.getenv("ATTACHMENT_CONSUMER_GROUP", "attachment-workers-v1")
|
||||
MAX_ATTEMPTS = int(os.getenv("JOB_MAX_ATTEMPTS", "5"))
|
||||
|
||||
|
||||
def client():
|
||||
return redis.Redis.from_url(os.environ["BROKER_URL"], decode_responses=True,
|
||||
socket_connect_timeout=3, socket_timeout=10,
|
||||
retry_on_timeout=True)
|
||||
|
||||
|
||||
def publish(attachment_id, job_id, attempt=1):
|
||||
job = {"schema_version": 1, "job_id": job_id,
|
||||
"type": "attachment.process.v1", "attachment_id": attachment_id,
|
||||
"pipeline_version": 1, "attempt": attempt,
|
||||
"created_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")}
|
||||
client().xadd(QUEUE, {"body": json.dumps(job, separators=(",", ":"))})
|
||||
|
||||
|
||||
def ping():
|
||||
client().ping()
|
||||
+382
@@ -0,0 +1,382 @@
|
||||
"""Lab 4 REST API: shared messenger state and private S3 attachments."""
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
import hashlib
|
||||
import tempfile
|
||||
import re as regex
|
||||
from datetime import timezone
|
||||
from fastapi import FastAPI, Request, Query
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
import redis
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from .database import SessionLocal
|
||||
from .models import CreateUser, CreateChat, CreateMessage
|
||||
from .store import store, cursor_for_chat, cursor_for_message
|
||||
from . import session_cache
|
||||
from . import object_store
|
||||
from . import jobs
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(self, status, code, message, headers=None):
|
||||
self.status, self.code, self.message, self.headers = status, code, message, headers or {}
|
||||
|
||||
|
||||
app = FastAPI(title="MTUSI Messenger", version="5.0.0")
|
||||
INSTANCE_ID = str(uuid.uuid4())
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_headers(request: Request, call_next):
|
||||
request.state.request_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return response
|
||||
|
||||
|
||||
@app.exception_handler(AppError)
|
||||
async def app_error_handler(request, exc):
|
||||
headers = {"X-Request-Id": request.state.request_id, **exc.headers}
|
||||
headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return JSONResponse(status_code=exc.status, content={"error":{"code":exc.code,"message":exc.message,"request_id":request.state.request_id}}, headers=headers)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_handler(request, exc):
|
||||
return JSONResponse(status_code=400, content={"error":{"code":"invalid_request","message":"Invalid request","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
@app.exception_handler(SQLAlchemyError)
|
||||
async def database_error_handler(request, exc):
|
||||
return JSONResponse(status_code=503, content={"error":{"code":"unavailable","message":"Database unavailable","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def internal_handler(request, exc):
|
||||
return JSONResponse(status_code=500, content={"error":{"code":"internal_error","message":"Internal server error","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
def stamp(value):
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
|
||||
|
||||
def user_json(user):
|
||||
return {"id":user.id,"username":user.username,"display_name":user.display_name,"created_at":stamp(user.created_at)}
|
||||
|
||||
|
||||
def auth_user(request):
|
||||
header = request.headers.get("Authorization", "")
|
||||
if not header.startswith("Bearer ") or not header[7:]:
|
||||
raise AppError(401,"unauthorized","Authentication required",{"WWW-Authenticate":'Bearer realm="messenger"'})
|
||||
try:
|
||||
found = store.get_session_by_token(header[7:])
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Authentication store unavailable")
|
||||
if not found:
|
||||
raise AppError(401,"unauthorized","Invalid or expired session",{"WWW-Authenticate":'Bearer realm="messenger"'})
|
||||
return found[0]
|
||||
|
||||
|
||||
def chat_json(chat):
|
||||
return {"id":chat.id,"title":chat.title,"member_ids":[m.user_id for m in chat.members],"created_at":stamp(chat.created_at)}
|
||||
|
||||
|
||||
def validate_uuid(value):
|
||||
try:
|
||||
uuid.UUID(value)
|
||||
except (ValueError, AttributeError):
|
||||
raise AppError(400,"invalid_request","Invalid UUID")
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
async def live():
|
||||
return {"status":"ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def ready():
|
||||
try:
|
||||
with SessionLocal() as db:
|
||||
db.execute(text("SELECT 1"))
|
||||
session_cache.ping()
|
||||
object_store.s3.list_objects_v2(Bucket=object_store.S3_BUCKET, Prefix="original/", MaxKeys=1)
|
||||
await run_in_threadpool(jobs.ping)
|
||||
return {"status":"ok"}
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
except (BotoCoreError, ClientError):
|
||||
raise AppError(503,"unavailable","Object storage unavailable")
|
||||
except (redis.exceptions.RedisError, OSError):
|
||||
raise AppError(503,"unavailable","Attachment queue unavailable")
|
||||
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
async def register(data: CreateUser):
|
||||
if not data.display_name.strip() or not 12 <= len(data.password) <= 128:
|
||||
raise AppError(400,"invalid_request","Invalid user fields")
|
||||
try:
|
||||
return user_json(store.add_user(data.username,data.display_name,data.password))
|
||||
except ValueError as exc:
|
||||
if str(exc) == "USERNAME_EXISTS":
|
||||
raise AppError(409,"conflict","Username already exists")
|
||||
raise AppError(400,"invalid_request","Invalid user fields")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
async def login(request: Request):
|
||||
header = request.headers.get("Authorization", "")
|
||||
challenge = {'WWW-Authenticate':'Basic realm="messenger", charset="UTF-8"'}
|
||||
if not header.startswith("Basic "):
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
try:
|
||||
decoded = base64.b64decode(header[6:], validate=True).decode("utf-8")
|
||||
username, password = decoded.split(":", 1)
|
||||
except (binascii.Error, UnicodeError, ValueError):
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
try:
|
||||
user = store.verify_user_password(username,password)
|
||||
if not user:
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
session = store.create_session(user.id)
|
||||
return {"token":session.token,"token_type":"Bearer","expires_at":stamp(session.expires_at),"user":user_json(user)}
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
async def logout(request: Request):
|
||||
auth_user(request)
|
||||
try:
|
||||
store.delete_session(request.headers["Authorization"][7:])
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Authentication store unavailable")
|
||||
return None
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
async def me(request: Request):
|
||||
user_id = auth_user(request)
|
||||
user = store.get_user_by_id(user_id)
|
||||
if not user:
|
||||
raise AppError(401,"unauthorized","Invalid session",{'WWW-Authenticate':'Bearer realm="messenger"'})
|
||||
return user_json(user)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
async def create_chat(data: CreateChat, request: Request):
|
||||
user_id = auth_user(request)
|
||||
if not data.title.strip():
|
||||
raise AppError(400,"invalid_request","Invalid title")
|
||||
try:
|
||||
return chat_json(store.create_chat(data.title,data.member_ids,user_id))
|
||||
except ValueError as exc:
|
||||
if str(exc)=="USER_NOT_FOUND":
|
||||
raise AppError(400,"invalid_request","Unknown member")
|
||||
raise AppError(400,"invalid_request","Invalid members")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
async def list_chats(request: Request, limit: int = Query(50,ge=1,le=100), cursor: str|None=None):
|
||||
user_id=auth_user(request)
|
||||
try:
|
||||
rows=store.get_user_chats(user_id,cursor,limit+1)
|
||||
except ValueError:
|
||||
raise AppError(400,"invalid_request","Invalid cursor")
|
||||
more=len(rows)>limit
|
||||
rows=rows[:limit]
|
||||
return {"items":[chat_json(c) for c in rows],"next_cursor":cursor_for_chat(rows[-1]) if more else None}
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
async def get_chat(chat_id: str, request: Request):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if not store.is_chat_member(chat_id,user_id):
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
chat=store.get_chat(chat_id)
|
||||
if not chat:
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
return chat_json(chat)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
async def create_message(chat_id: str, data: CreateMessage, request: Request):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if data.text is None or len(data.text)>4000 or len(data.attachment_ids) != len(set(data.attachment_ids)):
|
||||
raise AppError(400,"invalid_request","Invalid message text")
|
||||
for attachment_id in data.attachment_ids:
|
||||
validate_uuid(attachment_id)
|
||||
if not data.text.strip() and not data.attachment_ids:
|
||||
raise AppError(400,"invalid_request","Message must contain text or attachments")
|
||||
key = request.headers.get("Idempotency-Key", "")
|
||||
if not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key, flags=re.ASCII):
|
||||
raise AppError(400,"invalid_request","A valid Idempotency-Key is required")
|
||||
canonical = json.dumps({"text":data.text,"attachment_ids":data.attachment_ids}, ensure_ascii=False,
|
||||
sort_keys=True, separators=(",", ":"))
|
||||
request_hash = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
try:
|
||||
msg=store.add_message_idempotent(chat_id,user_id,data.text,data.attachment_ids,key,request_hash)
|
||||
return {"id":msg.id,"chat_id":msg.chat_id,"sender_id":msg.sender_id,"text":msg.text,"created_at":stamp(msg.created_at),"attachment_ids":msg.attachment_ids}
|
||||
except ValueError as exc:
|
||||
if str(exc) == "IDEMPOTENCY_CONFLICT":
|
||||
raise AppError(409,"conflict","Idempotency key was used with a different request")
|
||||
if str(exc) == "EMPTY_MESSAGE":
|
||||
raise AppError(400,"invalid_request","Message must contain text or attachments")
|
||||
if str(exc) == "ATTACHMENT_FAILED":
|
||||
raise AppError(409,"conflict","Failed attachment cannot be added to a message")
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
async def list_messages(chat_id: str, request: Request, limit: int=Query(50,ge=1,le=100), cursor: str|None=None):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if not store.is_chat_member(chat_id,user_id):
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
try:
|
||||
rows=store.get_messages(chat_id,cursor,limit+1)
|
||||
except ValueError:
|
||||
raise AppError(400,"invalid_request","Invalid cursor")
|
||||
more=len(rows)>limit
|
||||
rows=rows[:limit]
|
||||
items=[{"id":m.id,"chat_id":m.chat_id,"sender_id":m.sender_id,"text":m.text,"created_at":stamp(m.created_at),"attachment_ids":m.attachment_ids or []} for m in rows]
|
||||
return {"items":items,"next_cursor":cursor_for_message(rows[-1]) if more else None}
|
||||
|
||||
|
||||
def attachment_json(row):
|
||||
return {"id":row.id,"chat_id":row.chat_id,"owner_id":row.owner_id,"filename":row.filename,
|
||||
"content_type":row.content_type,"size_bytes":row.size_bytes,"sha256":row.sha256,
|
||||
"status":row.status,"variants":row.variants or [],"error_code":row.error_code,
|
||||
"created_at":stamp(row.created_at),"updated_at":stamp(row.updated_at)}
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=202)
|
||||
async def upload_attachment(chat_id: str, request: Request, filename: str = Query(...)):
|
||||
user_id = auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if not store.is_chat_member(chat_id, user_id):
|
||||
raise AppError(404, "not_found", "Chat not found")
|
||||
if not 1 <= len(filename) <= 255 or any(ch in filename for ch in "/\\\x00") or any(ord(ch) < 32 or 127 <= ord(ch) < 160 for ch in filename):
|
||||
raise AppError(400, "invalid_request", "Invalid filename")
|
||||
declared = request.headers.get("content-type", "").split(";", 1)[0].lower()
|
||||
if declared not in ("image/png", "image/jpeg"):
|
||||
raise AppError(415, "unsupported_media_type", "Only PNG and JPEG are accepted")
|
||||
max_bytes = 10 * 1024 * 1024
|
||||
spool = tempfile.SpooledTemporaryFile(max_size=1024 * 1024, mode="w+b")
|
||||
digest = hashlib.sha256()
|
||||
size = 0
|
||||
try:
|
||||
async for chunk in request.stream():
|
||||
size += len(chunk)
|
||||
if size > max_bytes:
|
||||
raise AppError(413, "payload_too_large", "Image exceeds 10 MiB")
|
||||
digest.update(chunk)
|
||||
spool.write(chunk)
|
||||
if size == 0:
|
||||
raise AppError(415, "unsupported_media_type", "Empty image")
|
||||
spool.seek(0)
|
||||
try:
|
||||
image = Image.open(spool)
|
||||
fmt = image.format
|
||||
width, height = image.size
|
||||
if width * height > 40_000_000:
|
||||
raise AppError(413, "payload_too_large", "Image exceeds 40 million pixels")
|
||||
if (fmt, declared) not in (("PNG", "image/png"), ("JPEG", "image/jpeg")):
|
||||
raise AppError(415, "unsupported_media_type", "Image content does not match Content-Type")
|
||||
image.verify()
|
||||
except AppError:
|
||||
raise
|
||||
except (UnidentifiedImageError, OSError, Image.DecompressionBombError, Image.DecompressionBombWarning):
|
||||
raise AppError(415, "unsupported_media_type", "Invalid or unsupported image")
|
||||
object_id = str(uuid.uuid4())
|
||||
object_key = f"original/{object_id}"
|
||||
staging_key = f"staging/{object_id}"
|
||||
job_id = str(uuid.uuid4())
|
||||
spool.seek(0)
|
||||
await run_in_threadpool(object_store.s3.put_object, Bucket=object_store.S3_BUCKET,
|
||||
Key=staging_key, Body=spool, ContentLength=size, ContentType=declared)
|
||||
row = store.save_attachment(dict(id=object_id, chat_id=chat_id, owner_id=user_id,
|
||||
filename=filename, content_type=declared, size_bytes=size, sha256=digest.hexdigest(),
|
||||
object_key=object_key, staging_key=staging_key, status="queued", job_id=job_id,
|
||||
variants=[], error_code=None))
|
||||
await run_in_threadpool(jobs.publish, object_id, job_id)
|
||||
return attachment_json(row)
|
||||
except AppError:
|
||||
raise
|
||||
except (BotoCoreError, ClientError):
|
||||
raise AppError(503, "unavailable", "Object storage unavailable")
|
||||
except (redis.exceptions.RedisError, OSError, RuntimeError):
|
||||
raise AppError(503, "unavailable", "Attachment queue unavailable")
|
||||
finally:
|
||||
spool.close()
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}")
|
||||
async def get_attachment(attachment_id: str, request: Request):
|
||||
user_id = auth_user(request)
|
||||
validate_uuid(attachment_id)
|
||||
row = store.get_attachment(attachment_id, user_id)
|
||||
if not row:
|
||||
raise AppError(404, "not_found", "Attachment not found")
|
||||
return attachment_json(row)
|
||||
|
||||
|
||||
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||||
async def download_attachment(attachment_id: str, request: Request, variant: str = "original"):
|
||||
user_id = auth_user(request)
|
||||
validate_uuid(attachment_id)
|
||||
row = store.get_attachment(attachment_id, user_id)
|
||||
if not row:
|
||||
raise AppError(404, "not_found", "Attachment not found")
|
||||
if row.status in ("queued", "processing", "failed"):
|
||||
raise AppError(409, "conflict", "Attachment content is not ready")
|
||||
if variant == "original":
|
||||
key, content_type, length = row.object_key, row.content_type, row.size_bytes
|
||||
elif variant in {item.get("name") for item in row.variants or []}:
|
||||
key = f"variants/{attachment_id}/{variant}"
|
||||
item = next(item for item in row.variants if item.get("name") == variant)
|
||||
content_type, length = item["content_type"], item["size_bytes"]
|
||||
else:
|
||||
raise AppError(404, "not_found", "Attachment variant not found")
|
||||
try:
|
||||
obj = await run_in_threadpool(object_store.s3.get_object, Bucket=object_store.S3_BUCKET, Key=key)
|
||||
except (BotoCoreError, ClientError):
|
||||
raise AppError(503, "unavailable", "Object storage unavailable")
|
||||
body = obj["Body"]
|
||||
def chunks():
|
||||
try:
|
||||
while True:
|
||||
chunk = body.read(64 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
yield chunk
|
||||
finally:
|
||||
body.close()
|
||||
return StreamingResponse(chunks(), media_type=content_type, headers={"Content-Length": str(length)})
|
||||
@@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import List
|
||||
|
||||
|
||||
# ==================== REQUEST MODELS ====================
|
||||
|
||||
class CreateUser(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", str_strip_whitespace=False)
|
||||
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(..., min_length=1, max_length=100)
|
||||
password: str = Field(..., min_length=12, max_length=128)
|
||||
|
||||
|
||||
class CreateChat(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)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Private S3-compatible object storage client."""
|
||||
import os
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
|
||||
S3_BUCKET = os.environ.get("S3_BUCKET", "messenger-attachments")
|
||||
s3 = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=os.environ.get("S3_ENDPOINT", "http://minio:9000"),
|
||||
aws_access_key_id=os.environ["S3_ACCESS_KEY"],
|
||||
aws_secret_access_key=os.environ["S3_SECRET_KEY"],
|
||||
region_name=os.environ.get("S3_REGION", "us-east-1"),
|
||||
config=Config(connect_timeout=3, read_timeout=10, retries={"max_attempts": 2}),
|
||||
)
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Redis TTL cache for active sessions; PostgreSQL remains authoritative."""
|
||||
import os
|
||||
import socket
|
||||
from urllib.parse import urlparse
|
||||
from redis import Redis
|
||||
from redis.backoff import NoBackoff
|
||||
from redis.exceptions import RedisError
|
||||
from redis.retry import Retry
|
||||
|
||||
CACHE_URL = os.getenv("CACHE_URL", "redis://redis:6379/0")
|
||||
_cache_address = urlparse(CACHE_URL)
|
||||
_cache_host = _cache_address.hostname or "redis"
|
||||
_cache_port = _cache_address.port or (6380 if _cache_address.scheme == "rediss" else 6379)
|
||||
try:
|
||||
_cache_ips = tuple(dict.fromkeys(item[4][0] for item in socket.getaddrinfo(
|
||||
_cache_host, _cache_port, type=socket.SOCK_STREAM)))
|
||||
except OSError:
|
||||
_cache_ips = (_cache_host,)
|
||||
client = Redis.from_url(CACHE_URL, decode_responses=True,
|
||||
socket_connect_timeout=0.2, socket_timeout=0.2,
|
||||
health_check_interval=15, retry=Retry(NoBackoff(), 0))
|
||||
|
||||
|
||||
class CacheUnavailable(Exception):
|
||||
"""The required session cache cannot be reached within its short timeout."""
|
||||
|
||||
|
||||
def _check_port():
|
||||
# Fail fast when the Redis container is stopped. redis-py may otherwise
|
||||
# spend several seconds cycling through connection retries on Docker DNS.
|
||||
try:
|
||||
for address in _cache_ips:
|
||||
try:
|
||||
with socket.create_connection((address, _cache_port), timeout=0.2):
|
||||
return
|
||||
except OSError:
|
||||
continue
|
||||
raise OSError("Redis is not accepting connections")
|
||||
except OSError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def get(token: str):
|
||||
try:
|
||||
_check_port()
|
||||
return client.get("session:" + token)
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def put(token: str, user_id: str, ttl_seconds: int):
|
||||
try:
|
||||
_check_port()
|
||||
client.set("session:" + token, user_id, ex=max(1, ttl_seconds))
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def delete(token: str):
|
||||
try:
|
||||
_check_port()
|
||||
client.delete("session:" + token)
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def ping():
|
||||
try:
|
||||
_check_port()
|
||||
return bool(client.ping())
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
"""Small repository layer; each operation owns and closes its DB transaction."""
|
||||
import base64
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone, timedelta
|
||||
import math
|
||||
from sqlalchemy import and_, or_, select, update
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from .database import User, Session as SessionRow, Chat, ChatMember, Message, MessageIdempotency, Attachment, SessionLocal
|
||||
from .auth import hash_password, verify_password, new_token, SESSION_TTL_SECONDS
|
||||
from . import session_cache
|
||||
|
||||
|
||||
class Store:
|
||||
def add_user(self, username, display_name, password):
|
||||
with SessionLocal() as db:
|
||||
user = User(username=username, display_name=display_name, password_hash=hash_password(password))
|
||||
db.add(user)
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
raise ValueError("USERNAME_EXISTS")
|
||||
db.refresh(user)
|
||||
return user
|
||||
|
||||
def get_user_by_id(self, user_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
def verify_user_password(self, username, password):
|
||||
with SessionLocal() as db:
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
# A fixed dummy hash is avoided here; the public contract only requires
|
||||
# identical response status and challenge for unknown users/bad passwords.
|
||||
if not user or not verify_password(password, user.password_hash):
|
||||
return None
|
||||
return user
|
||||
|
||||
def create_session(self, user_id):
|
||||
with SessionLocal() as db:
|
||||
row = SessionRow(token=new_token(), user_id=user_id,
|
||||
expires_at=datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS))
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
try:
|
||||
session_cache.put(row.token, row.user_id, SESSION_TTL_SECONDS)
|
||||
except session_cache.CacheUnavailable:
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
raise
|
||||
return row
|
||||
|
||||
def get_session_by_token(self, token):
|
||||
cached_user_id = session_cache.get(token)
|
||||
if cached_user_id:
|
||||
return (cached_user_id, None)
|
||||
with SessionLocal() as db:
|
||||
row = db.query(SessionRow).filter(SessionRow.token == token,
|
||||
SessionRow.expires_at > datetime.now(timezone.utc)).first()
|
||||
if not row:
|
||||
return None
|
||||
remaining = math.ceil((row.expires_at - datetime.now(timezone.utc)).total_seconds())
|
||||
session_cache.put(row.token, row.user_id, remaining)
|
||||
return (row.user_id, row.expires_at)
|
||||
|
||||
def delete_session(self, token):
|
||||
# Require successful cache invalidation before reporting a completed logout.
|
||||
session_cache.delete(token)
|
||||
with SessionLocal() as db:
|
||||
row = db.query(SessionRow).filter(SessionRow.token == token).first()
|
||||
if row:
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def create_chat(self, title, member_ids, creator_id):
|
||||
if creator_id not in member_ids:
|
||||
raise ValueError("CREATOR_NOT_MEMBER")
|
||||
if len(member_ids) != len(set(member_ids)):
|
||||
raise ValueError("DUPLICATE_MEMBER")
|
||||
with SessionLocal() as db:
|
||||
users = db.query(User.id).filter(User.id.in_(member_ids)).all()
|
||||
if len(users) != len(member_ids):
|
||||
raise ValueError("USER_NOT_FOUND")
|
||||
chat = Chat(title=title)
|
||||
chat.members = [ChatMember(user_id=mid) for mid in member_ids]
|
||||
db.add(chat)
|
||||
db.commit()
|
||||
db.refresh(chat)
|
||||
list(chat.members)
|
||||
return chat
|
||||
|
||||
def get_chat(self, chat_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(Chat).options(selectinload(Chat.members)).filter(Chat.id == chat_id).first()
|
||||
|
||||
def get_user_chats(self, user_id, cursor=None, limit=50):
|
||||
with SessionLocal() as db:
|
||||
query = db.query(Chat).options(selectinload(Chat.members)).join(ChatMember).filter(ChatMember.user_id == user_id)
|
||||
if cursor:
|
||||
decoded = decode_cursor(cursor, "chats")
|
||||
query = query.filter(or_(Chat.created_at > decoded[0], and_(Chat.created_at == decoded[0], Chat.id > decoded[1])))
|
||||
rows = query.order_by(Chat.created_at, Chat.id).limit(limit).all()
|
||||
for row in rows:
|
||||
list(row.members)
|
||||
return rows
|
||||
|
||||
def is_chat_member(self, chat_id, user_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id).first() is not None
|
||||
|
||||
def add_message(self, chat_id, sender_id, text):
|
||||
with SessionLocal() as db:
|
||||
if not db.query(Chat.id).filter(Chat.id == chat_id).first():
|
||||
raise ValueError("CHAT_NOT_FOUND")
|
||||
if not db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id, ChatMember.user_id == sender_id).first():
|
||||
raise ValueError("NOT_MEMBER")
|
||||
msg = Message(chat_id=chat_id, sender_id=sender_id, text=text)
|
||||
db.add(msg)
|
||||
db.commit()
|
||||
db.refresh(msg)
|
||||
return msg
|
||||
|
||||
def add_message_idempotent(self, chat_id, sender_id, text, attachment_ids, key, request_hash):
|
||||
"""Claim a scoped key and create its message in the same DB transaction."""
|
||||
with SessionLocal() as db:
|
||||
if not db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id,
|
||||
ChatMember.user_id == sender_id).first():
|
||||
raise ValueError("CHAT_NOT_FOUND")
|
||||
if not text.strip() and not attachment_ids:
|
||||
raise ValueError("EMPTY_MESSAGE")
|
||||
if attachment_ids:
|
||||
rows = db.query(Attachment).filter(Attachment.id.in_(attachment_ids),
|
||||
Attachment.chat_id == chat_id).all()
|
||||
if len(rows) != len(attachment_ids):
|
||||
raise ValueError("ATTACHMENT_NOT_FOUND")
|
||||
if any(row.status == "failed" for row in rows):
|
||||
raise ValueError("ATTACHMENT_FAILED")
|
||||
scope = dict(user_id=sender_id, method="POST", chat_path=f"/api/v1/chats/{chat_id}/messages", key=key)
|
||||
claim = insert(MessageIdempotency).values(**scope, request_hash=request_hash,
|
||||
message_id=None).on_conflict_do_nothing(
|
||||
constraint="uq_idempotency_scope").returning(MessageIdempotency.id)
|
||||
claimed = db.execute(claim).scalar_one_or_none()
|
||||
if claimed is None:
|
||||
record = db.execute(select(MessageIdempotency).filter_by(**scope)).scalar_one()
|
||||
if record.request_hash != request_hash:
|
||||
raise ValueError("IDEMPOTENCY_CONFLICT")
|
||||
msg = db.get(Message, record.message_id)
|
||||
if msg is None:
|
||||
raise RuntimeError("Idempotency record has no message")
|
||||
return msg
|
||||
msg = Message(chat_id=chat_id, sender_id=sender_id, text=text, attachment_ids=attachment_ids)
|
||||
db.add(msg)
|
||||
db.flush()
|
||||
db.execute(update(MessageIdempotency).where(MessageIdempotency.id == claimed).values(message_id=msg.id))
|
||||
db.commit()
|
||||
db.refresh(msg)
|
||||
return msg
|
||||
|
||||
def get_messages(self, chat_id, cursor=None, limit=50):
|
||||
with SessionLocal() as db:
|
||||
query = db.query(Message).filter(Message.chat_id == chat_id)
|
||||
if cursor:
|
||||
created, message_id = decode_cursor(cursor, chat_id)
|
||||
query = query.filter(or_(Message.created_at > created,
|
||||
and_(Message.created_at == created, Message.id > message_id)))
|
||||
return query.order_by(Message.created_at, Message.id).limit(limit).all()
|
||||
|
||||
def save_attachment(self, attachment):
|
||||
with SessionLocal() as db:
|
||||
row = Attachment(**attachment)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return row
|
||||
|
||||
def get_attachment_for_worker(self, attachment_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(Attachment).filter(Attachment.id == attachment_id).first()
|
||||
|
||||
def set_attachment_processing(self, attachment_id):
|
||||
with SessionLocal() as db:
|
||||
row = db.query(Attachment).filter(Attachment.id == attachment_id).with_for_update().first()
|
||||
if not row or row.status == "ready":
|
||||
return row
|
||||
row.status = "processing"
|
||||
row.error_code = None
|
||||
row.updated_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return row
|
||||
|
||||
def finish_attachment(self, attachment_id, variants):
|
||||
with SessionLocal() as db:
|
||||
row = db.query(Attachment).filter(Attachment.id == attachment_id).with_for_update().first()
|
||||
if row and row.status != "ready":
|
||||
row.variants = variants
|
||||
row.status = "ready"
|
||||
row.error_code = None
|
||||
row.updated_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return row
|
||||
|
||||
def fail_attachment(self, attachment_id, error_code):
|
||||
with SessionLocal() as db:
|
||||
row = db.query(Attachment).filter(Attachment.id == attachment_id).with_for_update().first()
|
||||
if row and row.status != "ready":
|
||||
row.status = "failed"
|
||||
row.error_code = error_code
|
||||
row.updated_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
|
||||
def get_attachment(self, attachment_id, user_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(Attachment).join(ChatMember, ChatMember.chat_id == Attachment.chat_id).filter(
|
||||
Attachment.id == attachment_id, ChatMember.user_id == user_id).first()
|
||||
|
||||
|
||||
def encode_cursor(resource, created_at, item_id):
|
||||
raw = json.dumps([resource, created_at.isoformat(), item_id], separators=(",", ":")).encode()
|
||||
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(cursor, resource):
|
||||
try:
|
||||
data = json.loads(base64.urlsafe_b64decode(cursor + "=" * (-len(cursor) % 4)))
|
||||
if data[0] != resource:
|
||||
raise ValueError
|
||||
return datetime.fromisoformat(data[1]), data[2]
|
||||
except Exception:
|
||||
raise ValueError("INVALID_CURSOR")
|
||||
|
||||
|
||||
def cursor_for_chat(chat):
|
||||
return encode_cursor("chats", chat.created_at, chat.id)
|
||||
|
||||
|
||||
def cursor_for_message(message):
|
||||
return encode_cursor(message.chat_id, message.created_at, message.id)
|
||||
|
||||
|
||||
store = Store()
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
"""At-least-once Redis Streams image worker with idempotent S3/DB effects."""
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
import redis
|
||||
from PIL import Image, ImageOps, UnidentifiedImageError
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
from . import object_store
|
||||
from .jobs import QUEUE, CONSUMER_GROUP, MAX_ATTEMPTS, client, publish
|
||||
from .store import store
|
||||
|
||||
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
|
||||
log = logging.getLogger("attachment-worker")
|
||||
CONSUMER = f"worker-{socket.gethostname()}-{os.getpid()}"
|
||||
RECLAIM_IDLE_MS = int(os.getenv("JOB_RECLAIM_IDLE_MS", "30000"))
|
||||
|
||||
|
||||
def thumbnail(source):
|
||||
source.seek(0)
|
||||
with Image.open(source) as opened:
|
||||
image = ImageOps.exif_transpose(opened)
|
||||
image.load()
|
||||
if image.mode in ("RGBA", "LA") or (image.mode == "P" and "transparency" in image.info):
|
||||
rgba = image.convert("RGBA")
|
||||
background = Image.new("RGB", rgba.size, "white")
|
||||
background.paste(rgba, mask=rgba.getchannel("A"))
|
||||
image = background
|
||||
else:
|
||||
image = image.convert("RGB")
|
||||
scale = min(1.0, 256 / image.width, 256 / image.height)
|
||||
dimensions = (max(1, math.floor(image.width * scale)),
|
||||
max(1, math.floor(image.height * scale)))
|
||||
if dimensions != image.size:
|
||||
image = image.resize(dimensions, Image.Resampling.LANCZOS)
|
||||
output = io.BytesIO()
|
||||
image.save(output, format="JPEG", quality=85, optimize=True)
|
||||
return output.getvalue(), image.width, image.height
|
||||
|
||||
|
||||
def process(job):
|
||||
row = store.get_attachment_for_worker(job["attachment_id"])
|
||||
if row is None or row.job_id != job["job_id"] or row.status == "ready":
|
||||
return
|
||||
row = store.set_attachment_processing(row.id)
|
||||
if row is None or row.status == "ready":
|
||||
return
|
||||
obj = object_store.s3.get_object(Bucket=object_store.S3_BUCKET, Key=row.staging_key)
|
||||
raw = obj["Body"].read()
|
||||
obj["Body"].close()
|
||||
try:
|
||||
with Image.open(io.BytesIO(raw)) as image:
|
||||
if image.width * image.height > 40_000_000:
|
||||
raise ValueError("invalid_image")
|
||||
image.verify()
|
||||
except (UnidentifiedImageError, OSError, Image.DecompressionBombError) as exc:
|
||||
raise ValueError("invalid_image") from exc
|
||||
thumb, width, height = thumbnail(io.BytesIO(raw))
|
||||
s3 = object_store.s3
|
||||
s3.put_object(Bucket=object_store.S3_BUCKET, Key=row.object_key, Body=raw,
|
||||
ContentLength=len(raw), ContentType=row.content_type)
|
||||
key = f"variants/{row.id}/thumbnail"
|
||||
s3.put_object(Bucket=object_store.S3_BUCKET, Key=key, Body=thumb,
|
||||
ContentLength=len(thumb), ContentType="image/jpeg")
|
||||
variant = {"name": "thumbnail", "content_type": "image/jpeg",
|
||||
"size_bytes": len(thumb), "width": width, "height": height,
|
||||
"sha256": hashlib.sha256(thumb).hexdigest()}
|
||||
store.finish_attachment(row.id, [variant])
|
||||
|
||||
|
||||
def handle(broker, stream_id, body):
|
||||
job = None
|
||||
try:
|
||||
job = json.loads(body)
|
||||
if job.get("schema_version") != 1 or job.get("type") != "attachment.process.v1":
|
||||
raise ValueError("invalid_job")
|
||||
process(job)
|
||||
except (ValueError, KeyError) as exc:
|
||||
log.warning("permanent job failure code=%s", str(exc)[:64])
|
||||
if job:
|
||||
code = "invalid_image" if str(exc) == "invalid_image" else "invalid_job"
|
||||
store.fail_attachment(job.get("attachment_id"), code)
|
||||
except Exception:
|
||||
log.exception("attachment processing failure")
|
||||
if job:
|
||||
attempt = int(job.get("attempt", 1))
|
||||
if attempt >= MAX_ATTEMPTS:
|
||||
store.fail_attachment(job.get("attachment_id"), "retry_exhausted")
|
||||
else:
|
||||
time.sleep(min(2 ** (attempt - 1), 8))
|
||||
# Add the next bounded attempt before acking this delivery.
|
||||
publish(job["attachment_id"], job["job_id"], attempt + 1)
|
||||
broker.xack(QUEUE, CONSUMER_GROUP, stream_id)
|
||||
|
||||
|
||||
def main():
|
||||
while True:
|
||||
try:
|
||||
broker = client()
|
||||
try:
|
||||
broker.xgroup_create(QUEUE, CONSUMER_GROUP, id="0-0", mkstream=True)
|
||||
except redis.ResponseError as exc:
|
||||
if "BUSYGROUP" not in str(exc):
|
||||
raise
|
||||
log.info("worker consuming Redis Stream %s as %s", QUEUE, CONSUMER)
|
||||
cursor = "0-0"
|
||||
while True:
|
||||
reclaimed = broker.xautoclaim(QUEUE, CONSUMER_GROUP, CONSUMER,
|
||||
min_idle_time=RECLAIM_IDLE_MS,
|
||||
start_id=cursor, count=1)
|
||||
cursor, pending = reclaimed[0], reclaimed[1]
|
||||
if pending:
|
||||
for stream_id, fields in pending:
|
||||
handle(broker, stream_id, fields["body"])
|
||||
continue
|
||||
fresh = broker.xreadgroup(CONSUMER_GROUP, CONSUMER,
|
||||
{QUEUE: ">"}, count=1, block=5000)
|
||||
for _, entries in fresh:
|
||||
for stream_id, fields in entries:
|
||||
handle(broker, stream_id, fields["body"])
|
||||
except Exception:
|
||||
log.exception("broker unavailable; reconnecting")
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Verify that S3 outages fail readiness/uploads cleanly and recover after restart."""
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, arguments, run # noqa: E402
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("MinIO outage and recovery checks")
|
||||
# S3 health checks can spend the configured botocore connect timeout on an
|
||||
# unreachable service before the API returns the expected JSON 503.
|
||||
c = Client(args.base_url, args.ca_file, timeout=30)
|
||||
alice, _bob, _eve, chat = c.bootstrap()
|
||||
fixture = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
subprocess.run(["docker", "compose", "--env-file", ".env", "stop", "minio"],
|
||||
cwd=ROOT, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
try:
|
||||
# Nginx may hit its upstream timeout while both replicas are waiting
|
||||
# for the S3 connect timeout, so 504 is also an expected outage result.
|
||||
c.call("GET", "/health/ready", [503, 504])
|
||||
c.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=outage.png", [503, 504],
|
||||
actor=alice, raw=fixture, headers={"Content-Type": "image/png"})
|
||||
finally:
|
||||
subprocess.run(["docker", "compose", "--env-file", ".env", "start", "minio"],
|
||||
cwd=ROOT, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
deadline = time.monotonic() + 45
|
||||
while True:
|
||||
try:
|
||||
c.call("GET", "/health/ready", schema="Health")
|
||||
break
|
||||
except Exception:
|
||||
if time.monotonic() >= deadline:
|
||||
raise
|
||||
time.sleep(0.5)
|
||||
print("PASS: MinIO outage returned 503/504 for readiness/upload and recovered after service restart")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Demonstrate accepted upload recovery while the worker is stopped."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, check
|
||||
from image_probe import jpeg_size
|
||||
|
||||
|
||||
def compose(*args):
|
||||
subprocess.run(["docker", "compose", "--env-file", ".env", *args],
|
||||
cwd=ROOT, check=True)
|
||||
|
||||
|
||||
def main():
|
||||
client = Client("http://localhost:8080", timeout=10)
|
||||
alice, bob, _, chat = client.bootstrap()
|
||||
print("Created synthetic users and chat", flush=True)
|
||||
compose("stop", "worker")
|
||||
print("Stopped worker", flush=True)
|
||||
try:
|
||||
attachment, original = client.attachment(alice, chat)
|
||||
print("Upload returned queued", flush=True)
|
||||
sent = client.message(alice, chat, "", attachments=[attachment["id"]])["json"]
|
||||
check(sent["attachment_ids"] == [attachment["id"]], "message did not retain attachment reference")
|
||||
client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
409, actor=bob)
|
||||
print("Message linked; content correctly unavailable while queued", flush=True)
|
||||
|
||||
# The worker stays stopped while the API and durable broker are restarted.
|
||||
compose("restart", "api1", "api2", "redis-broker", "lb")
|
||||
print("Restarted API and broker with worker stopped", flush=True)
|
||||
compose("start", "worker")
|
||||
print("Started worker", flush=True)
|
||||
|
||||
ready = client.wait_ready(bob, attachment)
|
||||
original_response = client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content",
|
||||
actor=bob)
|
||||
check(original_response["bytes"] == original, "recovered original bytes changed")
|
||||
check(hashlib.sha256(original_response["bytes"]).hexdigest() == attachment["sha256"],
|
||||
"recovered original SHA-256 mismatch")
|
||||
thumbnail_meta = next(v for v in ready["variants"] if v["name"] == "thumbnail")
|
||||
thumbnail = client.call("GET", "/api/v1/attachments/" + attachment["id"] +
|
||||
"/content?variant=thumbnail", actor=bob)
|
||||
check(thumbnail["headers"].get_content_type() == "image/jpeg", "thumbnail is not JPEG")
|
||||
check(jpeg_size(thumbnail["bytes"]) == (256, 160), "thumbnail dimensions mismatch")
|
||||
check(hashlib.sha256(thumbnail["bytes"]).hexdigest() == thumbnail_meta["sha256"],
|
||||
"thumbnail checksum mismatch")
|
||||
print("PASS: worker recovery; upload stayed queued through API/broker restart and became ready")
|
||||
finally:
|
||||
compose("start", "worker")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user