Skip to content

storage: Halt Kafka sources on responses over receive.message.max.bytes - #41074

Open
martykulma wants to merge 5 commits into
MaterializeInc:mainfrom
martykulma:maz-kafka-silent-retries
Open

martykulma wants to merge 5 commits into
MaterializeInc:mainfrom
martykulma:maz-kafka-silent-retries

Conversation

@martykulma

@martykulma martykulma commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Closes: SS-549

When a broker's fetch response exceeds the reader consumer's receive.message.max.bytes, librdkafka logs the error, drops the connection, and retries the same fetch indefinitely. The reader consumer's client context discarded librdkafka's errors. The metadata consumer kept running though, so the source reported running even though it couldn't read the data from the topic.

Description

Adds a ResponseTooLarge variant to MzKafkaError for librdkafka's "increase receive.message.max.bytes" error.

The Kafka source reader consumer now keeps librdkafka's errors on a channel, which the reader drains on every loop iteration. On a ResponseTooLarge error, the reader reports a halting status. The source shows as stalled with the error and restarts after storage_suspend_and_restart_delay. The restart re-creates the reader consumers, which picks up a raised kafka_source_receive_message_max_bytes.

Verification

Adds an oversized-fetch-response scenario to test/source-sink-errors. It writes a 100,000,000-byte record to an Apache Kafka topic and expects the source status history to show the error. It then raises kafka_source_receive_message_max_bytes and expects the source to ingest the record and return to running. Adds a unit test for parsing the error.

Release notes

This release will report a Kafka source as stalled and restart it when a broker response exceeds the size the source accepts, instead of reporting it as running while it retries indefinitely.

🤖 Generated with Claude Code
🤺 Improved by hooman

@martykulma
martykulma force-pushed the maz-kafka-silent-retries branch from 815d9f3 to 43a44d6 Compare November 25, 2026 23:48
martykulma and others added 3 commits November 30, 2026 08:57
Add a source-sink-errors scenario that writes a record whose fetch
response exceeds librdkafka's default receive.message.max.bytes, and
expects the source status history to show the error. librdkafka
currently retries silently, so the scenario fails until the source
surfaces it.

Co-Authored-By: Claude <noreply@anthropic.com>
librdkafka retries a fetch whose response exceeds
receive.message.max.bytes indefinitely, and the source kept reporting
running. Recognize the error, keep the reader consumer's librdkafka
errors, and report a halting status when it occurs, so the source shows
stalled with the error and restarts.

Closes: SS-549

Co-Authored-By: Claude <noreply@anthropic.com>
…record

Co-Authored-By: Claude <noreply@anthropic.com>
@martykulma
martykulma force-pushed the maz-kafka-silent-retries branch from 43a44d6 to 580ae2c Compare November 30, 2026 13:57
@martykulma
martykulma marked this pull request as ready for review November 30, 2026 14:09
@martykulma
martykulma requested review from a team as code owners November 30, 2026 14:09
@def-

def- commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review

1. MEDIUM -- Each retry rereads the snapshot prefix before an oversized batch

src/storage/src/source/kafka.rs:818

If an existing topic has an oversized batch near the end of its initial snapshot, every restart fetches and processes the readable prefix again. One unchanged batch can keep the broker and storage replica replaying the same backlog until someone raises the limit.

Details

The first timestamp binding covers the entire initial snapshot at time zero (src/storage/src/source/reclock.rs:141). While the reader is stuck before that bound, the export has no committed progress past zero. Restart derives its offsets from that committed frontier (src/storage/src/storage_state/async_storage_worker.rs:178). An established upsert export also rereads its persisted state on each restart (src/storage/src/render/sources.rs:313). Keep the stalled status across metadata refreshes, let librdkafka retry the fetch in place, and restart when the effective size limit changes.

2. MEDIUM -- The wedged reader can discard a detected topic deletion

src/storage/src/source/kafka.rs:822

On a single-worker source, deleting and recreating the topic during the restart delay can leave the table stuck without its terminal deletion error. The metadata fetcher can observe the deletion and close the remap shard while the reader is permanently parked before it can put the error into the table.

Details

The fetcher sends both an empty upstream frontier and a DefiniteError (src/storage/src/source/kafka.rs:2146). The empty frontier closes the remap shard (src/storage/src/source/reclock.rs:136). Only the reader's metadata handler emits the corresponding error row (src/storage/src/source/kafka.rs:704). If the replacement topic has the same partitions and has reached the saved offsets by restart, the new metadata fetcher accepts it. The deletion error is gone and the remap shard can no longer mint timestamps. Keep servicing terminal metadata updates while the fetch is stalled.

