Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving memory performance when it comes to snappy #11177

Merged
merged 6 commits into from
Sep 25, 2024

Conversation

MovieStoreGuy
Copy link
Contributor

Description

Moving snappy to lazy read from the original payload instead decompressing all of the buffer into memory.

This is something I noticed while trying to introduce support for lz4 compression, moved into its own PR at the suggestion of @atoulme.

Testing

All of the tests are already present so no additional tests were needed.

@MovieStoreGuy MovieStoreGuy requested review from a team and mx-psi September 16, 2024 04:15
@MovieStoreGuy MovieStoreGuy force-pushed the msg/fix-snappy-lazy-read branch from f78bc6a to d842e6c Compare September 16, 2024 04:16
Copy link

codecov bot commented Sep 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.56%. Comparing base (04379eb) to head (b3318bf).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11177   +/-   ##
=======================================
  Coverage   91.55%   91.56%           
=======================================
  Files         424      425    +1     
  Lines       20199    20198    -1     
=======================================
+ Hits        18494    18495    +1     
+ Misses       1320     1319    -1     
+ Partials      385      384    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

config/confighttp/compression.go Outdated Show resolved Hide resolved
config/confighttp/compression.go Outdated Show resolved Hide resolved
@MovieStoreGuy MovieStoreGuy force-pushed the msg/fix-snappy-lazy-read branch from 04d1409 to 4be7ad7 Compare September 17, 2024 02:55
@MovieStoreGuy MovieStoreGuy requested a review from a team as a code owner September 19, 2024 14:42
@MovieStoreGuy MovieStoreGuy force-pushed the msg/fix-snappy-lazy-read branch from a17556a to ecac030 Compare September 20, 2024 03:21
@MovieStoreGuy MovieStoreGuy force-pushed the msg/fix-snappy-lazy-read branch from ecac030 to 7ff0414 Compare September 20, 2024 04:04
Copy link
Member

@jpkrohling jpkrohling left a comment

Choose a reason for hiding this comment

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

The issue description makes me believe we were susceptible to a compression bomb attack, similar to the one we had with zstd. However, I remember you adding a test to ensure this is not the case. I'm a bit confused about this PR :-) Can you clarify its purpose?

@MovieStoreGuy
Copy link
Contributor Author

MovieStoreGuy commented Sep 23, 2024

The issue description makes me believe we were susceptible to a compression bomb attack, similar to the one we had with zstd. However, I remember you adding a test to ensure this is not the case.

It is technically, but within my local testing the snappy compression ratio wasn't high enough to do it within one request. Performing some local testing, to match the allowed default content size using snappy (snzip) it roughly expanded out to 430Mb which if you're using the recommended size of 2Gb of RAM per core then you'd roughly need 5 requests to be executed within the same time to breach that memory limit. The potential attack requires more of a setup compared to the previous zstd reported CVE and likelihood is less but non-zero.

I had added the tests to help verify that check the read bytes here #11108 but not copied it out to a new PR.

I'm a bit confused about this PR :-) Can you clarify its purpose?

This PR removes the intermediate step copying the compressed buffer from the request into an *bytes.Buffer.
This allows for some time to be saved when trying to consume the data in the receivers since decompression is only performed once it is needed (and behave the same as the other decompressors).

There is also the nice side affect of addressing the above concern since it is each compressed body is wrapped http.MaxBytesReader means snappy will no longer read more than the allow amount of bytes permitted from the original compressed buffer.

Copy link
Member

@jpkrohling jpkrohling left a comment

Choose a reason for hiding this comment

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

Thank you for the explanation, it's now clear to me.

// compressReadCloser couples the original compressed reader
// and the compression reader to ensure that the original body
// is correctly closed to ensure resources are freed.
type compressReadCloser struct {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just readCloser?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mostly wanted to make it clear that the usage is intended for compression.

@dmitryax dmitryax merged commit 1bfe6c3 into open-telemetry:main Sep 25, 2024
49 checks passed
@github-actions github-actions bot added this to the next release milestone Sep 25, 2024
jackgopack4 pushed a commit to jackgopack4/opentelemetry-collector that referenced this pull request Oct 8, 2024
…11177)

#### Description
Moving snappy to lazy read from the original payload instead
decompressing all of the buffer into memory.

This is something I noticed while trying to introduce support for lz4
compression, moved into its own PR at the suggestion of @atoulme.

#### Testing

All of the tests are already present so no additional tests were needed.
HongChenTW pushed a commit to HongChenTW/opentelemetry-collector that referenced this pull request Dec 19, 2024
…11177)

#### Description
Moving snappy to lazy read from the original payload instead
decompressing all of the buffer into memory.

This is something I noticed while trying to introduce support for lz4
compression, moved into its own PR at the suggestion of @atoulme.

#### Testing

All of the tests are already present so no additional tests were needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants