2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% File : ejabberd_sql_sup.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% Purpose : SQL connections supervisor
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 22 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2008-10-06 16:18:46 +02:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2004-12-30 00:10:14 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-module(ejabberd_sql_sup).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2019-08-12 11:21:31 +02:00
|
|
|
-export([start/1, stop/1, stop/0]).
|
|
|
|
-export([start_link/0, start_link/1]).
|
|
|
|
-export([init/1, reload/1, config_reloaded/0, is_started/1]).
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2010-07-02 12:31:42 +02:00
|
|
|
|
2019-08-12 11:21:31 +02:00
|
|
|
start(Host) ->
|
|
|
|
case is_started(Host) of
|
|
|
|
true -> ok;
|
|
|
|
false ->
|
|
|
|
App = case ejabberd_option:sql_type(Host) of
|
|
|
|
mysql -> p1_mysql;
|
|
|
|
pgsql -> p1_pgsql;
|
|
|
|
sqlite -> sqlite3;
|
|
|
|
_ -> odbc
|
|
|
|
end,
|
|
|
|
ejabberd:start_app(App),
|
|
|
|
Spec = #{id => gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
start => {ejabberd_sql_sup, start_link, [Host]},
|
|
|
|
restart => transient,
|
|
|
|
shutdown => infinity,
|
|
|
|
type => supervisor,
|
|
|
|
modules => [?MODULE]},
|
|
|
|
case supervisor:start_child(ejabberd_db_sup, Spec) of
|
|
|
|
{ok, _} -> ok;
|
2019-10-29 11:36:40 +01:00
|
|
|
{error, {already_started, Pid}} ->
|
|
|
|
%% Wait for the supervisor to fully start
|
|
|
|
_ = supervisor:count_children(Pid),
|
|
|
|
ok;
|
2019-08-12 11:21:31 +02:00
|
|
|
{error, Why} = Err ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to start ~ts: ~p", [?MODULE, Why]),
|
2019-08-12 11:21:31 +02:00
|
|
|
Err
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
case supervisor:terminate_child(ejabberd_db_sup, Proc) of
|
|
|
|
ok -> supervisor:delete_child(ejabberd_db_sup, Proc);
|
|
|
|
Err -> Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
start_link(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
supervisor:start_link({local,
|
|
|
|
gen_mod:get_module_proc(Host, ?MODULE)},
|
2005-07-13 05:24:13 +02:00
|
|
|
?MODULE, [Host]).
|
2004-12-30 00:10:14 +01:00
|
|
|
|
2019-08-12 11:21:31 +02:00
|
|
|
stop() ->
|
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, start, 20),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, stop, 90),
|
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 20).
|
|
|
|
|
|
|
|
init([]) ->
|
|
|
|
file:delete(ejabberd_sql:odbcinst_config()),
|
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, start, 20),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, stop, 90),
|
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 20),
|
|
|
|
ignore;
|
2005-07-13 05:24:13 +02:00
|
|
|
init([Host]) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Type = ejabberd_option:sql_type(Host),
|
2016-11-21 19:34:56 +01:00
|
|
|
PoolSize = get_pool_size(Type, Host),
|
2015-03-16 19:53:19 +01:00
|
|
|
case Type of
|
|
|
|
sqlite ->
|
2015-04-08 14:31:28 +02:00
|
|
|
check_sqlite_db(Host);
|
2015-09-13 16:41:54 +02:00
|
|
|
mssql ->
|
2020-10-08 16:23:34 +02:00
|
|
|
ejabberd_sql:init_mssql(Host);
|
2015-03-16 19:53:19 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2019-07-24 13:28:43 +02:00
|
|
|
{ok, {{one_for_one, PoolSize * 10, 1}, child_specs(Host, PoolSize)}}.
|
2015-03-16 19:53:19 +01:00
|
|
|
|
2019-08-12 11:21:31 +02:00
|
|
|
-spec config_reloaded() -> ok.
|
|
|
|
config_reloaded() ->
|
|
|
|
lists:foreach(fun reload/1, ejabberd_option:hosts()).
|
|
|
|
|
2019-07-24 13:28:43 +02:00
|
|
|
-spec reload(binary()) -> ok.
|
2018-09-19 10:55:40 +02:00
|
|
|
reload(Host) ->
|
2019-07-24 13:28:43 +02:00
|
|
|
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),
|
2018-09-19 10:55:40 +02:00
|
|
|
lists:foreach(
|
2019-07-24 13:28:43 +02:00
|
|
|
fun(Spec) ->
|
2018-09-19 10:55:40 +02:00
|
|
|
supervisor:start_child(Sup, Spec)
|
2019-07-24 13:28:43 +02:00
|
|
|
end, child_specs(Host, PoolSize)),
|
2018-09-19 10:55:40 +02:00
|
|
|
lists:foreach(
|
2019-07-24 13:28:43 +02:00
|
|
|
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
|
2014-04-24 08:34:41 +02:00
|
|
|
end.
|
2010-07-02 12:31:42 +02:00
|
|
|
|
2019-07-24 13:28:43 +02:00
|
|
|
-spec is_started(binary()) -> boolean().
|
|
|
|
is_started(Host) ->
|
|
|
|
whereis(gen_mod:get_module_proc(Host, ?MODULE)) /= undefined.
|
2013-08-12 14:25:05 +02:00
|
|
|
|
2016-11-21 19:34:56 +01:00
|
|
|
-spec get_pool_size(atom(), binary()) -> pos_integer().
|
|
|
|
get_pool_size(SQLType, Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
PoolSize = ejabberd_option:sql_pool_size(Host),
|
2016-11-21 19:34:56 +01:00
|
|
|
if PoolSize > 1 andalso SQLType == sqlite ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("It's not recommended to set sql_pool_size > 1 for "
|
2016-11-21 19:34:56 +01:00
|
|
|
"sqlite, because it may cause race conditions", []);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
PoolSize.
|
|
|
|
|
2019-07-24 13:28:43 +02:00
|
|
|
-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)].
|
2018-09-19 10:55:40 +02:00
|
|
|
|
2015-04-08 14:31:28 +02:00
|
|
|
check_sqlite_db(Host) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
DB = ejabberd_sql:sqlite_db(Host),
|
|
|
|
File = ejabberd_sql:sqlite_file(Host),
|
2015-04-08 14:31:28 +02:00
|
|
|
Ret = case filelib:ensure_dir(File) of
|
|
|
|
ok ->
|
|
|
|
case sqlite3:open(DB, [{file, File}]) of
|
|
|
|
{ok, _Ref} -> ok;
|
|
|
|
{error, {already_started, _Ref}} -> ok;
|
|
|
|
{error, R} -> {error, R}
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end,
|
2015-03-16 19:53:19 +01:00
|
|
|
case Ret of
|
|
|
|
ok ->
|
2015-04-08 14:31:28 +02:00
|
|
|
sqlite3:sql_exec(DB, "pragma foreign_keys = on"),
|
|
|
|
case sqlite3:list_tables(DB) of
|
2015-03-16 19:53:19 +01:00
|
|
|
[] ->
|
2015-04-08 14:31:28 +02:00
|
|
|
create_sqlite_tables(DB),
|
|
|
|
sqlite3:close(DB),
|
2015-03-16 19:53:19 +01:00
|
|
|
ok;
|
|
|
|
[_H | _] ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
{error, Reason} ->
|
2018-09-19 22:12:14 +02:00
|
|
|
?WARNING_MSG("Failed open sqlite database, reason ~p", [Reason])
|
2015-03-16 19:53:19 +01:00
|
|
|
end.
|
|
|
|
|
2015-04-08 14:31:28 +02:00
|
|
|
create_sqlite_tables(DB) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
SqlDir = misc:sql_dir(),
|
2015-03-16 19:53:19 +01:00
|
|
|
File = filename:join(SqlDir, "lite.sql"),
|
|
|
|
case file:open(File, [read, binary]) of
|
|
|
|
{ok, Fd} ->
|
|
|
|
Qs = read_lines(Fd, File, []),
|
2015-04-08 14:31:28 +02:00
|
|
|
ok = sqlite3:sql_exec(DB, "begin"),
|
|
|
|
[ok = sqlite3:sql_exec(DB, Q) || Q <- Qs],
|
|
|
|
ok = sqlite3:sql_exec(DB, "commit");
|
2015-03-16 19:53:19 +01:00
|
|
|
{error, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to read SQLite schema file: ~ts",
|
2018-09-19 22:12:14 +02:00
|
|
|
[file:format_error(Reason)])
|
2015-03-16 19:53:19 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
read_lines(Fd, File, Acc) ->
|
|
|
|
case file:read_line(Fd) of
|
|
|
|
{ok, Line} ->
|
|
|
|
NewAcc = case str:strip(str:strip(Line, both, $\r), both, $\n) of
|
|
|
|
<<"--", _/binary>> ->
|
|
|
|
Acc;
|
|
|
|
<<>> ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
[Line|Acc]
|
|
|
|
end,
|
|
|
|
read_lines(Fd, File, NewAcc);
|
|
|
|
eof ->
|
|
|
|
QueryList = str:tokens(list_to_binary(lists:reverse(Acc)), <<";">>),
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Query) ->
|
|
|
|
case str:strip(str:strip(Query, both, $\r), both, $\n) of
|
|
|
|
<<>> ->
|
|
|
|
[];
|
|
|
|
Q ->
|
|
|
|
[<<Q/binary, $;>>]
|
|
|
|
end
|
|
|
|
end, QueryList);
|
|
|
|
{error, _} = Err ->
|
|
|
|
?ERROR_MSG("Failed read from lite.sql, reason: ~p", [Err]),
|
|
|
|
[]
|
|
|
|
end.
|