From 2ee6d22383df887237d390e77f810c9d062160d7 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 13 Jun 2024 12:43:29 +0800 Subject: [PATCH] statistics: updating stats cache can trigger evict (#53979) close pingcap/tidb#53742 --- pkg/statistics/handle/cache/internal/lfu/BUILD.bazel | 2 +- .../handle/cache/internal/lfu/lfu_cache.go | 1 + .../handle/cache/internal/lfu/lfu_cache_test.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/statistics/handle/cache/internal/lfu/BUILD.bazel b/pkg/statistics/handle/cache/internal/lfu/BUILD.bazel index 1e732998623ab..a435d8a974bc6 100644 --- a/pkg/statistics/handle/cache/internal/lfu/BUILD.bazel +++ b/pkg/statistics/handle/cache/internal/lfu/BUILD.bazel @@ -30,7 +30,7 @@ go_test( embed = [":lfu"], flaky = True, race = "on", - shard_count = 9, + shard_count = 10, deps = [ "//pkg/statistics", "//pkg/statistics/handle/cache/internal/testutil", diff --git a/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go b/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go index d4299d19b45be..4b82355945aa2 100644 --- a/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go +++ b/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go @@ -207,6 +207,7 @@ func (s *LFU) onExit(val any) { if s.closed.Load() { return } + s.triggerEvict() // Subtract the memory usage of the table from the total memory usage. s.addCost(-val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage()) } diff --git a/pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go b/pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go index de690b8e06ca9..cb74e5fdcedb3 100644 --- a/pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go +++ b/pkg/statistics/handle/cache/internal/lfu/lfu_cache_test.go @@ -295,3 +295,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()) +}