Skip to content

Commit

Permalink
*: get nil in the LFU's onEvict (#45949)
Browse files Browse the repository at this point in the history
close #45946
  • Loading branch information
hawkingrei authored Aug 10, 2023
1 parent 0f41cfe commit d819ea3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions planner/core/plan_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
10 changes: 10 additions & 0 deletions statistics/handle/cache/internal/lfu/lfu_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()))
}
Expand Down
1 change: 1 addition & 0 deletions statistics/handle/cache/statscacheinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d819ea3

Please sign in to comment.