Skip to content

Fix exception when querying system.rocksdb during concurrent table drop - #101108

Merged
alexey-milovidov merged 30 commits into
masterfrom
fix-rocksdb-statistics-race
May 6, 2026
Merged

alexey-milovidov merged 30 commits into
masterfrom
fix-rocksdb-statistics-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

StorageSystemRocksDB::fillData collects RocksDB tables in a first pass, but by the time it calls getRocksDBStatistics in the second pass, the table may have been concurrently dropped. getRocksDBStatistics returns null when rocksdb_ptr is null (table shut down), and the code threw a LOGICAL_ERROR exception instead of gracefully skipping the table.

Found in Stress test (amd_debug): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100290&sha=672c551dd5b6b553a8c76d3fc13e823303b2a822&name_0=PR&name_1=Stress%20test%20%28amd_debug%29
#100290

Changelog category (leave one):

  • CI Fix or Improvement

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

Fix LOGICAL_ERROR exception "RocksDB statistics are not available" when querying system.rocksdb while a RocksDB table is being concurrently dropped.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.322

…drop

`StorageSystemRocksDB::fillData` collects RocksDB tables in a first pass, but
by the time it calls `getRocksDBStatistics`, the table may have been dropped.
Skip the table instead of throwing a `LOGICAL_ERROR` exception.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100290&sha=672c551dd5b6b553a8c76d3fc13e823303b2a822&name_0=PR&name_1=Stress%20test%20%28amd_debug%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [938d2aa]

Summary: ✅


AI Review

Summary

This PR fixes a real race around system.rocksdb during concurrent DROP TABLE, hardens multiple rocksdb_ptr call sites with proper locking/null checks, and adds a targeted race test that now correctly fails on regressions in the system.rocksdb path. I did not find new correctness, concurrency, or safety issues beyond already-addressed review history.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 29, 2026
@clickhouse-gh clickhouse-gh Bot added pr-ci and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 30, 2026
@Algunenano Algunenano self-assigned this Mar 30, 2026

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

I see multiple 3 other problematic spots related to this problem with rocksdb:

  • StorageEmbeddedRocksDB.cpp::336: rocksdb_ptr->Write(...) in the mutation path: no lock on rocksdb_ptr_mx at all. If DROP runs concurrently with a mutation, this dereferences a null pointer.
  • StorageEmbeddedRocksDB.cpp::401: rocksdb_ptr->CompactRange(...) in optimize: acquires the shared lock but doesn't check for null before dereferencing, unlike the other guarded sites (lines 833-835, 842-844, 961-964, 972-975 all do the null check).
  • StorageEmbeddedRocksDB::684 — storage.rocksdb_ptr->NewIterator(...) in ReadFromEmbeddedRocksDB::initializePipeline: no lock at all, direct access to rocksdb_ptr.

I think we should use TSA anotations for rocksdb_ptr and make sure we check for null before dereferencing it

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The test 02859_replicated_db_name_zookeeper is fixed in #101952

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The flaky test failures in this PR (01605_drop_settings_profile_while_assigned, 01418_custom_settings, 02015_async_inserts_4) are caused by non-unique access entity names and will be resolved by #102131.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Hung check failure is fixed by #102008 and #102010, let's update the branch

alexey-milovidov and others added 5 commits April 9, 2026 19:16
…uarded accesses

Add `TSA_GUARDED_BY(rocksdb_ptr_mx)` annotation to `rocksdb_ptr` and
`TSA_NO_THREAD_SAFETY_ANALYSIS` to `initDB` (called from constructor
and from `truncate` which already holds the lock).

Fix three race conditions identified in review:
- `mutate` DELETE path: `rocksdb_ptr->Write` had no lock — add
  `SharedLockGuard` + null check.
- `optimize`: `rocksdb_ptr->CompactRange` had shared lock but no null
  check — add null check.
- `ReadFromEmbeddedRocksDB::initializePipeline`: `rocksdb_ptr->NewIterator`
  had no lock — add `SharedLockGuard` + null check.

Also protect `rocksdb_ptr` accesses in `EmbeddedRocksDBSink` and
`EmbeddedRocksDBBulkSink` for consistency, and switch all shared locks
from `std::shared_lock` to TSA-aware `SharedLockGuard`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Four concurrent loops for 10 seconds:
- CREATE/INSERT/DROP cycle
- SELECT from `system.rocksdb`
- SELECT from the RocksDB table
- OPTIMIZE on the RocksDB table

Exercises the code paths fixed in this PR: `getRocksDBStatistics`,
`initializePipeline`, and `optimize` racing with `drop`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
fm4v added a commit that referenced this pull request Apr 14, 2026
Split flaky check and targeted check into separate roles:

Flaky check — runs only new/modified tests from the PR with thread
fuzzer enabled. Tests run in parallel with themselves (shared queue)
to ensure new tests are parallel-safe under randomized thread ordering.

Targeted check — runs previously failed and coverage-relevant tests
with per-worker partitioning (--no-self-parallel) so the same test
never runs concurrently on multiple workers, avoiding conflicts on
shared resources (hardcoded users, roles, settings profiles, etc.).
Both use --test-runs 50.

Changes:
- Add --no-self-parallel flag to clickhouse-test: partitions tests
  by name hash so all copies go to the same worker
- Flaky check: only changed tests, thread fuzzer, shared queue
- Targeted check: relevant tests, no thread fuzzer, per-worker queues,
  50 runs, single clickhouse-test invocation
- Restore original thread fuzzer parameters (reverts reductions from
  06c945a and 9790435)
- Add amd_binary to flaky check, use AMD_LARGE for targeted debug
- Targeted check uses --flaky-check --no-self-parallel

PRs affected by tests running in parallel with themselves:
#102015 #101093 #99653 #101503 #101108 #101884 #100746
#101447 #100941 #101099 #100203 #96130

#102038
fm4v added a commit that referenced this pull request Apr 14, 2026
Split flaky check and targeted check into separate roles:

Flaky check — runs only new/modified tests from the PR with thread
fuzzer enabled. Tests can run in parallel with themselves (shared
queue) to ensure new tests are parallel-safe.

Targeted check — runs previously failed and coverage-relevant tests
with per-worker partitioning (--no-self-parallel) so the same test
never runs concurrently on multiple workers, avoiding conflicts on
shared resources (hardcoded users, roles, settings profiles, etc.).
Both use --test-runs 50.

Changes:
- Add --no-self-parallel flag to clickhouse-test: partitions tests
  by name hash so all copies go to the same worker
- Flaky check: only changed tests, thread fuzzer, shared queue
- Targeted check: relevant tests, no thread fuzzer, per-worker
  partitioning, 50 runs, single clickhouse-test invocation
- Restore original thread fuzzer parameters
- Change targeted check to arm_asan_ubsan on ARM_LARGE

PRs affected by tests running in parallel with themselves:
#102015 #101093 #99653 #101503 #101108 #101884 #100746
#101447 #100941 #101099 #100203 #96130
alexey-milovidov and others added 2 commits April 24, 2026 09:57
The raw pointer obtained via `GetSystemClock().get()` was taken from a
temporary `shared_ptr` that was destroyed at the end of the full
expression. After `lock.unlock()`, a concurrent `DROP TABLE` could
destroy the `rocksdb_ptr` and its `Env`, so `GetCurrentTime` could be
called through a dangling pointer. Keep the `shared_ptr` returned by
`GetSystemClock` to extend ownership across the call.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Add the `use-rocksdb` tag back to `04094_rocksdb_system_table_race.sh`
so the test is only scheduled on builds with `EmbeddedRocksDB` support.

Tolerate expected `Table ... does not exist` errors in the `SELECT`,
`OPTIMIZE`, and `system.rocksdb` workers with `|| true`, so they keep
running for the full timeout instead of exiting on the first race
under `set -o errexit`.
alexey-milovidov and others added 6 commits May 1, 2026 21:31
In `04094_rocksdb_system_table_race.sh`, the `wait` loop overwrote
`status` on each failed `wait`, so the exit code reflected the **last**
failing background job rather than the first one (contradicting the
inline comment).

Capture each `wait` result into `ec` and only update `status` while it
is still zero, so the first non-zero exit code is preserved.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, fix the fuzzer finding, Logical error: Primary key type mismatch and send a separate PR with the fix.

@groeneai

groeneai commented May 5, 2026

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — already addressed by PR #103928 ("Fix LOGICAL_ERROR Primary key type mismatch in DirectKeyValueJoin (STID 3436-4175)").

It's currently waiting on @vdimir's response to my 2026-05-04 question about whether we should keep the legacy-planner fix as a stopgap (no open removal PR for the legacy path; STID 3436-4175 still recurring chronically — 29 hits in last 30 days, latest 2026-05-04 12:22 UTC) or close the PR pending the new-analyzer-only direction.

grantholly-clickhouse pushed a commit to grantholly-clickhouse/ClickHouse that referenced this pull request May 5, 2026
When a direct join between an `EmbeddedRocksDB` table and a subquery whose
join key has a different type reaches `StorageEmbeddedRocksDB::getByKeys`,
the existing defensive check threw `LOGICAL_ERROR` with the message
"Primary key type mismatch, expected ..., got ...". This was inappropriate
because the condition is reachable from a regular user query (the planner
does not always insert a type conversion before a direct join, e.g. with
`USING`), so it should surface as a user-facing `TYPE_MISMATCH` instead of
a server-side logical error that aborts in debug builds.

Found by AST fuzzer (amd_debug, targeted, old_compatibility) on
ClickHouse#101108
Report: https://s3.amazonaws.com/clickhouse-test-reports/PRs/101108/dbafc9d33a11336412d0f9ffdd53fbfbb560aa70/ast_fuzzer_amd_debug_targeted_old_compatibility/

Triggered by:
SELECT * FROM (SELECT k AS key FROM t2__fuzz_22) AS t2
INNER JOIN rdb USING (key) ORDER BY key ASC NULLS LAST
…ics-race

# Conflicts:
#	src/Storages/RocksDB/StorageEmbeddedRocksDB.cpp
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.20% +0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 75.00% (57/76) | lost baseline coverage: 2 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 6, 2026
Merged via the queue into master with commit 37a8ed1 May 6, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-rocksdb-statistics-race branch May 6, 2026 09:06
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 6, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @alexey-milovidov @Algunenano — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request May 6, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Split flaky check and targeted check into separate roles:

Flaky check — runs only new/modified tests from the PR with thread
fuzzer enabled. Tests run in parallel with themselves (shared queue)
to ensure new tests are parallel-safe under randomized thread ordering.

Targeted check — runs previously failed and coverage-relevant tests
with per-worker partitioning (--no-self-parallel) so the same test
never runs concurrently on multiple workers, avoiding conflicts on
shared resources (hardcoded users, roles, settings profiles, etc.).
Both use --test-runs 50.

Changes:
- Add --no-self-parallel flag to clickhouse-test: partitions tests
  by name hash so all copies go to the same worker
- Flaky check: only changed tests, thread fuzzer, shared queue
- Targeted check: relevant tests, no thread fuzzer, per-worker queues,
  50 runs, single clickhouse-test invocation
- Restore original thread fuzzer parameters (reverts reductions from
  06c945a and 9790435)
- Add amd_binary to flaky check, use AMD_LARGE for targeted debug
- Targeted check uses --flaky-check --no-self-parallel

PRs affected by tests running in parallel with themselves:
ClickHouse#102015 ClickHouse#101093 ClickHouse#99653 ClickHouse#101503 ClickHouse#101108 ClickHouse#101884 ClickHouse#100746
ClickHouse#101447 ClickHouse#100941 ClickHouse#101099 ClickHouse#100203 ClickHouse#96130

ClickHouse#102038
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants