From 47197921048884011a0a317db9c4b3dccae9bd01 Mon Sep 17 00:00:00 2001 From: Nick Vatamaniuc Date: Mon, 9 Mar 2026 16:52:22 -0400 Subject: [PATCH 1/2] Don't use db handle after closing in dreyfus_index Make sure we call `maybe_create_local_purge_doc(Db, Pid, Index)` with the still opened `Db` not after we close it. --- src/dreyfus/src/dreyfus_index.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dreyfus/src/dreyfus_index.erl b/src/dreyfus/src/dreyfus_index.erl index 5295a0065fb..d7540500151 100644 --- a/src/dreyfus/src/dreyfus_index.erl +++ b/src/dreyfus/src/dreyfus_index.erl @@ -119,11 +119,11 @@ init({DbName, Index}) -> case couch_db:open_int(DbName, []) of {ok, Db} -> try - couch_db:monitor(Db) + couch_db:monitor(Db), + dreyfus_util:maybe_create_local_purge_doc(Db, Pid, Index) after couch_db:close(Db) end, - dreyfus_util:maybe_create_local_purge_doc(Db, Pid, Index), proc_lib:init_ack({ok, self()}), gen_server:enter_loop(?MODULE, [], State); Error -> From 875cae6fa551f7f8b80f8cbbdda9c1b6ea85c89d Mon Sep 17 00:00:00 2001 From: Nick Vatamaniuc Date: Mon, 9 Mar 2026 16:58:08 -0400 Subject: [PATCH 2/2] Do not reset dreyfus purge_seq back to 0 in dreyfus Issue: https://github.com/apache/couchdb/issues/5916 Previously, `dreyfus_index_updater:purge_index/3`, updated the final purge_seq from the last value seen in the accumulator. When there is nothing to purge that will be the initial accumulator purge_seq value = 0, which is wrong. For example, if the minimum purge sequence is 12 and current purge sequence is 42, resetting the value back to 0 would mean the users would get an exception like `{invalid_start_purge_seq, 0, 12}` every time they update the index. To the purge system this looks like the "client" (the search index) has missed processing some purges and is now out of sync with the main database. To fix the issue we do what nouveau does, and set the index purge sequence based on the current database purge sequence, instead of getting it from the accumulator. As a side-effect, we simplify the fold function a bit, since we don't have to track the purge sequence inside. --- src/dreyfus/src/dreyfus_index_updater.erl | 43 +++++++++++------------ 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/dreyfus/src/dreyfus_index_updater.erl b/src/dreyfus/src/dreyfus_index_updater.erl index 6edc5a257e8..278d42b5419 100644 --- a/src/dreyfus/src/dreyfus_index_updater.erl +++ b/src/dreyfus/src/dreyfus_index_updater.erl @@ -92,30 +92,27 @@ purge_index(Db, IndexPid, Index) -> Proc = get_os_process(Index#index.def_lang), try true = proc_prompt(Proc, [<<"add_fun">>, Index#index.def]), - FoldFun = fun({PurgeSeq, _UUID, Id, _Revs}, {Acc, _}) -> - Acc0 = - case couch_db:get_full_doc_info(Db, Id) of - not_found -> - ok = clouseau_rpc:delete(IndexPid, Id), - Acc; - FDI -> - DI = couch_doc:to_doc_info(FDI), - #doc_info{id = Id, revs = [#rev_info{rev = Rev} | _]} = DI, - case lists:member({Id, Rev}, Acc) of - true -> - Acc; - false -> - update_or_delete_index(IndexPid, Db, DI, Proc), - [{Id, Rev} | Acc] - end - end, - update_task(1), - {ok, {Acc0, PurgeSeq}} + FoldFun = fun({_PurgeSeq, _UUID, Id, _Revs}, Acc) -> + case couch_db:get_full_doc_info(Db, Id) of + not_found -> + ok = clouseau_rpc:delete(IndexPid, Id), + update_task(1), + {ok, Acc}; + FDI -> + DI = couch_doc:to_doc_info(FDI), + #doc_info{id = Id, revs = [#rev_info{rev = Rev} | _]} = DI, + case lists:member({Id, Rev}, Acc) of + true -> + {ok, Acc}; + false -> + update_or_delete_index(IndexPid, Db, DI, Proc), + update_task(1), + {ok, [{Id, Rev} | Acc]} + end + end end, - - {ok, {ExcludeList, NewPurgeSeq}} = couch_db:fold_purge_infos( - Db, IdxPurgeSeq, FoldFun, {[], 0}, [] - ), + {ok, ExcludeList} = couch_db:fold_purge_infos(Db, IdxPurgeSeq, FoldFun, []), + NewPurgeSeq = couch_db:get_purge_seq(Db), clouseau_rpc:set_purge_seq(IndexPid, NewPurgeSeq), update_local_doc(Db, Index, NewPurgeSeq), {ok, ExcludeList}