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


##########
src/nouveau/src/nouveau_rpc.erl:
##########
@@ -24,49 +24,65 @@
 -include("nouveau.hrl").
 -import(nouveau_util, [index_path/1]).
 
+-define(nouveau_update_latency, nouveau_update_latency).
+
 search(DbName, #index{} = Index0, QueryArgs0) ->
     %% Incorporate the shard name into the record.
     Index1 = Index0#index{dbname = DbName},
 
-    %% get minimum seqs for search
-    {MinUpdateSeq, MinPurgeSeq} = nouveau_index_updater:get_db_info(Index1),
+    Update = maps:get(update, QueryArgs0, true),
 
     %% Incorporate min seqs into the query args.
-    QueryArgs1 = QueryArgs0#{
-        min_update_seq => MinUpdateSeq,
-        min_purge_seq => MinPurgeSeq
-    },
-    Update = maps:get(update, QueryArgs1, true),
-
-    %% check if index is up to date
-    T0 = erlang:monotonic_time(),
-    case Update andalso nouveau_index_updater:outdated(Index1) of
-        true ->
-            case nouveau_index_manager:update_index(Index1) of
-                ok ->
-                    ok;
-                {error, Reason} ->
-                    rexi:reply({error, Reason})
-            end;
-        false ->
-            ok;
-        {error, Reason} ->
-            rexi:reply({error, Reason})
-    end,
-    T1 = erlang:monotonic_time(),
-    UpdateLatency = erlang:convert_time_unit(T1 - T0, native, millisecond),
+    QueryArgs1 =
+        case Update of
+            true ->
+                %% get minimum seqs for search
+                {MinUpdateSeq, MinPurgeSeq} = 
nouveau_index_updater:get_db_info(Index1),
+                QueryArgs0#{
+                    min_update_seq => MinUpdateSeq,
+                    min_purge_seq => MinPurgeSeq
+                };
+            false ->
+                QueryArgs0#{
+                    min_update_seq => 0,
+                    min_purge_seq => 0
+                }
+        end,
 
     %% Run the search
     case nouveau_api:search(Index1, QueryArgs1) of
         {ok, Response} ->
-            rexi:reply({ok, Response#{update_latency => UpdateLatency}});
-        {error, stale_index} ->
-            %% try again.
-            search(DbName, Index0, QueryArgs0);
+            rexi:reply({ok, Response#{update_latency => 
get_update_latency()}});
+        {error, stale_index} when Update ->
+            update_and_retry(DbName, Index0, QueryArgs0);
+        {error, {not_found, _}} when Update ->
+            update_and_retry(DbName, Index0, QueryArgs0);
+        Else ->
+            rexi:reply(Else)
+    end.
+
+update_and_retry(DbName, Index, QueryArgs) ->
+    T0 = erlang:monotonic_time(),
+    case nouveau_index_manager:update_index(Index#index{dbname = DbName}) of
+        ok ->
+            T1 = erlang:monotonic_time(),
+            erlang:put(

Review Comment:
   Process dict for stats is probably ok, but wonder if it would be a lot more 
work to carry around an extra map for timing so we'd accumulate it in there. 
Like say `search(DbName, #index{} = Index0, QueryArgs0)`  would call 
`search(DbName, #index{} = Index0, QueryArgs0, TimingInfo = #{})` and then 
`TimingInfo` would accumulate latency times



-- 
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: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to