Skip to content

Wait for the owner replica before giving its files to another one - #125611

Merged
scanhex12 merged 3 commits into
masterfrom
cluster-task-distributor-wait-for-replicas
Oct 15, 2026
Merged

scanhex12 merged 3 commits into
masterfrom
cluster-task-distributor-wait-for-replicas

Conversation

@scanhex12

@scanhex12 scanhex12 commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

When the iterator is exhausted, the distributor gave the files queued for replicas that have not asked for tasks yet to whoever asked first. The initiator's own replica asks for max_threads tasks at once before the other replicas receive the query, so three files on three nodes were read by one node (3/0/0 tasks).

The distributor can now wait for the owner of a file to connect before giving the file to another replica. The wait is controlled by the new setting cluster_function_wait_for_replica_ms, which is 0 (disabled) by default. Among replicas that are already working, it takes a file from the one with the most remaining bytes, kept as a per-replica counter, so stealing does not scan all unprocessed files.

Part 2: #124890

Changelog category (leave one):

  • Improvement

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

Wait for the owner replica before giving its files to another one


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

Version info

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

When the iterator is exhausted, the distributor gave the files queued for
replicas that have not asked for tasks yet to whoever asked first. The
initiator's own replica asks for `max_threads` tasks at once before the other
replicas receive the query, so three files on three nodes were read by one
node (3/0/0 tasks).

The distributor now waits up to one second for the owner of a file to connect
before giving the file to another replica. Among replicas that are already
working, it takes a file from the one with the most remaining bytes, kept as a
per-replica counter, so stealing does not scan all unprocessed files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5260922]

Summary: ✅


AI Review

Summary

The PR adds an optional owner-arrival wait for object-storage cluster reads and chooses a stealing source using per-replica task weights. Reviewed head 5260922f6302a6962cac44de4243db085f60ed52. The blocking wait stalls cancellation and can prevent owner requests from being handled; the existing concern about unavailable byte sizes also remains. ❌ Block pending the scheduling fix.

PR Metadata

The Improvement category is appropriate. A changelog entry is required, but the current entry omits the affected feature, the new setting, and the disabled default. Suggested replacement:

Added cluster_function_wait_for_replica_ms to let *Cluster table functions and parallel replicas over object storage wait for file owners before stealing their tasks. The wait is disabled by default (0).

Findings

❌ Blockers

  • StorageObjectStorageStableTaskDistributor.cpp:252: Waiting for an owner must preserve cancellation and replica-request progress. getNextTask waits synchronously inside RemoteQueryExecutor::processReadTaskRequest. The caller holds was_cancelled_mutex while processing packets (read, readAsync); cancel needs that mutex. With an unseen owner and cluster_function_wait_for_replica_ms = 60000, cancellation can stall for a minute and delay cancelling the remaining processors. With max_threads = 1, the blocked worker also cannot service another owner's pending request, so the interval expires before that owner can be marked as seen. Suspend the pending response asynchronously and terminate pending waits on cancellation.

⚠️ Majors

  • StorageObjectStorageStableTaskDistributor.cpp:201: The claimed byte-based policy becomes file counting for brace-expanded keys. The cluster iterator passes skip_object_metadata = true; the brace-expansion path forwards this to KeysIterator, which returns zero-size placeholder metadata. Consequently, getTaskWeight assigns every such file weight 1, and the stealing policy cannot distinguish a large file from a small one. General glob listings do carry sizes, so this concern is specifically about iterators without size metadata. Supply sizes/hints for those paths, or explicitly document and test the count-based policy when sizes are unavailable. This is the existing unresolved finding; no duplicate inline comment was posted.

💡 Nits

  • PR changelog: name the affected cluster reads and the opt-in setting; use the replacement in PR Metadata.
Tests
  • ⚠️ All added distributor tests invoke replicas sequentially. Add a focused regression proving that an owner request arriving during the wait is serviced before the deadline with one initiator worker, and that cancellation completes without waiting out a long configured interval.
  • ⚠️ The byte-weight test supplies synthetic metadata. Add coverage using the production brace-expanded iterator, or test and document its unknown-size behavior.
Missing context / blind spots
  • ⚠️ No local ClickHouse build is available; the scheduling and cancellation findings are based on source traces, without an end-to-end server reproduction. A focused server regression would close this validation gap.
Final Verdict

❌ Block. Make the wait asynchronous and cancellable, add focused progress/cancellation coverage, and reconcile the byte-weight policy with iterators that do not supply sizes.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 13, 2026
The time the distributor waits for a replica to ask for tasks before giving
its files to other replicas was hardcoded to one second. It is now a setting
with the same default; `0` restores the previous behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). label Oct 13, 2026
std::lock_guard lock(mutex);
unprocessed_files.emplace(file_identifier, object_info);
if (unprocessed_files.emplace(file_identifier, object_info).second)
remaining_bytes[file_replica_idx] += getTaskWeight(object_info);

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.

getTaskWeight() does not see real byte sizes on the main *Cluster path. StorageObjectStorageCluster::getTaskIteratorExtension still builds the iterator with skip_object_metadata=true (src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp:463), and KeysIterator::next then returns the placeholder metadata with size_bytes == 0 / is_fetched == false unless a specialized iterator overrides getFileSizeHint() (src/Storages/ObjectStorage/StorageObjectStorageSource.cpp:2352). For ordinary object-storage queries this counter is therefore just counting files, not bytes, so the new stealing policy can still leave the largest files concentrated on one replica despite the new test passing with synthetic metadata. Can we plumb real sizes into this iterator path, or adjust the heuristic/tests to match the data that is actually available here?

@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 5260922f6302a6962cac44de4243db085f60ed52 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@murphy-4o murphy-4o left a comment

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.

LGTM

return next_file;
}

replica_seen_cv.wait_until(lock, *stealing_allowed_since);

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.

better to add a comment for the exit conditions

…ment exit conditions

The wait is now disabled by default, which keeps the previous behaviour unless
the setting is enabled explicitly. Also add a comment describing the exit
conditions of the loop in `getAnyUnprocessedFile`, as requested in the review.

Related: #125611

Co-Authored-By: Claude Opus 5.5 <[email protected]>
return next_file;
}

replica_seen_cv.wait_until(lock, *stealing_allowed_since);

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.

❌ Make the owner wait asynchronous and cancellable

This wait runs synchronously inside RemoteQueryExecutor::processReadTaskRequest. Both read and readAsync hold was_cancelled_mutex while processing that packet, and RemoteQueryExecutor::cancel needs the same mutex. If an owner has not requested a task, setting cluster_function_wait_for_replica_ms = 60000 therefore makes cancellation block for up to a minute: neither cancellation nor a query timeout wakes this condition variable, and the cancellation sweep cannot reach the remaining processors until this call returns.

The same blocking call also occupies a pipeline worker. With max_threads = 1, the sole worker cannot handle another replica's pending request to mark it in replica_seen, so the wait necessarily expires and then steals that owner's files even if its request is already on the socket.

Please suspend the pending task response through the asynchronous scheduling path, allowing other replica requests and cancellation to run, and wake/terminate pending waits on cancellation. Add coverage for cancellation during the wait and owner arrival with one initiator worker.

@scanhex12
scanhex12 added this pull request to the merge queue Oct 15, 2026
Merged via the queue into master with commit 19b5d2a Oct 15, 2026
350 of 351 checks passed
@scanhex12
scanhex12 deleted the cluster-task-distributor-wait-for-replicas branch October 15, 2026 16:02
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 2026
scanhex12 added a commit that referenced this pull request Nov 4, 2026
…tting-improvements

Most of this branch landed through separate PRs:
#125611
#125615
#125636
Their versions are kept. Bucket splitting by `min_buckets` is applied on
top of the greedy splitter of master, the weight of a byte range bucket is
capped by the file size, byte ranges are gated by
`cluster_table_function_split_by_split_offsets`, and the duplicate test of
the filter pushdown is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). pr-improvement Pull request with some product improvements 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.

3 participants