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