Skip to content

Commit a79a621

Browse files
committed
Move common peer discovery functions to rabbit_nodes
lock_id/1 and lock_retries/0 get used by peer discovery backends classic, k8s, and aws. Ideally, these two functions live in app rabbitmq_peer_discovery_common. However, the classic backend (and therefore app rabbit) cannot depend on app rabbitmq_peer_discovery_common since the latter already depends on the former. We don't want cyclic dependencies between Erlang apps. That's why we move these two functions into module rabbit_nodes. We could have moved them into app rabbit_common instead. However that module is mainly used for sharing code across server and client. These two functions are used only in the server.
1 parent ed56aa3 commit a79a621

File tree

6 files changed

+27
-41
lines changed

6 files changed

+27
-41
lines changed

deps/rabbit/src/rabbit_nodes.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
boot/0]).
1616
-export([persistent_cluster_id/0, seed_internal_cluster_id/0, seed_user_provided_cluster_name/0]).
1717
-export([all_running_with_hashes/0]).
18+
-export([lock_id/1, lock_retries/0]).
1819

1920
-include_lib("kernel/include/inet.hrl").
2021
-include_lib("rabbit_common/include/rabbit.hrl").
@@ -23,6 +24,12 @@
2324

2425
-define(INTERNAL_CLUSTER_ID_PARAM_NAME, internal_cluster_id).
2526

27+
% Retries as passed to https://erlang.org/doc/man/global.html#set_lock-3
28+
% To understand how retries map to the timeout, read
29+
% https://github.com/erlang/otp/blob/d256ae477014158a49bb860b283df9c040011197/lib/kernel/src/global.erl#L2062-L2075
30+
% 80 corresponds to a timeout of ca 300 seconds.
31+
-define(DEFAULT_LOCK_RETRIES, 80).
32+
2633
%%----------------------------------------------------------------------------
2734
%% API
2835
%%----------------------------------------------------------------------------
@@ -160,3 +167,16 @@ await_running_count_with_retries(TargetCount, Retries) ->
160167
-spec all_running_with_hashes() -> #{non_neg_integer() => node()}.
161168
all_running_with_hashes() ->
162169
maps:from_list([{erlang:phash2(Node), Node} || Node <- all_running()]).
170+
171+
-spec lock_id(Node :: node()) -> {ResourceId :: string(), LockRequesterId :: node()}.
172+
lock_id(Node) ->
173+
{cookie_hash(), Node}.
174+
175+
-spec lock_retries() -> integer().
176+
lock_retries() ->
177+
case application:get_env(rabbit, cluster_formation) of
178+
{ok, PropList} ->
179+
proplists:get_value(internal_lock_retries, PropList, ?DEFAULT_LOCK_RETRIES);
180+
undefined ->
181+
?DEFAULT_LOCK_RETRIES
182+
end.

deps/rabbit/src/rabbit_peer_discovery_classic_config.erl

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
-export([list_nodes/0, supports_registration/0, register/0, unregister/0,
1414
post_registration/0, lock/1, unlock/1]).
1515

16-
%% TODO ansd: see todo below
17-
-define(DEFAULT_LOCK_RETRIES, 80).
1816
%%
1917
%% API
2018
%%
@@ -31,26 +29,15 @@ list_nodes() ->
3129
-spec lock(Node :: node()) -> {ok, {ResourceId :: string(), LockRequesterId :: node()}} | {error, Reason :: string()}.
3230

