Skip to content

Support the KIP-848 consumer group protocol in the Kafka engine - #127757

Open
rorylshanks wants to merge 8 commits into
ClickHouse:masterfrom
rorylshanks:kafka-kip-848
Open

rorylshanks wants to merge 8 commits into
ClickHouse:masterfrom
rorylshanks:kafka-kip-848

Conversation

@rorylshanks

@rorylshanks rorylshanks commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

The Kafka engine did not work with group.protocol=consumer (KIP-848): that protocol's rebalances are always cooperative (only the added or revoked partitions are passed) and librdkafka rejects rd_kafka_assign, which both cppkafka's rebalance handler and KafkaConsumer used. The consumer never got its partitions.

The cppkafka change is in a separate PR; the submodule points to its commit until that is merged.

Related: ClickHouse/cppkafka#9

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The Kafka table engine now supports the next-generation consumer group protocol of Kafka 4.0 (KIP-848). Set <group_protocol>consumer</group_protocol> in the <kafka> configuration to enable broker-managed incremental partition assignment.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127757]

With `group.protocol=consumer` (KIP-848) the rebalance protocol is always cooperative: rebalances pass only
the partitions being added or revoked, and librdkafka rejects `rd_kafka_assign`. Both cppkafka's rebalance
handler and `KafkaConsumer` called it, so a `Kafka` table configured with `<group_protocol>consumer</group_protocol>`
never got its partitions and consumed nothing.

- Bump cppkafka to use `rd_kafka_incremental_assign` / `rd_kafka_incremental_unassign` in the rebalance
  handler under the cooperative protocol.
- `KafkaConsumer` tracks the assignment incrementally, and rewinds with `seek` under the cooperative protocol.
  After a partial revocation, the partitions that stay assigned are rewound to the committed offsets, since the
  current batch is dropped.
- Document `group_protocol`.
- Test it in `test_storage_kafka_sasl`, whose broker is bumped to `cp-kafka:8.0.0` (Kafka 4.0, required by KIP-848).

Related: ClickHouse/cppkafka#9

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization labels Oct 24, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f070db6]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, targeted) FAIL
test_grpc_protocol/test.py::test_ipv6_select_one FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR enables KIP-848 consumption in the Kafka engine through incremental assignment tracking and seek-based rollback. The earlier review findings are addressed, but rollback before a partition's first commit can still lose data with auto_offset_reset=latest. Incremental callbacks were traced through assignment/revocation handling, post-rebalance polling, dirty retries, cancellation, and graceful shutdown; Keeper-backed consumption uses manual assignments without subscribing.

Findings

❌ Blockers

  • src/Storages/Kafka/KafkaConsumer.cpp:434: Rollback must replay discarded rows even before the first checkpoint. For a new group or partition, get_offsets_committed returns OFFSET_INVALID. Seeking to that value reapplies auto.offset.reset; with latest, a failed insert or partial revocation skips the discarded rows. A mock-broker reproducer using the exact bundled librdkafka revision read offsets 0–4, obtained checkpoint -1001, performed the new rollback sequence, and next received newly produced offset 5. Controls using earliest without a checkpoint and latest with checkpoint 0 both replayed 0. Preserve concrete block-start offsets until commit and use them when a broker checkpoint is absent, including for retained partitions after partial revocation.
Tests
  • ⚠️ Add focused coverage with auto_offset_reset=latest and no committed checkpoint: fail the first target insert, recover, and verify all discarded IDs reach the target before subsequent commits. Exercise the same boundary in the unfinished-block partial-revocation case. Existing tests use the default earliest, which masks this loss.
Missing context / blind spots
  • ⚠️ The reproducer validates client rollback against a mock broker, not the complete ClickHouse pipeline. No built ClickHouse binary was available locally, and the retrieved current-head CI reports exposed no results for the new Kafka cases. A focused integration run against the corrected server would close this gap.
Final Verdict

Status: ❌ Block. Preserve concrete rollback offsets when no broker checkpoint exists and add the focused latest regression coverage.

@clickhouse-gh clickhouse-gh Bot added pr-feature Pull request with new product feature submodule changed At least one submodule changed in this PR. labels Oct 25, 2026
Comment thread src/Storages/Kafka/KafkaConsumer.cpp Resolved
Comment thread src/Storages/Kafka/KafkaConsumer.cpp Resolved
Under the cooperative protocol re-subscribing a dirty consumer keeps the
assignment and the fetch positions, so after a failed insert the next block
started after the failed one and its commit skipped it. Rewind to the committed
offsets explicitly instead.

