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/05/19 16:56:53 UTC

[GitHub] [couchdb] nickva opened a new pull request #2894: Add batching for _bulk_docs

nickva opened a new pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894


   WIP still need to add tests
   
    * Interactive (regular) _bulk_docs can be split into smaller transactions.
   
    * Non-interactive (replicated) _bulk_docs requests can be updated in batches
      with more than one document per transaction.
   
   


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



[GitHub] [couchdb] davisp commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428333576



##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),

Review comment:
       I wouldn't have expected you'd be able to exceed 5000 as a limit (with the exception of a single doc transaction).




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



[GitHub] [couchdb] nickva merged pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
nickva merged pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894


   


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



[GitHub] [couchdb] nickva commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428331419



##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),

Review comment:
       It would be 2 since [10, 10, 9000] is the first, then it would commit then another [10] would be the last.




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



[GitHub] [couchdb] davisp commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428335241



##########
File path: src/fabric/src/fabric2_db.erl
##########
@@ -1659,17 +1662,119 @@ update_doc_int(#{} = Db, #doc{} = Doc, Options) ->
     end.
 
 
-update_docs_interactive(Db, Docs0, Options) ->
-    Docs = tag_docs(Docs0),
-    Futures = get_winning_rev_futures(Db, Docs),
-    {Result, _} = lists:mapfoldl(fun(Doc, SeenIds) ->
-        try
-            update_docs_interactive(Db, Doc, Options, Futures, SeenIds)
-        catch throw:{?MODULE, Return} ->
-            {Return, SeenIds}
+batch_update_docs(Db, Docs, Options) ->
+    BAcc = #bacc{
+        db = Db,
+        docs = Docs,
+        batch_size = get_batch_size(Options),
+        options = Options,
+        rev_futures = #{},
+        seen = [],
+        seen_tx = [],
+        results = []
+    },
+    #bacc{results = Res} = batch_update_docs(BAcc),
+    lists:reverse(Res).
+
+
+batch_update_docs(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_docs(#bacc{db = Db} = BAcc) ->
+    #bacc{
+        db = Db,
+        docs = Docs,
+        options = Options
+    } = BAcc,
+
+    BAccTx2 = fabric2_fdb:transactional(Db, fun(TxDb) ->
+        BAccTx = BAcc#bacc{db = TxDb},
+        case is_replicated(Options) of
+            false ->
+                Tagged = tag_docs(Docs),
+                RevFutures = get_winning_rev_futures(TxDb, Tagged),
+                BAccTx1 = BAccTx#bacc{
+                    docs = Tagged,
+                    rev_futures = RevFutures
+                },
+                batch_update_interactive_tx(BAccTx1);
+            true ->
+                batch_update_replicated_tx(BAccTx)
         end
-    end, [], Docs),
-    Result.
+    end),
+
+    % Clean up after the transaction ends so we can recurse with a clean state
+    maps:map(fun(Tag, RangeFuture) when is_reference(Tag) ->
+        ok = erlfdb:cancel(RangeFuture, [flush])
+    end, BAccTx2#bacc.rev_futures),
+
+    BAcc1 = BAccTx2#bacc{
+        db = Db,
+        rev_futures = #{},
+        seen_tx = []
+    },
+
+    batch_update_docs(BAcc1).
+
+
+batch_update_interactive_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_interactive_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        rev_futures = RevFutures,
+        seen = Seen,
+        results = Results
+    } = BAcc,
+    {Res, Seen1} = try
+        update_docs_interactive(TxDb, Doc, Options, RevFutures, Seen)
+    catch throw:{?MODULE, Return} ->
+        {Return, Seen}
+    end,
+    BAcc1 = BAcc#bacc{
+        docs = Docs,
+        results = [Res | Results],
+        seen = Seen1
+    },
+    case fabric2_fdb:get_approximate_tx_size(TxDb) > MaxSize of
+        true -> BAcc1;
+        false -> batch_update_interactive_tx(BAcc1)
+    end.
+
+
+batch_update_replicated_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_replicated_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        seen_tx = SeenTx,

Review comment:
       Just clear in a different spot, I think? Move line 1714 to between 1701/1702. Similar to what you've already got for interactive since you have to reset some state there as well.




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



