Разбить API на модули и перевести комментарии на русский

This commit is contained in:
celis
2026-10-29 22:26:25 +03:00
parent c4f9eb7648
commit e8ba028b89
29 changed files with 630 additions and 456 deletions
+4 -4
View File
@@ -1,6 +1,6 @@
#!/bin/sh
# Level 4: under constant k6 load, stop each API replica in turn (restoring the previous one first).
# Prints per-replica k6 summary: successful RPS, errors, latency. Recovery = downtime of the failed replica.
# Уровень 4: под постоянной нагрузкой k6 по очереди останавливает каждую API-реплику (предыдущую восстанавливает).
# Печатает итог k6 по каждой: успешные RPS, ошибки, задержки.
set -eu
cd "$(dirname "$0")/.."
RATE=${RATE:-300}
@@ -8,9 +8,9 @@ for cid in $(docker compose ps -q api); do
name=$(docker inspect -f '{{.Name}}' "$cid")
docker run --rm --network messenger-lab3_frontend -v "$PWD/research/k6:/k6:ro" \
-e RATE="$RATE" -e DURATION=20s -e PAIRS=5 grafana/k6:1.8.2 run -q /k6/messenger.js 2>/dev/null | tail -1 > /tmp/failover-each.$$ &
sleep 12 # setup + 5 s of steady load
sleep 12 # setup k6 + 5 с установившейся нагрузки
t0=$(date +%s)
docker stop "$cid" >/dev/null # graceful: readiness 503 -> LB DOWN -> drain
docker stop "$cid" >/dev/null # штатно: readiness 503 → LB DOWN → drain
t1=$(date +%s)
wait
echo "stopped $name in $((t1 - t0)) s: $(cat /tmp/failover-each.$$)"
+2 -2
View File
@@ -1,5 +1,5 @@
#!/bin/sh
# Creates local synthetic credentials and .env once. Never overwrites: the pgdata volume keeps the old password.
# Один раз создаёт локальные учебные секреты и .env. Не перезаписывает: в volume pgdata остаётся старый пароль.
set -eu
cd "$(dirname "$0")/.."
[ -f .env ] || cp .env.example .env
@@ -12,5 +12,5 @@ chmod 700 secrets
password=$(openssl rand -hex 24)
printf '%s' "$password" > secrets/postgres_password
printf 'postgres://messenger:%s@postgres:5432/messenger' "$password" > secrets/database_url
chmod 644 secrets/postgres_password secrets/database_url # read by container users; the directory stays 700
chmod 644 secrets/postgres_password secrets/database_url # читают пользователи контейнеров; сам каталог остаётся 700
echo "created secrets/ and .env"
+4 -4
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Restarts the LB, every API replica and PostgreSQL; the pgdata volume and the schema are kept as is.
# Перезапускает LB, все API-реплики и PostgreSQL; volume pgdata и схема сохраняются.
set -eu
cd "$(dirname "$0")/.."
docker compose stop lb api # graceful SIGTERM, replicas drain first
docker compose restart postgres # same container, same volume
docker compose start api lb # migrate is a no-op on an up-to-date schema
docker compose stop lb api # штатный SIGTERM, реплики сначала завершают запросы
docker compose restart postgres # тот же контейнер и тот же volume
docker compose start api lb # на актуальной схеме migrate ничего не делает
+4 -4
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Rolling replacement for Compose: rebuild the image, then for each old replica start one new replica,
# wait until it is healthy (HAProxy picks it up via DNS), and only then stop/remove the old one.
# Migrations run before (expand-only, compatible with the running version).
# Поочерёдная замена для Compose: пересобрать образ, затем для каждой старой реплики запустить одну новую,
# дождаться healthy (HAProxy находит её через DNS) и только потом остановить и удалить старую.
# Миграции выполняются заранее и только расширяют схему (совместимы с работающей версией).
set -eu
cd "$(dirname "$0")/.."
docker compose build api >/dev/null
@@ -15,7 +15,7 @@ for cid in $old; do
known="$known
$new"
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$new")" = healthy ]; do sleep 1; done
sleep 2 # rise 2 x inter 1 s: LB marks the new replica UP
sleep 2 # rise 2 × inter 1 с: LB помечает новую реплику UP
docker stop "$cid" >/dev/null && docker rm "$cid" >/dev/null
echo "replaced $(echo "$cid" | cut -c1-12) -> $(echo "$new" | cut -c1-12)"
done
+4 -4
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Stops exactly the API replica whose X-Instance-Id is TARGET_INSTANCE_ID and leaves it stopped.
# X-Instance-Id = container hostname = first 12 hex chars of the container ID.
# Restore afterwards: docker compose start api
# Останавливает ровно ту API-реплику, чей X-Instance-Id передан в TARGET_INSTANCE_ID, и оставляет её остановленной.
# X-Instance-Id = hostname контейнера = первые 12 hex-символов ID контейнера.
# Восстановление после теста: docker compose start api
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
cd "$(dirname "$0")/.."
@@ -10,5 +10,5 @@ case "$TARGET_INSTANCE_ID" in
esac
ids=$(docker compose ps -q api | grep "^$TARGET_INSTANCE_ID" || true)
[ "$(printf '%s\n' "$ids" | grep -c .)" -eq 1 ] || { echo "no single running api container for $TARGET_INSTANCE_ID" >&2; exit 1; }
docker stop "$ids" >/dev/null # SIGTERM -> readiness 503 -> drain -> exit (about 3 s)
docker stop "$ids" >/dev/null # SIGTERM → readiness 503 → drain → выход (около 3 с)
echo "stopped $(docker inspect -f '{{.Name}}' "$ids") ($TARGET_INSTANCE_ID)"