-
Notifications
You must be signed in to change notification settings - Fork 288
/
sink.go
1257 lines (1119 loc) · 39.7 KB
/
sink.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"fmt"
"sort"
"strconv"
"strings"
"sync/atomic"
"unsafe"
"github.com/pingcap/log"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/util/rowcodec"
"github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/integrity"
"github.com/pingcap/tiflow/pkg/quotes"
"github.com/pingcap/tiflow/pkg/sink"
"github.com/pingcap/tiflow/pkg/util"
"go.uber.org/zap"
)
//go:generate msgp
// MessageType is the type of message, which is used by MqSink and RedoLog.
type MessageType int
const (
// MessageTypeUnknown is unknown type of message key
MessageTypeUnknown MessageType = iota
// MessageTypeRow is row type of message key
MessageTypeRow
// MessageTypeDDL is ddl type of message key
MessageTypeDDL
// MessageTypeResolved is resolved type of message key
MessageTypeResolved
)
const (
// the RowChangedEvent order in the same transaction
typeDelete = iota + 1
typeUpdate
typeInsert
)
// ColumnFlagType is for encapsulating the flag operations for different flags.
type ColumnFlagType util.Flag
const (
// BinaryFlag means the column charset is binary
BinaryFlag ColumnFlagType = 1 << ColumnFlagType(iota)
// HandleKeyFlag means the column is selected as the handle key
// The handleKey is chosen by the following rules in the order:
// 1. if the table has primary key, it's the handle key.
// 2. If the table has not null unique key, it's the handle key.
// 3. If the table has no primary key and no not null unique key, it has no handleKey.
HandleKeyFlag
// GeneratedColumnFlag means the column is a generated column
GeneratedColumnFlag
// PrimaryKeyFlag means the column is primary key
PrimaryKeyFlag
// UniqueKeyFlag means the column is unique key
UniqueKeyFlag
// MultipleKeyFlag means the column is multiple key
MultipleKeyFlag
// NullableFlag means the column is nullable
NullableFlag
// UnsignedFlag means the column stores an unsigned integer
UnsignedFlag
)
// SetIsBinary sets BinaryFlag
func (b *ColumnFlagType) SetIsBinary() {
(*util.Flag)(b).Add(util.Flag(BinaryFlag))
}
// UnsetIsBinary unsets BinaryFlag
func (b *ColumnFlagType) UnsetIsBinary() {
(*util.Flag)(b).Remove(util.Flag(BinaryFlag))
}
// IsBinary shows whether BinaryFlag is set
func (b *ColumnFlagType) IsBinary() bool {
return (*util.Flag)(b).HasAll(util.Flag(BinaryFlag))
}
// SetIsHandleKey sets HandleKey
func (b *ColumnFlagType) SetIsHandleKey() {
(*util.Flag)(b).Add(util.Flag(HandleKeyFlag))
}
// UnsetIsHandleKey unsets HandleKey
func (b *ColumnFlagType) UnsetIsHandleKey() {
(*util.Flag)(b).Remove(util.Flag(HandleKeyFlag))
}
// IsHandleKey shows whether HandleKey is set
func (b *ColumnFlagType) IsHandleKey() bool {
return (*util.Flag)(b).HasAll(util.Flag(HandleKeyFlag))
}
// SetIsGeneratedColumn sets GeneratedColumn
func (b *ColumnFlagType) SetIsGeneratedColumn() {
(*util.Flag)(b).Add(util.Flag(GeneratedColumnFlag))
}
// UnsetIsGeneratedColumn unsets GeneratedColumn
func (b *ColumnFlagType) UnsetIsGeneratedColumn() {
(*util.Flag)(b).Remove(util.Flag(GeneratedColumnFlag))
}
// IsGeneratedColumn shows whether GeneratedColumn is set
func (b *ColumnFlagType) IsGeneratedColumn() bool {
return (*util.Flag)(b).HasAll(util.Flag(GeneratedColumnFlag))
}
// SetIsPrimaryKey sets PrimaryKeyFlag
func (b *ColumnFlagType) SetIsPrimaryKey() {
(*util.Flag)(b).Add(util.Flag(PrimaryKeyFlag))
}
// UnsetIsPrimaryKey unsets PrimaryKeyFlag
func (b *ColumnFlagType) UnsetIsPrimaryKey() {
(*util.Flag)(b).Remove(util.Flag(PrimaryKeyFlag))
}
// IsPrimaryKey shows whether PrimaryKeyFlag is set
func (b *ColumnFlagType) IsPrimaryKey() bool {
return (*util.Flag)(b).HasAll(util.Flag(PrimaryKeyFlag))
}
// SetIsUniqueKey sets UniqueKeyFlag
func (b *ColumnFlagType) SetIsUniqueKey() {
(*util.Flag)(b).Add(util.Flag(UniqueKeyFlag))
}
// UnsetIsUniqueKey unsets UniqueKeyFlag
func (b *ColumnFlagType) UnsetIsUniqueKey() {
(*util.Flag)(b).Remove(util.Flag(UniqueKeyFlag))
}
// IsUniqueKey shows whether UniqueKeyFlag is set
func (b *ColumnFlagType) IsUniqueKey() bool {
return (*util.Flag)(b).HasAll(util.Flag(UniqueKeyFlag))
}
// IsMultipleKey shows whether MultipleKeyFlag is set
func (b *ColumnFlagType) IsMultipleKey() bool {
return (*util.Flag)(b).HasAll(util.Flag(MultipleKeyFlag))
}
// SetIsMultipleKey sets MultipleKeyFlag
func (b *ColumnFlagType) SetIsMultipleKey() {
(*util.Flag)(b).Add(util.Flag(MultipleKeyFlag))
}
// UnsetIsMultipleKey unsets MultipleKeyFlag
func (b *ColumnFlagType) UnsetIsMultipleKey() {
(*util.Flag)(b).Remove(util.Flag(MultipleKeyFlag))
}
// IsNullable shows whether NullableFlag is set
func (b *ColumnFlagType) IsNullable() bool {
return (*util.Flag)(b).HasAll(util.Flag(NullableFlag))
}
// SetIsNullable sets NullableFlag
func (b *ColumnFlagType) SetIsNullable() {
(*util.Flag)(b).Add(util.Flag(NullableFlag))
}
// UnsetIsNullable unsets NullableFlag
func (b *ColumnFlagType) UnsetIsNullable() {
(*util.Flag)(b).Remove(util.Flag(NullableFlag))
}
// IsUnsigned shows whether UnsignedFlag is set
func (b *ColumnFlagType) IsUnsigned() bool {
return (*util.Flag)(b).HasAll(util.Flag(UnsignedFlag))
}
// SetIsUnsigned sets UnsignedFlag
func (b *ColumnFlagType) SetIsUnsigned() {
(*util.Flag)(b).Add(util.Flag(UnsignedFlag))
}
// UnsetIsUnsigned unsets UnsignedFlag
func (b *ColumnFlagType) UnsetIsUnsigned() {
(*util.Flag)(b).Remove(util.Flag(UnsignedFlag))
}
// TableName represents name of a table, includes table name and schema name.
type TableName struct {
Schema string `toml:"db-name" msg:"db-name"`
Table string `toml:"tbl-name" msg:"tbl-name"`
TableID int64 `toml:"tbl-id" msg:"tbl-id"`
IsPartition bool `toml:"is-partition" msg:"is-partition"`
}
// String implements fmt.Stringer interface.
func (t TableName) String() string {
return fmt.Sprintf("%s.%s", t.Schema, t.Table)
}
// QuoteString returns quoted full table name
func (t TableName) QuoteString() string {
return quotes.QuoteSchema(t.Schema, t.Table)
}
// GetSchema returns schema name.
func (t *TableName) GetSchema() string {
return t.Schema
}
// GetTable returns table name.
func (t *TableName) GetTable() string {
return t.Table
}
// GetTableID returns table ID.
func (t *TableName) GetTableID() int64 {
return t.TableID
}
// RedoLogType is the type of log
type RedoLogType int
const (
// RedoLogTypeUnknown is unknown type of log
RedoLogTypeUnknown RedoLogType = iota
// RedoLogTypeRow is row type of log
RedoLogTypeRow
// RedoLogTypeDDL is ddl type of log
RedoLogTypeDDL
)
// RedoLog defines the persistent structure of redo log
// since MsgPack do not support types that are defined in another package,
// more info https://github.com/tinylib/msgp/issues/158, https://github.com/tinylib/msgp/issues/149
// so define a RedoColumn, RedoDDLEvent instead of using the Column, DDLEvent
type RedoLog struct {
RedoRow RedoRowChangedEvent `msg:"row"`
RedoDDL RedoDDLEvent `msg:"ddl"`
Type RedoLogType `msg:"type"`
}
// GetCommitTs returns the commit ts of the redo log.
func (r *RedoLog) GetCommitTs() Ts {
switch r.Type {
case RedoLogTypeRow:
return r.RedoRow.Row.CommitTs
case RedoLogTypeDDL:
return r.RedoDDL.DDL.CommitTs
default:
log.Panic("invalid redo log type", zap.Any("type", r.Type))
}
return 0
}
// TrySplitAndSortUpdateEvent redo log do nothing
func (r *RedoLog) TrySplitAndSortUpdateEvent(_ string) error {
return nil
}
// RedoRowChangedEvent represents the DML event used in RedoLog
type RedoRowChangedEvent struct {
Row *RowChangedEventInRedoLog `msg:"row"`
Columns []RedoColumn `msg:"columns"`
PreColumns []RedoColumn `msg:"pre-columns"`
}
// RedoDDLEvent represents DDL event used in redo log persistent
type RedoDDLEvent struct {
DDL *DDLEvent `msg:"ddl"`
Type byte `msg:"type"`
TableName TableName `msg:"table-name"`
}
// ToRedoLog converts row changed event to redo log
func (r *RowChangedEvent) ToRedoLog() *RedoLog {
rowInRedoLog := &RowChangedEventInRedoLog{
StartTs: r.StartTs,
CommitTs: r.CommitTs,
Table: &TableName{
Schema: r.TableInfo.GetSchemaName(),
Table: r.TableInfo.GetTableName(),
TableID: r.PhysicalTableID,
IsPartition: r.TableInfo.IsPartitionTable(),
},
Columns: r.GetColumns(),
PreColumns: r.GetPreColumns(),
IndexColumns: r.TableInfo.IndexColumnsOffset,
}
return &RedoLog{
RedoRow: RedoRowChangedEvent{Row: rowInRedoLog},
Type: RedoLogTypeRow,
}
}
// ToRedoLog converts ddl event to redo log
func (d *DDLEvent) ToRedoLog() *RedoLog {
return &RedoLog{
RedoDDL: RedoDDLEvent{DDL: d},
Type: RedoLogTypeDDL,
}
}
// RowChangedEvent represents a row changed event
//
//msgp:ignore RowChangedEvent
type RowChangedEvent struct {
StartTs uint64
CommitTs uint64
RowID int64 // Deprecated. It is empty when the RowID comes from clustered index table.
PhysicalTableID int64
// NOTICE: We probably store the logical ID inside TableInfo's TableName,
// not the physical ID.
// For normal table, there is only one ID, which is the physical ID.
// AKA TIDB_TABLE_ID.
// For partitioned table, there are two kinds of ID:
// 1. TIDB_PARTITION_ID is the physical ID of the partition.
// 2. TIDB_TABLE_ID is the logical ID of the table.
// In general, we always use the physical ID to represent a table, but we
// record the logical ID from the DDL event(job.BinlogInfo.TableInfo).
// So be careful when using the TableInfo.
TableInfo *TableInfo
Columns []*ColumnData
PreColumns []*ColumnData
// Checksum for the event, only not nil if the upstream TiDB enable the row level checksum
// and TiCDC set the integrity check level to the correctness.
Checksum *integrity.Checksum
// ApproximateDataSize is the approximate size of protobuf binary
// representation of this event.
ApproximateDataSize int64
// SplitTxn marks this RowChangedEvent as the first line of a new txn.
SplitTxn bool
// ReplicatingTs is ts when a table starts replicating events to downstream.
ReplicatingTs Ts
// HandleKey is the key of the row changed event.
// It can be used to identify the row changed event.
// It can be one of three : common_handle, int_handle or _tidb_rowid based on the table definitions
// 1. primary key is the clustered index, and key is not int type, then we use `CommonHandle`
// 2. primary key is int type(including different types of int, such as bigint, TINYINT), then we use IntHandle
// 3. when the table doesn't have the primary key and clustered index,
// tidb will make a hidden column called "_tidb_rowid" as the handle.
// due to the type of "_tidb_rowid" is int, so we also use IntHandle to represent.
HandleKey kv.Handle
}
// RowChangedEventInRedoLog is used to store RowChangedEvent in redo log v2 format
type RowChangedEventInRedoLog struct {
StartTs uint64 `msg:"start-ts"`
CommitTs uint64 `msg:"commit-ts"`
// Table contains the table name and table ID.
// NOTICE: We store the physical table ID here, not the logical table ID.
Table *TableName `msg:"table"`
Columns []*Column `msg:"columns"`
PreColumns []*Column `msg:"pre-columns"`
IndexColumns [][]int `msg:"index-columns"`
}
// txnRows represents a set of events that belong to the same transaction.
type txnRows []*RowChangedEvent
// Len is the number of elements in the collection.
func (e txnRows) Len() int {
return len(e)
}
// Less sort the events base on the order of event type delete<update<insert
func (e txnRows) Less(i, j int) bool {
return getDMLOrder(e[i]) < getDMLOrder(e[j])
}
// getDMLOrder returns the order of the dml types: delete<update<insert
func getDMLOrder(event *RowChangedEvent) int {
if event.IsDelete() {
return typeDelete
} else if event.IsUpdate() {
return typeUpdate
}
return typeInsert
}
func (e txnRows) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
// GetCommitTs returns the commit timestamp of this event.
func (r *RowChangedEvent) GetCommitTs() uint64 {
return r.CommitTs
}
// TrySplitAndSortUpdateEvent do nothing
func (r *RowChangedEvent) TrySplitAndSortUpdateEvent(_ string) error {
return nil
}
// IsDelete returns true if the row is a delete event
func (r *RowChangedEvent) IsDelete() bool {
return len(r.PreColumns) != 0 && len(r.Columns) == 0
}
// IsInsert returns true if the row is an insert event
func (r *RowChangedEvent) IsInsert() bool {
return len(r.PreColumns) == 0 && len(r.Columns) != 0
}
// IsUpdate returns true if the row is an update event
func (r *RowChangedEvent) IsUpdate() bool {
return len(r.PreColumns) != 0 && len(r.Columns) != 0
}
func columnData2Column(col *ColumnData, tableInfo *TableInfo) *Column {
colID := col.ColumnID
offset, ok := tableInfo.columnsOffset[colID]
if !ok {
log.Panic("invalid column id",
zap.Int64("columnID", colID),
zap.Any("tableInfo", tableInfo))
}
colInfo := tableInfo.Columns[offset]
return &Column{
Name: colInfo.Name.O,
Type: colInfo.GetType(),
Charset: colInfo.GetCharset(),
Collation: colInfo.GetCollate(),
Flag: tableInfo.ColumnsFlag[colID],
Value: col.Value,
Default: GetColumnDefaultValue(colInfo),
}
}
func columnDatas2Columns(cols []*ColumnData, tableInfo *TableInfo) []*Column {
if cols == nil {
return nil
}
columns := make([]*Column, len(cols))
for i, colData := range cols {
if colData == nil {
log.Warn("meet nil column data, should not happened in production env",
zap.Any("cols", cols),
zap.Any("tableInfo", tableInfo))
continue
}
columns[i] = columnData2Column(colData, tableInfo)
}
return columns
}
// GetColumns returns the columns of the event
func (r *RowChangedEvent) GetColumns() []*Column {
return columnDatas2Columns(r.Columns, r.TableInfo)
}
// GetPreColumns returns the pre columns of the event
func (r *RowChangedEvent) GetPreColumns() []*Column {
return columnDatas2Columns(r.PreColumns, r.TableInfo)
}
// PrimaryKeyColumnNames return all primary key's name
func (r *RowChangedEvent) PrimaryKeyColumnNames() []string {
var result []string
var cols []*ColumnData
if r.IsDelete() {
cols = r.PreColumns
} else {
cols = r.Columns
}
result = make([]string, 0)
tableInfo := r.TableInfo
for _, col := range cols {
if col != nil && tableInfo.ForceGetColumnFlagType(col.ColumnID).IsPrimaryKey() {
result = append(result, tableInfo.ForceGetColumnName(col.ColumnID))
}
}
return result
}
// GetHandleKeyColumnValues returns all handle key's column values
func (r *RowChangedEvent) GetHandleKeyColumnValues() []string {
var result []string
var cols []*ColumnData
if r.IsDelete() {
cols = r.PreColumns
} else {
cols = r.Columns
}
result = make([]string, 0)
tableInfo := r.TableInfo
for _, col := range cols {
if col != nil && tableInfo.ForceGetColumnFlagType(col.ColumnID).IsHandleKey() {
result = append(result, ColumnValueString(col.Value))
}
}
return result
}
// HandleKeyColInfos returns the column(s) and colInfo(s) corresponding to the handle key(s)
func (r *RowChangedEvent) HandleKeyColInfos() ([]*Column, []rowcodec.ColInfo) {
pkeyCols := make([]*Column, 0)
pkeyColInfos := make([]rowcodec.ColInfo, 0)
var cols []*ColumnData
if r.IsDelete() {
cols = r.PreColumns
} else {
cols = r.Columns
}
tableInfo := r.TableInfo
colInfos := tableInfo.GetColInfosForRowChangedEvent()
for i, col := range cols {
if col != nil && tableInfo.ForceGetColumnFlagType(col.ColumnID).IsHandleKey() {
pkeyCols = append(pkeyCols, columnData2Column(col, tableInfo))
pkeyColInfos = append(pkeyColInfos, colInfos[i])
}
}
// It is okay not to have handle keys, so the empty array is an acceptable result
return pkeyCols, pkeyColInfos
}
// ApproximateBytes returns approximate bytes in memory consumed by the event.
func (r *RowChangedEvent) ApproximateBytes() int {
const sizeOfRowEvent = int(unsafe.Sizeof(*r))
size := 0
// Size of cols
for i := range r.Columns {
size += r.Columns[i].ApproximateBytes
}
// Size of pre cols
for i := range r.PreColumns {
if r.PreColumns[i] != nil {
size += r.PreColumns[i].ApproximateBytes
}
}
// Size of an empty row event
size += sizeOfRowEvent
return size
}
// Columns2ColumnDatas convert `Column`s to `ColumnData`s
func Columns2ColumnDatas(cols []*Column, tableInfo *TableInfo) []*ColumnData {
if cols == nil {
return nil
}
columns := make([]*ColumnData, len(cols))
for i, col := range cols {
if col == nil {
continue
}
colID := tableInfo.ForceGetColumnIDByName(col.Name)
columns[i] = &ColumnData{
ColumnID: colID,
Value: col.Value,
}
}
return columns
}
// Column represents a column value and its schema info
type Column struct {
Name string `msg:"name"`
Type byte `msg:"type"`
Charset string `msg:"charset"`
Collation string `msg:"collation"`
Flag ColumnFlagType `msg:"-"`
Value interface{} `msg:"-"`
Default interface{} `msg:"-"`
// ApproximateBytes is approximate bytes consumed by the column.
ApproximateBytes int `msg:"-"`
}
// ColumnData represents a column value in row changed event
type ColumnData struct {
// ColumnID may be just a mock id, because we don't store it in redo log.
// So after restore from redo log, we need to give every a column a mock id.
// The only guarantee is that the column id is unique in a RowChangedEvent
ColumnID int64 `json:"column_id" msg:"column_id"`
Value interface{} `json:"value" msg:"-"`
// ApproximateBytes is approximate bytes consumed by the column.
ApproximateBytes int `json:"-" msg:"-"`
}
// RedoColumn stores Column change
type RedoColumn struct {
// Fields from Column and can't be marshaled directly in Column.
Value interface{} `msg:"column"`
// msgp transforms empty byte slice into nil, PTAL msgp#247.
ValueIsEmptyBytes bool `msg:"value-is-empty-bytes"`
Flag uint64 `msg:"flag"`
}
// ColumnIDAllocator represents the interface to allocate column id for tableInfo
type ColumnIDAllocator interface {
// GetColumnID return the column id according to the column name
GetColumnID(name string) int64
}
// IncrementalColumnIDAllocator allocates column id in an incremental way.
// At most of the time, it is the default implementation when you don't care the column id's concrete value.
//
//msgp:ignore IncrementalColumnIDAllocator
type IncrementalColumnIDAllocator struct {
nextColID int64
}
// NewIncrementalColumnIDAllocator creates a new IncrementalColumnIDAllocator
func NewIncrementalColumnIDAllocator() *IncrementalColumnIDAllocator {
return &IncrementalColumnIDAllocator{
nextColID: 100, // 100 is an arbitrary number
}
}
// GetColumnID return the next mock column id
func (d *IncrementalColumnIDAllocator) GetColumnID(name string) int64 {
result := d.nextColID
d.nextColID += 1
return result
}
// NameBasedColumnIDAllocator allocates column id using an prefined map from column name to id
//
//msgp:ignore NameBasedColumnIDAllocator
type NameBasedColumnIDAllocator struct {
nameToIDMap map[string]int64
}
// NewNameBasedColumnIDAllocator creates a new NameBasedColumnIDAllocator
func NewNameBasedColumnIDAllocator(nameToIDMap map[string]int64) *NameBasedColumnIDAllocator {
return &NameBasedColumnIDAllocator{
nameToIDMap: nameToIDMap,
}
}
// GetColumnID return the column id of the name
func (n *NameBasedColumnIDAllocator) GetColumnID(name string) int64 {
colID, ok := n.nameToIDMap[name]
if !ok {
log.Panic("column not found",
zap.String("name", name),
zap.Any("nameToIDMap", n.nameToIDMap))
}
return colID
}
// BuildTableInfo builds a table info from given information.
// Note that some fields of the result TableInfo may just be mocked.
// The only guarantee is that we can use the result to reconstrut the information in `Column`.
// The main use cases of this function it to build TableInfo from redo log and in tests.
func BuildTableInfo(schemaName, tableName string, columns []*Column, indexColumns [][]int) *TableInfo {
tidbTableInfo := BuildTiDBTableInfo(tableName, columns, indexColumns)
return WrapTableInfo(100 /* not used */, schemaName, 1000 /* not used */, tidbTableInfo)
}
// BuildTableInfoWithPKNames4Test builds a table info from given information.
func BuildTableInfoWithPKNames4Test(schemaName, tableName string, columns []*Column, pkNames map[string]struct{}) *TableInfo {
if len(pkNames) == 0 {
return BuildTableInfo(schemaName, tableName, columns, nil)
}
indexColumns := make([][]int, 1)
indexColumns[0] = make([]int, 0)
for i, col := range columns {
if _, ok := pkNames[col.Name]; ok {
indexColumns[0] = append(indexColumns[0], i)
col.Flag.SetIsHandleKey()
col.Flag.SetIsPrimaryKey()
}
}
if len(indexColumns[0]) != len(pkNames) {
log.Panic("cannot find all pks",
zap.Any("indexColumns", indexColumns),
zap.Any("pkNames", pkNames))
}
return BuildTableInfo(schemaName, tableName, columns, indexColumns)
}
// AddExtraColumnInfo is used to add some extra column info to the table info.
// Just use it in test.
func AddExtraColumnInfo(tableInfo *model.TableInfo, extraColInfos []rowcodec.ColInfo) {
for i, colInfo := range extraColInfos {
tableInfo.Columns[i].SetElems(colInfo.Ft.GetElems())
tableInfo.Columns[i].SetFlen(colInfo.Ft.GetFlen())
}
}
// GetHandleAndUniqueIndexOffsets4Test is used to get the offsets of handle columns and other unique index columns in test
func GetHandleAndUniqueIndexOffsets4Test(cols []*Column) [][]int {
result := make([][]int, 0)
handleColumns := make([]int, 0)
for i, col := range cols {
if col.Flag.IsHandleKey() {
handleColumns = append(handleColumns, i)
} else if col.Flag.IsUniqueKey() {
// When there is a unique key which is not handle key,
// we cannot get the accurate index info for this key.
// So just be aggressive to make each unique column a unique index
// to make sure there is no write conflict when syncing data in tests.
result = append(result, []int{i})
}
}
if len(handleColumns) != 0 {
result = append(result, handleColumns)
}
return result
}
// BuildTiDBTableInfoWithoutVirtualColumns build a TableInfo without virual columns from the source table info
func BuildTiDBTableInfoWithoutVirtualColumns(source *model.TableInfo) *model.TableInfo {
ret := source.Clone()
ret.Columns = make([]*model.ColumnInfo, 0, len(source.Columns))
rowColumnsCurrentOffset := 0
columnsOffset := make(map[string]int, len(source.Columns))
for _, srcCol := range source.Columns {
if !IsColCDCVisible(srcCol) {
continue
}
colInfo := srcCol.Clone()
colInfo.Offset = rowColumnsCurrentOffset
ret.Columns = append(ret.Columns, colInfo)
columnsOffset[colInfo.Name.O] = rowColumnsCurrentOffset
rowColumnsCurrentOffset += 1
}
// Keep all the index info even if it contains virtual columns for simplicity
for _, indexInfo := range ret.Indices {
for _, col := range indexInfo.Columns {
col.Offset = columnsOffset[col.Name.O]
}
}
return ret
}
// BuildTiDBTableInfo is a simple wrapper over BuildTiDBTableInfoImpl which create a default ColumnIDAllocator.
func BuildTiDBTableInfo(tableName string, columns []*Column, indexColumns [][]int) *model.TableInfo {
return BuildTiDBTableInfoImpl(tableName, columns, indexColumns, NewIncrementalColumnIDAllocator())
}
// BuildTiDBTableInfoImpl builds a TiDB TableInfo from given information.
// Note the result TableInfo may not be same as the original TableInfo in tidb.
// The only guarantee is that you can restore the `Name`, `Type`, `Charset`, `Collation`
// and `Flag` field of `Column` using the result TableInfo.
// The precondition required for calling this function:
// 1. There must be at least one handle key in `columns`;
// 2. The handle key must either be a primary key or a non null unique key;
// 3. The index that is selected as the handle must be provided in `indexColumns`;
func BuildTiDBTableInfoImpl(
tableName string,
columns []*Column,
indexColumns [][]int,
columnIDAllocator ColumnIDAllocator,
) *model.TableInfo {
ret := &model.TableInfo{}
ret.Name = model.NewCIStr(tableName)
hasPrimaryKeyColumn := false
for i, col := range columns {
columnInfo := &model.ColumnInfo{
Offset: i,
State: model.StatePublic,
}
if col == nil {
// actually, col should never be nil according to `datum2Column` and `WrapTableInfo` in prod env
// we mock it as generated column just for test
columnInfo.Name = model.NewCIStr("omitted")
columnInfo.GeneratedExprString = "pass_generated_check"
columnInfo.GeneratedStored = false
ret.Columns = append(ret.Columns, columnInfo)
continue
}
// add a mock id to identify columns inside cdc
columnInfo.ID = columnIDAllocator.GetColumnID(col.Name)
columnInfo.Name = model.NewCIStr(col.Name)
columnInfo.SetType(col.Type)
if col.Collation != "" {
columnInfo.SetCollate(col.Collation)
} else {
// collation is not stored, give it a default value
columnInfo.SetCollate(mysql.UTF8MB4DefaultCollation)
}
// inverse initColumnsFlag
flag := col.Flag
if col.Charset != "" {
columnInfo.SetCharset(col.Charset)
} else if flag.IsBinary() {
columnInfo.SetCharset("binary")
} else {
// charset is not stored, give it a default value
columnInfo.SetCharset(mysql.UTF8MB4Charset)
}
if flag.IsGeneratedColumn() {
// we do not use this field, so we set it to any non-empty string
columnInfo.GeneratedExprString = "pass_generated_check"
columnInfo.GeneratedStored = true
}
if flag.IsPrimaryKey() {
columnInfo.AddFlag(mysql.PriKeyFlag)
hasPrimaryKeyColumn = true
if !flag.IsHandleKey() {
log.Panic("Primary key must be handle key",
zap.String("table", tableName),
zap.Any("columns", columns),
zap.Any("indexColumns", indexColumns))
}
// just set it for test compatibility,
// actually we cannot deduce the value of IsCommonHandle from the provided args.
ret.IsCommonHandle = true
}
if flag.IsUniqueKey() {
columnInfo.AddFlag(mysql.UniqueKeyFlag)
}
if flag.IsHandleKey() {
if !flag.IsPrimaryKey() && !flag.IsUniqueKey() {
log.Panic("Handle key must either be primary key or unique key",
zap.String("table", tableName),
zap.Any("columns", columns),
zap.Any("indexColumns", indexColumns))
}
}
if !flag.IsNullable() {
columnInfo.AddFlag(mysql.NotNullFlag)
}
if flag.IsMultipleKey() {
columnInfo.AddFlag(mysql.MultipleKeyFlag)
}
if flag.IsUnsigned() {
columnInfo.AddFlag(mysql.UnsignedFlag)
}
ret.Columns = append(ret.Columns, columnInfo)
}
hasPrimaryKeyIndex := false
hasHandleIndex := false
// TiCDC handles columns according to the following rules:
// 1. If a primary key (PK) exists, it is chosen.
// 2. If there is no PK, TiCDC looks for a not null unique key (UK) with the least number of columns and the smallest index ID.
// So we assign the smallest index id to the index which is selected as handle to mock this behavior.
minIndexID := int64(1)
nextMockIndexID := minIndexID + 1
for i, colOffsets := range indexColumns {
indexInfo := &model.IndexInfo{
Name: model.NewCIStr(fmt.Sprintf("idx_%d", i)),
State: model.StatePublic,
}
firstCol := columns[colOffsets[0]]
if firstCol == nil {
// when the referenced column is nil, we already have a handle index
// so we can skip this index.
// only happens for DELETE event and old value feature is disabled
continue
}
if firstCol.Flag.IsPrimaryKey() {
indexInfo.Unique = true
}
if firstCol.Flag.IsUniqueKey() {
indexInfo.Unique = true
}
isPrimary := true
isAllColumnsHandle := true
for _, offset := range colOffsets {
col := columns[offset]
// When only all columns in the index are primary key, then the index is primary key.
if col == nil || !col.Flag.IsPrimaryKey() {
isPrimary = false
}
if col == nil || !col.Flag.IsHandleKey() {
isAllColumnsHandle = false
}
tiCol := ret.Columns[offset]
indexCol := &model.IndexColumn{}
indexCol.Name = tiCol.Name
indexCol.Offset = offset
indexInfo.Columns = append(indexInfo.Columns, indexCol)
indexInfo.Primary = isPrimary
}
hasPrimaryKeyIndex = hasPrimaryKeyIndex || isPrimary
if isAllColumnsHandle {
// If there is no primary index, only one index will contain columns which are all handles.
// If there is a primary index, the primary index must be the handle.
// And there may be another index which is a subset of the primary index. So we skip this check.
if hasHandleIndex && !hasPrimaryKeyColumn {
log.Panic("Multiple handle index found",
zap.String("table", tableName),
zap.Any("colOffsets", colOffsets),
zap.String("indexName", indexInfo.Name.O),
zap.Any("columns", columns),
zap.Any("indexColumns", indexColumns))
}
hasHandleIndex = true
}
// If there is no primary column, we need allocate the min index id to the one selected as handle.
// In other cases, we don't care the concrete value of index id.
if isAllColumnsHandle && !hasPrimaryKeyColumn {
indexInfo.ID = minIndexID
} else {
indexInfo.ID = nextMockIndexID
nextMockIndexID += 1
}
// TODO: revert the "all column set index related flag" to "only the
// first column set index related flag" if needed
ret.Indices = append(ret.Indices, indexInfo)
}
if hasPrimaryKeyColumn != hasPrimaryKeyIndex {
log.Panic("Primary key column and primary key index is not consistent",
zap.String("table", tableName),
zap.Any("columns", columns),
zap.Any("indexColumns", indexColumns),
zap.Bool("hasPrimaryKeyColumn", hasPrimaryKeyColumn),
zap.Bool("hasPrimaryKeyIndex", hasPrimaryKeyIndex))
}
return ret
}
// ColumnValueString returns the string representation of the column value
func ColumnValueString(c interface{}) string {
var data string
switch v := c.(type) {
case nil:
data = "null"
case bool:
if v {
data = "1"
} else {
data = "0"
}
case int:
data = strconv.FormatInt(int64(v), 10)
case int8:
data = strconv.FormatInt(int64(v), 10)
case int16:
data = strconv.FormatInt(int64(v), 10)
case int32:
data = strconv.FormatInt(int64(v), 10)
case int64:
data = strconv.FormatInt(v, 10)
case uint8:
data = strconv.FormatUint(uint64(v), 10)
case uint16:
data = strconv.FormatUint(uint64(v), 10)
case uint32:
data = strconv.FormatUint(uint64(v), 10)
case uint64:
data = strconv.FormatUint(v, 10)
case float32:
data = strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
data = strconv.FormatFloat(v, 'f', -1, 64)
case string:
data = v
case []byte:
data = string(v)
default:
data = fmt.Sprintf("%v", v)
}
return data
}
// DDLEvent stores DDL event
type DDLEvent struct {
StartTs uint64 `msg:"start-ts"`
CommitTs uint64 `msg:"commit-ts"`
Query string `msg:"query"`
TableInfo *TableInfo `msg:"-"`