ЛР 3 — БВТ2303 — Кучерявенко — на 4 #40

Closed
Mraksimus wants to merge 4 commits from Mraksimus/mtusi2026lab3-fork:solution/bvt2303-mraksimus into main
198 changed files with 8620 additions and 14 deletions
+18
View File
@@ -18,3 +18,21 @@ __pycache__
node_modules
evidence
.DS_Store
.gradle
.kotlin
build
.idea
*.iml
tests
scripts
examples
run
COURSE.md
CONTRIBUTING.md
MAINTAINERS.md
REPORT.md
README.md
requirements-dev.txt
Dockerfile.example
compose.example.yaml
Makefile
+4
View File
@@ -28,5 +28,9 @@ coverage/
.DS_Store
.idea/
.vscode/
.gradle/
.kotlin/
!gradle/wrapper/
/run/development/
# Keep small, sanitized evidence summaries in evidence/ under version control.
# Inspect git diff --cached: ignore rules are not a secret scanner.
+23
View File
@@ -0,0 +1,23 @@
FROM gradle:8.14-jdk21 AS build
WORKDIR /src
COPY settings.gradle.kts build.gradle.kts gradle.properties ./
COPY gradle ./gradle
COPY gradlew ./
COPY src ./src
COPY contracts ./contracts
RUN --mount=type=cache,target=/home/gradle/.gradle gradle --no-daemon shadowJar
FROM eclipse-temurin:21-jre-alpine
# Distinguishes builds of the same code, e.g. the image rolled out by scripts/rolling-update.sh.
ARG BUILD_ID=dev
LABEL org.opencontainers.image.revision=$BUILD_ID
RUN apk add --no-cache wget && addgroup -S app && adduser -S -G app app
WORKDIR /app
COPY --from=build /src/build/libs/messenger.jar /app/messenger.jar
USER app
ENV APP_HOST=0.0.0.0 APP_PORT=8080
EXPOSE 8080
# Docker's own view of the replica (compose depends_on, rolling update). The balancer runs its own checks every 1 s.
HEALTHCHECK --interval=10s --timeout=3s --start-period=30s --start-interval=1s --retries=3 \
CMD wget -qO- http://127.0.0.1:8080/health/ready || exit 1
ENTRYPOINT ["java","-XX:MaxRAMPercentage=75.0","-jar","/app/messenger.jar"]
+187 -14
View File
@@ -1,37 +1,210 @@
# Отчёт — лабораторная 3
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Автор, группа: Кучерявенко Максим, БВТ2303
- Целевая оценка; выбранный трек 5, если нужен: 4
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
- Использованные источники, библиотеки и помощь ИИ: всё из lab2 (Kotlin 2.3.20, Ktor 3.3.0, Koin 4.1, Exposed 1.0, HikariCP, Flyway 11.9, Lettuce 6.7, BouncyCastle, Testcontainers) плюс Ktor CORS. Балансировщик HAProxy 3.2.4, нагрузка k6 2.3. Документация: [Compose services](https://docs.docker.com/reference/compose-file/services/), [HAProxy 3.2 configuration manual](https://docs.haproxy.org/3.2/configuration.html) (server-template, resolvers, httpchk, retries/redispatch), [PostgreSQL INSERT … ON CONFLICT](https://www.postgresql.org/docs/17/sql-insert.html). ИИ (Claude) использовал как парного разработчика.
## Запуск из чистого clone
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
Нужны Docker 24+ с Compose v2, Python 3.9+ (публичные тесты), `openssl` (секреты). Для `./gradlew test` ещё JDK 21+ и запущенный Docker, для нагрузочных прогонов — k6. Я всё запускал на Docker 28.4, Compose 2.39, JDK 24, k6 2.3.
```sh
# один раз: пароли PostgreSQL и Redis в secrets/ (папка в .gitignore, существующие файлы не перезаписываются)
./scripts/init-secrets.sh
# сборка образа и запуск: postgres, redis -> migrate (один раз применяет миграции и выходит) -> api x2 -> lb
# --wait ждёт healthcheck всех сервисов и успешного завершения migrate
docker compose up -d --build --wait
# публичные проверки, всё через балансировщик http://localhost:8080
make check
make test
make test-persistence ACTION_SCRIPT=scripts/restart.sh
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
docker compose start api # вернуть остановленную реплику
# свои тесты (Testcontainers)
./gradlew test
# одновременные повторы одного Idempotency-Key через обе реплики
python3 scripts/idempotency-race.py
# отзыв токена при паузе записи в Redis (сценарий из замечания на PR): 204 через ~5 с, потом 401 от обеих реплик
PAUSE_MS=5000 scripts/revocation-pause.sh
PAUSE_MS=12000 scripts/revocation-pause.sh # дольше срока logout: 503, токен всё равно 401
# уровень 4: отказ каждой реплики под нагрузкой и rolling update (нужен k6)
MODE=stop TARGET=api-1 scripts/load/failover-under-load.sh # также MODE=kill, TARGET=api-2
scripts/load/rolling-under-load.sh # сам update без нагрузки: scripts/rolling-update.sh
# остановка с сохранением данных
docker compose stop # или docker compose down, volume postgres-data остаётся
# намеренное удаление всех учебных данных
docker compose down -v
```
`.env` не обязателен: у всех переменных в `compose.yaml` есть значения по умолчанию. `docker compose --env-file .env.example config --quiet` проходит.
`make test-failover` останавливает ту реплику, которая ответила на первое сообщение теста. Тест делает одинаковое число запросов, и round robin каждый раз приводит его к одной и той же реплике. Чтобы проверить вторую, перед повторным запуском я делаю один лишний запрос (`curl -s localhost:8080/health/live`), он сдвигает очередь.
После `scripts/rolling-update.sh` контейнеры называются `api-3`, `api-4` (Compose нумерует новые дальше). `stop-one.sh` от имён не зависит. `failover-under-load.sh` принимает имя (`TARGET=api-3`), прежние имена возвращает `docker compose down && docker compose up -d --wait` без `-v`.
Локально без Docker: `./gradlew run`, конфиг из `run/development/config/*.conf`, порт 8081, один процесс сам применяет миграции.
## Архитектура и решения
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
```
клиент ──HTTP──▶ lb: HAProxy 3.2.4 (127.0.0.1:8080, единственный опубликованный порт)
│ round robin, проверка GET /health/ready раз в 1 с, адреса реплик из Docker DNS
├──▶ api-1 (Ktor) ─┐
└──▶ api-2 (Ktor) ─┼──JDBC, пул ≤10 на реплику──▶ postgres (volume postgres-data) ← источник истины
└──RESP──▶ redis (кэш сессий, без persistence)
migrate (тот же образ, `migrate`) ── один раз до старта реплик ──▶ postgres
```
| Роль | Service | Образ | Health/readiness | Volume | Порт | Credentials | Лимиты |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Балансировщик | `lb` | `haproxy:3.2.4-alpine` | `wget http://127.0.0.1:8404/healthz` (внутренний порт) | — | `127.0.0.1:8080` | нет | 0.5 CPU, 128 МБ |
| API ×2 | `api` | `mtusi-messenger:lab3` (свой Dockerfile) | Docker: `/health/ready` раз в 10 с; HAProxy: `/health/ready` раз в 1 с | — | 8080 только в сети compose | secrets `db_password`, `redis_password` | 1 CPU, 512 МБ каждая |
| Миграции | `migrate` | тот же образ, команда `migrate` | разовый, healthcheck выключен | — | — | `db_password` | 0.5 CPU, 384 МБ |
| БД | `postgres` | `postgres:17.6-alpine3.22` | `pg_isready` | `postgres-data` | 5432 в сети | `db_password` | 1 CPU, 1 ГБ |
| Кэш сессий | `redis` | `redis:8.4.0-alpine` | `redis-cli ping` | — | 6379 в сети | `redis_password` | 0.5 CPU, 128 МБ |
**Что перенёс из lab2 без изменений.** Весь API и контракт, PostgreSQL как источник истины, Bearer-сессии в PostgreSQL с кэшем в Redis (single-flight, tombstone при logout, outbox `session_revocations`), курсорную пагинацию, Argon2id, Docker secrets, 503 при недоступной базе. У API и раньше не было состояния в памяти, от которого зависит корректность: сессии в PostgreSQL, кэш в общем Redis, курсор пагинации не подписан ключом процесса. Поэтому реплики взаимозаменяемы без доработок.
**Что поменял в этой работе.**
- **`X-Instance-Id`** (`plugins/InstanceId.kt`) — в каждом ответе API, включая ошибки и health. Значение: `INSTANCE_ID`, если задан, иначе `HOSTNAME` (в Docker это короткий id контейнера), иначе случайная строка. Задаётся при старте процесса и не меняется. Благодаря тому, что это id контейнера, `stop-one.sh` находит нужный контейнер без таблицы соответствий.
- **`Idempotency-Key`** на `POST /chats/{id}/messages`, ключ обязателен, формат `[A-Za-z0-9._:-]{1,128}`, иначе 400. Таблица `message_idempotency_keys` (миграция `V3`) с первичным ключом `(user_id, chat_id, key)` — это область уникальности из контракта: метод и путь у этого запроса фиксированы, остаются пользователь, чат и ключ. В той же транзакции, что и сообщение:
1. Проверка членства в чате, в том числе при повторе: постороннему — 404.
2. `INSERT … ON CONFLICT DO NOTHING` занимает ключ.
3. Ключ свободен — создаём сообщение и записываем его id в строку ключа.
4. Ключ занят — тот же SHA-256 тела даёт то же сообщение с 201, другой — 409.
5. Ключ старше `IDEMPOTENCY_TTL_SECONDS` (86400) удаляется и занимается заново.
Если тот же ключ сейчас вставляет другая транзакция (другая реплика), PostgreSQL на шаге 2 ждёт её конца. Поэтому одновременные повторы создают одно сообщение. При ошибке 5xx транзакция откатывается вместе с ключом, и неудачная попытка не запоминается как результат.
- **Миграции отдельно от реплик.** `java -jar messenger.jar migrate` применяет Flyway и выходит (main-класс jar теперь `AppKt`, а не `EngineMain`). В compose это сервис `migrate`. Реплики запускаются с `DB_MIGRATE_ON_START=false` после его успешного завершения (`service_completed_successfully`) и делают только `flyway.validate()`: при неприменённой миграции реплика не стартует. Так реплики не устраивают гонку за схему. Локальный процесс и тесты по-прежнему мигрируют сами.
- **Балансировщик HAProxy** (`infra/haproxy/haproxy.cfg`):
- адреса реплик берутся из Docker DNS (`server-template api 4 api:8080`, resolvers `127.0.0.11`, `hold` 2 с), так что масштабирование и замена контейнеров не требуют правки конфига;
- активная проверка `GET /health/ready`: `inter 1s fall 2 rise 2`;
- `balance roundrobin`;
- `retries 2` + `option redispatch` — только при ошибке соединения, то есть до отправки запроса. POST, который дошёл до реплики, балансировщик не повторяет;
- `maxconn 4096` в `global` (без него HAProxy занимал ~100 МБ из 128);
- внутренний frontend `:8404` с `/healthz` и `/stats` наружу не опубликован.
Sticky sessions нет.
- **Плавная остановка** (`plugins/GracefulShutdown.kt`). По SIGTERM `/health/ready` сразу отвечает 503, процесс ещё 3 с (`SHUTDOWN_DRAIN_DELAY_MS`) обслуживает запросы. За это время HAProxy выводит реплику из ротации. Потом Ktor закрывает сокет и дожидается начатых запросов (`shutdownGracePeriod` 1 с, `shutdownTimeout` 5 с). Задержка сделана в обработчике `ApplicationStopPreparing`: Ktor вызывает его синхронно до закрытия каналов. До этой правки (на уровне 3) каждая остановка давала один 502 `SH--`: реплика закрывала соединение посреди POST.
- **Rolling update** (`scripts/rolling-update.sh`):
1. Собирает образ с `BUILD_ID` (Dockerfile `ARG` + `LABEL`, чтобы новая версия отличалась даже при том же коде).
2. Один раз применяет миграции: `docker compose run --rm migrate`.
3. Для каждой старой реплики: `up --no-recreate --scale api=N+1` поднимает новую, скрипт ждёт `healthy` и состояние UP в `/stats` HAProxy, затем `docker stop` (с drain) и `docker rm` старой.
Работающих реплик ни в какой момент не меньше двух.
- **Отзыв сессии виден всем репликам.** По замечанию на PR. Раньше logout отвечал 204 сразу после удаления сессии в PostgreSQL, даже если метку `revoked` не удалось записать в Redis. Признак «кэшу нельзя верить» после сбоя Redis жил в памяти только той реплики, которая сама увидела ошибку. Воспроизведение: `CLIENT PAUSE 5000 WRITE` (чтения из Redis идут, запись метки зависает), logout через LB — 204. После этого реплика, обработавшая logout, давала 401, а вторая — 200 на чтение и 201 на повтор сообщения. Теперь в `DefaultAuthenticationService`:
1. **204 только после записи метки.** Logout по-прежнему сначала удаляет сессию и пишет строку в `session_revocations` (одна транзакция). Затем пишет `revoked` в Redis и повторяет попытку каждые 250 мс до `SESSION_REVOKE_TIMEOUT_MS` (8 с, меньше `timeout server` HAProxy). Получилось — строка удаляется, ответ 204: метка уже в общем Redis, и любая реплика при чтении кэша видит `revoked`. Не получилось за 8 с — ответ 503 `unavailable`: logout не подтверждён.
2. **Общий сигнал через PostgreSQL** (`plugins/SessionRevocationSync.kt`). Каждая реплика раз в секунду проверяет `session_revocations`. Строка там значит, что какой-то logout ещё не дописал метку. Реплика тогда перестаёт доверять кэшу: сессии читаются из базы, где сессии уже нет, а значит 401. Как только Redis принимает запись, недописанные метки дописывает любая реплика. Это закрывает и случай с 503, и сбой Redis во время logout на другой реплике.
Цена решения: если Redis недоступен на запись дольше 8 с, logout отвечает 503 вместо 204. В lab2 при остановленном Redis logout отвечал 204. Это сознательный выбор: 204 обещает, что токен больше нигде не примут, а без записи в общий кэш это обещание проверить нельзя.
- **Ресурсы.** CPU/RAM-лимиты у всех контейнеров, `container_name` убран у всех, порт опубликован только у `lb` и только на `127.0.0.1`.
- **CORS** (`plugins/Cors.kt`, `config/cors/`). Нужен для Swagger UI на `/docs`, открытого с другого адреса (например, dev-порт 8081). Разрешены только `localhost`/`127.0.0.1:8080/8081`, наружу отдаются `X-Request-Id`, `X-Instance-Id`, `WWW-Authenticate`. На API без браузера не влияет.
**Транзакционные границы.** Отправка сообщения: проверка членства, ключ идемпотентности и само сообщение — одна транзакция PostgreSQL. Ключ без сообщения снаружи не виден (проверено запросом: 0 строк с `message_id IS NULL`). Остальное как в lab2.
**Совместимость миграций с работающей версией.** Схема меняется только расширением. `V3` добавляет таблицу и индексы, старая версия кода её не трогает. Новая реплика проверяет схему через `validate()`. Старая реплика при `validate()` не падает от неизвестной ей новой миграции: Flyway по умолчанию пропускает «будущие» миграции (`ignoreMigrationPatterns=*:future`). Удаление или переименование колонок пришлось бы делать в два релиза (expand/contract); в этой работе таких изменений нет.
**Переменные `.env.example` → реальная конфигурация.**
| `.env.example` | У меня |
| --- | --- |
| `APP_HOST`, `APP_PORT` | так же, задаются в `compose.yaml` (`0.0.0.0:8080` внутри сети) |
| `APP_ENV` | не используется |
| `SESSION_TTL_SECONDS` | так же, по умолчанию 3600 |
| `DATABASE_URL_FILE` | разбит на `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER`, `DB_PASSWORD_FILE=/run/secrets/db_password` |
| `SESSION_STORE=postgres` | так и есть: сессии в PostgreSQL, Redis — кэш |
| `CACHE_URL_FILE` | `REDIS_HOST`, `REDIS_PORT`, `REDIS_PASSWORD_FILE=/run/secrets/redis_password` |
| `API_REPLICAS` | так же, `deploy.replicas: ${API_REPLICAS:-2}` (HAProxy рассчитан максимум на 4) |
| `IDEMPOTENCY_TTL_SECONDS` | так же, по умолчанию 86400 |
| — | `DB_MIGRATE_ON_START` (false у реплик), `SHUTDOWN_DRAIN_DELAY_MS` (3000), `SESSION_REVOKE_TIMEOUT_MS` (8000), `INSTANCE_ID` (не задаю) |
**Таймауты и проверки — одна цепочка.**
| Что | Значение | Почему так |
| --- | --- | --- |
| Проверка готовности в HAProxy | каждую 1 с; DOWN после 2 неудач, UP после 2 успехов | обнаружение ≤ 2 с |
| Обновление адресов из DNS | `hold` 2 с, `timeout resolve` 1 с | упавший контейнер исчезает из DNS, слот уходит в MAINT |
| Подключение HAProxy → реплика | 1 с, затем повтор на другую реплику (`retries 2`, `redispatch`) | до отправки запроса повтор безопасен и для POST |
| Ответ реплики | 10 с | больше, чем ожидание пула 2 с + `statement_timeout` 5 с |
| Drain после SIGTERM | 3 с | больше, чем 2 с на обнаружение |
| Остановка Ktor | 1 с тишины, максимум 5 с | дожать начатые запросы |
| `stop_grace_period` | 10 с | 3 + 5 = 8 с < 10 с, SIGKILL не нужен |
| `stop-one.sh` | `docker stop --time 9` | укладывается в 10 с, которые даёт тест |
| Подтверждение logout в Redis | повтор каждые 250 мс, максимум 8 с, затем 503 | меньше `timeout server` 10 с; пауза записи Redis до 8 с даёт 204 |
| Проверка `session_revocations` | раз в 1 с на каждой реплике | реплика, не видевшая сбоя Redis, перестаёт доверять кэшу ≤ 1 с |
Максимальное время обнаружения отказа — около 2 с: при остановке по проверке готовности, при падении процесса по DNS или проверке. Измерено 1,5–2,0 с, см. ниже.
## Доказательства критериев
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
| Публичный smoke | `make test` | PASS, профиль lab3 | evidence/smoke.txt |
| Запуск из чистого clone | копия рабочего дерева без игнорируемых файлов, новые секреты, свежий volume, все `make`-проверки | все PASS | evidence/clean-start.txt |
| Стенд в compose: ≥2 API, один вход, общие БД и сессии | `docker compose ps` | `api-1`, `api-2`, `lb` с портом `127.0.0.1:8080`, `postgres`, `redis`; `migrate` завершился с 0 | evidence/instances.txt, evidence/limits.txt |
| Разные реплики без повторного входа, видно по `X-Instance-Id` | 6 запросов через LB; failover-тест сам проверяет ≥2 реплики | два id чередуются и совпадают с id контейнеров `api-1`, `api-2`; `observed_instances_before: 2` | evidence/instances.txt, evidence/failover-api-*.txt |
| Остановка любой одной реплики: новые чтения/записи ≤ 10 с, подтверждённое на месте | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` для api-1, потом для api-2 | PASS оба раза; 124–125 записей, 0 неудачных; остановлен именно контейнер с id из теста (`Exited (143)`) | evidence/failover-api-1.txt, evidence/failover-api-2.txt |
| Повтор POST с тем же ключом без дубля, в том числе одновременно и через другую реплику | `scripts/idempotency-race.py`: 20 раундов по 32 одновременных запроса с одним ключом; тесты `MessageIdempotencyTest` | 640 ответов 201 (320 + 320 по репликам), в каждом раунде один id, в истории 20 сообщений; 7 тестов: повтор, 409, 400, область ключа, доступ, 20 одновременных запросов, TTL | evidence/idempotency-race.txt, evidence/gradle-tests.txt |
| Неподтверждённую запись можно повторить с тем же ключом | failover-тест повторяет неопределённый POST с исходным ключом | дублей нет, проверка текстов прошла | evidence/failover-api-*.txt |
| `make test`, persistence, failover | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; у lb, обеих api, postgres, redis новый `StartedAt`, volume тот же | evidence/persistence.txt |
| Отзыв токена при сбое записи в Redis виден всем репликам (замечание на PR) | `PAUSE_MS=5000 scripts/revocation-pause.sh`, `PAUSE_MS=12000 …` через LB; тест `CrossReplicaRevocationTest` | пауза 5 с: logout 204 через 5,1 с, затем обе реплики: чтение 401, повтор сообщения с тем же ключом 401. Пауза 12 с: logout 503 через 8,6 с, обе реплики всё равно 401, после паузы в Redis `revoked`. Тест: две реплики на одних PostgreSQL и Redis, вторая отклоняет токен | evidence/revocation-pause.txt, evidence/gradle-tests.txt |
| Уровень 4: health/readiness, таймауты, SIGTERM согласованы, LB исключает недоступную реплику | таблица таймаутов выше; отказ под нагрузкой | после SIGTERM реплика получает последний запрос через 1,5–1,6 с, ошибок 0 | evidence/load/failover-stop-*/ |
| Уровень 4: поочерёдная остановка каждой реплики, RPS, ошибки, восстановление | `scripts/load/failover-under-load.sh`, 150 RPS, stop и kill для api-1 и api-2 | 4 прогона: ~150 RPS, 0 ошибок; таблица ниже | evidence/load/ |
| Уровень 4: параллельные запросы с одним ключом | см. строку про идемпотентность | 0 дублей | evidence/idempotency-race.txt |
| Уровень 4: лимиты CPU/RAM, API без портов наружу, секреты только где нужно, нет `container_name` | `docker inspect`, `docker compose ps`, `docker compose config` | лимиты у всех; порт только у `lb`; `lb` без секретов, `migrate` и `postgres` только `db_password`, `redis` только `redis_password` | evidence/limits.txt |
| Уровень 4: rolling update под нагрузкой, миграции однократно | `scripts/load/rolling-under-load.sh` | 10 500 запросов, 0 ошибок, p99 46,7 мс; обе реплики заменены на образ с новым `BUILD_ID` | evidence/load/rolling-update/ |
| Свои автотесты | `./gradlew test` | 70 тестов, все проходят | evidence/gradle-tests.txt |
| Секреты не в Git | `secrets/` и `.env` в `.gitignore`; в evidence нет токенов (k6 их не пишет, сырые запросы не сохраняются) | — | .gitignore |
## Эксперименты и отказы
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
Стенд: Apple M4 (10 ядер), 16 ГБ, macOS 27, Docker Desktop 28.4 (VM 10 CPU, 8 ГБ), лимиты контейнеров как в таблице выше. k6 2.3 на той же машине.
Профиль (`scripts/load/mixed.js`):
- открытая модель, 150 запросов/с независимо от времени ответа, поэтому медленные ответы не прячут ошибки;
- 70 % `GET /users/me`, 30 % отправка сообщения с уникальным ключом;
- 10 пользователей, один чат, таймаут 5 с, без повторов.
В каждом прогоне 45 с нагрузки: на 10-й секунде реплику останавливаю, на 25-й запускаю обратно. Ошибка — любой ответ не 2xx, включая обрыв. Сводки считает `scripts/load/summarize.py`. Время «LB вывел» и «LB вернул» берётся из лога HAProxy, «получила трафик» — по `X-Instance-Id` в ответах. Каждый вариант прогнан один раз, повторов нет.
Гипотеза: благодаря drain плавная остановка реплики проходит без ошибок, а падение процесса стоит только задержки до 1 с (таймаут подключения), но не ошибок, потому что HAProxy повторяет неудавшееся подключение на другой реплике.
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
| `docker stop` api-1 (SIGTERM) под 150 RPS | 0 ошибок, реплика выходит из ротации за ≤ 2 с | 6 751 запрос, 0 ошибок, 150 RPS, p99 24 мс; последний ответ остановленной реплики через 1,6 с, HAProxy пометил DOWN через 1,6 с | вернувшаяся реплика получила трафик через 4,1 с после `docker start`; потерь нет |
| `docker stop` api-2 | то же | 6 750 запросов, 0 ошибок, 150 RPS, p99 14 мс; DOWN через 1,5 с | трафик через 4,0 с; потерь нет |
| `docker kill` api-1 (SIGKILL, падение) | 0 ошибок, задержка до 1 с у части запросов | 6 713 запросов, 0 ошибок, 149,2 RPS, p99 1 005 мс; DOWN через 1,89 с; k6 не запустил 38 итераций (VU ждали таймаут подключения) | трафик через 4,0 с; потерь нет |
| `docker kill` api-2 | то же | 6 697 запросов, 0 ошибок, 145,6 RPS, p99 1 004 мс; DOWN через 1,98 с; не запущено 54 итерации | трафик через 2,3 с; потерь нет |
| Rolling update двух реплик под 150 RPS | 0 ошибок | 10 500 запросов, 0 ошибок, p99 46,7 мс, max 390 мс; замена двух реплик заняла 31 с (вместе со сборкой образа) | — |
| `make test-failover`, api-1 и api-2 | ≤ 10 с до записи, без дублей | PASS, 0 неудачных из ~124; `first_recovery_seconds` 4,9–5,4 | это время самого `docker stop` (drain 3 с + остановка), записи всё это время шли через вторую реплику |
| `scripts/restart.sh` (lb, api, redis, postgres) | данные и токен на месте | PASS, у всех контейнеров новый `StartedAt` | тест сам ждёт готовности и проверяет данные тем же токеном; потерь нет |
Что из этого следует:
- **SIGTERM.** Drain работает: реплика получает запросы ещё ~1,5 с после SIGTERM, а сокет закрывается только через 3 с. Начатых запросов, которые обрывались бы, нет.
- **SIGKILL.** Ошибок тоже нет, но в первые 1–2 с запросы к мёртвой реплике ждут таймаут подключения 1 с и уходят на живую. Отсюда p99 около 1 с. Адреса упавшего контейнера в сети больше нет, поэтому отказ в соединении (RST) не приходит — только таймаут. Пока виртуальные пользователи k6 ждут эту секунду, часть итераций (38–54 из ~6 750) k6 не успевает запустить, отсюда 145–149 RPS.
- **Обратный ввод в ротацию.** Сначала нужно 2–2,5 с на старт JVM и Ktor, потом 2 успешные проверки (≈2 с).
- **Один прогон на вариант.** Число ошибок (0) от повторов, скорее всего, не изменится, а p99 и времена — оценка, а не распределение.
- **Измерение.** k6 запускался на той же машине. Хвосты p99 в «спокойных» прогонах (до 50 мс) похожи на паузы Docker Desktop и GC, а не на работу балансировщика.
## Ограничения и следующие шаги
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
- **Выдерживается** остановка или падение одной API-реплики, в том числе под нагрузкой, и поочерёдная замена всех реплик. **Не выдерживается** отказ `lb`, `postgres` или всего хоста: они в одном экземпляре на одной машине. Это единые точки отказа, HA для них в этой работе не заявляю. Отказ Redis, как и в lab2, не ломает API: запросы идут в PostgreSQL.
- **Хвост задержки при падении.** При внезапном падении реплики p99 около 1 с из-за `timeout connect 1s`. В сети Docker таймаут подключения можно сделать 200–300 мс, это должно сократить хвост, но я это не проверял.
- **Logout при Redis, который дольше 8 с не принимает запись, отвечает 503.** Сессия при этом уже удалена в PostgreSQL. В течение ~1 с, пока другие реплики не увидели строку в `session_revocations`, они ещё могут принять токен из кэша. Подтверждения (204) клиент в этом случае не получил; повтор logout вернёт 401, потому что сессии в базе уже нет.
- **Single-flight** (защита от одновременных промахов кэша) работает внутри процесса: на N реплик до N запросов к базе на один токен.
- **`make test-failover` всегда выбирает api-1** при одинаковом числе запросов. Вторую реплику проверяю после одного лишнего запроса, это описано в разделе запуска.
- **Без очистки.** Старые ключи идемпотентности (старше 24 ч) удаляются только при повторе того же ключа, а не отдельной задачей. Таблица растёт на одну строку на сообщение; для учебного объёма это нормально, в продакшене нужна периодическая чистка по `created_at`. Просроченные сессии тоже не удаляются (как в lab2).
- **Ограничения rolling update.**
- Скрипт для Compose, не Swarm.
- Совместимость новой версии со старой держится на правиле «миграции только расширяют схему», автоматически это не проверяется.
- После обновления имена контейнеров `api-3`/`api-4`.
- **HAProxy** рассчитан на 4 реплики (`server-template api 4`), для большего числа нужно поменять число в конфиге.
+116
View File
@@ -0,0 +1,116 @@
plugins {
alias(libs.plugins.kotlin)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.ksp)
alias(libs.plugins.shadow)
id("application")
}
group = "com.mtusi.messenger"
version = "2.0.0"
repositories {
mavenCentral()
}
dependencies {
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.serialization.hocon)
implementation(libs.logback.classic)
implementation(ktorLibs.server.core)
implementation(ktorLibs.server.netty)
implementation(ktorLibs.server.auth)
implementation(ktorLibs.server.contentNegotiation)
implementation(ktorLibs.server.statusPages)
implementation(ktorLibs.server.callLogging)
implementation(ktorLibs.server.defaultHeaders)
implementation(ktorLibs.server.cors)
implementation(ktorLibs.server.swagger)
implementation(ktorLibs.serialization.kotlinx.json)
implementation(libs.koin.core)
implementation(libs.koin.ktor)
implementation(libs.koin.annotations)
ksp(libs.koin.compiler)
implementation(libs.exposed.core)
implementation(libs.exposed.jdbc)
implementation(libs.exposed.kotlin.datetime)
implementation(libs.exposed.migration.core)
implementation(libs.exposed.migration.jdbc)
implementation(libs.postgres)
implementation(libs.hikaricp)
implementation(libs.flyway)
implementation(libs.flyway.postgresql)
implementation(libs.lettuce)
implementation(libs.bouncycastle)
testImplementation(kotlin("test"))
testImplementation(ktorLibs.server.testHost)
testImplementation(ktorLibs.client.contentNegotiation)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.koin.test)
testImplementation(libs.testContainers)
testImplementation(libs.testContainers.postgresql)
}
kotlin {
jvmToolchain(21)
}
ksp {
arg("KOIN_CONFIG_CHECK", "true")
arg("KOIN_DEFAULT_MODULE", "false")
}
application {
mainClass = "com.mtusi.messenger.main.AppKt"
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=false")
}
sourceSets {
named("main") {
resources {
srcDir(layout.projectDirectory.dir("contracts"))
}
}
}
tasks {
test {
useJUnitPlatform()
}
shadowJar {
archiveBaseName = "messenger"
archiveClassifier = ""
archiveVersion = ""
mergeServiceFiles()
}
getByName<JavaExec>("run") {
val configArgs = File("run/development/config")
.listFiles()
?.filter { it.extension == "conf" }
?.map {
"-config=config/${it.name}"
}
?: emptyList()
args = configArgs
workingDir = file("run/development")
}
register<JavaExec>("generateMigration") {
group = "database"
description = "Generates a new migration"
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("com.mtusi.messenger.main.plugins.DatabaseKt")
}
}
+154
View File
@@ -0,0 +1,154 @@
name: mtusi-messenger-lab3
services:
postgres:
image: postgres:17.6-alpine3.22
command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements", "-c", "pg_stat_statements.track=all"]
environment:
POSTGRES_DB: messenger
POSTGRES_USER: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
deploy:
resources:
limits:
cpus: "1.0"
memory: 1g
restart: unless-stopped
redis:
image: redis:8.4.0-alpine
# Session cache only, PostgreSQL is the source of truth: no RDB/AOF, a restart simply empties the cache.
# Every key carries a TTL, so under memory pressure volatile-lru evicts the least used sessions.
command:
- sh
- -c
- >-
exec docker-entrypoint.sh redis-server
--requirepass "$$(cat /run/secrets/redis_password)"
--save ""
--appendonly no
--maxmemory 64mb
--maxmemory-policy volatile-lru
secrets:
- redis_password
healthcheck:
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/redis_password)\" redis-cli ping | grep -q PONG"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
deploy:
resources:
limits:
cpus: "0.5"
memory: 128m
restart: unless-stopped
# One-shot: applies Flyway migrations once and exits. Replicas start only after it succeeded,
# so they never race each other on the schema.
migrate:
build: .
image: mtusi-messenger:lab3
command: ["migrate"]
healthcheck:
test: ["NONE"]
depends_on:
postgres:
condition: service_healthy
environment:
DB_HOST: postgres
DB_PORT: "5432"
DB_NAME: messenger
DB_USER: messenger
DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
deploy:
resources:
limits:
cpus: "0.5"
memory: 384m
restart: "no"
api:
build: .
image: mtusi-messenger:lab3
# Interchangeable replicas: no container_name and no published port, traffic only through `lb`.
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "1.0"
memory: 512m
depends_on:
migrate:
condition: service_completed_successfully
postgres:
condition: service_healthy
redis:
condition: service_healthy
environment:
APP_HOST: "0.0.0.0"
APP_PORT: "8080"
DB_HOST: postgres
DB_PORT: "5432"
DB_NAME: messenger
DB_USER: messenger
DB_PASSWORD_FILE: /run/secrets/db_password
DB_MIGRATE_ON_START: "false"
REDIS_HOST: redis
REDIS_PORT: "6379"
REDIS_PASSWORD_FILE: /run/secrets/redis_password
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
SESSION_CACHE_SINGLE_FLIGHT: ${SESSION_CACHE_SINGLE_FLIGHT:-true}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
SHUTDOWN_DRAIN_DELAY_MS: "3000"
secrets:
- db_password
- redis_password
# SIGTERM: readiness 503 and 3 s drain (HAProxy needs <= 2 s to notice), then Ktor closes the socket and
# finishes requests in flight within 5 s. 3 + 5 = 8 s < 10 s before Docker sends SIGKILL.
stop_grace_period: 10s
restart: unless-stopped
lb:
image: haproxy:3.2.4-alpine
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api:
condition: service_healthy
ports:
- "127.0.0.1:8080:8080"
# Port 8404 (health and stats) stays inside the compose network.
healthcheck:
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8404/healthz"]
interval: 5s
timeout: 2s
retries: 3
deploy:
resources:
limits:
cpus: "0.5"
memory: 128m
restart: unless-stopped
secrets:
db_password:
file: ./secrets/db_password
redis_password:
file: ./secrets/redis_password
volumes:
postgres-data:
+20
View File
@@ -0,0 +1,20 @@
# clean copy of the working tree (git ls-files -co --exclude-standard), fresh secrets and volume, COMPOSE_PROJECT_NAME=lab3-clean
# 2026-10-23T19:07Z, base commit af184ca + fix of session revocation across replicas
$ ./scripts/init-secrets.sh && docker compose up -d --build --wait && make check && make test && make test-persistence ACTION_SCRIPT=scripts/restart.sh && make test-failover ACTION_SCRIPT=scripts/stop-one.sh && PAUSE_MS=5000 scripts/revocation-pause.sh
created secrets/db_password
created secrets/redis_password
Container lab3-clean-api-2 Healthy
Container lab3-clean-lb-1 Healthy
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 4.945}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
GET /users/me -> 200 e4b51cb6e956
GET /users/me -> 200 20b26306c56f
GET /users/me -> 200 e4b51cb6e956
GET /users/me -> 200 20b26306c56f
DELETE /auth/sessions/current -> 204 e4b51cb6e956 after 5.09 s
GET /users/me -> 401 20b26306c56f
GET /users/me -> 401 e4b51cb6e956
GET /users/me -> 401 20b26306c56f
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 73b8fff02245 running Up 22 seconds (healthy)
mtusi-messenger-lab3-api-2 01d14766531f running Up 22 seconds (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 125, "failed_attempts": 0, "first_recovery_seconds": 5.401}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 73b8fff02245 exited Exited (143) 10 seconds ago
mtusi-messenger-lab3-api-2 01d14766531f running Up 38 seconds (healthy)
# HAProxy log around the failure (non-2xx and state changes)
172.24.0.1:59474 [23/Oct/2026:19:02:09.419] http api/api2 0/0/0/55/55 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
172.24.0.1:56176 [23/Oct/2026:19:02:23.431] http api/api2 0/0/0/73/73 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
172.24.0.1:56204 [23/Oct/2026:19:02:23.611] http api/api2 0/0/0/27/27 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
172.24.0.1:56220 [23/Oct/2026:19:02:23.714] http api/api2 0/0/0/50/50 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
[WARNING] (8) : Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api2 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 5ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ).
Server api/api2 was DOWN and now enters maintenance (No IP for server ).
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 73b8fff02245 running Up 7 seconds (healthy)
mtusi-messenger-lab3-api-2 01d14766531f running Up About a minute (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 124, "failed_attempts": 0, "first_recovery_seconds": 4.882}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 73b8fff02245 running Up 24 seconds (healthy)
mtusi-messenger-lab3-api-2 01d14766531f exited Exited (143) 10 seconds ago
# HAProxy log around the failure (non-2xx and state changes)
Server api/api2 is UP, reason: Layer7 check passed, code: 200, check duration: 5ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
172.24.0.1:57320 [23/Oct/2026:19:03:21.353] http api/api1 0/0/0/77/77 201 504 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
172.24.0.1:57342 [23/Oct/2026:19:03:21.513] http api/api1 0/0/0/105/105 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
172.24.0.1:57356 [23/Oct/2026:19:03:21.724] http api/api1 0/0/0/85/85 201 500 - - ---- 1/1/0/0/0 0/0 "POST /api/v1/auth/sessions HTTP/1.1"
[WARNING] (8) : Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api1 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api1 was DOWN and now enters maintenance (No IP for server ).
[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ).
+90
View File
@@ -0,0 +1,90 @@
# ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine)
total: 70 tests, 0 failed
infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 3.509 s
- logout revokes only the current token
- Bearer token cannot open a session
- password containing a colon and non-ASCII characters works
- unknown token returns 401
- session expires after SESSION_TTL_SECONDS
- each login issues a different token
- valid Basic credentials open a session
- missing or malformed Basic header returns 401
- wrong password and unknown user get the same 401
infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 1.078 s
- member can read own chat and members keep their order
- unknown member is rejected before anything is written
- foreign chat returns 404 for non-member
- cursor issued to another user is rejected
- chat list contains only own chats and is paginated
- invalid member lists are rejected with 400
infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.102 s
- live returns 200
- ready returns 200 when database is reachable
infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.230 s
- parallel senders produce exactly N unique messages
infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.020 s
- foreign chat returns 404 when sending
- last page has null next_cursor
- pagination walks all messages exactly once
- foreign chat returns 404 when listing
- empty text returns 400 invalid_request
- history that is an exact multiple of limit ends with null next_cursor
- foreign cursor returns 400
infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 2.500 s
- expired key can be used for a new message
- key scope is user and chat
- same key with another body is a conflict
- missing or malformed key is rejected before anything is written
- retry with the same key and body returns the original message
- retry by a former outsider is still checked for access
- concurrent retries create exactly one message
infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 0.917 s
- password shorter than 12 code points returns 400
- concurrent registrations of one username create exactly one user
- X-User-Id alone gives no access
- create user returns 201 without password
- duplicate username returns 409 conflict
- missing token returns 401 with Bearer challenge
- invalid JSON body returns 400 invalid_request
- missing password returns 400
- Basic credentials are rejected on business endpoints
- me returns the session owner
- X-User-Id does not override bearer identity
infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.124 s
- cached session round-trips and expires together with the session
- revoke replaces a cached session with a tombstone
- late cache fill cannot overwrite a tombstone
infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.233 s
- chat row is rolled back when inserting members fails
- successful creation commits the chat and every member
infrastructure.services.authentication.CrossReplicaRevocationTest: 2 tests, 0 failed, 11.634 s
- unconfirmed logout makes every replica bypass the cache
- logout waits for paused Redis writes and then no replica accepts the token
infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.091 s
- request arriving after logout does not join a lookup started before it
- without single-flight every concurrent miss queries the database
- with single-flight concurrent misses share one database lookup
- lookup that finished before a request arrived is not reused
infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.170 s
- passwords differing only after 72 bytes are distinguished
- validates the right password and rejects a wrong one
- malformed hash is rejected instead of throwing
- hash is a salted Argon2id PHC string
plugins.CorsTest: 2 tests, 0 failed, 0.129 s
- page of the API itself may call it
- unknown origin is rejected
plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.185 s
- re-running migrations applies nothing and keeps data
plugins.DependencyFailureTest: 5 tests, 0 failed, 25.701 s
- without PostgreSQL requests fail with a bounded 503
- logout during a Redis outage survives an API restart
- logout during a Redis outage is not undone by a stale cached copy
- without Redis requests bypass the cache
- hanging Redis costs one command timeout and recovers
plugins.InstanceIdTest: 1 tests, 0 failed, 0.087 s
- every response names the replica
plugins.SessionCacheTest: 3 tests, 0 failed, 0.624 s
- emptied cache falls back to PostgreSQL
- first authenticated request fills the cache
- logout leaves a tombstone and the token is rejected
+19
View File
@@ -0,0 +1,19 @@
$ python3 scripts/idempotency-race.py
{
"rounds": 20,
"parallel_requests_per_round": 32,
"statuses": {
"201": 640
},
"requests_per_instance": {
"73b8fff02245": 320,
"01d14766531f": 320
},
"rounds_with_more_than_one_message_id": 0,
"messages_in_history": 20,
"distinct_texts_in_history": 20
}
PASS
$ psql: keys without a message (must be 0)
0
+21
View File
@@ -0,0 +1,21 @@
$ docker compose ps -a
NAME IMAGE STATE STATUS PORTS
mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 9 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 9 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 7 seconds (healthy) 127.0.0.1:8080->8080/tcp
mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 10 seconds ago
mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 20 seconds (healthy) 5432/tcp
mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 20 seconds (healthy) 6379/tcp
$ for i in 1..6: curl -sD - http://localhost:8080/health/ready | grep X-Instance-Id
x-instance-id: 01d14766531f
x-instance-id: 73b8fff02245
x-instance-id: 01d14766531f
x-instance-id: 73b8fff02245
x-instance-id: 01d14766531f
x-instance-id: 73b8fff02245
# X-Instance-Id = HOSTNAME = short container id of the replica
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 73b8fff02245 running Up 10 seconds (healthy)
mtusi-messenger-lab3-api-2 01d14766531f running Up 10 seconds (healthy)
+44
View File
@@ -0,0 +1,44 @@
$ docker compose ps
NAME IMAGE STATUS PORTS
mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 Up 57 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 Up 57 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine Up 54 seconds (healthy) 127.0.0.1:8080->8080/tcp
mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 Up About a minute (healthy) 5432/tcp
mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine Up About a minute (healthy) 6379/tcp
# limits (NanoCpus / Memory) from docker inspect
/mtusi-messenger-lab3-api-1 cpus=1000000000 memory=536870912 restart=unless-stopped
/mtusi-messenger-lab3-api-2 cpus=1000000000 memory=536870912 restart=unless-stopped
/mtusi-messenger-lab3-lb-1 cpus=500000000 memory=134217728 restart=unless-stopped
/mtusi-messenger-lab3-postgres-1 cpus=1000000000 memory=1073741824 restart=unless-stopped
/mtusi-messenger-lab3-redis-1 cpus=500000000 memory=134217728 restart=unless-stopped
$ docker stats --no-stream
NAME CPU % MEM USAGE / LIMIT
mtusi-messenger-lab3-lb-1 0.21% 13.65MiB / 128MiB
mtusi-messenger-lab3-api-1 2.36% 183.7MiB / 512MiB
mtusi-messenger-lab3-api-2 1.42% 184.4MiB / 512MiB
mtusi-messenger-lab3-postgres-1 0.13% 67.29MiB / 1GiB
mtusi-messenger-lab3-redis-1 0.26% 12.44MiB / 128MiB
# published ports: only the balancer
mtusi-messenger-lab3-api-1 [{ 8080 0 tcp}]
mtusi-messenger-lab3-api-2 [{ 8080 0 tcp}]
mtusi-messenger-lab3-lb-1 [{127.0.0.1 8080 8080 tcp}]
mtusi-messenger-lab3-postgres-1 [{ 5432 0 tcp}]
mtusi-messenger-lab3-redis-1 [{ 6379 0 tcp}]
# secrets per service
api ['db_password', 'redis_password']
lb []
migrate ['db_password']
postgres ['db_password']
redis ['redis_password']
# HAProxy backend table
# pxname,svname,status,check_status
api,api1,UP,L7OK
api,api2,UP,L7OK
api,api3,MAINT (resolution),
api,api4,MAINT (resolution),
api,BACKEND,UP,
+20
View File
@@ -0,0 +1,20 @@
# Нагрузочные прогоны
Дата: 2026-10-23. Код: commit af184ca + исправление отзыва сессий по замечанию на PR (тот же код, что в PR).
Стенд: Apple M4 (10 ядер), 16 ГБ, macOS 27; Docker Desktop 28.4 (VM 10 CPU, 8 ГБ), Compose 2.39; k6 2.3 на той же машине.
Лимиты контейнеров — как в `compose.yaml`: api 1 CPU / 512 МБ ×2, postgres 1 CPU / 1 ГБ, redis 0.5 CPU / 128 МБ, lb 0.5 CPU / 128 МБ.
Профиль (`scripts/load/mixed.js`): открытая модель, 150 запросов/с через `localhost:8080`, 10 пользователей, один общий чат.
70 % `GET /api/v1/users/me`, 30 % `POST /api/v1/chats/{id}/messages` с уникальным `Idempotency-Key`. Таймаут запроса 5 с, повторов нет.
Ошибка — любой ответ не 2xx, включая status 0 (обрыв, таймаут).
| Каталог | Команда | Что происходит |
| --- | --- | --- |
| `failover-stop-api-1`, `failover-stop-api-2` | `MODE=stop TARGET=api-N scripts/load/failover-under-load.sh` | 45 с нагрузки, на 10-й с `docker stop` реплики (SIGTERM), на 25-й `docker start` |
| `failover-kill-api-1`, `failover-kill-api-2` | `MODE=kill TARGET=api-N scripts/load/failover-under-load.sh` | то же, но `docker kill` (SIGKILL, падение процесса) |
| `rolling-update` | `scripts/load/rolling-under-load.sh` | 70 с нагрузки, с 10-й с `scripts/rolling-update.sh` |
В каждом каталоге: `k6.txt` (итог k6), `events.json` (моменты действий, epoch ms), `haproxy.log` (смены состояния backend и ответы 5xx),
`per-second.csv` (по секундам: успешные, ошибки, p99, max, сколько ответила каждая реплика), `summary.json` (итог, считает `scripts/load/summarize.py`).
Сырые данные по каждому запросу в Git не кладутся; `KEEP_RAW=1` оставляет `requests.csv` локально.
@@ -0,0 +1,2 @@
{"mode": "kill", "target": "api-1", "target_instance": "ef8f943d313b", "restored_instance": "ef8f943d313b",
"load_start": 1792781931991, "fault": 1792781942042, "fault_done": 1792781942234, "restore_begin": 1792781957033, "restore_healthy": 1792781959476}
+26
View File
@@ -0,0 +1,26 @@
█ TOTAL RESULTS
CUSTOM
served_by......................: 6713 145.257079/s
HTTP
http_req_duration..............: avg=42.08ms med=2.01ms p(95)=171.07ms p(99)=1s max=1.29s
{ expected_response:true }...: avg=42.08ms med=2.01ms p(95)=171.07ms p(99)=1s max=1.29s
http_req_failed................: 0.00% 0 out of 6734
http_reqs......................: 6734 145.711481/s
EXECUTION
dropped_iterations.............: 38 0.822251/s
iteration_duration.............: avg=42.25ms med=2.21ms p(95)=171.3ms p(99)=1s max=1.29s
iterations.....................: 6713 145.257079/s
vus............................: 1 min=0 max=85
vus_max........................: 88 min=50 max=88
NETWORK
data_received..................: 2.6 MB 56 kB/s
data_sent......................: 1.3 MB 29 kB/s
@@ -0,0 +1,46 @@
second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b
0,151,0,13.2,16.3,76,75
1,150,0,4.8,6.2,75,75
2,150,0,5.2,5.5,75,75
3,150,0,7.9,10.4,75,75
4,150,0,6.3,7.3,75,75
5,150,0,8.1,8.9,75,75
6,150,0,6.0,6.1,75,75
7,150,0,4.6,4.9,75,75
8,150,0,4.2,4.4,75,75
9,150,0,5.1,6.0,75,75
10,150,0,13.5,13.6,77,73
11,68,0,10.1,10.1,68,0
12,146,0,1006.5,1006.6,146,0
13,213,0,1012.8,1013.9,213,0
14,150,0,7.0,7.6,150,0
15,150,0,4.5,4.5,150,0
16,150,0,4.3,8.0,150,0
17,150,0,4.7,7.5,150,0
18,150,0,4.7,5.4,150,0
19,150,0,6.0,6.3,150,0
20,150,0,5.6,5.8,150,0
21,150,0,6.2,6.5,150,0
22,150,0,6.1,6.2,150,0
23,150,0,11.8,24.3,150,0
24,150,0,7.8,14.1,150,0
25,150,0,5.3,6.8,150,0
26,150,0,4.2,5.5,150,0
27,150,0,5.3,5.7,150,0
28,150,0,7.0,11.0,150,0
29,145,0,546.0,550.9,100,45
30,155,0,135.4,179.9,75,80
31,128,0,53.6,57.2,64,64
32,89,0,926.0,926.0,69,20
33,218,0,1266.2,1293.6,84,134
34,150,0,6.1,7.9,75,75
35,150,0,6.8,7.7,75,75
36,150,0,8.0,10.8,75,75
37,150,0,29.3,34.7,75,75
38,150,0,8.0,8.9,75,75
39,150,0,5.2,5.4,75,75
40,150,0,4.9,6.1,75,75
41,150,0,6.6,7.0,75,75
42,150,0,5.7,6.2,75,75
43,150,0,9.2,15.0,75,75
44,150,0,7.0,8.1,75,75
1 second ok errors p99_ms max_ms 33ea33bfe1fd ef8f943d313b
2 0 151 0 13.2 16.3 76 75
3 1 150 0 4.8 6.2 75 75
4 2 150 0 5.2 5.5 75 75
5 3 150 0 7.9 10.4 75 75
6 4 150 0 6.3 7.3 75 75
7 5 150 0 8.1 8.9 75 75
8 6 150 0 6.0 6.1 75 75
9 7 150 0 4.6 4.9 75 75
10 8 150 0 4.2 4.4 75 75
11 9 150 0 5.1 6.0 75 75
12 10 150 0 13.5 13.6 77 73
13 11 68 0 10.1 10.1 68 0
14 12 146 0 1006.5 1006.6 146 0
15 13 213 0 1012.8 1013.9 213 0
16 14 150 0 7.0 7.6 150 0
17 15 150 0 4.5 4.5 150 0
18 16 150 0 4.3 8.0 150 0
19 17 150 0 4.7 7.5 150 0
20 18 150 0 4.7 5.4 150 0
21 19 150 0 6.0 6.3 150 0
22 20 150 0 5.6 5.8 150 0
23 21 150 0 6.2 6.5 150 0
24 22 150 0 6.1 6.2 150 0
25 23 150 0 11.8 24.3 150 0
26 24 150 0 7.8 14.1 150 0
27 25 150 0 5.3 6.8 150 0
28 26 150 0 4.2 5.5 150 0
29 27 150 0 5.3 5.7 150 0
30 28 150 0 7.0 11.0 150 0
31 29 145 0 546.0 550.9 100 45
32 30 155 0 135.4 179.9 75 80
33 31 128 0 53.6 57.2 64 64
34 32 89 0 926.0 926.0 69 20
35 33 218 0 1266.2 1293.6 84 134
36 34 150 0 6.1 7.9 75 75
37 35 150 0 6.8 7.7 75 75
38 36 150 0 8.0 10.8 75 75
39 37 150 0 29.3 34.7 75 75
40 38 150 0 8.0 8.9 75 75
41 39 150 0 5.2 5.4 75 75
42 40 150 0 4.9 6.1 75 75
43 41 150 0 6.6 7.0 75 75
44 42 150 0 5.7 6.2 75 75
45 43 150 0 9.2 15.0 75 75
46 44 150 0 7.0 8.1 75 75
@@ -0,0 +1,26 @@
{
"requests": 6713,
"ok": 6713,
"errors": 0,
"error_codes": {},
"ok_rps": 149.2,
"p50_ms": 2.0,
"p95_ms": 171.4,
"p99_ms": 1005.3,
"max_ms": 1293.6,
"requests_per_instance": {
"33ea33bfe1fd": 4722,
"ef8f943d313b": 1991
},
"event_load_start_s": 0.89,
"event_fault_s": 10.94,
"event_fault_done_s": 11.13,
"event_restore_begin_s": 25.93,
"event_restore_healthy_s": 28.38,
"errors_after_fault": 0,
"last_error_after_fault_s": null,
"target_last_response_after_fault_s": 0.02,
"restored_replica_first_response_after_start_s": 3.95,
"lb_marked_down_after_fault_s": 1.89,
"lb_marked_up_after_start_s": 3.39
}
@@ -0,0 +1,2 @@
{"mode": "kill", "target": "api-2", "target_instance": "33ea33bfe1fd", "restored_instance": "33ea33bfe1fd",
"load_start": 1792781980971, "fault": 1792781991007, "fault_done": 1792781991173, "restore_begin": 1792782006018, "restore_healthy": 1792782008497}
+26
View File
@@ -0,0 +1,26 @@
█ TOTAL RESULTS
CUSTOM
served_by......................: 6697 145.107345/s
HTTP
http_req_duration..............: avg=44.22ms med=1.98ms p(95)=297.87ms p(99)=1s max=1.12s
{ expected_response:true }...: avg=44.22ms med=1.98ms p(95)=297.87ms p(99)=1s max=1.12s
http_req_failed................: 0.00% 0 out of 6718
http_reqs......................: 6718 145.562363/s
EXECUTION
dropped_iterations.............: 54 1.170046/s
iteration_duration.............: avg=44.43ms med=2.16ms p(95)=298.99ms p(99)=1s max=1.12s
iterations.....................: 6697 145.107345/s
vus............................: 97 min=0 max=97
vus_max........................: 97 min=50 max=97
NETWORK
data_received..................: 2.6 MB 56 kB/s
data_sent......................: 1.3 MB 29 kB/s
@@ -0,0 +1,47 @@
second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b
0,152,0,6.1,8.1,76,76
1,150,0,9.8,13.4,75,75
2,150,0,4.5,4.5,75,75
3,150,0,96.5,99.6,75,75
4,149,0,391.0,395.5,74,75
5,151,0,21.4,21.6,76,75
6,150,0,5.1,6.4,75,75
7,150,0,5.3,5.9,75,75
8,149,0,5.6,7.3,74,75
9,148,0,36.3,40.2,75,73
10,89,0,28.8,28.8,13,76
11,110,0,1006.8,1008.0,0,110
12,214,0,1006.9,1008.4,0,214
13,159,0,1004.7,1005.4,0,159
14,149,0,6.3,7.3,0,149
15,151,0,9.9,12.8,0,151
16,150,0,36.1,43.2,0,150
17,149,0,5.0,5.0,0,149
18,151,0,6.2,8.4,0,151
19,150,0,5.4,7.6,0,150
20,150,0,5.5,5.8,0,150
21,150,0,6.0,8.0,0,150
22,150,0,6.2,8.0,0,150
23,149,0,8.0,9.0,0,149
24,151,0,5.1,5.4,0,151
25,150,0,4.1,4.5,0,150
26,136,0,4.1,4.5,0,136
27,154,0,954.6,986.7,79,75
28,160,0,148.6,161.1,85,75
29,150,0,21.8,36.3,75,75
30,149,0,4.5,5.4,75,74
31,151,0,5.7,6.1,75,76
32,150,0,5.4,5.8,75,75
33,150,0,9.0,21.0,75,75
34,150,0,48.5,55.4,75,75
35,149,0,4.7,5.7,75,74
36,151,0,7.1,7.5,75,76
37,150,0,4.5,6.0,75,75
38,150,0,5.8,6.3,75,75
39,150,0,4.8,5.5,75,75
40,150,0,4.7,5.8,75,75
41,149,0,11.5,11.8,75,74
42,151,0,10.9,20.2,75,76
43,123,0,471.7,504.0,54,69
44,56,0,588.3,588.3,25,31
45,97,0,1125.0,1125.0,59,38
1 second ok errors p99_ms max_ms 33ea33bfe1fd ef8f943d313b
2 0 152 0 6.1 8.1 76 76
3 1 150 0 9.8 13.4 75 75
4 2 150 0 4.5 4.5 75 75
5 3 150 0 96.5 99.6 75 75
6 4 149 0 391.0 395.5 74 75
7 5 151 0 21.4 21.6 76 75
8 6 150 0 5.1 6.4 75 75
9 7 150 0 5.3 5.9 75 75
10 8 149 0 5.6 7.3 74 75
11 9 148 0 36.3 40.2 75 73
12 10 89 0 28.8 28.8 13 76
13 11 110 0 1006.8 1008.0 0 110
14 12 214 0 1006.9 1008.4 0 214
15 13 159 0 1004.7 1005.4 0 159
16 14 149 0 6.3 7.3 0 149
17 15 151 0 9.9 12.8 0 151
18 16 150 0 36.1 43.2 0 150
19 17 149 0 5.0 5.0 0 149
20 18 151 0 6.2 8.4 0 151
21 19 150 0 5.4 7.6 0 150
22 20 150 0 5.5 5.8 0 150
23 21 150 0 6.0 8.0 0 150
24 22 150 0 6.2 8.0 0 150
25 23 149 0 8.0 9.0 0 149
26 24 151 0 5.1 5.4 0 151
27 25 150 0 4.1 4.5 0 150
28 26 136 0 4.1 4.5 0 136
29 27 154 0 954.6 986.7 79 75
30 28 160 0 148.6 161.1 85 75
31 29 150 0 21.8 36.3 75 75
32 30 149 0 4.5 5.4 75 74
33 31 151 0 5.7 6.1 75 76
34 32 150 0 5.4 5.8 75 75
35 33 150 0 9.0 21.0 75 75
36 34 150 0 48.5 55.4 75 75
37 35 149 0 4.7 5.7 75 74
38 36 151 0 7.1 7.5 75 76
39 37 150 0 4.5 6.0 75 75
40 38 150 0 5.8 6.3 75 75
41 39 150 0 4.8 5.5 75 75
42 40 150 0 4.7 5.8 75 75
43 41 149 0 11.5 11.8 75 74
44 42 151 0 10.9 20.2 75 76
45 43 123 0 471.7 504.0 54 69
46 44 56 0 588.3 588.3 25 31
47 45 97 0 1125.0 1125.0 59 38
@@ -0,0 +1,26 @@
{
"requests": 6697,
"ok": 6697,
"errors": 0,
"error_codes": {},
"ok_rps": 145.6,
"p50_ms": 2.0,
"p95_ms": 299.1,
"p99_ms": 1003.8,
"max_ms": 1125.0,
"requests_per_instance": {
"ef8f943d313b": 4582,
"33ea33bfe1fd": 2115
},
"event_load_start_s": 0.1,
"event_fault_s": 10.13,
"event_fault_done_s": 10.3,
"event_restore_begin_s": 25.14,
"event_restore_healthy_s": 27.62,
"errors_after_fault": 0,
"last_error_after_fault_s": null,
"target_last_response_after_fault_s": 0.03,
"restored_replica_first_response_after_start_s": 2.27,
"lb_marked_down_after_fault_s": 1.98,
"lb_marked_up_after_start_s": null
}
@@ -0,0 +1,2 @@
{"mode": "stop", "target": "api-1", "target_instance": "ef8f943d313b", "restored_instance": "ef8f943d313b",
"load_start": 1792781831378, "fault": 1792781841419, "fault_done": 1792781845703, "restore_begin": 1792781856431, "restore_healthy": 1792781858936}
+25
View File
@@ -0,0 +1,25 @@
█ TOTAL RESULTS
CUSTOM
served_by......................: 6751 146.192761/s
HTTP
http_req_duration..............: avg=4.7ms med=1.97ms p(95)=5.34ms p(99)=43.67ms max=610.04ms
{ expected_response:true }...: avg=4.7ms med=1.97ms p(95)=5.34ms p(99)=43.67ms max=610.04ms
http_req_failed................: 0.00% 0 out of 6772
http_reqs......................: 6772 146.647516/s
EXECUTION
iteration_duration.............: avg=4.75ms med=2.17ms p(95)=5.5ms p(99)=23.83ms max=610.26ms
iterations.....................: 6751 146.192761/s
vus............................: 1 min=0 max=22
vus_max........................: 50 min=50 max=50
NETWORK
data_received..................: 2.6 MB 56 kB/s
data_sent......................: 1.3 MB 29 kB/s
@@ -0,0 +1,46 @@
second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b
0,152,0,5.8,12.1,76,76
1,150,0,8.8,19.5,75,75
2,150,0,4.7,4.9,75,75
3,150,0,18.4,19.9,75,75
4,150,0,5.2,5.5,75,75
5,150,0,4.7,4.8,75,75
6,149,0,5.0,5.6,74,75
7,151,0,6.3,6.4,76,75
8,150,0,13.9,14.1,75,75
9,150,0,10.2,10.8,75,75
10,150,0,6.7,7.9,75,75
11,150,0,4.1,4.6,75,75
12,150,0,4.7,6.2,112,38
13,150,0,14.8,18.6,150,0
14,150,0,5.5,6.8,150,0
15,150,0,5.3,7.5,150,0
16,150,0,5.2,5.4,150,0
17,150,0,4.4,5.0,150,0
18,150,0,4.9,5.3,150,0
19,149,0,5.6,5.8,149,0
20,151,0,5.2,6.1,151,0
21,150,0,4.4,6.5,150,0
22,150,0,6.2,7.0,150,0
23,150,0,16.9,17.2,150,0
24,150,0,4.8,9.1,150,0
25,150,0,5.2,6.1,150,0
26,150,0,7.4,16.9,150,0
27,150,0,4.8,5.6,150,0
28,150,0,4.8,4.9,150,0
29,115,0,6.0,6.2,115,0
30,185,0,588.5,610.0,75,110
31,150,0,7.2,8.2,75,75
32,150,0,8.4,11.9,75,75
33,150,0,26.3,30.7,75,75
34,150,0,6.9,7.3,75,75
35,150,0,7.0,8.7,75,75
36,150,0,5.2,5.9,75,75
37,150,0,5.4,6.5,75,75
38,150,0,7.9,11.6,75,75
39,150,0,4.9,5.1,75,75
40,149,0,6.5,7.2,74,75
41,151,0,6.4,15.9,76,75
42,150,0,11.7,14.7,75,75
43,147,0,20.3,28.0,73,74
44,152,0,26.7,42.2,76,76
1 second ok errors p99_ms max_ms 33ea33bfe1fd ef8f943d313b
2 0 152 0 5.8 12.1 76 76
3 1 150 0 8.8 19.5 75 75
4 2 150 0 4.7 4.9 75 75
5 3 150 0 18.4 19.9 75 75
6 4 150 0 5.2 5.5 75 75
7 5 150 0 4.7 4.8 75 75
8 6 149 0 5.0 5.6 74 75
9 7 151 0 6.3 6.4 76 75
10 8 150 0 13.9 14.1 75 75
11 9 150 0 10.2 10.8 75 75
12 10 150 0 6.7 7.9 75 75
13 11 150 0 4.1 4.6 75 75
14 12 150 0 4.7 6.2 112 38
15 13 150 0 14.8 18.6 150 0
16 14 150 0 5.5 6.8 150 0
17 15 150 0 5.3 7.5 150 0
18 16 150 0 5.2 5.4 150 0
19 17 150 0 4.4 5.0 150 0
20 18 150 0 4.9 5.3 150 0
21 19 149 0 5.6 5.8 149 0
22 20 151 0 5.2 6.1 151 0
23 21 150 0 4.4 6.5 150 0
24 22 150 0 6.2 7.0 150 0
25 23 150 0 16.9 17.2 150 0
26 24 150 0 4.8 9.1 150 0
27 25 150 0 5.2 6.1 150 0
28 26 150 0 7.4 16.9 150 0
29 27 150 0 4.8 5.6 150 0
30 28 150 0 4.8 4.9 150 0
31 29 115 0 6.0 6.2 115 0
32 30 185 0 588.5 610.0 75 110
33 31 150 0 7.2 8.2 75 75
34 32 150 0 8.4 11.9 75 75
35 33 150 0 26.3 30.7 75 75
36 34 150 0 6.9 7.3 75 75
37 35 150 0 7.0 8.7 75 75
38 36 150 0 5.2 5.9 75 75
39 37 150 0 5.4 6.5 75 75
40 38 150 0 7.9 11.6 75 75
41 39 150 0 4.9 5.1 75 75
42 40 149 0 6.5 7.2 74 75
43 41 151 0 6.4 15.9 76 75
44 42 150 0 11.7 14.7 75 75
45 43 147 0 20.3 28.0 73 74
46 44 152 0 26.7 42.2 76 76
@@ -0,0 +1,26 @@
{
"requests": 6751,
"ok": 6751,
"errors": 0,
"error_codes": {},
"ok_rps": 150.0,
"p50_ms": 2.0,
"p95_ms": 5.2,
"p99_ms": 24.0,
"max_ms": 610.0,
"requests_per_instance": {
"33ea33bfe1fd": 4652,
"ef8f943d313b": 2099
},
"event_load_start_s": 0.86,
"event_fault_s": 10.9,
"event_fault_done_s": 15.18,
"event_restore_begin_s": 25.91,
"event_restore_healthy_s": 28.41,
"errors_after_fault": 0,
"last_error_after_fault_s": null,
"target_last_response_after_fault_s": 1.6,
"restored_replica_first_response_after_start_s": 4.11,
"lb_marked_down_after_fault_s": 1.6,
"lb_marked_up_after_start_s": 3.62
}
@@ -0,0 +1,2 @@
{"mode": "stop", "target": "api-2", "target_instance": "33ea33bfe1fd", "restored_instance": "33ea33bfe1fd",
"load_start": 1792781881945, "fault": 1792781891997, "fault_done": 1792781897229, "restore_begin": 1792781906997, "restore_healthy": 1792781909498}
+25
View File
@@ -0,0 +1,25 @@
█ TOTAL RESULTS
CUSTOM
served_by......................: 6750 146.705085/s
HTTP
http_req_duration..............: avg=4.21ms med=1.89ms p(95)=5.05ms p(99)=25.58ms max=638.21ms
{ expected_response:true }...: avg=4.21ms med=1.89ms p(95)=5.05ms p(99)=25.58ms max=638.21ms
http_req_failed................: 0.00% 0 out of 6771
http_reqs......................: 6771 147.161501/s
EXECUTION
iteration_duration.............: avg=4.27ms med=2.09ms p(95)=5.24ms p(99)=13.83ms max=638.37ms
iterations.....................: 6750 146.705085/s
vus............................: 0 min=0 max=28
vus_max........................: 50 min=50 max=50
NETWORK
data_received..................: 2.6 MB 57 kB/s
data_sent......................: 1.4 MB 29 kB/s
@@ -0,0 +1,46 @@
second,ok,errors,p99_ms,max_ms,33ea33bfe1fd,ef8f943d313b
0,150,0,5.0,5.7,75,75
1,151,0,5.3,6.4,75,76
2,150,0,6.1,11.8,75,75
3,150,0,8.1,14.1,75,75
4,150,0,8.7,9.4,75,75
5,150,0,7.5,9.7,75,75
6,150,0,18.3,20.1,75,75
7,150,0,6.8,8.4,75,75
8,150,0,5.0,5.2,75,75
9,150,0,5.5,6.6,75,75
10,150,0,5.6,12.9,75,75
11,150,0,5.4,5.6,75,75
12,150,0,5.2,5.5,44,106
13,150,0,7.1,10.4,0,150
14,150,0,5.9,6.2,0,150
15,150,0,4.8,8.6,0,150
16,150,0,10.2,11.1,0,150
17,150,0,7.6,9.1,0,150
18,150,0,7.7,9.7,0,150
19,150,0,5.3,12.9,0,150
20,150,0,5.0,5.1,0,150
21,150,0,5.3,7.2,0,150
22,150,0,6.1,6.2,0,150
23,150,0,6.3,6.4,0,150
24,150,0,5.6,6.0,0,150
25,150,0,5.4,5.5,0,150
26,150,0,7.9,9.0,0,150
27,150,0,5.4,5.7,0,150
28,150,0,22.6,37.2,0,150
29,121,0,5.0,5.3,0,121
30,179,0,478.3,638.2,104,75
31,150,0,37.6,40.7,75,75
32,150,0,7.7,10.6,75,75
33,150,0,9.0,10.3,75,75
34,150,0,6.3,7.2,75,75
35,150,0,5.7,7.3,75,75
36,150,0,8.2,9.2,75,75
37,150,0,4.9,6.1,75,75
38,150,0,4.2,5.7,75,75
39,150,0,6.0,6.1,75,75
40,150,0,11.3,12.2,75,75
41,150,0,10.6,11.8,75,75
42,150,0,8.5,12.1,75,75
43,150,0,9.0,15.2,75,75
44,149,0,5.9,6.4,75,74
1 second ok errors p99_ms max_ms 33ea33bfe1fd ef8f943d313b
2 0 150 0 5.0 5.7 75 75
3 1 151 0 5.3 6.4 75 76
4 2 150 0 6.1 11.8 75 75
5 3 150 0 8.1 14.1 75 75
6 4 150 0 8.7 9.4 75 75
7 5 150 0 7.5 9.7 75 75
8 6 150 0 18.3 20.1 75 75
9 7 150 0 6.8 8.4 75 75
10 8 150 0 5.0 5.2 75 75
11 9 150 0 5.5 6.6 75 75
12 10 150 0 5.6 12.9 75 75
13 11 150 0 5.4 5.6 75 75
14 12 150 0 5.2 5.5 44 106
15 13 150 0 7.1 10.4 0 150
16 14 150 0 5.9 6.2 0 150
17 15 150 0 4.8 8.6 0 150
18 16 150 0 10.2 11.1 0 150
19 17 150 0 7.6 9.1 0 150
20 18 150 0 7.7 9.7 0 150
21 19 150 0 5.3 12.9 0 150
22 20 150 0 5.0 5.1 0 150
23 21 150 0 5.3 7.2 0 150
24 22 150 0 6.1 6.2 0 150
25 23 150 0 6.3 6.4 0 150
26 24 150 0 5.6 6.0 0 150
27 25 150 0 5.4 5.5 0 150
28 26 150 0 7.9 9.0 0 150
29 27 150 0 5.4 5.7 0 150
30 28 150 0 22.6 37.2 0 150
31 29 121 0 5.0 5.3 0 121
32 30 179 0 478.3 638.2 104 75
33 31 150 0 37.6 40.7 75 75
34 32 150 0 7.7 10.6 75 75
35 33 150 0 9.0 10.3 75 75
36 34 150 0 6.3 7.2 75 75
37 35 150 0 5.7 7.3 75 75
38 36 150 0 8.2 9.2 75 75
39 37 150 0 4.9 6.1 75 75
40 38 150 0 4.2 5.7 75 75
41 39 150 0 6.0 6.1 75 75
42 40 150 0 11.3 12.2 75 75
43 41 150 0 10.6 11.8 75 75
44 42 150 0 8.5 12.1 75 75
45 43 150 0 9.0 15.2 75 75
46 44 149 0 5.9 6.4 75 74
@@ -0,0 +1,26 @@
{
"requests": 6750,
"ok": 6750,
"errors": 0,
"error_codes": {},
"ok_rps": 150.0,
"p50_ms": 1.9,
"p95_ms": 5.0,
"p99_ms": 14.1,
"max_ms": 638.2,
"requests_per_instance": {
"ef8f943d313b": 4652,
"33ea33bfe1fd": 2098
},
"event_load_start_s": 1.03,
"event_fault_s": 11.08,
"event_fault_done_s": 16.32,
"event_restore_begin_s": 26.08,
"event_restore_healthy_s": 28.59,
"errors_after_fault": 0,
"last_error_after_fault_s": null,
"target_last_response_after_fault_s": 1.49,
"restored_replica_first_response_after_start_s": 3.99,
"lb_marked_down_after_fault_s": 1.5,
"lb_marked_up_after_start_s": 3.52
}
+1
View File
@@ -0,0 +1 @@
{"load_start": 1792782033873, "rolling_begin": 1792782043905, "rolling_end": 1792782074163}
+25
View File
@@ -0,0 +1,25 @@
█ TOTAL RESULTS
CUSTOM
served_by......................: 10500 148.071162/s
HTTP
http_req_duration..............: avg=4.42ms med=1.91ms p(95)=6.51ms p(99)=49.84ms max=390.14ms
{ expected_response:true }...: avg=4.42ms med=1.91ms p(95)=6.51ms p(99)=49.84ms max=390.14ms
http_req_failed................: 0.00% 0 out of 10521
http_reqs......................: 10521 148.367304/s
EXECUTION
iteration_duration.............: avg=4.52ms med=2.08ms p(95)=6.57ms p(99)=46.81ms max=390.44ms
iterations.....................: 10500 148.071162/s
vus............................: 0 min=0 max=4
vus_max........................: 50 min=50 max=50
NETWORK
data_received..................: 4.1 MB 57 kB/s
data_sent......................: 2.1 MB 30 kB/s
@@ -0,0 +1,71 @@
second,ok,errors,p99_ms,max_ms,05feed80156e,33ea33bfe1fd,5bd75b122d67,ef8f943d313b
0,151,0,7.0,7.5,0,75,0,76
1,150,0,4.9,5.2,0,75,0,75
2,150,0,8.2,10.0,0,75,0,75
3,150,0,7.2,8.1,0,75,0,75
4,150,0,4.1,4.3,0,75,0,75
5,150,0,5.1,5.3,0,75,0,75
6,150,0,5.6,6.1,0,75,0,75
7,148,0,42.6,46.7,0,74,0,74
8,152,0,26.0,32.5,0,76,0,76
9,150,0,30.0,39.6,0,75,0,75
10,150,0,20.6,25.4,0,75,0,75
11,149,0,382.2,390.1,0,75,0,74
12,151,0,49.4,52.9,0,75,0,76
13,150,0,9.2,12.4,0,75,0,75
14,149,0,7.4,8.7,0,75,0,74
15,151,0,43.0,44.2,0,75,0,76
16,150,0,8.3,11.0,0,75,0,75
17,149,0,23.8,26.5,0,74,0,75
18,151,0,99.7,111.5,0,76,0,75
19,150,0,11.7,17.6,0,75,0,75
20,150,0,8.7,9.3,0,75,0,75
21,150,0,4.6,4.7,0,75,0,75
22,150,0,11.3,14.6,0,75,0,75
23,150,0,4.7,4.8,0,75,0,75
24,150,0,4.3,7.6,0,75,0,75
25,145,0,263.8,284.0,0,65,16,64
26,155,0,182.9,259.5,0,50,55,50
27,150,0,6.7,7.0,0,59,60,31
28,150,0,13.8,15.4,0,75,75,0
29,150,0,6.1,6.5,0,75,75,0
30,150,0,6.7,9.6,0,75,75,0
31,150,0,8.1,9.0,0,75,75,0
32,150,0,6.5,7.3,0,75,75,0
33,150,0,4.3,4.4,0,75,75,0
34,135,0,7.2,10.3,0,68,67,0
35,165,0,325.1,348.3,65,50,50,0
36,150,0,6.7,8.2,54,42,54,0
37,149,0,4.8,11.0,75,0,74,0
38,150,0,16.4,18.0,75,0,75,0
39,151,0,10.1,14.6,75,0,76,0
40,150,0,12.2,12.3,75,0,75,0
41,150,0,6.4,7.1,75,0,75,0
42,150,0,5.2,9.0,75,0,75,0
43,150,0,7.9,8.0,75,0,75,0
44,150,0,4.4,4.9,75,0,75,0
45,150,0,4.9,8.7,75,0,75,0
46,150,0,11.5,15.5,75,0,75,0
47,150,0,7.4,9.3,75,0,75,0
48,150,0,5.7,7.4,75,0,75,0
49,150,0,5.0,5.3,75,0,75,0
50,150,0,8.5,9.1,75,0,75,0
51,150,0,5.7,6.6,75,0,75,0
52,150,0,5.0,6.1,75,0,75,0
53,150,0,5.0,5.3,75,0,75,0
54,150,0,11.3,13.3,75,0,75,0
55,150,0,5.7,6.7,75,0,75,0
56,150,0,5.1,7.2,75,0,75,0
57,150,0,5.6,5.7,75,0,75,0
58,150,0,4.2,4.4,75,0,75,0
59,150,0,5.0,5.1,75,0,75,0
60,150,0,8.9,9.7,75,0,75,0
61,150,0,8.0,8.2,75,0,75,0
62,150,0,5.4,5.4,75,0,75,0
63,150,0,4.0,5.5,75,0,75,0
64,150,0,3.5,4.1,75,0,75,0
65,150,0,5.4,5.5,75,0,75,0
66,150,0,5.6,5.6,75,0,75,0
67,150,0,5.4,6.0,75,0,75,0
68,150,0,5.5,6.5,75,0,75,0
69,149,0,5.0,5.6,75,0,74,0
1 second ok errors p99_ms max_ms 05feed80156e 33ea33bfe1fd 5bd75b122d67 ef8f943d313b
2 0 151 0 7.0 7.5 0 75 0 76
3 1 150 0 4.9 5.2 0 75 0 75
4 2 150 0 8.2 10.0 0 75 0 75
5 3 150 0 7.2 8.1 0 75 0 75
6 4 150 0 4.1 4.3 0 75 0 75
7 5 150 0 5.1 5.3 0 75 0 75
8 6 150 0 5.6 6.1 0 75 0 75
9 7 148 0 42.6 46.7 0 74 0 74
10 8 152 0 26.0 32.5 0 76 0 76
11 9 150 0 30.0 39.6 0 75 0 75
12 10 150 0 20.6 25.4 0 75 0 75
13 11 149 0 382.2 390.1 0 75 0 74
14 12 151 0 49.4 52.9 0 75 0 76
15 13 150 0 9.2 12.4 0 75 0 75
16 14 149 0 7.4 8.7 0 75 0 74
17 15 151 0 43.0 44.2 0 75 0 76
18 16 150 0 8.3 11.0 0 75 0 75
19 17 149 0 23.8 26.5 0 74 0 75
20 18 151 0 99.7 111.5 0 76 0 75
21 19 150 0 11.7 17.6 0 75 0 75
22 20 150 0 8.7 9.3 0 75 0 75
23 21 150 0 4.6 4.7 0 75 0 75
24 22 150 0 11.3 14.6 0 75 0 75
25 23 150 0 4.7 4.8 0 75 0 75
26 24 150 0 4.3 7.6 0 75 0 75
27 25 145 0 263.8 284.0 0 65 16 64
28 26 155 0 182.9 259.5 0 50 55 50
29 27 150 0 6.7 7.0 0 59 60 31
30 28 150 0 13.8 15.4 0 75 75 0
31 29 150 0 6.1 6.5 0 75 75 0
32 30 150 0 6.7 9.6 0 75 75 0
33 31 150 0 8.1 9.0 0 75 75 0
34 32 150 0 6.5 7.3 0 75 75 0
35 33 150 0 4.3 4.4 0 75 75 0
36 34 135 0 7.2 10.3 0 68 67 0
37 35 165 0 325.1 348.3 65 50 50 0
38 36 150 0 6.7 8.2 54 42 54 0
39 37 149 0 4.8 11.0 75 0 74 0
40 38 150 0 16.4 18.0 75 0 75 0
41 39 151 0 10.1 14.6 75 0 76 0
42 40 150 0 12.2 12.3 75 0 75 0
43 41 150 0 6.4 7.1 75 0 75 0
44 42 150 0 5.2 9.0 75 0 75 0
45 43 150 0 7.9 8.0 75 0 75 0
46 44 150 0 4.4 4.9 75 0 75 0
47 45 150 0 4.9 8.7 75 0 75 0
48 46 150 0 11.5 15.5 75 0 75 0
49 47 150 0 7.4 9.3 75 0 75 0
50 48 150 0 5.7 7.4 75 0 75 0
51 49 150 0 5.0 5.3 75 0 75 0
52 50 150 0 8.5 9.1 75 0 75 0
53 51 150 0 5.7 6.6 75 0 75 0
54 52 150 0 5.0 6.1 75 0 75 0
55 53 150 0 5.0 5.3 75 0 75 0
56 54 150 0 11.3 13.3 75 0 75 0
57 55 150 0 5.7 6.7 75 0 75 0
58 56 150 0 5.1 7.2 75 0 75 0
59 57 150 0 5.6 5.7 75 0 75 0
60 58 150 0 4.2 4.4 75 0 75 0
61 59 150 0 5.0 5.1 75 0 75 0
62 60 150 0 8.9 9.7 75 0 75 0
63 61 150 0 8.0 8.2 75 0 75 0
64 62 150 0 5.4 5.4 75 0 75 0
65 63 150 0 4.0 5.5 75 0 75 0
66 64 150 0 3.5 4.1 75 0 75 0
67 65 150 0 5.4 5.5 75 0 75 0
68 66 150 0 5.6 5.6 75 0 75 0
69 67 150 0 5.4 6.0 75 0 75 0
70 68 150 0 5.5 6.5 75 0 75 0
71 69 149 0 5.0 5.6 75 0 74 0
+20
View File
@@ -0,0 +1,20 @@
{
"requests": 10500,
"ok": 10500,
"errors": 0,
"error_codes": {},
"ok_rps": 150.0,
"p50_ms": 1.9,
"p95_ms": 6.3,
"p99_ms": 46.7,
"max_ms": 390.1,
"requests_per_instance": {
"ef8f943d313b": 2021,
"33ea33bfe1fd": 2659,
"5bd75b122d67": 3226,
"05feed80156e": 2594
},
"event_load_start_s": 0.11,
"event_rolling_begin_s": 10.14,
"event_rolling_end_s": 40.4
}
+24
View File
@@ -0,0 +1,24 @@
# before
NAME STATE STATUS
mtusi-messenger-lab3-api-1 running Up 36 seconds (healthy)
mtusi-messenger-lab3-api-2 running Up 7 seconds (healthy)
mtusi-messenger-lab3-lb-1 running Up About a minute (healthy)
mtusi-messenger-lab3-migrate-1 exited Exited (0) 4 seconds ago
mtusi-messenger-lab3-postgres-1 running Up About a minute (healthy)
mtusi-messenger-lab3-redis-1 running Up About a minute (healthy)
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-23T19:03:13.056739134Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T19:03:42.093218675Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T19:02:03.051437129Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T19:01:50.139036512Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T19:01:50.133911762Z
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: every container has a new StartedAt, volume postgres-data is kept
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-23T19:04:06.150995464Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-23T19:04:06.224890922Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-23T19:04:08.799986382Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-23T19:03:55.446885876Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-23T19:03:55.448259126Z
mtusi-messenger-lab3_postgres-data
+53
View File
@@ -0,0 +1,53 @@
# 2026-10-23T18:55Z scripts/revocation-pause.sh against the compose stack (2 replicas behind HAProxy)
# replica ids: ef8f943d313b 33ea33bfe1fd
$ PAUSE_MS=5000 scripts/revocation-pause.sh
send message with Idempotency-Key: 201 33ea33bfe1fd
warm the session on both replicas:
GET /users/me -> 200 ef8f943d313b
GET /users/me -> 200 33ea33bfe1fd
GET /users/me -> 200 ef8f943d313b
GET /users/me -> 200 33ea33bfe1fd
redis: CLIENT PAUSE 5000 WRITE -> OK
DELETE /auth/sessions/current -> 204 ef8f943d313b after 5.1 s
old token after logout (redis writes still paused: no):
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
POST message, same key -> 401 33ea33bfe1fd
POST message, same key -> 401 ef8f943d313b
after the pause: session key in redis = revoked
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
$ PAUSE_MS=12000 scripts/revocation-pause.sh
send message with Idempotency-Key: 201 33ea33bfe1fd
warm the session on both replicas:
GET /users/me -> 200 ef8f943d313b
GET /users/me -> 200 33ea33bfe1fd
GET /users/me -> 200 ef8f943d313b
GET /users/me -> 200 33ea33bfe1fd
redis: CLIENT PAUSE 12000 WRITE -> OK
DELETE /auth/sessions/current -> 503 ef8f943d313b after 8.63 s
old token after logout (redis writes still paused: yes):
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
POST message, same key -> 401 33ea33bfe1fd
POST message, same key -> 401 ef8f943d313b
after the pause: session key in redis = revoked
GET /users/me -> 401 33ea33bfe1fd
GET /users/me -> 401 ef8f943d313b
# API log: pending revocation seen by the replicas
api-1 2026-10-23T18:55:37.498Z Pending session revocations found, cached sessions are not trusted until they are written
api-1 2026-10-23T18:55:42.419Z Wrote 1 pending session revocations to the cache
api-1 2026-10-23T18:55:45.442Z Pending session revocations found, cached sessions are not trusted until they are written
api-1 2026-10-23T18:55:57.192Z Wrote 1 pending session revocations to the cache
api-2 2026-10-23T18:55:23.552Z Pending session revocations found, cached sessions are not trusted until they are written
api-2 2026-10-23T18:55:37.425Z Pending session revocations found, cached sessions are not trusted until they are written
api-2 2026-10-23T18:55:42.420Z Wrote 1 pending session revocations to the cache
api-2 2026-10-23T18:55:45.442Z Pending session revocations found, cached sessions are not trusted until they are written
api-2 2026-10-23T18:55:57.193Z Wrote 1 pending session revocations to the cache
+2
View File
@@ -0,0 +1,2 @@
$ make test
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
+1
View File
@@ -0,0 +1 @@
kotlin.code.style=official
+54
View File
@@ -0,0 +1,54 @@
[versions]
kotlin = "2.3.20"
ksp = "2.3.0"
kotlinx-serialization = "1.10.0"
kotlinx-coroutines = "1.9.0"
logback = "1.5.13"
shadow = "8.3.5"
koin = "4.1.0-RC1"
koin-ktor = "4.1.0-Beta8"
koin-compiler = "2.0.0"
exposed = "1.0.0"
postgres = "42.7.3"
hikaricp = "6.3.0"
flyway = "11.9.0"
lettuce = "6.7.1.RELEASE"
bouncycastle = "1.81"
test-containers = "1.21.0"
[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
[libraries]
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
kotlinx-serialization-hocon = { module = "org.jetbrains.kotlinx:kotlinx-serialization-hocon", version.ref = "kotlinx-serialization" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-ktor = { module = "io.insert-koin:koin-ktor3", version.ref = "koin-ktor" }
koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-compiler" }
koin-compiler = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koin-compiler" }
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" }
exposed-core = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "exposed" }
exposed-kotlin-datetime = { module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed" }
exposed-migration-core = { module = "org.jetbrains.exposed:exposed-migration-core", version.ref = "exposed" }
exposed-migration-jdbc = { module = "org.jetbrains.exposed:exposed-migration-jdbc", version.ref = "exposed" }
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
hikaricp = { module = "com.zaxxer:HikariCP", version.ref = "hikaricp" }
flyway = { module = "org.flywaydb:flyway-core", version.ref = "flyway" }
flyway-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version.ref = "flyway" }
lettuce = { module = "io.lettuce:lettuce-core", version.ref = "lettuce" }
bouncycastle = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncycastle" }
testContainers = { module = "org.testcontainers:testcontainers", version.ref = "test-containers" }
testContainers-postgresql = { module = "org.testcontainers:postgresql", version.ref = "test-containers" }
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Vendored Executable
+251
View File
@@ -0,0 +1,251 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH="\\\"\\\""
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"
Vendored
+94
View File
@@ -0,0 +1,94 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
set CLASSPATH=
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
+57
View File
@@ -0,0 +1,57 @@
# Single public entry point in front of the API replicas.
# Backends are discovered through Docker DNS (service name `api`) and receive traffic only while /health/ready is 200.
global
log stdout format raw local0 info
# Without it HAProxy sizes buffers from the file descriptor limit and uses ~100 MB.
maxconn 4096
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 1s
timeout client 30s
timeout server 10s
timeout http-request 10s
timeout http-keep-alive 5s
# Retried only when the connection to a replica fails, i.e. before the request was sent:
# a POST that reached a replica is never repeated by the balancer.
retries 2
option redispatch 1
resolvers docker
nameserver dns 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold valid 2s
hold other 2s
hold refused 2s
hold nx 2s
hold timeout 2s
hold obsolete 2s
accepted_payload_size 8192
frontend http
bind :8080
default_backend api
# Not published: container healthcheck and the backend table (wget -qO- "http://127.0.0.1:8404/stats;csv").
frontend internal
bind :8404
monitor-uri /healthz
stats enable
stats uri /stats
stats refresh 5s
backend api
balance roundrobin
option httpchk GET /health/ready
http-check expect status 200
# A replica leaves rotation after 2 failed checks 1 s apart and returns after 2 successful ones.
default-server check inter 1s fall 2 rise 2 resolvers docker init-addr none
# Up to 4 replicas; slots without a resolved address stay in maintenance.
server-template api 4 api:8080
+12
View File
@@ -0,0 +1,12 @@
ktor {
application {
modules = [com.mtusi.messenger.main.AppKt.module]
}
deployment {
host = "127.0.0.1"
port = 8080
}
}
+9
View File
@@ -0,0 +1,9 @@
authentication {
realm = "messenger"
sessionTtlSeconds = 3600
sessionCacheTtlSeconds = 60
# Collapse concurrent cache misses for one token into a single database lookup
sessionCacheSingleFlight = true
# A logout answers 204 only after its tombstone is in Redis; retries up to this long, then 503
sessionRevokeTimeoutMs = 8000
}
+23
View File
@@ -0,0 +1,23 @@
cors {
# Defaults to false
#allowAnyHost = true
# Swagger UI sends "Try it out" to the contract server http://localhost:8080; pages opened elsewhere need these.
allowedHosts = [
{ host = "localhost:8080", schemes = ["http"] }
{ host = "localhost:8081", schemes = ["http"] }
{ host = "127.0.0.1:8080", schemes = ["http"] }
{ host = "127.0.0.1:8081", schemes = ["http"] }
]
# Defaults to true
#allowAnyMethod = true
#allowedMethods = [GET, POST, PATCH, PUT, DELETE, OPTIONS]
allowAnyHeader = true
#allowedHeaders = []
exposedHeaders = [X-Request-Id, X-Instance-Id, WWW-Authenticate]
# Browsers send Origin on same-origin POST too, e.g. Swagger UI served by the API itself.
allowSameOrigin = true
}
+12
View File
@@ -0,0 +1,12 @@
database {
host = "localhost"
port = 5432
name = "messenger"
username = "username"
password = "password"
maxPoolSize = 10
connectionTimeoutMs = 2000
statementTimeoutMs = 5000
# A single local process migrates itself; replicas in compose leave it to the `migrate` service
migrateOnStart = true
}
+4
View File
@@ -0,0 +1,4 @@
instance {
# X-Instance-Id of this process. Defaults to HOSTNAME (the short container id in Docker), else a random alias
#id = "dev"
}
+4
View File
@@ -0,0 +1,4 @@
messages {
# How long a stored Idempotency-Key answers a retry with the original message (contract: at least 24 h)
idempotencyTtlSeconds = 86400
}
+8
View File
@@ -0,0 +1,8 @@
redis {
host = "localhost"
port = 6379
# Only if the local Redis requires AUTH
#password = ""
connectTimeoutMs = 500
commandTimeoutMs = 500
}
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
# Runs EXPLAIN (ANALYZE, BUFFERS) of the chat history query against the Compose PostgreSQL on 70 000 synthetic
# messages. The data is inserted and rolled back in one transaction; the plans are saved to evidence/.
set -eu
cd "$(dirname "$0")/.."
mkdir -p evidence
docker compose exec -T postgres psql -U messenger -d messenger -X -q < scripts/sql/explain-history.sql \
| tee evidence/explain-history.txt
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""Sends one message with the SAME Idempotency-Key many times in parallel through the balancer.
Expects every response to be 201 with the same message, at least two replicas answering (X-Instance-Id)
and exactly one message in the chat history. Synthetic users only; the token stays in memory.
"""
import argparse
import base64
import json
import threading
import urllib.error
import urllib.request
import uuid
from collections import Counter
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default='http://localhost:8080')
parser.add_argument('--parallel', type=int, default=32)
parser.add_argument('--rounds', type=int, default=20)
args = parser.parse_args()
def call(method, path, body=None, headers=None):
data = json.dumps(body).encode() if body is not None else None
request = urllib.request.Request(args.base_url + path, data=data, method=method, headers={
'Content-Type': 'application/json', **(headers or {})})
try:
with urllib.request.urlopen(request, timeout=10) as response:
return response.status, response.headers, json.loads(response.read() or b'null')
except urllib.error.HTTPError as error:
return error.code, error.headers, json.loads(error.read() or b'null')
username = 'race_' + uuid.uuid4().hex[:12]
password = 'race-password-' + uuid.uuid4().hex
status, _, user = call('POST', '/api/v1/users', {'username': username, 'display_name': 'race', 'password': password})
assert status == 201, status
basic = base64.b64encode(f'{username}:{password}'.encode()).decode()
status, _, session = call('POST', '/api/v1/auth/sessions', headers={'Authorization': 'Basic ' + basic})
assert status == 201, status
auth = {'Authorization': 'Bearer ' + session['token']}
status, _, chat = call('POST', '/api/v1/chats', {'title': 'race', 'member_ids': [user['id']]}, auth)
assert status == 201, status
path = f"/api/v1/chats/{chat['id']}/messages"
statuses, instances, violations = Counter(), Counter(), 0
for round_number in range(args.rounds):
key = uuid.uuid4().hex
results = []
barrier = threading.Barrier(args.parallel)
def send():
barrier.wait()
results.append(call('POST', path, {'text': f'race {round_number}'}, {**auth, 'Idempotency-Key': key}))
threads = [threading.Thread(target=send) for _ in range(args.parallel)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
ids = {body['id'] for status, _, body in results if status == 201}
for status, headers, _ in results:
statuses[status] += 1
instances[headers.get('X-Instance-Id', '-')] += 1
if len(ids) != 1:
violations += 1
status, _, page = call('GET', path + '?limit=100', headers=auth)
texts = [m['text'] for m in page['items']]
print(json.dumps({
'rounds': args.rounds,
'parallel_requests_per_round': args.parallel,
'statuses': dict(statuses),
'requests_per_instance': dict(instances),
'rounds_with_more_than_one_message_id': violations,
'messages_in_history': len(texts),
'distinct_texts_in_history': len(set(texts)),
}, indent=2))
ok = violations == 0 and len(texts) == args.rounds == len(set(texts)) and len(instances) >= 2 and set(statuses) == {201}
print('PASS' if ok else 'FAIL')
raise SystemExit(0 if ok else 1)
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
# Creates local secrets for the Compose stack. Never overwrites an existing secret:
# the PostgreSQL volume is initialised with the first password and keeps it.
set -eu
cd "$(dirname "$0")/.."
mkdir -p secrets
# Readable by the non-root users inside the containers (bind-mounted as is).
umask 022
for name in db_password redis_password; do
if [ ! -s "secrets/$name" ]; then
openssl rand -hex 24 > "secrets/$name"
echo "created secrets/$name"
else
echo "secrets/$name already exists, keeping it"
fi
done
+55
View File
@@ -0,0 +1,55 @@
#!/bin/sh
# One replica fault under constant load through the balancer, then its restoration.
# MODE=stop docker stop (SIGTERM: drain, graceful shutdown) MODE=kill docker kill (SIGKILL: crash)
# TARGET=api-1|api-2 RATE=150 req/s DURATION=45 s fault at 10 s, restore at 25 s
# Output: evidence/load/failover-<mode>-<target>/{k6.txt,events.json,haproxy.log,per-second.csv,summary.json}
set -eu
cd "$(dirname "$0")/../.."
MODE=${MODE:-stop}
TARGET=${TARGET:-api-1}
RATE=${RATE:-150}
DURATION=${DURATION:-45}
FAULT_AT=${FAULT_AT:-10}
RESTORE_AT=${RESTORE_AT:-25}
out=evidence/load/failover-$MODE-$TARGET
rm -rf "$out" && mkdir -p "$out"
now() { python3 -c 'import time; print(int(time.time() * 1000))'; }
# Sleeps until <seconds> after the load started, so the schedule does not drift with slow docker commands.
sleep_until() { python3 -c "import time; time.sleep(max(0, $start / 1000 + $1 - time.time()))"; }
container=mtusi-messenger-lab3-$TARGET
target_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container")
since=$(date -u +%Y-%m-%dT%H:%M:%SZ)
K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \
-e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 &
k6_pid=$!
# Load starts after k6 setup (users, sessions, chat): wait for the first request of the scenario.
until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done
start=$(now)
sleep_until "$FAULT_AT"
fault=$(now)
if [ "$MODE" = kill ]; then docker kill "$container" >/dev/null; else docker stop --time 9 "$container" >/dev/null; fi
fault_done=$(now)
sleep_until "$RESTORE_AT"
restore_begin=$(now)
docker start "$container" >/dev/null
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$container")" = healthy ]; do sleep 0.2; done
restore_healthy=$(now)
restored_instance=$(docker inspect -f '{{.Config.Hostname}}' "$container")
wait "$k6_pid" || true
docker compose logs --timestamps --no-log-prefix --since "$since" lb 2>/dev/null | grep -E 'Server api/|SH--|5[0-9][0-9] ' > "$out/haproxy.log" || true
cat > "$out/events.json" <<JSON
{"mode": "$MODE", "target": "$TARGET", "target_instance": "$target_instance", "restored_instance": "$restored_instance",
"load_start": $start, "fault": $fault, "fault_done": $fault_done, "restore_begin": $restore_begin, "restore_healthy": $restore_healthy}
JSON
python3 scripts/load/summarize.py "$out" > /dev/null
# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis.
[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv"
cat "$out/summary.json"
+67
View File
@@ -0,0 +1,67 @@
// Open-model load through the balancer: RATE requests per second regardless of response time.
// 70 % GET /users/me (session lookup), 30 % POST message with a unique Idempotency-Key.
// Every response adds a `served_by` sample tagged with X-Instance-Id and status, so the summary can show
// which replica answered each second. Tokens live only in this process.
import http from 'k6/http';
import encoding from 'k6/encoding';
import { Counter } from 'k6/metrics';
const BASE = __ENV.BASE_URL || 'http://localhost:8080';
const RATE = Number(__ENV.RATE || 150);
const DURATION = __ENV.DURATION || '40s';
const USERS = 10;
const PASSWORD = 'load-test-password';
const servedBy = new Counter('served_by');
export const options = {
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: RATE,
timeUnit: '1s',
duration: DURATION,
preAllocatedVUs: 50,
maxVUs: 300,
},
},
summaryTrendStats: ['avg', 'med', 'p(95)', 'p(99)', 'max'],
};
export function setup() {
const json = { headers: { 'Content-Type': 'application/json' } };
const users = [];
for (let i = 0; i < USERS; i++) {
const username = `load_${Date.now().toString(36)}_${i}`;
const user = http.post(`${BASE}/api/v1/users`, JSON.stringify({ username, display_name: 'load', password: PASSWORD }), json).json();
const basic = encoding.b64encode(`${username}:${PASSWORD}`);
const session = http.post(`${BASE}/api/v1/auth/sessions`, null, { headers: { Authorization: `Basic ${basic}` } }).json();
users.push({ id: user.id, token: session.token });
}
const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ title: 'load', member_ids: users.map((u) => u.id) }), {
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${users[0].token}` },
}).json();
return { users, chatId: chat.id };
}
export default function (data) {
const user = data.users[Math.floor(Math.random() * data.users.length)];
const auth = { Authorization: `Bearer ${user.token}` };
let response;
if (Math.random() < 0.7) {
response = http.get(`${BASE}/api/v1/users/me`, { headers: auth, tags: { name: 'me' }, timeout: '5s' });
} else {
response = http.post(
`${BASE}/api/v1/chats/${data.chatId}/messages`,
JSON.stringify({ text: `load ${__VU}-${__ITER}` }),
{
headers: { ...auth, 'Content-Type': 'application/json', 'Idempotency-Key': `load-${__VU}-${__ITER}-${Date.now()}` },
tags: { name: 'send' },
timeout: '5s',
},
);
}
servedBy.add(1, { instance: response.headers['X-Instance-Id'] || 'none', code: String(response.status) });
}
+34
View File
@@ -0,0 +1,34 @@
#!/bin/sh
# scripts/rolling-update.sh while k6 keeps RATE req/s through the balancer.
# Output: evidence/load/rolling-update/{k6.txt,rolling.log,events.json,per-second.csv,summary.json}
set -eu
cd "$(dirname "$0")/../.."
RATE=${RATE:-150}
DURATION=${DURATION:-70}
out=evidence/load/rolling-update
rm -rf "$out" && mkdir -p "$out"
now() { python3 -c 'import time; print(int(time.time() * 1000))'; }
# Build outside the measured window: compiling would compete with the API for CPU.
docker compose build --quiet api
{ echo "# before"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } > "$out/rolling.log"
K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --out csv="$out/requests.csv" \
-e RATE="$RATE" -e DURATION="${DURATION}s" scripts/load/mixed.js > "$out/k6.txt" 2>&1 &
k6_pid=$!
until grep -q ',load,' "$out/requests.csv" 2>/dev/null; do sleep 0.2; done
start=$(now)
sleep 10
begin=$(now)
scripts/rolling-update.sh >> "$out/rolling.log" 2>&1
end=$(now)
wait "$k6_pid" || true
{ echo "# after"; docker compose ps api --format '{{.Name}} {{.ID}} {{.Image}}'; } >> "$out/rolling.log"
echo "{\"load_start\": $start, \"rolling_begin\": $begin, \"rolling_end\": $end}" > "$out/events.json"
python3 scripts/load/summarize.py "$out" > /dev/null
# Raw per-request data stays out of Git (evidence keeps aggregates only); KEEP_RAW=1 keeps it for analysis.
[ "${KEEP_RAW:-0}" = 1 ] || rm -f "$out/requests.csv"
cat "$out/rolling.log" "$out/summary.json"
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env python3
"""Per-second view of one k6 run (requests.csv) plus the events of the experiment (events.json).
Writes per-second.csv and summary.json next to the input. An error is any response that is not 2xx
(including k6 status 0: connection reset or timeout). Times are seconds from the start of the load.
"""
import csv
import json
import math
import sys
from collections import Counter, defaultdict
from pathlib import Path
from urllib.parse import parse_qsl
run = Path(sys.argv[1])
events = json.loads((run / 'events.json').read_text()) if (run / 'events.json').exists() else {}
durations = [] # (t_ms, duration_ms, status)
served = [] # (t_ms, instance, status)
with open(run / 'requests.csv') as f:
for row in csv.DictReader(f):
if row['metric_name'] == 'http_req_duration' and row['scenario'] == 'load':
durations.append((int(row['timestamp']), float(row['metric_value']), int(row['status'] or 0)))
elif row['metric_name'] == 'served_by':
tags = dict(parse_qsl(row['extra_tags']))
served.append((int(row['timestamp']), tags.get('instance', 'none'), int(tags.get('code', 0))))
t0 = min(t for t, _, _ in durations)
rel = lambda ms: (ms - t0) / 1000
def percentile(values, p):
if not values:
return None
values = sorted(values)
return values[min(len(values) - 1, math.ceil(p / 100 * len(values)) - 1)]
per_second = defaultdict(lambda: {'ok': 0, 'errors': 0, 'durations': [], 'instances': Counter()})
for t, d, status in durations:
second = per_second[math.floor(rel(t))]
second['durations'].append(d)
if 200 <= status < 300:
second['ok'] += 1
else:
second['errors'] += 1
for t, instance, status in served:
if 200 <= status < 300:
per_second[math.floor(rel(t))]['instances'][instance] += 1
instances = sorted({i for s in per_second.values() for i in s['instances']})
with open(run / 'per-second.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['second', 'ok', 'errors', 'p99_ms', 'max_ms'] + instances)
for second in sorted(per_second):
s = per_second[second]
writer.writerow([second, s['ok'], s['errors'], round(percentile(s['durations'], 99), 1), round(max(s['durations']), 1)]
+ [s['instances'].get(i, 0) for i in instances])
errors = sorted(rel(t) for t, _, status in durations if not 200 <= status < 300)
seconds = max(per_second) + 1
summary = {
'requests': len(durations),
'ok': len(durations) - len(errors),
'errors': len(errors),
'error_codes': dict(Counter(str(status) for _, _, status in durations if not 200 <= status < 300)),
'ok_rps': round((len(durations) - len(errors)) / seconds, 1),
'p50_ms': round(percentile([d for _, d, _ in durations], 50), 1),
'p95_ms': round(percentile([d for _, d, _ in durations], 95), 1),
'p99_ms': round(percentile([d for _, d, _ in durations], 99), 1),
'max_ms': round(max(d for _, d, _ in durations), 1),
'requests_per_instance': dict(Counter(i for _, i, status in served if 200 <= status < 300)),
}
for name, value in events.items():
if isinstance(value, (int, float)):
summary[f'event_{name}_s'] = round(rel(value), 2)
if 'fault' in events:
fault = rel(events['fault'])
after = [e - fault for e in errors if e >= fault - 0.5]
summary['errors_after_fault'] = len(after)
summary['last_error_after_fault_s'] = round(max(after), 2) if after else None
target = events.get('target_instance')
end = rel(events['restore_begin']) if 'restore_begin' in events else float('inf')
last = [rel(t) - fault for t, i, status in served if i == target and fault <= rel(t) < end]
summary['target_last_response_after_fault_s'] = round(max(last), 2) if last else None
if 'restore_begin' in events and events.get('restored_instance'):
begin = rel(events['restore_begin'])
first = [rel(t) - begin for t, i, status in served
if i == events['restored_instance'] and 200 <= status < 300 and rel(t) >= begin]
summary['restored_replica_first_response_after_start_s'] = round(min(first), 2) if first else None
# HAProxy state changes (docker log timestamps): when the balancer took the target out and put it back.
log = run / 'haproxy.log'
if log.exists() and 'fault' in events:
from datetime import datetime
def at(line):
stamp = line.split()[0].rstrip('Z')
whole, _, fraction = stamp.partition('.')
return datetime.fromisoformat(whole + '+00:00').timestamp() * 1000 + float('0.' + (fraction or '0')) * 1000
changes = [(at(line), line) for line in log.read_text().splitlines() if 'Server api/' in line and '[WARNING]' not in line]
down = [t for t, line in changes if ('is DOWN' in line or 'going DOWN' in line) and t >= events['fault']]
summary['lb_marked_down_after_fault_s'] = round((min(down) - events['fault']) / 1000, 2) if down else None
if 'restore_begin' in events:
up = [t for t, line in changes if 'is UP, reason' in line and t >= events['restore_begin']]
summary['lb_marked_up_after_start_s'] = round((min(up) - events['restore_begin']) / 1000, 2) if up else None
(run / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False))
print(json.dumps(summary, indent=2, ensure_ascii=False))
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
# Restarts the balancer, every API replica, PostgreSQL and Redis, keeping the named volume (no `down -v`, no re-seed).
set -eu
cd "$(dirname "$0")/.."
docker compose stop lb api redis postgres
docker compose up -d --wait
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# Reproduces the review scenario on the running stack, everything through the balancer:
# session + message, warm the session on both replicas, CLIENT PAUSE <ms> WRITE on Redis (reads keep working),
# logout, then read and repeat the original message with the old token and key.
# Expected: logout is 204 only once the tombstone is in Redis; afterwards every replica answers 401.
# PAUSE_MS=5000 (default): the logout waits for the pause and answers 204.
# PAUSE_MS=12000: longer than the logout deadline (8 s): the logout answers 503 and every replica still rejects the token.
set -euo pipefail
cd "$(dirname "$0")/.."
PAUSE_MS=${PAUSE_MS:-5000}
B=${BASE_URL:-http://localhost:8080}/api/v1
U=pause_$RANDOM$RANDOM
PW=pause-test-password
KEY=pause-$RANDOM$RANDOM
redis() { docker compose exec -T redis redis-cli --no-auth-warning -a "$(cat secrets/redis_password)" "$@"; }
now() { python3 -c 'import time; print(time.time())'; }
status() { curl -s -o /dev/null -w '%{http_code} %header{x-instance-id}' "$@"; }
curl -s -o /dev/null -X POST "$B/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$U\",\"display_name\":\"pause\",\"password\":\"$PW\"}"
T=$(curl -s -X POST "$B/auth/sessions" -u "$U:$PW" | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])')
ME=$(curl -s "$B/users/me" -H "Authorization: Bearer $T" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
CHAT=$(curl -s -X POST "$B/chats" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \
-d "{\"title\":\"pause\",\"member_ids\":[\"$ME\"]}" | python3 -c 'import sys,json;print(json.load(sys.stdin)["id"])')
send() { status -X POST "$B/chats/$CHAT/messages" -H "Authorization: Bearer $T" -H 'Content-Type: application/json' \
-H "Idempotency-Key: $KEY" -d '{"text":"original message"}'; }
echo "send message with Idempotency-Key: $(send)"
echo "warm the session on both replicas:"
for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done
echo "redis: CLIENT PAUSE $PAUSE_MS WRITE -> $(redis CLIENT PAUSE "$PAUSE_MS" WRITE)"
start=$(now)
echo "DELETE /auth/sessions/current -> $(status -X DELETE "$B/auth/sessions/current" -H "Authorization: Bearer $T") after $(python3 -c "print(round($(now) - $start, 2))") s"
sleep 1.5 # replicas check pending revocations once a second; only matters for the unconfirmed (503) case
echo "old token after logout (redis writes still paused: $(python3 -c "print('yes' if $(now) - $start < $PAUSE_MS / 1000 else 'no')")):"
for i in 1 2 3 4; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done
for i in 1 2; do echo " POST message, same key -> $(send)"; done
python3 -c "import time; time.sleep(max(0, $start + $PAUSE_MS / 1000 + 2 - time.time()))"
echo "after the pause: session key in redis = $(redis GET "session:$(printf %s "$T" | shasum -a 256 | cut -d' ' -f1)")"
for i in 1 2; do echo " GET /users/me -> $(status "$B/users/me" -H "Authorization: Bearer $T")"; done
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Replaces the API replicas one by one with a freshly built image while the balancer keeps serving.
# 1. build the new image, 2. apply migrations once (they must be compatible with the running version),
# 3. for every old replica: start one new replica, wait until HAProxy has it UP, then stop (drain) and remove the old.
# The number of serving replicas never drops below the configured count.
set -euo pipefail
cd "$(dirname "$0")/.."
build_id=${BUILD_ID:-$(git rev-parse --short HEAD)-$(date +%s)}
log() { echo "$(date -u +%H:%M:%S) $*"; }
up_in_lb() {
docker compose exec -T lb wget -qO- 'http://127.0.0.1:8404/stats;csv' | awk -F, '$1 == "api" && $2 != "BACKEND" && $18 == "UP"' | wc -l
}
log "build image (BUILD_ID=$build_id)"
docker compose build --quiet --build-arg BUILD_ID="$build_id" api
log "migrate"
docker compose run --rm --no-deps migrate > /dev/null
old=$(docker compose ps -q api)
count=$(echo "$old" | wc -l | tr -d ' ')
for container in $old; do
before=$(docker compose ps -q api | sort)
docker compose up -d --no-deps --no-recreate --scale api=$((count + 1)) api 2> /dev/null
new=$(comm -13 <(echo "$before") <(docker compose ps -q api | sort))
log "started $(docker inspect -f '{{.Name}}' "$new") ($(echo "$new" | cut -c1-12))"
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 0.5; done
until [ "$(up_in_lb)" -ge $((count + 1)) ]; do sleep 0.5; done
log "new replica is UP in HAProxy"
docker stop --time 9 "$container" > /dev/null
docker rm "$container" > /dev/null
log "stopped and removed $(echo "$container" | cut -c1-12)"
done
log "done: $(docker compose ps -q api | while read -r c; do docker inspect -f '{{.Name}} {{index .Config.Labels "org.opencontainers.image.revision"}}' "$c"; done | tr '\n' ' ')"
+100
View File
@@ -0,0 +1,100 @@
-- EXPLAIN (ANALYZE, BUFFERS) of the chat history query on synthetic data.
-- Everything runs in one transaction that is rolled back: the database is left untouched.
-- The queries are the ones ExposedMessageRepository.findPageByChatId generates (page size 50 + 1 look-ahead row).
\set ON_ERROR_STOP on
\pset pager off
BEGIN;
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ('00000000-0000-0000-0000-000000000001', 'explain_alice', 'Alice', 'not-a-hash', now()),
('00000000-0000-0000-0000-000000000002', 'explain_bob', 'Bob', 'not-a-hash', now());
-- One large chat (20 000 messages) and 50 neighbours with 1 000 messages each: 70 000 rows in total,
-- so the plan has to skip foreign chats instead of reading a table that only holds the target chat.
INSERT INTO chats (id, title, created_at)
SELECT ('00000000-0000-0000-0001-' || lpad(n::text, 12, '0'))::uuid, 'explain chat ' || n, now()
FROM generate_series(0, 50) AS n;
INSERT INTO chat_members (chat_id, user_id, position)
SELECT c.id, u.id, u.position
FROM chats c
CROSS JOIN (VALUES ('00000000-0000-0000-0000-000000000001'::uuid, 0),
('00000000-0000-0000-0000-000000000002'::uuid, 1)) AS u (id, position)
WHERE c.title LIKE 'explain chat %';
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(),
'00000000-0000-0000-0001-000000000000',
CASE WHEN n % 2 = 0 THEN '00000000-0000-0000-0000-000000000001'::uuid
ELSE '00000000-0000-0000-0000-000000000002'::uuid END,
'message #' || n,
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
FROM generate_series(1, 20000) AS n;
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(),
('00000000-0000-0000-0001-' || lpad(c::text, 12, '0'))::uuid,
'00000000-0000-0000-0000-000000000001',
'neighbour message #' || n,
timestamptz '2026-01-01 00:00:00+00' + n * interval '1 second'
FROM generate_series(1, 50) AS c, generate_series(1, 1000) AS n;
ANALYZE users, chats, chat_members, messages;
\echo
\echo '== Data volume'
SELECT count(*) AS total_messages,
count(*) FILTER (WHERE chat_id = '00000000-0000-0000-0001-000000000000') AS target_chat_messages
FROM messages;
-- Cursor of the 15 000th message: a deep page, where OFFSET pagination would have to skip 15 000 rows.
SELECT created_at AS cursor_created_at, id AS cursor_id
FROM messages
WHERE chat_id = '00000000-0000-0000-0001-000000000000'
ORDER BY created_at, id
OFFSET 14999 LIMIT 1 \gset
\echo
\echo '== 1. First page: WHERE chat_id = ? ORDER BY created_at, id LIMIT 51'
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000'
ORDER BY messages.created_at ASC, messages.id ASC
LIMIT 51;
\echo
\echo '== 2. Deep page after the 15 000th message: (created_at, id) > (?, ?)'
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000')
AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id'))
ORDER BY messages.created_at ASC, messages.id ASC
LIMIT 51;
\echo
\echo '== 3. Same deep page with OFFSET instead of a cursor (for comparison, not used by the API)'
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE messages.chat_id = '00000000-0000-0000-0001-000000000000'
ORDER BY messages.created_at ASC, messages.id ASC
LIMIT 51 OFFSET 15000;
\echo
\echo '== 4. Deep page without messages_chat_id_created_at_id_idx (index scans disabled for this query only)'
SET LOCAL enable_indexscan = off;
SET LOCAL enable_bitmapscan = off;
SET LOCAL enable_indexonlyscan = off;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF)
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
FROM messages
WHERE (messages.chat_id = '00000000-0000-0000-0001-000000000000')
AND ((messages.created_at, messages.id) > (:'cursor_created_at', :'cursor_id'))
ORDER BY messages.created_at ASC, messages.id ASC
LIMIT 51;
ROLLBACK;
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
# Stops the ONE API replica named by TARGET_INSTANCE_ID and leaves it stopped (no DB/LB/volume changes).
# The replica's X-Instance-Id is its container hostname, i.e. the short container id.
# Restore afterwards with: docker compose start api
set -eu
cd "$(dirname "$0")/.."
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
case "$TARGET_INSTANCE_ID" in
*[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;;
esac
# Only containers of the api service are candidates, so a wrong id can never stop the database or the balancer.
container=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true)
if [ -z "$container" ] || [ "$(printf '%s\n' "$container" | wc -l)" -ne 1 ]; then
echo "no single api replica matches $TARGET_INSTANCE_ID" >&2
exit 1
fi
docker stop --time 9 "$container" >/dev/null
+17
View File
@@ -0,0 +1,17 @@
dependencyResolutionManagement {
repositories {
mavenCentral()
}
versionCatalogs {
create("ktorLibs") {
from("io.ktor:ktor-version-catalog:3.3.0")
}
}
}
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
}
rootProject.name = "mtusi-messenger-lab3"
@@ -0,0 +1,48 @@
package com.mtusi.messenger.main
import com.mtusi.messenger.main.plugins.authentication.configureAuthentication
import com.mtusi.messenger.main.plugins.configureCallLogging
import com.mtusi.messenger.main.plugins.configureContentNegotiation
import com.mtusi.messenger.main.plugins.configureCors
import com.mtusi.messenger.main.plugins.configureDatabase
import com.mtusi.messenger.main.plugins.configureDefaultHeaders
import com.mtusi.messenger.main.plugins.configureGracefulShutdown
import com.mtusi.messenger.main.plugins.configureInstanceId
import com.mtusi.messenger.main.plugins.configureKoin
import com.mtusi.messenger.main.plugins.configureRedis
import com.mtusi.messenger.main.plugins.configureRequestId
import com.mtusi.messenger.main.plugins.configureSessionRevocationSync
import com.mtusi.messenger.main.plugins.configureRouting
import com.mtusi.messenger.main.plugins.configureStatusPages
import com.mtusi.messenger.main.plugins.migrateDatabase
import io.ktor.server.application.Application
import io.ktor.server.netty.EngineMain
fun main(args: Array<String>) {
// `migrate` applies database migrations and exits; the API replicas start with migrations already in place.
if (args.firstOrNull() == "migrate") {
return migrateDatabase()
}
EngineMain.main(args)
}
fun Application.module() {
configureKoin()
configureDatabase()
configureRedis()
configureGracefulShutdown()
configureSessionRevocationSync()
configureContentNegotiation()
configureDefaultHeaders()
configureInstanceId()
configureCors()
configureRequestId()
configureCallLogging()
configureStatusPages()
configureAuthentication()
configureRouting()
}
@@ -0,0 +1,31 @@
package com.mtusi.messenger.main.compat.serializers
import io.ktor.http.HttpMethod
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
typealias SerialHttpMethod = @Serializable(HttpMethodSerializer::class) HttpMethod
object HttpMethodSerializer : KSerializer<HttpMethod> {
override val descriptor = PrimitiveSerialDescriptor(
serialName = "HttpMethod",
kind = PrimitiveKind.STRING
)
override fun serialize(
encoder: Encoder,
value: HttpMethod
) {
encoder.encodeString(value.value)
}
override fun deserialize(decoder: Decoder): HttpMethod {
return HttpMethod.parse(decoder.decodeString())
}
}
@@ -0,0 +1,31 @@
package com.mtusi.messenger.main.compat.serializers
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.util.UUID
typealias SerialUUID = @Serializable(UUIDSerializer::class) UUID
object UUIDSerializer : KSerializer<UUID> {
override val descriptor = PrimitiveSerialDescriptor(
serialName = "UUID",
kind = PrimitiveKind.STRING
)
override fun deserialize(decoder: Decoder): UUID {
return decoder.decodeString().run(UUID::fromString)
}
override fun serialize(
encoder: Encoder,
value: UUID
) {
encoder.encodeString(value.toString())
}
}
@@ -0,0 +1,8 @@
package com.mtusi.messenger.main.config
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
@Module(createdAtStart = true)
@ComponentScan
class ConfigModule
@@ -0,0 +1,15 @@
package com.mtusi.messenger.main.config.authentication
interface AuthenticationConfig {
val realm: String
val sessionTtlSeconds: Long
val sessionCacheTtlSeconds: Long
val sessionCacheSingleFlight: Boolean
/**
* How long a logout keeps trying to write its tombstone to Redis before it answers 503 instead of 204.
*/
val sessionRevokeTimeoutMs: Long
}
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.authentication
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultAuthenticationConfig(
@Provided app: Application
) : AuthenticationConfig by app.environment.config.property("authentication").getAs<SerialAuthenticationConfig>()
@@ -0,0 +1,12 @@
package com.mtusi.messenger.main.config.authentication
import kotlinx.serialization.Serializable
@Serializable
data class SerialAuthenticationConfig(
override val realm: String,
override val sessionTtlSeconds: Long,
override val sessionCacheTtlSeconds: Long,
override val sessionCacheSingleFlight: Boolean = true,
override val sessionRevokeTimeoutMs: Long = 8000
) : AuthenticationConfig
@@ -0,0 +1,100 @@
package com.mtusi.messenger.main.config.cors
import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod
import io.ktor.http.HttpMethod
import kotlinx.serialization.Serializable
import io.ktor.server.plugins.cors.CORSConfig as CorsPluginConfig
interface CorsConfig {
/**
* Whether any host should be allowed
*
* Must default to false
*/
val allowAnyHost: Boolean
val allowedHosts: List<AllowedHost>
/**
* Whether any [HttpMethod][io.ktor.http.HttpMethod] should be allowed
*
* Must default to true
*/
val allowAnyMethod: Boolean
val allowedMethods: List<SerialHttpMethod>
/**
* Whether any header should be allowed
*
* Must default to true
*/
val allowAnyHeader: Boolean
val allowedHeaders: List<String>
/**
* Response headers a browser page may read, e.g. X-Request-Id and X-Instance-Id in Swagger UI
*/
val exposedHeaders: List<String>
/**
* Whether requests from the API's own origin should be allowed
*
* Must default to false
*/
val allowSameOrigin: Boolean
/**
* Whether credentials should be allowed
*
* Must default to true
*/
val allowCredentials: Boolean
/**
* Whether non-simple content types should be allowed
*
* Must default to true
*
* @see [io.ktor.server.plugins.cors.CORSConfig.allowNonSimpleContentTypes]
*/
val allowNonSimpleContentTypes: Boolean
@Serializable
data class AllowedHost(
val host: String,
val schemes: List<String>
)
}
fun CorsPluginConfig.configure(config: CorsConfig) {
if (config.allowAnyHost) {
anyHost()
} else {
config.allowedHosts.forEach { (host, schemes) ->
allowHost(host, schemes)
}
}
if (config.allowAnyMethod) {
HttpMethod.DefaultMethods.forEach(::allowMethod)
} else {
config.allowedMethods.forEach(::allowMethod)
}
if (config.allowAnyHeader) {
allowHeaders { true }
} else {
config.allowedHeaders.forEach(::allowHeader)
}
config.exposedHeaders.forEach(::exposeHeader)
allowSameOrigin = config.allowSameOrigin
allowCredentials = config.allowCredentials
allowNonSimpleContentTypes = config.allowNonSimpleContentTypes
}
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.cors
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultCorsConfig(
@Provided app: Application
) : CorsConfig by app.environment.config.property("cors").getAs<SerialCorsConfig>()
@@ -0,0 +1,18 @@
package com.mtusi.messenger.main.config.cors
import com.mtusi.messenger.main.compat.serializers.SerialHttpMethod
import kotlinx.serialization.Serializable
@Serializable
data class SerialCorsConfig(
override val allowAnyHost: Boolean = false,
override val allowedHosts: List<CorsConfig.AllowedHost> = emptyList(),
override val allowAnyMethod: Boolean = true,
override val allowedMethods: List<SerialHttpMethod> = emptyList(),
override val allowAnyHeader: Boolean = true,
override val allowedHeaders: List<String> = emptyList(),
override val exposedHeaders: List<String> = emptyList(),
override val allowSameOrigin: Boolean = false,
override val allowCredentials: Boolean = true,
override val allowNonSimpleContentTypes: Boolean = true
) : CorsConfig
@@ -0,0 +1,22 @@
package com.mtusi.messenger.main.config.database
interface DatabaseConfig {
val host: String
val port: Int
val name: String
val username: String
val password: String?
val passwordFile: String?
val maxPoolSize: Int
val connectionTimeoutMs: Long
val statementTimeoutMs: Long
val migrateOnStart: Boolean
val jdbcConnectionUrl: String
get() = "jdbc:postgresql://$host:$port/$name"
}
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.database
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultDatabaseConfig(
@Provided app: Application
) : DatabaseConfig by app.environment.config.property("database").getAs<SerialDatabaseConfig>()
@@ -0,0 +1,17 @@
package com.mtusi.messenger.main.config.database
import kotlinx.serialization.Serializable
@Serializable
data class SerialDatabaseConfig(
override val host: String,
override val port: Int,
override val name: String,
override val username: String,
override val password: String? = null,
override val passwordFile: String? = null,
override val maxPoolSize: Int,
override val connectionTimeoutMs: Long,
override val statementTimeoutMs: Long,
override val migrateOnStart: Boolean = true
) : DatabaseConfig
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.message
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultMessageConfig(
@Provided app: Application
) : MessageConfig by app.environment.config.propertyOrNull("messages")?.getAs<SerialMessageConfig>() ?: SerialMessageConfig()
@@ -0,0 +1,5 @@
package com.mtusi.messenger.main.config.message
interface MessageConfig {
val idempotencyTtlSeconds: Long
}
@@ -0,0 +1,8 @@
package com.mtusi.messenger.main.config.message
import kotlinx.serialization.Serializable
@Serializable
data class SerialMessageConfig(
override val idempotencyTtlSeconds: Long = 86400
) : MessageConfig
@@ -0,0 +1,11 @@
package com.mtusi.messenger.main.config.redis
import io.ktor.server.application.Application
import io.ktor.server.config.getAs
import org.koin.core.annotation.Provided
import org.koin.core.annotation.Single
@Single
class DefaultRedisConfig(
@Provided app: Application
) : RedisConfig by app.environment.config.property("redis").getAs<SerialRedisConfig>()
@@ -0,0 +1,15 @@
package com.mtusi.messenger.main.config.redis
interface RedisConfig {
val host: String
val port: Int
val username: String?
val password: String?
val passwordFile: String?
val connectTimeoutMs: Long
val commandTimeoutMs: Long
}
@@ -0,0 +1,14 @@
package com.mtusi.messenger.main.config.redis
import kotlinx.serialization.Serializable
@Serializable
data class SerialRedisConfig(
override val host: String,
override val port: Int,
override val username: String? = null,
override val password: String? = null,
override val passwordFile: String? = null,
override val connectTimeoutMs: Long,
override val commandTimeoutMs: Long
) : RedisConfig
@@ -0,0 +1,37 @@
package com.mtusi.messenger.main.extensions
import com.mtusi.messenger.main.infrastructure.types.AuthChallenge
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.RequestIdKey
import com.mtusi.messenger.main.plugins.authentication.UserPrincipal
import io.ktor.http.HttpHeaders
import io.ktor.server.application.ApplicationCall
import io.ktor.server.auth.principal
import io.ktor.server.response.header
import io.ktor.server.response.respond
import kotlinx.serialization.Serializable
@Serializable
data class ErrorBody(val error: ErrorDetails)
@Serializable
data class ErrorDetails(
val code: String,
val message: String,
val requestId: String,
)
suspend fun ApplicationCall.respondError(
code: ErrorCode,
message: String,
challenge: String = AuthChallenge.BEARER,
) {
val requestId = attributes[RequestIdKey]
if (code == ErrorCode.UNAUTHORIZED) {
response.header(HttpHeaders.WWWAuthenticate, challenge)
}
respond(code.status, ErrorBody(ErrorDetails(code.wire, message, requestId)))
}
val ApplicationCall.currentUser: UserPrincipal
get() = principal<UserPrincipal>()!!
@@ -0,0 +1,31 @@
package com.mtusi.messenger.main.infrastructure.controllers._common.parameters
import com.mtusi.messenger.main.infrastructure.types.ApiException
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import io.ktor.server.application.ApplicationCall
private const val DEFAULT_LIMIT = 50
private const val MAX_LIMIT = 100
data class PageParameters(
val limit: Int,
val cursor: String?
)
fun ApplicationCall.pageParameters(): PageParameters {
val query = request.queryParameters
val limit = query["limit"]
?.let { it.toIntOrNull() ?: throw ApiException(ErrorCode.INVALID_REQUEST, "invalid limit") }
?: DEFAULT_LIMIT
if (limit !in 1..MAX_LIMIT) {
throw ApiException(ErrorCode.INVALID_REQUEST, "limit out of range")
}
val cursor = query["cursor"]
?.takeIf { it.isNotEmpty() }
return PageParameters(limit, cursor)
}
@@ -0,0 +1,53 @@
package com.mtusi.messenger.main.infrastructure.controllers.authentication
import com.mtusi.messenger.main.extensions.currentUser
import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse
import com.mtusi.messenger.main.infrastructure.services.authentication.AuthenticationService
import com.mtusi.messenger.main.plugins.authentication.CREDENTIALS_AUTH
import com.mtusi.messenger.main.plugins.authentication.CredentialsPrincipal
import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH
import com.mtusi.messenger.main.plugins.suspendedTransaction
import io.ktor.http.HttpStatusCode
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.principal
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.delete
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import org.koin.ktor.ext.inject
fun Routing.configureAuthenticationRouting() = route("/api/v1/auth/sessions") {
val authenticationService by inject<AuthenticationService>()
authenticate(CREDENTIALS_AUTH) {
post {
val (user) = call.principal<CredentialsPrincipal>()!!
val issued = suspendedTransaction {
authenticationService.createSession(user.id)
}
call.respond(
HttpStatusCode.Created,
SessionResponse(
token = issued.token,
expiresAt = issued.session.expiresAt,
user = user
)
)
}
}
authenticate(SESSION_AUTH) {
delete("current") {
authenticationService.revokeSession(call.currentUser.session)
call.respond(HttpStatusCode.NoContent)
}
}
}
@@ -0,0 +1,13 @@
package com.mtusi.messenger.main.infrastructure.controllers.authentication.responses
import com.mtusi.messenger.main.infrastructure.dto.User
import kotlinx.serialization.Serializable
import kotlin.time.Instant
@Serializable
data class SessionResponse(
val token: String,
val tokenType: String = "Bearer",
val expiresAt: Instant,
val user: User,
)
@@ -0,0 +1,112 @@
package com.mtusi.messenger.main.infrastructure.controllers.chat
import com.mtusi.messenger.main.extensions.currentUser
import com.mtusi.messenger.main.extensions.respondError
import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters
import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest
import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_MEMBERS
import com.mtusi.messenger.main.infrastructure.models.chat.MAX_CHAT_TITLE_LENGTH
import com.mtusi.messenger.main.infrastructure.services.chat.ChatService
import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatCreationResult
import com.mtusi.messenger.main.infrastructure.services.chat.results.ChatPageResult
import com.mtusi.messenger.main.infrastructure.types.ApiException
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH
import com.mtusi.messenger.main.plugins.suspendedTransaction
import com.mtusi.messenger.main.utils.requireNonBlankBounded
import com.mtusi.messenger.main.utils.requireUuid
import io.ktor.http.HttpStatusCode
import io.ktor.server.auth.authenticate
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import org.koin.ktor.ext.inject
fun Routing.configureChatRouting() = route("/api/v1/chats") {
val chatService by inject<ChatService>()
authenticate(SESSION_AUTH) {
post {
val actor = call.currentUser
val body = call.receive<CreateChatRequest>()
requireNonBlankBounded(
body.title,
min = 1,
max = MAX_CHAT_TITLE_LENGTH,
field = "title"
)
if (body.memberIds.isEmpty() || body.memberIds.size > MAX_CHAT_MEMBERS) {
throw ApiException(ErrorCode.INVALID_REQUEST, "member_ids size out of range")
}
val memberIds = body.memberIds.map { requireUuid(it, "member_ids") }
val result = suspendedTransaction {
chatService.create(
creatorId = actor.id,
title = body.title,
memberIds = memberIds
)
}
when (result) {
is ChatCreationResult.DuplicateMembers ->
call.respondError(ErrorCode.INVALID_REQUEST, "member_ids must be unique")
is ChatCreationResult.CreatorNotMember ->
call.respondError(ErrorCode.INVALID_REQUEST, "creator must be in member ids")
is ChatCreationResult.UnknownMembers ->
call.respondError(ErrorCode.INVALID_REQUEST, "unknown member")
is ChatCreationResult.Success ->
call.respond(HttpStatusCode.Created, result.chat)
}
}
get {
val actor = call.currentUser
val (limit, cursor) = call.pageParameters()
val result = suspendedTransaction {
chatService.findPageAccessible(
userId = actor.id,
limit = limit,
cursor = cursor
)
}
when (result) {
is ChatPageResult.InvalidCursor ->
call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor")
is ChatPageResult.Success ->
call.respond(HttpStatusCode.OK, result.page)
}
}
get("{chat_id}") {
val actor = call.currentUser
val chatId = requireUuid(call.parameters["chat_id"], "chat_id")
val chat = suspendedTransaction {
chatService.findAccessible(actor.id, chatId)
}
if (chat == null) {
call.respondError(ErrorCode.NOT_FOUND, "chat not found")
return@get
}
call.respond(HttpStatusCode.OK, chat)
}
}
}
@@ -0,0 +1,9 @@
package com.mtusi.messenger.main.infrastructure.controllers.chat.requests
import kotlinx.serialization.Serializable
@Serializable
data class CreateChatRequest(
val title: String,
val memberIds: List<String>,
)
@@ -0,0 +1,48 @@
package com.mtusi.messenger.main.infrastructure.controllers.health
import com.mtusi.messenger.main.extensions.respondError
import com.mtusi.messenger.main.infrastructure.controllers.health.responses.HealthResponse
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.isDraining
import com.mtusi.messenger.main.plugins.suspendedTransaction
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.log
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.route
fun Routing.configureHealthRouting() = route("/health") {
get("live") {
call.respond(HttpStatusCode.OK, HealthResponse())
}
// PostgreSQL is the only dependency of the synchronous path: without it nothing can be served.
get("ready") {
// SIGTERM received: leave the balancer's rotation while requests in flight are still being finished.
if (call.application.isDraining) {
call.respondError(ErrorCode.UNAVAILABLE, "shutting down")
return@get
}
val isDatabaseReachable = try {
suspendedTransaction {
exec("SELECT 1")
}
true
} catch (cause: Exception) {
call.application.log.warn("Readiness check failed: ${cause.message}")
false
}
if (!isDatabaseReachable) {
call.respondError(ErrorCode.UNAVAILABLE, "database unavailable")
return@get
}
call.respond(HttpStatusCode.OK, HealthResponse())
}
}
@@ -0,0 +1,6 @@
package com.mtusi.messenger.main.infrastructure.controllers.health.responses
import kotlinx.serialization.Serializable
@Serializable
data class HealthResponse(val status: String = "ok")
@@ -0,0 +1,93 @@
package com.mtusi.messenger.main.infrastructure.controllers.message
import com.mtusi.messenger.main.extensions.currentUser
import com.mtusi.messenger.main.extensions.respondError
import com.mtusi.messenger.main.infrastructure.controllers._common.parameters.pageParameters
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
import com.mtusi.messenger.main.infrastructure.models.message.MAX_MESSAGE_TEXT_LENGTH
import com.mtusi.messenger.main.infrastructure.services.message.MessageService
import com.mtusi.messenger.main.infrastructure.services.message.results.MessageCreationResult
import com.mtusi.messenger.main.infrastructure.services.message.results.MessagePageResult
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH
import com.mtusi.messenger.main.plugins.suspendedTransaction
import com.mtusi.messenger.main.utils.requireIdempotencyKey
import com.mtusi.messenger.main.utils.requireNonBlankBounded
import com.mtusi.messenger.main.utils.requireUuid
import io.ktor.http.HttpStatusCode
import io.ktor.server.auth.authenticate
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import org.koin.ktor.ext.inject
fun Routing.configureMessageRouting() = route("/api/v1/chats/{chat_id}/messages") {
val messageService by inject<MessageService>()
authenticate(SESSION_AUTH) {
post {
val actor = call.currentUser
val chatId = requireUuid(call.parameters["chat_id"], "chat_id")
val idempotencyKey = requireIdempotencyKey(call.request.headers["Idempotency-Key"])
val body = call.receive<CreateMessageRequest>()
requireNonBlankBounded(
body.text,
min = 1,
max = MAX_MESSAGE_TEXT_LENGTH,
field = "text"
)
val result = suspendedTransaction {
messageService.send(
chatId = chatId,
senderId = actor.id,
text = body.text,
idempotencyKey = idempotencyKey
)
}
when (result) {
is MessageCreationResult.ChatNotFound ->
call.respondError(ErrorCode.NOT_FOUND, "chat not found")
is MessageCreationResult.IdempotencyKeyReused ->
call.respondError(ErrorCode.CONFLICT, "Idempotency-Key was used with a different message")
is MessageCreationResult.Success ->
call.respond(HttpStatusCode.Created, result.message)
}
}
get {
val actor = call.currentUser
val chatId = requireUuid(call.parameters["chat_id"], "chat_id")
val (limit, cursor) = call.pageParameters()
val result = suspendedTransaction {
messageService.findPageByChatId(
chatId = chatId,
actorId = actor.id,
limit = limit,
cursor = cursor
)
}
when (result) {
is MessagePageResult.ChatNotFound ->
call.respondError(ErrorCode.NOT_FOUND, "chat not found")
is MessagePageResult.InvalidCursor ->
call.respondError(ErrorCode.INVALID_REQUEST, "invalid cursor")
is MessagePageResult.Success ->
call.respond(HttpStatusCode.OK, result.page)
}
}
}
}
@@ -0,0 +1,8 @@
package com.mtusi.messenger.main.infrastructure.controllers.message.requests
import kotlinx.serialization.Serializable
@Serializable
data class CreateMessageRequest(
val text: String,
)
@@ -0,0 +1,83 @@
package com.mtusi.messenger.main.infrastructure.controllers.user
import com.mtusi.messenger.main.extensions.currentUser
import com.mtusi.messenger.main.extensions.respondError
import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest
import com.mtusi.messenger.main.infrastructure.models.user.MAX_DISPLAY_NAME_LENGTH
import com.mtusi.messenger.main.infrastructure.services.user.UserService
import com.mtusi.messenger.main.infrastructure.services.user.results.UserCreationResult
import com.mtusi.messenger.main.infrastructure.types.ErrorCode
import com.mtusi.messenger.main.plugins.authentication.SESSION_AUTH
import com.mtusi.messenger.main.plugins.suspendedTransaction
import com.mtusi.messenger.main.utils.requireNonBlankBounded
import com.mtusi.messenger.main.utils.requireUsername
import io.ktor.http.HttpStatusCode
import io.ktor.server.auth.authenticate
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import org.koin.ktor.ext.inject
private const val PASSWORD_MIN = 12
private const val PASSWORD_MAX = 128
fun Routing.configureUserRouting() = route("/api/v1/users") {
val userService by inject<UserService>()
post {
val body = call.receive<CreateUserRequest>()
requireUsername(body.username)
requireNonBlankBounded(
body.displayName,
min = 1,
max = MAX_DISPLAY_NAME_LENGTH,
field = "display_name"
)
requireNonBlankBounded(
body.password,
min = PASSWORD_MIN,
max = PASSWORD_MAX,
field = "password"
)
val result = suspendedTransaction {
userService.create(
username = body.username,
displayName = body.displayName,
password = body.password
)
}
when (result) {
is UserCreationResult.UsernameTaken ->
call.respondError(ErrorCode.CONFLICT, "username already taken")
is UserCreationResult.Success ->
call.respond(HttpStatusCode.Created, result.user)
}
}
authenticate(SESSION_AUTH) {
get("me") {
val user = suspendedTransaction {
userService.findById(call.currentUser.id)
}
if (user == null) {
call.respondError(ErrorCode.UNAUTHORIZED, "unknown user")
return@get
}
call.respond(HttpStatusCode.OK, user)
}
}
}
@@ -0,0 +1,10 @@
package com.mtusi.messenger.main.infrastructure.controllers.user.requests
import kotlinx.serialization.Serializable
@Serializable
data class CreateUserRequest(
val username: String,
val displayName: String,
val password: String,
)
@@ -0,0 +1,13 @@
package com.mtusi.messenger.main.infrastructure.dto
import com.mtusi.messenger.main.compat.serializers.SerialUUID
import kotlinx.serialization.Serializable
import kotlin.time.Instant
@Serializable
data class Chat(
val id: SerialUUID,
val title: String,
val memberIds: List<SerialUUID>,
val createdAt: Instant,
)
@@ -0,0 +1,14 @@
package com.mtusi.messenger.main.infrastructure.dto
import com.mtusi.messenger.main.compat.serializers.SerialUUID
import kotlinx.serialization.Serializable
import kotlin.time.Instant
@Serializable
data class Message(
val id: SerialUUID,
val chatId: SerialUUID,
val senderId: SerialUUID,
val text: String,
val createdAt: Instant,
)
@@ -0,0 +1,9 @@
package com.mtusi.messenger.main.infrastructure.dto
import kotlinx.serialization.Serializable
@Serializable
data class PageResponse<T : Any>(
val items: List<T>,
val nextCursor: String?,
)
@@ -0,0 +1,13 @@
package com.mtusi.messenger.main.infrastructure.dto
import com.mtusi.messenger.main.compat.serializers.SerialUUID
import kotlinx.serialization.Serializable
import kotlin.time.Instant
@Serializable
data class User(
val id: SerialUUID,
val username: String,
val displayName: String,
val createdAt: Instant,
)
@@ -0,0 +1,6 @@
package com.mtusi.messenger.main.infrastructure.dto.authentication
data class IssuedSession(
val session: Session,
val token: String,
)

Some files were not shown because too many files have changed in this diff Show More