Skip to content

Commit

Permalink
Merge pull request #4069 from esl/domain_workers_supervisor
Browse files Browse the repository at this point in the history
Initialise domain workers in the supervision tree instead of manually
  • Loading branch information
DenysGonchar authored Aug 3, 2023
2 parents 2dff20b + 73ef2c5 commit 36493bc
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 140 deletions.
6 changes: 6 additions & 0 deletions big_tests/tests/domain_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ domain_to_host_type(Node, Domain) ->
{ok, HostType} = rpc(Node, mongoose_domain_core, get_host_type, [Domain]),
HostType.

restart_domain_core(Node) ->
{ok, _} = rpc(Node, mongoose_domain_sup, restart_core, [[]]).

restart_domain_core(Node, Pairs, AllowedHostTypes) ->
{ok, _} = rpc(Node, mongoose_domain_sup, restart_core, [[Pairs, AllowedHostTypes]]).

domain(NodeKey) ->
get_or_fail({hosts, NodeKey, domain}).

Expand Down
6 changes: 2 additions & 4 deletions big_tests/tests/service_domain_db_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1110,12 +1110,10 @@ service_disabled(Node) ->
false = rpc(Node, service_domain_db, enabled, []).

restart_domain_core(Node, Pairs, AllowedHostTypes) ->
ok = rpc(Node, mongoose_domain_core, stop, []),
ok = rpc(Node, mongoose_domain_core, start, [Pairs, AllowedHostTypes]).
domain_helper:restart_domain_core(Node, Pairs, AllowedHostTypes).

restart_domain_core(Node) ->
ok = rpc(Node, mongoose_domain_core, stop, []),
ok = rpc(Node, mongoose_domain_core, start, []).
domain_helper:restart_domain_core(Node).

insert_domain(Node, Domain, HostType) ->
rpc(Node, mongoose_domain_api, insert_domain, [Domain, HostType]).
Expand Down
2 changes: 1 addition & 1 deletion doc/developers-guide/domain_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ It provides the following interfaces:
Any of these lists can be empty, initial list of domain/host\_type pairs can
have some unique host\_types not mentioned in the host\_types list.
The component is initialised by the main MIM supervisor.
Implemented in `mongoose_domain_api:init()`.
Implemented in `mongoose_domain_sup:start_link/0`.
- Insert - adding new domain/host\_type pair.
This function is idempotent. It returns success on an attempt to insert the existing data,
but fails if ETS already has the domain name associated with another host type.
Expand Down
20 changes: 1 addition & 19 deletions src/domain/mongoose_domain_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

-include("mongoose_logger.hrl").

-export([init/0,
stop/0,
get_host_type/1]).
-export([get_host_type/1]).

