-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
instrumentation.go
545 lines (481 loc) · 17.5 KB
/
instrumentation.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2020 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 (
"bytes"
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec/explain"
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/stmtdiagnostics"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/cockroachdb/errors"
)
var collectTxnStatsSampleRate = settings.RegisterFloatSetting(
"sql.txn_stats.sample_rate",
"the probability that a given transaction will collect execution statistics (displayed in the DB Console)",
0.1,
func(f float64) error {
if f < 0 || f > 1 {
return errors.New("value must be between 0 and 1 inclusive")
}
return nil
},
)
// instrumentationHelper encapsulates the logic around extracting information
// about the execution of a statement, like bundles and traces. Typical usage:
//
// - SetOutputMode() can be used as necessary if we are running an EXPLAIN
// ANALYZE variant.
//
// - Setup() is called before query execution.
//
// - SetDiscardRows(), ShouldDiscardRows(), ShouldSaveFlows(),
// ShouldBuildExplainPlan(), RecordExplainPlan(), RecordPlanInfo(),
// PlanForStats() can be called at any point during execution.
//
// - Finish() is called after query execution.
//
type instrumentationHelper struct {
outputMode outputMode
// explainFlags is used when outputMode is explainAnalyzePlanOutput or
// explainAnalyzeDistSQLOutput.
explainFlags explain.Flags
// Query fingerprint (anonymized statement).
fingerprint string
implicitTxn bool
codec keys.SQLCodec
// -- The following fields are initialized by Setup() --
// collectBundle is set when we are collecting a diagnostics bundle for a
// statement; it triggers saving of extra information like the plan string.
collectBundle bool
// collectExecStats is set when we are collecting execution statistics for a
// statement.
collectExecStats bool
// discardRows is set if we want to discard any results rather than sending
// them back to the client. Used for testing/benchmarking. Note that the
// resulting schema or the plan are not affected.
// See EXECUTE .. DISCARD ROWS.
discardRows bool
diagRequestID stmtdiagnostics.RequestID
finishCollectionDiagnostics func()
withStatementTrace func(trace tracing.Recording, stmt string)
sp *tracing.Span
// If true, the instrumentation helper is responsible for finishing sp.
finishSpan bool
origCtx context.Context
evalCtx *tree.EvalContext
// If savePlanForStats is true, the explainPlan will be collected and returned
// via PlanForStats().
savePlanForStats bool
explainPlan *explain.Plan
distribution physicalplan.PlanDistribution
vectorized bool
traceMetadata execNodeTraceMetadata
}
// outputMode indicates how the statement output needs to be populated (for
// EXPLAIN ANALYZE variants).
type outputMode int8
const (
unmodifiedOutput outputMode = iota
explainAnalyzeDebugOutput
explainAnalyzePlanOutput
explainAnalyzeDistSQLOutput
)
// SetOutputMode can be called before Setup, if we are running an EXPLAIN
// ANALYZE variant.
func (ih *instrumentationHelper) SetOutputMode(outputMode outputMode, explainFlags explain.Flags) {
ih.outputMode = outputMode
ih.explainFlags = explainFlags
}
// Setup potentially enables verbose tracing for the statement, depending on
// output mode or statement diagnostic activation requests. Finish() must be
// called after the statement finishes execution (unless needFinish=false, in
// which case Finish() is a no-op).
func (ih *instrumentationHelper) Setup(
ctx context.Context,
cfg *ExecutorConfig,
appStats *appStats,
p *planner,
stmtDiagnosticsRecorder *stmtdiagnostics.Registry,
fingerprint string,
implicitTxn bool,
collectExecStats bool,
) (newCtx context.Context, needFinish bool) {
ih.fingerprint = fingerprint
ih.implicitTxn = implicitTxn
ih.codec = cfg.Codec
switch ih.outputMode {
case explainAnalyzeDebugOutput:
ih.collectBundle = true
// EXPLAIN ANALYZE (DEBUG) does not return the rows for the given query;
// instead it returns some text which includes a URL.
// TODO(radu): maybe capture some of the rows and include them in the
// bundle.
ih.discardRows = true
case explainAnalyzePlanOutput, explainAnalyzeDistSQLOutput:
ih.discardRows = true
default:
ih.collectBundle, ih.diagRequestID, ih.finishCollectionDiagnostics =
stmtDiagnosticsRecorder.ShouldCollectDiagnostics(ctx, fingerprint)
}
ih.withStatementTrace = cfg.TestingKnobs.WithStatementTrace
ih.savePlanForStats = appStats.shouldSaveLogicalPlanDescription(fingerprint, implicitTxn)
if sp := tracing.SpanFromContext(ctx); sp != nil {
ih.sp = sp
ih.finishSpan = false
if sp.IsVerbose() {
// If verbose tracing was enabled at a higher level, stats
// collection is enabled so that stats are shown in the traces, but
// no extra work is needed by the instrumentationHelper.
ih.collectExecStats = true
return ctx, false
}
} else {
if util.CrdbTestBuild {
panic(errors.AssertionFailedf("the context doesn't have a tracing span"))
}
}
ih.collectExecStats = collectExecStats
if !ih.collectBundle && ih.withStatementTrace == nil && ih.outputMode == unmodifiedOutput {
if ih.collectExecStats {
ih.origCtx = ctx
if ih.sp != nil {
// The span present in the context is sufficient for us to
// record stats as structured metadata, so there is nothing else
// to do.
return ctx, true
}
// This is an edge case when the span is not present in the original
// context for some reason. This should *never* happen, but we
// choose to be defensive about it.
newCtx, ih.sp = tracing.EnsureChildSpan(ctx, cfg.AmbientCtx.Tracer, "traced statement")
ih.finishSpan = true
return newCtx, true
}
return ctx, false
}
ih.collectExecStats = true
ih.traceMetadata = make(execNodeTraceMetadata)
ih.origCtx = ctx
ih.evalCtx = p.EvalContext()
newCtx, ih.sp = tracing.StartVerboseTrace(ctx, cfg.AmbientCtx.Tracer, "traced statement")
ih.finishSpan = true
return newCtx, true
}
func (ih *instrumentationHelper) Finish(
cfg *ExecutorConfig,
appStats *appStats,
txnStats *execstats.QueryLevelStats,
statsCollector *sqlStatsCollector,
p *planner,
ast tree.Statement,
stmtRawSQL string,
res RestrictedCommandResult,
retErr error,
) error {
ctx := ih.origCtx
if ih.finishSpan {
ih.sp.Finish()
}
// Record the statement information that we've collected.
// Note that in case of implicit transactions, the trace contains the auto-commit too.
trace := ih.sp.GetRecording()
if ih.withStatementTrace != nil {
ih.withStatementTrace(trace, stmtRawSQL)
}
if ih.traceMetadata != nil && ih.explainPlan != nil {
ih.traceMetadata.annotateExplain(
ih.explainPlan,
p.curPlan.distSQLFlowInfos,
trace,
cfg.TestingKnobs.DeterministicExplainAnalyze,
)
}
// Get the query-level stats.
var flowsMetadata []*execstats.FlowsMetadata
for _, flowInfo := range p.curPlan.distSQLFlowInfos {
flowsMetadata = append(flowsMetadata, flowInfo.flowsMetadata)
}
queryLevelStats, err := execstats.GetQueryLevelStats(trace, cfg.TestingKnobs.DeterministicExplainAnalyze, flowsMetadata)
if err != nil {
const msg = "error getting query level stats for statement: %s: %+v"
if util.CrdbTestBuild {
panic(fmt.Sprintf(msg, ih.fingerprint, err))
}
log.VInfof(ctx, 1, msg, ih.fingerprint, err)
} else {
// TODO(radu): this should be unified with other stmt stats accesses.
stmtStats, _ := appStats.getStatsForStmt(ih.fingerprint, ih.implicitTxn, retErr, false)
if stmtStats != nil {
stmtStats.recordExecStats(queryLevelStats)
txnStats.Accumulate(queryLevelStats)
}
}
var bundle diagnosticsBundle
if ih.collectBundle {
ie := p.extendedEvalCtx.InternalExecutor.(*InternalExecutor)
placeholders := p.extendedEvalCtx.Placeholders
ob := ih.buildExplainAnalyzePlan(
explain.Flags{Verbose: true, ShowTypes: true},
&statsCollector.phaseTimes,
&queryLevelStats,
)
bundle = buildStatementBundle(
ih.origCtx, cfg.DB, ie, &p.curPlan, ob.BuildString(), trace, placeholders,
)
bundle.insert(ctx, ih.fingerprint, ast, cfg.StmtDiagnosticsRecorder, ih.diagRequestID)
if ih.finishCollectionDiagnostics != nil {
ih.finishCollectionDiagnostics()
telemetry.Inc(sqltelemetry.StatementDiagnosticsCollectedCounter)
}
}
// If there was a communication error already, no point in setting any
// results.
if retErr != nil {
return retErr
}
switch ih.outputMode {
case explainAnalyzeDebugOutput:
return setExplainBundleResult(ctx, res, bundle, cfg)
case explainAnalyzePlanOutput, explainAnalyzeDistSQLOutput:
phaseTimes := &statsCollector.phaseTimes
var flows []flowInfo
if ih.outputMode == explainAnalyzeDistSQLOutput {
flows = p.curPlan.distSQLFlowInfos
}
return ih.setExplainAnalyzeResult(ctx, res, phaseTimes, &queryLevelStats, flows, trace)
default:
return nil
}
}
// SetDiscardRows should be called when we want to discard rows for a
// non-ANALYZE statement (via EXECUTE .. DISCARD ROWS).
func (ih *instrumentationHelper) SetDiscardRows() {
ih.discardRows = true
}
// ShouldDiscardRows returns true if this is an EXPLAIN ANALYZE variant or
// SetDiscardRows() was called.
func (ih *instrumentationHelper) ShouldDiscardRows() bool {
return ih.discardRows
}
// ShouldSaveFlows is true if we should save the flow specifications (to be able
// to generate diagrams - when shouldSaveDiagrams() returns true - and to get
// query level stats when sampling statements).
func (ih *instrumentationHelper) ShouldSaveFlows() bool {
return ih.collectBundle || ih.outputMode == explainAnalyzeDistSQLOutput || ih.collectExecStats
}
// shouldSaveDiagrams returns whether saveFlows() function should also be saving
// diagrams in flowInfo objects.
func (ih *instrumentationHelper) shouldSaveDiagrams() bool {
return ih.collectBundle || ih.outputMode != unmodifiedOutput
}
// ShouldUseJobForCreateStats indicates if we should run CREATE STATISTICS as a
// job (normally true). It is false if we are running a statement under
// EXPLAIN ANALYZE, in which case we want to run the CREATE STATISTICS plan
// directly.
func (ih *instrumentationHelper) ShouldUseJobForCreateStats() bool {
return ih.outputMode == unmodifiedOutput
}
// ShouldBuildExplainPlan returns true if we should build an explain plan and
// call RecordExplainPlan.
func (ih *instrumentationHelper) ShouldBuildExplainPlan() bool {
return ih.collectBundle || ih.savePlanForStats || ih.outputMode == explainAnalyzePlanOutput ||
ih.outputMode == explainAnalyzeDistSQLOutput
}
// ShouldCollectExecStats returns true if we should collect statement execution
// statistics.
func (ih *instrumentationHelper) ShouldCollectExecStats() bool {
return ih.collectExecStats
}
// RecordExplainPlan records the explain.Plan for this query.
func (ih *instrumentationHelper) RecordExplainPlan(explainPlan *explain.Plan) {
ih.explainPlan = explainPlan
}
// RecordPlanInfo records top-level information about the plan.
func (ih *instrumentationHelper) RecordPlanInfo(
distribution physicalplan.PlanDistribution, vectorized bool,
) {
ih.distribution = distribution
ih.vectorized = vectorized
}
// PlanForStats returns the plan as an ExplainTreePlanNode tree, if it was
// collected (nil otherwise). It should be called after RecordExplainPlan() and
// RecordPlanInfo().
func (ih *instrumentationHelper) PlanForStats(ctx context.Context) *roachpb.ExplainTreePlanNode {
if ih.explainPlan == nil {
return nil
}
ob := explain.NewOutputBuilder(explain.Flags{
HideValues: true,
})
ob.AddDistribution(ih.distribution.String())
ob.AddVectorized(ih.vectorized)
if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
log.Warningf(ctx, "unable to emit explain plan tree: %v", err)
return nil
}
return ob.BuildProtoTree()
}
// buildExplainAnalyzePlan creates an explain.OutputBuilder and populates it
// with the EXPLAIN ANALYZE plan. BuildString/BuildStringRows can be used on the
// result.
func (ih *instrumentationHelper) buildExplainAnalyzePlan(
flags explain.Flags, phaseTimes *phaseTimes, queryStats *execstats.QueryLevelStats,
) *explain.OutputBuilder {
ob := explain.NewOutputBuilder(flags)
if ih.explainPlan == nil {
// Return an empty builder if there is no plan.
return ob
}
ob.AddPlanningTime(phaseTimes.getPlanningLatency())
ob.AddExecutionTime(phaseTimes.getRunLatency())
ob.AddDistribution(ih.distribution.String())
ob.AddVectorized(ih.vectorized)
if queryStats.KVRowsRead != 0 {
ob.AddKVReadStats(queryStats.KVRowsRead, queryStats.KVBytesRead)
}
if queryStats.KVTime != 0 {
ob.AddKVTime(queryStats.KVTime)
}
if queryStats.ContentionTime != 0 {
ob.AddContentionTime(queryStats.ContentionTime)
}
ob.AddMaxMemUsage(queryStats.MaxMemUsage)
ob.AddNetworkStats(queryStats.NetworkMessages, queryStats.NetworkBytesSent)
if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
ob.AddTopLevelField("error emitting plan", fmt.Sprint(err))
}
return ob
}
// setExplainAnalyzeResult sets the result for an EXPLAIN ANALYZE or EXPLAIN
// ANALYZE (DISTSQL) statement (in the former case, distSQLFlowInfos and trace
// are nil).
// Returns an error only if there was an error adding rows to the result.
func (ih *instrumentationHelper) setExplainAnalyzeResult(
ctx context.Context,
res RestrictedCommandResult,
phaseTimes *phaseTimes,
queryLevelStats *execstats.QueryLevelStats,
distSQLFlowInfos []flowInfo,
trace tracing.Recording,
) (commErr error) {
res.ResetStmtType(&tree.ExplainAnalyze{})
res.SetColumns(ctx, colinfo.ExplainPlanColumns)
if res.Err() != nil {
// Can't add rows if there was an error.
return nil //nolint:returnerrcheck
}
ob := ih.buildExplainAnalyzePlan(ih.explainFlags, phaseTimes, queryLevelStats)
rows := ob.BuildStringRows()
if distSQLFlowInfos != nil {
rows = append(rows, "")
for i, d := range distSQLFlowInfos {
var buf bytes.Buffer
if len(distSQLFlowInfos) > 1 {
fmt.Fprintf(&buf, "Diagram %d (%s): ", i+1, d.typ)
} else {
buf.WriteString("Diagram: ")
}
d.diagram.AddSpans(trace)
_, url, err := d.diagram.ToURL()
if err != nil {
buf.WriteString(err.Error())
} else {
buf.WriteString(url.String())
}
rows = append(rows, buf.String())
}
}
for _, row := range rows {
if err := res.AddRow(ctx, tree.Datums{tree.NewDString(row)}); err != nil {
return err
}
}
return nil
}
// execNodeTraceMetadata associates exec.Nodes with metadata for corresponding
// execution components.
// Currently, we only store info about processors. A node can correspond to
// multiple processors if the plan is distributed.
//
// TODO(radu): we perform similar processing of execution traces in various
// parts of the code. Come up with some common infrastructure that makes this
// easier.
type execNodeTraceMetadata map[exec.Node]execComponents
type execComponents []execinfrapb.ComponentID
// associateNodeWithComponents is called during planning, as processors are
// planned for an execution operator.
func (m execNodeTraceMetadata) associateNodeWithComponents(
node exec.Node, components execComponents,
) {
m[node] = components
}
// annotateExplain aggregates the statistics in the trace and annotates
// explain.Nodes with execution stats.
func (m execNodeTraceMetadata) annotateExplain(
plan *explain.Plan, flowInfos []flowInfo, spans []tracingpb.RecordedSpan, makeDeterministic bool,
) {
statsMap := execinfrapb.ExtractStatsFromSpans(spans, makeDeterministic)
var walk func(n *explain.Node)
walk = func(n *explain.Node) {
wrapped := n.WrappedNode()
if components, ok := m[wrapped]; ok {
var nodeStats exec.ExecutionStats
incomplete := false
var nodes util.FastIntSet
for _, c := range components {
if c.Type == execinfrapb.ComponentID_PROCESSOR {
nodes.Add(int(c.SQLInstanceID))
}
stats := statsMap[c]
if stats == nil {
incomplete = true
break
}
nodeStats.RowCount.MaybeAdd(stats.Output.NumTuples)
nodeStats.KVBytesRead.MaybeAdd(stats.KV.BytesRead)
nodeStats.KVRowsRead.MaybeAdd(stats.KV.TuplesRead)
}
// If we didn't get statistics for all processors, we don't show the
// incomplete results. In the future, we may consider an incomplete flag
// if we want to show them with a warning.
if !incomplete {
for i, ok := nodes.Next(0); ok; i, ok = nodes.Next(i + 1) {
nodeStats.Nodes = append(nodeStats.Nodes, fmt.Sprintf("n%d", i))
}
n.Annotate(exec.ExecutionStatsID, &nodeStats)
}
}
for i := 0; i < n.ChildCount(); i++ {
walk(n.Child(i))
}
}
walk(plan.Root)
for i := range plan.Subqueries {
walk(plan.Subqueries[i].Root.(*explain.Node))
}
for i := range plan.Checks {
walk(plan.Checks[i])
}
}