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 2020/03/09 18:07:14 UTC

[GitHub] [couchdb] davisp commented on a change in pull request #2637: Index auto-updater

davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389860648
 
 

 ##########
 File path: src/fabric/src/fabric2_index.erl
 ##########
 @@ -0,0 +1,228 @@
+% 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(fabric2_index).
+
+
+-behaviour(gen_server).
+
+
+-export([
+    register_index/1,
+
+    db_updated/1,
+
+    start_link/0,
+
+    init/1,
+    terminate/2,
+    handle_call/3,
+    handle_cast/2,
+    handle_info/2,
+    code_change/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-callback build_indices(Db :: map(), DDocs :: list(#doc{})) ->
+    [{ok, JobId::binary()} | {error, any()}].
+
+
+-define(SHARDS, 32).
+-define(DEFAULT_DELAY_MSEC, 60000).
+-define(DEFAULT_RESOLUTION_MSEC, 10000).
+
+
+register_index(Mod) when is_atom(Mod) ->
+    Indices = lists:usort([Mod | registrations()]),
+    application:set_env(fabric, indices, Indices).
+
+
+db_updated(DbName) when is_binary(DbName) ->
+    Table = table(erlang:phash2(DbName) rem ?SHARDS),
+    ets:insert_new(Table, {DbName, now_msec()}).
+
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+
+init(_) ->
+    lists:foreach(fun(T) ->
+        spawn_link(fun() -> process_loop(T) end)
+    end, create_tables()),
+    {ok, nil}.
+
+
+terminate(_M, _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(Msg, St) ->
+    {stop, {bad_info, Msg}, St}.
+
+
+code_change(_OldVsn, St, _Extra) ->
+    {ok, St}.
+
+
+create_tables() ->
+    Opts = [
+        named_table,
+        public,
+        {write_concurrency, true},
+        {read_concurrency, true}
+    ],
+    Tables = [table(N) || N <- lists:seq(0, ?SHARDS - 1)],
+    [ets:new(T, Opts) || T <- Tables].
+
+
+table(Id) when is_integer(Id), Id >= 0 andalso Id < ?SHARDS ->
+    list_to_atom("fabric2_index_" ++ integer_to_list(Id)).
+
+
+process_loop(Table) ->
+    % In some cases we'd want to monitor the parent process and exit if it
+    % exits `normal` since links won't trigger then. However our parent never
+    % exists `normal`. When supervisor shuts it down, it will do it with a
+    % `shutdown` signal. Then the `process_loop` also never exits `normal` so
+    % there is no need to monitor it from the parent.
 
 Review comment:
   I remember asking about this, but I'm not sure its important enough to keep a long comment about something we're not doing that's more Erlang system knowledge.

----------------------------------------------------------------
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