Skip to content

fix(limit): report a bucket stale only when all its items are expired - #5746

Open
amalsaidov wants to merge 1 commit into
prometheus:mainfrom
amalsaidov:fix/bucket-isstale-min-heap
Open

amalsaidov wants to merge 1 commit into
prometheus:mainfrom
amalsaidov:fix/bucket-isstale-min-heap

Conversation

@amalsaidov

Copy link
Copy Markdown

What

Bucket.IsStale decided whether a per-alert limit bucket could be dropped by reading b.items[len-1] as "the latest item". But items is a min-heap ordered by expiry. Only the root is guaranteed to be the earliest-expiring item; the latest-expiring item can sit at any index, so the final slot is just an arbitrary leaf.

Why it matters

When the latest-expiring (active) alert was not at the final heap index, IsStale returned true even though the bucket still tracked a non-expired alert. store.gcLimitBuckets then removed the bucket, resetting the per-alert count for that alertname, so the configured per-alert limit could be exceeded.

It is only a false positive (it never kept a fully expired bucket), but it weakens the limit it is meant to enforce.

Fix

Scan the items and report the bucket stale only when no item is still active. Added a test that inserts the active alert first and two expired alerts after it, so the active alert stays off the last heap slot. The new test fails before the change and passes after; existing limit and store tests still pass.

AI-assisted; directed, reviewed and tested by the author.

Bucket.IsStale read b.items[len-1] to find the latest-expiring item, but
items is a min-heap ordered by expiry. Only the root is guaranteed to be
the earliest-expiring item; the latest-expiring item can sit at any index,
so the last slot is an arbitrary leaf.

As a result a bucket could be reported stale while it still tracked an
active (non-expired) alert whenever that alert was not at the final heap
index. store.gcLimitBuckets then dropped the bucket early, resetting the
per-alert limit count for that alertname and letting the limit be exceeded.

Scan the items instead and treat the bucket as stale only when no item is
still active. Add a test that places the active alert off the last heap
slot, which fails before this change.

Signed-off-by: Amal Saidov <amalsaidov@users.noreply.github.com>
@amalsaidov
amalsaidov requested a review from a team as a code owner November 16, 2026 03:48
@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Bucket.IsStale now checks every item against one captured current time. It returns false if any item is unexpired and true if the bucket is empty or all items are expired. A test covers an active alert that is not the final heap element.

Changes: Bucket staleness check

Layer / File(s) Summary
Scan bucket items
limit/bucket.go, limit/bucket_test.go
IsStale checks every heap item using one captured current time. The test verifies that an active alert keeps the bucket non-stale even when the final heap element is expired.

Estimated code review effort: 2 (Simple) | ~8 minutes

Merge Risk: Minimal · up to d91dd

Bucket staleness is now computed correctly, so buckets that still track active alerts are no longer removed early. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Bucket.IsStale should report a bucket stale only when all its items are expired. It follows the area: description form and matches the summary and objectives, which de…
Description check Passed The description covers the problem, its impact, the fix, and the new test. It explains that the heap's final slot is not the latest-expiring item, and it states the test results. It does not fill in t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Bucket.IsStale should report a bucket stale only when all its items are expired. It follows the area: description form and matches the summary and objectives, which describe the scan-all-items fix in limit/bucket.go.

Full details: Description check

Explanation

The description covers the problem, its impact, the fix, and the new test. It explains that the heap's final slot is not the latest-expiring item, and it states the test results. It does not fill in the template's checklist, the linked issue, the CHANGELOG release-notes block, or the sign-off item. The template asks for these, and the author should complete them. The core content is present, so the check passes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 373edcb and d91dd9e.

Files selected for processing: 2

  • limit/bucket.go
  • limit/bucket_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@TheMeier

Copy link
Copy Markdown
Contributor

/workflow-approve

@jamesconwaydev jamesconwaydev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The diagnosis is right. Read only, didn't run it.

sortedItems.Less orders by priority.Before, so items is a min-heap and only items[0] is placed; the last slot is whatever leaf heap.Push left there. Tracing the new test by hand: push Active (index 0), push Expired1 (-10m) sifts up above it, giving [Expired1, Active], then Expired2 (-5m) stays at index 2 because it isn't before Expired1. So the last slot is expired while Active sits at index 1, which is the case the old IsStale got wrong. The setup assertion guards that.

The red Test job is lint, not the tests: godot flags limit/bucket.go:102, the comment line that starts with lowercase items is a min-heap.... Starting it with a capital (for example "The items heap is a min-heap...") should clear it.

Comment thread limit/bucket_test.go
})

t.Run("IsStale returns false when the active item is not the last heap element", func(t *testing.T) {
// items is a min-heap ordered by expiry, so the item with the latest

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.

Can we avoid the comment in both places?

Comment thread limit/bucket.go
// IsStale returns true if the latest item in the bucket is expired.
// IsStale returns true if every item in the bucket is expired.
//
// items is a min-heap ordered by priority (expiry): only the root is

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.

Maybe we can keep this shorter? Also note that the comment needs to be edited to succeed for CI:

running golangci-lint
/go/bin/golangci-lint run ./...
level=warning msg="[runner/exclusion_rules] Skipped 0 issues by rules: [Text: "SA1019:.types\\.Alert.", Linters: "staticcheck"]"
limit/bucket.go:102:1: Sentence should start with a capital letter (godot)
// items is a min-heap ordered by priority (expiry): only the root is
^
1 issues:

  • godot: 1
    make: *** [Makefile.common:205: common-lint] Error 1
    Error: Process completed with exit code 2.

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