Skip to content

Install CAS decommission test callbacks before workers start - #2801

Merged
filimonov merged 3 commits into
antalya-26.6from
fix/cas-test-callback-locks
Nov 10, 2026
Merged

filimonov merged 3 commits into
antalya-26.6from
fix/cas-test-callback-locks

Conversation

@k-morozov

@k-morozov k-morozov commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

TSan reported a data race in CASDecommission.FenceLossOnAListPageStillRunsTheConfirm: decommissionPoolMember installed the test clock and sleep callbacks on the admin pool after Pool::openForDecommission had started the lease renewer, which reads the same std::function objects.

Install the callbacks before any worker thread exists instead of guarding them with locks. The request planes already take boot_ms_fn and retry_sleep_fn from PoolConfig in the Pool constructor, and decommissionPoolMember fills those fields from the drain callbacks. The ref-recovery sleep was the only seam reached solely through the post-open setter, so openForDecommission now sets it from config.retry_sleep_fn before mountWritable starts the renewer. The post-open setter block is removed.

Changes

  • Pool::openForDecommission: set the ref-recovery retry sleep from config.retry_sleep_fn before mountWritable.
  • decommissionPoolMember: drop the post-open setCasRequestNowFnForTest / setCasRetrySleepForTest block.
  • gtest_cas_gc_teardown_stop.cpp: ASSERT_EQ on the first pause, so the sleep setters never run while that pause may still read the callback.

Risks / notes

  • Test-only path. SYSTEM CAS DROP POOL MEMBER passes no drain callbacks, and production never sets PoolConfig::boot_ms_fn / retry_sleep_fn, so the new block is skipped there.
  • antalya-26.8 has the same post-open install in Tools/CasDecommission.cpp; the code has diverged, so it needs its own patch.

Testing

Closes: #2798

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 to CHANGELOG.md):

...

Documentation entry for user-facing changes

...

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)

ThreadSanitizer reported a data race in
`CASDecommission.FenceLossOnAListPageStillRunsTheConfirm`. The test installs
clock callbacks after `Pool::openForDecommission` has started the lease
renewer. The main thread assigns the clock's `std::function` while the
renewer calls the same object from `CasOperation::replace`. Owning the
captured clock keeps that clock alive, but does not make replacement of
the enclosing `std::function` safe.

Protect the clock and request-sleep callbacks in `CasRequests` with
separate `SharedMutex` instances. Callback calls take `SharedLockGuard`,
and test setters take `UniqueLock`. Hold shared ownership until the
callback returns, including exception unwinding, so a setter cannot
replace or destroy a callable that another thread is still using.
Separate mutexes allow a sleep callback to issue a request that reads
the clock without acquiring the same mutex again.

Keep the existing `now_ms` and `sleep_ms` call expressions. These names
now refer to private methods that acquire the guards, while the stored
callbacks become `now_ms_fn` and `sleep_ms_fn`. Existing request loops,
hot-key handling, and `pause` therefore use the protected accessors
without changes to their call sites or retry logic.

Apply the same protection to `CasRefLedger::recovery_retry_sleep_fn`.
Both `setCasRetrySleepForTest` and `setRefRecoveryRetrySleepForTest`
replace this callback under exclusive ownership. Recovery holds shared
ownership during backoff, after releasing the table's `state_mutex`.
The callback guard is released before the exception path reacquires
`state_mutex`, preserving the existing recovery cleanup order.

Use the project's TSA-aware lock wrappers and mark all three callback
fields with `TSA_GUARDED_BY`. Document that the mutexes protect callback
replacement by tests while background work is running. Shared ownership
still allows concurrent callback calls; captured mutable state must
remain thread-safe.

Fix the shared callback APIs rather than only the failing decommission
call site. This also covers other tests that replace callbacks on an
already open pool, while preserving the existing decommission callback
selection and clock precedence.

CI: https://github.com/Altinity/ClickHouse/actions/runs/41911689821
Related: #2798

Validation: `git diff --check` passed. Source review covered callback
accesses, recovery exception cleanup, and existing reentrant test
callbacks. Builds, tests, and a TSan rerun were not run as requested.

Signed-off-by: Konstantin Morozov <[email protected]>
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Workflow [PR], commit [3963b4f]

Calling the stored `std::function` does not change the `CasRequests`
object. The callback mutex is already `mutable`, so shared locking works
in a const method while still protecting callback replacement.

Keep the callback storage non-mutable and retain the existing lock scope.

CI: https://github.com/Altinity/ClickHouse/actions/runs/41911689821

Validation: `git diff --check` passed. Builds and tests were not run as
requested.

Signed-off-by: Konstantin Morozov <[email protected]>
The original TSan race in
`CASDecommission.FenceLossOnAListPageStillRunsTheConfirm` came from
replacing an admin pool's clock callback after
`Pool::openForDecommission` had already started its lease renewer.
The previous repair protected callback replacement and invocation with
shared mutexes, adding locking to ordinary production requests as well.

Use initialization order to remove that overlap. All three request
planes already receive `PoolConfig::boot_ms_fn` and
`PoolConfig::retry_sleep_fn` in the `Pool` constructor. Initialize the
ref recovery retry callback from the same configuration before
`mountWritable` can start workers, and remove the post-open admin
setters from `decommissionPoolMember`.

Remove the callback mutexes and their guards from `CasRequests` and
`CasRefLedger`. Restore direct `std::function` calls under the existing
`now_ms` and `sleep_ms` names. Callback setters require setup without
concurrent callback use; they no longer support replacement during
active work. Captured mutable state still needs its own synchronization.

