Реализация ЛР 3 на оценку 3: HAProxy, 2 реплики API, идемпотентность, evidence

This commit is contained in:
2026-11-10 01:26:52 +03:00
parent 0ab3a341aa
commit de55dd1d8a
29 changed files with 2032 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
# Перезапуск API и хранилищ с сохранением volume (никаких down -v).
set -eu
cd "$(dirname "$0")/.."
docker compose --env-file .env stop api
docker compose --env-file .env restart postgres valkey
docker compose --env-file .env start api
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
# Одна сессия, 10 запросов через LB: в ответах должны быть >=2 разных X-Instance-Id.
set -eu
BASE="${BASE_URL:-http://localhost:8080}"
U="demo_$(date +%s)"
curl -sf -X POST "$BASE/api/v1/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$U\",\"display_name\":\"Demo\",\"password\":\"demo-password-123\"}" >/dev/null
TOKEN=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -u "$U:demo-password-123" \
| python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])')
for i in 1 2 3 4 5 6 7 8 9 10; do
curl -si "$BASE/api/v1/users/me" -H "Authorization: Bearer $TOKEN" | grep -i '^x-instance-id'
done | sort | uniq -c
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
# Останавливает ОДНУ API-реплику по TARGET_INSTANCE_ID (= hostname контейнера = короткий ID)
# Реплика остаётся остановленной. БД, LB и volumes не затрагиваются
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
case "$TARGET_INSTANCE_ID" in
""|*[!A-Za-z0-9_.-]*) echo "bad instance id" >&2; exit 2 ;;
esac
cd "$(dirname "$0")/.."
CID=$(docker ps --filter "label=com.docker.compose.service=api" \
--filter "id=$TARGET_INSTANCE_ID" --format '{{.ID}}')
[ -n "$CID" ] || { echo "no running api container for this instance" >&2; exit 1; }
docker stop -t 2 "$CID" >/dev/null
echo "stopped api container $CID" >&2