Repository navigation
Fix Kafka engine statistics: broken default and StorageKafka2 override - #128896
yackushevas wants to merge 27 commits into
Conversation
…the broken default StorageKafka2 disabled librdkafka statistics unconditionally, overriding even an explicitly configured statistics_interval_ms, so rdkafka_stat in system.kafka_consumers was always empty for tables with kafka_keeper_path and user opt-in was impossible. Separately, the documented default of 3000 never took effect for any engine: the default was applied with has_property(), but cppkafka's has_property() is implemented via rd_kafka_conf_get(), which reports every known property with its built-in default, so the check was always true and statistics.interval.ms stayed at librdkafka's default (0, disabled) unless configured explicitly. Fix both by pre-seeding the default into the configuration before the user configuration is applied, so an explicit user value (including an explicit 0) always wins. StorageKafka2 pre-seeds 0 for now: unlike StorageKafka, which closes idle consumer handles after kafka_consumers_pool_ttl_ms, StorageKafka2 keeps its consumers alive for the whole storage lifetime, and without an attached materialized view nobody polls them, so statistics events would pile up in the librdkafka reply queue indefinitely (a live memory leak).
…le statistics by default StorageKafka2 kept its librdkafka consumer handles alive for the whole storage lifetime, but only polled them while a materialized view was attached and streaming: with no MV attached, nobody served the reply queue, so statistics and error events would accumulate indefinitely (a live memory leak, slow consumer shutdown). This is why statistics were disabled for StorageKafka2 in the first place. Serve the reply queue from the existing per-consumer background task on cycles that did not stream (no attached views, streaming stopped, dependencies not ready). The consumer is acquired through acquireConsumer(), so direct SELECTs and the drain are mutually exclusive, and tables that were never streamed from are skipped (no handle is created just to poll an empty queue). Partition queues are never forwarded to the reply queue (cppkafka::Consumer::get_partition_queue disables forwarding), so polling an idle consumer cannot consume or drop real messages. With the drain in place, enable statistics by default for StorageKafka2 as well (3000 ms, same as StorageKafka), so system.kafka_consumers.rdkafka_stat is populated for tables with kafka_keeper_path and consumer lag becomes observable.
- statistics enabled by default for both engines (the pre-seeded default; previously the default was dead code due to has_property() semantics) - StorageKafka2 keeps serving rdkafka events after the materialized view is detached (the drain), and continues consuming without losses after the view is re-attached
Both table generators default to the same consumer group. The old engine subscribes to the group and commits offsets to the broker; assign() without an explicit offset (the Keeper-backed engine, before its first Keeper commit) resolves the start position from the broker-committed offset of the group and only falls back to auto.offset.reset when there is none. Sharing the group made which messages the Keeper-backed engine sees timing-dependent on whether the old engine's commit landed first - locally reproducible as a missing message - so give each engine its own group.
|
Workflow [PR], commit [4ce68c2] Summary: ✅
AI ReviewSummaryThe PR honors Kafka statistics configuration, enables statistics by default for Findings❌ Blockers
Tests
Missing context / blind spots
Final VerdictStatus: ❌ Block Preserve callback servicing for direct-read handles retained after detachment, and make the outage/blocked-insert regression assertions prove their required states. These are retained summary findings; no dismissed inline threads were reopened or repeated. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units15 translation units recompiled, 49 s compile time in total, 15 of them have a recent master baseline. |
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
Thank you @yackushevas for a nice PR! I left two small comments inline. Once the open threads of the AI review are addressed, I'll be happy to take another look.
One more personal suggstion, I'd appreciaete if you reduce your comments to align their length with other already exisiting ones.
This review was checked, modified and approved by human (@NIKTONIKTO717).
|
Both AI review findings are addressed in the last three commits and verified out of band. (The review suggestions are answered in their threads; the patch comments are also reduced to the usual length.) 1. Producer statistics without regular servicing — the producer default is back to 2. Statistics stop being drained during Keeper outages — the activation task now drains Added On the producer-side CI test: producer statistics are not observable in |
A producer lives as long as the INSERT, which is not bounded (a client can stream input, an INSERT SELECT can run for hours), and produce() polls the handle only on RD_KAFKA_RESP_ERR__QUEUE_FULL: with a healthy broker and slow input the local queue never fills, so nobody serves the reply queue, and librdkafka enqueues a statistics event on every interval even without a callback attached. The enabled default would thus grow the reply queue for the whole duration of the insert. Keep the "0" default; an explicitly configured value still wins over the pre-seed.
A Keeper outage stops the consumer tasks without closing the Kafka handles, and until activation succeeds nobody polls them: direct reads are rejected by assertActive() and streaming is cancelled. librdkafka keeps emitting statistics on every interval, so the reply queue would grow for the whole duration of the outage. Serve the events from the activation task: it keeps retrying with a bounded backoff, and a single drain invocation flushes an arbitrarily deep backlog (pollEvents() takes max_tries for that). Integration test: rdkafka_stat keeps advancing while Keeper is down and consumption resumes without losses after the outage.
default_statistics_interval_ms was always "3000" for both engines: set the default directly in getConsumerConfiguration() instead, with a comment naming what keeps each engine's reply queue bounded (the consumer pool TTL for StorageKafka, pollIdleConsumerEvents() for StorageKafka2). Shrink the test-only kafka_no_statistics.xml to the minimum while at it.
d54ac80 to
ed862bb
Compare
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
One thing from my previous review is still open: the comments got longer rather than shorter. For example, the NOTE in updateConfigurationFromConfig now repeats what the new comment in getConsumerConfiguration() already says about the pool TTL and pollIdleConsumerEvents(). Could you please shorten them to the length of the existing comments around them?
Once that and the open threads of the AI review are addressed, I'll be happy to take look again.
This review was checked, modified and approved by human (@NIKTONIKTO717).
|
I'd like to encourage you to check out the yackushevas#1 , from what I've seen, that's the amount of comments I'd prefer to see in the final version. Thanks for responding to my previous comments so promtly! |
Set the consumer default directly in getConsumerConfiguration() with a short note on what bounds each engine's reply queue, and drop the producer pre-seed entirely: the librdkafka default (0) is what we want, and the user configuration applies on top of it either way. Update the sample config comment accordingly, shorten the patch comments, and rework the Keeper outage test to use a network partition with an explicit wait for the session expiration instead of killing ZooKeeper containers.
A consumer can be held for a long time without polling: an MV insert blocked on a stalled destination keeps its streaming task inside streamToViews for the whole delivery timeout, and nothing else drains the events queue. Serve such consumers from the activation task on every tick (it already drains all of them while the replica is inactive): polling the events queue needs no ownership, since data lives on detached partition queues and cannot be stolen. drainKafkaEvents and the new pollEventsNonOwning share the deep-drain pollEvents(max_tries). Integration test: statistics keep being served while a blocked MV insert holds the consumer.
|
Both round-2 findings are addressed in the last two commits, which also rework everything to the shape of your suggestion PR (short comments, 1. Busy-but-not-polling — the activation task now serves the events queue of all consumers on every tick, including ones held by a blocked MV insert ( 2. Fixed sleep — replaced with Integration test for the busy-hold case: |
partialShutdown can block on a stalled MV insert for the whole destination delivery timeout; stopping the drain task before it restores the unbounded reply queue this change prevents.
|
Fixed in |
Acquiring an idle consumer can block for up to 30 s on the condition variable if it becomes busy between the check and the acquire, which would stall the drain task and every later slot. The non-owning poll is safe regardless of ownership and never waits.
|
Both addressed in |
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
I think my comment about the 50 ms wait in the idle drain was resolved without a change, and the new drainEventsTask() now adds the same wait on the common schedule pool. Could I please ask you to review that, or answer if you really think it's not relevant?
Once that is addressed, I'll be happy to take a look again.
This review was checked, modified and approved by human (@NIKTONIKTO717).
NIKTONIKTO717
left a comment
There was a problem hiding this comment.
I did a review myself after the agentic one, and added a couple of additional suggestions under the @ch-samuel-reviewer-1 account. Additionally, it's matter of personal taste, but I'd appreciate if you reduce comments in StorageKafka2::pollIdleConsumerEvents and StorageKafka2::drainEventsTask.
…drain pollEvents() gains a wait flag so the periodic drain never blocks on an empty queue (50 ms per call), and the drain in threadFunc() is dropped: the drain task already covers every consumer. Also remove the unused drainKafkaEvents(), rename drain_idle_consumers to enable_statistics_by_default, align the rdkafka_stat column description with the actual defaults, and shorten the comments touched in review.
|
All addressed in |
ch-samuel-reviewer-1
left a comment
There was a problem hiding this comment.
Thank you @yackushevas for addressing all my comments. I left two small nits inline, they are not necessary to be fixed right now in order to get this approved. I think @antaljanosbenjamin could you maybe take a look, please?
This review was checked, modified and approved by human (@NIKTONIKTO717).
…ategory a) The drain task serves every consumer, not only idle ones; category a) now names `startUsing` and `stopUsing` explicitly instead of implying the exemption covers `moveConsumer` too.
|
@antaljanosbenjamin a gentle ping on this Kafka |
|
Filed #133722 tracking the residual direct-read servicing gap discussed in r4501664339 — the AI review's recurring "Major" is that issue. |
|
#133722 closed as not applicable to master (it only exists with the statistics default this PR introduces) — the direct-read servicing gap will be fixed here before merging. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix Kafka engine statistics: the default
statistics_interval_msnever took effect, and the Keeper-backed Kafka engine overrode even an explicit user configuration; the default is now enabled for StorageKafka2 (the old engine stays opt-in).Summary
system.kafka_consumers.rdkafka_statwas always empty, for two independent reasons:KafkaConfigLoaderapplied the default 3000 ms behindif (!config.has_property("statistics.interval.ms")), butcppkafka::Configuration::has_property()is implemented via
rd_kafka_conf_get(), which reports every known property with its built-indefault. The check was therefore always true, and
statistics.interval.msstayed at librdkafka'sdefault (0, disabled) unless the user configured it explicitly.
kafka_config.set("statistics.interval.ms", "0"), added when the engine was introduced becausewithout an attached materialized view nobody polls idle consumers, and unconsumed statistics events
accumulate in the librdkafka reply queue indefinitely (a live memory leak; StorageKafka bounds this
by closing idle handles after
kafka_consumers_pool_ttl_ms, StorageKafka2 keeps them for the wholestorage lifetime).
Change
so an explicit value (including an explicit
0) always wins. Enabled for StorageKafka2 only:the old engine cannot safely drain a held consumer (data and events share one queue).
that polls every consumer non-blockingly (a zero-timeout poll, so an empty queue costs nothing).
Partition queues are never forwarded to the reply queue
(
cppkafka::Consumer::get_partition_queue()disables forwarding), so polling an idle consumercannot consume or drop real messages. With the drain in place, statistics are enabled by default
for StorageKafka2 as well.
insert (the streaming task is stuck in
streamToViews), a Keeper outage (the activation task isstuck in
partialShutdown), and both overlapping. Actively streaming consumers are served byprepareToPolleach cycle.pollEventsis serialized with a per-handle mutex.and the user configuration wins on top either way).
Polling an idle consumer also cannot fall behind: librdkafka serves callback operations
(statistics, logs) inside
rd_kafka_consumer_poll()without consuming the message quota, so a singlepoll drains an arbitrarily deep backlog of statistics events (verified empirically at a 50 ms interval
with 200 partitions:
replyqstays 0 and served timestamps stay current while the view is detached).Motivation
On our production cluster we had no way to observe consumer lag or rdkafka internals:
rdkafka_statwas empty for every table, including explicitly configured ones, and lag had to be inferred from
num_messages_readdeltas andmax(Timestamp)freshness. Consumer problems (broker timeouts,coordinator failures, rebalance storms) had to be diagnosed from exception arrays alone.
Verification
Local end-to-end (native server + Kafka KRaft broker + embedded Keeper), three binaries:
unpatched / commit 1 (config honored) / commit 2 (drain + defaults):
statistics_interval_ms=600configured, MV attachedSYSTEM STOP0Integration tests:
test_storage_kafka/test_statistics.py(default enabled for StorageKafka2;statistics survive MV detach, a Keeper outage, a blocked MV insert, and both overlapping).
How to test
Integration tests:
tests/integration/test_storage_kafka/test_statistics.py(
pytest tests/integration/test_storage_kafka/test_statistics.py::test_statistics_enabled_by_default,::test_statistics_survive_mv_detach,::test_statistics_survive_keeper_outage,::test_statistics_survive_blocked_mv_insertand::test_statistics_survive_blocked_insert_during_keeper_outage).CI Settings (Only check the boxes if you know what you are doing):
Workflow [PR]
Sync PR [sync-upstream/pr/128896]