Skip to content

Commit

Permalink
metric, tikv: record duration for each backoff type (#11710) (#11728)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and zz-jason committed Aug 16, 2019
1 parent afa2a12 commit 5dab5e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
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 @@ -276,7 +284,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 @@ -287,7 +296,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 @@ -473,9 +473,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

0 comments on commit 5dab5e7

Please sign in to comment.