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

metric, tikv: record duration for each backoff type #11710

Merged
merged 3 commits into from
Aug 13, 2019
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
4 changes: 2 additions & 2 deletions metrics/tikvclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ var (
Help: "Counter of backoff.",
}, []string{LblType})

TiKVBackoffHistogram = prometheus.NewHistogram(
TiKVBackoffHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "tikvclient",
Name: "backoff_seconds",
Help: "total backoff seconds of a single backoffer.",
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 20), // 0.5ms ~ 524s
})
}, []string{LblType})

TiKVSendReqHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand Down
49 changes: 30 additions & 19 deletions store/tikv/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,42 @@ const (
)

var (
tikvBackoffCounterRPC = metrics.TiKVBackoffCounter.WithLabelValues("tikvRPC")
tikvBackoffCounterLock = metrics.TiKVBackoffCounter.WithLabelValues("txnLock")
tikvBackoffCounterLockFast = metrics.TiKVBackoffCounter.WithLabelValues("tikvLockFast")
tikvBackoffCounterPD = metrics.TiKVBackoffCounter.WithLabelValues("pdRPC")
tikvBackoffCounterRegionMiss = metrics.TiKVBackoffCounter.WithLabelValues("regionMiss")
tikvBackoffCounterUpdateLeader = metrics.TiKVBackoffCounter.WithLabelValues("updateLeader")
tikvBackoffCounterServerBusy = metrics.TiKVBackoffCounter.WithLabelValues("serverBusy")
tikvBackoffCounterEmpty = metrics.TiKVBackoffCounter.WithLabelValues("")
tikvBackoffCounterRPC = metrics.TiKVBackoffCounter.WithLabelValues("tikvRPC")
tikvBackoffCounterLock = metrics.TiKVBackoffCounter.WithLabelValues("txnLock")
tikvBackoffCounterLockFast = metrics.TiKVBackoffCounter.WithLabelValues("tikvLockFast")
tikvBackoffCounterPD = metrics.TiKVBackoffCounter.WithLabelValues("pdRPC")
tikvBackoffCounterRegionMiss = metrics.TiKVBackoffCounter.WithLabelValues("regionMiss")
tikvBackoffCounterUpdateLeader = metrics.TiKVBackoffCounter.WithLabelValues("updateLeader")
tikvBackoffCounterServerBusy = metrics.TiKVBackoffCounter.WithLabelValues("serverBusy")
tikvBackoffCounterEmpty = metrics.TiKVBackoffCounter.WithLabelValues("")
tikvBackoffHistogramRPC = metrics.TiKVBackoffHistogram.WithLabelValues("tikvRPC")
tikvBackoffHistogramLock = metrics.TiKVBackoffHistogram.WithLabelValues("txnLock")
tikvBackoffHistogramLockFast = metrics.TiKVBackoffHistogram.WithLabelValues("tikvLockFast")
tikvBackoffHistogramPD = metrics.TiKVBackoffHistogram.WithLabelValues("pdRPC")
tikvBackoffHistogramRegionMiss = metrics.TiKVBackoffHistogram.WithLabelValues("regionMiss")
tikvBackoffHistogramUpdateLeader = metrics.TiKVBackoffHistogram.WithLabelValues("updateLeader")
tikvBackoffHistogramServerBusy = metrics.TiKVBackoffHistogram.WithLabelValues("serverBusy")
tikvBackoffHistogramEmpty = metrics.TiKVBackoffHistogram.WithLabelValues("")
)

func (t backoffType) Counter() prometheus.Counter {
func (t backoffType) metric() (prometheus.Counter, prometheus.Observer) {
switch t {
case boTiKVRPC:
return tikvBackoffCounterRPC
return tikvBackoffCounterRPC, tikvBackoffHistogramRPC
case BoTxnLock:
return tikvBackoffCounterLock
return tikvBackoffCounterLock, tikvBackoffHistogramLock
case boTxnLockFast:
return tikvBackoffCounterLockFast
return tikvBackoffCounterLockFast, tikvBackoffHistogramLockFast
case BoPDRPC:
return tikvBackoffCounterPD
return tikvBackoffCounterPD, tikvBackoffHistogramPD
case BoRegionMiss:
return tikvBackoffCounterRegionMiss
return tikvBackoffCounterRegionMiss, tikvBackoffHistogramRegionMiss
case BoUpdateLeader:
return tikvBackoffCounterUpdateLeader
return tikvBackoffCounterUpdateLeader, tikvBackoffHistogramUpdateLeader
case boServerBusy:
return tikvBackoffCounterServerBusy
return tikvBackoffCounterServerBusy, tikvBackoffHistogramServerBusy
}
return tikvBackoffCounterEmpty
return tikvBackoffCounterEmpty, tikvBackoffHistogramEmpty
}

// NewBackoffFn creates a backoff func which implements exponential backoff with
Expand Down Expand Up @@ -278,7 +286,8 @@ func (b *Backoffer) BackoffWithMaxSleep(typ backoffType, maxSleepMs int, err err
default:
}

typ.Counter().Inc()
backoffCounter, backoffDuration := typ.metric()
backoffCounter.Inc()
// Lazy initialize.
if b.fn == nil {
b.fn = make(map[backoffType]func(context.Context, int) int)
Expand All @@ -289,7 +298,9 @@ func (b *Backoffer) BackoffWithMaxSleep(typ backoffType, maxSleepMs int, err err
b.fn[typ] = f
}

b.totalSleep += f(b.ctx, maxSleepMs)
realSleep := f(b.ctx, maxSleepMs)
backoffDuration.Observe(float64(realSleep) / 1000)
b.totalSleep += realSleep
b.types = append(b.types, typ)

var startTs interface{}
Expand Down
3 changes: 0 additions & 3 deletions store/tikv/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,6 @@ func (worker *copIteratorWorker) run(ctx context.Context) {

bo := NewBackoffer(ctx, copNextMaxBackoff).WithVars(worker.vars)
worker.handleTask(bo, task, respCh)
if bo.totalSleep > 0 {
metrics.TiKVBackoffHistogram.Observe(float64(bo.totalSleep) / 1000)
}
close(task.respChan)
select {
case <-worker.finishCh:
Expand Down