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

server: add counter for keys locking numbers(#14625) #14634

Merged
merged 2 commits into from
Feb 5, 2020
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: 4 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
if a.retryCount > 0 {
metrics.StatementPessimisticRetryCount.Observe(float64(a.retryCount))
}
lockKeysCnt := a.Ctx.GetSessionVars().StmtCtx.LockKeysCount
if lockKeysCnt > 0 {
metrics.StatementLockKeysCount.Add(float64(lockKeysCnt))
}
return
}
if str, ok := r.(string); !ok || !strings.HasPrefix(str, memory.PanicMemoryExceed) {
Expand Down
1 change: 1 addition & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ func newLockCtx(seVars *variable.SessionVars, lockWaitTime int64) *kv.LockCtx {
WaitStartTime: seVars.StmtCtx.GetLockWaitStartTime(),
PessimisticLockWaited: &seVars.StmtCtx.PessimisticLockWaited,
LockKeysDuration: &seVars.StmtCtx.LockKeysDuration,
LockKeysCount: &seVars.StmtCtx.LockKeysCount,
}
}

Expand Down
1 change: 1 addition & 0 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type LockCtx struct {
WaitStartTime time.Time
PessimisticLockWaited *int32
LockKeysDuration *time.Duration
LockKeysCount *int32
}

// Client is used to send request to KV layer.
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func RegisterMetrics() {
prometheus.MustRegister(TransactionDuration)
prometheus.MustRegister(StatementDeadlockDetectDuration)
prometheus.MustRegister(StatementPessimisticRetryCount)
prometheus.MustRegister(StatementLockKeysCount)
prometheus.MustRegister(UpdateSelfVersionHistogram)
prometheus.MustRegister(UpdateStatsCounter)
prometheus.MustRegister(WatchOwnerCounter)
Expand Down
8 changes: 8 additions & 0 deletions metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ var (
Help: "Bucketed historgram of statement pessimistic retry count",
Buckets: prometheus.ExponentialBuckets(1, 1.5, 14), // 1 ~ 291
})

StatementLockKeysCount = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "session",
Name: "statement_lock_keys_count",
Help: "Keys locking for a single statement",
})
)

// Label constants.
Expand Down
1 change: 1 addition & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type StatementContext struct {
// planNormalized use for cache the normalized plan, avoid duplicate builds.
planNormalized string
planDigest string
LockKeysCount int32
}

// GetNowTsCached getter for nowTs, if not set get now time and cache it
Expand Down
5 changes: 4 additions & 1 deletion store/tikv/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func (txn *tikvTxn) rollbackPessimisticLocks() error {
func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput ...kv.Key) error {
// Exclude keys that are already locked.
var err error
keys := make([][]byte, 0, len(keysInput))
defer func() {
if err == nil {
if lockCtx.PessimisticLockWaited != nil {
Expand All @@ -362,8 +363,10 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput
}
}
}
if lockCtx.LockKeysCount != nil {
*lockCtx.LockKeysCount += int32(len(keys))
}
}()
keys := make([][]byte, 0, len(keysInput))
txn.mu.Lock()
for _, key := range keysInput {
if _, ok := txn.lockedMap[string(key)]; !ok {
Expand Down