Skip to content

Commit 7da3482

Browse files
dumbbellmergify[bot]
authored andcommitted
rabbitmq_peer_discovery_consul: Separate service name and ID
[Why] The `consul_svc` parameter is used as the service name and to construct the service ID. The problem with the way the service ID is constructed is that it doesn't allow to register several distinct RabbitMQ nodes in the same Consul agent. This is a problem for testsuites where we want to run several RabbitMQ nodes on the same host with a single local Consul agent. [How] The service ID has now its own parameters, `consul_svc_id`. If this one is unset, it falls back to the previous construction from the service name. This allows to remain 100% compatible with previous versions. (cherry picked from commit 684ec76)
1 parent 6424c86 commit 7da3482

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

deps/rabbitmq_peer_discovery_consul/include/rabbit_peer_discovery_consul.hrl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
env_variable = "CONSUL_SVC_ADDR_NODENAME",
7070
default_value = false
7171
},
72+
consul_svc_id => #peer_discovery_config_entry_meta{
73+
type = string,
74+
env_variable = "CONSUL_SVC_ID",
75+
default_value = "undefined"
76+
},
7277
consul_svc_port => #peer_discovery_config_entry_meta{
7378
type = integer,
7479
env_variable = "CONSUL_SVC_PORT",

deps/rabbitmq_peer_discovery_consul/priv/schema/rabbitmq_peer_discovery_consul.schema

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fun(Conf) ->
140140
end}.
141141

142142

143-
%% use (Erlang) node name when compuing service address?
143+
%% use (Erlang) node name when computing service address?
144144

145145
{mapping, "cluster_formation.consul.svc_addr_use_nodename", "rabbit.cluster_formation.peer_discovery_consul.consul_svc_addr_nodename", [
146146
{datatype, {enum, [true, false]}}
@@ -155,6 +155,21 @@ fun(Conf) ->
155155
end}.
156156

157157

158+
%% service ID
159+
160+
{mapping, "cluster_formation.consul.svc_id", "rabbit.cluster_formation.peer_discovery_consul.consul_svc_id", [
161+
{datatype, string}
162+
]}.
163+
164+
{translation, "rabbit.cluster_formation.peer_discovery_consul.consul_svc_id",
165+
fun(Conf) ->
166+
case cuttlefish:conf_get("cluster_formation.consul.svc_id", Conf, undefined) of
167+
undefined -> cuttlefish:unset();
168+
Value -> Value
169+
end
170+
end}.
171+
172+
158173
%% (optionally) append a suffix to node names retrieved from Consul
159174

160175
{mapping, "cluster_formation.consul.domain_suffix", "rabbit.cluster_formation.peer_discovery_consul.consul_domain", [

deps/rabbitmq_peer_discovery_consul/src/rabbit_peer_discovery_consul.erl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ list_nodes() ->
6666
end,
6767
Fun2 = fun(Proplist) ->
6868
M = maps:from_list(Proplist),
69-
Path = rabbit_peer_discovery_httpc:build_path([v1, health, service, get_config_key(consul_svc, M)]),
69+
Path = rabbit_peer_discovery_httpc:build_path([v1, health, service, service_name()]),
7070
HttpOpts = http_options(M),
7171
case rabbit_peer_discovery_httpc:get(get_config_key(consul_scheme, M),
7272
get_config_key(consul_host, M),
@@ -335,8 +335,7 @@ registration_body_add_id() ->
335335

336336
-spec registration_body_add_name(Payload :: list()) -> list().
337337
registration_body_add_name(Payload) ->
338-
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
339-
Name = rabbit_data_coercion:to_atom(get_config_key(consul_svc, M)),
338+
Name = rabbit_data_coercion:to_atom(service_name()),
340339
lists:append(Payload, [{'Name', Name}]).
341340

342341
-spec registration_body_maybe_add_address(Payload :: list())
@@ -484,14 +483,24 @@ service_address(_, false, NIC, _) ->
484483
-spec service_id() -> string().
485484
service_id() ->
486485
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
487-
service_id(get_config_key(consul_svc, M),
488-
service_address()).
486+
case get_config_key(consul_svc_id, M) of
487+
"undefined" ->
488+
service_id(get_config_key(consul_svc, M),
489+
service_address());
490+
ID ->
491+
ID
492+
end.
489493

490494
-spec service_id(Name :: string(), Address :: string()) -> string().
491495
service_id(Service, "undefined") -> Service;
492496
service_id(Service, Address) ->
493497
string:join([Service, Address], ":").
494498

499+
-spec service_name() -> string().
500+
service_name() ->
501+
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
502+
get_config_key(consul_svc, M).
503+
495504
-spec service_ttl(TTL :: integer()) -> string().
496505
service_ttl(Value) ->
497506
rabbit_peer_discovery_util:as_string(Value) ++ "s".

0 commit comments

Comments
 (0)