Skip to content

Commit

Permalink
sql: add sql.txn.contended.count metrics
Browse files Browse the repository at this point in the history
Expose transaction contention metric for cockroachdb#73566.

Release note (sql change): new sql.txn.contended.count metric
exposes total number of transactions that experienced contentions.
  • Loading branch information
Azhng committed May 4, 2022
1 parent 609825e commit 9fbaefc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/server/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,9 @@ func TestStatusAPIContentionEvents(t *testing.T) {
server1Conn := sqlutils.MakeSQLRunner(testCluster.ServerConn(0))
server2Conn := sqlutils.MakeSQLRunner(testCluster.ServerConn(1))

contentionCountBefore := testCluster.Server(1).SQLServer().(*sql.Server).
Metrics.EngineMetrics.SQLContendedTxns.Count()

sqlutils.CreateTable(
t,
testCluster.ServerConn(0),
Expand Down Expand Up @@ -3214,6 +3217,13 @@ SET TRACING=off;
(statistics -> 'execution_statistics' -> 'contentionTime' ->> 'mean')::FLOAT > 0
AND app_name = 'contentionTest'
`, [][]string{{"1"}})

contentionCountNow := testCluster.Server(1).SQLServer().(*sql.Server).
Metrics.EngineMetrics.SQLContendedTxns.Count()

require.Greaterf(t, contentionCountNow, contentionCountBefore,
"expected txn contention count to be more than %d, but it is %d",
contentionCountBefore, contentionCountNow)
}

func TestStatusCancelSessionGatewayMetadataPropagation(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ func makeMetrics(internal bool) Metrics {
6*metricsSampleInterval),
SQLTxnsOpen: metric.NewGauge(getMetricMeta(MetaSQLTxnsOpen, internal)),
SQLActiveStatements: metric.NewGauge(getMetricMeta(MetaSQLActiveQueries, internal)),
SQLContendedTxns: metric.NewCounter(getMetricMeta(MetaSQLTxnContended, internal)),

TxnAbortCount: metric.NewCounter(getMetricMeta(MetaTxnAbort, internal)),
FailureCount: metric.NewCounter(getMetricMeta(MetaFailure, internal)),
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,10 @@ func (ex *connExecutor) recordTransactionFinish(
}
ex.metrics.EngineMetrics.SQLTxnLatency.RecordValue(txnTime.Nanoseconds())

if contentionDuration := ex.extraTxnState.accumulatedStats.ContentionTime.Nanoseconds(); contentionDuration > 0 {
ex.metrics.EngineMetrics.SQLContendedTxns.Inc(1)
}

ex.txnIDCacheWriter.Record(contentionpb.ResolvedTxnID{
TxnID: ev.txnID,
TxnFingerprintID: transactionFingerprintID,
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,12 @@ var (
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLTxnContended = metric.Metadata{
Name: "sql.txn.contended.count",
Help: "Number of SQL transactions experienced contention",
Measurement: "Contention",
Unit: metric.Unit_COUNT,
}
MetaSelectStarted = metric.Metadata{
Name: "sql.select.started.count",
Help: "Number of SQL SELECT statements started",
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/executor_statement_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type EngineMetrics struct {
SQLTxnLatency *metric.Histogram
SQLTxnsOpen *metric.Gauge
SQLActiveStatements *metric.Gauge
SQLContendedTxns *metric.Counter

// TxnAbortCount counts transactions that were aborted, either due
// to non-retriable errors, or retriable errors when the client-side
Expand Down
8 changes: 8 additions & 0 deletions pkg/ts/catalog/chart_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,14 @@ var charts = []sectionDescription{
},
AxisLabel: "Count",
},
{
Title: "SQL Transaction Contention",
Metrics: []string{
"sql.txn.contended.count",
"sql.txn.contended.count.internal",
},
AxisLabel: "Transactions",
},
},
},
{
Expand Down

0 comments on commit 9fbaefc

Please sign in to comment.