Skip to content

fix(kafka): use explicit transaction checkpoints - #27593

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
Lundomn:fix/issue-27587-kafka-progress-statement-rollback
Aug 26, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
Lundomn:fix/issue-27587-kafka-progress-statement-rollback

Conversation

@Lundomn

@Lundomn Lundomn commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Documentation
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #27587

What this PR does / why we need it:

Kafka scan progress is statement/session state, not MatrixOne transaction
state. The previous transaction-owned queue could publish or discard progress
at the wrong lifecycle terminal and still could not make a Kafka group offset
atomic with MatrixOne data.

This change:

  • finalizes deferred Kafka scan progress once at the whole-statement terminal;
  • removes Kafka progress handling from TxnHandler.Commit and rollback paths;
  • documents that LAST_KAFKA_MESSAGE_ID() is statement/session state;
  • documents that broker group offsets are not MatrixOne transaction checkpoints;
  • provides the supported transaction-consistent workflow: use a Kafka table
    with autocommit=false, lock/read an ordinary checkpoint-table row, write
    destination data, and update that checkpoint with
    LAST_KAFKA_MESSAGE_ID() in the same MatrixOne transaction;
  • rewrites the Kafka E2E transaction case to verify destination rows and the
    explicit checkpoint roll back and commit atomically;
  • adds regressions proving the transaction manager does not own Kafka
    callbacks and covers manual-checkpoint error paths.

Local validation on 63ab44956:

  • affected-package unit tests: pass;
  • affected-package race tests: pass;
  • affected-package vet and full-repo vet: pass;
  • make config, make err-check, and git diff --check: pass;
  • changed executable line coverage: 86.4% overall, 100% production code;
  • isolated Kafka 4.3.1 + patched MatrixOne real E2E: pass, including explicit
    checkpoint rollback/commit atomicity.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify mergify Bot added the kind/bug Something isn't working label Aug 25, 2026
@Lundomn
Lundomn force-pushed the fix/issue-27587-kafka-progress-statement-rollback branch from 6b968c2 to 0ed6cb6 Compare August 25, 2026 17:12
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 25, 2026

@XuPeng-SH XuPeng-SH 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.

Blocking correctness issue: the statement suffix is separated, but the queue is still session-global and is not closed at every real transaction terminal.

TxnHandler.Commit publishes or discards the queue, but two other production paths call commitUnsafe directly:

  1. createUnsafe commits an active transaction before BEGIN creates the replacement transaction (pkg/frontend/txn.go:551).
  2. SetAutocommit commits the active transaction for OFF -> ON (pkg/frontend/txn.go:1197).

Neither path finalizes Kafka progress for the transaction that just became durable. This leaves callbacks from generation N in the queue for generation N+1. A concrete counterexample is:

SET autocommit=0;
<successful Kafka INSERT>;  -- progress belongs to txn N
BEGIN;                      -- createUnsafe commits txn N, but keeps its progress
ROLLBACK;                   -- rolls back txn N+1 and discards txn N progress

I added a temporary white-box regression against this exact lifecycle: the old txn operator had commitCalls == 1, but after the replacement txn rollback the progress callback observed [false]; the required result is [true]. This recreates the same durable-data / missing-LAST_KAFKA_MESSAGE_ID / missing-group-offset split that this PR is meant to eliminate.

The same missing terminal exists when SET autocommit=1 commits an active transaction and a later assignment in the same multi-assignment SET fails: statement cleanup discards progress for data that is already durable. A second temporary regression also produced actual [false], expected [true].

Please bind queued progress to a transaction generation, or centralize progress finalization so every actual commit/rollback terminal handles its own generation exactly once. Add regressions for at least active-txn BEGIN rollover and OFF -> ON autocommit followed by a later SET error, in addition to the current statement-savepoint tests.

The #27587 path itself is correctly fixed and its unit/E2E tests are useful. Performance-wise, the new per-statement mark measured 4.17-4.19 ns/op and 0 allocs/op on Apple M4, so that mutex is not a blocker. FinalizeKafkaProgressFrom is O(k) and copies k callback pointers; the larger pre-existing Kafka-txn risk is retaining one client per queued scan and serial finalization of up to 15 seconds per callback, so total commit latency/resource growth is not transaction-wide bounded. That deserves follow-up/coalescing, but it is not the reason for this request-changes decision.

@aunjgr aunjgr 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.

Reviewed exact head 0ed6cb61499098194dbc6e88d8467af504c08b3c against base 92ea12a6c6ffcb0baa0e1bf5de1fa64d8eac01f3. The statement-suffix fix closes #27587 itself, but the new transaction-owned queue is not finalized at every real transaction terminal.

TxnHandler.Commit publishes/discards the queue around commitUnsafe, but createUnsafe calls commitUnsafe directly before replacing an active transaction, and SetAutocommit does the same for OFF -> ON. Neither path closes the Kafka-progress generation that just became durable.

A deterministic public sequence is:

SET autocommit = 0;
-- successful INSERT ... SELECT from Kafka queues progress for txn N
BEGIN;    -- createUnsafe commits txn N but leaves its progress queued
ROLLBACK; -- rolls back txn N+1 and discards txn N progress

The Kafka rows from txn N are durable while LAST_KAFKA_MESSAGE_ID() and the consumer offset are lost, recreating the exact data/progress split this PR is meant to prevent. The OFF -> ON path has the same generation leak and can later discard already-durable progress on statement failure.

Bind progress to a transaction generation or centralize finalization at the actual commit/rollback ownership boundary so every terminal closes exactly its own generation once. Add regressions through active-transaction BEGIN rollover and OFF -> ON autocommit, including commit failure and a later failure in the same SET statement.

@fengttt fengttt 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.

This is not the right thing to do. Should not manage any kafka state in transaction or anything.

Only supported transaction consistent offset management is via,

autocommit = off
explicit offset management in same transaction in ANOTHER table.

@XuPeng-SH XuPeng-SH 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.

Deep review of exact head 63ab449.

The revised ownership model is correct. Kafka progress is statement/session state and the MatrixOne transaction manager no longer publishes, discards, or rolls it back. A drained source transfers its client/progress to the session only after source-pipeline success; executeStmtWithResponse finalizes the detached queue once after the complete statement and after transaction finalization, while source failure and Session.Close discard it. The transaction workspace converts panics to an execution error before this terminal, so panic/error/cancel paths do not publish a failed scan.

For transaction-consistent consumption, the documented autocommit=false checkpoint-table workflow has the right first owner and commit point: lock the ordinary MO checkpoint row, consume from that durable start id, write destination rows, then update the checkpoint from LAST_KAFKA_MESSAGE_ID() in the same MO transaction. Rollback restores both MO tables; the session last id intentionally remains non-transactional and is not the retry source. The real E2E covers rollback and commit, and the simulator covers the manual-checkpoint error stages.

I also checked multi-statement terminals, transaction commit/rollback paths, queue detach/clear ordering, client release, repeated cleanup, and the existing bounded 15-second autocommit broker commit. No new wait, retry, memory-growth, or log-storm blocker is introduced by this delta. This is a focused correction/removal of misplaced ownership rather than a new architecture requiring a separate design gate; docs/cn/kafka_exttab.md now records the stable contract and limitations.

Validation on this exact head:

  • focused frontend tests passed;
  • simulator success/error tests passed for 20 repetitions;
  • focused frontend race tests passed for 10 repetitions;
  • diff check passed;
  • latest CI is green.

No blocking findings.

@aunjgr aunjgr 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.

Approved at exact head 63ab4495629ce3e23e7d8139b9b7634b9c019cb7 against base 5bc051abc44f34ccb6813e2a0bb5585d5a1db67e.

The revision now follows the recorded ownership contract: Kafka offsets and LAST_KAFKA_MESSAGE_ID() are whole-statement/session state, not MatrixOne transaction state. Source EOF transfers the client exactly once; source/pipeline failure discards locally; successful source reset queues it; the statement terminal publishes or discards after transaction finalization; and session close drains leftovers. Removing all transaction-manager callbacks eliminates the cross-generation ownership problem rather than coordinating another queue state machine.

The documented autocommit=false checkpoint-table workflow gives transaction-consistent consumers the correct durable owner and retry source. Rollback restores destination and checkpoint together while the session last ID intentionally remains non-transactional. The E2E and simulator cover rollback, commit, and manual-checkpoint failure stages. I found no additional correctness, wait, leak, or unbounded-growth blocker.

@mergify mergify Bot added the queued label Aug 26, 2026
@mergify

mergify Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-26 21:58 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-26 22:48 UTC · at de57f6a4e4cbe8c171d9634ced974bac0c4cb002 · squash

This pull request spent 49 minutes 40 seconds in the queue, including 49 minutes 6 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify
mergify Bot merged commit 0f10095 into matrixorigin:main Aug 26, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Aug 26, 2026
fengttt added a commit to fengttt/matrixone that referenced this pull request Aug 27, 2026
Conflict in test/kafkaexttab/kafka_e2e_local_test.go with matrixorigin#27593
(fix(kafka): use explicit transaction checkpoints), which reworked the
simulator's transaction bookkeeping while this branch added error-mode
cases to the same two switches. Both sides are additive: upstream's
ROLLBACK line now clears pendCheckpointSet instead of pendLastSet, and
its kafka_checkpoint query case sits alongside the error-mode cases.
Taken as such, with upstream's rewritten line kept.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_016qdL5akNrbUfXfqjC659n6

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/documentation Improvements or additions to documentation kind/refactor Code refactor kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants