./ct_report/coverage/mongoose_wpool.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @doc
3 %%% This is here because there are pool options which have to be given when calling
4 %%% the pool (selection strategy, timeout), while we want to set it once for the pool and not
5 %%% worry about them later, hence additional storage.
6 %%% @end
7 %%%-------------------------------------------------------------------
8 -module(mongoose_wpool).
9 -author("bartlomiej.gorny@erlang-solutions.com").
10 -include("mongoose.hrl").
11
12 -record(mongoose_wpool, {
13 name :: pool_name(),
14 atom_name :: wpool:name(),
15 strategy :: wpool:strategy(),
16 call_timeout :: pos_integer()
17 }).
18
19 %% API
20 -export([ensure_started/0,
21 start/2, start/3, start/4, start/5,
22 stop/0, stop/1, stop/2, stop/3,
23 get_worker/2, get_worker/3,
24 call/2, call/3, call/4, call/5,
25 send_request/2, send_request/3, send_request/4, send_request/5,
26 cast/2, cast/3, cast/4, cast/5,
27 get_pool_settings/3, get_pools/0, stats/3]).
28
29 -export([start_sup_pool/3]).
30 -export([start_configured_pools/0, start_configured_pools/1,
31 start_configured_pools/2, start_configured_pools/3]).
32 -export([is_configured/1]).
33 -export([make_pool_name/3]).
34 -export([call_start_callback/2]).
35
36 %% Mostly for tests
37 -export([expand_pools/3]).
38
39 -ignore_xref([call/2, cast/2, cast/3, expand_pools/3, get_worker/2,
40 send_request/2, send_request/3, send_request/4, send_request/5,
41 is_configured/2, is_configured/1, is_configured/1, start/2, start/3,
42 start/5, start_configured_pools/1, start_configured_pools/2, start_configured_pools/3,
43 stats/3, stop/1, stop/2]).
44
45 -type pool_type() :: redis | http | rdbms | cassandra | elastic | generic | rabbit | ldap.
46
47 %% Config scope
48 -type scope() :: global | host_type | mongooseim:host_type().
49 -type host_type_or_global() :: mongooseim:host_type_or_global().
50 -type tag() :: atom().
51
52 %% Name of a process
53 -type proc_name() :: atom().
54
55 %% ID of a pool. Used as a key for an ETS table
56 -type pool_name() :: {PoolType :: pool_type(),
57 HostType :: host_type_or_global(),
58 Tag :: tag()}.
59
60 -type pool_opts_in() :: map().
61 -type pool_opts() :: [wpool:option()].
62 -type conn_opts() :: map().
63
64 -type pool_map_in() :: #{type := pool_type(),
65 scope := scope(),
66 tag := tag(),
67 opts := pool_opts_in(),
68 conn_opts := conn_opts()}.
69 %% Pool map with expanded HostType argument instead of scope
70 -type pool_map() :: #{type := pool_type(),
71 host_type := host_type_or_global(),
72 tag := tag(),
73 opts := pool_opts(),
74 conn_opts := conn_opts()}.
75 -type pool_error() :: {pool_not_started, term()}.
76 -type worker_result() :: {ok, pid()} | {error, pool_error()}.
77 -type pool_record_result() :: {ok, #mongoose_wpool{}} | {error, pool_error()}.
78 -type start_result() :: {ok, pid()} | {error, term()}.
79 -type stop_result() :: ok | term().
80
81 -export_type([pool_type/0, tag/0, scope/0, proc_name/0, pool_name/0, pool_opts/0, conn_opts/0]).
82
83 -type callback_fun() :: init | start | is_supported_strategy | stop.
84
85 -callback init() -> ok | {error, term()}.
86 -callback start(host_type_or_global(), tag(), WPoolOpts :: pool_opts(), ConnOpts :: conn_opts()) ->
87 {ok, {pid(), proplists:proplist()}} | {ok, pid()} | {error, Reason :: term()}.
88 -callback is_supported_strategy(Strategy :: wpool:strategy()) -> boolean().
89 -callback stop(host_type_or_global(), tag()) -> ok.
90
91 -optional_callbacks([is_supported_strategy/1]).
92
93 ensure_started() ->
94 93 wpool:start(),
95 93 case whereis(mongoose_wpool_sup) of
96 undefined ->
97
:-(
mongoose_wpool_sup:start_link();
98 _ ->
99 93 ok
100 end,
101 93 ejabberd_sup:create_ets_table(
102 ?MODULE,
103 [named_table, public,
104 {read_concurrency, true},
105 {keypos, #mongoose_wpool.name},
106 {heir, whereis(mongoose_wpool_sup), undefined}]).
107
108 start_configured_pools() ->
109 93 Pools = mongoose_config:get_opt(outgoing_pools),
110 93 start_configured_pools(Pools).
111
112 start_configured_pools(PoolsIn) ->
113 96 start_configured_pools(PoolsIn, ?ALL_HOST_TYPES).
114
115 start_configured_pools(PoolsIn, HostTypes) ->
116 97 HostTypeSpecific = get_host_type_specific_pools(HostTypes),
117 97 start_configured_pools(PoolsIn, HostTypeSpecific, HostTypes).
118
119 start_configured_pools(PoolsIn, HostTypeSpecific, HostTypes) ->
120 97 Pools = expand_pools(PoolsIn, HostTypeSpecific, HostTypes),
121 97 [call_callback(init, PoolType, []) || PoolType <- get_unique_types(PoolsIn, HostTypeSpecific)],
122 97 [start(Pool) || Pool <- Pools].
123
124 -spec start(pool_map()) -> start_result().
125 start(#{type := PoolType, host_type := HostType, tag := Tag,
126 opts := PoolOpts, conn_opts := ConnOpts}) ->
127 190 start(PoolType, HostType, Tag, PoolOpts, ConnOpts).
128
129 -spec start(pool_type(), pool_opts()) -> start_result().
130 start(PoolType, PoolOpts) ->
131
:-(
start(PoolType, global, PoolOpts).
132
133 -spec start(pool_type(), host_type_or_global(), pool_opts()) -> start_result().
134 start(PoolType, HostType, PoolOpts) ->
135
:-(
start(PoolType, HostType, default, PoolOpts).
136
137 -spec start(pool_type(), host_type_or_global(), tag(),
138 pool_opts()) -> start_result().
139 start(PoolType, HostType, Tag, PoolOpts) ->
140
:-(
start(PoolType, HostType, Tag, PoolOpts, #{}).
141
142 -spec start(pool_type(), host_type_or_global(), tag(),
143 pool_opts(), conn_opts()) -> start_result().
144 start(PoolType, HostType, Tag, PoolOpts, ConnOpts) ->
145 190 {Opts0, WpoolOptsIn} = proplists:split(PoolOpts, [strategy, call_timeout]),
146 190 Opts = lists:append(Opts0),
147 190 Strategy = proplists:get_value(strategy, Opts, best_worker),
148 190 CallTimeout = proplists:get_value(call_timeout, Opts, 5000),
149 %% If a callback doesn't explicitly blacklist a strategy, let's proceed.
150 190 CallbackModule = make_callback_module_name(PoolType),
151 190 case catch CallbackModule:is_supported_strategy(Strategy) of
152 false ->
153
:-(
error({strategy_not_supported, PoolType, HostType, Tag, Strategy});
154 _ ->
155 190 start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts, Strategy, CallTimeout)
156 end.
157
158 -spec start(pool_type(), host_type_or_global(), tag(),
159 pool_opts(), conn_opts(), wpool:strategy(), pos_integer()) ->
160 start_result().
161 start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts, Strategy, CallTimeout) ->
162 190 case mongoose_wpool_mgr:start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts) of
163 {ok, Pid} ->
164 190 ets:insert(?MODULE, #mongoose_wpool{name = {PoolType, HostType, Tag},
165 atom_name = make_pool_name(PoolType, HostType, Tag),
166 strategy = Strategy,
167 call_timeout = CallTimeout}),
168 190 {ok, Pid};
169 Error ->
170
:-(
Error
171 end.
172
173 %% @doc this function starts the worker_pool's pool under a specific supervisor
174 %% in MongooseIM application.
175 %% It's needed for 2 reasons:
176 %% 1. We want to have a full control of all the pools and its restarts
177 %% 2. When a pool is started via wpool:start_pool it's supposed be called by a supervisor,
178 %% if not, there is no way to stop the pool.
179 -spec start_sup_pool(pool_type(), proc_name(), [wpool:option()]) ->
180 {ok, pid()} | {error, term()}.
181 start_sup_pool(PoolType, ProcName, WpoolOpts) ->
182 190 SupName = mongoose_wpool_type_sup:name(PoolType),
183 190 ChildSpec = #{id => ProcName,
184 start => {wpool, start_pool, [ProcName, WpoolOpts]},
185 restart => temporary,
186 type => supervisor,
187 modules => [wpool]},
188 190 supervisor:start_child(SupName, ChildSpec).
189
190 -spec stop() -> term().
191 stop() ->
192 93 [stop_pool(PoolName) || PoolName <- get_pools()].
193
194 -spec stop_pool(pool_name()) -> stop_result().
195 stop_pool({PoolType, HostType, Tag}) ->
196 183 stop(PoolType, HostType, Tag).
197
198 -spec stop(pool_type()) -> stop_result().
199 stop(PoolType) ->
200
:-(
stop(PoolType, global).
201
202 -spec stop(pool_type(), host_type_or_global()) -> stop_result().
203 stop(PoolType, HostType) ->
204
:-(
stop(PoolType, HostType, default).
205
206 -spec stop(pool_type(), host_type_or_global(), tag()) -> stop_result().
207 stop(PoolType, HostType, Tag) ->
208 186 try
209 186 ets:delete(?MODULE, {PoolType, HostType, Tag}),
210 186 call_callback(stop, PoolType, [HostType, Tag]),
211 186 mongoose_wpool_mgr:stop(PoolType, HostType, Tag)
212 catch
213 C:R:S ->
214
:-(
?LOG_ERROR(#{what => pool_stop_failed,
215 pool_type => PoolType, server => HostType, pool_tag => Tag,
216 pool_key => {PoolType, HostType, Tag},
217
:-(
class => C, reason => R, stacktrace => S})
218 end.
219
220 -spec is_configured(pool_type()) -> boolean().
221 is_configured(PoolType) ->
222 258 Pools = mongoose_config:get_opt(outgoing_pools),
223 258 lists:any(fun(#{type := Type}) -> Type =:= PoolType end, Pools).
224
225 -spec get_worker(pool_type(), host_type_or_global()) -> worker_result().
226 get_worker(PoolType, HostType) ->
227 33 get_worker(PoolType, HostType, default).
228
229 -spec get_worker(pool_type(), host_type_or_global(), tag()) -> worker_result().
230 get_worker(PoolType, HostType, Tag) ->
231 33 case get_pool(PoolType, HostType, Tag) of
232 {ok, #mongoose_wpool{strategy = Strategy} = Pool} ->
233 33 Worker = wpool_pool:Strategy(make_pool_name(Pool)),
234 33 {ok, whereis(Worker)};
235 Err ->
236
:-(
Err
237 end.
238
239 call(PoolType, Request) ->
240
:-(
call(PoolType, global, Request).
241
242 call(PoolType, HostType, Request) ->
243
:-(
call(PoolType, HostType, default, Request).
244
245 call(PoolType, HostType, Tag, Request) ->
246 117263 case get_pool(PoolType, HostType, Tag) of
247 {ok, #mongoose_wpool{strategy = Strategy, call_timeout = CallTimeout} = Pool} ->
248 117263 wpool:call(make_pool_name(Pool), Request, Strategy, CallTimeout);
249 Err ->
250
:-(
Err
251 end.
252
253 call(PoolType, HostType, Tag, HashKey, Request) ->
254
:-(
case get_pool(PoolType, HostType, Tag) of
255 {ok, #mongoose_wpool{call_timeout = CallTimeout} = Pool} ->
256
:-(
wpool:call(make_pool_name(Pool), Request, {hash_worker, HashKey}, CallTimeout);
257 Err ->
258
:-(
Err
259 end.
260
261 send_request(PoolType, Request) ->
262
:-(
send_request(PoolType, global, Request).
263
264 send_request(PoolType, HostType, Request) ->
265
:-(
send_request(PoolType, HostType, default, Request).
266
267 send_request(PoolType, HostType, Tag, Request) ->
268 657 case get_pool(PoolType, HostType, Tag) of
269 {ok, #mongoose_wpool{strategy = Strategy, call_timeout = CallTimeout} = Pool} ->
270 657 wpool_send_request(make_pool_name(Pool), Request, Strategy, CallTimeout);
271 Err ->
272
:-(
Err
273 end.
274
275 send_request(PoolType, HostType, Tag, HashKey, Request) ->
276
:-(
case get_pool(PoolType, HostType, Tag) of
277 {ok, #mongoose_wpool{call_timeout = CallTimeout} = Pool} ->
278
:-(
wpool_send_request(make_pool_name(Pool), Request, {hash_worker, HashKey}, CallTimeout);
279 Err ->
280
:-(
Err
281 end.
282
283 wpool_send_request(PoolName, Request, Strategy, Timeout) ->
284 657 wpool:send_request(PoolName, Request, Strategy, Timeout).
285
286 cast(PoolType, Request) ->
287
:-(
cast(PoolType, global, Request).
288
289 cast(PoolType, HostType, Request) ->
290
:-(
cast(PoolType, HostType, default, Request).
291
292 cast(PoolType, HostType, Tag, Request) ->
293 4 case get_pool(PoolType, HostType, Tag) of
294 {ok, #mongoose_wpool{strategy = Strategy} = Pool} ->
295 4 wpool:cast(make_pool_name(Pool), Request, Strategy);
296 Err ->
297
:-(
Err
298 end.
299
300 cast(PoolType, HostType, Tag, HashKey, Request) ->
301
:-(
case get_pool(PoolType, HostType, Tag) of
302 {ok, #mongoose_wpool{} = Pool} ->
303
:-(
wpool:cast(make_pool_name(Pool), Request, {hash_worker, HashKey});
304 Err ->
305
:-(
Err
306 end.
307
308 -spec get_pool_settings(pool_type(), host_type_or_global(), tag()) ->
309 #mongoose_wpool{} | undefined.
310 get_pool_settings(PoolType, HostType, Tag) ->
311 199 case get_pool(PoolType, HostType, Tag) of
312 199 {ok, PoolRec} -> PoolRec;
313
:-(
{error, {pool_not_started, _}} -> undefined
314 end.
315
316 -spec get_pools() -> [pool_name()].
317 get_pools() ->
318 93 lists:map(fun(#mongoose_wpool{name = Name}) -> Name end, ets:tab2list(?MODULE)).
319
320 stats(PoolType, HostType, Tag) ->
321
:-(
wpool:stats(make_pool_name(PoolType, HostType, Tag)).
322
323 -spec make_pool_name(pool_type(), scope(), tag()) -> proc_name().
324 make_pool_name(PoolType, HostType, Tag) when is_atom(HostType) ->
325 951 make_pool_name(PoolType, atom_to_binary(HostType, utf8), Tag);
326 make_pool_name(PoolType, HostType, Tag) when is_binary(HostType) ->
327 954 binary_to_atom(<<"mongoose_wpool$", (atom_to_binary(PoolType, utf8))/binary, $$,
328 HostType/binary, $$, (atom_to_binary(Tag, utf8))/binary>>, utf8).
329
330 make_pool_name(#mongoose_wpool{atom_name = undefined, name = {PoolType, HostType, Tag}}) ->
331
:-(
make_pool_name(PoolType, HostType, Tag);
332 make_pool_name(#mongoose_wpool{atom_name = AtomName}) ->
333 117957 AtomName.
334
335 -spec call_start_callback(pool_type(), list()) -> term().
336 call_start_callback(PoolType, Args) ->
337 190 call_callback(start, PoolType, Args).
338
339 -spec call_callback(callback_fun(), pool_type(), list()) -> term().
340 call_callback(CallbackFun, PoolType, Args) ->
341 566 try
342 566 CallbackModule = make_callback_module_name(PoolType),
343 566 erlang:apply(CallbackModule, CallbackFun, Args)
344 catch E:R:ST ->
345
:-(
?LOG_ERROR(#{what => pool_callback_failed,
346 pool_type => PoolType, callback_function => CallbackFun,
347
:-(
error => E, reason => R, stacktrace => ST}),
348
:-(
{error, {callback_crashed, CallbackFun, E, R, ST}}
349 end.
350
351 -spec make_callback_module_name(pool_type()) -> module().
352 make_callback_module_name(PoolType) ->
353 756 Name = "mongoose_wpool_" ++ atom_to_list(PoolType),
354 756 list_to_atom(Name).
355
356 -spec get_host_type_specific_pools([mongooseim:host_type()]) -> [pool_map_in()].
357 get_host_type_specific_pools(HostTypes) ->
358 97 lists:append([ mongoose_config:get_opt({outgoing_pools, HostType}, [])
359 97 || HostType <- HostTypes ]).
360
361 -spec expand_pools([pool_map_in()], [pool_map_in()], [mongooseim:host_type()]) -> [pool_map()].
362 expand_pools(Pools, PerHostType, HostTypes) ->
363 %% First we select only vhost/host_type specific pools
364 97 HostSpecific = [ {Type, HT, Tag}
365 97 || #{type := Type, scope := HT, tag := Tag} <- PerHostType ],
366 %% Then we expand all pools with `host_type` as scope
367 %% but using host_type specific configs if they were provided
368 97 F = fun(M = #{type := PoolType, scope := host_type, tag := Tag}) ->
369
:-(
[M#{scope => HostType} || HostType <- HostTypes,
370
:-(
not lists:member({PoolType, HostType, Tag}, HostSpecific)];
371 190 (Other) -> [Other]
372 end,
373 97 Pools1 = lists:flatmap(F, Pools),
374 97 lists:map(fun prepare_pool_map/1, PerHostType ++ Pools1).
375
376 -spec prepare_pool_map(pool_map_in()) -> pool_map().
377 prepare_pool_map(Pool = #{scope := HT, opts := Opts}) ->
378 %% Rename "scope" field to "host_type" and change wpool opts to a KV list
379 190 Pool1 = maps:remove(scope, Pool),
380 190 Pool1#{host_type => HT, opts => maps:to_list(Opts)}.
381
382 -spec get_unique_types([pool_map_in()], [pool_map_in()]) -> [pool_type()].
383 get_unique_types(Pools, HostTypeSpecific) ->
384 97 lists:usort([maps:get(type, Pool) || Pool <- Pools ++ HostTypeSpecific]).
385
386 -spec get_pool(pool_type(), host_type_or_global(), tag()) -> pool_record_result().
387 get_pool(PoolType, HostType, Tag) ->
388 232335 case ets:lookup(?MODULE, {PoolType, HostType, Tag}) of
389 114179 [] when is_binary(HostType) -> get_pool(PoolType, global, Tag);
390
:-(
[] -> {error, {pool_not_started, {PoolType, HostType, Tag}}};
391 118156 [Pool] -> {ok, Pool}
392 end.
Line Hits Source