|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package changefeedccl |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/cdctest" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl/changefeedbase" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 18 | +) |
| 19 | + |
| 20 | +// TestChangefeedUnwatchedFamilyMemoryMonitoring verifies that changefeeds |
| 21 | +// correctly release memory allocations for events corresponding to unwatched |
| 22 | +// column families after discarding them. This is a regression test for #154776. |
| 23 | +func TestChangefeedUnwatchedFamilyMemoryMonitoring(t *testing.T) { |
| 24 | + defer leaktest.AfterTest(t)() |
| 25 | + defer log.Scope(t).Close(t) |
| 26 | + |
| 27 | + ctx := context.Background() |
| 28 | + |
| 29 | + testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) { |
| 30 | + sqlDB := sqlutils.MakeSQLRunner(s.DB) |
| 31 | + |
| 32 | + // Set a low memory limit. |
| 33 | + changefeedbase.PerChangefeedMemLimit.Override( |
| 34 | + ctx, &s.Server.ClusterSettings().SV, 1<<20 /* 1 MiB */) |
| 35 | + |
| 36 | + // Create a table with two column families. |
| 37 | + sqlDB.Exec(t, `CREATE TABLE foo ( |
| 38 | + id INT PRIMARY KEY, |
| 39 | + a STRING, |
| 40 | + b STRING, |
| 41 | + FAMILY f1 (id, a), |
| 42 | + FAMILY f2 (b) |
| 43 | + )`) |
| 44 | + |
| 45 | + // Insert initial data. |
| 46 | + sqlDB.Exec(t, `INSERT INTO foo VALUES (1, 'a1', 'b1')`) |
| 47 | + |
| 48 | + var args []any |
| 49 | + if _, ok := f.(*webhookFeedFactory); ok { |
| 50 | + args = append(args, optOutOfMetamorphicEnrichedEnvelope{ |
| 51 | + reason: "enriched envelopes do not support column families for webhook sinks", |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + // Start changefeed watching only f1 with diff enabled. |
| 56 | + // Events from f2 should trigger ErrUnwatchedFamily. |
| 57 | + feed := feed(t, f, |
| 58 | + `CREATE CHANGEFEED FOR foo FAMILY f1 WITH diff, initial_scan='no'`, args...) |
| 59 | + defer closeFeed(t, feed) |
| 60 | + |
| 61 | + // Update a watched column to generate an event. |
| 62 | + sqlDB.Exec(t, `UPDATE foo SET a = 'a1_1' WHERE id = 1`) |
| 63 | + assertPayloads(t, feed, []string{ |
| 64 | + `foo.f1: [1]->{"after": {"a": "a1_1", "id": 1}, "before": {"a": "a1", "id": 1}}`, |
| 65 | + }) |
| 66 | + |
| 67 | + // Generate a lot of events for the unwatched family. If the memory |
| 68 | + // allocations are being leaked, this would cause the changefeed to |
| 69 | + // exceed the previously configured 1 MiB limit. |
| 70 | + for i := range 1000 { |
| 71 | + sqlDB.Exec(t, `UPDATE foo SET b = $1 WHERE id = 1`, |
| 72 | + fmt.Sprintf(`b1_%d`+string(make([]byte, 2000)), i), // ~2KB per update |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + // Update watched column again to verify the feed is still progressing. |
| 77 | + // If the memory allocations leaked, this assertion would time out. |
| 78 | + sqlDB.Exec(t, `UPDATE foo SET a = 'a1_2' WHERE id = 1`) |
| 79 | + assertPayloads(t, feed, []string{ |
| 80 | + `foo.f1: [1]->{"after": {"a": "a1_2", "id": 1}, "before": {"a": "a1_1", "id": 1}}`, |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + cdcTest(t, testFn) |
| 85 | +} |
0 commit comments