Skip to content

Marshall blocks in parallel for plan-based parallel replicas - #121642

Merged
devcrafter merged 12 commits into
masterfrom
fix-118274-pr-plan-based-blocks-marshalling
Oct 1, 2026
Merged

devcrafter merged 12 commits into
masterfrom
fix-118274-pr-plan-based-blocks-marshalling

Conversation

@devcrafter

@devcrafter devcrafter commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Closes: #118274

With parallel_replicas_plan_based = 1 the replica runs a shipped plan fragment and never runs the planner, so the gate in Planner::buildPlanForQueryNode never fired and BlocksMarshalling was never added — blocks came back marshalled single-threaded in TCPHandler. The initiator now adds the step to the fragment in createRemotePlanFragmentForParallelReplicas; the local fragment deliberately does not get it, since nothing unmarshals it in this process. The context-dependent part of the planner gate is shared as contextAllowsBlocksMarshalling.

Two supporting changes:

  • BlocksMarshallingStep was serializable but not registered in QueryPlanStepRegistry, so a serialized plan carrying it failed with Unknown query plan step: BlocksMarshalling. Now registered with 21 as its introduction version; DBMS_QUERY_PLAN_SERIALIZATION_VERSION is bumped to 21, which also excludes older replicas from plan-based parallel replicas.
  • ReadFromParallelReplicasStep sets add_agg_info by checking the fragment root for a partial AggregatingStep; it now looks through the marshalling step.

Changelog category (leave one):

  • Experimental Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Blocks sent back by remote replicas are now marshalled in parallel (enable_parallel_blocks_marshalling) when parallel_replicas_plan_based is enabled.

🤖 Generated with Claude Code


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

Version info

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

The replica executes a shipped plan fragment and never runs the planner,
so `BlocksMarshalling` was never added. Put it on the fragment on the
initiator, and register the step so a serialized plan carrying it can be
deserialized at all.

Closes: #118274

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [100b3c9]

Summary: ✅


AI Review

Summary

