Repository navigation
Speed up server shutdown for databases with many tables - #104969
Conversation
|
Workflow [PR], commit [185d8ce] Summary: ❌
AI ReviewSummaryThis PR parallelizes the expensive per-table Findings
Tests
Final VerdictChanges requested. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 110/115 (95.65%) · Uncovered code |
|
@groeneai can you check what prevents merging and help? |
|
📊 Cloud Performance Report ✅ AI verdict: This sync PR parallelizes per-table shutdown during server stop (a new ShutdownTables thread pool and database_catalog_shutdown_table_concurrency setting) and touches only the shutdown path — no query-execution code. The two flagged ClickBench queries (Q4 -17.9%, Q15 -20.5%) both land inside master's current variance band with essentially unchanged CPU time, so they were correctly kept as no-change; the apparent speedups are run-to-run noise, not an effect of this change. The deterministic envelope suppression is consistent with the diff and requires no correction. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@serxa diagnosis for #104969 (head Everything else is green:
Nothing on the author's side looks blocking. Happy to dig further once the sync logs are visible. |
`AddDefaultDatabaseVisitor::visit(ASTSelectQuery &, ASTPtr &)` guards its WITH-traversal on `select.recursive_with` but then dereferences `select.with()->children` unconditionally. The parser sets the flag together with a non-empty WITH expression, but AST mutation after parsing (server-side `BuzzHouse` fuzzing, in particular) can leave the flag set while the expression is absent, producing a null pointer dereference inside `IStorageCluster::read`. Surfaced by `BuzzHouse (amd_tsan)` as `STID: 3440-3393` on PRs ClickHouse#104969 and ClickHouse#105332 (two unrelated PRs in five days). Guard the dereference with the actual presence of the WITH expression, matching the pattern already used in `ASTSelectQuery::formatImpl` and `QueryTreeBuilder::buildSelectQuery`. Valid `WITH RECURSIVE x AS ...` queries are unaffected. Adds a `gtest` that constructs the inconsistent AST shape, runs the visitor, and asserts no fatal failure; the test reliably aborts on the previous code with the `boost::intrusive_ptr` null-deref assertion. Closes ClickHouse#105370 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105332&sha=ac51d1463d5def7f7ba0354f581e078cbf10d6c6&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29 Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Hi @serxa, gentle ping. PR #104969 is green on public CI and all review threads are resolved; the only thing still blocking merge is the internal |
|
@groeneai Let's merge master into this branch to take new fixes into account and into sync PR |
|
@serxa branch is ~3.9k commits behind master but @jaymebrd could you merge master into |
|
@groeneai check recent CI failures? do we have fixes for them? are they merged in master? |
|
@serxa Triaged the 5 public failures at head 1. Integration tests (4 shards: asan_ubsan / msan / tsan / arm_binary) - 2. BuzzHouse (amd_debug) - 3. CH Inc sync - internal check, no public target_url; only CH Inc can inspect or re-run (unchanged from prior runs). Recommendation: nothing on the author side blocks merge. The shutdown code is not implicated by any of the five. Re-running BuzzHouse + the Integration shards should clear them; the only real gate remains the internal sync. |
|
@groeneai Failed test test_format_cannot_allocate_thread/test.py::test_it looks suspicious. Is it related to the changes in this PR? |
|
@serxa Yes, it looks related to this PR. I need to correct my 06-18 triage: I lumped it in with a pre-existing teardown race, but the hard signature on the current runs is specific to this branch. Evidence:
Mechanism: a SystemLog flush thread lazily CREATES its backing MergeTree table during shutdown; the So it is a latent shutdown-ordering fragility (system logs can lazily create their tables during shutdown and reach a torn-down static pool), and this PR's parallel shutdown is the first thing to surface it. Possible directions: skip lazy system-log table creation when the catalog is shutting down (just drop the unflushed entries, since the table was never materialized), or make sure the new parallel-shutdown work is fully joined before system logs flush so |
|
@groeneai Shutting down multiple interdependent pools is tricky. In easy cases, it could be resolved just by the proper ordering. Try to prepare the fix in a separate PR before we can merge this PR. The goal is to add necessary dependencies between startup and shutdown procedures, such that issue you found here is resolved. I hope you will not face circular dependencies; be careful with that (if nothing can prevent them, report back here) |
|
@serxa Will do. Preparing the fix in a separate PR off master. Plan: make SystemLogs teardown happen-before the static ActivePartsLoading pool teardown on all paths. Concretely, in No circular dependency expected: system logs depend on the static pools and on the |
Capture shared error state as shared_ptr<ErrorRecorder> and the logger by value, dropping `this` and by-ref captures. An orphan task — rare case where the pool accepts the job but tasks.emplace_back throws — can outlive shutdown() and would otherwise dangle into the stack frame. Also read recorder->first_error under the recorder's mutex in the tail, since an orphan task can still write to it after waitForAllToFinish. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
enqueueAndKeepTrack schedules the task, then tracks it with tasks.emplace_back. If that emplace_back reallocated and threw after the task was already scheduled, the catch ran the task inline a second time while the scheduled copy stayed untracked (and unwaited). Reserve the tracking vector up front so emplace_back never reallocates; then a throw from enqueueAndKeepTrack always means the task was never scheduled. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Parallelizing flushAndPrepareForShutdown was unsafe: some tables flush into other tables there (a Buffer table writes to its destination), and running that concurrently could hit a destination already in shutdown-prepared read-only mode and silently drop rows. Keep the prepare phase sequential (unchanged from master), and parallelize only the raw shutdown() phase — the expensive, table-independent drain (ZooKeeper sessions, interserver endpoints, background pools) that was the actual bottleneck for databases with many tables. Since prepare already ran, call shutdown() directly instead of flushAndShutdown() to avoid re-running the flush. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…er-side in test - database_catalog_shutdown_table_concurrency = 0 now means "number of CPU cores" (matching max_database_replicated_create_table_thread_pool_size) instead of being clamped to 1; documented in the setting description. - The integration test asserts on the server-reported "Shut down N tables in <db> in M ms" duration instead of wall-clock time, so sanitizer overhead on the orchestration side does not make it flaky. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ores) Per review: default the shutdown pool size to 0, which means "number of CPU cores", instead of a fixed 16. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
8fcdb66 to
18f21b4
Compare
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 109/114 (95.61%) · Uncovered code |
|
@groeneai, investigate the failure: https://github.com/ClickHouse/ClickHouse/actions/runs/29755463902/job/88491825359 and provide a fix in a separate PR. If the fix is already in progress, link it here. Note: job logs are no longer accessible via the GitHub API (artifact expired). Cloud benchmark report shows no regression (37 queries, no change). If this is a pre-existing or infrastructure flakiness in the performance comparison, please confirm. |
|
Confirmed: infrastructure, not a performance regression, and the fix is already merged. What failed. The GH Actions log is expired, but the check-run annotation survives ( The runner ran out of disk. The job died before uploading anything, which is why the shard reports no rows to CIDB and there is no failing query to look at. All 3 attempts (jobs 88449175162, 88476564379, 88491825359) failed the same way on 3 different runners ( Not a regression: at Not specific to this PR. Since a disk-death shard reports nothing, I counted incomplete shard sets instead (
A 3-day fleet-wide window, amd and arm together. I spot-checked other PRs in it (jobs 89032972175, 89041380548, 88991625264) and they carry the byte-identical ENOSPC annotation. Cause and fix. The window opens with #110009 (merged 07-20T13:16Z), which added So there is nothing for me to fix in a separate PR: #111377 is the fix, and the rate has been 0-2% since it merged. A rerun of shard 4/6 on a rebased head should be green. One note while I am here: the other red on this PR, |
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Dear @serxa, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes73 object files changed (+118.55 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units511 translation units recompiled, 4142 s compile time in total, 511 of them have a recent master baseline. |
a5c2615
|
@jaymebrd @serxa @evillique — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
PR ClickHouse#104969 parallelized the table shutdown drain and split the drain's flushAndShutdown() into a sequential prepare pass (flushAndPrepareForShutdown) followed by a parallel plain shutdown(). That dropped the second flush that flushAndShutdown() used to perform. For a same-database Buffer -> Buffer chain, tables are prepared in table-name order. When the destination buffer sorts before the source, it is prepared (and flushed while empty) before the source flushes its rows into the destination's in-memory buffer. StorageBuffer has no shutdown() override and no destructor flush, so the parallel drain then destroys those rows silently, with no error in the log. Run the prepare pass twice, restoring the pre-ClickHouse#104969 re-flush while keeping the expensive shutdown drain parallel. Depth-2 chains are rescued regardless of name order; deeper adversarial chains were already broken before ClickHouse#104969. Closes ClickHouse#116803. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Table shutdown runs sequentially per table, which can make server shutdown take many minutes to 1hr+ on clusters with many thousands of tables.
This PR parallelises the table shutdown drain in
DatabaseWithOwnTablesBase::shutdown:src/Databases/DatabasesCommon.cpp: the prepare phaseflushAndPrepareForShutdownstays sequential and the expensive, table-independentshutdown()drain (ZooKeeper sessions, interserver endpoints, background pools) runs concurrently via a dedicated thread pool.The speedup is best measured by SIGTERM-to-exit wall time on many-table clusters.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up server shutdown for databases with many tables by shutting tables down in parallel. Controlled by the new
database_catalog_shutdown_table_concurrencysetting (0 by default, meaning the number of CPU cores).Workflow [PR]
Sync PR [sync-upstream/pr/104969]
Version info
26.9.1.221(included in26.9and later)