Repository navigation
Description
[Bug]: Kafka progress is discarded by a later failed statement although the earlier Kafka INSERT commits
Summary
Inside an explicit MatrixOne transaction, a successful INSERT ... SELECT FROM an ENGINE=KAFKA external table defers its Kafka progress until transaction commit, as designed. If a later unrelated ordinary statement fails but MatrixOne rolls back only that last statement, a subsequent COMMIT makes the earlier Kafka-derived rows durable. However, the failed statement clears all deferred Kafka progress. LAST_KAFKA_MESSAGE_ID() remains NULL and the consumer-group offset is never committed.
This splits durable data from its consumption progress and breaks the advertised exactly-once chaining contract.
Environment
- MatrixOne SQL endpoint:
127.0.0.1:16001 - Version string:
8.0.30-MatrixOne-v1.3.0 - Running binary source commit:
a3027255760850f891e6dad6be95abfe8c7588f8 - Public
mainchecked at:ae12701a888fac102e9641cc7cbb8706527366d8(2026-08-25 06:31 UTC) - Kafka: 4.3.1, local plaintext broker
127.0.0.1:19092 - External table:
ENGINE=KAFKA,format=jsonl,autocommit=true, one partition - Consumer group, external table, sink table, and error table were globally unique for every repetition.
The relevant frontend source files in public main are byte-for-byte identical to the locally tested source files:
| File | SHA-256 |
|---|---|
pkg/frontend/mysql_cmd_executor.go |
8256af13e7ed44dfd670fdc0a4afb78d20be5ed00091d9f2e8380cdb834cd639 |
pkg/frontend/session_kafka.go |
081ee05415451019b6496eaed455ff21158db48345e597a0c8523d0d701e2f33 |
pkg/frontend/txn.go |
a3e2c67e60cc02dab5bc935edc60359fd8c2fc7119f4da8b8ddc93ada54837c7 |
Minimal prerequisites
Create a Kafka topic with partition 0 containing exactly one committed JSON object compatible with this schema:
{"event_id":"event-0","producer_id":0,"producer_seq":0,"key":"key-0","target_partition":0,"business_group":0,"numeric_value":1,"nullable_value":null,"checksum":1,"batch_id":0,"attempt":"original"}Use a fresh consumer group that has no committed offset.
Minimal reproduction
CREATE DATABASE mo_kafka_progress_repro;
USE mo_kafka_progress_repro;
CREATE EXTERNAL TABLE kafka_src (
event_id VARCHAR(96),
producer_id INT,
producer_seq INT,
`key` VARCHAR(64),
target_partition INT,
business_group INT,
numeric_value BIGINT,
nullable_value BIGINT NULL,
checksum BIGINT,
batch_id INT,
attempt VARCHAR(16)
) ENGINE=KAFKA WITH (
'brokers'='127.0.0.1:19092',
'topic'='<fresh-topic>',
'partition'='0',
'autocommit'='true',
'group'='<fresh-group>',
'format'='jsonl'
);
CREATE TABLE sink (
kafka_offset BIGINT PRIMARY KEY,
event_id VARCHAR(96)
);
CREATE TABLE unrelated (id INT PRIMARY KEY);
INSERT INTO unrelated VALUES (1);
BEGIN;
INSERT INTO sink
SELECT __mo_message_id, event_id
FROM kafka_src
WHERE __mo_read_size=1 AND __mo_read_timeout=5;
-- succeeds, affected rows = 1
INSERT INTO unrelated VALUES (1);
-- expected duplicate-key error 1062
COMMIT;
-- succeeds
SELECT * FROM sink;
-- actual: (0, 'event-0') is durable
SELECT LAST_KAFKA_MESSAGE_ID();
-- actual: NULLThen inspect <fresh-group> with Kafka's read-only consumer-group describe command. The group has no committed offset. The same test without the duplicate-key statement produces LAST_KAFKA_MESSAGE_ID() = 0 and group offset 1.
The executable local harness used for the evidence is kafka_native_progress_loss_min_repro.py. From the QA workspace:
cd /Users/ljy/Documents/Codex/2026-08-20/ce-s
MO_PORT=16001 python3 work/kafka_native_progress_loss_min_repro.py \
--database mo_kafka_native_txn_1787643890_dd94c4d8e9 \
--topic mo-kafka-native-txn-1787643890-b6fce530d704 \
--output work/kafka-native-concurrent-txn-runs/1787643890-b6fce530d704/progress-loss-min-repro.json \
--repetitions 3Expected result
MatrixOne uses statement-level rollback for the later duplicate-key error and permits COMMIT. Therefore the earlier successful Kafka INSERT and its deferred progress must remain paired:
- durable sink rows:
1 LAST_KAFKA_MESSAGE_ID():0- Kafka consumer-group offset:
1
An alternative atomic result would be to abort the whole transaction, leaving both rows and progress absent. Durable data with absent progress is not atomic.
Actual result
Three independent repetitions, each with a new external table, sink, error table, and consumer group:
| Repetition | Control data / last / group | Candidate data / last / group | Split |
|---|---|---|---|
| 1 | 1 / 0 / 1 |
1 / NULL / absent |
yes |
| 2 | 1 / 0 / 1 |
1 / NULL / absent |
yes |
| 3 | 1 / 0 / 1 |
1 / NULL / absent |
yes |
The candidate's durable row was exactly Kafka partition 0 offset 0 and had the independently verified event id mo-kafka-native-txn-1787643890-b6fce530d704-event-5-3 in all repetitions.
A larger four-way audit also reproduced the split three times with 5,000 messages per candidate:
BEGIN; Kafka INSERT; ROLLBACK: correct (0 / NULL / absent)BEGIN; Kafka INSERT; COMMIT: correct (5000 / 5790 / 5791)- two successful Kafka scans followed by
COMMIT: correct (5000 / 5790 / 5791) - successful Kafka scan, later duplicate error, then
COMMIT: incorrect (5000 / NULL / absent)
An independent non-Kafka prerequisite test confirmed normal MatrixOne semantics: BEGIN; successful ordinary INSERT; later duplicate-key error; COMMIT succeeded and preserved the first INSERT. Thus this is not based on an assumption that any statement error must abort the full transaction.
Impact
The next consumer using the same group sees the already-materialized Kafka record again. A retry may fail on a sink uniqueness constraint, require INSERT IGNORE, or duplicate downstream effects. The persisted data and the advertised progress marker cannot be treated as one transaction.
Implementation analysis
Public main still contains this sequence:
- A completed Kafka scan enqueues a transaction-owned progress finalizer.
- On any later statement error,
executeStmtWithResponsecallsses.FinalizeKafkaProgress(false)(pkg/frontend/mysql_cmd_executor.go:4793-4802). FinalizeKafkaProgress(false)takes and clears the entire sessionkafkaProgressQueue, not only entries owned by the failing statement (pkg/frontend/session_kafka.go:61-73).- The transaction rollback path also finalizes the entire queue before deciding whether it will roll back the whole transaction or only call
RollbackLastStatement(pkg/frontend/txn.go:927-965). - For the duplicate-key case, only the failed ordinary statement is rolled back. The earlier Kafka-derived rows remain in the transaction and later commit, but their already-cleared progress finalizer is no longer available to the commit terminal.
The queue needs statement/savepoint ownership so that statement-level rollback discards only Kafka progress created by that failed statement, while preserving progress from prior successful statements still present in the enclosing transaction.
Public issue/PR deduplication
Read-only GitHub searches were performed on 2026-08-25 for Kafka issues/PRs and the exact identifiers/error paths (LAST_KAFKA_MESSAGE_ID, FinalizeKafkaProgress, kafkaProgressQueue, Kafka progress + transaction/rollback/offset). No matching existing bug or fix was found.
- [Feature Request]: Kafka engine for external table. #27518 is the closed feature request defining Kafka external tables and exactly-once chaining.
- PR feat: ENGINE=KAFKA external table and LAST_KAFKA_MESSAGE_ID() #27531 implements the feature and fixed downstream statement failure, full transaction rollback, read-committed isolation, and other reviewed cases. Its transaction regression covers
Kafka INSERT; ROLLBACKandKafka INSERT; COMMIT, but not a later unrelated statement failure followed by commit. - Public
mainatae12701retains the faulty queue-wide discard path. - Permanently excluded prior issues [Bug]: JSON_UNQUOTE drops boundary quotes from JSON string values #27427, [Bug]: nullable VARCHAR CASE aggregate panics with bitmap external storage capacity exceeded #27432/fix(container): bound external null bitmap merge #27444, [Bug]: INTERSECT DISTINCT duplicates set keys under parallel scans #27525/fix(compile): deduplicate parallel distinct set results #27544 are unrelated and were not treated as candidates.
After the local QA and deduplication were complete, this report was submitted as #27587 with explicit user authorization and assigned to Lundomn. No PR was created or modified.
2026-10-11 retest on a different runtime revision
Runtime: local source build 652c643666428f1c434e03369834f2a9bead7c27, Kafka 3.9.1, one partition/replica; not a claim about latest main or the original revision. Three independent fresh topic/group repetitions for each of COMMIT, ROLLBACK, later duplicate-key error followed by COMMIT, and disconnect. Each topic contains exactly one JSON object {event_id:<unique>,delta:1} at offset 0. To avoid the independent JSON projection defect #29856, use all declared data columns: INSERT INTO target SELECT __mo_message_id,event_id,delta FROM external ...; external Kafka autocommit=true. Target contains offset PK, event_id and delta; unrelated error table has PK=1 already inserted.
Results across all three repetitions:
| SQL outcome after successful Kafka INSERT | Durable target rows | LAST_KAFKA_MESSAGE_ID | Broker committed offset |
|---|---|---|---|
| COMMIT | 1 | 0 | 1 |
| unrelated duplicate error 1062 then COMMIT | 1 | 0 | 1 |
| ROLLBACK | 0 | 0 | 1 |
| disconnect | 0 | connection closed | 1 |
The original later-failed-statement symptom was not reproduced at this runtime revision. Current mysql_cmd_executor.go finalizes Kafka progress at statement terminal; its comment explicitly treats progress as statement/session state, rather than transaction state, and recommends storing LAST_KAFKA_MESSAGE_ID in a separate MO checkpoint table in the same explicit transaction. ROLLBACK/disconnect therefore do not roll back broker progress in this implementation; this is a documented current boundary, not asserted here to be a new ACID bug.
One earlier full-field run overlapped the planned MO SIGKILL and its first COMMIT timed out; that availability-contaminated run was excluded. The above 12 cases were rerun on new topic/group/table names after MO recovery and all completed. Initial projected-field runs independently hit #29856 and were not counted as progress results.
Related new sink atomicity/recovery feature with actual Connect and NiFi counterexamples and a scoped successful ledger/checkpoint adapter: #29857. Overall qualification: #29848. This evidence does not automatically close this issue for its original revision or establish end-to-end exactly-once.
Activity
fengttt commentedon Aug 25, 2026
This is not a bug. It is exactly what autocommit = on should behave.
fengttt commentedon Aug 25, 2026
Filling in design rationale.
The external table is designed to exactly match the kafka offset management, nothing less and nothing more. This is the only way to keep tract of upstream behavior (though I don't expect it to change any time soon), and allow people to REASON the ETL/data read behavior is correct. The Kafka says if you have autocommit = on, read from kafka permanently consumed the rows, kafka does not care/know what we do with the read rows, transaction aborted or not. Do not change this.
The the test case in this issue, should look at how the transaction code on MO side is wrong. To be explicit, to get exactly once semantics, we need to
a. select max offset from watermark where topic = ... and partition = ...
b. start reading from external table of this partition, using start offset from the max offset selected.
NOTE: at this moment, kafka will move kafka managed offset to prev read
c. insert into a MO table.
d. insert the new watermark
Note that you can change watermark table, adding more columns, using inplace updates instead of inserting new ones, etc etc.
But the idea is the same.
fengttt commentedon Aug 25, 2026
Related, #27610
Note that the current behavior is CORRECT, same as MySQL. We will add a session variable to control database behavior so that to make writing code a little bit easier.
Lundomn commentedon Sep 17, 2026
补充 QA 转测请求(PR #27593 → Issue #27587)。PR 已合并;Issue 当前为 open、带
phase/testing,负责人是Ariznawlll。此前尚无 QA 结论。本评论请求核验,不代表已通过,也不应据此关闭 Issue。验收口径有待澄清,测试时请明确记录: Issue 的原始复现要求 autocommit=true 的 Kafka INSERT 与 MatrixOne 数据保持进度原子一致;PR 则把 Kafka 进度定义为 statement/session state,并建议使用 autocommit=false + 普通 checkpoint 表的事务工作流。维护者此前评论认为 autocommit=true 下 Kafka 消费进度不随 MatrixOne 事务回滚,这是预期行为。因此请不要仅凭替代工作流通过就判定原始 Issue 已 Fixed。
请在包含 PR #27593 的最新 main 上覆盖并分别报告:
LAST_KAFKA_MESSAGE_ID()和 Kafka group offset,并说明是否符合当前维护者定义的预期。请记录 MatrixOne commit、Kafka 版本、拓扑/端点、重复轮数、实际 SQL/命令和未覆盖项;若失败,附最小复现及错误信息。
Ariznawlll commentedon Oct 27, 2026
测试版本
main;实测二进制 commit:802376360c574f44aea23db5eef3cfcebf0527fd。0f100957038e3adb450ee067000f6ad3f3fe8e76是上述 main 的祖先。autocommit=true的解释,Kafka 位点是语句/会话状态,不随 MO 事务回滚;需要数据与进度事务一致时,使用autocommit=false和同一 MO 事务中的普通 checkpoint 表。原报告中“Kafka group 位点与 MO 事务本身原子回滚”的假设不属于这一口径。原问题验证
每轮写入一条 JSON 记录,外表开启
autocommit=true;事务内执行INSERT INTO sink SELECT __mo_message_id,event_id,delta FROM src WHERE __mo_read_size=1 AND __mo_read_timeout=5,随后向已有主键的普通表再次插入,确认返回重复键 1062,再执行COMMIT。每轮使用两列 JSON 输入(均参与读取),这是保留原问题执行路径的精简复现,不是原报告的完整 11 列数据。3/3 实际结果:首次 Kafka INSERT 影响 1 行;重复键语句返回 1062;提交后 sink 精确为
(0, event_id, 1),LAST_KAFKA_MESSAGE_ID()为0,Kafka consumer group 已提交 offset 为1。没有复现原报告的“sink 已提交但会话位点为 NULL、group 位点缺失”。结果符合维护者定义的语句完成后发布进度语义。Happy Path 覆盖
autocommit=false的受支持检查点工作流:事务内锁定并读取初始 checkpoint-1,从该位置读取 Kafka,写 sink,并以LAST_KAFKA_MESSAGE_ID()更新 checkpoint;COMMIT 后 3/3 为 sink 1 行、checkpoint 0。Unhappy Path 覆盖
autocommit=true下 ROLLBACK:3/3 sink 为 0 行,会话位点仍为 0、broker offset 仍为 1;位点不随 MO 事务回滚属于上述维护者定义,并非跨系统原子性证明。autocommit=true下事务中途断开:3/3 新连接观察 sink 0 行,broker offset 1;原会话已关闭,不能继续读取其会话位点。autocommit=false的检查点工作流回滚与重试:3/3 回滚后 sink 0 行、checkpoint-1;从持久化 checkpoint 重读并提交后为 sink 1 行、checkpoint 0,MO 两张表的修改没有分离。白盒验证
本轮以真实 Kafka 和公开 SQL/客户端行为验收,未运行新增白盒 UT 或 race;相关修复 PR 的研发侧测试不替代本轮黑盒结果。
回归测试
MOTR PR #284 的
issue_27587_progress.py覆盖上述原场景、正常提交、回滚、断连及显式 checkpoint;独立 Kafka 环境下整个18_kafka_source定向运行 3/3 成功。该 suite 为需真实 Kafka 的显式启用测试;未运行完整 MOTR 集成套件。PR 新 head 的 CI 状态以链接页面为准,本次不把离线 CI 当作真实 Kafka 集成测试。测试证据
f513ee7dda4b3737cefc18f33e017b6b708d56cb。./motr --suite 18_kafka_source --repeat 3 --branch main(配置为隔离 MO 与 Kafka);3 次均成功。资源按 CN、TN、Log 顺序退出,测试 Kafka 容器已移除。测试结论
测试通过。