Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp/metrics: evict only stale #33265

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
exp/metrics: evict only stale
fixes broken check in eviction behavior such that it only evicts
actually stale metrics
  • Loading branch information
sh0rez committed May 28, 2024
commit c7722036066c834d12b67f9870656c73e597f78d
2 changes: 1 addition & 1 deletion internal/exp/metrics/staleness/staleness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
36 changes: 36 additions & 0 deletions internal/exp/metrics/staleness/staleness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,39 @@ 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"})
stale.Store(idA, 0)

now = 20
idB := generateStreamID(t, map[string]any{"bbb": "456"})
stale.Store(idB, 1)

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())
}
Loading