3331
lock(Node) ->
34-
%% TODO ansd: instead of below lines, ideally we reuse functions rabbit_peer_discovery_util:lock_id/1 and
35-
%% rabbit_peer_discovery_util:lock_retries/0
36-
%% When using these two functions, executing tests failed since node couldn't boot: undef rabbit_peer_discovery_util:lock_id/1
37-
%% Can the rabbit app depend on the rabbit_peer_discovery_common_app?
38-
%% If not, should we move these two functions to some other place (e.g. rabbit_common)?
39-
LockId = {rabbit_nodes:cookie_hash(), Node},
40-
Retries = case application:get_env(rabbit, cluster_formation) of
41-
{ok, PropList} ->
42-
proplists:get_value(internal_lock_retries, PropList, ?DEFAULT_LOCK_RETRIES);
43-
undefined ->
44-
?DEFAULT_LOCK_RETRIES
45-
end,
46-
4732
Nodes = node_list(),
4833
case lists:member(Node, Nodes) of
4934
false when Nodes =/= [] ->
5035
rabbit_log:warning("Local node ~s is not part of configured nodes ~p. "
5136
"This might lead to incorrect cluster formation.", [Node, Nodes]);
5237
_ -> ok
5338
end,
39+
LockId = rabbit_nodes:lock_id(Node),
40+
Retries = rabbit_nodes:lock_retries(),
5441
case global:set_lock(LockId, Nodes, Retries) of
5542
true ->
5643
{ok, LockId};

deps/rabbitmq_peer_discovery_aws/src/rabbit_peer_discovery_aws.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ lock(Node) ->
125125
case lists:member(Node, Nodes) of
126126
true ->
127127
rabbit_log:info("Will try to lock connecting to nodes ~p", [Nodes]),
128-
LockId = ?UTIL_MODULE:lock_id(Node),
129-
Retries = ?UTIL_MODULE:lock_retries(),
128+
LockId = rabbit_nodes:lock_id(Node),
129+
Retries = rabbit_nodes:lock_retries(),
130130
case global:set_lock(LockId, Nodes, Retries) of
131131
true ->
132132
{ok, LockId};

deps/rabbitmq_peer_discovery_common/include/rabbit_peer_discovery.hrl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
% by `httpc`
1717
-define(DEFAULT_HTTP_TIMEOUT, 2250).
1818

19-
% Retries as passed to https://erlang.org/doc/man/global.html#set_lock-3
20-
% To understand how retries map to the timeout, read
21-
% https://github.com/erlang/otp/blob/d256ae477014158a49bb860b283df9c040011197/lib/kernel/src/global.erl#L2062-L2075
22-
% 80 corresponds to a timeout of ca 300 seconds.
23-
-define(DEFAULT_LOCK_RETRIES, 80).
24-
2519
-type peer_discovery_config_value() :: atom() | integer() | string() | undefined.
2620

2721
-record(peer_discovery_config_entry_meta,

deps/rabbitmq_peer_discovery_common/src/rabbit_peer_discovery_util.erl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
as_proplist/1,
2222
as_map/1,
2323
stringify_error/1,
24-
maybe_backend_configured/4,
25-
lock_id/1,
26-
lock_retries/0
24+
maybe_backend_configured/4
2725
]).
2826

2927
-include_lib("kernel/include/logger.hrl").
@@ -428,16 +426,3 @@ as_list(Value) ->
428426
[Value],
429427
#{domain => ?RMQLOG_DOMAIN_PEER_DIS}),
430428
Value.
431-
432-
-spec lock_id(Node :: node()) -> {ResourceId :: string(), LockRequesterId :: node()}.
433-
lock_id(Node) ->
434-
{rabbit_nodes:cookie_hash(), Node}.
435-
436-
-spec lock_retries() -> integer().
437-
lock_retries() ->
438-
case application:get_env(rabbit, cluster_formation) of
439-
{ok, PropList} ->
440-
proplists:get_value(internal_lock_retries, PropList, ?DEFAULT_LOCK_RETRIES);
441-
undefined ->
442-
?DEFAULT_LOCK_RETRIES
443-
end.

deps/rabbitmq_peer_discovery_k8s/src/rabbit_peer_discovery_k8s.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ lock(Node) ->
7272
case lists:member(Node, Nodes) of
7373
true ->
7474
rabbit_log:info("Will try to lock connecting to nodes ~p", [Nodes]),
75-
LockId = ?UTIL_MODULE:lock_id(Node),
76-
Retries = ?UTIL_MODULE:lock_retries(),
75+
LockId = rabbit_nodes:lock_id(Node),
76+
Retries = rabbit_nodes:lock_retries(),
7777
case global:set_lock(LockId, Nodes, Retries) of
7878
true ->
7979
{ok, LockId};

0 commit comments

Comments
 (0)