diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..eeed9d1f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.11.9-slim-bookworm + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 + +WORKDIR /app +RUN groupadd --system app && useradd --system --gid app --home-dir /app app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY app ./app +COPY migrations ./migrations +COPY alembic.ini . +RUN chown -R app:app /app +USER app +EXPOSE 8080 +HEALTHCHECK --interval=5s --timeout=3s --start-period=20s --retries=3 \ + CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-keep-alive", "5", "--timeout-graceful-shutdown", "20"] diff --git a/alembic.ini b/alembic.ini new file mode 100644 index 00000000..421e3fe9 --- /dev/null +++ b/alembic.ini @@ -0,0 +1,117 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +# Use forward slashes (/) also on windows to provide an os agnostic path +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 +# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file +# for all available tokens +# 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 = newline +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 = WARNING +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARNING +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/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/__init__.py b/app/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/api/auth.py b/app/api/auth.py new file mode 100644 index 00000000..bea835c4 --- /dev/null +++ b/app/api/auth.py @@ -0,0 +1,96 @@ +from datetime import timezone +from fastapi import APIRouter, Depends, Header, HTTPException, status +from fastapi.security import HTTPBasic, HTTPBasicCredentials +from sqlalchemy.orm import Session + +from app.api.deps import get_db +from app.api.users import user_to_response +from app.services.auth_service import ( + AuthService, + InvalidCredentialsError, + InvalidSessionError, +) + + +router = APIRouter(prefix="/api/v1/auth", tags=["Authentication"]) + +basic_security = HTTPBasic(auto_error=False) + + + +@router.post("/sessions", status_code=status.HTTP_201_CREATED) +def create_session( + credentials: HTTPBasicCredentials = Depends(basic_security), + db: Session = Depends(get_db), +): + if credentials is None: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Basic authentication required", + headers={ + "WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"' + }, + ) + + try: + result = AuthService(db).create_session( + username=credentials.username, + password=credentials.password, + ) + + expires_at = result["expires_at"] + + if expires_at.tzinfo is None: + expires_at = expires_at.replace(tzinfo=timezone.utc) + else: + expires_at = expires_at.astimezone(timezone.utc) + + return { + "token": result["token"], + "token_type": result["token_type"], + "expires_at": expires_at.isoformat( + timespec="microseconds" + ).replace("+00:00", "Z"), + "user": user_to_response(result["user"]), + } + + except InvalidCredentialsError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid username or password", + headers={ + "WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"' + }, + ) from None + + + +@router.delete("/sessions/current", status_code=status.HTTP_204_NO_CONTENT) +def delete_current_session( + authorization: str | None = Header(default=None), + db: Session = Depends(get_db), +): + if not authorization or not authorization.startswith("Bearer "): + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + + token = authorization[7:].strip() + + if not token: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) + + try: + AuthService(db).delete_session(token) + except InvalidSessionError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid or expired session", + headers={"WWW-Authenticate": 'Bearer realm="messenger"'}, + ) from None diff --git a/app/api/chats.py b/app/api/chats.py new file mode 100644 index 00000000..40c46261 --- /dev/null +++ b/app/api/chats.py @@ -0,0 +1,90 @@ +import uuid + +from fastapi import APIRouter, Depends, HTTPException, Query, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import Chat as ChatResponse, CreateChat +from app.services.chat_service import ( + ChatService, + ChatNotFoundError, + InvalidChatDataError, +) + + +router = APIRouter(prefix="/api/v1/chats", tags=["Chats"]) + + +def chat_to_response(chat) -> ChatResponse: + return ChatResponse( + id=str(chat.id), + title=chat.title, + member_ids=[str(member.user_id) for member in chat.members], + created_at=chat.created_at, + ) + + +@router.post("", response_model=ChatResponse, status_code=status.HTTP_201_CREATED) +def create_chat( + body: CreateChat, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + try: + chat = ChatService(db).create_chat( + title=body.title, + member_ids=body.member_ids, + creator_id=user.id, + ) + return chat_to_response(chat) + + except InvalidChatDataError as exc: + raise HTTPException( + status_code=400, + detail=str(exc), + ) from None + + +@router.get("") +def list_chats( + limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = None, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + if cursor is not None: + raise HTTPException( + status_code=400, + detail="Chat cursor pagination is not implemented yet", + ) + + chats = ChatService(db).list_chats(user.id) + chats = chats[:limit] + + return { + "items": [chat_to_response(chat) for chat in chats], + "next_cursor": None, + } + + +@router.get("/{chat_id}", response_model=ChatResponse) +def get_chat( + chat_id: str, + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + try: + parsed_id = uuid.UUID(chat_id) + except ValueError: + raise HTTPException(status_code=400, detail="Invalid chat UUID") from None + + try: + chat = ChatService(db).get_chat(parsed_id, user.id) + return chat_to_response(chat) + + except ChatNotFoundError: + raise HTTPException( + status_code=404, + detail="Chat not found", + ) from None diff --git a/app/api/deps.py b/app/api/deps.py new file mode 100644 index 00000000..6c695862 --- /dev/null +++ b/app/api/deps.py @@ -0,0 +1,40 @@ +from fastapi import Depends, HTTPException, status +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from sqlalchemy.orm import Session + +from app.db.session import SessionLocal +from app.services.auth_service import AuthService, InvalidSessionError + + +bearer_scheme = HTTPBearer(auto_error=False) + + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +def get_current_user( + credentials: HTTPAuthorizationCredentials | None = Depends(bearer_scheme), + db: Session = Depends(get_db), +): + if credentials is None or credentials.scheme.lower() != "bearer": + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Bearer token required", + headers={"WWW-Authenticate": "Bearer"}, + ) + + try: + user = AuthService(db).authenticate(credentials.credentials) + return user + + except InvalidSessionError: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Invalid or expired token", + headers={"WWW-Authenticate": "Bearer"}, + ) from None diff --git a/app/api/messages.py b/app/api/messages.py new file mode 100644 index 00000000..9b642345 --- /dev/null +++ b/app/api/messages.py @@ -0,0 +1,73 @@ +import re +import uuid + +from fastapi import APIRouter, Depends, Header, HTTPException, Query, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import CreateMessage, Message as MessageResponse +from app.services.message_service import ( + MessageService, MessageNotFoundError, InvalidMessageDataError, + IdempotencyConflictError, +) + +router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["Messages"]) +IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$") + + +def message_to_response(message) -> MessageResponse: + return MessageResponse( + id=str(message.id), chat_id=str(message.chat_id), + sender_id=str(message.sender_id), text=message.text, + created_at=message.created_at, + ) + + +def parse_chat_id(chat_id: str) -> uuid.UUID: + try: + return uuid.UUID(chat_id) + except ValueError: + raise HTTPException(status_code=400, detail="Invalid chat UUID") from None + + +@router.post("", response_model=MessageResponse, status_code=status.HTTP_201_CREATED) +def create_message( + chat_id: str, body: CreateMessage, + idempotency_key: str = Header(..., alias="Idempotency-Key", min_length=1, max_length=128), + db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + if not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key): + raise HTTPException(status_code=400, detail="Invalid Idempotency-Key") + parsed_id = parse_chat_id(chat_id) + try: + message = MessageService(db).create_message( + chat_id=parsed_id, sender_id=user.id, text=body.text, + idempotency_key=idempotency_key, + ) + return message_to_response(message) + except MessageNotFoundError: + raise HTTPException(status_code=404, detail="Chat not found") from None + except InvalidMessageDataError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from None + except IdempotencyConflictError: + raise HTTPException(status_code=409, detail="Idempotency-Key was already used with a different request") from None + + +@router.get("") +def list_messages( + chat_id: str, limit: int = Query(default=50, ge=1, le=100), + cursor: str | None = None, db: Session = Depends(get_db), + user: UserModel = Depends(get_current_user), +): + parsed_id = parse_chat_id(chat_id) + try: + messages, next_cursor = MessageService(db).get_history( + chat_id=parsed_id, user_id=user.id, limit=limit, cursor=cursor + ) + return {"items": [message_to_response(item) for item in messages], "next_cursor": next_cursor} + except MessageNotFoundError: + raise HTTPException(status_code=404, detail="Chat not found") from None + except InvalidMessageDataError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from None diff --git a/app/api/users.py b/app/api/users.py new file mode 100644 index 00000000..67adc7c6 --- /dev/null +++ b/app/api/users.py @@ -0,0 +1,55 @@ +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session + +from app.api.deps import get_db, get_current_user +from app.db.models import User as UserModel +from app.models import CreateUser, User +from app.services.user_service import ( + UserService, + UsernameAlreadyExistsError, +) + + +router = APIRouter(prefix="/api/v1/users", tags=["Users"]) + + +def user_to_response(user: UserModel) -> User: + return User( + id=str(user.id), + username=user.username, + display_name=user.display_name, + created_at=user.created_at, + ) + + +@router.post("", response_model=User, status_code=status.HTTP_201_CREATED) +def register_user( + body: CreateUser, + db: Session = Depends(get_db), +): + try: + user = UserService(db).register( + username=body.username, + display_name=body.display_name, + password=body.password, + ) + return user_to_response(user) + + except UsernameAlreadyExistsError: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Username already exists", + ) from None + + except ValueError as exc: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=str(exc), + ) from None + + +@router.get("/me", response_model=User) +def get_me( + current_user: UserModel = Depends(get_current_user), +): + return user_to_response(current_user) diff --git a/app/core/__init__.py b/app/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 00000000..ec78b7be --- /dev/null +++ b/app/core/config.py @@ -0,0 +1,24 @@ +import os +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + app_name: str = "Messenger API" + app_env: str = "lab" + database_url: str = "postgresql+psycopg://messenger:messenger@postgres:5432/messenger" + db_pool_size: int = 5 + db_max_overflow: int = 5 + db_pool_timeout: int = 10 + session_ttl_seconds: int = 3600 + cache_url: str = "redis://redis:6379/0" + message_cache_ttl_seconds: int = 60 + idempotency_ttl_seconds: int = 86400 + instance_id: str = os.getenv("INSTANCE_ID") or os.getenv("HOSTNAME") or "api-local" + shutdown_grace_seconds: int = 20 + + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", extra="ignore" + ) + + +settings = Settings() diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 00000000..e69de29b 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..772fdcff --- /dev/null +++ b/app/db/models.py @@ -0,0 +1,215 @@ +import uuid + +from datetime import datetime, timezone + +from sqlalchemy import ( + DateTime, + ForeignKey, + Index, + String, + Text, + UniqueConstraint, +) +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from app.db.base import Base + + +def utc_now() -> datetime: + return datetime.now(timezone.utc) + + +class User(Base): + __tablename__ = "users" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.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( + String(255), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + memberships: Mapped[list["ChatMember"]] = relationship( + back_populates="user", + cascade="all, delete-orphan", + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="sender", + ) + + +class Chat(Base): + __tablename__ = "chats" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + title: Mapped[str] = mapped_column( + String(100), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + members: Mapped[list["ChatMember"]] = relationship( + back_populates="chat", + cascade="all, delete-orphan", + ) + messages: Mapped[list["Message"]] = relationship( + back_populates="chat", + cascade="all, delete-orphan", + ) + + +class ChatMember(Base): + __tablename__ = "chat_members" + + chat_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + primary_key=True, + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + primary_key=True, + ) + + chat: Mapped["Chat"] = relationship( + back_populates="members", + ) + user: Mapped["User"] = relationship( + back_populates="memberships", + ) + + +class Message(Base): + __tablename__ = "messages" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + chat_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("chats.id", ondelete="CASCADE"), + nullable=False, + ) + sender_id: Mapped[uuid.UUID] = mapped_column( + 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=utc_now, + nullable=False, + ) + + chat: Mapped["Chat"] = relationship( + back_populates="messages", + ) + sender: Mapped["User"] = relationship( + back_populates="messages", + ) + + __table_args__ = ( + Index( + "ix_messages_chat_created_id", + "chat_id", + "created_at", + "id", + ), + ) + + +class Session(Base): + __tablename__ = "sessions" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + ) + token_hash: Mapped[str] = mapped_column( + String(64), + unique=True, + nullable=False, + index=True, + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), + ForeignKey("users.id", ondelete="CASCADE"), + nullable=False, + ) + expires_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + nullable=False, + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), + default=utc_now, + nullable=False, + ) + + user: Mapped["User"] = relationship() + +class IdempotencyRecord(Base): + __tablename__ = "idempotency_records" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 + ) + user_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False + ) + method: Mapped[str] = mapped_column(String(10), nullable=False) + path: Mapped[str] = mapped_column(String(255), nullable=False) + idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False) + request_hash: Mapped[str] = mapped_column(String(64), nullable=False) + # NULL is visible only inside the creating transaction; the message and + # idempotency row are committed atomically. + message_id: Mapped[uuid.UUID | None] = mapped_column( + UUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), nullable=True + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), default=utc_now, nullable=False + ) + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) + + __table_args__ = ( + UniqueConstraint( + "user_id", "method", "path", "idempotency_key", + name="uq_idempotency_scope_key", + ), + Index("ix_idempotency_records_expires_at", "expires_at"), + ) diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 00000000..190589bd --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,15 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + +from app.core.config import settings + +engine = create_engine( + settings.database_url, + pool_pre_ping=True, + pool_size=settings.db_pool_size, + max_overflow=settings.db_max_overflow, + pool_timeout=settings.db_pool_timeout, + pool_recycle=1800, +) + +SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..32fc2746 --- /dev/null +++ b/app/main.py @@ -0,0 +1,86 @@ +import logging +import uuid +from contextlib import asynccontextmanager + +from fastapi import FastAPI, HTTPException, Request +from fastapi.exceptions import RequestValidationError +from fastapi.responses import JSONResponse +from pathlib import Path +from sqlalchemy import text +from sqlalchemy.exc import SQLAlchemyError + +from app.api.auth import router as auth_router +from app.api.users import router as users_router +from app.api.chats import router as chats_router +from app.api.messages import router as messages_router +from app.core.config import settings +from app.db.session import engine + +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") +logger = logging.getLogger(__name__) +@asynccontextmanager +async def lifespan(app: FastAPI): + logger.info("Application startup instance_id=%s", settings.instance_id) + try: + yield + finally: + engine.dispose() + logger.info("Application shutdown instance_id=%s", settings.instance_id) + +app = FastAPI(title=settings.app_name, version="1.3.0", lifespan=lifespan) +app.include_router(users_router) +app.include_router(auth_router) +app.include_router(chats_router) +app.include_router(messages_router) + +@app.middleware("http") +async def response_headers(request: Request, call_next): + request_id = str(uuid.uuid4()) + request.state.request_id = request_id + try: + response = await call_next(request) + except Exception: + logger.exception("Unhandled request error; request_id=%s", request_id) + response = JSONResponse(status_code=500, content={"error": {"code": "internal_error", "message": "Unexpected error", "request_id": request_id}}) + response.headers["X-Request-Id"] = request_id + response.headers["X-Instance-Id"] = settings.instance_id + response.headers["X-Content-Type-Options"] = "nosniff" + return response + +@app.exception_handler(HTTPException) +async def http_exception_handler(request: Request, exc: HTTPException): + request_id = getattr(request.state, "request_id", str(uuid.uuid4())) + code = {400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict", 413: "payload_too_large", 415: "unsupported_media_type", 429: "rate_limited", 500: "internal_error", 503: "unavailable"}.get(exc.status_code, "internal_error") + headers = dict(exc.headers or {}) + headers["X-Request-Id"] = request_id + headers["X-Instance-Id"] = settings.instance_id + return JSONResponse(status_code=exc.status_code, content={"error": {"code": code, "message": exc.detail if isinstance(exc.detail, str) else "Request failed", "request_id": request_id}}, headers=headers) + +@app.exception_handler(RequestValidationError) +async def validation_exception_handler(request: Request, exc: RequestValidationError): + request_id = getattr(request.state, "request_id", str(uuid.uuid4())) + return JSONResponse(status_code=400, content={"error": {"code": "invalid_request", "message": "Validation error", "request_id": request_id}}, headers={"X-Request-Id": request_id, "X-Instance-Id": settings.instance_id}) + +@app.get("/health/live") +def liveness(): + return {"status": "ok"} + +DRAIN_FILE = Path("/tmp/api-draining") +@app.get("/health/ready") +def readiness(): + if DRAIN_FILE.exists(): + raise HTTPException( + status_code=503, + detail="Instance is draining", + ) + + try: + with engine.connect() as connection: + connection.execute(text("SELECT 1")) + return {"status": "ok"} + except SQLAlchemyError: + logger.exception("PostgreSQL readiness check failed") + raise HTTPException( + status_code=503, + detail="Database unavailable", + ) from None diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..ace6808f --- /dev/null +++ b/app/models.py @@ -0,0 +1,110 @@ + +from datetime import datetime, timezone +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class BaseConfig(BaseModel): + model_config = ConfigDict( + from_attributes=True, + json_encoders={ + datetime: lambda value: ( + value.astimezone(timezone.utc) + .isoformat(timespec="microseconds") + .replace("+00:00", "Z") + ) + }, + ) + + +class CreateUser(BaseConfig): + 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") + @classmethod + def validate_display_name(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("display_name must not be blank") + return value + + +class User(BaseConfig): + id: str + username: str + display_name: str + created_at: datetime + + +class CreateChat(BaseConfig): + title: str = Field(..., min_length=1, max_length=100) + member_ids: List[str] = Field(..., min_length=1, max_length=100) + + @field_validator("title") + @classmethod + def validate_title(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("title must not be blank") + return value + + @field_validator("member_ids") + @classmethod + def validate_unique_members(cls, value: List[str]) -> List[str]: + if len(value) != len(set(value)): + raise ValueError("member_ids must be unique") + return value + + +class Chat(BaseConfig): + id: str + title: str + member_ids: List[str] + created_at: datetime + + +class CreateMessage(BaseConfig): + text: str = Field(..., min_length=1, max_length=4000) + + @field_validator("text") + @classmethod + def validate_text(cls, value: str) -> str: + value = value.strip() + if not value: + raise ValueError("text must not be blank") + return value + + +class Message(BaseConfig): + id: str + chat_id: str + sender_id: str + text: str + created_at: datetime + + +class Health(BaseConfig): + status: str = "ok" + + +class ErrorDetail(BaseConfig): + code: str + message: str + request_id: str + + +class ErrorResponse(BaseConfig): + error: ErrorDetail + + +class ChatPage(BaseConfig): + items: List[Chat] + next_cursor: Optional[str] = None + + +class MessagePage(BaseConfig): + items: List[Message] + next_cursor: Optional[str] = None diff --git a/app/repositories/__init__.py b/app/repositories/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/repositories/chat_repository.py b/app/repositories/chat_repository.py new file mode 100644 index 00000000..e5cd838e --- /dev/null +++ b/app/repositories/chat_repository.py @@ -0,0 +1,71 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session, selectinload + +from app.db.models import Chat, ChatMember + + +class ChatRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, chat_id: uuid.UUID) -> Chat | None: + statement = ( + select(Chat) + .options(selectinload(Chat.members)) + .where(Chat.id == chat_id) + ) + + return self.db.scalar(statement) + + def get_by_user( + self, + user_id: uuid.UUID, + limit: int = 50, + ) -> list[Chat]: + statement = ( + 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) + ) + + return list(self.db.scalars(statement).all()) + + def is_member( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + ) -> bool: + statement = select(ChatMember).where( + ChatMember.chat_id == chat_id, + ChatMember.user_id == user_id, + ) + + return self.db.scalar(statement) is not None + + def create( + self, + title: str, + member_ids: list[uuid.UUID], + ) -> Chat: + chat = Chat(title=title) + + self.db.add(chat) + self.db.flush() + + memberships = [ + ChatMember( + chat_id=chat.id, + user_id=user_id, + ) + for user_id in member_ids + ] + + self.db.add_all(memberships) + self.db.flush() + + return chat \ No newline at end of file diff --git a/app/repositories/message_repository.py b/app/repositories/message_repository.py new file mode 100644 index 00000000..6698ed1d --- /dev/null +++ b/app/repositories/message_repository.py @@ -0,0 +1,71 @@ +import uuid + +from datetime import datetime + +from sqlalchemy import and_, or_, select +from sqlalchemy.orm import Session + +from app.db.models import Message + + +class MessageRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id( + self, + message_id: uuid.UUID, + ) -> Message | None: + statement = select(Message).where( + Message.id == message_id + ) + + return self.db.scalar(statement) + + def create( + self, + chat_id: uuid.UUID, + sender_id: uuid.UUID, + text: str, + ) -> Message: + message = Message( + chat_id=chat_id, + sender_id=sender_id, + text=text, + ) + + self.db.add(message) + self.db.flush() + + return message + + def get_history( + self, + chat_id: uuid.UUID, + limit: int = 50, + cursor_created_at: datetime | None = None, + cursor_id: uuid.UUID | None = None, + ) -> list[Message]: + statement = select(Message).where( + Message.chat_id == chat_id + ) + + if cursor_created_at is not None and cursor_id is not None: + statement = statement.where( + or_( + Message.created_at > cursor_created_at, + and_( + Message.created_at == cursor_created_at, + Message.id > cursor_id, + ), + ) + ) + + statement = ( + statement + .order_by(Message.created_at.asc(), Message.id.asc()) + .limit(limit) + ) + + return list(self.db.scalars(statement).all()) \ No newline at end of file diff --git a/app/repositories/session_repository.py b/app/repositories/session_repository.py new file mode 100644 index 00000000..7366b170 --- /dev/null +++ b/app/repositories/session_repository.py @@ -0,0 +1,78 @@ +import uuid + +from datetime import datetime, timezone + +from sqlalchemy import delete, select +from sqlalchemy.orm import Session + +from app.db.models import Session as UserSession + + +class SessionRepository: + + def __init__(self, db: Session): + self.db = db + + def create( + self, + user_id: uuid.UUID, + token_hash: str, + expires_at: datetime, + ) -> UserSession: + session = UserSession( + user_id=user_id, + token_hash=token_hash, + expires_at=expires_at, + ) + + self.db.add(session) + self.db.flush() + + return session + + def get_by_token_hash( + self, + token_hash: str, + ) -> UserSession | None: + statement = select(UserSession).where( + UserSession.token_hash == token_hash + ) + + return self.db.scalar(statement) + + def get_valid_session( + self, + token_hash: str, + ) -> UserSession | None: + now = datetime.now(timezone.utc) + + statement = select(UserSession).where( + UserSession.token_hash == token_hash, + UserSession.expires_at > now, + ) + + return self.db.scalar(statement) + + def delete_by_token_hash( + self, + token_hash: str, + ) -> bool: + statement = ( + delete(UserSession) + .where(UserSession.token_hash == token_hash) + .returning(UserSession.id) + ) + + return self.db.scalar(statement) is not None + + def delete_expired(self) -> int: + now = datetime.now(timezone.utc) + + statement = ( + delete(UserSession) + .where(UserSession.expires_at <= now) + ) + + result = self.db.execute(statement) + + return result.rowcount \ No newline at end of file diff --git a/app/repositories/user_repository.py b/app/repositories/user_repository.py new file mode 100644 index 00000000..4569dcd1 --- /dev/null +++ b/app/repositories/user_repository.py @@ -0,0 +1,41 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session + +from app.db.models import User + + +class UserRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, user_id: uuid.UUID) -> User | None: + statement = select(User).where(User.id == user_id) + + return self.db.scalar(statement) + + def get_by_username(self, username: str) -> User | None: + statement = select(User).where( + User.username == username + ) + + return self.db.scalar(statement) + + def create( + self, + username: str, + display_name: str, + password_hash: str, + ) -> User: + user = User( + username=username, + display_name=display_name, + password_hash=password_hash, + ) + + self.db.add(user) + self.db.flush() + + return user \ No newline at end of file diff --git a/app/services/__init__.py b/app/services/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/services/auth_service.py b/app/services/auth_service.py new file mode 100644 index 00000000..e459ddf3 --- /dev/null +++ b/app/services/auth_service.py @@ -0,0 +1,159 @@ +import hashlib +import secrets +from datetime import datetime, timedelta, timezone + +from redis import Redis +from redis.backoff import NoBackoff +from redis.exceptions import RedisError +from redis.retry import Retry +from sqlalchemy.orm import Session + +from app.core.config import settings +from app.db.models import User +from app.repositories.session_repository import SessionRepository +from app.repositories.user_repository import UserRepository +from app.services.user_service import password_hasher + + +class InvalidCredentialsError(Exception): + pass + + +class InvalidSessionError(Exception): + pass + + +class AuthService: + def __init__(self, db: Session): + self.db = db + self.sessions = SessionRepository(db) + self.users = UserRepository(db) + + self._redis_available = True + + self.redis = Redis.from_url( + settings.cache_url, + decode_responses=True, + socket_connect_timeout=0.5, + socket_timeout=0.5, + retry_on_timeout=False, + retry=Retry(NoBackoff(), 0), + ) + + @staticmethod + def _hash_token(token: str) -> str: + return hashlib.sha256(token.encode("utf-8")).hexdigest() + + def create_session(self, username: str, password: str) -> dict: + user = self.users.get_by_username(username) + + if user is None or not password_hasher.verify( + password, user.password_hash + ): + raise InvalidCredentialsError("Invalid username or password") + + token = secrets.token_urlsafe(32) + token_hash = self._hash_token(token) + + expires_at = datetime.now(timezone.utc) + timedelta( + seconds=settings.session_ttl_seconds + ) + + try: + self.sessions.create( + user_id=user.id, + token_hash=token_hash, + expires_at=expires_at, + ) + self.db.commit() + except Exception: + self.db.rollback() + raise + + self._cache_session(token_hash, user.id, expires_at) + + return { + "token": token, + "token_type": "Bearer", + "expires_at": expires_at, + "user": user, + } + + def _cache_session( + self, + token_hash: str, + user_id, + expires_at, + ) -> None: + if not self._redis_available: + return + + ttl = max( + 1, + int((expires_at - datetime.now(timezone.utc)).total_seconds()), + ) + + try: + self.redis.setex( + f"session:{token_hash}", + ttl, + str(user_id), + ) + except RedisError: + self._redis_available = False + + def authenticate(self, token: str) -> User: + token_hash = self._hash_token(token) + cached_user_id = None + + if self._redis_available: + try: + cached_user_id = self.redis.get( + f"session:{token_hash}" + ) + except RedisError: + self._redis_available = False + + session = self.sessions.get_valid_session(token_hash) + + if session is None: + if self._redis_available: + try: + self.redis.delete(f"session:{token_hash}") + except RedisError: + self._redis_available = False + + raise InvalidSessionError( + "Session is invalid or expired" + ) + + if cached_user_id is None: + self._cache_session( + token_hash, + session.user_id, + session.expires_at, + ) + + return session.user + + def delete_session(self, token: str) -> None: + token_hash = self._hash_token(token) + + try: + deleted = self.sessions.delete_by_token_hash(token_hash) + + if not deleted: + raise InvalidSessionError( + "Session is invalid or expired" + ) + + self.db.commit() + except Exception: + self.db.rollback() + raise + + if self._redis_available: + try: + self.redis.delete(f"session:{token_hash}") + except RedisError: + self._redis_available = False diff --git a/app/services/chat_service.py b/app/services/chat_service.py new file mode 100644 index 00000000..10ff5656 --- /dev/null +++ b/app/services/chat_service.py @@ -0,0 +1,94 @@ +import uuid + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session + +from app.db.models import Chat +from app.repositories.chat_repository import ChatRepository +from app.repositories.user_repository import UserRepository + + +class ChatNotFoundError(Exception): + pass + + +class ChatAccessError(Exception): + pass + + +class InvalidChatDataError(Exception): + pass + + +class ChatService: + def __init__(self, db: Session): + self.db = db + self.chats = ChatRepository(db) + self.users = UserRepository(db) + + def create_chat( + self, + title: str, + member_ids: list[str], + creator_id: uuid.UUID, + ) -> Chat: + title = title.strip() + + if not title or len(title) > 100: + raise InvalidChatDataError("Invalid chat title") + + if not member_ids or len(member_ids) > 100: + raise InvalidChatDataError("Invalid members") + + try: + parsed_ids = [uuid.UUID(str(value)) for value in member_ids] + except (ValueError, TypeError, AttributeError): + raise InvalidChatDataError("Invalid member UUID") from None + + if len(parsed_ids) != len(set(parsed_ids)): + raise InvalidChatDataError("Duplicate members") + + if creator_id not in parsed_ids: + raise InvalidChatDataError("Creator must be a member") + + for user_id in parsed_ids: + if self.users.get_by_id(user_id) is None: + raise InvalidChatDataError("Unknown member") + + try: + chat = self.chats.create( + title=title, + member_ids=parsed_ids, + ) + self.db.commit() + self.db.refresh(chat) + return chat + + except IntegrityError as exc: + self.db.rollback() + raise InvalidChatDataError("Unable to create chat") from exc + + except Exception: + self.db.rollback() + raise + + def get_chat(self, chat_id: uuid.UUID, user_id: uuid.UUID) -> Chat: + chat = self.chats.get_by_id(chat_id) + + if chat is None: + raise ChatNotFoundError() + + if not self.chats.is_member(chat_id, user_id): + raise ChatNotFoundError() + + return chat + + def list_chats(self, user_id: uuid.UUID) -> list[Chat]: + return self.chats.get_by_user(user_id) + + def check_membership( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + ) -> bool: + return self.chats.is_member(chat_id, user_id) diff --git a/app/services/message_service.py b/app/services/message_service.py new file mode 100644 index 00000000..381da242 --- /dev/null +++ b/app/services/message_service.py @@ -0,0 +1,418 @@ +import base64 +import hashlib +import json +import time +import uuid +from datetime import datetime, timedelta, timezone +from types import SimpleNamespace + +from redis import Redis +from redis.backoff import NoBackoff +from redis.exceptions import RedisError +from redis.retry import Retry +from sqlalchemy import delete, select, update +from sqlalchemy.dialects.postgresql import insert as pg_insert +from sqlalchemy.orm import Session + +from app.core.config import settings +from app.db.models import IdempotencyRecord, Message +from app.repositories.chat_repository import ChatRepository +from app.repositories.message_repository import MessageRepository + + +class MessageNotFoundError(Exception): + pass + + +class MessageAccessError(Exception): + pass + + +class InvalidMessageDataError(Exception): + pass + + +class IdempotencyConflictError(Exception): + pass + + +class MessageService: + def __init__(self, db: Session): + self.db = db + self.messages = MessageRepository(db) + self.chats = ChatRepository(db) + + self._redis_available = True + + self.redis = Redis.from_url( + settings.cache_url, + decode_responses=True, + socket_connect_timeout=0.5, + socket_timeout=0.5, + retry_on_timeout=False, + retry=Retry(NoBackoff(), 0), + ) + + @staticmethod + def encode_cursor(message: Message) -> str: + payload = { + "created_at": message.created_at.isoformat(), + "id": str(message.id), + } + + raw = json.dumps(payload).encode("utf-8") + + return ( + base64.urlsafe_b64encode(raw) + .decode("ascii") + .rstrip("=") + ) + + @staticmethod + def decode_cursor(cursor: str | None): + if not cursor: + return None, None + + try: + padded = cursor + "=" * (-len(cursor) % 4) + raw = base64.urlsafe_b64decode(padded.encode("ascii")) + payload = json.loads(raw.decode("utf-8")) + + created_at = datetime.fromisoformat(payload["created_at"]) + message_id = uuid.UUID(payload["id"]) + + if created_at.tzinfo is None: + raise ValueError("Cursor timestamp must include timezone") + + return created_at, message_id + + except (ValueError, TypeError, KeyError, UnicodeError): + raise InvalidMessageDataError("Invalid cursor") from None + + def _version_key(self, chat_id: uuid.UUID) -> str: + return f"messages:version:{chat_id}" + + def _cache_key( + self, + chat_id: uuid.UUID, + version: int, + limit: int, + cursor: str | None, + ) -> str: + cursor_hash = hashlib.sha256( + (cursor or "first-page").encode("utf-8") + ).hexdigest()[:16] + + return ( + f"messages:{chat_id}:v{version}:" + f"limit:{limit}:cursor:{cursor_hash}" + ) + + def _get_version(self, chat_id: uuid.UUID) -> int: + if not self._redis_available: + return 0 + + try: + value = self.redis.get(self._version_key(chat_id)) + return int(value or 0) + except (RedisError, ValueError): + self._redis_available = False + return 0 + + def _bump_version(self, chat_id: uuid.UUID) -> None: + if not self._redis_available: + return + + try: + self.redis.incr(self._version_key(chat_id)) + except RedisError: + self._redis_available = False + + @staticmethod + def _serialize(messages, next_cursor): + return json.dumps({ + "items": [ + { + "id": str(message.id), + "chat_id": str(message.chat_id), + "sender_id": str(message.sender_id), + "text": message.text, + "created_at": message.created_at.isoformat(), + } + for message in messages + ], + "next_cursor": next_cursor, + }) + + @staticmethod + def _deserialize(payload: str): + data = json.loads(payload) + + messages = [ + SimpleNamespace( + id=uuid.UUID(item["id"]), + chat_id=uuid.UUID(item["chat_id"]), + sender_id=uuid.UUID(item["sender_id"]), + text=item["text"], + created_at=datetime.fromisoformat(item["created_at"]), + ) + for item in data["items"] + ] + + return messages, data["next_cursor"] + + def _read_cache(self, key: str): + if not self._redis_available: + return None + + try: + payload = self.redis.get(key) + + if payload is None: + return None + + return self._deserialize(payload) + + except RedisError: + self._redis_available = False + return None + except (ValueError, TypeError, KeyError): + return None + + def _load_from_database( + self, + chat_id, + limit, + cursor_created_at, + cursor_id, + ): + rows = self.messages.get_history( + chat_id=chat_id, + limit=limit + 1, + cursor_created_at=cursor_created_at, + cursor_id=cursor_id, + ) + + has_more = len(rows) > limit + items = rows[:limit] + + next_cursor = ( + self.encode_cursor(items[-1]) + if has_more and items + else None + ) + + return items, next_cursor + + def create_message( + self, + chat_id: uuid.UUID, + sender_id: uuid.UUID, + text: str, + idempotency_key: str, + ) -> Message: + text = text.strip() + if not text or len(text) > 4000: + raise InvalidMessageDataError("Invalid message text") + + # Access is checked for every retry, before consulting the key. + if not self.chats.is_member(chat_id, sender_id): + raise MessageNotFoundError() + + method = "POST" + path = f"/api/v1/chats/{chat_id}/messages" + request_hash = hashlib.sha256( + json.dumps({"text": text}, sort_keys=True, separators=(",", ":")).encode("utf-8") + ).hexdigest() + now = datetime.now(timezone.utc) + expires_at = now + timedelta(seconds=max(settings.idempotency_ttl_seconds, 86400)) + scope = ( + IdempotencyRecord.user_id == sender_id, + IdempotencyRecord.method == method, + IdempotencyRecord.path == path, + IdempotencyRecord.idempotency_key == idempotency_key, + ) + + try: + # An expired key may be reused. Deletion and claim are serialized by + # PostgreSQL's unique constraint for the scope. + self.db.execute( + delete(IdempotencyRecord).where(*scope, IdempotencyRecord.expires_at <= now) + ) + claim = ( + pg_insert(IdempotencyRecord) + .values( + id=uuid.uuid4(), user_id=sender_id, method=method, path=path, + idempotency_key=idempotency_key, request_hash=request_hash, + message_id=None, created_at=now, expires_at=expires_at, + ) + .on_conflict_do_nothing(constraint="uq_idempotency_scope_key") + .returning(IdempotencyRecord.id) + ) + claimed_id = self.db.execute(claim).scalar_one_or_none() + + if claimed_id is None: + existing = self.db.execute( + select(IdempotencyRecord).where(*scope).with_for_update() + ).scalar_one_or_none() + if existing is None: + # Rare expiration race: retry once in a fresh transaction. + self.db.rollback() + return self.create_message(chat_id, sender_id, text, idempotency_key) + if existing.request_hash != request_hash: + self.db.rollback() + raise IdempotencyConflictError() + if existing.message_id is None: + raise RuntimeError("Committed idempotency record has no message") + message = self.db.get(Message, existing.message_id) + if message is None: + raise RuntimeError("Idempotency record references a missing message") + self.db.commit() + return message + + message = self.messages.create( + chat_id=chat_id, sender_id=sender_id, text=text + ) + self.db.execute( + update(IdempotencyRecord) + .where(IdempotencyRecord.id == claimed_id) + .values(message_id=message.id) + ) + self.db.commit() + self.db.refresh(message) + self._bump_version(chat_id) + return message + except IdempotencyConflictError: + raise + except Exception: + self.db.rollback() + raise + + def get_history( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + limit: int = 50, + cursor: str | None = None, + ): + if not 1 <= limit <= 100: + raise InvalidMessageDataError( + "Limit must be between 1 and 100" + ) + + if not self.chats.is_member(chat_id, user_id): + raise MessageNotFoundError() + + cursor_created_at, cursor_id = self.decode_cursor(cursor) + + version = self._get_version(chat_id) + + cache_key = self._cache_key( + chat_id, + version, + limit, + cursor, + ) + + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + lock_key = f"lock:{cache_key}" + lock_token = str(uuid.uuid4()) + + try: + acquired = self.redis.set( + lock_key, + lock_token, + nx=True, + ex=10, + ) + except RedisError: + self._redis_available = False + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + if not acquired: + for _ in range(10): + time.sleep(0.05) + + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + break + + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + try: + cached = self._read_cache(cache_key) + + if cached is not None: + return cached + + if not self._redis_available: + return self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + items, next_cursor = self._load_from_database( + chat_id, + limit, + cursor_created_at, + cursor_id, + ) + + if self._redis_available and self._get_version(chat_id) == version: + try: + self.redis.setex( + cache_key, + settings.message_cache_ttl_seconds, + self._serialize(items, next_cursor), + ) + except RedisError: + self._redis_available = False + + return items, next_cursor + + finally: + if self._redis_available: + try: + self.redis.eval( + """ + if redis.call('get', KEYS[1]) == ARGV[1] + then + return redis.call('del', KEYS[1]) + else + return 0 + end + """, + 1, + lock_key, + lock_token, + ) + except RedisError: + self._redis_available = False diff --git a/app/services/user_service.py b/app/services/user_service.py new file mode 100644 index 00000000..896ee5ec --- /dev/null +++ b/app/services/user_service.py @@ -0,0 +1,81 @@ +import re + +from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import Session +from pwdlib import PasswordHash + +from app.db.models import User +from app.repositories.user_repository import UserRepository + + +password_hasher = PasswordHash.recommended() + + +class UsernameAlreadyExistsError(Exception): + pass + + +class UserService: + + def __init__(self, db: Session): + self.db = db + self.users = UserRepository(db) + + def register( + self, + username: str, + display_name: str, + password: str, + ) -> User: + + if not re.fullmatch(r"[a-z0-9_]{3,32}", username): + raise ValueError("Invalid username") + + if not display_name or not display_name.strip(): + raise ValueError("Display name cannot be empty") + + if len(display_name) > 100: + raise ValueError("Display name is too long") + + if not 12 <= len(password) <= 128: + raise ValueError("Invalid password length") + + if self.users.get_by_username(username) is not None: + raise UsernameAlreadyExistsError( + "Username already exists" + ) + + password_hash = password_hasher.hash(password) + + try: + user = self.users.create( + username=username, + display_name=display_name, + password_hash=password_hash, + ) + + self.db.commit() + self.db.refresh(user) + + return user + + except IntegrityError as exc: + self.db.rollback() + + raise UsernameAlreadyExistsError( + "Username already exists" + ) from exc + + except Exception: + self.db.rollback() + raise + + def verify_password( + self, + user: User, + password: str, + ) -> bool: + return password_hasher.verify( + password, + user.password_hash, + ) \ No newline at end of file diff --git a/app/store.py b/app/store.py new file mode 100644 index 00000000..4802821c --- /dev/null +++ b/app/store.py @@ -0,0 +1,193 @@ +import base64 +import bisect +import binascii +import json +import os +import threading +import uuid +from datetime import datetime, timezone +from typing import Dict, List, Optional, Tuple + +from .models import Chat, Message, User + + +Key = Tuple[datetime, str] + + +def _encode_cursor(kind: str, scope: str, item) -> str: + payload = { + "v": 1, + "kind": kind, + "scope": scope, + "created_at": item.created_at.isoformat(), + "id": item.id, + } + raw = json.dumps(payload, separators=(",", ":")).encode("utf-8") + return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=") + + +def _decode_cursor(cursor: str, kind: str, scope: str) -> Key: + try: + if not cursor or len(cursor) > 4096: + raise ValueError + padded = cursor + "=" * (-len(cursor) % 4) + raw = base64.b64decode(padded, altchars=b"-_", validate=True) + payload = json.loads(raw.decode("utf-8")) + if not isinstance(payload, dict) or payload.get("v") != 1: + raise ValueError + if payload.get("kind") != kind or payload.get("scope") != scope: + raise ValueError + item_id = payload.get("id") + timestamp = payload.get("created_at") + if not isinstance(item_id, str) or not isinstance(timestamp, str): + raise ValueError + created_at = datetime.fromisoformat(timestamp) + if created_at.tzinfo is None: + raise ValueError + created_at = created_at.astimezone(timezone.utc) + return created_at, item_id + except ( + ValueError, + TypeError, + KeyError, + UnicodeDecodeError, + json.JSONDecodeError, + binascii.Error, + ): + raise ValueError("invalid_request") from None + + +class InMemoryStore: + def __init__(self): + self._lock = threading.RLock() + self.users: Dict[str, User] = {} + self.chats: Dict[str, Chat] = {} + self.messages: Dict[str, List[Message]] = {} + self.usernames: Dict[str, str] = {} + self.message_keys: Dict[str, List[Key]] = {} + self.message_index_enabled = os.getenv("MESSAGE_INDEX_ENABLED", "1") != "0" + + def create_user(self, username: str, display_name: str) -> User: + with self._lock: + if username in self.usernames: + raise ValueError("conflict") + user_id = str(uuid.uuid4()) + user = User( + id=user_id, + username=username, + display_name=display_name, + created_at=datetime.now(timezone.utc), + ) + self.users[user_id] = user + self.usernames[username] = user_id + return user + + def get_user(self, user_id: str) -> Optional[User]: + with self._lock: + return self.users.get(user_id) + + def create_chat(self, title: str, member_ids: List[str], creator_id: str) -> Chat: + with self._lock: + if creator_id not in member_ids or len(member_ids) != len(set(member_ids)): + raise ValueError("invalid_request") + if any(member_id not in self.users for member_id in member_ids): + raise ValueError("invalid_request") + + chat_id = str(uuid.uuid4()) + chat = Chat( + id=chat_id, + title=title, + member_ids=list(member_ids), + created_at=datetime.now(timezone.utc), + ) + self.chats[chat_id] = chat + self.messages[chat_id] = [] + self.message_keys[chat_id] = [] + return chat + + def get_chat(self, chat_id: str, user_id: str) -> Optional[Chat]: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or user_id not in chat.member_ids: + return None + return chat + + def list_chats( + self, user_id: str, limit: int, cursor: Optional[str] + ) -> Tuple[List[Chat], Optional[str]]: + with self._lock: + user_chats = [chat for chat in self.chats.values() if user_id in chat.member_ids] + user_chats.sort(key=lambda chat: (chat.created_at, chat.id)) + keys = [(chat.created_at, chat.id) for chat in user_chats] + + start_index = 0 + if cursor: + target_key = _decode_cursor(cursor, kind="chats", scope=user_id) + start_index = bisect.bisect_right(keys, target_key) + + page_with_extra = user_chats[start_index : start_index + limit + 1] + has_next = len(page_with_extra) > limit + page = page_with_extra[:limit] + next_cursor = None + if has_next and page: + next_cursor = _encode_cursor("chats", user_id, page[-1]) + return page, next_cursor + + def create_message(self, chat_id: str, sender_id: str, text: str) -> Message: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or sender_id not in chat.member_ids: + raise ValueError("not_found") + + message = Message( + id=str(uuid.uuid4()), + chat_id=chat_id, + sender_id=sender_id, + text=text, + created_at=datetime.now(timezone.utc), + ) + messages = self.messages[chat_id] + keys = self.message_keys[chat_id] + key = (message.created_at, message.id) + + if self.message_index_enabled: + index = bisect.bisect_right(keys, key) + keys.insert(index, key) + messages.insert(index, message) + else: + current_keys = [(item.created_at, item.id) for item in messages] + index = bisect.bisect_right(current_keys, key) + messages.insert(index, message) + keys.insert(index, key) + return message + + def list_messages( + self, + chat_id: str, + user_id: str, + limit: int, + cursor: Optional[str], + ) -> Tuple[List[Message], Optional[str]]: + with self._lock: + chat = self.chats.get(chat_id) + if chat is None or user_id not in chat.member_ids: + raise ValueError("not_found") + + messages = self.messages[chat_id] + if self.message_index_enabled: + keys = self.message_keys[chat_id] + else: + keys = [(item.created_at, item.id) for item in messages] + + start_index = 0 + if cursor: + target_key = _decode_cursor(cursor, kind="messages", scope=chat_id) + start_index = bisect.bisect_right(keys, target_key) + + page_with_extra = messages[start_index : start_index + limit + 1] + has_next = len(page_with_extra) > limit + page = page_with_extra[:limit] + next_cursor = None + if has_next and page: + next_cursor = _encode_cursor("messages", chat_id, page[-1]) + return page, next_cursor diff --git a/migrations/README b/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 00000000..0bc520ad --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,60 @@ +from logging.config import fileConfig + +from alembic import context +from sqlalchemy import engine_from_config, pool + +from app.core.config import settings +from app.db.base import Base +from app.db import models + + +config = context.config + +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +config.set_main_option( + "sqlalchemy.url", + settings.database_url.replace("%", "%%"), +) + +target_metadata = Base.metadata + + +def run_migrations_offline() -> None: + url = settings.database_url + + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + compare_type=True, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online() -> None: + connectable = engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + compare_type=True, + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() \ No newline at end of file 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/21ad89656327_create_initial_database_schema.py b/migrations/versions/21ad89656327_create_initial_database_schema.py new file mode 100644 index 00000000..6c48b769 --- /dev/null +++ b/migrations/versions/21ad89656327_create_initial_database_schema.py @@ -0,0 +1,79 @@ +"""create initial database schema + +Revision ID: 21ad89656327 +Revises: +Create Date: 2026-11-04 17:31:39.336368 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '21ad89656327' +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_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.String(length=255), 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_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('ix_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('token_hash', sa.String(length=64), nullable=False), + sa.Column('user_id', sa.UUID(), 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_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_table('sessions') + op.drop_index('ix_messages_chat_created_id', table_name='messages') + op.drop_table('messages') + op.drop_table('chat_members') + op.drop_index(op.f('ix_users_username'), table_name='users') + op.drop_table('users') + op.drop_table('chats') + # ### end Alembic commands ### diff --git a/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py b/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py new file mode 100644 index 00000000..a228b426 --- /dev/null +++ b/migrations/versions/8c3d9f4a21b7_add_idempotency_records.py @@ -0,0 +1,39 @@ +"""add shared PostgreSQL idempotency records + +Revision ID: 8c3d9f4a21b7 +Revises: 21ad89656327 +Create Date: 2026-11-05 +""" +from typing import Sequence, Union +from alembic import op +import sqlalchemy as sa + +revision: str = "8c3d9f4a21b7" +down_revision: Union[str, None] = "21ad89656327" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.create_table( + "idempotency_records", + sa.Column("id", sa.UUID(), nullable=False), + sa.Column("user_id", sa.UUID(), nullable=False), + sa.Column("method", sa.String(length=10), nullable=False), + sa.Column("path", sa.String(length=255), nullable=False), + sa.Column("idempotency_key", sa.String(length=128), nullable=False), + sa.Column("request_hash", sa.String(length=64), nullable=False), + sa.Column("message_id", sa.UUID(), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), + sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False), + sa.ForeignKeyConstraint(["message_id"], ["messages.id"], ondelete="CASCADE"), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("user_id", "method", "path", "idempotency_key", name="uq_idempotency_scope_key"), + ) + op.create_index("ix_idempotency_records_expires_at", "idempotency_records", ["expires_at"], unique=False) + + +def downgrade() -> None: + op.drop_index("ix_idempotency_records_expires_at", table_name="idempotency_records") + op.drop_table("idempotency_records") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..e519117f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +fastapi==0.115.6 +uvicorn[standard]==0.34.0 +pydantic==2.10.4 +pydantic-settings==2.7.1 +SQLAlchemy==2.0.36 +psycopg[binary]==3.2.3 +alembic==1.14.0 +redis==5.2.1 +pwdlib[argon2]==0.2.1 diff --git a/scripts/analyze-scaling.py b/scripts/analyze-scaling.py new file mode 100644 index 00000000..de77ecdf --- /dev/null +++ b/scripts/analyze-scaling.py @@ -0,0 +1,52 @@ + +#!/usr/bin/env python3 +"""Create charts from real Track 1 summary.csv data; does not invent measurements.""" + +import csv +from pathlib import Path +import matplotlib.pyplot as plt + +root = Path(__file__).resolve().parents[1] +source = root / 'evidence' / 'scaling' / 'summary.csv' + +if not source.exists(): + raise SystemExit('Run scripts/run-scaling-experiment.py first') + +with source.open(encoding='utf-8', newline='') as f: + rows = list(csv.DictReader(f)) + +if not rows: + raise SystemExit('summary.csv is empty') + +out = source.parent + +for key, title, ylabel, filename in [ + ('successful_rps', 'Track 1: successful throughput', + 'Successful requests/sec', 'throughput.png'), + ('p95_ms', 'Track 1: p95 latency', + 'Milliseconds', 'p95-latency.png'), + ('error_ratio', 'Track 1: error ratio', + 'Error ratio', 'errors.png'), +]: + labels = [r['scenario'] for r in rows] + values = [float(r[key]) if r[key] else 0 for r in rows] + + plt.figure(figsize=(11, 5)) + plt.bar(labels, values) + + if key == 'error_ratio': + plt.ylim(0, 0.01) + for i, value in enumerate(values): + plt.text( + i, 0.0002, f'{value:.1%}', + ha='center', va='bottom' + ) + + plt.title(title) + plt.ylabel(ylabel) + plt.xticks(rotation=25, ha='right') + plt.tight_layout() + plt.savefig(out / filename, dpi=160) + plt.close() + +print('Charts written to', out) diff --git a/scripts/concurrent-idempotency.py b/scripts/concurrent-idempotency.py new file mode 100644 index 00000000..0253e5a3 --- /dev/null +++ b/scripts/concurrent-idempotency.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Concurrent idempotency verification through the public load-balancer URL.""" +from concurrent.futures import ThreadPoolExecutor +import uuid +import sys +from pathlib import Path +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client, Failure, arguments, check, run + + +def main(): + def configure(p): + p.add_argument('--requests', type=int, default=20) + args = arguments('Concurrent cross-replica idempotency test', configure) + check(2 <= args.requests <= 100, 'requests must be 2..100') + c = Client(args.base_url, args.ca_file, timeout=10) + alice, bob, eve, chat = c.bootstrap() + key = 'race-' + uuid.uuid4().hex + body = 'same concurrent request ' + uuid.uuid4().hex + + def send(_): + return Client(args.base_url, args.ca_file, timeout=10).message(alice, chat, body, key=key) + + with ThreadPoolExecutor(max_workers=args.requests) as pool: + responses = list(pool.map(send, range(args.requests))) + messages = [r['json'] for r in responses] + check(all(m == messages[0] for m in messages), 'concurrent retries returned different messages') + instances = {r['headers'].get('X-Instance-Id') for r in responses} + check(None not in instances, 'missing X-Instance-Id') + check(len(instances) >= 2, 'load balancer did not expose at least two API replicas') + check(len({m['id'] for m in messages}) == 1, 'concurrent retries created duplicates') + conflict = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, + actor=alice, body={'text': body + ' changed'}, + headers={'Idempotency-Key': key}) + print({'requests': args.requests, 'unique_message_ids': 1, + 'observed_instances': len(instances), 'different_body_status': conflict['status']}) + print('PASS: concurrent same-key requests produced exactly one message') + +if __name__ == '__main__': + run(main) diff --git a/scripts/load-diagnostic.py b/scripts/load-diagnostic.py new file mode 100644 index 00000000..a958e494 --- /dev/null +++ b/scripts/load-diagnostic.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Diagnostic closed-loop load test. Does not modify public tests.""" +from concurrent.futures import ThreadPoolExecutor +from collections import Counter +from pathlib import Path +import json +import math +import re +import sys +import threading +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) + +from client import Client, Failure, LAB, arguments, check, run + + +def main(): + def configure(p): + p.add_argument('--duration', type=float, default=90) + p.add_argument('--concurrency', type=int, default=8) + p.add_argument('--output', default='evidence/rolling-update-diagnostic.json') + + args = arguments('Diagnostic synthetic POST message load', configure) + check(1 <= args.duration <= 3600, 'duration must be 1..3600 seconds') + check(1 <= args.concurrency <= 128, 'concurrency must be 1..128') + + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + barrier = threading.Barrier(args.concurrency) + + def worker(worker_id): + local = Client(args.base_url, args.ca_file, timeout=5) + barrier.wait() + deadline = time.monotonic() + args.duration + latencies = [] + errors = Counter() + examples = [] + index = 0 + + while time.monotonic() < deadline: + start = time.monotonic() + try: + local.message(alice, chat, 'diagnostic-%s-%s' % (worker_id, index)) + latencies.append(time.monotonic() - start) + except Failure as exc: + message = str(exc) + match = re.search(r'received (\d{3})', message) + + if match: + category = 'http_' + match.group(1) + elif 'connection/TLS failure' in message: + kind = re.search(r'\(([^)]+)\)', message) + category = 'connection_' + (kind.group(1) if kind else 'unknown') + else: + category = 'other_failure' + + errors[category] += 1 + if len(examples) < 10: + examples.append(message[:300]) + + index += 1 + + return latencies, errors, examples + + started = time.monotonic() + with ThreadPoolExecutor(max_workers=args.concurrency) as pool: + results = list(pool.map(worker, range(args.concurrency))) + elapsed = time.monotonic() - started + + latencies = sorted(x for values, _, _ in results for x in values) + errors = Counter() + examples = [] + + for _, counts, samples in results: + errors.update(counts) + examples.extend(samples) + + total_errors = sum(errors.values()) + total = len(latencies) + total_errors + + def percentile(p): + if not latencies: + return None + return round(1000 * latencies[max(0, math.ceil(p * len(latencies)) - 1)], 3) + + report = { + 'lab': LAB, + 'workload': 'closed-loop POST messages; no automatic retries', + 'duration_seconds': round(elapsed, 3), + 'concurrency': args.concurrency, + 'successful_requests': len(latencies), + 'failed_requests': total_errors, + 'error_ratio': round(total_errors / total, 6) if total else None, + 'errors_by_type': dict(errors), + 'successful_rps': round(len(latencies) / elapsed, 3), + 'successful_latency_ms': { + 'p50': percentile(.50), + 'p95': percentile(.95), + 'p99': percentile(.99) + }, + 'error_examples': examples[:20], + 'note': 'Latency percentiles cover successful requests only.' + } + + text = json.dumps(report, ensure_ascii=False, indent=2) + dest = Path(args.output) + dest.parent.mkdir(parents=True, exist_ok=True) + dest.write_text(text + '\n', encoding='utf-8') + print(text) + check(bool(latencies), 'no successful writes') + + +if __name__ == '__main__': + run(main) diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..cd4695e5 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +# Restart all stateful dependencies and all API replicas without touching volumes. +docker compose restart postgres redis api diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh new file mode 100644 index 00000000..4cbb334a --- /dev/null +++ b/scripts/rolling-update.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -eu +# Compose does not provide a native rolling-update primitive. This script +# performs a surge replacement: add one new healthy replica, then remove one old. +desired="${API_REPLICAS:-2}" +load_duration="${ROLLING_LOAD_DURATION:-90}" +load_script="${LOAD_SCRIPT:-tests/load.py}" +load_output="${ROLLING_LOAD_OUTPUT:-evidence/rolling-update-load.json}" +load_log="${ROLLING_LOAD_LOG:-evidence/rolling-update-load.log}" +if [ "$desired" -lt 2 ]; then echo 'API_REPLICAS must be >= 2' >&2; exit 2; fi + +docker compose build api +python "$load_script" --base-url http://localhost:8080 --duration "$load_duration" --concurrency 8 --output "$load_output" >"$load_log" 2>&1 & +load_pid=$! +cleanup() { kill "$load_pid" 2>/dev/null || true; wait "$load_pid" 2>/dev/null || true; } +trap cleanup EXIT INT TERM + +# Capture the old replica ids. Each iteration adds a new container before removing an old one. +old_ids=$(docker compose ps -q api) +for old_id in $old_ids; do + before=$(docker compose ps -q api | sort) + docker compose up -d --no-deps --no-recreate --scale "api=$((desired + 1))" api + new_id='' + attempt=0 + while [ "$attempt" -lt 60 ]; do + after=$(docker compose ps -q api | sort) + new_id=$(comm -13 <(printf '%s\n' "$before") <(printf '%s\n' "$after") | head -n 1 || true) + if [ -n "$new_id" ]; then + health=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$new_id") + if [ "$health" = healthy ]; then break; fi + fi + attempt=$((attempt + 1)); sleep 2 + done + if [ -z "$new_id" ] || [ "${health:-}" != healthy ]; then + echo 'New replica did not become healthy; leaving old replicas running' >&2 + exit 1 + fi + docker exec "$old_id" sh -c 'touch /tmp/api-draining' + sleep 10 + docker stop --timeout 20 "$old_id" >/dev/null + docker rm "$old_id" >/dev/null + docker compose up -d --no-deps --no-recreate --scale "api=$desired" api + python scripts/wait-ready.py http://localhost:8080/health/ready 30 +done +wait "$load_pid" || { echo 'Load test failed; inspect evidence/rolling-update-load.log' >&2; exit 1; } +trap - EXIT INT TERM diff --git a/scripts/run-scaling-experiment.py b/scripts/run-scaling-experiment.py new file mode 100644 index 00000000..24e00595 --- /dev/null +++ b/scripts/run-scaling-experiment.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 +"""Run Track 1 scenarios on the user's Docker host; records only measured results.""" +import argparse +import csv +import json +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +OUT = ROOT / 'evidence' / 'scaling' +BASE_ENV = ROOT / '.env' + + +def run(cmd, env=None, check=True, capture=False): + return subprocess.run(cmd, cwd=ROOT, env=env, check=check, + text=True, stdout=subprocess.PIPE if capture else None, + stderr=subprocess.STDOUT if capture else None) + + +def stats(): + p = run(['docker', 'stats', '--no-stream', '--format', '{{.Name}};{{.CPUPerc}};{{.MemUsage}}'], capture=True) + return p.stdout.strip().splitlines() + + +def env_value(source, key, default): + prefix = key + '=' + for line in source.splitlines(): + if line.startswith(prefix): + return line[len(prefix):].strip().strip('\"').strip("'") + return default + + +def db_activity(env_path, db_user, db_name): + query = "SELECT count(*) FILTER (WHERE state='active'), count(*), current_setting('max_connections') FROM pg_stat_activity" + result = run(['docker', 'compose', '--env-file', str(env_path), 'exec', '-T', 'postgres', + 'psql', '-U', db_user, '-d', db_name, '-At', '-F', ',', '-c', query], + capture=True, check=False) + return result.stdout.strip() if result.returncode == 0 else 'unavailable' + + +def scenario(name, replicas, total_cpu, total_mem, duration, concurrency): + source = BASE_ENV.read_text(encoding='utf-8') + db_user = env_value(source, 'POSTGRES_USER', 'messenger') + db_name = env_value(source, 'POSTGRES_DB', 'messenger') + api_cpu = total_cpu / replicas + api_mem_mb = total_mem // replicas + env_text = source.rstrip() + f'\nAPI_CPU_LIMIT={api_cpu:.3f}\nAPI_MEMORY_LIMIT={api_mem_mb}M\n' + env_path = OUT / ('.env-' + name) + env_path.write_text(env_text, encoding='utf-8') + compose = ['docker', 'compose', '--env-file', str(env_path), 'up', '-d', '--build', '--scale', f'api={replicas}'] + run(compose) + deadline = time.time() + 120 + while time.time() < deadline: + result = run(['docker', 'compose', '--env-file', str(env_path), 'ps', '-q', 'api'], capture=True, check=False) + ids = [line.strip() for line in result.stdout.splitlines() if line.strip()] + healthy = 0 + for container_id in ids: + inspected = run(['docker', 'inspect', '--format', '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}', container_id], capture=True, check=False) + if inspected.returncode == 0 and inspected.stdout.strip() == 'healthy': + healthy += 1 + if len(ids) >= replicas and healthy >= replicas: + break + time.sleep(2) + else: + raise RuntimeError(f'{name}: API replicas did not become healthy') + + output = OUT / (name + '.json') + cmd = [sys.executable, 'tests/load.py', '--base-url', 'http://localhost:8080', + '--duration', str(duration), '--concurrency', str(concurrency), '--output', str(output)] + proc = subprocess.Popen(cmd, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + samples = [] + while proc.poll() is None: + timestamp = time.time() + try: + samples.append({'timestamp_epoch': timestamp, 'containers': stats(), + 'postgres_active_total_max_connections': db_activity(env_path, db_user, db_name)}) + except Exception as exc: + samples.append({'timestamp_epoch': timestamp, 'stats_error': type(exc).__name__}) + time.sleep(1) + stdout = proc.stdout.read() if proc.stdout else '' + if proc.returncode != 0: + raise RuntimeError(f'load failed for {name}: {stdout}') + metrics = json.loads(output.read_text(encoding='utf-8')) + metrics.update({'scenario': name, 'api_replicas': replicas, + 'total_api_cpu_budget': total_cpu, + 'total_api_memory_budget_mb': total_mem, + 'per_replica_cpu_limit': api_cpu, + 'per_replica_memory_limit_mb': api_mem_mb, + 'container_stats_samples': samples}) + output.write_text(json.dumps(metrics, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') + run(['docker', 'compose', '--env-file', str(env_path), 'down']) + return metrics + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--duration', type=int, default=30) + p.add_argument('--concurrency', type=int, default=8) + p.add_argument('--fixed-cpu', type=float, default=2.0) + p.add_argument('--fixed-memory-mb', type=int, default=2048) + p.add_argument('--expanded-cpu', type=float, nargs=3, default=[2.0, 4.0, 8.0]) + p.add_argument('--expanded-memory-mb', type=int, nargs=3, default=[2048, 4096, 8192]) + args = p.parse_args() + if not BASE_ENV.exists(): + p.error('Create .env from .env.example before running') + OUT.mkdir(parents=True, exist_ok=True) + results = [] + for n in (1, 2, 4): + results.append(scenario(f'fixed_{n}replicas', n, args.fixed_cpu, + args.fixed_memory_mb, args.duration, args.concurrency)) + for cpu, mem in zip(args.expanded_cpu, args.expanded_memory_mb): + results.append(scenario(f'expanded_4replicas_{cpu:g}cpu_{mem}mb', 4, cpu, + mem, args.duration, args.concurrency)) + with (OUT / 'summary.csv').open('w', newline='', encoding='utf-8') as f: + fields = ['scenario', 'api_replicas', 'total_api_cpu_budget', 'total_api_memory_budget_mb', + 'successful_rps', 'failed_requests', 'error_ratio', 'p50_ms', 'p95_ms', 'p99_ms'] + writer = csv.DictWriter(f, fieldnames=fields) + writer.writeheader() + for x in results: + lat = x['successful_latency_ms'] + writer.writerow({'scenario': x['scenario'], 'api_replicas': x['api_replicas'], + 'total_api_cpu_budget': x['total_api_cpu_budget'], + 'total_api_memory_budget_mb': x['total_api_memory_budget_mb'], + 'successful_rps': x['successful_rps'], 'failed_requests': x['failed_requests'], + 'error_ratio': x['error_ratio'], 'p50_ms': lat['p50'], 'p95_ms': lat['p95'], 'p99_ms': lat['p99']}) + print(f'Wrote measured results to {OUT}') + +if __name__ == '__main__': + main() diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..e9bb595c --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -eu +: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}" +project="${COMPOSE_PROJECT_NAME:-mtusi-lab3}" +found="" +for id in $(docker ps -q --filter "label=com.docker.compose.project=${project}" --filter "label=com.docker.compose.service=api"); do + hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id") + if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then + found="$id" + break + fi +done +if [ -z "$found" ]; then + echo "No running API container matches TARGET_INSTANCE_ID" >&2 + exit 1 +fi +docker stop --time 3 "$found" >/dev/null diff --git a/scripts/wait-ready.py b/scripts/wait-ready.py new file mode 100644 index 00000000..a9006ecf --- /dev/null +++ b/scripts/wait-ready.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +import sys +import time +import urllib.request + +url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080/health/ready' +timeout = int(sys.argv[2]) if len(sys.argv) > 2 else 30 +deadline = time.monotonic() + timeout +while time.monotonic() < deadline: + try: + with urllib.request.urlopen(url, timeout=2) as response: + if response.status == 200: + raise SystemExit(0) + except Exception: + time.sleep(0.5) +raise SystemExit('Readiness timeout: ' + url)