You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2019/07/26 12:29:46 UTC

[GitHub] [couchdb] iilyak commented on a change in pull request #2060: CouchDB map indexes on FDB

iilyak commented on a change in pull request #2060: CouchDB map indexes on FDB
URL: https://github.com/apache/couchdb/pull/2060#discussion_r307720331
 
 

 ##########
 File path: src/couch_views/src/couch_views_server.erl
 ##########
 @@ -0,0 +1,101 @@
+% 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(couch_views_server).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    start_link/0
+]).
+
+
+-export([
+    init/1,
+    terminate/2,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    code_change/3
+]).
+
+
+-define(MAX_WORKERS, 100).
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    process_flag(trap_exit, true),
+    couch_views_jobs:set_timeout(),
+    State = #{
+        workers => [],
+        num_workers => num_workers()
+    },
+    {ok, spawn_workers(State)}.
+
+
+terminate(_, _St) ->
+    ok.
+
+
+handle_call(Msg, _From, St) ->
+    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.
+
+
+handle_cast(Msg, St) ->
+    {stop, {bad_cast, Msg}, St}.
+
+
+handle_info({'EXIT', Pid, Reason}, State) ->
+    #{workers := Workers} = State,
+    case Workers -- [Pid] of
+        Workers ->
+            LogMsg = "~p : unknown process ~p exited with ~p",
+            couch_log:error(LogMsg, [?MODULE, Pid, Reason]),
+            {stop, {unknown_pid_exit, Pid}, State};
+        NewWorkers ->
+            if Reason == normal -> ok; true ->
+                LogMsg = "~p : indexer process ~p exited with ~p",
+                couch_log:error(LogMsg, [?MODULE, Pid, Reason])
+            end,
+            {noreply, spawn_workers(State#{workers := NewWorkers})}
+    end;
+
+handle_info(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+spawn_workers(State) ->
 
 Review comment:
   Not a huge concern if MAX_WORKERS would always be 100. However if we ever want to increase number of them (after performance evaluation) we might need to rewrite this function. The `length(Workers) < NumWorkers` is slow part. The opportunities for rewrite are:
   - use map to hold workers (this would also speed up `case Workers -- [Pid] of` on line 65)
   - write this function as:
   ```
   spawn_workers(#{workers = Workers, num_workers = NumWorkers} = State) -> 
     HowManyToSpawn = NumWorkers - length(Workers),
     spawn_workers(State, HowManyToSpawn).
   spawn_workers(State, 0) -> 
     State;
   spawn_workers(State, HowManyToSpawn) ->
      ...
      spawn_workers(State, HowManyToSpawn - 1).
   ```
   
   Feel free to ignore since for list of 100 it is a questionable optimization.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services