Skip to content

Speed up server shutdown by shutting down databases concurrently - #128923

Open
evillique wants to merge 18 commits into
ClickHouse:masterfrom
evillique:concurrent-database-shutdown
Open

evillique wants to merge 18 commits into
ClickHouse:masterfrom
evillique:concurrent-database-shutdown

Conversation

@evillique

@evillique evillique commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Server shutdown visited databases one at a time, so with many small databases it stayed almost serial even after #104969. Now databases shut down concurrently. Tables that write into others in the background (FileLog, Kafka with kafka_keeper_path, NATS, QueryRunner, refreshable materialized views, MaterializedPostgreSQL) stop before Buffer tables are flushed, so their rows are no longer lost, and shutdown no longer lists external databases.

Changelog category (leave one):

  • Performance Improvement

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

Faster server shutdown with many databases, which now shut down concurrently. Tables that write into others in the background stop before Buffer tables are flushed, so their rows are no longer lost.


Workflow [PR]
Sync PR [sync-upstream/pr/128923]

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [44a16a6]

Summary: ✅


AI Review

Summary

The PR shuts down databases concurrently and adds a repeated background-writer stop and buffer-drain barrier. One data-loss path remains: the barrier excludes user-created tables in predefined databases, so it does not cover every buffer or producer whose destination can shut down.

Findings

❌ Blockers

  • src/Interpreters/DatabaseCatalog.cpp:394: isPredefinedDatabase excludes user-created storages as well as built-in tables. A valid system.buf using Buffer(dst, mt, ...) misses every global drain. The delayed shutdown of system flushes it after dst has cleared its tables, so the destination lookup fails and acknowledged rows are discarded. Background writers in predefined databases likewise miss the stop barrier. Include already-attached user-created storages without materializing deferred system tables. This omission predates the PR but remains in the discovery path the new shutdown barrier depends on.
Tests
  • ⚠️ Add a focused clickhouse-local regression with a pending row in system.buf targeting a durable table in another database; reopen the path and verify the row survived. The new tests use ordinary user databases and do not cover the excluded path. The finding is based on source tracing; no local ClickHouse executable was available to run the reproduction.
  • ⚠️ The Performance Improvement claim has no before/after shutdown timings or focused performance test in the available PR description, discussion, or diff. Provide a reproducible comparison for many small databases at a fixed database_catalog_shutdown_table_concurrency; binary-size and compilation-time reports do not establish shutdown speedup.
Final Verdict

Status: ❌ Block

Include user-created storages in predefined databases in the shutdown barrier and cover their buffered rows with a regression test, or explicitly track this pre-existing data-loss defect in a linked issue. Provide shutdown timing evidence for the performance claim.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 3, 2026
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 44a16a6b5 with master 30ff53fe8 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 1d9af504a; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

272 object files changed (+214.39 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/DatabaseCatalog.cpp.o 982.93 KiB 1020.17 KiB +37.25 KiB (+3.79%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

2206 translation units recompiled, 7480 s compile time in total, 2206 of them have a recent master baseline.

Job report

Comment thread src/Storages/Kafka/StorageKafka2.cpp Resolved
@evillique
evillique force-pushed the concurrent-database-shutdown branch from b663224 to 6de1e97 Compare November 4, 2026 12:38
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Storages/StorageQueryRunner.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Storages/StorageQueryRunner.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Storages/NATS/StorageNATS.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
… unlocked during shutdown, always stop `QueryRunner` early
Comment thread src/Storages/NATS/StorageNATS.cpp Resolved
…close the open `NATS` batch when preparing for shutdown
…, and do not list external databases on shutdown
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
…stop pass until no new background writer appears
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved

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

Labels

comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant