forked from mtusi2026/mtusi2026lab4
Bring the system changes of lab 3 from 61c0c28..72a1e57 onto the lab 4 code, resolving each conflict by meaning and keeping everything lab 4 added: - a message is posted with one SQL statement under the chat row lock, and the attachment links go into the same statement; a foreign key violation on the attachment link still answers 404 and leaves nothing behind. Text and the attachment list are checked before the write, an outsider gets 404 for both, as before the carry over; - the request pipeline counts the body bytes actually received against the limit of the route, so a chunked body over 1 MiB gets 413 and the upload route keeps its MAX_UPLOAD_BYTES limit; - a request cancelled at shutdown gets 503 unavailable in the contract format, the worker waits a second less than the rest of the shutdown budget, SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks 7 s; - readiness sits behind a Readiness port with the draining flag in shared infrastructure, and the drain window is logged through Logger; - logout revokes through SessionRevocations and withdraws the mark when PostgreSQL rejects the delete; an empty cursor is 400; - EventBus.project is gone, the sweep test subscribes and waits on a queue; domain tests check attachment_list instead of Chat.post; - make up waits for every replica in the balancer, BASE_URL follows the lb port, the image is tagged by the compose project, retry-check and faults-evidence are added, detection runs three times, failover-each and rolling-update record the stand usage, CI checks numeric uids. down also takes the tools and reconcile profiles of lab 4. Track 1 tooling of lab 3 (scaling.py, its samplers, tools/ and the CPU pinning of api, lb, valkey and k6) stays out; stand-usage.sh queries the database size itself instead of the track 1 sampler. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
137 lines
4.4 KiB
Python
137 lines
4.4 KiB
Python
import asyncio
|
||
import csv
|
||
import os
|
||
import subprocess
|
||
import time
|
||
from base64 import b64encode
|
||
from collections.abc import Awaitable, Callable
|
||
from pathlib import Path
|
||
from typing import Any
|
||
from uuid import uuid4
|
||
|
||
import httpx
|
||
from httpx import AsyncClient, Response, TransportError
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
||
PASSWORD = "Пароль-для-стенда-01"
|
||
BASIC = 'Basic realm="messenger", charset="UTF-8"'
|
||
BEARER = 'Bearer realm="messenger"'
|
||
VALKEY_AUTH = 'REDISCLI_AUTH="$(sed -n "s/^requirepass //p" /run/secrets/valkey.conf)"'
|
||
|
||
|
||
def compose(*args: str, stdin: str | None = None) -> str:
|
||
done = subprocess.run(
|
||
["docker", "compose", *args],
|
||
cwd=ROOT,
|
||
check=True,
|
||
capture_output=True,
|
||
text=True,
|
||
input=stdin,
|
||
)
|
||
return done.stdout.strip()
|
||
|
||
|
||
def restart_stack() -> None:
|
||
subprocess.run(["sh", "scripts/restart.sh"], cwd=ROOT, check=True, capture_output=True)
|
||
|
||
|
||
def psql(sql: str, **variables: str) -> str:
|
||
assigned = [item for name, value in variables.items() for item in ("-v", f"{name}={value}")]
|
||
command = ["exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-tA"]
|
||
return compose(*command, *assigned, stdin=sql)
|
||
|
||
|
||
def valkey(command: str) -> str:
|
||
return compose("exec", "-T", "valkey", "sh", "-c", f"{VALKEY_AUTH} valkey-cli {command}")
|
||
|
||
|
||
def started_at(service: str) -> str:
|
||
return subprocess.run(
|
||
[
|
||
"docker",
|
||
"inspect",
|
||
"--format",
|
||
"{{.State.StartedAt}}",
|
||
*compose("ps", "-q", service).split(),
|
||
],
|
||
check=True,
|
||
capture_output=True,
|
||
text=True,
|
||
).stdout.strip()
|
||
|
||
|
||
def inside_replica(path: str) -> int:
|
||
replica = compose("ps", "-q", "api").split()[0]
|
||
probe = f"import urllib.request as u;print(u.urlopen('http://127.0.0.1:8080{path}').status)"
|
||
done = subprocess.run(
|
||
["docker", "exec", replica, "python", "-c", probe],
|
||
capture_output=True,
|
||
text=True,
|
||
check=False,
|
||
)
|
||
return int(done.stdout.strip() or 0)
|
||
|
||
|
||
def lb_up() -> int:
|
||
stats = httpx.get(f"http://{compose('port', 'lb', '8404')}/stats;csv", timeout=2).text
|
||
rows = csv.DictReader(stats.lstrip("# ").splitlines())
|
||
servers = (r for r in rows if r["pxname"] == "api" and r["svname"] != "BACKEND")
|
||
return sum(1 for r in servers if r["status"].startswith("UP"))
|
||
|
||
|
||
def basic(username: str, password: str = PASSWORD) -> dict[str, str]:
|
||
return {"Authorization": "Basic " + b64encode(f"{username}:{password}".encode()).decode()}
|
||
|
||
|
||
def bearer(token: str) -> dict[str, str]:
|
||
return {"Authorization": f"Bearer {token}"}
|
||
|
||
|
||
def new_user(label: str) -> dict[str, str]:
|
||
return {
|
||
"username": f"u_{label}_{uuid4().hex[:12]}",
|
||
"display_name": f"Стенд {label}",
|
||
"password": PASSWORD,
|
||
}
|
||
|
||
|
||
async def enroll(http: AsyncClient, label: str) -> tuple[dict[str, Any], str]:
|
||
body = new_user(label)
|
||
created = await http.post("/api/v1/users", json=body)
|
||
assert created.status_code == 201, created.text
|
||
session = await http.post("/api/v1/auth/sessions", headers=basic(body["username"]))
|
||
assert session.status_code == 201, session.text
|
||
return created.json(), session.json()["token"]
|
||
|
||
|
||
async def timed(call: Awaitable[Response]) -> tuple[Response, float]:
|
||
started = time.perf_counter()
|
||
response = await call
|
||
return response, time.perf_counter() - started
|
||
|
||
|
||
async def wait_ready(http: AsyncClient, seconds: float = 60) -> float:
|
||
started = time.perf_counter()
|
||
while time.perf_counter() - started < seconds:
|
||
try:
|
||
if (await http.get("/health/ready")).status_code == 200:
|
||
return time.perf_counter() - started
|
||
except TransportError:
|
||
pass
|
||
await asyncio.sleep(0.25)
|
||
raise AssertionError("стенд не вернулся в готовность")
|
||
|
||
|
||
async def waited(condition: Callable[[], bool], seconds: float = 30) -> float:
|
||
started = time.perf_counter()
|
||
while time.perf_counter() - started < seconds:
|
||
if condition():
|
||
return time.perf_counter() - started
|
||
await asyncio.sleep(0.05)
|
||
raise AssertionError("условие не выполнилось за отведённое время")
|
||
|
||
|
||
def report(event: str, response: Response, elapsed: float) -> None:
|
||
print(f"{event}: {response.status_code} за {elapsed:.3f} с")
|