Debezium connector for Cloud Spanner

Overview

To read and process database changes, the connector queries from a change stream.

The connector produces a change event for every row-level insert, update, and delete operation that was captured and sends change event records for each table in a separate Kafka topic. Client applications read the Kafka topics that correspond to the database tables of interest, and can react to every row-level event they receive from those topics.

The connector is tolerant of failures. As the connector reads changes and produces events, it records the last commit timestamp processed for each change stream partition. If the connector stops for any reason (including communication failures, network problems, or crashes), upon restart the connector continues streaming records where it last left off.

How the connector works

To optimally configure and run a Debezium Spanner connector, it is helpful to understand how the connector streams change events, determines Kafka topic names, and uses metadata.

Streaming changes

The Debezium Spanner connector spends all its time streaming changes from the change stream to which it is subscribed. When a change occurs on a table, Spanner writes a corresponding change stream record in the database, synchronously in the same transaction as the data change. To scale change streams writes and reads, Spanner splits and merges the internal change stream storage along with the database data. To support reading change stream records in near real-time as database writes scale, the Spanner API is designed for a change stream to be queried concurrently using the change stream partitions. See about Spanner change stream’s partitioning model.

The connector offers an abstraction over the Spanner API for querying change streams. With this connector, you don’t have to manage the change stream partition lifecycle. The connector provides you with a stream of data change records so that you are free to focus more on application logic and less on specific API details and dynamic change stream partitioning.

When subscribing to a change stream, the connector needs to provide the the project ID, Spanner instance ID, Spanner database ID, as well as the change stream name. Optionally, the user can also provide a start timestamp and an end timestamp. See the this section for a more detailed list of the connector configuration properties.

When the connector receives changes it transforms the events into Debezium create, update, or delete events. The connector forwards these change events in records to the Kafka Connect framework, which is running in the same process. The Kafka Connect process asynchronously writes the change event records in the same order in which they were generated to the appropriate Kafka topic.

Periodically, Kafka Connect records the most recent offset in another Kafka topic. The offset indicates source-specific position information that Debezium includes with each event. For the Spanner connector, the last processed commit timestamps for the change stream partitions is the offset.

When Kafka Connect gracefully shuts down, it stops the connectors, flushes all event records to Kafka, and records the last offset received from each connector. When Kafka Connect restarts, it reads the last recorded offset for each connector, and starts each connector at its last recorded offset.

The Spanner connector also records metadata information in the following metadata topics during streaming. It is not advisable to modify the content or the configuration of these topics:

  • _consumer_offsets: A topic automatically created by Kafka. Stores consumer offsets for consumers created in the Kafka connector

  • _kafka-connect-offsets: A topic automatically created by Kafka Connect. Stores the connector offsets.

  • _sync_topic_spanner_connector_connectorname: A topic automatically created by the connector. Stores metadata regarding change stream partitions.

  • _rebalancing_topic_spanner_connector_connectorname: A topic automatically created by the connector. Used to determine connector task aliveness.

  • _debezium-heartbeat.connectorname: A topic used to process Spanner change stream heartbeats.

Topic names

The Spanner connector writes events for all insert, update, and delete operations on a single table to a single Kafka topic. By default, the Kafka topic name is topicPrefix.connectorName.tableName where:

  • topicPrefix is the topic prefix as specified by the topic.prefix connector configuration property.

  • connectorName is the user-specified name of the connector.

  • tableName is the name of the database table in which the operation occurred.

For example, suppose that spanner is the logical name in the configuration for a connector that is capturing changes from a Spanner change stream that tracks changes in a database that contains four tables: table1, table2, table3, and table4. The connector would stream records to these four Kafka topics:

  • spanner.table1

  • spanner.table2

  • spanner.table3

  • spanner.table4

Data change events

The Debezium Spanner connector generates a data change event for each row-level INSERT, UPDATE, and DELETE operation. Each event contains a key and a value. The key and value are separate documents. The structure of the key and the value depends on the table that was changed.

Debezium and Kafka Connect are designed around continuous streams of event messages. However, the structure of these events may change over time, which can be difficult for consumers to handle. To address this, each event contains the schema for its content or, if you are using a schema registry, a schema ID that a consumer can use to obtain the schema from the registry. This makes each event self-contained. The schema for the key will never change. Please note that the schema for the value is an amalgamation of all the columns in the table that the change stream has tracked since the start time of the connector.

The following skeleton JSON documents show the basic structure for the key and value documents. However, how you configure the Kafka Connect converter that you choose to use in your application determines the representation of the key and value documents. A schema field is in a change event key or change event value only when you configure the converter to produce it. Likewise, the event key and event payload are present only if you configure a converter to produce it. If you use the JSON converter and you configure it to produce schemas, change events have this structure:

  1. // Key
  2. {
  3. "schema": { (1)
  4. ...
  5. },
  6. "payload": { (2)
  7. ...
  8. }
  9. }
  10. // Value
  11. {
  12. "schema": { (3)
  13. ...
  14. },
  15. "payload": { (4)
  16. ...
  17. }
  18. }
