Skip to content

Read Parquet files even faster#49121

Closed
al13n321 wants to merge 6 commits intomasterfrom
multifile
Closed

Read Parquet files even faster#49121
al13n321 wants to merge 6 commits intomasterfrom
multifile

Conversation

@al13n321
Copy link
Copy Markdown
Member

@al13n321 al13n321 commented Apr 25, 2023

Changelog category (leave one):

  • Performance Improvement

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

  • Better performance when reading local Parquet files (through parallel reading).
  • Better performance for url() table function in any format (through connection pooling).
  • Fix http_skip_not_found_url_for_globs not working for compressed files.

Parquet's parallel random reads from local file are now actually parallel (pread() calls or memcpy from mmap), instead of locking two mutexes for no reason.


Added some limited HTTP session pooling for StorageURL. Sessions are now reused by all (non-concurrent) read operations on the same ReadBuffer. This makes SELECT count(*) from remote (different region) parquet files 2.5x faster!

We should add similar pooling for StorageS3, but that'll take more work: there's no simple way to attach the pool to a ReadBufferFromS3 instance, so it's either a global pool (with more care for eviction policy and correctness) or some hacks to indirectly pass a pool pointer to PocoHTTPClient.


Fixed a confusing error message when getting HTTP 404 error: it used to say "Not a Parquet file" because asArrowFileLoadIntoMemory() was catching exceptions too eagerly.


The behavior for skipping not-found URLs for globs was kind of inconsistent, in part broken by me in #47964

It skipped files by pretending that they're empty. This didn't work when the file is compressed - the decompression would error out trying to decompress an empty string.

Fixed by explicitly skipping the URL if the initial HTTP request failed, without pretending that the file is empty.


input_format_parquet_preserve_order now implies parallelize_output_from_storages.


In #47964 I refactored things to use SeekableReadBufferFactory in more places, as the abstraction for random-access "files". I think that was a mistake.

This PR re-refactors everything in the opposite direction: it adds a thread-safe positioned-read method in SeekableReadBuffer and uses it whenever parallel random reading is needed (ParallelReadBuffer and ParquetBlockInputFormat). This turned out better than the factory stuff, I think: less (?) code, fewer boilerplate objects, less useless extra cpu work (like maintaining a pool of ReadBuffers to reuse), less awkward code like ifs everywhere for buffer vs factory. The factory is removed altogether.


Inverted how attachSessionData() is used for marking whether HTTP sessions can be reused. Sessions were considered reusable by default, and ReadWriteBufferFromHTTP had to mark them as non-reusable on any error. It was missing some cases because there are lots of them. This PR flips it - the sessions are not reusable unless marked as reusable, which ReadWriteBufferFromHTTP does after receiving full response successfully. It seems silly that any of this needs to be done at all, instead of HTTPClientSession doing this tracking internally; but this was less work than changing HTTPClientSession.


Split ReadWriteBufferFromHTTP from header-only into a .h and .cpp files. This had no (easily-) measurable effect on build time (I ran a clean build 3 times with and without the change, difference was within the noise, which was ~0.1%). So I probably won't waste time on this type of refactoring in future :)

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-performance Pull request with some performance improvements label Apr 25, 2023
@alexey-milovidov alexey-milovidov changed the title Reduce unneecssary lock contention when reading local Parquet files Reduce unnecessary lock contention when reading local Parquet files Apr 25, 2023
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-backward-incompatible Pull request with backwards incompatible changes and removed pr-performance Pull request with some performance improvements labels Apr 25, 2023
@al13n321
Copy link
Copy Markdown
Member Author

al13n321 commented Apr 26, 2023

I'm still not sure whether the SeekableReadBufferFactoryPtr stuff was the right way to parallelize reading in the first place. Maybe instead there should be an optional thread-safe pread-like method in SeekableReadBuffer. Sounds like less code and more speed. I should try that.

Doing this, see the new methods in SeekableReadBuffer. It turned out ok, probably better than the SeekableReadBufferFactory nonsense. But I was hoping to also get rid of the factory in ParallelReadBuffer, then realized it wouldn't work that well: it currently streams data from each read range, i.e. reads+delivers smaller chunks (whatever size the read buffer returns); with a 'readBigAt()' method it would only be able to read the whole range at once; e.g. at default 10 MB ranges, on slow connection, it can be tens of seconds with no progress. So I guess we'll have both the factory and the readBigAt(). If anyone has better ideas, plz lmk, I'm going crazy over this :) EDIT: Figured out how to use it in ParallelReadBuffer, good bye factory.

