diff --git a/ChangeLog.txt b/ChangeLog.txt index b2ce7b9e..36a3d868 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -8,6 +8,8 @@ Broker: - Fix include_dir not sorting config files before loading. This was partially fixed in 1.5 previously. - Improve documentation around the `include_dir` option. Closes #1154. +- Fix case where old unreferenced msg_store messages were being saved to the + persistence file, bloating its size unnecessarily. Closes #389. Library: - Fix `mosquitto_topic_matches_sub()` not returning MOSQ_ERR_INVAL for diff --git a/src/database.c b/src/database.c index 8253cb41..6596dd23 100644 --- a/src/database.c +++ b/src/database.c @@ -235,6 +235,21 @@ void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **s } +void db__msg_store_compact(struct mosquitto_db *db) +{ + struct mosquitto_msg_store *store, *next; + + store = db->msg_store; + while(store){ + next = store->next; + if(store->ref_count < 1){ + db__msg_store_remove(db, store); + } + store = next; + } +} + + static void db__message_remove(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto_client_msg **msg, struct mosquitto_client_msg *last) { if(!context || !msg || !(*msg)){ @@ -1019,8 +1034,3 @@ void db__limits_set(int inflight, unsigned long inflight_bytes, int queued, unsi max_queued_bytes = queued_bytes; } -void db__vacuum(void) -{ - /* FIXME - reimplement? */ -} - diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 31305841..db96f1f9 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -562,8 +562,8 @@ void db__msg_store_add(struct mosquitto_db *db, struct mosquitto_msg_store *stor void db__msg_store_remove(struct mosquitto_db *db, struct mosquitto_msg_store *store); void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **store); void db__msg_store_clean(struct mosquitto_db *db); +void db__msg_store_compact(struct mosquitto_db *db); int db__message_reconnect_reset(struct mosquitto_db *db, struct mosquitto *context); -void db__vacuum(void); void sys_tree__init(struct mosquitto_db *db); void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time); diff --git a/src/persist.c b/src/persist.c index 13b34d2c..abce7e9a 100644 --- a/src/persist.c +++ b/src/persist.c @@ -151,6 +151,11 @@ static int persist__message_store_write(struct mosquitto_db *db, FILE *db_fptr) stored = db->msg_store; while(stored){ + if(stored->ref_count < 1){ + stored = stored->next; + continue; + } + if(stored->topic && !strncmp(stored->topic, "$SYS", 4)){ if(stored->ref_count <= 1 && stored->dest_id_count == 0){ /* $SYS messages that are only retained shouldn't be persisted. */ @@ -982,6 +987,9 @@ int persist__restore(struct mosquitto_db *db) HASH_DELETE(hh, db->msg_store_load, load); mosquitto__free(load); } + + db__msg_store_compact(db); + return rc; error: err = strerror(errno);