Keep the local `control_requests` setup: its callbacks are installed
before the first `admit`, and the admin workers use separate engines.
The admin planes now use the configured callbacks. Explicit drain
callbacks fill empty configuration fields and still directly configure
the local control engine. No existing caller supplies different
callbacks through both channels.

Make the first async completion check fatal in
`CASGCTeardownStop.OpenPlaneSleepReturnsAtOnceOnceTeardownBegan`.
After a timeout, the test must not continue replacing a sleep callback
while the earlier async `pause` may still be running.

Validation: `git diff --check` passed. Independent source reviews
covered setter callers, worker startup, detached snapshot and recovery
tasks, callback ownership, and timeout paths. Builds, tests, and a TSan
rerun were not run as requested.

CI: https://github.com/Altinity/ClickHouse/actions/runs/41911689821
Related: #2798
Signed-off-by: Konstantin Morozov <[email protected]>
@filimonov filimonov changed the title Synchronize CAS test clock and retry callbacks Install CAS decommission test callbacks before workers start Nov 10, 2026
@filimonov
filimonov marked this pull request as ready for review November 10, 2026 22:34

@filimonov filimonov 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.

Code is ok, only the comments need some improvements.

Not for this PR, a follow-up: the comments around this path now say the wrong thing.

  • Tools/CasDecommission.h:67-70 says the drain fns "replace" the admin pool's clock and sleep. They now only fill PoolConfig::boot_ms_fn / retry_sleep_fn when unset; only the control engine gets them unconditionally. No caller passes both today, but the doc is wrong.
  • Pool/CasPool.cpp:950-957 says the worker pair never runs in unit tests. openForDecommission forces background_watermark, so decommission tests do run it. That is why this race looked impossible.
  • Pool/CasPool.h setter docs (setCasRequestNowFnForTest and friends) should say "before workers start; openForDecommission starts them". The fix relies on that rule.
  • openForDecommission could call the existing adapter ref_ledger.setCasRetrySleepForTest(config.retry_sleep_fn) (CasRefLedger.cpp:269) instead of the 8-line lambda, with a one-line note that it must precede mountWritable.
  • the CasRequests.h hunk deletes the only sentence that states the rule ("test setters ... belong to setup, before any operation runs"). That sentence is what this fix depends on

@filimonov
filimonov merged commit 943249e into antalya-26.6 Nov 10, 2026
374 of 392 checks passed
filimonov added a commit that referenced this pull request Nov 10, 2026
Install CAS decommission test callbacks before workers start

(cherry picked from commit 943249e)
Signed-off-by: Mikhail Filimonov <[email protected]>
filimonov added a commit that referenced this pull request Nov 10, 2026
Follow-up to #2801 (review
#2801 (review)):

- `decommissionPoolMember` doc: the drain fns fill `PoolConfig::boot_ms_fn` /
  `retry_sleep_fn` when unset and reach the pool through its config, before
  its renewer starts; only the control engine takes them unconditionally.
- `mountWritable` comment: `openForDecommission` forces `background_watermark`,
  so the decommission unit tests do run the worker pair.
- `Pool` test setter docs: call before the background workers start;
  `openForDecommission` starts them inside the open.
- `openForDecommission`: reuse `CasRefLedger::setCasRetrySleepForTest`
  instead of an inline adapter lambda.
- `CasRequests` doc: restore the sentence that the test setters write members
  and belong to setup.

`CAS*` gtest gate (ASan): 2909 tests passed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Mikhail Filimonov <[email protected]>
filimonov added a commit that referenced this pull request Nov 11, 2026
Install CAS decommission test callbacks before workers start

(cherry picked from commit 943249e)
Signed-off-by: Mikhail Filimonov <[email protected]>
filimonov added a commit that referenced this pull request Nov 11, 2026
Follow-up to #2801 (review
#2801 (review)):

- `decommissionPoolMember` doc: the drain fns fill `PoolConfig::boot_ms_fn` /
  `retry_sleep_fn` when unset and reach the pool through its config, before
  its renewer starts; only the control engine takes them unconditionally.
- `mountWritable` comment: `openForDecommission` forces `background_watermark`,
  so the decommission unit tests do run the worker pair.
- `Pool` test setter docs: call before the background workers start;
  `openForDecommission` starts them inside the open.
- `openForDecommission`: reuse `CasRefLedger::setCasRetrySleepForTest`
  instead of an inline adapter lambda.
- `CasRequests` doc: restore the sentence that the test setters write members
  and belong to setup.

`CAS*` gtest gate (ASan): 2909 tests passed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Mikhail Filimonov <[email protected]>
filimonov added a commit to filimonov/ClickHouse that referenced this pull request Nov 11, 2026
Altinity#2801 drops the post-open test
clock setters in decommissionPoolMember; its CI reds are tracked elsewhere
(CAS-430, CAS-440, CAS-340) or base-level.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Mikhail Filimonov <[email protected]>
@filimonov

Copy link
Copy Markdown
Member

Ported to antalya-26.8 in #2805 (commit c840f70). Clean cherry-pick.

The port also applies the review #2801 (review) in 2f89c5d: comment and doc fixes around the decommission clock and sleep seams, and openForDecommission reuses CasRefLedger::setCasRetrySleepForTest. antalya-26.6 does not have that commit.

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.

TSan data race: CASDecommission.FenceLossOnAListPageStillRunsTheConfirm replaces the request clock under the lease renewer

2 participants