mirror of
https://github.com/lightseekorg/tokenspeed.git
synced 2026-12-02 23:51:06 +08:00
## Problem
A failed Decode receive may reuse its pages only after every Prefill
target acknowledges the cancellation, because a failure report or
timeout does not prove that remote writes stopped
(`docs/design/event-loop.md`, PD cancellation). Sometimes that
acknowledgment never comes:
- the Prefill process died;
- it restarted at a new control port, so cancellations go to a dead
address;
- it holds a native write whose DMA may still land, so it withholds the
acknowledgment.
In all three cases the pages stayed pinned until the Decode rank
restarted. Every Prefill crash leaked the pages of the receives in
flight to it at that moment. Sleep with memory release, cache flush and
weight replacement could not drain either.
```mermaid
sequenceDiagram
participant D as Decode scheduler
participant O as Old Prefill
participant N as Restarted Prefill
D->>O: manifest (Decode page addresses)
O->>D: RDMA writes into the pages
Note over O: process dies, a write may be unfinished
Note over D: heartbeat lost, room failed
loop every second
D->>O: Cancel (to the old control port)
end
Note over O: nothing listens on the old port
Note over N: new random control port, old cache unknown
Note over D: CancelAck never comes: pages and slot stay pinned
```
## Fix: revoke the writers instead of trusting a timeout
Each Decode rank owns a Mooncake transfer engine (`ts_mooncake::Engine`
in its PD transfer peer). The engine registers the rank's KV cache with
the RDMA NICs and serves the session Prefill handshakes with to obtain
the keys it writes with. A revocation replaces this engine; it is not
the inference engine.
Trigger: a failed receive has waited `waiting_timeout` for
acknowledgments, and its manifest named the current transfer engine. The
Decode peer then starts a revocation on a background thread:
1. Start a fresh Mooncake engine with the cache regions registered (no
lock held).
2. Under the state lock:
- fail every receive still attached to the replaced transport, whether
published or waiting for admission;
- make the fresh engine the one transfers use and registrations name
(`Transport::install_engine`);
- switch to a new cache identity;
- clear route registrations.
3. Deregister the cache from the replaced engine.
4. Only if that succeeded, the receives whose manifests named the
replaced engine retire without acknowledgments. Otherwise they keep
waiting and do not trigger revocations of later engines. The replaced
engine may still reach the cache, so the cache is then never released
(sleep with memory release is refused); this is fail-closed on a double
failure.
```mermaid
sequenceDiagram
participant D as Decode scheduler
participant R as Revocation thread
participant E1 as Decode Mooncake engine (old)
participant E2 as Decode Mooncake engine (new)
participant W as Old Prefill writer
Note over E1,E2: Decode's transfer engines:<br/>each registers the KV cache with the RDMA NICs<br/>and hands Prefill the keys it writes with
Note over D: failed room waited waiting_timeout for CancelAck
D-)R: start revocation (off the scheduling loop)
R->>E2: start engine, register the cache
Note over R: swap engine and cache identity, fail attached receives
R->>E1: deregister the cache
alt deregistration succeeded
E1-->>R: deregistered, old keys invalid
R-->>D: retire the rooms, reuse their pages
W->>E1: RDMA write with an old key
E1--xW: rejected by the NIC
else deregistration failed
E1-->>R: deregistration error
Note over R: some regions may still be registered, so old keys may still work: rooms keep waiting for CancelAck, memory never released
end
```
The fence, not the timeout, is what authorizes reuse. An RDMA NIC checks
each incoming write against the destination's registration, and
`ibv_dereg_mr` returns only once the NIC no longer uses the MR; the
kernel may unpin the pages right after. The timeout only decides when to
pay for the fence.
Details:
- **Transport:** only `rdma` revokes. With other transports such
receives stay pinned, as before.
- **Replaced engine kept alive:** it stays without the cache registered
for the peer's lifetime, so its session (host and RPC port) is never
reused by a later engine. A revoked writer that resolves its old session
again would otherwise reach the newer engine. The test reproduces
exactly this with a sleep/wake after the revocation when the replaced
engine is dropped.
- **Cache identity per receive:** each room records the cache identity
its route registered (and so the engine its manifest names).
Cancellations and their acknowledgments use that identity, so a receive
failed before admission still reaches Prefill under an identity it
knows. Under `deferred` decode allocation (#3394), a room's
announcements use that identity too.
- **No sleep mid-revocation:** memory release is refused while a
revocation runs, since it installs a fresh engine with the cache
registered. `revoke` marks the fenced rooms and ends the revocation in
the critical section that listed them, so a room retiring in between is
not marked again. The running revocation is also an RAII guard whose
drop ends a revocation that did not end itself (no fresh engine, or a
panic).
- **No stranded rooms:** rooms connecting during a revocation wait and
reconnect under the new identity instead of staying in Bootstrapping. A
receive that already failed never publishes its manifest later.
- **Observability:** each outcome is logged as an event with the rank,
the rooms and the old and fresh sessions:
`event=ts_decode_revocation_fenced`,
`event=ts_decode_revocation_unfenced` (error, with the pinned rooms),
`event=ts_decode_revocation_not_started`. A release refused for an
unfenced cache logs `event=ts_decode_release_refused_unfenced`, so the
lasting effect can be traced back after the first log line is gone.
- **Cost:** each revocation fails every receive attached on that rank at
that moment (its replica peers fail them too, through the agreed
transfer status), and keeps one idle engine.
`docs/design/event-loop.md` gains a "PD receive revocation" paragraph.
## Tests
`tests/cancellation.rs::a_cancellation_unacknowledged_past_the_receive_budget_is_retired_by_revoking_the_transport`
runs real peers, sockets and bootstrap with the CPU native stand-in. The
Prefill write is blocked in native I/O, so it withholds the
acknowledgment as a dead peer would. The test checks:
- the cancelled receive retires after the budget;
- a request waiting for admission fails, and admitting it afterwards
publishes no manifest (no second revocation);
- a sleep/wake while the revoked write is still blocked;
- the released write does not reach the recycled pages;
- the next request transfers through the fresh engine.
The test fails on `main` (the receive never retires). It also fails if:
- a failed receive still publishes;
- the replaced engine keeps its registration;
- the replaced engine is dropped (session port reuse).
`a_revocation_whose_deregistration_fails_keeps_waiting_for_acknowledgments`
makes the stand-in's deregistration fail. It checks that:
- the receive keeps waiting and starts no second revocation;
- it retires on the late acknowledgment;
- the cache can no longer be released.
Unit tests cover cancellations acknowledged under the cache their route
registered, revoked rooms retiring without acknowledgments, rooms marked
as the revocation ends (a room that retires before the guard drops is
not marked again), deferred-allocation announcements after a revocation,
a registration wait interrupted by a revocation reconnecting under the
fresh cache, and the logged outcome events.
`cargo test -p tokenspeed-pd`, `cargo fmt --all --check`, `cargo clippy
--all-targets -- -D warnings` and `pre-commit run --all-files` pass
locally.
## Not covered / review notes
- The fence relies on `ts_mooncake_unregister` returning only after
`ibv_dereg_mr` succeeded on every device. This needs confirming against
the Mooncake build in use, plus a run on real RDMA (kill a Prefill rank
mid-transfer and check the Decode pages are released and not
overwritten).
- Builds on #3657 (merged): a room waiting for its route's registration
acknowledgment gives up that registration when a revocation starts and
connects again under the fresh cache identity once it finished.
Signed-off-by: wangbo981016 <wangbo981016@gmail.com>
Co-authored-by: longcat-ia-team <longcat_ia_team@meituan.com>