Files
mtusi2026lab4/research/rolling_update.py
T

58 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Уровень 4: rolling update под нагрузкой. Нагрузка идёт через LB 60 с; на t=10 запускается scripts/rolling-update.sh.
Проверяем: ошибки клиента (после повторов с тем же ключом), потерянные/задублированные сообщения, смену instance id."""
import argparse
import asyncio
import json
import os
import subprocess
import sys
import threading
import time
sys.path.insert(0, os.path.dirname(__file__))
import loadgen # noqa: E402
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
async def main(args):
stand = loadgen.Stand("http://127.0.0.1:8080")
await stand.setup()
rec = loadgen.Recorder()
info = {}
def update():
time.sleep(max(0, rec.t0 + 10 - time.time()))
info["start"] = round(time.time() - rec.t0, 2)
out = subprocess.run(["sh", "scripts/rolling-update.sh", args.revision], cwd=ROOT, capture_output=True, text=True)
info["end"] = round(time.time() - rec.t0, 2)
info["script_exit"] = out.returncode
info["script_output"] = out.stdout.strip().splitlines()[-6:]
threading.Thread(target=update, daemon=True).start()
await loadgen.run_load(stand, rec, args.seconds, args.concurrency)
await asyncio.sleep(1)
check = await loadgen.verify(stand, rec)
instances_before = {e[4] for e in rec.events if e[1] == "POST" and e[0] < info["start"] and e[4]}
instances_during = {e[4] for e in rec.events if e[1] == "POST" and info["start"] <= e[0] <= info["end"] and e[4]}
instances_after = {e[4] for e in rec.events if e[1] == "POST" and e[0] > info["end"] + 2 and e[4]}
report = {"date": time.strftime("%Y-%m-%d %H:%M:%S"), "update": info, "revision": args.revision,
"instances_before": sorted(instances_before), "instances_during": sorted(instances_during),
"instances_after": sorted(instances_after), "all_replicas_replaced": not (instances_before & instances_after),
"before": loadgen.summarize(rec, 2, info["start"]), "during": loadgen.summarize(rec, info["start"], info["end"]),
"after": loadgen.summarize(rec, info["end"] + 2, None), "total": loadgen.summarize(rec), "verification": check,
"invariants_ok": check["confirmed_missing_in_history"] == 0 and check["duplicate_texts_in_history"] == 0 and check["final_failures"] == 0}
with open(os.path.join(args.out, "rolling_update.json"), "w", encoding="utf-8") as fh:
json.dump(report, fh, ensure_ascii=False, indent=2)
print(json.dumps(report, ensure_ascii=False, indent=2))
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--out", default="evidence")
ap.add_argument("--seconds", type=int, default=60)
ap.add_argument("--concurrency", type=int, default=16)
ap.add_argument("--revision", default="rolling-%d" % int(time.time()))
asyncio.run(main(ap.parse_args()))