The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): count worker heartbeats on the primary so replica lag cannot undercount
mateo-berri3217b8edae27
diff --git a/litellm/proxy/db/proxy_worker_heartbeat.py b/litellm/proxy/db/proxy_worker_heartbeat.pyindex 6a2a4572e43..990ff48eb18 100644--- a/litellm/proxy/db/proxy_worker_heartbeat.py+++ b/litellm/proxy/db/proxy_worker_heartbeat.py@@ -20,6 +20,7 @@ from typing_extensions import ReadOnly, TypedDictfrom litellm._logging import verbose_proxy_loggerfrom litellm._uuid import uuid+from litellm.proxy.db.routing_prisma_wrapper import RoutingPrismaWrapperif TYPE_CHECKING:from litellm.proxy.utils import PrismaClient@@ -79,10 +80,13 @@ class ProxyWorkerHeartbeat:async def count_live_proxy_workers(prisma_client: PrismaClient) -> int | None:"""The number of workers with a recent heartbeat, or None when the database- cannot answer. Callers must treat None as "unknown", not as zero.+ cannot answer. Callers must treat None as "unknown", not as zero. Always+ counts on the primary: a lagging read replica must never undercount."""try:- rows: Final = await prisma_client.db.query_raw(COUNT_SQL, PROXY_WORKER_LIVENESS_WINDOW_SECONDS)+ db: Final = prisma_client.db+ primary_db: Final = db.writer if isinstance(db, RoutingPrismaWrapper) else db+ rows: Final = await primary_db.query_raw(COUNT_SQL, PROXY_WORKER_LIVENESS_WINDOW_SECONDS)return _COUNT_ROWS_ADAPTER.validate_python(rows)[0]["live_workers"]except Exception as count_err: # noqa: BLE001 # an unknown count must degrade to "warn", never to a 503verbose_proxy_logger.debug("Live proxy worker count unavailable: %s", count_err)diff --git a/tests/test_litellm/proxy/db/test_proxy_worker_heartbeat.py b/tests/test_litellm/proxy/db/test_proxy_worker_heartbeat.pyindex 2209be0dc2e..33ae6190411 100644--- a/tests/test_litellm/proxy/db/test_proxy_worker_heartbeat.py+++ b/tests/test_litellm/proxy/db/test_proxy_worker_heartbeat.py@@ -12,6 +12,7 @@ from litellm.proxy.db.proxy_worker_heartbeat import (ProxyWorkerHeartbeat,count_live_proxy_workers,)+from litellm.proxy.db.routing_prisma_wrapper import RoutingPrismaWrapperdef _prisma():@@ -67,6 +68,18 @@ async def test_count_reads_workers_within_the_liveness_window():assert prisma.db.query_raw.call_args.args == (COUNT_SQL, PROXY_WORKER_LIVENESS_WINDOW_SECONDS)+async def test_count_reads_from_the_primary_when_reads_route_to_a_replica():+ writer = MagicMock()+ writer.query_raw = AsyncMock(return_value=[{"live_workers": 2}])+ reader = MagicMock()+ reader.query_raw = AsyncMock(return_value=[{"live_workers": 1}])+ prisma = MagicMock()+ prisma.db = RoutingPrismaWrapper(writer=writer, reader=reader)+ assert await count_live_proxy_workers(prisma) == 2+ reader.query_raw.assert_not_awaited()++@pytest.mark.asyncioasync def test_count_returns_unknown_when_the_query_fails():prisma = _prisma()