summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjiangph <jiangph@cn.ibm.com>2020-09-01 16:26:20 +0800
committerjiangph <jiangph@cn.ibm.com>2020-09-01 16:26:20 +0800
commitb69a11a14acae8ece68244b079d3329e60be4e70 (patch)
tree32b7f8e4613ad2e724b5e6839ae2f3c07f4b87ee
parentbb869c1c27354550309f025f98a49b193a492a1f (diff)
downloadcouchdb-b69a11a14acae8ece68244b079d3329e60be4e70.tar.gz
Allow to continue to cleanup search index even if there is invalid ddoc
In some situation where design document for search index created by customer is not valid, the _search_cleanup endpoint will stop to clean up. This will leave some search index orphan. The change is to allow to continue to clean up search index even if there is invalid design document for search.
-rw-r--r--src/dreyfus/src/dreyfus_fabric_cleanup.erl16
-rw-r--r--src/dreyfus/test/elixir/test/search_test.exs25
2 files changed, 35 insertions, 6 deletions
diff --git a/src/dreyfus/src/dreyfus_fabric_cleanup.erl b/src/dreyfus/src/dreyfus_fabric_cleanup.erl
index 2840a2f2d..681712748 100644
--- a/src/dreyfus/src/dreyfus_fabric_cleanup.erl
+++ b/src/dreyfus/src/dreyfus_fabric_cleanup.erl
@@ -30,12 +30,16 @@ go(DbName) ->
ok.
active_sigs(#doc{body={Fields}}=Doc) ->
- {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].
+ 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(),
diff --git a/src/dreyfus/test/elixir/test/search_test.exs b/src/dreyfus/test/elixir/test/search_test.exs
index e524a5cf4..829b3395f 100644
--- a/src/dreyfus/test/elixir/test/search_test.exs
+++ b/src/dreyfus/test/elixir/test/search_test.exs
@@ -37,6 +37,20 @@ defmodule SearchTest do
assert Map.has_key?(resp.body, "ok") == true
end
+ def create_invalid_ddoc(db_name, opts \\ %{}) do
+ invalid_ddoc = %{
+ :indexes => [
+ %{"name" => "foo", "ddoc" => "bar", "type" => "text"},
+ ]
+ }
+
+ ddoc = Enum.into(opts, invalid_ddoc)
+
+ resp = Couch.put("/#{db_name}/_design/search", body: ddoc)
+ assert resp.status_code in [201, 202]
+ assert Map.has_key?(resp.body, "ok") == true
+ end
+
def get_items (resp) do
%{:body => %{"rows" => rows}} = resp
Enum.map(rows, fn row -> row["doc"]["item"] end)
@@ -198,4 +212,15 @@ defmodule SearchTest do
ids = get_items(resp)
assert Enum.sort(ids) == ["apple"]
end
+
+ @tag :with_db
+ test "clean up search index with invalid design document", context do
+ db_name = context[:db_name]
+ create_search_docs(db_name)
+ create_ddoc(db_name)
+ create_invalid_ddoc(db_name)
+
+ resp = Couch.post("/#{db_name}/_search_cleanup")
+ assert resp.status_code in [201, 202]
+ end
end