Skip to content

ci(backend): Copy per-worker Snuba databases instead of bootstrapping each - #131172

Merged
joshuarli merged 1 commit into
masterfrom
ci/clickhouse-cpu-bootstrap-clone
Nov 18, 2026
Merged

joshuarli merged 1 commit into
masterfrom
ci/clickhouse-cpu-bootstrap-clone

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Most of ClickHouse's CPU on a backend shard goes to setup. Phase 1 of bootstrap-snuba.py runs snuba bootstrap --force five times at once: snuba-snuba-1's default, plus default_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.

  • One bootstrap, then copies. Phase 1 bootstraps default_gw0, then runs BACKUP DATABASE ... TO Memory and RESTORE ... AS default_gwN for the other workers. RESTORE rewrites the database in materialized views and in the Merge table. The copies match a bootstrapped database (SHOW CREATE for every table plus migrations_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.
  • CI ClickHouse config, loaded before any per-worker Snuba connects. A users.d profile turns off log_queries, processor profiles, and the query and memory profilers, and the logger level drops from trace to warning. 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 new ClickHouse queries logged step prints query_log rows per database, and default_gw* should be empty.
  • Phases in the resource monitor. Run monitor-resources.py --phase NAME to set the phase. The sampler takes a sample as soon as the phase changes, and the summary's cpu_s_by_phase splits CPU by phase: setup, snuba-bootstrap, tests.

Before/after CI numbers, normalized by runner CPU model, are in a comment below.

Open questions:

  • Is it OK for per-worker databases to come from RESTORE rather than migrations? The fallback covers a failed copy, not a schema that silently differs.
  • Follow-ups not in this PR: snuba-snuba-1 still bootstraps 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]

… 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]>
@sentry-junior sentry-junior Bot added the Trigger: Override Selective Testing Run the full test suite; necessary in cases where selected tests are flaky due to reshuffling label Nov 14, 2026
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 14, 2026
@sentry-junior

sentry-junior Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Results from run 43539870699 (all 30 shards passed, full suite with Trigger: Override Selective Testing).

PR backend shards run on Arm, and every shard here and in the baselines got the same CPU (arm implementer=0x41 part=0xd49), so no CPU-model normalization was needed. The baselines are four concurrent full-suite Arm PR runs on master's bootstrap: 43539157894, 43537880436, 43536092756, 43530563379. Master's x64 runs aren't comparable because they use different runners. For each shard index, I took the median of the four baselines and compared it with the same shard here. Setup and test are split at the Run backend test step's start time.

per shard (median) baseline this PR paired diff
job wall 261.8s 245.5s −14.8s (−6%)
setup wall (job start → tests) 93.8s 78.0s −15.5s (−17%)
test step wall 157.8s 155.0s −1.2s (noise)
ClickHouse CPU-s, setup 81.3 29.8 −52.1 (−64%)
ClickHouse CPU-s, tests 63.3 48.8 −13.1 (−21%)
ClickHouse CPU-s, total 145.6 78.4 −64.5 (−44%)

Across the 30 shards, that's about 8 runner-minutes and 32 ClickHouse CPU-minutes saved per full run.

  • Phase split (new cpu_s_by_phase, median per shard): snuba-bootstrap lasts 20.1s and uses 27.6 ClickHouse CPU-s. That covers the gw0 bootstrap plus snuba-snuba-1's own default bootstrap running at the same time. The rest of setup uses 2.3 and tests uses 49.0. Before this PR, phase 1 was a ~37s window with ClickHouse at ~2.2 cores.
  • Profile took effect: all 30 shards logged 0 query_log rows for default_gw0..3. Before, it was ~23k rows per shard, and [experiment] ci(backend): stop ClickHouse logging every test query #129369's reload didn't change that. default logged 1–193 rows from snuba-snuba-1 sessions opened before the reload. That's expected, since phase 2 stops snuba-snuba-1. The test-step drop of 13 CPU-s comes from this profile and the logger level.
  • Copy: all 30 shards copied default_gw0 to gw1..3, and none fell back to bootstrapping each worker.

Open follow-ups, not in this PR:

  • snuba-snuba-1's default bootstrap (~13–15 ClickHouse CPU-s, unused by tests) is now about half of the snuba-bootstrap phase.
  • Turning off server-level system logs needs a restart. It saves ~2–3 CPU-s per shard (measured locally).

@linear-code

linear-code Bot commented Nov 14, 2026

Copy link
Copy Markdown

DI-2852

@joshuarli
joshuarli marked this pull request as ready for review November 18, 2026 18:16
@joshuarli
joshuarli requested a review from a team as a code owner November 18, 2026 18:16
@sentry-junior
sentry-junior Bot requested a review from joshuarli November 18, 2026 18:16
@joshuarli
joshuarli merged commit cb4f5ac into master Nov 18, 2026
104 of 118 checks passed
@joshuarli
joshuarli deleted the ci/clickhouse-cpu-bootstrap-clone branch November 18, 2026 18:17

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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:])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 62db1d5. Configure here.

joshuarli added a commit that referenced this pull request Nov 18, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: Override Selective Testing Run the full test suite; necessary in cases where selected tests are flaky due to reshuffling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant