feat(app) lab2 prepared for mr

This commit is contained in:
imadev
2026-10-23 18:55:20 +03:00
parent 1db3953226
commit f6e74247fd
57 changed files with 2367 additions and 660 deletions
+14
View File
@@ -0,0 +1,14 @@
# Environment and fixed versions
date_utc: 2026-10-20T13:35:48Z
kernel: Linux 6.12.73+deb13-amd64 x86_64
cpus: 4
memory: 7.5Gi
go_module: go version go1.27.1 linux/amd64
python: Python 3.12.3
docker_client: 29.9.1
docker_server: 29.3.0
compose: 5.6.0
images:
postgres:16.10-alpine
redis:7.4.7-alpine
mtusi2026lab2-api
+3
View File
@@ -0,0 +1,3 @@
$ make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
+14
View File
@@ -0,0 +1,14 @@
$ cd app && go build ./... && go vet ./... && go test ./...
? gitflic.ru/project/bulla-max/mtusi2026lab2/cmd/server [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal [no test files]
ok gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth (cached)
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/chats [no test files]
ok gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common (cached)
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/database [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/health [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/messages [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store [no test files]
? gitflic.ru/project/bulla-max/mtusi2026lab2/internal/users [no test files]
+3
View File
@@ -0,0 +1,3 @@
$ make validate PYTHON=/tmp/lab2-venv/bin/python
/tmp/lab2-venv/bin/python scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
+9
View File
@@ -0,0 +1,9 @@
$ docker compose --env-file .env config --quiet
OK: compose file and .env are valid
$ docker compose --env-file .env config --services
redis
postgres
api
$ docker compose --env-file .env config --volumes
pgdata
redisdata
+3
View File
@@ -0,0 +1,3 @@
$ make test BASE_URL=http://172.18.0.1:8080
python3 tests/smoke.py --base-url "http://172.18.0.1:8080"
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
+32
View File
@@ -0,0 +1,32 @@
# Persistence hook scope: real container restarts, volumes preserved
$ docker inspect before
/api started=2026-10-20T13:43:34.744581601Z restarts=0
/postgres started=2026-10-20T13:45:47.006469682Z restarts=0
/redis started=2026-10-20T13:45:33.276541726Z restarts=0
$ make test-persistence BASE_URL=http://172.18.0.1:8080 ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://172.18.0.1:8080"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker inspect after
/api started=2026-10-20T13:46:01.985254148Z restarts=0
/postgres started=2026-10-20T13:46:00.989433719Z restarts=0
/redis started=2026-10-20T13:46:01.023470617Z restarts=0
$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mtusi2026lab2-api-1 sha256:4e5079fddfd7792f1ccf90891259fc4d737a8486a7a92461dcd72c6b0fd6914d "/usr/local/bin/serv…" api 2 minutes ago Up 5 seconds (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp
mtusi2026lab2-api-short-ttl-1 mtusi2026lab2-api-short-ttl "/usr/local/bin/serv…" api-short-ttl 2 minutes ago Up 2 minutes (healthy) 0.0.0.0:8081->8080/tcp, [::]:8081->8080/tcp
mtusi2026lab2-postgres-1 postgres:16.10-alpine "docker-entrypoint.s…" postgres 10 minutes ago Up 6 seconds (healthy) 5432/tcp
mtusi2026lab2-redis-1 redis:7.4.7-alpine "docker-entrypoint.s…" redis 10 minutes ago Up 6 seconds (healthy) 6379/tcp
$ docker volume ls (project volumes)
mtusi2026lab2_pgdata
mtusi2026lab2_redisdata
$ redis AOF files
total 20
-rw------- 1 redis redis 88 Oct 20 13:35 appendonly.aof.1.base.rdb
-rw------- 1 redis redis 11185 Oct 20 13:46 appendonly.aof.1.incr.aof
-rw------- 1 redis redis 88 Oct 20 13:35 appendonly.aof.manifest
+27
View File
@@ -0,0 +1,27 @@
$ docker compose --env-file .env --profile checks up -d --wait api-short-ttl
Container mtusi2026lab2-postgres-1 Running
Container mtusi2026lab2-redis-1 Running
Container mtusi2026lab2-api-short-ttl-1 Running
Container mtusi2026lab2-redis-1 Waiting
Container mtusi2026lab2-postgres-1 Waiting
Container mtusi2026lab2-postgres-1 Healthy
Container mtusi2026lab2-redis-1 Healthy
Container mtusi2026lab2-redis-1 Waiting
Container mtusi2026lab2-api-short-ttl-1 Waiting
Container mtusi2026lab2-postgres-1 Waiting
Container mtusi2026lab2-api-short-ttl-1 Healthy
Container mtusi2026lab2-redis-1 Healthy
Container mtusi2026lab2-postgres-1 Healthy
$ python3 scripts/grade4_checks.py --base-url http://172.18.0.1:8080 --short-ttl-url http://172.18.0.1:8081
PASS: wrong password -> 401 with Basic challenge
PASS: logout revoked the current token (repeat -> 401)
PASS: X-User-Id is ignored and grants no access
PASS: 12 concurrent registrations of one username -> one 201, eleven 409
PASS: failed composite chat creation rolled back completely
PASS: SESSION_TTL_SECONDS=2 session expired and was rejected by the main API
PASS: redis down: live=200, ready=503, protected=503 (bounded unavailable error, no anonymous access; detected in 4.4s)
PASS: redis recovered in 4.0s; same token and confirmed message intact
PASS: postgres down: live=200, ready=503, protected=503 (bounded unavailable error, session retained; detected in 2.8s)
PASS: postgres recovered in 0.9s; same token and confirmed message intact
PASS: grade 4 checks completed
+35
View File
@@ -0,0 +1,35 @@
$ ./scripts/seed_and_explain.sh
INSERT 0 0
INSERT 0 0
INSERT 0 0
DELETE 10000
INSERT 0 10000
VACUUM
seeded_messages
-----------------
10000
(1 row)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..7.03 rows=51 width=70) (actual time=0.018..0.037 rows=51 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.41..1298.32 rows=9999 width=70) (actual time=0.017..0.032 rows=51 loops=1)
Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'::uuid) AND (ROW(created_at, id) > ROW('0001-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Buffers: shared hit=6
Planning:
Buffers: shared hit=18
Planning Time: 0.208 ms
Execution Time: 0.104 ms
(9 rows)
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.41..7.83 rows=51 width=70) (actual time=0.039..0.054 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_chat_created_id_idx on messages (cost=0.41..1055.85 rows=7259 width=70) (actual time=0.038..0.049 rows=51 loops=1)
Index Cond: ((chat_id = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 01:23:20+00'::timestamp with time zone, '00000000-0000-0000-0000-000000000000'::uuid)))
Buffers: shared hit=5
Planning Time: 0.073 ms
Execution Time: 0.087 ms
(7 rows)
+18
View File
@@ -0,0 +1,18 @@
$ python3 tests/load.py --base-url http://172.18.0.1:8080 --duration 10 --concurrency 4 --output evidence/load.json
{
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 10.0,
"elapsed_seconds": 10.089,
"concurrency": 4,
"successful_requests": 8854,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 877.599,
"successful_latency_ms": {
"p50": 4.06,
"p95": 7.835,
"p99": 10.654
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}
+17
View File
@@ -0,0 +1,17 @@
$ docker compose --env-file .env --profile checks ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mtusi2026lab2-api-1 sha256:4e5079fddfd7792f1ccf90891259fc4d737a8486a7a92461dcd72c6b0fd6914d "/usr/local/bin/serv…" api 3 minutes ago Up 22 seconds (healthy) 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp
mtusi2026lab2-api-short-ttl-1 mtusi2026lab2-api-short-ttl "/usr/local/bin/serv…" api-short-ttl 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:8081->8080/tcp, [::]:8081->8080/tcp
mtusi2026lab2-postgres-1 postgres:16.10-alpine "docker-entrypoint.s…" postgres 10 minutes ago Up 23 seconds (healthy) 5432/tcp
mtusi2026lab2-redis-1 redis:7.4.7-alpine "docker-entrypoint.s…" redis 10 minutes ago Up 23 seconds (healthy) 6379/tcp
$ curl -s -o /dev/null -w "%{http_code} %{content_type}\\n" http://172.18.0.1:8080/health/ready
200 application/json; charset=utf-8
$ docker volume ls --filter name=mtusi2026lab2
DRIVER VOLUME NAME
local mtusi2026lab2_pgdata
local mtusi2026lab2_redisdata
$ redis-cli --scan --pattern "messenger:session:*" | wc -l
46
+66
View File
@@ -0,0 +1,66 @@
$ docker compose --env-file .env exec -T postgres psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\\d users" -c "\\d chats" -c "\\d chat_members" -c "\\d messages" -c "TABLE schema_migrations"
Table "public.users"
Column | Type | Collation | Nullable | Default
---------------+--------------------------+-----------+----------+---------
id | uuid | | not null |
username | text | | not null |
display_name | text | | not null |
password_hash | text | | not null |
created_at | timestamp with time zone | | not null |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"users_username_key" UNIQUE CONSTRAINT, btree (username)
Check constraints:
"users_display_name_len" CHECK (char_length(display_name) >= 1 AND char_length(display_name) <= 100)
"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) ON DELETE RESTRICT
TABLE "messages" CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE RESTRICT
Table "public.chats"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+---------
id | uuid | | not null |
title | text | | not null |
created_at | timestamp with time zone | | not null |
Indexes:
"chats_pkey" PRIMARY KEY, btree (id)
Check constraints:
"chats_title_len" 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
TABLE "messages" CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
Table "public.chat_members"
Column | Type | Collation | Nullable | Default
---------+------+-----------+----------+---------
chat_id | uuid | | not null |
user_id | uuid | | not null |
Indexes:
"chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id)
"chat_members_user_idx" btree (user_id, chat_id)
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) ON DELETE RESTRICT
Table "public.messages"
Column | Type | Collation | Nullable | Default
------------+--------------------------+-----------+----------+---------
id | uuid | | not null |
chat_id | uuid | | not null |
sender_id | uuid | | not null |
text | text | | not null |
created_at | timestamp with time zone | | not null |
Indexes:
"messages_pkey" PRIMARY KEY, btree (id)
"messages_chat_created_id_idx" btree (chat_id, created_at, id)
Check constraints:
"messages_text_len" CHECK (char_length(text) >= 1 AND char_length(text) <= 4000)
Foreign-key constraints:
"messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
"messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE RESTRICT
version | applied_at
---------------+-----------------------------
0001_init.sql | 2026-10-20 13:35:37.6134+00
(1 row)
+17
View File
@@ -0,0 +1,17 @@
# Git hygiene: no secrets, volumes or build artifacts tracked
$ git ls-files | grep -E "(^|/)\\.env$|app/tmp|volumes/|(^|/)server$" || echo "OK: none tracked"
OK: none tracked
$ git check-ignore -v .env
.gitignore:2:.env .env
$ git status --short
(empty after this evidence commit)
$ git log --oneline --decorate -3
0b37e16 (HEAD -> master, tag: lab2-green) Реализовать лабораторную 2: PostgreSQL, Redis-сессии, keyset и проверки уровня 4
65003b6 (origin/master, origin/HEAD) Подготовить skeleton лабораторной 2 по мессенджеру
4c14ccb Первый коммит
$ git tag --list
lab2-green
+17
View File
@@ -0,0 +1,17 @@
{
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 10.0,
"elapsed_seconds": 10.089,
"concurrency": 4,
"successful_requests": 8854,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 877.599,
"successful_latency_ms": {
"p50": 4.06,
"p95": 7.835,
"p99": 10.654
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}