TiCDC Avro Protocol

Avro is a data exchange format protocol defined by Apache Avro™ and chosen by Confluent Platform as the default data exchange format. This document describes the implementation of the Avro data format in TiCDC, including TiDB extension fields, definition of the Avro data format, and the interaction between Avro and Confluent Schema Registry.

TiCDC Avro Protocol - 图1

Warning

Starting from v7.3.0, if you enable TiCDC to replicate tables without a valid index, TiCDC will report an error when you create a changefeed that uses the Avro protocol.

Use Avro

When using Message Queue (MQ) as a downstream sink, you can specify Avro in sink-uri. TiCDC captures TiDB DML events, creates Avro messages from these events, and sends the messages downstream. When Avro detects a schema change, it registers the latest schema with Schema Registry.

The following is a configuration example using Avro:

  1. cdc cli changefeed create --server=http://127.0.0.1:8300 --changefeed-id="kafka-avro" --sink-uri="kafka://127.0.0.1:9092/topic-name?protocol=avro" --schema-registry=http://127.0.0.1:8081 --config changefeed_config.toml
  1. [sink]
  2. dispatchers = [
  3. {matcher = ['*.*'], topic = "tidb_{schema}_{table}"},
  4. ]

The value of --schema-registry supports the https protocol and username:password authentication, for example, --schema-registry=https://username:password@schema-registry-uri.com. The username and password must be URL-encoded.

TiDB extension fields

By default, Avro only collects data of changed rows in DML events and does not collect the type of data changes or TiDB-specific CommitTS (the unique identifiers of transactions). To address this issue, TiCDC introduces the following three TiDB extension fields to the Avro protocol message. When enable-tidb-extension is set to true (false by default) in sink-uri, TiCDC adds these three fields to the Avro messages during message generation.

  • _tidb_op: The DML type. “c” indicates insert and “u” indicates updates.
  • _tidb_commit_ts: The unique identifier of a transaction.
  • _tidb_commit_physical_time: The physical timestamp in a transaction identifier.

The following is a configuration example:

  1. cdc cli changefeed create --server=http://127.0.0.1:8300 --changefeed-id="kafka-avro-enable-extension" --sink-uri="kafka://127.0.0.1:9092/topic-name?protocol=avro&enable-tidb-extension=true" --schema-registry=http://127.0.0.1:8081 --config changefeed_config.toml
  1. [sink]
  2. dispatchers = [
  3. {matcher = ['*.*'], topic = "tidb_{schema}_{table}"},
  4. ]

Definition of the data format

TiCDC converts a DML event into a Kafka event, and the Key and Value of an event are encoded according to the Avro protocol.

Key data format

  1. {
  2. "name":"{{TableName}}",
  3. "namespace":"{{Namespace}}",
  4. "type":"record",
  5. "fields":[
  6. {{ColumnValueBlock}},
  7. {{ColumnValueBlock}},
  8. ]
  9. }
  • {{TableName}} indicates the name of the table where the event occurs.
  • {{Namespace}} is the namespace of Avro.
  • {{ColumnValueBlock}} defines the format of each column of data.

The fields in the key contains only primary key columns or unique index columns.

Value data format

  1. {
  2. "name":"{{TableName}}",
  3. "namespace":"{{Namespace}}",
  4. "type":"record",
  5. "fields":[
  6. {{ColumnValueBlock}},
  7. {{ColumnValueBlock}},
  8. ]
  9. }

The data format of Value is the same as that of Key, by default. However, fields in the Value contains all columns, not just the primary key columns.

After you enable enable-tidb-extension, the data format of the Value will be as follows:

  1. {
  2. "name":"{{TableName}}",
  3. "namespace":"{{Namespace}}",
  4. "type":"record",
  5. "fields":[
  6. {{ColumnValueBlock}},
  7. {{ColumnValueBlock}},
  8. {
  9. "name":"_tidb_op",
  10. "type":"string"
  11. },
  12. {
  13. "name":"_tidb_commit_ts",
  14. "type":"long"
  15. },
  16. {
  17. "name":"_tidb_commit_physical_time",
  18. "type":"long"
  19. }
  20. ]
  21. }

Compared with the Value data format with enable-tidb-extension disabled, three new fields are added: _tidb_op, _tidb_commit_ts, and _tidb_commit_physical_time.

Column data format

The Column data is the {{ColumnValueBlock}} part of the Key/Value data format. TiCDC generates the Column data format based on the SQL Type. The basic Column data format is as follows:

  1. {
  2. "name":"{{ColumnName}}",
  3. "type":{
  4. "connect.parameters":{
  5. "tidb_type":"{{TIDB_TYPE}}"
  6. },
  7. "type":"{{AVRO_TYPE}}"
  8. }
  9. }

If one column can be NULL, the Column data format can be:

  1. {
  2. "default":null,
  3. "name":"{{ColumnName}}",
  4. "type":[
  5. "null",
  6. {
  7. "connect.parameters":{
  8. "tidb_type":"{{TIDB_TYPE}}"
  9. },
  10. "type":"{{AVRO_TYPE}}"
  11. }
  12. ]
  13. }
  • {{ColumnName}} indicates the column name.
  • {{TIDB_TYPE}} indicates the type in TiDB, which is not a one-to-one mapping with the SQL type.
  • {{AVRO_TYPE}} indicates the type in avro spec.
SQL TYPETIDB_TYPEAVRO_TYPEDescription
BOOLINTint
TINYINTINTintWhen it is unsigned, TIDB_TYPE is INT UNSIGNED.
SMALLINTINTintWhen it is unsigned, TIDB_TYPE is INT UNSIGNED.
MEDIUMINTINTintWhen it is unsigned, TIDB_TYPE is INT UNSIGNED.
INTINTintWhen it is unsigned, TIDB_TYPE is INT UNSIGNED and AVRO_TYPE is long.
BIGINTBIGINTlongWhen it is unsigned, TIDB_TYPE is BIGINT UNSIGNED. If avro-bigint-unsigned-handling-mode is string, AVRO_TYPE is string.
TINYBLOBBLOBbytes-
BLOBBLOBbytes-
MEDIUMBLOBBLOBbytes-
LONGBLOBBLOBbytes-
BINARYBLOBbytes-
VARBINARYBLOBbytes-
TINYTEXTTEXTstring-
TEXTTEXTstring-
MEDIUMTEXTTEXTstring-
LONGTEXTTEXTstring-
CHARTEXTstring-
VARCHARTEXTstring-
FLOATFLOATdouble-
DOUBLEDOUBLEdouble-
DATEDATEstring-
DATETIMEDATETIMEstring-
TIMESTAMPTIMESTAMPstring-
TIMETIMEstring-
YEARYEARint-
BITBITbytes-
JSONJSONstring-
ENUMENUMstring-
SETSETstring-
DECIMALDECIMALbytesWhen avro-decimal-handling-mode is string, AVRO_TYPE is string.

In the Avro protocol, two other sink-uri parameters might affect the Column data format as well: avro-decimal-handling-mode and avro-bigint-unsigned-handling-mode.

  • avro-decimal-handling-mode controls how Avro handles decimal fields, including:

    • string: Avro handles decimal fields as strings.
    • precise: Avro handles decimal fields as bytes.
  • avro-bigint-unsigned-handling-mode controls how Avro handles BIGINT UNSIGNED fields, including:

    • string: Avro handles BIGINT UNSIGNED fields as strings.
    • long: Avro handles BIGINT UNSIGNED fields as 64-bit signed integers. When the value is greater than 9223372036854775807, overflow will occur.

The following is a configuration example:

  1. cdc cli changefeed create --server=http://127.0.0.1:8300 --changefeed-id="kafka-avro-string-option" --sink-uri="kafka://127.0.0.1:9092/topic-name?protocol=avro&avro-decimal-handling-mode=string&avro-bigint-unsigned-handling-mode=string" --schema-registry=http://127.0.0.1:8081 --config changefeed_config.toml
  1. [sink]
  2. dispatchers = [
  3. {matcher = ['*.*'], topic = "tidb_{schema}_{table}"},
  4. ]

Most SQL types are mapped to the base Column data format. Some other SQL types extend the base data format to provide more information.

BIT(64)

  1. {
  2. "name":"{{ColumnName}}",
  3. "type":{
  4. "connect.parameters":{
  5. "tidb_type":"BIT",
  6. "length":"64"
  7. },
  8. "type":"bytes"
  9. }
  10. }

ENUM/SET(a,b,c)

  1. {
  2. "name":"{{ColumnName}}",
  3. "type":{
  4. "connect.parameters":{
  5. "tidb_type":"ENUM/SET",
  6. "allowed":"a,b,c"
  7. },
  8. "type":"string"
  9. }
  10. }

DECIMAL(10, 4)

  1. {
  2. "name":"{{ColumnName}}",
  3. "type":{
  4. "connect.parameters":{
  5. "tidb_type":"DECIMAL",
  6. },
  7. "logicalType":"decimal",
  8. "precision":10,
  9. "scale":4,
  10. "type":"bytes"
  11. }
  12. }

DDL events and schema changes

Avro does not generate DDL events downstream. It checks whether a schema changes each time a DML event occurs. If a schema changes, Avro generates a new schema and registers it with the Schema Registry. If the schema change does not pass the compatibility check, the registration fails. TiCDC does not resolve any schema compatibility issues.

Note that, even if a schema change passes the compatibility check and a new version is registered, the data producers and consumers still need to perform an upgrade to ensure normal running of the system.

Assume that the default compatibility policy of Confluent Schema Registry is BACKWARD and add a non-empty column to the source table. In this situation, Avro generates a new schema but fails to register it with Schema Registry due to compatibility issues. At this time, the changefeed enters an error state.

For more information about schemas, refer to Schema Registry related documents.

Topic distribution

Schema Registry supports three Subject Name Strategies: TopicNameStrategy, RecordNameStrategy, and TopicRecordNameStrategy. Currently, TiCDC Avro only supports TopicNameStrategy, which means that a Kafka topic can only receive data in one data format. Therefore, TiCDC Avro prohibits mapping multiple tables to the same topic. When you create a changefeed, an error will be reported if the topic rule does not include the {schema} and {table} placeholders in the configured distribution rule.

Compatibility

When upgrading the TiCDC cluster to v7.0.0, if a table replicated using Avro contains the FLOAT data type, you need to manually adjust the compatibility policy of Confluent Schema Registry to None before upgrading so that the changefeed can successfully update the schema. Otherwise, after upgrading, the changefeed will be unable to update the schema and enter an error state. For more information, see #8490.