You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/03/03 16:30:46 UTC

[couchdb] branch prototype/fdb-layer-couch-views-size-tests created (now 685481f)

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a change to branch prototype/fdb-layer-couch-views-size-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git.


      at 685481f  WIP: generating test cases

This branch includes the following new commits:

     new de409f2  Refactor view index layout
     new b8742a7  WIP: Test generation
     new 685481f  WIP: generating test cases

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[couchdb] 01/03: Refactor view index layout

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-views-size-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit de409f2a541be75482d24d5c1c3d983c952745b9
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Mon Mar 2 13:39:00 2020 -0600

    Refactor view index layout
    
    This moves index meta information to its own key space so that we can
    scan all existing indexes efficiently.
---
 src/couch_views/include/couch_views.hrl | 16 ++++++++++------
 src/couch_views/src/couch_views_fdb.erl | 23 +++++++++++++++--------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/src/couch_views/include/couch_views.hrl b/src/couch_views/include/couch_views.hrl
index 642431d..c40bb02 100644
--- a/src/couch_views/include/couch_views.hrl
+++ b/src/couch_views/include/couch_views.hrl
@@ -10,14 +10,18 @@
 % License for the specific language governing permissions and limitations under
 % the License.
 
-% indexing
+% Index info/data subspaces
+-define(VIEW_INFO, 0).
+-define(VIEW_DATA, 1).
+
+% Index info keys
 -define(VIEW_UPDATE_SEQ, 0).
--define(VIEW_ID_INFO, 1).
--define(VIEW_ID_RANGE, 2).
--define(VIEW_MAP_RANGE, 3).
+-define(VIEW_ROW_COUNT, 1).
+-define(VIEW_KV_SIZE, 2).
 
--define(VIEW_ROW_COUNT, 0).
--define(VIEW_KV_SIZE, 1).
+% Data keys
+-define(VIEW_ID_RANGE, 0).
+-define(VIEW_MAP_RANGE, 1).
 
 % jobs api
 -define(INDEX_JOB_TYPE, <<"views">>).
diff --git a/src/couch_views/src/couch_views_fdb.erl b/src/couch_views/src/couch_views_fdb.erl
index 5edaa3a..9ac80a9 100644
--- a/src/couch_views/src/couch_views_fdb.erl
+++ b/src/couch_views/src/couch_views_fdb.erl
@@ -251,7 +251,7 @@ get_view_keys(TxDb, Sig, DocId) ->
     } = TxDb,
     {Start, End} = id_idx_range(DbPrefix, Sig, DocId),
     lists:map(fun({K, V}) ->
-        {?DB_VIEWS, Sig, ?VIEW_ID_RANGE, DocId, ViewId} =
+        {?DB_VIEWS, ?VIEW_DATA, Sig, ?VIEW_ID_RANGE, DocId, ViewId} =
                 erlfdb_tuple:unpack(K, DbPrefix),
         [TotalKeys, TotalSize, UniqueKeys] = couch_views_encoding:decode(V),
         {ViewId, TotalKeys, TotalSize, UniqueKeys}
@@ -285,17 +285,17 @@ update_kv_size(TxDb, Sig, ViewId, Increment) ->
 
 
 seq_key(DbPrefix, Sig) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_UPDATE_SEQ},
+    Key = {?DB_VIEWS, ?VIEW_INFO, ?VIEW_UPDATE_SEQ, Sig},
     erlfdb_tuple:pack(Key, DbPrefix).
 
 
 row_count_key(DbPrefix, Sig, ViewId) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_ID_INFO, ViewId, ?VIEW_ROW_COUNT},
+    Key = {?DB_VIEWS, ?VIEW_INFO, ?VIEW_ROW_COUNT, Sig, ViewId},
     erlfdb_tuple:pack(Key, DbPrefix).
 
 
 kv_size_key(DbPrefix, Sig, ViewId) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_ID_INFO, ViewId, ?VIEW_KV_SIZE},
+    Key = {?DB_VIEWS, ?VIEW_INFO, ?VIEW_KV_SIZE, Sig, ViewId},
     erlfdb_tuple:pack(Key, DbPrefix).
 
 
@@ -305,17 +305,17 @@ db_kv_size_key(DbPrefix) ->
 
 
 id_idx_key(DbPrefix, Sig, DocId, ViewId) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_ID_RANGE, DocId, ViewId},
+    Key = {?DB_VIEWS, ?VIEW_DATA, Sig, ?VIEW_ID_RANGE, DocId, ViewId},
     erlfdb_tuple:pack(Key, DbPrefix).
 
 
 id_idx_range(DbPrefix, Sig, DocId) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_ID_RANGE, DocId},
