Skip to content

Commit

Permalink
Merge pull request #3345 from esl/wdbm-tests-mongoose_helper
Browse files Browse the repository at this point in the history
Add mongoose_backend:get_backend_name
  • Loading branch information
vkatsuba committed Oct 20, 2021
2 parents 154e492 + 01fb11a commit 24ec4b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
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]).

%% 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),
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

0 comments on commit 24ec4b1

Please sign in to comment.