Skip to content

fix(binding-mqtt-kafka): delete the group offsets of a session when it expires - #3267

Merged
jfallows merged 3 commits into
developfrom
fix/3204-mqtt-kafka-expiry-group-offsets
Nov 17, 2026
Merged

jfallows merged 3 commits into
developfrom
fix/3204-mqtt-kafka-expiry-group-offsets

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

When an MQTT v5 session expires, binding-mqtt-kafka removed the session record and the <clientId>#offsets record but left the committed offsets of the consumer group <groupIdPrefix>-<clientId> in Kafka. A client connecting after expiry was handed the backlog of the expired session. The Clean Start path already deletes those group offsets (#3170, #3202); expiry now does the same.

At expiry the signal stream opens a meta stream for each messages topic and for the retained topic, collects the partitions, finds the group coordinator and deletes the group offsets for exactly those partitions. This mirrors the Clean Start exchange of #3202 (meta streams, FindCoordinator, OffsetDelete). The meta and API request streams now report to a small GroupOffsetsResetDelegate interface instead of the session proxy, so the same exchange serves a session and an expiry reset.

Design decisions (settled on the issue's design questions):

  • Partitions: obtained from meta streams opened at expiry, as above.
  • Reconnect race: a connecting clientId waits for an in-flight reset of the same clientId to finish before its session is established, as Clean Start waits for its delete before the offset fetch. The connect waits from the MQTT BEGIN until the reset completes (meta streams, FindCoordinator, OffsetDelete). It can stall for as long as that Kafka exchange runs. If the MQTT stream ends, aborts or is reset while waiting, it stops waiting.
  • Failure: on a coordinator or partition error the delete is skipped and not retried, as Clean Start does. The waiting connect proceeds. A meta, FindCoordinator or OffsetDelete stream that ends, aborts or is reset also completes the reset, so a failed reset does not block a connect indefinitely.
  • v5 only; v3.1.1 has no Session Expiry Interval.

Specification statements (OASIS MQTT v5.0 text downloaded with curl and read from section 3.1.2.11.2 and sections 4.1 to 4.1.1; the rest of the document was not read line by line):

  • 4.1: "The Session State in the Server consists of: ... QoS 1 and QoS 2 messages which have been sent to the Client, but have not been completely acknowledged. QoS 1 and QoS 2 messages pending transmission to the Client ..." In this binding that state is the group offsets of the subscribe streams.
  • 4.1.1 [MQTT-4.1.0-2]: "The Server MUST discard the Session State when the Network Connection is closed and the Session Expiry Interval has passed."
  • 3.1.2.11.2: "If the Session Expiry Interval is absent the value 0 is used. If it is set to 0, or is absent, the Session ends when the Network Connection is closed." and "If the Session Expiry Interval is 0xFFFFFFFF (UINT_MAX), the Session does not expire." Note: the issue cites 3.1.2.11.2 for the discard rule; the normative sentence [MQTT-4.1.0-2] is in 4.1.1.

Kafka protocol documentation (kafka.apache.org/44/design/protocol, read for FindCoordinator (key 10) and OffsetDelete (key 47) request and response schemas and the error code table entries used here, not the whole page):

  • OffsetDelete Request (Version: 0) => { group_id [topics] }, topics => { name [partitions] }, partitions => { partition_index }.
  • Error code GROUP_SUBSCRIBED_TO_TOPIC (86): "Deleting offsets of a topic is forbidden while the consumer group is actively subscribed to it." The documentation does not say more about when a group counts as subscribed.

Test evidence:

  • First commit (tests only): kafka-side scripts for an expiring session (FindCoordinator and OffsetDelete after the expiry signal) and for a connect that arrives while the reset is in flight, with the matching KafkaIT, MqttIT and MqttKafkaSessionProxyIT methods. Run against the unchanged expiry handler, both MqttKafkaSessionProxyIT methods (shouldResetGroupOffsetsOnSessionExpiry, shouldReconnectDuringSessionExpiryGroupOffsetsReset) fail: the server script never sees the meta stream for the reset, and the reconnecting client never connects because no OffsetDelete is issued.
  • Second commit: both pass. Clean verify of runtime/binding-mqtt-kafka (checkstyle, license and coverage checks on): 166 tests, 0 failures, including 51 in MqttKafkaSessionProxyIT. Clean verify of specs/binding-mqtt-kafka.spec with the build cache skipped: KafkaIT 161 and MqttIT 129 tests, 0 failures (run with -Dskip.checks=true; a run with module checks on and the cache skipped stops earlier in the unrelated build module's NOTICE check, which cannot resolve two SNAPSHOT artifacts here).

Fixes #3204

Not delivered

  • The script for the reconnect race pins a connect that arrives while the OffsetDelete request is outstanding, and a session established only after the response. k3po cannot express "no stream opens before X", so a session that did not wait would not be caught by the script alone.
  • No retry or timeout for a Kafka exchange that never answers; the waiting connect stalls until the exchange ends, aborts or is reset.
  • No separate scripts for each coordinator or partition error at expiry; the error paths reuse the Clean Start handling and are covered by the Clean Start scripts only.
  • Multiple Zilla instances receiving the same expiry signal were not tested.
  • v3.1.1 sessions (no expiry interval) are unchanged.

Generated by Claude Code

…ion to be reset

An expiring session must find the partitions of the messages and retained
topics, find the group coordinator of <groupIdPrefix>-<clientId> and delete the
group offsets for exactly those partitions, as the Clean Start path does.

A client that connects while that reset is in flight is established only after
the reset completes.

Both scenarios fail against the current expiry handler, which only writes the
session and session offsets tombstones.

Refs #3204

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWcvTJLQJnx15CWtWu
…t expires

When a session expires, the expiry handler now opens a meta stream per messages
topic and for the retained topic, finds the coordinator of the group
<groupIdPrefix>-<clientId> and deletes the group offsets for exactly the
partitions it learned, mirroring the Clean Start exchange.

A client that connects while the reset is in flight is established after the
reset completes. A coordinator or partition error skips the delete without a
retry and releases the waiting connect.

The meta and API request streams now report to a small delegate interface
instead of the session proxy, so the same exchange serves a session and an
expiry reset.

Refs #3204

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWcvTJLQJnx15CWtWu
…a-expiry-group-offsets

Conflict in MqttKafkaSessionFactory: develop replaced the two-argument
doMqttReset(traceId, EMPTY_OCTETS) calls with a one-argument doMqttReset(traceId)
that carries the implementation specific error reason code. The meta stream now
reports a reset through GroupOffsetsResetDelegate.onPartitionsReset, which the
session proxy implements with the one-argument doMqttReset.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWcvtJLQJnx15CWtWu
@jfallows
jfallows merged commit be57165 into develop Nov 17, 2026
16 checks passed
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.

mqtt-kafka: delete the consumer group offsets of a session when it expires

2 participants