Skip to content
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

Merged
merged 7 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/mcs/scheduling/server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewCluster(parentCtx context.Context, persistConfig *config.PersistConfig,
ruleManager: ruleManager,
labelerManager: labelerManager,
persistConfig: persistConfig,
hotStat: statistics.NewHotStat(ctx),
hotStat: statistics.NewHotStat(ctx, basicCluster),
labelStats: statistics.NewLabelStatistics(),
regionStats: statistics.NewRegionStatistics(basicCluster, persistConfig, ruleManager),
storage: storage,
Expand Down
5 changes: 3 additions & 2 deletions pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ type Cluster struct {

// NewCluster creates a new Cluster
func NewCluster(ctx context.Context, opts *config.PersistOptions) *Cluster {
bc := core.NewBasicCluster()
c := &Cluster{
ctx: ctx,
BasicCluster: core.NewBasicCluster(),
BasicCluster: bc,
IDAllocator: mockid.NewIDAllocator(),
HotStat: statistics.NewHotStat(ctx),
HotStat: statistics.NewHotStat(ctx, bc),
HotBucketCache: buckets.NewBucketsCache(ctx),
PersistOptions: opts,
pendingProcessedRegions: 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 @@ -1614,6 +1614,9 @@ func TestHotCacheUpdateCache(t *testing.T) {
re := require.New(t)
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := range 3 {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}

// For read flow
addRegionInfo(tc, utils.Read, []testRegionInfo{
Expand Down Expand Up @@ -1680,6 +1683,9 @@ func TestHotCacheKeyThresholds(t *testing.T) {
{ // only a few regions
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := range 6 {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
addRegionInfo(tc, utils.Read, []testRegionInfo{
{1, []uint64{1, 2, 3}, 0, 1, 0},
{2, []uint64{1, 2, 3}, 0, 1 * units.KiB, 0},
Expand All @@ -1698,6 +1704,9 @@ func TestHotCacheKeyThresholds(t *testing.T) {
{ // many regions
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := range 3 {
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 @@ -1751,6 +1760,9 @@ func TestHotCacheByteAndKey(t *testing.T) {
re := require.New(t)
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := range 3 {
tc.PutStore(core.NewStoreInfo(&metapb.Store{Id: uint64(i + 1)}))
}
statistics.ThresholdsUpdateInterval = 0
defer func() {
statistics.ThresholdsUpdateInterval = 8 * time.Second
Expand Down Expand Up @@ -1877,6 +1889,9 @@ func TestHotCacheCheckRegionFlow(t *testing.T) {
func checkHotCacheCheckRegionFlow(re *require.Assertions, testCase testHotCacheCheckRegionFlowCase, enablePlacementRules bool) {
cancel, _, tc, oc := prepareSchedulersTest()
defer cancel()
for i := range 3 {
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 @@ -1953,6 +1968,9 @@ func TestHotCacheCheckRegionFlowWithDifferentThreshold(t *testing.T) {
func checkHotCacheCheckRegionFlowWithDifferentThreshold(re *require.Assertions, enablePlacementRules bool) {
cancel, _, tc, _ := prepareSchedulersTest()
defer cancel()
for i := range 3 {
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 @@ -39,11 +39,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, utils.Write),
readCache: NewHotPeerCache(ctx, utils.Read),
writeCache: NewHotPeerCache(ctx, cluster, utils.Write),
readCache: NewHotPeerCache(ctx, cluster, utils.Read),
}
go w.updateItems(w.readCache.taskQueue, w.runReadTask)
go w.updateItems(w.writeCache.taskQueue, w.runWriteTask)
Expand Down
6 changes: 4 additions & 2 deletions pkg/statistics/hot_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/statistics/utils"
)

Expand All @@ -27,8 +28,9 @@ func TestIsHot(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for i := utils.RWType(0); i < utils.RWTypeLen; i++ {
cache := NewHotCache(ctx)
region := buildRegion(i, 3, 60)
cluster := core.NewBasicCluster()
cache := NewHotCache(ctx, cluster)
region := buildRegion(cluster, i, 3, 60)
stats := cache.CheckReadPeerSync(region, region.GetPeers(), []float64{100000000, 1000, 1000}, 60)
cache.Update(stats[0], i)
for range 100 {
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 @@ -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{}),
Expand Down Expand Up @@ -115,6 +117,7 @@ func (f *HotPeerCache) UpdateStat(item *HotPeerStat) {
return
}
f.incMetrics(item.actionType, item.StoreID)
f.gc()
}

func (f *HotPeerCache) incMetrics(action utils.ActionType, storeID uint64) {
Expand Down Expand Up @@ -548,6 +551,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)
}
}
}
}

// removeAllItem removes all items of the cache.
// It is used for test.
func (f *HotPeerCache) removeAllItem() {
Expand Down
Loading