You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2025/03/18 16:29:38 UTC

(couchdb) branch couch-stats-resource-tracker-v3 updated: Add dynamic toggles for init_p RPC metrics

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

chewbranca pushed a commit to branch couch-stats-resource-tracker-v3
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/couch-stats-resource-tracker-v3 by this push:
     new d6c3b9d57 Add dynamic toggles for init_p RPC metrics
d6c3b9d57 is described below

commit d6c3b9d57fde998be6d9abca3c05a932f8a9836e
Author: Russell Branca <ch...@apache.org>
AuthorDate: Mon Mar 17 18:05:39 2025 -0700

    Add dynamic toggles for init_p RPC metrics
---
 rel/overlay/etc/default.ini                        |  18 ++++
 src/couch_stats/src/couch_stats.erl                |   2 +-
 .../src/couch_stats_resource_tracker.hrl           |   1 +
 src/couch_stats/src/csrt.erl                       |  69 ++++++++------
 src/couch_stats/src/csrt_util.erl                  | 105 ++++++++++++++++++++-
 5 files changed, 162 insertions(+), 33 deletions(-)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index 8053f1bdd..aac3a5157 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -1060,3 +1060,21 @@ url = {{nouveau_url}}
 
 ; Scanner settings to skip dbs and docs would also work:
 ;[couch_quickjs_scanner_plugin.skip_{dbs,ddoc,docs}]
+
+; Couch Stats Resource Tracker (CSRT)
+[csrt]
+enabled = true
+
+; CSRT Rexi Server Init P tracking
+; Enable these to enable additional metrics for RPC worker spawn rates
+; Mod and Function are separated by double underscores
+[csrt.init_p]
+enabled = false
+fabric_rpc__all_docs = true
+fabric_rpc__changes = true
+fabric_rpc__map_view = true
+fabric_rpc__reduce_view = true
+fabric_rpc__get_all_security = true
+fabric_rpc__open_doc = true
+fabric_rpc__update_docs = true
+fabric_rpc__open_shard = true
diff --git a/src/couch_stats/src/couch_stats.erl b/src/couch_stats/src/couch_stats.erl
index a9abfce31..f92950fa9 100644
--- a/src/couch_stats/src/couch_stats.erl
+++ b/src/couch_stats/src/couch_stats.erl
@@ -121,7 +121,7 @@ maybe_track_local_counter(_, _) ->
 
 maybe_track_rexi_init_p({M, F, _A}) ->
     Metric = [M, F, spawned],
-    case csrt:should_track(Metric) of
+    case csrt:should_track_init_p(Metric) of
         true -> increment_counter(Metric);
         false -> ok
     end.
diff --git a/src/couch_stats/src/couch_stats_resource_tracker.hrl b/src/couch_stats/src/couch_stats_resource_tracker.hrl
index 998621bfb..6c5ba6e53 100644
--- a/src/couch_stats/src/couch_stats_resource_tracker.hrl
+++ b/src/couch_stats/src/couch_stats_resource_tracker.hrl
@@ -11,6 +11,7 @@
 % the License.
 
 -define(CSRT, "csrt").
+-define(CSRT_INIT_P, "csrt.init_p").
 
 %% CSRT pdict markers
 -define(DELTA_TA, csrt_delta_ta).
diff --git a/src/couch_stats/src/csrt.erl b/src/couch_stats/src/csrt.erl
index f66755c4c..a2b5f51d8 100644
--- a/src/couch_stats/src/csrt.erl
+++ b/src/couch_stats/src/csrt.erl
@@ -45,6 +45,7 @@
 %% Public API
 -export([
     is_enabled/0,
+    is_enabled_init_p/0,
     do_report/2,
     maybe_report/2,
     conf_get/1,
@@ -67,7 +68,7 @@
     maybe_add_delta/1,
     maybe_add_delta/2,
     maybe_inc/2,
-    should_track/1
+    should_track_init_p/1
 ]).
 
 %% aggregate query api
@@ -249,6 +250,11 @@ destroy_context({_, _} = PidRef) ->
 is_enabled() ->
     csrt_util:is_enabled().
 
+%% @equiv csrt_util:is_enabled_init_p().
+-spec is_enabled_init_p() -> boolean().
+is_enabled_init_p() ->
+    csrt_util:is_enabled_init_p().
+
 -spec get_resource() -> maybe_rctx().
 get_resource() ->
     get_resource(get_pid_ref()).
@@ -289,34 +295,10 @@ maybe_inc(Stat, Val) ->
             0
     end.
 
-%% TODO: update stats_descriptions.cfg for relevant apps
--spec should_track(Stat :: [atom()]) -> boolean().
-should_track([fabric_rpc, all_docs, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, changes, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, changes, processed]) ->
-    is_enabled();
-should_track([fabric_rpc, changes, returned]) ->
-    is_enabled();
-should_track([fabric_rpc, map_view, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, reduce_view, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, get_all_security, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, open_doc, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, update_docs, spawned]) ->
-    is_enabled();
-should_track([fabric_rpc, open_shard, spawned]) ->
-    is_enabled();
-should_track([mango_cursor, view, all_docs]) ->
-    is_enabled();
-should_track([mango_cursor, view, idx]) ->
-    is_enabled();
-should_track(_Metric) ->
-    %%io:format("SKIPPING METRIC: ~p~n", [Metric]),
+-spec should_track_init_p(Stat :: [atom()]) -> boolean().
+should_track_init_p([Mod, Func, spawned]) ->
+    is_enabled_init_p() andalso csrt_util:should_track_init_p(Mod, Func);
+should_track_init_p(_Metric) ->
     false.
 
 -spec ioq_called() -> non_neg_integer().
@@ -473,7 +455,9 @@ couch_stats_resource_tracker_test_() ->
         fun setup/0,
         fun teardown/1,
         [
-            ?TDEF_FE(t_static_map_translations)
+            ?TDEF_FE(t_static_map_translations),
+            ?TDEF_FE(t_should_track_init_p),
+            ?TDEF_FE(t_should_not_track_init_p)
         ]
     }.
 
@@ -488,4 +472,29 @@ t_static_map_translations(_) ->
     %% TODO: properly handle ioq_calls field
     ?assertEqual(lists:sort(maps:values(?STATS_TO_KEYS)), lists:delete(docs_written, lists:delete(ioq_calls, lists:sort(maps:keys(?KEYS_TO_FIELDS))))).
 
+t_should_track_init_p(_) ->
+    config:set(?CSRT_INIT_P, "enabled", "true", false),
+    Metrics = [
+        [fabric_rpc, all_docs, spawned],
+        [fabric_rpc, changes, spawned],
+        [fabric_rpc, map_view, spawned],
+        [fabric_rpc, reduce_view, spawned],
+        [fabric_rpc, get_all_security, spawned],
+        [fabric_rpc, open_doc, spawned],
+        [fabric_rpc, update_docs, spawned],
+        [fabric_rpc, open_shard, spawned]
+    ],
+    [csrt_util:set_fabric_init_p(F, true, false) || [_, F, _] <- Metrics],
+    [?assert(should_track_init_p(M), M) || M <- Metrics].
+
+t_should_not_track_init_p(_) ->
+    config:set(?CSRT_INIT_P, "enabled", "true", false),
+    Metrics = [
+        [couch_db, name, spawned],
+        [couch_db, get_db_info, spawned],
+        [couch_db, open, spawned],
+        [fabric_rpc, get_purge_seq, spawned]
+    ],
+    [?assert(should_track_init_p(M) =:= false, M) || M <- Metrics].
+
 -endif.
diff --git a/src/couch_stats/src/csrt_util.erl b/src/couch_stats/src/csrt_util.erl
index 04e101fed..dbc73066a 100644
--- a/src/couch_stats/src/csrt_util.erl
+++ b/src/couch_stats/src/csrt_util.erl
@@ -14,11 +14,13 @@
 
 -export([
     is_enabled/0,
+    is_enabled_init_p/0,
     conf_get/1,
     conf_get/2,
     get_pid_ref/0,
     get_pid_ref/1,
     set_pid_ref/1,
+    should_track_init_p/2,
     tnow/0,
     tutc/0,
     tutc/1
@@ -50,19 +52,36 @@
     set_delta_zero/1
 ]).
 
+%% Extra niceties and testing facilities
+-export([
+    set_fabric_init_p/2,
+    set_fabric_init_p/3
+]).
+
+
 -include_lib("couch_stats_resource_tracker.hrl").
 
 -spec is_enabled() -> boolean().
 is_enabled() ->
     config:get_boolean(?CSRT, "enabled", true).
 
