-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
instrumentation.go
479 lines (418 loc) · 15.6 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
// 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/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/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
)
// 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
// collectStast is set when we are collecting stats for a statement.
collectStats 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
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,
) (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 !ih.collectBundle && ih.withStatementTrace == nil && ih.outputMode == unmodifiedOutput {
// TODO(asubiotto): Create a span for stat collection in future commit.
return ctx, false
}
ih.collectStats = true
ih.traceMetadata = make(execNodeTraceMetadata)
ih.origCtx = ctx
ih.evalCtx = p.EvalContext()
newCtx, ih.sp = tracing.StartVerboseTrace(ctx, cfg.AmbientCtx.Tracer, "traced statement")
return newCtx, true
}
func (ih *instrumentationHelper) Finish(
cfg *ExecutorConfig,
appStats *appStats,
statsCollector *sqlStatsCollector,
p *planner,
ast tree.Statement,
stmtRawSQL string,
res RestrictedCommandResult,
retErr error,
) error {
if ih.sp == nil {
return retErr
}
// Record the statement information that we've collected.
// Note that in case of implicit transactions, the trace contains the auto-commit too.
ih.sp.Finish()
ctx := ih.origCtx
trace := ih.sp.GetRecording()
if ih.withStatementTrace != nil {
ih.withStatementTrace(trace, stmtRawSQL)
}
if ih.traceMetadata != nil && ih.explainPlan != nil {
ih.traceMetadata.annotateExplain(ih.explainPlan, trace, cfg.TestingKnobs.DeterministicExplainAnalyze)
}
// TODO(radu): this should be unified with other stmt stats accesses.
stmtStats, _ := appStats.getStatsForStmt(ih.fingerprint, ih.implicitTxn, retErr, false)
if stmtStats != nil {
var flowMetadata []*execstats.FlowMetadata
for _, flowInfo := range p.curPlan.distSQLFlowInfos {
flowMetadata = append(flowMetadata, flowInfo.flowMetadata)
}
queryLevelStats, error := execstats.GetQueryLevelStats(trace, cfg.TestingKnobs.DeterministicExplainAnalyze, flowMetadata)
if error != nil {
log.VInfof(ctx, 1, "error getting query level stats for statement %s: %+v", ast, error)
}
stmtStats.mu.Lock()
// Record trace-related statistics. A count of 1 is passed given that this
// statistic is only recorded when statement diagnostics are enabled.
// TODO(asubiotto): NumericStat properties will be properly calculated
// once this statistic is always collected.
stmtStats.mu.data.BytesSentOverNetwork.Record(1 /* count */, float64(queryLevelStats.NetworkBytesSent))
stmtStats.mu.Unlock()
}
var bundle diagnosticsBundle
if ih.collectBundle {
ie := p.extendedEvalCtx.InternalExecutor.(*InternalExecutor)
placeholders := p.extendedEvalCtx.Placeholders
planStr := ih.planStringForBundle(&statsCollector.phaseTimes)
bundle = buildStatementBundle(
ih.origCtx, cfg.DB, ie, &p.curPlan, planStr, trace, placeholders,
)
bundle.insert(ctx, ih.fingerprint, ast, cfg.StmtDiagnosticsRecorder, ih.diagRequestID)
if ih.finishCollectionDiagnostics != nil {
ih.finishCollectionDiagnostics()
telemetry.Inc(sqltelemetry.StatementDiagnosticsCollectedCounter)
}
// Handle EXPLAIN ANALYZE (DEBUG). If there was a communication error
// already, no point in setting any results.
if ih.outputMode == explainAnalyzeDebugOutput && retErr == nil {
retErr = setExplainBundleResult(ctx, res, bundle, cfg)
}
}
// 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, 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).
func (ih *instrumentationHelper) ShouldSaveFlows() bool {
return ih.collectBundle || ih.outputMode == explainAnalyzeDistSQLOutput || ih.collectStats
}
// 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
}
// ShouldCollectStats returns true if we should collect statement execution
// statistics.
func (ih *instrumentationHelper) ShouldCollectStats() bool {
return ih.collectStats
}
// 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()
}
// planStringForBundle generates the plan tree as a string; used internally for bundles.
func (ih *instrumentationHelper) planStringForBundle(phaseTimes *phaseTimes) string {
if ih.explainPlan == nil {
return ""
}
ob := explain.NewOutputBuilder(explain.Flags{
Verbose: true,
ShowTypes: true,
})
ob.AddPlanningTime(phaseTimes.getPlanningLatency())
ob.AddExecutionTime(phaseTimes.getRunLatency())
ob.AddDistribution(ih.distribution.String())
ob.AddVectorized(ih.vectorized)
if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
return fmt.Sprintf("error emitting plan: %v", err)
}
return ob.BuildString()
}
// planRowsForExplainAnalyze generates the plan tree as a list of strings (one
// for each line).
// Used in explainAnalyzePlanOutput and explainAnalyzeDistSQLOutput modes.
func (ih *instrumentationHelper) planRowsForExplainAnalyze(phaseTimes *phaseTimes) []string {
if ih.explainPlan == nil {
return nil
}
ob := explain.NewOutputBuilder(ih.explainFlags)
ob.AddPlanningTime(phaseTimes.getPlanningLatency())
ob.AddExecutionTime(phaseTimes.getRunLatency())
ob.AddDistribution(ih.distribution.String())
ob.AddVectorized(ih.vectorized)
if err := emitExplain(ob, ih.evalCtx, ih.codec, ih.explainPlan); err != nil {
return []string{fmt.Sprintf("error emitting plan: %v", err)}
}
return ob.BuildStringRows()
}
// 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,
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
}
rows := ih.planRowsForExplainAnalyze(phaseTimes)
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())
}
}
rows = append(rows, "")
rows = append(rows, "WARNING: this statement is experimental!")
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, 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
for i := range components {
stats := statsMap[components[i]]
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 {
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])
}
}