Skip to content

Commit

Permalink
metrics: add statement deadlock detect duration (#14444) (#14484)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored and bb7133 committed Jan 16, 2020
1 parent 0d84245 commit 1c279e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
}
txnCtx := sctx.GetSessionVars().TxnCtx
for {
startPointGetLocking := time.Now()
_, err = a.handleNoDelayExecutor(ctx, e)
if !txn.Valid() {
return err
Expand All @@ -450,6 +451,9 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
// It is possible the DML has point get plan that locks the key.
e, err = a.handlePessimisticLockError(ctx, err)
if err != nil {
if ErrDeadlock.Equal(err) {
metrics.StatementDeadlockDetectDuration.Observe(time.Since(startPointGetLocking).Seconds())
}
return err
}
continue
Expand All @@ -464,12 +468,16 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
}
seVars := sctx.GetSessionVars()
lockCtx := newLockCtx(seVars, seVars.LockWaitTimeout)
startLocking := time.Now()
err = txn.LockKeys(ctx, lockCtx, keys...)
if err == nil {
return nil
}
e, err = a.handlePessimisticLockError(ctx, err)
if err != nil {
if ErrDeadlock.Equal(err) {
metrics.StatementDeadlockDetectDuration.Observe(time.Since(startLocking).Seconds())
}
return err
}
}
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func RegisterMetrics() {
prometheus.MustRegister(TimeJumpBackCounter)
prometheus.MustRegister(TransactionCounter)
prometheus.MustRegister(TransactionDuration)
prometheus.MustRegister(StatementDeadlockDetectDuration)
prometheus.MustRegister(UpdateSelfVersionHistogram)
prometheus.MustRegister(UpdateStatsCounter)
prometheus.MustRegister(WatchOwnerCounter)
Expand Down
10 changes: 10 additions & 0 deletions metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ var (
Help: "Bucketed histogram of a transaction execution duration, including retry.",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), // 1ms ~ 1049s
}, []string{LblSQLType, LblType})

StatementDeadlockDetectDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: "tidb",
Subsystem: "session",
Name: "statement_deadlock_detect_duration_seconds",
Help: "Bucketed histogram of a statement deadlock detect duration.",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 20), // 1ms ~ 1049s
},
)
)

// Label constants.
Expand Down

0 comments on commit 1c279e9

Please sign in to comment.