Skip to content

Perf tests: stop background merges before the measured queries - #121937

Merged
fm4v merged 2 commits into
masterfrom
perf-stop-merges-before-queries
Sep 24, 2026
Merged

fm4v merged 2 commits into
masterfrom
perf-stop-merges-before-queries

Conversation

@fm4v

@fm4v fm4v commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Each server of a performance comparison creates and fills its own copy of the test tables. Background merges that run while the queries are measured change the part layout under them and compete with them for the CPU, and that happens differently on the two servers.

With the new --stop-merges flag, which compare.sh passes in CI, perf.py:

  • runs SYSTEM STOP MERGES on every server after the setup queries, then waits until system.merges is empty before the first measured query;
  • runs SYSTEM START MERGES after the teardown;
  • runs SYSTEM START MERGES at the start of every test, so a test that failed before its teardown doesn't leave merges stopped for the next test on the same servers.

Tests whose measured queries start with a write keyword (INSERT, ALTER, OPTIMIZE, SYSTEM, DELETE, ...) or run shell scripts keep merges running. keep_merges_running="1" on <test> opts a test out explicitly. Of the 673 tests, 614 stop merges and 59 keep them running. Local and non-admin runs without the flag issue no SYSTEM ... MERGES command.

This doesn't make the layout deterministic by itself: merges that run during the fill still depend on timing. Tests that fill from numbers_mt or rand*() need their own fixes. Examples are and_compare_chain_derived, join_convert_outer_to_inner and read_in_reverse_order_final, whose two servers read different amounts of data in 28 to 72% of PR runs.

Checked locally with two 26.8 servers and a throwaway test:

  • A read-only test gets the stop-merges stage, and its part count stays constant on both servers for the whole run.
  • A test with a measured INSERT keeps merging (6 → 3 parts during the run).
  • After the teardown, new parts of the stopped table merge again (3 active parts). A control table with merges stopped keeps 11.

Related: #120927

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Workflow [PR]
Sync PR [sync-upstream/pr/121937]

Version info

  • Merged into: 26.10.1.602-master (included in 26.10 and later)

Each server creates and fills its own copy of the test tables, and merges that run during the measured queries change the part layout under them and compete for the CPU, differently on the two servers. `perf.py` now issues `SYSTEM STOP MERGES` on every server after the setup queries and `SYSTEM START MERGES` after the teardown. It also runs `SYSTEM START MERGES` at the start of every test, so a test that failed before its teardown does not leave merges stopped for the next one.

Tests whose measured queries write (`INSERT`, `OPTIMIZE`, `ALTER`, `SYSTEM`) or run shell scripts keep merges running, and `keep_merges_running="1"` on `<test>` opts out explicitly. Of the 673 tests, 612 stop merges and 61 keep them running.
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1308b5c]

Summary: ✅


AI Review

Summary

This PR adds a --stop-merges mode to perf.py and enables it from compare.sh so read-only performance tests run against a stable part layout. The stop/drain/start flow itself looks sensible, but the read-only classification is computed before query selection, so the confirmation rerun can still leave merges enabled for read-only queries in mixed tests.

Findings

⚠️ Majors

  • [tests/performance/scripts/perf.py:678] stop_merges is decided from the whole XML file before --queries-to-run / --max-queries narrow the workload. confirm_changes reruns flagged queries with --queries-to-run, so current mixed tests such as native_arrow_ipc.xml (INSERT, then SELECT) and hashed_dictionary_load_factor.xml (SYSTEM, then WITH SELECT) will keep merges running even when the rerun executes only the read-only query. That leaves the confirmation path nondeterministic on exactly the cases this change is trying to stabilize. Suggested fix: finalize queries_to_run first, then compute stop_merges from only the selected query items, or keep a per-query needs_merges flag and reduce it over the selected indexes.
Final Verdict