Table 1. Overview of change event basic content
ItemField nameDescription

1

schema

The first schema field is part of the event key. It specifies a Kafka Connect schema that describes what is in the event key’s payload portion. In other words, the first schema field describes the structure of the primary key.

2

payload

The first payload field is part of the event key. It has the structure described by the previous schema field and it contains the key for the row that was changed.

3

schema

The second schema field is part of the event value. It specifies the Kafka Connect schema that describes what is in the event value’s payload portion. In other words, the second schema describes the structure of the row that was changed. Typically, this schema contains nested schemas.

4

payload

The second payload field is part of the event value. It has the structure described by the previous schema field and it contains the actual data for the row that was changed.

The default behavior is that the connector streams change event records to topics with names that are the same as the event’s originating table.

Starting with Kafka 0.10, Kafka can optionally record the event key and value with the timestamp at which the message was created (recorded by the producer) or written to the log by Kafka.

Change event keys

For a given table, the change event’s key has a structure that contains a field for each column in the primary key of the table at the time the event was created. All key columns will be marked as non-optional.

Consider a users table defined in the business database and the example of a change event key for that table.

Example table

  1. CREATE TABLE Users (
  2. id INT64 NOT NULL,
  3. username STRING(MAX) NOT NULL,
  4. password STRING(MAX) NOT NULL,
  5. email STRING(MAX) NOT NULL)
  6. PRIMARY KEY (id);

Example change event key

Every change event for the users table while it has this definition has the same key structure, which in JSON looks like this:

  1. {
  2. "schema": { (1)
  3. "type": "struct",
  4. "name": "Users.Key", (2)
  5. "optional": false, (3)
  6. "fields": [ (4)
  7. {
  8. "type": "int64",
  9. "optional": "false",
  10. "field": "false"
  11. }
  12. ]
  13. },
  14. "payload": { (5)
  15. "id": "1"
  16. },
  17. }
Table 2. Description of change event key
ItemField nameDescription

1

schema

The schema portion of the key specifies a Kafka Connect schema that describes what is in the key’s payload portion.

2

Users.Key

Name of the schema that defines the structure of the key’s payload. This schema describes the structure of the primary key for the table that was changed.

3

optional

Indicates whether the event key must contain a value in its payload field. Primary key columns are always required.

4

fields

Specifies each field that is expected in the payload, including each field’s name, type, and whether it is optional.

5

payload

Contains the key for the row for which this change event was generated. In this example, the key, contains a single id field whose value is 1.

Change event values

Consider the same sample table that was used to show an example of a change event key:

  1. CREATE TABLE Users (
  2. id INT64 NOT NULL,
  3. username STRING(MAX) NOT NULL,
  4. password STRING(MAX) NOT NULL,
  5. email STRING(MAX) NOT NULL)
  6. PRIMARY KEY (id);

create events

The following example shows the value portion of a change event that the connector generates for an operation that creates data in the Users table. If a Spanner column is marked is non-optional, its value is required for all mutations that insert a row. All primary key columns in Spanner will be marked as non-optional. Note that even if a non-key column is marked as non-optional in Spanner, it will be shown as optional in the schema. Only primary key columns are marked as non-optional in the schema.

  1. {
  2. "schema": { (1)
  3. "type": "struct",
  4. "fields": [
  5. {
  6. "type": "struct",
  7. "fields": [
  8. {
  9. "type": "int32",
  10. "optional": false,
  11. "field": "id"
  12. },
  13. {
  14. "type": "string",
  15. "optional": true,
  16. "field": "first_name"
  17. },
  18. {
  19. "type": "string",
  20. "optional": true,
  21. "field": "last_name"
  22. },
  23. {
  24. "type": "string",
  25. "optional": true,
  26. "field": "email"
  27. }
  28. ],
  29. "optional": true,
  30. "name": "Users.Value", (2)
  31. "field": "before"
  32. },
  33. {
  34. "type": "struct",
  35. "fields": [
  36. {
  37. "type": "int32",
  38. "optional": false,
  39. "field": "id"
  40. },
  41. {
  42. "type": "string",
  43. "optional": false,
  44. "field": "first_name"
  45. },
  46. {
  47. "type": "string",
  48. "optional": false,
  49. "field": "last_name"
  50. },
  51. {
  52. "type": "string",
  53. "optional": false,
  54. "field": "email"
  55. }
  56. ],
  57. "optional": true,
  58. "name": "Users.Value",
  59. "field": "after"
  60. },
  61. {
  62. "type": "struct",
  63. "fields": [
  64. {
  65. "type": "string",
  66. "optional": false,
  67. "field": "version"
  68. },
  69. {
  70. "type": "string",
  71. "optional": false,
  72. "field": "connector"
  73. },
  74. {
  75. "type": "string",
  76. "optional": false,
  77. "field": "name"
  78. },
  79. {
  80. "type": "int64",
  81. "optional": false,
  82. "field": "ts_ms"
  83. },
  84. {
  85. "type": "int64",
  86. "optional": false,
  87. "field": "ts_us"
  88. },
  89. {
  90. "type": "int64",
  91. "optional": false,
  92. "field": "ts_ns"
  93. },
  94. {
  95. "type": "boolean",
  96. "optional": true,
  97. "default": false,
  98. "field": "snapshot"
  99. },
  100. {
  101. "type": "string",
  102. "optional": false,
  103. "field": "db"
  104. },
  105. {
  106. "type": "string",
  107. "optional": false,
  108. "field": "sequence"
  109. },
  110. {
  111. "type": "string",
  112. "optional": false,
  113. "field": "project_id"
  114. },
  115. {
  116. "type": "string",
  117. "optional": false,
  118. "field": "instance_id"
  119. },
  120. {
  121. "type": "string",
  122. "optional": false,
  123. "field": "database_id"
  124. },
  125. {
  126. "type": "string",
  127. "optional": false,
  128. "field": "change_stream_name"
  129. },
  130. {
  131. "type": "string",
  132. "optional": true,
  133. "field": "table"
  134. }
  135. {
  136. "type": "string",
  137. "optional": true,
  138. "field": "server_transaction_id"
  139. }
  140. {
  141. "type": "int64",
  142. "optional": true,
  143. "field": "low_watermark"
  144. }
  145. {
  146. "type": "int64",
  147. "optional": true,
  148. "field": "read_at_timestamp"
  149. }
  150. {
  151. "type": "int64",
  152. "optional": true,
  153. "field": "number_of_records_in_transaction"
  154. }
  155. {
  156. "type": "string",
  157. "optional": true,
  158. "field": "transaction_tag"
  159. }
  160. {
  161. "type": "boolean",
  162. "optional": true,
  163. "field": "system_transaction"
  164. }
  165. {
  166. "type": "string",
  167. "optional": true,
  168. "field": "value_capture_type"
  169. }
  170. {
  171. "type": "string",
  172. "optional": true,
  173. "field": "partition_token"
  174. }
  175. {
  176. "type": "int32",
  177. "optional": true,
  178. "field": "mod_number"
  179. }
  180. {
  181. "type": "boolean",
  182. "optional": true,
  183. "field": "is_last_record_in_transaction_in_partition"
  184. }
  185. {
  186. "type": "int64",
  187. "optional": true,
  188. "field": "number_of_partitions_in_transaction"
  189. }
  190. ],
  191. "optional": false,
  192. "name": "io.debezium.connector.spanner.Source", (3)
  193. "field": "source"
  194. },
  195. {
  196. "type": "string",
  197. "optional": false,
  198. "field": "op"
  199. },
  200. {
  201. "type": "int64",
  202. "optional": true,
  203. "field": "ts_ms"
  204. },
  205. {
  206. "type": "int64",
  207. "optional": true,
  208. "field": "ts_us"
  209. },
  210. {
  211. "type": "int64",
  212. "optional": true,
  213. "field": "ts_ns"
  214. }
  215. ],
  216. "optional": false,
  217. "name": "connector_name.Users.Envelope" (4)
  218. },
  219. "payload": { (5)
  220. "before": null, (6)
  221. "after": { (7)
  222. "id": 1,
  223. "first_name": "Anne",
  224. "last_name": "Kretchmar",
  225. "email": "annek@noanswer.org"
  226. },
  227. "source": { (8)
  228. "version": "2.6.2.Final",
  229. "connector": "spanner",
  230. "name": "spanner_connector",
  231. "ts_ms": 1670955531785,
  232. "ts_us": 1670955531785000,
  233. "ts_ns": 1670955531785000000,
  234. "snapshot": "false",
  235. "db": "database",
  236. "sequence": "1",
  237. "project_id": "project",
  238. "instance_id": "instance",
  239. "database_id": "database",
  240. "change_stream_name": "change_stream",
  241. "table": "Users",
  242. "server_transaction_id": "transaction_id",
  243. "low_watermark": 1670955471635,
  244. "read_at_timestamp": 1670955531791,
  245. "number_records_in_transaction": 2,
  246. "transaction_tag": "",
  247. "system_transaction": false,
  248. "value_capture_type": "OLD_AND_NEW_VALUES",
  249. "partition_token": "partition_token",
  250. "mod_number": 0,
  251. "is_last_record_in_transaction_in_partition": true,
  252. "number_of_partitions_in_transaction": 1
  253. },
  254. "op": "c", (9)
  255. "ts_ms": 1559033904863, (10)
  256. "ts_us": 1559033904863769, (10)
  257. "ts_ns": 1559033904863769841 (10)
  258. }
  259. }
Table 3. Descriptions of create event value fields
ItemField nameDescription

1

schema

The value’s schema, which describes the structure of the value’s payload. A change event’s value schema is the same in every change event that the connector generates for a particular table.

2

name

In the schema section, each name field specifies the schema for a field in the value’s payload.

3

name

io.debezium.connector.spanner.Source is the schema for the payload’s source field. This schema is specific to the Spanner connector. The connector uses it for all events that it generates.

4

name

connector_name.Users.Envelope is the schema for the overall structure of the payload, where connector_name is the connector name, and customers is the table.

5

payload

The value’s actual data. This is the information that the change event is providing.

6

before

An optional field that specifies the state of the row before the event occurred. When the op field is c for create, as it is in this example, the before field is null since this change event is for new content.

7

after

An optional field that specifies the state of the row after the event occurred. In this example, the after field contains the values of the new row’s id, first_name, last_name, and email columns.

8

source

Mandatory field that describes the source metadata for the event. This field contains information that you can use to compare this event with other events, with regard to the origin of the events, the order in which the events occurred, and whether events were part of the same transaction. The source metadata includes:

  • Debezium version

  • Connector type and name

  • Database and table that contains the new row

  • If the event was part of a snapshot

  • The record sequence number for the data change event in the transaction

  • The project ID

  • The instance ID

  • The database ID

  • The change stream name

  • The transaction ID

  • The low watermark, which represents the timestamp at which all records with commit timestamp less than the low watermark timestamp have already been streamed out by the connector

  • The commit timestamp for when the change was made in the database

  • The timestamp at which the connnector processed the change

  • The number of records in the originating transaction

  • The transaction tag

  • Whether or not the transaction was a system transaction

  • The value capture type

  • The originating partition token used to query this change event

  • The mod number in the original data change event received from Spanner

  • Whether or not the data change event was the last record in the transaction in the partition

  • The total number of change stream partitions in the transaction

9

op

Mandatory string that describes the type of operation that caused the connector to generate the event. In this example, c indicates that the operation created a row. Valid values are:

  • c = create

  • u = update

  • d = delete

10

ts_ms, ts_us, ts_ns

Optional field that displays the time at which the connector processed the event. The time is based on the system clock in the JVM running the Kafka Connect task.

In the source object, ts_ms indicates the time that the change was made in the database. By comparing the value for payload.source.ts_ms with the value for payload.ts_ms, you can determine the lag between the source database update and Debezium.

update events

The value of a change event for an update in the sample Users table has the same schema as a create event for that table. Likewise, the event value’s payload has the same structure. However, the event value payload contains different values in an update event. Here is an example of a change event value in an event that the connector generates for an update in the Users table:

  1. {
  2. "schema": { ... },
  3. "payload": {
  4. "before": { (1)
  5. "id": 1,
  6. "first_name": "Anne",
  7. "last_name": "Kretchmar",
  8. "email": "annek@noanswer.org"
  9. },
  10. "after": { (2)
  11. "id": 1,
  12. "first_name": "Anne Marie",
  13. "last_name": "Kretchmar",
  14. "email": "annek@noanswer.org"
  15. },
  16. "source": { (3)
  17. "version": "2.6.2.Final",
  18. "connector": "spanner",
  19. "name": "spanner_connector",
  20. "ts_ms": 1670955531785,
  21. "ts_us": 1670955531785000,
  22. "ts_ns": 1670955531785000000,
  23. "snapshot": "false",
  24. "db": "database",
  25. "sequence": "1",
  26. "project_id": "project",
  27. "instance_id": "instance",
  28. "database_id": "database",
  29. "change_stream_name": "change_stream",
  30. "table": "Users",
  31. "server_transaction_id": "transaction_id",
  32. "low_watermark": 1670955471635,
  33. "read_at_timestamp": 1670955531791,
  34. "number_records_in_transaction": 2,
  35. "transaction_tag": "",
  36. "system_transaction": false,
  37. "value_capture_type": "OLD_AND_NEW_VALUES",
  38. "partition_token": "partition_token",
  39. "mod_number": 0,
  40. "is_last_record_in_transaction_in_partition": true,
  41. "number_of_partitions_in_transaction": 1
  42. },
  43. "op": "u", (4)
  44. "ts_ms": 1465584025523, (5)
  45. "ts_us": 1465584025523614, (5)
  46. "ts_ns": 1465584025523614723 (5)
  47. }
  48. }
Table 4. Descriptions of update event value fields
ItemField nameDescription

1

before

An optional field that contains all values of all columns that were in the row before the database commit.

2

after

An optional field that specifies the state of the row after the event occurred. In this example, the first_name value is now Anne Marie.

3

source

Mandatory field that describes the source metadata for the event. The source field structure has the same fields as in a create event, but some values are different. The source metadata includes:

  • Debezium version

  • Connector type and name

  • Database (a.k.a keyspace) and table that contains the new row

  • If the event was part of a snapshot

  • The record sequence number for the data change event in the transaction

  • The project ID

  • The instance ID

  • The database ID

  • The change stream name

  • The transaction ID

  • The low watermark, which represents the timestamp at which all records with commit timestamp less than the low watermark timestamp have already been streamed out by the connector

  • The commit timestamp for when the change was made in the database

  • The timestamp at which the connnector processed the change

  • The number of records in the originating transaction

  • The transaction tag

  • Whether or not the transaction was a system transaction

  • The value capture type

  • The originating partition token used to query this change event

  • The mod number in the original data change event received from Spanner

  • Whether or not the data change event was the last record in the transaction in the partition

  • The total number of change stream partitions in the transaction

4

op

Mandatory string that describes the type of operation. In an update event value, the op field value is u, signifying that this row changed because of an update.

5

ts_ms, ts_us, ts_ns

Optional field that displays the time at which the connector processed the event. The time is based on the system clock in the JVM running the Kafka Connect task.

In the source object, ts_ms indicates the time that the change was made in the database. By comparing the value for payload.source.ts_ms with the value for payload.ts_ms, you can determine the lag between the source database update and Debezium.

delete events

The value in a delete change event has the same schema portion as create and update events for the same table. The payload portion in a delete event for the sample Users table looks like this:

  1. {
  2. "schema": { ... },
  3. "payload": {
  4. "before": { (1)
  5. "id": 1,
  6. "first_name": "Anne Marie",
  7. "last_name": "Kretchmar",
  8. "email": "annek@noanswer.org"
  9. },
  10. "after": null, (2)
  11. "source": { (3)
  12. "version": "2.6.2.Final",
  13. "connector": "spanner",
  14. "name": "spanner_connector",
  15. "ts_ms": 1670955531785,
  16. "ts_us": 1670955531785000,
  17. "ts_ns": 1670955531785000000,
  18. "snapshot": "false",
  19. "db": "database",
  20. "sequence": "1",
  21. "project_id": "project",
  22. "instance_id": "instance",
  23. "database_id": "database",
  24. "change_stream_name": "change_stream",
  25. "table": "Users",
  26. "server_transaction_id": "transaction_id",
  27. "low_watermark": 1670955471635,
  28. "read_at_timestamp": 1670955531791,
  29. "number_records_in_transaction": 2,
  30. "transaction_tag": "",
  31. "system_transaction": false,
  32. "value_capture_type": "OLD_AND_NEW_VALUES",
  33. "partition_token": "partition_token",
  34. "mod_number": 0,
  35. "is_last_record_in_transaction_in_partition": true,
  36. "number_of_partitions_in_transaction": 1
  37. },
  38. "op": "d", (4)
  39. "ts_ms": 1465581902461, (5)
  40. "ts_us": 1465581902461425, (5)
  41. "ts_ns": 1465581902461425378 (5)
  42. }
  43. }
Table 5. Descriptions of delete event value fields
ItemField nameDescription

1

before

Optional field that specifies the state of the row before the event occurred. In a delete event value, the before field contains the values that were in the row before it was deleted with the database commit.

2

after

Optional field that specifies the state of the row after the event occurred. In a delete event value, the after field is null, signifying that the row no longer exists.

3

source

Mandatory field that describes the source metadata for the event. In a delete event value, the source field structure is the same as for create and update events for the same table. Many source field values are also the same. In a delete event value, the ts_ms and lsn field values, as well as other values, might have changed. But the source field in a delete event value provides the same metadata:

  • Debezium version

  • Connector type and name

  • Database (a.k.a keyspace) and table that contains the new row

  • If the event was part of a snapshot

  • The record sequence number for the data change event in the transaction

  • The project ID

  • The instance ID

  • The database ID

  • The change stream name

  • The transaction ID

  • The low watermark, which represents the timestamp at which all records with commit timestamp less than the low watermark timestamp have already been streamed out by the connector

  • The commit timestamp for when the change was made in the database

  • The timestamp at which the connnector processed the change

  • The number of records in the originating transaction

  • The transaction tag

  • Whether or not the transaction was a system transaction

  • The value capture type

  • The originating partition token used to query this change event

  • The mod number in the original data change event received from Spanner

  • Whether or not the data change event was the last record in the transaction in the partition

  • The total number of change stream partitions in the transaction

4

op

Mandatory string that describes the type of operation. The op field value is d, signifying that this row was deleted.

5

ts_ms, ts_us, ts_ns

Optional field that displays the time at which the connector processed the event. The time is based on the system clock in the JVM running the Kafka Connect task.

