Skip to content

Improve canceling queries with url() function - #102801

Merged
murphy-4o merged 7 commits into
ClickHouse:masterfrom
arenadata:ADQM-4403
May 4, 2026
Merged

murphy-4o merged 7 commits into
ClickHouse:masterfrom
arenadata:ADQM-4403

Conversation

@rvasin

@rvasin rvasin commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

Improve canceling queries with url() function by checking query cancellation between HTTP retry attempts.

Related #97459

Version info

  • Merged into: 26.5.1.262

@rvasin

rvasin commented Apr 15, 2026

Copy link
Copy Markdown
Contributor Author

@vdimir could you please assign can be tested label?

@yariks5s yariks5s added the can be tested Allows running workflows for external contributors label Apr 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [20b3344]

Summary: ✅


AI Review

Summary

This PR improves cancellation checks for url HTTP retries and adds focused GET/HEAD cancellation tests. However, the fix is still incomplete for one retry path: cancellation is installed only after getFirstAvailableURIAndReadBuffer returns, while that helper can already run retried HTTP work via res->eof under engine_url_skip_empty_files=1 with multiple URI options. High-level verdict: request changes for completeness of the cancellation fix.

Findings

⚠️ Majors

  • [src/Storages/StorageURL.cpp:386,593] Cancellation is still not wired into all retry paths for url reads. StorageURLSource now installs setCancellationCheck before the outer skip-empty loop, but StorageURLSource::getFirstAvailableURIAndReadBuffer can call res->eof internally (when skipping empty files across multiple URI options), and that can trigger ReadWriteBufferFromHTTP::nextImpl → doWithRetries before cancellation is attached.
    Suggested fix: pass a cancellation checker into getFirstAvailableURIAndReadBuffer and set it on res immediately after BuilderRWBufferFromHTTP::create, before any eof or other retrying calls.
Tests
  • ⚠️ Add a stateless test covering url failover options (multiple URI options) with engine_url_skip_empty_files=1, where an early option is empty/slow and cancellation is issued during the helper-internal eof path. Current tests cover GET/HEAD retry loops, but not this helper-internal path.
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 ⚠️ Cancellation behavior still has an uncovered retry path in url reads.
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Attach cancellation check inside getFirstAvailableURIAndReadBuffer before helper-internal retry-capable calls (not only in the caller after return).
    2. Add a regression test for the multi-URI + engine_url_skip_empty_files=1 cancellation path.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Apr 15, 2026
Comment thread src/Storages/StorageURL.cpp Resolved
@murphy-4o murphy-4o self-assigned this Apr 16, 2026
@rvasin

rvasin commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

@murphy-4o I see all tests were successfully passed (failed tests are not related). Is there anything else should I do in this PR?

@rvasin

rvasin commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

@murphy-4o, @vdimir could you please review this PR?

@@ -386,8 +386,17 @@ StorageURLSource::StorageURLSource(
while (getContext()->getSettingsRef()[Setting::engine_url_skip_empty_files] && uri_and_buf.second->eof());

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.

When engine_url_skip_empty_files=1, the while (... && uri_and_buf.second->eof()) loop can trigger retried HTTP requests before setCancellationCheck is installed. In that case, KILL QUERY still cannot interrupt retries during this phase.

Please install the cancellation callback immediately after getFirstAvailableURIAndReadBuffer returns (before the eof() loop), so retries in the empty-file-skip path are also cancellable.


if (auto * http_buf = dynamic_cast<ReadWriteBufferFromHTTP *>(uri_and_buf.second.get()))
{
http_buf->setCancellationCheck([this]() { return isCancelled(); });

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.

setCancellationCheck is still attached only after getFirstAvailableURIAndReadBuffer returns, but getFirstAvailableURIAndReadBuffer itself can execute retried HTTP work before that callback is installed:

  • inside StorageURLSource::getFirstAvailableURIAndReadBuffer, res->eof() is called when engine_url_skip_empty_files=1 and there are multiple URI options;
  • eof() can trigger ReadWriteBufferFromHTTP::nextImpl → doWithRetries.

So cancellation can still be delayed until after those retries finish.

Could we pass a cancellation checker into getFirstAvailableURIAndReadBuffer and install it on res immediately after create, before any eof/retrying calls?

@clickhouse-gh

clickhouse-gh Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (33/33) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@murphy-4o
murphy-4o added this pull request to the merge queue May 4, 2026
@rvasin

rvasin commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@murphy-4o could you remove PR from the merge queue? Because I am going to push refactored test in few minutes and we need to see the CI results again.

Merged via the queue into ClickHouse:master with commit 88cd803 May 4, 2026
165 checks passed
@murphy-4o

Copy link
Copy Markdown
Member

@murphy-4o could you remove PR from the merge queue? Because I am going to push refactored test in few minutes and we need to see the CI results again.

Apologies for missing your message earlier. Please submit a new PR for the refactor.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 4, 2026
@rvasin

rvasin commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@murphy-4o could you remove PR from the merge queue? Because I am going to push refactored test in few minutes and we need to see the CI results again.

Apologies for missing your message earlier. Please submit a new PR for the refactor.

@murphy-4o I created follow-up PR #104014 with refactoring. Please set can be tested and assign yourself to this follow up PR.

@Algunenano

Copy link
Copy Markdown
Member

This has introduced flakiness in the CI and 02270_errors_in_files_s3 started failing. Need to revert both PRs. Let's investigate and re-introduce

@rvasin

rvasin commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

This has introduced flakiness in the CI and 02270_errors_in_files_s3 started failing. Need to revert both PRs. Let's investigate and re-introduce

@Algunenano the follow up PR #104014 could fix the flakyness. I see you have reverted it too. Could you remind the link to see the flaky tests in ClickHouse?

@Algunenano

Copy link
Copy Markdown
Member

I reverted both because I could not revert just the old one. You can check the CI db with something like this

@Algunenano

Copy link
Copy Markdown
Member
  Since #104014 merged at 2026-05-05 02:41:56 UTC, there have been 15 failures of 02270_errors_in_files_s3 (1.87% fail rate, basically
  unchanged from the pre-fix 1.5%). I checked the 14 unique failing commits against the 660cf75fc65 merge:

  - 7 of 14 failing commits already include #104014 and still fail (e.g. d22c8ada51b, bf05e3363169, 2c9cb607f8d, d48dac539c4, ba68bbb16f1,
  f9183fd6f2c, 0b22515298a).
  - The other 7 are older PR bases that don't have the fix yet — those were already going to fail.

@rvasin

rvasin commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

@Algunenano it's clear why you reverted both PRs. But please read:
The second PR merged both tests from of current PR into one test:
04078_kill_query_url_function
and I fixed flakyness.
You may see that 04078_kill_query_url_function test was never failed. It's not flaky.
I will take a look 02270_errors_in_files_s3 now.

@Algunenano

Copy link
Copy Markdown
Member

Yes, I never said the new tests were flaky. I even said explicitly that 02270_errors_in_files_s3 started failing because of this PR. Old or new test, if a PR introduces flakiness it needs to be reverted as it caused a big impact in the developers work and cost

@rvasin

rvasin commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

I see in CI new test 04078_kill_query_url_function of my PR #104014 was failed only once in my PR and I have fixed.
And two tests of this PR 04077_kill_query_url_get_function was never failed, see
and 04078_kill_query_url_head_function test of this PR was also never failed, see
But potentially 04078_kill_query_url_head_function could fail. So I have created the second follow up PR. where I have merged both tests into one test 04078_kill_query_url_function and I have refactored test to remove any potential flakyness. So normally you should not revert it because it's very reliable solution.

Now I will try to see what is with 02270_errors_in_files_s3 test.

@rvasin

rvasin commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@Algunenano @murphy-4o
After analysis of logs for 02270_errors_in_files_s3 I see the problem is in the place where I have placed the cancellation check in my PRs:

void ReadWriteBufferFromHTTP::doWithRetries(std::function<void()> && callable,
                                            std::function<void()> on_retry,
                                            bool mute_logging, CheckCancelled check_cancelled) const
{
    [[maybe_unused]] auto milliseconds_to_wait = read_settings.http_retry_initial_backoff_ms;

    bool is_retriable = true;
    std::exception_ptr exception = nullptr;

    for (size_t attempt = 1; attempt <= read_settings.http_max_tries; ++attempt)
    {
        if (check_cancelled && check_cancelled())
        {
            throw Exception(ErrorCodes::QUERY_WAS_CANCELLED, "Query was cancelled during HTTP request");
        }

It's placed at the beginning of the loop and it influences the propagation of CANNOT_PARSE_INPUT_ASSERTION_FAILED which 02270_errors_in_files_s3 expects.

So it's correct to revert my two PRs.

I will create new PR to put the cancellation check in the right place and I will carefully check 02270_errors_in_files_s3 .

@rvasin

rvasin commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

Hopefully I was able to reproduce it by running:

 python3 -m ci.praktika run "Stateless tests (amd_debug, flaky check)" --test 02270_errors_in_files_s3 

on my local machine. So it will fix it and create new PR.

@rvasin

rvasin commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

I have fixed the cancellation callback in #104089 now test 02270_errors_in_files_s3 works fine.

alexey-milovidov pushed a commit that referenced this pull request May 5, 2026
This reverts commit 88cd803, reversing
changes made to d8baabb.
This was referenced May 15, 2026
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request May 19, 2026
Apply the edit-changelog skill workflow to the autogenerated 26.5 section:
add the release header, drop the NO CL ENTRY and NOT FOR CHANGELOG /
INSIGNIFICANT sections, rescue user-visible entries into Bug Fix,
Performance Improvement and Improvement, reconcile reverted PRs against
the rest of the changelog (drop entries for ClickHouse#102801, ClickHouse#103547, ClickHouse#103858,
ClickHouse#104800), record the isPrime / isProbablePrime revert-of-revert and
follow-up PRs on the original entry, prune Build/Testing to user-affecting
items, and apply a focused round of per-entry rewrites (proper-noun
capitalization, backticked identifiers, common typos).

Date, presentation URL and video URL in the release header are left as
FIXME placeholders.
pull Bot pushed a commit to weiyilai/ClickHouse that referenced this pull request Sep 26, 2026
Check query cancellation between HTTP retry attempts, so that `KILL QUERY`
and client disconnects stop a query over the `url` table function instead of
waiting until all the retries are exhausted.

`ReadWriteBufferFromHTTP` accepts an optional cancellation callback and, from
the second attempt on, leaves `doWithRetries` by `break` instead of throwing,
so the original HTTP error is still reported on the first attempt.
`StorageURLSource` passes `isCancelled` down to the buffer and checks for
cancellation after every step of its lazy initialization that can issue an
HTTP request.

This is a resubmission of ClickHouse#104089
by Roman Vasin (@rvasin) into the main repository, so that we can continue to
work on it. That pull request in turn fixes the reverted
ClickHouse#102801 and
ClickHouse#104014.

Differences from the original pull request: a comment in
`ReadWriteBufferFromHTTP::initialize` that a bad merge had garbled is restored,
the condition of the `chassert` in `readBigAt` is parenthesized, and the test is
renumbered to `04615` because `04078` is taken on `master`.

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

Labels

can be tested Allows running workflows for external contributors 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.

5 participants