Skip to content

Make S3Queue batch size halving global to fix flaky test - #98810

Merged
kssenii merged 2 commits into
masterfrom
fix-s3queue-parallel-inserts-flaky
Mar 5, 2026
Merged

kssenii merged 2 commits into
masterfrom
fix-s3queue-parallel-inserts-flaky

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Summary

  • The per-task batch halving in S3Queue streamToViews didn't converge with many streaming tasks (16 in the test). Different tasks encountered the bad file each time, each halving once and then resetting on the next successful batch. No single task ever reached batch size 1 to isolate the bad file.
  • Replace std::vector<size_t> max_files_override_per_task with a single std::atomic<size_t> max_files_override shared across all tasks. When ANY task encounters a batch failure, ALL tasks see the halved batch size on their next iteration.
  • Also increase run_with_retry default retries from 100 to 300 for additional MSan robustness.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=c90fedd0932aea5f5c4b4ed1a353b1255df9c759&name_0=MasterCI&name_1=Integration%20tests%20%28amd_msan%2C%206%2F6%29

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...


Note

Medium Risk
Touches concurrent error-handling logic in StorageObjectStorageQueue::streamToViews, which can change batching/throughput behavior under failures. Uses an atomic shared override, so concurrency semantics are improved but should be validated under load and repeated failures.

Overview
Makes S3/Azure Queue streaming batch-size backoff global: replaces per-streaming-task max_files_override_per_task with a single std::atomic<size_t> max_files_override so any task’s failed insert halves the next batch size for all tasks until a successful commit resets it.

Also increases integration-test run_with_retry retries from 100 to 300 to reduce flakiness in the parallel-inserts failure scenario (notably under MSan).

Written by Cursor Bugbot for commit ba18e2c. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.439

alexey-milovidov and others added 2 commits March 5, 2026 06:09
When an INSERT batch fails in `streamToViews`, the code previously
re-threw the exception after committing the failed batch. This caused
the entire `streamToViews` to exit, requiring a rescheduling cycle
(1+ seconds) before processing could resume. Under MSan (5-10x slower),
this delay compounded: remaining files from the iterator went unprocessed
until the next scheduling cycle, and the per-task batch halving never
converged because different tasks encountered the bad file each time,
each resetting their batch size on the next successful iteration.

Replace `throw` with `continue` so the while loop immediately processes
the next batch from the iterator. The failed batch is already properly
committed (good files reset for retry, bad files marked appropriately),
so there is no need to propagate the exception.

Also increase `run_with_retry` default retries from 100 to 300 in the
test for additional MSan robustness.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=c90fedd0932aea5f5c4b4ed1a353b1255df9c759&name_0=MasterCI&name_1=Integration%20tests%20%28amd_msan%2C%206%2F6%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The per-task batch halving didn't converge with many streaming tasks
because different tasks encountered the bad file each time. Each task
would halve once, then succeed without the bad file in the next batch,
resetting to the default batch size. No single task ever reached
batch size 1 to isolate the bad file.

Replace the per-task `max_files_override_per_task` vector with a single
shared `std::atomic<size_t> max_files_override`. Now when ANY task
encounters a batch failure, ALL tasks see the halved batch size on their
next iteration. This makes the halving converge regardless of which
task encounters the bad file.

Also restore the `throw` after batch failure (was changed to `continue`
in the previous commit) since the batch must be retried as a whole for
insert deduplication into MergeTree tables.

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

clickhouse-gh Bot commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ba18e2c]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 5, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

if (!current_max)
current_max = processing_progress->processed_files.load();
effective_max_files = std::max<size_t>(current_max / 2, 1);
max_files_override = std::max<size_t>(current_max / 2, 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-atomic load-compute-store allows stale halving overwrites

Medium Severity

The halving of max_files_override uses a non-atomic load-then-store pattern. Thread A can load a stale value (e.g., 4), get preempted while threads B and C further halve (4→2→1), then Thread A wakes and stores 2, overwriting the more-converged value of 1. Under MSan (which the PR explicitly targets), threads are significantly slowed, widening this race window. A compare-and-swap loop that only decreases the value would prevent stale writes from undoing convergence progress.

Fix in Cursor Fix in Web

commit(/*insert_succeeded=*/ true, rows, sources, transaction_start_time);
file_iterator->releaseFinishedBuckets();
effective_max_files = 0;
max_files_override = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Success reset undoes concurrent halving across all tasks

Medium Severity

The unconditional max_files_override = 0 on the success path resets the global override for ALL tasks. If Task A fails and halves the override while Task B (already running with the old batch size) succeeds, Task B's reset wipes out Task A's halving. This is the same non-convergence pattern the PR describes for the per-task approach — "each halving once and then resetting on the next successful batch" — but now at global scope, making it potentially worse since any single successful task undoes progress for everyone.

Fix in Cursor Fix in Web

@kssenii kssenii self-assigned this Mar 5, 2026
@kssenii
kssenii added this pull request to the merge queue Mar 5, 2026
Merged via the queue into master with commit 0bdede5 Mar 5, 2026
146 checks passed
@kssenii
kssenii deleted the fix-s3queue-parallel-inserts-flaky branch March 5, 2026 16:12
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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