2012-01-25 11:02:16 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_riak_sup.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Riak connections supervisor
|
|
|
|
%%% Created : 29 Dec 2011 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2012-01-25 11:02:16 +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.
|
|
|
|
%%%
|
2015-02-25 15:19:33 +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.
|
2012-01-25 11:02:16 +01:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_riak_sup).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-behaviour(supervisor).
|
2012-01-25 11:02:16 +01:00
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([start/0, start_link/0, init/1, get_pids/0,
|
|
|
|
get_random_pid/0, config_reloaded/0]).
|
2012-01-25 11:02:16 +01:00
|
|
|
|
2014-07-09 14:40:18 +02:00
|
|
|
-include("logger.hrl").
|
2012-01-25 11:02:16 +01:00
|
|
|
|
|
|
|
% time to wait for the supervisor to start its child before returning
|
|
|
|
% a timeout error to the request
|
|
|
|
-define(CONNECT_TIMEOUT, 500). % milliseconds
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
start() ->
|
|
|
|
case is_started() of
|
|
|
|
true -> ok;
|
2017-02-23 08:12:19 +01:00
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd:start_app(riakc),
|
|
|
|
Spec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
permanent, infinity, supervisor, [?MODULE]},
|
|
|
|
case supervisor:start_child(ejabberd_db_sup, Spec) of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
{error, {already_started, _}} -> ok;
|
|
|
|
{error, Why} = Err ->
|
|
|
|
?ERROR_MSG("Failed to start ~s: ~p",
|
|
|
|
[?MODULE, Why]),
|
|
|
|
Err
|
|
|
|
end
|
2017-02-23 08:12:19 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
config_reloaded() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case is_started() of
|
2014-07-09 14:40:18 +02:00
|
|
|
true ->
|
2017-02-24 10:05:47 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(Spec) ->
|
|
|
|
supervisor:start_child(?MODULE, Spec)
|
2019-06-14 11:33:26 +02:00
|
|
|
end, get_specs()),
|
|
|
|
PoolSize = get_pool_size(),
|
2017-02-24 10:05:47 +01:00
|
|
|
lists:foreach(
|
2019-06-14 11:33:26 +02:00
|
|
|
fun({Id, _, _, _}) when Id > PoolSize ->
|
|
|
|
case supervisor:terminate_child(?MODULE, Id) of
|
|
|
|
ok -> supervisor:delete_child(?MODULE, Id);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, supervisor:which_children(?MODULE));
|
|
|
|
false ->
|
|
|
|
ok
|
2012-11-06 16:58:08 +01:00
|
|
|
end.
|
|
|
|
|
2012-01-25 11:02:16 +01:00
|
|
|
start_link() ->
|
|
|
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
|
|
|
|
|
|
|
init([]) ->
|
2017-02-24 10:05:47 +01:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 20),
|
2019-06-14 11:33:26 +02:00
|
|
|
{ok, {{one_for_one, 500, 1}, get_specs()}}.
|
|
|
|
|
|
|
|
is_started() ->
|
|
|
|
whereis(?MODULE) /= undefined.
|
2017-02-24 10:05:47 +01:00
|
|
|
|
|
|
|
-spec get_specs() -> [supervisor:child_spec()].
|
|
|
|
get_specs() ->
|
2014-07-09 14:40:18 +02:00
|
|
|
PoolSize = get_pool_size(),
|
|
|
|
StartInterval = get_start_interval(),
|
|
|
|
Server = get_riak_server(),
|
|
|
|
Port = get_riak_port(),
|
2016-03-18 17:41:14 +01:00
|
|
|
CACertFile = get_riak_cacertfile(),
|
|
|
|
Username = get_riak_username(),
|
|
|
|
Password = get_riak_password(),
|
|
|
|
Options = lists:filter(
|
|
|
|
fun(X) -> X /= nil end,
|
|
|
|
[auto_reconnect,
|
2016-03-22 17:32:30 +01:00
|
|
|
{keepalive, true},
|
2016-03-18 17:41:14 +01:00
|
|
|
if CACertFile /= nil -> {cacertfile ,CACertFile};
|
|
|
|
true -> nil
|
|
|
|
end,
|
|
|
|
if (Username /= nil) and (Password /= nil) ->
|
|
|
|
{credentials, Username, Password};
|
|
|
|
true -> nil
|
2017-02-24 10:05:47 +01:00
|
|
|
end]),
|
|
|
|
lists:map(
|
|
|
|
fun(I) ->
|
|
|
|
{ejabberd_riak:get_proc(I),
|
|
|
|
{ejabberd_riak, start_link,
|
2019-06-14 11:33:26 +02:00
|
|
|
[I, Server, Port, StartInterval, Options]},
|
2017-02-24 10:05:47 +01:00
|
|
|
transient, 2000, worker, [?MODULE]}
|
|
|
|
end, lists:seq(1, PoolSize)).
|
2012-01-25 11:02:16 +01:00
|
|
|
|
2014-07-09 14:40:18 +02:00
|
|
|
get_start_interval() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_start_interval().
|
2014-07-09 14:40:18 +02:00
|
|
|
|
|
|
|
get_pool_size() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_pool_size().
|
2014-07-09 14:40:18 +02:00
|
|
|
|
|
|
|
get_riak_server() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_server().
|
2014-07-09 14:40:18 +02:00
|
|
|
|
2016-03-18 17:41:14 +01:00
|
|
|
get_riak_cacertfile() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_cacertfile().
|
2016-03-18 17:41:14 +01:00
|
|
|
|
|
|
|
get_riak_username() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_username().
|
2016-03-18 17:41:14 +01:00
|
|
|
|
|
|
|
get_riak_password() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_password().
|
2016-03-18 17:41:14 +01:00
|
|
|
|
2014-07-09 14:40:18 +02:00
|
|
|
get_riak_port() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:riak_port().
|
2014-07-09 14:40:18 +02:00
|
|
|
|
2012-01-25 11:02:16 +01:00
|
|
|
get_pids() ->
|
2014-07-09 14:40:18 +02:00
|
|
|
[ejabberd_riak:get_proc(I) || I <- lists:seq(1, get_pool_size())].
|
2012-01-25 11:02:16 +01:00
|
|
|
|
|
|
|
get_random_pid() ->
|
2018-07-05 10:51:49 +02:00
|
|
|
I = p1_rand:round_robin(get_pool_size()) + 1,
|
2014-07-09 14:40:18 +02:00
|
|
|
ejabberd_riak:get_proc(I).
|