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

chore(dataobj): reduce memory overhead of merging logs stripes #16833

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

rfratto
Copy link
Member

@rfratto rfratto commented Mar 19, 2025

Merging logs stripes was causing OOMs at scale. We found this to be for two main reasons:

  • Zstd decoders were not always being released back into the pool. Creating new decoders is expensive, so releasing them back into the pool will help relieve memory pressure.

  • Merging all stripes at once has a significant overhead.

This PR helps ensure that all Zstd decoders are released back into the pool and can be reused, and additionally adds a new config option to limit how many stripes are merged at once when forming a section, allowing operators to trade off between total flush time and total memory overhead.

rfratto added 6 commits March 19, 2025 10:11
Previously, pageReader instances would wait until the next call to
pageReader.init to release any old resources, and even then it would
only do it after creating a new reader (which means that the new reader
wouldn't have access to the previous reader in the pool).

This change allows pageReaders to proactively release Zstd decoders upon
hitting EOF, and ensures that readers are closed before allocating new
ones.
When calling basicReader.Reset and shrinking the number of columns, the
old columns readers will be zeroed out and available for garbage
collection. However, they are not closed, which means that they must
wait until GC runs to release any resources like Zstd decoders.

Closing them before clearing the remainder of the slice ensures that
resources get released back to the pool and are available for reuse
rather than just letting the GC clear them up.
Closing readers is required to guarantee that all resources are returned
to pools, specifically for Zstd decoders. Without a Close call, some
readers may be GC'd directly without having a chance to be reentered
into the decoder pool.
While the loser tree guarantees that all sequences are closed if the
entire input sequence is consumed, there are cases where the entire
sequence _isn't_ consumed (like on encountering an error).

For safety, we now close the loser tree on a defer to make sure that any
resources associated with the tree are released.
Closing StreamsReader and LogsReaders helps release resources for Zstd
decocders back into the pool.
This adds two metrics:

* loki_dataobj_dataset_zstd_decoders_created_total
* loki_dataobj_dataset_zstd_decoders_closed_total

If zstdPool is being used properly, we would expect the rate of changes
to loki_dataobj_dataset_zstd_decoders_created_total to be low (but not
zero, due to pooled resources being garbage collected eventually).

Similarly, if runtime.AddCleanup is being used properly, we would expect
loki_dataobj_dataset_zstd_decoders_closed_total to be non-zero.
@pull-request-size pull-request-size bot added size/L and removed size/M labels Mar 19, 2025
Update logs.Logs.flushSection to create the final section by doing
several passes over the stripes, merging a group of stripes at a time
until there is only one stripe remaining, which then becomes the
section.

This approach increases the flush time but lowers memory usage; as the
loser tree merge requires opening one decompresser per column per
stripe, the more stripes there are, the higher the memory usage of
merging tables will be.

The maximum number of stripes to merge at once can be configured with a
new SectionStripeMergeLimit option. This value must be greater than 1.
@rfratto rfratto force-pushed the dataobj-zstd-decoder-improvements branch from 91ab242 to f6ef9ae Compare March 21, 2025 14:51
@rfratto rfratto changed the title chore(dataobj): Various improvements to releasing Zstd decoders chore(dataobj): reduce memory overhead of merging logs stripes Mar 21, 2025
@rfratto rfratto marked this pull request as ready for review March 21, 2025 14:56
@rfratto rfratto requested a review from a team as a code owner March 21, 2025 14:56
@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant