Skip to content

CAS: pace part uploads and stop them on KILL QUERY - #2974

Draft
filimonov wants to merge 46 commits into
antalya-26.8from
work/antalya-26.8/cas-upload-guard
Draft

filimonov wants to merge 46 commits into
antalya-26.8from
work/antalya-26.8/cas-upload-guard

Conversation

@filimonov

@filimonov filimonov commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

CAS part uploads now obey the remote write bandwidth limits, and KILL QUERY or max_execution_time end an INSERT while its part is still uploading. Before, CAS wrote blob bodies with default WriteSettings: the limits did not reach them, and a stopped query waited for the upload in flight (265 s for a 740 MB part on a 2 MB/s link).

Built on the commits of #2970. The upload guard is the 5 commits from 19e24418802 to 49884b9ea37; the rest is that pull request, merged in again at 7e25d52e212 to take its fix of the retry order after a lost conditional write.

Related: #2970

Changes

  • Bandwidth. Each blob body and each S3 staging object carries the remote_throttler of the writer that produced it, also after a hardlink or a move of the staged file. max_remote_write_network_bandwidth, max_remote_write_network_bandwidth_for_server and the user network limits now pace these uploads.
  • Stop. For a part that is being built, the throttler is wrapped by a guard that throws the query's stop when the socket refills its send budget. The query ends with QUERY_WAS_CANCELLED or TIMEOUT_EXCEEDED; the multipart upload is aborted. With cas_staging_backend = s3 the write to the staging object is stopped the same way.
  • Not stopped. Uploads inside work that must finish (a write onto a committed part, transaction commit and rollback, cleanup after a failed commit) and uploads of a merge, a mutation, a background fetch or a background move. Part manifests, refs, the catalog, the mount lease, GC state and deletes are neither paced nor stopped.

Risks / notes

  • Behavior change after an upgrade: a server that already has one of these limits set starts to pace CAS uploads. A low max_remote_write_network_bandwidth_for_server makes merges and mutations hold their queue slots longer, and they are not stopped while they upload. docs/en/antalya/cas/configuration.md#write-bandwidth-limits states it.
  • A merge is not stopped because its thread group's cancellation predicate never throws on this base. Upstream made merges cancellable; CasOwner.cpp has a static_assert that fails on a base with that API, and a compile-time probe for ThreadStatus::QueryCancellationBlocker. Both are inert here and exist to fail or switch on when the base moves.
  • No time is promised for a stop. The guard is consulted when a request next asks the throttler, so a stop waits for the bytes already handed to the kernel: one stand run took 16.4 s on a 2 MB/s link, the others 1.0 to 2.5 s.
  • A stopped upload logs one <Error> line per cancelled multipart part task (TaskTracker::safeWaitAll, 19 to 26 lines for a 740 MB part). The same lines appear upstream; not changed here.
  • No code outside the CAS directory changes. No setting, no format change. The carrier of the throttler from the transaction to the backend is thread-scoped state (UploadStopScope, BodyGuardScope), chosen over six signature changes in the request engine.
  • The first write of a request on a reused keep-alive connection: Poco catches the stop, reconnects and writes again; the stop then surfaces from the second write. One extra connection per stopped request.

Testing

  • Unit gate CAS*:ReadBufferFromS3SessionRelease*:S3BulkDeleteFallback*:ReadBufferFromFileView* on this exact tree (7e25d52e212): release 5503 passed; ASan 5503 passed, no sanitizer report. 12 tests are new (CASUploadGuard), on an object storage fake that records the WriteSettings of each writeObject and copyObject.
  • Integration tests/integration/test_cas_request_window (28 tests, 7 new cases): passed in the default lane, in the "db disk" lane, and on an ASan server in the "db disk" lane. The new cases: a paced upload stopped in flight by KILL QUERY and by max_execution_time, with local and with S3 staging; a server-wide limit paces uploads while the mount keeps renewing; a killed implicit transaction rolls back during a paced upload; SIGTERM during an upload ends in an orderly shutdown.
  • The new integration cases fail without the change in two ways: on the base they stop at the progress gate because nothing paces the body; with the throttler and without the stop the query ends 100 to 113 s after the stop instead of within 20 s.
  • Stand, 2 MB/s rate-shaped link, 740 MB INSERT, no bandwidth limit set (the default configuration), on the build before the last review round, which did not change the blob or staging paths:
