aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_riak_sup.erl
blob: a01f3538a52b0419ec42da4f3d37f5b60e559367 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
%%%----------------------------------------------------------------------
%%% 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>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2017   ProcessOne
%%%
%%% 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.
%%%
%%% 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.
%%%
%%%----------------------------------------------------------------------

-module(ejabberd_riak_sup).

-behaviour(supervisor).
-behaviour(ejabberd_config).
-author('alexey@process-one.net').

-export([start_link/0, init/1, get_pids/0,
	 transform_options/1, get_random_pid/0,
	 host_up/1, config_reloaded/0, opt_type/1]).

-include("ejabberd.hrl").
-include("logger.hrl").

-define(DEFAULT_POOL_SIZE, 10).
-define(DEFAULT_RIAK_START_INTERVAL, 30). % 30 seconds
-define(DEFAULT_RIAK_HOST, "127.0.0.1").
-define(DEFAULT_RIAK_PORT, 8087).

% time to wait for the supervisor to start its child before returning
% a timeout error to the request
-define(CONNECT_TIMEOUT, 500). % milliseconds

host_up(Host) ->
    case is_riak_configured(Host) of
	true ->
	    ejabberd:start_app(riakc),
	    lists:foreach(
	      fun(Spec) ->
		      supervisor:start_child(?MODULE, Spec)
	      end, get_specs());
	false ->
	    ok
    end.

config_reloaded() ->
    case is_riak_configured() of
	true ->
	    ejabberd:start_app(riakc),
	    lists:foreach(
	      fun(Spec) ->
		      supervisor:start_child(?MODULE, Spec)
	      end, get_specs());
	false ->
	    lists:foreach(
	      fun({Id, _, _, _}) ->
		      supervisor:terminate_child(?MODULE, Id),
		      supervisor:delete_child(?MODULE, Id)
	      end, supervisor:which_children(?MODULE))
    end.

is_riak_configured() ->
    lists:any(fun is_riak_configured/1, ?MYHOSTS).

is_riak_configured(Host) ->
    ServerConfigured = ejabberd_config:has_option({riak_server, Host}),
    PortConfigured = ejabberd_config:has_option({riak_port, Host}),
    StartIntervalConfigured = ejabberd_config:has_option({riak_start_interval, Host}),
    PoolConfigured = ejabberd_config:has_option({riak_pool_size, Host}),
    CacertConfigured = ejabberd_config:has_option({riak_cacertfile, Host}),
    UserConfigured = ejabberd_config:has_option({riak_username, Host}),
    PassConfigured = ejabberd_config:has_option({riak_password, Host}),
    AuthConfigured = lists:member(
		       ejabberd_auth_riak,
		       ejabberd_auth:auth_modules(Host)),
    SMConfigured = ejabberd_config:get_option(
		     {sm_db_type, Host},
		     ejabberd_sm:opt_type(sm_db_type)) == riak,
    RouterConfigured = ejabberd_config:get_option(
			 {router_db_type, Host},
			 ejabberd_router:opt_type(router_db_type)) == riak,
    Modules = ejabberd_config:get_option(
		{modules, Host},
		fun(L) when is_list(L) -> L end, []),
    ModuleWithRiakDBConfigured = lists:any(
				   fun({Module, Opts}) ->
					   gen_mod:db_type(Host, Opts, Module) == riak
				   end, Modules),
    ServerConfigured or PortConfigured or StartIntervalConfigured
	or PoolConfigured or CacertConfigured
	or UserConfigured or PassConfigured
	or SMConfigured or RouterConfigured
	or AuthConfigured or ModuleWithRiakDBConfigured.

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 20),
    ejabberd_hooks:add(host_up, ?MODULE, host_up, 20),
    Specs = case is_riak_configured() of
		true ->
		    ejabberd:start_app(riakc),
		    get_specs();
		false ->
		    []
	    end,
    {ok, {{one_for_one, 500, 1}, Specs}}.

-spec get_specs() -> [supervisor:child_spec()].
get_specs() ->
    PoolSize = get_pool_size(),
    StartInterval = get_start_interval(),
    Server = get_riak_server(),
    Port = get_riak_port(),
    CACertFile = get_riak_cacertfile(),
    Username = get_riak_username(),
    Password = get_riak_password(),
    Options = lists:filter(
		fun(X) -> X /= nil end,
		[auto_reconnect,
		 {keepalive, true},
		 if CACertFile /= nil -> {cacertfile ,CACertFile};
		    true -> nil
		 end,
		 if (Username /= nil) and (Password /= nil) ->
			 {credentials, Username, Password};
		    true -> nil
		 end]),
    lists:map(
      fun(I) ->
	      {ejabberd_riak:get_proc(I),
	       {ejabberd_riak, start_link,
		[I, Server, Port, StartInterval*1000, Options]},
	       transient, 2000, worker, [?MODULE]}
      end, lists:seq(1, PoolSize)).

get_start_interval() ->
    ejabberd_config:get_option(
      riak_start_interval,
      fun(N) when is_integer(N), N >= 1 -> N end,
      ?DEFAULT_RIAK_START_INTERVAL).

get_pool_size() ->
    ejabberd_config:get_option(
      riak_pool_size,
      fun(N) when is_integer(N), N >= 1 -> N end,
      ?DEFAULT_POOL_SIZE).

get_riak_server() ->
    ejabberd_config:get_option(
      riak_server,
      fun(S) ->
	      binary_to_list(iolist_to_binary(S))
      end, ?DEFAULT_RIAK_HOST).

get_riak_cacertfile() ->
    ejabberd_config:get_option(
      riak_cacertfile,
      fun(S) ->
	      binary_to_list(iolist_to_binary(S))
      end, nil).

get_riak_username() ->
    ejabberd_config:get_option(
      riak_username,
      fun(S) ->
	      binary_to_list(iolist_to_binary(S))
      end, nil).

get_riak_password() ->
    ejabberd_config:get_option(
      riak_password,
      fun(S) ->
	      binary_to_list(iolist_to_binary(S))
      end, nil).

get_riak_port() ->
    ejabberd_config:get_option(
      riak_port,
      fun(P) when is_integer(P), P > 0, P < 65536 -> P end,
      ?DEFAULT_RIAK_PORT).

get_pids() ->
    [ejabberd_riak:get_proc(I) || I <- lists:seq(1, get_pool_size())].

get_random_pid() ->
    I = randoms:round_robin(get_pool_size()) + 1,
    ejabberd_riak:get_proc(I).

transform_options(Opts) ->
    lists:foldl(fun transform_options/2, [], Opts).

transform_options({riak_server, {S, P}}, Opts) ->
    [{riak_server, S}, {riak_port, P}|Opts];
transform_options(Opt, Opts) ->
    [Opt|Opts].

opt_type(modules) -> fun (L) when is_list(L) -> L end;
opt_type(riak_pool_size) ->
    fun (N) when is_integer(N), N >= 1 -> N end;
opt_type(riak_port) -> fun (_) -> true end;
opt_type(riak_server) -> fun (_) -> true end;
opt_type(riak_start_interval) ->
    fun (N) when is_integer(N), N >= 1 -> N end;
opt_type(riak_cacertfile) -> fun iolist_to_binary/1;
opt_type(riak_username) -> fun iolist_to_binary/1;
opt_type(riak_password) -> fun iolist_to_binary/1;
opt_type(_) ->
    [modules, riak_pool_size, riak_port, riak_server,
     riak_start_interval, riak_cacertfile, riak_username, riak_password].