Skip to content

S3 parallel request to listing files - #66504

Closed
scanhex12 wants to merge 17 commits into
ClickHouse:masterfrom
scanhex12:s3_listing
Closed

scanhex12 wants to merge 17 commits into
ClickHouse:masterfrom
scanhex12:s3_listing

Conversation

@scanhex12

@scanhex12 scanhex12 commented Jul 14, 2024 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Speed-up listing files in S3. This closes #65572.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

CI Settings (Only check the boxes if you know what you are doing):

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64, release, debug

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

@CLAassistant

CLAassistant commented Jul 14, 2024 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@scanhex12 scanhex12 changed the title S3 listing S3 parallel request to listing files Jul 14, 2024
@scanhex12

scanhex12 commented Jul 14, 2024 •

Copy link
Copy Markdown
Member Author

Hi, I wrote the solution to the problem as it was written in the task description, but I ran into several problems while solving the problem

  1. I executed one query (let it take time T) and n queries in the thread pool with the number of workers >n. And in total, in the second case, the query time was much longer than T (apparently, we are running into the bandwidth limit of the network card somewhere). This is one of the reasons why multithreaded queries don't work as well as we would like.
  2. I noticed that in order for the algorithm to work well from the task description, it is necessary that when converting files to numbers, these numbers are distributed approximately evenly. However, this is not the case at all, because there are characters in the alphabet that are not in the file names (and they cannot be removed), so there are a lot of segments in which there are no files, but we set queries for them and it takes a lot of time

Because of these problems, I managed to speed up the solution only by about 2 times (provided that the hyperparameters are correctly selected, which will be discussed below)

I also noticed that we cannot initially know exactly how many segments we want to check in parallel (otherwise, queries will not become completely parallel there), so this is a hyperparameter that has to be iterated from query to query, as well as the constant 0.9 (which is not optimal for all queries)

please help me decide what to do with these problems =)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 14, 2024
@CheSema CheSema self-assigned this Jul 17, 2024
@CheSema

CheSema commented Jul 17, 2024 •

Copy link
Copy Markdown
Member

CI is not running due to incomplete description. You shall not delete category section and changelog section.

@CheSema

CheSema commented Jul 22, 2024

Copy link
Copy Markdown
Member

Please, fix the PR's description, @scanhex12. It needs to run the CI.

@clickhouse-ci clickhouse-ci Bot added the manual approve Manual approve required to run CI label Jul 29, 2024
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-improvement Pull request with some product improvements label Jul 29, 2024
@robot-clickhouse-ci-1

robot-clickhouse-ci-1 commented Jul 29, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 8afa00a with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS❌ failure
Successful checks
Check nameDescriptionStatus
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success

@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2024

Copy link
Copy Markdown
Contributor

Dear @CheSema, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@alexey-milovidov alexey-milovidov removed manual approve Manual approve required to run CI pr-improvement Pull request with some product improvements labels Sep 25, 2024
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Sep 25, 2024
@CheSema CheSema self-assigned this Nov 7, 2024
@CheSema

CheSema commented Nov 7, 2024 •

Copy link
Copy Markdown
Member

I admit that the idea is great.

But I do not see that it is appropriate here.
So I'm going to unassign myself from the reviewers. May be someone else could do the review of that PR.

My vision here is the following.
The listing API has the pagination feature. We has to introduce the designated processor which acts as a source of the file names. The next processor is receiving the file names, performs the reading data and returning the file context to the following processors for the further transformation. It allows to start the data transformation as soon as we have the first batch. And retrieve the second batch with file names in parallel with the transformation.
That approach would solve several issues which we currently have.
We could build the pipeline without communication with S3 at all. As a result the explain statement would not read the data from S3. Also we do not have to feed the real endpoint with the real data in such explain queries.
All exceptions during the listing would be handled as the exception during the execution. Right now the exceptions during the listing happen on the stage of building the pipeline. It is not right.

@alexey-milovidov

Copy link
Copy Markdown
Member

I tried to finish this pull request, but found several problems:

  1. It assumes a certain set of characters in file names. It was difficult to fix.
  2. The speed-up in my tests was not as high as I expected.

alexey-milovidov added a commit that referenced this pull request Jun 15, 2026
The previous commit parallelizes hierarchical globs by walking the '/' tree, but
a single big flat directory still lists serially (S3 pagination within a prefix
is sequential). Such directories are common when files are named by hash/UUID,
e.g. `musicbrainz/mlhdplus-complete/<uuid>.txt.zst` (~600k files in one prefix).

When a listed prefix is truncated but has no sub-directories, split it by
keyspace: tile its remaining range into contiguous `(start_after, end]`
sub-ranges and list them concurrently. Boundaries are derived from the byte
alphabet observed in the listed page (so the split adapts to the actual keys,
e.g. hex for UUIDs). Because the sub-ranges are contiguous they tile the
interval with no gaps — a key whose byte is absent from the sampled alphabet
still falls into the range that brackets it — so the split is complete
regardless of the key distribution or character set. This avoids the fragility
of the earlier attempt #66504, which used fixed split points assuming a uniform
distribution and a fixed character set.

Measured on `musicbrainz/mlhdplus-complete` (594415 flat files, real S3,
~75ms/list-request): listing drops from 37.6s to 3.7s (~10x) with identical
results (same count, no duplicates, identical path checksum).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Listing files for the `s3` table function with a glob was a single serial
stream of `ListObjectsV2` requests paginated by a continuation token, bound by
the per-request latency of S3 (~0.1-0.3s). This dominates queries over buckets
with many objects (issue ClickHouse#65572; an earlier attempt was ClickHouse#66504).

List the matching files in parallel by walking the "directory" tree formed by
the `/` delimiter: worker threads list different common prefixes concurrently,
and subtrees that cannot contain a matching key are pruned during the walk, so
for hierarchically partitioned layouts fewer objects are fetched, not merely
listed in parallel. Controlled by the new setting `s3_list_object_parallelism`
(default `1`, the previous serial behavior). Recursive globs (`**`) keep serial
listing. Correctness is guaranteed by the existing per-file `RE2::FullMatch`, so
the directory pruning only needs to be conservative.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The previous commit parallelizes hierarchical globs by walking the '/' tree, but
a single big flat directory still lists serially (S3 pagination within a prefix
is sequential). Such directories are common when files are named by hash/UUID,
e.g. `musicbrainz/mlhdplus-complete/<uuid>.txt.zst` (~600k files in one prefix).

When a listed prefix is truncated but has no sub-directories, split it by
keyspace: tile its remaining range into contiguous `(start_after, end]`
sub-ranges and list them concurrently. Boundaries are derived from the byte
alphabet observed in the listed page (so the split adapts to the actual keys,
e.g. hex for UUIDs). Because the sub-ranges are contiguous they tile the
interval with no gaps — a key whose byte is absent from the sampled alphabet
still falls into the range that brackets it — so the split is complete
regardless of the key distribution or character set. This avoids the fragility
of the earlier attempt ClickHouse#66504, which used fixed split points assuming a uniform
distribution and a fixed character set.

Measured on `musicbrainz/mlhdplus-complete` (594415 flat files, real S3,
~75ms/list-request): listing drops from 37.6s to 3.7s (~10x) with identical
results (same count, no duplicates, identical path checksum).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parallelize listing of files on S3

6 participants