Changes requested: the stop/start machinery itself is fine, but the selected-query rerun path needs to participate in the read-only classification before this achieves the intended stabilization in CI.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 24, 2026
Comment thread tests/performance/scripts/perf.py Resolved
Comment thread tests/performance/scripts/perf.py Resolved
Comment thread tests/performance/scripts/perf.py Resolved
…, detect writes

Addresses the review of #121937:
- `SYSTEM STOP MERGES` only cancels the running merges, so wait until `system.merges` is empty before the first measured query.
- All merge control is behind `perf.py --stop-merges`, which `compare.sh` passes in CI, so local and non-admin runs need no `SYSTEM MERGES` privilege.
- A test keeps merges running when a measured query starts with a write keyword; everything else counts as read-only, so queries like `sparseGrams(...)` are covered. 614 tests stop merges, 59 keep them running.
"CREATE", "DROP", "RENAME", "EXCHANGE", "ATTACH", "DETACH", "KILL",
"BACKUP", "RESTORE",
}
stop_merges = (

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.

stop_merges is decided from the whole XML file before --queries-to-run / --max-queries narrow the workload, so the confirmation rerun in compare.sh can miss the stabilization on current mixed tests. For example, native_arrow_ipc.xml has an INSERT benchmark followed by a read-only SELECT; rerunning only query 1 still leaves stop_merges false because query 0 is still present in test_queries. The same shape exists in hashed_dictionary_load_factor.xml and sparse_grams_index_build.xml. That means confirm_changes keeps background merges enabled for some read-only reruns, which defeats the determinism this PR is trying to add. Can we compute this from the selected query set after queries_to_run is finalized?

@clickhouse-gh clickhouse-gh Bot added the comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). label Sep 24, 2026
@fm4v
fm4v added this pull request to the merge queue Sep 24, 2026
Merged via the queue into master with commit 280b67e Sep 24, 2026
172 checks passed
@fm4v
fm4v deleted the perf-stop-merges-before-queries branch September 24, 2026 12:44
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 24, 2026
@snelheidai

Copy link
Copy Markdown

Two problems with --stop-merges on current master (0110158):

1. CI's measured runs don't get the flag. The TEST stage runs every test through CHServer.run_test in ci/jobs/performance_tests.py, which doesn't pass it. CI enters compare.sh at stage=get_profiles, which comes after run_tests, so the flag in run_tests is only used by the manual flow. In CI, only the confirm_changes rerun passes it. This PR's own arm_release 1/6 log shows this: none of the main-run perf.py commands has --stop-merges, and the only one that does is the confirmation rerun of top_k_through_join.

2. Wherever the flag is used, a read-only test whose drop query needs a mutation hangs in teardown. ALTER TABLE ... DROP INDEX (or DROP COLUMN) waits for its mutation while alter_sync > 0 (StorageMergeTree::alter). Mutations don't run while merges are stopped, and SYSTEM START MERGES only runs after the drop queries. The ALTER runs into max_execution_time = 300, and perf.py exits with code 1 before running the drop queries on the second (right) server, which keeps the test's index.

Four of the 614 tests that stop merges have such a drop query:

  • text_index.xml: ALTER TABLE hits_10m_single DROP INDEX idx_sp
  • text_index_lazy_apply.xml: ALTER TABLE hits_10m_single DROP INDEX idx_sp_lazy
  • text_index_phrase_blocked.xml: ALTER TABLE hits_10m_single DROP INDEX idx_sp_blocked
  • tpch-join_algorithm-partial_merge-fixed-str.xml: ALTER TABLE lineitem/orders/customer DROP COLUMN IF EXISTS ...

In CI today, this only affects confirmation reruns. A flagged query from these tests can't be confirmed: the rerun spends 300 s of the 1200 s confirmation budget, fails, and the test keeps its original verdict. A later rerun in the same job of another test that adds a text index on hits_10m_single.SearchPhrase then fails in setup with Column `SearchPhrase` must not have more than one text index. If the flag is added to CHServer.run_test, these four tests will fail on every run unless the teardown is fixed first.

