Skip to content

Commit d71aee1

Browse files
committed
fix consumer
1 parent 206c8d4 commit d71aee1

2 files changed

Lines changed: 80 additions & 62 deletions

File tree

‎cmd/consumer/source/storage.go‎

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ func (c *storageReader) discoverDir(
262262
return nil
263263
}
264264

265+
// addIndex records any new DML file range referenced by indexPath.
266+
// It reports whether indexPath matched a valid TiCDC DML index path (including
267+
// versions skipped by checkpoint or watermark) so discoverDir can stop descending.
265268
func (c *storageReader) addIndex(
266269
ctx context.Context, indexPath string,
267270
files map[cloudstorage.DMLPathKey]map[cloudstorage.FileIndexKey]storageIndexRange,
@@ -405,68 +408,70 @@ func (c *storageReader) readSchema(ctx context.Context, path string) (cloudstora
405408
}
406409

407410
func (c *storageReader) Read(ctx context.Context) ([]*Payload, error) {
408-
if err := context.Cause(ctx); err != nil {
409-
return nil, err
410-
}
411-
if len(c.ranges) == 0 {
412-
if c.scanned && c.sourceCheckpoint > c.watermark {
413-
c.watermark = c.sourceCheckpoint
414-
// The metadata checkpoint covers every file in the completed scan.
415-
return []*Payload{{Watermark: c.sourceCheckpoint}}, nil
416-
}
417-
if err := c.scanInputs(ctx); err != nil {
411+
for {
412+
if err := context.Cause(ctx); err != nil {
418413
return nil, err
419414
}
420-
if len(c.ranges) == 0 && c.sourceCheckpoint > c.watermark {
421-
c.watermark = c.sourceCheckpoint
422-
return []*Payload{{Watermark: c.sourceCheckpoint}}, nil
415+
if len(c.ranges) == 0 {
416+
if c.scanned && c.sourceCheckpoint > c.watermark {
417+
c.watermark = c.sourceCheckpoint
418+
// The metadata checkpoint covers every file in the completed scan.
419+
return []*Payload{{Watermark: c.sourceCheckpoint}}, nil
420+
}
421+
if err := c.scanInputs(ctx); err != nil {
422+
return nil, err
423+
}
424+
if len(c.ranges) == 0 && c.sourceCheckpoint > c.watermark {
425+
c.watermark = c.sourceCheckpoint
426+
return []*Payload{{Watermark: c.sourceCheckpoint}}, nil
427+
}
428+
continue
423429
}
424-
return nil, nil
425-
}
426-
item := c.ranges[0]
427-
key := item.key
428-
schema := c.schemas[key.SchemaPathKey]
429-
ddlWatermark := c.schemaWatermark(key.SchemaPathKey, schema)
430-
switch {
431-
case item.index.Idx < item.end && key.TableVersion >= ddlWatermark:
432-
c.ranges[0].index.Idx++
433-
default:
434-
c.ranges[0] = storageRange{}
435-
c.ranges = c.ranges[1:]
436-
}
437-
if schema == nil {
438-
log.Panic("storage DML file has no matching schema", zap.String("schema", key.Schema), zap.String("table", key.Table), zap.Uint64("tableVersion", key.TableVersion))
439-
}
440-
if key.IsSchemaFileDMLPathKey() && (schema.Query == "" || key.TableVersion <= ddlWatermark) {
441-
return nil, nil
442-
}
443-
if key.TableVersion < ddlWatermark {
444-
c.fileIndices[key][item.index.FileIndexKey] = max(item.index.Idx, item.end)
445-
return nil, nil
446-
}
447-
if key.IsSchemaFileDMLPathKey() {
448-
return []*Payload{{File: &File{Schema: schema}}}, nil
449-
}
450-
schemaName, tableName := key.Schema, key.Table
451-
if schema.Schema != "" {
452-
schemaName, tableName = schema.Schema, schema.Table
453-
}
454-
idKey := storageTableKey{
455-
schema: schemaName,
456-
table: tableName,
457-
partition: key.PartitionNum,
458-
}
459-
tableID := c.tableIDs[idKey]
460-
if tableID == 0 {
461-
c.nextTableID++
462-
tableID = c.nextTableID
463-
c.tableIDs[idKey] = tableID
464-
}
465-
data, err := c.readDMLFile(ctx, item, schema, tableID)
466-
if err != nil {
467-
return nil, err
430+
item := c.ranges[0]
431+
key := item.key
432+
schema := c.schemas[key.SchemaPathKey]
433+
ddlWatermark := c.schemaWatermark(key.SchemaPathKey, schema)
434+
switch {
435+
case item.index.Idx < item.end && key.TableVersion >= ddlWatermark:
436+
c.ranges[0].index.Idx++
437+
default:
438+
c.ranges[0] = storageRange{}
439+
c.ranges = c.ranges[1:]
440+
}
441+
if schema == nil {
442+
log.Panic("storage DML file has no matching schema", zap.String("schema", key.Schema), zap.String("table", key.Table), zap.Uint64("tableVersion", key.TableVersion))
443+
}
444+
if key.IsSchemaFileDMLPathKey() && (schema.Query == "" || key.TableVersion <= ddlWatermark) {
445+
continue
446+
}
447+
if key.TableVersion < ddlWatermark {
448+
c.fileIndices[key][item.index.FileIndexKey] = max(item.index.Idx, item.end)
449+
continue
450+
}
451+
if key.IsSchemaFileDMLPathKey() {
452+
return []*Payload{{File: &File{Schema: schema}}}, nil
453+
}
454+
schemaName, tableName := key.Schema, key.Table
455+
if schema.Schema != "" {
456+
schemaName, tableName = schema.Schema, schema.Table
457+
}
458+
idKey := storageTableKey{
459+
schema: schemaName,
460+
table: tableName,
461+
partition: key.PartitionNum,
462+
}
463+
tableID := c.tableIDs[idKey]
464+
if tableID == 0 {
465+
c.nextTableID++
466+
tableID = c.nextTableID
467+
c.tableIDs[idKey] = tableID
468+
}
469+
data, err := c.readDMLFile(ctx, item, schema, tableID)
470+
if err != nil {
471+
return nil, err
472+
}
473+
return []*Payload{data}, nil
468474
}
469-
return []*Payload{data}, nil
470475
}
471476

472477
func (c *storageReader) scanInputs(ctx context.Context) error {
@@ -503,7 +508,20 @@ func (c *storageReader) scanInputs(ctx context.Context) error {
503508
c.ranges = append(c.ranges, storageRange{key: key})
504509
continue
505510
}
506-
for indexKey, span := range files[key] {
511+
indexKeys := slices.SortedFunc(maps.Keys(files[key]), func(a, b cloudstorage.FileIndexKey) int {
512+
if c := cmp.Compare(a.DispatcherID, b.DispatcherID); c != 0 {
513+
return c
514+
}
515+
if a.EnableTableAcrossNodes == b.EnableTableAcrossNodes {
516+
return 0
517+
}
518+
if a.EnableTableAcrossNodes {
519+
return 1
520+
}
521+
return -1
522+
})
523+
for _, indexKey := range indexKeys {
524+
span := files[key][indexKey]
507525
c.ranges = append(c.ranges, storageRange{
508526
key: key,
509527
index: cloudstorage.FileIndex{

‎docs/design/2026-11-25-ticdc-consumer-design.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ To confirm external progress safely under concurrent or out-of-order downstream
102102
1. Decode and update the shared schema cache;
103103
2. Synchronously wake and decode all pending DMLs waiting on that schema across any partition;
104104
3. Drop schema-only metadata (`Bootstrap`) and deduplicate broadcast DDLs by DDL identity so the first-arriving partition forwards the DDL to `conductor` while duplicate copies on other partitions are dropped (retaining the DDL's `commit-ts` as each partition's marker so offsets are not acknowledged before the DDL persists).
105-
- **Fail-fast error handling**: Unrecoverable decoding errors or downstream write failures abort the pipeline immediately without advancing $C$ or committing unpersisted offsets; as a test and verification tool, the Consumer does not support cross-restart recovery.
106-
- **Three-layer in-process replay deduplication and sink batching**: `writer` passes ready DML events directly to the downstream DB sink for native batching and filters duplicate deliveries (such as MQ redelivery or Storage rescans) via three checks:
105+
- **Fail-fast error handling**: Unrecoverable decoding errors or downstream write failures abort the pipeline immediately without advancing $C$ or committing unpersisted offsets; as a test and verification tool, the Consumer itself does not support cross-restart recovery.
106+
- **Three-layer replay deduplication and sink batching**: `writer` passes ready DML events directly to the downstream DB sink for native batching and filters duplicate events via three checks:
107107
1. Drops events with `commit-ts <= C` as already durable;
108108
2. Tracks written Handle keys in the `commit-ts > C` window to drop duplicates, reclaiming keys with `commit-ts <= C` once $C$ advances;
109109
3. Retains an in-memory `(commit-ts, schema, table, query)` set for executed DDLs so non-idempotent DDLs never re-execute across replay windows.
@@ -114,4 +114,4 @@ To confirm external progress safely under concurrent or out-of-order downstream
114114

115115
- **Source-agnostic correctness**: Unifying Kafka, Pulsar, and Cloud Storage behind one `commit-ts` <= $W_{\text{eff}}$ gate, `DDL Barrier`, and $C$-driven acknowledgement model eliminates per-source consistency gaps.
116116
- **High throughput without sacrificing safety**: Concurrent decoding and cross-table/cross-row dispatch avoid single-partition or whole-table bottlenecks while keeping per-row order and `DDL Barrier` invariants intact.
117-
- **Safe in-process deduplication**: Bounding $C$ by downstream persistence callbacks and pairing offset commits with `writer` deduplication handles redeliveries and rescans cleanly within a run.
117+
- **Safe deduplication**: Bounding $C$ by downstream persistence callbacks and pairing offset commits with `writer` deduplication handles duplicate events cleanly within a Consumer run.

0 commit comments

Comments
 (0)