-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcreate_stats_job_test.go
547 lines (479 loc) · 17.6 KB
/
create_stats_job_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
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
546
547
// Copyright 2019 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 stats_test
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/rowexec"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/jobutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
// TestCreateStatsControlJob tests that PAUSE JOB, RESUME JOB, and CANCEL JOB
// work as intended on create statistics jobs.
func TestCreateStatsControlJob(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
// Test with 3 nodes and rowexec.SamplerProgressInterval=100 to ensure
// that progress metadata is sent correctly after every 100 input rows.
const nodes = 3
defer func(oldSamplerInterval int, oldSampleAgggregatorInterval time.Duration) {
rowexec.SamplerProgressInterval = oldSamplerInterval
rowexec.SampleAggregatorProgressInterval = oldSampleAgggregatorInterval
}(rowexec.SamplerProgressInterval, rowexec.SampleAggregatorProgressInterval)
rowexec.SamplerProgressInterval = 100
rowexec.SampleAggregatorProgressInterval = time.Millisecond
var allowRequest chan struct{}
var serverArgs base.TestServerArgs
filter, setTableID := createStatsRequestFilter(&allowRequest)
params := base.TestClusterArgs{ServerArgs: serverArgs}
params.ServerArgs.Knobs.JobsTestingKnobs = jobs.NewTestingKnobsWithShortIntervals()
params.ServerArgs.Knobs.Store = &kvserver.StoreTestingKnobs{
TestingRequestFilter: filter,
}
ctx := context.Background()
tc := testcluster.StartTestCluster(t, nodes, params)
defer tc.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (x INT PRIMARY KEY)`)
var tID descpb.ID
sqlDB.QueryRow(t, `SELECT 'd.t'::regclass::int`).Scan(&tID)
sqlDB.Exec(t, `INSERT INTO d.t SELECT generate_series(1,1000)`)
t.Run("cancel", func(t *testing.T) {
// Test that CREATE STATISTICS can be canceled.
query := `CREATE STATISTICS s1 FROM d.t`
setTableID(tID)
if _, err := jobutils.RunJob(
t, sqlDB, &allowRequest, []string{"cancel"}, query,
); err == nil {
t.Fatal("expected an error")
}
// There should be no results here.
sqlDB.CheckQueryResults(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE d.t]`,
[][]string{})
})
t.Run("pause", func(t *testing.T) {
// Test that CREATE STATISTICS can be paused and resumed.
query := `CREATE STATISTICS s2 FROM d.t`
jobID, err := jobutils.RunJob(
t, sqlDB, &allowRequest, []string{"PAUSE"}, query,
)
if !testutils.IsError(err, "pause") && !testutils.IsError(err, "liveness") {
t.Fatalf("unexpected: %v", err)
}
// There should be no results here.
sqlDB.CheckQueryResults(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE d.t]`,
[][]string{})
testutils.SucceedsSoon(t, func() error {
_, err := sqlDB.DB.ExecContext(context.Background(), `RESUME JOB $1`, jobID)
return err
})
jobutils.WaitForJobToSucceed(t, sqlDB, jobID)
// Now the job should have succeeded in producing stats.
sqlDB.CheckQueryResults(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE d.t]`,
[][]string{
{"s2", "{x}", "1000"},
})
})
}
func TestCreateStatisticsCanBeCancelled(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
skip.UnderStress(t, "test can be slow to quiesce because of filter")
var allowRequest chan struct{}
var serverArgs base.TestServerArgs
filter, setTableID := createStatsRequestFilter(&allowRequest)
serverArgs.Knobs.JobsTestingKnobs = jobs.NewTestingKnobsWithShortIntervals()
serverArgs.Knobs.Store = &kvserver.StoreTestingKnobs{
TestingRequestFilter: filter,
}
ctx := context.Background()
tc, conn, _ := serverutils.StartServer(t, serverArgs)
defer tc.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(conn)
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (x INT PRIMARY KEY) WITH (sql_stats_automatic_collection_enabled = false)`)
sqlDB.Exec(t, `INSERT INTO d.t SELECT generate_series(1,1000)`)
var tID descpb.ID
sqlDB.QueryRow(t, `SELECT 'd.t'::regclass::int`).Scan(&tID)
// Run CREATE STATISTICS and wait for to create the job.
allowRequest = make(chan struct{})
errCh := make(chan error)
go func() {
_, err := conn.Exec(`CREATE STATISTICS s1 FROM d.t`)
errCh <- err
}()
allowRequest <- struct{}{}
// If we end up ever retrying the txn, we should not block on
// the allow request channel again.
setTableID(descpb.InvalidID)
testutils.SucceedsSoon(t, func() error {
row := conn.QueryRow("SELECT query_id FROM [SHOW CLUSTER STATEMENTS] WHERE query LIKE 'CREATE STATISTICS%';")
var queryID string
if err := row.Scan(&queryID); err != nil {
return err
}
_, err := conn.Exec("CANCEL QUERIES VALUES ((SELECT query_id FROM [SHOW CLUSTER STATEMENTS] WHERE query LIKE 'CREATE STATISTICS%'));")
return err
})
err := <-errCh
allowRequest <- struct{}{}
require.ErrorContains(t, err, "pq: query execution canceled")
}
func TestAtMostOneRunningCreateStats(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
var allowRequest chan struct{}
var serverArgs base.TestServerArgs
filter, setTableID := createStatsRequestFilter(&allowRequest)
params := base.TestClusterArgs{ServerArgs: serverArgs}
params.ServerArgs.Knobs.JobsTestingKnobs = jobs.NewTestingKnobsWithShortIntervals()
params.ServerArgs.Knobs.Store = &kvserver.StoreTestingKnobs{
TestingRequestFilter: filter,
}
ctx := context.Background()
const nodes = 1
tc := testcluster.StartTestCluster(t, nodes, params)
defer tc.Stopper().Stop(ctx)
conn := tc.Conns[0]
sqlDB := sqlutils.MakeSQLRunner(conn)
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (x INT PRIMARY KEY)`)
sqlDB.Exec(t, `INSERT INTO d.t SELECT generate_series(1,1000)`)
var tID descpb.ID
sqlDB.QueryRow(t, `SELECT 'd.t'::regclass::int`).Scan(&tID)
setTableID(tID)
// Start a CREATE STATISTICS run and wait until it's done one scan.
allowRequest = make(chan struct{})
errCh := make(chan error)
go func() {
_, err := conn.Exec(`CREATE STATISTICS s1 FROM d.t`)
errCh <- err
}()
select {
case allowRequest <- struct{}{}:
case err := <-errCh:
t.Fatal(err)
}
autoStatsRunShouldFail := func() {
_, err := conn.Exec(`CREATE STATISTICS __auto__ FROM d.t`)
expected := "another CREATE STATISTICS job is already running"
if !testutils.IsError(err, expected) {
t.Fatalf("expected '%s' error, but got %v", expected, err)
}
}
// Attempt to start an automatic stats run. It should fail.
autoStatsRunShouldFail()
// PAUSE JOB does not bloack until the job is paused but only requests it.
// Wait until the job is set to paused.
var jobID jobspb.JobID
sqlDB.QueryRow(t, `SELECT id FROM system.jobs ORDER BY created DESC LIMIT 1`).Scan(&jobID)
opts := retry.Options{
InitialBackoff: 1 * time.Millisecond,
MaxBackoff: time.Second,
Multiplier: 2,
}
if err := retry.WithMaxAttempts(context.Background(), opts, 10, func() error {
_, err := sqlDB.DB.ExecContext(context.Background(), `PAUSE JOB $1`, jobID)
if err != nil {
t.Fatal(err)
}
var status string
sqlDB.QueryRow(t, `SELECT status FROM system.jobs WHERE id = $1 LIMIT 1`, jobID).Scan(&status)
if status != "paused" {
return errors.New("could not pause job")
}
return err
}); err != nil {
t.Fatal(err)
}
// Starting another automatic stats run should still fail.
autoStatsRunShouldFail()
// Attempt to start a regular stats run. It should succeed.
errCh2 := make(chan error)
go func() {
_, err := conn.Exec(`CREATE STATISTICS s2 FROM d.t`)
errCh2 <- err
}()
select {
case allowRequest <- struct{}{}:
case err := <-errCh:
t.Fatal(err)
case err := <-errCh2:
t.Fatal(err)
}
close(allowRequest)
// Verify that the second job completed successfully.
if err := <-errCh2; err != nil {
t.Fatalf("create stats job should have completed: %s", err)
}
// Verify that the first job completed successfully.
sqlDB.Exec(t, fmt.Sprintf("RESUME JOB %d", jobID))
jobutils.WaitForJobToSucceed(t, sqlDB, jobID)
<-errCh
}
func TestDeleteFailedJob(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
serverArgs := base.TestServerArgs{Knobs: base.TestingKnobs{JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals()}}
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{ServerArgs: serverArgs})
defer tc.Stopper().Stop(ctx)
conn := tc.Conns[0]
sqlDB := sqlutils.MakeSQLRunner(conn)
sqlDB.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false`)
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (x INT PRIMARY KEY)`)
sqlDB.Exec(t, `CREATE TABLE d.u (x INT PRIMARY KEY)`)
sqlDB.Exec(t, `INSERT INTO d.t SELECT generate_series(1,1000)`)
sqlDB.Exec(t, `INSERT INTO d.u SELECT generate_series(1,1000)`)
// Start two CREATE STATISTICS runs at once.
errCh1 := make(chan error)
go func() {
_, err := conn.Exec(`CREATE STATISTICS __auto__ FROM d.t`)
errCh1 <- err
}()
errCh2 := make(chan error)
go func() {
_, err := conn.Exec(`CREATE STATISTICS __auto__ FROM d.u`)
errCh2 <- err
}()
err1 := <-errCh1
err2 := <-errCh2
// At least one of the jobs should have succeeded.
if err1 != nil && err2 != nil {
t.Fatalf("one job should have succeeded but both failed. err1:%v, err2:%v", err1, err2)
}
// Check that if one of the jobs failed, it was deleted and doesn't show up in
// SHOW AUTOMATIC JOBS.
// Note: if this test fails, it will likely show up by using stressrace.
if res := sqlDB.QueryStr(t,
`SELECT statement, status, error FROM [SHOW AUTOMATIC JOBS] WHERE status = $1`,
jobs.StatusFailed,
); len(res) != 0 {
t.Fatalf("job should have been deleted but found: %v", res)
}
}
// TestCreateStatsProgress tests that progress reporting works correctly
// for the CREATE STATISTICS job.
func TestCreateStatsProgress(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
defer func(oldProgressInterval time.Duration) {
rowexec.SampleAggregatorProgressInterval = oldProgressInterval
}(rowexec.SampleAggregatorProgressInterval)
rowexec.SampleAggregatorProgressInterval = time.Nanosecond
defer func(oldProgressInterval int) {
rowexec.SamplerProgressInterval = oldProgressInterval
}(rowexec.SamplerProgressInterval)
rowexec.SamplerProgressInterval = 10
var allowRequest chan struct{}
var serverArgs base.TestServerArgs
filter, setTableID := createStatsRequestFilter(&allowRequest)
params := base.TestClusterArgs{ServerArgs: serverArgs}
params.ServerArgs.Knobs.Store = &kvserver.StoreTestingKnobs{
TestingRequestFilter: filter,
}
params.ServerArgs.Knobs.DistSQL = &execinfra.TestingKnobs{
// Force the stats job to iterate through the input rows instead of reading
// them all at once.
TableReaderBatchBytesLimit: 100,
}
ctx := context.Background()
const nodes = 1
tc := testcluster.StartTestCluster(t, nodes, params)
defer tc.Stopper().Stop(ctx)
conn := tc.Conns[0]
sqlDB := sqlutils.MakeSQLRunner(conn)
sqlDB.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false`)
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (i INT8 PRIMARY KEY)`)
sqlDB.Exec(t, `INSERT INTO d.t SELECT generate_series(1,1000)`)
var tID descpb.ID
sqlDB.QueryRow(t, `SELECT 'd.t'::regclass::int`).Scan(&tID)
setTableID(tID)
getFractionCompleted := func(jobID jobspb.JobID) float32 {
var progress *jobspb.Progress
testutils.SucceedsSoon(t, func() error {
progress = jobutils.GetJobProgress(t, sqlDB, jobID)
if progress.Progress == nil {
return errors.Errorf("progress is nil. jobID: %d", jobID)
}
return nil
})
return progress.Progress.(*jobspb.Progress_FractionCompleted).FractionCompleted
}
const query = `CREATE STATISTICS s1 FROM d.t`
// Start a CREATE STATISTICS run and wait until it has scanned part of the
// table.
allowRequest = make(chan struct{})
errCh := make(chan error)
go func() {
_, err := conn.Exec(query)
errCh <- err
}()
// Ten iterations here allows us to read some of the rows but not all.
for i := 0; i < 10; i++ {
select {
case allowRequest <- struct{}{}:
case err := <-errCh:
if err == nil {
t.Fatalf("query unexpectedly finished")
} else {
t.Fatal(err)
}
}
}
// Fetch the new job ID since we know it's running now.
jobID := jobutils.GetLastJobID(t, sqlDB)
// Ensure that 0 progress has been recorded since there are no existing
// stats available to estimate progress.
fractionCompleted := getFractionCompleted(jobID)
if fractionCompleted != 0 {
t.Fatalf(
"create stats should not have recorded progress, but progress is %f",
fractionCompleted,
)
}
// Allow the job to complete and verify that the client didn't see anything
// amiss.
close(allowRequest)
if err := <-errCh; err != nil {
t.Fatalf("create stats job should have completed: %s", err)
}
// Verify that full progress is now recorded.
fractionCompleted = getFractionCompleted(jobID)
if fractionCompleted != 1 {
t.Fatalf(
"create stats should have recorded full progress, but progress is %f",
fractionCompleted,
)
}
// Invalidate the stats cache so that we can be sure to get the latest stats.
var tableID descpb.ID
sqlDB.QueryRow(t, `SELECT id FROM system.namespace WHERE name = 't'`).Scan(&tableID)
tc.Servers[0].ExecutorConfig().(sql.ExecutorConfig).TableStatsCache.InvalidateTableStats(
ctx, tableID,
)
// Start another CREATE STATISTICS run and wait until it has scanned part of
// the table.
allowRequest = make(chan struct{})
go func() {
_, err := conn.Exec(query)
errCh <- err
}()
// Ten iterations here allows us to read some of the rows but not all.
for i := 0; i < 10; i++ {
select {
case allowRequest <- struct{}{}:
case err := <-errCh:
if err == nil {
t.Fatalf("query unexpectedly finished")
} else {
t.Fatal(err)
}
}
}
// Fetch the new job ID since we know it's running now.
jobID = jobutils.GetLastJobID(t, sqlDB)
// Ensure that partial progress has been recorded since there are existing
// stats available.
fractionCompleted = getFractionCompleted(jobID)
if fractionCompleted <= 0 || fractionCompleted > 0.99 {
t.Fatalf(
"create stats should have recorded partial progress, but progress is %f",
fractionCompleted,
)
}
// Allow the job to complete and verify that the client didn't see anything
// amiss.
close(allowRequest)
if err := <-errCh; err != nil {
t.Fatalf("create stats job should have completed: %s", err)
}
// Verify that full progress is now recorded.
fractionCompleted = getFractionCompleted(jobID)
if fractionCompleted != 1 {
t.Fatalf(
"create stats should have recorded full progress, but progress is %f",
fractionCompleted,
)
}
}
func TestCreateStatsAsOfTime(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{})
defer tc.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])
sqlDB.Exec(t, `CREATE DATABASE d`)
sqlDB.Exec(t, `CREATE TABLE d.t (x INT PRIMARY KEY)`)
var ts1 []uint8
sqlDB.QueryRow(t, `
INSERT INTO d.t VALUES (1)
RETURNING cluster_logical_timestamp();
`).Scan(&ts1)
sqlDB.Exec(t, `INSERT INTO d.t VALUES (2)`)
sqlDB.Exec(t, fmt.Sprintf("CREATE STATISTICS s FROM d.t AS OF SYSTEM TIME %s", string(ts1)))
// Check that we only see the first row, not the second.
sqlDB.CheckQueryResults(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE d.t]`,
[][]string{
{"s", "{x}", "1"},
})
}
// Create a blocking request filter for the actions related
// to CREATE STATISTICS, i.e. Scanning a user table. See discussion
// on jobutils.RunJob for where this might be useful.
func createStatsRequestFilter(
allowProgressIota *chan struct{},
) (kvserverbase.ReplicaRequestFilter, func(descpb.ID)) {
var tableToBlock atomic.Value
tableToBlock.Store(descpb.InvalidID)
return func(ctx context.Context, ba *kvpb.BatchRequest) *kvpb.Error {
if req, ok := ba.GetArg(kvpb.Scan); ok {
_, tableID, _ := encoding.DecodeUvarintAscending(req.(*kvpb.ScanRequest).Key)
// Ensure that the tableID is what we expect it to be.
if tableID > 0 && descpb.ID(tableID) == tableToBlock.Load().(descpb.ID) {
// Read from the channel twice to allow jobutils.RunJob to complete
// even though there is only one ScanRequest.
<-*allowProgressIota
<-*allowProgressIota
}
}
return nil
}, func(id descpb.ID) { tableToBlock.Store(id) }
}