Check that the balancer drops every replica while PostgreSQL is down

The stack test for a stopped PostgreSQL asked for liveness 200 through
the balancer, which only held while the balancer had not noticed the
outage yet. Now the protected endpoints and readiness through the LB
must answer 503 unavailable within 2 s, liveness is checked inside a
replica with docker exec, the balancer must report zero UP servers
within 2 s and answer liveness itself with 503 in the contract error
format, the request id echoed and no X-Instance-Id. After PostgreSQL
starts again every replica must be back in rotation without a restart.

Stand helpers read the number of UP servers from the HAProxy stats,
excluding the backend summary row, and reach a replica by container
id, which survives the renumbering after a rolling update.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 19:52:29 +03:00
co-authored by Claude Opus 5.5
parent 572204950a
commit 3584501de8
2 changed files with 50 additions and 6 deletions
+31 -1
View File
@@ -1,13 +1,15 @@
import asyncio
import csv
import os
import subprocess
import time
from base64 import b64encode
from collections.abc import Awaitable
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]
@@ -55,6 +57,25 @@ def started_at(service: str) -> str:
).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()}
@@ -98,5 +119,14 @@ async def wait_ready(http: AsyncClient, seconds: float = 60) -> float:
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} с")