Repository navigation
Do not skip already-consumed Kafka messages when a StorageKafka2 streaming cycle fails - #116485
Conversation
…e fails `KeeperHandlingConsumer::poll` consumes messages one at a time, and every `consume` advances the consumer past the message it returns: `KafkaConsumer2::consume` returns `getNextMessage` straight out of the buffered batch, and `getNextMessage` increments `current` before the allocation that hands the buffer out. The `OffsetGuard` that owns the rollback was constructed only inside `if (message_sink(...))`, after the sink had declared the batch complete, and `poll` had no try/catch. So any throw in between escaped with no guard in existence and nothing rewound the consumer: the default-mode rethrow for an unparseable message, a `MEMORY_LIMIT_EXCEEDED` raised while parsing, or a Keeper failure while saving the intent size. `prepareToPoll` returns early while the assignment is still fresh, so its own rollback did not run on the next cycle either, and the next successful batch committed `last_read_offset + 1` past messages that had been consumed but never delivered to the materialized views. Measured on an unmodified build: after committing keys 0 and 1, feeding one batch of 2, 3, <unparseable>, 4, 5 left the destination table holding [0, 1, 4, 5]. Arm the rollback before the work instead of after it, as `StorageKafka` already does: `KafkaSource::broken` is true from construction and is cleared only in `commit`. `poll` now wraps its consuming loop in a catch-all that rolls back and rethrows. The rollback is unconditional rather than guarded on a consumed-message counter, because the position can advance while that counter is still 0. The rollback wrapper is `noexcept` and takes `LockMemoryExceptionInThread`, because it runs from inside a catch handler where `memoryTrackerCanThrow` is no longer suppressed by `std::uncaught_exceptions`, and because `~OffsetGuard` already ran the rollback during unwinding, where a throw out of a destructor would terminate the server. That destructor now uses the non-throwing variant too. `Kafka2Source` (direct SELECT) and `StorageKafka2` need no change: both reach the same `poll`. `StorageKafka` is unaffected. One user-visible consequence: with the default `kafka_handle_error_mode` an unparseable message now blocks the table instead of being silently skipped. That matches `StorageKafka`, and `kafka_handle_error_mode = 'stream'`, `kafka_handle_error_mode = 'dead_letter_queue'` and `kafka_skip_broken_messages` remain the ways to skip such a message.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings) plus my own cold review
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260826-102000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260826-082300 |
|
Workflow [PR], commit [a65b1fc] Summary: ❌
AI ReviewSummaryThis PR moves the Final VerdictStatus: ✅ Approve LLVM Coverage Report |
|
cc @kssenii @antaljanosbenjamin, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+232.25 KiB total), 1 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units12 translation units recompiled, 47 s compile time in total, 12 of them have a recent master baseline. |
antaljanosbenjamin
left a comment
There was a problem hiding this comment.
I want to check this before merging it.
prepareToPoll repopulates assigned_topic_partitions and resets poll_count to 0 before it rewinds the consumer to the committed offsets. If the rewind throws, the assignment is left non-empty with poll_count == 0, so the next cycle takes the fast path at the top of the function and never retries it: StorageKafka2 only logs an exception escaping prepareToPoll and reschedules, and nothing else clears the assignment. A consumer already advanced past undelivered messages then keeps consuming, and the next successful batch commits past them. That is the window the rollback in poll was added to close. Its fallback clears the assignment precisely so that prepareToPoll rebuilds it and rewinds, which only helps if prepareToPoll does not leave a fresh assignment behind itself. Measured on a debug build with both rollback attempts forced to fail with MEMORY_LIMIT_EXCEEDED, the class the allocations in rollbackToCommittedOffsets raise: without this change two consumed messages were skipped and the two behind them delivered, and the unparseable message was read once. With it the consumer re-read the same batch and delivered nothing past the failure. The guard starts before the repopulation, not just before the rewind, because a throw between the two leaves the same fresh-but-not-rewound assignment.
|
@antaljanosbenjamin heads up before you start: the head moved after your review, so the commit to check is 774a3f2, not 9f72177. The AI reviewer found a second path into the same loss window: |
|
@groeneai Why the checks haven't started? can you merge master to start a CI run? |
|
Merged master and pushed On why nothing ran before: the previous head had no workflow run at all, so there was nothing waiting for approval and nothing to re-run. So the The merge did not change the diff: still the same 3 files, +119/-34, and |
CI finish ledger - 8bf9a13Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260902-183000 |
CI finish ledger - a65b1fcEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Not one of the three is attributable to this diff, which touches Session id: cron:our-pr-ci-monitor:20260903-020000 |
21ec3b2
|
Fixing PR for the
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
Kafkatable withkafka_keeper_pathset (StorageKafka2) no longer skips messages it had already consumed when a streaming cycle fails part-way through; previously those messages were never delivered to the materialized views and a later cycle committed an offset past them. As a consequence, with the defaultkafka_handle_error_modean unparseable message now blocks the table instead of being silently skipped, matchingStorageKafka.kafka_handle_error_mode = 'stream',kafka_handle_error_mode = 'dead_letter_queue'andkafka_skip_broken_messagesremain the ways to skip such a message.Description
Related: #116167, where this defect was found and is out of scope.
KeeperHandlingConsumer::pollconsumes messages one at a time, and eachconsumeadvances the consumer past the message it returns. TheOffsetGuardthat owns the rollback was built only after the message sink declared the batch complete, andpollhad notry/catch, so any throw in between escaped with no guard in existence and nothing rewound the consumer.prepareToPollreturns early while the assignment is fresh, so its own rollback did not run either, and the next successful batch committed past the skipped messages.Measured on an unmodified build: after committing keys 0 and 1, a batch of
2, 3, <unparseable>, 4, 5left the destination holding[0, 1, 4, 5].The fix arms the rollback before the work rather than after it, as
StorageKafkaalready does (KafkaSource::brokenis true from construction and cleared only incommit).pollnow wraps its consuming loop incatch (...) { rollback; throw; }.prepareToPollalso fails closed now: it repopulates the assignment before rewinding, so a throwing rewind left behind an assignment the fast path would poll on; it is cleared instead. The rollback wrapper isnoexceptand takesLockMemoryExceptionInThread, because it runs inside a catch handler where the memory tracker may still raise, and because~OffsetGuardalready ran it during unwinding.Kafka2Source(directSELECT) andStorageKafka2need no change: both go through the same consumer.StorageKafkais unaffected. Not fixed here: with no offset committed in Keeper the rewind resolves through the consumer group's stored offset and thenauto.offset.reset(earliest), so an aborted first batch can be re-read from the start of the partition. That is duplication rather than loss, and it matchesStorageKafka.Most of both hunks is the reindent the
tryblocks force. New regression test:test_system_stop.py::test_kafka2_parse_error_does_not_skip_consumed_messages.Workflow [PR]
Sync PR [sync-upstream/pr/116485]
Version info
26.9.1.576(included in26.9and later)