Skip to content

storage: resolve classic and NHCB histogram collisions in NativeHistogramsAsClassicQuerier - #19881

Open
bwplotka wants to merge 15 commits into
mainfrom
bwplotka/pr-18048-resolve-collisions
Open

bwplotka wants to merge 15 commits into
mainfrom
bwplotka/pr-18048-resolve-collisions

Conversation

@bwplotka

@bwplotka bwplotka commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Part of #16948. Follow-up to #18048, addressing #18048 review feedback.

Resolves collisions, cutover transitions, staleness propagation, and sortSeries ordering between stored classic histograms and converted NHCB series in NativeHistogramsAsClassicQuerier:

  • Group stored classic and NHCB series by histogram identity, meaning all labels except __name__ and le, when both representations exist for a queried metric, while keeping pure-classic and pure-NHCB selections streaming.
  • Shadow converted NHCB samples across the histogram group at timestamps where a stored classic histogram has a live sample so bucket layout or le formatting differences such as "1" versus OpenMetrics "1.0" do not produce duplicate or hybrid buckets.
  • Merge disjoint time ranges across classic and NHCB cutovers or rollbacks into a single continuous Series while preserving start timestamps, preferring live samples over StaleNaN at the cutover timestamp, and dropping delayed staleness markers from the old representation once the new one is active.
  • Propagate StaleNaN onto active converted classic series when an NHCB sample is stale, drops a custom bucket boundary, transitions to an exponential schema or float sample, or yields to stored classic.
  • Ignore non-histogram _bucket series without an le label during shadowing and skip base series whose first live sample is not NHCB.
  • Push le matchers down on the classic Select widened with +Inf via withInfBucket so selective queries fetch at most one extra series per histogram instead of all buckets.
  • Open Series.Iterator at most once per input storage.Series across both nhcbSet and classicSet via peekNHCBSeries and collectClassicGroup for streaming Queryable implementations.
  • Sort output series globally by labels.Compare when sortSeries=true and per hybrid group when sortSeries=false.

Streaming and buffering

After peeking one candidate series from each SeriesSet in selectNHCBAsClassic, the querier takes one of four paths:

Query scenario SeriesSet streaming Sample materialization Why
1. Pure classic, no live NHCB series 100% streaming, classicSet returned directly Zero sample buffering Keeps le index pushdown and returns the underlying storage SeriesSet untouched.
2. Pure NHCB with sortSeries=false or _count/_sum 100% streaming per NHCB series in nhcbToClassicSeriesSet.Next 1 NHCB series at a time _count and _sum add no le label, preserving global labels.Compare order even when sortSeries=true. Local PromQL passes sortSeries=false and expands one NHCB series into its $B+1$ bucket series on demand without sorting.
3. Hybrid colliding metric with sortSeries=false Grouped by Series handle via histogramIndex, converted 1 group at a time on Next() 1 histogram group at a time classicSet and nhcbSet cannot co-stream in lockstep because TSDB iterates internal series-ref order when sortSeries=false, and le sorts before label names above "le" when sortSeries=true. Sample iterators are opened lazily once per group.
4. _bucket with sortSeries=true Materialized and sorted across groups Materialized across groups Used by NewMergeQuerier with remote_read. Global labels.Compare order interleaves buckets across different NHCB series when label names sort after "le".

Commits

  1. storage: resolve classic and NHCB histogram collisions in NativeHistogramsAsClassicQuerier: collision grouping, shadowing, cutover and rollback merging, staleness propagation, sortSeries handling, and feature flag docs.
  2. storage: keep le matchers pushed down in NativeHistogramsAsClassicQuerier classic Select: widens pushed-down le matchers with +Inf so selective queries do not scan all stored buckets.
  3. storage: avoid calling Series.Iterator multiple times in NativeHistogramsAsClassicQuerier: single-pass iterator consumption for both NHCB and classic series.

Benchmarks

go test ./storage -run '^$' -bench '^BenchmarkNativeHistogramsAsClassicSelect' -benchmem -count=6 -cpu=2 comparing main (58719dfa4) vs pr (30ba5b867) over 1,000 NHCB series with 30 custom buckets and 6 samples per series:

benchstat output
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/storage
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
                                         │    main     │                 pr                 │
                                         │   sec/op    │    sec/op     vs base              │
NativeHistogramsAsClassicSelect_Bucket-2   21.72m ± 2%   22.75m ± 17%  +4.72% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Count-2    3.056m ± 1%   3.242m ±  3%  +6.09% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Sum-2      3.058m ± 1%   3.156m ±  5%  +3.22% (p=0.002 n=6)
geomean                                    5.877m        6.151m        +4.67%

                                         │     main     │                 pr                 │
                                         │     B/op     │     B/op      vs base              │
