Skip to content

Commit

Permalink
Merge pull request #3339 from esl/mod_last_remove_dynamic_backend_module
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Oct 19, 2021
2 parents f231c96 + c46fa10 commit fbfd33a
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 65 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
mod_bosh_backend, mod_event_pusher_push_backend,
mod_global_distrib_mapping_backend, mod_http_upload_backend,
mod_inbox_backend, mod_keystore_backend,
mod_last_backend, mod_mam_cassandra_arch_params,
mod_mam_cassandra_arch_params,
mod_mam_cassandra_prefs_params, mod_mam_muc_cassandra_arch_params,
mod_muc_db_backend, mod_muc_light_codec_backend,
mod_muc_light_db_backend, mod_offline_backend,
Expand Down
9 changes: 0 additions & 9 deletions src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
start_module/3,
start_backend_module/2,
start_backend_module/3,
get_backend_module/3,
stop_module/2,
stop_module_keep_config/2,
reload_module/3,
Expand Down Expand Up @@ -225,14 +224,6 @@ start_backend_module(Module, Opts, TrackedFuncs) ->
Backend = gen_mod:get_opt(backend, Opts, mnesia),
backend_module:create(Module, Backend, TrackedFuncs).

-spec get_backend_module(host_type(), module(), atom()) -> module().
get_backend_module(HostType, Module, DefaultBackend) ->
Backend = get_module_opt(HostType, Module, backend, DefaultBackend),
backend_module(Module, Backend).

backend_module(Module, Backend) ->
list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Backend)).

-spec is_app_running(_) -> boolean().
is_app_running(AppName) ->
%% Use a high timeout to prevent a false positive in a high load system
Expand Down
2 changes: 1 addition & 1 deletion src/mod_auth_token.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
start(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, no_queue),
mod_auth_token_backend:start(HostType),
mod_auth_token_backend:start(HostType, Opts),
ejabberd_hooks:add(hooks(HostType)),
gen_iq_handler:add_iq_handler_for_domain(
HostType, ?NS_ESL_TOKEN_AUTH, ejabberd_sm,
Expand Down
8 changes: 4 additions & 4 deletions src/mod_auth_token_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%%% @end
%%%----------------------------------------------------------------------
-module(mod_auth_token_backend).
-export([start/1,
-export([start/2,
revoke/2,
get_valid_sequence_number/2,
clean_tokens/2]).
Expand All @@ -26,9 +26,9 @@
%% ----------------------------------------------------------------------
%% API Functions

-spec start(HostType :: mongooseim:host_type()) -> ok.
start(HostType) ->
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, [], rdbms),
-spec start(HostType :: mongooseim:host_type(), Opts :: gen_mod:module_opts()) -> ok.
start(HostType, Opts) ->
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, [], [{backend, rdbms} | Opts]),
Args = [HostType],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

Expand Down
40 changes: 7 additions & 33 deletions src/mod_last.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@

-export([config_metrics/1]).

-define(MOD_LAST_BACKEND, mod_last_backend).
-ignore_xref([
{?MOD_LAST_BACKEND, init, 2},
{?MOD_LAST_BACKEND, get_last, 3},
{?MOD_LAST_BACKEND, count_active_users, 3},
{?MOD_LAST_BACKEND, set_last_info, 5},
{?MOD_LAST_BACKEND, remove_user, 3},
{?MOD_LAST_BACKEND, remove_domain, 2},
behaviour_info/1, on_presence_update/5, process_local_iq/4,
process_sm_iq/4, remove_user/3, session_cleanup/5, remove_domain/3
]).
Expand All @@ -73,34 +66,15 @@
%% ------------------------------------------------------------------
%% Backend callbacks

-type host_type() :: mongooseim:host_type().
-export_type([timestamp/0, status/0]).

-type timestamp() :: non_neg_integer().
-type status() :: binary().

-export_type([host_type/0, timestamp/0, status/0]).

-callback init(host_type(), gen_mod:module_opts()) -> ok.

-callback get_last(host_type(), jid:luser(), jid:lserver()) ->
{ok, timestamp(), status()} | {error, term()} | not_found.

-callback count_active_users(host_type(), jid:lserver(), timestamp()) ->
non_neg_integer().

-callback set_last_info(host_type(), jid:luser(), jid:lserver(), timestamp(), status()) ->
ok | {error, term()}.

-callback remove_user(host_type(), jid:luser(), jid:lserver()) ->
ok | {error, term()}.

-callback remove_domain(host_type(), jid:lserver()) ->
ok | {error, term()}.

-spec start(mongooseim:host_type(), list()) -> 'ok'.
start(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),

gen_mod:start_backend_module(?MODULE, Opts, [get_last, set_last_info]),
mod_last_backend:init(HostType, Opts),

[gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_LAST, Component, Fn, #{}, IQDisc) ||
Expand Down Expand Up @@ -203,7 +177,7 @@ process_sm_iq(Acc, From, To, #iq{type = get, sub_el = SubEl} = IQ, _Extra) ->
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:forbidden()]}}
end.

