Skip to content

feat(debezium): close CDC gaps when a connector is recreated - #2650

Closed
Arsenii-Malov wants to merge 3 commits into
mainfrom
feature/debezium-gap-recovery
Closed

Arsenii-Malov wants to merge 3 commits into
mainfrom
feature/debezium-gap-recovery

Conversation

@Arsenii-Malov

@Arsenii-Malov Arsenii-Malov commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

ClickUp task: CU-17tkuw5wzkz Debezium: close CDC gaps when a connector is recreated

Why

Every time a Debezium connector is recreated as <base>_vN+1 (recovery or config drift) it gets a clean offset namespace. With snapshot.mode: initial that re-dumps the whole source (≈2.4M MeshCentral events, 60 days of Fleet activities); with no_data (saas-shared #2551) it silently loses everything written while the old version was down.

What

Behind openframe.debezium.recovery.gap.enabled (off unless configured; tenants unaffected):

  • Offset handover. A recreated version is created STOPPED, gets the old version's offsets via PATCH /connectors/{name}/offsets, then resumed: no gap, no snapshot. If the resume fails the new version is deleted so the next tick retries (a STOPPED connector looks healthy to every check).
    • Config drift: the old version is stopped (PUT /stop) before its offsets are read and is resumed again if the recreate fails.
    • A base recreated again within the tracker window starts clean (no_data + backfill from the last known position), so offsets that cause the failure are not carried forever.
  • Gap backfill. When the source history is gone (ChangeStreamHistoryLost, purged binlog) the new version starts no_data, and once it is RUNNING an execute-snapshot (INCREMENTAL) signal is sent over the connector's Kafka signal channel for the outage window only:
    • Mongo: {"_id": {"$gte": ObjectId(<last offset sec − margin>)}} – walks the _id index.
    • MySQL: per-table SQL from the connector spec's backfill map ({from} → FROM_UNIXTIME(<epoch>), right in any session time zone for TIMESTAMP columns); tables without a filter are re-read whole. Runs read.only on GTID, no signal table.
  • Unfilled gaps (backfill expired, no Kafka signal channel, unsupported source) raise a GAP_UNFILLED ConnectorAlert that a healthy tick never resolves; recovery alerts are now scoped to their own types.
  • The signal topic's retention.ms and segment.ms follow backfill-expiry (also enforced on an auto-created topic), so a version started without offsets – which reads the topic from the beginning – does not replay old backfills.
  • Offsets are handed over only when their partition matches the new topic.prefix; otherwise the version starts fresh and the window is backfilled.
  • Failure paths leave one live version: if a backfill cannot be saved the new version is deleted; if a cleanup delete does not remove a STOPPED version, [DEBEZIUM] Connector left STOPPED is logged for the alert. Signal-topic preparation is best-effort, the signal is sent even if the broker refuses the retention change.
  • History-lost errors now go straight to recreation instead of five useless restarts.
  • Checkpoints. Every health tick stores each running connector's offsets in connector_checkpoints; a connector that vanished from Kafka Connect is restored from a fresh checkpoint (handover, or backfill if the position is gone), within the recreation rate limit. Pending backfills live in connector_backfills (survive pod restarts / shedlock moving the tick).
  • Every recreated version runs no_data; the spec's snapshot.mode only governs the very first creation and is ignored in drift comparison.
  • RestTemplate now uses the JDK client (the default HttpURLConnection cannot send PATCH). Built on Spring Boot 4 / Jackson 3 (rebased onto chore(deps): upgrade to Spring Boot 4.1.1, Java 25 and Jackson 3 #2534).

Tests

Module had no tests before. Unit tests for every new class plus ITs against a real quay.io/debezium/connect:3.3.2 (Kafka Connect 4.1.0, same as prod) with Kafka, a Mongo replica set and MySQL 8 with GTID in docker:

  • Mongo window backfill replays only in-window events (op=r);
  • MySQL read-only backfill replays only in-window rows of the filtered table and the whole unfiltered one;
  • offset handover delivers an event written during the outage by streaming, without a snapshot;
  • config drift while the old version lags (Mongo): the new version resumes from the old position, events written meanwhile arrive by streaming, the old version is deleted;
  • config drift on a MySQL spec with snapshot.mode: initial (as Fleet): the new version resumes from the old GTID, no row is snapshotted again (the same test fails with the old recreate path: the table is re-snapshotted);
  • signal publisher creates the topic with the short retention, shortens it on an existing topic, and sends the signal on partition 0 keyed by topic.prefix;
  • real STOPPED create + PATCH offsets round-trip.

Unit tests 117/117 and ITs 9/9 green on JDK 25. PIT on changed lines: 252/252 killed, no survivors, no uncovered mutations (the Kafka send path has unit tests through injectable SignalKafkaClients). Downstream saas-shared (management, infra) and saas-tenant (management, tenant-stream) build and pass their tests against this branch installed as 999-SNAPSHOT.

Rollout

Safe to merge on its own: without the flag the only change in behaviour is the JDK HTTP client towards Kafka Connect. Merge → lib release → bump in flamingo-stack/openframe-saas-shared#2555 (connector signal keys + flag together).

Change-Set: feature-debezium-gap-recovery

Change set flamingo-stack/openframe-oss-lib#2534: these pull requests are one change, reviewed together.

Linked work

  • …and 1 linked item not shown here

Linked by the Depends-On / Change-Set lines in these descriptions; this block is maintained by the hub.

@github-actions

Copy link
Copy Markdown
Contributor

🦩 Flamingo Code Review

Not reviewed yet. Reviews start when you ask, so the reviewer reads this pull request when it is ready, not when it is opened.

When it is ready:

  • Review this pull request

Or comment @flamingo-review. Add the flamingo-review-always label to review every push.

Updated 2026-10-13 10:44 UTC · workflow run

Carry the old version's offsets into the new one (create STOPPED, PATCH
offsets, resume). When the source history is gone, start no_data and
refill only the outage window with an incremental snapshot signalled over
Kafka. Checkpoint offsets every health tick so a vanished connector can be
restored too. Behind openframe.debezium.recovery.gap.enabled.
- Drift recreate stops the old version, hands its offsets to the new one and
  resumes the old version if the recreate fails.
- A base recreated again within the tracker window starts clean (no_data plus
  backfill) instead of carrying offsets that may be the cause.
- A handed-over version that fails to resume is deleted so the next tick retries.
- Unfilled gaps raise a GAP_UNFILLED ConnectorAlert; recovery alerts are scoped
  to their own types so a healthy tick never resolves it.
- Signal topic retention and segment.ms follow backfill-expiry, enforced on an
  existing topic too, so a clean start does not replay old backfills.
- MySQL gap bound is FROM_UNIXTIME(epoch), correct in any session time zone.
- Restore from checkpoint goes through the recreation rate limit.
- A backfill needs the kafka channel enabled, not only its topic.
- Port to Spring Boot 4 / Jackson 3 after the rebase.
…view

- prepare the signal topic best-effort; the signal is sent even if the broker refuses the retention change
- hand offsets over only when their partition matches the new topic.prefix, otherwise start fresh with a backfill
- delete the new version when its backfill cannot be saved, so a failed drift leaves one live version
- log "Connector left STOPPED" when the cleanup delete does not remove the version, for the Grafana rule
- inject the Kafka clients of the signal publisher and cover the send path with unit tests
- add a MySQL IT: a config drift on an initial-snapshot spec resumes from the old GTID without a snapshot
@Arsenii-Malov
Arsenii-Malov force-pushed the feature/debezium-gap-recovery branch from 77dc6a6 to 61a3305 Compare October 22, 2026 17:42
@aliaska-varieva

Copy link
Copy Markdown
Contributor

Closing as stale: no activity for over a week. Reopen if still needed.

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.

2 participants