Repository navigation
Fix flaky 04257/04258 text-index ProfileEvents assertions under parallel replicas - #112263
PedroTadim merged 1 commit into
Conversation
04257_text_index_lazy_profile_events and 04258_text_index_lazy_intersect_heap assert that the TextIndexLazy* ProfileEvents fire, reading them from system.query_log. Under parallel replicas they intermittently see 0 for one or more events. The events do fire, but they are counted on a row the assertion did not select. PostingListCursor::~PostingListCursor increments six of the nine counters in the destructor, and the remaining three are incremented in buildPostingSegment and lazyIntersectPostingLists, so all of them are counted on whichever replica reads the granule. Remote ProfileEvents are not merged into the initiator's system.query_log row: the packet the initiator receives is pushed into the internal profile events queue, which is drained only for transmission to the client, while the query_log column comes from the thread group snapshot. The counters therefore land on secondary query_log rows, whose current_database is default rather than the test database, so the assertions' current_database = currentDatabase() predicate excluded them. Each test table is a single granule, so a tagged query's one mark is served by exactly one replica and columns fed by a single query drop individually. ProfileEvents are process-local counters and the initiator legitimately does not own counters incremented on a peer, so the engine behaviour is correct and the test predicate was wrong. Both assertions now resolve the initiator query ids by current_database and aggregate over every row of those queries via initial_query_id, the idiom already used by 04201_max_bytes_ratio_before_external_join_distributed, 02875_parallel_replicas_remote and 02950_parallel_replicas_used_count. All assertion expressions and the expected output are unchanged, so parallel-replica coverage of the lazy text-index read path is kept and no assertion is relaxed. 04258 is fixed alongside 04257 because it is the same carrier: the same lazy counters, the same predicate, no guard. The other text-index ProfileEvents tests already carry no-parallel-replicas, and 02346_text_index_parallel_replicas deliberately asserts parallel-replica behaviour, so neither is touched. Verified by forcing parallel_replicas_local_plan = 0 on a three-replica loopback cluster, which serves every mark remotely and makes the failure deterministic: both files fail before the change and pass after it, in that regime, with a local plan, and with parallel replicas disabled. Reverting only the outer WHERE on the fixed files reproduces the failure exactly. Both files then ran 50 times with randomized settings and 50 times without, all green. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (click to expand) - 7 findings across 2 rounds: 6 agreed and fixed, 1 disagreed with evidenceBefore opening this PR I reviewed it myself against the resulting code, then had a second model ❌ Disagreed, with evidence
Every premise is correct and I re-verified each one: neither file pins those settings; I still disagree, because the necessary step - that pinning makes the remote path deterministic and
Accepted in part: the observation that one green run is not evidence of the fix is worth stating, so
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-46:20260728-092200 |
|
cc @Ergus — could you review this? It fixes the flaky |
|
Workflow [PR], commit [741173a] Summary: ✅ AI ReviewSummaryThis PR rewrites the Final Verdict
|
CI finish ledger - 741173aCI has fully finished on this head: 174 check-runs, 0 queued, 0 in progress,
The two changed tests were exercised rather than skipped, which is the part worth stating for a The 127 skips are structural for a diff that touches only two Session id: cron:our-pr-ci-monitor:20260728-173000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
04257_text_index_lazy_profile_eventsand04258_text_index_lazy_intersect_heapassert that theTextIndexLazy*ProfileEvents fire, reading them fromsystem.query_log. Under parallel replicas theassertions intermittently see
0for one or more events, e.g.The events do fire; they are counted on a row the assertion does not select.
PostingListCursor::~PostingListCursorincrements six of the nine counters in the destructor(
src/Storages/MergeTree/MergeTreeIndexTextPostingListCursor.cpp:111-125), i.e. on whichever threaddestroys the cursor, which is the replica that read the granule; the other three are incremented at
:166and:1116/:1134, equally on the reading replica. Remote ProfileEvents are not merged into theinitiator's
query_logrow: the packet the initiator receives goes intoCurrentThread::getInternalProfileEventsQueue()(RemoteQueryExecutor.cpp:808-812), which is drained onlyfor transmission to the client (
ProfileEventsExt.cpp:194, reached only fromTCPHandler::sendSelectProfileEventsatTCPHandler.cpp:1838), while thequery_logcolumn comes from thethread-group snapshot (
ProcessList.cpp:892->executeQuery.cpp:719->QueryLog.cpp:265-273).So the counters land on secondary
query_logrows, whosecurrent_databaseis not the test database (theparallel-replica cluster entries declare no
default_database, so the secondary session keeps the serverdefault), and the assertions'
current_database = currentDatabase()predicate excludes them.04257's table is a single granule (2000 rows,index_granularity = 8192), so each tagged query has onemark, served by exactly one replica; four of its nine columns are fed by exactly one query each, so those
columns drop individually and the observed shapes differ between occurrences.
04258's table is larger(50000 rows, so at least seven granules), which is also why its exposure is only latent: with marks spread
over the replicas, the initiator usually gets some of them and the counters show up on its own row anyway.
The engine behaviour is correct - ProfileEvents are process-local counters and the initiator does not own
counters incremented on a peer. The test predicate is what is wrong, so that is what this PR changes: both
assertions resolve the initiator query ids by
current_databaseand then aggregate over every row of thosequeries via
initial_query_id, the same idiom already used by04201_max_bytes_ratio_before_external_join_distributed.sh:112-133,02875_parallel_replicas_remote.sql:13-16and02950_parallel_replicas_used_count.sql:22.This only widens the row set: an initiator row has
initial_query_id = query_id(
Context.cpp:3607-3608), so it is still selected, and the secondary rows of the same queries are selectedtoo. For
sum(...) > 0assertions that can turn a spurious0into the expected1and nothing else.All twelve assertion expressions, their aliases and the expected output are unchanged, so the "every event
must fire" contract is preserved verbatim and parallel-replica coverage of the lazy text-index read path is
kept. No tag, no setting pin, no
.referencechange: 2 files, +32/-4.Unlike the two SQL precedents above, the assertion queries here do not additionally pin
enable_parallel_replicas = 0/parallel_replicas_for_non_replicated_merge_tree = 0on themselves. Thatis not needed for correctness:
currentDatabase()is constant-folded on the initiator before the query isdistributed (
currentDatabase.cpp:22,49-52), and a query's own secondary rows are written after it hasfinished reading, so the CTE can neither resolve against the wrong database nor race its own rows.
04258is fixed together with04257because it is the same carrier - the same lazy counters (:122-124before this change), the same
current_databasepredicate, no guard; its failure mode is the same one, justless likely to be hit. Of the 20 text-index tests that read
ProfileEventsfromsystem.query_log, 17 already carryno-parallel-replicas; the only other one thatdoes not is
02346_text_index_parallel_replicas.sql, which deliberately asserts parallel-replica behaviourand already handles the secondary rows its own way, by matching them on the query text. None of those is
touched.
Verification
parallel_replicas_local_planis randomized 0/1 by the test runner (tests/clickhouse-test:1548). Forcingit to
0serves every mark remotely and makes the flake deterministic; the fix was measured in bothdirections on a 3-replica loopback cluster:
parallel_replicas_local_plan0 0 0 0 0 0 0 0 00 1 01 1 1 1 1 1 1 1 11 1 11 1 1 1 1 1 1 1 11 1 11 1 1 1 1 1 1 1 11 1 1With the
current_databasefilter removed, the event-bearing rows are visibly the secondary ones(
is_initial_query = 0,current_database = 'default'), and all 24 of them resolve to an initiatorquery_idthroughinitial_query_id- the premise the new predicate relies on. Reverting only the outerWHEREon the fixed files reproduces the failure exactly, which pins the predicate as the load-bearingchange. Both files then ran 50 times with randomized settings and 50 times without, 200/200 green.
Note the tests deliberately do not pin
parallel_replicas_local_planorautomatic_parallel_replicas_mode. Pinning them would make the assertion always take the remote path, butit would also fix the topology the test runs under, and the point of leaving them randomized is that the
assertion must hold under every topology the lane can produce - which is what the four rows above show. The
consequence is that any single run may or may not exercise the remote path, so the fix is not observable
from one green run; the deterministic evidence is the forced-remote arm.
04258's negative assertion (BruteForceIntersections = 0) is the one place a wider row set could inprinciple turn a pass into a failure. It cannot here: all three of its tagged queries set
text_index_lazy_intersection_density_threshold = 1.0, which forces leapfrog on whichever replica runsthem, so no replica increments the brute-force counter for those query ids. It stayed green in every arm
above.
Correction to my earlier comment on #111918
In #111918 (comment) I proposed pinning the
tests with
SET enable_parallel_replicas = 0. That would remove parallel-replica coverage of this readpath for no reason, so this PR takes the different route above and selects the rows that own the events
instead.
That comment also mis-attributed the breadth, and my first attempt at correcting it was itself too narrow
because it only looked back 30 days. Over 60 days,
04257shows this exact signature on three unrelatedPRs, all on lanes whose name contains
ParallelReplicas(only those lanes getconfig.d/enable_parallel_replicas.xml, via--parallel-repattests/config/install.sh:484-485):#112008 (columns 8 and 9), #111918 (column 6) and #97261 (column 6, 2026-06-11, outside the window I
originally queried). A fourth row, the all-nine-zeros one, is on an
amd_asan_ubsan, distributed planlane, which does not enable parallel replicas; it is collateral from a host-memory-pressure job (131
failures in that job, 120 of them
memory limit exceeded) and its own recorded settings showparallel_replicas_local_plan 1. The other04257rows in that window are unrelated classes:Code: 241host memory pressure, and one
Code: 36settings error. For04258the recorded failures are host memorypressure plus a cluster of self-caused ones on the branch that introduced the test, so its exposure to this
mechanism is latent rather than observed. This fix therefore addresses the parallel-replica shapes; it is
not expected to remove reds caused by host memory pressure.
A tracking issue was also requested. I do not open issues on my own initiative, so that offer still stands
on the #111918 thread.
Version info
26.8.1.431(included in26.8and later)