Skip to content

Validate EmbeddedRocksDB setting values before persisting metadata - #103417

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix/rocksdb-alter-setting-validation-20260423
Jun 17, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:fix/rocksdb-alter-setting-validation-20260423

Conversation

@groeneai

@groeneai groeneai commented Apr 23, 2026 •

Copy link
Copy Markdown
Collaborator

ALTER TABLE ... ENGINE = EmbeddedRocksDB MODIFY SETTING with an invalid value (e.g. a non-boolean string for a Bool setting) was being persisted to the table metadata on disk before the value was validated. The validation happens afterwards in StorageEmbeddedRocksDB::alter when applyChanges is called on a fresh RocksDBSettings object, but the corruption is already on disk.

On the next server restart the bad metadata cannot be parsed and the table fails to attach with CANNOT_PARSE_BOOL (or similar errors such as Field value ... is out of range of long type). In an Atomic database only the failing table is lost, but for databases where load failures are fatal it prevents the server from starting altogether. Master stress test serverfuzz amd_msan hit exactly this on a BuzzHouse-generated workload.

Reproduction (without this change):

CREATE TABLE t (key UInt64, value UInt64) ENGINE = EmbeddedRocksDB PRIMARY KEY key;
ALTER TABLE t MODIFY SETTING optimize_for_bulk_insert = '2020-02-29';
-- Server rejects the ALTER with CANNOT_PARSE_BOOL, but...
SELECT create_table_query FROM system.tables WHERE name = 't';
-- CREATE TABLE t (...) ENGINE = EmbeddedRocksDB PRIMARY KEY key SETTINGS optimize_for_bulk_insert = '2020-02-29'
--                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Metadata on disk has been corrupted. Next server restart fails to attach the table.

Fix: Move the validation into StorageEmbeddedRocksDB::checkAlterIsPossible via a new RocksDBSettings::checkCanSet helper, so invalid setting values are rejected before IStorage::alter writes anything to disk. Follows the same pattern as MergeTreeData::checkAlterIsPossible which calls MergeTreeSettings::checkCanSet. After the fix the failed ALTER leaves the table metadata unchanged and the table is still usable on restart.

Adds tests/queries/0_stateless/04111_rocksdb_alter_setting_validation.sql which covers the above scenario plus valid MODIFY SETTING, RESET SETTING, and invalid UInt64 values.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=ad5ffb299775af545f5b7d3ccc5239feb08ed25a&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20amd_msan%29

Related: #88443 (same class of bug reported a year earlier with a different setting: zstd_window_log_max = 18446744073709551516 gets persisted when the server rejects another setting in the same ALTER).

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix a bug where ALTER TABLE ... MODIFY SETTING on an EmbeddedRocksDB table could persist an invalid setting value to the table metadata file even when the server rejected the query. On the next server restart the table would fail to attach with CANNOT_PARSE_BOOL (or a similar parsing error), and in databases where load failures are fatal the server would refuse to start. Invalid setting values are now rejected before any metadata is written.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.901

`ALTER TABLE ... ENGINE = EmbeddedRocksDB MODIFY SETTING` with an invalid
value (e.g. a non-boolean string for a `Bool` setting) was being persisted
to the table metadata on disk before the value was validated. The
validation happens afterwards in `StorageEmbeddedRocksDB::alter` when
`applyChanges` is called on a fresh `RocksDBSettings` object, but the
corruption is already on disk.

On the next server restart the bad metadata cannot be parsed and the
table fails to attach with `CANNOT_PARSE_BOOL` (or similar errors such
as `Field value ... is out of range of long type`). In an `Atomic`
database this affects only the failing table, but for databases where
load failures are fatal it prevents the server from starting altogether.
Master stress test `serverfuzz amd_msan` hit exactly this on a
BuzzHouse-generated workload
(https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=ad5ffb299775af545f5b7d3ccc5239feb08ed25a&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20amd_msan%29).

Move the validation into `StorageEmbeddedRocksDB::checkAlterIsPossible`
via `RocksDBSettings::checkCanSet`, so invalid setting values are
rejected before `IStorage::alter` writes anything to disk. Follows the
same pattern as `MergeTreeData::checkAlterIsPossible` which calls
`MergeTreeSettings::checkCanSet`.

Adds `tests/queries/0_stateless/04111_rocksdb_alter_setting_validation.sql`
which verifies that a failed `MODIFY SETTING` leaves the table metadata
unchanged and that subsequent valid alters continue to work.

Related: ClickHouse#88443
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @GrigoryPervakov @rschu1ze — could you review this? ALTER TABLE ... MODIFY SETTING on an EmbeddedRocksDB table was persisting the change to the metadata file before validating the value, so e.g. optimize_for_bulk_insert = '2020-02-29' would land on disk and brick the table on the next restart. Fix validates settings in checkAlterIsPossible via a new RocksDBSettings::checkCanSet, mirroring the MergeTreeSettings::checkCanSet pattern. Surfaced by the Stress test (experimental, serverfuzz, amd_msan) job on master (BuzzHouse-generated workload). Related: #88443.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (session cron:clickhouse-ci-task-worker:20260423-021500):

a) Deterministic repro? Yes. CREATE TABLE t (...) ENGINE = EmbeddedRocksDB PRIMARY KEY key; ALTER TABLE t MODIFY SETTING optimize_for_bulk_insert = '2020-02-29' throws CANNOT_PARSE_BOOL, and before this patch SELECT create_table_query FROM system.tables WHERE name = 't' then shows SETTINGS optimize_for_bulk_insert = '2020-02-29' — the metadata file on disk is corrupted despite the client-side error.

b) Root cause explained? IStorage::alter does params.apply(new_metadata) + database->alterTable(...) (writes the metadata to disk and updates in-memory state), and only AFTER that StorageEmbeddedRocksDB::alter rebuilds a fresh RocksDBSettings and calls applyChanges, which is where stringToBool throws. By the time validation runs, the bad value is already persisted. checkAlterIsPossible was called earlier in the flow but only validated that the alter TYPE was MODIFY_SETTING/comment, not the setting VALUES.

c) Fix matches root cause? Yes — move the value validation to checkAlterIsPossible (via RocksDBSettings::checkCanSet, same helper pattern as MergeTreeSettings::checkCanSet), so invalid values are rejected BEFORE IStorage::alter touches the metadata. No band-aid; the engine-specific validation now runs at the correct point in the alter flow.

d) Test intent preserved? / New tests added? Existing RocksDB tests (01504_rocksdb, 01686_rocksdb, 02118_show_create_table_rocksdb) all still pass. Added new regression test 04111_rocksdb_alter_setting_validation.sql which asserts (1) invalid-bool and invalid-UInt64 alters throw AND leave create_table_query unchanged, (2) subsequent valid MODIFY SETTING and RESET SETTING still work.

e) Both directions demonstrated? Yes. Before fix: reproduction confirmed the stale metadata path. After fix: metadata is unchanged after failed ALTER, new test passes 20/20 runs with randomization enabled, existing tests still green.

f) Fix is general, not a narrow patch? Yes — RocksDBSettings::checkCanSet validates ALL typed settings (both existing Bool / UInt64 and any future ones declared in LIST_OF_ROCKSDB_SETTINGS). It's not specific to optimize_for_bulk_insert. I did NOT expand scope to other storages with a similar pattern (StorageObjectStorageQueue validates only setting NAMES in its checkAlterIsPossible, not VALUES — same class of latent bug), that would be a separate PR.

@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Apr 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c595e1]

Summary: ✅


AI Review

Summary

This PR validates EmbeddedRocksDB table setting values in StorageEmbeddedRocksDB::checkAlterIsPossible before IStorage::alter can persist updated metadata. The new RocksDBSettings::checkCanSet helper uses the same typed conversion path as applyChanges, and the regression test covers invalid Bool and UInt64 values plus valid MODIFY SETTING and RESET SETTING flows. I found no unresolved correctness or coverage issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 23, 2026
…lidation

Per reviewer feedback on PR ClickHouse#103417 (inline review on the test file at
commit 235ae89 by clickhouse-gh[bot]), extend the regression test to
cover `UInt64` setting validation in addition to the `Bool` cases.

Two new assertions on `bulk_insert_block_size`:

1. `bulk_insert_block_size = 'not-a-number'` — non-numeric string,
   expected `CANNOT_PARSE_INPUT_ASSERTION_FAILED`.
2. `bulk_insert_block_size = -5` — negative value, expected
   `CANNOT_CONVERT_TYPE`. This directly reproduces the failure mode
   from issue ClickHouse#88443 ("Field value ... is out of range of ... type"),
   which was the motivating case for this PR.

Each failing `ALTER` is followed by a `SELECT create_table_query`
assertion to prove that the on-disk metadata is unchanged. Without the
`RocksDBSettings::checkCanSet` call added in the preceding commit, the
persisted metadata would be corrupted the same way as for the `Bool`
cases, causing `ATTACH TABLE` to fail on the next server restart.

Verified locally: 20/20 passes with randomized settings.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Added UInt64 invalid-value assertions in commit 41a323d.

Two new cases on bulk_insert_block_size (the only UInt64 setting declared for EmbeddedRocksDB):

  1. Non-numeric string — bulk_insert_block_size = 'not-a-number' → serverError CANNOT_PARSE_INPUT_ASSERTION_FAILED (error code 27).

  2. Out-of-range value — bulk_insert_block_size = -5 → serverError CANNOT_CONVERT_TYPE (error code 70) with the exact message mentioned in the issue tracker:

    Field value -5 is out of range of unsigned long type.

    This directly reproduces the failure mode from Confusing SEGV after restart #88443.

Each failing ALTER is followed by SELECT create_table_query to assert that the on-disk metadata is unchanged. Without RocksDBSettings::checkCanSet from the preceding commit, the invalid value would have been persisted the same way as for the Bool cases.

Verified locally: 20/20 passes with randomized settings.

Session: cron:clickhouse-ci-task-worker:20260423-051500

@evillique evillique self-assigned this Apr 23, 2026
Collapse the 6-line rationale for pre-validation into a concise 2-line
comment that still references issue ClickHouse#88443.

Per review by @evillique on PR ClickHouse#103417.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Merged master (cf81ad1ddee2) into this branch to pick up PR #103489 (MSAN/UBSAN trunk fixes for protocol.h, Lexer.cpp, HasSubsequenceImpl.h). Prior CI on 66dc5642ad9f showed 3 unrelated failures (function_prop_fuzzer on asan_ubsan/msan and chronic AST fuzzer STID 3436-4175) — the first two are fixed by #103489 now in master.

PR code itself is unchanged by the merge — the 3 original commits (fix + UInt64 tests + shortened comment) are preserved as-is.

cc @evillique — would you mind re-approving once CI goes green? Approval went stale after the push.

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Dear @evillique, 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.

@clickhouse-gh clickhouse-gh Bot assigned evillique and unassigned evillique Jun 1, 2026
@evillique

Copy link
Copy Markdown
Member

@groeneai Investigate the CI failure and link to the PR that fixes it (if there is one)

@groeneai

groeneai commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator Author

@evillique The only failing job on the previous HEAD (2b2e397c85ea, from 2026-04-24) was:

Job Failure Cause Status
AST fuzzer (amd_debug, targeted, old_compatibility) Logical error: Primary key type mismatch, expected UInt32, got Decimal/Float64/UInt128 (STID 3436-4175) Pre-existing trunk bug in DirectKeyValueJoin (JOIN USING against EmbeddedRocksDB), unrelated to this PR's checkCanSet change Fixed on master

This was a chronic trunk bug, not caused by this PR (CIDB: 13 PRs / 36 hits over 90 days, 0 master hits, the JOIN code path is unrelated to the ALTER-setting validation here).

It is already fixed by merged PR #103928 ("Fix LOGICAL_ERROR Primary key type mismatch in DirectKeyValueJoin via JOIN USING (STID 3436-4175)"), merged 2026-05-07. CIDB confirms zero occurrences of STID 3436-4175 anywhere after 2026-05-05.

