nickva commented on code in PR #5014:
URL: https://github.com/apache/couchdb/pull/5014#discussion_r1562070956


##########
src/couch_scanner/src/couch_scanner_plugin_ddoc_features.erl:
##########
@@ -0,0 +1,236 @@
+% 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.
+
+% Scanner plugin to detect various design document features
+%
+% By default when enabled it will scan design documents for features slated to 
be
+% deprecated in 4.0 such as: rewrites, lists, shows, updates. There are options
+% to enable other feature checks: custom JS reducers, JS libraries, VDU 
functions,
+% and JS filters.
+%
+% By default the scanning will start and run on the first node of the cluster 
only.
+% It's possible to make the plugin run on all the and each node will pick a 
fraction
+% of dbs to scan. That will make the scan go fast but might consume more 
resources. That options
+% is contorlled via the run_on_first_node boolean setting.
+%
+% When features are detected they are aggregated per database and reported 
once all the
+% design documents for a particular database have been processed. To get 
details about
+% the specific design document enable the ddoc_report = true setting.
+%
+
+-module(couch_scanner_plugin_ddoc_features).
+-behaviour(couch_scanner_plugin).
+
+-export([
+    start/2,
+    resume/2,
+    complete/1,
+    checkpoint/1,
+    db/2,
+    ddoc/3
+]).
+
+-include_lib("couch_scanner/include/couch_scanner_plugin.hrl").
+
+-record(st, {
+    sid,
+    dbname,
+    report = #{},
+    opts = #{},
+    run_on_first_node = true,
+    ddoc_report = false
+}).
+
+-define(UPDATES, <<"updates">>).
+-define(SHOWS, <<"shows">>).
+-define(LISTS, <<"lists">>).
+-define(REWRITES, <<"rewrites">>).
+-define(FILTERS, <<"filters">>).
+-define(REDUCE, <<"reduce">>).
+-define(VDU, <<"validate_doc_update">>).
+-define(VIEWS, <<"views">>).
+
+-define(OPTS, #{
+    ?UPDATES => true,
+    ?SHOWS => true,
+    ?LISTS => true,
+    ?REWRITES => true,
+    ?FILTERS => false,
+    ?REDUCE => false,
+    ?VDU => false
+}).
+
+% Behavior callbacks
+
+start(SId, #{}) ->
+    St = init_config(#st{sid = SId}),
+    case should_run(St) of
+        true ->
+            ?INFO("Starting.", [], #{sid => SId}),
+            {ok, St};
+        false ->
+            ?INFO("Not starting. Not on first node.", [], #{sid => SId}),
+            skip
+    end.
+
+resume(SId, #{<<"opts">> := OldOpts}) ->
+    St = init_config(#st{sid = SId}),
+    case {OldOpts == St#st.opts, should_run(St)} of
+        {true, true} ->
+            ?INFO("Resuming.", [], #{sid => SId}),
+            {ok, St};
+        {false, true} ->
+            ?INFO("Resetting. Config changed.", [], #{sid => SId}),
+            reset;
+        {_, false} ->
+            ?INFO("Not resuming. Not on first node.", [], #{sid => SId}),
+            skip
+    end.
+
+complete(#st{sid = SId, dbname = DbName, report = Report} = St) ->
+    report_per_db(St, DbName, Report),
+    ?INFO("Completed", [], #{sid => SId}),
+    {ok, #{}}.
+
+checkpoint(#st{sid = SId, opts = Opts}) ->
+    case Opts == opts() of
+        true ->
+            {ok, #{<<"opts">> => Opts}};
+        false ->
+            ?INFO("Resetting. Config changed.", [], #{sid => SId}),
+            reset
+    end.
+
+db(#st{} = St, DbName) ->
+    % If we run on all nodes spread db checks across nodes
+    case should_pick_db(DbName, St) of
+        true -> {ok, St};
+        false -> {skip, St}
+    end.
+
+ddoc(#st{} = St, _DbName, #doc{id = <<"_design/_", _/binary>>}) ->
+    % These are auto-inserted ddocs _design/_auth, etc.
+    {ok, St};
+ddoc(#st{} = St, DbName, #doc{} = DDoc) ->
+    #doc{body = {Props = [_ | _]}} = DDoc,
+    case couch_util:get_value(<<"language">>, Props, <<"javascript">>) of
+        <<"javascript">> -> {ok, check_ddoc(St, DbName, DDoc)};
+        _ -> {ok, St}
+    end.
+
+% Private
+
+init_config(#st{} = St) ->
+    St#st{
+        opts = opts(),
+        run_on_first_node = cfg_bool("run_on_first_node", 
St#st.run_on_first_node),
+        ddoc_report = cfg_bool("ddoc_report", St#st.ddoc_report)
+    }.
+
+should_run(#st{run_on_first_node = true}) ->
+    hd(mem3_util:live_nodes()) =:= node();
+should_run(#st{run_on_first_node = false}) ->
+    true.
+
+should_pick_db(_DbName, #st{run_on_first_node = true}) ->
+    true;
+should_pick_db(DbName, #st{run_on_first_node = false}) ->
+    Nodes = mem3_util:live_nodes(),
+    hd(mem3_util:rotate_list(DbName, Nodes)) =:= node().

Review Comment:
   Good idea. I'll try to add  'on_first_node() -> true|false` and 
`consistent_hash_nodes(Item) -> true|false`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to