|
| 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 logical |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/sql" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/lease" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/sql/isql" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestTombstoneUpdaterSetsOriginID(t *testing.T) { |
| 25 | + defer leaktest.AfterTest(t)() |
| 26 | + defer log.Scope(t).Close(t) |
| 27 | + |
| 28 | + // This is a regression test for a bug in the tombstone updater. The |
| 29 | + // tombstone updater should always set the origin ID. Previously, it would |
| 30 | + // not set the origin id which caused logical replication to pick up the |
| 31 | + // tombstone update as a deletion event. This is undesirable because the |
| 32 | + // tombstone update case is only used when replicating deletes and if a |
| 33 | + // replicated write generates an LDR event, it leads to looping. |
| 34 | + |
| 35 | + // Start server with two databases |
| 36 | + ctx := context.Background() |
| 37 | + server, s, runners, dbNames := setupServerWithNumDBs(t, ctx, testClusterBaseClusterArgs, 1, 2) |
| 38 | + defer server.Stopper().Stop(ctx) |
| 39 | + |
| 40 | + // Create test table on both databases |
| 41 | + destRunner := runners[1] |
| 42 | + |
| 43 | + // Create a tombstone updater |
| 44 | + desc := desctestutils.TestingGetMutableExistingTableDescriptor( |
| 45 | + s.DB(), s.Codec(), dbNames[0], "tab") |
| 46 | + sd := sql.NewInternalSessionData(ctx, s.ClusterSettings(), "" /* opName */) |
| 47 | + tu := newTombstoneUpdater(s.Codec(), s.DB(), s.LeaseManager().(*lease.Manager), desc.GetID(), sd, s.ClusterSettings()) |
| 48 | + defer tu.ReleaseLeases(ctx) |
| 49 | + |
| 50 | + // Set up 1 way logical replication. The replication stream is used to ensure |
| 51 | + // that the tombstone update will not be replicated as a deletion event. |
| 52 | + var jobID jobspb.JobID |
| 53 | + destRunner.QueryRow(t, |
| 54 | + "CREATE LOGICAL REPLICATION STREAM FROM TABLE a.tab ON $1 INTO TABLE b.tab", |
| 55 | + GetPGURLs(t, s, dbNames)[0].String()).Scan(&jobID) |
| 56 | + |
| 57 | + // Write the row to the destination |
| 58 | + destRunner.Exec(t, "INSERT INTO tab VALUES (1, 42)") |
| 59 | + |
| 60 | + row := tree.Datums{ |
| 61 | + tree.NewDInt(tree.DInt(1)), // k |
| 62 | + tree.DNull, // v (deleted) |
| 63 | + } |
| 64 | + |
| 65 | + _, err := tu.updateTombstone(ctx, nil, s.Clock().Now(), row) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + config := s.ExecutorConfig().(sql.ExecutorConfig) |
| 69 | + err = sql.DescsTxn(ctx, &config, func( |
| 70 | + ctx context.Context, txn isql.Txn, descriptors *descs.Collection, |
| 71 | + ) error { |
| 72 | + _, err = tu.updateTombstone(ctx, txn, s.Clock().Now(), row) |
| 73 | + return err |
| 74 | + }) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Wait for replication to advance |
| 78 | + WaitUntilReplicatedTime(t, s.Clock().Now(), destRunner, jobID) |
| 79 | + |
| 80 | + // Verify the row still exists in the destination |
| 81 | + destRunner.CheckQueryResults(t, "SELECT pk, payload FROM tab", [][]string{ |
| 82 | + {"1", "42"}, |
| 83 | + }) |
| 84 | +} |
0 commit comments