Implement lab3 load balancing and idempotent messaging

This commit is contained in:
ferraronp
2026-11-06 18:57:11 +03:00
parent 0ab3a341aa
commit 303c60e2e1
27 changed files with 1133 additions and 33 deletions
+11
View File
@@ -0,0 +1,11 @@
@echo off
setlocal
cd /d "%~dp0.."
docker compose --env-file .env stop --timeout 5 lb api1 api2 redis postgres
if errorlevel 1 exit /b 1
docker compose --env-file .env up -d --wait postgres redis
if errorlevel 1 exit /b 1
docker compose --env-file .env up -d --no-deps api1 api2 lb
if errorlevel 1 exit /b 1
python scripts\wait_ready.py
exit /b %ERRORLEVEL%
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
# Preserve named volumes. Restart all state stores and both API processes.
docker compose --env-file .env stop --timeout 5 lb api1 api2 redis postgres
docker compose --env-file .env up -d --wait postgres redis
docker compose --env-file .env up -d --no-deps api1 api2 lb
python - <<'PY'
import time, urllib.request
for i in range(120):
try:
with urllib.request.urlopen('http://localhost:8080/health/ready', timeout=2) as response:
if response.status == 200:
print('[restart] all services ready')
break
except Exception:
pass
time.sleep(.5)
else:
raise SystemExit('[restart] health timeout')
PY
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
# Run tests/load.py in a second terminal while this script recreates API instances.
for replica in api1 api2; do
docker compose --env-file .env up -d --no-deps --force-recreate "$replica"
docker compose --env-file .env exec -T "$replica" python - <<'PY'
import time,urllib.request
for _ in range(40):
try:
if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200:break
except Exception:pass
time.sleep(.5)
else:raise SystemExit('replica did not recover')
PY
sleep 3
done
+10
View File
@@ -0,0 +1,10 @@
@echo off
setlocal
cd /d "%~dp0.."
if "%TARGET_INSTANCE_ID%"=="api1" goto stop
if "%TARGET_INSTANCE_ID%"=="api2" goto stop
echo Unexpected TARGET_INSTANCE_ID 1>&2
exit /b 2
:stop
docker compose --env-file .env stop --timeout 1 %TARGET_INSTANCE_ID%
exit /b %ERRORLEVEL%
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
# The public failover test sets TARGET_INSTANCE_ID from the actual response header.
set -eu
cd "$(dirname "$0")/.."
case "${TARGET_INSTANCE_ID:?missing TARGET_INSTANCE_ID}" in
api1|api2) docker compose --env-file .env stop --timeout 1 "$TARGET_INSTANCE_ID" ;;
*) echo 'Unexpected instance alias' >&2; exit 2 ;;
esac
+14
View File
@@ -0,0 +1,14 @@
"""Operator-facing readiness wait after container replacement."""
import time
import urllib.request
for _ in range(120):
try:
with urllib.request.urlopen("http://localhost:8080/health/ready", timeout=2) as response:
if response.status == 200:
print("[restart] all services ready")
break
except Exception:
pass
time.sleep(.5)
else:
raise SystemExit("[restart] health timeout")