Skip to content

[Bug] Index LoRA adapter blocks per adapter in KV event sync - #3097

Merged
varungup90 merged 2 commits into
vllm-project:mainfrom
theminjunchoi:fix/kv-sync-lora-isolation
Oct 15, 2026
Merged

varungup90 merged 2 commits into
vllm-project:mainfrom
theminjunchoi:fix/kv-sync-lora-isolation

Conversation

@theminjunchoi

Copy link
Copy Markdown
Contributor

Pull Request Description

With KV event sync on, blocks that vLLM stores for a LoRA adapter were indexed under the base model, because the key came from the pod and the event's lora_name was ignored. The router, for its part, looked up adapter requests by the adapter name with loraID = -1. As a result, base requests matched adapter blocks that vLLM can't reuse for them (its block hashes include the adapter), and adapter requests never matched anything the engine reported.

This keeps each adapter's blocks in their own context:

  • Storing: BlockStored events that carry a lora_name are indexed under the base model with a LoRA ID derived from that name (LoraIDFromName).
  • Lookup: the router resolves adapter requests to the same context through ModelBaseModel, both when routing and in the post-route update. This replaces the loraID := -1 // TODO.
  • Block removal: BlockRemoved events don't carry the adapter name, so the blocks are removed from whichever contexts of the model indexed them, using the existing block index.
  • Pod cleanup: RemovePrefix now drops the pod from the model's adapter contexts as well. It's called on pod deletion, AllBlocksCleared and sleep, and in each case the pod's whole cache is gone.

Nothing changes for base-model traffic or for the approximate prefix-cache path.

Notes for reviewers

  • I derived the LoRA ID from the adapter name instead of switching the key to a string, so the MatchPrefix/AddPrefix signatures stay the same across indexers. Happy to change it to a string key if you'd prefer that.
  • RemovePrefix no longer limits itself to the loraID it's given. Every caller is clearing a pod whose cache is entirely gone, and the adapter contexts need clearing too.

Testing

  • TestAdapterBlocksAreIndexedPerAdapter checks that adapter blocks match only the adapter, and that a BlockRemoved without an adapter name and RemovePrefix both clear them.
  • TestKVSyncScoreAllMatchesAdapterBlocks routes an adapter request and checks that it scores the pod holding the adapter's blocks.
  • Both fail on main.
  • go test passes for pkg/utils/syncprefixcacheindexer (with -race), pkg/plugins/gateway/..., pkg/cache/... and pkg/kvevent (with -tags zmq), and golangci-lint reports no issues.

Related Issues

Related: #2285 (the LoRA part left after #2384 and #2587)

Blocks vLLM stored for an adapter went into the base model's context, so base
requests matched them and adapter requests never matched engine state. Key them
by the adapter's lora_name, and clear them on BlockRemoved and pod removal.

Signed-off-by: MinJun Choi <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the prefix cache router to index and route LoRA adapter blocks separately from their base models. It introduces helper functions to derive LoRA IDs from adapter names, updates block storage and removal logic to handle adapter-specific contexts, and ensures pods are removed from all model contexts. Feedback suggests using defensive type assertions when iterating over the sync.Map in RemovePrefix to prevent potential runtime panics and ensure type safety.

@varungup90 varungup90 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: index LoRA adapter blocks per adapter in KV event sync

Verdict: approve with nits (recommendation only; I have not submitted a formal approval).

What I checked

  • The bug is real. On main, BlockStored events are keyed by the pod's model name and LoraID from the subscription, and event.LoraName is ignored, while the router looks adapter requests up under the adapter's own name with -1. The two never meet, and base requests can match blocks that belong to an adapter.
  • Store side: ctx.LoraID = LoraIDFromName(event.LoraName) is a 63-bit xxhash, so it can't be -1 and collisions are negligible. Lookup side: indexContext maps the adapter to its base model through ModelBaseModel and uses the same ID, in ScoreAll, Route and PostRouteUpdate.
  • BlockRemoved has no adapter name, so the new code finds contexts through the existing blockIndex and removes the hash from every context of that model that holds it. Because removal stays scoped by SourcePod, another pod's entry is not touched. I probed the worst case, where two pods store the same engine hash under the base model and under two different adapters: removing it from one pod left the other pod's entries in the other contexts intact.
  • RemovePrefix now clears the pod from every context of the model. All three callers (pod deletion, AllBlocksCleared and the sleep backstop) discard the pod's entire cache, so I agree with the change. A sibling model name is not affected.
  • The new blockIndex scan releases its read lock before calling removeBlocksFromContext, which takes the write lock in updateBlockIndex, so there is no lock-order problem.

What I ran

  • gofmt -l and go vet on the two touched packages: clean.
  • go test -race ./pkg/utils/syncprefixcacheindexer/..., go test ./pkg/plugins/gateway/... and go test ./pkg/cache/...: pass.
  • Mutation checks (production code altered, PR tests kept): ignoring LoraName on store, limiting RemovePrefix to one context, limiting BlockRemoved to the event's own context, and making indexContext return (model, -1) each make one of the two new tests fail. So the tests do constrain the behaviour.
  • A throw-away concurrent store/match/remove/RemovePrefix probe under -race -count=3: clean (deleted afterwards).
  • CI on this head is green: unit, race, integration, lint, verify, coverage, smoke, installation E2E, Volcano gang E2E and DCO.

