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

Fix matcher cache #8039

Merged
merged 2 commits into from
Jan 6, 2025
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
14 changes: 7 additions & 7 deletions pkg/store/cache/matchers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (n *NoopMatcherCache) GetOrSet(key ConversionLabelMatcher, newItem NewItemF
// LruMatchersCache implements MatchersCache with an LRU cache and metrics.
type LruMatchersCache struct {
reg prometheus.Registerer
cache *lru.Cache[ConversionLabelMatcher, *labels.Matcher]
cache *lru.Cache[string, *labels.Matcher]
metrics *matcherCacheMetrics
size int
sf singleflight.Group
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewMatchersCache(opts ...MatcherCacheOption) (*LruMatchersCache, error) {
}
cache.metrics = newMatcherCacheMetrics(cache.reg)

lruCache, err := lru.NewWithEvict[ConversionLabelMatcher, *labels.Matcher](cache.size, cache.onEvict)
lruCache, err := lru.NewWithEvict[string, *labels.Matcher](cache.size, cache.onEvict)
if err != nil {
return nil, err
}
Expand All @@ -89,9 +89,9 @@ func NewMatchersCache(opts ...MatcherCacheOption) (*LruMatchersCache, error) {

func (c *LruMatchersCache) GetOrSet(key ConversionLabelMatcher, newItem NewItemFunc) (*labels.Matcher, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still want ConversionLabelMatcher in the method itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking the same... maybe better to send the key as string... lemme do that

Copy link
Contributor Author

@alanprot alanprot Jan 3, 2025

Choose a reason for hiding this comment

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

Actually, we need as the key is also used as argument of the NewItemFunc. =/ We can change this function to not receive the key as well i think.. but is a bigger change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yeya24 PTAL? tried to simplify the code a bit.

c.metrics.requestsTotal.Inc()

v, err, _ := c.sf.Do(key.String(), func() (interface{}, error) {
if item, ok := c.cache.Get(key); ok {
ks := key.String()
v, err, _ := c.sf.Do(ks, func() (interface{}, error) {
if item, ok := c.cache.Get(ks); ok {
c.metrics.hitsTotal.Inc()
return item, nil
}
Expand All @@ -100,7 +100,7 @@ func (c *LruMatchersCache) GetOrSet(key ConversionLabelMatcher, newItem NewItemF
if err != nil {
return nil, err
}
c.cache.Add(key, item)
c.cache.Add(ks, item)
c.metrics.numItems.Set(float64(c.cache.Len()))
return item, nil
})
Expand All @@ -111,7 +111,7 @@ func (c *LruMatchersCache) GetOrSet(key ConversionLabelMatcher, newItem NewItemF
return v.(*labels.Matcher), nil
}

func (c *LruMatchersCache) onEvict(_ ConversionLabelMatcher, _ *labels.Matcher) {
func (c *LruMatchersCache) onEvict(_ string, _ *labels.Matcher) {
c.metrics.evicted.Inc()
c.metrics.numItems.Set(float64(c.cache.Len()))
}
Expand Down
22 changes: 15 additions & 7 deletions pkg/store/cache/matchers_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ func TestMatchersCache(t *testing.T) {
expected2 := labels.MustNewMatcher(labels.MatchRegexp, "key2", "val2|val3")
expected3 := labels.MustNewMatcher(labels.MatchEqual, "key3", "val3")

item, err := cache.GetOrSet(matcher, newItem)
item, err := cache.GetOrSet(cloneMatcher(matcher), newItem)
testutil.Ok(t, err)
testutil.Equals(t, false, cacheHit)
testutil.Equals(t, expected.String(), item.String())

cacheHit = true
item, err = cache.GetOrSet(matcher, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher), newItem)
testutil.Ok(t, err)
testutil.Equals(t, true, cacheHit)
testutil.Equals(t, expected.String(), item.String())

cacheHit = true
item, err = cache.GetOrSet(matcher2, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher2), newItem)
testutil.Ok(t, err)
testutil.Equals(t, false, cacheHit)
testutil.Equals(t, expected2.String(), item.String())

cacheHit = true
item, err = cache.GetOrSet(matcher2, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher2), newItem)
testutil.Ok(t, err)
testutil.Equals(t, true, cacheHit)
testutil.Equals(t, expected2.String(), item.String())

cacheHit = true
item, err = cache.GetOrSet(matcher, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher), newItem)
testutil.Ok(t, err)
testutil.Equals(t, true, cacheHit)
testutil.Equals(t, expected, item)

cacheHit = true
item, err = cache.GetOrSet(matcher3, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher3), newItem)
testutil.Ok(t, err)
testutil.Equals(t, false, cacheHit)
testutil.Equals(t, expected3, item)

cacheHit = true
item, err = cache.GetOrSet(matcher2, newItem)
item, err = cache.GetOrSet(cloneMatcher(matcher2), newItem)
testutil.Ok(t, err)
testutil.Equals(t, false, cacheHit)
testutil.Equals(t, expected2.String(), item.String())
Expand Down Expand Up @@ -110,3 +110,11 @@ func BenchmarkMatchersCache(b *testing.B) {
}
}
}

func cloneMatcher(m *storepb.LabelMatcher) *storepb.LabelMatcher {
return &storepb.LabelMatcher{
Type: m.Type,
Name: m.Name,
Value: m.Value,
}
}
Loading