fix: stream docker events during failover phases
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+40
-18
@@ -1387,6 +1387,34 @@ class Stand:
|
|||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def container_events(
|
||||||
|
self,
|
||||||
|
container: str,
|
||||||
|
path: Path,
|
||||||
|
) -> subprocess.Popen[bytes]:
|
||||||
|
"""Поток событий Docker одного контейнера (часы ВМ) в файл `path`;
|
||||||
|
формат — `failover.parse_events`.
|
||||||
|
|
||||||
|
Поток, запущенный до нагрузки фазы, а не `docker events --since`
|
||||||
|
после неё: демон хранит только последние 256 событий всех
|
||||||
|
контейнеров, а проверки здоровья и `docker exec` оркестратора (по
|
||||||
|
три события на вызов) вытесняют их (design change
|
||||||
|
`fix-known-issues`, решение 9)."""
|
||||||
|
with path.open("w") as out:
|
||||||
|
return subprocess.Popen(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"events",
|
||||||
|
"--filter",
|
||||||
|
f"container={container}",
|
||||||
|
"--format",
|
||||||
|
"{{.TimeNano}} {{.Action}} "
|
||||||
|
'{{index .Actor.Attributes "signal"}}',
|
||||||
|
],
|
||||||
|
stdout=out,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
)
|
||||||
|
|
||||||
def app_containers(self) -> list[dict[str, Any]]:
|
def app_containers(self) -> list[dict[str, Any]]:
|
||||||
"""Реплики `api` (и остановленные): имя, `X-Instance-Id`, тег и
|
"""Реплики `api` (и остановленные): имя, `X-Instance-Id`, тег и
|
||||||
digest конфигурации образа. В хранилище containerd у тега и у
|
digest конфигурации образа. В хранилище containerd у тега и у
|
||||||
@@ -2378,21 +2406,6 @@ class Stand:
|
|||||||
self.env = env
|
self.env = env
|
||||||
self.wait_ready()
|
self.wait_ready()
|
||||||
|
|
||||||
def events(self, container: str, since: float, until: float) -> str:
|
|
||||||
"""События Docker контейнера за окно: время — часы ВМ."""
|
|
||||||
return sh(
|
|
||||||
"docker",
|
|
||||||
"events",
|
|
||||||
"--since",
|
|
||||||
f"{since:.3f}",
|
|
||||||
"--until",
|
|
||||||
f"{until:.3f}",
|
|
||||||
"--filter",
|
|
||||||
f"container={container}",
|
|
||||||
"--format",
|
|
||||||
'{{.TimeNano}} {{.Action}} {{index .Actor.Attributes "signal"}}',
|
|
||||||
)
|
|
||||||
|
|
||||||
def logs(self, container: str, since: float) -> str:
|
def logs(self, container: str, since: float) -> str:
|
||||||
"""Журнал контейнера с отметками Docker (часы ВМ) с момента
|
"""Журнал контейнера с отметками Docker (часы ВМ) с момента
|
||||||
`since`; HAProxy и PostgreSQL пишут в stdout и stderr."""
|
`since`; HAProxy и PostgreSQL пишут в stdout и stderr."""
|
||||||
@@ -4396,6 +4409,11 @@ class Bench:
|
|||||||
)
|
)
|
||||||
duration = p.failover_before_s + p.failover_down_s + p.failover_after_s
|
duration = p.failover_before_s + p.failover_down_s + p.failover_after_s
|
||||||
log_name = f"{phase}.log"
|
log_name = f"{phase}.log"
|
||||||
|
# события цели — потоком с начала фазы, не из буфера демона после
|
||||||
|
# неё (решение 9 design change fix-known-issues)
|
||||||
|
events_path = stand.work_dir / f"{phase}-events.log"
|
||||||
|
stream = stand.container_events(target_id, events_path)
|
||||||
|
try:
|
||||||
started = time.time()
|
started = time.time()
|
||||||
load = stand.k6_console(
|
load = stand.k6_console(
|
||||||
"failover.js",
|
"failover.js",
|
||||||
@@ -4422,7 +4440,9 @@ class Bench:
|
|||||||
}[mode]
|
}[mode]
|
||||||
sh("docker", *command, target_id)
|
sh("docker", *command, target_id)
|
||||||
self.say(f"phase {phase}: {mode} {instance} ({slot})")
|
self.say(f"phase {phase}: {mode} {instance} ({slot})")
|
||||||
time.sleep(max(0.0, acted + p.failover_down_s - time.monotonic()))
|
time.sleep(
|
||||||
|
max(0.0, acted + p.failover_down_s - time.monotonic()),
|
||||||
|
)
|
||||||
restore = ("unpause",) if mode == "pause" else ("start",)
|
restore = ("unpause",) if mode == "pause" else ("start",)
|
||||||
sh("docker", *restore, target_id)
|
sh("docker", *restore, target_id)
|
||||||
self.say(f"phase {phase}: {restore[0]} {instance}")
|
self.say(f"phase {phase}: {restore[0]} {instance}")
|
||||||
@@ -4458,14 +4478,16 @@ class Bench:
|
|||||||
raise BenchError(
|
raise BenchError(
|
||||||
f"phase {phase}: replica not back: {exc}",
|
f"phase {phase}: replica not back: {exc}",
|
||||||
) from exc
|
) from exc
|
||||||
ended = time.time()
|
finally:
|
||||||
|
stream.terminate()
|
||||||
|
stream.wait()
|
||||||
down = stand.down_storages()
|
down = stand.down_storages()
|
||||||
if down:
|
if down:
|
||||||
raise BenchError(
|
raise BenchError(
|
||||||
f"{', '.join(down)} went down during phase {phase}",
|
f"{', '.join(down)} went down during phase {phase}",
|
||||||
)
|
)
|
||||||
events = failover.parse_events(
|
events = failover.parse_events(
|
||||||
stand.events(target_id, started - 5, ended + 1),
|
events_path.read_text(encoding="utf-8", errors="replace"),
|
||||||
)
|
)
|
||||||
action = next(
|
action = next(
|
||||||
(e for e in events if e.action == failover.ACTIONS[mode]),
|
(e for e in events if e.action == failover.ACTIONS[mode]),
|
||||||
|
|||||||
Reference in New Issue
Block a user