[GitHub] [couchdb] davisp commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428333754



##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),

Review comment:
       Not a big deal either way, to be clear. This is fine.




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



[GitHub] [couchdb] nickva commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428341696



##########
File path: src/fabric/src/fabric2_db.erl
##########
@@ -1659,17 +1662,119 @@ update_doc_int(#{} = Db, #doc{} = Doc, Options) ->
     end.
 
 
-update_docs_interactive(Db, Docs0, Options) ->
-    Docs = tag_docs(Docs0),
-    Futures = get_winning_rev_futures(Db, Docs),
-    {Result, _} = lists:mapfoldl(fun(Doc, SeenIds) ->
-        try
-            update_docs_interactive(Db, Doc, Options, Futures, SeenIds)
-        catch throw:{?MODULE, Return} ->
-            {Return, SeenIds}
+batch_update_docs(Db, Docs, Options) ->
+    BAcc = #bacc{
+        db = Db,
+        docs = Docs,
+        batch_size = get_batch_size(Options),
+        options = Options,
+        rev_futures = #{},
+        seen = [],
+        seen_tx = [],
+        results = []
+    },
+    #bacc{results = Res} = batch_update_docs(BAcc),
+    lists:reverse(Res).
+
+
+batch_update_docs(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_docs(#bacc{db = Db} = BAcc) ->
+    #bacc{
+        db = Db,
+        docs = Docs,
+        options = Options
+    } = BAcc,
+
+    BAccTx2 = fabric2_fdb:transactional(Db, fun(TxDb) ->
+        BAccTx = BAcc#bacc{db = TxDb},
+        case is_replicated(Options) of
+            false ->
+                Tagged = tag_docs(Docs),
+                RevFutures = get_winning_rev_futures(TxDb, Tagged),
+                BAccTx1 = BAccTx#bacc{
+                    docs = Tagged,
+                    rev_futures = RevFutures
+                },
+                batch_update_interactive_tx(BAccTx1);
+            true ->
+                batch_update_replicated_tx(BAccTx)
         end
-    end, [], Docs),
-    Result.
+    end),
+
+    % Clean up after the transaction ends so we can recurse with a clean state
+    maps:map(fun(Tag, RangeFuture) when is_reference(Tag) ->
+        ok = erlfdb:cancel(RangeFuture, [flush])
+    end, BAccTx2#bacc.rev_futures),
+
+    BAcc1 = BAccTx2#bacc{
+        db = Db,
+        rev_futures = #{},
+        seen_tx = []
+    },
+
+    batch_update_docs(BAcc1).
+
+
+batch_update_interactive_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_interactive_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        rev_futures = RevFutures,
+        seen = Seen,
+        results = Results
+    } = BAcc,
+    {Res, Seen1} = try
+        update_docs_interactive(TxDb, Doc, Options, RevFutures, Seen)
+    catch throw:{?MODULE, Return} ->
+        {Return, Seen}
+    end,
+    BAcc1 = BAcc#bacc{
+        docs = Docs,
+        results = [Res | Results],
+        seen = Seen1
+    },
+    case fabric2_fdb:get_approximate_tx_size(TxDb) > MaxSize of
+        true -> BAcc1;
+        false -> batch_update_interactive_tx(BAcc1)
+    end.
+
+
+batch_update_replicated_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_replicated_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        seen_tx = SeenTx,

Review comment:
       Good idea!




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



[GitHub] [couchdb] davisp commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428333885



##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_duplicate_ids_conflict(Db) ->
+    Doc = doc(),
+
+    Res = fabric2_db:update_docs(Db, [Doc, doc(), Doc]),
+    ?assertMatch({ok, [_, _, _]}, Res),
+
+    {ok, [Doc1Res, Doc2Res, Doc3Res]} = Res,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch(conflict, Doc3Res).
+
+
+update_docs_duplicate_ids_with_batches(Db) ->

Review comment:
       Aha, I read that different. So even less of an issue. Good call.




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



[GitHub] [couchdb] nickva commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428332798



##########
File path: src/fabric/src/fabric2_db.erl
##########
@@ -1659,17 +1662,119 @@ update_doc_int(#{} = Db, #doc{} = Doc, Options) ->
     end.
 
 
-update_docs_interactive(Db, Docs0, Options) ->
-    Docs = tag_docs(Docs0),
-    Futures = get_winning_rev_futures(Db, Docs),
-    {Result, _} = lists:mapfoldl(fun(Doc, SeenIds) ->
-        try
-            update_docs_interactive(Db, Doc, Options, Futures, SeenIds)
-        catch throw:{?MODULE, Return} ->
-            {Return, SeenIds}
+batch_update_docs(Db, Docs, Options) ->
+    BAcc = #bacc{
+        db = Db,
+        docs = Docs,
+        batch_size = get_batch_size(Options),
+        options = Options,
+        rev_futures = #{},
+        seen = [],
+        seen_tx = [],
+        results = []
+    },
+    #bacc{results = Res} = batch_update_docs(BAcc),
+    lists:reverse(Res).
+
+
+batch_update_docs(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_docs(#bacc{db = Db} = BAcc) ->
+    #bacc{
+        db = Db,
+        docs = Docs,
+        options = Options
+    } = BAcc,
+
+    BAccTx2 = fabric2_fdb:transactional(Db, fun(TxDb) ->
+        BAccTx = BAcc#bacc{db = TxDb},
+        case is_replicated(Options) of
+            false ->
+                Tagged = tag_docs(Docs),
+                RevFutures = get_winning_rev_futures(TxDb, Tagged),
+                BAccTx1 = BAccTx#bacc{
+                    docs = Tagged,
+                    rev_futures = RevFutures
+                },
+                batch_update_interactive_tx(BAccTx1);
+            true ->
+                batch_update_replicated_tx(BAccTx)
         end
-    end, [], Docs),
-    Result.
+    end),
+
+    % Clean up after the transaction ends so we can recurse with a clean state
+    maps:map(fun(Tag, RangeFuture) when is_reference(Tag) ->
+        ok = erlfdb:cancel(RangeFuture, [flush])
+    end, BAccTx2#bacc.rev_futures),
+
+    BAcc1 = BAccTx2#bacc{
+        db = Db,
+        rev_futures = #{},
+        seen_tx = []
+    },
+
+    batch_update_docs(BAcc1).
+
+
+batch_update_interactive_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_interactive_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        rev_futures = RevFutures,
+        seen = Seen,
+        results = Results
+    } = BAcc,
+    {Res, Seen1} = try
+        update_docs_interactive(TxDb, Doc, Options, RevFutures, Seen)
+    catch throw:{?MODULE, Return} ->
+        {Return, Seen}
+    end,
+    BAcc1 = BAcc#bacc{
+        docs = Docs,
+        results = [Res | Results],
+        seen = Seen1
+    },
+    case fabric2_fdb:get_approximate_tx_size(TxDb) > MaxSize of
+        true -> BAcc1;
+        false -> batch_update_interactive_tx(BAcc1)
+    end.
+
+
+batch_update_replicated_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_replicated_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        seen_tx = SeenTx,

Review comment:
       The idea is that for interactive bulk docs we keep the list of ids across all transactions but in case of replicated ones we care only about the having seen the same document in the same transaction.
   
   Could probably re-use the field with a `case` statement and `is_replicated/1` check




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



[GitHub] [couchdb] davisp commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
davisp commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428327874



##########
File path: src/fabric/src/fabric2_db.erl
##########
@@ -1659,17 +1662,119 @@ update_doc_int(#{} = Db, #doc{} = Doc, Options) ->
     end.
 
 
-update_docs_interactive(Db, Docs0, Options) ->
-    Docs = tag_docs(Docs0),
-    Futures = get_winning_rev_futures(Db, Docs),
-    {Result, _} = lists:mapfoldl(fun(Doc, SeenIds) ->
-        try
-            update_docs_interactive(Db, Doc, Options, Futures, SeenIds)
-        catch throw:{?MODULE, Return} ->
-            {Return, SeenIds}
+batch_update_docs(Db, Docs, Options) ->
+    BAcc = #bacc{
+        db = Db,
+        docs = Docs,
+        batch_size = get_batch_size(Options),
+        options = Options,
+        rev_futures = #{},
+        seen = [],
+        seen_tx = [],
+        results = []
+    },
+    #bacc{results = Res} = batch_update_docs(BAcc),
+    lists:reverse(Res).
+
+
+batch_update_docs(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_docs(#bacc{db = Db} = BAcc) ->
+    #bacc{
+        db = Db,
+        docs = Docs,
+        options = Options
+    } = BAcc,
+
+    BAccTx2 = fabric2_fdb:transactional(Db, fun(TxDb) ->
+        BAccTx = BAcc#bacc{db = TxDb},
+        case is_replicated(Options) of
+            false ->
+                Tagged = tag_docs(Docs),
+                RevFutures = get_winning_rev_futures(TxDb, Tagged),
+                BAccTx1 = BAccTx#bacc{
+                    docs = Tagged,
+                    rev_futures = RevFutures
+                },
+                batch_update_interactive_tx(BAccTx1);
+            true ->
+                batch_update_replicated_tx(BAccTx)
         end
-    end, [], Docs),
-    Result.
+    end),
+
+    % Clean up after the transaction ends so we can recurse with a clean state
+    maps:map(fun(Tag, RangeFuture) when is_reference(Tag) ->
+        ok = erlfdb:cancel(RangeFuture, [flush])
+    end, BAccTx2#bacc.rev_futures),
+
+    BAcc1 = BAccTx2#bacc{
+        db = Db,
+        rev_futures = #{},
+        seen_tx = []
+    },
+
+    batch_update_docs(BAcc1).
+
+
+batch_update_interactive_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_interactive_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        rev_futures = RevFutures,
+        seen = Seen,
+        results = Results
+    } = BAcc,
+    {Res, Seen1} = try
+        update_docs_interactive(TxDb, Doc, Options, RevFutures, Seen)
+    catch throw:{?MODULE, Return} ->
+        {Return, Seen}
+    end,
+    BAcc1 = BAcc#bacc{
+        docs = Docs,
+        results = [Res | Results],
+        seen = Seen1
+    },
+    case fabric2_fdb:get_approximate_tx_size(TxDb) > MaxSize of
+        true -> BAcc1;
+        false -> batch_update_interactive_tx(BAcc1)
+    end.
+
+
+batch_update_replicated_tx(#bacc{docs = []} = BAcc) ->
+    BAcc;
+
+batch_update_replicated_tx(#bacc{} = BAcc) ->
+    #bacc{
+        db = TxDb,
+        docs = [Doc | Docs],
+        options = Options,
+        batch_size = MaxSize,
+        seen_tx = SeenTx,

Review comment:
       Seems a bit silly to have both `seen` and `seen_tx` that do basically the same thing.

##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),

Review comment:
       Shouldn't this be three transactions? I'd think it'd be `[10, 10], [9000], [1]`

##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_duplicate_ids_conflict(Db) ->
+    Doc = doc(),
+
+    Res = fabric2_db:update_docs(Db, [Doc, doc(), Doc]),
+    ?assertMatch({ok, [_, _, _]}, Res),
+
+    {ok, [Doc1Res, Doc2Res, Doc3Res]} = Res,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch(conflict, Doc3Res).
+
+
+update_docs_duplicate_ids_with_batches(Db) ->

Review comment:
       Oooph. This is kinda gross but I don't think there's any way around it. Where by "this" I mean, "depending on ordering you may or may not get a conflict". Ah, though the likelihood an interactive edit has properly created two docs in a _bulk_docs call is vanishingly small so the second one will likely be a conflict regardless.




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



[GitHub] [couchdb] nickva commented on a change in pull request #2894: Add batching for _bulk_docs

Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2894:
URL: https://github.com/apache/couchdb/pull/2894#discussion_r428332005



##########
File path: src/fabric/test/fabric2_update_docs_tests.erl
##########
@@ -0,0 +1,222 @@
+% 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_update_docs_tests).
+
+
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include("fabric2_test.hrl").
+
+
+update_docs_test_() ->
+    {
+        "Test update_docs",
+        {
+            setup,
+            fun setup_all/0,
+            fun teardown_all/1,
+            {
+                foreach,
+                fun setup/0,
+                fun cleanup/1,
+                [
+                    ?TDEF_FE(update_docs),
+                    ?TDEF_FE(update_docs_replicated),
+                    ?TDEF_FE(update_docs_batches),
+                    ?TDEF_FE(update_docs_replicated_batches),
+                    ?TDEF_FE(update_docs_duplicate_ids_conflict),
+                    ?TDEF_FE(update_docs_duplicate_ids_with_batches),
+                    ?TDEF_FE(update_docs_replicate_batches_duplicate_id)
+                ]
+            }
+        }
+    }.
+
+
+setup_all() ->
+    test_util:start_couch([fabric]).
+
+
+teardown_all(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    meck:new(erlfdb, [passthrough]),
+    Db.
+
+
+cleanup(#{} = Db) ->
+    meck:unload(),
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+update_docs(Db) ->
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [])),
+
+    Doc1 = doc(),
+    Res1 = fabric2_db:update_docs(Db, [Doc1]),
+    ?assertMatch({ok, [_]}, Res1),
+    {ok, [Doc1Res]} = Res1,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    {ok, {1, Rev1}} = Doc1Res,
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1#doc{revs = {1, [Rev1]}}, Doc1Open),
+
+    Doc2 = doc(),
+    Doc3 = doc(),
+    Res2 = fabric2_db:update_docs(Db, [Doc2, Doc3]),
+    ?assertMatch({ok, [_, _]}, Res2),
+    {ok, [Doc2Res, Doc3Res]} = Res2,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc3Res).
+
+
+update_docs_replicated(Db) ->
+    Opts = [replicated_changes],
+
+    ?assertEqual({ok, []}, fabric2_db:update_docs(Db, [], Opts)),
+
+    Doc1 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc1], Opts)),
+    {ok, Doc1Open} = fabric2_db:open_doc(Db, Doc1#doc.id),
+    ?assertEqual(Doc1, Doc1Open),
+
+    Doc2 = doc(10, {1, [rev()]}),
+    Doc3 = doc(10, {1, [rev()]}),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, [Doc2, Doc3], Opts)),
+    {ok, Doc2Open} = fabric2_db:open_doc(Db, Doc2#doc.id),
+    ?assertEqual(Doc2, Doc2Open),
+    {ok, Doc3Open} = fabric2_db:open_doc(Db, Doc3#doc.id),
+    ?assertEqual(Doc3, Doc3Open).
+
+
+update_docs_batches(Db) ->
+    Opts = [{batch_size, 5000}],
+
+    Docs1 = [doc(9000), doc(9000)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(10), doc(10), doc(9000), doc(10)],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, [_ | _]}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertMatch({ok, #doc{}}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_replicated_batches(Db) ->
+    Opts = [{batch_size, 5000}, replicated_changes],
+
+    Docs1 = [doc(Size, {1, [rev()]}) || Size <- [9000, 9000]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs1, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs1),
+
+    Docs2 = [doc(Size, {1, [rev()]}) || Size <- [10, 10, 9000, 10]],
+
+    meck:reset(erlfdb),
+    ?assertMatch({ok, []}, fabric2_db:update_docs(Db, Docs2, Opts)),
+    ?assertEqual(2, meck:num_calls(erlfdb, transactional, 2)),
+
+    lists:foreach(fun(#doc{} = Doc) ->
+        ?assertEqual({ok, Doc}, fabric2_db:open_doc(Db, Doc#doc.id))
+    end, Docs2).
+
+
+update_docs_duplicate_ids_conflict(Db) ->
+    Doc = doc(),
+
+    Res = fabric2_db:update_docs(Db, [Doc, doc(), Doc]),
+    ?assertMatch({ok, [_, _, _]}, Res),
+
+    {ok, [Doc1Res, Doc2Res, Doc3Res]} = Res,
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc1Res),
+    ?assertMatch({ok, {1, <<_/binary>>}}, Doc2Res),
+    ?assertMatch(conflict, Doc3Res).
+
+
+update_docs_duplicate_ids_with_batches(Db) ->

Review comment:
       The main thing we are checking here is that we haven't lost or reset the set of `Seen` ids when if we switch to another transaction




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