Skip to content

fix: replace EncodeAll with streaming encoder in CDC chunk storage [backport #1065] - #1067

Merged
kalbasit merged 1 commit into
release-0.9from
backport-1065-to-release-0.9
Mar 11, 2026
Merged

kalbasit merged 1 commit into
release-0.9from
backport-1065-to-release-0.9

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

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.

…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
kalbasit enabled auto-merge (squash) March 11, 2026 06:36
@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 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

  • Memory Optimization: Switched Zstd compression from EncodeAll to a streaming API (NewPooledWriter, Write, Close) in chunk storage to prevent unbounded heap growth caused by repeated internal buffer allocations.
  • Streaming Compression: Implemented streaming compression for both local and S3 chunk storage, allowing the reuse of encoder states and significantly reducing memory footprint.
  • Compressed Size Calculation: Updated the method for calculating the size of compressed data in local storage to accurately reflect the streamed output.
Changelog
  • pkg/storage/chunk/local.go
    • Replaced EncodeAll with NewPooledWriter, Write, and Close for Zstd compression.
    • Updated the method for calculating the size of compressed data by using os.Stat on the temporary file.
  • pkg/storage/chunk/s3.go
    • Replaced EncodeAll with NewPooledWriter, Write, and Close for Zstd compression, writing to an in-memory buffer.
Activity
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 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.

Comment on lines +163 to +167
if _, err = pw.Write(data); err == nil {
err = pw.Close()
} else {
_ = pw.Close()
}

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.

medium

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
	}

Comment thread pkg/storage/chunk/s3.go
Comment on lines +211 to +215
if _, err = pw.Write(data); err == nil {
err = pw.Close()
} else {
_ = pw.Close()
}

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.

medium

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.

Suggested change
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

codecov Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 6.38%. Comparing base (304dc3f) to head (cfdf4e5).
⚠️ Report is 1 commits behind head on release-0.9.

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.
📢 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 7225a3c into release-0.9 Mar 11, 2026
13 checks passed
@kalbasit
kalbasit deleted the backport-1065-to-release-0.9 branch March 11, 2026 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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