Skip to content

Commit 8ec19b1

Browse files
committed
enhance smoosh to cleanup search indexes when ddocs change
1 parent 64faef7 commit 8ec19b1

File tree

6 files changed

+74
-40
lines changed

6 files changed

+74
-40
lines changed

src/dreyfus/src/dreyfus_fabric_cleanup.erl

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,17 @@
1414

1515
-module(dreyfus_fabric_cleanup).
1616

17-
-include("dreyfus.hrl").
1817
-include_lib("mem3/include/mem3.hrl").
1918
-include_lib("couch/include/couch_db.hrl").
2019

2120
-export([go/1]).
2221

2322
go(DbName) ->
24-
{ok, DesignDocs} = fabric:design_docs(DbName),
25-
ActiveSigs = lists:usort(
26-
lists:flatmap(
27-
fun active_sigs/1,
28-
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
29-
)
30-
),
23+
ActiveSigs = dreyfus_util:active_sigs(DbName),
3124
cleanup_local_purge_doc(DbName, ActiveSigs),
3225
clouseau_rpc:cleanup(DbName, ActiveSigs),
3326
ok.
3427

35-
active_sigs(#doc{body = {Fields}} = Doc) ->
36-
try
37-
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
38-
{IndexNames, _} = lists:unzip(RawIndexes),
39-
[
40-
begin
41-
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
42-
Index#index.sig
43-
end
44-
|| IndexName <- IndexNames
45-
]
46-
catch
47-
error:{badmatch, _Error} ->
48-
[]
49-
end.
50-
5128
cleanup_local_purge_doc(DbName, ActiveSigs) ->
5229
{ok, BaseDir} = clouseau_rpc:get_root_dir(),
5330
DbNamePattern = <<DbName/binary, ".*">>,

src/dreyfus/src/dreyfus_util.erl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
maybe_create_local_purge_doc/2,
3030
maybe_create_local_purge_doc/3,
3131
get_signature_from_idxdir/1,
32-
verify_index_exists/2
32+
verify_index_exists/2,
33+
active_sigs/1
3334
]).
3435

3536
get_shards(DbName, #index_query_args{partition = nil} = Args) ->
@@ -428,6 +429,33 @@ verify_index_exists(DbName, Props) ->
428429
false
429430
end.
430431

432+
active_sigs(DbName) when is_binary(DbName) ->
433+
couch_util:with_db(DbName, fun active_sigs/1);
434+
active_sigs(Db) ->
435+
{ok, DesignDocs} = couch_db:get_design_docs(Db),
436+
lists:usort(
437+
lists:flatmap(
438+
fun sigs_from_ddoc/1,
439+
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
440+
)
441+
).
442+
443+
sigs_from_ddoc(#doc{body = {Fields}} = Doc) ->
444+
try
445+
{RawIndexes} = couch_util:get_value(<<"indexes">>, Fields, {[]}),
446+
{IndexNames, _} = lists:unzip(RawIndexes),
447+
[
448+
begin
449+
{ok, Index} = dreyfus_index:design_doc_to_index(Doc, IndexName),
450+
Index#index.sig
451+
end
452+
|| IndexName <- IndexNames
453+
]
454+
catch
455+
error:{badmatch, _Error} ->
456+
[]
457+
end.
458+
431459
-ifdef(TEST).
432460
-include_lib("eunit/include/eunit.hrl").
433461

src/fabric/src/fabric.erl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -590,25 +590,38 @@ cleanup_index_files(DbName) ->
590590
cleanup_local_indices_and_purge_checkpoints([]) ->
591591
ok;
592592
cleanup_local_indices_and_purge_checkpoints([_ | _] = Dbs) ->
593-
AllIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs),
594-
AllPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs),
595-
Sigs = couch_mrview_util:get_signatures(hd(Dbs)),
596-
ok = cleanup_purges(Sigs, AllPurges, Dbs),
597-
ok = cleanup_indices(Sigs, AllIndices).
593+
MrViewIndices = lists:map(fun couch_mrview_util:get_index_files/1, Dbs),
594+
MrViewPurges = lists:map(fun couch_mrview_util:get_purge_checkpoints/1, Dbs),
595+
MrViewSigs = couch_mrview_util:get_signatures(hd(Dbs)),
596+
ok = cleanup_mrview_purges(MrViewSigs, MrViewPurges, Dbs),
597+
ok = cleanup_mrview_indices(MrViewSigs, MrViewIndices),
598598

