本文对照 sgl-project/sglang 的 main 分支(commit f534dba,2026-11-17)逐层拆开两件事:router 怎么选出一个(或一对)engine,以及 PD 部署下一条请求如何从 bootstrap_room 走到 KV 传输完成。所有结论都标注了文件与行号;凡是没有在源码里读到的推论,一律标 推断。
sgl-router 进程(Rust,sgl-router/src),机群级入口,一个进程服务一个模型。--disaggregation-mode 固定,不是请求级开关。bootstrap_room)= router 为一次 PD 配对生成的正整数键,是 prefill 与 decode 之间唯一的关联标识。bootstrap_host/port/room、/route),以及 router 之间的 peer bootstrap(radix tree 快照同步)。本文分别写作 PD bootstrap 与 peer bootstrap,不混用。
room、把同一份请求体同时发给 prefill 和 decode;KV 由 prefill 直连 decode 传,router 全程不碰(src/server/routes/chat/forward.rs:216-244)。round_robin,reorg 路径默认 power_of_two;cache_aware 要显式选,并且必须先有前缀信号(外部 KV indexer 或进程内 radix tree)(src/config/cli.rs:250-258、src/config/cli.rs:884-890)。src/policies/selection.rs:210-330)。src/server/routes/chat/forward.rs:890-950)。decode_prefix_len 声明自己已有多少、实际 RDMA 传多少页(prefill.py:536-546、decode.py:1809-1830)。把这四类进程各自"知道什么"分清,后面每一步才不会错位。关键点是:router 只持有元数据(哈希、计数、绑定),它既没有 KV 张量,也没有缓存块的 token 内容。
| 组件 | 持有的状态 | 明确不持有 | 源码 |
|---|---|---|---|
| sgl-router | worker 注册表与角色、每条请求的 in-flight 计数、可选的前缀索引(block_hash → (worker_url, dp_rank, tier))、会话绑定、断路器状态、引擎自报负载快照 |
KV 张量、显存地址、page 内容、缓存块的 token ids(LoRA 路径例外,仅用于重算键)、每块的真实 token 数 | state/kv_events/tree.rs:489-507 policies/admission.rs:555-590 |
| prefill worker | 自己的 radix cache 与 KV pool、bootstrap HTTP server(:8998)、ZMQ 传输端点、每个 room 的 sender 状态 |
decode 的页表;它只知道 decode 发来的目标地址与 decode_prefix_len |
disaggregation/prefill.py:500-546 disagg_service.py:17-45 |
| decode worker | 自己的 KV pool 与 req_to_token_pool、可选的 decode 侧 radix cache、receiver 与 room 状态机、采样出的 token 序列 |
prefill 上某段前缀是刚算的还是缓存里的——它只按页收 | disaggregation/decode.py:1806-1830 base/conn.py:141-147 |
| KV indexer(可选) | block hash → token 数 + (worker, tier)、per-worker 缓存规格、累计命中数 |
KV 本身;它是软状态索引,无持久化 | sgl-kv-indexer/README.md:56-65 |
| HiCache / L3(可选) | 主机内存或远端存储里的 KV 页;decode 侧可由 _commit_hicache_local_restore_to_req 回填 |
路由决策;router 完全不知道 L3 里有哪一页 | decode.py:1820-1828 mem_cache/common.py:385-421 |
一句话记法:router 认识"哪个 worker 可能有这段前缀",prefill 认识"这段前缀的 KV 在哪几页",decode 认识"我要把这些页放到哪几页"。三者之间传递的全部是整数与地址,没有 token 内容。
Router 没有"队列"和"批处理"——它是 tokio 上的无状态转发器,组批发生在 engine 的 Scheduler 里。Router 侧唯一近似队列的东西,是 decode 准入用的信号量(DecodeGate)和 router 自算的 in-flight 计数。所以下面这条链是每请求一遍的,不存在攒批再发。
POST /v1/chat/completions
└─ chat::route src/server/routes/chat.rs:190
└─ prepare → prepare_now :275
├─ parse_routing_fields / PreparedRequest::openai
├─ request_tokens_for(…) policies/mod.rs:52
│ └─ 仅当 should_tokenize_request 为真 preparation.rs:1203
└─ ModelRouting::lookup chat.rs:371
├─ Legacy(policy) ← --chat-routing legacy(默认)
└─ Reorg(resolver) ← --chat-routing reorg
└─ ModelRouting::dispatch chat.rs:398
│ for attempt in 0..--retry-max-attempts (默认 1,即不重试)
├─ sleep(backoff(attempt)) chat.rs:404
├─ select_workers chat.rs:442
│ ├─ PdPoolResolver::prefill_candidates policies/registry.rs:173
│ ├─ lookup_prefix_matches chat.rs:691
│ │ ├─ ctx.prefix_index.match_prefix(hashes) ← 外部 indexer
│ │ └─ radix_tree_prefix_provider.match_request_tokens ← 进程内树
│ ├─ capture_load_snapshot chat.rs:571
│ ├─ prefills_with_decode chat.rs:520 ← 版本组对齐
│ ├─ pick_prefill_worker → select_prefill_worker selection.rs:137
│ └─ pick_decode_worker → select_decode_peer selection.rs:779
└─ forward_request → dispatch forward.rs:61 / :167
├─ room = generate_room_id[_for_rank] preparation.rs:1264
├─ spawn_prefill_request(prefill, room) forward.rs:812
└─ transport.send(decode) ← 客户端响应来自这条腿
└─ forward_pd(prefill_task, decode_future) forward.rs:905
两个容易被忽略的细节:
--dp-aware、或者 bucket 路由需要按长度分桶时才做;否则 router 直接把原始 body 转发,省掉一次分词(preparation.rs:1203-1209、policies/mod.rs:538)。--no-tokenizer 会让 cache_aware 静默退化。没有 token 就没有 block hash,lookup_prefix_matches 返回 None,阶梯直接走 load 兜底。这不是报错,是降级。推断(由 chat.rs:691-729 的 zip(tokens) 分支推出)。room 与 bootstrap_host/port,不参与。序号 3 与 4 之间没有 await:spawn_prefill_request 先起任务,decode 腿紧接着发出;d = decode_prefix_len,L = prompt 长度。跨泳道的箭头在 prefill 竖线处断开,表示只是经过、不是与 prefill 交互。dispatch 先把 prefill 作为一个 tokio::spawn 任务起起来,再构造 decode 的请求并等待它;forward_pd 用 tokio::select! 同时盯两件事:decode 的响应,以及 prefill 的结果(forward.rs:246-330、:905-950)。语义如下:
held = None 分支);Blame),有重试预算时排除它;--pd-stream-after-prefill 时,decode 的响应先被 held 住,等 prefill 完成后再合并(wait_for_prefill)。这也解释了一个常见现象:客户端只看到 decode 的 TTFT,但 prefill 的排队时间其实已经在里面——decode 要等 KV 到达才能出第一个 token。
--policy 是每个模型一份(router 只服务一个模型,所以等价于全局)。legacy 默认 round_robin,--chat-routing reorg 时默认变成 power_of_two(src/config/cli.rs:250-258)。decode 侧是独立的一套:只有 power_of_two 与 legacy_host_affinity 两个取值(src/config/types.rs:151-159)。
| policy | 读什么 | 怎么排 | 什么时候被选中 |
|---|---|---|---|
| round_robin | 无(原子计数器) | 按注册顺序轮转 | legacy 默认;sticky 的未命中兜底也是它 |
| random | 无 | 均匀随机 | 显式指定;用于压测对照 |
| power_of_two | 引擎自报负载快照 | 抽 2 个(--min-load-choices)取较轻者 | reorg 默认;cache_aware 的兜底;decode 默认 |
| load_based | 负载快照 | 全池取最小 | 显式指定;小池子比 P2 更准,但多副本会共振 |
| cache_aware | 前缀命中(indexer 或 radix tree)+负载 | 未命中 token 最少者优先,近似并列时看压力 | 显式指定;PD 下只作用于 prefill/plain,decode 仍用 decode_policy |
| session_aware | 会话绑定 + 负载 | 绑定优先,绑定满则 P2 换人并重绑 | 多轮对话、需要 KV 复用 |
| sticky | 请求头 routing key | key → worker 的稳定映射 | 按业务 key 固定路由 |
| score_policy / fused_score | --fuse 指定的加权项 | 加权和 | 需要把前缀、负载等混合打分 |
select_prefill_worker 把输入打包成 PrefillSelectionInputs,交给 Selector::run。后者按固定顺序尝试,前一级拿到人就返回(selection.rs:210-330):
--cache-candidate-max-workers,先过队头闸门,再过 KV 容量准入,然后在"未命中 token 近似并列"的集合里按压力决胜。拿到即返回,后面几级不再执行。--policy session_aware 且 bucket 模式不是 bucket 时才有:先在全局池里探一次会话绑定,再把绑定落到目标 bucket 的域里。CandidateDomain,每个域内跑一次策略提案,再过 resolve_prefill 的准入(num_running+1 ≤ max_running 且 num_total_tokens+input ≤ max_total_num_tokens)。find_map,不是一轮带 flag。PolicySelectionFailed / NoPrefillWorkersAvailable → 503(src/server/error.rs:218-222)。decode 侧是同样的两轮结构,但只读负载:select_decode_peer 先对每个 decode 域做严格准入,再统一做 capacity fallback(selection.rs:779-838)。它连"没有负载快照"都不放过——没快照直接返回 None,而不是盲选(selection.rs:782-784)。
引擎通过自己的 ZMQ PUB 口周期发布 LoadStat,wire 形如 [b"load", seq, msgpack],router 订过来按 URL 存表;超过 5 秒没更新就视为过期,退回 router 自算的 in-flight(python/.../load_publisher.py:1-40、engine_reported_load.rs:294-297)。比较规则(engine_ranking.rs:15-59):
prefill:estimated_prefill_queue_ms ↑
→ num_waiting_uncached_tokens ↑
→ num_waiting_reqs ↑
→ num_running_reqs ↑
→ router 本地 in-flight ↑
decode :num_waiting_reqs ↑ → num_running_reqs ↑ → KV 占用率 ↑
逐字段字典序,第一个不等的字段决定胜负,不做加权。这也是为什么 P2 只需要"两个样本里挑一个":比较是 O(1) 的。
--policy cache_aware 不是"打开 radix cache",而是"让 router 去问谁有这段前缀"。信号有两个来源,二选一(cli.rs:884-890):
--kv-indexer-endpoint 设了就默认用它;router 本地树被整体关掉,只订 load(kv_events/index.rs:607-616)。MatchExternalKvPrefix(hashes, max_blocks),返回每个 worker 的 matched_prefix_blocks(sgl-kv-indexer/proto/kv_indexer.proto:125-163)。worker_address 必须与 router 注册的 URL 逐字节相同,否则静默不命中(policies/cache_aware.rs:45-48)。chat.rs:754-774)。--kv-events-config,router 从 /server_info 的 kv_events 块发现地址并订阅(kv_events/discovery.rs:116-127)。BlockStored / BlockRemoved / AllBlocksCleared(wire.rs:49-58)。(block_hash, parent, workers{(url,dp_rank): tiers}),不存 token ids、不存 KV(tree.rs:489-507)。tree.rs:1608-1635)。链式 SHA256:每块的摘要 = SHA256(父块完整 32 字节摘要 ‖ 本块 token 的 4 字节小端),最后截前 8 字节成 i64(hash.rs:137-163)。三个输入决定"算不算同一段前缀":
page_size,进程级单例,第一个 worker 定调,后来者不一致就拒绝该 worker(block_size_oracle.rs:120-123、index.rs:550-559)。hash.rs:68-71)。hash.rs:77-84)。CacheNamespace 来自请求 body(model: "base:adapter"、lora_path、cache_salt),不来自 header;一个请求里多个值不一致时置 ambiguous,直接返回空哈希——宁可 miss,不猜(preparation.rs:42-84、hash.rs:64-67)。
matched_tokens = input_tokens × matched_prefix_blocks / query_blocks
uncached_tokens = input_tokens − matched_tokens policies/cache_aware.rs:202-217
代码里没有任何 matched_blocks × block_size 的乘法——router 并不知道每块真实有多少 token(尤其最后一块往往是残块),所以按"查询块数"等比折算。同一段逻辑在 reorg 路径里也有一份内联实现(policies_reorg/cache_aware.rs:148-172)。
| 闸门 | 判据 | 默认值 | 源码 |
|---|---|---|---|
| 命中门限 | matched ≥ min_matched_tokens 且(若设)matched/input ≥ min_match_ratio | 1024 tokens;ratio 未设 | cache_aware.rs:117-131 |
| 候选截断 | min(引擎数, max_workers, max(min_workers, ceil(ratio×引擎数))) | 8 / 0.05 / 32 | cache_aware.rs:134-141 |
| 队头闸门 | num_waiting_reqs < --worker-queue-limit;无新鲜样本则放行 | 未设(关闭) | admission.rs:185-200 |
| 容量准入 | running+1 ≤ max_running 且 used+input ≤ max_total | 由引擎上报决定 | admission.rs:555-590 |
闸门之后还有两个"软"机制,它们决定"要不要为了负载放弃一点前缀":
--cache-switch-margin-tokens(默认 1024):只有两个候选的未命中量差超过它,才允许用负载翻盘;否则按"未命中更少"稳定排序(admission.rs:607-648)。--pressure-abs-threshold-tokens 默认 1024、--pressure-rel-threshold 默认 1.5、可选的 ms 阈值):在近似并列的集合内,如果 A 比 B 明显更受压,就把 A 排到后面(admission.rs:659-692)。最后是饱和钉住:如果队头闸门把所有候选都拒了,而全机群没有一个 worker 的队列低于 --saturation-queue-floor,那么"换个地方等"并不能少等,router 反而保留前缀所有者——同样的等待,至少 prefill 是命中而不是冷算(admission.rs:283-345,判决标签 SaturationPin)。这条路径的指标标签是 AllQueued 而不是 CacheHit,两者在监控里必须分开看。
match_prefix 在 page_size > 1 时会把长度截断到页的整数倍(mem_cache/radix_cache.py:386-387),所以哪怕引擎缓存了全部 token,router 看到的匹配长度也不会超过 floor(L/page)×page。PD 部署下 router 只说三句话:发给谁(prefill)、发给谁(decode)、你们用这个 room。剩下的握手、寻址、传输、完成确认,全部由两个 engine 自己完成。理解这一点,后面所有"谁发起"的问题都会变得简单。
pub(super) fn generate_room_id() -> u64 {
rand::random::<u64>() & (i64::MAX as u64) // 引擎按 int64 存
}
/// A room with `room % dp_ranks == rank`, which is how decode finds the prefill rank.
pub(super) fn generate_room_id_for_rank(rank: u32, dp_ranks: u32) -> u64 {
let dp_ranks = u64::from(dp_ranks);
(generate_room_id() >> 1) / dp_ranks * dp_ranks + u64::from(rank)
} preparation.rs:1264-1272
第二行是整个 PD 配对里最需要注意的一处:decode 默认不被额外告知 prefill 的 DP rank,它自己算 room % dp_size(decode.py:1197-1199)。所以当 router 开了 --dp-aware 并替 prefill 选定 rank 时,它必须反过来约束 room,让这个取模结果正好等于所选 rank——否则 decode 会去错误的 rank 取 KV。兜底的显式通道是请求体里的 disagg_prefill_dp_rank(forward.rs:759-778)。
| 步骤 | 谁发起 | 方向 / 协议 | 携带什么 | 源码 |
|---|---|---|---|---|
| ① 拓扑注册 | prefill(启动期,每 rank) | HTTP PUT /route | dp/cp/tp/pp rank、rank_ip、rank_port、page_size、kv_cache_dtype、kv_layout、state_wire、allocation policy | conn.py:3285-3406 |
| ② 请求到达 | router | HTTP POST,两条腿 | bootstrap_host、bootstrap_port、bootstrap_room(decode 腿另可带 disagg_prefill_dp_rank) | forward.rs:216-244 |
| ③ 查对端地址 | decode | HTTP GET /route?prefill_dp_rank&… | 返回该 rank 的 rank_ip:rank_port 与形状参数 | conn.py:2874 |
| ④ 注册接收缓冲 | decode → prefill | ZMQ PUSH(multipart) | local_ip、rank_port、session_id、每层 kv_data_ptrs、aux/state 指针、dst_kv_item_len、层 id | mooncake/conn.py:3544-3617 |
| ⑤ 发布目的页 | decode → prefill | ZMQ PUSH | room、kv_indices(目的页号)、aux_index、decode_prefix_len、required_dst_info_num | mooncake/conn.py:3619-3646 |
| ⑥ 定稿发送起点 | prefill | 本地状态机 | start_send_idx = decode_prefix_len;num_pages = ceil((L−d)/P) | prefill.py:528-549 |
| ⑦ 传 KV | prefill → decode | RDMA(mooncake / NIXL) | 按层、按页、按 chunk 的批量写 | mooncake/conn.py:2048 |
| ⑧ 交首个 token | prefill → decode | RDMA 写 metadata buffer | output_ids[0]、logprobs、bootstrap_room(防串号) | utils.py:908-918 |
| ⑨ 完成确认 | prefill → decode | ZMQ PUSH 状态帧 | [room, status, prefill_rank] | conn.py:858-867 |
注意 ④⑤ 与 ⑦ 的方向是相反的:decode 先告诉 prefill "往我这里写",prefill 再推数据。这就是为什么 bootstrap 必须发生在传输之前——它不是一次礼节性握手,而是一次地址交换。
KVPoll 只有五个状态:Failed=0 / Bootstrapping=1 / WaitingForInput=2 / Transferring=3 / Success=4(base/conn.py:141-147)。decode 的推进条件是三件事都到齐:
apply_prefill_status 按 prefill_unique_rank 去重计数,够 required_prefill_response_num 才置 Success,conn.py:1053-1076);bootstrap_room == 0 就仍算 Transferring,utils.py:339-354);kv_indices 收齐(传输层保证)。第 2 条是防"状态帧比数据先到"的经典竞态:状态帧走 ZMQ/控制面,首 token 走 RDMA/数据面,两者没有共同时钟。所以 decode 用 metadata buffer 里的 room 字段做栅栏——这同时也防了 room 复用导致的串号(不匹配直接 abort,报 Context corruption,decode.py:3420-3466)。
(src_page_indices, dst_page_indices) 入队,prefill 每个 TP rank 有 SGLANG_DISAGGREGATION_QUEUE_SIZE(默认 4)个队列,每个队列一个 worker 线程 + 一个线程池(mooncake/conn.py:354-402)。shard = Σ(目的 session 端口的数字部分) % 队列数,保证同一组目的端的请求落在同一队列(mooncake/conn.py:3440-3450)。executor.submit,mooncake/conn.py:1370-1379)/页间(连续页合并成一次 batch_transfer_sync,group_concurrent_contiguous,:1207-1209)。process_batch_result_disagg_prefill 发最后一块);但缓存命中部分可以在 forward 之前先发——maybe_send_cached_prefix_chunk,开关是 SGLANG_DISAGG_PREFILL_EARLY_SEND_CACHED_PREFIX(prefill.py:1627-1666)。这正是"部分命中"能进一步压 TTFT 的地方。| 失败面 | 检测 | 默认 | 后果 | 源码 |
|---|---|---|---|---|
| prefill 等不到 decode 的目的地 | _check_bootstrap_timeout | 300s | room 置 Failed,请求 abort(500),占用的 bootstrap 队列位释放 | conn.py:2546-2568 |
| decode 等不到 KV 完成 | _check_waiting_timeout | 300s | Failed + 发 abort 通知 + 失效连接缓存 | conn.py:3062-3086 |
| prefill 节点消失 | decode 心跳 GET /health | 5 s × 2 次 | 该地址下所有未完成 room 置 Failed | conn.py:2177-2267 |
| RDMA 返回非 0 | 传输 worker 检查返回值 | — | session 标记失败 → conclude_failure → 状态帧 Failed | mooncake/conn.py:3059-3076 |
| 客户端断开 | decode 侧 abort | — | 发 ABORT 帧;prefill 释放 sender;deferred 模式下等 drain 完再 ACK | conn.py:3129-3186 |
| prefill 侧释放 KV | 所有 rank 归约为 Success 之后 | — | release(checkpoint=True),KV 进 radix cache 可复用 | prefill.py:1420-1429 |
--kv-peer-selector 开启,默认预算 10 分钟,失败可让 /readyz 保持 503(--kv-bootstrap-seed-required)。两者除了名字没有任何关系。先把记号定下来,后面三个小节只是往同一组公式里代不同的数:
L prompt token 数
P page_size(引擎的 KV 页大小,也是 router 的 block_size)
p prefill 侧 radix 命中长度(page 对齐;0 ≤ p ≤ L)
d decode_prefix_len:decode 通过 metadata 告诉 prefill "这段我已有"
(仅当 decode 侧 radix cache 开启时可能 > 0,默认 0)
计算量 = L − p 只有 prefill 的 forward 处理未命中部分
传输量 = L − d prefill 把 [d, L) 的页写给 decode
router 可见的是 p(决定"发给谁");d 由 decode 自己算,router 从不知道
最后一行是这一页最重要的结论:router 的 cache-aware 优化的是"把计算放到有缓存的那台机器上",它并不会减少 KV 传输量。真正减少传输的是 decode 侧 radix cache(--disaggregation-decode-enable-radix-cache,默认 关,fields/disagg.py:104-110)。
prefill.py:1197),而采样要有 logits,logits 要有一次 forward;具体少到多少由 prefill 的 checkpoint 边界决定,这里不逐行展开。lookup_prefix_matches 拿到 PrefixOutcome::Empty 或全 0 匹配,cache_aware 的候选集为空 → 阶梯退回 P2(selection.rs:210-240);选出 prefill、按容量选 decode,生成 room。extend_end = L,forward 全量 L 个 token(可能被 chunked prefill 切成多批);每批 checkpoint 进 radix cache。decode_prefix_len = 0,prefill finalize_bootstrap 后 start_send_idx = 0、num_pages = ceil(L/P)(prefill.py:536-549)。send_kv_chunk(last_chunk=True) 把 [0, L) 全部页写给 decode。Success → 提交首 token → 开始逐 token decode。开销构成:prefill 算 L、传 L,decode 等 max(自己排队, prefill 算完 + 传完)。这是三种情况里唯一"算与传都满"的。
matched_prefix_blocks > 0;折算成 matched_tokens ≈ L × blocks/query_blocks。过 1024-token 门限后成为 cache 候选,按"未命中最少"排序;若该 owner 队列超 --worker-queue-limit,先尝试换到别的 owner(放弃一部分前缀),换不到且全池饱和则 SaturationPin 钉回原 owner。prefix_len = p,forward 只算 L − p;已在 cache 的 [0, p) 不重算。d = 0 所以 start_send_idx = 0、页数仍是 ceil(L/P)。[0, p) 的页虽然"不用算",但仍然要从 prefill 的 cache 里读出来发过去。开了 early-send 时,这段可以先发、与 forward 重叠(prefill.py:1627-1666)。收益全在 prefill 的 L − p 上:TTFT 里"算"的那一段按比例缩短;传输时间不变。所以 B 的加速比大致是 (L + 传输) / (L − p + 传输)——前缀越长、传输占比越高,命中带来的边际收益越小。
extend 只剩最后一页(甚至 0 个新 token)。即便如此,prefill 仍要跑一次 forward 才能采出第一个 token——req.output_ids.append(next_token_id) 发生在 prefill 的结果处理里(prefill.py:1197)。[0, L) 的页仍要发(d = 0)。只有 decode 已持有到 L(d = L)时传输区间才会空:end_idx < start_send_idx 时 _send_kv_chunk 直接 return,相等时也只是零页(prefill.py:1718-1723)。d = L,也就是 decode 自己已经有整段前缀——这需要 decode 侧 radix cache,且该请求此前在同一台 decode 上跑过。| 维度 | A 全未命中 | B 部分命中 | C 全部命中 |
|---|---|---|---|
| 是否触发 prefill | 是,算 L | 是,算 L − p | 是,但只算最后一页(采样所需) |
| 是否需要 bootstrap | 需要 | 需要 | 仍需要(即使 d = L、零页可传,握手与状态帧仍会发生) |
| KV 传输量 | L(ceil(L/P) 页) | L(不变) | L(不变;d = L 时才为 0) |
| 传输内容 | prompt 全部层的 KV + aux(首 token) | 同上;其中 [0,p) 来自 cache、[p,L) 来自本次 forward | 同上,几乎全部来自 cache |
| Router 决策差异 | cache 候选为空 → P2/轮转 | 有候选 → 按未命中排序;可能被队列/压力守卫改判 | 候选唯一且命中比≈1;决策退化为"容量够不够" |
| 延迟主要来源 | prefill 计算 ≫ 传输 | 计算 (L−p)/L + 传输 | 传输 + 握手往返(计算≈0) |
| 资源开销 | prefill GPU 满载一段;decode GPU 空等 | prefill GPU 部分负载;decode 空等变短 | prefill GPU 几乎空闲;瓶颈转到网络与 decode 侧准入 |
把这张表和 §07 的公式并排看,会得到一个有点反直觉的工程结论:当 p 很大时,继续优化 router 的 cache-aware 收益已经很小,因为剩下的时间几乎全在"把 L 个 token 的 KV 搬过去"。要动这一段,只有三条路:开 decode 侧 radix cache 让 d > 0、缩短 L(前缀复用/裁剪)、或者提高传输带宽与并行度。
| 机制 | 默认 | 行为 | 源码 |
|---|---|---|---|
| 重试次数 | 1(不重试) | 含首次共 N 次;3 is typical 只是注释建议 | types.rs:60-72 |
| 退避 | 50 ms 起,×2,上限 2000 ms,半区间 jitter | 首次重试实际等 25–50 ms;退避不超过 stale 预算剩余 | types.rs:39-55 chat.rs:401-411 |
| 可重试判定 | — | 只在"尚无任何响应到达客户端"时:传输错误、5xx、429/503;4xx(除 429)与 2xx 不重试 | forward.rs:365-371 metrics.rs:215-223 |
| 重试排除 | — | 排除给出响应的 worker;PD 下若判定 prefill 有责则排除该 prefill;客户端自带 rid 时两侧都排除 | forward.rs:371-379 |
| 请求总寿命 | 600 s | 超时返回 504 stale_request_expired;流已开始则只断流不改状态码 | types.rs:81-83 error.rs:221 |
| 上游响应超时 | 300s | 非流式=整个响应;流式=响应头(即首 token,含排队) | types.rs:56-58 |
| 流空闲 | 180 s | 两个 chunk 之间的最大静默 | types.rs:64 |
| 断路器 | 连续 3 次失败 / 冷却 30 s | 打开后候选集里剔除该 worker;冷却后放一个探针(half-open);503/429 视为背压,不计失败 | circuit_breaker.rs:34-42, 207-238 |
| 健康检查 | — | /healthz 恒 200;/readyz 只看池子可用 + peer bootstrap 是否结算,不含逐 worker 探测;worker 健康=被动熔断器 + 注册表 | routes/health.rs:13-52 |
| 注册表 fail-open | — | 全员熔断打开时重置所有 breaker 并返回全池(宁可试,不可全拒) | registry.rs:185-213 |
| decode 闸门 | 未设 | --decode-dispatch-limit N:每个 prefill DP rank 最多 N 个"首 token 前"的请求进 decode;超出阻塞等待(受 stale 预算约束),不返回错误 | decode_gate.rs:9-34 forward.rs:398-413 |
| 主动探测 | — | 不存在:没有 --health-* 参数、没有探测循环;get_load 在 gRPC 里返回 UNIMPLEMENTED | grpc.rs:321-328 |
背压的出口只有两个:503 与 504。router 自己不生成 429——ApiError 里没有这个变体;引擎返回的 429/503 会被原样转发,同时被归类为 Backpressure,既不打开熔断器、也不清空已有的失败连击(circuit_breaker.rs:207-238)。这条设计避免了"繁忙的 worker 被误判为坏 worker"。
不会。router 只传 room 和 bootstrap_host/port;KV 由 prefill 直接 RDMA 写到 decode 注册的地址(forward.rs:216-244、mooncake/conn.py:2048)。这也是为什么 router 的带宽不是 PD 部署的瓶颈——它只承载 token 流。
它看的是块哈希的持有关系:要么来自外部 indexer 的 block_hash → worker,要么来自 router 自己维护的树(由引擎的 BlockStored/BlockRemoved 事件喂养)。router 既没有 KV,也不知道缓存块的 token 内容(tree.rs:489-507)。误判的代价是"发到了一个其实没有这段前缀的 worker",后果是那次 prefill 退化成全量计算,不是错误。
两件事都不成立。prefill 至少要跑一次 forward 才能采出第一个 token(prefill.py:1197),而 bootstrap 是 decode 获取"往哪写、写多少、首 token 是什么"的唯一通道,与命中率无关。命中只改变 forward 的长度。
这个版本里没有主动探测:src/health/ 下只有一个 circuit_breaker.rs,没有任何 --health-* 参数。健康判断完全是被动的:请求失败累计到阈值 → 熔断器打开 → 候选集剔除;冷却后放一个探针。全员打开时还会 fail-open 回全池(registry.rs:200-212)。
仍然传 8192 个 token 的 KV(128 页)——除非 decode 侧 decode_prefix_len > 0。传输量 = L − d,与 prefill 的命中长度 p 无关。
generate_room_id_for_rank 要先把随机数对齐到 dp 的整数倍、再加 rank?因为 decode 默认用 room % prefill_dp_size 推断"该去哪个 prefill DP rank 取 KV"(decode.py:1197-1199)。router 既然已经替 prefill 选了 rank,就必须让 room 的取模结果等于它,否则 decode 会查错 rank;显式覆盖只有 disagg_prefill_dp_rank 一条路。
--worker-queue-limit 打开时,A 先被队头闸门拒掉,请求转到 B(放弃一部分前缀局部性);如果全池都在排队且没人低于 --saturation-queue-floor,则触发 SaturationPin,钉回压力最小的 owner——同样的等待,至少 prefill 是命中。指标上分别记 CacheWorkerQueued 与 AllQueued。
router 的 503 是"没有可用的 worker / 全部候选被准入拒绝"(no_healthy_workers、policy_selection_failed 等),属于路由层结论;引擎的 503 是背压,会被原样转发、归入 Backpressure,不打开熔断器,并可能触发重试(若还有重试预算)。