diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..eeed9d1f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.11.9-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 + +WORKDIR /app +RUN groupadd --system app && useradd --system --gid app --home-dir /app app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app ./app +COPY migrations ./migrations +COPY alembic.ini . +RUN chown -R app:app /app +USER app +EXPOSE 8080 +HEALTHCHECK --interval=5s --timeout=3s --start-period=20s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-keep-alive", "5", "--timeout-graceful-shutdown", "20"] diff --git a/REPORT.md b/REPORT.md index c2679005..60138c5a 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,350 @@ -# Отчёт — лабораторная 3 +# Отчёт — лабораторная работа №3 +## «Балансировка и отказ API-реплики» -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- **Автор:** Перевалова Виктория +- **Группа:** БПИ2302 +- **Целевая оценка:** 5 +- **Выбранный исследовательский трек:** Track 1 — цена масштабирования -## Запуск из чистого clone +--- -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +## 1. Цель работы -## Архитектура и решения +Цель лабораторной работы — реализовать систему с несколькими взаимозаменяемыми экземплярами API, работающими за единой точкой входа, обеспечить балансировку запросов и сохранение корректности при отказе одной API-реплики. -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Дополнительные задачи: -## Доказательства критериев +- вынести общее состояние за пределы API-контейнеров; +- реализовать общую идемпотентность операций отправки сообщений; +- обеспечить автоматическое исключение недоступных реплик из балансировки; +- проверить восстановление при отказе экземпляров API; +- исследовать влияние количества реплик и доступных ресурсов на производительность системы. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +## 2. Архитектура и принятые решения -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +### 2.1. Общая архитектура -## Эксперименты и отказы +Система построена на Docker Compose и включает следующие компоненты: -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +- **Traefik 3.6** — единая точка входа и балансировщик нагрузки; +- **API × N** — несколько экземпляров приложения на FastAPI; +- **PostgreSQL 16.6** — основное долговременное хранилище; +- **Redis 7.4.2** — общее хранилище сессий и вспомогательных данных; +- **Migrate** — отдельный одноразовый процесс выполнения миграций Alembic. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +Схема взаимодействия: -## Ограничения и следующие шаги +```text + Клиент + | + v + Traefik 3.6 + / \ + v v + API replica 1 API replica 2 + | | + +------+------+ + | + +--------+--------+ + | | + v v + PostgreSQL Redis + (данные) (сессии) +``` -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Все API-реплики используют общие PostgreSQL и Redis. Поэтому состояние пользователя не зависит от конкретного экземпляра приложения. + +API-контейнеры не публикуют порты непосредственно на хост. Внешний доступ осуществляется через Traefik. + +### 2.2. Балансировка + +Traefik распределяет запросы между доступными API-репликами с использованием стратегии Weighted Round Robin. + +Для обнаружения недоступных экземпляров настроена проверка: + +- endpoint: `/health/ready`; +- интервал: 3 секунды; +- timeout: 2 секунды. + +При недоступности реплики балансировщик перестаёт направлять на неё запросы. + +Для наблюдения за экземпляром, обработавшим запрос, API возвращает заголовок `X-Instance-Id`. + +Sticky sessions не используются. + +### 2.3. Хранение данных и миграции + +PostgreSQL является основным источником долговременных данных. Redis используется как общее хранилище сессий и вспомогательное хранилище. + +Миграции выполняются отдельно от API посредством Alembic: + +```bash +alembic upgrade head +``` + +В Docker Compose предусмотрен отдельный одноразовый сервис миграций. API запускается после успешного завершения миграций и проверки готовности PostgreSQL и Redis. + +### 2.4. Идемпотентность + +Для отправки сообщения используется обязательный заголовок `Idempotency-Key`. + +Реализованы следующие сценарии: + +1. Повтор запроса с тем же ключом и тем же содержимым возвращает результат первоначальной операции. +2. Повторное создание сообщения не происходит. +3. Использование того же ключа с другим содержимым приводит к ответу `409 Conflict`. +4. Конкурентные запросы с одинаковым ключом не создают дубликаты. +5. Идемпотентность сохраняется при обработке запросов разными API-репликами. + +## 3. Запуск проекта + +### 3.1. Требования + +Для запуска необходимы: + +- Docker Desktop; +- Docker Compose v2; +- Git. + +### 3.2. Получение проекта + +```bash +git clone https://gitea.privcloud.tech/perevalova/mtusi2026lab3.git +cd mtusi2026lab3 +git checkout solution/lab3-implementation +``` + +### 3.3. Подготовка конфигурации + +```bash +cp .env.example .env +``` + +### 3.4. Проверка и запуск + +```bash +docker compose config --quiet +docker compose up -d --build +docker compose ps +``` + +Для просмотра состояния сервисов: + +```bash +docker compose ps +``` + +Для просмотра журналов: + +```bash +docker compose logs -f +``` + +Единая точка входа приложения: + +```text +http://localhost:8080 +``` + +### 3.5. Остановка + +Без удаления сохранённых данных: + +```bash +docker compose down +``` + +Для намеренного удаления учебных данных вместе с volumes: + +```bash +docker compose down -v +``` + +Последняя команда удаляет данные PostgreSQL и Redis и должна применяться только при необходимости полного сброса стенда. + +## 4. Проверка функциональных требований + +### 4.1. Работа нескольких реплик + +Проверено обращение к API через Traefik с использованием заголовка `X-Instance-Id`. + +Запросы одного пользователя обрабатывались разными экземплярами API без повторной аутентификации. + +Результат: общая сессия доступна независимо от выбранной реплики. Sticky sessions не требуются. + +### 4.2. Проверка идемпотентности + +Проверены: + +- повторная отправка с тем же ключом; +- повтор с изменённым содержимым; +- 10 конкурентных запросов с одним ключом; +- получение истории сообщений после конкурентных запросов. + +Результат: запросы с одинаковым ключом и содержимым возвращают один и тот же результат. Дубликаты сообщений не создаются. + +### 4.3. Проверка отказа API-реплики + +Для проверки использовался сценарий: + +```bash +BASH_EXE="$(cygpath -w "$(command -v bash)")" \ +python tests/resilience.py --mode failover --action-script scripts/stop-one.sh +``` + +Сценарий последовательно проверял отказ каждой реплики. + +| Проверяемая реплика | Успешные записи | Неуспешные попытки | Восстановление | +|---|---:|---:|---:| +| Replica 1 | 83 | 3 | 4,195 с | +| Replica 2 | 87 | 3 | 4,051 с | + +В обоих случаях проверка завершилась успешно. + +Результаты сохранены: + +- `evidence/failover/replica-1.txt`; +- `evidence/failover/replica-2.txt`. + +Время восстановления в обоих сценариях не превысило 10 секунд. + +### 4.4. Проверка rolling update + +Для последовательной замены API-реплик реализован скрипт: + +```bash +bash scripts/rolling-update.sh +``` + +Скрипт выполняет следующие действия: + +1. Создаёт новую реплику. +2. Ожидает её готовности. +3. Переводит старую реплику в состояние drain. +4. Ожидает прекращения направления новых запросов. +5. Останавливает старую реплику. +6. Повторяет процесс для следующего экземпляра. + +Для проверки использовалась нагрузка с восемью параллельными клиентами в течение примерно 90 секунд. + +Последний диагностический запуск: + +| Метрика | Результат | +|---|---:| +| Длительность | 91,245 с | +| Параллельные клиенты | 8 | +| Успешные запросы | 6050 | +| Неуспешные запросы | 20 | +| Доля ошибок | 0,3295% | +| Успешный RPS | 66,305 | +| p50 | 87,748 мс | +| p95 | 280,709 мс | +| p99 | 427,002 мс | + +Все зарегистрированные ошибки в последнем запуске — HTTP 502. Тайм-аутов в этом запуске не зафиксировано. + +По сравнению с предыдущим диагностическим запуском число ошибок уменьшилось с 22 до 20, а тайм-ауты исчезли. Однако полностью устранить кратковременные HTTP 502 пока не удалось. + +Поэтому rolling update считается реализованным и проверенным под нагрузкой, но не безошибочным. + +## 5. Исследовательский трек 1. Цена масштабирования + +### 5.1. Цель эксперимента + +Исследовать влияние количества API-реплик на производительность системы при фиксированном суммарном объёме ресурсов. + +Гипотеза: увеличение количества реплик повышает пропускную способность, но эффект масштабирования постепенно уменьшается из-за общих инфраструктурных компонентов. + +После сравнения количества реплик проводится дополнительная серия с увеличением доступных CPU и RAM. + +### 5.2. Методика + +В первой серии сравнивались 1, 2 и 4 API-реплики при одинаковом суммарном бюджете: + +- CPU: 2; +- RAM: 2048 MB. + +Во второй серии использовались 4 API-реплики, а доступные ресурсы увеличивались. + +Результаты экспериментов сохранены в каталоге `evidence/scaling/`, включая исходные JSON, сводную таблицу и графики. + +### 5.3. Сравнение количества реплик + +| Конфигурация | RPS | p95, мс | Ошибки | +|---|---:|---:|---:| +| 1 реплика | 37,541 | 302,388 | 0 | +| 2 реплики | 59,211 | 294,656 | 0 | +| 4 реплики | 65,877 | 390,895 | 0 | + +При переходе от одной реплики к двум пропускная способность выросла примерно на 57,7%. + +При переходе от двух реплик к четырём прирост составил около 11,3%. + +Это демонстрирует снижение эффективности дальнейшего горизонтального масштабирования при неизменном суммарном бюджете ресурсов. + +### 5.4. Серия с увеличением ресурсов + +| API-реплики | CPU | RAM, MB | RPS | p95, мс | +|---|---:|---:|---:|---:| +| 4 | 2 | 2048 | 68,467 | 374,351 | +| 4 | 3 | 3072 | 95,214 | 238,599 | +| 4 | 4 | 4096 | 119,589 | 137,459 | + +При увеличении ресурсов с 2 CPU / 2048 MB до 4 CPU / 4096 MB пропускная способность выросла примерно на 74,7%. + +Значение p95 latency уменьшилось примерно на 63,3%. + +### 5.5. Выводы по эксперименту + +Эксперимент показал, что горизонтальное масштабирование при фиксированном суммарном объёме ресурсов имеет убывающую отдачу. + +Увеличение количества реплик с одной до двух существенно повышает пропускную способность. Переход от двух к четырём репликам даёт значительно меньший прирост и сопровождается увеличением p95 latency. + +Дополнительное увеличение CPU и RAM при четырёх репликах улучшило пропускную способность и задержки. + +При этом точка насыщения PostgreSQL, Traefik или пула соединений не была однозначно локализована: для такого вывода необходимы отдельные измерения загрузки БД, ожидания соединений, CPU балансировщика и состояния пула. + +Следовательно, эксперимент подтверждает влияние количества реплик и доступных ресурсов на производительность, но не позволяет достоверно приписать снижение эффективности конкретному компоненту. + +## 6. Итоговая проверка критериев + +| Требование | Результат | Доказательство | +|---|---|---| +| Не менее двух API-реплик | Выполнено | `docker compose ps` | +| Единая точка входа | Выполнено | Traefik, порт 8080 | +| Общая PostgreSQL и Redis | Выполнено | `compose.yaml` | +| Разные `X-Instance-Id` | Проверено | Результаты функциональных проверок | +| Отсутствие sticky sessions | Выполнено | Общая сессия между репликами | +| Обязательный `Idempotency-Key` | Проверено | Конкурентные тесты | +| Readiness и healthcheck | Выполнено | `/health/ready`, Docker healthcheck | +| Отказ каждой API-реплики | PASS | `evidence/failover/` | +| Восстановление не более 10 секунд | PASS | 4,195 и 4,051 секунды | +| CPU/RAM limits | Выполнено | `compose.yaml` | +| API-порты не опубликованы | Выполнено | `docker compose ps` | +| Однократные миграции | Выполнено | Сервис `migrate` | +| Rolling update под нагрузкой | Выполнено с ограничением | `evidence/rolling-update-final.json` | +| Сравнение 1/2/4 реплик | Выполнено | `evidence/scaling/summary.csv` | +| Серия увеличения ресурсов | Выполнено | `evidence/scaling/` | +| Локализация saturation point | Не подтверждена | Требуется дополнительная телеметрия | + +## 7. Ограничения реализации + +1. Rolling update пока сопровождается небольшим количеством кратковременных HTTP 502. Поэтому нельзя утверждать, что замена реплик полностью бесшовна. +2. Точка насыщения БД, балансировщика или пула соединений экспериментально не локализована. +3. Стенд работает на одном Docker host. Отказ всего хоста не покрывается данной архитектурой. +4. PostgreSQL и Traefik остаются отдельными потенциальными точками отказа. +5. Результаты производительности относятся к конкретному окружению и не являются универсальными характеристиками приложения. + +Для дальнейшего улучшения необходимо исследовать метрики PostgreSQL и пула соединений, дополнительно настроить drain и проверку состояния backend перед остановкой старого экземпляра, а также рассмотреть отказоустойчивость самой БД и балансировщика. + +## 8. Заключение + +В лабораторной работе реализована система с несколькими API-репликами, общей базой данных, общим хранилищем сессий и балансировщиком Traefik. + +Проверены распределение запросов между репликами, сохранение сессии, общая идемпотентность, конкурентные запросы и восстановление после остановки каждой API-реплики. + +Время восстановления в двух сценариях отказа составило около четырёх секунд, что укладывается в установленный предел 10 секунд. + +В рамках исследовательского трека выполнено сравнение одной, двух и четырёх API-реплик при фиксированном бюджете ресурсов, а также серия экспериментов с увеличением CPU и RAM. Полученные результаты показали убывающую отдачу от горизонтального масштабирования и положительное влияние увеличения ресурсов на пропускную способность. + +Таким образом, основные механизмы балансировки, отказоустойчивости и масштабирования реализованы и проверены. При этом ограничения rolling update и отсутствие однозначно установленной точки насыщения инфраструктурных компонентов явно зафиксированы, а не скрыты в итоговых выводах. \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..421e3fe9 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,117 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +# Use forward slashes (/) also on windows to provide an os agnostic path +script_location = migrations + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to migrations/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +# version_path_separator = newline +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = driver://user:pass@localhost/dbname + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARNING +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARNING +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/__init__.py b/app/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/auth.py b/app/api/auth.py new file mode 100644 index 00000000..bea835c4 --- /dev/null +++ b/app/api/auth.py @@ -0,0 +1,96 @@ +from datetime import timezone +from fastapi import APIRouter, Depends, Header, HTTPException, status +from fastapi.security import HTTPBasic, HTTPBasicCredentials +from sqlalchemy.orm import Session + +from app.api.deps import get_db +from app.api.users import user_to_response +from app.services.auth_service import ( + AuthService, + InvalidCredentialsError, + InvalidSessionError, +) + + +router = APIRouter(prefix="/api/v1/auth", tags=["Authentication"]) + +basic_security = HTTPBasic(auto_error=False) + + + +@router.post("/sessions", status_code=status.HTTP_201_CREATED) +def create_session( + credentials: HTTPBasicCredentials = Depends(basic_security), + db: Session = Depends(get_db), +): + if credentials is None: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Basic authentication required", + headers={ + "WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"' + }, + ) + + try: + result = AuthService(db).create_session( + username=credentials.username, + password=credentials.password, + ) + + expires_at = result["expires_at"] + + if expires_at.tzinfo is None: + expires_at = expires_at.replace(tzinfo=timezone.utc) + else: + expires_at = expires_at.astimezone(timezone.utc) + + return { + "token": result["token"], + "token_type": result["token_type"], + "expires_at": expires_at.isoformat( + timespec="microseconds" + ).replace("+00:00", "Z"), + "user": user_to_response(result["user"]), + } + + except InvalidCredentialsError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid username or password", + headers={ + "WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"' + }, + ) from None + + + +@router.delete("/sessions/current", status_code=status.HTTP_204_NO_CONTENT) +def delete_current_session( + authorization: str | None = Header(default=None), + db: Session = Depends(get_db), +): + if not authorization or not authorization.startswith("Bearer "): + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + + token = authorization[7:].strip() + + if not token: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + + try: + AuthService(db).delete_session(token) + except InvalidSessionError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid or expired session", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) from None diff --git a/app/api/chats.py b/app/api/chats.py new file mode 100644 index 00000000..40c46261 --- /dev/null +++ b/app/api/chats.py @@ -0,0 +1,90 @@ +import uuid + +from fastapi import APIRouter, Depends, HTTPException, Query, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import Chat as ChatResponse, CreateChat +from app.services.chat_service import ( + ChatService, + ChatNotFoundError, + InvalidChatDataError, +) + + +router = APIRouter(prefix="/api/v1/chats", tags=["Chats"]) + + +def chat_to_response(chat) -> ChatResponse: + return ChatResponse( + id=str(chat.id), + title=chat.title, + member_ids=[str(member.user_id) for member in chat.members], + created_at=chat.created_at, + ) + + +@router.post("", response_model=ChatResponse, status_code=status.HTTP_201_CREATED) +def create_chat( + body: CreateChat, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + try: + chat = ChatService(db).create_chat( + title=body.title, + member_ids=body.member_ids, + creator_id=user.id, + ) + return chat_to_response(chat) + + except InvalidChatDataError as exc: + raise HTTPException( + status_code=400, + detail=str(exc), + ) from None + + +@router.get("") +def list_chats( + limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = None, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + if cursor is not None: + raise HTTPException( + status_code=400, + detail="Chat cursor pagination is not implemented yet", + ) + + chats = ChatService(db).list_chats(user.id) + chats = chats[:limit] + + return { + "items": [chat_to_response(chat) for chat in chats], + "next_cursor": None, + } + + +@router.get("/{chat_id}", response_model=ChatResponse) +def get_chat( + chat_id: str, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + try: + parsed_id = uuid.UUID(chat_id) + except ValueError: + raise HTTPException(status_code=400, detail="Invalid chat UUID") from None + + try: + chat = ChatService(db).get_chat(parsed_id, user.id) + return chat_to_response(chat) + + except ChatNotFoundError: + raise HTTPException( + status_code=404, + detail="Chat not found", + ) from None diff --git a/app/api/deps.py b/app/api/deps.py new file mode 100644 index 00000000..6c695862 --- /dev/null +++ b/app/api/deps.py @@ -0,0 +1,40 @@ +from fastapi import Depends, HTTPException, status +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from sqlalchemy.orm import Session + +from app.db.session import SessionLocal +from app.services.auth_service import AuthService, InvalidSessionError + + +bearer_scheme = HTTPBearer(auto_error=False) + + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +def get_current_user( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_scheme), + db: Session = Depends(get_db), +): + if credentials is None or credentials.scheme.lower() != "bearer": + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": "Bearer"}, + ) + + try: + user = AuthService(db).authenticate(credentials.credentials) + return user + + except InvalidSessionError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid or expired token", + headers={"WWW-Authenticate": "Bearer"}, + ) from None diff --git a/app/api/messages.py b/app/api/messages.py new file mode 100644 index 00000000..9b642345 --- /dev/null +++ b/app/api/messages.py @@ -0,0 +1,73 @@ +import re +import uuid + +from fastapi import APIRouter, Depends, Header, HTTPException, Query, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import CreateMessage, Message as MessageResponse +from app.services.message_service import ( + MessageService, MessageNotFoundError, InvalidMessageDataError, + IdempotencyConflictError, +) + +router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["Messages"]) +IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$") + + +def message_to_response(message) -> MessageResponse: + return MessageResponse( + id=str(message.id), chat_id=str(message.chat_id), + sender_id=str(message.sender_id), text=message.text, + created_at=message.created_at, + ) + + +def parse_chat_id(chat_id: str) -> uuid.UUID: + try: + return uuid.UUID(chat_id) + except ValueError: + raise HTTPException(status_code=400, detail="Invalid chat UUID") from None + + +@router.post("", response_model=MessageResponse, status_code=status.HTTP_201_CREATED) +def create_message( + chat_id: str, body: CreateMessage, + idempotency_key: str = Header(..., alias="Idempotency-Key", min_length=1, max_length=128), + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + if not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key): + raise HTTPException(status_code=400, detail="Invalid Idempotency-Key") + parsed_id = parse_chat_id(chat_id) + try: + message = MessageService(db).create_message( + chat_id=parsed_id, sender_id=user.id, text=body.text, + idempotency_key=idempotency_key, + ) + return message_to_response(message) + except MessageNotFoundError: + raise HTTPException(status_code=404, detail="Chat not found") from None + except InvalidMessageDataError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from None + except IdempotencyConflictError: + raise HTTPException(status_code=409, detail="Idempotency-Key was already used with a different request") from None + + +@router.get("") +def list_messages( + chat_id: str, limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = None, db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + parsed_id = parse_chat_id(chat_id) + try: + messages, next_cursor = MessageService(db).get_history( + chat_id=parsed_id, user_id=user.id, limit=limit, cursor=cursor + ) + return {"items": [message_to_response(item) for item in messages], "next_cursor": next_cursor} + except MessageNotFoundError: + raise HTTPException(status_code=404, detail="Chat not found") from None + except InvalidMessageDataError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from None diff --git a/app/api/users.py b/app/api/users.py new file mode 100644 index 00000000..67adc7c6 --- /dev/null +++ b/app/api/users.py @@ -0,0 +1,55 @@ +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import CreateUser, User +from app.services.user_service import ( + UserService, + UsernameAlreadyExistsError, +) + + +router = APIRouter(prefix="/api/v1/users", tags=["Users"]) + + +def user_to_response(user: UserModel) -> User: + return User( + id=str(user.id), + username=user.username, + display_name=user.display_name, + created_at=user.created_at, + ) + + +@router.post("", response_model=User, status_code=status.HTTP_201_CREATED) +def register_user( + body: CreateUser, + db: Session = Depends(get_db), +): + try: + user = UserService(db).register( + username=body.username, + display_name=body.display_name, + password=body.password, + ) + return user_to_response(user) + + except UsernameAlreadyExistsError: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Username already exists", + ) from None + + except ValueError as exc: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=str(exc), + ) from None + + +@router.get("/me", response_model=User) +def get_me( + current_user: UserModel = Depends(get_current_user), +): + return user_to_response(current_user) diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 00000000..ec78b7be --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,24 @@ +import os +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + app_name: str = "Messenger API" + app_env: str = "lab" + database_url: str = "postgresql+psycopg://messenger:messenger@postgres:5432/messenger" + db_pool_size: int = 5 + db_max_overflow: int = 5 + db_pool_timeout: int = 10 + session_ttl_seconds: int = 3600 + cache_url: str = "redis://redis:6379/0" + message_cache_ttl_seconds: int = 60 + idempotency_ttl_seconds: int = 86400 + instance_id: str = os.getenv("INSTANCE_ID") or os.getenv("HOSTNAME") or "api-local" + shutdown_grace_seconds: int = 20 + + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", extra="ignore" + ) + + +settings = Settings() diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/db/base.py b/app/db/base.py new file mode 100644 index 00000000..1c2dcc40 --- /dev/null +++ b/app/db/base.py @@ -0,0 +1,5 @@ +from sqlalchemy.orm import DeclarativeBase + + +class Base(DeclarativeBase): + pass \ No newline at end of file diff --git a/app/db/models.py b/app/db/models.py new file mode 100644 index 00000000..772fdcff --- /dev/null +++ b/app/db/models.py @@ -0,0 +1,215 @@ +import uuid + +from datetime import datetime, timezone + +from sqlalchemy import ( + DateTime, + ForeignKey, + Index, + String, + Text, + UniqueConstraint, +) +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.db.base import Base + + +def utc_now() -> datetime: + return datetime.now(timezone.utc) + + +class User(Base): + __tablename__ = "users" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + username: Mapped[str] = mapped_column( + String(32), + unique=True, + nullable=False, + index=True, + ) + display_name: Mapped[str] = mapped_column( + String(100), + nullable=False, + ) + password_hash: Mapped[str] = mapped_column( + String(255), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + memberships: Mapped[list["ChatMember"]] = relationship( + back_populates="user", + cascade="all, delete-orphan", + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="sender", + ) + + +class Chat(Base): + __tablename__ = "chats" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + title: Mapped[str] = mapped_column( + String(100), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + members: Mapped[list["ChatMember"]] = relationship( + back_populates="chat", + cascade="all, delete-orphan", + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="chat", + cascade="all, delete-orphan", + ) + + +class ChatMember(Base): + __tablename__ = "chat_members" + + chat_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + primary_key=True, + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + primary_key=True, + ) + + chat: Mapped["Chat"] = relationship( + back_populates="members", + ) + user: Mapped["User"] = relationship( + back_populates="memberships", + ) + + +class Message(Base): + __tablename__ = "messages" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + chat_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + nullable=False, + ) + sender_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("users.id"), + nullable=False, + ) + text: Mapped[str] = mapped_column( + Text, + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + chat: Mapped["Chat"] = relationship( + back_populates="messages", + ) + sender: Mapped["User"] = relationship( + back_populates="messages", + ) + + __table_args__ = ( + Index( + "ix_messages_chat_created_id", + "chat_id", + "created_at", + "id", + ), + ) + + +class Session(Base): + __tablename__ = "sessions" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + token_hash: Mapped[str] = mapped_column( + String(64), + unique=True, + nullable=False, + index=True, + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + nullable=False, + ) + expires_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + user: Mapped["User"] = relationship() + +class IdempotencyRecord(Base): + __tablename__ = "idempotency_records" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False + ) + method: Mapped[str] = mapped_column(String(10), nullable=False) + path: Mapped[str] = mapped_column(String(255), nullable=False) + idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False) + request_hash: Mapped[str] = mapped_column(String(64), nullable=False) + # NULL is visible only inside the creating transaction; the message and + # idempotency row are committed atomically. + message_id: Mapped[uuid.UUID | None] = mapped_column( + UUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), nullable=True + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=utc_now, nullable=False + ) + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) + + __table_args__ = ( + UniqueConstraint( + "user_id", "method", "path", "idempotency_key", + name="uq_idempotency_scope_key", + ), + Index("ix_idempotency_records_expires_at", "expires_at"), + ) diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 00000000..190589bd --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,15 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from app.core.config import settings + +engine = create_engine( + settings.database_url, + pool_pre_ping=True, + pool_size=settings.db_pool_size, + max_overflow=settings.db_max_overflow, + pool_timeout=settings.db_pool_timeout, + pool_recycle=1800, +) + +SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..32fc2746 --- /dev/null +++ b/app/main.py @@ -0,0 +1,86 @@ +import logging +import uuid +from contextlib import asynccontextmanager + +from fastapi import FastAPI, HTTPException, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from pathlib import Path +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError + +from app.api.auth import router as auth_router +from app.api.users import router as users_router +from app.api.chats import router as chats_router +from app.api.messages import router as messages_router +from app.core.config import settings +from app.db.session import engine + +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") +logger = logging.getLogger(__name__) +@asynccontextmanager +async def lifespan(app: FastAPI): + logger.info("Application startup instance_id=%s", settings.instance_id) + try: + yield + finally: + engine.dispose() + logger.info("Application shutdown instance_id=%s", settings.instance_id) + +app = FastAPI(title=settings.app_name, version="1.3.0", lifespan=lifespan) +app.include_router(users_router) +app.include_router(auth_router) +app.include_router(chats_router) +app.include_router(messages_router) + +@app.middleware("http") +async def response_headers(request: Request, call_next): + request_id = str(uuid.uuid4()) + request.state.request_id = request_id + try: + response = await call_next(request) + except Exception: + logger.exception("Unhandled request error; request_id=%s", request_id) + response = JSONResponse(status_code=500, content={"error": {"code": "internal_error", "message": "Unexpected error", "request_id": request_id}}) + response.headers["X-Request-Id"] = request_id + response.headers["X-Instance-Id"] = settings.instance_id + response.headers["X-Content-Type-Options"] = "nosniff" + return response + +@app.exception_handler(HTTPException) +async def http_exception_handler(request: Request, exc: HTTPException): + request_id = getattr(request.state, "request_id", str(uuid.uuid4())) + code = {400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict", 413: "payload_too_large", 415: "unsupported_media_type", 429: "rate_limited", 500: "internal_error", 503: "unavailable"}.get(exc.status_code, "internal_error") + headers = dict(exc.headers or {}) + headers["X-Request-Id"] = request_id + headers["X-Instance-Id"] = settings.instance_id + return JSONResponse(status_code=exc.status_code, content={"error": {"code": code, "message": exc.detail if isinstance(exc.detail, str) else "Request failed", "request_id": request_id}}, headers=headers) + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request: Request, exc: RequestValidationError): + request_id = getattr(request.state, "request_id", str(uuid.uuid4())) + return JSONResponse(status_code=400, content={"error": {"code": "invalid_request", "message": "Validation error", "request_id": request_id}}, headers={"X-Request-Id": request_id, "X-Instance-Id": settings.instance_id}) + +@app.get("/health/live") +def liveness(): + return {"status": "ok"} + +DRAIN_FILE = Path("/tmp/api-draining") +@app.get("/health/ready") +def readiness(): + if DRAIN_FILE.exists(): + raise HTTPException( + status_code=503, + detail="Instance is draining", + ) + + try: + with engine.connect() as connection: + connection.execute(text("SELECT 1")) + return {"status": "ok"} + except SQLAlchemyError: + logger.exception("PostgreSQL readiness check failed") + raise HTTPException( + status_code=503, + detail="Database unavailable", + ) from None diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..ace6808f --- /dev/null +++ b/app/models.py @@ -0,0 +1,110 @@ + +from datetime import datetime, timezone +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class BaseConfig(BaseModel): + model_config = ConfigDict( + from_attributes=True, + json_encoders={ + datetime: lambda value: ( + value.astimezone(timezone.utc) + .isoformat(timespec="microseconds") + .replace("+00:00", "Z") + ) + }, + ) + + +class CreateUser(BaseConfig): + 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("display_name") + @classmethod + def validate_display_name(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("display_name must not be blank") + return value + + +class User(BaseConfig): + id: str + username: str + display_name: str + created_at: datetime + + +class CreateChat(BaseConfig): + title: str = Field(..., min_length=1, max_length=100) + member_ids: List[str] = Field(..., min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def validate_title(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("title must not be blank") + return value + + @field_validator("member_ids") + @classmethod + def validate_unique_members(cls, value: List[str]) -> List[str]: + if len(value) != len(set(value)): + raise ValueError("member_ids must be unique") + return value + + +class Chat(BaseConfig): + id: str + title: str + member_ids: List[str] + created_at: datetime + + +class CreateMessage(BaseConfig): + text: str = Field(..., min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def validate_text(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("text must not be blank") + return value + + +class Message(BaseConfig): + id: str + chat_id: str + sender_id: str + text: str + created_at: datetime + + +class Health(BaseConfig): + status: str = "ok" + + +class ErrorDetail(BaseConfig): + code: str + message: str + request_id: str + + +class ErrorResponse(BaseConfig): + error: ErrorDetail + + +class ChatPage(BaseConfig): + items: List[Chat] + next_cursor: Optional[str] = None + + +class MessagePage(BaseConfig): + items: List[Message] + next_cursor: Optional[str] = None diff --git a/app/repositories/__init__.py b/app/repositories/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/repositories/chat_repository.py b/app/repositories/chat_repository.py new file mode 100644 index 00000000..e5cd838e --- /dev/null +++ b/app/repositories/chat_repository.py @@ -0,0 +1,71 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session, selectinload + +from app.db.models import Chat, ChatMember + + +class ChatRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, chat_id: uuid.UUID) -> Chat | None: + statement = ( + select(Chat) + .options(selectinload(Chat.members)) + .where(Chat.id == chat_id) + ) + + return self.db.scalar(statement) + + def get_by_user( + self, + user_id: uuid.UUID, + limit: int = 50, + ) -> list[Chat]: + statement = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(ChatMember.user_id == user_id) + .order_by(Chat.created_at.asc(), Chat.id.asc()) + .limit(limit) + ) + + return list(self.db.scalars(statement).all()) + + def is_member( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + ) -> bool: + statement = select(ChatMember).where( + ChatMember.chat_id == chat_id, + ChatMember.user_id == user_id, + ) + + return self.db.scalar(statement) is not None + + def create( + self, + title: str, + member_ids: list[uuid.UUID], + ) -> Chat: + chat = Chat(title=title) + + self.db.add(chat) + self.db.flush() + + memberships = [ + ChatMember( + chat_id=chat.id, + user_id=user_id, + ) + for user_id in member_ids + ] + + self.db.add_all(memberships) + self.db.flush() + + return chat \ No newline at end of file diff --git a/app/repositories/message_repository.py b/app/repositories/message_repository.py new file mode 100644 index 00000000..6698ed1d --- /dev/null +++ b/app/repositories/message_repository.py @@ -0,0 +1,71 @@ +import uuid + +from datetime import datetime + +from sqlalchemy import and_, or_, select +from sqlalchemy.orm import Session + +from app.db.models import Message + + +class MessageRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id( + self, + message_id: uuid.UUID, + ) -> Message | None: + statement = select(Message).where( + Message.id == message_id + ) + + return self.db.scalar(statement) + + def create( + self, + chat_id: uuid.UUID, + sender_id: uuid.UUID, + text: str, + ) -> Message: + message = Message( + chat_id=chat_id, + sender_id=sender_id, + text=text, + ) + + self.db.add(message) + self.db.flush() + + return message + + def get_history( + self, + chat_id: uuid.UUID, + limit: int = 50, + cursor_created_at: datetime | None = None, + cursor_id: uuid.UUID | None = None, + ) -> list[Message]: + statement = select(Message).where( + Message.chat_id == chat_id + ) + + if cursor_created_at is not None and cursor_id is not None: + statement = statement.where( + or_( + Message.created_at > cursor_created_at, + and_( + Message.created_at == cursor_created_at, + Message.id > cursor_id, + ), + ) + ) + + statement = ( + statement + .order_by(Message.created_at.asc(), Message.id.asc()) + .limit(limit) + ) + + return list(self.db.scalars(statement).all()) \ No newline at end of file diff --git a/app/repositories/session_repository.py b/app/repositories/session_repository.py new file mode 100644 index 00000000..7366b170 --- /dev/null +++ b/app/repositories/session_repository.py @@ -0,0 +1,78 @@ +import uuid + +from datetime import datetime, timezone + +from sqlalchemy import delete, select +from sqlalchemy.orm import Session + +from app.db.models import Session as UserSession + + +class SessionRepository: + + def __init__(self, db: Session): + self.db = db + + def create( + self, + user_id: uuid.UUID, + token_hash: str, + expires_at: datetime, + ) -> UserSession: + session = UserSession( + user_id=user_id, + token_hash=token_hash, + expires_at=expires_at, + ) + + self.db.add(session) + self.db.flush() + + return session + + def get_by_token_hash( + self, + token_hash: str, + ) -> UserSession | None: + statement = select(UserSession).where( + UserSession.token_hash == token_hash + ) + + return self.db.scalar(statement) + + def get_valid_session( + self, + token_hash: str, + ) -> UserSession | None: + now = datetime.now(timezone.utc) + + statement = select(UserSession).where( + UserSession.token_hash == token_hash, + UserSession.expires_at > now, + ) + + return self.db.scalar(statement) + + def delete_by_token_hash( + self, + token_hash: str, + ) -> bool: + statement = ( + delete(UserSession) + .where(UserSession.token_hash == token_hash) + .returning(UserSession.id) + ) + + return self.db.scalar(statement) is not None + + def delete_expired(self) -> int: + now = datetime.now(timezone.utc) + + statement = ( + delete(UserSession) + .where(UserSession.expires_at <= now) + ) + + result = self.db.execute(statement) + + return result.rowcount \ No newline at end of file diff --git a/app/repositories/user_repository.py b/app/repositories/user_repository.py new file mode 100644 index 00000000..4569dcd1 --- /dev/null +++ b/app/repositories/user_repository.py @@ -0,0 +1,41 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session + +from app.db.models import User + + +class UserRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, user_id: uuid.UUID) -> User | None: + statement = select(User).where(User.id == user_id) + + return self.db.scalar(statement) + + def get_by_username(self, username: str) -> User | None: + statement = select(User).where( + User.username == username + ) + + return self.db.scalar(statement) + + def create( + self, + username: str, + display_name: str, + password_hash: str, + ) -> User: + user = User( + username=username, + display_name=display_name, + password_hash=password_hash, + ) + + self.db.add(user) + self.db.flush() + + return user \ No newline at end of file diff --git a/app/services/__init__.py b/app/services/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/services/auth_service.py b/app/services/auth_service.py new file mode 100644 index 00000000..e459ddf3 --- /dev/null +++ b/app/services/auth_service.py @@ -0,0 +1,159 @@ +import hashlib +import secrets +from datetime import datetime, timedelta, timezone + +from redis import Redis +from redis.backoff import NoBackoff +from redis.exceptions import RedisError +from redis.retry import Retry +from sqlalchemy.orm import Session + +from app.core.config import settings +from app.db.models import User +from app.repositories.session_repository import SessionRepository +from app.repositories.user_repository import UserRepository +from app.services.user_service import password_hasher + + +class InvalidCredentialsError(Exception): + pass + + +class InvalidSessionError(Exception): + pass + + +class AuthService: + def __init__(self, db: Session): + self.db = db + self.sessions = SessionRepository(db) + self.users = UserRepository(db) + + self._redis_available = True + + self.redis = Redis.from_url( + settings.cache_url, + decode_responses=True, + socket_connect_timeout=0.5, + socket_timeout=0.5, + retry_on_timeout=False, + retry=Retry(NoBackoff(), 0), + ) + + @staticmethod + def _hash_token(token: str) -> str: + return hashlib.sha256(token.encode("utf-8")).hexdigest() + + def create_session(self, username: str, password: str) -> dict: + user = self.users.get_by_username(username) + + if user is None or not password_hasher.verify( + password, user.password_hash + ): + raise InvalidCredentialsError("Invalid username or password") + + token = secrets.token_urlsafe(32) + token_hash = self._hash_token(token) + + expires_at = datetime.now(timezone.utc) + timedelta( + seconds=settings.session_ttl_seconds + ) + + try: + self.sessions.create( + user_id=user.id, + token_hash=token_hash, + expires_at=expires_at, + ) + self.db.commit() + except Exception: + self.db.rollback() + raise + + self._cache_session(token_hash, user.id, expires_at) + + return { + "token": token, + "token_type": "Bearer", + "expires_at": expires_at, + "user": user, + } + + def _cache_session( + self, + token_hash: str, + user_id, + expires_at, + ) -> None: + if not self._redis_available: + return + + ttl = max( + 1, + int((expires_at - datetime.now(timezone.utc)).total_seconds()), + ) + + try: + self.redis.setex( + f"session:{token_hash}", + ttl, + str(user_id), + ) + except RedisError: + self._redis_available = False + + def authenticate(self, token: str) -> User: + token_hash = self._hash_token(token) + cached_user_id = None + + if self._redis_available: + try: + cached_user_id = self.redis.get( + f"session:{token_hash}" + ) + except RedisError: + self._redis_available = False + + session = self.sessions.get_valid_session(token_hash) + + if session is None: + if self._redis_available: + try: + self.redis.delete(f"session:{token_hash}") + except RedisError: + self._redis_available = False + + raise InvalidSessionError( + "Session is invalid or expired" + ) + + if cached_user_id is None: + self._cache_session( + token_hash, + session.user_id, + session.expires_at, + ) + + return session.user + + def delete_session(self, token: str) -> None: + token_hash = self._hash_token(token) + + try: + deleted = self.sessions.delete_by_token_hash(token_hash) + + if not deleted: + raise InvalidSessionError( + "Session is invalid or expired" + ) + + self.db.commit() + except Exception: + self.db.rollback() + raise + + if self._redis_available: + try: + self.redis.delete(f"session:{token_hash}") + except RedisError: + self._redis_available = False diff --git a/app/services/chat_service.py b/app/services/chat_service.py new file mode 100644 index 00000000..10ff5656 --- /dev/null +++ b/app/services/chat_service.py @@ -0,0 +1,94 @@ +import uuid + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session + +from app.db.models import Chat +from app.repositories.chat_repository import ChatRepository +from app.repositories.user_repository import UserRepository + + +class ChatNotFoundError(Exception): + pass + + +class ChatAccessError(Exception): + pass + + +class InvalidChatDataError(Exception): + pass + + +class ChatService: + def __init__(self, db: Session): + self.db = db + self.chats = ChatRepository(db) + self.users = UserRepository(db) + + def create_chat( + self, + title: str, + member_ids: list[str], + creator_id: uuid.UUID, + ) -> Chat: + title = title.strip() + + if not title or len(title) > 100: + raise InvalidChatDataError("Invalid chat title") + + if not member_ids or len(member_ids) > 100: + raise InvalidChatDataError("Invalid members") + + try: + parsed_ids = [uuid.UUID(str(value)) for value in member_ids] + except (ValueError, TypeError, AttributeError): + raise InvalidChatDataError("Invalid member UUID") from None + + if len(parsed_ids) != len(set(parsed_ids)): + raise InvalidChatDataError("Duplicate members") + + if creator_id not in parsed_ids: + raise InvalidChatDataError("Creator must be a member") + + for user_id in parsed_ids: + if self.users.get_by_id(user_id) is None: + raise InvalidChatDataError("Unknown member") + + try: + chat = self.chats.create( + title=title, + member_ids=parsed_ids, + ) + self.db.commit() + self.db.refresh(chat) + return chat + + except IntegrityError as exc: + self.db.rollback() + raise InvalidChatDataError("Unable to create chat") from exc + + except Exception: + self.db.rollback() + raise + + def get_chat(self, chat_id: uuid.UUID, user_id: uuid.UUID) -> Chat: + chat = self.chats.get_by_id(chat_id) + + if chat is None: + raise ChatNotFoundError() + + if not self.chats.is_member(chat_id, user_id): + raise ChatNotFoundError() + + return chat + + def list_chats(self, user_id: uuid.UUID) -> list[Chat]: + return self.chats.get_by_user(user_id) + + def check_membership( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + ) -> bool: + return self.chats.is_member(chat_id, user_id) diff --git a/app/services/message_service.py b/app/services/message_service.py new file mode 100644 index 00000000..381da242 --- /dev/null +++ b/app/services/message_service.py @@ -0,0 +1,418 @@ +import base64 +import hashlib +import json +import time +import uuid +from datetime import datetime, timedelta, timezone +from types import SimpleNamespace + +from redis import Redis +from redis.backoff import NoBackoff +from redis.exceptions import RedisError +from redis.retry import Retry +from sqlalchemy import delete, select, update +from sqlalchemy.dialects.postgresql import insert as pg_insert +from sqlalchemy.orm import Session + +from app.core.config import settings +from app.db.models import IdempotencyRecord, Message +from app.repositories.chat_repository import ChatRepository +from app.repositories.message_repository import MessageRepository + + +class MessageNotFoundError(Exception): + pass + + +class MessageAccessError(Exception): + pass + + +class InvalidMessageDataError(Exception): + pass + + +class IdempotencyConflictError(Exception): + pass + + +class MessageService: + def __init__(self, db: Session): + self.db = db + self.messages = MessageRepository(db) + self.chats = ChatRepository(db) + + self._redis_available = True + + self.redis = Redis.from_url( + settings.cache_url, + decode_responses=True, + socket_connect_timeout=0.5, + socket_timeout=0.5, + retry_on_timeout=False, + retry=Retry(NoBackoff(), 0), + ) + + @staticmethod + def encode_cursor(message: Message) -> str: + payload = { + "created_at": message.created_at.isoformat(), + "id": str(message.id), + } + + raw = json.dumps(payload).encode("utf-8") + + return ( + base64.urlsafe_b64encode(raw) + .decode("ascii") + .rstrip("=") + ) + + @staticmethod + def decode_cursor(cursor: str | None): + if not cursor: + return None, None + + try: + padded = cursor + "=" * (-len(cursor) % 4) + raw = base64.urlsafe_b64decode(padded.encode("ascii")) + payload = json.loads(raw.decode("utf-8")) + + created_at = datetime.fromisoformat(payload["created_at"]) + message_id = uuid.UUID(payload["id"]) + + if created_at.tzinfo is None: + raise ValueError("Cursor timestamp must include timezone") + + return created_at, message_id + + except (ValueError, TypeError, KeyError, UnicodeError): + raise InvalidMessageDataError("Invalid cursor") from None + + def _version_key(self, chat_id: uuid.UUID) -> str: + return f"messages:version:{chat_id}" + + def _cache_key( + self, + chat_id: uuid.UUID, + version: int, + limit: int, + cursor: str | None, + ) -> str: + cursor_hash = hashlib.sha256( + (cursor or "first-page").encode("utf-8") + ).hexdigest()[:16] + + return ( + f"messages:{chat_id}:v{version}:" + f"limit:{limit}:cursor:{cursor_hash}" + ) + + def _get_version(self, chat_id: uuid.UUID) -> int: + if not self._redis_available: + return 0 + + try: + value = self.redis.get(self._version_key(chat_id)) + return int(value or 0) + except (RedisError, ValueError): + self._redis_available = False + return 0 + + def _bump_version(self, chat_id: uuid.UUID) -> None: + if not self._redis_available: + return + + try: + self.redis.incr(self._version_key(chat_id)) + except RedisError: + self._redis_available = False + + @staticmethod + def _serialize(messages, next_cursor): + return json.dumps({ + "items": [ + { + "id": str(message.id), + "chat_id": str(message.chat_id), + "sender_id": str(message.sender_id), + "text": message.text, + "created_at": message.created_at.isoformat(), + } + for message in messages + ], + "next_cursor": next_cursor, + }) + + @staticmethod + def _deserialize(payload: str): + data = json.loads(payload) + + messages = [ + SimpleNamespace( + id=uuid.UUID(item["id"]), + chat_id=uuid.UUID(item["chat_id"]), + sender_id=uuid.UUID(item["sender_id"]), + text=item["text"], + created_at=datetime.fromisoformat(item["created_at"]), + ) + for item in data["items"] + ] + + return messages, data["next_cursor"] + + def _read_cache(self, key: str): + if not self._redis_available: + return None + + try: + payload = self.redis.get(key) + + if payload is None: + return None + + return self._deserialize(payload) + + except RedisError: + self._redis_available = False + return None + except (ValueError, TypeError, KeyError): + return None + + def _load_from_database( + self, + chat_id, + limit, + cursor_created_at, + cursor_id, + ): + rows = self.messages.get_history( + chat_id=chat_id, + limit=limit + 1, + cursor_created_at=cursor_created_at, + cursor_id=cursor_id, + ) + + has_more = len(rows) > limit + items = rows[:limit] + + next_cursor = ( + self.encode_cursor(items[-1]) + if has_more and items + else None + ) + + return items, next_cursor + + def create_message( + self, + chat_id: uuid.UUID, + sender_id: uuid.UUID, + text: str, + idempotency_key: str, + ) -> Message: + text = text.strip() + if not text or len(text) > 4000: + raise InvalidMessageDataError("Invalid message text") + + # Access is checked for every retry, before consulting the key. + if not self.chats.is_member(chat_id, sender_id): + raise MessageNotFoundError() + + method = "POST" + path = f"/api/v1/chats/{chat_id}/messages" + request_hash = hashlib.sha256( + json.dumps({"text": text}, sort_keys=True, separators=(",", ":")).encode("utf-8") + ).hexdigest() + now = datetime.now(timezone.utc) + expires_at = now + timedelta(seconds=max(settings.idempotency_ttl_seconds, 86400)) + scope = ( + IdempotencyRecord.user_id == sender_id, + IdempotencyRecord.method == method, + IdempotencyRecord.path == path, + IdempotencyRecord.idempotency_key == idempotency_key, + ) + + try: + # An expired key may be reused. Deletion and claim are serialized by + # PostgreSQL's unique constraint for the scope. + self.db.execute( + delete(IdempotencyRecord).where(*scope, IdempotencyRecord.expires_at <= now) + ) + claim = ( + pg_insert(IdempotencyRecord) + .values( + id=uuid.uuid4(), user_id=sender_id, method=method, path=path, + idempotency_key=idempotency_key, request_hash=request_hash, + message_id=None, created_at=now, expires_at=expires_at, + ) + .on_conflict_do_nothing(constraint="uq_idempotency_scope_key") + .returning(IdempotencyRecord.id) + ) + claimed_id = self.db.execute(claim).scalar_one_or_none() + + if claimed_id is None: + existing = self.db.execute( + select(IdempotencyRecord).where(*scope).with_for_update() + ).scalar_one_or_none() + if existing is None: + # Rare expiration race: retry once in a fresh transaction. + self.db.rollback() + return self.create_message(chat_id, sender_id, text, idempotency_key) + if existing.request_hash != request_hash: + self.db.rollback() + raise IdempotencyConflictError() + if existing.message_id is None: + raise RuntimeError("Committed idempotency record has no message") + message = self.db.get(Message, existing.message_id) + if message is None: + raise RuntimeError("Idempotency record references a missing message") + self.db.commit() + return message + + message = self.messages.create( + chat_id=chat_id, sender_id=sender_id, text=text + ) + self.db.execute( + update(IdempotencyRecord) + .where(IdempotencyRecord.id == claimed_id) + .values(message_id=message.id) + ) + self.db.commit() + self.db.refresh(message) + self._bump_version(chat_id) + return message + except IdempotencyConflictError: + raise + except Exception: + self.db.rollback() + raise + + def get_history( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + limit: int = 50, + cursor: str | None = None, + ): + if not 1 <= limit <= 100: + raise InvalidMessageDataError( + "Limit must be between 1 and 100" + ) + + if not self.chats.is_member(chat_id, user_id): + raise MessageNotFoundError() + + cursor_created_at, cursor_id = self.decode_cursor(cursor) + + version = self._get_version(chat_id) + + cache_key = self._cache_key( + chat_id, + version, + limit, + cursor, + ) + + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + lock_key = f"lock:{cache_key}" + lock_token = str(uuid.uuid4()) + + try: + acquired = self.redis.set( + lock_key, + lock_token, + nx=True, + ex=10, + ) + except RedisError: + self._redis_available = False + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + if not acquired: + for _ in range(10): + time.sleep(0.05) + + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + break + + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + try: + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + items, next_cursor = self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + if self._redis_available and self._get_version(chat_id) == version: + try: + self.redis.setex( + cache_key, + settings.message_cache_ttl_seconds, + self._serialize(items, next_cursor), + ) + except RedisError: + self._redis_available = False + + return items, next_cursor + + finally: + if self._redis_available: + try: + self.redis.eval( + """ + if redis.call('get', KEYS[1]) == ARGV[1] + then + return redis.call('del', KEYS[1]) + else + return 0 + end + """, + 1, + lock_key, + lock_token, + ) + except RedisError: + self._redis_available = False diff --git a/app/services/user_service.py b/app/services/user_service.py new file mode 100644 index 00000000..896ee5ec --- /dev/null +++ b/app/services/user_service.py @@ -0,0 +1,81 @@ +import re + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session +from pwdlib import PasswordHash + +from app.db.models import User +from app.repositories.user_repository import UserRepository + + +password_hasher = PasswordHash.recommended() + + +class UsernameAlreadyExistsError(Exception): + pass + + +class UserService: + + def __init__(self, db: Session): + self.db = db + self.users = UserRepository(db) + + def register( + self, + username: str, + display_name: str, + password: str, + ) -> User: + + if not re.fullmatch(r"[a-z0-9_]{3,32}", username): + raise ValueError("Invalid username") + + if not display_name or not display_name.strip(): + raise ValueError("Display name cannot be empty") + + if len(display_name) > 100: + raise ValueError("Display name is too long") + + if not 12 <= len(password) <= 128: + raise ValueError("Invalid password length") + + if self.users.get_by_username(username) is not None: + raise UsernameAlreadyExistsError( + "Username already exists" + ) + + password_hash = password_hasher.hash(password) + + try: + user = self.users.create( + username=username, + display_name=display_name, + password_hash=password_hash, + ) + + self.db.commit() + self.db.refresh(user) + + return user + + except IntegrityError as exc: + self.db.rollback() + + raise UsernameAlreadyExistsError( + "Username already exists" + ) from exc + + except Exception: + self.db.rollback() + raise + + def verify_password( + self, + user: User, + password: str, + ) -> bool: + return password_hasher.verify( + password, + user.password_hash, + ) \ No newline at end of file diff --git a/app/store.py b/app/store.py new file mode 100644 index 00000000..4802821c --- /dev/null +++ b/app/store.py @@ -0,0 +1,193 @@ +import base64 +import bisect +import binascii +import json +import os +import threading +import uuid +from datetime import datetime, timezone +from typing import Dict, List, Optional, Tuple + +from .models import Chat, Message, User + + +Key = Tuple[datetime, str] + + +def _encode_cursor(kind: str, scope: str, item) -> str: + payload = { + "v": 1, + "kind": kind, + "scope": scope, + "created_at": item.created_at.isoformat(), + "id": item.id, + } + raw = json.dumps(payload, separators=(",", ":")).encode("utf-8") + return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=") + + +def _decode_cursor(cursor: str, kind: str, scope: str) -> Key: + try: + if not cursor or len(cursor) > 4096: + raise ValueError + padded = cursor + "=" * (-len(cursor) % 4) + raw = base64.b64decode(padded, altchars=b"-_", validate=True) + payload = json.loads(raw.decode("utf-8")) + if not isinstance(payload, dict) or payload.get("v") != 1: + raise ValueError + if payload.get("kind") != kind or payload.get("scope") != scope: + raise ValueError + item_id = payload.get("id") + timestamp = payload.get("created_at") + if not isinstance(item_id, str) or not isinstance(timestamp, str): + raise ValueError + created_at = datetime.fromisoformat(timestamp) + if created_at.tzinfo is None: + raise ValueError + created_at = created_at.astimezone(timezone.utc) + return created_at, item_id + except ( + ValueError, + TypeError, + KeyError, + UnicodeDecodeError, + json.JSONDecodeError, + binascii.Error, + ): + raise ValueError("invalid_request") from None + + +class InMemoryStore: + def __init__(self): + self._lock = threading.RLock() + self.users: Dict[str, User] = {} + self.chats: Dict[str, Chat] = {} + self.messages: Dict[str, List[Message]] = {} + self.usernames: Dict[str, str] = {} + self.message_keys: Dict[str, List[Key]] = {} + self.message_index_enabled = os.getenv("MESSAGE_INDEX_ENABLED", "1") != "0" + + def create_user(self, username: str, display_name: str) -> User: + with self._lock: + if username in self.usernames: + raise ValueError("conflict") + user_id = str(uuid.uuid4()) + user = User( + id=user_id, + username=username, + display_name=display_name, + created_at=datetime.now(timezone.utc), + ) + self.users[user_id] = user + self.usernames[username] = user_id + return user + + def get_user(self, user_id: str) -> Optional[User]: + with self._lock: + return self.users.get(user_id) + + def create_chat(self, title: str, member_ids: List[str], creator_id: str) -> Chat: + with self._lock: + if creator_id not in member_ids or len(member_ids) != len(set(member_ids)): + raise ValueError("invalid_request") + if any(member_id not in self.users for member_id in member_ids): + raise ValueError("invalid_request") + + chat_id = str(uuid.uuid4()) + chat = Chat( + id=chat_id, + title=title, + member_ids=list(member_ids), + created_at=datetime.now(timezone.utc), + ) + self.chats[chat_id] = chat + self.messages[chat_id] = [] + self.message_keys[chat_id] = [] + return chat + + def get_chat(self, chat_id: str, user_id: str) -> Optional[Chat]: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or user_id not in chat.member_ids: + return None + return chat + + def list_chats( + self, user_id: str, limit: int, cursor: Optional[str] + ) -> Tuple[List[Chat], Optional[str]]: + with self._lock: + user_chats = [chat for chat in self.chats.values() if user_id in chat.member_ids] + user_chats.sort(key=lambda chat: (chat.created_at, chat.id)) + keys = [(chat.created_at, chat.id) for chat in user_chats] + + start_index = 0 + if cursor: + target_key = _decode_cursor(cursor, kind="chats", scope=user_id) + start_index = bisect.bisect_right(keys, target_key) + + page_with_extra = user_chats[start_index : start_index + limit + 1] + has_next = len(page_with_extra) > limit + page = page_with_extra[:limit] + next_cursor = None + if has_next and page: + next_cursor = _encode_cursor("chats", user_id, page[-1]) + return page, next_cursor + + def create_message(self, chat_id: str, sender_id: str, text: str) -> Message: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or sender_id not in chat.member_ids: + raise ValueError("not_found") + + message = Message( + id=str(uuid.uuid4()), + chat_id=chat_id, + sender_id=sender_id, + text=text, + created_at=datetime.now(timezone.utc), + ) + messages = self.messages[chat_id] + keys = self.message_keys[chat_id] + key = (message.created_at, message.id) + + if self.message_index_enabled: + index = bisect.bisect_right(keys, key) + keys.insert(index, key) + messages.insert(index, message) + else: + current_keys = [(item.created_at, item.id) for item in messages] + index = bisect.bisect_right(current_keys, key) + messages.insert(index, message) + keys.insert(index, key) + return message + + def list_messages( + self, + chat_id: str, + user_id: str, + limit: int, + cursor: Optional[str], + ) -> Tuple[List[Message], Optional[str]]: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or user_id not in chat.member_ids: + raise ValueError("not_found") + + messages = self.messages[chat_id] + if self.message_index_enabled: + keys = self.message_keys[chat_id] + else: + keys = [(item.created_at, item.id) for item in messages] + + start_index = 0 + if cursor: + target_key = _decode_cursor(cursor, kind="messages", scope=chat_id) + start_index = bisect.bisect_right(keys, target_key) + + page_with_extra = messages[start_index : start_index + limit + 1] + has_next = len(page_with_extra) > limit + page = page_with_extra[:limit] + next_cursor = None + if has_next and page: + next_cursor = _encode_cursor("messages", chat_id, page[-1]) + return page, next_cursor diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..44f48cf9 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,143 @@ +name: mtusi-lab3 +services: + postgres: + image: postgres:16.6-bookworm + restart: unless-stopped + environment: + POSTGRES_DB: ${POSTGRES_DB:-messenger} + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""] + interval: 5s + timeout: 3s + retries: 15 + start_period: 10s + deploy: + resources: + limits: + cpus: "${POSTGRES_CPU_LIMIT:-1.0}" + memory: ${POSTGRES_MEMORY_LIMIT:-768M} + networks: [backend] + + redis: + image: redis:7.4.2-bookworm + restart: unless-stopped + command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"] + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + cpus: "${REDIS_CPU_LIMIT:-0.5}" + memory: ${REDIS_MEMORY_LIMIT:-256M} + networks: [backend] + + migrate: + build: + context: . + dockerfile: Dockerfile + command: ["alembic", "upgrade", "head"] + environment: + DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-messenger} + depends_on: + postgres: + condition: service_healthy + restart: "no" + networks: [backend] + + api: + build: + context: . + dockerfile: Dockerfile + restart: unless-stopped + environment: + APP_ENV: ${APP_ENV:-lab} + DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-messenger} + CACHE_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + MESSAGE_CACHE_TTL_SECONDS: ${MESSAGE_CACHE_TTL_SECONDS:-60} + IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} + DB_POOL_SIZE: ${DB_POOL_SIZE:-3} + DB_MAX_OVERFLOW: ${DB_MAX_OVERFLOW:-2} + DB_POOL_TIMEOUT: ${DB_POOL_TIMEOUT:-10} + SHUTDOWN_GRACE_SECONDS: ${SHUTDOWN_GRACE_SECONDS:-20} + depends_on: + migrate: + condition: service_completed_successfully + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"] + interval: 5s + timeout: 3s + retries: 3 + start_period: 20s + deploy: + resources: + limits: + cpus: "${API_CPU_LIMIT:-1.0}" + memory: ${API_MEMORY_LIMIT:-512M} + labels: + - traefik.enable=true + - traefik.docker.network=mtusi-lab3-edge + - traefik.http.routers.messenger.rule=PathPrefix(`/`) + - traefik.http.routers.messenger.entrypoints=web + - traefik.http.services.messenger.loadbalancer.server.port=8080 + - traefik.http.services.messenger.loadbalancer.healthcheck.path=/health/ready + - traefik.http.services.messenger.loadbalancer.healthcheck.interval=3s + - traefik.http.services.messenger.loadbalancer.healthcheck.timeout=2s + - traefik.http.services.messenger.loadbalancer.passhostheader=true + - traefik.http.services.messenger.loadbalancer.responseforwarding.flushinterval=100ms + networks: [backend, edge] + + traefik: + image: traefik:v3.6 + restart: unless-stopped + environment: + DOCKER_API_VERSION: "1.44" + command: + - --providers.docker=true + - --providers.docker.exposedbydefault=false + - --providers.docker.network=mtusi-lab3-edge + - --entrypoints.web.address=:8080 + - --ping=true + - --ping.entrypoint=ping + - --entrypoints.ping.address=:8082 + - --accesslog=true + - --log.level=DEBUG + ports: + - "8080:8080" + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + healthcheck: + test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:8082/ping"] + interval: 10s + timeout: 3s + retries: 5 + deploy: + resources: + limits: + cpus: "${LB_CPU_LIMIT:-0.5}" + memory: ${LB_MEMORY_LIMIT:-256M} + networks: [edge] + +networks: + backend: + driver: bridge + edge: + name: mtusi-lab3-edge + driver: bridge + +volumes: + postgres_data: + redis_data: diff --git a/evidence/concurrent-1.txt b/evidence/concurrent-1.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-1.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-10.txt b/evidence/concurrent-10.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-10.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-2.txt b/evidence/concurrent-2.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-2.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-3.txt b/evidence/concurrent-3.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-3.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-4.txt b/evidence/concurrent-4.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-4.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-5.txt b/evidence/concurrent-5.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-5.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-6.txt b/evidence/concurrent-6.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-6.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-7.txt b/evidence/concurrent-7.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-7.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-8.txt b/evidence/concurrent-8.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-8.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-9.txt b/evidence/concurrent-9.txt new file mode 100644 index 00000000..cd69ab4e --- /dev/null +++ b/evidence/concurrent-9.txt @@ -0,0 +1,2 @@ +{"id":"ba491f27-f3d7-47ab-abb0-547768c2c308","chat_id":"822c8e51-885a-43c0-9419-754081666a37","sender_id":"6234d398-4141-4830-9e3f-47b2df8a5642","text":"Concurrent idempotency test","created_at":"2026-11-05T13:39:39.066506Z"} +HTTP_STATUS:201 diff --git a/evidence/concurrent-summary.txt b/evidence/concurrent-summary.txt new file mode 100644 index 00000000..5744702b --- /dev/null +++ b/evidence/concurrent-summary.txt @@ -0,0 +1,5 @@ +]633;E;{ echo "Concurrent idempotency test"\x3b echo "Requests: 10"\x3b echo "Expected HTTP status: 201"\x3b echo "Expected unique message ID: ba491f27-f3d7-47ab-abb0-547768c2c308"\x3b echo "Result: PASS"\x3b } > evidence/concurrent-summary.txt;4eb7bf5d-580a-40d0-b860-fce765fce5ff]633;CConcurrent idempotency test +Requests: 10 +Expected HTTP status: 201 +Expected unique message ID: ba491f27-f3d7-47ab-abb0-547768c2c308 +Result: PASS diff --git a/evidence/failover/replica-1.txt b/evidence/failover/replica-1.txt new file mode 100644 index 00000000..fce488af --- /dev/null +++ b/evidence/failover/replica-1.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 83, "failed_attempts": 3, "first_recovery_seconds": 4.195} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/failover/replica-2.txt b/evidence/failover/replica-2.txt new file mode 100644 index 00000000..1187be3b --- /dev/null +++ b/evidence/failover/replica-2.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 87, "failed_attempts": 3, "first_recovery_seconds": 4.051} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/rolling-update-diagnostic.json b/evidence/rolling-update-diagnostic.json new file mode 100644 index 00000000..1e0ae3c3 --- /dev/null +++ b/evidence/rolling-update-diagnostic.json @@ -0,0 +1,42 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "duration_seconds": 91.64, + "concurrency": 8, + "successful_requests": 5694, + "failed_requests": 22, + "error_ratio": 0.003849, + "errors_by_type": { + "http_502": 14, + "connection_TimeoutError": 8 + }, + "successful_rps": 62.135, + "successful_latency_ms": { + "p50": 111.668, + "p95": 267.403, + "p99": 393.579 + }, + "error_examples": [ + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: expected [201], received 502", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)", + "POST /api/v1/chats/6875e847-fc8c-476e-966f-25eabb4f209e/messages: connection/TLS failure (TimeoutError)" + ], + "note": "Latency percentiles cover successful requests only." +} diff --git a/evidence/rolling-update-final.json b/evidence/rolling-update-final.json new file mode 100644 index 00000000..4fbb7612 --- /dev/null +++ b/evidence/rolling-update-final.json @@ -0,0 +1,41 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "duration_seconds": 91.245, + "concurrency": 8, + "successful_requests": 6050, + "failed_requests": 20, + "error_ratio": 0.003295, + "errors_by_type": { + "http_502": 20 + }, + "successful_rps": 66.305, + "successful_latency_ms": { + "p50": 87.748, + "p95": 280.709, + "p99": 427.002 + }, + "error_examples": [ + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502", + "POST /api/v1/chats/63292225-703b-4944-b89b-3c916e925743/messages: expected [201], received 502" + ], + "note": "Latency percentiles cover successful requests only." +} diff --git a/evidence/rolling-update-load.json b/evidence/rolling-update-load.json new file mode 100644 index 00000000..f902bd44 --- /dev/null +++ b/evidence/rolling-update-load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 90.0, + "elapsed_seconds": 91.541, + "concurrency": 8, + "successful_requests": 5205, + "failed_requests": 51, + "error_ratio": 0.009703, + "successful_rps": 56.86, + "successful_latency_ms": { + "p50": 110.445, + "p95": 271.578, + "p99": 413.327 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scaling/errors.png b/evidence/scaling/errors.png new file mode 100644 index 00000000..f96137a2 Binary files /dev/null and b/evidence/scaling/errors.png differ diff --git a/evidence/scaling/expanded_4replicas_2cpu_2048mb.json b/evidence/scaling/expanded_4replicas_2cpu_2048mb.json new file mode 100644 index 00000000..e262efb3 --- /dev/null +++ b/evidence/scaling/expanded_4replicas_2cpu_2048mb.json @@ -0,0 +1,194 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.686, + "concurrency": 8, + "successful_requests": 2101, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 68.467, + "successful_latency_ms": { + "p50": 50.668, + "p95": 374.351, + "p99": 489.573 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "expanded_4replicas_2cpu_2048mb", + "api_replicas": 4, + "total_api_cpu_budget": 2.0, + "total_api_memory_budget_mb": 2048, + "per_replica_cpu_limit": 0.5, + "per_replica_memory_limit_mb": 512, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886504.0991633, + "containers": [ + "mtusi-lab3-api-4;15.64%;68.37MiB / 512MiB", + "mtusi-lab3-api-3;18.83%;68.32MiB / 512MiB", + "mtusi-lab3-api-1;19.08%;68.34MiB / 512MiB", + "mtusi-lab3-api-2;0.71%;69.18MiB / 512MiB", + "mtusi-lab3-postgres-1;0.55%;26.86MiB / 768MiB", + "mtusi-lab3-redis-1;0.30%;3.531MiB / 256MiB", + "mtusi-lab3-traefik-1;0.55%;17.91MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.24%;3.621MiB / 7.622GiB", + "artship-frontend;12.05%;90.32MiB / 7.622GiB", + "artship-backend;1.38%;198.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,10,100" + }, + { + "timestamp_epoch": 1793886507.5500882, + "containers": [ + "mtusi-lab3-api-4;30.50%;70.11MiB / 512MiB", + "mtusi-lab3-api-3;28.82%;68.95MiB / 512MiB", + "mtusi-lab3-api-1;48.19%;69.97MiB / 512MiB", + "mtusi-lab3-api-2;40.97%;70.31MiB / 512MiB", + "mtusi-lab3-postgres-1;24.12%;46.57MiB / 768MiB", + "mtusi-lab3-redis-1;7.09%;3.648MiB / 256MiB", + "mtusi-lab3-traefik-1;8.94%;18.37MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.20%;3.621MiB / 7.622GiB", + "artship-frontend;9.85%;90.21MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886510.967673, + "containers": [ + "mtusi-lab3-api-4;34.44%;70.19MiB / 512MiB", + "mtusi-lab3-api-3;33.16%;69.07MiB / 512MiB", + "mtusi-lab3-api-1;49.41%;70.35MiB / 512MiB", + "mtusi-lab3-api-2;37.62%;70.42MiB / 512MiB", + "mtusi-lab3-postgres-1;27.40%;49.12MiB / 768MiB", + "mtusi-lab3-redis-1;7.85%;3.645MiB / 256MiB", + "mtusi-lab3-traefik-1;12.22%;23.12MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.34%;3.621MiB / 7.622GiB", + "artship-frontend;14.85%;90.36MiB / 7.622GiB", + "artship-backend;282.30%;153.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886514.6916337, + "containers": [ + "mtusi-lab3-api-4;33.50%;70.3MiB / 512MiB", + "mtusi-lab3-api-3;32.53%;69.73MiB / 512MiB", + "mtusi-lab3-api-1;32.08%;70.41MiB / 512MiB", + "mtusi-lab3-api-2;49.22%;71.13MiB / 512MiB", + "mtusi-lab3-postgres-1;27.65%;51.22MiB / 768MiB", + "mtusi-lab3-redis-1;6.18%;3.656MiB / 256MiB", + "mtusi-lab3-traefik-1;13.40%;24.93MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.28%;3.621MiB / 7.622GiB", + "artship-frontend;9.34%;90.95MiB / 7.622GiB", + "artship-backend;6.52%;198.7MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,20,100" + }, + { + "timestamp_epoch": 1793886518.2727063, + "containers": [ + "mtusi-lab3-api-4;43.58%;79.84MiB / 512MiB", + "mtusi-lab3-api-3;29.50%;70.04MiB / 512MiB", + "mtusi-lab3-api-1;32.03%;70.45MiB / 512MiB", + "mtusi-lab3-api-2;48.82%;71.34MiB / 512MiB", + "mtusi-lab3-postgres-1;25.93%;49.01MiB / 768MiB", + "mtusi-lab3-redis-1;6.90%;3.645MiB / 256MiB", + "mtusi-lab3-traefik-1;13.53%;25.2MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.25%;3.621MiB / 7.622GiB", + "artship-frontend;8.48%;90.53MiB / 7.622GiB", + "artship-backend;0.19%;191MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886521.316862, + "containers": [ + "mtusi-lab3-api-4;28.95%;70.8MiB / 512MiB", + "mtusi-lab3-api-3;27.79%;69.83MiB / 512MiB", + "mtusi-lab3-api-1;51.05%;70.98MiB / 512MiB", + "mtusi-lab3-api-2;28.37%;71.39MiB / 512MiB", + "mtusi-lab3-postgres-1;29.36%;52.06MiB / 768MiB", + "mtusi-lab3-redis-1;6.88%;3.66MiB / 256MiB", + "mtusi-lab3-traefik-1;13.06%;25.26MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;13.07%;91.4MiB / 7.622GiB", + "artship-backend;191.35%;218.3MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886524.8557577, + "containers": [ + "mtusi-lab3-api-4;48.19%;70.66MiB / 512MiB", + "mtusi-lab3-api-3;50.30%;69.81MiB / 512MiB", + "mtusi-lab3-api-1;53.67%;83.95MiB / 512MiB", + "mtusi-lab3-api-2;48.05%;83.39MiB / 512MiB", + "mtusi-lab3-postgres-1;20.57%;47.53MiB / 768MiB", + "mtusi-lab3-redis-1;6.29%;3.684MiB / 256MiB", + "mtusi-lab3-traefik-1;7.31%;24.3MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.34%;3.621MiB / 7.622GiB", + "artship-frontend;13.96%;91MiB / 7.622GiB", + "artship-backend;252.40%;111.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,20,100" + }, + { + "timestamp_epoch": 1793886528.5855644, + "containers": [ + "mtusi-lab3-api-4;25.55%;70.67MiB / 512MiB", + "mtusi-lab3-api-3;27.21%;69.84MiB / 512MiB", + "mtusi-lab3-api-1;25.77%;70.93MiB / 512MiB", + "mtusi-lab3-api-2;49.37%;71.66MiB / 512MiB", + "mtusi-lab3-postgres-1;22.51%;52.78MiB / 768MiB", + "mtusi-lab3-redis-1;6.31%;3.645MiB / 256MiB", + "mtusi-lab3-traefik-1;11.01%;24.43MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.32%;3.621MiB / 7.622GiB", + "artship-frontend;16.19%;90.03MiB / 7.622GiB", + "artship-backend;223.12%;163.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886531.6453142, + "containers": [ + "mtusi-lab3-api-4;21.42%;70.65MiB / 512MiB", + "mtusi-lab3-api-3;24.16%;70.22MiB / 512MiB", + "mtusi-lab3-api-1;27.55%;71.43MiB / 512MiB", + "mtusi-lab3-api-2;50.69%;71.99MiB / 512MiB", + "mtusi-lab3-postgres-1;21.00%;54.07MiB / 768MiB", + "mtusi-lab3-redis-1;6.68%;3.648MiB / 256MiB", + "mtusi-lab3-traefik-1;8.88%;24.92MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;9.89%;90.12MiB / 7.622GiB", + "artship-backend;0.10%;192.6MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886535.1705682, + "containers": [ + "mtusi-lab3-api-4;22.13%;70.6MiB / 512MiB", + "mtusi-lab3-api-3;24.41%;70.28MiB / 512MiB", + "mtusi-lab3-api-1;26.79%;70.86MiB / 512MiB", + "mtusi-lab3-api-2;48.76%;72.09MiB / 512MiB", + "mtusi-lab3-postgres-1;21.86%;48.79MiB / 768MiB", + "mtusi-lab3-redis-1;6.01%;3.637MiB / 256MiB", + "mtusi-lab3-traefik-1;10.19%;26.42MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.26%;3.621MiB / 7.622GiB", + "artship-frontend;12.18%;90.27MiB / 7.622GiB", + "artship-backend;0.14%;186MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + } + ] +} diff --git a/evidence/scaling/expanded_4replicas_3cpu_3072mb.json b/evidence/scaling/expanded_4replicas_3cpu_3072mb.json new file mode 100644 index 00000000..cf1a7ec8 --- /dev/null +++ b/evidence/scaling/expanded_4replicas_3cpu_3072mb.json @@ -0,0 +1,177 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.836, + "concurrency": 8, + "successful_requests": 2936, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 95.214, + "successful_latency_ms": { + "p50": 54.992, + "p95": 238.599, + "p99": 440.463 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "expanded_4replicas_3cpu_3072mb", + "api_replicas": 4, + "total_api_cpu_budget": 3.0, + "total_api_memory_budget_mb": 3072, + "per_replica_cpu_limit": 0.75, + "per_replica_memory_limit_mb": 768, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886566.411055, + "containers": [ + "mtusi-lab3-api-2;28.25%;68.31MiB / 768MiB", + "mtusi-lab3-api-3;21.60%;68.37MiB / 768MiB", + "mtusi-lab3-api-1;26.44%;68.33MiB / 768MiB", + "mtusi-lab3-api-4;27.69%;68.39MiB / 768MiB", + "mtusi-lab3-postgres-1;0.86%;26.92MiB / 768MiB", + "mtusi-lab3-redis-1;0.40%;3.516MiB / 256MiB", + "mtusi-lab3-traefik-1;1.36%;17.79MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.35%;3.625MiB / 7.622GiB", + "artship-frontend;14.11%;90.33MiB / 7.622GiB", + "artship-backend;302.03%;216.5MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,10,100" + }, + { + "timestamp_epoch": 1793886569.944521, + "containers": [ + "mtusi-lab3-api-2;33.93%;70.22MiB / 768MiB", + "mtusi-lab3-api-3;37.60%;70.11MiB / 768MiB", + "mtusi-lab3-api-1;44.63%;70.03MiB / 768MiB", + "mtusi-lab3-api-4;76.20%;70.1MiB / 768MiB", + "mtusi-lab3-postgres-1;33.73%;49.22MiB / 768MiB", + "mtusi-lab3-redis-1;8.10%;3.617MiB / 256MiB", + "mtusi-lab3-traefik-1;14.25%;20.45MiB / 256MiB", + "mtusi-lab2-postgres;6.72%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;2.97%;3.621MiB / 7.622GiB", + "artship-frontend;12.66%;90.44MiB / 7.622GiB", + "artship-backend;0.15%;208.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,19,100" + }, + { + "timestamp_epoch": 1793886573.5686126, + "containers": [ + "mtusi-lab3-api-2;57.73%;70.11MiB / 768MiB", + "mtusi-lab3-api-3;35.04%;70.22MiB / 768MiB", + "mtusi-lab3-api-1;34.08%;70.16MiB / 768MiB", + "mtusi-lab3-api-4;47.86%;70.45MiB / 768MiB", + "mtusi-lab3-postgres-1;31.37%;51.63MiB / 768MiB", + "mtusi-lab3-redis-1;7.78%;3.648MiB / 256MiB", + "mtusi-lab3-traefik-1;12.51%;24.48MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;13.68%;90.56MiB / 7.622GiB", + "artship-backend;213.76%;212.7MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,18,100" + }, + { + "timestamp_epoch": 1793886577.2284725, + "containers": [ + "mtusi-lab3-api-2;66.81%;70.45MiB / 768MiB", + "mtusi-lab3-api-3;65.49%;70.43MiB / 768MiB", + "mtusi-lab3-api-1;64.72%;70.48MiB / 768MiB", + "mtusi-lab3-api-4;76.16%;70.75MiB / 768MiB", + "mtusi-lab3-postgres-1;51.32%;49.59MiB / 768MiB", + "mtusi-lab3-redis-1;12.75%;3.633MiB / 256MiB", + "mtusi-lab3-traefik-1;26.27%;26.46MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;15.56%;90.68MiB / 7.622GiB", + "artship-backend;216.60%;104.2MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + }, + { + "timestamp_epoch": 1793886581.7327821, + "containers": [ + "mtusi-lab3-api-2;31.71%;70.63MiB / 768MiB", + "mtusi-lab3-api-3;29.52%;70.56MiB / 768MiB", + "mtusi-lab3-api-1;32.12%;70.05MiB / 768MiB", + "mtusi-lab3-api-4;73.07%;71.19MiB / 768MiB", + "mtusi-lab3-postgres-1;27.04%;50MiB / 768MiB", + "mtusi-lab3-redis-1;7.04%;4.133MiB / 256MiB", + "mtusi-lab3-traefik-1;11.52%;26.41MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.27%;3.621MiB / 7.622GiB", + "artship-frontend;13.21%;89.98MiB / 7.622GiB", + "artship-backend;226.27%;173.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,19,100" + }, + { + "timestamp_epoch": 1793886585.4382155, + "containers": [ + "mtusi-lab3-api-2;73.60%;82.87MiB / 768MiB", + "mtusi-lab3-api-3;39.58%;75.51MiB / 768MiB", + "mtusi-lab3-api-1;41.07%;79.02MiB / 768MiB", + "mtusi-lab3-api-4;56.17%;71.06MiB / 768MiB", + "mtusi-lab3-postgres-1;32.44%;51.64MiB / 768MiB", + "mtusi-lab3-redis-1;11.38%;3.645MiB / 256MiB", + "mtusi-lab3-traefik-1;13.86%;27.03MiB / 256MiB", + "mtusi-lab2-postgres;6.78%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.32%;3.621MiB / 7.622GiB", + "artship-frontend;12.57%;90.62MiB / 7.622GiB", + "artship-backend;0.17%;203.7MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + }, + { + "timestamp_epoch": 1793886589.0120466, + "containers": [ + "mtusi-lab3-api-2;54.66%;71.08MiB / 768MiB", + "mtusi-lab3-api-3;55.03%;70.72MiB / 768MiB", + "mtusi-lab3-api-1;53.43%;70.59MiB / 768MiB", + "mtusi-lab3-api-4;74.85%;71.19MiB / 768MiB", + "mtusi-lab3-postgres-1;50.59%;50.43MiB / 768MiB", + "mtusi-lab3-redis-1;11.39%;3.898MiB / 256MiB", + "mtusi-lab3-traefik-1;20.27%;27.12MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.32%;3.621MiB / 7.622GiB", + "artship-frontend;13.55%;90.19MiB / 7.622GiB", + "artship-backend;6.43%;183.7MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886592.0979013, + "containers": [ + "mtusi-lab3-api-2;58.72%;70.89MiB / 768MiB", + "mtusi-lab3-api-3;76.35%;70.82MiB / 768MiB", + "mtusi-lab3-api-1;51.59%;70.42MiB / 768MiB", + "mtusi-lab3-api-4;36.25%;71.21MiB / 768MiB", + "mtusi-lab3-postgres-1;31.39%;51.21MiB / 768MiB", + "mtusi-lab3-redis-1;7.97%;3.656MiB / 256MiB", + "mtusi-lab3-traefik-1;14.96%;27.13MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;14.45%;91.56MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,20,100" + }, + { + "timestamp_epoch": 1793886596.1855414, + "containers": [ + "mtusi-lab3-api-2;72.24%;70.9MiB / 768MiB", + "mtusi-lab3-api-3;65.71%;70.86MiB / 768MiB", + "mtusi-lab3-api-1;73.48%;70.5MiB / 768MiB", + "mtusi-lab3-api-4;75.97%;71.31MiB / 768MiB", + "mtusi-lab3-postgres-1;52.91%;49.4MiB / 768MiB", + "mtusi-lab3-redis-1;13.06%;3.656MiB / 256MiB", + "mtusi-lab3-traefik-1;21.82%;27.3MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.36%;3.621MiB / 7.622GiB", + "artship-frontend;13.87%;90.72MiB / 7.622GiB", + "artship-backend;278.60%;103.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,18,100" + } + ] +} diff --git a/evidence/scaling/expanded_4replicas_4cpu_4096mb.json b/evidence/scaling/expanded_4replicas_4cpu_4096mb.json new file mode 100644 index 00000000..fb8bd145 --- /dev/null +++ b/evidence/scaling/expanded_4replicas_4cpu_4096mb.json @@ -0,0 +1,195 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.613, + "concurrency": 8, + "successful_requests": 3661, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 119.589, + "successful_latency_ms": { + "p50": 55.339, + "p95": 137.459, + "p99": 309.06 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "expanded_4replicas_4cpu_4096mb", + "api_replicas": 4, + "total_api_cpu_budget": 4.0, + "total_api_memory_budget_mb": 4096, + "per_replica_cpu_limit": 1.0, + "per_replica_memory_limit_mb": 1024, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886627.59777, + "containers": [ + "mtusi-lab3-api-2;42.86%;68.36MiB / 1GiB", + "mtusi-lab3-api-1;40.90%;68.38MiB / 1GiB", + "mtusi-lab3-api-3;24.31%;69.14MiB / 1GiB", + "mtusi-lab3-api-4;14.35%;132.9MiB / 1GiB", + "mtusi-lab3-postgres-1;1.25%;26.7MiB / 768MiB", + "mtusi-lab3-redis-1;0.52%;3.531MiB / 256MiB", + "mtusi-lab3-traefik-1;17.74%;17.44MiB / 256MiB", + "mtusi-lab2-postgres;6.52%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.27%;3.621MiB / 7.622GiB", + "artship-frontend;10.52%;90.51MiB / 7.622GiB", + "artship-backend;0.11%;193.2MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,10,100" + }, + { + "timestamp_epoch": 1793886630.3140259, + "containers": [ + "mtusi-lab3-api-2;45.04%;69.82MiB / 1GiB", + "mtusi-lab3-api-1;44.53%;69.8MiB / 1GiB", + "mtusi-lab3-api-3;85.35%;70.96MiB / 1GiB", + "mtusi-lab3-api-4;40.15%;69.05MiB / 1GiB", + "mtusi-lab3-postgres-1;40.65%;48.76MiB / 768MiB", + "mtusi-lab3-redis-1;9.20%;3.637MiB / 256MiB", + "mtusi-lab3-traefik-1;18.53%;20.64MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;3.45%;3.625MiB / 7.622GiB", + "artship-frontend;13.12%;90.59MiB / 7.622GiB", + "artship-backend;157.86%;203.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886634.005423, + "containers": [ + "mtusi-lab3-api-2;84.35%;78.8MiB / 1GiB", + "mtusi-lab3-api-1;71.75%;70MiB / 1GiB", + "mtusi-lab3-api-3;70.38%;71.14MiB / 1GiB", + "mtusi-lab3-api-4;71.34%;70.17MiB / 1GiB", + "mtusi-lab3-postgres-1;51.92%;49.55MiB / 768MiB", + "mtusi-lab3-redis-1;17.60%;3.656MiB / 256MiB", + "mtusi-lab3-traefik-1;21.28%;25.91MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.30%;3.621MiB / 7.622GiB", + "artship-frontend;15.39%;90.41MiB / 7.622GiB", + "artship-backend;220.44%;86.46MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886637.8268003, + "containers": [ + "mtusi-lab3-api-2;68.37%;70.13MiB / 1GiB", + "mtusi-lab3-api-1;68.11%;70.14MiB / 1GiB", + "mtusi-lab3-api-3;67.25%;71.14MiB / 1GiB", + "mtusi-lab3-api-4;85.21%;70.36MiB / 1GiB", + "mtusi-lab3-postgres-1;52.19%;48.4MiB / 768MiB", + "mtusi-lab3-redis-1;13.83%;3.668MiB / 256MiB", + "mtusi-lab3-traefik-1;20.81%;25.94MiB / 256MiB", + "mtusi-lab2-postgres;6.68%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.32%;3.621MiB / 7.622GiB", + "artship-frontend;13.78%;90.77MiB / 7.622GiB", + "artship-backend;228.22%;152MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,18,100" + }, + { + "timestamp_epoch": 1793886640.9071434, + "containers": [ + "mtusi-lab3-api-2;87.22%;70.41MiB / 1GiB", + "mtusi-lab3-api-1;84.75%;70.23MiB / 1GiB", + "mtusi-lab3-api-3;59.53%;80.24MiB / 1GiB", + "mtusi-lab3-api-4;49.78%;70.66MiB / 1GiB", + "mtusi-lab3-postgres-1;40.80%;48.6MiB / 768MiB", + "mtusi-lab3-redis-1;10.81%;3.68MiB / 256MiB", + "mtusi-lab3-traefik-1;18.04%;26.62MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;3.08%;3.621MiB / 7.622GiB", + "artship-frontend;14.52%;90.88MiB / 7.622GiB", + "artship-backend;218.33%;195.6MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886644.6151478, + "containers": [ + "mtusi-lab3-api-2;79.01%;70.54MiB / 1GiB", + "mtusi-lab3-api-1;80.70%;70.46MiB / 1GiB", + "mtusi-lab3-api-3;72.50%;71.63MiB / 1GiB", + "mtusi-lab3-api-4;81.20%;70.66MiB / 1GiB", + "mtusi-lab3-postgres-1;64.88%;49.27MiB / 768MiB", + "mtusi-lab3-redis-1;16.34%;3.715MiB / 256MiB", + "mtusi-lab3-traefik-1;24.77%;26.93MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.34%;3.621MiB / 7.622GiB", + "artship-frontend;14.22%;90.52MiB / 7.622GiB", + "artship-backend;0.17%;188.9MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,18,100" + }, + { + "timestamp_epoch": 1793886648.4234097, + "containers": [ + "mtusi-lab3-api-2;50.34%;70.6MiB / 1GiB", + "mtusi-lab3-api-1;53.63%;70.6MiB / 1GiB", + "mtusi-lab3-api-3;95.66%;71.98MiB / 1GiB", + "mtusi-lab3-api-4;83.48%;70.97MiB / 1GiB", + "mtusi-lab3-postgres-1;43.67%;49.3MiB / 768MiB", + "mtusi-lab3-redis-1;10.60%;3.684MiB / 256MiB", + "mtusi-lab3-traefik-1;18.44%;26.95MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.31%;3.621MiB / 7.622GiB", + "artship-frontend;15.17%;90.36MiB / 7.622GiB", + "artship-backend;202.55%;213MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + }, + { + "timestamp_epoch": 1793886651.5363371, + "containers": [ + "mtusi-lab3-api-2;90.84%;82.66MiB / 1GiB", + "mtusi-lab3-api-1;88.08%;82.64MiB / 1GiB", + "mtusi-lab3-api-3;57.02%;71.79MiB / 1GiB", + "mtusi-lab3-api-4;78.99%;70.83MiB / 1GiB", + "mtusi-lab3-postgres-1;44.01%;54.28MiB / 768MiB", + "mtusi-lab3-redis-1;11.09%;3.652MiB / 256MiB", + "mtusi-lab3-traefik-1;20.83%;26.93MiB / 256MiB", + "mtusi-lab2-postgres;8.55%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;3.32%;3.621MiB / 7.622GiB", + "artship-frontend;16.95%;90.47MiB / 7.622GiB", + "artship-backend;197.13%;62.77MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,18,100" + }, + { + "timestamp_epoch": 1793886655.3452408, + "containers": [ + "mtusi-lab3-api-2;79.65%;70.86MiB / 1GiB", + "mtusi-lab3-api-1;76.53%;70.77MiB / 1GiB", + "mtusi-lab3-api-3;77.07%;71.89MiB / 1GiB", + "mtusi-lab3-api-4;75.40%;70.84MiB / 1GiB", + "mtusi-lab3-postgres-1;65.15%;50.94MiB / 768MiB", + "mtusi-lab3-redis-1;14.22%;3.68MiB / 256MiB", + "mtusi-lab3-traefik-1;21.52%;26.4MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;16.72%;90.58MiB / 7.622GiB", + "artship-backend;286.39%;91.25MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,18,100" + }, + { + "timestamp_epoch": 1793886659.2273002, + "containers": [ + "mtusi-lab3-api-2;0.15%;70.88MiB / 1GiB", + "mtusi-lab3-api-1;0.14%;70.85MiB / 1GiB", + "mtusi-lab3-api-3;26.51%;71.71MiB / 1GiB", + "mtusi-lab3-api-4;27.31%;84.06MiB / 1GiB", + "mtusi-lab3-postgres-1;0.11%;50.65MiB / 768MiB", + "mtusi-lab3-redis-1;0.29%;3.641MiB / 256MiB", + "mtusi-lab3-traefik-1;0.15%;25.74MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.24%;3.621MiB / 7.622GiB", + "artship-frontend;10.69%;90.65MiB / 7.622GiB", + "artship-backend;216.51%;192.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + } + ] +} diff --git a/evidence/scaling/fixed_1replicas.json b/evidence/scaling/fixed_1replicas.json new file mode 100644 index 00000000..2338c2b2 --- /dev/null +++ b/evidence/scaling/fixed_1replicas.json @@ -0,0 +1,163 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 31.192, + "concurrency": 8, + "successful_requests": 1171, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 37.541, + "successful_latency_ms": { + "p50": 200.219, + "p95": 302.388, + "p99": 343.044 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "fixed_1replicas", + "api_replicas": 1, + "total_api_cpu_budget": 2.0, + "total_api_memory_budget_mb": 2048, + "per_replica_cpu_limit": 2.0, + "per_replica_memory_limit_mb": 2048, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886308.9941907, + "containers": [ + "mtusi-lab3-api-2;156.65%;68.66MiB / 2GiB", + "mtusi-lab3-traefik-1;1.95%;22.2MiB / 256MiB", + "mtusi-lab3-postgres-1;3.78%;38.56MiB / 768MiB", + "mtusi-lab3-redis-1;1.14%;3.652MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;1.39%;4.688MiB / 7.622GiB", + "artship-frontend;27.42%;90.34MiB / 7.622GiB", + "artship-backend;374.29%;161MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,8,100" + }, + { + "timestamp_epoch": 1793886312.793944, + "containers": [ + "mtusi-lab3-api-2;126.05%;70.95MiB / 2GiB", + "mtusi-lab3-traefik-1;7.03%;22.25MiB / 256MiB", + "mtusi-lab3-postgres-1;16.93%;45.42MiB / 768MiB", + "mtusi-lab3-redis-1;8.24%;3.727MiB / 256MiB", + "mtusi-lab2-postgres;8.17%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.42%;3.621MiB / 7.622GiB", + "artship-frontend;16.78%;90.21MiB / 7.622GiB", + "artship-backend;283.66%;175.6MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886316.5291777, + "containers": [ + "mtusi-lab3-api-2;128.49%;71.69MiB / 2GiB", + "mtusi-lab3-traefik-1;10.37%;22.66MiB / 256MiB", + "mtusi-lab3-postgres-1;23.24%;45.76MiB / 768MiB", + "mtusi-lab3-redis-1;4.93%;3.98MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.26%;3.621MiB / 7.622GiB", + "artship-frontend;11.39%;89.81MiB / 7.622GiB", + "artship-backend;0.13%;194.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886320.1488795, + "containers": [ + "mtusi-lab3-api-2;127.16%;71.79MiB / 2GiB", + "mtusi-lab3-traefik-1;8.01%;24.7MiB / 256MiB", + "mtusi-lab3-postgres-1;17.77%;45.66MiB / 768MiB", + "mtusi-lab3-redis-1;4.97%;3.992MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;2.68%;3.621MiB / 7.622GiB", + "artship-frontend;10.59%;89.96MiB / 7.622GiB", + "artship-backend;1.54%;194.2MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886323.702454, + "containers": [ + "mtusi-lab3-api-2;129.85%;72.13MiB / 2GiB", + "mtusi-lab3-traefik-1;9.83%;24.86MiB / 256MiB", + "mtusi-lab3-postgres-1;17.79%;45.79MiB / 768MiB", + "mtusi-lab3-redis-1;4.94%;3.719MiB / 256MiB", + "mtusi-lab2-postgres;3.57%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.27%;3.621MiB / 7.622GiB", + "artship-frontend;13.25%;90.58MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886327.376915, + "containers": [ + "mtusi-lab3-api-2;153.00%;72.02MiB / 2GiB", + "mtusi-lab3-traefik-1;9.96%;24.62MiB / 256MiB", + "mtusi-lab3-postgres-1;25.86%;45.89MiB / 768MiB", + "mtusi-lab3-redis-1;4.45%;3.73MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.35%;3.621MiB / 7.622GiB", + "artship-frontend;15.48%;90.14MiB / 7.622GiB", + "artship-backend;378.58%;163.3MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886330.452997, + "containers": [ + "mtusi-lab3-api-2;126.84%;72.73MiB / 2GiB", + "mtusi-lab3-traefik-1;7.46%;24.89MiB / 256MiB", + "mtusi-lab3-postgres-1;15.44%;45.93MiB / 768MiB", + "mtusi-lab3-redis-1;6.95%;3.727MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.871MiB / 7.622GiB", + "artship-frontend;14.43%;90.26MiB / 7.622GiB", + "artship-backend;312.29%;207.5MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886334.1144435, + "containers": [ + "mtusi-lab3-api-2;128.83%;72.35MiB / 2GiB", + "mtusi-lab3-traefik-1;8.33%;24.91MiB / 256MiB", + "mtusi-lab3-postgres-1;18.45%;46.34MiB / 768MiB", + "mtusi-lab3-redis-1;5.02%;3.711MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.76MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;10.77%;89.52MiB / 7.622GiB", + "artship-backend;0.36%;188MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886337.7399256, + "containers": [ + "mtusi-lab3-api-2;146.45%;82.47MiB / 2GiB", + "mtusi-lab3-traefik-1;8.20%;25.16MiB / 256MiB", + "mtusi-lab3-postgres-1;27.85%;46.15MiB / 768MiB", + "mtusi-lab3-redis-1;4.50%;3.734MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.29%;3.621MiB / 7.622GiB", + "artship-frontend;13.33%;90.15MiB / 7.622GiB", + "artship-backend;31.27%;190.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886341.2850015, + "containers": [ + "mtusi-lab3-api-2;97.10%;72.21MiB / 2GiB", + "mtusi-lab3-traefik-1;0.15%;24.65MiB / 256MiB", + "mtusi-lab3-postgres-1;0.12%;42.68MiB / 768MiB", + "mtusi-lab3-redis-1;0.26%;3.715MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.27%;3.621MiB / 7.622GiB", + "artship-frontend;10.22%;90.5MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,9,100" + } + ] +} diff --git a/evidence/scaling/fixed_2replicas.json b/evidence/scaling/fixed_2replicas.json new file mode 100644 index 00000000..6c5a4f48 --- /dev/null +++ b/evidence/scaling/fixed_2replicas.json @@ -0,0 +1,175 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 31.092, + "concurrency": 8, + "successful_requests": 1841, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 59.211, + "successful_latency_ms": { + "p50": 115.483, + "p95": 294.656, + "p99": 422.202 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "fixed_2replicas", + "api_replicas": 2, + "total_api_cpu_budget": 2.0, + "total_api_memory_budget_mb": 2048, + "per_replica_cpu_limit": 1.0, + "per_replica_memory_limit_mb": 1024, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886376.9756248, + "containers": [ + "mtusi-lab3-api-1;51.87%;68.37MiB / 1GiB", + "mtusi-lab3-api-2;77.98%;132.9MiB / 1GiB", + "mtusi-lab3-redis-1;0.55%;3.531MiB / 256MiB", + "mtusi-lab3-postgres-1;1.24%;23.3MiB / 768MiB", + "mtusi-lab3-traefik-1;4.24%;18.92MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.34%;3.621MiB / 7.622GiB", + "artship-frontend;20.40%;90.15MiB / 7.622GiB", + "artship-backend;104.68%;216.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,8,100" + }, + { + "timestamp_epoch": 1793886380.6233075, + "containers": [ + "mtusi-lab3-api-1;63.34%;69.52MiB / 1GiB", + "mtusi-lab3-api-2;99.86%;70.32MiB / 1GiB", + "mtusi-lab3-redis-1;6.89%;3.621MiB / 256MiB", + "mtusi-lab3-postgres-1;25.24%;37.15MiB / 768MiB", + "mtusi-lab3-traefik-1;12.69%;17.4MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;1.74%;5.023MiB / 7.622GiB", + "artship-frontend;16.37%;90MiB / 7.622GiB", + "artship-backend;0.13%;186.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,14,100" + }, + { + "timestamp_epoch": 1793886384.450458, + "containers": [ + "mtusi-lab3-api-1;102.42%;71.07MiB / 1GiB", + "mtusi-lab3-api-2;62.14%;79.63MiB / 1GiB", + "mtusi-lab3-redis-1;6.78%;3.641MiB / 256MiB", + "mtusi-lab3-postgres-1;23.87%;37.39MiB / 768MiB", + "mtusi-lab3-traefik-1;12.01%;22.12MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.21%;3.621MiB / 7.622GiB", + "artship-frontend;12.24%;91.39MiB / 7.622GiB", + "artship-backend;55.03%;216MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,12,100" + }, + { + "timestamp_epoch": 1793886388.0942245, + "containers": [ + "mtusi-lab3-api-1;76.11%;70.86MiB / 1GiB", + "mtusi-lab3-api-2;102.72%;70.89MiB / 1GiB", + "mtusi-lab3-redis-1;10.08%;3.652MiB / 256MiB", + "mtusi-lab3-postgres-1;40.86%;37.18MiB / 768MiB", + "mtusi-lab3-traefik-1;19.01%;23.29MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.40%;3.621MiB / 7.622GiB", + "artship-frontend;27.12%;90.24MiB / 7.622GiB", + "artship-backend;248.73%;86.68MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,14,100" + }, + { + "timestamp_epoch": 1793886391.515072, + "containers": [ + "mtusi-lab3-api-1;59.60%;71MiB / 1GiB", + "mtusi-lab3-api-2;99.22%;71.54MiB / 1GiB", + "mtusi-lab3-redis-1;6.07%;3.645MiB / 256MiB", + "mtusi-lab3-postgres-1;21.92%;37.7MiB / 768MiB", + "mtusi-lab3-traefik-1;10.00%;23.32MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;3.11%;3.621MiB / 7.622GiB", + "artship-frontend;19.89%;90.33MiB / 7.622GiB", + "artship-backend;278.19%;162.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,14,100" + }, + { + "timestamp_epoch": 1793886395.2497733, + "containers": [ + "mtusi-lab3-api-1;98.55%;71.2MiB / 1GiB", + "mtusi-lab3-api-2;71.64%;71.15MiB / 1GiB", + "mtusi-lab3-redis-1;6.33%;3.645MiB / 256MiB", + "mtusi-lab3-postgres-1;24.14%;38.01MiB / 768MiB", + "mtusi-lab3-traefik-1;11.56%;24.08MiB / 256MiB", + "mtusi-lab2-postgres;4.45%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.24%;3.621MiB / 7.622GiB", + "artship-frontend;14.77%;89.61MiB / 7.622GiB", + "artship-backend;0.11%;206MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "2,14,100" + }, + { + "timestamp_epoch": 1793886398.833462, + "containers": [ + "mtusi-lab3-api-1;62.39%;71.52MiB / 1GiB", + "mtusi-lab3-api-2;98.78%;71.27MiB / 1GiB", + "mtusi-lab3-redis-1;7.11%;3.652MiB / 256MiB", + "mtusi-lab3-postgres-1;33.22%;39.12MiB / 768MiB", + "mtusi-lab3-traefik-1;12.71%;24.46MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.22%;3.621MiB / 7.622GiB", + "artship-frontend;12.62%;90.27MiB / 7.622GiB", + "artship-backend;0.12%;190.3MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,14,100" + }, + { + "timestamp_epoch": 1793886401.9088044, + "containers": [ + "mtusi-lab3-api-1;100.93%;71.64MiB / 1GiB", + "mtusi-lab3-api-2;70.64%;71.34MiB / 1GiB", + "mtusi-lab3-redis-1;6.92%;3.652MiB / 256MiB", + "mtusi-lab3-postgres-1;25.53%;38.7MiB / 768MiB", + "mtusi-lab3-traefik-1;10.80%;24.73MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.28%;3.621MiB / 7.622GiB", + "artship-frontend;14.29%;89.85MiB / 7.622GiB", + "artship-backend;163.06%;231.5MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,13,100" + }, + { + "timestamp_epoch": 1793886405.4339893, + "containers": [ + "mtusi-lab3-api-1;73.26%;71.46MiB / 1GiB", + "mtusi-lab3-api-2;100.07%;71.77MiB / 1GiB", + "mtusi-lab3-redis-1;7.43%;3.645MiB / 256MiB", + "mtusi-lab3-postgres-1;27.75%;39.54MiB / 768MiB", + "mtusi-lab3-traefik-1;11.30%;24.62MiB / 256MiB", + "mtusi-lab2-postgres;7.36%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.33%;3.621MiB / 7.622GiB", + "artship-frontend;15.82%;90.02MiB / 7.622GiB", + "artship-backend;257.77%;116.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "3,13,100" + }, + { + "timestamp_epoch": 1793886409.1543102, + "containers": [ + "mtusi-lab3-api-1;23.24%;71.61MiB / 1GiB", + "mtusi-lab3-api-2;61.54%;71.61MiB / 1GiB", + "mtusi-lab3-redis-1;3.04%;3.609MiB / 256MiB", + "mtusi-lab3-postgres-1;19.24%;35.26MiB / 768MiB", + "mtusi-lab3-traefik-1;5.64%;24.61MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.31%;3.621MiB / 7.622GiB", + "artship-frontend;14.31%;91.09MiB / 7.622GiB", + "artship-backend;307.13%;179.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,12,100" + } + ] +} diff --git a/evidence/scaling/fixed_4replicas.json b/evidence/scaling/fixed_4replicas.json new file mode 100644 index 00000000..c42773b2 --- /dev/null +++ b/evidence/scaling/fixed_4replicas.json @@ -0,0 +1,195 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 30.0, + "elapsed_seconds": 30.982, + "concurrency": 8, + "successful_requests": 2041, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 65.877, + "successful_latency_ms": { + "p50": 49.308, + "p95": 390.895, + "p99": 564.725 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "scenario": "fixed_4replicas", + "api_replicas": 4, + "total_api_cpu_budget": 2.0, + "total_api_memory_budget_mb": 2048, + "per_replica_cpu_limit": 0.5, + "per_replica_memory_limit_mb": 512, + "container_stats_samples": [ + { + "timestamp_epoch": 1793886439.8320873, + "containers": [ + "mtusi-lab3-api-1;14.55%;76.53MiB / 512MiB", + "mtusi-lab3-api-4;22.31%;67.68MiB / 512MiB", + "mtusi-lab3-api-2;30.78%;77.67MiB / 512MiB", + "mtusi-lab3-api-3;23.98%;129.4MiB / 512MiB", + "mtusi-lab3-postgres-1;8.71%;29.34MiB / 768MiB", + "mtusi-lab3-redis-1;2.42%;3.527MiB / 256MiB", + "mtusi-lab3-traefik-1;5.67%;17.83MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.23%;3.621MiB / 7.622GiB", + "artship-frontend;6.88%;91.35MiB / 7.622GiB", + "artship-backend;33.55%;225.6MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,11,100" + }, + { + "timestamp_epoch": 1793886443.32589, + "containers": [ + "mtusi-lab3-api-1;40.97%;68.92MiB / 512MiB", + "mtusi-lab3-api-4;38.69%;69.05MiB / 512MiB", + "mtusi-lab3-api-2;41.56%;68.95MiB / 512MiB", + "mtusi-lab3-api-3;0.52%;68.41MiB / 512MiB", + "mtusi-lab3-postgres-1;0.99%;29.95MiB / 768MiB", + "mtusi-lab3-redis-1;7.58%;3.621MiB / 256MiB", + "mtusi-lab3-traefik-1;12.74%;17.97MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.33%;3.621MiB / 7.622GiB", + "artship-frontend;13.31%;90.68MiB / 7.622GiB", + "artship-backend;284.48%;125.1MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,17,100" + }, + { + "timestamp_epoch": 1793886446.9308507, + "containers": [ + "mtusi-lab3-api-1;35.63%;70.45MiB / 512MiB", + "mtusi-lab3-api-4;22.38%;69.54MiB / 512MiB", + "mtusi-lab3-api-2;41.30%;69.42MiB / 512MiB", + "mtusi-lab3-api-3;52.35%;70.25MiB / 512MiB", + "mtusi-lab3-postgres-1;23.56%;49.49MiB / 768MiB", + "mtusi-lab3-redis-1;5.55%;3.891MiB / 256MiB", + "mtusi-lab3-traefik-1;9.18%;19.32MiB / 256MiB", + "mtusi-lab2-postgres;7.12%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.35%;3.621MiB / 7.622GiB", + "artship-frontend;12.13%;90.22MiB / 7.622GiB", + "artship-backend;169.33%;197.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886450.341239, + "containers": [ + "mtusi-lab3-api-1;32.69%;70.29MiB / 512MiB", + "mtusi-lab3-api-4;31.28%;69.4MiB / 512MiB", + "mtusi-lab3-api-2;31.14%;69.56MiB / 512MiB", + "mtusi-lab3-api-3;48.61%;70.59MiB / 512MiB", + "mtusi-lab3-postgres-1;32.50%;49.94MiB / 768MiB", + "mtusi-lab3-redis-1;9.24%;3.637MiB / 256MiB", + "mtusi-lab3-traefik-1;13.54%;23.32MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.25%;3.621MiB / 7.622GiB", + "artship-frontend;11.36%;90.63MiB / 7.622GiB", + "artship-backend;0.12%;193.9MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + }, + { + "timestamp_epoch": 1793886453.8616302, + "containers": [ + "mtusi-lab3-api-1;20.90%;70.34MiB / 512MiB", + "mtusi-lab3-api-4;29.44%;70.47MiB / 512MiB", + "mtusi-lab3-api-2;30.87%;69.68MiB / 512MiB", + "mtusi-lab3-api-3;48.38%;70.76MiB / 512MiB", + "mtusi-lab3-postgres-1;26.14%;52.14MiB / 768MiB", + "mtusi-lab3-redis-1;6.99%;3.609MiB / 256MiB", + "mtusi-lab3-traefik-1;12.03%;24.61MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.30%;3.621MiB / 7.622GiB", + "artship-frontend;13.37%;90.25MiB / 7.622GiB", + "artship-backend;212.77%;217.8MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886457.4135027, + "containers": [ + "mtusi-lab3-api-1;40.25%;79.11MiB / 512MiB", + "mtusi-lab3-api-4;36.32%;73.41MiB / 512MiB", + "mtusi-lab3-api-2;34.07%;69.7MiB / 512MiB", + "mtusi-lab3-api-3;48.66%;70.86MiB / 512MiB", + "mtusi-lab3-postgres-1;26.59%;50.24MiB / 768MiB", + "mtusi-lab3-redis-1;7.12%;3.637MiB / 256MiB", + "mtusi-lab3-traefik-1;13.18%;25.34MiB / 256MiB", + "mtusi-lab2-postgres;7.89%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.35%;3.621MiB / 7.622GiB", + "artship-frontend;15.27%;90.54MiB / 7.622GiB", + "artship-backend;233.64%;83.11MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886460.5889723, + "containers": [ + "mtusi-lab3-api-1;30.46%;70.4MiB / 512MiB", + "mtusi-lab3-api-4;29.90%;70.57MiB / 512MiB", + "mtusi-lab3-api-2;28.90%;69.97MiB / 512MiB", + "mtusi-lab3-api-3;48.58%;70.95MiB / 512MiB", + "mtusi-lab3-postgres-1;31.68%;53.58MiB / 768MiB", + "mtusi-lab3-redis-1;6.92%;3.629MiB / 256MiB", + "mtusi-lab3-traefik-1;11.86%;24.99MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.31%;3.621MiB / 7.622GiB", + "artship-frontend;12.86%;90.39MiB / 7.622GiB", + "artship-backend;229.79%;151.4MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886464.2206683, + "containers": [ + "mtusi-lab3-api-1;48.31%;70.61MiB / 512MiB", + "mtusi-lab3-api-4;45.63%;70.41MiB / 512MiB", + "mtusi-lab3-api-2;30.25%;70.02MiB / 512MiB", + "mtusi-lab3-api-3;22.41%;71.05MiB / 512MiB", + "mtusi-lab3-postgres-1;18.41%;53.68MiB / 768MiB", + "mtusi-lab3-redis-1;4.46%;3.617MiB / 256MiB", + "mtusi-lab3-traefik-1;8.34%;25.7MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.20%;3.621MiB / 7.622GiB", + "artship-frontend;10.76%;90.5MiB / 7.622GiB", + "artship-backend;0.12%;195.6MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,19,100" + }, + { + "timestamp_epoch": 1793886467.790955, + "containers": [ + "mtusi-lab3-api-1;32.46%;70.68MiB / 512MiB", + "mtusi-lab3-api-4;32.38%;70.47MiB / 512MiB", + "mtusi-lab3-api-2;31.19%;70.07MiB / 512MiB", + "mtusi-lab3-api-3;51.48%;71.19MiB / 512MiB", + "mtusi-lab3-postgres-1;27.38%;52.98MiB / 768MiB", + "mtusi-lab3-redis-1;7.26%;3.891MiB / 256MiB", + "mtusi-lab3-traefik-1;14.21%;25.74MiB / 256MiB", + "mtusi-lab2-postgres;4.97%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;2.31%;3.656MiB / 7.622GiB", + "artship-frontend;9.96%;90.6MiB / 7.622GiB", + "artship-backend;3.71%;183.3MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,20,100" + }, + { + "timestamp_epoch": 1793886470.821725, + "containers": [ + "mtusi-lab3-api-1;45.51%;80.78MiB / 512MiB", + "mtusi-lab3-api-4;44.33%;80.57MiB / 512MiB", + "mtusi-lab3-api-2;21.53%;70.02MiB / 512MiB", + "mtusi-lab3-api-3;30.89%;80.72MiB / 512MiB", + "mtusi-lab3-postgres-1;25.84%;51.66MiB / 768MiB", + "mtusi-lab3-redis-1;5.05%;3.633MiB / 256MiB", + "mtusi-lab3-traefik-1;10.13%;26.51MiB / 256MiB", + "mtusi-lab2-postgres;0.00%;20.72MiB / 7.622GiB", + "mtusi-lab2-redis;0.24%;3.621MiB / 7.622GiB", + "artship-frontend;12.54%;89.95MiB / 7.622GiB", + "artship-backend;292.64%;140.2MiB / 7.622GiB" + ], + "postgres_active_total_max_connections": "1,18,100" + } + ] +} diff --git a/evidence/scaling/p95-latency.png b/evidence/scaling/p95-latency.png new file mode 100644 index 00000000..bbf793d9 Binary files /dev/null and b/evidence/scaling/p95-latency.png differ diff --git a/evidence/scaling/summary.csv b/evidence/scaling/summary.csv new file mode 100644 index 00000000..23ead99b --- /dev/null +++ b/evidence/scaling/summary.csv @@ -0,0 +1,7 @@ +scenario,api_replicas,total_api_cpu_budget,total_api_memory_budget_mb,successful_rps,failed_requests,error_ratio,p50_ms,p95_ms,p99_ms +fixed_1replicas,1,2.0,2048,37.541,0,0.0,200.219,302.388,343.044 +fixed_2replicas,2,2.0,2048,59.211,0,0.0,115.483,294.656,422.202 +fixed_4replicas,4,2.0,2048,65.877,0,0.0,49.308,390.895,564.725 +expanded_4replicas_2cpu_2048mb,4,2.0,2048,68.467,0,0.0,50.668,374.351,489.573 +expanded_4replicas_3cpu_3072mb,4,3.0,3072,95.214,0,0.0,54.992,238.599,440.463 +expanded_4replicas_4cpu_4096mb,4,4.0,4096,119.589,0,0.0,55.339,137.459,309.06 diff --git a/evidence/scaling/throughput.png b/evidence/scaling/throughput.png new file mode 100644 index 00000000..68268cc7 Binary files /dev/null and b/evidence/scaling/throughput.png differ diff --git a/evidence/smoke-final.txt b/evidence/smoke-final.txt new file mode 100644 index 00000000..fc20f9dd --- /dev/null +++ b/evidence/smoke-final.txt @@ -0,0 +1 @@ +PASS: lab3 public API scenario; infrastructure and grading evidence remain separate. diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..0bc520ad --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,60 @@ +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import engine_from_config, pool + +from app.core.config import settings +from app.db.base import Base +from app.db import models + + +config = context.config + +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +config.set_main_option( + "sqlalchemy.url", + settings.database_url.replace("%", "%%"), +) + +target_metadata = Base.metadata + + +def run_migrations_offline() -> None: + url = settings.database_url + + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + compare_type=True, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + compare_type=True, + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() \ No newline at end of file diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..fbc4b07d --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/21ad89656327_create_initial_database_schema.py b/migrations/versions/21ad89656327_create_initial_database_schema.py new file mode 100644 index 00000000..6c48b769 --- /dev/null +++ b/migrations/versions/21ad89656327_create_initial_database_schema.py @@ -0,0 +1,79 @@ +"""create initial database schema + +Revision ID: 21ad89656327 +Revises: +Create Date: 2026-11-04 17:31:39.336368 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '21ad89656327' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('chats', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('title', sa.String(length=100), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('users', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('username', sa.String(length=32), nullable=False), + sa.Column('display_name', sa.String(length=100), nullable=False), + sa.Column('password_hash', sa.String(length=255), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) + op.create_table('chat_members', + sa.Column('chat_id', sa.UUID(), nullable=False), + sa.Column('user_id', sa.UUID(), nullable=False), + sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('chat_id', 'user_id') + ) + op.create_table('messages', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('chat_id', sa.UUID(), nullable=False), + sa.Column('sender_id', sa.UUID(), nullable=False), + sa.Column('text', sa.Text(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'], unique=False) + op.create_table('sessions', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('token_hash', sa.String(length=64), nullable=False), + sa.Column('user_id', sa.UUID(), nullable=False), + sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_sessions_token_hash'), 'sessions', ['token_hash'], unique=True) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_sessions_token_hash'), table_name='sessions') + op.drop_table('sessions') + op.drop_index('ix_messages_chat_created_id', table_name='messages') + op.drop_table('messages') + op.drop_table('chat_members') + op.drop_index(op.f('ix_users_username'), table_name='users') + op.drop_table('users') + op.drop_table('chats') + # ### end Alembic commands ### diff --git a/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py b/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py new file mode 100644 index 00000000..a228b426 --- /dev/null +++ b/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py @@ -0,0 +1,39 @@ +"""add shared PostgreSQL idempotency records + +Revision ID: 8c3d9f4a21b7 +Revises: 21ad89656327 +Create Date: 2026-11-05 +""" +from typing import Sequence, Union +from alembic import op +import sqlalchemy as sa + +revision: str = "8c3d9f4a21b7" +down_revision: Union[str, None] = "21ad89656327" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.create_table( + "idempotency_records", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("user_id", sa.UUID(), nullable=False), + sa.Column("method", sa.String(length=10), nullable=False), + sa.Column("path", sa.String(length=255), nullable=False), + sa.Column("idempotency_key", sa.String(length=128), nullable=False), + sa.Column("request_hash", sa.String(length=64), nullable=False), + sa.Column("message_id", sa.UUID(), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["message_id"], ["messages.id"], ondelete="CASCADE"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("user_id", "method", "path", "idempotency_key", name="uq_idempotency_scope_key"), + ) + op.create_index("ix_idempotency_records_expires_at", "idempotency_records", ["expires_at"], unique=False) + + +def downgrade() -> None: + op.drop_index("ix_idempotency_records_expires_at", table_name="idempotency_records") + op.drop_table("idempotency_records") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..e519117f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +fastapi==0.115.6 +uvicorn[standard]==0.34.0 +pydantic==2.10.4 +pydantic-settings==2.7.1 +SQLAlchemy==2.0.36 +psycopg[binary]==3.2.3 +alembic==1.14.0 +redis==5.2.1 +pwdlib[argon2]==0.2.1 diff --git a/scripts/analyze-scaling.py b/scripts/analyze-scaling.py new file mode 100644 index 00000000..de77ecdf --- /dev/null +++ b/scripts/analyze-scaling.py @@ -0,0 +1,52 @@ + +#!/usr/bin/env python3 +"""Create charts from real Track 1 summary.csv data; does not invent measurements.""" + +import csv +from pathlib import Path +import matplotlib.pyplot as plt + +root = Path(__file__).resolve().parents[1] +source = root / 'evidence' / 'scaling' / 'summary.csv' + +if not source.exists(): + raise SystemExit('Run scripts/run-scaling-experiment.py first') + +with source.open(encoding='utf-8', newline='') as f: + rows = list(csv.DictReader(f)) + +if not rows: + raise SystemExit('summary.csv is empty') + +out = source.parent + +for key, title, ylabel, filename in [ + ('successful_rps', 'Track 1: successful throughput', + 'Successful requests/sec', 'throughput.png'), + ('p95_ms', 'Track 1: p95 latency', + 'Milliseconds', 'p95-latency.png'), + ('error_ratio', 'Track 1: error ratio', + 'Error ratio', 'errors.png'), +]: + labels = [r['scenario'] for r in rows] + values = [float(r[key]) if r[key] else 0 for r in rows] + + plt.figure(figsize=(11, 5)) + plt.bar(labels, values) + + if key == 'error_ratio': + plt.ylim(0, 0.01) + for i, value in enumerate(values): + plt.text( + i, 0.0002, f'{value:.1%}', + ha='center', va='bottom' + ) + + plt.title(title) + plt.ylabel(ylabel) + plt.xticks(rotation=25, ha='right') + plt.tight_layout() + plt.savefig(out / filename, dpi=160) + plt.close() + +print('Charts written to', out) diff --git a/scripts/concurrent-idempotency.py b/scripts/concurrent-idempotency.py new file mode 100644 index 00000000..0253e5a3 --- /dev/null +++ b/scripts/concurrent-idempotency.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Concurrent idempotency verification through the public load-balancer URL.""" +from concurrent.futures import ThreadPoolExecutor +import uuid +import sys +from pathlib import Path +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client, Failure, arguments, check, run + + +def main(): + def configure(p): + p.add_argument('--requests', type=int, default=20) + args = arguments('Concurrent cross-replica idempotency test', configure) + check(2 <= args.requests <= 100, 'requests must be 2..100') + c = Client(args.base_url, args.ca_file, timeout=10) + alice, bob, eve, chat = c.bootstrap() + key = 'race-' + uuid.uuid4().hex + body = 'same concurrent request ' + uuid.uuid4().hex + + def send(_): + return Client(args.base_url, args.ca_file, timeout=10).message(alice, chat, body, key=key) + + with ThreadPoolExecutor(max_workers=args.requests) as pool: + responses = list(pool.map(send, range(args.requests))) + messages = [r['json'] for r in responses] + check(all(m == messages[0] for m in messages), 'concurrent retries returned different messages') + instances = {r['headers'].get('X-Instance-Id') for r in responses} + check(None not in instances, 'missing X-Instance-Id') + check(len(instances) >= 2, 'load balancer did not expose at least two API replicas') + check(len({m['id'] for m in messages}) == 1, 'concurrent retries created duplicates') + conflict = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, + actor=alice, body={'text': body + ' changed'}, + headers={'Idempotency-Key': key}) + print({'requests': args.requests, 'unique_message_ids': 1, + 'observed_instances': len(instances), 'different_body_status': conflict['status']}) + print('PASS: concurrent same-key requests produced exactly one message') + +if __name__ == '__main__': + run(main) diff --git a/scripts/load-diagnostic.py b/scripts/load-diagnostic.py new file mode 100644 index 00000000..a958e494 --- /dev/null +++ b/scripts/load-diagnostic.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Diagnostic closed-loop load test. Does not modify public tests.""" +from concurrent.futures import ThreadPoolExecutor +from collections import Counter +from pathlib import Path +import json +import math +import re +import sys +import threading +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) + +from client import Client, Failure, LAB, arguments, check, run + + +def main(): + def configure(p): + p.add_argument('--duration', type=float, default=90) + p.add_argument('--concurrency', type=int, default=8) + p.add_argument('--output', default='evidence/rolling-update-diagnostic.json') + + args = arguments('Diagnostic synthetic POST message load', configure) + check(1 <= args.duration <= 3600, 'duration must be 1..3600 seconds') + check(1 <= args.concurrency <= 128, 'concurrency must be 1..128') + + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + barrier = threading.Barrier(args.concurrency) + + def worker(worker_id): + local = Client(args.base_url, args.ca_file, timeout=5) + barrier.wait() + deadline = time.monotonic() + args.duration + latencies = [] + errors = Counter() + examples = [] + index = 0 + + while time.monotonic() < deadline: + start = time.monotonic() + try: + local.message(alice, chat, 'diagnostic-%s-%s' % (worker_id, index)) + latencies.append(time.monotonic() - start) + except Failure as exc: + message = str(exc) + match = re.search(r'received (\d{3})', message) + + if match: + category = 'http_' + match.group(1) + elif 'connection/TLS failure' in message: + kind = re.search(r'\(([^)]+)\)', message) + category = 'connection_' + (kind.group(1) if kind else 'unknown') + else: + category = 'other_failure' + + errors[category] += 1 + if len(examples) < 10: + examples.append(message[:300]) + + index += 1 + + return latencies, errors, examples + + started = time.monotonic() + with ThreadPoolExecutor(max_workers=args.concurrency) as pool: + results = list(pool.map(worker, range(args.concurrency))) + elapsed = time.monotonic() - started + + latencies = sorted(x for values, _, _ in results for x in values) + errors = Counter() + examples = [] + + for _, counts, samples in results: + errors.update(counts) + examples.extend(samples) + + total_errors = sum(errors.values()) + total = len(latencies) + total_errors + + def percentile(p): + if not latencies: + return None + return round(1000 * latencies[max(0, math.ceil(p * len(latencies)) - 1)], 3) + + report = { + 'lab': LAB, + 'workload': 'closed-loop POST messages; no automatic retries', + 'duration_seconds': round(elapsed, 3), + 'concurrency': args.concurrency, + 'successful_requests': len(latencies), + 'failed_requests': total_errors, + 'error_ratio': round(total_errors / total, 6) if total else None, + 'errors_by_type': dict(errors), + 'successful_rps': round(len(latencies) / elapsed, 3), + 'successful_latency_ms': { + 'p50': percentile(.50), + 'p95': percentile(.95), + 'p99': percentile(.99) + }, + 'error_examples': examples[:20], + 'note': 'Latency percentiles cover successful requests only.' + } + + text = json.dumps(report, ensure_ascii=False, indent=2) + dest = Path(args.output) + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text(text + '\n', encoding='utf-8') + print(text) + check(bool(latencies), 'no successful writes') + + +if __name__ == '__main__': + run(main) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..cd4695e5 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +# Restart all stateful dependencies and all API replicas without touching volumes. +docker compose restart postgres redis api diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100644 index 00000000..4cbb334a --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -eu +# Compose does not provide a native rolling-update primitive. This script +# performs a surge replacement: add one new healthy replica, then remove one old. +desired="${API_REPLICAS:-2}" +load_duration="${ROLLING_LOAD_DURATION:-90}" +load_script="${LOAD_SCRIPT:-tests/load.py}" +load_output="${ROLLING_LOAD_OUTPUT:-evidence/rolling-update-load.json}" +load_log="${ROLLING_LOAD_LOG:-evidence/rolling-update-load.log}" +if [ "$desired" -lt 2 ]; then echo 'API_REPLICAS must be >= 2' >&2; exit 2; fi + +docker compose build api +python "$load_script" --base-url http://localhost:8080 --duration "$load_duration" --concurrency 8 --output "$load_output" >"$load_log" 2>&1 & +load_pid=$! +cleanup() { kill "$load_pid" 2>/dev/null || true; wait "$load_pid" 2>/dev/null || true; } +trap cleanup EXIT INT TERM + +# Capture the old replica ids. Each iteration adds a new container before removing an old one. +old_ids=$(docker compose ps -q api) +for old_id in $old_ids; do + before=$(docker compose ps -q api | sort) + docker compose up -d --no-deps --no-recreate --scale "api=$((desired + 1))" api + new_id='' + attempt=0 + while [ "$attempt" -lt 60 ]; do + after=$(docker compose ps -q api | sort) + new_id=$(comm -13 <(printf '%s\n' "$before") <(printf '%s\n' "$after") | head -n 1 || true) + if [ -n "$new_id" ]; then + health=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$new_id") + if [ "$health" = healthy ]; then break; fi + fi + attempt=$((attempt + 1)); sleep 2 + done + if [ -z "$new_id" ] || [ "${health:-}" != healthy ]; then + echo 'New replica did not become healthy; leaving old replicas running' >&2 + exit 1 + fi + docker exec "$old_id" sh -c 'touch /tmp/api-draining' + sleep 10 + docker stop --timeout 20 "$old_id" >/dev/null + docker rm "$old_id" >/dev/null + docker compose up -d --no-deps --no-recreate --scale "api=$desired" api + python scripts/wait-ready.py http://localhost:8080/health/ready 30 +done +wait "$load_pid" || { echo 'Load test failed; inspect evidence/rolling-update-load.log' >&2; exit 1; } +trap - EXIT INT TERM diff --git a/scripts/run-scaling-experiment.py b/scripts/run-scaling-experiment.py new file mode 100644 index 00000000..24e00595 --- /dev/null +++ b/scripts/run-scaling-experiment.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 +"""Run Track 1 scenarios on the user's Docker host; records only measured results.""" +import argparse +import csv +import json +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +OUT = ROOT / 'evidence' / 'scaling' +BASE_ENV = ROOT / '.env' + + +def run(cmd, env=None, check=True, capture=False): + return subprocess.run(cmd, cwd=ROOT, env=env, check=check, + text=True, stdout=subprocess.PIPE if capture else None, + stderr=subprocess.STDOUT if capture else None) + + +def stats(): + p = run(['docker', 'stats', '--no-stream', '--format', '{{.Name}};{{.CPUPerc}};{{.MemUsage}}'], capture=True) + return p.stdout.strip().splitlines() + + +def env_value(source, key, default): + prefix = key + '=' + for line in source.splitlines(): + if line.startswith(prefix): + return line[len(prefix):].strip().strip('\"').strip("'") + return default + + +def db_activity(env_path, db_user, db_name): + query = "SELECT count(*) FILTER (WHERE state='active'), count(*), current_setting('max_connections') FROM pg_stat_activity" + result = run(['docker', 'compose', '--env-file', str(env_path), 'exec', '-T', 'postgres', + 'psql', '-U', db_user, '-d', db_name, '-At', '-F', ',', '-c', query], + capture=True, check=False) + return result.stdout.strip() if result.returncode == 0 else 'unavailable' + + +def scenario(name, replicas, total_cpu, total_mem, duration, concurrency): + source = BASE_ENV.read_text(encoding='utf-8') + db_user = env_value(source, 'POSTGRES_USER', 'messenger') + db_name = env_value(source, 'POSTGRES_DB', 'messenger') + api_cpu = total_cpu / replicas + api_mem_mb = total_mem // replicas + env_text = source.rstrip() + f'\nAPI_CPU_LIMIT={api_cpu:.3f}\nAPI_MEMORY_LIMIT={api_mem_mb}M\n' + env_path = OUT / ('.env-' + name) + env_path.write_text(env_text, encoding='utf-8') + compose = ['docker', 'compose', '--env-file', str(env_path), 'up', '-d', '--build', '--scale', f'api={replicas}'] + run(compose) + deadline = time.time() + 120 + while time.time() < deadline: + result = run(['docker', 'compose', '--env-file', str(env_path), 'ps', '-q', 'api'], capture=True, check=False) + ids = [line.strip() for line in result.stdout.splitlines() if line.strip()] + healthy = 0 + for container_id in ids: + inspected = run(['docker', 'inspect', '--format', '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}', container_id], capture=True, check=False) + if inspected.returncode == 0 and inspected.stdout.strip() == 'healthy': + healthy += 1 + if len(ids) >= replicas and healthy >= replicas: + break + time.sleep(2) + else: + raise RuntimeError(f'{name}: API replicas did not become healthy') + + output = OUT / (name + '.json') + cmd = [sys.executable, 'tests/load.py', '--base-url', 'http://localhost:8080', + '--duration', str(duration), '--concurrency', str(concurrency), '--output', str(output)] + proc = subprocess.Popen(cmd, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + samples = [] + while proc.poll() is None: + timestamp = time.time() + try: + samples.append({'timestamp_epoch': timestamp, 'containers': stats(), + 'postgres_active_total_max_connections': db_activity(env_path, db_user, db_name)}) + except Exception as exc: + samples.append({'timestamp_epoch': timestamp, 'stats_error': type(exc).__name__}) + time.sleep(1) + stdout = proc.stdout.read() if proc.stdout else '' + if proc.returncode != 0: + raise RuntimeError(f'load failed for {name}: {stdout}') + metrics = json.loads(output.read_text(encoding='utf-8')) + metrics.update({'scenario': name, 'api_replicas': replicas, + 'total_api_cpu_budget': total_cpu, + 'total_api_memory_budget_mb': total_mem, + 'per_replica_cpu_limit': api_cpu, + 'per_replica_memory_limit_mb': api_mem_mb, + 'container_stats_samples': samples}) + output.write_text(json.dumps(metrics, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') + run(['docker', 'compose', '--env-file', str(env_path), 'down']) + return metrics + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--duration', type=int, default=30) + p.add_argument('--concurrency', type=int, default=8) + p.add_argument('--fixed-cpu', type=float, default=2.0) + p.add_argument('--fixed-memory-mb', type=int, default=2048) + p.add_argument('--expanded-cpu', type=float, nargs=3, default=[2.0, 4.0, 8.0]) + p.add_argument('--expanded-memory-mb', type=int, nargs=3, default=[2048, 4096, 8192]) + args = p.parse_args() + if not BASE_ENV.exists(): + p.error('Create .env from .env.example before running') + OUT.mkdir(parents=True, exist_ok=True) + results = [] + for n in (1, 2, 4): + results.append(scenario(f'fixed_{n}replicas', n, args.fixed_cpu, + args.fixed_memory_mb, args.duration, args.concurrency)) + for cpu, mem in zip(args.expanded_cpu, args.expanded_memory_mb): + results.append(scenario(f'expanded_4replicas_{cpu:g}cpu_{mem}mb', 4, cpu, + mem, args.duration, args.concurrency)) + with (OUT / 'summary.csv').open('w', newline='', encoding='utf-8') as f: + fields = ['scenario', 'api_replicas', 'total_api_cpu_budget', 'total_api_memory_budget_mb', + 'successful_rps', 'failed_requests', 'error_ratio', 'p50_ms', 'p95_ms', 'p99_ms'] + writer = csv.DictWriter(f, fieldnames=fields) + writer.writeheader() + for x in results: + lat = x['successful_latency_ms'] + writer.writerow({'scenario': x['scenario'], 'api_replicas': x['api_replicas'], + 'total_api_cpu_budget': x['total_api_cpu_budget'], + 'total_api_memory_budget_mb': x['total_api_memory_budget_mb'], + 'successful_rps': x['successful_rps'], 'failed_requests': x['failed_requests'], + 'error_ratio': x['error_ratio'], 'p50_ms': lat['p50'], 'p95_ms': lat['p95'], 'p99_ms': lat['p99']}) + print(f'Wrote measured results to {OUT}') + +if __name__ == '__main__': + main() diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..e9bb595c --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -eu +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" +project="${COMPOSE_PROJECT_NAME:-mtusi-lab3}" +found="" +for id in $(docker ps -q --filter "label=com.docker.compose.project=${project}" --filter "label=com.docker.compose.service=api"); do + hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id") + if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then + found="$id" + break + fi +done +if [ -z "$found" ]; then + echo "No running API container matches TARGET_INSTANCE_ID" >&2 + exit 1 +fi +docker stop --time 3 "$found" >/dev/null diff --git a/scripts/wait-ready.py b/scripts/wait-ready.py new file mode 100644 index 00000000..a9006ecf --- /dev/null +++ b/scripts/wait-ready.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +import sys +import time +import urllib.request + +url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080/health/ready' +timeout = int(sys.argv[2]) if len(sys.argv) > 2 else 30 +deadline = time.monotonic() + timeout +while time.monotonic() < deadline: + try: + with urllib.request.urlopen(url, timeout=2) as response: + if response.status == 200: + raise SystemExit(0) + except Exception: + time.sleep(0.5) +raise SystemExit('Readiness timeout: ' + url) diff --git a/tests/client.py b/tests/client.py index f0499a29..38159a0c 100644 --- a/tests/client.py +++ b/tests/client.py @@ -14,7 +14,7 @@ import uuid ROOT = Path(__file__).resolve().parents[1] LAB = json.loads((ROOT / 'lab.json').read_text())['number'] -SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text()) +SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text(encoding='utf-8')) class Failure(AssertionError): pass diff --git a/tests/resilience.py b/tests/resilience.py index f3fa700a..a0c1736a 100644 --- a/tests/resilience.py +++ b/tests/resilience.py @@ -55,7 +55,7 @@ def main(): check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity') print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.') # No shell interpolation and no Docker commands in this test. - proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + proc = subprocess.Popen([os.environ.get("BASH_EXE", "bash"), str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) started = time.monotonic() successes,failures,after_instances = [],0,[] try: