Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release-23.1: c2c: fix rangefeed error propogration race #103014

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ func (rf *ReplicationFeed) consumeUntil(
for {
msg, haveMoreRows := rf.f.Next()
if !haveMoreRows {
if rf.f.Error() != nil {
if errPred(rf.f.Error()) {
if err := rf.f.Error(); err != nil {
if errPred(err) {
return nil
}
return rf.f.Error()
return err
}
return errors.Newf("ran out of rows after processing %d rows", rowCount)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/ccl/streamingccl/streamproducer/event_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func (s *eventStream) Start(ctx context.Context, txn *kv.Txn) error {

s.acc = s.mon.MakeBoundAccount()

// errCh consumed by ValueGenerator and is signaled when go routines encounter error.
s.errCh = make(chan error)
// errCh is buffered to ensure the sender can send an error to
// the buffer, without waiting, when the channel receiver is not waiting on
// the channel.
s.errCh = make(chan error, 1)

// Events channel gets RangeFeedEvents and is consumed by ValueGenerator.
s.eventsCh = make(chan kvcoord.RangeFeedMessage)
Expand Down Expand Up @@ -159,6 +161,9 @@ func (s *eventStream) Start(ctx context.Context, txn *kv.Txn) error {
}

func (s *eventStream) maybeSetError(err error) {
// Only send the error if the channel is empty, else it's ok to swallow the
// error because the first error in the channel will shut down the event
// stream.
select {
case s.errCh <- err:
default:
Expand Down Expand Up @@ -223,7 +228,6 @@ func (s *eventStream) Close(ctx context.Context) {
// Note: error in close is normal; we expect to be terminated with context canceled.
log.Errorf(ctx, "partition stream %d terminated with error %v", s.streamID, err)
}

s.sp.Finish()
}

Expand Down
22 changes: 10 additions & 12 deletions pkg/ccl/streamingccl/streamproducer/replication_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (d *partitionStreamDecoder) decode() {
func (f *pgConnReplicationFeedSource) Next() (streamingccl.Event, bool) {
f.mu.Lock()
defer f.mu.Unlock()
// First check if there exists a decoded event.
if e := f.mu.codec.pop(); e != nil {
return e, true
}
Expand Down Expand Up @@ -342,9 +343,7 @@ func TestStreamPartition(t *testing.T) {
srcTenant.SQL.Exec(t, `
CREATE DATABASE d;
CREATE TABLE d.t1(i int primary key, a string, b string);
CREATE TABLE d.t2(i int primary key, a string, b string);
INSERT INTO d.t1 (i) VALUES (42);
INSERT INTO d.t2 (i) VALUES (42);
USE d;
`)

Expand All @@ -355,18 +354,22 @@ USE d;

const streamPartitionQuery = `SELECT * FROM crdb_internal.stream_partition($1, $2)`
t1Descr := desctestutils.TestingGetPublicTableDescriptor(h.SysServer.DB(), srcTenant.Codec, "d", "t1")
t2Descr := desctestutils.TestingGetPublicTableDescriptor(h.SysServer.DB(), srcTenant.Codec, "d", "t2")

t.Run("stream-table-cursor-error", func(t *testing.T) {
skip.WithIssue(t, 102286)

srcTenant.SQL.Exec(t, `
CREATE TABLE d.t2(i int primary key, a string, b string);
INSERT INTO d.t2 (i) VALUES (42);
`)
_, feed := startReplication(ctx, t, h, makePartitionStreamDecoder,
streamPartitionQuery, streamID, encodeSpec(t, h, srcTenant, initialScanTimestamp, hlc.Timestamp{}, "t2"))
defer feed.Close(ctx)
t2Descr := desctestutils.TestingGetPublicTableDescriptor(h.SysServer.DB(), srcTenant.Codec, "d", "t2")
expected := replicationtestutils.EncodeKV(t, srcTenant.Codec, t2Descr, 42)
feed.ObserveKey(ctx, expected.Key)

subscribedSpan := h.TableSpan(srcTenant.Codec, "t2")
// Send a ClearRange to trigger rows cursor to return internal error from rangefeed.
// Choose 't2' so that it doesn't trigger error on other registered span in rangefeeds,
// affecting other tests.
_, err := kv.SendWrapped(ctx, h.SysServer.DB().NonTransactionalSender(), &kvpb.ClearRangeRequest{
RequestHeader: kvpb.RequestHeader{
Key: subscribedSpan.Key,
Expand All @@ -375,13 +378,8 @@ USE d;
})
require.Nil(t, err)

expected := replicationtestutils.EncodeKV(t, srcTenant.Codec, t2Descr, 42)
feed.ObserveKey(ctx, expected.Key)
feed.ObserveError(ctx, func(err error) bool {
return strings.Contains(err.Error(), "unexpected MVCC history mutation") ||
// TODO(casper): disabled this once we figured out why we have context cancellation
// emitted from ingestion side.
strings.Contains(err.Error(), "context canceled")
return strings.Contains(err.Error(), "unexpected MVCC history mutation")
})
})

Expand Down