The branch was 6 weeks behind master, so CI was still running the stale failing build. I have merged current master (4aea0f3aa3a) to pick up #103928 and refresh CI. The fuzzer failure should not recur. Once CI is green this is ready to merge.

@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

@evillique investigated — the failing check is Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6), and the only failing test is:

test_s3_aws_sdk_has_slightly_unreliable_behaviour/test.py::test_dataloss

This is unrelated to this PR (which only touches StorageEmbeddedRocksDB setting validation in checkAlterIsPossible — no S3 / object-storage code). It is a chronic cross-PR flake: CIDB shows 33 hits across 25 distinct PRs (3 on master) in the last 30 days, last hit 2026-06-02 22:29.

The test deliberately injects unreliable S3 SDK behaviour (broken-pipe / retry simulation), so it is timing-sensitive and flakes under ASan + ThreadFuzzer load. There is no open issue or dedicated fix PR for it yet — it is endemic infra noise rather than a regression. It clears on rerun.

The PR itself is green apart from this flake. Could you re-approve / merge when convenient? Approval was already given; only the flaky integration shard is red.

@evillique

Copy link
Copy Markdown
Member

@groeneai As per our policy, we unfortunately can't merge the PRs with any red checks on them. Could you please fix the test in question in a separate PR?

@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

@evillique Done. Opened a separate PR to fix the red test_dataloss check: #106400

Root cause was two-fold: the INSERT ran through the async queue (async_insert defaults to true) so a background flush kept retry-storming the always-failing S3 proxy and raced the cleanup DROP; and the disk's bare retry_attempts key was silently ignored (disk request settings need the s3_ prefix), leaving the 500-attempt default. The fix disables async_insert and renames the key to s3_retry_attempts=2. The test now fails synchronously in ~6s; validated 6/6 locally.

Once #106400 merges this check should go green. Could you re-approve/merge this PR after that?

@evillique

Copy link
Copy Markdown
Member

@groeneai, This test was fixed in #106353, update the branch to the latest master

@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

@evillique Done. Merged latest master into the branch (new HEAD 2c595e14ac63), which now includes #106353 - so the previously-red test_s3_aws_sdk_has_slightly_unreliable_behaviour::test_dataloss check should pass on the re-run. Verified the committed diff vs master is only the 5 PR files (RocksDBSettings.cpp/.h, StorageEmbeddedRocksDB.cpp, 04111 .sql/.reference) - no submodule or contrib leakage. The master-merge push invalidates the prior approval; could you re-approve and merge once CI is green? Thanks.

@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.50% +0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.10% +0.10%

Changed lines: Changed C/C++ lines covered by tests: 7/8 (87.50%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 5 line(s) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI is now fully finished and green on HEAD 2c595e14ac63 (master merge picking up #106353).

CI ledger for 2c595e14ac63:

@evillique the master-merge push invalidated your prior approval. Could you re-approve and merge? All checks apart from the non-blocking CH Inc sync infra job are green.

Session: cron:our-pr-ci-monitor:20260603-230000

@alexey-milovidov
alexey-milovidov merged commit 7ba34c3 into ClickHouse:master Jun 17, 2026
165 of 166 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 17, 2026
alexey-milovidov added a commit that referenced this pull request Jun 18, 2026
Refresh the branch (was 440 commits behind). This pulls in #103417
(`Validate EmbeddedRocksDB setting values before persisting metadata`),
which fixes the sole CI red on the previous run: `Stress test (amd_tsan)`
failed with `Cannot start clickhouse-server` because a fuzzed
`ALTER TABLE rocksdb_worm MODIFY SETTING optimize_for_bulk_insert = NULL`
(from `02956_rocksdb_bulk_sink`) persisted an invalid `Bool` value to the
metadata, so the table could not be re-attached on restart
(`Invalid value NULL of the setting, which needs bool`, CANNOT_CONVERT_TYPE).
That failure is master-side and unrelated to this test-only PR; the fix is
absent from the old merge-base and is brought in by this merge.

The diff stays test-only (`03518_alter_logical_race.{sh,reference}`).
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 pr-bugfix Pull request with bugfix, not backported by default 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