Repository navigation
fix(proxy-extras): bound the lock waits of the partitioned SpendLogs index build - #44109
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…index build Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ed3536f to
7e68c8b
Compare
|
@greptileai please re-review at 7e68c8b (rebased onto current main and squashed; adds the two test fixes from the previous round) |
…index build (BerriAI#44109) Co-authored-by: yassin <yassin@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> (cherry picked from commit 123237c)
…index build (BerriAI#44109) Co-authored-by: yassin <yassin@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> (cherry picked from commit 123237c)
TLDR
Problem this solves:
How it solves it:
User Flow
Before: while a long-running read touches a partition index, every spend log write queues behind the index build
After: the same requests are logged within seconds while the read is still open
Before, http://localhost:4000/ui/?page=logs during the open read (rows never load):
After, same page and same open read (all 5 requests listed):
Relevant issues
Follow-up to #43948, which moved this index build out of the migrations so it would not block writes
Linear ticket
Resolves LIT-9130
Files changed
litellm-proxy-extras/litellm_proxy_extras/request_log_indexes.py_with_bounded_lockruns one DDL step under the migration lock withlock_timeout = 200ms, retriesLockNotAvailablewith capped jittered backoff, holds the migration lock per attempt only, and returns False after 10 tries. ParentCREATE INDEX ... ON ONLYandALTER INDEX ... ATTACH PARTITIONboth go through it, replacing the old 2s x 30 parent-only loop and the unbounded attachHow it runs
On a partitioned
LiteLLM_SpendLogs, Postgres takes a SHARE lock on the parent forCREATE INDEX ON ONLYand an ACCESS EXCLUSIVE lock on the child index forATTACH PARTITION. While either waits for an open transaction, every new INSERT (and every planner that opens the table's indexes, including the Logs page query) queues behind it. A shortlock_timeoutcaps how long that queue can form.flowchart LR A[ensure_request_log_indexes] --> B[_with_bounded_lock] B -->|SET lock_timeout 200ms| C[migration lock + DDL step] C -->|ok| D[done] C -->|LockNotAvailable| E[release migration lock, sleep 0.25s x 2^n, max 8s] E -->|tries left| B E -->|10 tries used| F[return False, next build retries]The step is called from the migration job (
run_migration_job, which reports False so the job is rerun) and from the serving proxy's daemon index thread (start_request_log_index_build, picked up by the next boot). The invalid parent index left by a step that never got its lock is kept, since the next build resumes from it.Pre-Submission checklist
uv run pytest tests/unit/<your_test_file>.py -vScreenshots / Proof of Fix
Shared setup: Postgres 16 (
pg16, the image pinned in.github/workflows/test-postgres.yml), databaseproxy9130with a partitionedLiteLLM_SpendLogs. Before each arm the table is truncated and the call-id index is reset to an un-attached pair: an invalid parentLiteLLM_SpendLogs_litellm_call_id_idx(ON ONLY) and a valid partition index onLiteLLM_SpendLogs_pdefault. A reader session then runsSET enable_seqscan = off, selects through the partition index (holding itsAccessShareLock), and sleeps 300s. The proxy is started withPYTHONPATH=<tree>:<tree>/litellm-proxy-extrasand this config:Each arm sends the same 5 requests, one at a time:
The build is identified by
hasattr(request_log_indexes, "_with_bounded_lock"), printed asMARKER_with_bounded_lockorMARKER_with_bounded_lock ABSENT.Before (cc42a35)
Proxy writes while a reader holds the partition index
MARKER_with_bounded_lock ABSENT .../litellm-base/litellm-proxy-extras/litellm_proxy_extras/request_log_indexes.pycontent=ok http=200(1.0s to 1.6s each)pg_stat_activityabout 2.5 minutes in:00:34:24 Attached index ... Request-log indexes are all in place, only once the reader ended; the 5 rows land after thatIndex build without the proxy (
repro.py, same fixture both arms, 10s blocker)CREATE ON ONLYbehind an open write:max_insert_latency=1.999s inserts_over_0.5s=5 probe_inserts=61ATTACH PARTITIONbehind a child-index reader:max_insert_latency=9.946s inserts_over_0.5s=1 probe_inserts=2After (7e68c8b)
Proxy writes while a reader holds the partition index
MARKER_with_bounded_lock .../litellm/litellm-proxy-extras/litellm_proxy_extras/request_log_indexes.pycontent=ok http=200(1.1s to 1.5s each)pg_stat_activity53s into the reader: onlySELECT pg_sleep(300);, noLockwaiters3, 3, 3, 5, 5, ...withblocked_waiters=0throughoutWaiting for open transactions before attaching LiteLLM_SpendLogs_pdefault_litellm_call_id_idx, thenCould not get the lock for attaching ... leaving it for the next index buildIndex build without the proxy (
repro.py, same fixture both arms, 10s blocker)CREATE ON ONLYbehind an open write:build_result=[True] parent_valid=(True,) max_insert_latency=0.186s inserts_over_0.5s=0 probe_inserts=279ATTACH PARTITIONbehind a child-index reader:build_result=[True] parent_valid=(True,) max_insert_latency=0.197s inserts_over_0.5s=0 probe_inserts=282The proxy arm ran at
bbe32b45cc;request_log_indexes.pythere is byte-identical to7e68c8b8e5(git diff --quiet bbe32b45cc 7e68c8b8e5 -- <file>), the tip only rebased onto newermainand added tests.Tests
tests/proxy_migration_tests/test_request_log_indexes.pygets four Postgres tests. Two confirm the builder's DDL is actually lock-blocked (pg_stat_activity) and then probe inserts for 3s with a 1slock_timeout: one with the parent index waiting on an open write, one with ATTACH waiting on a reader of the child index. Two exhaust the retries (_DDL_LOCK_ATTEMPTS = 2), one for the parent and one for the attach, assert the build returns False with the give-up warning, then release the blocker and assert the next build succeeds and covers every partition. The four lock tests passed 3 runs in a row on Postgres 16; the earlier full-file run was35 passedand the partition/attach/parent tests pass on Postgres 14.Mutation checks (each restored from a
cpbackup, verified withcmp -s):_DDL_LOCK_TIMEOUT = "2s"_under_migration_lockdirectly (unbounded)SET lock_timeoutin the helperQueryCanceledunderstatement_timeout=5sTaxonomy audit
git diff origin/main -- tests/ | grep -E '^-.*assert'is empty)finally, so a failingcommit()cannot skip the jointest-postgres.ymlrunstests/proxy_migration_testswith nopaths:filterCREATE INDEX CONCURRENTLYpath takes no blocking lock and is unchangedType
🐛 Bug Fix
Caveats (if any)
Medium
lock_timeoutto 0, not the prior value; the build owns and closes its connectionLow
Link to Devin session: https://app.devin.ai/sessions/c5d88ea32b4e4a018f64035903078e78
Open in Devin Desktop: https://app.devin.ai/desktop/session/c5d88ea32b4e4a018f64035903078e78?variant=devin
Requested by: @yassin-berriai