feat(lps): 알림 룰 4종 추가 — 데드라인·검색원가·포트 고갈·예산 누수
협의로 선정한 조기 신호 4종을 AlertManager 에 추가한다. - deadline: 최근 1h JobDeadlineExceeded 수 ≥ LPS_ALERT_DEADLINE_1H(5). 재시도로 살아나면 dead 룰엔 안 잡히는 크롤 행 반복 신호를 별도 집계. - cost: 최근 1h 완료 잡 검색원가 합 ≥ LPS_ALERT_COST_1H_USD(1.0). 비용의 87%가 프록시 대역폭 — 리소스차단 풀림·재시도 루프의 조용한 비용 폭주를 감시. job.result 의 metrics.cost.total_usd JSONB 합산. - proxy_ports_low: 가용 포트 비율 ≤ LPS_ALERT_PORTS_LOW_PCT(30%). 쿨다운 격리 누적 — blocks_1h(80건)보다 먼저 우는 대규모 차단 조기 신호. 워커별 프록시 중 가장 소진된 것 기준(min). - budget_leak: 최근 6h end_reason=block 세션 ≥ LPS_ALERT_BLOCK_SESSIONS_6H(1). 요청 예산(3회)을 지켰는데도 차단됨 = 예산 하향 검토 신호. - deadline_1h·cost_1h_usd 는 queue.ops() 에 편입 → /v1/lps/ops 로도 노출. 포트·세션 지표는 워커 웹훅 스냅샷에 포함(프록시 상태는 워커에만 있음). - 테스트 4건 추가(ops 집계 2·포트 스냅샷 2), 전체 145 passed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7d2c87fbe
commit
ca7f057e41
6 files changed
+109
-6
No files matched your search
@@ -97,3 +97,31 @@ def test_pool_status_shape():
|
||||
assert set(st) == {"checked_out", "capacity", "pct"}
|
||||
assert st["capacity"] > 0 # R/W 2엔진 × (pool+overflow)
|
||||
assert 0 <= st["pct"] <= 100
|
||||
|
||||
|
||||
# ---- 가용 프록시 포트 현황(포트 고갈 룰의 데이터) --------------------------
|
||||
|
||||
def test_proxy_ports_snapshot_min_across_workers():
|
||||
from config.config_models import DecodoConfig
|
||||
from services.search.proxy import DecodoProxy
|
||||
from worker_main import _proxy_ports_snapshot
|
||||
|
||||
def _proxy():
|
||||
return DecodoProxy(DecodoConfig(host="h", username="u", password="p",
|
||||
port_start=10001, port_end=10010, session_minutes=10))
|
||||
|
||||
class _Ad:
|
||||
def __init__(self, proxy):
|
||||
self._proxy = proxy
|
||||
|
||||
p1, p2 = _proxy(), _proxy()
|
||||
p2.mark_burned(10001)
|
||||
p2.mark_burned(10002)
|
||||
avail, total = _proxy_ports_snapshot([_Ad(p1), _Ad(p2), object()]) # 프록시 없는 어댑터 혼재 OK
|
||||
assert (avail, total) == (8, 10) # 가장 소진된 워커(p2) 기준
|
||||
|
||||
|
||||
def test_proxy_ports_snapshot_none_without_proxy():
|
||||
from worker_main import _proxy_ports_snapshot
|
||||
assert _proxy_ports_snapshot([object()]) is None
|
||||
assert _proxy_ports_snapshot(None) is None
|
||||
@@ -91,3 +91,26 @@ def test_backoff_is_exponential_capped():
|
||||
assert compute_backoff(2, base=5) == 10
|
||||
assert compute_backoff(3, base=5) == 20
|
||||
assert compute_backoff(100, base=5, cap=600) == 600
|
||||
|
||||
|
||||
async def test_ops_counts_deadline_and_cost(q):
|
||||
# 완료 잡 2건의 검색원가 합산 — claim 반환 잡을 완료(순서 의존 제거)
|
||||
for code, cost in (("b", 0.01), ("c", 0.02)):
|
||||
await q.enqueue(JobType.SEARCH.value, {"q": code})
|
||||
job = await q.claim("w1")
|
||||
await q.complete(job["job_id"], "w1", {"metrics": {"cost": {"total_usd": cost}}})
|
||||
# 데드라인 강제종료 — 재큐(PENDING)로 살아나도 deadline_1h 에 잡혀야 한다(dead 와 별개 축)
|
||||
j1 = await q.enqueue(JobType.SEARCH.value, {"q": "a"})
|
||||
await q.claim("w1")
|
||||
await q.fail(j1, "w1", "JobDeadlineExceeded: 300s", backoff_sec=0)
|
||||
snap = await q.ops()
|
||||
assert snap["deadline_1h"] == 1
|
||||
assert abs(snap["cost_1h_usd"] - 0.03) < 1e-9
|
||||
|
||||
|
||||
async def test_ops_cost_ignores_jobs_without_metrics(q):
|
||||
jid = await q.enqueue(JobType.SEARCH.value, {"q": "x"})
|
||||
await q.claim("w1")
|
||||
await q.complete(jid, "w1", {"count": 3}) # metrics 없는 결과 — 합산에서 무시(0)
|
||||
snap = await q.ops()
|
||||
assert snap["cost_1h_usd"] == 0
|
||||
@@ -89,6 +89,8 @@ async def test_ops_snapshot(client, clean_jobs):
|
||||
r = await client.get("/v1/lps/ops")
|
||||
assert r.status_code == 200
|
||||
j = r.json()
|
||||
for k in ("pending", "running", "done", "dead", "dead_1h", "stuck_running", "oldest_pending_sec", "blocks_1h"):
|
||||
for k in ("pending", "running", "done", "dead", "dead_1h", "stuck_running", "oldest_pending_sec",
|
||||
"blocks_1h", "deadline_1h", "pool_pct"):
|
||||
assert k in j and isinstance(j[k], int)
|
||||
assert isinstance(j["cost_1h_usd"], (int, float))
|
||||
assert j["pending"] == 1
|
||||
Reference in new issue
Block a user