`seek` also keeps the stored offsets, so an offset-less commit could persist
the offsets of rewound messages. Reset them together with the fetch positions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Oct 25, 2026
rorylshanks and others added 3 commits November 3, 2026 22:23
No conflicts. Picks up the fix for the flaky
`05055_format_schema_source_query_background_access` test
(ClickHouse#127797).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
Pin `contrib/librdkafka` to a fix that stops `rd_kafka_app_polled` from
reading `rkcg_flags` and expediting the next heartbeat on the
application thread. The consumer group state is owned by the `librdkafka`
main thread, which writes `rkcg_flags` without a lock in
`rd_kafka_cgrp_handle_ConsumerGroupHeartbeat`, so every poll of a
`group.protocol=consumer` consumer raced with heartbeat responses. This
was reported by ThreadSanitizer in
`test_storage_kafka_sasl/test.py::test_kafka_kip848_consumer_group_protocol`.
The main thread already resumes heartbeats after a poll on its own, based
on the atomic `rk_ts_last_poll`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127757&sha=8a7d4f876f2fb445eabc63d016c5bec72c7f079d&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%20targeted%29
ClickHouse#127757

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
…ocation

The existing KIP-848 test changes the group membership only after all the
produced rows have reached the target, so it would pass without the
rewind of the retained partitions in `KafkaConsumer::doPoll`. Add a
focused case: produce continuously with long blocks, add a second member
right after a commit so that the revocation drops a block that already
holds rows from the partitions the first member keeps (checked via
`KafkaRowsRejected`), and verify that all the rows reach the target after
the following commits. Move the table and wait helpers to the module
level to share them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
rorylshanks and others added 2 commits November 4, 2026 22:55
Replace the timing-based `test_kafka_kip848_partial_revocation_rewinds_unfinished_block`
from 89d7879, which produced continuously with 15 s blocks and relied on the
revocation arriving while a block was being formed.

Add the pausable failpoint `kafka_consumer_pause_mid_block` to `KafkaSource`: it holds
the streaming thread after a poll returned nothing while the block already has rows.
The test produces a fixed batch to all 6 partitions, holds the first table's block once
it has read every row, adds a second group member, and waits until the broker has told
the first member to revoke a part of its partitions (no member owns all 6 any more).
Then it releases the block: the next poll revokes the partitions, the block is dropped
(`KafkaRowsRejected` grows by the whole batch), and every produced id must reach the
target exactly once, which needs the rewind of the retained partitions in
`KafkaConsumer::doPoll`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5v7QTSR65LdYnQEv9HJVu
read_from.push_back(offsets[i]);

if (!read_from.empty())
consumer->seek(read_from, SEEK_TIMEOUT_MS);

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.

[Blocker] Preserve a concrete rollback offset before the first commit

For a new group or a newly added partition, get_offsets_committed returns OFFSET_INVALID until the first successful commit. Passing that value to seek reapplies auto.offset.reset. With <auto_offset_reset>latest</auto_offset_reset>, a failed first insert therefore rewinds to the current end and permanently skips the discarded rows. The same problem affects retained partitions during a partial revocation before their first commit.

I reproduced the exact seek / store_offsets sequence against the bundled librdkafka revision with group.protocol=consumer: after reading offsets 0–4 without committing, the broker checkpoint was -1001; rollback returned no replay, and the next newly produced message was offset 5. Both controls replayed 0: earliest without a checkpoint, and latest with committed offset 0.

Please retain an explicit block-start offset for each polled partition until its block is committed, and use that concrete offset when no broker checkpoint exists, including across partial revocation. Add a focused latest regression; the existing tests use earliest, which masks this case.

@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f070db63d with master f30d7ec4f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

24 translation units recompiled, 100 s compile time in total, 24 of them have a recent master baseline.

Job report

Establish the group's initial `latest` checkpoint before reading a new
partition. Finish each cooperative `kafka_commit_every_batch` insert
before polling again so that a handoff can replay an unfinished insert.

Cover a failed first insert, partition handoff, and group recovery using
an isolated topic for each integration test.
@clickhouse-gh clickhouse-gh Bot added comp-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... and removed comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). labels Nov 19, 2026

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

can be tested Allows running workflows for external contributors comp-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... external Author is not a member of the ClickHouse organization manual approve Manual approve required to run CI pr-feature Pull request with new product feature submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants