-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathhelpers_test.go
732 lines (657 loc) · 20.4 KB
/
helpers_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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// Copyright 2018 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package changefeedccl
import (
"bytes"
"context"
gosql "database/sql"
gojson "encoding/json"
"fmt"
"net/url"
"reflect"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/sql/distsqlrun"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/workload"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
)
type benchSink struct {
syncutil.Mutex
cond *sync.Cond
emits int
emitBytes int64
}
func makeBenchSink() *benchSink {
s := &benchSink{}
s.cond = sync.NewCond(&s.Mutex)
return s
}
func (s *benchSink) EmitRow(ctx context.Context, _ string, k, v []byte) error {
return s.emit(int64(len(k) + len(v)))
}
func (s *benchSink) EmitResolvedTimestamp(_ context.Context, p []byte) error {
return s.emit(int64(len(p)))
}
func (s *benchSink) Flush(_ context.Context) error { return nil }
func (s *benchSink) Close() error { return nil }
func (s *benchSink) emit(bytes int64) error {
s.Lock()
defer s.Unlock()
s.emits++
s.emitBytes += bytes
s.cond.Broadcast()
return nil
}
// WaitForEmit blocks until at least one thing is emitted by the sink. It
// returns the number of emitted messages and bytes since the last WaitForEmit.
func (s *benchSink) WaitForEmit() (int, int64) {
s.Lock()
defer s.Unlock()
for s.emits == 0 {
s.cond.Wait()
}
emits, emitBytes := s.emits, s.emitBytes
s.emits, s.emitBytes = 0, 0
return emits, emitBytes
}
// createBenchmarkChangefeed starts a stripped down changefeed. It watches
// `database.table` and outputs to `sinkURI`. The given `feedClock` is only used
// for the internal ExportRequest polling, so a benchmark can write data with
// different timestamps beforehand and simulate the changefeed going through
// them in steps.
//
// The returned sink can be used to count emits and the closure handed back
// cancels the changefeed (blocking until it's shut down) and returns an error
// if the changefeed had failed before the closure was called.
//
// This intentionally skips the distsql and sink parts to keep the benchmark
// focused on the core changefeed work, but it does include the poller.
func createBenchmarkChangefeed(
ctx context.Context,
s serverutils.TestServerInterface,
feedClock *hlc.Clock,
database, table string,
) (*benchSink, func() error) {
tableDesc := sqlbase.GetTableDescriptor(s.DB(), database, table)
spans := []roachpb.Span{tableDesc.PrimaryIndexSpan()}
details := jobspb.ChangefeedDetails{
Targets: jobspb.ChangefeedTargets{tableDesc.ID: jobspb.ChangefeedTarget{
StatementTimeName: tableDesc.Name,
}},
Opts: map[string]string{
optEnvelope: string(optEnvelopeRow),
},
}
initialHighWater := hlc.Timestamp{}
encoder := makeJSONEncoder(details.Opts)
sink := makeBenchSink()
buf := makeBuffer()
poller := makePoller(
s.ClusterSettings(), s.DB(), feedClock, s.Gossip(), spans, details, initialHighWater, buf)
th := makeTableHistory(func(*sqlbase.TableDescriptor) error { return nil }, initialHighWater)
thUpdater := &tableHistoryUpdater{
settings: s.ClusterSettings(),
db: s.DB(),
targets: details.Targets,
m: th,
}
rowsFn := kvsToRows(s.LeaseManager().(*sql.LeaseManager), th, details, buf.Get)
tickFn := emitEntries(details, encoder, sink, rowsFn, TestingKnobs{})
ctx, cancel := context.WithCancel(ctx)
go func() { _ = poller.Run(ctx) }()
go func() { _ = thUpdater.PollTableDescs(ctx) }()
errCh := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := func() error {
sf := makeSpanFrontier(spans...)
for {
// This is basically the ChangeAggregator processor.
resolvedSpans, err := tickFn(ctx)
if err != nil {
return err
}
// This is basically the ChangeFrontier processor, the resolved
// spans are normally sent using distsql, so we're missing a bit
// of overhead here.
for _, rs := range resolvedSpans {
if sf.Forward(rs.Span, rs.Timestamp) {
if err := emitResolvedTimestamp(
ctx, encoder, sink, sf.Frontier(),
); err != nil {
return err
}
}
}
}
}()
errCh <- err
}()
cancelFn := func() error {
select {
case err := <-errCh:
return err
default:
}
cancel()
wg.Wait()
return nil
}
return sink, cancelFn
}
// loadWorkloadBatches inserts a workload.Table's row batches, each in one
// transaction. It returns the timestamps of these transactions and the byte
// size for use with b.SetBytes.
func loadWorkloadBatches(sqlDB *gosql.DB, table workload.Table) ([]time.Time, int64, error) {
if _, err := sqlDB.Exec(`CREATE TABLE "` + table.Name + `" ` + table.Schema); err != nil {
return nil, 0, err
}
var now time.Time
var timestamps []time.Time
var benchBytes int64
var insertStmtBuf bytes.Buffer
var params []interface{}
for batchIdx := 0; batchIdx < table.InitialRows.NumBatches; batchIdx++ {
if _, err := sqlDB.Exec(`BEGIN`); err != nil {
return nil, 0, err
}
params = params[:0]
insertStmtBuf.Reset()
insertStmtBuf.WriteString(`INSERT INTO "` + table.Name + `" VALUES `)
for _, row := range table.InitialRows.Batch(batchIdx) {
if len(params) != 0 {
insertStmtBuf.WriteString(`,`)
}
insertStmtBuf.WriteString(`(`)
for colIdx, datum := range row {
if colIdx != 0 {
insertStmtBuf.WriteString(`,`)
}
benchBytes += workload.ApproxDatumSize(datum)
params = append(params, datum)
fmt.Fprintf(&insertStmtBuf, `$%d`, len(params))
}
insertStmtBuf.WriteString(`)`)
}
if _, err := sqlDB.Exec(insertStmtBuf.String(), params...); err != nil {
return nil, 0, err
}
if err := sqlDB.QueryRow(`SELECT transaction_timestamp(); COMMIT;`).Scan(&now); err != nil {
return nil, 0, err
}
timestamps = append(timestamps, now)
}
if table.InitialRows.NumTotal != 0 {
var totalRows int
if err := sqlDB.QueryRow(
`SELECT count(*) FROM "` + table.Name + `"`,
).Scan(&totalRows); err != nil {
return nil, 0, err
}
if table.InitialRows.NumTotal != totalRows {
return nil, 0, errors.Errorf(`sanity check failed: expected %d rows got %d`,
table.InitialRows.NumTotal, totalRows)
}
}
return timestamps, benchBytes, nil
}
type testfeedFactory interface {
Feed(t testing.TB, create string, args ...interface{}) testfeed
Server() serverutils.TestServerInterface
}
type testfeed interface {
Partitions() []string
Next(t testing.TB) (topic, partition string, key, value, payload []byte, ok bool)
Err() error
Close(t testing.TB)
}
type sinklessFeedFactory struct {
s serverutils.TestServerInterface
db *gosql.DB
}
func makeSinkless(s serverutils.TestServerInterface, db *gosql.DB) *sinklessFeedFactory {
return &sinklessFeedFactory{s: s, db: db}
}
func (f *sinklessFeedFactory) Feed(t testing.TB, create string, args ...interface{}) testfeed {
t.Helper()
if _, err := f.db.Exec(
`SET CLUSTER SETTING changefeed.experimental_poll_interval = '0ns'`,
); err != nil {
t.Fatal(err)
}
s := &sinklessFeed{db: f.db}
now := timeutil.Now()
var err error
s.rows, err = s.db.Query(create, args...)
if err != nil {
t.Fatal(err)
}
queryIDRows, err := s.db.Query(
`SELECT query_id FROM [SHOW QUERIES] WHERE query LIKE 'CREATE CHANGEFEED%' AND start > $1`,
now,
)
if err != nil {
t.Fatal(err)
}
if !queryIDRows.Next() {
t.Fatalf(`could not find query id`)
}
if err := queryIDRows.Scan(&s.queryID); err != nil {
t.Fatal(err)
}
if queryIDRows.Next() {
t.Fatalf(`found too many query ids`)
}
return s
}
func (f *sinklessFeedFactory) Server() serverutils.TestServerInterface {
return f.s
}
type sinklessFeed struct {
db *gosql.DB
rows *gosql.Rows
queryID string
}
func (c *sinklessFeed) Partitions() []string { return []string{`sinkless`} }
func (c *sinklessFeed) Next(
t testing.TB,
) (topic, partition string, key, value, resolved []byte, ok bool) {
t.Helper()
partition = `sinkless`
var noKey, noValue, noResolved []byte
if !c.rows.Next() {
return ``, ``, nil, nil, nil, false
}
var maybeTopic gosql.NullString
if err := c.rows.Scan(&maybeTopic, &key, &value); err != nil {
t.Fatal(err)
}
if maybeTopic.Valid {
return maybeTopic.String, partition, key, value, noResolved, true
}
resolvedPayload := value
return ``, partition, noKey, noValue, resolvedPayload, true
}
func (c *sinklessFeed) Err() error {
if c.rows != nil {
return c.rows.Err()
}
return nil
}
func (c *sinklessFeed) Close(t testing.TB) {
t.Helper()
// TODO(dan): We should just be able to close the `gosql.Rows` but that
// currently blocks forever without this.
if _, err := c.db.Exec(`CANCEL QUERY IF EXISTS $1`, c.queryID); err != nil {
t.Error(err)
}
// Ignore the error because we just force canceled the feed.
_ = c.rows.Close()
}
type tableFeedFactory struct {
s serverutils.TestServerInterface
db *gosql.DB
flushCh chan struct{}
}
func makeTable(
s serverutils.TestServerInterface, db *gosql.DB, flushCh chan struct{},
) *tableFeedFactory {
return &tableFeedFactory{s: s, db: db, flushCh: flushCh}
}
func (f *tableFeedFactory) Feed(t testing.TB, create string, args ...interface{}) testfeed {
t.Helper()
sink, cleanup := sqlutils.PGUrl(t, f.s.ServingAddr(), t.Name(), url.User(security.RootUser))
sink.Path = fmt.Sprintf(`table_%d`, timeutil.Now().UnixNano())
db, err := gosql.Open("postgres", sink.String())
if err != nil {
t.Fatal(err)
}
sink.Scheme = sinkSchemeExperimentalSQL
c := &tableFeed{db: db, urlCleanup: cleanup, sinkURI: sink.String(), flushCh: f.flushCh}
if _, err := c.db.Exec(
`SET CLUSTER SETTING changefeed.experimental_poll_interval = '0ns'`,
); err != nil {
t.Fatal(err)
}
if _, err := c.db.Exec(`CREATE DATABASE ` + sink.Path); err != nil {
t.Fatal(err)
}
parsed, err := parser.ParseOne(create)
if err != nil {
t.Fatal(err)
}
createStmt := parsed.(*tree.CreateChangefeed)
if createStmt.SinkURI != nil {
t.Fatalf(`unexpected sink provided: "INTO %s"`, tree.AsString(createStmt.SinkURI))
}
createStmt.SinkURI = tree.NewStrVal(c.sinkURI)
if err := f.db.QueryRow(createStmt.String(), args...).Scan(&c.jobID); err != nil {
t.Fatal(err)
}
return c
}
func (f *tableFeedFactory) Server() serverutils.TestServerInterface {
return f.s
}
type tableFeed struct {
db *gosql.DB
sinkURI string
urlCleanup func()
jobID int64
flushCh chan struct{}
rows *gosql.Rows
jobErr error
}
func (c *tableFeed) Partitions() []string {
// The sqlSink hardcodes these.
return []string{`0`, `1`, `2`}
}
func (c *tableFeed) Next(
t testing.TB,
) (topic, partition string, key, value, payload []byte, ok bool) {
// sinkSink writes all changes to a table with primary key of topic,
// partition, message_id. To simulate the semantics of kafka, message_ids
// are only comparable within a given (topic, partition). Internally the
// message ids are generated as a 64 bit int with a timestamp in bits 1-49
// and a hash of the partition in 50-64. This tableFeed.Next function works
// by repeatedly fetching and deleting all rows in the table. Then it pages
// through the results until they are empty and repeats.
//
// To avoid busy waiting, we wait for the AfterFlushHook (which is called
// after results are flushed to a sink) in between polls. It is required
// that this is hooked up to `flushCh`, which is usually handled by the
// `enterpriseTest` helper.
//
// The trickiest bit is handling errors in the changefeed. The tests want to
// eventually notice them, but want to return all generated results before
// giving up and returning the error. This is accomplished by checking the
// job error immediately before every poll. If it's set, the error is
// stashed and one more poll's result set is paged through, before finally
// returning the error. If we're careful to run the last poll after getting
// the error, then it's guaranteed to contain everything flushed by the
// changefeed before it shut down.
for {
if c.rows != nil && c.rows.Next() {
var msgID int64
if err := c.rows.Scan(&topic, &partition, &msgID, &key, &value, &payload); err != nil {
t.Fatal(err)
}
// Scan turns NULL bytes columns into a 0-length, non-nil byte
// array, which is pretty unexpected. Nil them out before returning.
// Either key+value or payload will be set, but not both.
if len(key) > 0 {
payload = nil
} else {
key, value = nil, nil
}
return topic, partition, key, value, payload, true
}
if c.rows != nil {
if err := c.rows.Close(); err != nil {
t.Fatal(err)
}
c.rows = nil
}
if c.jobErr != nil {
return ``, ``, nil, nil, nil, false
}
// We're not guaranteed to get a flush notification if the feed exits,
// so bound how long we wait.
select {
case <-c.flushCh:
case <-time.After(30 * time.Millisecond):
}
// If the error was set, save it, but do one more poll as described
// above.
var errorStr gosql.NullString
if err := c.db.QueryRow(
`SELECT error FROM [SHOW JOBS] WHERE job_id=$1`, c.jobID,
).Scan(&errorStr); err != nil {
t.Fatal(err)
}
if len(errorStr.String) > 0 {
c.jobErr = errors.New(errorStr.String)
}
// TODO(dan): It's a bummer that this mutates the sqlsink table. I
// originally tried paging through message_id by repeatedly generating a
// new high-water with GenerateUniqueInt, but this was racy with rows
// being flushed out by the sink. An alternative is to steal the nanos
// part from `high_water_timestamp` in `crdb_internal.jobs` and run it
// through `builtins.GenerateUniqueID`, but that would mean we're only
// ever running tests on rows that have gotten a resolved timestamp,
// which seems limiting.
var err error
c.rows, err = c.db.Query(
`DELETE FROM sqlsink ORDER BY PRIMARY KEY sqlsink RETURNING *`)
if err != nil {
t.Fatal(err)
}
}
}
func (c *tableFeed) Err() error {
return c.jobErr
}
func (c *tableFeed) Close(t testing.TB) {
if c.rows != nil {
if err := c.rows.Close(); err != nil {
t.Errorf(`could not close rows: %v`, err)
}
}
if _, err := c.db.Exec(`CANCEL JOB $1`, c.jobID); err != nil {
log.Infof(context.Background(), `could not cancel feed %d: %v`, c.jobID, err)
}
if err := c.db.Close(); err != nil {
t.Error(err)
}
c.urlCleanup()
}
func waitForSchemaChange(
t testing.TB, sqlDB *sqlutils.SQLRunner, stmt string, arguments ...interface{},
) {
sqlDB.Exec(t, stmt, arguments...)
row := sqlDB.QueryRow(t, "SELECT job_id FROM [SHOW JOBS] ORDER BY created DESC LIMIT 1")
var jobID string
row.Scan(&jobID)
testutils.SucceedsSoon(t, func() error {
row := sqlDB.QueryRow(t, "SELECT status FROM [SHOW JOBS] WHERE job_id = $1", jobID)
var status string
row.Scan(&status)
if status != "succeeded" {
return fmt.Errorf("Job %s had status %s, wanted 'succeeded'", jobID, status)
}
return nil
})
}
func assertPayloads(t testing.TB, f testfeed, expected []string) {
t.Helper()
var actual []string
for len(actual) < len(expected) {
topic, _, key, value, _, ok := f.Next(t)
if !ok {
break
} else if key != nil {
actual = append(actual, fmt.Sprintf(`%s: %s->%s`, topic, key, value))
}
}
// The tests that use this aren't concerned with order, just that these are
// the next len(expected) messages.
sort.Strings(expected)
sort.Strings(actual)
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected\n %s\ngot\n %s",
strings.Join(expected, "\n "), strings.Join(actual, "\n "))
}
}
func assertPayloadsAvro(t testing.TB, reg *testSchemaRegistry, f testfeed, expected []string) {
t.Helper()
var actual []string
for len(actual) < len(expected) {
topic, _, keyBytes, valueBytes, _, ok := f.Next(t)
if !ok {
break
} else if keyBytes != nil {
key, err := reg.encodedAvroToJSON(keyBytes)
if err != nil {
t.Fatal(err)
}
value, err := reg.encodedAvroToJSON(valueBytes)
if err != nil {
t.Fatal(err)
}
actual = append(actual, fmt.Sprintf(`%s: %s->%s`, topic, key, value))
}
}
// The tests that use this aren't concerned with order, just that these are
// the next len(expected) messages.
sort.Strings(expected)
sort.Strings(actual)
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected\n %s\ngot\n %s",
strings.Join(expected, "\n "), strings.Join(actual, "\n "))
}
}
func skipResolvedTimestamps(t *testing.T, f testfeed) {
for {
table, _, key, value, _, ok := f.Next(t)
if !ok {
break
}
if key != nil {
t.Errorf(`unexpected row %s: %s->%s`, table, key, value)
}
}
}
func parseTimeToHLC(t testing.TB, s string) hlc.Timestamp {
t.Helper()
d, _, err := apd.NewFromString(s)
if err != nil {
t.Fatal(err)
}
ts, err := tree.DecimalToHLC(d)
if err != nil {
t.Fatal(err)
}
return ts
}
func expectResolvedTimestamp(t testing.TB, f testfeed) hlc.Timestamp {
t.Helper()
topic, _, key, value, resolved, _ := f.Next(t)
if key != nil {
t.Fatalf(`unexpected row %s: %s -> %s`, topic, key, value)
}
if resolved == nil {
t.Fatal(`expected a resolved timestamp notification`)
}
var valueRaw struct {
CRDB struct {
Resolved string `json:"resolved"`
} `json:"__crdb__"`
}
if err := gojson.Unmarshal(resolved, &valueRaw); err != nil {
t.Fatal(err)
}
return parseTimeToHLC(t, valueRaw.CRDB.Resolved)
}
func sinklessTest(testFn func(*testing.T, *gosql.DB, testfeedFactory)) func(*testing.T) {
return func(t *testing.T) {
ctx := context.Background()
knobs := base.TestingKnobs{DistSQL: &distsqlrun.TestingKnobs{Changefeed: &TestingKnobs{}}}
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{
UseDatabase: "d",
Knobs: knobs,
// TODO(dan): HACK until the changefeed can control pgwire flushing.
ConnResultsBufferBytes: 1,
})
defer s.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(db)
sqlDB.Exec(t, `SET CLUSTER SETTING changefeed.experimental_poll_interval = '0ns'`)
sqlDB.Exec(t, `CREATE DATABASE d`)
f := makeSinkless(s, db)
testFn(t, db, f)
}
}
func enterpriseTest(testFn func(*testing.T, *gosql.DB, testfeedFactory)) func(*testing.T) {
return func(t *testing.T) {
ctx := context.Background()
flushCh := make(chan struct{}, 1)
defer close(flushCh)
knobs := base.TestingKnobs{DistSQL: &distsqlrun.TestingKnobs{Changefeed: &TestingKnobs{
AfterSinkFlush: func() error {
select {
case flushCh <- struct{}{}:
default:
}
return nil
},
}}}
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{
UseDatabase: "d",
Knobs: knobs,
})
defer s.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(db)
sqlDB.Exec(t, `SET CLUSTER SETTING changefeed.experimental_poll_interval = '0ns'`)
sqlDB.Exec(t, `CREATE DATABASE d`)
f := makeTable(s, db, flushCh)
testFn(t, db, f)
}
}
func forceTableGC(
t testing.TB,
tsi serverutils.TestServerInterface,
sqlDB *sqlutils.SQLRunner,
database, table string,
) {
var stmt string
if database == "system" {
stmt = `ALTER DATABASE system CONFIGURE ZONE USING gc.ttlseconds = $1`
} else {
fmt.Sprintf(`ALTER TABLE %s.%s CONFIGURE ZONE USING gc.ttlseconds = $1`, database, table)
}
sqlDB.Exec(t, stmt, 1)
tblID, err := sqlutils.QueryTableID(sqlDB.DB, database, table)
if err != nil {
t.Fatal(err)
}
tablePrefix := keys.MakeTablePrefix(tblID)
tableStartKey := roachpb.RKey(tablePrefix)
tableSpan := roachpb.RSpan{
Key: tableStartKey,
EndKey: tableStartKey.PrefixEnd(),
}
ts := tsi.(*server.TestServer)
if err := ts.GetStores().(*storage.Stores).VisitStores(func(st *storage.Store) error {
return st.ManuallyGCSpan(context.Background(), tableSpan)
}); err != nil {
t.Fatal(err)
}
}