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/05 17:16:45 UTC

[GitHub] [couchdb] nickva opened a new pull request #2637: wip 4

nickva opened a new pull request #2637: wip 4
URL: https://github.com/apache/couchdb/pull/2637
 
 
   Notes:
    
    * Instead of a separate ken2 / ken app use couch_views app
   
    * Main logic happens in `couch_view_updater` module
   
    * Introduce an index registration system to other indexing apps can register themselves using the `couch_views_indices`
   
    * Re-use the `after_doc_write` EPI callback from the Mango PR to let `couch_views` application receive updates and send them to `couch_views_updater:db_updated/1` function
   
    * `couch_views_updater:db_updated/1` should have minimal impact on the CRUD hot path. Each calls performs an single `ets:insert_new` call into sharded ETS table and immediately returns.
   
     * Each sharded ets table has it's separate process which bump all the registered indices after some configured amount of time.
   
    * How long a db will accumulate updates before it triggers a rebuild is configured via `updater_delay_msec` settings.
   
    * The resolution (how often each updater process checks) is configured via `updater_resolution_msec` setting. There is a 25% jitter applied here as well.
   
    * Multiple nodes would be bumping the same indexing job since CRUD requests are spread uniformly amongst the nodes. To avoid un-necessarily churning through couch_jobs' pending queue, noticed an optimization to avoid writes if the scheduled time during resubmit is not changed. So the first node which gets bump the job will put the job in the pending state, others will do a read, single KV read see that it's already pending and then not issue any writes.
   
   
   

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

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

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389883273
 
 

 ##########
 File path: src/fabric/test/fabric2_index_tests.erl
 ##########
 @@ -0,0 +1,302 @@
+% 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_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+% Should match fabric2_index define
+-define(SHARDS, 32).
+
+
+index_test_() ->
+    {
+        "Test fabric indexing module",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            with([
+                ?TDEF(register_index_works),
+                ?TDEF(single_update),
+                ?TDEF(multiple_updates),
+                ?TDEF(skip_db_if_no_ddocs),
+                ?TDEF(ignore_deleted_dbs),
+                ?TDEF(check_gen_server_messages)
+            ])
+        }
+    }.
+
+
+index_process_cleanup_test_() ->
+    {
+        "Test fabric process cleanup in indexing module",
+        {
+            foreach,
+            fun setup/0,
+            fun cleanup/1,
+            [
+                ?TDEF_FE(updater_processes_start),
+                ?TDEF_FE(updater_processes_stop),
+                ?TDEF_FE(indexing_can_be_disabled),
+                ?TDEF_FE(handle_indexer_blowing_up)
+            ]
+        }
+    }.
+
+
+setup() ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get_integer, fun
+        ("fabric", "index_updater_delay_msec", _) -> 200;
+        ("fabric", "index_updater_resolution_msec", _) -> 100;
+
+        (_, _, Default) -> Default
+    end),
+    meck:expect(config, get_boolean, fun
+        ("fabric", "index_updater_enabled", _) -> true;
+        (_, _, Default) -> Default
+    end),
+
+    Indices = application:get_env(fabric, indices, []),
+
+    Ctx = test_util:start_couch([fabric]),
+
+    % Db1 has a valid design doc, a deleted one and one with "autoupdate":false
+    {ok, Db1} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+    {_, _} = create_doc(Db1, <<"_design/doc1">>),
+
+    DDocId2 = <<"_design/doc2">>,
+    {DDocId2, {Pos, Rev}} = create_doc(Db1, DDocId2),
+    Delete2 = #doc{id = DDocId2, revs = {Pos, [Rev]}, deleted = true},
+    {ok, _} = fabric2_db:update_doc(Db1, Delete2),
+
+    NoAutoUpdate = {[{<<"autoupdate">>, false}]},
+    {_, _} = create_doc(Db1, <<"_design/doc3">>, NoAutoUpdate),
+
+    % Db2 doesn't have any desig documents
+    {ok, Db2} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+
+    #{db1 => Db1, db2 => Db2, ctx => Ctx, indices => Indices}.
+
+
+cleanup(#{db1 := Db1, db2 := Db2, ctx := Ctx, indices := Indices}) ->
+    catch fabric2_db:delete(fabric2_db:name(Db1), []),
+    catch fabric2_db:delete(fabric2_db:name(Db2), []),
+
+    test_util:stop_couch(Ctx),
+    application:set_env(fabric, indices, Indices),
+
+    meck:unload().
+
+
+register_index_works(_) ->
+    reset_callbacks(),
+
+    Mod1 = fabric2_test_callback1,
+    fabric2_index:register_index(Mod1),
+    Indices1 = application:get_env(fabric, indices, []),
+    ?assertEqual([Mod1], Indices1),
+
+    Mod2 = fabric2_test_callback2,
+    fabric2_index:register_index(Mod2),
+    Indices2 = application:get_env(fabric, indices, []),
+    ?assertEqual(lists:sort([Mod1, Mod2]), lists:sort(Indices2)).
+
+
+single_update(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback3,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    meck:wait(Mod, build_indices, 2, 2000),
+    ?assertEqual(1, meck:num_calls(Mod, build_indices, 2)).
+
+
+multiple_updates(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback4,
+    setup_callback(Mod),
+    create_docs(Db, 10),
+
+    % should be called at least once
+    meck:wait(Mod, build_indices, 2, 2000),
+
+    % Maybe called another time or two at most
 
 Review comment:
   1st call is call from the pervious test line
   
   2nd is if that might be called while we are inserting the docs.
   
   3rd is the one called after we are done inserting all the docs.
   

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

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

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389867292
 
 

 ##########
 File path: src/fabric/test/fabric2_index_tests.erl
 ##########
 @@ -0,0 +1,302 @@
+% 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_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+% Should match fabric2_index define
+-define(SHARDS, 32).
+
+
+index_test_() ->
+    {
+        "Test fabric indexing module",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            with([
+                ?TDEF(register_index_works),
+                ?TDEF(single_update),
+                ?TDEF(multiple_updates),
+                ?TDEF(skip_db_if_no_ddocs),
+                ?TDEF(ignore_deleted_dbs),
+                ?TDEF(check_gen_server_messages)
+            ])
+        }
+    }.
+
+
+index_process_cleanup_test_() ->
+    {
+        "Test fabric process cleanup in indexing module",
+        {
+            foreach,
+            fun setup/0,
+            fun cleanup/1,
+            [
+                ?TDEF_FE(updater_processes_start),
+                ?TDEF_FE(updater_processes_stop),
+                ?TDEF_FE(indexing_can_be_disabled),
+                ?TDEF_FE(handle_indexer_blowing_up)
+            ]
+        }
+    }.
+
+
+setup() ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get_integer, fun
+        ("fabric", "index_updater_delay_msec", _) -> 200;
+        ("fabric", "index_updater_resolution_msec", _) -> 100;
+
+        (_, _, Default) -> Default
+    end),
+    meck:expect(config, get_boolean, fun
+        ("fabric", "index_updater_enabled", _) -> true;
+        (_, _, Default) -> Default
+    end),
+
+    Indices = application:get_env(fabric, indices, []),
+
+    Ctx = test_util:start_couch([fabric]),
+
+    % Db1 has a valid design doc, a deleted one and one with "autoupdate":false
+    {ok, Db1} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+    {_, _} = create_doc(Db1, <<"_design/doc1">>),
+
+    DDocId2 = <<"_design/doc2">>,
+    {DDocId2, {Pos, Rev}} = create_doc(Db1, DDocId2),
+    Delete2 = #doc{id = DDocId2, revs = {Pos, [Rev]}, deleted = true},
+    {ok, _} = fabric2_db:update_doc(Db1, Delete2),
+
+    NoAutoUpdate = {[{<<"autoupdate">>, false}]},
+    {_, _} = create_doc(Db1, <<"_design/doc3">>, NoAutoUpdate),
+
+    % Db2 doesn't have any desig documents
+    {ok, Db2} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+
+    #{db1 => Db1, db2 => Db2, ctx => Ctx, indices => Indices}.
+
+
+cleanup(#{db1 := Db1, db2 := Db2, ctx := Ctx, indices := Indices}) ->
+    catch fabric2_db:delete(fabric2_db:name(Db1), []),
+    catch fabric2_db:delete(fabric2_db:name(Db2), []),
+
+    test_util:stop_couch(Ctx),
+    application:set_env(fabric, indices, Indices),
+
+    meck:unload().
+
+
+register_index_works(_) ->
+    reset_callbacks(),
+
+    Mod1 = fabric2_test_callback1,
+    fabric2_index:register_index(Mod1),
+    Indices1 = application:get_env(fabric, indices, []),
+    ?assertEqual([Mod1], Indices1),
+
+    Mod2 = fabric2_test_callback2,
+    fabric2_index:register_index(Mod2),
+    Indices2 = application:get_env(fabric, indices, []),
+    ?assertEqual(lists:sort([Mod1, Mod2]), lists:sort(Indices2)).
+
+
+single_update(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback3,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    meck:wait(Mod, build_indices, 2, 2000),
+    ?assertEqual(1, meck:num_calls(Mod, build_indices, 2)).
+
+
+multiple_updates(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback4,
+    setup_callback(Mod),
+    create_docs(Db, 10),
+
+    % should be called at least once
+    meck:wait(Mod, build_indices, 2, 2000),
+
+    % Maybe called another time or two at most
+    timer:sleep(500),
+    ?assert(meck:num_calls(Mod, build_indices, 2) =< 3).
+
+
+skip_db_if_no_ddocs(#{db2 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback5,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    timer:sleep(500),
+    ?assertEqual(0, meck:num_calls(Mod, build_indices, 2)).
+
+
+ignore_deleted_dbs(#{}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback6,
+    setup_callback(Mod),
+    lists:foreach(fun(_) ->
+        RandomDbName = fabric2_util:uuid(),
+        fabric2_index:db_updated(RandomDbName)
+    end, lists:seq(1, 10000)),
+
+    test_util:wait(fun() ->
+        case table_sizes() =:= 0 of
+            true -> ok;
+            false -> wait
+        end
+    end, 5000).
+
+
+check_gen_server_messages(#{}) ->
+    CallExpect = {stop, {bad_call, foo}, {bad_call, foo}, baz},
+    CastExpect = {stop, {bad_cast, foo}, bar},
+    InfoExpect = {stop, {bad_info, foo}, bar},
+    ?assertEqual(CallExpect, fabric2_index:handle_call(foo, bar, baz)),
+    ?assertEqual(CastExpect, fabric2_index:handle_cast(foo, bar)),
+    ?assertEqual(InfoExpect, fabric2_index:handle_info(foo, bar)),
+    ?assertEqual(ok, fabric2_index:terminate(shutdown, nil)),
+    ?assertEqual({ok, nil}, fabric2_index:code_change(v0, nil, extra)).
+
+
+updater_processes_start(#{}) ->
+    Pid = whereis(fabric2_index),
+    ?assert(is_process_alive(Pid)),
+    lists:map(fun(N) ->
+        ?assertEqual(tid(N), ets:info(tid(N), name))
+    end, lists:seq(0, ?SHARDS - 1)).
+
+
+updater_processes_stop(#{}) ->
+    Refs = lists:map(fun(N) ->
+        Pid = ets:info(tid(N), owner),
+        ?assert(is_process_alive(Pid)),
+        monitor(process, Pid)
+    end, lists:seq(0, ?SHARDS - 1)),
+
+    application:stop(fabric),
 
 Review comment:
   You don't have to restart fabric after this?

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

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

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389856174
 
 

 ##########
 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,
 
 Review comment:
   Can you separate gen_server exports into their own group?

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

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

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389848334
 
 

 ##########
 File path: src/couch_views/src/couch_views.erl
 ##########
 @@ -55,6 +64,22 @@ query(Db, DDoc, ViewName, Callback, Acc0, Args0) ->
     end.
 
 
+register_index() ->
 
 Review comment:
   Not sure I see a point having this here instead of directly in `couch_views_sup:register_views_index/0`.

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

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

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389867675
 
 

 ##########
 File path: src/fabric/test/fabric2_index_tests.erl
 ##########
 @@ -0,0 +1,302 @@
+% 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_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+% Should match fabric2_index define
+-define(SHARDS, 32).
+
+
+index_test_() ->
+    {
+        "Test fabric indexing module",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            with([
+                ?TDEF(register_index_works),
+                ?TDEF(single_update),
+                ?TDEF(multiple_updates),
+                ?TDEF(skip_db_if_no_ddocs),
+                ?TDEF(ignore_deleted_dbs),
+                ?TDEF(check_gen_server_messages)
+            ])
+        }
+    }.
+
+
+index_process_cleanup_test_() ->
+    {
+        "Test fabric process cleanup in indexing module",
+        {
+            foreach,
+            fun setup/0,
+            fun cleanup/1,
+            [
+                ?TDEF_FE(updater_processes_start),
+                ?TDEF_FE(updater_processes_stop),
+                ?TDEF_FE(indexing_can_be_disabled),
+                ?TDEF_FE(handle_indexer_blowing_up)
+            ]
+        }
+    }.
+
+
+setup() ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get_integer, fun
+        ("fabric", "index_updater_delay_msec", _) -> 200;
+        ("fabric", "index_updater_resolution_msec", _) -> 100;
+
+        (_, _, Default) -> Default
+    end),
+    meck:expect(config, get_boolean, fun
+        ("fabric", "index_updater_enabled", _) -> true;
+        (_, _, Default) -> Default
+    end),
+
+    Indices = application:get_env(fabric, indices, []),
+
+    Ctx = test_util:start_couch([fabric]),
+
+    % Db1 has a valid design doc, a deleted one and one with "autoupdate":false
+    {ok, Db1} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+    {_, _} = create_doc(Db1, <<"_design/doc1">>),
+
+    DDocId2 = <<"_design/doc2">>,
+    {DDocId2, {Pos, Rev}} = create_doc(Db1, DDocId2),
+    Delete2 = #doc{id = DDocId2, revs = {Pos, [Rev]}, deleted = true},
+    {ok, _} = fabric2_db:update_doc(Db1, Delete2),
+
+    NoAutoUpdate = {[{<<"autoupdate">>, false}]},
+    {_, _} = create_doc(Db1, <<"_design/doc3">>, NoAutoUpdate),
+
+    % Db2 doesn't have any desig documents
+    {ok, Db2} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+
+    #{db1 => Db1, db2 => Db2, ctx => Ctx, indices => Indices}.
+
+
+cleanup(#{db1 := Db1, db2 := Db2, ctx := Ctx, indices := Indices}) ->
+    catch fabric2_db:delete(fabric2_db:name(Db1), []),
+    catch fabric2_db:delete(fabric2_db:name(Db2), []),
+
+    test_util:stop_couch(Ctx),
+    application:set_env(fabric, indices, Indices),
+
+    meck:unload().
+
+
+register_index_works(_) ->
+    reset_callbacks(),
+
+    Mod1 = fabric2_test_callback1,
+    fabric2_index:register_index(Mod1),
+    Indices1 = application:get_env(fabric, indices, []),
+    ?assertEqual([Mod1], Indices1),
+
+    Mod2 = fabric2_test_callback2,
+    fabric2_index:register_index(Mod2),
+    Indices2 = application:get_env(fabric, indices, []),
+    ?assertEqual(lists:sort([Mod1, Mod2]), lists:sort(Indices2)).
+
+
+single_update(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback3,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    meck:wait(Mod, build_indices, 2, 2000),
+    ?assertEqual(1, meck:num_calls(Mod, build_indices, 2)).
+
+
+multiple_updates(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback4,
+    setup_callback(Mod),
+    create_docs(Db, 10),
+
+    % should be called at least once
+    meck:wait(Mod, build_indices, 2, 2000),
+
+    % Maybe called another time or two at most
+    timer:sleep(500),
+    ?assert(meck:num_calls(Mod, build_indices, 2) =< 3).
+
+
+skip_db_if_no_ddocs(#{db2 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback5,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    timer:sleep(500),
+    ?assertEqual(0, meck:num_calls(Mod, build_indices, 2)).
+
+
+ignore_deleted_dbs(#{}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback6,
+    setup_callback(Mod),
+    lists:foreach(fun(_) ->
+        RandomDbName = fabric2_util:uuid(),
+        fabric2_index:db_updated(RandomDbName)
+    end, lists:seq(1, 10000)),
+
+    test_util:wait(fun() ->
+        case table_sizes() =:= 0 of
+            true -> ok;
+            false -> wait
+        end
+    end, 5000).
+
+
+check_gen_server_messages(#{}) ->
+    CallExpect = {stop, {bad_call, foo}, {bad_call, foo}, baz},
+    CastExpect = {stop, {bad_cast, foo}, bar},
+    InfoExpect = {stop, {bad_info, foo}, bar},
+    ?assertEqual(CallExpect, fabric2_index:handle_call(foo, bar, baz)),
+    ?assertEqual(CastExpect, fabric2_index:handle_cast(foo, bar)),
+    ?assertEqual(InfoExpect, fabric2_index:handle_info(foo, bar)),
+    ?assertEqual(ok, fabric2_index:terminate(shutdown, nil)),
+    ?assertEqual({ok, nil}, fabric2_index:code_change(v0, nil, extra)).
+
+
+updater_processes_start(#{}) ->
+    Pid = whereis(fabric2_index),
+    ?assert(is_process_alive(Pid)),
+    lists:map(fun(N) ->
+        ?assertEqual(tid(N), ets:info(tid(N), name))
+    end, lists:seq(0, ?SHARDS - 1)).
+
+
+updater_processes_stop(#{}) ->
+    Refs = lists:map(fun(N) ->
+        Pid = ets:info(tid(N), owner),
+        ?assert(is_process_alive(Pid)),
+        monitor(process, Pid)
+    end, lists:seq(0, ?SHARDS - 1)),
+
+    application:stop(fabric),
 
 Review comment:
   Oh, subtle, you have it as a foreach on the second group. Might make a note of that here since its a subtle thing to miss and up way above.

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

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

Posted by GitBox <gi...@apache.org>.
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

[GitHub] [couchdb] nickva merged pull request #2637: Index auto-updater

Posted by GitBox <gi...@apache.org>.
nickva merged pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637
 
 
   

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

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

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389866455
 
 

 ##########
 File path: src/fabric/test/fabric2_index_tests.erl
 ##########
 @@ -0,0 +1,302 @@
+% 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_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+% Should match fabric2_index define
+-define(SHARDS, 32).
+
+
+index_test_() ->
+    {
+        "Test fabric indexing module",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            with([
+                ?TDEF(register_index_works),
+                ?TDEF(single_update),
+                ?TDEF(multiple_updates),
+                ?TDEF(skip_db_if_no_ddocs),
+                ?TDEF(ignore_deleted_dbs),
+                ?TDEF(check_gen_server_messages)
+            ])
+        }
+    }.
+
+
+index_process_cleanup_test_() ->
+    {
+        "Test fabric process cleanup in indexing module",
+        {
+            foreach,
+            fun setup/0,
+            fun cleanup/1,
+            [
+                ?TDEF_FE(updater_processes_start),
+                ?TDEF_FE(updater_processes_stop),
+                ?TDEF_FE(indexing_can_be_disabled),
+                ?TDEF_FE(handle_indexer_blowing_up)
+            ]
+        }
+    }.
+
+
+setup() ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get_integer, fun
+        ("fabric", "index_updater_delay_msec", _) -> 200;
+        ("fabric", "index_updater_resolution_msec", _) -> 100;
+
+        (_, _, Default) -> Default
+    end),
+    meck:expect(config, get_boolean, fun
+        ("fabric", "index_updater_enabled", _) -> true;
+        (_, _, Default) -> Default
+    end),
+
+    Indices = application:get_env(fabric, indices, []),
+
+    Ctx = test_util:start_couch([fabric]),
+
+    % Db1 has a valid design doc, a deleted one and one with "autoupdate":false
+    {ok, Db1} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+    {_, _} = create_doc(Db1, <<"_design/doc1">>),
+
+    DDocId2 = <<"_design/doc2">>,
+    {DDocId2, {Pos, Rev}} = create_doc(Db1, DDocId2),
+    Delete2 = #doc{id = DDocId2, revs = {Pos, [Rev]}, deleted = true},
+    {ok, _} = fabric2_db:update_doc(Db1, Delete2),
+
+    NoAutoUpdate = {[{<<"autoupdate">>, false}]},
+    {_, _} = create_doc(Db1, <<"_design/doc3">>, NoAutoUpdate),
+
+    % Db2 doesn't have any desig documents
+    {ok, Db2} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+
+    #{db1 => Db1, db2 => Db2, ctx => Ctx, indices => Indices}.
+
+
+cleanup(#{db1 := Db1, db2 := Db2, ctx := Ctx, indices := Indices}) ->
+    catch fabric2_db:delete(fabric2_db:name(Db1), []),
+    catch fabric2_db:delete(fabric2_db:name(Db2), []),
+
+    test_util:stop_couch(Ctx),
+    application:set_env(fabric, indices, Indices),
+
+    meck:unload().
+
+
+register_index_works(_) ->
+    reset_callbacks(),
+
+    Mod1 = fabric2_test_callback1,
+    fabric2_index:register_index(Mod1),
+    Indices1 = application:get_env(fabric, indices, []),
+    ?assertEqual([Mod1], Indices1),
+
+    Mod2 = fabric2_test_callback2,
+    fabric2_index:register_index(Mod2),
+    Indices2 = application:get_env(fabric, indices, []),
+    ?assertEqual(lists:sort([Mod1, Mod2]), lists:sort(Indices2)).
+
+
+single_update(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback3,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    meck:wait(Mod, build_indices, 2, 2000),
+    ?assertEqual(1, meck:num_calls(Mod, build_indices, 2)).
+
+
+multiple_updates(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback4,
+    setup_callback(Mod),
+    create_docs(Db, 10),
+
+    % should be called at least once
+    meck:wait(Mod, build_indices, 2, 2000),
+
+    % Maybe called another time or two at most
 
 Review comment:
   I'm not seeing why your assertion of `=< 3` is correct. Is it because of the jitter and frequency checks and the fact you're only waiting 500ms?

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

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

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2637: Index auto-updater
URL: https://github.com/apache/couchdb/pull/2637#discussion_r389885475
 
 

 ##########
 File path: src/fabric/test/fabric2_index_tests.erl
 ##########
 @@ -0,0 +1,302 @@
+% 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_tests).
+
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include("fabric2_test.hrl").
+
+
+% Should match fabric2_index define
+-define(SHARDS, 32).
+
+
+index_test_() ->
+    {
+        "Test fabric indexing module",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            with([
+                ?TDEF(register_index_works),
+                ?TDEF(single_update),
+                ?TDEF(multiple_updates),
+                ?TDEF(skip_db_if_no_ddocs),
+                ?TDEF(ignore_deleted_dbs),
+                ?TDEF(check_gen_server_messages)
+            ])
+        }
+    }.
+
+
+index_process_cleanup_test_() ->
+    {
+        "Test fabric process cleanup in indexing module",
+        {
+            foreach,
+            fun setup/0,
+            fun cleanup/1,
+            [
+                ?TDEF_FE(updater_processes_start),
+                ?TDEF_FE(updater_processes_stop),
+                ?TDEF_FE(indexing_can_be_disabled),
+                ?TDEF_FE(handle_indexer_blowing_up)
+            ]
+        }
+    }.
+
+
+setup() ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get_integer, fun
+        ("fabric", "index_updater_delay_msec", _) -> 200;
+        ("fabric", "index_updater_resolution_msec", _) -> 100;
+
+        (_, _, Default) -> Default
+    end),
+    meck:expect(config, get_boolean, fun
+        ("fabric", "index_updater_enabled", _) -> true;
+        (_, _, Default) -> Default
+    end),
+
+    Indices = application:get_env(fabric, indices, []),
+
+    Ctx = test_util:start_couch([fabric]),
+
+    % Db1 has a valid design doc, a deleted one and one with "autoupdate":false
+    {ok, Db1} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+    {_, _} = create_doc(Db1, <<"_design/doc1">>),
+
+    DDocId2 = <<"_design/doc2">>,
+    {DDocId2, {Pos, Rev}} = create_doc(Db1, DDocId2),
+    Delete2 = #doc{id = DDocId2, revs = {Pos, [Rev]}, deleted = true},
+    {ok, _} = fabric2_db:update_doc(Db1, Delete2),
+
+    NoAutoUpdate = {[{<<"autoupdate">>, false}]},
+    {_, _} = create_doc(Db1, <<"_design/doc3">>, NoAutoUpdate),
+
+    % Db2 doesn't have any desig documents
+    {ok, Db2} = fabric2_db:create(?tempdb(), [?ADMIN_CTX]),
+
+    #{db1 => Db1, db2 => Db2, ctx => Ctx, indices => Indices}.
+
+
+cleanup(#{db1 := Db1, db2 := Db2, ctx := Ctx, indices := Indices}) ->
+    catch fabric2_db:delete(fabric2_db:name(Db1), []),
+    catch fabric2_db:delete(fabric2_db:name(Db2), []),
+
+    test_util:stop_couch(Ctx),
+    application:set_env(fabric, indices, Indices),
+
+    meck:unload().
+
+
+register_index_works(_) ->
+    reset_callbacks(),
+
+    Mod1 = fabric2_test_callback1,
+    fabric2_index:register_index(Mod1),
+    Indices1 = application:get_env(fabric, indices, []),
+    ?assertEqual([Mod1], Indices1),
+
+    Mod2 = fabric2_test_callback2,
+    fabric2_index:register_index(Mod2),
+    Indices2 = application:get_env(fabric, indices, []),
+    ?assertEqual(lists:sort([Mod1, Mod2]), lists:sort(Indices2)).
+
+
+single_update(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback3,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    meck:wait(Mod, build_indices, 2, 2000),
+    ?assertEqual(1, meck:num_calls(Mod, build_indices, 2)).
+
+
+multiple_updates(#{db1 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback4,
+    setup_callback(Mod),
+    create_docs(Db, 10),
+
+    % should be called at least once
+    meck:wait(Mod, build_indices, 2, 2000),
+
+    % Maybe called another time or two at most
+    timer:sleep(500),
+    ?assert(meck:num_calls(Mod, build_indices, 2) =< 3).
+
+
+skip_db_if_no_ddocs(#{db2 := Db}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback5,
+    setup_callback(Mod),
+    create_doc(Db),
+
+    timer:sleep(500),
+    ?assertEqual(0, meck:num_calls(Mod, build_indices, 2)).
+
+
+ignore_deleted_dbs(#{}) ->
+    reset_callbacks(),
+
+    Mod = fabric2_test_callback6,
+    setup_callback(Mod),
+    lists:foreach(fun(_) ->
+        RandomDbName = fabric2_util:uuid(),
+        fabric2_index:db_updated(RandomDbName)
+    end, lists:seq(1, 10000)),
+
+    test_util:wait(fun() ->
+        case table_sizes() =:= 0 of
+            true -> ok;
+            false -> wait
+        end
+    end, 5000).
+
+
+check_gen_server_messages(#{}) ->
+    CallExpect = {stop, {bad_call, foo}, {bad_call, foo}, baz},
+    CastExpect = {stop, {bad_cast, foo}, bar},
+    InfoExpect = {stop, {bad_info, foo}, bar},
+    ?assertEqual(CallExpect, fabric2_index:handle_call(foo, bar, baz)),
+    ?assertEqual(CastExpect, fabric2_index:handle_cast(foo, bar)),
+    ?assertEqual(InfoExpect, fabric2_index:handle_info(foo, bar)),
+    ?assertEqual(ok, fabric2_index:terminate(shutdown, nil)),
+    ?assertEqual({ok, nil}, fabric2_index:code_change(v0, nil, extra)).
+
+
+updater_processes_start(#{}) ->
+    Pid = whereis(fabric2_index),
+    ?assert(is_process_alive(Pid)),
+    lists:map(fun(N) ->
+        ?assertEqual(tid(N), ets:info(tid(N), name))
+    end, lists:seq(0, ?SHARDS - 1)).
+
+
+updater_processes_stop(#{}) ->
+    Refs = lists:map(fun(N) ->
+        Pid = ets:info(tid(N), owner),
+        ?assert(is_process_alive(Pid)),
+        monitor(process, Pid)
+    end, lists:seq(0, ?SHARDS - 1)),
+
+    application:stop(fabric),
 
 Review comment:
   Yeah it's because a few tests are running in a foreach-type fixture since they kill stop the app and do other nasty things to the application and it was easier ensure they don't affect other tests.
   
   I'll add a comment in the test why we are not restarting the app there.

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