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.
This commit is contained in:
Jean-Sébastien Pédron 2024-05-02 10:38:03 +02:00
parent 50b490100d
commit 684ec76f77
No known key found for this signature in database
GPG Key ID: 39E99761A5FD94CC
3 changed files with 35 additions and 6 deletions

View File

@ -69,6 +69,11 @@
env_variable = "CONSUL_SVC_ADDR_NODENAME",
default_value = false
},
consul_svc_id => #peer_discovery_config_entry_meta{
type = string,
env_variable = "CONSUL_SVC_ID",
default_value = "undefined"
},
consul_svc_port => #peer_discovery_config_entry_meta{
type = integer,
env_variable = "CONSUL_SVC_PORT",

View File

@ -140,7 +140,7 @@ fun(Conf) ->
end}.
%% use (Erlang) node name when compuing service address?
%% use (Erlang) node name when computing service address?
{mapping, "cluster_formation.consul.svc_addr_use_nodename", "rabbit.cluster_formation.peer_discovery_consul.consul_svc_addr_nodename", [
{datatype, {enum, [true, false]}}
@ -155,6 +155,21 @@ fun(Conf) ->
end}.
%% service ID
{mapping, "cluster_formation.consul.svc_id", "rabbit.cluster_formation.peer_discovery_consul.consul_svc_id", [
{datatype, string}
]}.
{translation, "rabbit.cluster_formation.peer_discovery_consul.consul_svc_id",
fun(Conf) ->
case cuttlefish:conf_get("cluster_formation.consul.svc_id", Conf, undefined) of
undefined -> cuttlefish:unset();
Value -> Value
end
end}.
%% (optionally) append a suffix to node names retrieved from Consul
{mapping, "cluster_formation.consul.domain_suffix", "rabbit.cluster_formation.peer_discovery_consul.consul_domain", [

View File

@ -66,7 +66,7 @@ list_nodes() ->
end,
Fun2 = fun(Proplist) ->
M = maps:from_list(Proplist),
Path = rabbit_peer_discovery_httpc:build_path([v1, health, service, get_config_key(consul_svc, M)]),
Path = rabbit_peer_discovery_httpc:build_path([v1, health, service, service_name()]),
HttpOpts = http_options(M),
case rabbit_peer_discovery_httpc:get(get_config_key(consul_scheme, M),
get_config_key(consul_host, M),
@ -335,8 +335,7 @@ registration_body_add_id() ->
-spec registration_body_add_name(Payload :: list()) -> list().
registration_body_add_name(Payload) ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
Name = rabbit_data_coercion:to_atom(get_config_key(consul_svc, M)),
Name = rabbit_data_coercion:to_atom(service_name()),
lists:append(Payload, [{'Name', Name}]).
-spec registration_body_maybe_add_address(Payload :: list())
@ -484,14 +483,24 @@ service_address(_, false, NIC, _) ->
-spec service_id() -> string().
service_id() ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
service_id(get_config_key(consul_svc, M),
service_address()).
case get_config_key(consul_svc_id, M) of
"undefined" ->
service_id(get_config_key(consul_svc, M),
service_address());
ID ->
ID
end.
-spec service_id(Name :: string(), Address :: string()) -> string().
service_id(Service, "undefined") -> Service;
service_id(Service, Address) ->
string:join([Service, Address], ":").
-spec service_name() -> string().
service_name() ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
get_config_key(consul_svc, M).
-spec service_ttl(TTL :: integer()) -> string().
service_ttl(Value) ->
rabbit_peer_discovery_util:as_string(Value) ++ "s".