Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Give the data refresh index creation its own timeout and make it resumable (#5856)
Olga Bulat16501c341e4b
diff --git a/catalog/dags/data_refresh/data_refresh_types.py b/catalog/dags/data_refresh/data_refresh_types.pyindex 960d90e43..438c11844 100644--- a/catalog/dags/data_refresh/data_refresh_types.py+++ b/catalog/dags/data_refresh/data_refresh_types.py@@ -76,6 +76,9 @@ class DataRefreshConfig:copy the upstream table into the downstream DBadd_primary_key_timeout: timedelta expressing the amount of time it may take toadd the primary key to the temp table+ create_indices_timeout: timedelta expressing the amount of time it may take to+ create all the indices of a temp table, one after+ anotheralter_data_batch_size: int number of records to process per batch in alter_datatasksalter_data_timeout: timedelta expressing the amount of time it may take to@@ -106,6 +109,7 @@ class DataRefreshConfig:dag_timeout: timedelta = timedelta(days=1)copy_data_timeout: timedelta = timedelta(hours=1)add_primary_key_timeout: timedelta = timedelta(hours=1)+ create_indices_timeout: timedelta = timedelta(hours=1)alter_data_batch_size: int = DATA_REFRESH_ALTER_BATCH_SIZEalter_data_timeout: timedelta = timedelta(hours=1)indexer_worker_timeout: timedelta = timedelta(hours=12)@@ -138,6 +142,7 @@ DATA_REFRESH_CONFIGS = {dag_timeout=timedelta(days=6),copy_data_timeout=timedelta(hours=12),add_primary_key_timeout=timedelta(hours=12),+ create_indices_timeout=timedelta(days=1),alter_data_timeout=timedelta(hours=12),indexer_worker_timeout=timedelta(days=3),concurrency_check_poke_interval=int(diff --git a/catalog/dags/data_refresh/promote_table.py b/catalog/dags/data_refresh/promote_table.pyindex 6a0da5c55..24697072d 100644--- a/catalog/dags/data_refresh/promote_table.py+++ b/catalog/dags/data_refresh/promote_table.py@@ -8,6 +8,7 @@ In this TaskGroup, indices and constraints from the original table are applied timport loggingfrom dataclasses import asdict+from datetime import timedeltafrom airflow.sdk import task, task_group@@ -53,12 +54,14 @@ def promote_table(postgres_conn_id: str,downstream_table_name: str,temp_table_name: str,+ create_indices_timeout: timedelta,):# Recreate indices from the original table.remap_table_indices = remap_table_indices_to_table(postgres_conn_id=postgres_conn_id,table_name=downstream_table_name,temp_table_name=temp_table_name,+ create_indices_timeout=create_indices_timeout,)# Recreate constraints from the original table.@@ -96,4 +99,5 @@ def promote_tables(promote_table.partial(postgres_conn_id=downstream_conn_id,+ create_indices_timeout=data_refresh_config.create_indices_timeout,).expand_kwargs([asdict(tm) for tm in data_refresh_config.table_mappings])diff --git a/catalog/dags/data_refresh/remap_table_indices.py b/catalog/dags/data_refresh/remap_table_indices.pyindex 6cb7cc504..9748857c5 100644--- a/catalog/dags/data_refresh/remap_table_indices.py+++ b/catalog/dags/data_refresh/remap_table_indices.py@@ -5,6 +5,7 @@ This file contains TaskGroups related to rmampping the table indices from the li"""import logging+from datetime import timedeltafrom typing import TypedDictfrom airflow.sdk import task, task_group@@ -79,8 +80,18 @@ def transform_index_defs(@task(map_index_template="{{ task.op_kwargs['table_name'] }}")def create_table_indices(- postgres_conn_id: str, table_name: str, index_configs: list[TableIndex]+ postgres_conn_id: str,+ table_name: str,+ index_configs: list[TableIndex],+ statement_timeout: float | None = None,):+ """+ Create the indices on the temp table one at a time.++ An index that already exists is skipped, so a retry resumes after the last+ index that finished instead of failing on the first one. Each statement may run+ for as long as `statement_timeout` seconds, rather than the one hour default.+ """logger.info(f"Creating indices for `{table_name}`.'")for index_config in index_configs:if "(id)" in index_config["index_def"]:@@ -90,7 +101,11 @@ def create_table_indices(# Create the indexrun_sql.function(- postgres_conn_id=postgres_conn_id, sql_template=index_config["index_def"]+ postgres_conn_id=postgres_conn_id,+ sql_template=index_config["index_def"].replace(+ " INDEX ", " INDEX IF NOT EXISTS ", 1+ ),+ timeout=statement_timeout,)return index_configs@@ -98,7 +113,10 @@ def create_table_indices(@task_groupdef remap_table_indices_to_table(- table_name: str, temp_table_name: str, postgres_conn_id: str+ table_name: str,+ temp_table_name: str,+ postgres_conn_id: str,+ create_indices_timeout: timedelta,):"""Apply all indices on the main media table to the temp table. Indices will be given@@ -123,10 +141,11 @@ def remap_table_indices_to_table()# Actually create the new indices on the temp table- indices = create_table_indices(+ indices = create_table_indices.override(execution_timeout=create_indices_timeout)(postgres_conn_id=postgres_conn_id,table_name=table_name,index_configs=new_index_configs,+ statement_timeout=create_indices_timeout.total_seconds(),)# Return the information for the newly created indices, so that they can laterdiff --git a/catalog/tests/dags/data_refresh/test_remap_table_indices.py b/catalog/tests/dags/data_refresh/test_remap_table_indices.pyindex daac087a7..7503facfe 100644--- a/catalog/tests/dags/data_refresh/test_remap_table_indices.py+++ b/catalog/tests/dags/data_refresh/test_remap_table_indices.py@@ -86,8 +86,8 @@ def test_transform_index_defs(existing_index_defs, expected_index_defs):),],[- "CREATE INDEX temp_import_image_provider_7d11f847 ON"- " public.temp_import_image USING btree (provider)",+ "CREATE INDEX IF NOT EXISTS temp_import_image_provider_7d11f847"+ " ON public.temp_import_image USING btree (provider)",],),# If there are multiple indices, run_sql is called for all except the pk@@ -113,10 +113,10 @@ def test_transform_index_defs(existing_index_defs, expected_index_defs):),],[- "CREATE INDEX temp_import_image_provider_7d11f847 ON"- " public.temp_import_image USING btree (provider)",- "CREATE UNIQUE INDEX temp_import_image_identifier_key ON"- " public.temp_import_image USING btree (identifier)",+ "CREATE INDEX IF NOT EXISTS temp_import_image_provider_7d11f847"+ " ON public.temp_import_image USING btree (provider)",+ "CREATE UNIQUE INDEX IF NOT EXISTS temp_import_image_identifier_key"+ " ON public.temp_import_image USING btree (identifier)",],),],@@ -124,14 +124,18 @@ def test_transform_index_defs(existing_index_defs, expected_index_defs):def test_create_table_indices(index_configs, expected_sql):with patch("common.sql.run_sql.function") as run_sql_mock:create_table_indices.function(- postgres_conn_id="test_id", index_configs=index_configs, table_name="image"+ postgres_conn_id="test_id",+ index_configs=index_configs,+ table_name="image",+ statement_timeout=7200.0,)expected_calls = [- call(postgres_conn_id="test_id", sql_template=sql) for sql in expected_sql+ call(postgres_conn_id="test_id", sql_template=sql, timeout=7200.0)+ for sql in expected_sql]- run_sql_mock.assert_has_calls(expected_calls)+ assert run_sql_mock.call_args_list == expected_callsdef test_index_configs_survive_xcom():@@ -152,8 +156,9 @@ def test_index_configs_survive_xcom():)run_sql_mock.assert_called_once_with(postgres_conn_id="test_id",- sql_template="CREATE INDEX temp_import_image_provider_7d11f847 ON"- " public.temp_import_image USING btree (provider)",+ sql_template="CREATE INDEX IF NOT EXISTS temp_import_image_provider_7d11f847"+ " ON public.temp_import_image USING btree (provider)",+ timeout=None,)go_live_query = get_go_live_query.function(