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

Define a reason why the fifocache has evicted an item #6335

Merged
merged 4 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
21 changes: 14 additions & 7 deletions pkg/storage/chunk/cache/fifo_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,21 @@ type FifoCache struct {

entriesAdded prometheus.Counter
entriesAddedNew prometheus.Counter
entriesEvicted prometheus.Counter
entriesEvicted *prometheus.CounterVec
entriesCurrent prometheus.Gauge
totalGets prometheus.Counter
totalMisses prometheus.Counter
staleGets prometheus.Counter
memoryBytes prometheus.Gauge
}

const (
expiredReason string = "expired" //nolint:staticcheck
fullReason = "full"
tooBigReason = "object too big"
stoppedReason = "stopped"
dannykopping marked this conversation as resolved.
Show resolved Hide resolved
)

type cacheEntry struct {
updated time.Time
key string
Expand Down Expand Up @@ -155,13 +162,13 @@ func NewFifoCache(name string, cfg FifoCacheConfig, reg prometheus.Registerer, l
ConstLabels: prometheus.Labels{"cache": name},
}),

entriesEvicted: promauto.With(reg).NewCounter(prometheus.CounterOpts{
entriesEvicted: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: "querier",
Subsystem: "cache",
Name: "evicted_total",
Help: "The total number of evicted entries",
ConstLabels: prometheus.Labels{"cache": name},
}),
}, []string{"reason"}),

entriesCurrent: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Namespace: "querier",
Expand Down Expand Up @@ -237,7 +244,7 @@ func (c *FifoCache) pruneExpiredItems(ttl time.Duration) {
delete(c.entries, k)
c.currSizeBytes -= sizeOf(entry)
c.entriesCurrent.Dec()
c.entriesEvicted.Inc()
c.entriesEvicted.WithLabelValues(expiredReason).Inc()
}
}
}
Expand Down Expand Up @@ -278,7 +285,7 @@ func (c *FifoCache) Stop() {

close(c.done)

c.entriesEvicted.Add(float64(c.lru.Len()))
c.entriesEvicted.WithLabelValues(stoppedReason).Add(float64(c.lru.Len()))

c.entries = make(map[string]*list.Element)
c.lru.Init()
Expand Down Expand Up @@ -314,7 +321,7 @@ func (c *FifoCache) put(key string, value []byte) {
// Cannot keep this item in the cache.
if ok {
// We do not replace this item.
c.entriesEvicted.Inc()
c.entriesEvicted.WithLabelValues(tooBigReason).Inc()
}
c.memoryBytes.Set(float64(c.currSizeBytes))
return
Expand All @@ -330,7 +337,7 @@ func (c *FifoCache) put(key string, value []byte) {
delete(c.entries, evicted.key)
c.currSizeBytes -= sizeOf(evicted)
c.entriesCurrent.Dec()
c.entriesEvicted.Inc()
c.entriesEvicted.WithLabelValues(fullReason).Inc()
}

// Finally, we have space to add the item.
Expand Down
16 changes: 10 additions & 6 deletions pkg/storage/chunk/cache/fifo_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ func TestFifoCacheEviction(t *testing.T) {
require.NoError(t, err)
require.Len(t, c.entries, cnt)

reason := fullReason

assert.Equal(t, testutil.ToFloat64(c.entriesAdded), float64(1))
assert.Equal(t, testutil.ToFloat64(c.entriesAddedNew), float64(cnt))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted), float64(0))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted.WithLabelValues(reason)), float64(0))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(cnt))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(len(c.entries)))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(c.lru.Len()))
Expand Down Expand Up @@ -98,7 +100,7 @@ func TestFifoCacheEviction(t *testing.T) {

assert.Equal(t, testutil.ToFloat64(c.entriesAdded), float64(2))
assert.Equal(t, testutil.ToFloat64(c.entriesAddedNew), float64(cnt+evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted.WithLabelValues(reason)), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(cnt))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(len(c.entries)))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(c.lru.Len()))
Expand All @@ -119,7 +121,7 @@ func TestFifoCacheEviction(t *testing.T) {

assert.Equal(t, testutil.ToFloat64(c.entriesAdded), float64(2))
assert.Equal(t, testutil.ToFloat64(c.entriesAddedNew), float64(cnt+evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted.WithLabelValues(reason)), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(cnt))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(len(c.entries)))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(c.lru.Len()))
Expand Down Expand Up @@ -149,7 +151,7 @@ func TestFifoCacheEviction(t *testing.T) {

assert.Equal(t, testutil.ToFloat64(c.entriesAdded), float64(3))
assert.Equal(t, testutil.ToFloat64(c.entriesAddedNew), float64(cnt+evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesEvicted.WithLabelValues(reason)), float64(evicted))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(cnt))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(len(c.entries)))
assert.Equal(t, testutil.ToFloat64(c.entriesCurrent), float64(c.lru.Len()))
Expand Down Expand Up @@ -208,7 +210,8 @@ func TestFifoCacheExpiry(t *testing.T) {

assert.Equal(t, float64(1), testutil.ToFloat64(c.entriesAdded))
assert.Equal(t, float64(4), testutil.ToFloat64(c.entriesAddedNew))
assert.Equal(t, float64(1), testutil.ToFloat64(c.entriesEvicted))
assert.Equal(t, float64(0), testutil.ToFloat64(c.entriesEvicted.WithLabelValues(expiredReason)))
assert.Equal(t, float64(1), testutil.ToFloat64(c.entriesEvicted.WithLabelValues(fullReason)))
assert.Equal(t, float64(3), testutil.ToFloat64(c.entriesCurrent))
assert.Equal(t, float64(len(c.entries)), testutil.ToFloat64(c.entriesCurrent))
assert.Equal(t, float64(c.lru.Len()), testutil.ToFloat64(c.entriesCurrent))
Expand All @@ -223,7 +226,8 @@ func TestFifoCacheExpiry(t *testing.T) {

assert.Equal(t, float64(1), testutil.ToFloat64(c.entriesAdded))
assert.Equal(t, float64(4), testutil.ToFloat64(c.entriesAddedNew))
assert.Equal(t, float64(4), testutil.ToFloat64(c.entriesEvicted))
assert.Equal(t, float64(3), testutil.ToFloat64(c.entriesEvicted.WithLabelValues(expiredReason)))
assert.Equal(t, float64(1), testutil.ToFloat64(c.entriesEvicted.WithLabelValues(fullReason)))
assert.Equal(t, float64(0), testutil.ToFloat64(c.entriesCurrent))
assert.Equal(t, float64(len(c.entries)), testutil.ToFloat64(c.entriesCurrent))
assert.Equal(t, float64(c.lru.Len()), testutil.ToFloat64(c.entriesCurrent))
Expand Down