summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Newson <rnewson@apache.org>2023-04-04 10:32:52 +0100
committerRobert Newson <rnewson@apache.org>2023-04-04 10:54:51 +0100
commit126a28cdacc765a36b40b7cf8338762254410ce5 (patch)
tree8f3ceb98af11e35d981c763f47c7a830e6f21d9b
parenta9bce2f598edc8ef843baa9412c60d22157eeabf (diff)
downloadcouchdb-dreyfus-await-time.tar.gz
WIP send await time in response header - dreyfusdreyfus-await-time
-rw-r--r--src/dreyfus/src/dreyfus_fabric_search.erl20
-rw-r--r--src/dreyfus/src/dreyfus_httpd.erl24
-rw-r--r--src/dreyfus/src/dreyfus_rpc.erl3
3 files changed, 35 insertions, 12 deletions
diff --git a/src/dreyfus/src/dreyfus_fabric_search.erl b/src/dreyfus/src/dreyfus_fabric_search.erl
index 7e78e5fc3..77a19d44a 100644
--- a/src/dreyfus/src/dreyfus_fabric_search.erl
+++ b/src/dreyfus/src/dreyfus_fabric_search.erl
@@ -27,7 +27,8 @@
counters,
start_args,
replacements,
- ring_opts
+ ring_opts,
+ await_time = 0
}).
go(DbName, GroupId, IndexName, QueryArgs) when is_binary(GroupId) ->
@@ -125,7 +126,7 @@ go(DbName, DDoc, IndexName, QueryArgs, Counters, Bookmark, RingOpts) ->
)
of
{ok, Result} ->
- #state{top_docs = TopDocs} = Result,
+ #state{top_docs = TopDocs, await_time = AwaitTime} = Result,
#top_docs{
total_hits = TotalHits,
hits = Hits,
@@ -134,11 +135,11 @@ go(DbName, DDoc, IndexName, QueryArgs, Counters, Bookmark, RingOpts) ->
} = TopDocs,
case RawBookmark of
true ->
- {ok, Bookmark, TotalHits, Hits, Counts, Ranges};
+ {ok, Bookmark, TotalHits, Hits, Counts, Ranges, AwaitTime};
false ->
Bookmark1 = dreyfus_bookmark:update(Sort, Bookmark, Hits),
Hits1 = remove_sortable(Hits),
- {ok, Bookmark1, TotalHits, Hits1, Counts, Ranges}
+ {ok, Bookmark1, TotalHits, Hits1, Counts, Ranges, AwaitTime}
end;
{error, Reason} ->
{error, Reason}
@@ -178,6 +179,17 @@ handle_message({ok, {top_docs, UpdateSeq, TotalHits, Hits}}, Shard, State) ->
hits = Hits
},
handle_message({ok, TopDocs}, Shard, State);
+handle_message({await_time, Time}, Shard, State) ->
+ case fabric_dict:lookup_element(Shard, State#state.counters) of
+ undefined ->
+ %% already heard from someone else in this range
+ {ok, State};
+ nil ->
+ State1 = State#state{
+ await_time = max(Time, State#state.await_time)
+ },
+ {ok, State1}
+ end;
handle_message(Error, Worker, State0) ->
State = upgrade_state(State0),
case
diff --git a/src/dreyfus/src/dreyfus_httpd.erl b/src/dreyfus/src/dreyfus_httpd.erl
index f6607d644..ae7826ce3 100644
--- a/src/dreyfus/src/dreyfus_httpd.erl
+++ b/src/dreyfus/src/dreyfus_httpd.erl
@@ -26,7 +26,7 @@
-include_lib("couch/include/couch_db.hrl").
-import(chttpd, [
send_method_not_allowed/2,
- send_json/2, send_json/3,
+ send_json/2, send_json/3, send_json/4,
send_error/2
]).
@@ -68,7 +68,7 @@ handle_search_req(
{rows, Hits}
]}
);
- {ok, Bookmark0, TotalHits, Hits0, Counts0, Ranges0} ->
+ {ok, Bookmark0, TotalHits, Hits0, Counts0, Ranges0, AwaitTime} ->
Hits = hits_to_json(DbName, IncludeDocs, Hits0),
Bookmark = dreyfus_bookmark:pack(Bookmark0),
Counts =
@@ -85,13 +85,21 @@ handle_search_req(
_ ->
[{ranges, facets_to_json(Ranges0)}]
end,
- send_json(Req, 200, {
+ AwaitTimeMs = erlang:convert_time_unit(AwaitTime, native, millisecond),
+ send_json(
+ Req,
+ 200,
[
- {total_rows, TotalHits},
- {bookmark, Bookmark},
- {rows, Hits}
- ] ++ Counts ++ Ranges
- });
+ {"x-cloudant-await-time", integer_to_list(AwaitTimeMs)}
+ ],
+ {
+ [
+ {total_rows, TotalHits},
+ {bookmark, Bookmark},
+ {rows, Hits}
+ ] ++ Counts ++ Ranges
+ }
+ );
{error, Reason} ->
handle_error(Req, Db, DDoc, RetryCount, RetryPause, Reason)
end;
diff --git a/src/dreyfus/src/dreyfus_rpc.erl b/src/dreyfus/src/dreyfus_rpc.erl
index 2ebc5ffe5..4c2e64a8b 100644
--- a/src/dreyfus/src/dreyfus_rpc.erl
+++ b/src/dreyfus/src/dreyfus_rpc.erl
@@ -48,9 +48,12 @@ call(Fun, DbName, DDoc, IndexName, QueryArgs0) ->
{ok, Index} ->
case dreyfus_index_manager:get_index(DbName, Index) of
{ok, Pid} ->
+ T0 = erlang:monotonic_time(),
case dreyfus_index:await(Pid, MinSeq) of
{ok, IndexPid, _Seq} ->
+ T1 = erlang:monotonic_time(),
Result = dreyfus_index:Fun(IndexPid, QueryArgs),
+ rexi:reply({await_time, T1 - T0}),
rexi:reply(Result);
% obsolete clauses, remove after upgrade
ok ->