This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dpi.erl
134 lines (119 loc) · 4.53 KB
/
dpi.erl
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
-module(dpi).
-compile({parse_transform, dpi_transform}).
-export([load/1, unload/0]).
-export([load_unsafe/0]).
-export([safe/1, safe/2, safe/3]).
-include("dpiContext.hrl").
-include("dpiConn.hrl").
-include("dpiStmt.hrl").
-include("dpiQueryInfo.hrl").
-include("dpiData.hrl").
-include("dpiVar.hrl").
%===============================================================================
% Slave Node APIs
%===============================================================================
load(SlaveNodeName) when is_atom(SlaveNodeName) ->
case get(dpi_node) of
undefined ->
case is_alive() of
false -> {error, not_distributed};
true ->
case start_slave(SlaveNodeName) of
{ok, SlaveNode} ->
put(dpi_node, SlaveNode),
ok = rpc_call(
SlaveNode, code, add_paths, [code:get_path()]
),
rpc_call(
SlaveNode, dpi, load_unsafe, []
),
ok;
{error, {already_running, SlaveNode}} ->
put(dpi_node, SlaveNode),
ok;
Error -> Error
end
end;
SlaveNode ->
case catch rpc_call(SlaveNode, erlang, monotonic_time, []) of
Time when is_integer(Time) ->
ok;
_ ->
catch unload(),
load(SlaveNodeName)
end
end.
unload() ->
SlaveNode = erase(dpi_node),
slave:stop(SlaveNode).
%===============================================================================
% NIF test / debug interface (DO NOT use in production)
%===============================================================================
load_unsafe() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, _} ->
EbinDir = filename:dirname(code:which(?MODULE)),
AppPath = filename:dirname(EbinDir),
filename:join(AppPath, "priv");
Path -> Path
end,
case erlang:load_nif(filename:join(PrivDir, "dpi_nif"), 0) of
ok -> ok;
{error, {reload, _}} -> ok;
{error, Error} -> {error, Error}
end.
%===============================================================================
% local helper functions
%===============================================================================
start_slave(SlaveNodeName) when is_atom(SlaveNodeName) ->
[_,SlaveHost] = string:tokens(atom_to_list(node()), "@"),
ExtraArgs =
case {init:get_argument(pa), init:get_argument(boot)} of
{error, error} -> {error, bad_config};
{error, {ok, [[Boot]]}} ->
[_ | T] = lists:reverse(filename:split(Boot)),
StartClean = filename:join(lists:reverse(["start_clean" | T])),
case filelib:is_regular(StartClean ++ ".boot") of
true -> " -boot \"" ++ StartClean ++ "\"";
false ->
% {error, "Start clean boot not found"}
io:format(user, "[ERROR] REVISIT ~p:~p boot file not found!!~n", [?MODULE, ?LINE]),
[]
end;
{{ok, _}, _} -> []
end,
case ExtraArgs of
{error, _} = Error -> Error;
ExtraArgs ->
slave:start(
SlaveHost, SlaveNodeName,
lists:concat([
" -hidden ",
"-setcookie ", erlang:get_cookie(),
ExtraArgs
])
)
end.
slave_call(Mod, Fun, Args) ->
rpc_call(get(dpi_node), Mod, Fun, Args).
rpc_call(undefined, _Mod, _Fun, _Args) ->
{error, slave_down};
rpc_call(Node, Mod, Fun, Args) ->
case (catch rpc:call(Node, Mod, Fun, Args)) of
{badrpc, {'EXIT', Error}} ->
{error, Error};
{badrpc, nodedown} ->
erase(dpi_node),
{error, slave_down};
Result ->
Result
end.
-spec safe(atom(), atom(), list()) -> term().
safe(Module, Fun, Args) when is_atom(Module), is_atom(Fun), is_list(Args) ->
slave_call(Module, Fun, Args).
-spec safe(function(), list()) -> term().
safe(Fun, Args) when is_function(Fun), is_list(Args) ->
slave_call(erlang, apply, [Fun, Args]).
-spec safe(function()) -> term().
safe(Fun) when is_function(Fun)->
slave_call(erlang, apply, [Fun, []]).