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

metrics: Add metrics and telemetry for aggressive locking #41038

Merged
merged 22 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1b7833a
Add metrics and telemetry for aggressive locking
MyonKeminta Jan 18, 2023
b90bc35
Merge branch 'master' of https://github.com/pingcap/tidb into m/aggre…
MyonKeminta Feb 3, 2023
737db31
Fix build
MyonKeminta Feb 3, 2023
9ca41c0
Fix build
MyonKeminta Feb 3, 2023
6761bdb
Add telemetry for the global switch
MyonKeminta Feb 3, 2023
97109db
Add to grafana dashboard
MyonKeminta Feb 3, 2023
3f40e70
Update dependency
MyonKeminta Feb 7, 2023
91eae45
Merge branch 'master' of https://github.com/pingcap/tidb into m/aggre…
MyonKeminta Feb 7, 2023
d4727d1
Merge branch 'master' of https://github.com/pingcap/tidb into m/aggre…
MyonKeminta Feb 7, 2023
1a42667
Address comments
MyonKeminta Feb 7, 2023
6831fb7
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 7, 2023
4e75f56
Fix build
MyonKeminta Feb 7, 2023
fca1cf6
Merge branch 'm/aggressive-locking-metrics' of https://github.com/Myo…
MyonKeminta Feb 7, 2023
ef2378b
Fix build
MyonKeminta Feb 7, 2023
99e8f4d
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 7, 2023
151fd2f
Fix test
MyonKeminta Feb 8, 2023
30307de
Merge branch 'master' of https://github.com/pingcap/tidb into m/aggre…
MyonKeminta Feb 8, 2023
54900a5
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 8, 2023
48e32cb
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 8, 2023
fc59d2c
Merge branch 'master' of https://github.com/pingcap/tidb into m/aggre…
MyonKeminta Feb 9, 2023
07a5194
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 9, 2023
57b51d9
Merge branch 'master' into m/aggressive-locking-metrics
MyonKeminta Feb 9, 2023
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
45 changes: 45 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ var (
selectForUpdateRetryDuration = metrics.PessimisticDMLDurationByAttempt.WithLabelValues("select-for-update", "retry")
dmlFirstAttemptDuration = metrics.PessimisticDMLDurationByAttempt.WithLabelValues("dml", "first-attempt")
dmlRetryDuration = metrics.PessimisticDMLDurationByAttempt.WithLabelValues("dml", "retry")

MyonKeminta marked this conversation as resolved.
Show resolved Hide resolved
// aggressiveLockingTxnUsedCount counts transactions where at least one statement has aggressive locking enabled.
aggressiveLockingTxnUsedCount = metrics.AggressiveLockingUsageCount.WithLabelValues(metrics.LblAggressiveLockingTxnUsed)
// aggressiveLockingStmtUsedCount counts statements that have aggressive locking enabled.
aggressiveLockingStmtUsedCount = metrics.AggressiveLockingUsageCount.WithLabelValues(metrics.LblAggressiveLockingStmtUsed)
// aggressiveLockingTxnUsedCount counts transactions where at least one statement has aggressive locking enabled,
// and it takes effect (which is determined according to whether lock-with-conflict has occurred during execution).
aggressiveLockingTxnEffectiveCount = metrics.AggressiveLockingUsageCount.WithLabelValues(metrics.LblAggressiveLockingTxnEffective)
// aggressiveLockingTxnUsedCount counts statements where at least one statement has aggressive locking enabled,
// and it takes effect (which is determined according to whether lock-with-conflict has occurred during execution).
aggressiveLockingStmtEffectiveCount = metrics.AggressiveLockingUsageCount.WithLabelValues(metrics.LblAggressiveLockingStmtEffective)
)

// processinfoSetter is the interface use to set current running process info.
Expand Down Expand Up @@ -450,6 +461,18 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
if lockKeysCnt > 0 {
metrics.StatementLockKeysCount.Observe(float64(lockKeysCnt))
}

