Skip to content

Automatic clean up of retained messages with expired message expiry interval #3221

@asx64

Description

@asx64

Hi,

We are currently using an expiry interval for retained messages with different topics to make sure that unrequired messages get automatically deleted by the broker. We recently noticed that the metrics exposed by the topics "$SYS/broker/store/messages/count" and "$SYS/broker/retained messages/count" increased to unexpected high values although we set message expiry intervals. From tests / observations we conducted we conclude that expired retained messages are currently only removed if new subscriptions on their topics are created by clients.
In our usage scenario this behavior currently causes a constantly increasing retained message count and of course memory amount. Thus, as a workaround we consider to call mosquitto_sub -t '#' regularly to trigger the message clean up.

Is there a mosquitto setting, we missed, that triggers regularly an automatic clean up of expired retained messages?

Steps to reproduce our observations

  1. starting mosquitto broker version 2.0.20 with default settings;
  2. observation of metric "$SYS/broker/store/messages/count" with mosquitto_sub -t '$SYS/broker/store/messages/count';
  3. initially metric reports 51 stored messages;
  4. publishing 10 retained messages with an expiry interval of 10 seconds with following bash script:
for i in $(seq 1 10);
do
    mosquitto_pub -t "/test/$i" -r -m test -D publish message-expiry-interval 10 -V 5
done
  1. the metric increases to 61 (as expected); nevertheless, stays at this value even after 10 seconds;
  2. calling mosquitto_sub -t '/test/1' reduces the the message count to 60;
  3. calling mosquitto_sub -t '#' decreases the count to 51 - the initial value;

Related code lines

The removal of expired retained messages seems to take place in the code at following lines:

mosquitto/src/retain.c

Lines 161 to 166 in 7f50849

if(branch->retained->message_expiry_time > 0 && db.now_real_s >= branch->retained->message_expiry_time){
db__msg_store_ref_dec(&branch->retained);
branch->retained = NULL;
#ifdef WITH_SYS_TREE
db.retained_count--;
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: mosquitto-brokerStatus: CompletedNothing further to be done with this issue, it can be closed by the requestor or committer.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions