| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Utilities for checking endpoint readiness.""" |
| 4 | |
| 5 | import asyncio |
| 6 | import time |
| 7 | |
| 8 | import aiohttp |
| 9 | from tqdm.asyncio import tqdm |
| 10 | |
| 11 | from vllm.logger import init_logger |
| 12 | |
| 13 | from .endpoint_request_func import RequestFunc, RequestFuncInput, RequestFuncOutput |
| 14 | |
| 15 | logger = init_logger(__name__) |
| 16 | |
| 17 | |
| 18 | async def wait_for_endpoint( |
| 19 | request_func: RequestFunc, |
| 20 | test_input: RequestFuncInput, |
| 21 | session: aiohttp.ClientSession, |
| 22 | timeout_seconds: int = 600, |
| 23 | retry_interval: int = 5, |
| 24 | ) -> RequestFuncOutput: |
| 25 | """Wait for an endpoint to become available before starting benchmarks. |
| 26 | |
| 27 | Args: |
| 28 | request_func: The async request function to call |
| 29 | test_input: The RequestFuncInput to test with |
| 30 | session: The aiohttp session used to issue the probe requests |
| 31 | timeout_seconds: Maximum time to wait in seconds (default: 10 minutes) |
| 32 | retry_interval: Time between retries in seconds (default: 5 seconds) |
| 33 | |
| 34 | Returns: |
| 35 | RequestFuncOutput: The successful response |
| 36 | |
| 37 | Raises: |
| 38 | ValueError: If the endpoint doesn't become available within the timeout |
| 39 | |
| 40 | """ |
| 41 | deadline = time.perf_counter() + timeout_seconds |
| 42 | output = RequestFuncOutput(success=False) |
| 43 | print(f"Waiting for endpoint to become up in {timeout_seconds} seconds") |
| 44 | |
| 45 | with tqdm( |
| 46 | total=timeout_seconds, |
| 47 | bar_format="{desc} |{bar}| {elapsed} elapsed, {remaining} remaining", |
| 48 | unit="s", |
| 49 | ) as pbar: |
| 50 | while True: |
| 51 | # update progress bar |
| 52 | remaining = deadline - time.perf_counter() |
| 53 | elapsed = timeout_seconds - remaining |
| 54 | update_amount = min(elapsed - pbar.n, timeout_seconds - pbar.n) |
| 55 | pbar.update(update_amount) |
| 56 | pbar.refresh() |
| 57 | if remaining <= 0: |
| 58 | pbar.close() |
| 59 | break |
| 60 | |
| 61 | # ping the endpoint using request_func |
| 62 | try: |
| 63 | output = await request_func( |
| 64 | request_func_input=test_input, session=session |
| 65 | ) |
| 66 | if output.success: |
| 67 | pbar.close() |
| 68 | return output |
| 69 | else: |
| 70 | err_last_line = str(output.error).rstrip().rsplit("\n", 1)[-1] |
| 71 | logger.warning("Endpoint is not ready. Error='%s'", err_last_line) |
| 72 | except aiohttp.ClientConnectorError: |
| 73 | pass |
| 74 | |
| 75 | # retry after a delay |
| 76 | sleep_duration = min(retry_interval, remaining) |
| 77 | if sleep_duration > 0: |
| 78 | await asyncio.sleep(sleep_duration) |
| 79 | |
| 80 | return output |