Repository navigation
[Serve][LLM][5/5] Preserve KV-aware application routing - #67218
eicherseiji wants to merge 12 commits into
Conversation
## Why this is needed
Multi-model direct streaming deploys each model as an independent Serve
application. This PR adds a lightweight shared OpenAI router application
that maps the model ID in each request to one of those applications,
then asks the request router for the target application to select a
replica. HAProxy sends the original request directly to that replica,
preserving the direct streaming data path.
## Usage
```python
from ray.llm._internal.serve.core.ingress.builder import build_openai_router_app
build_openai_router_app(
{
"model_applications": {
"qwen-0.5b": "llm-model-qwen",
"llama-8b": "llm-model-llama",
},
"llm_configs": [
{"model_loading_config": {"model_id": "qwen-0.5b"}},
{"model_loading_config": {"model_id": "llama-8b"}},
],
}
)
```
The builder is private, requires HAProxy and LLM direct streaming, and
accepts the same dictionary through declarative YAML. It requires
exactly one `LLMConfig` for every model-to-application mapping. Updating
this router does not replace or delete the independently deployed model
applications.
## Design
`RouterApplication` implements the private `POST /internal/route`
protocol introduced in #66507. It reads the model from JSON or form
data, resolves and caches the target application handle, and passes the
routing payload and session ID to the request router for that
application. The response contains the target application name and full
replica ID for HAProxy to validate and dispatch.
Native model routes are delegated without maintaining a duplicate
allowlist. The router itself owns only the global model endpoints: `GET`
and `HEAD /v1/models` and `/v1/models/{model}`. These endpoints do not
wake scale-to-zero model applications. They enforce each target model
API key policy, expose only authorized or public models, and return
vLLM-compatible errors. CORS preflight is handled before model
extraction.
Handle lookup runs in background tasks and retries transient Serve and
Ray failures. Lookup and replica selection remain bounded by the HAProxy
decision deadline. Client errors, unavailable applications, and
router-owned responses use the marked response path from #66507.
This layer uses a fast non-reserving pick and therefore requires an
available target replica. #67087 adds bounded reserving selection for
scale-to-zero targets, and #67218 preserves `KVAwareRouter` behavior in
the shared router process.
## Test plan
- `test_router_application.py` covers model parsing, authentication,
handle lookup, selection, session affinity, errors, and timeouts.
- `test_builder_openai_router_app.py` covers builder validation,
declarative arguments, and router-role propagation.
- `test_openai_router_app_direct_streaming.py` covers the end-to-end
multi-application path and native OpenAI routes.
- Pinned Black 22.10.0 and pre-commit checks on changed files.
Signed-off-by: Seiji Eicher <[email protected]>
Co-authored-by: Seiji Eicher <[email protected]>
6487ec8 to
c7ecfe4
Compare
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
c7ecfe4 to
4e28185
Compare
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
4e28185 to
f410f6c
Compare
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
… ownership Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b9be1e0. Configure here.
| ): | ||
| state = self._kv_states.get(model_id) | ||
| if state is not None and state.tracker is not None: | ||
| await state.tracker.on_replica_updates(source_writer_id, batch) |
There was a problem hiding this comment.
KV events dropped before tracker starts
Medium Severity
RouterApplication becomes ready with tracker still unset, then on_model_lifecycle_events and on_model_replica_updates silently no-op until _resolve_handle finishes. Engine completions and peer reservations that arrive in that window never reach the new replica, so it starts routing with an empty load view and can overfill busy workers after router scale-out.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit b9be1e0. Configure here.
…r strip Signed-off-by: Seiji Eicher <[email protected]>
|
Still in review -- need more time to complete the review (est. Tue / Wed). |


Why this is needed
A shared router application runs each model's request router in its own process. KV-aware selection needs the tracker and prompt token IDs in that same process, and engine lifecycle events must return to the tracker that made the reservation. This change extends the existing LLMRouter integration to independently deployed model applications.
Summary
For a model built with
build_openai_app, set the shared owner's Serve application name in that model's LLM config:The name must match the application deployed with
build_openai_router_app. Omit the setting to use the model's own LLMRouter. To change ownership, drain traffic through the old router before redeploying the model and router configuration.Validation
Local pytest runs used the matching compiled Ray wheel with editable Serve and LLM modules and
--import-mode=importlib -q -rs.test_router.py::TestDirectStreamingLLMRouter,test_router_application.py,test_builder_openai_router_app.py, and the KVtest_tokenizer.py,test_kv_aware_router.py,test_request_lifecycle_tracking.py, andtest_kv_token_tracker.pysuites. The skipped cases requiredynamo.llm. The 63 builder and tokenizer cases were rerun against the final app build changes.test_build_app.pyandtest_application_state.pywith-k 'router or multiplex'.test_haproxy_router_application.py,test_haproxy_api.py::test_ingress_request_router_forward_body_gate_renders, andtest_haproxy.py::test_router_application_dispatches_across_applications.serve.runand declarative deployment verified that the controller reports the configured owner, a second router is rejected, and the model remains at zero replicas.test_openai_router_app_direct_streaming.py::test_shared_kv_router_releases_streaming_reservationverified that the configured shared KV router keeps a reservation while a stream is open and releases it after completion. The test runs real Serve and HAProxy with CPU doubles for generation and Dynamo. It passed its assertions in pytest and exited successfully when run standalone. The local pytest process still hit the native shutdown abort described below.The HAProxy and live smoke checks exited successfully. The LLM and Serve build pytest processes reported the results above and then aborted during native shutdown in the local environment.
AI assistance was used for implementation and local validation.