execDetails := a.Ctx.GetSessionVars().StmtCtx.GetExecDetails()
if err == nil && execDetails.LockKeysDetail != nil &&
(execDetails.LockKeysDetail.AggressiveLockNewCount > 0 || execDetails.LockKeysDetail.AggressiveLockDerivedCount > 0) {
a.Ctx.GetSessionVars().TxnCtx.AggressiveLockingUsed = true
// If this statement is finished when some of the keys are locked with conflict in the last retry, or
// some of the keys are derived from the previous retry, we consider the optimization of aggressive locking
// takes effect on this statement.
if execDetails.LockKeysDetail.LockedWithConflictCount > 0 || execDetails.LockKeysDetail.AggressiveLockDerivedCount > 0 {
a.Ctx.GetSessionVars().TxnCtx.AggressiveLockingEffective = true
}
}
return
}
if str, ok := r.(string); !ok || !strings.Contains(str, memory.PanicMemoryExceed) {
Expand Down Expand Up @@ -1470,6 +1493,28 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
if sessVars.StmtCtx.ReadFromTableCache {
metrics.ReadFromTableCacheCounter.Inc()
}

// Update aggressive locking related counters by stmt
if execDetail.LockKeysDetail != nil {
if execDetail.LockKeysDetail.AggressiveLockNewCount > 0 || execDetail.LockKeysDetail.AggressiveLockDerivedCount > 0 {
aggressiveLockingStmtUsedCount.Inc()
// If this statement is finished when some of the keys are locked with conflict in the last retry, or
// some of the keys are derived from the previous retry, we consider the optimization of aggressive locking
// takes effect on this statement.
if execDetail.LockKeysDetail.LockedWithConflictCount > 0 || execDetail.LockKeysDetail.AggressiveLockDerivedCount > 0 {
aggressiveLockingStmtEffectiveCount.Inc()
}
}
}
// If the transaction is committed, update aggressive locking related counters by txn
if execDetail.CommitDetail != nil {
if sessVars.TxnCtx.AggressiveLockingUsed {
aggressiveLockingTxnUsedCount.Inc()
}
if sessVars.TxnCtx.AggressiveLockingEffective {
aggressiveLockingTxnEffectiveCount.Inc()
}
}
}

func (a *ExecStmt) checkPlanReplayerCapture(txnTS uint64) {
Expand Down
200 changes: 199 additions & 1 deletion metrics/grafana/tidb.json
Original file line number Diff line number Diff line change
Expand Up @@ -6799,6 +6799,204 @@
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_TEST-CLUSTER}",
"description": "Counters of transactions or statements in which aggressive locking is enabled / takes effect.",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"grid": {},
"gridPos": {
"h": 7,
"w": 8,
"x": 0,
"y": 74
},
"hiddenSeries": false,
"id": 295,
"legend": {
"alignAsTable": false,
"avg": false,
"current": false,
"hideEmpty": false,
"max": false,
"min": false,
"rightSide": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.11",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"exemplar": true,
"expr": "sum(irate(tidb_session_transaction_aggressive_locking_usage{instance=~\"$instance\"}[30s])) by (type)",
"interval": "",
"legendFormat": "{{type}}",
"refId": "A"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Aggressive Locking Usage",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": "0",
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "${DS_TEST-CLUSTER}",
"description": "Counters of keys involved in aggressive locking.",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"grid": {},
"gridPos": {
"h": 7,
"w": 8,
"x": 8,
"y": 74
},
"hiddenSeries": false,
"id": 296,
"legend": {
"alignAsTable": false,
"avg": false,
"current": false,
"hideEmpty": false,
"max": false,
"min": false,
"rightSide": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.11",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"exemplar": true,
"expr": "sum(irate(tidb_tikvclient_aggressive_locking_count{instance=~\"$instance\"}[30s])) by (type)",
"interval": "",
"legendFormat": "{{type}}",
"refId": "A"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Aggressive Locking Keys",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": "0",
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"repeat": null,
Expand Down Expand Up @@ -12883,7 +13081,7 @@
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "tidb-cluster",
"datasource": "${DS_TEST-CLUSTER}",
"description": "Speed of add index",
"editable": true,
"error": false,
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func RegisterMetrics() {
prometheus.MustRegister(AutoIDReqDuration)
prometheus.MustRegister(RegionCheckpointSubscriptionEvent)
prometheus.MustRegister(RCCheckTSWriteConfilictCounter)
prometheus.MustRegister(AggressiveLockingUsageCount)

prometheus.MustRegister(TTLQueryDuration)
prometheus.MustRegister(TTLProcessedExpiredRowsCounter)
Expand Down
16 changes: 15 additions & 1 deletion metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ var (
Name: "resource_group_query_total",
Help: "Counter of the total number of queries for the resource group",
}, []string{LblName})

AggressiveLockingUsageCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "server",
Name: "transaction_aggressive_locking_usage",
Help: "The counter of statements and transactions in which aggressive locking is used or takes effect",
}, []string{LblType})
)

