Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance smoosh to cleanup search indexes when ddocs change #4718

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions src/dreyfus/src/dreyfus_fabric_cleanup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,17 @@

-module(dreyfus_fabric_cleanup).

-include("dreyfus.hrl").
-include_lib("mem3/include/mem3.hrl").
-include_lib("couch/include/couch_db.hrl").

-export([go/1]).

go(DbName) ->
{ok, DesignDocs} = fabric:design_docs(DbName),
ActiveSigs = lists:usort(
lists:flatmap(
fun active_sigs/1,
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
)
),
ActiveSigs = dreyfus_util:active_sigs(DbName),
cleanup_local_purge_doc(DbName, ActiveSigs),
clouseau_rpc:cleanup(DbName, ActiveSigs),
ok.

active_sigs(#doc{body = {Fields}} = Doc) ->
try
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
{IndexNames, _} = lists:unzip(RawIndexes),
[
begin
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
Index#index.sig
end
|| IndexName <- IndexNames
]
catch
error:{badmatch, _Error} ->
[]
end.

cleanup_local_purge_doc(DbName, ActiveSigs) ->
{ok, BaseDir} = clouseau_rpc:get_root_dir(),
DbNamePattern = <<DbName/binary, ".*">>,
Expand Down
30 changes: 29 additions & 1 deletion src/dreyfus/src/dreyfus_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
maybe_create_local_purge_doc/2,
maybe_create_local_purge_doc/3,
get_signature_from_idxdir/1,
verify_index_exists/2
verify_index_exists/2,
active_sigs/1
]).

get_shards(DbName, #index_query_args{partition = nil} = Args) ->
Expand Down Expand Up @@ -428,6 +429,33 @@ verify_index_exists(DbName, Props) ->
false
end.

active_sigs(DbName) when is_binary(DbName) ->
couch_util:with_db(DbName, fun active_sigs/1);
active_sigs(Db) ->
{ok, DesignDocs} = couch_db:get_design_docs(Db),
lists:usort(
lists:flatmap(
fun sigs_from_ddoc/1,
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
)
).

sigs_from_ddoc(#doc{body = {Fields}} = Doc) ->
try
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
{IndexNames, _} = lists:unzip(RawIndexes),
[
begin
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
Index#index.sig
end
|| IndexName <- IndexNames
]
catch
error:{badmatch, _Error} ->
[]
end.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

Expand Down
27 changes: 20 additions & 7 deletions src/fabric/src/fabric.erl
Original file line number Diff line number Diff line change
Expand Up @@ -590,25 +590,38 @@ cleanup_index_files(DbName) ->
cleanup_local_indices_and_purge_checkpoints([]) ->
ok;
cleanup_local_indices_and_purge_checkpoints([_ | _] = Dbs) ->
AllIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs),
AllPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs),
Sigs = couch_mrview_util:get_signatures(hd(Dbs)),
ok = cleanup_purges(Sigs, AllPurges, Dbs),
ok = cleanup_indices(Sigs, AllIndices).
MrViewIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs),
MrViewPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs),
MrViewSigs = couch_mrview_util:get_signatures(hd(Dbs)),
ok = cleanup_mrview_purges(MrViewSigs, MrViewPurges, Dbs),
ok = cleanup_mrview_indices(MrViewSigs, MrViewIndices),

cleanup_purges(Sigs, AllPurges, Dbs) ->
ClouseauSigs = dreyfus_util:active_sigs(hd(Dbs)),
ok = cleanup_clouseau_indices(Dbs, ClouseauSigs),

NouveauSigs = nouveau_util:active_sigs(hd(Dbs)),
ok = cleanup_nouveau_indices(Dbs, NouveauSigs).

cleanup_mrview_purges(Sigs, AllPurges, Dbs) ->
Fun = fun(DbPurges, Db) ->
couch_mrview_cleanup:cleanup_purges(Db, Sigs, DbPurges)
end,
lists:zipwith(Fun, AllPurges, Dbs),
ok.

cleanup_indices(Sigs, AllIndices) ->
cleanup_mrview_indices(Sigs, AllIndices) ->
Fun = fun(DbIndices) ->
couch_mrview_cleanup:cleanup_indices(Sigs, DbIndices)
end,
lists:foreach(Fun, AllIndices).

