Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1f75154
Pace CAS part uploads and stop them in a part being built
filimonov Nov 27, 2026
7d5a0b1
Move the generation fake storage to a header; record write settings
filimonov Nov 27, 2026
88a7ed3
Test the CAS upload guard on a recording object storage
filimonov Nov 27, 2026
45548c6
Test paced and stopped CAS uploads on real servers
filimonov Nov 27, 2026
8363501
Document write limits and stopped uploads on CAS disks
filimonov Nov 27, 2026
0c581eb
test(cas): fetch cleanup after a stop must not publish the part
filimonov Nov 28, 2026
f0920f4
fix(cas): recursive removal of a part discards what the transaction s…
filimonov Nov 28, 2026
37f8746
test(cas): a stopped byte fetch leaves no temporary part
filimonov Nov 29, 2026
d2ffcf2
Merge pull request #115733 from ClickHouse/shutdown-cancel-merges
alexey-milovidov Aug 29, 2026
8434948
Merge pull request #113653 from groeneai/fix-executor-shutdown-drains…
alexey-milovidov Sep 22, 2026
2de4145
Merge pull request #123870 from jgsogo/fix/121748-mergelist-cancelall
alexey-milovidov Oct 5, 2026
16b724f
Do not let query cancellation interrupt transaction finalization
Algunenano Oct 5, 2026
32dd1f4
Carry `QueryCancellationBlocker` into the distributed cache backgroun…
Algunenano Oct 8, 2026
831e3cb
Do not let cancellation interrupt the rollback of a merge or mutation
alexey-milovidov Oct 21, 2026
b3aaf39
fix: keep S3 retries of cleanup when the query or merge is cancelled
CheSema Nov 11, 2026
acc4f60
Revert "Revert "Cancel vector similarity index builds when a merge or…
Algunenano Oct 3, 2026
df87d34
Merge pull request #127662 from groeneai/05294-time-merge-cancellatio…
alexey-milovidov Oct 24, 2026
0b2b72c
Merge pull request #113939 from groeneai/fix-background-mutation-canc…
CheSema Nov 2, 2026
d42b1bd
Run system log flushes in a thread group without a query context
cwurm Nov 1, 2026
685fa52
Keep the profilers of a thread when it detaches from a group without …
cwurm Nov 13, 2026
495b18d
Test thread group switching with a group that has no query context
filimonov Nov 29, 2026
7ee546c
Cancel replication fetches and part moves that are blocked in a write
filimonov Nov 29, 2026
f6e2ba3
fix(cas): a stopped relink does not fall back to a byte fetch
filimonov Nov 29, 2026
68f422e
test(cas): cut the upload of a merge, a mutation, a fetch and a move
filimonov Nov 29, 2026
b5edc12
docs(cas): stopping the upload of background work
filimonov Nov 29, 2026
8369d42
fix(cas): the leader of a shared ref-log flush ignores its query's ca…
filimonov Nov 30, 2026
25116bc
test(cas): replication bandwidth limits pace a byte fetch into a CAS …
filimonov Dec 1, 2026
a1df4b1
Merge branch 'antalya-26.8' into work/antalya-26.8/cas-upload-guard
filimonov Dec 1, 2026
c4d694a
test(cas): time the paced byte fetch by `system.part_log`
filimonov Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/en/antalya/cas/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ Source: `ServerSettings.cpp`. This setting is process-wide rather than scoped to
|---|---|---|
| `cas_blob_upload_pool_size` | `64` | Size of the dedicated server-wide thread pool used to upload blobs in parallel when committing a `CAS` part. Zero is rejected: the pool must have at least one thread |

## Write bandwidth limits {#write-bandwidth-limits}

These limits pace blob bodies and part files staged in the object store with `cas_staging_backend = s3`:

| Setting | Paces |
|---|---|
| `max_remote_write_network_bandwidth` | The uploads of the query |
| `max_remote_write_network_bandwidth_for_server` | Every upload of the server, also merges, mutations and moves |
| `max_network_bandwidth_for_user`, `max_network_bandwidth_for_all_users` | Uploads that run under a query, together with the query's other network traffic that these limits count |

Part manifests, refs, the catalog, the mount lease, the GC state and deletes are never paced by these limits.

A request that the S3 client sends by itself during an upload may be throttled with the upload. Two such requests
are a token request with `http_client = gcp_oauth` and the bucket region lookup of a client that has not sent a
request yet.

After an upgrade, a server that already has one of these limits set starts to pace `CAS` uploads. A low server-wide
limit makes merges and mutations hold their queue slots longer while they upload. A stop ends such an upload; see
[stopping `CAS` work](/antalya/cas/operations/troubleshooting#stopping-cas-work).

## `SYSTEM CAS` commands {#system-commands}

`SYSTEM CAS GC RUN`, `SYSTEM CAS GC STOP`, `SYSTEM CAS GC START`, `SYSTEM CAS GC REBUILD`,
Expand Down
66 changes: 63 additions & 3 deletions docs/en/antalya/cas/operations/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ A query that is killed with `KILL QUERY` or hits its time limit stops `CAS` work
- A retry after a failed request. The retry is not sent. When a read or a blob upload failed before the stop, the query fails with that request's error, or with a retry-later `NETWORK_ERROR` (210) that names a credential refusal, not with the stop's own error. That request's error can itself be `TIMEOUT_EXCEEDED` (159), when the failed attempt timed out. When a metadata write failed before the stop, the query fails with a retry-later `NETWORK_ERROR` (210) that says the write is unresolved and what a read of the key found; it does not carry the store's error. A metadata write that the store refused, with no earlier attempt left unanswered, keeps the store's error. When such a write was refused for its credentials and the stop cancels its resend with fresh credentials, it fails with a retry-later `NETWORK_ERROR` (210) that names the refusal. When the request fails after the stop, the S3 client reports the stop itself (see below). The pause before a retry notices the stop within 200 ms. A pause inside cleanup that the operation owes after a send runs to its end, and the request is sent once more unless the mount fence, the mount lease or a deadline stops it; if that attempt fails after the stop, it is not retried.
- The next step of work that `CAS` can resume later: the catalog cleanup of a manual `SYSTEM CAS GC RUN`, the drain of `SYSTEM CAS DROP POOL MEMBER`, and the upload of a blob body the writer has to publish, because the store does not hold the blob or holds one the writer cannot reuse. The query fails with `QUERY_WAS_CANCELLED` or `TIMEOUT_EXCEEDED`. When an earlier upload attempt of the same blob failed, the check before the next upload fails the query with that attempt's error instead. The drain checks the stop before each of the member's namespaces, before the listing of its manifests and after each page of that listing arrives, before each group of its manifests and before each key it deletes. A `SYSTEM CAS DROP POOL MEMBER` stopped after its drain began leaves the member retired; rerun it to finish. Stopped before that, it can leave the member as it was.

Everywhere else the `CAS` requests of a stopped query run as on a plain S3 disk. This covers the first attempt of every request, a new decision after another writer won a race before the stop, waits for other callers, cleanup after a failed write, transaction rollback, dropping a table or a temporary table, `SYSTEM CAS FORGET`, and the scan of `SYSTEM CAS FSCK`.
Everywhere else the `CAS` requests of a stopped query run as on a plain S3 disk, except the upload of a part that is being built (see below). This covers the first attempt of every other request, a new decision after another writer won a race before the stop, waits for other callers, cleanup after a failed write, transaction rollback, dropping a table or a temporary table, `SYSTEM CAS FORGET`, and the scan of `SYSTEM CAS FSCK`.

The S3 client reports a stopped query's error in place of any failed answer from the store, as on a plain S3 disk. This includes an expected answer of a healthy store, such as `404` for a key that does not exist. So the query can still fail with `QUERY_WAS_CANCELLED` or `TIMEOUT_EXCEEDED` when the store answers with an error after the stop. A bulk delete that the store answers successfully, with an error for one of its keys, keeps that key's error. Writes of several queries to one table's metadata can be sent together by one of them. When that query is stopped, such a failed answer can fail the other queries' writes in the same request too.

Expand All @@ -154,12 +154,72 @@ Two background owners stop `CAS` work at its next step, with their own error:

A `GC` round that `SYSTEM CAS GC STOP` ends is recorded as `Stopped` in `system.cas_gc_log`. A manual round whose query is killed or timed out is recorded as `Stopped` when it ends with `QUERY_WAS_CANCELLED`, or with one of the transient errors `S3_ERROR`, `NETWORK_ERROR`, `SOCKET_TIMEOUT`, `TIMEOUT_EXCEEDED`, `ABORTED` or `MEMORY_LIMIT_EXCEEDED`. It is recorded as `Error` when it ends with any other error, for example `READONLY`. A stop that comes after the round's last check leaves the round's own outcome.

A stop also ends the upload of a part that is being built:

- The `INSERT` ends with `QUERY_WAS_CANCELLED` after `KILL QUERY`, or with `TIMEOUT_EXCEEDED` when it reaches
`max_execution_time` with `timeout_overflow_mode = 'throw'`. It may be retried unchanged.
- The stop is noticed while a blob body is being sent, usually within a second. A bandwidth limit can add its own
wait. No time is promised.
- With `cas_staging_backend = s3` the write to the staging object is what is stopped, also for a data file written
into an existing part.
- A blob body that was cut may still have reached the store. Cleanup is best effort, and the rest is left to GC. The
next mount start removes a staging object.
- The server aborts the incomplete multipart upload of a cut body. If the abort fails, the upload stays until the
bucket's lifecycle rule removes it.
- A stopped upload may log an error line for each cancelled request of a multipart upload.
- A fetch or a move that a query runs itself (`ALTER TABLE ... FETCH PARTITION`, `ALTER TABLE ... MOVE PARTITION`)
is stopped with that query.

The upload of the result of a merge or a mutation, of a fetched part and of a moved part is stopped too. The stop
reaches the body being sent at its next check. On a test stand the upload stopped within seconds. That is a measured
time, not a bound.

| What stops the upload | What does not |
|---|---|
| `SYSTEM STOP MERGES`, `SYSTEM STOP FETCHES`, `SYSTEM STOP MOVES` for the table | `KILL QUERY` on a foreground `OPTIMIZE`: the merge runs under its own task. `OPTIMIZE ... DRY RUN PARTS` runs inside the query and stops with it |
| `KILL MUTATION` | A shutdown while a client connection is still open: it waits for the connection first |
| `DROP TABLE`, `DETACH TABLE` | Another replica that is already uploading the same result |
| Server shutdown | `SYSTEM STOP MERGES ... ON VOLUME`: it changes selection and does not cancel a running merge |
| | A server-side copy: no body is streamed |

What you see after such a stop:

- A cancelled merge or mutation is expected after a stop, not a failure. A `MergeTree` table usually logs an error
line "Cancelled merging parts" or "Cancelled mutating parts" with code `ABORTED`. A cut that lands at the final
commit can log it at a lower level. A `ReplicatedMergeTree` table logs the cancellation at information level and
leaves `last_exception` of the queue entry as it was.
- A cut with local staging may leave no failed row in `system.part_log`.
- The source parts of a merge or mutation stay active, as long as the table exists. `DROP TABLE` removes its data.
- After `SYSTEM STOP MERGES`, `SYSTEM STOP FETCHES` or `SYSTEM STOP MOVES`, work that is still eligible can be
selected again after the matching `SYSTEM START`. A `MergeTree` table selects afresh, so an insert made while
merges were stopped can change which parts are merged. After a restart, the work is scheduled again. This holds while the table and the work
still exist: a killed mutation is gone.
- A running fetch has a row in `system.replicated_fetches`, also a fetch by relink. A running move has a row in
`system.moves`.

A stop can leave:

- The objects of a cancelled merge or mutation whose result part goes to a plain S3 disk of the same storage policy.
- The objects that a stopped byte fetch onto a plain S3 disk had already written.
- A `tmp-fetch_` directory or a `moving/` directory, when the stop coincides with a failed request. A later byte fetch of the same part removes
a `tmp-fetch_` directory only when it uses the same disk and the same temporary path; a retry can choose another disk. The startup
cleanup does not look under `detached/`: there, `ALTER TABLE ... DROP DETACHED PART` removes a named leftover, and it needs the
setting `allow_drop_detached`. A `moving/` directory is
removed when the table is loaded again, if removal of stale moving parts is enabled (the default).

Known limits:

- A `SYSTEM STOP` followed by a `SYSTEM START` in quick succession can be missed by an upload that is running.
- Other work can still hold a shutdown, for example a fetch blocked in a read that does not return.
- A running step of an `EXPORT PART` is not cancelled by a shutdown. It is waited for and can hold the shutdown.
- Cleanup after a cut retries its requests. A shutdown with the object store unreachable waits for those retries.

No stop interrupts:

- A request already in flight.
- Any other request already in flight.
- The first attempt of a read, and a read that only waits for another reader's result.
- A recovery walk that is already running. On S3 the walk of a stopped query still fails at the first error answer. A walk that looks for a record after its last one ends with such an answer, the `404` for that key; a walk that ends at the table's final removal record looks for none. The failed read is not retried. When the stop cuts short the read that follows a refused write of the walk, the walk ends with a retry-later `NETWORK_ERROR` (210) instead of starting again.
- Cleanup that the operation owes after a send, and merges.
- Cleanup that the operation owes after a send.

If the stop lands after a metadata write whose outcome is unknown, the error is the retry-later `NETWORK_ERROR` (210), and its message says the write is unresolved or its outcome is unknown. Check the table before you repeat the write.

Expand Down
4 changes: 4 additions & 0 deletions src/Common/FailPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static struct InitFiu
REGULAR(rmt_dedup_conflict_part_name_missing) \
REGULAR(smt_dedup_conflict_part_name_missing) \
REGULAR(merge_tree_sink_on_start_random_sleep) \
REGULAR(merge_tree_sequential_source_sleep_before_read) \
REGULAR(replicated_sends_sleep_before_file_send) \
PAUSEABLE_ONCE(replicated_fetch_pause_after_projections) \
REGULAR(use_delayed_remote_source) \
ONCE(remote_query_executor_cancel_before_send) \
PAUSEABLE_ONCE(remote_query_executor_receive_packet_pause) \
Expand Down Expand Up @@ -405,6 +408,7 @@ static struct InitFiu
REGULAR(smt_takeover_fake_hardware_error_after_set) \
REGULAR(cas_relink_receiver_force_mechanism_failure) \
PAUSEABLE_ONCE(cas_relink_receiver_pause_before_confirm) \
REGULAR(cas_relink_receiver_cancel_fetches_in_preparation) \
REGULAR(cas_relink_sender_omit_pool_cookie) \
REGULAR(cas_relink_receiver_drop_forced_disk) \
ONCE(cas_gc_scheduler_fail_before_heartbeat_worker_start) \
Expand Down
4 changes: 2 additions & 2 deletions src/Common/OOMCanary/OOMCanary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ void OOMCanary::onCanaryOOM()
/// Cancel all merges and mutations
try
{
context->getMergeList().cancelAll();
LOG_INFO(log, "Cancelled all running merges");
context->getMergeList().cancelCurrent();
LOG_INFO(log, "Cancelled currently running merges");
}
catch (...)
{
Expand Down
15 changes: 13 additions & 2 deletions src/Common/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class JobWithPriority

DB::OpenTelemetry::TracingContextOnThread thread_trace_context;

/// Propagated like the tracing context: a job scheduled under `QueryCancellationBlocker` runs under one.
bool query_cancellation_blocked = false;

/// Call stacks of all jobs' schedulings leading to this one
std::vector<FramePointers> frame_pointers;
bool enable_job_stack_trace = false;
Expand All @@ -253,11 +256,12 @@ class JobWithPriority

JobWithPriority(
Job job_, Priority priority_, CurrentMetrics::Metric metric,
const DB::OpenTelemetry::TracingContextOnThread & thread_trace_context_,
const DB::OpenTelemetry::TracingContextOnThread & thread_trace_context_, bool query_cancellation_blocked_,
bool capture_frame_pointers, ScopedDecrement available_threads_decrement_)
: job(job_), priority(priority_), metric_increment(metric),
available_threads_decrement(std::move(available_threads_decrement_)),
thread_trace_context(thread_trace_context_), enable_job_stack_trace(capture_frame_pointers)
thread_trace_context(thread_trace_context_), query_cancellation_blocked(query_cancellation_blocked_),
enable_job_stack_trace(capture_frame_pointers)
{
if (!capture_frame_pointers)
return;
Expand Down Expand Up @@ -542,6 +546,8 @@ ReturnType ThreadPoolImpl<Thread>::scheduleImpl(Job job, Priority priority, std:
metric_scheduled_jobs,
/// Tracing context on this thread is used as parent context for the sub-thread that runs the job
propagate_opentelemetry_tracing_context ? DB::OpenTelemetry::CurrentContext() : DB::OpenTelemetry::TracingContextOnThread(),
/// Not for pool worker threads (created without propagation), which outlive the scheduling scope
propagate_opentelemetry_tracing_context && DB::ThreadStatus::QueryCancellationBlocker::isActive(),
/// capture_frame_pointers
DB::Exception::enable_job_stack_trace,
std::move(available_threads_decrement));
Expand Down Expand Up @@ -1108,6 +1114,11 @@ void ThreadPoolImpl<Thread>::ThreadFromThreadPool::worker()
/// Set up tracing context for this thread by its parent context.
DB::OpenTelemetry::TracingContextHolder thread_trace_context("ThreadPool::worker()", job_data->thread_trace_context);

/// The job may attach to the thread group of the scheduling thread and inherit its cancellation predicates.
std::optional<DB::ThreadStatus::QueryCancellationBlocker> cancellation_blocker;
if (job_data->query_cancellation_blocked)
cancellation_blocker.emplace();

DB::Exception::enable_job_stack_trace = job_data->enable_job_stack_trace;
if (DB::Exception::enable_job_stack_trace)
DB::Exception::setThreadFramePointers(std::move(job_data->frame_pointers));
Expand Down
24 changes: 22 additions & 2 deletions src/Common/ThreadStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,29 @@ void ThreadStatus::flushUntrackedMemory()
memory_tracker.adjustWithUntrackedMemory(current_untracked_memory);
}

namespace
{
thread_local bool query_cancellation_blocked = false;
}

ThreadStatus::QueryCancellationBlocker::QueryCancellationBlocker()
: previous(std::exchange(query_cancellation_blocked, true))
{
}

ThreadStatus::QueryCancellationBlocker::~QueryCancellationBlocker()
{
query_cancellation_blocked = previous;
}

bool ThreadStatus::QueryCancellationBlocker::isActive()
{
return query_cancellation_blocked;
}

bool ThreadStatus::isQueryCanceled() const
{
if (!thread_group)
if (!thread_group || query_cancellation_blocked)
return false;

if (local_data.query_is_canceled_predicate)
Expand All @@ -279,7 +299,7 @@ bool ThreadStatus::isQueryCanceled() const

void ThreadStatus::throwIfQueryCanceled() const
{
if (!thread_group)
if (!thread_group || query_cancellation_blocked)
return;

if (local_data.throw_if_query_canceled_predicate)
Expand Down
Loading