+    Key = {?DB_VIEWS, ?VIEW_DATA, Sig, ?VIEW_ID_RANGE, DocId},
     erlfdb_tuple:range(Key, DbPrefix).
 
 
 map_idx_prefix(DbPrefix, Sig, ViewId) ->
-    Key = {?DB_VIEWS, Sig, ?VIEW_MAP_RANGE, ViewId},
+    Key = {?DB_VIEWS, ?VIEW_DATA, Sig, ?VIEW_MAP_RANGE, ViewId},
     erlfdb_tuple:pack(Key, DbPrefix).
 
 
@@ -326,7 +326,14 @@ map_idx_key(MapIdxPrefix, MapKey, DupeId) ->
 
 map_idx_range(DbPrefix, Sig, ViewId, MapKey, DocId) ->
     Encoded = couch_views_encoding:encode(MapKey, key),
-    Key = {?DB_VIEWS, Sig, ?VIEW_MAP_RANGE, ViewId, {Encoded, DocId}},
+    Key = {
+        ?DB_VIEWS,
+        ?VIEW_DATA,
+        Sig,
+        ?VIEW_MAP_RANGE,
+        ViewId,
+        {Encoded, DocId}
+    },
     erlfdb_tuple:range(Key, DbPrefix).
 
 


[couchdb] 02/03: WIP: Test generation

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-views-size-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit b8742a7332511ad26aa33a305a798bed6ce71e82
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Mon Mar 2 16:53:18 2020 -0600

    WIP: Test generation
---
 src/couch_views/test/couch_views_size_test.erl | 163 +++++++++++++++++++++++++
 1 file changed, 163 insertions(+)

diff --git a/src/couch_views/test/couch_views_size_test.erl b/src/couch_views/test/couch_views_size_test.erl
new file mode 100644
index 0000000..888a7ea
--- /dev/null
+++ b/src/couch_views/test/couch_views_size_test.erl
@@ -0,0 +1,163 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_views_size_test).
+
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+
+% N.B., we should move to couch_ejson_size instead
+% of erlang:external_size
+%
+% to calculate view size:
+% total = 0
+% for (fdb_k, fdb_v) in VIEW_MAP_RANGE:
+%   {EncUserKey, EncUserval} = erlfdb_tuple:unpack(fdb_v),
+%   UserKey = couch_views_encoding:decode(EncUserKey),
+%   UserVal = couch_views_encoding:decode(EncUserVal),
+%   total += erlang:external_size(UserKey),
+%   total += erlang:external_size(UserVal)
+%
+% Our goal in checking the size calculations is that we cover
+% as much of the possible key mutation space as possible while
+% not relying on fuzzing out the edge cases. Conceptually we have
+% two sets of keys E and U. E is keys as currently exist in the
+% view, and U is the new set of keys corresponding to an update.
+%
+% Both sets E and U have the same possible set of state variables:
+%
+% 1. N unique keys, where 0 =< N =< infinity
+% 2. D keys with duplicates, where 0 =< D =< N,
+% 3. R repeats for each member of D, for 2 =< R =< infinity
+%
+% Given two sets S1 and S2, we then have a set of transition variables:
+%
+% 1. deltaN - shared unique keys, where 0 =< deltaN =< N
+% 2. deltaD - shared duplicates, where 0 =< deltaD =< N
+% 3. deltaR - shared repeats for each D, where 2 =< deltaR =< infinity
+%
+% To search our state transition space, we can create two functions to
+% first define our start and end states, and for each transition we have
+% a function that defines the shared overlap between states.
+%
+% Given a list of transitions are checks then become simple in that
+% we can iterate over each transition checking that our index is valid
+% after each one. Index validation will purely look at the existing
+% state of the index in fdb and validate correctness.
+
+
+-define(N_DOMAIN, [0, 1, 2, 5]).
+-define(D_DOMAIN, [0, 1, 2, 5]).
+-define(R_DOMAIN, [2, 4]).
+
+-define(DELTA_N_DOMAIN, [0, 1, 2, 5]).
+-define(DELTA_D_DOMAIN, [0, 1, 2, 5]).
+-define(DELTA_R_DOMAIN, [1, 2, 4]).
+
+
+generate_sets() ->
+    lists:foldl(fun(N, NAcc) ->
+        lists:foldl(fun(D, DAcc) ->
+            if
+                D > N ->
+                    DAcc;
+                D == 0 ->
+                    [{N, 0, 0} | DAcc];
+                D > 0 ->
+                    lists:foldl(fun(R, RAcc) ->
+                        [{N, D, R} | RAcc]
+                    end, DAcc, ?R_DOMAIN)
+            end
+        end, NAcc, ?D_DOMAIN)
+    end, [], ?N_DOMAIN).
+
+
+-define(I_HEART_EUNIT(Tests), [{with, [T]} || T <- Tests]).
+
+
+size_test_() ->
+    {
+        "Test view size tracking",
+        {
+            setup,
+            fun setup/0,
+            fun cleanup/1,
+            {
+                foreach,
+                fun foreach_setup/0,
+                fun foreach_teardown/1,
+                ?I_HEART_EUNIT([
+                    fun single_view/1
+                ])
+            }
+        }
+    }.
+
+
+setup() ->
+    Ctx = test_util:start_couch([
+            fabric,
+            couch_jobs,
+            couch_js,
+            couch_views
+        ]),
+    Ctx.
+
+
+cleanup(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+
+foreach_setup() ->
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    Db.
+
+
+foreach_teardown(Db) ->
+    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+
+
+single_view(_Db) ->
+    Sets = generate_sets(),
+    Pairs = [{Set1, Set2} || Set1 <- Sets, Set2 <- Sets],
+    Transitions = lists:flatmap(fun({{N1, D1, R1} = S1, {N2, D2, R2} = S2}) ->
+        lists:foldl(fun(DeltaN, DNAcc) ->
+            case DeltaN > min(N1, N2) of
+                true ->
+                    DNAcc;
+                false when DeltaN == 0 ->
+                    [{S1, S2, {0, 0, 0}}];
+                false ->
+                    lists:foldl(fun(DeltaD, DDAcc) ->
+                        case DeltaD > min(D1, D2) orelse DeltaD > DeltaN of
+                            true ->
+                                DDAcc;
+                            false when DeltaD == 0 ->
+                                [{S1, S2, {DeltaN, 0, 0}} | DDAcc];
+                            false ->
+                                lists:foldl(fun(DeltaR, DRAcc) ->
+                                    case DeltaR > min(R1, R2) of
+                                        true ->
+                                            DRAcc;
+                                        false ->
+                                            T = {DeltaN, DeltaD, DeltaR},
+                                            [{S1, S2, T} | DRAcc]
+                                    end
+                                end, DDAcc, ?DELTA_R_DOMAIN)
+                        end
+                    end, DNAcc, ?DELTA_D_DOMAIN)
+            end
+        end, [], ?DELTA_N_DOMAIN)
+    end, Pairs),
+      io:format(standard_error, "~n~n~p~n~n", [lists:sort(Transitions)]).


[couchdb] 03/03: WIP: generating test cases

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch prototype/fdb-layer-couch-views-size-tests
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 685481f5381a621479598f2e3288258fdf25996e
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Tue Mar 3 10:30:25 2020 -0600

    WIP: generating test cases
---
 src/couch_views/test/couch_views_size_test.erl | 243 ++++++++++++++++++++-----
 1 file changed, 195 insertions(+), 48 deletions(-)

diff --git a/src/couch_views/test/couch_views_size_test.erl b/src/couch_views/test/couch_views_size_test.erl
index 888a7ea..16a6708 100644
--- a/src/couch_views/test/couch_views_size_test.erl
+++ b/src/couch_views/test/couch_views_size_test.erl
@@ -83,24 +83,48 @@ generate_sets() ->
     end, [], ?N_DOMAIN).
 
 
--define(I_HEART_EUNIT(Tests), [{with, [T]} || T <- Tests]).
+generate_transitions() ->
+    Sets = generate_sets(),
+    Pairs = [{Set1, Set2} || Set1 <- Sets, Set2 <- Sets],
+    lists:flatmap(fun({{N1, D1, R1} = S1, {N2, D2, R2} = S2}) ->
+        lists:foldl(fun(DeltaN, DNAcc) ->
+            case DeltaN > min(N1, N2) of
+                true ->
+                    DNAcc;
+                false when DeltaN == 0 ->
+                    [{S1, S2, {0, 0, 0}}];
+                false ->
+                    lists:foldl(fun(DeltaD, DDAcc) ->
+                        case DeltaD > min(D1, D2) orelse DeltaD > DeltaN of
+                            true ->
+                                DDAcc;
+                            false when DeltaD == 0 ->
+                                [{S1, S2, {DeltaN, 0, 0}} | DDAcc];
+                            false ->
+                                lists:foldl(fun(DeltaR, DRAcc) ->
+                                    case DeltaR > min(R1, R2) of
+                                        true ->
+                                            DRAcc;
+                                        false ->
+                                            T = {DeltaN, DeltaD, DeltaR},
+                                            [{S1, S2, T} | DRAcc]
+                                    end
+                                end, DDAcc, ?DELTA_R_DOMAIN)
+                        end
+                    end, DNAcc, ?DELTA_D_DOMAIN)
+            end
+        end, [], ?DELTA_N_DOMAIN)
+    end, Pairs).
 
 
-size_test_() ->
+row_transition_test_() ->
     {
         "Test view size tracking",
         {
             setup,
             fun setup/0,
             fun cleanup/1,
-            {
-                foreach,
-                fun foreach_setup/0,
-                fun foreach_teardown/1,
-                ?I_HEART_EUNIT([
-                    fun single_view/1
-                ])
-            }
+            fun create_transition_tests/1
         }
     }.
 
