#!/usr/bin/env python3 """LB balancing under mixed traffic: slow uploads interleaved with short GETs. Each of 8 uploads (~1 MiB PNG) streams its body over ~4 s; between upload starts the client sends one short GET. Records which replica served every upload and GET. The balance algorithm is read from the running LB config. Results are merged into evidence/lb-interleave.json keyed by algorithm. Tokens are kept in memory only. """ from __future__ import annotations import base64 import collections import http.client import json import os import subprocess import sys import threading import time import uuid import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "student-tests")) import imagegen # noqa: E402 HOST = os.environ.get("LB_HOST", "127.0.0.1") PORT = int(os.environ.get("LB_PORT", "8080")) UPLOADS = 8 def call(method, path, body=None, token=None, headers=None): h = {"Accept": "application/json", **(headers or {})} data = None if body is not None: data = json.dumps(body).encode() h["Content-Type"] = "application/json" if token: h["Authorization"] = "Bearer " + token req = urllib.request.Request(f"http://{HOST}:{PORT}{path}", data=data, headers=h, method=method) resp = urllib.request.urlopen(req, timeout=30) return resp.headers, json.loads(resp.read() or b"null") def actor(label): username, password = f"ilv_{label}_{uuid.uuid4().hex[:10]}", "T3st-" + uuid.uuid4().hex _, user = call("POST", "/api/v1/users", {"username": username, "display_name": label, "password": password}) cred = base64.b64encode(f"{username}:{password}".encode()).decode() _, session = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}) return user["id"], session["token"] def balance_algorithm(): cfg = subprocess.check_output( ["docker", "compose", "--env-file", ".env", "exec", "-T", "lb", "cat", "/usr/local/etc/haproxy/haproxy.cfg"], cwd=ROOT, text=True) algos = {line.split()[1] for line in cfg.splitlines() if line.strip().startswith("balance ")} return "/".join(sorted(algos)) def main(): algo = balance_algorithm() alice, token = actor("a") bob, _ = actor("b") _, chat = call("POST", "/api/v1/chats", {"title": "interleave", "member_ids": [alice, bob]}, token=token) payload = imagegen.rgb_png(700, 500, noise=True) uploads, gets = [], [] def slow_upload(i): conn = http.client.HTTPConnection(HOST, PORT, timeout=60) conn.putrequest("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=ilv{i}.png") for k, v in {"Authorization": "Bearer " + token, "Content-Type": "image/png", "Content-Length": str(len(payload))}.items(): conn.putheader(k, v) conn.endheaders() step = len(payload) // 20 for off in range(0, len(payload), step): conn.send(payload[off:off + step]) time.sleep(0.2) resp = conn.getresponse() resp.read() uploads.append({"upload": i, "status": resp.status, "instance": resp.getheader("X-Instance-Id")}) threads = [] for i in range(UPLOADS): th = threading.Thread(target=slow_upload, args=(i,)) th.start() threads.append(th) time.sleep(0.15) gets.append(call("GET", "/api/v1/users/me", token=token)[0]["X-Instance-Id"]) time.sleep(0.15) for th in threads: th.join() result = { "date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "balance": algo, "uploads": UPLOADS, "upload_bytes": len(payload), "uploads_per_replica": dict(collections.Counter(u["instance"] for u in uploads)), "upload_statuses": dict(collections.Counter(str(u["status"]) for u in uploads)), "short_gets_per_replica": dict(collections.Counter(gets)), } out = ROOT / "evidence" / "lb-interleave.json" report = json.loads(out.read_text(encoding="utf-8")) if out.exists() else {} report[algo] = result out.write_text(json.dumps(report, ensure_ascii=False, indent=1), encoding="utf-8") print(json.dumps(result, ensure_ascii=False)) print("WROTE", out) if __name__ == "__main__": main()