Skip to content

fix(binding-mqtt-kafka): discard the session offsets record at session expiry - #3184

Merged
jfallows merged 2 commits into
fix/3172-mqtt-kafka-retained-filter-flagsfrom
fix/3170-mqtt-kafka-discard-session-offsets
Nov 13, 2026
Merged

jfallows merged 2 commits into
fix/3172-mqtt-kafka-retained-filter-flagsfrom
fix/3170-mqtt-kafka-discard-session-offsets

Conversation

@jfallows

@jfallows jfallows commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #3170. This PR covers the per-session #offsets record only; resetting the consumer-group offsets is the remaining work (see below).

  • Session expiry removed only the main session record. The <clientId>#offsets record, which holds the per-session QoS 2 packet ids and the producer state, survived, so a later connect with Clean Start=0 (or Clean Session=0) could restore stale inbound QoS 2 packet ids. Expiry now produces a tombstone for <clientId>#offsets (hash key <clientId>) right after the tombstone for the session record, built the same way as the existing session tombstone.
  • Clean Start=1 / Clean Session=1 already replaces the record: the fetched entries are ignored, a new producer is initialized, and a fresh #offsets record with no packet ids is written. A new scenario shows this, passes on unchanged code, and is included as a regression guard.

This PR is based on #3182's branch (fix/3172-mqtt-kafka-retained-filter-flags), the top of the current stack (develop <- #3176 <- #3179 <- #3186 <- #3190 <- #3191 <- #3180 <- #3182). It will be retargeted to develop after the PRs below it merge.

Part of #3170

MQTT specification statements this change serves

I read these sections in the full OASIS texts (MQTT 5.0 OS and MQTT 3.1.1 OS), downloaded and read as text, not through a summarising fetch:

  • v5.0 3.1.2.4 Clean Start, 3.1.2.11.2 Session Expiry Interval, 4.1 Session State, 4.1.1 Storing Session State
  • v3.1.1 3.1.2.4 Clean Session, 4.1 Storing state

What the text says, and how it maps to this change:

  • Session State includes QoS 1 and QoS 2 message state. v5.0 4.1 lists, for the Server: the existence of the Session, the subscriptions, "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", and "QoS 2 messages which have been received from the Client, but have not been completely acknowledged". v3.1.1 3.1.2.4 has the same list (without Subscription Identifiers, the Will Message and the Will Delay Interval). The #offsets record carries the last item: the inbound QoS 2 packet ids that have not been completely acknowledged.
  • Expiry discards that state. v5.0 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 adds that the Session ends when the Network Connection closes if the interval is 0 or absent, that a value of 0xFFFFFFFF means the Session does not expire, and that "When the Session expires the Client and Server need not process the deletion of state atomically", which is why a second, separate tombstone is acceptable. This PR makes expiry remove the inbound QoS 2 packet ids together with the session record.
  • Clean Start=1 discards any existing session. v5.0 3.1.2.4 [MQTT-3.1.2-4]: "If a CONNECT packet is received with Clean Start is set to 1, the Client and Server MUST discard any existing Session and start a new Session". v3.1.1 3.1.2.4 [MQTT-3.1.2-6]: "If CleanSession is set to 1, the Client and Server MUST discard any previous Session and start a new one. This Session lasts as long as the Network Connection. State data associated with this Session MUST NOT be reused in any subsequent Session". The regression-guard scenario checks that a stale #offsets record is not reused: the new session starts with a new producer and no packet ids.
  • MQTT 3.1.1 has no Session Expiry Interval. A v3.1.1 session with CleanSession=0 is stored until it is discarded by other means (v3.1.1 4.1 only says Session state is stored for the entire duration of the Session, and a non-normative comment allows an administrator to discard it). So the expiry tombstone matters for v5.0 sessions and for v3.1.1 CleanSession=1 sessions, which end with the Network Connection. This PR does not touch the 3.1.1 CleanSession=0 handling.

The specifications do not mention a per-session #offsets record: it is an implementation detail of how this binding stores part of the Session State. The change is anchored to the statements above about the State it holds, not to any wording about the record itself.