Measurement Local staging S3 staging Before this change
KILL QUERY ... SYNC 60 s in: time to the query's end 2.5 s (one run 16.4 s) 1.0 s 265 s
max_execution_time = 60: time after the deadline 2.2 s 2.5 s 265 s
docker stop 60 s in: time to exit, orderly 5.6 s 4.8 s not measured
10.6 MB INSERT at max_remote_write_network_bandwidth = 1000000 9.7 s 12.0 s 0.17 s
Two concurrent INSERTs at a 1 MB/s server limit 1.02 MB/s 0.99 MB/s unlimited
SYSTEM STOP MERGES during a merge's upload returns in 5 ms, merge completes returns in 6 ms, merge completes the same
fsck after all runs dangling=0 dangling=0
1 h combined soak with faults PASS PASS
  • Mount lease under a server-wide limit: renewals are not paced (the server's write throttler counter does not move while the mount renews idle). In the first stand session seven renewals of the 336-byte mount object took 3 to 16 s and each recovered on its first retry; one pair coincided with 503 answers of the object store. Twelve repeat runs of the same load (six through the stand's TCP proxy, six straight to the store, local and S3 staging) showed no slow renewal and a minimum time to expiry of 32.0 s. Not reproduced, not explained; the lease never came closer than 30.2 s to expiry.

Changelog category (leave one):

  • Improvement

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

Content-addressed storage: part uploads obey max_remote_write_network_bandwidth, max_remote_write_network_bandwidth_for_server and the user network limits, and KILL QUERY or max_execution_time end an INSERT while its part is uploading instead of waiting for the upload to finish.

Documentation entry for user-facing changes

  • Documentation is written: docs/en/antalya/cas/configuration.md (write bandwidth limits), docs/en/antalya/cas/operations/troubleshooting.md (stopping CAS work).

CI/CD Options

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • Aarch64 tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All Regression
  • Disable CI Cache

Regression jobs to run:

  • Fast suites (mostly <1h)
  • Aggregate Functions (2h)
  • Alter (1.5h)
  • Benchmark (30m)
  • CAS (content-addressed storage; Antalya only)
  • ClickHouse Keeper (1h)
  • Iceberg (2h)
  • LDAP (1h)
  • OAuth (5m)
  • Parquet (1.5h)
  • RBAC (1.5h)
  • SSL Server (1h)
  • S3 (2h)
  • S3 Export (2h)
  • Swarms (30m)
  • Tiered Storage (2h)

