-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdc.go
407 lines (335 loc) · 11.8 KB
/
cdc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright © 2022 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iterator
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/conduitio-labs/conduit-connector-vitess/columntypes"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/doug-martin/goqu/v9"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/proto/binlogdata"
"vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/proto/vtgate"
"vitess.io/vitess/go/vt/vtctl/vtctlclient"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
// we need the goqu/v9/dialect/mysql to work with the mysql dialect.
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
// we need the grpcvtctlclient to interact with the vtctl client via gRPC.
_ "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient"
)
const (
// defaultInitialGtid is the default gtid to start with.
defaultInitialGtid = "current"
// defaultVtctlCommandTimeout is the default action timeout for any vtctl command.
defaultVtctlCommandTimeout = time.Second * 5
// findAllShardsInKeyspaceCommand is a name for vtctl's FindAllShardsInKeyspace command.
findAllShardsInKeyspaceCommand = "FindAllShardsInKeyspace"
)
// cdc is an implementation of a CDC iterator for Vitess.
type cdc struct {
reader vtgateconn.VStreamReader
// fields contains all fields that vstream returns,
// fields can change, for example, the field type can change,
// and storing the fields here we can handle this.
fields []*query.Field
records chan sdk.Record
errCh chan error
stopCh chan struct{}
table string
keyColumn string
orderingColumn string
position *Position
}
// cdcParams is incoming params for the newCDC function.
type cdcParams struct {
Conn *vtgateconn.VTGateConn
Address string
Keyspace string
Table string
TabletType string
KeyColumn string
OrderingColumn string
Columns []string
Position *Position
}
// newCDC creates new instance of the CDC.
func newCDC(ctx context.Context, params cdcParams) (*cdc, error) {
cdc := &cdc{
records: make(chan sdk.Record, defaultRecordsBufferSize),
errCh: make(chan error, 1),
stopCh: make(chan struct{}, 1),
table: params.Table,
keyColumn: params.KeyColumn,
orderingColumn: params.OrderingColumn,
position: params.Position,
}
if cdc.position == nil {
shardGtids, err := cdc.findAllShardsInKeyspace(ctx, params.Address, params.Keyspace)
if err != nil {
return nil, fmt.Errorf("find all shards in keyspace %q: %w", params.Keyspace, err)
}
cdc.position = &Position{
Mode: ModeCDC,
Keyspace: params.Keyspace,
ShardGtids: shardGtids,
}
}
if err := cdc.setupVStream(ctx, params); err != nil {
return nil, fmt.Errorf("setup vstream: %w", err)
}
go cdc.listen(ctx)
return cdc, nil
}
// HasNext returns a bool indicating whether the iterator has the next record to return or not.
func (c *cdc) HasNext(context.Context) (bool, error) {
return len(c.records) > 0, nil
}
// Next returns the next record.
func (c *cdc) Next(ctx context.Context) (sdk.Record, error) {
select {
case <-ctx.Done():
return sdk.Record{}, ctx.Err()
case err := <-c.errCh:
return sdk.Record{}, err
case record := <-c.records:
return record, nil
}
}
// Stop does nothing.
func (c *cdc) Stop(ctx context.Context) error {
sdk.Logger(ctx).Debug().Msgf("stop cdc iterator")
c.stopCh <- struct{}{}
return nil
}
// setupVStream opens a connection to a vtgate and create a VStream reader.
// The method returns the connection, the VStream reader and a gtid.
func (c *cdc) setupVStream(ctx context.Context, params cdcParams) error {
vgtid := &binlogdata.VGtid{
ShardGtids: c.position.ShardGtids,
}
ruleFilter, err := c.constructRuleFilter(params.Table, params.OrderingColumn, params.Columns)
if err != nil {
return fmt.Errorf("construct rule filter: %w", err)
}
filter := &binlogdata.Filter{
Rules: []*binlogdata.Rule{{
Match: params.Table,
Filter: ruleFilter,
}},
}
tabletType := topodata.TabletType(topodata.TabletType_value[params.TabletType])
reader, err := params.Conn.VStream(ctx, tabletType, vgtid, filter, &vtgate.VStreamFlags{
MinimizeSkew: false,
})
if err != nil {
return fmt.Errorf("create vstream reader: %w", err)
}
c.reader = reader
return nil
}
// findAllShardsInKeyspace executes a vtctl's FindAllShardsInKeyspace command, parses and returns the result.
func (c *cdc) findAllShardsInKeyspace(ctx context.Context, address, keyspace string) ([]*binlogdata.ShardGtid, error) {
vtctlClient, err := vtctlclient.New(address)
if err != nil {
return nil, fmt.Errorf("vtctlclient connect: %w", err)
}
defer vtctlClient.Close()
// for more details,
// see https://vitess.io/docs/14.0/reference/programs/vtctldclient/vtctldclient_findallshardsinkeyspace/
eventStream, err := vtctlClient.ExecuteVtctlCommand(
ctx, []string{findAllShardsInKeyspaceCommand, keyspace}, defaultVtctlCommandTimeout,
)
if err != nil {
return nil, fmt.Errorf("execute FindAllShardsInKeyspace vtctl command: %w", err)
}
event, err := eventStream.Recv()
if err != nil {
return nil, fmt.Errorf("receive FindAllShardsInKeyspace vtctl command result: %w", err)
}
eventStr := event.GetValue()
if eventStr == "" {
return nil, ErrFindAllShardsInKeyspaceReturnedNothing
}
shardsInfo := make(map[string]any)
if err := json.Unmarshal([]byte(eventStr), &shardsInfo); err != nil {
return nil, fmt.Errorf("unmarshal shards info: %w", err)
}
shardGtids := make([]*binlogdata.ShardGtid, 0, len(shardsInfo))
for shard := range shardsInfo {
shardGtids = append(shardGtids, &binlogdata.ShardGtid{
Keyspace: keyspace,
Shard: shard,
Gtid: defaultInitialGtid,
})
}
return shardGtids, nil
}
// constructRuleFilter constructs an SQL query for the binlogdata.Filter.Rules.
func (c *cdc) constructRuleFilter(table, orderingColumn string, columns []string) (string, error) {
selectDataset := goqu.Dialect("mysql").Select()
if len(columns) > 0 {
keyColumnPresent := false
// add one to the capacity to have a space for the keyColumn
// if it's not present in the columns list.
cols := make([]any, 0, len(columns)+1)
for i := 0; i < len(columns); i++ {
if columns[i] == c.keyColumn {
keyColumnPresent = true
}
cols = append(cols, columns[i])
}
if !keyColumnPresent {
cols = append(cols, c.keyColumn)
}
selectDataset = selectDataset.Select(cols...)
}
query, _, err := selectDataset.
From(table).
Order(goqu.C(orderingColumn).Asc()).
ToSQL()
if err != nil {
return "", fmt.Errorf("construct rule filter query: %w", err)
}
return query, nil
}
// listen listens for VStream events.
// If the VStream encountered an error the method will send it to the errCh channel.
// All the data are sent to the records channel.
func (c *cdc) listen(ctx context.Context) {
for {
select {
case <-c.stopCh:
return
default:
events, err := c.reader.Recv()
if err != nil {
c.errCh <- fmt.Errorf("read from vstream: %w", err)
return
}
var rowEvent *binlogdata.VEvent
for _, event := range events {
switch eventType := event.Type; {
case eventType == binlogdata.VEventType_VGTID && rowEvent != nil:
c.position.ShardGtids = event.Vgtid.ShardGtids
if err := c.processRowEvent(rowEvent); err != nil {
sdk.Logger(ctx).Error().Str("err", err.Error()).Msg("process row event")
c.errCh <- fmt.Errorf("process row event: %w", err)
return
}
case eventType == binlogdata.VEventType_FIELD:
c.fields = event.FieldEvent.Fields
case eventType == binlogdata.VEventType_ROW:
if c.fields == nil {
// shouldn't happen cause VEventType_FIELD always comes before VEventType_ROW.
sdk.Logger(ctx).Warn().Msgf("i.fields is nil, skipping the row")
continue
}
rowEvent = event
default:
}
}
}
}
}
// processRowEvent makes rows from the event.RowEvent.RowChanges trusted,
// constructs the resulting slice containing all needed sqltypes.Values,
// transforms it to a sdk.Record and sends the record to a c.records channel.
func (c *cdc) processRowEvent(event *binlogdata.VEvent) error {
for _, change := range event.RowEvent.RowChanges {
var (
valuesBefore []sqltypes.Value
valuesAfter []sqltypes.Value
operation = sdk.OperationCreate
)
switch after, before := change.After, change.Before; {
case after != nil && before != nil:
operation = sdk.OperationUpdate
valuesBefore = sqltypes.MakeRowTrusted(c.fields, before)
valuesAfter = sqltypes.MakeRowTrusted(c.fields, after)
case before != nil:
operation = sdk.OperationDelete
valuesAfter = sqltypes.MakeRowTrusted(c.fields, before)
default:
valuesAfter = sqltypes.MakeRowTrusted(c.fields, after)
}
record, err := c.transformRowsToRecord(valuesBefore, valuesAfter, operation)
if err != nil {
return fmt.Errorf("transform rows to record: %w", err)
}
c.records <- record
}
return nil
}
// transformRowsToRecord transforms after and before of type []sqltypes.Values to a sdk.Record,
// based on the provided operation.
func (c *cdc) transformRowsToRecord(before, after []sqltypes.Value, operation sdk.Operation) (sdk.Record, error) {
var (
transformedRowBefore sdk.StructuredData
transformedRowAfter sdk.StructuredData
orderingColumnValue any
key sdk.StructuredData
err error
)
if len(before) > 0 {
_, _, transformedRowBefore, err = c.transformValuesToNative(before)
if err != nil {
return sdk.Record{}, fmt.Errorf("transform values to native: %w", err)
}
}
if len(after) > 0 {
key, orderingColumnValue, transformedRowAfter, err = c.transformValuesToNative(after)
if err != nil {
return sdk.Record{}, fmt.Errorf("transform values to native: %w", err)
}
// set this in order to avoid the 'same position' error,
// as the event can have multiple rows under one gtid.
c.position.LastProcessedElementValue = orderingColumnValue
}
sdkPosition, err := c.position.MarshalSDKPosition()
if err != nil {
return sdk.Record{}, fmt.Errorf("marshal position to sdk position: %w", err)
}
metadata := make(sdk.Metadata)
metadata.SetCreatedAt(time.Now())
metadata[metadataKeyTable] = c.table
switch operation {
case sdk.OperationCreate:
return sdk.Util.Source.NewRecordCreate(sdkPosition, metadata, key, transformedRowAfter), nil
case sdk.OperationUpdate:
return sdk.Util.Source.NewRecordUpdate(
sdkPosition, metadata, key, transformedRowBefore, transformedRowAfter,
), nil
case sdk.OperationDelete:
return sdk.Util.Source.NewRecordDelete(sdkPosition, metadata, key), nil
default:
// shouldn't happen
return sdk.Record{}, fmt.Errorf("unknown operation: %q", operation)
}
}
// transformValuesToNative transforms a provided row to native values.
// The methods returns extracted value for sdk.Record.Key, ordering column's value,
// transormed row's bytes and an error.
func (c *cdc) transformValuesToNative(row []sqltypes.Value) (sdk.StructuredData, any, sdk.StructuredData, error) {
transformedRow, err := columntypes.TransformValuesToNative(c.fields, row)
if err != nil {
return nil, nil, nil, fmt.Errorf("transform row value: %w", err)
}
return sdk.StructuredData{
c.keyColumn: transformedRow[c.keyColumn],
}, transformedRow[c.orderingColumn], sdk.StructuredData(transformedRow), nil
}