Record level 3 evidence and fill in the report

Evidence comes from the stand started from an empty state and from a
fresh clone: the public smoke, the contract checks, lint and 325 own
tests, the storage layout with an out-of-API comparison of a stored
object against its metadata, failover of each replica under writes, the
upload failover scenario, persistence with StartedAt of every service
including S3, the S3 outage scenario and 22 S3 privacy and bootstrap
checks. The storage evidence script now covers the attachment tables
and the S3 secrets.

REPORT.md follows the lab 4 template: launch from a clean clone,
architecture with the source of truth for each fact, the write order
between PostgreSQL and S3 and what each failure leaves behind, where
each upload limit applies, roles and services, the .env.example
mapping, grade 3 rows and the grade 3 failure table. The design notes
the botocore Expect header that hung uploads behind the port proxy.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 22:20:10 +03:00
co-authored by Claude Opus 5.5
parent 24f9d6195b
commit db901d6401
21 changed files with 1004 additions and 25 deletions
+43
View File
@@ -0,0 +1,43 @@
date: 2026-10-28T17:50:32Z commit: 24f9d61
$ git clone … && git checkout solution/bpi2303-kostylev
24f9d61 Add the attachment failure scenarios and S3 checks
$ make secrets
sh scripts/secrets.sh
создан secrets/postgres_password
создан secrets/database_url
создан secrets/cursor_secret
создан secrets/valkey_password
создан secrets/valkey.conf
создан secrets/cache_url
создан secrets/s3_root_user
создан secrets/s3_root_password
создан secrets/s3_access_key_id
создан secrets/s3_secret_access_key
создан .env из .env.example
$ sed LB_HTTP_PORT=18084 LB_STATS_PORT=24084 .env # 8080 на этой машине занят стендами других лаб
$ docker compose --env-file .env config --quiet && echo valid
valid
$ make up # docker compose up -d --build --wait
Container mtusi2026lab4-api-1 Healthy
Container mtusi2026lab4-api-2 Healthy
Container mtusi2026lab4-lb-1 Healthy
Container mtusi2026lab4-migrate-1 Exited
Container mtusi2026lab4-postgres-1 Healthy
Container mtusi2026lab4-s3-1 Healthy
Container mtusi2026lab4-s3-init-1 Exited
Container mtusi2026lab4-valkey-1 Healthy
up --wait: 18 s
$ docker compose ps -a --format '{{.Service}} {{.Status}}'
api Up 3 seconds (healthy)
api Up 3 seconds (healthy)
lb Up 10 seconds (healthy)
migrate Exited (0) 4 seconds ago
postgres Up 10 seconds (healthy)
s3 Up 10 seconds (healthy)
s3-init Exited (0) 4 seconds ago
valkey Up 10 seconds (healthy)
$ make test BASE_URL=http://localhost:18084
python3 tests/smoke.py --base-url "http://localhost:18084"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
$ docker compose down -v # стенд из clone удалён вместе с учебными данными
Network mtusi2026lab4_backend Removed
+7
View File
@@ -0,0 +1,7 @@
date: 2026-10-28T17:51:36Z commit: 24f9d61
$ make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ make validate PYTHON="uv run --no-project --with-requirements requirements-dev.txt python" # venv с requirements-dev.txt
uv run -q --no-project --with-requirements requirements-dev.txt python scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
+42
View File
@@ -0,0 +1,42 @@
date: 2026-10-28T18:08:00Z commit: 24f9d61
host: Apple M4 Max, 16 CPU, 48 GiB RAM, macOS 15.1
docker: 28.3.2, VM: 16 CPU, 8217190400 bytes
compose: 2.38.2-desktop.1
images:
image: ${API_IMAGE:-mtusi-messenger:lab4}
image: rustfs/rc:v0.1.36@sha256:ab024bfebee49a750ce886b4c70963ccd9ddaa03f491704a90710641d7a26699
image: haproxy:3.2.26-alpine@sha256:5893840576071e6217cd52ebdcc14f454c6f7a631056fa6f4e0f40d3cac4215b
image: postgres:17.11-alpine@sha256:266d3c257594e195b309ae9d1805e54c788f1d7b375f56bf6c367aac6c917e7b
image: valkey/valkey:8.1.11-alpine@sha256:2602ace2be6569bc2bbb06f873776516ab9089821525be34f4305c67eb9db02a
image: rustfs/rustfs:1.0.4@sha256:ed9210d6411d77ffceb70d94b964a3cb7d225983368bc25bebe01e995293dcc2
image: grafana/k6:2.3.0@sha256:2dd4756ec6e14e62778916e1eb850108e4b499304c02df56760fa0bdb0fba0b8
FROM python:3.14-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
COPY --from=ghcr.io/astral-sh/uv:0.9.7@sha256:ba4857bf2a068e9bc0e64eed8563b065908a4cd6bfb66b531a9c424c8e25e142 /uv /usr/local/bin/uv
FROM python:3.14-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS runtime
python in image: Python 3.14.7
haproxy: HAProxy version 3.2.26-3e0f11a 2026/10/14 - https://haproxy.org/
rustfs: rustfs 1.0.4
packages (uv.lock):
fastapi 0.141.1
uvicorn 0.54.0
uvicorn-worker 0.4.0
gunicorn 26.2.0
asyncpg 0.32.0
argon2-cffi 25.1.0
pydantic 2.13.5
pydantic-settings 2.15.0
dishka 1.10.1
structlog 26.1.0
redis 8.1.0
aiobotocore 3.10.0
botocore 1.43.117
aiohttp 3.14.5
pillow 13.0.0
process users (service uid command):
api 10001 python
lb 99 haproxy
postgres 70 pg_isready
postgres 70 postgres
s3 10001 rustfs
valkey 999 tini
valkey 999 valkey-server
+39
View File
@@ -0,0 +1,39 @@
date: 2026-10-28T18:29:25Z commit: 24f9d61
== before: api replicas (hostname = X-Instance-Id), state, StartedAt, FinishedAt
196b3a876d81 running started=2026-10-28T18:28:29.324800505Z finished=2026-10-28T18:26:09.117691179Z
8bd1d38428d0 running started=2026-10-28T18:25:38.512082013Z finished=2026-10-28T18:25:25.52109609Z
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:18084"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 129, "failed_attempts": 0, "first_recovery_seconds": 2.737}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
== after: api replicas
196b3a876d81 exited started=2026-10-28T18:28:29.324800505Z finished=2026-10-28T18:29:28.219275009Z
8bd1d38428d0 running started=2026-10-28T18:25:38.512082013Z finished=2026-10-28T18:25:25.52109609Z
== stopped replica: 196b3a876d81; shutdown: signal, drain window with readiness 503, close, exit
{"event":"Handling signal: term","level":"info","ts":"2026-10-28T18:29:25.807319Z"}
{"event":"Draining for 2.0 s","level":"info","ts":"2026-10-28T18:29:25.850076Z"}
{"event":"Draining for 2.0 s","level":"info","ts":"2026-10-28T18:29:25.854947Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"3ee651bebf50472d9f12e5496f2500d7","duration_ms":0.64,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:25.990552Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"5f3ad3b3bc0449869616a50c4c0f1960","duration_ms":1.764,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:26.504373Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"1466f11698c14a9892a8e9a5bd446597","duration_ms":0.3,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:27.081651Z"}
{"event":"Shutting down","level":"info","ts":"2026-10-28T18:29:27.852426Z"}
{"event":"Shutting down","level":"info","ts":"2026-10-28T18:29:27.856950Z"}
{"event":"service.stopped","level":"info","ts":"2026-10-28T18:29:27.963846Z"}
{"event":"Finished server process [8]","level":"info","ts":"2026-10-28T18:29:27.964073Z"}
{"event":"service.stopped","level":"info","ts":"2026-10-28T18:29:27.964840Z"}
{"event":"Finished server process [7]","level":"info","ts":"2026-10-28T18:29:27.965086Z"}
{"event":"Worker (pid:7) was sent SIGTERM!","level":"info","ts":"2026-10-28T18:29:28.007750Z"}
{"event":"Worker (pid:8) was sent SIGTERM!","level":"info","ts":"2026-10-28T18:29:28.007877Z"}
{"event":"Shutting down: Master","level":"info","ts":"2026-10-28T18:29:28.116313Z"}
$ scripts/restore.sh
Container mtusi2026lab4-api-2 Running
Container mtusi2026lab4-api-1 Starting
Container mtusi2026lab4-api-1 Started
Container mtusi2026lab4-api-1 Waiting
Container mtusi2026lab4-api-2 Waiting
Container mtusi2026lab4-api-1 Healthy
Container mtusi2026lab4-api-2 Healthy
== after restore
196b3a876d81 running started=2026-10-28T18:29:41.187041709Z finished=2026-10-28T18:29:28.219275009Z
8bd1d38428d0 running started=2026-10-28T18:25:38.512082013Z finished=2026-10-28T18:25:25.52109609Z
+39
View File
@@ -0,0 +1,39 @@
date: 2026-10-28T18:29:44Z commit: 24f9d61
== before: api replicas (hostname = X-Instance-Id), state, StartedAt, FinishedAt
196b3a876d81 running started=2026-10-28T18:29:41.187041709Z finished=2026-10-28T18:29:28.219275009Z
8bd1d38428d0 running started=2026-10-28T18:25:38.512082013Z finished=2026-10-28T18:25:25.52109609Z
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:18084"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 127, "failed_attempts": 0, "first_recovery_seconds": 3.076}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
== after: api replicas
196b3a876d81 running started=2026-10-28T18:29:41.187041709Z finished=2026-10-28T18:29:28.219275009Z
8bd1d38428d0 exited started=2026-10-28T18:25:38.512082013Z finished=2026-10-28T18:29:47.669441462Z
== stopped replica: 8bd1d38428d0; shutdown: signal, drain window with readiness 503, close, exit
{"event":"Handling signal: term","level":"info","ts":"2026-10-28T18:29:44.898894Z"}
{"event":"Draining for 2.0 s","level":"info","ts":"2026-10-28T18:29:44.961814Z"}
{"event":"Draining for 2.0 s","level":"info","ts":"2026-10-28T18:29:44.977718Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"ec593d2f46144036b1dd668ecd5f92ec","duration_ms":0.286,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:45.347641Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"c31a3838896d4bf5b00be6a5d1e13aa8","duration_ms":1.492,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:45.711924Z"}
{"method":"GET","route":"/health/ready","status":503,"request_id":"2be83d842d0e4f9bbf38240a90eaffb0","duration_ms":0.413,"event":"request.handled","level":"info","ts":"2026-10-28T18:29:46.217772Z"}
{"event":"Shutting down","level":"info","ts":"2026-10-28T18:29:46.963596Z"}
{"event":"Shutting down","level":"info","ts":"2026-10-28T18:29:46.979906Z"}
{"event":"service.stopped","level":"info","ts":"2026-10-28T18:29:47.071763Z"}
{"event":"Finished server process [8]","level":"info","ts":"2026-10-28T18:29:47.072039Z"}
{"event":"service.stopped","level":"info","ts":"2026-10-28T18:29:47.084542Z"}
{"event":"Finished server process [7]","level":"info","ts":"2026-10-28T18:29:47.084788Z"}
{"event":"Worker (pid:8) was sent SIGTERM!","level":"info","ts":"2026-10-28T18:29:47.087800Z"}
{"event":"Worker (pid:7) was sent SIGTERM!","level":"info","ts":"2026-10-28T18:29:47.199665Z"}
{"event":"Shutting down: Master","level":"info","ts":"2026-10-28T18:29:47.308734Z"}
$ scripts/restore.sh
Container mtusi2026lab4-api-1 Running
Container mtusi2026lab4-api-2 Starting
Container mtusi2026lab4-api-2 Started
Container mtusi2026lab4-api-1 Waiting
Container mtusi2026lab4-api-2 Waiting
Container mtusi2026lab4-api-1 Healthy
Container mtusi2026lab4-api-2 Healthy
== after restore
196b3a876d81 running started=2026-10-28T18:29:41.187041709Z finished=2026-10-28T18:29:28.219275009Z
8bd1d38428d0 running started=2026-10-28T18:30:00.267276635Z finished=2026-10-28T18:29:47.669441462Z
+10
View File
@@ -0,0 +1,10 @@
{
"ok": true,
"scenario": "failover-upload",
"upload_instance": "196b3a876d81",
"download_instance": "8bd1d38428d0",
"sha256_equal": true,
"hook_seconds": 2.69,
"download_ok_after_seconds": 2.704,
"restored": true
}
+13
View File
@@ -0,0 +1,13 @@
date: 2026-10-28T18:07:56Z commit: 24f9d61
$ psql -c "SELECT substring(password_hash from '^[$]argon2id[$]v=[0-9]+[$]m=[0-9]+,t=[0-9]+,p=[0-9]+[$]') AS kdf_prefix, count(*) AS users, min(length(password_hash)) AS hash_length FROM users GROUP BY 1"
kdf_prefix | users | hash_length
---------------------------------+-------+-------------
$argon2id$v=19$m=19456,t=2,p=1$ | 3 | 97
(1 row)
$ psql -c "SELECT octet_length(token_hash) AS stored_bytes, count(*) AS sessions FROM sessions GROUP BY 1"
stored_bytes | sessions
--------------+----------
32 | 2
(1 row)
+15
View File
@@ -0,0 +1,15 @@
date: 2026-10-28T18:07:54Z commit: 24f9d61
$ psql -c "SELECT (SELECT count(*) FROM users) users, (SELECT count(*) FROM sessions) sessions, (SELECT count(*) FROM chats) chats, (SELECT count(*) FROM chat_members) members, (SELECT count(*) FROM messages) messages, (SELECT count(*) FROM idempotency_keys) keys, (SELECT count(*) FROM attachments) attachments, (SELECT count(*) FROM message_attachments) links, (SELECT count(*) FROM schema_migrations) versions"
users | sessions | chats | members | messages | keys | attachments | links | versions
-------+----------+-------+---------+----------+------+-------------+-------+----------
3 | 2 | 1 | 2 | 3 | 3 | 1 | 1 | 3
(1 row)
$ make migrate
{"reason":"up to date","event":"migration.completed","level":"info","ts":"2026-10-28T18:07:56.099296Z"}
$ psql -c "SELECT (SELECT count(*) FROM users) users, (SELECT count(*) FROM sessions) sessions, (SELECT count(*) FROM chats) chats, (SELECT count(*) FROM chat_members) members, (SELECT count(*) FROM messages) messages, (SELECT count(*) FROM idempotency_keys) keys, (SELECT count(*) FROM attachments) attachments, (SELECT count(*) FROM message_attachments) links, (SELECT count(*) FROM schema_migrations) versions"
users | sessions | chats | members | messages | keys | attachments | links | versions
-------+----------+-------+---------+----------+------+-------------+-------+----------
3 | 2 | 1 | 2 | 3 | 3 | 1 | 1 | 3
(1 row)
+22
View File
@@ -0,0 +1,22 @@
date: 2026-10-28T18:45:50Z commit: 24f9d61
== before: service hostname StartedAt status
api 196b3a876d81 2026-10-28T18:29:41.187041709Z running
api 8bd1d38428d0 2026-10-28T18:30:00.267276635Z running
lb 5ee5dad8003c 2026-10-28T17:51:03.006429091Z running
migrate 953e57e47bd0 2026-10-28T17:51:08.581653636Z exited
postgres b6579b111fba 2026-10-28T17:51:03.015556258Z running
s3 b6098ae6a07c 2026-10-28T17:51:03.018554716Z running
s3-init a815e0190d6e 2026-10-28T17:51:08.598551386Z exited
valkey fb5b482d1d20 2026-10-28T17:51:03.019280466Z running
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:18084"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
== after: service hostname StartedAt status
api 196b3a876d81 2026-10-28T18:46:00.241797636Z running
api 8bd1d38428d0 2026-10-28T18:46:00.316077636Z running
lb 5ee5dad8003c 2026-10-28T18:45:54.117490049Z running
migrate 953e57e47bd0 2026-10-28T18:45:59.135693677Z exited
postgres b6579b111fba 2026-10-28T18:45:53.821784591Z running
s3 b6098ae6a07c 2026-10-28T18:45:53.797547883Z running
s3-init a815e0190d6e 2026-10-28T18:45:59.140801177Z exited
valkey fb5b482d1d20 2026-10-28T18:45:53.875502383Z running
+39
View File
@@ -0,0 +1,39 @@
{
"ok": true,
"scenario": "s3-outage",
"during_outage": {
"upload": {
"status": 503,
"seconds": 0.975,
"instance": "8bd1d38428d0"
},
"metadata": {
"status": 200,
"seconds": 0.01,
"instance": "196b3a876d81"
},
"download": {
"status": 503,
"seconds": 2.533,
"instance": "8bd1d38428d0"
},
"text_message": {
"status": 201,
"seconds": 0.014,
"instance": "196b3a876d81"
},
"health_ready": {
"status": 200,
"seconds": 0.004,
"instance": "8bd1d38428d0"
}
},
"expected": {
"upload": 503,
"download": 503,
"text_message": 201
},
"as_expected": true,
"s3_back_healthy_seconds": 5.773,
"download_after_recovery": "sha256 equal"
}
+94
View File
@@ -0,0 +1,94 @@
== 2026-10-28T19:01:13Z S3 privacy and bootstrap checks
PASS s3: опубликованные host-порты: нет
PASS s3: консоль :9001 внутри контейнера: 000
PASS s3-init: повторный запуск 1: 0
PASS s3-init: повторный запуск 2: 0
PASS s3-init: состояние IAM/bucket не изменилось: same
PASS api: PutObject: allow
PASS api: HeadObject: allow
PASS api: GetObject: allow
PASS api: ListBucket: allow
PASS api: CreateBucket: deny
PASS api: DeleteBucket: deny
PASS api: PutBucketPolicy (public): deny
PASS api: PutBucketLifecycle: deny
PASS api: admin ListUsers: deny
PASS api: PutObject в чужой bucket: deny
PASS api: ListBucket чужого bucket: deny
PASS anonymous: HeadObject: deny
PASS anonymous: ListBucket: deny
PASS anonymous: PutObject: deny
PASS anonymous: прямой HTTP GET объекта из отдельного контейнера: 403
PASS bucket: anonymous policy: private
PASS api: DeleteObject: allow
== IAM/bucket snapshot (без секретов)
{
"items": [
{
"key": "messenger-attachments",
"last_modified": "2026-10-28T17:51:08Z",
"is_dir": true
}
],
"truncated": false
}
{
"path": "root/messenger-attachments",
"permission": "private"
}
{
"name": "messenger-api",
"policy": {
"policy": {
"Statement": [
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::messenger-attachments/*"
],
"Sid": "MessengerApiObjects"
},
{
"Action": [
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:GetBucketLocation"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::messenger-attachments"
],
"Sid": "MessengerApiBucket"
}
],
"Version": "2012-10-17"
},
"policy_name": "messenger-api",
}
}
{
"accessKey": "<api-key>",
"status": "enabled",
"policies": [
"messenger-api"
],
"memberOf": []
}
{
"rules": [
{
"id": "abort-incomplete-uploads",
"status": "Enabled",
"prefix": "",
"abortIncompleteMultipartUploadDays": 1
}
]
}
RESULT: PASS
+193
View File
@@ -0,0 +1,193 @@
date: 2026-10-28T18:07:53Z commit: 24f9d61
$ psql -c '\d+ users'
Table "public.users"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
---------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
id | uuid | | not null | | plain | | |
username | text | | not null | | extended | | |
display_name | text | | not null | | extended | | |
password_hash | text | | not null | | extended | | |
created_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"users_username_key" UNIQUE CONSTRAINT, btree (username)
Check constraints:
"users_display_name_check" CHECK (char_length(display_name) >= 1 AND char_length(display_name) <= 100)
"users_password_hash_check" CHECK (password_hash ~~ '$argon2id$%'::text)
"users_username_format" CHECK (username ~ '^[a-z0-9_]{3,32}$'::text)
Referenced by:
TABLE "chat_members" CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
TABLE "sessions" CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ sessions'
Table "public.sessions"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
token_hash | bytea | | not null | | extended | | |
user_id | uuid | | not null | | plain | | |
created_at | timestamp with time zone | | not null | | plain | | |
expires_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"sessions_pkey" PRIMARY KEY, btree (token_hash)
"sessions_user_idx" btree (user_id)
Check constraints:
"sessions_check" CHECK (expires_at > created_at)
"sessions_token_hash_check" CHECK (octet_length(token_hash) = 32)
Foreign-key constraints:
"sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ chats'
Table "public.chats"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
-----------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
id | uuid | | not null | | plain | | |
title | text | | not null | | extended | | |
created_at | timestamp with time zone | | not null | | plain | | |
last_message_at | timestamp with time zone | | | | plain | | |
Indexes:
"chats_pkey" PRIMARY KEY, btree (id)
Check constraints:
"chats_title_check" CHECK (char_length(title) >= 1 AND char_length(title) <= 100)
Referenced by:
TABLE "chat_members" CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ chat_members'
Table "public.chat_members"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
-----------------+--------------------------+-----------+----------+---------+---------+-------------+--------------+-------------
chat_id | uuid | | not null | | plain | | |
user_id | uuid | | not null | | plain | | |
position | smallint | | not null | | plain | | |
chat_created_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id)
"chat_members_user_idx" btree (user_id, chat_created_at, chat_id)
Check constraints:
"chat_members_position_check" CHECK ("position" >= 1 AND "position" <= 100)
Foreign-key constraints:
"chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
"chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
Referenced by:
TABLE "attachments" CONSTRAINT "attachments_owner_fkey" FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id)
TABLE "idempotency_keys" CONSTRAINT "idempotency_keys_chat_id_user_id_fkey" FOREIGN KEY (chat_id, user_id) REFERENCES chat_members(chat_id, user_id)
TABLE "messages" CONSTRAINT "messages_chat_id_sender_id_fkey" FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
Access method: heap
$ psql -c '\d+ messages'
Table "public.messages"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
id | uuid | | not null | | plain | | |
chat_id | uuid | | not null | | plain | | |
sender_id | uuid | | not null | | plain | | |
text | text | | not null | | extended | | |
created_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"messages_pkey" PRIMARY KEY, btree (id)
"messages_history_idx" btree (chat_id, created_at, id)
Check constraints:
"messages_text_check" CHECK (char_length(text) <= 4000)
Foreign-key constraints:
"messages_chat_id_sender_id_fkey" FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
Referenced by:
TABLE "idempotency_keys" CONSTRAINT "idempotency_keys_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
TABLE "message_attachments" CONSTRAINT "message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ idempotency_keys'
Table "public.idempotency_keys"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
-------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
chat_id | uuid | | not null | | plain | | |
user_id | uuid | | not null | | plain | | |
key | text | | not null | | extended | | |
fingerprint | bytea | | not null | | extended | | |
message_id | uuid | | not null | | plain | | |
created_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"idempotency_keys_pkey" PRIMARY KEY, btree (chat_id, user_id, key)
Check constraints:
"idempotency_keys_fingerprint_check" CHECK (octet_length(fingerprint) = 32)
"idempotency_keys_key_check" CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'::text)
Foreign-key constraints:
"idempotency_keys_chat_id_user_id_fkey" FOREIGN KEY (chat_id, user_id) REFERENCES chat_members(chat_id, user_id)
"idempotency_keys_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ attachments'
Table "public.attachments"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
--------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
id | uuid | | not null | | plain | | |
chat_id | uuid | | not null | | plain | | |
owner_id | uuid | | not null | | plain | | |
filename | text | | not null | | extended | | |
content_type | text | | not null | | extended | | |
size_bytes | bigint | | not null | | plain | | |
sha256 | text | | not null | | extended | | |
width | integer | | not null | | plain | | |
height | integer | | not null | | plain | | |
object_key | text | C | not null | | extended | | |
status | text | | not null | | extended | | |
created_at | timestamp with time zone | | not null | | plain | | |
updated_at | timestamp with time zone | | not null | | plain | | |
Indexes:
"attachments_pkey" PRIMARY KEY, btree (id)
"attachments_chat_key" UNIQUE CONSTRAINT, btree (id, chat_id)
"attachments_object_key_key" UNIQUE CONSTRAINT, btree (object_key)
"attachments_pending_idx" btree (created_at) WHERE status = 'pending'::text
Check constraints:
"attachments_content_type_check" CHECK (content_type = ANY (ARRAY['image/png'::text, 'image/jpeg'::text]))
"attachments_filename_check" CHECK (char_length(filename) >= 1 AND char_length(filename) <= 255)
"attachments_height_check" CHECK (height > 0)
"attachments_sha256_check" CHECK (sha256 ~ '^[0-9a-f]{64}$'::text)
"attachments_size_bytes_check" CHECK (size_bytes >= 1 AND size_bytes <= 10485760)
"attachments_status_check" CHECK (status = ANY (ARRAY['pending'::text, 'ready'::text]))
"attachments_width_check" CHECK (width > 0)
Foreign-key constraints:
"attachments_owner_fkey" FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id)
Referenced by:
TABLE "message_attachments" CONSTRAINT "message_attachments_attachment_fkey" FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE RESTRICT
Access method: heap
$ psql -c '\d+ message_attachments'
Table "public.message_attachments"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
---------------+----------+-----------+----------+---------+---------+-------------+--------------+-------------
message_id | uuid | | not null | | plain | | |
position | smallint | | not null | | plain | | |
chat_id | uuid | | not null | | plain | | |
attachment_id | uuid | | not null | | plain | | |
Indexes:
"message_attachments_pkey" PRIMARY KEY, btree (message_id, "position")
"message_attachments_attachment_idx" btree (attachment_id)
"message_attachments_message_id_attachment_id_key" UNIQUE CONSTRAINT, btree (message_id, attachment_id)
Check constraints:
"message_attachments_position_check" CHECK ("position" >= 0 AND "position" <= 9)
Foreign-key constraints:
"message_attachments_attachment_fkey" FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE RESTRICT
"message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
Access method: heap
$ psql -c '\d+ schema_migrations'
Table "public.schema_migrations"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------+--------------------------+-----------+----------+---------+----------+-------------+--------------+-------------
version | text | | not null | | extended | | |
checksum | text | | not null | | extended | | |
applied_at | timestamp with time zone | | not null | now() | plain | | |
Indexes:
"schema_migrations_pkey" PRIMARY KEY, btree (version)
Access method: heap
$ psql -c 'SELECT version, left(checksum, 12), applied_at FROM schema_migrations'
version | checksum | applied_at
-----------------------+--------------+-------------------------------
0001_init | 9a13618f078a | 2026-10-28 17:51:09.25584+00
0002_idempotency_keys | d9159a355532 | 2026-10-28 17:51:09.261879+00
0003_attachments | c4417f560538 | 2026-10-28 17:51:09.263628+00
(3 rows)
+15
View File
@@ -0,0 +1,15 @@
date: 2026-10-28T18:07:56Z commit: 24f9d61
$ git ls-files secrets .env backups volumes
(пусто: секретов, .env, дампов и volumes в Git нет)
$ git check-ignore -v secrets/database_url secrets/postgres_password secrets/s3_root_password secrets/s3_secret_access_key .env backups/
.gitignore:5:secrets/ secrets/database_url
.gitignore:5:secrets/ secrets/postgres_password
.gitignore:5:secrets/ secrets/s3_root_password
.gitignore:5:secrets/ secrets/s3_secret_access_key
.gitignore:2:.env .env
.gitignore:15:backups/ backups/
$ docker volume ls --filter name=mtusi2026lab4
DRIVER VOLUME NAME
local mtusi2026lab4_pgdata
local mtusi2026lab4_s3data
local mtusi2026lab4_valkeydata
+4
View File
@@ -0,0 +1,4 @@
date: 2026-10-28T17:51:13Z commit: 24f9d61
$ make test BASE_URL=http://localhost:18084
python3 tests/smoke.py --base-url "http://localhost:18084"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
+33
View File
@@ -0,0 +1,33 @@
date: 2026-10-28T18:08:00Z commit: 24f9d61
$ docker compose --env-file .env config --quiet && echo valid
valid
$ docker compose ps --format '{{.Service}} {{.Name}} {{.Status}} {{.Ports}}'
api mtusi2026lab4-api-1 Up 16 minutes (healthy)
api mtusi2026lab4-api-2 Up 16 minutes (healthy)
lb mtusi2026lab4-lb-1 Up 16 minutes (healthy) 127.0.0.1:18084->8080/tcp, 127.0.0.1:24084->8404/tcp
postgres mtusi2026lab4-postgres-1 Up 16 minutes (healthy)
s3 mtusi2026lab4-s3-1 Up 16 minutes (healthy)
valkey mtusi2026lab4-valkey-1 Up 16 minutes (healthy)
$ docker compose config | grep -c container_name
0
$ docker port <каждая реплика api> # реплики не публикуют порт
196b3a876d81: []
8bd1d38428d0: []
$ docker inspect: hostname = X-Instance-Id, сети, лимиты CPU (nano) и памяти
api 196b3a876d81 /mtusi2026lab4-api-1 nets=mtusi2026lab4_backend, cpus=1000000000 mem=536870912 user=10001:10001 ro=true
api 8bd1d38428d0 /mtusi2026lab4-api-2 nets=mtusi2026lab4_backend, cpus=1000000000 mem=536870912 user=10001:10001 ro=true
lb 5ee5dad8003c /mtusi2026lab4-lb-1 nets=mtusi2026lab4_backend,mtusi2026lab4_edge, cpus=1000000000 mem=134217728 user=haproxy ro=true
postgres b6579b111fba /mtusi2026lab4-postgres-1 nets=mtusi2026lab4_backend, cpus=2000000000 mem=1073741824 user=70:70 ro=false
s3 b6098ae6a07c /mtusi2026lab4-s3-1 nets=mtusi2026lab4_backend, cpus=1000000000 mem=536870912 user=rustfs ro=true
valkey fb5b482d1d20 /mtusi2026lab4-valkey-1 nets=mtusi2026lab4_backend, cpus=500000000 mem=134217728 user=999:1000 ro=false
$ curl LB stats: backend api, server status check
api1 UP L7OK 200
api2 UP L7OK 200
BACKEND UP
$ for i in 1..6: curl -D - http://127.0.0.1:18084/health/live | X-Instance-Id
x-instance-id: 8bd1d38428d0
x-instance-id: 196b3a876d81
x-instance-id: 8bd1d38428d0
x-instance-id: 196b3a876d81
x-instance-id: 8bd1d38428d0
x-instance-id: 196b3a876d81
+99
View File
@@ -0,0 +1,99 @@
date: 2026-10-28T19:01:15Z commit: 24f9d61
$ docker compose ps --format '{{.Service}} {{.Status}} {{.Ports}}'
api Up 17 seconds (healthy)
api Up 17 seconds (healthy)
lb Up 23 seconds (healthy) 127.0.0.1:18084->8080/tcp, 127.0.0.1:24084->8404/tcp
migrate Exited (0) 17 seconds ago
postgres Up 23 seconds (healthy)
s3 Up 8 seconds (healthy)
s3-init Exited (0) 18 seconds ago
valkey Up 23 seconds (healthy)
$ psql -c '\d attachments'
Table "public.attachments"
Column | Type | Collation | Nullable | Default
--------------+--------------------------+-----------+----------+---------
id | uuid | | not null |
chat_id | uuid | | not null |
owner_id | uuid | | not null |
filename | text | | not null |
content_type | text | | not null |
size_bytes | bigint | | not null |
sha256 | text | | not null |
width | integer | | not null |
height | integer | | not null |
object_key | text | C | not null |
status | text | | not null |
created_at | timestamp with time zone | | not null |
updated_at | timestamp with time zone | | not null |
Indexes:
"attachments_pkey" PRIMARY KEY, btree (id)
"attachments_chat_key" UNIQUE CONSTRAINT, btree (id, chat_id)
"attachments_object_key_key" UNIQUE CONSTRAINT, btree (object_key)
"attachments_pending_idx" btree (created_at) WHERE status = 'pending'::text
Check constraints:
"attachments_content_type_check" CHECK (content_type = ANY (ARRAY['image/png'::text, 'image/jpeg'::text]))
"attachments_filename_check" CHECK (char_length(filename) >= 1 AND char_length(filename) <= 255)
"attachments_height_check" CHECK (height > 0)
"attachments_sha256_check" CHECK (sha256 ~ '^[0-9a-f]{64}$'::text)
"attachments_size_bytes_check" CHECK (size_bytes >= 1 AND size_bytes <= 10485760)
"attachments_status_check" CHECK (status = ANY (ARRAY['pending'::text, 'ready'::text]))
"attachments_width_check" CHECK (width > 0)
Foreign-key constraints:
"attachments_owner_fkey" FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id)
Referenced by:
TABLE "message_attachments" CONSTRAINT "message_attachments_attachment_fkey" FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE RESTRICT
$ psql -c '\d message_attachments'
Table "public.message_attachments"
Column | Type | Collation | Nullable | Default
---------------+----------+-----------+----------+---------
message_id | uuid | | not null |
position | smallint | | not null |
chat_id | uuid | | not null |
attachment_id | uuid | | not null |
Indexes:
"message_attachments_pkey" PRIMARY KEY, btree (message_id, "position")
"message_attachments_attachment_idx" btree (attachment_id)
"message_attachments_message_id_attachment_id_key" UNIQUE CONSTRAINT, btree (message_id, attachment_id)
Check constraints:
"message_attachments_position_check" CHECK ("position" >= 0 AND "position" <= 9)
Foreign-key constraints:
"message_attachments_attachment_fkey" FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE RESTRICT
"message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
$ psql: столбцы bytea во всей схеме (хеши токенов и отпечатки, не файлы)
table_name | column_name
------------------+-------------
idempotency_keys | fingerprint
sessions | token_hash
(2 rows)
$ psql: вложения по статусу и максимальный размер строки attachments
status | count | max_row_bytes
---------+-------+---------------
ready | 14 | 256
pending | 1 | 256
(2 rows)
$ rc object list --recursive root/messenger-attachments/attachments/ | count
14
$ rc object list root/messenger-attachments/ (первые 3 объекта, ключи назначены сервером)
[2026-10-28 17:51:13] 109.20 KiB attachments/01a183a3-351f-76fa-aeea-a062669b6d93
[2026-10-28 18:08:05] 109.20 KiB attachments/01a183b2-a436-72cf-be25-cebcbb7b5753
[2026-10-28 18:08:15] 109.20 KiB attachments/01a183b2-cbcf-721b-afae-a1b01f9fdbbd
$ сверка вне API: sha256 и размер последнего ready-вложения в PostgreSQL против байтов объекта в bucket
postgres: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 111817
s3: 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75 111817
$ docker inspect api: ReadonlyRootfs, tmpfs, volumes/bind mounts
196b3a876d81 ro=true tmpfs={"/tmp":""} mounts=bind:/run/secrets/database_url bind:/run/secrets/cache_url bind:/run/secrets/cursor_secret bind:/run/secrets/s3_access_key_id bind:/run/secrets/s3_secret_access_key
8bd1d38428d0 ro=true tmpfs={"/tmp":""} mounts=bind:/run/secrets/s3_access_key_id bind:/run/secrets/s3_secret_access_key bind:/run/secrets/database_url bind:/run/secrets/cache_url bind:/run/secrets/cursor_secret
$ docker inspect s3: volume данных
volume:mtusi2026lab4_s3data:/data bind::/run/secrets/s3_root_user bind::/run/secrets/s3_root_password
$ секреты сервисов: root-ключи S3 только у s3/s3-init/s3-cli
api ['cache_url', 'cursor_secret', 'database_url', 's3_access_key_id', 's3_secret_access_key']
lb []
migrate ['database_url']
postgres ['postgres_password']
s3 ['s3_root_password', 's3_root_user']
s3-init ['s3_access_key_id', 's3_root_password', 's3_root_user', 's3_secret_access_key']
valkey ['valkey.conf']
+49
View File
@@ -0,0 +1,49 @@
date: 2026-10-28T17:51:45Z commit: 24f9d61
$ make lint
.venv/bin/ruff check src student-tests scripts/probe.py scripts/limits.py scripts/lab4_scenarios.py
All checks passed!
.venv/bin/ruff format --check src student-tests scripts/probe.py scripts/limits.py scripts/lab4_scenarios.py
131 files already formatted
.venv/bin/ty check src student-tests
All checks passed!
$ make student-test # pytest без маркера stack, по файлам
============================= test session starts ==============================
platform darwin -- Python 3.14.5, pytest-9.1.1, pluggy-1.6.0
rootdir: <repo>
configfile: pyproject.toml
testpaths: student-tests
plugins: asyncio-1.4.0, anyio-4.15.1
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 335 items / 10 deselected / 325 selected
student-tests/adapters/test_s3_storage.py .. [ 0%]
student-tests/api/test_access.py ....... [ 2%]
student-tests/api/test_attachments.py .................................. [ 13%]
... [ 14%]
student-tests/api/test_bounded_contexts.py ................... [ 20%]
student-tests/api/test_cache.py ..... [ 21%]
student-tests/api/test_concurrency.py ... [ 22%]
student-tests/api/test_consistency.py ........ [ 24%]
student-tests/api/test_drain.py .. [ 25%]
student-tests/api/test_events.py ....... [ 27%]
student-tests/api/test_idempotency.py ............... [ 32%]
student-tests/api/test_instance.py .... [ 33%]
student-tests/api/test_message_attachments.py ........... [ 36%]
student-tests/api/test_migrations.py ....... [ 39%]
student-tests/api/test_negative.py ..................................... [ 50%]
[ 50%]
student-tests/api/test_operability.py ............. [ 54%]
student-tests/api/test_pagination.py ................. [ 59%]
student-tests/api/test_pool.py .. [ 60%]
student-tests/api/test_sessions.py .................. [ 65%]
student-tests/api/test_timeout.py ...... [ 67%]
student-tests/api/test_wire_format.py .. [ 68%]
student-tests/unit/test_attachments_domain.py .......................... [ 76%]
[ 76%]
student-tests/unit/test_authentication.py ..................... [ 82%]
student-tests/unit/test_cursor.py ........... [ 86%]
student-tests/unit/test_domain.py ...................................... [ 97%]
...... [ 99%]
student-tests/unit/test_timeouts.py . [100%]
===================== 325 passed, 10 deselected in 35.54s ======================