Skip to content

[Serve] Move shared replica methods from ReplicaBase to Replica, remove abstract base class - #60198

Merged
abrarsheikh merged 12 commits into
ray-project:masterfrom
eicherseiji:replica-refactor
Mar 28, 2026
Merged

abrarsheikh merged 12 commits into
ray-project:masterfrom
eicherseiji:replica-refactor

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

This refactors the Replica class hierarchy to remove ReplicaBase in favor of Replica for unified gRPC and direct ingress handling. Reference PRs:

Related issue number

N/A

Checks

Changes

@eicherseiji
eicherseiji requested a review from a team as a code owner January 16, 2026 01:43
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Jan 16, 2026

@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 refactors the Replica class hierarchy by moving shared methods and logic from the Replica subclass into the ReplicaBase base class. This is a good change that improves code structure and enables better code reuse for other subclasses. A new extensibility hook _get_request_context_kwargs has been added to allow subclasses to easily extend the request context. Additionally, a comprehensive test suite for RPCSerializer has been added, which improves test coverage. My review includes a couple of suggestions to improve code quality and type safety.

Comment thread python/ray/serve/_private/replica.py Resolved
Comment thread python/ray/serve/_private/replica.py Resolved
@eicherseiji
eicherseiji marked this pull request as draft January 16, 2026 01:48
This refactors the Replica class hierarchy to move shared methods into
ReplicaBase, enabling better code reuse for subclasses.

Changes:
- Move _on_request_cancelled() and _on_request_failed() from Replica
  to ReplicaBase as concrete implementations (previously abstract)
- Move _wrap_request() to ReplicaBase with new _get_request_context_kwargs()
  hook for subclasses to extend request context
- Move gRPC handler methods (HandleRequest, HandleRequestStreaming,
  HandleRequestWithRejection, HandleRequestWithRejectionStreaming)
  to ReplicaBase
- Add test_serialization.py for RPCSerializer coverage

The Replica class now only implements _on_initialized() for gRPC
servicer registration.

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

Copy link
Copy Markdown
Contributor Author

python/ray/serve/tests:test_serve_with_tracing is failing on master

@eicherseiji
eicherseiji marked this pull request as ready for review January 16, 2026 04:48
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!



@pytest.mark.timeout(60)
async def test_by_reference_false_raises_error(serve_instance):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing @pytest.mark.asyncio decorator on async test

Medium Severity

The async test function test_by_reference_false_raises_error is missing the @pytest.mark.asyncio decorator. Other async tests in this codebase (e.g., in test_handle_1.py and test_handle_2.py) consistently use this decorator. Without it, pytest-asyncio will not properly execute this as an async test, causing the test to fail or behave unexpectedly.

Fix in Cursor Fix in Web

@eicherseiji

Copy link
Copy Markdown
Contributor Author

Needs the fix #60209

@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Jan 16, 2026
@github-actions

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Feb 5, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

@github-actions github-actions Bot closed this Feb 19, 2026
@eicherseiji eicherseiji reopened this Mar 27, 2026

@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

Comment thread python/ray/serve/tests/test_handle.py Resolved
# Conflicts:
#	python/ray/serve/_private/replica.py
Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji marked this pull request as draft March 27, 2026 00:23
Signed-off-by: Seiji Eicher <[email protected]>
@github-actions github-actions Bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Mar 27, 2026
@eicherseiji eicherseiji changed the title [Serve] Move shared replica methods from Replica to ReplicaBase [Serve] Remove ReplicaBase in favor of Replica Mar 27, 2026
Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji changed the title [Serve] Remove ReplicaBase in favor of Replica [Serve] Move shared replica methods from Replica to ReplicaBase Mar 27, 2026
@@ -1102,6 +1101,34 @@ def __init__(
# Silence spammy false positive errors from gRPC Python
self._event_loop.set_exception_handler(asyncio_grpc_exception_handler)

try:

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.

Checked

@@ -1253,6 +1280,13 @@ def _configure_logger_and_profilers(
}

def _can_accept_request(self, request_metadata: RequestMetadata) -> bool:

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.

Checked

@@ -1333,6 +1367,23 @@ def _record_errors_and_metrics(
exception_type=exception_type,
)

if is_span_recording():

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.

Checked

async def _on_initialized(self):
raise NotImplementedError
await self._maybe_start_direct_ingress_servers()

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.

Checked

def _on_request_cancelled(
self, request_metadata: RequestMetadata, e: asyncio.CancelledError
):
pass
"""Recursively cancel child requests.

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.

Checked

@@ -1772,13 +1853,34 @@ async def shutdown(self):
async def perform_graceful_shutdown(self):
self._shutting_down = True

coros = []
if (

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.

Checked

self._direct_ingress_http_server_task.cancel()
if self._direct_ingress_grpc_server_task:
self._direct_ingress_grpc_server_task.cancel()
async def send_http_response(message, status_code, send):

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.

Checked

@eicherseiji eicherseiji changed the title [Serve] Move shared replica methods from Replica to ReplicaBase [Serve] Move shared replica methods from ReplicaBase to Replica, remove abstract base class Mar 27, 2026
Reverts the class collapse while preserving the metadata->request_metadata
parameter rename in Replica._on_request_cancelled.

Signed-off-by: Seiji Eicher <[email protected]>
Moves all Replica contents into ReplicaBase and removes the Replica
subclass. ReplicaBase is now a concrete class that can be directly
subclassed by users.

Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji requested a review from abrarsheikh March 28, 2026 03:53
@eicherseiji
eicherseiji marked this pull request as ready for review March 28, 2026 03:53
@abrarsheikh
abrarsheikh merged commit f26c313 into ray-project:master Mar 28, 2026
2 checks passed
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
…ve abstract base class (ray-project#60198)

## Why are these changes needed?

This refactors the Replica class hierarchy to remove `ReplicaBase` in
favor of `Replica` for unified gRPC and direct ingress handling.
Reference PRs:
- ray-project#60914
- ray-project#59908
- ray-project#60118

## Related issue number

N/A

## Checks

- [x] I've signed off every commit (by using the -s flag)
- [x] I've run `scripts/format.sh` to lint the changes in this PR
- [ ] I've included any doc changes needed for
https://docs.ray.io/en/master/
- [ ] I've added any new APIs to the API reference
- [x] I've made sure the tests are passing
- [ ] Testing Strategy
  - [x] Unit tests
  - [ ] Release tests
  - [ ] This PR is not tested :(

## Changes

---------

Signed-off-by: Seiji Eicher <[email protected]>
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 unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants