Repository navigation
Speed up loading of plain_rewritable disk metadata - #121412
Conversation
Loading the metadata of a `plain_rewritable` disk took one `ListObjects` request per directory, plus a sequential scan of `__meta` whose pages can only be fetched one after another. For a disk with 36401 directories that is 36401 `ListObjects` plus 36401 `GetObject` requests and ~37 sequential listing pages, which took 116 seconds on `https://data.clickhouse.com/public-datasets/data/census2012/`. Directory names in this layout are random strings from `getRandomASCIIString`, so they can be grouped by their first characters and listed in parallel: - `__meta` is enumerated by 26 prefix shards, plus two boundary shards so a name outside of the alphabet is not silently dropped; - the files of all directories are collected by a few listings of the whole disk instead of one listing per directory (716 requests for 501234 objects), interleaved with the `prefix.path` reads so both run at the same time. The metadata load of that disk goes from 116 to 37 seconds, and the query that triggers it from 2m22s to 1m05s. This needs the object storage to list by an arbitrary key prefix and to honour `start_after`, which `LocalObjectStorage` and `AzureObjectStorage` do not, so it is gated on the new `IObjectStorage::supportsPrefixListing`. A disk with few directories keeps the previous single-request path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [20fdf11] Summary: ✅
AI ReviewSummaryThis PR parallelizes Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 20fdf11.
Changed lines: Changed C/C++ lines covered: 74/143 (51.75%) · Uncovered code |
| /// The point of the sharded listing is to replace many sequential pages with a few parallel ones, | ||
| /// so it only pays off once a plain listing would need many pages. Until then a plain listing costs | ||
| /// a single request while a sharded one costs one request per shard. | ||
| constexpr size_t MIN_DIRECTORIES_TO_LIST_IN_PARALLEL = 4096; |
There was a problem hiding this comment.
list_object_keys_size is a supported S3 disk setting, but these cutover/shard constants bake in the default 1000-key page size.
On a disk configured with a smaller page size (for example list_object_keys_size = 1, which we already use in tests to force pagination), the sequential probe above burns 4096 ListObjects requests before it even switches to the sharded path, and then the sharded loaders re-read the same keys. With a larger page size, the same constants can overshard a path that still fits in one page. That makes the “large disk” optimization depend on an unstated setting value and can regress supported configurations.
Can this derive its cutover and shard sizing from the storage’s actual list_object_keys_size, or at least expose the thresholds as settings so they can be tuned and tested?
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes47 object files changed (+62.18 KiB total), 0 added.
716 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 units422 translation units recompiled, 2571 s compile time in total, 422 of them have a recent master baseline. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The implementation is good.
|
I'm afraid it is not covered by tests, but in this case it is acceptable. |
…apshot-file Resolve the conflict in `MetadataStorageFromPlainRewritableObjectStorage.cpp` with #121412, which reworked the listing of the metadata directory (prefix shards, prelisted files, per-directory result slots). `listRemoteLayout` now enumerates the directories with the sharded listing from master, then takes the directories whose `prefix.path` ETag matches the base (the snapshot or the current state) without reading, and loads only the rest. The whole-disk prelisting of files is used when there are many directories left to load, instead of being tied to the refresh flag that no longer exists. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Motivation
Attaching a database whose tables live on a large
s3_plain_rewritabledisk and running a singleSELECTtook minutes. Reproducer against a public dataset:lazy_load_tablesworks as intended here — theATTACH DATABASEitself takes 2.2 seconds. The cost is in theSELECT, which instantiates the table's own disk, and that disk's metadata is loaded in full:MetadataStorageFromPlainRewritableObjectStorage::loadran one task per directory, and each task made two requests: aGetObjectfor__meta/{directory}/prefix.path(the logical path is stored in the object body, so it has to be read) and aListObjectsfor the files of that directory. On top of that,__metaitself was enumerated by a single listing, whose pages can only be fetched one after another.Profile events for the whole reproducer:
S3ListObjects = 36468,S3GetObject = 45644, of which 36401 areprefix.pathreads and the rest is real part metadata.The object storage is the limit, not the CPU or the thread count. Measured against that endpoint: a small
GetObjecttakes about 70 ms and about 1350 of them per second complete at concurrency 100; aListObjectspage of 1000 keys takes about 1.0 s and returns 261 KB; the prefix holds 501234 objects. Raisingmax_io_thread_pool_sizefrom 100 to 1000 brings the load from 116 to 54 seconds, but it scales sub-linearly and produces 1834 retriedSSL_read timed outfailures, so the answer has to be fewer requests rather than more threads.Approach
Directory names in this layout are random strings from
getRandomASCIIString, so they are uniformly distributed overa-zand the key space can be split by leading characters and listed in parallel.__metais enumerated by 26 prefix shards instead of one sequential listing, plus two boundary shards covering whatever sorts before and after the alphabet, so a directory written by another implementation is never silently dropped.prefix.pathreads rather than scheduled ahead of them, because the pool takes tasks in order and neither kind of request saturates the object storage on its own.This requires the object storage to treat its argument as an arbitrary key prefix and to honour
start_after.LocalObjectStoragewalks a real directory tree and returns nothing for a prefix that is not a directory, andAzureObjectStorageignoresstart_after— both would fail silently, so the new path is gated onIObjectStorage::supportsPrefixListing, currently true only for S3. A disk with few directories keeps the previous single-request path, since sharding a listing that fits in one page only adds requests.Results
max_io_thread_pool_size = 1000The directory counts differ because the dataset is live and grew during the measurements (
__metawent from 36401 to 37166 objects over the session).Correctness was checked against the same dataset:
count()overcensus2012_rdnsreturns 10575805741, and a read of one partition returns 49720453 rows with correct column aggregates, which exercises the prelisted file sizes on real reads. On a localplain_rewritabledisk (the non-sharded path), insert, merge,SYSTEM RESTART DISK,DROP PARTITIONand detach/attach all produce consistent results.Remaining cost
About 30 of the remaining 37 seconds are the 36401
prefix.pathreads, which cannot be avoided while the logical path lives in the object body. Removing them needs either encoding the logical path in the__metaobject key, so that a listing alone yields the mapping, or persisting the resolved layout for read-only disks. A smaller related improvement is to key the unchanged-directory check inrefreshon the remote directory name instead of the resolved local path, so that a periodic refresh does not have to read everyprefix.pathbefore it can consult the snapshot.MIN_DIRECTORIES_TO_LIST_IN_PARALLELis a compile-time constant, which makes the sharded path awkward to cover by a functional test; promoting it to a server setting would make it testable and give an escape hatch.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Loading the metadata of a large
s3_plain_rewritabledisk is several times faster. Previously it made oneListObjectsrequest per directory and enumerated the__metadirectory by a single paginated listing, so a disk with tens of thousands of directories needed tens of thousands of sequential-ish requests. The listings are now split by key prefix and run in parallel, and the files of all directories are collected by a few listings of the whole disk. For a disk with 36 thousand directories holding half a million objects, the load time dropped from 116 to 37 seconds.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121412]
Version info
26.10.1.458-master(included in26.10and later)