25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Optimize room_unused_* commands

Previously to check if hibernated room was old enough we had to fetch info
about all rooms from database. Now we repurpose created_at field in sql
to store that info, that allow us to have more efficient query just for it.
This commit is contained in:
Paweł Chmielowski 2022-02-18 14:01:22 +01:00
parent f20e9e9b66
commit f86055378d
11 changed files with 99 additions and 42 deletions

View File

@ -285,6 +285,7 @@ CREATE TABLE muc_room (
); );
CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room (name, host); CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room (name, host);
CREATE INDEX i_muc_room_host_created_at ON muc_room USING btree (host, created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -260,6 +260,7 @@ CREATE TABLE muc_room (
); );
CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room (name, host); CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room (name, host);
CREATE INDEX i_muc_room_host_created_at ON muc_room USING btree (host, created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -114,6 +114,8 @@ CREATE TABLE [dbo].[muc_room] (
CREATE UNIQUE CLUSTERED INDEX [muc_room_name_host] ON [muc_room] (name, host) CREATE UNIQUE CLUSTERED INDEX [muc_room_name_host] ON [muc_room] (name, host)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON); WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON);
CREATE INDEX [muc_room_host_created_at] ON [muc_registered] (host, nick)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON);
CREATE TABLE [dbo].[muc_online_room] ( CREATE TABLE [dbo].[muc_online_room] (
[name] [varchar] (250) NOT NULL, [name] [varchar] (250) NOT NULL,

View File

@ -301,6 +301,7 @@ CREATE TABLE muc_room (
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE UNIQUE INDEX i_muc_room_name_host USING BTREE ON muc_room(name(75), host(75)); CREATE UNIQUE INDEX i_muc_room_name_host USING BTREE ON muc_room(name(75), host(75));
CREATE INDEX i_muc_room_host_created_at ON muc_room(host(75), created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -276,6 +276,7 @@ CREATE TABLE muc_room (
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE UNIQUE INDEX i_muc_room_name_host USING BTREE ON muc_room(name(75), host(75)); CREATE UNIQUE INDEX i_muc_room_name_host USING BTREE ON muc_room(name(75), host(75));
CREATE INDEX i_muc_room_host_created_at ON muc_room(host(75), created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -450,6 +450,7 @@ CREATE TABLE muc_room (
); );
CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room USING btree (name, host); CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room USING btree (name, host);
CREATE INDEX i_muc_room_host_created_at ON muc_room USING btree (host, created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -278,6 +278,7 @@ CREATE TABLE muc_room (
); );
CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room USING btree (name, host); CREATE UNIQUE INDEX i_muc_room_name_host ON muc_room USING btree (name, host);
CREATE INDEX i_muc_room_host_created_at ON muc_room USING btree (host, created_at);
CREATE TABLE muc_registered ( CREATE TABLE muc_registered (
jid text NOT NULL, jid text NOT NULL,

View File

@ -402,10 +402,10 @@ init([Host, Worker]) ->
{stop, normal, ok, state()}. {stop, normal, ok, state()}.
handle_call(stop, _From, State) -> handle_call(stop, _From, State) ->
{stop, normal, ok, State}; {stop, normal, ok, State};
handle_call({unhibernate, Room, Host}, _From, handle_call({unhibernate, Room, Host, ResetHibernationTime}, _From,
#{server_host := ServerHost} = State) -> #{server_host := ServerHost} = State) ->
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE), RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
{reply, load_room(RMod, Host, ServerHost, Room), State}; {reply, load_room(RMod, Host, ServerHost, Room, ResetHibernationTime), State};
handle_call({create, Room, Host, Opts}, _From, handle_call({create, Room, Host, Opts}, _From,
#{server_host := ServerHost} = State) -> #{server_host := ServerHost} = State) ->
?DEBUG("MUC: create new room '~ts'~n", [Room]), ?DEBUG("MUC: create new room '~ts'~n", [Room]),
@ -579,11 +579,15 @@ extract_password(#iq{} = IQ) ->
-spec unhibernate_room(binary(), binary(), binary()) -> {ok, pid()} | error. -spec unhibernate_room(binary(), binary(), binary()) -> {ok, pid()} | error.
unhibernate_room(ServerHost, Host, Room) -> unhibernate_room(ServerHost, Host, Room) ->
unhibernate_room(ServerHost, Host, Room, true).
-spec unhibernate_room(binary(), binary(), binary(), boolean()) -> {ok, pid()} | error.
unhibernate_room(ServerHost, Host, Room, ResetHibernationTime) ->
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE), RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
case RMod:find_online_room(ServerHost, Room, Host) of case RMod:find_online_room(ServerHost, Room, Host) of
error -> error ->
Proc = procname(ServerHost, {Room, Host}), Proc = procname(ServerHost, {Room, Host}),
case ?GEN_SERVER:call(Proc, {unhibernate, Room, Host}, 20000) of case ?GEN_SERVER:call(Proc, {unhibernate, Room, Host, ResetHibernationTime}, 20000) of
{ok, _} = R -> R; {ok, _} = R -> R;
_ -> error _ -> error
end; end;
@ -605,7 +609,7 @@ route_to_room(Packet, ServerHost) ->
Err = xmpp:err_item_not_found(ErrText, Lang), Err = xmpp:err_item_not_found(ErrText, Lang),
ejabberd_router:route_error(Packet, Err); ejabberd_router:route_error(Packet, Err);
StartType -> StartType ->
case load_room(RMod, Host, ServerHost, Room) of case load_room(RMod, Host, ServerHost, Room, true) of
{error, notfound} when StartType == start -> {error, notfound} when StartType == start ->
case check_create_room(ServerHost, Host, Room, From) of case check_create_room(ServerHost, Host, Room, From) of
true -> true ->
@ -849,28 +853,36 @@ load_permanent_rooms(Hosts, ServerHost, Opts) ->
lists:foreach( lists:foreach(
fun(R) -> fun(R) ->
{Room, _} = R#muc_room.name_host, {Room, _} = R#muc_room.name_host,
unhibernate_room(ServerHost, Host, Room) unhibernate_room(ServerHost, Host, Room, false)
end, get_rooms(ServerHost, Host)) end, get_rooms(ServerHost, Host))
end, Hosts); end, Hosts);
false -> false ->
ok ok
end. end.
-spec load_room(module(), binary(), binary(), binary()) -> {ok, pid()} | -spec load_room(module(), binary(), binary(), binary(), boolean()) ->
{error, notfound | term()}. {ok, pid()} | {error, notfound | term()}.
load_room(RMod, Host, ServerHost, Room) -> load_room(RMod, Host, ServerHost, Room, ResetHibernationTime) ->
case restore_room(ServerHost, Host, Room) of case restore_room(ServerHost, Host, Room) of
error -> error ->
{error, notfound}; {error, notfound};
Opts0 -> Opts0 ->
Mod = gen_mod:db_mod(ServerHost, mod_muc),
case proplists:get_bool(persistent, Opts0) of case proplists:get_bool(persistent, Opts0) of
true -> true ->
?DEBUG("Restore room: ~ts", [Room]), ?DEBUG("Restore room: ~ts", [Room]),
start_room(RMod, Host, ServerHost, Room, Opts0); Res2 = start_room(RMod, Host, ServerHost, Room, Opts0),
case {Res2, ResetHibernationTime} of
{{ok, _}, true} ->
NewOpts = lists:keyreplace(hibernation_time, 1, Opts0, {hibernation_time, undefined}),
store_room(ServerHost, Host, Room, NewOpts, []);
_ ->
ok
end,
Res2;
_ -> _ ->
?DEBUG("Restore hibernated non-persistent room: ~ts", [Room]), ?DEBUG("Restore hibernated non-persistent room: ~ts", [Room]),
Res = start_room(RMod, Host, ServerHost, Room, Opts0), Res = start_room(RMod, Host, ServerHost, Room, Opts0),
Mod = gen_mod:db_mod(ServerHost, mod_muc),
case erlang:function_exported(Mod, get_subscribed_rooms, 3) of case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
true -> true ->
ok; ok;

View File

@ -858,7 +858,7 @@ rooms_report(Method, Action, Service, Days) ->
muc_unused(Method, Action, Service, Last_allowed) -> muc_unused(Method, Action, Service, Last_allowed) ->
%% Get all required info about all existing rooms %% Get all required info about all existing rooms
Rooms_all = get_all_rooms(Service), Rooms_all = get_all_rooms(Service, erlang:system_time(microsecond) - Last_allowed*24*60*60*1000),
%% Decide which ones pass the requirements %% Decide which ones pass the requirements
Rooms_pass = decide_rooms(Method, Rooms_all, Last_allowed), Rooms_pass = decide_rooms(Method, Rooms_all, Last_allowed),
@ -883,14 +883,14 @@ get_online_rooms(ServiceArg) ->
|| {RoomName, RoomHost, Pid} <- mod_muc:get_online_rooms(Host)] || {RoomName, RoomHost, Pid} <- mod_muc:get_online_rooms(Host)]
end, Hosts). end, Hosts).
get_all_rooms(ServiceArg) -> get_all_rooms(ServiceArg, Timestamp) ->
Hosts = find_services(ServiceArg), Hosts = find_services(ServiceArg),
lists:flatmap( lists:flatmap(
fun(Host) -> fun(Host) ->
get_all_rooms2(Host) get_all_rooms2(Host, Timestamp)
end, Hosts). end, Hosts).
get_all_rooms2(Host) -> get_all_rooms2(Host, Timestamp) ->
ServerHost = ejabberd_router:host_of_route(Host), ServerHost = ejabberd_router:host_of_route(Host),
OnlineRooms = get_online_rooms(Host), OnlineRooms = get_online_rooms(Host),
OnlineMap = lists:foldl( OnlineMap = lists:foldl(
@ -900,8 +900,11 @@ get_all_rooms2(Host) ->
Mod = gen_mod:db_mod(ServerHost, mod_muc), Mod = gen_mod:db_mod(ServerHost, mod_muc),
DbRooms = DbRooms =
case erlang:function_exported(Mod, get_rooms_without_subscribers, 2) of case {erlang:function_exported(Mod, get_rooms_without_subscribers, 2),
true -> erlang:function_exported(Mod, get_hibernated_rooms_older_than, 3)} of
{_, true} ->
Mod:get_hibernated_rooms_older_than(ServerHost, Host, Timestamp);
{true, _} ->
Mod:get_rooms_without_subscribers(ServerHost, Host); Mod:get_rooms_without_subscribers(ServerHost, Host);
_ -> _ ->
Mod:get_rooms(ServerHost, Host) Mod:get_rooms(ServerHost, Host)
@ -956,6 +959,8 @@ decide_room(unused, {_Room_name, _Host, ServerHost, Room_pid}, Last_allowed) ->
case lists:keyfind(hibernation_time, 1, Opts) of case lists:keyfind(hibernation_time, 1, Opts) of
false -> false ->
{NodeStartTime, 0}; {NodeStartTime, 0};
{_, undefined} ->
{NodeStartTime, 0};
{_, T} -> {_, T} ->
{T, 0} {T, 0}
end end

View File

@ -644,7 +644,7 @@ normal_state({route, ToNick,
normal_state(hibernate, StateData) -> normal_state(hibernate, StateData) ->
case maps:size(StateData#state.users) of case maps:size(StateData#state.users) of
0 -> 0 ->
store_room_no_checks(StateData, []), store_room_no_checks(StateData, [], erlang:system_time(microsecond)),
?INFO_MSG("Hibernating room ~ts@~ts", [StateData#state.room, StateData#state.host]), ?INFO_MSG("Hibernating room ~ts@~ts", [StateData#state.room, StateData#state.host]),
{stop, normal, StateData#state{hibernate_timer = hibernating}}; {stop, normal, StateData#state{hibernate_timer = hibernating}};
_ -> _ ->
@ -3997,8 +3997,8 @@ set_vcard_xupdate(State) ->
-define(MAKE_CONFIG_OPT(Opt), -define(MAKE_CONFIG_OPT(Opt),
{get_config_opt_name(Opt), element(Opt, Config)}). {get_config_opt_name(Opt), element(Opt, Config)}).
-spec make_opts(state()) -> [{atom(), any()}]. -spec make_opts(state(), integer | undefined) -> [{atom(), any()}].
make_opts(StateData) -> make_opts(StateData, HibernationTime) ->
Config = StateData#state.config, Config = StateData#state.config,
Subscribers = muc_subscribers_fold( Subscribers = muc_subscribers_fold(
fun(_LJID, Sub, Acc) -> fun(_LJID, Sub, Acc) ->
@ -4042,7 +4042,7 @@ make_opts(StateData) ->
{hats_users, {hats_users,
lists:map(fun({U, H}) -> {U, maps:to_list(H)} end, lists:map(fun({U, H}) -> {U, maps:to_list(H)} end,
maps:to_list(StateData#state.hats_users))}, maps:to_list(StateData#state.hats_users))},
{hibernation_time, erlang:system_time(microsecond)}, {hibernation_time, HibernationTime},
{subscribers, Subscribers}]. {subscribers, Subscribers}].
expand_opts(CompactOpts) -> expand_opts(CompactOpts) ->
@ -5004,13 +5004,13 @@ add_to_log(Type, Data, StateData)
when Type == roomconfig_change_disabledlogging -> when Type == roomconfig_change_disabledlogging ->
mod_muc_log:add_to_log(StateData#state.server_host, mod_muc_log:add_to_log(StateData#state.server_host,
roomconfig_change, Data, StateData#state.jid, roomconfig_change, Data, StateData#state.jid,
make_opts(StateData)); make_opts(StateData, undefined));
add_to_log(Type, Data, StateData) -> add_to_log(Type, Data, StateData) ->
case (StateData#state.config)#config.logging of case (StateData#state.config)#config.logging of
true -> true ->
mod_muc_log:add_to_log(StateData#state.server_host, mod_muc_log:add_to_log(StateData#state.server_host,
Type, Data, StateData#state.jid, Type, Data, StateData#state.jid,
make_opts(StateData)); make_opts(StateData, undefined));
false -> ok false -> ok
end. end.
@ -5075,16 +5075,16 @@ store_room(StateData, ChangesHints) ->
StateData#state.host, StateData#state.room, StateData#state.host, StateData#state.room,
ChangesHints); ChangesHints);
_ -> _ ->
store_room_no_checks(StateData, ChangesHints) store_room_no_checks(StateData, ChangesHints, undefined)
end; end;
true -> true ->
ok ok
end. end.
store_room_no_checks(StateData, ChangesHints) -> store_room_no_checks(StateData, ChangesHints, HibernationTime) ->
mod_muc:store_room(StateData#state.server_host, mod_muc:store_room(StateData#state.server_host,
StateData#state.host, StateData#state.room, StateData#state.host, StateData#state.room,
make_opts(StateData), make_opts(StateData, HibernationTime),
ChangesHints). ChangesHints).
-spec send_subscriptions_change_notifications(jid(), binary(), subscribe|unsubscribe, state()) -> ok. -spec send_subscriptions_change_notifications(jid(), binary(), subscribe|unsubscribe, state()) -> ok.

View File

@ -38,6 +38,7 @@
register_online_user/4, unregister_online_user/4, register_online_user/4, unregister_online_user/4,
count_online_rooms_by_user/3, get_online_rooms_by_user/3, count_online_rooms_by_user/3, get_online_rooms_by_user/3,
get_subscribed_rooms/3, get_rooms_without_subscribers/2, get_subscribed_rooms/3, get_rooms_without_subscribers/2,
get_hibernated_rooms_older_than/3,
find_online_room_by_pid/2, remove_user/2]). find_online_room_by_pid/2, remove_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5, -export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]). get_affiliations/3, search_affiliation/4]).
@ -64,13 +65,19 @@ store_room(LServer, Host, Name, Opts, ChangesHints) ->
_ -> {[], Opts} _ -> {[], Opts}
end, end,
SOpts = misc:term_to_expr(Opts2), SOpts = misc:term_to_expr(Opts2),
Timestamp = case lists:keyfind(hibernation_time, 1, Opts) of
false -> <<"1900-01-01 00:00:00">>;
{_, undefined} -> <<"1900-01-01 00:00:00">>;
{_, Time} -> usec_to_sql_timestamp(Time)
end,
F = fun () -> F = fun () ->
?SQL_UPSERT_T( ?SQL_UPSERT_T(
"muc_room", "muc_room",
["!name=%(Name)s", ["!name=%(Name)s",
"!host=%(Host)s", "!host=%(Host)s",
"server_host=%(LServer)s", "server_host=%(LServer)s",
"opts=%(SOpts)s"]), "opts=%(SOpts)s",
"created_at=%(Timestamp)s"]),
case ChangesHints of case ChangesHints of
Changes when is_list(Changes) -> Changes when is_list(Changes) ->
[change_room(Host, Name, Change) || Change <- Changes]; [change_room(Host, Name, Change) || Change <- Changes];
@ -179,6 +186,23 @@ get_rooms_without_subscribers(LServer, Host) ->
[] []
end. end.
get_hibernated_rooms_older_than(LServer, Host, Timestamp) ->
TimestampS = usec_to_sql_timestamp(Timestamp),
case catch ejabberd_sql:sql_query(
LServer,
?SQL("select @(name)s, @(opts)s from muc_room"
" where host=%(Host)s and created_at < %(TimestampS)s and created_at > '1900-01-01 00:00:00'")) of
{selected, RoomOpts} ->
lists:map(
fun({Room, Opts}) ->
OptsD = ejabberd_sql:decode_term(Opts),
#muc_room{name_host = {Room, Host},
opts = mod_muc:opts_to_binary(OptsD)}
end, RoomOpts);
_Err ->
[]
end.
get_rooms(LServer, Host) -> get_rooms(LServer, Host) ->
case catch ejabberd_sql:sql_query( case catch ejabberd_sql:sql_query(
LServer, LServer,
@ -497,3 +521,11 @@ clean_tables(ServerHost) ->
?ERROR_MSG("Failed to clean 'muc_online_users' table: ~p", [Err2]), ?ERROR_MSG("Failed to clean 'muc_online_users' table: ~p", [Err2]),
Err2 Err2
end. end.
usec_to_sql_timestamp(Timestamp) ->
case calendar:system_time_to_universal_time(Timestamp, microsecond) of
{{Year, Month, Day}, {Hour, Minute, Second}} ->
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0B "
"~2..0B:~2..0B:~2..0B",
[Year, Month, Day, Hour, Minute, Second]))
end.