Skip to content

Commit 6801e64

Browse files
craig[bot]asg0451
craig[bot]
andcommittedMar 19, 2025·
Merge #143102
143102: changefeedccl: ensure that spans planned from cdc queries have end keys r=rharding6373,andyyang890 a=asg0451 When a cdc query results in a plan that scans a single key, the end key of the span is unset. This causes the feed to fail on startup. We now set span.EndKey = span.Key.Next(). Fixes: #143101 Release note (bug fix): Fixed a bug that caused feeds to fail on startup when scanning a single key. Co-authored-by: Miles Frankel <[email protected]>
2 parents 7c38217 + adb564a commit 6801e64

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
 

‎pkg/ccl/changefeedccl/cdceval/plan.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ func SpansForExpression(
132132
return nil, withErrorHint(err, d.FamilyName, d.HasOtherFamilies)
133133
}
134134

135-
return plan.Spans, nil
135+
// Make sure any single-key spans are expanded to have end keys.
136+
spans := plan.Spans
137+
for i := range spans {
138+
if len(spans[i].EndKey) == 0 {
139+
spans[i].EndKey = spans[i].Key.Clone().Next()
140+
}
141+
}
142+
143+
return spans, nil
136144
}
137145

138146
// withErrorHint wraps error with error hints.

‎pkg/ccl/changefeedccl/changefeed_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -10794,6 +10794,58 @@ func TestChangefeedProtectedTimestampUpdate(t *testing.T) {
1079410794
cdcTest(t, testFn, feedTestForceSink("kafka"), withTxnRetries)
1079510795
}
1079610796

10797+
func TestCDCQuerySelectSingleRow(t *testing.T) {
10798+
defer leaktest.AfterTest(t)()
10799+
defer log.Scope(t).Close(t)
10800+
10801+
errCh := make(chan error, 1)
10802+
knobsFn := func(knobs *base.TestingKnobs) {
10803+
if knobs.DistSQL == nil {
10804+
knobs.DistSQL = &execinfra.TestingKnobs{}
10805+
}
10806+
if knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed == nil {
10807+
knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed = &TestingKnobs{}
10808+
}
10809+
cfKnobs := knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed.(*TestingKnobs)
10810+
cfKnobs.HandleDistChangefeedError = func(err error) error {
10811+
errCh <- err
10812+
return err
10813+
}
10814+
}
10815+
10816+
testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) {
10817+
db := sqlutils.MakeSQLRunner(s.DB)
10818+
db.Exec(t, `CREATE TABLE foo (key INT PRIMARY KEY);`)
10819+
db.Exec(t, `INSERT INTO foo VALUES (1), (2), (3);`)
10820+
10821+
// initial_scan='only' is not required, but it makes testing this easier.
10822+
foo := feed(t, f, `CREATE CHANGEFEED WITH initial_scan='only' AS SELECT * FROM foo WHERE key = 1`)
10823+
defer closeFeed(t, foo)
10824+
10825+
done := make(chan struct{})
10826+
go func() {
10827+
defer close(done)
10828+
assertPayloads(t, foo, []string{`foo: [1]->{"key": 1}`})
10829+
}()
10830+
10831+
select {
10832+
case err := <-errCh:
10833+
// Ignore any error after the above assertion completed, because
10834+
// it's likely just due to feed shutdown.
10835+
select {
10836+
case <-done:
10837+
default:
10838+
t.Fatalf("unexpected error: %v", err)
10839+
}
10840+
case <-time.After(30 * time.Second):
10841+
t.Fatal("timed out")
10842+
case <-done:
10843+
return
10844+
}
10845+
}
10846+
cdcTest(t, testFn, withKnobsFn(knobsFn))
10847+
}
10848+
1079710849
func assertReasonableMVCCTimestamp(t *testing.T, ts string) {
1079810850
epochNanos := parseTimeToHLC(t, ts).WallTime
1079910851
now := timeutil.Now()

0 commit comments

Comments
 (0)
Please sign in to comment.