fix(ops): align host pressure evidence routing
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Successful in 52s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled

This commit is contained in:
Your Name
2026-07-02 13:06:53 +08:00
parent c6422fb6fe
commit 3f656a5246
4 changed files with 91 additions and 12 deletions

View File

@@ -276,26 +276,34 @@ def top_docker_containers(samples: list[dict[str, Any]], *, host: str, top_n: in
def recommend_playbook(process_families: list[dict[str, Any]], containers: list[dict[str, Any]]) -> str:
top_container = containers[0] if containers else {}
top_container_name = str(top_container.get("container_name") or "").lower()
top_container_cpu = float(top_container.get("cpu_cores") or 0.0)
top_family = process_families[0] if process_families else {}
family = str(top_family.get("family") or "")
family_cpu = {
str(item.get("family") or ""): float(item.get("cpu_percent") or 0.0)
for item in process_families
}
container_cpu = {
str(item.get("container_name") or "").lower(): float(item.get("cpu_cores") or 0.0)
for item in containers
}
stock_container_cpu = max(
[
cpu
for name, cpu in container_cpu.items()
if "stockplatform-v2-postgres-1" in name
or name == "stockplatform-v2-api-1"
or ("postgres" in name and "gitea" not in name)
]
or [0.0]
)
gitea_container_cpu = max(
[cpu for name, cpu in container_cpu.items() if "gitea" in name] or [0.0]
)
if "gitea" in top_container_name and top_container_cpu >= 1.0:
return "gitea_queue_or_hook_backlog_playbook"
if (
(
"postgres" in top_container_name
or "stockplatform-v2-postgres-1" in top_container_name
)
and top_container_cpu >= 1.0
) or family_cpu.get("postgres", 0.0) >= 50.0:
if stock_container_cpu >= 1.0 or family_cpu.get("postgres", 0.0) >= 50.0:
return "postgres_hot_query_or_backup_export_playbook"
if gitea_container_cpu >= 1.0:
return "gitea_queue_or_hook_backlog_playbook"
if family_cpu.get("gitea_service", 0.0) >= 50.0:
return "gitea_queue_or_hook_backlog_playbook"
if family in {"docker_build", "web_build", "gitea_actions_runner"}:

View File

@@ -961,6 +961,54 @@ def test_sustained_load_evidence_prioritizes_hot_gitea_container_over_control_pl
assert "/home/wooo/gitea/app.ini" not in result.stdout
def test_sustained_load_evidence_aligns_stock_postgres_priority_with_controller(
tmp_path: Path,
) -> None:
ps_file = tmp_path / "ps.txt"
ps_file.write_text(
"\n".join(
[
"100 1 100 78516 79.8 0.5 postgres postgres: stockplatform SELECT",
"200 1 200 78513 53.1 1.3 gitea /usr/local/bin/gitea web --config /home/wooo/gitea/app.ini",
]
),
encoding="utf-8",
)
docker_file = tmp_path / "docker.prom"
docker_file.write_text(
"\n".join(
[
'docker_container_cpu_cores{host="110",container_name="gitea"} 1.6725',
'docker_container_cpu_cores{host="110",container_name="stockplatform-v2-postgres-1"} 1.6317',
]
),
encoding="utf-8",
)
result = subprocess.run(
[
sys.executable,
str(EVIDENCE_PATH),
"--host",
"110",
"--ps-file",
str(ps_file),
"--docker-stats-file",
str(docker_file),
"--json",
],
check=True,
capture_output=True,
text=True,
)
payload = json.loads(result.stdout)
assert payload["recommendation"] == "postgres_hot_query_or_backup_export_playbook"
assert payload["top_containers"][0]["container_name"] == "gitea"
assert payload["top_process_families"][0]["family"] == "postgres"
assert "/home/wooo/gitea/app.ini" not in result.stdout
def test_sustained_load_evidence_keeps_stale_container_samples_untrusted(tmp_path: Path) -> None:
metrics_file = tmp_path / "host.prom"
metrics_file.write_text(