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

sql: fixes statement contention count metric #96458

Merged
merged 1 commit into from
Feb 3, 2023
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
1 change: 0 additions & 1 deletion pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,6 @@ func (ex *connExecutor) recordTransactionFinish(

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

ex.txnIDCacheWriter.Record(contentionpb.ResolvedTxnID{
Expand Down
23 changes: 15 additions & 8 deletions pkg/sql/distsql_running_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,15 @@ func TestDistSQLReceiverReportsContention(t *testing.T) {
}()
// TODO(yuzefovich): turning the tracing ON won't be necessary once
// always-on tracing is enabled.
_, err = otherConn.ExecContext(ctx, `
SET TRACING=on;
BEGIN;
SET TRANSACTION PRIORITY HIGH;
UPDATE test.test SET x = 100 WHERE x = 1;
COMMIT;
SET TRACING=off;
`)
_, err = otherConn.ExecContext(ctx, `SET TRACING=on;`)
require.NoError(t, err)
txn, err := otherConn.BeginTx(ctx, nil)
require.NoError(t, err)
_, err = txn.ExecContext(ctx, `
SET TRANSACTION PRIORITY HIGH;
UPDATE test.test SET x = 100 WHERE x = 1;
`)

require.NoError(t, err)
if contention {
// Soft check to protect against flakiness where an internal query
Expand All @@ -346,8 +347,14 @@ SET TRACING=off;
"contention metric unexpectedly non-zero when no contention events are produced",
)
}

require.Equal(t, contention, strings.Contains(contentionRegistry.String(), contentionEventSubstring))
err = txn.Commit()
require.NoError(t, err)
_, err = otherConn.ExecContext(ctx, `SET TRACING=off;`)
require.NoError(t, err)
})

}

// TestDistSQLReceiverDrainsOnError is a simple unit test that asserts that the
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/executor_statement_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ func (ex *connExecutor) recordStatementSummary(
ex.server.cfg.ContentionRegistry.AddContentionEvent(contentionEvent)
}

if queryLevelStats.ContentionTime > 0 {
ex.planner.DistSQLPlanner().distSQLSrv.Metrics.ContendedQueriesCount.Inc(1)
}

err = ex.statsCollector.RecordStatementExecStats(recordedStmtStatsKey, *queryLevelStats)
if err != nil {
if log.V(2 /* level */) {
Expand Down