Repository navigation
Conversation
…ing a read
With parallel_replicas_plan_based = 1, a query over a system log table or a
SQL SECURITY DEFINER view failed on the initiator with
LOGICAL_ERROR: Next task callback is not set for query with id: <uuid>
applyParallelReplicas marks the coordinated read with
enableParallelReadingFromReplicasForSerialization, which deliberately attaches
no callbacks: the step is only serialized and shipped to replicas, where
deserialize rebuilds it and resolves the callbacks from the context.
createParallelReplicasPlan then clones that fragment once more to build the
initiator's local arm, and ReadFromMergeTree::clone() passed the
marked-but-callback-less state back through the constructor. The constructor
cannot distinguish "marked for serialization" from "replica side, resolve from
the context", so it took its context fallback, and an initiator holds no
coordinator callback.
Ordinary plan-based queries survive that fallback only because Context's copy
constructor carries the callbacks TCPHandler installed. Instrumenting the
constructor showed the fallback is taken on the ordinary path too, and merely
happens to find a callback there. The failing shapes read under a context built
fresh from the global context, which never holds them: a system log storage, or
a DEFINER view.
clone() now copies the flag and both callback optionals directly, so a copy no
longer re-derives state from the context. All three fields move together:
restoring only the flag would drop the callbacks of a marked read that has them,
which getParallelReadingExtension asserts on. The constructor's context fallback
is unchanged, since deserialize, StorageMergeTree::read and
StorageReplicatedMergeTree::readLocalImpl rely on it on a follower.
Both branches of that fallback were reachable with the same state: with the
first one made non-throwing, the same query threw the sibling message from
Context::getMergeTreeReadTaskCallback, so copying both optionals is required.
Still reproduced with both in-flight Merge route fixes applied (ClickHouse#112849,
ClickHouse#113788), so this is a separate defect rather than one of those routes.
Related: ClickHouse#111677
Review round 1 raised two majors about the test and the public claims, plus one comment-budget nit. The C++ fix itself is unchanged. Engagement oracle for the affected shape. The three affected-shape queries asserted results only, so a regression making plan-based parallel replicas silently decline for the system log shape would have left every oracle green (count() > 0 is still 1 when the read is served locally). The affected query now carries a unique log_comment and is followed by an assertion that ParallelReplicasQueryCount > 0 for its own initiator row, following the pattern of 02950 and 02875. The assertion was proven mutation-sensitive: it reddens 3/3 under each of enable_parallel_replicas = 0, parallel_replicas_for_non_replicated_merge_tree = 0, max_parallel_replicas = 1 and automatic_parallel_replicas_mode = 2. Note it witnesses that parallel replicas engaged, not that the plan-based path specifically engaged. The counter is incremented in ParallelReplicasReadingCoordinator's destructor, which also runs for ordinary task-based parallel replicas, and the tree exposes no plan-based-specific observable: there is no such ProfileEvent, and EXPLAIN prints an identical plan for this shape whether plan-based is on or off, because the split happens below the printed plan. A stricter oracle keyed on the replicas shipping a serialized plan fragment was measured and rejected: it discriminated correctly by hand but failed 4 of 20 runs under the test runner, because the number of participating replicas and the log table's part layout vary per run. Dropped the SQL SECURITY DEFINER view arm and its public claim. The arm never reddened. StorageInMemoryMetadata::getSQLSecurityOverriddenContext starts the DEFINER branch from the global context but then propagates both parallel-replicas callbacks, gated on canUseTaskBasedParallelReplicas() and hasMergeTreeAllRangesCallback(), both of which hold on an initiator under this test's settings, so the pre-fix constructor fallback succeeds there. That is the asymmetry with ReadFromUserQueryLog::initializePipeline, which propagates the progress callback, the ProcessList element and the normalized query hash but not the callbacks. Confirmed on the pristine-master binary with the view query run in isolation, including with parallel_replicas_allow_view_over_mergetree = 1: it passes in every arm. The changelog entry and description no longer name it as a carrier, and the no-replicated-database tag went with the view, which was the only access-entity object in the file. Trimmed the comment block to the local invariant. The clause about the initiator context was motivation rather than something the next reader needs at that spot, so it lives here instead. Revalidated: the amended test still aborts a pristine-master server with the exact message and the expected frames (clone present, deserialize absent), and passes 50/50 randomized plus 50/50 with plan-based and local-plan forced.
The engagement assertion added in the previous round checks ParallelReplicasQueryCount, which is incremented in the reading coordinator's destructor. The coordinator is constructed in createParallelReplicasPlan before the arm choice, and the remote-only branch reuses it, so that counter fires on both arms. It therefore proves a coordinator existed, not that the initiator-local fragment was built, and it is the local fragment that reaches the clone this change repairs. The snapshot replica is pinned only on the local arm (setSnapshotReplicaNum has exactly two callers, both local-arm-only), so its DEBUG line in system.text_log, scoped by query_id to the affected query, witnesses that arm specifically. A regression routing this shape to the remote-only arm would stop exercising the clone while results and the existing counter stayed green. Measured: query_id attribution is exact (one pin line in the window, zero unattributed, sharing the initiator's own query_id). The assertion reddens 3/3 under each of parallel_replicas_local_plan = 0 and parallel_replicas_prefer_local_replica = 0 applied to the affected query alone, while ParallelReplicasQueryCount stays green in both, and it is stable over 50 randomized and 50 forced runs.
Internal second-model review (click to expand)Three review rounds by a second model, independent of the author, plus my own cold read of the
Two further notes I checked in source and did not raise: the assertion needs no Stated plainly rather than left implicit: the new assertion is never reached on master, because the Session id: cron:clickhouse-review-slot-11:20260809-082700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260809-050700 |
|
cc @KochetovNicolai @vdimir, could you review this? With |
|
Workflow [PR], commit [cfff54b] Summary: ✅
AI ReviewSummaryThis PR fixes the plan-based parallel-replicas clone path so a Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit cfff54b.
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
CI finish ledger — ffcc3e1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260809-220000 |
Refresh the branch against master d466df7 so it is ready to review. The two CI failures the finish ledger owned have since been fixed on master (ClickHouse#108855 and ClickHouse#112484), which is what was blocking Mergeable Check. No conflict: the clone() triple copy still applies verbatim and master's newer restores in the same function are preserved.
Comment-only: stripping comment lines from both revisions gives byte-identical SQL. The two comments keep their substance (why parallel_replicas_local_plan is required for the second clone, and why the log flush is required for the read to reach it) and say it plainly.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 42 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - bc194a3Every failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link
No failure here is caused by this PR's diff: the two bugfix arms and the Session id: cron:our-pr-ci-monitor:20260901-203000 |
The constructor read `enable_parallel_reading` as "this read must have coordinator callbacks" and resolved from the query context any callback it was not given. That conflates two callers: one that has no callbacks and wants them resolved (a read a replica rebuilds from a serialized plan, and a follower read), and one whose callbacks are genuinely absent because the read is executed on the replicas rather than here. `enableParallelReadingFromReplicasForSerialization` marks a read of the second kind, so cloning such a read asked the context for a callback an initiator does not hold and got `Next task callback is not set for query with id`. The constructor now stores exactly the state it is given, and the resolution moved to `MergeTreeDataSelectExecutor::readFromParts`, which is the single seam both context-resolving callers pass through and where "no extension was passed" really does mean "take them from the context". `clone` is back to passing its own three fields, so the assignments that followed the constructor call are gone. The test also asserts that no query in its database ended in an exception. A replica-side arm of a coordinated read runs under its own query id and can fail without changing the rows the initiator returns, so the result comparisons alone did not cover the `readFromParts` half of this change.
…eptions Two gaps in the previous revision of this test, both about the fallback that moved into `MergeTreeDataSelectExecutor::readFromParts`. The non-plan-based control kept `parallel_replicas_local_plan = 1`, so the initiator had a local arm that could produce the whole result while the unused remote sources were cancelled: the row counts could match without any follower having resolved a callback. Setting `parallel_replicas_local_plan = 0` removes the local arm, so the result can only come from followers. The callback lookup happens while the plan is built, before the query is logged as started, so a missing one is recorded as `ExceptionBeforeStart`, which the oracle did not count. Measured over 30 runs each: unchanged 30/30 green with the fix; reverting only the `readFromParts` hunk now fails on the first runs instead of 23 of 30.
…path
`SELECT count() > 0 FROM merge('system', '^user_query_log$')` was in the test as a second
carrier shape, but a `Merge` table never gets a parallel-replicas child plan:
`getChildPlanOptimizationSettings` clears `enable_parallel_replicas` in the child
optimization settings and `ReadFromMerge::createPlanForTable` clears it in the child
context as well. Neither is conditional, so no setting turns it back on.
Measured on this tree with the two oracles the test already uses, scoped per query by
`log_comment`. The direct `system.user_query_log` read reports
`ParallelReplicasQueryCount = 1` and one `ParallelReplicasReadingCoordinator` snapshot-replica
line; an ordinary MergeTree read reports the same; the `merge()`-wrapped read reports 0 and
has no coordinator line at all. On a binary with the constructor fallback restored, the
`merge()` query alone passes with no throw, while the direct read alone aborts the server
with `Next task callback is not set for query with id` and the recorded frames.
So the line could not fail whatever this change did to the constructor, and the claim that
the test covered two carrier shapes was wrong. The direct read is the shape that reproduces.
Unchanged 20/20 green with the fix, and still aborts the server with the fallback restored.
Backport #114022 to 26.8: Do not resolve parallel-replicas callbacks from the context when cloning a read
…callbacks from the context when cloning a read
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR: Next task callback is not set for query with idexception whenparallel_replicas_plan_basedis enabled and the query reads a system log table.Description
Related: #111677, #116087
With
parallel_replicas_plan_based = 1, a query over a system log table fails on the initiator:An exception, handled normally in a release build, but it aborts debug and sanitizer builds.
Root cause.
applyParallelReplicasmarks the coordinated read withenableParallelReadingFromReplicasForSerialization, which deliberately attaches no callbacks: the step isonly serialized and shipped to replicas, where
deserializeresolves them from the context.createParallelReplicasPlanthen clones that fragment once more for the initiator's local arm, and theconstructor cannot tell "marked for serialization" from "replica side, resolve from the context", so it took
its context fallback, which on an initiator has no coordinator callback. Ordinary plan-based queries survive
because
Context's copy constructor carries the callbacksTCPHandlerinstalled; the failing shape readsunder a context built fresh from the global context, which never holds them.
Fix. The constructor now stores exactly the parallel-reading state it is given, and the context
resolution moved into
MergeTreeDataSelectExecutor::readFromParts. That is the single seam bothcontext-resolving callers pass through (
deserializeon a replica,StorageMergeTree::readandStorageReplicatedMergeTree::readLocalImplon a follower) and the only place where "no extension waspassed" means "take them from the context".
cloneis back to master's form.Validation. New test
04830_pr_plan_based_clone_marked_readaborts the server on master and is green50/50 here. Both hunks are load-bearing over 30 runs each: restoring the constructor fallback aborts the
server with the original message; dropping the
readFromPartsresolution fails the follower arm withbad_optional_access. It still reproduces on current master, so neither #112849 (since merged) nor #113788covers it. It does not close #111677, whose report is broader.
Workflow [PR]
Sync PR [sync-upstream/pr/114022]
Version info
26.8.5.15