Also added session reuse in ReadWriteBufferFromHTTP, it sped up SELECT count(*) by 2.5x (when reading from different region)! Need to add it to S3 too, that's a little more work.

@al13n321 al13n321 force-pushed the multifile branch 2 times, most recently from d2ebd4f to feb844f Compare April 28, 2023 02:31
@al13n321 al13n321 marked this pull request as ready for review April 28, 2023 02:39
@al13n321 al13n321 changed the title Reduce unnecessary lock contention when reading local Parquet files Read Parquet files even faster Apr 28, 2023
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-performance Pull request with some performance improvements and removed pr-backward-incompatible Pull request with backwards incompatible changes labels Apr 28, 2023
@al13n321 al13n321 marked this pull request as draft April 28, 2023 06:13
@al13n321 al13n321 marked this pull request as ready for review April 28, 2023 06:41
@robot-clickhouse
Copy link
Copy Markdown
Member

robot-clickhouse commented May 1, 2023

This is an automated comment for commit 67c6e75 with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🔴 failure

Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help.🟢 success
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR🟡 pending
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process.🔴 failure
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
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read https://clickhouse.com/blog/decorating-a-christmas-tree-with-the-help-of-flaky-tests/🔴 failure
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests🔴 failure
Mergeable CheckChecks if all other necessary checks are successful🔴 failure
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool.🟢 success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc.🟢 success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc.🟢 success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors.🔴 failure
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
Unit testsRuns the unit tests for different release types🟢 success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts.🟢 success

@devcrafter
Copy link
Copy Markdown
Member

I wonder if it would be possible to break the PR to smaller gradual PRs (according to the description, it addresses many issues at once, so looks feasible). It's quite inconvenient for reviewer big PRs. Generally, smaller gradual changes are better.

@al13n321
Copy link
Copy Markdown
Member Author

al13n321 commented May 5, 2023

Ok, will split.

@al13n321
Copy link
Copy Markdown
Member Author

al13n321 commented May 5, 2023

#49536 #49533 #49539 #49540 #49541

@al13n321 al13n321 closed this May 5, 2023
@al13n321 al13n321 deleted the multifile branch May 5, 2023 04:57
@al13n321
Copy link
Copy Markdown
Member Author

al13n321 commented May 5, 2023

@devcrafter, is there a good way to do dependent PRs?

For #49539 , #49540 , and #49541 I've set base branch to the parent PR's branch. The list of commits in each PR is correct, but CI didn't run any of the checks.

@qoega
Copy link
Copy Markdown
Member

qoega commented May 5, 2023

No good way in GitHub. But if you merge PR that incorporates some other PRs, they are marked as merged. So you can test and review separately and merge all at once

@qoega
Copy link
Copy Markdown
Member

qoega commented May 5, 2023

It's quite inconvenient for reviewer big PRs. Generally, smaller gradual changes are better.

We rarely ask to split PRs. There is an option to review changes per commit if history is split into separate commits

@devcrafter
Copy link
Copy Markdown
Member

@devcrafter, is there a good way to do dependent PRs?

For #49539 , #49540 , and #49541 I've set base branch to the parent PR's branch. The list of commits in each PR is correct, but CI didn't run any of the checks.

As mentioned already, unfortunately there is no good way for dependent PRs. Gerrit does it much better, but it's a different story.
Currently, the way to trigger PR is to change parent branch in PR (press Edit button near PR name and choose master). It's inconvenient since the PR will contain diff from master to PR branch (i.e. including parent branch), but you can switch the PR parent branch to see exact diff.

@devcrafter
Copy link
Copy Markdown
Member

It's quite inconvenient for reviewer big PRs. Generally, smaller gradual changes are better.

We rarely ask to split PRs.

imo, It makes sense here. To what degree, the author can decide himself (and it's nice if the author considers reviewer PoV)

There is an option to review changes per commit if history is split into separate commits

It doesn't work since most of the time authors doesn't put effort to separate commits logically (it's also not necessary). Just tackling different issues in different PRs already helps.

@al13n321
Copy link
Copy Markdown
Member Author

al13n321 commented May 5, 2023

I'm ok with it here, it was a bit messy. But it did take 2-3 hours.

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

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants