diff --git a/cogstack_model_gateway/common/config/models.py b/cogstack_model_gateway/common/config/models.py index 0cddff9..e2b2a66 100644 --- a/cogstack_model_gateway/common/config/models.py +++ b/cogstack_model_gateway/common/config/models.py @@ -159,6 +159,12 @@ class ModelsDeployment(BaseModel): " Docker network." ), ) + health_check_request_timeout: float = Field( + 20.0, + description="HTTP timeout in seconds for each /readyz health check request to a model", + gt=0, + examples=[10.0, 20.0, 30.0], + ) class ModelsConfig(BaseModel): @@ -276,7 +282,7 @@ class LabelsConfig(BaseModel): These labels are used for: - Identifying CogStack ModelServe containers (CMS labels) - - Marking containers as managed by the gateway (CMG labels) + - Marking containers as managed by the gateway (CMG labels) - Tracking TTL and other metadata """ diff --git a/cogstack_model_gateway/gateway/core/auto_deploy.py b/cogstack_model_gateway/gateway/core/auto_deploy.py index 88149c0..48ab6fe 100644 --- a/cogstack_model_gateway/gateway/core/auto_deploy.py +++ b/cogstack_model_gateway/gateway/core/auto_deploy.py @@ -37,13 +37,19 @@ def is_model_running(model_name: str) -> bool: ) -async def wait_for_model_health(model_name: str, timeout: int, check_interval: float = 1.0) -> bool: +async def wait_for_model_health( + model_name: str, + timeout: int, + check_interval: float = 1.0, + request_timeout: float = 20.0, +) -> bool: """Poll model's /readyz endpoint until healthy or timeout. Args: model_name: Name of the model timeout: Maximum seconds to wait check_interval: Seconds between health checks (exponential backoff) + request_timeout: HTTP timeout in seconds for each /readyz request Returns: True if model became healthy, False if timeout @@ -57,7 +63,7 @@ async def wait_for_model_health(model_name: str, timeout: int, check_interval: f async with httpx.AsyncClient(verify=False) as client: while time.time() - start_time < timeout: try: - response = await client.get(url, timeout=5.0) + response = await client.get(url, timeout=request_timeout) if response.status_code == 200: elapsed = time.time() - start_time log.info("Model '%s' is ready (took %.1fs)", model_name, elapsed) @@ -70,7 +76,11 @@ async def wait_for_model_health(model_name: str, timeout: int, check_interval: f ) except (httpx.RequestError, httpx.TimeoutException) as e: log.debug( - "Health check failed for '%s': %s, retrying in %.1fs", model_name, e, interval + "Health check failed for '%s' (%s): %r, retrying in %.1fs", + model_name, + url, + e, + interval, ) await asyncio.sleep(interval) @@ -157,6 +167,8 @@ async def ensure_model_available( Returns: True if model is available and ready, False otherwise """ + request_timeout = config.models.deployment.health_check_request_timeout + # Step 1: Check if model is currently running if is_model_running(model_name): log.debug("Model '%s' container is running, checking health", model_name) @@ -165,7 +177,7 @@ async def ensure_model_available( url = get_cms_url(resolve_model_host(model_name), "readyz") try: async with httpx.AsyncClient(verify=False) as client: - response = await client.get(url, timeout=5.0) + response = await client.get(url, timeout=request_timeout) if response.status_code == 200: log.debug("Model '%s' is running and healthy", model_name) @@ -193,7 +205,7 @@ async def ensure_model_available( response.status_code, ) except (httpx.RequestError, httpx.TimeoutException) as e: - log.info("Model '%s' is running but health check failed: %s", model_name, e) + log.info("Model '%s' is running but health check failed (%s): %r", model_name, url, e) else: log.info("Model '%s' is not currently running", model_name) @@ -225,6 +237,7 @@ async def ensure_model_available( is_healthy = await wait_for_model_health( model_name, config.models.deployment.auto.health_check_timeout, + request_timeout=request_timeout, ) if is_healthy: # Mark as ready (the other worker might have crashed after deploying) @@ -256,6 +269,7 @@ async def ensure_model_available( is_healthy = await wait_for_model_health( model_name, config.models.deployment.auto.health_check_timeout, + request_timeout=request_timeout, ) if is_healthy: model_manager.mark_model_ready(model_name) @@ -269,6 +283,7 @@ async def ensure_model_available( is_healthy = await wait_for_model_health( model_name, config.models.deployment.auto.health_check_timeout, + request_timeout=request_timeout, ) if is_healthy: diff --git a/config.example.jsonc b/config.example.jsonc index 16da542..f510121 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -72,7 +72,8 @@ "require_model_uri_validation": false }, "static": {}, - "use_ip_addresses": false + "use_ip_addresses": false, + "health_check_request_timeout": 20.0 } }, "labels": { diff --git a/tests/unit/common/test_config.py b/tests/unit/common/test_config.py index 3a39b79..f0d85a4 100644 --- a/tests/unit/common/test_config.py +++ b/tests/unit/common/test_config.py @@ -192,6 +192,12 @@ def test_config_models_deployment(): assert config.models.deployment.manual.allow_ttl_override is True assert hasattr(config.models.deployment, "static") + assert config.models.deployment.health_check_request_timeout == 20.0 + + custom_config = Config.model_validate( + {"models": {"deployment": {"health_check_request_timeout": 30.0}}} + ) + assert custom_config.models.deployment.health_check_request_timeout == 30.0 def test_config_labels():