ЛР 6 - БВТ2302 - Саватеев - на 4 #78
@@ -18,3 +18,20 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
.pytest_cache
|
||||
*.py[cod]
|
||||
tests
|
||||
student-tests
|
||||
contracts
|
||||
scripts
|
||||
examples
|
||||
docs
|
||||
*.md
|
||||
Makefile
|
||||
Dockerfile*
|
||||
requirements-dev.txt
|
||||
lab.json
|
||||
.mypy_cache
|
||||
.ruff_cache
|
||||
infra
|
||||
.venv-validate
|
||||
|
||||
+80
-6
@@ -3,14 +3,40 @@
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
LOG_LEVEL=INFO
|
||||
SHUTDOWN_TIMEOUT_SEC=5
|
||||
|
||||
SHUTDOWN_DRAIN_SEC=1
|
||||
APP_KEEPALIVE_TIMEOUT_SEC=75
|
||||
|
||||
DB_USER=test
|
||||
DB_PASS=test
|
||||
DB_NAME=test
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_POOL_MIN_SIZE=2
|
||||
DB_POOL_MAX_SIZE=10
|
||||
DB_CONNECT_TIMEOUT_SEC=3
|
||||
DB_COMMAND_TIMEOUT_SEC=5
|
||||
DB_STARTUP_TIMEOUT_SEC=30
|
||||
|
||||
VALKEY_HOST=localhost
|
||||
VALKEY_PORT=6379
|
||||
VALKEY_PASS=test
|
||||
VALKEY_POOL_MAX_SIZE=20
|
||||
VALKEY_TIMEOUT_SEC=0.5
|
||||
VALKEY_RETRY_AFTER_SEC=5
|
||||
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
SESSION_CACHE_TTL_SECONDS=60
|
||||
PASSWORD_HASH_WORKERS=4
|
||||
|
||||
# lab3: число API-реплик за балансировщиком и срок хранения Idempotency-Key
|
||||
API_REPLICAS=2
|
||||
# Публичный порт балансировщика на 127.0.0.1 (порт для make test-expiry — LB_EXPIRY_PORT=8081)
|
||||
LB_PORT=8082
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
@@ -18,13 +44,61 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Только путь к файлам; сами ключи — в secrets/ (scripts/init-secrets.sh)
|
||||
S3_ADMIN_ACCESS_KEY_ID_FILE=/run/secrets/s3_admin_access_key_id
|
||||
S3_ADMIN_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_admin_secret_access_key
|
||||
S3_POOL_MAX_SIZE=20
|
||||
S3_CONNECT_TIMEOUT_SEC=2
|
||||
S3_READ_TIMEOUT_SEC=10
|
||||
S3_MAX_ATTEMPTS=2
|
||||
S3_STARTUP_TIMEOUT_SEC=60
|
||||
# Байт загрузки в RAM; остальное — во временном файле запроса (удаляется сразу)
|
||||
UPLOAD_SPOOL_MEMORY_BYTES=1048576
|
||||
# Одновременных проверок изображений на реплику: худший случай ~250 МБ на одну
|
||||
IMAGE_INSPECT_WORKERS=1
|
||||
ATTACHMENT_STORE_TIMEOUT_SEC=30
|
||||
# Janitor очищает незавершённые загрузки не моложе grace period
|
||||
ATTACHMENT_PENDING_GRACE_SEC=900
|
||||
JANITOR_INTERVAL_SEC=60
|
||||
|
||||
# Логические параметры skeleton; это приложение использует DB_*/VALKEY_* выше.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
BROKER_URL_FILE=/run/secrets/broker_url
|
||||
WORKER_CONCURRENCY=2
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
STAGING_RETENTION_SECONDS=86400
|
||||
|
||||
# Порт PostgreSQL на localhost (55432 занят другим локальным стендом).
|
||||
DB_HOST_PORT=55435
|
||||
|
||||
# Lab5: RabbitMQ и ограниченный pipeline изображений
|
||||
RABBITMQ_USER=test
|
||||
RABBITMQ_PASS=test
|
||||
WORKER_PREFETCH=2
|
||||
JOB_LEASE_SECONDS=90
|
||||
JOB_RETRY_BASE_SECONDS=2
|
||||
JOB_RETRY_MAX_SECONDS=60
|
||||
IMAGE_PROCESS_MEMORY_MB=768
|
||||
OUTBOX_POLL_SECONDS=0.5
|
||||
|
||||
OTEL_SERVICE_NAME=messenger-api
|
||||
# Worker must set a distinct service.name.
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
||||
OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
|
||||
LB_EXPIRY_PORT=8083
|
||||
APP_REVISION=1.6.0
|
||||
OTEL_METRIC_EXPORT_INTERVAL=2000
|
||||
OTEL_METRIC_EXPORT_TIMEOUT=1000
|
||||
OTEL_EXPORTER_OTLP_TIMEOUT=1
|
||||
OTEL_BSP_MAX_QUEUE_SIZE=256
|
||||
OTEL_BSP_MAX_EXPORT_BATCH_SIZE=64
|
||||
OTEL_BSP_SCHEDULE_DELAY=1000
|
||||
OTEL_BSP_EXPORT_TIMEOUT=1000
|
||||
OTEL_BLRP_MAX_QUEUE_SIZE=256
|
||||
OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=64
|
||||
OTEL_BLRP_SCHEDULE_DELAY=1000
|
||||
OTEL_BLRP_EXPORT_TIMEOUT=1000
|
||||
GRAFANA_PORT=3006
|
||||
|
||||
@@ -30,3 +30,7 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
.pytest_cache/
|
||||
.mypy_cache/
|
||||
.ruff_cache/
|
||||
.venv-validate/
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
FROM python:3.12.12-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
POETRY_VIRTUALENVS_CREATE=false
|
||||
|
||||
WORKDIR /app
|
||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||
pip install --timeout 30 --retries 8 "poetry==2.2.1"
|
||||
COPY poetry.lock pyproject.toml ./
|
||||
RUN --mount=type=cache,target=/root/.cache/pypoetry \
|
||||
poetry config requests.max-retries 6 \
|
||||
&& poetry install --only main --no-root --no-interaction
|
||||
COPY migrations ./migrations
|
||||
COPY src ./src
|
||||
|
||||
# Процесс работает не от root
|
||||
RUN useradd --system --uid 10001 --no-create-home app
|
||||
USER app
|
||||
|
||||
ARG APP_REVISION=dev
|
||||
ENV APP_REVISION=${APP_REVISION}
|
||||
LABEL org.opencontainers.image.revision=${APP_REVISION}
|
||||
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["python", "src/healthcheck.py"]
|
||||
CMD ["python", "src/gateway.py"]
|
||||
@@ -1,13 +1,22 @@
|
||||
PYTHON ?= python3
|
||||
BASE_URL ?= http://localhost:8080
|
||||
BASE_URL ?= http://localhost:8082
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
export BUILDX_NO_DEFAULT_ATTESTATIONS := 1
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover install init up down restart restore instances rolling-update ps logs wait-ready destroy-data student-test concurrency-test test-expiry test-failures test-migrations explain sigterm-check experiment-failover experiment-rolling test-storage test-s3-failure janitor-dry-run janitor-once experiment-uploads s3-ui
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "up/down/restart/ps/logs: compose stack; destroy-data: DELETE volumes intentionally"
|
||||
@echo "instances: API containers <-> X-Instance-Id; restore: start replicas stopped by stop-one.sh; rolling-update: replace replicas one by one"
|
||||
@echo "experiment-failover / experiment-rolling: load + stop each replica / rolling update, metrics to evidence/"
|
||||
@echo "student-test: own tests; test-migrations: from empty volume; test-expiry: short SESSION_TTL; test-failures: stop Valkey/PostgreSQL/S3; explain: EXPLAIN history"
|
||||
@echo "init: .env + local S3 secrets; test-storage: private bucket/credentials/consistency; janitor-dry-run / janitor-once: S3/DB reconcile"
|
||||
@echo "experiment-uploads: >=10 concurrent uploads, peak API memory to evidence/"
|
||||
@echo "s3-ui: temporary read-only SeaweedFS Filer UI on 127.0.0.1:8888 (Ctrl+C to close)"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +37,114 @@ test-persistence:
|
||||
test-failover:
|
||||
@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)
|
||||
|
||||
install:
|
||||
poetry install --no-root
|
||||
|
||||
init:
|
||||
./scripts/init-secrets.sh
|
||||
|
||||
up:
|
||||
docker compose up -d --build --wait
|
||||
|
||||
down:
|
||||
docker compose --profile expiry down
|
||||
|
||||
restart:
|
||||
./scripts/restart.sh
|
||||
@$(MAKE) --no-print-directory wait-ready
|
||||
|
||||
restore:
|
||||
./scripts/restore.sh
|
||||
|
||||
instances:
|
||||
./scripts/instances.sh
|
||||
|
||||
rolling-update:
|
||||
./scripts/rolling-update.sh
|
||||
|
||||
ps:
|
||||
docker compose ps
|
||||
|
||||
logs:
|
||||
docker compose logs -f api nginx
|
||||
|
||||
wait-ready:
|
||||
@for i in $$(seq 1 60); do \
|
||||
curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1 && echo "ready: $(BASE_URL)" && exit 0; \
|
||||
sleep 1; \
|
||||
done; echo "API is not ready after 60s"; exit 1
|
||||
|
||||
destroy-data:
|
||||
docker compose --profile expiry down -v
|
||||
|
||||
student-test:
|
||||
BASE_URL="$(BASE_URL)" poetry run pytest
|
||||
|
||||
concurrency-test:
|
||||
BASE_URL="$(BASE_URL)" poetry run pytest student-tests/test_concurrency.py -s
|
||||
|
||||
test-expiry:
|
||||
docker compose --profile expiry up -d --build --wait api-short-ttl
|
||||
EXPIRY_BASE_URL=http://localhost:8083 poetry run pytest student-tests/test_expiry.py -s; \
|
||||
status=$$?; docker compose --profile expiry stop api-short-ttl; exit $$status
|
||||
|
||||
test-failures:
|
||||
BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_dependency_failures.py student-tests/test_storage_failures.py student-tests/test_attachment_failover.py -s
|
||||
|
||||
test-s3-failure:
|
||||
BASE_URL="$(BASE_URL)" RUN_FAILURE_TESTS=1 poetry run pytest student-tests/test_storage_failures.py -s
|
||||
|
||||
test-storage:
|
||||
BASE_URL="$(BASE_URL)" RUN_STORAGE_TESTS=1 poetry run pytest student-tests/test_storage_security.py student-tests/test_storage_consistency.py -s
|
||||
|
||||
janitor-dry-run:
|
||||
docker compose exec -T janitor python src/janitor.py --once --dry-run
|
||||
|
||||
janitor-once:
|
||||
docker compose exec -T janitor python src/janitor.py --once
|
||||
|
||||
s3-ui:
|
||||
$(PYTHON) scripts/s3_ui.py
|
||||
|
||||
experiment-uploads:
|
||||
poetry run python scripts/upload_experiment.py --base-url "$(BASE_URL)" --output evidence/experiment-uploads.json
|
||||
|
||||
test-migrations:
|
||||
./scripts/check_migrations.sh
|
||||
|
||||
explain:
|
||||
docker compose exec -T postgres sh -c 'psql -X -q -v ON_ERROR_STOP=1 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"' \
|
||||
< scripts/explain_history.sql > evidence/explain_history.txt
|
||||
@echo "saved: evidence/explain_history.txt"
|
||||
|
||||
sigterm-check:
|
||||
$(PYTHON) scripts/sigterm_check.py --base-url "$(BASE_URL)"
|
||||
|
||||
experiment-failover:
|
||||
$(PYTHON) scripts/experiment.py failover --base-url "$(BASE_URL)" --output evidence/experiment-failover.json
|
||||
|
||||
experiment-rolling:
|
||||
$(PYTHON) scripts/experiment.py rolling --base-url "$(BASE_URL)" --output evidence/experiment-rolling.json
|
||||
|
||||
.PHONY: test-pipeline-failures jobs-failed replay
|
||||
|
||||
test-pipeline-failures:
|
||||
BASE_URL="$(BASE_URL)" RUN_PIPELINE_FAILURE_TESTS=1 poetry run pytest student-tests/test_pipeline_failures.py -v -s
|
||||
|
||||
jobs-failed:
|
||||
docker compose exec -T dispatcher python src/jobs.py failed
|
||||
|
||||
replay:
|
||||
@test -n "$(JOB_ID)" -a -n "$(OPERATOR)" -a -n "$(REASON)" || (echo "Set JOB_ID, OPERATOR and REASON"; exit 2)
|
||||
docker compose exec -T dispatcher python src/jobs.py replay --job-id "$(JOB_ID)" --operator "$(OPERATOR)" --reason "$(REASON)"
|
||||
|
||||
GRAFANA_URL ?= http://localhost:3006
|
||||
|
||||
.PHONY: test-observability test-observability-incidents
|
||||
|
||||
test-observability:
|
||||
$(PYTHON) scripts/check_observability.py --base-url "$(BASE_URL)" --grafana-url "$(GRAFANA_URL)"
|
||||
|
||||
test-observability-incidents:
|
||||
$(PYTHON) scripts/check_observability.py --base-url "$(BASE_URL)" --grafana-url "$(GRAFANA_URL)" --incidents
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
# Лабораторная 6. Наблюдаемость: OpenTelemetry и Grafana
|
||||
|
||||
Научиться находить причину деградации через метрики, логи и трассы всей системы, включая фоновую обработку.
|
||||
## Запуск
|
||||
|
||||
Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен.
|
||||
```sh
|
||||
make init
|
||||
make up
|
||||
make test
|
||||
make test-observability
|
||||
```
|
||||
|
||||
API и Swagger доступны на порту 8082. [Дашборд Grafana](http://localhost:3006/d/messenger-lab6/) открывается на порту 3006. Для проверки алертов и восстановления запустите `make test-observability-incidents`. Запуск и результаты описаны в [REPORT.md](REPORT.md).
|
||||
|
||||
## Задание
|
||||
|
||||
Научиться находить причину деградации через метрики, логи и трассы всей системы, включая фоновую обработку.
|
||||
|
||||
[Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md)
|
||||
|
||||
@@ -51,7 +62,7 @@
|
||||
make check
|
||||
|
||||
# После реализации запустите свой сервер/стенд по REPORT.md.
|
||||
# По умолчанию тест использует http://localhost:8080.
|
||||
# Makefile использует http://localhost:8082.
|
||||
make test
|
||||
```
|
||||
|
||||
|
||||
@@ -1,37 +1,58 @@
|
||||
# Отчёт — лабораторная 6
|
||||
# Лабораторная 6. Наблюдаемость Messenger
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
Саватеев Артём. Целевая оценка **4**.
|
||||
|
||||
## Запуск из чистого clone
|
||||
## Запуск
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
```sh
|
||||
make init
|
||||
make up
|
||||
make test
|
||||
make test-observability
|
||||
make test-observability-incidents
|
||||
```
|
||||
|
||||
## Архитектура и решения
|
||||
Нужны Docker Compose ≥ 2.20.2 и Python ≥ 3.10 для проверки телеметрии. Приложение собрано на Python 3.12.12. Swagger доступен на [8082/docs](http://localhost:8082/docs), дашборд Grafana — на [3006](http://localhost:3006/d/messenger-lab6/). Тома Prometheus, Loki, Tempo и Grafana сохраняются между запусками.
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab5, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
## Сигналы и корреляция
|
||||
|
||||
## Доказательства критериев
|
||||
Две API-реплики, dispatcher и worker отправляют OTLP/HTTP в Collector. Prometheus забирает метрики с Collector. Loki принимает структурированные логи a Grafana читает эти источника.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
API сохраняет W3C trace context в той же транзакции, что job и outbox. Dispatcher создаёт producer span для каждой отправки и передаёт её контекст в `traceparent`/`tracestate` задания RabbitMQ. Worker продолжает его consumer span, внутри которого видны S3 и PostgreSQL. При retry обе попытки остаются в трассе исходного upload. `X-Trace-Id` ведёт в Tempo, `X-Request-Id` связывает ответ с HTTP-логом. В Loki для корреляции доступны `service_name`, `event`, `severity`, `trace_id` и `span_id`.
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
## Дашборд и алерты
|
||||
|
||||
## Эксперименты и отказы
|
||||
| Сигнал | Запрос или метрика | Единица |
|
||||
| --- | --- | --- |
|
||||
| RPS | `sum by (method, route) (rate(messenger_http_requests_total[1m]))` | запрос/с |
|
||||
| p50/p95/p99 HTTP | `histogram_quantile(0.95, sum by (le) (rate(messenger_http_duration_seconds_bucket[2m])))`; аналогично 0.5/0.99 | с |
|
||||
| Обработка и время до ready | `messenger_jobs_total{outcome=…}`, `messenger_job_duration_seconds_bucket`, `messenger_attachment_ready_duration_seconds_bucket` | задания, с |
|
||||
| Ожидающие jobs и outbox | `messenger_queue_depth`, `messenger_queue_oldest_age_seconds`, `messenger_outbox_pending` | задания, с, события |
|
||||
| PostgreSQL и worker | `messenger_db_pool{state=…}`, `sum(messenger_worker_busy) / sum(messenger_worker_capacity)` | соединения, доля |
|
||||
| Ресурсы | `rate(messenger_process_cpu_seconds_total[1m])`, `messenger_process_memory_bytes`; аналогично метрики cgroup | ядра, байты |
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Глубина и возраст относятся к ожидающим jobs в PostgreSQL, включая задания до отправки в RabbitMQ. Возраст считается от принятия исходной job. Перцентили агрегируют histogram buckets реплик. Таймер ready заканчивается после фиксации результата.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
**SLI** — доля бизнес-ответов без 5xx в скользящем окне 15 минут. Health и Swagger исключены, 4xx входят в знаменатель. **SLO** — ≥ 99%. При отсутствии запросов SLI не определён. `MessengerHighErrorRate` срабатывает при доле 5xx > 5% за 30 с при минимум пяти запросах и выдержке 6 с. `MessengerQueueLag` срабатывает при возрасте > 10 с и непустой очереди в течение 6 с. Правила оценивает Prometheus. Диагностика описана в [runbook](infra/otel/runbook.md).
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
## Буферизация и отказы
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
SDK держит до 256 записей логов и 256 спанов на процесс и отправляет их пакетами до 64. Таймаут OTLP — 1 с. Collector ограничен 256 MiB, memory limiter настроен на 160 MiB с резервом 32 MiB. Размер batch — до 128 записей. Для Loki и Tempo используются отдельные очереди по 16 пакетов с повторами до 8 с. Измеряемые потери при отказе отражаются в счётчиках ошибок отправки Collector. API и worker выполняют бизнес-операции независимо от экспорта.
|
||||
|
||||
## Проверки
|
||||
|
||||
Локальный прогон 23.11.2026. Все 27 запросов дашборда выполнились через Grafana. В Prometheus найдены 16 проверяемых метрик. Одна загрузка дала 22 связанных спана и логи API и worker. [Проверка дашборда](evidence/lab6-dashboard.txt).
|
||||
|
||||
| Сценарий | Наблюдение и результат |
|
||||
| --- | --- |
|
||||
| Остановлен worker | В очереди появилось одно задание, возраст достиг 19,9 с. Алерт `MessengerQueueLag` сработал и снялся после обработки. В трассе `2cb55e0cec7f29110c363a2a581ca265` видны publish и consume для исходной попытки и retry. SHA-256 скачанного оригинала совпал. |
|
||||
| Остановлен PostgreSQL | 44 ответа 503, доля 5xx в окне достигла 73%. В трассе `83aa5b76b36831200387fe72ba38c175` видна ошибка `postgres.pool.acquire`, в Loki найден связанный лог `postgres.unavailable`. После запуска БД запись прошла, алерт снялся. |
|
||||
| Остановлены Loki и Tempo | API принял 450 сообщений за 18 с. Сумма счётчиков ошибок отправки и переполнения очередей выросла на 2017 записей логов и спанов. Пик RSS Collector составил 213,9 MiB при лимите 256 MiB. После запуска хранилищ доступны прежние логи и трассы, новые записи снова поступают. |
|
||||
|
||||
[Протокол сценариев](evidence/lab6-incidents.txt). Прошли [публичный smoke](evidence/lab6-public.txt), [OpenAPI и JSON Schema](evidence/lab6-contracts.txt) и [миграции на пустой БД](evidence/lab6-migrations.txt). В [основном наборе](evidence/lab6-student-tests.txt) прошло 195 тестов, ещё 23 требуют отдельного запуска.
|
||||
|
||||
После перезапуска PostgreSQL, Valkey, S3, RabbitMQ и сервисов приложения сохранились сессия, сообщение и байты вложения. Команда `make test-persistence ACTION_SCRIPT=scripts/restart.sh`. Через Grafana доступны исторические метрики, логи и трассы после перезапуска стенда. [Протокол перезапуска](evidence/lab6-persistence.txt).
|
||||
|
||||
Нагрузка 24.11.2026. Docker на Mac16,12, 10 vCPU и 7,65 GiB RAM. Лимиты каждой API-реплики 1 CPU и 512 MiB, worker 2 CPU и 1792 MiB, Collector 256 MiB. После 5 с прогрева 4 клиента отправляли синтетические сообщения в один чат в течение 30 с (`python3 tests/load.py --base-url http://localhost:8082 --duration 30 --concurrency 4`). Получено 3797 успешных запросов и 4 ошибки (0,11%), 114,7 успешных запросов/с. p50/p95/p99 успешных ответов — 14,5/63,6/92,9 мс. [Результат нагрузки](evidence/lab6-load.json), [замер CPU и RAM во время теста](evidence/lab6-load-resources.txt).
|
||||
|
||||
Стенд работает на одном хосте. История сигналов хранится 7 дней. Счётчики потерь Collector охватывают записи, дошедшие до его экспортёров.
|
||||
|
||||
+465
@@ -0,0 +1,465 @@
|
||||
name: messenger-lab6
|
||||
|
||||
# Один образ приложения для API, worker, dispatcher, migrate, s3-bootstrap и janitor. Собирает его
|
||||
# только api: параллельная сборка одного тега несколькими сервисами конфликтует.
|
||||
x-app: &app
|
||||
image: mtusi-messenger:lab6
|
||||
pull_policy: never
|
||||
env_file:
|
||||
- .env
|
||||
environment: &app-network
|
||||
DB_HOST: postgres
|
||||
DB_PORT: "5432"
|
||||
VALKEY_HOST: valkey
|
||||
VALKEY_PORT: "6379"
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
|
||||
x-api-resources: &api-resources
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
reservations:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
x-api: &api
|
||||
<<: *app
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_started
|
||||
# Только ограниченные ключи своего bucket; административных у API нет
|
||||
secrets:
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- edge
|
||||
- backend
|
||||
- storage
|
||||
- ingest
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "src/healthcheck.py"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 2
|
||||
start_period: 20s
|
||||
start_interval: 1s
|
||||
stop_grace_period: 10s
|
||||
|
||||
services:
|
||||
nginx:
|
||||
image: nginx:1.28.0-alpine3.21
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- "127.0.0.1:${LB_PORT:-8082}:8080"
|
||||
- "127.0.0.1:${LB_EXPIRY_PORT:-8083}:8081"
|
||||
volumes:
|
||||
- ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
- ./infra/nginx/errors.conf:/etc/nginx/errors.conf:ro
|
||||
networks:
|
||||
- edge
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8090/healthz"]
|
||||
interval: 5s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 64M
|
||||
|
||||
api:
|
||||
<<: *api
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
APP_REVISION: ${APP_REVISION}
|
||||
deploy:
|
||||
<<: *api-resources
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
|
||||
migrate:
|
||||
<<: *app
|
||||
command: ["python", "src/migrate.py"]
|
||||
restart: "no"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
postgres:
|
||||
image: postgres:18.2-alpine3.22
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
POSTGRES_PASSWORD: ${DB_PASS}
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql
|
||||
ports:
|
||||
- "127.0.0.1:${DB_HOST_PORT:-55435}:5432"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
start_period: 10s
|
||||
networks:
|
||||
- backend
|
||||
- db-host
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.5"
|
||||
memory: 512M
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:8.1.4-alpine3.22
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
VALKEY_PASS: ${VALKEY_PASS}
|
||||
command:
|
||||
- valkey-server
|
||||
- --requirepass
|
||||
- ${VALKEY_PASS}
|
||||
- --save
|
||||
- ""
|
||||
- --appendonly
|
||||
- "no"
|
||||
- --maxmemory
|
||||
- 64mb
|
||||
- --maxmemory-policy
|
||||
- allkeys-lru
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASS\" ping | grep -q PONG"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
networks:
|
||||
- backend
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 96M
|
||||
|
||||
# SeaweedFS всё-в-одном. master, volume, filer и их веб-интерфейсы слушают
|
||||
# только loopback контейнера; в сеть storage открыт лишь S3 API (9000).
|
||||
# Порты наружу не публикуются: bucket доступен только через API.
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:4.48
|
||||
restart: unless-stopped
|
||||
command:
|
||||
- server
|
||||
- -dir=/data
|
||||
- -ip=127.0.0.1
|
||||
- -ip.bind=127.0.0.1
|
||||
- -master.volumeSizeLimitMB=1024
|
||||
- -volume.max=0
|
||||
- -s3
|
||||
- -s3.port=9000
|
||||
- -s3.ip.bind=0.0.0.0
|
||||
- -s3.config=/run/secrets/s3_identities
|
||||
- -s3.port.iceberg=0
|
||||
- -s3.port.lance=0
|
||||
# Тела загрузок SeaweedFS держит в памяти: без лимита 20 одновременных
|
||||
# PUT по 9 MiB вызывали OOM kill weed (evidence/experiment-uploads.json).
|
||||
# Сверх лимита запросы ждут своей очереди.
|
||||
- -s3.concurrentUploadLimitMB=128
|
||||
- -volume.concurrentUploadLimitMB=128
|
||||
environment:
|
||||
# Мягкий предел кучи Go ниже лимита контейнера: GC срабатывает раньше OOM
|
||||
GOMEMLIMIT: 384MiB
|
||||
secrets:
|
||||
- s3_identities
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
networks:
|
||||
- storage
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9000/healthz"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
# Идемпотентное создание приватного bucket административными ключами
|
||||
s3-bootstrap:
|
||||
<<: *app
|
||||
command: ["python", "src/s3_bootstrap.py"]
|
||||
restart: "no"
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
secrets:
|
||||
- s3_admin_access_key_id
|
||||
- s3_admin_secret_access_key
|
||||
networks:
|
||||
- storage
|
||||
healthcheck:
|
||||
disable: true
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 256M
|
||||
|
||||
# Сверка S3/БД: незавершённые загрузки старше grace period и сироты
|
||||
janitor:
|
||||
<<: *app
|
||||
command: ["python", "src/janitor.py"]
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
secrets:
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- backend
|
||||
- storage
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "src/healthcheck.py", "janitor"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
start_interval: 1s
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 192M
|
||||
|
||||
broker:
|
||||
image: rabbitmq:4.1.4-alpine
|
||||
hostname: broker
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
RABBITMQ_DEFAULT_USER: ${RABBITMQ_USER:-test}
|
||||
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASS:-test}
|
||||
volumes:
|
||||
- rabbitmq_data:/var/lib/rabbitmq
|
||||
networks:
|
||||
- backend
|
||||
healthcheck:
|
||||
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
stop_grace_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
|
||||
dispatcher:
|
||||
<<: *app
|
||||
command: ["python", "src/dispatcher.py"]
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
<<: *app-network
|
||||
OTEL_SERVICE_NAME: messenger-dispatcher
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
secrets:
|
||||
- broker_url
|
||||
networks:
|
||||
- backend
|
||||
- ingest
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "src/healthcheck.py", "dispatcher"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
start_period: 20s
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 192M
|
||||
|
||||
worker:
|
||||
<<: *app
|
||||
command: ["python", "src/worker.py"]
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
<<: *app-network
|
||||
OTEL_SERVICE_NAME: messenger-worker
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
secrets:
|
||||
- broker_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
networks:
|
||||
- backend
|
||||
- storage
|
||||
- ingest
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "src/healthcheck.py", "worker"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
start_period: 20s
|
||||
stop_grace_period: 10s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "2.0"
|
||||
memory: 1792M
|
||||
|
||||
api-short-ttl:
|
||||
<<: *api
|
||||
profiles:
|
||||
- expiry
|
||||
environment:
|
||||
<<: *app-network
|
||||
SESSION_TTL_SECONDS: "2"
|
||||
deploy:
|
||||
<<: *api-resources
|
||||
replicas: 1
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:0.145.0
|
||||
command: ["--config=/etc/otelcol-contrib/collector.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel/collector.yaml:/etc/otelcol-contrib/collector.yaml:ro
|
||||
networks: [ingest, monitoring]
|
||||
mem_limit: 256m
|
||||
restart: unless-stopped
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v3.11.3
|
||||
command:
|
||||
- --config.file=/etc/prometheus/prometheus.yaml
|
||||
- --storage.tsdb.path=/prometheus
|
||||
- --storage.tsdb.retention.time=7d
|
||||
volumes:
|
||||
- ./infra/prometheus:/etc/prometheus:ro
|
||||
- prometheus_data:/prometheus
|
||||
networks: [monitoring]
|
||||
mem_limit: 384m
|
||||
restart: unless-stopped
|
||||
|
||||
loki:
|
||||
image: grafana/loki:3.6.8
|
||||
command: ["-config.file=/etc/loki/loki.yaml"]
|
||||
volumes:
|
||||
- ./infra/loki/loki.yaml:/etc/loki/loki.yaml:ro
|
||||
- loki_data:/loki
|
||||
networks: [monitoring]
|
||||
mem_limit: 512m
|
||||
restart: unless-stopped
|
||||
|
||||
tempo:
|
||||
image: grafana/tempo:2.9.0
|
||||
command: ["-config.file=/etc/tempo/tempo.yaml"]
|
||||
volumes:
|
||||
- ./infra/tempo/tempo.yaml:/etc/tempo/tempo.yaml:ro
|
||||
- tempo_data:/var/tempo
|
||||
networks: [monitoring]
|
||||
mem_limit: 384m
|
||||
restart: unless-stopped
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:13.2.4
|
||||
environment:
|
||||
GF_AUTH_ANONYMOUS_ENABLED: "true"
|
||||
GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
|
||||
GF_SECURITY_ADMIN_PASSWORD__FILE: /run/secrets/grafana_admin_password
|
||||
GF_USERS_ALLOW_SIGN_UP: "false"
|
||||
GF_ANALYTICS_REPORTING_ENABLED: "false"
|
||||
GF_ANALYTICS_CHECK_FOR_UPDATES: "false"
|
||||
secrets: [grafana_admin_password]
|
||||
ports:
|
||||
- "127.0.0.1:${GRAFANA_PORT}:3000"
|
||||
volumes:
|
||||
- ./infra/grafana/provisioning:/etc/grafana/provisioning:ro
|
||||
- ./infra/grafana/dashboards:/etc/grafana/dashboards:ro
|
||||
- grafana_data:/var/lib/grafana
|
||||
networks: [edge, monitoring]
|
||||
mem_limit: 384m
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
ingest:
|
||||
internal: true
|
||||
monitoring:
|
||||
internal: true
|
||||
edge: {}
|
||||
backend:
|
||||
internal: true
|
||||
# Только localhost-проброс PostgreSQL для локальных клиентов вроде DBeaver
|
||||
db-host: {}
|
||||
# API, janitor и bootstrap ↔ S3; PostgreSQL и Valkey сюда не входят
|
||||
storage:
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
s3_data:
|
||||
rabbitmq_data:
|
||||
prometheus_data:
|
||||
loki_data:
|
||||
tempo_data:
|
||||
grafana_data:
|
||||
|
||||
# Создаются scripts/init-secrets.sh; в git не попадают
|
||||
secrets:
|
||||
grafana_admin_password:
|
||||
file: ./secrets/grafana_admin_password
|
||||
broker_url:
|
||||
file: ./secrets/broker_url
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key
|
||||
s3_admin_access_key_id:
|
||||
file: ./secrets/s3_admin_access_key_id
|
||||
s3_admin_secret_access_key:
|
||||
file: ./secrets/s3_admin_secret_access_key
|
||||
s3_identities:
|
||||
file: ./secrets/s3_identities.json
|
||||
@@ -0,0 +1,4 @@
|
||||
.venv/bin/python scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
.venv/bin/python scripts/validate_openapi.py
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
@@ -0,0 +1 @@
|
||||
PASS: Grafana loaded 17 panels, Prometheus executed 27 dashboard queries
|
||||
@@ -0,0 +1,35 @@
|
||||
python3 scripts/check_observability.py --base-url "http://localhost:8082" --grafana-url "http://localhost:3006" --incidents
|
||||
SIGNALS trace=4a2dec658965c2c25e0e33c7f357c738 request_id=99d9ad1341dd202e5b6f22975ae3e9b8 spans=22 metrics=16 panels=17
|
||||
Container messenger-lab6-worker-1 Stopping
|
||||
Container messenger-lab6-worker-1 Stopped
|
||||
Container messenger-lab6-postgres-1 Waiting
|
||||
Container messenger-lab6-s3-1 Waiting
|
||||
Container messenger-lab6-s3-1 Healthy
|
||||
Container messenger-lab6-postgres-1 Healthy
|
||||
Container messenger-lab6-s3-bootstrap-1 Starting
|
||||
Container messenger-lab6-migrate-1 Starting
|
||||
Container messenger-lab6-s3-bootstrap-1 Started
|
||||
Container messenger-lab6-migrate-1 Started
|
||||
Container messenger-lab6-broker-1 Waiting
|
||||
Container messenger-lab6-migrate-1 Waiting
|
||||
Container messenger-lab6-s3-bootstrap-1 Waiting
|
||||
Container messenger-lab6-broker-1 Healthy
|
||||
Container messenger-lab6-s3-bootstrap-1 Exited
|
||||
Container messenger-lab6-migrate-1 Exited
|
||||
Container messenger-lab6-worker-1 Starting
|
||||
Container messenger-lab6-worker-1 Started
|
||||
QUEUE firing → resolved; depth=1 oldest=19.9s trace=2cb55e0cec7f29110c363a2a581ca265 retry linked
|
||||
Container messenger-lab6-postgres-1 Stopping
|
||||
Container messenger-lab6-postgres-1 Stopped
|
||||
Container messenger-lab6-postgres-1 Starting
|
||||
Container messenger-lab6-postgres-1 Started
|
||||
DB firing → resolved; 503=44 ratio=73.0% trace=83aa5b76b36831200387fe72ba38c175 failed_span=postgres.pool.acquire
|
||||
Container messenger-lab6-tempo-1 Stopping
|
||||
Container messenger-lab6-loki-1 Stopping
|
||||
Container messenger-lab6-tempo-1 Stopped
|
||||
Container messenger-lab6-loki-1 Stopped
|
||||
Container messenger-lab6-loki-1 Starting
|
||||
Container messenger-lab6-tempo-1 Starting
|
||||
Container messenger-lab6-tempo-1 Started
|
||||
Container messenger-lab6-loki-1 Started
|
||||
STORAGE successful writes=450 dropped logs/spans=2017 peak collector RSS=213.9 MiB; history preserved and new signals ingested
|
||||
@@ -0,0 +1,4 @@
|
||||
messenger-lab6-api-1 49.66% 81.23MiB / 512MiB
|
||||
messenger-lab6-api-2 99.58% 81.36MiB / 512MiB
|
||||
messenger-lab6-worker-1 0.34% 77.75MiB / 1.75GiB
|
||||
messenger-lab6-otel-collector-1 3.29% 65.54MiB / 256MiB
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"lab": 6,
|
||||
"workload": "closed-loop POST messages; no automatic retries",
|
||||
"requested_duration_seconds": 30.0,
|
||||
"elapsed_seconds": 33.104,
|
||||
"concurrency": 4,
|
||||
"successful_requests": 3797,
|
||||
"failed_requests": 4,
|
||||
"error_ratio": 0.001052,
|
||||
"successful_rps": 114.697,
|
||||
"successful_latency_ms": {
|
||||
"p50": 14.476,
|
||||
"p95": 63.627,
|
||||
"p99": 92.915
|
||||
},
|
||||
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
./scripts/check_migrations.sh
|
||||
2026-11-23 16:54:45.450 | INFO | infrastructure.dependencies.database:create_pool:71 - Установка соединения с базой данных psql
|
||||
2026-11-23 16:54:45.523 | INFO | infrastructure.dependencies.database:create_pool:101 - Соединение с базой данных psql установлено. DB_NAME: test, pool: 2..10
|
||||
пустой volume: таблиц в public = 0
|
||||
API до migrate: В БД не применены миграции [1, 2, 3, 4, 5, 6]: сначала запустите migrate
|
||||
2026-11-23 16:54:45.540 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0001_init.sql
|
||||
2026-11-23 16:54:45.541 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0002_message_idempotency.sql
|
||||
2026-11-23 16:54:45.546 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0003_attachments.sql
|
||||
2026-11-23 16:54:45.553 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0004_attachment_jobs.sql
|
||||
2026-11-23 16:54:45.554 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0005_job_error_code.sql
|
||||
2026-11-23 16:54:45.554 | INFO | infrastructure.dependencies.database:apply_migrations:154 - Применена миграция 0006_job_trace_context.sql
|
||||
2026-11-23 16:54:45.555 | INFO | infrastructure.dependencies.database:apply_migrations:159 - Схема БД в актуальной версии
|
||||
запуск 1: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency'), (3, '0003_attachments'), (4, '0004_attachment_jobs'), (5, '0005_job_error_code'), (6, '0006_job_trace_context')], таблиц = 13
|
||||
2026-11-23 16:54:45.556 | INFO | infrastructure.dependencies.database:apply_migrations:159 - Схема БД в актуальной версии
|
||||
запуск 2: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency'), (3, '0003_attachments'), (4, '0004_attachment_jobs'), (5, '0005_job_error_code'), (6, '0006_job_trace_context')], таблиц = 13
|
||||
2026-11-23 16:54:45.556 | INFO | infrastructure.dependencies.database:apply_migrations:159 - Схема БД в актуальной версии
|
||||
запуск 3: schema_migrations = [(1, '0001_init'), (2, '0002_message_idempotency'), (3, '0003_attachments'), (4, '0004_attachment_jobs'), (5, '0005_job_error_code'), (6, '0006_job_trace_context')], таблиц = 13
|
||||
2026-11-23 16:54:45.557 | INFO | infrastructure.dependencies.database:check_migrations:181 - Схема БД совместима: версия 6
|
||||
API после migrate: схема совместима
|
||||
@@ -0,0 +1,4 @@
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8082"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
PASS: historical metrics, logs and spans are still accessible via Grafana after restarting the stack.
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 tests/smoke.py --base-url "http://localhost:8082"
|
||||
PASS: lab6 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1 @@
|
||||
================== 195 passed, 23 skipped in 61.04s (0:01:01) ==================
|
||||
@@ -0,0 +1,675 @@
|
||||
{
|
||||
"uid": "messenger-lab6",
|
||||
"title": "Messenger · наблюдаемость",
|
||||
"tags": [
|
||||
"lab6",
|
||||
"messenger"
|
||||
],
|
||||
"schemaVersion": 41,
|
||||
"timezone": "browser",
|
||||
"version": 1,
|
||||
"refresh": "5s",
|
||||
"time": {
|
||||
"from": "now-15m",
|
||||
"to": "now"
|
||||
},
|
||||
"panels": [
|
||||
{
|
||||
"id": 1,
|
||||
"type": "timeseries",
|
||||
"title": "HTTP — запросы в секунду",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "reqps"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (method, route) (rate(messenger_http_requests_total[1m]))",
|
||||
"legendFormat": "{{method}} {{route}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"type": "timeseries",
|
||||
"title": "HTTP — длительность",
|
||||
"description": "Перцентили из суммарных histogram buckets всех API-реплик за 2 минуты.",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 0,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.5, sum by (le) (rate(messenger_http_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p50"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_http_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p95"
|
||||
},
|
||||
{
|
||||
"refId": "C",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.99, sum by (le) (rate(messenger_http_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p99"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"type": "timeseries",
|
||||
"title": "HTTP — доля 5xx",
|
||||
"description": "Только /api/v1. Health и Swagger исключены. 4xx входят в знаменатель.",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 8,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "percentunit"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "messenger:http_error_ratio:30s",
|
||||
"legendFormat": "5xx / все запросы"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"type": "timeseries",
|
||||
"title": "SLO доступности API за 15 минут",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 8,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "percentunit"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "messenger:http_availability:15m",
|
||||
"legendFormat": "SLI"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "vector(0.99)",
|
||||
"legendFormat": "SLO 99%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"type": "timeseries",
|
||||
"title": "Worker — исходы обработки",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 16,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "ops"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (outcome) (rate(messenger_jobs_total[1m]))",
|
||||
"legendFormat": "{{outcome}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"type": "timeseries",
|
||||
"title": "Worker — время обработки",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 16,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.5, sum by (le) (rate(messenger_job_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p50"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_job_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p95"
|
||||
},
|
||||
{
|
||||
"refId": "C",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.99, sum by (le) (rate(messenger_job_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p99"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"type": "timeseries",
|
||||
"title": "Очередь — ожидающие задания",
|
||||
"description": "Устойчивый backlog в PostgreSQL включает задачи до publish и после publish, пока worker их не захватил.",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 24,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "max(messenger_queue_depth)",
|
||||
"legendFormat": "queued"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "max(messenger_outbox_pending)",
|
||||
"legendFormat": "не отправлено в RabbitMQ"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"type": "timeseries",
|
||||
"title": "Очередь — возраст старейшей job",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 24,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "max by (queue) (messenger_queue_oldest_age_seconds)",
|
||||
"legendFormat": "{{queue}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"type": "timeseries",
|
||||
"title": "Время от 202 до ready",
|
||||
"description": "От создания job в транзакции принятия до COMMIT готового вложения.",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 32,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.5, sum by (le) (rate(messenger_attachment_ready_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p50"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_attachment_ready_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p95"
|
||||
},
|
||||
{
|
||||
"refId": "C",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.99, sum by (le) (rate(messenger_attachment_ready_duration_seconds_bucket[2m])))",
|
||||
"legendFormat": "p99"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"type": "timeseries",
|
||||
"title": "Worker — занятые слоты",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 32,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "percentunit"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum(messenger_worker_busy) / sum(messenger_worker_capacity)",
|
||||
"legendFormat": "busy / capacity"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"type": "timeseries",
|
||||
"title": "PostgreSQL — пул соединений",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 40,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (service_name, state) (messenger_db_pool{state=~\"used|waiting|capacity\"})",
|
||||
"legendFormat": "{{service_name}} {{state}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"type": "timeseries",
|
||||
"title": "CPU — ядра",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 40,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "cores"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (service_name) (rate(messenger_process_cpu_seconds_total[1m]))",
|
||||
"legendFormat": "процесс {{service_name}}"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (service_name) (rate(messenger_container_cpu_seconds_total[1m]))",
|
||||
"legendFormat": "контейнер {{service_name}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 13,
|
||||
"type": "timeseries",
|
||||
"title": "RAM — процесс и контейнер",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 48,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "bytes"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (service_name) (messenger_process_memory_bytes)",
|
||||
"legendFormat": "RSS {{service_name}}"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum by (service_name) (messenger_container_memory_bytes)",
|
||||
"legendFormat": "cgroup {{service_name}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 14,
|
||||
"type": "timeseries",
|
||||
"title": "Алерты",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 48,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "ALERTS",
|
||||
"legendFormat": "{{alertname}} {{alertstate}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 15,
|
||||
"type": "timeseries",
|
||||
"title": "Collector — потери записей",
|
||||
"description": "",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 56,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum(increase(otelcol_exporter_send_failed_log_records_total[1m])) + sum(increase(otelcol_exporter_enqueue_failed_log_records_total[1m]))",
|
||||
"legendFormat": "логи {{exporter}}"
|
||||
},
|
||||
{
|
||||
"refId": "B",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "sum(increase(otelcol_exporter_send_failed_spans_total[1m])) + sum(increase(otelcol_exporter_enqueue_failed_spans_total[1m]))",
|
||||
"legendFormat": "спаны {{exporter}}"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 16,
|
||||
"type": "timeseries",
|
||||
"title": "Collector — память",
|
||||
"description": "RSS процесса Collector. Лимит контейнера 256 MiB.",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 56,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "bytes"
|
||||
},
|
||||
"overrides": []
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "prometheus"
|
||||
},
|
||||
"expr": "otelcol_process_memory_rss_bytes",
|
||||
"legendFormat": "RSS"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 17,
|
||||
"type": "logs",
|
||||
"title": "API, dispatcher и worker",
|
||||
"datasource": {
|
||||
"type": "loki",
|
||||
"uid": "loki"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 64,
|
||||
"w": 24,
|
||||
"h": 12
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "loki",
|
||||
"uid": "loki"
|
||||
},
|
||||
"expr": "{service_name=~\"messenger-api|messenger-worker|messenger-dispatcher\"} | event=~\"http.response|job.processed|job.processing_error|postgres.unavailable\""
|
||||
}
|
||||
],
|
||||
"options": {
|
||||
"showTime": true,
|
||||
"showLabels": true,
|
||||
"enableLogDetails": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
apiVersion: 1
|
||||
providers:
|
||||
- name: Messenger
|
||||
folder: Lab6
|
||||
type: file
|
||||
options:
|
||||
path: /etc/grafana/dashboards
|
||||
@@ -0,0 +1,39 @@
|
||||
apiVersion: 1
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
uid: prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
editable: false
|
||||
jsonData:
|
||||
timeInterval: 2s
|
||||
- name: Loki
|
||||
uid: loki
|
||||
type: loki
|
||||
access: proxy
|
||||
url: http://loki:3100
|
||||
editable: false
|
||||
jsonData:
|
||||
derivedFields:
|
||||
- name: TraceID
|
||||
matcherType: label
|
||||
matcherRegex: trace_id
|
||||
datasourceUid: tempo
|
||||
url: '$${__value.raw}'
|
||||
- name: Tempo
|
||||
uid: tempo
|
||||
type: tempo
|
||||
access: proxy
|
||||
url: http://tempo:3200
|
||||
editable: false
|
||||
jsonData:
|
||||
tracesToLogsV2:
|
||||
datasourceUid: loki
|
||||
spanStartTimeShift: '-2m'
|
||||
spanEndTimeShift: '2m'
|
||||
filterByTraceID: true
|
||||
tags:
|
||||
- key: service.name
|
||||
value: service_name
|
||||
@@ -0,0 +1,39 @@
|
||||
auth_enabled: false
|
||||
server:
|
||||
http_listen_port: 3100
|
||||
analytics:
|
||||
reporting_enabled: false
|
||||
common:
|
||||
instance_addr: 127.0.0.1
|
||||
path_prefix: /loki
|
||||
storage:
|
||||
filesystem:
|
||||
chunks_directory: /loki/chunks
|
||||
rules_directory: /loki/rules
|
||||
replication_factor: 1
|
||||
ring:
|
||||
kvstore:
|
||||
store: inmemory
|
||||
schema_config:
|
||||
configs:
|
||||
- from: 2024-01-01
|
||||
store: tsdb
|
||||
object_store: filesystem
|
||||
schema: v13
|
||||
index:
|
||||
prefix: index_
|
||||
period: 24h
|
||||
limits_config:
|
||||
allow_structured_metadata: true
|
||||
discover_log_levels: true
|
||||
retention_period: 168h
|
||||
otlp_config:
|
||||
resource_attributes:
|
||||
ignore_defaults: true
|
||||
attributes_config:
|
||||
- action: index_label
|
||||
attributes: [service.name, deployment.environment.name]
|
||||
compactor:
|
||||
working_directory: /loki/compactor
|
||||
retention_enabled: true
|
||||
delete_request_store: filesystem
|
||||
@@ -0,0 +1,15 @@
|
||||
# Ошибки, созданные самим балансировщиком, — в формате ошибок контракта.
|
||||
# Ответы реплик (включая их 503) проходят без изменений: proxy_intercept_errors off.
|
||||
# X-Instance-Id здесь нет: ответ создан LB, а не API-репликой.
|
||||
|
||||
location @unavailable {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
return 503 '{"error":{"code":"unavailable","message":"Нет доступной API-реплики","request_id":"$request_id"}}';
|
||||
}
|
||||
|
||||
location @too_large {
|
||||
default_type application/json;
|
||||
add_header X-Request-Id $request_id always;
|
||||
return 413 '{"error":{"code":"payload_too_large","message":"Тело запроса слишком большое","request_id":"$request_id"}}';
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
worker_processes 1;
|
||||
pid /tmp/nginx.pid;
|
||||
error_log /dev/stderr warn;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
|
||||
log_format upstream_json escape=json
|
||||
'{"time":"$time_iso8601","request_id":"$request_id",'
|
||||
'"method":"$request_method","uri":"$uri","status":$status,'
|
||||
'"request_time":$request_time,"upstream_addr":"$upstream_addr",'
|
||||
'"upstream_status":"$upstream_status",'
|
||||
'"upstream_response_time":"$upstream_response_time",'
|
||||
'"instance":"$upstream_http_x_instance_id"}';
|
||||
access_log /dev/stdout upstream_json;
|
||||
|
||||
server_tokens off;
|
||||
client_max_body_size 1m;
|
||||
client_body_buffer_size 64k;
|
||||
client_header_timeout 10s;
|
||||
client_body_timeout 10s;
|
||||
send_timeout 10s;
|
||||
keepalive_timeout 65s;
|
||||
|
||||
upstream api {
|
||||
zone api 64k;
|
||||
server api:8080 resolve max_fails=1 fail_timeout=5s;
|
||||
keepalive 32;
|
||||
# Меньше APP_KEEPALIVE_TIMEOUT_SEC (75 с): idle-соединение первым
|
||||
# закрывает nginx, а не реплика, — нет гонки «запрос в закрытое соединение»
|
||||
keepalive_timeout 60s;
|
||||
}
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
# Один ID на LB и реплику: API принимает $request_id и возвращает его
|
||||
proxy_set_header X-Request-Id $request_id;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# connect внутри bridge-сети завершает ядро за доли мс даже у занятой реплики.
|
||||
# Самый долгий запрос API: ожидание пула 3 с + statement_timeout 5 с
|
||||
proxy_connect_timeout 500ms;
|
||||
proxy_send_timeout 10s;
|
||||
proxy_read_timeout 10s;
|
||||
|
||||
# Повтор на другой реплике только при ошибке соединения/таймауте.
|
||||
# POST, уже отправленный реплике, nginx не повторяет (non_idempotent выключен):
|
||||
# повтор сообщения делает клиент с тем же Idempotency-Key.
|
||||
# 503 приложения (общая зависимость недоступна) не повторяется и не исключает
|
||||
# реплику: другая реплика зависит от той же БД.
|
||||
proxy_next_upstream error timeout;
|
||||
proxy_next_upstream_tries 3;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
location / {
|
||||
proxy_pass http://api;
|
||||
}
|
||||
|
||||
# Загрузка вложения. Лимит LB чуть больше лимита API (10 MiB): тело
|
||||
# 10 MiB + 1 отклоняет сама реплика ответом 413 по контракту, а LB
|
||||
# отсекает только заведомо огромные тела.
|
||||
# Тело НЕ буферизуется в nginx, а идёт потоком в API (там память
|
||||
# на загрузку ограничена spool 1 MiB). С буферизацией 10 одновременных
|
||||
# тел по 9 MiB во временных файлах nginx давали грязный page cache
|
||||
# сверх лимита контейнера 64M и OOM kill воркеров (эксперимент
|
||||
# evidence/experiment-uploads.json). Медленный клиент занимает в API
|
||||
# только корутину; client_body_timeout обрывает зависшую передачу.
|
||||
# read timeout больше ATTACHMENT_STORE_TIMEOUT_SEC (30 с) + проверки.
|
||||
location ~ ^/api/v1/chats/[^/]+/attachments$ {
|
||||
client_max_body_size 11m;
|
||||
proxy_request_buffering off;
|
||||
proxy_read_timeout 45s;
|
||||
proxy_pass http://api;
|
||||
}
|
||||
|
||||
error_page 502 503 504 = @unavailable;
|
||||
error_page 413 = @too_large;
|
||||
|
||||
include /etc/nginx/errors.conf;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8081;
|
||||
|
||||
# Реплика api-short-ttl только для make test-expiry (профиль expiry):
|
||||
# адрес резолвится на каждый запрос, без профиля — 503 от LB
|
||||
location / {
|
||||
set $api_short_ttl http://api-short-ttl:8080;
|
||||
proxy_pass $api_short_ttl;
|
||||
}
|
||||
|
||||
error_page 502 503 504 = @unavailable;
|
||||
error_page 413 = @too_large;
|
||||
|
||||
include /etc/nginx/errors.conf;
|
||||
}
|
||||
|
||||
# Liveness самого балансировщика для healthcheck контейнера; наружу не публикуется
|
||||
server {
|
||||
listen 127.0.0.1:8090;
|
||||
access_log off;
|
||||
|
||||
location = /healthz {
|
||||
default_type text/plain;
|
||||
return 200 "ok\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
# Конфигурация наблюдаемости — реализовать
|
||||
# Наблюдаемость Messenger
|
||||
|
||||
Добавьте `collector.yaml` с OTLP receivers, processors и exporters выбранной версии Collector. Укажите явные bind endpoints, внутренние сети, memory limits/batch и поведение при backpressure по уровню. Сопоставьте все три pipeline с реально развёрнутыми backend.
|
||||
API, dispatcher и worker отправляют метрики, логи и трассы по OTLP/HTTP в `otel-collector`. Collector отдаёт метрики на scrape Prometheus и отправляет логи в Loki, трассы в Tempo. Grafana получает все три источника и дашборд из `infra/grafana`.
|
||||
|
||||
Добавьте конфигурации storage, Grafana provisioning/datasources, provisioning/dashboards и импортируемые dashboard JSON. На 4 — alert rules и runbooks. Пустой dashboard или debug exporter, который только печатает данные в stdout, не заменяют хранилища/доказательства из `contracts/observability.md`.
|
||||
Collector использует memory limiter и batch. Экспортёры Loki/Tempo имеют отдельные очереди и ограниченный повтор. Порты OTLP, Prometheus, Loki и Tempo доступны в сетях Compose. Grafana доступна только через loopback хоста на порту 3006.
|
||||
|
||||
Не публикуйте OTLP/metrics/административные ports в общую сеть без необходимости. В lab7 реализуйте TLS/mTLS по trust matrix; экспорт Authorization и message bodies запрещён.
|
||||
Конфиги лежат в `infra/otel`, `infra/prometheus`, `infra/loki`, `infra/tempo`. Сценарии диагностики описаны в [runbook.md](runbook.md).
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
extensions:
|
||||
health_check:
|
||||
endpoint: 0.0.0.0:13133
|
||||
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
max_request_body_size: 4194304
|
||||
|
||||
processors:
|
||||
memory_limiter:
|
||||
check_interval: 1s
|
||||
limit_mib: 160
|
||||
spike_limit_mib: 32
|
||||
batch:
|
||||
timeout: 1s
|
||||
send_batch_size: 128
|
||||
send_batch_max_size: 128
|
||||
|
||||
exporters:
|
||||
prometheus:
|
||||
endpoint: 0.0.0.0:9464
|
||||
metric_expiration: 30s
|
||||
resource_to_telemetry_conversion:
|
||||
enabled: true
|
||||
otlp_http/loki:
|
||||
endpoint: http://loki:3100/otlp
|
||||
timeout: 2s
|
||||
sending_queue:
|
||||
num_consumers: 1
|
||||
queue_size: 16
|
||||
retry_on_failure:
|
||||
initial_interval: 1s
|
||||
max_interval: 3s
|
||||
max_elapsed_time: 8s
|
||||
otlp_http/tempo:
|
||||
endpoint: http://tempo:4318
|
||||
timeout: 2s
|
||||
sending_queue:
|
||||
num_consumers: 1
|
||||
queue_size: 16
|
||||
retry_on_failure:
|
||||
initial_interval: 1s
|
||||
max_interval: 3s
|
||||
max_elapsed_time: 8s
|
||||
|
||||
service:
|
||||
extensions: [health_check]
|
||||
telemetry:
|
||||
metrics:
|
||||
level: detailed
|
||||
readers:
|
||||
- pull:
|
||||
exporter:
|
||||
prometheus:
|
||||
host: 0.0.0.0
|
||||
port: 8888
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [prometheus]
|
||||
logs:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlp_http/loki]
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlp_http/tempo]
|
||||
@@ -0,0 +1,19 @@
|
||||
# Диагностика стенда Lab6
|
||||
|
||||
Откройте дашборд **Messenger · наблюдаемость** в Grafana на порту 3006. В Explore доступны Prometheus, Loki и Tempo. Для перехода от HTTP-ответа к трассе возьмите `X-Trace-Id` и найдите Trace ID в Tempo. Связанный лог в Loki находится запросом `{service_name="messenger-api"} | trace_id="<id>"`. Для worker замените имя сервиса на `messenger-worker`.
|
||||
|
||||
## Queue lag
|
||||
|
||||
1. Проверьте `max(messenger_queue_oldest_age_seconds)`, `max(messenger_queue_depth)` и `max(messenger_outbox_pending)`.
|
||||
2. Если возраст растёт, outbox пуст, а актуальный ряд `messenger_worker_capacity` исчез, проверяйте запущен ли worker. Если worker работает, сравните `messenger_worker_busy` с его capacity и разберите ошибки `job.processed` в Loki.
|
||||
3. После запуска worker убедитесь, что глубина очереди вернулась к нулю, `MessengerQueueLag` снялся и вложение стало ready. В Tempo проследите publish, consume, запись S3 и фиксацию результата PostgreSQL.
|
||||
|
||||
## Error rate
|
||||
|
||||
1. Проверьте `messenger:http_error_ratio:30s` и разложите запросы по статусам и маршрутам. Алерт `MessengerHighErrorRate` реагирует на 5xx.
|
||||
2. Откройте лог `{service_name="messenger-api"} | event="http.response" | status_code>=500` и перейдите в Tempo по Trace ID. Ошибка в спане `postgres.pool.acquire` или репозитории и событие `postgres.unavailable` в той же трассе указывают на PostgreSQL.
|
||||
3. После восстановления проверьте успешную запись сообщения и дождитесь выхода 5xx из окна алерта.
|
||||
|
||||
## Экспорт телеметрии
|
||||
|
||||
Проверьте `otelcol_exporter_send_failed_log_records_total`, `otelcol_exporter_send_failed_spans_total`, `otelcol_exporter_queue_size` и `otelcol_process_memory_rss_bytes`. При недоступности Loki или Tempo возможна потеря пакетов после исчерпания очереди и времени повтора. После восстановления отправьте новый запрос и проверьте наличие лога и трассы. Сценарии воспроизводятся командой `make test-observability-incidents` на локальном стенде lab6.
|
||||
@@ -0,0 +1,15 @@
|
||||
global:
|
||||
scrape_interval: 2s
|
||||
scrape_timeout: 1s
|
||||
evaluation_interval: 2s
|
||||
|
||||
rule_files:
|
||||
- /etc/prometheus/rules.yaml
|
||||
|
||||
scrape_configs:
|
||||
- job_name: messenger
|
||||
static_configs:
|
||||
- targets: [otel-collector:9464]
|
||||
- job_name: collector
|
||||
static_configs:
|
||||
- targets: [otel-collector:8888]
|
||||
@@ -0,0 +1,31 @@
|
||||
groups:
|
||||
- name: messenger
|
||||
rules:
|
||||
- record: messenger:http_error_ratio:30s
|
||||
expr: |
|
||||
(sum(rate(messenger_http_requests_total{status_class="5xx"}[30s])) or vector(0))
|
||||
/ sum(rate(messenger_http_requests_total[30s]))
|
||||
- record: messenger:http_availability:15m
|
||||
expr: |
|
||||
1 - (sum(increase(messenger_http_requests_total{status_class="5xx"}[15m])) or vector(0))
|
||||
/ sum(increase(messenger_http_requests_total[15m]))
|
||||
- alert: MessengerHighErrorRate
|
||||
expr: messenger:http_error_ratio:30s > 0.05 and sum(increase(messenger_http_requests_total[30s])) >= 5
|
||||
for: 6s
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: API возвращает более 5% ответов 5xx
|
||||
description: За 30 секунд наблюдалось минимум 5 бизнес-запросов.
|
||||
runbook: infra/otel/runbook.md#error-rate
|
||||
- alert: MessengerQueueLag
|
||||
expr: |
|
||||
max(messenger_queue_oldest_age_seconds) > 10
|
||||
and max(messenger_queue_depth) > 0
|
||||
for: 6s
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: Старейшее ожидающее вложение в очереди более 10 секунд
|
||||
description: Проверьте worker capacity, backlog и ожидающие события outbox.
|
||||
runbook: infra/otel/runbook.md#queue-lag
|
||||
@@ -0,0 +1,35 @@
|
||||
server:
|
||||
http_listen_port: 3200
|
||||
usage_report:
|
||||
reporting_enabled: false
|
||||
distributor:
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
ingester:
|
||||
max_block_duration: 5m
|
||||
compactor:
|
||||
compaction:
|
||||
block_retention: 168h
|
||||
metrics_generator:
|
||||
storage:
|
||||
path: /var/tempo/generator/wal
|
||||
traces_storage:
|
||||
path: /var/tempo/generator/traces
|
||||
processor:
|
||||
local_blocks:
|
||||
filter_server_spans: false
|
||||
flush_to_storage: true
|
||||
overrides:
|
||||
defaults:
|
||||
metrics_generator:
|
||||
processors: [local-blocks]
|
||||
storage:
|
||||
trace:
|
||||
backend: local
|
||||
wal:
|
||||
path: /var/tempo/wal
|
||||
local:
|
||||
path: /var/tempo/blocks
|
||||
@@ -0,0 +1,60 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
username text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT users_username_key UNIQUE (username),
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- Источник истины для сессий; токен хранится только как SHA-256
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL,
|
||||
revoked_at timestamptz,
|
||||
CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32),
|
||||
CONSTRAINT sessions_expiry_order CHECK (expires_at > created_at)
|
||||
);
|
||||
|
||||
CREATE INDEX sessions_user_id_idx ON sessions (user_id);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
title text NOT NULL,
|
||||
created_by uuid NOT NULL REFERENCES users (id),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- position сохраняет порядок member_ids из запроса создания
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
position smallint NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
CONSTRAINT chat_members_position_key UNIQUE (chat_id, position)
|
||||
);
|
||||
|
||||
-- Список чатов пользователя: WHERE user_id = $1
|
||||
CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
-- created_at назначает приложение под блокировкой чата (строго возрастает в чате)
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
-- отправитель обязан быть участником этого чата
|
||||
CONSTRAINT messages_sender_member_fkey FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Keyset-пагинация истории: WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
-- ORDER BY created_at, id LIMIT $4 — Index Scan без сортировки и полного прохода
|
||||
CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Idempotency-Key отправки сообщения (lab3). Область по контракту:
|
||||
-- пользователь + метод + путь + ключ. Метод и путь фиксированы
|
||||
-- (POST /api/v1/chats/{chat_id}/messages), поэтому ключ — (sender_id, chat_id, key).
|
||||
-- Только добавление таблицы (expand): предыдущая версия API работает на новой схеме.
|
||||
CREATE TABLE message_idempotency_keys (
|
||||
sender_id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL,
|
||||
idempotency_key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, idempotency_key),
|
||||
CONSTRAINT message_idempotency_keys_key_format
|
||||
CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
CONSTRAINT message_idempotency_keys_hash_len CHECK (octet_length(request_hash) = 32),
|
||||
CONSTRAINT message_idempotency_keys_expiry_order CHECK (expires_at > created_at)
|
||||
);
|
||||
@@ -0,0 +1,69 @@
|
||||
-- Вложения (lab4): метаданные в PostgreSQL, байты — в приватном bucket S3.
|
||||
-- Только расширение схемы (expand): API lab3 продолжает работать на новой схеме.
|
||||
|
||||
-- Строка создаётся ДО записи объекта в S3 (status = pending) и удаляется только
|
||||
-- ПОСЛЕ удаления объекта (через deleting). Поэтому у каждого объекта S3 есть
|
||||
-- строка, и janitor находит незавершённые загрузки по БД, а не обходом bucket.
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
chat_id uuid NOT NULL,
|
||||
owner_id uuid NOT NULL,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes integer NOT NULL,
|
||||
sha256 bytea NOT NULL,
|
||||
-- назначает сервер (originals/<random>); filename в ключ не входит
|
||||
object_key text NOT NULL,
|
||||
status text NOT NULL DEFAULT 'pending',
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT attachments_object_key_key UNIQUE (object_key),
|
||||
-- цель составного FK из message_attachments: вложение того же чата
|
||||
CONSTRAINT attachments_id_chat_id_key UNIQUE (id, chat_id),
|
||||
-- загружать может только участник чата; без CASCADE: молча удалённая
|
||||
-- строка оставила бы объект S3 неучтённым
|
||||
CONSTRAINT attachments_owner_member_fkey FOREIGN KEY (chat_id, owner_id)
|
||||
REFERENCES chat_members (chat_id, user_id),
|
||||
CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('pending', 'ready', 'deleting')),
|
||||
CONSTRAINT attachments_content_type_check
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_bytes_range CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_len CHECK (octet_length(sha256) = 32),
|
||||
CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255)
|
||||
);
|
||||
|
||||
-- janitor: WHERE status <> 'ready' AND created_at < now() - grace;
|
||||
-- готовых строк подавляющее большинство, в индекс они не попадают
|
||||
CREATE INDEX attachments_unfinished_created_at_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status <> 'ready';
|
||||
|
||||
-- Пустой текст допустим, если у сообщения есть вложения (проверяет API:
|
||||
-- условие охватывает две таблицы). Ограничение только ослабляется.
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_len;
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||
|
||||
-- цель составного FK из message_attachments: сообщение того же чата
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_id_key UNIQUE (id, chat_id);
|
||||
|
||||
-- position сохраняет порядок attachment_ids из запроса
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL,
|
||||
attachment_id uuid NOT NULL,
|
||||
position smallint NOT NULL,
|
||||
PRIMARY KEY (message_id, position),
|
||||
CONSTRAINT message_attachments_attachment_key UNIQUE (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_message_fkey FOREIGN KEY (message_id, chat_id)
|
||||
REFERENCES messages (id, chat_id) ON DELETE CASCADE,
|
||||
-- вложение принадлежит чату сообщения; используемое вложение не удалить
|
||||
CONSTRAINT message_attachments_attachment_fkey FOREIGN KEY (attachment_id, chat_id)
|
||||
REFERENCES attachments (id, chat_id),
|
||||
CONSTRAINT message_attachments_position_range CHECK (position BETWEEN 0 AND 9)
|
||||
);
|
||||
|
||||
-- Проверка FK при удалении вложения: без индекса — полный проход таблицы
|
||||
CREATE INDEX message_attachments_attachment_id_idx
|
||||
ON message_attachments (attachment_id);
|
||||
@@ -0,0 +1,57 @@
|
||||
-- Durable handoff: staging + queued/job/outbox фиксируются до ответа 202.
|
||||
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed', 'deleting'));
|
||||
ALTER TABLE attachments ADD COLUMN staging_key text;
|
||||
ALTER TABLE attachments ADD COLUMN error_code text;
|
||||
UPDATE attachments SET staging_key = object_key WHERE status IN ('pending', 'deleting');
|
||||
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY DEFAULT uuidv7(),
|
||||
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
|
||||
pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version = 1),
|
||||
attempt integer NOT NULL DEFAULT 1 CHECK (attempt > 0),
|
||||
failures integer NOT NULL DEFAULT 0 CHECK (failures >= 0),
|
||||
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
lease_token uuid,
|
||||
lease_until timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until)
|
||||
WHERE lease_until IS NOT NULL;
|
||||
|
||||
CREATE TABLE attachment_outbox (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
attempt integer NOT NULL CHECK (attempt > 0),
|
||||
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
published_at timestamptz,
|
||||
claim_token uuid,
|
||||
claim_until timestamptz,
|
||||
UNIQUE (job_id, attempt)
|
||||
);
|
||||
CREATE INDEX attachment_outbox_pending_idx ON attachment_outbox (available_at, id)
|
||||
WHERE published_at IS NULL;
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
pipeline_version integer NOT NULL CHECK (pipeline_version = 1),
|
||||
name text NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL,
|
||||
size_bytes integer NOT NULL CHECK (size_bytes > 0),
|
||||
width integer NOT NULL CHECK (width > 0),
|
||||
height integer NOT NULL CHECK (height > 0),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
||||
PRIMARY KEY (attachment_id, pipeline_version, name)
|
||||
);
|
||||
|
||||
-- failed + error_code в attachments — устойчивый реестр ошибок вместо DLQ.
|
||||
CREATE TABLE attachment_replays (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
attempt integer NOT NULL,
|
||||
operator text NOT NULL CHECK (char_length(operator) BETWEEN 1 AND 128),
|
||||
reason text NOT NULL CHECK (char_length(reason) BETWEEN 1 AND 512),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Сохраняем безопасную причину временной ошибки после retry_exhausted.
|
||||
ALTER TABLE attachment_jobs ADD COLUMN last_error_code text;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Контекст хранится вместе с намерением обработки и переживает рестарт outbox.
|
||||
ALTER TABLE attachment_jobs ADD COLUMN traceparent text;
|
||||
ALTER TABLE attachment_jobs ADD COLUMN tracestate text;
|
||||
Generated
+2427
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
[tool.poetry]
|
||||
name = "max2"
|
||||
version = "1.6.0"
|
||||
description = "Мессенджер с OpenTelemetry и Prometheus, Loki и Tempo, лабораторная 6"
|
||||
authors = ["Саватеев Артём"]
|
||||
package-mode = false
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.12,<3.13"
|
||||
fastapi = "0.142.2"
|
||||
uvicorn = "0.54.0"
|
||||
pydantic = "2.13.5"
|
||||
envparse = "0.2.0"
|
||||
loguru = "0.7.3"
|
||||
asyncpg = "0.31.0"
|
||||
redis = "6.4.0"
|
||||
argon2-cffi = "25.1.0"
|
||||
aiobotocore = "3.9.1"
|
||||
pillow = "12.3.0"
|
||||
aio-pika = "9.5.8"
|
||||
opentelemetry-sdk = "1.45.2"
|
||||
opentelemetry-exporter-otlp-proto-http = "1.45.2"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
pytest = "9.1.1"
|
||||
httpx = "0.28.1"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["student-tests"]
|
||||
addopts = "-v"
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Миграции с нуля и повторно, отказ API на непримененной схеме: отдельный
|
||||
# compose-проект с собственным пустым volume. Рабочий стенд и его данные
|
||||
# не затрагиваются; временный volume этого проекта удаляется в конце.
|
||||
cd "$(dirname "$0")/.."
|
||||
PROJECT=messenger-lab6-migrations-check
|
||||
# Свои порты, чтобы не конфликтовать с основным стендом
|
||||
export LB_PORT="${MIGRATIONS_CHECK_LB_PORT:-58082}"
|
||||
export LB_EXPIRY_PORT="${MIGRATIONS_CHECK_LB_EXPIRY_PORT:-58083}"
|
||||
export DB_HOST_PORT="${MIGRATIONS_CHECK_DB_HOST_PORT:-55436}"
|
||||
COMPOSE="docker compose --env-file ${ENV_FILE:-.env} -p $PROJECT"
|
||||
trap '$COMPOSE down -v --remove-orphans >/dev/null 2>&1' EXIT
|
||||
|
||||
$COMPOSE up -d --wait postgres >/dev/null 2>&1
|
||||
# Тот же образ и сеть, что у сервиса migrate; apply_migrations трижды подряд
|
||||
$COMPOSE run --rm --no-deps -T migrate python - <<'EOF'
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, "src")
|
||||
|
||||
from infrastructure.dependencies.database import database_pool_manager as db
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
await db.create_pool(30)
|
||||
async with db.acquire() as conn:
|
||||
tables = await conn.fetchval(
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';"
|
||||
)
|
||||
print(f"пустой volume: таблиц в public = {tables}")
|
||||
# Так стартует API-реплика: без migrate она отказывается работать
|
||||
try:
|
||||
await db.check_migrations()
|
||||
except RuntimeError as error:
|
||||
print(f"API до migrate: {error}")
|
||||
else:
|
||||
raise SystemExit("FAIL: API стартовал бы на пустой схеме")
|
||||
for attempt in (1, 2, 3):
|
||||
await db.apply_migrations()
|
||||
async with db.acquire() as conn:
|
||||
versions = await conn.fetch(
|
||||
"SELECT version, name FROM schema_migrations ORDER BY version;"
|
||||
)
|
||||
tables = await conn.fetchval(
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';"
|
||||
)
|
||||
print(
|
||||
f"запуск {attempt}: schema_migrations = "
|
||||
f"{[(r['version'], r['name']) for r in versions]}, таблиц = {tables}"
|
||||
)
|
||||
await db.check_migrations()
|
||||
print("API после migrate: схема совместима")
|
||||
await db.close()
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
EOF
|
||||
@@ -0,0 +1,428 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка сигналов и отказов на локальном учебном Compose-стенде."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import urlopen
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure, check, run
|
||||
|
||||
BACKLOG_ALERT = "MessengerQueueLag"
|
||||
ERROR_ALERT = "MessengerHighErrorRate"
|
||||
UPLOAD_ROUTE = "/api/v1/chats/{chat_id}/attachments"
|
||||
|
||||
|
||||
def compose(*args, stdin=None):
|
||||
subprocess.run(
|
||||
["docker", "compose", "-p", "messenger-lab6", *args],
|
||||
cwd=ROOT,
|
||||
input=stdin,
|
||||
text=True,
|
||||
check=True,
|
||||
timeout=90,
|
||||
stdout=sys.stderr,
|
||||
)
|
||||
|
||||
|
||||
def sql(statement):
|
||||
compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
'psql -X -q -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"',
|
||||
stdin=statement,
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def stopped(*services):
|
||||
compose("stop", *services)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
compose("start", *services)
|
||||
|
||||
|
||||
def eventually(label, predicate, timeout=60):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
result = predicate()
|
||||
if result:
|
||||
return result
|
||||
except (URLError, TimeoutError, Failure):
|
||||
pass
|
||||
time.sleep(1)
|
||||
raise Failure(label)
|
||||
|
||||
|
||||
class Observations:
|
||||
def __init__(self, grafana):
|
||||
self.grafana = grafana.rstrip("/")
|
||||
|
||||
def get(self, path, **params):
|
||||
url = self.grafana + path + ("?" + urlencode(params) if params else "")
|
||||
with urlopen(url, timeout=5) as response:
|
||||
return json.load(response)
|
||||
|
||||
def ready(self, datasource):
|
||||
url = self.grafana + f"/api/datasources/proxy/uid/{datasource}/ready"
|
||||
with urlopen(url, timeout=5) as response:
|
||||
return response.status == 200
|
||||
|
||||
def query(self, expression):
|
||||
data = self.get(
|
||||
"/api/datasources/proxy/uid/prometheus/api/v1/query", query=expression
|
||||
)
|
||||
return data["data"]["result"]
|
||||
|
||||
def value(self, expression):
|
||||
rows = self.query(expression)
|
||||
return float(rows[0]["value"][1]) if rows else 0.0
|
||||
|
||||
def firing(self, name):
|
||||
alerts = self.get("/api/datasources/proxy/uid/prometheus/api/v1/alerts")[
|
||||
"data"
|
||||
]["alerts"]
|
||||
return any(
|
||||
alert["labels"]["alertname"] == name and alert["state"] == "firing"
|
||||
for alert in alerts
|
||||
)
|
||||
|
||||
def trace(self, trace_id):
|
||||
try:
|
||||
data = self.get("/api/datasources/proxy/uid/tempo/api/traces/" + trace_id)
|
||||
except HTTPError as error:
|
||||
if error.code == 404:
|
||||
return []
|
||||
raise
|
||||
return [
|
||||
(span, attributes(batch["resource"]).get("service.name"))
|
||||
for batch in data.get("batches", data.get("resourceSpans", []))
|
||||
for scope in batch["scopeSpans"]
|
||||
for span in scope["spans"]
|
||||
]
|
||||
|
||||
def logs(self, service, trace_id):
|
||||
data = self.get(
|
||||
"/api/datasources/proxy/uid/loki/loki/api/v1/query_range",
|
||||
query=f'{{service_name="{service}"}} | trace_id="{trace_id}"',
|
||||
limit=50,
|
||||
)
|
||||
return data["data"]["result"]
|
||||
|
||||
|
||||
def attributes(entity):
|
||||
return {
|
||||
item["key"]: next(iter(item["value"].values()))
|
||||
for item in entity.get("attributes", [])
|
||||
}
|
||||
|
||||
|
||||
def upload(client, actor, chat):
|
||||
body = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
name = "private-filename-" + uuid.uuid4().hex + ".png"
|
||||
response = client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/attachments?filename={name}",
|
||||
202,
|
||||
actor=actor,
|
||||
raw=body,
|
||||
headers={"Content-Type": "image/png"},
|
||||
schema="Attachment",
|
||||
)
|
||||
check(response["json"]["status"] == "queued", "upload did not return queued")
|
||||
trace_id = response["headers"].get("X-Trace-Id", "")
|
||||
check(bool(re.fullmatch(r"[0-9a-f]{32}", trace_id)), "missing trace ID")
|
||||
return response["json"], trace_id, response["headers"]["X-Request-Id"], name
|
||||
|
||||
|
||||
def chain(obs, trace_id, attempts):
|
||||
def lookup():
|
||||
spans = obs.trace(trace_id)
|
||||
producer = {
|
||||
int(attributes(span)["job.attempt"]): span
|
||||
for span, service in spans
|
||||
if service == "messenger-dispatcher" and span["name"] == "rabbitmq.publish"
|
||||
}
|
||||
consumer = {
|
||||
int(attributes(span)["job.attempt"]): span
|
||||
for span, service in spans
|
||||
if service == "messenger-worker" and span["name"] == "rabbitmq.consume"
|
||||
}
|
||||
names = {span["name"] for span, _ in spans}
|
||||
if not (
|
||||
{f"POST {UPLOAD_ROUTE}", "s3.put", "postgres.complete"} <= names
|
||||
and attempts <= producer.keys()
|
||||
and attempts <= consumer.keys()
|
||||
):
|
||||
return None
|
||||
for attempt in attempts:
|
||||
check(
|
||||
consumer[attempt]["parentSpanId"] == producer[attempt]["spanId"],
|
||||
"consumer parent mismatch",
|
||||
)
|
||||
return spans
|
||||
|
||||
return eventually("trace lacks upload, RabbitMQ, S3 or committed result", lookup)
|
||||
|
||||
|
||||
def verify_signals(client, obs, alice, chat):
|
||||
attachment, trace_id, request_id, filename = upload(client, alice, chat)
|
||||
ready = client.wait_ready(alice, attachment)
|
||||
check(len(ready["variants"]) == 3, "missing image variants")
|
||||
spans = chain(obs, trace_id, {1})
|
||||
api_logs = eventually(
|
||||
"API log missing", lambda: obs.logs("messenger-api", trace_id)
|
||||
)
|
||||
worker_logs = eventually(
|
||||
"worker log missing", lambda: obs.logs("messenger-worker", trace_id)
|
||||
)
|
||||
exported = json.dumps([spans, api_logs, worker_logs])
|
||||
check(request_id in exported, "request ID absent")
|
||||
check(
|
||||
alice["token"] not in exported
|
||||
and alice["registration"]["password"] not in exported
|
||||
and filename not in exported,
|
||||
"private data exported",
|
||||
)
|
||||
metrics = [
|
||||
"messenger_http_requests_total",
|
||||
"messenger_http_duration_seconds_bucket",
|
||||
"messenger_jobs_total",
|
||||
"messenger_job_duration_seconds_bucket",
|
||||
"messenger_attachment_ready_duration_seconds_bucket",
|
||||
"messenger_queue_depth",
|
||||
"messenger_queue_oldest_age_seconds",
|
||||
"messenger_outbox_pending",
|
||||
"messenger_db_pool",
|
||||
"messenger_worker_busy",
|
||||
"messenger_worker_capacity",
|
||||
"messenger_process_cpu_seconds_total",
|
||||
"messenger_process_memory_bytes",
|
||||
"messenger_container_cpu_seconds_total",
|
||||
"messenger_container_memory_bytes",
|
||||
"messenger:http_availability:15m",
|
||||
]
|
||||
eventually(
|
||||
"missing Prometheus series",
|
||||
lambda: all(obs.query(metric) for metric in metrics),
|
||||
)
|
||||
disallowed = {
|
||||
"request_id",
|
||||
"user_id",
|
||||
"chat_id",
|
||||
"message_id",
|
||||
"attachment_id",
|
||||
"filename",
|
||||
"url",
|
||||
}
|
||||
check(
|
||||
not any(
|
||||
disallowed & row["metric"].keys()
|
||||
for metric in metrics
|
||||
for row in obs.query(metric)
|
||||
),
|
||||
"unexpected metric labels",
|
||||
)
|
||||
dashboard = obs.get("/api/dashboards/uid/messenger-lab6")["dashboard"]
|
||||
check(
|
||||
dashboard["uid"] == "messenger-lab6" and len(dashboard["panels"]) >= 16,
|
||||
"dashboard not provisioned",
|
||||
)
|
||||
print(
|
||||
f"SIGNALS trace={trace_id} request_id={request_id} spans={len(spans)} "
|
||||
f"metrics={len(metrics)} panels={len(dashboard['panels'])}",
|
||||
flush=True,
|
||||
)
|
||||
return trace_id
|
||||
|
||||
|
||||
def verify_queue(client, obs, alice, chat):
|
||||
with stopped("worker"):
|
||||
attachment, trace_id, _, _ = upload(client, alice, chat)
|
||||
client.message(alice, chat, "Контроль очереди", attachments=[attachment["id"]])
|
||||
client.call(
|
||||
"GET", f"/api/v1/attachments/{attachment['id']}/content", 409, actor=alice
|
||||
)
|
||||
eventually("queue alert did not fire", lambda: obs.firing(BACKLOG_ALERT))
|
||||
depth = obs.value("max(messenger_queue_depth)")
|
||||
age = obs.value("max(messenger_queue_oldest_age_seconds)")
|
||||
check(depth >= 1 and age > 10, "queue metrics did not show backlog")
|
||||
trigger = "lab6_retry_" + uuid.uuid4().hex
|
||||
sql(f"""CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN IF NEW.id = '{attachment["id"]}' AND NEW.status = 'ready'
|
||||
THEN RAISE EXCEPTION 'lab6 retry verification'; END IF; RETURN NEW; END; $$;
|
||||
CREATE TRIGGER {trigger} BEFORE UPDATE ON attachments FOR EACH ROW
|
||||
EXECUTE FUNCTION {trigger}();""")
|
||||
try:
|
||||
eventually(
|
||||
"retry was not exported",
|
||||
lambda: any(
|
||||
attributes(span).get("job.outcome") == "retry"
|
||||
for span, _ in obs.trace(trace_id)
|
||||
),
|
||||
)
|
||||
finally:
|
||||
sql(
|
||||
f"DROP TRIGGER IF EXISTS {trigger} ON attachments; DROP FUNCTION IF EXISTS {trigger}();"
|
||||
)
|
||||
ready = client.wait_ready(alice, attachment)
|
||||
chain(obs, trace_id, {1, 2})
|
||||
downloaded = client.call(
|
||||
"GET", f"/api/v1/attachments/{ready['id']}/content", actor=alice
|
||||
)["bytes"]
|
||||
check(hashlib.sha256(downloaded).hexdigest() == ready["sha256"], "original changed")
|
||||
eventually(
|
||||
"queue did not drain or alert still firing",
|
||||
lambda: obs.value("max(messenger_queue_depth)") == 0
|
||||
and not obs.firing(BACKLOG_ALERT),
|
||||
)
|
||||
print(
|
||||
f"QUEUE firing → resolved; depth={depth:g} oldest={age:.1f}s trace={trace_id} retry linked",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
|
||||
def verify_database(client, obs, alice, chat):
|
||||
with stopped("postgres"):
|
||||
first_failed = None
|
||||
started = time.monotonic()
|
||||
failures = 0
|
||||
while time.monotonic() - started < 35:
|
||||
response = client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
503,
|
||||
actor=alice,
|
||||
body={"text": "Контроль отказа"},
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex},
|
||||
)
|
||||
first_failed = first_failed or response["headers"]["X-Trace-Id"]
|
||||
failures += 1
|
||||
if obs.firing(ERROR_ALERT):
|
||||
break
|
||||
time.sleep(0.25)
|
||||
check(obs.firing(ERROR_ALERT), "5xx alert did not fire")
|
||||
error_ratio = obs.value("messenger:http_error_ratio:30s")
|
||||
spans = eventually(
|
||||
"PostgreSQL failure span missing",
|
||||
lambda: [
|
||||
span
|
||||
for span, _ in obs.trace(first_failed)
|
||||
if span["name"].startswith("postgres.")
|
||||
and attributes(span).get("error.type")
|
||||
],
|
||||
)
|
||||
logs = eventually(
|
||||
"PostgreSQL error log missing",
|
||||
lambda: obs.logs("messenger-api", first_failed),
|
||||
)
|
||||
check(
|
||||
"postgres.unavailable" in json.dumps(logs),
|
||||
"trace and DB error log not linked",
|
||||
)
|
||||
eventually(
|
||||
"PostgreSQL did not recover",
|
||||
lambda: client.call("GET", "/health/ready", schema="Health"),
|
||||
)
|
||||
client.message(alice, chat, "PostgreSQL восстановлен")
|
||||
eventually("5xx alert still firing", lambda: not obs.firing(ERROR_ALERT))
|
||||
print(
|
||||
f"DB firing → resolved; 503={failures} ratio={error_ratio:.1%} "
|
||||
f"trace={first_failed} failed_span={spans[0]['name']}",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
|
||||
def verify_storage(client, obs, alice, chat, previous_trace):
|
||||
metrics = 'sum({__name__=~"otelcol_exporter_(send|enqueue)_failed_(log_records|spans)_total"})'
|
||||
before = eventually(
|
||||
"Prometheus counters unavailable", lambda: (obs.value(metrics),)
|
||||
)[0]
|
||||
with stopped("loki", "tempo"):
|
||||
deadline = time.monotonic() + 18
|
||||
successes = 0
|
||||
with ThreadPoolExecutor(max_workers=3) as pool:
|
||||
while time.monotonic() < deadline:
|
||||
successes += sum(
|
||||
pool.map(
|
||||
lambda _: client.message(alice, chat, "Контроль экспорта")[
|
||||
"status"
|
||||
]
|
||||
== 201,
|
||||
range(6),
|
||||
)
|
||||
)
|
||||
time.sleep(0.2)
|
||||
# Counters increase after a batch exhausts its retry budget.
|
||||
lost = eventually(
|
||||
"collector drop counters did not increase",
|
||||
lambda: max(0, obs.value(metrics) - before),
|
||||
)
|
||||
rss = eventually(
|
||||
"Collector RSS missing",
|
||||
lambda: obs.value(
|
||||
"max(max_over_time(otelcol_process_memory_rss_bytes[1m]))"
|
||||
),
|
||||
)
|
||||
check(
|
||||
0 < rss < 256 * 1024 * 1024, "collector RSS not observed below cgroup limit"
|
||||
)
|
||||
check(
|
||||
successes >= 30,
|
||||
"not enough successful business requests during telemetry outage",
|
||||
)
|
||||
eventually("Loki/Tempo not ready", lambda: obs.ready("loki") and obs.ready("tempo"))
|
||||
eventually(
|
||||
"collector queues did not drain",
|
||||
lambda: bool(obs.query("otelcol_exporter_queue_size"))
|
||||
and obs.value("sum(otelcol_exporter_queue_size)") == 0,
|
||||
)
|
||||
check(obs.logs("messenger-api", previous_trace), "Loki history lost on restart")
|
||||
check(obs.trace(previous_trace), "Tempo history lost on restart")
|
||||
_, trace_id, _, _ = upload(client, alice, chat)
|
||||
eventually(
|
||||
"Loki did not resume ingest", lambda: obs.logs("messenger-api", trace_id)
|
||||
)
|
||||
eventually("Tempo did not resume ingest", lambda: obs.trace(trace_id))
|
||||
print(
|
||||
f"STORAGE successful writes={successes} dropped logs/spans={lost:g} "
|
||||
f"peak collector RSS={rss / 1024**2:.1f} MiB; history preserved and new signals ingested",
|
||||
flush=True,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", required=True)
|
||||
parser.add_argument("--grafana-url", required=True)
|
||||
parser.add_argument("--incidents", action="store_true")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
obs = Observations(args.grafana_url)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
trace_id = verify_signals(client, obs, alice, chat)
|
||||
if args.incidents:
|
||||
verify_queue(client, obs, alice, chat)
|
||||
verify_database(client, obs, alice, chat)
|
||||
verify_storage(client, obs, alice, chat, trace_id)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Совместимость текущей схемы с предыдущей версией API — условие rolling update:
|
||||
# миграции применяются до замены реплик, и старые реплики продолжают работать.
|
||||
# Образ собирается из коммита PREVIOUS_REVISION (по умолчанию lab2-код aa971fb),
|
||||
# запускается одним процессом в сети backend рабочего стенда (наружу порт
|
||||
# не публикуется) и выполняет вход, создание чата, запись и чтение сообщения.
|
||||
# Создаются только синтетические данные; volumes не затрагиваются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
REVISION="${PREVIOUS_REVISION:-aa971fb}"
|
||||
IMAGE="mtusi-messenger:compat-$REVISION"
|
||||
PROJECT=$(docker compose --env-file "$ENV_FILE" config --format json | python3 -c \
|
||||
'import json, sys; print(json.load(sys.stdin)["name"])')
|
||||
NETWORK="${PROJECT}_backend"
|
||||
|
||||
git archive "$REVISION" | docker build --quiet --tag "$IMAGE" - >/dev/null
|
||||
echo "previous version image: $IMAGE ($(git log -1 --format='%h %s' "$REVISION"))"
|
||||
echo "schema on stand: $(docker compose --env-file "$ENV_FILE" exec -T postgres sh -c \
|
||||
'psql -X -q -tA -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT string_agg(name, \$\$, \$\$ ORDER BY version) FROM schema_migrations;"')"
|
||||
|
||||
docker run --rm -i --network "$NETWORK" --env-file "$ENV_FILE" \
|
||||
-e DB_HOST=postgres -e DB_PORT=5432 -e VALKEY_HOST=valkey -e VALKEY_PORT=6379 \
|
||||
"$IMAGE" sh -c 'python src/gateway.py >/tmp/api.log 2>&1 & exec python -' <<'EOF'
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
data = None if body is None else json.dumps(body).encode()
|
||||
request = urllib.request.Request(BASE + path, data=data, method=method)
|
||||
request.add_header("Content-Type", "application/json")
|
||||
for name, value in (headers or {}).items():
|
||||
request.add_header(name, value)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=5) as response:
|
||||
return response.status, json.loads(response.read() or b"null")
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, None
|
||||
|
||||
|
||||
for _ in range(60):
|
||||
try:
|
||||
if call("GET", "/health/ready")[0] == 200:
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
print(open("/tmp/api.log").read()[-2000:])
|
||||
raise SystemExit("FAIL: previous version did not become ready on current schema")
|
||||
|
||||
username = "compat_" + uuid.uuid4().hex[:12]
|
||||
password = "Compat-" + uuid.uuid4().hex
|
||||
status_user, user = call(
|
||||
"POST", "/api/v1/users",
|
||||
{"username": username, "display_name": "Compat", "password": password},
|
||||
)
|
||||
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
status_login, session = call(
|
||||
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + credentials}
|
||||
)
|
||||
bearer = {"Authorization": "Bearer " + session["token"]}
|
||||
status_chat, chat = call(
|
||||
"POST", "/api/v1/chats", {"title": "compat", "member_ids": [user["id"]]}, bearer
|
||||
)
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
status_send, message = call("POST", path, {"text": "старая версия пишет"}, bearer)
|
||||
status_read, page = call("GET", path, headers=bearer)
|
||||
result = {
|
||||
"ready": True,
|
||||
"register": status_user,
|
||||
"login": status_login,
|
||||
"create_chat": status_chat,
|
||||
"send_message_without_key": status_send,
|
||||
"read_history": status_read,
|
||||
"history_matches": page["items"] == [message],
|
||||
}
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
log = open("/tmp/api.log").read()
|
||||
print("startup log:", [line.split(" - ", 1)[-1] for line in log.splitlines() if "миграц" in line.lower() or "Схема" in line])
|
||||
if any(code >= 400 for code in result.values() if isinstance(code, int)) or not result["history_matches"]:
|
||||
raise SystemExit("FAIL: previous version is not compatible with current schema")
|
||||
print("PASS: previous API version works on the current schema (expand-only migration)")
|
||||
EOF
|
||||
@@ -0,0 +1,482 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Нагрузка через LB во время отказа/замены API-реплик; метрики и проверка данных.
|
||||
|
||||
failover — для каждой API-реплики по очереди: нагрузка → остановка этой реплики
|
||||
(docker stop = SIGTERM, затем docker kill = SIGKILL) → восстановление
|
||||
(scripts/restore.sh) → нагрузка после восстановления.
|
||||
rolling — нагрузка во время scripts/rolling-update.sh (поочерёдная замена реплик).
|
||||
|
||||
Клиент ведёт себя как настоящий: неподтверждённый POST повторяет с тем же
|
||||
Idempotency-Key и телом. После прогона вся история читается заново: каждое
|
||||
подтверждённое сообщение на месте и не изменено, дублей нет.
|
||||
Python 3.9+, стандартная библиотека; tokens не печатаются и не сохраняются.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
REQUEST_TIMEOUT_SEC = 5
|
||||
RETRY_PAUSE_SEC = 0.05
|
||||
STATS_FORMAT = "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Attempt:
|
||||
"""Одна HTTP-попытка клиента; время — секунды от начала прогона"""
|
||||
|
||||
op: str
|
||||
started: float
|
||||
finished: float
|
||||
status: int | None
|
||||
error: str | None
|
||||
instance: str | None
|
||||
retry: bool
|
||||
|
||||
@property
|
||||
def ok(self) -> bool:
|
||||
return self.status in (200, 201)
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self) -> None:
|
||||
self.started = time.monotonic()
|
||||
self.lock = threading.Lock()
|
||||
self.attempts: list[Attempt] = []
|
||||
self.confirmed: list[dict] = []
|
||||
self.events: dict[str, float] = {}
|
||||
|
||||
def now(self) -> float:
|
||||
return time.monotonic() - self.started
|
||||
|
||||
def mark(self, name: str) -> None:
|
||||
self.events[name] = round(self.now(), 3)
|
||||
|
||||
def add(self, attempt: Attempt, message: dict | None = None) -> None:
|
||||
with self.lock:
|
||||
self.attempts.append(attempt)
|
||||
if message is not None:
|
||||
self.confirmed.append(message)
|
||||
|
||||
|
||||
def request(
|
||||
base: str, method: str, path: str, token: str, body: dict | None, key: str | None
|
||||
) -> tuple[int | None, str | None, str | None, dict | None]:
|
||||
headers = {"Authorization": "Bearer " + token, "Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
if key is not None:
|
||||
headers["Idempotency-Key"] = key
|
||||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT_SEC) as response:
|
||||
payload = json.loads(response.read())
|
||||
return response.status, None, response.headers.get("X-Instance-Id"), payload
|
||||
except urllib.error.HTTPError as error:
|
||||
error.read()
|
||||
return error.code, None, error.headers.get("X-Instance-Id"), None
|
||||
except OSError as error:
|
||||
return None, type(error).__name__, None, None
|
||||
|
||||
|
||||
def worker(
|
||||
base: str,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
worker_id: int,
|
||||
recorder: Recorder,
|
||||
stop: threading.Event,
|
||||
) -> None:
|
||||
"""Запись с повтором по тому же ключу до подтверждения, затем чтение"""
|
||||
path = f"/api/v1/chats/{chat_id}/messages"
|
||||
index = 0
|
||||
while not stop.is_set():
|
||||
key = uuid.uuid4().hex
|
||||
body = {"text": f"exp-{worker_id}-{index}-{key[:8]}"}
|
||||
retry = False
|
||||
while not stop.is_set():
|
||||
started = recorder.now()
|
||||
status, error, instance, payload = request(
|
||||
base, "POST", path, token, body, key
|
||||
)
|
||||
attempt = Attempt(
|
||||
"write", started, recorder.now(), status, error, instance, retry
|
||||
)
|
||||
recorder.add(attempt, payload if status == 201 else None)
|
||||
if status == 201:
|
||||
break
|
||||
retry = True
|
||||
time.sleep(RETRY_PAUSE_SEC)
|
||||
started = recorder.now()
|
||||
status, error, instance, _ = request(
|
||||
base, "GET", path + "?limit=1", token, None, None
|
||||
)
|
||||
recorder.add(
|
||||
Attempt("read", started, recorder.now(), status, error, instance, False)
|
||||
)
|
||||
index += 1
|
||||
|
||||
|
||||
def percentile(values: list[float], p: float) -> float | None:
|
||||
if not values:
|
||||
return None
|
||||
ordered = sorted(values)
|
||||
return round(1000 * ordered[max(0, math.ceil(p * len(ordered)) - 1)], 1)
|
||||
|
||||
|
||||
def phase_stats(attempts: list[Attempt], start: float, end: float) -> dict:
|
||||
inside = [a for a in attempts if start <= a.finished < end]
|
||||
duration = max(end - start, 1e-9)
|
||||
result: dict = {"seconds": round(duration, 3)}
|
||||
for op in ("write", "read"):
|
||||
ops = [a for a in inside if a.op == op]
|
||||
ok = [a for a in ops if a.ok]
|
||||
result[op] = {
|
||||
"ok": len(ok),
|
||||
"errors": len(ops) - len(ok),
|
||||
"ok_rps": round(len(ok) / duration, 1),
|
||||
"latency_ms": {
|
||||
"p50": percentile([a.finished - a.started for a in ok], 0.50),
|
||||
"p95": percentile([a.finished - a.started for a in ok], 0.95),
|
||||
"p99": percentile([a.finished - a.started for a in ok], 0.99),
|
||||
},
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def recovery_stats(attempts: list[Attempt], action_start: float, end: float) -> dict:
|
||||
"""
|
||||
Время восстановления записи: от первой ошибки после воздействия до первой
|
||||
успешной записи после последней ошибки; без ошибок — 0. Отдельно —
|
||||
наибольший разрыв между подтверждениями записи (задержка без ошибок).
|
||||
"""
|
||||
window = sorted(
|
||||
(a for a in attempts if a.op == "write" and a.finished >= action_start - 1),
|
||||
key=lambda a: a.finished,
|
||||
)
|
||||
errors = [a for a in window if not a.ok and a.finished >= action_start]
|
||||
kinds: dict[str, int] = {}
|
||||
for a in errors:
|
||||
kind = a.error or f"HTTP {a.status}"
|
||||
kinds[kind] = kinds.get(kind, 0) + 1
|
||||
if errors:
|
||||
last_error = errors[-1].finished
|
||||
first_ok_after = next(
|
||||
(a.finished for a in window if a.ok and a.finished > last_error), end
|
||||
)
|
||||
recovery = first_ok_after - errors[0].finished
|
||||
else:
|
||||
recovery = 0.0
|
||||
oks = [a.finished for a in window if a.ok and a.finished <= end]
|
||||
gap = max((b - a for a, b in zip(oks, oks[1:])), default=None)
|
||||
return {
|
||||
"write_errors": len(errors),
|
||||
"error_kinds": kinds,
|
||||
"write_recovery_seconds": round(recovery, 3),
|
||||
"max_write_gap_seconds": None if gap is None else round(gap, 3),
|
||||
}
|
||||
|
||||
|
||||
def timeline(attempts: list[Attempt], end: float) -> list[dict]:
|
||||
rows = []
|
||||
for second in range(int(end) + 1):
|
||||
inside = [a for a in attempts if second <= a.finished < second + 1]
|
||||
instances: dict[str, int] = {}
|
||||
for a in inside:
|
||||
if a.ok and a.instance:
|
||||
instances[a.instance] = instances.get(a.instance, 0) + 1
|
||||
rows.append(
|
||||
{
|
||||
"t": second,
|
||||
"ok": sum(a.ok for a in inside),
|
||||
"errors": sum(not a.ok for a in inside),
|
||||
"instances": instances,
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def read_history(client: Client, actor: dict, chat_id: str) -> list[dict]:
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100"
|
||||
items: list[dict] = []
|
||||
while True:
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
items.extend(page["items"])
|
||||
if page["next_cursor"] is None:
|
||||
return items
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100&cursor=" + quote(
|
||||
page["next_cursor"], safe=""
|
||||
)
|
||||
|
||||
|
||||
def integrity(client: Client, actor: dict, chat_id: str, recorder: Recorder) -> dict:
|
||||
history = read_history(client, actor, chat_id)
|
||||
by_id = {m["id"]: m for m in history}
|
||||
texts = [m["text"] for m in history]
|
||||
confirmed = {m["id"]: m for m in recorder.confirmed}
|
||||
return {
|
||||
"messages_in_history": len(history),
|
||||
"confirmed_unique": len(confirmed),
|
||||
"confirmed_missing_or_changed": sum(
|
||||
by_id.get(i) != m for i, m in confirmed.items()
|
||||
),
|
||||
"duplicate_ids": len(history) - len(by_id),
|
||||
"duplicate_texts": len(texts) - len(set(texts)),
|
||||
"retried_writes_confirmed_as_replay": len(recorder.confirmed) - len(confirmed),
|
||||
}
|
||||
|
||||
|
||||
def run(command: list[str], env: dict | None = None) -> str:
|
||||
result = subprocess.run(
|
||||
command, cwd=ROOT, env=env, check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def api_containers() -> list[str]:
|
||||
ids = run(["docker", "compose", "--env-file", ".env", "ps", "-q", "api"]).split()
|
||||
names = [run(["docker", "inspect", "-f", "{{.Name}}", i]).lstrip("/") for i in ids]
|
||||
return sorted(names)
|
||||
|
||||
|
||||
def instance_of(container: str) -> str:
|
||||
return run(["docker", "exec", container, "cat", "/tmp/instance-id"])
|
||||
|
||||
|
||||
def container_stats() -> list[str]:
|
||||
names = run(
|
||||
["docker", "compose", "--env-file", ".env", "ps", "-q", "nginx", "postgres", "api"]
|
||||
).split()
|
||||
output = run(["docker", "stats", "--no-stream", "--format", STATS_FORMAT, *names])
|
||||
return output.splitlines()
|
||||
|
||||
|
||||
def scenario(
|
||||
args: argparse.Namespace,
|
||||
label: str,
|
||||
action: list[str],
|
||||
restore: list[str] | None,
|
||||
target: str | None = None,
|
||||
env: dict | None = None,
|
||||
) -> dict:
|
||||
client = Client(args.base_url, timeout=REQUEST_TIMEOUT_SEC)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
base = client.base
|
||||
recorder = Recorder()
|
||||
stop = threading.Event()
|
||||
threads = [
|
||||
threading.Thread(
|
||||
target=worker,
|
||||
args=(base, alice["token"], chat["id"], n, recorder, stop),
|
||||
daemon=True,
|
||||
)
|
||||
for n in range(args.workers)
|
||||
]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
time.sleep(args.before / 2)
|
||||
stats = container_stats()
|
||||
time.sleep(args.before / 2)
|
||||
|
||||
instances_before = sorted(
|
||||
{a.instance for a in recorder.attempts if a.ok and a.instance}
|
||||
)
|
||||
recorder.mark("action_start")
|
||||
action_output = run(action, env)
|
||||
recorder.mark("action_end")
|
||||
restore_output = ""
|
||||
if restore is not None:
|
||||
time.sleep(args.outage)
|
||||
recorder.mark("restore_start")
|
||||
restore_output = run(restore)
|
||||
recorder.mark("restore_end")
|
||||
time.sleep(args.after)
|
||||
stop.set()
|
||||
for thread in threads:
|
||||
thread.join(timeout=REQUEST_TIMEOUT_SEC + 1)
|
||||
recorder.mark("end")
|
||||
|
||||
events = recorder.events
|
||||
end = events["end"]
|
||||
after_start = events.get("restore_end", events["action_end"])
|
||||
attempts = recorder.attempts
|
||||
seen_after = [
|
||||
a for a in attempts if a.ok and a.finished >= after_start and a.instance
|
||||
]
|
||||
back_in_rotation = next(
|
||||
(
|
||||
round(a.finished - after_start, 3)
|
||||
for a in sorted(seen_after, key=lambda a: a.finished)
|
||||
if a.instance not in instances_before
|
||||
),
|
||||
None,
|
||||
)
|
||||
served = sorted(
|
||||
(a for a in attempts if a.ok and a.instance), key=lambda a: a.finished
|
||||
)
|
||||
instances_seen: dict[str, list[float]] = {}
|
||||
for a in served:
|
||||
offset = round(a.finished - events["action_start"], 3)
|
||||
instances_seen.setdefault(a.instance, [offset, offset])[1] = offset
|
||||
phases = {
|
||||
"before": phase_stats(attempts, 0, events["action_start"]),
|
||||
"during": phase_stats(attempts, events["action_start"], after_start),
|
||||
"after": phase_stats(attempts, after_start, end),
|
||||
}
|
||||
target_after_stop = None
|
||||
if target is not None:
|
||||
target_after_stop = sum(
|
||||
1
|
||||
for a in attempts
|
||||
if a.ok
|
||||
and a.instance == target
|
||||
and a.finished > events["action_end"]
|
||||
and a.finished < events.get("restore_start", end)
|
||||
)
|
||||
return {
|
||||
"scenario": label,
|
||||
"target_instance": target,
|
||||
"workers": args.workers,
|
||||
"events_seconds": events,
|
||||
"instances_before": instances_before,
|
||||
"instances_after": sorted({a.instance for a in seen_after}),
|
||||
"target_responses_while_stopped": target_after_stop,
|
||||
"restored_replica_in_rotation_seconds": back_in_rotation
|
||||
if restore is not None
|
||||
else None,
|
||||
# alias → [первый, последний] успешный ответ, секунды от начала воздействия
|
||||
"instances_seen_from_action": instances_seen,
|
||||
"phases": phases,
|
||||
"recovery": recovery_stats(attempts, events["action_start"], end),
|
||||
"integrity": integrity(client, alice, chat["id"], recorder),
|
||||
"container_stats_before_action": stats,
|
||||
"action_output": action_output.splitlines(),
|
||||
"restore_output_tail": restore_output.splitlines()[-2:],
|
||||
"timeline": timeline(attempts, end),
|
||||
}
|
||||
|
||||
|
||||
def failover(args: argparse.Namespace) -> list[dict]:
|
||||
results = []
|
||||
for signal in args.signals:
|
||||
for container in api_containers():
|
||||
target = instance_of(container)
|
||||
if signal == "SIGTERM":
|
||||
action = ["docker", "stop", "--timeout", "10", container]
|
||||
else:
|
||||
action = ["docker", "kill", "--signal", "SIGKILL", container]
|
||||
label = f"{signal} {container}"
|
||||
print(f"== {label} (instance {target})", flush=True)
|
||||
result = scenario(
|
||||
args, label, action, ["./scripts/restore.sh"], target=target
|
||||
)
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
def rolling(args: argparse.Namespace) -> list[dict]:
|
||||
env = dict(os.environ)
|
||||
env.setdefault("APP_REVISION", f"exp-{int(time.time())}")
|
||||
print(f"== rolling update to revision {env['APP_REVISION']}", flush=True)
|
||||
result = scenario(
|
||||
args, "rolling update", ["./scripts/rolling-update.sh"], None, env=env
|
||||
)
|
||||
result["revisions_after"] = run(["./scripts/instances.sh"]).splitlines()
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
return [result]
|
||||
|
||||
|
||||
def brief(result: dict) -> dict:
|
||||
"""Краткая строка прогона для терминала; полная версия — в --output"""
|
||||
return {
|
||||
"scenario": result["scenario"],
|
||||
"rps_ok_write": {k: v["write"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"rps_ok_read": {k: v["read"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"errors": {
|
||||
k: v["write"]["errors"] + v["read"]["errors"]
|
||||
for k, v in result["phases"].items()
|
||||
},
|
||||
"recovery": result["recovery"],
|
||||
"target_responses_while_stopped": result["target_responses_while_stopped"],
|
||||
"restored_replica_in_rotation_seconds": result[
|
||||
"restored_replica_in_rotation_seconds"
|
||||
],
|
||||
"instances_seen_from_action": result["instances_seen_from_action"],
|
||||
"integrity": result["integrity"],
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("mode", choices=["failover", "rolling"])
|
||||
parser.add_argument("--base-url", default=None)
|
||||
parser.add_argument("--workers", type=int, default=4)
|
||||
parser.add_argument("--before", type=float, default=8, help="нагрузка до отказа")
|
||||
parser.add_argument("--outage", type=float, default=10, help="реплика остановлена")
|
||||
parser.add_argument("--after", type=float, default=8, help="после восстановления")
|
||||
parser.add_argument(
|
||||
"--signals",
|
||||
nargs="+",
|
||||
default=["SIGTERM", "SIGKILL"],
|
||||
choices=["SIGTERM", "SIGKILL"],
|
||||
)
|
||||
parser.add_argument("--output", default=None, help="JSON с полными результатами")
|
||||
args = parser.parse_args()
|
||||
started = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
results = failover(args) if args.mode == "failover" else rolling(args)
|
||||
report = {
|
||||
"mode": args.mode,
|
||||
"started": started,
|
||||
"profile": {
|
||||
"workers": args.workers,
|
||||
"loop": "closed: POST message (retry same key until 201) + GET limit=1",
|
||||
"client_timeout_seconds": REQUEST_TIMEOUT_SEC,
|
||||
"phases_seconds": {
|
||||
"before": args.before,
|
||||
"outage": args.outage,
|
||||
"after": args.after,
|
||||
},
|
||||
},
|
||||
"runs": results,
|
||||
}
|
||||
if args.output:
|
||||
destination = ROOT / args.output
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
||||
print(f"saved: {args.output}")
|
||||
failed = [
|
||||
r["scenario"]
|
||||
for r in results
|
||||
if r["integrity"]["confirmed_missing_or_changed"]
|
||||
or r["integrity"]["duplicate_ids"]
|
||||
or r["integrity"]["duplicate_texts"]
|
||||
or r["target_responses_while_stopped"]
|
||||
]
|
||||
if failed:
|
||||
raise SystemExit(f"FAIL: потеря/дубль или отказ не изолирован: {failed}")
|
||||
print(f"PASS: прогонов {len(results)}, подтверждённые сообщения целы, дублей нет")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,111 @@
|
||||
-- Обоснование индекса истории: EXPLAIN (ANALYZE, BUFFERS) на ≥10 000 сообщений.
|
||||
-- Запуск: make explain → evidence/explain_history.txt
|
||||
-- Сид синтетический и идемпотентный: повторный запуск данных не дублирует.
|
||||
-- Пользователь explain_seed не может войти: password_hash не является hash Argon2.
|
||||
|
||||
\pset pager off
|
||||
\timing off
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
seed_user uuid;
|
||||
big_chat uuid;
|
||||
noise uuid;
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM chats WHERE title = 'explain-big') THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ('explain_seed', 'EXPLAIN seed', 'not-an-argon2-hash')
|
||||
ON CONFLICT (username) DO UPDATE SET display_name = EXCLUDED.display_name
|
||||
RETURNING id INTO seed_user;
|
||||
|
||||
-- Большой чат: 20 000 сообщений
|
||||
INSERT INTO chats (title, created_by) VALUES ('explain-big', seed_user)
|
||||
RETURNING id INTO big_chat;
|
||||
INSERT INTO chat_members (chat_id, user_id, position) VALUES (big_chat, seed_user, 0);
|
||||
INSERT INTO messages (chat_id, sender_id, text, created_at)
|
||||
SELECT big_chat, seed_user, 'synthetic message #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 20000) AS n;
|
||||
|
||||
-- Шум: 50 чатов по 1 000 сообщений, чтобы таблица была больше одного чата
|
||||
FOR i IN 1..50 LOOP
|
||||
INSERT INTO chats (title, created_by) VALUES ('explain-noise-' || i, seed_user)
|
||||
RETURNING id INTO noise;
|
||||
INSERT INTO chat_members (chat_id, user_id, position) VALUES (noise, seed_user, 0);
|
||||
INSERT INTO messages (chat_id, sender_id, text, created_at)
|
||||
SELECT noise, seed_user, 'noise #' || n,
|
||||
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
|
||||
FROM generate_series(1, 1000) AS n;
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
ANALYZE users, chats, chat_members, messages;
|
||||
|
||||
SELECT id AS big_chat FROM chats WHERE title = 'explain-big' \gset
|
||||
|
||||
-- Cursor из середины истории: ключ 10 000-го сообщения
|
||||
SELECT created_at AS cursor_created_at, id AS cursor_id
|
||||
FROM messages WHERE chat_id = :'big_chat'
|
||||
ORDER BY created_at, id OFFSET 9999 LIMIT 1 \gset
|
||||
|
||||
\echo '== Объём данных'
|
||||
SELECT
|
||||
(SELECT count(*) FROM messages) AS messages_total,
|
||||
(SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS messages_in_big_chat,
|
||||
pg_size_pretty(pg_relation_size('messages')) AS messages_heap,
|
||||
pg_size_pretty(pg_relation_size('messages_chat_id_created_at_id_idx')) AS history_index;
|
||||
|
||||
\echo ''
|
||||
\echo '== 1. Первая страница истории (limit 50 + 1), как list_for_chat'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo ''
|
||||
\echo '== 2. Страница из середины по cursor (после 10 000-го), как list_for_chat_after'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
|
||||
\echo ''
|
||||
\echo '== 3. Тот же запрос как generic plan prepared statement (так его исполняет asyncpg)'
|
||||
SET plan_cache_mode = force_generic_plan;
|
||||
PREPARE history_page(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4;
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
EXECUTE history_page(:'big_chat', :'cursor_created_at', :'cursor_id', 51);
|
||||
DEALLOCATE history_page;
|
||||
RESET plan_cache_mode;
|
||||
|
||||
\echo ''
|
||||
\echo '== 4. MAX(created_at) при вставке сообщения (монотонное время чата)'
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT max(created_at) FROM messages WHERE chat_id = :'big_chat';
|
||||
|
||||
\echo ''
|
||||
\echo '== 5. Для сравнения: тот же запрос из середины БЕЗ индекса истории'
|
||||
\echo ' (DROP INDEX внутри транзакции с ROLLBACK — индекс не теряется)'
|
||||
BEGIN;
|
||||
DROP INDEX messages_chat_id_created_at_id_idx;
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
AND (created_at, id) > (:'cursor_created_at'::timestamptz, :'cursor_id'::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
ROLLBACK;
|
||||
Executable
+90
@@ -0,0 +1,90 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Локальные синтетические секреты стенда в secrets/ (каталог в .gitignore)
|
||||
# и .env из .env.example, если их ещё нет. Повторный запуск ничего не меняет:
|
||||
# существующие ключи не перезаписываются, иначе данные bucket станут недоступны.
|
||||
#
|
||||
# s3_admin_* — ключи s3-bootstrap: создание bucket (действие Admin)
|
||||
# s3_access_* — ключи API и janitor: Read/Write/List только своего bucket
|
||||
# s3_identities — конфигурация identities SeaweedFS из этих ключей
|
||||
#
|
||||
# Значения не печатаются. Файлы 0644 внутри каталога 0700: процесс API в
|
||||
# контейнере работает не от root и должен прочитать смонтированный секрет.
|
||||
cd "$(dirname "$0")/.."
|
||||
umask 077
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
cp .env.example .env
|
||||
echo "created .env from .env.example"
|
||||
fi
|
||||
|
||||
BUCKET=$(sed -n 's/^S3_BUCKET=//p' .env)
|
||||
: "${BUCKET:?S3_BUCKET is not set in .env}"
|
||||
|
||||
mkdir -p secrets
|
||||
chmod 700 secrets
|
||||
|
||||
random() {
|
||||
LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$1"
|
||||
}
|
||||
|
||||
secret() {
|
||||
file="secrets/$1"
|
||||
if [ ! -s "$file" ]; then
|
||||
random "$2" >"$file"
|
||||
echo "created $file"
|
||||
fi
|
||||
chmod 644 "$file"
|
||||
}
|
||||
|
||||
secret s3_admin_access_key_id 20
|
||||
secret s3_admin_secret_access_key 40
|
||||
secret s3_access_key_id 20
|
||||
secret s3_secret_access_key 40
|
||||
|
||||
# Без identity "anonymous" SeaweedFS отклоняет любой неподписанный запрос
|
||||
cat >secrets/s3_identities.json <<EOF
|
||||
{
|
||||
"identities": [
|
||||
{
|
||||
"name": "bootstrap",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "$(cat secrets/s3_admin_access_key_id)",
|
||||
"secretKey": "$(cat secrets/s3_admin_secret_access_key)"
|
||||
}
|
||||
],
|
||||
"actions": ["Admin"]
|
||||
},
|
||||
{
|
||||
"name": "messenger-api",
|
||||
"credentials": [
|
||||
{
|
||||
"accessKey": "$(cat secrets/s3_access_key_id)",
|
||||
"secretKey": "$(cat secrets/s3_secret_access_key)"
|
||||
}
|
||||
],
|
||||
"actions": ["Read:$BUCKET", "Write:$BUCKET", "List:$BUCKET"]
|
||||
}
|
||||
]
|
||||
}
|
||||
EOF
|
||||
chmod 644 secrets/s3_identities.json
|
||||
echo "secrets ready in secrets/ (values are not printed)"
|
||||
|
||||
# URL хранится в файле и монтируется только dispatcher/worker.
|
||||
if [ ! -s secrets/broker_url ]; then
|
||||
python3 - <<'PYTHON'
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
values = dict(line.split("=", 1) for line in Path(".env").read_text().splitlines()
|
||||
if "=" in line and not line.startswith("#"))
|
||||
user = quote(values.get("RABBITMQ_USER", "test"), safe="")
|
||||
password = quote(values.get("RABBITMQ_PASS", "test"), safe="")
|
||||
Path("secrets/broker_url").write_text(f"amqp://{user}:{password}@broker:5672/")
|
||||
PYTHON
|
||||
echo "created secrets/broker_url"
|
||||
fi
|
||||
chmod 644 secrets/broker_url
|
||||
|
||||
secret grafana_admin_password 32
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Операторский вид API-реплик: контейнер ↔ alias X-Instance-Id ↔ ревизия образа.
|
||||
# Нужен, чтобы показать, какой реальный контейнер обслужил запрос или остановлен:
|
||||
# заголовок сам по себе не доказывает разные процессы.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' CONTAINER STATE INSTANCE PID REVISION
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -a -q api); do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
state=$(docker inspect -f '{{.State.Status}}{{if .State.Health}}/{{.State.Health.Status}}{{end}}' "$container")
|
||||
pid=$(docker inspect -f '{{.State.Pid}}' "$container")
|
||||
revision=$(docker inspect -f '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$container")
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || echo -)
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' "${name#/}" "$state" "$alias" "$pid" "$revision"
|
||||
done
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Перезапуск всего стенда с сохранением volumes: хранилища (PostgreSQL, Valkey,
|
||||
# S3, RabbitMQ), затем API, worker, dispatcher, janitor и балансировщик. Никаких down -v, flush
|
||||
# или повторного seed; migrate и s3-bootstrap не запускаются — схема и bucket
|
||||
# уже в volumes. Готовность ждёт тест.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps postgres valkey s3 broker
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps api janitor worker dispatcher
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps nginx
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Возврат API-реплик к числу API_REPLICAS после stop-one.sh: запуск остановленных
|
||||
# контейнеров и ожидание healthy. Работающие реплики не пересоздаются
|
||||
# (--no-recreate), хранилища, migrate и LB не перезапускаются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" up -d --no-deps --no-recreate --wait --wait-timeout 60 api
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Поочерёдная замена API-реплик новой сборкой без остановки обслуживания.
|
||||
# 1. build нового образа (ревизия видна в метке образа и журнале старта);
|
||||
# 2. migrate однократно: миграции только расширяют схему, старые реплики
|
||||
# продолжают работать на новой схеме;
|
||||
# 3. для каждой старой реплики: +1 новая → ждать healthy → пауза на обновление
|
||||
# DNS в nginx → graceful stop старой (SIGTERM, LB уводит трафик) → удалить.
|
||||
# Во время замены реплик на одну больше, чем API_REPLICAS: ресурс заложен заранее.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
COMPOSE="docker compose --env-file $ENV_FILE"
|
||||
APP_REVISION="${APP_REVISION:-$(git rev-parse --short HEAD 2>/dev/null || echo dev)-$(date +%s)}"
|
||||
export APP_REVISION
|
||||
# ID образа зависит только от содержимого (без provenance-attestation)
|
||||
export BUILDX_NO_DEFAULT_ATTESTATIONS=1
|
||||
# Больше resolver valid=2s в nginx: новая реплика уже в upstream до остановки старой
|
||||
LB_DNS_REFRESH_SEC=3
|
||||
STOP_TIMEOUT_SEC=10
|
||||
|
||||
log() {
|
||||
echo "[$(date +%H:%M:%S)] $*"
|
||||
}
|
||||
|
||||
log "build revision $APP_REVISION"
|
||||
$COMPOSE build --quiet api
|
||||
log "migrate"
|
||||
$COMPOSE run --rm --no-deps migrate >/dev/null
|
||||
|
||||
old_containers=$($COMPOSE ps -q api)
|
||||
for container in $old_containers; do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
running=$($COMPOSE ps -q api | wc -l | tr -d ' ')
|
||||
log "scale api to $((running + 1)), wait healthy"
|
||||
$COMPOSE up -d --no-deps --no-recreate --wait --wait-timeout 60 \
|
||||
--scale "api=$((running + 1))" api
|
||||
sleep "$LB_DNS_REFRESH_SEC"
|
||||
log "graceful stop ${name#/}"
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
docker rm "$container" >/dev/null
|
||||
done
|
||||
log "done: all API replicas run revision $APP_REVISION"
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Временный read-only доступ к Filer UI SeaweedFS (просмотр бакетов) с хоста.
|
||||
|
||||
Filer UI слушает только 127.0.0.1 внутри контейнера s3 и в сеть не открыт
|
||||
(требование: административная консоль не доступна в общей сети). Этот скрипт
|
||||
— операторский инструмент на время демонстрации:
|
||||
|
||||
* слушает только 127.0.0.1 хоста и живёт, пока запущен (Ctrl+C — закрыть);
|
||||
* пропускает только GET/HEAD: удаление/загрузка через UI (POST/PUT/DELETE)
|
||||
отклоняются 405, данные bucket через него не изменить;
|
||||
* каждый запрос выполняется в контейнере s3 (docker exec … curl) к
|
||||
127.0.0.1:8888 — порты контейнера и сети стенда не меняются.
|
||||
|
||||
make s3-ui # http://127.0.0.1:8888/buckets/
|
||||
python3 scripts/s3_ui.py --port 18888
|
||||
|
||||
Python 3.9+, стандартная библиотека.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FILER_URL = "http://127.0.0.1:8888"
|
||||
CURL_TIMEOUT_SEC = 30
|
||||
# Заголовки клиента, передаваемые filer: от Accept зависит HTML или JSON
|
||||
FORWARDED_HEADERS = ("Accept", "Range")
|
||||
# Ответ пересобирается целиком: длина и кодирование задаются заново
|
||||
DROPPED_HEADERS = {"transfer-encoding", "connection", "content-length", "keep-alive"}
|
||||
|
||||
|
||||
def s3_container() -> str:
|
||||
container = subprocess.run(
|
||||
["docker", "compose", "--env-file", ".env", "ps", "-q", "s3"],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
).stdout.strip()
|
||||
if not container:
|
||||
raise SystemExit("контейнер s3 не запущен: сначала make up")
|
||||
return container
|
||||
|
||||
|
||||
class ReadOnlyFilerProxy(BaseHTTPRequestHandler):
|
||||
container = ""
|
||||
|
||||
def do_GET(self) -> None:
|
||||
self._proxy(head=False)
|
||||
|
||||
def do_HEAD(self) -> None:
|
||||
self._proxy(head=True)
|
||||
|
||||
def _reject(self) -> None:
|
||||
self.send_error(405, "Read-only: изменение bucket через UI запрещено")
|
||||
|
||||
do_POST = do_PUT = do_DELETE = do_PATCH = _reject
|
||||
|
||||
def _proxy(self, head: bool) -> None:
|
||||
command = ["docker", "exec", self.container, "curl", "-s", "-i"]
|
||||
if head:
|
||||
command.append("-I")
|
||||
for name in FORWARDED_HEADERS:
|
||||
if self.headers.get(name):
|
||||
command += ["-H", f"{name}: {self.headers[name]}"]
|
||||
command.append(FILER_URL + self.path)
|
||||
try:
|
||||
raw = subprocess.run(
|
||||
command, capture_output=True, timeout=CURL_TIMEOUT_SEC, check=True
|
||||
).stdout
|
||||
except (subprocess.SubprocessError, OSError):
|
||||
self.send_error(502, "Filer недоступен")
|
||||
return
|
||||
head_part, _, body = raw.partition(b"\r\n\r\n")
|
||||
lines = head_part.decode("latin-1").split("\r\n")
|
||||
status = int(lines[0].split()[1])
|
||||
self.send_response(status)
|
||||
for line in lines[1:]:
|
||||
name, _, value = line.partition(":")
|
||||
if name.strip().lower() not in DROPPED_HEADERS:
|
||||
self.send_header(name.strip(), value.strip())
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.send_header("Connection", "close")
|
||||
self.end_headers()
|
||||
if not head:
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, format: str, *args: object) -> None:
|
||||
print(f"{self.command} {self.path.split('?')[0]} -> {args[1] if len(args) > 1 else ''}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
||||
parser.add_argument("--port", type=int, default=8888)
|
||||
args = parser.parse_args()
|
||||
|
||||
ReadOnlyFilerProxy.container = s3_container()
|
||||
server = ThreadingHTTPServer(("127.0.0.1", args.port), ReadOnlyFilerProxy)
|
||||
print(f"Filer UI (read-only): http://127.0.0.1:{args.port}/buckets/ — Ctrl+C, чтобы закрыть")
|
||||
try:
|
||||
server.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
server.server_close()
|
||||
print("проброс закрыт")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""SIGTERM одной API-реплики за балансировщиком.
|
||||
|
||||
Реплика перестаёт принимать соединения, дожидается текущих запросов и
|
||||
завершается не дольше чем за 10 секунд; LB уводит трафик на другие реплики.
|
||||
Во время проверки фоновые клиенты непрерывно шлют GET /health/live через LB:
|
||||
ошибок (5xx/обрыв) быть не должно, после выхода реплики её X-Instance-Id
|
||||
в ответах не появляется.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_BASE_URL = json.loads((ROOT / "lab.json").read_text())["default_base_url"]
|
||||
|
||||
SHUTDOWN_LIMIT_SEC = 10
|
||||
PROBE_INTERVAL_SEC = 0.02
|
||||
PROBERS = 4
|
||||
|
||||
|
||||
class Prober(threading.Thread):
|
||||
"""Фоновый клиент через LB: время ответа и обслужившая реплика"""
|
||||
|
||||
def __init__(self, url: str) -> None:
|
||||
super().__init__(daemon=True)
|
||||
self.url = url
|
||||
self.stop = threading.Event()
|
||||
self.ok: list[tuple[float, str]] = []
|
||||
self.errors: list[tuple[float, str]] = []
|
||||
|
||||
def run(self) -> None:
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
with urllib.request.urlopen(self.url, timeout=2) as response:
|
||||
self.ok.append(
|
||||
(time.monotonic(), response.headers.get("X-Instance-Id", ""))
|
||||
)
|
||||
except urllib.error.HTTPError as error:
|
||||
self.errors.append((time.monotonic(), f"HTTP {error.code}"))
|
||||
except OSError as error:
|
||||
self.errors.append((time.monotonic(), type(error).__name__))
|
||||
time.sleep(PROBE_INTERVAL_SEC)
|
||||
|
||||
|
||||
def docker(*args: str) -> str:
|
||||
result = subprocess.run(
|
||||
["docker", *args], check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def first_api_container() -> str:
|
||||
ids = docker("compose", "--env-file", ".env", "ps", "-q", "api").split()
|
||||
if not ids:
|
||||
raise SystemExit("FAIL: нет запущенных API-реплик")
|
||||
return docker("inspect", "-f", "{{.Name}}", ids[0]).lstrip("/")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--base-url", default=os.environ.get("BASE_URL") or DEFAULT_BASE_URL
|
||||
)
|
||||
parser.add_argument("--container", default=os.environ.get("CONTAINER"))
|
||||
args = parser.parse_args()
|
||||
os.chdir(ROOT)
|
||||
container = args.container or first_api_container()
|
||||
instance = docker("exec", container, "cat", "/tmp/instance-id")
|
||||
|
||||
logs = subprocess.Popen(
|
||||
["docker", "logs", "--follow", "--since", "0s", container],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
)
|
||||
waiter = subprocess.Popen(
|
||||
["docker", "wait", container], stdout=subprocess.PIPE, text=True
|
||||
)
|
||||
probers = [
|
||||
Prober(args.base_url.rstrip("/") + "/health/live") for _ in range(PROBERS)
|
||||
]
|
||||
for prober in probers:
|
||||
prober.start()
|
||||
time.sleep(1)
|
||||
before = {alias for prober in probers for _, alias in prober.ok}
|
||||
if instance not in before:
|
||||
raise SystemExit("FAIL: целевая реплика не обслуживает запросы до SIGTERM")
|
||||
|
||||
sent_at = time.monotonic()
|
||||
docker("kill", "--signal", "SIGTERM", container)
|
||||
exit_code = int(waiter.communicate(timeout=SHUTDOWN_LIMIT_SEC + 5)[0].strip())
|
||||
exited_at = time.monotonic()
|
||||
time.sleep(1)
|
||||
for prober in probers:
|
||||
prober.stop.set()
|
||||
prober.join()
|
||||
logs.terminate()
|
||||
shutdown_log = logs.communicate(timeout=5)[0]
|
||||
|
||||
ok_after = [(t, a) for p in probers for t, a in p.ok if t > sent_at]
|
||||
errors_after = [(t, e) for p in probers for t, e in p.errors if t > sent_at]
|
||||
target_after_exit = [t for t, a in ok_after if a == instance and t > exited_at]
|
||||
last_target = max((t for t, a in ok_after if a == instance), default=None)
|
||||
print("Журнал реплики после SIGTERM:")
|
||||
print(
|
||||
"\n".join(line for line in shutdown_log.splitlines() if "/health/" not in line)
|
||||
)
|
||||
summary = {
|
||||
"container": container,
|
||||
"instance": instance,
|
||||
"exit_code": exit_code,
|
||||
"shutdown_seconds": round(exited_at - sent_at, 3),
|
||||
"limit_seconds": SHUTDOWN_LIMIT_SEC,
|
||||
"instances_before": sorted(before),
|
||||
"probes_ok_after_sigterm": len(ok_after),
|
||||
"instances_after_sigterm": sorted({a for _, a in ok_after}),
|
||||
"last_target_response_after_sigterm_seconds": None
|
||||
if last_target is None
|
||||
else round(last_target - sent_at, 3),
|
||||
"target_responses_after_exit": len(target_after_exit),
|
||||
"errors_after_sigterm": len(errors_after),
|
||||
"error_kinds": sorted({e for _, e in errors_after}),
|
||||
}
|
||||
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
||||
if (
|
||||
exit_code != 0
|
||||
or exited_at - sent_at > SHUTDOWN_LIMIT_SEC
|
||||
or errors_after
|
||||
or target_after_exit
|
||||
or not ok_after
|
||||
):
|
||||
raise SystemExit("FAIL: завершение по SIGTERM не соответствует требованиям")
|
||||
print(
|
||||
"PASS: SIGTERM завершил %s за %.2f с, LB обслужил %d запросов без ошибок"
|
||||
% (container, exited_at - sent_at, len(ok_after))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Остановка ОДНОЙ API-реплики, обслужившей тест: TARGET_INSTANCE_ID → контейнер.
|
||||
# Каждый процесс API пишет свой alias (X-Instance-Id) в /tmp/instance-id.
|
||||
# docker stop: SIGTERM → graceful shutdown; restart-политика unless-stopped
|
||||
# не поднимает вручную остановленный контейнер, он остаётся остановленным.
|
||||
# БД, LB и volumes не затрагиваются. Восстановление — scripts/restore.sh.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
# Меньше 10 с лимита hook; больше drain + graceful shutdown (1 + 5 с)
|
||||
STOP_TIMEOUT_SEC=8
|
||||
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -q api); do
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || true)
|
||||
if [ "$alias" = "$TARGET_INSTANCE_ID" ]; then
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
echo "stopped ${name#/} (instance $alias)" >&2
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
echo "no running API replica with instance $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,318 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Эксперимент: N одновременных загрузок вложений и пик памяти API-реплик.
|
||||
|
||||
Гипотеза: память реплики на загрузку ограничена (spool ≤ 1 MiB RAM, потоковый
|
||||
PUT в S3, проверка изображения в пуле из IMAGE_INSPECT_WORKERS потоков),
|
||||
поэтому пик RSS растёт не пропорционально N × размер файла.
|
||||
|
||||
Профили:
|
||||
png — PNG ~9 MiB из шума (почти не сжимается): нагрузка на приём тела,
|
||||
spool, SHA-256 и PUT в S3. Генерируется стандартной библиотекой.
|
||||
jpeg — progressive JPEG 8000×5000 = 40 Мпикс (предел): худший случай
|
||||
декодера libjpeg. Нужен Pillow (poetry run python ...).
|
||||
|
||||
Перед каждым прогоном API-реплики перезапускаются: VmHWM (пиковый RSS
|
||||
процесса) начинается заново. Память опрашивается внутри контейнеров каждые
|
||||
0.2 с (cgroup memory.current и VmRSS процесса). Все N запросов стартуют по
|
||||
одному барьеру. Tokens не печатаются и не сохраняются.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import statistics
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import zlib
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
REQUEST_TIMEOUT_SEC = 120
|
||||
READY_TIMEOUT_SEC = 90
|
||||
SAMPLE_INTERVAL_SEC = 0.2
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
SAMPLER = (
|
||||
"while :; do "
|
||||
"echo $(cat /sys/fs/cgroup/memory.current) "
|
||||
"$(awk '/VmRSS/ {print $2}' /proc/1/status); "
|
||||
f"sleep {SAMPLE_INTERVAL_SEC}; done"
|
||||
)
|
||||
|
||||
|
||||
def png_noise(width: int, height: int) -> bytes:
|
||||
"""Настоящий RGB PNG из случайных байт: размер ≈ width × height × 3"""
|
||||
|
||||
def chunk(kind: bytes, data: bytes) -> bytes:
|
||||
return (
|
||||
struct.pack(">I", len(data))
|
||||
+ kind
|
||||
+ data
|
||||
+ struct.pack(">I", zlib.crc32(kind + data))
|
||||
)
|
||||
|
||||
raw = b"".join(b"\x00" + os.urandom(width * 3) for _ in range(height))
|
||||
header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)
|
||||
return (
|
||||
b"\x89PNG\r\n\x1a\n"
|
||||
+ chunk(b"IHDR", header)
|
||||
+ chunk(b"IDAT", zlib.compress(raw, 1))
|
||||
+ chunk(b"IEND", b"")
|
||||
)
|
||||
|
||||
|
||||
def jpeg_worst_case() -> bytes:
|
||||
from PIL import Image
|
||||
|
||||
Image.MAX_IMAGE_PIXELS = None
|
||||
image = Image.effect_noise((8000, 5000), 64).convert("RGB")
|
||||
buffer = io.BytesIO()
|
||||
image.save(buffer, "JPEG", quality=10, progressive=True, subsampling=0)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def compose(*args: str) -> str:
|
||||
return subprocess.run(
|
||||
[*COMPOSE, *args], cwd=ROOT, capture_output=True, text=True, check=True
|
||||
).stdout
|
||||
|
||||
|
||||
def api_containers() -> list[str]:
|
||||
return compose("ps", "-q", "api").split()
|
||||
|
||||
|
||||
def container_name(container: str) -> str:
|
||||
return subprocess.run(
|
||||
["docker", "inspect", "-f", "{{.Name}}", container],
|
||||
capture_output=True, text=True, check=True,
|
||||
).stdout.strip().lstrip("/")
|
||||
|
||||
|
||||
def process_status(container: str) -> dict:
|
||||
"""VmRSS/VmHWM процесса API (PID 1), байты, и его X-Instance-Id"""
|
||||
output = subprocess.run(
|
||||
["docker", "exec", container, "sh", "-c",
|
||||
"grep -E 'VmRSS|VmHWM' /proc/1/status; cat /tmp/instance-id"],
|
||||
capture_output=True, text=True, check=True,
|
||||
).stdout.split("\n")
|
||||
values: dict = {line.split(":")[0]: int(line.split()[1]) * 1024 for line in output[:2]}
|
||||
values["instance"] = output[2].strip()
|
||||
return values
|
||||
|
||||
|
||||
def restart_api(base_url: str) -> None:
|
||||
compose("restart", "api")
|
||||
deadline = time.monotonic() + READY_TIMEOUT_SEC
|
||||
while time.monotonic() < deadline:
|
||||
states = [
|
||||
subprocess.run(
|
||||
["docker", "inspect", "-f", "{{.State.Health.Status}}", c],
|
||||
capture_output=True, text=True,
|
||||
).stdout.strip()
|
||||
for c in api_containers()
|
||||
]
|
||||
if states and all(state == "healthy" for state in states):
|
||||
return
|
||||
time.sleep(1)
|
||||
raise SystemExit("API replicas are not healthy after restart")
|
||||
|
||||
|
||||
class Sampler(threading.Thread):
|
||||
"""Опрос памяти одного контейнера изнутри одним долгим docker exec"""
|
||||
|
||||
def __init__(self, container: str) -> None:
|
||||
super().__init__(daemon=True)
|
||||
self.container = container
|
||||
self.cgroup_max = 0
|
||||
self.rss_max = 0
|
||||
self.samples = 0
|
||||
self._process = subprocess.Popen(
|
||||
["docker", "exec", container, "sh", "-c", SAMPLER],
|
||||
stdout=subprocess.PIPE, text=True,
|
||||
)
|
||||
|
||||
def run(self) -> None:
|
||||
assert self._process.stdout is not None
|
||||
for line in self._process.stdout:
|
||||
parts = line.split()
|
||||
if len(parts) == 2:
|
||||
self.cgroup_max = max(self.cgroup_max, int(parts[0]))
|
||||
self.rss_max = max(self.rss_max, int(parts[1]) * 1024)
|
||||
self.samples += 1
|
||||
|
||||
def stop(self) -> None:
|
||||
self._process.terminate()
|
||||
self._process.wait()
|
||||
|
||||
|
||||
def upload_once(base: str, token: str, chat_id: str, content: bytes, content_type: str) -> dict:
|
||||
request = urllib.request.Request(
|
||||
f"{base}/api/v1/chats/{chat_id}/attachments?filename=experiment",
|
||||
data=content,
|
||||
method="POST",
|
||||
headers={"Authorization": "Bearer " + token, "Content-Type": content_type},
|
||||
)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=REQUEST_TIMEOUT_SEC) as response:
|
||||
body, status, instance = response.read(), response.status, response.headers.get("X-Instance-Id")
|
||||
except urllib.error.HTTPError as error:
|
||||
body, status, instance = error.read(), error.code, error.headers.get("X-Instance-Id")
|
||||
except OSError as error:
|
||||
return {"status": None, "error": type(error).__name__, "seconds": time.monotonic() - started}
|
||||
result = {"status": status, "instance": instance, "seconds": time.monotonic() - started}
|
||||
if status == 202:
|
||||
result["id"] = json.loads(body)["id"]
|
||||
return result
|
||||
|
||||
|
||||
def percentile(values: list[float], share: float) -> float:
|
||||
ordered = sorted(values)
|
||||
return ordered[min(len(ordered) - 1, round(share * (len(ordered) - 1)))]
|
||||
|
||||
|
||||
def run_round(client: Client, actor: dict, chat: dict, profile: str, content: bytes,
|
||||
content_type: str, concurrency: int, restart: bool) -> dict:
|
||||
if restart:
|
||||
restart_api(client.base)
|
||||
containers = api_containers()
|
||||
# Прогрев: первый запрос каждой реплики открывает пулы БД/S3
|
||||
warmup = png_noise(4, 4)
|
||||
for _ in range(2 * len(containers)):
|
||||
upload_once(client.base, actor["token"], chat["id"], warmup, "image/png")
|
||||
before = {container_name(c): process_status(c) for c in containers}
|
||||
samplers = [Sampler(c) for c in containers]
|
||||
for sampler in samplers:
|
||||
sampler.start()
|
||||
time.sleep(1)
|
||||
|
||||
barrier = threading.Barrier(concurrency)
|
||||
results: list[dict] = [{} for _ in range(concurrency)]
|
||||
|
||||
def worker(index: int) -> None:
|
||||
barrier.wait()
|
||||
results[index] = upload_once(client.base, actor["token"], chat["id"], content, content_type)
|
||||
|
||||
started = time.monotonic()
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(concurrency)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
wall = time.monotonic() - started
|
||||
time.sleep(1)
|
||||
for sampler in samplers:
|
||||
sampler.stop()
|
||||
after = {container_name(c): process_status(c) for c in containers}
|
||||
|
||||
ok = [r for r in results if r.get("status") == 202]
|
||||
sample = ok[0]["id"] if ok else None
|
||||
checksum_ok = None
|
||||
if sample:
|
||||
client.wait_ready(actor, {"id": sample})
|
||||
downloaded = client.call("GET", f"/api/v1/attachments/{sample}/content", actor=actor)["bytes"]
|
||||
checksum_ok = hashlib.sha256(downloaded).digest() == hashlib.sha256(content).digest()
|
||||
seconds = [r["seconds"] for r in ok]
|
||||
mib = 1024 * 1024
|
||||
replicas = {}
|
||||
for sampler in samplers:
|
||||
name = container_name(sampler.container)
|
||||
replicas[name] = {
|
||||
"uploads_served": sum(1 for r in ok if r.get("instance") == after[name].get("instance")),
|
||||
"rss_before_mib": round(before[name]["VmRSS"] / mib, 1),
|
||||
"rss_peak_process_vmhwm_mib": round(after[name]["VmHWM"] / mib, 1),
|
||||
"rss_peak_sampled_mib": round(sampler.rss_max / mib, 1),
|
||||
"cgroup_peak_sampled_mib": round(sampler.cgroup_max / mib, 1),
|
||||
"samples": sampler.samples,
|
||||
}
|
||||
by_instance: dict[str, int] = {}
|
||||
for r in ok:
|
||||
by_instance[r["instance"]] = by_instance.get(r["instance"], 0) + 1
|
||||
return {
|
||||
"profile": profile,
|
||||
"file_bytes": len(content),
|
||||
"concurrency": concurrency,
|
||||
"replicas_restarted_before": restart,
|
||||
"status_counts": {str(s): sum(1 for r in results if r.get("status") == s)
|
||||
for s in sorted({r.get("status") for r in results}, key=str)},
|
||||
"uploads_by_instance": by_instance,
|
||||
"wall_seconds": round(wall, 2),
|
||||
"latency_seconds": {
|
||||
"p50": round(percentile(seconds, 0.5), 3),
|
||||
"p95": round(percentile(seconds, 0.95), 3),
|
||||
"max": round(max(seconds), 3),
|
||||
} if seconds else None,
|
||||
"throughput_mib_per_second": round(len(ok) * len(content) / mib / wall, 1),
|
||||
"download_sha256_matches": checksum_ok,
|
||||
"replicas": replicas,
|
||||
}
|
||||
|
||||
|
||||
def environment() -> dict:
|
||||
info = json.loads(subprocess.run(
|
||||
["docker", "info", "--format", "{{json .}}"], capture_output=True, text=True, check=True
|
||||
).stdout)
|
||||
settings = {}
|
||||
for line in (ROOT / ".env").read_text().splitlines():
|
||||
key = line.split("=", 1)[0]
|
||||
if key in {"IMAGE_INSPECT_WORKERS", "UPLOAD_SPOOL_MEMORY_BYTES", "MAX_UPLOAD_BYTES",
|
||||
"MAX_IMAGE_PIXELS", "API_REPLICAS", "S3_POOL_MAX_SIZE"}:
|
||||
settings[key] = line.split("=", 1)[1]
|
||||
return {
|
||||
"host": f"{platform.system()} {platform.machine()}",
|
||||
"docker": info.get("ServerVersion"),
|
||||
"docker_vm_cpus": info.get("NCPU"),
|
||||
"docker_vm_memory_gib": round(info.get("MemTotal", 0) / 2**30, 1),
|
||||
"api_limits": "cpus 1.0, memory 512M per replica (compose.yaml)",
|
||||
"s3": "chrislusf/seaweedfs:4.48",
|
||||
"settings": settings,
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
|
||||
parser.add_argument("--base-url", default=None)
|
||||
parser.add_argument("--output", default="evidence/experiment-uploads.json")
|
||||
parser.add_argument("--concurrency", default="10,20")
|
||||
parser.add_argument("--profiles", default="png,jpeg")
|
||||
parser.add_argument("--no-restart", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
client = Client(args.base_url)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
payloads = {}
|
||||
if "png" in args.profiles:
|
||||
payloads["png"] = (png_noise(1750, 1750), "image/png")
|
||||
if "jpeg" in args.profiles:
|
||||
try:
|
||||
payloads["jpeg"] = (jpeg_worst_case(), "image/jpeg")
|
||||
except ImportError:
|
||||
print("jpeg profile skipped: Pillow is not installed (use poetry run)")
|
||||
report = {"environment": environment(), "rounds": []}
|
||||
for profile, (content, content_type) in payloads.items():
|
||||
for concurrency in (int(x) for x in args.concurrency.split(",")):
|
||||
result = run_round(client, alice, chat, profile, content, content_type,
|
||||
concurrency, not args.no_restart)
|
||||
report["rounds"].append(result)
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
output = ROOT / args.output
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
||||
print("saved:", args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,4 @@
|
||||
from .api import app as api_endpoints
|
||||
from .api import health_router as health_endpoints
|
||||
|
||||
__all__ = ["api_endpoints", "health_endpoints"]
|
||||
@@ -0,0 +1,10 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .health import health_router
|
||||
from .routers import app as v1_endpoints
|
||||
|
||||
app = APIRouter(prefix="/api")
|
||||
|
||||
app.include_router(v1_endpoints)
|
||||
|
||||
__all__ = ["app", "health_router"]
|
||||
@@ -0,0 +1,4 @@
|
||||
from .auth import get_current_user
|
||||
from .security import basic_credentials, bearer_token
|
||||
|
||||
__all__ = ["get_current_user", "basic_credentials", "bearer_token"]
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import Depends
|
||||
|
||||
from domain import UserSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import AuthService
|
||||
|
||||
from .security import bearer_token
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str | None = Depends(bearer_token),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> UserSchema:
|
||||
"""Владелец действующей Bearer-сессии; X-User-Id не учитывается; иначе 401"""
|
||||
return await service.authenticate(token)
|
||||
@@ -0,0 +1,52 @@
|
||||
import base64
|
||||
import binascii
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from fastapi.security.http import HTTPBase
|
||||
|
||||
from domain import BasicCredentialsSchema
|
||||
from domain.errors import InvalidCredentialsError
|
||||
|
||||
BASIC_SCHEME = "basic"
|
||||
|
||||
basic_scheme = HTTPBase(
|
||||
scheme=BASIC_SCHEME,
|
||||
scheme_name="basicAuth",
|
||||
description="Только для POST /auth/sessions",
|
||||
auto_error=False,
|
||||
)
|
||||
|
||||
bearer_scheme = HTTPBearer(
|
||||
scheme_name="bearerAuth",
|
||||
bearerFormat="opaque session token",
|
||||
auto_error=False,
|
||||
)
|
||||
|
||||
|
||||
async def basic_credentials(
|
||||
authorization: HTTPAuthorizationCredentials | None = Depends(basic_scheme),
|
||||
) -> BasicCredentialsSchema:
|
||||
"""
|
||||
Authorization: Basic base64(username:password)
|
||||
|
||||
username — до первого двоеточия, остальное — пароль
|
||||
Любая ошибка формата — тот же 401, что и неверный пароль.
|
||||
"""
|
||||
if authorization is None or authorization.scheme.lower() != BASIC_SCHEME:
|
||||
raise InvalidCredentialsError()
|
||||
try:
|
||||
decoded = base64.b64decode(authorization.credentials, validate=True)
|
||||
username, separator, password = decoded.decode("utf-8").partition(":")
|
||||
except (binascii.Error, UnicodeDecodeError):
|
||||
raise InvalidCredentialsError() from None
|
||||
if not separator or not username.isascii():
|
||||
raise InvalidCredentialsError()
|
||||
return BasicCredentialsSchema(username=username, password=password)
|
||||
|
||||
|
||||
async def bearer_token(
|
||||
authorization: HTTPAuthorizationCredentials | None = Depends(bearer_scheme),
|
||||
) -> str | None:
|
||||
"""Токен из Authorization: Bearer; другие схемы (Basic) — как отсутствие"""
|
||||
return authorization.credentials if authorization is not None else None
|
||||
@@ -0,0 +1,3 @@
|
||||
from .exceptions import register_exception_handlers
|
||||
|
||||
__all__ = ["register_exception_handlers"]
|
||||
@@ -0,0 +1,61 @@
|
||||
from http import HTTPStatus
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from application.api.utils import error_response, get_request_id
|
||||
from domain.errors import AppError, ErrorCode, InvalidRequestError, NotFoundError
|
||||
|
||||
|
||||
async def app_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Доменные ошибки сервисов и репозиториев"""
|
||||
assert isinstance(error, AppError)
|
||||
return error_response(
|
||||
get_request_id(request),
|
||||
error.status_code,
|
||||
error.code,
|
||||
error.message,
|
||||
error.headers,
|
||||
)
|
||||
|
||||
|
||||
async def validation_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Malformed JSON, лишние поля, неверные типы, uuid и limit — 400"""
|
||||
assert isinstance(error, RequestValidationError)
|
||||
return error_response(
|
||||
get_request_id(request),
|
||||
InvalidRequestError.status_code,
|
||||
InvalidRequestError.code,
|
||||
_describe_validation_error(error),
|
||||
)
|
||||
|
||||
|
||||
async def http_error_handler(request: Request, error: Exception) -> JSONResponse:
|
||||
"""Ошибки маршрутизации неизвестный путь, неподдерживаемый метод"""
|
||||
assert isinstance(error, StarletteHTTPException)
|
||||
if error.status_code == HTTPStatus.NOT_FOUND:
|
||||
code, message = NotFoundError.code, NotFoundError.default_message
|
||||
elif error.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR:
|
||||
code, message = AppError.code, AppError.default_message
|
||||
else:
|
||||
code, message = ErrorCode.INVALID_REQUEST, HTTPStatus(error.status_code).phrase
|
||||
return error_response(
|
||||
get_request_id(request), error.status_code, code, message, error.headers
|
||||
)
|
||||
|
||||
|
||||
def _describe_validation_error(error: RequestValidationError) -> str:
|
||||
"""Безопасное описание: только тип и путь поля, без присланного значения"""
|
||||
first = error.errors()[0]
|
||||
if first.get("type") == "json_invalid":
|
||||
return "Некорректный JSON в теле запроса"
|
||||
location = ".".join(str(part) for part in first.get("loc", ()))
|
||||
return f"Некорректное значение: {location}"
|
||||
|
||||
|
||||
def register_exception_handlers(app: FastAPI) -> None:
|
||||
app.add_exception_handler(AppError, app_error_handler)
|
||||
app.add_exception_handler(RequestValidationError, validation_error_handler)
|
||||
app.add_exception_handler(StarletteHTTPException, http_error_handler)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import health_router
|
||||
|
||||
__all__ = ["health_router"]
|
||||
@@ -0,0 +1,49 @@
|
||||
from fastapi import APIRouter, Depends, Request, status
|
||||
|
||||
from domain import HealthSchema
|
||||
from domain.errors import UnavailableError
|
||||
from infrastructure.dependencies import (
|
||||
DatabasePoolManager,
|
||||
ObjectStorageManager,
|
||||
get_database,
|
||||
get_storage,
|
||||
)
|
||||
|
||||
health_router = APIRouter(prefix="/health", tags=["Health"])
|
||||
|
||||
|
||||
@health_router.get(
|
||||
"/live",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=HealthSchema,
|
||||
)
|
||||
async def liveness() -> HealthSchema:
|
||||
"""Процесс жив и обрабатывает хттп"""
|
||||
return HealthSchema()
|
||||
|
||||
|
||||
@health_router.get(
|
||||
"/ready",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=HealthSchema,
|
||||
)
|
||||
async def readiness(
|
||||
request: Request,
|
||||
database: DatabasePoolManager = Depends(get_database),
|
||||
storage: ObjectStorageManager = Depends(get_storage),
|
||||
) -> HealthSchema:
|
||||
"""
|
||||
Приложение не завершается, PostgreSQL и S3 отвечают: с lab4 загрузка
|
||||
и скачивание вложений входят в обязательный синхронный путь.
|
||||
|
||||
Valkey не критичен: без него сессии читаются из PostgreSQL. Без S3
|
||||
недоступны только вложения — nginx не исключает реплику по readiness,
|
||||
поэтому сообщения продолжают работать, а реплика честно сообщает 503.
|
||||
"""
|
||||
if not getattr(request.app.state, "is_ready", False):
|
||||
raise UnavailableError()
|
||||
if not await database.check():
|
||||
raise UnavailableError("бд недоступна")
|
||||
if not await storage.check():
|
||||
raise UnavailableError("хранилище файлов недоступно")
|
||||
return HealthSchema()
|
||||
@@ -0,0 +1,3 @@
|
||||
from .request_context import RequestContextMiddleware
|
||||
|
||||
__all__ = ["RequestContextMiddleware"]
|
||||
@@ -0,0 +1,76 @@
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
||||
|
||||
from application.api.middlewares.telemetry import normalize_method, route_template
|
||||
from application.api.utils import INSTANCE_ID_HEADER, REQUEST_ID_HEADER, error_response
|
||||
from config import INSTANCE_ID, LOGGER
|
||||
from domain.errors import AppError
|
||||
|
||||
HEALTH_PATH_PREFIX = "/health/"
|
||||
|
||||
INCOMING_REQUEST_ID = re.compile(r"^[0-9a-f]{32}$")
|
||||
|
||||
|
||||
class RequestContextMiddleware(BaseHTTPMiddleware):
|
||||
"""
|
||||
X-Request-Id и X-Instance-Id в каждом ответе, журнал запросов и защита
|
||||
от необработанных ошибок.
|
||||
|
||||
X-Request-Id берётся у балансировщика (nginx выставляет $request_id),
|
||||
поэтому одна строка access-лога LB и журнал реплики связаны одним ID.
|
||||
В лог попадают метод, путь, статус и длительность — без тел запросов,
|
||||
query-параметров и текста сообщений.
|
||||
"""
|
||||
|
||||
async def dispatch(
|
||||
self, request: Request, call_next: RequestResponseEndpoint
|
||||
) -> Response:
|
||||
request_id = self._request_id(request)
|
||||
request.state.request_id = request_id
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
LOGGER.bind(event="http.unhandled", request_id=request_id).error(
|
||||
"Необработанная ошибка"
|
||||
)
|
||||
error = AppError()
|
||||
response = error_response(
|
||||
request_id, error.status_code, error.code, error.message
|
||||
)
|
||||
response.headers[REQUEST_ID_HEADER] = request_id
|
||||
response.headers[INSTANCE_ID_HEADER] = INSTANCE_ID
|
||||
if getattr(request.app.state, "is_draining", False):
|
||||
response.headers["Connection"] = "close"
|
||||
self._log_request(request, response, request_id, started)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _request_id(request: Request) -> str:
|
||||
incoming = request.headers.get(REQUEST_ID_HEADER, "")
|
||||
if INCOMING_REQUEST_ID.fullmatch(incoming):
|
||||
return incoming
|
||||
return uuid.uuid4().hex
|
||||
|
||||
@staticmethod
|
||||
def _log_request(
|
||||
request: Request, response: Response, request_id: str, started: float
|
||||
) -> None:
|
||||
duration_ms = (time.perf_counter() - started) * 1000
|
||||
if response.status_code >= 500:
|
||||
level = "ERROR"
|
||||
elif request.url.path.startswith(HEALTH_PATH_PREFIX):
|
||||
level = "DEBUG"
|
||||
else:
|
||||
level = "INFO"
|
||||
LOGGER.bind(
|
||||
event="http.response",
|
||||
request_id=request_id,
|
||||
method=normalize_method(request.method),
|
||||
route=route_template(request.scope),
|
||||
status_code=response.status_code,
|
||||
).log(level, f"HTTP {response.status_code} за {duration_ms:.1f} мс")
|
||||
@@ -0,0 +1,81 @@
|
||||
import time
|
||||
|
||||
from opentelemetry import trace
|
||||
from opentelemetry.trace import SpanKind, Status, StatusCode
|
||||
|
||||
from infrastructure.observability import telemetry
|
||||
|
||||
BUSINESS_PREFIX = "/api/v1"
|
||||
|
||||
KNOWN_METHODS = frozenset({"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"})
|
||||
|
||||
|
||||
def route_template(scope):
|
||||
route = scope.get("route")
|
||||
path = getattr(route, "path", None)
|
||||
if path is None:
|
||||
return "unmatched"
|
||||
if scope["path"].startswith(BUSINESS_PREFIX + "/") and not path.startswith(
|
||||
BUSINESS_PREFIX + "/"
|
||||
):
|
||||
return BUSINESS_PREFIX + path
|
||||
return path
|
||||
|
||||
|
||||
def normalize_method(method):
|
||||
return method if method in KNOWN_METHODS else "OTHER"
|
||||
|
||||
|
||||
class TelemetryMiddleware:
|
||||
"""Только бизнес-HTTP: route template, полный ASGI response, без URL/headers/body."""
|
||||
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope["type"] != "http" or not scope["path"].startswith(
|
||||
BUSINESS_PREFIX + "/"
|
||||
):
|
||||
await self.app(scope, receive, send)
|
||||
return
|
||||
headers = {
|
||||
key.decode("latin1"): value.decode("latin1")
|
||||
for key, value in scope["headers"]
|
||||
if key in {b"traceparent", b"tracestate"}
|
||||
}
|
||||
method = normalize_method(scope["method"])
|
||||
started, status = time.perf_counter(), 500
|
||||
with telemetry.span(
|
||||
"HTTP",
|
||||
{"http.request.method": method},
|
||||
kind=SpanKind.SERVER,
|
||||
context=telemetry.extract(headers),
|
||||
root=True,
|
||||
) as span:
|
||||
|
||||
async def send_wrapper(message):
|
||||
nonlocal status
|
||||
if message["type"] == "http.response.start":
|
||||
status = message["status"]
|
||||
span_context = trace.get_current_span().get_span_context()
|
||||
if span_context.is_valid:
|
||||
message.setdefault("headers", []).append(
|
||||
(b"x-trace-id", f"{span_context.trace_id:032x}".encode())
|
||||
)
|
||||
await send(message)
|
||||
|
||||
try:
|
||||
await self.app(scope, receive, send_wrapper)
|
||||
finally:
|
||||
route = route_template(scope)
|
||||
span.update_name(f"{method} {route}")
|
||||
span.set_attribute("http.route", route)
|
||||
span.set_attribute("http.response.status_code", status)
|
||||
request_id = scope.get("state", {}).get("request_id")
|
||||
if request_id:
|
||||
span.set_attribute("request_id", request_id)
|
||||
if status >= 500:
|
||||
span.set_status(Status(StatusCode.ERROR))
|
||||
telemetry.record_http(
|
||||
time.perf_counter() - started, method, route, status
|
||||
)
|
||||
@@ -0,0 +1,17 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .attachments import attachments_router
|
||||
from .auth import auth_router
|
||||
from .chats import chats_router
|
||||
from .messages import messages_router
|
||||
from .users import users_router
|
||||
|
||||
app = APIRouter(prefix="/v1")
|
||||
|
||||
app.include_router(users_router)
|
||||
app.include_router(auth_router)
|
||||
app.include_router(chats_router)
|
||||
app.include_router(messages_router)
|
||||
app.include_router(attachments_router)
|
||||
|
||||
__all__ = ["app"]
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import attachments_router
|
||||
|
||||
__all__ = ["attachments_router"]
|
||||
@@ -0,0 +1,99 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, Query, Request, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
from starlette.background import BackgroundTask
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from application.api.utils import content_disposition, request_chunks
|
||||
from domain import (
|
||||
AttachmentContentType,
|
||||
AttachmentFilename,
|
||||
AttachmentSchema,
|
||||
AttachmentVariant,
|
||||
UserSchema,
|
||||
)
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import AttachmentService
|
||||
|
||||
attachments_router = APIRouter(tags=["Attachments"])
|
||||
|
||||
IMAGE_BODY = {
|
||||
content_type: {"schema": {"type": "string", "format": "binary"}}
|
||||
for content_type in AttachmentContentType
|
||||
}
|
||||
|
||||
|
||||
@attachments_router.post(
|
||||
"/chats/{chat_id}/attachments",
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
response_model=AttachmentSchema,
|
||||
openapi_extra={"requestBody": {"required": True, "content": IMAGE_BODY}},
|
||||
)
|
||||
async def upload_attachment(
|
||||
chat_id: UUID,
|
||||
filename: Annotated[AttachmentFilename, Query()],
|
||||
request: Request,
|
||||
content_type: Annotated[str | None, Header()] = None,
|
||||
content_length: Annotated[int | None, Header(ge=0)] = None,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
|
||||
) -> AttachmentSchema:
|
||||
"""
|
||||
Сырые байты одного PNG/JPEG, не multipart. 202 queued — после staging и транзакции job/outbox; resize выполняет worker.
|
||||
"""
|
||||
return await service.upload(
|
||||
current_user,
|
||||
chat_id,
|
||||
filename,
|
||||
content_type,
|
||||
content_length,
|
||||
request_chunks(request),
|
||||
)
|
||||
|
||||
|
||||
@attachments_router.get(
|
||||
"/attachments/{attachment_id}",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=AttachmentSchema,
|
||||
)
|
||||
async def get_attachment(
|
||||
attachment_id: UUID,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
|
||||
) -> AttachmentSchema:
|
||||
"""Метаданные доступны только участникам чата вложения; остальным — 404"""
|
||||
return await service.get_attachment(current_user, attachment_id)
|
||||
|
||||
|
||||
@attachments_router.get(
|
||||
"/attachments/{attachment_id}/content",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_class=StreamingResponse,
|
||||
responses={200: {"content": IMAGE_BODY}},
|
||||
)
|
||||
async def download_attachment(
|
||||
attachment_id: UUID,
|
||||
variant: Annotated[AttachmentVariant, Query()] = AttachmentVariant.ORIGINAL,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: AttachmentService = Depends(ServiceCreator.get_attachment_service),
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
Байты через API, без redirect на S3: доступ проверяет приложение.
|
||||
Ответ идёт потоком из S3, файл целиком в памяти реплики не собирается.
|
||||
"""
|
||||
attachment, content = await service.open_content(
|
||||
current_user, attachment_id, variant
|
||||
)
|
||||
return StreamingResponse(
|
||||
content.iter_chunks(),
|
||||
media_type=attachment.content_type,
|
||||
headers={
|
||||
"Content-Length": str(content.content_length),
|
||||
"Content-Disposition": content_disposition(attachment.filename),
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
background=BackgroundTask(content.close),
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import auth_router
|
||||
|
||||
__all__ = ["auth_router"]
|
||||
@@ -0,0 +1,35 @@
|
||||
from fastapi import APIRouter, Depends, Response, status
|
||||
|
||||
from application.api.dependencies import basic_credentials, bearer_token
|
||||
from domain import BasicCredentialsSchema, SessionSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import AuthService
|
||||
|
||||
auth_router = APIRouter(prefix="/auth/sessions", tags=["Auth"])
|
||||
|
||||
|
||||
@auth_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=SessionSchema,
|
||||
)
|
||||
async def create_session(
|
||||
credentials: BasicCredentialsSchema = Depends(basic_credentials),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> SessionSchema:
|
||||
"""Вход по Basic, без JSON body; возвращает Bearer-токен"""
|
||||
return await service.login(credentials)
|
||||
|
||||
|
||||
@auth_router.delete(
|
||||
"/current",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
response_class=Response,
|
||||
)
|
||||
async def revoke_session(
|
||||
token: str | None = Depends(bearer_token),
|
||||
service: AuthService = Depends(ServiceCreator.get_auth_service),
|
||||
) -> Response:
|
||||
"""Logout: текущий токен перестаёт действовать; повтор — 401"""
|
||||
await service.logout(token)
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import chats_router
|
||||
|
||||
__all__ = ["chats_router"]
|
||||
@@ -0,0 +1,58 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Query, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import (
|
||||
ChatPageSchema,
|
||||
ChatSchema,
|
||||
CreateChatSchema,
|
||||
UserSchema,
|
||||
)
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import ChatService
|
||||
|
||||
chats_router = APIRouter(prefix="/chats", tags=["Chats"])
|
||||
|
||||
|
||||
@chats_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=ChatSchema,
|
||||
)
|
||||
async def create_chat(
|
||||
body: Annotated[CreateChatSchema, Body(...)],
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatSchema:
|
||||
"""Вызывающий должен входить в member_ids"""
|
||||
return await service.create_chat(current_user, body)
|
||||
|
||||
|
||||
@chats_router.get(
|
||||
"",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=ChatPageSchema,
|
||||
)
|
||||
async def list_chats(
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatPageSchema:
|
||||
"""Чаты текущего пользователя по (created_at, id) по возрастанию"""
|
||||
return await service.list_chats(current_user, limit, cursor)
|
||||
|
||||
|
||||
@chats_router.get(
|
||||
"/{chat_id}",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=ChatSchema,
|
||||
)
|
||||
async def get_chat(
|
||||
chat_id: UUID,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
||||
) -> ChatSchema:
|
||||
return await service.get_chat(current_user, chat_id)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import messages_router
|
||||
|
||||
__all__ = ["messages_router"]
|
||||
@@ -0,0 +1,63 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Header, Query, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
CreateMessageSchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
UserSchema,
|
||||
)
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import MessageService
|
||||
|
||||
messages_router = APIRouter(
|
||||
prefix="/chats/{chat_id}/messages", tags=["Messages"]
|
||||
)
|
||||
|
||||
|
||||
@messages_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=MessageSchema,
|
||||
)
|
||||
async def send_message(
|
||||
chat_id: UUID,
|
||||
body: Annotated[CreateMessageSchema, Body(...)],
|
||||
idempotency_key: Annotated[
|
||||
str,
|
||||
Header(
|
||||
alias="Idempotency-Key",
|
||||
min_length=1,
|
||||
max_length=IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
pattern=IDEMPOTENCY_KEY_PATTERN,
|
||||
),
|
||||
],
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: MessageService = Depends(ServiceCreator.get_message_service),
|
||||
) -> MessageSchema:
|
||||
"""
|
||||
Idempotency-Key обязателен: повтор того же тела возвращает тот же 201
|
||||
и то же сообщение, другое тело с тем же ключом — 409. Нет/неверный ключ — 400.
|
||||
"""
|
||||
return await service.send_message(current_user, chat_id, body, idempotency_key)
|
||||
|
||||
|
||||
@messages_router.get(
|
||||
"",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=MessagePageSchema,
|
||||
)
|
||||
async def list_messages(
|
||||
chat_id: UUID,
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
current_user: UserSchema = Depends(get_current_user),
|
||||
service: MessageService = Depends(ServiceCreator.get_message_service),
|
||||
) -> MessagePageSchema:
|
||||
"""История чата: keyset cursor по (created_at, id) по возрастанию"""
|
||||
return await service.list_messages(current_user, chat_id, limit, cursor)
|
||||
@@ -0,0 +1,3 @@
|
||||
from .endpoints import users_router
|
||||
|
||||
__all__ = ["users_router"]
|
||||
@@ -0,0 +1,33 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, status
|
||||
|
||||
from application.api.dependencies import get_current_user
|
||||
from domain import CreateUserSchema, UserSchema
|
||||
from infrastructure.dependencies import ServiceCreator
|
||||
from infrastructure.services import UserService
|
||||
|
||||
users_router = APIRouter(prefix="/users", tags=["Users"])
|
||||
|
||||
|
||||
@users_router.post(
|
||||
"",
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
response_model=UserSchema,
|
||||
openapi_extra={"security": []},
|
||||
)
|
||||
async def register_user(
|
||||
body: Annotated[CreateUserSchema, Body(...)],
|
||||
service: UserService = Depends(ServiceCreator.get_user_service),
|
||||
) -> UserSchema:
|
||||
"""Публичная регистрация; username уникален"""
|
||||
return await service.register(body)
|
||||
|
||||
|
||||
@users_router.get(
|
||||
"/me",
|
||||
status_code=status.HTTP_200_OK,
|
||||
response_model=UserSchema,
|
||||
)
|
||||
async def get_me(current_user: UserSchema = Depends(get_current_user)) -> UserSchema:
|
||||
return current_user
|
||||
@@ -0,0 +1,16 @@
|
||||
from .errors import (
|
||||
INSTANCE_ID_HEADER,
|
||||
REQUEST_ID_HEADER,
|
||||
error_response,
|
||||
get_request_id,
|
||||
)
|
||||
from .streams import content_disposition, request_chunks
|
||||
|
||||
__all__ = [
|
||||
"REQUEST_ID_HEADER",
|
||||
"INSTANCE_ID_HEADER",
|
||||
"error_response",
|
||||
"get_request_id",
|
||||
"request_chunks",
|
||||
"content_disposition",
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
from collections.abc import Mapping
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from domain.errors import ErrorBodySchema, ErrorCode, ErrorSchema
|
||||
|
||||
REQUEST_ID_HEADER = "X-Request-Id"
|
||||
INSTANCE_ID_HEADER = "X-Instance-Id"
|
||||
|
||||
|
||||
def get_request_id(request: Request) -> str:
|
||||
"""ID запроса, назначенный RequestContextMiddleware"""
|
||||
request_id: str = request.state.request_id
|
||||
return request_id
|
||||
|
||||
|
||||
def error_response(
|
||||
request_id: str,
|
||||
status_code: int,
|
||||
code: ErrorCode,
|
||||
message: str,
|
||||
headers: Mapping[str, str] | None = None,
|
||||
) -> JSONResponse:
|
||||
"""Ответ в едином формате ошибки контракта"""
|
||||
body = ErrorSchema(
|
||||
error=ErrorBodySchema(code=code, message=message, request_id=request_id)
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=status_code,
|
||||
content=body.model_dump(mode="json"),
|
||||
headers={**(headers or {}), REQUEST_ID_HEADER: request_id},
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import Request
|
||||
from starlette.requests import ClientDisconnect
|
||||
|
||||
from domain.errors import InvalidRequestError
|
||||
|
||||
|
||||
async def request_chunks(request: Request) -> AsyncIterator[bytes]:
|
||||
"""
|
||||
Тело запроса потоком, без буферизации целиком. Обрыв соединения клиентом
|
||||
— доменная ошибка, а не необработанное исключение (500 в журнале).
|
||||
"""
|
||||
try:
|
||||
async for chunk in request.stream():
|
||||
if chunk:
|
||||
yield chunk
|
||||
except ClientDisconnect:
|
||||
raise InvalidRequestError("Загрузка прервана клиентом") from None
|
||||
|
||||
|
||||
def content_disposition(filename: str) -> str:
|
||||
"""
|
||||
inline с именем по RFC 6266/5987: имя целиком percent-encoded, поэтому
|
||||
кавычки, ; и переводы строк не могут разорвать заголовок.
|
||||
"""
|
||||
return f"inline; filename*=UTF-8''{quote(filename, safe='')}"
|
||||
@@ -0,0 +1,107 @@
|
||||
from .log_config import LOGGER
|
||||
from .settings import (
|
||||
APP_ENV,
|
||||
APP_HOST,
|
||||
APP_KEEPALIVE_TIMEOUT_SEC,
|
||||
APP_PORT,
|
||||
APP_REVISION,
|
||||
ATTACHMENT_PENDING_GRACE_SEC,
|
||||
ATTACHMENT_STORE_TIMEOUT_SEC,
|
||||
DB_COMMAND_TIMEOUT_SEC,
|
||||
DB_CONNECT_TIMEOUT_SEC,
|
||||
DB_HOST,
|
||||
DB_NAME,
|
||||
DB_PASS,
|
||||
DB_POOL_MAX_SIZE,
|
||||
DB_POOL_MIN_SIZE,
|
||||
DB_PORT,
|
||||
DB_STARTUP_TIMEOUT_SEC,
|
||||
DB_USER,
|
||||
IDEMPOTENCY_TTL_SECONDS,
|
||||
IMAGE_INSPECT_WORKERS,
|
||||
INSTANCE_ID,
|
||||
INSTANCE_ID_FILE,
|
||||
JANITOR_HEARTBEAT_FILE,
|
||||
JANITOR_INTERVAL_SEC,
|
||||
LOG_LEVEL,
|
||||
MAX_IMAGE_PIXELS,
|
||||
MAX_UPLOAD_BYTES,
|
||||
PASSWORD_HASH_WORKERS,
|
||||
S3_ACCESS_KEY_ID_FILE,
|
||||
S3_ADMIN_ACCESS_KEY_ID_FILE,
|
||||
S3_ADMIN_SECRET_ACCESS_KEY_FILE,
|
||||
S3_BUCKET,
|
||||
S3_CONNECT_TIMEOUT_SEC,
|
||||
S3_ENDPOINT,
|
||||
S3_MAX_ATTEMPTS,
|
||||
S3_POOL_MAX_SIZE,
|
||||
S3_READ_TIMEOUT_SEC,
|
||||
S3_REGION,
|
||||
S3_SECRET_ACCESS_KEY_FILE,
|
||||
S3_STARTUP_TIMEOUT_SEC,
|
||||
SESSION_CACHE_TTL_SECONDS,
|
||||
SESSION_TTL_SECONDS,
|
||||
SHUTDOWN_DRAIN_SEC,
|
||||
SHUTDOWN_TIMEOUT_SEC,
|
||||
UPLOAD_SPOOL_MEMORY_BYTES,
|
||||
VALKEY_HOST,
|
||||
VALKEY_PASS,
|
||||
VALKEY_POOL_MAX_SIZE,
|
||||
VALKEY_PORT,
|
||||
VALKEY_RETRY_AFTER_SEC,
|
||||
VALKEY_TIMEOUT_SEC,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"LOGGER",
|
||||
"APP_HOST",
|
||||
"APP_PORT",
|
||||
"APP_ENV",
|
||||
"APP_REVISION",
|
||||
"APP_KEEPALIVE_TIMEOUT_SEC",
|
||||
"LOG_LEVEL",
|
||||
"SHUTDOWN_TIMEOUT_SEC",
|
||||
"SHUTDOWN_DRAIN_SEC",
|
||||
"DB_USER",
|
||||
"DB_PASS",
|
||||
"DB_NAME",
|
||||
"DB_HOST",
|
||||
"DB_PORT",
|
||||
"DB_POOL_MIN_SIZE",
|
||||
"DB_POOL_MAX_SIZE",
|
||||
"DB_CONNECT_TIMEOUT_SEC",
|
||||
"DB_COMMAND_TIMEOUT_SEC",
|
||||
"DB_STARTUP_TIMEOUT_SEC",
|
||||
"VALKEY_HOST",
|
||||
"VALKEY_PORT",
|
||||
"VALKEY_PASS",
|
||||
"VALKEY_POOL_MAX_SIZE",
|
||||
"VALKEY_TIMEOUT_SEC",
|
||||
"VALKEY_RETRY_AFTER_SEC",
|
||||
"SESSION_TTL_SECONDS",
|
||||
"SESSION_CACHE_TTL_SECONDS",
|
||||
"PASSWORD_HASH_WORKERS",
|
||||
"IDEMPOTENCY_TTL_SECONDS",
|
||||
"S3_ENDPOINT",
|
||||
"S3_REGION",
|
||||
"S3_BUCKET",
|
||||
"S3_ACCESS_KEY_ID_FILE",
|
||||
"S3_SECRET_ACCESS_KEY_FILE",
|
||||
"S3_ADMIN_ACCESS_KEY_ID_FILE",
|
||||
"S3_ADMIN_SECRET_ACCESS_KEY_FILE",
|
||||
"S3_POOL_MAX_SIZE",
|
||||
"S3_CONNECT_TIMEOUT_SEC",
|
||||
"S3_READ_TIMEOUT_SEC",
|
||||
"S3_MAX_ATTEMPTS",
|
||||
"S3_STARTUP_TIMEOUT_SEC",
|
||||
"MAX_UPLOAD_BYTES",
|
||||
"MAX_IMAGE_PIXELS",
|
||||
"UPLOAD_SPOOL_MEMORY_BYTES",
|
||||
"IMAGE_INSPECT_WORKERS",
|
||||
"ATTACHMENT_STORE_TIMEOUT_SEC",
|
||||
"ATTACHMENT_PENDING_GRACE_SEC",
|
||||
"JANITOR_INTERVAL_SEC",
|
||||
"INSTANCE_ID",
|
||||
"INSTANCE_ID_FILE",
|
||||
"JANITOR_HEARTBEAT_FILE",
|
||||
]
|
||||
@@ -0,0 +1,12 @@
|
||||
"""Сборщик логов"""
|
||||
|
||||
import sys
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from .settings import LOG_LEVEL
|
||||
|
||||
LOGGER = logger
|
||||
|
||||
LOGGER.remove()
|
||||
LOGGER.add(sys.stdout, level=LOG_LEVEL, diagnose=False)
|
||||
@@ -0,0 +1,90 @@
|
||||
import os
|
||||
import secrets
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from envparse import Env
|
||||
|
||||
env = Env()
|
||||
|
||||
ENV_FILE = Path(os.getenv("ENV_FILE", ".env"))
|
||||
if ENV_FILE.is_file():
|
||||
env.read_envfile(path=str(ENV_FILE))
|
||||
|
||||
APP_HOST = env.str("APP_HOST")
|
||||
APP_PORT = env.int("APP_PORT")
|
||||
APP_ENV = env.str("APP_ENV")
|
||||
APP_REVISION = env.str("APP_REVISION", default="dev")
|
||||
# Больше keepalive_timeout балансировщика: idle-соединение закрывает LB, а не API
|
||||
APP_KEEPALIVE_TIMEOUT_SEC = env.int("APP_KEEPALIVE_TIMEOUT_SEC")
|
||||
|
||||
LOG_LEVEL = env.str("LOG_LEVEL")
|
||||
SHUTDOWN_TIMEOUT_SEC = env.int("SHUTDOWN_TIMEOUT_SEC")
|
||||
SHUTDOWN_DRAIN_SEC = env.float("SHUTDOWN_DRAIN_SEC")
|
||||
|
||||
DB_USER = env.str("DB_USER")
|
||||
DB_PASS = env.str("DB_PASS")
|
||||
DB_NAME = env.str("DB_NAME")
|
||||
DB_HOST = env.str("DB_HOST")
|
||||
DB_PORT = env.int("DB_PORT")
|
||||
DB_POOL_MIN_SIZE = env.int("DB_POOL_MIN_SIZE")
|
||||
DB_POOL_MAX_SIZE = env.int("DB_POOL_MAX_SIZE")
|
||||
DB_CONNECT_TIMEOUT_SEC = env.float("DB_CONNECT_TIMEOUT_SEC")
|
||||
DB_COMMAND_TIMEOUT_SEC = env.float("DB_COMMAND_TIMEOUT_SEC")
|
||||
DB_STARTUP_TIMEOUT_SEC = env.float("DB_STARTUP_TIMEOUT_SEC")
|
||||
|
||||
VALKEY_HOST = env.str("VALKEY_HOST")
|
||||
VALKEY_PORT = env.int("VALKEY_PORT")
|
||||
VALKEY_PASS = env.str("VALKEY_PASS")
|
||||
VALKEY_POOL_MAX_SIZE = env.int("VALKEY_POOL_MAX_SIZE")
|
||||
VALKEY_TIMEOUT_SEC = env.float("VALKEY_TIMEOUT_SEC")
|
||||
VALKEY_RETRY_AFTER_SEC = env.float("VALKEY_RETRY_AFTER_SEC")
|
||||
|
||||
SESSION_TTL_SECONDS = env.int("SESSION_TTL_SECONDS")
|
||||
SESSION_CACHE_TTL_SECONDS = env.int("SESSION_CACHE_TTL_SECONDS")
|
||||
PASSWORD_HASH_WORKERS = env.int("PASSWORD_HASH_WORKERS")
|
||||
IDEMPOTENCY_TTL_SECONDS = env.int("IDEMPOTENCY_TTL_SECONDS")
|
||||
|
||||
# S3: ключи читаются из файлов-секретов при старте, в env только пути к ним
|
||||
S3_ENDPOINT = env.str("S3_ENDPOINT")
|
||||
S3_REGION = env.str("S3_REGION")
|
||||
S3_BUCKET = env.str("S3_BUCKET")
|
||||
S3_ACCESS_KEY_ID_FILE = Path(env.str("S3_ACCESS_KEY_ID_FILE"))
|
||||
S3_SECRET_ACCESS_KEY_FILE = Path(env.str("S3_SECRET_ACCESS_KEY_FILE"))
|
||||
# Административные ключи нужны только одноразовому s3-bootstrap, не API
|
||||
S3_ADMIN_ACCESS_KEY_ID_FILE = Path(env.str("S3_ADMIN_ACCESS_KEY_ID_FILE"))
|
||||
S3_ADMIN_SECRET_ACCESS_KEY_FILE = Path(env.str("S3_ADMIN_SECRET_ACCESS_KEY_FILE"))
|
||||
S3_POOL_MAX_SIZE = env.int("S3_POOL_MAX_SIZE")
|
||||
S3_CONNECT_TIMEOUT_SEC = env.float("S3_CONNECT_TIMEOUT_SEC")
|
||||
S3_READ_TIMEOUT_SEC = env.float("S3_READ_TIMEOUT_SEC")
|
||||
S3_MAX_ATTEMPTS = env.int("S3_MAX_ATTEMPTS")
|
||||
S3_STARTUP_TIMEOUT_SEC = env.float("S3_STARTUP_TIMEOUT_SEC")
|
||||
|
||||
MAX_UPLOAD_BYTES = env.int("MAX_UPLOAD_BYTES")
|
||||
MAX_IMAGE_PIXELS = env.int("MAX_IMAGE_PIXELS")
|
||||
# Сколько байт загрузки держится в RAM, остальное — во временном файле запроса
|
||||
UPLOAD_SPOOL_MEMORY_BYTES = env.int("UPLOAD_SPOOL_MEMORY_BYTES")
|
||||
IMAGE_INSPECT_WORKERS = env.int("IMAGE_INSPECT_WORKERS")
|
||||
# Жёсткий срок PUT в S3 + подтверждения в БД; много меньше grace period janitor
|
||||
ATTACHMENT_STORE_TIMEOUT_SEC = env.float("ATTACHMENT_STORE_TIMEOUT_SEC")
|
||||
ATTACHMENT_PENDING_GRACE_SEC = env.int("ATTACHMENT_PENDING_GRACE_SEC")
|
||||
JANITOR_INTERVAL_SEC = env.float("JANITOR_INTERVAL_SEC")
|
||||
|
||||
INSTANCE_ID = secrets.token_hex(6)
|
||||
INSTANCE_ID_FILE = Path(tempfile.gettempdir()) / "instance-id"
|
||||
# Время последней успешной сверки janitor — для healthcheck его контейнера
|
||||
JANITOR_HEARTBEAT_FILE = Path(tempfile.gettempdir()) / "janitor-heartbeat"
|
||||
|
||||
BROKER_URL_FILE = Path(env.str("BROKER_URL_FILE"))
|
||||
WORKER_CONCURRENCY = env.int("WORKER_CONCURRENCY")
|
||||
WORKER_PREFETCH = env.int("WORKER_PREFETCH")
|
||||
JOB_MAX_ATTEMPTS = env.int("JOB_MAX_ATTEMPTS")
|
||||
JOB_TIMEOUT_SECONDS = env.int("JOB_TIMEOUT_SECONDS")
|
||||
JOB_LEASE_SECONDS = env.int("JOB_LEASE_SECONDS")
|
||||
JOB_RETRY_BASE_SECONDS = env.float("JOB_RETRY_BASE_SECONDS")
|
||||
JOB_RETRY_MAX_SECONDS = env.float("JOB_RETRY_MAX_SECONDS")
|
||||
IMAGE_PROCESS_MEMORY_MB = env.int("IMAGE_PROCESS_MEMORY_MB")
|
||||
STAGING_RETENTION_SECONDS = env.int("STAGING_RETENTION_SECONDS")
|
||||
OUTBOX_POLL_SECONDS = env.float("OUTBOX_POLL_SECONDS")
|
||||
|
||||
OTEL_SERVICE_NAME = env.str("OTEL_SERVICE_NAME")
|
||||
@@ -0,0 +1,70 @@
|
||||
import asyncio
|
||||
import random
|
||||
import signal
|
||||
from pathlib import Path
|
||||
|
||||
from config import DB_STARTUP_TIMEOUT_SEC, LOGGER
|
||||
from config.settings import BROKER_URL_FILE, OUTBOX_POLL_SECONDS, WORKER_PREFETCH
|
||||
from domain.errors import UnavailableError
|
||||
from infrastructure import database_pool_manager
|
||||
from infrastructure.broker import RabbitBrokerManager
|
||||
from infrastructure.dependencies.jobs import create_job_repository
|
||||
from infrastructure.observability import telemetry
|
||||
from infrastructure.repositories.observability import PostgresObservabilityRepository
|
||||
from infrastructure.utils.faults import crash_once
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
stop = asyncio.Event()
|
||||
loop = asyncio.get_running_loop()
|
||||
for signum in (signal.SIGTERM, signal.SIGINT):
|
||||
loop.add_signal_handler(signum, stop.set)
|
||||
telemetry.start(database_pool_manager)
|
||||
await database_pool_manager.create_pool(DB_STARTUP_TIMEOUT_SEC)
|
||||
await database_pool_manager.check_migrations()
|
||||
jobs = create_job_repository()
|
||||
broker = RabbitBrokerManager(BROKER_URL_FILE, WORKER_PREFETCH)
|
||||
failures = 0
|
||||
observability = PostgresObservabilityRepository(database_pool_manager)
|
||||
|
||||
async def sample_backlog():
|
||||
while not stop.is_set():
|
||||
try:
|
||||
telemetry.queue_stats = await observability.queue_snapshot()
|
||||
except UnavailableError:
|
||||
telemetry.queue_stats = None
|
||||
await asyncio.sleep(2)
|
||||
|
||||
sampler = asyncio.create_task(sample_backlog())
|
||||
try:
|
||||
while not stop.is_set():
|
||||
delay = OUTBOX_POLL_SECONDS
|
||||
try:
|
||||
await jobs.recover_expired()
|
||||
event = await jobs.claim_event()
|
||||
if event is not None:
|
||||
await broker.publish(event.message)
|
||||
crash_once("after-publish")
|
||||
await jobs.mark_published(event)
|
||||
delay = 0
|
||||
failures = 0
|
||||
Path("/tmp/dispatcher-heartbeat").touch()
|
||||
except Exception as error:
|
||||
failures += 1
|
||||
LOGGER.warning(f"Outbox: {type(error).__name__}, намерение сохранено")
|
||||
delay = random.uniform(0.5, min(10, 2 ** min(failures, 4)))
|
||||
if delay:
|
||||
try:
|
||||
await asyncio.wait_for(stop.wait(), delay)
|
||||
except TimeoutError:
|
||||
pass
|
||||
finally:
|
||||
sampler.cancel()
|
||||
await asyncio.gather(sampler, return_exceptions=True)
|
||||
await broker.close()
|
||||
await database_pool_manager.close()
|
||||
await asyncio.to_thread(telemetry.shutdown)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,60 @@
|
||||
from .attachments import (
|
||||
ORIGINAL_OBJECT_PREFIX,
|
||||
AttachmentContentType,
|
||||
AttachmentFilename,
|
||||
AttachmentSchema,
|
||||
AttachmentStatus,
|
||||
AttachmentVariant,
|
||||
InspectedImageSchema,
|
||||
NewAttachmentSchema,
|
||||
ReconcileReportSchema,
|
||||
StoredAttachmentSchema,
|
||||
UnfinishedUploadSchema,
|
||||
)
|
||||
from .auth import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema
|
||||
from .chats import ChatPageSchema, ChatSchema, CreateChatSchema
|
||||
from .health import HealthSchema
|
||||
from .messages import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
MESSAGE_ATTACHMENTS_MAX,
|
||||
CreateMessageSchema,
|
||||
IdempotencyKeySchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
)
|
||||
from .pagination import PageCursorSchema, PageKey
|
||||
from .users import CreateUserSchema, UserCredentialsSchema, UserSchema
|
||||
|
||||
__all__ = [
|
||||
"CreateUserSchema",
|
||||
"UserSchema",
|
||||
"UserCredentialsSchema",
|
||||
"BasicCredentialsSchema",
|
||||
"SessionSchema",
|
||||
"ActiveSessionSchema",
|
||||
"CreateChatSchema",
|
||||
"ChatSchema",
|
||||
"ChatPageSchema",
|
||||
"CreateMessageSchema",
|
||||
"IdempotencyKeySchema",
|
||||
"IDEMPOTENCY_KEY_PATTERN",
|
||||
"IDEMPOTENCY_KEY_MAX_LENGTH",
|
||||
"MESSAGE_ATTACHMENTS_MAX",
|
||||
"MessageSchema",
|
||||
"MessagePageSchema",
|
||||
"HealthSchema",
|
||||
"PageKey",
|
||||
"PageCursorSchema",
|
||||
"ORIGINAL_OBJECT_PREFIX",
|
||||
"AttachmentContentType",
|
||||
"AttachmentFilename",
|
||||
"AttachmentStatus",
|
||||
"AttachmentVariant",
|
||||
"AttachmentSchema",
|
||||
"NewAttachmentSchema",
|
||||
"StoredAttachmentSchema",
|
||||
"UnfinishedUploadSchema",
|
||||
"InspectedImageSchema",
|
||||
"ReconcileReportSchema",
|
||||
]
|
||||
@@ -0,0 +1,33 @@
|
||||
from .schemas import (
|
||||
FILENAME_MAX_LENGTH,
|
||||
ORIGINAL_OBJECT_PREFIX,
|
||||
AttachmentContentType,
|
||||
AttachmentFilename,
|
||||
AttachmentSchema,
|
||||
AttachmentStatus,
|
||||
AttachmentVariant,
|
||||
AttachmentVariantSchema,
|
||||
InspectedImageSchema,
|
||||
NewAttachmentSchema,
|
||||
ReconcileReportSchema,
|
||||
StoredAttachmentSchema,
|
||||
UnfinishedUploadSchema,
|
||||
ensure_safe_filename,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"FILENAME_MAX_LENGTH",
|
||||
"ORIGINAL_OBJECT_PREFIX",
|
||||
"AttachmentContentType",
|
||||
"AttachmentFilename",
|
||||
"AttachmentStatus",
|
||||
"AttachmentVariant",
|
||||
"AttachmentVariantSchema",
|
||||
"AttachmentSchema",
|
||||
"NewAttachmentSchema",
|
||||
"StoredAttachmentSchema",
|
||||
"UnfinishedUploadSchema",
|
||||
"InspectedImageSchema",
|
||||
"ReconcileReportSchema",
|
||||
"ensure_safe_filename",
|
||||
]
|
||||
@@ -0,0 +1,164 @@
|
||||
import unicodedata
|
||||
from enum import StrEnum
|
||||
from typing import Annotated, Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import AfterValidator, BaseModel, ConfigDict, Field
|
||||
|
||||
from domain.utils import NotBlankStr, UtcDateTime
|
||||
|
||||
FILENAME_MAX_LENGTH = 255
|
||||
|
||||
# Объекты оригиналов в bucket: ключ назначает сервер, filename в него не входит
|
||||
ORIGINAL_OBJECT_PREFIX = "originals/"
|
||||
|
||||
# Невидимое управление направлением текста: «photognp.exe» отображается
|
||||
# как «photoexe.png»
|
||||
BIDI_CONTROL_CHARACTERS = frozenset("")
|
||||
|
||||
|
||||
def ensure_safe_filename(value: str) -> str:
|
||||
"""
|
||||
Отображаемое имя файла — не путь и не ключ S3. Запрещены разделители путей,
|
||||
управляющие символы (включая NUL), bidi-управление и имена из одних точек.
|
||||
"""
|
||||
if "/" in value or "\\" in value:
|
||||
raise ValueError("Имя файла не может содержать / или \\")
|
||||
if any(
|
||||
unicodedata.category(char) == "Cc" or char in BIDI_CONTROL_CHARACTERS
|
||||
for char in value
|
||||
):
|
||||
raise ValueError("Имя файла не может содержать управляющие символы")
|
||||
if not value.strip("."):
|
||||
raise ValueError("Имя файла не может состоять из точек")
|
||||
return value
|
||||
|
||||
|
||||
AttachmentFilename = Annotated[
|
||||
NotBlankStr,
|
||||
Field(min_length=1, max_length=FILENAME_MAX_LENGTH),
|
||||
AfterValidator(ensure_safe_filename),
|
||||
]
|
||||
|
||||
|
||||
class AttachmentContentType(StrEnum):
|
||||
"""Допустимые форматы оригинала; определяются по содержимому"""
|
||||
|
||||
PNG = "image/png"
|
||||
JPEG = "image/jpeg"
|
||||
|
||||
|
||||
class AttachmentStatus(StrEnum):
|
||||
"""Публичные состояния pipeline и внутренние состояния приёма/очистки"""
|
||||
|
||||
PENDING = "pending"
|
||||
QUEUED = "queued"
|
||||
PROCESSING = "processing"
|
||||
READY = "ready"
|
||||
FAILED = "failed"
|
||||
DELETING = "deleting"
|
||||
|
||||
|
||||
class AttachmentVariant(StrEnum):
|
||||
"""Имена вариантов из контракта; original и производные pipeline v1"""
|
||||
|
||||
ORIGINAL = "original"
|
||||
THUMBNAIL = "thumbnail"
|
||||
CROP = "crop"
|
||||
OPTIMIZED = "optimized"
|
||||
|
||||
|
||||
class AttachmentVariantSchema(BaseModel):
|
||||
"""Производный вариант изображения; появляется с lab5"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
name: AttachmentVariant
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
width: int
|
||||
height: int
|
||||
sha256: str
|
||||
|
||||
|
||||
class AttachmentSchema(BaseModel):
|
||||
"""Метаданные вложения; size_bytes, sha256 и content_type описывают оригинал"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
owner_id: UUID
|
||||
filename: str
|
||||
content_type: AttachmentContentType
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: Literal["queued", "processing", "ready", "failed"] = "ready"
|
||||
variants: list[AttachmentVariantSchema] = Field(default_factory=list)
|
||||
error_code: str | None = None
|
||||
created_at: UtcDateTime
|
||||
updated_at: UtcDateTime
|
||||
|
||||
|
||||
class NewAttachmentSchema(BaseModel):
|
||||
"""Проверенная загрузка: метаданные и назначенный сервером ключ объекта S3"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
chat_id: UUID
|
||||
owner_id: UUID
|
||||
filename: str
|
||||
content_type: AttachmentContentType
|
||||
size_bytes: int
|
||||
sha256: bytes
|
||||
object_key: str
|
||||
|
||||
|
||||
class StoredAttachmentSchema(BaseModel):
|
||||
"""Вложение вместе с ключом объекта — только внутри приложения"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
attachment: AttachmentSchema
|
||||
object_key: str
|
||||
variant_keys: dict[str, str] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class UnfinishedUploadSchema(BaseModel):
|
||||
"""Строка pending/deleting, забранная на очистку, и её объект S3"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
object_key: str
|
||||
|
||||
|
||||
class InspectedImageSchema(BaseModel):
|
||||
"""Результат проверки содержимого: истинный формат и размеры"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
content_type: AttachmentContentType
|
||||
width: int
|
||||
height: int
|
||||
|
||||
|
||||
class ReconcileReportSchema(BaseModel):
|
||||
"""Итог одного прохода сверки S3 и БД"""
|
||||
|
||||
dry_run: bool
|
||||
unfinished_claimed: int = 0
|
||||
unfinished_objects_deleted: int = 0
|
||||
unfinished_rows_deleted: int = 0
|
||||
objects_scanned: int = 0
|
||||
orphan_objects_found: int = 0
|
||||
orphan_objects_deleted: int = 0
|
||||
|
||||
|
||||
class AttachmentDownloadSchema(BaseModel):
|
||||
"""Метаданные HTTP-выдачи original либо производного варианта"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
filename: str
|
||||
content_type: str
|
||||
@@ -0,0 +1,7 @@
|
||||
from .schemas import ActiveSessionSchema, BasicCredentialsSchema, SessionSchema
|
||||
|
||||
__all__ = [
|
||||
"BasicCredentialsSchema",
|
||||
"SessionSchema",
|
||||
"ActiveSessionSchema",
|
||||
]
|
||||
@@ -0,0 +1,35 @@
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from domain.users import UserSchema
|
||||
from domain.utils import UtcDateTime
|
||||
|
||||
|
||||
class BasicCredentialsSchema(BaseModel):
|
||||
"""Разобранный заголовок Authorization: Basic"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
username: str
|
||||
password: str
|
||||
|
||||
|
||||
class SessionSchema(BaseModel):
|
||||
"""Ответ входа: непрозрачный токен показывается клиенту один раз"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
token: str
|
||||
token_type: Literal["Bearer"] = "Bearer"
|
||||
expires_at: UtcDateTime
|
||||
user: UserSchema
|
||||
|
||||
|
||||
class ActiveSessionSchema(BaseModel):
|
||||
"""Действующая серверная сессия: владелец и срок; так же лежит в кэше"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
user: UserSchema
|
||||
expires_at: UtcDateTime
|
||||
@@ -0,0 +1,7 @@
|
||||
from .schemas import ChatPageSchema, ChatSchema, CreateChatSchema
|
||||
|
||||
__all__ = [
|
||||
"CreateChatSchema",
|
||||
"ChatSchema",
|
||||
"ChatPageSchema",
|
||||
]
|
||||
@@ -0,0 +1,42 @@
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from domain.utils import NotBlankStr, UtcDateTime
|
||||
|
||||
ChatTitle = Annotated[NotBlankStr, Field(min_length=1, max_length=100)]
|
||||
|
||||
|
||||
class CreateChatSchema(BaseModel):
|
||||
"""Запрос создания чата: участники задаются явно, включая вызывающего"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
title: ChatTitle
|
||||
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def validate_unique_members(cls, value: list[UUID]) -> list[UUID]:
|
||||
if len(set(value)) != len(value):
|
||||
raise ValueError("member_ids не должен содержать дубликаты")
|
||||
return value
|
||||
|
||||
|
||||
class ChatSchema(BaseModel):
|
||||
"""Чат"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: list[UUID]
|
||||
created_at: UtcDateTime
|
||||
|
||||
|
||||
class ChatPageSchema(BaseModel):
|
||||
"""Страница чатов текущего пользователя"""
|
||||
|
||||
items: list[ChatSchema]
|
||||
next_cursor: str | None
|
||||
@@ -0,0 +1,28 @@
|
||||
from .codes import ErrorCode
|
||||
from .exceptions import (
|
||||
AppError,
|
||||
ConflictError,
|
||||
InvalidCredentialsError,
|
||||
InvalidRequestError,
|
||||
NotFoundError,
|
||||
PayloadTooLargeError,
|
||||
UnauthorizedError,
|
||||
UnavailableError,
|
||||
UnsupportedMediaTypeError,
|
||||
)
|
||||
from .schemas import ErrorBodySchema, ErrorSchema
|
||||
|
||||
__all__ = [
|
||||
"ErrorCode",
|
||||
"AppError",
|
||||
"InvalidRequestError",
|
||||
"UnauthorizedError",
|
||||
"InvalidCredentialsError",
|
||||
"NotFoundError",
|
||||
"ConflictError",
|
||||
"PayloadTooLargeError",
|
||||
"UnsupportedMediaTypeError",
|
||||
"UnavailableError",
|
||||
"ErrorBodySchema",
|
||||
"ErrorSchema",
|
||||
]
|
||||
@@ -0,0 +1,15 @@
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class ErrorCode(StrEnum):
|
||||
"""коды ошибок"""
|
||||
|
||||
INVALID_REQUEST = "invalid_request"
|
||||
UNAUTHORIZED = "unauthorized"
|
||||
NOT_FOUND = "not_found"
|
||||
CONFLICT = "conflict"
|
||||
PAYLOAD_TOO_LARGE = "payload_too_large"
|
||||
UNSUPPORTED_MEDIA_TYPE = "unsupported_media_type"
|
||||
RATE_LIMITED = "rate_limited"
|
||||
UNAVAILABLE = "unavailable"
|
||||
INTERNAL_ERROR = "internal_error"
|
||||
@@ -0,0 +1,80 @@
|
||||
from http import HTTPStatus
|
||||
|
||||
from .codes import ErrorCode
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
"""Базовая ошибка приложения: статус, машинный код и безопасное сообщение"""
|
||||
|
||||
status_code: HTTPStatus = HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
code: ErrorCode = ErrorCode.INTERNAL_ERROR
|
||||
default_message: str = "Внутренняя ошибка сервера"
|
||||
headers: dict[str, str] = {}
|
||||
|
||||
def __init__(self, message: str | None = None) -> None:
|
||||
self.message = message or self.default_message
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class InvalidRequestError(AppError):
|
||||
"""Неверные поля, UUID, cursor или limit"""
|
||||
|
||||
status_code = HTTPStatus.BAD_REQUEST
|
||||
code = ErrorCode.INVALID_REQUEST
|
||||
default_message = "Некорректный запрос"
|
||||
|
||||
|
||||
class UnauthorizedError(AppError):
|
||||
"""Нет действительной Bearer-сессии: токена нет, он истёк, отозван или неизвестен"""
|
||||
|
||||
status_code = HTTPStatus.UNAUTHORIZED
|
||||
code = ErrorCode.UNAUTHORIZED
|
||||
default_message = "Требуется действительная сессия: Authorization: Bearer <token>"
|
||||
headers = {"WWW-Authenticate": 'Bearer realm="messenger"'}
|
||||
|
||||
|
||||
class InvalidCredentialsError(UnauthorizedError):
|
||||
"""Неверные Basic-credentials: одинаковый ответ для неизвестного логина и пароля"""
|
||||
|
||||
default_message = "Неверное имя пользователя или пароль"
|
||||
headers = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
|
||||
|
||||
|
||||
class NotFoundError(AppError):
|
||||
"""Объекта нет или он чужой: существование чужого объекта не раскрывается"""
|
||||
|
||||
status_code = HTTPStatus.NOT_FOUND
|
||||
code = ErrorCode.NOT_FOUND
|
||||
default_message = "Объект не найден"
|
||||
|
||||
|
||||
class ConflictError(AppError):
|
||||
"""Конфликт состояния, например занятый username"""
|
||||
|
||||
status_code = HTTPStatus.CONFLICT
|
||||
code = ErrorCode.CONFLICT
|
||||
default_message = "Конфликт состояния"
|
||||
|
||||
|
||||
class PayloadTooLargeError(AppError):
|
||||
"""Тело больше лимита байт или изображение больше лимита пикселей"""
|
||||
|
||||
status_code = HTTPStatus.REQUEST_ENTITY_TOO_LARGE
|
||||
code = ErrorCode.PAYLOAD_TOO_LARGE
|
||||
default_message = "Слишком большой файл"
|
||||
|
||||
|
||||
class UnsupportedMediaTypeError(AppError):
|
||||
"""Содержимое не PNG/JPEG, повреждено или не совпадает с Content-Type"""
|
||||
|
||||
status_code = HTTPStatus.UNSUPPORTED_MEDIA_TYPE
|
||||
code = ErrorCode.UNSUPPORTED_MEDIA_TYPE
|
||||
default_message = "Поддерживаются только изображения PNG и JPEG"
|
||||
|
||||
|
||||
class UnavailableError(AppError):
|
||||
"""Приложение не готово обслуживать запросы"""
|
||||
|
||||
status_code = HTTPStatus.SERVICE_UNAVAILABLE
|
||||
code = ErrorCode.UNAVAILABLE
|
||||
default_message = "Сервис временно недоступен"
|
||||
@@ -0,0 +1,21 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from .codes import ErrorCode
|
||||
|
||||
|
||||
class ErrorBodySchema(BaseModel):
|
||||
"""Тело ошибки"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
code: ErrorCode
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorSchema(BaseModel):
|
||||
"""Единый формат ошибки: {"error": {...}}"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
error: ErrorBodySchema
|
||||
@@ -0,0 +1,3 @@
|
||||
from .schemas import HealthSchema
|
||||
|
||||
__all__ = ["HealthSchema"]
|
||||
@@ -0,0 +1,9 @@
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class HealthSchema(BaseModel):
|
||||
"""Ответ health-проверок"""
|
||||
|
||||
status: Literal["ok"] = "ok"
|
||||
@@ -0,0 +1,15 @@
|
||||
from .schemas import (
|
||||
ClaimedJobSchema,
|
||||
JobMessageSchema,
|
||||
OutboxEventSchema,
|
||||
ProcessedVariantSchema,
|
||||
ProcessingError,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ClaimedJobSchema",
|
||||
"JobMessageSchema",
|
||||
"OutboxEventSchema",
|
||||
"ProcessedVariantSchema",
|
||||
"ProcessingError",
|
||||
]
|
||||
@@ -0,0 +1,59 @@
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from domain.attachments import AttachmentVariantSchema
|
||||
|
||||
|
||||
class JobMessageSchema(BaseModel):
|
||||
"""Сообщение соответствует contracts/jobs.schema.json; байтов и адресов нет"""
|
||||
|
||||
model_config = ConfigDict(frozen=True, extra="forbid")
|
||||
|
||||
schema_version: Literal[1] = 1
|
||||
job_id: UUID
|
||||
type: Literal["attachment.process.v1"] = "attachment.process.v1"
|
||||
attachment_id: UUID
|
||||
pipeline_version: Literal[1] = 1
|
||||
attempt: int = Field(ge=1)
|
||||
created_at: datetime
|
||||
traceparent: str | None = Field(
|
||||
default=None, pattern=r"^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$"
|
||||
)
|
||||
tracestate: str | None = Field(default=None, max_length=512)
|
||||
|
||||
|
||||
class ClaimedJobSchema(BaseModel):
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
message: JobMessageSchema
|
||||
lease_token: UUID
|
||||
object_key: str
|
||||
staging_key: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
content_type: str
|
||||
|
||||
|
||||
class OutboxEventSchema(BaseModel):
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: int
|
||||
claim_token: UUID
|
||||
message: JobMessageSchema
|
||||
|
||||
|
||||
class ProcessedVariantSchema(AttachmentVariantSchema):
|
||||
"""Метаданные эффекта; object_key не попадает в публичный ответ"""
|
||||
|
||||
object_key: str
|
||||
|
||||
|
||||
class ProcessingError(Exception):
|
||||
"""Безопасная постоянная ошибка: повтор с теми же данными не поможет"""
|
||||
|
||||
def __init__(self, code: str) -> None:
|
||||
self.code = code
|
||||
super().__init__(code)
|
||||
@@ -0,0 +1,19 @@
|
||||
from .schemas import (
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH,
|
||||
IDEMPOTENCY_KEY_PATTERN,
|
||||
MESSAGE_ATTACHMENTS_MAX,
|
||||
CreateMessageSchema,
|
||||
IdempotencyKeySchema,
|
||||
MessagePageSchema,
|
||||
MessageSchema,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"IDEMPOTENCY_KEY_PATTERN",
|
||||
"IDEMPOTENCY_KEY_MAX_LENGTH",
|
||||
"MESSAGE_ATTACHMENTS_MAX",
|
||||
"CreateMessageSchema",
|
||||
"IdempotencyKeySchema",
|
||||
"MessageSchema",
|
||||
"MessagePageSchema",
|
||||
]
|
||||
@@ -0,0 +1,85 @@
|
||||
from typing import Annotated, Self
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import (
|
||||
AfterValidator,
|
||||
BaseModel,
|
||||
ConfigDict,
|
||||
Field,
|
||||
StrictStr,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
|
||||
from domain.utils import UtcDateTime, ensure_not_blank, ensure_storable
|
||||
|
||||
MessageText = Annotated[
|
||||
StrictStr, Field(max_length=4000), AfterValidator(ensure_storable)
|
||||
]
|
||||
|
||||
MESSAGE_ATTACHMENTS_MAX = 10
|
||||
|
||||
IDEMPOTENCY_KEY_PATTERN = r"^[A-Za-z0-9._:-]{1,128}$"
|
||||
IDEMPOTENCY_KEY_MAX_LENGTH = 128
|
||||
|
||||
|
||||
class CreateMessageSchema(BaseModel):
|
||||
"""
|
||||
Запрос отправки сообщения: отправитель берётся из текущей сессии.
|
||||
Пустой или пробельный текст допустим только вместе с вложениями.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
text: MessageText
|
||||
attachment_ids: list[UUID] = Field(
|
||||
default_factory=list, max_length=MESSAGE_ATTACHMENTS_MAX
|
||||
)
|
||||
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def validate_unique_attachments(cls, value: list[UUID]) -> list[UUID]:
|
||||
if len(set(value)) != len(value):
|
||||
raise ValueError("attachment_ids не должен содержать дубликаты")
|
||||
return value
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_not_empty(self) -> Self:
|
||||
if not self.attachment_ids:
|
||||
ensure_not_blank(self.text)
|
||||
return self
|
||||
|
||||
|
||||
class MessageSchema(BaseModel):
|
||||
"""Сообщение"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: UtcDateTime
|
||||
attachment_ids: list[UUID]
|
||||
|
||||
|
||||
class MessagePageSchema(BaseModel):
|
||||
"""Страница истории чата"""
|
||||
|
||||
items: list[MessageSchema]
|
||||
next_cursor: str | None
|
||||
|
||||
|
||||
class IdempotencyKeySchema(BaseModel):
|
||||
"""
|
||||
Ключ повторной отправки сообщения.
|
||||
|
||||
request_hash — SHA-256 канонического тела: повтор с тем же ключом и тем же
|
||||
телом возвращает исходное сообщение, с другим телом — конфликт.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(frozen=True)
|
||||
|
||||
key: str
|
||||
request_hash: bytes
|
||||
ttl_seconds: int
|
||||
@@ -0,0 +1,6 @@
|
||||
from .schemas import PageCursorSchema, PageKey
|
||||
|
||||
__all__ = [
|
||||
"PageKey",
|
||||
"PageCursorSchema",
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
from datetime import datetime
|
||||
from typing import NamedTuple
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class PageKey(NamedTuple):
|
||||
"""Ключ keyset-пагинации: порядок (created_at, id) по возрастанию"""
|
||||
|
||||
created_at: datetime
|
||||
id: UUID
|
||||
|
||||
|
||||
class PageCursorSchema(BaseModel):
|
||||
"""Содержимое непрозрачного cursor: ключ последнего элемента и его ресурс"""
|
||||
|
||||
model_config = ConfigDict(extra="forbid", frozen=True)
|
||||
|
||||
scope: str
|
||||
created_at: datetime
|
||||
id: UUID
|
||||
|
||||
@property
|
||||
def key(self) -> PageKey:
|
||||
return PageKey(self.created_at, self.id)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user