Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions api/v2/changefeed_toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func TestChangeFeedInfoTOMLRoundTripToInternal(t *testing.T) {
DebeziumConfig: &DebeziumConfig{
IncludeStartTs: util.AddressOf(true),
},
SimpleConfig: &SimpleConfig{
IncludeStartTs: util.AddressOf(true),
},
},
SyncPointInterval: &JSONDuration{duration: 10 * time.Minute},
Integrity: &IntegrityConfig{
Expand All @@ -120,6 +123,7 @@ func TestChangeFeedInfoTOMLRoundTripToInternal(t *testing.T) {
require.Contains(t, out, `sink-uri = "blackhole://"`)
require.Contains(t, out, "start-ts")
require.Contains(t, out, "[config.sink.debezium]")
require.Contains(t, out, "[config.sink.simple]")
require.Contains(t, out, "include-start-ts = true")
require.NotContains(t, out, "gid") // GID is omitted from TOML (toml:"-")
Comment on lines 123 to 131

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 97ad02d: the TOML round-trip no longer treats include-start-ts = true as a global substring. It now requires that string inside both [config.sink.simple] and [config.sink.debezium] via section-scoped regex, so a Debezium-only serialization cannot pass the Simple check.


Expand All @@ -139,6 +143,7 @@ func TestChangeFeedInfoTOMLRoundTripToInternal(t *testing.T) {
require.Equal(t, "correctness", util.GetOrZero(wrapper.Config.Integrity.IntegrityCheckLevel))
require.Equal(t, "eventual", util.GetOrZero(wrapper.Config.Consistent.Level))
require.True(t, util.GetOrZero(wrapper.Config.Sink.Debezium.IncludeStartTs))
require.True(t, util.GetOrZero(wrapper.Config.Sink.Simple.IncludeStartTs))
}

func TestCodecConfigTOMLRoundTripToInternal(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions api/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,12 @@ func (c *ReplicaConfig) toInternalReplicaConfigWithOriginConfig(
OutputOldValue: c.Sink.OpenProtocolConfig.OutputOldValue,
}
}
var simpleConfig *config.SimpleConfig
if c.Sink.SimpleConfig != nil && c.Sink.SimpleConfig.IncludeStartTs != nil {
simpleConfig = &config.SimpleConfig{
IncludeStartTs: util.AddressOf(*c.Sink.SimpleConfig.IncludeStartTs),
}
}

res.Sink = &config.SinkConfig{
DispatchRules: dispatchRules,
Expand All @@ -584,6 +590,7 @@ func (c *ReplicaConfig) toInternalReplicaConfigWithOriginConfig(
SafeMode: c.Sink.SafeMode,
OpenProtocol: openProtocolConfig,
Debezium: debeziumConfig,
Simple: simpleConfig,
}

if c.Sink.TxnAtomicity != nil {
Expand Down Expand Up @@ -932,6 +939,12 @@ func ToAPIReplicaConfig(c *config.ReplicaConfig) *ReplicaConfig {
OutputOldValue: cloned.Sink.OpenProtocol.OutputOldValue,
}
}
var simpleConfig *SimpleConfig
if cloned.Sink.Simple != nil && cloned.Sink.Simple.IncludeStartTs != nil {
simpleConfig = &SimpleConfig{
IncludeStartTs: util.AddressOf(*cloned.Sink.Simple.IncludeStartTs),
}
}
res.Sink = &SinkConfig{
Protocol: cloned.Sink.Protocol,
SchemaRegistry: cloned.Sink.SchemaRegistry,
Expand All @@ -953,6 +966,7 @@ func ToAPIReplicaConfig(c *config.ReplicaConfig) *ReplicaConfig {
SafeMode: cloned.Sink.SafeMode,
DebeziumConfig: debeziumConfig,
OpenProtocolConfig: openProtocolConfig,
SimpleConfig: simpleConfig,
}

if cloned.Sink.TxnAtomicity != nil {
Expand Down Expand Up @@ -1222,6 +1236,7 @@ type SinkConfig struct {
DebeziumDisableSchema *bool `json:"debezium_disable_schema,omitempty" toml:"debezium-disable-schema,omitempty"`
DebeziumConfig *DebeziumConfig `json:"debezium,omitempty" toml:"debezium,omitempty"`
OpenProtocolConfig *OpenProtocolConfig `json:"open,omitempty" toml:"open,omitempty"`
SimpleConfig *SimpleConfig `json:"simple,omitempty" toml:"simple,omitempty"`
}

// CSVConfig denotes the csv config
Expand Down Expand Up @@ -1666,6 +1681,11 @@ type DebeziumConfig struct {
IncludeStartTs *bool `json:"include_start_ts,omitempty" toml:"include-start-ts,omitempty"`
}

// SimpleConfig represents the configurations for simple protocol encoding
type SimpleConfig struct {
IncludeStartTs *bool `json:"include_start_ts,omitempty" toml:"include-start-ts,omitempty"`
}

type DispatcherCount struct {
Count int `json:"count"`
}
Expand Down
5 changes: 5 additions & 0 deletions api/v2/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func TestReplicaConfigConversion(t *testing.T) {
DebeziumConfig: &DebeziumConfig{
IncludeStartTs: util.AddressOf(true),
},
SimpleConfig: &SimpleConfig{
IncludeStartTs: util.AddressOf(true),
},
KafkaConfig: &KafkaConfig{
SASLOAuthCA: util.AddressOf("/etc/ssl/oauth-ca.pem"),
},
Expand Down Expand Up @@ -140,6 +143,7 @@ func TestReplicaConfigConversion(t *testing.T) {
require.Equal(t, int64(1024), util.GetOrZero(internalCfg.Sink.CloudStorageConfig.SpoolDiskQuota))
require.Equal(t, "/tmp/ticdc-spool", util.GetOrZero(internalCfg.Sink.CloudStorageConfig.SpoolBaseDir))
require.True(t, util.GetOrZero(internalCfg.Sink.Debezium.IncludeStartTs))
require.True(t, util.GetOrZero(internalCfg.Sink.Simple.IncludeStartTs))
require.Equal(t, "/etc/ssl/oauth-ca.pem", util.GetOrZero(internalCfg.Sink.KafkaConfig.SASLOAuthCA))
require.Equal(t, internalCfg.Mounter.WorkerNum, *apiCfg.Mounter.WorkerNum)
require.True(t, util.GetOrZero(internalCfg.Scheduler.EnableTableAcrossNodes))
Expand Down Expand Up @@ -185,6 +189,7 @@ func TestReplicaConfigConversion(t *testing.T) {
require.Equal(t, int64(1024), *apiCfgBack.Sink.CloudStorageConfig.SpoolDiskQuota)
require.Equal(t, "/tmp/ticdc-spool", *apiCfgBack.Sink.CloudStorageConfig.SpoolBaseDir)
require.True(t, util.GetOrZero(apiCfgBack.Sink.DebeziumConfig.IncludeStartTs))
require.True(t, util.GetOrZero(apiCfgBack.Sink.SimpleConfig.IncludeStartTs))
require.True(t, util.GetOrZero(apiCfgBack.Sink.DebeziumConfig.OutputOldValue))
require.Equal(t, "/etc/ssl/oauth-ca.pem", util.GetOrZero(apiCfgBack.Sink.KafkaConfig.SASLOAuthCA))
require.Equal(t, 16, *apiCfgBack.Mounter.WorkerNum)
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ type SinkConfig struct {
OpenProtocol *OpenProtocolConfig `toml:"open" json:"open,omitempty"`
// DebeziumConfig related configurations
Debezium *DebeziumConfig `toml:"debezium" json:"debezium,omitempty"`
// Simple protocol related configurations
Simple *SimpleConfig `toml:"simple" json:"simple,omitempty"`

CaseSensitive *bool `toml:"case-sensitive" json:"case-sensitive,omitempty"`
// Integrity is only available when the downstream is MQ.
Expand Down Expand Up @@ -1191,6 +1193,13 @@ type DebeziumConfig struct {
IncludeStartTs *bool `toml:"include-start-ts" json:"include-start-ts,omitempty"`
}

// SimpleConfig represents the configurations for simple protocol encoding
type SimpleConfig struct {
// IncludeStartTs controls whether the transaction start_ts is included in
// Simple JSON DML messages. Encoding-format=avro rejects this option.
IncludeStartTs *bool `toml:"include-start-ts" json:"include-start-ts,omitempty"`
}

// validRoutingExpressionRegexp accepts routing expressions made of literal text
// and the {schema}/{table} placeholders, such as "archive", "{table}_bak", or "{schema}_{table}".
var validRoutingExpressionRegexp = regexp.MustCompile(`^(?:[^{}]|\{schema\}|\{table\})*$`)
Expand Down
27 changes: 27 additions & 0 deletions pkg/sink/codec/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ type Config struct {
// Debezium only. Whether the transaction start_ts should be included in
// the source block of the output. JSON protocol only.
DebeziumIncludeStartTs bool
// Simple only. Whether the transaction start_ts should be included in
// Simple JSON DML messages. Encoding-format=avro rejects this option.
SimpleIncludeStartTs bool
// CSV only. Whether header should be included in the output.
CSVOutputFieldHeader bool
}
Expand Down Expand Up @@ -149,6 +152,7 @@ func NewConfig(protocol config.Protocol) *Config {
OpenOutputOldValue: true,
DebeziumDisableSchema: false,
DebeziumIncludeStartTs: false,
SimpleIncludeStartTs: false,
CSVOutputFieldHeader: false,
}
}
Expand Down Expand Up @@ -191,6 +195,7 @@ type urlConfig struct {

DebeziumDisableSchema *bool `form:"debezium-disable-schema"`
DebeziumIncludeStartTs *bool `form:"debezium-include-start-ts"`
SimpleIncludeStartTs *bool `form:"simple-include-start-ts"`
// EncodingFormatType is only works for the simple protocol,
// can be `json` and `avro`, default to `json`.
EncodingFormatType *string `form:"encoding-format"`
Expand Down Expand Up @@ -328,6 +333,12 @@ func (c *Config) Apply(sinkURI *url.URL, sinkConfig *config.SinkConfig) error {
if rawURLParameter.DebeziumIncludeStartTs != nil {
c.DebeziumIncludeStartTs = *rawURLParameter.DebeziumIncludeStartTs
}
if urlParameter.SimpleIncludeStartTs != nil {
c.SimpleIncludeStartTs = *urlParameter.SimpleIncludeStartTs
}
if rawURLParameter.SimpleIncludeStartTs != nil {
c.SimpleIncludeStartTs = *rawURLParameter.SimpleIncludeStartTs
}

return nil
}
Expand Down Expand Up @@ -363,6 +374,9 @@ func mergeConfig(
if sinkConfig.Debezium != nil && sinkConfig.Debezium.IncludeStartTs != nil {
dest.DebeziumIncludeStartTs = sinkConfig.Debezium.IncludeStartTs
}
if sinkConfig.Simple != nil && sinkConfig.Simple.IncludeStartTs != nil {
dest.SimpleIncludeStartTs = sinkConfig.Simple.IncludeStartTs
}
}
if err := mergo.Merge(dest, urlParameters, mergo.WithOverride); err != nil {
return nil, err
Expand Down Expand Up @@ -412,6 +426,19 @@ func (c *Config) Validate() error {
)
}

if c.SimpleIncludeStartTs {
if c.Protocol != config.ProtocolSimple {
return errors.ErrCodecInvalidConfig.GenWithStack(
`simple-include-start-ts only takes effect with protocol "simple"`,
)
}
if c.EncodingFormat == EncodingFormatAvro {
return errors.ErrCodecInvalidConfig.GenWithStack(
`simple-include-start-ts is not supported with encoding-format "avro"`,
)
}
}

if c.Protocol == config.ProtocolAvro || c.Protocol == config.ProtocolDebeziumAvro {
if c.AvroConfluentSchemaRegistry != "" && c.AvroGlueSchemaRegistry != nil {
protocol := "Avro"
Expand Down
39 changes: 39 additions & 0 deletions pkg/sink/codec/common/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,42 @@ func TestDebeziumIncludeStartTsConfig(t *testing.T) {
require.True(t, ok)
require.Equal(t, errors.ErrCodecInvalidConfig.RFCCode(), errCode)
}

func TestSimpleIncludeStartTsConfig(t *testing.T) {
cfg := NewConfig(config.ProtocolSimple)
sinkURI, err := url.Parse("kafka://127.0.0.1:9092/topic?protocol=simple&simple-include-start-ts=true")
require.NoError(t, err)
require.NoError(t, cfg.Apply(sinkURI, config.GetDefaultReplicaConfig().Sink))
require.True(t, cfg.SimpleIncludeStartTs)
require.NoError(t, cfg.Validate())

on := true
cfg2 := NewConfig(config.ProtocolSimple)
sinkConfig := config.GetDefaultReplicaConfig().Sink
sinkConfig.Simple = &config.SimpleConfig{IncludeStartTs: &on}
sinkURI2, err := url.Parse("kafka://127.0.0.1:9092/topic?protocol=simple")
require.NoError(t, err)
require.NoError(t, cfg2.Apply(sinkURI2, sinkConfig))
require.True(t, cfg2.SimpleIncludeStartTs)

cfg3 := NewConfig(config.ProtocolSimple)
sinkConfig3 := config.GetDefaultReplicaConfig().Sink
sinkConfig3.Simple = &config.SimpleConfig{IncludeStartTs: &on}
sinkURI3, err := url.Parse("kafka://127.0.0.1:9092/topic?protocol=simple&simple-include-start-ts=false")
require.NoError(t, err)
require.NoError(t, cfg3.Apply(sinkURI3, sinkConfig3))
require.False(t, cfg3.SimpleIncludeStartTs)

cfg4 := NewConfig(config.ProtocolSimple)
cfg4.SimpleIncludeStartTs = true
cfg4.EncodingFormat = EncodingFormatAvro
errCode, ok := errors.RFCCode(cfg4.Validate())
require.True(t, ok)
require.Equal(t, errors.ErrCodecInvalidConfig.RFCCode(), errCode)

cfg5 := NewConfig(config.ProtocolDebezium)
cfg5.SimpleIncludeStartTs = true
errCode, ok = errors.RFCCode(cfg5.Validate())
require.True(t, ok)
require.Equal(t, errors.ErrCodecInvalidConfig.RFCCode(), errCode)
}
9 changes: 9 additions & 0 deletions pkg/sink/codec/simple/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ func (d *Decoder) assembleHandleKeyOnlyRowChangedEvent(
TableID: m.TableID,
Type: m.Type,
CommitTs: m.CommitTs,
StartTs: m.StartTs,
SchemaVersion: m.SchemaVersion,
}

Expand Down Expand Up @@ -595,8 +596,16 @@ func parseValue(
return val
}

func startTsFromMessage(msg *message) uint64 {
if msg.StartTs != 0 {
return msg.StartTs
}
return msg.CommitTs
}

func buildDMLEvent(msg *message, tableInfo *commonType.TableInfo, enableRowChecksum bool, db *sql.DB) *commonEvent.DMLEvent {
result := &commonEvent.DMLEvent{
StartTs: startTsFromMessage(msg),
CommitTs: msg.CommitTs,
PhysicalTableID: msg.TableID,
TableInfo: tableInfo,
Expand Down
9 changes: 8 additions & 1 deletion pkg/sink/codec/simple/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ type message struct {
// SQL is only for the DDL event.
SQL string `json:"sql,omitempty"`
CommitTs uint64 `json:"commitTs"`
BuildTs int64 `json:"buildTs"`
// StartTs is the transaction start TSO. Present on DML messages only when
// simple-include-start-ts is enabled. omitempty keeps the pre-feature format
// when the field is unset (zero).
StartTs uint64 `json:"startTs,omitempty"`
BuildTs int64 `json:"buildTs"`
// SchemaVersion is for the DML event.
SchemaVersion uint64 `json:"schemaVersion,omitempty"`

Expand Down Expand Up @@ -332,6 +336,9 @@ func (a *jsonMarshaller) newDMLMessage(
HandleKeyOnly: onlyHandleKey,
ClaimCheckLocation: claimCheckFileName,
}
if a.config.SimpleIncludeStartTs {
m.StartTs = event.StartTs
}
if event.IsInsert() {
m.Type = DMLTypeInsert
m.Data = a.formatColumns(event.GetRows(), event.TableInfo, onlyHandleKey, event.ColumnSelector)
Expand Down
Loading
Loading