fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性

This commit is contained in:
yml
2026-11-29 14:10:24 +08:00
parent be7146a9ef
commit d12d9e02b3
51 changed files with 2510 additions and 561 deletions
+46 -16
View File
@@ -100,6 +100,10 @@ class ExecutionJobWorker:
self._inflight: dict[str, int] = {}
self._owner = execution_worker_owner()
self._event_writer = ExecutionEventBatchWriter()
self._last_poll_at: str | None = None
self._last_claim_error: str | None = None
self._last_claim_error_at: str | None = None
self._total_claimed = 0
def start(self) -> bool:
with self._lock:
@@ -159,23 +163,27 @@ class ExecutionJobWorker:
DEFAULT_POLL_SECONDS,
)
while not self._stop.is_set():
# 回收已完成的 job,同时把 lane 的在途计数减回来。
for future in [item for item in futures if item.done()]:
lane = futures.pop(future)
self._inflight[lane] = max(0, self._inflight.get(lane, 0) - 1)
try:
future.result()
except Exception: # noqa: BLE001, S110 - 结果已落库,这里只消费异常
pass
executors = self._executors
if not executors:
break
slots = max_jobs - sum(self._inflight.values())
now_iso = datetime.now(UTC).isoformat()
with self._lock:
self._last_poll_at = now_iso
# 回收已完成的 job,同时把 lane 的在途计数减回来。
for future in [item for item in futures if item.done()]:
lane = futures.pop(future)
self._inflight[lane] = max(0, self._inflight.get(lane, 0) - 1)
try:
future.result()
except Exception: # noqa: BLE001, S110 - 结果已落库,这里只消费异常
pass
executors = dict(self._executors)
if not executors:
break
slots = max_jobs - sum(self._inflight.values())
inflight_snapshot = dict(self._inflight)
if slots > 0:
# 履约优先:先把 fulfillment 的槽填满,登录只用剩下的。
# 登录批次等邮箱验证码时可能持槽 25 分钟,绝不能把履约饿死。
for lane in _lane_claim_order():
lane_free = lane_limits.get(lane, 0) - self._inflight.get(lane, 0)
lane_free = lane_limits.get(lane, 0) - inflight_snapshot.get(lane, 0)
take = min(slots, lane_free)
if take <= 0:
continue
@@ -185,7 +193,12 @@ class ExecutionJobWorker:
owner=self._owner,
limit=take,
)
except Exception: # noqa: BLE001 - 消费循环必须存活
with self._lock:
self._last_claim_error = None
except Exception as exc: # noqa: BLE001 - 消费循环必须存活
with self._lock:
self._last_claim_error = f"{type(exc).__name__}: {exc}"[:500]
self._last_claim_error_at = now_iso
logger.exception("领取执行任务失败 lane={}", lane)
jobs = []
executor = executors.get(lane)
@@ -196,7 +209,10 @@ class ExecutionJobWorker:
future = executor.submit(self._run_job, job.id)
except RuntimeError:
break
self._inflight[lane] = self._inflight.get(lane, 0) + 1
with self._lock:
self._inflight[lane] = self._inflight.get(lane, 0) + 1
self._total_claimed += 1
inflight_snapshot[lane] = inflight_snapshot.get(lane, 0) + 1
futures[future] = lane
slots -= len(jobs)
if futures:
@@ -384,16 +400,30 @@ def stop_execution_job_worker() -> None:
def execution_job_worker_status() -> dict[str, Any]:
thread = execution_job_worker._thread
with execution_job_worker._lock:
thread = execution_job_worker._thread
inflight = dict(execution_job_worker._inflight)
last_poll_at = execution_job_worker._last_poll_at
last_claim_error = execution_job_worker._last_claim_error
last_claim_error_at = execution_job_worker._last_claim_error_at
total_claimed = execution_job_worker._total_claimed
event_writer = execution_job_worker._event_writer.status()
return {
"running": bool(thread and thread.is_alive()),
"lanes": list(_lane_scopes()),
"lane_slots": _lane_slot_limits(),
"inflight": inflight,
"inflight_total": sum(inflight.values()),
"max_jobs": _env_positive_int(
"EXECUTION_WORKER_MAX_JOBS",
DEFAULT_EXECUTION_WORKERS,
),
"owner": execution_job_worker._owner,
"total_claimed": total_claimed,
"last_poll_at": last_poll_at,
"last_claim_error": last_claim_error,
"last_claim_error_at": last_claim_error_at,
"event_writer": event_writer,
}