|
| 1 | +// Copyright 2025 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Licensed as a Redpanda Enterprise file under the Redpanda Community |
| 4 | +// License (the "License"); you may not use this file except in compliance with |
| 5 | +// the License. You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md |
| 8 | + |
| 9 | +package changestreams |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "errors" |
| 14 | + "fmt" |
| 15 | + |
| 16 | + "cloud.google.com/go/spanner" |
| 17 | + "cloud.google.com/go/spanner/apiv1/spannerpb" |
| 18 | + "google.golang.org/api/iterator" |
| 19 | + |
| 20 | + "github.com/redpanda-data/connect/v4/internal/impl/gcp/enterprise/changestreams/metadata" |
| 21 | +) |
| 22 | + |
| 23 | +type readResult struct { |
| 24 | + ChangeRecords []*ChangeRecord `spanner:"ChangeRecord" json:"change_record"` |
| 25 | +} |
| 26 | + |
| 27 | +type querier struct { |
| 28 | + client *spanner.Client |
| 29 | + dialect dialect |
| 30 | + streamName string |
| 31 | + priority spannerpb.RequestOptions_Priority |
| 32 | +} |
| 33 | + |
| 34 | +// query executes a change stream query for the specified stream and partition. |
| 35 | +// It processes each record from the change stream and calls the callback function. |
| 36 | +func (q *querier) query( |
| 37 | + ctx context.Context, |
| 38 | + pm metadata.PartitionMetadata, |
| 39 | + cb func(ctx context.Context, pm metadata.PartitionMetadata, cr ChangeRecord) error, |
| 40 | +) error { |
| 41 | + var stmt spanner.Statement |
| 42 | + if q.isPostgres() { |
| 43 | + stmt = spanner.Statement{ |
| 44 | + SQL: fmt.Sprintf(`SELECT * FROM spanner.read_json_%s($1, $2, $3, $4, null)`, q.streamName), |
| 45 | + Params: map[string]any{ |
| 46 | + "p1": pm.Watermark, |
| 47 | + "p2": pm.EndTimestamp, |
| 48 | + "p3": pm.PartitionToken, |
| 49 | + "p4": pm.HeartbeatMillis, |
| 50 | + }, |
| 51 | + } |
| 52 | + // Convert to NULL |
| 53 | + if pm.EndTimestamp.IsZero() { |
| 54 | + stmt.Params["p2"] = nil |
| 55 | + } |
| 56 | + if pm.PartitionToken == "" { |
| 57 | + stmt.Params["p3"] = nil |
| 58 | + } |
| 59 | + } else { |
| 60 | + stmt = spanner.Statement{ |
| 61 | + SQL: fmt.Sprintf(`SELECT ChangeRecord FROM READ_%s(@start_timestamp, @end_timestamp, @partition_token, @heartbeat_millis)`, q.streamName), |
| 62 | + Params: map[string]any{ |
| 63 | + "start_timestamp": pm.Watermark, |
| 64 | + "end_timestamp": pm.EndTimestamp, |
| 65 | + "partition_token": pm.PartitionToken, |
| 66 | + "heartbeat_millis": pm.HeartbeatMillis, |
| 67 | + }, |
| 68 | + } |
| 69 | + // Convert to NULL |
| 70 | + if pm.EndTimestamp.IsZero() { |
| 71 | + stmt.Params["end_timestamp"] = nil |
| 72 | + } |
| 73 | + if pm.PartitionToken == "" { |
| 74 | + stmt.Params["partition_token"] = nil |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + iter := q.client.Single().QueryWithOptions(ctx, stmt, spanner.QueryOptions{Priority: q.priority}) |
| 79 | + defer iter.Stop() |
| 80 | + |
| 81 | + for { |
| 82 | + row, err := iter.Next() |
| 83 | + if err != nil { |
| 84 | + if errors.Is(err, iterator.Done) { |
| 85 | + break |
| 86 | + } |
| 87 | + return fmt.Errorf("get change stream results: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + if q.isPostgres() { |
| 91 | + cr, err := decodePostgresRow(row) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("decode postgres row: %w", err) |
| 94 | + } |
| 95 | + if err := cb(ctx, pm, cr); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + } else { |
| 99 | + var rr readResult |
| 100 | + if err := row.ToStruct(&rr); err != nil { |
| 101 | + return fmt.Errorf("row to struct: %w", err) |
| 102 | + } |
| 103 | + for _, cr := range rr.ChangeRecords { |
| 104 | + if err := cb(ctx, pm, *cr); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (q *querier) isPostgres() bool { |
| 115 | + return q.dialect == dialectPostgreSQL |
| 116 | +} |
0 commit comments