filimonov and others added 17 commits November 27, 2026 04:27
Each request binds `cas_request_window_ms` when it is sent; requests share no deadline.
`Retry::bind` takes the smallest of the window, an outside cap (a tool's own deadline) and
the lease, and records which one won in `Retry::Bound::Source`.
Decision loops end after `kCasMaxDecisions` (100) with `GaveUp::Why::Attempts`; a fence or
lease lost during the last request is reported instead of the count.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hot-key lane, catalog updates and erases, namespace life and drop, the blob publication
loop and the copy-source confirm poll no longer freeze one deadline for the whole loop.
Each request binds its own window. The loops end at count caps (100 decisions or
submissions, 8 blob publications); the confirm poll's deadline only decides whether
another pass starts.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`recovery_retry_budget_ms` is removed. The walk that fails at `kCasMaxRecoveryWalks` rethrows
its own error; a restart at the cap fails as "did not converge".

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No loop freezes a deadline any more. A tool's own deadline stays an outside cap
through `Retry::cappedAt`.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The owner is the query or a background owner (`OwnerScope::background`). `OwnerScope::owed`
marks work owed to a possibly-sent write or a failed commit; `OwnerScope::shared` marks work
done for several callers. `CasOwner.h` lists where work may end and the facts that classify
a failure. Adds the killable-query test fixture.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A query's stop is consulted only after a failed attempt: it vetoes the retry, and the call
ends with that attempt's error. A write attempt is classified first, then the loop picks
its next step; a sent write that stays unresolved ends as `NETWORK_ERROR` (210). Local faults surface unchanged, and a stopped owner's
exception is no store answer in the backend or the sentinel probe.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A catalog step decides its exit before the submission cap and the pause, by one rule.
A write that saw its query stop starts no further discretionary request. An earlier
unresolved write is reported at every non-commit exit, also from the catalog erase.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stopped follower withdraws its queued item, or waits for its result once a leader carved it.
A leader checks its owner before each flush and withdraws its own pending item.
An exceptional erase notifies waiters; a shared failure is rethrown as each thread's own copy.
The leader flushes in its caller's thread group under `OwnerScope::shared`: CAS's owner checks
do not reach shared work, but the S3 client still reports an unsuccessful answer as a killed
leader's stop.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only a walk that threw lets a stopped owner end recovery; the walk itself is owed.
A sent stamp or tombstone that stays unresolved keeps its retry-later exit after a stop,
and a query stop confirms real corruption before ending recovery. The backoff sleeps in
slices, and a stopped background owner leaves the wait for another caller's recovery.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stop before a blob publication ends the upload with the previous publication's error;
a stopped owner's failure is not retried, and a landed body stays debris. A refused
precommit owes no cleanup of its own and keeps an earlier call's duty. The commit compensation and the rename undo are owed. Upload
workers check they run in the submitter's thread group.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only the catalog's drop worker removes an `Atomic` table directory without a query, and
it retries after a restart, so shutdown may stop it. The removal lane stays closed after
any sent removal write, and its reopen proof is owed. Waits for the lane and for writer
cleanup duties use `waitStoppable`.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A round stopped by `SYSTEM CAS GC STOP` or by its query is recorded as `Stopped`.
A manual round checks its query at scan heads, and its read-ahead and delete workers run in
the round's thread group. Committed orphan nominations are deleted before the janitor
runs, and the post-commit phases are one owed scope.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decommission drain checks a killed query per page of the manifest listing and per
key. `runBounded` workers check their thread group before any request. Catches in the
tools leave a query's stored exception unannotated.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`s3_fault_proxy.py` moves to `helpers/s3_mocks` and can hold a request until the test
releases it. `test_cas_request_window` covers per-request windows, kills and query
timeouts end to end.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`test_shutdown_hands_atomic_drop_to_restart` listed the local
`metadata_dropped` directory. On the "db disk" lanes the database disk is
remote, the directory is empty, and the test failed although the server
stopped the removal as expected ("Cannot drop table ... Will retry later:
... CAS table namespace removal stopped by server shutdown"). The test now
checks that `DatabaseCatalog` logs no removal of the table's metadata
before the exit and logs it after the restart.

https://altinity-build-artifacts.s3.amazonaws.com/json.html?PR=2970&sha=d42bd3d271cdf293f9fec5bf61bfe4ea079ff6fc&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%208%2F8%29
#2970

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Workflow [PR], commit [b5edc12]

filimonov and others added 4 commits November 28, 2026 19:51
After a conditional write lost a race, the engine read the object, paused,
and then wrote on the body it had read before the pause. With other writers
committing more often than once per pause every retry was refused: a
`CREATE TABLE ... ON CLUSTER` lost 100 submissions of the ref catalog in
11 s and failed with "did not converge after 100 submissions".

A read and the write conditioned on it are now one probe with nothing
between them; pauses come only between probes:
- a write from the cached catalog that loses is decided again at once on
  the read that settled it;
- a write decided on a fresh read that loses is followed by a pause, a new
  read and a write. A clean 412 on that path is no longer settled by a read
  of its own, and a submission after a pause never starts from the cache;
- the pause after the n-th consecutive clean loss is
  min(750 ms, 40 ms * (n - 1)) plus up to a quarter: the first retry comes
  at once. A conflict that settled a transport fault keeps the fault
  schedule and restarts the count;
- `readModifyWrite` and `readModifyWriteOnPresence` share one loop;
- the catalog erase loop refreshes GC authority before its catalog read on
  a retry, and `resolveNamespaceLife` paces by its collisions.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stopped query whose catalog write lost a race ends at the next failed
read. On the cached path that read settles the refused write and the query
ends with its conflict; on the fresh path it is the read before the next
attempt and the query ends with its own stop. The test runs both in a
fixed order; no further `PUT` follows in either.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
filimonov and others added 7 commits November 29, 2026 21:48
CAS blob bodies and S3 staging objects were written with default
`WriteSettings`, so the remote write bandwidth limits did not reach them,
and `KILL QUERY` or `max_execution_time` waited for an upload in flight
to finish. Each body now carries the `remote_throttler` of the writer
that produced it. For a part that is being built the throttler is
wrapped by a guard that throws the query's stop when the socket refills
its send budget. Part manifests and other control requests are not
touched. A merge or a mutation is paced and not stopped: its thread
group's cancellation predicate never throws on this base.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The upload guard tests need the fake that `gtest_cas_s3_staging.cpp`
defines. It moves to `cas_fake_generation_object_storage.h` and now
records the `WriteSettings` of each `writeObject` and `copyObject`
and can call a recorded throttler while a body is written.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Four tests in `test_cas_request_window`: a paced upload is stopped in
flight by `KILL QUERY` and by `max_execution_time` with local and with
S3 staging; a server-wide limit paces uploads while the mount keeps
renewing; a killed implicit transaction rolls back during a paced
upload; SIGTERM during an upload ends in an orderly shutdown.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Red on purpose: removeRecursive on a part directory leaves the transaction's staged files, so the commit that follows publishes them.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…taged

A stopped byte fetch removes the half-downloaded part with a recursive
removal and then commits the same transaction. The recursive removal dropped
the ref but kept the staged files, so the commit published the part that was
just removed. `removeDirectory` already discarded the staging; both now share
`discardStagingOf`.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
filimonov and others added 18 commits November 29, 2026 21:48
Adds the pausable failpoint `replicated_fetch_pause_after_projections` and a test
that stops a paused byte fetch of a part with a projection, on local and S3 staging.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…merges

Cancel in-flight merges and mutations on server shutdown

(cherry picked from commit 691db87)

Port notes: one conflict, in `src/Storages/MergeTree/DataPartsExchange.cpp`
`Fetcher::fetchSelectedPart`, where upstream registers the fetch entry and this
fork has the CAS fetch-by-relink block. The registration and the per-entry
`ReplicatedFetchReadCallback` now come first, right after the common header,
and the relink block follows. Everything in the relink block is kept as is. Its
`fall_back_to_byte_fetch` lambda additionally calls `entry.reset()` before it
releases the directory lock, as upstream does before the zero-copy retry. The
separate confirmation request in `relinkPartToDisk` keeps its own buffer.
No hunk of upstream is left out. The test files are taken unchanged.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…own-drains-pending-tasks

Release the tasks left in a MergeTree background executor's queue on shutdown

(cherry picked from commit d59f7af)

Port notes: clean pick.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…-cancelall

fix: OOM-canary, stop only current merges
(cherry picked from commit 589d92c)

Port notes: clean pick.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
`afterCommit` and `rollback` are `noexcept`. With the merge thread group now having a throwing cancellation predicate, a cancelled merge could throw `ABORTED` from a read in `afterCommit` and terminate the server. Block cancellation checks in the transaction finalization and rollback paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 66ab027)

Port notes: three conflicts. `src/Common/ThreadStatus.cpp`: upstream's context
carries `publishUntrackedMemory`, which this base lacks; only the blocker
definition is taken. `src/Interpreters/MergeTreeTransaction.cpp`: this base's
`rollback` has no commit gate; the blocker is added in front of this base's
`csn` exchange and nothing else of that hunk is taken. `src/Interpreters/TransactionManager.cpp`
does not exist here: its two guards go into `TransactionLog::finalizeCommittedTransaction`
and `TransactionLog::rollbackTransaction` (not `commitTransaction`, whose precommit
stays cancellable), with the `ThreadStatus.h` include. `CasOwner.cpp`: the
compile-time probe for the blocker and its use in `uploadStopCheck` are deleted.
No other hunk of upstream is left out.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…d write thread

Sync from private: the background thread now creates its own blocker when the caller had one, so a cancelled merge cannot throw from `MergeTreeTransaction::afterCommit`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c3d8033)