cleanup_clouseau_indices(Dbs, ActiveSigs) ->
Fun = fun(Db) -> clouseau_rpc:cleanup(Db, ActiveSigs) end,
lists:foreach(Fun, Dbs).
cleanup_nouveau_indices(Dbs, ActiveSigs) ->
Fun = fun(Db) -> nouveau_api:delete_path(nouveau_util:index_name(Db), ActiveSigs) end,
lists:foreach(Fun, Dbs).
Comment on lines +618 to +623
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't crash if either of those are not enabled? If there is a chance we could wrap them in a try ... catch maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clouseau_rpc:cleanup is a gen_server:cast so that's fine even if the target node doesn't exist.

all the nouveau_api functions have a send_if_enabled check and return {error, nouveau_not_enabled} when it's not enabled.

in either case I ignore the function result, but perhaps I should check that it's one of the two expected results?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The only worry was that we would throw an exception and prevent other indexes from getting cleaned up. Thanks for double-checking, I think it's fine as is, then.


%% @doc clean up index files for a specific db on all nodes
-spec cleanup_index_files_all_nodes(dbname()) -> [reference()].
cleanup_index_files_all_nodes(DbName) ->
Expand Down
9 changes: 1 addition & 8 deletions src/nouveau/src/nouveau_fabric_cleanup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

-module(nouveau_fabric_cleanup).

-include_lib("couch/include/couch_db.hrl").

-include("nouveau.hrl").
-include_lib("mem3/include/mem3.hrl").

-export([go/1]).
Expand All @@ -26,7 +23,7 @@ go(DbName) ->
ActiveSigs =
lists:usort(
lists:flatmap(
fun(Doc) -> active_sigs(DbName, Doc) end,
fun(Doc) -> nouveau_util:active_sigs(DbName, Doc) end,
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
)
),
Expand All @@ -37,7 +34,3 @@ go(DbName) ->
end,
Shards
).

active_sigs(DbName, #doc{} = Doc) ->
Indexes = nouveau_util:design_doc_to_indexes(DbName, Doc),
lists:map(fun(Index) -> Index#index.sig end, Indexes).
18 changes: 18 additions & 0 deletions src/nouveau/src/nouveau_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
index_name/1,
design_doc_to_indexes/2,
design_doc_to_index/3,
active_sigs/1,
active_sigs/2,
verify_index_exists/2,
ensure_local_purge_docs/2,
maybe_create_local_purge_doc/2,
Expand Down Expand Up @@ -98,6 +100,22 @@ design_doc_to_index(DbName, #doc{id = Id, body = {Fields}}, IndexName) ->
{error, InvalidDDocError}
end.

active_sigs(DbName) when is_binary(DbName) ->
couch_util:with_db(DbName, fun active_sigs/1);
active_sigs(Db) ->
DbName = couch_db:name(Db),
{ok, DesignDocs} = couch_db:get_design_docs(Db),
lists:usort(
lists:flatmap(
fun(Doc) -> active_sigs(DbName, Doc) end,
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
)
).

active_sigs(DbName, #doc{} = Doc) ->
Indexes = design_doc_to_indexes(DbName, Doc),
lists:map(fun(Index) -> Index#index.sig end, Indexes).

verify_index_exists(DbName, Props) ->
try
Type = couch_util:get_value(<<"type">>, Props),
Expand Down
5 changes: 5 additions & 0 deletions src/smoosh/src/smoosh_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ handle_db_event(DbName, local_updated, St) ->
handle_db_event(DbName, updated, St) ->
enqueue(DbName),
{ok, St};
handle_db_event(DbName, ddoc_updated, St) ->
enqueue({?INDEX_CLEANUP, mem3:dbname(DbName)}),
{ok, St};
handle_db_event(DbName, {index_commit, IdxName}, St) ->
enqueue({DbName, IdxName}),
{ok, St};
Expand Down Expand Up @@ -342,6 +345,8 @@ find_channel(#state{} = State, [Channel | Rest], Object) ->
find_channel(State, Rest, Object)
end.

stale_enough({?INDEX_CLEANUP, _}) ->
true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ddoc_updated events are rare, unlike database updates, so choosing not to action one would leave unreferenced indexes on disk for a long time, perhaps indefinitely.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're also triggering these during every shard compaction attempt, proportionally to the number of shards (so we'd roughly trigger once for each clustered db). That should be a low enough rate, it may be worth checking what it looks like a busy cluster.

stale_enough(Object) ->
LastUpdatedSec = last_updated(Object),
Staleness = erlang:monotonic_time(second) - LastUpdatedSec,
Expand Down