Skip to content

fix: replace EncodeAll with streaming encoder in CDC chunk storage - #1065

Merged
kalbasit merged 1 commit into
mainfrom
fix-replace-encodeall-with
Mar 11, 2026
Merged

kalbasit merged 1 commit into
mainfrom
fix-replace-encodeall-with

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

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.

@kalbasit

kalbasit commented Mar 11, 2026 •

Copy link
Copy Markdown
Owner Author

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels Mar 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 significantly optimizes memory usage in the CDC chunk storage by refactoring the ZSTD compression mechanism. It addresses a critical issue where the previous EncodeAll method led to unbounded heap growth due to constant re-allocation of internal encoder buffers. By switching to a streaming NewPooledWriter API, the system now efficiently reuses encoder states, drastically reducing memory footprint during chunk writing operations and improving overall stability. The PR also mentions the intent to add a pprof HTTP server for live profiling, though the code for this is not included in the provided diffs.

Highlights

  • Memory Optimization: Replaced the zstd.Writer.EncodeAll method with the zstd.NewPooledWriter streaming API in both local and S3 chunk storage to prevent unbounded heap growth caused by repeated internal encoder allocations.
  • Streaming Compression: Implemented a streaming compression approach using Reset+Write+Close with pooled ZSTD encoders, which allows for efficient reuse of the encoder's internal state across calls.
  • Accurate Size Calculation: Updated the local chunk storage to accurately calculate the compressed file size by using os.Stat on the temporary file, rather than relying on the len of the compressed byte slice, which is no longer directly available with the streaming approach.
Changelog
  • pkg/storage/chunk/local.go
    • Replaced the direct zstd.GetWriter().EncodeAll call with zstd.NewPooledWriter for streaming compression to a temporary file.
    • Modified the calculation of compressedSize to use os.Stat on the temporary file after writing, ensuring accurate size reporting.
  • pkg/storage/chunk/s3.go
    • Replaced the direct zstd.GetWriter().EncodeAll call with zstd.NewPooledWriter for streaming compression into a bytes.Buffer before uploading to S3.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a significant memory leak by replacing EncodeAll with a streaming zstd encoder. The changes in both local.go and s3.go correctly adopt the NewPooledWriter for streaming compression, which should resolve the unbounded heap growth. All suggested code review comments were dropped as they contradicted the repository's rule to prioritize consistency with existing code patterns over adopting alternative patterns for readability.

@kalbasit
kalbasit force-pushed the fix-replace-encodeall-with branch from a796f69 to aa51bb5 Compare March 11, 2026 06:22
Base automatically changed from feat-implement-a-pprof-server-to to main March 11, 2026 06:22
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 11, 2026
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.
@kalbasit
kalbasit force-pushed the fix-replace-encodeall-with branch from aa51bb5 to 09f3129 Compare March 11, 2026 06:23
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Mar 11, 2026
@kalbasit
kalbasit enabled auto-merge (squash) March 11, 2026 06:26
@kalbasit kalbasit added the backport release-0.9 Backport PR automatically label Mar 11, 2026
@codecov

codecov Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (1f9c6d8) to head (09f3129).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1065   +/-   ##
=====================================
  Coverage   5.04%   5.04%           
=====================================
  Files          6       6           
  Lines        436     436           
=====================================
  Hits          22      22           
  Misses       410     410           
  Partials       4       4           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 7620270 into main Mar 11, 2026
18 checks passed
@kalbasit
kalbasit deleted the fix-replace-encodeall-with branch March 11, 2026 06:36
kalbasit added a commit that referenced this pull request Mar 11, 2026
…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)
@kalbasit

Copy link
Copy Markdown
Owner Author

Successfully created backport PR for release-0.9:

kalbasit added a commit that referenced this pull request Mar 11, 2026
…ackport #1065] (#1067)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport release-0.9 Backport PR automatically bug Something isn't working go Pull requests that update go code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant