Skip to content

Commit

Permalink
Merge pull request #3386 from esl/mu-allow-global-in-mongoose_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa authored Nov 5, 2021
2 parents 2ed6b62 + 9299add commit d21eb5d
Show file tree
Hide file tree
Showing 26 changed files with 177 additions and 197 deletions.
109 changes: 0 additions & 109 deletions src/ejabberd_gen_sm.erl

This file was deleted.

75 changes: 29 additions & 46 deletions src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
}.
-type info() :: #{info_key() => any()}.

-type backend_type() :: mnesia | redis.
-type backend() :: ejabberd_sm_mnesia | ejabberd_sm_redis.
-type close_reason() :: resumed | normal | replaced.
-type info_key() :: atom().
Expand Down Expand Up @@ -222,9 +221,8 @@ open_session(HostType, SID, JID, Info) ->
Info :: info(),
ReplacedPids :: [pid()].
open_session(HostType, SID, JID, Priority, Info) ->
BackendModule = sm_backend(),
set_session(SID, JID, Priority, Info),
ReplacedPIDs = check_for_sessions_to_replace(HostType, BackendModule, JID),
ReplacedPIDs = check_for_sessions_to_replace(HostType, JID),
mongoose_hooks:sm_register_connection_hook(HostType, SID, JID, Info),
ReplacedPIDs.

Expand All @@ -236,14 +234,13 @@ open_session(HostType, SID, JID, Priority, Info) ->
Acc1 :: mongoose_acc:t().
close_session(Acc, SID, JID, Reason) ->
#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
BackendModule = sm_backend(),
Info = case ejabberd_gen_sm:get_sessions(BackendModule, LUser, LServer, LResource) of
Info = case ejabberd_sm_backend:get_sessions(LUser, LServer, LResource) of
[Session] ->
Session#session.info;
_ ->
[]
end,
ejabberd_gen_sm:delete_session(BackendModule, SID, LUser, LServer, LResource),
ejabberd_sm_backend:delete_session(SID, LUser, LServer, LResource),
mongoose_hooks:sm_remove_connection_hook(Acc, SID, JID, Info, Reason).

-spec store_info(jid:jid(), info_key(), any()) ->
Expand Down Expand Up @@ -335,7 +332,7 @@ disconnect_removed_user(Acc, User, Server) ->
-spec get_user_resources(JID :: jid:jid()) -> [binary()].
get_user_resources(JID) ->
#jid{luser = LUser, lserver = LServer} = JID,
Ss = ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer),
Ss = ejabberd_sm_backend:get_sessions(LUser, LServer),
[element(3, S#session.usr) || S <- clean_session_list(Ss)].


Expand All @@ -355,7 +352,7 @@ get_session_ip(JID) ->
JID :: jid:jid().
get_session(JID) ->
#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
case ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer, LResource) of
case ejabberd_sm_backend:get_sessions(LUser, LServer, LResource) of
[] ->
offline;
Ss ->
Expand All @@ -365,7 +362,7 @@ get_session(JID) ->
-spec get_raw_sessions(jid:jid()) -> [session()].
get_raw_sessions(#jid{luser = LUser, lserver = LServer}) ->
clean_session_list(
ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer)).
ejabberd_sm_backend:get_sessions(LUser, LServer)).

-spec set_presence(Acc, SID, JID, Prio, Presence, Info) -> Acc1 when
Acc :: mongoose_acc:t(),
Expand Down Expand Up @@ -415,7 +412,7 @@ get_session_pid(JID) ->
-spec get_unique_sessions_number() -> integer().
get_unique_sessions_number() ->
try
C = ejabberd_gen_sm:unique_count(sm_backend()),
C = ejabberd_sm_backend:unique_count(),
mongoose_metrics:update(global, ?UNIQUE_COUNT_CACHE, C),
C
catch
Expand All @@ -426,17 +423,17 @@ get_unique_sessions_number() ->

-spec get_total_sessions_number() -> integer().
get_total_sessions_number() ->
ejabberd_gen_sm:total_count(sm_backend()).
ejabberd_sm_backend:total_count().


-spec get_vh_session_number(jid:server()) -> non_neg_integer().
get_vh_session_number(Server) ->
length(ejabberd_gen_sm:get_sessions(sm_backend(), Server)).
length(ejabberd_sm_backend:get_sessions(Server)).


-spec get_vh_session_list(jid:server()) -> [session()].
get_vh_session_list(Server) ->
ejabberd_gen_sm:get_sessions(sm_backend(), Server).
ejabberd_sm_backend:get_sessions(Server).


-spec get_node_sessions_number() -> non_neg_integer().
Expand All @@ -448,7 +445,7 @@ get_node_sessions_number() ->

-spec get_full_session_list() -> [session()].
get_full_session_list() ->
ejabberd_gen_sm:get_sessions(sm_backend()).
ejabberd_sm_backend:get_sessions().


register_iq_handler(Host, XMLNS, IQHandler) ->
Expand Down Expand Up @@ -499,18 +496,12 @@ init([]) ->
undefined -> {mnesia, []};
Value -> Value
end,
store_backend(Backend),

ejabberd_sm_backend:init([{backend, Backend}|Opts]),
ets:new(sm_iqtable, [named_table, protected, {read_concurrency, true}]),

ejabberd_hooks:add(node_cleanup, global, ?MODULE, node_cleanup, 50),
lists:foreach(fun(HostType) -> ejabberd_hooks:add(hooks(HostType)) end,
?ALL_HOST_TYPES),

ejabberd_commands:register_commands(commands()),

ejabberd_gen_sm:start(sm_backend(), Opts),

