Skip to content

[Serve][1/5] Expose backend HTTP endpoint metadata - #62667

Merged
kouroshHakha merged 16 commits into
ray-project:masterfrom
eicherseiji:ingress-bypass/1-asgi-substrate
Apr 21, 2026
Merged

kouroshHakha merged 16 commits into
ray-project:masterfrom
eicherseiji:ingress-bypass/1-asgi-substrate

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Why this is needed

Later in the stack, /internal/route returns a replica_id, and the controller / HAProxy layers need to turn that chosen replica into a concrete backend host:port. Replica-owned backend HTTP servers already exist, but their ports are only local runtime state on the replica actor; this PR exposes that endpoint information through RunningReplicaInfo / RunningReplica so replica_id -> backend host:port resolution becomes possible.

Summary

  • add backend_http_port to RunningReplicaInfo
  • populate that field from DeploymentReplica.get_running_replica_info() so controller and proxy layers can resolve a chosen replica to its backend HTTP port
  • expose RunningReplica.backend_http_endpoint as a convenience (host, port) accessor for backend-HTTP-capable replicas
  • add focused unit tests for RunningReplicaInfo hashing and RunningReplica.update_replica_info() refreshing the derived endpoint

This PR is only the metadata substrate. It does not add routing or change request flow by itself.

Stack: this -> #62680 -> #62668 -> #62669 -> #62670

Test plan

  • Added unit tests in python/ray/serve/tests/unit/test_common.py
  • Added unit tests in python/ray/serve/tests/test_actor_replica_wrapper.py
  • python -m py_compile on the edited files
  • git diff --check
  • Repo commit hooks / linters on commit
  • Targeted pytest from the detached PR worktree
    • blocked locally because importing ray from the PR worktree fails on missing built _raylet

@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 introduces a "direct ingress" mechanism for Ray Serve, allowing users to register custom ASGI apps that bypass the standard request handling pipeline. Key changes include the addition of a _DirectIngressRegistry for tracking endpoints, the set_asgi_app API in the context module, and logic within the Replica class to allocate ports and start the custom HTTP server. Feedback focuses on avoiding blocking ray.get calls within the asynchronous replica environment, managing the lifecycle of the registry actor to prevent resource leaks, and removing unused code related to body chunk logging.

Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
Add a generic Serve-level primitive that allows any deployment to register
a custom ASGI app on its replica's direct ingress HTTP port. This enables
the "ingress bypass" pattern where a proxy routes requests directly to
replica-hosted ASGI apps, bypassing the standard Serve request pipeline.

Key changes:
- context.py: set_asgi_app(), inc/dec_num_ongoing_requests() callbacks
- replica.py: deferred custom ASGI app lifecycle, counting middleware,
  _DirectIngressRegistry for endpoint discovery
- common.py: direct_ingress_http_port on RunningReplicaInfo
- deployment_state.py: plumb direct_ingress_http_port through
- replica_wrapper.py: direct_ingress_endpoint property
- handle.py: get_request_router() for local routing decisions

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji force-pushed the ingress-bypass/1-asgi-substrate branch from 8bd37f4 to 72fb5ac Compare April 16, 2026 17:25
@eicherseiji eicherseiji changed the title [Serve] Add set_asgi_app() and custom direct ingress ASGI substrate [Serve][1/4] Add set_asgi_app() and custom direct ingress ASGI substrate Apr 16, 2026
- Remove _DirectIngressRegistry and all its usage (never read by any
  downstream PR; endpoint discovery uses replica_wrapper instead)
- Replace blocking ray.get with asyncio.gather for http/grpc options
- Remove unused first_body_chunk_logged tracking

Signed-off-by: Seiji Eicher <[email protected]>
The counting middleware in _maybe_start_direct_ingress_servers already
handles request counting directly. The context-level callbacks were
never called by anything in the stack.

Signed-off-by: Seiji Eicher <[email protected]>
The retry-loop for port allocation + server start was duplicated between
_start_direct_ingress_for_custom_app and the nested function inside
_maybe_start_direct_ingress_servers. Extract it as a proper method so
both paths share the same implementation.

Signed-off-by: Seiji Eicher <[email protected]>
Delete _start_direct_ingress_for_custom_app, _replica_initialized flag,
and the branching callback. set_asgi_app() just stores the app; the
existing _maybe_start_direct_ingress_servers() handles everything at
init time. Also make set_asgi_app() sync and remove the
_allocate_and_start_server extraction (revert to original nested fn).

