Skip to content

fix(binding-mqtt-kafka): resolve retained replay subscription ids per filter - #3182

Merged
jfallows merged 2 commits into
fix/3167-mqtt-kafka-subscribe-offset-mapsfrom
fix/3172-mqtt-kafka-retained-filter-flags
Nov 13, 2026
Merged

jfallows merged 2 commits into
fix/3167-mqtt-kafka-subscribe-offset-mapsfrom
fix/3172-mqtt-kafka-retained-filter-flags

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

The retained stream (the replay path) kept a de-duplicated list of subscription ids (retainedSubscriptionIds) and indexed the Kafka filter bitmask of each record over that list. The bitmask is aligned with the Kafka filter order, one bit per filter, so the two disagree as soon as two filters share a subscription identifier, or the filters of an open retained stream change:

  • SUBSCRIBE with sensor/one (id 1), sensor/two (id 1), sensor/three (id 2), all with Send Retained. The list is [1, 2]. A retained message matching only sensor/two (bit 1) was delivered with subscription id 2 instead of 1, and one matching only sensor/three (bit 2) was delivered with no subscription id.
  • A filter change on an open retained stream from [one(1)] to [two(2), one(1)] left the list as [1, 2] (it was only appended to), so a retained message matching sensor/two was delivered with id 1 instead of 2.

The messages stream already resolves this correctly with one id and one flags entry per filter, aligned with the Kafka filter order. This change makes the retained stream use that same implementation instead of a copy: the per-filter id and flag lists and buildSubscriptionIds (with matchedSubscriptionId) move into the shared KafkaProxy base class, and both KafkaMessagesProxy and KafkaRetainedProxy use them. The lists are rebuilt on every begin and every flush from the filter list sent to Kafka, as the messages stream does. retainedSubscriptionIds is removed. Retained replay still always sets RETAIN, so Retain As Published and No Local do not change the replay flags; No Local is still applied as a Kafka filter condition through buildFilterConditions.

Fixes #3172

Based on #3180's branch (fix/3167-mqtt-kafka-subscribe-offset-maps, head ad8f8dfcf), which is itself stacked on fix/3177-mqtt-per-filter-suback-failure and develop. Only my two commits sit on top of that head. MqttKafkaSubscribeFactory.java is identical between #3180's previous head (747ecdc34) and its current head, so the restack of this PR was a mechanical rebase with no edits. The base should be retargeted to develop after the PRs below it merge.

MQTT specification

Read in full from the OASIS pages (v5.0 OS and v3.1.1 OS, tags stripped, then read as text). Quotes are from that text.

  • Identifiers on a delivery. v5 3.3.4: "The PUBLISH packet includes the Subscription Identifier carried in the SUBSCRIBE packet, if there was one." For overlapping subscriptions: "If the Client specified a Subscription Identifier for any of the overlapping subscriptions the Server MUST send those Subscription Identifiers in the message which is published as the result of the subscriptions [MQTT-3.3.4-3]"; for a single copy "it MUST include in the PUBLISH packet the Subscription Identifiers for all matching subscriptions which have a Subscription Identifiers, their order is not significant [MQTT-3.3.4-4]"; for several copies "in each of them, the Subscription Identifier of the matching subscription [MQTT-3.3.4-5]". v5 3.3.2.3.8: "Multiple Subscription Identifiers will be included if the publication is the result of a match to more than one subscription". This is what the scripts assert: a message matching only sensor/two carries id 1, one matching only sensor/three carries id 2.
  • Several filters in one SUBSCRIBE. v5 [MQTT-3.8.4-5] (v3.1.1 [MQTT-3.8.4-4]): a SUBSCRIBE with multiple Topic Filters is handled "as if it had received a sequence of multiple SUBSCRIBE packets", so each filter is its own subscription, with its own options and the identifier of the SUBSCRIBE (v5 3.8.2.1.2: the identifier "is associated with any subscription created or modified as the result of this SUBSCRIBE packet"). This is why the bookkeeping has to be per filter.
  • RETAIN on replay. v5 3.3.1.3: "When a new Non-shared Subscription is made, the last retained message, if any, on each matching topic name is sent to the Client ... These messages are sent with the RETAIN flag set to 1." v5 3.8.3.1: "Retained messages sent when the subscription is established have the RETAIN flag set to 1." v3.1.1 [MQTT-3.3.1-8]: "the Server MUST set the RETAIN flag to 1 if a message is sent as a result of a new subscription being made". So replay flags do not depend on the per-filter options, and the "flags of the filters it matched" wording of mqtt-kafka: retained replay filter bookkeeping is not aligned with the per-filter flags of the messages stream #3172 only applies to live delivery (Retain As Published, v5 [MQTT-3.3.1-12], [MQTT-3.3.1-13]); this PR therefore aligns identifiers, not flags.
  • No Local. v5 [MQTT-3.8.3-3]: "Application Messages MUST NOT be forwarded to a connection with a ClientID equal to the ClientID of the publishing connection". Unchanged here; still applied through the Kafka filter conditions.

Statements from #3161 and #3159 that I did not re-verify in this PR (Retain Handling, [MQTT-3.3.1-9] to [MQTT-3.3.1-11]) are not relied on here.

Test evidence

Test-first: the first commit holds the scripts and ITs only (subscribe.retain.shared.subscription.id and subscribe.filter.change.retain.reordered, mqtt and kafka sides, run by MqttIT, KafkaIT and MqttKafkaSubscribeProxyIT). The peer-to-peer spec ITs pass, so the scripts agree with each other. Against #3180's unchanged MqttKafkaSubscribeFactory (ad8f8dfcf), both MqttKafkaSubscribeProxyIT methods fail (2 failures, in the Data extension of the first delivered message: wrong subscription id). With the fix they pass.

After the fix, every module run with clean verify, checkstyle and license checks on, build cache skipped, upstream modules installed with -am first:

  • specs/binding-mqtt-kafka.spec: 268 ITs, 0 failures (+ 27 unit tests)
  • runtime/binding-mqtt-kafka: 152 ITs, 0 failures (+ 10 unit tests)

Not run: other modules; the change is confined to MqttKafkaSubscribeFactory and its scripts. The known intermittent shouldReceiveServerSentRetainedReset failure (see #3161) did not occur in this run.

Not delivered / follow-ups

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoTpNwUipH69JKLSfoQ

@jfallows
jfallows force-pushed the fix/3172-mqtt-kafka-retained-filter-flags branch from de3f4d8 to cb98b40 Compare November 13, 2026 01:49
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 01:51
@jfallows
jfallows merged commit fa81e61 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: retained replay filter bookkeeping is not aligned with the per-filter flags of the messages stream

2 participants