Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/dreyfus_index.erl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ handle_call(info, _From, State) -> % obsolete
Reply = info_int(State#state.index_pid),
{reply, Reply, State}.

handle_cast({ddoc_updated, DDocResult}, #state{} = State) ->
#index{sig = Sig} = State#state.index,
KeepIndexProcess = case DDocResult of
{not_found, deleted} ->
false;
{ok, DDoc} ->
Indexes = design_doc_to_indexes(DDoc),
% find if any index of new DDoc has the same Sig
lists:foldl(fun(#index{sig=SigNew}, Acc) ->
(SigNew == Sig) or Acc
end, false, Indexes)
end,
case KeepIndexProcess of
false ->
{stop, {shutdown, ddoc_updated}, State};
true ->
{noreply, State}
end;
handle_cast(_Msg, State) ->
{noreply, State}.

Expand Down Expand Up @@ -214,6 +232,9 @@ handle_info({'DOWN',_,_,Pid,Reason}, #state{
[gen_server:reply(P, {error, Reason}) || {P, _} <- WaitList],
{stop, normal, State}.

terminate({shutdown, ddoc_updated}, State) ->
Waiters = State#state.waiting_list,
[gen_server:reply(From, ddoc_updated) || {From, _} <- Waiters];
terminate(_Reason, _State) ->
ok.

Expand Down
83 changes: 68 additions & 15 deletions src/dreyfus_index_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

-define(BY_SIG, dreyfus_by_sig).
-define(BY_PID, dreyfus_by_pid).
-define(BY_DB, dreyfus_by_db).

% public api.
-export([start_link/0, get_index/2, get_disk_size/2]).
Expand All @@ -44,8 +45,9 @@ get_disk_size(DbName, Index) ->
% gen_server functions.

init([]) ->
ets:new(?BY_SIG, [set, private, named_table]),
ets:new(?BY_SIG, [set, protected, named_table]),
ets:new(?BY_PID, [set, private, named_table]),
ets:new(?BY_DB, [bag, protected, named_table]),
couch_event:link_listener(?MODULE, handle_db_event, nil, [all_dbs]),
process_flag(trap_exit, true),
{ok, nil}.
Expand All @@ -61,20 +63,22 @@ handle_call({get_index, DbName, #index{sig=Sig}=Index}, From, State) ->
ets:insert(?BY_SIG, {{DbName, Sig}, [From | WaitList]}),
{noreply, State};
[{_, ExistingPid}] ->
DDocId = Index#index.ddoc_id,
ets:insert(?BY_DB, {DbName, {DDocId, Sig}}),
{reply, {ok, ExistingPid}, State}
end;

handle_call({get_disk_size, DbName, #index{sig=Sig}=Index}, From, State) ->
handle_call({get_disk_size, DbName, #index{sig=Sig}}, _From, State) ->
Path = <<DbName/binary,"/",Sig/binary>>,
Reply = clouseau_rpc:disk_size(Path),
{reply, Reply, State};

handle_call({open_ok, DbName, Sig, NewPid}, {OpenerPid, _}, State) ->
handle_call({open_ok, DbName, DDocId, Sig, NewPid}, {OpenerPid, _}, State) ->
link(NewPid),
[{_, WaitList}] = ets:lookup(?BY_SIG, {DbName, Sig}),
[gen_server:reply(From, {ok, NewPid}) || From <- WaitList],
ets:delete(?BY_PID, OpenerPid),
add_to_ets(NewPid, DbName, Sig),
add_to_ets(NewPid, DbName, DDocId, Sig),
{reply, ok, State};

handle_call({open_error, DbName, Sig, Error}, {OpenerPid, _}, State) ->
Expand All @@ -86,6 +90,9 @@ handle_call({open_error, DbName, Sig, Error}, {OpenerPid, _}, State) ->

handle_cast({cleanup, DbName}, State) ->
clouseau_rpc:cleanup(DbName),
{noreply, State};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tonysun83 Are you referring to the line 93? If yes, we did not add this here, it was here before - this a standard way to finish handle_cast. This line has changed as we added a new handle_cast clause.

handle_cast({rem_from_ets, [DbName, DDocId, Sig]}, State) ->
ets:delete_object(?BY_DB, {DbName, {DDocId, Sig}}),
{noreply, State}.

handle_info({'EXIT', FromPid, Reason}, State) ->
Expand Down Expand Up @@ -115,31 +122,77 @@ code_change(_OldVsn, nil, _Extra) ->

% private functions

handle_db_event(DbName, created, _St) ->
handle_db_event(DbName, created, St) ->
gen_server:cast(?MODULE, {cleanup, DbName}),
{ok, nil};
handle_db_event(DbName, deleted, _St) ->
{ok, St};
handle_db_event(DbName, deleted, St) ->
gen_server:cast(?MODULE, {cleanup, DbName}),
{ok, nil};
handle_db_event(_DbName, _Event, _St) ->
{ok, nil}.

new_index(DbName, #index{sig=Sig}=Index) ->
{ok, St};
handle_db_event(<<"shards/", _/binary>> = DbName, {ddoc_updated,
DDocId}, St) ->
DDocResult = couch_util:with_db(DbName, fun(Db) ->
couch_db:open_doc(Db, DDocId, [ejson_body, ?ADMIN_CTX])
end),
DbShards = [mem3:name(Sh) || Sh <- mem3:local_shards(mem3:dbname(DbName))],
lists:foreach(fun(DbShard) ->
lists:foreach(fun({_DbShard, {_DDocId, Sig}}) ->
% check if there are other ddocs with the same Sig for the same db
SigDDocs = ets:match_object(?BY_DB, {DbShard, {'_', Sig}}),
if length(SigDDocs) > 1 ->
% remove a record from this DDoc from ?BY_DB
Args = [DbShard, DDocId, Sig],
gen_server:cast(?MODULE, {rem_from_ets, Args});
true ->
% single DDoc with this Sig - maybe close dreyfus_index process
case ets:lookup(?BY_SIG, {DbShard, Sig}) of
[{_, IndexPid}] -> (catch
gen_server:cast(IndexPid, {ddoc_updated, DDocResult}));
[] -> []
end
end
end, ets:match_object(?BY_DB, {DbShard, {DDocId, '_'}}))
end, DbShards),
{ok, St};
handle_db_event(DbName, {ddoc_updated, DDocId}, St) ->
DDocResult = couch_util:with_db(DbName, fun(Db) ->
couch_db:open_doc(Db, DDocId, [ejson_body, ?ADMIN_CTX])
end),
lists:foreach(fun({_DbName, {_DDocId, Sig}}) ->
SigDDocs = ets:match_object(?BY_DB, {DbName, {'_', Sig}}),
if length(SigDDocs) > 1 ->
Args = [DbName, DDocId, Sig],
gen_server:cast(?MODULE, {rem_from_ets, Args});
true ->
case ets:lookup(?BY_SIG, {DbName, Sig}) of
[{_, IndexPid}] -> (catch
gen_server:cast(IndexPid, {ddoc_updated, DDocResult}));
[] -> []
end
end
end, ets:match_object(?BY_DB, {DbName, {DDocId, '_'}})),
{ok, St};
handle_db_event(_DbName, _Event, St) ->
{ok, St}.


new_index(DbName, #index{ddoc_id=DDocId, sig=Sig}=Index) ->
case (catch dreyfus_index:start_link(DbName, Index)) of
{ok, NewPid} ->
Msg = {open_ok, DbName, Sig, NewPid},
Msg = {open_ok, DbName, DDocId, Sig, NewPid},
ok = gen_server:call(?MODULE, Msg, infinity),
unlink(NewPid);
Error ->
Msg = {open_error, DbName, Sig, Error},
ok = gen_server:call(?MODULE, Msg, infinity)
end.

add_to_ets(Pid, DbName, Sig) ->
add_to_ets(Pid, DbName, DDocId, Sig) ->
true = ets:insert(?BY_PID, {Pid, {DbName, Sig}}),
true = ets:insert(?BY_SIG, {{DbName, Sig}, Pid}).
true = ets:insert(?BY_SIG, {{DbName, Sig}, Pid}),
true = ets:insert(?BY_DB, {DbName, {DDocId, Sig}}).

delete_from_ets(Pid, DbName, Sig) ->
true = ets:match_delete(?BY_DB, {DbName, {'_', Sig}}),
true = ets:delete(?BY_PID, Pid),
true = ets:delete(?BY_SIG, {DbName, Sig}).

Loading