Перенести API с общими сессиями и идемпотентной отправкой сообщения.

Реплика больше не держит корректность в своей памяти: сессия в Redis, а повтор POST сообщения фиксируется в Postgres вместе с X-Instance-Id.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-07 12:46:37 +03:00
co-authored by Cursor
parent 0ab3a341aa
commit c0d313703e
34 changed files with 2342 additions and 7 deletions
+29
View File
@@ -0,0 +1,29 @@
FROM python:3.12.8-slim-bookworm AS runtime
WORKDIR /app
RUN groupadd --system app && useradd --system --gid app --home /app --shell /usr/sbin/nologin app \
&& apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app ./app
COPY migrations ./migrations
COPY contracts/openapi.json ./contracts/openapi.json
USER app
ENV APP_HOST=0.0.0.0 \
APP_PORT=8080 \
APP_ENV=lab \
PYTHONUNBUFFERED=1
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=3s --start-period=15s --retries=12 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"
CMD ["python", "-m", "app.main"]
+25 -7
View File
@@ -4,27 +4,45 @@ CA_FILE ?=
ACTION_SCRIPT ?=
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
.PHONY: help check validate test load test-persistence test-failover
.PHONY: help check validate test load test-persistence test-failover test-student test-infra test-expiry explain evidence-grade4 evidence-track1
help:
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
check:
$(PYTHON) scripts/check_contract.py
$(PYTHON) -X utf8 scripts/check_contract.py
validate:
$(PYTHON) scripts/validate_openapi.py
$(PYTHON) -X utf8 scripts/validate_openapi.py
test:
$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
$(PYTHON) -X utf8 tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
load:
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
$(PYTHON) -X utf8 tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
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)
$(PYTHON) -X utf8 tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
test-failover:
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
$(PYTHON) -X utf8 tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
test-student:
$(PYTHON) -m pytest student_tests -q -m "not infra and not expiry"
test-infra:
RUN_INFRA=1 $(PYTHON) -m pytest student_tests/test_infra.py -q -m infra
test-expiry:
$(PYTHON) scripts/run_expiry.py
explain:
docker compose --env-file .env exec -T api python -m app.tools.explain
evidence-grade4:
$(PYTHON) -X utf8 scripts/grade4_evidence.py
evidence-track1:
$(PYTHON) -X utf8 scripts/track1_experiment.py
+1
View File
@@ -0,0 +1 @@
"""Messenger API (lab 2): PostgreSQL source of truth, Redis sessions/cache."""
+151
View File
@@ -0,0 +1,151 @@
import asyncio
import json
from collections.abc import Awaitable, Callable
from typing import TypeVar
from app import config, rediscli
from app.errors import AppError
T = TypeVar("T")
CFG_SINGLEFLIGHT = "cfg:singleflight"
CFG_INVALIDATE = "cfg:invalidate"
CFG_DELAY_MS = "cfg:delay_ms"
SQL_HISTORY = "metrics:sql:messages_select"
class SingleFlight:
def __init__(self) -> None:
self._lock = asyncio.Lock()
self._inflight: dict[str, asyncio.Task] = {}
async def do(self, key: str, factory: Callable[[], Awaitable[T]]) -> T:
async with self._lock:
task = self._inflight.get(key)
if task is None:
task = asyncio.create_task(factory())
self._inflight[key] = task
owner = True
else:
owner = False
try:
return await asyncio.shield(task)
finally:
if owner:
async with self._lock:
self._inflight.pop(key, None)
_sf = SingleFlight()
async def _flag(name: str, default: bool = True) -> bool:
if not config.lab_mode():
return default
try:
raw = await rediscli.require().get(name)
except Exception:
return default
if raw is None:
return default
return raw not in {"0", "false", "off"}
async def singleflight_enabled() -> bool:
return await _flag(CFG_SINGLEFLIGHT, True)
async def invalidate_enabled() -> bool:
return await _flag(CFG_INVALIDATE, True)
async def incr_history_sql() -> None:
try:
await rediscli.require().incr(SQL_HISTORY)
except Exception:
pass
async def history_sql_count() -> int:
try:
return int(await rediscli.require().get(SQL_HISTORY) or 0)
except Exception:
return 0
async def reset_history_sql_count() -> None:
try:
await rediscli.require().set(SQL_HISTORY, 0)
except Exception:
pass
async def load_delay_seconds() -> float:
if not config.lab_mode():
return 0.0
try:
raw = await rediscli.require().get(CFG_DELAY_MS)
except Exception:
return 0.0
try:
return max(0.0, float(raw or 0) / 1000.0)
except (TypeError, ValueError):
return 0.0
async def set_flags(
*,
singleflight: bool | None = None,
invalidate: bool | None = None,
delay_ms: int | None = None,
) -> dict:
r = rediscli.require()
if singleflight is not None:
await r.set(CFG_SINGLEFLIGHT, "1" if singleflight else "0")
if invalidate is not None:
await r.set(CFG_INVALIDATE, "1" if invalidate else "0")
if delay_ms is not None:
await r.set(CFG_DELAY_MS, str(int(delay_ms)))
return {
"singleflight": await singleflight_enabled(),
"invalidate": await invalidate_enabled(),
"history_sql": await history_sql_count(),
"delay_ms": int((await load_delay_seconds()) * 1000),
}
async def get_history_page(chat_id: str, cursor: str, limit: int, loader: Callable[[], Awaitable[dict]]) -> dict:
r = rediscli.require()
try:
version = await r.get("histver:" + chat_id) or "0"
key = f"hist:{chat_id}:{version}:{cursor}:{limit}"
cached = await r.get(key)
if cached:
return json.loads(cached)
async def load_and_store() -> dict:
delay = await load_delay_seconds()
if delay:
await asyncio.sleep(delay)
page = await loader()
try:
await r.set(key, json.dumps(page, ensure_ascii=False), ex=config.CACHE_TTL_SECONDS)
except Exception:
pass
return page
if await singleflight_enabled():
return await _sf.do(key, load_and_store)
return await load_and_store()
except AppError:
raise
except Exception:
return await loader()
async def invalidate_history(chat_id: str) -> None:
if not await invalidate_enabled():
return
try:
await rediscli.require().incr("histver:" + chat_id)
except Exception:
pass
+42
View File
@@ -0,0 +1,42 @@
import os
import socket
from pathlib import Path
def _env(name: str, default: str = "") -> str:
value = os.environ.get(name)
return default if value is None or value == "" else value
def _file_or_env(name: str, default: str = "") -> str:
path = os.environ.get(name + "_FILE")
if path:
return Path(path).read_text(encoding="utf-8").strip()
return _env(name, default)
HOST = _env("APP_HOST", "0.0.0.0")
PORT = int(_env("APP_PORT", "8080"))
ENV = _env("APP_ENV", "lab")
LOG_LEVEL = _env("LOG_LEVEL", "info").lower()
SHUTDOWN_GRACE_SECONDS = float(_env("SHUTDOWN_GRACE_SECONDS", "10"))
SESSION_TTL_SECONDS = int(_env("SESSION_TTL_SECONDS", "3600"))
CACHE_TTL_SECONDS = int(_env("CACHE_TTL_SECONDS", "60"))
DATABASE_URL = _file_or_env("DATABASE_URL")
REDIS_URL = _file_or_env("REDIS_URL", "redis://redis:6379/0")
SESSION_STORE = _env("SESSION_STORE", "redis")
DB_POOL_MIN = int(_env("DB_POOL_MIN", "1"))
DB_POOL_MAX = int(_env("DB_POOL_MAX", "10"))
DB_COMMAND_TIMEOUT = float(_env("DB_COMMAND_TIMEOUT", "5"))
DB_CONNECT_TIMEOUT = float(_env("DB_CONNECT_TIMEOUT", "5"))
REDIS_SOCKET_TIMEOUT = float(_env("REDIS_SOCKET_TIMEOUT", "2"))
IDEMPOTENCY_TTL_SECONDS = int(_env("IDEMPOTENCY_TTL_SECONDS", "86400"))
MIGRATIONS_ON_START = _env("MIGRATIONS_ON_START", "1") == "1"
LB_DRAIN_SECONDS = float(_env("LB_DRAIN_SECONDS", "2"))
# Stable for the life of this process. Compose leaves it unset so each
# container uses its own hostname; stop-one.sh maps that value to one replica.
INSTANCE_ID = _env("INSTANCE_ID", socket.gethostname())
def lab_mode() -> bool:
return ENV == "lab"
+112
View File
@@ -0,0 +1,112 @@
import base64
import json
from datetime import datetime
from typing import Any
from uuid import UUID
def encode_cursor(chat_id: str, created_at: str, entity_id: str) -> str:
payload = json.dumps({"c": chat_id, "t": created_at, "i": entity_id}, separators=(",", ":"))
return base64.urlsafe_b64encode(payload.encode("utf-8")).decode("ascii").rstrip("=")
def _validate_cursor_timestamp(value: str) -> None:
if not value.endswith("Z"):
raise ValueError("invalid cursor timestamp")
try:
datetime.fromisoformat(value[:-1] + "+00:00")
except ValueError as exc:
raise ValueError("invalid cursor timestamp") from exc
def _require_str_field(data: dict, key: str) -> str:
if key not in data:
raise ValueError("invalid cursor")
value = data[key]
if not isinstance(value, str):
raise ValueError("invalid cursor")
return value
def decode_cursor(cursor: str, expected_chat_id: str) -> tuple[str, str]:
pad = "=" * (-len(cursor) % 4)
try:
raw = base64.urlsafe_b64decode((cursor + pad).encode("ascii"))
data: Any = json.loads(raw.decode("utf-8"))
except (ValueError, json.JSONDecodeError, UnicodeDecodeError) as exc:
raise ValueError("invalid cursor") from exc
if not isinstance(data, dict):
raise ValueError("invalid cursor")
try:
chat_id = _require_str_field(data, "c")
created_at = _require_str_field(data, "t")
entity_id = _require_str_field(data, "i")
except ValueError as exc:
raise ValueError("invalid cursor") from exc
if chat_id != expected_chat_id:
raise ValueError("cursor chat mismatch")
try:
UUID(entity_id)
except ValueError as exc:
raise ValueError("invalid cursor") from exc
try:
_validate_cursor_timestamp(created_at)
except ValueError as exc:
raise ValueError("invalid cursor") from exc
return created_at, entity_id
+87
View File
@@ -0,0 +1,87 @@
from pathlib import Path
import asyncpg
from app import config
from app.errors import AppError
pool: asyncpg.Pool | None = None
INTEGRITY = (
asyncpg.UniqueViolationError,
asyncpg.ForeignKeyViolationError,
asyncpg.CheckViolationError,
)
def _migrations_dir() -> Path:
here = Path(__file__).resolve().parent
for candidate in (here.parent / "migrations", Path("/app/migrations")):
if candidate.is_dir():
return candidate
raise RuntimeError("migrations directory not found")
async def connect() -> asyncpg.Pool:
global pool
if not config.DATABASE_URL:
raise RuntimeError("DATABASE_URL is not set")
pool = await asyncpg.create_pool(
dsn=config.DATABASE_URL,
min_size=config.DB_POOL_MIN,
max_size=config.DB_POOL_MAX,
command_timeout=config.DB_COMMAND_TIMEOUT,
timeout=config.DB_CONNECT_TIMEOUT,
)
return pool
async def close() -> None:
global pool
if pool is not None:
await pool.close()
pool = None
def require() -> asyncpg.Pool:
if pool is None:
raise AppError(503, "service unavailable")
return pool
async def ping() -> None:
try:
async with require().acquire() as conn:
await conn.execute("SELECT 1")
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
_MIGRATION_LOCK = 87133103
async def apply_migrations() -> None:
"""Apply pending SQL files once. A session advisory lock stops replica races."""
directory = _migrations_dir()
async with require().acquire() as conn:
await conn.execute("SELECT pg_advisory_lock($1)", _MIGRATION_LOCK)
try:
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS schema_migrations (
filename TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
)
"""
)
applied = {row["filename"] for row in await conn.fetch("SELECT filename FROM schema_migrations")}
for path in sorted(p for p in directory.glob("*.sql") if p.is_file()):
if path.name in applied:
continue
sql = path.read_text(encoding="utf-8")
async with conn.transaction():
await conn.execute(sql)
await conn.execute("INSERT INTO schema_migrations(filename) VALUES($1)", path.name)
finally:
await conn.execute("SELECT pg_advisory_unlock($1)", _MIGRATION_LOCK)
+52
View File
@@ -0,0 +1,52 @@
import json
from functools import lru_cache
from pathlib import Path
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse, Response
_SPEC_CANDIDATES = (
Path(__file__).resolve().parents[1] / "contracts" / "openapi.json",
Path("/app/contracts/openapi.json"),
)
_SWAGGER_HTML = """<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8"/>
<title>MTUSI Messenger — Swagger (lab2)</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css"/>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js"></script>
<script>
window.onload = function () {
SwaggerUIBundle({
url: "/openapi.json",
dom_id: "#swagger-ui",
deepLinking: true,
persistAuthorization: true,
tryItOutEnabled: true
});
};
</script>
</body>
</html>
"""
@lru_cache(maxsize=1)
def _load_spec() -> dict:
for path in _SPEC_CANDIDATES:
if path.is_file():
return json.loads(path.read_text(encoding="utf-8"))
return {"openapi": "3.1.0", "info": {"title": "MTUSI Messenger", "version": "1.2.0"}, "paths": {}}
async def openapi_json(_: Request) -> Response:
return JSONResponse(_load_spec())
async def swagger_ui(_: Request) -> Response:
return HTMLResponse(_SWAGGER_HTML)
+49
View File
@@ -0,0 +1,49 @@
from typing import Dict, Optional
from starlette.responses import JSONResponse
ERROR_CODES = {
400: "invalid_request",
401: "unauthorized",
404: "not_found",
409: "conflict",
413: "payload_too_large",
415: "unsupported_media_type",
429: "rate_limited",
503: "unavailable",
500: "internal_error",
}
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
BEARER_CHALLENGE = 'Bearer realm="messenger"'
class AppError(Exception):
def __init__(self, status: int, message: str, www_authenticate: str | None = None) -> None:
super().__init__(message)
self.status = status
self.message = message
self.www_authenticate = www_authenticate
def error_response(
status: int,
message: str,
request_id: str,
extra_headers: Optional[Dict[str, str]] = None,
) -> JSONResponse:
code = ERROR_CODES.get(status, "internal_error")
headers = {"X-Request-Id": request_id, "Content-Type": "application/json"}
if extra_headers:
headers.update(extra_headers)
return JSONResponse(
status_code=status,
headers=headers,
content={"error": {"code": code, "message": message, "request_id": request_id}},
)
def from_app_error(exc: AppError, request_id: str) -> JSONResponse:
extra = {"WWW-Authenticate": exc.www_authenticate} if exc.www_authenticate else None
return error_response(exc.status, exc.message, request_id, extra)
+146
View File
@@ -0,0 +1,146 @@
import asyncio
import logging
import signal
import sys
import uuid
from pathlib import Path
import uvicorn
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
from app import config, db, rediscli
from app.docs import openapi_json, swagger_ui
from app.errors import AppError, error_response, from_app_error
from app.middleware import RequestContextMiddleware
from app.routes import (
create_chat,
create_message,
create_session,
current_user,
get_chat,
health_live,
health_ready,
lab_flags,
lab_metrics,
list_chats,
list_messages,
register_user,
revoke_session,
)
def _request_id(request: Request) -> str:
existing = getattr(request.state, "request_id", None)
if existing:
return existing
return request.headers.get("X-Request-Id") or str(uuid.uuid4())
async def not_found_handler(request: Request, exc: HTTPException) -> Response:
return error_response(404, "not found", _request_id(request))
async def app_error_handler(request: Request, exc: AppError) -> Response:
return from_app_error(exc, _request_id(request))
def publish_instance_id() -> None:
path = Path(config._env("INSTANCE_ID_FILE", "/tmp/instance-id"))
try:
path.write_text(config.INSTANCE_ID, encoding="utf-8")
except OSError:
logging.getLogger("messenger").warning("instance id file was not written")
async def startup() -> None:
publish_instance_id()
logging.getLogger("messenger").info("instance_id=%s", config.INSTANCE_ID)
await db.connect()
if config.MIGRATIONS_ON_START:
await db.apply_migrations()
await rediscli.connect()
async def shutdown() -> None:
await rediscli.close()
await db.close()
def build_app() -> Starlette:
app = Starlette(
routes=[
Route("/docs", swagger_ui, methods=["GET"]),
Route("/openapi.json", openapi_json, methods=["GET"]),
Route("/health/live", health_live, methods=["GET"]),
Route("/health/ready", health_ready, methods=["GET"]),
Route("/api/v1/users", register_user, methods=["POST"]),
Route("/api/v1/users/me", current_user, methods=["GET"]),
Route("/api/v1/auth/sessions", create_session, methods=["POST"]),
Route("/api/v1/auth/sessions/current", revoke_session, methods=["DELETE"]),
Route("/api/v1/chats", create_chat, methods=["POST"]),
Route("/api/v1/chats", list_chats, methods=["GET"]),
Route("/api/v1/chats/{chat_id}", get_chat, methods=["GET"]),
Route("/api/v1/chats/{chat_id}/messages", create_message, methods=["POST"]),
Route("/api/v1/chats/{chat_id}/messages", list_messages, methods=["GET"]),
Route("/api/v1/_lab/metrics", lab_metrics, methods=["GET"]),
Route("/api/v1/_lab/flags", lab_flags, methods=["POST"]),
],
exception_handlers={404: not_found_handler, AppError: app_error_handler},
on_startup=[startup],
on_shutdown=[shutdown],
)
app.state.shutting_down = False
app.add_middleware(RequestContextMiddleware)
return app
def configure_logging() -> None:
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s %(levelname)s %(name)s %(message)s",
stream=sys.stdout,
)
def main() -> None:
configure_logging()
app = build_app()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
server = uvicorn.Server(
uvicorn.Config(
app,
host=config.HOST,
port=config.PORT,
log_level=config.LOG_LEVEL,
access_log=False,
timeout_graceful_shutdown=int(config.SHUTDOWN_GRACE_SECONDS),
)
)
def _begin_shutdown() -> None:
if app.state.shutting_down:
return
logging.getLogger("messenger").info(
"shutdown signal received; readiness fails now, exit in %.1fs",
config.LB_DRAIN_SECONDS,
)
app.state.shutting_down = True
loop.call_later(config.LB_DRAIN_SECONDS, lambda: setattr(server, "should_exit", True))
for sig in (signal.SIGTERM, signal.SIGINT):
try:
loop.add_signal_handler(sig, _begin_shutdown)
except NotImplementedError:
signal.signal(sig, lambda *_: _begin_shutdown())
loop.run_until_complete(server.serve())
if __name__ == "__main__":
main()
+48
View File
@@ -0,0 +1,48 @@
import logging
import time
import uuid
from typing import Callable
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from app.config import INSTANCE_ID
from app.errors import AppError, error_response, from_app_error
logger = logging.getLogger("messenger.access")
def _stamp(response: Response, request_id: str) -> Response:
response.headers["X-Request-Id"] = request_id
response.headers["X-Instance-Id"] = INSTANCE_ID
return response
class RequestContextMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: Callable) -> Response:
request_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
request.state.request_id = request_id
if request.app.state.shutting_down and request.url.path not in ("/health/live", "/health/ready"):
return _stamp(error_response(503, "service is shutting down", request_id), request_id)
started = time.perf_counter()
try:
response = await call_next(request)
except AppError as exc:
return _stamp(from_app_error(exc, request_id), request_id)
except Exception:
logger.exception("request_id=%s unhandled error path=%s", request_id, request.url.path)
return _stamp(error_response(500, "internal error", request_id), request_id)
elapsed_ms = (time.perf_counter() - started) * 1000
_stamp(response, request_id)
logger.info(
"request_id=%s method=%s path=%s status=%s duration_ms=%.2f",
request_id,
request.method,
request.url.path,
response.status_code,
elapsed_ms,
)
return response
+29
View File
@@ -0,0 +1,29 @@
"""One-shot schema apply. API replicas do not run this on startup."""
import asyncio
import logging
import sys
from app import config, db
async def _main() -> None:
if not config.DATABASE_URL:
raise SystemExit("DATABASE_URL is not set")
await db.connect()
try:
await db.apply_migrations()
finally:
await db.close()
def main() -> None:
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s %(levelname)s %(name)s %(message)s",
stream=sys.stdout,
)
asyncio.run(_main())
if __name__ == "__main__":
main()
+23
View File
@@ -0,0 +1,23 @@
from argon2 import PasswordHasher
from argon2.exceptions import InvalidHashError, VerifyMismatchError
hasher = PasswordHasher(time_cost=2, memory_cost=8192, parallelism=1)
_dummy = None
def hash_password(password: str) -> str:
return hasher.hash(password)
def dummy_hash() -> str:
global _dummy
if _dummy is None:
_dummy = hash_password("timing-dummy-password")
return _dummy
def verify_password(password_hash: str, password: str) -> bool:
try:
return hasher.verify(password_hash, password)
except (VerifyMismatchError, InvalidHashError, TypeError, ValueError):
return False
+40
View File
@@ -0,0 +1,40 @@
import redis.asyncio as redis
from app import config
from app.errors import AppError
client: redis.Redis | None = None
async def connect() -> redis.Redis:
global client
client = redis.from_url(
config.REDIS_URL,
socket_connect_timeout=config.REDIS_SOCKET_TIMEOUT,
socket_timeout=config.REDIS_SOCKET_TIMEOUT,
decode_responses=True,
)
await client.ping()
return client
async def close() -> None:
global client
if client is not None:
await client.close()
client = None
def require() -> redis.Redis:
if client is None:
raise AppError(503, "service unavailable")
return client
async def ping() -> None:
try:
await require().ping()
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
+284
View File
@@ -0,0 +1,284 @@
import base64
import binascii
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from app import cache, config, db, rediscli, sessions, store
from app.cursor_codec import decode_cursor, encode_cursor
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
from app.passwords import dummy_hash, hash_password, verify_password
from app.validation import (
parse_json_body,
require_exact_keys,
validate_display_name,
validate_limit,
validate_idempotency_key,
validate_message_text,
validate_password,
validate_title,
validate_username,
validate_uuid,
)
def _json(data: dict, request_id: str, status: int = 200) -> JSONResponse:
return JSONResponse(status_code=status, headers={"X-Request-Id": request_id}, content=data)
def _empty(request_id: str) -> Response:
return Response(status_code=204, headers={"X-Request-Id": request_id})
async def _bearer_user(request: Request) -> dict:
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "bearer" or not rest.strip():
raise AppError(401, "authentication required", BEARER_CHALLENGE)
user_id = str(await sessions.resolve(rest.strip()))
user = await store.get_user(user_id)
if user is None:
raise AppError(401, "authentication required", BEARER_CHALLENGE)
return user
async def health_live(request: Request) -> Response:
return _json({"status": "ok"}, request.state.request_id)
async def health_ready(request: Request) -> Response:
rid = request.state.request_id
if request.app.state.shutting_down:
return error_response(503, "service is shutting down", rid)
await db.ping()
await rediscli.ping()
return _json({"status": "ok"}, rid)
async def register_user(request: Request) -> Response:
rid = request.state.request_id
if request.headers.get("content-type", "").split(";")[0].strip().lower() not in ("application/json", ""):
if request.headers.get("content-type"):
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"username", "display_name", "password"}, {"username", "display_name", "password"}):
return error_response(400, "invalid user payload", rid)
if validate_username(body["username"]) or validate_display_name(body["display_name"]) or validate_password(body["password"]):
return error_response(400, "invalid user fields", rid)
user, conflict = await store.create_user(body["username"], body["display_name"], hash_password(body["password"]))
if conflict:
return error_response(409, "username already taken", rid)
assert user is not None
return _json(user, rid, 201)
async def current_user(request: Request) -> Response:
user = await _bearer_user(request)
return _json(user, request.state.request_id)
async def create_session(request: Request) -> Response:
rid = request.state.request_id
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "basic" or not rest.strip():
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
try:
decoded = base64.b64decode(rest.strip(), validate=True).decode("utf-8")
except (binascii.Error, UnicodeError, ValueError):
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
username, sep, password = decoded.partition(":")
if not sep:
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
row = await store.get_user_by_username(username)
password_hash = row["password_hash"] if row else dummy_hash()
if row is None or not verify_password(password_hash, password):
return error_response(401, "authentication required", rid, {"WWW-Authenticate": BASIC_CHALLENGE})
token, expires_at = await sessions.create(row["id"])
return _json(
{
"token": token,
"token_type": "Bearer",
"expires_at": expires_at,
"user": store.user_dict(row),
},
rid,
201,
)
async def revoke_session(request: Request) -> Response:
header = request.headers.get("Authorization") or ""
scheme, _, rest = header.partition(" ")
if scheme.lower() != "bearer" or not rest.strip():
raise AppError(401, "authentication required", BEARER_CHALLENGE)
await sessions.revoke(rest.strip())
return _empty(request.state.request_id)
async def create_chat(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
if ct and ct != "application/json":
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"title", "member_ids"}, {"title", "member_ids"}):
return error_response(400, "invalid chat payload", rid)
if validate_title(body["title"]):
return error_response(400, "invalid title", rid)
members = body["member_ids"]
if not isinstance(members, list) or not members:
return error_response(400, "invalid member_ids", rid)
if len(members) > 100:
return error_response(400, "too many members", rid)
for mid in members:
if validate_uuid(mid):
return error_response(400, "invalid member id", rid)
chat, create_err = await store.create_chat(body["title"], members, user["id"])
if create_err == "caller not in members":
return error_response(400, "caller must be a member", rid)
if create_err in ("duplicate members", "unknown member"):
return error_response(400, "invalid members", rid)
assert chat is not None
return _json(chat, rid, 201)
async def list_chats(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
limit, limit_err = validate_limit(request.query_params.get("limit"))
if limit_err:
return error_response(400, limit_err, rid)
assert limit is not None
cursor_raw = request.query_params.get("cursor")
after = None
if cursor_raw is not None:
if not cursor_raw.strip():
return error_response(400, "invalid cursor", rid)
try:
created_at, entity_id = decode_cursor(cursor_raw, user["id"])
except ValueError:
return error_response(400, "invalid cursor", rid)
after = (created_at, entity_id)
items, next_key = await store.list_chats(user["id"], limit, after)
next_cursor = None
if next_key is not None:
next_cursor = encode_cursor(user["id"], next_key[0], next_key[1])
return _json({"items": items, "next_cursor": next_cursor}, rid)
async def get_chat(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
chat = await store.get_chat(chat_id, user["id"])
if chat is None:
return error_response(404, "chat not found", rid)
return _json(chat, rid)
async def create_message(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
ct = request.headers.get("content-type", "").split(";")[0].strip().lower()
if ct and ct != "application/json":
return error_response(415, "unsupported media type", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"text"}, {"text"}):
return error_response(400, "invalid message payload", rid)
if validate_message_text(body["text"]):
return error_response(400, "invalid message text", rid)
if validate_idempotency_key(request.headers.get("Idempotency-Key")):
return error_response(400, "invalid idempotency key", rid)
message, msg_err = await store.add_message(
chat_id,
user["id"],
body["text"],
request.headers.get("Idempotency-Key") or "",
request.url.path,
)
if msg_err == "not_found":
return error_response(404, "chat not found", rid)
if msg_err == "conflict":
return error_response(409, "idempotency key reused with a different body", rid)
assert message is not None
return _json(message, rid, 201)
async def list_messages(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
limit, limit_err = validate_limit(request.query_params.get("limit"))
if limit_err:
return error_response(400, limit_err, rid)
assert limit is not None
cursor_raw = request.query_params.get("cursor")
after = None
if cursor_raw is not None:
if not cursor_raw.strip():
return error_response(400, "invalid cursor", rid)
try:
created_at, entity_id = decode_cursor(cursor_raw, chat_id)
except ValueError:
return error_response(400, "invalid cursor", rid)
after = (created_at, entity_id)
page, next_key, list_err = await store.list_messages(chat_id, user["id"], limit, after)
if list_err == "not_found":
return error_response(404, "chat not found", rid)
assert page is not None
next_cursor = None
if next_key is not None:
next_cursor = encode_cursor(chat_id, next_key[0], next_key[1])
return _json({"items": page, "next_cursor": next_cursor}, rid)
async def lab_metrics(request: Request) -> Response:
if not config.lab_mode():
return error_response(404, "not found", request.state.request_id)
return _json(
{
"singleflight": await cache.singleflight_enabled(),
"invalidate": await cache.invalidate_enabled(),
"history_sql": await cache.history_sql_count(),
},
request.state.request_id,
)
async def lab_flags(request: Request) -> Response:
if not config.lab_mode():
return error_response(404, "not found", request.state.request_id)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, request.state.request_id)
assert body is not None
if body.get("reset_sql"):
await cache.reset_history_sql_count()
flags = await cache.set_flags(
singleflight=body.get("singleflight"),
invalidate=body.get("invalidate"),
delay_ms=body.get("delay_ms"),
)
return _json(flags, request.state.request_id)
+56
View File
@@ -0,0 +1,56 @@
import hashlib
import json
import secrets
from datetime import datetime, timedelta, timezone
from uuid import UUID
from app import config, rediscli
from app.errors import AppError, BEARER_CHALLENGE
def new_token() -> str:
return secrets.token_urlsafe(32)
def _key(token: str) -> str:
return "sess:" + hashlib.sha256(token.encode("utf-8")).hexdigest()
def _fmt(dt: datetime) -> str:
return dt.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
async def create(user_id: UUID) -> tuple[str, str]:
token = new_token()
expires = datetime.now(timezone.utc) + timedelta(seconds=config.SESSION_TTL_SECONDS)
payload = json.dumps({"user_id": str(user_id), "expires_at": _fmt(expires)})
try:
await rediscli.require().set(_key(token), payload, ex=config.SESSION_TTL_SECONDS)
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
return token, _fmt(expires)
async def resolve(token: str) -> UUID:
try:
raw = await rediscli.require().get(_key(token))
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
if not raw:
raise AppError(401, "authentication required", BEARER_CHALLENGE)
return UUID(json.loads(raw)["user_id"])
async def revoke(token: str) -> None:
try:
deleted = await rediscli.require().delete(_key(token))
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
if not deleted:
raise AppError(401, "authentication required", BEARER_CHALLENGE)
+361
View File
@@ -0,0 +1,361 @@
from __future__ import annotations
import hashlib
import json
from datetime import datetime, timezone
from typing import List, Optional, Tuple
from uuid import UUID, uuid4
import asyncpg
from app import cache, config, db
from app.errors import AppError
def format_ts(dt: datetime) -> str:
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
def user_dict(row) -> dict:
return {
"id": str(row["id"]),
"username": row["username"],
"display_name": row["display_name"],
"created_at": format_ts(row["created_at"]),
}
def chat_dict(row, member_ids: List[str]) -> dict:
return {
"id": str(row["id"]),
"title": row["title"],
"member_ids": member_ids,
"created_at": format_ts(row["created_at"]),
}
def message_dict(row) -> dict:
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
"sender_id": str(row["sender_id"]),
"text": row["text"],
"created_at": format_ts(row["created_at"]),
}
async def _run(coro):
try:
return await coro
except AppError:
raise
except db.INTEGRITY:
raise
except Exception:
raise AppError(503, "service unavailable") from None
async def create_user(username: str, display_name: str, password_hash: str) -> Tuple[Optional[dict], Optional[str]]:
user_id = uuid4()
created = datetime.now(timezone.utc)
try:
async with db.require().acquire() as conn:
await conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ($1, $2, $3, $4, $5)
""",
user_id,
username,
display_name,
password_hash,
created,
)
except asyncpg.UniqueViolationError:
return None, "conflict"
except Exception:
raise AppError(503, "service unavailable") from None
return user_dict({"id": user_id, "username": username, "display_name": display_name, "created_at": created}), None
async def get_user(user_id: str) -> Optional[dict]:
row = await _run(
_fetchrow("SELECT id, username, display_name, created_at FROM users WHERE id = $1", UUID(user_id))
)
return user_dict(row) if row else None
async def get_user_by_username(username: str):
return await _run(
_fetchrow(
"SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1",
username,
)
)
async def _fetchrow(sql: str, *args):
async with db.require().acquire() as conn:
return await conn.fetchrow(sql, *args)
async def _fetch(sql: str, *args):
async with db.require().acquire() as conn:
return await conn.fetch(sql, *args)
async def member_ids_of(chat_id: UUID) -> List[str]:
rows = await _run(_fetch("SELECT user_id FROM chat_members WHERE chat_id = $1 ORDER BY user_id", chat_id))
return [str(r["user_id"]) for r in rows]
async def create_chat(title: str, member_ids: List[str], caller_id: str) -> Tuple[Optional[dict], Optional[str]]:
if caller_id not in member_ids:
return None, "caller not in members"
if len(set(member_ids)) != len(member_ids):
return None, "duplicate members"
ids = [UUID(x) for x in member_ids]
chat_id = uuid4()
created = datetime.now(timezone.utc)
try:
async with db.require().acquire() as conn:
async with conn.transaction():
existing = await conn.fetch("SELECT id FROM users WHERE id = ANY($1::uuid[])", ids)
if len(existing) != len(ids):
return None, "unknown member"
await conn.execute(
"INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)",
chat_id,
title,
created,
)
await conn.executemany(
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
[(chat_id, mid) for mid in ids],
)
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
return chat_dict({"id": chat_id, "title": title, "created_at": created}, member_ids), None
async def get_chat(chat_id: str, user_id: str) -> Optional[dict]:
row = await load_chat_for_member(chat_id, user_id)
if row is None:
return None
return chat_dict(row, await member_ids_of(UUID(chat_id)))
async def load_chat_for_member(chat_id: str, user_id: str):
return await _run(
_fetchrow(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members m ON m.chat_id = c.id AND m.user_id = $2
WHERE c.id = $1
""",
UUID(chat_id),
UUID(user_id),
)
)
async def list_chats(user_id: str, limit: int, after: Optional[Tuple[str, str]] = None):
args: list = [UUID(user_id)]
clause = ""
if after is not None:
clause = "AND (c.created_at, c.id) > ($2, $3)"
args.extend([datetime.fromisoformat(after[0].replace("Z", "+00:00")), UUID(after[1])])
rows = await _run(
_fetch(
f"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = $1
WHERE 1=1 {clause}
ORDER BY c.created_at, c.id
LIMIT {int(limit) + 1}
""",
*args,
)
)
page = rows[:limit]
items = [chat_dict(row, await member_ids_of(row["id"])) for row in page]
next_key = None
if len(rows) > limit:
last = page[-1]
next_key = (format_ts(last["created_at"]), str(last["id"]))
return items, next_key
def _canonical_message(text: str) -> str:
# Missing attachment_ids and [] are the same message. Key order does not matter.
return json.dumps(
{"attachment_ids": [], "text": text},
sort_keys=True,
ensure_ascii=False,
separators=(",", ":"),
)
async def add_message(
chat_id: str,
sender_id: str,
text: str,
idem_key: str,
path: str,
) -> Tuple[Optional[dict], Optional[str]]:
"""Insert one message or replay the stored 201. The row and the result commit together.
Same user+method+path+key is serialized with a Postgres advisory lock so two
replicas cannot insert two messages. A failed transaction stores nothing.
"""
digest = hashlib.sha256(_canonical_message(text).encode("utf-8")).hexdigest()
lock_key = f"{sender_id}|POST|{path}|{idem_key}"
payload: Optional[dict] = None
created_new = False
try:
async with db.require().acquire() as conn:
async with conn.transaction():
await conn.execute("SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))", lock_key)
member = await conn.fetchrow(
"""
SELECT c.id
FROM chats c
JOIN chat_members m ON m.chat_id = c.id AND m.user_id = $2
WHERE c.id = $1
""",
UUID(chat_id),
UUID(sender_id),
)
if member is None:
return None, "not_found"
row = await conn.fetchrow(
"""
SELECT request_hash, response_json, created_at
FROM idempotency_keys
WHERE user_id = $1 AND http_method = 'POST' AND path = $2 AND idem_key = $3
""",
UUID(sender_id),
path,
idem_key,
)
if row is not None:
created_at = row["created_at"]
if created_at.tzinfo is None:
created_at = created_at.replace(tzinfo=timezone.utc)
age = (datetime.now(timezone.utc) - created_at).total_seconds()
if age <= config.IDEMPOTENCY_TTL_SECONDS:
if row["request_hash"] != digest:
return None, "conflict"
return json.loads(row["response_json"]), None
await conn.execute(
"""
DELETE FROM idempotency_keys
WHERE user_id = $1 AND http_method = 'POST' AND path = $2 AND idem_key = $3
""",
UUID(sender_id),
path,
idem_key,
)
message_id = uuid4()
created = datetime.now(timezone.utc)
await conn.execute(
"""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
VALUES ($1, $2, $3, $4, $5)
""",
message_id,
UUID(chat_id),
UUID(sender_id),
text,
created,
)
payload = message_dict(
{
"id": message_id,
"chat_id": UUID(chat_id),
"sender_id": UUID(sender_id),
"text": text,
"created_at": created,
}
)
await conn.execute(
"""
INSERT INTO idempotency_keys
(user_id, http_method, path, idem_key, request_hash, response_json, created_at)
VALUES ($1, 'POST', $2, $3, $4, $5, $6)
""",
UUID(sender_id),
path,
idem_key,
digest,
json.dumps(payload, ensure_ascii=False, separators=(",", ":")),
created,
)
created_new = True
except AppError:
raise
except Exception:
raise AppError(503, "service unavailable") from None
if created_new:
await cache.invalidate_history(chat_id)
return payload, None
async def list_messages(chat_id: str, user_id: str, limit: int, after: Optional[Tuple[str, str]] = None):
if await load_chat_for_member(chat_id, user_id) is None:
return None, None, "not_found"
cursor = ""
after_ts = after_id = None
if after is not None:
cursor = after[0] + ":" + after[1]
after_ts = datetime.fromisoformat(after[0].replace("Z", "+00:00"))
after_id = UUID(after[1])
async def loader() -> dict:
await cache.incr_history_sql()
if after_ts is None:
rows = await _run(
_fetch(
"""
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1
ORDER BY created_at, id
LIMIT $2
""",
UUID(chat_id),
limit + 1,
)
)
else:
rows = await _run(
_fetch(
"""
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
ORDER BY created_at, id
LIMIT $4
""",
UUID(chat_id),
after_ts,
after_id,
limit + 1,
)
)
page = rows[:limit]
items = [message_dict(r) for r in page]
next_key = None
if len(rows) > limit:
last = page[-1]
next_key = (format_ts(last["created_at"]), str(last["id"]))
return {"items": items, "next_key": next_key}
page = await cache.get_history_page(chat_id, cursor, limit, loader)
return page["items"], page.get("next_key"), None
+1
View File
@@ -0,0 +1 @@
+73
View File
@@ -0,0 +1,73 @@
"""Seed ≥10 000 messages and print EXPLAIN (ANALYZE, BUFFERS) for keyset pagination."""
import asyncio
import os
from uuid import uuid4
import asyncpg
from app.passwords import hash_password
async def main() -> None:
conn = await asyncpg.connect(dsn=os.environ["DATABASE_URL"], timeout=10, command_timeout=120)
try:
alice = uuid4()
bob = uuid4()
chat = uuid4()
pwd = hash_password("Example-only-not-a-real-secret-123")
await conn.execute(
"INSERT INTO users (id, username, display_name, password_hash) VALUES ($1,$2,$3,$4),($5,$6,$7,$4)",
alice,
"explain_a_" + alice.hex[:8],
"Alice",
pwd,
bob,
"explain_b_" + bob.hex[:8],
"Bob",
)
await conn.execute("INSERT INTO chats (id, title) VALUES ($1,$2)", chat, "Explain chat")
await conn.execute(
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1,$2),($1,$3)",
chat,
alice,
bob,
)
n = int(os.environ.get("EXPLAIN_MESSAGES", "10000"))
await conn.execute(
"""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), $1, $2, 'seed-' || g, now() - (g || ' milliseconds')::interval
FROM generate_series(1, $3) AS g
""",
chat,
alice,
n,
)
count = await conn.fetchval("SELECT count(*) FROM messages WHERE chat_id = $1", chat)
print("seeded_messages", count, "chat_id", chat)
first = await conn.fetchrow(
"SELECT created_at, id FROM messages WHERE chat_id = $1 ORDER BY created_at, id LIMIT 1",
chat,
)
query = """
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
ORDER BY created_at, id
LIMIT 50
"""
print("\n=== WITH index messages_chat_created_id_idx ===")
plan = await conn.fetch("EXPLAIN (ANALYZE, BUFFERS) " + query, chat, first["created_at"], first["id"])
print("\n".join(row[0] for row in plan))
await conn.execute("SET enable_indexscan = off")
await conn.execute("SET enable_bitmapscan = off")
await conn.execute("SET enable_indexonlyscan = off")
print("\n=== WITHOUT index scans (seq scan baseline) ===")
plan2 = await conn.fetch("EXPLAIN (ANALYZE, BUFFERS) " + query, chat, first["created_at"], first["id"])
print("\n".join(row[0] for row in plan2))
finally:
await conn.close()
if __name__ == "__main__":
asyncio.run(main())
+102
View File
@@ -0,0 +1,102 @@
import json
import re
from typing import Any, Dict, Optional, Tuple
from uuid import UUID
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
IDEMPOTENCY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
def parse_json_body(raw: bytes) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:
if not raw:
return None, "empty body"
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError):
return None, "malformed JSON"
if not isinstance(data, dict):
return None, "JSON body must be an object"
return data, None
def require_exact_keys(data: Dict[str, Any], allowed: set, required: set) -> Optional[str]:
keys = set(data.keys())
if keys - allowed:
return "unexpected fields"
if required - keys:
return "missing required fields"
return None
def validate_username(value: Any) -> Optional[str]:
if not isinstance(value, str) or not USERNAME_RE.fullmatch(value):
return "invalid username"
return None
def validate_display_name(value: Any) -> Optional[str]:
if not isinstance(value, str):
return "invalid display_name"
if not value.strip():
return "display_name must not be blank"
if len(value) > 100:
return "display_name too long"
return None
def validate_password(value: Any) -> Optional[str]:
if not isinstance(value, str):
return "invalid password"
if not value.strip():
return "password must not be blank"
if len(value) < 12 or len(value) > 128:
return "password length"
return None
def validate_title(value: Any) -> Optional[str]:
if not isinstance(value, str):
return "invalid title"
if not value.strip():
return "title must not be blank"
if len(value) > 100:
return "title too long"
return None
def validate_uuid(value: Any) -> Optional[str]:
if not isinstance(value, str):
return "invalid uuid"
try:
UUID(value)
except ValueError:
return "invalid uuid"
return None
def validate_idempotency_key(value: Optional[str]) -> Optional[str]:
if value is None or IDEMPOTENCY_RE.fullmatch(value) is None:
return "invalid idempotency key"
return None
def validate_message_text(value: Any) -> Optional[str]:
if not isinstance(value, str):
return "invalid text"
if not value.strip():
return "text must not be blank"
if len(value) > 4000:
return "text too long"
return None
def validate_limit(value: Optional[str], default: int = 50) -> Tuple[Optional[int], Optional[str]]:
if value is None:
return default, None
try:
limit = int(value)
except ValueError:
return None, "invalid limit"
if limit < 1 or limit > 100:
return None, "limit out of range"
return limit, None
+49
View File
@@ -0,0 +1,49 @@
-- Lab2 schema (written before application queries).
-- Source of truth: PostgreSQL for users, chats, membership, messages.
-- Transaction boundary: INSERT chat + INSERT members in one transaction (rollback together).
-- Sessions are stored in Redis with AOF, not in this database.
CREATE TABLE IF NOT EXISTS schema_migrations (
filename TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE users (
id UUID PRIMARY KEY,
username TEXT NOT NULL,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE UNIQUE INDEX users_username_key ON users (username);
CREATE TABLE chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
);
CREATE TABLE chat_members (
chat_id UUID NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users (id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_id_idx ON chat_members (user_id);
CREATE INDEX chats_created_at_id_idx ON chats (created_at, id);
CREATE TABLE 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 DEFAULT now(),
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
);
-- Keyset history: WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT n
CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id);
+14
View File
@@ -0,0 +1,14 @@
-- Shared idempotency for POST /messages. Scope: user + method + path + key.
-- The message row and this result are committed in one transaction.
-- A rolled-back request leaves no row, so a later retry with the same key can insert.
CREATE TABLE IF NOT EXISTS idempotency_keys (
user_id UUID NOT NULL REFERENCES users (id),
http_method TEXT NOT NULL,
path TEXT NOT NULL,
idem_key TEXT NOT NULL,
request_hash TEXT NOT NULL,
response_json TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, http_method, path, idem_key)
);
+6
View File
@@ -0,0 +1,6 @@
[pytest]
testpaths = student_tests
pythonpath = .
markers =
infra: compose stop/restart of redis or postgres
expiry: requires SESSION_TTL_SECONDS<=3
+2
View File
@@ -0,0 +1,2 @@
pytest==8.3.4
httpx==0.28.1
+5
View File
@@ -0,0 +1,5 @@
starlette==0.41.3
uvicorn[standard]==0.32.1
asyncpg==0.30.0
redis==5.2.1
argon2-cffi==23.1.0
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
"""Recreate API with SESSION_TTL_SECONDS=2, test expiry, restore TTL."""
import os
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def compose(args, extra_env=None):
env = os.environ.copy()
if extra_env:
env.update(extra_env)
cmd = ["docker", "compose"]
if (ROOT / ".env").is_file():
cmd.extend(["--env-file", str(ROOT / ".env")])
cmd.extend(args)
proc = subprocess.run(cmd, cwd=ROOT, env=env)
if proc.returncode != 0:
raise SystemExit(proc.returncode)
def wait_ready():
import urllib.request
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
urllib.request.urlopen("http://127.0.0.1:8080/health/ready", timeout=2)
return
except Exception:
time.sleep(0.5)
raise SystemExit("API did not become ready")
def main():
compose(["up", "-d", "--no-deps", "--force-recreate", "api"], {"SESSION_TTL_SECONDS": "2"})
wait_ready()
env = os.environ.copy()
env["SESSION_TTL_SECONDS"] = "2"
env["RUN_EXPIRY"] = "1"
proc = subprocess.run(
[sys.executable, "-m", "pytest", "student_tests/test_expiry.py", "-q"],
cwd=ROOT,
env=env,
)
compose(["up", "-d", "--no-deps", "--force-recreate", "api"], {"SESSION_TTL_SECONDS": "3600"})
wait_ready()
raise SystemExit(proc.returncode)
if __name__ == "__main__":
main()
View File
+24
View File
@@ -0,0 +1,24 @@
import os
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure # noqa: E402
@pytest.fixture(scope="session")
def base_url():
return os.environ.get("BASE_URL", "http://127.0.0.1:8080")
@pytest.fixture(scope="session")
def live(base_url):
c = Client(base_url, timeout=15)
try:
c.call("GET", "/health/ready", schema="Health")
except Failure as exc:
pytest.skip("API is not ready: %s" % exc)
return c
+195
View File
@@ -0,0 +1,195 @@
"""Negative, pagination and auth tests for lab 2."""
import uuid
from urllib.parse import quote
from client import Client
PASSWORD = "Example-only-not-a-real-secret-123"
def register(c: Client, username=None):
username = username or ("u_" + uuid.uuid4().hex[:10])
user = c.call(
"POST",
"/api/v1/users",
201,
body={"username": username, "display_name": "Test User", "password": PASSWORD},
schema="User",
)["json"]
return user
def login(c: Client, username, password=PASSWORD):
import base64
cred = base64.b64encode((username + ":" + password).encode()).decode()
session = c.call(
"POST",
"/api/v1/auth/sessions",
201,
headers={"Authorization": "Basic " + cred},
schema="Session",
)["json"]
return {"id": session["user"]["id"], "user": session["user"], "token": session["token"], "registration": {"username": username, "password": password}}
def actor_of(c: Client, username=None):
user = register(c, username)
return login(c, user["username"])
def test_malformed_json(live: Client):
response = live.call(
"POST",
"/api/v1/users",
400,
raw=b"{not-json",
headers={"Content-Type": "application/json"},
)
assert response["json"]["error"]["code"] == "invalid_request"
assert response["headers"].get("X-Request-Id")
def test_duplicate_username(live: Client):
user = register(live, username="dup_" + uuid.uuid4().hex[:8])
again = live.call(
"POST",
"/api/v1/users",
409,
body={"username": user["username"], "display_name": "Other", "password": PASSWORD},
)
assert again["json"]["error"]["code"] == "conflict"
def test_empty_message_rejected(live: Client):
alice = actor_of(live)
bob = actor_of(live)
chat = live.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "t", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
response = live.call(
"POST",
"/api/v1/chats/" + chat["id"] + "/messages",
400,
actor=alice,
body={"text": " "},
)
assert response["status"] == 400
def test_outsider_chat_is_404(live: Client):
alice = actor_of(live)
bob = actor_of(live)
eve = actor_of(live)
chat = live.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "secret", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
response = live.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)
assert response["json"]["error"]["code"] == "not_found"
def test_message_pagination_multiple_pages(live: Client):
alice = actor_of(live)
bob = actor_of(live)
chat = live.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "pages", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
created = []
for idx in range(5):
created.append(live.message(alice, chat, "m%s" % idx)["json"])
collected = []
cursor = None
for _ in range(10):
path = "/api/v1/chats/%s/messages?limit=2" % chat["id"]
if cursor:
path += "&cursor=" + quote(cursor, safe="")
page = live.call("GET", path, actor=bob, schema="MessagePage")["json"]
collected.extend(page["items"])
cursor = page["next_cursor"]
if cursor is None:
break
assert len(collected) == 5
ordered = sorted(created, key=lambda x: (x["created_at"], x["id"]))
assert collected == ordered
def test_cursor_from_other_chat_rejected(live: Client):
alice = actor_of(live)
bob = actor_of(live)
chat_a = live.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "a", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
chat_b = live.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "b", "member_ids": [alice["id"], bob["id"]]},
schema="Chat",
)["json"]
msg = live.message(alice, chat_a, "only in a")["json"]
page = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=1" % chat_a["id"],
actor=alice,
schema="MessagePage",
)["json"]
assert page["items"][0]["id"] == msg["id"]
cursor = page["next_cursor"]
# If history is a single item, next_cursor is null; encode a foreign cursor via first page of A after a dummy extra message.
if cursor is None:
live.message(alice, chat_a, "second")
page = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=1" % chat_a["id"],
actor=alice,
schema="MessagePage",
)["json"]
cursor = page["next_cursor"]
response = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=10&cursor=%s" % (chat_b["id"], quote(cursor, safe="")),
400,
actor=alice,
)
assert response["status"] == 400
def test_x_user_id_does_not_authenticate(live: Client):
alice = actor_of(live)
live.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
def test_wrong_password(live: Client):
user = register(live)
import base64
bad = base64.b64encode((user["username"] + ":wrong-password-xx").encode()).decode()
live.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic " + bad})
def test_logout(live: Client):
alice = actor_of(live)
live.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
live.call("GET", "/api/v1/users/me", 401, actor=alice)
+48
View File
@@ -0,0 +1,48 @@
"""10 clients × 10 messages = 100 unique messages."""
import threading
import uuid
from client import Client
from student_tests.test_api import actor_of
def test_concurrent_messages(live: Client):
users = [actor_of(live) for _ in range(10)]
member_ids = [u["id"] for u in users]
chat = live.call(
"POST",
"/api/v1/chats",
201,
actor=users[0],
body={"title": "load", "member_ids": member_ids},
schema="Chat",
)["json"]
errors = []
lock = threading.Lock()
def worker(user):
local = Client(live.base, timeout=15)
for _ in range(10):
try:
local.message(user, chat, uuid.uuid4().hex)
except Exception as exc:
with lock:
errors.append(str(exc))
threads = [threading.Thread(target=worker, args=(u,)) for u in users]
for thread in threads:
thread.start()
for thread in threads:
thread.join(timeout=60)
assert not errors, "unexpected failures: %s" % errors[:5]
page = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=100" % chat["id"],
actor=users[0],
schema="MessagePage",
)["json"]
ids = [item["id"] for item in page["items"]]
assert len(ids) == len(set(ids)) == 100
+17
View File
@@ -0,0 +1,17 @@
import os
import time
import pytest
from client import Client
@pytest.mark.expiry
def test_session_expires(live: Client):
ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
if os.environ.get("RUN_EXPIRY") != "1":
pytest.skip("set RUN_EXPIRY=1 and SESSION_TTL_SECONDS<=3")
assert ttl <= 3
actor = live.user("ttl")
live.call("GET", "/api/v1/users/me", actor=actor, schema="User")
time.sleep(ttl + 1.5)
live.call("GET", "/api/v1/users/me", 401, actor=actor)
+44
View File
@@ -0,0 +1,44 @@
"""Grade-4: username race and atomic chat rollback."""
import threading
import uuid
from client import Client
from student_tests.test_api import PASSWORD, actor_of
def test_register_race(live: Client):
username = "r_" + uuid.uuid4().hex[:12]
body = {"username": username, "display_name": "Race", "password": PASSWORD}
results = []
def once():
local = Client(live.base, timeout=20)
try:
results.append(local.call("POST", "/api/v1/users", [201, 409], body=body)["status"])
except Exception as exc:
results.append(str(exc))
threads = [threading.Thread(target=once) for _ in range(16)]
for t in threads:
t.start()
for t in threads:
t.join()
assert results.count(201) == 1, results
assert results.count(409) == 15, results
def test_chat_rollback(live: Client):
alice = actor_of(live)
before = live.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"]
missing = str(uuid.uuid4())
live.call(
"POST",
"/api/v1/chats",
400,
actor=alice,
body={"title": "rollback-should-not-exist", "member_ids": [alice["id"], missing]},
)
after = live.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"]
assert len(after) == len(before)
assert all(x["title"] != "rollback-should-not-exist" for x in after)
+89
View File
@@ -0,0 +1,89 @@
"""Dependency failure and Redis restart (AOF + cache rebuild)."""
import os
import subprocess
import time
from pathlib import Path
import pytest
from client import Client, Failure
ROOT = Path(__file__).resolve().parents[1]
def compose(*args):
env_file = ROOT / ".env"
cmd = ["docker", "compose"]
if env_file.is_file():
cmd.extend(["--env-file", str(env_file)])
cmd.extend(args)
proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True)
if proc.returncode != 0:
raise AssertionError("docker compose %s failed: %s" % (" ".join(args), (proc.stderr or "")[-500:]))
return proc
def wait_ready(c: Client, seconds=90):
deadline = time.monotonic() + seconds
last = None
while time.monotonic() < deadline:
try:
c.call("GET", "/health/ready", schema="Health")
return
except Failure as exc:
last = exc
time.sleep(0.5)
raise AssertionError("ready not restored: %s" % last)
def wait_status(c: Client, path, expected, seconds=30, actor=None):
deadline = time.monotonic() + seconds
last = None
while time.monotonic() < deadline:
try:
schema = "Health" if expected == 200 and path.startswith("/health") else None
c.call("GET", path, expected, actor=actor, schema=schema)
return
except Failure as exc:
last = exc
time.sleep(0.4)
raise AssertionError("did not observe %s for %s: %s" % (expected, path, last))
@pytest.mark.infra
def test_redis_and_postgres_unavailable(live: Client):
if os.environ.get("RUN_INFRA") != "1":
pytest.skip("set RUN_INFRA=1 to stop compose services")
alice = live.user("infra")
compose("stop", "redis")
wait_status(live, "/health/live", 200)
wait_status(live, "/health/ready", 503)
wait_status(live, "/api/v1/users/me", 503, actor=alice)
compose("start", "redis")
wait_ready(live)
live.call("GET", "/api/v1/users/me", actor=alice, schema="User")
compose("stop", "postgres")
wait_status(live, "/health/live", 200)
wait_status(live, "/health/ready", 503)
compose("start", "postgres")
wait_ready(live)
live.call("GET", "/api/v1/users/me", actor=alice, schema="User")
@pytest.mark.infra
def test_redis_restart_keeps_session_and_history(live: Client):
if os.environ.get("RUN_INFRA") != "1":
pytest.skip("set RUN_INFRA=1 to restart redis")
alice, bob, _eve, chat = live.bootstrap()
msg = live.message(alice, chat, "before-redis-restart")["json"]
compose("restart", "redis")
wait_ready(live)
me = live.call("GET", "/api/v1/users/me", actor=alice, schema="User")["json"]
assert me["id"] == alice["id"]
page = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=100" % chat["id"],
actor=bob,
schema="MessagePage",
)["json"]
assert any(m["id"] == msg["id"] for m in page["items"])
+83
View File
@@ -0,0 +1,83 @@
"""Track 1: cache consistency — stampede, stale read, read-after-write."""
import json
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor
from client import Client
def _lab_metrics(c: Client):
return json.loads(c.call("GET", "/api/v1/_lab/metrics")["bytes"].decode())
def _lab_flags(c: Client, body):
return json.loads(c.call("POST", "/api/v1/_lab/flags", 200, body=body)["bytes"].decode())
def test_read_after_write(live: Client):
alice, bob, _eve, chat = live.bootstrap()
_lab_flags(live, {"invalidate": True, "singleflight": True, "reset_sql": True})
first = live.message(alice, chat, "raw-visible-" + uuid.uuid4().hex)["json"]
page = live.call(
"GET",
"/api/v1/chats/%s/messages?limit=100" % chat["id"],
actor=bob,
schema="MessagePage",
)["json"]
assert any(m["id"] == first["id"] for m in page["items"])
def test_stale_without_invalidation_then_fixed(live: Client):
alice, bob, _eve, chat = live.bootstrap()
path = "/api/v1/chats/%s/messages?limit=50" % chat["id"]
live.call("GET", path, actor=alice, schema="MessagePage")
_lab_flags(live, {"invalidate": False, "singleflight": True})
try:
stale_text = "stale-probe-" + uuid.uuid4().hex
live.message(alice, chat, stale_text)
page = live.call("GET", path, actor=bob, schema="MessagePage")["json"]
assert all(m["text"] != stale_text for m in page["items"])
finally:
_lab_flags(live, {"invalidate": True})
fresh_text = "fresh-probe-" + uuid.uuid4().hex
live.message(alice, chat, fresh_text)
page2 = live.call("GET", path, actor=bob, schema="MessagePage")["json"]
assert any(m["text"] == fresh_text for m in page2["items"])
def test_stampede_singleflight_reduces_sql(live: Client):
alice, _bob, _eve, chat = live.bootstrap()
live.message(alice, chat, "stampede-seed")
path = "/api/v1/chats/%s/messages?limit=50" % chat["id"]
def burst(n=24):
barrier = threading.Barrier(n)
def one(_i):
local = Client(live.base)
barrier.wait()
return local.call("GET", path, actor=alice, schema="MessagePage")
with ThreadPoolExecutor(max_workers=n) as pool:
list(pool.map(one, range(n)))
try:
_lab_flags(live, {"singleflight": False, "invalidate": True, "reset_sql": True, "delay_ms": 250})
live.message(alice, chat, "miss-" + uuid.uuid4().hex)
_lab_flags(live, {"reset_sql": True, "singleflight": False, "delay_ms": 250})
burst()
unprotected = _lab_metrics(live)["history_sql"]
_lab_flags(live, {"singleflight": True, "reset_sql": True, "delay_ms": 250})
live.message(alice, chat, "miss2-" + uuid.uuid4().hex)
_lab_flags(live, {"reset_sql": True, "singleflight": True, "delay_ms": 250})
burst()
protected = _lab_metrics(live)["history_sql"]
finally:
_lab_flags(live, {"invalidate": True, "singleflight": True, "delay_ms": 0})
print(json.dumps({"stampede_sql_without_singleflight": unprotected, "stampede_sql_with_singleflight": protected}))
assert unprotected >= 8, unprotected
assert protected < unprotected, (protected, unprotected)
assert protected <= 4, protected