@@ -112,52 +136,175 @@ setup() ->
             couch_js,
             couch_views
         ]),
-    Ctx.
+    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
+    {Ctx, Db}.
 
 
-cleanup(Ctx) ->
+cleanup({Ctx, Db}) ->
+    ok = fabric2_db:delete(fabric2_db:name(Db), []),
     test_util:stop_couch(Ctx).
 
 
-foreach_setup() ->
-    {ok, Db} = fabric2_db:create(?tempdb(), [{user_ctx, ?ADMIN_USER}]),
-    Db.
+create_transition_tests({_Ctx, Db}) ->
+    lists:map(fun(T) ->
+        Name = lists:flatten(io_lib:format("~w", [T])),
+        {Name, fun() -> check_transition(Db, T) end}
+    end, lists:sort(generate_transitions())).
 
 
-foreach_teardown(Db) ->
-    ok = fabric2_db:delete(fabric2_db:name(Db), []).
+check_transition(Db, {Set1, Set2, Transition}) ->
+    InitKVs = init_set(Set1, [a, b, c, d, e]),
+    CommonKVs = reduce_set(Transition, InitKVs),
+    FinalKVs = fill_set(Set2, CommonKVs, [v, w, x, y, z]),
+    {InitJSONKVs, Bindings} = unlabel(InitKVs, #{}),
+    {FinalJSONKVs, _} = unlabel(FinalKVs, Bindings),
 
+    Sig = couch_uuids:random(),
+    DocId = couch_uuids:random(),
 
-single_view(_Db) ->
-    Sets = generate_sets(),
-    Pairs = [{Set1, Set2} || Set1 <- Sets, Set2 <- Sets],
-    Transitions = lists:flatmap(fun({{N1, D1, R1} = S1, {N2, D2, R2} = S2}) ->
-        lists:foldl(fun(DeltaN, DNAcc) ->
-            case DeltaN > min(N1, N2) of
+    fabric2_fdb:transactional(Db, fun(TxDb) ->
+        Doc = make_doc(DocId, InitJSONKVs),
+        couch_views_indexer:write_doc(TxDb, Sig, Doc, [1])
+    end),
+
+    fabric2_fdb:transactional(Db, fun(TxDb) ->
+        Doc = make_doc(DocId, FinalJSONKVs),
+        couch_views_indexer:write_doc(TxDb, Sig, Doc, [1])
+    end),
+
+    validate_index(Db, Sig).
+
+
+make_doc(DocId, []) ->
+    case rand:uniform() < 0.5 of
+        true ->
+            #{
+                id => DocId,
+                deleted => true,
+                results => []
+            };
+        false ->
+            #{
+                id => DocId,
+                deleted => false,
+                results => []
+            }
+    end;
+make_doc(DocId, Results) ->
+    #{
+        id => DocId,
+        deleted => false,
+        results => Results
+    }.
+
+
+validate_index(_Db, _Sig) ->
+    ok.
+
+
+init_set({N, D, R}, Labels) ->
+    {Dupes, RestLabels} = fill_keys(D, Labels, []),
+    {Unique, _} = fill_keys(N - D, RestLabels, []),
+    % Sanity assertions
+    N = length(Unique) + length(Dupes),
+    D = length(Dupes),
+    {Unique, [{Key, R} || Key <- Dupes]}.
+
+
+reduce_set({DeltaN, DeltaD, DeltaR}, {Unique, Dupes}) ->
+    NewDupes = lists:sublist(Dupes, DeltaD),
+    NewUnique = lists:sublist(Unique, DeltaN),
+    {NewUnique, [{Key, DeltaR} || {Key, _} <- NewDupes]}.
+
+
+fill_set({N, D, R}, {Unique, Dupes}, Labels) ->
+    AddDupes = D - length(Dupes),
+    {NewDupes, RestLabels} = fill_keys(AddDupes, Labels, Dupes),
+
+    AddUnique = N - length(Unique) - length(NewDupes),
+    {NewUnique, _} = fill_keys(AddUnique, RestLabels, Unique),
+    % Sanity assertions
+    N = length(NewUnique) + length(NewDupes),
+    D = length(NewDupes),
+    {NewUnique, lists:map(fun(Dupe) ->
+        case Dupe of
+            {_, _} -> Dupe;
+            A when is_atom(A) -> {A, R}
+        end
+    end, NewDupes)}.
+
+
+fill_keys(0, Labels, Acc) ->
+    {Acc, Labels};
+fill_keys(Count, [Label | RestLabels], Acc) when Count > 0 ->
+    fill_keys(Count - 1, RestLabels, [Label | Acc]).
+
+
+unlabel({Unique, Dupes}, Bindings) ->
+    lists:foldl(fun(Item, {KVAcc, BindingsAcc}) ->
+        {KVs, NewBindingsAcc} = unlabel_item(Item, BindingsAcc),
+        {KVs ++ KVAcc, NewBindingsAcc}
+    end, {[], Bindings}, Unique ++ Dupes).
+
+
+unlabel_item(Label, Bindings) when is_atom(Label) ->
+    NewBindings = maybe_bind(Label, Bindings),
+    KV = maps:get(Label, Bindings),
+    {[KV], NewBindings};
+unlabel_item({Label, Count}, Bindings) when is_atom(Label), is_integer(Count) ->
+    NewBindings = maybe_bind(Label, Bindings),
+    {K, _} = KV = maps:get(Label, Bindings),
+    ToAdd = lists:map(fun(_) ->
+        {K, gen_value()}
+    end,  lists:seq(1, Count - 1)),
+    {[KV | ToAdd], NewBindings}.
+
+
+maybe_bind(Label, Bindings) ->
+    case maps:is_key(Label, Bindings) of
+        true ->
+            case rand:uniform() < 0.5 of
                 true ->