NativeHistogramsAsClassicSelect_Bucket-2   10.96Mi ± 0%   10.96Mi ± 0%  +0.01% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Count-2    380.1Ki ± 0%   380.8Ki ± 0%  +0.18% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Sum-2      380.1Ki ± 0%   380.8Ki ± 0%  +0.18% (p=0.002 n=6)
geomean                                    1.147Mi        1.149Mi       +0.12%

                                         │    main     │                pr                 │
                                         │  allocs/op  │  allocs/op   vs base              │
NativeHistogramsAsClassicSelect_Bucket-2   33.18k ± 0%   33.18k ± 0%  -0.01% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Count-2    3.031k ± 0%   3.034k ± 0%  +0.10% (p=0.002 n=6)
NativeHistogramsAsClassicSelect_Sum-2      3.031k ± 0%   3.034k ± 0%  +0.10% (p=0.002 n=6)
geomean                                    6.730k        6.734k       +0.06%
[ENHANCEMENT] PromQL: Handle classic and NHCB histogram collisions, migration cutovers, and staleness under `--enable-feature=promql-native-histograms-as-classic`.

@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from ce699ac to 6594bd4 Compare October 1, 2026 15:42
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch 3 times, most recently from 20cda53 to 54dcda7 Compare October 1, 2026 16:25
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from 54dcda7 to ddffdd6 Compare October 1, 2026 16:34
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from ddffdd6 to e867ff5 Compare October 1, 2026 21:28
@bwplotka
bwplotka marked this pull request as ready for review October 1, 2026 21:34
@bwplotka
bwplotka requested a review from a team as a code owner October 1, 2026 21:34
@bwplotka
bwplotka requested review from RichiH and removed request for a team October 1, 2026 21:34
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from e867ff5 to ddffdd6 Compare October 1, 2026 21:35
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from ddffdd6 to 9318262 Compare October 1, 2026 21:59
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from 9318262 to cec2995 Compare October 1, 2026 22:11
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from cec2995 to d69edda Compare October 1, 2026 22:22
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch 2 times, most recently from 3b20140 to d3894fd Compare October 3, 2026 14:12
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from d3894fd to e4dfbb5 Compare October 4, 2026 12:13
@bwplotka
bwplotka changed the base branch from bwplotka/pr-18048-streaming-perf to main October 6, 2026 05:13
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from c54dd0e to af3db9a Compare November 3, 2026 08:31
@bwplotka bwplotka changed the title storage: resolve classic and NHCB histogram collisions in NHCBAsClassicQuerier storage: resolve classic and NHCB histogram collisions in NativeHistogramsAsClassicQuerier Nov 3, 2026
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from af3db9a to f55631c Compare November 5, 2026 05:11
@bwplotka
bwplotka added this pull request to stack #20333 November 5, 2026 07:04

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This should be now good to go @rbizos !

@bwplotka
bwplotka marked this pull request as ready for review November 5, 2026 07:09
Comment thread storage/native_histograms_as_classic.go Resolved

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

storage/native_histograms_as_classic_test.go: The shadow branch at native_histograms_as_classic.go:1003 (classic sample strictly between two NHCB samples) is exercised by "disjoint timestamps across forward migration cutover" but only with matching labels, so the stale it emits is discarded by mergeSamples. There is no test where that stale must survive because classic doesn't carry the bucket, which is the core reason the shadow exists (lookback-leak / partial-bucket prevention the merge can't handle). Consider a case: NHCB le=5 at t=1 and t=3, classic (no le=5) at t=2, expecting le=5: {t=1,f},{t=2,STALE},{t=3,f}.

Comment thread storage/native_histograms_as_classic.go Resolved
Comment thread storage/native_histograms_as_classic.go Resolved
Comment thread storage/native_histograms_as_classic.go Resolved
bwplotka and others added 15 commits November 19, 2026 09:20
…gramsAsClassicQuerier

Group stored classic and NHCB series by histogram identity when both representations exist for a metric, while keeping pure-classic and pure-NHCB paths streaming.

- Shadow converted NHCB samples at timestamps where a stored classic histogram has a live sample across the histogram group, avoiding hybrid bucket layouts and duplicate series.
- Merge disjoint time ranges across classic and NHCB cutovers or rollbacks into a single continuous Series while preserving start timestamps, preferring live samples over StaleNaN at the cutover timestamp, and dropping delayed staleness markers from the old representation.
- Propagate StaleNaN onto active converted classic series when an NHCB sample is stale, drops a custom bucket boundary, transitions to an exponential schema or float sample, or yields to stored classic.
- Ignore non-histogram _bucket series without an le label during shadowing and skip base series whose first sample is not NHCB.
- Sort output series by labels.Compare per group when sortSeries is false and globally across groups when sortSeries is true.

Signed-off-by: Bartek Plotka <[email protected]>
…rier classic Select

Dropping le matchers on the classic Select fetched all B+1 bucket series and filtered client-side for selective queries such as foo_bucket{le="+Inf"} or foo_bucket{le="0.5"}, including on the pure-classic fast path when no NHCB series exist.

Shadowing only needs the timestamps at which a stored classic histogram has a live sample, and every classic histogram has a +Inf bucket. Push le matchers down widened to also match +Inf so selective queries fetch at most one extra series per histogram while keeping the original le filter client-side. Matchers that cannot be widened such as le!="+Inf" remain client-side.

Signed-off-by: Bartek Plotka <[email protected]>
…ramsAsClassicQuerier

Streaming Queryable implementations such as Mimir do not allow calling Series.Iterator more than once on the same storage.Series.

- Carry the iterator and first live NHCB sample from peekNHCBSeries directly into convertPeekedNHCB, and inspect subsequent candidate series lazily on conversion so each NHCB Series.Iterator is opened at most once.
- Materialize matching classic float samples into a reusable slab during collectClassicGroup when resolving a hybrid collision group so stored classic Series.Iterator is also opened at most once while pure-classic and pure-NHCB paths stay zero-copy.

Signed-off-by: Bartek Plotka <[email protected]>
…stogramsAsClassicQuerier

Check ctx.Err() inside selectNHCBAsClassic and nhcbToClassicSeriesSet.Next loops so cancelled queries abort promptly when skipping large numbers of non-NHCB series or indexing classic/NHCB groups.

Signed-off-by: Bartek Plotka <[email protected]>
…ClassicQuerier

Cover multi-sample FloatHistogram bucket/count/sum conversion, start timestamp preservation, staleness and schema transitions, and classic/NHCB collisions.

Signed-off-by: Bartek Plotka <[email protected]>
Verify leFilterSeriesSet strips widened +Inf series on the pure-classic fast path and test warning propagation across all four selectNHCBAsClassic execution paths.

Signed-off-by: Bartek Plotka <[email protected]>
…eMatchers test

Verify actual sample values (confirming t=1 NHCB buckets are shadowed via widened +Inf pushdown) and cover MatchNotRegexp and extra non-le matchers.

Signed-off-by: Bartek Plotka <[email protected]>
Invert the fallback loop guard in mergeSeriesByLabels to satisfy gocritic nestingReduce.

Signed-off-by: Bartek Plotka <[email protected]>
…CB samples

Verify that shadowing an active NHCB bucket missing from the stored classic histogram emits a StaleNaN at the intermediate classic timestamp that survives merging.

Signed-off-by: Bartek Plotka <[email protected]>
…Querier

Series.Iterator returns a non-nil chunkenc.Iterator across Prometheus storage implementations (using chunkenc.NewNopIterator when empty), so drop the unused nil checks in peekNHCBSeries, collectClassicGroup, mergeSamples, and nextFloat.

Signed-off-by: Bartek Plotka <[email protected]>
Explain in convertPeekedNHCB's doc comment why shadowing emits StaleNaN even when matching labels will override it in mergeSamples: surviving StaleNaN prevents PromQL lookback from leaking converted NHCB buckets absent from the stored classic histogram.

Signed-off-by: Bartek Plotka <[email protected]>
…lectClassicGroup

Document why collectClassicGroup only buffers native histogram samples while no float sample has been seen (only pure non-float series are preserved via nonFloatClassicSeries) and cover the float-then-histogram ordering in unit tests.

Signed-off-by: Bartek Plotka <[email protected]>
@bwplotka
bwplotka force-pushed the bwplotka/pr-18048-resolve-collisions branch from f55631c to 30ba5b8 Compare November 19, 2026 09:32

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed, all thanks for the detailed review @rbizos !

Comment thread storage/native_histograms_as_classic.go Resolved
Comment thread storage/native_histograms_as_classic.go Resolved
Comment thread storage/native_histograms_as_classic.go Resolved
Comment thread storage/native_histograms_as_classic.go Resolved

@rbizos rbizos 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

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.

3 participants