Repository navigation
Fix cache key collisions when using table_disk - #96818
Conversation
It was created to read external data. table_disk assumes the disk is only used by a single table, with no directory-level isolation between tables. Allowing writes would make it possible to create multiple tables pointing to the same disk and insert into both, corrupting the data
|
Workflow [PR], commit [15ca537] Summary: ❌
|
There was a problem hiding this comment.
Pull request overview
This PR fixes critical cache key collisions and data corruption issues when using the table_disk=1 setting by (1) including disk names in all cache hash keys to prevent collisions when different disks have identical relative part paths, and (2) restricting table_disk to read-only disks only to prevent data corruption from multiple tables writing to the same writable disk without directory isolation.
Changes:
- Added disk name prefix (
diskname:) to all cache key generation for mark cache, primary index cache, text index caches (header, dictionary block, postings), and vector similarity index cache - Modified
table_diskvalidation to reject writable disks and only allow read-only disks - Updated existing test to use read-only plain metadata instead of plain_rewritable
- Added comprehensive tests for cache key collision prevention and writable disk rejection
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Storages/MergeTree/MergeTreeSettings.cpp | Changed validation logic to restrict table_disk to read-only disks only; updated documentation comment |
| src/Storages/MergeTree/MergeTreeMarksLoader.cpp | Added disk name prefix to mark cache key generation in loadMarksSync, loadMarksAsync, and addMarksToCache |
| src/Storages/MergeTree/IMergeTreeDataPart.cpp | Added disk name prefix to primary index cache keys in loadIndexToCache, moveIndexToCache, removeIndexFromCache, and vector similarity cache in removeFromVectorIndexCache |
| src/Storages/MergeTree/MergeTreeIndexText.cpp | Added disk name prefix to text index cache keys (header, dictionary block, and postings caches) |
| src/Storages/MergeTree/MergeTreeIndexReader.cpp | Added disk name prefix to vector similarity index cache key generation |
| src/Storages/MergeTree/MergeTreeDataPartWide.cpp | Added disk name prefix to mark cache key in removeMarksFromCache |
| src/Storages/MergeTree/MergeTreeDataPartCompact.cpp | Added disk name prefix to mark cache key in removeMarksFromCache |
| src/Storages/StorageMergeTree.cpp | Removed trailing period from error message |
| src/Storages/StorageReplicatedMergeTree.cpp | Removed trailing period from error message |
| tests/queries/0_stateless/03824_table_disk_rejects_writable_disks.sql | New test verifying writable disks are rejected and read-only disks are accepted for table_disk |
| tests/queries/0_stateless/03824_table_disk_rejects_writable_disks.reference | Empty reference file (test expects error for writable disk, success for readonly) |
| tests/queries/0_stateless/03823_table_disk_cache_key_collision.sql | New test creating two tables on separate web disks to verify no cache collisions |
| tests/queries/0_stateless/03823_table_disk_cache_key_collision.reference | Expected output showing both tables can be queried independently |
| tests/queries/0_stateless/03362_merge_tree_with_background_refresh.sh | Updated to use standard writer table and read-only plain disk for reader table instead of plain_rewritable |
| tests/queries/0_stateless/03355_mergetree_table_disk.sql | Updated comment to reflect that table_disk only supports s3_plain/web (removed s3_plain_rewritable) |
Co-authored-by: Copilot <[email protected]>
Please do not. We use it for writable disks. |
@alexey-milovidov are there any alternatives to prevent data corruption? We used it in two tests and I patched them |
|
Check that for every disk with "table_disk" there is only one table where it is not "readonly". |
…se/ClickHouse into fix-table-disk-plain-rewritable
@alexey-milovidov thanks for the suggestion! Now we allow one non-readonly table per disk |
|
|
||
| for (const auto & existing_disk : existing_merge_tree->getDisks()) | ||
| { | ||
| if (existing_disk->getName() == disk_name && !existing_disk->isReadOnly()) |
There was a problem hiding this comment.
I can still register 2 disks with different names but the same settings. In this case, this check will not find them.
There was a problem hiding this comment.
let's use disk->getDataSourceDescription()->sameKind
|
|
||
| /// Ensures that for a given disk used with `table_disk`, at most one table is non-readonly. table_disk assumes no directory-level isolation | ||
| /// between tables on the same disk, so allowing multiple writable tables would risk data corruption. | ||
| static void validateTableDiskUniqueness(const MergeTreeData & merge_tree, ContextPtr context) |
There was a problem hiding this comment.
Let's move this code to registerStorageMergeTree.cpp
|
|
||
| validateStorage(*res, mode); | ||
|
|
||
| /// Validate that for a given disk used with `table_disk`, at most one table is non-readonly |
There was a problem hiding this comment.
also to registerStorageMergeTree.cpp
fc5719e to
15ca537
Compare
|
In discussion with @azat and @Michicosun we realised the current solution to data corruption is not thorough enough. We prevent only the situation where two disks share the same name. However, we can create two disks with different names, but equal endpoints through a config. As such, I changed this PR to solve only a single problem. |
table_disktable_disk
... by including disk name in cache hash keys. With
table_disk, two separate web disks can have identical relative part paths. This caused cache collisions in mark cache, primary index cache, text index caches, and vector similarity index cache.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed cache key collisions between tables with
table_disk=1settingVersion info
26.2.1.991