Skip to content

Commit

Permalink
[#108] Collect eviction statistics for expired entries
Browse files Browse the repository at this point in the history
* [#108] Collect eviction statistics for expired entries

* [Chore] Fix issues from linter
  • Loading branch information
maypok86 authored Sep 30, 2024
1 parent 507bc49 commit a35617e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestCache_SetWithTTL(t *testing.T) {
var mutex sync.Mutex
m := make(map[DeletionCause]int)
c, err := MustBuilder[int, int](size).
CollectStats().
InitialCapacity(size).
WithTTL(time.Second).
DeletionListener(func(key int, value int, cause DeletionCause) {
Expand Down Expand Up @@ -199,6 +200,14 @@ func TestCache_SetWithTTL(t *testing.T) {
mutex.Unlock()
t.Fatalf("cache was supposed to expire %d, but expired %d entries", size, e)
}
if c.Stats().EvictedCount() != int64(m[Expired]) {
mutex.Unlock()
t.Fatalf(
"Eviction statistics are not collected for expiration. EvictedCount: %d, expired entries: %d",
c.Stats().EvictedCount(),
m[Expired],
)
}
mutex.Unlock()

m = make(map[DeletionCause]int)
Expand Down Expand Up @@ -240,6 +249,14 @@ func TestCache_SetWithTTL(t *testing.T) {
if len(m) != 1 || m[Expired] != size {
t.Fatalf("cache was supposed to expire %d, but expired %d entries", size, m[Expired])
}
if c.Stats().EvictedCount() != int64(m[Expired]) {
mutex.Unlock()
t.Fatalf(
"Eviction statistics are not collected for expiration. EvictedCount: %d, expired entries: %d",
c.Stats().EvictedCount(),
m[Expired],
)
}
}

func TestCache_Delete(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions internal/core/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ func (c *Cache[K, V]) deleteExpiredNode(n node.Node[K, V]) {
if deleted != nil {
n.Die()
c.notifyDeletion(n.Key(), n.Value(), Expired)
c.stats.IncEvictedCount()
c.stats.AddEvictedCost(n.Cost())
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/hashtable/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ type table[K comparable] struct {
}

func (t *table[K]) addSize(bucketIdx uint64, delta int) {
//nolint:gosec // there will never be an overflow
counterIdx := uint64(len(t.size)-1) & bucketIdx
atomic.AddInt64(&t.size[counterIdx].c, int64(delta))
}

func (t *table[K]) addSizePlain(bucketIdx uint64, delta int) {
//nolint:gosec // there will never be an overflow
counterIdx := uint64(len(t.size)-1) & bucketIdx
t.size[counterIdx].c += int64(delta)
}
Expand Down Expand Up @@ -159,6 +161,7 @@ func newTable[K comparable](bucketCount int, prevHasher maphash.Hasher[K]) *tabl
counterLength = maxCounterLength
}
counter := make([]paddedCounter, counterLength)
//nolint:gosec // there will never be an overflow
mask := uint64(len(buckets) - 1)
t := &table[K]{
buckets: buckets,
Expand Down Expand Up @@ -435,6 +438,7 @@ func (m *Map[K, V]) resize(known *table[K], hint resizeHint) {
if hint != clearHint {
for i := 0; i < tableLen; i++ {
copied := m.copyBuckets(&t.buckets[i], nt)
//nolint:gosec // there will never be an overflow
nt.addSizePlain(uint64(i), copied)
}
}
Expand Down

0 comments on commit a35617e

Please sign in to comment.