Reproduced with perf.py from master b014c166, the configs from tests/performance/scripts/config, two servers running a release build of 7f7a3b04, and a 9.9M-row stand-in for hits_10m_single. On unmodified text_index.xml, all 35 queries ran with merges stopped, then the first server's DROP INDEX idx_sp failed after 300 s (Timeout exceeded: elapsed 300035 ms). Afterwards the second server still had idx_sp, and the first had a pending mutation. With SYSTEM START MERGES moved before the drop queries, the same run passes in 9 s: the queries are still measured with merges stopped, and each DROP INDEX takes 20 ms. The patch applies to current master:

--- a/tests/performance/scripts/perf.py
+++ b/tests/performance/scripts/perf.py
@@ -1273,6 +1273,11 @@
 
 reportStageEnd("run")
 
+# The drop queries may need merges: a mutating ALTER (e.g. DROP INDEX) waits for its mutation.
+if stop_merges:
+    for c in all_connections:
+        c.execute("SYSTEM START MERGES")
+
 # Run drop queries
 if not args.keep_created_tables and not args.use_existing_tables:
     drop_queries = substitute_parameters(drop_query_templates)
@@ -1289,7 +1294,3 @@
             print(f"drop\t{conn_index}\t{c.last_query.elapsed}\t{tsv_escape(q)}")
 
     reportStageEnd("drop-2")
-
-if stop_merges:
-    for c in all_connections:
-        c.execute("SYSTEM START MERGES")

PedroTadim pushed a commit to groeneai/ClickHouse that referenced this pull request Sep 25, 2026
The main runs of the performance job go through `CHServer.run_test`, which did not pass `--stop-merges`, so only the `confirm_changes` reruns stopped merges. And a drop query that creates a mutation, such as `ALTER TABLE ... DROP INDEX`, waited for it until `max_execution_time` because `SYSTEM START MERGES` ran only after the drop queries.

ClickHouse#121937 (comment)
Related: ClickHouse#121937
pull Bot pushed a commit to Lobinson1/ClickHouse that referenced this pull request Sep 26, 2026
Since ClickHouse#122073 the measured perf.py runs stop merges on both servers right
after the fill. The old fill writes 54 parts in about a second, so each
server keeps whatever part set its background merges reached, and the two
servers of one run measure different layouts (21 vs 38 parts on ClickHouse#122222,
24 vs 44 on ClickHouse#122154, 22 vs 44 on master 52a67f8). FINAL ... ORDER BY
x DESC LIMIT n reads the first reverse ranges of every part (about 23K rows
each), so its time grows with the part count, and the dashboard scores the
side with more parts as a confirmed regression.

The fill now merges the first insert, stops merges on the table and writes
the second insert as exactly one part, so both servers read the same two
parts whatever the merge timing, like final_read_in_order_limit.xml:

- OPTIMIZE ... FINAL waits for the running merges before it selects, so
  the background merges of the first insert cannot leave it partial, and
  optimize_throw_if_noop = 1 makes a no-op OPTIMIZE fail with its reason
  instead of surfacing as a wrong part count.
- min_insert_block_size_rows = 16777216 (2^24, above the 10M rows) with
  min_insert_block_size_bytes = 0: the INSERT squashes on one stream before
  it fans out to the sinks and never reaches the minimum, so the whole
  insert is flushed as one part, independent of max_insert_threads,
  max_block_size and how the numbers_mt threads interleave.
- The throwIf over system.parts fails the job unless exactly 2 parts are
  active, because SYSTEM STOP MERGES on a missing table is a silent no-op.

Validated on two servers of one master release build with perf.py
--stop-merges, 5 runs per side of the change: before, 14 to 19 parts,
different on the two servers in every run, right/left up to 1.18; after,
2 parts and identical SelectedRows on both servers, right/left within 3%.

Related: ClickHouse#122073
Related: ClickHouse#121937

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants