Skip to content

ci(tests): Run the Python tests in parallel with pytest-xdist - #8764

Merged
joshuarli merged 9 commits into
masterfrom
ci/pytest-xdist
Dec 1, 2026
Merged

joshuarli merged 9 commits into
masterfrom
ci/pytest-xdist

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Runs snuba's Python test jobs in parallel with pytest-xdist (-n 4 --dist loadfile) to cut CI time. It builds on #8763, which made the per-test ClickHouse fixture cheaper.

Per-worker isolation

tests/xdist_isolation.py, a pytest plugin loaded from addopts, sets per-worker environment variables before snuba.settings is imported, so settings and subprocesses pick them up:

  • ClickHouse: each worker gwN gets its own database, <base>_gwN. Table names, the Distributed engine's target, and replicated tables' ZooKeeper paths all include the database name.
  • Redis: redis_db flushes every client and a Redis Cluster only has DB 0, so each worker gets its own three-node Valkey cluster on ports 7000+10N to 7002+10N (scripts/redis-test-clusters.sh). Outside cluster mode, each worker gets its own DB.
  • Kafka stays shared. Topics are created idempotently. Tests in different files that read and write the same topic need their own topic names: test_combined_scheduler_and_executor now publishes to its own results topic, because test_executor_consumer expects the first message on events-subscription-results to be its own.

Migration, multi-node, and initialization runs stay serial: they leave SNUBA_TEST_WORKERS unset, which means -n 0, and the plugin does nothing without xdist. XDIST_WORKERS in ci.yml sets the worker count to match the runners' 4 vCPUs.

Order-dependent tests

Some tests relied on state that other tests left behind, which xdist exposes:

  • _restore_tables() puts back the tables and migration status rows that custom_clickhouse_db drops. custom_clickhouse_db depends on create_databases, because _restore_tables() queries the worker's database before anything else would create it.
  • tests.helpers.restore_module replaces importlib.reload teardowns of snuba.clusters.cluster, which left stale exception classes behind.
  • A few tests stop relying on another test's topics, cached partition counts, or table rows, on the default database, or on time.time() in a parameter. ShardedExecutor's test waits for its thread pool.

sentry job shards

With API changes, the sentry job runs 4 shards instead of 16. Every shard collects Sentry's whole suite to find its -m share, so most of the job's runner time was repeated collection. Without API changes it stays at 2 shards.

Open question

Rarely, a test under tests/web/rpc gets no rows back right after inserting them: ClickHouse reads 0 rows from a part that holds the inserted rows, so every granule was skipped before reading. The cause isn't known yet. To help find it, tests/web/rpc/conftest.py adds the worker database's recent ClickHouse query_log, part_log, and active parts to a failed test's report.

via @joshuarli.

--

View Session [Sentry]

@sentry-junior

sentry-junior Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

CI timings: baseline run 46518637735 (master test code, serial) vs. this PR, run 46556781944 (-n 4).

Job pytest before pytest after job before job after
test 658s 284s 764s 388s
test_distributed shard 0/2 699s 362s 774s 444s
test_distributed shard 1/2 957s 399s 1047s 485s
CH 25.8.16 snuba-test 670s 287s 753s 387s
CH 25.8.33 snuba-test 632s 265s 709s 348s
CH 26.3.33 snuba-test 732s 313s 815s 414s
CH 26.8.36 snuba-test 763s 309s 855s 403s
test_migrations_distributed (serial) 495s 499s 561s 569s
test_migrations (serial) 112s 122s 185s 221s
Whole ci run before after
Wall clock 18m30s 13m35s
Runner time, all jobs 156.8 min 115.4 min
Runner time, Tests… jobs 131.7 min 85.8 min

Test counts match: test and each clickhouse-versions snuba-test job have 3725 passed, 6 skipped, 6 xfailed, 7 xpassed before and after. The test_distributed shards have 1951 passed, 4 skipped, 5 xfailed, 2 xpassed, and 1774 passed, 2 skipped, 1 xfailed, 5 xpassed. The migration jobs have 408 passed.

Notes:

  • The slowest job used to be test_distributed shard 1. Now it's sentry (0), followed by the serial test_migrations_distributed. The test jobs also started about 100s later in the after run, because Build snuba CI image took 155s instead of 52s (this PR changes uv.lock), so without the rebuild the wall clock gain is about that much bigger.
  • Worker count, from the test job in run 46541551818: 3 workers 310s, 4 workers 263s, 6 workers 288s.
  • test_distributed as a single shard with 4 workers took 540s of pytest (674s job) in the same run, against two shards at 363s and 378s, so it keeps 2 shards. One shard would save about 4.5 runner-minutes, but it would become the slowest job.
  • Sentry's CI ClickHouse profile (query profilers off, lower log level, with log_queries left on because admin tests read system.query_log) made no consistent difference. Compare run 46544041985 without it and run 46546743654 with it: test went 278s to 255s, but CH 26.3 snuba-test went 298s to 374s. It's not in this PR.

