Skip to content

Commit f0d3134

Browse files
cfzjywxksre-bot
authored andcommitted
executor: record lockkeys waited flag and duration in stmtctx (pingcap#14209)
1 parent 85afb82 commit f0d3134

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

executor/adapter.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,7 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error {
542542
return nil
543543
}
544544
seVars := sctx.GetSessionVars()
545-
lockCtx := &kv.LockCtx{
546-
Killed: &seVars.Killed,
547-
ForUpdateTS: txnCtx.GetForUpdateTS(),
548-
LockWaitTime: seVars.LockWaitTimeout,
549-
WaitStartTime: seVars.StmtCtx.GetLockWaitStartTime(),
550-
}
545+
lockCtx := newLockCtx(seVars, seVars.LockWaitTimeout)
551546
err = txn.LockKeys(ctx, lockCtx, keys...)
552547
if err == nil {
553548
return nil

executor/executor.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,12 @@ func (e *SelectLockExec) Next(ctx context.Context, req *chunk.Chunk) error {
826826

827827
func newLockCtx(seVars *variable.SessionVars, lockWaitTime int64) *kv.LockCtx {
828828
return &kv.LockCtx{
829-
Killed: &seVars.Killed,
830-
ForUpdateTS: seVars.TxnCtx.GetForUpdateTS(),
831-
LockWaitTime: lockWaitTime,
832-
WaitStartTime: seVars.StmtCtx.GetLockWaitStartTime(),
829+
Killed: &seVars.Killed,
830+
ForUpdateTS: seVars.TxnCtx.GetForUpdateTS(),
831+
LockWaitTime: lockWaitTime,
832+
WaitStartTime: seVars.StmtCtx.GetLockWaitStartTime(),
833+
PessimisticLockWaited: &seVars.StmtCtx.PessimisticLockWaited,
834+
LockKeysDuration: &seVars.StmtCtx.LockKeysDuration,
833835
}
834836
}
835837

kv/kv.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ type LockCtx struct {
198198
ForUpdateTS uint64
199199
LockWaitTime int64
200200
WaitStartTime time.Time
201-
PessimisticLockWaited int32
202-
LockTimeWaited time.Duration
201+
PessimisticLockWaited *int32
202+
LockKeysDuration *time.Duration
203203
}
204204

205205
// Client is used to send request to KV layer.

sessionctx/stmtctx/stmtctx.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,13 @@ type StatementContext struct {
135135
digest string
136136
}
137137
// planNormalized use for cache the normalized plan, avoid duplicate builds.
138-
planNormalized string
139-
planDigest string
140-
Tables []TableEntry
141-
PointExec bool // for point update cached execution, Constant expression need to set "paramMarker"
142-
lockWaitStartTime *time.Time // LockWaitStartTime stores the pessimistic lock wait start time
138+
planNormalized string
139+
planDigest string
140+
Tables []TableEntry
141+
PointExec bool // for point update cached execution, Constant expression need to set "paramMarker"
142+
lockWaitStartTime *time.Time // LockWaitStartTime stores the pessimistic lock wait start time
143+
PessimisticLockWaited int32
144+
LockKeysDuration time.Duration
143145
}
144146

145147
// StmtHints are SessionVars related sql hints.

store/tikv/2pc.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,9 @@ func (action actionPessimisticLock) handleSingleBatch(c *twoPhaseCommitter, bo *
767767
return errors.Trace(ErrLockWaitTimeout)
768768
}
769769
}
770-
atomic.StoreInt32(&action.LockCtx.PessimisticLockWaited, 1)
770+
if action.LockCtx.PessimisticLockWaited != nil {
771+
atomic.StoreInt32(action.LockCtx.PessimisticLockWaited, 1)
772+
}
771773
}
772774

773775
// Handle the killed flag when waiting for the pessimistic lock.

store/tikv/txn.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,12 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput
372372
var err error
373373
defer func() {
374374
if err == nil {
375-
if lockCtx.PessimisticLockWaited > 0 {
376-
lockCtx.LockTimeWaited = time.Since(lockCtx.WaitStartTime)
377-
metrics.TiKVPessimisticLockKeysDuration.Observe(lockCtx.LockTimeWaited.Seconds())
375+
if lockCtx.PessimisticLockWaited != nil {
376+
if atomic.LoadInt32(lockCtx.PessimisticLockWaited) > 0 {
377+
timeWaited := time.Since(lockCtx.WaitStartTime)
378+
*lockCtx.LockKeysDuration = timeWaited
379+
metrics.TiKVPessimisticLockKeysDuration.Observe(timeWaited.Seconds())
380+
}
378381
}
379382
}
380383
}()

0 commit comments

Comments
 (0)