forked from mtusi2026/mtusi2026lab3
feat: конф нгинкс, тесты, ключ идмпт etc
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Миграции с нуля и повторно: отдельный compose-проект с собственным пустым
|
||||
# volume. Рабочий стенд и его данные не затрагиваются; временный volume
|
||||
# этого проекта удаляется в конце.
|
||||
# Миграции с нуля и повторно, отказ API на непримененной схеме: отдельный
|
||||
# compose-проект с собственным пустым volume. Рабочий стенд и его данные
|
||||
# не затрагиваются; временный volume этого проекта удаляется в конце.
|
||||
cd "$(dirname "$0")/.."
|
||||
PROJECT=messenger-lab3-migrations-check
|
||||
# Свой порт на хосте: основной стенд уже занимает DB_PORT
|
||||
export DB_PORT="${MIGRATIONS_CHECK_DB_PORT:-55432}"
|
||||
# Публикуется только порт LB: свои порты, чтобы не конфликтовать с основным стендом
|
||||
export LB_PORT="${MIGRATIONS_CHECK_LB_PORT:-58080}"
|
||||
export LB_EXPIRY_PORT="${MIGRATIONS_CHECK_LB_EXPIRY_PORT:-58081}"
|
||||
COMPOSE="docker compose --env-file ${ENV_FILE:-.env} -p $PROJECT"
|
||||
trap '$COMPOSE down -v --remove-orphans >/dev/null 2>&1' EXIT
|
||||
|
||||
$COMPOSE up -d --wait postgres >/dev/null 2>&1
|
||||
$COMPOSE run --rm --no-deps -T api python - <<'EOF'
|
||||
# Тот же образ и сеть, что у сервиса migrate; apply_migrations трижды подряд
|
||||
$COMPOSE run --rm --no-deps -T migrate python - <<'EOF'
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
@@ -27,6 +29,13 @@ async def main() -> None:
|
||||
"SELECT count(*) FROM pg_tables WHERE schemaname = 'public';"
|
||||
)
|
||||
print(f"пустой volume: таблиц в public = {tables}")
|
||||
# Так стартует API-реплика: без migrate она отказывается работать
|
||||
try:
|
||||
await db.check_migrations()
|
||||
except RuntimeError as error:
|
||||
print(f"API до migrate: {error}")
|
||||
else:
|
||||
raise SystemExit("FAIL: API стартовал бы на пустой схеме")
|
||||
for attempt in (1, 2, 3):
|
||||
await db.apply_migrations()
|
||||
async with db.acquire() as conn:
|
||||
@@ -40,6 +49,8 @@ async def main() -> None:
|
||||
f"запуск {attempt}: schema_migrations = "
|
||||
f"{[(r['version'], r['name']) for r in versions]}, таблиц = {tables}"
|
||||
)
|
||||
await db.check_migrations()
|
||||
print("API после migrate: схема совместима")
|
||||
await db.close()
|
||||
|
||||
|
||||
|
||||
Executable
+89
@@ -0,0 +1,89 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Совместимость текущей схемы с предыдущей версией API — условие rolling update:
|
||||
# миграции применяются до замены реплик, и старые реплики продолжают работать.
|
||||
# Образ собирается из коммита PREVIOUS_REVISION (по умолчанию lab2-код aa971fb),
|
||||
# запускается одним процессом в сети backend рабочего стенда (наружу порт
|
||||
# не публикуется) и выполняет вход, создание чата, запись и чтение сообщения.
|
||||
# Создаются только синтетические данные; volumes не затрагиваются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
REVISION="${PREVIOUS_REVISION:-aa971fb}"
|
||||
IMAGE="mtusi-messenger:compat-$REVISION"
|
||||
NETWORK=messenger-lab3_backend
|
||||
|
||||
git archive "$REVISION" | docker build --quiet --tag "$IMAGE" - >/dev/null
|
||||
echo "previous version image: $IMAGE ($(git log -1 --format='%h %s' "$REVISION"))"
|
||||
echo "schema on stand: $(docker compose --env-file "$ENV_FILE" exec -T postgres sh -c \
|
||||
'psql -X -q -tA -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT string_agg(name, \$\$, \$\$ ORDER BY version) FROM schema_migrations;"')"
|
||||
|
||||
docker run --rm -i --network "$NETWORK" --env-file "$ENV_FILE" \
|
||||
-e DB_HOST=postgres -e DB_PORT=5432 -e VALKEY_HOST=valkey -e VALKEY_PORT=6379 \
|
||||
"$IMAGE" sh -c 'python src/gateway.py >/tmp/api.log 2>&1 & exec python -' <<'EOF'
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
|
||||
|
||||
def call(method, path, body=None, headers=None):
|
||||
data = None if body is None else json.dumps(body).encode()
|
||||
request = urllib.request.Request(BASE + path, data=data, method=method)
|
||||
request.add_header("Content-Type", "application/json")
|
||||
for name, value in (headers or {}).items():
|
||||
request.add_header(name, value)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=5) as response:
|
||||
return response.status, json.loads(response.read() or b"null")
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, None
|
||||
|
||||
|
||||
for _ in range(60):
|
||||
try:
|
||||
if call("GET", "/health/ready")[0] == 200:
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
print(open("/tmp/api.log").read()[-2000:])
|
||||
raise SystemExit("FAIL: previous version did not become ready on current schema")
|
||||
|
||||
username = "compat_" + uuid.uuid4().hex[:12]
|
||||
password = "Compat-" + uuid.uuid4().hex
|
||||
status_user, user = call(
|
||||
"POST", "/api/v1/users",
|
||||
{"username": username, "display_name": "Compat", "password": password},
|
||||
)
|
||||
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
|
||||
status_login, session = call(
|
||||
"POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + credentials}
|
||||
)
|
||||
bearer = {"Authorization": "Bearer " + session["token"]}
|
||||
status_chat, chat = call(
|
||||
"POST", "/api/v1/chats", {"title": "compat", "member_ids": [user["id"]]}, bearer
|
||||
)
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
status_send, message = call("POST", path, {"text": "старая версия пишет"}, bearer)
|
||||
status_read, page = call("GET", path, headers=bearer)
|
||||
result = {
|
||||
"ready": True,
|
||||
"register": status_user,
|
||||
"login": status_login,
|
||||
"create_chat": status_chat,
|
||||
"send_message_without_key": status_send,
|
||||
"read_history": status_read,
|
||||
"history_matches": page["items"] == [message],
|
||||
}
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
log = open("/tmp/api.log").read()
|
||||
print("startup log:", [line.split(" - ", 1)[-1] for line in log.splitlines() if "миграц" in line.lower() or "Схема" in line])
|
||||
if any(code >= 400 for code in result.values() if isinstance(code, int)) or not result["history_matches"]:
|
||||
raise SystemExit("FAIL: previous version is not compatible with current schema")
|
||||
print("PASS: previous API version works on the current schema (expand-only migration)")
|
||||
EOF
|
||||
@@ -0,0 +1,480 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Нагрузка через LB во время отказа/замены API-реплик; метрики и проверка данных.
|
||||
|
||||
failover — для каждой API-реплики по очереди: нагрузка → остановка этой реплики
|
||||
(docker stop = SIGTERM, затем docker kill = SIGKILL) → восстановление
|
||||
(scripts/restore.sh) → нагрузка после восстановления.
|
||||
rolling — нагрузка во время scripts/rolling-update.sh (поочерёдная замена реплик).
|
||||
|
||||
Клиент ведёт себя как настоящий: неподтверждённый POST повторяет с тем же
|
||||
Idempotency-Key и телом. После прогона вся история читается заново: каждое
|
||||
подтверждённое сообщение на месте и не изменено, дублей нет.
|
||||
Python 3.9+, стандартная библиотека; tokens не печатаются и не сохраняются.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client # noqa: E402
|
||||
|
||||
REQUEST_TIMEOUT_SEC = 5
|
||||
RETRY_PAUSE_SEC = 0.05
|
||||
STATS_FORMAT = "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Attempt:
|
||||
"""Одна HTTP-попытка клиента; время — секунды от начала прогона"""
|
||||
|
||||
op: str
|
||||
started: float
|
||||
finished: float
|
||||
status: int | None
|
||||
error: str | None
|
||||
instance: str | None
|
||||
retry: bool
|
||||
|
||||
@property
|
||||
def ok(self) -> bool:
|
||||
return self.status in (200, 201)
|
||||
|
||||
|
||||
class Recorder:
|
||||
def __init__(self) -> None:
|
||||
self.started = time.monotonic()
|
||||
self.lock = threading.Lock()
|
||||
self.attempts: list[Attempt] = []
|
||||
self.confirmed: list[dict] = []
|
||||
self.events: dict[str, float] = {}
|
||||
|
||||
def now(self) -> float:
|
||||
return time.monotonic() - self.started
|
||||
|
||||
def mark(self, name: str) -> None:
|
||||
self.events[name] = round(self.now(), 3)
|
||||
|
||||
def add(self, attempt: Attempt, message: dict | None = None) -> None:
|
||||
with self.lock:
|
||||
self.attempts.append(attempt)
|
||||
if message is not None:
|
||||
self.confirmed.append(message)
|
||||
|
||||
|
||||
def request(
|
||||
base: str, method: str, path: str, token: str, body: dict | None, key: str | None
|
||||
) -> tuple[int | None, str | None, str | None, dict | None]:
|
||||
headers = {"Authorization": "Bearer " + token, "Accept": "application/json"}
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
if key is not None:
|
||||
headers["Idempotency-Key"] = key
|
||||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT_SEC) as response:
|
||||
payload = json.loads(response.read())
|
||||
return response.status, None, response.headers.get("X-Instance-Id"), payload
|
||||
except urllib.error.HTTPError as error:
|
||||
error.read()
|
||||
return error.code, None, error.headers.get("X-Instance-Id"), None
|
||||
except OSError as error:
|
||||
return None, type(error).__name__, None, None
|
||||
|
||||
|
||||
def worker(
|
||||
base: str,
|
||||
token: str,
|
||||
chat_id: str,
|
||||
worker_id: int,
|
||||
recorder: Recorder,
|
||||
stop: threading.Event,
|
||||
) -> None:
|
||||
"""Запись с повтором по тому же ключу до подтверждения, затем чтение"""
|
||||
path = f"/api/v1/chats/{chat_id}/messages"
|
||||
index = 0
|
||||
while not stop.is_set():
|
||||
key = uuid.uuid4().hex
|
||||
body = {"text": f"exp-{worker_id}-{index}-{key[:8]}"}
|
||||
retry = False
|
||||
while not stop.is_set():
|
||||
started = recorder.now()
|
||||
status, error, instance, payload = request(
|
||||
base, "POST", path, token, body, key
|
||||
)
|
||||
attempt = Attempt(
|
||||
"write", started, recorder.now(), status, error, instance, retry
|
||||
)
|
||||
recorder.add(attempt, payload if status == 201 else None)
|
||||
if status == 201:
|
||||
break
|
||||
retry = True
|
||||
time.sleep(RETRY_PAUSE_SEC)
|
||||
started = recorder.now()
|
||||
status, error, instance, _ = request(
|
||||
base, "GET", path + "?limit=1", token, None, None
|
||||
)
|
||||
recorder.add(
|
||||
Attempt("read", started, recorder.now(), status, error, instance, False)
|
||||
)
|
||||
index += 1
|
||||
|
||||
|
||||
def percentile(values: list[float], p: float) -> float | None:
|
||||
if not values:
|
||||
return None
|
||||
ordered = sorted(values)
|
||||
return round(1000 * ordered[max(0, math.ceil(p * len(ordered)) - 1)], 1)
|
||||
|
||||
|
||||
def phase_stats(attempts: list[Attempt], start: float, end: float) -> dict:
|
||||
inside = [a for a in attempts if start <= a.finished < end]
|
||||
duration = max(end - start, 1e-9)
|
||||
result: dict = {"seconds": round(duration, 3)}
|
||||
for op in ("write", "read"):
|
||||
ops = [a for a in inside if a.op == op]
|
||||
ok = [a for a in ops if a.ok]
|
||||
result[op] = {
|
||||
"ok": len(ok),
|
||||
"errors": len(ops) - len(ok),
|
||||
"ok_rps": round(len(ok) / duration, 1),
|
||||
"latency_ms": {
|
||||
"p50": percentile([a.finished - a.started for a in ok], 0.50),
|
||||
"p95": percentile([a.finished - a.started for a in ok], 0.95),
|
||||
"p99": percentile([a.finished - a.started for a in ok], 0.99),
|
||||
},
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def recovery_stats(attempts: list[Attempt], action_start: float, end: float) -> dict:
|
||||
"""
|
||||
Время восстановления записи: от первой ошибки после воздействия до первой
|
||||
успешной записи после последней ошибки; без ошибок — 0. Отдельно —
|
||||
наибольший разрыв между подтверждениями записи (задержка без ошибок).
|
||||
"""
|
||||
window = sorted(
|
||||
(a for a in attempts if a.op == "write" and a.finished >= action_start - 1),
|
||||
key=lambda a: a.finished,
|
||||
)
|
||||
errors = [a for a in window if not a.ok and a.finished >= action_start]
|
||||
kinds: dict[str, int] = {}
|
||||
for a in errors:
|
||||
kind = a.error or f"HTTP {a.status}"
|
||||
kinds[kind] = kinds.get(kind, 0) + 1
|
||||
if errors:
|
||||
last_error = errors[-1].finished
|
||||
first_ok_after = next(
|
||||
(a.finished for a in window if a.ok and a.finished > last_error), end
|
||||
)
|
||||
recovery = first_ok_after - errors[0].finished
|
||||
else:
|
||||
recovery = 0.0
|
||||
oks = [a.finished for a in window if a.ok and a.finished <= end]
|
||||
gap = max((b - a for a, b in zip(oks, oks[1:])), default=None)
|
||||
return {
|
||||
"write_errors": len(errors),
|
||||
"error_kinds": kinds,
|
||||
"write_recovery_seconds": round(recovery, 3),
|
||||
"max_write_gap_seconds": None if gap is None else round(gap, 3),
|
||||
}
|
||||
|
||||
|
||||
def timeline(attempts: list[Attempt], end: float) -> list[dict]:
|
||||
rows = []
|
||||
for second in range(int(end) + 1):
|
||||
inside = [a for a in attempts if second <= a.finished < second + 1]
|
||||
instances: dict[str, int] = {}
|
||||
for a in inside:
|
||||
if a.ok and a.instance:
|
||||
instances[a.instance] = instances.get(a.instance, 0) + 1
|
||||
rows.append(
|
||||
{
|
||||
"t": second,
|
||||
"ok": sum(a.ok for a in inside),
|
||||
"errors": sum(not a.ok for a in inside),
|
||||
"instances": instances,
|
||||
}
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def read_history(client: Client, actor: dict, chat_id: str) -> list[dict]:
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100"
|
||||
items: list[dict] = []
|
||||
while True:
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
items.extend(page["items"])
|
||||
if page["next_cursor"] is None:
|
||||
return items
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=100&cursor=" + quote(
|
||||
page["next_cursor"], safe=""
|
||||
)
|
||||
|
||||
|
||||
def integrity(client: Client, actor: dict, chat_id: str, recorder: Recorder) -> dict:
|
||||
history = read_history(client, actor, chat_id)
|
||||
by_id = {m["id"]: m for m in history}
|
||||
texts = [m["text"] for m in history]
|
||||
confirmed = {m["id"]: m for m in recorder.confirmed}
|
||||
return {
|
||||
"messages_in_history": len(history),
|
||||
"confirmed_unique": len(confirmed),
|
||||
"confirmed_missing_or_changed": sum(
|
||||
by_id.get(i) != m for i, m in confirmed.items()
|
||||
),
|
||||
"duplicate_ids": len(history) - len(by_id),
|
||||
"duplicate_texts": len(texts) - len(set(texts)),
|
||||
"retried_writes_confirmed_as_replay": len(recorder.confirmed) - len(confirmed),
|
||||
}
|
||||
|
||||
|
||||
def run(command: list[str], env: dict | None = None) -> str:
|
||||
result = subprocess.run(
|
||||
command, cwd=ROOT, env=env, check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def api_containers() -> list[str]:
|
||||
ids = run(["docker", "compose", "--env-file", ".env", "ps", "-q", "api"]).split()
|
||||
names = [run(["docker", "inspect", "-f", "{{.Name}}", i]).lstrip("/") for i in ids]
|
||||
return sorted(names)
|
||||
|
||||
|
||||
def instance_of(container: str) -> str:
|
||||
return run(["docker", "exec", container, "cat", "/tmp/instance-id"])
|
||||
|
||||
|
||||
def container_stats() -> list[str]:
|
||||
names = ["messenger-lab3-nginx-1", "messenger-lab3-postgres-1", *api_containers()]
|
||||
output = run(["docker", "stats", "--no-stream", "--format", STATS_FORMAT, *names])
|
||||
return output.splitlines()
|
||||
|
||||
|
||||
def scenario(
|
||||
args: argparse.Namespace,
|
||||
label: str,
|
||||
action: list[str],
|
||||
restore: list[str] | None,
|
||||
target: str | None = None,
|
||||
env: dict | None = None,
|
||||
) -> dict:
|
||||
client = Client(args.base_url, timeout=REQUEST_TIMEOUT_SEC)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
base = client.base
|
||||
recorder = Recorder()
|
||||
stop = threading.Event()
|
||||
threads = [
|
||||
threading.Thread(
|
||||
target=worker,
|
||||
args=(base, alice["token"], chat["id"], n, recorder, stop),
|
||||
daemon=True,
|
||||
)
|
||||
for n in range(args.workers)
|
||||
]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
time.sleep(args.before / 2)
|
||||
stats = container_stats()
|
||||
time.sleep(args.before / 2)
|
||||
|
||||
instances_before = sorted(
|
||||
{a.instance for a in recorder.attempts if a.ok and a.instance}
|
||||
)
|
||||
recorder.mark("action_start")
|
||||
action_output = run(action, env)
|
||||
recorder.mark("action_end")
|
||||
restore_output = ""
|
||||
if restore is not None:
|
||||
time.sleep(args.outage)
|
||||
recorder.mark("restore_start")
|
||||
restore_output = run(restore)
|
||||
recorder.mark("restore_end")
|
||||
time.sleep(args.after)
|
||||
stop.set()
|
||||
for thread in threads:
|
||||
thread.join(timeout=REQUEST_TIMEOUT_SEC + 1)
|
||||
recorder.mark("end")
|
||||
|
||||
events = recorder.events
|
||||
end = events["end"]
|
||||
after_start = events.get("restore_end", events["action_end"])
|
||||
attempts = recorder.attempts
|
||||
seen_after = [
|
||||
a for a in attempts if a.ok and a.finished >= after_start and a.instance
|
||||
]
|
||||
back_in_rotation = next(
|
||||
(
|
||||
round(a.finished - after_start, 3)
|
||||
for a in sorted(seen_after, key=lambda a: a.finished)
|
||||
if a.instance not in instances_before
|
||||
),
|
||||
None,
|
||||
)
|
||||
served = sorted(
|
||||
(a for a in attempts if a.ok and a.instance), key=lambda a: a.finished
|
||||
)
|
||||
instances_seen: dict[str, list[float]] = {}
|
||||
for a in served:
|
||||
offset = round(a.finished - events["action_start"], 3)
|
||||
instances_seen.setdefault(a.instance, [offset, offset])[1] = offset
|
||||
phases = {
|
||||
"before": phase_stats(attempts, 0, events["action_start"]),
|
||||
"during": phase_stats(attempts, events["action_start"], after_start),
|
||||
"after": phase_stats(attempts, after_start, end),
|
||||
}
|
||||
target_after_stop = None
|
||||
if target is not None:
|
||||
target_after_stop = sum(
|
||||
1
|
||||
for a in attempts
|
||||
if a.ok
|
||||
and a.instance == target
|
||||
and a.finished > events["action_end"]
|
||||
and a.finished < events.get("restore_start", end)
|
||||
)
|
||||
return {
|
||||
"scenario": label,
|
||||
"target_instance": target,
|
||||
"workers": args.workers,
|
||||
"events_seconds": events,
|
||||
"instances_before": instances_before,
|
||||
"instances_after": sorted({a.instance for a in seen_after}),
|
||||
"target_responses_while_stopped": target_after_stop,
|
||||
"restored_replica_in_rotation_seconds": back_in_rotation
|
||||
if restore is not None
|
||||
else None,
|
||||
# alias → [первый, последний] успешный ответ, секунды от начала воздействия
|
||||
"instances_seen_from_action": instances_seen,
|
||||
"phases": phases,
|
||||
"recovery": recovery_stats(attempts, events["action_start"], end),
|
||||
"integrity": integrity(client, alice, chat["id"], recorder),
|
||||
"container_stats_before_action": stats,
|
||||
"action_output": action_output.splitlines(),
|
||||
"restore_output_tail": restore_output.splitlines()[-2:],
|
||||
"timeline": timeline(attempts, end),
|
||||
}
|
||||
|
||||
|
||||
def failover(args: argparse.Namespace) -> list[dict]:
|
||||
results = []
|
||||
for signal in args.signals:
|
||||
for container in api_containers():
|
||||
target = instance_of(container)
|
||||
if signal == "SIGTERM":
|
||||
action = ["docker", "stop", "--timeout", "10", container]
|
||||
else:
|
||||
action = ["docker", "kill", "--signal", "SIGKILL", container]
|
||||
label = f"{signal} {container}"
|
||||
print(f"== {label} (instance {target})", flush=True)
|
||||
result = scenario(
|
||||
args, label, action, ["./scripts/restore.sh"], target=target
|
||||
)
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
results.append(result)
|
||||
return results
|
||||
|
||||
|
||||
def rolling(args: argparse.Namespace) -> list[dict]:
|
||||
env = dict(os.environ)
|
||||
env.setdefault("APP_REVISION", f"exp-{int(time.time())}")
|
||||
print(f"== rolling update to revision {env['APP_REVISION']}", flush=True)
|
||||
result = scenario(
|
||||
args, "rolling update", ["./scripts/rolling-update.sh"], None, env=env
|
||||
)
|
||||
result["revisions_after"] = run(["./scripts/instances.sh"]).splitlines()
|
||||
print(json.dumps(brief(result), ensure_ascii=False), flush=True)
|
||||
return [result]
|
||||
|
||||
|
||||
def brief(result: dict) -> dict:
|
||||
"""Краткая строка прогона для терминала; полная версия — в --output"""
|
||||
return {
|
||||
"scenario": result["scenario"],
|
||||
"rps_ok_write": {k: v["write"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"rps_ok_read": {k: v["read"]["ok_rps"] for k, v in result["phases"].items()},
|
||||
"errors": {
|
||||
k: v["write"]["errors"] + v["read"]["errors"]
|
||||
for k, v in result["phases"].items()
|
||||
},
|
||||
"recovery": result["recovery"],
|
||||
"target_responses_while_stopped": result["target_responses_while_stopped"],
|
||||
"restored_replica_in_rotation_seconds": result[
|
||||
"restored_replica_in_rotation_seconds"
|
||||
],
|
||||
"instances_seen_from_action": result["instances_seen_from_action"],
|
||||
"integrity": result["integrity"],
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("mode", choices=["failover", "rolling"])
|
||||
parser.add_argument("--base-url", default=None)
|
||||
parser.add_argument("--workers", type=int, default=4)
|
||||
parser.add_argument("--before", type=float, default=8, help="нагрузка до отказа")
|
||||
parser.add_argument("--outage", type=float, default=10, help="реплика остановлена")
|
||||
parser.add_argument("--after", type=float, default=8, help="после восстановления")
|
||||
parser.add_argument(
|
||||
"--signals",
|
||||
nargs="+",
|
||||
default=["SIGTERM", "SIGKILL"],
|
||||
choices=["SIGTERM", "SIGKILL"],
|
||||
)
|
||||
parser.add_argument("--output", default=None, help="JSON с полными результатами")
|
||||
args = parser.parse_args()
|
||||
started = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
results = failover(args) if args.mode == "failover" else rolling(args)
|
||||
report = {
|
||||
"mode": args.mode,
|
||||
"started": started,
|
||||
"profile": {
|
||||
"workers": args.workers,
|
||||
"loop": "closed: POST message (retry same key until 201) + GET limit=1",
|
||||
"client_timeout_seconds": REQUEST_TIMEOUT_SEC,
|
||||
"phases_seconds": {
|
||||
"before": args.before,
|
||||
"outage": args.outage,
|
||||
"after": args.after,
|
||||
},
|
||||
},
|
||||
"runs": results,
|
||||
}
|
||||
if args.output:
|
||||
destination = ROOT / args.output
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
||||
print(f"saved: {args.output}")
|
||||
failed = [
|
||||
r["scenario"]
|
||||
for r in results
|
||||
if r["integrity"]["confirmed_missing_or_changed"]
|
||||
or r["integrity"]["duplicate_ids"]
|
||||
or r["integrity"]["duplicate_texts"]
|
||||
or r["target_responses_while_stopped"]
|
||||
]
|
||||
if failed:
|
||||
raise SystemExit(f"FAIL: потеря/дубль или отказ не изолирован: {failed}")
|
||||
print(f"PASS: прогонов {len(results)}, подтверждённые сообщения целы, дублей нет")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Операторский вид API-реплик: контейнер ↔ alias X-Instance-Id ↔ ревизия образа.
|
||||
# Нужен, чтобы показать, какой реальный контейнер обслужил запрос или остановлен:
|
||||
# заголовок сам по себе не доказывает разные процессы.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' CONTAINER STATE INSTANCE PID REVISION
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -a -q api); do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
state=$(docker inspect -f '{{.State.Status}}{{if .State.Health}}/{{.State.Health.Status}}{{end}}' "$container")
|
||||
pid=$(docker inspect -f '{{.State.Pid}}' "$container")
|
||||
revision=$(docker inspect -f '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$container")
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || echo -)
|
||||
printf '%-24s %-28s %-14s %-8s %s\n' "${name#/}" "$state" "$alias" "$pid" "$revision"
|
||||
done
|
||||
+6
-4
@@ -1,9 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Перезапуск API и всех используемых хранилищ (PostgreSQL, Valkey) с сохранением
|
||||
# volumes: никаких down -v, flush или повторного seed. Готовность ждёт тест.
|
||||
# Перезапуск всего стенда с сохранением volumes: хранилища (PostgreSQL, Valkey),
|
||||
# затем все API-реплики и балансировщик. Никаких down -v, flush или повторного
|
||||
# seed; migrate не запускается — схема уже в volume. Готовность ждёт тест.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" restart postgres valkey
|
||||
docker compose --env-file "$ENV_FILE" restart api
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps postgres valkey
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps api
|
||||
docker compose --env-file "$ENV_FILE" restart --no-deps nginx
|
||||
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Возврат API-реплик к числу API_REPLICAS после stop-one.sh: запуск остановленных
|
||||
# контейнеров и ожидание healthy. Работающие реплики не пересоздаются
|
||||
# (--no-recreate), хранилища, migrate и LB не перезапускаются.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
|
||||
docker compose --env-file "$ENV_FILE" up -d --no-deps --no-recreate --wait --wait-timeout 60 api
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Поочерёдная замена API-реплик новой сборкой без остановки обслуживания.
|
||||
# 1. build нового образа (ревизия видна в метке образа и журнале старта);
|
||||
# 2. migrate однократно: миграции только расширяют схему, старые реплики
|
||||
# продолжают работать на новой схеме;
|
||||
# 3. для каждой старой реплики: +1 новая → ждать healthy → пауза на обновление
|
||||
# DNS в nginx → graceful stop старой (SIGTERM, LB уводит трафик) → удалить.
|
||||
# Во время замены реплик на одну больше, чем API_REPLICAS: ресурс заложен заранее.
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
COMPOSE="docker compose --env-file $ENV_FILE"
|
||||
APP_REVISION="${APP_REVISION:-$(git rev-parse --short HEAD 2>/dev/null || echo dev)-$(date +%s)}"
|
||||
export APP_REVISION
|
||||
# ID образа зависит только от содержимого (без provenance-attestation)
|
||||
export BUILDX_NO_DEFAULT_ATTESTATIONS=1
|
||||
# Больше resolver valid=2s в nginx: новая реплика уже в upstream до остановки старой
|
||||
LB_DNS_REFRESH_SEC=3
|
||||
STOP_TIMEOUT_SEC=10
|
||||
|
||||
log() {
|
||||
echo "[$(date +%H:%M:%S)] $*"
|
||||
}
|
||||
|
||||
log "build revision $APP_REVISION"
|
||||
$COMPOSE build --quiet api
|
||||
log "migrate"
|
||||
$COMPOSE run --rm --no-deps migrate >/dev/null
|
||||
|
||||
old_containers=$($COMPOSE ps -q api)
|
||||
for container in $old_containers; do
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
running=$($COMPOSE ps -q api | wc -l | tr -d ' ')
|
||||
log "scale api to $((running + 1)), wait healthy"
|
||||
$COMPOSE up -d --no-deps --no-recreate --wait --wait-timeout 60 \
|
||||
--scale "api=$((running + 1))" api
|
||||
sleep "$LB_DNS_REFRESH_SEC"
|
||||
log "graceful stop ${name#/}"
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
docker rm "$container" >/dev/null
|
||||
done
|
||||
log "done: all API replicas run revision $APP_REVISION"
|
||||
+78
-57
@@ -1,8 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
"""SIGTERM: процесс перестаёт принимать запросы и завершается не дольше чем за 10 секунд.
|
||||
"""SIGTERM одной API-реплики за балансировщиком.
|
||||
|
||||
Во время проверки фоновый клиент непрерывно шлёт GET /health/live: до сигнала
|
||||
ответы 200, после — отказ в соединении; ответов 5xx быть не должно.
|
||||
Реплика перестаёт принимать соединения, дожидается текущих запросов и
|
||||
завершается не дольше чем за 10 секунд; LB уводит трафик на другие реплики.
|
||||
Во время проверки фоновые клиенты непрерывно шлют GET /health/live через LB:
|
||||
ошибок (5xx/обрыв) быть не должно, после выхода реплики её X-Instance-Id
|
||||
в ответах не появляется.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -20,106 +23,124 @@ DEFAULT_BASE_URL = json.loads((ROOT / "lab.json").read_text())["default_base_url
|
||||
|
||||
SHUTDOWN_LIMIT_SEC = 10
|
||||
PROBE_INTERVAL_SEC = 0.02
|
||||
PROBERS = 4
|
||||
|
||||
|
||||
class Prober(threading.Thread):
|
||||
"""Фоновый клиент: каждое обращение — новое соединение"""
|
||||
"""Фоновый клиент через LB: время ответа и обслужившая реплика"""
|
||||
|
||||
def __init__(self, url: str) -> None:
|
||||
super().__init__(daemon=True)
|
||||
self.url = url
|
||||
self.stop = threading.Event()
|
||||
self.ok: list[float] = []
|
||||
self.refused: list[float] = []
|
||||
self.server_errors = 0
|
||||
self.ok: list[tuple[float, str]] = []
|
||||
self.errors: list[tuple[float, str]] = []
|
||||
|
||||
def run(self) -> None:
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
with urllib.request.urlopen(self.url, timeout=2) as response:
|
||||
if response.status == 200:
|
||||
self.ok.append(time.monotonic())
|
||||
self.ok.append(
|
||||
(time.monotonic(), response.headers.get("X-Instance-Id", ""))
|
||||
)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code >= 500:
|
||||
self.server_errors += 1
|
||||
except OSError:
|
||||
self.refused.append(time.monotonic())
|
||||
self.errors.append((time.monotonic(), f"HTTP {error.code}"))
|
||||
except OSError as error:
|
||||
self.errors.append((time.monotonic(), type(error).__name__))
|
||||
time.sleep(PROBE_INTERVAL_SEC)
|
||||
|
||||
|
||||
def docker(*args: str) -> str:
|
||||
result = subprocess.run(
|
||||
["docker", *args], check=True, capture_output=True, text=True
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def first_api_container() -> str:
|
||||
ids = docker("compose", "--env-file", ".env", "ps", "-q", "api").split()
|
||||
if not ids:
|
||||
raise SystemExit("FAIL: нет запущенных API-реплик")
|
||||
return docker("inspect", "-f", "{{.Name}}", ids[0]).lstrip("/")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--base-url", default=os.environ.get("BASE_URL") or DEFAULT_BASE_URL
|
||||
)
|
||||
parser.add_argument(
|
||||
"--container", default=os.environ.get("CONTAINER", "messenger-lab3-api-1")
|
||||
)
|
||||
parser.add_argument("--container", default=os.environ.get("CONTAINER"))
|
||||
args = parser.parse_args()
|
||||
os.chdir(ROOT)
|
||||
container = args.container or first_api_container()
|
||||
instance = docker("exec", container, "cat", "/tmp/instance-id")
|
||||
|
||||
logs = subprocess.Popen(
|
||||
["docker", "logs", "--follow", "--since", "0s", args.container],
|
||||
["docker", "logs", "--follow", "--since", "0s", container],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
)
|
||||
waiter = subprocess.Popen(
|
||||
["docker", "wait", args.container], stdout=subprocess.PIPE, text=True
|
||||
["docker", "wait", container], stdout=subprocess.PIPE, text=True
|
||||
)
|
||||
prober = Prober(args.base_url.rstrip("/") + "/health/live")
|
||||
prober.start()
|
||||
probers = [
|
||||
Prober(args.base_url.rstrip("/") + "/health/live") for _ in range(PROBERS)
|
||||
]
|
||||
for prober in probers:
|
||||
prober.start()
|
||||
time.sleep(1)
|
||||
if not prober.ok:
|
||||
raise SystemExit("FAIL: API не отвечает до отправки SIGTERM")
|
||||
before = {alias for prober in probers for _, alias in prober.ok}
|
||||
if instance not in before:
|
||||
raise SystemExit("FAIL: целевая реплика не обслуживает запросы до SIGTERM")
|
||||
|
||||
sent_at = time.monotonic()
|
||||
subprocess.run(
|
||||
["docker", "kill", "--signal", "SIGTERM", args.container],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
docker("kill", "--signal", "SIGTERM", container)
|
||||
exit_code = int(waiter.communicate(timeout=SHUTDOWN_LIMIT_SEC + 5)[0].strip())
|
||||
exited_at = time.monotonic()
|
||||
time.sleep(0.5)
|
||||
prober.stop.set()
|
||||
prober.join()
|
||||
time.sleep(1)
|
||||
for prober in probers:
|
||||
prober.stop.set()
|
||||
prober.join()
|
||||
logs.terminate()
|
||||
shutdown_log = logs.communicate(timeout=5)[0]
|
||||
|
||||
shutdown_sec = exited_at - sent_at
|
||||
ok_after = [t for t in prober.ok if t > sent_at]
|
||||
first_refused = min((t for t in prober.refused if t > sent_at), default=None)
|
||||
print("Логи контейнера после SIGTERM:")
|
||||
ok_after = [(t, a) for p in probers for t, a in p.ok if t > sent_at]
|
||||
errors_after = [(t, e) for p in probers for t, e in p.errors if t > sent_at]
|
||||
target_after_exit = [t for t, a in ok_after if a == instance and t > exited_at]
|
||||
last_target = max((t for t, a in ok_after if a == instance), default=None)
|
||||
print("Журнал реплики после SIGTERM:")
|
||||
print(
|
||||
"\n".join(line for line in shutdown_log.splitlines() if "/health/" not in line)
|
||||
)
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"container": args.container,
|
||||
"exit_code": exit_code,
|
||||
"shutdown_seconds": round(shutdown_sec, 3),
|
||||
"limit_seconds": SHUTDOWN_LIMIT_SEC,
|
||||
"probes_ok_before_sigterm": len(prober.ok) - len(ok_after),
|
||||
"probes_ok_after_sigterm": len(ok_after),
|
||||
"first_refused_after_sigterm_seconds": None
|
||||
if first_refused is None
|
||||
else round(first_refused - sent_at, 3),
|
||||
"probes_refused_after_sigterm": len(prober.refused),
|
||||
"server_errors_5xx": prober.server_errors,
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
summary = {
|
||||
"container": container,
|
||||
"instance": instance,
|
||||
"exit_code": exit_code,
|
||||
"shutdown_seconds": round(exited_at - sent_at, 3),
|
||||
"limit_seconds": SHUTDOWN_LIMIT_SEC,
|
||||
"instances_before": sorted(before),
|
||||
"probes_ok_after_sigterm": len(ok_after),
|
||||
"instances_after_sigterm": sorted({a for _, a in ok_after}),
|
||||
"last_target_response_after_sigterm_seconds": None
|
||||
if last_target is None
|
||||
else round(last_target - sent_at, 3),
|
||||
"target_responses_after_exit": len(target_after_exit),
|
||||
"errors_after_sigterm": len(errors_after),
|
||||
"error_kinds": sorted({e for _, e in errors_after}),
|
||||
}
|
||||
print(json.dumps(summary, indent=2, ensure_ascii=False))
|
||||
if (
|
||||
exit_code != 0
|
||||
or shutdown_sec > SHUTDOWN_LIMIT_SEC
|
||||
or prober.server_errors
|
||||
or first_refused is None
|
||||
or exited_at - sent_at > SHUTDOWN_LIMIT_SEC
|
||||
or errors_after
|
||||
or target_after_exit
|
||||
or not ok_after
|
||||
):
|
||||
raise SystemExit("FAIL: завершение по SIGTERM не соответствует требованиям")
|
||||
print(
|
||||
"PASS: SIGTERM завершил процесс за %.2f с, новые соединения отклоняются"
|
||||
% shutdown_sec
|
||||
"PASS: SIGTERM завершил %s за %.2f с, LB обслужил %d запросов без ошибок"
|
||||
% (container, exited_at - sent_at, len(ok_after))
|
||||
)
|
||||
|
||||
|
||||
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Остановка ОДНОЙ API-реплики, обслужившей тест: TARGET_INSTANCE_ID → контейнер.
|
||||
# Каждый процесс API пишет свой alias (X-Instance-Id) в /tmp/instance-id.
|
||||
# docker stop: SIGTERM → graceful shutdown; restart-политика unless-stopped
|
||||
# не поднимает вручную остановленный контейнер, он остаётся остановленным.
|
||||
# БД, LB и volumes не затрагиваются. Восстановление — scripts/restore.sh.
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
cd "$(dirname "$0")/.."
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
# Меньше 10 с лимита hook; больше drain + graceful shutdown (1 + 5 с)
|
||||
STOP_TIMEOUT_SEC=8
|
||||
|
||||
for container in $(docker compose --env-file "$ENV_FILE" ps -q api); do
|
||||
alias=$(docker exec "$container" cat /tmp/instance-id 2>/dev/null || true)
|
||||
if [ "$alias" = "$TARGET_INSTANCE_ID" ]; then
|
||||
name=$(docker inspect -f '{{.Name}}' "$container")
|
||||
docker stop --timeout "$STOP_TIMEOUT_SEC" "$container" >/dev/null
|
||||
echo "stopped ${name#/} (instance $alias)" >&2
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
echo "no running API replica with instance $TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user