Fix run_duration calculation in airflow dags test by decoupling start_date#68837
Open
sejal-gupta-ksolves wants to merge 1 commit into
Open
Fix run_duration calculation in airflow dags test by decoupling start_date#68837sejal-gupta-ksolves wants to merge 1 commit into
sejal-gupta-ksolves wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
closes: #66127
Description
Problem:
When running local execution validation using
DAG.test(logical_date=...), the test harness erroneously anchors theDagRun.start_dateto the historical scheduled execution boundary (logical_dateorrun_after). Becausestart_dateis overridden with a past date instead of the actual wall-clock execution time, downstream calculations forrun_duration(which relies on subtractingstart_datefromend_date) yield completely inaccurate and inflated durations. This breaks isolated performance tuning metrics and runtime analytics during local development testing.Solution:
This fix explicitly decouples the physical execution clock from the logical scheduling parameters within the
DAG.test()orchestration pathway inside thetask-sdk.task-sdk/src/airflow/sdk/definitions/dag.py, a dedicatedrun_start_dateis introduced usingtimezone.utcnow(). This captures the exact time the test actually begins executing, regardless of how far in the past the targetedlogical_dateis set.get_or_create_dagruncaller is updated to cleanly map the parameters:logical_dateretains the historical scheduling context requested by the user.start_dateuses the newly isolatedrun_start_dateexecution timestamp.test_dag_test_runtime_start_date_decoupled_from_logical_date) is implemented intask-sdk/tests/task_sdk/definitions/test_dag.py. It freezes the execution environment usingtime_machineand asserts thatstart_dateanchors to the frozen wall-clock matrix while thelogical_dateaccurately tracks the historical context parameter.