diff options
Diffstat (limited to 'src/ejabberd_sql_sup.erl')
-rw-r--r-- | src/ejabberd_sql_sup.erl | 99 |
1 files changed, 35 insertions, 64 deletions
diff --git a/src/ejabberd_sql_sup.erl b/src/ejabberd_sql_sup.erl index fc16b784b..6d1e63b48 100644 --- a/src/ejabberd_sql_sup.erl +++ b/src/ejabberd_sql_sup.erl @@ -27,22 +27,11 @@ -author('alexey@process-one.net'). --export([start_link/1, init/1, add_pid/2, remove_pid/2, - get_pids/1, get_random_pid/1, reload/1]). +-export([start_link/1, init/1, reload/1, is_started/1]). -include("logger.hrl"). --include_lib("stdlib/include/ms_transform.hrl"). - --record(sql_pool, {host :: binary(), - pid :: pid()}). start_link(Host) -> - ejabberd_mnesia:create(?MODULE, sql_pool, - [{ram_copies, [node()]}, {type, bag}, - {local_content, true}, - {attributes, record_info(fields, sql_pool)}]), - F = fun () -> mnesia:delete({sql_pool, Host}) end, - mnesia:ets(F), supervisor:start_link({local, gen_mod:get_module_proc(Host, ?MODULE)}, ?MODULE, [Host]). @@ -58,61 +47,35 @@ init([Host]) -> _ -> ok end, - {ok, {{one_for_one, PoolSize * 10, 1}, - [child_spec(I, Host) || I <- lists:seq(1, PoolSize)]}}. + {ok, {{one_for_one, PoolSize * 10, 1}, child_specs(Host, PoolSize)}}. +-spec reload(binary()) -> ok. reload(Host) -> - Type = ejabberd_option:sql_type(Host), - NewPoolSize = get_pool_size(Type, Host), - OldPoolSize = ets:select_count( - sql_pool, - ets:fun2ms( - fun(#sql_pool{host = H}) when H == Host -> - true - end)), - reload(Host, NewPoolSize, OldPoolSize). - -reload(Host, NewPoolSize, OldPoolSize) -> - Sup = gen_mod:get_module_proc(Host, ?MODULE), - if NewPoolSize == OldPoolSize -> - ok; - NewPoolSize > OldPoolSize -> + case is_started(Host) of + true -> + Sup = gen_mod:get_module_proc(Host, ?MODULE), + Type = ejabberd_option:sql_type(Host), + PoolSize = get_pool_size(Type, Host), lists:foreach( - fun(I) -> - Spec = child_spec(I, Host), + fun(Spec) -> supervisor:start_child(Sup, Spec) - end, lists:seq(OldPoolSize+1, NewPoolSize)); - OldPoolSize > NewPoolSize -> + end, child_specs(Host, PoolSize)), lists:foreach( - fun(I) -> - supervisor:terminate_child(Sup, I), - supervisor:delete_child(Sup, I) - end, lists:seq(NewPoolSize+1, OldPoolSize)) - end. - -get_pids(Host) -> - Rs = mnesia:dirty_read(sql_pool, Host), - [R#sql_pool.pid || R <- Rs, is_process_alive(R#sql_pool.pid)]. - -get_random_pid(Host) -> - case get_pids(Host) of - [] -> none; - Pids -> - I = p1_rand:round_robin(length(Pids)) + 1, - lists:nth(I, Pids) + fun({Id, _, _, _}) when Id > PoolSize -> + case supervisor:terminate_child(Sup, Id) of + ok -> supervisor:delete_child(Sup, Id); + _ -> ok + end; + (_) -> + ok + end, supervisor:which_children(Sup)); + false -> + ok end. -add_pid(Host, Pid) -> - F = fun () -> - mnesia:write(#sql_pool{host = Host, pid = Pid}) - end, - mnesia:ets(F). - -remove_pid(Host, Pid) -> - F = fun () -> - mnesia:delete_object(#sql_pool{host = Host, pid = Pid}) - end, - mnesia:ets(F). +-spec is_started(binary()) -> boolean(). +is_started(Host) -> + whereis(gen_mod:get_module_proc(Host, ?MODULE)) /= undefined. -spec get_pool_size(atom(), binary()) -> pos_integer(). get_pool_size(SQLType, Host) -> @@ -125,10 +88,18 @@ get_pool_size(SQLType, Host) -> end, PoolSize. -child_spec(I, Host) -> - StartInterval = ejabberd_option:sql_start_interval(Host), - {I, {ejabberd_sql, start_link, [Host, StartInterval]}, - transient, 2000, worker, [?MODULE]}. +-spec child_spec(binary(), pos_integer()) -> supervisor:child_spec(). +child_spec(Host, I) -> + #{id => I, + start => {ejabberd_sql, start_link, [Host, I]}, + restart => transient, + shutdown => 2000, + type => worker, + modules => [?MODULE]}. + +-spec child_specs(binary(), pos_integer()) -> [supervisor:child_spec()]. +child_specs(Host, PoolSize) -> + [child_spec(Host, I) || I <- lists:seq(1, PoolSize)]. check_sqlite_db(Host) -> DB = ejabberd_sql:sqlite_db(Host), |