Repository navigation
Wait for replication before asserting a row count in test_rename_distributed - #114274
Conversation
…ributed test_rename_distributed asserted an exact row count immediately after inserting into a Distributed table, with no barrier of any kind. The only thing between the INSERT and the assertion was a 30 second wall-clock poll inside the select() helper, so the test bet on convergence instead of waiting for it. Two data-movement gaps sit in that window and neither was awaited: 1. The Distributed INSERT is background-spooled. insert_sync at StorageDistributed.cpp:1143 is false here (distributed_foreground_insert defaults false, insert_shard_id is unset, the table is persistent), so DistributedSink::consume takes writeAsync -> writeToShard, which spools the block to disk, and onFinish waits on the job pool only when insert_sync is set. The client's INSERT therefore returns while a block is still on the initiator's disk. 2. internal_replication is true, so an INSERT lands on one replica per shard and the peer replica must FETCH every part through the ReplicatedMergeTree queue. PARTITION BY num % 100 over 1000 rows makes that ~100 tiny parts per INSERT per shard, and because load_balancing defaults to RANDOM the SELECT can be served by the replica that is still fetching. Measured at the moment of the failing read: system.distribution_queue on the initiator held 2 un-flushed files while all four system.replication_queue tables were empty. A bare SYSTEM SYNC REPLICA would have returned immediately and vacuously, so the spool has to drain first. The barrier is also required after the FIRST insert, not only the last. The spool replays stored query text that names the columns (createInsertToRemoteTableQuery at DistributedSink.cpp:167, written into the file header at :949), and the test renames num2 -> foo2 between its two inserts. Holding the spool across the rename reproduces a permanent failure of that file: Code: 16. No such column num2 in table default.test_rename_distributed_replicated ... While sending .../shard2_all_replicas/1.bin (NO_SUCH_COLUMN_IN_TABLE) poll=30 is left unchanged. Raising it would only lengthen the same bet, and removing it would be a gratuitous change to a line the barrier already makes deterministic. Validated with the same binary in both arms and poll patched to 0, so the barrier rather than the poll is what is being tested: the unmodified test fails 2/2 with assert '1473\n' == '1998\n', and passes 3/3 with the barrier. Then 50/50 green with poll=30 restored, and all 10 tests in the file green. The sibling test_rename_distributed_parallel_insert_and_select also reads a Distributed table, but its select() calls pass no expected_result and select() asserts only when one is given, so it is not a carrier of this invariant and is left untouched. The other eight tests in the file read plain ReplicatedMergeTree per node, with no Distributed spool involved.
Internal second-model reviewTwo findings from an independent cold review plus a second-model pass. ❌ 1 - wrong mechanism in a comment (fixed). The helper comment and the commit message
I kept it for two reasons. Pairing a barrier with a retry is ordinary here: 52 of the 108 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260811-024900 |
|
cc @devcrafter, could you review this? It is test-only: test_rename_distributed asserted an exact row count with no barrier, so it raced the Distributed spool drain plus the writer to replica part fetch against a 30 second poll, and the fix adds SYSTEM FLUSH DISTRIBUTED then SYSTEM SYNC REPLICA after each insert. |
|
Workflow [PR], commit [af431ec] Summary: ✅ AI ReviewSummaryThis PR makes Findings
Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline. |
CI finish ledger - 3b1be38CI is fully finished on this head and there are no failures to own.
175 leaf jobs: 11 OK, 164 skipped, 0 failed, 0 dropped. The high skip count is expected for a Session id: cron:our-pr-ci-monitor:20260811-113000 |
…e-distributed-replication-barrier
| try: | ||
| create_distributed_table(node1, table_name) | ||
| insert(node1, table_name, 1000) | ||
| sync_distributed_table(node1, nodes, table_name) |
There was a problem hiding this comment.
Flushing here changes the test contract from “a successful async INSERT survives a later RENAME COLUMN” to “rename only after the Distributed queue is empty”. That narrows the only coverage in this file for a pre-rename block still sitting in the queue: DistributedSink persists the original column list into the queued insert header, so the old path is exactly what would catch a block accepted as (num, num2) later replaying into foo2 and failing with NO_SUCH_COLUMN_IN_TABLE. If the flaky part is only the final exact-count assertion, I think the barrier belongs before the final select, and the pre-rename queued-insert case should stay covered here (or move into a dedicated regression test/product fix) instead of being serialized away.
There was a problem hiding this comment.
The mechanism is right, and I measured it before shipping: DistributedSink stores the query
text with the original column list, so a block from the first INSERT that outlives the rename
replays (num, num2) and fails with NO_SUCH_COLUMN_IN_TABLE. Holding the spool across the
rename with SYSTEM STOP DISTRIBUTED SENDS reproduces it exactly (Code: 16 ... While sending .../shard2_all_replicas/1.bin, thrown at InterpreterInsertQuery.cpp:292).
What I disagree with is that the old path covered it. That replay runs on the directory queue's
background pool: the exception is caught at DistributedAsyncInsertDirectoryQueue.cpp:253 and
only recorded in status.last_exception (:439), and NO_SUCH_COLUMN_IN_TABLE is not in
isDistributedSendBroken (DistributedAsyncInsertHelpers.cpp:39), so the file is retried with
backoff rather than quarantined. The test reads neither system.distribution_queue nor
last_exception (zero occurrences before this change), its only assertion is the row count at
test.py:227, and the finally runs DROP TABLE ... SYNC, which deletes the pending blocks
(StorageDistributed.cpp:1596). So that case could only ever surface as a short count,
indistinguishable from the replication lag this PR is about.
Moving to a single barrier just before the final select makes it worse rather than better.
SYSTEM FLUSH DISTRIBUTED is synchronous and rethrows the first error
(StorageDistributed.cpp:2023), so with the first INSERT still spooled at that point the test
would hard-fail with Code: 16 precisely when the race lands. On a natural run with only the
first barrier removed I measured the spool surviving both renames (data_files=1), so that
window is live rather than theoretical. That trades a silent flake for a loud one.
On a dedicated test for the queued-insert-across-rename case: I am not raising one. The
user-visible report, #74723, was closed without a code change, and I have no evidence a
maintainer wants the case pinned. If a reviewer does want it, SYSTEM STOP DISTRIBUTED SENDS is
the deterministic lever (01790_dist_INSERT_block_structure_mismatch_types_and_names.sql uses
the same pattern for a structure mismatch), and it belongs in its own test rather than inside a
row-count assertion.
CI finish ledger - af431ecEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task CI is fully finished on this head and there are no failures.
Gates: 0 incomplete checks; This head is @ PedroTadim's merge of master into the branch (2026-08-13T08:14:14Z); my own commit Session id: cron:our-pr-ci-monitor:20260813-130000 |
|
Backporting to fix CI |
Backport #114274 to 26.3: Wait for replication before asserting a row count in test_rename_distributed
Backport #114274 to 26.6: Wait for replication before asserting a row count in test_rename_distributed
Backport #114274 to 26.7: Wait for replication before asserting a row count in test_rename_distributed
…ting a row count in test_rename_distributed
…ting a row count in test_rename_distributed
…ting a row count in test_rename_distributed
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
test_rename_column/test.py::test_rename_distributedasserts an exact row count right afterinserting into a
Distributedtable, with no barrier - only a 30 second poll inselect().Two things must finish first, and neither is awaited:
insert_syncatStorageDistributed.cpp:1143is false here(
distributed_foreground_insertdefaults false, table is persistent), soDistributedSinktakeswriteAsyncand the INSERT returns while a block is still on the initiator's disk.internal_replicationis true, so a shard's peer replica must FETCH every part (~100 tiny partsper INSERT per shard given
PARTITION BY num % 100), andload_balancingdefaults toRANDOM,so a shard leg can be served by a replica still fetching.
Found by inspection plus a local repro; public CI has not lost this race yet. Forcing the assert
onto its first read (
poll=0) fails reproducibly withassert '1473\n' == '1998\n'. There,system.distribution_queueheld 2 un-flushed files while all foursystem.replication_queueswere empty, so a bare
SYSTEM SYNC REPLICAreturns vacuously. The spool drains first.The fix adds a helper running
SYSTEM FLUSH DISTRIBUTEDthenSYSTEM SYNC REPLICAon all fournodes, after each insert. The first insert needs it too: the spool replays stored query text naming
the columns, so a block outliving the
num2 -> foo2rename replaysINSERT ... (num, num2)andfails permanently with
NO_SUCH_COLUMN_IN_TABLE, reproduced separately.poll=30is unchanged;raising it only lengthens the bet.
Validated with the same
poll=0probe in both arms on one binary, so the barrier does the work andnot the poll: 2/2 fail without it, 3/3 pass with it. Then 50/50 green with
poll=30restored, wholefile green.
The sibling
test_rename_distributed_parallel_insert_and_selectalso reads aDistributedtable,but its
select()calls pass noexpected_result, so they assert nothing about counts and are leftalone. The other eight tests read plain
ReplicatedMergeTree.Version info
26.7.11.8,26.6.9.6,26.3.33.97