-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
automatic_stats_test.go
579 lines (513 loc) · 19.1 KB
/
automatic_stats_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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright 2018 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
import (
"context"
"fmt"
"math"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/testutils"
"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/timeutil"
)
func TestMaybeRefreshStats(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false)
AutomaticStatisticsMinStaleRows.Override(ctx, &st.SV, 5)
sqlRun := sqlutils.MakeSQLRunner(sqlDB)
sqlRun.Exec(t,
`CREATE DATABASE t;
CREATE TABLE t.a (k INT PRIMARY KEY);
INSERT INTO t.a VALUES (1);
CREATE VIEW t.vw AS SELECT k, k+1 FROM t.a;`)
executor := s.InternalExecutor().(sqlutil.InternalExecutor)
descA := desctestutils.TestingGetPublicTableDescriptor(s.DB(), keys.SystemSQLCodec, "t", "a")
cache := NewTableStatisticsCache(
ctx,
10, /* cacheSize */
kvDB,
executor,
keys.SystemSQLCodec,
s.ClusterSettings(),
s.RangeFeedFactory().(*rangefeed.Factory),
s.CollectionFactory().(*descs.CollectionFactory),
)
refresher := MakeRefresher(s.AmbientCtx(), st, executor, cache, time.Microsecond /* asOfTime */)
// There should not be any stats yet.
if err := checkStatsCount(ctx, cache, descA, 0 /* expected */); err != nil {
t.Fatal(err)
}
// There are no stats yet, so this must refresh the statistics on table t
// even though rowsAffected=0.
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, descA, 1 /* expected */); err != nil {
t.Fatal(err)
}
// Try to refresh again. With rowsAffected=0, the probability of a refresh
// is 0, so refreshing will not succeed.
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, descA, 1 /* expected */); err != nil {
t.Fatal(err)
}
// Setting minStaleRows for the table prevents refreshing from occurring.
minStaleRowsForTable := int64(100000000)
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 10 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, &minStaleRowsForTable, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, descA, 1 /* expected */); err != nil {
t.Fatal(err)
}
// Setting fractionStaleRows for the table can also prevent refreshing from
// occurring, though this is a not a typical value for this setting.
fractionStaleRowsForTable := float64(100000000)
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 10 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
&fractionStaleRowsForTable /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, descA, 1 /* expected */); err != nil {
t.Fatal(err)
}
// With rowsAffected=10, refreshing should work. Since there are more rows
// updated than exist in the table, the probability of a refresh is 100%.
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 10 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, descA, 2 /* expected */); err != nil {
t.Fatal(err)
}
// Ensure that attempt to refresh stats on view does not result in re-
// enqueuing the attempt.
// TODO(rytaft): Should not enqueue views to begin with.
descVW := desctestutils.TestingGetPublicTableDescriptor(s.DB(), keys.SystemSQLCodec, "t", "vw")
refresher.maybeRefreshStats(
ctx, s.Stopper(), descVW.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
select {
case <-refresher.mutations:
t.Fatal("refresher should not re-enqueue attempt to create stats over view")
default:
}
}
func TestAverageRefreshTime(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false)
sqlRun := sqlutils.MakeSQLRunner(sqlDB)
sqlRun.Exec(t,
`CREATE DATABASE t;
CREATE TABLE t.a (k INT PRIMARY KEY);
INSERT INTO t.a VALUES (1);`)
executor := s.InternalExecutor().(sqlutil.InternalExecutor)
table := desctestutils.TestingGetPublicTableDescriptor(s.DB(), keys.SystemSQLCodec, "t", "a")
cache := NewTableStatisticsCache(
ctx,
10, /* cacheSize */
kvDB,
executor,
keys.SystemSQLCodec,
s.ClusterSettings(),
s.RangeFeedFactory().(*rangefeed.Factory),
s.CollectionFactory().(*descs.CollectionFactory),
)
refresher := MakeRefresher(s.AmbientCtx(), st, executor, cache, time.Microsecond /* asOfTime */)
// curTime is used as the current time throughout the test to ensure that the
// calculated average refresh time is consistent even if there are delays due
// to running the test under race.
curTime := timeutil.Now()
checkAverageRefreshTime := func(expected time.Duration) error {
return testutils.SucceedsSoonError(func() error {
stats, err := cache.GetTableStats(ctx, table)
if err != nil {
return err
}
if actual := avgRefreshTime(stats).Round(time.Minute); actual != expected {
return fmt.Errorf("expected avgRefreshTime %s but found %s",
expected.String(), actual.String())
}
return nil
})
}
// Checks that the most recent statistic was created less than (greater than)
// expectedAge time ago if lessThan is true (false).
checkMostRecentStat := func(expectedAge time.Duration, lessThan bool) error {
return testutils.SucceedsSoonError(func() error {
stats, err := cache.GetTableStats(ctx, table)
if err != nil {
return err
}
stat := mostRecentAutomaticStat(stats)
if stat == nil {
return fmt.Errorf("no recent automatic statistic found")
}
if !lessThan && stat.CreatedAt.After(curTime.Add(-1*expectedAge)) {
return fmt.Errorf("most recent stat is less than %s old. Created at: %s Current time: %s",
expectedAge, stat.CreatedAt, curTime,
)
}
if lessThan && stat.CreatedAt.Before(curTime.Add(-1*expectedAge)) {
return fmt.Errorf("most recent stat is more than %s old. Created at: %s Current time: %s",
expectedAge, stat.CreatedAt, curTime,
)
}
return nil
})
}
// Since there are no stats yet, avgRefreshTime should return the default
// value.
if err := checkAverageRefreshTime(defaultAverageTimeBetweenRefreshes); err != nil {
t.Fatal(err)
}
insertStat := func(
txn *kv.Txn, name string, columnIDs *tree.DArray, createdAt *tree.DTimestamp,
) error {
_, err := executor.Exec(
ctx, "insert-statistic", txn,
`INSERT INTO system.table_statistics (
"tableID",
"name",
"columnIDs",
"createdAt",
"rowCount",
"distinctCount",
"nullCount",
"avgSize"
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
table.GetID(),
name,
columnIDs,
createdAt,
1, /* rowCount */
1, /* distinctCount */
0, /* nullCount */
4, /* avgSize */
)
return err
}
// Add some stats on column k in table a with a name different from
// AutoStatsName, separated by three hours each, starting 7 hours ago.
if err := s.DB().Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
for i := 0; i < 10; i++ {
columnIDsVal := tree.NewDArray(types.Int)
if err := columnIDsVal.Append(tree.NewDInt(tree.DInt(1))); err != nil {
return err
}
createdAt, err := tree.MakeDTimestamp(
curTime.Add(time.Duration(-1*(i*3+7))*time.Hour), time.Hour,
)
if err != nil {
return err
}
name := fmt.Sprintf("stat%d", i)
if err := insertStat(txn, name, columnIDsVal, createdAt); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := checkStatsCount(ctx, cache, table, 10 /* expected */); err != nil {
t.Fatal(err)
}
// None of the stats have the name AutoStatsName, so avgRefreshTime
// should still return the default value.
if err := checkAverageRefreshTime(defaultAverageTimeBetweenRefreshes); err != nil {
t.Fatal(err)
}
// Add some stats on column v in table a with name AutoStatsName, separated
// by four hours each, starting 6 hours ago.
if err := s.DB().Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
for i := 0; i < 10; i++ {
columnIDsVal := tree.NewDArray(types.Int)
if err := columnIDsVal.Append(tree.NewDInt(tree.DInt(2))); err != nil {
return err
}
createdAt, err := tree.MakeDTimestamp(
curTime.Add(time.Duration(-1*(i*4+6))*time.Hour), time.Hour,
)
if err != nil {
return err
}
if err := insertStat(txn, jobspb.AutoStatsName, columnIDsVal, createdAt); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := checkStatsCount(ctx, cache, table, 20 /* expected */); err != nil {
t.Fatal(err)
}
// Check that the calculated average refresh time is four hours. Even though
// the average time between all stats just added is less than four hours, we
// should only calculate the average based on stats with the name __auto__,
// and only on the automatic column statistic that was most recently updated
// (in this case, column v, 6 hours ago).
if err := checkAverageRefreshTime(4 * time.Hour); err != nil {
t.Fatal(err)
}
// Check that the most recent stat is less than 8 hours old.
if err := checkMostRecentStat(8*time.Hour, true /* lessThan */); err != nil {
t.Fatal(err)
}
// The most recent stat is less than 8 hours old, which is less than 2x the
// average time between refreshes, so this call is not required to refresh
// the statistics on table t. With rowsAffected=0, the probability of refresh
// is 0.
refresher.maybeRefreshStats(
ctx, s.Stopper(), table.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, table, 20 /* expected */); err != nil {
t.Fatal(err)
}
// Add some stats on column k in table a with name AutoStatsName, separated
// by 1.5 hours each, starting 5 hours ago.
if err := s.DB().Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
for i := 0; i < 10; i++ {
columnIDsVal := tree.NewDArray(types.Int)
if err := columnIDsVal.Append(tree.NewDInt(tree.DInt(1))); err != nil {
return err
}
createdAt, err := tree.MakeDTimestamp(
curTime.Add(time.Duration(-1*(i*90+300))*time.Minute), time.Minute,
)
if err != nil {
return err
}
if err := insertStat(txn, jobspb.AutoStatsName, columnIDsVal, createdAt); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatal(err)
}
if err := checkStatsCount(ctx, cache, table, 30 /* expected */); err != nil {
t.Fatal(err)
}
// Check that the calculated average refresh time is 1.5 hours, based on the
// automatic column statistic that was most recently updated (in this case,
// column k, 5 hours ago).
if err := checkAverageRefreshTime(90 * time.Minute); err != nil {
t.Fatal(err)
}
// Check that the most recent stat is over 4 hours old.
if err := checkMostRecentStat(4*time.Hour, false /* lessThan */); err != nil {
t.Fatal(err)
}
// The most recent stat is over 4 hours old, which is more than 2x the
// average time between refreshes, so this call must refresh the statistics
// on table t even though rowsAffected=0. After refresh, only 15 stats should
// remain (5 from column k and 10 from column v), since the old stats on k
// were deleted.
refresher.maybeRefreshStats(
ctx, s.Stopper(), table.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
if err := checkStatsCount(ctx, cache, table, 15 /* expected */); err != nil {
t.Fatal(err)
}
}
func TestAutoStatsReadOnlyTables(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
st := cluster.MakeTestingClusterSettings()
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false)
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
sqlRun := sqlutils.MakeSQLRunner(sqlDB)
sqlRun.Exec(t,
`CREATE DATABASE t;
CREATE TABLE t.a (k INT PRIMARY KEY);`)
// Test that stats for tables in user-defined schemas are also refreshed.
sqlRun.Exec(t,
`CREATE SCHEMA my_schema;
CREATE TABLE my_schema.b (j INT PRIMARY KEY);`)
executor := s.InternalExecutor().(sqlutil.InternalExecutor)
cache := NewTableStatisticsCache(
ctx,
10, /* cacheSize */
kvDB,
executor,
keys.SystemSQLCodec,
s.ClusterSettings(),
s.RangeFeedFactory().(*rangefeed.Factory),
s.CollectionFactory().(*descs.CollectionFactory),
)
refresher := MakeRefresher(s.AmbientCtx(), st, executor, cache, time.Microsecond /* asOfTime */)
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, true)
if err := refresher.Start(
ctx, s.Stopper(), time.Millisecond, /* refreshInterval */
); err != nil {
t.Fatal(err)
}
// There should be one stat for table t.a.
sqlRun.CheckQueryResultsRetry(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE t.a]`,
[][]string{
{"__auto__", "{k}", "0"},
})
// There should be one stat for table my_schema.b.
sqlRun.CheckQueryResultsRetry(t,
`SELECT statistics_name, column_names, row_count FROM [SHOW STATISTICS FOR TABLE my_schema.b]`,
[][]string{
{"__auto__", "{j}", "0"},
})
}
func TestNoRetryOnFailure(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
executor := s.InternalExecutor().(sqlutil.InternalExecutor)
cache := NewTableStatisticsCache(
ctx,
10, /* cacheSize */
kvDB,
executor,
keys.SystemSQLCodec,
s.ClusterSettings(),
s.RangeFeedFactory().(*rangefeed.Factory),
s.CollectionFactory().(*descs.CollectionFactory),
)
r := MakeRefresher(s.AmbientCtx(), st, executor, cache, time.Microsecond /* asOfTime */)
// Try to refresh stats on a table that doesn't exist.
r.maybeRefreshStats(
ctx, s.Stopper(), 100 /* tableID */, math.MaxInt32, time.Microsecond, /* asOfTime */
false, /* enabledForTable */
nil /* fractionStaleRowsForTable */, nil, /* minStaleRowsForTable */
)
// Ensure that we will not try to refresh tableID 100 again.
if expected, actual := 0, len(r.mutations); expected != actual {
t.Fatalf("expected channel size %d but found %d", expected, actual)
}
}
func TestMutationsChannel(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, true)
r := Refresher{
st: st,
mutations: make(chan mutation, refreshChanBufferLen),
}
tbl := descpb.TableDescriptor{ID: 53, ParentID: 52, Name: "foo"}
tableDesc := tabledesc.NewBuilder(&tbl).BuildImmutableTable()
// Test that the mutations channel doesn't block even when we add 10 more
// items than can fit in the buffer.
for i := 0; i < refreshChanBufferLen+10; i++ {
r.NotifyMutation(tableDesc, 5 /* rowsAffected */)
}
if expected, actual := refreshChanBufferLen, len(r.mutations); expected != actual {
t.Fatalf("expected channel size %d but found %d", expected, actual)
}
}
func TestDefaultColumns(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
s, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
st := cluster.MakeTestingClusterSettings()
AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false)
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)
sqlRun := sqlutils.MakeSQLRunner(sqlDB)
sqlRun.Exec(t,
`CREATE DATABASE t;
CREATE TABLE t.a (c0 INT PRIMARY KEY);`)
for i := 1; i < 110; i++ {
// Add more columns than we will collect stats on.
sqlRun.Exec(t,
fmt.Sprintf("ALTER TABLE t.a ADD COLUMN c%d INT", i))
}
sqlRun.Exec(t, `CREATE STATISTICS s FROM t.a`)
// There should be 101 stats. One for the primary index, plus 100 other
// columns.
sqlRun.CheckQueryResults(t,
`SELECT count(*) FROM [SHOW STATISTICS FOR TABLE t.a] WHERE statistics_name = 's'`,
[][]string{
{"101"},
})
}
func checkStatsCount(
ctx context.Context, cache *TableStatisticsCache, table catalog.TableDescriptor, expected int,
) error {
return testutils.SucceedsSoonError(func() error {
stats, err := cache.GetTableStats(ctx, table)
if err != nil {
return err
}
if len(stats) != expected {
return fmt.Errorf("expected %d stat(s) but found %d", expected, len(stats))
}
return nil
})
}