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

[couchdb] 01/01: report changes stats intermittently

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

tonysun83 pushed a commit to branch report-changes-stats
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 4c20483b0f6016c96b531dee16132591d186b88b
Author: Tony Sun <to...@gmail.com>
AuthorDate: Fri Apr 10 12:10:25 2020 -0700

    report changes stats intermittently
    
    Stats are reported at the end of a request. With changes feeds,
    sometimes this can take a long or never end at all. This changes allows
    stats to be reported when changes are flushed and sent to user.
---
 src/chttpd/src/chttpd_db.erl    | 18 +++++++++++-------
 src/chttpd/src/chttpd_stats.erl | 12 +++++++++++-
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 730cf3e..5c85632 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -50,7 +50,8 @@
     chunks_sent = 0,
     buffer = [],
     bufsize = 0,
-    threshold
+    threshold,
+    request
 }).
 
 -define(IS_ALL_DOCS(T), (
@@ -131,8 +132,9 @@ handle_changes_req_tx(#httpd{}=Req, Db) ->
 
 % callbacks for continuous feed (newline-delimited JSON Objects)
 changes_callback(start, #cacc{feed = continuous} = Acc) ->
-    {ok, Resp} = chttpd:start_delayed_json_response(Acc#cacc.mochi, 200),
-    {ok, Acc#cacc{mochi = Resp, responding = true}};
+    Req = Acc#cacc.mochi,
+    {ok, Resp} = chttpd:start_delayed_json_response(Req, 200),
+    {ok, Acc#cacc{mochi = Resp, responding = true, request=Req}};
 changes_callback({change, Change}, #cacc{feed = continuous} = Acc) ->
     chttpd_stats:incr_rows(),
     Data = [?JSON_ENCODE(Change) | "\n"],
@@ -156,7 +158,7 @@ changes_callback(start, #cacc{feed = eventsource} = Acc) ->
         {"Cache-Control", "no-cache"}
     ],
     {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers),
-    {ok, Acc#cacc{mochi = Resp, responding = true}};
+    {ok, Acc#cacc{mochi = Resp, responding = true, request = Req}};
 changes_callback({change, {ChangeProp}=Change}, #cacc{feed = eventsource} = Acc) ->
     chttpd_stats:incr_rows(),
     Seq = proplists:get_value(seq, ChangeProp),
@@ -183,12 +185,12 @@ changes_callback(start, #cacc{feed = normal} = Acc) ->
     FirstChunk = "{\"results\":[\n",
     {ok, Resp} = chttpd:start_delayed_json_response(Req, 200,
         [{"ETag",Etag}], FirstChunk),
-    {ok, Acc#cacc{mochi = Resp, responding = true}};
+    {ok, Acc#cacc{mochi = Resp, responding = true, request=Req}};
 changes_callback(start, Acc) ->
     #cacc{mochi = Req} = Acc,
     FirstChunk = "{\"results\":[\n",
     {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, [], FirstChunk),
-    {ok, Acc#cacc{mochi = Resp, responding = true}};
+    {ok, Acc#cacc{mochi = Resp, responding = true, request=Req}};
 changes_callback({change, Change}, Acc) ->
     chttpd_stats:incr_rows(),
     Data = [Acc#cacc.prepend, ?JSON_ENCODE(Change)],
@@ -239,8 +241,10 @@ changes_callback({error, Reason}, Acc) ->
 
 maybe_flush_changes_feed(#cacc{bufsize=Size, threshold=Max} = Acc, Data, Len)
          when Size > 0 andalso (Size + Len) > Max ->
-    #cacc{buffer = Buffer, mochi = Resp} = Acc,
+    #cacc{buffer = Buffer, mochi = Resp, request = Req} = Acc,
     {ok, R1} = chttpd:send_delayed_chunk(Resp, Buffer),
+    chttpd_stats:report(Req, Resp),
+    chttpd_stats:reset_after_reporting(),
     {ok, Acc#cacc{prepend = ",\r\n", buffer = Data, bufsize=Len, mochi = R1}};
 maybe_flush_changes_feed(Acc0, Data, Len) ->
     #cacc{buffer = Buf, bufsize = Size, chunks_sent = ChunksSent} = Acc0,
diff --git a/src/chttpd/src/chttpd_stats.erl b/src/chttpd/src/chttpd_stats.erl
index 59ec926..70e8408 100644
--- a/src/chttpd/src/chttpd_stats.erl
+++ b/src/chttpd/src/chttpd_stats.erl
@@ -24,7 +24,9 @@
     incr_writes/1,
 
     incr_rows/0,
-    incr_rows/1
+    incr_rows/1,
+
+    reset_after_report/0
 ]).
 
 
@@ -105,3 +107,11 @@ incr(Idx, Count) ->
         _ ->
             ok
     end.
+
+
+reset_after_report() ->
+    put(?KEY, #st{
+        reads = 0,
+        writes = 0,
+        rows = 0
+    }).