Skip to content
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

Only orphan non-orphaned Datasets #40806

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow/jobs/scheduler_job_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,7 @@ def _orphan_unreferenced_datasets(self, session: Session = NEW_SESSION) -> None:
isouter=True,
)
.group_by(DatasetModel.id)
.where(~DatasetModel.is_orphaned)
shahar1 marked this conversation as resolved.
Show resolved Hide resolved
.having(
and_(
func.count(DagScheduleDatasetReference.dag_id) == 0,
Expand Down
42 changes: 42 additions & 0 deletions tests/jobs/test_scheduler_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5796,6 +5796,48 @@ def test_dataset_orphaning(self, dag_maker, session):
]
assert orphaned_datasets == ["ds2", "ds4"]

def test_dataset_orphaning_ignore_orphaned_datasets(self, dag_maker, session):
dataset1 = Dataset(uri="ds1")

with dag_maker(dag_id="datasets-1", schedule=[dataset1], session=session):
BashOperator(task_id="task", bash_command="echo 1")

non_orphaned_dataset_count = session.query(DatasetModel).filter(~DatasetModel.is_orphaned).count()
assert non_orphaned_dataset_count == 1
orphaned_dataset_count = session.query(DatasetModel).filter(DatasetModel.is_orphaned).count()
assert orphaned_dataset_count == 0

# now remove dataset1 reference
with dag_maker(dag_id="datasets-1", schedule=None, session=session):
BashOperator(task_id="task", bash_command="echo 1")

scheduler_job = Job()
self.job_runner = SchedulerJobRunner(job=scheduler_job, subdir=os.devnull)

self.job_runner._orphan_unreferenced_datasets(session=session)
session.flush()

orphaned_datasets_before_rerun = (
session.query(DatasetModel.updated_at, DatasetModel.uri)
.filter(DatasetModel.is_orphaned)
.order_by(DatasetModel.uri)
)
assert [dataset.uri for dataset in orphaned_datasets_before_rerun] == ["ds1"]
updated_at_timestamps = [dataset.updated_at for dataset in orphaned_datasets_before_rerun]

# when rerunning we should ignore the already orphaned datasets and thus the updated_at timestamp
# should remain the same
self.job_runner._orphan_unreferenced_datasets(session=session)
session.flush()

orphaned_datasets_after_rerun = (
session.query(DatasetModel.updated_at, DatasetModel.uri)
.filter(DatasetModel.is_orphaned)
.order_by(DatasetModel.uri)
)
assert [dataset.uri for dataset in orphaned_datasets_after_rerun] == ["ds1"]
assert updated_at_timestamps == [dataset.updated_at for dataset in orphaned_datasets_after_rerun]

def test_misconfigured_dags_doesnt_crash_scheduler(self, session, dag_maker, caplog):
"""Test that if dagrun creation throws an exception, the scheduler doesn't crash"""

Expand Down