Skip to content

Commit b8d3186

Browse files
committed
Feedback
1 parent d99e3ae commit b8d3186

File tree

5 files changed

+67
-61
lines changed

5 files changed

+67
-61
lines changed

deps/rabbit/src/rabbit_peer_discovery.erl

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,22 @@ maybe_init() ->
108108
%% node, even if the configuration changed in between.
109109
persistent_term:put(?PT_PEER_DISC_BACKEND, Backend),
110110

111-
_ = code:ensure_loaded(Backend),
112-
case erlang:function_exported(Backend, init, 0) of
113-
true ->
114-
?LOG_DEBUG(
115-
"Peer discovery: backend supports initialisation",
111+
case catch Backend:init() of
112+
ok ->
113+
?LOG_INFO(
114+
"Peer discovery: backend initialisation succeeded",
116115
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
117-
case Backend:init() of
118-
ok ->
119-
?LOG_DEBUG(
120-
"Peer discovery: backend initialisation succeeded",
121-
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
122-
ok;
123-
{error, _Reason} = Error ->
124-
?LOG_WARNING(
125-
"Peer discovery: backend initialisation failed: ~tp.",
126-
[Error],
127-
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
128-
ok
129-
end;
130-
false ->
116+
ok;
117+
{error, _Reason} = Error ->
118+
?LOG_ERROR(
119+
"Peer discovery: backend initialisation failed: ~tp",
120+
[Error],
121+
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
122+
ok;
123+
{'EXIT', {undef, _}} ->
131124
?LOG_DEBUG(
132125
"Peer discovery: backend does not support initialisation",
133-
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
126+
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
134127
ok
135128
end.
136129

@@ -159,7 +152,7 @@ sync_desired_cluster() ->
159152

160153
-spec sync_desired_cluster(Backend, RetriesLeft, RetryDelay) -> ok when
161154
Backend :: backend(),
162-
RetriesLeft :: non_neg_integer() | infinity,
155+
RetriesLeft :: non_neg_integer() | unlimited,
163156
RetryDelay :: non_neg_integer().
164157
%% @private
165158

@@ -240,18 +233,18 @@ sync_desired_cluster(Backend, RetriesLeft, RetryDelay) ->
240233

241234
-spec retry_sync_desired_cluster(Backend, RetriesLeft, RetryDelay) -> ok when
242235
Backend :: backend(),
243-
RetriesLeft :: non_neg_integer() | infinity,
236+
RetriesLeft :: non_neg_integer() | unlimited,
244237
RetryDelay :: non_neg_integer().
245238
%% @private
246239

247-
retry_sync_desired_cluster(Backend, infinity, RetryDelay) ->
240+
retry_sync_desired_cluster(Backend, unlimited, RetryDelay) ->
248241
?LOG_DEBUG(
249242
"Peer discovery: retrying to create/sync cluster in ~b ms "
250243
"(will retry forever)",
251244
[RetryDelay],
252245
#{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
253246
timer:sleep(RetryDelay),
254-
sync_desired_cluster(Backend, infinity, RetryDelay);
247+
sync_desired_cluster(Backend, unlimited, RetryDelay);
255248
retry_sync_desired_cluster(Backend, RetriesLeft, RetryDelay)
256249
when RetriesLeft > 0 ->
257250
RetriesLeft1 = RetriesLeft - 1,
@@ -1017,33 +1010,30 @@ maybe_unregister() ->
10171010

10181011
-spec discovery_retries(Backend) -> {Retries, RetryDelay} when
10191012
Backend :: backend(),
1020-
Retries :: non_neg_integer() | infinity,
1013+
Retries :: non_neg_integer() | unlimited,
10211014
RetryDelay :: non_neg_integer().
10221015

10231016
discovery_retries(Backend) ->
1024-
_ = code:ensure_loaded(Backend),
1025-
{Retries0, Interval} = case application:get_env(rabbit, cluster_formation) of
1026-
{ok, Proplist} ->
1027-
Retries1 = proplists:get_value(
1028-
discovery_retry_limit,
1029-
Proplist,
1030-
?DEFAULT_DISCOVERY_RETRY_COUNT),
1031-
Interval1 = proplists:get_value(
1032-
discovery_retry_interval,
1033-
Proplist,
1034-
?DEFAULT_DISCOVERY_RETRY_INTERVAL_MS),
1035-
{Retries1, Interval1};
1036-
undefined ->
1037-
{?DEFAULT_DISCOVERY_RETRY_COUNT, ?DEFAULT_DISCOVERY_RETRY_INTERVAL_MS}
1038-
end,
1039-
Retries = case erlang:function_exported(Backend, retry_forever, 0)
1040-
andalso Backend:retry_forever() of
1041-
true ->
1042-
infinity;
1043-
false ->
1044-
Retries0
1045-
end,
1046-
{Retries, Interval}.
1017+
{_Retries, RetryDelay} = RetryConfig = discovery_retries_from_config(),
1018+
case catch Backend:retry_strategy() of
1019+
unlimited ->
1020+
{unlimited, RetryDelay};
1021+
_ ->
1022+
RetryConfig
1023+
end.
1024+
1025+
-spec discovery_retries_from_config() -> {Retries, RetryDelay} when
1026+
Retries :: non_neg_integer(),
1027+
RetryDelay :: non_neg_integer().
1028+
discovery_retries_from_config() ->
1029+
case application:get_env(rabbit, cluster_formation) of
1030+
{ok, Proplist} ->
1031+
Retries = proplists:get_value(discovery_retry_limit, Proplist, ?DEFAULT_DISCOVERY_RETRY_COUNT),
1032+
Interval = proplists:get_value(discovery_retry_interval, Proplist, ?DEFAULT_DISCOVERY_RETRY_INTERVAL_MS),
1033+
{Retries, Interval};
1034+
undefined ->
1035+
{?DEFAULT_DISCOVERY_RETRY_COUNT, ?DEFAULT_DISCOVERY_RETRY_INTERVAL_MS}
1036+
end.
10471037

10481038
-spec register(Backend) -> ok when
10491039
Backend :: backend().

deps/rabbit_common/src/rabbit_peer_discovery_backend.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454

5555
-callback unlock(Data :: term()) -> ok.
5656

57-
-callback retry_forever() -> boolean().
57+
-callback retry_strategy() -> limited | unlimited.
5858

59-
-optional_callbacks([init/0, retry_forever/0]).
59+
-optional_callbacks([init/0, retry_strategy/0]).
6060

6161
-export([api_version/0]).
6262

deps/rabbitmq_peer_discovery_k8s/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
## Overview
44

5-
This is an implementation of RabbitMQ peer discovery interface for for Kubernetes. This is a completely new implementation (version 2) that has little to do with the original design but is backwards compatible
5+
This is an implementation of RabbitMQ peer discovery interface for Kubernetes. This is a completely new implementation (version 2) that has little to do with the original design but is backwards compatible
66
(all the configuration options of version 1 are accepted but ignored).
77

88
### Version 1 vs Version 2
99

1010
The original implementation of this plugin performed peer discovery using Kubernetes API as the source of data on running cluster pods. It queried the Kubernetes API for the list of endpoints serving as the backends of a Kubernetes Service.
1111

12-
However, RabbitMQ should be deployed using a StatefulSet and pods of a StatefulSet have consistent names - Kubernetes always creates the pods with the StatefulSet name and an ID suffix, starting with 0. For example, a StatefulSet with 3 replicas named `foobar` will have pods named `foobar-0`, `foobar-1`, and `foobar-2`. It is therefore not necessary to query the Kubernetes API to discover peers. Version 2 doesn't perform any API queries and insteard checks the suffix of the local node and:
13-
* if the suffix is `-0`, it forms a new cluster
12+
However, RabbitMQ should be deployed using a StatefulSet and pods of a StatefulSet have consistent names - Kubernetes always creates the pods with the StatefulSet name and an ID suffix, starting with 0. For example, a StatefulSet with 3 replicas named `foobar` will have pods named `foobar-0`, `foobar-1`, and `foobar-2`. It is therefore not necessary to query the Kubernetes API to discover peers. Version 2 doesn't perform any API queries and instead checks the suffix of the local node and:
13+
* if the suffix is `-0`, it starts normally (effectively forming a new single-node cluster)
1414
* if the suffix is different, it never forms a new cluster and will always join the node with the `-0` suffix
1515

1616
This avoids any race conditions that could lead to the cluster being formed incorrectly (Version 1 was prone to this problem in some environments).

deps/rabbitmq_peer_discovery_k8s/src/rabbit_peer_discovery_k8s.erl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
-export([init/0, list_nodes/0, supports_registration/0, register/0,
1212
unregister/0, post_registration/0, lock/1, unlock/1, node/0,
13-
retry_forever/0]).
13+
retry_strategy/0]).
1414

1515
-include_lib("kernel/include/logger.hrl").
1616
-include_lib("rabbit_common/include/logging.hrl").
@@ -19,14 +19,31 @@
1919
-compile([node/0]).
2020
-endif.
2121

22+
init() ->
23+
Formation = application:get_env(rabbit, cluster_formation, []),
24+
case proplists:get_value(peer_discovery_k8s, Formation, undefined) of
25+
undefined -> ok;
26+
_ -> ?LOG_WARNING("Peer discovery: ignoring deprecated cluster_formation.k8s.* configuration options",
27+
[], #{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
28+
ok
29+
end,
30+
case proplists:get_value(discovery_retry_limit, Formation, undefined) of
31+
undefined -> ok;
32+
_ -> ?LOG_WARNING("Peer discovery: ignoring cluster_formation.discovery_retry_limit option "
33+
"(will retry forever)",
34+
[], #{domain => ?RMQLOG_DOMAIN_PEER_DISC}),
35+
ok
36+
end,
37+
ok.
38+
2239
-spec list_nodes() -> {ok, {Nodes :: [node()] | node(), NodeType :: rabbit_types:node_type()}} | {error, Reason :: string()}.
2340

2441
list_nodes() ->
2542
Nodename = atom_to_list(?MODULE:node()),
2643
try
2744
[[], Prefix, StatefulSetName, MyPodId, Domain] = re:split(
2845
Nodename,
29-
"([^@]+@)([^.]*-)([0-9]+)",
46+
"^([^@]+@)([^.]*-)([0-9]+)",
3047
[{return, list}]),
3148
_ = list_to_integer(MyPodId),
3249
SeedNode = list_to_atom(lists:flatten(Prefix ++ StatefulSetName ++ "0" ++ Domain)),
@@ -44,10 +61,9 @@ node() ->
4461
erlang:node().
4562

4663
supports_registration() -> false.
47-
init() -> ok.
4864
register() -> ok.
4965
unregister() -> ok.
5066
post_registration() -> ok.
5167
lock(_) -> not_supported.
5268
unlock(_) -> ok.
53-
retry_forever() -> true.
69+
retry_strategy() -> unlimited.

deps/rabbitmq_peer_discovery_k8s/src/rabbitmq_peer_discovery_k8s.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
-behaviour(rabbit_peer_discovery_backend).
1313

1414
-export([init/0, list_nodes/0, supports_registration/0, register/0, unregister/0,
15-
post_registration/0, lock/1, unlock/1, retry_forever/0]).
15+
post_registration/0, lock/1, unlock/1, retry_strategy/0]).
1616

1717
-define(DELEGATE, rabbit_peer_discovery_k8s).
1818

@@ -53,7 +53,7 @@ lock(Node) ->
5353
unlock(Data) ->
5454
?DELEGATE:unlock(Data).
5555

56-
-spec retry_forever() -> boolean().
57-
retry_forever() ->
58-
?DELEGATE:retry_forever().
56+
-spec retry_strategy() -> limited | unlimited.
57+
retry_strategy() ->
58+
?DELEGATE:retry_strategy().
5959

0 commit comments

Comments
 (0)