Skip to content

Commit 0958bae

Browse files
committed
sql: add metrics counting SQL-level automatic retries
Add metrics counting the number of automatic retries of transactions and statements within conn executor. Informs: #145377 Release note (sql change): Add new metrics `sql.txn.auto_retry.count` and `sql.statements.auto_retry.count` which count the number of automatic retries of SQL transactions and statements, respectively, within the database. These metrics differ from the related `txn.restarts.*` metrics in that the latter count retryable errors emitted by the KV layer of the database, which must be retried, and the former count auto-retry actions taken by the SQL layer of the database in response to some of those retryable errors.
1 parent 422711a commit 0958bae

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

docs/generated/metrics/metrics.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8823,6 +8823,22 @@ layers:
88238823
unit: COUNT
88248824
aggregation: AVG
88258825
derivative: NON_NEGATIVE_DERIVATIVE
8826+
- name: sql.statements.auto_retry.count
8827+
exported_name: sql_statements_auto_retry_count
8828+
description: Number of SQL statement automatic retries
8829+
y_axis_label: SQL Statements
8830+
type: COUNTER
8831+
unit: COUNT
8832+
aggregation: AVG
8833+
derivative: NON_NEGATIVE_DERIVATIVE
8834+
- name: sql.statements.auto_retry.count.internal
8835+
exported_name: sql_statements_auto_retry_count_internal
8836+
description: Number of SQL statement automatic retries (internal queries)
8837+
y_axis_label: SQL Internal Statements
8838+
type: COUNTER
8839+
unit: COUNT
8840+
aggregation: AVG
8841+
derivative: NON_NEGATIVE_DERIVATIVE
88268842
- name: sql.stats.activity.update.latency
88278843
exported_name: sql_stats_activity_update_latency
88288844
description: The latency of updates made by the SQL activity updater job. Includes failed update attempts
@@ -8991,6 +9007,22 @@ layers:
89919007
unit: COUNT
89929008
aggregation: AVG
89939009
derivative: NON_NEGATIVE_DERIVATIVE
9010+
- name: sql.txn.auto_retry.count
9011+
exported_name: sql_txn_auto_retry_count
9012+
description: Number of SQL transaction automatic retries
9013+
y_axis_label: SQL Transactions
9014+
type: COUNTER
9015+
unit: COUNT
9016+
aggregation: AVG
9017+
derivative: NON_NEGATIVE_DERIVATIVE
9018+
- name: sql.txn.auto_retry.count.internal
9019+
exported_name: sql_txn_auto_retry_count_internal
9020+
description: Number of SQL transaction automatic retries (internal queries)
9021+
y_axis_label: SQL Internal Statements
9022+
type: COUNTER
9023+
unit: COUNT
9024+
aggregation: AVG
9025+
derivative: NON_NEGATIVE_DERIVATIVE
89949026
- name: sql.txn.begin.started.count
89959027
exported_name: sql_txn_begin_started_count
89969028
description: Number of SQL transaction BEGIN statements started

pkg/sql/conn_executor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,8 @@ func makeMetrics(internal bool, sv *settings.Values) Metrics {
616616
TransactionTimeoutCount: metric.NewCounter(getMetricMeta(MetaTransactionTimeout, internal)),
617617
FullTableOrIndexScanCount: aggmetric.NewSQLCounter(getMetricMeta(MetaFullTableOrIndexScan, internal)),
618618
FullTableOrIndexScanRejectedCount: metric.NewCounter(getMetricMeta(MetaFullTableOrIndexScanRejected, internal)),
619+
TxnRetryCount: metric.NewCounter(getMetricMeta(MetaTxnRetry, internal)),
620+
StatementRetryCount: metric.NewCounter(getMetricMeta(MetaStatementRetry, internal)),
619621
},
620622
StartedStatementCounters: makeStartedStatementCounters(internal),
621623
ExecutedStatementCounters: makeExecutedStatementCounters(internal),

pkg/sql/conn_executor_exec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,7 @@ func (ex *connExecutor) dispatchReadCommittedStmtToExecutionEngine(
28112811
ppInfo.dispatchReadCommittedStmtToExecutionEngine.autoRetryStmtReason = p.autoRetryStmtReason
28122812
ppInfo.dispatchReadCommittedStmtToExecutionEngine.autoRetryStmtCounter = p.autoRetryStmtCounter
28132813
}
2814+
ex.metrics.EngineMetrics.StatementRetryCount.Inc(1)
28142815
}
28152816
return nil
28162817
}
@@ -4398,6 +4399,7 @@ func (ex *connExecutor) recordTransactionFinish(
43984399
ex.sessionData().Database, ex.sessionData().ApplicationName)
43994400
ex.metrics.EngineMetrics.SQLTxnLatency.RecordValue(elapsedTime.Nanoseconds(),
44004401
ex.sessionData().Database, ex.sessionData().ApplicationName)
4402+
ex.metrics.EngineMetrics.TxnRetryCount.Inc(int64(ex.state.mu.autoRetryCounter))
44014403

44024404
ex.txnIDCacheWriter.Record(contentionpb.ResolvedTxnID{
44034405
TxnID: ev.txnID,

pkg/sql/exec_util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,18 @@ var (
13481348
Measurement: "SQL Statements",
13491349
Unit: metric.Unit_COUNT,
13501350
}
1351+
MetaTxnRetry = metric.Metadata{
1352+
Name: "sql.txn.auto_retry.count",
1353+
Help: "Number of SQL transaction automatic retries",
1354+
Measurement: "SQL Transactions",
1355+
Unit: metric.Unit_COUNT,
1356+
}
1357+
MetaStatementRetry = metric.Metadata{
1358+
Name: "sql.statements.auto_retry.count",
1359+
Help: "Number of SQL statement automatic retries",
1360+
Measurement: "SQL Statements",
1361+
Unit: metric.Unit_COUNT,
1362+
}
13511363
)
13521364

13531365
func getMetricMeta(meta metric.Metadata, internal bool) metric.Metadata {

pkg/sql/executor_statement_metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ type EngineMetrics struct {
7575
// FullTableOrIndexScanRejectedCount counts the number of queries that were
7676
// rejected because of the `disallow_full_table_scans` guardrail.
7777
FullTableOrIndexScanRejectedCount *metric.Counter
78+
79+
// TxnRetryCount counts the number of automatic transaction retries that
80+
// have occurred.
81+
TxnRetryCount *metric.Counter
82+
83+
// StatementRetryCount counts the number of automatic statement retries that
84+
// have occurred under READ COMMITTED isolation.
85+
StatementRetryCount *metric.Counter
7886
}
7987

8088
// EngineMetrics implements the metric.Struct interface.

0 commit comments

Comments
 (0)