forked from mtusi2026/mtusi2026lab3
- compose: один масштабируемый сервис api (deploy.replicas=2, без ports и container_name), HAProxy 3.2 LTS по digest — единственная точка входа (127.0.0.1), разовый migrate до старта реплик, CPU/RAM-лимиты у всех сервисов, каждому сервису только его secrets. - HAProxy: server-template + Docker DNS (re-resolve 1 с), активная проверка GET /health/ready каждую 1 с (fall 2 / rise 2), без backup-серверов, retry только conn-failure, ошибки самого LB — в формате контракта с X-Request-Id. - API: X-Instance-Id = hostname контейнера на каждом ответе (и на 500/503); Idempotency-Key обязателен; ключ, сообщение и результат пишутся в ОДНОЙ транзакции (миграция 0002), конкурентный повтор ждёт уникальный индекс; другой body — 409, посторонний — всегда 404; attachment_ids=[] равен отсутствию поля; дубли ключей JSON и тело у POST /auth/sessions — 400. - SIGTERM: readiness 503 + DRAIN_SECONDS=3 обслуживания, затем graceful stop. - scripts: stop-one.sh (+ .cmd для Windows), restore-api.sh, restart.sh перезапускает и LB, lbctl.py — состояние бэкендов HAProxy -> контейнеры. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
36 lines
1.6 KiB
Bash
Executable File
36 lines
1.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# Failover hook for `make test-failover ACTION_SCRIPT=scripts/stop-one.sh`.
|
|
# Stops exactly ONE API replica: the container whose X-Instance-Id (= container hostname,
|
|
# see src/messenger/config.py) is in TARGET_INSTANCE_ID. DB, Valkey, LB and volumes are
|
|
# not touched. The replica is LEFT STOPPED (`docker stop` = no restart by the policy);
|
|
# restore it explicitly afterwards: scripts/restore-api.sh.
|
|
# STOP_MODE=stop (default) SIGTERM: readiness 503, 3 s drain, graceful exit (~4 s)
|
|
# STOP_MODE=kill SIGKILL at once (`docker stop -t 0`): crash of the process
|
|
# Returns within 10 s. The test hides the output; run by hand to see it (no secrets printed).
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
|
case "$TARGET_INSTANCE_ID" in
|
|
*[!A-Za-z0-9._-]*) echo "stop-one: invalid TARGET_INSTANCE_ID" >&2; exit 2 ;;
|
|
esac
|
|
mode="${STOP_MODE:-stop}"
|
|
|
|
target=""
|
|
# Only RUNNING containers of THIS compose project's service `api` are candidates.
|
|
for id in $(docker compose ps -q api); do
|
|
if [ "$(docker inspect -f '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then
|
|
target="$id"
|
|
fi
|
|
done
|
|
if [ -z "$target" ]; then
|
|
echo "stop-one: no running api replica with X-Instance-Id=$TARGET_INSTANCE_ID" >&2
|
|
exit 3
|
|
fi
|
|
name="$(docker inspect -f '{{.Name}}' "$target")"
|
|
case "$mode" in
|
|
stop) docker stop --time 15 "$target" >/dev/null ;;
|
|
kill) docker stop --time 0 "$target" >/dev/null ;;
|
|
*) echo "stop-one: STOP_MODE must be stop or kill" >&2; exit 2 ;;
|
|
esac
|
|
echo "stop-one: $mode $name (X-Instance-Id=$TARGET_INSTANCE_ID) -> $(docker inspect -f '{{.State.Status}} exit={{.State.ExitCode}}' "$target")"
|