Skip to content

[Fix][Connector-V2] Run the final Databend CDC merge when the writer closes - #12848

Merged
nzw921rx merged 1 commit into
apache:devfrom
goutamadwant:fix/databend-cdc-final-merge
Oct 19, 2026
Merged

nzw921rx merged 1 commit into
apache:devfrom
goutamadwant:fix/databend-cdc-final-merge

Conversation

@goutamadwant

Copy link
Copy Markdown
Collaborator

Purpose of this pull request

Fixes #12847.

In CDC mode the Databend sink writes rows to a raw table and moves them to the target table with MERGE INTO. On Flink 1.15–1.18 the aggregated committer runs in a separate global committer task that drops commits arriving after end of data and is never closed, so if no checkpoint completes after the last insert, the MERGE never runs. DatabendCDCSinkIT fails this way on Flink 1.18 (expected: <3> but was: <0>), and Flink 1.15 was disabled for the same symptom in #11624.

DatabendSinkWriter.close() already tries a final MERGE, but performMerge() checks batchCount, which executeBatch() has just reset to 0, so it never runs.

This PR tracks CDC rows written to the raw table but not yet merged with a flag, so the writer's final MERGE runs when there is something to merge.

The writer and the aggregated committer may now both merge the same stream. That is safe: when several MERGEs read one stream at the same time, one applies the rows and the others merge nothing (tested below).

Before: on Flink 1.15–1.18 the job finishes with the rows in the raw table and an empty target table.
After: the writer merges the remaining rows when it closes, on every engine.

Does this PR introduce any user-facing change?

Bug fix only: on Flink 1.15–1.18, CDC rows written after the last completed checkpoint now reach the target table. No config or API change.

How was this patch tested?

  • DatabendSinkWriterTest: added tests that close() merges after a CDC batch was flushed and skips the merge when nothing was written. The first fails without the fix. 15/15 pass on JDK 11 and 17.
  • DatabendCDCSinkIT with checkpoint.interval = 30000, so no checkpoint completes before the writes (deterministic repro):
    • before (dev): fails on Flink 1.18 with expected: <3> but was: <0>; Flink 1.20, Spark 3.3 and Zeta pass;
    • after: passes on Flink 1.18, Flink 1.20, Spark 3.3 and Zeta.
  • DatabendCDCSinkIT with the default checkpoint.interval = 1000: passes on all four engines.
  • Concurrent merges, on the datafuselabs/databend:nightly image used by the IT: 9 simultaneous MERGEs from one stream, and 8 writers inserting and merging while another session merged every 200 ms. No errors, no duplicate rows, and every row reached the target table.

Check list

…closes

On Flink 1.15-1.18 the aggregated committer drops commits that arrive
after end of data and is never closed, so CDC rows written after the last
completed checkpoint stay in the raw table. The writer's close-time merge
was meant to cover this but never ran, because executeBatch() resets
batchCount before performMerge() checks it. Track unmerged CDC rows with a
separate flag.

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Report

Scope

Yes — this review covers the Apache SeaTunnel Databend CDC writer's final-merge tracking and its focused close-path tests.

Verification

  • Reviewed the PR head 3be4814c0011e6e4f4ee4ab7eda3a13329622e31 against the current dev base.
  • Traced CDC batch execution, batch-count reset, merge eligibility, merge-success state reset, close ordering, and resource cleanup.
  • Reviewed the two added close-path tests for already-flushed CDC rows and an untouched writer.
  • The mandatory Build workflow is still in progress at review time; no completed failing check was available to attribute to this change.
  • No live Databend run was performed because it requires the external CDC sink environment; the behavior is covered by focused mocks and should run in CI.

Findings

High

None found in the reviewed source.

Medium

None found in the reviewed source.

Low

None found in the reviewed source.

Required Fixes

No source changes requested from this review.

Suggested Tests

  • Let the mandatory Build workflow complete on the current head.
  • Run the Databend connector unit tests, including both new close-path cases.
  • In a Databend CDC environment, flush a full batch, close with batchCount == 0, and verify that the raw-table changes are merged into the target table exactly once.

Runtime Before/After

Before: a CDC batch could be successfully flushed to the raw table and reset batchCount to zero, causing close() to skip the final merge even though unmerged changes existed.

After: the writer records successful CDC flushes independently of batchCount, performs the required final merge on close, and clears that marker only after a successful merge.

Backward Compatibility

The change preserves existing CDC SQL and public connector configuration. It corrects the close-time delivery behavior for data already accepted by the raw table.

Risks

The merge marker is writer-local and depends on the existing close lifecycle. The supplied tests cover the key zero-batch-count regression; an end-to-end CDC close test remains useful to validate behavior against a real Databend stream.

Review Status

No source-level blocker found; waiting for the required Build workflow to finish successfully.

Remaining Concerns

Please recheck the mandatory CI result on the current head, especially the Databend connector test suite.

@nzw921rx nzw921rx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 LGTM

Non-blocking follow-up: abortPrepare() should also clear hasUnmergedCdcRows, so a writer closed after an aborted checkpoint does not merge that batch while DatabendSinkAggregatedCommitter is in the aborted state. A small abortPrepare() + close() test would cover it. Cleaning up the aborted raw rows can be a separate issue.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Connector-V2] Databend CDC sink leaves rows unmerged on Flink 1.15-1.18

3 participants