ЛР 3 - БВТ2301 - Шептунов - на 5 #5
@@ -4,6 +4,7 @@
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets
|
||||
.secrets
|
||||
certs
|
||||
volumes
|
||||
data
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets/
|
||||
.secrets/
|
||||
certs/private/
|
||||
*.key
|
||||
*.p12
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
FROM python:3.12.10-slim-bookworm
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir --requirement requirements.txt \
|
||||
&& groupadd --system --gid 10001 app \
|
||||
&& useradd --system --uid 10001 --gid app --home-dir /nonexistent --shell /usr/sbin/nologin app
|
||||
|
||||
COPY --chown=app:app src ./src
|
||||
COPY --chown=app:app migrations ./migrations
|
||||
COPY --chown=app:app scripts ./scripts
|
||||
COPY --chown=app:app student-tests ./student-tests
|
||||
COPY --chown=app:app tests ./tests
|
||||
COPY --chown=app:app contracts ./contracts
|
||||
COPY --chown=app:app lab.json ./lab.json
|
||||
RUN mkdir -p evidence && chown app:app evidence
|
||||
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=3s --retries=3 \
|
||||
CMD ["python", "/app/scripts/healthcheck.py"]
|
||||
|
||||
CMD ["sh", "-c", "exec uvicorn src.main:app --host \"${APP_HOST}\" --port \"${APP_PORT}\" --workers 1 --timeout-graceful-shutdown 8"]
|
||||
@@ -28,3 +28,26 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
.PHONY: setup start ready student-checks test-availability test-expiry experiment stop
|
||||
setup:
|
||||
bash scripts/setup.sh
|
||||
start:
|
||||
bash scripts/start.sh
|
||||
ready:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/stack.py wait --count 2
|
||||
student-checks:
|
||||
$(PYTHON) student-tests/test_api.py --base-url "$(BASE_URL)"
|
||||
$(PYTHON) student-tests/checks.py --base-url "$(BASE_URL)"
|
||||
$(PYTHON) student-tests/idempotency.py --base-url "$(BASE_URL)"
|
||||
docker exec "$$(docker compose ps --status running -q api | head -n 1)" python student-tests/database.py
|
||||
test-availability:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) student-tests/availability.py
|
||||
test-expiry:
|
||||
bash scripts/test_expiry.sh
|
||||
experiment:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/experiment.py
|
||||
test-dependencies:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) student-tests/dependencies.py
|
||||
stop:
|
||||
docker compose down
|
||||
|
||||
@@ -1,37 +1,371 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор: Максим Шептунов, БВТ2301.
|
||||
- Целевая оценка: 5; трек 1, Цена масштабирования.
|
||||
- Skeleton: `0ab3a341aa7ba32363ce43061d4ff02b1f2964af`.
|
||||
- Дата проверки: 12.10.2026.
|
||||
- Commit/PR: добавляется после публикации.
|
||||
- Стенд: Windows + WSL2 Ubuntu; Docker видит 16 логических CPU и 8 187 711 488 байт RAM (около 7,63 GiB).
|
||||
- Использование ИИ: помощь с реализацией, проверками и объяснением результатов.
|
||||
|
||||
Результаты получены на моём Docker-стенде. Вывод команд находится в `evidence/local_*`,
|
||||
исходные измерения — в JSON. Публичные tests, OpenAPI и README задания сохранены.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Engine с Compose v2, Python 3.10+ и свободный порт 8080.
|
||||
В Windows — WSL Ubuntu с Docker Desktop и включённой WSL Integration.
|
||||
|
||||
```bash
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -r requirements-dev.txt -r requirements-experiment.txt
|
||||
chmod +x scripts/*.sh
|
||||
make setup
|
||||
make start
|
||||
make ready
|
||||
make check
|
||||
make validate
|
||||
make test
|
||||
make student-checks
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
bash scripts/restore-api.sh
|
||||
make test-availability
|
||||
make test-dependencies
|
||||
make test-expiry
|
||||
make experiment
|
||||
make test
|
||||
```
|
||||
|
||||
API-зависимости устанавливаются в Docker из `requirements.txt`. `setup.sh` создаёт
|
||||
`.env` и `.secrets` вне Git. Вывод проверок сохранялся через `2>&1 | tee evidence/local_ИМЯ.txt`.
|
||||
`make stop` сохраняет volumes. Намеренное удаление учебных данных:
|
||||
`docker compose down -v`, только если данные больше не нужны.
|
||||
|
||||
Образы: Python 3.12.10 slim-bookworm, PostgreSQL 16.9 bookworm,
|
||||
Redis 7.4.3 bookworm, HAProxy 3.0.11 bookworm. Библиотеки закреплены по версиям.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Клиент → HAProxy (`127.0.0.1:8080`) → две API → общий PostgreSQL.
|
||||
У API нет опубликованных портов или `container_name`. Балансировка roundrobin.
|
||||
PostgreSQL хранит пользователей, чаты, сообщения, сессии и результаты повторов.
|
||||
Redis — только кеш неизменяемых публичных профилей с TTL 60 s; без него API
|
||||
работает через БД. Read-after-write обеспечивается общей основной БД.
|
||||
Из lab2 перенесены PostgreSQL, миграция основной схемы, общие сессии, Argon2id,
|
||||
Redis-кеш и keyset-пагинация. В lab3 добавлены HAProxy, несколько API, идентификатор
|
||||
процесса, таблица идемпотентности и отдельный запуск миграций. Учтены права на
|
||||
Docker secret и ограничение DNS-задержек при отказе Redis. API не хранит состояние
|
||||
сообщений или сессий только в памяти одной реплики.
|
||||
|
||||
Пароли — Argon2id. SHA-256 Bearer проверяется в PostgreSQL при каждом запросе;
|
||||
один токен действует на всех API. Токены не логируются. `X-Instance-Id` состоит
|
||||
из hostname и случайной части, стабилен при жизни процесса, есть и у ошибок API.
|
||||
|
||||
Область Idempotency-Key: пользователь + POST + чат + ключ. Метод не хранится
|
||||
колонкой: таблица предназначена только для POST messages. Допустимый ASCII-ключ
|
||||
проверяется по контракту. SHA-256 тела берётся от нормализованного JSON текста,
|
||||
поэтому форматирование JSON не меняет результат.
|
||||
|
||||
Одна транзакция берёт общий PostgreSQL advisory lock, проверяет старый результат,
|
||||
вставляет сообщение и результат повтора. Другая реплика ждёт ту же блокировку
|
||||
и возвращает прежний Message с 201. Другой текст при действующем ключе — 409.
|
||||
Ошибка откатывает обе вставки, 5xx не запоминается. Результат действует 24 часа.
|
||||
Истёкший ключ можно использовать заново. Авторизация и участие проверяются
|
||||
перед выдачей сохранённого результата. Случайная коллизия lock-хеша только
|
||||
сериализует независимые запросы: в таблице полные ключи сравниваются отдельно.
|
||||
|
||||
Сервис `migrate` выполняется до API; миграции повторно безопасны и защищены lock.
|
||||
Rolling проверяет совместимые версии lab3 при уже применённой схеме: временно
|
||||
запускается третья API, ожидается её готовность, затем останавливается старая;
|
||||
это повторяется для второй. LB не перезапускается. Переход lab2 → lab3 без паузы
|
||||
не заявляется: старый API не понимает обязательный ключ.
|
||||
|
||||
`/health/live` проверяет процесс, `/health/ready` — PostgreSQL. Redis не влияет
|
||||
на readiness. HAProxy: interval 1 s, check timeout 1 s, fall 2, fastinter 0.5 s,
|
||||
rise 1; Docker DNS hold valid 1 s. Ориентир обнаружения — несколько секунд;
|
||||
граница ≤10 s подтверждается fault-тестом. LB connect timeout 0.5 s, server 5 s,
|
||||
прозрачные retries отключены. SQL timeout 3 s, lock/pool timeout 2 s.
|
||||
SIGTERM передаётся Uvicorn через exec; graceful timeout 8 s, Docker grace 10 s.
|
||||
Клиент после неопределённого POST повторяет тот же ключ и тело.
|
||||
|
||||
Обычные лимиты: API по 1 CPU/256 MiB; PG 1 CPU/512 MiB; Redis и LB по
|
||||
0.5 CPU/128 MiB. Фактические лимиты и ID контейнеров сохраняются через inspect.
|
||||
Настройки `.env`: APP_PORT, SESSION_TTL_SECONDS, API_CPU, API_MEMORY, DB_POOL_MAX.
|
||||
DATABASE_URL_FILE используется. Остальные подсказки неизменённого `.env.example`:
|
||||
API_REPLICAS заменено Compose scale; CACHE_URL задан в compose, SESSION_STORE
|
||||
фиксирован как PostgreSQL, IDEMPOTENCY_TTL_SECONDS фиксирован контрактом в 24 h;
|
||||
APP_HOST задаётся в образе. Эти подсказки не управляют данным решением.
|
||||
|
||||
## Среда и воспроизводимость
|
||||
|
||||
Docker Engine сервера 26.1.1, Docker CLI 28.0.1, Compose 2.27.0-desktop.2,
|
||||
Python хоста 3.10.12. Ядро WSL2: 5.15.146.1-microsoft-standard-WSL2.
|
||||
Полный вывод — `evidence/local_environment.txt`. Объём RAM относится к Docker,
|
||||
а не к всей физической памяти ноутбука. Модель CPU отдельно в замерах не записана.
|
||||
|
||||
`make start` выполняет build и ждёт готовности. `make ready` проверяет обе API
|
||||
через LB. Сервис миграций имеет Exited(0), что является нормальным завершением.
|
||||
Во время испытаний не удалялись volumes. Рестарт выполнялся реальным hook,
|
||||
остановка конкретной API сопоставлялась с ID Docker-контейнера.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий | Команда/сценарий | Полученный результат | Файлы в evidence |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Структура и контракт | make check; make validate | PASS; эти проверки не тестируют запущенный API | local_check.txt, local_validate.txt |
|
||||
| Публичный HTTP API | make test | PASS до отказов, после них и после исследования | local_smoke.txt, local_smoke_after_failures.txt, local_final_smoke.txt |
|
||||
| Две настоящие API, общий токен | make ready; make student-checks; snapshot | Два ID контейнеров/процессов; токен использован на обеих API без login | containers_before.json, local_ready.txt, idempotency.json |
|
||||
| Сохранение функциональности lab2 | make student-checks | PASS: доступ, logout, курсоры, гонка username; 100 уникальных сообщений от 10 клиентов | local_student_checks.txt |
|
||||
| Миграции и атомарность | database.py через Makefile | PASS: повтор миграций, rollback чата/участников и сообщения/результата, истечение ключа | local_student_checks.txt |
|
||||
| Общая идемпотентность | make student-checks | 32 конкурентных запроса через 2 API дали одну запись; 409/400/401/404 проверены | idempotency.json, local_student_checks.txt |
|
||||
| Перезапуск API, PG, Redis и LB | make test-persistence ACTION_SCRIPT=scripts/restart.sh | PASS: подтверждённое сообщение и старый Bearer сохранены | local_persistence.txt, containers_before.json, containers_after_restart.json |
|
||||
| Публичный failover ≤10 s | make test-failover ACTION_SCRIPT=scripts/stop-one.sh | 2 API до отказа; восстановление записи 1,698 s; 114 успешных записей, 3 ошибки | local_failover.txt |
|
||||
| Каждая API отдельно | make test-availability | Верхние оценки восстановления 2,998 и 3,997 s; все подтверждения сохранены | failures.json, local_availability.txt, last_stop.json |
|
||||
| CPU/RAM и закрытые API-порты | stack.py snapshot | Лимиты применены; API-порты не опубликованы; публичен только LB | containers_before.json, containers_final.json |
|
||||
| Rolling под нагрузкой | make test-availability | Старые API заменены; LB не заменён; 2074 сообщения сохранены | rolling.json, rolling_containers.json |
|
||||
| Отказ Redis и PG | make test-dependencies | PASS: Redis fallback; без PG ответы 503; после возврата старые сессии и данные действуют | dependencies.json, local_dependencies.txt |
|
||||
| Истечение сессии | make test-expiry | PASS при TTL=2 s; обычная конфигурация возвращена | local_expiry.txt |
|
||||
| Трек 1: масштабирование | make experiment | 54 запуска, 112519 успешных измеренных запросов, 0 ошибок; 1/2/4 API, fixed/growth | track1_raw.json, track1_summary.json, scaling_rps.png, scaling_p95_ms.png |
|
||||
| Подтверждённое изменение | Парное сравнение DB_POOL_MAX 8 и 1 | При 4 API по 0,5 CPU пул 1 быстрее во всех трёх парах; median RPS +74,0%, p95 −35,6% | Последние 6 запусков track1_raw.json; раздел ниже |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
## Отказы, рестарт и rolling
|
||||
|
||||
## Эксперименты и отказы
|
||||
Публичный failover выбирает API по ответу POST. После завершения hook тот же
|
||||
Bearer продолжает работать через другую API, чтение подтверждений проверяется.
|
||||
Результат: 114 успешных записей, 3 неудачные попытки, первая успешная запись
|
||||
после завершённого stop — через 1,698 s от старта hook. Потери подтверждённых
|
||||
сообщений и дубли от повторов не обнаружены.
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Дополнительный fault-тест использует 4 concurrent clients и смешанный профиль.
|
||||
При timeout/503 POST повторяется с прежними ключом и телом. Затем сверяется
|
||||
полная история с подтверждениями. Первая остановленная API возвращается перед
|
||||
остановкой второй. Идентификаторы и Running=false находятся также внутри
|
||||
`action_events.result` каждого элемента `failures.json`.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Воздействие | Успешные запросы / ошибки | RPS | p50 / p95 / p99, ms | Восстановление и данные |
|
||||
| --- | --- | ---: | --- | --- |
|
||||
| API 1, контейнер 12b697ef94f3 | 1202 / 35; 2,83% ошибок | 75,081 | 40,809 / 115,945 / 184,804 | Верхняя оценка 2,998 s; 729 подтверждённых сообщений сохранены |
|
||||
| API 2, контейнер 67a7ac749a94 | 1004 / 39; 3,74% ошибок | 62,689 | 41,276 / 193,142 / 303,374 | Верхняя оценка 3,997 s; 614 подтверждённых сообщений сохранены |
|
||||
| Rolling, 35,089 s | 3433 / 69; 1,97% ошибок | 97,836 | 19,713 / 133,456 / 234,083 | 2074 подтверждённых сообщения сохранены |
|
||||
|
||||
Время восстановления собственных fault-тестов — консервативная оценка по
|
||||
секундным корзинам успешных запросов после stop. Это не точное время завершения
|
||||
TCP-соединения. Публичный тест отдельно измеряет успешный POST.
|
||||
Краткие 503 зафиксированы: нулевой простой не заявляется.
|
||||
|
||||
При rolling старые контейнеры 12b697ef94f3 и 67a7ac749a94 заменены на
|
||||
9f0a21ee9eb4 и 5b7bd8390bb9. ID балансировщика до/после одинаков:
|
||||
887f07a19c48. В каждой фазе остаётся работающая API, новую запускаем и проверяем
|
||||
до удаления старой. Миграции при каждой замене не выполняются: схема уже применена
|
||||
отдельным сервисом. Сценарий проверяет механизм замены совместимых API lab3,
|
||||
а не несовместимое обновление схемы или новую бизнес-функцию.
|
||||
|
||||
После отключения Redis шесть GET профиля и один POST сообщения успешно завершились
|
||||
за 7,113 s суммарно. Это длительность серии, не latency одного запроса.
|
||||
После отключения PostgreSQL readiness и защищённый запрос через LB вернули 503.
|
||||
После возврата PG прежние токен и сообщение проверены без нового входа.
|
||||
Перезапуск полного стенда также сохранил данные и действующую сессию.
|
||||
|
||||
## Трек 1: цена масштабирования
|
||||
|
||||
### Гипотезы и методика
|
||||
|
||||
Проверяю, даёт ли большее число API прирост при том же суммарном бюджете,
|
||||
а затем при добавлении CPU/RAM. Отдельная исходная гипотеза: увеличение пула
|
||||
с 1 до 8 соединений уменьшит очередь и повысит пропускную способность.
|
||||
Эта гипотеза проверена, а не принята заранее.
|
||||
|
||||
| Серия | API | CPU на API | RAM на API | Общий бюджет API |
|
||||
| --- | ---: | ---: | ---: | --- |
|
||||
| fixed | 1 | 2 | 1024 MiB | 2 CPU / 1024 MiB |
|
||||
| fixed | 2 | 1 | 512 MiB | 2 CPU / 1024 MiB |
|
||||
| fixed | 4 | 0,5 | 256 MiB | 2 CPU / 1024 MiB |
|
||||
| growth | 1 | 1 | 256 MiB | 1 CPU / 256 MiB |
|
||||
| growth | 2 | 1 | 256 MiB | 2 CPU / 512 MiB |
|
||||
| growth | 4 | 1 | 256 MiB | 4 CPU / 1024 MiB |
|
||||
|
||||
PG остаётся 1 CPU/512 MiB, Redis и LB — по 0,5 CPU/128 MiB.
|
||||
Фактические лимиты каждого запуска записаны в raw из Docker inspect.
|
||||
Во всех 54 запусках наблюдаемое число X-Instance-Id соответствует числу API.
|
||||
|
||||
Профиль closed-loop: клиент ждёт ответ перед следующим запросом; 60% POST
|
||||
с текстом 128 символов, 20% чтение первых 50 сообщений, 20% собственный профиль.
|
||||
Concurrency: 1/4/16/32. Перед каждым случаем создаётся новый чат с 10000 сообщений,
|
||||
потом 3 s прогрева и 15 s измерения. Последние запросы заканчиваются позже:
|
||||
фактические интервалы 15,001–16,239 s. В каждой из двух серий 24 случая:
|
||||
3 числа API × 4 concurrency × 2 повтора. Затем 6 парных опытов пула.
|
||||
Итого 54 случая; начальная засеянная история суммарно 540000 сообщений,
|
||||
сверх неё создавались сообщения прогрева и нагрузки.
|
||||
|
||||
RPS считается по успешным запросам и фактическому измеренному интервалу.
|
||||
p50/p95/p99 относятся только к успешным запросам. Ошибки учитываются отдельно.
|
||||
В таблице приведены медианы показателей повторов, а не pooled percentile
|
||||
всех запросов. В масштабировании было по два повтора, в опыте пула — по три.
|
||||
Нагрузочный клиент не делает transparent retry в исследовательской серии.
|
||||
Для fault-сценариев retry, наоборот, явно включён.
|
||||
|
||||
Монитор собирает CPU/RAM контейнеров, состояния/ожидания PG и get_stats пула API.
|
||||
Между сборами пауза 3 s, но docker stats/exec тоже занимают время. В опытах пула
|
||||
по два снимка на запуск: первый заканчивается примерно на 8–10 s,
|
||||
второй — после 17 s, частично за измеренным окном. CPU-диагностика ниже использует
|
||||
снимки внутри окна; поздние снимки не считаются загрузкой всей серии.
|
||||
Накопительные requests_queued/requests_wait_ms включают прогрев и проверки готовности;
|
||||
их нельзя считать чистой средней задержкой одного измеренного HTTP-запроса.
|
||||
|
||||
### Измеренные результаты
|
||||
|
||||
Все 112519 измеренных запросов успешны; failed_attempts=0 в каждом запуске.
|
||||
|
||||
| Серия | API | Clients | RPS | p50 ms | p95 ms | p99 ms | Ошибки |
|
||||
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
|
||||
| fixed | 1 | 1 | 145.511 | 7.158 | 8.774 | 9.997 | 0% |
|
||||
| fixed | 1 | 4 | 164.724 | 25.214 | 34.649 | 45.324 | 0% |
|
||||
| fixed | 1 | 16 | 133.147 | 128.583 | 171.690 | 202.012 | 0% |
|
||||
| fixed | 1 | 32 | 130.375 | 264.331 | 345.976 | 383.635 | 0% |
|
||||
| fixed | 2 | 1 | 131.016 | 7.498 | 9.557 | 31.917 | 0% |
|
||||
| fixed | 2 | 4 | 200.523 | 15.407 | 71.814 | 95.170 | 0% |
|
||||
| fixed | 2 | 16 | 114.050 | 97.128 | 401.072 | 591.932 | 0% |
|
||||
| fixed | 2 | 32 | 122.042 | 213.054 | 694.255 | 861.278 | 0% |
|
||||
| fixed | 4 | 1 | 72.672 | 8.273 | 64.723 | 77.507 | 0% |
|
||||
| fixed | 4 | 4 | 129.416 | 10.895 | 127.184 | 235.002 | 0% |
|
||||
| fixed | 4 | 16 | 82.490 | 16.673 | 866.833 | 1542.302 | 0% |
|
||||
| fixed | 4 | 32 | 76.677 | 131.091 | 1650.318 | 2029.143 | 0% |
|
||||
| growth | 1 | 1 | 122.103 | 7.552 | 10.720 | 46.281 | 0% |
|
||||
| growth | 1 | 4 | 115.231 | 27.557 | 88.178 | 98.727 | 0% |
|
||||
| growth | 1 | 16 | 86.630 | 176.629 | 377.891 | 480.830 | 0% |
|
||||
| growth | 1 | 32 | 89.212 | 340.793 | 674.803 | 898.039 | 0% |
|
||||
| growth | 2 | 1 | 129.289 | 7.566 | 9.698 | 32.124 | 0% |
|
||||
| growth | 2 | 4 | 239.492 | 12.857 | 57.887 | 87.562 | 0% |
|
||||
| growth | 2 | 16 | 154.389 | 79.523 | 292.688 | 445.308 | 0% |
|
||||
| growth | 2 | 32 | 142.447 | 164.731 | 650.997 | 773.957 | 0% |
|
||||
| growth | 4 | 1 | 127.805 | 7.809 | 10.032 | 20.178 | 0% |
|
||||
| growth | 4 | 4 | 262.183 | 12.334 | 47.691 | 67.927 | 0% |
|
||||
| growth | 4 | 16 | 210.021 | 14.433 | 295.197 | 470.346 | 0% |
|
||||
| growth | 4 | 32 | 200.486 | 21.111 | 614.256 | 833.542 | 0% |
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
### Насыщение и границы масштабирования
|
||||
|
||||
При fixed и 4 clients median RPS: 164,724 → 200,523 → 129,416 для 1/2/4 API.
|
||||
Разделение того же бюджета на четыре API ухудшило результат: каждая получает
|
||||
только 0,5 CPU, а переключение между процессами/соединениями добавляет работу.
|
||||
Для двух API разброс заметен: повторы 245,752 и 155,293 RPS. Поэтому преимуществу
|
||||
двух API в этой серии нельзя приписывать высокую статистическую точность.
|
||||
|
||||
При growth и 4 clients median RPS: 115,231 → 239,492 → 262,183.
|
||||
Переход с 1 на 2 API дал около +108%, с 2 на 4 — только +9,5%.
|
||||
Добавление ресурсов помогает, но линейного ускорения после двух API нет.
|
||||
|
||||
В fixed для двух API увеличение clients с 4 до 16 снизило RPS
|
||||
с 200,523 до 114,050, а p95 вырос с 71,814 до 401,072 ms.
|
||||
При 32 clients p95 достиг 694,255 ms. Насыщение в данной сетке находится
|
||||
между 4 и 16 clients; точнее его определить без дополнительных точек нельзя.
|
||||
При pool=8 максимальная наблюдаемая очередь на API для этого случая:
|
||||
1 при 4 clients, 7 при 16, 22 при 32. Это наблюдаемая точка давления на пул,
|
||||
а не вывод только по одному графику RPS.
|
||||
|
||||
В парных опытах с четырьмя API по 0,5 CPU отдельные API достигают
|
||||
примерно 50% Docker CPU, то есть своего лимита в половину ядра
|
||||
(максимум внутри окна при pool=8 — 52,53% кратковременного снимка).
|
||||
PG при pool=8 показывает 19,12–24,77% CPU из доступного лимита 100%,
|
||||
LB — 7,67–8,13% из лимита 50%. CPU-насыщение PG и LB этими снимками
|
||||
не подтверждается. БД часто находится в ClientRead, в том числе idle in transaction:
|
||||
она ждёт действий API-клиента, а не постоянно исполняет SQL.
|
||||
Есть и отдельные ожидания WALSync, поэтому влияние дисковой задержки не исключено.
|
||||
|
||||
Из этого следует рабочее объяснение: при малом CPU-бюджете API большая
|
||||
параллельность создаёт конкуренцию за CPU и удерживает больше соединений/транзакций.
|
||||
Очередь пула есть, но увеличение его размера не снимает ограничение обработки API.
|
||||
CPU-снимки редкие; это объяснение механизма, а не профилирование каждого запроса.
|
||||
Сам эффект изменения пула проверен отдельно ниже.
|
||||
|
||||
### Подтверждённый выбор размера пула
|
||||
|
||||
Сравнивались 4 API по 0,5 CPU/256 MiB, 32 clients; все остальные ресурсы одинаковы.
|
||||
Порядок: pool 1/8, затем 8/1, затем 1/8. Каждый запуск с новым чатом,
|
||||
тем же seed, прогревом и профилем. Названия pool_before/pool_after в raw сохранены:
|
||||
они обозначают исходную гипотезу 1 → 8, а не заранее лучший вариант.
|
||||
|
||||
| Пара | RPS pool=8 | RPS pool=1 | p95 pool=8, ms | p95 pool=1, ms | Ошибки |
|
||||
| --- | ---: | ---: | ---: | ---: | --- |
|
||||
| 1 | 78.992 | 137.435 | 1372.051 | 961.728 | 0% / 0% |
|
||||
| 2 | 77.599 | 147.111 | 1611.915 | 807.740 | 0% / 0% |
|
||||
| 3 | 84.820 | 126.450 | 1493.381 | 1008.669 | 0% / 0% |
|
||||
| Медиана | 78.992 | 137.435 | 1493.381 | 961.728 | 0% / 0% |
|
||||
|
||||
Исходная гипотеза об увеличении пула опровергнута: 1 → 8 снизило median RPS
|
||||
на 42,5%. Подтверждено обратное изменение, **уменьшение пула с 8 до 1**:
|
||||
median RPS +74,0%, median p95 −35,6%; p50 110,893 → 95,751 ms,
|
||||
p99 2209,060 → 1404,250 ms. В каждой паре RPS лучше и p95 ниже:
|
||||
прирост RPS по парам +74,0%, +89,6%, +49,1%. Ошибок не добавилось.
|
||||
|
||||
При pool=1 максимальная наблюдаемая очередь всё равно достигает 31 запроса
|
||||
на API. Значит, меньший пул не устраняет ожидание: он ограничивает полезную
|
||||
параллельность, и для этого CPU-бюджета итоговая пропускная способность выше.
|
||||
У PostgreSQL меньше клиентских соединений: примерно 4 вместо 31–32 в первых
|
||||
снимках парного сравнения. Единственным изменяемым параметром был размер пула.
|
||||
Результат не означает, что один коннект оптимален для любого профиля и hardware.
|
||||
|
||||
Обычная конфигурация двух API сохраняет pool=8. Исследованное улучшение относится
|
||||
к четырём API с 0,5 CPU на каждую. Воспроизвести именно эту настройку можно так:
|
||||
|
||||
```bash
|
||||
python3 - <<'CONFIG'
|
||||
from scripts.stack import scale
|
||||
scale(4, 0.5, '256m', 1)
|
||||
CONFIG
|
||||
```
|
||||
|
||||
Для возврата обычного стенда:
|
||||
|
||||
```bash
|
||||
python3 - <<'CONFIG'
|
||||
from scripts.stack import scale
|
||||
scale(2, 1, '256m', 8)
|
||||
CONFIG
|
||||
```
|
||||
|
||||
Исходный журнал local_experiment.txt содержит NEEDS_REVIEW: первоначальный анализ
|
||||
оценивал только гипотезу увеличения пула. Исходные числа не изменялись.
|
||||
Анализ обеих сторон сравнения отражён в обновлённом summary и в этом отчёте.
|
||||
Повторная агрегация имеющихся данных не запускает новую нагрузку:
|
||||
|
||||
```bash
|
||||
python3 scripts/experiment.py --summarize-only
|
||||
```
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Стенд выдержал остановку любой одной API, но остаются SPOF: один host,
|
||||
один PostgreSQL и один LB. Их отказ не заявляется как безостановочный.
|
||||
Rolling дал краткие ошибки 503, хотя все подтверждённые сообщения сохранены.
|
||||
Совместимость миграций для произвольных будущих версий не проверялась.
|
||||
|
||||
Docker CPU — ограничение, не резервирование. Клиент и диагностические docker exec
|
||||
используют тот же ноутбук. Короткие серии и два повтора масштабирования дают
|
||||
заметный разброс. Пулы сравнивались три раза, но доверительные интервалы
|
||||
и полноценное CPU-профилирование не строились. Более длинные опыты без диагностики
|
||||
помогут оценить её overhead; лимит пула следует подбирать заново при другом CPU.
|
||||
|
||||
База между запусками не очищалась: создавались новые чаты, а общий объём данных
|
||||
рос. На старте прогрева в каждом чате 10000 сообщений; к началу измерения
|
||||
добавлены сообщения прогрева, их число зависит от пропускной способности.
|
||||
Полные схемы/volumes не восстанавливались из одного snapshot перед каждой парой.
|
||||
Последовательность AB/BA частично уменьшает влияние порядка, но не устраняет его.
|
||||
|
||||
В PostgreSQL нет автоматической очистки истёкших sessions/message_requests:
|
||||
они не действуют, но занимают место. Redis хранит только кеш; durability сообщений
|
||||
обеспечивается PG, а не AOF Redis. Без TLS приложение предназначено для локального
|
||||
учебного стенда. При неопределённом результате POST повтор безопасен только
|
||||
с прежними ключом и телом, в пределах срока ключа и с действующей авторизацией.
|
||||
|
||||
Источники: README, COURSE и OpenAPI lab3; собственное решение lab2;
|
||||
[Compose services](https://docs.docker.com/reference/compose-file/services/),
|
||||
[HAProxy health checks](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/reliability/health-checks/),
|
||||
[HAProxy DNS](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/proxying-essentials/dns-resolution/),
|
||||
[PostgreSQL advisory locks](https://www.postgresql.org/docs/16/explicit-locking.html#ADVISORY-LOCKS).
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
services:
|
||||
db:
|
||||
image: postgres:16.9-bookworm
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
|
||||
secrets: [db_password]
|
||||
volumes: [postgres_data:/var/lib/postgresql/data]
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
cache:
|
||||
image: redis:7.4.3-bookworm
|
||||
command: [redis-server, --appendonly, 'yes', --appendfsync, everysec]
|
||||
volumes: [redis_data:/data]
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
healthcheck:
|
||||
test: [CMD, redis-cli, ping]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
migrate:
|
||||
build: .
|
||||
command: [python, -m, src.migrate]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
secrets: [database_url]
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
depends_on:
|
||||
db: {condition: service_healthy}
|
||||
healthcheck: {disable: true}
|
||||
api:
|
||||
build: .
|
||||
scale: 2
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL: redis://cache:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DB_POOL_MAX: ${DB_POOL_MAX:-8}
|
||||
secrets: [database_url]
|
||||
dns_opt: ['timeout:1', 'attempts:1']
|
||||
cpus: ${API_CPU:-1.0}
|
||||
mem_limit: ${API_MEMORY:-256m}
|
||||
stop_grace_period: 10s
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
cache: {condition: service_healthy}
|
||||
healthcheck:
|
||||
test: [CMD, python, /app/scripts/healthcheck.py]
|
||||
interval: 2s
|
||||
timeout: 4s
|
||||
retries: 30
|
||||
lb:
|
||||
image: haproxy:3.0.11-bookworm
|
||||
ports: ['127.0.0.1:${APP_PORT:-8080}:8080']
|
||||
volumes: ['./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro']
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
depends_on:
|
||||
api: {condition: service_healthy}
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
secrets:
|
||||
db_password: {file: .secrets/db_password}
|
||||
database_url: {file: .secrets/database_url}
|
||||
@@ -0,0 +1,83 @@
|
||||
[
|
||||
{
|
||||
"id": "12b697ef94f3be33cc73e4aeda1d15484b55c8e257411253d66bf1083b6d7d15",
|
||||
"name": "mtusi2026lab3-api-1",
|
||||
"hostname": "12b697ef94f3",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:21.173524533Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "67a7ac749a94",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:21.568086583Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359548516Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359588699Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:16:12.954388922Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,83 @@
|
||||
[
|
||||
{
|
||||
"id": "1b73eb7fa6e21b533fa50dc11e2acd58f5f4ec016f033facf08cff6e7f9f8a07",
|
||||
"name": "mtusi2026lab3-api-1",
|
||||
"hostname": "1b73eb7fa6e2",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:16:16.271508624Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "83c7a9707bb9ae44c8b7d3db3c7ec7f8d8adcc50911d15e1ee29f5a0b816e8af",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "83c7a9707bb9",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:16:15.860908952Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:16:08.112136813Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:16:08.112101628Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "e5ed4c393124c27053b4f69320586bb57c2cc549d4328cf9cd813e518fa2baad",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "e5ed4c393124",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:16:19.366531525Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:16:12.954388922Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,83 @@
|
||||
[
|
||||
{
|
||||
"id": "c561f6e5eb62d94ddf6ca4bc28cf419147df5f6424165930b96eeb88ec81adb7",
|
||||
"name": "mtusi2026lab3-api-6",
|
||||
"hostname": "c561f6e5eb62",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:49:44.571149204Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "d10f701f2c07a02dd3a83c077394d24d3b7592451979e143b7abcb7ad36353eb",
|
||||
"name": "mtusi2026lab3-api-7",
|
||||
"hostname": "d10f701f2c07",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:49:44.201147944Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:22:40.482739435Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:22:48.133994931Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:20:06.265768415Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"redis": {
|
||||
"status": "PASS",
|
||||
"elapsed_seconds": 7.112801708000006
|
||||
},
|
||||
"postgres": {
|
||||
"status": "PASS",
|
||||
"readiness_and_request_status": [
|
||||
503,
|
||||
503
|
||||
]
|
||||
},
|
||||
"recovery": "old session and confirmed message preserved"
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"status": "PASS",
|
||||
"concurrent_requests": 32,
|
||||
"observed_instances": 2,
|
||||
"messages_after_race": 1,
|
||||
"conflict": 409,
|
||||
"invalid_key": 400,
|
||||
"logout": 401
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
{
|
||||
"target": "67a7ac749a94:78bf43a5",
|
||||
"stopped_container": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"before": [
|
||||
{
|
||||
"id": "12b697ef94f3be33cc73e4aeda1d15484b55c8e257411253d66bf1083b6d7d15",
|
||||
"name": "mtusi2026lab3-api-1",
|
||||
"hostname": "12b697ef94f3",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:19:45.149692804Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "67a7ac749a94",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:21.568086583Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359548516Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359588699Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:19:42.176269661Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"id": "12b697ef94f3be33cc73e4aeda1d15484b55c8e257411253d66bf1083b6d7d15",
|
||||
"name": "mtusi2026lab3-api-1",
|
||||
"hostname": "12b697ef94f3",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:19:45.149692804Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "67a7ac749a94",
|
||||
"service": "api",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:18:21.568086583Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359548516Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359588699Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:19:42.176269661Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
BASE_URL="http://localhost:8080" python3 student-tests/availability.py
|
||||
PASS: both API failures and rolling replacement, confirmations preserved
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
@@ -0,0 +1,10 @@
|
||||
python3 student-tests/test_api.py --base-url "http://localhost:8080"
|
||||
PASS: malformed JSON/media type, duplicate username, blank message, outsider access, pagination
|
||||
PASS: 10 clients created exactly 100 unique messages
|
||||
python3 student-tests/checks.py --base-url "http://localhost:8080"
|
||||
PASS: registration race, invalid input, cursor scope/types on two API, read-after-write, logout
|
||||
python3 student-tests/idempotency.py --base-url "http://localhost:8080"
|
||||
PASS: shared idempotency, scope, conflict and auth on replay
|
||||
docker exec "$(docker compose ps --status running -q api | head -n 1)" python student-tests/database.py
|
||||
PASS: rollback of chat + members; migration reapplied twice
|
||||
PASS: persisted key expiry, atomic message/result rollback
|
||||
@@ -0,0 +1,2 @@
|
||||
BASE_URL="http://localhost:8080" python3 student-tests/dependencies.py
|
||||
PASS: Redis fallback, PostgreSQL unready, recovery without new login
|
||||
@@ -0,0 +1,31 @@
|
||||
CPU=16 RAM_BYTES=8187711488
|
||||
Client: Docker Engine - Community
|
||||
Version: 28.0.1
|
||||
API version: 1.45 (downgraded from 1.48)
|
||||
Go version: go1.23.6
|
||||
Git commit: 068a01e
|
||||
Built: Wed Feb 26 10:41:08 2025
|
||||
OS/Arch: linux/amd64
|
||||
Context: default
|
||||
|
||||
Server: Docker Desktop
|
||||
Engine:
|
||||
Version: 26.1.1
|
||||
API version: 1.45 (minimum version 1.24)
|
||||
Go version: go1.21.9
|
||||
Git commit: ac2de55
|
||||
Built: Tue Apr 30 11:48:28 2024
|
||||
OS/Arch: linux/amd64
|
||||
Experimental: false
|
||||
containerd:
|
||||
Version: 1.6.31
|
||||
GitCommit: e377cd56a71523140ca6ae87e30244719194a521
|
||||
runc:
|
||||
Version: 1.1.12
|
||||
GitCommit: v1.1.12-0-g51d5e94
|
||||
docker-init:
|
||||
Version: 0.19.0
|
||||
GitCommit: de40ad0
|
||||
Docker Compose version v2.27.0-desktop.2
|
||||
Python 3.10.12
|
||||
Linux LAPTOP-66HKAM8B 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
|
||||
@@ -0,0 +1,110 @@
|
||||
BASE_URL="http://localhost:8080" python3 scripts/experiment.py
|
||||
[1/54] fixed, API=1, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[2/54] fixed, API=1, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[3/54] fixed, API=1, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[4/54] fixed, API=1, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[5/54] fixed, API=1, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[6/54] fixed, API=1, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[7/54] fixed, API=1, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[8/54] fixed, API=1, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[9/54] fixed, API=2, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[10/54] fixed, API=2, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[11/54] fixed, API=2, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[12/54] fixed, API=2, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[13/54] fixed, API=2, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[14/54] fixed, API=2, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[15/54] fixed, API=2, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[16/54] fixed, API=2, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[17/54] fixed, API=4, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[18/54] fixed, API=4, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[19/54] fixed, API=4, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[20/54] fixed, API=4, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[21/54] fixed, API=4, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[22/54] fixed, API=4, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[23/54] fixed, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[24/54] fixed, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[25/54] growth, API=1, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[26/54] growth, API=1, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[27/54] growth, API=1, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[28/54] growth, API=1, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[29/54] growth, API=1, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[30/54] growth, API=1, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[31/54] growth, API=1, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[32/54] growth, API=1, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[33/54] growth, API=2, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[34/54] growth, API=2, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[35/54] growth, API=2, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[36/54] growth, API=2, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[37/54] growth, API=2, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[38/54] growth, API=2, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[39/54] growth, API=2, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[40/54] growth, API=2, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[41/54] growth, API=4, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[42/54] growth, API=4, clients=1, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[43/54] growth, API=4, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[44/54] growth, API=4, clients=4, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[45/54] growth, API=4, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[46/54] growth, API=4, clients=16, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[47/54] growth, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[48/54] growth, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[49/54] pool_before, API=4, clients=32, pool=1
|
||||
Synthetic history: 10000 messages
|
||||
[50/54] pool_after, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[51/54] pool_after, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
[52/54] pool_before, API=4, clients=32, pool=1
|
||||
Synthetic history: 10000 messages
|
||||
[53/54] pool_before, API=4, clients=32, pool=1
|
||||
Synthetic history: 10000 messages
|
||||
[54/54] pool_after, API=4, clients=32, pool=8
|
||||
Synthetic history: 10000 messages
|
||||
Research status: NEEDS_REVIEW
|
||||
@@ -0,0 +1,20 @@
|
||||
bash scripts/test_expiry.sh
|
||||
Container mtusi2026lab3-api-4 Recreate
|
||||
Container mtusi2026lab3-api-3 Recreate
|
||||
Container mtusi2026lab3-api-3 Recreated
|
||||
Container mtusi2026lab3-api-4 Recreated
|
||||
Container mtusi2026lab3-api-3 Starting
|
||||
Container mtusi2026lab3-api-3 Started
|
||||
Container mtusi2026lab3-api-4 Starting
|
||||
Container mtusi2026lab3-api-4 Started
|
||||
API ready: 66d7b9dd6776:eaf4888e, a261a276eec2:a368da04
|
||||
PASS: short TTL expiry
|
||||
Container mtusi2026lab3-api-3 Recreate
|
||||
Container mtusi2026lab3-api-4 Recreate
|
||||
Container mtusi2026lab3-api-3 Recreated
|
||||
Container mtusi2026lab3-api-4 Recreated
|
||||
Container mtusi2026lab3-api-3 Starting
|
||||
Container mtusi2026lab3-api-3 Started
|
||||
Container mtusi2026lab3-api-4 Starting
|
||||
Container mtusi2026lab3-api-4 Started
|
||||
API ready: c52afc55c4f6:f0693d8f, dbb5737620df:289d3a28
|
||||
@@ -0,0 +1,4 @@
|
||||
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 114, "failed_attempts": 3, "first_recovery_seconds": 1.698}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,2 @@
|
||||
BASE_URL="http://localhost:8080" python3 scripts/stack.py wait --count 2
|
||||
API ready: c561f6e5eb62:1b45ca31, d10f701f2c07:61ee66f6
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,3 @@
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,2 @@
|
||||
BASE_URL="http://localhost:8080" python3 scripts/stack.py wait --count 2
|
||||
API ready: 1b73eb7fa6e2:9d3131df, 83c7a9707bb9:ef265d89
|
||||
@@ -0,0 +1,7 @@
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
mtusi2026lab3-api-1 mtusi2026lab3-api "sh -c 'exec uvicorn…" api 21 seconds ago Up 12 seconds (healthy) 8080/tcp
|
||||
mtusi2026lab3-api-2 mtusi2026lab3-api "sh -c 'exec uvicorn…" api 21 seconds ago Up 12 seconds (healthy) 8080/tcp
|
||||
mtusi2026lab3-cache-1 redis:7.4.3-bookworm "docker-entrypoint.s…" cache 21 seconds ago Up 20 seconds (healthy) 6379/tcp
|
||||
mtusi2026lab3-db-1 postgres:16.9-bookworm "docker-entrypoint.s…" db 21 seconds ago Up 20 seconds (healthy) 5432/tcp
|
||||
mtusi2026lab3-lb-1 haproxy:3.0.11-bookworm "docker-entrypoint.s…" lb 21 seconds ago Up 9 seconds 127.0.0.1:8080->8080/tcp
|
||||
mtusi2026lab3-migrate-1 mtusi2026lab3-migrate "python -m src.migra…" migrate 21 seconds ago Exited (0) 13 seconds ago
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,222 @@
|
||||
bash scripts/start.sh
|
||||
lb Pulling
|
||||
b1badc6e5066 Already exists
|
||||
93966a25fd29 Pulling fs layer
|
||||
7fffeaaf8311 Pulling fs layer
|
||||
4125f09b2efb Pulling fs layer
|
||||
426458412bd2 Pulling fs layer
|
||||
4f4fb700ef54 Pulling fs layer
|
||||
4f4fb700ef54 Waiting
|
||||
426458412bd2 Waiting
|
||||
7fffeaaf8311 Downloading [==================================================>] 1.159kB/1.159kB
|
||||
7fffeaaf8311 Verifying Checksum
|
||||
7fffeaaf8311 Download complete
|
||||
93966a25fd29 Downloading [> ] 49.67kB/3.51MB
|
||||
4125f09b2efb Downloading [> ] 101.9kB/9.437MB
|
||||
93966a25fd29 Downloading [=====> ] 382.5kB/3.51MB
|
||||
4125f09b2efb Downloading [==> ] 415.2kB/9.437MB
|
||||
93966a25fd29 Downloading [===========> ] 815.6kB/3.51MB
|
||||
4125f09b2efb Downloading [====> ] 815.6kB/9.437MB
|
||||
93966a25fd29 Downloading [============================> ] 1.984MB/3.51MB
|
||||
4125f09b2efb Downloading [==========> ] 2.018MB/9.437MB
|
||||
93966a25fd29 Verifying Checksum
|
||||
93966a25fd29 Download complete
|
||||
4125f09b2efb Downloading [=======================> ] 4.399MB/9.437MB
|
||||
93966a25fd29 Extracting [> ] 65.54kB/3.51MB
|
||||
4125f09b2efb Downloading [===================================> ] 6.673MB/9.437MB
|
||||
93966a25fd29 Extracting [====> ] 327.7kB/3.51MB
|
||||
4125f09b2efb Downloading [==================================================>] 9.437MB/9.437MB
|
||||
4125f09b2efb Download complete
|
||||
93966a25fd29 Extracting [============================================> ] 3.146MB/3.51MB
|
||||
93966a25fd29 Extracting [==================================================>] 3.51MB/3.51MB
|
||||
93966a25fd29 Extracting [==================================================>] 3.51MB/3.51MB
|
||||
93966a25fd29 Pull complete
|
||||
7fffeaaf8311 Extracting [==================================================>] 1.159kB/1.159kB
|
||||
7fffeaaf8311 Extracting [==================================================>] 1.159kB/1.159kB
|
||||
7fffeaaf8311 Pull complete
|
||||
4125f09b2efb Extracting [> ] 98.3kB/9.437MB
|
||||
4125f09b2efb Extracting [========================> ] 4.62MB/9.437MB
|
||||
4125f09b2efb Extracting [==================================================>] 9.437MB/9.437MB
|
||||
4125f09b2efb Pull complete
|
||||
426458412bd2 Downloading [==================================================>] 448B/448B
|
||||
426458412bd2 Verifying Checksum
|
||||
426458412bd2 Download complete
|
||||
426458412bd2 Extracting [==================================================>] 448B/448B
|
||||
426458412bd2 Extracting [==================================================>] 448B/448B
|
||||
426458412bd2 Pull complete
|
||||
4f4fb700ef54 Downloading [==================================================>] 32B/32B
|
||||
4f4fb700ef54 Verifying Checksum
|
||||
4f4fb700ef54 Download complete
|
||||
4f4fb700ef54 Extracting [==================================================>] 32B/32B
|
||||
4f4fb700ef54 Extracting [==================================================>] 32B/32B
|
||||
4f4fb700ef54 Pull complete
|
||||
lb Pulled
|
||||
#0 building with "default" instance using docker driver
|
||||
|
||||
#1 [migrate internal] load build definition from Dockerfile
|
||||
#1 transferring dockerfile: 1.04kB done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [migrate internal] load metadata for docker.io/library/python:3.12.10-slim-bookworm
|
||||
#2 DONE 2.0s
|
||||
|
||||
#3 [migrate internal] load .dockerignore
|
||||
#3 transferring context: 225B done
|
||||
#3 DONE 0.0s
|
||||
|
||||
#4 [migrate 1/12] FROM docker.io/library/python:3.12.10-slim-bookworm@sha256:fd95fa221297a88e1cf49c55ec1828edd7c5a428187e67b5d1805692d11588db
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [migrate internal] load build context
|
||||
#5 transferring context: 227.94kB 0.0s done
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [migrate 2/12] WORKDIR /app
|
||||
#6 CACHED
|
||||
|
||||
#7 [migrate 3/12] COPY requirements.txt .
|
||||
#7 CACHED
|
||||
|
||||
#8 [migrate 4/12] RUN pip install --no-cache-dir --requirement requirements.txt && groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --home-dir /nonexistent --shell /usr/sbin/nologin app
|
||||
#8 CACHED
|
||||
|
||||
#9 [migrate 5/12] COPY --chown=app:app src ./src
|
||||
#9 DONE 0.1s
|
||||
|
||||
#10 [migrate 6/12] COPY --chown=app:app migrations ./migrations
|
||||
#10 DONE 0.1s
|
||||
|
||||
#11 [migrate 7/12] COPY --chown=app:app scripts ./scripts
|
||||
#11 DONE 0.1s
|
||||
|
||||
#12 [migrate 8/12] COPY --chown=app:app student-tests ./student-tests
|
||||
#12 DONE 0.1s
|
||||
|
||||
#13 [migrate 9/12] COPY --chown=app:app tests ./tests
|
||||
#13 DONE 0.1s
|
||||
|
||||
#14 [migrate 10/12] COPY --chown=app:app contracts ./contracts
|
||||
#14 DONE 0.1s
|
||||
|
||||
#15 [migrate 11/12] COPY --chown=app:app lab.json ./lab.json
|
||||
#15 DONE 0.1s
|
||||
|
||||
#16 [migrate 12/12] RUN mkdir -p evidence && chown app:app evidence
|
||||
#16 DONE 0.4s
|
||||
|
||||
#17 [migrate] exporting to image
|
||||
#17 exporting layers
|
||||
#17 exporting layers 0.2s done
|
||||
#17 writing image sha256:1085e4bae473759743af485a32df780221988b100eeb685530288c4cc71d287a done
|
||||
#17 naming to docker.io/library/mtusi2026lab3-migrate done
|
||||
#17 DONE 0.2s
|
||||
|
||||
#18 [api internal] load build definition from Dockerfile
|
||||
#18 transferring dockerfile: 1.04kB done
|
||||
#18 DONE 0.0s
|
||||
|
||||
#2 [api internal] load metadata for docker.io/library/python:3.12.10-slim-bookworm
|
||||
#2 DONE 4.0s
|
||||
|
||||
#19 [api internal] load .dockerignore
|
||||
#19 transferring context: 225B 0.0s done
|
||||
#19 DONE 0.0s
|
||||
|
||||
#4 [api 1/12] FROM docker.io/library/python:3.12.10-slim-bookworm@sha256:fd95fa221297a88e1cf49c55ec1828edd7c5a428187e67b5d1805692d11588db
|
||||
#4 DONE 0.0s
|
||||
|
||||
#20 [api internal] load build context
|
||||
#20 transferring context: 1.79kB done
|
||||
#20 DONE 0.0s
|
||||
|
||||
#21 [api 6/12] COPY --chown=app:app migrations ./migrations
|
||||
#21 CACHED
|
||||
|
||||
#22 [api 8/12] COPY --chown=app:app student-tests ./student-tests
|
||||
#22 CACHED
|
||||
|
||||
#6 [api 2/12] WORKDIR /app
|
||||
#6 CACHED
|
||||
|
||||
#23 [api 10/12] COPY --chown=app:app contracts ./contracts
|
||||
#23 CACHED
|
||||
|
||||
#24 [api 3/12] COPY requirements.txt .
|
||||
#24 CACHED
|
||||
|
||||
#25 [api 11/12] COPY --chown=app:app lab.json ./lab.json
|
||||
#25 CACHED
|
||||
|
||||
#26 [api 5/12] COPY --chown=app:app src ./src
|
||||
#26 CACHED
|
||||
|
||||
#27 [api 7/12] COPY --chown=app:app scripts ./scripts
|
||||
#27 CACHED
|
||||
|
||||
#28 [api 9/12] COPY --chown=app:app tests ./tests
|
||||
#28 CACHED
|
||||
|
||||
#29 [api 4/12] RUN pip install --no-cache-dir --requirement requirements.txt && groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --home-dir /nonexistent --shell /usr/sbin/nologin app
|
||||
#29 CACHED
|
||||
|
||||
#30 [api 12/12] RUN mkdir -p evidence && chown app:app evidence
|
||||
#30 CACHED
|
||||
|
||||
#31 [api] exporting to image
|
||||
#31 exporting layers done
|
||||
#31 writing image sha256:45122a96360ac6b55027dc2f138c19fa3050580d301f017a6ee93cdb15928c44 done
|
||||
#31 naming to docker.io/library/mtusi2026lab3-api 0.0s done
|
||||
#31 DONE 0.0s
|
||||
Network mtusi2026lab3_default Creating
|
||||
Network mtusi2026lab3_default Created
|
||||
Volume "mtusi2026lab3_postgres_data" Creating
|
||||
Volume "mtusi2026lab3_postgres_data" Created
|
||||
Volume "mtusi2026lab3_redis_data" Creating
|
||||
Volume "mtusi2026lab3_redis_data" Created
|
||||
Container mtusi2026lab3-cache-1 Creating
|
||||
Container mtusi2026lab3-db-1 Creating
|
||||
Container mtusi2026lab3-db-1 Created
|
||||
Container mtusi2026lab3-migrate-1 Creating
|
||||
Container mtusi2026lab3-cache-1 Created
|
||||
Container mtusi2026lab3-migrate-1 Created
|
||||
Container mtusi2026lab3-api-2 Creating
|
||||
Container mtusi2026lab3-api-1 Creating
|
||||
Container mtusi2026lab3-api-1 Created
|
||||
Container mtusi2026lab3-api-2 Created
|
||||
Container mtusi2026lab3-lb-1 Creating
|
||||
Container mtusi2026lab3-lb-1 Created
|
||||
Container mtusi2026lab3-cache-1 Starting
|
||||
Container mtusi2026lab3-db-1 Starting
|
||||
Container mtusi2026lab3-db-1 Started
|
||||
Container mtusi2026lab3-db-1 Waiting
|
||||
Container mtusi2026lab3-cache-1 Started
|
||||
Container mtusi2026lab3-db-1 Healthy
|
||||
Container mtusi2026lab3-migrate-1 Starting
|
||||
Container mtusi2026lab3-migrate-1 Started
|
||||
Container mtusi2026lab3-cache-1 Waiting
|
||||
Container mtusi2026lab3-migrate-1 Waiting
|
||||
Container mtusi2026lab3-cache-1 Healthy
|
||||
Container mtusi2026lab3-migrate-1 Exited
|
||||
Container mtusi2026lab3-api-2 Starting
|
||||
Container mtusi2026lab3-api-2 Started
|
||||
Container mtusi2026lab3-api-1 Starting
|
||||
Container mtusi2026lab3-api-1 Started
|
||||
Container mtusi2026lab3-api-2 Waiting
|
||||
Container mtusi2026lab3-api-1 Waiting
|
||||
Container mtusi2026lab3-api-2 Healthy
|
||||
Container mtusi2026lab3-api-1 Healthy
|
||||
Container mtusi2026lab3-lb-1 Starting
|
||||
Container mtusi2026lab3-lb-1 Started
|
||||
Container mtusi2026lab3-cache-1 Waiting
|
||||
Container mtusi2026lab3-migrate-1 Waiting
|
||||
Container mtusi2026lab3-api-2 Waiting
|
||||
Container mtusi2026lab3-api-1 Waiting
|
||||
Container mtusi2026lab3-lb-1 Waiting
|
||||
Container mtusi2026lab3-db-1 Waiting
|
||||
Container mtusi2026lab3-db-1 Healthy
|
||||
Container mtusi2026lab3-lb-1 Healthy
|
||||
Container mtusi2026lab3-cache-1 Healthy
|
||||
Container mtusi2026lab3-migrate-1 Exited
|
||||
Container mtusi2026lab3-api-2 Healthy
|
||||
Container mtusi2026lab3-api-1 Healthy
|
||||
API ready: 1b73eb7fa6e2:9d3131df, 83c7a9707bb9:ef265d89
|
||||
@@ -0,0 +1,10 @@
|
||||
python3 student-tests/test_api.py --base-url "http://localhost:8080"
|
||||
PASS: malformed JSON/media type, duplicate username, blank message, outsider access, pagination
|
||||
PASS: 10 clients created exactly 100 unique messages
|
||||
python3 student-tests/checks.py --base-url "http://localhost:8080"
|
||||
PASS: registration race, invalid input, cursor scope, read-after-write, logout
|
||||
python3 student-tests/idempotency.py --base-url "http://localhost:8080"
|
||||
PASS: shared idempotency, scope, conflict and auth on replay
|
||||
docker compose exec -T --index 1 api python student-tests/database.py
|
||||
PASS: rollback of chat + members; migration reapplied twice
|
||||
PASS: persisted key expiry, atomic message/result rollback
|
||||
@@ -0,0 +1,2 @@
|
||||
python3 scripts/validate_openapi.py
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,226 @@
|
||||
{
|
||||
"before": [
|
||||
{
|
||||
"id": "12b697ef94f3be33cc73e4aeda1d15484b55c8e257411253d66bf1083b6d7d15",
|
||||
"name": "mtusi2026lab3-api-1",
|
||||
"hostname": "12b697ef94f3",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:19:45.149692804Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "67a7ac749a94",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:09.161822104Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359548516Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359588699Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:20:06.265768415Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
],
|
||||
"steps": [
|
||||
[
|
||||
{
|
||||
"id": "67a7ac749a9470e0c4ddeb63e4e6f4b9d29c3478754eec4b2b4f890aac5435b9",
|
||||
"name": "mtusi2026lab3-api-2",
|
||||
"hostname": "67a7ac749a94",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:09.161822104Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "9f0a21ee9eb4a5fb3b53f04855a7a17d7bbd478b5196e2e2742d223ebc96db3b",
|
||||
"name": "mtusi2026lab3-api-3",
|
||||
"hostname": "9f0a21ee9eb4",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:19.243853374Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"id": "9f0a21ee9eb4a5fb3b53f04855a7a17d7bbd478b5196e2e2742d223ebc96db3b",
|
||||
"name": "mtusi2026lab3-api-3",
|
||||
"hostname": "9f0a21ee9eb4",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:19.243853374Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "5b7bd8390bb98ada3b06c28f9fc9fb2ab28860c8ce2df33a7856af8eca734356",
|
||||
"name": "mtusi2026lab3-api-4",
|
||||
"hostname": "5b7bd8390bb9",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:24.89656974Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
}
|
||||
]
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"id": "9f0a21ee9eb4a5fb3b53f04855a7a17d7bbd478b5196e2e2742d223ebc96db3b",
|
||||
"name": "mtusi2026lab3-api-3",
|
||||
"hostname": "9f0a21ee9eb4",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:19.243853374Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "5b7bd8390bb98ada3b06c28f9fc9fb2ab28860c8ce2df33a7856af8eca734356",
|
||||
"name": "mtusi2026lab3-api-4",
|
||||
"hostname": "5b7bd8390bb9",
|
||||
"service": "api",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:20:24.89656974Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 268435456,
|
||||
"published_ports": {
|
||||
"8080/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "3549b02e000b9cdf992460cd7a094dc7e4633025684356185ae7101f47da92c0",
|
||||
"name": "mtusi2026lab3-cache-1",
|
||||
"hostname": "3549b02e000b",
|
||||
"service": "cache",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359548516Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"6379/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "08d464eda0d8f96c636092caa5da13604709e95636cab46b5039aa1328d49b63",
|
||||
"name": "mtusi2026lab3-db-1",
|
||||
"hostname": "08d464eda0d8",
|
||||
"service": "db",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:14.359588699Z",
|
||||
"cpu_limit": 1.0,
|
||||
"memory_limit_bytes": 536870912,
|
||||
"published_ports": {
|
||||
"5432/tcp": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "887f07a19c48f53fec2e53076d2a6bbece3345f58d512b2c80c1f768dcee3a1e",
|
||||
"name": "mtusi2026lab3-lb-1",
|
||||
"hostname": "887f07a19c48",
|
||||
"service": "lb",
|
||||
"running": true,
|
||||
"started_at": "2026-10-12T17:18:24.755095627Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {
|
||||
"8080/tcp": [
|
||||
{
|
||||
"HostIp": "127.0.0.1",
|
||||
"HostPort": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ebfb1caed752e639ea50fae62b0c09dbe0cad29661a9d8e69d7d6dc4c80f833e",
|
||||
"name": "mtusi2026lab3-migrate-1",
|
||||
"hostname": "ebfb1caed752",
|
||||
"service": "migrate",
|
||||
"running": false,
|
||||
"started_at": "2026-10-12T17:20:06.265768415Z",
|
||||
"cpu_limit": 0.5,
|
||||
"memory_limit_bytes": 134217728,
|
||||
"published_ports": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 92 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 96 KiB |
+138161
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,313 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 1,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 145.511,
|
||||
"p95_ms": 8.774,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 1,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 164.724,
|
||||
"p95_ms": 34.649,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 1,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 133.147,
|
||||
"p95_ms": 171.69,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 1,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 130.375,
|
||||
"p95_ms": 345.976,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 2,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 131.016,
|
||||
"p95_ms": 9.557,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 2,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 200.523,
|
||||
"p95_ms": 71.814,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 2,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 114.05,
|
||||
"p95_ms": 401.072,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 2,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 122.042,
|
||||
"p95_ms": 694.255,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 4,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 72.672,
|
||||
"p95_ms": 64.723,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 4,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 129.416,
|
||||
"p95_ms": 127.184,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 4,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 82.49,
|
||||
"p95_ms": 866.833,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 4,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 76.677,
|
||||
"p95_ms": 1650.318,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 1,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 122.103,
|
||||
"p95_ms": 10.72,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 1,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 115.231,
|
||||
"p95_ms": 88.178,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 1,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 86.63,
|
||||
"p95_ms": 377.891,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 1,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 89.212,
|
||||
"p95_ms": 674.803,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 2,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 129.289,
|
||||
"p95_ms": 9.698,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 2,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 239.492,
|
||||
"p95_ms": 57.887,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 2,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 154.389,
|
||||
"p95_ms": 292.688,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 2,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 142.447,
|
||||
"p95_ms": 650.997,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 4,
|
||||
"concurrency": 1,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 127.805,
|
||||
"p95_ms": 10.032,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 4,
|
||||
"concurrency": 4,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 262.183,
|
||||
"p95_ms": 47.691,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 4,
|
||||
"concurrency": 16,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 210.021,
|
||||
"p95_ms": 295.197,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 4,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 2,
|
||||
"rps": 200.486,
|
||||
"p95_ms": 614.256,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "pool_before",
|
||||
"replicas": 4,
|
||||
"concurrency": 32,
|
||||
"pool": 1,
|
||||
"repetitions": 3,
|
||||
"rps": 137.435,
|
||||
"p95_ms": 961.728,
|
||||
"error_ratio": 0.0
|
||||
},
|
||||
{
|
||||
"series": "pool_after",
|
||||
"replicas": 4,
|
||||
"concurrency": 32,
|
||||
"pool": 8,
|
||||
"repetitions": 3,
|
||||
"rps": 78.992,
|
||||
"p95_ms": 1493.381,
|
||||
"error_ratio": 0.0
|
||||
}
|
||||
],
|
||||
"conclusion": {
|
||||
"status": "MEASURED_IMPROVEMENT",
|
||||
"original_increase_rps_gain": -0.4252410230290683,
|
||||
"tested_change": "8_to_1",
|
||||
"recommended_pool": 1,
|
||||
"rps_gain": 0.7398597326311525,
|
||||
"p95_reduction": 0.35600627033556753,
|
||||
"paired_repetitions": 3,
|
||||
"consistent_pairs": true,
|
||||
"maximum_pool_queue_before": 31,
|
||||
"plateau": [
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 1,
|
||||
"clients_from": 4,
|
||||
"clients_to": 16
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 2,
|
||||
"clients_from": 4,
|
||||
"clients_to": 16
|
||||
},
|
||||
{
|
||||
"series": "fixed",
|
||||
"replicas": 4,
|
||||
"clients_from": 4,
|
||||
"clients_to": 16
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 1,
|
||||
"clients_from": 1,
|
||||
"clients_to": 4
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 2,
|
||||
"clients_from": 4,
|
||||
"clients_to": 16
|
||||
},
|
||||
{
|
||||
"series": "growth",
|
||||
"replicas": 4,
|
||||
"clients_from": 4,
|
||||
"clients_to": 16
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
# Трек 1: фактические результаты
|
||||
|
||||
Медианы повторов; p95 считается только по успешным запросам. Ошибки показаны отдельно.
|
||||
|
||||
| Серия | API | Clients | Pool/API | RPS | p95 ms | Ошибки |
|
||||
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
|
||||
| fixed | 1 | 1 | 8 | 145.511 | 8.774 | 0.00% |
|
||||
| fixed | 1 | 4 | 8 | 164.724 | 34.649 | 0.00% |
|
||||
| fixed | 1 | 16 | 8 | 133.147 | 171.69 | 0.00% |
|
||||
| fixed | 1 | 32 | 8 | 130.375 | 345.976 | 0.00% |
|
||||
| fixed | 2 | 1 | 8 | 131.016 | 9.557 | 0.00% |
|
||||
| fixed | 2 | 4 | 8 | 200.523 | 71.814 | 0.00% |
|
||||
| fixed | 2 | 16 | 8 | 114.05 | 401.072 | 0.00% |
|
||||
| fixed | 2 | 32 | 8 | 122.042 | 694.255 | 0.00% |
|
||||
| fixed | 4 | 1 | 8 | 72.672 | 64.723 | 0.00% |
|
||||
| fixed | 4 | 4 | 8 | 129.416 | 127.184 | 0.00% |
|
||||
| fixed | 4 | 16 | 8 | 82.49 | 866.833 | 0.00% |
|
||||
| fixed | 4 | 32 | 8 | 76.677 | 1650.318 | 0.00% |
|
||||
| growth | 1 | 1 | 8 | 122.103 | 10.72 | 0.00% |
|
||||
| growth | 1 | 4 | 8 | 115.231 | 88.178 | 0.00% |
|
||||
| growth | 1 | 16 | 8 | 86.63 | 377.891 | 0.00% |
|
||||
| growth | 1 | 32 | 8 | 89.212 | 674.803 | 0.00% |
|
||||
| growth | 2 | 1 | 8 | 129.289 | 9.698 | 0.00% |
|
||||
| growth | 2 | 4 | 8 | 239.492 | 57.887 | 0.00% |
|
||||
| growth | 2 | 16 | 8 | 154.389 | 292.688 | 0.00% |
|
||||
| growth | 2 | 32 | 8 | 142.447 | 650.997 | 0.00% |
|
||||
| growth | 4 | 1 | 8 | 127.805 | 10.032 | 0.00% |
|
||||
| growth | 4 | 4 | 8 | 262.183 | 47.691 | 0.00% |
|
||||
| growth | 4 | 16 | 8 | 210.021 | 295.197 | 0.00% |
|
||||
| growth | 4 | 32 | 8 | 200.486 | 614.256 | 0.00% |
|
||||
| pool_before | 4 | 32 | 1 | 137.435 | 961.728 | 0.00% |
|
||||
| pool_after | 4 | 32 | 8 | 78.992 | 1493.381 | 0.00% |
|
||||
|
||||
## Где рост нагрузки перестал давать заметный прирост RPS
|
||||
|
||||
- fixed, 1 API: clients 4 → 16, RPS 164.724 → 133.147; p95 34.649 → 171.69 ms.
|
||||
- fixed, 2 API: clients 4 → 16, RPS 200.523 → 114.05; p95 71.814 → 401.072 ms.
|
||||
- fixed, 4 API: clients 4 → 16, RPS 129.416 → 82.49; p95 127.184 → 866.833 ms.
|
||||
- growth, 1 API: clients 1 → 4, RPS 122.103 → 115.231; p95 10.72 → 88.178 ms.
|
||||
- growth, 2 API: clients 4 → 16, RPS 239.492 → 154.389; p95 57.887 → 292.688 ms.
|
||||
- growth, 4 API: clients 4 → 16, RPS 262.183 → 210.021; p95 47.691 → 295.197 ms.
|
||||
|
||||
## Проверка одного изменения
|
||||
|
||||
Одинаковые 4 API, по 0.5 CPU и 256 MiB. Сравниваются DB_POOL_MAX=1 и 8; остальные лимиты неизменны.
|
||||
Исходная гипотеза — увеличение пула 1 → 8; названия pool_before/pool_after сохранены.
|
||||
Изменение median RPS при 1 → 8: -42.5%.
|
||||
Лучший измеренный вариант: pool=1; сравнение 8 → 1.
|
||||
Median RPS: 78.992 → 137.435; изменение +74.0%.
|
||||
Median p95: 1493.381 → 961.728 ms; снижение 35.6%.
|
||||
Число пар повторов: 3; одинаковое направление RPS/p95 без роста ошибок: да.
|
||||
Максимум наблюдаемой очереди при pool=1: 31.
|
||||
|
||||
Наличие очереди не доказывает, что нужно увеличивать пул. Вывод о механизме проверяется по CPU/RAM и ожиданиям PG из monitor. Результат относится к измеренному профилю и лимитам, не к любому серверу.
|
||||
|
||||
Статус эксперимента: MEASURED_IMPROVEMENT.
|
||||
@@ -0,0 +1,37 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 2048
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 500ms
|
||||
timeout client 30s
|
||||
timeout server 5s
|
||||
timeout check 1s
|
||||
retries 0
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 1
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold nx 1s
|
||||
hold other 1s
|
||||
hold refused 1s
|
||||
hold timeout 1s
|
||||
hold obsolete 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
http-request deny if { path_beg /internal/ }
|
||||
default_backend messenger
|
||||
|
||||
backend messenger
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 1
|
||||
server-template api 1-4 api:8080 check resolvers docker init-addr none resolve-prefer ipv4
|
||||
@@ -0,0 +1,36 @@
|
||||
-- Сначала схема и ограничения, затем запросы приложения.
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name varchar(100) NOT NULL CHECK (length(btrim(display_name)) > 0),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_user_idx ON chat_members(user_id, chat_id);
|
||||
CREATE INDEX chats_order_idx ON chats(created_at, id);
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL,
|
||||
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
|
||||
);
|
||||
-- Equality по chat_id и range/order по двум полям курсора.
|
||||
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
|
||||
CREATE TABLE sessions (
|
||||
token_hash char(64) PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE message_requests (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
request_key varchar(128) NOT NULL,
|
||||
body_hash char(64) NOT NULL,
|
||||
response jsonb NOT NULL,
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, chat_id, request_key)
|
||||
);
|
||||
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
|
||||
@@ -0,0 +1 @@
|
||||
matplotlib==3.9.4
|
||||
@@ -0,0 +1,16 @@
|
||||
annotated-types==0.8.0
|
||||
anyio==4.15.1
|
||||
click==8.5.0
|
||||
fastapi==0.115.12
|
||||
h11==0.16.0
|
||||
idna==3.20
|
||||
pydantic==2.11.5
|
||||
pydantic_core==2.33.2
|
||||
starlette==0.46.2
|
||||
typing-inspection==0.4.4
|
||||
typing_extensions==4.16.0
|
||||
uvicorn==0.34.2
|
||||
psycopg[binary]==3.2.9
|
||||
psycopg-pool==3.3.3
|
||||
redis==5.2.1
|
||||
argon2-cffi==23.1.0
|
||||
@@ -0,0 +1,156 @@
|
||||
"""Один профиль нагрузки для всех серий. Только синтетические данные, без токенов в отчёте."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from collections import Counter
|
||||
import math
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, Failure
|
||||
|
||||
|
||||
def context(base):
|
||||
c = Client(base)
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
return dict(actor=alice, reader=bob, chat=chat)
|
||||
|
||||
|
||||
def percentile(values, p):
|
||||
if not values:
|
||||
return None
|
||||
return round(sorted(values)[max(0, math.ceil(p * len(values)) - 1)] * 1000, 3)
|
||||
|
||||
|
||||
def measure(base, ctx, duration, concurrency, action=None, retry_writes=False):
|
||||
gate = threading.Event()
|
||||
started = time.monotonic()
|
||||
deadline = started + duration
|
||||
action_events = []
|
||||
|
||||
def perform_action():
|
||||
nonlocal deadline
|
||||
time.sleep(2)
|
||||
start = time.monotonic()
|
||||
try:
|
||||
info = action()
|
||||
deadline = max(deadline, time.monotonic() + 5)
|
||||
action_events.append(dict(start_seconds=start-started, end_seconds=time.monotonic()-started,
|
||||
result=info))
|
||||
except Exception as exc:
|
||||
action_events.append(dict(start_seconds=start-started, error=str(exc)))
|
||||
|
||||
def worker(worker):
|
||||
c = Client(base, timeout=3)
|
||||
gate.wait()
|
||||
latencies, confirmed, bins = [], [], Counter()
|
||||
errors, instances, failures, operation_counts = Counter(), Counter(), [], Counter()
|
||||
step, pending = worker, None
|
||||
while time.monotonic() < deadline or (action_thread and action_thread.is_alive()):
|
||||
if time.monotonic() - started > 180:
|
||||
raise RuntimeError('Infrastructure action exceeded workload time limit')
|
||||
begin = time.monotonic()
|
||||
# Один и тот же цикл: 60% POST, 20% история, 20% профиль.
|
||||
operation = 'write' if pending or step % 5 < 3 else ('history' if step % 5 == 3 else 'profile')
|
||||
operation_counts[operation] += 1
|
||||
try:
|
||||
if operation == 'write':
|
||||
key, text = pending or (uuid.uuid4().hex, 'x' * 128)
|
||||
response = c.message(ctx['actor'], ctx['chat'], text, key=key)
|
||||
confirmed.append(response['json'])
|
||||
pending = None
|
||||
elif operation == 'history':
|
||||
response = c.call('GET', '/api/v1/chats/' + ctx['chat']['id'] + '/messages?limit=50',
|
||||
actor=ctx['reader'], schema='MessagePage')
|
||||
else:
|
||||
response = c.call('GET', '/api/v1/users/me', actor=ctx['actor'], schema='User')
|
||||
elapsed = time.monotonic() - begin
|
||||
latencies.append(elapsed)
|
||||
instances[response['headers']['X-Instance-Id']] += 1
|
||||
bins[int(time.monotonic() - started)] += 1
|
||||
except Failure as exc:
|
||||
# Сообщения ошибки клиента не содержат credentials/body.
|
||||
errors[str(exc)] += 1
|
||||
failures.append(round(time.monotonic()-started, 3))
|
||||
if operation == 'write' and retry_writes:
|
||||
pending = (key, text)
|
||||
step += 1
|
||||
# Последний timeout мог произойти после COMMIT. Уточняем результат тем же
|
||||
# ключом вне измеряемого окна, иначе проверка ошибочно сочтёт его дубликатом.
|
||||
finished = time.monotonic()
|
||||
resolved = 0
|
||||
if pending:
|
||||
for attempt in range(10):
|
||||
try:
|
||||
confirmed.append(c.message(ctx['actor'], ctx['chat'], pending[1], key=pending[0])['json'])
|
||||
pending = None
|
||||
resolved = 1
|
||||
break
|
||||
except Failure:
|
||||
time.sleep(.5)
|
||||
if pending:
|
||||
raise RuntimeError('Final uncertain write could not be resolved')
|
||||
return dict(latencies=latencies, confirmed=confirmed, errors=errors, instances=instances,
|
||||
failures=failures, bins=bins, operation_counts=operation_counts,
|
||||
finished=finished, resolved_outside_window=resolved)
|
||||
|
||||
action_thread = threading.Thread(target=perform_action) if action else None
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
jobs = [pool.submit(worker, i) for i in range(concurrency)]
|
||||
started = time.monotonic()
|
||||
deadline = started + duration
|
||||
if action_thread:
|
||||
action_thread.start()
|
||||
gate.set()
|
||||
results = [job.result() for job in jobs]
|
||||
elapsed = max(r['finished'] for r in results) - started
|
||||
if action_thread:
|
||||
action_thread.join(timeout=120)
|
||||
if action_thread.is_alive():
|
||||
raise RuntimeError('Infrastructure action did not finish')
|
||||
if any('error' in e for e in action_events):
|
||||
raise RuntimeError(str(action_events))
|
||||
values = [x for r in results for x in r['latencies']]
|
||||
errors, instances, bins, operation_counts = Counter(), Counter(), Counter(), Counter()
|
||||
for r in results:
|
||||
errors.update(r['errors']); instances.update(r['instances'])
|
||||
bins.update(r['bins']); operation_counts.update(r['operation_counts'])
|
||||
successes, failed = len(values), sum(errors.values())
|
||||
report = dict(duration_requested=duration, elapsed_seconds=round(elapsed, 3), concurrency=concurrency,
|
||||
profile='closed-loop: 60% POST text128, 20% first50 history, 20% profile',
|
||||
retry_writes=retry_writes, successful_requests=successes, failed_attempts=failed,
|
||||
uncertain_writes_resolved_after_window=sum(r['resolved_outside_window'] for r in results),
|
||||
successful_rps=round(successes/elapsed, 3), error_ratio=failed/max(1, successes+failed),
|
||||
successful_latency_ms={f'p{int(p*100)}': percentile(values, p) for p in [.5, .95, .99]},
|
||||
instances=dict(instances), errors=dict(errors), operations=dict(operation_counts),
|
||||
successful_per_second=dict(sorted(bins.items())),
|
||||
failure_seconds=[x for r in results for x in r['failures']],
|
||||
latency_samples_ms=[round(x*1000, 3) for x in values], action_events=action_events)
|
||||
if not successes:
|
||||
raise RuntimeError('No successful requests')
|
||||
return report, [m for r in results for m in r['confirmed']]
|
||||
|
||||
|
||||
def verify_confirmations(base, ctx, confirmations):
|
||||
from urllib.parse import quote
|
||||
c = Client(base)
|
||||
path = '/api/v1/chats/' + ctx['chat']['id'] + '/messages?limit=100'
|
||||
stored = {}
|
||||
while True:
|
||||
page = c.call('GET', path, actor=ctx['reader'], schema='MessagePage')['json']
|
||||
for message in page['items']:
|
||||
if message['id'] in stored:
|
||||
raise AssertionError('Duplicate ID in history')
|
||||
stored[message['id']] = message
|
||||
if page['next_cursor'] is None:
|
||||
break
|
||||
path = '/api/v1/chats/' + ctx['chat']['id'] + '/messages?limit=100&cursor=' + quote(page['next_cursor'])
|
||||
if not all(stored.get(m['id']) == m for m in confirmations):
|
||||
raise AssertionError('Confirmed message missing after infrastructure action')
|
||||
# В fault-сценарии нет seed. Каждый новый ключ должен породить одну строку.
|
||||
expected = {m['id'] for m in confirmations}
|
||||
if set(stored) - expected:
|
||||
raise AssertionError('Retry created an extra message or left an unconfirmed result')
|
||||
return dict(confirmed_messages=len(expected), stored_messages=len(stored), status='PASS')
|
||||
@@ -0,0 +1,191 @@
|
||||
"""Трек 1: цена масштабирования и отдельная проверка размера пула соединений."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from scripts.benchmark import context, measure
|
||||
from scripts.monitor import Monitor
|
||||
from scripts.stack import scale, save, command, containers
|
||||
|
||||
|
||||
def seed(ctx):
|
||||
data = json.dumps(dict(chat_id=ctx['chat']['id'], sender_id=ctx['actor']['id']))
|
||||
running = [row for row in containers('api') if row['running']]
|
||||
if not running:
|
||||
raise RuntimeError('Нет запущенной API для заполнения истории')
|
||||
subprocess.run(['docker', 'exec', '-i', running[0]['id'],
|
||||
'python', 'scripts/seed_history.py'], input=data, text=True,
|
||||
cwd=ROOT, check=True, timeout=60)
|
||||
|
||||
|
||||
def summarize(rows):
|
||||
groups = {}
|
||||
for row in rows:
|
||||
key = (row['series'], row['replicas'], row['concurrency'], row['pool'])
|
||||
groups.setdefault(key, []).append(row)
|
||||
result = []
|
||||
for key, group in groups.items():
|
||||
result.append(dict(series=key[0], replicas=key[1], concurrency=key[2], pool=key[3],
|
||||
repetitions=len(group),
|
||||
rps=round(statistics.median(r['successful_rps'] for r in group), 3),
|
||||
p95_ms=round(statistics.median(r['successful_latency_ms']['p95'] for r in group), 3),
|
||||
error_ratio=statistics.mean(r['error_ratio'] for r in group)))
|
||||
return result
|
||||
|
||||
|
||||
def plots(summary):
|
||||
import matplotlib
|
||||
matplotlib.use('Agg')
|
||||
import matplotlib.pyplot as plt
|
||||
for metric, name in [('rps', 'Successful RPS'), ('p95_ms', 'p95, ms')]:
|
||||
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
|
||||
for ax, series in zip(axes, ['fixed', 'growth']):
|
||||
for n in [1, 2, 4]:
|
||||
points = sorted((r for r in summary if r['series'] == series and r['replicas'] == n),
|
||||
key=lambda r: r['concurrency'])
|
||||
ax.plot([r['concurrency'] for r in points], [r[metric] for r in points],
|
||||
marker='o', label=f'{n} replicas')
|
||||
ax.set(title=series, xlabel='Concurrent clients', ylabel=name)
|
||||
ax.grid(alpha=.3); ax.legend()
|
||||
fig.tight_layout()
|
||||
fig.savefig(ROOT / 'evidence' / f'scaling_{metric}.png', dpi=150)
|
||||
plt.close(fig)
|
||||
|
||||
|
||||
def conclusions(summary, rows):
|
||||
lines = ['# Трек 1: фактические результаты', '',
|
||||
'Медианы повторов; p95 считается только по успешным запросам. Ошибки показаны отдельно.', '',
|
||||
'| Серия | API | Clients | Pool/API | RPS | p95 ms | Ошибки |',
|
||||
'| --- | ---: | ---: | ---: | ---: | ---: | ---: |']
|
||||
for r in summary:
|
||||
lines.append(f"| {r['series']} | {r['replicas']} | {r['concurrency']} | {r['pool']} | "
|
||||
f"{r['rps']} | {r['p95_ms']} | {r['error_ratio']:.2%} |")
|
||||
lines += ['', '## Где рост нагрузки перестал давать заметный прирост RPS', '']
|
||||
plateau = []
|
||||
for series in ['fixed', 'growth']:
|
||||
for n in [1, 2, 4]:
|
||||
points = sorted((r for r in summary if r['series'] == series and r['replicas'] == n),
|
||||
key=lambda r: r['concurrency'])
|
||||
pairs = [(a, b) for a, b in zip(points, points[1:]) if b['rps'] <= a['rps'] * 1.10]
|
||||
if pairs:
|
||||
a, b = pairs[0]
|
||||
plateau.append(dict(series=series, replicas=n, clients_from=a['concurrency'],
|
||||
clients_to=b['concurrency']))
|
||||
lines.append(f"- {series}, {n} API: clients {a['concurrency']} → {b['concurrency']}, "
|
||||
f"RPS {a['rps']} → {b['rps']}; p95 {a['p95_ms']} → {b['p95_ms']} ms.")
|
||||
if not plateau:
|
||||
lines.append('Плато не найдено: увеличить список concurrency и повторить. Насыщение пока не доказано.')
|
||||
before = next(r for r in summary if r['series'] == 'pool_before')
|
||||
after = next(r for r in summary if r['series'] == 'pool_after')
|
||||
increase_gain = after['rps'] / before['rps'] - 1
|
||||
# Имена серий отражают исходную гипотезу 1 -> 8, а не заранее известный победитель.
|
||||
# Сравниваем оба направления, сохраняя исходные замеры и номера повторов.
|
||||
baseline, candidate = (after, before) if before['rps'] > after['rps'] else (before, after)
|
||||
gain = candidate['rps'] / baseline['rps'] - 1
|
||||
p95_reduction = 1 - candidate['p95_ms'] / baseline['p95_ms']
|
||||
baseline_runs = {r['repetition']: r for r in rows if r['series'] == baseline['series']}
|
||||
candidate_runs = {r['repetition']: r for r in rows if r['series'] == candidate['series']}
|
||||
repeats = sorted(baseline_runs.keys() & candidate_runs.keys())
|
||||
consistent = len(repeats) >= 3 and all(
|
||||
candidate_runs[n]['successful_rps'] > baseline_runs[n]['successful_rps'] and
|
||||
candidate_runs[n]['successful_latency_ms']['p95'] < baseline_runs[n]['successful_latency_ms']['p95'] and
|
||||
candidate_runs[n]['error_ratio'] <= baseline_runs[n]['error_ratio'] + .005
|
||||
for n in repeats)
|
||||
improved = gain >= .05 and p95_reduction > 0 and consistent
|
||||
queued = max((pool['stats'].get('requests_waiting', 0)
|
||||
for r in rows if r['series'] == 'pool_before'
|
||||
for sample in r['monitor'] for pool in sample.get('pools', [])), default=0)
|
||||
lines += ['', '## Проверка одного изменения', '',
|
||||
'Одинаковые 4 API, по 0.5 CPU и 256 MiB. Сравниваются DB_POOL_MAX=1 и 8; остальные лимиты неизменны.',
|
||||
'Исходная гипотеза — увеличение пула 1 → 8; названия pool_before/pool_after сохранены.',
|
||||
f'Изменение median RPS при 1 → 8: {increase_gain:+.1%}.',
|
||||
f'Лучший измеренный вариант: pool={candidate["pool"]}; сравнение {baseline["pool"]} → {candidate["pool"]}.',
|
||||
f'Median RPS: {baseline["rps"]} → {candidate["rps"]}; изменение {gain:+.1%}.',
|
||||
f'Median p95: {baseline["p95_ms"]} → {candidate["p95_ms"]} ms; снижение {p95_reduction:.1%}.',
|
||||
f'Число пар повторов: {len(repeats)}; одинаковое направление RPS/p95 без роста ошибок: {"да" if consistent else "нет"}.',
|
||||
f'Максимум наблюдаемой очереди при pool=1: {queued}.',
|
||||
'', 'Наличие очереди не доказывает, что нужно увеличивать пул. '
|
||||
'Вывод о механизме проверяется по CPU/RAM и ожиданиям PG из monitor. '
|
||||
'Результат относится к измеренному профилю и лимитам, не к любому серверу.']
|
||||
confirmed = improved and queued > 0 and bool(plateau)
|
||||
status = 'MEASURED_IMPROVEMENT' if confirmed else 'NEEDS_REVIEW'
|
||||
lines += ['', f'Статус эксперимента: {status}.']
|
||||
(ROOT / 'evidence' / 'track1_summary.md').write_text('\n'.join(lines) + '\n')
|
||||
return dict(status=status, original_increase_rps_gain=increase_gain,
|
||||
tested_change=f'{baseline["pool"]}_to_{candidate["pool"]}',
|
||||
recommended_pool=candidate['pool'], rps_gain=gain, p95_reduction=p95_reduction,
|
||||
paired_repetitions=len(repeats), consistent_pairs=consistent,
|
||||
maximum_pool_queue_before=queued, plateau=plateau)
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('--duration', type=int, default=15)
|
||||
p.add_argument('--warmup', type=int, default=3)
|
||||
p.add_argument('--repeats', type=int, default=2)
|
||||
p.add_argument('--concurrency', nargs='+', type=int, default=[1, 4, 16, 32])
|
||||
p.add_argument('--summarize-only', action='store_true', help='Analyse saved raw data without load or Docker changes')
|
||||
args = p.parse_args()
|
||||
if args.summarize_only:
|
||||
rows = json.loads((ROOT / 'evidence' / 'track1_raw.json').read_text())['runs']
|
||||
summary = summarize(rows)
|
||||
verdict = conclusions(summary, rows)
|
||||
save('track1_summary.json', dict(groups=summary, conclusion=verdict))
|
||||
print('Experiment status:', verdict['status'])
|
||||
return
|
||||
if min(args.duration, args.warmup, args.repeats, *args.concurrency) < 1:
|
||||
p.error('All parameters must be positive')
|
||||
base = os.getenv('BASE_URL', 'http://localhost:8080')
|
||||
rows = []
|
||||
metadata = dict(parameters=vars(args), docker_version=command(['docker', 'version', '--format', '{{.Server.Version}}']),
|
||||
compose_version=command(['docker', 'compose', 'version', '--short']),
|
||||
logical_cpus=os.cpu_count(), started_at=time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
|
||||
database_and_lb_limits='Unchanged throughout; actual values in containers',
|
||||
initial_history_per_case=10000, monitoring_interval_seconds=3,
|
||||
pool_before_is_deliberately_constrained=True)
|
||||
cases = []
|
||||
for series in ['fixed', 'growth']:
|
||||
for n in [1, 2, 4]:
|
||||
cpu, memory = (2 / n, f'{1024 // n}m') if series == 'fixed' else (1, '256m')
|
||||
for clients in args.concurrency:
|
||||
for repeat in range(args.repeats):
|
||||
cases.append((series, n, cpu, memory, 8, clients, repeat))
|
||||
# Порядок AB/BA чередуется, чтобы рост базы и прогрев не помогали всегда одному варианту.
|
||||
for repeat in range(max(3, args.repeats)):
|
||||
for pool in ([1, 8] if repeat % 2 == 0 else [8, 1]):
|
||||
cases.append(('pool_before' if pool == 1 else 'pool_after', 4, .5, '256m', pool,
|
||||
max(args.concurrency), repeat))
|
||||
try:
|
||||
for index, (series, n, cpu, memory, pool, clients, repeat) in enumerate(cases, 1):
|
||||
print(f'[{index}/{len(cases)}] {series}, API={n}, clients={clients}, pool={pool}', flush=True)
|
||||
topology = scale(n, cpu, memory, pool)
|
||||
ctx = context(base)
|
||||
seed(ctx)
|
||||
measure(base, ctx, args.warmup, clients)
|
||||
with Monitor() as monitor:
|
||||
report, _ = measure(base, ctx, args.duration, clients)
|
||||
report.update(series=series, replicas=n, cpu_per_api=cpu, ram_per_api=memory,
|
||||
pool=pool, repetition=repeat + 1, containers=containers(),
|
||||
topology=topology, monitor=monitor.samples)
|
||||
rows.append(report)
|
||||
save('track1_raw.json', dict(metadata=metadata, runs=rows))
|
||||
summary = summarize(rows)
|
||||
verdict = conclusions(summary, rows)
|
||||
save('track1_summary.json', dict(groups=summary, conclusion=verdict))
|
||||
plots(summary)
|
||||
print('Experiment status:', verdict['status'])
|
||||
if verdict['status'] == 'NEEDS_REVIEW':
|
||||
print('Изменение не подтверждено: проверьте ошибки, мониторинг и условия измерения.')
|
||||
finally:
|
||||
scale(2, 1, '256m', 8)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import os
|
||||
import urllib.request
|
||||
|
||||
|
||||
host = "127.0.0.1"
|
||||
port = os.getenv("APP_PORT", "8080")
|
||||
with urllib.request.urlopen(f"http://{host}:{port}/health/ready", timeout=2) as response:
|
||||
payload = json.load(response)
|
||||
if response.status != 200 or payload != {"status": "ok"}:
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Редкие снимки Docker CPU/RAM и ожиданий PostgreSQL; overhead отражён в методике."""
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from scripts.stack import command, containers
|
||||
|
||||
|
||||
class Monitor:
|
||||
def __init__(self):
|
||||
self.samples = []
|
||||
self.stop = threading.Event()
|
||||
self.thread = threading.Thread(target=self.loop)
|
||||
|
||||
def __enter__(self):
|
||||
self.started = time.monotonic()
|
||||
self.thread.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.stop.set()
|
||||
self.thread.join(timeout=120)
|
||||
|
||||
def loop(self):
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
ids = [r['id'] for r in containers() if r['running'] and r['service'] != 'migrate']
|
||||
raw = command(['docker', 'stats', '--no-stream', '--format', '{{json .}}', *ids])
|
||||
stats = [json.loads(line) for line in raw.splitlines()]
|
||||
query = "SELECT coalesce(json_agg(x),'[]') FROM (SELECT state,wait_event_type," \
|
||||
"wait_event,count(*) FROM pg_stat_activity WHERE backend_type='client backend' " \
|
||||
"AND pid<>pg_backend_pid() GROUP BY state,wait_event_type,wait_event) x"
|
||||
waits = json.loads(command(['docker', 'compose', 'exec', '-T', 'db',
|
||||
'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', query]))
|
||||
pools = []
|
||||
for row in containers('api'):
|
||||
if row['running']:
|
||||
output = command(['docker', 'exec', row['id'], 'python', '-c',
|
||||
'import urllib.request; print(urllib.request.urlopen("http://localhost:8080/internal/pool",timeout=3).read().decode())'])
|
||||
pools.append(dict(instance=row['hostname'], stats=json.loads(output)))
|
||||
self.samples.append(dict(seconds=round(time.monotonic()-self.started, 3),
|
||||
docker=stats, postgres=waits, pools=pools))
|
||||
except Exception as exc:
|
||||
self.samples.append(dict(seconds=round(time.monotonic()-self.started, 3), error=str(exc)))
|
||||
self.stop.wait(3)
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose stop lb api cache db
|
||||
docker compose up -d --wait db cache
|
||||
docker compose run --rm --no-deps migrate
|
||||
docker compose up -d --no-deps --wait api
|
||||
docker compose up -d --no-deps lb
|
||||
python3 scripts/stack.py wait --count 2
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose start api
|
||||
python3 scripts/stack.py wait --count 2
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Только эксперимент: фиксированная начальная история синтетического чата."""
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from uuid import UUID
|
||||
import psycopg
|
||||
from src.config import DATABASE_URL
|
||||
|
||||
data = json.load(sys.stdin)
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
db.execute("INSERT INTO messages(id,chat_id,sender_id,text,created_at) "
|
||||
"SELECT gen_random_uuid(),%s,%s,repeat('h',128), "
|
||||
"clock_timestamp()-(10000-n)*interval '1 millisecond' FROM generate_series(1,10000) n",
|
||||
(UUID(data['chat_id']), UUID(data['sender_id'])))
|
||||
print('Synthetic history: 10000 messages')
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p .secrets
|
||||
chmod 700 .secrets
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
password_file = Path('.secrets/db_password')
|
||||
if not password_file.exists():
|
||||
password_file.write_text(secrets.token_hex(24))
|
||||
password = password_file.read_text().strip()
|
||||
Path('.secrets/database_url').write_text(f'postgresql://messenger:{password}@db:5432/messenger')
|
||||
for file in Path('.secrets').iterdir():
|
||||
file.chmod(0o644 if file.name == "database_url" else 0o600)
|
||||
if not Path('.env').exists():
|
||||
Path('.env').write_text('APP_PORT=8080\nSESSION_TTL_SECONDS=3600\nDB_POOL_MAX=8\nAPI_CPU=1.0\nAPI_MEMORY=256m\n')
|
||||
print('Локальные настройки подготовлены; секреты не выводятся.')
|
||||
PY
|
||||
@@ -0,0 +1,129 @@
|
||||
"""Небольшие операции со стендом: состояние, ожидание, масштабирование и замена."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def command(args, env=None):
|
||||
result = subprocess.run(args, cwd=ROOT, env=env, text=True, capture_output=True, timeout=120)
|
||||
if result.returncode:
|
||||
raise RuntimeError(result.stderr[-2000:])
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def containers(service=None):
|
||||
args = ['docker', 'compose', 'ps', '-a', '-q']
|
||||
if service:
|
||||
args.append(service)
|
||||
ids = command(args).split()
|
||||
if not ids:
|
||||
return []
|
||||
# Сохраняем только нужные поля, без environment с возможными секретами.
|
||||
rows = json.loads(command(['docker', 'inspect', *ids]))
|
||||
return [dict(id=r['Id'], name=r['Name'].lstrip('/'), hostname=r['Config']['Hostname'],
|
||||
service=r['Config']['Labels']['com.docker.compose.service'],
|
||||
running=r['State']['Running'], started_at=r['State']['StartedAt'],
|
||||
cpu_limit=r['HostConfig']['NanoCpus'] / 1e9,
|
||||
memory_limit_bytes=r['HostConfig']['Memory'],
|
||||
published_ports=r['NetworkSettings']['Ports']) for r in rows]
|
||||
|
||||
|
||||
def save(name, data):
|
||||
path = ROOT / 'evidence' / name
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + '\n')
|
||||
|
||||
|
||||
def wait(count, base=None):
|
||||
base = base or os.getenv('BASE_URL', 'http://localhost:8080')
|
||||
deadline = time.monotonic() + 75
|
||||
while time.monotonic() < deadline:
|
||||
observed = set()
|
||||
for _ in range(max(12, count * 8)):
|
||||
try:
|
||||
with urllib.request.urlopen(base + '/health/ready', timeout=3) as response:
|
||||
observed.add(response.headers['X-Instance-Id'])
|
||||
except (OSError, ValueError):
|
||||
break
|
||||
if len(observed) == count and None not in observed:
|
||||
return sorted(observed)
|
||||
time.sleep(.5)
|
||||
raise RuntimeError(f'Не дождались {count} готовых API через LB')
|
||||
|
||||
|
||||
def stop(instance):
|
||||
hostname = instance.split(':')[0]
|
||||
found = [r for r in containers('api') if r['hostname'] == hostname and r['running']]
|
||||
if len(found) != 1:
|
||||
raise RuntimeError('Instance alias не соответствует одной работающей API-реплике')
|
||||
before = containers()
|
||||
command(['docker', 'stop', '--time', '3', found[0]['id']])
|
||||
result = dict(target=instance, stopped_container=found[0]['id'], before=before, after=containers())
|
||||
save('last_stop.json', result)
|
||||
return result
|
||||
|
||||
|
||||
def scale(count, cpu, memory, pool):
|
||||
env = dict(os.environ, API_CPU=str(cpu), API_MEMORY=memory, DB_POOL_MAX=str(pool))
|
||||
command(['docker', 'compose', 'up', '-d', '--no-deps', '--force-recreate',
|
||||
'--scale', f'api={count}', 'api'], env)
|
||||
instances = wait(count)
|
||||
rows = containers('api')
|
||||
if len(rows) != count or not all(r['running'] for r in rows):
|
||||
raise RuntimeError('Фактическое число контейнеров отличается от запрошенного')
|
||||
for row in rows:
|
||||
if abs(row['cpu_limit'] - cpu) > .001:
|
||||
raise RuntimeError('Docker не применил CPU limit')
|
||||
if row['memory_limit_bytes'] != int(memory.rstrip('m')) * 1024 * 1024:
|
||||
raise RuntimeError('Docker не применил RAM limit')
|
||||
return dict(instances=instances, containers=rows)
|
||||
|
||||
|
||||
def rolling():
|
||||
old = [r['id'] for r in containers('api') if r['running']]
|
||||
if len(old) != 2:
|
||||
raise RuntimeError('Rolling проверяется на двух репликах обычного стенда')
|
||||
before = containers()
|
||||
command(['docker', 'compose', 'build', 'api'])
|
||||
steps = []
|
||||
for container in old:
|
||||
# Не пересоздаём одновременно старые контейнеры при изменении image.
|
||||
command(['docker', 'compose', 'up', '-d', '--no-deps', '--no-recreate', '--scale', 'api=3', 'api'])
|
||||
wait(3)
|
||||
command(['docker', 'stop', '--time', '10', container])
|
||||
command(['docker', 'rm', container])
|
||||
wait(2)
|
||||
steps.append(containers('api'))
|
||||
save('rolling_containers.json', dict(before=before, steps=steps, after=containers()))
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('action', choices=['wait', 'snapshot', 'stop', 'rolling'])
|
||||
p.add_argument('--count', type=int, default=2)
|
||||
p.add_argument('--instance')
|
||||
p.add_argument('--output', default='containers.json')
|
||||
args = p.parse_args()
|
||||
if args.action == 'wait':
|
||||
print('API ready:', ', '.join(wait(args.count)))
|
||||
elif args.action == 'snapshot':
|
||||
save(args.output, containers())
|
||||
print('Container identities and limits saved')
|
||||
elif args.action == 'stop':
|
||||
if not args.instance:
|
||||
p.error('--instance required')
|
||||
stop(args.instance)
|
||||
print('Selected API stopped')
|
||||
else:
|
||||
rolling()
|
||||
print('API replaced one at a time')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose up -d --build --wait
|
||||
python3 scripts/stack.py wait --count 2
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
python3 scripts/stack.py stop --instance "$TARGET_INSTANCE_ID"
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
restore() {
|
||||
docker compose up -d --no-deps --force-recreate --scale api=2 api
|
||||
python3 scripts/stack.py wait --count 2
|
||||
}
|
||||
trap restore EXIT
|
||||
SESSION_TTL_SECONDS=2 docker compose up -d --no-deps --force-recreate --scale api=2 api
|
||||
python3 scripts/stack.py wait --count 2
|
||||
python3 student-tests/checks.py --expiry
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Ожидание готовности API без сторонних библиотек."""
|
||||
import os
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
url = os.getenv('BASE_URL', 'http://localhost:8080') + '/health/ready'
|
||||
for attempt in range(90):
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=2) as response:
|
||||
if response.status == 200:
|
||||
print('API ready')
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise SystemExit('API не стал готов за 90 попыток')
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Настройки из окружения или Docker secrets; секреты не печатаем."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def setting(name, default=None):
|
||||
file = os.getenv(name + '_FILE')
|
||||
return Path(file).read_text().strip() if file else os.getenv(name, default)
|
||||
|
||||
|
||||
DATABASE_URL = setting('DATABASE_URL')
|
||||
CACHE_URL = setting('CACHE_URL', 'redis://localhost:6379/0')
|
||||
SESSION_TTL = int(os.getenv('SESSION_TTL_SECONDS', '3600'))
|
||||
POOL_MAX = int(os.getenv('DB_POOL_MAX', '8'))
|
||||
+567
@@ -0,0 +1,567 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from contextlib import asynccontextmanager
|
||||
import hashlib
|
||||
import secrets
|
||||
import socket
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg.types.json import Jsonb
|
||||
from psycopg_pool import ConnectionPool, PoolTimeout
|
||||
import redis
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerificationError, InvalidHashError
|
||||
from starlette.exceptions import HTTPException
|
||||
from src.config import DATABASE_URL, CACHE_URL, SESSION_TTL, POOL_MAX
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, FastAPI, Header, Query, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, Response
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
level=os.getenv("LOG_LEVEL", "INFO").upper(),
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("messenger")
|
||||
|
||||
|
||||
def utc_now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def as_json(value: BaseModel) -> dict:
|
||||
return value.model_dump(mode="json")
|
||||
|
||||
|
||||
class StrictModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class User(StrictModel):
|
||||
id: uuid.UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateUser(StrictModel):
|
||||
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
password: str = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def password_has_visible_characters(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("password must not contain only whitespace")
|
||||
return value
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def display_name_has_visible_characters(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("display_name must not contain only whitespace")
|
||||
return value
|
||||
|
||||
|
||||
class Chat(StrictModel):
|
||||
id: uuid.UUID
|
||||
title: str
|
||||
member_ids: list[uuid.UUID]
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateChat(StrictModel):
|
||||
title: str = Field(min_length=1, max_length=100)
|
||||
member_ids: list[uuid.UUID] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def title_has_visible_characters(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("title must not contain only whitespace")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def member_ids_are_unique(cls, value: list[uuid.UUID]) -> list[uuid.UUID]:
|
||||
if len(value) != len(set(value)):
|
||||
raise ValueError("member_ids must be unique")
|
||||
return value
|
||||
|
||||
|
||||
class Message(StrictModel):
|
||||
id: uuid.UUID
|
||||
chat_id: uuid.UUID
|
||||
sender_id: uuid.UUID
|
||||
text: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class CreateMessage(StrictModel):
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def text_has_visible_characters(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("text must not contain only whitespace")
|
||||
return value
|
||||
|
||||
|
||||
class ApiProblem(Exception):
|
||||
def __init__(self, status: int, code: str, message: str, challenge: str | None = None):
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.challenge = challenge
|
||||
|
||||
|
||||
class Store:
|
||||
"""Источник истины теперь PostgreSQL. Redis — только кэш профилей."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.pool = ConnectionPool(
|
||||
DATABASE_URL, min_size=1, max_size=POOL_MAX, timeout=2, open=False,
|
||||
kwargs={"row_factory": dict_row, "connect_timeout": 2,
|
||||
"options": "-c statement_timeout=3000 -c lock_timeout=2000 -c timezone=UTC"},
|
||||
)
|
||||
self.cache = redis.Redis.from_url(
|
||||
CACHE_URL, decode_responses=True, socket_connect_timeout=0.25, socket_timeout=0.25,
|
||||
)
|
||||
|
||||
def start(self) -> None:
|
||||
self.pool.open()
|
||||
self.pool.wait(timeout=10)
|
||||
|
||||
def close(self) -> None:
|
||||
self.pool.close()
|
||||
self.cache.close()
|
||||
|
||||
def chat(self, db, chat_id: uuid.UUID) -> Chat | None:
|
||||
row = db.execute("SELECT * FROM chats WHERE id=%s", (chat_id,)).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
members = db.execute("SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id",
|
||||
(chat_id,)).fetchall()
|
||||
return Chat(**row, member_ids=[member["user_id"] for member in members])
|
||||
|
||||
def chat_messages(self, db, chat_id: uuid.UUID, limit: int, after) -> list[Message]:
|
||||
# В lab1 здесь были общий список/список чата и bisect_right.
|
||||
# Теперь границу ищет B-tree PostgreSQL (chat_id, created_at, id).
|
||||
query = "SELECT * FROM messages WHERE chat_id=%s"
|
||||
params = [chat_id]
|
||||
if after is not None:
|
||||
query += " AND (created_at,id)>(%s,%s)"
|
||||
params.extend(after)
|
||||
rows = db.execute(query + " ORDER BY created_at,id LIMIT %s", params + [limit + 1]).fetchall()
|
||||
return [Message(**row) for row in rows]
|
||||
|
||||
|
||||
INSTANCE_ID = os.getenv("INSTANCE_ID") or f"{socket.gethostname()}:{uuid.uuid4().hex[:8]}"
|
||||
store = Store()
|
||||
passwords = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
|
||||
dummy_hash = passwords.hash(secrets.token_urlsafe(32))
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app):
|
||||
store.start()
|
||||
yield
|
||||
store.close()
|
||||
|
||||
|
||||
app = FastAPI(title="MTUCI PostgreSQL messenger", version="3.0.0", lifespan=lifespan,
|
||||
docs_url=None, redoc_url=None, openapi_url=None)
|
||||
|
||||
|
||||
def request_id(request: Request) -> str:
|
||||
return request.state.request_id
|
||||
|
||||
|
||||
def error_response(request: Request, status: int, code: str, message: str, challenge: str | None = None) -> JSONResponse:
|
||||
rid = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
headers = {"X-Request-Id": rid, "X-Instance-Id": INSTANCE_ID}
|
||||
if challenge:
|
||||
headers["WWW-Authenticate"] = challenge
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={"error": {"code": code, "message": message, "request_id": rid}},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_context(request: Request, call_next):
|
||||
request.state.request_id = str(uuid.uuid4())
|
||||
started = time.perf_counter()
|
||||
status = 500
|
||||
try:
|
||||
# Без токена отказываем ДО обращения к БД и проверки JSON.
|
||||
if request.url.path.startswith("/api/v1/") and request.url.path not in {
|
||||
"/api/v1/users", "/api/v1/auth/sessions"
|
||||
}:
|
||||
scheme, _, token = request.headers.get("authorization", "").partition(" ")
|
||||
if scheme.lower() != "bearer" or not token or len(token) > 256:
|
||||
status = 401
|
||||
return error_response(request, 401, "unauthorized", "Требуется Bearer",
|
||||
'Bearer realm="messenger"')
|
||||
json_endpoints = {
|
||||
"/api/v1/users",
|
||||
"/api/v1/chats",
|
||||
}
|
||||
requires_json = request.method == "POST" and (
|
||||
request.url.path in json_endpoints
|
||||
or re.fullmatch(r"/api/v1/chats/[^/]+/messages", request.url.path) is not None
|
||||
)
|
||||
media_type = request.headers.get("content-type", "").split(";", 1)[0].strip().lower()
|
||||
if requires_json and media_type != "application/json":
|
||||
status = 415
|
||||
return error_response(
|
||||
request,
|
||||
415,
|
||||
"unsupported_media_type",
|
||||
"Требуется Content-Type: application/json",
|
||||
)
|
||||
if requires_json:
|
||||
# Ограниченный JSON; после body() Starlette повторно отдаёт его маршруту.
|
||||
raw = await request.body()
|
||||
if len(raw) > 65536:
|
||||
status = 413
|
||||
return error_response(request, 413, "payload_too_large", "Запрос слишком большой")
|
||||
response = await call_next(request)
|
||||
status = response.status_code
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return response
|
||||
finally:
|
||||
logger.info(
|
||||
"request method=%s path=%s status=%s duration_ms=%.3f request_id=%s",
|
||||
request.method,
|
||||
request.url.path,
|
||||
status,
|
||||
(time.perf_counter() - started) * 1000,
|
||||
request.state.request_id,
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(ApiProblem)
|
||||
async def api_problem_handler(request: Request, exc: ApiProblem):
|
||||
return error_response(request, exc.status, exc.code, exc.message, exc.challenge)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_handler(request: Request, exc: RequestValidationError):
|
||||
return error_response(request, 400, "invalid_request", "Запрос не соответствует контракту")
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unexpected_error_handler(request: Request, exc: Exception):
|
||||
logger.error("unexpected_error request_id=%s", request_id(request))
|
||||
return error_response(request, 500, "internal_error", "Внутренняя ошибка сервера")
|
||||
|
||||
|
||||
@app.exception_handler(psycopg.Error)
|
||||
@app.exception_handler(PoolTimeout)
|
||||
async def database_error_handler(request: Request, exc: Exception):
|
||||
return error_response(request, 503, "unavailable", "Хранилище временно недоступно")
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_error_handler(request: Request, exc: HTTPException):
|
||||
code = "not_found" if exc.status_code == 404 else "invalid_request"
|
||||
return error_response(request, exc.status_code, code, "Неверный маршрут или метод")
|
||||
|
||||
|
||||
def unauthorized(basic=False):
|
||||
challenge = 'Basic realm="messenger", charset="UTF-8"' if basic else 'Bearer realm="messenger"'
|
||||
raise ApiProblem(401, "unauthorized", "Нужна действительная идентификация", challenge)
|
||||
|
||||
|
||||
def token_hash(token: str) -> str:
|
||||
return hashlib.sha256(token.encode()).hexdigest()
|
||||
|
||||
|
||||
def current_user(authorization: Annotated[str | None, Header()] = None) -> User:
|
||||
scheme, _, token = (authorization or "").partition(" ")
|
||||
if scheme.lower() != "bearer" or not token or len(token) > 256:
|
||||
unauthorized()
|
||||
with store.pool.connection() as db:
|
||||
session = db.execute("SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at>now()",
|
||||
(token_hash(token),)).fetchone()
|
||||
if session is None:
|
||||
unauthorized()
|
||||
key = "user:" + str(session["user_id"])
|
||||
cache_available = True
|
||||
try:
|
||||
cached = store.cache.get(key)
|
||||
if cached:
|
||||
user = User.model_validate_json(cached)
|
||||
if user.id == session["user_id"]:
|
||||
return user
|
||||
except (redis.RedisError, ValueError):
|
||||
cache_available = False
|
||||
row = db.execute("SELECT id,username,display_name,created_at FROM users WHERE id=%s",
|
||||
(session["user_id"],)).fetchone()
|
||||
user = User(**row)
|
||||
if cache_available:
|
||||
try:
|
||||
store.cache.setex(key, 60, user.model_dump_json())
|
||||
except redis.RedisError:
|
||||
pass
|
||||
return user
|
||||
|
||||
|
||||
def decode_cursor(cursor: str, expected_scope: str) -> tuple[datetime, uuid.UUID]:
|
||||
try:
|
||||
if not cursor or len(cursor) > 1024:
|
||||
raise ValueError
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
payload = json.loads(base64.b64decode(padded.encode(), altchars=b"-_", validate=True).decode())
|
||||
if not isinstance(payload, dict) or set(payload) != {"scope", "created_at", "id"}:
|
||||
raise ValueError
|
||||
if payload["scope"] != expected_scope:
|
||||
raise ValueError
|
||||
if not isinstance(payload["created_at"], str) or not isinstance(payload["id"], str):
|
||||
raise ValueError
|
||||
created_at = datetime.fromisoformat(payload["created_at"].replace("Z", "+00:00"))
|
||||
item_id = uuid.UUID(payload["id"])
|
||||
if created_at.tzinfo is None:
|
||||
raise ValueError
|
||||
return created_at, item_id
|
||||
except (ValueError, TypeError, KeyError, json.JSONDecodeError, UnicodeError):
|
||||
raise ApiProblem(400, "invalid_request", "Некорректный cursor") from None
|
||||
|
||||
|
||||
def encode_cursor(scope: str, created_at: datetime, item_id: uuid.UUID) -> str:
|
||||
payload = {
|
||||
"scope": scope,
|
||||
"created_at": created_at.isoformat().replace("+00:00", "Z"),
|
||||
"id": str(item_id),
|
||||
}
|
||||
return base64.urlsafe_b64encode(json.dumps(payload, separators=(",", ":")).encode()).decode().rstrip("=")
|
||||
|
||||
|
||||
def page(items: list, limit: int, scope: str) -> dict:
|
||||
# SQL уже выбрал limit+1 строк после курсора; весь список не загружаем.
|
||||
selected = items[:limit]
|
||||
next_cursor = None
|
||||
if len(items) > limit and selected:
|
||||
last = selected[-1]
|
||||
next_cursor = encode_cursor(scope, last.created_at, last.id)
|
||||
return {"items": [as_json(item) for item in selected], "next_cursor": next_cursor}
|
||||
|
||||
|
||||
def visible_chat(chat_id: uuid.UUID, user: User) -> Chat:
|
||||
with store.pool.connection() as db:
|
||||
member = db.execute("SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s",
|
||||
(chat_id, user.id)).fetchone()
|
||||
if member is None:
|
||||
raise ApiProblem(404, "not_found", "Объект не найден")
|
||||
return store.chat(db, chat_id)
|
||||
|
||||
|
||||
def page_parameters(request: Request, scope: str, limit: int, cursor: str | None):
|
||||
# Query(ge/le) сохранён из lab1, добавлена проверка дублей и формы limit.
|
||||
if set(request.query_params) - {"limit", "cursor"} or any(
|
||||
len(request.query_params.getlist(k)) > 1 for k in request.query_params
|
||||
):
|
||||
raise ApiProblem(400, "invalid_request", "Некорректные параметры страницы")
|
||||
if not re.fullmatch(r"[0-9]{1,3}", request.query_params.get("limit", "50")):
|
||||
raise ApiProblem(400, "invalid_request", "Некорректный limit")
|
||||
return decode_cursor(cursor, scope) if cursor is not None else None
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
def liveness():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
def readiness():
|
||||
with store.pool.connection() as db:
|
||||
db.execute("SELECT 1")
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/internal/pool")
|
||||
def pool_statistics():
|
||||
# Диагностика доступна только внутри Docker network: LB закрывает этот путь.
|
||||
return store.pool.get_stats()
|
||||
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
def register_user(body: CreateUser):
|
||||
# Синхронные маршруты FastAPI исполняет в worker thread, не в event loop.
|
||||
hashed = passwords.hash(body.password)
|
||||
try:
|
||||
with store.pool.connection() as db:
|
||||
row = db.execute(
|
||||
"INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) "
|
||||
"RETURNING id,username,display_name,created_at",
|
||||
(uuid.uuid4(), body.username, body.display_name, hashed),
|
||||
).fetchone()
|
||||
except psycopg.errors.UniqueViolation:
|
||||
raise ApiProblem(409, "conflict", "Username уже занят") from None
|
||||
return as_json(User(**row))
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
def create_session(request: Request, authorization: Annotated[str | None, Header()] = None):
|
||||
if request.headers.get("content-length", "0") != "0" or "transfer-encoding" in request.headers:
|
||||
raise ApiProblem(400, "invalid_request", "Вход не принимает тело запроса")
|
||||
scheme, _, encoded = (authorization or "").partition(" ")
|
||||
try:
|
||||
if scheme.lower() != "basic" or len(encoded) > 2048:
|
||||
raise ValueError
|
||||
username, password = base64.b64decode(encoded, validate=True).decode("utf-8").split(":", 1)
|
||||
if not re.fullmatch(r"[a-z0-9_]{3,32}", username) or not 12 <= len(password) <= 128:
|
||||
raise ValueError
|
||||
except (ValueError, UnicodeError):
|
||||
unauthorized(basic=True)
|
||||
with store.pool.connection() as db:
|
||||
row = db.execute("SELECT * FROM users WHERE username=%s", (username,)).fetchone()
|
||||
try:
|
||||
passwords.verify(row["password_hash"] if row else dummy_hash, password)
|
||||
except (VerificationError, InvalidHashError):
|
||||
unauthorized(basic=True)
|
||||
if row is None:
|
||||
unauthorized(basic=True)
|
||||
token = secrets.token_urlsafe(32)
|
||||
expires_at = utc_now() + timedelta(seconds=SESSION_TTL)
|
||||
with store.pool.connection() as db:
|
||||
db.execute("INSERT INTO sessions(token_hash,user_id,expires_at) VALUES (%s,%s,%s)",
|
||||
(token_hash(token), row["id"], expires_at))
|
||||
user = User(**{k: row[k] for k in ("id", "username", "display_name", "created_at")})
|
||||
return {"token": token, "token_type": "Bearer", "user": as_json(user),
|
||||
"expires_at": expires_at.isoformat().replace("+00:00", "Z")}
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
def delete_session(user: Annotated[User, Depends(current_user)],
|
||||
authorization: Annotated[str, Header()]):
|
||||
token = authorization.partition(" ")[2]
|
||||
with store.pool.connection() as db:
|
||||
db.execute("DELETE FROM sessions WHERE token_hash=%s", (token_hash(token),))
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
def get_current_user(user: Annotated[User, Depends(current_user)]):
|
||||
return as_json(user)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(body: CreateChat, user: Annotated[User, Depends(current_user)]):
|
||||
if user.id not in body.member_ids:
|
||||
raise ApiProblem(400, "invalid_request", "Текущий пользователь должен быть участником")
|
||||
with store.pool.connection() as db:
|
||||
found = db.execute("SELECT id FROM users WHERE id=ANY(%s)", (body.member_ids,)).fetchall()
|
||||
if len(found) != len(body.member_ids):
|
||||
raise ApiProblem(400, "invalid_request", "Указан неизвестный участник")
|
||||
row = db.execute("INSERT INTO chats(id,title) VALUES (%s,%s) RETURNING *",
|
||||
(uuid.uuid4(), body.title)).fetchone()
|
||||
with db.cursor() as cur:
|
||||
cur.executemany("INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)",
|
||||
[(row["id"], member) for member in body.member_ids])
|
||||
chat = store.chat(db, row["id"])
|
||||
return as_json(chat) # COMMIT уже выполнен для чата и всех участников.
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
def list_chats(
|
||||
request: Request,
|
||||
user: Annotated[User, Depends(current_user)],
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
):
|
||||
scope = f"chats:{user.id}"
|
||||
after = page_parameters(request, scope, limit, cursor)
|
||||
query = "SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=%s"
|
||||
params = [user.id]
|
||||
if after is not None:
|
||||
query += " AND (c.created_at,c.id)>(%s,%s)"
|
||||
params.extend(after)
|
||||
with store.pool.connection() as db:
|
||||
rows = db.execute(query + " ORDER BY c.created_at,c.id LIMIT %s", params + [limit + 1]).fetchall()
|
||||
chats = [store.chat(db, row["id"]) for row in rows]
|
||||
return page(chats, limit, scope)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: uuid.UUID, user: Annotated[User, Depends(current_user)]):
|
||||
return as_json(visible_chat(chat_id, user))
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
def create_message(
|
||||
request: Request,
|
||||
chat_id: uuid.UUID,
|
||||
body: CreateMessage,
|
||||
user: Annotated[User, Depends(current_user)],
|
||||
idempotency_key: Annotated[str | None, Header()] = None,
|
||||
):
|
||||
if (not idempotency_key or
|
||||
not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", idempotency_key) or
|
||||
len(request.headers.getlist("idempotency-key")) != 1):
|
||||
raise ApiProblem(400, "invalid_request", "Нужен корректный Idempotency-Key")
|
||||
visible_chat(chat_id, user) # Повтор тоже проверяет сессию и участие.
|
||||
payload = json.dumps({"text": body.text}, ensure_ascii=False, sort_keys=True,
|
||||
separators=(",", ":"))
|
||||
digest = hashlib.sha256(payload.encode()).hexdigest()
|
||||
lock_scope = f"{user.id}:POST:{chat_id}:{idempotency_key}"
|
||||
with store.pool.connection() as db:
|
||||
# Общая блокировка в PostgreSQL, а не локальный Lock одной реплики.
|
||||
# Коллизия хеша только сериализует независимые запросы; ключи сравниваются полностью.
|
||||
db.execute("SELECT pg_advisory_xact_lock(hashtextextended(%s,0))", (lock_scope,))
|
||||
previous = db.execute(
|
||||
"SELECT * FROM message_requests WHERE user_id=%s AND chat_id=%s AND request_key=%s",
|
||||
(user.id, chat_id, idempotency_key),
|
||||
).fetchone()
|
||||
if previous and previous["expires_at"] > utc_now():
|
||||
if previous["body_hash"] != digest:
|
||||
raise ApiProblem(409, "conflict", "Ключ уже использован с другим сообщением")
|
||||
result = previous["response"]
|
||||
else:
|
||||
if previous:
|
||||
db.execute("DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND request_key=%s",
|
||||
(user.id, chat_id, idempotency_key))
|
||||
row = db.execute(
|
||||
"INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *",
|
||||
(uuid.uuid4(), chat_id, user.id, body.text),
|
||||
).fetchone()
|
||||
result = as_json(Message(**row))
|
||||
db.execute(
|
||||
"INSERT INTO message_requests(user_id,chat_id,request_key,body_hash,response,expires_at) "
|
||||
"VALUES (%s,%s,%s,%s,%s,clock_timestamp()+interval '24 hours')",
|
||||
(user.id, chat_id, idempotency_key, digest, Jsonb(result)),
|
||||
)
|
||||
# Сообщение и результат повтора зафиксированы одной транзакцией до ответа.
|
||||
return result
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(
|
||||
request: Request,
|
||||
chat_id: uuid.UUID,
|
||||
user: Annotated[User, Depends(current_user)],
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
||||
):
|
||||
visible_chat(chat_id, user)
|
||||
scope = f"messages:{chat_id}"
|
||||
after = page_parameters(request, scope, limit, cursor)
|
||||
with store.pool.connection() as db:
|
||||
messages = store.chat_messages(db, chat_id, limit, after)
|
||||
return page(messages, limit, scope)
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Версионируемые миграции: один файл = одна атомарная транзакция."""
|
||||
from pathlib import Path
|
||||
import psycopg
|
||||
from src.config import DATABASE_URL
|
||||
|
||||
|
||||
def migrate():
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
# Миграции запускает отдельный сервис. Lock защищает от второго запуска оператором.
|
||||
db.execute('SELECT pg_advisory_lock(20260002)')
|
||||
db.execute('CREATE TABLE IF NOT EXISTS schema_migrations '
|
||||
'(version text PRIMARY KEY, applied_at timestamptz DEFAULT now())')
|
||||
db.commit()
|
||||
for file in sorted(Path('migrations').glob('*.sql')):
|
||||
with db.transaction():
|
||||
applied = db.execute('SELECT 1 FROM schema_migrations WHERE version=%s',
|
||||
(file.name,)).fetchone()
|
||||
if not applied:
|
||||
db.execute(file.read_text())
|
||||
db.execute('INSERT INTO schema_migrations(version) VALUES (%s)', (file.name,))
|
||||
print('Applied', file.name)
|
||||
db.execute('SELECT pg_advisory_unlock(20260002)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
migrate()
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Каждая реплика отдельно и поочерёдная замена под одним профилем нагрузки."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from scripts.benchmark import context, measure, verify_confirmations
|
||||
from scripts.stack import containers, stop, wait, command, rolling, save
|
||||
from scripts.monitor import Monitor
|
||||
|
||||
|
||||
def main():
|
||||
base = os.getenv('BASE_URL', 'http://localhost:8080')
|
||||
aliases = wait(2, base)
|
||||
results = []
|
||||
for target in aliases:
|
||||
ctx = context(base)
|
||||
# Сохраняем подтверждённую запись до отказа вместе с подтверждениями нагрузки.
|
||||
from scripts.benchmark import Client
|
||||
original = Client(base).message(ctx['actor'], ctx['chat'], 'До отказа')['json']
|
||||
try:
|
||||
with Monitor() as monitor:
|
||||
report, confirmations = measure(base, ctx, 16, 4,
|
||||
action=lambda: stop(target), retry_writes=True)
|
||||
event = report['action_events'][0]
|
||||
successful_times = sorted(int(x) for x, count in report['successful_per_second'].items() if count)
|
||||
first = next((x for x in successful_times if x > event['end_seconds']), None)
|
||||
if first is None or first+1-event['start_seconds'] > 10:
|
||||
raise AssertionError('Recovery exceeded 10 seconds')
|
||||
stopped_host = target.split(':')[0]
|
||||
if any(r['running'] for r in containers('api') if r['hostname'] == stopped_host):
|
||||
raise AssertionError('Target is still running')
|
||||
report['first_successful_bucket_after_stop_seconds'] = first
|
||||
report['recovery_upper_bound_seconds'] = round(first+1-event['start_seconds'], 3)
|
||||
report['verification'] = verify_confirmations(base, ctx, [original, *confirmations])
|
||||
report['monitor'] = monitor.samples
|
||||
report['target'] = target
|
||||
results.append(report)
|
||||
save('failures.json', results)
|
||||
finally:
|
||||
command(['docker', 'compose', 'start', 'api'])
|
||||
wait(2, base)
|
||||
ctx = context(base)
|
||||
with Monitor() as monitor:
|
||||
report, confirmations = measure(base, ctx, 35, 4, action=rolling, retry_writes=True)
|
||||
report['verification'] = verify_confirmations(base, ctx, confirmations)
|
||||
report['monitor'] = monitor.samples
|
||||
save('rolling.json', report)
|
||||
print('PASS: both API failures and rolling replacement, confirmations preserved')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,90 @@
|
||||
"""Дополнения к публичному smoke. Не печатает и не сохраняет tokens."""
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
from urllib.parse import quote
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def check_cursor_types(c, actor, path, cursor, schema):
|
||||
check(bool(cursor), 'test requires a real pagination cursor')
|
||||
payload = json.loads(base64.urlsafe_b64decode(cursor + '=' * (-len(cursor) % 4)))
|
||||
c.call('GET', path + '?cursor=' + quote(cursor, safe=''), actor=actor, schema=schema)
|
||||
invalid = []
|
||||
for field in ['created_at', 'id']:
|
||||
for value in [1, 1.5, None, True, [], {}, '', 'invalid']:
|
||||
invalid.append({**payload, field: value})
|
||||
invalid.extend([None, 1, True, [], 'cursor'])
|
||||
for value in invalid:
|
||||
encoded = base64.urlsafe_b64encode(json.dumps(value).encode()).decode().rstrip('=')
|
||||
instances = set()
|
||||
# Проверяем один и тот же cursor на обеих API за round-robin LB.
|
||||
for _ in range(8):
|
||||
response = c.call('GET', path + '?cursor=' + quote(encoded, safe=''), 400, actor=actor)
|
||||
check(response['json']['error']['code'] == 'invalid_request', 'incorrect cursor error code')
|
||||
instances.add(response['headers']['X-Instance-Id'])
|
||||
if len(instances) >= 2:
|
||||
break
|
||||
check(len(instances) >= 2, 'invalid cursor was not checked on two API instances')
|
||||
|
||||
|
||||
def main():
|
||||
def configure(p):
|
||||
p.add_argument('--expiry', action='store_true', help='API must run with TTL=2')
|
||||
args = arguments('Student API checks', configure)
|
||||
c = Client(args.base_url)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
if args.expiry:
|
||||
time.sleep(2.2)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
print('PASS: short TTL expiry')
|
||||
return
|
||||
registration = {'username': 'race_' + uuid4().hex[:12], 'display_name': 'Гонка',
|
||||
'password': 'Synthetic-password-42'}
|
||||
# Для гонки регистрируем оба допустимых исхода через urllib.
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
def register(_):
|
||||
req = urllib.request.Request(c.base + '/api/v1/users',
|
||||
data=json.dumps(registration).encode(),
|
||||
headers={'Content-Type': 'application/json'})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
|
||||
codes = list(executor.map(register, range(8)))
|
||||
check(codes.count(201) == 1 and codes.count(409) == 7, 'username race failed')
|
||||
path = '/api/v1/chats/' + chat['id']
|
||||
c.call('POST', '/api/v1/chats', 400, actor=alice,
|
||||
body={'title': 'unknown member', 'member_ids': [alice['id'], str(uuid4())]})
|
||||
for body in [{'text': 'x', 'sender_id': eve['id']}, {'text': 4}, {'text': ' '}]:
|
||||
c.call('POST', path + '/messages', 400, actor=alice, body=body)
|
||||
c.call('GET', path + '/messages?limit=true', 400, actor=alice)
|
||||
c.call('GET', path + '/messages?cursor=not-a-cursor', 400, actor=alice)
|
||||
for n in range(3):
|
||||
c.message(alice, chat, 'read-after-write ' + str(n))
|
||||
page = c.call('GET', path + '/messages?limit=1', actor=alice, schema='MessagePage')['json']
|
||||
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'other', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
c.call('GET', '/api/v1/chats/' + other['id'] + '/messages?cursor=' +
|
||||
quote(page['next_cursor'], safe=''), 400, actor=alice)
|
||||
check_cursor_types(c, alice, path + '/messages', page['next_cursor'], 'MessagePage')
|
||||
chats = c.call('GET', '/api/v1/chats?limit=1', actor=alice, schema='ChatPage')['json']
|
||||
check_cursor_types(c, alice, '/api/v1/chats', chats['next_cursor'], 'ChatPage')
|
||||
history = c.call('GET', path + '/messages', actor=bob, schema='MessagePage')['json']['items']
|
||||
check(len(history) == 3 and history[-1]['text'] == 'read-after-write 2', 'read-after-write failed')
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||||
print('PASS: registration race, invalid input, cursor scope/types on two API, read-after-write, logout')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Проверка реального rollback после вставки чата и первого участника."""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from uuid import uuid4
|
||||
import psycopg
|
||||
import os
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
from src.config import DATABASE_URL
|
||||
from src.migrate import migrate
|
||||
|
||||
migrate()
|
||||
migrate() # Повтор миграции не создаёт дубликаты.
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
user = db.execute('SELECT id FROM users LIMIT 1').fetchone()
|
||||
assert user, 'Сначала выполните smoke, чтобы создать синтетического пользователя'
|
||||
chat = uuid4()
|
||||
try:
|
||||
with db.transaction():
|
||||
db.execute('INSERT INTO chats(id,title) VALUES (%s,%s)', (chat, 'rollback test'))
|
||||
db.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat, user[0]))
|
||||
# FK прерывает составную операцию ПОСЛЕ двух успешных INSERT.
|
||||
db.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat, uuid4()))
|
||||
except psycopg.errors.ForeignKeyViolation:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('Expected foreign-key failure')
|
||||
assert db.execute('SELECT count(*) FROM chats WHERE id=%s', (chat,)).fetchone()[0] == 0
|
||||
assert db.execute('SELECT count(*) FROM chat_members WHERE chat_id=%s', (chat,)).fetchone()[0] == 0
|
||||
assert db.execute('SELECT count(*) FROM schema_migrations').fetchone()[0] == 2
|
||||
print('PASS: rollback of chat + members; migration reapplied twice')
|
||||
|
||||
c = Client(os.getenv('BASE_URL', 'http://localhost:8080'))
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
key = uuid4().hex
|
||||
original = c.message(alice, chat, 'До истечения ключа', key=key)['json']
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
# Без ожидания суток: сдвигаем именно persisted expiry, а не clock приложения.
|
||||
db.execute("UPDATE message_requests SET expires_at=now()-interval '1 second' "
|
||||
"WHERE chat_id=%s AND request_key=%s", (chat['id'], key))
|
||||
renewed = c.message(alice, chat, 'После истечения ключа', key=key)['json']
|
||||
assert renewed['id'] != original['id']
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
assert db.execute('SELECT count(*) FROM message_requests WHERE chat_id=%s AND request_key=%s',
|
||||
(chat['id'], key)).fetchone()[0] == 1
|
||||
with psycopg.connect(DATABASE_URL) as db:
|
||||
message = uuid4()
|
||||
try:
|
||||
with db.transaction():
|
||||
db.execute('INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s)',
|
||||
(message, chat['id'], bob['id'], 'atomic rollback'))
|
||||
# Ошибка сохранения результата должна откатить и сообщение.
|
||||
db.execute("INSERT INTO message_requests VALUES (%s,%s,%s,%s,%s,now())",
|
||||
(uuid4(), chat['id'], 'rollback', '0'*64, psycopg.types.json.Jsonb({})))
|
||||
except psycopg.errors.ForeignKeyViolation:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('Expected result-store failure')
|
||||
assert db.execute('SELECT count(*) FROM messages WHERE id=%s', (message,)).fetchone()[0] == 0
|
||||
print('PASS: persisted key expiry, atomic message/result rollback')
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Redis необязателен для корректности; без PostgreSQL готовность снимается."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from scripts.benchmark import context, Client
|
||||
from scripts.stack import command, wait, save
|
||||
|
||||
base = os.getenv('BASE_URL', 'http://localhost:8080')
|
||||
c = Client(base)
|
||||
ctx = context(base)
|
||||
original = c.message(ctx['actor'], ctx['chat'], 'До отказов')['json']
|
||||
results = {}
|
||||
try:
|
||||
command(['docker', 'compose', 'stop', 'cache'])
|
||||
started = time.monotonic()
|
||||
for _ in range(6):
|
||||
c.call('GET', '/api/v1/users/me', actor=ctx['actor'], schema='User')
|
||||
c.message(ctx['actor'], ctx['chat'], 'Без Redis')
|
||||
results['redis'] = dict(status='PASS', elapsed_seconds=time.monotonic()-started)
|
||||
finally:
|
||||
command(['docker', 'compose', 'up', '-d', '--wait', 'cache'])
|
||||
try:
|
||||
command(['docker', 'compose', 'stop', 'db'])
|
||||
# Даём LB время исключить все неготовые API. Его собственный 503 может быть HTML.
|
||||
time.sleep(4)
|
||||
codes = []
|
||||
for path in ['/health/ready', '/api/v1/users/me']:
|
||||
request = urllib.request.Request(base+path, headers={'Authorization': 'Bearer '+ctx['actor']['token']})
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=8) as response:
|
||||
codes.append(response.status)
|
||||
except urllib.error.HTTPError as exc:
|
||||
codes.append(exc.code)
|
||||
assert codes == [503, 503], codes
|
||||
results['postgres'] = dict(status='PASS', readiness_and_request_status=codes)
|
||||
finally:
|
||||
command(['docker', 'compose', 'up', '-d', '--wait', 'db'])
|
||||
wait(2, base)
|
||||
page = c.call('GET', '/api/v1/chats/'+ctx['chat']['id']+'/messages', actor=ctx['reader'], schema='MessagePage')['json']
|
||||
assert original in page['items']
|
||||
c.call('GET', '/api/v1/users/me', actor=ctx['actor'], schema='User')
|
||||
results['recovery'] = 'old session and confirmed message preserved'
|
||||
save('dependencies.json', results)
|
||||
print('PASS: Redis fallback, PostgreSQL unready, recovery without new login')
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Проверки общего результата повтора через LB и несколько API."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Shared idempotency through LB')
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Один результат для конкурентных повторов'
|
||||
|
||||
def send(_):
|
||||
return Client(args.base_url, args.ca_file).message(alice, chat, text, key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=16) as pool:
|
||||
responses = list(pool.map(send, range(32)))
|
||||
first = responses[0]['json']
|
||||
check(all(r['json'] == first and r['status'] == 201 for r in responses), 'replay differs')
|
||||
instances = {r['headers']['X-Instance-Id'] for r in responses}
|
||||
check(len(instances) >= 2, 'race did not cross API replicas')
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
history = c.call('GET', path, actor=bob, schema='MessagePage')['json']['items']
|
||||
check(len(history) == 1 and history[0] == first, 'concurrent replay created duplicates')
|
||||
c.call('POST', path, 409, body={'text': 'Другой текст'}, actor=alice,
|
||||
headers={'Idempotency-Key': key})
|
||||
for invalid in ['', 'bad key', 'a' * 129]:
|
||||
c.call('POST', path, 400, body={'text': text}, actor=alice,
|
||||
headers={'Idempotency-Key': invalid})
|
||||
c.call('POST', path, 400, body={'text': text}, actor=alice)
|
||||
# Тот же ключ независим для другого пользователя и другого чата.
|
||||
other = c.message(bob, chat, text, key=key)['json']
|
||||
check(other['id'] != first['id'], 'key scope must include user')
|
||||
chat2 = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
||||
body={'title': 'Second', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
||||
check(c.message(alice, chat2, text, key=key)['json']['id'] != first['id'], 'key scope must include chat')
|
||||
# Пробелы JSON не изменяют смысл текста и не должны менять результат.
|
||||
replay = c.call('POST', path, 201, actor=alice, raw=json.dumps({'text': text}, indent=2).encode(),
|
||||
headers={'Content-Type': 'application/json', 'Idempotency-Key': key}, schema='Message')
|
||||
check(replay['json'] == first, 'JSON formatting changed idempotency')
|
||||
c.call('POST', path, 404, actor=eve, body={'text': text}, headers={'Idempotency-Key': key})
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
c.call('POST', path, 401, actor=alice, body={'text': text}, headers={'Idempotency-Key': key})
|
||||
result = dict(status='PASS', concurrent_requests=32, observed_instances=len(instances),
|
||||
messages_after_race=len(history), conflict=409, invalid_key=400, logout=401)
|
||||
Path('evidence').mkdir(exist_ok=True)
|
||||
Path('evidence/idempotency.json').write_text(json.dumps(result, indent=2))
|
||||
print('PASS: shared idempotency, scope, conflict and auth on replay')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Black-box grading evidence beyond the public smoke test."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, arguments, check, run # noqa: E402
|
||||
|
||||
|
||||
def collect_messages(client, actor, chat, page_size=17):
|
||||
items = []
|
||||
cursor = None
|
||||
while True:
|
||||
path = f"/api/v1/chats/{chat['id']}/messages?limit={page_size}"
|
||||
if cursor:
|
||||
path += "&cursor=" + quote(cursor, safe="")
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
items.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
return items
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Student negative, pagination and concurrency tests")
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
alice, bob, outsider, chat = client.bootstrap()
|
||||
|
||||
client.call(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
400,
|
||||
raw=b'{"username":',
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
client.call(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
415,
|
||||
raw=b'{"username":"wrong_media","display_name":"Wrong"}',
|
||||
headers={"Content-Type": "text/plain"},
|
||||
)
|
||||
client.call("POST", "/api/v1/users", 409, body=alice["registration"])
|
||||
client.call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
400,
|
||||
actor=alice,
|
||||
body={"text": " \t"},
|
||||
)
|
||||
client.call("GET", f"/api/v1/chats/{chat['id']}", 404, actor=outsider)
|
||||
|
||||
prefix = "race-" + uuid.uuid4().hex + "-"
|
||||
|
||||
def send(worker_id):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
return [
|
||||
local.message(alice, chat, f"{prefix}{worker_id}-{index}")["json"]
|
||||
for index in range(10)
|
||||
]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
created = [message for batch in pool.map(send, range(10)) for message in batch]
|
||||
|
||||
check(len(created) == 100, "not all concurrent requests succeeded")
|
||||
check(len({message["id"] for message in created}) == 100, "duplicate message ids returned")
|
||||
|
||||
history = collect_messages(client, bob, chat)
|
||||
concurrent_history = [message for message in history if message["text"].startswith(prefix)]
|
||||
check(len(concurrent_history) == 100, "history does not contain exactly 100 concurrent messages")
|
||||
check(len({message["id"] for message in concurrent_history}) == 100, "history contains duplicates")
|
||||
check(
|
||||
history == sorted(history, key=lambda message: (message["created_at"], uuid.UUID(message["id"]).int)),
|
||||
"multi-page history is out of order",
|
||||
)
|
||||
|
||||
print("PASS: malformed JSON/media type, duplicate username, blank message, outsider access, pagination")
|
||||
print("PASS: 10 clients created exactly 100 unique messages")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user