Port notes: clean pick. Added in the same commit: `uploadStopCheck` in
`CasOwner.cpp` returns an empty stop while the calling thread holds the
blocker, and two tests in `gtest_cas_upload_guard.cpp`
(`StopIsEmptyUnderACancellationBlocker`, `APoolJobInheritsTheBlockerOnlyForItself`).

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
When a mutation is killed while its part is being committed, the commit fails and
`MergeTreeData::Transaction::rollback` runs in the thread of the mutation, under its query
cancellation predicates. Reading the version metadata in `setAndStoreCreationCSN` then threw
`Cancelled mutating parts` (in ClickHouse Cloud, from the distributed cache read buffer), so the
rolled back part was left Outdated without `RolledBackCSN`. Such a part is not skipped by
`getMaxLevelMutationInBetween`, and merges of its neighbours (including `OPTIMIZE ... FINAL`) are
refused with "There is an outdated part in a gap between two active parts" until it is removed.

This made `01168_mutations_isolation` flaky: `OPTIMIZE TABLE mt FINAL` in tx 7 did nothing, so the
mutation of tx 10 did not get the expected serialization error, and the mutation of tx 7 failed instead.

Use `ThreadStatus::QueryCancellationBlocker` during the rollback, as `MergeTreeTransaction::rollback` does.

