Skip to content

Commit

Permalink
statistics: updating stats cache can trigger evict (#53979) (#53991)
Browse files Browse the repository at this point in the history
close #53742
  • Loading branch information
ti-chi-bot authored Jun 25, 2024
1 parent 3c599cd commit 4a6f3e5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/statistics/handle/cache/internal/lfu/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ go_test(
embed = [":lfu"],
flaky = True,
race = "on",
shard_count = 9,
shard_count = 10,
deps = [
"//pkg/statistics",
"//pkg/statistics/handle/cache/internal/testutil",
Expand Down
5 changes: 3 additions & 2 deletions pkg/statistics/handle/cache/internal/lfu/lfu_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ func (s *LFU) onExit(val any) {
if s.closed.Load() {
return
}
s.addCost(
-1 * val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage())
s.triggerEvict()
// Subtract the memory usage of the table from the total memory usage.
s.addCost(-val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage())
}

// Len implements statsCacheInner
Expand Down
12 changes: 12 additions & 0 deletions pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,15 @@ func TestMemoryControl(t *testing.T) {
lfu.wait()
require.Equal(t, int64(10)*(2*mockCMSMemoryUsage+mockCMSMemoryUsage), lfu.Cost())
}

func TestMemoryControlWithUpdate(t *testing.T) {
capacity := int64(100)
lfu, err := NewLFU(capacity)
require.NoError(t, err)
for i := 0; i < 100; i++ {
t1 := testutil.NewMockStatisticsTable(i, 1, true, false, false)
lfu.Put(1, t1)
}
time.Sleep(1 * time.Second)
require.Equal(t, int64(0), lfu.Cost())
}

0 comments on commit 4a6f3e5

Please sign in to comment.