Skip to content

Add WrappedHistogram with size limit before unmarshal#7570

Merged
danielblando merged 1 commit into
cortexproject:masterfrom
PaurushGarg:fix/wrapped-histogram-size-limit
May 29, 2026
Merged

Add WrappedHistogram with size limit before unmarshal#7570
danielblando merged 1 commit into
cortexproject:masterfrom
PaurushGarg:fix/wrapped-histogram-size-limit

Conversation

@PaurushGarg
Copy link
Copy Markdown
Contributor

@PaurushGarg PaurushGarg commented May 29, 2026

What this PR does:
Adds a WrappedHistogram wrapper type (same pattern as PreallocTimeseries) that checks raw protobuf byte size before calling the generated Histogram.Unmarshal. This limits memory allocation during deserialization of packed repeated scalar fields (NegativeDeltas, PositiveDeltas) on the ingestion path.

  • WrappedHistogram wraps Histogram with a custom Unmarshal that rejects payloads exceeding the configured limit before any allocation happens.
  • The proto is updated to use WrappedHistogram as a customtype for the histograms field in both TimeSeries and TimeSeriesV2.
  • Configurable via CLI flag: -validation.max-native-histogram-size-bytes (default: 16KB, 0 to disable).
  • Applied only on the ingestion path, not on the query path.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

@PaurushGarg PaurushGarg force-pushed the fix/wrapped-histogram-size-limit branch from 489220d to 2c5d5a1 Compare May 29, 2026 01:14
@PaurushGarg PaurushGarg changed the title add WrappedHistogram with size limit before unmarshal Add WrappedHistogram with size limit before unmarshal May 29, 2026
@PaurushGarg PaurushGarg force-pushed the fix/wrapped-histogram-size-limit branch 2 times, most recently from 1ecb7ca to e476d78 Compare May 29, 2026 01:49
@PaurushGarg PaurushGarg marked this pull request as ready for review May 29, 2026 02:02
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

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

Thanks!

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 29, 2026
Comment thread pkg/ingester/ingester.go
Comment on lines -1440 to -1454
// Ensure the appender is always released so that we don't leak TSDB head
// series references, mmap'd chunks and pending state on early returns.
// `committed` is flipped to true immediately before app.Commit() because
// Prometheus closes the appender even on Commit failure (it self-rolls
// back internally on WAL error), so the deferred Rollback must not run
// afterwards.
committed := false
defer func() {
if committed {
return
}
if rollbackErr := app.Rollback(); rollbackErr != nil {
level.Warn(logutil.WithContext(ctx, i.logger)).Log("msg", "failed to rollback appender on early return", "user", userID, "err", rollbackErr)
}
}()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This code block seems to have been deleted by mistake.

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.

Yeah seems a mistake from rebase

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this. I copied some code from private to here which removed this. Corrected it now.

Signed-off-by: Paurush Garg <paurushg@amazon.com>
@PaurushGarg PaurushGarg force-pushed the fix/wrapped-histogram-size-limit branch from e476d78 to fee7e0c Compare May 29, 2026 15:56
@PaurushGarg PaurushGarg requested review from SungJin1212 and yeya24 May 29, 2026 15:58
@danielblando danielblando merged commit d4e51d5 into cortexproject:master May 29, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants