Skip to content

Commit 4614ee2

Browse files
committed
fix more code
1 parent 097781c commit 4614ee2

3 files changed

Lines changed: 96 additions & 24 deletions

File tree

‎cmd/consumer/conductor/conductor.go‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -368,28 +368,13 @@ func (c *Conductor) queueDDL(ddlEvent *event.DDLEvent) {
368368
func (c *Conductor) nextReady() (*event.DDLEvent, *dmlTask) {
369369
if len(c.pendingDDL) != 0 {
370370
head := c.pendingDDL[0]
371-
if c.ddlReady(head) && !c.hasRowBefore(0, min(c.effectiveWatermark(), head.GetCommitTs()), head) {
371+
if head.GetCommitTs() <= c.effectiveWatermark() && !c.hasRowBefore(0, head.GetCommitTs(), head) {
372372
return head, nil
373373
}
374374
}
375375
return nil, c.nextDML()
376376
}
377377

378-
// ddlReady reports whether every row before the DDL has been read.
379-
func (c *Conductor) ddlReady(ddl *event.DDLEvent) bool {
380-
if ddl.GetCommitTs() <= c.effectiveWatermark() {
381-
return true
382-
}
383-
// Creating a schema or standalone table cannot depend on earlier rows.
384-
switch model.ActionType(ddl.Type) {
385-
case model.ActionCreateSchema:
386-
return true
387-
case model.ActionCreateTable:
388-
return len(ddl.BlockedTableNames) == 0
389-
}
390-
return false
391-
}
392-
393378
func (c *Conductor) hasRowBefore(index int, limit uint64, block *event.DDLEvent) bool {
394379
if index >= len(c.readyHeap) {
395380
return false
@@ -711,8 +696,12 @@ func ddlProcessedTables(ddlEvent *event.DDLEvent) []event.SchemaTableName {
711696
}
712697
add(ddlEvent.SchemaName, ddlEvent.TableName)
713698
add(ddlEvent.ExtraSchemaName, ddlEvent.ExtraTableName)
714-
for _, name := range ddlEvent.BlockedTableNames {
715-
add(name.SchemaName, name.TableName)
699+
// CREATE TABLE ... LIKE ... only fences the referenced table at the DDL
700+
// barrier; it does not bump the source table's Storage TableVersion.
701+
if model.ActionType(ddlEvent.Type) != model.ActionCreateTable {
702+
for _, name := range ddlEvent.BlockedTableNames {
703+
add(name.SchemaName, name.TableName)
704+
}
716705
}
717706
for _, info := range ddlEvent.MultipleTableInfos {
718707
if info != nil {

‎cmd/consumer/conductor/conductor_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ func (s *stubReader) Read(context.Context) ([]*source.Payload, error) { return n
4242
func (s *stubReader) Partitions() int32 { return s.partitions }
4343
func (s *stubReader) Close() {}
4444

45+
type stubSchemaNotifierReader struct {
46+
stubReader
47+
processed []struct {
48+
tables []commonEvent.SchemaTableName
49+
timestamp uint64
50+
}
51+
}
52+
53+
func (s *stubSchemaNotifierReader) MarkProcessed(tables []commonEvent.SchemaTableName, timestamp uint64) {
54+
s.processed = append(s.processed, struct {
55+
tables []commonEvent.SchemaTableName
56+
timestamp uint64
57+
}{
58+
tables: tables,
59+
timestamp: timestamp,
60+
})
61+
}
62+
4563
func TestConductorFlushDDLWithoutDML(t *testing.T) {
4664
c := New(&stubReader{partitions: 0}, codecCommon.NewTableInfoProvider())
4765
w := writer.New(t.Context(), writer.Options{
@@ -65,6 +83,10 @@ func TestConductorFlushDDLWithoutDML(t *testing.T) {
6583
require.NoError(t, c.accept(ddl, &source.Payload{Partition: 0}))
6684
}
6785

86+
require.NoError(t, c.flush(t.Context(), w))
87+
require.Empty(t, flushed)
88+
89+
c.advanceWatermark(101, 0)
6890
require.NoError(t, c.flush(t.Context(), w))
6991
require.Equal(t, []string{
7092
"CREATE TABLE test.t1 (id INT PRIMARY KEY)",
@@ -426,3 +448,64 @@ func TestConductorBlockedTableReentersReadyHeapAndCreateTableLike(t *testing.T)
426448
require.NoError(t, c.flush(t.Context(), w))
427449
require.Equal(t, []string{"t1:dml@140", "t1:ddl@150", "t1:dml@160", "t2:create_like@200"}, flushed)
428450
}
451+
452+
func TestConductorCreateTableLikeMarksOnlyCreatedTableProcessed(t *testing.T) {
453+
reader := &stubSchemaNotifierReader{}
454+
c := New(reader, codecCommon.NewTableInfoProvider())
455+
456+
createLike := &commonEvent.DDLEvent{
457+
FinishedTs: 200,
458+
Type: byte(timodel.ActionCreateTable),
459+
SchemaName: "test",
460+
TableName: "t2",
461+
Query: "CREATE TABLE test.t2 LIKE test.t1",
462+
}
463+
require.NoError(t, c.accept(createLike, &source.Payload{}))
464+
require.Len(t, reader.processed, 1)
465+
require.Equal(t, uint64(200), reader.processed[0].timestamp)
466+
require.Equal(t, []commonEvent.SchemaTableName{{SchemaName: "test", TableName: "t2"}}, reader.processed[0].tables)
467+
require.Equal(t, []commonEvent.SchemaTableName{
468+
{SchemaName: "test", TableName: "t2"},
469+
{SchemaName: "test", TableName: "t1"},
470+
}, createLike.BlockedTableNames)
471+
}
472+
473+
func TestConductorWaitsWatermarkForOutOfOrderCreateDDLs(t *testing.T) {
474+
c := New(&stubReader{partitions: 2}, codecCommon.NewTableInfoProvider())
475+
w := writer.New(t.Context(), writer.Options{
476+
DownstreamURI: "blackhole://",
477+
ReplicaConfig: config.GetDefaultReplicaConfig(),
478+
})
479+
480+
var flushed []string
481+
createTable := &commonEvent.DDLEvent{
482+
FinishedTs: 120,
483+
Type: byte(timodel.ActionCreateTable),
484+
SchemaName: "test",
485+
TableName: "t",
486+
Query: "CREATE TABLE test.t (id INT PRIMARY KEY)",
487+
}
488+
createTable.AddPostFlushFunc(func() {
489+
flushed = append(flushed, "create_table@120")
490+
})
491+
createDB := &commonEvent.DDLEvent{
492+
FinishedTs: 100,
493+
Type: byte(timodel.ActionCreateSchema),
494+
SchemaName: "test",
495+
Query: "CREATE DATABASE test",
496+
}
497+
createDB.AddPostFlushFunc(func() {
498+
flushed = append(flushed, "create_db@100")
499+
})
500+
501+
// Partition 0 delivers CREATE TABLE@120 before partition 1 delivers CREATE DATABASE@100.
502+
require.NoError(t, c.accept(createTable, &source.Payload{Partition: 0}))
503+
c.advanceWatermark(120, 0)
504+
require.NoError(t, c.flush(t.Context(), w))
505+
require.Empty(t, flushed)
506+
507+
require.NoError(t, c.accept(createDB, &source.Payload{Partition: 1}))
508+
c.advanceWatermark(120, 1)
509+
require.NoError(t, c.flush(t.Context(), w))
510+
require.Equal(t, []string{"create_db@100", "create_table@120"}, flushed)
511+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The Consumer connects four stages in sequence: `source` -> `decoder group` -> `c
5050
- **`source`**: Hides data source details; validates sharding consistency when routing rules apply; triggers prompt Cloud Storage file refreshes during `DDL Barrier` waits; and commits consumption progress asynchronously.
5151
- **`decoder group`**: Decodes normalized payloads concurrently without coupling to source partition layout while preserving per-sequence submission order. Schema-only metadata messages without a `commit-ts` (such as Simple protocol `Bootstrap`) are digested inside `decoder group` to populate the shared schema cache and wake pending DMLs, without forwarding downstream events or entering `DDL Barrier`, while their source offsets still advance normally.
5252
- **`conductor`**: Aggregates the global Source Watermark $W$ and effective watermark $W_{\text{eff}}$, enforces `DDL Barrier` and `commit-ts` <= $W_{\text{eff}}$ release gating, preserves per-row order under concurrent dispatch, coordinates schema cache GC, and drives `source` commits as $C$ advances.
53-
- **`writer`**: Filters restart replays, delegates DML batching and asynchronous execution to the downstream DB sink, and advances the Persisted Checkpoint $C$.
53+
- **`writer`**: Filters duplicate replays, delegates DML batching and asynchronous execution to the downstream DB sink, and advances the Persisted Checkpoint $C$.
5454

5555
All supported sources normalize into the same abstraction model, and every downstream event forwarded to `conductor` (`DML`, `DDL`, and `Watermark`) carries a valid `commit-ts` so downstream gating and deduplication operate identically across sources:
5656

@@ -89,21 +89,21 @@ To confirm external progress safely under concurrent or out-of-order downstream
8989
- **Unified `commit-ts` <= $W_{\text{eff}}$ gate**: DML events become eligible for downstream dispatch only when `commit-ts` <= $W_{\text{eff}}$, ensuring all partitions or files up to that timestamp have been observed and decoded before row changes execute.
9090
- **Concurrent execution with row-level order**: Once `commit-ts` <= $W_{\text{eff}}$ holds, `conductor` does not serialize an entire table strictly by `commit-ts`. Instead, it dispatches independent rows and tables concurrently to persist data early, while preserving single-row execution order when related changes arrive across partitions or files.
9191
- **DDL Barrier readiness**: A DDL statement waits until both conditions hold before execution:
92-
1. **Watermark readiness**: $W_{\text{eff}} \ge \text{ddl.commit-ts}$, guaranteeing no slower partition or file still holds an earlier or pending-decode DML targeting the affected tables (standalone `CREATE DATABASE` and `CREATE TABLE` statements without source-table dependencies may bypass this wait).
92+
1. **Watermark readiness**: $W_{\text{eff}} \ge \text{ddl.commit-ts}$, guaranteeing no slower partition or file still holds an earlier or pending-decode DML targeting the affected tables.
9393
2. **Preceding DML persistence**: All previously dispatched DML events for affected tables finish persisting downstream.
9494
While a DDL executes, subsequent DML events on affected tables are held back; unrelated tables continue progressing concurrently.
9595

9696
---
9797

98-
### Schema Lifecycle and Fault-Tolerant Replay
98+
### Schema Lifecycle and Deduplication
9999

100100
- **Shared multi-version schema cache**: Decoders share a versioned `TableInfoProvider` so schema updates from `DDL` and `Bootstrap` messages are visible across all concurrent decoders. As $W_{\text{eff}}$ advances, `conductor` notifies the cache to reclaim expired schema versions below $W_{\text{eff}}$. Before querying historical schemas or parking a DML in the waiting table, decoders skip stale replay events whose `commit-ts` is already covered by the reclaimed watermark, filtering directly on `commit-ts` without requiring table schemas.
101101
- **Early broadcast DDL deduplication and ordering**: Because sources may broadcast identical DDLs across partitions, every DDL or `Bootstrap` message processed at the `decoder group` boundary follows a strict three-step order:
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 and at-least-once recovery**: Unrecoverable decoding errors or downstream write failures abort the pipeline immediately without advancing $C$ or committing unpersisted offsets. On restart, `source` replays from the last committed offset.
106-
- **Three-layer replay deduplication and sink batching**: `writer` passes ready DML events directly to the downstream DB sink for native batching and filters restart replays 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 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:
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 restartability**: Bounding $C$ by downstream persistence callbacks and pairing offset commits with `writer` deduplication provides clean at-least-once replay idempotency.
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.

0 commit comments

Comments
 (0)