Skip to content

refactor(rust): track ClickHouse migrations in a checksummed ledger - #44580

Merged
yujonglee-berri merged 4 commits into
mainfrom
litellm_clickhouse_migration_ledger
Oct 5, 2026
Merged

yujonglee-berri merged 4 commits into
mainfrom
litellm_clickhouse_migration_ledger

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Every restart replays all tracing schema migrations
  • Failed HTTP commands can appear successful and enter the ledger
  • Quoted migration versions can break tracing on restart

How it solves it:

  • Run tracing migrations through SQLx's runner with a small ClickHouse adapter
  • Record checksums in _sqlx_migrations and apply only pending migrations
  • Require synchronous commands and validate bounded response bodies
  • Accept numeric and quoted versions when reading the ledger
  • Reapply retention from the current RETENTION policy on every start

Intentional product change: editing an applied migration disables tracing at startup so an incompatible schema cannot silently continue. The proxy still serves traffic

User Flow

Before: restarting repeats schema work even when nothing changed

  1. Configure ClickHouse tracing and start the proxy
  2. GET http://localhost:4000/health/liveliness returns 200 and tracing starts
  3. Restart the proxy, and ClickHouse records the same schema commands again

After: restarting preserves the applied schema and refreshes retention

  1. Configure ClickHouse tracing and start the proxy
  2. GET http://localhost:4000/health/liveliness returns 200 and tracing starts
  3. Restart the proxy, and ClickHouse receives no repeated table creation
  4. Restart with a new retention_days, and every trace table picks up the new TTL

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

Local validation: 212 Rust tests against real ClickHouse, 75 Python tracing tests, cargo check --workspace --all-targets, Clippy with warnings denied, and formatting passed. Greptile scored 5/5 at 19ffc67. CI is pending at this tip

Screenshots / Proof of Fix

Live ClickHouse 26.9.6.6 and the proxy on localhost:4000. Before ran against localhost:18123 with merge-base Python source and a cached native extension whose Rust source matches the merge base. After ran against localhost:8123 with the native extension rebuilt from 19ffc67

Set CLICKHOUSE_URL to your authenticated ClickHouse URL with output_format_json_quote_64bit_integers=1&async_insert=1&wait_for_async_insert=0. Set CLICKHOUSE_DATABASE to pr44580_live_before_base for Before and pr44580_live_after_sqlx for After. Start with LITELLM_RUST=1 python -m litellm.proxy.proxy_cli --config config.yaml --port 4000

general_settings:
  master_key: sk-pr44580-local
  tracing:
    store:
      type: clickhouse
      url: os.environ/CLICKHOUSE_URL
      database: os.environ/CLICKHOUSE_DATABASE
      retention_days: 7

The local curl configuration supplied authentication privately. Substitute your own credentials and port when reproducing

curl -fsS --user "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" http://localhost:8123/ \
  --data-binary 'SYSTEM FLUSH LOGS'
curl -fsS --user "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" http://localhost:8123/ \
  --data-binary "SELECT count() AS creates FROM system.query_log WHERE type='QueryFinish' AND query LIKE 'CREATE TABLE IF NOT EXISTS \`$CLICKHOUSE_DATABASE\`.otel_traces%' FORMAT JSONEachRow"

Before (02f61c9)

  1. Start the proxy. curl -sS -w '\nHTTP %{http_code}\n' http://localhost:4000/health/liveliness returns "I'm alive!" and HTTP 200
  2. Run the shared ClickHouse commands: {"creates":1}. Query EXISTS TABLE pr44580_live_before_base.schema_migrations: 0
  3. Stop and restart the proxy. Repeat the health curl: "I'm alive!" and HTTP 200. Repeat the ClickHouse commands: {"creates":2}. No ledger exists

After (19ffc67)

  1. Start the proxy. The health curl returns "I'm alive!" and HTTP 200
  2. Run the shared ClickHouse commands: {"creates":1}. Query SELECT count() AS rows, uniqExact(version) AS versions FROM pr44580_live_after_sqlx._sqlx_migrations WHERE success FORMAT JSONEachRow: {"rows":15,"versions":15}. system.tables shows toIntervalDay(7) TTLs on otel_traces, agent_traces_by_key, and spend_logs
  3. Stop and restart the proxy. The health curl returns HTTP 200, creates stay at {"creates":1}, the ledger stays at {"rows":15,"versions":15}, and the log has no "tracing unavailable" line
  4. Restart with retention_days: 3. The health curl returns HTTP 200, creates and ledger are unchanged, and all three TTLs now show toIntervalDay(3)
  5. Set the stored checksum of version 1 to '00' and restart. The health curl still returns HTTP 200, and the log shows Agent tracing unavailable: migration 1 was previously applied but has been modified

Type

Bug Fix, Refactoring

Caveats

Severe

  • Existing databases replay schema once when adopting the ledger
  • Editing an applied migration disables tracing until resolved

Medium

  • Concurrent starts or interrupted ledger writes can replay migrations
  • Startup migrations must remain replay-safe schema changes
  • Backfills require separate coordinated jobs
  • The adapter relies on SQLx's hidden Migrator::run_direct, pinned at 0.9.0

Low

  • Concurrent starts can create duplicate ledger rows, safely tolerated
  • Retention TTL changes go in RETENTION, not new migrations

Final Attestation

  • Tests cover migration failures, quoted versions, bounded reads, concurrency, adoption, checksum changes, and retention changes

Link to Devin session: https://app.devin.ai/sessions/048172f0828249fd840be1c3fbee23d4
Open in Devin Desktop: https://app.devin.ai/desktop/session/048172f0828249fd840be1c3fbee23d4?variant=devin
Requested by: @yujonglee-berri

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

devin-ai-integration Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Critical risk] Replaces database migration system with checksummed ledger tracking.

The PR appears safe to merge based on the reviewed changes and previous thread state.

Summary

The PR replaces the custom migration tracker with a SQLx-backed ClickHouse ledger, validates synchronous migration responses, and reconciles retention on startup.

  • Tests cover pending migrations, checksum mismatches, quoted versions, and retention updates.

Reviews (2) · Last reviewed commit: "docs(rust): keep ClickHouse retention TT..."

@codecov

codecov Bot commented Oct 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

yujonglee-berri and others added 2 commits October 5, 2026 15:51
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai

@yujonglee-berri
yujonglee-berri enabled auto-merge (squash) October 5, 2026 15:58
@yujonglee-berri
yujonglee-berri merged commit fdc0e7d into main Oct 5, 2026
93 of 95 checks passed
@yujonglee-berri
yujonglee-berri deleted the litellm_clickhouse_migration_ledger branch October 5, 2026 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant