Skip to content

Commit 7b0c306

Browse files
committed
changefeedccl: ensure that spans planned from cdc queries have end keys
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: cockroachdb#143101 Release note (bug fix): Fixed a bug that caused feeds to fail on startup when scanning a single key.
1 parent cb94136 commit 7b0c306

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-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

+51
Original file line numberDiff line numberDiff line change
@@ -10794,6 +10794,57 @@ 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+
done := make(chan struct{})
10822+
go func() {
10823+
defer close(done)
10824+
// initial_scan='only' is not required, but it makes testing this easier.
10825+
foo := feed(t, f, `CREATE CHANGEFEED WITH initial_scan='only' AS SELECT * FROM foo WHERE key = 1`)
10826+
defer closeFeed(t, foo)
10827+
assertPayloads(t, foo, []string{`foo: [1]->{"key": 1}`})
10828+
}()
10829+
10830+
select {
10831+
case err := <-errCh:
10832+
// Ignore any error after the above assertion completed, because
10833+
// it's likely just due to feed shutdown.
10834+
select {
10835+
case <-done:
10836+
default:
10837+
t.Fatalf("unexpected error: %v", err)
10838+
}
10839+
case <-time.After(30 * time.Second):
10840+
t.Fatal("timed out")
10841+
case <-done:
10842+
return
10843+
}
10844+
}
10845+
cdcTest(t, testFn, withKnobsFn(knobsFn))
10846+
}
10847+
1079710848
func assertReasonableMVCCTimestamp(t *testing.T, ts string) {
1079810849
epochNanos := parseTimeToHLC(t, ts).WallTime
1079910850
now := timeutil.Now()

0 commit comments

Comments
 (0)