Skip to content

Commit 4a6f3e5

Browse files
authored
statistics: updating stats cache can trigger evict (pingcap#53979) (pingcap#53991)
close pingcap#53742
1 parent 3c599cd commit 4a6f3e5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

pkg/statistics/handle/cache/internal/lfu/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ go_test(
3131
embed = [":lfu"],
3232
flaky = True,
3333
race = "on",
34-
shard_count = 9,
34+
shard_count = 10,
3535
deps = [
3636
"//pkg/statistics",
3737
"//pkg/statistics/handle/cache/internal/testutil",

pkg/statistics/handle/cache/internal/lfu/lfu_cache.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ func (s *LFU) onExit(val any) {
206206
if s.closed.Load() {
207207
return
208208
}
209-
s.addCost(
210-
-1 * val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage())
209+
s.triggerEvict()
210+
// Subtract the memory usage of the table from the total memory usage.
211+
s.addCost(-val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage())
211212
}
212213

213214
// Len implements statsCacheInner

pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,15 @@ func TestMemoryControl(t *testing.T) {
306306
lfu.wait()
307307
require.Equal(t, int64(10)*(2*mockCMSMemoryUsage+mockCMSMemoryUsage), lfu.Cost())
308308
}
309+
310+
func TestMemoryControlWithUpdate(t *testing.T) {
311+
capacity := int64(100)
312+
lfu, err := NewLFU(capacity)
313+
require.NoError(t, err)
314+
for i := 0; i < 100; i++ {
315+
t1 := testutil.NewMockStatisticsTable(i, 1, true, false, false)
316+
lfu.Put(1, t1)
317+
}
318+
time.Sleep(1 * time.Second)
319+
require.Equal(t, int64(0), lfu.Cost())
320+
}

0 commit comments

Comments
 (0)