Skip to content

Commit f90c961

Browse files
committed
sqlstats: reduce stats collector calls from conn exec
Previously, separate functions in the stats collector were required to send execution statistics for statements and transactions to sqlstats and insights respectively. This commit consolidates the sending of execution events to sqlstats and insights into one function on the stats collector per event type (one for statements and one for transactions). Epic: none Part of: #141024 Release note: None
1 parent 40c5937 commit f90c961

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

pkg/sql/conn_executor_exec.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4376,8 +4376,6 @@ func (ex *connExecutor) recordTransactionFinish(
43764376
)
43774377
}
43784378

4379-
ex.statsCollector.ObserveTransaction(ctx, transactionFingerprintID, recordedTxnStats)
4380-
43814379
return ex.statsCollector.RecordTransaction(
43824380
ctx,
43834381
transactionFingerprintID,

pkg/sql/executor_statement_metrics.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ func (ex *connExecutor) recordStatementSummary(
235235
}
236236
}
237237

238-
ex.statsCollector.ObserveStatement(stmtFingerprintID, recordedStmtStats)
239-
240238
// Do some transaction level accounting for the transaction this statement is
241239
// a part of.
242240

pkg/sql/sqlstats/insights/ingester.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type eventBufChPayload struct {
5151
events *eventBuffer
5252
}
5353

54-
// ConcurrentBufferIngester buffers the "events" it sees (via ObserveStatement
55-
// and ObserveTransaction) and passes them along to the underlying registry
54+
// ConcurrentBufferIngester buffers the "events" it sees (via observeStatement
55+
// and observeTransaction) and passes them along to the underlying registry
5656
// once its buffer is full. (Or once a timeout has passed, for low-traffic
5757
// clusters and tests.)
5858
//

pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type bufferedStmtStats []sqlstats.RecordedStmtStats
3939
//
4040
// 2. The insights subsystem (insightsWriter) which is used to
4141
// persist statement and transaction insights to an in-memory cache.
42-
// Events are sent to the insights subsystem for async processing.
42+
// Events are sent to the insights subsystem for async processing in
43+
// observeStatement() and observeTransaction() respectively.
4344
type StatsCollector struct {
4445

4546
// stmtBuf contains the current transaction's statement
@@ -227,11 +228,9 @@ func (s *StatsCollector) shouldObserveInsights() bool {
227228
return sqlstats.StmtStatsEnable.Get(&s.st.SV) && sqlstats.TxnStatsEnable.Get(&s.st.SV)
228229
}
229230

230-
// ObserveStatement sends the recorded statement stats to the insights system
231+
// observeStatement sends the recorded statement stats to the insights system
231232
// for further processing.
232-
func (s *StatsCollector) ObserveStatement(
233-
stmtFingerprintID appstatspb.StmtFingerprintID, value sqlstats.RecordedStmtStats,
234-
) {
233+
func (s *StatsCollector) observeStatement(value sqlstats.RecordedStmtStats) {
235234
if !s.sendInsights {
236235
return
237236
}
@@ -257,7 +256,7 @@ func (s *StatsCollector) ObserveStatement(
257256

258257
insight := insights.Statement{
259258
ID: value.StatementID,
260-
FingerprintID: stmtFingerprintID,
259+
FingerprintID: value.FingerprintID,
261260
LatencyInSeconds: value.ServiceLatencySec,
262261
Query: value.Query,
263262
Status: getInsightStatus(value.StatementError),
@@ -283,12 +282,10 @@ func (s *StatsCollector) ObserveStatement(
283282
}
284283
}
285284

286-
// ObserveTransaction sends the recorded transaction stats to the insights system
285+
// observeTransaction sends the recorded transaction stats to the insights system
287286
// for further processing.
288-
func (s *StatsCollector) ObserveTransaction(
289-
_ctx context.Context,
290-
txnFingerprintID appstatspb.TransactionFingerprintID,
291-
value sqlstats.RecordedTxnStats,
287+
func (s *StatsCollector) observeTransaction(
288+
txnFingerprintID appstatspb.TransactionFingerprintID, value sqlstats.RecordedTxnStats,
292289
) {
293290
if !s.sendInsights {
294291
return
@@ -344,6 +341,8 @@ func (s *StatsCollector) ObserveTransaction(
344341
func (s *StatsCollector) RecordStatement(
345342
ctx context.Context, value sqlstats.RecordedStmtStats,
346343
) error {
344+
s.observeStatement(value)
345+
347346
// TODO(xinhaoz): This isn't the best place to set this, but we'll clean this up
348347
// when we refactor the stats collection code to send the stats to an ingester.
349348
s.stmtFingerprintID = value.FingerprintID
@@ -362,6 +361,8 @@ func (s *StatsCollector) RecordStatement(
362361
func (s *StatsCollector) RecordTransaction(
363362
ctx context.Context, key appstatspb.TransactionFingerprintID, value sqlstats.RecordedTxnStats,
364363
) error {
364+
s.observeTransaction(key, value)
365+
365366
// TODO(117690): Unify StmtStatsEnable and TxnStatsEnable into a single cluster setting.
366367
if !sqlstats.TxnStatsEnable.Get(&s.st.SV) {
367368
return nil

0 commit comments

Comments
 (0)