Skip to content

Add parallel_replicas_prefer_local_replica setting - #100139

Merged
alexey-milovidov merged 23 commits into
masterfrom
parallel_replicas_no_local_preference
Apr 25, 2026
Merged

alexey-milovidov merged 23 commits into
masterfrom
parallel_replicas_no_local_preference

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

Add parallel_replicas_prefer_local_replica setting that, when disabled, allows replicas for parallel reading to be selected purely by the load balancing algorithm without forcing the local replica into the set. This enables queries with max_parallel_replicas = 1 to be directed to another host, which can improve cache locality when many short queries are distributed across a cluster.

Closes #79335

Changelog category (leave one):

  • New Feature

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

Add parallel_replicas_prefer_local_replica setting: when disabled, parallel replicas are selected purely by the load balancing algorithm, allowing even max_parallel_replicas = 1 queries to be directed to another host.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

New setting parallel_replicas_prefer_local_replica (Bool, default true):

  • When enabled (default), the local replica is always included in the set of replicas used for parallel reading (existing behavior).
  • When disabled, the local replica is not given any preference and replicas are selected purely by the load_balancing algorithm. This allows queries with max_parallel_replicas = 1 to be directed to another host, which can improve cache locality when many different short queries are distributed across a cluster.

The setting is documented in the auto-generated session settings reference via its DECLARE block in src/Core/Settings.cpp, which populates docs/en/operations/settings/settings.md.

Example:

SELECT count() FROM my_table
SETTINGS
    enable_parallel_replicas = 1,
    max_parallel_replicas = 1,
    cluster_for_parallel_replicas = 'my_cluster',
    parallel_replicas_prefer_local_replica = 0;

Version info

  • Merged into: 26.5.1.92

When disabled, replicas for parallel reading are selected purely by the
load balancing algorithm without forcing the local replica into the set.
This allows queries with `max_parallel_replicas = 1` to be directed to
another host, which can improve cache locality when many short queries
are distributed across a cluster.

Closes #79335

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

clickhouse-gh Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d8994ea]

Summary: ✅


AI Review

Summary

This PR adds the parallel_replicas_prefer_local_replica setting and threads it through both SELECT and INSERT SELECT task-based parallel-replicas paths, with a dedicated stateless test that checks behavior for max_parallel_replicas = 1 and default-compatibility behavior. I did not find any correctness, safety, compatibility, or rollout issues that require changes.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ✅
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Mar 19, 2026
-- With prefer_local_replica = 0 and max_parallel_replicas = 1, the query should
-- use the parallel replicas path (i.e., the query is sent to a replica selected
-- by load balancing, not necessarily the local one).
SELECT count()

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.

The new test does not actually verify the behavior this PR introduces.

This query only asserts count() = 1000, which is identical whether parallel_replicas_prefer_local_replica = 0 is respected or ignored. So CI can stay green even if local-replica preference is still enforced for max_parallel_replicas = 1.

Please add an assertion that observes replica selection/path directly (for example via system.query_log / ProfileEvents / trace-level evidence), and keep the existing result check as a secondary correctness assertion.

alexey-milovidov and others added 2 commits March 21, 2026 22:59
Move `parallel_replicas_prefer_local_replica` to version 26.4 in
`SettingsChangesHistory.cpp` to fix the `02995_new_settings_history` test.

Strengthen the stateless test to verify actual parallel replica usage
via `system.query_log` ProfileEvents, not just query result correctness.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100139&sha=latest&name_0=PR

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Interpreters/Context.cpp Resolved
alexey-milovidov and others added 7 commits March 22, 2026 11:07
…tion

The trivial count optimization (`optimize_trivial_count_query`) intercepts
bare `SELECT count()` and returns the answer from metadata, completely
bypassing the parallel replicas code path. This caused
`ParallelReplicasUsedCount` to be 0 in all test cases.

Adding a second aggregate (`min(key)`) prevents the optimization from
triggering, ensuring the query goes through the parallel replicas path.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
When `parallel_replicas_prefer_local_replica` is disabled, the INSERT SELECT
code path in `InterpreterInsertQuery` was still forcing local pipeline
participation, creating inconsistent behavior with regular SELECT queries.

Skip the local pipeline when the setting is disabled, and add a test case
for INSERT SELECT with parallel replicas.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…nt_proj` interference

The `minmax_count_proj` projection optimization on the parallel replicas
follower interferes with the coordinator-based reading path, causing 0
rows to be returned. Switch from `min(key)` to `sum(key)` (which cannot
be satisfied by projections) and add `optimize_use_projections = 0` for
safety.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…mized settings interference

The test was failing because randomized CI settings could set
`automatic_parallel_replicas_mode = 2`, which causes
`canUseTaskBasedParallelReplicas` to return false, preventing
the parallel replicas path from being taken regardless of the
`parallel_replicas_prefer_local_replica` setting under test.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov and others added 4 commits March 30, 2026 02:59
…yzer CI

In CI configurations where `allow_experimental_analyzer` = 0, the default
`parallel_replicas_only_with_analyzer` = 1 causes
`canUseTaskBasedParallelReplicas` to return false, making
`ParallelReplicasUsedCount` checks produce false negatives.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Core/Settings.cpp Resolved
alexey-milovidov and others added 7 commits April 19, 2026 13:17
`InterpreterInsertQuery::buildInsertSelectPipelineParallelReplicas` has a
hard `allow_experimental_analyzer` gate independent of
`parallel_replicas_only_with_analyzer`, so in analyzer-disabled CI runs
the assertion `ProfileEvents['ParallelReplicasUsedCount'] > 0` for the
INSERT SELECT case could fail for reasons unrelated to this PR.

Force `allow_experimental_analyzer = 1` in the INSERT SELECT `SETTINGS`
to keep the assertion deterministic.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…r merge

The 26.4 branch already diverged; place the new setting in the
currently in-progress 26.5 version block.
@clickhouse-gh

clickhouse-gh Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.30% 76.20% -0.10%

Changed lines: 100.00% (30/30) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks ready.

@alexey-milovidov alexey-milovidov self-assigned this Apr 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 25, 2026
Merged via the queue into master with commit 2a33437 Apr 25, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the parallel_replicas_no_local_preference branch April 25, 2026 15:48
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product 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.

Parallel replicas: a mode with no localhost preference, when even a query with max_parallel_replicas = 1 can be directed to another host

2 participants