Skip to content
Closed
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
6 changes: 6 additions & 0 deletions cogstack_model_gateway/common/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ class ModelsDeployment(BaseModel):
" Docker network."
),
)
health_check_request_timeout: float = Field(
10.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):
Expand Down
25 changes: 20 additions & 5 deletions cogstack_model_gateway/gateway/core/auto_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 5.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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion config.example.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"require_model_uri_validation": false
},
"static": {},
"use_ip_addresses": false
"use_ip_addresses": false,
"health_check_request_timeout": 10.0
}
},
"labels": {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/common/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == 10.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():
Expand Down
Loading