Skip to content

fix(binding-mqtt-kafka): keep subscribe offset bookkeeping per client and per stream - #3180

Merged
jfallows merged 4 commits into
fix/3177-mqtt-per-filter-suback-failurefrom
fix/3167-mqtt-kafka-subscribe-offset-maps
Nov 13, 2026
Merged

jfallows merged 4 commits into
fix/3177-mqtt-per-filter-suback-failurefrom
fix/3167-mqtt-kafka-subscribe-offset-maps

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

MqttKafkaSubscribeFactory kept two maps that every client on an engine worker shared. Two clients on one worker interfered with each other's acknowledgement state. The suspicion in #3167 came from reading the code; both findings below are demonstrated by failing tests.

1. High-water marks (highWaterMarks)

One mark per topic partition was shared by every client. When a second client began a subscription, its Kafka begin replaced the first client's mark with a new empty one. Offset commits the first client had deferred were dropped and never sent to Kafka. A commit is deferred when an acknowledgement for a later offset arrives while an earlier offset is still unacknowledged.

The marks are now kept per client: a ClientHighWaterMarks entry keyed by client id, shared by that client's QoS 0, 1 and 2 streams, and removed when the client's last subscribe stream closes. They must stay shared across a client's streams, because the QoS 0 stream advances the mark past offsets the QoS 1 and 2 streams never receive (shouldReceiveMessagesMixtureQos covers this). My first attempt scoped them to one stream and broke that test.

2. Packet id offsets (offsetsPerPacketId)

Counter-assigned packet ids are unique per worker, but ids restored from offset metadata on reconnect are only unique within one client. Two clients restoring the same packet id replaced each other's offset. The first acknowledgement removed the entry the second client had restored, and the second acknowledgement threw NullPointerException: Cannot read field "topicKey" because "offset" is null in onMessageAcked, which terminates the engine worker.

The offsets are now kept by the MqttSubscribeProxy that delivers or restores the packet id and receives its acknowledgement.

Fixes #3167

This PR is stacked on #3191's branch (fix/3177-mqtt-per-filter-suback-failure, 3fd498c5), the tip of the mqtt binding PRs, which sits on develop. It will be retargeted to develop after the PRs below it merge. The stack below it changes MqttKafkaPublishFactory, MqttKafkaSessionFactory, the mqtt server and the spec ITs, not MqttKafkaSubscribeFactory, so the restack onto it was mechanical: no conflicts, and no changes beyond the cherry-picks.

MQTT specification statements

I read these in the full OASIS texts (MQTT v5.0 OS and v3.1.1 OS, downloaded and read as text), not from a summary.

What the fixes serve:

  • Each client is treated independently. v5.0 section 4.3 and v3.1.1 section 4.3 both say: "When the Server is delivering an Application Message to more than one Client, each Client is treated independently." One client's subscription must not change another client's pending acknowledgements, which is what finding 1 broke.
  • Delivery state belongs to the session of one Client Identifier. v5.0 section 4.1: "the Client and Server need to associate state with the Client Identifier, this is referred to as the Session State", and 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." v3.1.1 section 3.1.2.4 describes the same Session state, identified by the Client identifier, for CleanSession 0 ([MQTT-3.1.2-4], [MQTT-3.1.2-5]).
  • Packet identifiers are scoped to a session. v5.0 section 2.2.1: "Packet Identifiers used with PUBLISH, SUBSCRIBE and UNSUBSCRIBE packets form a single, unified set of identifiers separately for the Client and the Server in a Session." The Server must assign "a non zero Packet Identifier that is currently unused" ([MQTT-2.2.1-4]), and "A PUBACK, PUBREC, PUBREL, or PUBCOMP packet MUST contain the same Packet Identifier as the PUBLISH packet that was originally sent" ([MQTT-2.2.1-5]). So packet id 1 may be in use in two clients' sessions at once; a worker-wide map keyed by packet id cannot be correct. This is the anchor for finding 2.
  • Restored packet ids come from the original session. v5.0 section 4.4: when a Client reconnects with Clean Start 0 and a session is present, the Server must resend unacknowledged PUBLISH and PUBREL packets "using their original Packet Identifiers" ([MQTT-4.4.0-1]); v3.1.1 section 4.4 says the same for CleanSession 0 ([MQTT-4.4.0-1]). That is where two clients can legitimately hold the same restored id, because each id was assigned within its own session.
  • Acknowledgement order a client may use. v5.0 section 4.6 and v3.1.1 section 4.6 require a Client to send PUBACKs in the order the PUBLISH packets were received ([MQTT-4.6.0-2]) and PUBRECs in the order received ([MQTT-4.6.0-3]). They do not order a PUBACK against a PUBREC. The first scenario below relies on exactly that: a PUBACK for a later QoS 1 message precedes the PUBREC for an earlier QoS 2 message, which a conforming client may do, and that is what leaves an offset commit deferred.

