From fd30dfb86738028fbb4fabd2ed2125324d73e571 Mon Sep 17 00:00:00 2001
From: John Garbutt <john.garbutt@stackhpc.com>
Date: Fri, 17 Dec 2021 18:16:23 +0000
Subject: [PATCH] Set RabbitMQ message TTL and queue expiry

Following ideas here:
https://wiki.openstack.org/wiki/Large_Scale_Configuration_Rabbit

Make sure old messages with no consumer are dropped after the message
TTL of 10 mins, longer than the 1 min RPC timeout.
Also ensure queues expire after an hour of inactivity, so queues from
removed nodes or renamed nodes don't grow over time.

Change-Id: Ifb28ac68b6328adb604a7474d01e5f7a47b2e788
---
 ansible/roles/rabbitmq/defaults/main.yml             |  6 ++++--
 ...essage-ttl-and-queue-expiry-90c16da5e14c0265.yaml | 12 ++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 releasenotes/notes/rabbitmq-set-message-ttl-and-queue-expiry-90c16da5e14c0265.yaml

diff --git a/ansible/roles/rabbitmq/defaults/main.yml b/ansible/roles/rabbitmq/defaults/main.yml
index 46d907bf5..dc807ef59 100644
--- a/ansible/roles/rabbitmq/defaults/main.yml
+++ b/ansible/roles/rabbitmq/defaults/main.yml
@@ -100,9 +100,11 @@ rabbitmq_ha_promote_on_shutdown:
 rabbitmq_server_count: "{{ groups[role_rabbitmq_groups] | length }}"
 rabbitmq_ha_replica_count: "{{ (rabbitmq_server_count | int // 2 + 1) }}"
 # If no TTL is specified, messages will not expire
-rabbitmq_message_ttl_ms:
+# Set max message ttl to 10 mins (more than 1 min rpc timeout)
+# so we don't have queues with no consumers growing unbounded
+rabbitmq_message_ttl_ms: 600000
 # If no queue expiry is specified, queues will not expire
-rabbitmq_queue_expiry_ms:
+rabbitmq_queue_expiry_ms: 3600000
 rabbitmq_extra_config: {}
 
 ####################
diff --git a/releasenotes/notes/rabbitmq-set-message-ttl-and-queue-expiry-90c16da5e14c0265.yaml b/releasenotes/notes/rabbitmq-set-message-ttl-and-queue-expiry-90c16da5e14c0265.yaml
new file mode 100644
index 000000000..3a829b9fa
--- /dev/null
+++ b/releasenotes/notes/rabbitmq-set-message-ttl-and-queue-expiry-90c16da5e14c0265.yaml
@@ -0,0 +1,12 @@
+---
+upgrade:
+  - |
+    In RabbitMQ, messages now have a TTL of 10 minutes and inactive queues will
+    expire after 1 hour. These queue arguments can be changed dynamically at
+    runtime [1], but it should be noted that applying a TTL to queues which
+    already have messages will discard the messages when specific events occur.
+    See [2] for more details. Note that the contents of the RabbitMQ
+    definitions.json are now changed, meaning RabbitMQ containers will be
+    restarted on next deploy/upgrade.
+    [1] https://www.rabbitmq.com/queues.html#optional-arguments
+    [2] https://www.rabbitmq.com/ttl.html#per-message-ttl-caveats
-- 
GitLab