%% external domain API for GraphQL or REST handlers
-export([insert_domain/2,
Expand Down Expand Up @@ -45,7 +43,6 @@

-ignore_xref([get_all_static/0]).
-ignore_xref([get_all_dynamic/0]).
-ignore_xref([stop/0]).

-type status() :: enabled | disabled | deleting.
-type domain() :: jid:lserver().
Expand All @@ -66,21 +63,6 @@

-export_type([status/0, remove_domain_acc/0]).

-spec init() -> ok | {error, term()}.
init() ->
mongoose_domain_core:start(),
mongoose_subdomain_core:start(),
mongoose_lazy_routing:start().

%% Stops gen_servers, that are started from init/0
%% Does not fail, even if servers are already stopped
-spec stop() -> ok.
stop() ->
catch mongoose_domain_core:stop(),
catch mongoose_subdomain_core:stop(),
catch mongoose_lazy_routing:stop(),
ok.

-spec insert_domain(domain(), host_type()) -> insert_result().
insert_domain(Domain, HostType) ->
M = #{domain => Domain, host_type => HostType},
Expand Down
42 changes: 1 addition & 41 deletions src/domain/mongoose_domain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
%% required for ets:fun2ms/1 pseudo function
-include_lib("stdlib/include/ms_transform.hrl").

-export([start/0, start/2, stop/0]).
-export([start_link/2]).
-export([get_host_type/1]).
-export([is_static/1]).
Expand All @@ -34,45 +33,13 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-ignore_xref([get_start_args/0, start_link/2, start/2, stop/0]).
-ignore_xref([get_start_args/0, start_link/2]).

-define(TABLE, ?MODULE).
-define(HOST_TYPE_TABLE, mongoose_domain_core_host_types).

-type host_type() :: mongooseim:host_type().
-type domain() :: mongooseim:domain_name().
-type pair() :: {domain(), host_type()}.

start() ->
Pairs = get_static_pairs(),
AllowedHostTypes = mongoose_config:get_opt(host_types),
start(Pairs, AllowedHostTypes).

-ifdef(TEST).

%% required for unit tests
start(Pairs, AllowedHostTypes) ->
just_ok(gen_server:start({local, ?MODULE}, ?MODULE, [Pairs, AllowedHostTypes], [])).

stop() ->
gen_server:stop(?MODULE).

-else.

start(Pairs, AllowedHostTypes) ->
ChildSpec =
{?MODULE,
{?MODULE, start_link, [Pairs, AllowedHostTypes]},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
ok.

-endif.

start_link(Pairs, AllowedHostTypes) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Pairs, AllowedHostTypes], []).
Expand Down Expand Up @@ -182,10 +149,6 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%% internal functions
%%--------------------------------------------------------------------
%% Domains should be nameprepped using `jid:nameprep'
-spec get_static_pairs() -> [pair()].
get_static_pairs() ->
[{H, H} || H <- mongoose_config:get_opt(hosts)].

for_each_selected_domain('$end_of_table', _) -> ok;
for_each_selected_domain({MatchList, Continuation}, Func) ->
Expand All @@ -208,9 +171,6 @@ insert_initial_pair(Tab, Domain, HostType) ->
new_object(Domain, HostType, Source) ->
{Domain, HostType, Source}.

just_ok({ok, _}) -> ok;
just_ok(Other) -> Other.

insert_host_types(Tab, AllowedHostTypes) ->
lists:foreach(fun(HostType) ->
ets:insert_new(Tab, {HostType})
Expand Down
44 changes: 44 additions & 0 deletions src/domain/mongoose_domain_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-module(mongoose_domain_sup).

-behaviour(supervisor).

-type pair() :: {mongooseim:domain_name(), mongooseim:host_type()}.

-export([start_link/0, init/1]).
-ignore_xref([start_link/0]).

-export([start_link/2, restart_core/1]).
-ignore_xref([start_link/2, restart_core/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

start_link(Pairs, AllowedHostTypes) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [Pairs, AllowedHostTypes]).

restart_core(Args) ->
supervisor:terminate_child(?MODULE, mongoose_domain_core),
supervisor:delete_child(?MODULE, mongoose_domain_core),
supervisor:start_child(?MODULE, worker_spec(mongoose_domain_core, fill_args(Args))).

init(Args) ->
DomainCore = worker_spec(mongoose_domain_core, fill_args(Args)),
SubdomainCore = worker_spec(mongoose_subdomain_core, []),
LazyRouting = worker_spec(mongoose_lazy_routing, []),
{ok, {{one_for_one, 10, 1},
[DomainCore, SubdomainCore, LazyRouting]}}.

worker_spec(Mod, Args) ->
{Mod, {Mod, start_link, Args}, permanent, timer:seconds(5), worker, [Mod]}.

%% Domains should be nameprepped using `jid:nameprep'
-spec get_static_pairs() -> [pair()].
get_static_pairs() ->
[{H, H} || H <- mongoose_config:get_opt(hosts)].

fill_args([]) ->
Pairs = get_static_pairs(),
AllowedHostTypes = mongoose_config:get_opt(host_types),
[Pairs, AllowedHostTypes];
fill_args([_, _] = Args) ->
Args.
29 changes: 1 addition & 28 deletions src/domain/mongoose_lazy_routing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

-include("mongoose_logger.hrl").

%% start/stop API
-export([start/0, stop/0]).
-export([start_link/0]).
-export([sync/0]).

Expand All @@ -43,7 +41,7 @@
terminate/2,
code_change/3]).

-ignore_xref([start_link/0, stop/0, sync/0]).
-ignore_xref([start_link/0, sync/0]).

-define(IQ_TABLE, mongoose_lazy_routing_iqs).
-define(ROUTING_TABLE, mongoose_lazy_routing).
Expand All @@ -66,29 +64,6 @@
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
-ifdef(TEST).

%% required for unit tests
start() ->
just_ok(gen_server:start({local, ?MODULE}, ?MODULE, [], [])).

stop() ->
gen_server:stop(?MODULE).

-else.

