chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests, bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts, Dockerfile, compose, pyproject and uv lock - merge Makefile, .gitignore and .dockerignore (the lab 3 versions only add lines to the skeleton, which is the same in lab 4) and .env.example (lab 4 skeleton names listed in the header) - rename the compose project to mtusi-lab4 and the image to :lab4 so the stand does not reuse the lab 3 containers and volumes - update AGENTS.md for lab 4: current lab, origin commit, contract and test files of the new skeleton Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Executable
+429
@@ -0,0 +1,429 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Hook стенда отказов для проверок `fault` (`make failure_checks`).
|
||||
|
||||
Тесты вызывают его как `[FAULT_HOOK, действие, аргументы…]` без shell —
|
||||
так же `tests/resilience.py` вызывает `ACTION_SCRIPT`. Сами тесты docker
|
||||
не трогают. Hook работает только с проектом compose из `FAULT_PROJECT`,
|
||||
его задаёт `student-tests/run_failures.py`. Проект рабочего стенда hook
|
||||
не трогает. Решение 3 design change `failure-checks`.
|
||||
|
||||
Действия:
|
||||
stop|start|pause|unpause postgres|valkey
|
||||
flush-cache FLUSHALL
|
||||
cache-hits keyspace_hits из INFO stats
|
||||
cache-has-chat <chat_id> EXISTS ключа карточки: 0 или 1
|
||||
fail-members <title> вставка участников чата с этим названием
|
||||
падает (триггер в базе стенда отказов)
|
||||
clear-fail-members убрать триггер
|
||||
fail-messages <text> вставка сообщения с этим текстом падает
|
||||
(триггер; ключ идемпотентности к этому
|
||||
моменту уже вставлен в той же транзакции)
|
||||
clear-fail-messages убрать триггер
|
||||
count-chats <title> число чатов с этим названием
|
||||
fill-connections <секунд> занять все слоты max_connections сессиями
|
||||
psql (`SELECT pg_sleep`) и оборвать
|
||||
соединения API: их пулы переподключаются
|
||||
и получают «too many clients» (53300)
|
||||
clear-connections освободить слоты и дождаться подключения
|
||||
db-sessions pg_stat_database.sessions базы стенда:
|
||||
сколько сессий PostgreSQL приняла за всё
|
||||
время (учитывает и сессию самого вызова)
|
||||
|
||||
Пароли не печатает: valkey-cli берёт пароль из VALKEYCLI_AUTH
|
||||
контейнера, psql работает от POSTGRES_USER контейнера. Только stdlib.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMPOSE_FILES = (
|
||||
ROOT / "docker-compose.yml",
|
||||
ROOT / "student-tests" / "failures.compose.yml",
|
||||
)
|
||||
PROJECT_SUFFIX = "-failures"
|
||||
SERVICES = ("postgres", "valkey")
|
||||
# та же проверка, что в healthcheck сервиса в docker-compose.yml. У
|
||||
# PostgreSQL стенда отказов healthcheck переопределён на базу postgres
|
||||
# (failures.compose.yml), а проба после start / unpause ходит в базу
|
||||
# приложения и добавляет в её pg_stat_database одну сессию: тест «Соединения
|
||||
# пула не переоткрываются» start в своём окне не вызывает
|
||||
PROBES = {
|
||||
"postgres": 'pg_isready -q -h 127.0.0.1 -U "$POSTGRES_USER" '
|
||||
'-d "$POSTGRES_DB"',
|
||||
"valkey": 'test "$(valkey-cli -p "$VALKEY_PORT" ping)" = PONG',
|
||||
}
|
||||
PROBE_TIMEOUT_S = 30
|
||||
# начало ключа карточки — копия CACHE_KEY_PREFIX из src/utils/constants.py:
|
||||
# hook на stdlib и код приложения не импортирует. Меняется формат ключа —
|
||||
# менять и здесь, иначе упадут test_flush_cache и test_valkey_back
|
||||
CACHE_KEY_PREFIX = "messenger:v1"
|
||||
|
||||
FAIL_MEMBERS_SQL = """\
|
||||
CREATE OR REPLACE FUNCTION fault_fail_members() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF (SELECT title FROM chats WHERE id = NEW.chat_id) = TG_ARGV[0] THEN
|
||||
RAISE EXCEPTION 'fault injected: chat members insert';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
DROP TRIGGER IF EXISTS fault_fail_members ON chat_members;
|
||||
CREATE TRIGGER fault_fail_members BEFORE INSERT ON chat_members
|
||||
FOR EACH ROW EXECUTE FUNCTION fault_fail_members(:'title');
|
||||
"""
|
||||
CLEAR_FAIL_MEMBERS_SQL = """\
|
||||
DROP TRIGGER IF EXISTS fault_fail_members ON chat_members;
|
||||
DROP FUNCTION IF EXISTS fault_fail_members();
|
||||
"""
|
||||
FAIL_MESSAGES_SQL = """\
|
||||
CREATE OR REPLACE FUNCTION fault_fail_messages() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NEW.text = TG_ARGV[0] THEN
|
||||
RAISE EXCEPTION 'fault injected: message insert';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
DROP TRIGGER IF EXISTS fault_fail_messages ON messages;
|
||||
CREATE TRIGGER fault_fail_messages BEFORE INSERT ON messages
|
||||
FOR EACH ROW EXECUTE FUNCTION fault_fail_messages(:'title');
|
||||
"""
|
||||
CLEAR_FAIL_MESSAGES_SQL = """\
|
||||
DROP TRIGGER IF EXISTS fault_fail_messages ON messages;
|
||||
DROP FUNCTION IF EXISTS fault_fail_messages();
|
||||
"""
|
||||
COUNT_CHATS_SQL = "SELECT count(*) FROM chats WHERE title = :'title';\n"
|
||||
# Сессия psql учитывает себя в sessions, только когда сбросит свои
|
||||
# счётчики: обычно на первом простое до запроса, но без ожидания
|
||||
# блокировки, и при занятой — позже. Принудительный сброс в отдельной
|
||||
# транзакции делает это детерминированным: значение всегда включает
|
||||
# сессию самого вызова
|
||||
DB_SESSIONS_SQL = """\
|
||||
SELECT pg_stat_force_next_flush();
|
||||
SELECT sessions FROM pg_stat_database WHERE datname = current_database();
|
||||
"""
|
||||
|
||||
# Заполнение слотов: отвязанный sh в контейнере Postgres. Сначала столько
|
||||
# попыток, сколько слотов, затем новая попытка раз в 0,05 с до конца
|
||||
# срока: слот, освободившийся после обрыва соединения API, сразу занимает
|
||||
# снова psql, а не переподключение пула. Лишние попытки получают отказ и
|
||||
# выходят. Сессии — через сокет ([local]), пользователь контейнера.
|
||||
# После CLEAR_FILL_SH фоновые psql — сироты пространства PID: их подбирает
|
||||
# init стенда отказов (init: true у postgres в failures.compose.yml), а не
|
||||
# postmaster, для которого выход чужого потомка с кодом 2 — авария
|
||||
FILL_PID = "/tmp/fault-fill.pid"
|
||||
FILL_SH = f"""\
|
||||
echo $$ > {FILL_PID}
|
||||
hold() {{
|
||||
PGAPPNAME=fault_fill psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -XAtq \\
|
||||
-c "SELECT pg_sleep($1)" >/dev/null 2>&1 &
|
||||
}}
|
||||
end=$(( $(date +%s) + $1 ))
|
||||
slots=$(psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -XAtq \\
|
||||
-c 'SHOW max_connections')
|
||||
i=0
|
||||
while [ "$i" -lt "$slots" ]; do hold "$1"; i=$((i + 1)); done
|
||||
while [ "$(date +%s)" -lt "$end" ]; do hold "$1"; sleep 0.05; done
|
||||
"""
|
||||
# Обрыв соединений по TCP (это пулы API) сигналом SIGTERM — то же, что
|
||||
# pg_terminate_backend, но без своего слота. Заголовок процесса backend —
|
||||
# «postgres: <user> <db> <адрес>(<порт>) <состояние>»; сокет ([local]) и
|
||||
# проверку готовности с 127.0.0.1 не трогаем. Печатает число оборванных.
|
||||
KILL_TCP_SH = """\
|
||||
n=0
|
||||
for d in /proc/[0-9]*; do
|
||||
t=$(tr '\\0' ' ' < "$d/cmdline" 2>/dev/null) || continue
|
||||
case "$t" in
|
||||
"postgres: "*" 127.0.0.1("*) ;;
|
||||
"postgres: "*"("*")"*) kill -TERM "${d#/proc/}" 2>/dev/null \\
|
||||
&& n=$((n + 1)) ;;
|
||||
esac
|
||||
done
|
||||
echo "$n"
|
||||
"""
|
||||
CLEAR_FILL_SH = f"""\
|
||||
[ -f {FILL_PID} ] && kill "$(cat {FILL_PID})" 2>/dev/null
|
||||
rm -f {FILL_PID}
|
||||
for d in /proc/[0-9]*; do
|
||||
t=$(tr '\\0' ' ' < "$d/cmdline" 2>/dev/null) || continue
|
||||
case "$t" in
|
||||
"postgres: "*" [local] "*) kill -TERM "${{d#/proc/}}" 2>/dev/null ;;
|
||||
esac
|
||||
done
|
||||
"""
|
||||
PROBE_SQL_SH = (
|
||||
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -XAtq -c "SELECT 1"'
|
||||
)
|
||||
TOO_MANY = "too many clients"
|
||||
FILL_TIMEOUT_S = 20
|
||||
|
||||
|
||||
class HookError(Exception):
|
||||
"""Неверный вызов или проект: код 2 и сообщение в stderr."""
|
||||
|
||||
|
||||
def working_project() -> str:
|
||||
"""Имя проекта рабочего стенда — из docker-compose.yml без `-p`."""
|
||||
result = subprocess.run(
|
||||
["docker", "compose", "config", "--format", "json"],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
return json.loads(result.stdout)["name"]
|
||||
|
||||
|
||||
def fault_project() -> str:
|
||||
project = os.environ.get("FAULT_PROJECT", "")
|
||||
if not project.endswith(PROJECT_SUFFIX):
|
||||
raise HookError(
|
||||
f"FAULT_PROJECT must end with {PROJECT_SUFFIX!r}: "
|
||||
"run make failure_checks",
|
||||
)
|
||||
if project == working_project():
|
||||
raise HookError("FAULT_PROJECT is the working stand, refusing")
|
||||
return project
|
||||
|
||||
|
||||
def compose_run(
|
||||
project: str,
|
||||
*args: str,
|
||||
stdin: str | None = None,
|
||||
) -> subprocess.CompletedProcess[str]:
|
||||
files = [arg for path in COMPOSE_FILES for arg in ("-f", str(path))]
|
||||
return subprocess.run(
|
||||
["docker", "compose", "-p", project, *files, *args],
|
||||
cwd=ROOT,
|
||||
input=stdin,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
|
||||
def compose(project: str, *args: str, stdin: str | None = None) -> str:
|
||||
result = compose_run(project, *args, stdin=stdin)
|
||||
if result.returncode != 0:
|
||||
sys.stderr.write(result.stderr)
|
||||
raise SystemExit(result.returncode)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def wait_answers(project: str, name: str) -> str:
|
||||
"""Ждать, пока сервис ответит своей проверкой из healthcheck.
|
||||
|
||||
После `unpause` Docker до следующего запуска healthcheck (до 5 с)
|
||||
показывает `unhealthy`, и `up --wait` сразу выходит с ошибкой, хотя
|
||||
сервис уже отвечает. Поэтому проверка — напрямую.
|
||||
"""
|
||||
deadline = time.monotonic() + PROBE_TIMEOUT_S
|
||||
while time.monotonic() < deadline:
|
||||
probe = compose_run(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
name,
|
||||
"sh",
|
||||
"-c",
|
||||
PROBES[name],
|
||||
)
|
||||
if probe.returncode == 0:
|
||||
return ""
|
||||
time.sleep(0.2)
|
||||
raise HookError(f"{name} does not answer in {PROBE_TIMEOUT_S}s")
|
||||
|
||||
|
||||
def valkey(project: str, *args: str) -> str:
|
||||
# аргументы — позиционные параметры sh, без подстановки в строку
|
||||
return compose(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
"valkey",
|
||||
"sh",
|
||||
"-c",
|
||||
'exec valkey-cli -p "$VALKEY_PORT" "$@"',
|
||||
"sh",
|
||||
*args,
|
||||
)
|
||||
|
||||
|
||||
def psql(project: str, sql: str, *, title: str = "") -> str:
|
||||
return compose(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
'exec psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -XAtq '
|
||||
'-v ON_ERROR_STOP=1 -v title="$1"',
|
||||
"sh",
|
||||
title,
|
||||
stdin=sql,
|
||||
)
|
||||
|
||||
|
||||
def service(name: str) -> str:
|
||||
if name not in SERVICES:
|
||||
raise HookError(f"service must be one of {SERVICES}")
|
||||
return name
|
||||
|
||||
|
||||
def wait_healthy(project: str, name: str) -> str:
|
||||
# тот же контейнер, без пересоздания; --wait ждёт его healthcheck
|
||||
return compose(project, "up", "-d", "--wait", "--no-recreate", name)
|
||||
|
||||
|
||||
def cache_hits(project: str) -> str:
|
||||
for line in valkey(project, "INFO", "stats").splitlines():
|
||||
if line.startswith("keyspace_hits:"):
|
||||
return line.split(":", 1)[1].strip()
|
||||
raise HookError("keyspace_hits not found in INFO stats")
|
||||
|
||||
|
||||
def cache_has_chat(project: str, chat_id: str) -> str:
|
||||
database = compose(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"printenv",
|
||||
"POSTGRES_DB",
|
||||
)
|
||||
key = f"{CACHE_KEY_PREFIX}:{database}:chat:{uuid.UUID(chat_id)}"
|
||||
return valkey(project, "EXISTS", key)
|
||||
|
||||
|
||||
def postgres_sh(project: str, script: str, *args: str) -> str:
|
||||
return compose(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
script,
|
||||
"sh",
|
||||
*args,
|
||||
)
|
||||
|
||||
|
||||
def probe_connect(project: str) -> subprocess.CompletedProcess[str]:
|
||||
"""Новое подключение psql и `SELECT 1`."""
|
||||
return compose_run(
|
||||
project,
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
PROBE_SQL_SH,
|
||||
)
|
||||
|
||||
|
||||
def slots_full(project: str) -> bool:
|
||||
"""Новое подключение получает «too many clients»."""
|
||||
probe = probe_connect(project)
|
||||
return probe.returncode != 0 and TOO_MANY in probe.stderr
|
||||
|
||||
|
||||
def fill_connections(project: str, seconds: str) -> str:
|
||||
"""Все слоты заняты psql, соединений API не осталось.
|
||||
|
||||
Готово, когда новое подключение получает отказ, а обрывать уже
|
||||
некого: пул API, переподключаясь, получит 53300.
|
||||
"""
|
||||
if int(seconds) <= 0:
|
||||
raise HookError("seconds must be positive")
|
||||
compose(
|
||||
project,
|
||||
"exec",
|
||||
"-d",
|
||||
"postgres",
|
||||
"sh",
|
||||
"-c",
|
||||
FILL_SH,
|
||||
"sh",
|
||||
str(int(seconds)),
|
||||
)
|
||||
deadline = time.monotonic() + FILL_TIMEOUT_S
|
||||
while time.monotonic() < deadline:
|
||||
if slots_full(project) and postgres_sh(project, KILL_TCP_SH) == "0":
|
||||
return ""
|
||||
time.sleep(0.1)
|
||||
raise HookError(f"connections are not filled in {FILL_TIMEOUT_S}s")
|
||||
|
||||
|
||||
def clear_connections(project: str) -> str:
|
||||
deadline = time.monotonic() + FILL_TIMEOUT_S
|
||||
while time.monotonic() < deadline:
|
||||
postgres_sh(project, CLEAR_FILL_SH)
|
||||
if probe_connect(project).returncode == 0:
|
||||
return ""
|
||||
time.sleep(0.2)
|
||||
raise HookError(f"connections are not freed in {FILL_TIMEOUT_S}s")
|
||||
|
||||
|
||||
def run(project: str, action: str, args: list[str]) -> str:
|
||||
"""Выполнить действие; синтаксис без match: hook запускают и 3.9."""
|
||||
if action in ("stop", "pause") and len(args) == 1:
|
||||
return compose(project, action, service(args[0]))
|
||||
if action == "start" and len(args) == 1:
|
||||
return wait_healthy(project, service(args[0]))
|
||||
if action == "unpause" and len(args) == 1:
|
||||
compose(project, "unpause", service(args[0]))
|
||||
return wait_answers(project, args[0])
|
||||
if action == "flush-cache" and not args:
|
||||
return valkey(project, "FLUSHALL")
|
||||
if action == "cache-hits" and not args:
|
||||
return cache_hits(project)
|
||||
if action == "cache-has-chat" and len(args) == 1:
|
||||
return cache_has_chat(project, args[0])
|
||||
if action == "fail-members" and len(args) == 1:
|
||||
return psql(project, FAIL_MEMBERS_SQL, title=args[0])
|
||||
if action == "clear-fail-members" and not args:
|
||||
return psql(project, CLEAR_FAIL_MEMBERS_SQL)
|
||||
if action == "fail-messages" and len(args) == 1:
|
||||
# аргумент триггера передаётся той же переменной psql, что title
|
||||
return psql(project, FAIL_MESSAGES_SQL, title=args[0])
|
||||
if action == "clear-fail-messages" and not args:
|
||||
return psql(project, CLEAR_FAIL_MESSAGES_SQL)
|
||||
if action == "count-chats" and len(args) == 1:
|
||||
return psql(project, COUNT_CHATS_SQL, title=args[0])
|
||||
if action == "fill-connections" and len(args) == 1:
|
||||
return fill_connections(project, args[0])
|
||||
if action == "clear-connections" and not args:
|
||||
return clear_connections(project)
|
||||
if action == "db-sessions" and not args:
|
||||
return psql(project, DB_SESSIONS_SQL)
|
||||
raise HookError(f"unknown action or arguments: {action} {args}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if len(sys.argv) < 2:
|
||||
print(__doc__, file=sys.stderr)
|
||||
return 2
|
||||
try:
|
||||
output = run(fault_project(), sys.argv[1], sys.argv[2:])
|
||||
except (HookError, ValueError) as e:
|
||||
print(f"fault_hook: {e}", file=sys.stderr)
|
||||
return 2
|
||||
if output:
|
||||
print(output)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user