Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] support ddl for kafka #464

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions downstreamadapter/sink/helper/eventrouter/event_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,20 @@

// GetTopicForDDL returns the target topic for DDL.
func (s *EventRouter) GetTopicForDDL(ddl *commonEvent.DDLEvent) string {
schema := ddl.SchemaName
table := ddl.TableName

// TODO: fix this
//var schema, table string
// if ddl.PreTableInfo != nil {
// if ddl.PreTableInfo.TableName.Table == "" {
// return s.defaultTopic
// }
// schema = ddl.PreTableInfo.TableName.Schema
// table = ddl.PreTableInfo.TableName.Table
// } else {
// if ddl.TableInfo.TableName.Table == "" {
// return s.defaultTopic
// }
// schema = ddl.TableInfo.TableName.Schema
// table = ddl.TableInfo.TableName.Table
// }
var schema, table string
preTableInfo := ddl.GetPreTableInfo()

Check failure on line 86 in downstreamadapter/sink/helper/eventrouter/event_router.go

View workflow job for this annotation

GitHub Actions / Linux Build

ddl.GetPreTableInfo undefined (type *event.DDLEvent has no field or method GetPreTableInfo)

Check failure on line 86 in downstreamadapter/sink/helper/eventrouter/event_router.go

View workflow job for this annotation

GitHub Actions / Mac OS Build

ddl.GetPreTableInfo undefined (type *event.DDLEvent has no field or method GetPreTableInfo)
if preTableInfo != nil {
schema, table = preTableInfo.GetSchemaName(), preTableInfo.GetTableName()
if table == "" {
return s.defaultTopic
}
} else {
tableInfo := ddl.GetTableInfo()

Check failure on line 93 in downstreamadapter/sink/helper/eventrouter/event_router.go

View workflow job for this annotation

GitHub Actions / Linux Build

ddl.GetTableInfo undefined (type *event.DDLEvent has no field or method GetTableInfo)

Check failure on line 93 in downstreamadapter/sink/helper/eventrouter/event_router.go

View workflow job for this annotation

GitHub Actions / Mac OS Build

ddl.GetTableInfo undefined (type *event.DDLEvent has no field or method GetTableInfo)
schema, table = tableInfo.GetSchemaName(), tableInfo.GetTableName()
if table == "" {
return s.defaultTopic
}
}

topicGenerator := s.matchTopicGenerator(schema, table)
return topicGenerator.Substitute(schema, table)
Expand Down
80 changes: 54 additions & 26 deletions downstreamadapter/worker/kafka_ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pingcap/ticdc/pkg/sink/codec/encoder"
"github.com/pingcap/ticdc/pkg/sink/util"
"github.com/pingcap/tiflow/cdc/sink/ddlsink/mq/ddlproducer"
ticommon "github.com/pingcap/tiflow/pkg/sink/codec/common"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -126,38 +127,65 @@ func (w *KafkaDDLWorker) encodeAndSendDDLEvents() error {
zap.String("changefeed", w.changeFeedID.Name()))
return nil
}
message, err := w.encoder.EncodeDDLEvent(event)
if err != nil {
log.Error("Failed to encode ddl event",
zap.String("namespace", w.changeFeedID.Namespace()),
zap.String("changefeed", w.changeFeedID.Name()),
zap.Error(err))
continue
}

topic := w.eventRouter.GetTopicForDDL(event)
partitionNum, err := w.topicManager.GetPartitionNum(w.ctx, topic)
if err != nil {
log.Error("failed to get partition number for topic", zap.String("topic", topic), zap.Error(err))
continue
messages := make([]*ticommon.Message, 0)
topics := make([]string, 0)
// Some ddl event may be multi-events, we need to split it into multiple messages.
// Such as rename table test.table1 to test.table10, test.table2 to test.table20
if event.IsMultiEvents() {
subEvents := event.GetSubEvents()
for _, subEvent := range subEvents {
message, err := w.encoder.EncodeDDLEvent(subEvent)
if err != nil {
log.Error("Failed to encode ddl event",
zap.String("namespace", w.changeFeedID.Namespace()),
zap.String("changefeed", w.changeFeedID.Name()),
zap.Error(err))
continue
}
messages = append(messages, message)
topics = append(topics, w.eventRouter.GetTopicForDDL(subEvent))
}
} else {
message, err := w.encoder.EncodeDDLEvent(event)
if err != nil {
log.Error("Failed to encode ddl event",
zap.String("namespace", w.changeFeedID.Namespace()),
zap.String("changefeed", w.changeFeedID.Name()),
zap.Error(err))
continue
}
messages = append(messages, message)
topic := w.eventRouter.GetTopicForDDL(event)
topics = append(topics, topic)
}

if w.partitionRule == PartitionAll {
for i, message := range messages {
topic := topics[i]

partitionNum, err := w.topicManager.GetPartitionNum(w.ctx, topic)
if err != nil {
log.Error("failed to get partition number for topic", zap.String("topic", topic), zap.Error(err))
continue
}

if w.partitionRule == PartitionAll {
err = w.statistics.RecordDDLExecution(func() error {
return w.producer.SyncBroadcastMessage(w.ctx, topic, partitionNum, message)
})
return errors.Trace(err)
}
err = w.statistics.RecordDDLExecution(func() error {
return w.producer.SyncBroadcastMessage(w.ctx, topic, partitionNum, message)
return w.producer.SyncSendMessage(w.ctx, topic, 0, message)
})
return errors.Trace(err)
}
err = w.statistics.RecordDDLExecution(func() error {
return w.producer.SyncSendMessage(w.ctx, topic, 0, message)
})

if err != nil {
log.Error("Failed to RecordDDLExecution",
zap.String("namespace", w.changeFeedID.Namespace()),
zap.String("changefeed", w.changeFeedID.Name()),
zap.Error(err))
continue
if err != nil {
log.Error("Failed to RecordDDLExecution",
zap.String("namespace", w.changeFeedID.Namespace()),
zap.String("changefeed", w.changeFeedID.Name()),
zap.Error(err))
continue
}
}

}
Expand Down
Loading