|
| 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 changefeeds |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/util/timeutil" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/workload" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/workload/histogram" |
| 18 | + "github.com/cockroachdb/errors" |
| 19 | + "github.com/jackc/pgx/v5" |
| 20 | +) |
| 21 | + |
| 22 | +// AddChangefeedToQueryLoad augments the passed QueryLoad to contain an extra |
| 23 | +// worker to run a changefeed over the tables of the generator. |
| 24 | +func AddChangefeedToQueryLoad( |
| 25 | + ctx context.Context, |
| 26 | + gen workload.ConnFlagser, |
| 27 | + dbName string, |
| 28 | + urls []string, |
| 29 | + reg *histogram.Registry, |
| 30 | + ql *workload.QueryLoad, |
| 31 | +) error { |
| 32 | + cfg := workload.MultiConnPoolCfg{ |
| 33 | + Method: gen.ConnFlags().Method, |
| 34 | + MaxTotalConnections: 2, |
| 35 | + } |
| 36 | + |
| 37 | + mcp, err := workload.NewMultiConnPool(ctx, cfg, urls...) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + conn, err := mcp.Get().Acquire(ctx) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + if _, err := conn.Exec(ctx, fmt.Sprintf("USE %q", dbName)); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + setAppName := fmt.Sprintf("SET application_name='%s_changefeed'", gen.Meta().Name) |
| 50 | + if _, err := conn.Exec(ctx, setAppName); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + cfLatency := reg.GetHandle().Get("changefeed") |
| 55 | + |
| 56 | + var sessionID string |
| 57 | + if err := conn.QueryRow(ctx, "SHOW session_id").Scan(&sessionID); err != nil { |
| 58 | + return errors.Wrap(err, "getting session_id") |
| 59 | + } |
| 60 | + |
| 61 | + // Create a second connection to close the first connection by issuing a |
| 62 | + // cancel request. |
| 63 | + closeConn, err := mcp.Get().Acquire(ctx) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + if _, err := closeConn.Exec(ctx, setAppName); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + tableNames := strings.Builder{} |
| 72 | + for i, table := range gen.Tables() { |
| 73 | + if i == 0 { |
| 74 | + fmt.Fprintf(&tableNames, "%q", table.Name) |
| 75 | + } else { |
| 76 | + fmt.Fprintf(&tableNames, ", %q", table.Name) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + stmt := fmt.Sprintf("EXPERIMENTAL CHANGEFEED FOR %s WITH updated, no_initial_scan, schema_change_policy=nobackfill", |
| 81 | + tableNames.String()) |
| 82 | + cfCtx, cancel := context.WithCancel(ctx) |
| 83 | + |
| 84 | + var doneErr error |
| 85 | + maybeMarkDone := func(err error) (done bool) { |
| 86 | + if err == nil { |
| 87 | + return false |
| 88 | + } |
| 89 | + cancel() |
| 90 | + _ = conn.Conn().Close(ctx) |
| 91 | + doneErr, conn = err, nil |
| 92 | + return true |
| 93 | + } |
| 94 | + var rows pgx.Rows |
| 95 | + maybeSetupRows := func() (done bool) { |
| 96 | + if rows != nil { |
| 97 | + return false |
| 98 | + } |
| 99 | + var err error |
| 100 | + rows, err = conn.Query(cfCtx, stmt) |
| 101 | + return maybeMarkDone(err) |
| 102 | + } |
| 103 | + ql.WorkerFns = append(ql.WorkerFns, func(ctx context.Context) error { |
| 104 | + if doneErr != nil { |
| 105 | + return doneErr |
| 106 | + } |
| 107 | + if maybeSetupRows() { |
| 108 | + return doneErr |
| 109 | + } |
| 110 | + if rows.Next() { |
| 111 | + values, err := rows.Values() |
| 112 | + if maybeMarkDone(err) { |
| 113 | + return doneErr |
| 114 | + } |
| 115 | + type updatedJSON struct { |
| 116 | + Updated string `json:"updated"` |
| 117 | + } |
| 118 | + var v updatedJSON |
| 119 | + if maybeMarkDone(json.Unmarshal(values[2].([]byte), &v)) { |
| 120 | + return doneErr |
| 121 | + } |
| 122 | + updated, err := hlc.ParseHLC(v.Updated) |
| 123 | + if maybeMarkDone(err) { |
| 124 | + return doneErr |
| 125 | + } |
| 126 | + cfLatency.Record(timeutil.Since(updated.GoTime())) |
| 127 | + return nil |
| 128 | + } |
| 129 | + if maybeMarkDone(rows.Err()) { |
| 130 | + return doneErr |
| 131 | + } |
| 132 | + maybeMarkDone(errors.New("changefeed ended")) |
| 133 | + return doneErr |
| 134 | + }) |
| 135 | + |
| 136 | + prevClose := ql.Close |
| 137 | + ql.Close = func(ctx context.Context) error { |
| 138 | + cancel() |
| 139 | + _, _ = closeConn.Exec(ctx, "CANCEL SESSION $1", sessionID) |
| 140 | + if err := closeConn.Conn().Close(ctx); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + if conn != nil { |
| 144 | + if err := conn.Conn().Close(ctx); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } |
| 148 | + if err := prevClose(ctx); err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + return nil |
| 152 | + } |
| 153 | + return nil |
| 154 | +} |
0 commit comments