Skip to content

Commit 69cb68e

Browse files
authored
Ignore NotFoundErrors in switch_indices function (#1654)
1 parent effca27 commit 69cb68e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

learning_resources_search/indexing_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,12 @@ def switch_indices(backing_index, object_type):
560560
conn.indices.delete(index)
561561

562562
# Finally, remove the link to the reindexing alias
563-
conn.indices.delete_alias(
564-
name=get_reindexing_alias_name(object_type), index=backing_index
565-
)
563+
try:
564+
conn.indices.delete_alias(
565+
name=get_reindexing_alias_name(object_type), index=backing_index
566+
)
567+
except NotFoundError:
568+
log.warning("Reindex alias not found for %s", object_type)
566569

567570

568571
def delete_orphaned_indexes(obj_types, delete_reindexing_tags):

learning_resources_search/indexing_api_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,23 @@ def test_clear_and_create_index(mocked_es, object_type, skip_mapping, already_ex
108108

109109
@pytest.mark.parametrize("object_type", [COURSE_TYPE, PROGRAM_TYPE])
110110
@pytest.mark.parametrize("default_exists", [True, False])
111-
def test_switch_indices(mocked_es, mocker, default_exists, object_type):
111+
@pytest.mark.parametrize("alias_exists", [True, False])
112+
def test_switch_indices(mocked_es, mocker, default_exists, alias_exists, object_type):
112113
"""
113114
switch_indices should atomically remove the old backing index
114115
for the default alias and replace it with the new one
115116
"""
117+
mock_log = mocker.patch("learning_resources_search.indexing_api.log.warning")
116118
refresh_mock = mocker.patch(
117119
"learning_resources_search.indexing_api.refresh_index", autospec=True
118120
)
119121
conn_mock = mocked_es.conn
120122
conn_mock.indices.exists_alias.return_value = default_exists
121123
old_backing_index = "old_backing"
122124
conn_mock.indices.get_alias.return_value.keys.return_value = [old_backing_index]
123-
125+
conn_mock.indices.delete_alias.side_effect = (
126+
None if alias_exists else NotFoundError()
127+
)
124128
backing_index = "backing"
125129
switch_indices(backing_index, object_type)
126130

@@ -155,6 +159,10 @@ def test_switch_indices(mocked_es, mocker, default_exists, object_type):
155159
conn_mock.indices.delete_alias.assert_called_once_with(
156160
name=get_reindexing_alias_name(object_type), index=backing_index
157161
)
162+
if not alias_exists:
163+
mock_log.assert_called_once_with("Reindex alias not found for %s", object_type)
164+
else:
165+
mock_log.assert_not_called()
158166

159167

160168
@pytest.mark.parametrize("temp_alias_exists", [True, False])

0 commit comments

Comments
 (0)