diff --git a/.chloggen/fix-staleness-evict.yaml b/.chloggen/fix-staleness-evict.yaml new file mode 100644 index 0000000000000..00494faf0851b --- /dev/null +++ b/.chloggen/fix-staleness-evict.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: exp/metrics + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: fixes staleness.Evict such that it only ever evicts actually stale metrics + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [33265] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user,api] diff --git a/internal/exp/metrics/staleness/staleness.go b/internal/exp/metrics/staleness/staleness.go index 975e7750a4460..dae1870cbfd60 100644 --- a/internal/exp/metrics/staleness/staleness.go +++ b/internal/exp/metrics/staleness/staleness.go @@ -90,7 +90,7 @@ func (s *Staleness[T]) Next() time.Time { func (s *Staleness[T]) Evict() (identity.Stream, bool) { _, ts := s.pq.Peek() - if ts.Add(s.Max).Before(time.Now()) { + if NowFunc().Sub(ts) < s.Max { return identity.Stream{}, false } diff --git a/internal/exp/metrics/staleness/staleness_test.go b/internal/exp/metrics/staleness/staleness_test.go index 98fec276238e1..9d0b2a4ada30b 100644 --- a/internal/exp/metrics/staleness/staleness_test.go +++ b/internal/exp/metrics/staleness/staleness_test.go @@ -92,3 +92,41 @@ func validateStalenessMapEntries(t *testing.T, expected map[identity.Stream]int, }) require.Equal(t, expected, actual) } + +func TestEvict(t *testing.T) { + now := 0 + NowFunc = func() time.Time { + return time.Unix(int64(now), 0) + } + + stale := NewStaleness(1*time.Minute, make(streams.HashMap[int])) + + now = 10 + idA := generateStreamID(t, map[string]any{"aaa": "123"}) + err := stale.Store(idA, 0) + require.NoError(t, err) + + now = 20 + idB := generateStreamID(t, map[string]any{"bbb": "456"}) + err = stale.Store(idB, 1) + require.NoError(t, err) + + require.Equal(t, 2, stale.Len()) + + // nothing stale yet, must not evict + _, ok := stale.Evict() + require.False(t, ok) + require.Equal(t, 2, stale.Len()) + + // idA stale + now = 71 + gone, ok := stale.Evict() + require.True(t, ok) + require.NotZero(t, gone) + require.Equal(t, 1, stale.Len()) + + // idB not yet stale + _, ok = stale.Evict() + require.False(t, ok) + require.Equal(t, 1, stale.Len()) +}