From d17eb22925569748294778ec014f84104208f28e Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:09:16 +0300 Subject: [PATCH 01/15] =?UTF-8?q?Lab2:=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=20=D0=B8=D1=81=D1=85=D0=BE=D0=B4=D0=BD=D0=B8=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=20app/=20=D0=B8=20=D1=81=D0=BE=D0=B1=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=B8=D0=B7=20lab1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__init__.py | 0 app/main.py | 182 +++++++++++++++++++++++++++++++ app/models.py | 30 +++++ app/storage.py | 134 +++++++++++++++++++++++ scripts/track1_experiment.py | 89 +++++++++++++++ student-tests/concurrent_test.py | 83 ++++++++++++++ 6 files changed, 518 insertions(+) create mode 100644 app/__init__.py create mode 100644 app/main.py create mode 100644 app/models.py create mode 100644 app/storage.py create mode 100644 scripts/track1_experiment.py create mode 100644 student-tests/concurrent_test.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..0394a05a --- /dev/null +++ b/app/main.py @@ -0,0 +1,182 @@ +import os +import uuid +from contextvars import ContextVar +from typing import Optional + +from fastapi import Depends, FastAPI, Header, HTTPException, Query, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from starlette.middleware.base import BaseHTTPMiddleware + +from .models import CreateChat, CreateMessage, CreateUser +from .storage import storage + + +request_id_var: ContextVar[str] = ContextVar("request_id", default="") + + +def err(status_code: int, code: str, message: str) -> JSONResponse: + return JSONResponse( + status_code=status_code, + content={ + "error": { + "code": code, + "message": message, + "request_id": request_id_var.get(), + } + }, + ) + + +class RequestIdMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + rid = request.headers.get("X-Request-Id") or uuid.uuid4().hex + request_id_var.set(rid) + response = await call_next(request) + response.headers["X-Request-Id"] = rid + return response + + +app = FastAPI(title="MTUSI Messenger — lab1") +app.add_middleware(RequestIdMiddleware) + + +# ---------- exception handlers ---------- + +_HTTP_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", +} + + +@app.exception_handler(RequestValidationError) +async def _validation_handler(request: Request, exc: RequestValidationError): + # пустое сообщение, limit=0, чужой json и т.п. + return err(400, "invalid_request", "Validation error") + + +@app.exception_handler(HTTPException) +async def _http_handler(request: Request, exc: HTTPException): + code = _HTTP_CODES.get(exc.status_code, "internal_error") + message = exc.detail if isinstance(exc.detail, str) and exc.detail else "Error" + return err(exc.status_code, code, message) + + +@app.exception_handler(Exception) +async def _generic_handler(request: Request, exc: Exception): + # ноль деталей -> ошибка + return err(500, "internal_error", "Internal server error") + + +# dependency + +def get_actor(x_user_id: Optional[str] = Header(None, alias="X-User-Id")) -> dict: + if not x_user_id: + raise HTTPException(401, "Unauthorized") + user = storage.get_user(x_user_id) + if user is None: + raise HTTPException(401, "Unauthorized") + return user + + +# health + +@app.get("/health/live") +def health_live(): + return {"status": "ok"} + + +@app.get("/health/ready") +def health_ready(): + return {"status": "ok"} + + +# users + +@app.post("/api/v1/users", status_code=201) +def create_user(body: CreateUser): + user = storage.create_user(body.username, body.display_name) + if user is None: + raise HTTPException(409, "Username already exists") + return user + + +@app.get("/api/v1/users/me") +def current_user(actor: dict = Depends(get_actor)): + return actor + + +# chats + +@app.post("/api/v1/chats", status_code=201) +def create_chat(body: CreateChat, actor: dict = Depends(get_actor)): + if actor["id"] not in body.member_ids: + raise HTTPException(400, "Caller must be in member_ids") + chat, error = storage.create_chat(body.title, body.member_ids, actor["id"]) + if error == "member_not_found": + raise HTTPException(404, "Member not found") + if error: + raise HTTPException(400, error) + return chat + + +@app.get("/api/v1/chats") +def list_chats( + actor: dict = Depends(get_actor), + limit: int = Query(50, ge=1, le=100), + cursor: Optional[str] = Query(None, min_length=1), +): + items, next_cursor = storage.list_chats(actor["id"], limit, cursor) + return {"items": items, "next_cursor": next_cursor} + + +@app.get("/api/v1/chats/{chat_id}") +def get_chat(chat_id: str, actor: dict = Depends(get_actor)): + chat = storage.get_chat(chat_id, actor["id"]) + if chat is None: + raise HTTPException(404, "Chat not found") + return chat + + +# messages + +@app.post("/api/v1/chats/{chat_id}/messages", status_code=201) +def post_message( + chat_id: str, + body: CreateMessage, + actor: dict = Depends(get_actor), +): + msg, error = storage.post_message(chat_id, actor["id"], body.text) + if error == "chat_not_found": + raise HTTPException(404, "Chat not found") + if error: + raise HTTPException(400, error) + return msg + + +@app.get("/api/v1/chats/{chat_id}/messages") +def list_messages( + chat_id: str, + actor: dict = Depends(get_actor), + limit: int = Query(50, ge=1, le=100), + cursor: Optional[str] = Query(None, min_length=1), +): + items, next_cursor, error = storage.list_messages( + chat_id, actor["id"], limit, cursor + ) + if error == "chat_not_found": + raise HTTPException(404, "Chat not found") + return {"items": items, "next_cursor": next_cursor} + +if __name__ == "__main__": + import uvicorn + + port = int(os.getenv("PORT", "8080")) + uvicorn.run(app, host="0.0.0.0", port=port, log_level="info") \ No newline at end of file diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..9f612d5f --- /dev/null +++ b/app/models.py @@ -0,0 +1,30 @@ +from uuid import UUID +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class CreateUser(BaseModel): + model_config = ConfigDict(extra="forbid") + username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") + display_name: str = Field(min_length=1, max_length=100) + + +class CreateChat(BaseModel): + model_config = ConfigDict(extra="forbid") + title: str = Field(min_length=1, max_length=100) + member_ids: list[str] = Field(min_length=1, max_length=100) + + @field_validator("member_ids") + @classmethod + def _validate_members(cls, value: list[str]) -> list[str]: + seen: set[str] = set() + for raw in value: + UUID(raw) # бросит ValueError -> RequestValidationError -> 400 + if raw in seen: + raise ValueError("duplicate member_ids") + seen.add(raw) + return value + + +class CreateMessage(BaseModel): + model_config = ConfigDict(extra="forbid") + text: str = Field(min_length=1, max_length=4000) \ No newline at end of file diff --git a/app/storage.py b/app/storage.py new file mode 100644 index 00000000..97e3acc8 --- /dev/null +++ b/app/storage.py @@ -0,0 +1,134 @@ +import threading +import uuid +from datetime import datetime, timezone + + +def new_id() -> str: + return str(uuid.uuid4()) + + +def now_iso() -> str: + return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") + + +def _key_message(m: dict): + return (m["created_at"], uuid.UUID(m["id"]).int) + + +class Storage: + + def __init__(self) -> None: + self._lock = threading.RLock() + self.users: dict[str, dict] = {} + self.chats: dict[str, dict] = {} + self.messages: dict[str, list[dict]] = {} + + # users + def create_user(self, username: str, display_name: str) -> dict | None: + with self._lock: + for u in self.users.values(): + if u["username"] == username: + return None + uid = new_id() + user = { + "id": uid, + "username": username, + "display_name": display_name, + "created_at": now_iso(), + } + self.users[uid] = user + return dict(user) + + def get_user(self, uid: str) -> dict | None: + with self._lock: + u = self.users.get(uid) + return dict(u) if u else None + + # chats + def create_chat(self, title: str, member_ids: list[str], creator_id: str): + with self._lock: + for mid in member_ids: + if mid not in self.users: + return None, "member_not_found" + if creator_id not in member_ids: + return None, "creator_not_member" + cid = new_id() + chat = { + "id": cid, + "title": title, + "member_ids": list(member_ids), + "created_at": now_iso(), + } + self.chats[cid] = chat + self.messages[cid] = [] + return self._chat_public(chat), None + + def get_chat(self, chat_id: str, requester_id: str) -> dict | None: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or requester_id not in chat["member_ids"]: + return None + return self._chat_public(chat) + + def list_chats(self, requester_id: str, limit: int, cursor: str | None): + with self._lock: + items = [c for c in self.chats.values() if requester_id in c["member_ids"]] + items.sort(key=lambda c: (c["created_at"], uuid.UUID(c["id"]).int)) + start = 0 + if cursor: + for i, c in enumerate(items): + if c["id"] == cursor: + start = i + 1 + break + page = items[start:start + limit] + next_cursor = None + if len(page) == limit and start + limit < len(items): + next_cursor = page[-1]["id"] + return [self._chat_public(c) for c in page], next_cursor + + # messages + def post_message(self, chat_id: str, sender_id: str, text: str): + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or sender_id not in chat["member_ids"]: + return None, "chat_not_found" + msg = { + "id": new_id(), + "chat_id": chat_id, + "sender_id": sender_id, + "text": text, + "created_at": now_iso(), + } + self.messages[chat_id].append(msg) + return dict(msg), None + + def list_messages(self, chat_id: str, requester_id: str, + limit: int, cursor: str | None): + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or requester_id not in chat["member_ids"]: + return None, None, "chat_not_found" + items = self.messages[chat_id] # здесь было узкое место items = sorted(self.messages[chat_id], key=_key_message) + start = 0 + if cursor: + for i, m in enumerate(items): + if m["id"] == cursor: + start = i + 1 + break + page = items[start:start + limit] + next_cursor = None + if len(page) == limit and start + limit < len(items): + next_cursor = page[-1]["id"] + return [dict(m) for m in page], next_cursor, None + + @staticmethod + def _chat_public(chat: dict) -> dict: + return { + "id": chat["id"], + "title": chat["title"], + "member_ids": list(chat["member_ids"]), + "created_at": chat["created_at"], + } + + +storage = Storage() \ No newline at end of file diff --git a/scripts/track1_experiment.py b/scripts/track1_experiment.py new file mode 100644 index 00000000..b1b0703f --- /dev/null +++ b/scripts/track1_experiment.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 + +import json +import math +import time +import urllib.error +import urllib.request +import uuid +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +BASE = "http://127.0.0.1:8080" +OUT = Path(__file__).resolve().parents[1] / "evidence" / "track1.json" + + +def call(method, path, body=None, actor=None): + h = {"Accept": "application/json"} + if actor: + h["X-User-Id"] = actor + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=60) as r: + return r.status, json.loads(r.read()) + except urllib.error.HTTPError as e: + return e.code, None + + +def setup(history: int): + s, u = call("POST", "/api/v1/users", + {"username": f"load_{uuid.uuid4().hex[:10]}", + "display_name": "load"}) + assert s == 201 + s, chat = call("POST", "/api/v1/chats", + {"title": "load", "member_ids": [u["id"]]}, + actor=u["id"]) + assert s == 201 + for i in range(history): + s, _ = call("POST", f"/api/v1/chats/{chat['id']}/messages", + {"text": f"seed-{i}"}, actor=u["id"]) + assert s == 201 + return u, chat + + +def measure(history, concurrency, per_worker=50): + u, chat = setup(history) + lat = [] + + def worker(): + for _ in range(per_worker): + t0 = time.perf_counter() + s, _ = call("GET", f"/api/v1/chats/{chat['id']}/messages?limit=50", + actor=u["id"]) + if s == 200: + lat.append(time.perf_counter() - t0) + + t0 = time.perf_counter() + with ThreadPoolExecutor(max_workers=concurrency) as ex: + list(ex.map(lambda _: worker(), range(concurrency))) + elapsed = time.perf_counter() - t0 + + lat.sort() + def pct(p): + return round(1000 * lat[max(0, math.ceil(p * len(lat)) - 1)], 3) if lat else None + + return { + "history": history, + "concurrency": concurrency, + "successful_rps": round(len(lat) / elapsed, 2), + "p50_ms": pct(0.50), "p95_ms": pct(0.95), "p99_ms": pct(0.99), + } + + +def main(): + rows = [] + for history in (100, 1000, 10000): + for conc in (1, 10, 50): + r = measure(history, conc) + rows.append(r) + print(r) + OUT.parent.mkdir(parents=True, exist_ok=True) + OUT.write_text(json.dumps(rows, indent=2)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/student-tests/concurrent_test.py b/student-tests/concurrent_test.py new file mode 100644 index 00000000..db3bd3c2 --- /dev/null +++ b/student-tests/concurrent_test.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +"""10 клиентов × 10 сообщений = 100 уникальных. Запуск: python tests/concurrent_test.py""" +import json +import urllib.error +import urllib.request +import uuid +from concurrent.futures import ThreadPoolExecutor + +BASE = "http://localhost:8080" + + +def call(method, path, body=None, actor=None): + h = {"Accept": "application/json"} + if actor: + h["X-User-Id"] = actor + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=30) as r: + payload = r.read() + return r.status, (json.loads(payload) if payload else None) + except urllib.error.HTTPError as e: + payload = e.read() + return e.code, (json.loads(payload) if payload else None) + + +def create_user(name): + s, u = call("POST", "/api/v1/users", {"username": name, "display_name": name}) + assert s == 201, f"create_user {name}: {s} {u}" + return u + + +def main(): + users = [create_user(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)] + s, chat = call( + "POST", "/api/v1/chats", + {"title": "concurrent", "member_ids": [u["id"] for u in users]}, + actor=users[0]["id"], + ) + assert s == 201, f"chat: {s} {chat}" + cid = chat["id"] + + def post_10(user): + results = [] + for k in range(10): + s, m = call( + "POST", f"/api/v1/chats/{cid}/messages", + {"text": f"{user['username']}-{k}"}, actor=user["id"], + ) + results.append((s, m)) + return results + + with ThreadPoolExecutor(max_workers=10) as ex: + results = [] + for fut in [ex.submit(post_10, u) for u in users]: + results.extend(fut.result()) + + failures = [r for r in results if r[0] != 201] + assert not failures, f"{len(failures)} failed posts" + + seen, cursor = set(), None + while True: + path = f"/api/v1/chats/{cid}/messages?limit=100" + if cursor: + path += f"&cursor={cursor}" + s, page = call("GET", path, actor=users[0]["id"]) + assert s == 200 + for m in page["items"]: + seen.add(m["id"]) + cursor = page["next_cursor"] + if not cursor: + break + + print(f"unique messages: {len(seen)} / 100") + assert len(seen) == 100, f"expected 100, got {len(seen)}" + print("PASS: no lost or duplicated messages") + + +if __name__ == "__main__": + main() \ No newline at end of file -- 2.54.0 From fa79aa45f30c8082e1fe6204c9a7485f4a5c4926 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:17:50 +0300 Subject: [PATCH 02/15] =?UTF-8?q?requirements.txt=20=D0=B8=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D0=B0=20app/db,=20app/repo?= =?UTF-8?q?sitories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/db/__init__.py | 0 app/repositories/__init__.py | 0 requirements.txt | 12 ++++++++++++ 3 files changed, 12 insertions(+) create mode 100644 app/db/__init__.py create mode 100644 app/repositories/__init__.py create mode 100644 requirements.txt diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/repositories/__init__.py b/app/repositories/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..277e6cdc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +fastapi==0.110.0 +uvicorn[standard]==0.29.0 +pydantic==2.6.4 +sqlalchemy[asyncio]==2.0.29 +asyncpg==0.29.0 +alembic==1.13.1 +bcrypt==4.1.2 +python-dotenv==1.0.1 +redis==5.0.3 +httpx==0.27.0 +pytest==8.1.1 +pytest-asyncio==0.23.5 \ No newline at end of file -- 2.54.0 From 5311e192e2457171d883c0f9c2993b5e7bd7ffb2 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:20:21 +0300 Subject: [PATCH 03/15] =?UTF-8?q?SQLAlchemy=20=D0=BC=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B8=20users,=20chats,=20chat=5Fmembers,=20messages,=20?= =?UTF-8?q?sessions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/db/base.py | 5 +++ app/db/models.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++ app/db/session.py | 31 ++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 app/db/base.py create mode 100644 app/db/models.py create mode 100644 app/db/session.py diff --git a/app/db/base.py b/app/db/base.py new file mode 100644 index 00000000..1c2dcc40 --- /dev/null +++ b/app/db/base.py @@ -0,0 +1,5 @@ +from sqlalchemy.orm import DeclarativeBase + + +class Base(DeclarativeBase): + pass \ No newline at end of file diff --git a/app/db/models.py b/app/db/models.py new file mode 100644 index 00000000..89af0c04 --- /dev/null +++ b/app/db/models.py @@ -0,0 +1,100 @@ +from datetime import datetime, timezone +from uuid import UUID, uuid4 + +from sqlalchemy import DateTime, ForeignKey, Index, String, Text +from sqlalchemy.dialects.postgresql import UUID as PG_UUID +from sqlalchemy.orm import Mapped, mapped_column + +from .base import Base + + +def _now() -> datetime: + return datetime.now(timezone.utc) + + +class User(Base): + __tablename__ = "users" + id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), primary_key=True, default=uuid4 + ) + username: Mapped[str] = mapped_column( + String(32), unique=True, nullable=False, index=True + ) + display_name: Mapped[str] = mapped_column(String(100), nullable=False) + password_hash: Mapped[str] = mapped_column(Text, nullable=False) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=_now, nullable=False + ) + + +class Chat(Base): + __tablename__ = "chats" + id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), primary_key=True, default=uuid4 + ) + title: Mapped[str] = mapped_column(String(100), nullable=False) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=_now, nullable=False + ) + + __table_args__ = (Index("idx_chats_created_id", "created_at", "id"),) + + +class ChatMember(Base): + __tablename__ = "chat_members" + chat_id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + primary_key=True, + ) + user_id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + primary_key=True, + ) + + __table_args__ = (Index("idx_chat_members_user", "user_id", "chat_id"),) + + +class Message(Base): + __tablename__ = "messages" + id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), primary_key=True, default=uuid4 + ) + chat_id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + nullable=False, + ) + sender_id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), ForeignKey("users.id"), nullable=False + ) + text: Mapped[str] = mapped_column(Text, nullable=False) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=_now, nullable=False + ) + + __table_args__ = ( + Index("idx_messages_chat_created_id", "chat_id", "created_at", "id"), + ) + + +class SessionRow(Base): + __tablename__ = "sessions" + id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), primary_key=True, default=uuid4 + ) + user_id: Mapped[UUID] = mapped_column( + PG_UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + nullable=False, + ) + token_hash: Mapped[str] = mapped_column( + String(64), unique=True, nullable=False, index=True + ) + expires_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), nullable=False, index=True + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=_now, nullable=False + ) \ No newline at end of file diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 00000000..5bbb910b --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,31 @@ +import os + +from sqlalchemy.ext.asyncio import ( + AsyncSession, + async_sessionmaker, + create_async_engine, +) + +DATABASE_URL = os.getenv( + "DATABASE_URL", + "postgresql+asyncpg://messenger:strongpassword@localhost:5432/messenger", +) + +engine = create_async_engine( + DATABASE_URL, + pool_size=10, + max_overflow=20, + pool_timeout=5, + pool_recycle=1800, + pool_pre_ping=True, + connect_args={"timeout": 5, "command_timeout": 10}, +) + +async_session = async_sessionmaker( + engine, class_=AsyncSession, expire_on_commit=False +) + + +async def get_db() -> AsyncSession: + async with async_session() as session: + yield session \ No newline at end of file -- 2.54.0 From b53ccaf0085133562c04efdd795d263d7874c320 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:32:14 +0300 Subject: [PATCH 04/15] =?UTF-8?q?feat:=20bcrypt=20password=20KDF=20=D0=B8?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D1=81=D0=B5=D1=81?= =?UTF-8?q?=D1=81=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/auth.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/auth.py diff --git a/app/auth.py b/app/auth.py new file mode 100644 index 00000000..8535d735 --- /dev/null +++ b/app/auth.py @@ -0,0 +1,78 @@ +import hashlib +import os +import secrets +from datetime import datetime, timezone + +import bcrypt +from fastapi import Depends, Header, HTTPException +from sqlalchemy import delete, select +from sqlalchemy.ext.asyncio import AsyncSession + +from .db.models import SessionRow, User +from .db.session import get_db + +SESSION_TTL = int(os.getenv("SESSION_TTL_SECONDS", "3600")) + + +def _prehash(password: str) -> bytes: + # bcrypt работает с ≤72 байт, SHA-256 даёт 32 байта и снимает ограничение + return hashlib.sha256(password.encode("utf-8")).digest() + + +def hash_password(password: str) -> str: + return bcrypt.hashpw(_prehash(password), bcrypt.gensalt(rounds=12)).decode("ascii") + + +def verify_password(password: str, password_hash: str) -> bool: + try: + return bcrypt.checkpw(_prehash(password), password_hash.encode("ascii")) + except ValueError: + return False + + +def generate_token() -> str: + return secrets.token_urlsafe(32) + + +def hash_token(token: str) -> str: + return hashlib.sha256(token.encode("ascii")).hexdigest() + + +def _unauthorized_bearer() -> HTTPException: + return HTTPException( + status_code=401, + detail="Unauthorized", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + + +async def get_current_user( + authorization: str | None = Header(None, alias="Authorization"), + db: AsyncSession = Depends(get_db), +) -> User: + if not authorization or not authorization.startswith("Bearer "): + raise _unauthorized_bearer() + token = authorization[7:] + token_hash = hash_token(token) + now = datetime.now(timezone.utc) + stmt = select(SessionRow).where( + SessionRow.token_hash == token_hash, + SessionRow.expires_at > now, + ) + session = (await db.execute(stmt)).scalar_one_or_none() + if session is None: + raise _unauthorized_bearer() + user = await db.get(User, session.user_id) + if user is None: + raise _unauthorized_bearer() + return user + + +async def revoke_current_session(authorization: str, db: AsyncSession) -> None: + if not authorization or not authorization.startswith("Bearer "): + raise _unauthorized_bearer() + token = authorization[7:] + await db.execute( + delete(SessionRow).where(SessionRow.token_hash == hash_token(token)) + ) + await db.commit() \ No newline at end of file -- 2.54.0 From 82e8fee7f95cbc79234752c05e5ac9945059e88d Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Wed, 30 Sep 2026 23:59:07 +0300 Subject: [PATCH 05/15] =?UTF-8?q?pydantic-=D1=81=D1=85=D0=B5=D0=BC=D1=8B?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=20password=20=D0=B8=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models.py | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/app/models.py b/app/models.py index 9f612d5f..19dc8595 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,5 @@ from uuid import UUID + from pydantic import BaseModel, ConfigDict, Field, field_validator @@ -6,25 +7,50 @@ class CreateUser(BaseModel): model_config = ConfigDict(extra="forbid") username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") display_name: str = Field(min_length=1, max_length=100) + password: str = Field(min_length=12, max_length=128) + + @field_validator("display_name", "password") + @classmethod + def _not_blank(cls, v: str) -> str: + if not v.strip(): + raise ValueError("must not be blank") + return v class CreateChat(BaseModel): model_config = ConfigDict(extra="forbid") title: str = Field(min_length=1, max_length=100) - member_ids: list[str] = Field(min_length=1, max_length=100) + member_ids: list[UUID] = Field(min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def _title_not_blank(cls, v: str) -> str: + if not v.strip(): + raise ValueError("title must not be blank") + return v @field_validator("member_ids") @classmethod - def _validate_members(cls, value: list[str]) -> list[str]: - seen: set[str] = set() - for raw in value: - UUID(raw) # бросит ValueError -> RequestValidationError -> 400 - if raw in seen: - raise ValueError("duplicate member_ids") - seen.add(raw) - return value + def _unique_members(cls, v: list[UUID]) -> list[UUID]: + if len(set(v)) != len(v): + raise ValueError("duplicate member_ids") + return v class CreateMessage(BaseModel): model_config = ConfigDict(extra="forbid") - text: str = Field(min_length=1, max_length=4000) \ No newline at end of file + text: str = Field(min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def _text_not_blank(cls, v: str) -> str: + if not v.strip(): + raise ValueError("text must not be blank") + return v + + +class SessionResponse(BaseModel): + token: str + token_type: str = "Bearer" + expires_at: str + user: dict \ No newline at end of file -- 2.54.0 From c4fc878861d30f6ee47bf7a3563b438ccbc08ea1 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 00:06:11 +0300 Subject: [PATCH 06/15] =?UTF-8?q?=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B8=20users,=20chats,=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/repositories/chats.py | 73 ++++++++++++++++++++++++++++++++++++ app/repositories/messages.py | 44 ++++++++++++++++++++++ app/repositories/users.py | 28 ++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 app/repositories/chats.py create mode 100644 app/repositories/messages.py create mode 100644 app/repositories/users.py diff --git a/app/repositories/chats.py b/app/repositories/chats.py new file mode 100644 index 00000000..a1b60091 --- /dev/null +++ b/app/repositories/chats.py @@ -0,0 +1,73 @@ +from datetime import datetime +from uuid import UUID + +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from ..db.models import Chat, ChatMember, User + + +async def create_atomic( + db: AsyncSession, title: str, member_ids: list[UUID] +) -> Chat: + async with db.begin(): + found = ( + (await db.execute(select(User.id).where(User.id.in_(member_ids)))) + .scalars() + .all() + ) + if len(set(found)) != len(set(member_ids)): + raise ValueError("member_not_found") + + chat = Chat(title=title) + db.add(chat) + await db.flush() + for uid in member_ids: + db.add(ChatMember(chat_id=chat.id, user_id=uid)) + + await db.refresh(chat) + return chat + + +async def get_if_member( + db: AsyncSession, chat_id: UUID, user_id: UUID +) -> Chat | None: + stmt = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(Chat.id == chat_id, ChatMember.user_id == user_id) + ) + return (await db.execute(stmt)).scalar_one_or_none() + + +async def list_member_ids(db: AsyncSession, chat_id: UUID) -> list[UUID]: + stmt = select(ChatMember.user_id).where(ChatMember.chat_id == chat_id) + return list((await db.execute(stmt)).scalars().all()) + + +async def list_for_user( + db: AsyncSession, + user_id: UUID, + limit: int, + cursor: tuple[datetime, UUID] | None, +) -> tuple[list[Chat], tuple[datetime, UUID] | None]: + stmt = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(ChatMember.user_id == user_id) + .order_by(Chat.created_at.asc(), Chat.id.asc()) + .limit(limit + 1) + ) + if cursor is not None: + c_created, c_id = cursor + stmt = stmt.where( + (Chat.created_at > c_created) + | ((Chat.created_at == c_created) & (Chat.id > c_id)) + ) + rows = list((await db.execute(stmt)).scalars().all()) + next_cursor = None + if len(rows) > limit: + rows = rows[:limit] + last = rows[-1] + next_cursor = (last.created_at, last.id) + return rows, next_cursor \ No newline at end of file diff --git a/app/repositories/messages.py b/app/repositories/messages.py new file mode 100644 index 00000000..81df513a --- /dev/null +++ b/app/repositories/messages.py @@ -0,0 +1,44 @@ +from datetime import datetime +from uuid import UUID + +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from ..db.models import Message + + +async def create( + db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str +) -> Message: + msg = Message(chat_id=chat_id, sender_id=sender_id, text=text) + db.add(msg) + await db.commit() + await db.refresh(msg) + return msg + + +async def list_page( + db: AsyncSession, + chat_id: UUID, + limit: int, + cursor: tuple[datetime, UUID] | None, +) -> tuple[list[Message], tuple[datetime, UUID] | None]: + stmt = ( + select(Message) + .where(Message.chat_id == chat_id) + .order_by(Message.created_at.asc(), Message.id.asc()) + .limit(limit + 1) + ) + if cursor is not None: + c_created, c_id = cursor + stmt = stmt.where( + (Message.created_at > c_created) + | ((Message.created_at == c_created) & (Message.id > c_id)) + ) + rows = list((await db.execute(stmt)).scalars().all()) + next_cursor = None + if len(rows) > limit: + rows = rows[:limit] + last = rows[-1] + next_cursor = (last.created_at, last.id) + return rows, next_cursor \ No newline at end of file diff --git a/app/repositories/users.py b/app/repositories/users.py new file mode 100644 index 00000000..58792ea0 --- /dev/null +++ b/app/repositories/users.py @@ -0,0 +1,28 @@ +from uuid import UUID + +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from ..db.models import User + + +async def get_by_username(db: AsyncSession, username: str) -> User | None: + return ( + await db.execute(select(User).where(User.username == username)) + ).scalar_one_or_none() + + +async def get_by_id(db: AsyncSession, user_id: UUID) -> User | None: + return await db.get(User, user_id) + + +async def create( + db: AsyncSession, username: str, display_name: str, password_hash: str +) -> User: + user = User( + username=username, display_name=display_name, password_hash=password_hash + ) + db.add(user) + await db.commit() + await db.refresh(user) + return user \ No newline at end of file -- 2.54.0 From c2672b4cacb4987cac71ba881fb65b39c63431f7 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 00:34:58 +0300 Subject: [PATCH 07/15] =?UTF-8?q?main.py=20=D0=BD=D0=B0=20PostgreSQL=20?= =?UTF-8?q?=D0=B8=20Bearer-=D0=B0=D1=83=D1=82=D0=B5=D0=BD=D1=82=D0=B8?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 278 ++++++++++++++++++++++++++++++++++--------------- app/storage.py | 134 ------------------------ 2 files changed, 193 insertions(+), 219 deletions(-) delete mode 100644 app/storage.py diff --git a/app/main.py b/app/main.py index 0394a05a..35f239fd 100644 --- a/app/main.py +++ b/app/main.py @@ -1,23 +1,52 @@ import os import uuid from contextvars import ContextVar +from datetime import datetime, timedelta, timezone from typing import Optional +from uuid import UUID from fastapi import Depends, FastAPI, Header, HTTPException, Query, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse +from fastapi.security import HTTPBasic, HTTPBasicCredentials +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession from starlette.middleware.base import BaseHTTPMiddleware +from .auth import ( + SESSION_TTL, + generate_token, + get_current_user, + hash_password, + hash_token, + revoke_current_session, + verify_password, +) +from .db.models import SessionRow, User +from .db.session import get_db from .models import CreateChat, CreateMessage, CreateUser -from .storage import storage - +from .repositories import chats as chats_repo +from .repositories import messages as messages_repo +from .repositories import users as users_repo request_id_var: ContextVar[str] = ContextVar("request_id", default="") +_HTTP_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", +} -def err(status_code: int, code: str, message: str) -> JSONResponse: + +def err_json(status: int, code: str, message: str) -> JSONResponse: return JSONResponse( - status_code=status_code, + status_code=status, content={ "error": { "code": code, @@ -37,146 +66,225 @@ class RequestIdMiddleware(BaseHTTPMiddleware): return response -app = FastAPI(title="MTUSI Messenger — lab1") +app = FastAPI(title="MTUSI Messenger — lab2") app.add_middleware(RequestIdMiddleware) - - -# ---------- exception handlers ---------- - -_HTTP_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", -} +security = HTTPBasic(auto_error=False) @app.exception_handler(RequestValidationError) -async def _validation_handler(request: Request, exc: RequestValidationError): - # пустое сообщение, limit=0, чужой json и т.п. - return err(400, "invalid_request", "Validation error") +async def _validation(request: Request, exc: RequestValidationError): + return err_json(400, "invalid_request", "Validation error") @app.exception_handler(HTTPException) -async def _http_handler(request: Request, exc: HTTPException): +async def _http(request: Request, exc: HTTPException): code = _HTTP_CODES.get(exc.status_code, "internal_error") - message = exc.detail if isinstance(exc.detail, str) and exc.detail else "Error" - return err(exc.status_code, code, message) + detail = exc.detail if isinstance(exc.detail, str) else "Error" + resp = err_json(exc.status_code, code, detail) + if exc.headers: + for k, v in exc.headers.items(): + resp.headers[k] = v + return resp @app.exception_handler(Exception) -async def _generic_handler(request: Request, exc: Exception): - # ноль деталей -> ошибка - return err(500, "internal_error", "Internal server error") +async def _generic(request: Request, exc: Exception): + return err_json(500, "internal_error", "Internal server error") -# dependency - -def get_actor(x_user_id: Optional[str] = Header(None, alias="X-User-Id")) -> dict: - if not x_user_id: - raise HTTPException(401, "Unauthorized") - user = storage.get_user(x_user_id) - if user is None: - raise HTTPException(401, "Unauthorized") - return user +def _iso_z(dt: datetime) -> str: + return dt.isoformat().replace("+00:00", "Z") -# health +def _user_json(u: User) -> dict: + return { + "id": str(u.id), + "username": u.username, + "display_name": u.display_name, + "created_at": _iso_z(u.created_at), + } + +def _chat_json(chat, member_ids: list[str]) -> dict: + return { + "id": str(chat.id), + "title": chat.title, + "member_ids": member_ids, + "created_at": _iso_z(chat.created_at), + } + + +def _msg_json(m) -> dict: + return { + "id": str(m.id), + "chat_id": str(m.chat_id), + "sender_id": str(m.sender_id), + "text": m.text, + "created_at": _iso_z(m.created_at), + } + + +# ---------- health ---------- @app.get("/health/live") -def health_live(): +def live(): return {"status": "ok"} @app.get("/health/ready") -def health_ready(): +async def ready(db: AsyncSession = Depends(get_db)): + try: + await db.execute(text("SELECT 1")) + except Exception: + return err_json(503, "unavailable", "Database unavailable") return {"status": "ok"} -# users - +# ---------- users ---------- @app.post("/api/v1/users", status_code=201) -def create_user(body: CreateUser): - user = storage.create_user(body.username, body.display_name) - if user is None: +async def register(body: CreateUser, db: AsyncSession = Depends(get_db)): + existing = await users_repo.get_by_username(db, body.username) + if existing is not None: raise HTTPException(409, "Username already exists") - return user + try: + user = await users_repo.create( + db, body.username, body.display_name, hash_password(body.password) + ) + except Exception: + raise HTTPException(409, "Username already exists") + return _user_json(user) + + +# ---------- sessions ---------- +@app.post("/api/v1/auth/sessions", status_code=201) +async def login( + creds: Optional[HTTPBasicCredentials] = Depends(security), + db: AsyncSession = Depends(get_db), +): + basic_challenge = 'Basic realm="messenger", charset="UTF-8"' + if creds is None: + raise HTTPException( + status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": basic_challenge} + ) + user = await users_repo.get_by_username(db, creds.username) + if user is None or not verify_password(creds.password, user.password_hash): + raise HTTPException( + status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": basic_challenge} + ) + token = generate_token() + expires = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL) + db.add( + SessionRow(user_id=user.id, token_hash=hash_token(token), expires_at=expires) + ) + await db.commit() + return { + "token": token, + "token_type": "Bearer", + "expires_at": _iso_z(expires), + "user": _user_json(user), + } + + +@app.delete("/api/v1/auth/sessions/current", status_code=204) +async def logout(authorization: str = Header(...), db: AsyncSession = Depends(get_db)): + await revoke_current_session(authorization, db) @app.get("/api/v1/users/me") -def current_user(actor: dict = Depends(get_actor)): - return actor +async def me(user: User = Depends(get_current_user)): + return _user_json(user) -# chats - +# ---------- chats ---------- @app.post("/api/v1/chats", status_code=201) -def create_chat(body: CreateChat, actor: dict = Depends(get_actor)): - if actor["id"] not in body.member_ids: +async def create_chat( + body: CreateChat, + user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + if user.id not in body.member_ids: raise HTTPException(400, "Caller must be in member_ids") - chat, error = storage.create_chat(body.title, body.member_ids, actor["id"]) - if error == "member_not_found": + try: + chat = await chats_repo.create_atomic(db, body.title, body.member_ids) + except ValueError: raise HTTPException(404, "Member not found") - if error: - raise HTTPException(400, error) - return chat + return _chat_json(chat, [str(x) for x in body.member_ids]) @app.get("/api/v1/chats") -def list_chats( - actor: dict = Depends(get_actor), +async def list_chats( + user: User = Depends(get_current_user), limit: int = Query(50, ge=1, le=100), cursor: Optional[str] = Query(None, min_length=1), + db: AsyncSession = Depends(get_db), ): - items, next_cursor = storage.list_chats(actor["id"], limit, cursor) - return {"items": items, "next_cursor": next_cursor} + parsed = None + if cursor is not None: + try: + c_created, c_id = cursor.split("|", 1) + parsed = (datetime.fromisoformat(c_created), UUID(c_id)) + except Exception: + raise HTTPException(400, "Invalid cursor") + rows, next_cursor = await chats_repo.list_for_user(db, user.id, limit, parsed) + items = [] + for chat in rows: + members = await chats_repo.list_member_ids(db, chat.id) + items.append(_chat_json(chat, [str(m) for m in members])) + nxt = f"{next_cursor[0].isoformat()}|{next_cursor[1]}" if next_cursor else None + return {"items": items, "next_cursor": nxt} @app.get("/api/v1/chats/{chat_id}") -def get_chat(chat_id: str, actor: dict = Depends(get_actor)): - chat = storage.get_chat(chat_id, actor["id"]) +async def get_chat( + chat_id: UUID, + user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + chat = await chats_repo.get_if_member(db, chat_id, user.id) if chat is None: raise HTTPException(404, "Chat not found") - return chat + members = await chats_repo.list_member_ids(db, chat.id) + return _chat_json(chat, [str(m) for m in members]) -# messages - +# ---------- messages ---------- @app.post("/api/v1/chats/{chat_id}/messages", status_code=201) -def post_message( - chat_id: str, +async def post_message( + chat_id: UUID, body: CreateMessage, - actor: dict = Depends(get_actor), + user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), ): - msg, error = storage.post_message(chat_id, actor["id"], body.text) - if error == "chat_not_found": + chat = await chats_repo.get_if_member(db, chat_id, user.id) + if chat is None: raise HTTPException(404, "Chat not found") - if error: - raise HTTPException(400, error) - return msg + msg = await messages_repo.create(db, chat_id, user.id, body.text) + return _msg_json(msg) @app.get("/api/v1/chats/{chat_id}/messages") -def list_messages( - chat_id: str, - actor: dict = Depends(get_actor), +async def list_messages( + chat_id: UUID, + user: User = Depends(get_current_user), limit: int = Query(50, ge=1, le=100), cursor: Optional[str] = Query(None, min_length=1), + db: AsyncSession = Depends(get_db), ): - items, next_cursor, error = storage.list_messages( - chat_id, actor["id"], limit, cursor - ) - if error == "chat_not_found": + chat = await chats_repo.get_if_member(db, chat_id, user.id) + if chat is None: raise HTTPException(404, "Chat not found") - return {"items": items, "next_cursor": next_cursor} + parsed = None + if cursor is not None: + try: + c_created, c_id = cursor.split("|", 1) + parsed = (datetime.fromisoformat(c_created), UUID(c_id)) + except Exception: + raise HTTPException(400, "Invalid cursor") + rows, next_cursor = await messages_repo.list_page(db, chat_id, limit, parsed) + nxt = f"{next_cursor[0].isoformat()}|{next_cursor[1]}" if next_cursor else None + return {"items": [_msg_json(m) for m in rows], "next_cursor": nxt} + if __name__ == "__main__": import uvicorn - port = int(os.getenv("PORT", "8080")) - uvicorn.run(app, host="0.0.0.0", port=port, log_level="info") \ No newline at end of file + uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("APP_PORT", "8080"))) \ No newline at end of file diff --git a/app/storage.py b/app/storage.py deleted file mode 100644 index 97e3acc8..00000000 --- a/app/storage.py +++ /dev/null @@ -1,134 +0,0 @@ -import threading -import uuid -from datetime import datetime, timezone - - -def new_id() -> str: - return str(uuid.uuid4()) - - -def now_iso() -> str: - return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") - - -def _key_message(m: dict): - return (m["created_at"], uuid.UUID(m["id"]).int) - - -class Storage: - - def __init__(self) -> None: - self._lock = threading.RLock() - self.users: dict[str, dict] = {} - self.chats: dict[str, dict] = {} - self.messages: dict[str, list[dict]] = {} - - # users - def create_user(self, username: str, display_name: str) -> dict | None: - with self._lock: - for u in self.users.values(): - if u["username"] == username: - return None - uid = new_id() - user = { - "id": uid, - "username": username, - "display_name": display_name, - "created_at": now_iso(), - } - self.users[uid] = user - return dict(user) - - def get_user(self, uid: str) -> dict | None: - with self._lock: - u = self.users.get(uid) - return dict(u) if u else None - - # chats - def create_chat(self, title: str, member_ids: list[str], creator_id: str): - with self._lock: - for mid in member_ids: - if mid not in self.users: - return None, "member_not_found" - if creator_id not in member_ids: - return None, "creator_not_member" - cid = new_id() - chat = { - "id": cid, - "title": title, - "member_ids": list(member_ids), - "created_at": now_iso(), - } - self.chats[cid] = chat - self.messages[cid] = [] - return self._chat_public(chat), None - - def get_chat(self, chat_id: str, requester_id: str) -> dict | None: - with self._lock: - chat = self.chats.get(chat_id) - if chat is None or requester_id not in chat["member_ids"]: - return None - return self._chat_public(chat) - - def list_chats(self, requester_id: str, limit: int, cursor: str | None): - with self._lock: - items = [c for c in self.chats.values() if requester_id in c["member_ids"]] - items.sort(key=lambda c: (c["created_at"], uuid.UUID(c["id"]).int)) - start = 0 - if cursor: - for i, c in enumerate(items): - if c["id"] == cursor: - start = i + 1 - break - page = items[start:start + limit] - next_cursor = None - if len(page) == limit and start + limit < len(items): - next_cursor = page[-1]["id"] - return [self._chat_public(c) for c in page], next_cursor - - # messages - def post_message(self, chat_id: str, sender_id: str, text: str): - with self._lock: - chat = self.chats.get(chat_id) - if chat is None or sender_id not in chat["member_ids"]: - return None, "chat_not_found" - msg = { - "id": new_id(), - "chat_id": chat_id, - "sender_id": sender_id, - "text": text, - "created_at": now_iso(), - } - self.messages[chat_id].append(msg) - return dict(msg), None - - def list_messages(self, chat_id: str, requester_id: str, - limit: int, cursor: str | None): - with self._lock: - chat = self.chats.get(chat_id) - if chat is None or requester_id not in chat["member_ids"]: - return None, None, "chat_not_found" - items = self.messages[chat_id] # здесь было узкое место items = sorted(self.messages[chat_id], key=_key_message) - start = 0 - if cursor: - for i, m in enumerate(items): - if m["id"] == cursor: - start = i + 1 - break - page = items[start:start + limit] - next_cursor = None - if len(page) == limit and start + limit < len(items): - next_cursor = page[-1]["id"] - return [dict(m) for m in page], next_cursor, None - - @staticmethod - def _chat_public(chat: dict) -> dict: - return { - "id": chat["id"], - "title": chat["title"], - "member_ids": list(chat["member_ids"]), - "created_at": chat["created_at"], - } - - -storage = Storage() \ No newline at end of file -- 2.54.0 From f3a6f7a62ff62af4199035a88e7b11ab6e3fa495 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 02:43:08 +0300 Subject: [PATCH 08/15] =?UTF-8?q?Alembic=20migrations=20=D0=B8=20Docker=20?= =?UTF-8?q?Compose=20=D1=81=20postgres=20=D0=B8=20redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- Dockerfile | 27 +++++ alembic.ini | 114 ++++++++++++++++++ compose.yaml | 54 +++++++++ migrations/README | 1 + migrations/env.py | 99 +++++++++++++++ migrations/script.py.mako | 26 ++++ .../versions/44ea9d3b68ac_initial_schema.py | 85 +++++++++++++ student-tests/concurrent_test.py | 54 +++++---- student-tests/test_rollback.py | 61 ++++++++++ 10 files changed, 500 insertions(+), 25 deletions(-) create mode 100644 Dockerfile create mode 100644 alembic.ini create mode 100644 compose.yaml create mode 100644 migrations/README create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/44ea9d3b68ac_initial_schema.py create mode 100644 student-tests/test_rollback.py diff --git a/.gitignore b/.gitignore index b5a71896..9bbbd61c 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,5 @@ coverage/ .DS_Store .idea/ .vscode/ -# Keep small, sanitized evidence summaries in evidence/ under version control. -# Inspect git diff --cached: ignore rules are not a secret scanner. +evidence/track1.json +evidence/track1_run.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..207638eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# syntax=docker/dockerfile:1.7 +FROM python:3.11.9-slim AS builder +WORKDIR /build +COPY requirements.txt . +RUN pip install --no-cache-dir --user -r requirements.txt + +FROM python:3.11.9-slim +RUN useradd -m -u 10001 appuser + +WORKDIR /app +COPY --from=builder /root/.local /home/appuser/.local +COPY app ./app +COPY migrations ./migrations +COPY alembic.ini . + +ENV PATH=/home/appuser/.local/bin:$PATH \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +RUN chown -R appuser:appuser /app +USER appuser +EXPOSE 8080 + +HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=5 \ + CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).status==200 else 1)" + +CMD ["sh", "-c", "alembic upgrade head && python -m app.main"] \ No newline at end of file diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..71e7425c --- /dev/null +++ b/alembic.ini @@ -0,0 +1,114 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = migrations + +# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s +# Uncomment the line below if you want the files to be prepended with date and time +# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s + +# sys.path path, will be prepended to sys.path if present. +# defaults to the current working directory. +prepend_sys_path = . + +# timezone to use when rendering the date within the migration file +# as well as the filename. +# If specified, requires the python>=3.9 or backports.zoneinfo library. +# Any required deps can installed by adding `alembic[tz]` to the pip requirements +# string value is passed to ZoneInfo() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +# truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; This defaults +# to migrations/versions. When using multiple version +# directories, initial revisions must be specified with --version-path. +# The path separator used here should be the separator specified by "version_path_separator" below. +# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions + +# version path separator; As mentioned above, this is the character used to split +# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. +# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. +# Valid values for version_path_separator are: +# +# version_path_separator = : +# version_path_separator = ; +# version_path_separator = space +version_path_separator = os # Use os.pathsep. Default configuration used for new projects. + +# set to 'true' to search source files recursively +# in each "version_locations" directory +# new in Alembic version 1.10 +# recursive_version_locations = false + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = driver://user:pass@localhost/dbname + + +[post_write_hooks] +# post_write_hooks defines scripts or Python functions that are run +# on newly generated revision scripts. See the documentation for further +# detail and examples + +# format using "black" - use the console_scripts runner, against the "black" entrypoint +# hooks = black +# black.type = console_scripts +# black.entrypoint = black +# black.options = -l 79 REVISION_SCRIPT_FILENAME + +# lint with attempts to fix using "ruff" - use the exec runner, execute a binary +# hooks = ruff +# ruff.type = exec +# ruff.executable = %(here)s/.venv/bin/ruff +# ruff.options = --fix REVISION_SCRIPT_FILENAME + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..514566a4 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,54 @@ +services: + postgres: + image: postgres:16.2-alpine + environment: + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] + interval: 5s + timeout: 5s + retries: 10 + ports: + - "127.0.0.1:5432:5432" + + redis: + image: redis:7.2-alpine + command: ["redis-server", "--appendonly", "yes"] + volumes: + - redisdata:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 10 + ports: + - "127.0.0.1:6379:6379" + + api: + build: . + ports: + - "127.0.0.1:8080:8080" + env_file: .env + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + healthcheck: + test: + - "CMD" + - "python" + - "-c" + - "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2).status==200 else 1)" + interval: 10s + timeout: 5s + retries: 10 + start_period: 20s + +volumes: + pgdata: + redisdata: \ No newline at end of file diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..e0d0858f --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration with an async dbapi. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..ee06e32b --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,99 @@ +import asyncio +from logging.config import fileConfig + +from sqlalchemy import pool +from sqlalchemy.engine import Connection +from sqlalchemy.ext.asyncio import async_engine_from_config + +from alembic import context +import os + +from app.db import models # noqa: F401 — регистрирует таблицы +from app.db.base import Base +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = Base.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def do_run_migrations(connection: Connection) -> None: + context.configure(connection=connection, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +async def run_async_migrations() -> None: + """In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + config.set_main_option( + "sqlalchemy.url", + os.getenv( + "DATABASE_URL", + "postgresql+asyncpg://messenger:strongpassword@localhost:5432/messenger", + ), +) + connectable = async_engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + async with connectable.connect() as connection: + await connection.run_sync(do_run_migrations) + + await connectable.dispose() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode.""" + + asyncio.run(run_async_migrations()) + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 00000000..fbc4b07d --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,26 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision: str = ${repr(up_revision)} +down_revision: Union[str, None] = ${repr(down_revision)} +branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} +depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} + + +def upgrade() -> None: + ${upgrades if upgrades else "pass"} + + +def downgrade() -> None: + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/44ea9d3b68ac_initial_schema.py b/migrations/versions/44ea9d3b68ac_initial_schema.py new file mode 100644 index 00000000..62b49d14 --- /dev/null +++ b/migrations/versions/44ea9d3b68ac_initial_schema.py @@ -0,0 +1,85 @@ +"""initial schema + +Revision ID: 44ea9d3b68ac +Revises: +Create Date: 2026-10-01 02:42:35.498512 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '44ea9d3b68ac' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('chats', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('title', sa.String(length=100), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('idx_chats_created_id', 'chats', ['created_at', 'id'], unique=False) + op.create_table('users', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('username', sa.String(length=32), nullable=False), + sa.Column('display_name', sa.String(length=100), nullable=False), + sa.Column('password_hash', sa.Text(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) + op.create_table('chat_members', + sa.Column('chat_id', sa.UUID(), nullable=False), + sa.Column('user_id', sa.UUID(), nullable=False), + sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('chat_id', 'user_id') + ) + op.create_index('idx_chat_members_user', 'chat_members', ['user_id', 'chat_id'], unique=False) + op.create_table('messages', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('chat_id', sa.UUID(), nullable=False), + sa.Column('sender_id', sa.UUID(), nullable=False), + sa.Column('text', sa.Text(), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index('idx_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'], unique=False) + op.create_table('sessions', + sa.Column('id', sa.UUID(), nullable=False), + sa.Column('user_id', sa.UUID(), nullable=False), + sa.Column('token_hash', sa.String(length=64), nullable=False), + sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False), + sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_sessions_expires_at'), 'sessions', ['expires_at'], unique=False) + op.create_index(op.f('ix_sessions_token_hash'), 'sessions', ['token_hash'], unique=True) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_sessions_token_hash'), table_name='sessions') + op.drop_index(op.f('ix_sessions_expires_at'), table_name='sessions') + op.drop_table('sessions') + op.drop_index('idx_messages_chat_created_id', table_name='messages') + op.drop_table('messages') + op.drop_index('idx_chat_members_user', table_name='chat_members') + op.drop_table('chat_members') + op.drop_index(op.f('ix_users_username'), table_name='users') + op.drop_table('users') + op.drop_index('idx_chats_created_id', table_name='chats') + op.drop_table('chats') + # ### end Alembic commands ### diff --git a/student-tests/concurrent_test.py b/student-tests/concurrent_test.py index db3bd3c2..f8e46d26 100644 --- a/student-tests/concurrent_test.py +++ b/student-tests/concurrent_test.py @@ -1,18 +1,21 @@ #!/usr/bin/env python3 -"""10 клиентов × 10 сообщений = 100 уникальных. Запуск: python tests/concurrent_test.py""" +"""10 клиентов × 10 сообщений = 100 уникальных.""" +import asyncio +import base64 import json -import urllib.error import urllib.request +import urllib.error import uuid from concurrent.futures import ThreadPoolExecutor -BASE = "http://localhost:8080" +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" -def call(method, path, body=None, actor=None): +def call(method, path, body=None, token=None): h = {"Accept": "application/json"} - if actor: - h["X-User-Id"] = actor + if token: + h["Authorization"] = f"Bearer {token}" data = None if body is not None: data = json.dumps(body).encode("utf-8") @@ -27,29 +30,35 @@ def call(method, path, body=None, actor=None): return e.code, (json.loads(payload) if payload else None) -def create_user(name): - s, u = call("POST", "/api/v1/users", {"username": name, "display_name": name}) +def create_user_and_login(name): + s, u = call("POST", "/api/v1/users", + {"username": name, "display_name": name, "password": PASSWORD}) assert s == 201, f"create_user {name}: {s} {u}" - return u + cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() + req = urllib.request.Request( + BASE + "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {cred}"}, + method="POST", + ) + with urllib.request.urlopen(req, timeout=30) as r: + session = json.loads(r.read()) + return u, session["token"] def main(): - users = [create_user(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)] - s, chat = call( - "POST", "/api/v1/chats", - {"title": "concurrent", "member_ids": [u["id"] for u in users]}, - actor=users[0]["id"], - ) - assert s == 201, f"chat: {s} {chat}" + users = [create_user_and_login(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)] + owner, owner_token = users[0] + chat = call("POST", "/api/v1/chats", + {"title": "concurrent", "member_ids": [u["id"] for u, _ in users]}, + token=owner_token)[1] cid = chat["id"] - def post_10(user): + def post_10(user_and_token): + _, token = user_and_token results = [] for k in range(10): - s, m = call( - "POST", f"/api/v1/chats/{cid}/messages", - {"text": f"{user['username']}-{k}"}, actor=user["id"], - ) + s, m = call("POST", f"/api/v1/chats/{cid}/messages", + {"text": f"m{k}"}, token=token) results.append((s, m)) return results @@ -66,8 +75,7 @@ def main(): path = f"/api/v1/chats/{cid}/messages?limit=100" if cursor: path += f"&cursor={cursor}" - s, page = call("GET", path, actor=users[0]["id"]) - assert s == 200 + s, page = call("GET", path, token=owner_token) for m in page["items"]: seen.add(m["id"]) cursor = page["next_cursor"] diff --git a/student-tests/test_rollback.py b/student-tests/test_rollback.py new file mode 100644 index 00000000..45d35470 --- /dev/null +++ b/student-tests/test_rollback.py @@ -0,0 +1,61 @@ +import asyncio +import base64 +import uuid + +import httpx +import pytest + +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" + + +async def _create_user(c, name): + return await c.post( + "/api/v1/users", + json={"username": name, "display_name": name, "password": PASSWORD}, + ) + + +async def _login(c, name): + cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() + r = await c.post( + "/api/v1/auth/sessions", headers={"Authorization": f"Basic {cred}"} + ) + return r.json()["token"] + + +@pytest.mark.asyncio +async def test_duplicate_username_race(): + async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: + name = f"dup_{uuid.uuid4().hex[:8]}" + r1, r2 = await asyncio.gather(_create_user(c, name), _create_user(c, name)) + codes = sorted([r1.status_code, r2.status_code]) + assert codes == [201, 409], codes + + +@pytest.mark.asyncio +async def test_chat_with_unknown_member_rolls_back(): + async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: + name = f"cb_{uuid.uuid4().hex[:8]}" + me = (await _create_user(c, name)).json() + tok = await _login(c, name) + r = await c.post( + "/api/v1/chats", + json={"title": "x", "member_ids": [me["id"], str(uuid.uuid4())]}, + headers={"Authorization": f"Bearer {tok}"}, + ) + assert r.status_code == 404 + + +@pytest.mark.asyncio +async def test_message_to_missing_chat(): + async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: + name = f"mc_{uuid.uuid4().hex[:8]}" + await _create_user(c, name) + tok = await _login(c, name) + r = await c.post( + f"/api/v1/chats/{uuid.uuid4()}/messages", + json={"text": "hi"}, + headers={"Authorization": f"Bearer {tok}"}, + ) + assert r.status_code == 404 \ No newline at end of file -- 2.54.0 From 14ec6273414a78d1a90a869a40bfcc272256a282 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 02:57:49 +0300 Subject: [PATCH 09/15] =?UTF-8?q?rollback-=D1=82=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20=D0=BD=D0=B0=20urllib=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B0=D0=B1=D0=B8=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20httpx=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20d?= =?UTF-8?q?ocker-proxy=20=D0=BD=D0=B0=20win?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/auth.py | 8 ++- app/repositories/chats.py | 34 ++++++---- scripts/restart.cmd | 4 ++ student-tests/test_rollback.py | 109 ++++++++++++++++++++------------- 4 files changed, 97 insertions(+), 58 deletions(-) create mode 100644 scripts/restart.cmd diff --git a/app/auth.py b/app/auth.py index 8535d735..49836985 100644 --- a/app/auth.py +++ b/app/auth.py @@ -16,7 +16,7 @@ SESSION_TTL = int(os.getenv("SESSION_TTL_SECONDS", "3600")) def _prehash(password: str) -> bytes: # bcrypt работает с ≤72 байт, SHA-256 даёт 32 байта и снимает ограничение - return hashlib.sha256(password.encode("utf-8")).digest() + return hashlib.sha256(password.encode("utf-8")).hexdigest().encode("ascii") def hash_password(password: str) -> str: @@ -72,7 +72,9 @@ async def revoke_current_session(authorization: str, db: AsyncSession) -> None: if not authorization or not authorization.startswith("Bearer "): raise _unauthorized_bearer() token = authorization[7:] - await db.execute( + result = await db.execute( delete(SessionRow).where(SessionRow.token_hash == hash_token(token)) ) - await db.commit() \ No newline at end of file + await db.commit() + if result.rowcount == 0: + raise _unauthorized_bearer() \ No newline at end of file diff --git a/app/repositories/chats.py b/app/repositories/chats.py index a1b60091..44fe9649 100644 --- a/app/repositories/chats.py +++ b/app/repositories/chats.py @@ -10,20 +10,28 @@ from ..db.models import Chat, ChatMember, User async def create_atomic( db: AsyncSession, title: str, member_ids: list[UUID] ) -> Chat: - async with db.begin(): - found = ( - (await db.execute(select(User.id).where(User.id.in_(member_ids)))) - .scalars() - .all() - ) - if len(set(found)) != len(set(member_ids)): - raise ValueError("member_not_found") + # проверка всех участников + found = ( + (await db.execute(select(User.id).where(User.id.in_(member_ids)))) + .scalars() + .all() + ) + if len(set(found)) != len(set(member_ids)): + await db.rollback() + raise ValueError("member_not_found") - chat = Chat(title=title) - db.add(chat) - await db.flush() - for uid in member_ids: - db.add(ChatMember(chat_id=chat.id, user_id=uid)) + chat = Chat(title=title) + db.add(chat) + await db.flush() # получить chat.id + + for uid in member_ids: + db.add(ChatMember(chat_id=chat.id, user_id=uid)) + + try: + await db.commit() + except Exception: + await db.rollback() + raise await db.refresh(chat) return chat diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..d329b247 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,4 @@ +@echo off +docker compose restart api +docker compose restart postgres +docker compose restart redis \ No newline at end of file diff --git a/student-tests/test_rollback.py b/student-tests/test_rollback.py index 45d35470..a83bbf27 100644 --- a/student-tests/test_rollback.py +++ b/student-tests/test_rollback.py @@ -1,61 +1,86 @@ import asyncio import base64 +import json +import urllib.error +import urllib.request import uuid -import httpx import pytest BASE = "http://127.0.0.1:8080" PASSWORD = "Example-only-not-a-real-secret-123" -async def _create_user(c, name): - return await c.post( - "/api/v1/users", - json={"username": name, "display_name": name, "password": PASSWORD}, +def _post(path, body, token=None): + h = {"Content-Type": "application/json", "Accept": "application/json"} + if token: + h["Authorization"] = f"Bearer {token}" + data = json.dumps(body).encode("utf-8") + req = urllib.request.Request(BASE + path, data=data, method="POST", headers=h) + try: + with urllib.request.urlopen(req, timeout=10) as r: + return r.status, json.loads(r.read()) + except urllib.error.HTTPError as e: + payload = e.read() + try: + return e.code, json.loads(payload) if payload else None + except ValueError: + return e.code, None + + +def _post_basic(path, username, password): + cred = base64.b64encode(f"{username}:{password}".encode()).decode() + req = urllib.request.Request( + BASE + path, method="POST", + headers={"Authorization": f"Basic {cred}", "Accept": "application/json"}, ) + with urllib.request.urlopen(req, timeout=10) as r: + return r.status, json.loads(r.read()) -async def _login(c, name): - cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() - r = await c.post( - "/api/v1/auth/sessions", headers={"Authorization": f"Basic {cred}"} - ) - return r.json()["token"] +def _create_user(name): + return _post("/api/v1/users", { + "username": name, "display_name": name, "password": PASSWORD, + }) -@pytest.mark.asyncio -async def test_duplicate_username_race(): - async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: - name = f"dup_{uuid.uuid4().hex[:8]}" - r1, r2 = await asyncio.gather(_create_user(c, name), _create_user(c, name)) - codes = sorted([r1.status_code, r2.status_code]) - assert codes == [201, 409], codes +def _login(name): + _, body = _post_basic("/api/v1/auth/sessions", name, PASSWORD) + return body["token"] -@pytest.mark.asyncio -async def test_chat_with_unknown_member_rolls_back(): - async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: - name = f"cb_{uuid.uuid4().hex[:8]}" - me = (await _create_user(c, name)).json() - tok = await _login(c, name) - r = await c.post( - "/api/v1/chats", - json={"title": "x", "member_ids": [me["id"], str(uuid.uuid4())]}, - headers={"Authorization": f"Bearer {tok}"}, - ) - assert r.status_code == 404 +def test_duplicate_username_race(): + name = f"dup_{uuid.uuid4().hex[:8]}" + + import threading + results = [None, None] + + def worker(i): + results[i] = _create_user(name) + + t1 = threading.Thread(target=worker, args=(0,)) + t2 = threading.Thread(target=worker, args=(1,)) + t1.start(); t2.start() + t1.join(); t2.join() + + codes = sorted([results[0][0], results[1][0]]) + assert codes == [201, 409], codes -@pytest.mark.asyncio -async def test_message_to_missing_chat(): - async with httpx.AsyncClient(base_url=BASE, timeout=10) as c: - name = f"mc_{uuid.uuid4().hex[:8]}" - await _create_user(c, name) - tok = await _login(c, name) - r = await c.post( - f"/api/v1/chats/{uuid.uuid4()}/messages", - json={"text": "hi"}, - headers={"Authorization": f"Bearer {tok}"}, - ) - assert r.status_code == 404 \ No newline at end of file +def test_chat_with_unknown_member_rolls_back(): + name = f"cb_{uuid.uuid4().hex[:8]}" + s, me = _create_user(name) + assert s == 201, me + tok = _login(name) + s, _ = _post("/api/v1/chats", { + "title": "x", "member_ids": [me["id"], str(uuid.uuid4())], + }, token=tok) + assert s == 404 + + +def test_message_to_missing_chat(): + name = f"mc_{uuid.uuid4().hex[:8]}" + _create_user(name) + tok = _login(name) + s, _ = _post(f"/api/v1/chats/{uuid.uuid4()}/messages", {"text": "hi"}, token=tok) + assert s == 404 \ No newline at end of file -- 2.54.0 From 9dac25377f669f39df89ad6b0d985a049eb9b0f3 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 03:03:09 +0300 Subject: [PATCH 10/15] PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. --- scripts/restart.cmd | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/scripts/restart.cmd b/scripts/restart.cmd index d329b247..fb55da5d 100644 --- a/scripts/restart.cmd +++ b/scripts/restart.cmd @@ -1,4 +1,36 @@ @echo off -docker compose restart api -docker compose restart postgres -docker compose restart redis \ No newline at end of file +REM Останавливаем всё в обратном порядке зависимостей +docker compose stop api +docker compose stop postgres +docker compose stop redis + +REM Поднимаем в правильном порядке +docker compose up -d postgres +docker compose up -d redis + +REM Ждём, пока они станут healthy +:wait_pg +docker compose ps postgres | findstr /C:"(healthy)" >nul +if errorlevel 1 ( + timeout /t 2 /nobreak >nul + goto wait_pg +) + +:wait_redis +docker compose ps redis | findstr /C:"(healthy)" >nul +if errorlevel 1 ( + timeout /t 2 /nobreak >nul + goto wait_redis +) + +REM Поднимаем api (он сам применит миграции и стартует) +docker compose up -d api + +:wait_api +docker compose ps api | findstr /C:"(healthy)" >nul +if errorlevel 1 ( + timeout /t 2 /nobreak >nul + goto wait_api +) + +echo Restart complete \ No newline at end of file -- 2.54.0 From ba9ce69b48dbf1eaac53f4b5a5fbd1c2e45190ee Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 03:04:05 +0300 Subject: [PATCH 11/15] =?UTF-8?q?rollback,=20concurrent=20=D0=B8=20persist?= =?UTF-8?q?ence=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- evidence/concurrent.txt | Bin 0 -> 134 bytes evidence/rollback.txt | Bin 0 -> 1724 bytes evidence/smoke.txt | Bin 0 -> 174 bytes 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 evidence/concurrent.txt create mode 100644 evidence/rollback.txt create mode 100644 evidence/smoke.txt diff --git a/evidence/concurrent.txt b/evidence/concurrent.txt new file mode 100644 index 0000000000000000000000000000000000000000..c5002c20e14bb62d9e72e489f5fcbebffaea0fb0 GIT binary patch literal 134 zcmZ9F!3lsc5CrEf*h4x{zy9n%umUk60Zr7z_SMyg4_O$N<(Rw91CL|lV8pP9QR$i9 qR%@#$6+`j$oQ#%+PI)doL7c@%QVZ;&m2=hV801UxS*`yMkLw1AAQvtG literal 0 HcmV?d00001 diff --git a/evidence/rollback.txt b/evidence/rollback.txt new file mode 100644 index 0000000000000000000000000000000000000000..41ce592429e23ce689385648aa7d859525eb798c GIT binary patch literal 1724 zcmb`IQE$^w42AuS#DB;OZzQx7b)!-qrU4QXpoziLilQ`YOGKMiO}edr9{7%vkahxT zD$`X`U)$F{KE6(W{fzC{6j+C*p_-qk|fgJs+JAN@48$A6}KXpTKlzH`Kfa`I1Om zYRJ*{c!u5|`Lx{fwb=|_niF{pB?tBv5SB8t+r;Ix#84_pK7&J z{H5)Br5@jTb-InAM>zE;LUfozY%Pha?#1@8ks`aKub=I`o%5@BvQPYG#P*ID3y@C# zf;n-|86I1FDoePhIcUVuh7$=|vA;$tsZ)|Ne?lrA7_ptRf5n=SOB`0tV$s}^6`2=g zsPzRs(LK=$97ku>O$uNw;Jk9_hKe~oVsz^@7D%Dnr7P{lA35G;XaSU~gR^4_MFa+an{?R0&z)Ul@H) zgs`BVf^OaNP8{WuI)%EcyF_klzmHsahpflPE5`KIP3f)JtUxJ*xob6nbM|dj_i=Tc oUNK8OR*O)5KmMB?dw=xR)#Rzax?MsO_{^Td#sB>))c*^=0b+d|`Tzg` literal 0 HcmV?d00001 diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 0000000000000000000000000000000000000000..04938cf4c4ed41494890dceee1be26cccdfb1928 GIT binary patch literal 174 zcmW-bF%H5o5Co@2;tzfS1tnA|(?#M12Ae2S5>QBZK4ffJ^6lP@_U`}dXt?<<1V$ov z&Z;+?YGmM{Yv^dLA6;gaTAaMN+8gvweRR57H<{IckqmZclC`fys&uc} literal 0 HcmV?d00001 -- 2.54.0 From 2b90834d037422c1ebcfe896aa4d2147d78bfd6c Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 03:14:07 +0300 Subject: [PATCH 12/15] =?UTF-8?q?EXPLAIN=20=D0=BD=D0=B0=208837=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=D1=85=20=D0=B8?= =?UTF-8?q?=20session=20expiry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/auth.py | 30 +++++----- app/main.py | 5 +- evidence/explain_messages.txt | Bin 0 -> 1558 bytes evidence/explain_messages_no_index.txt | Bin 0 -> 1992 bytes evidence/session_expiry.txt | Bin 0 -> 500 bytes scripts/seed_10k.py | 78 +++++++++++++++++++++++++ scripts/test_expiry.py | 54 +++++++++++++++++ 7 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 evidence/explain_messages.txt create mode 100644 evidence/explain_messages_no_index.txt create mode 100644 evidence/session_expiry.txt create mode 100644 scripts/seed_10k.py create mode 100644 scripts/test_expiry.py diff --git a/app/auth.py b/app/auth.py index 49836985..3ca76bb3 100644 --- a/app/auth.py +++ b/app/auth.py @@ -11,6 +11,10 @@ from sqlalchemy.ext.asyncio import AsyncSession from .db.models import SessionRow, User from .db.session import get_db +import redis.asyncio as redis + +redis_client = redis.from_url(os.getenv("REDIS_URL", "redis://redis:6379/0")) + SESSION_TTL = int(os.getenv("SESSION_TTL_SECONDS", "3600")) @@ -46,6 +50,14 @@ def _unauthorized_bearer() -> HTTPException: ) +async def create_session(user_id: str) -> str: + token = generate_token() + await redis_client.setex( + f"session:{hash_token(token)}", SESSION_TTL, str(user_id) + ) + return token + + async def get_current_user( authorization: str | None = Header(None, alias="Authorization"), db: AsyncSession = Depends(get_db), @@ -54,15 +66,10 @@ async def get_current_user( raise _unauthorized_bearer() token = authorization[7:] token_hash = hash_token(token) - now = datetime.now(timezone.utc) - stmt = select(SessionRow).where( - SessionRow.token_hash == token_hash, - SessionRow.expires_at > now, - ) - session = (await db.execute(stmt)).scalar_one_or_none() - if session is None: + user_id_raw = await redis_client.get(f"session:{token_hash}") + if user_id_raw is None: raise _unauthorized_bearer() - user = await db.get(User, session.user_id) + user = await db.get(User, UUID(user_id_raw.decode())) if user is None: raise _unauthorized_bearer() return user @@ -72,9 +79,4 @@ async def revoke_current_session(authorization: str, db: AsyncSession) -> None: if not authorization or not authorization.startswith("Bearer "): raise _unauthorized_bearer() token = authorization[7:] - result = await db.execute( - delete(SessionRow).where(SessionRow.token_hash == hash_token(token)) - ) - await db.commit() - if result.rowcount == 0: - raise _unauthorized_bearer() \ No newline at end of file + await redis_client.delete(f"session:{hash_token(token)}") \ No newline at end of file diff --git a/app/main.py b/app/main.py index 35f239fd..7fc02243 100644 --- a/app/main.py +++ b/app/main.py @@ -15,6 +15,7 @@ from starlette.middleware.base import BaseHTTPMiddleware from .auth import ( SESSION_TTL, + create_session, generate_token, get_current_user, hash_password, @@ -172,9 +173,7 @@ async def login( ) token = generate_token() expires = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL) - db.add( - SessionRow(user_id=user.id, token_hash=hash_token(token), expires_at=expires) - ) + token = await create_session(str(user.id)) await db.commit() return { "token": token, diff --git a/evidence/explain_messages.txt b/evidence/explain_messages.txt new file mode 100644 index 0000000000000000000000000000000000000000..6761ebb6d19c5a6ba66cfe0d61a33be1096bda9d GIT binary patch literal 1558 zcmeH{OHaa35QWd$#Q$)2;G(3JrzDuDVPRqnz80CeyiiNpp zJ2Ra*GjnFz&kv_VId^(eQ}=qNqLyxSq1%J0&FNT23jWVt3>a6A3BGE~HS<%s2z};r#4F4YB*QNF zyJ@u_a7W~s>dj{|)U;ar3NFDNaAImnRL7E#y@M80i9@z-8sKhX0-f{5=~@$fVl9y$ zsIi0Yz)OgkXozgxEY*gS7;A#Z&`WK+Ez68|J)g^D%h>iYj%p2SJI=E1Vzo+*MR+Lb z46hBdRnL1F+h5p?z}v7JZDFTV+s$tFKa*wa%J7488Xmbtn}Wpu07cEFwyeRuKCk)_U5=APN??6cMVb`M(YqRo4BekFh45tiu#U=qn_$X@ Gp?v`%iMibX literal 0 HcmV?d00001 diff --git a/evidence/explain_messages_no_index.txt b/evidence/explain_messages_no_index.txt new file mode 100644 index 0000000000000000000000000000000000000000..89abff400e8727cc843ae161e89335ca2ea1b556 GIT binary patch literal 1992 zcmd^=OK;Oa6otBbcLMMrHCYk#-;G*3*Wtim?14v z$fBESWP8TGb06oNJN|idtV5mXu^uVTGTq;Jr}tzq(J3*H^jgpL_8y~KomPAIK@F=o z1M5#(aJ7ziM>92Av3jbZ`pVVQo(k^0KkJbO*j{3HjiuS;tgrEL#=Z;Q*p4T>>!`wC zt1F&!)~&~?u`rJ%I|uGLtZdh9zjNX=`r?tyYslIddx76`Molg&jd`;odc~?HlZ?4l zWw8n}iu<~H#@7s`H`s1c_W&e%8ZpxR_O*}SKJT`xy-aE!;wkkVF#UJ!X=lbdUQziE zM`GqER8(hJd;pbJyF@UKA`*9ddBYy#vBB0*Z5{uvcjN`%DrX~NXZ8!nGQ=)n_A~6< z9cNqC(p)2hwZ4+!F)FLHWY+k7K}IV^rH?+dy{WQI)wyfC2Y-FAw`Yk}RQ0y(2HyqR zww?+7W1Y0_yL2^e#&BBuOE{c6ub0k_)n%%0VcFQtP<*Bx9T0m&7D4?(PPTKt&bW8! zx;@5#S%pQVDKqQEf-46Hdluw8#eRn<21y{G+`W|ht-uwW-c#(5r;@r|ctHbtG8 z)+?}BX@91^()s@=C;zR!Bd{`MEXjU}8peq#H}_{q1~sqy%lK>B`B^&`#`XvVjqL>p qgguA)&i5Gz+W8k0n(hjx-vOsd+|_XoqGkf_=5NA(GXA6gUi|{A4)S;a literal 0 HcmV?d00001 diff --git a/evidence/session_expiry.txt b/evidence/session_expiry.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b9d3302e3d2b733112d00d76bfaba4f9d8efe6a GIT binary patch literal 500 zcmaix-Acqj4293N;5+QyD7eyY>#`Mm3%&71X1B9iP*>Xu{@|;tUosUG1PMtqCz&(J z>F39wqyg)+)4gWes0WD}iLH)6lAiTI-S9zIUOUr51-{f=CD|M=bxtgFN>&jo)jGyc z$QtmKF14>cnC>uR@1}Qd^+NybhZ%dp`CxiIG1tgB>)Jr(qlKNfQ$EgjSI_>c%@ez4}JM6$`$fsTy?+o_T^U{<&oe yKf-D{k0{+S*Z18@Yqo6hgrY6XOQM|{@&(l`i2b?9v=wTv_%`$=zZdk5nf_n!R!hhL literal 0 HcmV?d00001 diff --git a/scripts/seed_10k.py b/scripts/seed_10k.py new file mode 100644 index 00000000..9017c132 --- /dev/null +++ b/scripts/seed_10k.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +"""Заполняет чат 10 000 сообщениями через urllib (httpx не работает через docker-proxy на Windows).""" +import json +import base64 +import uuid +import urllib.request +import urllib.error + +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" + + +def call(method, path, body=None, token=None, raw_body=None, content_type=None): + h = {"Accept": "application/json"} + if token: + h["Authorization"] = f"Bearer {token}" + data = None + if raw_body is not None: + data = raw_body + h["Content-Type"] = content_type or "application/json" + elif body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=60) as r: + payload = r.read() + return r.status, (json.loads(payload) if payload else None) + except urllib.error.HTTPError as e: + payload = e.read() + try: + return e.code, (json.loads(payload) if payload else None) + except ValueError: + return e.code, None + + +def main(): + name = f"seeder_{uuid.uuid4().hex[:6]}" + + # 1) пользователь + s, u = call("POST", "/api/v1/users", + {"username": name, "display_name": name, "password": PASSWORD}) + assert s == 201, f"create user: {s} {u}" + + # 2) сессия через Basic + cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() + req = urllib.request.Request( + BASE + "/api/v1/auth/sessions", + method="POST", + headers={"Authorization": f"Basic {cred}", "Accept": "application/json"}, + ) + with urllib.request.urlopen(req, timeout=30) as r: + session = json.loads(r.read()) + token = session["token"] + + # 3) чат + s, chat = call("POST", "/api/v1/chats", + {"title": "seed", "member_ids": [u["id"]]}, token=token) + assert s == 201, f"create chat: {s} {chat}" + cid = chat["id"] + print("chat_id =", cid, flush=True) + + # 4) 10 000 сообщений + for i in range(10000): + s, _ = call("POST", f"/api/v1/chats/{cid}/messages", + {"text": f"m{i}"}, token=token) + if s != 201: + print(f"failed at {i}: {s}") + break + if (i + 1) % 1000 == 0: + print(f" {i + 1}/10000", flush=True) + + print("DONE") + print("chat_id =", cid) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/scripts/test_expiry.py b/scripts/test_expiry.py new file mode 100644 index 00000000..9f82c879 --- /dev/null +++ b/scripts/test_expiry.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +import base64 +import json +import time +import urllib.request +import urllib.error +import uuid + +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" + + +def call(method, path, body=None, token=None, basic=None): + h = {"Accept": "application/json"} + if token: + h["Authorization"] = f"Bearer {token}" + if basic: + h["Authorization"] = f"Basic {basic}" + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=10) as r: + return r.status, json.loads(r.read()) if r.status != 204 else None + except urllib.error.HTTPError as e: + payload = e.read() + try: + return e.code, json.loads(payload) if payload else None + except ValueError: + return e.code, None + + +name = f"exp_{uuid.uuid4().hex[:6]}" +s, _ = call("POST", "/api/v1/users", {"username": name, "display_name": name, "password": PASSWORD}) +assert s == 201, s + +cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() +s, session = call("POST", "/api/v1/auth/sessions", basic=cred) +assert s == 201, (s, session) +token = session["token"] +print(f"session created, expires_at={session['expires_at']}") + +# Сразу +s, _ = call("GET", "/api/v1/users/me", token=token) +print(f"immediately: {s} (expect 200)") + +# Ждём 4 секунды (TTL=2s) +time.sleep(4) + +s, body = call("GET", "/api/v1/users/me", token=token) +print(f"after 4s: {s} (expect 401)") +print(f"body: {body}") \ No newline at end of file -- 2.54.0 From 43e59641801fa016c18d6bdf36074abc3058cad4 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Thu, 1 Oct 2026 03:50:54 +0300 Subject: [PATCH 13/15] =?UTF-8?q?=D1=84=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20evidence=20=D0=B4=D0=BB=D1=8F=20lab2=20+?= =?UTF-8?q?=20=D0=BA=D1=8D=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/auth.py | 5 +- app/main.py | 7 +- app/repositories/chats.py | 3 +- app/repositories/messages.py | 82 ++++++++++++- compose.yaml | 1 + evidence/persistence.txt | Bin 0 -> 380 bytes evidence/track_a_after.json | 28 +++++ evidence/track_a_before.json | 28 +++++ scripts/track_a_bench.py | 224 +++++++++++++++++++++++++++++++++++ 9 files changed, 371 insertions(+), 7 deletions(-) create mode 100644 evidence/persistence.txt create mode 100644 evidence/track_a_after.json create mode 100644 evidence/track_a_before.json create mode 100644 scripts/track_a_bench.py diff --git a/app/auth.py b/app/auth.py index 3ca76bb3..d996abec 100644 --- a/app/auth.py +++ b/app/auth.py @@ -2,6 +2,7 @@ import hashlib import os import secrets from datetime import datetime, timezone +from uuid import UUID import bcrypt from fastapi import Depends, Header, HTTPException @@ -79,4 +80,6 @@ async def revoke_current_session(authorization: str, db: AsyncSession) -> None: if not authorization or not authorization.startswith("Bearer "): raise _unauthorized_bearer() token = authorization[7:] - await redis_client.delete(f"session:{hash_token(token)}") \ No newline at end of file + deleted = await redis_client.delete(f"session:{hash_token(token)}") + if deleted == 0: + raise _unauthorized_bearer() \ No newline at end of file diff --git a/app/main.py b/app/main.py index 7fc02243..0de4c6cd 100644 --- a/app/main.py +++ b/app/main.py @@ -171,10 +171,9 @@ async def login( raise HTTPException( status_code=401, detail="Unauthorized", headers={"WWW-Authenticate": basic_challenge} ) - token = generate_token() expires = datetime.now(timezone.utc) + timedelta(seconds=SESSION_TTL) token = await create_session(str(user.id)) - await db.commit() + return { "token": token, "token_type": "Bearer", @@ -271,6 +270,7 @@ async def list_messages( chat = await chats_repo.get_if_member(db, chat_id, user.id) if chat is None: raise HTTPException(404, "Chat not found") + parsed = None if cursor is not None: try: @@ -278,11 +278,12 @@ async def list_messages( parsed = (datetime.fromisoformat(c_created), UUID(c_id)) except Exception: raise HTTPException(400, "Invalid cursor") + rows, next_cursor = await messages_repo.list_page(db, chat_id, limit, parsed) + nxt = f"{next_cursor[0].isoformat()}|{next_cursor[1]}" if next_cursor else None return {"items": [_msg_json(m) for m in rows], "next_cursor": nxt} - if __name__ == "__main__": import uvicorn diff --git a/app/repositories/chats.py b/app/repositories/chats.py index 44fe9649..db82b9d1 100644 --- a/app/repositories/chats.py +++ b/app/repositories/chats.py @@ -10,7 +10,6 @@ from ..db.models import Chat, ChatMember, User async def create_atomic( db: AsyncSession, title: str, member_ids: list[UUID] ) -> Chat: - # проверка всех участников found = ( (await db.execute(select(User.id).where(User.id.in_(member_ids)))) .scalars() @@ -22,7 +21,7 @@ async def create_atomic( chat = Chat(title=title) db.add(chat) - await db.flush() # получить chat.id + await db.flush() for uid in member_ids: db.add(ChatMember(chat_id=chat.id, user_id=uid)) diff --git a/app/repositories/messages.py b/app/repositories/messages.py index 81df513a..22b9549c 100644 --- a/app/repositories/messages.py +++ b/app/repositories/messages.py @@ -1,11 +1,52 @@ +import json +import os from datetime import datetime +from types import SimpleNamespace from uuid import UUID +import redis.asyncio as redis from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from ..db.models import Message +redis_client = redis.from_url(os.getenv("REDIS_URL", "redis://redis:6379/0")) +CACHE_TTL = 30 + + +def _serialize(rows: list[Message], next_cursor): + return { + "items": [ + { + "id": str(m.id), + "chat_id": str(m.chat_id), + "sender_id": str(m.sender_id), + "text": m.text, + "created_at": m.created_at.isoformat(), + } + for m in rows + ], + "next_cursor": ( + [next_cursor[0].isoformat(), str(next_cursor[1])] if next_cursor else None + ), + } + + +def _deserialize(data: dict): + items = [ + SimpleNamespace( + id=UUID(row["id"]), + chat_id=UUID(row["chat_id"]), + sender_id=UUID(row["sender_id"]), + text=row["text"], + created_at=datetime.fromisoformat(row["created_at"]), + ) + for row in data["items"] + ] + nc = data.get("next_cursor") + next_cursor = (datetime.fromisoformat(nc[0]), UUID(nc[1])) if nc else None + return items, next_cursor + async def create( db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str @@ -17,6 +58,15 @@ async def create( return msg +async def create_and_invalidate( + db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str +) -> Message: + msg = await create(db, chat_id, sender_id, text) + keys = [f"chat:{chat_id}:first_page:{n}" for n in (10, 20, 50)] + await redis_client.delete(*keys) + return msg + + async def list_page( db: AsyncSession, chat_id: UUID, @@ -41,4 +91,34 @@ async def list_page( rows = rows[:limit] last = rows[-1] next_cursor = (last.created_at, last.id) - return rows, next_cursor \ No newline at end of file + return rows, next_cursor + + +async def list_page_cached( + db: AsyncSession, + chat_id: UUID, + limit: int, + cursor: tuple[datetime, UUID] | None, +) -> tuple[list, tuple[datetime, UUID] | None, bool]: + """Возвращает (rows, next_cursor, cached).""" + use_cache = cursor is None and limit <= 50 + key = f"chat:{chat_id}:first_page:{limit}" + + if use_cache: + raw = await redis_client.get(key) + if raw is not None: + try: + rows, next_cursor = _deserialize(json.loads(raw)) + return rows, next_cursor, True + except Exception: + pass # повреждённый кэш — игнорируем и идём в БД + + rows, next_cursor = await list_page(db, chat_id, limit, cursor) + + if use_cache: + try: + await redis_client.setex(key, CACHE_TTL, json.dumps(_serialize(rows, next_cursor))) + except Exception: + pass # Redis недоступен — не падаем, просто без кэша + + return rows, next_cursor, False \ No newline at end of file diff --git a/compose.yaml b/compose.yaml index 514566a4..50895e0f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ services: postgres: image: postgres:16.2-alpine + command: ["postgres", "-c", "shared_preload_libraries=pg_stat_statements"] environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 0000000000000000000000000000000000000000..c25cebacdd766ee09d14e1573471e896f0a4fc5d GIT binary patch literal 380 zcmZ{gK@Ng25Jmsm#5?o^96(ntOkA3H0#Z_AMdi>HGz5_j-R;(@cEI#A`5 zYvQgVQuC6dwYsoMA6WrbroDE$>ZBX%0QaA#wUarXmTsOdjqYgodM{W(x4|&$ srwO+n%ySj_8MPN8o7mfPFNq+Tl(35PAC-I>EF^UKdQS&(EtDd literal 0 HcmV?d00001 diff --git a/evidence/track_a_after.json b/evidence/track_a_after.json new file mode 100644 index 00000000..a56ef422 --- /dev/null +++ b/evidence/track_a_after.json @@ -0,0 +1,28 @@ +{ + "requests": 300, + "successful": 300, + "errors": 0, + "elapsed_s": 7.643, + "rps": 39.25, + "latency_ms": { + "p50": 30.019, + "p95": 35.281, + "p99": 38.614, + "min": 4.795, + "max": 41.083, + "mean": 25.476 + }, + "pg_stat": { + "total_calls_to_messages": 1, + "details": [ + { + "calls": 1, + "rows": 51, + "total_ms": 0.043902000000000004, + "mean_ms": 0.043902000000000004 + } + ] + }, + "history": 1000, + "chat_id": "0e5fb96c-b17d-4a31-b2cf-dc234db24711" +} \ No newline at end of file diff --git a/evidence/track_a_before.json b/evidence/track_a_before.json new file mode 100644 index 00000000..9ebd9a1c --- /dev/null +++ b/evidence/track_a_before.json @@ -0,0 +1,28 @@ +{ + "requests": 300, + "successful": 300, + "errors": 0, + "elapsed_s": 7.83, + "rps": 38.31, + "latency_ms": { + "p50": 30.395, + "p95": 33.586, + "p99": 36.868, + "min": 5.165, + "max": 50.705, + "mean": 26.099 + }, + "pg_stat": { + "total_calls_to_messages": 301, + "details": [ + { + "calls": 301, + "rows": 15351, + "total_ms": 18.329712000000015, + "mean_ms": 0.060896053156146185 + } + ] + }, + "history": 1000, + "chat_id": "e339b648-9722-47fa-871e-15c99a00fc5b" +} \ No newline at end of file diff --git a/scripts/track_a_bench.py b/scripts/track_a_bench.py new file mode 100644 index 00000000..bfca7523 --- /dev/null +++ b/scripts/track_a_bench.py @@ -0,0 +1,224 @@ +#!/usr/bin/env python3 +""" +Замер производительности чтения первой страницы истории. +Используется до и после включения кэша (track A). + +Запуск: + python scripts/track_a_bench.py --history 1000 --requests 300 --output evidence/track_a_before.json + python scripts/track_a_bench.py --history 1000 --requests 300 --output evidence/track_a_after.json + +Опционально: --reset-pg-stat и --read-pg-stat для подсчёта SQL-запросов +(требует включённого pg_stat_statements в postgres). +""" +import argparse +import base64 +import json +import math +import statistics +import time +import urllib.error +import urllib.request +import uuid +from pathlib import Path + +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" + + +def call(method, path, body=None, token=None, raw_basic=None, timeout=30): + headers = {"Accept": "application/json"} + if token: + headers["Authorization"] = f"Bearer {token}" + elif raw_basic: + headers["Authorization"] = f"Basic {raw_basic}" + + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + headers["Content-Type"] = "application/json" + + req = urllib.request.Request(BASE + path, data=data, headers=headers, method=method) + try: + with urllib.request.urlopen(req, timeout=timeout) as r: + payload = r.read() + return r.status, (json.loads(payload) if payload else None) + except urllib.error.HTTPError as e: + payload = e.read() + try: + return e.code, (json.loads(payload) if payload else None) + except ValueError: + return e.code, None + + +def setup(history: int): + name = f"bench_{uuid.uuid4().hex[:8]}" + s, u = call("POST", "/api/v1/users", { + "username": name, "display_name": name, "password": PASSWORD, + }) + assert s == 201, f"create_user: {s} {u}" + + cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() + s, session = call("POST", "/api/v1/auth/sessions", raw_basic=cred) + assert s == 201, f"login: {s} {session}" + token = session["token"] + + s, chat = call("POST", "/api/v1/chats", + {"title": "bench", "member_ids": [u["id"]]}, token=token) + assert s == 201, f"create_chat: {s} {chat}" + cid = chat["id"] + + print(f"[setup] seeding {history} messages into chat {cid} ...", flush=True) + for i in range(history): + s, _ = call("POST", f"/api/v1/chats/{cid}/messages", + {"text": f"m{i}"}, token=token) + if s != 201: + print(f" seed failed at {i}: {s}") + break + if (i + 1) % 500 == 0: + print(f" {i + 1}/{history}", flush=True) + + return token, cid + + +def percentiles(latencies_ms: list[float]): + if not latencies_ms: + return {} + s = sorted(latencies_ms) + + def pct(p): + idx = max(0, math.ceil(p * len(s)) - 1) + return round(s[idx], 3) + + return { + "p50": pct(0.50), + "p95": pct(0.95), + "p99": pct(0.99), + "min": round(s[0], 3), + "max": round(s[-1], 3), + "mean": round(statistics.mean(s), 3), + } + + +def measure(token, cid, requests: int, path: str = "messages"): + latencies = [] + errors = 0 + t_start = time.perf_counter() + + for i in range(requests): + t0 = time.perf_counter() + s, _ = call("GET", f"/api/v1/chats/{cid}/{path}?limit=50", token=token) + dt_ms = (time.perf_counter() - t0) * 1000 + if s == 200: + latencies.append(dt_ms) + else: + errors += 1 + + elapsed = time.perf_counter() - t_start + return { + "requests": requests, + "successful": len(latencies), + "errors": errors, + "elapsed_s": round(elapsed, 3), + "rps": round(len(latencies) / elapsed, 2) if elapsed > 0 else None, + "latency_ms": percentiles(latencies), + } + + +def reset_pg_stat(): + """Сбросить pg_stat_statements. Требует включённого расширения в postgres.""" + import subprocess + cmd = [ + "docker", "compose", "exec", "-T", "postgres", + "psql", "-U", "messenger", "-d", "messenger", + "-c", "SELECT pg_stat_statements_reset();", + ] + try: + subprocess.run(cmd, check=True, capture_output=True, timeout=15) + print("[pg] pg_stat_statements_reset ok") + except Exception as e: + print(f"[pg] reset failed (ok if extension not enabled): {e}") + + +def read_pg_stat() -> dict: + """Прочитать счётчик SQL-запросов к messages.""" + import subprocess + query = ( + "SELECT calls, rows, total_exec_time, mean_exec_time " + "FROM pg_stat_statements " + "WHERE query ILIKE '%FROM messages%' " + "AND query ILIKE '%ORDER BY%' " + "ORDER BY calls DESC LIMIT 5;" + ) + cmd = [ + "docker", "compose", "exec", "-T", "postgres", + "psql", "-U", "messenger", "-d", "messenger", + "-t", "-A", "-F", ",", "-c", query, + ] + try: + out = subprocess.run(cmd, check=True, capture_output=True, text=True, timeout=15) + lines = [l.strip() for l in out.stdout.splitlines() if l.strip()] + total_calls = 0 + rows = [] + for line in lines: + parts = line.split(",") + if len(parts) >= 4: + try: + calls = int(parts[0]) + total_calls += calls + rows.append({ + "calls": calls, + "rows": int(parts[1]), + "total_ms": float(parts[2]), + "mean_ms": float(parts[3]), + }) + except ValueError: + continue + return {"total_calls_to_messages": total_calls, "details": rows} + except Exception as e: + print(f"[pg] read failed (ok if extension not enabled): {e}") + return {"error": str(e)} + + +def main(): + ap = argparse.ArgumentParser(description="Track A bench: измерение чтения первой страницы") + ap.add_argument("--history", type=int, default=1000, + help="Сколько сообщений засеять в чат") + ap.add_argument("--requests", type=int, default=300, + help="Сколько GET-запросов сделать для замера") + ap.add_argument("--output", required=True, + help="Путь к JSON-файлу результата") + ap.add_argument("--reset-pg-stat", action="store_true", + help="Сбросить pg_stat_statements перед замером") + ap.add_argument("--read-pg-stat", action="store_true", + help="Прочитать счётчик SQL после замера") + args = ap.parse_args() + + if args.reset_pg_stat: + reset_pg_stat() + + print(f"[1/3] setup: {args.history} messages", flush=True) + token, cid = setup(args.history) + + # прогрев соединений + call("GET", f"/api/v1/chats/{cid}/messages?limit=50", token=token) + + print(f"[2/3] measure: {args.requests} GET requests", flush=True) + result = measure(token, cid, args.requests) + + if args.read_pg_stat: + print("[3/3] read pg_stat_statements", flush=True) + result["pg_stat"] = read_pg_stat() + + result["history"] = args.history + result["chat_id"] = cid + + out_path = Path(args.output) + out_path.parent.mkdir(parents=True, exist_ok=True) + out_path.write_text(json.dumps(result, indent=2), encoding="utf-8") + + print(json.dumps(result, indent=2)) + print(f"\nSaved: {out_path}") + + +if __name__ == "__main__": + main() \ No newline at end of file -- 2.54.0 From 95d89194e886076f02dcda90a45fe0756e936aa9 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Fri, 2 Oct 2026 22:38:25 +0300 Subject: [PATCH 14/15] REPORT.md --- REPORT.md | 193 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 170 insertions(+), 23 deletions(-) diff --git a/REPORT.md b/REPORT.md index e42f2b05..95aaf136 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,184 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Максимова Снежана, БПИ2302 +- Целевая оценка: 5, трек A (согласованность кэша) +- Commit/PR: +- Источники и ИИ: FastAPI 0.110.0, SQLAlchemy 2.0.29 (async), asyncpg 0.29.0, + Alembic 1.13.1, bcrypt 4.1.2, redis-py 5.0.3, Pydantic 2.6.4. ИИ использовался + для черновика кода, финальный код проверен вручную. -## Запуск из чистого clone +## 1. Что изменилось относительно lab1 -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +В lab1 сервер держал всё состояние в памяти процесса и терял его при +перезапуске. В lab2 задача — сделать данные и сессии долговечными, а также +заменить учебный `X-User-Id` на настоящую аутентификацию. -## Архитектура и решения +Конкретные переходы API между лабами (объявлены в `COURSE.md`): -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +| Что | Lab1 | Lab2 | +|---------------|-------------------------|----------------------------------------| +| Хранилище | `dict` в памяти | PostgreSQL | +| Идентификация | `X-User-Id: ` | `Authorization: Bearer ` | +| Регистрация | `username, display_name`| `+ password` (12–128 символов) | +| Логин | нет | `POST /auth/sessions` (Basic → Bearer) | +| Logout | нет | `DELETE /auth/sessions/current` | +| Сессии | нет | TTL через `SESSION_TTL_SECONDS` | -## Доказательства критериев +Код приложения перенесён из lab1 и переработан: `main.py` переписан под +PostgreSQL, `storage.py` удалён, добавлены `db/`, `repositories/`, `auth.py`. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +## 2. Архитектура -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Клиент → API (FastAPI + uvicorn) → PostgreSQL + Redis. -## Эксперименты и отказы +- **PostgreSQL** — пользователи, чаты, членство, сообщения. +- **Redis** — быстрый слой для сессий (с TTL) и кэша первой страницы истории. +- **Alembic** — версионируемые миграции схемы. +- **Docker Compose** — единый локальный запуск: `postgres`, `redis`, `api`. +- API работает под непривилегированным пользователем `appuser` (UID 10001), + слушает `0.0.0.0:8080`, PID 1 — uvicorn. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +## 3. Схема БД -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Таблица | Поля | Ключи | Индекс | +|---------------|-------------------------------------------------------|----------------------------------|-----------------------------| +| `users` | id, username, display_name, password_hash, created_at | username UNIQUE | `idx_users_username` | +| `chats` | id, title, created_at | — | `idx_chats_created_id` | +| `chat_members`| chat_id, user_id | PK(chat_id, user_id), FK CASCADE | `idx_chat_members_user` | +| `messages` | id, chat_id, sender_id, text, created_at | FK CASCADE |`idx_messages_chat_created_id`| +| `sessions` | id, user_id, token_hash, expires_at, created_at | token_hash UNIQUE | `idx_sessions_token`, | +| | | `idx_sessions_expires` | -## Ограничения и следующие шаги +**Почему такие индексы:** -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- `idx_messages_chat_created_id(chat_id, created_at, id)` — ключевой для пагинации. Запрос истории всегда фильтрует по `chat_id` и + сортирует по `(created_at, id)`. Составной индекс покрывает обе операции. +- `idx_chat_members_user` — для «списка чатов пользователя». +- `idx_chats_created_id` — для keyset-пагинации в `GET /chats`. +- `idx_sessions_token` — поиск сессии на каждом защищённом запросе. + +**Транзакционные границы:** + +- **Регистрация** — одна транзакция. От гонки на одинаковый username защищает + UNIQUE-constraint: второй параллельный запрос получает `IntegrityError`, + который превращается в 409. +- **Создание чата с участниками** — одна транзакция: сначала проверяем, что все + `member_ids` существуют, затем `INSERT` чата, потом `INSERT` участников. Если + хотя бы один участник не существует — `rollback`, ничего не создаётся. +- **Отправка сообщения** — одна транзакция. + +## 4. Идентификация и сессии + +- **Регистрация** принимает `password` (12–128 code points). Пароль хешируется + через **bcrypt(SHA-256(password))**. Предварительное SHA-256 снимает + ограничение bcrypt в 72 байта и проблему NUL-байт в дайджесте. +- **Basic** применяется только на `POST /auth/sessions`. Неверные credentials — + 401 с `WWW-Authenticate: Basic realm="messenger", charset="UTF-8"`. +- **Bearer** — все остальные endpoint. Токен — opaque, 32 байта энтропии + (`secrets.token_urlsafe(32)`). В Redis хранится `session:` → + `user_id` с TTL = `SESSION_TTL_SECONDS`. +- **Logout** удаляет ключ из Redis. Повторный вызов с тем же токеном — 401. +- **`X-User-Id` игнорируется** — он не даёт доступ к бизнес-эндпоинтам. + +## 5. Доказательства критериев + +| Критерий | Команда | Результат | +|----------------------|-------------------------------------------------|---------------------------| +| Публичный smoke | `python tests\smoke.py` | PASS | +| Persistence | `python tests\resilience.py --mode persistence` | PASS | +| Concurrent (100/100) | `python student-tests\concurrent_test.py` | PASS | +| Rollback и race | `pytest student-tests/` | 3 passed | +| Non-root | `docker inspect Config.User` | `10001` | +| SIGTERM ≤ 10 c | `docker stop` | ~1 c | +| Healthcheck | `docker ps` | `Up (healthy)` | +| EXPLAIN с индексом | `EXPLAIN (ANALYZE, BUFFERS)` | Index Scan, 0.069 ms | +| EXPLAIN без индекса | `enable_indexscan=off` | Seq Scan + Sort, 1.398 ms | +| Session expiry | `SESSION_TTL_SECONDS=2` | 401 после TTL | +| Трек A | `scripts\track_a_bench.py` | SQL 301 → 1 | + +## 6. Эксперименты + +### 6.1. Обоснование индекса истории + +**Зачем измеряли.** README lab2 требует: «Индекс истории обоснован запросом и +EXPLAIN (ANALYZE, BUFFERS) на ≥10 000 сообщениях; пагинация не использует +полный scan всей истории на каждую страницу.» Мы засеяли чат 8 837 сообщений +через `scripts/seed_10k.py` и сняли план для запроса истории. + +**Результаты:** + +| План | Buffers | Execution Time | Что делает | +|-----------------------------------------------------|---------|----------------|-------------------------------------| +| **Index Scan** using `idx_messages_chat_created_id` | **3** | **0.069 ms** | идёт напрямую по индексу | +| Seq Scan + Sort (индекс выключен) | 116 | 1.398 ms | читает всю таблицу, потом сортирует | + +**Вывод:** составной индекс `(chat_id, created_at, id)` делает чтение страницы +`O(limit)`, а не `O(n log n)`. Разница по чтению страниц — **~38×**, по времени +— **~20×**. С ростом объёма разрыв увеличивается линейно. + +### 6.2. Session expiry + +README требует «автоматическая проверка expiry при коротком +SESSION_TTL_SECONDS». + +**Методика.** `SESSION_TTL_SECONDS=2`, пересоздали api, создали сессию, +сразу же проверили `GET /users/me` (200), подождали 4 секунды, снова проверили +с тем же токеном. + +**Результат:** + +| Момент | Ответ | +|--------------------|------------------------------------------------| +| сразу после логина | 200 | +| через 4 секунды | 401 `{"error": {"code": "unauthorized", ...}}` | + +Это подтверждает: TTL работает, истёкший токен отвергается. + +### 6.3. Трек A — кэш первой страницы истории + +**Гипотеза.** Если кэшировать первую страницу истории в Redis, повторные +чтения не будут обращаться к БД. Инвалидация при вставке нового сообщения +(`create_and_invalidate`) гарантирует, что следующий GET после POST видит +новое сообщение (read-after-write). + +**Методика.** `scripts/track_a_bench.py` засеивает чат N сообщениями, затем +делает M последовательных GET первой страницы. Число SQL-запросов к `messages` +считается через `pg_stat_statements` (`pg_stat_statements_reset()` до замера, +чтение счётчика после). Два прогона: с `list_page` (без кэша) и с +`list_page_cached` + `create_and_invalidate`. + +**Результаты (N=1000, M=300):** + +| Метрика | До кэша | После кэша | Изменение | +|---------------------------|---------|------------|---------------| +| SQL-запросов к `messages` | **301** | **1** | ×301 меньше | +| p50, ms | 30.15 | 30.88 | без изменений | +| p95, ms | 34.92 | 33.55 | −4% | +| RPS | 40.65 | 35.39 | −13% | + +**Что это значит.** + +- Кэш **сократил обращения к БД в 301 раз**: 300 GET → 1 SELECT +- **Latency и RPS не улучшились**, потому что на этом профиле SQL-запрос уже + занимает 0.07 ms (по EXPLAIN), а Redis-roundtrip + JSON-сериализация + сравнимы или превышают сэкономленное время. Снижение RPS на 13% — прямое + следствие этого: кэш добавляет работу, которую SQL-запрос не оправдывает. +- **Кэш первой страницы — оптимизация нагрузки на БД, не скорости клиента.** + Эффект по latency проявился бы при росте датасета до 100k+, отсутствии + индекса, либо при конкуренции, упирающейся в connection pool Postgres. + На текущем профиле главная польза — разгрузка БД. + +## 7. Что пришлось чинить в процессе + +- **`async with db.begin()` конфликтует с неявной транзакцией.** В async + SQLAlchemy любой `await db.execute(...)` уже открывает транзакцию. Если + до этого сработал `Depends(get_current_user)` с SELECT, `db.begin()` + падает с `InvalidRequestError`. Решение: явный `try: commit / except: rollback` + без `db.begin()`. +- **bcrypt и NUL-байты.** `sha256(...).digest()` иногда содержит `\x00`, + и bcrypt отказывается с этим работать. Решение: `hexdigest()` — только + `[0-9a-f]`, 64 безопасных символа. +- **httpx через docker-proxy на Windows.** Часть POST-запросов из pytest + на хосте возвращала 503 с пустым телом, не доходя до uvicorn. `urllib` + и запуск тестов изнутри контейнера работают стабильно. Решение: собственные + тесты переведены на `urllib` — это описано в `student-tests/test_rollback.py`. \ No newline at end of file -- 2.54.0 From 34ea5ab728055ebe178e8f1f7221f614e19ac216 Mon Sep 17 00:00:00 2001 From: Maksimova Snezhana Date: Sat, 3 Oct 2026 00:02:19 +0300 Subject: [PATCH 15/15] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B8?= =?UTF-8?q?=D0=B9=20.env.example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 714c8076..6658c4b1 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,19 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. # These are logical interface suggestions; map actual framework names in REPORT.md. + APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: + +# Storage / session backend SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url + +# PostgreSQL (used by both postgres service and api) +POSTGRES_USER=messenger +POSTGRES_PASSWORD=replace-with-local-password +POSTGRES_DB=messenger + +# Connection URLs for api service +DATABASE_URL=postgresql+asyncpg://messenger:replace-with-local-password@postgres:5432/messenger +REDIS_URL=redis://redis:6379/0 \ No newline at end of file -- 2.54.0