Skip to content

Commit 886f79c

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 41984dd commit 886f79c

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
@@ -9702,3 +9702,55 @@ func TestChangefeedProtectedTimestampUpdate(t *testing.T) {
97029702

97039703
cdcTest(t, testFn, feedTestForceSink("kafka"), withTxnRetries)
97049704
}
9705+
9706+
func TestCDCQuerySelectSingleRow(t *testing.T) {
9707+
defer leaktest.AfterTest(t)()
9708+
defer log.Scope(t).Close(t)
9709+
9710+
errCh := make(chan error, 1)
9711+
knobsFn := func(knobs *base.TestingKnobs) {
9712+
if knobs.DistSQL == nil {
9713+
knobs.DistSQL = &execinfra.TestingKnobs{}
9714+
}
9715+
if knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed == nil {
9716+
knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed = &TestingKnobs{}
9717+
}
9718+
cfKnobs := knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed.(*TestingKnobs)
9719+
cfKnobs.HandleDistChangefeedError = func(err error) error {
9720+
errCh <- err
9721+
return err
9722+
}
9723+
}
9724+
9725+
testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) {
9726+
db := sqlutils.MakeSQLRunner(s.DB)
9727+
db.Exec(t, `CREATE TABLE foo (key INT PRIMARY KEY);`)
9728+
db.Exec(t, `INSERT INTO foo VALUES (1), (2), (3);`)
9729+
9730+
// initial_scan='only' is not required, but it makes testing this easier.
9731+
foo := feed(t, f, `CREATE CHANGEFEED WITH initial_scan='only' AS SELECT * FROM foo WHERE key = 1`)
9732+
defer closeFeed(t, foo)
9733+
9734+
done := make(chan struct{})
9735+
go func() {
9736+
defer close(done)
9737+
assertPayloads(t, foo, []string{`foo: [1]->{"key": 1}`})
9738+
}()
9739+
9740+
select {
9741+
case err := <-errCh:
9742+
// Ignore any error after the above assertion completed, because
9743+
// it's likely just due to feed shutdown.
9744+
select {
9745+
case <-done:
9746+
default:
9747+
t.Fatalf("unexpected error: %v", err)
9748+
}
9749+
case <-time.After(30 * time.Second):
9750+
t.Fatal("timed out")
9751+
case <-done:
9752+
return
9753+
}
9754+
}
9755+
cdcTest(t, testFn, withKnobsFn(knobsFn))
9756+
}

0 commit comments

Comments
 (0)