-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathexecutor_statement_metrics.go
316 lines (272 loc) · 11.4 KB
/
executor_statement_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"strconv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/idxrecommendations"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
)
// EngineMetrics groups a set of SQL metrics.
type EngineMetrics struct {
// The subset of SELECTs that are processed through DistSQL.
DistSQLSelectCount *metric.Counter
// The subset of queries which we attempted and failed to plan with the
// cost-based optimizer.
SQLOptFallbackCount *metric.Counter
SQLOptPlanCacheHits *metric.Counter
SQLOptPlanCacheMisses *metric.Counter
DistSQLExecLatency *metric.Histogram
SQLExecLatency *metric.Histogram
DistSQLServiceLatency *metric.Histogram
SQLServiceLatency *metric.Histogram
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
// retry protocol is not in use.
TxnAbortCount *metric.Counter
// FailureCount counts non-retriable errors in open transactions.
FailureCount *metric.Counter
// FullTableOrIndexScanCount counts the number of full table or index scans.
FullTableOrIndexScanCount *metric.Counter
// FullTableOrIndexScanRejectedCount counts the number of queries that were
// rejected because of the `disallow_full_table_scans` guardrail.
FullTableOrIndexScanRejectedCount *metric.Counter
}
// EngineMetrics implements the metric.Struct interface.
var _ metric.Struct = EngineMetrics{}
// MetricStruct is part of the metric.Struct interface.
func (EngineMetrics) MetricStruct() {}
// StatsMetrics groups metrics related to SQL Stats collection.
type StatsMetrics struct {
SQLStatsMemoryMaxBytesHist *metric.Histogram
SQLStatsMemoryCurBytesCount *metric.Gauge
ReportedSQLStatsMemoryMaxBytesHist *metric.Histogram
ReportedSQLStatsMemoryCurBytesCount *metric.Gauge
DiscardedStatsCount *metric.Counter
SQLStatsFlushStarted *metric.Counter
SQLStatsFlushFailure *metric.Counter
SQLStatsFlushDuration *metric.Histogram
SQLStatsRemovedRows *metric.Counter
SQLTxnStatsCollectionOverhead *metric.Histogram
}
// StatsMetrics is part of the metric.Struct interface.
var _ metric.Struct = StatsMetrics{}
// MetricStruct is part of the metric.Struct interface.
func (StatsMetrics) MetricStruct() {}
// GuardrailMetrics groups metrics related to different guardrails in the SQL
// layer.
type GuardrailMetrics struct {
TxnRowsWrittenLogCount *metric.Counter
TxnRowsWrittenErrCount *metric.Counter
TxnRowsReadLogCount *metric.Counter
TxnRowsReadErrCount *metric.Counter
}
var _ metric.Struct = GuardrailMetrics{}
// MetricStruct is part of the metric.Struct interface.
func (GuardrailMetrics) MetricStruct() {}
// recordStatementSummary gathers various details pertaining to the
// last executed statement/query and performs the associated
// accounting in the passed-in EngineMetrics.
// - distSQLUsed reports whether the query was distributed.
// - automaticRetryCount is the count of implicit txn retries
// so far.
// - result is the result set computed by the query/statement.
// - err is the error encountered, if any.
func (ex *connExecutor) recordStatementSummary(
ctx context.Context,
planner *planner,
automaticRetryCount int,
rowsAffected int,
stmtErr error,
stats topLevelQueryStats,
) roachpb.StmtFingerprintID {
phaseTimes := ex.statsCollector.PhaseTimes()
// Collect the statistics.
idleLatRaw := phaseTimes.GetIdleLatency(ex.statsCollector.PreviousPhaseTimes())
idleLat := idleLatRaw.Seconds()
runLatRaw := phaseTimes.GetRunLatency()
runLat := runLatRaw.Seconds()
parseLat := phaseTimes.GetParsingLatency().Seconds()
planLat := phaseTimes.GetPlanningLatency().Seconds()
// We want to exclude any overhead to reduce possible confusion.
svcLatRaw := phaseTimes.GetServiceLatencyNoOverhead()
svcLat := svcLatRaw.Seconds()
// processing latency: contributing towards SQL results.
processingLat := parseLat + planLat + runLat
// overhead latency: txn/retry management, error checking, etc
execOverhead := svcLat - processingLat
stmt := &planner.stmt
shouldIncludeInLatencyMetrics := shouldIncludeStmtInLatencyMetrics(stmt)
flags := planner.curPlan.flags
if automaticRetryCount == 0 {
ex.updateOptCounters(flags)
m := &ex.metrics.EngineMetrics
if flags.IsDistributed() {
if _, ok := stmt.AST.(*tree.Select); ok {
m.DistSQLSelectCount.Inc(1)
}
if shouldIncludeInLatencyMetrics {
m.DistSQLExecLatency.RecordValue(runLatRaw.Nanoseconds())
m.DistSQLServiceLatency.RecordValue(svcLatRaw.Nanoseconds())
}
}
if shouldIncludeInLatencyMetrics {
m.SQLExecLatency.RecordValue(runLatRaw.Nanoseconds())
m.SQLServiceLatency.RecordValue(svcLatRaw.Nanoseconds())
}
}
fullScan := flags.IsSet(planFlagContainsFullIndexScan) || flags.IsSet(planFlagContainsFullTableScan)
recordedStmtStatsKey := roachpb.StatementStatisticsKey{
Query: stmt.StmtNoConstants,
QuerySummary: stmt.StmtSummary,
DistSQL: flags.IsDistributed(),
Vec: flags.IsSet(planFlagVectorized),
ImplicitTxn: flags.IsSet(planFlagImplicitTxn),
FullScan: fullScan,
Failed: stmtErr != nil,
Database: planner.SessionData().Database,
PlanHash: planner.instrumentation.planGist.Hash(),
}
idxRecommendations := idxrecommendations.FormatIdxRecommendations(planner.instrumentation.indexRecs)
queryLevelStats, queryLevelStatsOk := planner.instrumentation.GetQueryLevelStats()
// We only have node information when it was collected with trace, but we know at least the current
// node should be on the list.
nodeID, err := strconv.ParseInt(ex.server.sqlStats.GetSQLInstanceID().String(), 10, 64)
if err != nil {
log.Warningf(ctx, "failed to convert node ID to int: %s", err)
}
// TODO(todd): Is there a way to gather region info from the other nodes
// the query traverses? (Compare with getNodesFromPlanner.) While the
// information is / may be available in ListNodesInternal, we surely
// don't want to make an RPC call here, and I haven't yet found a place
// where that information has already been cached.
var regions []string
if region, ok := ex.server.cfg.Locality.Find("region"); ok {
regions = append(regions, region)
}
recordedStmtStats := sqlstats.RecordedStmtStats{
SessionID: ex.sessionID,
StatementID: planner.stmt.QueryID,
AutoRetryCount: automaticRetryCount,
AutoRetryReason: ex.state.mu.autoRetryReason,
RowsAffected: rowsAffected,
IdleLatency: idleLat,
ParseLatency: parseLat,
PlanLatency: planLat,
RunLatency: runLat,
ServiceLatency: svcLat,
OverheadLatency: execOverhead,
BytesRead: stats.bytesRead,
RowsRead: stats.rowsRead,
RowsWritten: stats.rowsWritten,
Nodes: util.CombineUniqueInt64(getNodesFromPlanner(planner), []int64{nodeID}),
Regions: regions,
StatementType: stmt.AST.StatementType(),
Plan: planner.instrumentation.PlanForStats(ctx),
PlanGist: planner.instrumentation.planGist.String(),
StatementError: stmtErr,
IndexRecommendations: idxRecommendations,
Query: stmt.StmtNoConstants,
StartTime: phaseTimes.GetSessionPhaseTime(sessionphase.PlannerStartExecStmt),
EndTime: phaseTimes.GetSessionPhaseTime(sessionphase.PlannerStartExecStmt).Add(svcLatRaw),
FullScan: fullScan,
SessionData: planner.SessionData(),
ExecStats: queryLevelStats,
}
stmtFingerprintID, err :=
ex.statsCollector.RecordStatement(ctx, recordedStmtStatsKey, recordedStmtStats)
if err != nil {
if log.V(1) {
log.Warningf(ctx, "failed to record statement: %s", err)
}
ex.server.ServerMetrics.StatsMetrics.DiscardedStatsCount.Inc(1)
}
// Record statement execution statistics if span is recorded and no error was
// encountered while collecting query-level statistics.
if queryLevelStatsOk {
err = ex.statsCollector.RecordStatementExecStats(recordedStmtStatsKey, *queryLevelStats)
if err != nil {
if log.V(2 /* level */) {
log.Warningf(ctx, "unable to record statement exec stats: %s", err)
}
}
}
// Do some transaction level accounting for the transaction this statement is
// a part of.
// We limit the number of statementFingerprintIDs stored for a transaction, as dictated
// by the TxnStatsNumStmtFingerprintIDsToRecord cluster setting.
maxStmtFingerprintIDsLen := sqlstats.TxnStatsNumStmtFingerprintIDsToRecord.Get(&ex.server.cfg.Settings.SV)
if int64(len(ex.extraTxnState.transactionStatementFingerprintIDs)) < maxStmtFingerprintIDsLen {
ex.extraTxnState.transactionStatementFingerprintIDs = append(
ex.extraTxnState.transactionStatementFingerprintIDs, stmtFingerprintID)
}
// Add the current statement's ID to the hash. We don't track queries issued
// by the internal executor, in which case the hash is uninitialized, and
// can therefore be safely ignored.
if ex.extraTxnState.transactionStatementsHash.IsInitialized() {
ex.extraTxnState.transactionStatementsHash.Add(uint64(stmtFingerprintID))
}
ex.extraTxnState.numRows += rowsAffected
if log.V(2) {
// ages since significant epochs
sessionAge := phaseTimes.GetSessionAge().Seconds()
log.Infof(ctx,
"query stats: %d rows, %d retries, "+
"parse %.2fµs (%.1f%%), "+
"plan %.2fµs (%.1f%%), "+
"run %.2fµs (%.1f%%), "+
"overhead %.2fµs (%.1f%%), "+
"session age %.4fs",
rowsAffected, automaticRetryCount,
parseLat*1e6, 100*parseLat/svcLat,
planLat*1e6, 100*planLat/svcLat,
runLat*1e6, 100*runLat/svcLat,
execOverhead*1e6, 100*execOverhead/svcLat,
sessionAge,
)
}
return stmtFingerprintID
}
func (ex *connExecutor) updateOptCounters(planFlags planFlags) {
m := &ex.metrics.EngineMetrics
if planFlags.IsSet(planFlagOptCacheHit) {
m.SQLOptPlanCacheHits.Inc(1)
} else if planFlags.IsSet(planFlagOptCacheMiss) {
m.SQLOptPlanCacheMisses.Inc(1)
}
}
// We only want to keep track of DML (Data Manipulation Language) statements in our latency metrics.
func shouldIncludeStmtInLatencyMetrics(stmt *Statement) bool {
return stmt.AST.StatementType() == tree.TypeDML
}
func getNodesFromPlanner(planner *planner) []int64 {
// Retrieve the list of all nodes which the statement was executed on.
var nodes []int64
if _, ok := planner.instrumentation.Tracing(); !ok {
trace := planner.instrumentation.sp.GetRecording(tracingpb.RecordingStructured)
// ForEach returns nodes in order.
execinfrapb.ExtractNodesFromSpans(trace).ForEach(func(i int) {
nodes = append(nodes, int64(i))
})
}
return nodes
}