You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mqtt-kafka: report retained as unsupported and raise an event when the retained topic is not compacted #3158
Retained messages are stored in a Kafka topic keyed by the MQTT topic name, and rely on log compaction to keep only the latest retained message per topic. Without compaction the topic keeps every retained publish: a new subscriber's replay can receive several messages for one topic, and a removal (a tombstone for a zero-byte retained publish) never takes effect.
mqtt-kafka checks compaction only for the sessions topic (it resets the connection with 0x83 and raises an event, MqttKafkaSessionFactory about lines 3316-3330 on develop at 1e802cb). It never checks the retained topic, so with a non-compacted retained topic retained messages silently misbehave.
Decision
Compaction of the retained topic is a hard requirement for correct retained support. When the retained topic is not compacted:
Retained is reported as not supported. For MQTT 5 that is CONNACK Retain Available = 0 (section 3.2.2.3.5; the binding reports it through the capability bit it returns in the session BEGIN). Retained publishes and a will with retain are refused through the existing not-supported paths, and no retained replay is attempted.
An event is raised once (not once per connection) stating the reason, using the existing event pattern of the binding.
If retained support cannot be disabled in some case, the event is still raised and the MQTT client is refused. MQTT 3.1.1 has no way to signal that retained is unsupported, so what happens to a 3.1.1 client is part of the design to be written up here before code.
Expected
Retained topic compacted: no change in behavior.
Retained topic not compacted: the behavior above, for each protocol version.
If the compaction setting cannot be determined, behave as the sessions topic check does today, and say so in the pull request.
Test plan (test-first)
Spec scripts on both sides of mqtt-kafka and in binding-mqtt:
compacted retained topic: unchanged;
not compacted: CONNACK Retain Available 0 (v5), a retained publish refused, a will with retain refused, no retained stream opened on SUBSCRIBE, and the event raised once across several connections;
the MQTT 3.1.1 behavior as decided in the design note.
The first commit contains the failing scripts.
Notes
Related to the retained-message conformance work: the zero-byte tombstone (#3146) and the RETAIN flag on replay (#3152) both assume a compacted retained topic. Part of the MQTT conformance work under #512.
1. Can retained be switched off per connection? Yes, no refusal fallback needed for v5. MqttKafkaSessionFactory returns MQTT_KAFKA_CAPABILITIES (a constant that always includes RETAIN) in the session BEGIN extension, and MqttServerFactory reads it per connection (capabilities = mqttSessionBeginEx.capabilities()). With the RETAIN bit cleared the server already:
v5: sends CONNACK Retain Available = 0;
v5: refuses a retained PUBLISH (disconnect 0x9A) and a will with retain (CONNACK 0x9A);
v3.1.1: reason 0x9A is above the v3.1.1 CONNACK range, so it sends nothing and just closes the network (see onDecodeError).
The retainAvailable in the publish/subscribe factories is the per-publish RETAIN flag / per-filter send-retained flag, not the capability. So mqtt-kafka only needs to stop returning RETAIN_AVAILABLE_MASK when the retained topic is not compacted. One server-side gap: the server still sets the send-retained flag on SUBSCRIBE filters regardless of capabilities, so I will clear it there when retainAvailable(capabilities) is false, so no retained stream is opened (per connection, no change to the subscribe factory).
2. Reading the compaction setting. The sessions topic check reads cleanup.policy from the merged BEGIN reply, but no merged stream to the retained topic exists at session time, and the meta stream carries only partitions (extending it would change binding-kafka, which I will not do). I plan to use a Kafka describe stream (KafkaDescribeBeginEx, topic + cleanup.policy), opened for the retained topic before the session BEGIN reply is built, and compare it against the existing CONFIG_COMPACT_CLEANUP_POLICY. The result is a named value (retained compaction known/compacted) used to pick the capability mask for the connection.
3. Where the "once" is kept. The event must be raised once, not per connection. I found no established "raise an event once" pattern in this repository (event contexts raise on every call); I have asked the parent how to proceed before implementing this part.
4. Protocol versions.
v5: Retain Available 0, retained publish and will-retain refused via the existing paths, no retained replay.
v3.1.1: there is no Retain Available. Existing behavior with the capability cleared is a silent close for a retained publish or a will with retain; a retained subscribe replay is simply not attempted. Whether that, or refusing every v3.1.1 connection, is the intended behavior is the open decision I have asked the parent about before coding the v3.1.1 part.
5. Setting cannot be determined (describe fails/absent cleanup.policy): behave like the sessions topic check today, i.e. treat a missing cleanup.policy=compact as not compacted.
Tests (first commit, failing): mqtt-kafka spec scripts on both sides (compacted: unchanged; not compacted: capability without RETAIN, retained publish and will-retain refused, no retained stream on SUBSCRIBE, event raised once across connections), plus binding-mqtt scripts for the server behavior.
Activity
jfallows commentedon Nov 12, 2026
Design note (before code)
1. Can retained be switched off per connection? Yes, no refusal fallback needed for v5.
MqttKafkaSessionFactoryreturnsMQTT_KAFKA_CAPABILITIES(a constant that always includesRETAIN) in the session BEGIN extension, andMqttServerFactoryreads it per connection (capabilities = mqttSessionBeginEx.capabilities()). With theRETAINbit cleared the server already:onDecodeError).The
retainAvailablein the publish/subscribe factories is the per-publish RETAIN flag / per-filter send-retained flag, not the capability. So mqtt-kafka only needs to stop returningRETAIN_AVAILABLE_MASKwhen the retained topic is not compacted. One server-side gap: the server still sets the send-retained flag on SUBSCRIBE filters regardless of capabilities, so I will clear it there whenretainAvailable(capabilities)is false, so no retained stream is opened (per connection, no change to the subscribe factory).2. Reading the compaction setting. The sessions topic check reads
cleanup.policyfrom the merged BEGIN reply, but no merged stream to the retained topic exists at session time, and the meta stream carries only partitions (extending it would change binding-kafka, which I will not do). I plan to use a Kafka describe stream (KafkaDescribeBeginEx, topic +cleanup.policy), opened for the retained topic before the session BEGIN reply is built, and compare it against the existingCONFIG_COMPACT_CLEANUP_POLICY. The result is a named value (retained compaction known/compacted) used to pick the capability mask for the connection.3. Where the "once" is kept. The event must be raised once, not per connection. I found no established "raise an event once" pattern in this repository (event contexts raise on every call); I have asked the parent how to proceed before implementing this part.
4. Protocol versions.
5. Setting cannot be determined (describe fails/absent
cleanup.policy): behave like the sessions topic check today, i.e. treat a missingcleanup.policy=compactas not compacted.Tests (first commit, failing): mqtt-kafka spec scripts on both sides (compacted: unchanged; not compacted: capability without RETAIN, retained publish and will-retain refused, no retained stream on SUBSCRIBE, event raised once across connections), plus binding-mqtt scripts for the server behavior.
Generated by Claude Code
fix(binding-mqtt-kafka): report retained as unsupported when the reta…
fix(binding-mqtt-kafka): report retained as unsupported when the reta…
fix(binding-mqtt-kafka): report retained as unsupported when the reta…