nickva commented on code in PR #4811:
URL: https://github.com/apache/couchdb/pull/4811#discussion_r1362879000
##########
src/couch_index/src/couch_index_server.erl:
##########
@@ -167,19 +167,29 @@ handle_call({get_index, {_Mod, _IdxState, DbName, Sig} =
Args}, From, State) ->
[{_, Pid}] when is_pid(Pid) ->
{reply, {ok, Pid}, State}
end;
-handle_call({async_open, {DbName, DDocId, Sig}, {ok, Pid}}, {OpenerPid, _},
State) ->
- [{_, Waiters}] = ets:lookup(State#st.by_sig, {DbName, Sig}),
- [gen_server:reply(From, {ok, Pid}) || From <- Waiters],
+handle_call({async_open, {DbName, DDocId, Sig}, {ok, Pid}}, {OpenerPid, _} =
FromOpener, State) ->
link(Pid),
+ % Once linked with the indexer, dismiss and ignore the opener.
+ unlink(OpenerPid),
ets:delete(State#st.openers, OpenerPid),
- add_to_ets(DbName, Sig, DDocId, Pid, State),
- {reply, ok, State};
-handle_call({async_error, {DbName, _DDocId, Sig}, Error}, {OpenerPid, _},
State) ->
+ gen_server:reply(FromOpener, ok),
[{_, Waiters}] = ets:lookup(State#st.by_sig, {DbName, Sig}),
- [gen_server:reply(From, Error) || From <- Waiters],
+ add_to_ets(DbName, Sig, DDocId, Pid, State),
+ [gen_server:reply(From, {ok, Pid}) || From <- Waiters],
+ % Flush opener exit messages in case it died before we unlinked it
+ ok = flush_exit_messages_from(OpenerPid),
+ {noreply, State};
Review Comment:
Good question, @jaydoane. That's because we already replied early to the
opener with `gen_server:reply(FromOpener, ok)`. The "opener" call into the
index_server then gets to end early and it doesn't have to wait for us to
finish the rest of the call.
These two calls can now end a bit quicker and we should be able to garbage
collect them sooner.
```erlang
new_index({Mod, IdxState, DbName, Sig}) ->
DDocId = Mod:get(idx_name, IdxState),
case couch_index:start_link({Mod, IdxState}) of
{ok, Pid} ->
ok = gen_server:call(
server_name(DbName), {async_open, {DbName, DDocId, Sig},
{ok, Pid}}
),
unlink(Pid);
Error ->
ok = gen_server:call(
server_name(DbName), {async_error, {DbName, DDocId, Sig},
Error}
)
end.
```
The most importantly, however is we get the opener out of the way once we
already linked to the main indexer process, or received the error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]