Add the stop-one, restart and restore hooks and an LB probe

scripts/stop-one.sh maps TARGET_INSTANCE_ID to the api container whose
hostname matches, stops only it with docker stop -t 6 and leaves it
stopped; an unknown id exits 3 and touches nothing. scripts/restart.sh
replaces the lab 2 hook: it stops lb and the replicas, restarts
PostgreSQL and Valkey with their volumes and brings the stand back
with up --wait, which also reruns the idempotent migrate job.
scripts/restore.sh starts stopped replicas without recreating them
and scripts/lb-wait.sh waits until the balancer statistics show every
replica UP again, so the next failover run sees two replicas.

scripts/probe.py check (stdlib, Python 3.9 syntax) counts X-Instance-Id
over 40 calls with one token and races 8 posts with one key through
the balancer. make restore and make lb-check run them, ruff covers the
probe, and the CI acceptance job runs the failover test and the probe.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 11:43:52 +03:00
co-authored by Claude Opus 5.5
parent dc2015473f
commit ceb266ed05
9 changed files with 198 additions and 11 deletions
+11
View File
@@ -0,0 +1,11 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
service=${API_SERVICE:-api}
want=${1:-$(docker compose ps -q "$service" | wc -l | tr -d ' ')}
url=${LB_STATS_URL:-http://$(docker compose port lb 8404)/stats;csv}
deadline=$(($(date +%s) + ${WAIT_TIMEOUT:-60}))
until [ "$(curl -fsS "$url" | awk -F, -v b="$service" '$1 == b && $2 != "BACKEND" && $18 == "UP"' | wc -l)" -ge "$want" ]; do
[ "$(date +%s)" -lt "$deadline" ] || { echo "LB does not see $want UP $service servers" >&2; exit 4; }
sleep 0.5
done
+138
View File
@@ -0,0 +1,138 @@
#!/usr/bin/env python3
import argparse
import base64
import collections
import functools
import json
import os
import subprocess
import sys
import threading
import urllib.error
import urllib.request
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
@functools.cache
def published(port, fallback):
command = ["docker", "compose", "port", "lb", str(port)]
found = subprocess.run(command, cwd=str(ROOT), capture_output=True, text=True, check=False)
return found.stdout.strip() or fallback
def base_url():
return os.environ.get("BASE_URL") or "http://" + published(8080, "localhost:8080")
def call(method, path, body=None, headers=None, timeout=2.0):
data = None if body is None else json.dumps(body, ensure_ascii=False).encode()
head = {"Accept": "application/json"}
if data is not None:
head["Content-Type"] = "application/json"
head.update(headers or {})
request = urllib.request.Request(
base_url().rstrip("/") + path, data=data, headers=head, method=method
)
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
payload = json.loads(response.read() or b"null")
return response.status, response.headers.get("X-Instance-Id", ""), payload
except urllib.error.HTTPError as error:
return error.code, error.headers.get("X-Instance-Id", ""), None
except (OSError, ValueError):
return 0, "", None
def emit(report, out=None):
text = json.dumps(report, ensure_ascii=False, indent=2)
if out:
Path(out).parent.mkdir(parents=True, exist_ok=True)
Path(out).write_text(text + "\n")
print(text)
def created(result, what):
if result[0] != 201:
raise SystemExit(f"{what} failed: HTTP {result[0]}")
return result[2]
def actor(label):
username = f"p_{label}_{uuid.uuid4().hex[:12]}"
password = "Probe-" + uuid.uuid4().hex
body = {"username": username, "display_name": "probe " + label, "password": password}
user = created(call("POST", "/api/v1/users", body, timeout=10), "register")
basic = base64.b64encode(f"{username}:{password}".encode()).decode()
login = call(
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + basic}, timeout=10
)
return {
"id": user["id"],
"auth": {"Authorization": "Bearer " + created(login, "login")["token"]},
}
def chat_of(owner, other):
body = {"title": "probe", "member_ids": [owner["id"], other["id"]]}
return created(call("POST", "/api/v1/chats", body, owner["auth"], timeout=10), "chat")["id"]
def send(owner, chat, text, key):
headers = dict(owner["auth"], **{"Idempotency-Key": key})
return call("POST", f"/api/v1/chats/{chat}/messages", {"text": text}, headers)
def check(args):
alice, bob = actor("alice"), actor("bob")
chat = chat_of(alice, bob)
seen = collections.Counter(
call("GET", "/api/v1/users/me", headers=alice["auth"])[1] for _ in range(args.samples)
)
key, text = uuid.uuid4().hex, "same key " + uuid.uuid4().hex
barrier, results = threading.Barrier(args.parallel), []
def worker():
barrier.wait()
results.append(send(alice, chat, text, key))
threads = [threading.Thread(target=worker) for _ in range(args.parallel)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
page = call("GET", f"/api/v1/chats/{chat}/messages?limit=100", headers=bob["auth"])[2]
bodies = [r[2] for r in results if r[0] == 201]
race = {
"parallel": args.parallel,
"statuses": dict(collections.Counter(r[0] for r in results)),
"instances": dict(collections.Counter(r[1] for r in results)),
"identical_bodies": bool(bodies) and all(b == bodies[0] for b in bodies),
"stored_copies": sum(1 for m in (page or {"items": []})["items"] if m.get("text") == text),
"other_body_status": send(alice, chat, text + " changed", key)[0],
}
passed = (
len(seen) >= 2
and race["statuses"] == {201: args.parallel}
and race["identical_bodies"]
and race["stored_copies"] == 1
and race["other_body_status"] == 409
)
emit({"instances_one_token": dict(seen), "same_key": race, "pass": passed}, args.out)
return 0 if passed else 1
def main():
parser = argparse.ArgumentParser(description="LB probes over the public HTTP contract")
sub = parser.add_subparsers(dest="mode", required=True)
one = sub.add_parser("check", help="instances behind one token and a same-key race")
one.add_argument("--samples", type=int, default=40)
one.add_argument("--parallel", type=int, default=8)
one.add_argument("--out", help="JSON file, written only after a complete report")
return check(parser.parse_args())
if __name__ == "__main__":
sys.exit(main())
+9 -3
View File
@@ -1,6 +1,12 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose stop api
docker compose restart postgres valkey
docker compose up -d --wait api
defined=$(docker compose config --services)
stores=""
for service in ${STATE_SERVICES:-postgres valkey}; do
if printf '%s\n' "$defined" | grep -qx "$service"; then stores="$stores $service"; fi
done
[ -n "$stores" ] || { echo "no state services found" >&2; exit 3; }
docker compose stop lb "${API_SERVICE:-api}"
docker compose restart $stores
docker compose up -d --wait --wait-timeout "${WAIT_TIMEOUT:-75}"
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose up -d --no-deps --no-recreate --wait --wait-timeout "${WAIT_TIMEOUT:-60}" "${API_SERVICE:-api}"
scripts/lb-wait.sh
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
cd "$(dirname "$0")/.."
ids=$(docker compose ps -q "${API_SERVICE:-api}")
[ -n "$ids" ] || { echo "no running ${API_SERVICE:-api} replicas" >&2; exit 3; }
target=$(docker inspect -f '{{.Id}} {{.Config.Hostname}}' $ids | awk -v t="$TARGET_INSTANCE_ID" '$2 == t {print $1}')
[ -n "$target" ] || { echo "no running ${API_SERVICE:-api} replica answers as $TARGET_INSTANCE_ID" >&2; exit 3; }
docker stop -t "${STOP_TIMEOUT:-6}" "$target" >/dev/null