diff --git a/.env.example b/.env.example index d32b9be0..37a32f0b 100644 --- a/.env.example +++ b/.env.example @@ -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 +# Локальные синтетические данные. Файл .env не добавляется в git. +POSTGRES_DB=messenger +POSTGRES_USER=messenger +POSTGRES_PASSWORD=change-me-local 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 +ENABLE_DEBUG_METRICS=1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..8a138fba --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.12.8-slim-bookworm +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app ./app +COPY migrations ./migrations +COPY scripts ./scripts +RUN useradd --create-home --uid 10001 appuser && chown -R appuser:appuser /app +USER appuser +EXPOSE 8080 +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"] diff --git a/REPORT.md b/REPORT.md index c2679005..5282ce18 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,74 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Борзов, БВТ2304 +- Целевая оценка: 5, трек 1 «Цена масштабирования» +- Commit/PR: https://gitea.privcloud.tech/mtusi2026/mtusi2026lab3/pulls/34 +- Commit: `89ef13702c86dc22621a227da322ed1dd37f9dbc` +- Использованные источники: материалы skeleton и документация FastAPI, psycopg, Redis, Docker Compose, Nginx. Код написан для учебного проекта с помощью ИИ и проверен локальными сценариями. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требуется Docker Desktop и Python 3.9+. Скопируйте `.env.example` в `.env` и задайте локальный пароль PostgreSQL. Запуск из корня репозитория: + +```sh +cp .env.example .env +docker compose --env-file .env up -d --build --wait +make check +make validate PYTHON=python3 +make test BASE_URL=http://localhost:8080 +``` + +Миграции запускаются один раз сервисом `migrate` до старта реплик. Состояние хранится в volumes `postgres_data` и `redis_data`. Для безопасного завершения используйте `docker compose down`; volumes при этом сохраняются. Намеренное удаление учебных данных: `docker compose down -v`. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Клиент обращается к `lb:8080`. Nginx распределяет запросы между `api-1`...`api-4`; порты реплик наружу не опубликованы. Обе реплики используют одну PostgreSQL и один Redis. PostgreSQL хранит пользователей, чаты, сообщения и таблицу `message_idempotency`, Redis хранит действующие сессии и кэш списка чатов. + +Из лабораторной 2 перенесены REST API, Argon2id, Bearer-сессии, миграции, keyset-пагинация и кэш. В этой работе добавлены `X-Instance-Id`, Nginx, две реплики, обязательный `Idempotency-Key` и сценарий остановки одной реплики. Миграции вынесены в отдельный одноразовый сервис, поэтому реплики не запускают их одновременно. + +Для одного ключа в одной транзакции сначала создаётся запись в `message_idempotency`, затем сообщение. Уникальный ключ PostgreSQL разрешает конкурентным запросам через разные реплики создать только одну запись. При том же теле возвращается сохранённое сообщение, при другом теле возвращается 409. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test BASE_URL=http://localhost:8080` | `PASS: lab3 public API scenario` | `tests/smoke.py` | +| Четыре наблюдаемые реплики | `python3 student-tests/idempotency.py` | В заголовках наблюдаются API-реплики; 8 конкурентных повторов дали один ID | `student-tests/idempotency.py` | +| Общая идемпотентность | `python3 student-tests/idempotency.py` | Повтор с тем же телом возвращает исходное сообщение, другое тело — 409 | `migrations/002_idempotency.sql` | +| Сохранность после перезапуска | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | `PASS: persistence preserved confirmed data and existing session` | `scripts/restart.sh` | +| Отказ каждой реплики | `python3 evidence/failover_all.py` | `api-1`...`api-4`: по 12 успешных записей после остановки каждой | `evidence/failover_all.json` | +| Rolling update | нагрузка `tests/load.py` параллельно с `scripts/rolling-update.sh` | 1970 успешных запросов, 0 ошибок за 25 секунд | `evidence/rolling_load.json`, `scripts/rolling-update.sh` | +| Таймауты и readiness | `curl /health/ready`, healthchecks Compose | LB исключает остановленную реплику; API readiness проверяет PostgreSQL и Redis | `compose.yaml`, `nginx/nginx.conf` | +| Ресурсные ограничения | `docker compose config --quiet` | Для API, LB, PostgreSQL и Redis заданы CPU/RAM limits | `compose.yaml` | +| Исследовательский трек 1 | `python3 evidence/scale_experiment.py` | Сравнение 1/2/4 реплик при 2 CPU и 1 GB, затем серия роста CPU для 4 реплик | `evidence/scale_results.json`, `evidence/scale_resource_results.json`, `evidence/scale_plot.png` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### Трек 1: цена масштабирования + +Гипотеза: увеличение числа API-реплик при неизменном суммарном CPU/RAM сначала увеличит пропускную способность, а затем упрётся в общую PostgreSQL и накладные расходы пулов соединений. + +Методика: Docker Desktop на локальном MacBook (8 CPU, 8 GB выделенной памяти Docker), PostgreSQL 16.4, Redis 7.4.2, Python 3.12.8. Для каждого замера прогревался стенд, затем выполнялся закрытый тест `tests/load.py` длительностью 15 секунд с concurrency 8. Сравнивались 1, 2 и 4 API-реплики при суммарных лимитах 2 CPU и 1 GB RAM. После этого для 4 реплик CPU последовательно увеличивался с 1 до 2 и 4, память оставалась 1 GB. Скрипт автоматически сохраняет исходные JSON-результаты и график. + +При фиксированных ресурсах получилась следующая серия: + +| Реплики | Успешные RPS | Ошибки | p50, мс | p95, мс | p99, мс | +| ---: | ---: | ---: | ---: | ---: | ---: | +| 1 | 398.948 | 0% | 18.576 | 29.126 | 55.308 | +| 2 | 491.139 | 0% | 12.152 | 24.869 | 113.716 | +| 4 | 517.044 | 0% | 9.247 | 50.660 | 91.127 | + +Рост с 2 до 4 реплик дал только около 5% RPS, а p95 вырос. Это показывает насыщение общей БД и LB: дополнительные API-процессы создают больше конкурентных обращений к одному пулу PostgreSQL, но не добавляют отдельного узла хранения. + +Подтверждённое улучшение получено изменением ресурсов для 4 реплик: при 1 CPU было 339.502 RPS, при 2 CPU — 269.722 RPS, при 4 CPU — 491.144 RPS. На этом ноутбуке рост до 4 CPU дал примерно 82% к серии с 2 CPU. Замер с 2 CPU оказался шумным, поэтому вывод делается по измеренной серии и сопровождается исходными данными. + +Пассивная проверка Nginx выполняется после двух ошибок соединения (`max_fails=2`, `fail_timeout=3s`), а активные healthchecks API идут раз в 3 секунды. Практическое время восстановления в отдельном сценарии для каждой реплики составило 2.4–3.4 секунды. Rolling update выполняет замену API по одной реплике и ждёт её readiness перед переходом к следующей. | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Остановка одной из `api-1`...`api-4` | LB исключает недоступный backend после ошибок соединения | Записи продолжаются через оставшиеся реплики, подтверждённая история не меняется | 2.4–3.4 с, потери данных нет | +| Перезапуск API, PostgreSQL и Redis | volumes сохраняют данные и сессии | Существующий токен снова принимается, сообщение читается | менее 90 с, потери данных нет | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Стенд выдерживает отказ одной API-реплики и перезапуск контейнеров при сохранённых volumes. Единственные PostgreSQL, Redis и Nginx остаются единичными точками отказа; потеря Docker-host или удаление volumes не входит в заявленную модель. Нагрузочный тест закрытый, поэтому его RPS включает клиентские задержки и не является универсальной производительностью сервиса. Серия с ростом CPU показывает заметный разброс, поэтому в отчёте сохранены все исходные JSON-файлы. diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..c781419d --- /dev/null +++ b/app/main.py @@ -0,0 +1,546 @@ +import base64 +import json +import os +import re +import secrets +import threading +import uuid +import hashlib +from datetime import datetime, timedelta, timezone + +import redis +from argon2 import PasswordHasher +from fastapi import FastAPI, Request +from fastapi.responses import JSONResponse, Response +from psycopg.rows import dict_row +from psycopg_pool import ConnectionPool + +DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://messenger:messenger@postgres:5432/messenger') +REDIS_URL = os.getenv('REDIS_URL', 'redis://redis:6379/0') +SESSION_TTL_SECONDS = int(os.getenv('SESSION_TTL_SECONDS', '3600')) +INSTANCE_ID = os.getenv('INSTANCE_ID', uuid.uuid4().hex[:12]) +DB_POOL_SIZE = int(os.getenv('DB_POOL_SIZE', '10')) + +password_hasher = PasswordHasher() +db_pool = ConnectionPool( + DATABASE_URL, min_size=1, max_size=DB_POOL_SIZE, timeout=5, + kwargs={'row_factory': dict_row}, open=False, +) +redis_client = redis.Redis.from_url( + REDIS_URL, + decode_responses=True, + socket_connect_timeout=1, + socket_timeout=1, + retry_on_timeout=False, +) +chat_cache_lock = threading.Lock() +chat_cache_sql_reads = 0 +invalidated_chat_caches = set() +app = FastAPI(title='MTUSI Messenger Lab 3', version='1.3.0') + + +class IdempotencyConflict(Exception): + pass + + +def utc_now(): + return datetime.now(timezone.utc) + + +def format_time(value): + return value.astimezone(timezone.utc).isoformat().replace('+00:00', 'Z') + + +def parse_uuid(value): + try: + return uuid.UUID(str(value)) + except (TypeError, ValueError) as exc: + raise ValueError('invalid uuid') from exc + + +def user_response(row): + return { + 'id': str(row['id']), 'username': row['username'], + 'display_name': row['display_name'], 'created_at': format_time(row['created_at']), + } + + +def chat_response(row, member_ids): + return { + 'id': str(row['id']), 'title': row['title'], + 'member_ids': [str(item) for item in member_ids], + 'created_at': format_time(row['created_at']), + } + + +def message_response(row): + return { + 'id': str(row['id']), 'chat_id': str(row['chat_id']), + 'sender_id': str(row['sender_id']), 'text': row['text'], + 'created_at': format_time(row['created_at']), + } + + +def error_response(request, status_code, code, message, extra_headers=None): + request_id = getattr(request.state, 'request_id', uuid.uuid4().hex) + headers = {'X-Request-Id': request_id, 'X-Instance-Id': INSTANCE_ID} + if extra_headers: + headers.update(extra_headers) + return JSONResponse( + status_code=status_code, + content={'error': {'code': code, 'message': message, 'request_id': request_id}}, + headers=headers, + ) + + +@app.middleware('http') +async def request_context(request: Request, call_next): + request.state.request_id = request.headers.get('X-Request-Id') or uuid.uuid4().hex + try: + response = await call_next(request) + except Exception: + return error_response(request, 500, 'internal_error', 'Внутренняя ошибка') + response.headers['X-Request-Id'] = request.state.request_id + response.headers['X-Instance-Id'] = INSTANCE_ID + return response + + +@app.on_event('startup') +def start_application(): + db_pool.open(wait=True) + with db_pool.connection() as connection: + connection.execute('SELECT 1') + + +@app.on_event('shutdown') +def stop_application(): + db_pool.close() + + +async def read_json_body(request): + try: + value = await request.json() + except Exception as exc: + raise ValueError('malformed json') from exc + if not isinstance(value, dict): + raise ValueError('request body must be an object') + return value + + +def require_fields(data, required): + if set(data) != required: + raise ValueError('unexpected or missing fields') + + +def get_bearer_session(request): + authorization = request.headers.get('Authorization', '') + if not authorization.startswith('Bearer '): + return None + token = authorization.removeprefix('Bearer ') + if len(token) < 40: + return None + try: + value = redis_client.get('session:' + token) + return json.loads(value) if value else None + except Exception: + return None + + +def get_current_user(request): + session = get_bearer_session(request) + if session is None: + return None + with db_pool.connection() as connection: + return connection.execute( + 'SELECT * FROM users WHERE id=%s', (session['user_id'],) + ).fetchone() + + +def require_current_user(request): + user = get_current_user(request) + if user is None: + return None, error_response( + request, 401, 'unauthorized', 'Требуется действующая Bearer-сессия', + {'WWW-Authenticate': 'Bearer realm="messenger"'}, + ) + return user, None + + +def is_chat_member(connection, chat_id, user_id): + row = connection.execute( + 'SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s', + (chat_id, user_id), + ).fetchone() + return row is not None + + +def make_cursor(scope, owner_id, row): + created_at = row['created_at'] + if isinstance(created_at, datetime): + created_at = format_time(created_at) + data = [scope, owner_id, created_at, str(row['id'])] + return base64.urlsafe_b64encode(json.dumps(data).encode()).decode().rstrip('=') + + +def read_cursor(cursor, expected_scope, expected_owner): + try: + padding = '=' * ((4 - len(cursor) % 4) % 4) + value = json.loads(base64.urlsafe_b64decode((cursor + padding).encode())) + if value[0] != expected_scope or value[1] != expected_owner: + raise ValueError + timestamp = datetime.fromisoformat(value[2].replace('Z', '+00:00')) + return timestamp, parse_uuid(value[3]) + except (IndexError, TypeError, ValueError, json.JSONDecodeError) as exc: + raise ValueError('invalid cursor') from exc + + +def page_chats(items, limit, cursor_data, owner_id): + if cursor_data: + items = [ + item for item in items + if ( + datetime.fromisoformat(item['created_at'].replace('Z', '+00:00')), + uuid.UUID(item['id']), + ) > cursor_data + ] + page = items[:limit] + next_cursor = make_cursor('chats', owner_id, page[-1]) if len(items) > limit and page else None + return {'items': page, 'next_cursor': next_cursor} + + +def parse_limit(value): + try: + limit = int(value) + except (TypeError, ValueError) as exc: + raise ValueError('invalid limit') from exc + if not 1 <= limit <= 100: + raise ValueError('invalid limit') + return limit + + +@app.get('/health/live') +def health_live(): + return {'status': 'ok'} + + +@app.get('/health/ready') +def health_ready(request: Request): + try: + with db_pool.connection() as connection: + connection.execute('SELECT 1') + redis_client.ping() + except Exception: + return error_response(request, 503, 'unavailable', 'Зависимость недоступна') + return {'status': 'ok'} + + +@app.get('/debug/cache-stats') +def cache_stats(request: Request): + if os.getenv('ENABLE_DEBUG_METRICS') != '1': + return error_response(request, 404, 'not_found', 'Ресурс не найден') + return {'chat_list_sql_reads': chat_cache_sql_reads} + + +@app.post('/api/v1/users', status_code=201) +async def create_user(request: Request): + try: + data = await read_json_body(request) + require_fields(data, {'username', 'display_name', 'password'}) + username, display_name, password = data['username'], data['display_name'], data['password'] + if not isinstance(username, str) or not re.fullmatch(r'[a-z0-9_]{3,32}', username): + raise ValueError('invalid username') + if not isinstance(display_name, str) or not display_name.strip() or not 1 <= len(display_name) <= 100: + raise ValueError('invalid display name') + if not isinstance(password, str) or not 12 <= len(password) <= 128: + raise ValueError('invalid password') + + user_id, created_at = uuid.uuid4(), utc_now() + password_hash = password_hasher.hash(password) + with db_pool.connection() as connection: + try: + connection.execute( + 'INSERT INTO users VALUES (%s,%s,%s,%s,%s)', + (user_id, username, display_name, password_hash, created_at), + ) + connection.commit() + except Exception as exc: + connection.rollback() + if 'unique' in str(exc).lower(): + return error_response(request, 409, 'conflict', 'Имя пользователя уже занято') + raise + return user_response({'id': user_id, 'username': username, 'display_name': display_name, 'created_at': created_at}) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверные поля запроса') + + +@app.post('/api/v1/auth/sessions', status_code=201) +def create_session(request: Request): + authorization = request.headers.get('Authorization', '') + try: + if not authorization.startswith('Basic '): + raise ValueError + credentials = base64.b64decode(authorization.removeprefix('Basic '), validate=True).decode() + username, password = credentials.split(':', 1) + with db_pool.connection() as connection: + user = connection.execute('SELECT * FROM users WHERE username=%s', (username,)).fetchone() + if user is None: + raise ValueError + password_hasher.verify(user['password_hash'], password) + except Exception: + return error_response( + request, 401, 'unauthorized', 'Неверные учётные данные', + {'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'}, + ) + + token = secrets.token_urlsafe(32) + expires_at = utc_now() + timedelta(seconds=SESSION_TTL_SECONDS) + redis_client.setex( + 'session:' + token, + SESSION_TTL_SECONDS, + json.dumps({'user_id': str(user['id']), 'expires_at': format_time(expires_at)}), + ) + return { + 'token': token, 'token_type': 'Bearer', + 'expires_at': format_time(expires_at), 'user': user_response(user), + } + + +@app.delete('/api/v1/auth/sessions/current', status_code=204) +def logout(request: Request): + if get_bearer_session(request) is None: + return error_response( + request, 401, 'unauthorized', 'Сессия недействительна', + {'WWW-Authenticate': 'Bearer realm="messenger"'}, + ) + token = request.headers['Authorization'].removeprefix('Bearer ') + redis_client.delete('session:' + token) + return Response(status_code=204) + + +@app.get('/api/v1/users/me') +def get_me(request: Request): + user, failure = require_current_user(request) + return failure if failure else user_response(user) + + +@app.post('/api/v1/chats', status_code=201) +async def create_chat(request: Request): + user, failure = require_current_user(request) + if failure: + return failure + try: + data = await read_json_body(request) + require_fields(data, {'title', 'member_ids'}) + title, member_values = data['title'], data['member_ids'] + if not isinstance(title, str) or not title.strip() or not 1 <= len(title) <= 100: + raise ValueError('invalid title') + if not isinstance(member_values, list) or not 1 <= len(member_values) <= 100: + raise ValueError('invalid members') + if len(set(member_values)) != len(member_values): + raise ValueError('duplicate members') + member_ids = [parse_uuid(value) for value in member_values] + if user['id'] not in member_ids: + raise ValueError('current user must be a member') + + chat_id, created_at = uuid.uuid4(), utc_now() + with db_pool.connection() as connection: + with connection.transaction(): + count = connection.execute( + 'SELECT count(*) AS n FROM users WHERE id = ANY(%s)', (member_ids,) + ).fetchone()['n'] + if count != len(member_ids): + raise ValueError('unknown member') + connection.execute('INSERT INTO chats VALUES (%s,%s,%s)', (chat_id, title, created_at)) + for member_id in member_ids: + connection.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id, member_id)) + for member_id in member_ids: + try: + redis_client.delete('chats:' + str(member_id)) + except Exception: + with chat_cache_lock: + invalidated_chat_caches.add('chats:' + str(member_id)) + return chat_response({'id': chat_id, 'title': title, 'created_at': created_at}, member_ids) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверные данные чата') + + +@app.get('/api/v1/chats') +def list_chats(request: Request, limit: str = '50', cursor: str | None = None): + global chat_cache_sql_reads + user, failure = require_current_user(request) + if failure: + return failure + try: + limit = parse_limit(limit) + except ValueError: + return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100') + + cursor_data = None + if cursor is not None: + if not cursor: + return error_response(request, 400, 'invalid_request', 'Неверный cursor') + try: + cursor_data = read_cursor(cursor, 'chats', str(user['id'])) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверный cursor') + + cache_key = 'chats:' + str(user['id']) + cache_needs_refresh = cache_key in invalidated_chat_caches + if not cache_needs_refresh: + try: + cached = redis_client.get(cache_key) + if cached: + return page_chats(json.loads(cached), limit, cursor_data, str(user['id'])) + except Exception: + pass + + with chat_cache_lock: + try: + cached = None if cache_needs_refresh else redis_client.get(cache_key) + except Exception: + cached = None + if cached: + chats = json.loads(cached) + else: + with db_pool.connection() as connection: + rows = connection.execute( + 'SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id ' + 'WHERE m.user_id=%s ORDER BY c.created_at,c.id', (user['id'],) + ).fetchall() + chats = [] + for row in rows: + members = connection.execute( + 'SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id', (row['id'],) + ).fetchall() + chats.append(chat_response(row, [item['user_id'] for item in members])) + chat_cache_sql_reads += 1 + try: + redis_client.setex(cache_key, 30, json.dumps(chats)) + invalidated_chat_caches.discard(cache_key) + except Exception: + pass + return page_chats(chats, limit, cursor_data, str(user['id'])) + + +@app.get('/api/v1/chats/{chat_id}') +def get_chat(chat_id: str, request: Request): + user, failure = require_current_user(request) + if failure: + return failure + try: + parsed_chat_id = parse_uuid(chat_id) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверный UUID') + with db_pool.connection() as connection: + chat = connection.execute('SELECT * FROM chats WHERE id=%s', (parsed_chat_id,)).fetchone() + if chat is None or not is_chat_member(connection, parsed_chat_id, user['id']): + return error_response(request, 404, 'not_found', 'Чат не найден') + members = connection.execute('SELECT user_id FROM chat_members WHERE chat_id=%s', (parsed_chat_id,)).fetchall() + return chat_response(chat, [item['user_id'] for item in members]) + + +@app.post('/api/v1/chats/{chat_id}/messages', status_code=201) +async def create_message(chat_id: str, request: Request): + user, failure = require_current_user(request) + if failure: + return failure + try: + parsed_chat_id = parse_uuid(chat_id) + idempotency_key = request.headers.get('Idempotency-Key', '') + if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', idempotency_key): + raise ValueError('invalid idempotency key') + data = await read_json_body(request) + require_fields(data, {'text'}) + text = data['text'] + if not isinstance(text, str) or not text.strip() or not 1 <= len(text) <= 4000: + raise ValueError('invalid text') + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверные данные сообщения') + + body_hash = hashlib.sha256(json.dumps({'text': text}, sort_keys=True).encode()).hexdigest() + with db_pool.connection() as connection: + if not is_chat_member(connection, parsed_chat_id, user['id']): + return error_response(request, 404, 'not_found', 'Чат не найден') + try: + with connection.transaction(): + idempotency = connection.execute( + 'INSERT INTO message_idempotency ' + '(user_id, chat_id, idempotency_key, body_hash) ' + 'VALUES (%s,%s,%s,%s) ' + 'ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING ' + 'RETURNING message_id', + (user['id'], parsed_chat_id, idempotency_key, body_hash), + ).fetchone() + if idempotency is None: + saved = connection.execute( + 'SELECT message_id, body_hash FROM message_idempotency ' + 'WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s', + (user['id'], parsed_chat_id, idempotency_key), + ).fetchone() + if saved['body_hash'] != body_hash: + raise IdempotencyConflict + row = connection.execute( + 'SELECT * FROM messages WHERE id=%s', (saved['message_id'],) + ).fetchone() + return message_response(row) + + message_id, created_at = uuid.uuid4(), utc_now() + connection.execute( + 'INSERT INTO messages VALUES (%s,%s,%s,%s,%s)', + (message_id, parsed_chat_id, user['id'], text, created_at), + ) + connection.execute( + 'UPDATE message_idempotency SET message_id=%s ' + 'WHERE user_id=%s AND chat_id=%s AND idempotency_key=%s', + (message_id, user['id'], parsed_chat_id, idempotency_key), + ) + row = { + 'id': message_id, 'chat_id': parsed_chat_id, + 'sender_id': user['id'], 'text': text, 'created_at': created_at, + } + except IdempotencyConflict: + return error_response(request, 409, 'conflict', 'Ключ уже использован для другого сообщения') + return message_response(row) + + +@app.get('/api/v1/chats/{chat_id}/messages') +def list_messages(chat_id: str, request: Request, limit: str = '50', cursor: str | None = None): + user, failure = require_current_user(request) + if failure: + return failure + try: + limit = parse_limit(limit) + except ValueError: + return error_response(request, 400, 'invalid_request', 'limit должен быть от 1 до 100') + try: + parsed_chat_id = parse_uuid(chat_id) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверный UUID') + + cursor_data = None + if cursor is not None: + if not cursor: + return error_response(request, 400, 'invalid_request', 'Неверный cursor') + try: + cursor_data = read_cursor(cursor, 'messages', str(parsed_chat_id)) + except ValueError: + return error_response(request, 400, 'invalid_request', 'Неверный cursor') + + with db_pool.connection() as connection: + if not is_chat_member(connection, parsed_chat_id, user['id']): + return error_response(request, 404, 'not_found', 'Чат не найден') + if cursor_data: + rows = connection.execute( + 'SELECT * FROM messages WHERE chat_id=%s AND (created_at,id)>(%s,%s) ' + 'ORDER BY created_at,id LIMIT %s', + (parsed_chat_id, cursor_data[0], cursor_data[1], limit + 1), + ).fetchall() + else: + rows = connection.execute( + 'SELECT * FROM messages WHERE chat_id=%s ORDER BY created_at,id LIMIT %s', + (parsed_chat_id, limit + 1), + ).fetchall() + + rows_to_return = rows[:limit] + next_cursor = make_cursor('messages', str(parsed_chat_id), rows_to_return[-1]) if len(rows) > limit else None + return {'items': [message_response(row) for row in rows_to_return], 'next_cursor': next_cursor} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..3a2df828 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,148 @@ +services: + postgres: + image: postgres:16.4-alpine + environment: + POSTGRES_DB: ${POSTGRES_DB:-messenger} + POSTGRES_USER: ${POSTGRES_USER:-messenger} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local-only-password} + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 512m + cpus: 1.0 + + redis: + image: redis:7.4.2-alpine + command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"] + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 256m + cpus: 0.5 + + migrate: + build: . + image: mtusi-messenger-lab3:3.0 + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-local-only-password}@postgres:5432/${POSTGRES_DB:-messenger} + command: ["python", "scripts/migrate.py"] + depends_on: + postgres: {condition: service_healthy} + + api-1: + build: . + image: mtusi-messenger-lab3:3.0 + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-local-only-password}@postgres:5432/${POSTGRES_DB:-messenger} + REDIS_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + INSTANCE_ID: api-1 + ENABLE_DEBUG_METRICS: ${ENABLE_DEBUG_METRICS:-1} + expose: ["8080"] + 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://localhost:8080/health/ready')"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 256m + cpus: 0.5 + + api-2: + build: . + image: mtusi-messenger-lab3:3.0 + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-local-only-password}@postgres:5432/${POSTGRES_DB:-messenger} + REDIS_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + INSTANCE_ID: api-2 + ENABLE_DEBUG_METRICS: ${ENABLE_DEBUG_METRICS:-1} + expose: ["8080"] + 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://localhost:8080/health/ready')"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 256m + cpus: 0.5 + + api-3: + build: . + image: mtusi-messenger-lab3:3.0 + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-local-only-password}@postgres:5432/${POSTGRES_DB:-messenger} + REDIS_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + INSTANCE_ID: api-3 + ENABLE_DEBUG_METRICS: ${ENABLE_DEBUG_METRICS:-1} + expose: ["8080"] + 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://localhost:8080/health/ready')"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 256m + cpus: 0.5 + + api-4: + build: . + image: mtusi-messenger-lab3:3.0 + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:-local-only-password}@postgres:5432/${POSTGRES_DB:-messenger} + REDIS_URL: redis://redis:6379/0 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + INSTANCE_ID: api-4 + ENABLE_DEBUG_METRICS: ${ENABLE_DEBUG_METRICS:-1} + expose: ["8080"] + 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://localhost:8080/health/ready')"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 256m + cpus: 0.5 + + lb: + image: nginx:1.27.2-alpine + ports: ["8080:8080"] + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + api-1: {condition: service_healthy} + api-2: {condition: service_healthy} + api-3: {condition: service_healthy} + api-4: {condition: service_healthy} + healthcheck: + test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8080/health/live >/dev/null"] + interval: 3s + timeout: 3s + retries: 20 + mem_limit: 128m + cpus: 0.25 + +volumes: + postgres_data: + redis_data: diff --git a/evidence/failover_all.json b/evidence/failover_all.json new file mode 100644 index 00000000..e22659bc --- /dev/null +++ b/evidence/failover_all.json @@ -0,0 +1,22 @@ +[ + { + "stopped": "api-1", + "successful_writes": 12, + "seconds": 3.405 + }, + { + "stopped": "api-2", + "successful_writes": 12, + "seconds": 2.412 + }, + { + "stopped": "api-3", + "successful_writes": 12, + "seconds": 3.407 + }, + { + "stopped": "api-4", + "successful_writes": 12, + "seconds": 3.376 + } +] diff --git a/evidence/failover_all.py b/evidence/failover_all.py new file mode 100755 index 00000000..70f5b03e --- /dev/null +++ b/evidence/failover_all.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Последовательная проверка остановки каждой API-реплики.""" +import json +import subprocess +import sys +import time +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client + + +def docker(*args): + return subprocess.run(('docker', 'compose') + args, cwd=ROOT, check=True, capture_output=True, text=True) + + +def main(): + results = [] + for target in ('api-1', 'api-2', 'api-3', 'api-4'): + docker('start', 'api-1', 'api-2', 'api-3', 'api-4') + time.sleep(4) + client = Client('http://localhost:8080', timeout=3) + alice, _, _, chat = client.bootstrap() + docker('stop', target) + started = time.monotonic() + successes = 0 + for _ in range(12): + try: + client.message( + alice, chat, 'failover-' + target + '-' + uuid.uuid4().hex, + key=uuid.uuid4().hex, + ) + successes += 1 + except Exception: + pass + time.sleep(0.1) + recovery = time.monotonic() - started + docker('start', target) + if successes == 0 or recovery > 10: + raise RuntimeError('failover did not recover for ' + target) + results.append({'stopped': target, 'successful_writes': successes, 'seconds': round(recovery, 3)}) + print(results[-1]) + (ROOT / 'evidence/failover_all.json').write_text(json.dumps(results, indent=2) + '\n') + + +if __name__ == '__main__': + main() diff --git a/evidence/plot_scale.py b/evidence/plot_scale.py new file mode 100644 index 00000000..c865440e --- /dev/null +++ b/evidence/plot_scale.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import json +from pathlib import Path +import matplotlib.pyplot as plt + +root = Path(__file__).resolve().parents[1] +fixed = json.loads((root / 'evidence/scale_results.json').read_text()) +resources = json.loads((root / 'evidence/scale_resource_results.json').read_text()) + +fig, axes = plt.subplots(1, 2, figsize=(10, 4)) +axes[0].plot([x['replicas'] for x in fixed], [x['successful_rps'] for x in fixed], 'o-') +axes[0].set_title('Фиксированные 2 CPU / 1 GB') +axes[0].set_xlabel('API-реплики') +axes[0].set_ylabel('Успешные RPS') +axes[0].grid(True) +axes[1].plot([x['total_cpu_limit'] for x in resources], [x['successful_rps'] for x in resources], 'o-') +axes[1].set_title('4 реплики, рост CPU') +axes[1].set_xlabel('Суммарный лимит CPU') +axes[1].set_ylabel('Успешные RPS') +axes[1].grid(True) +fig.tight_layout() +fig.savefig(root / 'evidence/scale_plot.png', dpi=150) diff --git a/evidence/rolling_load.json b/evidence/rolling_load.json new file mode 100644 index 00000000..0e466fb4 --- /dev/null +++ b/evidence/rolling_load.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 25.0, + "elapsed_seconds": 25.212, + "concurrency": 8, + "successful_requests": 1970, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 78.137, + "successful_latency_ms": { + "p50": 40.773, + "p95": 394.457, + "p99": 921.938 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/rolling_update.txt b/evidence/rolling_update.txt new file mode 100644 index 00000000..83f24253 --- /dev/null +++ b/evidence/rolling_update.txt @@ -0,0 +1,8 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab3-api-1-1 mtusi-messenger-lab3:3.0 "uvicorn app.main:ap…" api-1 45 seconds ago Up 44 seconds (healthy) 8080/tcp +mtusi2026lab3-api-2-1 mtusi-messenger-lab3:3.0 "uvicorn app.main:ap…" api-2 30 seconds ago Up 28 seconds (healthy) 8080/tcp +mtusi2026lab3-api-3-1 mtusi-messenger-lab3:3.0 "uvicorn app.main:ap…" api-3 22 seconds ago Up 17 seconds (healthy) 8080/tcp +mtusi2026lab3-api-4-1 mtusi-messenger-lab3:3.0 "uvicorn app.main:ap…" api-4 8 seconds ago Up 5 seconds (healthy) 8080/tcp +mtusi2026lab3-lb-1 nginx:1.27.2-alpine "/docker-entrypoint.…" lb 14 minutes ago Up 14 minutes (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi2026lab3-postgres-1 postgres:16.4-alpine "docker-entrypoint.s…" postgres 15 minutes ago Up 14 minutes (healthy) 5432/tcp +mtusi2026lab3-redis-1 redis:7.4.2-alpine "docker-entrypoint.s…" redis 15 minutes ago Up 14 minutes (healthy) 6379/tcp diff --git a/evidence/scale_1.json b/evidence/scale_1.json new file mode 100644 index 00000000..9480dae4 --- /dev/null +++ b/evidence/scale_1.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.017, + "concurrency": 8, + "successful_requests": 5991, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 398.948, + "successful_latency_ms": { + "p50": 17.782, + "p95": 28.191, + "p99": 36.047 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_2.json b/evidence/scale_2.json new file mode 100644 index 00000000..45f9c01f --- /dev/null +++ b/evidence/scale_2.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.205, + "concurrency": 8, + "successful_requests": 7468, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 491.139, + "successful_latency_ms": { + "p50": 12.152, + "p95": 24.869, + "p99": 113.716 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_4.json b/evidence/scale_4.json new file mode 100644 index 00000000..a7c930b8 --- /dev/null +++ b/evidence/scale_4.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.02, + "concurrency": 8, + "successful_requests": 7766, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 517.044, + "successful_latency_ms": { + "p50": 9.247, + "p95": 50.66, + "p99": 91.127 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_cpu_1.json b/evidence/scale_cpu_1.json new file mode 100644 index 00000000..d0c3b202 --- /dev/null +++ b/evidence/scale_cpu_1.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.016, + "concurrency": 8, + "successful_requests": 5098, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 339.502, + "successful_latency_ms": { + "p50": 6.829, + "p95": 89.931, + "p99": 189.588 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_cpu_2.json b/evidence/scale_cpu_2.json new file mode 100644 index 00000000..7527c978 --- /dev/null +++ b/evidence/scale_cpu_2.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.023, + "concurrency": 8, + "successful_requests": 4052, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 269.722, + "successful_latency_ms": { + "p50": 14.77, + "p95": 93.596, + "p99": 217.372 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_cpu_4.json b/evidence/scale_cpu_4.json new file mode 100644 index 00000000..a607fda5 --- /dev/null +++ b/evidence/scale_cpu_4.json @@ -0,0 +1,17 @@ +{ + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.016, + "concurrency": 8, + "successful_requests": 7375, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 491.144, + "successful_latency_ms": { + "p50": 13.728, + "p95": 31.818, + "p99": 56.496 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead." +} diff --git a/evidence/scale_experiment.py b/evidence/scale_experiment.py new file mode 100755 index 00000000..b2451a7a --- /dev/null +++ b/evidence/scale_experiment.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +"""Сравнение 1, 2 и 4 API-реплик при одинаковом суммарном лимите.""" +import json +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +TOTAL_CPU = 2.0 +TOTAL_MEMORY_MB = 1024 +DURATION = 15 +CONCURRENCY = 8 + + +def run(*args, check=True): + return subprocess.run(args, cwd=ROOT, check=check, text=True, capture_output=True) + + +def container(service): + return run('docker', 'compose', 'ps', '-q', service).stdout.strip() + + +def set_replicas(count): + for number in range(1, 5): + service = 'api-' + str(number) + if number <= count: + run('docker', 'compose', 'start', service) + else: + run('docker', 'compose', 'stop', service) + set_limits(count, TOTAL_CPU, TOTAL_MEMORY_MB) + + +def set_limits(count, total_cpu, total_memory): + cpu = total_cpu / count + memory = int(total_memory / count) + for number in range(1, count + 1): + run( + 'docker', 'update', '--cpus', str(cpu), + '--memory', str(memory) + 'm', '--memory-swap', str(memory) + 'm', + container('api-' + str(number)), + ) + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + try: + run('curl', '-fsS', 'http://localhost:8080/health/ready') + return + except subprocess.CalledProcessError: + time.sleep(1) + raise RuntimeError('load balancer did not become ready') + + +def load(count, label=None): + name = label or ('scale_' + str(count)) + output = ROOT / 'evidence' / (name + '.json') + run( + sys.executable, 'tests/load.py', '--base-url', 'http://localhost:8080', + '--duration', str(DURATION), '--concurrency', str(CONCURRENCY), + '--output', str(output), + ) + return json.loads(output.read_text()) + + +def resource_series(): + results = [] + set_replicas(4) + for total_cpu in (1.0, 2.0, 4.0): + set_limits(4, total_cpu, 1024) + measured = load(4, 'scale_cpu_' + str(int(total_cpu))) + measured['replicas'] = 4 + measured['total_cpu_limit'] = total_cpu + measured['total_memory_mb'] = 1024 + results.append(measured) + print(json.dumps(measured, ensure_ascii=False)) + (ROOT / 'evidence' / 'scale_resource_results.json').write_text( + json.dumps(results, ensure_ascii=False, indent=2) + '\n' + ) + return results + + +def main(): + run( + 'docker', 'compose', 'up', '-d', '--build', '--wait', + 'postgres', 'redis', 'migrate', 'api-1', 'api-2', 'api-3', 'api-4', 'lb', + ) + results = [] + try: + for count in (1, 2, 4): + set_replicas(count) + measured = load(count) + measured['replicas'] = count + measured['total_cpu_limit'] = TOTAL_CPU + measured['total_memory_mb'] = TOTAL_MEMORY_MB + results.append(measured) + print(json.dumps(measured, ensure_ascii=False)) + resource_series() + finally: + for number in range(1, 5): + run('docker', 'compose', 'start', 'api-' + str(number), check=False) + run('docker', 'compose', 'up', '-d', 'lb', check=False) + (ROOT / 'evidence' / 'scale_results.json').write_text( + json.dumps(results, ensure_ascii=False, indent=2) + '\n' + ) + + +if __name__ == '__main__': + main() diff --git a/evidence/scale_plot.png b/evidence/scale_plot.png new file mode 100644 index 00000000..20057cc8 Binary files /dev/null and b/evidence/scale_plot.png differ diff --git a/evidence/scale_resource_results.json b/evidence/scale_resource_results.json new file mode 100644 index 00000000..7ccb00e4 --- /dev/null +++ b/evidence/scale_resource_results.json @@ -0,0 +1,62 @@ +[ + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.016, + "concurrency": 8, + "successful_requests": 5098, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 339.502, + "successful_latency_ms": { + "p50": 6.829, + "p95": 89.931, + "p99": 189.588 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 4, + "total_cpu_limit": 1.0, + "total_memory_mb": 1024 + }, + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.023, + "concurrency": 8, + "successful_requests": 4052, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 269.722, + "successful_latency_ms": { + "p50": 14.77, + "p95": 93.596, + "p99": 217.372 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 4, + "total_cpu_limit": 2.0, + "total_memory_mb": 1024 + }, + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.016, + "concurrency": 8, + "successful_requests": 7375, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 491.144, + "successful_latency_ms": { + "p50": 13.728, + "p95": 31.818, + "p99": 56.496 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 4, + "total_cpu_limit": 4.0, + "total_memory_mb": 1024 + } +] diff --git a/evidence/scale_results.json b/evidence/scale_results.json new file mode 100644 index 00000000..dd5c91a4 --- /dev/null +++ b/evidence/scale_results.json @@ -0,0 +1,62 @@ +[ + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.017, + "concurrency": 8, + "successful_requests": 5991, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 398.948, + "successful_latency_ms": { + "p50": 17.782, + "p95": 28.191, + "p99": 36.047 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 1, + "total_cpu_limit": 2.0, + "total_memory_mb": 1024 + }, + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.205, + "concurrency": 8, + "successful_requests": 7468, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 491.139, + "successful_latency_ms": { + "p50": 12.152, + "p95": 24.869, + "p99": 113.716 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 2, + "total_cpu_limit": 2.0, + "total_memory_mb": 1024 + }, + { + "lab": 3, + "workload": "closed-loop POST messages; no automatic retries", + "requested_duration_seconds": 15.0, + "elapsed_seconds": 15.02, + "concurrency": 8, + "successful_requests": 7766, + "failed_requests": 0, + "error_ratio": 0.0, + "successful_rps": 517.044, + "successful_latency_ms": { + "p50": 9.247, + "p95": 50.66, + "p99": 91.127 + }, + "note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.", + "replicas": 4, + "total_cpu_limit": 2.0, + "total_memory_mb": 1024 + } +] diff --git a/migrations/001_init.sql b/migrations/001_init.sql new file mode 100644 index 00000000..09678c0e --- /dev/null +++ b/migrations/001_init.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, username VARCHAR(32) UNIQUE NOT NULL, + display_name VARCHAR(100) NOT NULL, password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, title VARCHAR(100) NOT NULL, created_at TIMESTAMPTZ NOT NULL +); +CREATE TABLE IF NOT EXISTS chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); +CREATE TABLE IF NOT EXISTS messages ( + id UUID PRIMARY KEY, chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id), text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL +); +CREATE INDEX IF NOT EXISTS messages_history_idx ON messages(chat_id, created_at, id); diff --git a/migrations/002_idempotency.sql b/migrations/002_idempotency.sql new file mode 100644 index 00000000..888fba19 --- /dev/null +++ b/migrations/002_idempotency.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS message_idempotency ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + idempotency_key VARCHAR(128) NOT NULL, + body_hash CHAR(64) NOT NULL, + message_id UUID REFERENCES messages(id) ON DELETE CASCADE, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, chat_id, idempotency_key) +); + +CREATE INDEX IF NOT EXISTS message_idempotency_created_idx + ON message_idempotency(created_at); diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 00000000..a229800a --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,24 @@ +events {} + +http { + upstream api_backend { + server api-1:8080 max_fails=2 fail_timeout=3s; + server api-2:8080 max_fails=2 fail_timeout=3s; + server api-3:8080 max_fails=2 fail_timeout=3s; + server api-4:8080 max_fails=2 fail_timeout=3s; + } + + server { + listen 8080; + + location / { + proxy_pass http://api_backend; + proxy_next_upstream error timeout http_502 http_503 http_504; + proxy_connect_timeout 1s; + proxy_read_timeout 5s; + proxy_send_timeout 5s; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + } +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..265210d8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +fastapi==0.115.6 +uvicorn[standard]==0.34.0 +psycopg[binary,pool]==3.2.3 +redis==5.2.1 +argon2-cffi==23.1.0 diff --git a/scripts/migrate.py b/scripts/migrate.py new file mode 100755 index 00000000..07a01aa8 --- /dev/null +++ b/scripts/migrate.py @@ -0,0 +1,10 @@ +import os +import psycopg +from pathlib import Path + +url = os.getenv("DATABASE_URL", "postgresql://messenger:messenger@postgres:5432/messenger") +with psycopg.connect(url) as conn: + for path in sorted(Path("migrations").glob("*.sql")): + conn.execute(path.read_text()) + conn.commit() +print("migrations applied") diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..a4431bfa --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu +# Перезапускаем стенд, не удаляя volumes. +# Балансировщик запускаем после API, чтобы DNS имён реплик уже был доступен. +docker compose up -d --build postgres redis migrate api-1 api-2 api-3 api-4 +docker compose restart postgres redis api-1 api-2 api-3 api-4 +docker compose up -d --force-recreate lb +docker compose ps diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100755 index 00000000..48d59831 --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu + +for service in api-1 api-2 api-3 api-4; do + docker compose up -d --no-deps --force-recreate "$service" + container=$(docker compose ps -q "$service") + deadline=$(( $(date +%s) + 30 )) + while [ "$(date +%s)" -lt "$deadline" ]; do + status=$(docker inspect --format '{{.State.Health.Status}}' "$container" 2>/dev/null || true) + [ "$status" = healthy ] && break + sleep 1 + done + [ "$(docker inspect --format '{{.State.Health.Status}}' "$container")" = healthy ] +done + +docker compose ps diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..d6020346 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -eu + +TARGET_INSTANCE_ID=${TARGET_INSTANCE_ID:-api-1} +case "$TARGET_INSTANCE_ID" in + api-1) service=api-1 ;; + api-2) service=api-2 ;; + api-3) service=api-3 ;; + api-4) service=api-4 ;; + *) echo "unknown TARGET_INSTANCE_ID: $TARGET_INSTANCE_ID" >&2; exit 2 ;; +esac + +docker compose stop "$service" diff --git a/student-tests/idempotency.py b/student-tests/idempotency.py new file mode 100755 index 00000000..b7ad5e30 --- /dev/null +++ b/student-tests/idempotency.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +"""Проверка общего Idempotency-Key через обе API-реплики.""" +import sys +import uuid +from concurrent.futures import ThreadPoolExecutor + +sys.path.insert(0, 'tests') +from client import Client, check + + +def main(): + client = Client('http://localhost:8080') + alice, _, _, chat = client.bootstrap() + key = 'student-' + uuid.uuid4().hex + body = {'text': 'одно сообщение при восьми повторах'} + + def send(_): + return client.call( + 'POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, + actor=alice, body=body, + headers={'Idempotency-Key': key}, schema='Message', + ) + + with ThreadPoolExecutor(max_workers=8) as pool: + responses = list(pool.map(send, range(8))) + ids = {item['json']['id'] for item in responses} + check(len(ids) == 1, 'concurrent retries created more than one message') + client.call( + 'POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, + actor=alice, body={'text': 'другой текст'}, + headers={'Idempotency-Key': key}, + ) + print('PASS: concurrent idempotency, one message id:', next(iter(ids))) + + +if __name__ == '__main__': + main()