-
Notifications
You must be signed in to change notification settings - Fork 721
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
*: add metrics for hotspot cache #1027
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3581057
*: add metrics for hotspot cache
nolouch f2d5a19
address comments
nolouch be57eaf
address comment
nolouch 06981e7
fix test
nolouch e2bbce8
address comments
nolouch 3797f57
Merge branch 'master' into add-hot-metrics
disksing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,47 +45,71 @@ func newHotSpotCache() *HotSpotCache { | |
|
||
// CheckWrite checks the write status, returns whether need update statistics and item. | ||
func (w *HotSpotCache) CheckWrite(region *core.RegionInfo, stores *core.StoresInfo) (bool, *core.RegionStat) { | ||
var WrittenBytesPerSec uint64 | ||
var ( | ||
WrittenBytesPerSec uint64 | ||
value *core.RegionStat | ||
) | ||
v, isExist := w.writeFlow.Peek(region.GetId()) | ||
if isExist && !Simulating { | ||
interval := time.Since(v.(*core.RegionStat).LastUpdateTime).Seconds() | ||
value = v.(*core.RegionStat) | ||
interval := time.Since(value.LastUpdateTime).Seconds() | ||
if interval < minHotRegionReportInterval { | ||
return false, nil | ||
} | ||
WrittenBytesPerSec = uint64(float64(region.WrittenBytes) / interval) | ||
} else { | ||
WrittenBytesPerSec = uint64(float64(region.WrittenBytes) / float64(RegionHeartBeatReportInterval)) | ||
} | ||
region.WrittenBytes = WrittenBytesPerSec | ||
|
||
// hotRegionThreshold is use to pick hot region | ||
// suppose the number of the hot Regions is statCacheMaxLen | ||
// and we use total written Bytes past storeHeartBeatReportInterval seconds to divide the number of hot Regions | ||
// divide 2 because the store reports data about two times than the region record write to rocksdb | ||
divisor := float64(statCacheMaxLen) * 2 * storeHeartBeatReportInterval | ||
hotRegionThreshold := uint64(float64(stores.TotalWrittenBytes()) / divisor) | ||
|
||
if hotRegionThreshold < hotWriteRegionMinFlowRate { | ||
hotRegionThreshold = hotWriteRegionMinFlowRate | ||
} | ||
return w.isNeedUpdateStatCache(region, hotRegionThreshold, WriteFlow) | ||
hotRegionThreshold := calculateWriteHotThreshold(stores) | ||
return w.isNeedUpdateStatCache(region, WrittenBytesPerSec, hotRegionThreshold, value, WriteFlow) | ||
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. Do we need metrics for |
||
} | ||
|
||
// CheckRead checks the read status, returns whether need update statistics and item. | ||
func (w *HotSpotCache) CheckRead(region *core.RegionInfo, stores *core.StoresInfo) (bool, *core.RegionStat) { | ||
var ReadBytesPerSec uint64 | ||
var ( | ||
ReadBytesPerSec uint64 | ||
value *core.RegionStat | ||
) | ||
v, isExist := w.readFlow.Peek(region.GetId()) | ||
if isExist && !Simulating { | ||
interval := time.Since(v.(*core.RegionStat).LastUpdateTime).Seconds() | ||
value = v.(*core.RegionStat) | ||
interval := time.Since(value.LastUpdateTime).Seconds() | ||
if interval < minHotRegionReportInterval { | ||
return false, nil | ||
} | ||
ReadBytesPerSec = uint64(float64(region.ReadBytes) / interval) | ||
} else { | ||
ReadBytesPerSec = uint64(float64(region.ReadBytes) / float64(RegionHeartBeatReportInterval)) | ||
} | ||
region.ReadBytes = ReadBytesPerSec | ||
hotRegionThreshold := calculateReadHotThreshold(stores) | ||
return w.isNeedUpdateStatCache(region, ReadBytesPerSec, hotRegionThreshold, value, ReadFlow) | ||
} | ||
|
||
func (w *HotSpotCache) incMetrics(name string, kind FlowKind) { | ||
switch kind { | ||
case WriteFlow: | ||
hotCacheStatusGauge.WithLabelValues(name, "write").Inc() | ||
case ReadFlow: | ||
hotCacheStatusGauge.WithLabelValues(name, "read").Inc() | ||
} | ||
} | ||
|
||
func calculateWriteHotThreshold(stores *core.StoresInfo) uint64 { | ||
// hotRegionThreshold is use to pick hot region | ||
// suppose the number of the hot Regions is statCacheMaxLen | ||
// and we use total written Bytes past storeHeartBeatReportInterval seconds to divide the number of hot Regions | ||
// divide 2 because the store reports data about two times than the region record write to rocksdb | ||
divisor := float64(statCacheMaxLen) * 2 * storeHeartBeatReportInterval | ||
hotRegionThreshold := uint64(float64(stores.TotalWrittenBytes()) / divisor) | ||
|
||
if hotRegionThreshold < hotWriteRegionMinFlowRate { | ||
hotRegionThreshold = hotWriteRegionMinFlowRate | ||
} | ||
return hotRegionThreshold | ||
} | ||
|
||
func calculateReadHotThreshold(stores *core.StoresInfo) uint64 { | ||
// hotRegionThreshold is use to pick hot region | ||
// suppose the number of the hot Regions is statLRUMaxLen | ||
// and we use total Read Bytes past storeHeartBeatReportInterval seconds to divide the number of hot Regions | ||
|
@@ -95,26 +119,10 @@ func (w *HotSpotCache) CheckRead(region *core.RegionInfo, stores *core.StoresInf | |
if hotRegionThreshold < hotReadRegionMinFlowRate { | ||
hotRegionThreshold = hotReadRegionMinFlowRate | ||
} | ||
return w.isNeedUpdateStatCache(region, hotRegionThreshold, ReadFlow) | ||
return hotRegionThreshold | ||
} | ||
|
||
func (w *HotSpotCache) isNeedUpdateStatCache(region *core.RegionInfo, hotRegionThreshold uint64, kind FlowKind) (bool, *core.RegionStat) { | ||
var ( | ||
v *core.RegionStat | ||
value interface{} | ||
isExist bool | ||
flowBytes uint64 | ||
) | ||
key := region.GetId() | ||
|
||
switch kind { | ||
case WriteFlow: | ||
value, isExist = w.writeFlow.Peek(key) | ||
flowBytes = region.WrittenBytes | ||
case ReadFlow: | ||
value, isExist = w.readFlow.Peek(key) | ||
flowBytes = region.ReadBytes | ||
} | ||
func (w *HotSpotCache) isNeedUpdateStatCache(region *core.RegionInfo, flowBytes uint64, hotRegionThreshold uint64, oldItem *core.RegionStat, kind FlowKind) (bool, *core.RegionStat) { | ||
newItem := &core.RegionStat{ | ||
RegionID: region.GetId(), | ||
FlowBytes: flowBytes, | ||
|
@@ -124,31 +132,27 @@ func (w *HotSpotCache) isNeedUpdateStatCache(region *core.RegionInfo, hotRegionT | |
AntiCount: hotRegionAntiCount, | ||
} | ||
|
||
if isExist { | ||
v = value.(*core.RegionStat) | ||
newItem.HotDegree = v.HotDegree + 1 | ||
if oldItem != nil { | ||
newItem.HotDegree = oldItem.HotDegree + 1 | ||
} | ||
switch kind { | ||
case WriteFlow: | ||
if region.WrittenBytes >= hotRegionThreshold { | ||
return true, newItem | ||
} | ||
case ReadFlow: | ||
if region.ReadBytes >= hotRegionThreshold { | ||
return true, newItem | ||
if flowBytes >= hotRegionThreshold { | ||
if oldItem == nil { | ||
w.incMetrics("add_item", kind) | ||
} | ||
return true, newItem | ||
} | ||
// smaller than hotReionThreshold | ||
if !isExist { | ||
if oldItem == nil { | ||
return false, newItem | ||
} | ||
if v.AntiCount <= 0 { | ||
if oldItem.AntiCount <= 0 { | ||
w.incMetrics("remove_item", kind) | ||
return true, nil | ||
} | ||
// eliminate some noise | ||
newItem.HotDegree = v.HotDegree - 1 | ||
newItem.AntiCount = v.AntiCount - 1 | ||
newItem.FlowBytes = v.FlowBytes | ||
newItem.HotDegree = oldItem.HotDegree - 1 | ||
newItem.AntiCount = oldItem.AntiCount - 1 | ||
newItem.FlowBytes = oldItem.FlowBytes | ||
return true, newItem | ||
} | ||
|
||
|
@@ -160,12 +164,14 @@ func (w *HotSpotCache) Update(key uint64, item *core.RegionStat, kind FlowKind) | |
w.writeFlow.Remove(key) | ||
} else { | ||
w.writeFlow.Put(key, item) | ||
w.incMetrics("update_item", kind) | ||
} | ||
case ReadFlow: | ||
if item == nil { | ||
w.readFlow.Remove(key) | ||
} else { | ||
w.readFlow.Put(key, item) | ||
w.incMetrics("update_item", kind) | ||
} | ||
} | ||
} | ||
|
@@ -197,6 +203,16 @@ func (w *HotSpotCache) RandHotRegionFromStore(storeID uint64, kind FlowKind, hot | |
return nil | ||
} | ||
|
||
// CollectMetrics collect the hot cache metrics | ||
func (w *HotSpotCache) CollectMetrics(stores *core.StoresInfo) { | ||
hotCacheStatusGauge.WithLabelValues("total_length", "write").Set(float64(w.writeFlow.Len())) | ||
hotCacheStatusGauge.WithLabelValues("total_length", "read").Set(float64(w.readFlow.Len())) | ||
threshold := calculateWriteHotThreshold(stores) | ||
hotCacheStatusGauge.WithLabelValues("hotThreshold", "write").Set(float64(threshold)) | ||
threshold = calculateReadHotThreshold(stores) | ||
hotCacheStatusGauge.WithLabelValues("hotThreshold", "read").Set(float64(threshold)) | ||
} | ||
|
||
func (w *HotSpotCache) isRegionHot(id uint64, hotThreshold int) bool { | ||
if stat, ok := w.writeFlow.Peek(id); ok { | ||
if stat.(*core.RegionStat).HotDegree >= hotThreshold { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,18 @@ var ( | |
Help: "Bucketed histogram of processing time (s) of finished operator step.", | ||
Buckets: prometheus.ExponentialBuckets(0.01, 2, 16), | ||
}, []string{"type"}) | ||
|
||
hotCacheStatusGauge = prometheus.NewGaugeVec( | ||
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. Add an empty line above this line. |
||
prometheus.GaugeOpts{ | ||
Namespace: "pd", | ||
Subsystem: "hotcache", | ||
Name: "status", | ||
Help: "Status of the hotspot.", | ||
}, []string{"name", "type"}) | ||
) | ||
|
||
func init() { | ||
prometheus.MustRegister(checkerCounter) | ||
prometheus.MustRegister(operatorStepDuration) | ||
prometheus.MustRegister(hotCacheStatusGauge) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
missing
region.WrittenBytes = WrittenBytesPerSec
?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.
This is unnecessary.