From 1e065b8c8f5cf28a4f94f3d58a40a64fa052ec03 Mon Sep 17 00:00:00 2001 From: Gustaw Lippa Date: Mon, 18 Oct 2021 17:07:12 +0200 Subject: [PATCH 1/3] Add mongoose_backend:get_backend_name It returns the atom describing the backend, like `mssql`, `pgsql`, etc. This is analogous to `backend_name` function that was exposed by the dynamically compiled modules in `backend_module.erl`. It is used by some tests and `mongoose_rdbms`. --- src/mongoose_backend.erl | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/mongoose_backend.erl b/src/mongoose_backend.erl index 853b7808ea..0a24d8b219 100644 --- a/src/mongoose_backend.erl +++ b/src/mongoose_backend.erl @@ -3,7 +3,8 @@ %% API -export([init_per_host_type/4, call/4, - call_tracked/4]). + call_tracked/4, + get_backend_name/2]). %% Legacy call from backend_module -export([ensure_backend_metrics/2]). @@ -24,7 +25,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) -> @@ -50,18 +51,28 @@ ensure_backend_metrics(MainModule, FunNames) -> end, lists:foreach(EnsureFun, FunNames). -persist_backend_name(HostType, MainModule, Backend) -> +persist_backend_name(HostType, MainModule, Backend, BackendModule) -> Key = backend_key(HostType, MainModule), - persistent_term:put(Key, Backend). + persistent_term:put(Key, {Backend, BackendModule}). -%% 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 - persistent_term:get(Key). + {_BackendName, BackendModule} = persistent_term:get(Key), + BackendModule. + +%% @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_key(HostType, MainModule), + %% Would crash, if the key is missing + {BackendName, _BackendModule} = persistent_term:get(Key), + BackendName. -spec call(HostType :: mongooseim:host_type(), MainModule :: main_module(), From bffd68157eb1998b618eac617f4e329f1fc83173 Mon Sep 17 00:00:00 2001 From: Gustaw Lippa Date: Mon, 18 Oct 2021 17:13:47 +0200 Subject: [PATCH 2/3] Make get_backend helper use HostType Because backends can be running per host type. This also fixes an issue of suites logging that they finished dirty, after having dynamically compiled backends removed. The old interface is called for the time being, but will be removed when all modules will have dynamic backends converted. --- big_tests/tests/mongoose_helper.erl | 38 ++++++++++++++++++++++------- big_tests/tests/offline_SUITE.erl | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/big_tests/tests/mongoose_helper.erl b/big_tests/tests/mongoose_helper.erl index 0fcc14e3ba..33191e1a5e 100644 --- a/big_tests/tests/mongoose_helper.erl +++ b/big_tests/tests/mongoose_helper.erl @@ -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]). @@ -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 @@ -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. diff --git a/big_tests/tests/offline_SUITE.erl b/big_tests/tests/offline_SUITE.erl index fe7bd59695..1f5734aa0b 100644 --- a/big_tests/tests/offline_SUITE.erl +++ b/big_tests/tests/offline_SUITE.erl @@ -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}, From 01fb11a7ca8a5d78d3905b82e5e92aed35ee8723 Mon Sep 17 00:00:00 2001 From: Gustaw Lippa Date: Tue, 19 Oct 2021 15:15:11 +0200 Subject: [PATCH 3/3] Use different keys --- src/mongoose_backend.erl | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/mongoose_backend.erl b/src/mongoose_backend.erl index 0a24d8b219..85044acc2f 100644 --- a/src/mongoose_backend.erl +++ b/src/mongoose_backend.erl @@ -6,10 +6,13 @@ 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]). @@ -40,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) -> @@ -52,27 +58,25 @@ ensure_backend_metrics(MainModule, FunNames) -> lists:foreach(EnsureFun, FunNames). persist_backend_name(HostType, MainModule, Backend, BackendModule) -> - Key = backend_key(HostType, MainModule), - persistent_term:put(Key, {Backend, BackendModule}). + ModuleKey = backend_key(HostType, MainModule), + persistent_term:put(ModuleKey, BackendModule), + NameKey = backend_name_key(HostType, MainModule), + persistent_term:put(NameKey, Backend). %% @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 - {_BackendName, BackendModule} = persistent_term:get(Key), - BackendModule. + 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_key(HostType, MainModule), - %% Would crash, if the key is missing - {BackendName, _BackendModule} = persistent_term:get(Key), - BackendName. + Key = backend_name_key(HostType, MainModule), + persistent_term:get(Key). -spec call(HostType :: mongooseim:host_type(), MainModule :: main_module(),