Skip to content

Add ability to disable checksums for S3 to avoid excessive input file read - #55559

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
azat:s3-fix-excessive-reads
Dec 5, 2023
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
azat:s3-fix-excessive-reads

Conversation

@azat

@azat azat commented Oct 12, 2023 •

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):

Add ability to disable checksums for S3 to avoid excessive input file read (this new behavior could be enabled with s3_disable_checksum=true)

AWS S3 client can read file multiple times, this is required for:

  • calculate checksums
  • calculate signature (done only for HTTP, since ClickHouse uses
    PayloadSigningPolicy::Never)

So this means that for HTTP, to send file to S3 it will be read 3x
times
, and for HTTPS 2x times.

By overriding GetChecksumAlgorithmName() to return empty string,
checksums can be disabled, and the input file will be read only once.

And even though additional https layer adds extra integrity layer,
it may be still too risky I guess, even though ClickHouse
internal format (for MergeTree only though) has checksums, and more.

Or maybe it will be better to add this as an optional behavior
(though if it questionable I guess it should not exists at all).

Here is an example stacktrace of this excessive read:

stacktrace
(lldb) bt
* thread 383, name = 'BackupWorker', stop reason = breakpoint 1.1
  * frame 0: 0x00000000103c5fc0 clickhouse`DB::StdStreamBufFromReadBuffer::seekpos() + 32 at StdStreamBufFromReadBuffer.cpp:67
    frame 1: 0x000000001777f7f8 clickhouse`std::__1::basic_istream<char, std::__1::char_traits<char>>::tellg() [inlined] std::__1::basic_streambuf<char, std::__1::char_traits<char>>::pubseekoff[abi:v15000](this=<unavailable>, __off=0, __way=cur, __which=8) + 120 at streambuf:162
    frame 2: 0x000000001777f7e3 clickhouse`std::__1::basic_istream<char, std::__1::char_traits<char>>::tellg() + 99 at istream:1249
    frame 3: 0x00000000152e4979 clickhouse`Aws::Utils::Crypto::MD5OpenSSLImpl::Calculate() + 57 at CryptoImpl.cpp:223
    frame 4: 0x00000000152dedee clickhouse`Aws::Utils::Crypto::MD5::Calculate() + 14 at MD5.cpp:30
    frame 5: 0x00000000152db5ac clickhouse`Aws::Utils::HashingUtils::CalculateMD5() + 44 at HashingUtils.cpp:235
    frame 6: 0x000000001528b97b clickhouse`Aws::Client::AWSClient::AddChecksumToRequest() const + 507 at AWSClient.cpp:772
    frame 7: 0x000000001528ded2 clickhouse`Aws::Client::AWSClient::BuildHttpRequest() const + 1682 at AWSClient.cpp:930
    frame 8: 0x00000000100b864f clickhouse`DB::S3::Client::BuildHttpRequest() const + 15 at Client.cpp:622
    frame 9: 0x0000000015286a41 clickhouse`Aws::Client::AWSClient::AttemptOneRequest(this=0x00007ffde2f8f000, httpRequest=<unavailable>, request=<unavailable>, signerName=<unavailable>, signerRegionOverride=<unavailable>, signerServiceNameOverride="s3") const + 65 at AWSClient.cpp:491
    frame 10: 0x00000000152845b9 clickhouse`Aws::Client::AWSClient::AttemptExhaustively(this=0x00007ffde2f8f000, uri=0x00007ffdd4d44f38, request=0x00007ffdd4d45d10, method=HTTP_PUT, signerName="SignatureV4", signerRegionOverride="us-east-1", signerServiceNameOverride="s3") const + 1337 at AWSClient.cpp:272
    frame 11: 0x0000000015298d0d clickhouse`Aws::Client::AWSXMLClient::MakeRequest() const + 45 at AWSXmlClient.cpp:99
    frame 12: 0x0000000015298cb5 clickhouse`Aws::Client::AWSXMLClient::MakeRequest() const + 309 at AWSXmlClient.cpp:66
    frame 13: 0x0000000015354b23 clickhouse`Aws::S3::S3Client::PutObject(this=0x00007ffde2f8f000, request=0x00007ffdd4d45d10) const + 2659 at S3Client.cpp:1731
    frame 14: 0x00000000100b174f clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const [inlined]
    frame 15: 0x00000000100b173a clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const + 41 at Client.cpp:578
    frame 16: 0x00000000100b1711 clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const + 981 at Client.cpp:508
    frame 17: 0x00000000100b133c clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const [inlined]
    frame 18: 0x00000000100b133c clickhouse`DB::S3::Client::PutObject() const + 28 at Client.cpp:418
    frame 19: 0x00000000103b96d6 clickhouse`DB::copyDataToS3File()

This new behaviour could be enabled with s3_disable_checksum=true.

Note, that I've checked this implementation with GCS/R2/S3/MinIO and it
works everywhere.

Refs: #55561

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-performance Pull request with some performance improvements label Oct 12, 2023
@robot-ch-test-poll3

robot-ch-test-poll3 commented Oct 12, 2023 •

Copy link
Copy Markdown
Contributor

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

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

Successful checks
Check nameDescriptionStatus
Mergeable CheckChecks if all other necessary checks are successful✅ success
Check nameDescriptionStatus
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

@azat
azat force-pushed the s3-fix-excessive-reads branch 3 times, most recently from 0961540 to 27c87eb Compare October 13, 2023 18:28
@azat
azat marked this pull request as draft November 23, 2023 16:40
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review November 26, 2023 03:07
@alexey-milovidov

Copy link
Copy Markdown
Member

I like the idea, and it should not be risky.
Let's ask @nickitat about his reaction 😕, and if he is ok, we will merge.

@alexey-milovidov alexey-milovidov self-assigned this Nov 26, 2023
@nickitat

nickitat commented Nov 26, 2023 •

Copy link
Copy Markdown
Member
  • I'm sceptical that improvement will be noticeable. but this is rather minor, because from perf standpoint it is clear win regardless of the magnitude
  • GCS/R2/AWS S3/MinIO prob not the only existing s3 implementations and I bet none of them guarantees that they will continue to accept requests w/o checksums. so at least I think we should have a setting to bring the old behaviour back
  • ClickHouse internal format (for MergeTree only though) has checksums sounds like maybe this functionality should be only for MT family or opt-in per each specific table

@alexey-milovidov

Copy link
Copy Markdown
Member

@azat, let's make it with a setting, so it will control insertions into table functions and S3 tables, inserts to MergeTree, and background merges through the default profile.

@azat

azat commented Nov 26, 2023

Copy link
Copy Markdown
Member Author

Yep, but I think it is better to make it server setting (I doubt that it should be configured on a per-table level).

@azat

azat commented Nov 26, 2023

Copy link
Copy Markdown
Member Author

Though, maybe, for instance in my case it was important for BACKUPs...

@alexey-milovidov

Copy link
Copy Markdown
Member

I thought it should be possible to disable it for MergeTree but enable checksums for S3 engine.

… read

AWS S3 client can read file multiple times, this is required for:
- calculate checksums
- calculate signature (done only for HTTP, since ClickHouse uses
  PayloadSigningPolicy::Never)

So this means that for HTTP, to send file to S3 it will be read 3x
times, and for HTTPS 2x times.

By overriding GetChecksumAlgorithmName() to return empty string,
checksums can be disabled, and the input file will be read only once.

And even though additional https layer adds extra integrity layer,
someone still may find this too risky I guess, even though ClickHouse
internal format (for MergeTree) has checksums, and more.

Here is an example stacktrace of this excessive read:

<details>

