Repository navigation
ci(backend): Copy per-worker Snuba databases instead of bootstrapping each - #131172
Conversation
… each Backend shards spend about 80 CPU-seconds of ClickHouse during setup, on a 4-core runner that is already pegged, running `snuba bootstrap` five times (snuba-snuba-1 plus one per xdist worker). Almost all of that is ~420 ALTERs of wide tables per database. Bootstrap only default_gw0 and copy it to the other workers with BACKUP ... TO Memory / RESTORE ... AS, which rewrites database references in materialized views and Merge tables. The copies are identical to a bootstrapped database, schema and migration state, and take under a second. If the copy fails, fall back to bootstrapping each worker. Also load a CI ClickHouse profile (no query log, processor profiles or query/memory profiler) and logger level before any per-worker Snuba connects, so it reaches Snuba's sessions, unlike #129369. A new step prints query_log rows per database to show it. The resource monitor now takes `--phase NAME` and attributes samples to phases exactly (setup, snuba-bootstrap, tests), so ClickHouse CPU can be split between setup and the test run. Co-Authored-By: Joshua Li <[email protected]>
|
Results from run 43539870699 (all 30 shards passed, full suite with PR backend shards run on Arm, and every shard here and in the baselines got the same CPU (
Across the 30 shards, that's about 8 runner-minutes and 32 ClickHouse CPU-minutes saved per full run.
Open follow-ups, not in this PR:
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 62db1d5. Configure here.
| clone_databases(workers[0], workers[1:]) | ||
| except (URLError, OSError) as e: | ||
| log(f"::warning::Copying {worker_db(workers[0])} failed ({e}), bootstrapping each") | ||
| run_parallel(bootstrap, workers[1:]) |
There was a problem hiding this comment.
Failed copy fallback leaves partial databases
Medium Severity
A failed RESTORE can leave a half-created default_gwN database. The fallback then runs CREATE DATABASE IF NOT EXISTS and snuba bootstrap --force on that leftover, so migrations may skip or collide instead of rebuilding a clean schema.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 62db1d5. Configure here.
…ck (#131652) Follow-up to #131172, fixing [this Bugbot finding](#131172 (comment)). `bootstrap-snuba.py` bootstraps `default_gw0`, then copies it to the other worker databases with RESTORE. When a copy failed, the fallback bootstrapped on top of whatever was left over: - A RESTORE that fails partway leaves a half-created `default_gwN`. Its partial `migrations_local` can make snuba bootstrap skip migrations, so the tests would run against a broken schema. - A client timeout is an `OSError`, so it started the fallback while ClickHouse could still be running the RESTORE on the server. Over HTTP, ClickHouse doesn't cancel a non-readonly query when the client disconnects. Each RESTORE now gets a known query ID. The fallback first kills any of those queries that are still running and waits for them to stop. Then it drops every target database, and only then bootstraps them. ClickHouse documents KILL QUERY as the way to cancel a restore. If the kill or a drop fails, phase 1 fails instead of bootstrapping on top of a partial database. The copy-or-fallback logic moves out of `main()` into `copy_or_bootstrap()` so it can be unit tested. The happy path doesn't change. <!-- junior-request-attribution:start --> via @joshuarli. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:agent%3A38db8d2dfa34cd3e58644f0c2d132b68 --> -- [View Session](https://junior-prod.sentry.dev/conversations/agent%3A38db8d2dfa34cd3e58644f0c2d132b68) [[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A38db8d2dfa34cd3e58644f0c2d132b68/?project=4510944073809921) <!-- junior-session-footer:end --> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Joshua Li <[email protected]>


Most of ClickHouse's CPU on a backend shard goes to setup. Phase 1 of
bootstrap-snuba.pyrunssnuba bootstrap --forcefive times at once: snuba-snuba-1'sdefault, plusdefault_gw0..3. Each one is ~700 DDL statements, mostly ALTERs of wide tables. On master x64 shard 21, ClickHouse used 80 of its 129 CPU-s in that ~37s window, about 2.2 of the runner's 4 cores, while the machine was pegged.default_gw0, then runsBACKUP DATABASE ... TO MemoryandRESTORE ... AS default_gwNfor the other workers. RESTORE rewrites the database in materialized views and in theMergetable. The copies match a bootstrapped database (SHOW CREATEfor every table plusmigrations_local) and take under a second. If the copy fails, it falls back to bootstrapping each worker. Locally this cut ClickHouse CPU for the bootstrap from 54 to 11 CPU-s.users.dprofile turns offlog_queries, processor profiles, and the query and memory profilers, and the logger level drops fromtracetowarning. Both reload without a restart. Profile settings only apply when a session starts, and [experiment] ci(backend): stop ClickHouse logging every test query #129369 loaded them too late. A newClickHouse queries loggedstep prints query_log rows per database, anddefault_gw*should be empty.monitor-resources.py --phase NAMEto set the phase. The sampler takes a sample as soon as the phase changes, and the summary'scpu_s_by_phasesplits CPU by phase:setup,snuba-bootstrap,tests.Before/after CI numbers, normalized by runner CPU model, are in a comment below.
Open questions:
default, which no test uses (~13–16 CPU-s). Turning off the server-level system logs (metric_log,asynchronous_metric_log, ...) needs a ClickHouse restart and saves only ~2–3 CPU-s per shard.via @joshuarli.
--
View Session [Sentry]