Related: https://github.com/ClickHouse/clickhouse-private/issues/13092
Example failure: https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/praktika.html?PR=82719&sha=b87fa9bb1fa635e3ef67043098f0931d959a45ca&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20cache%2C%20meta%20in%20keeper%2C%20s3%20storage%2C%20parallel%29
PR: https://github.com/ClickHouse/clickhouse-private/pull/82719

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

(cherry picked from commit 85c0301)

Port notes: clean pick.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Hold `ThreadStatus::QueryCancellationBlocker` in `WriteBufferFromS3::abortMultipartUpload`,
the multipart abort of `copyS3File`, `DiskObjectStorageTransaction::undo` and
`IMergeTreeDataPart::removeIfNeeded`.

(cherry picked from commit 16d34d5)

Port notes: one conflict in `src/IO/S3/copyS3File.cpp`, include block. This base has no
`Common/getRandomASCIIString.h` include there, so only the `Common/ThreadStatus.h` include is added. The separate
test commit `fdda364d55b` is not taken.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… mutation is cancelled""

(cherry picked from commit 9e118a6)

Port notes: clean pick of the product and test files. One CAS file changed in
the same commit: `CasOwner.cpp` loses the `static_assert` that
`ThreadGroup::setQueryCancellationPredicates` does not exist, together with
the `kGroupHasCancellationSetter` probe it used.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ancellation-not-drop

Fix flaky test `05294_drop_table_cancels_vector_index_merge`

(cherry picked from commit 8802b18)

Port notes: clean pick, first parent diff (`-m 1`); test file only.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ation-cancellation-predicate

Make background merges and mutations observe cancellation in the S3 retry loop

(cherry picked from commit 356ae39)

Port notes:
- `src/IO/ReadBufferFromS3.cpp`: the textually clean pick put the new throwing
  `CurrentThread::checkIfNotCancelled` after this fork's early return on
  `isQueryCanceled`. Moved it before that return, so a cancelled task gets the
  cancellation exception; the Boolean check stays (exports use it).
- `tests/integration/helpers/s3_mocks/broken_s3.py`: conflict in
  `_ServerRuntime.__init__`. Kept the base; added only `at_object_get`. Upstream's
  `at_complete_multi_part_upload` initialisation is not taken: the base mock has
  no such state. The `MockControl` method, the reset line and the GET dispatch
  hunk are as upstream.
- No hunk left out; the ported tests run here without fixture repair
  (`test_merge_cancel_s3_read`, `04826_kill_mutation_cancels_s3_read`).
