-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
traceanalyzer_test.go
340 lines (312 loc) · 10.7 KB
/
traceanalyzer_test.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
// 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 execstats_test
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/execstats"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/optional"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestTraceAnalyzer verifies that the TraceAnalyzer correctly calculates
// expected top-level statistics from a physical plan and an accompanying trace
// from that plan's execution. It does this by starting up a multi-node cluster,
// enabling tracing, capturing the physical plan of a test statement, and
// constructing a TraceAnalyzer from the resulting trace and physical plan.
func TestTraceAnalyzer(t *testing.T) {
defer log.Scope(t).Close(t)
defer leaktest.AfterTest(t)()
const (
testStmt = "SELECT * FROM test.foo ORDER BY v"
numNodes = 3
)
ctx := context.Background()
analyzerChan := make(chan *execstats.TraceAnalyzer, 1)
tc := serverutils.StartNewTestCluster(t, numNodes, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
ServerArgs: base.TestServerArgs{
UseDatabase: "test",
Knobs: base.TestingKnobs{
SQLExecutor: &sql.ExecutorTestingKnobs{
TestingSaveFlows: func(stmt string) func(map[base.SQLInstanceID]*execinfrapb.FlowSpec, execinfra.OpChains) error {
if stmt != testStmt {
return func(map[base.SQLInstanceID]*execinfrapb.FlowSpec, execinfra.OpChains) error { return nil }
}
return func(flows map[base.SQLInstanceID]*execinfrapb.FlowSpec, _ execinfra.OpChains) error {
flowsMetadata := execstats.NewFlowsMetadata(flows)
analyzer := execstats.NewTraceAnalyzer(flowsMetadata)
analyzerChan <- analyzer
return nil
}
},
},
DistSQL: &execinfra.TestingKnobs{
ForceDiskSpill: true,
},
},
}})
defer tc.Stopper().Stop(ctx)
const gatewayNode = 0
db := tc.ServerConn(gatewayNode)
sqlDB := sqlutils.MakeSQLRunner(db)
sqlutils.CreateTable(
t, db, "foo",
"k INT PRIMARY KEY, v INT",
30,
sqlutils.ToRowFn(sqlutils.RowIdxFn, sqlutils.RowModuloFn(2)),
)
sqlDB.Exec(t, "ALTER TABLE test.foo SPLIT AT VALUES (10), (20)")
sqlDB.Exec(
t,
fmt.Sprintf("ALTER TABLE test.foo EXPERIMENTAL_RELOCATE VALUES (ARRAY[%d], 0), (ARRAY[%d], 10), (ARRAY[%d], 20)",
tc.Server(0).GetFirstStoreID(),
tc.Server(1).GetFirstStoreID(),
tc.Server(2).GetFirstStoreID(),
),
)
execCfg := tc.Server(gatewayNode).ExecutorConfig().(sql.ExecutorConfig)
var (
rowexecTraceAnalyzer *execstats.TraceAnalyzer
colexecTraceAnalyzer *execstats.TraceAnalyzer
)
for _, vectorizeMode := range []sessiondatapb.VectorizeExecMode{sessiondatapb.VectorizeOff, sessiondatapb.VectorizeOn} {
execCtx, finishAndCollect := tracing.ContextWithRecordingSpan(ctx, execCfg.AmbientCtx.Tracer, t.Name())
defer finishAndCollect()
ie := execCfg.InternalExecutor
ie.SetSessionData(
&sessiondata.SessionData{
SessionData: sessiondatapb.SessionData{
VectorizeMode: vectorizeMode,
},
LocalOnlySessionData: sessiondatapb.LocalOnlySessionData{
DistSQLMode: sessiondatapb.DistSQLOn,
},
},
)
_, err := ie.ExecEx(
execCtx,
t.Name(),
nil, /* txn */
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
testStmt,
)
require.NoError(t, err)
trace := finishAndCollect()
analyzer := <-analyzerChan
require.NoError(t, analyzer.AddTrace(trace, true /* makeDeterministic */))
require.NoError(t, analyzer.ProcessStats())
switch vectorizeMode {
case sessiondatapb.VectorizeOff:
rowexecTraceAnalyzer = analyzer
case sessiondatapb.VectorizeOn:
colexecTraceAnalyzer = analyzer
default:
t.Fatalf("programming error, vectorize mode %s not handled", vectorizeMode)
}
}
for _, tc := range []struct {
name string
analyzer *execstats.TraceAnalyzer
}{
{
name: "RowExec",
analyzer: rowexecTraceAnalyzer,
},
{
name: "ColExec",
analyzer: colexecTraceAnalyzer,
},
} {
t.Run(tc.name, func(t *testing.T) {
nodeLevelStats := tc.analyzer.GetNodeLevelStats()
require.Equal(
t, numNodes-1, len(nodeLevelStats.NetworkBytesSentGroupedByNode), "expected all nodes minus the gateway node to have sent bytes",
)
require.Equal(
t, numNodes, len(nodeLevelStats.MaxMemoryUsageGroupedByNode), "expected all nodes to have specified maximum memory usage",
)
require.Equal(
t, numNodes, len(nodeLevelStats.MaxDiskUsageGroupedByNode), "expected all nodes to have specified maximum disk usage",
)
queryLevelStats := tc.analyzer.GetQueryLevelStats()
// The stats don't count the actual bytes, but they are a synthetic value
// based on the number of tuples. In this test 21 tuples flow over the
// network.
require.Equal(t, int64(21*8), queryLevelStats.NetworkBytesSent)
// Soft check that MaxMemUsage is set to a non-zero value. The actual
// value differs between test runs due to metamorphic randomization.
require.Greater(t, queryLevelStats.MaxMemUsage, int64(0))
require.Equal(t, int64(1048576), queryLevelStats.MaxDiskUsage)
require.Equal(t, int64(30), queryLevelStats.KVRowsRead)
// For tests, the bytes read is based on the number of rows read, rather
// than actual bytes read.
require.Equal(t, int64(30*8), queryLevelStats.KVBytesRead)
// For tests, network messages is a synthetic value based on the number of
// network tuples. In this test 21 tuples flow over the network.
require.Equal(t, int64(21/2), queryLevelStats.NetworkMessages)
})
}
}
func TestTraceAnalyzerProcessStats(t *testing.T) {
const (
node1KVTime = 1 * time.Second
node1ContentionTime = 2 * time.Second
node2KVTime = 3 * time.Second
node2ContentionTime = 4 * time.Second
cumulativeKVTime = node1KVTime + node2KVTime
cumulativeContentionTime = node1ContentionTime + node2ContentionTime
)
a := &execstats.TraceAnalyzer{FlowsMetadata: &execstats.FlowsMetadata{}}
n1 := base.SQLInstanceID(1)
n2 := base.SQLInstanceID(2)
a.AddComponentStats(
&execinfrapb.ComponentStats{
Component: execinfrapb.ProcessorComponentID(
n1,
execinfrapb.FlowID{UUID: uuid.MakeV4()},
1, /* processorID */
),
KV: execinfrapb.KVStats{
KVTime: optional.MakeTimeValue(node1KVTime),
ContentionTime: optional.MakeTimeValue(node1ContentionTime),
},
},
)
a.AddComponentStats(
&execinfrapb.ComponentStats{
Component: execinfrapb.ProcessorComponentID(
n2,
execinfrapb.FlowID{UUID: uuid.MakeV4()},
2, /* processorID */
),
KV: execinfrapb.KVStats{
KVTime: optional.MakeTimeValue(node2KVTime),
ContentionTime: optional.MakeTimeValue(node2ContentionTime),
},
},
)
expected := execstats.QueryLevelStats{
KVTime: cumulativeKVTime,
ContentionTime: cumulativeContentionTime,
}
assert.NoError(t, a.ProcessStats())
if got := a.GetQueryLevelStats(); !reflect.DeepEqual(got, expected) {
t.Errorf("ProcessStats() = %v, want %v", got, expected)
}
}
func TestQueryLevelStatsAccumulate(t *testing.T) {
a := execstats.QueryLevelStats{
NetworkBytesSent: 1,
MaxMemUsage: 2,
KVBytesRead: 3,
KVRowsRead: 4,
KVTime: 5 * time.Second,
NetworkMessages: 6,
ContentionTime: 7 * time.Second,
MaxDiskUsage: 8,
Regions: []string{"gcp-us-east1"},
}
b := execstats.QueryLevelStats{
NetworkBytesSent: 8,
MaxMemUsage: 9,
KVBytesRead: 10,
KVRowsRead: 11,
KVTime: 12 * time.Second,
NetworkMessages: 13,
ContentionTime: 14 * time.Second,
MaxDiskUsage: 15,
Regions: []string{"gcp-us-west1"},
}
expected := execstats.QueryLevelStats{
NetworkBytesSent: 9,
MaxMemUsage: 9,
KVBytesRead: 13,
KVRowsRead: 15,
KVTime: 17 * time.Second,
NetworkMessages: 19,
ContentionTime: 21 * time.Second,
MaxDiskUsage: 15,
Regions: []string{"gcp-us-east1", "gcp-us-west1"},
}
aCopy := a
a.Accumulate(b)
require.Equal(t, expected, a)
reflectedAccumulatedStats := reflect.ValueOf(a)
reflectedOriginalStats := reflect.ValueOf(aCopy)
for i := 0; i < reflectedAccumulatedStats.NumField(); i++ {
require.NotEqual(
t,
reflectedAccumulatedStats.Field(i).Interface(),
reflectedOriginalStats.Field(i).Interface(),
"no struct field should be the same after accumulation in this test but %s was unchanged, did you forget to update Accumulate?",
reflectedAccumulatedStats.Type().Field(i).Name,
)
}
}
// TestGetQueryLevelStatsAccumulates does a sanity check that GetQueryLevelStats
// accumulates the stats for all flows passed into it. It does so by creating
// two FlowsMetadata objects and, thus, simulating a subquery and a main query.
func TestGetQueryLevelStatsAccumulates(t *testing.T) {
const f1KVTime = 1 * time.Second
const f2KVTime = 3 * time.Second
// Artificially inject component stats directly into the FlowsMetadata (in
// the non-testing setting the stats come from the trace).
var f1, f2 execstats.FlowsMetadata
n1 := base.SQLInstanceID(1)
n2 := base.SQLInstanceID(2)
f1.AddComponentStats(
&execinfrapb.ComponentStats{
Component: execinfrapb.ProcessorComponentID(
n1,
execinfrapb.FlowID{UUID: uuid.MakeV4()},
1, /* processorID */
),
KV: execinfrapb.KVStats{
KVTime: optional.MakeTimeValue(f1KVTime),
},
},
)
f2.AddComponentStats(
&execinfrapb.ComponentStats{
Component: execinfrapb.ProcessorComponentID(
n2,
execinfrapb.FlowID{UUID: uuid.MakeV4()},
2, /* processorID */
),
KV: execinfrapb.KVStats{
KVTime: optional.MakeTimeValue(f2KVTime),
},
},
)
queryLevelStats, err := execstats.GetQueryLevelStats(
nil, /* trace */
false, /* deterministicExplainAnalyze */
[]*execstats.FlowsMetadata{&f1, &f2},
)
require.NoError(t, err)
require.Equal(t, f1KVTime+f2KVTime, queryLevelStats.KVTime)
}