599-
cleanup_purges(Sigs, AllPurges, Dbs) ->
599+
ClouseauSigs = dreyfus_util:active_sigs(hd(Dbs)),
600+
ok = cleanup_clouseau_indices(Dbs, ClouseauSigs),
601+
602+
NouveauSigs = nouveau_util:active_sigs(hd(Dbs)),
603+
ok = cleanup_nouveau_indices(Dbs, NouveauSigs).
604+
605+
cleanup_mrview_purges(Sigs, AllPurges, Dbs) ->
600606
Fun = fun(DbPurges, Db) ->
601607
couch_mrview_cleanup:cleanup_purges(Db, Sigs, DbPurges)
602608
end,
603609
lists:zipwith(Fun, AllPurges, Dbs),
604610
ok.
605611

606-
cleanup_indices(Sigs, AllIndices) ->
612+
cleanup_mrview_indices(Sigs, AllIndices) ->
607613
Fun = fun(DbIndices) ->
608614
couch_mrview_cleanup:cleanup_indices(Sigs, DbIndices)
609615
end,
610616
lists:foreach(Fun, AllIndices).
611617

618+
cleanup_clouseau_indices(Dbs, ActiveSigs) ->
619+
Fun = fun(Db) -> clouseau_rpc:cleanup(Db, ActiveSigs) end,
620+
lists:foreach(Fun, Dbs).
621+
cleanup_nouveau_indices(Dbs, ActiveSigs) ->
622+
Fun = fun(Db) -> nouveau_api:delete_path(nouveau_util:index_name(Db), ActiveSigs) end,
623+
lists:foreach(Fun, Dbs).
624+
612625
%% @doc clean up index files for a specific db on all nodes
613626
-spec cleanup_index_files_all_nodes(dbname()) -> [reference()].
614627
cleanup_index_files_all_nodes(DbName) ->

src/nouveau/src/nouveau_fabric_cleanup.erl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
-module(nouveau_fabric_cleanup).
1616

17-
-include_lib("couch/include/couch_db.hrl").
18-
19-
-include("nouveau.hrl").
2017
-include_lib("mem3/include/mem3.hrl").
2118

2219
-export([go/1]).
@@ -26,7 +23,7 @@ go(DbName) ->
2623
ActiveSigs =
2724
lists:usort(
2825
lists:flatmap(
29-
fun(Doc) -> active_sigs(DbName, Doc) end,
26+
fun(Doc) -> nouveau_util:active_sigs(DbName, Doc) end,
3027
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
3128
)
3229
),
@@ -37,7 +34,3 @@ go(DbName) ->
3734
end,
3835
Shards
3936
).
40-
41-
active_sigs(DbName, #doc{} = Doc) ->
42-
Indexes = nouveau_util:design_doc_to_indexes(DbName, Doc),
43-
lists:map(fun(Index) -> Index#index.sig end, Indexes).

src/nouveau/src/nouveau_util.erl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
index_name/1,
2323
design_doc_to_indexes/2,
2424
design_doc_to_index/3,
25+
active_sigs/1,
26+
active_sigs/2,
2527
nouveau_url/0
2628
]).
2729

@@ -93,5 +95,21 @@ design_doc_to_index(DbName, #doc{id = Id, body = {Fields}}, IndexName) ->
9395
{error, InvalidDDocError}
9496
end.
9597

98+
active_sigs(DbName) when is_binary(DbName) ->
99+
couch_util:with_db(DbName, fun active_sigs/1);
100+
active_sigs(Db) ->
101+
DbName = couch_db:name(Db),
102+
{ok, DesignDocs} = couch_db:get_design_docs(Db),
103+
lists:usort(
104+
lists:flatmap(
105+
fun(Doc) -> active_sigs(DbName, Doc) end,
106+
[couch_doc:from_json_obj(DD) || DD <- DesignDocs]
107+
)
108+
).
109+
110+
active_sigs(DbName, #doc{} = Doc) ->
111+
Indexes = design_doc_to_indexes(DbName, Doc),
112+
lists:map(fun(Index) -> Index#index.sig end, Indexes).
113+
96114
nouveau_url() ->
97115
config:get("nouveau", "url", "http://127.0.0.1:8080").

src/smoosh/src/smoosh_server.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ handle_db_event(DbName, local_updated, St) ->
123123
handle_db_event(DbName, updated, St) ->
124124
enqueue(DbName),
125125
{ok, St};
126+
handle_db_event(DbName, ddoc_updated, St) ->
127+
enqueue({?INDEX_CLEANUP, mem3:dbname(DbName)}),
128+
{ok, St};
126129
handle_db_event(DbName, {index_commit, IdxName}, St) ->
127130
enqueue({DbName, IdxName}),
128131
{ok, St};
@@ -342,6 +345,8 @@ find_channel(#state{} = State, [Channel | Rest], Object) ->
342345
find_channel(State, Rest, Object)
343346
end.
344347

348+
stale_enough({?INDEX_CLEANUP, _}) ->
349+
true;
345350
stale_enough(Object) ->
346351
LastUpdatedSec = last_updated(Object),
347352
Staleness = erlang:monotonic_time(second) - LastUpdatedSec,

0 commit comments

Comments
 (0)