start() ->
ChildSpec = {?MODULE, {?MODULE, start_link, []},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
ok.

-endif.

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
Expand Down Expand Up @@ -221,8 +196,6 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%% local functions
%%--------------------------------------------------------------------
just_ok({ok, _}) -> ok;
just_ok(Other) -> Other.

-spec handle_register_iq_handler_for_domain(HostType :: mongooseim:host_type(),
Namespace :: binary(),
Expand Down
28 changes: 2 additions & 26 deletions src/domain/mongoose_subdomain_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
-behaviour(gen_server).

-include("mongoose_logger.hrl").

%% API
-export([start/0, stop/0]).
-export([start_link/0]).

-export([register_subdomain/3,
Expand All @@ -26,7 +26,7 @@
code_change/3,
terminate/2]).

-ignore_xref([start_link/0, stop/0, sync/0]).
-ignore_xref([start_link/0, sync/0]).

-ifdef(TEST).

Expand Down Expand Up @@ -69,30 +69,8 @@
%% API
%%--------------------------------------------------------------------
-ifdef(TEST).

%% required for unit tests
start() ->
just_ok(gen_server:start({local, ?MODULE}, ?MODULE, [], [])).

stop() ->
gen_server:stop(?MODULE).

%% this interface is required only to detect errors in unit tests
log_error(_Function, _Error) -> ok.

-else.

start() ->
ChildSpec = {?MODULE, {?MODULE, start_link, []},
permanent, infinity, worker, [?MODULE]},
just_ok(supervisor:start_child(ejabberd_sup, ChildSpec)).

%% required for integration tests
stop() ->
supervisor:terminate_child(ejabberd_sup, ?MODULE),
supervisor:delete_child(ejabberd_sup, ?MODULE),
ok.

-endif.

start_link() ->
Expand Down Expand Up @@ -196,8 +174,6 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%% local functions
%%--------------------------------------------------------------------
just_ok({ok, _}) -> ok;
just_ok(Other) -> Other.

-spec handle_register(host_type(), subdomain_pattern(),
mongoose_packet_handler:t(), any()) -> ok.
Expand Down
1 change: 0 additions & 1 deletion src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ start(normal, _Args) ->
mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel)),
mongoose_deprecations:start(),
{ok, _} = Sup = ejabberd_sup:start_link(),
mongoose_domain_api:init(),
mongoose_wpool:ensure_started(),
mongoose_wpool:start_configured_pools(),
%% ejabberd_sm is started separately because it may use one of the outgoing_pools
Expand Down
7 changes: 6 additions & 1 deletion src/ejabberd_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ init([]) ->
{mongoose_shaper_sup,
{mongoose_shaper_sup, start_link, []},
permanent, infinity, supervisor, [mongoose_shaper_sup]},
DomainSup =
{mongoose_domain_sup,
{mongoose_domain_sup, start_link, []},
permanent, infinity, supervisor, [mongoose_domain_sup]},
PG =
{pg,
{pg, start_link, [mim_scope]},
Expand All @@ -170,7 +174,8 @@ init([]) ->
IQSupervisor,
Listener,
MucIQ,
ShaperSup]}}.
ShaperSup,
DomainSup]}}.

start_child(ChildSpec) ->
case supervisor:start_child(ejabberd_sup, ChildSpec) of
Expand Down
7 changes: 2 additions & 5 deletions test/acl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ init_per_suite(Config) ->
Config.

end_per_suite(_Config) ->
mongoose_domain_api:stop(),
meck:unload(),
ok.

Expand Down Expand Up @@ -303,12 +302,10 @@ given_registered_domains(Config, DomainsList) ->
register_static_domains(DomainsList) ->
mongoose_config:set_opt(hosts, DomainsList),
mongoose_config:set_opt(host_types, []),
mongoose_domain_api:stop(),
mongoose_domain_api:init().
mongoose_domain_sup:start_link().

register_dynamic_domains(DomainsList) ->
mongoose_config:set_opt(hosts, []),
mongoose_config:set_opt(host_types, [<<"test type">>, <<"empty type">>]),
mongoose_domain_api:stop(),
mongoose_domain_api:init(),
mongoose_domain_sup:start_link(),
[mongoose_domain_core:insert(Domain, <<"test type">>, test) || Domain <- DomainsList].
9 changes: 7 additions & 2 deletions test/auth_internal_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ init_per_suite(C) ->
internal => #{},
password => #{format => scram,
scram_iterations => 10}}),
mongoose_domain_core:start([{domain(), host_type()}], []),
ejabberd_auth_internal:start(host_type()),
C.

end_per_suite(_C) ->
ejabberd_auth_internal:stop(host_type()),
mongoose_domain_core:stop(),
mongoose_config:unset_opt({auth, host_type()}),
mnesia:stop(),
mnesia:delete_schema([node()]).

init_per_testcase(_TC, Config) ->
mongoose_domain_core:start_link([{domain(), host_type()}], []),
Config.

end_per_testcase(_TC, _Config) ->
ok.

passwords_as_records_are_still_supported(_C) ->
%% given in mnesia there is a user with password in old scram format
{U, S, P} = gen_user(),
Expand Down
Loading

0 comments on commit 36493bc

Please sign in to comment.