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
|
-module(couch_proc_manager).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-export([start_link/0, get_proc_count/0]).
-include("couch_db.hrl").
-record(state, {tab}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
get_proc_count() ->
gen_server:call(?MODULE, get_proc_count).
init([]) ->
process_flag(trap_exit, true),
{ok, #state{tab = ets:new(procs, [{keypos, #proc.pid}])}}.
handle_call(get_table, _From, State) ->
{reply, State#state.tab, State};
handle_call(get_proc_count, _From, State) ->
{reply, ets:info(State#state.tab, size), State};
handle_call({get_proc, #doc{body={Props}}=DDoc, DDocKey}, {Client, _}, State) ->
Lang = couch_util:get_value(<<"language">>, Props, <<"javascript">>),
try get_procs(State#state.tab, Lang) of
Procs ->
case proc_with_ddoc(DDoc, DDocKey, Procs) of
{ok, Proc0} ->
Proc = Proc0#proc{client = erlang:monitor(process, Client)},
ets:insert(State#state.tab, Proc),
{reply, {ok, Proc, get_query_server_config()}, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end
catch {unknown_query_language, _} ->
{reply, {unknown_query_language, Lang}, State};
error:Reason ->
?LOG_ERROR("~p ~p ~p", [?MODULE, Reason, erlang:get_stacktrace()]),
{reply, {error, Reason}, State}
end;
handle_call({get_proc, Lang}, {Client, _}, State) ->
try get_procs(State#state.tab, Lang) of
[Proc0|_] ->
Proc = Proc0#proc{client = erlang:monitor(process, Client)},
ets:insert(State#state.tab, Proc),
{reply, {ok, Proc, get_query_server_config()}, State}
catch {unknown_query_language, _} ->
{reply, {unknown_query_language, Lang}, State};
error:Reason ->
?LOG_ERROR("~p ~p ~p", [?MODULE, Reason, erlang:get_stacktrace()]),
{reply, {error, Reason}, State}
end;
handle_call({ret_proc, #proc{client=Ref, pid=Pid} = Proc}, _From, State) ->
erlang:demonitor(Ref, [flush]),
% We need to check if the process is alive here, as the client could be
% handing us a #proc{} with a dead one. We would have already removed the
% #proc{} from our own table, so the alternative is to do a lookup in the
% table before the insert. Don't know which approach is cheaper.
case is_process_alive(Pid) of true ->
maybe_reuse_proc(State#state.tab, Proc);
false -> ok end,
{reply, true, State};
handle_call(_Call, _From, State) ->
{reply, ignored, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({'EXIT', Pid, Reason}, State) ->
?LOG_INFO("~p ~p died ~p", [?MODULE, Pid, Reason]),
ets:delete(State#state.tab, Pid),
{noreply, State};
handle_info({'DOWN', Ref, _, _, _Reason}, State) ->
case ets:match_object(State#state.tab, #proc{client=Ref, _='_'}) of
[] ->
ok;
[#proc{pid = Pid} = Proc] ->
case is_process_alive(Pid) of true ->
maybe_reuse_proc(State#state.tab, Proc);
false -> ok end
end,
{noreply, State};
handle_info(_Msg, State) ->
{noreply, State}.
terminate(_Reason, #state{tab=Tab}) ->
ets:foldl(fun(#proc{pid=P}, _) -> couch_util:shutdown_sync(P) end, 0, Tab),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
maybe_reuse_proc(Tab, #proc{pid = Pid} = Proc) ->
Limit = couch_config:get("query_server_config", "os_process_soft_limit", "100"),
case ets:info(Tab, size) > list_to_integer(Limit) of
true ->
ets:delete(Tab, Pid),
unlink(Pid),
exit(Pid, kill);
false ->
garbage_collect(Pid),
ets:insert(Tab, Proc#proc{client=nil})
end.
get_procs(Tab, Lang) when is_binary(Lang) ->
get_procs(Tab, binary_to_list(Lang));
get_procs(Tab, Lang) when is_list(Lang) ->
case ets:match_object(Tab, #proc{lang=Lang, client=nil, _='_'}) of
[] ->
{ok, NewProc} = new_proc(Lang), % check OS process limit
[NewProc];
Procs ->
Procs
end.
new_proc(Lang) when is_list(Lang) ->
case couch_config:get("query_servers", Lang) of
undefined ->
case couch_config:get("native_query_servers", Lang) of
undefined ->
throw({unknown_query_language, Lang});
SpecStr ->
{ok, {M,F,A}} = couch_util:parse_term(SpecStr),
{ok, Pid} = apply(M, F, A),
make_proc(Pid, Lang, M)
end;
Command ->
{ok, Pid} = couch_os_process:start_link(Command),
make_proc(Pid, Lang, couch_os_process)
end.
make_proc(Pid, Lang, Mod) ->
Proc = #proc{
lang = Lang,
pid = Pid,
prompt_fun = {Mod, prompt},
set_timeout_fun = {Mod, set_timeout},
stop_fun = {Mod, stop}
},
{ok, Proc}.
get_query_server_config() ->
Limit = couch_config:get("query_server_config", <<"reduce_limit">>, "true"),
{[{<<"reduce_limit">>, list_to_atom(Limit)}]}.
proc_with_ddoc(DDoc, DDocKey, Procs) ->
Filter = fun(#proc{ddoc_keys=Keys}) -> not lists:member(DDocKey, Keys) end,
case lists:dropwhile(Filter, Procs) of
[DDocProc|_] ->
{ok, DDocProc};
[] ->
teach_any_proc(DDoc, DDocKey, Procs)
end.
teach_any_proc(DDoc, DDocKey, [Proc|Rest]) ->
try
teach_ddoc(DDoc, DDocKey, Proc)
catch _:_ ->
teach_any_proc(DDoc, DDocKey, Rest)
end;
teach_any_proc(_, _, []) ->
{error, noproc}.
teach_ddoc(DDoc, {DDocId, _Rev}=DDocKey, #proc{ddoc_keys=Keys}=Proc) ->
% send ddoc over the wire
% we only share the rev with the client we know to update code
% but it only keeps the latest copy, per each ddoc, around.
true = couch_query_servers:proc_prompt(Proc, [<<"ddoc">>, <<"new">>,
DDocId, couch_doc:to_json_obj(DDoc, [])]),
% we should remove any other ddocs keys for this docid
% because the query server overwrites without the rev
Keys2 = [{D,R} || {D,R} <- Keys, D /= DDocId],
% add ddoc to the proc
{ok, Proc#proc{ddoc_keys=[DDocKey|Keys2]}}.
|