forked from mtusi2026/mtusi2026lab3
ЛР 3 уровень 4: drain при SIGTERM, лимиты ресурсов, нагрузка с отказами, rolling update
This commit is contained in:
@@ -12,8 +12,8 @@ import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
IMAGE = os.environ.get("IMAGE", "mtusi-messenger:lab2")
|
||||
NETWORK = os.environ.get("COMPOSE_NETWORK", "mtusi-lab2_default")
|
||||
IMAGE = os.environ.get("IMAGE", "mtusi-messenger:lab3")
|
||||
NETWORK = os.environ.get("COMPOSE_NETWORK", "mtusi-lab3_default")
|
||||
PORT = int(os.environ.get("SIGTERM_PORT", "18080"))
|
||||
|
||||
|
||||
@@ -55,6 +55,15 @@ def connection_rejected(base_url):
|
||||
except (urllib.error.URLError, ConnectionError, TimeoutError):
|
||||
return True
|
||||
|
||||
def not_ready(base_url):
|
||||
# Во время drain /health/ready отвечает 503 (или соединение уже закрыто)
|
||||
try:
|
||||
with urllib.request.urlopen(base_url + "/health/ready", timeout=0.5):
|
||||
return False
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code == 503
|
||||
except (urllib.error.URLError, ConnectionError, TimeoutError):
|
||||
return True
|
||||
|
||||
def test_shutdown(incomplete_request):
|
||||
name = "mtusi-messenger-sigterm-" + uuid.uuid4().hex[:8]
|
||||
@@ -82,24 +91,24 @@ def test_shutdown(incomplete_request):
|
||||
raise AssertionError(f"Expected 100 Continue, received {response!r}")
|
||||
pending.sendall(b"{")
|
||||
|
||||
print(f"Scenario: {scenario}; docker stop --timeout 10 {name}", flush=True)
|
||||
print(f"Scenario: {scenario}; docker stop --timeout 20 {name}", flush=True)
|
||||
start = time.monotonic()
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
|
||||
stopped = executor.submit(run, "docker", "stop", "--timeout", "10", name)
|
||||
stopped = executor.submit(run, "docker", "stop", "--timeout", "20", name)
|
||||
deadline = start + 2
|
||||
while not connection_rejected(base_url):
|
||||
while not not_ready(base_url):
|
||||
if time.monotonic() >= deadline:
|
||||
raise AssertionError("New connections are still accepted during shutdown")
|
||||
raise AssertionError("Readiness still OK after SIGTERM")
|
||||
time.sleep(0.05)
|
||||
rejected_after = time.monotonic() - start
|
||||
stopped.result(timeout=12)
|
||||
stopped.result(timeout=25)
|
||||
elapsed = time.monotonic() - start
|
||||
|
||||
exit_code = int(run("docker", "inspect", name, "--format", "{{.State.ExitCode}}").stdout)
|
||||
if exit_code != 0:
|
||||
raise AssertionError(f"Container exited with code {exit_code}, expected 0")
|
||||
if elapsed > 10:
|
||||
raise AssertionError(f"Shutdown exceeded 10 seconds: {elapsed:.3f}s")
|
||||
if elapsed > 15:
|
||||
raise AssertionError(f"Shutdown exceeded 15 seconds: {elapsed:.3f}s")
|
||||
if not connection_rejected(base_url):
|
||||
raise AssertionError("API still accepts connections after shutdown")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user