Iaroslav Kozlov Русский

Data integration Streaming into Greenplum

Streaming into Greenplum

Change data capture: a connector reads the committed changes of rows from the log of the source database, Kafka keeps them as events in a log split into partitions, and the Greenplum Streaming Server loads them into the warehouse in small batches.

My work

I designed streaming ingestion from MySQL into Greenplum on Kafka, Debezium and GPSS.

How it is built

An example of the mechanism, not the configuration of the real pipeline: a generalised layout from the documentation of Debezium 3.7, Apache Kafka 4.4 and the Greenplum Streaming Server 2.2. The source is MySQL, as in my work. Two Kafka Connect workers stand for a distributed Connect cluster. Topics, tables and batch settings of the real pipeline are not shown.

data control My part

After the rebalance, in a distributed cluster with a second worker

the route after the failure unavailable takes over

  • Worker 1 stops sending heartbeats. After session.timeout.ms, 10 seconds by default, the broker removes it from the group and starts a rebalance.
  • For up to scheduled.rebalance.max.delay.ms, 5 minutes by default, the cluster can wait for the worker to return, and its task stays unassigned; then the task goes to worker 2.
  • All that time MySQL goes on writing the binlog; its files are removed automatically after binlog_expire_logs_seconds, 30 days by default.
  • The task on worker 2 resumes from the last offset recorded and reads the binlog from that position. Events processed after that offset can come again, as duplicates: GPSS applies them by key, like any other change. 1 3510

Components

Press a component to read what it does.

MySQL binlog, ROW

The operational database: MySQL. Its binary log records all operations in the order in which they are committed; for the connector it is written by rows, with binlog_format ROW and binlog_row_image FULL.

Connect worker 1 Debezium task

A Kafka Connect worker. Here it runs the task of the Debezium connector, which always uses a single task: it reads the binlog as another server of the MySQL cluster and turns each change of a row into an event.

Connect worker 2 same group.id

Another worker of the same Connect cluster: workers with the same group.id form one cluster, which balances the work across them.

Offsets topic internal

A topic of the Connect cluster itself, where Kafka Connect records how far each connector has got: for this one, a position in the binlog.

Table topic partitions

The events of one table. A new event is appended to one of the topic’s partitions, and events with the same key are written to the same one. Events are not deleted after they are read.

GPSS micro-batches

The Greenplum Streaming Server: it reads the topic as a Kafka consumer and writes what it has read into a Greenplum table, batch by batch.

Target table MERGE

The table in the warehouse. In MERGE mode a batch is applied by key: matching rows are updated or deleted, the others inserted.

History table GPSS offsets

A table in Greenplum where GPSS stores the offset of each load job; given a consumer group, with strong consistency, it stores it in the Kafka broker as well.

  1. Reading the log

    On its first start the connector takes a consistent snapshot of the database. After that it reads the binlog from the point at which the snapshot was made, and turns each change of a row into an event.

    In detail

    MySQL has a binary log that records all operations in the order in which they are committed, and uses it for replication and recovery; the connector reads it, under a server ID unique in the cluster, and produces change events for row-level INSERT, UPDATE and DELETE operations. The binlog format must be ROW, and binlog_row_image FULL. With the default snapshot mode, initial, the connector first takes a consistent snapshot; with snapshot.locking.mode minimal, the default, it holds the global read lock only while it reads the schemas and other metadata, and selects the rows in a REPEATABLE READ transaction. Then it reads the binlog from the point at which the snapshot was made. 1

    Where it breaks MySQL is typically set up to purge binlogs after a set period: binlog_expire_logs_seconds, 30 days by default. If the connector stops for too long, the server purges older binlog files and the connector’s last position may be lost: a new snapshot is needed, a baseline of the current state of the database. With snapshot.mode when_needed the connector takes one by itself.

  2. The event and its key

    Each change becomes an event in its table’s topic, keyed by the primary key of the row. Events with the same key go to the same partition, and a partition is read in the order it was written.

    In detail

    The key of a change event reflects the structure of the table’s primary key; a table with no primary key takes the structure of its unique key, and message.key.columns can name other columns. A new event is appended to one of the topic’s partitions; events with the same key are written to the same partition, and Kafka guarantees that any consumer of a partition reads its events in exactly the order they were written. Events are not deleted after they are read: a setting of the topic says how long they are kept. 1 2

    Where it breaks Order holds within a partition, not across partitions. And an update that changes a row’s primary key does not come as an update: the connector emits a delete event for the old key, a tombstone, and a create event for the new key.

  3. The connector’s offset

    From time to time the connector’s offset is recorded: how far it has got, a position in the binlog. After a restart the connector reads the binlog again from that position.

    In detail

    The connector periodically records its offsets. When it restarts, after a crash or a graceful stop, it starts reading the binlog from a specific position, that is, from a specific point in time. A distributed Connect cluster keeps the configurations of its connectors, their offsets and their status in three topics of its own, which the documentation asks to be replicated and compacted. 1 34

    Where it breaks What was sent after the last recorded offset can be sent again after a restart: consumers see every event at least once, and such duplicates are visible to them.

  4. Batches

    GPSS reads the topic as a Kafka consumer and writes what it has read into Greenplum in batches: by default at most once in five seconds, with no limit on rows.

    In detail

    The batch settings control how GPSS commits data to Greenplum. interval_ms is the minimum time to wait between two inserts into the table, 5000 milliseconds by default; max_count, the number of rows to batch before an insert, is 0 by default, which tells GPSS to ignore that trigger. The documentation advises tuning interval_ms and adding max_count only if buffering the messages takes too much memory. A consumer’s position in each partition is a single integer: the offset of the next message it will read. 2 56

    Where it breaks Under a retention by time or by size, a topic discards old events whether they were read or not: a consumer that falls further behind has lost them. A compacted topic keeps at least the last value of each key that was not deleted; a deletion is a tombstone, which is itself cleaned out after delete.retention.ms, 24 hours by default, so a consumer that lags by more than that can miss it.

  5. Merge by key

    In MERGE mode a batch is applied by key: matching rows are updated or deleted, the others inserted. Of several versions of one row in a batch, the order column picks the one to use.

    My part I designed streaming ingestion from MySQL into Greenplum on Kafka, Debezium and GPSS.

    In detail

    MERGE updates a row when columns are listed in update_columns, the match columns of the target table equal those of the input, and an optional update condition is met, deletes it when they are equal and an optional delete condition is met, and inserts the whole input row when its match columns have no counterpart in the table. When order_columns are given, GPSS sorts the input on them and inserts or updates from the input row with the largest value. 5

    Where it breaks Without order_columns, of several input rows with the same key GPSS uses a random one. And the order column chooses among the rows of one batch, not against the row already in the table: a repeat that comes in a later batch is applied like any other change.

  6. The load’s offset

    GPSS keeps the offset of each load job in a history table in Greenplum itself, and resumes a job from its last load offset.

    In detail

    By default GPSS acts as a legacy Kafka consumer and always stores the message offset of each load job in a history table in Greenplum. Given a consumer group, it acts as a high-level consumer; with consistency strong, the default, it stores the offsets both in the history table and in the Kafka broker. gpss resumes a job from the last load offset; jobs submitted to a server that was restarted without a job store have to be submitted again. 7 89

    Where it breaks Resetting the offsets by force when loading may lose or duplicate messages; the documentation keeps those options for an offset that no longer matches the topic.

  7. The Connect cluster

    Workers that share a group.id are one Connect cluster: it balances the work across them, and when a worker is lost it gives its tasks to the workers left, after a rebalance.

    In detail

    In distributed mode Kafka Connect balances the work automatically, can be scaled up or down while it runs, and offers fault tolerance both for the active tasks and for the configuration and offset data. A worker sends heartbeats; if none arrive within session.timeout.ms, 10 seconds by default, the broker removes it from the group and starts a rebalance. 4 10

    Where it breaks The cluster can wait for a departed worker to return for up to scheduled.rebalance.max.delay.ms, 5 minutes by default; during that time its connectors and tasks remain unassigned.

The whole diagram

Streaming into Greenplum

Every component and step of the figure, drawn at once; the page lists them in text.

data control My part

Next section

Infrastructure as Code

Changes to infrastructure and databases go through code and validation.

Infrastructure as Code Data integration