From 2d9f87b58c11edda40f0d9f3323108f18b60e3f8 Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Fri, 10 Jan 2020 17:25:01 +0800 Subject: [PATCH 1/2] metrics: add statement deadlock detect duration Signed-off-by: Shuaipeng Yu --- executor/adapter.go | 4 ++++ metrics/metrics.go | 1 + metrics/session.go | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/executor/adapter.go b/executor/adapter.go index e2e50556a2127..9b571ed792a7c 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -464,12 +464,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 } } diff --git a/metrics/metrics.go b/metrics/metrics.go index 57401d1c0b879..09f28dbf69f04 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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) diff --git a/metrics/session.go b/metrics/session.go index d6f113f57af47..c9eb64b86b76e 100644 --- a/metrics/session.go +++ b/metrics/session.go @@ -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. From 77aedce8614f567d8c68a09744ef070038f6f6cf Mon Sep 17 00:00:00 2001 From: Shuaipeng Yu Date: Fri, 10 Jan 2020 17:29:02 +0800 Subject: [PATCH 2/2] support for point get execute Signed-off-by: Shuaipeng Yu --- executor/adapter.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/executor/adapter.go b/executor/adapter.go index 9b571ed792a7c..50771f13fa73e 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -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 @@ -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