Merge ~cjwatson/launchpad:charm-rabbitmq-ha into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: d81b56720777b0037f5112f9d9fe1e4d4ee85866
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:charm-rabbitmq-ha
Merge into: launchpad:master
Diff against target: 76 lines (+21/-14)
3 files modified
charm/layer/launchpad-base/reactive/launchpad-base.py (+20/-8)
charm/layer/launchpad-base/templates/launchpad-base-lazr.conf (+0/-5)
charm/layer/launchpad-base/templates/launchpad-base-secrets-lazr.conf (+1/-1)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+432508@code.launchpad.net

Commit message

charm: Handle multiple RabbitMQ broker URLs

Description of the change

Now that we can tell Launchpad to use multiple RabbitMQ broker URLs with round-robin between them, we should make use of that rather than just picking one of them. This allows for higher availability.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/charm/layer/launchpad-base/reactive/launchpad-base.py b/charm/layer/launchpad-base/reactive/launchpad-base.py
2index 8e0d8d1..a3473f7 100644
3--- a/charm/layer/launchpad-base/reactive/launchpad-base.py
4+++ b/charm/layer/launchpad-base/reactive/launchpad-base.py
5@@ -35,6 +35,19 @@ def prepare_rabbitmq(rabbitmq):
6 rabbitmq.request_access(config["rabbitmq_user"], config["domain"])
7
8
9+def get_rabbitmq_uris(rabbitmq):
10+ for conversation in rabbitmq.conversations():
11+ for relation_id in conversation.relation_ids:
12+ for unit in hookenv.related_units(relation_id):
13+ hostname = hookenv.relation_get(
14+ "private-address", unit, relation_id
15+ )
16+ vhost = rabbitmq.vhost()
17+ username = rabbitmq.username()
18+ password = rabbitmq.password()
19+ yield f"amqp://{username}:{password}@{hostname}/{vhost}"
20+
21+
22 @when("ols.configured", "db.master.available", "rabbitmq.available")
23 @when_not("launchpad.base.configured")
24 def configure(db, rabbitmq):
25@@ -54,14 +67,7 @@ def configure(db, rabbitmq):
26 # specific to the appserver. We need to teach Launchpad to be able to
27 # log in as one role and then switch to another.
28 config["db_user"] = parse_dsn(db_primary)["user"]
29- # XXX cjwatson 2022-09-29: How do we implement HA? Looks like we'd need
30- # code changes to let us configure multiple broker URLs. (At the moment
31- # we rely on round-robin DNS on production, but that probably doesn't
32- # provide very good HA either.)
33- config["rabbitmq_host"] = rabbitmq.private_address()
34- config["rabbitmq_username"] = rabbitmq.username()
35- config["rabbitmq_password"] = rabbitmq.password()
36- config["rabbitmq_vhost"] = rabbitmq.vhost()
37+ config["rabbitmq_broker_urls"] = sorted(get_rabbitmq_uris(rabbitmq))
38 configure_lazr(
39 config,
40 "launchpad-base-lazr.conf",
41@@ -100,3 +106,9 @@ def build_label_changed():
42 def config_changed():
43 remove_state("launchpad.base.configured")
44 remove_state("service.configured")
45+
46+
47+@hook("{requires:rabbitmq}-relation-changed")
48+def rabbitmq_relation_changed(rabbitmq):
49+ remove_state("launchpad.base.configured")
50+ remove_state("service.configured")
51diff --git a/charm/layer/launchpad-base/templates/launchpad-base-lazr.conf b/charm/layer/launchpad-base/templates/launchpad-base-lazr.conf
52index 4ea639e..9e597f5 100644
53--- a/charm/layer/launchpad-base/templates/launchpad-base-lazr.conf
54+++ b/charm/layer/launchpad-base/templates/launchpad-base-lazr.conf
55@@ -82,11 +82,6 @@ authentication_endpoint: http://{{ domain_xmlrpc_private }}:{{ port_xmlrpc }}/au
56 buglist_batch_size: {{ default_batch_size }}
57 bugmail_error_from_address: noreply@bugs.{{ domain }}
58
59-[rabbitmq]
60-host: {{ rabbitmq_host }}
61-userid: {{ rabbitmq_username }}
62-virtual_host: {{ rabbitmq_vhost }}
63-
64 {%- if statsd_environment %}
65 [statsd]
66 host: 127.0.0.1
67diff --git a/charm/layer/launchpad-base/templates/launchpad-base-secrets-lazr.conf b/charm/layer/launchpad-base/templates/launchpad-base-secrets-lazr.conf
68index 1d5ff79..5b45030 100644
69--- a/charm/layer/launchpad-base/templates/launchpad-base-secrets-lazr.conf
70+++ b/charm/layer/launchpad-base/templates/launchpad-base-secrets-lazr.conf
71@@ -14,5 +14,5 @@
72 {{- opt("shared_secret", mailman_shared_secret) }}
73
74 [rabbitmq]
75-password: {{ rabbitmq_password }}
76+broker_urls: {{ rabbitmq_broker_urls|join(" ") }}
77

Subscribers

People subscribed via source and target branches

to status/vote changes: