Skip to content

Commit 85c03be

Browse files
[serve][2/3] Add choose_replica/dispatch to AsyncioRouter (#63254)
Signed-off-by: Jeffrey Wang <jeffreywang@anyscale.com> Co-authored-by: machichima <nary12321@gmail.com>
1 parent 98eecf3 commit 85c03be

5 files changed

Lines changed: 1197 additions & 88 deletions

File tree

python/ray/serve/_private/request_router/replica_wrapper.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,12 @@ async def reserve_slot(
348348
accepted, num_ongoing_requests = await obj_ref
349349
except asyncio.CancelledError:
350350
ray.cancel(obj_ref)
351-
# TODO (#63254): Add error handling for actor-unavailable cleanup.
351+
self._actor_handle.release_slot.remote(slot_token)
352+
raise
353+
except Exception:
354+
# The actor may have reserved the slot before the reply was lost
355+
# (e.g. ActorUnavailableError). `release_slot` is idempotent for unknown
356+
# tokens, so this is safe even when the reservation never actually happened.
352357
self._actor_handle.release_slot.remote(slot_token)
353358
raise
354359

@@ -408,6 +413,11 @@ class ReplicaSelection:
408413
default=False, init=False
409414
) # Tracks if dispatch was called
410415

416+
# Set by dispatch once the result's done-callback is wired up. Read by
417+
# choose_replica's finally to decide whether to fire on_request_completed
418+
# manually (only one of the two paths should fire it).
419+
_completion_callback_registered: bool = field(default=False, init=False)
420+
411421
@property
412422
def address(self) -> str:
413423
"""Returns the replica address in host:port format."""

python/ray/serve/_private/request_router/request_router.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
DeploymentHandleSource,
2525
DeploymentID,
2626
ReplicaID,
27-
ReplicaQueueLengthInfo,
2827
RequestMetadata,
2928
RunningReplicaInfo,
3029
)
@@ -830,17 +829,11 @@ def _insert_pending_request_sorted(
830829
index += 1
831830
queue.insert(index, pending_request)
832831

833-
def on_new_queue_len_info(
834-
self, replica_id: ReplicaID, queue_len_info: ReplicaQueueLengthInfo
835-
):
832+
def on_new_queue_len_info(self, replica_id: ReplicaID, num_ongoing_requests: int):
836833
"""Update queue length cache with new info received from replica."""
837834
if self._use_replica_queue_len_cache:
838-
self._replica_queue_len_cache.update(
839-
replica_id, queue_len_info.num_ongoing_requests
840-
)
841-
self._update_router_queue_len_gauge(
842-
replica_id, queue_len_info.num_ongoing_requests
843-
)
835+
self._replica_queue_len_cache.update(replica_id, num_ongoing_requests)
836+
self._update_router_queue_len_gauge(replica_id, num_ongoing_requests)
844837

845838
def on_send_request(self, replica_id: ReplicaID):
846839
"""Increment queue length cache when a request is sent to a replica."""

0 commit comments

Comments
 (0)