Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Skip the create filtered index steps that expect an index to already exist (#2106)

Madison Swain-Bowdene290157a7202
diff --git a/catalog/dags/common/ingestion_server.py b/catalog/dags/common/ingestion_server.py
index e31e74e0c..07043466d 100644
--- a/catalog/dags/common/ingestion_server.py
+++ b/catalog/dags/common/ingestion_server.py
@@ -3,7 +3,7 @@ import os
from datetime import timedelta
from urllib.parse import urlparse
-from airflow.exceptions import AirflowException
+from airflow.exceptions import AirflowSkipException
from airflow.providers.http.operators.http import SimpleHttpOperator
from airflow.providers.http.sensors.http import HttpSensor
from requests import Response
@@ -24,7 +24,10 @@ def response_filter_stat(response: Response) -> str:
This is used to extract the name of the current index that the concerned alias
points to. This index name will be available via XCom in the downstream tasks.
"""
- index_name = response.json()["alt_names"]
+ data = response.json()
+ if not (data["exists"]):
+ raise AirflowSkipException("Index does not exist.")
+ index_name = data["alt_names"]
# Indices are named as '<media type>-<suffix>', so everything after the first
# hyphen '-' is the suffix.
_, index_suffix = index_name.split("-", maxsplit=1)
@@ -56,9 +59,7 @@ def response_check_wait_for_completion(response: Response) -> bool:
return False
if data["error"]:
- raise AirflowException(
- "Ingestion server encountered an error during data refresh."
- )
+ raise ValueError("Ingestion server encountered an error during data refresh.")
logger.info(f"Data refresh done with {data['progress']}% completed.")
return True
diff --git a/catalog/dags/data_refresh/create_filtered_index_dag.py b/catalog/dags/data_refresh/create_filtered_index_dag.py
index 9c9a76f23..b2978376b 100644
--- a/catalog/dags/data_refresh/create_filtered_index_dag.py
+++ b/catalog/dags/data_refresh/create_filtered_index_dag.py
@@ -57,7 +57,9 @@ from airflow import DAG
from airflow.decorators import task
from airflow.exceptions import AirflowSensorTimeout
from airflow.models.param import Param
+from airflow.operators.empty import EmptyOperator
from airflow.sensors.external_task import ExternalTaskSensor
+from airflow.utils.trigger_rule import TriggerRule
from common import ingestion_server
from common.constants import DAG_DEFAULT_ARGS, XCOM_PULL_TEMPLATE
@@ -197,9 +199,9 @@ def filtered_index_creation_dag_factory(data_refresh: DataRefresh):
"{{ params.destination_index_suffix }}"
)
- get_current_index = ingestion_server.get_current_index(target_alias)
+ get_current_index_if_exists = ingestion_server.get_current_index(target_alias)
- do_create = create_and_populate_filtered_index(
+ do_create, await_create = create_and_populate_filtered_index(
origin_index_suffix="{{ params.origin_index_suffix }}",
destination_index_suffix=destination_index_suffix,
)
@@ -211,20 +213,32 @@ def filtered_index_creation_dag_factory(data_refresh: DataRefresh):
model=data_refresh.media_type,
data={
"index_suffix": XCOM_PULL_TEMPLATE.format(
- get_current_index.task_id, "return_value"
+ get_current_index_if_exists.task_id, "return_value"
),
},
)
- (
- prevent_concurrency
- >> destination_index_suffix
- >> get_current_index
- >> do_create
- >> do_point_alias
- >> delete_old_index
+ # Once concurrency has been checked against, determine the destination index
+ # suffix and get the current index. The current index retrieval has to happen
+ # prior to any of the index creation steps to ensure the appropriate index
+ # information is retrieved.
+ prevent_concurrency >> [get_current_index_if_exists, destination_index_suffix]
+
+ # The current index retrieval step can be skipped if the index does not
+ # currently exist. The empty operator below works as a control flow management
+ # step to ensure the create step runs even if the current index retrieval step
+ # is skipped (the trigger rule would be tedious to percolate through all the
+ # helper functions to the index creation step itself).
+ continue_if_no_current_index = EmptyOperator(
+ task_id="continue_if_no_current_index",
+ trigger_rule=TriggerRule.NONE_FAILED,
)
+ get_current_index_if_exists >> continue_if_no_current_index >> do_create
+ await_create >> do_point_alias
+
+ [get_current_index_if_exists, do_point_alias] >> delete_old_index
+
return dag
diff --git a/catalog/tests/dags/common/test_ingestion_server.py b/catalog/tests/dags/common/test_ingestion_server.py
new file mode 100644
index 000000000..086a0519e
--- /dev/null
+++ b/catalog/tests/dags/common/test_ingestion_server.py
@@ -0,0 +1,25 @@
+from unittest.mock import MagicMock
+
+import pytest
+from airflow.exceptions import AirflowSkipException
+
+from common import ingestion_server
+
+
+ "data, expected",
+ [
+ ({"exists": True, "is_alias": False, "alt_names": "asdf-1234"}, "1234"),
+ pytest.param(
+ {"exists": False, "is_alias": None, "alt_names": None},
+ None,
+ marks=pytest.mark.raises(exception=AirflowSkipException),
+ ),
+ pytest.param({}, None, marks=pytest.mark.raises(exception=KeyError)),
+ ],
+)
+def test_response_filter_stat(data, expected):
+ response = MagicMock()
+ response.json.return_value = data
+ actual = ingestion_server.response_filter_stat(response)
+ assert actual == expected