You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2023/05/19 21:05:41 UTC

[couchdb] 01/01: WIP add a few more metrics

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

vatamane pushed a commit to branch add-a-few-more-metrics
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 700cb966b0c0efad6584cb067ab1d478b48e2521
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Fri May 19 17:05:12 2023 -0400

    WIP add a few more metrics
---
 src/couch/priv/stats_descriptions.cfg | 44 +++++++++++++++++++++++++++++++++++
 src/couch/src/couch_bt_engine.erl     |  1 +
 src/couch/src/couch_db_updater.erl    |  4 ++++
 src/couch/src/couch_file.erl          | 14 ++++++++---
 src/couch/src/couch_os_process.erl    |  7 ++++++
 src/couch/src/couch_proc_manager.erl  |  8 +++++--
 6 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/src/couch/priv/stats_descriptions.cfg b/src/couch/priv/stats_descriptions.cfg
index 7c8fd94cb..a1fc30005 100644
--- a/src/couch/priv/stats_descriptions.cfg
+++ b/src/couch/priv/stats_descriptions.cfg
@@ -278,6 +278,18 @@
     {type, histogram},
     {desc, <<"length of a request inside CouchDB without MochiWeb">>}
 ]}.
+{[couchdb, commits], [
+    {type, counter},
+    {desc, <<"number of commits performed">>}
+]},
+{[couchdb, coalesced_updates, interactive], [
+    {type, counter},
+    {desc, <<"number of coalesced interactive updates">>}
+]},
+{[couchdb, coalesced_updates, replicated], [
+    {type, counter},
+    {desc, <<"number of coalesced replicated updates">>}
+]},
 {[couchdb, couch_server, lru_skip], [
     {type, counter},
     {desc, <<"number of couch_server LRU operations skipped">>}
@@ -290,6 +302,30 @@
     {type, histogram},
     {desc, <<"duration of validate_doc_update function calls">>}
 ]}.
+{[couchdb, query_server, acquired_processes], [
+    {type, counter},
+    {desc, <<"number of acquired external processes">>}
+]},
+{[couchdb, query_server, process_starts], [
+    {type, counter},
+    {desc, <<"number of os process starts">>}
+]},
+{[couchdb, query_server, process_exists], [
+    {type, counter},
+    {desc, <<"number of os normal process exits">>}
+]},
+{[couchdb, query_server, process_error_exists], [
+    {type, counter},
+    {desc, <<"number of os error process exits">>}
+]},
+{[couchdb, query_server, process_prompts], [
+    {type, counter},
+    {desc, <<"number of successful os process prompts">>}
+]},
+{[couchdb, query_server, process_prompt_errors], [
+    {type, counter},
+    {desc, <<"number of os process prompt errors">>}
+]},
 {[pread, exceed_eof], [
     {type, counter},
     {desc, <<"number of the attempts to read beyond end of db file">>}
@@ -298,6 +334,14 @@
     {type, counter},
     {desc, <<"number of the attempts to read beyond set limit">>}
 ]}.