@sentry-junior
sentry-junior Bot changed the base branch from master to test/cheaper-clickhouse-db-fixture December 1, 2026 02:19
@sentry-junior

sentry-junior Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Before/after CI timings

One run each. Cells are pytest seconds / job seconds.

Job Before After pytest change
test 455 / 564 202 / 308 -56%
test_distributed shard 0 308 / 392 240 / 356 -22%
test_distributed shard 1 375 / 446 265 / 356 -29%
CH 25.8.16 snuba-test 429 / 512 197 / 311 -54%
CH 25.8.33 snuba-test 462 / 558 192 / 282 -58%
CH 26.3 snuba-test 527 / 661 229 / 387 -57%
CH 26.8 snuba-test 549 / 641 270 / 386 -51%
test_migrations (serial) 100 / 176 115 / 190 same
test_migrations_distributed (serial) 491 / 594 499 / 591 same
Whole run Before After
Runner minutes, Tests* jobs 101.9 77.0
Last xdist job done (s after run start) 833 555
Last Python test job done (s after run start) 833 760
ci wall clock (s) 834 1071

The wall clock comes from jobs this PR doesn't change. In the after run, sentry (0) spent 260s downloading the CI image artifact (30s before) and finished last; its pytest step was about the same (469s vs 448s). Without that delay, the run ends when sentry (N) and the serial test_migrations_distributed (760s) finish, so those two now set the floor.

Test counts match. test and each snuba-test job: 3738 passed, 6 skipped, 6 xfailed, 7 xpassed in both runs. test_distributed shards: 1951 + 1787. Migration jobs: 408.

Base automatically changed from test/cheaper-clickhouse-db-fixture to master December 1, 2026 18:00
sentry-junior Bot and others added 4 commits December 1, 2026 18:07
Give each pytest-xdist worker its own ClickHouse database and Redis, run the
test, test_distributed, and clickhouse-versions snuba-test jobs with four
workers, and fix the tests that depended on file order or timing.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
ClickHouse creates system.query_log on its first log flush, a few seconds
after it starts. Under xdist the admin tests can run before that, and then
the queries fail with UNKNOWN_TABLE.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
Some RPC tests occasionally get an empty result right after inserting
their rows when they run under pytest-xdist. When one fails, add the
worker database's recent query_log and part_log rows and its active parts
to the failure report, so the next failure shows whether the rows were
there at query time and which database the query read.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
The first captured failure read zero rows from a part that held the
inserted rows, and the query text was cut off before its WHERE clause.
Show the WHERE clause, the selected marks, and query condition cache hits
so the next failure shows which filter dropped the rows.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
@sentry-junior
sentry-junior Bot marked this pull request as ready for review December 1, 2026 18:45
@sentry-junior
sentry-junior Bot requested review from a team as code owners December 1, 2026 18:45
@sentry-junior
sentry-junior Bot requested a review from joshuarli December 1, 2026 18:45
@joshuarli
joshuarli marked this pull request as draft December 1, 2026 19:05
@sentry-junior sentry-junior Bot deleted a comment from sentry-warden Bot Dec 1, 2026
@joshuarli
joshuarli marked this pull request as ready for review December 1, 2026 19:19
sentry-junior Bot and others added 3 commits December 1, 2026 19:34
… topic

test_executor_consumer asserts that the first message on
events-subscription-results is its own result. Under xdist the two files
can run at the same time on different workers, so the combined test's
result could arrive first.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
…b runs

_restore_tables() queries the worker's database before _drop_tables()
would create it, so a worker whose first test used this fixture failed.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
Every shard collects sentry's whole suite to find its -m share, so most
of the job's runner time is repeated collection.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
Comment thread tests/admin/test_api.py Resolved
Comment thread tests/conftest.py Resolved
Review feedback: the implicitly concatenated fragments were hard to read.
@joshuarli
joshuarli merged commit 9c21cc1 into master Dec 1, 2026
61 checks passed
@joshuarli
joshuarli deleted the ci/pytest-xdist branch December 1, 2026 21:56
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.

2 participants