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: reduce unnecessary cpu time #5800

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 0 additions & 5 deletions server/core/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func NewPeerInfo(meta *metapb.Peer, loads []float64, interval uint64) *PeerInfo
}
}

// GetStoreID provides located storeID
func (p *PeerInfo) GetStoreID() uint64 {
return p.GetStoreId()
}

// GetLoads provides loads
func (p *PeerInfo) GetLoads() []float64 {
return p.loads
Expand Down
25 changes: 9 additions & 16 deletions server/statistics/hot_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"github.com/tikv/pd/server/core"
)

var (
readTaskMetrics = hotCacheFlowQueueStatusGauge.WithLabelValues(Read.String())
writeTaskMetrics = hotCacheFlowQueueStatusGauge.WithLabelValues(Write.String())
)

// HotCache is a cache hold hot regions.
type HotCache struct {
ctx context.Context
Expand Down Expand Up @@ -105,27 +110,15 @@ func (w *HotCache) GetHotPeerStat(kind RWType, regionID, storeID uint64) *HotPee

// CollectMetrics collects the hot cache metrics.
func (w *HotCache) CollectMetrics() {
writeMetricsTask := newCollectMetricsTask("write")
readMetricsTask := newCollectMetricsTask("read")
w.CheckWriteAsync(writeMetricsTask)
w.CheckReadAsync(readMetricsTask)
w.CheckWriteAsync(newCollectMetricsTask())
w.CheckReadAsync(newCollectMetricsTask())
}

// ResetMetrics resets the hot cache metrics.
func (w *HotCache) ResetMetrics() {
hotCacheStatusGauge.Reset()
}

func incMetrics(name string, storeID uint64, kind RWType) {
store := storeTag(storeID)
switch kind {
case Write:
hotCacheStatusGauge.WithLabelValues(name, store, "write").Inc()
case Read:
hotCacheStatusGauge.WithLabelValues(name, store, "read").Inc()
}
}

func (w *HotCache) updateItems(queue <-chan FlowItemTask, runTask func(task FlowItemTask)) {
for {
select {
Expand All @@ -141,15 +134,15 @@ func (w *HotCache) runReadTask(task FlowItemTask) {
if task != nil {
// TODO: do we need a run-task timeout to protect the queue won't be stuck by a task?
task.runTask(w.readCache)
hotCacheFlowQueueStatusGauge.WithLabelValues(Read.String()).Set(float64(len(w.readCache.taskQueue)))
readTaskMetrics.Set(float64(len(w.readCache.taskQueue)))
}
}

func (w *HotCache) runWriteTask(task FlowItemTask) {
if task != nil {
// TODO: do we need a run-task timeout to protect the queue won't be stuck by a task?
task.runTask(w.writeCache)
hotCacheFlowQueueStatusGauge.WithLabelValues(Write.String()).Set(float64(len(w.writeCache.taskQueue)))
writeTaskMetrics.Set(float64(len(w.writeCache.taskQueue)))
}
}

Expand Down
9 changes: 3 additions & 6 deletions server/statistics/hot_cache_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,14 @@ func (t *checkRegionHotTask) waitRet(ctx context.Context) bool {
}

type collectMetricsTask struct {
typ string
}

func newCollectMetricsTask(typ string) *collectMetricsTask {
return &collectMetricsTask{
typ: typ,
}
func newCollectMetricsTask() *collectMetricsTask {
return &collectMetricsTask{}
}

func (t *collectMetricsTask) runTask(cache *hotPeerCache) {
cache.collectMetrics(t.typ)
cache.collectMetrics()
}

type getHotPeerStatTask struct {
Expand Down
27 changes: 15 additions & 12 deletions server/statistics/hot_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"time"

"github.com/pingcap/log"
"github.com/tikv/pd/pkg/movingaverage"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/utils/syncutil"
Expand Down Expand Up @@ -126,18 +127,20 @@ func (stat *HotPeerStat) Less(dim int, than TopNItem) bool {
}

// Log is used to output some info
func (stat *HotPeerStat) Log(str string, level func(msg string, fields ...zap.Field)) {
level(str,
zap.Uint64("region-id", stat.RegionID),
zap.Bool("is-leader", stat.isLeader),
zap.Float64s("loads", stat.GetLoads()),
zap.Float64s("loads-instant", stat.Loads),
zap.Int("hot-degree", stat.HotDegree),
zap.Int("hot-anti-count", stat.AntiCount),
zap.Duration("sum-interval", stat.getIntervalSum()),
zap.Bool("allow-inherited", stat.allowInherited),
zap.String("action-type", stat.actionType.String()),
zap.Time("last-transfer-leader-time", stat.lastTransferLeaderTime))
func (stat *HotPeerStat) Log(str string) {
if log.GetLevel() <= zap.DebugLevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use log.Debug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will cost unnecessary cpu for zap

log.Debug(str,
zap.Uint64("region-id", stat.RegionID),
zap.Bool("is-leader", stat.isLeader),
zap.Float64s("loads", stat.GetLoads()),
zap.Float64s("loads-instant", stat.Loads),
zap.Int("hot-degree", stat.HotDegree),
zap.Int("hot-anti-count", stat.AntiCount),
zap.Duration("sum-interval", stat.getIntervalSum()),
zap.Bool("allow-inherited", stat.allowInherited),
zap.String("action-type", stat.actionType.String()),
zap.Time("last-transfer-leader-time", stat.lastTransferLeaderTime))
}
}

// IsNeedCoolDownTransferLeader use cooldown time after transfer leader to avoid unnecessary schedule
Expand Down
Loading