forked from mtusi2026/mtusi2026lab4
Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab12d3eb78 | ||
|
|
7263949ec6 | ||
|
|
2554cb75c5 | ||
|
|
6f8e4f127d | ||
|
|
78670b9fa6 | ||
|
|
869cff59d9 | ||
|
|
3fd912fccb | ||
|
|
f5a7a74f67 | ||
|
|
3d5a5ca84a | ||
|
|
fba6b6334e | ||
|
|
8e120ec937 | ||
|
|
f449033e15 |
@@ -18,3 +18,18 @@ __pycache__
|
|||||||
node_modules
|
node_modules
|
||||||
evidence
|
evidence
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
**/__pycache__
|
||||||
|
**/*.py[cod]
|
||||||
|
.work
|
||||||
|
.pytest_cache
|
||||||
|
.ruff_cache
|
||||||
|
tests
|
||||||
|
student-tests
|
||||||
|
research
|
||||||
|
scripts
|
||||||
|
contracts
|
||||||
|
*.md
|
||||||
|
Makefile
|
||||||
|
compose.yaml
|
||||||
|
|
||||||
|
.work*
|
||||||
|
|||||||
+9
-3
@@ -13,8 +13,14 @@ API_REPLICAS=2
|
|||||||
IDEMPOTENCY_TTL_SECONDS=86400
|
IDEMPOTENCY_TTL_SECONDS=86400
|
||||||
S3_ENDPOINT=http://s3:9000
|
S3_ENDPOINT=http://s3:9000
|
||||||
S3_REGION=us-east-1
|
S3_REGION=us-east-1
|
||||||
S3_BUCKET=messenger-attachments
|
S3_BUCKET=messenger
|
||||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
S3_ACCESS_KEY_FILE=/run/secrets/s3_access_key
|
||||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
S3_SECRET_KEY_FILE=/run/secrets/s3_secret_key
|
||||||
MAX_UPLOAD_BYTES=10485760
|
MAX_UPLOAD_BYTES=10485760
|
||||||
MAX_IMAGE_PIXELS=40000000
|
MAX_IMAGE_PIXELS=40000000
|
||||||
|
|
||||||
|
PUBLIC_PORT=8084
|
||||||
|
API_CPUS=0.5
|
||||||
|
API_MEMORY=768m
|
||||||
|
DB_POOL_SIZE=10
|
||||||
|
RECONCILE_GRACE_SECONDS=300
|
||||||
|
|||||||
@@ -1,18 +1,22 @@
|
|||||||
## Лабораторная 4
|
## Лабораторная 4
|
||||||
|
|
||||||
Автор / группа: TODO
|
Автор / группа: Маслов / БВТ2301
|
||||||
Целевая оценка (3/4/5): TODO
|
Целевая оценка: 5
|
||||||
Трек 5, если заявлен: TODO
|
Трек: 2 — согласованность PostgreSQL/S3
|
||||||
|
|
||||||
Что реализовано и что перенесено: TODO
|
Перенесена собственная база ЛР3: Bearer, ACL, PostgreSQL/Valkey, две API, HAProxy и идемпотентность сообщений. Добавлены private MinIO, синхронный upload201ready, byte/pixel/MIME/filename limits, авторизованный streaming download, atomic Message links, durable pending intent и dry-run/apply reconciler с grace≥300s.
|
||||||
Команды запуска из чистого clone: TODO
|
|
||||||
Результаты проверок и ссылка на REPORT.md: TODO
|
|
||||||
Ограничения / известные ошибки: TODO
|
|
||||||
Источники и использование ИИ: TODO
|
|
||||||
|
|
||||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
Запуск: `uv sync --locked`, `make bootstrap`, `VCS_REF=$(git rev-parse HEAD) docker compose up -d --build --wait`. Полные команды и архитектура — [REPORT.md](../REPORT.md), [SCHEMA.md](../SCHEMA.md).
|
||||||
- [ ] Контракт и публичные тесты не ослаблены.
|
|
||||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
Результаты: public smoke PASS; 108 HTTP tests PASS; реальные отказы двух API,
|
||||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
rolling/persistence PASS; private S3/IAM/bootstrap, PG/S3 faults, dry-run/apply/repeat
|
||||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
и 10 concurrent uploads PASS. Контролируемое исследование: 26/126/1126objects,
|
||||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
30rows/8HEAD/0GETbytes, 3 измерения на размер +warmup; медианы0.194/0.204/0.409s.
|
||||||
|
Raw: evidence/http-first.json, storage-first.json, cluster-first.json,
|
||||||
|
cost-controlled-first.json. Отчёт содержит SHA, методы, RAM/CPU, ограничения и
|
||||||
|
ответы защиты. Clean clone PASS: 108 tests, cluster/storage matrix, runtime hashes/OCI revision обеих API, fsck; receipt evidence/clean-clone-first.json.
|
||||||
|
Контракт и публичные tests сохранены.
|
||||||
|
|
||||||
|
Ограничения: один host, LB/PG/S3 — SPOF; HEAD не заменяет periodic checksum audit; inventory reconciler O(objects+rows); удалённый CI не подтверждён.
|
||||||
|
|
||||||
|
Источники: актуальное задание/контракты, собственная ЛР3, официальные библиотеки и исходники MinIO/mc. При разработке кода, тестов и отчёта использовалась помощь ИИ; результаты измерений получаются реальными командами.
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
name: Application integration
|
||||||
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
jobs:
|
||||||
|
application:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 60
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: astral-sh/setup-uv@v6
|
||||||
|
with:
|
||||||
|
version: 0.13.7
|
||||||
|
- run: uv sync --locked
|
||||||
|
- run: make bootstrap check validate lint PYTHON=.venv/bin/python
|
||||||
|
- run: VCS_REF=$(git rev-parse HEAD) docker compose up -d --build --wait
|
||||||
|
- run: make test test-student PYTHON=.venv/bin/python BASE_URL=http://127.0.0.1:8084
|
||||||
|
env:
|
||||||
|
NO_PROXY: localhost,127.0.0.1
|
||||||
|
no_proxy: localhost,127.0.0.1
|
||||||
|
- run: make verify VERIFY_OUTPUT=evidence/storage-ci.json
|
||||||
|
- if: always()
|
||||||
|
run: docker compose down --volumes --remove-orphans
|
||||||
@@ -30,3 +30,8 @@ coverage/
|
|||||||
.vscode/
|
.vscode/
|
||||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||||
|
|
||||||
|
.work/
|
||||||
|
.work-*.txt
|
||||||
|
.pytest_cache/
|
||||||
|
.ruff_cache/
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
3.14.7
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv
|
||||||
|
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
|
||||||
|
COPY --from=uv /uv /usr/local/bin/uv
|
||||||
|
WORKDIR /app
|
||||||
|
ENV UV_PYTHON_DOWNLOADS=never UV_LINK_MODE=copy
|
||||||
|
COPY pyproject.toml uv.lock ./
|
||||||
|
RUN --mount=type=cache,target=/root/.cache/uv uv sync --locked --no-dev --no-install-project
|
||||||
|
|
||||||
|
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS runtime
|
||||||
|
ARG VCS_REF=unknown
|
||||||
|
LABEL org.opencontainers.image.revision=$VCS_REF
|
||||||
|
WORKDIR /app
|
||||||
|
ENV PATH="/app/.venv/bin:$PATH" PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 APP_HOST=0.0.0.0 APP_PORT=8080
|
||||||
|
RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app
|
||||||
|
COPY --from=builder /app/.venv /app/.venv
|
||||||
|
COPY src /app/src
|
||||||
|
COPY migrations /app/migrations
|
||||||
|
USER 10001:10001
|
||||||
|
EXPOSE 8080
|
||||||
|
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --start-interval=1s --retries=3 CMD ["python", "-c", "import os,urllib.request; urllib.request.urlopen('http://127.0.0.1:'+os.getenv('APP_PORT','8080')+'/health/ready',timeout=1).read()"]
|
||||||
|
STOPSIGNAL SIGTERM
|
||||||
|
CMD ["python", "-m", "src"]
|
||||||
@@ -28,3 +28,21 @@ test-persistence:
|
|||||||
test-failover:
|
test-failover:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
|
UV ?= uv
|
||||||
|
BASE_URL = http://127.0.0.1:8084
|
||||||
|
VERIFY_OUTPUT ?= evidence/storage-repeat.json
|
||||||
|
.PHONY: bootstrap up down lint test-student verify
|
||||||
|
bootstrap:
|
||||||
|
$(PYTHON) scripts/bootstrap.py
|
||||||
|
up: bootstrap
|
||||||
|
VCS_REF=$$(git rev-parse HEAD) docker compose up -d --build --wait
|
||||||
|
down:
|
||||||
|
docker compose down
|
||||||
|
lint:
|
||||||
|
$(UV) run --locked ruff format src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py $(wildcard scripts/verify_*.py)
|
||||||
|
$(UV) run --locked ruff check src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py $(wildcard scripts/verify_*.py)
|
||||||
|
test-student:
|
||||||
|
BASE_URL="$(BASE_URL)" $(UV) run --locked pytest student-tests
|
||||||
|
verify:
|
||||||
|
$(UV) run --locked python -m scripts.verify_storage --output "$(VERIFY_OUTPUT)"
|
||||||
|
|||||||
@@ -1,37 +1,268 @@
|
|||||||
# Отчёт — лабораторная 4
|
# Лабораторная 4 — Маслов, БВТ2301
|
||||||
|
|
||||||
- Автор, группа: TODO
|
Цель — **5, трек 2: согласованность PostgreSQL и S3**. Основа — собственная
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
реализация ЛР3. Сохранены Bearer-сессии в PostgreSQL, Argon2id, ACL,
|
||||||
- Commit/PR: TODO после публикации
|
keyset pagination, общий Idempotency-Key, HAProxy и две независимые API.
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
Добавлены приватный S3, вложения и повторяемый reconciler.
|
||||||
|
|
||||||
|
В clean-clone проверке обнаружена и исправлена гонка регистрации: все
|
||||||
|
конкуренты одного username без пользы выполняли Argon2, один мог получить
|
||||||
|
503 при лимите CPU. Теперь 64 bounded locks сериализуют локальные повторы,
|
||||||
|
перед KDF проверяется существование имени. Между API арбитр — прежний
|
||||||
|
PostgreSQL UNIQUE, хеширование остаётся вне SQL-транзакции. Argon2id и
|
||||||
|
лимит двух KDF/process не ослаблены. [Первый отказ](evidence/registration-race-first-failure.txt)
|
||||||
|
сохранён; после исправления [10 повторов по10 запросов](evidence/registration-race-fixed.json)
|
||||||
|
дали каждый раз один201/девять409, без503. Полный повтор из чистого clone прошёл: [receipt](evidence/clean-clone-first.json).
|
||||||
|
|
||||||
|
Проверки и исследование завершены. PR: [ЛР4 №144](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab4/pulls/144).
|
||||||
|
Публичный smoke и 108 собственных HTTP-проверок прошли на полном стенде
|
||||||
|
(362.40 s). Проверки отказов/исследование прошли; повтор в чистом clone также PASS (108 tests, 380.09 s).
|
||||||
|
Runtime проверен на двух API; receipt содержит исходный commit.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
Нужны Git, Docker с Compose v2 и uv 0.13.7. Python 3.14.7 загружается через uv.
|
||||||
|
Docker Desktop использует около 6 GiB памяти на Apple M1 (8 CPU, 8 GiB host).
|
||||||
|
Первичная компиляция MinIO/клиента требует сеть и время. Исходный ZIP
|
||||||
|
MinIO с точным module version скачивается восемью потоками HTTP Range,
|
||||||
|
с ограниченным timeout/числом повторов; затем стандартный `go mod download`
|
||||||
|
проверяет h1 через Go checksum database перед сборкой. Проверки TLS и
|
||||||
|
checksum не отключены. Go parallelism
|
||||||
|
задан `GOMAXPROCS=2` и `GOFLAGS=-p=2`; зависимости и компилятор
|
||||||
|
используют BuildKit cache.
|
||||||
|
|
||||||
## Архитектура и решения
|
```sh
|
||||||
|
uv sync --locked
|
||||||
|
make bootstrap
|
||||||
|
VCS_REF=$(git rev-parse HEAD) docker compose up -d --build --wait
|
||||||
|
make check validate lint PYTHON=.venv/bin/python
|
||||||
|
make test test-student PYTHON=.venv/bin/python BASE_URL=http://127.0.0.1:8084
|
||||||
|
uv run python -m scripts.verify_storage --research --output evidence/storage-repeat.json
|
||||||
|
uv run python -m scripts.verify_cluster --output evidence/cluster-repeat.json
|
||||||
|
```
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
`bootstrap` создаёт отсутствующие `.env`/secret files, существующие сохраняет.
|
||||||
|
Secret directory имеет mode 0700; files читаются непривилегированными UID
|
||||||
|
контейнеров через read-only mounts. Их значения не должны попадать в Git.
|
||||||
|
Сначала стартует PostgreSQL, одноразовый migration применяет SQL по порядку
|
||||||
|
с checksum и advisory lock. Bootstrap S3 создаёт private bucket/app-user/policy
|
||||||
|
и переносит повторный запуск. После этого две API, reconciler и LB.
|
||||||
|
|
||||||
## Доказательства критериев
|
URL стенда: `http://127.0.0.1:8084`. API/S3/PG/cache не имеют host ports.
|
||||||
|
Безопасная остановка: `docker compose down` (volumes остаются).
|
||||||
|
Намеренное удаление **данных только текущего учебного проекта**:
|
||||||
|
`docker compose down --volumes`; это не обычный restart.
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
```sh
|
||||||
|
# Dry-run: без изменений
|
||||||
|
docker compose run --rm --no-deps reconciler python -m src.reconcile
|
||||||
|
# Оператор явно разрешает изменения
|
||||||
|
docker compose run --rm --no-deps reconciler python -m src.reconcile --apply
|
||||||
|
# Реальные рестарты всех state services, с прежними volumes
|
||||||
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8084
|
||||||
|
make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://127.0.0.1:8084
|
||||||
|
```
|
||||||
|
|
||||||
|
Не запускать fault scripts параллельно друг с другом или HTTP pytest:
|
||||||
|
они намеренно останавливают реальные сервисы и временно устанавливают
|
||||||
|
PostgreSQL triggers. Все их пользователи/картинки синтетические.
|
||||||
|
Evidence filename должен быть новым, предыдущие результаты сохраняются.
|
||||||
|
|
||||||
|
## Версии и ограничения ресурсов
|
||||||
|
|
||||||
|
Полные image digests закреплены в Dockerfile/compose.yaml/infra/s3.Dockerfile:
|
||||||
|
Python 3.14.7 slim-trixie, uv 0.13.7, PostgreSQL 18.3, Valkey 8.1.3,
|
||||||
|
HAProxy 3.2.1 alpine. MinIO собирается из commit
|
||||||
|
`9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a`, release
|
||||||
|
`2025-10-15T17-29-55Z`; mc — из
|
||||||
|
`7394ce0dd2a80935aded936b09fa12cbb3cb8096` (2025-08-13).
|
||||||
|
Go 1.24.8 bookworm закреплён digest, исходные LICENSE включены в образы.
|
||||||
|
Python dependency lock включает minio 7.2.20 и Pillow 13.0.0.
|
||||||
|
|
||||||
|
| Процесс | CPU | RAM |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| Каждая из двух API | 0.5 | 768 MiB |
|
||||||
|
| PostgreSQL | 1 | 512 MiB |
|
||||||
|
| MinIO | 1 | 1024 MiB |
|
||||||
|
| Valkey | 0.25 | 128 MiB |
|
||||||
|
| HAProxy | 0.5 | 128 MiB |
|
||||||
|
| Reconciler | 0.25 | 256 MiB |
|
||||||
|
|
||||||
|
API: read-only root, UID 10001, no capabilities, no-new-privileges,
|
||||||
|
128 PIDs, bounded `/tmp` tmpfs 128 MiB. At most two uploads/process,
|
||||||
|
одна декодируемая картинка/process. Spooled file после 1 MiB переносится в
|
||||||
|
tmpfs; максимум bytes 10 MiB, pixels 40 млн до decode. Проверяются формат
|
||||||
|
содержимого, фактический decode, filename; SHA-256 считается на входе.
|
||||||
|
S3 timeout connect 1/read 3 s, без transparent retry; DB transaction budget
|
||||||
|
4 s, statement 2.5 s; async ingress deadline 20 s. Отмена Python thread не
|
||||||
|
прерывает нативный decode, поэтому существенны также pixel/RAM limits.
|
||||||
|
Download передаёт chunks 64 KiB с закрытием S3 connection.
|
||||||
|
|
||||||
|
`.env.example`: PUBLIC_PORT/API_REPLICAS/API_CPUS/API_MEMORY/DB_POOL_SIZE/
|
||||||
|
SESSION_TTL_SECONDS/RECONCILE_GRACE_SECONDS используются Compose. Secret file
|
||||||
|
names указаны в compose.yaml. APP_HOST/APP_PORT задаются для API в Compose;
|
||||||
|
endpoint `s3:9000`, region us-east-1, bucket messenger, byte/pixel limits и
|
||||||
|
24 h idempotency заданы сервером. Это фиксированные параметры реализации,
|
||||||
|
логические подсказки skeleton не делают их автоматически настраиваемыми.
|
||||||
|
Сессии — PostgreSQL; Valkey хранит только disposable user cache.
|
||||||
|
|
||||||
|
## Архитектура и критерии
|
||||||
|
|
||||||
|
Подробные переходы и схема: [SCHEMA.md](SCHEMA.md).
|
||||||
|
`src/attachments.py` коммитит pending intent **до PUT**, затем ready;
|
||||||
|
201 означает подтверждённые bytes и metadata. Общей PG/S3 транзакции нет.
|
||||||
|
Readiness проверяет PG и S3. При потере S3 HAProxy может исключить все API и
|
||||||
|
вернуть собственный 503; прямой внутренний API возвращает JSON unavailable.
|
||||||
|
|
||||||
|
| Проверка | Команда | Наблюдение | Доказательство |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| SQL migrations | `docker compose run --rm --no-deps migrate`, повтор | 3 migrations, checksum match, repeat idempotent | [migrations-first.json](evidence/migrations-first.json) |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Публичный API | `make test` | PASS: upload201, Message, original SHA-256 и ACL | [http-first.json](evidence/http-first.json) |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Прежние возможности и негативные файлы | `uv run pytest student-tests -q` | 108 passed, 362.40 s; в том числе chunked >10 MiB, corrupt/MIME/pixels/filename/foreign chat | [http-first.json](evidence/http-first.json) |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Закреплённый S3 | `go mod download`, `minio --version`, inspect | module h1 verified; binary 2025-10-15/go1.24.8/arm64; 2 API, реальные CPU/RAM/ports | [minio-source-verification.json](evidence/minio-source-verification.json) |
|
||||||
|
| Failover файла | `uv run python -m scripts.verify_storage --research --output evidence/storage-repeat.json` | API загрузившая файл реально остановлена, другая вернула прежний SHA-256 | [storage-first.json](evidence/storage-first.json) |
|
||||||
|
| Ограниченные credentials/private bucket/bootstrap | Та же storage-проверка | anonymous403, forbidden operations AccessDenied, denied HEAD→503, console недоступна из сети, повтор bootstrap сохранил файл | [storage-first.json](evidence/storage-first.json) |
|
||||||
|
| Обрыв и ошибки PG/S3 | Та же storage-проверка | частичный upload2MiB без intent/bytes; INSERT failure без PUT; pending после PUT/ошибки UPDATE восстановлен; S3down→503 | [storage-first.json](evidence/storage-first.json) |
|
||||||
|
| Пределы RAM | Та же storage-проверка | 10/10 uploads и 10/10 originals; память/CPU/latencies записаны | [storage-first.json](evidence/storage-first.json) |
|
||||||
|
| Orphan/missing, dry-run/apply/repeat | Та же storage-проверка | 5 ожидаемых действий, все применены; повтор0 действий; fresh/ready/used защищены, ID/links сохранены | [storage-first.json](evidence/storage-first.json) |
|
||||||
|
| Реальные отказы/идемпотентность/rolling | `uv run python -m scripts.verify_cluster --output evidence/cluster-repeat.json` | две API остановлены по очереди; общий replay и rollback; rolling; подтверждённые сообщения без дублей | [cluster-first.json](evidence/cluster-first.json) |
|
||||||
|
| Полный restart с volumes | Та же cluster-проверка, public resilience | API/PG/cache/S3/LB изменили StartedAt, прежние token/metadata/bytes сохранены | [cluster-first.json](evidence/cluster-first.json) |
|
||||||
|
| Стоимость обхода | `uv run python -m research.cost --output evidence/cost-repeat.json` | 9 измерений +3 warmups, фиксированные 30rows/8HEAD/0GETbytes, 0/100/1100extra | [cost-controlled-first.json](evidence/cost-controlled-first.json) |
|
||||||
|
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
Source первых HTTP/storage/cluster = `f5a7a74`, OCI revision API совпадает с ним.
|
||||||
|
Контролируемый cost harness — `3fd912f`, исполняемый `src/reconcile.py` тот же.
|
||||||
|
В чистом clone дополнительно проверяются module hashes/OCI revision обеих API
|
||||||
|
и полный набор сценариев; итоговый receipt — evidence/clean-clone-first.json.
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Методика исследования
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
Гипотеза: сохранённое до PUT намерение и grace позволяют повторной сверкой
|
||||||
|
исправлять окна отказов PG/S3 без удаления свежих/используемых bytes.
|
||||||
|
Полный обход имеет линейную стоимость по числу объектов и metadata.
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
`scripts/verify_storage.py --research`: реальные отказы INSERT/ready UPDATE
|
||||||
|
через временные PG triggers, недоступность S3, abort HTTP, отсутствие
|
||||||
|
используемого оригинала, old orphan и fresh orphan. Объект orphan реально
|
||||||
|
стареет ≥300 s по S3 LastModified. Dry-run, apply, повторный apply.
|
||||||
|
Первичный cost в storage-first.json оставлен как разведочный: между
|
||||||
|
повторами HEAD count менялся 6→10→16, когда concurrent upload rows достигли
|
||||||
|
300s. Его время нельзя приписать одному размеру inventory при прочих равных.
|
||||||
|
|
||||||
|
Для окончательного исследования используется `research/cost.py`:
|
||||||
|
8 PNG-fixtures с metadata, намеренно состаренными в PG на 2 дня; grace 3600 s,
|
||||||
|
0/100/1100 добавочных свежих объектов по 1 byte. Остальные реальные строки
|
||||||
|
стенда учитываются; invariant фиксирует 30rows/8HEAD/0GETbytes во всех 9 cells.
|
||||||
|
Один warmup на dataset, затем 3 измеряемых dry-run, по возрастанию размера.
|
||||||
|
Каждый probe — новый контейнер **reconciler .25 CPU / 256 MiB**, не API.
|
||||||
|
Duration измеряется внутри reconcile (не включает запуск контейнера/seed),
|
||||||
|
CPU — process_time, RSS peak — Linux ru_maxrss, cgroup peak включает startup.
|
||||||
|
Временные fixtures и bytes удаляются только по собственным chat/prefix.
|
||||||
|
Это стоимость обхода, не максимальная пропускная способность S3.
|
||||||
|
|
||||||
|
10 потоков одновременно загружают одинаковое синтетическое PNG 1700×1700,
|
||||||
|
каждый сравнивает оригинал. Измеряются upload p50/p95/p99, ошибки/RPS,
|
||||||
|
cgroup memory.current samples и lifetime memory.peak обеих API, cpu.stat
|
||||||
|
до/после. Duration/RPS включают downloads и sampler; latency — upload only.
|
||||||
|
memory.peak может включать прежние запросы, поэтому отдельно показан
|
||||||
|
максимум memory.current в окне. Небольшая серия не является production SLA.
|
||||||
|
|
||||||
|
## Наблюдения и границы эксперимента
|
||||||
|
|
||||||
|
Storage: orphan действительно старел 331.301s; часы S3 не менялись.
|
||||||
|
Dry-run нашёл по одному delete_orphan/recover_pending/fail_pending_missing/
|
||||||
|
delete_failed_object/mark_missing. Apply выполнил их, повторный apply не
|
||||||
|
изменил ничего. Fresh orphan, fresh failed и ready без Message сохранились;
|
||||||
|
у missing used сохранены metadata ID и Message link, внешний GET→409.
|
||||||
|
Данные пропавшего original не «восстановлены» по checksum.
|
||||||
|
|
||||||
|
10 одновременных uploads: PNG 6,017,095 bytes, 2,890,000 pixels; 10/10 успехов,
|
||||||
|
ошибок 0, оригиналы совпали. Серия 5.149 s, 1.942successful uploads/s с учётом
|
||||||
|
скачивания и sampler; upload p50=2162.965ms, p95=p99=4465.512ms.
|
||||||
|
Это одна короткая серия, p95/p99 на 10 запросах совпадают с максимумом.
|
||||||
|
Sampled cgroup memory.current max по API 123.01/155.93 MiB; memory.peak
|
||||||
|
145.66/205.13 MiB. Последнее включает предыдущую работу, поэтому это верхняя
|
||||||
|
граница для данной серии. В clean-clone повторе API специально рестартуют
|
||||||
|
перед экспериментом, чтобы peak относился к новым cgroups.
|
||||||
|
|
||||||
|
| Дополнительные объекты | Фактически scanned | Медиана времени, s | Диапазон 3 повторов, s |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| 0 | 26 | 0.193719 | 0.189185–0.210146 |
|
||||||
|
| 100 | 126 | 0.204344 | 0.196670–0.210975 |
|
||||||
|
| 1100 | 1126 | 0.409094 | 0.391053–0.610401 |
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
При этих условиях увеличение inventory повысило стоимость. Код хранит
|
||||||
|
inventory в dict и обходит его целиком: O(objects+rows) времени/памяти.
|
||||||
|
Три размера и три повтора не доказывают точную линейную зависимость:
|
||||||
|
есть fixed HEAD cost, pagination LIST после 1000 keys, quota scheduling и
|
||||||
|
нагрузка общего ноутбука. CPU процесса 0.0368–0.1059 s; RSS peak 58.82–59.82 MiB,
|
||||||
|
cgroup peak 41.60–43.10 MiB. RSS и cgroup учитывают общие страницы по-разному;
|
||||||
|
это разные показатели. Небольшой dataset не доказывает пригодность полного
|
||||||
|
inventory для миллионов объектов. Следующий шаг — пагинация/partitioning
|
||||||
|
и ограниченные пачки metadata; это предложение, не измеренная оптимизация.
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|

|
||||||
|
|
||||||
|
Process failover: первая API: 194 подтверждённых записи / 8 временных ошибок,
|
||||||
|
восстановление 4.086 s; вторая: 217 / 7 / 2.720 s. Rolling: 609 записей / 34 ошибки,
|
||||||
|
подтверждённые записи сохранены. Public failover: 123 записи / 3 ошибки,
|
||||||
|
восстановление 2.635 s. Это не zero-error rolling и не HA единственного host.
|
||||||
|
|
||||||
|
## Вопросы защиты
|
||||||
|
|
||||||
|
**Почему volume API не равно S3?** Volume принадлежит контейнеру/host и сам
|
||||||
|
по себе не задаёт объектный HTTP API, bucket policy, общую адресацию и
|
||||||
|
независимый lifecycle. В этой реализации у API вообще нет persistent volume;
|
||||||
|
другая реплика читает общий S3 по metadata PostgreSQL.
|
||||||
|
|
||||||
|
**Когда можно вернуть 201?** После фактической проверки изображения,
|
||||||
|
успешного PUT и COMMIT ready. Если COMMIT не подтверждён, нельзя обещать
|
||||||
|
201. Клиент может потерять ответ после успешной записи; upload не имеет
|
||||||
|
Idempotency-Key, поэтому слепой повтор может создать другое вложение.
|
||||||
|
|
||||||
|
**PUT прошёл, COMMIT не прошёл?** Уже есть durable pending intent. После
|
||||||
|
grace reconciler проверяет размер/checksum и переводит в ready. При
|
||||||
|
отсутствующих/других bytes ставит internal failed. Очищать файл вслепую
|
||||||
|
при неизвестном результате COMMIT опасно. Восстановление уже пропавших
|
||||||
|
bytes только из checksum невозможно.
|
||||||
|
|
||||||
|
**Почему случайный key/private bucket не заменяют ACL?** Пользователи
|
||||||
|
могут узнать ID из сообщения; key не доказывает membership. Каждый GET
|
||||||
|
сначала проверяет Bearer и JOIN members, чужой/отсутствующий объект — 404.
|
||||||
|
Private bucket защищает обход API; application IAM ограничивает последствия
|
||||||
|
ошибок backend, но не принимает пользовательские решения о доступе.
|
||||||
|
|
||||||
|
## Источники, помощь и границы
|
||||||
|
|
||||||
|
Использованы задание и неизменённые контракты текущего репозитория,
|
||||||
|
собственный код ЛР3, FastAPI/Pydantic, psycopg, minio SDK, Pillow,
|
||||||
|
[исходники MinIO](https://github.com/minio/minio),
|
||||||
|
[исходники mc](https://github.com/minio/mc),
|
||||||
|
[S3 uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html).
|
||||||
|
При проектировании, написании кода, тестов и отчёта использована помощь ИИ;
|
||||||
|
фактические результаты должны подтверждаться сохранёнными командами.
|
||||||
|
Код и данные других студентов не использовались.
|
||||||
|
|
||||||
|
Один host/LB/PG/S3 — SPOF; нет защиты от потери единственного диска,
|
||||||
|
backup/restore S3, TLS и monitoring. Ready HEAD не проверяет весь checksum
|
||||||
|
на каждом проходе. Reconciler хранит полный inventory в RAM, metadata failed
|
||||||
|
сохраняет для диагностики. Повреждение S3 нельзя исправить без резервной копии.
|
||||||
|
CI workflow приложен; удалённый запуск не подтверждён.
|
||||||
|
|
||||||
|
## Итоговая проверка из чистого clone
|
||||||
|
|
||||||
|
[evidence/clean-clone-first.json](evidence/clean-clone-first.json): PASS,
|
||||||
|
source `6f8e4f127dee8857a3af908a9fb1d575f8907d90`. Новые secrets, env,
|
||||||
|
volumes, API image и фиксированный случайный host port; check/validate/lint,
|
||||||
|
public smoke, 108 HTTP tests (380.09 s), полная cluster/storage матрица,
|
||||||
|
hashes исходников и OCI revision обеих API совпали; git fsck PASS.
|
||||||
|
Временный стенд и только его volumes удалены в finally.
|
||||||
|
|
||||||
|
Реальное старение S3 334.035 s; пять ожидаемых действий dry-run/apply,
|
||||||
|
повтор 0; fresh/ready/used данные защищены. C10: 10/10, ошибок0,
|
||||||
|
6,017,095 bytes/image, 5.429 s включая verified downloads/sampler;
|
||||||
|
1.842 successful uploads/s, upload p50 2767.956 ms, p95=p99 5002.469 ms.
|
||||||
|
После рестарта API новые cgroups: sampled memory.current max 138.30 MiB,
|
||||||
|
memory.peak max 143.94 MiB (включает startup до серии).
|
||||||
|
Контролируемое исследование 9cells выполнено отдельно и не выдаётся
|
||||||
|
за повторённое в clean clone. Старый failed receipt сохранён для истории.
|
||||||
|
Последующие коммиты меняют только отчёт/evidence, проверенный runtime сохранён.
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# Бинарные данные и два независимых хранилища
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
flowchart LR
|
||||||
|
C[Клиент] --> L[HAProxy :8084]
|
||||||
|
L --> A[API × 2]
|
||||||
|
A --> P[(PostgreSQL)]
|
||||||
|
A --> S[(Приватный MinIO)]
|
||||||
|
A --> V[(Valkey: кэш пользователей)]
|
||||||
|
R[Reconciler: 60 секунд] --> P
|
||||||
|
R --> S
|
||||||
|
```
|
||||||
|
|
||||||
|
`users/chats/members/messages/sessions/idempotency` перенесены из собственной
|
||||||
|
ЛР3. Новые таблицы: `attachments` и `message_attachments`. Состав участников
|
||||||
|
чата неизменяемый. PostgreSQL хранит metadata и ACL, S3 — оригинальные bytes.
|
||||||
|
Временный файл в API ограничен размером, удаляется при закрытии запроса и
|
||||||
|
не является источником истины. API-процессы не обмениваются локальными файлами.
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant C as Клиент
|
||||||
|
participant A as API
|
||||||
|
participant P as PostgreSQL
|
||||||
|
participant S as S3
|
||||||
|
C->>A: Bearer, filename, PNG/JPEG bytes
|
||||||
|
A->>A: ACL, лимиты bytes/pixels, decode, SHA-256
|
||||||
|
A->>P: COMMIT pending intent, UUID/key, срок 120 с
|
||||||
|
A->>S: PUT оригинала
|
||||||
|
A->>P: COMMIT ready при действующем намерении
|
||||||
|
A-->>C: 201 ready
|
||||||
|
```
|
||||||
|
|
||||||
|
Общей транзакции PostgreSQL+S3 нет. Если INSERT намерения не прошёл, PUT не
|
||||||
|
выполняется. Если PUT не прошёл, остаётся pending без объекта. Если PUT
|
||||||
|
прошёл, а ready UPDATE/COMMIT нет, остаётся pending с bytes. Неопределённый
|
||||||
|
результат COMMIT нельзя исправлять безусловным DELETE файла: COMMIT мог
|
||||||
|
успешно завершиться, хотя клиент потерял ответ.
|
||||||
|
|
||||||
|
Reconciler по умолчанию выполняет **dry-run**. `--apply` разрешает изменения.
|
||||||
|
Автоматический процесс запускает apply каждые 60 секунд. Grace не менее
|
||||||
|
300 секунд; возраст берётся из PostgreSQL clock и S3 LastModified.
|
||||||
|
|
||||||
|
| Состояние | Действие после grace |
|
||||||
|
| --- | --- |
|
||||||
|
| Объект без metadata | Повторная проверка metadata и HEAD; удалить старый orphan |
|
||||||
|
| pending + корректные размер/SHA-256 bytes | ready |
|
||||||
|
| pending без bytes / с другим размером/checksum | failed с безопасной причиной |
|
||||||
|
| ready с bytes, включая файл без Message | Сохранить |
|
||||||
|
| ready без bytes | failed, сохранить ID, metadata и Message links |
|
||||||
|
| failed, связанный с Message | Сохранить |
|
||||||
|
| failed без Message со старым объектом | Повторно проверить свежесть HEAD; удалить bytes |
|
||||||
|
| Свежий объект | Сохранить |
|
||||||
|
| AccessDenied / 5xx / network error | Завершить проход с ошибкой; не считать объект отсутствующим |
|
||||||
|
|
||||||
|
Во внешнем контракте ЛР4 Attachment имеет только статус `ready`. Внутренние
|
||||||
|
pending/failed возвращают `409 conflict`, после проверки ACL. ЛР5 меняет
|
||||||
|
этот контракт на четыре публичных состояния и требует другого обработчика.
|
||||||
|
|
||||||
|
Связь Message↔Attachment вставляется в одной PostgreSQL-транзакции с Message
|
||||||
|
и Idempotency-Key. Композитные FK проверяют одинаковый chat_id; FOR SHARE
|
||||||
|
на вложении защищает проверку ready от конкурентного изменения состояния.
|
||||||
|
Несколько сообщений могут использовать одно вложение своего чата.
|
||||||
|
|
||||||
|
S3 app-user разрешены ListBucket/GetBucketLocation и object CRUD только
|
||||||
|
`messenger/objects/*`. Root credentials доступны лишь серверу и bootstrap.
|
||||||
|
Bucket private, S3-порт не опубликован на host, console слушает container
|
||||||
|
loopback. Авторизованный download проксируется через API; случайный key
|
||||||
|
не заменяет проверку membership.
|
||||||
|
|
||||||
|
Ограничения: полный LIST и выборка metadata имеют стоимость O(objects+rows)
|
||||||
|
и линейную память reconciler. Ready-проверка использует HEAD, не полный
|
||||||
|
периодический аудит битовой целостности. Metadata failed сохраняются для
|
||||||
|
диагностики. Reconciler обнаруживает пропажу bytes, но не восстанавливает
|
||||||
|
данные из checksum. Единственный Docker-host, LB, PostgreSQL и MinIO —
|
||||||
|
общие точки отказа; репликация API этого не устраняет.
|
||||||
+152
@@ -0,0 +1,152 @@
|
|||||||
|
name: mtusi-lab4-maslov
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:18.3@sha256:7e32e9833a6fb1c92c32552794cb6ed569d51b445a54907d35fc112ef39684db
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: messenger
|
||||||
|
POSTGRES_DB: messenger
|
||||||
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
|
PGDATA: /var/lib/postgresql/data/pgdata
|
||||||
|
secrets: [postgres_password]
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql
|
||||||
|
healthcheck:
|
||||||
|
test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 20
|
||||||
|
cpus: 1
|
||||||
|
mem_limit: 512m
|
||||||
|
memswap_limit: 512m
|
||||||
|
cache:
|
||||||
|
image: valkey/valkey:8.1.3@sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571
|
||||||
|
command: [valkey-server, /run/secrets/valkey.conf]
|
||||||
|
secrets:
|
||||||
|
- cache_password
|
||||||
|
- source: valkey_config
|
||||||
|
target: valkey.conf
|
||||||
|
healthcheck:
|
||||||
|
test: [CMD-SHELL, 'REDISCLI_AUTH="$$(cat /run/secrets/cache_password)" valkey-cli ping']
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 20
|
||||||
|
cpus: 0.25
|
||||||
|
mem_limit: 128m
|
||||||
|
memswap_limit: 128m
|
||||||
|
migrate:
|
||||||
|
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||||
|
pull_policy: never
|
||||||
|
command: [python, -m, src.migrate]
|
||||||
|
environment:
|
||||||
|
DATABASE_URL_FILE: /run/secrets/database_url
|
||||||
|
secrets: [database_url]
|
||||||
|
depends_on:
|
||||||
|
postgres: {condition: service_healthy}
|
||||||
|
cpus: 0.5
|
||||||
|
mem_limit: 256m
|
||||||
|
read_only: true
|
||||||
|
api:
|
||||||
|
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
args:
|
||||||
|
VCS_REF: ${VCS_REF:-unknown}
|
||||||
|
scale: ${API_REPLICAS:-2}
|
||||||
|
environment:
|
||||||
|
APP_HOST: 0.0.0.0
|
||||||
|
APP_PORT: 8080
|
||||||
|
DATABASE_URL_FILE: /run/secrets/database_url
|
||||||
|
CACHE_URL_FILE: /run/secrets/cache_url
|
||||||
|
S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key
|
||||||
|
S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
DB_POOL_SIZE: ${DB_POOL_SIZE:-10}
|
||||||
|
secrets: [database_url, cache_url, s3_access_key, s3_secret_key]
|
||||||
|
depends_on:
|
||||||
|
migrate: {condition: service_completed_successfully}
|
||||||
|
cache: {condition: service_healthy}
|
||||||
|
s3-bootstrap: {condition: service_completed_successfully}
|
||||||
|
cpus: ${API_CPUS:-0.5}
|
||||||
|
mem_limit: ${API_MEMORY:-384m}
|
||||||
|
memswap_limit: ${API_MEMORY:-384m}
|
||||||
|
pids_limit: 128
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:rw,noexec,nosuid,size=128m"]
|
||||||
|
cap_drop: [ALL]
|
||||||
|
security_opt: [no-new-privileges:true]
|
||||||
|
stop_grace_period: 10s
|
||||||
|
s3:
|
||||||
|
image: ${S3_IMAGE:-mtusi-storage:minio-20251015}
|
||||||
|
build: {context: ., dockerfile: infra/s3.Dockerfile, target: server}
|
||||||
|
secrets: [s3_root_user, s3_root_password]
|
||||||
|
volumes: [s3_data:/data]
|
||||||
|
cpus: 1
|
||||||
|
mem_limit: 1024m
|
||||||
|
memswap_limit: 1024m
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:rw,nosuid,size=32m"]
|
||||||
|
environment:
|
||||||
|
HOME: /tmp
|
||||||
|
security_opt: [no-new-privileges:true]
|
||||||
|
s3-bootstrap:
|
||||||
|
image: ${MC_IMAGE:-mtusi-storage:mc-20250813}
|
||||||
|
build: {context: ., dockerfile: infra/s3.Dockerfile, target: bootstrap}
|
||||||
|
secrets: [s3_root_user, s3_root_password, s3_access_key, s3_secret_key]
|
||||||
|
depends_on:
|
||||||
|
s3: {condition: service_healthy}
|
||||||
|
cpus: 0.25
|
||||||
|
mem_limit: 128m
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:rw,nosuid,size=16m"]
|
||||||
|
reconciler:
|
||||||
|
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||||
|
pull_policy: never
|
||||||
|
command: [python, -m, src.reconcile, --apply, --loop]
|
||||||
|
environment:
|
||||||
|
DATABASE_URL_FILE: /run/secrets/database_url
|
||||||
|
S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key
|
||||||
|
S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key
|
||||||
|
RECONCILE_GRACE_SECONDS: ${RECONCILE_GRACE_SECONDS:-300}
|
||||||
|
secrets: [database_url, s3_access_key, s3_secret_key]
|
||||||
|
depends_on:
|
||||||
|
migrate: {condition: service_completed_successfully}
|
||||||
|
s3-bootstrap: {condition: service_completed_successfully}
|
||||||
|
cpus: 0.25
|
||||||
|
mem_limit: 256m
|
||||||
|
memswap_limit: 256m
|
||||||
|
read_only: true
|
||||||
|
healthcheck: {disable: true}
|
||||||
|
lb:
|
||||||
|
image: haproxy:3.2.1-alpine@sha256:ac79fe145f2bb6626ff26b584a2d0a34e791906c01015f2ae037aa3137b683d9
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:${PUBLIC_PORT:-8084}:8080"
|
||||||
|
volumes:
|
||||||
|
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||||
|
depends_on:
|
||||||
|
api: {condition: service_healthy}
|
||||||
|
cpus: 0.5
|
||||||
|
mem_limit: 128m
|
||||||
|
memswap_limit: 128m
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:rw,nosuid,size=4m"]
|
||||||
|
security_opt: [no-new-privileges:true]
|
||||||
|
stop_grace_period: 10s
|
||||||
|
secrets:
|
||||||
|
s3_root_user: {file: ./secrets/s3_root_user}
|
||||||
|
s3_root_password: {file: ./secrets/s3_root_password}
|
||||||
|
s3_access_key: {file: ./secrets/s3_access_key}
|
||||||
|
s3_secret_key: {file: ./secrets/s3_secret_key}
|
||||||
|
postgres_password:
|
||||||
|
file: ./secrets/postgres_password
|
||||||
|
database_url:
|
||||||
|
file: ./secrets/database_url
|
||||||
|
cache_password:
|
||||||
|
file: ./secrets/cache_password
|
||||||
|
cache_url:
|
||||||
|
file: ./secrets/cache_url
|
||||||
|
valkey_config:
|
||||||
|
file: ./secrets/valkey.conf
|
||||||
|
name: valkey.conf
|
||||||
|
volumes:
|
||||||
|
s3_data:
|
||||||
|
postgres_data:
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-15T18:42:26.500607+00:00",
|
||||||
|
"source_commit": "869cff59d9878ce98640a71382c79e5f94c2bf76",
|
||||||
|
"fresh_secrets_environment_and_volumes": true,
|
||||||
|
"command": "python scripts/verify_clean.py",
|
||||||
|
"fixed_host_port_for_restart": 49840,
|
||||||
|
"static_checks": ".venv/bin/python scripts/check_contract.py\nPASS: offline skeleton structure, local refs and Python syntax; application NOT tested.\n.venv/bin/python scripts/validate_openapi.py\nPASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.\n<home>/.local/bin/uv run --locked ruff format src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_clean.py scripts/verify_cluster.py scripts/verify_storage.py\n32 files left unchanged\n<home>/.local/bin/uv run --locked ruff check src student-tests scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_clean.py scripts/verify_cluster.py scripts/verify_storage.py\nAll checks passed!",
|
||||||
|
"smoke": "python3 tests/smoke.py --base-url \"http://127.0.0.1:49840\" \nPASS: lab4 public API scenario; infrastructure and grading evidence remain separate.",
|
||||||
|
"failed_action": ".venv/bin/pytest student-tests -q",
|
||||||
|
"result": "INCOMPLETE"
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,271 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-15T18:28:56.018046+00:00",
|
||||||
|
"source_commit": "3fd912fccb761d7010783363fa49337542ffa4e0",
|
||||||
|
"complete": true,
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 26,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 26,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.189185,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.039170499000000136,
|
||||||
|
"rss_peak_bytes": 61718528,
|
||||||
|
"cgroup_memory_peak_bytes": 43945984
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 26,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 26,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.210146,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.05308141700000002,
|
||||||
|
"rss_peak_bytes": 61693952,
|
||||||
|
"cgroup_memory_peak_bytes": 43950080
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 26,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 26,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.193719,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.036812661999999996,
|
||||||
|
"rss_peak_bytes": 61718528,
|
||||||
|
"cgroup_memory_peak_bytes": 44134400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.19667,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.03935812599999999,
|
||||||
|
"rss_peak_bytes": 62033920,
|
||||||
|
"cgroup_memory_peak_bytes": 43622400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.210975,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.05283020999999999,
|
||||||
|
"rss_peak_bytes": 61865984,
|
||||||
|
"cgroup_memory_peak_bytes": 44126208
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.204344,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.05048583700000009,
|
||||||
|
"rss_peak_bytes": 61677568,
|
||||||
|
"cgroup_memory_peak_bytes": 43831296
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 1126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.409094,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.0955386250000001,
|
||||||
|
"rss_peak_bytes": 62730240,
|
||||||
|
"cgroup_memory_peak_bytes": 44974080
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 1126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.391053,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.09063437099999994,
|
||||||
|
"rss_peak_bytes": 62652416,
|
||||||
|
"cgroup_memory_peak_bytes": 45195264
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 1126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.610401,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.10586249599999986,
|
||||||
|
"rss_peak_bytes": 62672896,
|
||||||
|
"cgroup_memory_peak_bytes": 44793856
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"warmups": [
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 26,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 26,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.20732,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.05440116500000003,
|
||||||
|
"rss_peak_bytes": 61632512,
|
||||||
|
"cgroup_memory_peak_bytes": 44158976
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.198799,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.04178633399999998,
|
||||||
|
"rss_peak_bytes": 61964288,
|
||||||
|
"cgroup_memory_peak_bytes": 43732992
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"objects_scanned": 1126,
|
||||||
|
"rows_scanned": 30,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1126,
|
||||||
|
"ready_protected": 8,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.31776,
|
||||||
|
"complete": true,
|
||||||
|
"cpu_seconds": 0.08349883400000002,
|
||||||
|
"rss_peak_bytes": 62627840,
|
||||||
|
"cgroup_memory_peak_bytes": 45318144
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"method": "0/100/1100 extra fresh one-byte objects; one warmup then 3 repeats per dataset; real reconciler service .25 CPU/256MiB; grace3600; 8 PNG fixtures with deliberately aged PG metadata (2days); other runtime objects remain real",
|
||||||
|
"fixture_rows": 8,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
"stable_rows_head_bytes": [
|
||||||
|
30,
|
||||||
|
8,
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"median_seconds": {
|
||||||
|
"0": 0.193719,
|
||||||
|
"100": 0.204344,
|
||||||
|
"1100": 0.409094
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-15T18:04:52.576254+00:00",
|
||||||
|
"source_commit": "f5a7a74f679d936d851fa0e58f6cfb59dcd62c50",
|
||||||
|
"result": "PASS",
|
||||||
|
"smoke": {
|
||||||
|
"command": "make test PYTHON=.venv/bin/python BASE_URL=http://127.0.0.1:8084",
|
||||||
|
"seconds": 8.702,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ".venv/bin/python tests/smoke.py --base-url \"http://127.0.0.1:8084\" \nPASS: lab4 public API scenario; infrastructure and grading evidence remain separate.\n"
|
||||||
|
},
|
||||||
|
"http": {
|
||||||
|
"command": ".venv/bin/pytest student-tests -q",
|
||||||
|
"seconds": 362.959,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": "........................................................................ [ 66%]\n.................................... [100%]\n108 passed in 362.40s (0:06:02)\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"date": "2026-11-15T17:40:32.062021+00:00",
|
||||||
|
"source_commit": "fba6b6334e32653bf38576ec7e4dbff4e0626034",
|
||||||
|
"runtime_image_revision": "f449033e15f989aeaf35c776245cb4286e1f0ed5",
|
||||||
|
"command": "docker compose run --rm --no-deps migrate; repeat",
|
||||||
|
"migrations": [
|
||||||
|
{
|
||||||
|
"name": "001_core.sql",
|
||||||
|
"checksum": "4f91a73b92b768c48703b1adc1752b8c65f5e9da3da7c1f8b2db6d3dcb3c9efe"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "002_idempotency.sql",
|
||||||
|
"checksum": "b24f77687546b4f8d68cabb7009fc2b3db3d138c3c7a1eab32357e41cc0d6b13"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "003_attachments.sql",
|
||||||
|
"checksum": "6558f17559d4f96878400915d80a09a1fe5e9bcf7eeb6b0b04794d63ba03f3bd"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fresh_postgres_volume": true,
|
||||||
|
"repeat_idempotent": true,
|
||||||
|
"result": "PASS"
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
{
|
||||||
|
"source_origin": {
|
||||||
|
"VCS": "git",
|
||||||
|
"URL": "https://github.com/minio/minio",
|
||||||
|
"Hash": "9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a"
|
||||||
|
},
|
||||||
|
"version": "v0.0.0-20251015172955-9e49d5e7a648",
|
||||||
|
"module_checksum": "h1:6TdolSCLSs2nwm8i0PpWDqf9iX2Ty9WQK8wmr7dCnUM=",
|
||||||
|
"go_mod_checksum": "h1:yCWDkwWO9IWpGsT4mreDDN/B/QVmK2zC666uInRAcqE=",
|
||||||
|
"validation": "standard go mod download with default Go checksum database after ranged HTTPS download",
|
||||||
|
"compiler_build_status": "PASS",
|
||||||
|
"runtime_version": "minio version RELEASE.2025-10-15T17-29-55Z (commit-id=9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a)\nRuntime: go1.24.8 linux/arm64\nLicense: GNU AGPLv3 - https://www.gnu.org/licenses/agpl-3.0.html\nCopyright: 2015-0000 MinIO, Inc.",
|
||||||
|
"runtime_snapshot": [
|
||||||
|
{
|
||||||
|
"role": "api",
|
||||||
|
"container": "c9c495521bf2",
|
||||||
|
"pid": 39030,
|
||||||
|
"started_at": "2026-11-15T18:03:15.404279669Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 805306368,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "api",
|
||||||
|
"container": "2af84f3c87dd",
|
||||||
|
"pid": 39163,
|
||||||
|
"started_at": "2026-11-15T18:03:15.735686586Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 805306368,
|
||||||
|
"instance": "17cd27ba20064c5eb9580379e963f419"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "cache",
|
||||||
|
"container": "e33ddf35f293",
|
||||||
|
"pid": 5356,
|
||||||
|
"started_at": "2026-11-15T17:39:31.281698344Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 250000000,
|
||||||
|
"memory_limit": 134217728
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "lb",
|
||||||
|
"container": "6d7d02d323ed",
|
||||||
|
"pid": 39796,
|
||||||
|
"started_at": "2026-11-15T18:03:26.954206841Z",
|
||||||
|
"ports": {
|
||||||
|
"8080/tcp": [
|
||||||
|
{
|
||||||
|
"HostIp": "127.0.0.1",
|
||||||
|
"HostPort": "8084"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 134217728
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "postgres",
|
||||||
|
"container": "a5ffac5a4228",
|
||||||
|
"pid": 5418,
|
||||||
|
"started_at": "2026-11-15T17:39:31.614022427Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 1000000000,
|
||||||
|
"memory_limit": 536870912
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "reconciler",
|
||||||
|
"container": "c94b86137075",
|
||||||
|
"pid": 39092,
|
||||||
|
"started_at": "2026-11-15T18:03:15.578851294Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 250000000,
|
||||||
|
"memory_limit": 268435456
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "s3",
|
||||||
|
"container": "acc08930aff2",
|
||||||
|
"pid": 38412,
|
||||||
|
"started_at": "2026-11-15T18:03:03.039434803Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 1000000000,
|
||||||
|
"memory_limit": 1073741824
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"runtime_image_revision": {
|
||||||
|
"api": "f5a7a74f679d936d851fa0e58f6cfb59dcd62c50",
|
||||||
|
"s3": "9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a",
|
||||||
|
"bootstrap": "7394ce0dd2a80935aded936b09fa12cbb3cb8096"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
Traceback (most recent call last):
|
||||||
|
File "<repository>/scripts/verify_clean.py", line 215, in <module>
|
||||||
|
main()
|
||||||
|
~~~~^^
|
||||||
|
File "<repository>/scripts/verify_clean.py", line 130, in main
|
||||||
|
receipt["http_tests"] = run(
|
||||||
|
~~~^
|
||||||
|
[".venv/bin/pytest", "student-tests", "-q"], cwd=clone, env=env
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
)
|
||||||
|
^
|
||||||
|
File "<repository>/scripts/verify_clean.py", line 55, in run
|
||||||
|
raise RuntimeError(redact(result.stdout + result.stderr))
|
||||||
|
RuntimeError: ...........................................................F............ [ 66%]
|
||||||
|
.................................... [100%]
|
||||||
|
=================================== FAILURES ===================================
|
||||||
|
____________________________ test_registration_race ____________________________
|
||||||
|
|
||||||
|
client = <httpx.Client object at 0x120c15790>
|
||||||
|
|
||||||
|
def test_registration_race(client):
|
||||||
|
username = "race_" + uuid4().hex[:20]
|
||||||
|
barrier = threading.Barrier(10)
|
||||||
|
|
||||||
|
def worker(_):
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE_URL, timeout=15, trust_env=False
|
||||||
|
) as conn:
|
||||||
|
barrier.wait(timeout=15)
|
||||||
|
result = conn.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Test",
|
||||||
|
"password": PASSWORD,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
result,
|
||||||
|
result.status_code,
|
||||||
|
"User" if result.status_code == 201 else "Error",
|
||||||
|
)
|
||||||
|
return result.status_code
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
> statuses = list(pool.map(worker, range(10)))
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
student-tests/test_api.py:406:
|
||||||
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/_base.py:645: in result_iterator
|
||||||
|
yield _result_or_cancel(fs.pop())
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/_base.py:312: in _result_or_cancel
|
||||||
|
return fut.result(timeout)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/_base.py:454: in result
|
||||||
|
return self.__get_result()
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/_base.py:396: in __get_result
|
||||||
|
raise self._exception
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/thread.py:86: in run
|
||||||
|
result = ctx.run(self.task)
|
||||||
|
^^^^^^^^^^^^^^^^^^
|
||||||
|
<home>/.local/share/uv/python/cpython-3.14.7-macos-aarch64-none/lib/python3.14/concurrent/futures/thread.py:73: in run
|
||||||
|
return fn(*args, **kwargs)
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
student-tests/test_api.py:398: in worker
|
||||||
|
check(
|
||||||
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
|
||||||
|
|
||||||
|
result = <Response [503 Service Unavailable]>, status = 503, schema = 'Error'
|
||||||
|
|
||||||
|
def check(result: httpx.Response, status: int, schema: str) -> dict:
|
||||||
|
assert result.status_code == status, result.text
|
||||||
|
assert result.headers["x-request-id"]
|
||||||
|
assert result.headers["x-instance-id"]
|
||||||
|
assert result.headers["content-type"].startswith("application/json")
|
||||||
|
payload = result.json()
|
||||||
|
spec = {"$ref": f"#/components/schemas/{schema}", **CONTRACT}
|
||||||
|
Draft202012Validator(spec, format_checker=FormatChecker()).validate(
|
||||||
|
payload
|
||||||
|
)
|
||||||
|
if schema == "Error":
|
||||||
|
assert payload["error"]["request_id"] == result.headers["x-request-id"]
|
||||||
|
assert (
|
||||||
|
payload["error"]["code"]
|
||||||
|
> == {
|
||||||
|
400: "invalid_request",
|
||||||
|
401: "unauthorized",
|
||||||
|
404: "not_found",
|
||||||
|
409: "conflict",
|
||||||
|
500: "internal_error",
|
||||||
|
}[status]
|
||||||
|
)
|
||||||
|
E KeyError: 503
|
||||||
|
|
||||||
|
student-tests/conftest.py:32: KeyError
|
||||||
|
=========================== short test summary info ============================
|
||||||
|
FAILED student-tests/test_api.py::test_registration_race - KeyError: 503
|
||||||
|
1 failed, 107 passed in 377.89s (0:06:17)
|
||||||
|
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
{
|
||||||
|
"source_commit": "78670b9fa67202ddc26570daa245c75d1837991a",
|
||||||
|
"date": "2026-11-15T19:00:28.275924+00:00",
|
||||||
|
"complete": true,
|
||||||
|
"runs": [
|
||||||
|
{
|
||||||
|
"repeat": 1,
|
||||||
|
"seconds": 1.6275634999910835,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 1.17s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 2,
|
||||||
|
"seconds": 1.1972682919877116,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.83s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 3,
|
||||||
|
"seconds": 1.2752694170048926,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.90s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 4,
|
||||||
|
"seconds": 1.3356571669864934,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.97s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 5,
|
||||||
|
"seconds": 1.1046671249787323,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.74s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 6,
|
||||||
|
"seconds": 1.2911863749905024,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.93s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 7,
|
||||||
|
"seconds": 1.6777148330002092,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 1.32s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 8,
|
||||||
|
"seconds": 1.4123140000156127,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 1.03s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 9,
|
||||||
|
"seconds": 1.2036787920224015,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.83s\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"repeat": 10,
|
||||||
|
"seconds": 1.2103008750127628,
|
||||||
|
"exit_code": 0,
|
||||||
|
"output": ". [100%]\n1 passed in 0.84s\n"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,502 @@
|
|||||||
|
{
|
||||||
|
"source_commit": "f5a7a74f679d936d851fa0e58f6cfb59dcd62c50",
|
||||||
|
"date": "2026-11-15T18:13:30.360880+00:00",
|
||||||
|
"complete": true,
|
||||||
|
"runtime": [
|
||||||
|
{
|
||||||
|
"role": "api",
|
||||||
|
"container": "c9c495521bf2",
|
||||||
|
"pid": 39030,
|
||||||
|
"started_at": "2026-11-15T18:03:15.404279669Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 805306368,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "api",
|
||||||
|
"container": "2af84f3c87dd",
|
||||||
|
"pid": 39163,
|
||||||
|
"started_at": "2026-11-15T18:03:15.735686586Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 805306368,
|
||||||
|
"instance": "17cd27ba20064c5eb9580379e963f419"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "cache",
|
||||||
|
"container": "e33ddf35f293",
|
||||||
|
"pid": 5356,
|
||||||
|
"started_at": "2026-11-15T17:39:31.281698344Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 250000000,
|
||||||
|
"memory_limit": 134217728
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "lb",
|
||||||
|
"container": "6d7d02d323ed",
|
||||||
|
"pid": 39796,
|
||||||
|
"started_at": "2026-11-15T18:03:26.954206841Z",
|
||||||
|
"ports": {
|
||||||
|
"8080/tcp": [
|
||||||
|
{
|
||||||
|
"HostIp": "127.0.0.1",
|
||||||
|
"HostPort": "8084"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"cpu_nano": 500000000,
|
||||||
|
"memory_limit": 134217728
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "postgres",
|
||||||
|
"container": "a5ffac5a4228",
|
||||||
|
"pid": 5418,
|
||||||
|
"started_at": "2026-11-15T17:39:31.614022427Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 1000000000,
|
||||||
|
"memory_limit": 536870912
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "reconciler",
|
||||||
|
"container": "c94b86137075",
|
||||||
|
"pid": 39092,
|
||||||
|
"started_at": "2026-11-15T18:03:15.578851294Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 250000000,
|
||||||
|
"memory_limit": 268435456
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"role": "s3",
|
||||||
|
"container": "acc08930aff2",
|
||||||
|
"pid": 38412,
|
||||||
|
"started_at": "2026-11-15T18:03:03.039434803Z",
|
||||||
|
"ports": {},
|
||||||
|
"cpu_nano": 1000000000,
|
||||||
|
"memory_limit": 1073741824
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"upload_replica_stopped_downloaded_elsewhere": {
|
||||||
|
"stopped_container": "2af84f3c87dd",
|
||||||
|
"uploader": "17cd27ba20064c5eb9580379e963f419",
|
||||||
|
"downloader": "e91b8ff7122d4842abb3aa0ff38e0352",
|
||||||
|
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"
|
||||||
|
},
|
||||||
|
"private_bucket_and_iam": {
|
||||||
|
"denied_head_is_not_missing": 503,
|
||||||
|
"anonymous_get": 403,
|
||||||
|
"list_other_bucket": "AccessDenied",
|
||||||
|
"create_bucket": "AccessDenied",
|
||||||
|
"write_outside_prefix": "AccessDenied",
|
||||||
|
"console_internal_network": "unreachable"
|
||||||
|
},
|
||||||
|
"bootstrap_repeat_preserved_original": true,
|
||||||
|
"aborted_upload_has_no_intent_or_bytes": true,
|
||||||
|
"pg_intent_failure_has_no_bytes": true,
|
||||||
|
"s3_unavailable": {
|
||||||
|
"status": 503,
|
||||||
|
"error": "unavailable"
|
||||||
|
},
|
||||||
|
"concurrent_uploads": {
|
||||||
|
"concurrency": 10,
|
||||||
|
"requests": 10,
|
||||||
|
"successes": 10,
|
||||||
|
"errors": 0,
|
||||||
|
"bytes_per_image": 6017095,
|
||||||
|
"pixels": 2890000,
|
||||||
|
"duration_seconds": 5.149,
|
||||||
|
"successful_rps": 1.942,
|
||||||
|
"latency_scope": "upload only; duration includes verified downloads and memory sampler",
|
||||||
|
"p50_ms": 2162.965,
|
||||||
|
"p95_ms": 4465.512,
|
||||||
|
"p99_ms": 4465.512,
|
||||||
|
"cpu_before": {
|
||||||
|
"c9c495521bf2e0eb21d94eebc8ac06ea3735ee282401f8bbf4918203b05e69ea": "usage_usec 148242470\nuser_usec 139413925\nsystem_usec 8828544\nnice_usec 0\nnr_periods 5268\nnr_throttled 2720\nthrottled_usec 566509749\nnr_bursts 0\nburst_usec 0",
|
||||||
|
"2af84f3c87dd51105775135adaa2864b852c68c3b3d929f566d714ee4bbf13a2": "usage_usec 7938759\nuser_usec 7311622\nsystem_usec 627137\nnice_usec 0\nnr_periods 338\nnr_throttled 144\nthrottled_usec 17342812\nnr_bursts 0\nburst_usec 0"
|
||||||
|
},
|
||||||
|
"cpu_after": {
|
||||||
|
"c9c495521bf2e0eb21d94eebc8ac06ea3735ee282401f8bbf4918203b05e69ea": "usage_usec 150694911\nuser_usec 141253264\nsystem_usec 9441647\nnice_usec 0\nnr_periods 5320\nnr_throttled 2767\nthrottled_usec 573793811\nnr_bursts 0\nburst_usec 0",
|
||||||
|
"2af84f3c87dd51105775135adaa2864b852c68c3b3d929f566d714ee4bbf13a2": "usage_usec 9567439\nuser_usec 8568377\nsystem_usec 999061\nnice_usec 0\nnr_periods 386\nnr_throttled 171\nthrottled_usec 20452342\nnr_bursts 0\nburst_usec 0"
|
||||||
|
},
|
||||||
|
"memory_samples": [
|
||||||
|
{
|
||||||
|
"at": 0.0,
|
||||||
|
"container": "c9c495521bf2",
|
||||||
|
"memory.current": 128004096,
|
||||||
|
"memory.peak": 215093248
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"at": 0.931,
|
||||||
|
"container": "2af84f3c87dd",
|
||||||
|
"memory.current": 128983040,
|
||||||
|
"memory.peak": 152731648
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"at": 2.318,
|
||||||
|
"container": "c9c495521bf2",
|
||||||
|
"memory.current": 163500032,
|
||||||
|
"memory.peak": 215093248
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"at": 3.846,
|
||||||
|
"container": "2af84f3c87dd",
|
||||||
|
"memory.current": 126009344,
|
||||||
|
"memory.peak": 152731648
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"at": 4.515,
|
||||||
|
"container": "c9c495521bf2",
|
||||||
|
"memory.current": 153980928,
|
||||||
|
"memory.peak": 215093248
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"at": 4.862,
|
||||||
|
"container": "2af84f3c87dd",
|
||||||
|
"memory.current": 125583360,
|
||||||
|
"memory.peak": 152731648
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"results": [
|
||||||
|
{
|
||||||
|
"id": "7df54468-dffb-48e1-b435-bb64e73b087c",
|
||||||
|
"latency_ms": 774.2156249878462,
|
||||||
|
"instance": "ba0474b668fd43b294ece08957bf0648"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "e1ba3763-89f2-497f-a152-6008f4737e08",
|
||||||
|
"latency_ms": 1630.1402079989202,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "69c854d8-ca53-46aa-9dbd-646b68bc42be",
|
||||||
|
"latency_ms": 3091.3296249927953,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "03328959-bf30-4e57-a233-4084e599bbfc",
|
||||||
|
"latency_ms": 2098.3562500041444,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "483eff54-3137-4764-bc89-3feccf7d00e6",
|
||||||
|
"latency_ms": 4303.714374982519,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "f4eb43f0-65bf-462c-bfd2-9c8d88ff1d34",
|
||||||
|
"latency_ms": 1105.6364579999354,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "90a80cf9-d955-4bcc-9f16-01587a3f2f66",
|
||||||
|
"latency_ms": 3825.5119579844177,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "3e2a2c9c-4204-4883-b18a-5b1deeca70c3",
|
||||||
|
"latency_ms": 2162.9652910050936,
|
||||||
|
"instance": "ba0474b668fd43b294ece08957bf0648"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ea4b8e91-80fe-4741-8d0b-c77d2da6656f",
|
||||||
|
"latency_ms": 4465.512333001243,
|
||||||
|
"instance": "e91b8ff7122d4842abb3aa0ff38e0352"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "031a9c2b-4c93-4dd4-935b-8104080a0a86",
|
||||||
|
"latency_ms": 1169.7680839861277,
|
||||||
|
"instance": "ba0474b668fd43b294ece08957bf0648"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"window_max_memory_current": 163500032,
|
||||||
|
"lifetime_memory_peak": 215093248
|
||||||
|
},
|
||||||
|
"reconciliation": {
|
||||||
|
"dry_run": {
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 20,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 8,
|
||||||
|
"bytes_read": 111817,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 5,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {
|
||||||
|
"delete_orphan": 1,
|
||||||
|
"recover_pending": 1,
|
||||||
|
"fail_pending_missing": 1,
|
||||||
|
"delete_failed_object": 1,
|
||||||
|
"mark_missing": 1
|
||||||
|
},
|
||||||
|
"applied": {},
|
||||||
|
"details": [
|
||||||
|
{
|
||||||
|
"action": "delete_orphan",
|
||||||
|
"identity": "objects/verification-f49e0d70d0f54f62bce62b4a3dccdbdc/orphan"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "recover_pending",
|
||||||
|
"identity": "06e24ef9-2a84-43d1-bc08-facf33155e9f"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "fail_pending_missing",
|
||||||
|
"identity": "44b0d154-ed76-4f79-a356-7feb85a3b14c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "delete_failed_object",
|
||||||
|
"identity": "54e914d0-5a4c-4dc9-982c-ef887467700c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "mark_missing",
|
||||||
|
"identity": "fa66fcd8-ca7e-41e4-8f25-25ff119c38e9"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"duration_seconds": 0.0604,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
"apply": {
|
||||||
|
"apply": true,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 20,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 10,
|
||||||
|
"bytes_read": 111817,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 5,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {
|
||||||
|
"delete_orphan": 1,
|
||||||
|
"recover_pending": 1,
|
||||||
|
"fail_pending_missing": 1,
|
||||||
|
"delete_failed_object": 1,
|
||||||
|
"mark_missing": 1
|
||||||
|
},
|
||||||
|
"applied": {
|
||||||
|
"delete_orphan": 1,
|
||||||
|
"recover_pending": 1,
|
||||||
|
"fail_pending_missing": 1,
|
||||||
|
"delete_failed_object": 1,
|
||||||
|
"mark_missing": 1
|
||||||
|
},
|
||||||
|
"details": [
|
||||||
|
{
|
||||||
|
"action": "delete_orphan",
|
||||||
|
"identity": "objects/verification-f49e0d70d0f54f62bce62b4a3dccdbdc/orphan"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "recover_pending",
|
||||||
|
"identity": "06e24ef9-2a84-43d1-bc08-facf33155e9f"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "fail_pending_missing",
|
||||||
|
"identity": "44b0d154-ed76-4f79-a356-7feb85a3b14c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "delete_failed_object",
|
||||||
|
"identity": "54e914d0-5a4c-4dc9-982c-ef887467700c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "mark_missing",
|
||||||
|
"identity": "fa66fcd8-ca7e-41e4-8f25-25ff119c38e9"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"duration_seconds": 0.106004,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
"repeat": {
|
||||||
|
"apply": true,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 18,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.102749,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
"s3_grace_wait_seconds": 331.301,
|
||||||
|
"fresh_and_ready_unlinked_protected": true,
|
||||||
|
"old_failed_unlinked_deleted_and_fresh_failed_protected": true,
|
||||||
|
"missing_used_id_and_link_preserved": true,
|
||||||
|
"dry_run_unchanged": true
|
||||||
|
},
|
||||||
|
"cost_research": {
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 18,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.183485,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 18,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.043382,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 0,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 18,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 13,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.046774,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 112,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.078726,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 112,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.088476,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 100,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 112,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.206964,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 1,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 1118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 6,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1112,
|
||||||
|
"ready_protected": 6,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.197227,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 2,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 1118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 10,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1108,
|
||||||
|
"ready_protected": 10,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.145977,
|
||||||
|
"complete": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"extra_objects": 1100,
|
||||||
|
"repetition": 3,
|
||||||
|
"apply": false,
|
||||||
|
"grace_seconds": 300,
|
||||||
|
"objects_scanned": 1118,
|
||||||
|
"rows_scanned": 20,
|
||||||
|
"head_requests": 16,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 1102,
|
||||||
|
"ready_protected": 16,
|
||||||
|
"linked_protected": 1,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
"duration_seconds": 0.199997,
|
||||||
|
"complete": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"repeats": 3,
|
||||||
|
"method": "same running stand; 0/100/1100 fresh one-byte objects; dry-run; serial ascending dataset; no warmup",
|
||||||
|
"median_seconds": {
|
||||||
|
"0": 0.046774,
|
||||||
|
"100": 0.088476,
|
||||||
|
"1100": 0.197227
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 80 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
@@ -0,0 +1,107 @@
|
|||||||
|
// Fetch the immutable module ZIP in bounded HTTP ranges. The subsequent
|
||||||
|
// go mod download validates its h1 checksum through the Go checksum database.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
const version = "v0.0.0-20251015172955-9e49d5e7a648"
|
||||||
|
const total int64 = 25342606
|
||||||
|
const chunk int64 = 512 * 1024
|
||||||
|
const source = "https://proxy.golang.org/github.com/minio/minio/@v/" + version + ".zip"
|
||||||
|
directory := "/go/pkg/mod/cache/download/github.com/minio/minio/@v"
|
||||||
|
if err := os.MkdirAll(directory, 0755); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
target := filepath.Join(directory, version+".zip")
|
||||||
|
if info, err := os.Stat(target); err == nil && info.Size() == total {
|
||||||
|
fmt.Println("Source ZIP cached; Go will validate checksum")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
client := &http.Client{Timeout: 90 * time.Second}
|
||||||
|
jobs := make(chan int64)
|
||||||
|
errors := make(chan error, 64)
|
||||||
|
var group sync.WaitGroup
|
||||||
|
for worker := 0; worker < 8; worker++ {
|
||||||
|
group.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer group.Done()
|
||||||
|
for start := range jobs {
|
||||||
|
end := min(start+chunk, total) - 1
|
||||||
|
part := fmt.Sprintf("%s.part-%d", target, start)
|
||||||
|
if info, err := os.Stat(part); err == nil && info.Size() == end-start+1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var last error
|
||||||
|
for attempt := 0; attempt < 4; attempt++ {
|
||||||
|
req, _ := http.NewRequest("GET", source, nil)
|
||||||
|
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
|
||||||
|
response, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
last = fmt.Errorf("source range request failed")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
expected := fmt.Sprintf("bytes %d-%d/%d", start, end, total)
|
||||||
|
if response.StatusCode != 206 || response.Header.Get("Content-Range") != expected {
|
||||||
|
response.Body.Close()
|
||||||
|
last = fmt.Errorf("unexpected source range response: HTTP %d", response.StatusCode)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(io.LimitReader(response.Body, end-start+2))
|
||||||
|
response.Body.Close()
|
||||||
|
if err != nil || int64(len(data)) != end-start+1 {
|
||||||
|
last = fmt.Errorf("incomplete source range")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(part, data, 0644); err != nil {
|
||||||
|
last = err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
last = nil
|
||||||
|
fmt.Printf("Source part %d verified\n", start/chunk+1)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if last != nil {
|
||||||
|
errors <- last
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
for start := int64(0); start < total; start += chunk {
|
||||||
|
jobs <- start
|
||||||
|
}
|
||||||
|
close(jobs)
|
||||||
|
group.Wait()
|
||||||
|
close(errors)
|
||||||
|
for err := range errors {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
out, err := os.Create(target + ".partial")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for start := int64(0); start < total; start += chunk {
|
||||||
|
part, err := os.Open(fmt.Sprintf("%s.part-%d", target, start))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(out, part); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
part.Close()
|
||||||
|
}
|
||||||
|
if err := out.Close(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := os.Rename(target+".partial", target); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
global
|
||||||
|
log stdout format raw local0
|
||||||
|
maxconn 512
|
||||||
|
stats socket /tmp/admin.sock mode 600 level admin
|
||||||
|
|
||||||
|
defaults
|
||||||
|
mode http
|
||||||
|
log global
|
||||||
|
option httplog
|
||||||
|
timeout connect 1s
|
||||||
|
timeout client 15s
|
||||||
|
timeout server 8s
|
||||||
|
timeout http-request 10s
|
||||||
|
timeout http-keep-alive 5s
|
||||||
|
retries 0
|
||||||
|
|
||||||
|
resolvers docker
|
||||||
|
nameserver dns 127.0.0.11:53
|
||||||
|
resolve_retries 3
|
||||||
|
timeout resolve 1s
|
||||||
|
timeout retry 1s
|
||||||
|
hold valid 1s
|
||||||
|
hold obsolete 1s
|
||||||
|
hold nx 1s
|
||||||
|
|
||||||
|
frontend public
|
||||||
|
bind :8080
|
||||||
|
default_backend messenger
|
||||||
|
|
||||||
|
backend messenger
|
||||||
|
balance roundrobin
|
||||||
|
option httpchk
|
||||||
|
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api
|
||||||
|
http-check expect status 200
|
||||||
|
default-server check inter 1s fastinter 250ms fall 2 rise 1 resolvers docker init-addr libc,none resolve-prefer ipv4
|
||||||
|
server-template api 1-8 api:8080
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
root_user=$(cat /run/secrets/s3_root_user)
|
||||||
|
root_password=$(cat /run/secrets/s3_root_password)
|
||||||
|
api_key=$(cat /run/secrets/s3_access_key)
|
||||||
|
api_secret=$(cat /run/secrets/s3_secret_key)
|
||||||
|
# Credentials are passed only inside this isolated bootstrap container.
|
||||||
|
mc alias set local http://s3:9000 "$root_user" "$root_password" >/dev/null 2>&1
|
||||||
|
mc mb --ignore-existing local/messenger >/dev/null 2>&1
|
||||||
|
mc anonymous set none local/messenger >/dev/null 2>&1
|
||||||
|
mc admin user add local "$api_key" "$api_secret" >/dev/null 2>&1
|
||||||
|
mc admin policy create local messenger-api /policy.json >/dev/null 2>&1
|
||||||
|
mc admin policy attach local messenger-api --user "$api_key" >/dev/null 2>&1
|
||||||
|
printf '%s\n' 'Private bucket and scoped application policy ready'
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
export MINIO_ROOT_USER="$(cat /run/secrets/s3_root_user)"
|
||||||
|
export MINIO_ROOT_PASSWORD="$(cat /run/secrets/s3_root_password)"
|
||||||
|
export MINIO_BROWSER_REDIRECT_URL=http://127.0.0.1:9001
|
||||||
|
exec minio server /data --address :9000 --console-address 127.0.0.1:9001
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": ["s3:GetBucketLocation", "s3:ListBucket"],
|
||||||
|
"Resource": ["arn:aws:s3:::messenger"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||||
|
"Resource": ["arn:aws:s3:::messenger/objects/*"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
FROM golang:1.24.8-bookworm@sha256:4ed690d6649d63c312b99a6120025ec79ce3b542968a37da53d6236c7c61a848 AS toolchain
|
||||||
|
ENV CGO_ENABLED=0 GOMAXPROCS=2 GOFLAGS=-p=2 GOTOOLCHAIN=local
|
||||||
|
|
||||||
|
FROM toolchain AS minio-build
|
||||||
|
WORKDIR /source
|
||||||
|
COPY infra/fetch-source.go /tmp/fetch-source.go
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod,sharing=locked \
|
||||||
|
--mount=type=cache,target=/root/.cache/go-build,sharing=locked \
|
||||||
|
go run /tmp/fetch-source.go && \
|
||||||
|
for attempt in 1 2 3 4; do \
|
||||||
|
go mod download -json github.com/minio/minio@v0.0.0-20251015172955-9e49d5e7a648 && break; \
|
||||||
|
test "$attempt" -lt 4 || exit 1; sleep 1; \
|
||||||
|
done && \
|
||||||
|
cp -a /go/pkg/mod/github.com/minio/minio@v0.0.0-20251015172955-9e49d5e7a648/. /source/
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod,sharing=locked \
|
||||||
|
--mount=type=cache,target=/root/.cache/go-build,sharing=locked \
|
||||||
|
for attempt in 1 2 3; do \
|
||||||
|
timeout 900s go build -trimpath -ldflags='-s -w -X github.com/minio/minio/cmd.Version=2025-10-15T17:29:55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z -X github.com/minio/minio/cmd.CommitID=9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a' -o /out/minio . && break; \
|
||||||
|
test "$attempt" -lt 3 || exit 1; sleep 1; \
|
||||||
|
done
|
||||||
|
|
||||||
|
FROM toolchain AS mc-build
|
||||||
|
WORKDIR /source
|
||||||
|
RUN git init . && git remote add origin https://github.com/minio/mc.git && \
|
||||||
|
git fetch --depth 1 origin 7394ce0dd2a80935aded936b09fa12cbb3cb8096 && git checkout --detach FETCH_HEAD
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod,sharing=locked \
|
||||||
|
--mount=type=cache,target=/root/.cache/go-build,sharing=locked \
|
||||||
|
go build -trimpath -ldflags='-s -w' -o /out/mc .
|
||||||
|
|
||||||
|
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS server
|
||||||
|
LABEL org.opencontainers.image.revision=9e49d5e7a648f00e26f2246f4dc28e6b07f8c84a org.opencontainers.image.version=RELEASE.2025-10-15T17-29-55Z
|
||||||
|
COPY --from=minio-build /out/minio /usr/local/bin/minio
|
||||||
|
COPY --from=minio-build /source/LICENSE /licenses/minio.LICENSE
|
||||||
|
COPY --chmod=755 infra/s3-entrypoint.sh /usr/local/bin/s3-entrypoint
|
||||||
|
RUN mkdir /data && chown 10001:10001 /data
|
||||||
|
USER 10001:10001
|
||||||
|
ENTRYPOINT ["/usr/local/bin/s3-entrypoint"]
|
||||||
|
EXPOSE 9000
|
||||||
|
HEALTHCHECK --interval=2s --timeout=2s --start-period=10s --retries=30 CMD ["python","-c","import urllib.request; urllib.request.urlopen('http://127.0.0.1:9000/minio/health/ready',timeout=1).read()"]
|
||||||
|
|
||||||
|
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS bootstrap
|
||||||
|
LABEL org.opencontainers.image.revision=7394ce0dd2a80935aded936b09fa12cbb3cb8096 org.opencontainers.image.version=RELEASE.2025-08-13T08-35-41Z
|
||||||
|
COPY --from=mc-build /out/mc /usr/local/bin/mc
|
||||||
|
COPY --from=mc-build /source/LICENSE /licenses/mc.LICENSE
|
||||||
|
COPY --chmod=755 infra/s3-bootstrap.sh /usr/local/bin/s3-bootstrap
|
||||||
|
COPY infra/s3-policy.json /policy.json
|
||||||
|
USER 10001:10001
|
||||||
|
ENV MC_CONFIG_DIR=/tmp/mc
|
||||||
|
ENTRYPOINT ["/usr/local/bin/s3-bootstrap"]
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
CREATE TABLE users (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||||
|
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||||
|
password_hash text NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL
|
||||||
|
);
|
||||||
|
CREATE TABLE chats (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||||
|
created_at timestamptz NOT NULL
|
||||||
|
);
|
||||||
|
CREATE TABLE members (
|
||||||
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||||
|
user_id uuid NOT NULL REFERENCES users(id),
|
||||||
|
position integer NOT NULL CHECK (position BETWEEN 0 AND 99),
|
||||||
|
PRIMARY KEY (chat_id, user_id),
|
||||||
|
UNIQUE (chat_id, position)
|
||||||
|
);
|
||||||
|
CREATE INDEX members_user_chat_idx ON members(user_id,chat_id);
|
||||||
|
CREATE TABLE messages (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||||
|
sender_id uuid NOT NULL,
|
||||||
|
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||||
|
created_at timestamptz NOT NULL,
|
||||||
|
FOREIGN KEY (chat_id, sender_id) REFERENCES members(chat_id,user_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX messages_history_idx ON messages(chat_id,created_at,id);
|
||||||
|
CREATE TABLE sessions (
|
||||||
|
token_digest bytea PRIMARY KEY CHECK (octet_length(token_digest)=32),
|
||||||
|
user_id uuid NOT NULL REFERENCES users(id),
|
||||||
|
created_at timestamptz NOT NULL,
|
||||||
|
expires_at timestamptz NOT NULL CHECK (expires_at > created_at)
|
||||||
|
);
|
||||||
|
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE idempotency (
|
||||||
|
user_id uuid NOT NULL REFERENCES users(id),
|
||||||
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||||
|
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||||
|
body_hash bytea NOT NULL CHECK (octet_length(body_hash)=32),
|
||||||
|
message_id uuid NOT NULL REFERENCES messages(id),
|
||||||
|
expires_at timestamptz NOT NULL,
|
||||||
|
PRIMARY KEY(user_id, chat_id, key)
|
||||||
|
);
|
||||||
|
CREATE INDEX idempotency_expiry_idx ON idempotency(expires_at);
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||||
|
ALTER TABLE messages ADD CHECK (char_length(text) <= 4000);
|
||||||
|
ALTER TABLE messages ADD UNIQUE(chat_id,id);
|
||||||
|
CREATE TABLE attachments (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||||
|
owner_id uuid NOT NULL,
|
||||||
|
filename text NOT NULL CHECK(char_length(filename) BETWEEN 1 AND 255),
|
||||||
|
content_type text NOT NULL CHECK(content_type IN ('image/png','image/jpeg')),
|
||||||
|
size_bytes bigint NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760),
|
||||||
|
sha256 text NOT NULL CHECK(sha256 ~ '^[a-f0-9]{64}$'),
|
||||||
|
object_key text NOT NULL UNIQUE,
|
||||||
|
state text NOT NULL CHECK(state IN ('pending','ready','failed')),
|
||||||
|
error_code text,
|
||||||
|
uploader_instance text NOT NULL,
|
||||||
|
expires_at timestamptz NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||||
|
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||||
|
UNIQUE(chat_id,id),
|
||||||
|
FOREIGN KEY(chat_id,owner_id) REFERENCES members(chat_id,user_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX attachments_state_created_idx ON attachments(state,created_at,id);
|
||||||
|
CREATE TABLE message_attachments (
|
||||||
|
message_id uuid NOT NULL,
|
||||||
|
chat_id uuid NOT NULL,
|
||||||
|
attachment_id uuid NOT NULL,
|
||||||
|
position integer NOT NULL CHECK(position BETWEEN 0 AND 9),
|
||||||
|
PRIMARY KEY(message_id,attachment_id),
|
||||||
|
UNIQUE(message_id,position),
|
||||||
|
FOREIGN KEY(chat_id,message_id) REFERENCES messages(chat_id,id),
|
||||||
|
FOREIGN KEY(chat_id,attachment_id) REFERENCES attachments(chat_id,id)
|
||||||
|
);
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
[project]
|
||||||
|
name = "mtusi-messenger-lab4"
|
||||||
|
version = "1.0.0"
|
||||||
|
requires-python = ">=3.14,<3.15"
|
||||||
|
dependencies = [
|
||||||
|
"fastapi==0.143.3",
|
||||||
|
"pydantic==2.14.1",
|
||||||
|
"uvicorn==0.54.0",
|
||||||
|
"psycopg[binary,pool]==3.3.6",
|
||||||
|
"argon2-cffi>=25,<26",
|
||||||
|
"redis==8.1.0",
|
||||||
|
"minio==7.2.20",
|
||||||
|
"pillow==13.0.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[dependency-groups]
|
||||||
|
dev = ["pytest>=8,<10", "httpx>=0.28,<1", "ruff>=0.14,<1", "openapi-spec-validator==0.7.2", "psutil>=7,<8", "matplotlib>=3.10,<4"]
|
||||||
|
|
||||||
|
[tool.ruff]
|
||||||
|
line-length = 79
|
||||||
|
target-version = "py314"
|
||||||
|
include = ["src/**/*.py", "student-tests/**/*.py", "research/**/*.py", "scripts/verify_*.py"]
|
||||||
|
|
||||||
|
[tool.ruff.lint]
|
||||||
|
select = ["E", "F", "I", "B", "UP"]
|
||||||
|
|
||||||
|
[tool.ruff.per-file-target-version]
|
||||||
|
"scripts/*.py" = "py39"
|
||||||
|
|
||||||
|
# SQL и генерируемые Markdown-таблицы в инструментах не разрываем.
|
||||||
|
[tool.ruff.lint.per-file-ignores]
|
||||||
|
"research/*.py" = ["E501"]
|
||||||
|
"scripts/*.py" = ["E501"]
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
testpaths = ["student-tests"]
|
||||||
|
addopts = "-ra"
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
"""Plot only measured storage evidence; no reconstructed timing samples."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import matplotlib
|
||||||
|
|
||||||
|
matplotlib.use("Agg")
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("input")
|
||||||
|
parser.add_argument("--cost-input")
|
||||||
|
parser.add_argument("--output", default="evidence/storage-research.png")
|
||||||
|
args = parser.parse_args()
|
||||||
|
data = json.loads(Path(args.input).read_text())
|
||||||
|
assert data["complete"]
|
||||||
|
series = data["cost_research"]
|
||||||
|
if args.cost_input:
|
||||||
|
series = json.loads(Path(args.cost_input).read_text())
|
||||||
|
assert series["complete"]
|
||||||
|
fig, axes = plt.subplots(1, 2, figsize=(11, 4), layout="constrained")
|
||||||
|
for cell in series["cells"]:
|
||||||
|
axes[0].scatter(
|
||||||
|
cell["objects_scanned"],
|
||||||
|
cell["duration_seconds"],
|
||||||
|
color="#176b87",
|
||||||
|
alpha=0.65,
|
||||||
|
)
|
||||||
|
axes[0].set(
|
||||||
|
xlabel="Objects scanned (including real attachments)",
|
||||||
|
ylabel="Dry-run duration, seconds",
|
||||||
|
title="Inventory cost: 3 repeats per dataset",
|
||||||
|
)
|
||||||
|
measured = data["concurrent_uploads"]
|
||||||
|
ids = sorted({s["container"] for s in measured["memory_samples"]})
|
||||||
|
for i, cid in enumerate(ids):
|
||||||
|
points = [
|
||||||
|
s for s in measured["memory_samples"] if s["container"] == cid
|
||||||
|
]
|
||||||
|
axes[1].plot(
|
||||||
|
[s["at"] for s in points],
|
||||||
|
[s["memory.current"] / 2**20 for s in points],
|
||||||
|
label=f"API {i + 1} cgroup current",
|
||||||
|
marker="o",
|
||||||
|
markersize=3,
|
||||||
|
)
|
||||||
|
axes[1].set(
|
||||||
|
xlabel="Time, seconds",
|
||||||
|
ylabel="Memory, MiB",
|
||||||
|
title="10 simultaneous uploads, verified downloads",
|
||||||
|
)
|
||||||
|
axes[1].legend(fontsize=8)
|
||||||
|
for ax in axes:
|
||||||
|
ax.grid(alpha=0.2)
|
||||||
|
fig.savefig(args.output, dpi=160)
|
||||||
|
print("Saved measured inventory and memory figure")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
"""Controlled inventory experiment on the teaching stand, with own fixtures."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import statistics
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from scripts.stand import BASE, actor, auth, command, compose, ready, sql
|
||||||
|
from scripts.verify_storage import PNG, PREFIX, inside
|
||||||
|
|
||||||
|
|
||||||
|
def probe():
|
||||||
|
# Run the actual code with the reconciler service's .25 CPU /256 MiB.
|
||||||
|
code = (
|
||||||
|
PREFIX
|
||||||
|
+ """
|
||||||
|
import pathlib,resource
|
||||||
|
started=time.process_time()
|
||||||
|
result=reconcile(d,s,apply=False,grace=3600)
|
||||||
|
result['cpu_seconds']=time.process_time()-started
|
||||||
|
result['rss_peak_bytes']=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024
|
||||||
|
result['cgroup_memory_peak_bytes']=int(pathlib.Path('/sys/fs/cgroup/memory.peak').read_text())
|
||||||
|
print(json.dumps(result)); s.close(); d.close()
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return json.loads(
|
||||||
|
command(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"run",
|
||||||
|
"--rm",
|
||||||
|
"--no-deps",
|
||||||
|
"-T",
|
||||||
|
"reconciler",
|
||||||
|
"python",
|
||||||
|
"-",
|
||||||
|
],
|
||||||
|
input=code,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--output", default="evidence/cost-repeat.json")
|
||||||
|
args = parser.parse_args()
|
||||||
|
output = Path(args.output)
|
||||||
|
assert not output.exists()
|
||||||
|
output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
prefix = "objects/cost-" + uuid4().hex + "/"
|
||||||
|
result = {
|
||||||
|
"date": datetime.now(UTC).isoformat(),
|
||||||
|
"source_commit": command(["git", "rev-parse", "HEAD"]),
|
||||||
|
"complete": False,
|
||||||
|
"cells": [],
|
||||||
|
"warmups": [],
|
||||||
|
"method": "0/100/1100 extra fresh one-byte objects; one warmup then 3 repeats per dataset; real reconciler service .25 CPU/256MiB; grace3600; 8 PNG fixtures with deliberately aged PG metadata (2days); other runtime objects remain real",
|
||||||
|
"fixture_rows": 8,
|
||||||
|
"grace_seconds": 3600,
|
||||||
|
}
|
||||||
|
chat = None
|
||||||
|
ready()
|
||||||
|
compose("stop", "reconciler")
|
||||||
|
try:
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE, trust_env=False, timeout=20
|
||||||
|
) as client:
|
||||||
|
user, _, session = actor(client)
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=auth(session),
|
||||||
|
json={
|
||||||
|
"title": "Controlled inventory",
|
||||||
|
"member_ids": [user["id"]],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert r.status_code == 201
|
||||||
|
chat = r.json()["id"]
|
||||||
|
seed = f"""
|
||||||
|
from uuid import uuid4
|
||||||
|
data=bytes.fromhex({PNG.hex()!r})
|
||||||
|
for i in range(8):
|
||||||
|
identity=uuid4(); key={prefix!r}+'ready-'+str(i)
|
||||||
|
s.put(key=key,stream=io.BytesIO(data),size=len(data),content_type='image/png',sha256=hashlib.sha256(data).hexdigest())
|
||||||
|
with d.connection() as c:
|
||||||
|
c.execute("INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,state,uploader_instance,expires_at,created_at) VALUES(%s,%s,%s,'fixture.png','image/png',%s,%s,%s,'ready','cost-fixture',clock_timestamp()-interval '1 day',clock_timestamp()-interval '2 days')",(identity,{chat!r},{user["id"]!r},len(data),hashlib.sha256(data).hexdigest(),key))
|
||||||
|
print('true')
|
||||||
|
"""
|
||||||
|
inside(seed)
|
||||||
|
for count in [0, 100, 1100]:
|
||||||
|
if count:
|
||||||
|
start = 0 if count == 100 else 100
|
||||||
|
inside(
|
||||||
|
f"\nfor i in range({start},{count}):\n s.put(key={prefix!r}+'extra-'+str(i),stream=io.BytesIO(b'x'),size=1,content_type='application/octet-stream',sha256=hashlib.sha256(b'x').hexdigest())\nprint('true')"
|
||||||
|
)
|
||||||
|
result["warmups"].append({"extra_objects": count, **probe()})
|
||||||
|
for repeat in range(3):
|
||||||
|
cell = {
|
||||||
|
"extra_objects": count,
|
||||||
|
"repetition": repeat + 1,
|
||||||
|
**probe(),
|
||||||
|
}
|
||||||
|
result["cells"].append(cell)
|
||||||
|
assert cell["complete"] and not cell["applied"]
|
||||||
|
assert cell["ready_protected"] >= 8
|
||||||
|
controls = {
|
||||||
|
(v["rows_scanned"], v["head_requests"], v["bytes_read"])
|
||||||
|
for v in result["cells"]
|
||||||
|
}
|
||||||
|
assert len(controls) == 1, (
|
||||||
|
"Age boundary changed work; choose a fresh stand"
|
||||||
|
)
|
||||||
|
for count in [0, 100, 1100]:
|
||||||
|
assert (
|
||||||
|
len(
|
||||||
|
{
|
||||||
|
v["objects_scanned"]
|
||||||
|
for v in result["cells"]
|
||||||
|
if v["extra_objects"] == count
|
||||||
|
}
|
||||||
|
)
|
||||||
|
== 1
|
||||||
|
)
|
||||||
|
result["stable_rows_head_bytes"] = list(next(iter(controls)))
|
||||||
|
result["median_seconds"] = {
|
||||||
|
str(n): statistics.median(
|
||||||
|
v["duration_seconds"]
|
||||||
|
for v in result["cells"]
|
||||||
|
if v["extra_objects"] == n
|
||||||
|
)
|
||||||
|
for n in [0, 100, 1100]
|
||||||
|
}
|
||||||
|
result["complete"] = True
|
||||||
|
finally:
|
||||||
|
if chat:
|
||||||
|
sql(
|
||||||
|
"DELETE FROM attachments WHERE chat_id='"
|
||||||
|
+ chat
|
||||||
|
+ "' AND uploader_instance='cost-fixture'"
|
||||||
|
)
|
||||||
|
inside(
|
||||||
|
f"\nfor o in s.objects():\n if o.object_name.startswith({prefix!r}): s.delete(key=o.object_name)\nprint('true')"
|
||||||
|
)
|
||||||
|
compose("up", "-d", "--no-build", "--no-recreate", "reconciler")
|
||||||
|
output.write_text(
|
||||||
|
json.dumps(result, ensure_ascii=False, indent=2) + "\n"
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"PASS: controlled inventory cost, 9 measurements and constant metadata/HEAD work"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
"""Создаёт только отсутствующие локальные файлы; секреты не печатает."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import secrets
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
def create(path, value):
|
||||||
|
try:
|
||||||
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
|
||||||
|
except FileExistsError:
|
||||||
|
return
|
||||||
|
with os.fdopen(fd, "w") as stream:
|
||||||
|
stream.write(value + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
directory = ROOT / "secrets"
|
||||||
|
directory.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
pg = directory / "postgres_password"
|
||||||
|
cache = directory / "cache_password"
|
||||||
|
create(pg, secrets.token_urlsafe(32))
|
||||||
|
create(cache, secrets.token_urlsafe(32))
|
||||||
|
create(
|
||||||
|
directory / "database_url",
|
||||||
|
"postgresql://messenger:"
|
||||||
|
+ pg.read_text().strip()
|
||||||
|
+ "@postgres:5432/messenger",
|
||||||
|
)
|
||||||
|
create(
|
||||||
|
directory / "cache_url",
|
||||||
|
"redis://:" + cache.read_text().strip() + "@cache:6379/0",
|
||||||
|
)
|
||||||
|
create(
|
||||||
|
directory / "valkey.conf",
|
||||||
|
"bind 0.0.0.0\nprotected-mode yes\nport 6379\nmaxmemory 64mb\n"
|
||||||
|
'maxmemory-policy allkeys-lru\nsave ""\nappendonly no\nrequirepass '
|
||||||
|
+ cache.read_text().strip(),
|
||||||
|
)
|
||||||
|
create(directory / "s3_root_user", secrets.token_hex(10))
|
||||||
|
create(directory / "s3_root_password", secrets.token_urlsafe(32))
|
||||||
|
create(directory / "s3_access_key", secrets.token_hex(10))
|
||||||
|
create(directory / "s3_secret_key", secrets.token_urlsafe(32))
|
||||||
|
create(ROOT / ".env", (ROOT / ".env.example").read_text().rstrip())
|
||||||
|
print("Local configuration ready; existing files preserved")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
"""Явные операторские действия только над API текущего Compose-проекта."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
def command(*args):
|
||||||
|
return subprocess.check_output(args, cwd=ROOT, text=True).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def containers():
|
||||||
|
ids = command("docker", "compose", "ps", "-q", "api").split()
|
||||||
|
return json.loads(command("docker", "inspect", *ids)) if ids else []
|
||||||
|
|
||||||
|
|
||||||
|
def alias(cid):
|
||||||
|
code = (
|
||||||
|
"import urllib.request; r=urllib.request.urlopen("
|
||||||
|
"'http://127.0.0.1:8080/health/live',timeout=2); "
|
||||||
|
"print(r.headers['X-Instance-Id'])"
|
||||||
|
)
|
||||||
|
return command("docker", "exec", cid, "python", "-c", code)
|
||||||
|
|
||||||
|
|
||||||
|
def wait_count(count):
|
||||||
|
deadline = time.monotonic() + 60
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
data = containers()
|
||||||
|
if len(data) == count and all(
|
||||||
|
c["State"].get("Health", {}).get("Status") == "healthy"
|
||||||
|
for c in data
|
||||||
|
):
|
||||||
|
# DNS и проверки HAProxy должны успеть увидеть новый адрес.
|
||||||
|
time.sleep(3)
|
||||||
|
return
|
||||||
|
time.sleep(0.5)
|
||||||
|
raise TimeoutError("Replica readiness")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
action = sys.argv[1]
|
||||||
|
data = containers()
|
||||||
|
if action == "stop":
|
||||||
|
target = os.environ["TARGET_INSTANCE_ID"]
|
||||||
|
matches = [c for c in data if alias(c["Id"]) == target]
|
||||||
|
if len(matches) != 1 or len(data) < 2:
|
||||||
|
raise RuntimeError("Target must identify one running replica")
|
||||||
|
command("docker", "stop", "--time", "10", matches[0]["Id"])
|
||||||
|
print("Selected API stopped")
|
||||||
|
elif action == "rolling":
|
||||||
|
count = len(data)
|
||||||
|
if count < 2:
|
||||||
|
raise RuntimeError("Need at least two replicas")
|
||||||
|
# Сначала добавляется готовая реплика, затем удаляется одна старая.
|
||||||
|
for old in data:
|
||||||
|
command(
|
||||||
|
"docker",
|
||||||
|
"compose",
|
||||||
|
"up",
|
||||||
|
"-d",
|
||||||
|
"--no-deps",
|
||||||
|
"--no-recreate",
|
||||||
|
"--scale",
|
||||||
|
f"api={count + 1}",
|
||||||
|
"api",
|
||||||
|
)
|
||||||
|
wait_count(count + 1)
|
||||||
|
command("docker", "stop", "--time", "10", old["Id"])
|
||||||
|
command("docker", "rm", old["Id"])
|
||||||
|
wait_count(count)
|
||||||
|
print("Sequential replacement completed")
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown action")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Executable
+4
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
docker compose restart api postgres cache s3 lb
|
||||||
|
docker compose up -d --no-build --no-recreate --wait
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
exec python3 scripts/cluster.py rolling
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8084")
|
||||||
|
|
||||||
|
|
||||||
|
def command(args, *, input=None):
|
||||||
|
result = subprocess.run(
|
||||||
|
args, cwd=ROOT, input=input, capture_output=True, text=True
|
||||||
|
)
|
||||||
|
if result.returncode:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Command failed: "
|
||||||
|
+ args[0]
|
||||||
|
+ "; "
|
||||||
|
+ result.stderr.replace(str(ROOT), "<repository>")
|
||||||
|
)
|
||||||
|
return result.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def compose(*args):
|
||||||
|
return command(["docker", "compose", *args])
|
||||||
|
|
||||||
|
|
||||||
|
def sql(query, *, database="messenger"):
|
||||||
|
return compose(
|
||||||
|
"exec",
|
||||||
|
"-T",
|
||||||
|
"postgres",
|
||||||
|
"psql",
|
||||||
|
"-U",
|
||||||
|
"messenger",
|
||||||
|
"-d",
|
||||||
|
database,
|
||||||
|
"-A",
|
||||||
|
"-t",
|
||||||
|
"-v",
|
||||||
|
"ON_ERROR_STOP=1",
|
||||||
|
"-c",
|
||||||
|
query,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def cache(command_name, *args):
|
||||||
|
script = (
|
||||||
|
'export REDISCLI_AUTH="$(cat /run/secrets/cache_password)"; '
|
||||||
|
'exec valkey-cli "$@"'
|
||||||
|
)
|
||||||
|
return compose(
|
||||||
|
"exec", "-T", "cache", "sh", "-c", script, "sh", command_name, *args
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def ready(base=BASE, *, timeout=30):
|
||||||
|
end = time.monotonic() + timeout
|
||||||
|
with httpx.Client(base_url=base, trust_env=False, timeout=6) as client:
|
||||||
|
while time.monotonic() < end:
|
||||||
|
try:
|
||||||
|
if client.get("/health/ready").status_code == 200:
|
||||||
|
return
|
||||||
|
except httpx.HTTPError:
|
||||||
|
pass
|
||||||
|
time.sleep(0.2)
|
||||||
|
raise TimeoutError("API readiness")
|
||||||
|
|
||||||
|
|
||||||
|
def actor(client):
|
||||||
|
username = "verify_" + uuid4().hex[:20]
|
||||||
|
password = "Synthetic-" + uuid4().hex + ":🌍"
|
||||||
|
result = client.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Synthetic",
|
||||||
|
"password": password,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert result.status_code == 201
|
||||||
|
user = result.json()
|
||||||
|
session = client.post(
|
||||||
|
"/api/v1/auth/sessions", auth=httpx.BasicAuth(username, password)
|
||||||
|
)
|
||||||
|
assert session.status_code == 201
|
||||||
|
return user, password, session.json()
|
||||||
|
|
||||||
|
|
||||||
|
def auth(session):
|
||||||
|
return {"Authorization": "Bearer " + session["token"]}
|
||||||
|
|
||||||
|
|
||||||
|
def extra_api(*, ttl=3600, pool_size=1):
|
||||||
|
name = "mtusi-lab4-test-" + uuid4().hex[:10]
|
||||||
|
compose(
|
||||||
|
"run",
|
||||||
|
"-d",
|
||||||
|
"--no-deps",
|
||||||
|
"--name",
|
||||||
|
name,
|
||||||
|
"-e",
|
||||||
|
"SESSION_TTL_SECONDS=" + str(ttl),
|
||||||
|
"-e",
|
||||||
|
"DB_POOL_SIZE=" + str(pool_size),
|
||||||
|
"-p",
|
||||||
|
"127.0.0.1::8080",
|
||||||
|
"api",
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
info = json.loads(command(["docker", "inspect", name]))[0]
|
||||||
|
base = (
|
||||||
|
"http://127.0.0.1:"
|
||||||
|
+ info["NetworkSettings"]["Ports"]["8080/tcp"][0]["HostPort"]
|
||||||
|
)
|
||||||
|
ready(base)
|
||||||
|
return name, base
|
||||||
|
except Exception:
|
||||||
|
command(["docker", "rm", "-f", name])
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def instances():
|
||||||
|
data = {}
|
||||||
|
for role in ["api", "postgres", "cache"]:
|
||||||
|
cid = compose("ps", "-q", role)
|
||||||
|
info = json.loads(command(["docker", "inspect", cid]))[0]
|
||||||
|
data[role] = {
|
||||||
|
"container": cid[:12],
|
||||||
|
"pid": info["State"]["Pid"],
|
||||||
|
"started_at": info["State"]["StartedAt"],
|
||||||
|
}
|
||||||
|
return data
|
||||||
Executable
+3
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
exec python3 scripts/cluster.py stop
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
import argparse
|
||||||
|
import atexit
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
PROGRESS = {}
|
||||||
|
|
||||||
|
|
||||||
|
def save_incomplete():
|
||||||
|
receipt = PROGRESS.get("receipt")
|
||||||
|
if receipt and receipt.get("result") != "PASS":
|
||||||
|
target = PROGRESS["output"].with_name(
|
||||||
|
PROGRESS["output"].stem + "-failed.json"
|
||||||
|
)
|
||||||
|
if target.exists():
|
||||||
|
target = target.with_name(
|
||||||
|
target.stem + "-" + uuid4().hex[:8] + target.suffix
|
||||||
|
)
|
||||||
|
target.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
target.write_text(
|
||||||
|
json.dumps(
|
||||||
|
{**receipt, "result": "INCOMPLETE"},
|
||||||
|
ensure_ascii=False,
|
||||||
|
indent=2,
|
||||||
|
)
|
||||||
|
+ "\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
atexit.register(save_incomplete)
|
||||||
|
|
||||||
|
|
||||||
|
def run(args, *, cwd, env, expected=0):
|
||||||
|
result = subprocess.run(
|
||||||
|
args, cwd=cwd, env=env, capture_output=True, text=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def redact(text):
|
||||||
|
return (
|
||||||
|
text.replace(str(cwd), "<clean-clone>")
|
||||||
|
.replace(str(ROOT), "<repository>")
|
||||||
|
.replace(str(Path.home()), "<home>")
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.returncode != expected:
|
||||||
|
if "receipt" in PROGRESS:
|
||||||
|
PROGRESS["receipt"]["failed_action"] = redact(" ".join(args))
|
||||||
|
raise RuntimeError(redact(result.stdout + result.stderr))
|
||||||
|
return redact(result.stdout.strip())
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--output", default="evidence/clean-clone.json")
|
||||||
|
args = parser.parse_args()
|
||||||
|
output = ROOT / args.output
|
||||||
|
if output.exists():
|
||||||
|
raise SystemExit("Choose new output path")
|
||||||
|
assert not subprocess.check_output(
|
||||||
|
["git", "status", "--porcelain"], cwd=ROOT
|
||||||
|
)
|
||||||
|
sha = subprocess.check_output(
|
||||||
|
["git", "rev-parse", "HEAD"], cwd=ROOT, text=True
|
||||||
|
).strip()
|
||||||
|
identity = uuid4().hex[:10]
|
||||||
|
project = "mtusi-lab4-clean-" + identity
|
||||||
|
with socket.socket() as reservation:
|
||||||
|
reservation.bind(("127.0.0.1", 0))
|
||||||
|
public_port = str(reservation.getsockname()[1])
|
||||||
|
env = {
|
||||||
|
**os.environ,
|
||||||
|
"COMPOSE_PROJECT_NAME": project,
|
||||||
|
"API_IMAGE": "mtusi-messenger:lab4-clean-" + identity,
|
||||||
|
"PUBLIC_PORT": public_port,
|
||||||
|
"VCS_REF": sha,
|
||||||
|
"NO_PROXY": "localhost,127.0.0.1",
|
||||||
|
"no_proxy": "localhost,127.0.0.1",
|
||||||
|
}
|
||||||
|
receipt = {
|
||||||
|
"date": datetime.now(UTC).isoformat(),
|
||||||
|
"source_commit": sha,
|
||||||
|
"fresh_secrets_environment_and_volumes": True,
|
||||||
|
"command": "python scripts/verify_clean.py",
|
||||||
|
"fixed_host_port_for_restart": int(public_port),
|
||||||
|
}
|
||||||
|
PROGRESS.update(receipt=receipt, output=output)
|
||||||
|
with tempfile.TemporaryDirectory(prefix="mtusi-lab4-clean-") as temporary:
|
||||||
|
clone = Path(temporary) / "lab4"
|
||||||
|
run(
|
||||||
|
[
|
||||||
|
"git",
|
||||||
|
"clone",
|
||||||
|
"--no-hardlinks",
|
||||||
|
"--branch",
|
||||||
|
"solution/bvt2301-maslov",
|
||||||
|
str(ROOT),
|
||||||
|
str(clone),
|
||||||
|
],
|
||||||
|
cwd=ROOT,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
for name in [".env", ".venv", "secrets"]:
|
||||||
|
assert not (clone / name).exists()
|
||||||
|
run(["uv", "sync", "--locked"], cwd=clone, env=env)
|
||||||
|
receipt["static_checks"] = run(
|
||||||
|
["make", "check", "validate", "lint", "PYTHON=.venv/bin/python"],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
run(["make", "bootstrap"], cwd=clone, env=env)
|
||||||
|
try:
|
||||||
|
run(
|
||||||
|
["docker", "compose", "up", "-d", "--build", "--wait"],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
env["BASE_URL"] = "http://127.0.0.1:" + public_port
|
||||||
|
receipt["smoke"] = run(
|
||||||
|
["make", "test", "BASE_URL=" + env["BASE_URL"]],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
receipt["http_tests"] = run(
|
||||||
|
[".venv/bin/pytest", "student-tests", "-q"], cwd=clone, env=env
|
||||||
|
)
|
||||||
|
run(
|
||||||
|
[
|
||||||
|
".venv/bin/python",
|
||||||
|
"-m",
|
||||||
|
"scripts.verify_cluster",
|
||||||
|
"--output",
|
||||||
|
"evidence/clean-cluster.json",
|
||||||
|
],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
run(
|
||||||
|
[
|
||||||
|
".venv/bin/python",
|
||||||
|
"-m",
|
||||||
|
"scripts.verify_storage",
|
||||||
|
"--output",
|
||||||
|
"evidence/clean-storage.json",
|
||||||
|
],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
receipt["storage"] = json.loads(
|
||||||
|
(clone / "evidence/clean-storage.json").read_text()
|
||||||
|
)
|
||||||
|
receipt["cluster"] = json.loads(
|
||||||
|
(clone / "evidence/clean-cluster.json").read_text()
|
||||||
|
)
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
p.name: hashlib.sha256(p.read_bytes()).hexdigest()
|
||||||
|
for p in (clone / "src").glob("*.py")
|
||||||
|
}
|
||||||
|
ids = run(
|
||||||
|
["docker", "compose", "ps", "-q", "api"], cwd=clone, env=env
|
||||||
|
).split()
|
||||||
|
assert len(ids) == 2
|
||||||
|
code = (
|
||||||
|
"import hashlib,json,pathlib; print(json.dumps({p.name:"
|
||||||
|
"hashlib.sha256(p.read_bytes()).hexdigest() for p in pathlib.Path('/app/src').glob('*.py')}))"
|
||||||
|
)
|
||||||
|
for cid in ids:
|
||||||
|
actual = json.loads(
|
||||||
|
run(
|
||||||
|
["docker", "exec", cid, "python", "-c", code],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert actual == expected
|
||||||
|
info = json.loads(
|
||||||
|
run(["docker", "inspect", cid], cwd=clone, env=env)
|
||||||
|
)[0]
|
||||||
|
assert (
|
||||||
|
info["Config"]["Labels"][
|
||||||
|
"org.opencontainers.image.revision"
|
||||||
|
]
|
||||||
|
== sha
|
||||||
|
)
|
||||||
|
assert not info["HostConfig"]["PortBindings"]
|
||||||
|
receipt["both_replicas_runtime_hashes_and_revision_match"] = True
|
||||||
|
receipt["fsck"] = run(
|
||||||
|
["git", "fsck", "--full"], cwd=clone, env=env
|
||||||
|
)
|
||||||
|
receipt["checks_completed"] = True
|
||||||
|
finally:
|
||||||
|
assert project.startswith("mtusi-lab4-clean-")
|
||||||
|
run(
|
||||||
|
["docker", "compose", "down", "--volumes", "--remove-orphans"],
|
||||||
|
cwd=clone,
|
||||||
|
env=env,
|
||||||
|
)
|
||||||
|
receipt["result"] = "PASS"
|
||||||
|
output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
output.write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + "\n")
|
||||||
|
print(
|
||||||
|
"PASS: fresh clone, build, HTTP tests, failover, rolling, persistence and runtime hashes"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,342 @@
|
|||||||
|
"""Проверки реальных процессов, failover, rolling и общей идемпотентности."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from scripts import cluster
|
||||||
|
from scripts.stand import BASE, ROOT, actor, auth, command, compose, ready, sql
|
||||||
|
|
||||||
|
|
||||||
|
def snapshot():
|
||||||
|
ids = compose("ps", "-q").split()
|
||||||
|
result = []
|
||||||
|
for c in json.loads(command(["docker", "inspect", *ids])):
|
||||||
|
role = c["Config"]["Labels"]["com.docker.compose.service"]
|
||||||
|
entry = {
|
||||||
|
"role": role,
|
||||||
|
"container": c["Id"][:12],
|
||||||
|
"pid": c["State"]["Pid"],
|
||||||
|
"started_at": c["State"]["StartedAt"],
|
||||||
|
"ports": c["HostConfig"]["PortBindings"],
|
||||||
|
"cpu_nano": c["HostConfig"]["NanoCpus"],
|
||||||
|
"memory_limit": c["HostConfig"]["Memory"],
|
||||||
|
}
|
||||||
|
if role == "api":
|
||||||
|
entry["instance"] = cluster.alias(c["Id"])
|
||||||
|
assert not entry["ports"]
|
||||||
|
result.append(entry)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def exercise(client, session, chat, action, *, seconds=15):
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
observations, confirmed, errors, reads = [], {}, 0, 0
|
||||||
|
started = time.monotonic()
|
||||||
|
finished = threading.Event()
|
||||||
|
action_result = {}
|
||||||
|
|
||||||
|
def operate():
|
||||||
|
time.sleep(0.5)
|
||||||
|
try:
|
||||||
|
action()
|
||||||
|
except BaseException as exc:
|
||||||
|
action_result["error"] = type(exc).__name__
|
||||||
|
finally:
|
||||||
|
action_result["finished"] = round(time.monotonic() - started, 3)
|
||||||
|
finished.set()
|
||||||
|
|
||||||
|
thread = threading.Thread(target=operate)
|
||||||
|
thread.start()
|
||||||
|
key = uuid4().hex
|
||||||
|
try:
|
||||||
|
while time.monotonic() - started < seconds or not finished.is_set():
|
||||||
|
h = {**auth(session), "Idempotency-Key": key}
|
||||||
|
try:
|
||||||
|
r = client.post(path, headers=h, json={"text": key})
|
||||||
|
if r.status_code != 201:
|
||||||
|
errors += 1
|
||||||
|
continue
|
||||||
|
m = r.json()
|
||||||
|
confirmed[m["id"]] = m
|
||||||
|
observations.append(
|
||||||
|
{
|
||||||
|
"at": round(time.monotonic() - started, 3),
|
||||||
|
"instance": r.headers["x-instance-id"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
key = uuid4().hex
|
||||||
|
read = client.get(
|
||||||
|
path, headers=auth(session), params={"limit": 100}
|
||||||
|
)
|
||||||
|
reads += read.status_code == 200
|
||||||
|
errors += read.status_code != 200
|
||||||
|
except httpx.HTTPError:
|
||||||
|
errors += 1
|
||||||
|
time.sleep(0.04)
|
||||||
|
finally:
|
||||||
|
thread.join(timeout=90)
|
||||||
|
assert not action_result.get("error"), action_result
|
||||||
|
found, cursor = [], None
|
||||||
|
while True:
|
||||||
|
r = client.get(
|
||||||
|
path,
|
||||||
|
headers=auth(session),
|
||||||
|
params={"limit": 100, **({"cursor": cursor} if cursor else {})},
|
||||||
|
)
|
||||||
|
assert r.status_code == 200
|
||||||
|
page = r.json()
|
||||||
|
found.extend(page["items"])
|
||||||
|
cursor = page["next_cursor"]
|
||||||
|
if not cursor:
|
||||||
|
break
|
||||||
|
by_id = {m["id"]: m for m in found}
|
||||||
|
assert all(by_id.get(k) == v for k, v in confirmed.items())
|
||||||
|
texts = [m["text"] for m in found]
|
||||||
|
assert len(texts) == len(set(texts))
|
||||||
|
recovery = next(
|
||||||
|
(
|
||||||
|
x["at"]
|
||||||
|
for x in observations
|
||||||
|
if x["at"] >= action_result["finished"]
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
assert recovery is not None and (seconds > 20 or recovery <= 10)
|
||||||
|
elapsed = time.monotonic() - started
|
||||||
|
return {
|
||||||
|
"confirmed_writes": len(confirmed),
|
||||||
|
"successful_reads": reads,
|
||||||
|
"errors": errors,
|
||||||
|
"successful_rps": round((len(confirmed) + reads) / elapsed, 2),
|
||||||
|
"first_success_after_action_seconds": recovery,
|
||||||
|
"action": action_result,
|
||||||
|
"observations": observations,
|
||||||
|
"confirmed_preserved": True,
|
||||||
|
"duplicate_texts": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--output", default="evidence/cluster-repeat.json")
|
||||||
|
args = parser.parse_args()
|
||||||
|
report = {
|
||||||
|
"date": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"source_commit": command(["git", "rev-parse", "HEAD"]),
|
||||||
|
"complete": False,
|
||||||
|
}
|
||||||
|
output = Path(args.output)
|
||||||
|
output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
try:
|
||||||
|
ready()
|
||||||
|
initial = snapshot()
|
||||||
|
apis = [c for c in initial if c["role"] == "api"]
|
||||||
|
assert len(apis) == 2 and len({c["pid"] for c in apis}) == 2
|
||||||
|
report["before"] = initial
|
||||||
|
with httpx.Client(base_url=BASE, trust_env=False, timeout=8) as client:
|
||||||
|
user, _, session = actor(client)
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=auth(session),
|
||||||
|
json={
|
||||||
|
"title": "Cluster verification",
|
||||||
|
"member_ids": [user["id"]],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert r.status_code == 201
|
||||||
|
chat = r.json()
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
key = uuid4().hex
|
||||||
|
|
||||||
|
def repeat(_):
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE, trust_env=False, timeout=8
|
||||||
|
) as c:
|
||||||
|
r = c.post(
|
||||||
|
path,
|
||||||
|
headers={**auth(session), "Idempotency-Key": key},
|
||||||
|
json={"text": "Same concurrent request"},
|
||||||
|
)
|
||||||
|
assert r.status_code == 201
|
||||||
|
return r.json(), r.headers["x-instance-id"]
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
repeated = list(pool.map(repeat, range(30)))
|
||||||
|
assert len({m[0]["id"] for m in repeated}) == 1
|
||||||
|
assert len({m[1] for m in repeated}) == 2
|
||||||
|
report["parallel_replay"] = {
|
||||||
|
"requests": 30,
|
||||||
|
"messages": 1,
|
||||||
|
"instances": sorted({m[1] for m in repeated}),
|
||||||
|
}
|
||||||
|
# Сбой второй записи транзакции не должен оставить сообщение.
|
||||||
|
fault_key = uuid4().hex
|
||||||
|
fault_text = "Atomic rollback " + fault_key
|
||||||
|
sql(
|
||||||
|
"CREATE FUNCTION student_idempo_fault() RETURNS trigger "
|
||||||
|
"LANGUAGE plpgsql AS $$ BEGIN IF NEW.key='"
|
||||||
|
+ fault_key
|
||||||
|
+ "' THEN RAISE EXCEPTION 'synthetic'; END IF; RETURN NEW; END $$; "
|
||||||
|
"CREATE TRIGGER student_idempo_fault BEFORE INSERT ON idempotency "
|
||||||
|
"FOR EACH ROW EXECUTE FUNCTION student_idempo_fault()"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
failed = client.post(
|
||||||
|
path,
|
||||||
|
headers={**auth(session), "Idempotency-Key": fault_key},
|
||||||
|
json={"text": fault_text},
|
||||||
|
)
|
||||||
|
assert failed.status_code == 500
|
||||||
|
assert (
|
||||||
|
sql(
|
||||||
|
"SELECT count(*) FROM messages WHERE text='"
|
||||||
|
+ fault_text
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
== "0"
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
sql(
|
||||||
|
"DROP TRIGGER student_idempo_fault ON idempotency; "
|
||||||
|
"DROP FUNCTION student_idempo_fault()"
|
||||||
|
)
|
||||||
|
retried = client.post(
|
||||||
|
path,
|
||||||
|
headers={**auth(session), "Idempotency-Key": fault_key},
|
||||||
|
json={"text": fault_text},
|
||||||
|
)
|
||||||
|
assert retried.status_code == 201
|
||||||
|
report["atomic_rollback_and_5xx_retry"] = True
|
||||||
|
sql(
|
||||||
|
"UPDATE idempotency SET expires_at=clock_timestamp()-interval '1 second' "
|
||||||
|
"WHERE key='" + fault_key + "'"
|
||||||
|
)
|
||||||
|
expired = client.post(
|
||||||
|
path,
|
||||||
|
headers={**auth(session), "Idempotency-Key": fault_key},
|
||||||
|
json={"text": "New retention window"},
|
||||||
|
)
|
||||||
|
assert expired.status_code == 201
|
||||||
|
assert expired.json()["id"] != retried.json()["id"]
|
||||||
|
report["expired_key_can_be_reused"] = True
|
||||||
|
# Отдельный чат каждой серии: проверки дубликатов не смешивают тексты.
|
||||||
|
report["failovers"] = []
|
||||||
|
for api in apis:
|
||||||
|
|
||||||
|
def stop(target=api["instance"]):
|
||||||
|
env = {**os.environ, "TARGET_INSTANCE_ID": target}
|
||||||
|
subprocess.run(
|
||||||
|
["scripts/stop-one.sh"],
|
||||||
|
cwd=ROOT,
|
||||||
|
env=env,
|
||||||
|
check=True,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=auth(session),
|
||||||
|
json={"title": "Failover", "member_ids": [user["id"]]},
|
||||||
|
)
|
||||||
|
result = exercise(client, session, r.json(), stop)
|
||||||
|
assert api["instance"] not in [
|
||||||
|
o["instance"] for o in result["observations"][-5:]
|
||||||
|
]
|
||||||
|
assert (
|
||||||
|
command(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"inspect",
|
||||||
|
"-f",
|
||||||
|
"{{.State.Running}}",
|
||||||
|
api["container"],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
== "false"
|
||||||
|
)
|
||||||
|
result["stopped"] = api
|
||||||
|
report["failovers"].append(result)
|
||||||
|
compose("up", "-d", "--no-build", "--wait")
|
||||||
|
ready()
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=auth(session),
|
||||||
|
json={"title": "Rolling", "member_ids": [user["id"]]},
|
||||||
|
)
|
||||||
|
before_roll = snapshot()
|
||||||
|
report["rolling"] = exercise(
|
||||||
|
client,
|
||||||
|
session,
|
||||||
|
r.json(),
|
||||||
|
lambda: command(["scripts/rolling-update.sh"]),
|
||||||
|
seconds=45,
|
||||||
|
)
|
||||||
|
after_roll = snapshot()
|
||||||
|
assert not (
|
||||||
|
{c["container"] for c in before_roll if c["role"] == "api"}
|
||||||
|
& {c["container"] for c in after_roll if c["role"] == "api"}
|
||||||
|
)
|
||||||
|
report["rolling"]["before"] = before_roll
|
||||||
|
report["rolling"]["after"] = after_roll
|
||||||
|
replay = repeat(0)[0]
|
||||||
|
assert replay == repeated[0][0]
|
||||||
|
report["replay_after_replacement"] = True
|
||||||
|
before = snapshot()
|
||||||
|
env = {
|
||||||
|
**os.environ,
|
||||||
|
"NO_PROXY": "localhost,127.0.0.1",
|
||||||
|
"no_proxy": "localhost,127.0.0.1",
|
||||||
|
}
|
||||||
|
for mode, hook in [
|
||||||
|
("failover", "scripts/stop-one.sh"),
|
||||||
|
("persistence", "scripts/restart.sh"),
|
||||||
|
]:
|
||||||
|
if mode == "persistence":
|
||||||
|
compose("up", "-d", "--no-build", "--wait")
|
||||||
|
ready()
|
||||||
|
before = snapshot()
|
||||||
|
run = subprocess.run(
|
||||||
|
[
|
||||||
|
".venv/bin/python",
|
||||||
|
"tests/resilience.py",
|
||||||
|
"--mode",
|
||||||
|
mode,
|
||||||
|
"--action-script",
|
||||||
|
hook,
|
||||||
|
"--base-url",
|
||||||
|
BASE,
|
||||||
|
],
|
||||||
|
cwd=ROOT,
|
||||||
|
env=env,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
assert run.returncode == 0, run.stdout + run.stderr
|
||||||
|
report["public_" + mode] = run.stdout.strip()
|
||||||
|
after = snapshot()
|
||||||
|
for role in ["postgres", "cache", "s3", "lb"]:
|
||||||
|
old = next(c for c in before if c["role"] == role)
|
||||||
|
new = next(c for c in after if c["role"] == role)
|
||||||
|
assert old["started_at"] != new["started_at"]
|
||||||
|
report["after_persistence"] = after
|
||||||
|
report["complete"] = True
|
||||||
|
finally:
|
||||||
|
output.write_text(
|
||||||
|
json.dumps(report, ensure_ascii=False, indent=2) + "\n"
|
||||||
|
)
|
||||||
|
print("PASS: process failover, rolling, replay and full persistence")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,538 @@
|
|||||||
|
"""Operator checks on the disposable teaching stand, never production."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import socket
|
||||||
|
import statistics
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from scripts import cluster
|
||||||
|
from scripts.stand import BASE, ROOT, actor, auth, command, compose, ready, sql
|
||||||
|
|
||||||
|
PNG = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||||
|
PREFIX = """import hashlib,io,json,time,urllib.request,urllib.error
|
||||||
|
from src.object_store import ObjectStore,BUCKET
|
||||||
|
from src.database import Database
|
||||||
|
from src.settings import secret
|
||||||
|
from src.reconcile import reconcile
|
||||||
|
s=ObjectStore(); d=Database(url=secret('DATABASE_URL'),size=2); d.open()
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def inside(code):
|
||||||
|
containers = compose("ps", "-q", "api").split()
|
||||||
|
assert containers, "No running API in this project"
|
||||||
|
value = command(
|
||||||
|
["docker", "exec", "-i", containers[0], "python", "-"],
|
||||||
|
input=PREFIX + code + "\ns.close(); d.close()\n",
|
||||||
|
)
|
||||||
|
return json.loads(value)
|
||||||
|
|
||||||
|
|
||||||
|
def upload(client, session, chat, data=PNG, *, name=None):
|
||||||
|
return client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/attachments",
|
||||||
|
headers={**auth(session), "Content-Type": "image/png"},
|
||||||
|
params={"filename": name or uuid4().hex + ".png"},
|
||||||
|
content=data,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def state(identity):
|
||||||
|
return sql("SELECT state FROM attachments WHERE id='" + identity + "'")
|
||||||
|
|
||||||
|
|
||||||
|
def key(identity):
|
||||||
|
return "objects/" + identity + "/original"
|
||||||
|
|
||||||
|
|
||||||
|
def run_reconcile(apply=False):
|
||||||
|
return inside(
|
||||||
|
"print(json.dumps(reconcile(d,s,apply=" + str(apply) + ",grace=300)))"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def age(identity):
|
||||||
|
sql(
|
||||||
|
"UPDATE attachments SET created_at=clock_timestamp()-interval '301 seconds', "
|
||||||
|
"expires_at=clock_timestamp()-interval '180 seconds' WHERE id='"
|
||||||
|
+ identity
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def percentiles(values):
|
||||||
|
values = sorted(values)
|
||||||
|
return {
|
||||||
|
f"p{p}_ms": round(
|
||||||
|
values[min(len(values) - 1, int(p * len(values) / 100))], 3
|
||||||
|
)
|
||||||
|
for p in [50, 95, 99]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def concurrency(client, session, chat):
|
||||||
|
stream = io.BytesIO()
|
||||||
|
Image.effect_noise((1700, 1700), 100).convert("RGB").save(stream, "PNG")
|
||||||
|
image = stream.getvalue()
|
||||||
|
assert len(image) < 10 * 1024 * 1024
|
||||||
|
ids = compose("ps", "-q", "api").split()
|
||||||
|
samples, stop = [], threading.Event()
|
||||||
|
code = (
|
||||||
|
"import json,pathlib; p=pathlib.Path('/sys/fs/cgroup'); "
|
||||||
|
"print(json.dumps({k:int((p/k).read_text()) for k in ['memory.current','memory.peak']}))"
|
||||||
|
)
|
||||||
|
|
||||||
|
def sample():
|
||||||
|
while not stop.is_set():
|
||||||
|
for cid in ids:
|
||||||
|
samples.append(
|
||||||
|
{
|
||||||
|
"at": round(time.monotonic() - started, 3),
|
||||||
|
"container": cid[:12],
|
||||||
|
**json.loads(
|
||||||
|
command(
|
||||||
|
["docker", "exec", cid, "python", "-c", code]
|
||||||
|
)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
stop.wait(0.3)
|
||||||
|
|
||||||
|
barrier = threading.Barrier(10)
|
||||||
|
|
||||||
|
def one(_):
|
||||||
|
with httpx.Client(base_url=BASE, trust_env=False, timeout=60) as c:
|
||||||
|
barrier.wait(timeout=30)
|
||||||
|
begin = time.monotonic()
|
||||||
|
r = upload(c, session, chat, image)
|
||||||
|
latency = (time.monotonic() - begin) * 1000
|
||||||
|
assert r.status_code == 201, (r.status_code, r.text)
|
||||||
|
a = r.json()
|
||||||
|
assert a["sha256"] == hashlib.sha256(image).hexdigest()
|
||||||
|
data = c.get(
|
||||||
|
f"/api/v1/attachments/{a['id']}/content", headers=auth(session)
|
||||||
|
)
|
||||||
|
assert data.status_code == 200 and data.content == image
|
||||||
|
return {
|
||||||
|
"id": a["id"],
|
||||||
|
"latency_ms": latency,
|
||||||
|
"instance": r.headers["x-instance-id"],
|
||||||
|
}
|
||||||
|
|
||||||
|
cpu_before = {
|
||||||
|
cid: command(["docker", "exec", cid, "cat", "/sys/fs/cgroup/cpu.stat"])
|
||||||
|
for cid in ids
|
||||||
|
}
|
||||||
|
started = time.monotonic()
|
||||||
|
thread = threading.Thread(target=sample)
|
||||||
|
thread.start()
|
||||||
|
try:
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
results = list(pool.map(one, range(10)))
|
||||||
|
finally:
|
||||||
|
stop.set()
|
||||||
|
thread.join(timeout=30)
|
||||||
|
elapsed = time.monotonic() - started
|
||||||
|
assert samples
|
||||||
|
return {
|
||||||
|
"concurrency": 10,
|
||||||
|
"requests": 10,
|
||||||
|
"successes": len(results),
|
||||||
|
"errors": 0,
|
||||||
|
"bytes_per_image": len(image),
|
||||||
|
"pixels": 1700 * 1700,
|
||||||
|
"duration_seconds": round(elapsed, 3),
|
||||||
|
"successful_rps": round(10 / elapsed, 3),
|
||||||
|
"latency_scope": "upload only; duration includes verified downloads and memory sampler",
|
||||||
|
**percentiles([r["latency_ms"] for r in results]),
|
||||||
|
"cpu_before": cpu_before,
|
||||||
|
"cpu_after": {
|
||||||
|
cid: command(
|
||||||
|
["docker", "exec", cid, "cat", "/sys/fs/cgroup/cpu.stat"]
|
||||||
|
)
|
||||||
|
for cid in ids
|
||||||
|
},
|
||||||
|
"memory_samples": samples,
|
||||||
|
"results": results,
|
||||||
|
"window_max_memory_current": max(s["memory.current"] for s in samples),
|
||||||
|
"lifetime_memory_peak": max(s["memory.peak"] for s in samples),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--output", default="evidence/storage-repeat.json")
|
||||||
|
parser.add_argument("--research", action="store_true")
|
||||||
|
args = parser.parse_args()
|
||||||
|
output = Path(args.output)
|
||||||
|
assert not output.exists(), "Choose a fresh evidence path"
|
||||||
|
output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
result = {
|
||||||
|
"source_commit": command(["git", "rev-parse", "HEAD"]),
|
||||||
|
"date": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"complete": False,
|
||||||
|
}
|
||||||
|
ready()
|
||||||
|
from scripts.verify_cluster import snapshot
|
||||||
|
|
||||||
|
result["runtime"] = snapshot()
|
||||||
|
compose("stop", "reconciler")
|
||||||
|
orphan = "objects/verification-" + uuid4().hex + "/orphan"
|
||||||
|
fresh = "objects/verification-" + uuid4().hex + "/fresh"
|
||||||
|
research_prefix = "objects/research-" + uuid4().hex + "/"
|
||||||
|
try:
|
||||||
|
# Its server LastModified must really age; no clock manipulation in S3.
|
||||||
|
inside(
|
||||||
|
f"s.put(key={orphan!r},stream=io.BytesIO(b'orphan'),size=6,content_type='application/octet-stream',sha256=hashlib.sha256(b'orphan').hexdigest()); print('true')"
|
||||||
|
)
|
||||||
|
orphan_started = time.monotonic()
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE, trust_env=False, timeout=30
|
||||||
|
) as client:
|
||||||
|
user, _, session = actor(client)
|
||||||
|
r = client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=auth(session),
|
||||||
|
json={
|
||||||
|
"title": "Storage verification",
|
||||||
|
"member_ids": [user["id"]],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert r.status_code == 201
|
||||||
|
chat = r.json()
|
||||||
|
r = upload(client, session, chat)
|
||||||
|
assert r.status_code == 201
|
||||||
|
saved, uploader = r.json(), r.headers["x-instance-id"]
|
||||||
|
failed_old = upload(client, session, chat).json()
|
||||||
|
sql(
|
||||||
|
"UPDATE attachments SET state='failed',error_code='synthetic' WHERE id='"
|
||||||
|
+ failed_old["id"]
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
age(failed_old["id"])
|
||||||
|
grace_started = time.monotonic()
|
||||||
|
target = next(
|
||||||
|
c
|
||||||
|
for c in cluster.containers()
|
||||||
|
if cluster.alias(c["Id"]) == uploader
|
||||||
|
)
|
||||||
|
command(["docker", "stop", "--time", "10", target["Id"]])
|
||||||
|
try:
|
||||||
|
time.sleep(3)
|
||||||
|
download = client.get(
|
||||||
|
f"/api/v1/attachments/{saved['id']}/content",
|
||||||
|
headers=auth(session),
|
||||||
|
)
|
||||||
|
assert download.status_code == 200 and download.content == PNG
|
||||||
|
assert download.headers["x-instance-id"] != uploader
|
||||||
|
result["upload_replica_stopped_downloaded_elsewhere"] = {
|
||||||
|
"stopped_container": target["Id"][:12],
|
||||||
|
"uploader": uploader,
|
||||||
|
"downloader": download.headers["x-instance-id"],
|
||||||
|
"sha256": saved["sha256"],
|
||||||
|
}
|
||||||
|
finally:
|
||||||
|
compose(
|
||||||
|
"up",
|
||||||
|
"-d",
|
||||||
|
"--no-build",
|
||||||
|
"--no-recreate",
|
||||||
|
"--wait",
|
||||||
|
"api",
|
||||||
|
"lb",
|
||||||
|
)
|
||||||
|
ready()
|
||||||
|
private_code = f"""
|
||||||
|
from src.errors import DomainError
|
||||||
|
checks={{}}
|
||||||
|
try:
|
||||||
|
s.head(key='forbidden'); raise AssertionError('HEAD outside IAM prefix')
|
||||||
|
except DomainError as e:
|
||||||
|
assert e.status==503; checks['denied_head_is_not_missing']=e.status
|
||||||
|
try:
|
||||||
|
urllib.request.urlopen('http://s3:9000/messenger/{key(saved["id"])}',timeout=3)
|
||||||
|
raise AssertionError('public object')
|
||||||
|
except urllib.error.HTTPError as e: checks['anonymous_get']=e.code; assert e.code==403
|
||||||
|
for name,operation in [('list_other_bucket',lambda:list(s.client.list_objects('other-private'))),('create_bucket',lambda:s.client.make_bucket('forbidden-bucket')),('write_outside_prefix',lambda:s.client.put_object(BUCKET,'forbidden',io.BytesIO(b'x'),1))]:
|
||||||
|
try: operation(); raise AssertionError(name)
|
||||||
|
except Exception as e:
|
||||||
|
assert getattr(e,'code',None)=='AccessDenied',type(e).__name__; checks[name]='AccessDenied'
|
||||||
|
try:
|
||||||
|
urllib.request.urlopen('http://s3:9001/',timeout=2); raise AssertionError('console exposed')
|
||||||
|
except urllib.error.URLError: checks['console_internal_network']='unreachable'
|
||||||
|
print(json.dumps(checks))
|
||||||
|
"""
|
||||||
|
result["private_bucket_and_iam"] = inside(private_code)
|
||||||
|
compose("run", "--rm", "--no-deps", "s3-bootstrap")
|
||||||
|
assert (
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/attachments/{saved['id']}/content",
|
||||||
|
headers=auth(session),
|
||||||
|
).content
|
||||||
|
== PNG
|
||||||
|
)
|
||||||
|
result["bootstrap_repeat_preserved_original"] = True
|
||||||
|
# A partial HTTP body must never create a durable intent or object.
|
||||||
|
before = int(sql("SELECT count(*) FROM attachments"))
|
||||||
|
objects_before_abort = inside("print(len(list(s.objects())))")
|
||||||
|
parsed = httpx.URL(BASE)
|
||||||
|
with socket.create_connection(
|
||||||
|
(parsed.host, parsed.port), timeout=3
|
||||||
|
) as raw:
|
||||||
|
request = (
|
||||||
|
(
|
||||||
|
f"POST /api/v1/chats/{chat['id']}/attachments?filename=partial.png HTTP/1.1\r\n"
|
||||||
|
f"Host: {parsed.host}\r\nAuthorization: Bearer {session['token']}\r\n"
|
||||||
|
"Content-Type: image/png\r\nContent-Length: 10000000\r\n\r\n"
|
||||||
|
).encode()
|
||||||
|
+ PNG
|
||||||
|
+ b"x" * (2 * 1024 * 1024)
|
||||||
|
)
|
||||||
|
raw.sendall(request)
|
||||||
|
time.sleep(1)
|
||||||
|
assert int(sql("SELECT count(*) FROM attachments")) == before
|
||||||
|
assert (
|
||||||
|
inside("print(len(list(s.objects())))") == objects_before_abort
|
||||||
|
)
|
||||||
|
result["aborted_upload_has_no_intent_or_bytes"] = True
|
||||||
|
# Failure before any S3 PUT.
|
||||||
|
name = uuid4().hex + ".png"
|
||||||
|
objects_before = inside("print(len(list(s.objects())))")
|
||||||
|
sql(
|
||||||
|
"CREATE FUNCTION student_upload_fault() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'synthetic'; END $$; CREATE TRIGGER student_upload_fault BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION student_upload_fault()"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
assert (
|
||||||
|
upload(client, session, chat, name=name).status_code == 500
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
int(
|
||||||
|
sql(
|
||||||
|
"SELECT count(*) FROM attachments WHERE filename='"
|
||||||
|
+ name
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
== 0
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
inside("print(len(list(s.objects())))") == objects_before
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
sql(
|
||||||
|
"DROP TRIGGER student_upload_fault ON attachments; DROP FUNCTION student_upload_fault()"
|
||||||
|
)
|
||||||
|
result["pg_intent_failure_has_no_bytes"] = True
|
||||||
|
# S3 PUT succeeded, then ready UPDATE fails: durable pending is recoverable.
|
||||||
|
name = uuid4().hex + ".png"
|
||||||
|
sql(
|
||||||
|
"CREATE FUNCTION student_ready_fault() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.state='ready' THEN RAISE EXCEPTION 'synthetic'; END IF; RETURN NEW; END $$; CREATE TRIGGER student_ready_fault BEFORE UPDATE ON attachments FOR EACH ROW EXECUTE FUNCTION student_ready_fault()"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
assert (
|
||||||
|
upload(client, session, chat, name=name).status_code == 500
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
sql(
|
||||||
|
"DROP TRIGGER student_ready_fault ON attachments; DROP FUNCTION student_ready_fault()"
|
||||||
|
)
|
||||||
|
pending = sql(
|
||||||
|
"SELECT id FROM attachments WHERE filename='" + name + "'"
|
||||||
|
)
|
||||||
|
assert state(pending) == "pending"
|
||||||
|
assert inside(
|
||||||
|
f"print(json.dumps(s.head(key={key(pending)!r}) is not None))"
|
||||||
|
)
|
||||||
|
age(pending)
|
||||||
|
# Stop S3; bypass LB health exclusion only from inside the selected API.
|
||||||
|
compose("stop", "s3")
|
||||||
|
name = uuid4().hex + ".png"
|
||||||
|
try:
|
||||||
|
direct = f"""
|
||||||
|
request=urllib.request.Request('http://127.0.0.1:8080/api/v1/chats/{chat["id"]}/attachments?filename={name}',data=bytes.fromhex({PNG.hex()!r}),headers={{'Authorization':{auth(session)["Authorization"]!r},'Content-Type':'image/png'}})
|
||||||
|
try: urllib.request.urlopen(request,timeout=15); raise AssertionError('S3 down accepted')
|
||||||
|
except urllib.error.HTTPError as e: print(json.dumps({{'status':e.code,'error':json.loads(e.read())['error']['code']}}))
|
||||||
|
"""
|
||||||
|
observed = inside(direct)
|
||||||
|
assert observed == {"status": 503, "error": "unavailable"}
|
||||||
|
missing_pending = sql(
|
||||||
|
"SELECT id FROM attachments WHERE filename='" + name + "'"
|
||||||
|
)
|
||||||
|
assert state(missing_pending) == "pending"
|
||||||
|
result["s3_unavailable"] = observed
|
||||||
|
finally:
|
||||||
|
compose(
|
||||||
|
"up", "-d", "--no-build", "--no-recreate", "--wait", "s3"
|
||||||
|
)
|
||||||
|
ready()
|
||||||
|
age(missing_pending)
|
||||||
|
# Used object missing: preserve metadata ID and the Message link.
|
||||||
|
used = upload(client, session, chat).json()
|
||||||
|
sent = client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers={**auth(session), "Idempotency-Key": uuid4().hex},
|
||||||
|
json={"text": "", "attachment_ids": [used["id"]]},
|
||||||
|
)
|
||||||
|
assert sent.status_code == 201
|
||||||
|
inside(f"s.delete(key={key(used['id'])!r}); print('true')")
|
||||||
|
age(used["id"])
|
||||||
|
age(saved["id"])
|
||||||
|
# Fresh cgroups make memory.peak specific to this experiment.
|
||||||
|
compose("restart", "api")
|
||||||
|
cluster.wait_count(2)
|
||||||
|
ready()
|
||||||
|
result["concurrent_uploads"] = concurrency(client, session, chat)
|
||||||
|
result["concurrent_uploads"][
|
||||||
|
"api_restarted_before_memory_measurement"
|
||||||
|
] = True
|
||||||
|
while time.monotonic() - grace_started < 302:
|
||||||
|
print(
|
||||||
|
"Waiting for genuine S3 LastModified grace: "
|
||||||
|
+ str(round(302 - (time.monotonic() - grace_started)))
|
||||||
|
+ "s",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
time.sleep(
|
||||||
|
min(20, max(0, 302 - (time.monotonic() - grace_started)))
|
||||||
|
)
|
||||||
|
inside(
|
||||||
|
f"s.put(key={fresh!r},stream=io.BytesIO(b'fresh'),size=5,content_type='application/octet-stream',sha256=hashlib.sha256(b'fresh').hexdigest()); print('true')"
|
||||||
|
)
|
||||||
|
failed_fresh = upload(client, session, chat).json()
|
||||||
|
sql(
|
||||||
|
"UPDATE attachments SET state='failed',error_code='synthetic' WHERE id='"
|
||||||
|
+ failed_fresh["id"]
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
age(failed_fresh["id"])
|
||||||
|
before_states = [
|
||||||
|
state(i)
|
||||||
|
for i in [pending, missing_pending, used["id"], saved["id"]]
|
||||||
|
]
|
||||||
|
dry = run_reconcile()
|
||||||
|
assert [
|
||||||
|
state(i)
|
||||||
|
for i in [pending, missing_pending, used["id"], saved["id"]]
|
||||||
|
] == before_states
|
||||||
|
assert inside(
|
||||||
|
f"print(json.dumps(s.head(key={orphan!r}) is not None))"
|
||||||
|
)
|
||||||
|
for action in [
|
||||||
|
"recover_pending",
|
||||||
|
"fail_pending_missing",
|
||||||
|
"mark_missing",
|
||||||
|
"delete_orphan",
|
||||||
|
"delete_failed_object",
|
||||||
|
]:
|
||||||
|
assert dry["actions"].get(action, 0) >= 1, dry
|
||||||
|
applied = run_reconcile(True)
|
||||||
|
assert state(pending) == "ready"
|
||||||
|
assert (
|
||||||
|
state(missing_pending) == "failed"
|
||||||
|
and state(used["id"]) == "failed"
|
||||||
|
)
|
||||||
|
assert state(saved["id"]) == "ready"
|
||||||
|
assert (
|
||||||
|
int(
|
||||||
|
sql(
|
||||||
|
"SELECT count(*) FROM message_attachments WHERE attachment_id='"
|
||||||
|
+ used["id"]
|
||||||
|
+ "'"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
== 1
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/attachments/{used['id']}", headers=auth(session)
|
||||||
|
).status_code
|
||||||
|
== 409
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/attachments/{pending}/content",
|
||||||
|
headers=auth(session),
|
||||||
|
).content
|
||||||
|
== PNG
|
||||||
|
)
|
||||||
|
assert inside(
|
||||||
|
f"print(json.dumps(s.head(key={orphan!r}) is None and s.head(key={fresh!r}) is not None and s.head(key={key(saved['id'])!r}) is not None))"
|
||||||
|
)
|
||||||
|
assert inside(
|
||||||
|
f"print(json.dumps(s.head(key={key(failed_old['id'])!r}) is None and s.head(key={key(failed_fresh['id'])!r}) is not None))"
|
||||||
|
)
|
||||||
|
repeat = run_reconcile(True)
|
||||||
|
assert not repeat["applied"]
|
||||||
|
assert repeat["linked_protected"] >= 1
|
||||||
|
result["reconciliation"] = {
|
||||||
|
"dry_run": dry,
|
||||||
|
"apply": applied,
|
||||||
|
"repeat": repeat,
|
||||||
|
"s3_grace_wait_seconds": round(
|
||||||
|
time.monotonic() - orphan_started, 3
|
||||||
|
),
|
||||||
|
"fresh_and_ready_unlinked_protected": True,
|
||||||
|
"old_failed_unlinked_deleted_and_fresh_failed_protected": True,
|
||||||
|
"missing_used_id_and_link_preserved": True,
|
||||||
|
"dry_run_unchanged": True,
|
||||||
|
}
|
||||||
|
if args.research:
|
||||||
|
cells = []
|
||||||
|
for count in [0, 100, 1100]:
|
||||||
|
if count:
|
||||||
|
start = 0 if count == 100 else 100
|
||||||
|
inside(
|
||||||
|
f"\nfor i in range({start},{count}):\n s.put(key={research_prefix!r}+str(i),stream=io.BytesIO(b'x'),size=1,content_type='application/octet-stream',sha256=hashlib.sha256(b'x').hexdigest())\nprint('true')"
|
||||||
|
)
|
||||||
|
for repetition in range(3):
|
||||||
|
measured = run_reconcile()
|
||||||
|
cells.append(
|
||||||
|
{
|
||||||
|
"extra_objects": count,
|
||||||
|
"repetition": repetition + 1,
|
||||||
|
**measured,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result["cost_research"] = {
|
||||||
|
"cells": cells,
|
||||||
|
"repeats": 3,
|
||||||
|
"method": "same running stand; 0/100/1100 fresh one-byte objects; dry-run; serial ascending dataset; no warmup",
|
||||||
|
"median_seconds": {
|
||||||
|
str(n): statistics.median(
|
||||||
|
c["duration_seconds"]
|
||||||
|
for c in cells
|
||||||
|
if c["extra_objects"] == n
|
||||||
|
)
|
||||||
|
for n in [0, 100, 1100]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
result["complete"] = True
|
||||||
|
finally:
|
||||||
|
# Only keys created by this invocation; no broad bucket deletion.
|
||||||
|
try:
|
||||||
|
inside(
|
||||||
|
f"\nfor o in s.objects():\n if o.object_name.startswith({research_prefix!r}) or o.object_name in [{orphan!r},{fresh!r}]: s.delete(key=o.object_name)\nprint('true')"
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
compose("up", "-d", "--no-build", "--no-recreate", "reconciler")
|
||||||
|
output.write_text(
|
||||||
|
json.dumps(result, ensure_ascii=False, indent=2) + "\n"
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"PASS: private S3, failover, bounded concurrent uploads and two-store recovery"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def early_stop(signum, _frame):
|
||||||
|
print(
|
||||||
|
json.dumps({"event": "stopped_before_startup", "signal": signum}),
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
signal.signal(signal.SIGTERM, early_stop)
|
||||||
|
signal.signal(signal.SIGINT, early_stop)
|
||||||
|
|
||||||
|
import uvicorn # noqa: E402
|
||||||
|
|
||||||
|
from src.logging_config import JsonFormatter # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
host = os.environ.get("APP_HOST", "127.0.0.1")
|
||||||
|
port = int(os.environ.get("APP_PORT", "8080"))
|
||||||
|
if not 1 <= port <= 65535:
|
||||||
|
raise ValueError("APP_PORT must be between 1 and 65535")
|
||||||
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
|
handler.setFormatter(JsonFormatter())
|
||||||
|
logging.basicConfig(level=logging.INFO, handlers=[handler], force=True)
|
||||||
|
uvicorn.run(
|
||||||
|
"src.api:app",
|
||||||
|
host=host,
|
||||||
|
port=port,
|
||||||
|
workers=1,
|
||||||
|
access_log=False,
|
||||||
|
timeout_graceful_shutdown=5,
|
||||||
|
log_config=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
+424
@@ -0,0 +1,424 @@
|
|||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
from collections.abc import AsyncIterator
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
|
from urllib.parse import quote
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from anyio import Semaphore, fail_after, to_thread
|
||||||
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
from pydantic import ValidationError
|
||||||
|
from starlette.exceptions import HTTPException
|
||||||
|
from starlette.requests import ClientDisconnect
|
||||||
|
from starlette.responses import JSONResponse, Response, StreamingResponse
|
||||||
|
|
||||||
|
from src import images
|
||||||
|
from src.attachments import Attachments
|
||||||
|
from src.cache import UserCache
|
||||||
|
from src.cursor import parse_uuid
|
||||||
|
from src.database import Database
|
||||||
|
from src.domain import timestamp
|
||||||
|
from src.errors import DomainError, invalid
|
||||||
|
from src.object_store import ObjectStore
|
||||||
|
from src.password import Passwords
|
||||||
|
from src.repository import PostgresRepository
|
||||||
|
from src.schemas import CreateChat, CreateMessage, CreateUser, Input
|
||||||
|
from src.service import MessengerService
|
||||||
|
from src.settings import Settings
|
||||||
|
|
||||||
|
logger = logging.getLogger("messenger")
|
||||||
|
INSTANCE_ID = uuid4().hex
|
||||||
|
|
||||||
|
|
||||||
|
def response(data: object, *, status: int = 200) -> JSONResponse:
|
||||||
|
return JSONResponse(
|
||||||
|
jsonable_encoder(
|
||||||
|
data, custom_encoder={datetime: timestamp, UUID: str}
|
||||||
|
),
|
||||||
|
status_code=status,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def body(request: Request, model: type[Input]) -> Input:
|
||||||
|
try:
|
||||||
|
data = await request.body()
|
||||||
|
return model.model_validate(
|
||||||
|
json.loads(
|
||||||
|
data.decode("utf-8"),
|
||||||
|
parse_constant=lambda _: (_ for _ in ()).throw(ValueError()),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except ValueError, ValidationError, UnicodeError, RecursionError:
|
||||||
|
raise invalid() from None
|
||||||
|
|
||||||
|
|
||||||
|
def pagination(request: Request) -> tuple[int, str | None]:
|
||||||
|
limits = request.query_params.getlist("limit")
|
||||||
|
cursors = request.query_params.getlist("cursor")
|
||||||
|
if len(limits) > 1 or len(cursors) > 1:
|
||||||
|
raise invalid()
|
||||||
|
raw = limits[0] if limits else "50"
|
||||||
|
if not raw.isascii() or not raw.isdecimal():
|
||||||
|
raise invalid()
|
||||||
|
raw = raw.lstrip("0") or "0"
|
||||||
|
if len(raw) > 3:
|
||||||
|
raise invalid()
|
||||||
|
limit = int(raw)
|
||||||
|
if not 1 <= limit <= 100:
|
||||||
|
raise invalid()
|
||||||
|
return limit, cursors[0] if cursors else None
|
||||||
|
|
||||||
|
|
||||||
|
def service(request: Request) -> MessengerService:
|
||||||
|
return request.app.state.service
|
||||||
|
|
||||||
|
|
||||||
|
async def run_io(function, **kwargs):
|
||||||
|
return await to_thread.run_sync(partial(function, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
|
async def actor(request: Request):
|
||||||
|
return await run_io(
|
||||||
|
service(request).identify, value=request.headers.get("authorization")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||||
|
settings = Settings.load()
|
||||||
|
database = Database(url=settings.database_url, size=settings.pool_size)
|
||||||
|
cache = UserCache(url=settings.cache_url)
|
||||||
|
store = ObjectStore()
|
||||||
|
try:
|
||||||
|
await run_io(database.open)
|
||||||
|
passwords = await run_io(Passwords)
|
||||||
|
app.state.database = database
|
||||||
|
app.state.store = store
|
||||||
|
app.state.upload_slots = Semaphore(2)
|
||||||
|
app.state.attachments = Attachments(
|
||||||
|
database=database, store=store, instance=INSTANCE_ID
|
||||||
|
)
|
||||||
|
app.state.service = MessengerService(
|
||||||
|
repository=PostgresRepository(database=database, cache=cache),
|
||||||
|
passwords=passwords,
|
||||||
|
session_ttl=settings.session_ttl,
|
||||||
|
)
|
||||||
|
app.state.ready = True
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
app.state.ready = False
|
||||||
|
await run_io(store.close)
|
||||||
|
await run_io(cache.close)
|
||||||
|
await run_io(database.close)
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def request_context(request: Request, call_next):
|
||||||
|
request_id = str(uuid4())
|
||||||
|
request.state.request_id = request_id
|
||||||
|
started = time.perf_counter()
|
||||||
|
try:
|
||||||
|
result = await call_next(request)
|
||||||
|
except Exception:
|
||||||
|
logger.error(
|
||||||
|
json.dumps({"event": "internal_error", "request_id": request_id})
|
||||||
|
)
|
||||||
|
result = response(
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"code": "internal_error",
|
||||||
|
"message": "Internal error",
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
status=500,
|
||||||
|
)
|
||||||
|
result.headers["X-Request-Id"] = request_id
|
||||||
|
result.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
|
route = request.scope.get("route")
|
||||||
|
logger.info(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"event": "request",
|
||||||
|
"request_id": request_id,
|
||||||
|
"method": request.method,
|
||||||
|
"route": getattr(route, "path", "unmatched"),
|
||||||
|
"status": result.status_code,
|
||||||
|
"elapsed_ms": round((time.perf_counter() - started) * 1000, 3),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(DomainError)
|
||||||
|
async def domain_error(request: Request, error: DomainError):
|
||||||
|
result = response(
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"code": error.code,
|
||||||
|
"message": error.message,
|
||||||
|
"request_id": request.state.request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
status=error.status,
|
||||||
|
)
|
||||||
|
result.headers.update(error.headers)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@app.exception_handler(HTTPException)
|
||||||
|
async def http_error(request: Request, error: HTTPException):
|
||||||
|
code = "not_found" if error.status_code == 404 else "invalid_request"
|
||||||
|
return response(
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"code": code,
|
||||||
|
"message": "Resource not found"
|
||||||
|
if error.status_code == 404
|
||||||
|
else "Invalid request",
|
||||||
|
"request_id": request.state.request_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
status=error.status_code,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/live")
|
||||||
|
async def live():
|
||||||
|
return response({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health/ready")
|
||||||
|
async def ready():
|
||||||
|
if not getattr(app.state, "ready", False):
|
||||||
|
raise DomainError(503, "unavailable", "Not ready")
|
||||||
|
await run_io(app.state.database.ping)
|
||||||
|
await run_io(app.state.store.ping)
|
||||||
|
return response({"status": "ok"})
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/users")
|
||||||
|
async def register(request: Request):
|
||||||
|
data = await body(request, CreateUser)
|
||||||
|
return response(
|
||||||
|
await run_io(service(request).register, **data.model_dump()),
|
||||||
|
status=201,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/users/me")
|
||||||
|
async def current(request: Request):
|
||||||
|
return response(await actor(request))
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/chats")
|
||||||
|
async def create_chat(request: Request):
|
||||||
|
user = await actor(request)
|
||||||
|
data = await body(request, CreateChat)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).create_chat, actor_id=user.id, **data.model_dump()
|
||||||
|
),
|
||||||
|
status=201,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats")
|
||||||
|
async def list_chats(request: Request):
|
||||||
|
user = await actor(request)
|
||||||
|
limit, cursor = pagination(request)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).chats,
|
||||||
|
actor_id=user.id,
|
||||||
|
limit=limit,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats/{chat_id}")
|
||||||
|
async def get_chat(request: Request, chat_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).get_chat,
|
||||||
|
actor_id=user.id,
|
||||||
|
chat_id=parse_uuid(chat_id),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/chats/{chat_id}/messages")
|
||||||
|
async def send(request: Request, chat_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
cid = parse_uuid(chat_id)
|
||||||
|
await run_io(service(request).get_chat, actor_id=user.id, chat_id=cid)
|
||||||
|
data = await body(request, CreateMessage)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).send,
|
||||||
|
actor_id=user.id,
|
||||||
|
chat_id=cid,
|
||||||
|
idempotency_key=request.headers.get("idempotency-key"),
|
||||||
|
**data.model_dump(),
|
||||||
|
),
|
||||||
|
status=201,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||||
|
async def history(request: Request, chat_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
cid = parse_uuid(chat_id)
|
||||||
|
chat = await run_io(
|
||||||
|
service(request).get_chat, actor_id=user.id, chat_id=cid
|
||||||
|
)
|
||||||
|
limit, cursor = pagination(request)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).history,
|
||||||
|
actor_id=user.id,
|
||||||
|
chat=chat,
|
||||||
|
limit=limit,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/auth/sessions")
|
||||||
|
async def login(request: Request):
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
service(request).login,
|
||||||
|
authorization=request.headers.get("authorization"),
|
||||||
|
),
|
||||||
|
status=201,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete("/api/v1/auth/sessions/current")
|
||||||
|
async def logout(request: Request):
|
||||||
|
await run_io(
|
||||||
|
service(request).logout,
|
||||||
|
authorization=request.headers.get("authorization"),
|
||||||
|
)
|
||||||
|
return Response(status_code=204)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/v1/chats/{chat_id}/attachments")
|
||||||
|
async def upload(request: Request, chat_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
cid = parse_uuid(chat_id)
|
||||||
|
await run_io(service(request).get_chat, actor_id=user.id, chat_id=cid)
|
||||||
|
names = request.query_params.getlist("filename")
|
||||||
|
if len(names) != 1:
|
||||||
|
raise invalid()
|
||||||
|
name = images.filename(names[0])
|
||||||
|
declared = (
|
||||||
|
request.headers.get("content-type", "")
|
||||||
|
.partition(";")[0]
|
||||||
|
.strip()
|
||||||
|
.lower()
|
||||||
|
)
|
||||||
|
if declared not in {"image/png", "image/jpeg"}:
|
||||||
|
raise DomainError(
|
||||||
|
415, "unsupported_media_type", "PNG or JPEG required"
|
||||||
|
)
|
||||||
|
length = request.headers.get("content-length")
|
||||||
|
if length and (not length.isdecimal() or len(length) > 12):
|
||||||
|
raise invalid()
|
||||||
|
if length and int(length) > images.MAX_BYTES:
|
||||||
|
raise DomainError(413, "payload_too_large", "Byte limit exceeded")
|
||||||
|
try:
|
||||||
|
with fail_after(20):
|
||||||
|
async with app.state.upload_slots:
|
||||||
|
with tempfile.SpooledTemporaryFile(
|
||||||
|
max_size=1024 * 1024, mode="w+b"
|
||||||
|
) as stream:
|
||||||
|
size = 0
|
||||||
|
digest = hashlib.sha256()
|
||||||
|
async for chunk in request.stream():
|
||||||
|
size += len(chunk)
|
||||||
|
if size > images.MAX_BYTES:
|
||||||
|
raise DomainError(
|
||||||
|
413, "payload_too_large", "Byte limit exceeded"
|
||||||
|
)
|
||||||
|
digest.update(chunk)
|
||||||
|
stream.write(chunk)
|
||||||
|
await run_io(
|
||||||
|
images.verify, stream=stream, declared=declared
|
||||||
|
)
|
||||||
|
result = await run_io(
|
||||||
|
app.state.attachments.upload,
|
||||||
|
chat_id=cid,
|
||||||
|
owner_id=user.id,
|
||||||
|
filename=name,
|
||||||
|
content_type=declared,
|
||||||
|
size=size,
|
||||||
|
sha256=digest.hexdigest(),
|
||||||
|
stream=stream,
|
||||||
|
)
|
||||||
|
return response(result, status=201)
|
||||||
|
except ClientDisconnect:
|
||||||
|
raise invalid() from None
|
||||||
|
except TimeoutError:
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Upload deadline exceeded"
|
||||||
|
) from None
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/attachments/{attachment_id}")
|
||||||
|
async def attachment_metadata(request: Request, attachment_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
return response(
|
||||||
|
await run_io(
|
||||||
|
app.state.attachments.metadata,
|
||||||
|
identity=parse_uuid(attachment_id),
|
||||||
|
actor_id=user.id,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||||||
|
async def attachment_content(request: Request, attachment_id: str):
|
||||||
|
user = await actor(request)
|
||||||
|
variants = request.query_params.getlist("variant")
|
||||||
|
if len(variants) > 1:
|
||||||
|
raise invalid()
|
||||||
|
row, upstream = await run_io(
|
||||||
|
app.state.attachments.content,
|
||||||
|
identity=parse_uuid(attachment_id),
|
||||||
|
actor_id=user.id,
|
||||||
|
variant=variants[0] if variants else "original",
|
||||||
|
)
|
||||||
|
|
||||||
|
def chunks():
|
||||||
|
try:
|
||||||
|
while data := upstream.read(65536):
|
||||||
|
yield data
|
||||||
|
finally:
|
||||||
|
upstream.close()
|
||||||
|
upstream.release_conn()
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
chunks(),
|
||||||
|
media_type=row["content_type"],
|
||||||
|
headers={
|
||||||
|
"Content-Length": str(row["size_bytes"]),
|
||||||
|
"Content-Disposition": "inline; filename*=UTF-8''"
|
||||||
|
+ quote(row["filename"], safe=""),
|
||||||
|
},
|
||||||
|
)
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from src.errors import DomainError, not_found
|
||||||
|
|
||||||
|
|
||||||
|
def dto(row):
|
||||||
|
if row["state"] != "ready":
|
||||||
|
raise DomainError(409, "conflict", "Attachment is not ready")
|
||||||
|
return {
|
||||||
|
k: row[k]
|
||||||
|
for k in [
|
||||||
|
"id",
|
||||||
|
"chat_id",
|
||||||
|
"owner_id",
|
||||||
|
"filename",
|
||||||
|
"content_type",
|
||||||
|
"size_bytes",
|
||||||
|
"sha256",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
|
]
|
||||||
|
} | {
|
||||||
|
"status": "ready",
|
||||||
|
"variants": [],
|
||||||
|
"error_code": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Attachments:
|
||||||
|
def __init__(self, *, database, store, instance):
|
||||||
|
self.database = database
|
||||||
|
self.store = store
|
||||||
|
self.instance = instance
|
||||||
|
|
||||||
|
def upload(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
chat_id,
|
||||||
|
owner_id,
|
||||||
|
filename,
|
||||||
|
content_type,
|
||||||
|
size,
|
||||||
|
sha256,
|
||||||
|
stream,
|
||||||
|
):
|
||||||
|
identity = uuid4()
|
||||||
|
key = f"objects/{identity}/original"
|
||||||
|
# Durable intent precedes bytes; expired writers cannot confirm.
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO attachments(id,chat_id,owner_id,filename,"
|
||||||
|
"content_type,"
|
||||||
|
"size_bytes,sha256,object_key,state,uploader_instance,"
|
||||||
|
"expires_at) "
|
||||||
|
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s,'pending',%s,"
|
||||||
|
"clock_timestamp()+interval '120 seconds')",
|
||||||
|
(
|
||||||
|
identity,
|
||||||
|
chat_id,
|
||||||
|
owner_id,
|
||||||
|
filename,
|
||||||
|
content_type,
|
||||||
|
size,
|
||||||
|
sha256,
|
||||||
|
key,
|
||||||
|
self.instance,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.store.put(
|
||||||
|
key=key,
|
||||||
|
stream=stream,
|
||||||
|
size=size,
|
||||||
|
content_type=content_type,
|
||||||
|
sha256=sha256,
|
||||||
|
)
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"UPDATE attachments SET state='ready',"
|
||||||
|
"updated_at=clock_timestamp() "
|
||||||
|
"WHERE id=%s AND state='pending' "
|
||||||
|
"AND expires_at>clock_timestamp() "
|
||||||
|
"RETURNING *",
|
||||||
|
(identity,),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT * FROM attachments WHERE id=%s", (identity,)
|
||||||
|
).fetchone()
|
||||||
|
if row["state"] != "ready":
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Upload reservation expired"
|
||||||
|
)
|
||||||
|
return dto(row)
|
||||||
|
|
||||||
|
def row(self, *, identity, actor_id):
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT a.* FROM attachments a JOIN members m "
|
||||||
|
"ON m.chat_id=a.chat_id AND m.user_id=%s WHERE a.id=%s",
|
||||||
|
(actor_id, identity),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
raise not_found()
|
||||||
|
return row
|
||||||
|
|
||||||
|
def metadata(self, *, identity, actor_id):
|
||||||
|
return dto(self.row(identity=identity, actor_id=actor_id))
|
||||||
|
|
||||||
|
def content(self, *, identity, actor_id, variant):
|
||||||
|
row = self.row(identity=identity, actor_id=actor_id)
|
||||||
|
dto(row)
|
||||||
|
if variant != "original":
|
||||||
|
raise not_found()
|
||||||
|
if self.store.head(key=row["object_key"]) is None:
|
||||||
|
raise DomainError(503, "unavailable", "Original unavailable")
|
||||||
|
return row, self.store.get(key=row["object_key"])
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import json
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
import redis
|
||||||
|
from redis.backoff import NoBackoff
|
||||||
|
from redis.retry import Retry
|
||||||
|
|
||||||
|
from src.domain import User, timestamp
|
||||||
|
|
||||||
|
|
||||||
|
class UserCache:
|
||||||
|
def __init__(self, *, url: str):
|
||||||
|
self.client = redis.Redis.from_url(
|
||||||
|
url,
|
||||||
|
socket_connect_timeout=0.15,
|
||||||
|
socket_timeout=0.15,
|
||||||
|
max_connections=16,
|
||||||
|
retry=Retry(NoBackoff(), 0),
|
||||||
|
decode_responses=True,
|
||||||
|
)
|
||||||
|
self.disabled_until = 0.0
|
||||||
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.client.close()
|
||||||
|
|
||||||
|
def _command(self, function, *args, **kwargs):
|
||||||
|
if time.monotonic() < self.disabled_until:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return function(*args, **kwargs)
|
||||||
|
except redis.RedisError, OSError:
|
||||||
|
with self.lock:
|
||||||
|
self.disabled_until = time.monotonic() + 5
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get(self, *, user_id: UUID) -> User | None:
|
||||||
|
raw = self._command(self.client.get, "user:" + str(user_id))
|
||||||
|
if raw is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
row = json.loads(raw)
|
||||||
|
if UUID(row["id"]) != user_id:
|
||||||
|
return None
|
||||||
|
return User(
|
||||||
|
user_id,
|
||||||
|
row["username"],
|
||||||
|
row["display_name"],
|
||||||
|
datetime.fromisoformat(row["created_at"]),
|
||||||
|
)
|
||||||
|
except ValueError, TypeError, KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def put(self, *, user: User):
|
||||||
|
raw = json.dumps(
|
||||||
|
{
|
||||||
|
"id": str(user.id),
|
||||||
|
"username": user.username,
|
||||||
|
"display_name": user.display_name,
|
||||||
|
"created_at": timestamp(user.created_at),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self._command(self.client.set, "user:" + str(user.id), raw, ex=30)
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from datetime import datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from src.domain import Key, timestamp
|
||||||
|
from src.errors import invalid
|
||||||
|
|
||||||
|
|
||||||
|
def parse_uuid(value: str) -> UUID:
|
||||||
|
if not isinstance(value, str) or not re.fullmatch(
|
||||||
|
r"[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", value
|
||||||
|
):
|
||||||
|
raise invalid()
|
||||||
|
return UUID(value)
|
||||||
|
|
||||||
|
|
||||||
|
def encode_cursor(*, scope: str, key: Key) -> str:
|
||||||
|
payload = {
|
||||||
|
"v": 1,
|
||||||
|
"scope": scope,
|
||||||
|
"at": timestamp(key[0]),
|
||||||
|
"id": str(key[1]),
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
base64.urlsafe_b64encode(
|
||||||
|
json.dumps(payload, separators=(",", ":")).encode()
|
||||||
|
)
|
||||||
|
.decode()
|
||||||
|
.rstrip("=")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def decode_cursor(*, value: str | None, scope: str) -> Key | None:
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
if (
|
||||||
|
not value
|
||||||
|
or len(value) > 2048
|
||||||
|
or not re.fullmatch(r"[A-Za-z0-9_-]+", value)
|
||||||
|
):
|
||||||
|
raise ValueError
|
||||||
|
data = json.loads(
|
||||||
|
base64.b64decode(
|
||||||
|
value + "=" * (-len(value) % 4), altchars=b"-_", validate=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
not isinstance(data, dict)
|
||||||
|
or type(data.get("v")) is not int
|
||||||
|
or data["v"] != 1
|
||||||
|
or data.get("scope") != scope
|
||||||
|
or not isinstance(data.get("at"), str)
|
||||||
|
):
|
||||||
|
raise ValueError
|
||||||
|
at = datetime.fromisoformat(data["at"])
|
||||||
|
if at.tzinfo is None or timestamp(at) != data["at"]:
|
||||||
|
raise ValueError
|
||||||
|
return at, parse_uuid(data["id"])
|
||||||
|
except ValueError, TypeError, KeyError, binascii.Error, RecursionError:
|
||||||
|
raise invalid() from None
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import time
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
import psycopg
|
||||||
|
from psycopg.rows import dict_row
|
||||||
|
from psycopg_pool import ConnectionPool, PoolTimeout
|
||||||
|
|
||||||
|
from src.errors import DomainError
|
||||||
|
|
||||||
|
|
||||||
|
class DeadlineConnection(psycopg.Connection):
|
||||||
|
"""Клиентский срок действует и при замороженном PostgreSQL."""
|
||||||
|
|
||||||
|
def wait(self, gen, interval=0.1, timeout=None):
|
||||||
|
deadline = getattr(self, "request_deadline", None)
|
||||||
|
remaining = (
|
||||||
|
max(0.001, deadline - time.monotonic()) if deadline else 3.0
|
||||||
|
)
|
||||||
|
bound = min(remaining, timeout) if timeout is not None else remaining
|
||||||
|
try:
|
||||||
|
return super().wait(gen, interval=interval, timeout=bound)
|
||||||
|
except psycopg.OperationalError:
|
||||||
|
self.close()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
def __init__(self, *, url: str, size: int):
|
||||||
|
self.pool = ConnectionPool(
|
||||||
|
url,
|
||||||
|
connection_class=DeadlineConnection,
|
||||||
|
kwargs={
|
||||||
|
"autocommit": True,
|
||||||
|
"row_factory": dict_row,
|
||||||
|
"connect_timeout": 2,
|
||||||
|
"options": (
|
||||||
|
"-c statement_timeout=2500 -c lock_timeout=1000 "
|
||||||
|
"-c idle_in_transaction_session_timeout=6000"
|
||||||
|
),
|
||||||
|
},
|
||||||
|
min_size=1,
|
||||||
|
max_size=size,
|
||||||
|
timeout=1,
|
||||||
|
open=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
def open(self):
|
||||||
|
self.pool.open(wait=True, timeout=10)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.pool.close(timeout=2)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def connection(self):
|
||||||
|
try:
|
||||||
|
with self.pool.connection(timeout=1) as conn:
|
||||||
|
conn.request_deadline = time.monotonic() + 4
|
||||||
|
try:
|
||||||
|
yield conn
|
||||||
|
finally:
|
||||||
|
conn.request_deadline = None
|
||||||
|
except psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout:
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Database unavailable"
|
||||||
|
) from None
|
||||||
|
|
||||||
|
def ping(self):
|
||||||
|
with self.connection() as conn:
|
||||||
|
conn.execute("SELECT 1").fetchone()
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
||||||
|
def utcnow() -> datetime:
|
||||||
|
return datetime.now(UTC)
|
||||||
|
|
||||||
|
|
||||||
|
def timestamp(value: datetime) -> str:
|
||||||
|
return value.isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class User:
|
||||||
|
id: UUID
|
||||||
|
username: str
|
||||||
|
display_name: str
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class Chat:
|
||||||
|
id: UUID
|
||||||
|
title: str
|
||||||
|
member_ids: tuple[UUID, ...]
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class Message:
|
||||||
|
id: UUID
|
||||||
|
chat_id: UUID
|
||||||
|
sender_id: UUID
|
||||||
|
text: str
|
||||||
|
created_at: datetime
|
||||||
|
attachment_ids: tuple[UUID, ...] = ()
|
||||||
|
|
||||||
|
|
||||||
|
type Record = User | Chat | Message
|
||||||
|
type Key = tuple[datetime, UUID]
|
||||||
|
|
||||||
|
|
||||||
|
def order_key(record: Record) -> Key:
|
||||||
|
return record.created_at, record.id
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
class DomainError(Exception):
|
||||||
|
def __init__(
|
||||||
|
self, status: int, code: str, message: str, headers=None
|
||||||
|
) -> None:
|
||||||
|
self.status = status
|
||||||
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
self.headers = headers or {}
|
||||||
|
|
||||||
|
|
||||||
|
def invalid() -> DomainError:
|
||||||
|
return DomainError(400, "invalid_request", "Invalid request")
|
||||||
|
|
||||||
|
|
||||||
|
def not_found() -> DomainError:
|
||||||
|
return DomainError(404, "not_found", "Resource not found")
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import struct
|
||||||
|
import threading
|
||||||
|
import unicodedata
|
||||||
|
import zlib
|
||||||
|
|
||||||
|
from PIL import Image, UnidentifiedImageError
|
||||||
|
|
||||||
|
from src.errors import DomainError, invalid
|
||||||
|
|
||||||
|
MAX_BYTES = 10 * 1024 * 1024
|
||||||
|
MAX_PIXELS = 40_000_000
|
||||||
|
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
|
||||||
|
DECODE_SLOT = threading.BoundedSemaphore(1)
|
||||||
|
|
||||||
|
|
||||||
|
def filename(value):
|
||||||
|
if (
|
||||||
|
not value
|
||||||
|
or not 1 <= len(value) <= 255
|
||||||
|
or not value.strip()
|
||||||
|
or any(
|
||||||
|
c in "/\\" or unicodedata.category(c) in {"Cc", "Cs"}
|
||||||
|
for c in value
|
||||||
|
)
|
||||||
|
):
|
||||||
|
raise invalid()
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def verify(stream, *, declared):
|
||||||
|
if declared not in {"image/png", "image/jpeg"}:
|
||||||
|
raise DomainError(
|
||||||
|
415, "unsupported_media_type", "PNG or JPEG required"
|
||||||
|
)
|
||||||
|
with DECODE_SLOT:
|
||||||
|
try:
|
||||||
|
stream.seek(0)
|
||||||
|
with Image.open(stream) as image:
|
||||||
|
actual = {"PNG": "image/png", "JPEG": "image/jpeg"}.get(
|
||||||
|
image.format
|
||||||
|
)
|
||||||
|
if actual != declared:
|
||||||
|
raise DomainError(
|
||||||
|
415, "unsupported_media_type", "Image type mismatch"
|
||||||
|
)
|
||||||
|
if image.width * image.height > MAX_PIXELS:
|
||||||
|
raise DomainError(
|
||||||
|
413, "payload_too_large", "Pixel limit exceeded"
|
||||||
|
)
|
||||||
|
image.verify()
|
||||||
|
# JPEG verify alone does not decode entropy data.
|
||||||
|
stream.seek(0)
|
||||||
|
with Image.open(stream) as image:
|
||||||
|
image.load()
|
||||||
|
except Image.DecompressionBombError:
|
||||||
|
raise DomainError(
|
||||||
|
413, "payload_too_large", "Pixel limit exceeded"
|
||||||
|
) from None
|
||||||
|
except (
|
||||||
|
UnidentifiedImageError,
|
||||||
|
OSError,
|
||||||
|
ValueError,
|
||||||
|
SyntaxError,
|
||||||
|
struct.error,
|
||||||
|
zlib.error,
|
||||||
|
EOFError,
|
||||||
|
):
|
||||||
|
raise DomainError(
|
||||||
|
415, "unsupported_media_type", "Invalid image"
|
||||||
|
) from None
|
||||||
|
except MemoryError:
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Image memory budget exceeded"
|
||||||
|
) from None
|
||||||
|
finally:
|
||||||
|
stream.seek(0)
|
||||||
|
return declared
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from datetime import UTC, datetime
|
||||||
|
|
||||||
|
|
||||||
|
class JsonFormatter(logging.Formatter):
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
message = record.getMessage()
|
||||||
|
try:
|
||||||
|
event = json.loads(message)
|
||||||
|
if not isinstance(event, dict):
|
||||||
|
event = {"event": message}
|
||||||
|
except ValueError:
|
||||||
|
event = {"event": message}
|
||||||
|
event["timestamp"] = datetime.now(UTC).isoformat()
|
||||||
|
event["level"] = record.levelname.lower()
|
||||||
|
return json.dumps(event, ensure_ascii=False)
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import psycopg
|
||||||
|
|
||||||
|
from src.settings import secret
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with psycopg.connect(
|
||||||
|
secret("DATABASE_URL"), autocommit=True, connect_timeout=3
|
||||||
|
) as conn:
|
||||||
|
conn.execute("SELECT pg_advisory_lock(2301002)")
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS schema_migrations "
|
||||||
|
"(name text PRIMARY KEY, checksum text NOT NULL, "
|
||||||
|
"applied_at timestamptz NOT NULL DEFAULT now())"
|
||||||
|
)
|
||||||
|
for path in sorted(Path("migrations").glob("*.sql")):
|
||||||
|
content = path.read_bytes()
|
||||||
|
checksum = hashlib.sha256(content).hexdigest()
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT checksum FROM schema_migrations WHERE name=%s",
|
||||||
|
(path.name,),
|
||||||
|
).fetchone()
|
||||||
|
if row:
|
||||||
|
if row[0] != checksum:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Applied migration checksum mismatch"
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
with conn.transaction():
|
||||||
|
conn.execute(content.decode())
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO schema_migrations(name,checksum) "
|
||||||
|
"VALUES(%s,%s)",
|
||||||
|
(path.name, checksum),
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
json.dumps(
|
||||||
|
{"event": "migration_applied", "name": path.name}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
conn.execute("SELECT pg_advisory_unlock(2301002)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
import urllib3
|
||||||
|
from minio import Minio
|
||||||
|
from minio.error import S3Error
|
||||||
|
|
||||||
|
from src.errors import DomainError
|
||||||
|
from src.settings import secret
|
||||||
|
|
||||||
|
BUCKET = "messenger"
|
||||||
|
|
||||||
|
|
||||||
|
class ObjectStore:
|
||||||
|
def __init__(self):
|
||||||
|
self.http = urllib3.PoolManager(
|
||||||
|
maxsize=32,
|
||||||
|
block=True,
|
||||||
|
timeout=urllib3.Timeout(connect=1, read=3),
|
||||||
|
retries=urllib3.Retry(total=0),
|
||||||
|
)
|
||||||
|
self.client = Minio(
|
||||||
|
"s3:9000",
|
||||||
|
access_key=secret("S3_ACCESS_KEY"),
|
||||||
|
secret_key=secret("S3_SECRET_KEY"),
|
||||||
|
secure=False,
|
||||||
|
region="us-east-1",
|
||||||
|
http_client=self.http,
|
||||||
|
)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def operation(self):
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except S3Error, urllib3.exceptions.HTTPError, OSError:
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Object store unavailable"
|
||||||
|
) from None
|
||||||
|
|
||||||
|
def ping(self):
|
||||||
|
with self.operation():
|
||||||
|
if not self.client.bucket_exists(BUCKET):
|
||||||
|
raise DomainError(
|
||||||
|
503, "unavailable", "Object store unavailable"
|
||||||
|
)
|
||||||
|
|
||||||
|
def put(self, *, key, stream, size, content_type, sha256):
|
||||||
|
stream.seek(0)
|
||||||
|
with self.operation():
|
||||||
|
self.client.put_object(
|
||||||
|
BUCKET,
|
||||||
|
key,
|
||||||
|
stream,
|
||||||
|
length=size,
|
||||||
|
content_type=content_type,
|
||||||
|
metadata={"sha256": sha256},
|
||||||
|
part_size=10 * 1024 * 1024,
|
||||||
|
num_parallel_uploads=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def head(self, *, key):
|
||||||
|
with self.operation():
|
||||||
|
try:
|
||||||
|
return self.client.stat_object(BUCKET, key)
|
||||||
|
except S3Error as exc:
|
||||||
|
if exc.code in {"NoSuchKey", "NoSuchObject"}:
|
||||||
|
return None
|
||||||
|
raise
|
||||||
|
|
||||||
|
def get(self, *, key):
|
||||||
|
with self.operation():
|
||||||
|
return self.client.get_object(BUCKET, key)
|
||||||
|
|
||||||
|
def delete(self, *, key):
|
||||||
|
with self.operation():
|
||||||
|
self.client.remove_object(BUCKET, key)
|
||||||
|
|
||||||
|
def objects(self):
|
||||||
|
with self.operation():
|
||||||
|
yield from self.client.list_objects(
|
||||||
|
BUCKET, prefix="objects/", recursive=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.http.clear()
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import threading
|
||||||
|
|
||||||
|
from argon2 import PasswordHasher
|
||||||
|
from argon2.exceptions import InvalidHashError, VerificationError
|
||||||
|
from argon2.low_level import Type
|
||||||
|
|
||||||
|
from src.errors import DomainError
|
||||||
|
|
||||||
|
|
||||||
|
class Passwords:
|
||||||
|
def __init__(self):
|
||||||
|
self.hasher = PasswordHasher(
|
||||||
|
time_cost=3, memory_cost=65536, parallelism=4, type=Type.ID
|
||||||
|
)
|
||||||
|
self.slots = threading.BoundedSemaphore(2)
|
||||||
|
self.dummy = self.hasher.hash("Synthetic-dummy-password-not-for-login")
|
||||||
|
|
||||||
|
def _run(self, function, *args):
|
||||||
|
if not self.slots.acquire(timeout=5):
|
||||||
|
raise DomainError(503, "unavailable", "Authentication busy")
|
||||||
|
try:
|
||||||
|
return function(*args)
|
||||||
|
finally:
|
||||||
|
self.slots.release()
|
||||||
|
|
||||||
|
def hash(self, *, password: str) -> str:
|
||||||
|
return self._run(self.hasher.hash, password)
|
||||||
|
|
||||||
|
def verify(self, *, password: str, encoded: str | None) -> bool:
|
||||||
|
try:
|
||||||
|
return (
|
||||||
|
self._run(self.hasher.verify, encoded or self.dummy, password)
|
||||||
|
and encoded is not None
|
||||||
|
)
|
||||||
|
except VerificationError, InvalidHashError:
|
||||||
|
return False
|
||||||
@@ -0,0 +1,200 @@
|
|||||||
|
"""Сверка двух stores: dry-run по умолчанию, grace не менее пяти минут."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from src.database import Database
|
||||||
|
from src.object_store import ObjectStore
|
||||||
|
from src.settings import integer, secret
|
||||||
|
|
||||||
|
|
||||||
|
def verified(store, row, counters):
|
||||||
|
counters["head_requests"] += 1
|
||||||
|
info = store.head(key=row["object_key"])
|
||||||
|
if info is None:
|
||||||
|
return "missing"
|
||||||
|
if info.size != row["size_bytes"]:
|
||||||
|
return "mismatch"
|
||||||
|
result = store.get(key=row["object_key"])
|
||||||
|
digest = hashlib.sha256()
|
||||||
|
size = 0
|
||||||
|
try:
|
||||||
|
while data := result.read(65536):
|
||||||
|
digest.update(data)
|
||||||
|
size += len(data)
|
||||||
|
counters["bytes_read"] += len(data)
|
||||||
|
if size > 10 * 1024 * 1024:
|
||||||
|
return "mismatch"
|
||||||
|
finally:
|
||||||
|
result.close()
|
||||||
|
result.release_conn()
|
||||||
|
return "valid" if digest.hexdigest() == row["sha256"] else "mismatch"
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile(database, store, *, apply, grace):
|
||||||
|
started = time.monotonic()
|
||||||
|
result = {
|
||||||
|
"apply": apply,
|
||||||
|
"grace_seconds": grace,
|
||||||
|
"objects_scanned": 0,
|
||||||
|
"rows_scanned": 0,
|
||||||
|
"head_requests": 0,
|
||||||
|
"bytes_read": 0,
|
||||||
|
"fresh_protected": 0,
|
||||||
|
"ready_protected": 0,
|
||||||
|
"linked_protected": 0,
|
||||||
|
"actions": {},
|
||||||
|
"applied": {},
|
||||||
|
"details": [],
|
||||||
|
}
|
||||||
|
with database.connection() as conn:
|
||||||
|
now = conn.execute("SELECT clock_timestamp() AS at").fetchone()["at"]
|
||||||
|
rows = conn.execute("SELECT * FROM attachments ORDER BY id").fetchall()
|
||||||
|
cutoff = now - timedelta(seconds=grace)
|
||||||
|
objects = {o.object_name: o for o in store.objects()}
|
||||||
|
result["objects_scanned"] = len(objects)
|
||||||
|
result["rows_scanned"] = len(rows)
|
||||||
|
keys = {r["object_key"] for r in rows}
|
||||||
|
|
||||||
|
def record(action, identity, *, changed=False):
|
||||||
|
result["actions"][action] = result["actions"].get(action, 0) + 1
|
||||||
|
if changed:
|
||||||
|
result["applied"][action] = result["applied"].get(action, 0) + 1
|
||||||
|
if len(result["details"]) < 20:
|
||||||
|
result["details"].append(
|
||||||
|
{"action": action, "identity": str(identity)}
|
||||||
|
)
|
||||||
|
|
||||||
|
for key, obj in objects.items():
|
||||||
|
if obj.last_modified > cutoff:
|
||||||
|
result["fresh_protected"] += 1
|
||||||
|
continue
|
||||||
|
if key in keys:
|
||||||
|
continue
|
||||||
|
changed = False
|
||||||
|
if apply:
|
||||||
|
with database.connection() as conn, conn.transaction():
|
||||||
|
# Fresh intents are checked again immediately before deletion.
|
||||||
|
exists = conn.execute(
|
||||||
|
"SELECT 1 FROM attachments WHERE object_key=%s", (key,)
|
||||||
|
).fetchone()
|
||||||
|
if exists:
|
||||||
|
continue
|
||||||
|
current = store.head(key=key)
|
||||||
|
result["head_requests"] += 1
|
||||||
|
if current is not None and current.last_modified > cutoff:
|
||||||
|
result["fresh_protected"] += 1
|
||||||
|
continue
|
||||||
|
store.delete(key=key)
|
||||||
|
changed = True
|
||||||
|
record("delete_orphan", key, changed=changed)
|
||||||
|
for snapshot in rows:
|
||||||
|
if snapshot["created_at"] > cutoff:
|
||||||
|
continue
|
||||||
|
with database.connection() as conn, conn.transaction():
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT * FROM attachments WHERE id=%s FOR UPDATE",
|
||||||
|
(snapshot["id"],),
|
||||||
|
).fetchone()
|
||||||
|
if row is None or row["created_at"] > cutoff:
|
||||||
|
continue
|
||||||
|
action = None
|
||||||
|
if row["state"] == "pending":
|
||||||
|
state = verified(store, row, result)
|
||||||
|
action = (
|
||||||
|
"recover_pending"
|
||||||
|
if state == "valid"
|
||||||
|
else "fail_pending_" + state
|
||||||
|
)
|
||||||
|
if apply:
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE attachments SET state=%s,error_code=%s,"
|
||||||
|
"updated_at=clock_timestamp() WHERE id=%s",
|
||||||
|
(
|
||||||
|
"ready" if state == "valid" else "failed",
|
||||||
|
None if state == "valid" else "upload_" + state,
|
||||||
|
row["id"],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
elif row["state"] == "ready":
|
||||||
|
result["head_requests"] += 1
|
||||||
|
info = store.head(key=row["object_key"])
|
||||||
|
if info is not None:
|
||||||
|
# A ready attachment stays even if it has no Message link.
|
||||||
|
result["ready_protected"] += 1
|
||||||
|
continue
|
||||||
|
action = "mark_missing"
|
||||||
|
if apply:
|
||||||
|
conn.execute(
|
||||||
|
"UPDATE attachments SET state='failed',"
|
||||||
|
"error_code='original_missing',"
|
||||||
|
"updated_at=clock_timestamp() WHERE id=%s",
|
||||||
|
(row["id"],),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
linked = conn.execute(
|
||||||
|
"SELECT 1 FROM message_attachments "
|
||||||
|
"WHERE attachment_id=%s LIMIT 1",
|
||||||
|
(row["id"],),
|
||||||
|
).fetchone()
|
||||||
|
if linked:
|
||||||
|
result["linked_protected"] += 1
|
||||||
|
continue
|
||||||
|
obj = objects.get(row["object_key"])
|
||||||
|
if obj is None or obj.last_modified > cutoff:
|
||||||
|
continue
|
||||||
|
action = "delete_failed_object"
|
||||||
|
if apply:
|
||||||
|
# A timed-out uploader may finish PUT after LIST.
|
||||||
|
# Recheck freshness before removing its object.
|
||||||
|
current = store.head(key=row["object_key"])
|
||||||
|
result["head_requests"] += 1
|
||||||
|
if current is not None and current.last_modified > cutoff:
|
||||||
|
result["fresh_protected"] += 1
|
||||||
|
continue
|
||||||
|
store.delete(key=row["object_key"])
|
||||||
|
if action:
|
||||||
|
record(action, row["id"], changed=apply)
|
||||||
|
result["duration_seconds"] = round(time.monotonic() - started, 6)
|
||||||
|
result["complete"] = True
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--apply", action="store_true")
|
||||||
|
parser.add_argument("--loop", action="store_true")
|
||||||
|
args = parser.parse_args()
|
||||||
|
grace = integer("RECONCILE_GRACE_SECONDS", 300, 300, 604800)
|
||||||
|
database = Database(url=secret("DATABASE_URL"), size=2)
|
||||||
|
store = ObjectStore()
|
||||||
|
try:
|
||||||
|
database.open()
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
result = reconcile(
|
||||||
|
database, store, apply=args.apply, grace=grace
|
||||||
|
)
|
||||||
|
print(json.dumps(result, ensure_ascii=False), flush=True)
|
||||||
|
except Exception as exc:
|
||||||
|
print(
|
||||||
|
json.dumps(
|
||||||
|
{"complete": False, "error": type(exc).__name__}
|
||||||
|
),
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
if not args.loop:
|
||||||
|
raise SystemExit(1) from None
|
||||||
|
if not args.loop:
|
||||||
|
break
|
||||||
|
time.sleep(60)
|
||||||
|
finally:
|
||||||
|
store.close()
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,296 @@
|
|||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from psycopg.errors import ForeignKeyViolation, UniqueViolation
|
||||||
|
|
||||||
|
from src.cache import UserCache
|
||||||
|
from src.database import Database
|
||||||
|
from src.domain import Chat, Key, Message, User
|
||||||
|
from src.errors import DomainError, invalid, not_found
|
||||||
|
|
||||||
|
MESSAGE_COLUMNS = (
|
||||||
|
"m.*, ARRAY(SELECT attachment_id FROM message_attachments ma "
|
||||||
|
"WHERE ma.message_id=m.id ORDER BY ma.position) attachment_ids"
|
||||||
|
)
|
||||||
|
|
||||||
|
USER_COLUMNS = "id, username, display_name, created_at"
|
||||||
|
|
||||||
|
|
||||||
|
def user_dto(row) -> User:
|
||||||
|
return User(
|
||||||
|
row["id"], row["username"], row["display_name"], row["created_at"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def chat_dto(row) -> Chat:
|
||||||
|
return Chat(
|
||||||
|
row["id"], row["title"], tuple(row["member_ids"]), row["created_at"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def message_dto(row) -> Message:
|
||||||
|
return Message(
|
||||||
|
row["id"],
|
||||||
|
row["chat_id"],
|
||||||
|
row["sender_id"],
|
||||||
|
row["text"],
|
||||||
|
row["created_at"],
|
||||||
|
tuple(row.get("attachment_ids", ())),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PostgresRepository:
|
||||||
|
def __init__(self, *, database: Database, cache: UserCache):
|
||||||
|
self.database = database
|
||||||
|
self.cache = cache
|
||||||
|
|
||||||
|
def add_user(self, *, user: User, password_hash: str) -> User:
|
||||||
|
try:
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO users(id,username,display_name,"
|
||||||
|
"password_hash,created_at) VALUES(%s,%s,%s,%s,%s)",
|
||||||
|
(
|
||||||
|
user.id,
|
||||||
|
user.username,
|
||||||
|
user.display_name,
|
||||||
|
password_hash,
|
||||||
|
user.created_at,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
except UniqueViolation:
|
||||||
|
raise DomainError(
|
||||||
|
409, "conflict", "Username already exists"
|
||||||
|
) from None
|
||||||
|
return user
|
||||||
|
|
||||||
|
def get_user(self, *, user_id: UUID) -> User | None:
|
||||||
|
cached = self.cache.get(user_id=user_id)
|
||||||
|
if cached is not None:
|
||||||
|
return cached
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT " + USER_COLUMNS + " FROM users WHERE id=%s",
|
||||||
|
(user_id,),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
return None
|
||||||
|
user = user_dto(row)
|
||||||
|
self.cache.put(user=user)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def credentials(self, *, username: str):
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT "
|
||||||
|
+ USER_COLUMNS
|
||||||
|
+ ", password_hash FROM users WHERE username=%s",
|
||||||
|
(username,),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
return None, None
|
||||||
|
return user_dto(row), row["password_hash"]
|
||||||
|
|
||||||
|
def username_exists(self, *, username: str) -> bool:
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
return (
|
||||||
|
conn.execute(
|
||||||
|
"SELECT 1 FROM users WHERE username=%s", (username,)
|
||||||
|
).fetchone()
|
||||||
|
is not None
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_session(
|
||||||
|
self, *, digest: bytes, user_id: UUID, created_at, expires_at
|
||||||
|
):
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO sessions(token_digest,user_id,created_at,"
|
||||||
|
"expires_at) VALUES(%s,%s,%s,%s)",
|
||||||
|
(digest, user_id, created_at, expires_at),
|
||||||
|
)
|
||||||
|
|
||||||
|
def session_user(self, *, digest: bytes) -> UUID | None:
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT user_id FROM sessions WHERE token_digest=%s "
|
||||||
|
"AND expires_at>clock_timestamp()",
|
||||||
|
(digest,),
|
||||||
|
).fetchone()
|
||||||
|
return row["user_id"] if row else None
|
||||||
|
|
||||||
|
def delete_session(self, *, digest: bytes):
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM sessions WHERE token_digest=%s", (digest,)
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_chat(self, *, chat: Chat, actor_id: UUID) -> Chat:
|
||||||
|
members = chat.member_ids
|
||||||
|
if actor_id not in members or len(set(members)) != len(members):
|
||||||
|
raise invalid()
|
||||||
|
try:
|
||||||
|
with self.database.connection() as conn, conn.transaction():
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO chats(id,title,created_at) VALUES(%s,%s,%s)",
|
||||||
|
(chat.id, chat.title, chat.created_at),
|
||||||
|
)
|
||||||
|
with conn.cursor() as cursor:
|
||||||
|
cursor.executemany(
|
||||||
|
"INSERT INTO members(chat_id,user_id,position) "
|
||||||
|
"VALUES(%s,%s,%s)",
|
||||||
|
[
|
||||||
|
(chat.id, user_id, position)
|
||||||
|
for position, user_id in enumerate(members)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
except ForeignKeyViolation:
|
||||||
|
raise invalid() from None
|
||||||
|
return chat
|
||||||
|
|
||||||
|
def get_chat(self, *, chat_id: UUID, actor_id: UUID) -> Chat:
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT c.id,c.title,c.created_at,"
|
||||||
|
"array_agg(m.user_id ORDER BY m.position) member_ids "
|
||||||
|
"FROM chats c JOIN members m ON m.chat_id=c.id "
|
||||||
|
"WHERE c.id=%s AND EXISTS(SELECT 1 FROM members own "
|
||||||
|
"WHERE own.chat_id=c.id AND own.user_id=%s) GROUP BY c.id",
|
||||||
|
(chat_id, actor_id),
|
||||||
|
).fetchone()
|
||||||
|
if row is None:
|
||||||
|
raise not_found()
|
||||||
|
return chat_dto(row)
|
||||||
|
|
||||||
|
def add_message(
|
||||||
|
self, *, message: Message, idempotency_key: str
|
||||||
|
) -> Message:
|
||||||
|
# Область включает пользователя, метод и нормализованный путь чата.
|
||||||
|
scope = f"{message.sender_id}:POST:{message.chat_id}:{idempotency_key}"
|
||||||
|
lock = int.from_bytes(
|
||||||
|
hashlib.sha256(scope.encode()).digest()[:8], "big", signed=True
|
||||||
|
)
|
||||||
|
fingerprint = hashlib.sha256(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"text": message.text,
|
||||||
|
"attachment_ids": [str(x) for x in message.attachment_ids],
|
||||||
|
},
|
||||||
|
ensure_ascii=False,
|
||||||
|
sort_keys=True,
|
||||||
|
separators=(",", ":"),
|
||||||
|
).encode()
|
||||||
|
).digest()
|
||||||
|
try:
|
||||||
|
with self.database.connection() as conn, conn.transaction():
|
||||||
|
conn.execute("SELECT pg_advisory_xact_lock(%s)", (lock,))
|
||||||
|
prior = conn.execute(
|
||||||
|
"SELECT i.body_hash,i.message_id,"
|
||||||
|
"i.expires_at>clock_timestamp() active "
|
||||||
|
"FROM idempotency i WHERE user_id=%s AND chat_id=%s "
|
||||||
|
"AND key=%s FOR UPDATE",
|
||||||
|
(message.sender_id, message.chat_id, idempotency_key),
|
||||||
|
).fetchone()
|
||||||
|
if prior and prior["active"]:
|
||||||
|
if bytes(prior["body_hash"]) != fingerprint:
|
||||||
|
raise DomainError(409, "conflict", "Key already used")
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT "
|
||||||
|
+ MESSAGE_COLUMNS
|
||||||
|
+ " FROM messages m WHERE m.id=%s",
|
||||||
|
(prior["message_id"],),
|
||||||
|
).fetchone()
|
||||||
|
return message_dto(row)
|
||||||
|
if message.attachment_ids:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT id,state FROM attachments WHERE chat_id=%s "
|
||||||
|
"AND id=ANY(%s) FOR SHARE",
|
||||||
|
(message.chat_id, list(message.attachment_ids)),
|
||||||
|
).fetchall()
|
||||||
|
if len(rows) != len(message.attachment_ids):
|
||||||
|
raise not_found()
|
||||||
|
if any(row["state"] != "ready" for row in rows):
|
||||||
|
raise DomainError(
|
||||||
|
409, "conflict", "Attachment is not ready"
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO messages(id,chat_id,sender_id,"
|
||||||
|
"text,created_at) "
|
||||||
|
"VALUES(%s,%s,%s,%s,%s)",
|
||||||
|
(
|
||||||
|
message.id,
|
||||||
|
message.chat_id,
|
||||||
|
message.sender_id,
|
||||||
|
message.text,
|
||||||
|
message.created_at,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if message.attachment_ids:
|
||||||
|
with conn.cursor() as cursor:
|
||||||
|
cursor.executemany(
|
||||||
|
"INSERT INTO message_attachments("
|
||||||
|
"message_id,chat_id,"
|
||||||
|
"attachment_id,position) "
|
||||||
|
"VALUES(%s,%s,%s,%s)",
|
||||||
|
[
|
||||||
|
(message.id, message.chat_id, aid, i)
|
||||||
|
for i, aid in enumerate(message.attachment_ids)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO idempotency(user_id,chat_id,key,body_hash,"
|
||||||
|
"message_id,expires_at) VALUES(%s,%s,%s,%s,%s,"
|
||||||
|
"clock_timestamp()+interval '24 hours') "
|
||||||
|
"ON CONFLICT(user_id,chat_id,key) DO UPDATE SET "
|
||||||
|
"body_hash=excluded.body_hash,message_id=excluded.message_id,"
|
||||||
|
"expires_at=excluded.expires_at",
|
||||||
|
(
|
||||||
|
message.sender_id,
|
||||||
|
message.chat_id,
|
||||||
|
idempotency_key,
|
||||||
|
fingerprint,
|
||||||
|
message.id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
except ForeignKeyViolation:
|
||||||
|
raise not_found() from None
|
||||||
|
return message
|
||||||
|
|
||||||
|
def list_chats(
|
||||||
|
self, *, actor_id: UUID, after: Key | None, limit: int
|
||||||
|
) -> list[Chat]:
|
||||||
|
condition = "AND (c.created_at,c.id)>(%s,%s)" if after else ""
|
||||||
|
parameters = (
|
||||||
|
[actor_id, *after, limit + 1] if after else [actor_id, limit + 1]
|
||||||
|
)
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id "
|
||||||
|
"FROM members WHERE chat_id=c.id "
|
||||||
|
"ORDER BY position) member_ids "
|
||||||
|
"FROM chats c JOIN members own ON own.chat_id=c.id "
|
||||||
|
"WHERE own.user_id=%s "
|
||||||
|
+ condition
|
||||||
|
+ " ORDER BY c.created_at,c.id LIMIT %s",
|
||||||
|
parameters,
|
||||||
|
).fetchall()
|
||||||
|
return [chat_dto(row) for row in rows]
|
||||||
|
|
||||||
|
def list_messages(
|
||||||
|
self, *, chat_id: UUID, actor_id: UUID, after: Key | None, limit: int
|
||||||
|
) -> list[Message]:
|
||||||
|
condition = "AND (m.created_at,m.id)>(%s,%s)" if after else ""
|
||||||
|
parameters = (
|
||||||
|
[chat_id, *after, limit + 1] if after else [chat_id, limit + 1]
|
||||||
|
)
|
||||||
|
with self.database.connection() as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT "
|
||||||
|
+ MESSAGE_COLUMNS
|
||||||
|
+ " FROM messages m WHERE m.chat_id=%s "
|
||||||
|
+ condition
|
||||||
|
+ " ORDER BY m.created_at,m.id LIMIT %s",
|
||||||
|
parameters,
|
||||||
|
).fetchall()
|
||||||
|
return [message_dto(row) for row in rows]
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import re
|
||||||
|
from typing import Annotated
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import (
|
||||||
|
BaseModel,
|
||||||
|
BeforeValidator,
|
||||||
|
ConfigDict,
|
||||||
|
Field,
|
||||||
|
StrictStr,
|
||||||
|
ValidationInfo,
|
||||||
|
field_validator,
|
||||||
|
model_validator,
|
||||||
|
)
|
||||||
|
|
||||||
|
from src.cursor import parse_uuid
|
||||||
|
|
||||||
|
UuidIn = Annotated[UUID, BeforeValidator(parse_uuid)]
|
||||||
|
|
||||||
|
|
||||||
|
class Input(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
@field_validator("*", mode="after")
|
||||||
|
@classmethod
|
||||||
|
def nonblank(cls, value: object, info: ValidationInfo) -> object:
|
||||||
|
if isinstance(value, str) and (
|
||||||
|
(not value.strip() and info.field_name != "text")
|
||||||
|
or any(0xD800 <= ord(c) <= 0xDFFF for c in value)
|
||||||
|
):
|
||||||
|
raise ValueError("Invalid text")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class CreateUser(Input):
|
||||||
|
username: StrictStr
|
||||||
|
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||||
|
password: StrictStr = Field(min_length=12, max_length=128)
|
||||||
|
|
||||||
|
@field_validator("username")
|
||||||
|
@classmethod
|
||||||
|
def username_format(cls, value: str) -> str:
|
||||||
|
if not re.fullmatch(r"[a-z0-9_]{3,32}", value):
|
||||||
|
raise ValueError("Invalid username")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChat(Input):
|
||||||
|
title: StrictStr = Field(min_length=1, max_length=100)
|
||||||
|
member_ids: list[UuidIn] = Field(min_length=1, max_length=100, strict=True)
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMessage(Input):
|
||||||
|
text: StrictStr = Field(max_length=4000)
|
||||||
|
attachment_ids: list[UuidIn] = Field(
|
||||||
|
default_factory=list, max_length=10, strict=True
|
||||||
|
)
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def content(self):
|
||||||
|
if len(set(self.attachment_ids)) != len(self.attachment_ids):
|
||||||
|
raise ValueError("Duplicate attachment")
|
||||||
|
if not self.text.strip() and not self.attachment_ids:
|
||||||
|
raise ValueError("Empty message")
|
||||||
|
return self
|
||||||
+192
@@ -0,0 +1,192 @@
|
|||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
import re
|
||||||
|
import secrets
|
||||||
|
import threading
|
||||||
|
from datetime import timedelta
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
from src.cursor import decode_cursor, encode_cursor
|
||||||
|
from src.domain import Chat, Message, Record, User, order_key, utcnow
|
||||||
|
from src.errors import DomainError, invalid, not_found
|
||||||
|
from src.password import Passwords
|
||||||
|
from src.repository import PostgresRepository
|
||||||
|
|
||||||
|
|
||||||
|
def page(*, rows: list[Record], limit: int, scope: str) -> dict:
|
||||||
|
items = rows[:limit]
|
||||||
|
cursor = (
|
||||||
|
encode_cursor(scope=scope, key=order_key(items[-1]))
|
||||||
|
if len(rows) > limit
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
return {"items": items, "next_cursor": cursor}
|
||||||
|
|
||||||
|
|
||||||
|
def unauthorized(*, basic=False):
|
||||||
|
challenge = (
|
||||||
|
'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
if basic
|
||||||
|
else 'Bearer realm="messenger"'
|
||||||
|
)
|
||||||
|
return DomainError(
|
||||||
|
401,
|
||||||
|
"unauthorized",
|
||||||
|
"Authentication required",
|
||||||
|
{"WWW-Authenticate": challenge},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def bearer(value):
|
||||||
|
if not value:
|
||||||
|
raise unauthorized()
|
||||||
|
scheme, _, token = value.partition(" ")
|
||||||
|
if scheme.lower() != "bearer" or not re.fullmatch(
|
||||||
|
r"[A-Za-z0-9_-]{32,256}", token
|
||||||
|
):
|
||||||
|
raise unauthorized()
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
def token_digest(value):
|
||||||
|
return hashlib.sha256(value.encode()).digest()
|
||||||
|
|
||||||
|
|
||||||
|
class MessengerService:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
repository: PostgresRepository,
|
||||||
|
passwords: Passwords,
|
||||||
|
session_ttl: int,
|
||||||
|
) -> None:
|
||||||
|
self.repository = repository
|
||||||
|
self.passwords = passwords
|
||||||
|
self.session_ttl = session_ttl
|
||||||
|
self.registration_locks = [threading.Lock() for _ in range(64)]
|
||||||
|
|
||||||
|
def identify(self, *, value: str | None) -> User:
|
||||||
|
token = bearer(value)
|
||||||
|
user_id = self.repository.session_user(digest=token_digest(token))
|
||||||
|
user = self.repository.get_user(user_id=user_id) if user_id else None
|
||||||
|
if user is None:
|
||||||
|
raise unauthorized()
|
||||||
|
return user
|
||||||
|
|
||||||
|
def register(
|
||||||
|
self, *, username: str, display_name: str, password: str
|
||||||
|
) -> User:
|
||||||
|
# Bound lock memory and avoid running Argon2 for every local duplicate.
|
||||||
|
# Different replicas still race safely through PostgreSQL UNIQUE.
|
||||||
|
stripe = hashlib.sha256(username.encode()).digest()[0] % 64
|
||||||
|
lock = self.registration_locks[stripe]
|
||||||
|
if not lock.acquire(timeout=8):
|
||||||
|
raise DomainError(503, "unavailable", "Registration busy")
|
||||||
|
try:
|
||||||
|
if self.repository.username_exists(username=username):
|
||||||
|
raise DomainError(409, "conflict", "Username already exists")
|
||||||
|
encoded = self.passwords.hash(password=password)
|
||||||
|
return self.repository.add_user(
|
||||||
|
user=User(uuid4(), username, display_name, utcnow()),
|
||||||
|
password_hash=encoded,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
lock.release()
|
||||||
|
|
||||||
|
def login(self, *, authorization: str | None):
|
||||||
|
try:
|
||||||
|
scheme, _, encoded = (authorization or "").partition(" ")
|
||||||
|
if scheme.lower() != "basic":
|
||||||
|
raise ValueError
|
||||||
|
plain = base64.b64decode(encoded, validate=True).decode("utf-8")
|
||||||
|
username, separator, password = plain.partition(":")
|
||||||
|
if (
|
||||||
|
not separator
|
||||||
|
or not re.fullmatch(r"[a-z0-9_]{3,32}", username)
|
||||||
|
or not 12 <= len(password) <= 128
|
||||||
|
or not password.strip()
|
||||||
|
):
|
||||||
|
raise ValueError
|
||||||
|
except ValueError, UnicodeError:
|
||||||
|
raise unauthorized(basic=True) from None
|
||||||
|
user, password_hash = self.repository.credentials(username=username)
|
||||||
|
if not self.passwords.verify(password=password, encoded=password_hash):
|
||||||
|
raise unauthorized(basic=True)
|
||||||
|
now = utcnow()
|
||||||
|
expires = now + timedelta(seconds=self.session_ttl)
|
||||||
|
token = secrets.token_urlsafe(32)
|
||||||
|
self.repository.add_session(
|
||||||
|
digest=token_digest(token),
|
||||||
|
user_id=user.id,
|
||||||
|
created_at=now,
|
||||||
|
expires_at=expires,
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"token": token,
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"expires_at": expires,
|
||||||
|
"user": user,
|
||||||
|
}
|
||||||
|
|
||||||
|
def logout(self, *, authorization: str | None):
|
||||||
|
self.identify(value=authorization)
|
||||||
|
self.repository.delete_session(
|
||||||
|
digest=token_digest(bearer(authorization))
|
||||||
|
)
|
||||||
|
|
||||||
|
def create_chat(
|
||||||
|
self, *, actor_id: UUID, title: str, member_ids: list[UUID]
|
||||||
|
) -> Chat:
|
||||||
|
return self.repository.add_chat(
|
||||||
|
chat=Chat(uuid4(), title, tuple(member_ids), utcnow()),
|
||||||
|
actor_id=actor_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_chat(self, *, actor_id: UUID, chat_id: UUID) -> Chat:
|
||||||
|
return self.repository.get_chat(actor_id=actor_id, chat_id=chat_id)
|
||||||
|
|
||||||
|
def send(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
actor_id: UUID,
|
||||||
|
chat_id: UUID,
|
||||||
|
text: str,
|
||||||
|
idempotency_key: str | None,
|
||||||
|
attachment_ids: list[UUID],
|
||||||
|
) -> Message:
|
||||||
|
if not idempotency_key or not re.fullmatch(
|
||||||
|
r"[A-Za-z0-9._:-]{1,128}", idempotency_key
|
||||||
|
):
|
||||||
|
raise invalid()
|
||||||
|
return self.repository.add_message(
|
||||||
|
message=Message(
|
||||||
|
uuid4(),
|
||||||
|
chat_id,
|
||||||
|
actor_id,
|
||||||
|
text,
|
||||||
|
utcnow(),
|
||||||
|
tuple(attachment_ids),
|
||||||
|
),
|
||||||
|
idempotency_key=idempotency_key,
|
||||||
|
)
|
||||||
|
|
||||||
|
def chats(self, *, actor_id: UUID, limit: int, cursor: str | None) -> dict:
|
||||||
|
scope = f"chats:{actor_id}"
|
||||||
|
after = decode_cursor(value=cursor, scope=scope)
|
||||||
|
rows = self.repository.list_chats(
|
||||||
|
actor_id=actor_id, after=after, limit=limit
|
||||||
|
)
|
||||||
|
return page(rows=rows, limit=limit, scope=scope)
|
||||||
|
|
||||||
|
def history(
|
||||||
|
self, *, actor_id: UUID, chat: Chat, limit: int, cursor: str | None
|
||||||
|
) -> dict:
|
||||||
|
if actor_id not in chat.member_ids:
|
||||||
|
raise not_found()
|
||||||
|
chat_id = chat.id
|
||||||
|
scope = f"messages:{chat_id}"
|
||||||
|
after = decode_cursor(value=cursor, scope=scope)
|
||||||
|
rows = self.repository.list_messages(
|
||||||
|
actor_id=actor_id, chat_id=chat_id, after=after, limit=limit
|
||||||
|
)
|
||||||
|
return page(rows=rows, limit=limit, scope=scope)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import os
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def secret(name: str) -> str:
|
||||||
|
file = os.environ.get(name + "_FILE")
|
||||||
|
if file:
|
||||||
|
return Path(file).read_text().strip()
|
||||||
|
value = os.environ.get(name)
|
||||||
|
if not value:
|
||||||
|
raise ValueError("Missing required secret setting: " + name)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def integer(name: str, default: int, low: int, high: int) -> int:
|
||||||
|
value = int(os.environ.get(name, str(default)))
|
||||||
|
if not low <= value <= high:
|
||||||
|
raise ValueError("Invalid setting: " + name)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Settings:
|
||||||
|
database_url: str = field(repr=False)
|
||||||
|
cache_url: str = field(repr=False)
|
||||||
|
session_ttl: int = 3600
|
||||||
|
pool_size: int = 10
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls):
|
||||||
|
return cls(
|
||||||
|
secret("DATABASE_URL"),
|
||||||
|
secret("CACHE_URL"),
|
||||||
|
integer("SESSION_TTL_SECONDS", 3600, 1, 86400),
|
||||||
|
integer("DB_POOL_SIZE", 10, 1, 32),
|
||||||
|
)
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
from jsonschema import Draft202012Validator, FormatChecker
|
||||||
|
|
||||||
|
CONTRACT = json.loads(
|
||||||
|
(Path(__file__).parents[1] / "contracts/openapi.json").read_text()
|
||||||
|
)
|
||||||
|
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8084")
|
||||||
|
PASSWORD = "Synthetic-Password-🌍-123"
|
||||||
|
TOKENS = {}
|
||||||
|
|
||||||
|
|
||||||
|
def check(result: httpx.Response, status: int, schema: str) -> dict:
|
||||||
|
assert result.status_code == status, result.text
|
||||||
|
assert result.headers["x-request-id"]
|
||||||
|
assert result.headers["x-instance-id"]
|
||||||
|
assert result.headers["content-type"].startswith("application/json")
|
||||||
|
payload = result.json()
|
||||||
|
spec = {"$ref": f"#/components/schemas/{schema}", **CONTRACT}
|
||||||
|
Draft202012Validator(spec, format_checker=FormatChecker()).validate(
|
||||||
|
payload
|
||||||
|
)
|
||||||
|
if schema == "Error":
|
||||||
|
assert payload["error"]["request_id"] == result.headers["x-request-id"]
|
||||||
|
assert (
|
||||||
|
payload["error"]["code"]
|
||||||
|
== {
|
||||||
|
400: "invalid_request",
|
||||||
|
401: "unauthorized",
|
||||||
|
404: "not_found",
|
||||||
|
409: "conflict",
|
||||||
|
413: "payload_too_large",
|
||||||
|
415: "unsupported_media_type",
|
||||||
|
429: "rate_limited",
|
||||||
|
500: "internal_error",
|
||||||
|
503: "unavailable",
|
||||||
|
}[status]
|
||||||
|
)
|
||||||
|
return payload
|
||||||
|
|
||||||
|
|
||||||
|
def register(client: httpx.Client, **overrides) -> dict:
|
||||||
|
data = {
|
||||||
|
"username": "u_" + uuid4().hex[:24],
|
||||||
|
"display_name": "Алиса 🌍",
|
||||||
|
"password": PASSWORD,
|
||||||
|
}
|
||||||
|
data.update(overrides)
|
||||||
|
user = check(client.post("/api/v1/users", json=data), 201, "User")
|
||||||
|
session = check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
auth=httpx.BasicAuth(data["username"], data["password"]),
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Session",
|
||||||
|
)
|
||||||
|
TOKENS[user["id"]] = session["token"]
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client():
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE_URL, timeout=15, trust_env=False
|
||||||
|
) as client:
|
||||||
|
yield client
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def actors(client):
|
||||||
|
return register(client), register(client), register(client)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def chat(client, actors):
|
||||||
|
alice, bob, _ = actors
|
||||||
|
return check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(alice),
|
||||||
|
json={"title": "Учёба 🌍", "member_ids": [alice["id"], bob["id"]]},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Chat",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def headers(actor: dict) -> dict:
|
||||||
|
return {
|
||||||
|
"Authorization": "Bearer " + TOKENS[actor["id"]],
|
||||||
|
"Idempotency-Key": uuid4().hex,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def history(client, actor, chat_id, limit=7):
|
||||||
|
found, cursor = [], None
|
||||||
|
while True:
|
||||||
|
params = {"limit": limit}
|
||||||
|
if cursor:
|
||||||
|
params["cursor"] = cursor
|
||||||
|
page = check(
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/chats/{chat_id}/messages",
|
||||||
|
headers=headers(actor),
|
||||||
|
params=params,
|
||||||
|
),
|
||||||
|
200,
|
||||||
|
"MessagePage",
|
||||||
|
)
|
||||||
|
found.extend(page["items"])
|
||||||
|
cursor = page["next_cursor"]
|
||||||
|
if cursor is None:
|
||||||
|
return found
|
||||||
@@ -0,0 +1,412 @@
|
|||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import threading
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
from conftest import BASE_URL, PASSWORD, check, headers, history, register
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("path", ["/health/live", "/health/ready"])
|
||||||
|
def test_health(client, path):
|
||||||
|
assert check(client.get(path), 200, "Health") == {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value", [None, "not-a-uuid", str(uuid4()), uuid4().hex]
|
||||||
|
)
|
||||||
|
def test_identification(client, value):
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
"/api/v1/users/me", headers={"X-User-Id": value} if value else {}
|
||||||
|
),
|
||||||
|
401,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_identity(client, actors):
|
||||||
|
assert (
|
||||||
|
check(
|
||||||
|
client.get("/api/v1/users/me", headers=headers(actors[0])),
|
||||||
|
200,
|
||||||
|
"User",
|
||||||
|
)
|
||||||
|
== actors[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"data",
|
||||||
|
[
|
||||||
|
{},
|
||||||
|
[],
|
||||||
|
None,
|
||||||
|
{"username": "ABC", "display_name": "A"},
|
||||||
|
{"username": "abc\n", "display_name": "A"},
|
||||||
|
{"username": 123, "display_name": "A"},
|
||||||
|
{"username": "abc", "display_name": " "},
|
||||||
|
{"username": "abc", "display_name": "x" * 101},
|
||||||
|
{"username": "abc", "display_name": "A", "extra": True},
|
||||||
|
{"username": "abc", "display_name": "\ud800"},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid_user(client, data):
|
||||||
|
if isinstance(data, dict):
|
||||||
|
data = {"password": PASSWORD, **data}
|
||||||
|
check(client.post("/api/v1/users", content=json.dumps(data)), 400, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"raw", [b"{", b"NaN", b'{"username":"abc", "display_name":NaN}', b"\xff"]
|
||||||
|
)
|
||||||
|
def test_malformed_json(client, raw):
|
||||||
|
check(client.post("/api/v1/users", content=raw), 400, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_before_json(client):
|
||||||
|
check(client.post("/api/v1/chats", content=b"{"), 401, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_username(client, actors):
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": actors[0]["username"],
|
||||||
|
"display_name": "B",
|
||||||
|
"password": PASSWORD,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
409,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_unicode_codepoints(client, actors, chat):
|
||||||
|
user = register(client, display_name="🌍" * 100)
|
||||||
|
assert user["display_name"] == "🌍" * 100
|
||||||
|
text = "🌍" * 4000
|
||||||
|
result = check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"text": text},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)
|
||||||
|
assert result["text"] == text
|
||||||
|
assert result["sender_id"] == actors[0]["id"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"method,suffix,body",
|
||||||
|
[
|
||||||
|
("GET", "", None),
|
||||||
|
("GET", "/messages", None),
|
||||||
|
("POST", "/messages", {"text": "hello"}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_acl(client, actors, chat, method, suffix, body):
|
||||||
|
outsider = headers(actors[2])
|
||||||
|
actual = check(
|
||||||
|
client.request(
|
||||||
|
method,
|
||||||
|
f"/api/v1/chats/{chat['id']}{suffix}",
|
||||||
|
headers=outsider,
|
||||||
|
json=body,
|
||||||
|
),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
missing = check(
|
||||||
|
client.request(
|
||||||
|
method,
|
||||||
|
f"/api/v1/chats/{uuid4()}{suffix}",
|
||||||
|
headers=outsider,
|
||||||
|
json=body,
|
||||||
|
),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
assert actual["error"]["message"] == missing["error"]["message"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value", ["", " \t\n", 42, True, None, "a" * 4001])
|
||||||
|
def test_bad_message(client, actors, chat, value):
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"text": value},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_sender_not_client_controlled(client, actors, chat):
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"text": "Hi", "sender_id": actors[1]["id"]},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"case",
|
||||||
|
[
|
||||||
|
"duplicate",
|
||||||
|
"unknown",
|
||||||
|
"no_caller",
|
||||||
|
"empty",
|
||||||
|
"wrong_type",
|
||||||
|
"bad_uuid",
|
||||||
|
"too_many",
|
||||||
|
"blank_title",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid_chat(client, actors, case):
|
||||||
|
a, b, _ = actors
|
||||||
|
members = {
|
||||||
|
"duplicate": [a["id"], a["id"]],
|
||||||
|
"unknown": [a["id"], str(uuid4())],
|
||||||
|
"no_caller": [b["id"]],
|
||||||
|
"empty": [],
|
||||||
|
"wrong_type": a["id"],
|
||||||
|
"bad_uuid": ["broken"],
|
||||||
|
"too_many": [str(uuid4()) for _ in range(101)],
|
||||||
|
"blank_title": [a["id"]],
|
||||||
|
}[case]
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(a),
|
||||||
|
json={
|
||||||
|
"title": " " if case == "blank_title" else "Title",
|
||||||
|
"member_ids": members,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"limit", ["0", "101", "-1", "1.5", "true", "1e1", "", "12"]
|
||||||
|
)
|
||||||
|
def test_bad_limit(client, actors, chat, limit):
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
params={"limit": limit},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"cursor", ["", "broken", "W10", "e30", "*", "x" * 2049]
|
||||||
|
)
|
||||||
|
def test_bad_cursor(client, actors, chat, cursor):
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
params={"cursor": cursor},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_pagination(client, actors, chat):
|
||||||
|
a, b, outsider = actors
|
||||||
|
sent = []
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
for i in range(23):
|
||||||
|
sent.append(
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
path,
|
||||||
|
headers=headers(a if i % 2 else b),
|
||||||
|
json={"text": f"Msg {i}"},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
got = history(client, a, chat["id"])
|
||||||
|
assert got == sorted(
|
||||||
|
sent, key=lambda m: (m["created_at"], UUID(m["id"]).int)
|
||||||
|
)
|
||||||
|
first = check(
|
||||||
|
client.get(path, headers=headers(a), params={"limit": 1}),
|
||||||
|
200,
|
||||||
|
"MessagePage",
|
||||||
|
)
|
||||||
|
second = check(
|
||||||
|
client.get(
|
||||||
|
path,
|
||||||
|
headers=headers(b),
|
||||||
|
params={"limit": 22, "cursor": first["next_cursor"]},
|
||||||
|
),
|
||||||
|
200,
|
||||||
|
"MessagePage",
|
||||||
|
)
|
||||||
|
assert second["items"] == got[1:]
|
||||||
|
assert second["next_cursor"] is None
|
||||||
|
chats = check(
|
||||||
|
client.get("/api/v1/chats", headers=headers(outsider)), 200, "ChatPage"
|
||||||
|
)
|
||||||
|
assert chats["items"] == []
|
||||||
|
other = check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(a),
|
||||||
|
json={"title": "Other", "member_ids": [a["id"]]},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Chat",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
f"/api/v1/chats/{other['id']}/messages",
|
||||||
|
headers=headers(a),
|
||||||
|
params={"cursor": first["next_cursor"]},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
# Валидный base64/JSON с неверным типом ключа тоже даёт 400.
|
||||||
|
data = json.loads(
|
||||||
|
base64.urlsafe_b64decode(
|
||||||
|
first["next_cursor"] + "=" * (-len(first["next_cursor"]) % 4)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
data["at"] = 17
|
||||||
|
forged = (
|
||||||
|
base64.urlsafe_b64encode(json.dumps(data).encode())
|
||||||
|
.decode()
|
||||||
|
.rstrip("=")
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
client.get(path, headers=headers(a), params={"cursor": forged}),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_chat_pagination_scope(client, actors):
|
||||||
|
a, b, _ = actors
|
||||||
|
created = []
|
||||||
|
for i in range(4):
|
||||||
|
created.append(
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(a),
|
||||||
|
json={"title": str(i), "member_ids": [a["id"], b["id"]]},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Chat",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
page = check(
|
||||||
|
client.get("/api/v1/chats", headers=headers(a), params={"limit": 2}),
|
||||||
|
200,
|
||||||
|
"ChatPage",
|
||||||
|
)
|
||||||
|
assert page["items"] == created[:2]
|
||||||
|
last = check(
|
||||||
|
client.get(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(a),
|
||||||
|
params={"limit": 2, "cursor": page["next_cursor"]},
|
||||||
|
),
|
||||||
|
200,
|
||||||
|
"ChatPage",
|
||||||
|
)
|
||||||
|
assert last["items"] == created[2:]
|
||||||
|
assert last["next_cursor"] is None
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(b),
|
||||||
|
params={"cursor": page["next_cursor"]},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_concurrent_messages(client, actors, chat):
|
||||||
|
barrier = threading.Barrier(10)
|
||||||
|
a = actors[0]
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
|
||||||
|
def worker(i):
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE_URL, timeout=15, trust_env=False
|
||||||
|
) as conn:
|
||||||
|
barrier.wait(timeout=15)
|
||||||
|
return [
|
||||||
|
check(
|
||||||
|
conn.post(
|
||||||
|
path, headers=headers(a), json={"text": f"{i}:{j}"}
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)["id"]
|
||||||
|
for j in range(10)
|
||||||
|
]
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
ids = [item for batch in pool.map(worker, range(10)) for item in batch]
|
||||||
|
rows = history(client, a, chat["id"])
|
||||||
|
assert len(rows) == len(ids) == len(set(ids)) == 100
|
||||||
|
assert set(ids) == {row["id"] for row in rows}
|
||||||
|
assert len({row["text"] for row in rows}) == 100
|
||||||
|
|
||||||
|
|
||||||
|
def test_registration_race(client):
|
||||||
|
username = "race_" + uuid4().hex[:20]
|
||||||
|
barrier = threading.Barrier(10)
|
||||||
|
|
||||||
|
def worker(_):
|
||||||
|
with httpx.Client(
|
||||||
|
base_url=BASE_URL, timeout=15, trust_env=False
|
||||||
|
) as conn:
|
||||||
|
barrier.wait(timeout=15)
|
||||||
|
result = conn.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": username,
|
||||||
|
"display_name": "Test",
|
||||||
|
"password": PASSWORD,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
result,
|
||||||
|
result.status_code,
|
||||||
|
"User" if result.status_code == 201 else "Error",
|
||||||
|
)
|
||||||
|
return result.status_code
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
statuses = list(pool.map(worker, range(10)))
|
||||||
|
assert statuses.count(201) == 1
|
||||||
|
assert statuses.count(409) == 9
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_path(client):
|
||||||
|
check(client.get("/does-not-exist"), 404, "Error")
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from conftest import check, headers
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
PNG = (Path(__file__).parents[1] / "tests/fixtures/sample.png").read_bytes()
|
||||||
|
|
||||||
|
|
||||||
|
def upload(
|
||||||
|
client, actor, chat, *, data=PNG, filename="Фото 🌍.png", mime="image/png"
|
||||||
|
):
|
||||||
|
return client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/attachments",
|
||||||
|
headers={**headers(actor), "Content-Type": mime},
|
||||||
|
params={"filename": filename},
|
||||||
|
content=data,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_attachment_roundtrip_acl_and_message(client, actors, chat):
|
||||||
|
attachment = check(upload(client, actors[0], chat), 201, "Attachment")
|
||||||
|
assert attachment["status"] == "ready" and attachment["variants"] == []
|
||||||
|
assert attachment["sha256"] == hashlib.sha256(PNG).hexdigest()
|
||||||
|
assert attachment["size_bytes"] == len(PNG)
|
||||||
|
path = f"/api/v1/attachments/{attachment['id']}"
|
||||||
|
assert (
|
||||||
|
check(client.get(path, headers=headers(actors[1])), 200, "Attachment")
|
||||||
|
== attachment
|
||||||
|
)
|
||||||
|
downloaded = client.get(path + "/content", headers=headers(actors[1]))
|
||||||
|
assert downloaded.status_code == 200 and downloaded.content == PNG
|
||||||
|
assert downloaded.headers["Content-Type"] == "image/png"
|
||||||
|
assert not downloaded.history
|
||||||
|
for suffix in ["", "/content"]:
|
||||||
|
check(
|
||||||
|
client.get(path + suffix, headers=headers(actors[2])), 404, "Error"
|
||||||
|
)
|
||||||
|
check(client.get(path + suffix), 401, "Error")
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
path + "/content",
|
||||||
|
headers=headers(actors[1]),
|
||||||
|
params={"variant": "thumbnail"},
|
||||||
|
),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
sent = check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[1]),
|
||||||
|
json={"text": "", "attachment_ids": [attachment["id"]]},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)
|
||||||
|
assert sent["attachment_ids"] == [attachment["id"]]
|
||||||
|
assert sent["sender_id"] == actors[1]["id"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"name",
|
||||||
|
[
|
||||||
|
"../x.png",
|
||||||
|
"x/y.png",
|
||||||
|
"x\\y.png",
|
||||||
|
"x\x00.png",
|
||||||
|
"x\x7f.png",
|
||||||
|
"x\n.png",
|
||||||
|
"a" * 256,
|
||||||
|
"",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_filename_guard(client, actors, chat, name):
|
||||||
|
check(upload(client, actors[0], chat, filename=name), 400, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"data,mime,status",
|
||||||
|
[
|
||||||
|
(b"not an image", "image/png", 415),
|
||||||
|
(PNG, "image/jpeg", 415),
|
||||||
|
(PNG, "application/octet-stream", 415),
|
||||||
|
(PNG[:25], "image/png", 415),
|
||||||
|
(b"", "image/png", 415),
|
||||||
|
(b"x" * (10 * 1024 * 1024 + 1), "image/png", 413),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_image_guards(client, actors, chat, data, mime, status):
|
||||||
|
r = upload(client, actors[0], chat, data=data, mime=mime)
|
||||||
|
check(r, status, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
def test_jpeg_original_unchanged(client, actors, chat):
|
||||||
|
stream = io.BytesIO()
|
||||||
|
Image.new("RGB", (320, 200), (30, 60, 90)).save(stream, format="JPEG")
|
||||||
|
jpeg = stream.getvalue()
|
||||||
|
attachment = check(
|
||||||
|
upload(
|
||||||
|
client,
|
||||||
|
actors[0],
|
||||||
|
chat,
|
||||||
|
data=jpeg,
|
||||||
|
filename="camera.jpg",
|
||||||
|
mime="image/jpeg",
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Attachment",
|
||||||
|
)
|
||||||
|
r = client.get(
|
||||||
|
f"/api/v1/attachments/{attachment['id']}/content",
|
||||||
|
headers=headers(actors[1]),
|
||||||
|
)
|
||||||
|
assert r.content == jpeg
|
||||||
|
|
||||||
|
|
||||||
|
def test_cross_chat_link_and_unknown(client, actors, chat):
|
||||||
|
a = check(upload(client, actors[0], chat), 201, "Attachment")
|
||||||
|
other = check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/chats",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"title": "Other", "member_ids": [actors[0]["id"]]},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Chat",
|
||||||
|
)
|
||||||
|
for identity in [a["id"], str(uuid4())]:
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{other['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"text": "", "attachment_ids": [identity]},
|
||||||
|
),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
json={"text": "", "attachment_ids": [a["id"], a["id"]]},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_attachments_are_semantically_equal(client, actors, chat):
|
||||||
|
h = headers(actors[0])
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
first = check(
|
||||||
|
client.post(path, headers=h, json={"text": "Semantic default"}),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)
|
||||||
|
second = check(
|
||||||
|
client.post(
|
||||||
|
path,
|
||||||
|
headers=h,
|
||||||
|
json={"attachment_ids": [], "text": "Semantic default"},
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Message",
|
||||||
|
)
|
||||||
|
assert first == second and first["attachment_ids"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_pixel_limit_before_decode(client, actors, chat):
|
||||||
|
# Compressible synthetic image stays below10MiB but exceeds40M pixels.
|
||||||
|
stream = io.BytesIO()
|
||||||
|
Image.new("1", (8000, 5001)).save(stream, format="PNG")
|
||||||
|
assert len(stream.getvalue()) < 10 * 1024 * 1024
|
||||||
|
r = upload(client, actors[0], chat, data=stream.getvalue())
|
||||||
|
assert r.status_code == 413
|
||||||
|
|
||||||
|
|
||||||
|
def test_chunked_body_limit_without_content_length(client, actors, chat):
|
||||||
|
r = client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/attachments",
|
||||||
|
headers={**headers(actors[0]), "Content-Type": "image/png"},
|
||||||
|
params={"filename": "chunked.png"},
|
||||||
|
content=iter([b"x" * (10 * 1024 * 1024 + 1)]),
|
||||||
|
)
|
||||||
|
assert r.request.headers.get("content-length") is None
|
||||||
|
assert r.status_code == 413
|
||||||
|
assert r.json()["error"]["code"] == "payload_too_large"
|
||||||
|
assert r.json()["error"]["request_id"] == r.headers["x-request-id"]
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import json
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from conftest import check, headers
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("encoding", ["utf-16", "utf-32"])
|
||||||
|
def test_only_utf8_json(client, encoding):
|
||||||
|
payload = {"username": "u_" + uuid4().hex[:20], "display_name": "UTF"}
|
||||||
|
payload["password"] = "Synthetic-encoding-test-123"
|
||||||
|
result = client.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
content=json.dumps(payload).encode(encoding),
|
||||||
|
headers={"Content-Type": "application/json"},
|
||||||
|
)
|
||||||
|
check(result, 400, "Error")
|
||||||
|
|
||||||
|
|
||||||
|
def test_leading_zero_limit(client, actors, chat):
|
||||||
|
result = client.get(
|
||||||
|
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
params={"limit": "0001"},
|
||||||
|
)
|
||||||
|
check(result, 200, "MessagePage")
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from conftest import check, headers
|
||||||
|
|
||||||
|
|
||||||
|
def test_history_acl_precedes_query_validation(client, actors, chat):
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
path,
|
||||||
|
headers=headers(actors[2]),
|
||||||
|
params={"limit": 0, "cursor": "broken"},
|
||||||
|
),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
client.get(
|
||||||
|
path,
|
||||||
|
headers=headers(actors[0]),
|
||||||
|
params={"limit": 0, "cursor": "broken"},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
from conftest import BASE_URL, check, headers, history
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("key", [None, "", "a b", "x" * 129, "a/b"])
|
||||||
|
def test_key_required_and_valid(client, actors, chat, key):
|
||||||
|
h = headers(actors[0])
|
||||||
|
h.pop("Idempotency-Key")
|
||||||
|
if key is not None:
|
||||||
|
h["Idempotency-Key"] = key
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
f"/api/v1/chats/{chat['id']}/messages",
|
||||||
|
headers=h,
|
||||||
|
json={"text": "Key"},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parallel_replay_across_instances(client, actors, chat):
|
||||||
|
h = headers(actors[0])
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
|
||||||
|
def send(_):
|
||||||
|
with httpx.Client(base_url=BASE_URL, trust_env=False, timeout=15) as c:
|
||||||
|
r = c.post(path, headers=h, json={"text": "Один результат 🌍"})
|
||||||
|
return check(r, 201, "Message"), r.headers["x-instance-id"]
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
|
results = list(pool.map(send, range(30)))
|
||||||
|
assert len({r[0]["id"] for r in results}) == 1
|
||||||
|
assert len({r[1] for r in results}) >= 2
|
||||||
|
assert history(client, actors[0], chat["id"]) == [results[0][0]]
|
||||||
|
check(
|
||||||
|
client.post(path, headers=h, json={"text": "Другой текст"}),
|
||||||
|
409,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
# Другой пользователь может независимо использовать тот же ключ.
|
||||||
|
other = headers(actors[1])
|
||||||
|
other["Idempotency-Key"] = h["Idempotency-Key"]
|
||||||
|
second = check(
|
||||||
|
client.post(path, headers=other, json={"text": "Боб"}), 201, "Message"
|
||||||
|
)
|
||||||
|
assert second["id"] != results[0][0]["id"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_replay_rechecks_acl(client, actors, chat):
|
||||||
|
h = headers(actors[0])
|
||||||
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||||
|
check(
|
||||||
|
client.post(path, headers=h, json={"text": "Existing"}), 201, "Message"
|
||||||
|
)
|
||||||
|
outsider = headers(actors[2])
|
||||||
|
outsider["Idempotency-Key"] = h["Idempotency-Key"]
|
||||||
|
check(
|
||||||
|
client.post(path, headers=outsider, json={"text": "Existing"}),
|
||||||
|
404,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
path,
|
||||||
|
headers={"Idempotency-Key": uuid4().hex},
|
||||||
|
json={"text": "Existing"},
|
||||||
|
),
|
||||||
|
401,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
from conftest import PASSWORD, TOKENS, check, headers, register
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"password", ["", "x" * 11, "x" * 129, " " * 12, 42, True, None]
|
||||||
|
)
|
||||||
|
def test_password_validation(client, password):
|
||||||
|
check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/users",
|
||||||
|
json={
|
||||||
|
"username": "pwd_" + uuid4().hex[:20],
|
||||||
|
"display_name": "Test",
|
||||||
|
"password": password,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
400,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_full_utf8_password(client):
|
||||||
|
password = "🌍" * 127 + ":"
|
||||||
|
user = register(client, password=password)
|
||||||
|
assert (
|
||||||
|
check(
|
||||||
|
client.get("/api/v1/users/me", headers=headers(user)), 200, "User"
|
||||||
|
)
|
||||||
|
== user
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"authorization", [None, "Basic broken", "Bearer unknown", "Basic /w=="]
|
||||||
|
)
|
||||||
|
def test_bad_basic(client, authorization):
|
||||||
|
result = client.post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
headers={"Authorization": authorization} if authorization else {},
|
||||||
|
)
|
||||||
|
check(result, 401, "Error")
|
||||||
|
assert (
|
||||||
|
result.headers["www-authenticate"]
|
||||||
|
== 'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_wrong_password_and_unknown_user(client):
|
||||||
|
user = register(client)
|
||||||
|
for username in [user["username"], "missing_" + uuid4().hex[:20]]:
|
||||||
|
result = client.post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
auth=httpx.BasicAuth(username, PASSWORD + "wrong"),
|
||||||
|
)
|
||||||
|
data = check(result, 401, "Error")
|
||||||
|
assert data["error"]["message"] == "Authentication required"
|
||||||
|
|
||||||
|
|
||||||
|
def test_logout_is_not_revived_by_cache(client):
|
||||||
|
user = register(client)
|
||||||
|
auth = headers(user)
|
||||||
|
check(client.get("/api/v1/users/me", headers=auth), 200, "User")
|
||||||
|
result = client.delete("/api/v1/auth/sessions/current", headers=auth)
|
||||||
|
assert result.status_code == 204 and result.content == b""
|
||||||
|
assert result.headers["x-request-id"]
|
||||||
|
check(client.get("/api/v1/users/me", headers=auth), 401, "Error")
|
||||||
|
check(
|
||||||
|
client.delete("/api/v1/auth/sessions/current", headers=auth),
|
||||||
|
401,
|
||||||
|
"Error",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_only_current_session_revoked(client):
|
||||||
|
user = register(client)
|
||||||
|
another = check(
|
||||||
|
client.post(
|
||||||
|
"/api/v1/auth/sessions",
|
||||||
|
auth=httpx.BasicAuth(user["username"], PASSWORD),
|
||||||
|
),
|
||||||
|
201,
|
||||||
|
"Session",
|
||||||
|
)
|
||||||
|
assert another["token"] != TOKENS[user["id"]]
|
||||||
|
assert len(another["token"]) >= 32
|
||||||
|
assert (
|
||||||
|
client.delete(
|
||||||
|
"/api/v1/auth/sessions/current", headers=headers(user)
|
||||||
|
).status_code
|
||||||
|
== 204
|
||||||
|
)
|
||||||
|
result = client.get(
|
||||||
|
"/api/v1/users/me",
|
||||||
|
headers={"Authorization": "Bearer " + another["token"]},
|
||||||
|
)
|
||||||
|
assert check(result, 200, "User") == user
|
||||||
|
|
||||||
|
|
||||||
|
def test_bearer_source_and_ignored_user_header(client, actors):
|
||||||
|
alice, bob, _ = actors
|
||||||
|
auth = {**headers(alice), "X-User-Id": bob["id"]}
|
||||||
|
assert (
|
||||||
|
check(client.get("/api/v1/users/me", headers=auth), 200, "User")["id"]
|
||||||
|
== alice["id"]
|
||||||
|
)
|
||||||
|
result = client.get(
|
||||||
|
"/api/v1/users/me", auth=httpx.BasicAuth(alice["username"], PASSWORD)
|
||||||
|
)
|
||||||
|
check(result, 401, "Error")
|
||||||
|
assert result.headers["www-authenticate"] == 'Bearer realm="messenger"'
|
||||||
Reference in New Issue
Block a user