feat: API replicas over HAProxy (3)

This commit is contained in:
2026-11-06 03:26:54 +03:00
parent de2f63b926
commit ec111aadfb
32 changed files with 396 additions and 147 deletions
+14
View File
@@ -0,0 +1,14 @@
$ api replicas before
messenger-lab3_api_1 hostname=0405cf8a4a54 Up 10 seconds
messenger-lab3_api_2 hostname=2751ae93e6d1 Up 10 seconds
$ 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:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 87, "failed_attempts": 1, "first_recovery_seconds": 2.52}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
exit=0
$ api replicas after (stopped one = TARGET_INSTANCE_ID)
messenger-lab3_api_1 hostname=0405cf8a4a54 Up 26 seconds
messenger-lab3_api_2 hostname=2751ae93e6d1 Exited (0) 15 seconds ago
+18
View File
@@ -0,0 +1,18 @@
$ api replicas before (api_2 restored with: podman compose start api)
messenger-lab3_api_1 hostname=0405cf8a4a54 Up 2 minutes
messenger-lab3_api_2 hostname=2751ae93e6d1 Up 19 seconds
$ curl -s localhost:8080/health/live (shift round-robin so the test write lands on the other replica)
{"status":"ok"}
$ 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:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 81, "failed_attempts": 1, "first_recovery_seconds": 2.424}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
exit=0
$ api replicas after (stopped one = TARGET_INSTANCE_ID)
messenger-lab3_api_1 hostname=0405cf8a4a54 Exited (0) 15 seconds ago
messenger-lab3_api_2 hostname=2751ae93e6d1 Up 35 seconds
+8
View File
@@ -0,0 +1,8 @@
BASE_URL="http://localhost:8080" python3 scripts/idempotency-race.py
parallel requests with one key: 20
successful responses (201): 20
failed requests: 0 []
distinct message ids in responses: 1
messages stored in history: 1
instances that served requests: ['0405cf8a4a54', '2751ae93e6d1']
OK: exactly one message created
+18
View File
@@ -0,0 +1,18 @@
20 GET /api/v1/users/me through LB (localhost:8080) with ONE bearer session, no re-login
X-Instance-Id 0405cf8a4a54 responses 10
X-Instance-Id 2751ae93e6d1 responses 10
$ podman ps --filter label=com.docker.compose.service=api --format "{{.Names}} {{.ID}} {{.Status}}"
messenger-lab3_api_1 0405cf8a4a54 Up 54 seconds
messenger-lab3_api_2 2751ae93e6d1 Up 53 seconds
$ podman inspect -f "{{.Name}} hostname={{.Config.Hostname}} ports={{.NetworkSettings.Ports}}" api replicas
messenger-lab3_api_1 hostname=0405cf8a4a54 ports=map[8080/tcp:[]]
messenger-lab3_api_2 hostname=2751ae93e6d1 ports=map[8080/tcp:[]]
$ podman ps --format "{{.Names}} {{.Ports}}" (only lb publishes 8080)
messenger-lab3_postgres_1 127.0.0.1:55432->5432/tcp
messenger-lab3_redis_1 127.0.0.1:56379->6379/tcp
messenger-lab3_api_1 8080/tcp
messenger-lab3_api_2 8080/tcp
messenger-lab3_lb_1 0.0.0.0:8080->8080/tcp
+15
View File
@@ -0,0 +1,15 @@
$ podman ps -a --filter label=com.docker.compose.service=migrate
messenger-lab3_migrate_1 Exited (0) 53 seconds ago
$ podman logs messenger-lab3_migrate_1
1/u init (10.119477ms)
2/u message_idempotency (14.878187ms)
no change
no change
no change
$ podman logs messenger-lab3_api_1 | grep -i migrat (API replicas do not run migrations)
0
$ psql: SELECT version, dirty FROM schema_migrations
2|f
+25
View File
@@ -0,0 +1,25 @@
$ containers before
messenger-lab3_postgres_1 8c509b4f5143 Up About a minute (healthy)
messenger-lab3_redis_1 b5db2a433051 Up About a minute (healthy)
messenger-lab3_migrate_1 9fa71320cdba Exited (0) About a minute ago
messenger-lab3_api_1 0405cf8a4a54 Up About a minute
messenger-lab3_api_2 2751ae93e6d1 Up About a minute
messenger-lab3_lb_1 d3a35fe2295d Up About a minute
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost: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.
exit=0
$ containers after (same IDs, fresh Up time = restarted, volumes kept)
messenger-lab3_postgres_1 8c509b4f5143 Up 1 second (healthy)
messenger-lab3_redis_1 b5db2a433051 Up 1 second (healthy)
messenger-lab3_migrate_1 9fa71320cdba Exited (0) Less than a second ago
messenger-lab3_api_1 0405cf8a4a54 Up Less than a second
messenger-lab3_api_2 2751ae93e6d1 Up Less than a second
messenger-lab3_lb_1 d3a35fe2295d Up Less than a second
$ podman volume ls
messenger-lab3_pgdata
messenger-lab3_redisdata
+2
View File
@@ -0,0 +1,2 @@
python3 tests/smoke.py --base-url "http://localhost:8080"
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
+42
View File
@@ -0,0 +1,42 @@
? messenger/cmd/server [no test files]
? messenger/internal/config [no test files]
? messenger/internal/domain [no test files]
=== RUN TestSessionExpiry
--- PASS: TestSessionExpiry (3.09s)
=== RUN TestLogout
--- PASS: TestLogout (0.08s)
=== RUN TestWrongPassword
--- PASS: TestWrongPassword (0.10s)
=== RUN TestRedisUnavailable
redis: 2026/11/06 03:16:52 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/06 03:16:52 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/06 03:16:53 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/06 03:16:53 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/06 03:16:54 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/06 03:16:54 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestRedisUnavailable (2.89s)
=== RUN TestDatabaseUnavailable
--- PASS: TestDatabaseUnavailable (0.17s)
=== RUN TestInvalidJSON
--- PASS: TestInvalidJSON (0.01s)
=== RUN TestForeignChat
--- PASS: TestForeignChat (0.17s)
=== RUN TestEmptyMessage
--- PASS: TestEmptyMessage (0.08s)
=== RUN TestDuplicateUsername
--- PASS: TestDuplicateUsername (0.09s)
=== RUN TestPaginationMultiplePages
--- PASS: TestPaginationMultiplePages (0.08s)
=== RUN TestConcurrentMessages
--- PASS: TestConcurrentMessages (0.52s)
PASS
ok messenger/internal/handler 7.273s
=== RUN TestRegisterUsernameRace
--- PASS: TestRegisterUsernameRace (0.02s)
=== RUN TestCreateChatRollback
--- PASS: TestCreateChatRollback (0.02s)
PASS
ok messenger/internal/repository/postgres 0.043s
? messenger/internal/repository/redis [no test files]
? messenger/internal/router [no test files]
? messenger/internal/service [no test files]