ЛР3 - БВТ2302 Зюлина - на 3 #57
+3
-10
@@ -1,13 +1,6 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=replace-with-a-random-32-character-value
|
||||
POSTGRES_DB=messenger
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
FROM python:3.11.10-slim-bookworm
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Устанавливаем зависимости
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Копируем код приложения
|
||||
COPY src/ ./src/
|
||||
COPY alembic/ ./alembic/
|
||||
COPY alembic.ini .
|
||||
COPY entrypoint.sh .
|
||||
|
||||
RUN chmod +x entrypoint.sh
|
||||
|
||||
# Открываем порт
|
||||
EXPOSE 8080
|
||||
|
||||
ENTRYPOINT ["./entrypoint.sh"]
|
||||
|
||||
# Команда запуска
|
||||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"]
|
||||
@@ -4,7 +4,7 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help check validate test load test-persistence test-failover test-idempotency
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@@ -28,3 +28,6 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-idempotency:
|
||||
$(PYTHON) tests/concurrent_idempotency.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
@@ -1,37 +1,66 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Зюлина Екатерина Михайловна, БВТ2302.
|
||||
- Целевая оценка: 3.
|
||||
- Commit/PR: заполнить после публикации.
|
||||
- Использованные источники и помощь: документация/контракт репозитория; собственная реализация lab2; помощь Codex при анализе требований и реализации.
|
||||
|
||||
## Запуск из чистого clone
|
||||
## Запуск из чистого clone (Windows PowerShell + Docker Desktop Linux Engine)
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Требуются Docker Desktop с Linux containers, Docker Compose v2, Git и Python 3.11. Из PowerShell в корне clone:
|
||||
|
||||
```powershell
|
||||
Copy-Item .env.example .env
|
||||
py -3.11 -c 'from pathlib import Path; import secrets; p=Path(".env"); p.write_text(p.read_text().replace("replace-with-a-random-32-character-value", secrets.token_hex(24)))'
|
||||
$env:PYTHONUTF8 = '1'
|
||||
py -3.11 -m venv .venv
|
||||
.\.venv\Scripts\python.exe -m pip install -r requirements-dev.txt
|
||||
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
Invoke-WebRequest http://localhost:8080/health/ready -UseBasicParsing
|
||||
.\.venv\Scripts\python.exe scripts\check_contract.py
|
||||
.\.venv\Scripts\python.exe tests\smoke.py --base-url http://localhost:8080
|
||||
.\.venv\Scripts\python.exe tests\concurrent_idempotency.py --base-url http://localhost:8080
|
||||
.\.venv\Scripts\python.exe tests\resilience.py --mode persistence --action-script scripts\restart.bat --base-url http://localhost:8080
|
||||
.\.venv\Scripts\python.exe tests\resilience.py --mode failover --action-script scripts\stop-one.bat --base-url http://localhost:8080
|
||||
```
|
||||
|
||||
Для Linux runner используйте `scripts/restart.sh` и `scripts/stop-one.sh` после `chmod +x scripts/restart.sh scripts/stop-one.sh`. Smoke и отказовые сценарии создают синтетические данные. Для остановки без потери данных: `docker compose --env-file .env down`. Для намеренного удаления тестовой БД и сессий: `docker compose --env-file .env down -v`.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Путь запроса: клиент → Nginx LB → две API-реплики → общие PostgreSQL и Redis. PostgreSQL хранит пользователей, чаты, членство, сообщения, сессии и записи идемпотентности. Redis — общий TTL-cache активных сессий; чтение при cache miss обращается к PostgreSQL. Пароли — scrypt, токены — случайные opaque bearer.
|
||||
|
||||
## Доказательства критериев
|
||||
Из lab2 перенесены исходники REST API, схема и версии миграций, auth/store и Dockerfile/runtime dependencies. Новая миграция `003_message_idempotency` создаёт уникальную область `(user_id, method, chat_path, key)`. В одной PostgreSQL-транзакции реплика резервирует ключ, создаёт сообщение и фиксирует ссылку; конкурентный запрос блокируется уникальным индексом и возвращает прежнее сообщение либо 409 при другом JSON. Канонизация учитывает отсутствие `attachment_ids` как пустой список. Ключи сохраняются дольше минимальных 24 часов, автоматическая очистка не настроена.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
Миграции выполняются отдельным одноразовым Compose service `migrate` до запуска API. Nginx распределяет запросы между двумя API-сервисами и исключает недоступный backend при отказе. Для backend используются имена Docker DNS, общими остаются PostgreSQL и Redis.
|
||||
|
||||
| Роль | Compose service / версия | Назначение |
|
||||
| --- | --- | --- |
|
||||
| LB | `lb`, nginx:1.27.5-alpine | Публичная точка входа на host port 8080. |
|
||||
| API ×2 | `api1` и `api2`, локально собранный `mtusi-messenger-api:3.0.0` на Python 3.11.10 | Взаимозаменяемые экземпляры REST API, доступные LB. |
|
||||
| Миграция | одноразовый `migrate`, `mtusi-messenger-api:3.0.0` | Выполняет `alembic upgrade head` до запуска API. |
|
||||
| PostgreSQL | `db`, postgres:16.4-alpine | Общая БД, данные в named volume `postgres_data`. |
|
||||
| Session cache | `redis`, redis:7.4.2-alpine | Общий cache с AOF `appendfsync always` и named volume `redis_data`. |
|
||||
|
||||
`POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_DB` используются для PostgreSQL и `DATABASE_URL` API/migrate. `APP_PORT` публикует LB; `SESSION_TTL_SECONDS` задаёт TTL. Секрет PostgreSQL передаётся через локальный игнорируемый `.env`.
|
||||
|
||||
## Доказательства критериев уровня 3
|
||||
|
||||
| Критерий | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Структура skeleton | `python scripts/check_contract.py` | PASS. | `evidence/implementation-checks.txt` |
|
||||
| Контракт API через LB | `python tests/smoke.py --base-url http://localhost:8080` | PASS: сценарий публичного API lab3. | `evidence/runtime-checks.txt` |
|
||||
| Общие сессии и разные реплики | Smoke через LB | PASS: запросы одного пользователя обслуживаются API без повторного входа; виден `X-Instance-Id`. | `evidence/runtime-checks.txt` |
|
||||
| Persistence | `python tests/resilience.py --mode persistence --action-script scripts/restart.bat ...` | PASS: прежняя сессия и подтверждённые данные доступны после перезапуска сервисов с сохранением volumes. | `evidence/runtime-checks.txt`, `evidence/persistence-actions.txt` |
|
||||
| Failover API | `python tests/resilience.py --mode failover --action-script scripts/stop-one.bat ...` | PASS: другая реплика продолжает чтение и запись, подтверждённые сообщения сохранены, дубли не появились. | `evidence/runtime-checks.txt`, `evidence/failover-actions.txt` |
|
||||
| Конкурентная идемпотентность | `python tests/concurrent_idempotency.py --base-url http://localhost:8080` | PASS: конкурентный повтор одного ключа создаёт одну запись. | `evidence/runtime-checks.txt` |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
После failover восстановить остановленный сервис по записи в `evidence/failover-actions.txt`, например `docker compose --env-file .env start api2`, и дождаться ответа через LB.
|
||||
|
||||
## Эксперименты и отказы
|
||||
## Эксперименты и ограничения
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Failover-сценарий проверяет отказ одной выбранной API-реплики через LB: перед воздействием наблюдаются как минимум две реплики, а операции продолжаются через оставшуюся. Hook записывает соответствие `TARGET_INSTANCE_ID` и Compose-сервиса. Smoke, тест идемпотентности, persistence и failover запускаются отдельными командами.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Один Docker host, один LB и одна PostgreSQL — точки отказа; сценарий проверяет отказ одного API-процесса, а не потерю узла/хоста или БД.
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
[alembic]
|
||||
script_location = alembic
|
||||
sqlalchemy.url = postgresql://postgres:postgres@db:5432/messenger
|
||||
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
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
|
||||
@@ -0,0 +1,46 @@
|
||||
from logging.config import fileConfig
|
||||
from sqlalchemy import engine_from_config
|
||||
from alembic import context
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from src.database import Base, DATABASE_URL
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
url = context.config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=None,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,22 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Initial schema
|
||||
|
||||
Revision ID: 001
|
||||
Revises:
|
||||
Create Date: 2026-10-12
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '001'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.String(), 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=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_users_username', 'users', ['username'], unique=True)
|
||||
|
||||
op.create_table('sessions',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('token', sa.String(length=255), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_sessions_token', 'sessions', ['token'], unique=True)
|
||||
|
||||
op.create_table('chats',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('title', sa.String(length=100), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('chat_members',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('user_id', sa.String(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
op.create_table('messages',
|
||||
sa.Column('id', sa.String(), nullable=False),
|
||||
sa.Column('chat_id', sa.String(), nullable=False),
|
||||
sa.Column('sender_id', sa.String(), nullable=False),
|
||||
sa.Column('text', sa.Text(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['chat_id'], ['chats.id']),
|
||||
sa.ForeignKeyConstraint(['sender_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.drop_table('messages')
|
||||
op.drop_table('chat_members')
|
||||
op.drop_table('chats')
|
||||
op.drop_index('ix_sessions_token', table_name='sessions')
|
||||
op.drop_table('sessions')
|
||||
op.drop_index('ix_users_username', table_name='users')
|
||||
op.drop_table('users')
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Add membership uniqueness and the keyset history index.
|
||||
|
||||
Revision ID: 002
|
||||
Revises: 001
|
||||
Create Date: 2026-10-14
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '002'
|
||||
down_revision = '001'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Older schemas allowed nullable timestamps. Backfill before tightening the
|
||||
# constraint so the migration also works on an existing populated database.
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.execute(sa.text(f'UPDATE {table} SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL'))
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=False)
|
||||
|
||||
op.create_unique_constraint('uq_chat_member', 'chat_members', ['chat_id', 'user_id'])
|
||||
op.create_index('ix_chat_members_user_chat', 'chat_members', ['user_id', 'chat_id'])
|
||||
op.drop_index('ix_messages_chat_created', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index('ix_messages_chat_created_id', table_name='messages')
|
||||
op.create_index('ix_messages_chat_created', 'messages', ['chat_id', 'created_at'])
|
||||
op.drop_index('ix_chat_members_user_chat', table_name='chat_members')
|
||||
op.drop_constraint('uq_chat_member', 'chat_members', type_='unique')
|
||||
for table in ('users', 'sessions', 'chats', 'messages'):
|
||||
op.alter_column(table, 'created_at', existing_type=sa.DateTime(timezone=True), nullable=True)
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Persist message idempotency claims in the shared PostgreSQL database."""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = "003_message_idempotency"
|
||||
down_revision = "002"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
"message_idempotency",
|
||||
sa.Column("id", sa.String(), nullable=False),
|
||||
sa.Column("user_id", sa.String(), nullable=False),
|
||||
sa.Column("method", sa.String(length=8), nullable=False),
|
||||
sa.Column("chat_path", sa.String(length=80), nullable=False),
|
||||
sa.Column("key", sa.String(length=128), nullable=False),
|
||||
sa.Column("request_hash", sa.String(length=64), nullable=False),
|
||||
sa.Column("message_id", sa.String(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(["message_id"], ["messages.id"]),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("user_id", "method", "chat_path", "key", name="uq_idempotency_scope"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table("message_idempotency")
|
||||
@@ -0,0 +1,75 @@
|
||||
services:
|
||||
api1:
|
||||
build: .
|
||||
image: mtusi-messenger-api:3.0.0
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_started
|
||||
redis:
|
||||
condition: service_started
|
||||
restart: unless-stopped
|
||||
|
||||
api2:
|
||||
build: .
|
||||
image: mtusi-messenger-api:3.0.0
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_started
|
||||
redis:
|
||||
condition: service_started
|
||||
restart: unless-stopped
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
image: mtusi-messenger-api:3.0.0
|
||||
command: ["alembic", "upgrade", "head"]
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?Create a local .env first}@db:5432/${POSTGRES_DB:-messenger}
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_started
|
||||
restart: "no"
|
||||
|
||||
lb:
|
||||
image: nginx:1.27.5-alpine
|
||||
ports:
|
||||
- "${APP_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
depends_on:
|
||||
- api1
|
||||
- api2
|
||||
restart: unless-stopped
|
||||
|
||||
db:
|
||||
image: postgres:16.4-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Create a local .env first}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "always"]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
python - <<'PY'
|
||||
import os, time
|
||||
from sqlalchemy import create_engine, text
|
||||
url = os.environ["DATABASE_URL"]
|
||||
engine = create_engine(url, connect_args={"connect_timeout": 3})
|
||||
for attempt in range(60):
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
break
|
||||
except Exception:
|
||||
if attempt == 59:
|
||||
raise
|
||||
time.sleep(1)
|
||||
PY
|
||||
exec "$@"
|
||||
@@ -0,0 +1 @@
|
||||
2026-10-27 00:34:02 +03:00 target=e84a6cc7-57a8-4b85-9007-94854ac55397 service=api2 container=/mtusi2026lab3-fork-api2-1
|
||||
@@ -0,0 +1,9 @@
|
||||
# Implementation checks
|
||||
|
||||
Date: 2026-10-27 (Europe/Moscow).
|
||||
|
||||
- `scripts/check_contract.py` — PASS: offline skeleton structure, local refs and Python syntax.
|
||||
- `scripts/validate_openapi.py` — PASS: OpenAPI 3.1 and JSON Schema.
|
||||
- `python -m compileall -q src alembic scripts tests` — PASS.
|
||||
- `docker compose --env-file .env config --quiet` — PASS.
|
||||
- Runtime smoke, concurrent idempotency, persistence and one failover scenario — PASS; details in `runtime-checks.txt`.
|
||||
@@ -0,0 +1,11 @@
|
||||
[2026-10-27 00:33:51 +03:00]
|
||||
before service=db container=/mtusi2026lab3-fork-db-1 started_at=2026-10-26T21:32:43.19822304Z
|
||||
before service=redis container=/mtusi2026lab3-fork-redis-1 started_at=2026-10-26T21:32:43.234502434Z
|
||||
before service=api1 container=/mtusi2026lab3-fork-api1-1 started_at=2026-10-26T21:32:47.662735904Z
|
||||
before service=api2 container=/mtusi2026lab3-fork-api2-1 started_at=2026-10-26T21:32:47.639547191Z
|
||||
before service=lb container=/mtusi2026lab3-fork-lb-1 started_at=2026-10-26T21:32:48.230941054Z
|
||||
after service=db container=/mtusi2026lab3-fork-db-1 started_at=2026-10-26T21:33:44.87798288Z
|
||||
after service=redis container=/mtusi2026lab3-fork-redis-1 started_at=2026-10-26T21:33:45.696353566Z
|
||||
after service=api1 container=/mtusi2026lab3-fork-api1-1 started_at=2026-10-26T21:33:45.732430678Z
|
||||
after service=api2 container=/mtusi2026lab3-fork-api2-1 started_at=2026-10-26T21:33:45.632728749Z
|
||||
after service=lb container=/mtusi2026lab3-fork-lb-1 started_at=2026-10-26T21:33:45.634017354Z
|
||||
@@ -0,0 +1,16 @@
|
||||
# Runtime verification
|
||||
|
||||
Test window: 2026-10-27 00:32–00:35 (Europe/Moscow).
|
||||
Docker Engine 26.1.4; Docker Compose v2.27.1-desktop.1; Python 3.11.5.
|
||||
Compose topology: Nginx LB, api1, api2, PostgreSQL, Redis. Test data is synthetic; credentials and tokens are not recorded.
|
||||
|
||||
- `docker compose --env-file .env config --quiet` — PASS.
|
||||
- `python scripts/check_contract.py` — PASS.
|
||||
- `python scripts/validate_openapi.py` — PASS.
|
||||
- `python -m compileall -q src alembic scripts tests` — PASS.
|
||||
- `python tests/smoke.py --base-url http://localhost:8080` — PASS via LB.
|
||||
- `python tests/concurrent_idempotency.py --base-url http://localhost:8080` — PASS: 32 concurrent identical requests produced one Message across 2 API instances; history contains one record.
|
||||
- `python tests/resilience.py --mode persistence --action-script scripts/restart.bat --base-url http://localhost:8080` — PASS: confirmed data and the existing session survived restart; actual container start times are in `persistence-actions.txt`.
|
||||
- `python tests/resilience.py --mode failover --action-script scripts/stop-one.bat --base-url http://localhost:8080` — PASS for api2: two instances observed before failure; first recovered write at 3.985 s (≤10-second level-3 requirement); confirmed data and session preserved.
|
||||
- Restored api2; `/health/ready` via LB and final smoke — PASS.
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
upstream api_backend {
|
||||
zone api_backend 64k;
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
server api1:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
server api2:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
keepalive 32;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
location / {
|
||||
proxy_pass http://api_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fastapi==0.109.0
|
||||
uvicorn[standard]==0.27.0
|
||||
pydantic==2.5.3
|
||||
sqlalchemy==2.0.25
|
||||
alembic==1.13.1
|
||||
psycopg2-binary==2.9.9
|
||||
redis==5.0.1
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
setlocal
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0restart.ps1"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,21 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
function Get-ContainerState($phase) {
|
||||
foreach ($service in @('db', 'redis', 'api1', 'api2', 'lb')) {
|
||||
$containers = docker compose --env-file .env ps -q $service
|
||||
if ($LASTEXITCODE -ne 0) { throw "Cannot list $service containers" }
|
||||
foreach ($container in $containers) {
|
||||
$name = docker inspect --format '{{.Name}}' $container
|
||||
$started = docker inspect --format '{{.State.StartedAt}}' $container
|
||||
"$phase service=$service container=$name started_at=$started"
|
||||
}
|
||||
}
|
||||
}
|
||||
$before = @(Get-ContainerState 'before')
|
||||
docker compose --env-file .env restart db redis api1 api2 lb | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
$after = @(Get-ContainerState 'after')
|
||||
$stamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss K'
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value "[$stamp]"
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value $before
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\persistence-actions.txt') -Value $after
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env restart db redis api1 api2 lb
|
||||
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
setlocal
|
||||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0stop-one.ps1"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,22 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-Location (Join-Path $PSScriptRoot '..')
|
||||
$target = $env:TARGET_INSTANCE_ID
|
||||
if ([string]::IsNullOrWhiteSpace($target)) { exit 2 }
|
||||
foreach ($service in @('api1', 'api2')) {
|
||||
$containers = docker compose --env-file .env ps -q $service
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
foreach ($container in $containers) {
|
||||
try {
|
||||
$instance = docker exec $container python -c "import urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2).headers['X-Instance-Id'])" 2>$null
|
||||
if ($LASTEXITCODE -eq 0 -and $instance.Trim() -eq $target) {
|
||||
$name = docker inspect --format '{{.Name}}' $container
|
||||
$stamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss K'
|
||||
Add-Content -Path (Join-Path $PSScriptRoot '..\evidence\failover-actions.txt') -Value "$stamp target=$target service=$service container=$name"
|
||||
docker stop --time 5 $container | Out-Null
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
} catch { }
|
||||
}
|
||||
}
|
||||
Write-Error 'No running API container matched TARGET_INSTANCE_ID'
|
||||
exit 1
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
target=${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}
|
||||
cd "$(dirname "$0")/.."
|
||||
for service in api1 api2; do
|
||||
for container in $(docker compose --env-file .env ps -q "$service"); do
|
||||
instance=$(docker exec "$container" python -c 'import urllib.request; r=urllib.request.urlopen("http://127.0.0.1:8080/health/live",timeout=2); print(r.headers["X-Instance-Id"])' 2>/dev/null || true)
|
||||
if [ "$instance" = "$target" ]; then
|
||||
docker stop --time 5 "$container" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
done
|
||||
echo "No running API container matched TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1 @@
|
||||
from .main import app
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"""scrypt password KDF and opaque, cryptographically random bearer tokens."""
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
import secrets
|
||||
|
||||
SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
salt = secrets.token_bytes(16)
|
||||
digest = hashlib.scrypt(password.encode("utf-8"), salt=salt, n=2**14, r=8, p=1, dklen=32)
|
||||
return "scrypt$16384$8$1$" + base64.urlsafe_b64encode(salt + digest).decode("ascii")
|
||||
|
||||
|
||||
def verify_password(password: str, encoded: str) -> bool:
|
||||
try:
|
||||
scheme, n, r, p, raw = encoded.split("$", 4)
|
||||
if scheme != "scrypt":
|
||||
return False
|
||||
data = base64.urlsafe_b64decode(raw.encode("ascii"))
|
||||
salt, expected = data[:16], data[16:]
|
||||
actual = hashlib.scrypt(password.encode("utf-8"), salt=salt, n=int(n), r=int(r), p=int(p), dklen=len(expected))
|
||||
return hmac.compare_digest(actual, expected)
|
||||
except (ValueError, TypeError):
|
||||
return False
|
||||
|
||||
|
||||
def new_token() -> str:
|
||||
return secrets.token_urlsafe(32)
|
||||
@@ -0,0 +1,81 @@
|
||||
"""PostgreSQL schema and bounded connection pool."""
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import (Column, DateTime, ForeignKey, Index, String, Text,
|
||||
UniqueConstraint, create_engine)
|
||||
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:postgres@db:5432/messenger")
|
||||
engine = create_engine(DATABASE_URL, pool_size=5, max_overflow=5, pool_timeout=5,
|
||||
pool_pre_ping=True, connect_args={"connect_timeout": 5,
|
||||
"options": "-c statement_timeout=5000"})
|
||||
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
||||
Base = declarative_base()
|
||||
now = lambda: datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
username = Column(String(32), nullable=False)
|
||||
display_name = Column(String(100), nullable=False)
|
||||
password_hash = Column(String(255), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (Index("ix_users_username", "username", unique=True),)
|
||||
|
||||
|
||||
class Session(Base):
|
||||
__tablename__ = "sessions"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
token = Column(String(255), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
user = relationship("User")
|
||||
__table_args__ = (Index("ix_sessions_token", "token", unique=True),)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
title = Column(String(100), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
members = relationship("ChatMember", back_populates="chat", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
chat = relationship("Chat", back_populates="members")
|
||||
user = relationship("User")
|
||||
__table_args__ = (UniqueConstraint("chat_id", "user_id", name="uq_chat_member"),
|
||||
Index("ix_chat_members_user_chat", "user_id", "chat_id"))
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
chat_id = Column(String, ForeignKey("chats.id"), nullable=False)
|
||||
sender_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
text = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
chat = relationship("Chat")
|
||||
sender = relationship("User")
|
||||
__table_args__ = (Index("ix_messages_chat_created_id", "chat_id", "created_at", "id"),)
|
||||
|
||||
|
||||
class MessageIdempotency(Base):
|
||||
__tablename__ = "message_idempotency"
|
||||
id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
method = Column(String(8), nullable=False)
|
||||
chat_path = Column(String(80), nullable=False)
|
||||
key = Column(String(128), nullable=False)
|
||||
request_hash = Column(String(64), nullable=False)
|
||||
message_id = Column(String, ForeignKey("messages.id"), nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), nullable=False, default=now)
|
||||
__table_args__ = (UniqueConstraint("user_id", "method", "chat_path", "key",
|
||||
name="uq_idempotency_scope"),)
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
"""Lab 2 REST API: PostgreSQL-backed records and opaque bearer sessions."""
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
from datetime import timezone
|
||||
from fastapi import FastAPI, Request, Query
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from .database import SessionLocal
|
||||
from .models import CreateUser, CreateChat, CreateMessage
|
||||
from .store import store, cursor_for_chat, cursor_for_message
|
||||
from . import session_cache
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(self, status, code, message, headers=None):
|
||||
self.status, self.code, self.message, self.headers = status, code, message, headers or {}
|
||||
|
||||
|
||||
app = FastAPI(title="MTUSI Messenger", version="3.0.0")
|
||||
INSTANCE_ID = str(uuid.uuid4())
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_headers(request: Request, call_next):
|
||||
request.state.request_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return response
|
||||
|
||||
|
||||
@app.exception_handler(AppError)
|
||||
async def app_error_handler(request, exc):
|
||||
headers = {"X-Request-Id": request.state.request_id, **exc.headers}
|
||||
headers["X-Instance-Id"] = INSTANCE_ID
|
||||
return JSONResponse(status_code=exc.status, content={"error":{"code":exc.code,"message":exc.message,"request_id":request.state.request_id}}, headers=headers)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_handler(request, exc):
|
||||
return JSONResponse(status_code=400, content={"error":{"code":"invalid_request","message":"Invalid request","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
@app.exception_handler(SQLAlchemyError)
|
||||
async def database_error_handler(request, exc):
|
||||
return JSONResponse(status_code=503, content={"error":{"code":"unavailable","message":"Database unavailable","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def internal_handler(request, exc):
|
||||
return JSONResponse(status_code=500, content={"error":{"code":"internal_error","message":"Internal server error","request_id":request.state.request_id}}, headers={"X-Request-Id":request.state.request_id,"X-Instance-Id":INSTANCE_ID})
|
||||
|
||||
|
||||
def stamp(value):
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
|
||||
|
||||
|
||||
def user_json(user):
|
||||
return {"id":user.id,"username":user.username,"display_name":user.display_name,"created_at":stamp(user.created_at)}
|
||||
|
||||
|
||||
def auth_user(request):
|
||||
header = request.headers.get("Authorization", "")
|
||||
if not header.startswith("Bearer ") or not header[7:]:
|
||||
raise AppError(401,"unauthorized","Authentication required",{"WWW-Authenticate":'Bearer realm="messenger"'})
|
||||
try:
|
||||
found = store.get_session_by_token(header[7:])
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Authentication store unavailable")
|
||||
if not found:
|
||||
raise AppError(401,"unauthorized","Invalid or expired session",{"WWW-Authenticate":'Bearer realm="messenger"'})
|
||||
return found[0]
|
||||
|
||||
|
||||
def chat_json(chat):
|
||||
return {"id":chat.id,"title":chat.title,"member_ids":[m.user_id for m in chat.members],"created_at":stamp(chat.created_at)}
|
||||
|
||||
|
||||
def validate_uuid(value):
|
||||
try:
|
||||
uuid.UUID(value)
|
||||
except (ValueError, AttributeError):
|
||||
raise AppError(400,"invalid_request","Invalid UUID")
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
async def live():
|
||||
return {"status":"ok"}
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def ready():
|
||||
try:
|
||||
with SessionLocal() as db:
|
||||
db.execute(text("SELECT 1"))
|
||||
session_cache.ping()
|
||||
return {"status":"ok"}
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.post("/api/v1/users", status_code=201)
|
||||
async def register(data: CreateUser):
|
||||
if not data.display_name.strip() or not 12 <= len(data.password) <= 128:
|
||||
raise AppError(400,"invalid_request","Invalid user fields")
|
||||
try:
|
||||
return user_json(store.add_user(data.username,data.display_name,data.password))
|
||||
except ValueError as exc:
|
||||
if str(exc) == "USERNAME_EXISTS":
|
||||
raise AppError(409,"conflict","Username already exists")
|
||||
raise AppError(400,"invalid_request","Invalid user fields")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
||||
async def login(request: Request):
|
||||
header = request.headers.get("Authorization", "")
|
||||
challenge = {'WWW-Authenticate':'Basic realm="messenger", charset="UTF-8"'}
|
||||
if not header.startswith("Basic "):
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
try:
|
||||
decoded = base64.b64decode(header[6:], validate=True).decode("utf-8")
|
||||
username, password = decoded.split(":", 1)
|
||||
except (binascii.Error, UnicodeError, ValueError):
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
try:
|
||||
user = store.verify_user_password(username,password)
|
||||
if not user:
|
||||
raise AppError(401,"unauthorized","Invalid credentials",challenge)
|
||||
session = store.create_session(user.id)
|
||||
return {"token":session.token,"token_type":"Bearer","expires_at":stamp(session.expires_at),"user":user_json(user)}
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||
async def logout(request: Request):
|
||||
auth_user(request)
|
||||
try:
|
||||
store.delete_session(request.headers["Authorization"][7:])
|
||||
except session_cache.CacheUnavailable:
|
||||
raise AppError(503,"unavailable","Session cache unavailable")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Authentication store unavailable")
|
||||
return None
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
async def me(request: Request):
|
||||
user_id = auth_user(request)
|
||||
user = store.get_user_by_id(user_id)
|
||||
if not user:
|
||||
raise AppError(401,"unauthorized","Invalid session",{'WWW-Authenticate':'Bearer realm="messenger"'})
|
||||
return user_json(user)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats", status_code=201)
|
||||
async def create_chat(data: CreateChat, request: Request):
|
||||
user_id = auth_user(request)
|
||||
if not data.title.strip():
|
||||
raise AppError(400,"invalid_request","Invalid title")
|
||||
try:
|
||||
return chat_json(store.create_chat(data.title,data.member_ids,user_id))
|
||||
except ValueError as exc:
|
||||
if str(exc)=="USER_NOT_FOUND":
|
||||
raise AppError(400,"invalid_request","Unknown member")
|
||||
raise AppError(400,"invalid_request","Invalid members")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
async def list_chats(request: Request, limit: int = Query(50,ge=1,le=100), cursor: str|None=None):
|
||||
user_id=auth_user(request)
|
||||
try:
|
||||
rows=store.get_user_chats(user_id,cursor,limit+1)
|
||||
except ValueError:
|
||||
raise AppError(400,"invalid_request","Invalid cursor")
|
||||
more=len(rows)>limit
|
||||
rows=rows[:limit]
|
||||
return {"items":[chat_json(c) for c in rows],"next_cursor":cursor_for_chat(rows[-1]) if more else None}
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
async def get_chat(chat_id: str, request: Request):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if not store.is_chat_member(chat_id,user_id):
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
chat=store.get_chat(chat_id)
|
||||
if not chat:
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
return chat_json(chat)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
async def create_message(chat_id: str, data: CreateMessage, request: Request):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if data.text is None or len(data.text)>4000 or not data.text.strip() or data.attachment_ids:
|
||||
raise AppError(400,"invalid_request","Invalid message text")
|
||||
key = request.headers.get("Idempotency-Key", "")
|
||||
if not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key, flags=re.ASCII):
|
||||
raise AppError(400,"invalid_request","A valid Idempotency-Key is required")
|
||||
canonical = json.dumps({"text":data.text,"attachment_ids":[]}, ensure_ascii=False,
|
||||
sort_keys=True, separators=(",", ":"))
|
||||
request_hash = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
try:
|
||||
msg=store.add_message_idempotent(chat_id,user_id,data.text,[],key,request_hash)
|
||||
return {"id":msg.id,"chat_id":msg.chat_id,"sender_id":msg.sender_id,"text":msg.text,"created_at":stamp(msg.created_at)}
|
||||
except ValueError as exc:
|
||||
if str(exc) == "IDEMPOTENCY_CONFLICT":
|
||||
raise AppError(409,"conflict","Idempotency key was used with a different request")
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
except SQLAlchemyError:
|
||||
raise AppError(503,"unavailable","Database unavailable")
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
async def list_messages(chat_id: str, request: Request, limit: int=Query(50,ge=1,le=100), cursor: str|None=None):
|
||||
user_id=auth_user(request)
|
||||
validate_uuid(chat_id)
|
||||
if not store.is_chat_member(chat_id,user_id):
|
||||
raise AppError(404,"not_found","Chat not found")
|
||||
try:
|
||||
rows=store.get_messages(chat_id,cursor,limit+1)
|
||||
except ValueError:
|
||||
raise AppError(400,"invalid_request","Invalid cursor")
|
||||
more=len(rows)>limit
|
||||
rows=rows[:limit]
|
||||
items=[{"id":m.id,"chat_id":m.chat_id,"sender_id":m.sender_id,"text":m.text,"created_at":stamp(m.created_at)} for m in rows]
|
||||
return {"items":items,"next_cursor":cursor_for_message(rows[-1]) if more else None}
|
||||
@@ -0,0 +1,28 @@
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import List
|
||||
|
||||
|
||||
# ==================== REQUEST MODELS ====================
|
||||
|
||||
class CreateUser(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", str_strip_whitespace=False)
|
||||
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)
|
||||
|
||||
|
||||
class CreateChat(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
title: str = Field(..., min_length=1, max_length=100)
|
||||
member_ids: List[str] = Field(..., min_length=1, max_length=100)
|
||||
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
text: str = Field(..., min_length=1, max_length=4000)
|
||||
attachment_ids: List[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Redis TTL cache for active sessions; PostgreSQL remains authoritative."""
|
||||
import os
|
||||
import socket
|
||||
from urllib.parse import urlparse
|
||||
from redis import Redis
|
||||
from redis.backoff import NoBackoff
|
||||
from redis.exceptions import RedisError
|
||||
from redis.retry import Retry
|
||||
|
||||
CACHE_URL = os.getenv("CACHE_URL", "redis://redis:6379/0")
|
||||
_cache_address = urlparse(CACHE_URL)
|
||||
_cache_host = _cache_address.hostname or "redis"
|
||||
_cache_port = _cache_address.port or (6380 if _cache_address.scheme == "rediss" else 6379)
|
||||
try:
|
||||
_cache_ips = tuple(dict.fromkeys(item[4][0] for item in socket.getaddrinfo(
|
||||
_cache_host, _cache_port, type=socket.SOCK_STREAM)))
|
||||
except OSError:
|
||||
_cache_ips = (_cache_host,)
|
||||
client = Redis.from_url(CACHE_URL, decode_responses=True,
|
||||
socket_connect_timeout=0.2, socket_timeout=0.2,
|
||||
health_check_interval=15, retry=Retry(NoBackoff(), 0))
|
||||
|
||||
|
||||
class CacheUnavailable(Exception):
|
||||
"""The required session cache cannot be reached within its short timeout."""
|
||||
|
||||
|
||||
def _check_port():
|
||||
# Fail fast when the Redis container is stopped. redis-py may otherwise
|
||||
# spend several seconds cycling through connection retries on Docker DNS.
|
||||
try:
|
||||
for address in _cache_ips:
|
||||
try:
|
||||
with socket.create_connection((address, _cache_port), timeout=0.2):
|
||||
return
|
||||
except OSError:
|
||||
continue
|
||||
raise OSError("Redis is not accepting connections")
|
||||
except OSError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def get(token: str):
|
||||
try:
|
||||
_check_port()
|
||||
return client.get("session:" + token)
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def put(token: str, user_id: str, ttl_seconds: int):
|
||||
try:
|
||||
_check_port()
|
||||
client.set("session:" + token, user_id, ex=max(1, ttl_seconds))
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def delete(token: str):
|
||||
try:
|
||||
_check_port()
|
||||
client.delete("session:" + token)
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
|
||||
|
||||
def ping():
|
||||
try:
|
||||
_check_port()
|
||||
return bool(client.ping())
|
||||
except RedisError as exc:
|
||||
raise CacheUnavailable from exc
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
"""Small repository layer; each operation owns and closes its DB transaction."""
|
||||
import base64
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timezone, timedelta
|
||||
import math
|
||||
from sqlalchemy import and_, or_, select, update
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from .database import User, Session as SessionRow, Chat, ChatMember, Message, MessageIdempotency, SessionLocal
|
||||
from .auth import hash_password, verify_password, new_token, SESSION_TTL_SECONDS
|
||||
from . import session_cache
|
||||
|
||||
|
||||
class Store:
|
||||
def add_user(self, username, display_name, password):
|
||||
with SessionLocal() as db:
|
||||
user = User(username=username, display_name=display_name, password_hash=hash_password(password))
|
||||
db.add(user)
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
raise ValueError("USERNAME_EXISTS")
|
||||
db.refresh(user)
|
||||
return user
|
||||
|
||||
def get_user_by_id(self, user_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(User).filter(User.id == user_id).first()
|
||||
|
||||
def verify_user_password(self, username, password):
|
||||
with SessionLocal() as db:
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
# A fixed dummy hash is avoided here; the public contract only requires
|
||||
# identical response status and challenge for unknown users/bad passwords.
|
||||
if not user or not verify_password(password, user.password_hash):
|
||||
return None
|
||||
return user
|
||||
|
||||
def create_session(self, user_id):
|
||||
with SessionLocal() as db:
|
||||
row = SessionRow(token=new_token(), user_id=user_id,
|
||||
expires_at=datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL_SECONDS))
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
try:
|
||||
session_cache.put(row.token, row.user_id, SESSION_TTL_SECONDS)
|
||||
except session_cache.CacheUnavailable:
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
raise
|
||||
return row
|
||||
|
||||
def get_session_by_token(self, token):
|
||||
cached_user_id = session_cache.get(token)
|
||||
if cached_user_id:
|
||||
return (cached_user_id, None)
|
||||
with SessionLocal() as db:
|
||||
row = db.query(SessionRow).filter(SessionRow.token == token,
|
||||
SessionRow.expires_at > datetime.now(timezone.utc)).first()
|
||||
if not row:
|
||||
return None
|
||||
remaining = math.ceil((row.expires_at - datetime.now(timezone.utc)).total_seconds())
|
||||
session_cache.put(row.token, row.user_id, remaining)
|
||||
return (row.user_id, row.expires_at)
|
||||
|
||||
def delete_session(self, token):
|
||||
# Require successful cache invalidation before reporting a completed logout.
|
||||
session_cache.delete(token)
|
||||
with SessionLocal() as db:
|
||||
row = db.query(SessionRow).filter(SessionRow.token == token).first()
|
||||
if row:
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def create_chat(self, title, member_ids, creator_id):
|
||||
if creator_id not in member_ids:
|
||||
raise ValueError("CREATOR_NOT_MEMBER")
|
||||
if len(member_ids) != len(set(member_ids)):
|
||||
raise ValueError("DUPLICATE_MEMBER")
|
||||
with SessionLocal() as db:
|
||||
users = db.query(User.id).filter(User.id.in_(member_ids)).all()
|
||||
if len(users) != len(member_ids):
|
||||
raise ValueError("USER_NOT_FOUND")
|
||||
chat = Chat(title=title)
|
||||
chat.members = [ChatMember(user_id=mid) for mid in member_ids]
|
||||
db.add(chat)
|
||||
db.commit()
|
||||
db.refresh(chat)
|
||||
list(chat.members)
|
||||
return chat
|
||||
|
||||
def get_chat(self, chat_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(Chat).options(selectinload(Chat.members)).filter(Chat.id == chat_id).first()
|
||||
|
||||
def get_user_chats(self, user_id, cursor=None, limit=50):
|
||||
with SessionLocal() as db:
|
||||
query = db.query(Chat).options(selectinload(Chat.members)).join(ChatMember).filter(ChatMember.user_id == user_id)
|
||||
if cursor:
|
||||
decoded = decode_cursor(cursor, "chats")
|
||||
query = query.filter(or_(Chat.created_at > decoded[0], and_(Chat.created_at == decoded[0], Chat.id > decoded[1])))
|
||||
rows = query.order_by(Chat.created_at, Chat.id).limit(limit).all()
|
||||
for row in rows:
|
||||
list(row.members)
|
||||
return rows
|
||||
|
||||
def is_chat_member(self, chat_id, user_id):
|
||||
with SessionLocal() as db:
|
||||
return db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id).first() is not None
|
||||
|
||||
def add_message(self, chat_id, sender_id, text):
|
||||
with SessionLocal() as db:
|
||||
if not db.query(Chat.id).filter(Chat.id == chat_id).first():
|
||||
raise ValueError("CHAT_NOT_FOUND")
|
||||
if not db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id, ChatMember.user_id == sender_id).first():
|
||||
raise ValueError("NOT_MEMBER")
|
||||
msg = Message(chat_id=chat_id, sender_id=sender_id, text=text)
|
||||
db.add(msg)
|
||||
db.commit()
|
||||
db.refresh(msg)
|
||||
return msg
|
||||
|
||||
def add_message_idempotent(self, chat_id, sender_id, text, attachment_ids, key, request_hash):
|
||||
"""Claim a scoped key and create its message in the same DB transaction."""
|
||||
with SessionLocal() as db:
|
||||
if not db.query(ChatMember.id).filter(ChatMember.chat_id == chat_id,
|
||||
ChatMember.user_id == sender_id).first():
|
||||
raise ValueError("CHAT_NOT_FOUND")
|
||||
scope = dict(user_id=sender_id, method="POST", chat_path=f"/api/v1/chats/{chat_id}/messages", key=key)
|
||||
claim = insert(MessageIdempotency).values(**scope, request_hash=request_hash,
|
||||
message_id=None).on_conflict_do_nothing(
|
||||
constraint="uq_idempotency_scope").returning(MessageIdempotency.id)
|
||||
claimed = db.execute(claim).scalar_one_or_none()
|
||||
if claimed is None:
|
||||
record = db.execute(select(MessageIdempotency).filter_by(**scope)).scalar_one()
|
||||
if record.request_hash != request_hash:
|
||||
raise ValueError("IDEMPOTENCY_CONFLICT")
|
||||
msg = db.get(Message, record.message_id)
|
||||
if msg is None:
|
||||
raise RuntimeError("Idempotency record has no message")
|
||||
return msg
|
||||
msg = Message(chat_id=chat_id, sender_id=sender_id, text=text)
|
||||
db.add(msg)
|
||||
db.flush()
|
||||
db.execute(update(MessageIdempotency).where(MessageIdempotency.id == claimed).values(message_id=msg.id))
|
||||
db.commit()
|
||||
db.refresh(msg)
|
||||
return msg
|
||||
|
||||
def get_messages(self, chat_id, cursor=None, limit=50):
|
||||
with SessionLocal() as db:
|
||||
query = db.query(Message).filter(Message.chat_id == chat_id)
|
||||
if cursor:
|
||||
created, message_id = decode_cursor(cursor, chat_id)
|
||||
query = query.filter(or_(Message.created_at > created,
|
||||
and_(Message.created_at == created, Message.id > message_id)))
|
||||
return query.order_by(Message.created_at, Message.id).limit(limit).all()
|
||||
|
||||
|
||||
def encode_cursor(resource, created_at, item_id):
|
||||
raw = json.dumps([resource, created_at.isoformat(), item_id], separators=(",", ":")).encode()
|
||||
return base64.urlsafe_b64encode(raw).decode().rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(cursor, resource):
|
||||
try:
|
||||
data = json.loads(base64.urlsafe_b64decode(cursor + "=" * (-len(cursor) % 4)))
|
||||
if data[0] != resource:
|
||||
raise ValueError
|
||||
return datetime.fromisoformat(data[1]), data[2]
|
||||
except Exception:
|
||||
raise ValueError("INVALID_CURSOR")
|
||||
|
||||
|
||||
def cursor_for_chat(chat):
|
||||
return encode_cursor("chats", chat.created_at, chat.id)
|
||||
|
||||
|
||||
def cursor_for_message(message):
|
||||
return encode_cursor(message.chat_id, message.created_at, message.id)
|
||||
|
||||
|
||||
store = Store()
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify concurrent same-key writes are shared across real API instances."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import uuid
|
||||
from urllib.parse import quote
|
||||
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Concurrent message idempotency across API replicas')
|
||||
client = Client(args.base_url, args.ca_file, timeout=10)
|
||||
alice, _bob, _eve, chat = client.bootstrap()
|
||||
text = 'Concurrent idempotency ' + uuid.uuid4().hex
|
||||
key = 'race-' + uuid.uuid4().hex
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
|
||||
def post_once(_):
|
||||
return client.call('POST', path, 201, actor=alice,
|
||||
body={'text': text},
|
||||
headers={'Idempotency-Key': key}, schema='Message')
|
||||
|
||||
with ThreadPoolExecutor(max_workers=16) as pool:
|
||||
responses = list(pool.map(post_once, range(32)))
|
||||
|
||||
messages = [response['json'] for response in responses]
|
||||
check(all(message == messages[0] for message in messages),
|
||||
'concurrent same-key requests returned different messages')
|
||||
instances = {response['headers']['X-Instance-Id'] for response in responses}
|
||||
check(len(instances) >= 2,
|
||||
'concurrent requests did not reach at least two API replicas')
|
||||
|
||||
history = []
|
||||
cursor = None
|
||||
while True:
|
||||
query = '?limit=100' + ('&cursor=' + quote(cursor, safe='') if cursor else '')
|
||||
page = client.call('GET', path + query, actor=alice, schema='MessagePage')['json']
|
||||
history.extend(page['items'])
|
||||
cursor = page['next_cursor']
|
||||
if cursor is None:
|
||||
break
|
||||
check(sum(message['text'] == text for message in history) == 1,
|
||||
'same-key concurrent write created more than one history item')
|
||||
print('PASS: 32 concurrent same-key POSTs returned one message across '
|
||||
+ str(len(instances)) + ' API instances; history contains one record.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user