In the source object, ts_ms indicates the time that the change was made in the database. By comparing the value for payload.source.ts_ms with the value for payload.ts_ms, you can determine the lag between the source database update and Debezium.

A delete change event record provides a consumer with the information it needs to process the removal of this row.

Spanner connector events are designed to work with Kafka log compaction. Log compaction enables removal of some older messages as long as at least the most recent message for every key is kept. This lets Kafka reclaim storage space while ensuring that the topic contains a complete data set and can be used for reloading key-based state. Note that if the low watermark is enabled, compaction should not be enabled.

Tombstone events

When a row is deleted, the delete event value still works with log compaction, because Kafka can remove all earlier messages that have that same key. However, for Kafka to remove all messages that have that same key, the message value must be null. To make this possible, the connector follows a delete event with a special tombstone event that has the same key but a null value.

Data type mappings

The Spanner connector represents changes to rows with events that are structured like the table in which the row exists. The event contains a field for each column value. How that value is represented in the event depends on the Spanner data type of the column. This section describes these mappings.

Spanner types

Table 6. Mappings for Spanner data types
Spanner data typeLiteral type (schema type)

BOOLEAN

BOOLEAN

INT64

INT64

ARRAY

ARRAY

BYTES

BYTES

STRING

STRING

FLOAT

DOUBLE

NUMERIC

STRING

TIMESTAMP

STRING

NUMERIC

STRING

Setting up Spanner

Checklist

  • Make sure to provide a project ID, Spanner instance ID, Spanner database ID, and change stream name. See documentation on how to create change streams.

  • Make sure to create and configure a GCP service account with the proper credentials. The service account key will need to be provided in the connector configuration. See more information about service accounts here.

See the following section on how to configure a Debezium Spanner connector.

Deployment

With Zookeeper, Kafka, and Kafka Connect installed, the remaining tasks to deploy a Debezium Spanner connector are to download the connector’s plug-in archive, extract the JAR files into your Kafka Connect environment, and add the directory with the JAR files to Kafka Connect’s plugin.path. You then need to restart your Kafka Connect process to pick up the new JAR files.

If you are working with immutable containers, see Debezium’s Container images for Zookeeper, Kafka and Kafka Connect with the Spanner connector already installed and ready to run. You can also run Debezium on Kubernetes and OpenShift.

Connector configuration example

Following is an example of the configuration for a Spanner connector that connects to a change stream called changeStreamAll in the database Database in instance Instance and project Project.

You can choose to produce events for a subset of the schemas and tables. Optionally, ignore, mask, or truncate columns that are sensitive, too large, or not needed.

  1. {
  2. "name": "spanner-connector", (1)
  3. "config": {
  4. "connector.class": "io.debezium.connector.spanner.SpannerConnector", (2)
  5. "gcp.spanner.change.stream": "changeStreamAll", (3)
  6. "gcp.spanner.project.id": "Project", (4)
  7. "gcp.spanner.instance.id": "Instance", (5)
  8. "gcp.spanner.database.id": "Database", (6)
  9. "gcp.spanner.credentials.json": <key.json>, (7)
  10. "tasks.max": 1 (8)
  11. }
  12. }
1The name of the connector when registered with a Kafka Connect service.
2The name of this Spanner connector class.
3The change stream name.
4The GCP Project ID.
5The Spanner Instance ID.
6The Spanner Database ID.
7The GCP service account key JSON.
8The maximum number of tasks.

See the complete list of Spanner connector properties that can be specified in these configurations.

Adding connector configuration

To start running a Spanner connector, create a connector configuration and add the configuration to your Kafka Connect cluster.

Prerequisites

  • The Spanner change stream is created and available.

  • The Spanner connector is installed.

Procedure

  1. Create a configuration for the Spanner connector.

  2. Use the Kafka Connect REST API to add that connector configuration to your Kafka Connect cluster.

Results

When the connector starts, it starts generating data change events for row-level operations and streaming change event records to Kafka topics.

Monitoring

The Debezium Spanner connector provides only one type of metric in addition to the built-in support for JMX metrics that Zookeeper, Kafka, and Kafka Connect provide.

  • Streaming metrics provide information about connector operations when the connector is capturing changes and streaming change event records.

Debezium monitoring documentation provides details for how to expose these metrics by using JMX.

Streaming metrics

The MBean is debezium.Cloud Spanner:type=connector-metrics,context=streaming,server=_<topic.prefix>_.

AttributesTypeDescription

long

The number of milliseconds since the connector has read and processed the most recent event.

long

The total number of events that this connector has seen since last started or reset.

long

The number of events that have been filtered by include/exclude list filtering rules configured on the connector.

int

The length the queue used to pass events between the streamer and the main Kafka Connect loop.

int

The free capacity of the queue used to pass events between the streamer and the main Kafka Connect loop.

boolean

Flag that denotes whether the connector is currently connected to the database server.

long

The number of milliseconds between the last change event’s timestamp and the connector processing it. The values will incoporate any differences between the clocks on the machines where the database server and the connector are running.

long

The number of processed transactions that were committed.

long

The maximum buffer of the queue in bytes used to pass events between the streamer and the main Kafka Connect loop.

long

The current buffer of the queue in bytes used to pass events between the streamer and the main Kafka Connect loop.

long

The current low watermark of the connector task. The low watermark describes the time T at which the connector is guaranteed to have streamed out all events with timestamp < T.

long

The current low watermark of the connector task in milliseconds. The low watermark describes the time T at which the connector is guaranteed to have streamed out all events with timestamp < T.

long

The lag of the low watermark behind the current time in milliseconds. The low watermark describes the time T at which the connector is guaranteed to have streamed out all events with timestamp < T.

long

The distribution of the lag of the low watermark behind the current time in milliseconds. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The distribution of the Spanner commit timestamp to connector read latency. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The distribution of the Spanner read timestamp to connector emit latency. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The distribution of the Spanner commit timestamp to connector emit latency. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The distribution of the Spanner commit timestamp to Kafka publish timestamp latency. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The distribution of the connector emit timestamp to Kafka publish timestamp latency. The variant will include P50, P95, P99, Average, Min, Max calculations.

long

The total Spanner event queue capacity. This queue indicates the total capacity of StreamEventQueue, a Spanner-specific queue that stores elements received from change stream queries.

long

The remaining Spanner event queue capacity.

long

The total task state change event queue capacity. This queue indicates the total capacity of the TaskStateChangeEventQueue, a Spanner-specific queue that stores events happening in the connector.

long

The remaining task state change event queue capacity.

long

The total number of partitions detected by the current task.

long

The total number of change stream queries issued by the current task.

long

The active number of change stream queries detected by the current task.

Connector configuration properties

The Debezium Spanner connector has many configuration properties that you can use to achieve the right connector behavior for your application. Many properties have default values. Information about the properties is organized as follows:

The following configuration properties are required unless a default value is available.

Table 7. Required connector configuration properties
PropertyDefaultDescription

No default

Unique name for the connector. Attempting to register again with the same name will fail. This property is required by all Kafka Connect connectors.

No default

The name of the Java class for the connector. Always use a value of io.debezium.connector.spanner.SpannerConnector for the Spanner connector.

1

The maximum number of tasks that should be created for this connector. The Spanner connector can use more than 1 tasks if you enable offset.storage.per.task mode.

No default

The GCP project ID

No default

The Spanner instance ID

No default

The Spanner database ID

No default

The Spanner change stream

No default

The file path to the GCP service account key JSON.

No default

The GCP service account key JSON. Required if gcp.spanner.credentials.path is not provided.

none

Specifies how schema names should be adjusted for compatibility with the message converter used by the connector. Possible settings:

  • none does not apply any adjustment.

  • avro replaces the characters that cannot be used in the Avro type name with underscore.

  • avrounicode replaces the underscore or characters that cannot be used in the Avro type name with corresponding unicode like _uxxxx. Note: is an escape sequence like backslash in Java

none

Specifies how field names should be adjusted for compatibility with the message converter used by the connector. Possible settings:

  • none does not apply any adjustment.

  • avro replaces the characters that cannot be used in the Avro type name with underscore.

  • avrounicode replaces the underscore or characters that cannot be used in the Avro type name with corresponding unicode like _uxxxx. Note: is an escape sequence like backslash in Java

See Avro naming for more details.

The following advanced configuration properties have defaults that work in most situations and therefore rarely need to be specified in the connector’s configuration.

Table 8. Advanced connector configuration properties
PropertyDefaultDescription

false

Whether or not the low watermark is enabled for the connector.

1000 ms

The interval at which the low watermark is updated.

300000

The Spanner heartbeat interval.

current time

The connector start time.

indefinite end time

The connector end time.

10000

The Spanner event queue capacity. Increase this capacity if the remaining stream event queue capacity approaches zero during connector runtime.

1000

The task state change event queue capacity. Increase this capacity if the remaining task state change event queue capacity approaches zero during connector runtime.

5

The maximum number of missed heartbeats for a change stream query before an exception is thrown

false

Whether or not task autoscaling is enabled

sync_topic_spanner_connector<connectorname>

The name for the Sync topic. The Sync topic is an internal connector topic used to store communication between tasks.

500 ms

The poll duration for the sync topic.

5000 ms

The timeout for requests to the sync topic.

15000 ms

The timeout for publishing to the sync topic.

5000 ms

The timeout for committing offsets for the sync topic.

60000 ms

The interval at which offsets are committed for the sync topic.

5 ms

The interval at which messages are published to the sync topic.

rebalancing_topic_spanner_connector<connectorname>

The name for the rebalancing topic. The rebalancing topic is an internal connector topic used to determine task aliveness.

5000

The poll duration for the rebalancing topic.

5000

The timeout for committing offsets for the rebalance topic.

60000 ms

The interval at which offsets are committed for the sync topic.

1000 ms

The duration of time a task waits before processing a rebalancing event.

No default

The custom metric tags will accept key-value pairs to customize the MBean object name which should be appended the end of regular name, each key would represent a tag for the MBean object name, and the corresponding value would be the value of that tag the key is. For example: k1=v1,k2=v2.

-1

Specifies how the connector responds after an operation that results in a retriable error, such as a connection error.
Set one of the following options:

    -1

    No limit. The connector always restarts automatically, and retries the operation, regardless of the number of previous failures.

    0

    Disabled. The connector fails immediately, and never retries the operation. User intervention is required to restart the connector.

    > 0

    The connector restarts automatically until it reaches the specified maximum number of retries. After the next failure, the connector stops, and user intervention is required to restart it.

For a more complete list of advanced configuration, see Github code.

Pass-through connector configuration properties

The connector also supports pass-through configuration properties that are used when creating the Kafka producer and consumer.

Be sure to consult the Kafka documentation for all of the configuration properties for Kafka producers and consumers. The Spanner connector does use the new consumer configuration properties.

Behavior when things go wrong

Debezium is a distributed system that captures all changes in multiple upstream databases; it never misses or loses an event. When the system is operating normally or being managed carefully then Debezium provides exactly once delivery of every change event record.

If a fault does happen then the system does not lose any events. However, while it is recovering from the fault, it might repeat some change events. In these abnormal situations, Debezium, like Kafka, provides at least once delivery of change events.

The rest of this section describes how Debezium handles various kinds of faults and problems.

Configuration and startup errors

In the following situations, the connector fails when trying to start, reports an error/exception in the log, and stops running:

  • The connector’s configuration is invalid.

  • The connector cannot successfully connect to Spanner by using the specified connection parameters.

In these cases, the error message has details about the problem and possibly a suggested workaround. After you correct the configuration or address the Spanner problem, restart the connector.

Spanner becomes unavailable

When the connector is running, Spanner could become unavailable for any number of reasons. The connector will continue running and will be able to stream events once Spanner becomes available again.

Kafka Connect process stops gracefully

Suppose that Kafka Connect is being run in distributed mode and a Kafka Connect process is stopped gracefully. Prior to shutting down that process, Kafka Connect migrates the process’s connector tasks to another Kafka Connect process in that group. The new connector tasks start processing exactly where the prior tasks stopped. There is a short delay in processing while the connector tasks are stopped gracefully and restarted on the new processes.

Kafka Connect process crashes

If the Kafka Connector process stops unexpectedly, any connector tasks it was running terminate without recording their most recently processed offsets. When Kafka Connect is being run in distributed mode, Kafka Connect restarts those connector tasks on other processes. However, Spanner connectors resume from the last offset that was recorded by the earlier processes. This means that the new replacement tasks might generate some of the same change events that were processed just prior to the crash. The number of duplicate events depends on the offset flush period and the volume of data changes just before the crash.

Because there is a chance that some events might be duplicated during a recovery from failure, consumers should always anticipate some duplicate events.

In each change event record, Debezium connectors insert source-specific information about the origin of the event, such as the originating partition token, the commit timestamp, the transaction ID, the record sequence, and the mod number. Consumers could use these identifiers for deduplication.

Kafka becomes unavailable

As the connector generates change events, the Kafka Connect framework records those events in Kafka by using the Kafka producer API. Periodically, at a frequency that you specify in the Kafka Connect configuration, Kafka Connect records the latest offset that appears in those change events. If the Kafka brokers become unavailable, the Kafka Connect process that is running the connectors repeatedly tries to reconnect to the Kafka brokers. In other words, the connector tasks pause until a connection can be re-established, at which point the connectors resume exactly where they left off.

Connector is stopped for a duration

If the connector is gracefully stopped, the database can continue to be used. When the connector restarts, it resumes streaming changes where it left off. That is, it generates change event records for all database changes that were made while the connector was stopped.

A properly configured Kafka cluster is able to handle massive throughput. Kafka Connect is written according to Kafka best practices, and given enough resources a Kafka Connect connector can also handle very large numbers of database change events. Because of this, after being stopped for a while, when a Debezium connector restarts, it is very likely to catch up with the database changes that were made while it was stopped. How quickly this happens depends on the capabilities and performance of Kafka and the volume of changes being made to the data in Spanner.

Note that the current connector can only be streamed back in time by ~one hour. The Kafka connector reads the information schema at the Kafka connector’s start timestamp to retrieve schema information. By default, Spanner cannot read the information schema at read timestamps before the version retention period, which defaults to one hour. If you wants to start the connector from earlier than one hour into the past, you will need to increase the database’s version retention period.

Limitations