Repository navigation
[Bug] Fix tokenizer pool failures and double tokenization in prefix-cache routing - #3035
Conversation
Route renders chat requests through the chat template so the hashes match the blocks vLLM reports in KV events, but ScoreAll and PostRouteUpdate tokenized the plain prompt text. In a blended strategy the prefix score never matched the engine's cache, and the post-route update indexed hashes no request would look up. Signed-off-by: MinJun Choi <[email protected]>
A blended strategy scores pods with ScoreAll and then calls PostRouteUpdate on the winner, and both tokenized the prompt. With a remote tokenizer that was two round trips to vLLM per request, and with tiktoken it added about 70% to the routing time. The tokens are now kept on the routing context for the rest of the request. Signed-off-by: MinJun Choi <[email protected]>
performHealthCheck held the pool's write lock while it called each remote tokenizer, with a 5s timeout per model. GetTokenizer takes the same lock on every prefix-cache request, so a single slow vLLM endpoint stalled routing for all models until the probes finished. Signed-off-by: MinJun Choi <[email protected]>
A tokenizer found through pod discovery stays pinned to that pod's IP. After the pod was deleted in a rollout or scale-down, every prefix-cache request still called it, failed after the client retries, and returned 503 until the next health check marked it unhealthy (30s by default). GetTokenizer now replaces the tokenizer as soon as its pod is missing from the ready pods it is given. ModelServiceMap endpoints are unaffected. Signed-off-by: MinJun Choi <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request optimizes prefix tokenization and tokenizer management. It introduces caching for prefix tokens in RoutingContext to avoid redundant tokenization calls, and refactors TokenizerPool to track pod IPs so it can dynamically migrate away from terminated pods. Additionally, the tokenizer health checker is updated to run probes without holding the main lock, preventing unresponsive endpoints from blocking lookups. Feedback on the changes suggests using a value receiver instead of a pointer receiver for the new tokenize method on prefixCacheRouter to maintain consistency with other routing methods.
varungup90
left a comment
There was a problem hiding this comment.
Reviewed the diff and ran go test -race ./pkg/plugins/gateway/algorithms/ ./pkg/types/ on the PR head (both pass). I found no blocking bugs.
- Chat tokenization in KV sync:
kvSyncPrefixCacheRouter.tokenizeis now the single place that applies the chat template first and falls back to plain text, andRoute,ScoreAllandPostRouteUpdateall go through it, so the hashes they use match the engine's KV events. The nil-tokenizer error is raised inside the closure, so it is not cached. - Tokenize once:
cachedPrefixTokensstores only successful results on theRoutingContext, andresetclears them, so a pooled context cannot leak tokens into the next request. - Health checks:
performHealthChecksnapshots the entries under a read lock, probes without any lock, and writes the result back only if the same entry is still in the pool, so a replaced or removed entry is not resurrected. The log and counter for a healthy-to-unhealthy transition are emitted outside the lock. - Moving off a departed pod: an entry found by pod discovery remembers its pod IP and is replaced when that IP is not among the ready pods;
ModelServiceMapentries have no pod IP and are unaffected. Replacing an entry closes the old tokenizer, which only callsCloseIdleConnections()on its HTTP client, so requests still using it are not interrupted.
I read the code and did not run the benchmarks; the numbers in the description are the PR's own.
Overall: approve, with non-blocking notes.
PrefixTokens()usesnilto mean "not cached", so a legitimately empty tokenization result (a nil slice) is tokenized again on the next call. That is harmless, but a separate "cached" flag would make the intent explicit.- The pool entry is keyed by model but now validated against whichever pod list the caller passes. Today the prefix-cache routers pass the model's ready pods, so this is stable. If another caller ever passed a different subset for the same model (for example only prefill pods), the entry would be replaced back and forth, with a health probe and a tokenizer rebuild on the request path each time. A comment on
GetTokenizerstating that the list must be the model's full ready set would guard against that. - The health check now probes entries one after another, each with up to a 5-second timeout. That no longer blocks lookups, but with many models and several unhealthy endpoints one round can run longer than the check interval; probing in parallel, or skipping a round when the previous one is still running, would bound it.
Generated by Claude Code
An empty result was stored as nil, which reads as "not cached", so it was tokenized again. GetTokenizer also needs every ready pod of the model, or it replaces a pinned tokenizer that is still valid. Signed-off-by: MinJun Choi <[email protected]>
|
Thanks for taking a close look! Pushed 8227d92 for the first two. An empty tokenization now gets stored as an empty slice so it's cached like anything else, and the On the health check, rounds can't actually pile up. The checker runs on a single goroutine and |
Design simplification reviewVerdict: Approve (nits / optional follow-ups only) Head reviewed: What works well
Optional follow-ups (non-blocking)
Compatibility / scopeAdditive Design: Approve. |
Code reviewVerdict: Approve (nits only; no merge blockers) Head reviewed: Correctness
Tests / local checks
Nits (non-blocking)
Code: Approve. Merge recommendation: Approve. |
Pull Request Description
Fixes the four tokenizer problems in prefix-cache routing from #3034. Blended routing gets about 42% faster, and there are no config, API or metric changes.
Changes (one commit each)
ScoreAllandPostRouteUpdatenow use the same helper asRoute: chat template first, plain text as the fallback. Their tokens now match the engine's KV events.The tokens are cached on
RoutingContext(PrefixTokens, cleared on reset) and shared byScoreAll,RouteandPostRouteUpdate. Only successful results are cached.performHealthCheckcopies the entries under a read lock and probes them unlocked. It writes back only if the entry is still in the pool.An entry found through pod discovery remembers its pod IP. If that IP is no longer among the ready pods,
GetTokenizercreates a tokenizer on a ready pod right away.ModelServiceMapentries are unaffected.Results
Blended routing time, main vs this PR (n=8,
prefix-cachescorer, two pods):/tokenize, 4K tokens)Memory per request drops 48% and allocations drop 50%. Remote
/tokenizecalls go from 2 to 1 per request.benchstat output
The other cases from the issue:
ScoreAllafterRouteon the same chat request[0 0][100 0]GetTokenizerwhile another model's probe hangsRouteafter the pinned pod is deletedTests
TestKVSyncPrefixCacheTokenizesChatRequestsLikeRouteRoute,PostRouteUpdateandScoreAllall use chat-template tokensTestBlendedPrefixCacheRouteTokenizesOnce/tokenizecall per blended request (local index and KV sync)TestTokenizerPoolHealthCheckDoesNotBlockLookupsGetTokenizerreturns while another probe is blockedTestTokenizerPoolMovesOffAPodThatLeftgo test -race ./pkg/plugins/... ./pkg/types/... ./pkg/utils/...andgolangci-lint run(v2.13.1) on the changed packages pass.Related Issues
Resolves: #3034