+-spec is_enabled_init_p() -> boolean().
+is_enabled_init_p() ->
+    config:get_boolean(?CSRT_INIT_P, "enabled", true).
+
+-spec should_track_init_p(Mod :: atom(), Func :: atom()) -> boolean().
+should_track_init_p(fabric_rpc, Func) ->
+    config:get_boolean(?CSRT_INIT_P, fabric_conf_key(Func), false);
+should_track_init_p(_Mod, _Func) ->
+    false.
+
 -spec conf_get(Key :: list()) -> list().
 conf_get(Key) ->
-    conf_get(Key, "undefined").
+    conf_get(Key, undefined).
 
 -spec conf_get(Key :: list(), Default :: list()) -> list().
 conf_get(Key, Default) ->
-    config:get(?MODULE_STRING, Key, Default).
+    config:get(?CSRT, Key, Default).
 
 %% Monotnonic time now in native format using time forward only event tracking
 -spec tnow() -> integer().
@@ -278,3 +297,85 @@ set_pid_ref(PidRef) ->
     erlang:put(?PID_REF, PidRef),
     PidRef.
 
+%% @equiv set_fabric_init_p(Func, Enabled, true).
+-spec set_fabric_init_p(Func :: atom(), Enabled :: boolean()) -> ok.
+set_fabric_init_p(Func, Enabled) ->
+    set_fabric_init_p(Func, Enabled, true).
+
+%% Expose Persist for use in test cases outside this module
+-spec set_fabric_init_p(Func, Enabled, Persist) -> ok when
+        Func :: atom(), Enabled :: boolean(), Persist :: boolean().
+set_fabric_init_p(Func, Enabled, Persist) ->
+    Key = fabric_conf_key(Func),
+    ok = config:set_boolean(?CSRT_INIT_P, Key, Enabled, Persist).
+
+-spec fabric_conf_key(Key :: atom()) -> string().
+fabric_conf_key(Key) ->
+    %% Double underscore to separate Mod and Func
+    "fabric_rpc__" ++ atom_to_list(Key).
+
+-ifdef(TEST).
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+couch_stats_resource_tracker_test_() ->
+    {
+        foreach,
+        fun setup/0,
+        fun teardown/1,
+        [
+            ?TDEF_FE(t_should_track_init_p),
+            ?TDEF_FE(t_should_track_init_p_empty),
+            ?TDEF_FE(t_should_track_init_p_disabled),
+            ?TDEF_FE(t_should_not_track_init_p)
+        ]
+    }.
+
+setup() ->
+    test_util:start_couch().
+
+teardown(Ctx) ->
+    test_util:stop_couch(Ctx).
+
+t_should_track_init_p(_) ->
+    enable_init_p(),
+    [?assert(should_track_init_p(M, F), {M, F}) || [M, F] <- base_metrics()].
+
+t_should_track_init_p_empty(_) ->
+    config:set(?CSRT_INIT_P, "enabled", "true", false),
+    [?assert(should_track_init_p(M, F) =:= false, {M, F}) || [M, F] <- base_metrics()].
+
+t_should_track_init_p_disabled(_) ->
+    config:set(?CSRT_INIT_P, "enabled", "false", false),
+    [?assert(should_track_init_p(M, F) =:= false, {M, F}) || [M, F] <- base_metrics()].
+
+t_should_not_track_init_p(_) ->
+    enable_init_p(),
+    Metrics = [
+        [couch_db, name],
+        [couch_db, get_after_doc_read_fun],
+        [couch_db, open],
+        [fabric_rpc, get_purge_seq]
+    ],
+    [?assert(should_track_init_p(M, F) =:= false, {M, F}) || [M, F] <- Metrics].
+
+enable_init_p() ->
+    enable_init_p(base_metrics()).
+
+enable_init_p(Metrics) ->
+    config:set(?CSRT_INIT_P, "enabled", "true", false),
+    [set_fabric_init_p(F, true, false) || [_, F] <- Metrics].
+
+base_metrics() ->
+    [
+        [fabric_rpc, all_docs],
+        [fabric_rpc, changes],
+        [fabric_rpc, map_view],
+        [fabric_rpc, reduce_view],
+        [fabric_rpc, get_all_security],
+        [fabric_rpc, open_doc],
+        [fabric_rpc, update_docs],
+        [fabric_rpc, open_shard]
+    ].
+
+-endif.