Repository navigation
fix(proxy-extras): build the remaining request-log indexes in the migration job instead of in migrations - #49667
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! |
|
|
|
bugbot run |
[Medium impact] The PR appears safe to merge with no outstanding findings.
|
df915b8 to
6fd1081
Compare
|
@litereview code-review |
|
bugbot run |
6fd1081 to
f2abb5e
Compare
|
bugbot run |
f2abb5e to
bfc6cb1
Compare
|
bugbot run |
…ration job instead of in migrations Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
bfc6cb1 to
ebdc8d4
Compare
|
@litereview code-review |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit ebdc8d4. Configure here.
Backport the affected integration tests from BerriAI#53940, BerriAI#49310, BerriAI#54111, BerriAI#48665, and BerriAI#49667. Upstream commits: 96ae5fa, fbd126e, c90dfea, 21ab2cb, da04c60 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Handwritten 1-3 sentence description
<make-me-human-readable>Upgrading the proxy no longer blocks spend log writes while old migrations build indexes on the request log tables. Those indexes are now built by the migration job in the background without blocking writes, the same way the newer ones already are
</make-me-human-readable>Details
Link to Devin session: https://app.devin.ai/sessions/be31079aaff64bed97dcc3755ae0707f
Open in Devin Desktop: https://app.devin.ai/desktop/session/be31079aaff64bed97dcc3755ae0707f?variant=devin
Requested by: @yassin-berriai
TLDR
Problem this solves:
CREATE INDEXon request log tablesmigrate deployHow it solves it:
REQUEST_LOG_INDEXES, built concurrently by the migration jobLiteLLM_SpendLogToolIndextoo, so no new one shipsUser Flow
Before: an operator upgrading a proxy whose DB never got these indexes sees spend log writes stall during the migration
litellm --skip_server_startup) against a DB with millions of spend log rowsmigrate deployapplies20260724000000_add_spend_log_tool_index_start_time_idxand the other three with a plainCREATE INDEXAfter: the same upgrade keeps writes flowing
SELECT 1, then the job builds the indexes withCREATE INDEX CONCURRENTLY(per partition on a partitioned SpendLogs)Files changed
litellm/proxy-extras/litellm_proxy_extras/migrations/20250510142544_add_session_id_index_spend_logs/migration.sqlSELECT 1instead of buildingLiteLLM_SpendLogs_session_id_idxlitellm/proxy-extras/litellm_proxy_extras/migrations/20260228100000_add_spend_logs_composite_index/migration.sqlSELECT 1instead of buildingLiteLLM_SpendLogs_startTime_request_id_idxlitellm/proxy-extras/litellm_proxy_extras/migrations/20260226120000_add_spend_log_tool_index/migration.sqlCREATE TABLE, drops thetool_name, start_timeindexlitellm/proxy-extras/litellm_proxy_extras/migrations/20260724000000_add_spend_log_tool_index_start_time_idx/migration.sqlSELECT 1instead of buildingLiteLLM_SpendLogToolIndex_start_time_idxlitellm/proxy-extras/litellm_proxy_extras/request_log_indexes.pyREQUEST_LOG_INDEXEStests/code_coverage_tests/check_migrations_no_data_rewrites.pyLiteLLM_SpendLogToolIndextoREQUEST_LOG_TABLES, drops the two migrations that are clean now fromGRANDFATHEREDlitellm/proxy-extras/litellm_proxy_extras/migration_recovery.pySUPERSEDED_CHECKSUMSlets v2 recovery finish an unfinished20260226120000_add_spend_log_tool_indexrow that still carries the shipped checksum, androll_back_replayable_failed_migration(wasroll_back_failed_inert_migration) rolls back a failed row of that shipped file so the current file replaysLinear ticket
Resolves LIT-10377
Pre-Submission checklist
Screenshots / Proof of Fix
Rebased head ebdc8d4 on main aa166f1
main moved proxy-extras to
litellm/proxy-extrasand added DailyUserSpend and restricted EndUser entries toREQUEST_LOG_INDEXES. Both are kept next to the four indexes here. On a fresh DB the real migration job at the head (DATABASE_URL=<fresh db> PYTHONPATH=$PWD:$PWD/litellm/proxy-extras python -m litellm.proxy.proxy_cli --skip_server_startup) printedA two-worker proxy at the head on that DB, with Redis, served three real
anthropic/claude-haiku-5-5requests in sessionlit-10377-index-proof, one with a forced tool call. All three rows landed inLiteLLM_SpendLogsand the tool row inLiteLLM_SpendLogToolIndex, and the Logs page at/ui/logsshowed them grouped under the session. At the head the unit files pass (562),check_migrations_no_data_rewrites.pypasses over 264 migrations and the boot integration file passes (9). RemovingLiteLLM_SpendLogToolIndex_start_time_idxfromREQUEST_LOG_INDEXESfails the replay test, and it passes once restored. Admin UI screenshots were captured again but cannot be embedded yet because the PR media upload token answers 401Earlier proof at 6fd1081158
Shared setup, run at 6fd1081158 against merge base 4311254. The tip bfc6cb1 adds a test annotation and the failed tool table row rollback on top of it, covered by the tests below. Local Postgres 14. A DB is migrated at the merge base, then the four index migrations are rewound (indexes dropped, ledger rows deleted) so it looks like a DB that never applied them, and filled with 6M
LiteLLM_SpendLogToolIndexrows and 2MLiteLLM_SpendLogsrows. Each leg copies that template and runs the real migration job (python -m litellm.proxy.proxy_cli --skip_server_startup) withPYTHONPATHpointed at that checkout'slitellm-proxy-extras, while a writer inserts one SpendLogs row and one SpendLogToolIndex row every 20ms and records each write's latency. A third leg upgrades a DB fully migrated at the merge base (old checksums in_prisma_migrations) to the tipCommands to rerun the legs
Template DB. Create
tmpl10377, runjob.sh <merge-base checkout>/litellm-proxy-extras tmpl10377 tmpl_job.logon it, thenjob.shwriter.pyleg.sh, run asleg.sh before <merge-base checkout>/litellm-proxy-extras tmpl10377,leg.sh after <tip checkout>/litellm-proxy-extras tmpl10377andleg.sh upgrade <tip checkout>/litellm-proxy-extras full10377Before (4311254)
Upgrade a DB that never applied the four index migrations
After (6fd1081158)
Upgrade a DB that never applied the four index migrations
Upgrade a DB that already applied them at the merge base
The old checksums stay in the ledger, the job applies nothing, rebuilds nothing, and exits 0
Admin UI on the DB the fixed job migrated
A proxy at the tip (
python litellm/proxy/proxy_cli.py --config <dev_config copy with Redis> --num_workers 2 --detailed_debug --reload --use_v2_migration_resolver) was started on the "After" DB, one realanthropic-sonnet-5request was sent withlitellm_session_id=lit-10377-qa, and the Logs page at/ui/logsshowed it next to the seeded rows, and its session view opened on the session the newsession_idindex serves. Screenshots were captured but could not be embedded because the PR media upload token is expiredTests at 6fd1081158, with the touched files rerun at bfc6cb1 (541 unit tests, the migration checker and 8 recovery integration tests pass): the focused unit files pass (486),
check_migrations_no_data_rewrites.pypasses over all 232 migrations, andtests/integration/run.py databasepasses (62 tests) overtests/integration/database/migrations/test_request_log_indexes.pyandtests/integration/database/test_request_log_indexes_at_boot.py, including a v2 migration job run that finishes an unfinished tool table row with the shipped checksum, and the partitioned SpendLogs boot tests that expect all four SpendLogs indexes on every partition. Restoring oneCREATE INDEXfails the inert-migration test, and droppingLiteLLM_SpendLogToolIndexfromREQUEST_LOG_TABLESfails the checker test. Dropping theSUPERSEDED_CHECKSUMSentry fails the recovery test, and limiting the rollback to inert files again makes the failed tool table row test stop on P3009Taxonomy audit
H2: the
SELECT 1SQL comments match the inert migrations already on main (20260823000000_add_spend_logs_api_key_starttime_index,20260831120001_spend_logs_litellm_call_id_index), kept. C: a DB that already has these indexes skips them (valid index check beforeCREATE INDEX CONCURRENTLY IF NOT EXISTS), andmigrate deployaccepts the changed checksums of applied rows, covered by the existing-DB integration test. B: no new shared state, builds run under the existing migration lock. H6: the integration tests copy the shipped SQL bodies on purpose, since their checksums are what recovery matches. R: no concurrent index statement inside a migration transaction. H7, H8, H9 not applicable, no dashboard, Pydantic or workflow changeType
🐛 Bug Fix
Caveats (if any)
Medium
prisma migrate deployignores that for applied rows (third proof leg)SELECT 1files go through the inert rollback, and the tool table file accepts its shipped checksum throughSUPERSEDED_CHECKSUMS. A failed row of the shipped tool table file, as an interrupted blockingCREATE INDEXleaves it, is rolled back and the current file replays, while a failed row with any other checksum stays for the operatorLow
startTimeandend_userindexes, it only runs on an empty DBFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/ae16698145934124b3237569f0a3464a
Open in Devin Desktop: https://app.devin.ai/desktop/session/ae16698145934124b3237569f0a3464a?variant=devin
Requested by: @yassin-berriai
Note
Medium Risk
Changes live Prisma migration SQL and
_prisma_migrationsrecovery logic on large production databases; incorrect checksum/rollback handling could leave pending migrations, though tests focus on upgrade and failed-row edge cases.Overview
Stops blocking
CREATE INDEXsteps on request-log tables duringmigrate deployby turning several shipped migrations into inertSELECT 1(the tool-index migration still createsLiteLLM_SpendLogToolIndexbut no longer builds its indexes inline). Those indexes are added toREQUEST_LOG_INDEXESand built concurrently by the migration job or proxy boot path, same as existing spend-log indexes.Migration recovery is extended for checksum drift:
SUPERSEDED_CHECKSUMStreats legacy tool-index file hashes as completing the current migration, androll_back_replayable_failed_migration(replacing inert-only rollback) can replay failed ledger rows when the file is inert or matches a superseded checksum.The migration safety checker now treats
LiteLLM_SpendLogToolIndexas a request-log table (no new blocking indexes in SQL), with tests covering recovery, rollback, boot, and integration paths.Reviewed by Cursor Bugbot for commit ebdc8d4. Bugbot is set up for automated code reviews on this repo. Configure here.