-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
statistics: add gc in hot peer cache #8702
Changes from 4 commits
d8b8091
9266c24
fe4ebc4
f2d03a9
9d00e7a
9331644
9481fb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,20 +60,22 @@ type thresholds struct { | |
// HotPeerCache saves the hot peer's statistics. | ||
type HotPeerCache struct { | ||
kind utils.RWType | ||
cluster *core.BasicCluster | ||
peersOfStore map[uint64]*utils.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[func(*HotPeerCache)] | ||
thresholdsOfStore map[uint64]*thresholds // storeID -> thresholds | ||
metrics map[uint64][utils.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 utils.RWType) *HotPeerCache { | ||
func NewHotPeerCache(ctx context.Context, cluster *core.BasicCluster, kind utils.RWType) *HotPeerCache { | ||
return &HotPeerCache{ | ||
kind: kind, | ||
cluster: cluster, | ||
peersOfStore: make(map[uint64]*utils.TopN), | ||
storesOfRegion: make(map[uint64]map[uint64]struct{}), | ||
regionsOfStore: make(map[uint64]map[uint64]struct{}), | ||
|
@@ -84,9 +86,8 @@ func NewHotPeerCache(ctx context.Context, kind utils.RWType) *HotPeerCache { | |
} | ||
} | ||
|
||
// TODO: rename RegionStats as PeerStats | ||
// RegionStats returns hot items | ||
func (f *HotPeerCache) RegionStats(minHotDegree int) map[uint64][]*HotPeerStat { | ||
// PeerStats returns hot items | ||
func (f *HotPeerCache) PeerStats(minHotDegree int) map[uint64][]*HotPeerStat { | ||
res := make(map[uint64][]*HotPeerStat) | ||
defaultAntiCount := f.kind.DefaultAntiCount() | ||
for storeID, peers := range f.peersOfStore { | ||
|
@@ -115,6 +116,7 @@ func (f *HotPeerCache) UpdateStat(item *HotPeerStat) { | |
return | ||
} | ||
f.incMetrics(item.actionType, item.StoreID) | ||
f.removeExpiredItems() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be called every time we receive a region heartbeat? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In previous, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L554-L556 |
||
} | ||
|
||
func (f *HotPeerCache) incMetrics(action utils.ActionType, storeID uint64) { | ||
|
@@ -548,6 +550,36 @@ func (f *HotPeerCache) removeItem(item *HotPeerStat) { | |
} | ||
} | ||
|
||
func (f *HotPeerCache) removeExpiredItems() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about renaming to |
||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// removeAllItem removes all items of the cache. | ||
// It is used for test. | ||
func (f *HotPeerCache) removeAllItem() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change the name, it's not consistent with other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll keep the TODO and change them in another PR uniform.