Skip to content

tsdb/chunkenc: take a generic Appender as the prev parameter - #18857

Merged
krajorama merged 1 commit into
mainfrom
chunkenc/generic-appender-prev
Jun 9, 2026
Merged

krajorama merged 1 commit into
mainfrom
chunkenc/generic-appender-prev

Conversation

@krajorama

Copy link
Copy Markdown
Member

Summary

Today the chunkenc.Appender interface's AppendHistogram and AppendFloatHistogram methods take a typed previous appender (*HistogramAppender / *FloatHistogramAppender). Callers must type-assert s.app to that concrete type before every call, and silently discard the cross-chunk counter-reset signal whenever the running chunk happens to be a different concrete histogram-appender type.

This PR changes the parameter to the generic chunkenc.Appender interface and moves the concrete type check inside each implementation, onto the code path that actually needs the previous appender's state (the new-chunk branch in setCounterResetHeader). The type check goes through two new private interfaces (histogramAppendable, floatHistogramAppendable); any appender that exposes the right appendable() method satisfies them, and any appender that doesn't is silently ignored.

The cost of the type assertion is paid only on the new-chunk branch — i.e. once per chunk boundary, not per sample.

Motivation

Preparation for #18609, which introduces *HistogramSTAppender and *FloatHistogramSTAppender. Both embed their non-ST counterparts, so they automatically satisfy the new internal interfaces and can be handed off as prev without any caller-side knowledge of the ST variant.

It also removes a small foot-gun: the current prev, _ := s.app.(*chunkenc.HistogramAppender) discards the cross-chunk reset signal whenever s.app is any other concrete type (e.g. an xor chunk that's about to be followed by a histogram chunk, or — once #18609 lands — a *HistogramSTAppender).

Diff shape

  • tsdb/chunkenc/appendable.go (new): histogramAppendable / floatHistogramAppendable interfaces.
  • tsdb/chunkenc/chunk.go: Appender.AppendHistogram / AppendFloatHistogram take prev Appender.
  • tsdb/chunkenc/{histogram,float_histogram}.go: the new-chunk branch type-asserts to the appropriate appendable interface.
  • tsdb/chunkenc/{xor,xor2,histogram,float_histogram}.go: panic stubs for cross-kind appends updated to the new signature.
  • tsdb/{head_append,ooo_head}.go: drop the pre-call type assertion; pass s.app (or prevApp) through as chunkenc.Appender.

storage/series.go and tsdb/querier.go already pass nil for prev and are unaffected.

Test plan

  • go build ./...
  • go vet ./tsdb/...
  • go test ./tsdb/... — all packages green (full suite, no -short).
  • Rebase #18609 on top of this and remove the now-unneeded TODOs in tsdb/ooo_head.go for the ST chunk types (follow-up).
NONE

Coded with Claude Opus 4.7.

The chunkenc.Appender interface's AppendHistogram and AppendFloatHistogram
methods used to require a typed previous appender (*HistogramAppender or
*FloatHistogramAppender). Callers were forced to type-assert s.app to that
concrete type before each call, discarding it (and the cross-chunk
counter-reset signal it carries) whenever the actual concrete type didn't
match -- for example when a chunk used a different histogram encoding.

Change the signature to accept the generic chunkenc.Appender interface and
move the concrete-type check inside each implementation, onto the code path
that actually needs the previous appender's state (the new-chunk branch
where setCounterResetHeader runs). The check goes through small private
interfaces -- histogramAppendable and floatHistogramAppendable -- so any
appender type that exposes the appropriate appendable() method can serve as
prev, and types that don't (xor, xor2, or a histogram appender on the
opposite-kind code path) are silently ignored.

This prepares the ground for #18609, which introduces *HistogramSTAppender
and *FloatHistogramSTAppender. Both embed their non-ST counterparts and
will satisfy the new interfaces automatically, so they can be passed as
prev without a special case in the caller.

Callers in tsdb/head_append.go and tsdb/ooo_head.go are simplified
accordingly. The Appender consumers in storage/series.go and tsdb/querier.go
were already passing nil and continue to do so unchanged.

Signed-off-by: György Krajcsovits <[email protected]>
@krajorama
krajorama force-pushed the chunkenc/generic-appender-prev branch from 9ab4029 to 51d2458 Compare June 8, 2026 11:00
@krajorama
krajorama marked this pull request as ready for review June 8, 2026 11:00
@krajorama
krajorama requested review from a team, bwplotka, codesome and jesusvazquez as code owners June 8, 2026 11:00

@carrieedwards carrieedwards 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.

LGTM!

@krajorama
krajorama merged commit 2aefd00 into main Jun 9, 2026
59 of 60 checks passed
@krajorama
krajorama deleted the chunkenc/generic-appender-prev branch June 9, 2026 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants