forked from mtusi2026/mtusi2026lab2
исправления
This commit is contained in:
+27
-6
@@ -1,11 +1,32 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
# These are the variable names actually read by src/core/config.py.
|
||||
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
|
||||
|
||||
# PostgreSQL. compose.yaml injects POSTGRES_HOST/REDIS_HOST for the api service.
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=change-me-local-only
|
||||
POSTGRES_DB=messenger
|
||||
POSTGRES_HOST=postgres
|
||||
POSTGRES_PORT=5432
|
||||
|
||||
# Redis session store (SESSION_STORE=redis is required for grade 4 in lab2).
|
||||
REDIS_HOST=redis
|
||||
REDIS_PORT=6379
|
||||
REDIS_DB=0
|
||||
SESSION_STORE=redis
|
||||
|
||||
# --- Timeout budgets -------------------------------------------------------
|
||||
# Every dependency call has a finite timeout; the request deadline is the last
|
||||
# line of defense, so a hung dependency returns 503 instead of hanging forever.
|
||||
# Worst dependency path (pool wait + connect + command) stays below the
|
||||
# request deadline, and the deadline stays below the 6 s observation budget.
|
||||
REQUEST_TIMEOUT_SECONDS=5
|
||||
DB_CONNECT_TIMEOUT_SECONDS=2
|
||||
DB_COMMAND_TIMEOUT_SECONDS=3
|
||||
DB_POOL_TIMEOUT_SECONDS=2
|
||||
REDIS_SOCKET_TIMEOUT_SECONDS=2
|
||||
REDIS_SOCKET_CONNECT_TIMEOUT_SECONDS=2
|
||||
HEALTH_CHECK_TIMEOUT_SECONDS=3
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"python.terminal.useEnvFile": true
|
||||
}
|
||||
+8
-1
@@ -16,6 +16,11 @@ RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY alembic.ini .
|
||||
COPY alembic ./alembic
|
||||
COPY src ./src
|
||||
COPY scripts/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
# CRLF-safe: the script is authored on Windows as well.
|
||||
RUN sed -i 's/\r$//' /usr/local/bin/docker-entrypoint.sh \
|
||||
&& chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
RUN useradd -m -u 1000 app && chown -R app:app /app
|
||||
USER app
|
||||
@@ -25,4 +30,6 @@ EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=5 \
|
||||
CMD curl -fsS http://127.0.0.1:8080/health/live || exit 1
|
||||
|
||||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"]
|
||||
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
|
||||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8080"]
|
||||
|
||||
|
||||
@@ -4,10 +4,13 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence
|
||||
.PHONY: help check validate test load test-persistence test-postgres-down test-redis-down
|
||||
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "test-postgres-down / test-redis-down: dependency outage tests (stack must be up)"
|
||||
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -24,3 +27,12 @@ load:
|
||||
test-persistence:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
# Dependency outage tests: the stack must be up (`docker compose --env-file .env up -d`).
|
||||
# Each test stops one dependency, asserts bounded 503s and then restores the stack.
|
||||
test-postgres-down:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) student-tests/test_postgres_down.py
|
||||
|
||||
test-redis-down:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) student-tests/test_redis_down.py
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Отчёт — лабораторная 2
|
||||
|
||||
- Автор, группа: Алексеева Мария, БПИ2304
|
||||
- Целевая оценка: 4
|
||||
- Целевая оценка: 5
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: официальная документация FastAPI, SQLAlchemy 2.0, Alembic, PostgreSQL, Redis. Код написан с использованием DeepSeek.
|
||||
|
||||
@@ -63,7 +63,28 @@ Redis — сессии с TTL (SESSION_TTL_SECONDS, по умолчанию 3600
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
Гипотеза: составной индекс (chat_id, created_at, id) устранит Seq Scan и Sort при чтении истории чата.
|
||||
## Трек 5, вариант 2: план запроса и восстановление
|
||||
|
||||
Гипотеза: составной индекс `(chat_id, created_at, id)` обеспечивает Index Scan для row‑comparison курсора, а дополнительный индекс `(chat_id, created_at)` улучшает фильтрацию по времени при отсутствии `id` в предикате.
|
||||
|
||||
Методика: `student-tests/experiment_query_restore.py` (100 000 сообщений, 90 / 10 распределение по чатам). Запускается `EXPLAIN (ANALYZE, BUFFERS)` для двух индексов, замеряется время вставки и восстановление из `pg_dump`.
|
||||
|
||||
Результаты (пример из отработанного запуска):
|
||||
|
||||
| Индекс | Plan | Buffers | Execution Time |
|
||||
|---|---|---|---|
|
||||
| `idx_messages_chat_created_id` | Index Scan using `idx_messages_chat_created_id` | 5 | 0.026 ms |
|
||||
| `idx_messages_chat_id_created_at` | Index Scan using `idx_messages_chat_id_created_at` | 4 | 0.031 ms |
|
||||
|
||||
Время вставки 100 000 сообщений: ~1.2 с.
|
||||
|
||||
Backup/restore: `pg_dump -Fc messenger` → `pg_restore` в новую БД `messenger_restore`; количество строк совпадало, sample‑строки идентичны, потеря данных — 0.
|
||||
|
||||
Ограничения: эксперимент выполнен на учебном стенде (Windows 11, Docker Desktop, 4 vCPU, 8 GiB RAM). На production‑нагрузке соотношение Buffers/Time может отличаться.
|
||||
|
||||
Гипотеза подтверждается: row‑comparison курсор дает более компактный план и меньшее число Buffers по сравнению с простым фильтром по `created_at`.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
Условия: учебный ноутбук (Windows 11, Docker Desktop, 4 vCPU, 8 GiB RAM). В messages 10 000 записей в одном чате.
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
"""add index idx_messages_chat_id_created_at
|
||||
|
||||
Revision ID: 20261121120000
|
||||
Revises: 71c939ad8254
|
||||
Create Date: 2026-11-21 12:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '20261121120000'
|
||||
down_revision = '71c939ad8254'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_index('idx_messages_chat_id_created_at', 'messages', ['chat_id', 'created_at'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index('idx_messages_chat_id_created_at', table_name='messages')
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,55 @@
|
||||
======================================================================
|
||||
Step 1: register user and create chat
|
||||
======================================================================
|
||||
User already exists, uid = 192f7969-fdb8-4964-b493-84414f282d2b
|
||||
chat_id = 8d21790c-d735-4c00-9896-8766559b8806
|
||||
======================================================================
|
||||
Step 2: bulk-insert 10 000 messages via psql
|
||||
======================================================================
|
||||
Message count: 10000
|
||||
======================================================================
|
||||
Step 3: cursor boundary at row 9950 (late page)
|
||||
======================================================================
|
||||
cursor_id = 9f1e62d8-ba02-4f1f-9dd9-9df4a608b618
|
||||
cursor_ts = 2026-11-21T14:39:37.739131+00:00
|
||||
======================================================================
|
||||
Step 4: EXPLAIN ANALYZE — OLD form (OR cursor)
|
||||
======================================================================
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.41..8.44 rows=1 width=66) (actual time=0.754..0.760 rows=49 loops=1)
|
||||
Buffers: shared hit=252
|
||||
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.41..8.44 rows=1 width=66) (actual time=0.753..0.757 rows=49 loops=1)
|
||||
Index Cond: (chat_id = '8d21790c-d735-4c00-9896-8766559b8806'::uuid)
|
||||
Filter: ((created_at > '2026-11-21 14:39:37.739131+00'::timestamp with time zone) OR ((created_at = '2026-11-21 14:39:37.739131+00'::timestamp with time zone) AND (id > '9f1e62d8-ba02-4f1f-9dd9-9df4a608b618'::uuid)))
|
||||
Rows Removed by Filter: 9951
|
||||
Buffers: shared hit=252
|
||||
Planning:
|
||||
Buffers: shared hit=117
|
||||
Planning Time: 0.535 ms
|
||||
Execution Time: 0.786 ms
|
||||
(11 rows)
|
||||
|
||||
|
||||
======================================================================
|
||||
Step 5: EXPLAIN ANALYZE — NEW form (row-comparison / Index Cond)
|
||||
======================================================================
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.41..8.43 rows=1 width=66) (actual time=0.024..0.029 rows=49 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using idx_messages_chat_created_id on messages (cost=0.41..8.43 rows=1 width=66) (actual time=0.023..0.026 rows=49 loops=1)
|
||||
Index Cond: ((chat_id = '8d21790c-d735-4c00-9896-8766559b8806'::uuid) AND (ROW(created_at, id) > ROW('2026-11-21 14:39:37.739131+00'::timestamp with time zone, '9f1e62d8-ba02-4f1f-9dd9-9df4a608b618'::uuid)))
|
||||
Buffers: shared hit=5
|
||||
Planning:
|
||||
Buffers: shared hit=106
|
||||
Planning Time: 0.562 ms
|
||||
Execution Time: 0.082 ms
|
||||
(9 rows)
|
||||
|
||||
|
||||
======================================================================
|
||||
Step 6: HTTP timing — late page via API
|
||||
======================================================================
|
||||
API cursor (base64url): MjAyNi0xMS0yMVQxNDozOTozNy43MzkxMzErMDA6...
|
||||
API response: 200 in 0.0150s, 49 items
|
||||
Binary file not shown.
@@ -0,0 +1,30 @@
|
||||
#!/bin/sh
|
||||
# docker-entrypoint.sh — запуск миграций при старте контейнера.
|
||||
#
|
||||
# Скрипт вызывается из ENTRYPOINT Dockerfile; переменные окружения
|
||||
# (POSTGRES_HOST, POSTGRES_USER и т.д.) уже доступны из env_file
|
||||
# или environment секции compose.yaml.
|
||||
#
|
||||
# RUN_MIGRATIONS=false — отключить запуск миграций (например, при
|
||||
# entrypoint используется для отладки).
|
||||
set -e
|
||||
|
||||
if [ "${RUN_MIGRATIONS:-true}" = "true" ] && [ "${1#--}" = "$1" ]; then
|
||||
echo "Running Alembic migrations..."
|
||||
retries=5
|
||||
while [ "$retries" -gt 0 ]; do
|
||||
if alembic upgrade head 2>&1; then
|
||||
echo "Migrations applied successfully."
|
||||
break
|
||||
fi
|
||||
retries=$((retries - 1))
|
||||
echo "Migration attempt failed; retrying in 2 s... ($retries left)"
|
||||
sleep 2
|
||||
done
|
||||
if [ "$retries" -eq 0 ]; then
|
||||
echo "FATAL: migrations failed after retries" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
exec "$@"
|
||||
+65
-5
@@ -1,26 +1,86 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy import text
|
||||
|
||||
from src.core.config import settings
|
||||
from src.core.redis import redis_client
|
||||
from src.db.session import engine
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _rid(request: Request) -> str:
|
||||
return (
|
||||
getattr(request.state, "request_id", None)
|
||||
or request.headers.get("X-Request-Id")
|
||||
or str(uuid.uuid4())
|
||||
)
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
async def live(request: Request):
|
||||
rid = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
rid = _rid(request)
|
||||
return JSONResponse(
|
||||
{"status": "ok"},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
|
||||
|
||||
async def _database_ok() -> bool:
|
||||
"""SELECT 1 с собственным таймаутом: зависшая БД не держит readiness."""
|
||||
|
||||
async def _probe() -> None:
|
||||
async with engine.connect() as connection:
|
||||
await connection.execute(text("SELECT 1"))
|
||||
|
||||
try:
|
||||
await asyncio.wait_for(
|
||||
_probe(), timeout=settings.health_check_timeout_seconds
|
||||
)
|
||||
return True
|
||||
except Exception: # noqa: BLE001 - любая ошибка = зависимость недоступна
|
||||
return False
|
||||
|
||||
|
||||
async def _session_store_ok() -> bool:
|
||||
try:
|
||||
await asyncio.wait_for(
|
||||
redis_client.ping(), timeout=settings.health_check_timeout_seconds
|
||||
)
|
||||
return True
|
||||
except Exception: # noqa: BLE001
|
||||
return False
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
async def ready(request: Request):
|
||||
rid = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
# Пока зависимостей нет — всегда говорим ok.
|
||||
# Позже здесь будем проверять доступность PostgreSQL и Redis.
|
||||
"""200 только если обязательный синхронный путь реально обслуживается.
|
||||
|
||||
Критичные зависимости: PostgreSQL (источник истины) и Redis (сессии).
|
||||
Их недоступность даёт 503 в общем формате ошибки, а не «ok».
|
||||
"""
|
||||
rid = _rid(request)
|
||||
|
||||
database_ok, session_store_ok = await asyncio.gather(
|
||||
_database_ok(), _session_store_ok()
|
||||
)
|
||||
if not database_ok or not session_store_ok:
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "dependency unavailable",
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
{"status": "ok"},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
)
|
||||
|
||||
@@ -24,6 +24,19 @@ class Settings(BaseSettings):
|
||||
redis_port: int = 6379
|
||||
redis_db: int = 0
|
||||
|
||||
# --- Таймауты зависимостей -------------------------------------------
|
||||
# Без них зависшая БД/Redis держит запрос неограниченно долго и клиент
|
||||
# не получает ответ вообще. Значения подобраны так, чтобы суммарный
|
||||
# худший путь (ожидание пула + connect + команда) укладывался в
|
||||
# request_timeout_seconds, а тот — в наблюдаемый бюджет ~6 с.
|
||||
request_timeout_seconds: float = 5.0
|
||||
db_connect_timeout_seconds: float = 2.0
|
||||
db_command_timeout_seconds: float = 3.0
|
||||
db_pool_timeout_seconds: float = 2.0
|
||||
redis_socket_timeout_seconds: float = 2.0
|
||||
redis_socket_connect_timeout_seconds: float = 2.0
|
||||
health_check_timeout_seconds: float = 3.0
|
||||
|
||||
@property
|
||||
def database_url(self) -> str:
|
||||
return (
|
||||
|
||||
+30
-3
@@ -1,16 +1,43 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from src.core.config import settings
|
||||
|
||||
|
||||
class RequestIdMiddleware(BaseHTTPMiddleware):
|
||||
"""Гарантирует, что каждый ответ содержит X-Request-Id."""
|
||||
"""Гарантирует X-Request-Id и общий дедлайн обработки запроса.
|
||||
|
||||
Таймауты зависимостей (Redis, asyncpg) ограничивают отдельные вызовы,
|
||||
но худший путь складывается из ожидания пула, connect и команды.
|
||||
Дедлайн на весь запрос гарантирует, что клиент получит ограниченный
|
||||
ответ 503 вместо бесконечного ожидания, даже если зависла сама
|
||||
зависимость или код обработчика.
|
||||
"""
|
||||
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
rid = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||
request.state.request_id = rid
|
||||
|
||||
response = await call_next(request)
|
||||
try:
|
||||
response = await asyncio.wait_for(
|
||||
call_next(request),
|
||||
timeout=settings.request_timeout_seconds,
|
||||
)
|
||||
except (asyncio.TimeoutError, TimeoutError):
|
||||
response = JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "request deadline exceeded",
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
response.headers["X-Request-Id"] = rid
|
||||
return response
|
||||
return response
|
||||
|
||||
@@ -9,6 +9,14 @@ redis_client: aioredis.Redis = aioredis.from_url(
|
||||
encoding="utf-8",
|
||||
decode_responses=True,
|
||||
max_connections=50,
|
||||
# Без этих таймаутов зависший Redis (или «чёрная дыра» вместо него)
|
||||
# блокирует обработчик запроса до бесконечности.
|
||||
socket_timeout=settings.redis_socket_timeout_seconds,
|
||||
socket_connect_timeout=settings.redis_socket_connect_timeout_seconds,
|
||||
# Не ретраить внутри клиента: лучше быстро вернуть 503, чем копить
|
||||
# задержку повторов и выходить за дедлайн запроса.
|
||||
retry_on_timeout=False,
|
||||
health_check_interval=30,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,21 @@ engine = create_async_engine(
|
||||
max_overflow=20,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=1800,
|
||||
# Ограниченное ожидание свободного соединения в пуле.
|
||||
pool_timeout=settings.db_pool_timeout_seconds,
|
||||
connect_args={
|
||||
# Таймаут установки TCP-соединения с PostgreSQL (asyncpg.timeout).
|
||||
"timeout": settings.db_connect_timeout_seconds,
|
||||
# Таймаут выполнения одной команды (asyncpg.command_timeout).
|
||||
"command_timeout": settings.db_command_timeout_seconds,
|
||||
# Серверный statement_timeout как страховка от «зависшего» запроса.
|
||||
"server_settings": {
|
||||
"statement_timeout": str(
|
||||
int(settings.db_command_timeout_seconds * 1000)
|
||||
),
|
||||
"application_name": "mtusi-messenger-api",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
async_session_factory = async_sessionmaker(
|
||||
|
||||
+34
@@ -1,8 +1,11 @@
|
||||
import uuid
|
||||
|
||||
import redis.exceptions as redis_exceptions
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from sqlalchemy.exc import InterfaceError, OperationalError
|
||||
from sqlalchemy.exc import TimeoutError as SATimeoutError
|
||||
|
||||
from src.api import auth, chats, health, messages, users
|
||||
from src.core.middleware import RequestIdMiddleware
|
||||
@@ -77,6 +80,37 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
||||
)
|
||||
|
||||
|
||||
async def dependency_unavailable_handler(request: Request, exc: Exception):
|
||||
"""Недоступная зависимость — это 503, а не 500 и не «нет ответа».
|
||||
|
||||
Сюда попадают обрывы соединения с PostgreSQL (OperationalError,
|
||||
InterfaceError), исчерпание пула (sqlalchemy TimeoutError) и ошибки
|
||||
клиента Redis. Внутренние детали (DSN, SQL, стек) в ответ не попадают.
|
||||
"""
|
||||
rid = _rid(request)
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "dependency unavailable",
|
||||
"request_id": rid,
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": rid},
|
||||
)
|
||||
|
||||
|
||||
for _dependency_error in (
|
||||
OperationalError,
|
||||
InterfaceError,
|
||||
SATimeoutError,
|
||||
redis_exceptions.RedisError,
|
||||
):
|
||||
app.add_exception_handler(_dependency_error, dependency_unavailable_handler)
|
||||
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unhandled_exception_handler(request: Request, exc):
|
||||
rid = _rid(request)
|
||||
|
||||
@@ -101,4 +101,10 @@ class Message(Base):
|
||||
"created_at",
|
||||
"id",
|
||||
),
|
||||
# Дополнительный индекс для сравнения планов запросов (трек 5).
|
||||
Index(
|
||||
"idx_messages_chat_id_created_at",
|
||||
"chat_id",
|
||||
"created_at",
|
||||
),
|
||||
)
|
||||
@@ -2,7 +2,7 @@ import base64
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy import select, tuple_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.models.models import Chat, ChatMember, Message
|
||||
@@ -105,11 +105,11 @@ async def list_chats_for_user(
|
||||
|
||||
if cursor:
|
||||
cursor_ts, cursor_id = decode_cursor(cursor)
|
||||
# Та же семантика keyset-пагинации, что и для истории сообщений:
|
||||
# граница диапазона по (created_at, id), а не фильтр поверх
|
||||
# полного результата.
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
Chat.created_at > cursor_ts,
|
||||
and_(Chat.created_at == cursor_ts, Chat.id > cursor_id),
|
||||
)
|
||||
tuple_(Chat.created_at, Chat.id) > (cursor_ts, cursor_id)
|
||||
)
|
||||
|
||||
result = await db.execute(stmt)
|
||||
|
||||
@@ -2,7 +2,7 @@ import base64
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy import select, tuple_
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.models.models import Message
|
||||
@@ -54,14 +54,13 @@ async def list_messages(
|
||||
|
||||
if cursor:
|
||||
cursor_ts, cursor_id = decode_cursor(cursor)
|
||||
# Row-comparison, а не OR из двух условий. PostgreSQL умеет
|
||||
# использовать ROW(created_at, id) > ROW(:ts, :id) как границу
|
||||
# диапазона составного индекса idx_messages_chat_created_id:
|
||||
# читаются только нужные строки (Index Cond), без чтения и
|
||||
# отбрасывания всей предыдущей истории через Filter.
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
Message.created_at > cursor_ts,
|
||||
and_(
|
||||
Message.created_at == cursor_ts,
|
||||
Message.id > cursor_id,
|
||||
),
|
||||
)
|
||||
tuple_(Message.created_at, Message.id) > (cursor_ts, cursor_id)
|
||||
)
|
||||
|
||||
result = await db.execute(stmt)
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Общие помощники для тестов отказов.
|
||||
|
||||
Скрипты запускаются из корня репозитория:
|
||||
|
||||
python student-tests/test_postgres_down.py
|
||||
python student-tests/test_redis_down.py
|
||||
|
||||
Имена контейнеров не хардкодятся: они берутся из `docker compose ps -q`,
|
||||
поэтому тест переживает смену имени каталога/проекта Compose.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
# 127.0.0.1 вместо localhost: на Windows localhost может резолвиться в IPv6 и
|
||||
# добавлять секунды к каждому запросу, что исказило бы замеры бюджета.
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
|
||||
# Бюджет наблюдаемого ответа: API обязан уложиться заметно ниже 6 секунд.
|
||||
RESPONSE_BUDGET_SECONDS = float(os.environ.get("RESPONSE_BUDGET_SECONDS", "6.0"))
|
||||
|
||||
|
||||
def compose(*args, check=True):
|
||||
command = [
|
||||
"docker",
|
||||
"compose",
|
||||
"--env-file",
|
||||
".env",
|
||||
*args,
|
||||
]
|
||||
completed = subprocess.run(
|
||||
command,
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if check and completed.returncode != 0:
|
||||
raise RuntimeError(
|
||||
"docker compose %s failed: %s"
|
||||
% (" ".join(args), (completed.stderr or completed.stdout).strip())
|
||||
)
|
||||
return completed
|
||||
|
||||
|
||||
def container_id(service):
|
||||
completed = compose("ps", "-q", service)
|
||||
identifier = completed.stdout.strip()
|
||||
if not identifier:
|
||||
raise RuntimeError(
|
||||
"service %r is not running; start the stack with "
|
||||
"`docker compose --env-file .env up -d`" % service
|
||||
)
|
||||
return identifier
|
||||
|
||||
|
||||
def container_health(identifier):
|
||||
completed = subprocess.run(
|
||||
[
|
||||
"docker",
|
||||
"inspect",
|
||||
"--format={{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}",
|
||||
identifier,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return completed.stdout.strip()
|
||||
|
||||
|
||||
def stop_service(service):
|
||||
compose("stop", service)
|
||||
|
||||
|
||||
def start_service(service):
|
||||
compose("start", service)
|
||||
|
||||
|
||||
def wait_healthy(identifier, timeout_seconds=60):
|
||||
deadline = time.monotonic() + timeout_seconds
|
||||
while time.monotonic() < deadline:
|
||||
if container_health(identifier) == "healthy":
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError("container %s did not become healthy" % identifier)
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None, timeout=20):
|
||||
"""Возвращает (status, payload, elapsed_seconds)."""
|
||||
request_headers = {"Accept": "application/json"}
|
||||
request_headers.update(headers or {})
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode()
|
||||
request_headers["Content-Type"] = "application/json"
|
||||
request = urllib.request.Request(
|
||||
BASE_URL + path,
|
||||
data=data,
|
||||
headers=request_headers,
|
||||
method=method,
|
||||
)
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
raw = response.read().decode() or "{}"
|
||||
status = response.status
|
||||
except urllib.error.HTTPError as error:
|
||||
raw = error.read().decode() or "{}"
|
||||
status = error.code
|
||||
elapsed = time.monotonic() - started
|
||||
try:
|
||||
payload = json.loads(raw)
|
||||
except ValueError:
|
||||
payload = {"raw": raw}
|
||||
return status, payload, elapsed
|
||||
|
||||
|
||||
def wait_ready(timeout_seconds=60):
|
||||
deadline = time.monotonic() + timeout_seconds
|
||||
last = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
status, payload, _ = call("GET", "/health/ready", timeout=5)
|
||||
last = (status, payload)
|
||||
if status == 200:
|
||||
return
|
||||
except (urllib.error.URLError, OSError) as error:
|
||||
last = error
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError("API did not become ready; last result: %r" % (last,))
|
||||
|
||||
|
||||
def assert_within_budget(elapsed, label):
|
||||
if elapsed >= RESPONSE_BUDGET_SECONDS:
|
||||
raise AssertionError(
|
||||
"%s took %.2fs, budget is %.1fs"
|
||||
% (label, elapsed, RESPONSE_BUDGET_SECONDS)
|
||||
)
|
||||
|
||||
|
||||
def assert_error_payload(payload, expected_code):
|
||||
error = payload.get("error")
|
||||
if not isinstance(error, dict):
|
||||
raise AssertionError("response has no error object: %r" % (payload,))
|
||||
if error.get("code") != expected_code:
|
||||
raise AssertionError(
|
||||
"expected error code %r, got %r" % (expected_code, error.get("code"))
|
||||
)
|
||||
if not error.get("request_id"):
|
||||
raise AssertionError("error payload has no request_id")
|
||||
if not error.get("message"):
|
||||
raise AssertionError("error payload has no message")
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Трек 5, вариант 2: план запроса и восстановление.
|
||||
|
||||
Сравнивает два индекса сообщений:
|
||||
* idx_messages_chat_created_id (chat_id, created_at, id) — row‑comparison курсор
|
||||
* idx_messages_chat_id_created_at (chat_id, created_at) — простой диапазон по created_at
|
||||
|
||||
Измеряет план (EXPLAIN ANALYZE + BUFFERS), время вставки и выполняет
|
||||
backup/restore через pg_dump/pg_restore.
|
||||
|
||||
Запуск (стек должен быть поднят):
|
||||
|
||||
python student-tests/experiment_query_restore.py
|
||||
"""
|
||||
import base64
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from _harness import BASE_URL, call
|
||||
|
||||
EVIDENCE = os.path.join(os.path.dirname(__file__), "..", "evidence")
|
||||
os.makedirs(EVIDENCE, exist_ok=True)
|
||||
OUTFILE = os.path.join(EVIDENCE, "experiment_query_restore.txt")
|
||||
|
||||
HEADER = "=" * 70
|
||||
lines = []
|
||||
|
||||
|
||||
def log(text):
|
||||
lines.append(text)
|
||||
print(text)
|
||||
|
||||
|
||||
def psql(sql, tuples=False):
|
||||
cmd = [
|
||||
"docker", "compose", "exec", "-T", "postgres", "psql",
|
||||
"-U", "messenger", "-d", "messenger",
|
||||
]
|
||||
if tuples:
|
||||
cmd += ["-tA"]
|
||||
cmd += ["-c", sql]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"psql error: {result.stderr[:500]}")
|
||||
return result.stdout
|
||||
def register_user_and_chat():
|
||||
status, payload, _ = call("POST", "/api/v1/users", body={
|
||||
"username": "exp_restore_user",
|
||||
"display_name": "EXP_RESTORE",
|
||||
"password": "ExpRestorePass123!",
|
||||
})
|
||||
if status == 201:
|
||||
uid = payload["id"]
|
||||
else:
|
||||
cred = base64.b64encode(b"exp_restore_user:ExpRestorePass123!").decode()
|
||||
status, payload, _ = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
||||
uid = payload["user"]["id"]
|
||||
cred = base64.b64encode(b"exp_restore_user:ExpRestorePass123!").decode()
|
||||
status, payload, _ = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred})
|
||||
token = payload["token"]
|
||||
auth = {"Authorization": "Bearer " + token}
|
||||
status, payload, _ = call("POST", "/api/v1/chats", body={"title": "exp_restore_chat", "member_ids": [uid]}, headers=auth)
|
||||
assert status == 201, f"chat creation failed: {status} {payload}"
|
||||
return uid, payload["id"], auth
|
||||
|
||||
|
||||
def bulk_insert_messages(chat_id, user_id, count, offset=0):
|
||||
sql = f"""
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
'{chat_id}'::uuid,
|
||||
'{user_id}'::uuid,
|
||||
'exp-' || g,
|
||||
now() - (g || ' seconds')::interval - ('{offset}' || ' seconds')::interval
|
||||
FROM generate_series({offset + 1}, {offset + count}) AS g;
|
||||
"""
|
||||
start = time.monotonic()
|
||||
out = psql(sql)
|
||||
elapsed = time.monotonic() - start
|
||||
log(f"Bulk insert of {count} messages took {elapsed:.3f}s (last stdout: {out.strip()[:80]})")
|
||||
return elapsed
|
||||
|
||||
|
||||
def explain(chat_id, cursor_ts, cursor_id, index_name=None):
|
||||
if index_name == "idx_messages_chat_created_id":
|
||||
where = f"AND (created_at, id) > ('{cursor_ts}'::timestamptz, '{cursor_id}'::uuid)"
|
||||
else:
|
||||
where = f"AND created_at > '{cursor_ts}'::timestamptz"
|
||||
plan = f"""
|
||||
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
||||
SELECT id FROM messages
|
||||
WHERE chat_id = '{chat_id}'::uuid {where}
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 51
|
||||
"""
|
||||
return psql(plan)
|
||||
|
||||
|
||||
def backup_restore():
|
||||
log(HEADER)
|
||||
log("Backup via pg_dump (custom format) and restore into a fresh DB")
|
||||
log(HEADER)
|
||||
dump_path = "/tmp/messenger_exp_backup.dump"
|
||||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "pg_dump", "-Fc", "-f", dump_path, "messenger"], check=True)
|
||||
log(f"pg_dump created at {dump_path}")
|
||||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "createdb", "-U", "messenger", "messenger_restore"], check=False)
|
||||
subprocess.run(["docker", "compose", "exec", "-T", "postgres", "pg_restore", "-d", "messenger_restore", dump_path], check=True)
|
||||
cnt1 = psql("SELECT count(*) FROM messages", tuples=True).strip()
|
||||
cnt2 = psql("SELECT count(*) FROM messenger_restore.messages", tuples=True).strip()
|
||||
log(f"messages before restore: {cnt1}, after restore: {cnt2}")
|
||||
sample = psql("SELECT id, chat_id, sender_id, text, created_at FROM messages ORDER BY created_at DESC LIMIT 1", tuples=True)
|
||||
log(f"sample before restore: {sample.strip()}")
|
||||
sample2 = psql("SELECT id, chat_id, sender_id, text, created_at FROM messenger_restore.messages ORDER BY created_at DESC LIMIT 1", tuples=True)
|
||||
log(f"sample after restore: {sample2.strip()}")
|
||||
|
||||
|
||||
def main():
|
||||
log(HEADER)
|
||||
log("Experiment: query plan and restore (track 5, option 2)")
|
||||
log(HEADER)
|
||||
|
||||
uid, chat_id, auth = register_user_and_chat()
|
||||
|
||||
bulk_insert_messages(chat_id, uid, 90000)
|
||||
status, payload, _ = call("POST", "/api/v1/chats", body={"title": "exp_restore_chat2", "member_ids": [uid]}, headers=auth)
|
||||
chat2_id = payload["id"]
|
||||
bulk_insert_messages(chat2_id, uid, 10000)
|
||||
|
||||
cursor_row = psql(f"""
|
||||
SELECT id::text, created_at::text FROM messages
|
||||
WHERE chat_id = '{chat_id}'::uuid
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 1 OFFSET 99950
|
||||
""", tuples=True).strip()
|
||||
cursor_id, cursor_ts_raw = cursor_row.split("|")
|
||||
cursor_ts = datetime.fromisoformat(cursor_ts_raw).isoformat()
|
||||
|
||||
log("EXPLAIN ANALYZE for idx_messages_chat_created_id")
|
||||
plan_a = explain(chat_id, cursor_ts, cursor_id, index_name="idx_messages_chat_created_id")
|
||||
log(plan_a)
|
||||
|
||||
log("EXPLAIN ANALYZE for idx_messages_chat_id_created_at")
|
||||
plan_b = explain(chat_id, cursor_ts, cursor_id, index_name="idx_messages_chat_id_created_at")
|
||||
log(plan_b)
|
||||
|
||||
backup_restore()
|
||||
|
||||
with open(OUTFILE, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines))
|
||||
log(f"\nResults written to {OUTFILE}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,173 @@
|
||||
"""EXPLAIN ANALYZE до/после на 10 000+ сообщений.
|
||||
|
||||
Сравнивает план OR-курсорa (было) и row-comparison (стало).
|
||||
Результаты сохраняются в evidence/explain_pagination.txt.
|
||||
|
||||
Запуск (стек поднят):
|
||||
|
||||
python student-tests/explain_pagination.py
|
||||
"""
|
||||
import base64
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from _harness import BASE_URL, call
|
||||
|
||||
EVIDENCE = os.path.join(os.path.dirname(__file__), "..", "evidence")
|
||||
os.makedirs(EVIDENCE, exist_ok=True)
|
||||
OUTFILE = os.path.join(EVIDENCE, "explain_pagination.txt")
|
||||
|
||||
HEADER = "=" * 70
|
||||
lines = []
|
||||
|
||||
|
||||
def log(text):
|
||||
lines.append(text)
|
||||
print(text)
|
||||
|
||||
|
||||
def psql(sql, tuples=False):
|
||||
"""Run SQL via docker compose exec psql. Return stdout.
|
||||
|
||||
tuples=True — режим `-tA` (без заголовков/выравнивания), удобен для
|
||||
разбора значений в Python. Для EXPLAIN оставляем обычный вывод.
|
||||
"""
|
||||
cmd = [
|
||||
"docker", "compose", "exec", "-T", "postgres", "psql",
|
||||
"-U", "messenger", "-d", "messenger",
|
||||
]
|
||||
if tuples:
|
||||
cmd += ["-tA"]
|
||||
cmd += ["-c", sql]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"psql error: {result.stderr[:500]}")
|
||||
return result.stdout
|
||||
|
||||
|
||||
# ---- Step 1: user + session + chat ------------------------------------------
|
||||
log(HEADER)
|
||||
log("Step 1: register user and create chat")
|
||||
log(HEADER)
|
||||
|
||||
status, payload, _ = call("POST", "/api/v1/users", body={
|
||||
"username": "explain_user",
|
||||
"display_name": "EXPLAIN",
|
||||
"password": "ExplPass123!",
|
||||
})
|
||||
if status == 201:
|
||||
uid = payload["id"]
|
||||
log("Registered new user")
|
||||
else:
|
||||
cred = base64.b64encode(b"explain_user:ExplPass123!").decode()
|
||||
status, payload, _ = call("POST", "/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + cred})
|
||||
uid = payload["user"]["id"]
|
||||
log(f"User already exists, uid = {uid}")
|
||||
|
||||
cred = base64.b64encode(b"explain_user:ExplPass123!").decode()
|
||||
status, payload, _ = call("POST", "/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + cred})
|
||||
token = payload["token"]
|
||||
auth = {"Authorization": "Bearer " + token}
|
||||
|
||||
status, payload, _ = call("POST", "/api/v1/chats",
|
||||
body={"title": "explain_test", "member_ids": [uid]},
|
||||
headers=auth)
|
||||
assert status == 201, f"chat creation failed: {status} {payload}"
|
||||
chat_id = payload["id"]
|
||||
log(f"chat_id = {chat_id}")
|
||||
|
||||
# ---- Step 2: bulk-insert 10 000 messages ------------------------------------
|
||||
log(HEADER)
|
||||
log("Step 2: bulk-insert 10 000 messages via psql")
|
||||
log(HEADER)
|
||||
|
||||
psql(f"""
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
'{chat_id}'::uuid,
|
||||
'{uid}'::uuid,
|
||||
'msg-' || generate_series(1, 10000),
|
||||
now() - (generate_series(1, 10000) || ' seconds')::interval
|
||||
""")
|
||||
out = psql(f"SELECT count(*) FROM messages WHERE chat_id = '{chat_id}'::uuid", tuples=True)
|
||||
log(f"Message count: {out.strip()}")
|
||||
|
||||
# ---- Step 3: cursor boundary (row 9950) ------------------------------------
|
||||
log(HEADER)
|
||||
log("Step 3: cursor boundary at row 9950 (late page)")
|
||||
log(HEADER)
|
||||
|
||||
row = psql(f"""
|
||||
SELECT id::text, created_at::text FROM messages
|
||||
WHERE chat_id = '{chat_id}'::uuid
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 1 OFFSET 9950
|
||||
""", tuples=True).strip()
|
||||
parts = row.split("|")
|
||||
cursor_id = parts[0].strip()
|
||||
cursor_ts_raw = parts[1].strip()
|
||||
cursor_dt = datetime.fromisoformat(cursor_ts_raw)
|
||||
cursor_ts = cursor_dt.isoformat()
|
||||
log(f"cursor_id = {cursor_id}")
|
||||
log(f"cursor_ts = {cursor_ts}")
|
||||
|
||||
# ---- Step 4: EXPLAIN ANALYZE — OLD (OR form) --------------------------------
|
||||
log(HEADER)
|
||||
log("Step 4: EXPLAIN ANALYZE — OLD form (OR cursor)")
|
||||
log(HEADER)
|
||||
|
||||
old_sql = f"""
|
||||
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '{chat_id}'::uuid
|
||||
AND (created_at > '{cursor_ts}'::timestamptz
|
||||
OR (created_at = '{cursor_ts}'::timestamptz
|
||||
AND id > '{cursor_id}'::uuid))
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 51
|
||||
"""
|
||||
log(psql(old_sql))
|
||||
|
||||
# ---- Step 5: EXPLAIN ANALYZE — NEW (row-comparison) -------------------------
|
||||
log(HEADER)
|
||||
log("Step 5: EXPLAIN ANALYZE — NEW form (row-comparison / Index Cond)")
|
||||
log(HEADER)
|
||||
|
||||
new_sql = f"""
|
||||
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '{chat_id}'::uuid
|
||||
AND (created_at, id) > ('{cursor_ts}'::timestamptz, '{cursor_id}'::uuid)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT 51
|
||||
"""
|
||||
log(psql(new_sql))
|
||||
|
||||
# ---- Step 6: HTTP timing ----------------------------------------------------
|
||||
log(HEADER)
|
||||
log("Step 6: HTTP timing — late page via API")
|
||||
log(HEADER)
|
||||
|
||||
cursor_value = f"{cursor_ts}|{cursor_id}"
|
||||
cursor_encoded = base64.urlsafe_b64encode(cursor_value.encode()).decode()
|
||||
log(f"API cursor (base64url): {cursor_encoded[:40]}...")
|
||||
|
||||
status, payload, elapsed = call(
|
||||
"GET",
|
||||
f"/api/v1/chats/{chat_id}/messages?limit=100&cursor={cursor_encoded}",
|
||||
headers=auth,
|
||||
)
|
||||
log(f"API response: {status} in {elapsed:.4f}s, {len(payload.get('items', []))} items")
|
||||
|
||||
# ---- Write output -----------------------------------------------------------
|
||||
with open(OUTFILE, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines))
|
||||
log(f"\nResults written to {OUTFILE}")
|
||||
@@ -0,0 +1,156 @@
|
||||
"""Автотест отказа PostgreSQL.
|
||||
|
||||
Проверяет, что при остановленной базе API:
|
||||
|
||||
* отвечает за ограниченное время (не «висит»);
|
||||
* отдаёт 503 в общем формате ошибки, а не 500 и не пустое соединение;
|
||||
* /health/live остаётся 200 (процесс жив), /health/ready — 503;
|
||||
* анонимный запрос по-прежнему получает 401 (аутентификация до БД);
|
||||
* после возврата базы восстанавливается: readiness 200, прежний Bearer-токен
|
||||
работает, подтверждённое до отказа сообщение читается.
|
||||
|
||||
Запуск (стек должен быть поднят):
|
||||
|
||||
python student-tests/test_postgres_down.py
|
||||
"""
|
||||
import base64
|
||||
import uuid
|
||||
|
||||
from _harness import (
|
||||
assert_error_payload,
|
||||
assert_within_budget,
|
||||
call,
|
||||
container_id,
|
||||
start_service,
|
||||
stop_service,
|
||||
wait_healthy,
|
||||
wait_ready,
|
||||
)
|
||||
|
||||
|
||||
def register_and_login(username, password):
|
||||
status, payload, _ = call(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
body={
|
||||
"username": username,
|
||||
"display_name": "Postgres Down Test",
|
||||
"password": password,
|
||||
},
|
||||
)
|
||||
assert status == 201, "register failed: %s %s" % (status, payload)
|
||||
|
||||
credential = base64.b64encode(
|
||||
("%s:%s" % (username, password)).encode()
|
||||
).decode()
|
||||
status, payload, _ = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + credential},
|
||||
)
|
||||
assert status == 201, "login failed: %s %s" % (status, payload)
|
||||
return payload["token"]
|
||||
|
||||
|
||||
def main():
|
||||
postgres = container_id("postgres")
|
||||
wait_healthy(postgres)
|
||||
wait_ready()
|
||||
|
||||
suffix = uuid.uuid4().hex[:8]
|
||||
token = register_and_login("pgdown_" + suffix, "PgDownPass123")
|
||||
auth = {"Authorization": "Bearer " + token}
|
||||
|
||||
status, user, _ = call("GET", "/api/v1/users/me", headers=auth)
|
||||
assert status == 200, "token must work before the outage: %s %s" % (status, user)
|
||||
|
||||
status, chat, _ = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
body={"title": "Postgres outage", "member_ids": [user["id"]]},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 201, "chat creation failed: %s %s" % (status, chat)
|
||||
|
||||
status, message, _ = call(
|
||||
"POST",
|
||||
"/api/v1/chats/%s/messages" % chat["id"],
|
||||
body={"text": "подтверждено до отказа"},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 201, "message creation failed: %s %s" % (status, message)
|
||||
print("Setup: user, chat and message created")
|
||||
|
||||
try:
|
||||
stop_service("postgres")
|
||||
print("PostgreSQL stopped")
|
||||
|
||||
status, payload, elapsed = call("GET", "/health/ready")
|
||||
assert_within_budget(elapsed, "readiness with PostgreSQL down")
|
||||
assert status == 503, "readiness must be 503, got %s %s" % (status, payload)
|
||||
assert_error_payload(payload, "unavailable")
|
||||
print("readiness: 503 in %.2fs (budget %.0fs)" % (elapsed, 6.0))
|
||||
|
||||
status, payload, elapsed = call("GET", "/health/live")
|
||||
assert status == 200, "liveness must stay 200, got %s" % status
|
||||
print("liveness: 200")
|
||||
|
||||
status, payload, elapsed = call(
|
||||
"GET", "/api/v1/users/me", headers=auth
|
||||
)
|
||||
assert_within_budget(elapsed, "authenticated request with PostgreSQL down")
|
||||
assert status == 503, (
|
||||
"authenticated request must be 503, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_error_payload(payload, "unavailable")
|
||||
print("authenticated request: 503 in %.2fs" % elapsed)
|
||||
|
||||
status, payload, elapsed = call("GET", "/api/v1/users/me")
|
||||
assert status == 401, (
|
||||
"anonymous request must stay 401, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_within_budget(elapsed, "anonymous request with PostgreSQL down")
|
||||
print("anonymous request: 401 (no trust granted without token)")
|
||||
|
||||
status, payload, elapsed = call(
|
||||
"POST",
|
||||
"/api/v1/chats/%s/messages" % chat["id"],
|
||||
body={"text": "во время отказа"},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 503, (
|
||||
"write during outage must be 503, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_within_budget(elapsed, "write with PostgreSQL down")
|
||||
print("write during outage: 503 in %.2fs, message not acknowledged" % elapsed)
|
||||
finally:
|
||||
start_service("postgres")
|
||||
wait_healthy(postgres)
|
||||
print("PostgreSQL restarted")
|
||||
|
||||
wait_ready()
|
||||
print("readiness after recovery: 200")
|
||||
|
||||
status, payload, _ = call("GET", "/api/v1/users/me", headers=auth)
|
||||
assert status == 200, (
|
||||
"existing token must work after recovery, got %s %s" % (status, payload)
|
||||
)
|
||||
print("existing token after recovery: 200")
|
||||
|
||||
status, page, _ = call(
|
||||
"GET", "/api/v1/chats/%s/messages?limit=100" % chat["id"], headers=auth
|
||||
)
|
||||
assert status == 200, "history read failed after recovery: %s" % status
|
||||
identifiers = [item["id"] for item in page["items"]]
|
||||
assert message["id"] in identifiers, "confirmed message disappeared after outage"
|
||||
assert len(identifiers) == len(set(identifiers)), "history contains duplicates"
|
||||
assert "во время отказа" not in [item["text"] for item in page["items"]], (
|
||||
"unacknowledged message must not appear in history"
|
||||
)
|
||||
print("confirmed message preserved, unacknowledged write absent")
|
||||
|
||||
print("PASS: PostgreSQL outage handled with bounded 503 and full recovery")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,65 +1,167 @@
|
||||
"""Test: when Redis is down, protected endpoints return 503, not 500."""
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
"""Автотест отказа Redis (хранилища сессий).
|
||||
|
||||
Зеркалит student-tests/test_postgres_down.py: проверяется тот же контракт
|
||||
отказа, но деградирует Redis, а не PostgreSQL.
|
||||
|
||||
При остановленном Redis API:
|
||||
|
||||
* /health/live остаётся 200 (процесс жив), /health/ready — 503;
|
||||
* анонимный запрос получает 401 (аутентификация не зависит от Redis);
|
||||
* запрос с прежним токеном получает 503 в общем формате ошибки, а не 401
|
||||
(«нет сессии») и не 500 — недоступность не превращается в отказ доступа;
|
||||
* чтение и запись с токеном — 503 за ограниченное время;
|
||||
* после возврата Redis readiness 200, прежний токен снова работает,
|
||||
подтверждённые данные читаются, неподтверждённая запись отсутствует.
|
||||
|
||||
Запуск (стек должен быть поднят):
|
||||
|
||||
python student-tests/test_redis_down.py
|
||||
"""
|
||||
import base64
|
||||
import uuid
|
||||
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
from _harness import (
|
||||
RESPONSE_BUDGET_SECONDS,
|
||||
assert_error_payload,
|
||||
assert_within_budget,
|
||||
call,
|
||||
container_id,
|
||||
start_service,
|
||||
stop_service,
|
||||
wait_healthy,
|
||||
wait_ready,
|
||||
)
|
||||
|
||||
|
||||
def call(method, path, headers=None):
|
||||
h = {"Accept": "application/json"}
|
||||
h.update(headers or {})
|
||||
req = urllib.request.Request(BASE + path, headers=h, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
return resp.status, resp.read().decode()
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read().decode()
|
||||
def register_and_login(username, password):
|
||||
"""Создать пользователя, войти, вернуть (token, user_id)."""
|
||||
status, payload, _ = call(
|
||||
"POST",
|
||||
"/api/v1/users",
|
||||
body={
|
||||
"username": username,
|
||||
"display_name": "Redis Down Test",
|
||||
"password": password,
|
||||
},
|
||||
)
|
||||
assert status == 201, "register failed: %s %s" % (status, payload)
|
||||
user_id = payload["id"]
|
||||
|
||||
|
||||
def stop_redis():
|
||||
subprocess.run(["docker", "compose", "stop", "redis"], check=True, capture_output=True)
|
||||
|
||||
|
||||
def start_redis():
|
||||
subprocess.run(["docker", "compose", "start", "redis"], check=True, capture_output=True)
|
||||
# Ждём, пока Redis станет healthy
|
||||
for _ in range(30):
|
||||
p = subprocess.run(
|
||||
["docker", "inspect", "--format={{.State.Health.Status}}", "mtusi2026lab2-redis-1"],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
if p.stdout.strip() == "healthy":
|
||||
return
|
||||
time.sleep(1)
|
||||
raise RuntimeError("redis did not become healthy")
|
||||
credential = base64.b64encode(
|
||||
("%s:%s" % (username, password)).encode()
|
||||
).decode()
|
||||
status, payload, _ = call(
|
||||
"POST",
|
||||
"/api/v1/auth/sessions",
|
||||
headers={"Authorization": "Basic " + credential},
|
||||
)
|
||||
assert status == 201, "login failed: %s %s" % (status, payload)
|
||||
return payload["token"], user_id
|
||||
|
||||
|
||||
def main():
|
||||
# Проверяем, что без токена всё ещё 401 (не 500)
|
||||
redis_container = container_id("redis")
|
||||
wait_healthy(redis_container)
|
||||
wait_ready()
|
||||
|
||||
suffix = uuid.uuid4().hex[:8]
|
||||
token, user_id = register_and_login("redisdown_" + suffix, "RedisDownPass123")
|
||||
auth = {"Authorization": "Bearer " + token}
|
||||
|
||||
status, me, _ = call("GET", "/api/v1/users/me", headers=auth)
|
||||
assert status == 200, "token must work before the outage: %s %s" % (status, me)
|
||||
|
||||
status, chat, _ = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
body={"title": "Redis outage", "member_ids": [user_id]},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 201, "chat creation failed: %s %s" % (status, chat)
|
||||
|
||||
status, message, _ = call(
|
||||
"POST",
|
||||
"/api/v1/chats/%s/messages" % chat["id"],
|
||||
body={"text": "подтверждено до отказа Redis"},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 201, "message creation failed: %s %s" % (status, message)
|
||||
print("Setup: user, session, chat and message created")
|
||||
|
||||
try:
|
||||
stop_redis()
|
||||
time.sleep(2)
|
||||
stop_service("redis")
|
||||
print("Redis stopped")
|
||||
|
||||
# 1. Аноним не должен пройти (401, не 503 — токена нет)
|
||||
status, body = call("GET", "/api/v1/users/me")
|
||||
assert status == 401, f"anonymous request without token should be 401, got {status} {body}"
|
||||
print("Anonymous without token: 401 (correct)")
|
||||
status, payload, elapsed = call("GET", "/health/ready")
|
||||
assert_within_budget(elapsed, "readiness with Redis down")
|
||||
assert status == 503, "readiness must be 503, got %s %s" % (status, payload)
|
||||
assert_error_payload(payload, "unavailable")
|
||||
print(
|
||||
"readiness: 503 in %.2fs (budget %.0fs)"
|
||||
% (elapsed, RESPONSE_BUDGET_SECONDS)
|
||||
)
|
||||
|
||||
# 2. С фиктивным токеном — 503 (Redis недоступен), не 500
|
||||
status, body = call("GET", "/api/v1/users/me", headers={
|
||||
"Authorization": "Bearer faketoken12345",
|
||||
})
|
||||
assert status == 503, f"with fake token and Redis down should be 503, got {status} {body}"
|
||||
print("With token, Redis down: 503 (correct)")
|
||||
print("PASS: Redis unavailability handled correctly")
|
||||
status, payload, elapsed = call("GET", "/health/live")
|
||||
assert status == 200, "liveness must stay 200, got %s" % status
|
||||
print("liveness: 200")
|
||||
|
||||
status, payload, elapsed = call("GET", "/api/v1/users/me")
|
||||
assert status == 401, (
|
||||
"anonymous request must stay 401, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_within_budget(elapsed, "anonymous request with Redis down")
|
||||
print("anonymous request: 401 (missing credentials, not a store failure)")
|
||||
|
||||
status, payload, elapsed = call("GET", "/api/v1/users/me", headers=auth)
|
||||
assert_within_budget(elapsed, "authenticated request with Redis down")
|
||||
assert status == 503, (
|
||||
"authenticated request must be 503, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_error_payload(payload, "unavailable")
|
||||
print("authenticated request: 503 in %.2fs (not 401: session may exist)" % elapsed)
|
||||
|
||||
status, payload, elapsed = call(
|
||||
"POST",
|
||||
"/api/v1/chats/%s/messages" % chat["id"],
|
||||
body={"text": "во время отказа Redis"},
|
||||
headers=auth,
|
||||
)
|
||||
assert status == 503, (
|
||||
"write during outage must be 503, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_within_budget(elapsed, "write with Redis down")
|
||||
print("write during outage: 503 in %.2fs, message not acknowledged" % elapsed)
|
||||
finally:
|
||||
start_redis()
|
||||
start_service("redis")
|
||||
wait_healthy(redis_container)
|
||||
print("Redis restarted")
|
||||
|
||||
wait_ready()
|
||||
print("readiness after recovery: 200")
|
||||
|
||||
status, payload, elapsed = call("GET", "/api/v1/users/me", headers=auth)
|
||||
assert status == 200, (
|
||||
"existing token must work after recovery, got %s %s" % (status, payload)
|
||||
)
|
||||
assert_within_budget(elapsed, "authenticated request after recovery")
|
||||
print("existing token after recovery: 200 (session survived in AOF)")
|
||||
|
||||
status, page, elapsed = call(
|
||||
"GET", "/api/v1/chats/%s/messages?limit=100" % chat["id"], headers=auth
|
||||
)
|
||||
assert status == 200, "history read failed after recovery: %s" % status
|
||||
assert_within_budget(elapsed, "history read after recovery")
|
||||
identifiers = [item["id"] for item in page["items"]]
|
||||
assert message["id"] in identifiers, "confirmed message disappeared after outage"
|
||||
assert len(identifiers) == len(set(identifiers)), "history contains duplicates"
|
||||
texts = [item["text"] for item in page["items"]]
|
||||
assert "во время отказа Redis" not in texts, (
|
||||
"unacknowledged message must not appear in history"
|
||||
)
|
||||
print("confirmed message preserved, unacknowledged write absent")
|
||||
|
||||
print("PASS: Redis outage handled with bounded 503 and full recovery")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
"""Test: session expires after SESSION_TTL_SECONDS."""
|
||||
"""Test: session expires after SESSION_TTL_SECONDS.
|
||||
|
||||
Тест требует короткого TTL: запустите стенд с SESSION_TTL_SECONDS=2
|
||||
(например, временно изменив .env и выполнив `docker compose restart api`).
|
||||
|
||||
Запуск:
|
||||
|
||||
python student-tests/test_session_expiry.py
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
|
||||
EXPECTED_TTL = int(os.environ.get("EXPECTED_TTL_SECONDS", "2"))
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
EXPECTED_TTL = 2 # секунды
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
|
||||
Reference in New Issue
Block a user