-spec make_response(host_type(), jlib:iq(), SubEl :: 'undefined' | [exml:element()],
-spec make_response(mongooseim:host_type(), jlib:iq(), SubEl :: 'undefined' | [exml:element()],
jid:jid(), allow | deny) -> jlib:iq().
make_response(_HostType, IQ, SubEl, _, deny) ->
IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:forbidden()]};
Expand Down Expand Up @@ -240,7 +214,7 @@ make_response(HostType, IQ, SubEl, JID, allow) ->
children = []}]}
end.

-spec get_last_info(host_type(), jid:luser(), jid:lserver())
-spec get_last_info(mongooseim:host_type(), jid:luser(), jid:lserver())
-> 'not_found' | {'ok', integer(), binary()}.
get_last_info(HostType, LUser, LServer) ->
case get_last(HostType, LUser, LServer) of
Expand Down Expand Up @@ -280,7 +254,7 @@ store_last_info(Acc, LUser, LServer, Status) ->
store_last_info(HostType, LUser, LServer, TimeStamp, Status),
Acc.

-spec store_last_info(host_type(), jid:luser(), jid:lserver(), timestamp(), status()) -> ok.
-spec store_last_info(mongooseim:host_type(), jid:luser(), jid:lserver(), timestamp(), status()) -> ok.
store_last_info(HostType, LUser, LServer, TimeStamp, Status) ->
case mod_last_backend:set_last_info(HostType, LUser, LServer, TimeStamp, Status) of
{error, Reason} ->
Expand All @@ -293,12 +267,12 @@ store_last_info(HostType, LUser, LServer, TimeStamp, Status) ->
ok
end.

-spec get_last(host_type(), jid:luser(), jid:lserver()) ->
-spec get_last(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
{ok, timestamp(), status()} | {error, term()} | not_found.
get_last(HostType, LUser, LServer) ->
mod_last_backend:get_last(HostType, LUser, LServer).

-spec count_active_users(host_type(), jid:lserver(), timestamp()) -> non_neg_integer().
-spec count_active_users(mongooseim:host_type(), jid:lserver(), timestamp()) -> non_neg_integer().
count_active_users(HostType, LServer, Timestamp) ->
mod_last_backend:count_active_users(HostType, LServer, Timestamp).

Expand Down
74 changes: 74 additions & 0 deletions src/mod_last_backend.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
%% Just a proxy interface module between the main mod_last module and
%% the backend modules (i.e. mod_last_rdbms, mod_last_mnesia...).
-module(mod_last_backend).

-export([init/2,
get_last/3,
count_active_users/3,
set_last_info/5,
remove_user/3,
remove_domain/2]).

-define(MAIN_MODULE, mod_last).

-callback init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.

-callback get_last(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
{ok, mod_last:timestamp(), mod_last:status()} | {error, term()} | not_found.

-callback count_active_users(mongooseim:host_type(), jid:lserver(), mod_last:timestamp()) ->
non_neg_integer().

-callback set_last_info(
mongooseim:host_type(),
jid:luser(),
jid:lserver(),
mod_last:timestamp(),
mod_last:status()) -> ok | {error, term()}.

-callback remove_user(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
ok | {error, term()}.

-callback remove_domain(mongooseim:host_type(), jid:lserver()) ->
ok | {error, term()}.

-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
init(HostType, Opts) ->
TrackedFuns = [get_last, set_last_info],
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, TrackedFuns, Opts),
Args = [HostType, Opts],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec get_last(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
{ok, mod_last:timestamp(), mod_last:status()} | {error, term()} | not_found.
get_last(HostType, LUser, LServer) ->
Args = [HostType, LUser, LServer],
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec count_active_users(mongooseim:host_type(), jid:lserver(), mod_last:timestamp()) ->
non_neg_integer().
count_active_users(HostType, LServer, Timestamp) ->
Args = [HostType, LServer, Timestamp],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec set_last_info(
mongooseim:host_type(),
jid:luser(),
jid:lserver(),
mod_last:timestamp(),
mod_last:status()) -> ok | {error, term()}.
set_last_info(HostType, LUser, LServer, Timestamp, Status) ->
Args = [HostType, LUser, LServer, Timestamp, Status],
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec remove_user(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
ok | {error, term()}.
remove_user(HostType, LUser, LServer) ->
Args = [HostType, LUser, LServer],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec remove_domain(mongooseim:host_type(), jid:lserver()) ->
ok | {error, term()}.
remove_domain(HostType, LServer) ->
Args = [HostType, LServer],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).
2 changes: 1 addition & 1 deletion src/mod_last_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

-module(mod_last_mnesia).

-behaviour(mod_last).
-behaviour(mod_last_backend).

-include("mod_last.hrl").
-include("mongoose.hrl").
Expand Down
2 changes: 1 addition & 1 deletion src/mod_last_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

-module(mod_last_rdbms).

-behaviour(mod_last).
-behaviour(mod_last_backend).

-include("mongoose.hrl").

Expand Down
2 changes: 1 addition & 1 deletion src/mod_last_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
%%% @end
-module(mod_last_riak).

-behaviour(mod_last).
-behaviour(mod_last_backend).

-include("mongoose.hrl").

Expand Down
2 changes: 1 addition & 1 deletion src/mod_private_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
Opts :: list().
init(HostType, Opts) ->
TrackedFuns = [multi_get_data, multi_set_data],
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, TrackedFuns),
mongoose_backend:init_per_host_type(HostType, ?MAIN_MODULE, TrackedFuns, Opts),
Args = [HostType, Opts],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

Expand Down
21 changes: 9 additions & 12 deletions src/mongoose_backend.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
-module(mongoose_backend).

%% API
-export([init_per_host_type/3,
init_per_host_type/4,
-export([init_per_host_type/4,
call/4,
call_tracked/4]).

Expand All @@ -17,22 +16,20 @@
-type main_module() :: module().
-type backend_module() :: module().

-spec init_per_host_type(HostType :: mongooseim:host_type(),
MainModule :: main_module(),
TrackedFuns :: [function_name()]) -> ok.
init_per_host_type(HostType, MainModule, TrackedFuns) ->
init_per_host_type(HostType, MainModule, TrackedFuns, mnesia).

-spec init_per_host_type(HostType :: mongooseim:host_type(),
MainModule :: main_module(),
TrackedFuns :: [function_name()],
DefaultBackend :: atom()) -> ok.
init_per_host_type(HostType, MainModule, TrackedFuns, DefaultBackend) ->
Opts :: gen_mod:module_opts()) -> ok.
init_per_host_type(HostType, MainModule, TrackedFuns, Opts) ->
ensure_backend_metrics(MainModule, TrackedFuns),
Backend = gen_mod:get_backend_module(HostType, MainModule, DefaultBackend),
persist_backend_name(HostType, MainModule, Backend),
Backend = gen_mod:get_opt(backend, Opts, mnesia),
BackendModule = backend_module(MainModule, Backend),
persist_backend_name(HostType, MainModule, BackendModule),
ok.

backend_module(Module, Backend) ->
list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Backend)).

call_metric(MainModule, FunName) ->
[backends, MainModule, calls, FunName].

Expand Down
2 changes: 1 addition & 1 deletion test/auth_tokens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mock_mongoose_metrics() ->

mock_rdbms_backend() ->
meck:new(mod_auth_token_backend, []),
meck:expect(mod_auth_token_backend, start, fun(_) -> ok end),
meck:expect(mod_auth_token_backend, start, fun(_, _) -> ok end),
meck:expect(mod_auth_token_backend, get_valid_sequence_number,
fun (_, _) -> valid_seq_no_threshold() end),
ok.
Expand Down

0 comments on commit fbfd33a

Please sign in to comment.