Not covered by this PR, though it is part of the same Session State per the text above: the consumer-group offsets that hold the subscribe-side QoS 1 and 2 state (see Not delivered).

Test evidence

Test-first: the first commit holds the scripts and IT methods only; the second is the fix.

  • The four existing expiry scenarios that run with the signal stream (session.close.expire.session.state, session.abort.expire.session.state, session.expiry.after.signal.stream.restart, session.ignore.non.fetch.signal.stream.flush) now expect the <clientId>#offsets tombstone on both sides.
  • Two new scenarios feed the expiry signal back through the signal stream and expect both tombstones: session.close.expire.session.offsets.immediately (expiry 0, delay 0) and session.close.expire.session.offsets.large.interval (expiry 3000000, delay 2147483647). The existing .immediately and .large.interval scenarios could not cover this: their ITs run with WILL_AVAILABLE_NAME=false, so no signal stream is opened and expiry never fires. They are unchanged.
  • New scenario session.clean.start.discard.offsets: a stale client#offsets record (packet ids 1 to 3, producer 12345) is served, Clean Start=1 connects, and the Kafka side must see a new producer and a fresh #offsets record without packet ids.
  • All of these have a spec peer-to-peer IT (KafkaIT, MqttIT) and a binding IT in MqttKafkaSessionProxyIT.

Against the unchanged production code on this base (the first commit on top of #3182's head), MqttKafkaSessionProxyIT failed the four existing expiry scenarios and the two new ones, each because the #offsets tombstone never arrives. Zilla produced only the session tombstone (8-byte key, client-1); the zero-delay and large-interval paths do expire the session, they just never discarded the offsets record. Several of those failed on the retry run as well (timeouts). shouldDiscardOffsetsOnCleanStart passed on unchanged code (it also passed on the earlier base b1a53e98f), so no code change was needed for Clean Start. The spec peer-to-peer KafkaIT and MqttIT pass, so the scripts agree with each other.

After the fix, clean verify with checkstyle and license checks on, build cache skipped, for both touched modules (upstream modules were installed beforehand with -am -Dskip.checks=true; a full -am run with checks on stops in the unrelated build module's notice check, which cannot find licenses for manager and zpm-maven-plugin in this environment):

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

The known intermittent MqttKafkaSubscribeProxyIT.shouldReceiveServerSentRetainedReset (see #3161) did not occur in this run; I left its script alone.

The rebase onto #3182 was conflict free. The stack below changes MqttKafkaSessionFactory only in the publish-flush path (null guards on metadata), which does not overlap the expiry signal handler changed here, and none of the scenario scripts touched here. Nothing was changed beyond the mechanical rebase except folding the two expiry scenarios into the tests-first commit.

CI on the PR head e82b3cf33d (base #3182's branch): 17 check runs completed, 16 success and 1 skipped ("Publish build cache", which only runs on develop pushes), including Build (25), CodeQL, Analyze (java) and the mqtt.kafka.proxy and asyncapi.mqtt.kafka.proxy example tests.

Not delivered

Follow-ups

  • Reset the group offsets at expiry (the signal stream will need its own partition lookup).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoXkbbzwjvWSZ2M9zor

The expiry scenarios now expect a tombstone for <clientId>#offsets after the
tombstone for the session record, including two scenarios that feed the
expiry signal back through the signal stream with a zero delay and with the
largest delay. A new scenario shows a Clean Start=1 connect replacing a stale
<clientId>#offsets record.

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

Session expiry now produces a tombstone for <clientId>#offsets together
with the tombstone for the session record.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoXkbbzwjvWSZ2M9zor
@jfallows
jfallows changed the base branch from develop to fix/3172-mqtt-kafka-retained-filter-flags November 13, 2026 01:57
@jfallows
jfallows force-pushed the fix/3170-mqtt-kafka-discard-session-offsets branch from 861dd6f to e82b3cf Compare November 13, 2026 01:59
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 01:59
@jfallows
jfallows merged commit c46feab into develop Nov 13, 2026
17 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.

2 participants