The stand was recreated from a4ca9ef (make down, make run_docker) and
all evidence of the change was taken again on it: stand, distribution,
LB errors, test-persistence, test-failover for each replica (stop and
kill), stop-one refusals, make test and student_test through the LB,
test_isolated, failure_checks and bench self-checks.
New evidence closes scenarios verify found without proof: a real git
clone on non-default ports, a hung replica (docker pause: DOWN after
7.7 s, in-flight request 504), PostgreSQL outage behind the LB, replica
tests failing against one process, aborted responses via
driver.py kill-inflight. REPORT.md gets the corrected detection
formula, the measured cost of the 1 CPU limit for Argon2id and the new
rows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
8.7 KiB
Markdown
148 lines
8.7 KiB
Markdown
# Распределение одной сессии по репликам и масштабирование без правки LB
|
||
|
||
| | |
|
||
|---|---|
|
||
| Спеки | `load-balancing` «Распределение без привязки клиента» — «Распределение видно оператору»; «Взаимозаменяемые реплики» — «Две реплики по умолчанию», «Масштабирование без правки балансировщика»; «Проверка готовности реплик» — «Вернувшаяся реплика включена»; «Статистика балансировщика внутри сети» — «Состояние реплик» |
|
||
| Change | `replicas-lb`, задачи 6.1 и 7.2 |
|
||
| Дата | 2026-10-10 15:46:18–15:46:32 UTC |
|
||
| Commit | `a4ca9ef` (стенд пересоздан с этого commit, `stand.md`) |
|
||
| Стенд | `make run_docker`: проект `mtusi-lab3`, HAProxy на `127.0.0.1:8080`, `api` × 2 (`API_REPLICAS` по умолчанию) |
|
||
| Хост | Docker Desktop 29.8.1, macOS arm64; ВМ Docker: 12 CPU, 7,7 GiB RAM; ноутбук от батареи; лимиты — `stand.md` |
|
||
| Клиент | `make instances` (`driver.py instances` скилла `run-mtusi-messenger`): один синтетический пользователь, один вход по Basic, затем 12 × `GET /api/v1/users/me` с одним Bearer-токеном; токен не печатается |
|
||
|
||
Шаг 1 сценария защиты (`README.md`): ≥2 разных `X-Instance-Id` через LB
|
||
с одной сессией.
|
||
|
||
```text
|
||
$ make instances
|
||
1 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
2 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
3 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
4 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
5 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
6 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
7 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
8 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
9 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
10 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
11 GET /api/v1/users/me 200 x-instance-id: 3681880fc55f
|
||
12 GET /api/v1/users/me 200 x-instance-id: 8100f940a5fa
|
||
user lb_2939b70f: one login, 12 requests with one token
|
||
3681880fc55f: 6
|
||
8100f940a5fa: 6
|
||
instances: 2
|
||
|
||
$ make lb_stats
|
||
svname addr status check_status scur stot econ wretr wredis hrsp_5xx
|
||
api-1 172.30.0.5:8080 UP L7OK 0 156 0 0 0 0
|
||
api-2 172.30.0.4:8080 UP L7OK 0 156 0 0 0 0
|
||
BACKEND - UP - 0 312 0 0 0 0
|
||
free slots: 6
|
||
```
|
||
|
||
- Один вход, 12 запросов с одним токеном: все 200 с этим пользователем,
|
||
ответы чередуются между двумя репликами (round-robin, без sticky).
|
||
- `X-Instance-Id` — первые 12 символов ID контейнера:
|
||
`3681880fc55f` = `mtusi-lab3-api-1`, `8100f940a5fa` = `mtusi-lab3-api-2`
|
||
(`stand.md`).
|
||
- `make lb_stats` — занятые слоты HAProxy с адресом и состоянием
|
||
проверки (`L7OK` — `GET /health/ready` ответил 200). Счётчики
|
||
накопительные с подъёма стенда в 15:45, в них и замер цены лимита из
|
||
`stand.md`.
|
||
Слоты HAProxy не совпадают с именами контейнеров: слот получает адрес
|
||
из DNS.
|
||
|
||
## Третья реплика без правки конфигурации LB
|
||
|
||
```text
|
||
$ make replicas API_REPLICAS=3
|
||
docker compose up -d --no-deps --no-recreate --wait --scale api=3 api
|
||
Container mtusi-lab3-api-1 Running
|
||
Container mtusi-lab3-api-2 Running
|
||
Container mtusi-lab3-api-3 Creating
|
||
Container mtusi-lab3-api-3 Created
|
||
Container mtusi-lab3-api-3 Starting
|
||
Container mtusi-lab3-api-3 Started
|
||
Container mtusi-lab3-api-3 Waiting
|
||
Container mtusi-lab3-api-2 Waiting
|
||
Container mtusi-lab3-api-1 Waiting
|
||
Container mtusi-lab3-api-3 Healthy
|
||
Container mtusi-lab3-api-2 Healthy
|
||
Container mtusi-lab3-api-1 Healthy
|
||
# первый make instances с тремя значениями — через 2,0 с после начала make replicas
|
||
$ docker compose ps api
|
||
mtusi-lab3-api-1 3681880fc55f Up About a minute (healthy) 8080/tcp
|
||
mtusi-lab3-api-2 8100f940a5fa Up About a minute (healthy) 8080/tcp
|
||
mtusi-lab3-api-3 1885063aad9e Up 1 second (healthy) 8080/tcp
|
||
$ make instances
|
||
user lb_f9fd2695: one login, 12 requests with one token
|
||
8100f940a5fa: 4
|
||
1885063aad9e: 4
|
||
3681880fc55f: 4
|
||
instances: 3
|
||
$ make lb_stats
|
||
svname addr status check_status scur stot econ wretr wredis hrsp_5xx
|
||
api-1 172.30.0.5:8080 UP L7OK 0 166 0 0 0 0
|
||
api-2 172.30.0.4:8080 UP L7OK 0 165 0 0 0 0
|
||
api-3 172.30.0.7:8080 UP L7OK 0 9 0 0 0 0
|
||
BACKEND - UP - 0 340 0 0 0 0
|
||
free slots: 5
|
||
$ docker compose logs -t haproxy # окно масштабирования
|
||
2026-10-10T15:46:19.435866171Z api/api-3: IP changed from '(none)' to '172.30.0.7' by 'DNS cache'.
|
||
2026-10-10T15:46:19.435888296Z Server api/api-3 ('api') is DOWN/READY (resolves again).
|
||
2026-10-10T15:46:19.435889921Z Server api/api-3 administratively READY thanks to valid DNS answer.
|
||
2026-10-10T15:46:20.407450380Z Server api/api-3 is UP, reason: Layer7 check passed, code: 200, check duration: 8ms. 3 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
|
||
```
|
||
|
||
- Новая реплика попала в свободный слот из DNS Docker без
|
||
перезагрузки и правки `haproxy.cfg`. Слот начал в `DOWN`
|
||
(`init-state down`) и получил трафик только после проверок
|
||
`/health/ready` (через 1 с после появления адреса); через 2 с после
|
||
начала команды ответы шли уже от трёх реплик (спек: не позже 10 с).
|
||
|
||
## Обратно две реплики
|
||
|
||
```text
|
||
$ make replicas
|
||
docker compose up -d --no-deps --no-recreate --wait --scale api=2 api
|
||
Container mtusi-lab3-api-1 Running
|
||
Container mtusi-lab3-api-2 Running
|
||
Container mtusi-lab3-api-3 Stopping
|
||
Container mtusi-lab3-api-3 Stopped
|
||
Container mtusi-lab3-api-3 Removing
|
||
Container mtusi-lab3-api-3 Removed
|
||
Container mtusi-lab3-api-2 Waiting
|
||
Container mtusi-lab3-api-1 Waiting
|
||
Container mtusi-lab3-api-2 Healthy
|
||
Container mtusi-lab3-api-1 Healthy
|
||
$ docker compose ps api
|
||
mtusi-lab3-api-1 3681880fc55f Up About a minute (healthy) 8080/tcp
|
||
mtusi-lab3-api-2 8100f940a5fa Up About a minute (healthy) 8080/tcp
|
||
$ make lb_stats # сразу после команды
|
||
svname addr status check_status scur stot econ wretr wredis hrsp_5xx
|
||
api-1 172.30.0.5:8080 UP L7OK 0 166 0 0 0 0
|
||
api-2 172.30.0.4:8080 UP L7OK 0 165 0 0 0 0
|
||
api-3 172.30.0.7:8080 UP 1/2 L4CON 0 9 0 0 0 0
|
||
BACKEND - UP - 0 340 0 0 0 0
|
||
free slots: 5
|
||
$ make instances
|
||
8100f940a5fa: 6
|
||
3681880fc55f: 6
|
||
instances: 2
|
||
$ docker compose logs -t haproxy
|
||
2026-10-10T15:46:28.469533134Z Server api/api-3 is going DOWN for maintenance (No IP for server ). 2 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
|
||
$ make lb_stats # 15:46:32
|
||
svname addr status check_status scur stot econ wretr wredis hrsp_5xx
|
||
api-1 172.30.0.5:8080 UP L7OK 0 173 0 0 0 0
|
||
api-2 172.30.0.4:8080 UP L7OK 0 172 0 0 0 0
|
||
BACKEND - UP - 0 354 0 0 0 0
|
||
free slots: 6
|
||
```
|
||
|
||
`make replicas` без аргумента возвращает число из `.env` (по умолчанию
|
||
2): лишняя реплика остановлена graceful и удалена. Сразу после этого
|
||
слот ещё `UP 1/2` с одной проваленной проверкой (`L4CON` — отказ
|
||
соединения); запрос, который попал бы на него, LB перенаправил бы
|
||
(`retry-on conn-failure`). Затем DNS убирает адрес, и слот снова
|
||
свободен.
|