From d819ea3567607cb705ecff2be5adcdc13bff034d Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 10 Aug 2023 19:58:57 +0800 Subject: [PATCH] *: get nil in the LFU's onEvict (#45949) close pingcap/tidb#45946 --- planner/core/plan_stats_test.go | 3 +-- statistics/handle/cache/internal/lfu/lfu_cache.go | 10 ++++++++++ statistics/handle/cache/statscacheinner.go | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/planner/core/plan_stats_test.go b/planner/core/plan_stats_test.go index 77fa807873..3de8b4dfc0 100644 --- a/planner/core/plan_stats_test.go +++ b/planner/core/plan_stats_test.go @@ -296,8 +296,7 @@ func TestPlanStatsLoadTimeout(t *testing.T) { } func TestPlanStatsStatusRecord(t *testing.T) { - restore := config.RestoreFunc() - defer restore() + defer config.RestoreFunc()() config.UpdateGlobal(func(conf *config.Config) { conf.Performance.EnableStatsCacheMemQuota = true }) diff --git a/statistics/handle/cache/internal/lfu/lfu_cache.go b/statistics/handle/cache/internal/lfu/lfu_cache.go index 42eb9768a2..e2546c6724 100644 --- a/statistics/handle/cache/internal/lfu/lfu_cache.go +++ b/statistics/handle/cache/internal/lfu/lfu_cache.go @@ -117,6 +117,11 @@ func DropEvicted(item statistics.TableCacheItem) { } func (s *LFU) onEvict(item *ristretto.Item) { + if item.Value == nil { + // Sometimes the same key may be passed to the "onEvict/onExit" function twice, + // and in the second invocation, the value is empty, so it should not be processed. + return + } // We do not need to calculate the cost during onEvict, because the onexit function // is also called when the evict event occurs. metrics.EvictCounter.Inc() @@ -135,6 +140,11 @@ func (s *LFU) onEvict(item *ristretto.Item) { } func (s *LFU) onExit(val interface{}) { + if val == nil { + // Sometimes the same key may be passed to the "onEvict/onExit" function twice, + // and in the second invocation, the value is empty, so it should not be processed. + return + } s.cost.Add(-1 * val.(*statistics.Table).MemoryUsage().TotalTrackingMemUsage()) metrics.CostGauge.Set(float64(s.cost.Load())) } diff --git a/statistics/handle/cache/statscacheinner.go b/statistics/handle/cache/statscacheinner.go index 9b834ad3ad..a9688e1a4f 100644 --- a/statistics/handle/cache/statscacheinner.go +++ b/statistics/handle/cache/statscacheinner.go @@ -161,6 +161,7 @@ func (sc *StatsCache) SetCapacity(c int64) { // Close stops the cache. func (sc *StatsCache) Close() { sc.c.Close() + logutil.BgLogger().Info("closed LFU cache") } // Version returns the version of the current cache, which is defined as