Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[serve] Replace uuid.uuid4() with getrandbits #49537

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions python/ray/serve/_private/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import threading
import time
import uuid
from abc import ABC, abstractmethod
from collections import defaultdict
from contextlib import contextmanager
Expand Down Expand Up @@ -31,7 +30,7 @@
from ray.serve._private.metrics_utils import InMemoryMetricsStore, MetricsPusher
from ray.serve._private.replica_result import ReplicaResult
from ray.serve._private.replica_scheduler import PendingRequest, ReplicaScheduler
from ray.serve._private.utils import resolve_deployment_response
from ray.serve._private.utils import generate_request_id, resolve_deployment_response
from ray.serve.config import AutoscalingConfig
from ray.serve.exceptions import BackPressureError
from ray.util import metrics
Expand Down Expand Up @@ -564,7 +563,7 @@ async def assign_request(
) -> ReplicaResult:
"""Assign a request to a replica and return the resulting object_ref."""

response_id = uuid.uuid4()
response_id = generate_request_id()
assign_request_task = asyncio.current_task()
ray.serve.context._add_request_pending_assignment(
request_meta.internal_request_id, response_id, assign_request_task
Expand Down
6 changes: 5 additions & 1 deletion python/ray/serve/_private/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ def get_capacity_adjusted_num_replicas(


def generate_request_id() -> str:
return str(uuid.uuid4())
# NOTE(edoakes): we use random.getrandbits because it reduces CPU overhead
# significantly. This is less cryptographically secure but should be ok for
# request ID generation.
# See https://bugs.python.org/issue45556 for discussion.
return str(uuid.UUID(int=random.getrandbits(128), version=4))


def inside_ray_client_context() -> bool:
Expand Down
5 changes: 3 additions & 2 deletions python/ray/serve/tests/test_http_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import ray
from ray import serve
from ray.serve._private.utils import generate_request_id


def test_request_id_header_by_default(serve_instance):
Expand Down Expand Up @@ -154,10 +155,10 @@ async def main():
"""Sending 20 requests in parallel all with the same request id, but with
different request body.
"""
bodies = [{"app_name": f"an_{uuid.uuid4()}"} for _ in range(20)]
bodies = [{"app_name": f"an_{generate_request_id()}"} for _ in range(20)]
connector = TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=connector) as session:
request_id = f"rid_{uuid.uuid4()}"
request_id = f"rid_{generate_request_id()}"
tasks = [
send_request(session, body, request_id=request_id) for body in bodies
]
Expand Down
10 changes: 5 additions & 5 deletions python/ray/serve/tests/unit/test_pow_2_replica_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import random
import sys
import time
import uuid
from typing import Optional, Set

import pytest
Expand All @@ -29,6 +28,7 @@
)
from ray.serve._private.replica_scheduler.pow_2_scheduler import ReplicaQueueLengthCache
from ray.serve._private.test_utils import MockTimer
from ray.serve._private.utils import generate_request_id

TIMER = MockTimer()

Expand Down Expand Up @@ -184,8 +184,8 @@ def fake_pending_request(
args=list(),
kwargs=dict(),
metadata=RequestMetadata(
request_id=str(uuid.uuid4()),
internal_request_id=str(uuid.uuid4()),
request_id=generate_request_id(),
internal_request_id=generate_request_id(),
multiplexed_model_id=model_id,
),
created_at=created_at,
Expand All @@ -195,8 +195,8 @@ def fake_pending_request(
args=list(),
kwargs=dict(),
metadata=RequestMetadata(
request_id=str(uuid.uuid4()),
internal_request_id=str(uuid.uuid4()),
request_id=generate_request_id(),
internal_request_id=generate_request_id(),
multiplexed_model_id=model_id,
),
)
Expand Down
Loading