Repository navigation
[Serve] Gate ingress request router body forwarding behind escape hatch - #63183
kouroshHakha merged 95 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a direct streaming mode for Ray LLM Serve, allowing the LLMServer deployment to act as its own ingress. Key changes include the addition of a late-bound ASGI application construction mechanism via serve_build_asgi_app, the implementation of a new LLMRouter for replica-level routing, and updates to HAProxy templates to support optional request body forwarding. A critical feedback point identifies that LLMServer must ensure its engine is started before attempting to build the ASGI app to avoid initialization errors.
| async def __serve_build_asgi_app__(self): | ||
| return await self.engine.build_asgi_app() |
There was a problem hiding this comment.
In the direct streaming path, LLMServer acts as the ingress deployment. Ray Serve will instantiate the replica and call __serve_build_asgi_app__ to initialize the ASGI application. However, self.engine and its internal state (such as _vllm_args in VLLMEngine) are only initialized in the start() method, which is not automatically called by Serve's lifecycle for ingress deployments.
If start() hasn't been called, self.engine will be None or missing required attributes, leading to an AttributeError when build_asgi_app() is invoked. You should ensure that self.start() is called and awaited before accessing the engine.
| async def __serve_build_asgi_app__(self): | |
| return await self.engine.build_asgi_app() | |
| async def __serve_build_asgi_app__(self): | |
| await self.start() | |
| return await self.engine.build_asgi_app() |
Wire up the controller to support "ingress bypass" mode where a router deployment (e.g. OpenAiIngress) serves /internal/route for Lua routing decisions, while non-router deployments (e.g. LLMServer) serve data plane traffic directly via their direct ingress ports. Key changes: - controller.py: _get_target_groups_for_app_with_router splits targets into router_targets (for Lua) and main targets (data plane) - application_state.py: track router deployment name per app - build_app.py, client.py, deploy_utils.py, deployment_info.py: plumb router flag through deployment args - default_impl.py: factory plumbing for router deployment - schema.py: router_targets field on TargetGroup Signed-off-by: Seiji Eicher <[email protected]>
- Remove unused _summarize_target, _summarize_target_groups, _summarize_fallback_targets helpers - Remove unused get_fallback_targets_for_proxy_manager method - Revert unrelated RAY_SERVE_THROUGHPUT_OPTIMIZED env var plumbing - Fix stale docstring referencing OpenAiIngress 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]>
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]>
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]>
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]>
This reverts commit 9600be7f0e4337fa1e8911aba1dd05425d4451f7. 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]>
Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
Extend HAProxy configuration to support Lua-based custom request routing for the ingress bypass pattern. When a backend has router_targets configured, HAProxy calls /internal/route on a router replica to get the target host:port, then forwards the data plane request directly to that replica. Key changes: - haproxy.py: BackendConfig gains router_servers, custom_request_routing, max_server_conns, router_server fields; reload tracking; is_head readiness fix; summarize helpers for debugging - haproxy_templates.py: Lua route template for /internal/route calls Signed-off-by: Seiji Eicher <[email protected]>
- Remove streaming-only restriction: route ALL POST requests through Lua when ingress bypass is enabled, not just streaming ones - Remove killswitch file check (/tmp/ray-serve-disable-ingress-bypass-routing) - Reduce Lua socket timeout from 5s to 500ms for fast fallback - Revert enable_hap_optimization splitting back to single flag - Remove reload tracking (_reload_count, _last_reload_mode) - Remove _hard_reload and _reload_with_config indirection - Remove get_reload_debug_summary Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
- Remove _summarize_target, _summarize_target_group, _summarize_backend_config (defined but never called) - Add TODO on single router_server selection noting the HA gap Signed-off-by: Seiji Eicher <[email protected]>
The expensive part of body forwarding lives in HAProxy + Lua (the `wait-for-body` round-trip, `tune.bufsize` connection cost, and body re-emit on the socket). On the LLMRouter side, `await request.body()` on a Content-Length: 0 request is essentially free (one async tick, returns b""), so gating it earns nothing. Reverts the LLM-side branch and keeps the body read unconditional, making this PR Serve-only. Signed-off-by: Seiji Eicher <[email protected]>
2ba9e2b to
0390ad4
Compare
Brings in ray-project#63215 (fail-loud) and resolves the conflict in ``ingress_request_router.lua.tmpl`` against this branch's FORWARD_BODY gate. Resolution: drop the ``empty_body`` sentinel that ray-project#63215 added. With the FORWARD_BODY gate, an empty body is no longer a distinct failure mode -- both modes call the router with ``body=""`` and let the router decide. The "router or fail" invariant still holds because the router *is* called; we just don't synthesize a sentinel locally. The 4 remaining failure-mode sentinels from ray-project#63215 (``router_unreachable`` / ``router_non_200`` / ``unparseable_replica_id`` / ``unknown_replica_id``) are unchanged. ``test_router_failure_fails_loud_with_reason`` updates fall out of the resolution: both empty and non-empty POSTs now surface as ``router_non_200`` against the broken-router stub. The empty-body case stays in the test to pin that empty bodies are routed through the router rather than silently bypassing it. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Pins that ``tune.bufsize``, ``wait-for-body``, and the Lua ``FORWARD_BODY`` constant render consistently with the env var. Off by default; on only when ``RAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY=1``. Catches the failure mode where any one of the three signals drifts out of sync with the others. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Three pieces of dead code that drifted into this branch but aren't referenced by any FORWARD_BODY-related change and aren't reachable from runtime code paths: - ``router.py``: ``DIRECT_ROUTER_*`` env-var name constants and the ``ingress_request_router_app`` FastAPI placeholder. Defined but unused; staged for unrelated work. - ``haproxy.py``: free-function ``_get_safe_name`` and ``target_to_server`` duplicates of ``HAProxyManager._target_to_server`` / ``HAProxyManager.get_safe_name``. Never called. - ``test_router.py``: a second copy of ``_DirectRouterReplicaId`` / ``_FakeRequest`` / ``_DirectRouterReplica`` / ``_new_direct_router`` appended above an identical existing block (Python uses the second definition, making the first dead). Likely a merge artifact. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Frame the default as a TTFR concern rather than a policy-specific one: buffering and re-emitting large request bodies adds noticeable time-to- first-response. Call out concrete policy categories so a reader knows when to flip the flag without grepping the routers: - round-robin / power-of-two: body-independent, leave off. - session-aware: keys on ``x-session-id`` header, leave off. - prefix-aware / prefix-cache: needs the body, flip on. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Three failures in test_haproxy_api after merging master:
1. ``test_write_ingress_request_router_lua_no_routers``: assertion
``result == (None, False)`` was stale. ``_write_ingress_request_router_lua``
returns just the Lua path (or ``None``), not a tuple. Fix to
``assert result is None``.
2. ``test_ingress_request_router_does_not_leak_into_other_backends``: had
embedded assertions that ``wait-for-body`` and ``tune.bufsize 65536``
appear in the rendered config. Both are wrong on master:
``wait-for-body`` is gated behind ``FORWARD_BODY=true`` (off by
default), and the bufsize default is 262144 not 65536. Drop these
assertions; their FORWARD_BODY-aware replacement is
``test_ingress_request_router_forward_body_gate_renders``.
3. ``test_ingress_request_router_end_to_end``: assertion
``router_captured["bodies"] == ['{"prompt": "hello"}']`` failed because
``_create_router_server``'s readiness probe POSTs ``{}`` to the same
``/internal/route`` handler that records bodies, so the first capture
was always the probe body. Clear ``captured["bodies"]`` after the
probe completes so callers see only client traffic.
Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 380647f. Configure here.
| # decision, e.g. prefix-aware / prefix-cache routing. | ||
| RAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY = get_env_bool( | ||
| "RAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY", False | ||
| ) |
There was a problem hiding this comment.
Wrong default type passed to get_env_bool
Low Severity
get_env_bool documents and expects a string "0" or "1" as the default parameter, but False (a Python boolean) is passed here. Every other call site in the codebase passes "0" or "1". It happens to work because str(False) produces "False" which doesn't equal "1", but it violates the function's documented contract and is inconsistent with the established pattern.
Reviewed by Cursor Bugbot for commit 380647f. Configure here.
jeffreywang88
left a comment
There was a problem hiding this comment.
LGTM, leaving nits. Is this "escape hatch" approach a long-term solution we're going for?
| # Use file locking to prevent concurrent writes from multiple processes | ||
| # This is important in test environments where multiple nodes may run | ||
| # on the same machine | ||
| import fcntl |
There was a problem hiding this comment.
nit: move imports to top
| return lua_path | ||
|
|
||
| def _generate_config_file_internal(self) -> None: | ||
| def _generate_config_file_internal(self) -> bool: |
There was a problem hiding this comment.
we aren't returning bool
| # header (forwarded with the request line) rather than the body. | ||
| # | ||
| # Flip this to true if the configured request router needs the body for its | ||
| # decision, e.g. prefix-aware / prefix-cache routing. |
There was a problem hiding this comment.
does this imply that we might underperform other LLM routers under prefix-cache affinity routing policy?
There was a problem hiding this comment.
This is a fundamental system trade off and not so much of a implementation trade off, but we'll see. Your work of extending to other request routers need to be done for us to be able to answer this question
| # decision, e.g. prefix-aware / prefix-cache routing. | ||
| RAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY = get_env_bool( | ||
| "RAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY", False | ||
| ) |
For now, we couldn't think of a better approach to turn it on / off, we have to do a study for when we do other request routing policies that need the request body |
…ch (ray-project#63183) Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Kourosh Hakhamaneshi <[email protected]> Co-authored-by: Kourosh Hakhamaneshi <[email protected]>
…ch (ray-project#63183) Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Kourosh Hakhamaneshi <[email protected]> Co-authored-by: Kourosh Hakhamaneshi <[email protected]> Signed-off-by: Alexandr Plashchinsky <[email protected]>
## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [#64224](#64224), [#64328](#64328), and [#65095](#65095) use it for KV-aware routing. - [#65680](#65680) uses it for LoRA model selection. - [#66258](#66258) and [#66315](#66315) use it for deployment-aware, multi-model direct streaming. - [#63183](#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>
…t#66360) ## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [ray-project#64224](ray-project#64224), [ray-project#64328](ray-project#64328), and [ray-project#65095](ray-project#65095) use it for KV-aware routing. - [ray-project#65680](ray-project#65680) uses it for LoRA model selection. - [ray-project#66258](ray-project#66258) and [ray-project#66315](ray-project#66315) use it for deployment-aware, multi-model direct streaming. - [ray-project#63183](ray-project#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>
…t#66360) ## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [ray-project#64224](ray-project#64224), [ray-project#64328](ray-project#64328), and [ray-project#65095](ray-project#65095) use it for KV-aware routing. - [ray-project#65680](ray-project#65680) uses it for LoRA model selection. - [ray-project#66258](ray-project#66258) and [ray-project#66315](ray-project#66315) use it for deployment-aware, multi-model direct streaming. - [ray-project#63183](ray-project#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>


Stacked on #63167.
Round-robin
/internal/routeignores the request body, so make body forwarding opt-in viaRAY_SERVE_INGRESS_REQUEST_ROUTER_FORWARD_BODY(off by default). When off, HAProxy skipswait-for-body/tune.bufsizeand the Lua POSTsContent-Length: 0. When on, prior body-forwarding behavior is restored unchanged for body-aware policies (prefix-cache, etc.).LLMRouter is unchanged:
await request.body()on a Content-Length: 0 request is essentially free, so no gate is needed there._pick_replica'srequest_body/body_truncatedparameters stay so flipping the flag on doesn't touch call sites.Test plan
test_ingress_request_router_end_to_endflips the flag on viamonkeypatch; body-forwarded path still covered.