<summary>stacktrace</summary>

    (lldb) bt
    * thread 383, name = 'BackupWorker', stop reason = breakpoint 1.1
      * frame 0: 0x00000000103c5fc0 clickhouse`DB::StdStreamBufFromReadBuffer::seekpos() + 32 at StdStreamBufFromReadBuffer.cpp:67
        frame 1: 0x000000001777f7f8 clickhouse`std::__1::basic_istream<char, std::__1::char_traits<char>>::tellg() [inlined] std::__1::basic_streambuf<char, std::__1::char_traits<char>>::pubseekoff[abi:v15000](this=<unavailable>, __off=0, __way=cur, __which=8) + 120 at streambuf:162
        frame 2: 0x000000001777f7e3 clickhouse`std::__1::basic_istream<char, std::__1::char_traits<char>>::tellg() + 99 at istream:1249
        frame 3: 0x00000000152e4979 clickhouse`Aws::Utils::Crypto::MD5OpenSSLImpl::Calculate() + 57 at CryptoImpl.cpp:223
        frame 4: 0x00000000152dedee clickhouse`Aws::Utils::Crypto::MD5::Calculate() + 14 at MD5.cpp:30
        frame 5: 0x00000000152db5ac clickhouse`Aws::Utils::HashingUtils::CalculateMD5() + 44 at HashingUtils.cpp:235
        frame 6: 0x000000001528b97b clickhouse`Aws::Client::AWSClient::AddChecksumToRequest() const + 507 at AWSClient.cpp:772
        frame 7: 0x000000001528ded2 clickhouse`Aws::Client::AWSClient::BuildHttpRequest() const + 1682 at AWSClient.cpp:930
        frame 8: 0x00000000100b864f clickhouse`DB::S3::Client::BuildHttpRequest() const + 15 at Client.cpp:622
        frame 9: 0x0000000015286a41 clickhouse`Aws::Client::AWSClient::AttemptOneRequest(this=0x00007ffde2f8f000, httpRequest=<unavailable>, request=<unavailable>, signerName=<unavailable>, signerRegionOverride=<unavailable>, signerServiceNameOverride="s3") const + 65 at AWSClient.cpp:491
        frame 10: 0x00000000152845b9 clickhouse`Aws::Client::AWSClient::AttemptExhaustively(this=0x00007ffde2f8f000, uri=0x00007ffdd4d44f38, request=0x00007ffdd4d45d10, method=HTTP_PUT, signerName="SignatureV4", signerRegionOverride="us-east-1", signerServiceNameOverride="s3") const + 1337 at AWSClient.cpp:272
        frame 11: 0x0000000015298d0d clickhouse`Aws::Client::AWSXMLClient::MakeRequest() const + 45 at AWSXmlClient.cpp:99
        frame 12: 0x0000000015298cb5 clickhouse`Aws::Client::AWSXMLClient::MakeRequest() const + 309 at AWSXmlClient.cpp:66
        frame 13: 0x0000000015354b23 clickhouse`Aws::S3::S3Client::PutObject(this=0x00007ffde2f8f000, request=0x00007ffdd4d45d10) const + 2659 at S3Client.cpp:1731
        frame 14: 0x00000000100b174f clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const [inlined]
        frame 15: 0x00000000100b173a clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const + 41 at Client.cpp:578
        frame 16: 0x00000000100b1711 clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const + 981 at Client.cpp:508
        frame 17: 0x00000000100b133c clickhouse`DB::S3::Client::PutObject(DB::S3::ExtendedRequest<Aws::S3::Model::PutObjectRequest> const&) const [inlined]
        frame 18: 0x00000000100b133c clickhouse`DB::S3::Client::PutObject() const + 28 at Client.cpp:418
        frame 19: 0x00000000103b96d6 clickhouse`DB::copyDataToS3File()

</details>

This new behaviour could be enabled with `s3_disable_checksum=true`.

Note, that I've checked this implementation with GCS/R2/S3/MinIO and it
works everywhere.
@azat
azat force-pushed the s3-fix-excessive-reads branch from 27c87eb to c178390 Compare November 26, 2023 18:21
@azat azat changed the title [RFC] Disable checksums for S3 to avoid reading input file multiple times Add ability to disable checksums for S3 to avoid excessive input file read Nov 26, 2023
Comment thread src/IO/S3/tests/gtest_aws_s3_client.cpp Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, no content-md5 header here.

@azat

azat commented Nov 26, 2023

Copy link
Copy Markdown
Member Author

Ok, I've added s3_disable_checksum profile setting.

@azat
azat force-pushed the s3-fix-excessive-reads branch from c178390 to 1168b79 Compare November 26, 2023 19:03
@azat

azat commented Nov 27, 2023

Copy link
Copy Markdown
Member Author

Stress test (asan) — Killed by signal (in clickhouse-server.log)

@azat

azat commented Nov 27, 2023

Copy link
Copy Markdown
Member Author

AST fuzzer (tsan) — SUMMARY: ThreadSanitizer: data race build_docker/./contrib/boost/boost/smart_ptr/intrusive_ptr.hpp:183:16 in boost::intrusive_ptr<DB::ICo...

@azat
azat requested a review from alexey-milovidov December 4, 2023 09:39
Comment thread src/Core/Settings.h Outdated
M(Bool, s3_check_objects_after_upload, false, "Check each uploaded object to s3 with head request to be sure that upload was successful", 0) \
M(Bool, s3_allow_parallel_part_upload, true, "Use multiple threads for s3 multipart upload. It may lead to slightly higher memory usage", 0) \
M(Bool, s3_throw_on_zero_files_match, false, "Throw an error, when ListObjects request cannot match any files", 0) \
M(Bool, s3_disable_checksum, false, "Disable checksums for S3 to avoid excessive reads", 0) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation is not extensive enough. If a user reads it, it might think that we don't have checksums. Also, it confusingly says that it is related to reads, while in fact, it is related to writes.

@alexey-milovidov
alexey-milovidov merged commit 10d65a1 into ClickHouse:master Dec 5, 2023
@robot-ch-test-poll4 robot-ch-test-poll4 added the submodule changed At least one submodule changed in this PR. label Dec 5, 2023
@azat
azat deleted the s3-fix-excessive-reads branch December 5, 2023 07:27
baibaichen added a commit to Kyligence/gluten that referenced this pull request Dec 6, 2023
zzcclp pushed a commit to apache/gluten that referenced this pull request Dec 6, 2023
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20231206)

* fix build due to ClickHouse/ClickHouse#55559

---------

Co-authored-by: kyligence-git <[email protected]>
Co-authored-by: Chang Chen <[email protected]>
azat added a commit to azat/ClickHouse that referenced this pull request Sep 11, 2026
The ClickHouse#108573 added buffering, so checksum is calculated on top of it.

Remove s3_disable_checksum from test_throttling and also remove the warning for BACKUPs

Note, s3_disable_checksum initially has been introduced in ClickHouse#55559
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 submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants