25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Resize SQL pool on configuration reload

Fixes #2541
This commit is contained in:
Evgeny Khramtsov 2018-09-19 11:55:40 +03:00
parent d60d72d7bf
commit a2b2a27bb6
2 changed files with 46 additions and 19 deletions

View File

@ -71,7 +71,7 @@ get_spec(Host) ->
-spec config_reloaded() -> ok. -spec config_reloaded() -> ok.
config_reloaded() -> config_reloaded() ->
lists:foreach(fun start_host/1, ejabberd_config:get_myhosts()). lists:foreach(fun reload_host/1, ejabberd_config:get_myhosts()).
-spec start_host(binary()) -> ok. -spec start_host(binary()) -> ok.
start_host(Host) -> start_host(Host) ->
@ -96,6 +96,10 @@ stop_host(Host) ->
supervisor:delete_child(?MODULE, SupName), supervisor:delete_child(?MODULE, SupName),
ok. ok.
-spec reload_host(binary()) -> ok.
reload_host(Host) ->
ejabberd_sql_sup:reload(Host).
%% Returns {true, App} if we have configured sql for the given host %% Returns {true, App} if we have configured sql for the given host
needs_sql(Host) -> needs_sql(Host) ->
LHost = jid:nameprep(Host), LHost = jid:nameprep(Host),

View File

@ -31,21 +31,19 @@
-export([start_link/1, init/1, add_pid/2, remove_pid/2, -export([start_link/1, init/1, add_pid/2, remove_pid/2,
get_pids/1, get_random_pid/1, transform_options/1, get_pids/1, get_random_pid/1, transform_options/1,
opt_type/1]). reload/1, opt_type/1]).
-include("logger.hrl"). -include("logger.hrl").
-include_lib("stdlib/include/ms_transform.hrl").
-define(PGSQL_PORT, 5432). -define(PGSQL_PORT, 5432).
-define(MYSQL_PORT, 3306). -define(MYSQL_PORT, 3306).
-define(DEFAULT_POOL_SIZE, 10). -define(DEFAULT_POOL_SIZE, 10).
-define(DEFAULT_SQL_START_INTERVAL, 30). -define(DEFAULT_SQL_START_INTERVAL, 30).
-define(CONNECT_TIMEOUT, 500). -define(CONNECT_TIMEOUT, 500).
-record(sql_pool, {host, pid}). -record(sql_pool, {host :: binary(),
pid :: pid()}).
start_link(Host) -> start_link(Host) ->
ejabberd_mnesia:create(?MODULE, sql_pool, ejabberd_mnesia:create(?MODULE, sql_pool,
@ -59,9 +57,6 @@ start_link(Host) ->
?MODULE, [Host]). ?MODULE, [Host]).
init([Host]) -> init([Host]) ->
StartInterval = ejabberd_config:get_option(
{sql_start_interval, Host},
?DEFAULT_SQL_START_INTERVAL),
Type = ejabberd_config:get_option({sql_type, Host}, odbc), Type = ejabberd_config:get_option({sql_type, Host}, odbc),
PoolSize = get_pool_size(Type, Host), PoolSize = get_pool_size(Type, Host),
case Type of case Type of
@ -72,16 +67,37 @@ init([Host]) ->
_ -> _ ->
ok ok
end, end,
{ok, {{one_for_one, PoolSize * 10, 1},
[child_spec(I, Host) || I <- lists:seq(1, PoolSize)]}}.
{ok, reload(Host) ->
{{one_for_one, PoolSize * 10, 1}, Type = ejabberd_config:get_option({sql_type, Host}, odbc),
lists:map(fun (I) -> NewPoolSize = get_pool_size(Type, Host),
{I, OldPoolSize = ets:select_count(
{ejabberd_sql, start_link, sql_pool,
[Host, StartInterval * 1000]}, ets:fun2ms(
transient, 2000, worker, [?MODULE]} fun(#sql_pool{host = H}) when H == Host ->
end, true
lists:seq(1, PoolSize))}}. end)),
reload(Host, NewPoolSize, OldPoolSize).
reload(Host, NewPoolSize, OldPoolSize) ->
Sup = gen_mod:get_module_proc(Host, ?MODULE),
if NewPoolSize == OldPoolSize ->
ok;
NewPoolSize > OldPoolSize ->
lists:foreach(
fun(I) ->
Spec = child_spec(I, Host),
supervisor:start_child(Sup, Spec)
end, lists:seq(OldPoolSize+1, NewPoolSize));
OldPoolSize > NewPoolSize ->
lists:foreach(
fun(I) ->
supervisor:terminate_child(Sup, I),
supervisor:delete_child(Sup, I)
end, lists:seq(NewPoolSize+1, OldPoolSize))
end.
get_pids(Host) -> get_pids(Host) ->
Rs = mnesia:dirty_read(sql_pool, Host), Rs = mnesia:dirty_read(sql_pool, Host),
@ -123,6 +139,13 @@ get_pool_size(SQLType, Host) ->
end, end,
PoolSize. PoolSize.
child_spec(I, Host) ->
StartInterval = ejabberd_config:get_option(
{sql_start_interval, Host},
?DEFAULT_SQL_START_INTERVAL),
{I, {ejabberd_sql, start_link, [Host, timer:seconds(StartInterval)]},
transient, 2000, worker, [?MODULE]}.
transform_options(Opts) -> transform_options(Opts) ->
lists:foldl(fun transform_options/2, [], Opts). lists:foldl(fun transform_options/2, [], Opts).