mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 22:37:15 +08:00
变更: - 内存协调模式从持久化记录发现无主轮次,并复核租约保护活跃执行 - 先以状态和 fencing token 完成失败转换,再持久化并发布连续编号的终态事件 - 增加重启恢复、活跃租约保护、竞争处理及 Redis 事件保留回归测试 验证: - 相关协调、后台恢复、应用及订阅测试:101 项 PASS,含四 worker/1000 轮次压力测试 - 新增 Redis 恢复用例后的定向复测:14 项 PASS,与上述范围重叠 - 修改文件的适用 pre-commit 检查、架构检查、git diff --check:PASS - Chromium SIGKILL 实测:无需刷新或新提交,自动显示失败和重试入口 - 用户在独立测试实例复测:旧轮次自动标记 worker_lost,前端显示重试 Fixes #2034
127 lines
5.0 KiB
Python
127 lines
5.0 KiB
Python
"""Leader-only recovery of turns whose owner lease has been lost."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from deeptutor.services.session.protocol import TurnRepository
|
|
|
|
from .protocol import RuntimeCoordinator
|
|
from .types import TurnFailureCode, TurnStatus
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class TurnRecoveryService:
|
|
def __init__(
|
|
self,
|
|
coordinator: RuntimeCoordinator,
|
|
repository: TurnRepository,
|
|
) -> None:
|
|
self.coordinator = coordinator
|
|
self.repository = repository
|
|
self.backlog = 0
|
|
|
|
async def recover_once(self) -> int:
|
|
turn_ids = await self.coordinator.list_expired_turn_ids()
|
|
if self.coordinator.mode == "memory":
|
|
# A process restart loses the in-memory lease index entirely. The
|
|
# durable active rows are then the only way to discover old turns.
|
|
# Admission acquires its lease before inserting the row; the lease
|
|
# check in _recover_turn protects both new and parked live turns.
|
|
persisted = await self.repository.list_nonterminal_turns()
|
|
candidates = set(turn_ids)
|
|
for turn in persisted:
|
|
turn_id = str(turn["id"])
|
|
if await self.coordinator.get_lease(turn_id) is None:
|
|
candidates.add(turn_id)
|
|
turn_ids = sorted(candidates)
|
|
self.backlog = len(turn_ids)
|
|
recovered = 0
|
|
for turn_id in turn_ids:
|
|
try:
|
|
outcome = await self._recover_turn(turn_id)
|
|
# An expired id can belong to a different user repository.
|
|
# Do not globally acknowledge it until the leader finds the
|
|
# repository that owns the turn.
|
|
if outcome is None:
|
|
continue
|
|
if outcome:
|
|
recovered += 1
|
|
await self.coordinator.acknowledge_expired_turn(turn_id)
|
|
except Exception:
|
|
logger.exception("Failed to recover expired turn %s", turn_id)
|
|
self.backlog = max(0, self.backlog - recovered)
|
|
return recovered
|
|
|
|
async def _recover_turn(self, turn_id: str) -> bool | None:
|
|
turn = await self.repository.get_turn(turn_id)
|
|
if turn is None:
|
|
return None
|
|
if turn.get("status") not in {
|
|
TurnStatus.QUEUED.value,
|
|
TurnStatus.RUNNING.value,
|
|
TurnStatus.WAITING_INPUT.value,
|
|
}:
|
|
return False
|
|
# Recheck at recovery time, not just when collecting expired IDs.
|
|
# None also prevents acknowledging a candidate whose lease is live.
|
|
if await self.coordinator.get_lease(turn_id) is not None:
|
|
return None
|
|
token = int(turn.get("fencing_token") or 0)
|
|
# Preserve any events still available from an expired worker before
|
|
# allocating terminal sequence numbers from the durable journal.
|
|
pending = await self.coordinator.read_events(turn_id)
|
|
if pending:
|
|
await self.repository.append_events(turn_id, pending, fencing_token=token)
|
|
transitioned = await self.repository.transition_turn(
|
|
turn_id,
|
|
TurnStatus.FAILED.value,
|
|
expected_status=str(turn["status"]),
|
|
fencing_token=token,
|
|
error="The worker executing this turn was lost; regenerate to retry",
|
|
failure_code=TurnFailureCode.WORKER_LOST.value,
|
|
retryable=True,
|
|
)
|
|
if not transitioned:
|
|
return False
|
|
# Persist first: a fresh coordinator starts at seq=1, which would
|
|
# collide with pre-crash events and be ignored by resumed clients.
|
|
events = await self.repository.append_events(
|
|
turn_id,
|
|
[
|
|
{
|
|
"type": "error",
|
|
"source": "turn_recovery",
|
|
"stage": "recovery",
|
|
"content": "The worker executing this turn was lost; regenerate to retry",
|
|
"metadata": {
|
|
"turn_terminal": True,
|
|
"status": TurnStatus.FAILED.value,
|
|
"error_code": TurnFailureCode.WORKER_LOST.value,
|
|
"retryable": True,
|
|
},
|
|
"session_id": turn.get("session_id", ""),
|
|
},
|
|
{
|
|
"type": "done",
|
|
"source": "turn_recovery",
|
|
"stage": "recovery",
|
|
"content": "",
|
|
"metadata": {
|
|
"status": TurnStatus.FAILED.value,
|
|
"error_code": TurnFailureCode.WORKER_LOST.value,
|
|
"retryable": True,
|
|
},
|
|
"session_id": turn.get("session_id", ""),
|
|
},
|
|
],
|
|
fencing_token=token,
|
|
)
|
|
for event in events:
|
|
await self.coordinator.publish_event(turn_id, event)
|
|
return True
|
|
|
|
|
|
__all__ = ["TurnRecoveryService"]
|