blob: e5ed7b3cbe281e5ffb591972d679f1b5fad057a3 [file] [log] [blame]
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
-module(fabric_view_all_docs).
-export([go/5]).
-export([open_doc/4]). % exported for spawn
-include_lib("fabric/include/fabric.hrl").
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_mrview/include/couch_mrview.hrl").
go(DbName, Options, #mrargs{keys=undefined} = QueryArgs, Callback, Acc0) ->
Workers = fabric_util:submit_jobs(mem3:shards(DbName),all_docs,[Options, QueryArgs]),
#mrargs{limit = Limit, skip = Skip} = QueryArgs,
State = #collector{
query_args = QueryArgs,
callback = Callback,
counters = fabric_dict:init(Workers, 0),
skip = Skip,
limit = Limit,
user_acc = Acc0
},
RexiMon = fabric_util:create_monitors(Workers),
try rexi_utils:recv(Workers, #shard.ref, fun handle_message/3,
State, infinity, 5000) of
{ok, NewState} ->
{ok, NewState#collector.user_acc};
{timeout, NewState} ->
Callback({error, timeout}, NewState#collector.user_acc);
{error, Resp} ->
{ok, Resp}
after
rexi_monitor:stop(RexiMon),
fabric_util:cleanup(Workers)
end;
go(DbName, Options, QueryArgs, Callback, Acc0) ->
#mrargs{
direction = Dir,
include_docs = IncludeDocs,
limit = Limit,
skip = Skip,
keys = Keys0
} = QueryArgs,
{_, Ref0} = spawn_monitor(fun() -> exit(fabric:get_doc_count(DbName)) end),
SpawnFun = fun(Key) ->
spawn_monitor(?MODULE, open_doc, [DbName, Options, Key, IncludeDocs])
end,
MaxJobs = all_docs_concurrency(),
Keys1 = case Dir of
fwd -> Keys0;
_ -> lists:reverse(Keys0)
end,
Keys2 = case Skip < length(Keys1) of
true -> lists:nthtail(Skip, Keys1);
false -> []
end,
Keys3 = case Limit < length(Keys2) of
true -> lists:sublist(Keys2, Limit);
false -> Keys2
end,
receive {'DOWN', Ref0, _, _, {ok, TotalRows}} ->
{ok, Acc1} = Callback({meta, [{total, TotalRows}]}, Acc0),
{ok, Acc2} = doc_receive_loop(
Keys3, queue:new(), SpawnFun, MaxJobs, Callback, Acc1
),
Callback(complete, Acc2)
after 10000 ->
Callback(timeout, Acc0)
end.
handle_message({rexi_DOWN, _, {_, NodeRef}, _}, _, State) ->
fabric_view:remove_down_shards(State, NodeRef);
handle_message({rexi_EXIT, Reason}, Worker, State) ->
#collector{callback=Callback, counters=Counters0, user_acc=Acc} = State,
Counters = fabric_dict:erase(Worker, Counters0),
case fabric_view:is_progress_possible(Counters) of
true ->
{ok, State#collector{counters = Counters}};
false ->
{ok, Resp} = Callback({error, fabric_util:error_info(Reason)}, Acc),
{error, Resp}
end;
handle_message({meta, Meta0}, {Worker, From}, State) ->
Tot = couch_util:get_value(total, Meta0, 0),
Off = couch_util:get_value(offset, Meta0, 0),
#collector{
callback = Callback,
counters = Counters0,
total_rows = Total0,
offset = Offset0,
user_acc = AccIn
} = State,
case fabric_dict:lookup_element(Worker, Counters0) of
undefined ->
% this worker lost the race with other partition copies, terminate
gen_server:reply(From, stop),
{ok, State};
0 ->
gen_server:reply(From, ok),
Counters1 = fabric_dict:update_counter(Worker, 1, Counters0),
Counters2 = fabric_view:remove_overlapping_shards(Worker, Counters1),
Total = Total0 + Tot,
Offset = Offset0 + Off,
case fabric_dict:any(0, Counters2) of
true ->
{ok, State#collector{
counters = Counters2,
total_rows = Total,
offset = Offset
}};
false ->
FinalOffset = erlang:min(Total, Offset+State#collector.skip),
Meta = [{total, Total}, {offset, FinalOffset}],
{Go, Acc} = Callback({meta, Meta}, AccIn),
{Go, State#collector{
counters = fabric_dict:decrement_all(Counters2),
total_rows = Total,
offset = FinalOffset,
user_acc = Acc
}}
end
end;
handle_message(#view_row{} = Row, {Worker, From}, State) ->
#collector{query_args = Args, counters = Counters0, rows = Rows0} = State,
Dir = Args#mrargs.direction,
Rows = merge_row(Dir, Row#view_row{worker={Worker, From}}, Rows0),
Counters1 = fabric_dict:update_counter(Worker, 1, Counters0),
State1 = State#collector{rows=Rows, counters=Counters1},
fabric_view:maybe_send_row(State1);
handle_message(complete, Worker, State) ->
Counters = fabric_dict:update_counter(Worker, 1, State#collector.counters),
fabric_view:maybe_send_row(State#collector{counters = Counters}).
merge_row(fwd, Row, Rows) ->
lists:keymerge(#view_row.id, [Row], Rows);
merge_row(rev, Row, Rows) ->
lists:rkeymerge(#view_row.id, [Row], Rows).
all_docs_concurrency() ->
Value = config:get("fabric", "all_docs_concurrency", "10"),
try
list_to_integer(Value)
catch _:_ ->
10
end.
doc_receive_loop(Keys, Pids, SpawnFun, MaxJobs, Callback, AccIn) ->
case {Keys, queue:len(Pids)} of
{[], 0} ->
{ok, AccIn};
{[K | RKeys], Len} when Len < MaxJobs ->
Pids1 = queue:in(SpawnFun(K), Pids),
doc_receive_loop(RKeys, Pids1, SpawnFun, MaxJobs, Callback, AccIn);
_ ->
{{value, {Pid, Ref}}, RestPids} = queue:out(Pids),
receive {'DOWN', Ref, process, Pid, #view_row{} = Row} ->
case Callback(fabric_view:transform_row(Row), AccIn) of
{ok, Acc} ->
doc_receive_loop(
Keys, RestPids, SpawnFun, MaxJobs, Callback, Acc
);
{stop, Acc} ->
cancel_read_pids(RestPids),
{ok, Acc}
end
after 10000 ->
timeout
end
end.
open_doc(DbName, Options, Id, IncludeDocs) ->
Row = case fabric:open_doc(DbName, Id, [deleted | Options]) of
{not_found, missing} ->
Doc = undefined,
#view_row{key=Id};
{ok, #doc{deleted=true, revs=Revs}} ->
Doc = null,
{RevPos, [RevId|_]} = Revs,
Value = {[{rev,couch_doc:rev_to_str({RevPos, RevId})}, {deleted,true}]},
#view_row{key=Id, id=Id, value=Value};
{ok, #doc{revs=Revs} = Doc0} ->
Doc = couch_doc:to_json_obj(Doc0, []),
{RevPos, [RevId|_]} = Revs,
Value = {[{rev,couch_doc:rev_to_str({RevPos, RevId})}]},
#view_row{key=Id, id=Id, value=Value}
end,
exit(if IncludeDocs -> Row#view_row{doc=Doc}; true -> Row end).
cancel_read_pids(Pids) ->
case queue:out(Pids) of
{{value, {Pid, Ref}}, RestPids} ->
exit(Pid, kill),
erlang:demonitor(Ref, [flush]),
cancel_read_pids(RestPids);
{empty, _} ->
ok
end.