+{[fsync, time], [
+    {type, histogram},
+    {desc, <<"microseconds to call file:sync/1">>}
+]}.
+{[fsync, count], [
+    {type, counter},
+    {desc, <<"number of file:sync/1 calls">>}
+]}.
 {[mango, unindexed_queries], [
     {type, counter},
     {desc, <<"number of mango queries that could not use an index">>}
diff --git a/src/couch/src/couch_bt_engine.erl b/src/couch/src/couch_bt_engine.erl
index e5b7749f3..7bc02146e 100644
--- a/src/couch/src/couch_bt_engine.erl
+++ b/src/couch/src/couch_bt_engine.erl
@@ -570,6 +570,7 @@ commit_data(St) ->
             couch_file:sync(Fd),
             ok = couch_file:write_header(Fd, NewHeader),
             couch_file:sync(Fd),
+            couch_stats:increment_counter([couchdb, commits]),
             {ok, St#st{
                 header = NewHeader,
                 needs_commit = false
diff --git a/src/couch/src/couch_db_updater.erl b/src/couch/src/couch_db_updater.erl
index 93cdba16e..30f61aea0 100644
--- a/src/couch/src/couch_db_updater.erl
+++ b/src/couch/src/couch_db_updater.erl
@@ -295,6 +295,10 @@ collect_updates(GroupedDocsAcc, ClientsAcc, MergeConflicts) ->
         % updaters than deal with their possible conflicts, and local docs
         % writes are relatively rare. Can be optmized later if really needed.
         {update_docs, Client, GroupedDocs, [], MergeConflicts} ->
+            case MergeConflicts of
+                true -> couch_stats:increment_counter([couchdb, coalesced_updates, replicated]);
+                false -> couch_stats:increment_counter([couchdb, coalesced_updates, interactive])
+            end,
             GroupedDocs2 = sort_and_tag_grouped_docs(Client, GroupedDocs),
             GroupedDocsAcc2 =
                 merge_updates(GroupedDocsAcc, GroupedDocs2),
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index 514d4e3d9..d68f8c49c 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -250,7 +250,7 @@ sync(Filepath) when is_list(Filepath) ->
     case file:open(Filepath, [append, raw]) of
         {ok, Fd} ->
             try
-                case file:sync(Fd) of
+                case fsync(Fd) of
                     ok ->
                         ok;
                     {error, Reason} ->
@@ -437,7 +437,7 @@ init({Filepath, Options, ReturnPid, Ref}) ->
                                 true ->
                                     {ok, 0} = file:position(Fd, 0),
                                     ok = file:truncate(Fd),
-                                    ok = file:sync(Fd),
+                                    ok = fsync(Fd),
                                     maybe_track_open_os_files(Options),
                                     erlang:send_after(?INITIAL_WAIT, self(), maybe_close),
                                     {ok, #file{fd = Fd, is_sys = IsSys, pread_limit = Limit}};
@@ -554,7 +554,7 @@ handle_call({set_db_pid, Pid}, _From, #file{db_monitor = OldRef} = File) ->
     Ref = monitor(process, Pid),
     {reply, ok, File#file{db_monitor = Ref}};
 handle_call(sync, _From, #file{fd = Fd} = File) ->
-    case file:sync(Fd) of
+    case fsync(Fd) of
         ok ->
             {reply, ok, File};
         {error, _} = Error ->
@@ -642,6 +642,14 @@ format_status(_Opt, [PDict, #file{} = File]) ->
     {_Fd, FilePath} = couch_util:get_value(couch_file_fd, PDict),
     [{data, [{"State", File}, {"InitialFilePath", FilePath}]}].
 
+fsync(Fd) ->
+    T0 = erlang:monotonic_time(microsecond),
+    Res = file:sync(Fd),
+    Dt = erlang:monotonic_time(microsecond) - T0,
+    couch_stats:update_histogram([fsync, time], Dt),
+    couch_stats:increment_counter([fsync, count]),
+    Res.
+
 find_header(Fd, Block) ->
     case (catch load_header(Fd, Block)) of
         {ok, Bin} ->
diff --git a/src/couch/src/couch_os_process.erl b/src/couch/src/couch_os_process.erl
index 5ac387fc5..0b6436dda 100644
--- a/src/couch/src/couch_os_process.erl
+++ b/src/couch/src/couch_os_process.erl
@@ -169,6 +169,7 @@ init([Command, Options, PortOptions]) ->
     KillCmd = iolist_to_binary(readline(BaseProc)),
     Pid = self(),
     couch_log:debug("OS Process Start :: ~p", [BaseProc#os_proc.port]),
+    couch_stats:increment_counter([couchdb, query_server, process_starts]),
     spawn(fun() ->
         % this ensure the real os process is killed when this process dies.
         erlang:monitor(process, Pid),
@@ -207,13 +208,17 @@ handle_call({prompt, Data}, _From, #os_proc{idle = Idle} = OsProc) ->
     #os_proc{writer = Writer, reader = Reader} = OsProc,
     try
         Writer(OsProc, Data),
+        couch_stats:increment_counter([couchdb, query_server, process_prompts]),
         {reply, {ok, Reader(OsProc)}, OsProc, Idle}
     catch
         throw:{error, OsError} ->
+            couch_stats:increment_counter([couchdb, query_server, process_errors]),
             {reply, OsError, OsProc, Idle};
         throw:{fatal, OsError} ->
+            couch_stats:increment_counter([couchdb, query_server, process_errors]),
             {stop, normal, OsError, OsProc};
         throw:OtherError ->
+            couch_stats:increment_counter([couchdb, query_server, process_errors]),
             {stop, normal, OtherError, OsProc}
     after
         garbage_collect()
@@ -243,9 +248,11 @@ handle_info(timeout, #os_proc{idle = Idle} = OsProc) ->
     {noreply, OsProc, Idle};
 handle_info({Port, {exit_status, 0}}, #os_proc{port = Port} = OsProc) ->
     couch_log:info("OS Process terminated normally", []),
+    couch_stats:increment_counter([couchdb, query_server, process_exits]),
     {stop, normal, OsProc};
 handle_info({Port, {exit_status, Status}}, #os_proc{port = Port} = OsProc) ->
     couch_log:error("OS Process died with status: ~p", [Status]),
+    couch_stats:increment_counter([couchdb, query_server, process_error_exits]),
     {stop, {exit_status, Status}, OsProc};
 handle_info(Msg, #os_proc{idle = Idle} = OsProc) ->
     couch_log:debug("OS Proc: Unknown info: ~p", [Msg]),
diff --git a/src/couch/src/couch_proc_manager.erl b/src/couch/src/couch_proc_manager.erl
index 5055a06f3..f3eee25fa 100644
--- a/src/couch/src/couch_proc_manager.erl
+++ b/src/couch/src/couch_proc_manager.erl
@@ -81,13 +81,17 @@ get_proc(#doc{body = {Props}} = DDoc, DbKey, {_DDocId, _Rev} = DDocKey) ->
     Lang = couch_util:to_binary(LangStr),
     Client = #client{lang = Lang, ddoc = DDoc, db_key = DbKey, ddoc_key = DDocKey},
     Timeout = get_os_process_timeout(),
-    gen_server:call(?MODULE, {get_proc, Client}, Timeout).
+    Res = gen_server:call(?MODULE, {get_proc, Client}, Timeout),
+    couch_stats:increment_counter([couchdb, query_server, acquire_process]),
+    Res.
 
 get_proc(LangStr) ->
     Lang = couch_util:to_binary(LangStr),
     Client = #client{lang = Lang},
     Timeout = get_os_process_timeout(),
-    gen_server:call(?MODULE, {get_proc, Client}, Timeout).
+    Res = gen_server:call(?MODULE, {get_proc, Client}, Timeout),
+    couch_stats:increment_counter([couchdb, query_server, acquire_process]),
+    Res.
 
 ret_proc(#proc{} = Proc) ->
     gen_server:call(?MODULE, {ret_proc, Proc}, infinity).