This PR fixes the plan-based parallel-replicas path by injecting BlocksMarshallingStep into shipped fragments, registering the step for query-plan serialization, and teaching the replica-side reader to look through the marshalling wrapper when deciding whether to attach AggregatedChunkInfo. The current code still leaves two mixed-version serialized-query paths broken and does not update the matching native protocol spec, so the change is not ready to merge as-is.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/Marshall blocks in parallel for plan-based parallel replicas #121642#discussion_r4077663049] [src/Core/ProtocolDefines.h:130-135, src/Interpreters/ClusterProxy/executeQuery.cpp:1170-1174, src/QueryPipeline/RemoteQueryExecutor.cpp:180-186] Tying DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_PARALLEL_REPLICAS to the new global version 21 now disconnects every version-20 replica from the older query-based parallel-replicas path whenever serialize_query_plan = 1, even though that path still ships a plan without BlocksMarshalling. createRemotePlanForParallelReplicas still builds the old ReadFromParallelRemoteReplicasStep plan, ensureSerialized(DBMS_QUERY_PLAN_SERIALIZATION_VERSION) caches it at 21, and create_connections then drops any replica below 21 just because query_plan is present. tests/queries/0_stateless/03303_distributed_explain.reference still shows the serialize_query_plan = 1 branch under ReadFromRemoteParallelReplicas ending at Aggregating, so working version-20 replicas are lost for no query-local reason during a rolling upgrade.
    Suggested fix: split the version gate for the new plan-based fragment from the older query-based path, or stop pre-serializing the query-based path at the global version and let it serialize per peer / fall back to SQL when needed.
  • [dismissed by author -- https://github.com/Marshall blocks in parallel for plan-based parallel replicas #121642#discussion_r4076585361] [src/Planner/Planner.cpp:3286-3293, src/Client/Connection.cpp:1186-1195, src/Core/ProtocolDefines.h:136-139] Secondary queries that already get BlocksMarshallingStep from the planner still have no downgrade path for a pre-21 shard. The planner continues to add BlocksMarshallingStep on secondary non-local plans, and Connection::sendQueryPlan serializes such a plan against each peer's advertised plan version when the plan was not pre-serialized. For a version-20 shard, QueryPlanStepRegistry::versionToWrite("BlocksMarshalling", 20) now throws instead of falling back to SQL, so nested Distributed/remote() fan-out still fails on the sender during a rolling upgrade.
    Suggested fix: restore the per-peer SQL fallback for this ordinary serialize_query_plan path, or otherwise ensure a secondary-query plan with BlocksMarshalling is never serialized for peers below 21.

⚠️ Majors

  • [src/Core/ProtocolDefines.h:128-139, docs/reference/interfaces/specs/NativeProtocol.mdx:147, docs/reference/interfaces/specs/NativeProtocol.mdx:940-949] The PR changes the QueryPlan wire contract by introducing serialization version 21 and a versioned BlocksMarshalling step, but NativeProtocol.mdx still documents only the version-10 QueryPlan expansion and has no corresponding version-21 note. That leaves the public protocol spec behind the implementation, which violates the repository rule that wire-layout changes update the matching spec in the same PR.
    Suggested fix: update the QUERY_PLAN_SERIALIZATION and QueryPlan sections in docs/reference/interfaces/specs/NativeProtocol.mdx to describe the version-21 behavior and the new step gating.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Stop the version-21 bump from dropping still-valid version-20 replicas on the older query-based parallel-replicas path.
  2. Restore or replace the missing downgrade path for ordinary secondary-query serialize_query_plan fan-out to pre-21 shards.
  3. Update docs/reference/interfaces/specs/NativeProtocol.mdx for the version-21 QueryPlan contract change.

LLVM Coverage Report

Measured on commit 100b3c9.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 29/32 (90.62%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Sep 22, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). label Sep 22, 2026
@devcrafter
devcrafter requested a balanced review from Copilot September 22, 2026 21:20

Copilot AI left a comment

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.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 2 Medium severity

Open (3)
What changed in this PR

Adds plan-fragment-level BlocksMarshalling for plan-based parallel replicas so replicas marshal result blocks in parallel (when enabled) even though they don’t run the planner, and fixes deserialization/step-introspection fallout from introducing the step into serialized fragments.

Changes:

  • Add BlocksMarshallingStep to remote parallel-replica plan fragments under the same gate as the planner path.
  • Register BlocksMarshalling in QueryPlanStepRegistry and bump query plan serialization version.
  • Adjust ReadFromParallelReplicasStep to detect partial aggregation through a BlocksMarshallingStep root; add a stateless test covering the behavior.
File Description
tests/​queries/​0_stateless/​05238_pr_plan_based_blocks_marshalling.sh New stateless test validating step placement/counting and that shipped fragments execute correctly.
tests/​queries/​0_stateless/​05238_pr_plan_based_blocks_marshalling.reference Expected output for the new stateless test.
tests/​queries/​0_stateless/​04647_no_blocks_marshalling_for_local_shard_plan.sh Update comments now that BlocksMarshalling is registered and serialized plans can execute.
src/​Processors/​QueryPlan/​ReadFromParallelReplicas.cpp Look through BlocksMarshallingStep to preserve AggregatedChunkInfo detection.
src/​Processors/​QueryPlan/​QueryPlanStepRegistry.cpp Register BlocksMarshalling step so serialized fragments can be deserialized.
src/​Processors/​QueryPlan/​ParallelReplicasLocalPlan.cpp Add marshalling step to remote parallel-replica fragments when gating conditions match.
src/​Processors/​QueryPlan/​BlocksMarshallingStep.cpp Provide step-registration hook for QueryPlanStepRegistry.
src/​Core/​ProtocolDefines.h Bump plan serialization version due to new registered step.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 100b3c90f with master 1952ec9a7 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

262 translation units recompiled, 2390 s compile time in total, 262 of them have a recent master baseline.

Job report

Register the step with the version that introduced it, and fall back to
SQL for a peer below it, so a shard that cannot parse the name never gets
the plan. Share the context-side part of the gate with the planner.

Fix the new test under the randomized settings of the flaky check: with
`optimize_read_in_order = 0` the top-N read is picked for the Top-K filter
optimization and stays local, so the query it asserts on cannot use a LIMIT.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
devcrafter and others added 5 commits September 23, 2026 18:30
`chassert` is compiled out in release, where `children.front()` on an
empty node would be undefined behaviour. Silently skipping the step
instead would drop `AggregatedChunkInfo` and give wrong results, so
let the error propagate.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
It is the ordinary `serialize_query_plan` fan-out to shards, not parallel
replicas; the old comment led with the parallel-replicas case, which is
exactly the one that cannot reach the check.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Initiator in plan-based parallel replicas don't work with older remote
nodes
The comment on the step's introduction version still promised a SQL
fallback, and two includes were only needed by the removed plan walk.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@devcrafter
devcrafter requested review from nickitat and a balanced review from Copilot September 23, 2026 17:27

Copilot AI left a comment

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.

{
}

void registerBlocksMarshallingStep(QueryPlanStepRegistry & registry);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void registerBlocksMarshallingStep(QueryPlanStepRegistry & registry);

static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 20;
/// Version 21 registers the `BlocksMarshalling` step, so a plan fragment that pre-serializes its
/// result blocks can be shipped. A peer below it does not know the name and rejects the whole plan.
static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 21;

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.

DBMS_QUERY_PLAN_SERIALIZATION_VERSION is part of the inter-server QueryPlan packet contract, and this bump to 21 is paired with a new versioned BlocksMarshalling step becoming serializable.

The matching docs/reference/interfaces/specs/NativeProtocol.mdx entry still documents only the version-10 QueryPlan change, so the spec no longer describes the current wire behavior. Please update the QUERY_PLAN_SERIALIZATION / QueryPlan sections in the same change.

static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 20;
/// Version 21 registers the `BlocksMarshalling` step, so a plan fragment that pre-serializes its
/// result blocks can be shipped. A peer below it does not know the name and rejects the whole plan.
static constexpr auto DBMS_QUERY_PLAN_SERIALIZATION_VERSION = 21;

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.

Because DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_PARALLEL_REPLICAS still aliases the global DBMS_QUERY_PLAN_SERIALIZATION_VERSION, this bump to 21 now disconnects every version-20 replica from the older ReadFromParallelRemoteReplicasStep path as well. That path still builds a serialized plan without BlocksMarshalling (tests/queries/0_stateless/03303_distributed_explain.reference still shows the serialize_query_plan = 1 branch under ReadFromRemoteParallelReplicas ending at Aggregating), so those peers could execute the plan before this change and are now dropped during a rolling upgrade for no query-local reason.

The plan-based fragment does need a 21 gate, but it needs to be separated from the query-based parallel-replicas path, or that older path needs per-peer serialization or fallback instead of inheriting the stricter global minimum.

@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: af2b0ecb-617c-40de-b635-23e23d0a0448
  • MIRAI run: c233e2c6-93c3-4561-a48a-58a62abcf8d1
  • PR check IDs:
    • clickbench_2534836_1790769980
    • clickbench_2534843_1790769980
    • clickbench_2534859_1790769980
    • tpch_adapted_1_official_2534866_1790769980
    • tpch_adapted_1_official_2535360_1790770061
    • tpch_adapted_1_official_2535425_1790770064

@devcrafter
devcrafter added this pull request to the merge queue Oct 1, 2026
Merged via the queue into master with commit 7502b54 Oct 1, 2026
150 checks passed
@devcrafter
devcrafter deleted the fix-118274-pr-plan-based-blocks-marshalling branch October 1, 2026 14:49
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-parallel-replicas Parallel reading from replicas for query speed (not data replication consistency). pr-experimental Experimental Feature 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.

Plan-based parallel replicas: BlocksMarshalling steps are not inserted, so blocks are not marshalled in parallel

4 participants