// Label constants.
Expand Down Expand Up @@ -227,5 +235,11 @@ const (
LblModule = "module"
LblRCReadCheckTS = "read_check"
LblRCWriteCheckTS = "write_check"
LblName = "name"

LblName = "name"

LblAggressiveLockingTxnUsed = "txn-used"
LblAggressiveLockingTxnEffective = "txn-effective"
LblAggressiveLockingStmtUsed = "stmt-used"
LblAggressiveLockingStmtEffective = "stmt-effective"
)
22 changes: 22 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,25 @@ func GetIndexMergeCounter() IndexMergeUsageCounter {
IndexMergeUsed: readCounter(TelemetryIndexMergeUsage),
}
}

// AggressiveLockingUsageCounter records the usage of Aggressive Locking feature of pessimistic transaction.
type AggressiveLockingUsageCounter struct {
TxnAggressiveLockingUsed int64 `json:"txn_aggressive_locking_used"`
TxnAggressiveLockingEffective int64 `json:"txn_aggressive_locking_effective"`
}

// Sub returns the difference of two counters.
func (i AggressiveLockingUsageCounter) Sub(rhs AggressiveLockingUsageCounter) AggressiveLockingUsageCounter {
return AggressiveLockingUsageCounter{
TxnAggressiveLockingUsed: i.TxnAggressiveLockingUsed - rhs.TxnAggressiveLockingUsed,
TxnAggressiveLockingEffective: i.TxnAggressiveLockingEffective - rhs.TxnAggressiveLockingEffective,
}
}

// GetAggressiveLockingUsageCounter returns the Aggressive Locking usage counter.
func GetAggressiveLockingUsageCounter() AggressiveLockingUsageCounter {
return AggressiveLockingUsageCounter{
TxnAggressiveLockingUsed: readCounter(AggressiveLockingUsageCount.WithLabelValues(LblAggressiveLockingTxnUsed)),
TxnAggressiveLockingEffective: readCounter(AggressiveLockingUsageCount.WithLabelValues(LblAggressiveLockingTxnEffective)),
}
}
8 changes: 8 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ type TxnCtxNoNeedToRestore struct {
EnableMDL bool
// relatedTableForMDL records the `lock` table for metadata lock. It maps from int64 to int64(version).
relatedTableForMDL *sync.Map

// AggressiveLockingUsed marking whether at least one of the statements in the transaction was executed in
// aggressive locking mode.
AggressiveLockingUsed bool
// AggressiveLockingEffective marking whether at least one of the statements in the transaction was executed in
// aggressive locking mode, and it takes effect (which is determined according to whether lock-with-conflict
// has occurred during execution of any statement).
AggressiveLockingEffective bool
}

// SavepointRecord indicates a transaction's savepoint record.
Expand Down
1 change: 1 addition & 0 deletions telemetry/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func postReportTelemetryData() {
PostSavepointCount()
postReportLazyPessimisticUniqueCheckSetCount()
postReportDDLUsage()
postReportAggressiveLockingUsageCounter()
}

// PostReportTelemetryDataForTest is for test.
Expand Down
Loading