Skip to content

fix(binding-mqtt-kafka): report retained as unsupported when the retained topic is not compacted - #3162

Merged
jfallows merged 5 commits into
fix/3152-mqtt-kafka-retain-flagfrom
fix/3158-mqtt-kafka-retained-topic-compaction
Nov 12, 2026
Merged

jfallows merged 5 commits into
fix/3152-mqtt-kafka-retain-flagfrom
fix/3158-mqtt-kafka-retained-topic-compaction

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

Retained messages rely on log compaction of the retained topic. mqtt-kafka only checked compaction of the sessions topic, so with a non compacted retained topic retained messages silently misbehaved (several replayed messages per topic, tombstones with no effect).

Now, before a session is established, mqtt-kafka reads cleanup.policy of the retained topic with a Kafka describe stream and remembers the result in the binding config (a named capability mask, 0 until read). The decision is made in one place, the capability mask in the session BEGIN extension, and everything else follows from it per connection:

  • Retained topic compacted: no change in behavior.
  • Not compacted: the capability mask drops RETAIN and the event binding.mqtt.kafka.non.compact.retained.topic is raised.
    • MQTT 5: CONNACK Retain Available 0; a PUBLISH with RETAIN=1 is rejected (DISCONNECT 0x9A); a will with retain is rejected (CONNACK 0x9A); a SUBSCRIBE gets no retained replay.
    • MQTT 3.1.1: modelled as if the client had been told retain is unavailable, so the same two rejections apply, which for 3.1.1 is a network close with no server packet (3.1.1 has no server DISCONNECT and 0x9A is outside its CONNACK range), and no retained replay on SUBSCRIBE. 3.1.1 connections are not refused. Everything that does not use retain keeps working in both versions.
  • Setting undeterminable (no cleanup.policy=compact in the describe reply): treated like the sessions topic check, as not compacted. If the describe stream itself aborts or is reset, the connection is aborted/reset like the meta streams and nothing is remembered, so the next connection retries.

binding-mqtt changes: SUBSCRIBE filters no longer carry send-retained when the session reported retain unavailable (v4 and v5), so no retained stream is opened; and a v4 CONNECT with will retain no longer writes a CONNACK return code 0x9A (outside the 3.1.1 range, previously a malformed CONNACK), it closes the connection like the retained PUBLISH path does. The publish factory's own retain handling already derives from the publish begin flags the server sets from the same capability, and a will with retain is refused at CONNECT, so no path writes a retained record when the capability is off.

Event is raised once per worker: each engine worker reads the setting the first time a session connects and remembers it, so with several workers there can be one event per worker, never one per connection. Exactly once across workers would need shared state on the engine level binding, which was deliberately not done.

Based on #3161's branch (fix/3152-mqtt-kafka-retain-flag), stacked on #3159, #3160 and develop; to be retargeted to develop after the PRs below it merge.

Design discussion: #3158 (comment) (decisions on "once" per worker and the 3.1.1 model were taken in the parent discussion and are recorded here).

Fixes #3158

Test evidence

Test first: the spec scripts were committed first and run against the unchanged production code (re-confirmed after the rebase onto #3161's head).

  • runtime/binding-mqtt-kafka MqttKafkaSessionProxyIT: 37 run, 29 errors (the proxy never opened the describe stream the scripts expect).
  • runtime/binding-mqtt ConnectionIT and SubscribeIT (v4 and v5): the new v4/v5 subscribe scenarios failed (send retained still set) and the new v4 will-with-retain scenario failed (a CONNACK 0x9A was written); the new v4 retained PUBLISH scenario already passed.

After the change, verify with checkstyle and license, upstream modules installed, build cache bypassed (-Dmaven.build.cache.skipCache=true):

  • specs/binding-mqtt.spec: 465 tests, 0 failures, 2 skipped
  • specs/binding-mqtt-kafka.spec: 248 tests, 0 failures
  • runtime/binding-mqtt: 384 tests, 0 failures, 6 skipped (existing skips)
  • runtime/binding-mqtt-kafka: 142 ITs + 10 unit tests, 0 failures

New scenarios: session.retained.topic.not.compacted (mqtt-kafka, two connections and the event once), subscribe.retained.not.supported (v4 and v5), publish.reject.retain.not.supported and connect.reject.will.retain.not.supported (v4). The compacted case is covered by the retained describe stream added to the existing session scripts (which also needed the describe inserted after the will signal stream where the signal stream is opened first).

Not delivered / follow-ups

  • Event is once per worker, not once across workers (see above).
  • A will with retain that was already stored before the retained topic was found not compacted is not removed from session state.
  • The events reference page in the separate documentation repository needs an entry for binding.mqtt.kafka.non.compact.retained.topic ("The retained topic (%s) is not log compacted, retained messages are not supported. Update the cleanup policy to enable log compaction."); that repository was not edited.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoV3XVVvSgjE1orC2Yk

Add the retained topic describe stream to the session scripts, a scenario
for a non compacted retained topic raising the event once across two
connections, and binding-mqtt scripts for retained unavailable on v4 and v5.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV3XVVvSgjE1orC2Yk
…ined topic is not compacted

Read cleanup.policy of the retained topic with a describe stream before the
session is established, remember the result per binding and drop the retain
capability and raise an event when the topic is not compacted. The mqtt
server clears send retained on subscribe filters when retain is unavailable
and no longer writes a CONNACK reason code outside the MQTT 3.1.1 range for a
will with retain.

Fixes #3158

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV3XVVvSgjE1orC2Yk
@jfallows
jfallows force-pushed the fix/3158-mqtt-kafka-retained-topic-compaction branch from 7084d0f to 719d335 Compare November 12, 2026 23:10
@jfallows
jfallows changed the base branch from fix/3133-mqtt-v4-clean-session-persistence to fix/3152-mqtt-kafka-retain-flag November 12, 2026 23:11
@jfallows
jfallows added this pull request to stack #3163 November 12, 2026 23:23
@jfallows
jfallows merged commit 5556779 into develop Nov 12, 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: report retained as unsupported and raise an event when the retained topic is not compacted

2 participants