Skip to content

Commit edf9122

Browse files
Merge pull request #2656 from rabbitmq/run-plugins-boot-steps-during-rabbit-start
rabbit: Run plugins' boot steps during rabbit start/2 (cherry picked from commit 67624cd)
1 parent b67b842 commit edf9122

File tree

3 files changed

+71
-46
lines changed

3 files changed

+71
-46
lines changed

deps/rabbit/apps/rabbitmq_prelaunch/src/rabbit_boot_state.erl

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
-export([get/0,
2222
set/1,
23-
wait_for/2]).
23+
wait_for/2,
24+
has_reached/1,
25+
has_reached_and_is_active/1]).
2426

2527
-define(PT_KEY_BOOT_STATE, {?MODULE, boot_state}).
2628

27-
-type boot_state() :: 'stopped' | 'booting' | 'ready' | 'stopping'.
29+
-type boot_state() :: 'stopped' | 'booting' | 'core_started' | 'ready' | 'stopping'.
2830

2931
-export_type([boot_state/0]).
3032

@@ -44,15 +46,15 @@ set(BootState) ->
4446

4547
-spec wait_for(boot_state(), timeout()) -> ok | {error, timeout}.
4648
wait_for(BootState, infinity) ->
47-
case is_reached(BootState) of
49+
case has_reached(BootState) of
4850
true -> ok;
4951
false -> Wait = 200,
5052
timer:sleep(Wait),
5153
wait_for(BootState, infinity)
5254
end;
5355
wait_for(BootState, Timeout)
5456
when is_integer(Timeout) andalso Timeout >= 0 ->
55-
case is_reached(BootState) of
57+
case has_reached(BootState) of
5658
true -> ok;
5759
false -> Wait = 200,
5860
timer:sleep(Wait),
@@ -61,24 +63,35 @@ wait_for(BootState, Timeout)
6163
wait_for(_, _) ->
6264
{error, timeout}.
6365

64-
boot_state_idx(stopped) -> 0;
65-
boot_state_idx(booting) -> 1;
66-
boot_state_idx(ready) -> 2;
67-
boot_state_idx(stopping) -> 3.
66+
boot_state_idx(stopped) -> 0;
67+
boot_state_idx(booting) -> 1;
68+
boot_state_idx(core_started) -> 2;
69+
boot_state_idx(ready) -> 3;
70+
boot_state_idx(stopping) -> 4.
6871

6972
is_valid(BootState) ->
7073
is_integer(boot_state_idx(BootState)).
7174

72-
is_reached(TargetBootState) ->
73-
is_reached(?MODULE:get(), TargetBootState).
75+
has_reached(TargetBootState) ->
76+
has_reached(?MODULE:get(), TargetBootState).
7477

75-
is_reached(CurrentBootState, CurrentBootState) ->
78+
has_reached(CurrentBootState, CurrentBootState) ->
7679
true;
77-
is_reached(stopping, stopped) ->
80+
has_reached(stopping, stopped) ->
7881
false;
79-
is_reached(_CurrentBootState, stopped) ->
82+
has_reached(_CurrentBootState, stopped) ->
8083
true;
81-
is_reached(stopped, _TargetBootState) ->
84+
has_reached(stopped, _TargetBootState) ->
8285
true;
83-
is_reached(CurrentBootState, TargetBootState) ->
86+
has_reached(CurrentBootState, TargetBootState) ->
8487
boot_state_idx(TargetBootState) =< boot_state_idx(CurrentBootState).
88+
89+
has_reached_and_is_active(TargetBootState) ->
90+
case ?MODULE:get() of
91+
stopped ->
92+
false;
93+
CurrentBootState ->
94+
has_reached(CurrentBootState, TargetBootState)
95+
andalso
96+
not has_reached(CurrentBootState, stopping)
97+
end.

deps/rabbit/src/rabbit.erl

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,9 @@ handle_app_error(Term) ->
528528
is_booting() -> is_booting(node()).
529529

530530
is_booting(Node) when Node =:= node() ->
531-
case rabbit_boot_state:get() of
532-
booting -> true;
533-
_ -> false
534-
end;
531+
rabbit_boot_state:has_reached_and_is_active(booting)
532+
andalso
533+
not rabbit_boot_state:has_reached(ready);
535534
is_booting(Node) ->
536535
case rpc:call(Node, rabbit, is_booting, []) of
537536
{badrpc, _} = Err -> Err;
@@ -856,11 +855,30 @@ start(normal, []) ->
856855
log_banner(),
857856
warn_if_kernel_config_dubious(),
858857
warn_if_disc_io_options_dubious(),
859-
%% We run `rabbit` boot steps only for now. Plugins boot steps
860-
%% will be executed as part of the postlaunch phase after they
861-
%% are started.
862-
rabbit_boot_steps:run_boot_steps([rabbit]),
863-
run_postlaunch_phase(),
858+
859+
rabbit_log_prelaunch:debug(""),
860+
rabbit_log_prelaunch:debug("== Plugins (prelaunch phase) =="),
861+
862+
rabbit_log_prelaunch:debug("Setting plugins up"),
863+
%% `Plugins` contains all the enabled plugins, plus their
864+
%% dependencies. The order is important: dependencies appear
865+
%% before plugin which depend on them.
866+
Plugins = rabbit_plugins:setup(),
867+
rabbit_log_prelaunch:debug(
868+
"Loading the following plugins: ~p", [Plugins]),
869+
%% We can load all plugins and refresh their feature flags at
870+
%% once, because it does not involve running code from the
871+
%% plugins.
872+
ok = app_utils:load_applications(Plugins),
873+
ok = rabbit_feature_flags:refresh_feature_flags_after_app_load(
874+
Plugins),
875+
876+
rabbit_log_prelaunch:debug(""),
877+
rabbit_log_prelaunch:debug("== Boot steps =="),
878+
879+
ok = rabbit_boot_steps:run_boot_steps([rabbit | Plugins]),
880+
run_postlaunch_phase(Plugins),
881+
rabbit_boot_state:set(core_started),
864882
{ok, SupPid}
865883
catch
866884
throw:{error, _} = Error ->
@@ -879,46 +897,39 @@ start(normal, []) ->
879897
Error
880898
end.
881899

882-
run_postlaunch_phase() ->
883-
spawn(fun() -> do_run_postlaunch_phase() end).
900+
run_postlaunch_phase(Plugins) ->
901+
spawn(fun() -> do_run_postlaunch_phase(Plugins) end).
884902

885-
do_run_postlaunch_phase() ->
903+
do_run_postlaunch_phase(Plugins) ->
886904
%% Once RabbitMQ itself is started, we need to run a few more steps,
887905
%% in particular start plugins.
888906
rabbit_log_prelaunch:debug(""),
889907
rabbit_log_prelaunch:debug("== Postlaunch phase =="),
890908

891909
try
910+
%% Successful boot resets node maintenance state.
892911
rabbit_log_prelaunch:debug(""),
893-
rabbit_log_prelaunch:debug("== Plugins =="),
912+
rabbit_log_prelaunch:info("Resetting node maintenance status"),
913+
_ = rabbit_maintenance:unmark_as_being_drained(),
914+
915+
rabbit_log_prelaunch:debug(""),
916+
rabbit_log_prelaunch:debug("== Plugins (postlaunch phase) =="),
894917

895-
rabbit_log_prelaunch:debug("Setting plugins up"),
896-
%% `Plugins` contains all the enabled plugins, plus their
897-
%% dependencies. The order is important: dependencies appear
898-
%% before plugin which depend on them.
899-
Plugins = rabbit_plugins:setup(),
900-
rabbit_log_prelaunch:debug(
901-
"Starting the following plugins: ~p", [Plugins]),
902-
%% We can load all plugins and refresh their feature flags at
903-
%% once, because it does not involve running code from the
904-
%% plugins.
905-
app_utils:load_applications(Plugins),
906-
ok = rabbit_feature_flags:refresh_feature_flags_after_app_load(
907-
Plugins),
908918
%% However, we want to run their boot steps and actually start
909919
%% them one by one, to ensure a dependency is fully started
910920
%% before a plugin which depends on it gets a chance to start.
921+
rabbit_log_prelaunch:debug(
922+
"Starting the following plugins: ~p", [Plugins]),
911923
lists:foreach(
912924
fun(Plugin) ->
913-
ok = rabbit_boot_steps:run_boot_steps([Plugin]),
914925
case application:ensure_all_started(Plugin) of
915926
{ok, _} -> ok;
916927
Error -> throw(Error)
917928
end
918929
end, Plugins),
919930

920931
%% Export definitions after all plugins have been enabled,
921-
%% see rabbitmq/rabbitmq-server#2384
932+
%% see rabbitmq/rabbitmq-server#2384.
922933
case rabbit_definitions:maybe_load_definitions() of
923934
ok -> ok;
924935
DefLoadError -> throw(DefLoadError)
@@ -933,8 +944,9 @@ do_run_postlaunch_phase() ->
933944
%% The node is ready: mark it as such and log it.
934945
%% NOTE: PLEASE DO NOT ADD CRITICAL NODE STARTUP CODE AFTER THIS.
935946
ok = rabbit_lager:broker_is_started(),
936-
ok = log_broker_started(
937-
rabbit_plugins:strictly_plugins(rabbit_plugins:active())),
947+
ActivePlugins = rabbit_plugins:active(),
948+
StrictlyPlugins = rabbit_plugins:strictly_plugins(ActivePlugins),
949+
ok = log_broker_started(StrictlyPlugins),
938950

939951
rabbit_log_prelaunch:info("Resetting node maintenance status"),
940952
%% successful boot resets node maintenance state

deps/rabbit/src/rabbit_direct.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ auth_fun({Username, Password}, VHost, ExtraAuthProps) ->
7575
connect(Creds, VHost, Protocol, Pid, Infos) ->
7676
ExtraAuthProps = extract_extra_auth_props(Creds, VHost, Pid, Infos),
7777
AuthFun = auth_fun(Creds, VHost, ExtraAuthProps),
78-
case rabbit:is_running() of
78+
case rabbit_boot_state:has_reached_and_is_active(core_started) of
7979
true ->
8080
case whereis(rabbit_direct_client_sup) of
8181
undefined ->

0 commit comments

Comments
 (0)