From d3add7c6a1ff6b3b084e50de51fe301cac18ce10 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 11 Dec 2024 12:44:09 +0800 Subject: [PATCH] statstics: trigger evict by the timer (#58027) close pingcap/tidb#58052 --- pkg/domain/domain.go | 1 + pkg/statistics/handle/cache/internal/inner.go | 2 ++ pkg/statistics/handle/cache/internal/lfu/lfu_cache.go | 5 +++++ pkg/statistics/handle/cache/internal/mapcache/map_cache.go | 3 +++ pkg/statistics/handle/cache/statscache.go | 5 +++++ pkg/statistics/handle/cache/statscacheinner.go | 5 +++++ pkg/statistics/handle/types/interfaces.go | 3 +++ 7 files changed, 24 insertions(+) diff --git a/pkg/domain/domain.go b/pkg/domain/domain.go index 6e86ca1d6fcaa..a8d3df9895bcd 100644 --- a/pkg/domain/domain.go +++ b/pkg/domain/domain.go @@ -2718,6 +2718,7 @@ func (do *Domain) updateStatsWorker(_ sessionctx.Context) { } case <-readMemTicker.C: memory.ForceReadMemStats() + do.StatsHandle().StatsCache.TriggerEvict() case <-updateStatsHealthyTicker.C: statsHandle.UpdateStatsHealthyMetrics() } diff --git a/pkg/statistics/handle/cache/internal/inner.go b/pkg/statistics/handle/cache/internal/inner.go index 607de40de7d34..dc6b1d0af5371 100644 --- a/pkg/statistics/handle/cache/internal/inner.go +++ b/pkg/statistics/handle/cache/internal/inner.go @@ -41,4 +41,6 @@ type StatsCacheInner interface { SetCapacity(int64) // Close stops the cache Close() + // TriggerEvict triggers the cache to evict some items + TriggerEvict() } diff --git a/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go b/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go index f3e93454b16cc..4a16300e4b8c4 100644 --- a/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go +++ b/pkg/statistics/handle/cache/internal/lfu/lfu_cache.go @@ -251,3 +251,8 @@ func (s *LFU) addCost(v int64) { newv := s.cost.Add(v) metrics.CostGauge.Set(float64(newv)) } + +// TriggerEvict implements statsCacheInner +func (s *LFU) TriggerEvict() { + s.triggerEvict() +} diff --git a/pkg/statistics/handle/cache/internal/mapcache/map_cache.go b/pkg/statistics/handle/cache/internal/mapcache/map_cache.go index 37144b1c70128..8be0d8e08ed7e 100644 --- a/pkg/statistics/handle/cache/internal/mapcache/map_cache.go +++ b/pkg/statistics/handle/cache/internal/mapcache/map_cache.go @@ -131,3 +131,6 @@ func (*MapCache) SetCapacity(int64) {} // Close implements StatsCacheInner func (*MapCache) Close() {} + +// TriggerEvict implements statsCacheInner +func (*MapCache) TriggerEvict() {} diff --git a/pkg/statistics/handle/cache/statscache.go b/pkg/statistics/handle/cache/statscache.go index 7dc13de688eeb..a6c6979cd87ed 100644 --- a/pkg/statistics/handle/cache/statscache.go +++ b/pkg/statistics/handle/cache/statscache.go @@ -259,6 +259,11 @@ func (s *StatsCacheImpl) Put(id int64, t *statistics.Table) { s.Load().put(id, t) } +// TriggerEvict triggers the cache to evict some items. +func (s *StatsCacheImpl) TriggerEvict() { + s.Load().TriggerEvict() +} + // MaxTableStatsVersion returns the version of the current cache, which is defined as // the max table stats version the cache has in its lifecycle. func (s *StatsCacheImpl) MaxTableStatsVersion() uint64 { diff --git a/pkg/statistics/handle/cache/statscacheinner.go b/pkg/statistics/handle/cache/statscacheinner.go index 1cc55dbada482..b2ae6aeca61aa 100644 --- a/pkg/statistics/handle/cache/statscacheinner.go +++ b/pkg/statistics/handle/cache/statscacheinner.go @@ -183,3 +183,8 @@ func (sc *StatsCache) Update(tables []*statistics.Table, deletedIDs []int64, ski } } } + +// TriggerEvict triggers the cache to evict some items. +func (sc *StatsCache) TriggerEvict() { + sc.c.TriggerEvict() +} diff --git a/pkg/statistics/handle/types/interfaces.go b/pkg/statistics/handle/types/interfaces.go index 42079f8f5dbff..6cd80e3434731 100644 --- a/pkg/statistics/handle/types/interfaces.go +++ b/pkg/statistics/handle/types/interfaces.go @@ -260,6 +260,9 @@ type StatsCache interface { // UpdateStatsHealthyMetrics updates stats healthy distribution metrics according to stats cache. UpdateStatsHealthyMetrics() + + // TriggerEvict triggers the cache to evict some items + TriggerEvict() } // StatsLockTable is the table info of which will be locked.