Skip to content

Add metric for the number of elements in a cache #1782

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

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Cacher[K comparable, V any] interface {
// Flush removes all entries from the cache
Flush()

// Returns the number of elements currently in the cache
Len() int

// Returns fraction of cache currently filled (0 --> 1)
PortionFilled() float64
}
Expand Down
4 changes: 4 additions & 0 deletions cache/empty_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (*Empty[K, _]) Evict(K) {}

func (*Empty[_, _]) Flush() {}

func (*Empty[_, _]) Len() int {
return 0
}

func (*Empty[_, _]) PortionFilled() float64 {
return 0
}
16 changes: 15 additions & 1 deletion cache/lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func (c *LRU[_, _]) Flush() {
c.flush()
}

func (c *LRU[_, _]) Len() int {
c.lock.Lock()
defer c.lock.Unlock()

return c.len()
}

func (c *LRU[_, _]) PortionFilled() float64 {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down Expand Up @@ -88,8 +95,15 @@ func (c *LRU[K, V]) flush() {
c.elements = linkedhashmap.New[K, V]()
}

func (c *LRU[_, _]) len() int {
if c.elements == nil {
return 0
}
Comment on lines +99 to +101
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: if resize() hasn't been called yet - c.elements may be nil

return c.elements.Len()
}

func (c *LRU[_, _]) portionFilled() float64 {
return float64(c.elements.Len()) / float64(c.Size)
return float64(c.len()) / float64(c.Size)
}

// Initializes [c.elements] if it's nil.
Expand Down
11 changes: 11 additions & 0 deletions cache/lru_sized_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func (c *sizedLRU[K, V]) Flush() {
c.flush()
}

func (c *sizedLRU[_, _]) Len() int {
c.lock.Lock()
defer c.lock.Unlock()

return c.len()
}

func (c *sizedLRU[_, _]) PortionFilled() float64 {
c.lock.Lock()
defer c.lock.Unlock()
Expand Down Expand Up @@ -110,6 +117,10 @@ func (c *sizedLRU[K, V]) flush() {
c.currentSize = 0
}

func (c *sizedLRU[_, _]) len() int {
return c.elements.Len()
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 need the nil check here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no - elements is initialized in NewSizedLRU here

}

func (c *sizedLRU[_, _]) portionFilled() float64 {
return float64(c.currentSize) / float64(c.maxSize)
}
3 changes: 3 additions & 0 deletions cache/metercacher/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c *Cache[K, V]) Put(key K, value V) {
c.Cacher.Put(key, value)
end := c.clock.Time()
c.put.Observe(float64(end.Sub(start)))
c.len.Set(float64(c.Cacher.Len()))
c.portionFilled.Set(c.Cacher.PortionFilled())
}

Expand All @@ -52,10 +53,12 @@ func (c *Cache[K, V]) Get(key K) (V, bool) {

func (c *Cache[K, _]) Evict(key K) {
c.Cacher.Evict(key)
c.len.Set(float64(c.Cacher.Len()))
c.portionFilled.Set(c.Cacher.PortionFilled())
}

func (c *Cache[_, _]) Flush() {
c.Cacher.Flush()
c.len.Set(float64(c.Cacher.Len()))
c.portionFilled.Set(c.Cacher.PortionFilled())
}
16 changes: 12 additions & 4 deletions cache/metercacher/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ func newCounterMetric(namespace, name string, reg prometheus.Registerer, errs *w
}

type metrics struct {
get,
put metric.Averager
get metric.Averager
put metric.Averager
len prometheus.Gauge
portionFilled prometheus.Gauge
hit,
miss prometheus.Counter
hit prometheus.Counter
miss prometheus.Counter
}

func (m *metrics) Initialize(
Expand All @@ -47,6 +48,13 @@ func (m *metrics) Initialize(
errs := wrappers.Errs{}
m.get = newAveragerMetric(namespace, "get", reg, &errs)
m.put = newAveragerMetric(namespace, "put", reg, &errs)
m.len = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "len",
Help: "number of entries",
},
)
m.portionFilled = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
8 changes: 8 additions & 0 deletions cache/test_cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ func TestEviction(t *testing.T, cache Cacher[ids.ID, int64]) {
expectedValue2 := int64(2)
expectedValue3 := int64(3)

require.Zero(cache.Len())

cache.Put(id1, expectedValue1)

require.Equal(1, cache.Len())

cache.Put(id2, expectedValue2)

require.Equal(2, cache.Len())

val, found := cache.Get(id1)
require.True(found)
require.Equal(expectedValue1, val)
Expand All @@ -87,6 +94,7 @@ func TestEviction(t *testing.T, cache Cacher[ids.ID, int64]) {
require.False(found)

cache.Put(id3, expectedValue3)
require.Equal(2, cache.Len())

_, found = cache.Get(id1)
require.False(found)
Expand Down