- Not upstream, same commit: `CasOwner.h` and `CasOwner.cpp` comments of
  `uploadStopCheck` (a merge's group can now throw; the blocker is honoured at
  capture), and `gtest_cas_upload_guard.cpp` (renamed the no-predicate merge test,
  added `AGroupWithATaskPredicateThrowsThroughTheGuard`).

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Attaching a thread group with a query context applies the query settings to
the thread, including the profilers, which the flush did not have before. The
profile of a `trace_log` flush would be written to `trace_log` itself. The new
`ThreadGroup::createWithoutQueryContext` gives the flush only the memory
tracker of the group (with the overcommit wait time), so no query settings
apply to it, and the query context of the thread stays empty as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 703cb2a)

Port notes: taken in part, only the root constructor and `ThreadGroup::createWithoutQueryContext` (`ThreadStatus.h`, `ThreadStatusExt.cpp`). Left out: the `SystemLog.cpp` and `test_overcommit_tracker` hunks (conflict here, not needed). The upstream context hunks assumed `createWithoutQueryMemoryTracker`, `isNested` and a two-argument child constructor, which this base lacks; the existing one-argument child constructor is kept unchanged.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…a query context

Pool threads, including the system log saving threads, start the global
CPU and real time profilers once, in the same `ThreadStatus` slots as the
query profilers. Detaching from a thread group always destroyed them, and
attaching a group without a query context does not recreate them, so each
saving thread lost global profiling after its first flush. Such a group does
not set up query profilers, so detaching it now keeps the profilers of the
thread. Nested groups inherit this from their parent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 030903f)

Port notes: one conflict, in `ThreadStatus::detachFromGroup`. Upstream wraps the finalization in `if (boundToOSThread())`, which this base lacks; the base's unconditional `finalizeQueryProfiler` and `finalizePerformanceCounters` are kept and only `finalizeQueryProfiler` is made conditional on `thread_group->with_query_context`. The flag is propagated by the one-argument child constructor; the constructor taking a query context keeps the default `true`. Also restores the `memory_tracker` initializer of the root constructor as upstream master has it (no peak-memory log line when such a group is destroyed), which the partial port of `703cb2a994d` did not carry.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A thread attached to such a group, or to a child of it, gets its original
group and name back on detach, and the child reports `with_query_context`
as `false`.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A merge can be cancelled at any depth: its list entry owns a thread group,
the task runs on it, and the group carries cancellation predicates. A
replicated fetch and a part move had no thread group. Their cancellation
checks sit on the read side (a fetch: each chunk received and between files;
a move: each copied buffer and before the swap), so a write that blocks
after the last of them ran to its end: a request to object storage that
keeps retrying held `SYSTEM STOP FETCHES`, `SYSTEM STOP MOVES` and a server
shutdown for the whole retry budget.

Give both operations what a merge has.

- `ThreadGroup::createForBackgroundOperation` makes a group without a query
  context, or a child of the caller's group when there is one, so a query's
  limits and its kill still apply. It is not `createForMergeMutate`: the
  memory of a fetch or a move must not count against merges and mutations.
- `installCancellationPredicates` composes the group's inherited predicates
  with the operation's own stop: a shared latch and copies of the
  `ActionBlocker`s. The predicates own what they add, because threads of a
  writer pool can call them after the list entry is gone.
- A fetch runs under its group from the registration in
  `system.replicated_fetches` through the commit of the downloaded part. Its
  predicates read the entry's flag and the fetcher's blocker.
- A move runs the clone under its group. The copy hook and the check before
  the swap call the group's predicate. `MovesList::cancelAll` is a blocker
  cancelled for good that every move reads; `Context::shutdown` calls it next
  to the cancellation of merges and fetches, so a move no longer holds a
  shutdown.
- Cleanup after a stop runs under a `QueryCancellationBlocker`, so its
  requests keep their retries: the removal of a stopped fetch's directory,
  the removal of a failed clone, and the short stretch between publishing a
  fetched or cloned part and arming its removal. A fetch stopped through a
  query (`QUERY_WAS_CANCELLED`) is cleaned up like one stopped with
  `ABORTED`, and a failure of that cleanup is logged and does not replace the
  cancellation.

Behavior:

