forked from pingcap/ticdc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_active.go
More file actions
514 lines (470 loc) · 15.8 KB
/
active_active.go
File metadata and controls
514 lines (470 loc) · 15.8 KB
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
// Copyright 2025 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 event
import (
"sync/atomic"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/common"
"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/integrity"
"github.com/pingcap/tidb/pkg/parser/mysql"
tidbTypes "github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"go.uber.org/zap"
)
const (
// SoftDeleteTimeColumn stores TiDB soft delete timestamp.
SoftDeleteTimeColumn = "_tidb_softdelete_time"
// OriginTsColumn stores the origin commit ts for active-active tables.
OriginTsColumn = "_tidb_origin_ts"
// CommitTsColumn stores the downstream commit ts for active-active tables.
CommitTsColumn = "_tidb_commit_ts"
)
// RowPolicyDecision represents how a sink should treat a specific row.
type RowPolicyDecision int
const (
// RowPolicyKeep means the row should be emitted unchanged.
RowPolicyKeep RowPolicyDecision = iota
// RowPolicySkip means the row should be ignored.
RowPolicySkip
// RowPolicyConvertToDelete means the row should be converted into a delete event.
RowPolicyConvertToDelete
)
// EvaluateRowPolicy decides how special tables (active-active/soft-delete)
// should behave under the given changefeed mode.
// softDeleteTimeColIndex must refer to `_tidb_softdelete_time`
// when enableActiveActive is false and the table is active-active or soft-delete.
// The decision order is:
// 1. Tables with neither feature are returned untouched.
// 2. Delete rows are skipped no matter which mode we run in.
// 3. When enableActiveActive is true we keep inserts/updates as-is to let
// downstream LWW SQL handle the conflict resolution.
// 4. Otherwise, for update rows we determine whether this row represents a
// soft-delete transition (Origin -> Soft delete), and convert it into a
// delete event to keep downstream consistent.
func EvaluateRowPolicy(
tableInfo *common.TableInfo,
row *RowChange,
enableActiveActive bool,
softDeleteTimeColIndex int,
) RowPolicyDecision {
if tableInfo == nil || row == nil {
return RowPolicyKeep
}
isActiveActive := tableInfo.IsActiveActiveTable()
isSoftDelete := tableInfo.IsSoftDeleteTable()
if !isActiveActive && !isSoftDelete {
return RowPolicyKeep
}
if row.RowType == common.RowTypeDelete {
return RowPolicySkip
}
// When enable-active-active is true, we only drop delete events and keep the rest.
if enableActiveActive {
return RowPolicyKeep
}
if row.RowType != common.RowTypeUpdate {
return RowPolicyKeep
}
if needConvertUpdateToDelete(row, softDeleteTimeColIndex) {
return RowPolicyConvertToDelete
}
return RowPolicyKeep
}
// needConvertUpdateToDelete returns true when this update represents a soft-delete transition.
// The caller must ensure `softDeleteTimeColIndex` is valid for the row.
func needConvertUpdateToDelete(row *RowChange, softDeleteTimeColIndex int) bool {
if row == nil {
return false
}
return isSoftDeleteTransition(row.PreRow, row.Row, softDeleteTimeColIndex)
}
// isSoftDeleteTransition determines whether an update represents a soft-delete transition.
//
// It relies on the invariant that `_tidb_softdelete_time` is defined as `TIMESTAMP(6) NULL`,
// and NULL is the only representation of "not deleted". Under this invariant, a transition
// from not-deleted to deleted is strictly a NULL -> non-NULL change.
func isSoftDeleteTransition(preRow, row chunk.Row, offset int) bool {
if preRow.IsEmpty() || row.IsEmpty() {
return false
}
if offset >= preRow.Len() || offset >= row.Len() {
return false
}
return preRow.IsNull(offset) && !row.IsNull(offset)
}
// getSoftDeleteTimeColumnIndex returns the column offset for `_tidb_softdelete_time` after validating
// its semantics. It reports schema issues via `handleError` and returns ok=false in that case.
//
// The soft-delete transition detection relies on the invariant that `_tidb_softdelete_time` is
// defined as `TIMESTAMP(6) NULL`, and NULL is the only representation of "not deleted".
func getSoftDeleteTimeColumnIndex(event *DMLEvent, handleError func(error)) (idx int, ok bool) {
tableInfo := event.TableInfo
colInfo, ok := tableInfo.GetColumnInfoByName(SoftDeleteTimeColumn)
if !ok {
handleError(errors.Errorf(
"dispatcher %s table %s.%s missing required column %s",
event.DispatcherID.String(),
tableInfo.GetSchemaName(),
tableInfo.GetTableName(),
SoftDeleteTimeColumn,
))
return 0, false
}
offset, ok := tableInfo.GetColumnOffsetByName(SoftDeleteTimeColumn)
if !ok {
handleError(errors.Errorf(
"dispatcher %s table %s.%s missing required column offset %s",
event.DispatcherID.String(),
tableInfo.GetSchemaName(),
tableInfo.GetTableName(),
SoftDeleteTimeColumn,
))
return 0, false
}
notNull := mysql.HasNotNullFlag(colInfo.GetFlag())
if colInfo.GetType() != mysql.TypeTimestamp || colInfo.FieldType.GetDecimal() != tidbTypes.MaxFsp || notNull {
handleError(errors.Errorf(
"dispatcher %s table %s.%s invalid column %s, expect TIMESTAMP(6) NULL, got type %d fsp %d notNull %t",
event.DispatcherID.String(),
tableInfo.GetSchemaName(),
tableInfo.GetTableName(),
SoftDeleteTimeColumn,
colInfo.GetType(),
colInfo.FieldType.GetDecimal(),
notNull,
))
return 0, false
}
return offset, true
}
// getSoftDeleteTimeColumnOffset returns the column offset for `_tidb_softdelete_time` without validating
// its type semantics. It reports schema issues via `handleError` and returns ok=false in that case.
func getSoftDeleteTimeColumnOffset(event *DMLEvent, handleError func(error)) (idx int, ok bool) {
tableInfo := event.TableInfo
if _, ok := tableInfo.GetColumnInfoByName(SoftDeleteTimeColumn); !ok {
handleError(errors.Errorf(
"dispatcher %s table %s.%s missing required column %s",
event.DispatcherID.String(),
tableInfo.GetSchemaName(),
tableInfo.GetTableName(),
SoftDeleteTimeColumn,
))
return 0, false
}
offset, ok := tableInfo.GetColumnOffsetByName(SoftDeleteTimeColumn)
if !ok {
handleError(errors.Errorf(
"dispatcher %s table %s.%s missing required column offset %s",
event.DispatcherID.String(),
tableInfo.GetSchemaName(),
tableInfo.GetTableName(),
SoftDeleteTimeColumn,
))
return 0, false
}
return offset, true
}
// ApplyRowPolicyDecision mutates the row based on decision.
func ApplyRowPolicyDecision(row *RowChange, decision RowPolicyDecision) {
switch decision {
case RowPolicyConvertToDelete:
row.RowType = common.RowTypeDelete
row.Row = chunk.Row{}
// When converting an update to a delete, only the pre-image is retained. Clear the
// current checksum so downstream verification does not attempt to checksum an empty row.
if row.Checksum != nil {
row.Checksum.Current = 0
}
}
}
// FilterDMLEvent applies row policy decisions to each row in the DMLEvent.
// - Normal tables: the original event is returned as-is.
// - Active-active tables: delete rows are removed in both modes. When enable-active-active is
// false, updates that flip `_tidb_softdelete_time` from NULL to non-NULL are converted into delete
// events. When enable-active-active is true, inserts/updates pass through so downstream LWW SQL can
// resolve conflicts.
// - Soft-delete tables: delete rows are removed. When enable-active-active is false, updates that
// flip `_tidb_softdelete_time` from NULL to non-NULL are converted into deletes.
//
// `handleError` is used to report unexpected schema issues and is expected to stop the
// owning dispatcher. When it is invoked, the returned event must be treated as dropped.
func FilterDMLEvent(event *DMLEvent, enableActiveActive bool, handleError func(error)) (*DMLEvent, bool) {
if event == nil {
return nil, true
}
tableInfo := event.TableInfo
if tableInfo == nil || event.Rows == nil {
return event, false
}
isActiveActive := tableInfo.IsActiveActiveTable()
isSoftDelete := tableInfo.IsSoftDeleteTable()
if !isActiveActive && !isSoftDelete {
return event, false
}
// FilterDMLEvent iterates rows via GetNextRow. Always start from the beginning to
// keep the filtering result independent of the caller's current offset.
event.Rewind()
var (
softDeleteTimeColIndex int
hasSoftDeleteTimeCol bool
)
// `_tidb_softdelete_time` metadata is required for:
// - active-active tables: for logging hard deletes (enable-active-active) and converting
// soft-delete transitions (disabled).
// - soft-delete tables: for converting soft-delete transitions when enable-active-active is disabled.
needSoftDeleteTimeCol := isActiveActive || (!enableActiveActive && isSoftDelete)
if needSoftDeleteTimeCol {
var (
offset int
ok bool
)
offset, ok = getSoftDeleteTimeColumnOffset(event, handleError)
if !ok {
return nil, true
}
softDeleteTimeColIndex = offset
hasSoftDeleteTimeCol = true
}
rowTypeSummary := summarizeRowTypes(event.RowTypes)
if filtered, skip, ok := filterDMLEventFastPath(
event,
tableInfo,
enableActiveActive,
softDeleteTimeColIndex,
rowTypeSummary,
); ok {
return filtered, skip
}
return filterDMLEventSlowPath(
event,
tableInfo,
enableActiveActive,
softDeleteTimeColIndex,
hasSoftDeleteTimeCol,
)
}
// filterDMLEventSlowPath rebuilds the event by applying the row policy decisions to each row.
// The caller must ensure the input event has been rewound.
func filterDMLEventSlowPath(
event *DMLEvent,
tableInfo *common.TableInfo,
enableActiveActive bool,
softDeleteTimeColIndex int,
hasSoftDeleteTimeCol bool,
) (*DMLEvent, bool) {
fieldTypes := tableInfo.GetFieldSlice()
if fieldTypes == nil {
return event, false
}
newChunk := chunk.NewChunkWithCapacity(fieldTypes, len(event.RowTypes))
rowTypes := make([]common.RowType, 0, len(event.RowTypes))
hasRowKeys := len(event.RowKeys) != 0
var rowKeys [][]byte
if hasRowKeys {
rowKeys = make([][]byte, 0, len(event.RowTypes))
}
hasChecksum := len(event.Checksum) != 0
var checksums []*integrity.Checksum
if hasChecksum {
checksums = make([]*integrity.Checksum, 0, len(event.Checksum))
}
filtered := false
kept := 0
for {
row, ok := event.GetNextRow()
if !ok {
break
}
decision := EvaluateRowPolicy(tableInfo, &row, enableActiveActive, softDeleteTimeColIndex)
switch decision {
case RowPolicySkip:
if enableActiveActive && row.RowType == common.RowTypeDelete && hasSoftDeleteTimeCol && !row.PreRow.IsEmpty() && softDeleteTimeColIndex < row.PreRow.Len() {
// For active-active tables, TiCDC expects user-managed hard deletes to keep
// `_tidb_softdelete_time` as NULL, so we log it for observability.
if row.PreRow.IsNull(softDeleteTimeColIndex) {
log.Info("received hard delete row",
zap.Stringer("dispatcherID", event.DispatcherID),
zap.Int64("tableID", tableInfo.TableName.TableID),
zap.Uint64("commitTs", uint64(event.CommitTs)))
}
}
filtered = true
continue
case RowPolicyConvertToDelete:
ApplyRowPolicyDecision(&row, decision)
filtered = true
default:
}
appendRowChangeToChunk(newChunk, &row)
// RowTypes/RowKeys are indexed by physical row slots in `DMLEvent.Rows`.
// An update occupies two slots (pre and post), so we need to append
// its type/key twice to keep `GetNextRow()` semantics intact.
physicalSlots := 1
if row.RowType == common.RowTypeUpdate {
physicalSlots = 2
}
for i := 0; i < physicalSlots; i++ {
rowTypes = append(rowTypes, row.RowType)
}
if hasRowKeys {
rowKey := row.RowKey
if len(rowKey) == 0 {
rowKey = nil
}
for i := 0; i < physicalSlots; i++ {
rowKeys = append(rowKeys, rowKey)
}
}
if hasChecksum {
checksums = append(checksums, row.Checksum)
}
kept++
}
event.Rewind()
if !filtered {
return event, false
}
if kept == 0 {
return nil, true
}
return newFilteredDMLEvent(event, newChunk, rowTypes, rowKeys, checksums, kept), false
}
// filterDMLEventFastPath returns early when the row policy would not change the event,
// or when every row is dropped. It keeps the logic aligned with EvaluateRowPolicy to
// avoid diverging behavior between fast and slow paths.
//
// The input event must have been rewound. On return, the event is guaranteed to be
// rewound as well.
func filterDMLEventFastPath(
event *DMLEvent,
tableInfo *common.TableInfo,
enableActiveActive bool,
softDeleteTimeColIndex int,
rowTypeSummary rowTypeSummary,
) (*DMLEvent, bool, bool) {
if event == nil {
return nil, true, true
}
// enable-active-active only removes delete rows. When there is no delete row, return
// the original event directly.
if enableActiveActive && !rowTypeSummary.hasDelete {
return event, false, true
}
// When enable-active-active is false, delete rows are always dropped and soft-delete
// transitions are rewritten into deletes.
//
// If the event contains only delete rows, drop it without iterating the underlying
// chunk. When there is no delete row, scan for the first row that would be mutated
// by EvaluateRowPolicy and return the original event if none exists.
if !enableActiveActive {
if rowTypeSummary.allDelete {
return nil, true, true
}
if !rowTypeSummary.hasDelete {
if !rowTypeSummary.hasUpdate {
return event, false, true
}
for {
row, ok := event.GetNextRow()
if !ok {
break
}
if EvaluateRowPolicy(tableInfo, &row, enableActiveActive, softDeleteTimeColIndex) != RowPolicyKeep {
event.Rewind()
return nil, false, false
}
}
event.Rewind()
return event, false, true
}
}
return nil, false, false
}
// newFilteredDMLEvent builds a new DMLEvent from the filtered rows while preserving
// dispatcher-managed metadata from the source event.
func newFilteredDMLEvent(
source *DMLEvent,
rows *chunk.Chunk,
rowTypes []common.RowType,
rowKeys [][]byte,
checksums []*integrity.Checksum,
kept int,
) *DMLEvent {
newEvent := NewDMLEvent(source.DispatcherID, source.PhysicalTableID, source.StartTs, source.CommitTs, source.TableInfo)
newEvent.TableInfoVersion = source.TableInfoVersion
newEvent.Seq = source.Seq
newEvent.Epoch = source.Epoch
newEvent.ReplicatingTs = source.ReplicatingTs
newEvent.PostTxnEnqueued = source.PostTxnEnqueued
newEvent.PostTxnFlushed = source.PostTxnFlushed
newEvent.postEnqueueCalled = atomic.LoadUint32(&source.postEnqueueCalled)
source.PostTxnEnqueued = nil
source.PostTxnFlushed = nil
newEvent.SetRows(rows)
newEvent.RowTypes = rowTypes
newEvent.RowKeys = rowKeys
newEvent.Checksum = checksums
newEvent.Length = int32(kept)
newEvent.PreviousTotalOffset = 0
if source.Len() == 0 {
newEvent.ApproximateSize = 0
} else {
newEvent.ApproximateSize = source.ApproximateSize * int64(kept) / int64(source.Len())
}
return newEvent
}
func appendRowChangeToChunk(chk *chunk.Chunk, row *RowChange) {
if row == nil || chk == nil {
return
}
switch row.RowType {
case common.RowTypeInsert:
if !row.Row.IsEmpty() {
chk.AppendRow(row.Row)
}
case common.RowTypeDelete:
if !row.PreRow.IsEmpty() {
chk.AppendRow(row.PreRow)
}
case common.RowTypeUpdate:
if !row.PreRow.IsEmpty() {
chk.AppendRow(row.PreRow)
}
if !row.Row.IsEmpty() {
chk.AppendRow(row.Row)
}
}
}
type rowTypeSummary struct {
hasDelete bool
hasUpdate bool
allDelete bool
}
func summarizeRowTypes(rowTypes []common.RowType) rowTypeSummary {
summary := rowTypeSummary{
allDelete: len(rowTypes) != 0,
}
for _, rowType := range rowTypes {
switch rowType {
case common.RowTypeDelete:
summary.hasDelete = true
case common.RowTypeUpdate:
summary.hasUpdate = true
summary.allDelete = false
default:
summary.allDelete = false
}
}
return summary
}