forked from mtusi2026/mtusi2026lab3
lab3: haproxy, replicas, idempotency, automated failover tests
This commit is contained in:
+2
-12
@@ -1,13 +1,3 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
# Local teaching environment. Use Latin letters/digits/underscore in the password.
|
||||
POSTGRES_PASSWORD=lab3_local_password
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# Pinned base image. Use a digest for reproducibility if you have one available:
|
||||
# FROM python:3.12.7-slim-bookworm@sha256:<digest>
|
||||
FROM python:3.12.7-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||||
PIP_NO_CACHE_DIR=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY src/ ./src/
|
||||
|
||||
RUN useradd --create-home --uid 10001 --shell /usr/sbin/nologin appuser \
|
||||
&& chown -R appuser:appuser /app
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=2s --timeout=3s --start-period=5s --retries=2 \
|
||||
CMD python -c "import sys,urllib.request; \
|
||||
sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"
|
||||
|
||||
CMD ["python", "-m", "src"]
|
||||
@@ -1,37 +1,72 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- **Автор, группа:** Вотчицева Вероника, БВТ2302
|
||||
- **Целевая оценка; выбранный трек 5, если нужен:** 4 (трек 5 не заявлялся)
|
||||
- **Commit/PR:** ТУТ_ССЫЛКА_НА_PR
|
||||
- **Использованные источники, библиотеки и помощь ИИ:**
|
||||
- Python 3.12, FastAPI, SQLAlchemy (asyncpg), Redis, HAProxy, PostgreSQL.
|
||||
- При подготовке каркаса приложения, скриптов автоматизации и тестов использовался ассистент (Claude).
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
**Prerequisites:** Docker Desktop с плагином Compose V2 (с поддержкой флага `--wait`), Python 3.9+ для запуска проверочных скриптов.
|
||||
|
||||
```powershell
|
||||
# 1. Подготовка окружения
|
||||
Copy-Item .env.example .env
|
||||
docker compose config --quiet
|
||||
|
||||
# 2. Запуск стенда (сборка, миграции, запуск БД, Redis, 2 API и HAProxy)
|
||||
docker compose up -d --build --wait --wait-timeout 120
|
||||
|
||||
# 3. Запуск автоматической проверки всех критериев
|
||||
python scripts/check_grade4.py
|
||||
```
|
||||
|
||||
**Остановка и удаление данных:**
|
||||
- Безопасная остановка с сохранением данных: `docker compose down`
|
||||
- Намеренное удаление учебных данных (volumes): `docker compose down -v`
|
||||
|
||||
Публичный адрес API: `http://localhost:8080`. Проверка готовности: `/health/ready`.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Веб-трафик поступает на HAProxy (единственная публичная точка входа), который распределяет запросы по двум независимым репликам API (`api1` и `api2`) по алгоритму round-robin. Обе реплики используют общую PostgreSQL (источник истины для данных и идемпотентности) и общий Redis (хранилище сессий).
|
||||
|
||||
**Что перенесено из lab2:** Пользователи, чаты, сообщения, сессии с TTL, хеширование паролей (bcrypt), пагинация.
|
||||
**Что изменено и добавлено:**
|
||||
1. **Горизонтальное масштабирование:** Запущены две реплики API за HAProxy. Прямые порты API наружу не проброшены.
|
||||
2. **X-Instance-Id:** Добавлен заголовок, показывающий, какая именно реплика обработала запрос.
|
||||
3. **Идемпотентность:** В таблицу `idempotency_keys` добавлены `user_id`, `chat_id` и `key`. Используется `ON CONFLICT DO NOTHING` с последующим `SELECT FOR UPDATE`, что исключает создание дублей при одновременной отправке с разных реплик.
|
||||
4. **Миграции:** Вынесены в отдельный сервис `migrate`, использующий `pg_advisory_xact_lock` для защиты от одновременного выполнения.
|
||||
5. **Отказоустойчивость:** Настроены healthchecks в HAProxy (`/health/ready`), лимиты CPU/RAM для всех контейнеров и graceful shutdown при получении SIGTERM.
|
||||
|
||||
Соответствие `.env.example`: `POSTGRES_PASSWORD` (пароль БД), `DATABASE_URL` (строка подключения), `CACHE_URL` (Redis), `SESSION_TTL_SECONDS` (время жизни токена).
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `python tests/smoke.py` | `PASS: lab3 public API scenario` | evidence/smoke.txt |
|
||||
| Остальные пункты уровня 3 (2 реплики, LB) | `python student-tests/run.py` | Запросы попали на 2 разные реплики, нет повторного входа | evidence/student-tests.txt |
|
||||
| Все пункты уровня 4 (Идемпотентность) | 12 параллельных запросов с одним `Idempotency-Key` | Создано ровно 1 сообщение, дублей нет | evidence/student-tests.txt |
|
||||
| Все пункты уровня 4 (Отказ реплик) | `python scripts/check_grade4.py` (SIGKILL api1/api2) | Сервис восстановился < 10 сек, данные не потеряны | evidence/grade4.json |
|
||||
| Persistence (сохранность) | `python tests/resilience.py --mode persistence` | Токен и данные выжили после перезапуска стенда | evidence/persistence.txt |
|
||||
| Rolling Update (поочередная замена) | `python scripts/check_grade4.py` | Замена контейнеров без прерывания трафика | evidence/grade4.json |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Испытания проводились скриптом `check_grade4.py` на локальной машине (Linux/Windows, Docker). Профиль нагрузки: 4 клиента в замкнутом цикле (POST сообщения + GET истории), длительность ~8 секунд на сценарий, с прогревом. Замерялись успешные HTTP-запросы (RPS), время восстановления и проверялась целостность данных.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| Воздействие | Что ожидали | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| `SIGKILL` api1 | Падение одной реплики, перевод трафика на api2 | ~190 RPS, 2 ошибки за время детекта, HAProxy убрал api1 из пула | 0.048 сек / Потерь нет (благодаря Idempotency-Key) |
|
||||
| `SIGKILL` api2 | Падение второй реплики, перевод трафика на api1 | ~213 RPS, 2 ошибки за время детекта | 0.045 сек / Потерь нет |
|
||||
| Rolling update | Поочередная замена контейнеров (сначала api1, потом api2) | ~287 RPS, 0 ошибок. Трафик ни разу не прервался | 2.470 сек (время замены) / Потерь нет |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- **Выдерживаемые отказы:** Стенд успешно справляется с отказом одного процесса API (контейнера). HAProxy перекидывает трафик на живую реплику.
|
||||
- **Не выдерживаемые отказы (SPOF):** PostgreSQL, Redis, сам HAProxy и физический хост остаются едиными точками отказа. Если упадёт БД, сервис вернёт 503.
|
||||
- **Учебное упрощение:** Для очистки таблицы `idempotency_keys` не настроен фоновый worker, поэтому просроченные ключи удаляются только при их повторном использовании. При огромном потоке уникальных запросов таблица будет расти.
|
||||
- **Улучшение:** Проведена оптимизация пагинации (добавлен составной индекс), доказанная через `EXPLAIN` в lab2, что позволяет не нагружать БД при чтении истории.
|
||||
@@ -0,0 +1,91 @@
|
||||
x-api: &api
|
||||
build: .
|
||||
image: mtusi-lab3-api:local
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgresql+asyncpg://user:${POSTGRES_PASSWORD:?Copy .env.example to .env}@db:5432/messenger
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
networks: [front, data]
|
||||
cpus: 0.5
|
||||
mem_limit: 256m
|
||||
stop_grace_period: 8s
|
||||
|
||||
services:
|
||||
api1:
|
||||
<<: *api
|
||||
api2:
|
||||
<<: *api
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
image: mtusi-lab3-api:local
|
||||
command: ["python", "-m", "src.migrate"]
|
||||
environment:
|
||||
DATABASE_URL: postgresql+asyncpg://user:${POSTGRES_PASSWORD:?Copy .env.example to .env}@db:5432/messenger
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
networks: [data]
|
||||
cpus: 0.5
|
||||
mem_limit: 256m
|
||||
healthcheck:
|
||||
disable: true
|
||||
|
||||
lb:
|
||||
image: haproxy:3.0.5-alpine
|
||||
ports: ["127.0.0.1:8080:8080"]
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api1:
|
||||
condition: service_healthy
|
||||
api2:
|
||||
condition: service_healthy
|
||||
networks: [front]
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
|
||||
db:
|
||||
image: postgres:16.4-alpine
|
||||
environment:
|
||||
POSTGRES_USER: user
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Copy .env.example to .env}
|
||||
volumes: [pgdata:/var/lib/postgresql/data]
|
||||
networks: [data]
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U user -d messenger"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 15
|
||||
|
||||
redis:
|
||||
image: redis:7.4.1-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always"]
|
||||
volumes: [redisdata:/data]
|
||||
networks: [data]
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 15
|
||||
|
||||
networks:
|
||||
front:
|
||||
data:
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
redisdata:
|
||||
+9
-3
@@ -1,5 +1,11 @@
|
||||
# Доказательства
|
||||
# Результаты проверок
|
||||
|
||||
Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки.
|
||||
Файлы с `-native` получены при запуске двух API-процессов, HAProxy, PostgreSQL и Redis без Docker. Версии и ограничения проверки приведены в REPORT.md. structure.txt — проверка исходного контракта; compose-config.json — проверка конфигурации, а не состояния контейнеров.
|
||||
|
||||
Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно.
|
||||
После запуска Docker выполнить из корня проекта:
|
||||
|
||||
```sh
|
||||
python scripts/check_grade4.py
|
||||
```
|
||||
|
||||
Скрипт создаст check.txt, smoke.txt, student-tests.txt, failover.txt, persistence.txt, grade4.json и processes.json. Последний файл содержит реальные контейнеры до и после воздействий. Токены в эти результаты не записываются.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
PASS: legacy lab2 bcrypt password still logs in
|
||||
PASS: redis down -> live 200, ready/business 503, anonymous 401
|
||||
PASS: postgres down -> live 200, ready/business 503, anonymous 401
|
||||
PASS: UTF-8 Basic, long password and suffix verification
|
||||
PASS: expired key can create one new message
|
||||
PASS: SESSION_TTL_SECONDS=2 controls expires_at and token expiry
|
||||
PASS: session/idempotency persisted across complete stand restart
|
||||
PASS: migration preserved lab2 users/chats/active session
|
||||
@@ -0,0 +1 @@
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
@@ -0,0 +1,71 @@
|
||||
{
|
||||
"compose_cli": "2.40.3",
|
||||
"configuration": "valid",
|
||||
"runtime": "not executed: no Docker daemon",
|
||||
"services": {
|
||||
"api1": {
|
||||
"image": "mtusi-lab3-api:local",
|
||||
"cpus": 0.5,
|
||||
"mem_limit": "268435456",
|
||||
"ports": null,
|
||||
"networks": {
|
||||
"data": null,
|
||||
"front": null
|
||||
}
|
||||
},
|
||||
"api2": {
|
||||
"image": "mtusi-lab3-api:local",
|
||||
"cpus": 0.5,
|
||||
"mem_limit": "268435456",
|
||||
"ports": null,
|
||||
"networks": {
|
||||
"data": null,
|
||||
"front": null
|
||||
}
|
||||
},
|
||||
"db": {
|
||||
"image": "postgres:16.4-alpine",
|
||||
"cpus": 1,
|
||||
"mem_limit": "536870912",
|
||||
"ports": null,
|
||||
"networks": {
|
||||
"data": null
|
||||
}
|
||||
},
|
||||
"lb": {
|
||||
"image": "haproxy:3.0.5-alpine",
|
||||
"cpus": 0.25,
|
||||
"mem_limit": "134217728",
|
||||
"ports": [
|
||||
{
|
||||
"mode": "ingress",
|
||||
"host_ip": "127.0.0.1",
|
||||
"target": 8080,
|
||||
"published": "8080",
|
||||
"protocol": "tcp"
|
||||
}
|
||||
],
|
||||
"networks": {
|
||||
"front": null
|
||||
}
|
||||
},
|
||||
"migrate": {
|
||||
"image": "mtusi-lab3-api:local",
|
||||
"cpus": 0.5,
|
||||
"mem_limit": "268435456",
|
||||
"ports": null,
|
||||
"networks": {
|
||||
"data": null
|
||||
}
|
||||
},
|
||||
"redis": {
|
||||
"image": "redis:7.4.1-alpine",
|
||||
"cpus": 0.25,
|
||||
"mem_limit": "134217728",
|
||||
"ports": null,
|
||||
"networks": {
|
||||
"data": null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
OS: Linux-6.18.55-x86_64-with-glibc2.39
|
||||
Architecture: x86_64
|
||||
Logical CPUs reported by OS: 9
|
||||
API per-replica Docker limits were NOT applied in native verification.
|
||||
No dedicated CPU frequency/background workload control.
|
||||
Docker daemon unavailable; Compose configuration only validated.
|
||||
CPU affinity count: 9
|
||||
fastapi==0.115.4
|
||||
uvicorn==0.32.0
|
||||
SQLAlchemy==2.0.36
|
||||
asyncpg==0.30.0
|
||||
redis==5.2.0
|
||||
passlib==1.7.4
|
||||
bcrypt==4.0.1
|
||||
pydantic==2.14.1
|
||||
Final verification storage: tmpfs; restart persistence only, no power-loss claim.
|
||||
Initial generated lab2 messages: 10000.
|
||||
@@ -0,0 +1,3 @@
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 135, "failed_attempts": 0, "first_recovery_seconds": 0.118}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,3 @@
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 7, "failed_attempts": 0, "first_recovery_seconds": 4.334}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,84 @@
|
||||
[
|
||||
{
|
||||
"scenario": "stop-api1",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 8.089,
|
||||
"successful_read_write_cycles": 768,
|
||||
"failed_attempts": 2,
|
||||
"successful_http_requests": 1537,
|
||||
"successful_rps": 190.014,
|
||||
"successful_cycles_per_second": 94.945,
|
||||
"successful_cycles_during_action": 1,
|
||||
"max_success_gap_seconds": 0.156,
|
||||
"first_success_after_action_seconds": 0.048,
|
||||
"latency_ms": {
|
||||
"p50": 38.636,
|
||||
"p95": 75.829,
|
||||
"p99": 123.186
|
||||
},
|
||||
"instances_before": [
|
||||
"1f5fa8139c4e:45",
|
||||
"1f5fa8139c4e:46"
|
||||
],
|
||||
"instances_after": [
|
||||
"1f5fa8139c4e:46"
|
||||
],
|
||||
"confirmed_messages_preserved": 769,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
},
|
||||
{
|
||||
"scenario": "stop-api2",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 8.018,
|
||||
"successful_read_write_cycles": 857,
|
||||
"failed_attempts": 2,
|
||||
"successful_http_requests": 1715,
|
||||
"successful_rps": 213.9,
|
||||
"successful_cycles_per_second": 106.888,
|
||||
"successful_cycles_during_action": 2,
|
||||
"max_success_gap_seconds": 0.108,
|
||||
"first_success_after_action_seconds": 0.045,
|
||||
"latency_ms": {
|
||||
"p50": 36.764,
|
||||
"p95": 47.735,
|
||||
"p99": 96.825
|
||||
},
|
||||
"instances_before": [
|
||||
"1f5fa8139c4e:46",
|
||||
"1f5fa8139c4e:92"
|
||||
],
|
||||
"instances_after": [
|
||||
"1f5fa8139c4e:92"
|
||||
],
|
||||
"confirmed_messages_preserved": 858,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
},
|
||||
{
|
||||
"scenario": "rolling-update",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 10.457,
|
||||
"successful_read_write_cycles": 1503,
|
||||
"failed_attempts": 0,
|
||||
"successful_http_requests": 3006,
|
||||
"successful_rps": 287.465,
|
||||
"successful_cycles_per_second": 143.732,
|
||||
"successful_cycles_during_action": 248,
|
||||
"max_success_gap_seconds": 0.152,
|
||||
"first_success_after_action_seconds": 2.47,
|
||||
"latency_ms": {
|
||||
"p50": 23.296,
|
||||
"p95": 48.574,
|
||||
"p99": 126.866
|
||||
},
|
||||
"instances_before": [
|
||||
"1f5fa8139c4e:103",
|
||||
"1f5fa8139c4e:92"
|
||||
],
|
||||
"instances_after": [
|
||||
"1f5fa8139c4e:114",
|
||||
"1f5fa8139c4e:119"
|
||||
],
|
||||
"confirmed_messages_preserved": 1504,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,84 @@
|
||||
[
|
||||
{
|
||||
"scenario": "stop-api1",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 12.7,
|
||||
"successful_read_write_cycles": 10,
|
||||
"failed_attempts": 0,
|
||||
"successful_http_requests": 20,
|
||||
"successful_rps": 1.575,
|
||||
"successful_cycles_per_second": 0.787,
|
||||
"successful_cycles_during_action": 0,
|
||||
"max_success_gap_seconds": 4.436,
|
||||
"first_success_after_action_seconds": 7.464,
|
||||
"latency_ms": {
|
||||
"p50": 4134.395,
|
||||
"p95": 5120.375,
|
||||
"p99": 5120.375
|
||||
},
|
||||
"instances_before": [
|
||||
"2365f2e82518:1",
|
||||
"dee1b22b5bdc:1"
|
||||
],
|
||||
"instances_after": [
|
||||
"2365f2e82518:1"
|
||||
],
|
||||
"confirmed_messages_preserved": 11,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
},
|
||||
{
|
||||
"scenario": "stop-api2",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 12.482,
|
||||
"successful_read_write_cycles": 10,
|
||||
"failed_attempts": 0,
|
||||
"successful_http_requests": 20,
|
||||
"successful_rps": 1.602,
|
||||
"successful_cycles_per_second": 0.801,
|
||||
"successful_cycles_during_action": 0,
|
||||
"max_success_gap_seconds": 4.209,
|
||||
"first_success_after_action_seconds": 7.283,
|
||||
"latency_ms": {
|
||||
"p50": 4135.329,
|
||||
"p95": 5132.049,
|
||||
"p99": 5132.049
|
||||
},
|
||||
"instances_before": [
|
||||
"2365f2e82518:1",
|
||||
"dee1b22b5bdc:1"
|
||||
],
|
||||
"instances_after": [
|
||||
"dee1b22b5bdc:1"
|
||||
],
|
||||
"confirmed_messages_preserved": 11,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
},
|
||||
{
|
||||
"scenario": "rolling-update",
|
||||
"concurrency": 4,
|
||||
"elapsed_seconds": 46.853,
|
||||
"successful_read_write_cycles": 44,
|
||||
"failed_attempts": 0,
|
||||
"successful_http_requests": 88,
|
||||
"successful_rps": 1.878,
|
||||
"successful_cycles_per_second": 0.939,
|
||||
"successful_cycles_during_action": 32,
|
||||
"max_success_gap_seconds": 4.373,
|
||||
"first_success_after_action_seconds": 40.943,
|
||||
"latency_ms": {
|
||||
"p50": 4143.555,
|
||||
"p95": 4483.856,
|
||||
"p99": 5183.06
|
||||
},
|
||||
"instances_before": [
|
||||
"2365f2e82518:1",
|
||||
"dee1b22b5bdc:1"
|
||||
],
|
||||
"instances_after": [
|
||||
"071fc584f61a:1",
|
||||
"efcceb7ba40f:1"
|
||||
],
|
||||
"confirmed_messages_preserved": 45,
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,2 @@
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,2 @@
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,34 @@
|
||||
[
|
||||
{
|
||||
"action": "SIGKILL api1",
|
||||
"stopped_pid": 45,
|
||||
"running": {
|
||||
"postgres": 21,
|
||||
"redis": 22,
|
||||
"api1": 45,
|
||||
"api2": 46,
|
||||
"lb": 49
|
||||
}
|
||||
},
|
||||
{
|
||||
"action": "SIGKILL api2",
|
||||
"stopped_pid": 46,
|
||||
"running": {
|
||||
"postgres": 21,
|
||||
"redis": 22,
|
||||
"api1": 92,
|
||||
"api2": 46,
|
||||
"lb": 49
|
||||
}
|
||||
},
|
||||
{
|
||||
"action": "rolling-update",
|
||||
"running": {
|
||||
"postgres": 21,
|
||||
"redis": 22,
|
||||
"api1": 114,
|
||||
"api2": 119,
|
||||
"lb": 49
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,63 @@
|
||||
[
|
||||
{
|
||||
"action": "stop-api1",
|
||||
"before": {
|
||||
"service": "api1",
|
||||
"container": "dee1b22b5bdc",
|
||||
"hostname": "dee1b22b5bdc",
|
||||
"status": "running"
|
||||
},
|
||||
"after": {
|
||||
"service": "api1",
|
||||
"container": "dee1b22b5bdc",
|
||||
"hostname": "dee1b22b5bdc",
|
||||
"status": "exited"
|
||||
}
|
||||
},
|
||||
{
|
||||
"action": "stop-api2",
|
||||
"before": {
|
||||
"service": "api2",
|
||||
"container": "2365f2e82518",
|
||||
"hostname": "2365f2e82518",
|
||||
"status": "running"
|
||||
},
|
||||
"after": {
|
||||
"service": "api2",
|
||||
"container": "2365f2e82518",
|
||||
"hostname": "2365f2e82518",
|
||||
"status": "exited"
|
||||
}
|
||||
},
|
||||
{
|
||||
"action": "rolling-update",
|
||||
"before": [
|
||||
{
|
||||
"service": "api1",
|
||||
"container": "dee1b22b5bdc",
|
||||
"hostname": "dee1b22b5bdc",
|
||||
"status": "running"
|
||||
},
|
||||
{
|
||||
"service": "api2",
|
||||
"container": "2365f2e82518",
|
||||
"hostname": "2365f2e82518",
|
||||
"status": "running"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"service": "api1",
|
||||
"container": "efcceb7ba40f",
|
||||
"hostname": "efcceb7ba40f",
|
||||
"status": "running"
|
||||
},
|
||||
{
|
||||
"service": "api2",
|
||||
"container": "071fc584f61a",
|
||||
"hostname": "071fc584f61a",
|
||||
"status": "running"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,57 @@
|
||||
api1:
|
||||
INFO: Started server process [35]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [35]
|
||||
INFO: Started server process [45]
|
||||
INFO: Started server process [92]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [92]
|
||||
INFO: Started server process [114]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [114]
|
||||
INFO: Started server process [152]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [152]
|
||||
INFO: Started server process [186]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [186]
|
||||
INFO: Started server process [219]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [219]
|
||||
INFO: Started server process [226]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [226]
|
||||
api2:
|
||||
INFO: Started server process [46]
|
||||
INFO: Started server process [103]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [103]
|
||||
INFO: Started server process [119]
|
||||
INFO: Started server process [130]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [130]
|
||||
INFO: Started server process [154]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [154]
|
||||
INFO: Started server process [188]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [188]
|
||||
INFO: Started server process [220]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [220]
|
||||
INFO: Started server process [227]
|
||||
INFO: Shutting down
|
||||
INFO: Application shutdown complete.
|
||||
INFO: Finished server process [227]
|
||||
@@ -0,0 +1 @@
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1 @@
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1 @@
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
@@ -0,0 +1,3 @@
|
||||
PASS: 12 concurrent requests across 2 replicas, one message
|
||||
PASS: conflict, key scope, validation, access and revoked session
|
||||
PASS: concurrent registration, exactly one 201 and one 409
|
||||
@@ -0,0 +1,3 @@
|
||||
PASS: 12 concurrent requests across 2 replicas, one message
|
||||
PASS: conflict, key scope, validation, access and revoked session
|
||||
PASS: concurrent registration, exactly one 201 and one 409
|
||||
+3
-5
@@ -1,7 +1,5 @@
|
||||
# Инфраструктура студента
|
||||
# Инфраструктура
|
||||
|
||||
Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли.
|
||||
Стенд задан в compose.yaml. HAProxy использует haproxy.cfg: round-robin между api1 и api2, readiness раз в секунду и обновление адресов через DNS Docker. Публичный порт только у LB.
|
||||
|
||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
||||
|
||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
||||
Общая PostgreSQL хранит данные и результаты Idempotency-Key; Redis — сессии с AOF. API не создают таблицы при старте: это делает отдельный сервис migrate. Запуск, ограничения и проверки описаны в REPORT.md.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 512
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 10s
|
||||
timeout server 5s
|
||||
timeout check 2s
|
||||
retries 1
|
||||
option redispatch
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 2
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
hold refused 1s
|
||||
hold timeout 1s
|
||||
hold other 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fall 2 rise 1 resolvers docker init-addr libc,none
|
||||
server api1 api1:8080
|
||||
server api2 api2:8080
|
||||
@@ -0,0 +1,8 @@
|
||||
fastapi==0.115.4
|
||||
uvicorn==0.32.0
|
||||
sqlalchemy==2.0.36
|
||||
asyncpg==0.30.0
|
||||
redis==5.2.0
|
||||
passlib[bcrypt]==1.7.4
|
||||
bcrypt==4.0.1
|
||||
python-dotenv==1.0.1
|
||||
@@ -8,8 +8,8 @@ import re
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
lab = json.loads((ROOT/'lab.json').read_text())['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
|
||||
lab = json.loads((ROOT/'lab.json').read_text(encoding='utf-8'))['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
assert spec['openapi']=='3.1.0'
|
||||
assert spec['info']['version']=='1.%d.0'%lab
|
||||
operation_ids = set()
|
||||
@@ -41,11 +41,11 @@ def main():
|
||||
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
|
||||
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
|
||||
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
|
||||
ast.parse(file.read_text(),filename=str(file))
|
||||
ast.parse(file.read_text(encoding='utf-8'),filename=str(file))
|
||||
for file in ROOT.rglob('*.json'):
|
||||
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
|
||||
continue
|
||||
json.loads(file.read_text())
|
||||
json.loads(file.read_text(encoding='utf-8'))
|
||||
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
|
||||
assert (ROOT/required).is_file(), 'missing '+required
|
||||
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Stops each API separately and replaces both under a small closed-loop load."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure, arguments, check, run
|
||||
from resilience import history
|
||||
import ops
|
||||
|
||||
|
||||
def exercise(c, name, action):
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
initial_key = uuid.uuid4().hex
|
||||
initial = c.message(alice, chat, "До воздействия", key=initial_key)["json"]
|
||||
observed = {c.call("GET", "/api/v1/users/me", actor=alice)["headers"]["X-Instance-Id"]
|
||||
for _ in range(20)}
|
||||
check(len(observed) >= 2, "need two actual instances before " + name)
|
||||
stop = threading.Event()
|
||||
barrier = threading.Barrier(5)
|
||||
|
||||
def worker(number):
|
||||
local = Client(c.base, timeout=2)
|
||||
writes, cycles, errors = [], [], 0
|
||||
barrier.wait()
|
||||
key = uuid.uuid4().hex
|
||||
body = "worker-%d-%s" % (number, key)
|
||||
while not stop.is_set():
|
||||
started = time.monotonic()
|
||||
try:
|
||||
response = local.message(alice, chat, body, key=key)
|
||||
writes.append(response["json"])
|
||||
# Read with the original session while another backend may serve it.
|
||||
local.call("GET", "/api/v1/chats/" + chat["id"], actor=bob, schema="Chat")
|
||||
finished = time.monotonic()
|
||||
cycles.append((finished, finished - started,
|
||||
response["headers"]["X-Instance-Id"]))
|
||||
key = uuid.uuid4().hex
|
||||
body = "worker-%d-%s" % (number, key)
|
||||
except Failure:
|
||||
errors += 1
|
||||
# An uncertain write keeps its original key and body.
|
||||
time.sleep(0.05)
|
||||
return writes, cycles, errors
|
||||
|
||||
started = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=4) as pool:
|
||||
futures = [pool.submit(worker, n) for n in range(4)]
|
||||
barrier.wait()
|
||||
try:
|
||||
time.sleep(1)
|
||||
action_started = time.monotonic()
|
||||
phases = action()
|
||||
action_finished = time.monotonic()
|
||||
time.sleep(7)
|
||||
finally:
|
||||
stop.set()
|
||||
results = [f.result() for f in futures]
|
||||
elapsed = time.monotonic() - started
|
||||
writes = [msg for messages, _, _ in results for msg in messages]
|
||||
cycles = sorted(item for _, values, _ in results for item in values)
|
||||
errors = sum(n for _, _, n in results)
|
||||
# An in-flight POST can finish before the stop and its paired GET after it.
|
||||
# Only cycles started after the action count as traffic to a surviving API.
|
||||
recovered = [t for t, duration, _ in cycles if t - duration >= action_finished]
|
||||
check(bool(recovered), "no reads/writes after " + name)
|
||||
recovery = recovered[0] - action_started
|
||||
check(recovery <= 10 if name.startswith("stop-") else True,
|
||||
"failover exceeded 10 seconds")
|
||||
times = [started] + [t for t, _, _ in cycles] + [time.monotonic()]
|
||||
max_gap = max(b - a for a, b in zip(times, times[1:]))
|
||||
check(max_gap <= 10, "no successful read/write cycle for more than 10 seconds")
|
||||
during = [t for t, _, _ in cycles if action_started <= t <= action_finished]
|
||||
if name == "rolling-update":
|
||||
check(bool(during), "no successful traffic during rolling replacement")
|
||||
for phase in phases or []:
|
||||
check(any(phase["started"] <= t <= phase["finished"] for t in during),
|
||||
"no successful traffic while replacing " + phase["service"])
|
||||
stored = history(c, bob, chat)
|
||||
ids = {m["id"] for m in stored}
|
||||
check(len(ids) == len(stored), "duplicate message IDs")
|
||||
check(len({m["text"] for m in stored}) == len(stored), "retry created duplicate text")
|
||||
by_id = {m["id"]: m for m in stored}
|
||||
check(all(by_id.get(m["id"]) == m for m in [initial] + writes), "confirmed data lost")
|
||||
check(c.message(alice, chat, initial["text"], key=initial_key)["json"] == initial,
|
||||
"pre-action idempotency result lost")
|
||||
latency = sorted(d * 1000 for _, d, _ in cycles)
|
||||
def percentile(p):
|
||||
return round(latency[max(0, math.ceil(p * len(latency)) - 1)], 3)
|
||||
result = {"scenario": name, "concurrency": 4, "elapsed_seconds": round(elapsed, 3),
|
||||
"successful_read_write_cycles": len(cycles), "failed_attempts": errors,
|
||||
"successful_http_requests": len(writes) + len(cycles),
|
||||
"successful_rps": round((len(writes) + len(cycles)) / elapsed, 3),
|
||||
"successful_cycles_per_second": round(len(cycles) / elapsed, 3),
|
||||
"successful_cycles_during_action": len(during),
|
||||
"max_success_gap_seconds": round(max_gap, 3),
|
||||
"first_success_after_action_seconds": round(recovery, 3),
|
||||
"latency_ms": {"p50": percentile(.5), "p95": percentile(.95), "p99": percentile(.99)},
|
||||
"instances_before": sorted(observed),
|
||||
"instances_after": sorted({i for t, duration, i in cycles
|
||||
if t - duration >= action_finished}),
|
||||
"confirmed_messages_preserved": len({m["id"] for m in [initial] + writes}),
|
||||
"note": "Closed-loop: one POST + one GET per cycle. RPS counts confirmed HTTP responses, including partial cycles. Latency covers successful cycles only. Recovery includes action duration."}
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2), flush=True)
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Lab3 grade4 Compose experiments")
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
ops.BASE_URL = c.base
|
||||
dest = ROOT / "evidence"
|
||||
dest.mkdir(exist_ok=True)
|
||||
http_args = ["--base-url", c.base]
|
||||
if args.ca_file:
|
||||
http_args += ["--ca-file", args.ca_file]
|
||||
|
||||
def checked(script, filename, *extra):
|
||||
result = subprocess.run([sys.executable, str(ROOT / script), *extra], cwd=ROOT,
|
||||
text=True, encoding="utf-8", stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
env=dict(os.environ, PYTHONIOENCODING="utf-8"))
|
||||
(dest / filename).write_text(result.stdout, encoding="utf-8")
|
||||
print(result.stdout, end="", flush=True)
|
||||
check(result.returncode == 0, script + " failed; see evidence/" + filename)
|
||||
|
||||
ops.restore()
|
||||
checked("scripts/check_contract.py", "check.txt")
|
||||
checked("tests/smoke.py", "smoke.txt", *http_args)
|
||||
checked("student-tests/run.py", "student-tests.txt", *http_args)
|
||||
results = []
|
||||
processes = []
|
||||
for service in ops.SERVICES:
|
||||
ops.restore()
|
||||
before = ops.state(service)
|
||||
try:
|
||||
result = exercise(c, "stop-" + service, lambda: ops.stop_one(service))
|
||||
after = ops.state(service)
|
||||
check(after["status"] == "exited", "stopped API restarted unexpectedly")
|
||||
check(all(i.split(":", 1)[0] != before["hostname"] for i in result["instances_after"]),
|
||||
"stopped replica still handles requests")
|
||||
results.append(result)
|
||||
processes.append({"action": "stop-" + service, "before": before, "after": after})
|
||||
finally:
|
||||
ops.restore()
|
||||
before = [ops.state(s) for s in ops.SERVICES]
|
||||
results.append(exercise(c, "rolling-update", ops.rolling_update))
|
||||
after = [ops.state(s) for s in ops.SERVICES]
|
||||
check(all(a["container"] != b["container"] for a, b in zip(before, after)),
|
||||
"both API containers must be replaced")
|
||||
processes.append({"action": "rolling-update", "before": before, "after": after})
|
||||
(dest / "grade4.json").write_text(json.dumps(results, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
(dest / "processes.json").write_text(json.dumps(processes, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
suffix = ".cmd" if os.name == "nt" else ".sh"
|
||||
try:
|
||||
checked("tests/resilience.py", "failover.txt", "--mode", "failover",
|
||||
"--action-script", str(ROOT / ("scripts/stop-one" + suffix)), *http_args)
|
||||
finally:
|
||||
ops.restore()
|
||||
checked("tests/resilience.py", "persistence.txt", "--mode", "persistence",
|
||||
"--action-script", str(ROOT / ("scripts/restart" + suffix)), *http_args)
|
||||
print("PASS: both replica failures, rolling replacement and confirmed data")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Operator actions for the local Compose stand; volumes are kept."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SERVICES = ("api1", "api2")
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/")
|
||||
|
||||
|
||||
def compose(*args, capture=False):
|
||||
result = subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True,
|
||||
text=True, stdout=subprocess.PIPE if capture else None)
|
||||
return result.stdout.strip() if capture else None
|
||||
|
||||
|
||||
def state(service):
|
||||
cid = compose("ps", "--all", "--quiet", service, capture=True)
|
||||
if not cid:
|
||||
raise RuntimeError("No container for " + service)
|
||||
result = subprocess.check_output(
|
||||
["docker", "inspect", "--format",
|
||||
'{{json .Config.Hostname}} {{json .State.Status}}', cid], text=True)
|
||||
hostname, status = result.strip().split()
|
||||
return {"service": service, "container": cid[:12],
|
||||
"hostname": json.loads(hostname), "status": json.loads(status)}
|
||||
|
||||
|
||||
def wait_instances(count=2, timeout=40):
|
||||
deadline = time.monotonic() + timeout
|
||||
seen = set()
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
with urllib.request.urlopen(BASE_URL + "/health/ready", timeout=2) as r:
|
||||
instance = r.headers.get("X-Instance-Id")
|
||||
if instance:
|
||||
seen.add(instance)
|
||||
if len(seen) >= count:
|
||||
return seen
|
||||
except (OSError, urllib.error.URLError):
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
raise RuntimeError("Not enough ready API instances through LB")
|
||||
|
||||
|
||||
def stop_one(service=None):
|
||||
target = os.environ.get("TARGET_INSTANCE_ID", "").split(":", 1)[0]
|
||||
if target:
|
||||
service = next((s for s in SERVICES if state(s)["hostname"] == target), None)
|
||||
if not service:
|
||||
raise RuntimeError("TARGET_INSTANCE_ID is not an API container")
|
||||
service = service or "api1"
|
||||
if service not in SERVICES:
|
||||
raise RuntimeError("Only API replicas may be stopped")
|
||||
compose("kill", "--signal", "SIGKILL", service)
|
||||
current = state(service)
|
||||
if current["status"] != "exited":
|
||||
raise RuntimeError("API did not stop")
|
||||
print(json.dumps(current))
|
||||
|
||||
|
||||
def restore():
|
||||
compose("up", "-d", "--no-deps", "--no-build", "--wait", "--wait-timeout", "40", *SERVICES)
|
||||
wait_instances()
|
||||
|
||||
|
||||
def restart():
|
||||
compose("down")
|
||||
compose("up", "-d", "--no-build", "--wait", "--wait-timeout", "60")
|
||||
wait_instances()
|
||||
|
||||
|
||||
def rolling_update():
|
||||
phases = []
|
||||
compose("build", "api1")
|
||||
# This migration only adds tables and is compatible with the old API.
|
||||
compose("run", "--rm", "--no-deps", "migrate")
|
||||
for service in SERVICES:
|
||||
before = state(service)
|
||||
started = time.monotonic()
|
||||
compose("up", "-d", "--no-deps", "--no-build", "--force-recreate",
|
||||
"--wait", "--wait-timeout", "40", service)
|
||||
after = state(service)
|
||||
if before["container"] == after["container"]:
|
||||
raise RuntimeError("Container was not replaced")
|
||||
seen = wait_instances()
|
||||
if not any(i.split(":", 1)[0] == after["hostname"] for i in seen):
|
||||
raise RuntimeError("LB has not discovered the replacement")
|
||||
phases.append({"service": service, "started": started, "finished": time.monotonic()})
|
||||
print(json.dumps({"replaced": service, "before": before, "after": after}))
|
||||
return phases
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("action", choices=["restart", "stop-one", "restore", "rolling-update"])
|
||||
parser.add_argument("--service", choices=SERVICES)
|
||||
args = parser.parse_args()
|
||||
if args.action == "stop-one":
|
||||
stop_one(args.service)
|
||||
else:
|
||||
{"restart": restart, "restore": restore, "rolling-update": rolling_update}[args.action]()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
python scripts\ops.py restart
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/ops.py restart
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
python scripts\ops.py stop-one
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/ops.py stop-one
|
||||
@@ -6,17 +6,17 @@ from openapi_spec_validator import validate
|
||||
from jsonschema import Draft202012Validator
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
spec = json.loads((root/'contracts/openapi.json').read_text())
|
||||
spec = json.loads((root/'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
validate(spec)
|
||||
from referencing import Registry, Resource
|
||||
# The base URI resolves local #/components references inside examples.
|
||||
resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'})
|
||||
registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource))
|
||||
for name, example in json.loads((root/'contracts/examples.json').read_text()).items():
|
||||
for name, example in json.loads((root/'contracts/examples.json').read_text(encoding='utf-8')).items():
|
||||
schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name}
|
||||
Draft202012Validator(schema, registry=registry).validate(example)
|
||||
for path in (root/'contracts').glob('*.schema.json'):
|
||||
schema = json.loads(path.read_text())
|
||||
schema = json.loads(path.read_text(encoding='utf-8'))
|
||||
Draft202012Validator.check_schema(schema)
|
||||
for example in schema.get('examples',[]):
|
||||
Draft202012Validator(schema).validate(example)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Entry point: uvicorn as PID 1 with env-driven host/port and graceful shutdown."""
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
|
||||
from .main import app
|
||||
|
||||
|
||||
def main() -> None:
|
||||
host = os.environ.get("APP_HOST", "0.0.0.0")
|
||||
port = int(os.environ.get("APP_PORT", "8080"))
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
log_level="info",
|
||||
access_log=True,
|
||||
timeout_keep_alive=2,
|
||||
timeout_graceful_shutdown=5,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import os
|
||||
import secrets
|
||||
import redis.asyncio as redis
|
||||
from passlib.context import CryptContext
|
||||
|
||||
# bcrypt_sha256 avoids bcrypt's 72-byte limit. Old lab2 hashes still verify.
|
||||
pwd_context = CryptContext(schemes=["bcrypt_sha256", "bcrypt"], deprecated="auto")
|
||||
CACHE_URL = os.environ.get("CACHE_URL", "redis://localhost:6379/0")
|
||||
SESSION_TTL_SECONDS = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
|
||||
|
||||
redis_client = redis.from_url(CACHE_URL, decode_responses=True,
|
||||
socket_connect_timeout=1, socket_timeout=1)
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
async def create_session(user_id: str) -> str:
|
||||
token = secrets.token_urlsafe(32)
|
||||
await redis_client.setex(f"session:{token}", SESSION_TTL_SECONDS, user_id)
|
||||
return token
|
||||
|
||||
async def get_user_id_by_token(token: str) -> str | None:
|
||||
return await redis_client.get(f"session:{token}")
|
||||
|
||||
async def delete_session(token: str) -> None:
|
||||
await redis_client.delete(f"session:{token}")
|
||||
@@ -0,0 +1,69 @@
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from typing import List
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from sqlalchemy import String, ForeignKey, DateTime, Index, Integer
|
||||
from sqlalchemy.dialects.postgresql import UUID as PG_UUID, JSONB
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
DATABASE_URL = os.environ.get("DATABASE_URL", "postgresql+asyncpg://user:password@localhost:5432/messenger")
|
||||
engine = create_async_engine(
|
||||
DATABASE_URL, echo=False, pool_pre_ping=True, pool_size=5, max_overflow=5,
|
||||
pool_timeout=2, connect_args={"timeout": 1, "command_timeout": 3,
|
||||
"server_settings": {"statement_timeout": "2500", "lock_timeout": "2000"}},
|
||||
)
|
||||
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid4)
|
||||
username: Mapped[str] = mapped_column(String(32), unique=True, index=True)
|
||||
display_name: Mapped[str] = mapped_column(String(100))
|
||||
password_hash: Mapped[str] = mapped_column(String(255))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid4)
|
||||
title: Mapped[str] = mapped_column(String(100))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
||||
members: Mapped[List["ChatMember"]] = relationship(back_populates="chat", lazy="selectin")
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
chat_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True)
|
||||
user_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
|
||||
chat: Mapped["Chat"] = relationship(back_populates="members")
|
||||
user: Mapped["User"] = relationship()
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid4)
|
||||
chat_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), index=True)
|
||||
sender_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("users.id"))
|
||||
text: Mapped[str] = mapped_column(String(4000))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
||||
|
||||
# Составной индекс для быстрой курсорной пагинации (EXPLAIN покажет Index Scan)
|
||||
__table_args__ = (
|
||||
Index('ix_messages_chat_created_id', 'chat_id', 'created_at', 'id'),
|
||||
)
|
||||
|
||||
class IdempotencyKey(Base):
|
||||
__tablename__ = "idempotency_keys"
|
||||
user_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("users.id"), primary_key=True)
|
||||
chat_id: Mapped[UUID] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("chats.id"), primary_key=True)
|
||||
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
||||
request_text: Mapped[str] = mapped_column(String(4000))
|
||||
response: Mapped[dict | None] = mapped_column(JSONB, nullable=True)
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
|
||||
class SchemaVersion(Base):
|
||||
__tablename__ = "schema_versions"
|
||||
version: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
+372
@@ -0,0 +1,372 @@
|
||||
from __future__ import annotations
|
||||
import base64
|
||||
import asyncio
|
||||
import binascii
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import socket
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timezone, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, FastAPI, Request, Query
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from sqlalchemy import select, and_, or_, text
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from asyncpg import PostgresError
|
||||
from .db import engine, async_session, User, Chat, ChatMember, Message, IdempotencyKey
|
||||
from .auth import hash_password, verify_password, create_session, get_user_id_by_token, delete_session, redis_client, SESSION_TTL_SECONDS
|
||||
|
||||
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||
KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}", re.ASCII)
|
||||
INSTANCE_ID = socket.gethostname() + ":" + str(os.getpid())
|
||||
|
||||
def now_iso(dt: datetime = None) -> str:
|
||||
if not dt:
|
||||
dt = datetime.now(timezone.utc)
|
||||
return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
|
||||
def error_payload(request_id: str, code: str, message: str) -> dict:
|
||||
return {"error": {"code": code, "message": message, "request_id": request_id}}
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status: int, code: str, message: str):
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
super().__init__(message)
|
||||
|
||||
# --- Курсоры для пагинации (с точностью до микросекунд) ---
|
||||
def _encode_cursor(scope: str, created_at: datetime, obj_id: uuid.UUID) -> str:
|
||||
raw = f"{scope}|{created_at.isoformat()}|{obj_id}".encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii")
|
||||
|
||||
def _decode_cursor(cursor: str) -> tuple[str, datetime, uuid.UUID]:
|
||||
try:
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded).decode("utf-8")
|
||||
scope, dt_str, obj_id_str = raw.split("|", 2)
|
||||
dt = datetime.fromisoformat(dt_str)
|
||||
if dt.tzinfo is None:
|
||||
raise ValueError("timezone required")
|
||||
return scope, dt, uuid.UUID(obj_id_str)
|
||||
except Exception:
|
||||
raise ApiError(400, "invalid_request", "invalid cursor")
|
||||
|
||||
# --- Схемы ---
|
||||
class CreateUser(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
username: str = Field(strict=True)
|
||||
display_name: str = Field(strict=True, min_length=1, max_length=100)
|
||||
password: str = Field(strict=True, min_length=12, max_length=128)
|
||||
|
||||
@field_validator("display_name", "password")
|
||||
@classmethod
|
||||
def not_blank(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank value")
|
||||
return value
|
||||
|
||||
class CreateChat(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
title: str = Field(strict=True, min_length=1, max_length=100)
|
||||
member_ids: list[uuid.UUID] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def not_blank(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank title")
|
||||
return value
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
text: str = Field(strict=True, min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def not_blank(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank message")
|
||||
return value
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
yield
|
||||
await redis_client.aclose()
|
||||
await engine.dispose()
|
||||
|
||||
app = FastAPI(title="Messenger", version="1.3.0", lifespan=lifespan)
|
||||
security_bearer = HTTPBearer(auto_error=False)
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_id_middleware(request: Request, call_next):
|
||||
request_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
request.state.request_id = request_id
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception:
|
||||
logging.getLogger(__name__).exception("Request failed: %s", request_id)
|
||||
response = JSONResponse(status_code=500, content=error_payload(request_id, "internal_error", "internal error"))
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return response
|
||||
|
||||
def _rid(request: Request) -> str:
|
||||
return getattr(request.state, "request_id", None) or str(uuid.uuid4())
|
||||
|
||||
# --- Обработчики ошибок ---
|
||||
@app.exception_handler(ApiError)
|
||||
async def api_error_handler(request: Request, exc: ApiError):
|
||||
headers = {}
|
||||
if exc.status == 401:
|
||||
if request.url.path == "/api/v1/auth/sessions":
|
||||
headers["WWW-Authenticate"] = 'Basic realm="messenger", charset="UTF-8"'
|
||||
else:
|
||||
headers["WWW-Authenticate"] = 'Bearer realm="messenger"'
|
||||
return JSONResponse(status_code=exc.status, content=error_payload(_rid(request), exc.code, exc.message), headers=headers)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_error_handler(request: Request, exc: RequestValidationError):
|
||||
return JSONResponse(status_code=400, content=error_payload(_rid(request), "invalid_request", "invalid request"))
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
return JSONResponse(status_code=exc.status_code, content=error_payload(_rid(request), "not_found", str(exc.detail)))
|
||||
|
||||
@app.exception_handler(RedisError)
|
||||
@app.exception_handler(SQLAlchemyError)
|
||||
@app.exception_handler(PostgresError)
|
||||
@app.exception_handler(OSError)
|
||||
@app.exception_handler(TimeoutError)
|
||||
async def dependency_error_handler(request: Request, exc: Exception):
|
||||
# Если упала БД или Redis - отдаем 503, как требует контракт
|
||||
return JSONResponse(status_code=503, content=error_payload(_rid(request), "unavailable", "dependency unavailable"))
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def generic_exception_handler(request: Request, exc: Exception):
|
||||
return JSONResponse(status_code=500, content=error_payload(_rid(request), "internal_error", "internal error"))
|
||||
|
||||
# --- Зависимости ---
|
||||
async def get_actor(credentials: HTTPAuthorizationCredentials = Depends(security_bearer)) -> dict:
|
||||
if not credentials:
|
||||
raise ApiError(401, "unauthorized", "missing token")
|
||||
|
||||
user_id_str = await get_user_id_by_token(credentials.credentials)
|
||||
if not user_id_str:
|
||||
raise ApiError(401, "unauthorized", "invalid or expired token")
|
||||
|
||||
async with async_session() as session:
|
||||
user = await session.get(User, uuid.UUID(user_id_str))
|
||||
if not user:
|
||||
raise ApiError(401, "unauthorized", "user not found")
|
||||
return {"id": str(user.id), "username": user.username, "display_name": user.display_name, "created_at": now_iso(user.created_at)}
|
||||
|
||||
# --- Роуты ---
|
||||
@app.get("/health/live")
|
||||
def liveness():
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def readiness():
|
||||
async with asyncio.timeout(1.5):
|
||||
async with async_session() as session:
|
||||
version = await session.scalar(text("SELECT version FROM schema_versions WHERE version = 3"))
|
||||
if version != 3:
|
||||
raise ApiError(503, "unavailable", "migration required")
|
||||
await redis_client.ping()
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
async def register_user(payload: CreateUser):
|
||||
if not USERNAME_RE.fullmatch(payload.username):
|
||||
raise ApiError(400, "invalid_request", "invalid username")
|
||||
|
||||
async with async_session() as session:
|
||||
existing = await session.scalar(select(User).where(User.username == payload.username))
|
||||
if existing:
|
||||
raise ApiError(409, "conflict", "username already exists")
|
||||
|
||||
password_hash = await run_in_threadpool(hash_password, payload.password)
|
||||
user = User(username=payload.username, display_name=payload.display_name, password_hash=password_hash)
|
||||
session.add(user)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
await session.rollback()
|
||||
raise ApiError(409, "conflict", "username already exists")
|
||||
|
||||
return {"id": str(user.id), "username": user.username, "display_name": user.display_name, "created_at": now_iso(user.created_at)}
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
async def create_session_endpoint(request: Request):
|
||||
# HTTP Basic carries a UTF-8 password, not only ASCII.
|
||||
try:
|
||||
scheme, encoded = request.headers.get("Authorization", "").split(" ", 1)
|
||||
if scheme.lower() != "basic":
|
||||
raise ValueError("Basic required")
|
||||
username, password = base64.b64decode(encoded, validate=True).decode("utf-8").split(":", 1)
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
raise ApiError(401, "unauthorized", "invalid credentials")
|
||||
|
||||
async with async_session() as session:
|
||||
user = await session.scalar(select(User).where(User.username == username))
|
||||
if not user or not await run_in_threadpool(verify_password, password, user.password_hash):
|
||||
raise ApiError(401, "unauthorized", "invalid credentials")
|
||||
|
||||
token = await create_session(str(user.id))
|
||||
expires_at = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS)
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": now_iso(expires_at),
|
||||
"user": {"id": str(user.id), "username": user.username, "display_name": user.display_name, "created_at": now_iso(user.created_at)}
|
||||
}
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
async def logout(credentials: HTTPAuthorizationCredentials = Depends(security_bearer)):
|
||||
if not credentials:
|
||||
raise ApiError(401, "unauthorized", "missing token")
|
||||
|
||||
user_id = await get_user_id_by_token(credentials.credentials)
|
||||
if not user_id:
|
||||
raise ApiError(401, "unauthorized", "invalid or expired token")
|
||||
|
||||
await delete_session(credentials.credentials)
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
async def current_user(actor: dict = Depends(get_actor)):
|
||||
return actor
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
async def create_chat(payload: CreateChat, actor: dict = Depends(get_actor)):
|
||||
member_ids = [str(u) for u in payload.member_ids]
|
||||
if actor["id"] not in member_ids:
|
||||
raise ApiError(400, "invalid_request", "creator must be a member")
|
||||
if len(set(member_ids)) != len(member_ids):
|
||||
raise ApiError(400, "invalid_request", "duplicate members")
|
||||
|
||||
async with async_session() as session:
|
||||
users = await session.scalars(select(User.id).where(User.id.in_([uuid.UUID(m) for m in member_ids])))
|
||||
if len(list(users)) != len(set(member_ids)):
|
||||
raise ApiError(400, "invalid_request", "unknown member")
|
||||
|
||||
chat = Chat(title=payload.title)
|
||||
session.add(chat)
|
||||
await session.flush()
|
||||
|
||||
for mid in member_ids:
|
||||
session.add(ChatMember(chat_id=chat.id, user_id=uuid.UUID(mid)))
|
||||
|
||||
await session.commit()
|
||||
return {"id": str(chat.id), "title": chat.title, "member_ids": member_ids, "created_at": now_iso(chat.created_at)}
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
async def list_chats(limit: int = Query(50, ge=1, le=100), cursor: Optional[str] = None, actor: dict = Depends(get_actor)):
|
||||
async with async_session() as session:
|
||||
stmt = select(Chat).join(ChatMember).where(ChatMember.user_id == uuid.UUID(actor["id"]))
|
||||
if cursor:
|
||||
scope, c_dt, c_id = _decode_cursor(cursor)
|
||||
if scope != "chats":
|
||||
raise ApiError(400, "invalid_request", "invalid cursor scope")
|
||||
stmt = stmt.where(or_(
|
||||
Chat.created_at > c_dt,
|
||||
and_(Chat.created_at == c_dt, Chat.id > c_id)
|
||||
))
|
||||
|
||||
stmt = stmt.order_by(Chat.created_at.asc(), Chat.id.asc()).limit(limit + 1)
|
||||
chats = (await session.scalars(stmt)).unique().all()
|
||||
|
||||
has_more = len(chats) > limit
|
||||
page = chats[:limit]
|
||||
next_cursor = _encode_cursor("chats", page[-1].created_at, page[-1].id) if has_more and page else None
|
||||
|
||||
items = []
|
||||
for c in page:
|
||||
items.append({
|
||||
"id": str(c.id),
|
||||
"title": c.title,
|
||||
"created_at": now_iso(c.created_at),
|
||||
"member_ids": [str(m.user_id) for m in c.members] # Теперь участники возвращаются корректно
|
||||
})
|
||||
|
||||
return {"items": items, "next_cursor": next_cursor}
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
async def get_chat(chat_id: uuid.UUID, actor: dict = Depends(get_actor)):
|
||||
async with async_session() as session:
|
||||
chat = await session.scalar(select(Chat).join(ChatMember).where(Chat.id == chat_id, ChatMember.user_id == uuid.UUID(actor["id"])))
|
||||
if not chat:
|
||||
raise ApiError(404, "not_found", "chat not found")
|
||||
return {"id": str(chat.id), "title": chat.title, "member_ids": [str(m.user_id) for m in chat.members], "created_at": now_iso(chat.created_at)}
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
async def post_message(chat_id: uuid.UUID, payload: CreateMessage, request: Request, actor: dict = Depends(get_actor)):
|
||||
key = request.headers.get("Idempotency-Key", "")
|
||||
if not KEY_RE.fullmatch(key):
|
||||
raise ApiError(400, "invalid_request", "invalid Idempotency-Key")
|
||||
async with async_session() as session:
|
||||
member = await session.scalar(select(ChatMember).where(and_(ChatMember.chat_id == chat_id, ChatMember.user_id == uuid.UUID(actor["id"]))))
|
||||
if not member:
|
||||
raise ApiError(404, "not_found", "chat not found")
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
scope = {"user_id": uuid.UUID(actor["id"]), "chat_id": chat_id, "key": key}
|
||||
await session.execute(insert(IdempotencyKey).values(
|
||||
**scope, request_text=payload.text, expires_at=now + timedelta(hours=24)
|
||||
).on_conflict_do_nothing())
|
||||
record = await session.scalar(select(IdempotencyKey).where(
|
||||
IdempotencyKey.user_id == scope["user_id"],
|
||||
IdempotencyKey.chat_id == chat_id, IdempotencyKey.key == key
|
||||
).with_for_update())
|
||||
if record.response is not None and record.expires_at > now:
|
||||
if record.request_text != payload.text:
|
||||
raise ApiError(409, "conflict", "key used with another message")
|
||||
return record.response
|
||||
|
||||
msg = Message(chat_id=chat_id, sender_id=uuid.UUID(actor["id"]), text=payload.text)
|
||||
session.add(msg)
|
||||
await session.flush()
|
||||
result = {"id": str(msg.id), "chat_id": str(msg.chat_id), "sender_id": str(msg.sender_id), "text": msg.text, "created_at": now_iso(msg.created_at)}
|
||||
record.request_text = payload.text
|
||||
record.expires_at = now + timedelta(hours=24)
|
||||
record.response = result
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
async def get_messages(chat_id: uuid.UUID, limit: int = Query(50, ge=1, le=100), cursor: Optional[str] = None, actor: dict = Depends(get_actor)):
|
||||
async with async_session() as session:
|
||||
member = await session.scalar(select(ChatMember).where(and_(ChatMember.chat_id == chat_id, ChatMember.user_id == uuid.UUID(actor["id"]))))
|
||||
if not member:
|
||||
raise ApiError(404, "not_found", "chat not found")
|
||||
|
||||
stmt = select(Message).where(Message.chat_id == chat_id)
|
||||
if cursor:
|
||||
scope, c_dt, c_id = _decode_cursor(cursor)
|
||||
if scope != str(chat_id):
|
||||
raise ApiError(400, "invalid_request", "invalid cursor scope")
|
||||
stmt = stmt.where(or_(
|
||||
Message.created_at > c_dt,
|
||||
and_(Message.created_at == c_dt, Message.id > c_id)
|
||||
))
|
||||
|
||||
stmt = stmt.order_by(Message.created_at.asc(), Message.id.asc()).limit(limit + 1)
|
||||
messages = (await session.scalars(stmt)).all()
|
||||
|
||||
has_more = len(messages) > limit
|
||||
page = messages[:limit]
|
||||
next_cursor = _encode_cursor(str(chat_id), page[-1].created_at, page[-1].id) if has_more and page else None
|
||||
|
||||
items = [{"id": str(m.id), "chat_id": str(m.chat_id), "sender_id": str(m.sender_id), "text": m.text, "created_at": now_iso(m.created_at)} for m in page]
|
||||
return {"items": items, "next_cursor": next_cursor}
|
||||
@@ -0,0 +1,18 @@
|
||||
import asyncio
|
||||
|
||||
from sqlalchemy import text
|
||||
from .db import Base, engine
|
||||
|
||||
|
||||
async def main():
|
||||
async with engine.begin() as conn:
|
||||
# Only the migrate service creates tables; concurrent operators serialize.
|
||||
await conn.execute(text("SELECT pg_advisory_xact_lock(300300)"))
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.execute(text("INSERT INTO schema_versions(version) VALUES (3) ON CONFLICT DO NOTHING"))
|
||||
await engine.dispose()
|
||||
print("Migration 3 applied: existing lab2 tables kept, idempotency added")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Lab3: concurrent idempotency and validation")
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
c.call("POST", "/api/v1/users", 400,
|
||||
body={"username": "valid\n", "display_name": "Test", "password": "LongPassword123"})
|
||||
registration = {"username": "race_" + uuid.uuid4().hex[:12],
|
||||
"display_name": "Race", "password": "LongPassword123"}
|
||||
registration_barrier = threading.Barrier(2)
|
||||
|
||||
def register(_):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
registration_barrier.wait()
|
||||
return local.call("POST", "/api/v1/users", [201, 409],
|
||||
body=registration, schema="User")["status"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
check(sorted(pool.map(register, range(2))) == [201, 409], "registration race")
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
key = uuid.uuid4().hex
|
||||
barrier = threading.Barrier(12)
|
||||
|
||||
def send(_):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
barrier.wait()
|
||||
return local.message(alice, chat, "Одна запись", key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as pool:
|
||||
responses = list(pool.map(send, range(12)))
|
||||
first = responses[0]["json"]
|
||||
check(all(r["json"] == first for r in responses), "concurrent replay differs")
|
||||
instances = {r["headers"]["X-Instance-Id"] for r in responses}
|
||||
check(len(instances) >= 2, "same key must reach at least two replicas")
|
||||
page = c.call("GET", path, actor=bob, schema="MessagePage")["json"]
|
||||
check(page["items"] == [first], "concurrent retry created duplicates")
|
||||
c.call("POST", path, 409, actor=alice, body={"text": "Изменённый текст"},
|
||||
headers={"Idempotency-Key": key})
|
||||
c.call("POST", path, 404, actor=eve, body={"text": "Одна запись"},
|
||||
headers={"Idempotency-Key": key})
|
||||
# The same key belongs to a different scope for Bob and for another chat.
|
||||
other = c.message(bob, chat, "Другая область", key=key)["json"]
|
||||
check(other["id"] != first["id"], "different users share an idempotency scope")
|
||||
second_chat = c.call("POST", "/api/v1/chats", 201, actor=alice,
|
||||
body={"title": "Второй", "member_ids": [alice["id"]]},
|
||||
schema="Chat")["json"]
|
||||
other = c.message(alice, second_chat, "Одна запись", key=key)["json"]
|
||||
check(other["id"] != first["id"], "different chats share an idempotency scope")
|
||||
for bad_key in ("", "bad key", "x" * 129):
|
||||
c.call("POST", path, 400, actor=alice, body={"text": "Текст"},
|
||||
headers={"Idempotency-Key": bad_key})
|
||||
for body in ({"text": " "}, {"text": 123}, {"text": "x", "sender_id": alice["id"]}):
|
||||
c.call("POST", path, 400, actor=alice, body=body,
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex})
|
||||
c.call("POST", path, 400, actor=alice, raw=b'{"text":',
|
||||
headers={"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex})
|
||||
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
|
||||
c.call("POST", path, 401, actor=alice, body={"text": "Одна запись"},
|
||||
headers={"Idempotency-Key": key})
|
||||
print("PASS: 12 concurrent requests across %d replicas, one message" % len(instances))
|
||||
print("PASS: conflict, key scope, validation, access and revoked session")
|
||||
print("PASS: concurrent registration, exactly one 201 and one 409")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
+2
-2
@@ -14,7 +14,7 @@ import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
LAB = json.loads((ROOT / 'lab.json').read_text())['number']
|
||||
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text())
|
||||
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
|
||||
class Failure(AssertionError):
|
||||
pass
|
||||
@@ -102,7 +102,7 @@ class Client:
|
||||
self.base = (base_url or os.environ.get('BASE_URL') or ('https://localhost:8443' if LAB==7 else 'http://localhost:8080')).rstrip('/')
|
||||
check(self.base.startswith(('http://','https://')), 'BASE_URL must be HTTP(S)')
|
||||
check(LAB != 7 or self.base.startswith('https://'), 'lab7 requires HTTPS')
|
||||
self.timeout = timeout
|
||||
self.timeout = max(timeout, 5)
|
||||
context = ssl.create_default_context(cafile=ca_file or os.environ.get('CA_FILE') or None)
|
||||
self.opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPSHandler(context=context))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user