Skip to content

fix(binding-mqtt-kafka): set retain on retained replay and on live delivery as the spec requires - #3161

Merged
jfallows merged 3 commits into
fix/3153-mqtt-kafka-retained-replay-conditionsfrom
fix/3152-mqtt-kafka-retain-flag
Nov 12, 2026
Merged

jfallows merged 3 commits into
fix/3153-mqtt-kafka-retained-replay-conditionsfrom
fix/3152-mqtt-kafka-retain-flag

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

The RETAIN bit on PUBLISH packets sent to a subscriber did not follow the spec:

  • Retained replay carried RETAIN only when Retain As Published was requested. v3.1.1 [MQTT-3.3.1-8] and v5 section 3.3.1.3 require RETAIN=1 on every message sent as a result of a new subscription, with any options. A v3.1.1 client received RETAIN=0.
  • Live delivery could not honour Retain As Published=1: the messages-topic record did not carry the publisher's RETAIN bit, so v5 [MQTT-3.3.1-12] was only met for the RAP=0 half.
  • RAP lookup in the MQTT server was keyed by subscription identifier, skipped generated identifiers, took the first filter with that identifier, and could throw NoSuchElementException.

What changed (design as posted on #3152):

  • The outgoing RETAIN bit is now decided once, in MqttKafkaSubscribeFactory. Replay always sets RETAIN. Live delivery sets RETAIN when the publisher's RETAIN bit AND the matching filter's Retain As Published are both set. v3.1.1 live stays RETAIN=0 because that version has no RAP.
  • The publisher's bit is carried as a new zilla:retain header on the messages-topic record (MqttKafkaHeaderHelper, written by MqttKafkaPublishFactory next to the other zilla:* headers). Records without the header (older versions) are treated as not retained for live delivery, as today. Existing records are not rewritten.
  • The messages stream keeps one flags entry per filter, aligned with the Kafka filter order, so shared and generated identifiers and several filters in one SUBSCRIBE resolve. Subscription ids in the Data extension are still de-duplicated.
  • MqttServerFactory no longer looks up RAP or strips RETAIN. It encodes the flags it is given, which removes the lookup bugs rather than patching them.

Fixes #3152

This PR is based on #3159's branch (fix/3153-mqtt-kafka-retained-replay-conditions), which is stacked on #3160 and develop. It will be retargeted to develop after the PRs below it merge.

Mqtt-to-mqtt proxy pipelines

MqttClientFactory copies the RETAIN bit of each PUBLISH received from the upstream broker into the application Data extension flags, and forwards the downstream subscription's Retain As Published option to the broker. So the broker applies Retain As Published and the replay rule, and the MQTT server now forwards that RETAIN bit verbatim. Before this change the server removed RETAIN from every PUBLISH unless a non-generated subscription id matched a Retain As Published filter, which also removed RETAIN from broker replays. None of the mqtt example configs (mqtt.proxy.jwt, asyncapi.mqtt.proxy, mqtt.kafka.proxy, asyncapi.mqtt.kafka.proxy) use retain. No client-binding IT was run for this.

Test evidence

Test-first: the first commit holds the scripts and ITs only. Against the unchanged production code (that commit on top of #3159's head), MqttKafkaSubscribeProxyIT had 6 failures and 1 error (one test failed on both of its two runs: replay without RETAIN, live retained publish with RAP, two filters in one SUBSCRIBE, deferred filter change, resubscribe), MqttKafkaPublishProxyIT had 1 failure (zilla:retain header), and binding-mqtt v5 SubscribeIT.shouldReceiveRetainedAsPublished failed. The spec peer-to-peer ITs passed, so the scripts agree with each other.

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

  • specs/binding-mqtt.spec: 460 ITs, 0 failures, 2 skipped (+ 75 unit and schema tests, 4 skipped)
  • specs/binding-mqtt-kafka.spec: 246 ITs + 27 unit tests, 0 failures
  • runtime/binding-mqtt: 380 ITs, 0 failures, 6 skipped (+ 44 unit tests)
  • runtime/binding-mqtt-kafka: 141 ITs, 0 failures (+ 10 unit tests)

Rebasing onto #3159 produced no textual conflicts. #3159's buildFilterConditions and retained flush sit alongside the per-filter flags list, and nothing beyond the mechanical rebase was changed.

Known intermittent failure (not distinguished from this change)

MqttKafkaSubscribeProxyIT.shouldReceiveServerSentRetainedReset failed once in a full-class run with a bare AssertionError. Measured on the earlier base (83657f92) and on this change's head at that time (23dab5cd): the whole class, 15 runs each, alternated, build cache bypassed, trimStackTrace=false, under CPU load. The base failed 0 of 15 and the head failed 1 of 15, plus 1 failure in 3 earlier full-class runs of the head. Fisher exact p is about 0.5, so the two cannot be told apart, and I never reproduced the failure on the base. The failing stack is the engine-close synthetic reset (Target.doSyntheticReset, sequence=Long.MAX_VALUE, maximum=0) reaching the onMqttReset asserts while the mqtt reply stream is still open, which happens when the client script's own reset has not closed it first. That scenario has no data frames and its scripts are unchanged by this PR. A 400 ms delay on the retained stream's abort and reset handlers in the base did not change the test time, so that experiment is inconclusive. This is not claimed to be pre-existing or fixed.

Scripts changed or added (both sides, v4 and v5):

  • mqtt-kafka mqtt/ and kafka/: subscribe.publish.retained.retain.as.published, subscribe.publish.retained.no.retain.as.published, subscribe.publish.not.retained.retain.as.published, subscribe.publish.retained.retain.as.published.multiple.filters. The first and the last go through mqtt-kafka with the Kafka side writing real records.
  • Existing replay scripts now assert RETAIN where they omitted it or asserted RETAIN=0 on a replay: mqtt subscribe.retain, subscribe.filter.change.retain.resubscribe, subscribe.deferred.filter.change.retain, subscribe.retained.message.expiry.interval.reduced, and network v5 subscribe.get.retained.as.published.
  • Kafka publish.retained, publish.empty.retained.message, publish.qos2.retained, publish.retained.10k now expect the zilla:retain header.
  • binding-mqtt v5 network and application: subscribe.retain.as.published.not.retained.

Not delivered

  • The retained stream still de-duplicates subscription ids and indexes its filter bitmask over that list, so it is not yet aligned with the per-filter handling on the messages stream. I left it alone to keep this PR to the RETAIN bit.
  • No test for a copy matching several filters whose RAP differs beyond the multiple-filters scenario: any matching filter with RAP=1 enables RETAIN, as before.
  • No shared-subscription code is touched (Support MQTT 5 Shared Subscriptions via Kafka share groups #1801).

Follow-ups

  • Align the retained stream's filter bookkeeping with the per-filter handling.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoUCdAYMdhVqQ68F7yT

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: RETAIN flag on retained replay and live delivery does not follow the spec

2 participants