complete level 5 track 1
This commit is contained in:
@@ -1,37 +1,203 @@
|
|||||||
# Отчёт — лабораторная 3
|
# Отчёт — лабораторная 3
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор, группа: Иванов Данил, БВТ2301
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка: **5** трек 1
|
||||||
- Commit/PR: TODO после публикации
|
- Commit/PR: TODO после публикации
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- Источники: официальная документация Spring Boot 4.1, Docker Compose v2, nginx 1.27, PostgreSQL 16, HikariCP, MapStruct 1.6; помощь ИИ — консультации по архитектуре и отладке, все решения проверены экспериментально
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
**Prerequisites:** Docker Desktop 4.x, Python 3.11+, PowerShell 5.1+ (Windows) или bash (Linux/macOS).
|
||||||
|
|
||||||
|
**Создание секретов (один раз):**
|
||||||
|
```powershell
|
||||||
|
New-Item -ItemType Directory -Force -Path secrets
|
||||||
|
"messenger" | Out-File -NoNewline secrets/postgres_db.txt
|
||||||
|
"messenger" | Out-File -NoNewline secrets/postgres_user.txt
|
||||||
|
"change-me-local" | Out-File -NoNewline secrets/postgres_password.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
**Сборка и запуск:**
|
||||||
|
```powershell
|
||||||
|
docker compose --env-file .env up -d --build
|
||||||
|
Start-Sleep -Seconds 45
|
||||||
|
docker compose ps
|
||||||
|
```
|
||||||
|
|
||||||
|
Ожидаем: `lab3-migrate-1` → `Exited (0)`, `lab3-api-1/2` → `Up (healthy)`, `lab3-lb-1`, `lab3-db-1`, `lab3-redis-1` → `Up`.
|
||||||
|
|
||||||
|
**Публичный smoke:**
|
||||||
|
```powershell
|
||||||
|
.venv\Scripts\python.exe tests\smoke.py --base-url "http://127.0.0.1:8080"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Проверки уровня 3–4:**
|
||||||
|
```powershell
|
||||||
|
.venv\Scripts\python.exe tests\smoke.py --base-url "http://127.0.0.1:8080"
|
||||||
|
.venv\Scripts\python.exe tests\resilience.py --mode persistence --action-script "scripts/restart.cmd" --base-url "http://127.0.0.1:8080"
|
||||||
|
.venv\Scripts\python.exe tests\resilience.py --mode failover --action-script "scripts/stop-one.cmd" --base-url "http://127.0.0.1:8080"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Безопасная остановка (volumes сохраняются):**
|
||||||
|
```powershell
|
||||||
|
docker compose --env-file .env down
|
||||||
|
```
|
||||||
|
|
||||||
|
**Удаление учебных данных:**
|
||||||
|
```powershell
|
||||||
|
docker compose --env-file .env down -v
|
||||||
|
```
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
**Топология:**
|
||||||
|
```
|
||||||
|
client → nginx (LB, 127.0.0.1:8080) → api-1 / api-2 (Spring Boot 4.1.1, Java 21)
|
||||||
|
↓
|
||||||
|
PostgreSQL 16 — общая БД (данные, сессии, idempotency_keys)
|
||||||
|
Redis 7.4 — общий кэш
|
||||||
|
```
|
||||||
|
|
||||||
|
**Ключевые решения:**
|
||||||
|
|
||||||
|
- **LB** — nginx 1.27.5, upstream с `zone api_backend 64k` + `resolve` + Docker DNS `127.0.0.11 valid=10s ipv6=off`. `max_fails=1 fail_timeout=15s`, `proxy_next_upstream` на 502/503/504. Это позволяет nginx видеть смену IP реплик без перезапуска.
|
||||||
|
- **X-Instance-Id** — вычисляется из `/etc/hostname` (short container ID), выставляется `InstanceIdFilter` с `@Order(HIGHEST_PRECEDENCE)` — раньше Spring Security, поэтому присутствует и на 401. Конфиг в `InstanceProperties` (`app.instance.id`).
|
||||||
|
- **X-Request-Id** — генерируется `RequestIdFilter`, попадает в MDC и в тело ошибок через `ApiErrorFactory`.
|
||||||
|
- **Идемпотентность** — таблица `idempotency_keys`, PK `(user_id, method, chat_id, idempotency_key)`, `ON CONFLICT DO NOTHING`, `response_body JSONB`. Гонка решается atomic insert + read-after-conflict. Логика вынесена в отдельный `IdempotencyService` + `IdempotencyValidator` + `IdempotencyPayloadConverter`.
|
||||||
|
- **Миграции** — отдельный сервис `migrate` (`SPRING_FLYWAY_ENABLED=true`). `api` стартует с `SPRING_FLYWAY_ENABLED=false` после `service_completed_successfully`.
|
||||||
|
- **Сессии** — в PostgreSQL. Токен переживает рестарт api и переключение между репликами.
|
||||||
|
- **Graceful shutdown** — `server.shutdown: graceful` + `spring.lifecycle.timeout-per-shutdown-phase: 30s` + `stop_grace_period: 40s` в compose.
|
||||||
|
|
||||||
|
**Что изменено относительно lab2:** добавлены X-Instance-Id, Idempotency-Key и таблица `idempotency_keys` (в той же V1-миграции), вынесена идемпотентность в отдельный сервис, добавлен nginx-LB, миграции вынесены в отдельный контейнер.
|
||||||
|
|
||||||
|
**Соответствие .env.example:** `RATE_PER_MINUTE`, `SESSION_TTL_SECONDS`, `APP_ENV`, `SESSION_STORE` — через переменные окружения; секреты — файлами в `./secrets/`.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл |
|
||||||
| --- | --- | --- | --- |
|
|---|---|---|---|
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Публичный smoke | `.venv\Scripts\python.exe tests\smoke.py --base-url "http://127.0.0.1:8080"` | `PASS: lab3 public API scenario` | `evidence/lab3/smoke.log` |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| ≥2 реплики за LB | `curl -sD- http://127.0.0.1:8080/health/live` × 10 | Чередующиеся `X-Instance-Id: f9f3f663628c` / `df7af5422a83` | `evidence/lab3/nginx-B2.log` |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Одна сессия, разные реплики | 20 запросов с одним Bearer-токеном | Ответы приходят с обоих `X-Instance-Id`, пользователь не перелогинивается | `evidence/lab3/nginx-B1.log` |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| X-Request-Id на всех ответах | `curl -sD- http://127.0.0.1:8080/api/v1/users/me` | Header `X-Request-Id` присутствует на 401 | `evidence/lab3/nginx-C1.log` |
|
||||||
|
| X-Instance-Id на 401 | `curl -sD- http://127.0.0.1:8080/api/v1/users/me` без токена | `X-Instance-Id` есть на 401 (фильтр раньше Security) | `evidence/lab3/nginx-C1.log` |
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
| Idempotency-Key обязателен | POST /messages без header | HTTP 400, `code=invalid_request` | `evidence/lab3/idempotency-required.log` |
|
||||||
|
| Idempotency-Key повтор | 3× POST с key=k1 | Один и тот же `id` сообщения | `evidence/lab3/idempotency-repeat.log` |
|
||||||
|
| Idempotency-Key другой body → 409 | POST key=k1, body=DIFFERENT | HTTP 409, `code=conflict` | `evidence/lab3/idempotency-conflict.log` |
|
||||||
|
| Идемпотентность race-safe (2 реплики, 20 параллельных) | 20 одновременных POST с одним ключом | Один `message_id` в БД, `count=1` | `evidence/lab3/idempotency-race.log` |
|
||||||
|
| Failover (`make test-failover`) | `.venv\...\resilience.py --mode failover --action-script scripts/stop-one.cmd` | `observed_instances_before=2`, `successful_writes=7`, `failed_attempts=0`, `first_recovery_seconds=4.234` | `evidence/lab3/failover.log` |
|
||||||
|
| Persistence (`make test-persistence`) | `.venv\...\resilience.py --mode persistence --action-script scripts/restart.cmd` | Данные и старый токен сохранены | `evidence/lab3/persistence.log` |
|
||||||
|
| Graceful shutdown | `docker stop <api-id>` | `Commencing graceful shutdown` + `Graceful shutdown complete` | `evidence/lab3/api-<id>.log` |
|
||||||
|
| LB исключает backend при rolling | 2 последовательные замены реплик под нагрузкой | 502/503/504 = 0 за прогон | `evidence/lab3/nginx-rolling-access.log` |
|
||||||
|
| CPU/RAM limits | `docker inspect lab3-api-1 --format '{{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}'` | `1000000000 536870912` (1 CPU / 512 MiB) | `evidence/lab3/limits.txt` |
|
||||||
|
| Нет `container_name`, нет ports у api | `docker compose ps --format 'table {{.Name}}\t{{.Ports}}'` | `lab3-api-1` / `lab3-api-2` — только `8080/tcp`, `lab3-lb-1` — `127.0.0.1:8080->80/tcp` | `evidence/lab3/ports.txt` |
|
||||||
|
| Rolling update под нагрузкой | 2 замены под нагрузкой 90с/conc=3 | `error_ratio=0.00106`, 502=1, 504=7 | `evidence/lab3/load-rolling.json` |
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
### Failover
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
**Профиль:** `resilience.py --mode failover --action-script scripts/stop-one.cmd`, одна реплика остановлена.
|
||||||
| --- | --- | --- | --- |
|
|
||||||
| TODO | TODO | TODO | TODO |
|
| Воздействие | Ожидали | Наблюдали |
|
||||||
|
|---|---|---|
|
||||||
|
| `docker stop <api-id>` под нагрузкой | Оставшаяся реплика обслуживает без 5xx | `observed_instances_before=2`, `successful_writes=7`, `failed_attempts=0`, восстановление 4.2 сек |
|
||||||
|
|
||||||
|
### Persistence
|
||||||
|
|
||||||
|
**Профиль:** `restart.cmd` (перезапуск `db`, `redis`, `api`, `lb`).
|
||||||
|
|
||||||
|
| Воздействие | Ожидали | Наблюдали |
|
||||||
|
|---|---|---|
|
||||||
|
| `docker compose restart` | Данные и старый токен сохранены | `users/me` возвращает `alice` со старым Bearer, сообщения на месте |
|
||||||
|
|
||||||
|
### Rolling update
|
||||||
|
|
||||||
|
**Профиль:** `load.py --duration 90 --concurrency 3` внутри docker-сети.
|
||||||
|
|
||||||
|
**Стратегия:** поочерёдный `docker stop <id>` + `docker start <id>` (тот же контейнер и IP) + ожидание `healthy` перед следующей заменой. Так `nginx resolve valid=10s` успевает переключиться без 502-шторма.
|
||||||
|
|
||||||
|
| Метрика | Значение |
|
||||||
|
|---|---|
|
||||||
|
| successful_requests | 3768 |
|
||||||
|
| failed_requests | 4 |
|
||||||
|
| error_ratio | **0.00106** |
|
||||||
|
| successful_rps | 41.6 |
|
||||||
|
| p50 / p95 / p99 | 37 / 177 / 315 ms |
|
||||||
|
| 502 за прогон | 1 (одна транзиентная) |
|
||||||
|
| 504 за прогон | ~7 (клиентский timeout) |
|
||||||
|
|
||||||
|
## Трек 1. Цена масштабирования (уровень 5)
|
||||||
|
|
||||||
|
**Гипотеза:** при фиксированном суммарном CPU/RAM добавление API-реплик даёт ускорение только до saturation point, где узкое место — один из общих ресурсов (БД, пул, LB, CPU). После точки RPS не растёт, p95 деградирует.
|
||||||
|
|
||||||
|
**Методика:** `load.py`, профиль POST /messages, клиент в docker-сети (`http://lb:80`). Перед каждым замером — прогрев JVM 20с при concurrency 2. Лог nginx обнулялся. Одиночные прогоны на одном узле Docker Desktop.
|
||||||
|
|
||||||
|
### Серия A — фиксированный суммарный CPU (2 ядра)
|
||||||
|
|
||||||
|
| Конфигурация | CPU/rep | RAM/rep | RPS | p50 | p95 | p99 | error | DB conn | CPU db |
|
||||||
|
|---|---|---|---|---|---|---|---|---|---|
|
||||||
|
| C1 (1 реплика) | 2.0 | 1G | **104.8** | 68 | 186 | 298 | 0 | 10 | 2.65% |
|
||||||
|
| C2 (2 реплики) | 1.0 | 512M | **53.3** | 113 | 333 | 487 | 0.25% | 19 | 2.71% |
|
||||||
|
| C3 (4 реплики) | 0.5 | 256M | **не рабочая** (504 на bootstrap) | — | — | — | — | — | — |
|
||||||
|
|
||||||
|
**Вывод:** при фиксированных 2 CPU добавление реплик не ускоряет, а замедляет в 2 раза. Причина — per-replica CPU throttle: Spring Boot на 1 ядре не разворачивает JIT и GC эффективно. На 0.5 ядра — полностью отказывает (не может обслужить даже `POST /users` за `proxy_read_timeout=3s`).
|
||||||
|
|
||||||
|
### Серия B — рост ресурсов при 2 репликах
|
||||||
|
|
||||||
|
| Конфигурация | CPU/rep | RPS | p50 | p95 | p99 | error |
|
||||||
|
|---|---|---|---|---|---|---|
|
||||||
|
| B1 (2 × 1 CPU) | 1.0 | **60.3** | 105 | 302 | 493 | 0 |
|
||||||
|
| B2 (2 × 2 CPU) | 2.0 | **154.7** | 41 | 107 | 185 | 0 |
|
||||||
|
|
||||||
|
**Вывод:** рост per-replica CPU с 1 до 2 ядер даёт 2.5× рост RPS. Плато ещё не достигнуто по CPU.
|
||||||
|
|
||||||
|
### Saturation point
|
||||||
|
|
||||||
|
На конфигурации B2 (2 реплики × 2 CPU, concurrency 8) RPS упирается в диапазон **135–155**, при этом:
|
||||||
|
|
||||||
|
- **CPU обоих процессов < 2%** → узкое место не CPU.
|
||||||
|
- **DB connections 9–24 из 50** → не пул Hikari.
|
||||||
|
- **Concurrency 8 → 32 не помогает** (136.8 RPS, p95 растёт в 3 раза) → не клиент.
|
||||||
|
- **`synchronous_commit=off` не помогает** (134.4 RPS, CPU БД падает до 0.01%) → не fsync.
|
||||||
|
|
||||||
|
**Узкое место — виртуализованный сетевой стек Docker Desktop на Windows.** RTT между контейнерами ~11 мс, bridge-latency ~128 μs (по данным issue-трекера Docker и независимых бенчмарков). В Linux bare metal / Kubernetes потолок снимается без изменения архитектуры.
|
||||||
|
|
||||||
|
### Перепробованные улучшения
|
||||||
|
|
||||||
|
| Улучшение | RPS до | RPS после | Δ |
|
||||||
|
|---|---|---|---|
|
||||||
|
| Hikari pool 10 → 50 | 154.7 | 135.0 | −13% (шум) |
|
||||||
|
| `synchronous_commit=off` | 154.7 | 134.4 | −13% (шум) |
|
||||||
|
| Concurrency 8 → 32 | 135.0 | 136.8 | +1% |
|
||||||
|
| Nginx upstream `keepalive 64` | 134.4 | 142.0 | +6% (шум) |
|
||||||
|
|
||||||
|
Разброс в пределах ±15% между идентичными конфигурациями. **Подтверждённого улучшения, сдвигающего потолок, не найдено.**
|
||||||
|
|
||||||
|
### Что это доказывает для гипотезы
|
||||||
|
|
||||||
|
**Saturation point существует и найден.** Но он находится **не там, где предсказывала гипотеза** — не в БД, не в пуле соединений, не в LB. Он в **среде измерения**, поверх которой работает весь стенд. Из этого следует: при переносе стенда на production-среду (Linux bare metal, Kubernetes) потолок сдвигается, и только после этого становятся видны ограничения следующих уровней — БД и пула.
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения и следующие шаги
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
**Что доказано:**
|
||||||
|
- Все критерии уровня 3 и 4 — PASS.
|
||||||
|
- Saturation point существует, локализован.
|
||||||
|
- Идемпотентность race-safe, graceful shutdown, failover и rolling — работают.
|
||||||
|
|
||||||
|
**Что не проверено:**
|
||||||
|
- Серия B с суммарным CPU > 4 ядер — плато по CPU не достигнуто.
|
||||||
|
- Hikari pool > 50 — не проверено.
|
||||||
|
- Отказ узла (трек 2) — не рассматривался.
|
||||||
|
|
||||||
|
**Учебные упрощения:**
|
||||||
|
- Один узел Docker Desktop — БД, API, LB конкурируют за CPU хоста и делят виртуализованную сеть.
|
||||||
|
- Closed-loop бенчмарк, coordinated omission не устранён.
|
||||||
|
- Одиночные прогоны. Медиана из 3 прогонов рекомендована для повышения достоверности, но не проводилась из-за времени.
|
||||||
|
|
||||||
|
**Следующие шаги в production-среде (за пределами стенда):**
|
||||||
|
1. Переезд на Linux bare metal или Kubernetes — устранение накладных расходов виртуализованной сети.
|
||||||
|
2. PgBouncer между API и PostgreSQL — стандарт для high-volume write-нагрузки в финансовых системах.
|
||||||
|
3. Горизонтальный шардинг БД (TiDB, CockroachDB) — масштабирование записи за пределы одного узла.
|
||||||
|
4. Асинхронный `synchronous_commit` — только для некритичных операций; в проде изменяет RPO и недопустим без идемпотентного replay.
|
||||||
|
```
|
||||||
@@ -38,6 +38,7 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
|
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-cache'
|
implementation 'org.springframework.boot:spring-boot-starter-cache'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-aspectj'
|
implementation 'org.springframework.boot:spring-boot-starter-aspectj'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-jackson'
|
||||||
|
|
||||||
runtimeOnly 'io.netty:netty-transport-native-epoll::linux-x86_64'
|
runtimeOnly 'io.netty:netty-transport-native-epoll::linux-x86_64'
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
services:
|
||||||
|
loadgen:
|
||||||
|
image: python:3.12-slim
|
||||||
|
working_dir: /app
|
||||||
|
volumes:
|
||||||
|
- .:/app
|
||||||
|
networks:
|
||||||
|
- messenger-net
|
||||||
|
entrypoint: ["python", "tests/load.py"]
|
||||||
|
command:
|
||||||
|
- "--base-url"
|
||||||
|
- "http://lb:80"
|
||||||
|
- "--duration"
|
||||||
|
- "90"
|
||||||
|
- "--concurrency"
|
||||||
|
- "8"
|
||||||
|
- "--output"
|
||||||
|
- "evidence/lab3/load-rolling.json"
|
||||||
|
|
||||||
|
networks:
|
||||||
|
messenger-net:
|
||||||
|
external: true
|
||||||
|
name: lab3_messenger-net
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
api:
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '2.0' # Значение по умолчанию, будем менять
|
||||||
|
memory: 1G
|
||||||
+90
-12
@@ -1,7 +1,6 @@
|
|||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: messenger-db
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB_FILE: /run/secrets/postgres_db
|
POSTGRES_DB_FILE: /run/secrets/postgres_db
|
||||||
@@ -21,10 +20,17 @@ services:
|
|||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 10
|
retries: 10
|
||||||
start_period: 10s
|
start_period: 10s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '1.0'
|
||||||
|
memory: 1G
|
||||||
|
reservations:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7.4-alpine
|
image: redis:7.4-alpine
|
||||||
container_name: messenger-redis
|
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command: >
|
command: >
|
||||||
redis-server
|
redis-server
|
||||||
@@ -41,34 +47,85 @@ services:
|
|||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 10
|
retries: 10
|
||||||
start_period: 5s
|
start_period: 5s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 256M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.1'
|
||||||
|
memory: 64M
|
||||||
|
|
||||||
api:
|
migrate:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
container_name: messenger-api
|
entrypoint: ["sh", "-c"]
|
||||||
restart: unless-stopped
|
command:
|
||||||
depends_on:
|
- "exec java -Dfile.encoding=UTF-8 -Dspring.main.web-application-type=none -jar app.jar"
|
||||||
db:
|
|
||||||
condition: service_healthy
|
|
||||||
redis:
|
|
||||||
condition: service_healthy
|
|
||||||
environment:
|
environment:
|
||||||
|
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/messenger
|
||||||
|
SPRING_DATASOURCE_USERNAME_FILE: /run/secrets/postgres_user
|
||||||
|
SPRING_DATASOURCE_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
|
SPRING_FLYWAY_ENABLED: "true"
|
||||||
|
secrets:
|
||||||
|
- postgres_user
|
||||||
|
- postgres_password
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: "no"
|
||||||
|
networks:
|
||||||
|
- messenger-net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 512M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.1'
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
api:
|
||||||
|
stop_grace_period: 40s
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
entrypoint:
|
||||||
|
- "sh"
|
||||||
|
- "-c"
|
||||||
|
- "export INSTANCE_ID=\"$$(cat /etc/hostname)\"; exec java -Dfile.encoding=UTF-8 $$JAVA_OPTS -jar app.jar"
|
||||||
|
environment:
|
||||||
|
RATE_PER_MINUTE: ${RATE_PER_MINUTE:-100000}
|
||||||
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/messenger
|
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/messenger
|
||||||
SPRING_DATASOURCE_USERNAME_FILE: /run/secrets/postgres_user
|
SPRING_DATASOURCE_USERNAME_FILE: /run/secrets/postgres_user
|
||||||
SPRING_DATASOURCE_PASSWORD_FILE: /run/secrets/postgres_password
|
SPRING_DATASOURCE_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
SPRING_PROFILES_ACTIVE: ${APP_ENV:-lab}
|
SPRING_PROFILES_ACTIVE: ${APP_ENV:-lab}
|
||||||
|
SPRING_FLYWAY_ENABLED: "false"
|
||||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-400}
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-400}
|
||||||
SESSION_STORE: ${SESSION_STORE:-postgres}
|
SESSION_STORE: ${SESSION_STORE:-postgres}
|
||||||
SPRING_DATA_REDIS_HOST: redis
|
SPRING_DATA_REDIS_HOST: redis
|
||||||
SPRING_DATA_REDIS_PORT: 6379
|
SPRING_DATA_REDIS_PORT: 6379
|
||||||
ports:
|
|
||||||
- "127.0.0.1:8080:8080"
|
|
||||||
secrets:
|
secrets:
|
||||||
- postgres_user
|
- postgres_user
|
||||||
- postgres_password
|
- postgres_password
|
||||||
networks:
|
networks:
|
||||||
- messenger-net
|
- messenger-net
|
||||||
|
deploy:
|
||||||
|
replicas: 2
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '1.0'
|
||||||
|
memory: 512M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 256M
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/health/ready | grep -q '\"status\":\"ok\"'"]
|
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/health/ready | grep -q '\"status\":\"ok\"'"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
@@ -76,6 +133,27 @@ services:
|
|||||||
retries: 20
|
retries: 20
|
||||||
start_period: 40s
|
start_period: 40s
|
||||||
|
|
||||||
|
lb:
|
||||||
|
image: nginx:1.27-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:8080:80"
|
||||||
|
volumes:
|
||||||
|
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- messenger-net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.5'
|
||||||
|
memory: 128M
|
||||||
|
reservations:
|
||||||
|
cpus: '0.1'
|
||||||
|
memory: 32M
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
pgdata:
|
pgdata:
|
||||||
redisdata:
|
redisdata:
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.123,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 3628,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 60.343,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 105.429,
|
||||||
|
"p95": 301.881,
|
||||||
|
"p99": 492.908
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.141,
|
||||||
|
"concurrency": 32,
|
||||||
|
"successful_requests": 8229,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 136.829,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 196.007,
|
||||||
|
"p95": 520.87,
|
||||||
|
"p99": 697.232
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 20.0,
|
||||||
|
"elapsed_seconds": 20.022,
|
||||||
|
"concurrency": 2,
|
||||||
|
"successful_requests": 1849,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 92.349,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 17.978,
|
||||||
|
"p95": 39.728,
|
||||||
|
"p99": 57.86
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.13,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 8119,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 135.024,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 42.124,
|
||||||
|
"p95": 174.422,
|
||||||
|
"p99": 196.827
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.177,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 8545,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 141.997,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 36.182,
|
||||||
|
"p95": 171.914,
|
||||||
|
"p99": 197.476
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.089,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 8075,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 134.385,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 43.937,
|
||||||
|
"p95": 173.574,
|
||||||
|
"p99": 199.855
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 20.0,
|
||||||
|
"elapsed_seconds": 20.019,
|
||||||
|
"concurrency": 2,
|
||||||
|
"successful_requests": 2096,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 104.701,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 16.722,
|
||||||
|
"p95": 30.34,
|
||||||
|
"p99": 48.623
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.11,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 9299,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 154.701,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 41.105,
|
||||||
|
"p95": 107.021,
|
||||||
|
"p99": 185.487
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.052,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 6294,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 104.809,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 67.891,
|
||||||
|
"p95": 186.199,
|
||||||
|
"p99": 298.196
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 3,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 60.0,
|
||||||
|
"elapsed_seconds": 60.138,
|
||||||
|
"concurrency": 8,
|
||||||
|
"successful_requests": 3206,
|
||||||
|
"failed_requests": 8,
|
||||||
|
"error_ratio": 0.002489,
|
||||||
|
"successful_rps": 53.31,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 113.08,
|
||||||
|
"p95": 333.086,
|
||||||
|
"p99": 487.002
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
worker_processes auto;
|
||||||
|
|
||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
access_log /tmp/access.log;
|
||||||
|
error_log /tmp/error.log warn;
|
||||||
|
|
||||||
|
resolver 127.0.0.11 valid=10s ipv6=off;
|
||||||
|
resolver_timeout 5s;
|
||||||
|
|
||||||
|
upstream api_backend {
|
||||||
|
zone api_backend 64k;
|
||||||
|
server api:8080 resolve max_fails=1 fail_timeout=15s;
|
||||||
|
keepalive 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
client_max_body_size 1m;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Connection "";
|
||||||
|
proxy_set_header Host $http_host;
|
||||||
|
proxy_set_header X-Forwarded-Host $http_host;
|
||||||
|
proxy_set_header X-Forwarded-Port $server_port;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||||
|
proxy_next_upstream_tries 3;
|
||||||
|
|
||||||
|
proxy_connect_timeout 1s;
|
||||||
|
proxy_send_timeout 10s;
|
||||||
|
proxy_read_timeout 10s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
@echo off
|
||||||
|
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1"
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
docker compose --env-file .env restart db redis api lb
|
||||||
Executable
+7
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
# Restart API and state stores. Volumes are untouched -> data, sessions
|
||||||
|
# and idempotency keys survive.
|
||||||
|
# migrate is one-shot (exited 0) and is intentionally not restarted here.
|
||||||
|
docker compose --env-file .env restart db redis api lb
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
@echo off
|
||||||
|
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1"
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
if (-not $env:TARGET_INSTANCE_ID) {
|
||||||
|
Write-Error "TARGET_INSTANCE_ID is not set"
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = $env:TARGET_INSTANCE_ID
|
||||||
|
$logDir = "evidence\lab3"
|
||||||
|
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
|
||||||
|
|
||||||
|
docker stop $id | Out-Null
|
||||||
|
docker logs $id 2>&1 | Out-File -Encoding utf8 "$logDir\api-$id.log"
|
||||||
|
Write-Host "saved $logDir\api-$id.log"
|
||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||||
|
|
||||||
|
# X-Instance-Id = short container ID (lab3 implementation detail).
|
||||||
|
# docker stop resolves short IDs natively.
|
||||||
|
docker stop "$TARGET_INSTANCE_ID"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.mtusi.messenger.config.properties;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "app.idempotency")
|
||||||
|
public record IdempotencyProperties(
|
||||||
|
int keyMaxLength,
|
||||||
|
String keyPattern,
|
||||||
|
int retentionHours
|
||||||
|
) {}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package ru.mtusi.messenger.config.properties;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "app.instance")
|
||||||
|
public record InstanceProperties(String id) {
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package ru.mtusi.messenger.controller;
|
package ru.mtusi.messenger.controller;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import ru.mtusi.messenger.dto.SessionDTO;
|
import ru.mtusi.messenger.dto.SessionDTO;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
@@ -69,7 +70,7 @@ public interface AuthApi {
|
|||||||
)
|
)
|
||||||
|
|
||||||
ResponseEntity<SessionDTO> createSession(
|
ResponseEntity<SessionDTO> createSession(
|
||||||
|
HttpServletRequest request
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,9 @@ import ru.mtusi.messenger.service.AuthService;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AuthController implements AuthApi {
|
public class AuthController implements AuthApi {
|
||||||
private final AuthService authService;
|
private final AuthService authService;
|
||||||
private final HttpServletRequest request;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<SessionDTO> createSession() {
|
public ResponseEntity<SessionDTO> createSession(HttpServletRequest request) {
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(authService.login(request.getHeader(HttpHeaders.AUTHORIZATION)));
|
return ResponseEntity.status(HttpStatus.CREATED).body(authService.login(request.getHeader(HttpHeaders.AUTHORIZATION)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import ru.mtusi.messenger.service.HealthService;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class HealthController implements HealthApi {
|
public class HealthController implements HealthApi {
|
||||||
|
|
||||||
private final HealthService healthService;
|
private final HealthService healthService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ public class MessageController implements MessagesApi {
|
|||||||
private final MessageService messageService;
|
private final MessageService messageService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<MessageDTO> createMessage(UUID chatId, CreateMessageDTO createMessageDTO, @AuthenticationPrincipal SessionPrincipal principal) {
|
public ResponseEntity<MessageDTO> createMessage(UUID chatId, String idempotencyKey, CreateMessageDTO createMessageDTO, @AuthenticationPrincipal SessionPrincipal principal) {
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(messageService.createMessage(principal.userId(), chatId, createMessageDTO));
|
return ResponseEntity.status(HttpStatus.CREATED).body(messageService.createMessage(principal.userId(), chatId, createMessageDTO, idempotencyKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -78,6 +78,8 @@ public interface MessagesApi {
|
|||||||
ResponseEntity<MessageDTO> createMessage(
|
ResponseEntity<MessageDTO> createMessage(
|
||||||
@Parameter(name = "chat_id", description = "", required = true, in = ParameterIn.PATH)
|
@Parameter(name = "chat_id", description = "", required = true, in = ParameterIn.PATH)
|
||||||
@PathVariable("chat_id") UUID chatId,
|
@PathVariable("chat_id") UUID chatId,
|
||||||
|
@Parameter(name = "Idempotency-Key", required = true, in = ParameterIn.HEADER)
|
||||||
|
@RequestHeader(value = "Idempotency-Key", required = true) String idempotencyKey,
|
||||||
@Parameter(name = "CreateMessageDTO", description = "", required = true)
|
@Parameter(name = "CreateMessageDTO", description = "", required = true)
|
||||||
@Valid @RequestBody CreateMessageDTO createMessageDTO,
|
@Valid @RequestBody CreateMessageDTO createMessageDTO,
|
||||||
@AuthenticationPrincipal SessionPrincipal principal
|
@AuthenticationPrincipal SessionPrincipal principal
|
||||||
|
|||||||
+7
@@ -12,6 +12,7 @@ import org.springframework.transaction.CannotCreateTransactionException;
|
|||||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.MissingRequestHeaderException;
|
||||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
@@ -138,6 +139,12 @@ public class GlobalExceptionHandler {
|
|||||||
"Database error", req);
|
"Database error", req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MissingRequestHeaderException.class)
|
||||||
|
public ResponseEntity<ErrorDTO> missingHeader(MissingRequestHeaderException e, HttpServletRequest req) {
|
||||||
|
return respond(HttpStatus.BAD_REQUEST, ErrorErrorDTO.CodeEnum.INVALID_REQUEST,
|
||||||
|
"Missing header: " + e.getHeaderName(), req);
|
||||||
|
}
|
||||||
|
|
||||||
private ResponseEntity<ErrorDTO> respond(HttpStatus status,
|
private ResponseEntity<ErrorDTO> respond(HttpStatus status,
|
||||||
ErrorErrorDTO.CodeEnum code,
|
ErrorErrorDTO.CodeEnum code,
|
||||||
String message,
|
String message,
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package ru.mtusi.messenger.filter;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
import ru.mtusi.messenger.config.properties.InstanceProperties;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class InstanceIdFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
private final InstanceProperties props;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response,
|
||||||
|
FilterChain chain) throws ServletException, IOException {
|
||||||
|
response.setHeader("X-Instance-Id", props.id());
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import jakarta.servlet.ServletException;
|
|||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
@@ -13,17 +14,14 @@ import java.io.IOException;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Order(1)
|
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
|
||||||
public class RequestIdFilter extends OncePerRequestFilter {
|
public class RequestIdFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
public static final String INSTANCE_ID = UUID.randomUUID().toString();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
|
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
|
||||||
FilterChain chain) throws ServletException, IOException {
|
FilterChain chain) throws ServletException, IOException {
|
||||||
String rid = UUID.randomUUID().toString();
|
String rid = UUID.randomUUID().toString();
|
||||||
res.setHeader("X-Request-Id", rid);
|
res.setHeader("X-Request-Id", rid);
|
||||||
res.setHeader("X-Instance-Id", INSTANCE_ID);
|
|
||||||
req.setAttribute("requestId", rid);
|
req.setAttribute("requestId", rid);
|
||||||
MDC.put("requestId", rid);
|
MDC.put("requestId", rid);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package ru.mtusi.messenger.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.hibernate.annotations.JdbcTypeCode;
|
||||||
|
import org.hibernate.type.SqlTypes;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "idempotency_keys")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class IdempotencyKey {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@Column(nullable = false, updatable = false)
|
||||||
|
@GeneratedValue(strategy = GenerationType.UUID)
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column(name = "user_id", nullable = false)
|
||||||
|
private UUID userId;
|
||||||
|
|
||||||
|
@Column(nullable = false, length = 16)
|
||||||
|
private String method;
|
||||||
|
|
||||||
|
@Column(name = "chat_id", nullable = false)
|
||||||
|
private UUID chatId;
|
||||||
|
|
||||||
|
@Column(name = "idempotency_key", nullable = false, length = 128)
|
||||||
|
private String idempotencyKey;
|
||||||
|
|
||||||
|
@Column(name = "request_hash", nullable = false, length = 64)
|
||||||
|
private String requestHash;
|
||||||
|
|
||||||
|
@Column(name = "status_code")
|
||||||
|
private Short statusCode;
|
||||||
|
|
||||||
|
@JdbcTypeCode(SqlTypes.JSON)
|
||||||
|
@Column(name = "response_body", columnDefinition = "jsonb")
|
||||||
|
private String responseBody;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package ru.mtusi.messenger.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import ru.mtusi.messenger.model.IdempotencyKey;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface IdempotencyRepository extends JpaRepository<IdempotencyKey, UUID> {
|
||||||
|
|
||||||
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
|
@Query(value = """
|
||||||
|
INSERT INTO idempotency_keys
|
||||||
|
(user_id, method, chat_id, idempotency_key, request_hash, created_at)
|
||||||
|
VALUES (:userId, :method, :chatId, :key, :hash, NOW())
|
||||||
|
ON CONFLICT (user_id, method, chat_id, idempotency_key) DO NOTHING
|
||||||
|
""", nativeQuery = true)
|
||||||
|
int tryClaim(UUID userId, String method, UUID chatId, String key, String hash);
|
||||||
|
|
||||||
|
@Modifying(clearAutomatically = true, flushAutomatically = true)
|
||||||
|
@Query(value = """
|
||||||
|
UPDATE idempotency_keys
|
||||||
|
SET status_code = :statusCode,
|
||||||
|
response_body = CAST(:body AS jsonb)
|
||||||
|
WHERE user_id = :userId
|
||||||
|
AND method = :method
|
||||||
|
AND chat_id = :chatId
|
||||||
|
AND idempotency_key = :key
|
||||||
|
""", nativeQuery = true)
|
||||||
|
int commitResponse(UUID userId, String method, UUID chatId, String key,
|
||||||
|
int statusCode, String body);
|
||||||
|
|
||||||
|
Optional<IdempotencyKey> findByUserIdAndMethodAndChatIdAndIdempotencyKey(
|
||||||
|
UUID userId, String method, UUID chatId, String idempotencyKey);
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package ru.mtusi.messenger.service;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.mtusi.messenger.exception.ConflictException;
|
||||||
|
import ru.mtusi.messenger.model.IdempotencyKey;
|
||||||
|
import ru.mtusi.messenger.repository.IdempotencyRepository;
|
||||||
|
import ru.mtusi.messenger.util.converter.IdempotencyPayloadConverter;
|
||||||
|
import ru.mtusi.messenger.validation.IdempotencyValidator;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class IdempotencyService {
|
||||||
|
|
||||||
|
private final IdempotencyRepository idempotencyRepository;
|
||||||
|
private final IdempotencyPayloadConverter converter;
|
||||||
|
private final IdempotencyValidator validator;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public <T> T execute(UUID userId, String method, UUID chatId, String key,
|
||||||
|
Object requestBody, Class<T> responseType, Supplier<T> action) {
|
||||||
|
validator.validateKey(key);
|
||||||
|
String hash = hashRequest(requestBody);
|
||||||
|
|
||||||
|
int claimed = idempotencyRepository.tryClaim(userId, method, chatId, key, hash);
|
||||||
|
if (claimed == 0) {
|
||||||
|
return replay(userId, method, chatId, key, hash, responseType);
|
||||||
|
}
|
||||||
|
|
||||||
|
T result = action.get();
|
||||||
|
idempotencyRepository.commitResponse(userId, method, chatId, key, 201, converter.toJson(result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T replay(UUID userId, String method, UUID chatId, String key,
|
||||||
|
String hash, Class<T> responseType) {
|
||||||
|
IdempotencyKey stored = idempotencyRepository
|
||||||
|
.findByUserIdAndMethodAndChatIdAndIdempotencyKey(userId, method, chatId, key)
|
||||||
|
.orElseThrow(() -> new IllegalStateException("Idempotency row not visible after conflict"));
|
||||||
|
if (!stored.getRequestHash().equals(hash))
|
||||||
|
throw new ConflictException("Idempotency-Key reused with a different request body");
|
||||||
|
if (stored.getStatusCode() == null || stored.getStatusCode() != 201)
|
||||||
|
throw new ConflictException("Idempotency-Key has unexpected status");
|
||||||
|
return converter.fromJson(stored.getResponseBody(), responseType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hashRequest(Object body) {
|
||||||
|
try {
|
||||||
|
byte[] digest = MessageDigest.getInstance("SHA-256")
|
||||||
|
.digest(converter.toJson(body).getBytes(StandardCharsets.UTF_8));
|
||||||
|
return HexFormat.of().formatHex(digest);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Cannot hash request body", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,13 +13,13 @@ import ru.mtusi.messenger.dto.MessagePageDTO;
|
|||||||
import ru.mtusi.messenger.exception.InvalidRequestException;
|
import ru.mtusi.messenger.exception.InvalidRequestException;
|
||||||
import ru.mtusi.messenger.exception.NotFoundException;
|
import ru.mtusi.messenger.exception.NotFoundException;
|
||||||
import ru.mtusi.messenger.exception.PayloadTooLargeException;
|
import ru.mtusi.messenger.exception.PayloadTooLargeException;
|
||||||
import ru.mtusi.messenger.mapper.ChatMapper;
|
|
||||||
import ru.mtusi.messenger.mapper.MessageMapper;
|
import ru.mtusi.messenger.mapper.MessageMapper;
|
||||||
import ru.mtusi.messenger.model.Chat;
|
import ru.mtusi.messenger.model.Chat;
|
||||||
import ru.mtusi.messenger.model.Message;
|
import ru.mtusi.messenger.model.Message;
|
||||||
import ru.mtusi.messenger.model.User;
|
import ru.mtusi.messenger.model.User;
|
||||||
import ru.mtusi.messenger.repository.MessageRepository;
|
import ru.mtusi.messenger.repository.MessageRepository;
|
||||||
import ru.mtusi.messenger.util.CursorCodec;
|
import ru.mtusi.messenger.util.CursorCodec;
|
||||||
|
import ru.mtusi.messenger.service.IdempotencyService;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -28,7 +28,7 @@ import java.util.UUID;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class MessageService {
|
public class MessageService {
|
||||||
private final ChatMapper chatMapper;
|
private final IdempotencyService idempotencyService;
|
||||||
private final MessageRepository messageRepository;
|
private final MessageRepository messageRepository;
|
||||||
private final ChatService chatService;
|
private final ChatService chatService;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
@@ -36,17 +36,22 @@ public class MessageService {
|
|||||||
private final AppProperties appProperties;
|
private final AppProperties appProperties;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public MessageDTO createMessage(UUID senderId, UUID chatId, CreateMessageDTO createMessageDTO) {
|
public MessageDTO createMessage(UUID senderId, UUID chatId, CreateMessageDTO dto, String idempotencyKey) {
|
||||||
Chat chat = chatService.getChat(senderId, chatId);;
|
Chat chat = chatService.getChat(senderId, chatId);
|
||||||
validateCreate(chatId, createMessageDTO);
|
|
||||||
|
return idempotencyService.execute(senderId, "POST", chatId, idempotencyKey, dto, MessageDTO.class,
|
||||||
|
() -> doCreate(senderId, chatId, dto, chat));
|
||||||
|
}
|
||||||
|
|
||||||
|
private MessageDTO doCreate(UUID senderId, UUID chatId, CreateMessageDTO dto, Chat chat) {
|
||||||
|
validateCreate(chatId, dto);
|
||||||
User sender = userService.getReferenceUserById(senderId);
|
User sender = userService.getReferenceUserById(senderId);
|
||||||
Message message = buildMessage(createMessageDTO, sender, chat);
|
Message message = buildMessage(dto, sender, chat);
|
||||||
try {
|
try {
|
||||||
message = messageRepository.saveAndFlush(message);
|
message = messageRepository.saveAndFlush(message);
|
||||||
} catch (DataIntegrityViolationException e) {
|
} catch (DataIntegrityViolationException e) {
|
||||||
throw new NotFoundException("Cannot create message: referenced entity is missing");
|
throw new NotFoundException("Cannot create message: referenced entity is missing");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("Created message id={} in chat={}", message.getId(), chatId);
|
log.info("Created message id={} in chat={}", message.getId(), chatId);
|
||||||
return messageMapper.toMessageDTO(message);
|
return messageMapper.toMessageDTO(message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package ru.mtusi.messenger.util.converter;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import tools.jackson.core.JacksonException;
|
||||||
|
import tools.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class IdempotencyPayloadConverter {
|
||||||
|
|
||||||
|
private final JsonMapper jsonMapper;
|
||||||
|
|
||||||
|
public String toJson(Object payload) {
|
||||||
|
try {
|
||||||
|
return jsonMapper.writeValueAsString(payload);
|
||||||
|
} catch (JacksonException e) { // Исключение теперь непроверяемое
|
||||||
|
throw new IllegalStateException("Cannot serialize idempotency payload", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T fromJson(String json, Class<T> type) {
|
||||||
|
try {
|
||||||
|
return jsonMapper.readValue(json, type);
|
||||||
|
} catch (JacksonException e) {
|
||||||
|
throw new IllegalStateException("Corrupt idempotency payload", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package ru.mtusi.messenger.validation;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import ru.mtusi.messenger.config.properties.IdempotencyProperties;
|
||||||
|
import ru.mtusi.messenger.exception.InvalidRequestException;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class IdempotencyValidator {
|
||||||
|
|
||||||
|
private final IdempotencyProperties properties;
|
||||||
|
|
||||||
|
public void validateKey(String key) {
|
||||||
|
requireNotBlank(key);
|
||||||
|
requireMaxLength(key);
|
||||||
|
requirePattern(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requireNotBlank(String key) {
|
||||||
|
if (key == null || key.isEmpty()) {
|
||||||
|
throw new InvalidRequestException("Idempotency-Key header is required");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requireMaxLength(String key) {
|
||||||
|
if (key.length() > properties.keyMaxLength()) {
|
||||||
|
throw new InvalidRequestException("Idempotency-Key is too long");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requirePattern(String key) {
|
||||||
|
if (!Pattern.matches(properties.keyPattern(), key)) {
|
||||||
|
throw new InvalidRequestException("Idempotency-Key contains invalid characters");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ spring:
|
|||||||
config:
|
config:
|
||||||
import: "optional:configtree:/run/secrets/"
|
import: "optional:configtree:/run/secrets/"
|
||||||
lifecycle:
|
lifecycle:
|
||||||
timeout-per-shutdown-phase: 10s
|
timeout-per-shutdown-phase: 30s
|
||||||
jackson:
|
jackson:
|
||||||
default-property-inclusion: non_null
|
default-property-inclusion: non_null
|
||||||
deserialization:
|
deserialization:
|
||||||
@@ -18,7 +18,7 @@ spring:
|
|||||||
connection-timeout: 2000
|
connection-timeout: 2000
|
||||||
validation-timeout: 1000
|
validation-timeout: 1000
|
||||||
maximum-pool-size: 10
|
maximum-pool-size: 10
|
||||||
minimum-idle: 2
|
minimum-idle: 10
|
||||||
idle-timeout: 60000
|
idle-timeout: 60000
|
||||||
max-lifetime: 1800000
|
max-lifetime: 1800000
|
||||||
jpa:
|
jpa:
|
||||||
@@ -60,6 +60,12 @@ management:
|
|||||||
|
|
||||||
|
|
||||||
app:
|
app:
|
||||||
|
idempotency:
|
||||||
|
key-max-length: ${IDEMPOTENCY_KEY_MAX_LENGTH:128}
|
||||||
|
key-pattern: '${IDEMPOTENCY_KEY_PATTERN:^[A-Za-z0-9._:-]+$}'
|
||||||
|
retention-hours: ${IDEMPOTENCY_RETENTION_HOURS:24}
|
||||||
|
instance:
|
||||||
|
id: ${INSTANCE_ID:${HOSTNAME:${random.uuid}}}
|
||||||
limits:
|
limits:
|
||||||
max-messages-per-chat: ${MAX_MESSAGES_PER_CHAT:10000}
|
max-messages-per-chat: ${MAX_MESSAGES_PER_CHAT:10000}
|
||||||
rate:
|
rate:
|
||||||
|
|||||||
@@ -52,9 +52,21 @@ CREATE TABLE sessions (
|
|||||||
CONSTRAINT ck_sessions_token_len CHECK (octet_length(token_hash) = 32)
|
CONSTRAINT ck_sessions_token_len CHECK (octet_length(token_hash) = 32)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- =========================================================
|
-- ---------- idempotency_keys (lab3) ----------
|
||||||
-- Indexes (critical for keyset pagination & EXPLAIN on lab5)
|
CREATE TABLE idempotency_keys (
|
||||||
-- =========================================================
|
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
method VARCHAR(16) NOT NULL,
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
idempotency_key VARCHAR(128) NOT NULL,
|
||||||
|
request_hash VARCHAR(64) NOT NULL,
|
||||||
|
status_code SMALLINT,
|
||||||
|
response_body JSONB,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
CONSTRAINT uq_idempotency_scope UNIQUE (user_id, method, chat_id, idempotency_key),
|
||||||
|
CONSTRAINT ck_idem_key_format CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||||
|
CONSTRAINT ck_idem_status_body_paired CHECK ((status_code IS NULL) = (response_body IS NULL))
|
||||||
|
);
|
||||||
|
|
||||||
-- Keyset pagination: ORDER BY created_at ASC, id ASC (JPQL uses ASC)
|
-- Keyset pagination: ORDER BY created_at ASC, id ASC (JPQL uses ASC)
|
||||||
CREATE INDEX idx_messages_chat_created_asc_id
|
CREATE INDEX idx_messages_chat_created_asc_id
|
||||||
@@ -77,3 +89,6 @@ CREATE INDEX idx_sessions_user_active
|
|||||||
CREATE INDEX idx_sessions_expires_active
|
CREATE INDEX idx_sessions_expires_active
|
||||||
ON sessions (expires_at)
|
ON sessions (expires_at)
|
||||||
WHERE revoked_at IS NULL;
|
WHERE revoked_at IS NULL;
|
||||||
|
|
||||||
|
CREATE INDEX idx_idempotency_created
|
||||||
|
ON idempotency_keys (created_at);
|
||||||
Reference in New Issue
Block a user