Skip to content

mqtt-kafka: RETAIN flag on retained replay and live delivery does not follow the spec #3152

Closed

Description

@jfallows

Problem

Line numbers are on develop at 1e802cb.

  • Retained replay. MqttKafkaSubscribeFactory (about 2108-2124) sets RETAIN on a replayed message only when Retain As Published was requested for that subscription. MQTT 3.1.1 [MQTT-3.3.1-8] and MQTT 5.0 section 3.3.1.3 require RETAIN=1 on every message sent as the result of a new subscription, whatever the options. A v3.1.1 client, which has no such option, receives RETAIN=0.
  • Live delivery. The messages-topic record carries no marker of the publisher's RETAIN bit (the publish headers are the zilla:* set in MqttKafkaHeaderHelper), and the live data extension's flags are always 0 (about 1343-1353). So Retain As Published=1 cannot be honoured: v5 [MQTT-3.3.1-12] is met only for the RAP=0 half.
  • Retain As Published lookup. In the MQTT server (about 4756-4770) RAP is looked up by subscription identifier; generated identifiers are skipped, the first filter with an identifier is taken, and about 4761 can throw NoSuchElementException when a copy matches two identifiers and the second is not in the stream's list. So RAP works only when the client supplied an explicit Subscription Identifier.

The retained-topic record is compacted and keyed by the MQTT topic name, so its presence already marks a message as retained for replay; no marker is needed for that case.

Expected

  • Replay: RETAIN=1 always, for v3.1.1 and v5, with or without Retain As Published.
  • Live: RETAIN is the publisher's RETAIN bit combined with the matching subscription's Retain As Published; v3.1.1 live messages carry RETAIN=0.
  • The lookup works for generated and client-supplied identifiers and for several filters in one SUBSCRIBE.
  • The publisher's RETAIN bit is carried the way the other record metadata already is: a zilla: header on the messages-topic record, following MqttKafkaHeaderHelper. Records written by older versions have no header and are treated as not retained for live delivery, which is today's behavior.
  • The outgoing RETAIN bit is decided in one place, not computed in the mqtt-kafka binding and stripped again in the MQTT server.
  • Design points (header name, how the decision is split between the two bindings) are written up in this issue before code, per AGENTS.md.

Test plan (test-first)

Spec scripts on both sides of mqtt-kafka and in binding-mqtt, v4 and v5. Existing scripts that assert RETAIN=0 on a replay (for example subscribe.retain.as.published, subscribe.get.retained.as.published, subscribe.replay.retained.message.qos1 and qos2) encode the deviation and are changed because they violate the protocol. At least one scenario must go through mqtt-kafka and not a mocked application that sets RETAIN by hand. The first commit contains the failing scripts.

Notes

Found by a clause-by-clause conformance audit, by reading the code. Part of the MQTT conformance work under #512.

Activity

  1. jfallows commented on Nov 12, 2026

    @jfallows
    ContributorAuthor

    Design

    Where RETAIN is decided: MqttKafkaSubscribeFactory, once. It is the only place that knows both facts the decision needs: which record it is relaying (retained-topic replay vs messages-topic live) and the matching filters' flags, which MqttServerFactory already forwards on the Begin extension (NO_LOCAL, RETAIN_AS_PUBLISHED, SEND_RETAINED) and the mqtt-kafka binding already consumes (no-local filtering, retain-handling). The same split holds for the other options today: the server decodes SUBSCRIBE options into filter flags; mqtt-kafka applies them to the Kafka side and reports the result in the Data extension flags. The server then only encodes the subscribe.flags() it was given. v3.1.1 needs no special case: it has no Retain As Published, so the filter flag is never set and live is RETAIN=0 by the same rule.

    Rule, in mqtt-kafka

    • Retained replay: RETAIN=1 always (v3.1.1 [MQTT-3.3.1-8], v5 3.3.1.3). The retained-topic record being compacted by topic name already marks it as retained, so no marker is read.
    • Live delivery: RETAIN = publisher's RETAIN bit AND RAP of a matching filter (v5 [MQTT-3.3.1-12]). If a copy matches several filters, any filter with RAP=1 enables it, as the server does today.

    Publisher's bit: a new header zilla:retain on the messages-topic record, defined in MqttKafkaHeaderHelper next to zilla:qos and written by MqttKafkaPublishFactory next to the other zilla:* headers only when the publish carries RETAIN. The subscribe side reads it through the helper, so it is not exposed as a user property. Absent header (records written by older versions) means not retained for live delivery, which is today's behavior. Existing records are never rewritten.

    Per-filter RAP: the messages stream currently keeps RAP keyed by subscription id, populated only for filters that send retained messages, and its bitmask index runs over a de-duplicated id list. It will keep one entry of filter flags per filter, aligned with the Kafka filter order, so shared client-supplied ids with different options, generated ids, and several filters in one SUBSCRIBE all resolve. Subscription ids in the Data extension are still de-duplicated.

    MqttServerFactory: the RAP lookup (keyed by id, skipping generated ids, NoSuchElementException) and the RETAIN stripping in encodePublishV5 are removed. It still emits the non-generated subscription ids as v5 properties. encodePublishV4 is unchanged.

    Specs

    • binding-mqtt: v4 and v5 scripts where the application sends RETAIN=1 or 0 in the Data extension flags and the network packet follows it verbatim. Existing replay scripts that assert RETAIN=0 violate the protocol and are corrected.
    • binding-mqtt-kafka: both sides. Publish: zilla:retain header present/absent. Subscribe: replay carries RETAIN with and without RAP; live with RAP=1 and retained publish; live with RAP=0; live with RAP=1 but non-retained publish; no header. At least one v5 scenario through mqtt-kafka with a shared subscription id and two filters.

    No change to engine SPI or binding-kafka, and no shared-subscription code is touched.


    Generated by Claude Code

  2. self-assigned this
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions