-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dist_sender_rangefeed_test.go
1045 lines (906 loc) · 33.5 KB
/
dist_sender_rangefeed_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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 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 kvcoord_test
import (
"context"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/span"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type wrapRangeFeedClientFn func(client kvpb.Internal_RangeFeedClient) kvpb.Internal_RangeFeedClient
type testRangefeedClient struct {
rpc.RestrictedInternalClient
muxRangeFeedEnabled bool
count func()
wrapRangeFeedClient wrapRangeFeedClientFn
}
func (c *testRangefeedClient) RangeFeed(
ctx context.Context, args *kvpb.RangeFeedRequest, opts ...grpc.CallOption,
) (kvpb.Internal_RangeFeedClient, error) {
defer c.count()
if c.muxRangeFeedEnabled && ctx.Value(useMuxRangeFeedCtxKey{}) != nil {
panic(errors.AssertionFailedf("unexpected call to RangeFeed"))
}
rfClient, err := c.RestrictedInternalClient.RangeFeed(ctx, args, opts...)
if err != nil {
return nil, err
}
if c.wrapRangeFeedClient == nil {
return rfClient, nil
}
return c.wrapRangeFeedClient(rfClient), nil
}
func (c *testRangefeedClient) MuxRangeFeed(
ctx context.Context, opts ...grpc.CallOption,
) (kvpb.Internal_MuxRangeFeedClient, error) {
defer c.count()
if !c.muxRangeFeedEnabled || ctx.Value(useMuxRangeFeedCtxKey{}) == nil {
panic(errors.AssertionFailedf("unexpected call to MuxRangeFeed"))
}
return c.RestrictedInternalClient.MuxRangeFeed(ctx, opts...)
}
type internalClientCounts struct {
syncutil.Mutex
counts map[rpc.RestrictedInternalClient]int
}
func (c *internalClientCounts) Inc(ic rpc.RestrictedInternalClient) {
if c == nil {
return
}
c.Lock()
defer c.Unlock()
c.counts[ic]++
}
type countConnectionsTransport struct {
wrapped kvcoord.Transport
counts *internalClientCounts
wrapRangeFeedClient wrapRangeFeedClientFn
rfStreamEnabled bool
}
var _ kvcoord.Transport = (*countConnectionsTransport)(nil)
func (c *countConnectionsTransport) IsExhausted() bool {
return c.wrapped.IsExhausted()
}
func (c *countConnectionsTransport) SendNext(
ctx context.Context, request *kvpb.BatchRequest,
) (*kvpb.BatchResponse, error) {
return c.wrapped.SendNext(ctx, request)
}
type testFeedCtxKey struct{}
type useMuxRangeFeedCtxKey struct{}
func (c *countConnectionsTransport) NextInternalClient(
ctx context.Context,
) (rpc.RestrictedInternalClient, error) {
client, err := c.wrapped.NextInternalClient(ctx)
if err != nil {
return nil, err
}
// Use regular client if we're not running this tests rangefeed.
if ctx.Value(testFeedCtxKey{}) == nil {
return client, nil
}
tc := &testRangefeedClient{
RestrictedInternalClient: client,
muxRangeFeedEnabled: c.rfStreamEnabled,
wrapRangeFeedClient: c.wrapRangeFeedClient,
}
tc.count = func() {
if c.counts != nil {
c.counts.Inc(tc)
}
}
return tc, nil
}
func (c *countConnectionsTransport) NextReplica() roachpb.ReplicaDescriptor {
return c.wrapped.NextReplica()
}
func (c *countConnectionsTransport) SkipReplica() {
c.wrapped.SkipReplica()
}
func (c *countConnectionsTransport) MoveToFront(descriptor roachpb.ReplicaDescriptor) bool {
return c.wrapped.MoveToFront(descriptor)
}
func (c *countConnectionsTransport) Release() {
c.wrapped.Release()
}
func makeTransportFactory(
rfStreamEnabled bool, counts *internalClientCounts, wrapFn wrapRangeFeedClientFn,
) kvcoord.TransportFactory {
return func(
options kvcoord.SendOptions,
dialer *nodedialer.Dialer,
slice kvcoord.ReplicaSlice,
) (kvcoord.Transport, error) {
transport, err := kvcoord.GRPCTransportFactory(options, dialer, slice)
if err != nil {
return nil, err
}
countingTransport := &countConnectionsTransport{
wrapped: transport,
rfStreamEnabled: rfStreamEnabled,
counts: counts,
wrapRangeFeedClient: wrapFn,
}
return countingTransport, nil
}
}
// rangeFeed is a helper to execute rangefeed. We are not using rangefeed library
// here because of circular dependencies.
func rangeFeed(
dsI interface{},
sp roachpb.Span,
startFrom hlc.Timestamp,
onValue func(event kvcoord.RangeFeedMessage),
useMuxRangeFeed bool,
opts ...kvcoord.RangeFeedOption,
) func() {
ds := dsI.(*kvcoord.DistSender)
events := make(chan kvcoord.RangeFeedMessage)
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), testFeedCtxKey{}, struct{}{}))
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) (err error) {
if useMuxRangeFeed {
opts = append(opts, kvcoord.WithMuxRangeFeed())
ctx = context.WithValue(ctx, useMuxRangeFeedCtxKey{}, struct{}{})
}
return ds.RangeFeed(ctx, []roachpb.Span{sp}, startFrom, events, opts...)
})
g.GoCtx(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case ev := <-events:
onValue(ev)
}
}
})
return func() {
cancel()
_ = g.Wait()
}
}
// observeNValues returns on value handler which expects to see N rangefeed values,
// along with the channel which gets closed when requisite count of events has been seen.
func observeNValues(n int) (chan struct{}, func(ev kvcoord.RangeFeedMessage)) {
var count = struct {
syncutil.Mutex
c int
}{}
allSeen := make(chan struct{})
return allSeen, func(ev kvcoord.RangeFeedMessage) {
if ev.Val != nil {
count.Lock()
defer count.Unlock()
count.c++
log.Infof(context.Background(), "Waiting N values: saw %d, want %d; current=%s", count.c, n, ev.Val.Key)
if count.c == n {
close(allSeen)
}
}
}
}
func channelWaitWithTimeout(t *testing.T, ch chan struct{}) {
t.Helper()
timeOut := 30 * time.Second
if util.RaceEnabled {
timeOut *= 10
}
select {
case <-ch:
case <-time.After(timeOut):
t.Fatal("test timed out")
}
}
func TestBiDirectionalRangefeedNotUsedUntilUpgradeFinalilzed(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
startServerAtVer := func(ver roachpb.Version) (*testcluster.TestCluster, func()) {
st := cluster.MakeTestingClusterSettingsWithVersions(ver, ver, true)
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual, // Turn off replication queues.
ServerArgs: base.TestServerArgs{
Settings: st,
Knobs: base.TestingKnobs{
KVClient: &kvcoord.ClientTestingKnobs{
TransportFactory: makeTransportFactory(false, nil, nil),
},
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: ver,
},
},
},
})
return tc, func() { tc.Stopper().Stop(ctx) }
}
// Create a small table; run rangefeed. The transport factory we injected above verifies
// that we use the old rangefeed implementation.
runRangeFeed := func(tc *testcluster.TestCluster, opts ...kvcoord.RangeFeedOption) {
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
sqlDB.Exec(t, `SET CLUSTER SETTING kv.rangefeed.enabled = true`)
sqlDB.Exec(t, `CREATE TABLE foo (key INT PRIMARY KEY)`)
sqlDB.Exec(t, `INSERT INTO foo (key) SELECT * FROM generate_series(1, 1000)`)
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
allSeen, onValue := observeNValues(1000)
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, startTime, onValue, false)
channelWaitWithTimeout(t, allSeen)
closeFeed()
}
t.Run("rangefeed-stream-disabled-prior-to-version-upgrade", func(t *testing.T) {
noRfStreamVer := clusterversion.ByKey(clusterversion.TODODelete_V22_2RangefeedUseOneStreamPerNode - 1)
tc, cleanup := startServerAtVer(noRfStreamVer)
defer cleanup()
runRangeFeed(tc)
})
t.Run("rangefeed-stream-disabled-via-environment", func(t *testing.T) {
defer kvcoord.TestingSetEnableMuxRangeFeed(false)()
// Even though we could use rangefeed stream, it's disabled via kill switch.
rfStreamVer := clusterversion.ByKey(clusterversion.TODODelete_V22_2RangefeedUseOneStreamPerNode)
tc, cleanup := startServerAtVer(rfStreamVer)
defer cleanup()
runRangeFeed(tc, kvcoord.WithMuxRangeFeed())
})
}
func TestMuxRangeFeedConnectsToNodeOnce(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
connCounts := &internalClientCounts{counts: make(map[rpc.RestrictedInternalClient]int)}
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual, // Turn off replication queues.
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
KVClient: &kvcoord.ClientTestingKnobs{
TransportFactory: makeTransportFactory(true, connCounts, nil),
},
},
},
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
// Create a table, and split it so that we have multiple ranges, distributed across
// test cluster nodes.
sqlDB.ExecMultiple(t,
`SET CLUSTER SETTING kv.rangefeed.enabled = true`,
`ALTER DATABASE defaultdb CONFIGURE ZONE USING num_replicas = 1`,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 1000)`,
`ALTER TABLE foo SPLIT AT (SELECT * FROM generate_series(100, 900, 100))`,
)
for i := 100; i <= 900; i += 100 {
storeID := 1 + i%3
rowID := i
testutils.SucceedsSoon(t, func() error {
_, err := sqlDB.DB.ExecContext(context.Background(),
"ALTER TABLE foo EXPERIMENTAL_RELOCATE VALUES (ARRAY[$1], $2)", storeID, rowID)
return err
})
}
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
allSeen, onValue := observeNValues(1000)
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, startTime, onValue, true)
channelWaitWithTimeout(t, allSeen)
closeFeed()
// Verify we connected to each node once.
connCounts.Lock()
defer connCounts.Unlock()
for _, c := range connCounts.counts {
require.Equal(t, 1, c)
}
}
type blockRecvRangeFeedClient struct {
kvpb.Internal_RangeFeedClient
numRecvRemainingUntilBlocked int
ctxCanceled bool
}
func (b *blockRecvRangeFeedClient) Recv() (*kvpb.RangeFeedEvent, error) {
if !b.ctxCanceled {
ctx := b.Internal_RangeFeedClient.Context()
b.numRecvRemainingUntilBlocked--
if b.numRecvRemainingUntilBlocked < 0 {
select {
case <-ctx.Done():
b.ctxCanceled = true
return nil, ctx.Err()
case <-time.After(testutils.DefaultSucceedsSoonDuration):
return nil, errors.New("did not get stuck")
}
}
}
return b.Internal_RangeFeedClient.Recv()
}
var _ kvpb.Internal_RangeFeedClient = (*blockRecvRangeFeedClient)(nil)
func TestRestartsStuckRangeFeeds(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
blockingClient := &blockRecvRangeFeedClient{}
var wrapRfClient wrapRangeFeedClientFn = func(client kvpb.Internal_RangeFeedClient) kvpb.Internal_RangeFeedClient {
blockingClient.Internal_RangeFeedClient = client
blockingClient.numRecvRemainingUntilBlocked = 1 // let first Recv through, then block
return blockingClient
}
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
KVClient: &kvcoord.ClientTestingKnobs{
TransportFactory: makeTransportFactory(false, nil, wrapRfClient),
},
},
},
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
// Create a table, and split it so that we have multiple ranges, distributed across
// test cluster nodes.
sqlDB.ExecMultiple(t,
`SET CLUSTER SETTING kv.rangefeed.enabled = true`,
`SET CLUSTER SETTING kv.rangefeed.range_stuck_threshold='1s'`,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 100)`,
)
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
allSeen, onValue := observeNValues(100)
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, startTime, onValue, false)
channelWaitWithTimeout(t, allSeen)
closeFeed()
require.True(t, blockingClient.ctxCanceled)
require.EqualValues(t, 1, tc.Server(0).DistSenderI().(*kvcoord.DistSender).Metrics().RangefeedRestartStuck.Count())
}
func TestRestartsStuckRangeFeedsSecondImplementation(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
type testKey struct{}
ctx := context.Background()
var canceled int32 // atomic
var doneErr = errors.New("gracefully terminating test")
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
Store: &kvserver.StoreTestingKnobs{
TestingRangefeedFilter: func(args *kvpb.RangeFeedRequest, stream kvpb.RangeFeedEventSink) *kvpb.Error {
md, ok := metadata.FromIncomingContext(stream.Context())
if (!ok || len(md[t.Name()]) == 0) && stream.Context().Value(testKey{}) == nil {
return nil
}
if atomic.LoadInt32(&canceled) != 0 {
return kvpb.NewError(doneErr)
}
t.Logf("intercepting %s", args)
// Send a first response to "arm" the stuck detector in DistSender.
if assert.NoError(t, stream.Send(&kvpb.RangeFeedEvent{Checkpoint: &kvpb.RangeFeedCheckpoint{
Span: args.Span,
ResolvedTS: hlc.Timestamp{Logical: 1},
}})) {
t.Log("sent first event, now blocking")
}
select {
case <-time.After(testutils.DefaultSucceedsSoonDuration):
return kvpb.NewErrorf("timed out waiting for stuck rangefeed's ctx cancellation")
case <-stream.Context().Done():
t.Log("server side rangefeed canceled (as expected)")
atomic.StoreInt32(&canceled, 1)
}
return nil
},
},
},
},
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
for _, stmt := range []string{
`SET CLUSTER SETTING kv.rangefeed.enabled = true`,
`SET CLUSTER SETTING kv.rangefeed.range_stuck_threshold='1s'`,
} {
sqlDB.Exec(t, stmt)
}
span := func() roachpb.Span {
desc := tc.LookupRangeOrFatal(t, tc.ScratchRange(t))
t.Logf("r%d", desc.RangeID)
return desc.RSpan().AsRawSpanWithNoLocals()
}()
ds := ts.DistSenderI().(*kvcoord.DistSender)
// Use both gRPC metadata and a local ctx key to tag the context for the
// outgoing rangefeed. At time of writing, we're bypassing gRPC due to
// the local optimization, but it's not worth special casing on that.
ctx = metadata.AppendToOutgoingContext(ctx, t.Name(), "please block me")
rangeFeed := func(
t *testing.T,
ctx context.Context,
ds *kvcoord.DistSender,
sp roachpb.Span,
startFrom hlc.Timestamp,
) (_cancel func(), _wait func() error) {
events := make(chan kvcoord.RangeFeedMessage)
ctx, cancel := context.WithCancel(ctx)
{
origCancel := cancel
cancel = func() {
t.Helper()
t.Log("cancel invoked")
origCancel()
}
}
g := ctxgroup.WithContext(ctx)
g.GoCtx(func(ctx context.Context) error {
defer close(events)
err := ds.RangeFeed(ctx, []roachpb.Span{sp}, startFrom, events)
t.Logf("from RangeFeed: %v", err)
return err
})
g.GoCtx(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil // expected
case ev := <-events:
t.Logf("from consumer: %+v", ev)
case <-time.After(testutils.DefaultSucceedsSoonDuration):
return errors.New("timed out waiting to consume events")
}
}
})
return cancel, g.Wait
}
cancel, wait := rangeFeed(t, context.WithValue(ctx, testKey{}, testKey{}), ds, span, startTime)
defer time.AfterFunc(testutils.DefaultSucceedsSoonDuration, cancel).Stop()
{
err := wait()
require.True(t, errors.Is(err, doneErr), "%+v", err)
}
require.EqualValues(t, 1, atomic.LoadInt32(&canceled))
// NB: We really expect exactly 1 but with a 1s timeout, it's not inconceivable that
// on a particularly slow CI machine some unrelated rangefeed could also catch the occasional
// retry.
require.NotZero(t, ds.Metrics().RangefeedRestartStuck.Count())
}
func TestMuxRangeCatchupScanQuotaReleased(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
// Initial setup: only single catchup scan allowed.
sqlDB.ExecMultiple(t,
`SET CLUSTER SETTING kv.rangefeed.enabled = true`,
`SET CLUSTER SETTING kv.rangefeed.catchup_scan_concurrency = 1`,
`ALTER DATABASE defaultdb CONFIGURE ZONE USING num_replicas = 1`,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 1000)`,
)
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
// This error causes rangefeed to restart after re-resolving spans, and causes
// catchup scan quota acquisition.
transientErrEvent := kvpb.RangeFeedEvent{
Error: &kvpb.RangeFeedError{
Error: *kvpb.NewError(kvpb.NewRangeFeedRetryError(kvpb.RangeFeedRetryError_REASON_RANGE_SPLIT)),
}}
noValuesExpected := func(event kvcoord.RangeFeedMessage) {
panic("received value when none expected")
}
const numErrsToReturn = 100
var numErrors atomic.Int32
enoughErrors := make(chan struct{})
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, startTime, noValuesExpected, true,
kvcoord.TestingWithOnRangefeedEvent(
func(_ context.Context, _ roachpb.Span, _ int64, event *kvpb.RangeFeedEvent) (skip bool, _ error) {
*event = transientErrEvent
if numErrors.Add(1) == numErrsToReturn {
close(enoughErrors)
}
return false, nil
}))
channelWaitWithTimeout(t, enoughErrors)
closeFeed()
}
// Test to make sure the various metrics used by rangefeed are correctly
// updated during the lifetime of the rangefeed and when the rangefeed completes.
func TestRangeFeedMetricsManagement(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
startTime := ts.Clock().Now()
// Insert 1000 rows, and split them into 10 ranges.
const numRanges = 10
sqlDB.ExecMultiple(t,
`ALTER DATABASE defaultdb CONFIGURE ZONE USING num_replicas = 1`,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 1000)`,
`ALTER TABLE foo SPLIT AT (SELECT * FROM generate_series(100, 900, 100))`,
)
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
testutils.RunTrueAndFalse(t, "mux", func(t *testing.T, useMux bool) {
metrics := kvcoord.TestingMakeRangeFeedMetrics()
// Number of ranges for which we'll issue transient error.
const numRangesToRetry int64 = 3
// Number of ranges which we will block from completion.
const numCatchupToBlock int64 = 2
// Upon shutdown, make sure the metrics have correct values.
defer func() {
require.EqualValues(t, 0, metrics.RangefeedRanges.Value())
require.EqualValues(t, 0, metrics.RangefeedRestartStuck.Count())
// We injected numRangesToRetry transient errors during catchup scan.
// It is possible however, that we will observe key-mismatch error when restarting
// due to how we split the ranges above (i.e. there is a version of the range
// that goes from e.g. 800-Max, and then there is correct version 800-900).
// When iterating through the entire table span, we pick up correct version.
// However, if we attempt to re-resolve single range, we may get incorrect/old
// version that was cached. Thus, we occasionally see additional transient restarts.
require.GreaterOrEqual(t, metrics.RangefeedErrorCatchup.Count(), numRangesToRetry)
require.GreaterOrEqual(t, metrics.RangefeedRestartRanges.Count(), numRangesToRetry)
// Even though numCatchupToBlock ranges were blocked in the catchup scan phase,
// the counter should be 0 once rangefeed is done.
require.EqualValues(t, 0, metrics.RangefeedCatchupRanges.Value())
}()
frontier, err := span.MakeFrontier(fooSpan)
require.NoError(t, err)
// This error causes rangefeed to restart.
transientErrEvent := kvpb.RangeFeedEvent{
Error: &kvpb.RangeFeedError{Error: *kvpb.NewError(&kvpb.StoreNotFoundError{})},
}
var numRetried atomic.Int64
var numCatchupBlocked atomic.Int64
skipSet := struct {
syncutil.Mutex
stuck roachpb.SpanGroup // Spans that are stuck in catchup scan.
retry roachpb.SpanGroup // Spans we issued retry for.
}{}
const kindRetry = true
const kindStuck = false
shouldSkip := func(k roachpb.Key, kind bool) bool {
skipSet.Lock()
defer skipSet.Unlock()
if kind == kindRetry {
return skipSet.retry.Contains(k)
}
return skipSet.stuck.Contains(k)
}
ignoreValues := func(event kvcoord.RangeFeedMessage) {}
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, startTime, ignoreValues, useMux,
kvcoord.TestingWithRangeFeedMetrics(&metrics),
kvcoord.TestingWithOnRangefeedEvent(
func(ctx context.Context, s roachpb.Span, _ int64, event *kvpb.RangeFeedEvent) (skip bool, _ error) {
switch t := event.GetValue().(type) {
case *kvpb.RangeFeedValue:
// If we previously arranged for the range to be skipped (stuck catchup scan),
// then skip any value that belongs to the skipped range.
// This is only needed for mux rangefeed, since regular rangefeed just blocks.
return useMux && shouldSkip(t.Key, kindStuck), nil
case *kvpb.RangeFeedCheckpoint:
if checkpoint := t; checkpoint.Span.Contains(s) {
if checkpoint.ResolvedTS.IsEmpty() {
return false, nil
}
// Skip any subsequent checkpoint if we previously arranged for
// range to be skipped.
if useMux && shouldSkip(checkpoint.Span.Key, kindStuck) {
return true, nil
}
if !shouldSkip(checkpoint.Span.Key, kindRetry) && numRetried.Add(1) <= numRangesToRetry {
// Return transient error for this range, but do this only once per range.
skipSet.Lock()
skipSet.retry.Add(checkpoint.Span)
skipSet.Unlock()
log.Infof(ctx, "skipping span %s", checkpoint.Span)
*event = transientErrEvent
return false, nil
}
_, err := frontier.Forward(checkpoint.Span, checkpoint.ResolvedTS)
if err != nil {
return false, err
}
if numCatchupBlocked.Add(1) <= numCatchupToBlock {
if useMux {
// Mux rangefeed can't block single range, so just skip this event
// and arrange for other events belonging to this range to be skipped as well.
skipSet.Lock()
skipSet.stuck.Add(checkpoint.Span)
skipSet.Unlock()
log.Infof(ctx, "skipping stuck span %s", checkpoint.Span)
return true /* skip */, nil
}
// Regular rangefeed can block to prevent catchup completion until rangefeed is canceled.
return false, timeutil.RunWithTimeout(ctx, "wait-rf-timeout", time.Minute,
func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
}
}
}
return false, nil
}))
defer closeFeed()
// Wait for the test frontier to advance. Once it advances,
// we know the rangefeed is started, all ranges are running (even if some of them are blocked).
testutils.SucceedsWithin(t, func() error {
if frontier.Frontier().IsEmpty() {
return errors.Newf("waiting for frontier advance: %s", frontier.String())
}
return nil
}, 10*time.Second)
// At this point, we know the rangefeed for all ranges are running.
require.EqualValues(t, numRanges, metrics.RangefeedRanges.Value(), frontier.String())
// We also know that we have blocked numCatchupToBlock ranges in their catchup scan.
require.EqualValues(t, numCatchupToBlock, metrics.RangefeedCatchupRanges.Value())
})
}
// TestRangefeedRangeObserver ensures the kvcoord.WithRangeObserver option
// works correctly.
func TestRangefeedRangeObserver(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)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
kvserver.RangefeedEnabled.Override(
context.Background(), &ts.ClusterSettings().SV, true)
testutils.RunTrueAndFalse(t, "mux", func(t *testing.T, useMux bool) {
sqlDB.ExecMultiple(t,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 4)`,
`ALTER TABLE foo SPLIT AT (SELECT * FROM generate_series(1, 4, 1))`,
)
defer func() {
sqlDB.Exec(t, `DROP TABLE foo`)
}()
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
ignoreValues := func(event kvcoord.RangeFeedMessage) {}
ctx2, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up an observer to continuously poll for the list of ranges
// being watched.
var observedRangesMu syncutil.Mutex
observedRanges := make(map[string]struct{})
observer := func(fn kvcoord.ForEachRangeFn) {
go func() {
for {
select {
case <-ctx2.Done():
return
default:
}
observedRangesMu.Lock()
observedRanges = make(map[string]struct{})
err := fn(func(rfCtx kvcoord.RangeFeedContext, feed kvcoord.PartialRangeFeed) error {
observedRanges[feed.Span.String()] = struct{}{}
return nil
})
observedRangesMu.Unlock()
require.NoError(t, err)
}
}()
}
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, ts.Clock().Now(), ignoreValues, useMux,
kvcoord.WithRangeObserver(observer))
defer closeFeed()
makeSpan := func(suffix string) string {
return fmt.Sprintf("/Table/%d/%s", fooDesc.GetID(), suffix)
}
// The initial set of ranges we expect to observe.
expectedRanges := map[string]struct{}{
makeSpan("1{-/1}"): {},
makeSpan("1/{1-2}"): {},
makeSpan("1/{2-3}"): {},
makeSpan("1/{3-4}"): {},
makeSpan("{1/4-2}"): {},
}
checkExpectedRanges := func() {
testutils.SucceedsWithin(t, func() error {
observedRangesMu.Lock()
defer observedRangesMu.Unlock()
for r := range expectedRanges {
if _, ok := observedRanges[r]; !ok {
return errors.Newf("expected range %s to be observed in map %v", r, observedRanges)
}
}
return nil
}, 10*time.Second)
}
checkExpectedRanges()
// Add another range and ensure we can observe it.
sqlDB.ExecMultiple(t,
`INSERT INTO FOO VALUES(5)`,
`ALTER TABLE foo SPLIT AT VALUES(5)`,
)
expectedRanges = map[string]struct{}{
makeSpan("1{-/1}"): {},
makeSpan("1/{1-2}"): {},
makeSpan("1/{2-3}"): {},
makeSpan("1/{3-4}"): {},
makeSpan("1/{4-5}"): {},
makeSpan("{1/5-2}"): {},
}
checkExpectedRanges()
})
}
// TestMuxRangeFeedCanCloseStream verifies stream termination functionality in mux rangefeed.
func TestMuxRangeFeedCanCloseStream(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
})
defer tc.Stopper().Stop(ctx)
ts := tc.Server(0)
sqlDB := sqlutils.MakeSQLRunner(tc.ServerConn(0))
// Insert 1000 rows, and split them into 10 ranges.
sqlDB.ExecMultiple(t,
`SET CLUSTER SETTING kv.rangefeed.enabled = true`,
`SET CLUSTER SETTING kv.closed_timestamp.target_duration='100ms'`,
`ALTER DATABASE defaultdb CONFIGURE ZONE USING num_replicas = 1`,
`CREATE TABLE foo (key INT PRIMARY KEY)`,
`INSERT INTO foo (key) SELECT * FROM generate_series(1, 1000)`,
`ALTER TABLE foo SPLIT AT (SELECT * FROM generate_series(100, 900, 100))`,
)
fooDesc := desctestutils.TestingGetPublicTableDescriptor(
ts.DB(), keys.SystemSQLCodec, "defaultdb", "foo")
fooSpan := fooDesc.PrimaryIndexSpan(keys.SystemSQLCodec)
frontier, err := span.MakeFrontier(fooSpan)
require.NoError(t, err)
expectFrontierAdvance := func() {
t.Helper()
// Closed timestamp for range advances every100ms. We'll require frontier to
// advance a bit more thn that.
threshold := frontier.Frontier().AddDuration(250 * time.Millisecond)
testutils.SucceedsWithin(t, func() error {
if frontier.Frontier().Less(threshold) {
return errors.Newf("waiting for frontier advance to at least %s", threshold)
}
return nil
}, 10*time.Second)
}
var observedStreams sync.Map
var capturedSender atomic.Value
ignoreValues := func(event kvcoord.RangeFeedMessage) {}
var numRestartStreams atomic.Int32
closeFeed := rangeFeed(ts.DistSenderI(), fooSpan, ts.Clock().Now(), ignoreValues, true,
kvcoord.WithMuxRangeFeed(),
kvcoord.TestingWithMuxRangeFeedRequestSenderCapture(
// We expect a single mux sender since we have 1 node in this test.
func(nodeID roachpb.NodeID, capture func(request *kvpb.RangeFeedRequest) error) {
capturedSender.Store(capture)
},
),
kvcoord.TestingWithOnRangefeedEvent(
func(ctx context.Context, s roachpb.Span, streamID int64, event *kvpb.RangeFeedEvent) (skip bool, _ error) {
switch t := event.GetValue().(type) {
case *kvpb.RangeFeedCheckpoint:
observedStreams.Store(streamID, nil)
_, err := frontier.Forward(t.Span, t.ResolvedTS)
if err != nil {
return true, err
}
case *kvpb.RangeFeedError:
// Keep track of mux errors due to RangeFeedRetryError_REASON_RANGEFEED_CLOSED.
// Those results when we issue CloseStream request.
err := t.Error.GoError()
log.Infof(ctx, "Got err: %v", err)
var retryErr *kvpb.RangeFeedRetryError
if ok := errors.As(err, &retryErr); ok && retryErr.Reason == kvpb.RangeFeedRetryError_REASON_RANGEFEED_CLOSED {
numRestartStreams.Add(1)
}
}
return false, nil
}),
)
defer closeFeed()
// Wait until we capture mux rangefeed request sender. There should only be 1.
var muxRangeFeedRequestSender func(req *kvpb.RangeFeedRequest) error
testutils.SucceedsWithin(t, func() error {