Repository navigation
fix(limit): report a bucket stale only when all its items are expired - #5746
amalsaidov wants to merge 1 commit into
Conversation
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>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
/workflow-approve |
jamesconwaydev
left a comment
There was a problem hiding this comment.
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.
| }) | ||
|
|
||
| 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 |
There was a problem hiding this comment.
Can we avoid the comment in both places?
| // 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 |
There was a problem hiding this comment.
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.
What
Bucket.IsStaledecided whether a per-alert limit bucket could be dropped by readingb.items[len-1]as "the latest item". Butitemsis 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,
IsStalereturned true even though the bucket still tracked a non-expired alert.store.gcLimitBucketsthen 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
limitandstoretests still pass.AI-assisted; directed, reviewed and tested by the author.