*: get nil in the LFU's onEvict (#45949)

close pingcap/tidb#45946
This commit is contained in:
Weizhen Wang
2023-08-10 19:58:57 +08:00
committed by GitHub
parent 0f41cfe2a8
commit d819ea3567
3 changed files with 12 additions and 2 deletions

View File

@ -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
})

View File

@ -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()))
}

View File

@ -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