Skip to content

feat(binding-mqtt-kafka): delete the group offsets of the session on clean start - #3202

Merged
jfallows merged 2 commits into
fix/3199-k3po-read-option-affinityfrom
fix/3170-mqtt-kafka-reset-group-offsets
Nov 13, 2026
Merged

jfallows merged 2 commits into
fix/3199-k3po-read-option-affinityfrom
fix/3170-mqtt-kafka-reset-group-offsets

Conversation

@jfallows

@jfallows jfallows commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #3170 (the first part is #3184). On Clean Start=1 the session now deletes the committed offsets of the consumer group <groupIdPrefix>-<clientId> for the topic partitions it knows, before the new session is established, so a client that asked for a fresh session is no longer sent the backlog of its previous session.

How it works, on the path that already looks up the partitions (publish QoS max 2, the default):

  1. The session proxy fetches the partitions of the messages topics and the retained topic (existing meta streams).
  2. New: if Clean Start is set, it resolves the group coordinator (FindCoordinator v3, affinity 0, so any broker answers) and then sends OffsetDelete v0 for exactly those partitions, grouped by topic, as a raw apiRequest stream whose affinity is the coordinator's broker id from the FindCoordinator response. The client binding connects that stream to that broker (feat(binding-kafka): connect an api request stream to the broker named by its affinity #3198).
  3. Then it fetches the session offsets as before. The begin reply to the client is still gated by the existing session offsets seed, so the reset always finishes before the new session starts.

Failure handling: the exchange is not retried and never fails the connect. A FindCoordinator error, a top-level OffsetDelete error, a per-partition error, a reset (including an unknown broker id), an abort or an unavailable stream all let the session continue. The OffsetDelete response is read to completion and its error codes are not acted on. The version negotiation of the raw API path (apiChallenge answered with apiFlush) is handled as in binding-mcp-kafka.

The code is a private abstract KafkaApiRequestStream with two final subclasses (KafkaFindCoordinatorStream, KafkaOffsetDeleteStream) in MqttKafkaSessionFactory, beside KafkaOffsetFetchStream, plus a small MqttKafkaOffsetDeleteSource that supplies the partitions to the OffsetDelete request codec. binding-mqtt-kafka now depends on binding-kafka (provided scope, requires in module-info), as binding-mcp-kafka does.

This PR is based on #3200's branch (fix/3199-k3po-read-option-affinity), the top of the current stack: #3184 <- #3181 <- #3185 <- #3198 <- #3200. It will be retargeted to develop after the PRs below it merge.

Part of #3170

MQTT specification statements this change serves

(Same sections as #3184: v5.0 3.1.2.4, 3.1.2.11.2, 4.1, 4.1.1 and v3.1.1 3.1.2.4, 4.1, read in the full OASIS texts.)

  • v5.0 4.1: the Server's Session State includes "QoS 1 and QoS 2 messages which have been sent to the Client, but have not been completely acknowledged" and "QoS 1 and QoS 2 messages pending transmission to the Client". In this binding that state is the group offsets of the subscribe streams.
  • v5.0 3.1.2.4 [MQTT-3.1.2-4]: Clean Start=1 means the Server MUST discard any existing Session and start a new Session.
  • v3.1.1 3.1.2.4 [MQTT-3.1.2-6]: CleanSession=1 means MUST discard any previous Session, and state data associated with it MUST NOT be reused in any subsequent Session.

The specifications do not mention consumer groups: they are an implementation detail of where the subscribe-side state lives.

Test evidence

Test-first: the first commit holds the scripts and ITs only; the second is the implementation.

  • Nine existing Clean Start + QoS 2 scenarios (18 script files) now expect FindCoordinator then OffsetDelete after the meta streams: publish.qos2, publish.qos2.no.prior.offsets, publish.qos2.retained, publish.qos2.offset.commit.abort.phase1, .phase2, publish.qos2.offset.fetch.abort, publish.qos2.init.producer.abort, publish.mixture.qos, session.clean.start.discard.offsets.
  • Five new Kafka-side scenarios (10 script files), each paired with the existing MQTT scripts: session.clean.start.reset.group.offsets.negotiated, .partition.error, .group.not.found, .coordinator.not.available, .broker.zero. The per-partition error scenario pins that a partition error from the broker does not stop the session. It does not assert which error code a broker returns for a partition the group never committed.
  • Every scenario with a coordinator names broker 2 (node id 2) in the FindCoordinator response and asserts, with read option zilla:affinity (feat(engine): assert the affinity of a stream with read option zilla:affinity #3200), that the FindCoordinator stream has affinity 0 and the OffsetDelete stream has affinity 2. .broker.zero returns broker 0 and asserts affinity 0.
  • The raw API streams are scripted on the module's duplex accept, so the reply BEGIN extension precedes connected; the real order (reply after request) is exercised by binding-kafka's own tests.

Against unchanged production code (no group offset reset at all, measured earlier on 4dc43db067), 13 binding ITs failed (8 in MqttKafkaPublishProxyIT, 5 in MqttKafkaSessionProxyIT), each because the script expects the raw API stream the binding never opens. After the affinity assertions were added, with the reset implemented but opening the OffsetDelete stream at affinity 0, 12 binding ITs failed on this base (all of the above except .coordinator.not.available, which has no delete stream, and .broker.zero, which expects affinity 0), with "Expected affinity: 0x0000000000000002, but was: 0x0000000000000000". Several also time out on the retry run. The spec peer-to-peer KafkaIT passes throughout, so the scripts agree with each other.

After the implementation, clean verify with checkstyle, license and notice checks on, build cache skipped, both modules (upstream modules installed with -am first):

  • specs/binding-mqtt-kafka.spec: 279 ITs (KafkaIT 155, MqttIT 124), 0 failures, plus 27 unit and schema tests
  • runtime/binding-mqtt-kafka: 160 ITs (MqttKafkaSubscribeProxyIT 69, MqttKafkaSessionProxyIT 46, MqttKafkaPublishProxyIT 45), 0 failures, plus 10 unit tests

Not delivered

  • Session expiry. The reset is not done at expiry. The expiry handler (KafkaSignalStream) has only the client id, so it would need its own meta lookups, coordinator lookup and delete, and the timing against a reconnecting client needs care. Tracked in mqtt-kafka: delete the consumer group offsets of a session when it expires #3204.
  • publish.qos.max below 2. Sessions on such a binding never fetch the partitions, so they do not run the reset: the partitions are not known. Four existing Clean Start scenarios run on that path. Tracked in mqtt-kafka: reset the group offsets on Clean Start when publish.qos.max is below 2 #3205.
  • Broker table (limitations of the coordinator addressing). OffsetDelete is sent to the coordinator by carrying the FindCoordinator response's node id as the affinity of the raw API stream; the client binding connects to the broker with that id, using the route's broker table. The table is per core and is filled by the route's meta stream from the Metadata response (all brokers). The session's own meta streams, which run first on the same core, populate it before the delete runs. Two caveats: a metadata refresh clears and refills the table synchronously, and a core whose cache server fan-out has not served a meta stream yet has no table. An unknown broker id resets the stream with no fallback; the reset is treated like any other failure of the exchange, so the session continues without having deleted the offsets. This was read from the code; no test at the mqtt-kafka level covers the table, because the Kafka side is a script peer there.
  • A reset failure is not retried and not reported; there is no event for it.

Follow-ups

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoXkbbzwjvWSZ2M9zor

…lean start

On a Clean Start the session now asks the group coordinator for the group
(FindCoordinator) and deletes the committed offsets of the group for the
topic partitions it fetched (OffsetDelete v0), as raw api request streams,
before the session offsets are fetched. The qos 2 clean start scenarios
expect the exchange, and new scenarios cover version negotiation, a
partition error, a group that is not found and a coordinator that is not
available.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoXkbbzwjvWSZ2M9zor
…clean start

On a Clean Start, once the partitions of the messages and retained topics are
known and before the session offsets are fetched, the session resolves the
group coordinator (FindCoordinator v3) and deletes the committed offsets of the
group for those partitions (OffsetDelete v0) over raw api request streams, so a
new session does not receive the backlog of the previous one. A failure of
either request is not retried and does not fail the connect.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoXkbbzwjvWSZ2M9zor
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