-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
ttljob_test.go
685 lines (624 loc) · 20.6 KB
/
ttljob_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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Copyright 2022 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 ttljob_test
import (
"bytes"
"context"
"fmt"
"regexp"
"strings"
"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/jobs/jobstest"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/scheduledjobs"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"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/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
type ttlServer interface {
JobRegistry() interface{}
}
type rowLevelTTLTestJobTestHelper struct {
server ttlServer
env *jobstest.JobSchedulerTestEnv
cfg *scheduledjobs.JobExecutionConfig
sqlDB *sqlutils.SQLRunner
kvDB *kv.DB
executeSchedules func() error
}
func newRowLevelTTLTestJobTestHelper(
t *testing.T, testingKnobs *sql.TTLTestingKnobs, testMultiTenant bool, numNodes int,
) (*rowLevelTTLTestJobTestHelper, func()) {
th := &rowLevelTTLTestJobTestHelper{
env: jobstest.NewJobSchedulerTestEnv(
jobstest.UseSystemTables,
timeutil.Now(),
tree.ScheduledRowLevelTTLExecutor,
),
}
baseTestingKnobs := base.TestingKnobs{
JobsTestingKnobs: &jobs.TestingKnobs{
JobSchedulerEnv: th.env,
TakeOverJobsScheduling: func(fn func(ctx context.Context, maxSchedules int64) error) {
th.executeSchedules = func() error {
defer th.server.JobRegistry().(*jobs.Registry).TestingNudgeAdoptionQueue()
return fn(context.Background(), 0 /* allSchedules */)
}
},
CaptureJobExecutionConfig: func(config *scheduledjobs.JobExecutionConfig) {
th.cfg = config
},
},
TTL: testingKnobs,
}
// As `ALTER TABLE ... SPLIT AT ...` is not supported in multi-tenancy, we
// do not run those tests.
tc := serverutils.StartNewTestCluster(t, numNodes, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: baseTestingKnobs,
DisableWebSessionAuthentication: true,
},
})
ts := tc.Server(0)
if testMultiTenant {
tenantServer, db := serverutils.StartTenant(
t, ts, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
TestingKnobs: baseTestingKnobs,
},
)
th.sqlDB = sqlutils.MakeSQLRunner(db)
th.server = tenantServer
} else {
db := serverutils.OpenDBConn(
t,
ts.ServingSQLAddr(),
"", /* useDatabase */
false, /* insecure */
ts.Stopper(),
)
th.sqlDB = sqlutils.MakeSQLRunner(db)
th.server = ts
}
require.NotNil(t, th.cfg)
th.kvDB = ts.DB()
return th, func() {
tc.Stopper().Stop(context.Background())
}
}
func (h *rowLevelTTLTestJobTestHelper) waitForScheduledJob(
t *testing.T, expectedStatus jobs.Status, expectedErrorRe string,
) {
query := fmt.Sprintf(
`SELECT status, error FROM [SHOW JOBS]
WHERE job_id IN (
SELECT id FROM %s
WHERE created_by_id IN (SELECT schedule_id FROM %s WHERE executor_type = 'scheduled-row-level-ttl-executor')
)`,
h.env.SystemJobsTableName(),
h.env.ScheduledJobsTableName(),
)
testutils.SucceedsSoon(t, func() error {
// Force newly created job to be adopted and verify it succeeds.
h.server.JobRegistry().(*jobs.Registry).TestingNudgeAdoptionQueue()
var status, errorStr string
if err := h.sqlDB.DB.QueryRowContext(
context.Background(),
query,
).Scan(&status, &errorStr); err != nil {
return errors.Wrapf(err, "expected to scan row for a job, got")
}
if status != string(expectedStatus) {
return errors.Newf("expected status %s, got %s (error: %s)", expectedStatus, status, errorStr)
}
if expectedErrorRe != "" {
r, err := regexp.Compile(expectedErrorRe)
require.NoError(t, err)
if !r.MatchString(errorStr) {
return errors.Newf("expected error matches %s, got %s", expectedErrorRe, errorStr)
}
}
return nil
})
}
func (h *rowLevelTTLTestJobTestHelper) waitForSuccessfulScheduledJob(t *testing.T) {
h.waitForScheduledJob(t, jobs.StatusSucceeded, "")
}
func TestRowLevelTTLNoTestingKnobs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
th, cleanupFunc := newRowLevelTTLTestJobTestHelper(
t,
nil, /* SQLTestingKnobs */
true, /* testMultiTenant */
1, /* numNodes */
)
defer cleanupFunc()
th.sqlDB.Exec(t, `CREATE TABLE t (id INT PRIMARY KEY) WITH (ttl_expire_after = '1 minute')`)
th.sqlDB.Exec(t, `INSERT INTO t (id, crdb_internal_expiration) VALUES (1, now() - '1 month')`)
// Force the schedule to execute.
th.env.SetTime(timeutil.Now().Add(time.Hour * 24))
require.NoError(t, th.executeSchedules())
th.waitForScheduledJob(t, jobs.StatusFailed, `found a recent schema change on the table`)
}
// TestRowLevelTTLInterruptDuringExecution tests that row-level TTL errors
// as appropriate if there is some sort of "interrupting" request.
func TestRowLevelTTLInterruptDuringExecution(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
createTable := `CREATE TABLE t (
id INT PRIMARY KEY
) WITH (ttl_expire_after = '10 minutes', ttl_range_concurrency = 2);
ALTER TABLE t SPLIT AT VALUES (1), (2);
INSERT INTO t (id, crdb_internal_expiration) VALUES (1, now() - '1 month'), (2, now() - '1 month');`
testCases := []struct {
desc string
expectedTTLError string
aostDuration time.Duration
preDeleteChangeTableVersion bool
preSelectStatement string
}{
{
desc: "schema change too recent to start TTL job",
expectedTTLError: "found a recent schema change on the table at .*, aborting",
aostDuration: -48 * time.Hour,
},
{
desc: "schema change during job",
expectedTTLError: "error during row deletion: table has had a schema change since the job has started at .*, aborting",
aostDuration: time.Duration(0),
// We cannot use a schema change to change the version in this test as
// we overtook the job adoption method, which means schema changes get
// blocked and may not run.
preDeleteChangeTableVersion: true,
},
{
desc: "disable cluster setting",
expectedTTLError: `ttl jobs are currently disabled by CLUSTER SETTING sql.ttl.job.enabled`,
preSelectStatement: `SET CLUSTER SETTING sql.ttl.job.enabled = false`,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
th, cleanupFunc := newRowLevelTTLTestJobTestHelper(
t,
&sql.TTLTestingKnobs{
AOSTDuration: &tc.aostDuration,
PreDeleteChangeTableVersion: tc.preDeleteChangeTableVersion,
PreSelectStatement: tc.preSelectStatement,
},
false, /* testMultiTenant */
1, /* numNodes */
)
defer cleanupFunc()
th.sqlDB.Exec(t, createTable)
// Force the schedule to execute.
th.env.SetTime(timeutil.Now().Add(time.Hour * 24))
require.NoError(t, th.executeSchedules())
th.waitForScheduledJob(t, jobs.StatusFailed, tc.expectedTTLError)
})
}
}
func TestRowLevelTTLJobDisabled(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
createTable := func(addPause bool) string {
var pauseStr string
if addPause {
pauseStr = `, ttl_pause = true`
}
return fmt.Sprintf(`CREATE TABLE t (
id INT PRIMARY KEY
) WITH (ttl_expire_after = '10 minutes', ttl_range_concurrency = 2%s);
INSERT INTO t (id, crdb_internal_expiration) VALUES (1, now() - '1 month'), (2, now() - '1 month');`, pauseStr)
}
testCases := []struct {
desc string
expectedTTLError string
setup string
}{
{
desc: "disabled by cluster setting",
expectedTTLError: "ttl jobs are currently disabled by CLUSTER SETTING sql.ttl.job.enabled",
setup: createTable(false) + `SET CLUSTER SETTING sql.ttl.job.enabled = false`,
},
{
desc: "disabled by TTL pause",
expectedTTLError: "ttl jobs on table t are currently paused",
setup: createTable(true),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var zeroDuration time.Duration
th, cleanupFunc := newRowLevelTTLTestJobTestHelper(
t,
&sql.TTLTestingKnobs{
AOSTDuration: &zeroDuration,
},
true, /* testMultiTenant */
1 /* numNodes */)
defer cleanupFunc()
th.sqlDB.ExecMultiple(t, strings.Split(tc.setup, ";")...)
// Force the schedule to execute.
th.env.SetTime(timeutil.Now().Add(time.Hour * 24))
require.NoError(t, th.executeSchedules())
th.waitForScheduledJob(t, jobs.StatusFailed, tc.expectedTTLError)
var numRows int
th.sqlDB.QueryRow(t, `SELECT count(1) FROM t`).Scan(&numRows)
require.Equal(t, 2, numRows)
})
}
}
// TestRowLevelTTLJobRandomEntries inserts random entries into a given table
// and runs a TTL job on them.
func TestRowLevelTTLJobRandomEntries(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
rng, _ := randutil.NewTestRand()
var indexableTyps []*types.T
for _, typ := range types.Scalar {
// TODO(#76419): DateFamily has a broken `-infinity` case.
if colinfo.ColumnTypeIsIndexable(typ) && typ.Family() != types.DateFamily {
indexableTyps = append(indexableTyps, typ)
}
}
type testCase struct {
desc string
createTable string
preSetup []string
postSetup []string
numExpiredRows int
numNonExpiredRows int
numSplits int
forceNonMultiTenant bool
numNodes int
expirationExpression string
addRow func(th *rowLevelTTLTestJobTestHelper, createTableStmt *tree.CreateTable, ts time.Time)
}
// Add some basic one and three column row-level TTL tests.
testCases := []testCase{
{
desc: "one column pk",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days')`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
},
{
desc: "one column pk multiple nodes",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days')`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
numNodes: 5,
numSplits: 10,
},
{
desc: "one column pk, table ranges overlap",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days')`,
preSetup: []string{
`CREATE TABLE tbm (id INT PRIMARY KEY)`,
`ALTER TABLE tbm SPLIT AT VALUES (1)`,
},
postSetup: []string{
`CREATE TABLE tbl2 (id INT PRIMARY KEY)`,
`ALTER TABLE tbl2 SPLIT AT VALUES (1)`,
},
numExpiredRows: 1001,
numNonExpiredRows: 5,
forceNonMultiTenant: true,
},
{
desc: "one column pk with statistics",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days', ttl_row_stats_poll_interval = '1 minute')`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
},
{
desc: "one column pk with child labels & statistics",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days', ttl_row_stats_poll_interval = '1 minute', ttl_label_metrics = true)`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
},
{
desc: "one column pk, concurrentSchemaChange",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
text TEXT
) WITH (ttl_expire_after = '30 days', ttl_select_batch_size = 50, ttl_delete_batch_size = 10, ttl_range_concurrency = 3)`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
numSplits: 10,
},
{
desc: "three column pk",
createTable: `CREATE TABLE tbl (
id UUID DEFAULT gen_random_uuid(),
other_col INT,
"quote-kw-col" TIMESTAMPTZ,
text TEXT,
PRIMARY KEY (id, other_col, "quote-kw-col")
) WITH (ttl_expire_after = '30 days')`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
},
{
desc: "three column pk with rate limit",
createTable: `CREATE TABLE tbl (
id UUID DEFAULT gen_random_uuid(),
other_col INT,
"quote-kw-col" TIMESTAMPTZ,
text TEXT,
PRIMARY KEY (id, other_col, "quote-kw-col")
) WITH (ttl_expire_after = '30 days', ttl_delete_rate_limit = 350)`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
},
{
desc: "three column pk, concurrentSchemaChange",
createTable: `CREATE TABLE tbl (
id UUID DEFAULT gen_random_uuid(),
other_col INT,
"quote-kw-col" TIMESTAMPTZ,
text TEXT,
PRIMARY KEY (id, other_col, "quote-kw-col")
) WITH (ttl_expire_after = '30 days', ttl_select_batch_size = 50, ttl_delete_batch_size = 10, ttl_range_concurrency = 3)`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
numSplits: 10,
},
{
desc: "three column pk, concurrentSchemaChange with index",
createTable: `CREATE TABLE tbl (
id UUID DEFAULT gen_random_uuid(),
other_col INT,
"quote-kw-col" TIMESTAMPTZ,
text TEXT,
INDEX text_idx (text),
PRIMARY KEY (id, other_col, "quote-kw-col")
) WITH (ttl_expire_after = '30 days', ttl_select_batch_size = 50, ttl_delete_batch_size = 10, ttl_range_concurrency = 3)`,
postSetup: []string{
`ALTER INDEX tbl@text_idx SPLIT AT VALUES ('bob')`,
},
numExpiredRows: 1001,
numNonExpiredRows: 5,
numSplits: 10,
},
{
desc: "ttl expiration expression",
createTable: `CREATE TABLE tbl (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
expire_at TIMESTAMP
) WITH (ttl_expiration_expression = 'expire_at')`,
numExpiredRows: 1001,
numNonExpiredRows: 5,
expirationExpression: "expire_at",
addRow: func(th *rowLevelTTLTestJobTestHelper, createTableStmt *tree.CreateTable, ts time.Time) {
th.sqlDB.Exec(
t,
"INSERT INTO tbl (expire_at) VALUES ($1)",
ts,
)
},
},
}
// Also randomly generate random PKs.
for i := 0; i < 5; i++ {
testCases = append(
testCases,
testCase{
desc: fmt.Sprintf("random %d", i+1),
createTable: fmt.Sprintf(
`CREATE TABLE tbl (
id UUID DEFAULT gen_random_uuid(),
rand_col_1 %s,
rand_col_2 %s,
text TEXT,
PRIMARY KEY (id, rand_col_1, rand_col_2)
) WITH (ttl_expire_after = '30 days', ttl_select_batch_size = %d, ttl_delete_batch_size = %d, ttl_range_concurrency = %d)`,
randgen.RandTypeFromSlice(rng, indexableTyps).SQLString(),
randgen.RandTypeFromSlice(rng, indexableTyps).SQLString(),
1+rng.Intn(100),
1+rng.Intn(100),
1+rng.Intn(3),
),
numSplits: 1 + rng.Intn(9),
numExpiredRows: rng.Intn(2000),
numNonExpiredRows: rng.Intn(100),
},
)
}
defaultAddRow := func(th *rowLevelTTLTestJobTestHelper, createTableStmt *tree.CreateTable, ts time.Time) {
insertColumns := []string{"crdb_internal_expiration"}
placeholders := []string{"$1"}
values := []interface{}{ts}
for _, def := range createTableStmt.Defs {
if def, ok := def.(*tree.ColumnTableDef); ok {
if def.HasDefaultExpr() {
continue
}
placeholders = append(placeholders, fmt.Sprintf("$%d", len(placeholders)+1))
var b bytes.Buffer
lexbase.EncodeRestrictedSQLIdent(&b, string(def.Name), lexbase.EncNoFlags)
insertColumns = append(insertColumns, b.String())
d := randgen.RandDatum(rng, def.Type.(*types.T), false /* nullOk */)
f := tree.NewFmtCtx(tree.FmtBareStrings)
d.Format(f)
values = append(values, f.CloseAndGetString())
}
}
th.sqlDB.Exec(
t,
fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
createTableStmt.Table.Table(),
strings.Join(insertColumns, ","),
strings.Join(placeholders, ","),
),
values...,
)
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
// Log to make it slightly easier to reproduce a random config.
t.Logf("test case: %#v", tc)
var zeroDuration time.Duration
numNodes := tc.numNodes
if numNodes == 0 {
numNodes = 1
}
th, cleanupFunc := newRowLevelTTLTestJobTestHelper(
t,
&sql.TTLTestingKnobs{
AOSTDuration: &zeroDuration,
ReturnStatsError: true,
RequireMultipleSpanPartitions: numNodes > 0,
},
tc.numSplits == 0 && !tc.forceNonMultiTenant, // SPLIT AT does not work with multi-tenant
numNodes, /* numNodes */
)
defer cleanupFunc()
for _, stmt := range tc.preSetup {
t.Logf("running pre statement: %s", stmt)
th.sqlDB.Exec(t, stmt)
}
th.sqlDB.Exec(t, tc.createTable)
// Extract the columns from CREATE TABLE.
stmt, err := parser.ParseOne(tc.createTable)
require.NoError(t, err)
createTableStmt, ok := stmt.AST.(*tree.CreateTable)
require.True(t, ok)
// Split the ranges by a random PK value.
if tc.numSplits > 0 {
tbDesc := desctestutils.TestingGetPublicTableDescriptor(
th.kvDB,
keys.SystemSQLCodec,
"defaultdb",
createTableStmt.Table.Table(),
)
require.NotNil(t, tbDesc)
for i := 0; i < tc.numSplits; i++ {
var values []interface{}
var placeholders []string
// Note we can split a PRIMARY KEY partially.
numKeyCols := 1 + rng.Intn(tbDesc.GetPrimaryIndex().NumKeyColumns())
for idx := 0; idx < numKeyCols; idx++ {
col, err := tbDesc.FindColumnWithID(tbDesc.GetPrimaryIndex().GetKeyColumnID(idx))
require.NoError(t, err)
placeholders = append(placeholders, fmt.Sprintf("$%d", idx+1))
d := randgen.RandDatum(rng, col.GetType(), false)
f := tree.NewFmtCtx(tree.FmtBareStrings)
d.Format(f)
values = append(values, f.CloseAndGetString())
}
th.sqlDB.Exec(
t,
fmt.Sprintf(
"ALTER TABLE %s SPLIT AT VALUES (%s)",
createTableStmt.Table.Table(),
strings.Join(placeholders, ","),
),
values...,
)
}
}
addRow := defaultAddRow
if tc.addRow != nil {
addRow = tc.addRow
}
// Add expired and non-expired rows.
for i := 0; i < tc.numExpiredRows; i++ {
addRow(th, createTableStmt, timeutil.Now().Add(-time.Hour))
}
for i := 0; i < tc.numNonExpiredRows; i++ {
addRow(th, createTableStmt, timeutil.Now().Add(time.Hour*24*30))
}
for _, stmt := range tc.postSetup {
t.Logf("running post statement: %s", stmt)
th.sqlDB.Exec(t, stmt)
}
// Force the schedule to execute.
th.env.SetTime(timeutil.Now().Add(time.Hour * 24))
require.NoError(t, th.executeSchedules())
th.waitForSuccessfulScheduledJob(t)
table := createTableStmt.Table.Table()
// Check we have the number of expected rows.
var numRows int
th.sqlDB.QueryRow(
t,
fmt.Sprintf(`SELECT count(1) FROM %s`, table),
).Scan(&numRows)
require.Equal(t, tc.numNonExpiredRows, numRows)
// Also check all the rows expire way into the future.
expirationExpression := "crdb_internal_expiration"
if tc.expirationExpression != "" {
expirationExpression = tc.expirationExpression
}
th.sqlDB.QueryRow(
t,
fmt.Sprintf(`SELECT count(1) FROM %s WHERE %s >= now()`, table, expirationExpression),
).Scan(&numRows)
require.Equal(t, tc.numNonExpiredRows, numRows)
rows := th.sqlDB.Query(t, `
SELECT sys_j.status, sys_j.progress
FROM crdb_internal.jobs AS crdb_j
JOIN system.jobs as sys_j ON crdb_j.job_id = sys_j.id
WHERE crdb_j.job_type = 'ROW LEVEL TTL'
`)
jobCount := 0
for rows.Next() {
var status string
var progressBytes []byte
require.NoError(t, rows.Scan(&status, &progressBytes))
require.Equal(t, "succeeded", status)
var progress jobspb.Progress
require.NoError(t, protoutil.Unmarshal(progressBytes, &progress))
rowLevelTTLProgress := progress.UnwrapDetails().(jobspb.RowLevelTTLProgress)
require.Equal(t, int64(tc.numExpiredRows), rowLevelTTLProgress.RowCount)
jobCount++
}
require.Equal(t, 1, jobCount)
})
}
}