From 4f85fb96a69601f31fc69d1e4c75f7f467d590a1 Mon Sep 17 00:00:00 2001 From: Sergey Lyakhonov Date: Fri, 30 Oct 2026 17:33:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D1=83=D1=80=D0=BE=D0=B2=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=203:=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B0=D1=82=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20S3,=20=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B8=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=BF=D1=80=D0=B8=20=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=D0=B7=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 5 + compose.yaml | 44 +++++++- evidence/check.txt | 1 + evidence/failover-1.txt | 3 + evidence/failover-2.txt | 3 + evidence/operations.json | 168 +++++++++++++++++++++++++++++++ evidence/operations.txt | 1 + evidence/persistence.txt | 2 + evidence/smoke.txt | 1 + evidence/transfer.md | 7 ++ infra/bucket.sh | 20 ++++ infra/haproxy.cfg | 4 +- infra/s3-policy.json | 1 + migrations/003_attachments.sql | 17 ++++ requirements.lock.txt | 9 ++ requirements.txt | 3 + scripts/actions.py | 22 ++++ scripts/bootstrap.py | 5 + scripts/restart.cmd | 2 + scripts/restart.sh | 3 + scripts/stop-one.cmd | 2 + scripts/stop-one.sh | 3 + src/app.py | 8 +- src/models.py | 25 +++-- src/objects.py | 17 ++++ src/routes/attachments.py | 134 ++++++++++++++++++++++++ src/routes/health.py | 2 + src/routes/messages.py | 6 +- src/storage.py | 30 ++++-- student-tests/test_operations.py | 71 +++++++++++++ 30 files changed, 592 insertions(+), 27 deletions(-) create mode 100644 .gitattributes create mode 100644 evidence/check.txt create mode 100644 evidence/failover-1.txt create mode 100644 evidence/failover-2.txt create mode 100644 evidence/operations.json create mode 100644 evidence/operations.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/smoke.txt create mode 100644 evidence/transfer.md create mode 100644 infra/bucket.sh create mode 100644 infra/s3-policy.json create mode 100644 migrations/003_attachments.sql create mode 100644 scripts/actions.py create mode 100644 scripts/restart.cmd create mode 100644 scripts/restart.sh create mode 100644 scripts/stop-one.cmd create mode 100644 scripts/stop-one.sh create mode 100644 src/objects.py create mode 100644 src/routes/attachments.py create mode 100644 student-tests/test_operations.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..a1c22612 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +* text=auto +*.sh text eol=lf +*.py text eol=lf +*.sql text eol=lf +*.yaml text eol=lf diff --git a/compose.yaml b/compose.yaml index 80859499..d1da6816 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,19 +1,23 @@ -name: lyakhonov-lab3 +name: lyakhonov-lab4 x-api: &api - image: lyakhonov-messenger:lab3 + image: lyakhonov-messenger:lab4 build: . environment: DATABASE_URL_FILE: /run/secrets/database_url CACHE_URL: redis://redis:6379/0 SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} POOL_SIZE: ${POOL_SIZE:-10} + S3_ENDPOINT: http://s3:9000 + S3_BUCKET: attachments + S3_ACCESS_KEY_FILE: /run/secrets/s3_access_key + S3_SECRET_KEY_FILE: /run/secrets/s3_secret_key OPTIMIZED_WRITES: ${OPTIMIZED_WRITES:-1} - secrets: [database_url] + secrets: [database_url, s3_access_key, s3_secret_key] depends_on: migrate: condition: service_completed_successfully cpus: ${API_CPUS:-0.5} - mem_limit: ${API_MEMORY:-256m} + mem_limit: ${API_MEMORY:-512m} stop_grace_period: 12s services: db: @@ -44,13 +48,34 @@ services: condition: service_healthy healthcheck: disable: true + s3: + image: minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + command: [server, /data, --console-address, '127.0.0.1:9001'] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/s3_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_root_password + secrets: [s3_root_user, s3_root_password] + volumes: [s3_data:/data] + cpus: 1 + mem_limit: 768m + bucket-init: + image: minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e + entrypoint: [/bin/sh, /bootstrap/bucket.sh] + secrets: [s3_root_user, s3_root_password, s3_access_key, s3_secret_key] + volumes: [./infra/bucket.sh:/bootstrap/bucket.sh:ro, ./infra/s3-policy.json:/bootstrap/policy.json:ro] + depends_on: [s3] api: <<: *api + depends_on: + migrate: + condition: service_completed_successfully + bucket-init: + condition: service_completed_successfully deploy: replicas: ${API_REPLICAS:-2} lb: image: haproxy:3.0.6-alpine - ports: ["127.0.0.1:8080:8080"] + ports: ["127.0.0.1:${LAB4_PORT:-8084}:8080"] volumes: [./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro] depends_on: [api] cpus: 0.5 @@ -62,9 +87,18 @@ services: timeout: 2s retries: 3 secrets: + s3_root_user: + file: ./secrets/s3_root_user + s3_root_password: + file: ./secrets/s3_root_password + s3_access_key: + file: ./secrets/s3_access_key + s3_secret_key: + file: ./secrets/s3_secret_key db_password: file: ./secrets/db_password database_url: file: ./secrets/database_url volumes: postgres_data: + s3_data: diff --git a/evidence/check.txt b/evidence/check.txt new file mode 100644 index 00000000..b62af9c5 --- /dev/null +++ b/evidence/check.txt @@ -0,0 +1 @@ +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. diff --git a/evidence/failover-1.txt b/evidence/failover-1.txt new file mode 100644 index 00000000..8c478f30 --- /dev/null +++ b/evidence/failover-1.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 109, "failed_attempts": 2, "first_recovery_seconds": 2.439} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/failover-2.txt b/evidence/failover-2.txt new file mode 100644 index 00000000..180e5083 --- /dev/null +++ b/evidence/failover-2.txt @@ -0,0 +1,3 @@ +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 109, "failed_attempts": 3, "first_recovery_seconds": 2.442} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/operations.json b/evidence/operations.json new file mode 100644 index 00000000..aa22df4c --- /dev/null +++ b/evidence/operations.json @@ -0,0 +1,168 @@ +{ + "result": "PASS", + "each_uploading_replica_stopped": [ + { + "stopped": { + "id": "24498cc4891a", + "hostname": "24498cc4891a", + "name": "lyakhonov-lab4-api-1", + "running": true, + "started_at": "2026-10-30T14:31:07.744742532Z", + "project": "lyakhonov-lab4" + }, + "after": { + "id": "24498cc4891a", + "hostname": "24498cc4891a", + "name": "lyakhonov-lab4-api-1", + "running": false, + "started_at": "2026-10-30T14:31:07.744742532Z", + "project": "lyakhonov-lab4" + }, + "download_instance": "950e5ae78851-f8e15971", + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "recovery_seconds": 1.135 + }, + { + "stopped": { + "id": "950e5ae78851", + "hostname": "950e5ae78851", + "name": "lyakhonov-lab4-api-2", + "running": true, + "started_at": "2026-10-30T14:31:12.431277515Z", + "project": "lyakhonov-lab4" + }, + "after": { + "id": "950e5ae78851", + "hostname": "950e5ae78851", + "name": "lyakhonov-lab4-api-2", + "running": false, + "started_at": "2026-10-30T14:31:12.431277515Z", + "project": "lyakhonov-lab4" + }, + "download_instance": "24498cc4891a-5564fcf3", + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "recovery_seconds": 1.096 + } + ], + "s3_503_seconds": 4.046069000149146, + "restart_before": { + "api": [ + { + "id": "24498cc4891a", + "hostname": "24498cc4891a", + "name": "lyakhonov-lab4-api-1", + "running": true, + "started_at": "2026-10-30T14:31:55.001303564Z", + "project": "lyakhonov-lab4" + }, + { + "id": "950e5ae78851", + "hostname": "950e5ae78851", + "name": "lyakhonov-lab4-api-2", + "running": true, + "started_at": "2026-10-30T14:31:59.649868974Z", + "project": "lyakhonov-lab4" + } + ], + "db": [ + { + "id": "10138b5c0bee", + "hostname": "10138b5c0bee", + "name": "lyakhonov-lab4-db-1", + "running": true, + "started_at": "2026-10-30T14:30:00.149637002Z", + "project": "lyakhonov-lab4" + } + ], + "redis": [ + { + "id": "68997bbba33b", + "hostname": "68997bbba33b", + "name": "lyakhonov-lab4-redis-1", + "running": true, + "started_at": "2026-10-30T14:30:00.082955484Z", + "project": "lyakhonov-lab4" + } + ], + "s3": [ + { + "id": "83df7acfb73f", + "hostname": "83df7acfb73f", + "name": "lyakhonov-lab4-s3-1", + "running": true, + "started_at": "2026-10-30T14:32:07.860463524Z", + "project": "lyakhonov-lab4" + } + ], + "lb": [ + { + "id": "e9682a892ad3", + "hostname": "e9682a892ad3", + "name": "lyakhonov-lab4-lb-1", + "running": true, + "started_at": "2026-10-30T14:29:59.993685266Z", + "project": "lyakhonov-lab4" + } + ] + }, + "restart_after": { + "api": [ + { + "id": "24498cc4891a", + "hostname": "24498cc4891a", + "name": "lyakhonov-lab4-api-1", + "running": true, + "started_at": "2026-10-30T14:32:12.97023467Z", + "project": "lyakhonov-lab4" + }, + { + "id": "950e5ae78851", + "hostname": "950e5ae78851", + "name": "lyakhonov-lab4-api-2", + "running": true, + "started_at": "2026-10-30T14:32:13.834829902Z", + "project": "lyakhonov-lab4" + } + ], + "db": [ + { + "id": "10138b5c0bee", + "hostname": "10138b5c0bee", + "name": "lyakhonov-lab4-db-1", + "running": true, + "started_at": "2026-10-30T14:32:12.605184626Z", + "project": "lyakhonov-lab4" + } + ], + "redis": [ + { + "id": "68997bbba33b", + "hostname": "68997bbba33b", + "name": "lyakhonov-lab4-redis-1", + "running": true, + "started_at": "2026-10-30T14:32:12.532082166Z", + "project": "lyakhonov-lab4" + } + ], + "s3": [ + { + "id": "83df7acfb73f", + "hostname": "83df7acfb73f", + "name": "lyakhonov-lab4-s3-1", + "running": true, + "started_at": "2026-10-30T14:32:12.440778626Z", + "project": "lyakhonov-lab4" + } + ], + "lb": [ + { + "id": "e9682a892ad3", + "hostname": "e9682a892ad3", + "name": "lyakhonov-lab4-lb-1", + "running": true, + "started_at": "2026-10-30T14:32:13.55378444Z", + "project": "lyakhonov-lab4" + } + ] + } +} \ No newline at end of file diff --git a/evidence/operations.txt b/evidence/operations.txt new file mode 100644 index 00000000..75c1eb2c --- /dev/null +++ b/evidence/operations.txt @@ -0,0 +1 @@ +PASS: both uploading API replicas stopped; S3 failure/recovery; all services really restarted; original and session preserved diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..749fd29b --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,2 @@ +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..c554fd6f --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1 @@ +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/transfer.md b/evidence/transfer.md new file mode 100644 index 00000000..33627c7d --- /dev/null +++ b/evidence/transfer.md @@ -0,0 +1,7 @@ +# Перенос базы +Ляхонов Сергей, БВТ2301. Выбран трек 2 лабораторной 4. +Источник: собственная лабораторная 3, 374c43e, только чтение; оба рабочих дерева были чистыми. +Перенесены src, migrations, Dockerfile, зависимости и необходимые конфигурации/bootstrap/readiness. +Задание, README, COURSE, contracts и публичные tests лабораторной 4 сохранены. +Секреты, .env, Git, тома, старые измерения и виртуальное окружение не переносились. +Новый стенд: Compose project lyakhonov-lab4, порт 127.0.0.1:8084, отдельные тома/сеть/образ. diff --git a/infra/bucket.sh b/infra/bucket.sh new file mode 100644 index 00000000..295b0271 --- /dev/null +++ b/infra/bucket.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -eu + +# Не включать set -x: аргументы содержат учебные credentials. + +until mc alias set local http://s3:9000 "$(cat /run/secrets/s3_root_user)" "$(cat /run/secrets/s3_root_password)" >/dev/null 2>&1; do sleep 1; done + +mc mb --ignore-existing local/attachments >/dev/null + +mc anonymous set none local/attachments >/dev/null + +mc admin user add local "$(cat /run/secrets/s3_access_key)" "$(cat /run/secrets/s3_secret_key)" >/dev/null + +mc admin policy create local attachments-app /bootstrap/policy.json >/dev/null + +mc admin policy attach local attachments-app --user "$(cat /run/secrets/s3_access_key)" >/dev/null + +echo 'Private bucket and restricted application user ready' + diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg index 0981249f..042a6cb2 100644 --- a/infra/haproxy.cfg +++ b/infra/haproxy.cfg @@ -5,8 +5,8 @@ defaults mode http log global timeout connect 1s - timeout client 10s - timeout server 5s + timeout client 60s + timeout server 45s timeout check 1s retries 0 resolvers docker diff --git a/infra/s3-policy.json b/infra/s3-policy.json new file mode 100644 index 00000000..6b7d394e --- /dev/null +++ b/infra/s3-policy.json @@ -0,0 +1 @@ +{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::attachments"],"Condition":{"StringLike":{"s3:prefix":["originals/*"]}}},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::attachments/originals/*"]}]} \ No newline at end of file diff --git a/migrations/003_attachments.sql b/migrations/003_attachments.sql new file mode 100644 index 00000000..9bdc747c --- /dev/null +++ b/migrations/003_attachments.sql @@ -0,0 +1,17 @@ +ALTER TABLE messages DROP CONSTRAINT messages_text_check; +ALTER TABLE messages ADD CONSTRAINT messages_text_limit CHECK (length(text)<=4000); +CREATE TABLE attachments ( + id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL REFERENCES users(id), object_key text NOT NULL UNIQUE, + filename text NOT NULL, content_type text NOT NULL, size_bytes integer NOT NULL, + sha256 char(64) NOT NULL, state text NOT NULL CHECK(state IN ('ready','missing')), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id) +); +CREATE TABLE message_attachments ( + message_id uuid NOT NULL REFERENCES messages(id), attachment_id uuid NOT NULL REFERENCES attachments(id), + position integer NOT NULL, PRIMARY KEY(message_id,attachment_id), UNIQUE(message_id,position) +); +-- Старые сохранённые ключи теперь сравнивают полное нормализованное тело. +UPDATE message_requests SET body=json_build_object('text',body,'attachment_ids',json_build_array())::text; diff --git a/requirements.lock.txt b/requirements.lock.txt index ceec59ee..6fccf885 100644 --- a/requirements.lock.txt +++ b/requirements.lock.txt @@ -23,3 +23,12 @@ uvloop==0.23.0 watchfiles==1.3.0 websockets==17.2 redis==5.2.0 + +boto3==1.40.61 +Pillow==12.0.0 +botocore==1.40.76 +s3transfer==0.14.0 +jmespath==1.1.0 +python-dateutil==2.9.0.post0 +six==1.17.0 +urllib3==2.8.0 diff --git a/requirements.txt b/requirements.txt index 1b6204da..7010e5f2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,6 @@ psycopg[binary]==3.2.3 psycopg-pool==3.2.4 argon2-cffi==23.1.0 redis==5.2.0 + +boto3==1.40.61 +Pillow==12.0.0 diff --git a/scripts/actions.py b/scripts/actions.py new file mode 100644 index 00000000..4e2f15c4 --- /dev/null +++ b/scripts/actions.py @@ -0,0 +1,22 @@ +"""Restart/failover hooks scoped to lab4; never remove volumes.""" +import json +import os +from pathlib import Path +import subprocess +import sys +root=Path(__file__).resolve().parents[1] +base=['docker','compose','--project-directory',str(root),'-p','lyakhonov-lab4'] +def compose(*args): + return subprocess.check_output(base+list(args),text=True).strip() +mode=sys.argv[1] +if mode=='restart': + compose('restart','api','db','redis','s3','lb') +elif mode=='stop-one': + target=os.environ['TARGET_INSTANCE_ID'] + for cid in compose('ps','-q','api').splitlines(): + info=json.loads(subprocess.check_output(['docker','inspect',cid],text=True))[0] + if target.startswith(info['Config']['Hostname']+'-'): + subprocess.check_call(['docker','stop','--time','1',cid],stdout=subprocess.DEVNULL) + break + else: raise SystemExit('No matching lab4 API instance') +else: raise SystemExit('unknown action') diff --git a/scripts/bootstrap.py b/scripts/bootstrap.py index e1fd52c4..4824cfcd 100644 --- a/scripts/bootstrap.py +++ b/scripts/bootstrap.py @@ -13,3 +13,8 @@ if not url_file.exists(): password = password_file.read_text(encoding='utf-8').strip() url_file.write_text(f'postgresql://messenger:{password}@db:5432/messenger', encoding='utf-8') print('Локальные секреты готовы; значения не выводятся.') + +for name in ('s3_root_user', 's3_root_password', 's3_access_key', 's3_secret_key'): + target = folder / name + if not target.exists(): + target.write_text(secrets.token_hex(24), encoding='utf-8') diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..7bc66929 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,2 @@ +@echo off +python "%~dp0actions.py" restart diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..c22c1f7f --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/actions.py" restart diff --git a/scripts/stop-one.cmd b/scripts/stop-one.cmd new file mode 100644 index 00000000..c757e172 --- /dev/null +++ b/scripts/stop-one.cmd @@ -0,0 +1,2 @@ +@echo off +python "%~dp0actions.py" stop-one diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..04aa2950 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/actions.py" stop-one diff --git a/src/app.py b/src/app.py index 1b02146e..131bc654 100644 --- a/src/app.py +++ b/src/app.py @@ -13,7 +13,8 @@ from pydantic import ValidationError as PydanticValidationError from starlette.exceptions import HTTPException as StarletteHTTPException from src.models import ErrorResponse -from src.routes import chats, health, messages, users +from src.routes import chats, health, messages, users, attachments +from botocore.exceptions import BotoCoreError, ClientError from src.storage import storage @@ -28,8 +29,10 @@ def create_app() -> FastAPI: await run_in_threadpool(storage.pool.close) instance_id = os.getenv("INSTANCE_ID", socket.gethostname()) + "-" + uuid.uuid4().hex[:8] - app = FastAPI(title="MTUSI Messenger Lab3", version="1.3.0", lifespan=lifespan) + app = FastAPI(title="MTUSI Messenger Lab4", version="1.4.0", lifespan=lifespan) + @app.exception_handler(BotoCoreError) + @app.exception_handler(ClientError) @app.exception_handler(psycopg.OperationalError) @app.exception_handler(psycopg.errors.QueryCanceled) @app.exception_handler(psycopg.errors.LockNotAvailable) @@ -154,6 +157,7 @@ def create_app() -> FastAPI: app.include_router(users.router) app.include_router(chats.router) app.include_router(messages.router) + app.include_router(attachments.router) return app diff --git a/src/models.py b/src/models.py index e0ec1ac2..dc507cce 100644 --- a/src/models.py +++ b/src/models.py @@ -2,7 +2,7 @@ from datetime import datetime, timezone from typing import Optional import uuid -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator def utc_now() -> str: @@ -89,22 +89,29 @@ class Message(BaseModel): sender_id: str text: str created_at: str + attachment_ids: list[str] = Field(default_factory=list) class CreateMessage(BaseModel): """Данные для отправки сообщения.""" model_config = ConfigDict(extra="forbid", strict=True) - text: str = Field(..., min_length=1, max_length=4000) - attachment_ids: list[str] = Field(default_factory=list, max_length=0) + text: str = Field(..., max_length=4000) + attachment_ids: list[str] = Field(default_factory=list, max_length=10) - @field_validator("text") + @field_validator('attachment_ids') @classmethod - def text_not_whitespace_only(cls, v: str) -> str: - """Запрещаем текст, состоящий только из пробельных символов.""" - if v.strip() == "": - raise ValueError("текст не может состоять только из пробелов") - return v + def valid_attachments(cls, values): + ids = [str(uuid.UUID(v)) for v in values] + if len(set(ids)) != len(ids): + raise ValueError('duplicate attachment_id') + return ids + + @model_validator(mode='after') + def valid_content(self): + if not self.text.strip() and not self.attachment_ids: + raise ValueError('empty message') + return self class ChatPage(BaseModel): diff --git a/src/objects.py b/src/objects.py new file mode 100644 index 00000000..ef7f82ee --- /dev/null +++ b/src/objects.py @@ -0,0 +1,17 @@ +"""S3 operations with bounded timeouts and immutable server-generated keys.""" +import boto3 +from botocore.config import Config +from src.config import setting + +def client(): + return boto3.client('s3', endpoint_url=setting('S3_ENDPOINT','http://s3:9000'), + aws_access_key_id=setting('S3_ACCESS_KEY'), aws_secret_access_key=setting('S3_SECRET_KEY'), + region_name='us-east-1', config=Config(connect_timeout=2,read_timeout=5, + retries={'total_max_attempts':1},s3={'addressing_style':'path'})) + +BUCKET = setting('S3_BUCKET','attachments') + +def object_lock(conn, key, wait=True): + fn = 'pg_advisory_xact_lock' if wait else 'pg_try_advisory_xact_lock' + row = conn.execute('SELECT '+fn+'(hashtextextended(%s,42)) AS locked',(key,)).fetchone() + return wait or row['locked'] diff --git a/src/routes/attachments.py b/src/routes/attachments.py new file mode 100644 index 00000000..3461724e --- /dev/null +++ b/src/routes/attachments.py @@ -0,0 +1,134 @@ +"""Bounded synchronous image uploads; one decode per process.""" +import asyncio +import hashlib +import tempfile +import unicodedata +import uuid +import warnings +from PIL import Image, UnidentifiedImageError +from fastapi import APIRouter, Depends, HTTPException, Request +from starlette.concurrency import run_in_threadpool +from starlette.responses import StreamingResponse +from src.routes.common import require_user, make_error, get_request_id +from src.storage import storage, timestamp +from src.objects import client, BUCKET, object_lock + +router = APIRouter() +slots = asyncio.Semaphore(1) +MAX_BYTES=10*1024*1024 +Image.MAX_IMAGE_PIXELS=40_000_000 + +class InvalidImage(Exception): + def __init__(self,status): + self.status=status + +def error(request, status, code, message): + return HTTPException(status, detail=make_error(code,message,get_request_id(request)).model_dump()) + +def inspect_image(stream, declared): + try: + with warnings.catch_warnings(): + warnings.simplefilter('error', Image.DecompressionBombWarning) + with Image.open(stream) as image: + if image.width*image.height>40_000_000: + raise InvalidImage(413) + actual={'PNG':'image/png','JPEG':'image/jpeg'}.get(image.format) + if not actual or actual != declared: + raise InvalidImage(415) + image.verify() + stream.seek(0) + with Image.open(stream) as image: + image.load() + except (Image.DecompressionBombError,Image.DecompressionBombWarning): + raise InvalidImage(413) from None + except (UnidentifiedImageError,OSError,SyntaxError,ValueError): + raise InvalidImage(415) from None + finally: + stream.seek(0) + +def metadata(row): + return {k: str(row[k]) for k in ('id','chat_id','owner_id')} | { + k:row[k] for k in ('filename','content_type','size_bytes','sha256')} | { + 'status':'ready','variants':[],'error_code':None, + 'created_at':timestamp(row['created_at']),'updated_at':timestamp(row['updated_at'])} + +def save(stream, chat_id, owner, filename, content_type, size, digest): + attachment_id=uuid.uuid4() + key='originals/'+str(attachment_id) + # Keep this lock through S3 PUT and DB COMMIT/ROLLBACK. Reconciler shares it. + with storage.pool.connection() as conn: + object_lock(conn,key) + client().put_object(Bucket=BUCKET,Key=key,Body=stream,ContentLength=size, + ContentType=content_type,Metadata={'sha256':digest},IfNoneMatch='*') + row=conn.execute("INSERT INTO attachments(id,chat_id,owner_id,object_key,filename,content_type,size_bytes,sha256,state) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,'ready') RETURNING *", + (attachment_id,chat_id,owner,key,filename,content_type,size,digest)).fetchone() + # Never blindly delete after ambiguous COMMIT: grace-period reconciliation handles it. + return metadata(row) + +@router.post('/api/v1/chats/{chat_id}/attachments',status_code=201) +async def upload(request:Request,chat_id:uuid.UUID,filename:str='',actor:str=Depends(require_user)): + if not await run_in_threadpool(storage.is_chat_member,str(chat_id),actor): + raise error(request,404,'not_found','chat not found') + if not 1<=len(filename)<=255 or any(c in '/\\' or unicodedata.category(c)=='Cc' for c in filename): + raise error(request,400,'invalid_request','invalid filename') + mime=request.headers.get('content-type','').lower() + if mime not in ('image/png','image/jpeg'): + raise error(request,415,'unsupported_media_type','PNG/JPEG required') + length=request.headers.get('content-length') + if length is not None: + if not length.isdecimal(): + raise error(request,400,'invalid_request','invalid length') + if int(length)>MAX_BYTES: + raise error(request,413,'payload_too_large','limit 10 MiB') + try: + await asyncio.wait_for(slots.acquire(),timeout=25) + except asyncio.TimeoutError: + raise error(request,503,'unavailable','upload queue busy') from None + try: + with tempfile.TemporaryFile() as stream: + size=0; digest=hashlib.sha256() + async def receive(): + nonlocal size + async for chunk in request.stream(): + size+=len(chunk) + if size>MAX_BYTES: + raise error(request,413,'payload_too_large','limit 10 MiB') + digest.update(chunk) + await run_in_threadpool(stream.write,chunk) + try: + await asyncio.wait_for(receive(),timeout=20) + await run_in_threadpool(stream.seek,0) + await run_in_threadpool(inspect_image,stream,mime) + except asyncio.TimeoutError: + raise error(request,400,'invalid_request','upload timed out') from None + except InvalidImage as exc: + raise error(request,exc.status,'payload_too_large' if exc.status==413 else 'unsupported_media_type','invalid image') from None + return await run_in_threadpool(save,stream,str(chat_id),actor,filename,mime,size,digest.hexdigest()) + finally: + slots.release() + +def accessible(request,attachment_id,actor): + with storage.pool.connection() as conn: + row=conn.execute('SELECT a.* FROM attachments a JOIN chat_members m ON a.chat_id=m.chat_id WHERE a.id=%s AND m.user_id=%s',(attachment_id,actor)).fetchone() + if not row: + raise error(request,404,'not_found','attachment not found') + if row['state']!='ready': + raise error(request,503,'unavailable','original temporarily unavailable') + return row + +@router.get('/api/v1/attachments/{attachment_id}') +def get_attachment(request:Request,attachment_id:uuid.UUID,actor:str=Depends(require_user)): + return metadata(accessible(request,attachment_id,actor)) + +@router.get('/api/v1/attachments/{attachment_id}/content') +def content(request:Request,attachment_id:uuid.UUID,variant:str='original',actor:str=Depends(require_user)): + row=accessible(request,attachment_id,actor) + if variant!='original': + raise error(request,404,'not_found','variant not found') + obj=client().get_object(Bucket=BUCKET,Key=row['object_key']) + def chunks(): + try: + yield from obj['Body'].iter_chunks(chunk_size=64*1024) + finally: + obj['Body'].close() + return StreamingResponse(chunks(),media_type=row['content_type'],headers={'Content-Length':str(row['size_bytes'])}) diff --git a/src/routes/health.py b/src/routes/health.py index 2f2caafb..0e9b4b94 100644 --- a/src/routes/health.py +++ b/src/routes/health.py @@ -13,6 +13,8 @@ def liveness(): @router.get('/health/ready') def readiness(): storage.ready() + from src.objects import client, BUCKET + client().list_objects_v2(Bucket=BUCKET,Prefix="originals/",MaxKeys=1) return Health() diff --git a/src/routes/messages.py b/src/routes/messages.py index 1f8ea128..1045e5dc 100644 --- a/src/routes/messages.py +++ b/src/routes/messages.py @@ -6,7 +6,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request from src.models import CreateMessage, Message, MessagePage from src.routes.common import get_request_id, make_error, require_user -from src.storage import ConflictError, LimitExceeded, ValidationError, storage +from src.storage import NotFoundError, ConflictError, LimitExceeded, ValidationError, storage router = APIRouter() @@ -37,8 +37,10 @@ def create_message( key = request.headers.get('Idempotency-Key', '') if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', key): raise HTTPException(400, detail=make_error('invalid_request', 'неверный Idempotency-Key', request_id).model_dump()) - msg = storage.create_message(chat_id_str, x_user_id, body.text, key) + msg = storage.create_message(chat_id_str, x_user_id, body.text, key, body.attachment_ids) return msg + except NotFoundError as e: + raise HTTPException(404, detail=make_error('not_found', str(e), request_id).model_dump()) except ConflictError as e: raise HTTPException(409, detail=make_error('conflict', str(e), request_id).model_dump()) except LimitExceeded as e: diff --git a/src/storage.py b/src/storage.py index 358184ec..995a4cb1 100644 --- a/src/storage.py +++ b/src/storage.py @@ -199,25 +199,41 @@ class PostgresStorage: next_cursor = self._encode_cursor('chats',user_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None return page,next_cursor - def create_message(self, chat_id, sender_id, text, key): + def create_message(self, chat_id, sender_id, text, key, attachment_ids=None): + attachment_ids = attachment_ids or [] + normalized = {'text': text, 'attachment_ids': attachment_ids} # Блокировка сериализует один ключ на всех репликах; результат и сообщение атомарны. with self.pool.connection() as conn: scope = json.dumps([sender_id, chat_id, key]) conn.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', (scope,)) old = conn.execute('SELECT * FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s AND expires_at>clock_timestamp()', (sender_id,chat_id,key)).fetchone() if old: - if old['body'] != text: + if json.loads(old['body']) != normalized: raise ConflictError('ключ уже использован с другим сообщением') row = conn.execute('SELECT * FROM messages WHERE id=%s', (old['message_id'],)).fetchone() - return Message(**public_row(row)) - optimized = os.getenv('OPTIMIZED_WRITES','1') == '1' + return self.message_model(conn, row) + for attachment_id in sorted(attachment_ids): + attachment = conn.execute('SELECT state FROM attachments WHERE id=%s AND chat_id=%s FOR SHARE', (attachment_id,chat_id)).fetchone() + if not attachment: + raise NotFoundError('вложение не найдено') + if attachment['state'] != 'ready': + raise ConflictError('вложение недоступно') + optimized = os.getenv('OPTIMIZED_WRITES','1') == '1' if not optimized: conn.execute('DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s', (sender_id,chat_id,key)) row = conn.execute('INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *', (uuid.uuid4(),chat_id,sender_id,text)).fetchone() # Под той же блокировкой UPSERT заменяет просроченный результат без отдельного DELETE. suffix = ' ON CONFLICT (user_id,chat_id,key) DO UPDATE SET body=EXCLUDED.body,message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at' if optimized else '' - conn.execute("INSERT INTO message_requests VALUES (%s,%s,%s,%s,%s,clock_timestamp()+interval '24 hours')"+suffix, (sender_id,chat_id,key,text,row['id'])) - return Message(**public_row(row)) + conn.execute("INSERT INTO message_requests VALUES (%s,%s,%s,%s,%s,clock_timestamp()+interval '24 hours')"+suffix, (sender_id,chat_id,key,json.dumps(normalized,ensure_ascii=False),row['id'])) + for position, attachment_id in enumerate(attachment_ids): + conn.execute('INSERT INTO message_attachments VALUES (%s,%s,%s)', (row['id'],attachment_id,position)) + result = self.message_model(conn,row) + return result + + @staticmethod + def message_model(conn, row): + ids = conn.execute('SELECT attachment_id FROM message_attachments WHERE message_id=%s ORDER BY position', (row['id'],)).fetchall() + return Message(**public_row(row), attachment_ids=[str(x['attachment_id']) for x in ids]) def list_messages(self, chat_id, cursor, limit): with self.pool.connection() as conn: @@ -231,7 +247,7 @@ class PostgresStorage: boundary = ' AND (created_at,id) > (%s,%s)' params += [when,item] rows = conn.execute('SELECT * FROM messages WHERE chat_id=%s' + boundary + ' ORDER BY created_at,id LIMIT %s', params+[limit+1]).fetchall() - page = [Message(**public_row(row)) for row in rows[:limit]] + page = [self.message_model(conn,row) for row in rows[:limit]] next_cursor = self._encode_cursor('messages',chat_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None return page,next_cursor diff --git a/student-tests/test_operations.py b/student-tests/test_operations.py new file mode 100644 index 00000000..c0b03814 --- /dev/null +++ b/student-tests/test_operations.py @@ -0,0 +1,71 @@ +"""Actual Docker failures, scoped by Compose service labels.""" +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import time +sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) +from client import Client, check, Failure +ROOT=Path(__file__).resolve().parents[1] +BASE=['docker','compose','--project-directory',str(ROOT),'-p','lyakhonov-lab4'] +def dc(*args): return subprocess.check_output(BASE+list(args),text=True).strip() +def info(cid): + x=json.loads(subprocess.check_output(['docker','inspect',cid],text=True))[0] + return {'id':x['Id'][:12],'hostname':x['Config']['Hostname'],'name':x['Name'].lstrip('/'),'running':x['State']['Running'],'started_at':x['State']['StartedAt'],'project':x['Config']['Labels']['com.docker.compose.project']} +def wait(c): + for _ in range(120): + try: c.call('GET','/health/ready'); return + except Failure: time.sleep(.5) + raise AssertionError('readiness did not recover') +def main(): + c=Client('http://localhost:8084',timeout=8) + alice,bob,eve,chat=c.bootstrap() + data=(ROOT/'tests/fixtures/sample.png').read_bytes() + records=[] + ids=dc('ps','-q','api').splitlines() + for cid in ids: + target=info(cid) + check(target['project']=='lyakhonov-lab4','wrong project') + for _ in range(20): + result=c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=failover.png',201,actor=alice,raw=data,headers={'Content-Type':'image/png'},schema='Attachment') + if result['headers']['X-Instance-Id'].startswith(target['hostname']+'-'): break + else: raise AssertionError('target never uploaded') + started=time.monotonic() + try: + subprocess.check_call(['docker','stop','--time','1',cid],stdout=subprocess.DEVNULL) + check(not info(cid)['running'],'target still running') + for _ in range(40): + try: + downloaded=c.call('GET','/api/v1/attachments/'+result['json']['id']+'/content',actor=bob) + break + except Failure: time.sleep(.25) + else: raise AssertionError('download unavailable') + check(downloaded['bytes']==data,'original changed') + check(not downloaded['headers']['X-Instance-Id'].startswith(target['hostname']+'-'),'same replica') + records.append({'stopped':target,'after':info(cid),'download_instance':downloaded['headers']['X-Instance-Id'],'sha256':hashlib.sha256(downloaded['bytes']).hexdigest(),'recovery_seconds':round(time.monotonic()-started,3)}) + finally: + subprocess.check_call(['docker','start',cid],stdout=subprocess.DEVNULL) + wait(c); time.sleep(3) + attachment,_=c.attachment(alice,chat) + # S3 failure must bound the wait and preserve the existing DB/session. + try: + dc('stop','--timeout','1','s3') + started=time.monotonic() + c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',503,actor=bob) + failure_seconds=time.monotonic()-started + finally: + dc('start','s3'); wait(c) + check(c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',actor=bob)['bytes']==data,'S3 recovery lost original') + before={service:[info(x) for x in dc('ps','-q',service).splitlines()] for service in ('api','db','redis','s3','lb')} + dc('restart','api','db','redis','s3','lb'); wait(c) + after={service:[info(x) for x in dc('ps','-q',service).splitlines()] for service in before} + for service in before: + check(all(a['started_at']!=b['started_at'] for a,b in zip(before[service],after[service])),'service not restarted: '+service) + check(c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',actor=bob)['bytes']==data,'restart lost original') + c.call('GET','/api/v1/users/me',actor=alice,schema='User') + out={'result':'PASS','each_uploading_replica_stopped':records,'s3_503_seconds':failure_seconds,'restart_before':before,'restart_after':after} + (ROOT/'evidence/operations.json').write_text(json.dumps(out,indent=2),encoding='utf-8') + print('PASS: both uploading API replicas stopped; S3 failure/recovery; all services really restarted; original and session preserved') +if __name__=='__main__': main()