Signed-off-by: Seiji Eicher <[email protected]>
Replace the set_asgi_app() context callback with the standard Serve
convention: user callable sets self.direct_ingress_app during __init__
or start(), and the Replica discovers it via getattr (same pattern as
check_health, reconfigure, etc.). Removes context.py from this PR.

Signed-off-by: Seiji Eicher <[email protected]>
…60865

Rename to _get_request_router and add TODO to migrate to
DeploymentHandle.choose_replica() once ray-project#60865 lands.

Signed-off-by: Seiji Eicher <[email protected]>
- test_custom_direct_ingress_app: verifies a deployment that sets
  self.direct_ingress_app gets a direct ingress port and serves
  the custom ASGI app on it
- test_custom_direct_ingress_app_without_global_flag: verifies the
  custom app path works even without RAY_SERVE_ENABLE_DIRECT_INGRESS

Signed-off-by: Seiji Eicher <[email protected]>
Benchmarks showed no measurable overhead from the standard Serve ASGI
pipeline (~0.16ms TTFT difference). Replace the custom ASGI app
mechanism with a simple opt-in: non-ingress deployments set
self.enable_direct_ingress = True to get a direct ingress port
serving the standard Serve handler. Remove set_asgi_app, context
callbacks, counting middleware, and _get_request_router.

Signed-off-by: Seiji Eicher <[email protected]>
LLMServer will be the ingress deployment so it gets direct ingress
ports naturally. No guard clause changes needed. PR 1 is now just
the metadata plumbing to expose direct_ingress_http_port through
RunningReplicaInfo and RunningReplica.direct_ingress_endpoint.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji changed the title [Serve][1/4] Add set_asgi_app() and custom direct ingress ASGI substrate [Serve][1/5] Expose direct_ingress_http_port metadata Apr 16, 2026
- Guard against falsy (0) port values with `or None`
- Include direct_ingress_http_port in RunningReplicaInfo hash
  so long-poll broadcasts updates if the port changes
- Tighten return type to Optional[Tuple[str, int]]

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Apr 20, 2026
@eicherseiji
eicherseiji force-pushed the ingress-bypass/1-asgi-substrate branch from d0f59cb to 4205bc2 Compare April 20, 2026 05:09
@eicherseiji eicherseiji changed the title [Serve][1/5] Expose direct_ingress_http_port metadata [Serve][1/5] Expose direct ingress endpoint metadata Apr 20, 2026
@eicherseiji eicherseiji changed the title [Serve][1/5] Expose direct ingress endpoint metadata [Serve][1/5] Expose backend HTTP endpoint metadata Apr 20, 2026
@eicherseiji

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 introduces the backend_http_port field to RunningReplicaInfo and adds a backend_http_endpoint property to ReplicaWrapper to expose the host and port of a replica's HTTP server. The feedback identifies that the hash calculation for RunningReplicaInfo should also include node_ip and the gRPC port to ensure network changes trigger necessary updates. Additionally, a suggestion was made to ensure both host and port are present in backend_http_endpoint to avoid type hint violations.

Comment thread python/ray/serve/_private/common.py Resolved
@eicherseiji
eicherseiji marked this pull request as ready for review April 20, 2026 05:46
@eicherseiji
eicherseiji requested a review from a team as a code owner April 20, 2026 05:46

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 338959e. Configure here.

multiplexed_model_ids=self.multiplexed_model_ids,
routing_stats=self.routing_stats,
port=self._actor._internal_grpc_port,
backend_http_port=self._actor._http_port or None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Falsy port value silently converted to None

Low Severity

self._actor._http_port or None uses truthiness to coerce the value, which means a legitimate port value of 0 would be silently converted to None. The adjacent line passes self._actor._internal_grpc_port directly without or None, even though both attributes share the same lifecycle (None when unset, positive int when allocated). The or None is both redundant (when the value is already None) and subtly incorrect (it would suppress a valid falsy port), and is inconsistent with the established pattern.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 338959e. Configure here.

@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Apr 20, 2026
eicherseiji and others added 2 commits April 20, 2026 18:52
Fixes AttributeError in test_deployment_state.py unit tests after
backend_http_port was added to DeploymentReplica.get_running_replica_info().

Signed-off-by: Seiji Eicher <[email protected]>

@kouroshHakha kouroshHakha 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.

LGTM

@kouroshHakha
kouroshHakha merged commit c43aa4e into ray-project:master Apr 21, 2026
3 checks passed
HLDKNotFound pushed a commit to chichic21039/ray that referenced this pull request Apr 22, 2026
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants