Skip to content

Keep _version monotonic in binlog order so a late DELETE/UPDATE is not lost (#1552) - #1553

Open
koenverwimp wants to merge 2 commits into
Altinity:developfrom
koenverwimp:whs-5968-monotonic-version
Open

koenverwimp wants to merge 2 commits into
Altinity:developfrom
koenverwimp:whs-5968-monotonic-version

Conversation

@koenverwimp

Copy link
Copy Markdown

Fixes #1552 (related: #800, #1346).

Problem

Since 2.10.x the lightweight connector versions a row as source ts_ms * 1e6 + intra-second counter. The MySQL source timestamp is the binlog event header time: the second in which the statement started, not the commit order. A transaction started in second S can commit after one started in S+1; the S+1 event resets the counter, so the late event of second S gets S + small counter, lower than everything already written for S. When that late event is the DELETE (or UPDATE) of a row inserted earlier in S, ReplacingMergeTree keeps the INSERT and the row stays in ClickHouse forever. We found 633 such ghost rows in 23 tables on a production replica (queue-like tables where a row lives milliseconds). The initial snapshot has the same problem (#800): rows are stamped with the processing time at which their table was read, so every change made between snapshot start and that moment ranks below the snapshot row.

With gtid_mode=ON the problem comes back in another form: ClickHouseStruct.calculateVersion switches to SnowFlakeId(ts_ms, gtid), a different numeric domain than the rows written before, and again not monotonic when the source timestamp steps back (the GTID only occupies the low 22 bits).

Fix

  1. DebeziumChangeEventCapture.monotonicVersionTs: within a forward binlog stream (file index + position increasing) the version timestamp never goes below the one used for the previous record. Records without binlog coordinates, and re-deliveries whose coordinates are before the highest ones seen (in-process rewind), keep the current behaviour, so Delete re-insert version mismatch #1346 stays fixed. Unit test MonotonicVersionTsTest (6 cases; 3 fail without the clamp).
  2. ClickHouseStruct.calculateVersion: the lightweight sequence number wins over the GTID-based version, so switching the source to gtid_mode=ON keeps the same formula and domain. Unit test SequenceNumberBeatsGtidVersionTest. The Kafka Connect path (no sequence number) is unchanged.

The emitted formula stays ts_ms * 1e6 + counter, so existing data keeps comparing correctly and upgrades/downgrades remain safe. Running in production since 2026-10-02 on two connectors (MySQL 8.0 / Aurora, with GTIDs on): a full MySQL-vs-ClickHouse comparison of 2,731 tables afterwards showed no differences.

A more robust long-term option would be to derive the version from the binlog position itself, or from the commit timestamp in the GTID event (immediate_commit_timestamp, which the binlog library already parses but Debezium does not expose), instead of the event header time.

…ource has GTIDs

With gtid_mode=ON the snowflake id (timestamp << 22 | gtid) replaced the ts*1e6+counter version:
another numeric domain, not comparable with the rows written before, and not monotonic when the
source timestamp steps back.
@minguyen9988

Copy link
Copy Markdown
Collaborator

please test with the latest 2.11.0, this has been solved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DELETE lost (row stays in ClickHouse) when its binlog timestamp is older than the previous event: _version goes backwards in 2.10.x

2 participants