Skip to content

Commit

Permalink
Merge pull request rabbitmq#6930 from rabbitmq/add-rabbit-is_serving
Browse files Browse the repository at this point in the history
rabbit: Add `is_serving/{0,1}` function
  • Loading branch information
michaelklishin authored Jan 18, 2023
2 parents 51866bd + 027b887 commit bb9c791
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
42 changes: 39 additions & 3 deletions deps/rabbit/src/rabbit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

-export([start/0, boot/0, stop/0,
stop_and_halt/0, await_startup/0, await_startup/1, await_startup/3,
status/0, is_running/0, alarms/0,
is_running/1, environment/0, rotate_logs/0, force_event_refresh/1,
status/0, is_running/0, is_serving/0, alarms/0,
is_running/1, is_serving/1, environment/0, rotate_logs/0,
force_event_refresh/1,
start_fhc/0]).

-export([start/2, stop/1, prep_stop/1]).
Expand Down Expand Up @@ -767,7 +768,42 @@ total_queue_count() ->
end,
0, rabbit_vhost:list_names()).

-spec is_running() -> boolean().
-spec is_serving() -> IsServing when
IsServing :: boolean().
%% @doc Indicates if this RabbitMQ node is ready to serve clients or not.
%%
%% It differs from {@link is_running/0} in the sense that a running RabbitMQ
%% node could be under maintenance. A serving node is one where RabbitMQ is
%% running and is not under maintenance currently, thus accepting clients.

is_serving() ->
ThisNode = node(),
is_running() andalso
not rabbit_maintenance:is_being_drained_local_read(ThisNode).

-spec is_serving(Node) -> IsServing when
Node :: node(),
IsServing :: boolean().
%% @doc Indicates if the given node is ready to serve client or not.

is_serving(Node) when Node =:= node() ->
is_serving();
is_serving(Node) ->
case rpc:call(Node, rabbit, is_serving, []) of
true -> true;
_ -> false
end.

-spec is_running() -> IsRunning when
IsRunning :: boolean().
%% @doc Indicates if the `rabbit' application is running on this RabbitMQ node
%% or not.
%%
%% A RabbitMQ node is considered to run as soon as the `rabbit' application is
%% running. It means this node participates to the cluster. However, it could
%% accept or reject clients if it is under maintenance. See {@link
%% is_serving/0} to check if this node is running and is ready to serve
%% clients.

is_running() -> is_running(node()).

Expand Down
41 changes: 40 additions & 1 deletion deps/rabbit/test/maintenance_mode_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

all() ->
[
{group, cluster_size_1},
{group, cluster_size_3}
].

groups() ->
[
{cluster_size_1, [], [
is_serving_works
]},
{cluster_size_3, [], [
maintenance_mode_status,
listener_suspension_status,
Expand All @@ -40,7 +44,10 @@ init_per_suite(Config) ->
end_per_suite(Config) ->
rabbit_ct_helpers:run_teardown_steps(Config).

init_per_group(_Group, Config) ->
init_per_group(cluster_size_1, Config) ->
rabbit_ct_helpers:set_config(Config,
[{rmq_nodes_count, 1}]);
init_per_group(cluster_size_3, Config) ->
rabbit_ct_helpers:set_config(Config,
[{rmq_nodes_count, 3}]).

Expand Down Expand Up @@ -85,6 +92,38 @@ end_per_testcase(Testcase, Config) ->
%% Test Cases
%% -------------------------------------------------------------------

is_serving_works(Config) ->
[Node] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),

?assert(rabbit:is_running(Node)),
?assert(rabbit:is_serving(Node)),

rabbit_ct_broker_helpers:mark_as_being_drained(Config, Node),
rabbit_ct_helpers:await_condition(
fun () -> rabbit_ct_broker_helpers:is_being_drained_local_read(Config, Node) end,
10000),

?assert(rabbit:is_running(Node)),
?assertNot(rabbit:is_serving(Node)),

rabbit_ct_broker_helpers:unmark_as_being_drained(Config, Node),
rabbit_ct_helpers:await_condition(
fun () -> not rabbit_ct_broker_helpers:is_being_drained_local_read(Config, Node) end,
10000),

?assert(rabbit:is_running(Node)),
?assert(rabbit:is_serving(Node)),

rabbit_ct_broker_helpers:stop_broker(Config, Node),

?assertNot(rabbit:is_running(Node)),
?assertNot(rabbit:is_serving(Node)),

rabbit_ct_broker_helpers:start_broker(Config, Node),

?assert(rabbit:is_running(Node)),
?assert(rabbit:is_serving(Node)).

maintenance_mode_status(Config) ->
Nodes = [A, B, C] = rabbit_ct_broker_helpers:get_node_configs(Config, nodename),

Expand Down

0 comments on commit bb9c791

Please sign in to comment.