Prhub

#27412 Add scripted-runtime KV-pool and lock-ref exhauster primitives

原始 PR 作者 fzyzcjy 合并时间 2026-06-06 09:07 文件变更 4 提交数 1 评论 1 代码增减 +102 / -0

执行摘要

为 scripted runtime 添加 KV 池和锁引用耗尽原语

scripted runtime 测试需要能够精确模拟 KV 缓存不足和节点锁引用被持久的场景,以验证调度器在内存压力下的行为。原有框架缺乏此类原语,本 PR 填补了这一空白。

测试团队推荐精读这两个 Exhauster 的实现,后续 chunked-prefill 测试将依赖它们。也可作为如何在 scripted 测试中模拟系统状态的参考模式。

讨论亮点

无(仅作者自合并,无公开 review 评论)

实现拆解

  1. 新增 lock_ref_exhauster.py:实现 ScriptedLockRefExhauster 类,通过遍历 radix 树找到锁引用为 0 的节点,逐个调用 inc_lock_ref 增加锁引用,直到剩余可驱逐节点数不超过 leave_refs
  2. 新增 kv_pool_exhauster.py:实现 ScriptedKvPoolExhauster 类,通过从 token 分配器中分配超过 leave_pages 的 token 数量,模拟 KV 池紧张状态。
  3. 修改 api.py:在 ScriptedContext 中初始化两个 exhauster 实例,添加 exhaust_kvexhaust_lock_refs_release_exhausted_pools 方法,将耗尽操作暴露给测试脚本。
  4. 修改 scheduler_hook.py:在 _reset_engine_state 函数开头调用 _release_exhausted_pools,确保状态重置时释放所有耗尽的资源,避免状态泄漏。
文件 模块 状态 重要度
python/sglang/test/scripted_runtime/context/lock_ref_exhauster.py 锁耗尽器 added 6.92
python/sglang/test/scripted_runtime/context/kv_pool_exhauster.py KV 池耗尽器 added 6.79
python/sglang/test/scripted_runtime/context/api.py API 集成 modified 5.92
python/sglang/test/scripted_runtime/scheduler_hook.py 调度器钩子 modified 3.32

关键符号

ScriptedLockRefExhauster.__init__ ScriptedLockRefExhauster.exhaust ScriptedLockRefExhauster.release ScriptedLockRefExhauster._evictable_nodes ScriptedKvPoolExhauster.__init__ ScriptedKvPoolExhauster.exhaust ScriptedKvPoolExhauster.release ScriptedContext.exhaust_kv ScriptedContext.exhaust_lock_refs ScriptedContext._release_exhausted_pools

分析完成后,这里会展示 LLM 生成的相对完整源码片段和详细注释。

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

变更限定在测试辅助代码内,不涉及生产路径,风险极低。但使用 ScriptedKvPoolExhauster 时需注意 leave_pages 参数过小可能导致 allocator 分配失败触发断言,影响测试稳定性。

影响范围:仅 scripted runtime 测试框架。使用者可以通过 exhaust_kvexhaust_lock_refs 在测试脚本中精确控制压力水平,提升测试的覆盖面和可重复性。

测试辅助代码 无生产路径风险

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论