| Signal | Fetch | Move |
|---|---|---|
| `SYSTEM STOP FETCHES` / `SYSTEM STOP MOVES`, `DROP TABLE`, `DETACH TABLE` | the existing read-side checks, and now a retry of a write request | the copy hook and the check before the swap, and now a retry of a write request |
| server shutdown | the entry's flag | `MovesList::cancelAll` |
| `KILL QUERY` on `ALTER ... MOVE` or `FETCH PARTITION` | unchanged: the child group calls the query's predicate first | unchanged, and it now reaches the copy hook and the swap check |

Not covered:

- A body that is being sent with no retry is not polled.
- A byte fetch stopped after it wrote objects to an object storage disk
  leaves those objects: the handler removes the metadata and commits.
- The repeated Keeper wait of a zero-copy move reads none of the new
  predicates, and a read that is already blocked in an HTTP refill is not
  interrupted. Either can still hold a shutdown.
- A stop that coincides with a failed request in the stretch after
  publication can still leave a `tmp-fetch_` or `moving/` directory.
- `system.part_log` rows of `DownloadPart` and `MovePart` do not gain the
  counters of the worker threads.

A query-run fetch or move shows more rows in `system.query_thread_log` under
the same query id, because the thread attaches to the child group.

Tests: `gtest_background_operation_cancellation.cpp`, and
`test_fetch_move_cancellation_s3`: a fetch and a move onto an S3 disk whose
writes answer 503 are stopped by the statement, by SIGTERM and by `KILL
QUERY`, and a failed first request of the clone cleanup is retried.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Relink preparation reports a cancellation as "relink not possible", and the
fetch then started a byte fetch of the same part. Before that fallback the
fetch now asks its thread group's cancellation predicate, so a stop it
inherited (a killed `FETCH PARTITION`, a fetch inside a merge task) or
latched ends the fetch.

The stretch from the committed relink promote until the fetched part owns
its directory runs under a `QueryCancellationBlocker`, as the byte fetch
does after its commit.

`uploadStopCheck` reads the blocker when the stop is captured, on the
publisher's thread; its comment now says so.

Test seams: failpoint `cas_relink_receiver_cancel_fetches_in_preparation`;
`replicated_fetch_pause_after_projections` moves to the generic failpoints.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`s3_fault_proxy.py` gains a mode that paces one upload body and records how
it ended, a mode that holds a `GET` after its headers, and one that fails a
single multipart abort. A case waits until the body of the target is in
flight, issues the signal, lifts the pace and requires that the sender
closed the request early with fewer bytes than `Content-Length`. A server
that does not cut the upload completes the body, and the case fails.

`test_cas_request_window`:
- `SYSTEM STOP MERGES`, `KILL MUTATION`, `DROP TABLE`, `DETACH TABLE` and
  SIGTERM during the upload of a merge or a mutation;
- `SYSTEM STOP FETCHES`, `SYSTEM STOP MOVES`, `DROP TABLE` and SIGTERM during
  the upload of a part fetched by bytes or moved from a local disk;
- a multipart abort that fails once is retried; a merge stopped while a
  read fails reports the cancellation, not the read error;
- controls with no signal, and self-tests of the proxy modes.

The default set, which CI runs, has one or two variants per signal and adds
about five minutes to the module. `CAS_CUT_FULL_MATRIX=1` runs all listed
variants.

`test_cas_replicated_relink`: a cancelled relink does not fall back to a byte
fetch; the stopped-byte-fetch test compares a digest with the source.

On a CAS disk a temporary fetch or move entry is not listed before its
commit, so the "nothing left" checks show that no temporary part was
published.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`SYSTEM STOP MERGES`, `STOP FETCHES`, `STOP MOVES`, `KILL MUTATION`,
`DROP TABLE`, `DETACH TABLE` and a server shutdown now end the upload of a
merge, a mutation, a fetched part and a moved part. The troubleshooting page
lists what stops, what does not, what a stop leaves behind and what an
operator sees in the logs and system tables.

Signed-off-by: Mikhail Filimonov <mfilimonov@altinity.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@filimonov
filimonov force-pushed the work/antalya-26.8/cas-upload-guard branch from 7e25d52 to b5edc12 Compare November 29, 2026 21:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants