1 |
|
-module(mongoose_backend). |
2 |
|
|
3 |
|
%% API |
4 |
|
-export([init/4, |
5 |
|
call/4, |
6 |
|
call_tracked/4, |
7 |
|
is_exported/4, |
8 |
|
get_backend_name/2]). |
9 |
|
|
10 |
|
%% remove after mongoose_rdbms is refactored not to use dynamically compiled backend |
11 |
|
-ignore_xref([get_backend_name/2]). |
12 |
|
|
13 |
|
%% For debugging and tests |
14 |
|
-export([get_backend_module/2]). |
15 |
|
-ignore_xref([get_backend_module/2]). |
16 |
|
|
17 |
|
-type function_name() :: atom(). |
18 |
|
-type main_module() :: module(). |
19 |
|
-type backend_module() :: module(). |
20 |
|
-type host_type_or_global() :: mongooseim:host_type_or_global(). |
21 |
|
|
22 |
|
-spec init(HostType :: host_type_or_global(), |
23 |
|
MainModule :: main_module(), |
24 |
|
TrackedFuns :: [function_name()], |
25 |
|
Opts :: map()) -> ok. |
26 |
|
init(HostType, MainModule, TrackedFuns, Opts) -> |
27 |
1120 |
ensure_backend_metrics(MainModule, TrackedFuns), |
28 |
1120 |
Backend = maps:get(backend, Opts, mnesia), |
29 |
1120 |
BackendModule = backend_module(MainModule, Backend), |
30 |
1120 |
persist_backend_name(HostType, MainModule, Backend, BackendModule), |
31 |
1120 |
ok. |
32 |
|
|
33 |
|
backend_module(Module, Backend) -> |
34 |
1120 |
list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Backend)). |
35 |
|
|
36 |
|
call_metric(MainModule, FunName) -> |
37 |
201612 |
[backends, MainModule, calls, FunName]. |
38 |
|
|
39 |
|
time_metric(MainModule, FunName) -> |
40 |
201612 |
[backends, MainModule, FunName]. |
41 |
|
|
42 |
|
backend_key(HostType, MainModule) -> |
43 |
318396 |
{backend_module, HostType, MainModule}. |
44 |
|
|
45 |
|
backend_name_key(HostType, MainModule) -> |
46 |
10973 |
{backend_name, HostType, MainModule}. |
47 |
|
|
48 |
|
-spec ensure_backend_metrics(MainModule :: main_module(), |
49 |
|
FunNames :: [function_name()]) -> ok. |
50 |
|
ensure_backend_metrics(MainModule, FunNames) -> |
51 |
1120 |
EnsureFun = fun(FunName) -> |
52 |
3898 |
CM = call_metric(MainModule, FunName), |
53 |
3898 |
TM = time_metric(MainModule, FunName), |
54 |
3898 |
mongoose_metrics:ensure_metric(global, CM, spiral), |
55 |
3898 |
mongoose_metrics:ensure_metric(global, TM, histogram) |
56 |
|
end, |
57 |
1120 |
lists:foreach(EnsureFun, FunNames). |
58 |
|
|
59 |
|
persist_backend_name(HostType, MainModule, Backend, BackendModule) -> |
60 |
1120 |
ModuleKey = backend_key(HostType, MainModule), |
61 |
1120 |
persistent_term:put(ModuleKey, BackendModule), |
62 |
1120 |
NameKey = backend_name_key(HostType, MainModule), |
63 |
1120 |
persistent_term:put(NameKey, Backend). |
64 |
|
|
65 |
|
%% @doc Get a backend module, stored in init. |
66 |
|
-spec get_backend_module(HostType :: host_type_or_global(), |
67 |
|
MainModule :: main_module()) -> |
68 |
|
BackendModule :: backend_module(). |
69 |
|
get_backend_module(HostType, MainModule) -> |
70 |
317276 |
ModuleKey = backend_key(HostType, MainModule), |
71 |
317276 |
persistent_term:get(ModuleKey). |
72 |
|
|
73 |
|
%% @doc Get a backend name, like `pgsql', stored in init. |
74 |
|
-spec get_backend_name(HostType :: host_type_or_global(), |
75 |
|
MainModule :: main_module()) -> BackendName :: atom(). |
76 |
|
get_backend_name(HostType, MainModule) -> |
77 |
9853 |
Key = backend_name_key(HostType, MainModule), |
78 |
9853 |
persistent_term:get(Key). |
79 |
|
|
80 |
|
-spec call(HostType :: host_type_or_global(), |
81 |
|
MainModule :: main_module(), |
82 |
|
FunName :: function_name(), |
83 |
|
Args :: [term()]) -> term(). |
84 |
|
call(HostType, MainModule, FunName, Args) -> |
85 |
118681 |
BackendModule = get_backend_module(HostType, MainModule), |
86 |
118681 |
erlang:apply(BackendModule, FunName, Args). |
87 |
|
|
88 |
|
-spec call_tracked(HostType :: host_type_or_global(), |
89 |
|
MainModule :: main_module(), |
90 |
|
FunName :: function_name(), |
91 |
|
Args :: [term()]) -> term(). |
92 |
|
call_tracked(HostType, MainModule, FunName, Args) -> |
93 |
197714 |
BackendModule = get_backend_module(HostType, MainModule), |
94 |
197714 |
CM = call_metric(MainModule, FunName), |
95 |
197714 |
TM = time_metric(MainModule, FunName), |
96 |
197714 |
mongoose_metrics:update(global, CM, 1), |
97 |
197714 |
{Time, Result} = timer:tc(BackendModule, FunName, Args), |
98 |
197714 |
mongoose_metrics:update(global, TM, Time), |
99 |
197714 |
Result. |
100 |
|
|
101 |
|
-spec is_exported(HostType :: host_type_or_global(), |
102 |
|
MainModule :: main_module(), |
103 |
|
FunName :: function_name(), |
104 |
|
Arity :: integer()) -> boolean(). |
105 |
|
is_exported(HostType, MainModule, Function, Arity) -> |
106 |
335 |
BackendModule = get_backend_module(HostType, MainModule), |
107 |
335 |
erlang:function_exported(BackendModule, Function, Arity). |