Skip to content

storage: stop histogram conversion on query cancellation - #20288

Merged
bwplotka merged 2 commits into
mainfrom
perfloop-pr-open-h9ch7t7n74
Nov 30, 2026
Merged

bwplotka merged 2 commits into
mainfrom
perfloop-pr-open-h9ch7t7n74

Conversation

@perfloop

@perfloop perfloop Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) does the PR fix:

Follow-up to #18048. Carry the query context into NHCB-to-classic conversion and check it between samples, before materializing output, and when advancing the converted series set. This stops a cancelled query from draining the remaining history, even when le matchers reject every bucket.

Cancellation is cooperative: an in-progress iterator operation, individual histogram conversion or materialization must still finish. This applies with promql-native-histograms-as-classic enabled.

The cancellation tests use named scenarios under separate NHCB and float-NHCB subtests rather than a selector-by-cancellation-point matrix. They cover cancellation before iteration, during conversion, before materialization, and between output series, plus rejected bucket matchers. The PromQL integration test covers cancellation and query timeouts with local TSDB primary and secondary storage. An iterator-error control checks that storage errors still propagate.

Go tests for NHCB-to-classic conversion in storage and its PromQL integration passed with race detection. Uncancelled timing overhead remains inconclusive in local paired measurements; no timing-preservation guarantee is claimed.

Related work: #19881, #19886 and #20277 also change this conversion path; this change addresses cancellation within a single history.

Release notes for end users (ALL commits must be considered).

[BUGFIX] PromQL: Stop native-histogram-to-classic conversion between samples when a query is cancelled or times out.

Case and evidence


Made by Perfloop. To ask for a change, comment or review in plain words. How Perfloop handles pull requests.

@perfloop
perfloop Bot requested review from a team, roidelapluie and vpranckaitis as code owners November 3, 2026 08:02

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

Cancellation is only checked between samples in convertSeries, but this call can process and emit every bucket of a single NHCB before returning. Should cancellation be checked within the bucket conversion loop as well ? Maybe this is enough though

func TestNativeHistogramsAsClassicQuerier_Cancellation(t *testing.T) {
const numSamples = 64
h := makeNHCB([]float64{1, 2}, []int64{1, 2, 1})
for _, floatHistogram := range []bool{false, true} {

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.

The nested loops create a Cartesian product of histogram type, selector, and cancellation point, making it hard to tell which combinations are essential. Could these be expressed as a smaller table of named cancellation scenarios, with NHCB and float-NHCB coverage handled as two clear subtests? Keep separate cases only where a selector exercises distinct cancellation behavior.

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.

Yes, the full Cartesian product is unnecessary: the sample-boundary check is shared by bucket, count and sum conversion. Rejected buckets need a separate scenario because they produce no output, while the other selectors can share the cancellation-boundary scenarios. The current test still uses the nested matrix.

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.

Replaced the Cartesian product with named cancellation scenarios under separate NHCB and float-NHCB subtests. Rejected buckets retain their own scenario because they produce no output; count and sum use existing boundary scenarios rather than repeating every cancellation point.

Comment thread storage/native_histograms_as_classic.go Outdated
return nil
}
series := s.builder.buildSeries(dst, s.leMatchers)
if err := s.ctx.Err(); err != nil {

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.

s.ctx.Err() is already checked right before s.builder.buildSeries on line 501, and buildSeries only copies in-memory slices, so this second check after buildSeries can be dropped.

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.

Agreed. The pre-build cancellation check is sufficient for this cooperative boundary: buildSeries copies the in-memory histories and does not advance the source iterator.

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.

Dropped the post-build check. The cancellation check immediately before buildSeries remains, as do the checks between samples and when advancing the series set.

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-h9ch7t7n74 branch from 7fa1f84 to 6acc6fe Compare November 4, 2026 23:19
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-h9ch7t7n74 branch from 6acc6fe to a89b156 Compare November 5, 2026 05:16

@bwplotka bwplotka left a comment

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.

LGTM, thanks!

@saswatamcode saswatamcode left a comment

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.

Thanks, this makes sense!

@bwplotka
bwplotka merged commit da3157b into main Nov 30, 2026
40 checks passed
@bwplotka
bwplotka deleted the perfloop-pr-open-h9ch7t7n74 branch November 30, 2026 10:12
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.

4 participants