Repository navigation
Description
Problem
When a re-subscribe to an identical filter replaces the subscription with one of a lower QoS (for example 2 to 0), the per-QoS-level stream that carried the old entry (level 2) can be left with no entries. Frames already received on that stream are dropped by MqttSubscribeStream.onSubscribeData (the stream's subscriptions list is empty, the frame goes to droppedHandler, and doSubscribeComplete acknowledges and commits the offset). Those messages matched the old subscription and have not been delivered.
Read on develop, not run (see the analysis on #3217): the stream's subscriptions list is only read for the "is it empty" guard. encodePublishV4 and encodePublishV5 take it but do not use it. The QoS a frame is encoded at is the stream's own level, and the acknowledgement branch uses the same level. The Subscription Identifiers come from the data extension, which the proxy filled from the old entry. So an in-flight frame on a level stream that still has entries goes out at the old QoS with the old identifier; on a stream that has lost its last entry it is dropped. The same drop exists today for UNSUBSCRIBE.
Specification statements (to be re-read from the OASIS text)
- MQTT 5.0 3.8.4 [MQTT-3.8.4-4]: Application Messages MUST NOT be lost due to replacing the Subscription.
- MQTT 3.1.1 3.8.4 [MQTT-3.8.4-3]: the flow of publications MUST NOT be interrupted when a Subscription is replaced.
- [MQTT-3.8.4-8] (5.0): the QoS of messages sent in response to a Subscription MUST be the minimum of the published QoS and the Maximum QoS granted.
Expected
A message that matched the old subscription and has been received by the server before the replacement takes effect is delivered, not dropped, and no message is delivered twice. Which QoS and Subscription Identifier it carries (old entry or replacement) is part of the design below; [MQTT-3.8.4-8] points at min(published, new granted QoS).
Design first
The replace-by-filter change (#3217) removes the old entry from each level stream and does not handle this case. Candidate directions, none implemented:
- Split the delivery filters from the fetch filters on a level stream, choose the QoS and Subscription Identifier per frame, and change the acknowledgement path, so an in-flight frame goes out at min(published, new granted) against the replacement entry. A change in the delivery and acknowledgement code of the mqtt server.
- Hold the level change on the proxy side until the old stream's received data is delivered, with a signal between the two bindings.
- Keep the replaced entry live on the old stream until its received data is delivered (a drain marker). Keeping a QoS 0 entry on the old stream is not an option as is: the proxy would fetch QoS 2 messages on both levels and duplicate them.
Before any code, post a design that picks one, with the pattern mirrored from an existing implementation in the same modules; if none fits, say so and ask.
Test plan (test-first)
Scripts for v5 and v4 in specs/binding-mqtt.spec (and the mqtt-kafka spec if the proxy changes): a re-subscribe that lowers QoS while a message is in flight on the old level stream (the message is delivered once), the same for a re-subscribe at the same QoS, and the UNSUBSCRIBE case if the design covers it. The first commit holds the failing scripts.
Notes
Follow-up to #3217, which narrows the replacement to the state and stream entries. Found while designing it. Part of the MQTT conformance work under #512.
Activity
fix(binding-mqtt): replace the subscription when a filter is subscrib…
test(binding-mqtt): rename subscribe.reject.missing.packet.id to omit…