-                    DNAcc;
-                false when DeltaN == 0 ->
-                    [{S1, S2, {0, 0, 0}}];
+                    rebind(Label, Bindings);
                 false ->
-                    lists:foldl(fun(DeltaD, DDAcc) ->
-                        case DeltaD > min(D1, D2) orelse DeltaD > DeltaN of
-                            true ->
-                                DDAcc;
-                            false when DeltaD == 0 ->
-                                [{S1, S2, {DeltaN, 0, 0}} | DDAcc];
-                            false ->
-                                lists:foldl(fun(DeltaR, DRAcc) ->
-                                    case DeltaR > min(R1, R2) of
-                                        true ->
-                                            DRAcc;
-                                        false ->
-                                            T = {DeltaN, DeltaD, DeltaR},
-                                            [{S1, S2, T} | DRAcc]
-                                    end
-                                end, DDAcc, ?DELTA_R_DOMAIN)
-                        end
-                    end, DNAcc, ?DELTA_D_DOMAIN)
-            end
-        end, [], ?DELTA_N_DOMAIN)
-    end, Pairs),
-      io:format(standard_error, "~n~n~p~n~n", [lists:sort(Transitions)]).
+                    Bindings
+            end;
+        false ->
+            bind(Label, Bindings)
+    end.
+
+
+bind(Label, Bindings) ->
+    maps:put(Label, {gen_key(), gen_value()}, Bindings).
+
+
+rebind(Label, Bindings) ->
+    {Key, _} = maps:get(Label, Bindings),
+    maps:put(Label, {Key, gen_value()}, Bindings).
+
+
+gen_key() ->
+    Unique = couch_uuids:random(),
+    case rand:uniform() of
+        N when N < 0.2 ->
+            [Unique, true, rand:uniform()];
+        N when N < 0.4 ->
+            {[{Unique, true}, {<<"foo">>, [<<"bar">>, null, 1, {[]}]}]};
+        _ ->
+            Unique
+    end.
+
+
+gen_value() ->
+    case rand:uniform() of
+        N when N < 0.2 ->
+            [false, rand:uniform(), {[]}];
+        N when N < 0.4 ->
+            {[{<<"a">>, 1}, {<<"b">>, 2}]};
+        N when N < 0.6 ->
+            rand:uniform(100);
+        N when N < 0.8 ->
+            rand:uniform();
+        _ ->
+            1
+    end.