ЛР 3: балансировка, идемпотентность и failover — на 3

This commit is contained in:
2026-11-20 16:20:41 +03:00
parent 0ab3a341aa
commit be96c39563
31 changed files with 1490 additions and 21 deletions
+10
View File
@@ -0,0 +1,10 @@
PS> docker compose run --rm -T tools make check validate test
Container mtusi-lab3-grade3-tools-run-8d9dad6c38c7 Creating
Container mtusi-lab3-grade3-tools-run-8d9dad6c38c7 Created
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
python3 scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
python3 tests/smoke.py --base-url "http://lb:8080"
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
exit=0
+11
View File
@@ -0,0 +1,11 @@
PS> docker compose run --rm -T tools python student-tests/grade3.py core
Container mtusi-lab3-grade3-tools-run-309ba1f80a2b Creating
Container mtusi-lab3-grade3-tools-run-309ba1f80a2b Created
PASS: one Bearer accepted by api1 and api2
{"same_key_race": [{"instance": "api2", "status": 201, "message_id": "0f879955-e2e3-41da-a213-66a6105d1048"}, {"instance": "api1", "status": 201, "message_id": "0f879955-e2e3-41da-a213-66a6105d1048"}], "history_count": 1}
{"different_body_race": {"api1": 201, "api2": 409}}
PASS: independent keys/users/chats, invalid key/body, authorization and revocation on replay
PASS: Argon2id, session TTL and expiry, 24h key TTL and concurrent key reuse after expiry
PASS: retry of committed POST with unread response returned ae7b9594-56af-4e3b-a31e-3f400c28c36c
PASS: message insert lock timeout produced 503 and rolled back reservation, same key then committed once
exit=0
+64
View File
@@ -0,0 +1,64 @@
Windows build 10.0.19045.0
PS> docker version
Client:
Version: 29.9.3
API version: 1.56
Go version: go1.26.10
Git commit: 5457199
Built: Tue Nov 3 21:46:10 2026
OS/Arch: windows/amd64
Context: desktop-linux
Server: Docker Desktop 4.99.0 (245822)
Engine:
Version: 29.9.3
API version: 1.56 (minimum version 1.40)
Go version: go1.26.10
Git commit: 70efc0e
Built: Tue Nov 3 21:41:42 2026
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v2.4.2
GitCommit: b7a25261e029d68978bbf290df0a62fc819b3c51
runc:
Version: 1.5.2
GitCommit: v1.5.2-0-g29dd3dc2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
PS> docker compose version
Docker Compose version v5.7.0
PS> docker info (Docker Desktop Linux VM)
linux x86_64 CPUs=4 MemoryBytes=8242380800
PS> docker compose --profile test config --quiet
PS> docker compose ps -a
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mtusi-lab3-grade3-api1-1 mtusi-lab3-api:grade3 "sh -c 'exec uvicorn…" api1 7 minutes ago Up About a minute (healthy) 8080/tcp
mtusi-lab3-grade3-api2-1 mtusi-lab3-api:grade3 "sh -c 'exec uvicorn…" api2 7 minutes ago Up 51 seconds (healthy) 8080/tcp
mtusi-lab3-grade3-lb-1 haproxy:3.0.11-alpine "docker-entrypoint.s…" lb 9 minutes ago Up 9 minutes 127.0.0.1:8080->8080/tcp
mtusi-lab3-grade3-migrate-1 mtusi-lab3-api:grade3 "python -m src.db" migrate 7 minutes ago Exited (0) 3 minutes ago
mtusi-lab3-grade3-postgres-1 postgres:16.8-bookworm "docker-entrypoint.s…" postgres 9 minutes ago Up About a minute (healthy) 5432/tcp
PS> docker compose logs --no-color migrate
migrate-1 | PASS: versioned migrations applied (already applied versions skipped)
migrate-1 | PASS: versioned migrations applied (already applied versions skipped)
migrate-1 | PASS: versioned migrations applied (already applied versions skipped)
migrate-1 | PASS: versioned migrations applied (already applied versions skipped)
migrate-1 | PASS: versioned migrations applied (already applied versions skipped)
PS> Applied migration versions
001_initial.sql
002_message_idempotency.sql
PS> API runtime
Python 3.12.10
PS> HAProxy configuration check
PS> Tool versions and both executable hooks
Container mtusi-lab3-grade3-tools-run-e0c2ceb3000d Creating
Container mtusi-lab3-grade3-tools-run-e0c2ceb3000d Created
Python 3.12.15
GNU Make 4.4.1
Docker Compose version v2.33.0
755
755
PS> Windows loopback readiness
PS> Windows loopback public smoke
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
+22
View File
@@ -0,0 +1,22 @@
PS> docker compose run --rm -T tools python student-tests/grade3.py failover
Container mtusi-lab3-grade3-tools-run-e2ebc343dc86 Creating
Container mtusi-lab3-grade3-tools-run-e2ebc343dc86 Created
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://lb:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 118, "failed_attempts": 0, "first_recovery_seconds": 1.893}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
PASS: api2 read confirmed history after api1 stopped in 0.892s
{"target": "api1", "before": {"api1": "9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f running 2026-11-20T11:44:11.521174934Z", "api2": "ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 running 2026-11-20T11:44:11.248047203Z"}, "after": {"api1": "9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f exited 2026-11-20T11:44:11.521174934Z", "api2": "ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 running 2026-11-20T11:44:11.248047203Z"}}
PASS: api2 replayed e580ba9a-ae15-4763-89a5-4985eaec6d0c while api1 was stopped, history count 1
9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f
PASS: api1 rejoined the LB with the same Bearer; migrator was not restarted
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://lb:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 116, "failed_attempts": 0, "first_recovery_seconds": 1.889}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
PASS: api1 read confirmed history after api2 stopped in 1.002s
{"target": "api2", "before": {"api1": "9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f running 2026-11-20T11:44:35.925512672Z", "api2": "ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 running 2026-11-20T11:44:11.248047203Z"}, "after": {"api1": "9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f running 2026-11-20T11:44:35.925512672Z", "api2": "ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 exited 2026-11-20T11:44:11.248047203Z"}}
PASS: api1 replayed 8ee32cdc-1ad9-46f6-8f0c-fa5c3854353c while api2 was stopped, history count 1
ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02
PASS: api2 rejoined the LB with the same Bearer; migrator was not restarted
exit=0
+72
View File
@@ -0,0 +1,72 @@
Baseline lab3: 0ab3a34, solution/bvt2301-gamayunov, initially clean.
Read-only source lab2: 207e380, solution/bvt2301-gamayunov, initially and finally clean.
M = modified; A = added to working tree. Index remained empty.
M REPORT.md
A .gitattributes
A .github/workflows/application.yml
A Dockerfile
A PR_GRADE3_DRAFT.md
A compose.yaml
A evidence/grade3/checks.txt
A evidence/grade3/core.txt
A evidence/grade3/environment.txt
A evidence/grade3/failover.txt
A evidence/grade3/files.txt
A evidence/grade3/persistence.txt
A infra/Dockerfile.tools
A infra/haproxy.cfg
A infra/requirements-tools.lock
A migrations/001_initial.sql
A migrations/002_message_idempotency.sql
A requirements.lock
A requirements.txt
A scripts/init-env.ps1
A scripts/restart.sh
A scripts/stop-one.sh
A src/__init__.py
A src/config.py
A src/db.py
A src/errors.py
A src/main.py
A src/models.py
A src/pagination.py
A src/security.py
A src/store.py
A student-tests/grade3.py
Future staging allowlist (on publication):
.gitattributes
.github/workflows/application.yml
Dockerfile
REPORT.md
compose.yaml
evidence/grade3/checks.txt
evidence/grade3/core.txt
evidence/grade3/environment.txt
evidence/grade3/failover.txt
evidence/grade3/files.txt
evidence/grade3/persistence.txt
infra/Dockerfile.tools
infra/haproxy.cfg
infra/requirements-tools.lock
migrations/001_initial.sql
migrations/002_message_idempotency.sql
requirements.lock
requirements.txt
scripts/init-env.ps1
scripts/restart.sh
scripts/stop-one.sh
src/__init__.py
src/config.py
src/db.py
src/errors.py
src/main.py
src/models.py
src/pagination.py
src/security.py
src/store.py
student-tests/grade3.py
The two action hooks must receive file mode 100755 during future staging.
PR_GRADE3_DRAFT.md is local review material included in the ZIP.
+11
View File
@@ -0,0 +1,11 @@
PS> docker compose run --rm -T tools python student-tests/grade3.py persistence
Container mtusi-lab3-grade3-tools-run-6baa53019397 Creating
Container mtusi-lab3-grade3-tools-run-6baa53019397 Created
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://lb: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.
postgres before 5d254f0d75a9a9f9467e73ff3618215cf6535830043b347fb402ca84b8a63094 running 2026-11-20T11:39:51.532308399Z after 5d254f0d75a9a9f9467e73ff3618215cf6535830043b347fb402ca84b8a63094 running 2026-11-20T11:44:07.540264913Z
api1 before 9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f running 2026-11-20T11:41:43.244757785Z after 9b898d6720dca8eb1cfb84fde08090f64b808a495578c0185c77bd63892fdf1f running 2026-11-20T11:44:11.521174934Z
api2 before ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 running 2026-11-20T11:42:05.596639197Z after ab7c05600399d2c7dc9c5beb0dc661eb222032d32a9758f9cee945b4ae196a02 running 2026-11-20T11:44:11.248047203Z
PASS: old Bearer on both replicas after PostgreSQL restart, unchanged expiry, replay 443b9676-28b5-43f3-a2f7-2df69ab3cf76, history count 1
exit=0