Not run

  • go test -tags zmq ./pkg/kvevent/...: libzmq isn't available here, so that package doesn't build locally. The PR doesn't touch it; the CI results above are all I have for it.
  • golangci-lint locally.

Nits

  1. Test gap on Route and PostRouteUpdate (inline comment). Reverting either call site to modelName, -1 leaves the whole prefixcache package green. Only ScoreAll is covered.
  2. RemovePrefix ignores loraID (inline comment). Document it on the interface method (or rename the parameter), since the signature suggests otherwise.
  3. Base-model name must agree on both sides (inline comment). Stored blocks use the pod label; lookups use spec.baseModel. If they ever differ, adapters silently keep scoring 0, as before this PR.
  4. Stale doc comment: pkg/kvevent/interfaces.go:86-88 still says LoraName is "Reserved for per-adapter index isolation". The PR updated events.go but not this one.
  5. FYI, not a request: every adapter now has its own context, so many adapters per model count against AIBRIX_SYNC_MAX_CONTEXTS (default 1000) and become eviction candidates earlier than before.

Generated by Claude Code

// the adapter name, which is also the model name the request uses.
func (k *kvSyncPrefixCacheRouter) indexContext(model string) (string, int64) {
if k.cache != nil {
if base, ok := k.cache.ModelBaseModel(model); ok {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to be aware of: the two sides of the match are derived from different sources. Stored blocks are keyed by the pod's metadata model name (constants.ModelNameFromMetadata, the model.aibrix.ai/name label), while this lookup uses ModelAdapter.spec.baseModel (falling back to the host pod's label only when spec.baseModel is empty, per setModelBaseModelLocked). The samples keep the two equal, but nothing enforces it. If an adapter's spec.baseModel differs from the pods' label, adapter requests would silently score 0 on every pod, which is where they were before this PR (so not a regression).

It would help to say in the comment that the two must agree, and possibly to log once at V(4) when ModelBaseModel resolves but no context exists for it. This is a note, not something I expect in this PR.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spelling this out. I added a line to the comment saying the two need to match, and left the log out of this PR as you suggested.

return fmt.Errorf("sync indexer not available for KV sync routing")
}
_, prefixHashes := k.syncIndexer.MatchPrefix(modelName, int64(-1), tokens, readyPodsMap)
indexModel, loraID := k.indexContext(modelName)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (test gap): TestKVSyncScoreAllMatchesAdapterBlocks covers ScoreAll, but not the other two call sites of indexContext: this one in PostRouteUpdate and the one in Route. I reverted each of them to modelName, -1 locally and the whole prefixcache package still passed, so a future change could undo either without a test failing. Route is the production path for adapter requests, and PostRouteUpdate is where the speculative AddPrefix goes: before this PR that call created contexts named after the adapter.

A test that routes an adapter request through Route and then checks that the prefix was added under the base model's adapter context (not under a context named after the adapter) would cover both.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, only ScoreAll was covered. I added a test for each of Route and PostRouteUpdate, and they also check that nothing gets indexed under the adapter's own name.

// RemovePrefix removes a specific pod from all prefix entries of the model.
// Callers drop a pod whose whole cache is gone, so the pod is removed from the
// model's adapter contexts too, whatever loraID is passed.
func (s *SyncPrefixHashTable) RemovePrefix(modelName string, loraID int64, podName string) error {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: loraID is now ignored, but the signature and the SyncIndexer interface in pkg/kvevent/interfaces.go (which has no doc for RemovePrefix) still suggest it scopes the call. I read all three callers (manager.go pod deletion, PurgePodPrefixCache for AllBlocksCleared, and CheckSleepStateBackstop) and agree each one drops the pod's whole cache, so the behaviour looks right. But the next caller that wants to clear one adapter would be surprised. Please put the "removes the pod from every context of the model" contract on the interface method too, or rename the parameter to _.

I checked the sibling cases with a throw-away probe (deleted): RemovePrefix(model+"-2", ...) leaves this model's contexts untouched, and ProcessBlockRemoved stays scoped by SourcePod even when two pods use the same engine hash under different contexts. The bot's suggestion to guard the type assertions here isn't needed: the neighbouring code does the same unchecked value.(*ContextData) and the map only ever holds those types.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I added the same note to the interface method so it's clear from there too.

Add tests that fail if either call site goes back to the adapter name with -1, document that RemovePrefix clears every context of the model, and note that an adapter's baseModel has to match the pods' model label.

Signed-off-by: MinJun Choi <[email protected]>
@theminjunchoi

Copy link
Copy Markdown
Contributor Author

@varungup90 Thanks a lot for such a careful review. The mutation checks and the race probe were really helpful. I pushed b1be173 for the nits:

  1. Added tests for an adapter request going through Route and PostRouteUpdate. Each one now fails if its call site is reverted to modelName, -1.
  2. Documented on the SyncIndexer interface that RemovePrefix clears the pod from every context of the model.
  3. Mentioned in the indexContext comment that the adapter's baseModel needs to match the pods' model label.
  4. Fixed the outdated LoraName comment in pkg/kvevent/interfaces.go.

I hadn't thought about the context limit, so thanks for bringing it up. I've left it alone in this PR, but I'll keep it in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants