executor, metrics: add metrics for fast analyze. (#11079)
This commit is contained in:
committed by
GitHub
parent
2ea24e0201
commit
53f8a2c366
@ -526,6 +526,14 @@ func (e *AnalyzeColumnsExec) buildStats(ranges []*ranger.Range) (hists []*statis
|
||||
return hists, cms, nil
|
||||
}
|
||||
|
||||
var (
|
||||
fastAnalyzeHistogramSample = metrics.FastAnalyzeHistogram.WithLabelValues(metrics.LblGeneral, "sample")
|
||||
fastAnalyzeHistogramAccessRegions = metrics.FastAnalyzeHistogram.WithLabelValues(metrics.LblGeneral, "access_regions")
|
||||
fastAnalyzeHistogramRegionError = metrics.FastAnalyzeHistogram.WithLabelValues(metrics.LblGeneral, "region_error")
|
||||
fastAnalyzeHistogramSeekKeys = metrics.FastAnalyzeHistogram.WithLabelValues(metrics.LblGeneral, "seek_keys")
|
||||
fastAnalyzeHistogramScanKeys = metrics.FastAnalyzeHistogram.WithLabelValues(metrics.LblGeneral, "scan_keys")
|
||||
)
|
||||
|
||||
func analyzeFastExec(exec *AnalyzeFastExec) []analyzeResult {
|
||||
hists, cms, err := exec.buildStats()
|
||||
if err != nil {
|
||||
@ -624,6 +632,7 @@ func (e *AnalyzeFastExec) getSampRegionsRowCount(bo *tikv.Backoffer, needRebuild
|
||||
if *err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
resp, *err = client.SendRequest(ctx, rpcCtx.Addr, req, tikv.ReadTimeoutMedium)
|
||||
if *err != nil {
|
||||
@ -740,6 +749,7 @@ func (e *AnalyzeFastExec) buildSampTask() (needRebuild bool, err error) {
|
||||
task.EndOffset = e.rowCount + cnt
|
||||
e.rowCount += cnt
|
||||
}
|
||||
accessRegionsCounter := 0
|
||||
for {
|
||||
// Search for the region which contains the targetKey.
|
||||
loc, err := e.cache.LocateKey(bo, targetKey)
|
||||
@ -749,6 +759,8 @@ func (e *AnalyzeFastExec) buildSampTask() (needRebuild bool, err error) {
|
||||
if bytes.Compare(endKey, loc.StartKey) < 0 {
|
||||
break
|
||||
}
|
||||
accessRegionsCounter++
|
||||
|
||||
// Set the next search key.
|
||||
targetKey = loc.EndKey
|
||||
|
||||
@ -767,6 +779,7 @@ func (e *AnalyzeFastExec) buildSampTask() (needRebuild bool, err error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
fastAnalyzeHistogramAccessRegions.Observe(float64(accessRegionsCounter))
|
||||
|
||||
return false, nil
|
||||
}
|
||||
@ -881,7 +894,7 @@ func (e *AnalyzeFastExec) handleBatchSeekResponse(kvMap map[string][]byte) (err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *AnalyzeFastExec) handleScanIter(iter kv.Iterator) (err error) {
|
||||
func (e *AnalyzeFastExec) handleScanIter(iter kv.Iterator) (scanKeysSize int, err error) {
|
||||
hasPKInfo := 0
|
||||
if e.pkInfo != nil {
|
||||
hasPKInfo = 1
|
||||
@ -890,6 +903,7 @@ func (e *AnalyzeFastExec) handleScanIter(iter kv.Iterator) (err error) {
|
||||
for ; iter.Valid() && err == nil; err = iter.Next() {
|
||||
// reservoir sampling
|
||||
e.rowCount++
|
||||
scanKeysSize++
|
||||
randNum := rander.Int63n(int64(e.rowCount))
|
||||
if randNum > int64(MaxSampleSize) && e.sampCursor == int32(MaxSampleSize) {
|
||||
continue
|
||||
@ -903,28 +917,29 @@ func (e *AnalyzeFastExec) handleScanIter(iter kv.Iterator) (err error) {
|
||||
|
||||
err = e.updateCollectorSamples(iter.Value(), iter.Key(), p, hasPKInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
func (e *AnalyzeFastExec) handleScanTasks(bo *tikv.Backoffer) error {
|
||||
func (e *AnalyzeFastExec) handleScanTasks(bo *tikv.Backoffer) (keysSize int, err error) {
|
||||
snapshot, err := e.ctx.GetStore().(tikv.Storage).GetSnapshot(kv.MaxVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
for _, t := range e.scanTasks {
|
||||
iter, err := snapshot.Iter(t.StartKey, t.EndKey)
|
||||
if err != nil {
|
||||
return err
|
||||
return keysSize, err
|
||||
}
|
||||
err = e.handleScanIter(iter)
|
||||
size, err := e.handleScanIter(iter)
|
||||
keysSize += size
|
||||
if err != nil {
|
||||
return err
|
||||
return keysSize, err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return keysSize, nil
|
||||
}
|
||||
|
||||
func (e *AnalyzeFastExec) handleSampTasks(bo *tikv.Backoffer, workID int, err *error) {
|
||||
@ -969,6 +984,8 @@ func (e *AnalyzeFastExec) handleSampTasks(bo *tikv.Backoffer, workID int, err *e
|
||||
kvMap[string(iter.Key())] = iter.Value()
|
||||
}
|
||||
}
|
||||
fastAnalyzeHistogramSeekKeys.Observe(float64(len(keys)))
|
||||
fastAnalyzeHistogramSample.Observe(float64(len(kvMap)))
|
||||
|
||||
*err = e.handleBatchSeekResponse(kvMap)
|
||||
if *err != nil {
|
||||
@ -1060,7 +1077,8 @@ func (e *AnalyzeFastExec) runTasks() ([]*statistics.Histogram, []*statistics.CMS
|
||||
}
|
||||
}
|
||||
|
||||
err := e.handleScanTasks(bo)
|
||||
scanKeysSize, err := e.handleScanTasks(bo)
|
||||
fastAnalyzeHistogramScanKeys.Observe(float64(scanKeysSize))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -1107,12 +1125,15 @@ func (e *AnalyzeFastExec) buildStats() (hists []*statistics.Histogram, cms []*st
|
||||
|
||||
// Only four rebuilds for sample task are allowed.
|
||||
needRebuild, maxBuildTimes := true, 5
|
||||
regionErrorCounter := 0
|
||||
for counter := maxBuildTimes; needRebuild && counter > 0; counter-- {
|
||||
regionErrorCounter++
|
||||
needRebuild, err = e.buildSampTask()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
fastAnalyzeHistogramRegionError.Observe(float64(regionErrorCounter))
|
||||
if needRebuild {
|
||||
errMsg := "build fast analyze task failed, exceed maxBuildTimes: %v"
|
||||
return nil, nil, errors.Errorf(errMsg, maxBuildTimes)
|
||||
|
||||
@ -76,6 +76,7 @@ func RegisterMetrics() {
|
||||
prometheus.MustRegister(HandShakeErrorCounter)
|
||||
prometheus.MustRegister(HandleJobHistogram)
|
||||
prometheus.MustRegister(SignificantFeedbackCounter)
|
||||
prometheus.MustRegister(FastAnalyzeHistogram)
|
||||
prometheus.MustRegister(JobsGauge)
|
||||
prometheus.MustRegister(KeepAliveCounter)
|
||||
prometheus.MustRegister(LoadPrivilegeCounter)
|
||||
|
||||
@ -84,4 +84,13 @@ var (
|
||||
Name: "high_error_rate_feedback_total",
|
||||
Help: "Counter of query feedback whose actual count is much different than calculated by current statistics",
|
||||
})
|
||||
|
||||
FastAnalyzeHistogram = prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Namespace: "tidb",
|
||||
Subsystem: "statistics",
|
||||
Name: "fast_analyze_status",
|
||||
Help: "Bucketed histogram of some stats in fast analyze.",
|
||||
Buckets: prometheus.ExponentialBuckets(1, 2, 16),
|
||||
}, []string{LblSQLType, LblType})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user