{ok, #state{}}.

hooks(HostType) ->
Expand All @@ -531,8 +522,7 @@ hooks(HostType) ->
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({node_cleanup, Node}, _From, State) ->
BackendModule = sm_backend(),
{TimeDiff, _R} = timer:tc(fun BackendModule:cleanup/1, [Node]),
{TimeDiff, _R} = timer:tc(fun ejabberd_sm_backend:cleanup/1, [Node]),
?LOG_INFO(#{what => sm_node_cleanup,
text => <<"Cleaning after a node that went down">>,
cleanup_node => Node,
Expand Down Expand Up @@ -627,7 +617,7 @@ set_session(SID, JID, Priority, Info) ->
us = US,
priority = Priority,
info = Info},
ejabberd_gen_sm:create_session(sm_backend(), LUser, LServer, LResource, Session).
ejabberd_sm_backend:create_session(LUser, LServer, LResource, Session).

-spec update_session(SID, JID, Prio, Info) -> ok | {error, any()} when
SID :: sid() | 'undefined',
Expand All @@ -643,7 +633,7 @@ update_session(SID, JID, Priority, Info) ->
us = US,
priority = Priority,
info = Info},
ejabberd_gen_sm:update_session(sm_backend(), LUser, LServer, LResource, Session).
ejabberd_sm_backend:update_session(LUser, LServer, LResource, Session).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down Expand Up @@ -896,13 +886,13 @@ clean_session_list([S1, S2 | Rest], Res) ->
LUser :: jid:luser(),
LServer :: jid:lserver().
get_user_present_pids(LUser, LServer) ->
Ss = ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer),
Ss = ejabberd_sm_backend:get_sessions(LUser, LServer),
[{S#session.priority, element(2, S#session.sid)} ||
S <- clean_session_list(Ss), is_integer(S#session.priority)].

-spec get_user_present_resources(jid:jid()) -> [{priority(), binary()}].
get_user_present_resources(#jid{luser = LUser, lserver = LServer}) ->
Ss = ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer),
Ss = ejabberd_sm_backend:get_sessions(LUser, LServer),
[{S#session.priority, element(3, S#session.usr)} ||
S <- clean_session_list(Ss), is_integer(S#session.priority)].

Expand All @@ -918,31 +908,29 @@ is_offline(#jid{luser = LUser, lserver = LServer}) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @doc On new session, check if some existing connections need to be replace
-spec check_for_sessions_to_replace(HostType, BackendModule, JID) -> ReplacedPids when
-spec check_for_sessions_to_replace(HostType, JID) -> ReplacedPids when
HostType :: mongooseim:host_type(),
BackendModule :: backend(),
JID :: jid:jid(),
ReplacedPids :: [pid()].
check_for_sessions_to_replace(HostType, BackendModule, JID) ->
check_for_sessions_to_replace(HostType, JID) ->
#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
%% TODO: Depending on how this is executed, there could be an unneeded
%% replacement for max_sessions. We need to check this at some point.
ReplacedRedundantSessions = check_existing_resources(HostType, BackendModule, LUser, LServer, LResource),
AllReplacedSessionPids = check_max_sessions(HostType, BackendModule, LUser, LServer, ReplacedRedundantSessions),
ReplacedRedundantSessions = check_existing_resources(HostType, LUser, LServer, LResource),
AllReplacedSessionPids = check_max_sessions(HostType, LUser, LServer, ReplacedRedundantSessions),
[Pid ! replaced || Pid <- AllReplacedSessionPids],
AllReplacedSessionPids.

-spec check_existing_resources(HostType, BackendModule, LUser, LServer, LResource) ->
-spec check_existing_resources(HostType, LUser, LServer, LResource) ->
ReplacedSessionsPIDs when
HostType :: mongooseim:host_type(),
BackendModule :: backend(),
LUser :: jid:luser(),
LServer :: jid:lserver(),
LResource :: jid:lresource(),
ReplacedSessionsPIDs :: ordsets:ordset(pid()).
check_existing_resources(_HostType, BackendModule, LUser, LServer, LResource) ->
check_existing_resources(_HostType, LUser, LServer, LResource) ->
%% A connection exist with the same resource. We replace it:
Sessions = ejabberd_gen_sm:get_sessions(BackendModule, LUser, LServer, LResource),
Sessions = ejabberd_sm_backend:get_sessions(LUser, LServer, LResource),
case [S#session.sid || S <- Sessions] of
[] -> [];
SIDs ->
Expand All @@ -951,12 +939,11 @@ check_existing_resources(_HostType, BackendModule, LUser, LServer, LResource) ->
end.

-spec check_max_sessions(HostType :: mongooseim:host_type(),
BackendModule :: backend(),
LUser :: jid:luser(),
LServer :: jid:lserver(),
ReplacedPIDs :: [pid()]) ->
AllReplacedPIDs :: ordsets:ordset(pid()).
check_max_sessions(HostType, BackendModule, LUser, LServer, ReplacedPIDs) ->
check_max_sessions(HostType, LUser, LServer, ReplacedPIDs) ->
%% If the max number of sessions for a given is reached, we replace the
%% first one
SIDs = lists:filtermap(
Expand All @@ -967,7 +954,7 @@ check_max_sessions(HostType, BackendModule, LUser, LServer, ReplacedPIDs) ->
false -> {true, SID}
end
end,
ejabberd_gen_sm:get_sessions(BackendModule, LUser, LServer)),
ejabberd_sm_backend:get_sessions(LUser, LServer)),
MaxSessions = get_max_user_sessions(HostType, LUser, LServer),
case length(SIDs) =< MaxSessions of
true -> ordsets:to_list(ReplacedPIDs);
Expand Down Expand Up @@ -1024,7 +1011,7 @@ process_iq(_, From, To, Acc, Packet) ->

-spec force_update_presence(mongooseim:host_type(), {jid:luser(), jid:lserver()}) -> 'ok'.
force_update_presence(_HostType, {LUser, LServer}) ->
Ss = ejabberd_gen_sm:get_sessions(sm_backend(), LUser, LServer),
Ss = ejabberd_sm_backend:get_sessions(LUser, LServer),
lists:foreach(fun(#session{sid = {_, Pid}}) ->
Pid ! {force_update_presence, LUser}
end, Ss).
Expand Down Expand Up @@ -1072,11 +1059,7 @@ get_cached_unique_count() ->
0
end.

%% It is used from big tests
-spec sm_backend() -> backend().
sm_backend() ->
persistent_term:get(sm_backend_module, ejabberd_sm_mnesia).

-spec store_backend(backend_type()) -> ok.
store_backend(Backend) ->
BackendModule = list_to_atom("ejabberd_sm_" ++ atom_to_list(Backend)),
persistent_term:put(sm_backend_module, BackendModule).
mongoose_backend:get_backend_module(global, ?MODULE).
Loading

0 comments on commit d21eb5d

Please sign in to comment.