Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mongoose_backend:get_backend_name #3345

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions big_tests/tests/mongoose_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

-export([is_rdbms_enabled/1,
mnesia_or_rdbms_backend/0,
get_backend_name/1]).
get_backend_name/2]).

-export([auth_modules/0]).

Expand Down Expand Up @@ -141,13 +141,30 @@ new_mongoose_acc(Location, Server) ->
clear_caps_cache(CapsNode) ->
ok = rpc(mim(), mod_caps, delete_caps, [CapsNode]).

get_backend(Module) ->
case rpc(mim(), Module, backend, []) of
{badrpc, _Reason} -> false;
Backend -> Backend
end.
get_backend(HostType, Module) ->
try rpc(mim(), mongoose_backend, get_backend_module, [HostType, Module])
catch
error:{badrpc, _Reason} ->
% TODO: get rid of this after dynamically compiled modules are gone
get_backend_old(Module)
end.

get_backend_name(HostType, Module) ->
try rpc(mim(), mongoose_backend, get_backend_name, [HostType, Module])
catch
error:{badrpc, _Reason} ->
% TODO: get rid of this after dynamically compiled modules are gone
% used by offline_SUITE
get_backend_name_old(Module)
end.

get_backend_old(Module) ->
case rpc(mim(), Module, backend, []) of
{badrpc, _Reason} -> false;
Backend -> Backend
end.

get_backend_name(Module) ->
get_backend_name_old(Module) ->
case rpc(mim(), Module, backend_name, []) of
{badrpc, _Reason} -> false;
Backend -> Backend
Expand All @@ -158,9 +175,12 @@ generic_count(mod_offline_backend, {LUser, LServer}) ->
rpc(mim(), mod_offline_backend, count_offline_messages, [HostType, LUser, LServer, 100]).

generic_count(Module) ->
case get_backend(Module) of
lists:sum([generic_count_per_host_type(HT, Module) || HT <- domain_helper:host_types()]).

generic_count_per_host_type(HostType, Module) ->
case get_backend(HostType, Module) of
false -> %% module disabled
false;
0;
B when is_atom(B) ->
generic_count_backend(B)
end.
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/offline_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ init_per_group(chatmarkers, C) ->
init_per_group(_, C) -> C.

with_groupchat_modules() ->
OfflineBackend = mongoose_helper:get_backend_name(mod_offline_backend),
OfflineBackend = mongoose_helper:get_backend_name(host_type(), mod_offline_backend),
MucLightBackend = mongoose_helper:mnesia_or_rdbms_backend(),
MucPattern = distributed_helper:subhost_pattern(muc_light_helper:muc_host_pattern()),
[{mod_offline, [{store_groupchat_messages, true},
Expand Down
33 changes: 24 additions & 9 deletions src/mongoose_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
%% API
-export([init_per_host_type/4,
call/4,
call_tracked/4]).
call_tracked/4,
get_backend_name/2]).
gustawlippa marked this conversation as resolved.
Show resolved Hide resolved

%% remove after mongoose_rdbms is refactored not to use dynamically compiled backend
-ignore_xref([get_backend_name/2]).

%% Legacy call from backend_module
-export([ensure_backend_metrics/2]).

%% For debugging
%% For debugging and tests
-export([get_backend_module/2]).
-ignore_xref([get_backend_module/2]).

Expand All @@ -24,7 +28,7 @@ init_per_host_type(HostType, MainModule, TrackedFuns, Opts) ->
ensure_backend_metrics(MainModule, TrackedFuns),
Backend = gen_mod:get_opt(backend, Opts, mnesia),
BackendModule = backend_module(MainModule, Backend),
persist_backend_name(HostType, MainModule, BackendModule),
persist_backend_name(HostType, MainModule, Backend, BackendModule),
ok.

backend_module(Module, Backend) ->
Expand All @@ -39,6 +43,9 @@ time_metric(MainModule, FunName) ->
backend_key(HostType, MainModule) ->
{backend_module, HostType, MainModule}.

backend_name_key(HostType, MainModule) ->
{backend_name, HostType, MainModule}.

-spec ensure_backend_metrics(MainModule :: main_module(),
FunNames :: [function_name()]) -> ok.
ensure_backend_metrics(MainModule, FunNames) ->
Expand All @@ -50,17 +57,25 @@ ensure_backend_metrics(MainModule, FunNames) ->
end,
lists:foreach(EnsureFun, FunNames).

persist_backend_name(HostType, MainModule, Backend) ->
Key = backend_key(HostType, MainModule),
persistent_term:put(Key, Backend).
persist_backend_name(HostType, MainModule, Backend, BackendModule) ->
ModuleKey = backend_key(HostType, MainModule),
gustawlippa marked this conversation as resolved.
Show resolved Hide resolved
persistent_term:put(ModuleKey, BackendModule),
NameKey = backend_name_key(HostType, MainModule),
persistent_term:put(NameKey, Backend).

%% Get a backend name, stored in init_per_host_type
%% @doc Get a backend module, stored in init_per_host_type.
-spec get_backend_module(HostType :: mongooseim:host_type(),
MainModule :: main_module()) ->
BackendModule :: backend_module().
get_backend_module(HostType, MainModule) ->
Key = backend_key(HostType, MainModule),
%% Would crash, if the key is missing
ModuleKey = backend_key(HostType, MainModule),
persistent_term:get(ModuleKey).

%% @doc Get a backend name, like `pgsql', stored in init_per_host_type.
-spec get_backend_name(HostType :: mongooseim:host_type(),
MainModule :: main_module()) -> BackendName :: atom().
get_backend_name(HostType, MainModule) ->
Key = backend_name_key(HostType, MainModule),
persistent_term:get(Key).

-spec call(HostType :: mongooseim:host_type(),
Expand Down