-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Fix archival for cascading deletes by archiving dependent tables first #51952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4c2502b
enhance cleanup logic to archive dependent tables first
vatsrahul1001 2012c88
adding flag to just archive fk tables and let cascade take care of de…
vatsrahul1001 0ff24d7
add test
vatsrahul1001 07206f4
Merge branch 'main' into enhance-db-cleanup
vatsrahul1001 182975c
Merge branch 'main' into enhance-db-cleanup
vatsrahul1001 1f5f481
Adding hardcoded FK details for tables
vatsrahul1001 a2e9ac1
Merge branch 'enhance-db-cleanup' of github.com:astronomer/airflow in…
vatsrahul1001 0b37b80
fix table removal by typo
vatsrahul1001 4d867fa
remove task rechedule from archival
vatsrahul1001 ad9c1a3
add only direct dependency and let code find all indirects
vatsrahul1001 aac0e37
Merge branch 'main' into enhance-db-cleanup
vatsrahul1001 2325921
update logic to find dependent table sin _effective_table_names method
vatsrahul1001 f30788a
Merge branch 'enhance-db-cleanup' of github.com:astronomer/airflow in…
vatsrahul1001 5bfdb36
Refactor how we loop over the tables
jedcunningham 827ba5e
adding deadline table as dependent to dag_run
vatsrahul1001 2249725
Be loud if we have a dependent misconfigured
jedcunningham e5ae70f
Fix detection of unknown tables in table list
jedcunningham a490383
adding deadline table as dependent to dag
vatsrahul1001 df11f3b
Remove skip_delete
jedcunningham e0a6ea8
Update airflow-core/src/airflow/utils/db_cleanup.py
jedcunningham 685c629
Merge branch 'main' into enhance-db-cleanup
jedcunningham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ class _TableConfig: | |
in the table. to ignore certain records even if they are the latest in the table, you can | ||
supply additional filters here (e.g. externally triggered dag runs) | ||
:param keep_last_group_by: if keeping the last record, can keep the last record for each group | ||
:param dependent_tables: list of tables which have FK relationship with this table | ||
""" | ||
|
||
table_name: str | ||
|
@@ -81,6 +82,10 @@ class _TableConfig: | |
keep_last: bool = False | ||
keep_last_filters: Any | None = None | ||
keep_last_group_by: Any | None = None | ||
# We explicitly list these tables instead of detecting foreign keys automatically, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
# because the relationships are unlikely to change and the number of tables is small. | ||
# Relying on automation here would increase complexity and reduce maintainability. | ||
dependent_tables: list[str] | None = None | ||
vatsrahul1001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __post_init__(self): | ||
self.recency_column = column(self.recency_column_name) | ||
|
@@ -104,29 +109,46 @@ def readable_config(self): | |
|
||
config_list: list[_TableConfig] = [ | ||
_TableConfig(table_name="job", recency_column_name="latest_heartbeat"), | ||
_TableConfig(table_name="dag", recency_column_name="last_parsed_time"), | ||
_TableConfig( | ||
table_name="dag", | ||
recency_column_name="last_parsed_time", | ||
dependent_tables=["dag_version", "deadline"], | ||
), | ||
_TableConfig( | ||
table_name="dag_run", | ||
recency_column_name="start_date", | ||
extra_columns=["dag_id", "run_type"], | ||
keep_last=True, | ||
keep_last_filters=[column("run_type") != DagRunType.MANUAL], | ||
keep_last_group_by=["dag_id"], | ||
dependent_tables=["task_instance", "deadline"], | ||
), | ||
_TableConfig(table_name="asset_event", recency_column_name="timestamp"), | ||
_TableConfig(table_name="import_error", recency_column_name="timestamp"), | ||
_TableConfig(table_name="log", recency_column_name="dttm"), | ||
_TableConfig(table_name="sla_miss", recency_column_name="timestamp"), | ||
_TableConfig(table_name="task_instance", recency_column_name="start_date"), | ||
_TableConfig( | ||
table_name="task_instance", | ||
recency_column_name="start_date", | ||
dependent_tables=["task_instance_history", "xcom"], | ||
), | ||
_TableConfig(table_name="task_instance_history", recency_column_name="start_date"), | ||
_TableConfig(table_name="task_reschedule", recency_column_name="start_date"), | ||
_TableConfig(table_name="xcom", recency_column_name="timestamp"), | ||
_TableConfig(table_name="_xcom_archive", recency_column_name="timestamp"), | ||
_TableConfig(table_name="callback_request", recency_column_name="created_at"), | ||
_TableConfig(table_name="celery_taskmeta", recency_column_name="date_done"), | ||
_TableConfig(table_name="celery_tasksetmeta", recency_column_name="date_done"), | ||
_TableConfig(table_name="trigger", recency_column_name="created_date"), | ||
_TableConfig(table_name="dag_version", recency_column_name="created_at"), | ||
_TableConfig( | ||
table_name="trigger", | ||
recency_column_name="created_date", | ||
dependent_tables=["task_instance"], | ||
), | ||
_TableConfig( | ||
table_name="dag_version", | ||
recency_column_name="created_at", | ||
dependent_tables=["task_instance", "dag_run"], | ||
), | ||
_TableConfig(table_name="deadline", recency_column_name="deadline_time"), | ||
] | ||
|
||
|
@@ -234,6 +256,7 @@ def _do_delete( | |
logger.debug("delete statement:\n%s", delete.compile()) | ||
session.execute(delete) | ||
session.commit() | ||
|
||
except BaseException as e: | ||
raise e | ||
finally: | ||
|
@@ -414,17 +437,37 @@ def _suppress_with_logging(table: str, session: Session): | |
session.rollback() | ||
|
||
|
||
def _effective_table_names(*, table_names: list[str] | None) -> tuple[set[str], dict[str, _TableConfig]]: | ||
def _effective_table_names(*, table_names: list[str] | None) -> tuple[list[str], dict[str, _TableConfig]]: | ||
desired_table_names = set(table_names or config_dict) | ||
effective_config_dict = {k: v for k, v in config_dict.items() if k in desired_table_names} | ||
effective_table_names = set(effective_config_dict) | ||
if desired_table_names != effective_table_names: | ||
outliers = desired_table_names - effective_table_names | ||
|
||
outliers = desired_table_names - set(config_dict.keys()) | ||
if outliers: | ||
logger.warning( | ||
"The following table(s) are not valid choices and will be skipped: %s", sorted(outliers) | ||
"The following table(s) are not valid choices and will be skipped: %s", | ||
sorted(outliers), | ||
) | ||
if not effective_table_names: | ||
desired_table_names = desired_table_names - outliers | ||
|
||
visited: set[str] = set() | ||
effective_table_names: list[str] = [] | ||
|
||
def collect_deps(table: str): | ||
if table in visited: | ||
return | ||
visited.add(table) | ||
config = config_dict[table] | ||
for dep in config.dependent_tables or []: | ||
collect_deps(dep) | ||
effective_table_names.append(table) | ||
|
||
for table_name in desired_table_names: | ||
collect_deps(table_name) | ||
|
||
effective_config_dict = {n: config_dict[n] for n in effective_table_names} | ||
|
||
if not effective_config_dict: | ||
raise SystemExit("No tables selected for db cleanup. Please choose valid table names.") | ||
|
||
return effective_table_names, effective_config_dict | ||
|
||
|
||
|
@@ -480,6 +523,8 @@ def run_cleanup( | |
:param session: Session representing connection to the metadata database. | ||
""" | ||
clean_before_timestamp = timezone.coerce_datetime(clean_before_timestamp) | ||
|
||
# Get all tables to clean (root + dependents) | ||
effective_table_names, effective_config_dict = _effective_table_names(table_names=table_names) | ||
if dry_run: | ||
print("Performing dry run for db cleanup.") | ||
|
@@ -491,6 +536,7 @@ def run_cleanup( | |
if not dry_run and confirm: | ||
_confirm_delete(date=clean_before_timestamp, tables=sorted(effective_table_names)) | ||
existing_tables = reflect_tables(tables=None, session=session).tables | ||
|
||
for table_name, table_config in effective_config_dict.items(): | ||
if table_name in existing_tables: | ||
with _suppress_with_logging(table_name, session): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.