Skip to content

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]

chore: merge litellm_internal_staging into litellm_fix_invalid_index_after_migration_deadlock

yassin6a3046794813
diff --cc litellm-proxy-extras/litellm_proxy_extras/utils.py
index a52a9f9185c,71e7e9c683b..2145f891318
--- a/litellm-proxy-extras/litellm_proxy_extras/utils.py
+++ b/litellm-proxy-extras/litellm_proxy_extras/utils.py
@@@ -6,11 -6,16 +6,12 @@@ import shuti
import subprocess
import tempfile
import time
- from dataclasses import dataclass
+ from dataclasses import dataclass, replace
from pathlib import Path
-from typing import Optional
+from typing import TYPE_CHECKING, Final, Optional
+ from litellm_proxy_extras import prisma_toolchain
from litellm_proxy_extras._logging import logger
-from litellm_proxy_extras.replica_identity import (
- REPLICA_IDENTITY_FULL_ENV_VAR,
- apply_replica_identity_full,
-)
from litellm_proxy_extras.prisma_toolchain import (
PRISMA_COMMAND_TIMEOUT_ENV_VAR,
PRISMA_MIGRATE_DEPLOY_TIMEOUT_ENV_VAR,
@@@ -49,29 -46,39 +50,61 @@@ def _get_prisma_env() -> dict
_MIGRATION_TS_RE = re.compile(r"^(\d{14})_")
_MIGRATION_DEADLOCK_MARKER = "deadlock detected"
+INDEX_REPAIR_ADVISORY_LOCK_KEY: Final = int.from_bytes(b"litellm", "big")
+_TRANSIENT_INDEX_SUFFIX_RE: Final = re.compile(r"_cc(?:new|old)\d*$")
+_INVALID_LITELLM_INDEXES_SQL: Final = (
+ "SELECT n.nspname, c.relname, pg_size_pretty(pg_table_size(t.oid)) "
+ "FROM pg_index i "
+ "JOIN pg_class c ON c.oid = i.indexrelid "
+ "JOIN pg_class t ON t.oid = i.indrelid "
+ "JOIN pg_namespace n ON n.oid = t.relnamespace "
+ "WHERE NOT i.indisvalid "
+ " AND c.relkind = 'i' "
+ " AND n.nspname = %s "
+ " AND t.relname LIKE %s "
+ " AND NOT EXISTS (SELECT 1 FROM pg_constraint k WHERE k.conindid = i.indexrelid) "
+ "ORDER BY c.relname"
+)
+
+
+@dataclass(frozen=True, slots=True)
+class _InvalidIndex:
+ schema: str
+ name: str
+ table_size: str
+ MAX_MIGRATE_DEPLOY_ATTEMPTS = 4
+
+
+ @dataclass(frozen=True)
+ class _MigrateAttemptBudget:
+ """Retries left, and the recoveries already run.
+
+ A recovery that lands something new costs nothing, so a database full of
+ objects `prisma db push` created works through them one per pass. Anything
+ that made no progress spends an attempt, so a stuck run still gives up.
+ """
+
+ attempts_left: int
+ recoveries: frozenset[str] = frozenset()
+
+ @property
+ def exhausted(self) -> bool:
+ return self.attempts_left <= 0
+
+ @property
+ def attempt_number(self) -> int:
+ return MAX_MIGRATE_DEPLOY_ATTEMPTS - self.attempts_left + 1
+
+ def spend(self) -> "_MigrateAttemptBudget":
+ return replace(self, attempts_left=self.attempts_left - 1)
+
+ def after_recovery(self, recovery: str) -> "_MigrateAttemptBudget":
+ if recovery in self.recoveries:
+ return self.spend()
+ return replace(self, recoveries=self.recoveries | {recovery})
+
+
_SPEND_LOGS_ALTER_RE = re.compile(r'^ALTER\s+TABLE\s+"LiteLLM_SpendLogs"\s', re.IGNORECASE)
_SPEND_LOGS_ARTIFACT_DROP_RE = re.compile(
r'^DROP\s+TABLE\s+"LiteLLM_SpendLogs_[^"]*"', re.IGNORECASE