ЛР 3 — БПИ2303 — Морару — на 5: API на TypeScript/Node, HAProxy, failover, трек 1

This commit is contained in:
celis
2026-10-29 20:59:24 +03:00
parent 0ab3a341aa
commit c4f9eb7648
83 changed files with 1862 additions and 19 deletions
+21
View File
@@ -0,0 +1,21 @@
#!/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.
set -eu
cd "$(dirname "$0")/.."
RATE=${RATE:-300}
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
t0=$(date +%s)
docker stop "$cid" >/dev/null # graceful: readiness 503 -> LB DOWN -> drain
t1=$(date +%s)
wait
echo "stopped $name in $((t1 - t0)) s: $(cat /tmp/failover-each.$$)"
docker start "$cid" >/dev/null
until [ "$(docker inspect -f '{{.State.Health.Status}}' "$cid")" = healthy ]; do sleep 1; done
echo "restored $name"
done
rm -f /tmp/failover-each.$$
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
# Creates local synthetic credentials and .env once. Never overwrites: the pgdata volume keeps the old password.
set -eu
cd "$(dirname "$0")/.."
[ -f .env ] || cp .env.example .env
if [ -f secrets/postgres_password ]; then
echo "secrets/ already exists, keeping it"
exit 0
fi
mkdir -p secrets
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
echo "created secrets/ and .env"
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
# Restarts the LB, every API replica and PostgreSQL; the pgdata volume and the schema are kept as is.
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
+21
View File
@@ -0,0 +1,21 @@
#!/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).
set -eu
cd "$(dirname "$0")/.."
docker compose build api >/dev/null
docker compose run --rm migrate >/dev/null
old=$(docker compose ps -q api)
known=$old
n=$(printf '%s\n' "$old" | grep -c .)
for cid in $old; do
docker compose up -d --no-deps --no-recreate --scale api=$((n + 1)) api >/dev/null 2>&1
new=$(docker compose ps -q api | grep -v -F "$known")
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
docker stop "$cid" >/dev/null && docker rm "$cid" >/dev/null
echo "replaced $(echo "$cid" | cut -c1-12) -> $(echo "$new" | cut -c1-12)"
done
+14
View File
@@ -0,0 +1,14 @@
#!/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
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
cd "$(dirname "$0")/.."
case "$TARGET_INSTANCE_ID" in
*[!0-9a-f]* | "") echo "unexpected instance id" >&2; exit 2 ;;
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)
echo "stopped $(docker inspect -f '{{.Name}}' "$ids") ($TARGET_INSTANCE_ID)"