Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 20 additions & 0 deletions api/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,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 @@ -558,6 +564,7 @@ func (c *ReplicaConfig) toInternalReplicaConfigWithOriginConfig(
SafeMode: c.Sink.SafeMode,
OpenProtocol: openProtocolConfig,
Debezium: debeziumConfig,
Simple: simpleConfig,
}

if c.Sink.TxnAtomicity != nil {
Expand Down Expand Up @@ -906,6 +913,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 @@ -927,6 +940,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 @@ -1193,6 +1207,7 @@ type SinkConfig struct {
DebeziumDisableSchema *bool `json:"debezium_disable_schema,omitempty"`
DebeziumConfig *DebeziumConfig `json:"debezium,omitempty"`
OpenProtocolConfig *OpenProtocolConfig `json:"open,omitempty"`
SimpleConfig *SimpleConfig `json:"simple,omitempty"`
}

// CSVConfig denotes the csv config
Expand Down Expand Up @@ -1695,6 +1710,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"`
}

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) {
SpoolDiskQuota: util.AddressOf(int64(1024)),
SpoolBaseDir: util.AddressOf("/tmp/ticdc-spool"),
},
SimpleConfig: &SimpleConfig{
IncludeStartTs: util.AddressOf(true),
},
DebeziumConfig: &DebeziumConfig{
IncludeStartTs: util.AddressOf(true),
DecimalHandlingMode: util.AddressOf("string"),
Expand Down Expand Up @@ -143,6 +146,7 @@ func TestReplicaConfigConversion(t *testing.T) {
require.True(t, util.GetOrZero(internalCfg.Sink.CloudStorageConfig.UseTableIDAsPath))
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.Simple.IncludeStartTs))
require.True(t, util.GetOrZero(internalCfg.Sink.Debezium.IncludeStartTs))
require.Equal(t, "string", util.GetOrZero(internalCfg.Sink.Debezium.DecimalHandlingMode))
require.Equal(t, "string", util.GetOrZero(internalCfg.Sink.Debezium.BigintUnsignedHandlingMode))
Expand Down Expand Up @@ -194,6 +198,7 @@ func TestReplicaConfigConversion(t *testing.T) {
require.True(t, *apiCfgBack.Sink.CloudStorageConfig.UseTableIDAsPath)
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.SimpleConfig.IncludeStartTs))
require.True(t, util.GetOrZero(apiCfgBack.Sink.DebeziumConfig.IncludeStartTs))
require.Equal(t, "string", util.GetOrZero(apiCfgBack.Sink.DebeziumConfig.DecimalHandlingMode))
require.Equal(t, "string", util.GetOrZero(apiCfgBack.Sink.DebeziumConfig.BigintUnsignedHandlingMode))
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"`

// Integrity is only available when the downstream is MQ.
Integrity *IntegrityConfig `toml:"integrity" json:"integrity"`
Expand Down Expand Up @@ -1197,6 +1199,13 @@ type DebeziumConfig struct {
BinaryHandlingMode *string `toml:"binary-handling-mode" json:"binary-handling-mode,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
32 changes: 31 additions & 1 deletion pkg/sink/codec/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Config struct {
DebeziumDisableSchema bool
// Debezium only. Whether before value should be included in the output.
DebeziumOutputOldValue bool
// Simple only. Whether the transaction start_ts should be included in
// Simple JSON DML messages. Encoding-format=avro rejects this option.
SimpleIncludeStartTs bool
// Debezium only. Whether the transaction start_ts should be included in
// the source block of the output. JSON protocol only.
DebeziumIncludeStartTs bool
Expand Down Expand Up @@ -157,6 +160,7 @@ func NewConfig(protocol config.Protocol) *Config {
OpenOutputOldValue: true,
DebeziumDisableSchema: false,
CSVOutputFieldHeader: false,
SimpleIncludeStartTs: false,
DebeziumIncludeStartTs: false,
}
}
Expand Down Expand Up @@ -208,6 +212,7 @@ type urlConfig struct {
DebeziumDecimalHandlingMode *string `form:"debezium-decimal-handling-mode"`
DebeziumBigintUnsignedHandlingMode *string `form:"debezium-bigint-unsigned-handling-mode"`
DebeziumBinaryHandlingMode *string `form:"debezium-binary-handling-mode"`
SimpleIncludeStartTs *bool `form:"simple-include-start-ts"`
DebeziumIncludeStartTs *bool `form:"debezium-include-start-ts"`
// EncodingFormatType is only works for the simple protocol,
// can be `json` and `avro`, default to `json`.
Expand All @@ -226,7 +231,10 @@ func (c *Config) Apply(sinkURI *url.URL, sinkConfig *config.SinkConfig) error {
if err = binding.Query.Bind(req, urlParameter); err != nil {
return errors.WrapError(errors.ErrSinkInvalidConfig, err)
}
// Explicit URI values take precedence, including empty strings.
// Keep the raw URI parameters: mergeConfig uses mergo, which cannot
// override a *bool "true" (from the config file) with an explicit
// "false" from the sink URI, so explicit URI values are applied last.
// Explicit string URI values, including empty strings, also take precedence.
rawURLParameter := urlParameter
if urlParameter, err = mergeConfig(sinkConfig, urlParameter); err != nil {
return err
Expand Down Expand Up @@ -350,6 +358,12 @@ func (c *Config) Apply(sinkURI *url.URL, sinkConfig *config.SinkConfig) error {
if urlParameter.DebeziumDisableSchema != nil {
c.DebeziumDisableSchema = *urlParameter.DebeziumDisableSchema
}
if urlParameter.SimpleIncludeStartTs != nil {
c.SimpleIncludeStartTs = *urlParameter.SimpleIncludeStartTs
}
if rawURLParameter.SimpleIncludeStartTs != nil {
c.SimpleIncludeStartTs = *rawURLParameter.SimpleIncludeStartTs
}
if urlParameter.DebeziumIncludeStartTs != nil {
c.DebeziumIncludeStartTs = *urlParameter.DebeziumIncludeStartTs
}
Expand Down Expand Up @@ -392,6 +406,9 @@ func mergeConfig(
if sinkConfig.DebeziumDisableSchema != nil {
dest.DebeziumDisableSchema = sinkConfig.DebeziumDisableSchema
}
if sinkConfig.Simple != nil && sinkConfig.Simple.IncludeStartTs != nil {
dest.SimpleIncludeStartTs = sinkConfig.Simple.IncludeStartTs
}
if sinkConfig.Debezium != nil && sinkConfig.Debezium.IncludeStartTs != nil {
dest.DebeziumIncludeStartTs = sinkConfig.Debezium.IncludeStartTs
}
Expand Down Expand Up @@ -468,6 +485,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.ProtocolDebezium {
if c.DebeziumDecimalHandlingMode != "double" &&
c.DebeziumDecimalHandlingMode != DecimalHandlingModeString {
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 @@ -160,6 +160,45 @@ func TestValidateMaxBatchMessageBytes(t *testing.T) {
}
}

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)
}

func TestDebeziumIncludeStartTsConfig(t *testing.T) {
// URI parameter
cfg := NewConfig(config.ProtocolDebezium)
Expand Down
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 @@ -258,7 +258,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 @@ -340,6 +344,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