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 2021/10/24 05:26:25 UTC

[GitHub] [couchdb] jaydoane commented on a change in pull request #3796: Move custodian VDU to a BDU and fix _all_dbs off-by-one limit bug

jaydoane commented on a change in pull request #3796:
URL: https://github.com/apache/couchdb/pull/3796#discussion_r735064868



##########
File path: src/mem3/src/mem3_bdu.erl
##########
@@ -0,0 +1,106 @@
+% 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(mem3_bdu).
+
+
+-export([
+    before_doc_update/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-spec before_doc_update(#doc{}, Db::any(), couch_db:update_type()) -> #doc{}.
+before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db, _UpdateType) ->
+    % Skip design docs
+    Doc;
+
+before_doc_update(#doc{deleted = true} = Doc, _Db, _UpdateType) ->
+    % Skip deleted
+    Doc;
+
+before_doc_update(#doc{} = Doc, _Db, replicated_changes) ->
+    % Skip internal replicator updates
+    Doc;
+
+before_doc_update(#doc{} = Doc, _Db, _UpdateType) ->
+    Body1 = couch_util:json_encode(Doc#doc.body),
+    Body2 = couch_util:json_decode(Body1, [return_maps]),
+    validate(Body2),
+    Doc.
+
+
+validate(#{} = Body) ->
+    validate_key(<<"by_node">>, Body, ["by_node is mandatory"]),
+    validate_key(<<"by_range">>, Body, ["by_range is mandatory"]),
+    ByNode = maps:get(<<"by_node">>, Body),
+    ByRange = maps:get(<<"by_range">>, Body),
+    % "by_node": {
+    %    "node1@xxx.xxx.xxx.xxx": ["00000000-1fffffff",...]
+    % ]}
+    maps:map(fun(Node, Ranges) ->
+        validate_by_node(Node, Ranges, ByRange)
+    end, ByNode),
+    % "by_range": {
+    %   "00000000-1fffffff": ["node1@xxx.xxx.xxx.xxx", ...]
+    % ]}
+    maps:map(fun(Range, Nodes) ->
+        validate_by_range(Range, Nodes, ByNode)
+    end, ByRange).
+
+
+validate_by_node(Node, Ranges, ByRange) ->
+    validate_array(Ranges, ["by_node", Ranges, "value not an array"]),
+    lists:foreach(fun(Range) ->
+        validate_key(Range, ByRange, ["by_range for", Range, "missing"]),
+        Nodes = maps:get(Range, ByRange),
+        validate_member(Node, Nodes, ["by_range for", Range, "missing", Node])
+    end, Ranges).
+
+
+validate_by_range(Range, Nodes, ByNode) ->
+    validate_array(Nodes, ["by_range", Nodes, "value not an array"]),
+    lists:foreach(fun(Node) ->
+        validate_key(Node, ByNode, ["by_node for", Node, "missing"]),
+        Ranges = maps:get(Node, ByNode),
+        validate_member(Range, Ranges, ["by_node for", Node, "missing", Range])
+    end, Nodes).
+
+
+validate_array(Val, _ErrMsg) when is_list(Val) ->
+    ok;
+validate_array(_Val, ErrMsg) ->
+    throw({forbidden, errmsg(ErrMsg)}).
+
+
+validate_key(Key, #{} = Map, ErrMsg) ->
+    case maps:is_key(Key, Map) of
+        true -> ok;
+        false -> throw({forbidden, errmsg(ErrMsg)})
+    end;
+validate_key(_Key, _Map, ErrMsg) ->
+    throw({forbidden, errmsg(ErrMsg)}).

Review comment:
       This is another line that doesn't have test coverage. Is it possible for this to happen?

##########
File path: src/mem3/src/mem3_bdu.erl
##########
@@ -0,0 +1,106 @@
+% 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(mem3_bdu).
+
+
+-export([
+    before_doc_update/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-spec before_doc_update(#doc{}, Db::any(), couch_db:update_type()) -> #doc{}.
+before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db, _UpdateType) ->
+    % Skip design docs
+    Doc;
+
+before_doc_update(#doc{deleted = true} = Doc, _Db, _UpdateType) ->
+    % Skip deleted
+    Doc;
+
+before_doc_update(#doc{} = Doc, _Db, replicated_changes) ->
+    % Skip internal replicator updates
+    Doc;

Review comment:
       This is the other BDU clause not covered by eunit tests, and also I believe is the only functional difference between the VDU implementation? What is the reason this was added, and do you think it's worth adding a test case?

##########
File path: src/mem3/src/mem3_bdu.erl
##########
@@ -0,0 +1,106 @@
+% 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(mem3_bdu).
+
+
+-export([
+    before_doc_update/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-spec before_doc_update(#doc{}, Db::any(), couch_db:update_type()) -> #doc{}.
+before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db, _UpdateType) ->
+    % Skip design docs
+    Doc;
+
+before_doc_update(#doc{deleted = true} = Doc, _Db, _UpdateType) ->
+    % Skip deleted
+    Doc;
+
+before_doc_update(#doc{} = Doc, _Db, replicated_changes) ->
+    % Skip internal replicator updates
+    Doc;
+
+before_doc_update(#doc{} = Doc, _Db, _UpdateType) ->
+    Body1 = couch_util:json_encode(Doc#doc.body),
+    Body2 = couch_util:json_decode(Body1, [return_maps]),

Review comment:
       It seems a pity to go through this presumably expensive dance just to get a map here. I guess adding a `#doc.body_map` field would be tricky, but could `#doc.meta` be (ab)used for something sneaky like e.g.
   ```erlang
   {body_map, #{} = BodyMap}
   ```
   ?

##########
File path: src/mem3/src/mem3_bdu.erl
##########
@@ -0,0 +1,106 @@
+% 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(mem3_bdu).
+
+
+-export([
+    before_doc_update/3
+]).
+
+
+-include_lib("couch/include/couch_db.hrl").
+
+
+-spec before_doc_update(#doc{}, Db::any(), couch_db:update_type()) -> #doc{}.
+before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db, _UpdateType) ->
+    % Skip design docs
+    Doc;

Review comment:
       I added eunit coverage to mem3 and this module has 91% test coverage, but this clause is one exception. Do you think it's worth testing this case?




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

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org