Skip to content

Commit

Permalink
statistics: add gc in hot peer cache (tikv#8702) tikv#8750
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <admin@liudos.us>
  • Loading branch information
lhy1024 committed Oct 30, 2024
1 parent 4fbf433 commit 63a02b0
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 80 deletions.
5 changes: 3 additions & 2 deletions pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ type Cluster struct {

// NewCluster creates a new Cluster
func NewCluster(ctx context.Context, opts *config.PersistOptions) *Cluster {
basicCluster:= core.NewBasicCluster()
clus := &Cluster{
BasicCluster: core.NewBasicCluster(),
BasicCluster: basicCluster,
IDAllocator: mockid.NewIDAllocator(),
HotStat: statistics.NewHotStat(ctx),
HotStat: statistics.NewHotStat(ctx, basicCluster),
HotBucketCache: buckets.NewBucketsCache(ctx),
PersistOptions: opts,
suspectRegions: map[uint64]struct{}{},
Expand Down
18 changes: 18 additions & 0 deletions pkg/schedule/schedulers/hot_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,9 @@ func TestHotCacheUpdateCache(t *testing.T) {
re := require.New(t)
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := 0; i < 3; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
tc.SetHotRegionCacheHitsThreshold(0)

// For read flow
Expand Down Expand Up @@ -1561,6 +1564,9 @@ func TestHotCacheKeyThresholds(t *testing.T) {
{ // only a few regions
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := 0; i < 6; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
tc.SetHotRegionCacheHitsThreshold(0)
addRegionInfo(tc, statistics.Read, []testRegionInfo{
{1, []uint64{1, 2, 3}, 0, 1, 0},
Expand All @@ -1580,6 +1586,9 @@ func TestHotCacheKeyThresholds(t *testing.T) {
{ // many regions
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := 0; i < 3; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
regions := []testRegionInfo{}
for i := 1; i <= 1000; i += 2 {
regions = append(regions,
Expand Down Expand Up @@ -1633,6 +1642,9 @@ func TestHotCacheByteAndKey(t *testing.T) {
re := require.New(t)
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := 0; i < 3; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
tc.SetHotRegionCacheHitsThreshold(0)
statistics.ThresholdsUpdateInterval = 0
defer func() {
Expand Down Expand Up @@ -1760,6 +1772,9 @@ func TestHotCacheCheckRegionFlow(t *testing.T) {
func checkHotCacheCheckRegionFlow(re *require.Assertions, testCase testHotCacheCheckRegionFlowCase, enablePlacementRules bool) {
cancel, _, tc, oc := prepareSchedulersTest()
defer cancel()
for i := 0; i < 3; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
tc.SetClusterVersion(versioninfo.MinSupportedVersion(versioninfo.Version4_0))
tc.SetEnablePlacementRules(enablePlacementRules)
labels := []string{"zone", "host"}
Expand Down Expand Up @@ -1835,6 +1850,9 @@ func TestHotCacheCheckRegionFlowWithDifferentThreshold(t *testing.T) {
func checkHotCacheCheckRegionFlowWithDifferentThreshold(re *require.Assertions, enablePlacementRules bool) {
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := 0; i < 3; i++ {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
tc.SetClusterVersion(versioninfo.MinSupportedVersion(versioninfo.Version4_0))
tc.SetEnablePlacementRules(enablePlacementRules)
labels := []string{"zone", "host"}
Expand Down
6 changes: 3 additions & 3 deletions pkg/statistics/hot_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ type HotCache struct {
}

// NewHotCache creates a new hot spot cache.
func NewHotCache(ctx context.Context) *HotCache {
func NewHotCache(ctx context.Context, cluster *core.BasicCluster) *HotCache {
w := &HotCache{
ctx: ctx,
writeCache: NewHotPeerCache(ctx, Write),
readCache: NewHotPeerCache(ctx, Read),
writeCache: NewHotPeerCache(ctx, cluster, Write),
readCache: NewHotPeerCache(ctx, cluster, Read),
}
go w.updateItems(w.readCache.taskQueue, w.runReadTask)
go w.updateItems(w.writeCache.taskQueue, w.runWriteTask)
Expand Down
37 changes: 35 additions & 2 deletions pkg/statistics/hot_peer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@ type thresholds struct {
// hotPeerCache saves the hot peer's statistics.
type hotPeerCache struct {
kind RWType
cluster *core.BasicCluster
peersOfStore map[uint64]*TopN // storeID -> hot peers
storesOfRegion map[uint64]map[uint64]struct{} // regionID -> storeIDs
regionsOfStore map[uint64]map[uint64]struct{} // storeID -> regionIDs
topNTTL time.Duration
taskQueue *chanx.UnboundedChan[FlowItemTask]
thresholdsOfStore map[uint64]*thresholds // storeID -> thresholds
metrics map[uint64][ActionTypeLen]prometheus.Gauge // storeID -> metrics
// TODO: consider to remove store info when store is offline.
lastGCTime time.Time
}

// NewHotPeerCache creates a hotPeerCache
func NewHotPeerCache(ctx context.Context, kind RWType) *hotPeerCache {
func NewHotPeerCache(ctx context.Context, cluster *core.BasicCluster, kind RWType) *hotPeerCache {
return &hotPeerCache{
kind: kind,
cluster: cluster,
peersOfStore: make(map[uint64]*TopN),
storesOfRegion: make(map[uint64]map[uint64]struct{}),
regionsOfStore: make(map[uint64]map[uint64]struct{}),
Expand Down Expand Up @@ -130,6 +132,7 @@ func (f *hotPeerCache) updateStat(item *HotPeerStat) {
return
}
f.incMetrics(item.actionType, item.StoreID)
f.gc()
}

func (f *hotPeerCache) incMetrics(action ActionType, storeID uint64) {
Expand Down Expand Up @@ -560,6 +563,36 @@ func (f *hotPeerCache) removeItem(item *HotPeerStat) {
}
}

func (f *hotPeerCache) gc() {
if time.Since(f.lastGCTime) < f.topNTTL {
return
}
f.lastGCTime = time.Now()
// remove tombstone stores
stores := make(map[uint64]struct{})
for _, storeID := range f.cluster.GetStores() {
stores[storeID.GetID()] = struct{}{}
}
for storeID := range f.peersOfStore {
if _, ok := stores[storeID]; !ok {
delete(f.peersOfStore, storeID)
delete(f.regionsOfStore, storeID)
delete(f.thresholdsOfStore, storeID)
delete(f.metrics, storeID)
}
}
// remove expired items
for _, peers := range f.peersOfStore {
regions := peers.RemoveExpired()
for _, regionID := range regions {
delete(f.storesOfRegion, regionID)
for storeID := range f.regionsOfStore {
delete(f.regionsOfStore[storeID], regionID)
}
}
}
}

func (f *hotPeerCache) coldItem(newItem, oldItem *HotPeerStat) {
newItem.HotDegree = oldItem.HotDegree - 1
newItem.AntiCount = oldItem.AntiCount - 1
Expand Down
Loading

0 comments on commit 63a02b0

Please sign in to comment.