3. MEDIUM -- A failed bootstrap handshake can restart a healthy ingestion

src/kafka-util/src/client.rs:200

A bootstrap endpoint that returns an HTTP error during Kafka version negotiation triggers the same halt as an oversized fetch. Even if librdkafka falls back to a healthy bootstrap broker and all partition leaders are available, the queued handshake error restarts the whole source.

Details

The pinned librdkafka runs the Kafka response parser during APIVERSION_QUERY (rdkafka-sys/librdkafka/src/rdkafka_transport.c:752). It interprets the leading HTTP bytes as a length of 1,213,486,160 and emits the matched size diagnostic before it identifies the request (rdkafka-sys/librdkafka/src/rdkafka_broker.c:2213). A reader retains that error across successful fallback, and any worker that sees it can request another restart. Exclude connection-setup failures from the fetch halt decision so bootstrap failover can complete.

4. MEDIUM -- Broker errors accumulate without a bound while the reader is backpressured

src/storage/src/source/kafka.rs:1570

The reader now retains every broker error in an unbounded queue. During a prolonged persist write or upsert rehydration, Kafka's background threads can continue reporting connection failures while the reader cannot drain them. Memory grows with the number of failures across all affected readers.

Details

A restored reader creates its consumer before waiting for rehydration (src/storage/src/source/kafka.rs:421). During ingestion, a persist write can also hold the semaphore required to poll the reader (src/storage/src/render/persist_sink.rs:1621, src/storage/src/source/types.rs:374). The poll timeout applies only once the reader is running. Previously the default context dropped the receiver immediately (src/kafka-util/src/client.rs:72). Filter in the callback and retain just the first pending oversized-response error in a single slot.

- require messsage to contain "in state UP" to ensure error came from Kafka
- bound error channel
@def-

def- commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review

1. MEDIUM -- A full error queue can let the reader skip an oversized batch

src/storage/src/source/kafka.rs:1578

While a reader is backpressured, 64 unrelated broker errors can fill its queue and cause it to drop every oversized-response error for an unread batch. If Kafka retention deletes that batch before the reader drains the queue, an established source can resume at a later offset and report running with records missing.

Details

The reader polls librdkafka before draining the queue (src/storage/src/source/kafka.rs:743, src/storage/src/source/kafka.rs:814), so the error callback delivered on that poll also encounters the full queue. Once retention has advanced, retrying the fetch produces an out-of-range offset. The consumer's auto.offset.reset=earliest setting resumes it at the new low watermark (src/storage/src/source/kafka.rs:1597). Retaining the size error would halt the reader and let the restart detect the expired resume offset (src/storage/src/source/kafka.rs:1752). Reserve a slot for the first pending ResponseTooLarge so unrelated errors cannot crowd it out.

@def-

def- commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review

1. MEDIUM -- Topic deletion lets a historical query return an incomplete count

src/storage/src/source/kafka.rs:838

With one readable record and one oversized record in a partition, wait for the progress source to report offset 2 and issue a count(*) as of that time. Deleting the topic unblocks the query and returns 1 without an error, even though its timestamp includes both records.

Details

After partition discovery, data_cap covers future partitions. The reader holds a separate capability at each known partition’s next offset (src/storage/src/source/kafka.rs:660, src/storage/src/source/kafka.rs:956). With a fixed partition set, the error emitted at data_cap maps to the deletion timestamp. Returning drops the unread partition’s capability and completes the earlier snapshot before the error takes effect (src/timely-util/src/reclock.rs:316). Emit the error at the first unread offset for each affected export before releasing those capabilities.

2. MEDIUM -- Source status recommends raising the fetch limit after the table is permanently sealed

src/storage/src/source/kafka.rs:836

After topic deletion during the restart delay, querying the table reports that the source must be recreated. The source and table entries in mz_internal.mz_source_statuses still tell the operator to increase receive.message.max.bytes, even after the scheduled restart. Raising that limit cannot recover the sealed table.

Details

The normal deletion handler reports the terminal cause for the parent source (src/storage/src/source/kafka.rs:704). This branch only emits the error row. The status view gives the stalled parent’s error precedence over the table’s own error (src/catalog/src/builtin/mz_internal.rs:2416), and restart skips rendering once all exports are finished (src/storage/src/storage_state.rs:831). Ensure the status writer records the parent’s terminal cause before the finished ingestion is torn down.

This branch has not been deployed

No deployments
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