Where the specification is silent:

  • Commit offsets, high-water marks and deferred commits are Zilla's bookkeeping for Kafka consumer-group progress. The specification does not describe them, so finding 1 rests on the "each Client is treated independently" statement and on correctness of the implementation, not on a rule about offsets.
  • Section 2.2.1 scopes packet ids to a session. It does not say how a server allocates ids across sessions; the per-worker counter that hands out new ids is unchanged and not claimed to be non-conformant.
  • v3.1.1 section 2.3.1 requires a "currently unused Packet Identifier" ([MQTT-2.3.1-2], [MQTT-2.3.1-4]) but, unlike v5.0 section 2.2.1, does not use the words "in a Session". The v3.1.1 anchor for the scoping is section 3.1.2.4 (Session state identified by the Client identifier) together with section 4.3.

Test evidence

Test-first: the first two commits hold the scripts and IT methods only. Run against the unchanged production code on this base (3fd498c5), each scenario fails:

  • shouldKeepPendingOffsetCommitWhenSecondClientBeginsSubscribe: everything up to the early PUBACK and the later PUBREC matches, and the deferred commit progress(0, 4) never reaches Kafka (the test times out).
  • shouldKeepPacketIdOffsetWhenSecondClientRestoresQoS2: the engine worker terminates with the NullPointerException above.

The spec peer-to-peer ITs (MqttIT, KafkaIT) pass for both scenarios, so each script pair agrees with itself.

After the fix, clean verify with checkstyle and license checks on, build cache skipped (-Dmaven.build.cache.skipCache=true), upstream modules installed with -am:

  • specs/binding-mqtt-kafka.spec: 264 ITs (MqttIT 119, KafkaIT 145), 0 failures, plus 27 unit tests
  • runtime/binding-mqtt-kafka: 150 ITs (MqttKafkaSubscribeProxyIT 67, MqttKafkaSessionProxyIT 38, MqttKafkaPublishProxyIT 45), 0 failures, plus 10 unit tests

MqttKafkaSubscribeProxyIT.shouldReceiveServerSentRetainedReset (the known intermittent failure from #3161) did not fail in this run.

An install of the whole upstream reactor with notice checks on fails in the untouched build module (manager and zpm-maven-plugin snapshot licenses unresolved), so the upstream modules were installed with -Dskip.checks=true. The two touched modules were verified with all checks on.

Scripts added (both sides):

  • subscribe.mixed.qos.second.client.begin.keeps.pending.offset.commit (mqtt/ and kafka/): one client has a QoS 2 stream and a QoS 1 stream. It receives the QoS 2 message at offset 2 and the QoS 1 message at offset 3, and sends the PUBACK for the QoS 1 message first, which defers that offset commit. A second client then subscribes. The first client sends the PUBREC for the QoS 2 message, receives the PUBREL and sends the PUBCOMP. Both offset commits are expected to reach Kafka for the first client.
  • subscribe.qos2.second.client.restore.keeps.packet.id.offset (mqtt/ and kafka/): two clients each restore packet id 1 with different offsets and acknowledge it.

Not delivered

  • Within one client, a resubscribe of the same topic still replaces that client's own high-water mark when the new Kafka stream begins (the same put as before). That is the same-client case and was not demonstrated as a defect here.
  • The packetIdCounter is still per worker; it is not client state, and no interference was found there.

Follow-ups

  • Decide whether a client's own resubscribe should keep pending deferred commits.

… while an offset commit is deferred

Client one receives a QoS 2 message at offset 2 and a QoS 1 message at offset 3, and sends the PUBACK
for the QoS 1 message first. That is conforming, because MQTT-4.6.0-2 and MQTT-4.6.0-3 order PUBACKs
and PUBRECs only among themselves. The offset commit for offset 3 is deferred until offset 2 is
acknowledged. Client two then begins a subscription on the same topic and partition, and client one
sends the PUBREC for the QoS 2 message. Both offset commits are expected to reach Kafka for client
one. Currently the deferred commit is never sent.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUEKMNLJThWJnkEvpG
…packet id

Two clients each restore packet id 1 from offset metadata, with different offsets. Each client's
acknowledgement of packet id 1 is expected to commit its own offset. Currently the second client's
restore replaces the first client's offset and the second acknowledgement finds no offset.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUEKMNLJThWJnkEvpG
The subscribe factory kept one high-water mark per topic partition for every client on the worker.
A second client beginning a subscription replaced the mark of the first client and dropped its
deferred offset commits, so those commits were never sent to Kafka. The marks are now shared only by
the streams of one client and released with the client's last stream.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUEKMNLJThWJnkEvpG
Packet ids restored from offset metadata are scoped to one client, but were stored in a map shared
by every client on the worker. Two clients restoring the same packet id replaced each other's
offset, so one acknowledgement committed the wrong offset and the next found none. Offsets are now
kept by the subscribe stream that delivers or restores the packet id and receives its acknowledgement.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUEKMNLJThWJnkEvpG
@jfallows
jfallows changed the base branch from develop to fix/3177-mqtt-per-filter-suback-failure November 13, 2026 01:39
@jfallows
jfallows force-pushed the fix/3167-mqtt-kafka-subscribe-offset-maps branch from 747ecdc to ad8f8df Compare November 13, 2026 01:40
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 01:51
@jfallows
jfallows merged commit 2df852f 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.

mqtt-kafka: per-worker offset maps in the subscribe factory are shared across clients

2 participants