Repository navigation
fix: replace EncodeAll with streaming encoder in CDC chunk storage [backport #1065] - #1067
Conversation
…1065) EncodeAll allocates a fresh internal encoder (with a 128MB+ history buffer) on every call, regardless of the receiver object. This meant that even though the encoder was obtained from sync.Pool, the actual compression work was done by a new internally-allocated encoder each time — causing unbounded heap growth proportional to the number of chunks written (confirmed via pprof heap comparison: 363MB growth per batch before fix). Switch both local and S3 chunk stores to the streaming API: NewPooledWriter → Write → Close. This reuses the encoder's existing internal state across calls, which is what sync.Pool is designed for. Also add a dedicated pprof HTTP server behind the --pprof-addr flag (default disabled) to enable heap and goroutine profiling during live operation. (cherry picked from commit 7620270)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the Zstd compression mechanism used in both local and S3 chunk storage to utilize a streaming API. This change is crucial for optimizing memory usage by preventing the repeated allocation of large internal buffers, which previously led to unbounded heap growth. By adopting the streaming approach, the system now efficiently reuses encoder states, significantly reducing memory footprint during chunk compression operations. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a memory leak by replacing zstd.EncodeAll with a streaming encoder, which will prevent unbounded heap growth. The changes in pkg/storage/chunk/local.go and pkg/storage/chunk/s3.go correctly implement this fix using zstd.NewPooledWriter. My review includes a couple of suggestions to improve the readability and conciseness of the new error handling logic for the streaming writers.
| if _, err = pw.Write(data); err == nil { | ||
| err = pw.Close() | ||
| } else { | ||
| _ = pw.Close() | ||
| } |
There was a problem hiding this comment.
This block for writing data and closing the pooled writer can be simplified. While the current logic is correct in how it preserves the first error, it can be expressed more concisely in a way that is more idiomatic in Go.
_, err = pw.Write(data)
if closeErr := pw.Close(); err == nil {
err = closeErr
}| if _, err = pw.Write(data); err == nil { | ||
| err = pw.Close() | ||
| } else { | ||
| _ = pw.Close() | ||
| } |
There was a problem hiding this comment.
This block for writing data and closing the pooled writer can be simplified. While the current logic is correct in how it preserves the first error, it can be expressed more concisely in a way that is more idiomatic in Go.
| if _, err = pw.Write(data); err == nil { | |
| err = pw.Close() | |
| } else { | |
| _ = pw.Close() | |
| } | |
| if _, err = pw.Write(data); err != nil { | |
| _ = pw.Close() | |
| } else { | |
| err = pw.Close() | |
| } |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1067 +/- ##
==============================================
+ Coverage 5.04% 6.38% +1.33%
==============================================
Files 6 6
Lines 436 329 -107
==============================================
- Hits 22 21 -1
+ Misses 410 304 -106
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bot-based backport to
release-0.9, triggered by a label in #1065.EncodeAll allocates a fresh internal encoder (with a 128MB+ history
buffer) on every call, regardless of the receiver object. This meant
that even though the encoder was obtained from sync.Pool, the actual
compression work was done by a new internally-allocated encoder each
time — causing unbounded heap growth proportional to the number of
chunks written (confirmed via pprof heap comparison: 363MB growth per
batch before fix).
Switch both local and S3 chunk stores to the streaming API:
NewPooledWriter → Write → Close. This reuses the encoder's existing
internal state across calls, which is what sync.Pool is designed for.
Also add a dedicated pprof HTTP server behind the --pprof-addr flag
(default disabled) to enable heap and goroutine profiling during
live operation.