Skip to content

Wait for DETACH DATABASE to release tables in three integration tests - #114628

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/detach-database-sync-integration-tests
Aug 13, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/detach-database-sync-integration-tests

Conversation

@groeneai

@groeneai groeneai commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator

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):

...

Description

Related: #93064

Three integration tests intermittently fail with Code: 219 ... Database <db> cannot be detached, because some tables are still in use. Retry later. thrown from
DatabaseAtomic::assertCanBeDetached (src/Databases/DatabaseAtomic.cpp:521).

Root cause: non-SYNC DETACH DATABASE is best-effort by contract, and these tests assume it is
atomic. assertCanBeDetached throws if any table of the database still has a live refcount. The
engine already has the deterministic wait, waitDetachedTableNotInUse, but
executeToDatabaseImpl calls it only under query.sync
(src/Interpreters/InterpreterDropQuery.cpp:779-790). Stateless CI installs
tests/config/users.d/database_atomic_drop_detach_sync.xml
(database_atomic_wait_for_drop_and_detach_synchronously=1); the integration helpers install no
such profile and run at the server default of 0. Hence the failures are confined to the
integration suite, and 16 of the 17 integration occurrences in 180 days are sanitizer builds,
where the holder's window is wider.

Code 219 is intended behaviour of the asynchronous form and is pinned in-tree:
01107_atomic_db_detach_attach.sh:19 sets the setting to 0 to provoke it and asserts it. So
this is a test-side defect, and the change asks for the wait the engine already implements
rather than altering it.

I added SYNC to the five DETACH DATABASE statements with observed CI failures:
test_drop_replica (11 hits/180d), test_replicated_table_structure_alter (4), and
test_drop_database_replica:192 (2, both stacks confirm that line is the thrower). All 21
non-SYNC sites under tests/integration/ were enumerated; the other 16 are excluded because
they have zero CIDB hits in 180 days, already pass
database_atomic_wait_for_drop_and_detach_synchronously, or sit inside the existing
detach_database_with_retry helper, whose docstring documents a different holder.

Validation: with a holder injected the way 01107 does it, the DETACH returns 219 without
SYNC and succeeds with it, 10/10 on Atomic and on Replicated. The three tests pass 42/42
runs. Each DETACH in test_drop_replica measures ~0.115 s over 25 measurements, so the wait
costs nothing when no table is held, and it is cancellable and shutdown-aware.

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

Version info

  • Merged into: 26.8.1.1356 (included in 26.8 and later)
  • Backported to: 26.7.11.17, 26.6.9.17, 26.3.33.106

Three integration tests intermittently fail with Code 219 "Database <db> cannot be
detached, because some tables are still in use. Retry later." from
DatabaseAtomic::assertCanBeDetached (src/Databases/DatabaseAtomic.cpp:521).

Non-SYNC DETACH DATABASE is best-effort by contract: assertCanBeDetached throws if
any table of the database still has a live refcount. The engine already implements
the deterministic wait, waitDetachedTableNotInUse, but executeToDatabaseImpl calls
it only under query.sync (src/Interpreters/InterpreterDropQuery.cpp:779-790).
Stateless CI installs tests/config/users.d/database_atomic_drop_detach_sync.xml,
which sets database_atomic_wait_for_drop_and_detach_synchronously=1, so stateless
tests get the wait for free. The integration helpers install no such profile and
run at the server default of 0, which is why the failures are confined to the
integration suite and why 17 of 18 occurrences in 180 days are sanitizer builds,
where the holder's window is wider.

Code 219 is intended behaviour of the asynchronous form and is pinned in-tree by
01107_atomic_db_detach_attach.sh:19, which sets the setting to 0 to provoke it and
asserts it. So the defect is on the test side: the tests use the asynchronous form
while asserting synchronous semantics. Adding SYNC asks for the wait the engine
already implements instead of changing engine behaviour.

Fixed the five statements with observed CI failures: test_drop_replica (11 hits in
180 days), test_replicated_table_structure_alter (4 hits), and
test_drop_database_replica:192 (2 hits, both stacks confirm that line throws, so
:94 inside the existing detach_database_with_retry helper is left alone; its
docstring documents a different holder). All 21 non-SYNC sites under
tests/integration/ were enumerated; the remaining 16 either have zero failures in
180 days or already pass the setting explicitly.

Validation: with a holder injected the way 01107 does it, the DETACH returns 219
without SYNC and succeeds with it, 10/10 on Atomic and on Replicated. The three
tests pass 42 of 42 runs. Each DETACH in test_drop_replica measures about 0.115 s
over 25 measurements, so the wait costs nothing when no table is held, and it is
cancellable per iteration and shutdown-aware.

Related: ClickHouse#93064
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

An independent model reviewed this change cold against the code and the PR contract. Findings
and my adjudication:

# Severity Finding Verdict
⚠️ major test-liveness: the three modified tests do not deterministically prove that the added SYNC executes the wait, so a regression in which SYNC stops triggering it would leave ordinary runs green. Disagree. 04099_detach_database_sync_cancellation.sh already pins exactly that, and this PR does not touch it: it holds a StoragePtr with a 60 s sleepEachRow SELECT (:38-40), issues DETACH DATABASE ... SYNC using the keyword rather than the setting (:58), and asserts via system.processes that the DETACH is present and still running (:61-70, explicit failure if absent) before killing it and requiring cancellation. If SYNC stopped reaching InterpreterDropQuery.cpp:780, that DETACH would fall through to assertCanBeDetached at :801, throw 219 in about 0.1 s, and stop being a long-lived killable query, so 04099 reddens. 01107_atomic_db_detach_attach.sh:19 pins the opposite direction (setting 0 must still produce 219). Its detection is racy at the 0.1 s poll granularity rather than fully deterministic, so strengthening it is 04099's business rather than this PR's. Injecting a slow holder and a blocking oracle into three unrelated integration tests would change what each of them tests and add timing-based assertions; the in-tree precedent for this remedy carries no such oracle (test_store_cleanup/test.py:49,96 pass the setting bare, and six stateless tests write DETACH DATABASE ... SYNC bare). Happy to add coverage if a maintainer prefers it.
💡 nit The related-issue link sat only inside the HTML comment, which GitHub does not render, and then used the shorthand #93064 rather than the full URL the template asks for. Agree, fixed. The description now carries the full URL on its own line.
💡 nit "17 of 18 occurrences in 180 days are sanitizer builds" was not reproducible: the 18 only reaches 18 by including 5 stateless 01107 rows, which are one flaky-check event on a test that asserts Code 219, contradicting the same sentence's "confined to the integration suite". Agree, fixed. Re-measured: 17 integration occurrences, 16 of them sanitizer builds, 1 arm_binary, distributed plan. The description now says that.

Gate B was re-run after the description changed, so the reviewed contract is the published one.

Session id: cron:clickhouse-review-slot-9:20260813-112300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With a sleepEachRow INSERT holding a StoragePtr, DETACH DATABASE <db> returns Code: 219 on 5/5 attempts on Atomic and 1/1 on Replicated, in ~0.09 s. No race is chased; the holder is injected the way 01107_atomic_db_detach_attach.sh does it.
b Root cause explained? Yes. Non-SYNC DETACH DATABASE is best-effort: assertCanBeDetached (DatabaseAtomic.cpp:521) throws if any table still has a live refcount. The wait waitDetachedTableNotInUse (:805) exists but executeToDatabaseImpl calls it only under query.sync (InterpreterDropQuery.cpp:779-790). Stateless CI installs database_atomic_drop_detach_sync.xml (setting=1); integration helpers install no such profile and run at the default 0.
c Fix matches root cause? Yes. The tests used the asynchronous form while asserting synchronous semantics, so they now request the synchronous form. No widened bound, no reduced dataset, no no-parallel/no-random-* tag, no retry deadline, no engine-side guard. Code 219 stays reachable and stays asserted by 01107:19.
d Test intent preserved / new tests added? Yes. SYNC changes only when the statement returns, never what is asserted afterwards: no assertion added, removed or weakened. No new test is owed (test-only determinism fix, no source change); the A/B demonstration is the proof.
e Both directions demonstrated? Yes. Without SYNC: Code 219, 5/5. With SYNC: success, 5/5. Same on a Replicated database. Additionally the waitDetachedTableNotInUse DEBUG lines appear 4x with SYNC and 0x without it (negative control), proving the modifier reaches query.sync and the change is not a no-op.
f Fix is general across code paths? Yes. All 21 non-SYNC DETACH DATABASE sites under tests/integration/ enumerated with a disposition each: 5 fixed (the ones with CIDB failures), 3 already correct or inside the documented retry helper, 13 with zero hits in 180 days left out of scope. DatabaseReplicated : public DatabaseAtomic overrides neither the throw nor the wait (grep: 0 hits), verified end-to-end.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes on the axis that exists: database engine. Verified on Atomic and on Replicated. Type wrappers, settings matrices and numeric boundaries do not apply to a test-only DDL-modifier change.
h Backward compatible? Yes, vacuously. No setting default changed, so no SettingsChangesHistory.cpp entry; no format or serialization change; no production code touched. SYNC on DETACH DATABASE is long-established syntax (ParserDropQuery.cpp:147-162) and six stateless tests already use it.
i Invariants and contracts preserved? Yes. No engine invariant is altered; the change selects an existing documented path. The wait is cancellable per iteration (throwIfKilled) and shutdown-aware, so the worst case is a visible timeout, not a hang. Measured cost when nothing is held: ~0.115 s per DETACH over 25 measurements.

Session id: cron:clickhouse-impl-slot-5:20260813-103200

@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [979703c]

Summary: ✅


AI Review

Summary

This PR makes the touched integration tests use DETACH DATABASE ... SYNC so they exercise the synchronous detach path explicitly instead of relying on the default best-effort behavior. I checked the changed tests against the InterpreterDropQuery and DatabaseAtomic detach flow, and I did not find a correctness, coverage, or metadata issue in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @tavplubix, could you review this? Three integration tests issue a plain DETACH DATABASE, which is best-effort, and then assert synchronous semantics, so assertCanBeDetached intermittently throws Code 219 when a table still has a live refcount; adding SYNC asks for waitDetachedTableNotInUse, the wait the engine already implements.

@PedroTadim PedroTadim self-assigned this Aug 13, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Aug 13, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Aug 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 979703c

CI is fully finished on this head: 178 checks completed, none queued or in progress, the praktika aggregator reports OK, and no job was dropped. There are no failed tests to own.

Check / test Reason Owner / fixing PR
(none) no test failed on this head -

Coverage is real rather than skipped-through: the three integration shards this PR touches ran and passed (Integration tests (amd_asan_ubsan, db disk, old analyzer, 3/6, 4/6, 6/6), amd_msan 2/8, 7/8, 8/8, amd_tsan 3/6, 4/6, 6/6, arm_binary distributed plan 1/4, 3/4, 4/4), plus Integration tests (amd_asan_ubsan, flaky) green. Config Workflow and Build profile diff each failed a first attempt on infrastructure and concluded success on re-run; their final conclusions are green.

Session id: cron:our-pr-ci-monitor:20260813-173000

Merged via the queue into ClickHouse:master with commit b752b0f Aug 13, 2026
532 of 536 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 13, 2026
@groeneai groeneai added the groeneai-origin-oranjeai PR origin: delegated from the OranjeAI sibling fleet label Aug 19, 2026
@PedroTadim

Copy link
Copy Markdown
Member

backport to fix CI

@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 18, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 18, 2026
Backport #114628 to 26.6: Wait for DETACH DATABASE to release tables in three integration tests
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 18, 2026
Backport #114628 to 26.7: Wait for DETACH DATABASE to release tables in three integration tests
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Backport #114628 to 26.3: Wait for DETACH DATABASE to release tables in three integration tests
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
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 groeneai-origin-oranjeai PR origin: delegated from the OranjeAI sibling fleet pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants