-
Notifications
You must be signed in to change notification settings - Fork 71
/
dml_events.go
577 lines (484 loc) · 15.4 KB
/
dml_events.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package ghostferry
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
"github.com/go-mysql-org/go-mysql/schema"
)
var annotationRegex = regexp.MustCompile(`^/\*(.*?)\*/`)
type RowData []interface{}
// For historical reasons, this function ended up being used at two different
// places: the DataIterator and the DMLEvent (which indirectly is used by the
// BinlogStreamer, InlineVerifier, etc).
//
// The original code here was introduced in
// 152caec0ff5195d4698672df6dc0f72fb77b02fc, where it is used solely in context
// of the DataIterator. In this context, the value being parsed here is given to
// us by the go-sql-driver/mysql driver. This value could either by int64 or it
// could be a byte slice decimal string with the uint64 value in it, which is
// why we have this awkward byte slice to integer trick. This also means the
// original code is not designed to handle uint64, as go-sql-driver/mysql never
// returns uint64. This could possibly be an upstream problem that have since
// been fixed, but this was not investigated.
// (A possibly related PR: https://github.com/go-sql-driver/mysql/pull/690)
//
// At some point, this code was refactored into this function, such that the
// BinlogStreamer also uses the same code to decode integers. The binlog data is
// given to us by go-mysql-org/go-mysql. The go-mysql-org/go-mysql library should
// not be giving us awkward byte slices. Instead, it should properly gives us
// uint64. This code thus panics when it encounters such case. See
// https://github.com/Shopify/ghostferry/issues/165.
//
// In summary:
// - This code receives values from both go-sql-driver/mysql and
// go-mysql-org/go-mysql.
// - go-sql-driver/mysql gives us int64 for signed integer, and uint64 in a byte
// slice for unsigned integer.
// - go-mysql-org/go-mysql gives us int64 for signed integer, and uint64 for
// unsigned integer.
// - We currently make this function deal with both cases. In the future we can
// investigate alternative solutions.
func (r RowData) GetUint64(colIdx int) (uint64, error) {
u64, ok := Uint64Value(r[colIdx])
if ok {
return u64, nil
}
i64, ok := Int64Value(r[colIdx])
if ok {
return uint64(i64), nil
}
if valueByteSlice, ok := r[colIdx].([]byte); ok {
valueString := string(valueByteSlice)
return strconv.ParseUint(valueString, 10, 64)
}
return 0, fmt.Errorf("expected position %d to contain integer, but got type %T instead (with value of %v)", colIdx, r[colIdx], r[colIdx])
}
type DMLEvent interface {
Database() string
Table() string
TableSchema() *TableSchema
AsSQLString(string, string) (string, error)
OldValues() RowData
NewValues() RowData
PaginationKey() (uint64, error)
BinlogPosition() mysql.Position
ResumableBinlogPosition() mysql.Position
Annotation() (string, error)
Timestamp() time.Time
}
// The base of DMLEvent to provide the necessary methods.
type DMLEventBase struct {
table *TableSchema
pos mysql.Position
resumablePos mysql.Position
query []byte
timestamp time.Time
}
func (e *DMLEventBase) Database() string {
return e.table.Schema
}
func (e *DMLEventBase) Table() string {
return e.table.Name
}
func (e *DMLEventBase) TableSchema() *TableSchema {
return e.table
}
func (e *DMLEventBase) BinlogPosition() mysql.Position {
return e.pos
}
func (e *DMLEventBase) ResumableBinlogPosition() mysql.Position {
return e.resumablePos
}
// Annotation will return the first prefixed comment on the SQL string,
// or an error if the query attribute of the DMLEvent is not set
func (e *DMLEventBase) Annotation() (string, error) {
if e.query == nil {
return "", errors.New("could not get query from DML event")
}
captured := annotationRegex.FindSubmatch(e.query)
if len(captured) > 1 {
return string(captured[1]), nil
}
return "", nil
}
func (e *DMLEventBase) Timestamp() time.Time {
return e.timestamp
}
func NewDMLEventBase(table *TableSchema, pos, resumablePos mysql.Position, query []byte, timestamp time.Time) *DMLEventBase {
return &DMLEventBase{
table: table,
pos: pos,
resumablePos: resumablePos,
query: query,
timestamp: timestamp,
}
}
type BinlogInsertEvent struct {
newValues RowData
*DMLEventBase
}
func NewBinlogInsertEvents(eventBase *DMLEventBase, rowsEvent *replication.RowsEvent) ([]DMLEvent, error) {
insertEvents := make([]DMLEvent, len(rowsEvent.Rows))
for i, row := range rowsEvent.Rows {
insertEvents[i] = &BinlogInsertEvent{
newValues: row,
DMLEventBase: eventBase,
}
}
return insertEvents, nil
}
func (e *BinlogInsertEvent) OldValues() RowData {
return nil
}
func (e *BinlogInsertEvent) NewValues() RowData {
return e.newValues
}
func (e *BinlogInsertEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.newValues); err != nil {
return "", err
}
query := "INSERT IGNORE INTO " +
QuotedTableNameFromString(schemaName, tableName) +
" (" + strings.Join(quotedColumnNames(e.table), ",") + ")" +
" VALUES (" + buildStringListForValues(e.table.Columns, e.newValues) + ")"
return query, nil
}
func (e *BinlogInsertEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.newValues)
}
type BinlogUpdateEvent struct {
oldValues RowData
newValues RowData
*DMLEventBase
}
func NewBinlogUpdateEvents(eventBase *DMLEventBase, rowsEvent *replication.RowsEvent) ([]DMLEvent, error) {
// UPDATE events have two rows in the RowsEvent. The first row is the
// entries of the old record (for WHERE) and the second row is the
// entries of the new record (for SET).
// There can be n db rows changed in one RowsEvent, resulting in
// 2*n binlog rows.
updateEvents := make([]DMLEvent, len(rowsEvent.Rows)/2)
for i, row := range rowsEvent.Rows {
if i%2 == 1 {
continue
}
updateEvents[i/2] = &BinlogUpdateEvent{
oldValues: row,
newValues: rowsEvent.Rows[i+1],
DMLEventBase: eventBase,
}
}
return updateEvents, nil
}
func (e *BinlogUpdateEvent) OldValues() RowData {
return e.oldValues
}
func (e *BinlogUpdateEvent) NewValues() RowData {
return e.newValues
}
func (e *BinlogUpdateEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.oldValues, e.newValues); err != nil {
return "", err
}
query := "UPDATE " + QuotedTableNameFromString(schemaName, tableName) +
" SET " + buildStringMapForSet(e.table.Columns, e.newValues) +
" WHERE " + buildStringMapForWhere(e.table.Columns, e.oldValues)
return query, nil
}
func (e *BinlogUpdateEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.newValues)
}
type BinlogDeleteEvent struct {
oldValues RowData
*DMLEventBase
}
func (e *BinlogDeleteEvent) OldValues() RowData {
return e.oldValues
}
func (e *BinlogDeleteEvent) NewValues() RowData {
return nil
}
func NewBinlogDeleteEvents(eventBase *DMLEventBase, rowsEvent *replication.RowsEvent) ([]DMLEvent, error) {
deleteEvents := make([]DMLEvent, len(rowsEvent.Rows))
for i, row := range rowsEvent.Rows {
deleteEvents[i] = &BinlogDeleteEvent{
oldValues: row,
DMLEventBase: eventBase,
}
}
return deleteEvents, nil
}
func (e *BinlogDeleteEvent) AsSQLString(schemaName, tableName string) (string, error) {
if err := verifyValuesHasTheSameLengthAsColumns(e.table, e.oldValues); err != nil {
return "", err
}
query := "DELETE FROM " + QuotedTableNameFromString(schemaName, tableName) +
" WHERE " + buildStringMapForWhere(e.table.Columns, e.oldValues)
return query, nil
}
func (e *BinlogDeleteEvent) PaginationKey() (uint64, error) {
return paginationKeyFromEventData(e.table, e.oldValues)
}
func NewBinlogDMLEvents(table *TableSchema, ev *replication.BinlogEvent, pos, resumablePos mysql.Position, query []byte) ([]DMLEvent, error) {
rowsEvent := ev.Event.(*replication.RowsEvent)
for _, row := range rowsEvent.Rows {
if len(row) != len(table.Columns) {
return nil, fmt.Errorf(
"table %s.%s has %d columns but event has %d columns instead",
table.Schema,
table.Name,
len(table.Columns),
len(row),
)
}
for i, col := range table.Columns {
if col.IsUnsigned {
switch v := row[i].(type) {
case int64:
row[i] = uint64(v)
case int32:
row[i] = uint32(v)
case int16:
row[i] = uint16(v)
case int8:
row[i] = uint8(v)
case int:
row[i] = uint(v)
}
}
}
}
timestamp := time.Unix(int64(ev.Header.Timestamp), 0)
eventBase := NewDMLEventBase(table, pos, resumablePos, query, timestamp)
switch ev.Header.EventType {
case replication.WRITE_ROWS_EVENTv1, replication.WRITE_ROWS_EVENTv2:
return NewBinlogInsertEvents(eventBase, rowsEvent)
case replication.DELETE_ROWS_EVENTv1, replication.DELETE_ROWS_EVENTv2:
return NewBinlogDeleteEvents(eventBase, rowsEvent)
case replication.UPDATE_ROWS_EVENTv1, replication.UPDATE_ROWS_EVENTv2:
return NewBinlogUpdateEvents(eventBase, rowsEvent)
default:
return nil, fmt.Errorf("unrecognized rows event: %s", ev.Header.EventType.String())
}
}
func quotedColumnNames(table *TableSchema) []string {
cols := make([]string, len(table.Columns))
for i, column := range table.Columns {
cols[i] = QuoteField(column.Name)
}
return cols
}
func verifyValuesHasTheSameLengthAsColumns(table *TableSchema, values ...RowData) error {
for _, v := range values {
if len(table.Columns) != len(v) {
return fmt.Errorf(
"table %s.%s has %d columns but event has %d columns instead",
table.Schema,
table.Name,
len(table.Columns),
len(v),
)
}
}
return nil
}
func buildStringListForValues(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, ',')
}
buffer = appendEscapedValue(buffer, value, columns[i])
}
return string(buffer)
}
func buildStringMapForWhere(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, " AND "...)
}
buffer = append(buffer, QuoteField(columns[i].Name)...)
if isNilValue(value) {
// "WHERE value = NULL" will never match rows.
buffer = append(buffer, " IS NULL"...)
} else {
buffer = append(buffer, '=')
buffer = appendEscapedValue(buffer, value, columns[i])
}
}
return string(buffer)
}
func buildStringMapForSet(columns []schema.TableColumn, values []interface{}) string {
var buffer []byte
for i, value := range values {
if i > 0 {
buffer = append(buffer, ',')
}
buffer = append(buffer, QuoteField(columns[i].Name)...)
buffer = append(buffer, '=')
buffer = appendEscapedValue(buffer, value, columns[i])
}
return string(buffer)
}
func isNilValue(value interface{}) bool {
if value == nil {
return true
} else if vb, ok := value.([]byte); ok && vb == nil {
return true
}
return false
}
func appendEscapedValue(buffer []byte, value interface{}, column schema.TableColumn) []byte {
if isNilValue(value) {
return append(buffer, "NULL"...)
}
if uintv, ok := Uint64Value(value); ok {
return strconv.AppendUint(buffer, uintv, 10)
}
if intv, ok := Int64Value(value); ok {
return strconv.AppendInt(buffer, intv, 10)
}
switch v := value.(type) {
case string:
// since https://github.com/go-mysql-org/go-mysql/pull/658/files merged, go-mysql returns JSON events as a string, but we would prefer them as []byte for consistency with other types
if column.Type == schema.TYPE_JSON {
return appendEscapedBuffer(buffer, []byte(v), true)
}
var rightPadLengthForBinaryColumn int = 0
// see appendEscapedString() for details why we need special
// handling of BINARY column types
if column.Type == schema.TYPE_BINARY {
rightPadLengthForBinaryColumn = int(column.FixedSize)
}
return appendEscapedString(buffer, v, rightPadLengthForBinaryColumn)
case []byte:
// schema type cannot be JSON at this point because all JSON results are strings
return appendEscapedBuffer(buffer, v, false)
case bool:
if v {
return append(buffer, '1')
} else {
return append(buffer, '0')
}
case float64:
return strconv.AppendFloat(buffer, v, 'g', -1, 64)
case float32:
return strconv.AppendFloat(buffer, float64(v), 'g', -1, 64)
case decimal.Decimal:
return appendEscapedString(buffer, v.String(), 0)
default:
panic(fmt.Sprintf("unsupported type %t", value))
}
}
func Uint64Value(value interface{}) (uint64, bool) {
switch v := value.(type) {
case uint64:
return v, true
case uint32:
return uint64(v), true
case uint16:
return uint64(v), true
case uint8:
return uint64(v), true
case uint:
return uint64(v), true
}
return 0, false
}
func Int64Value(value interface{}) (int64, bool) {
switch v := value.(type) {
case int64:
return v, true
case int32:
return int64(v), true
case int16:
return int64(v), true
case int8:
return int64(v), true
case int:
return int64(v), true
}
return 0, false
}
// appendEscapedString replaces single quotes with quote-escaped single quotes.
// When the NO_BACKSLASH_ESCAPES mode is on, this is the extent of escaping
// necessary for strings.
//
// ref: https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
// ref: https://github.com/go-sql-driver/mysql/blob/9181e3a86a19bacd63e68d43ae8b7b36320d8092/utils.go#L717-L758
//
// We also need to support right-padding of the generated string using 0-bytes
// to mimic what a MySQL server would do for BINARY columns (with fixed length).
//
// ref: https://github.com/Shopify/ghostferry/pull/159
//
// This is specifically mentioned in the the below link:
//
// When BINARY values are stored, they are right-padded with the pad value
// to the specified length. The pad value is 0x00 (the zero byte). Values
// are right-padded with 0x00 for inserts, and no trailing bytes are removed
// for retrievals.
//
// ref: https://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html
func appendEscapedString(buffer []byte, value string, rightPadToLengthWithZeroBytes int) []byte {
buffer = append(buffer, '\'')
var i int
for i = 0; i < len(value); i++ {
c := value[i]
if c == '\'' {
buffer = append(buffer, '\'', '\'')
} else {
buffer = append(buffer, c)
}
}
// continue 0-padding up to the desired length as provided by the
// caller
if i < rightPadToLengthWithZeroBytes {
buffer = rightPadBufferWithZeroBytes(buffer, rightPadToLengthWithZeroBytes-i)
}
return append(buffer, '\'')
}
func rightPadBufferWithZeroBytes(buffer []byte, padLength int) []byte {
for i := 0; i < padLength; i++ {
buffer = append(buffer, '\x00')
}
return buffer
}
func appendEscapedBuffer(buffer, value []byte, isJSON bool) []byte {
if isJSON {
// See https://bugs.mysql.com/bug.php?id=98496
if len(value) == 0 {
value = []byte("null")
}
buffer = append(buffer, "CAST("...)
} else {
buffer = append(buffer, "_binary"...)
}
buffer = append(buffer, '\'')
for i := 0; i < len(value); i++ {
c := value[i]
if c == '\'' {
buffer = append(buffer, '\'', '\'')
} else {
buffer = append(buffer, c)
}
}
buffer = append(buffer, '\'')
if isJSON {
buffer = append(buffer, " AS JSON)"...)
}
return buffer
}
func paginationKeyFromEventData(table *TableSchema, rowData RowData) (uint64, error) {
if err := verifyValuesHasTheSameLengthAsColumns(table, rowData); err != nil {
return 0, err
}
return rowData.GetUint64(table.GetPaginationKeyIndex())
}