-
Notifications
You must be signed in to change notification settings - Fork 16.7k
Fix dynamic dag_id resolution in TriggerDagRunOperator links
#56973
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
Open
hwang-cadent
wants to merge
5
commits into
apache:main
Choose a base branch
from
hwang-cadent:fix-46402-trigger-dagrun-dynamic-dag-id-links
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−55
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c46395b
Fix dynamic dag_id resolution in TriggerDagRunOperator links
hwang-cadent fe23bc0
Merge branch 'main' into fix-46402-trigger-dagrun-dynamic-dag-id-links
hwang-cadent f7de2ef
Add test_deserialize_field_value_with_arg_not_set_for_date_fields to
hwang-cadent 20386c3
Merge branch 'main' into fix-46402-trigger-dagrun-dynamic-dag-id-links
hwang-cadent b045529
Merge branch 'main' into fix-46402-trigger-dagrun-dynamic-dag-id-links
hwang-cadent 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
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
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 |
|---|---|---|
|
|
@@ -54,6 +54,7 @@ | |
|
|
||
| XCOM_LOGICAL_DATE_ISO = "trigger_logical_date_iso" | ||
| XCOM_RUN_ID = "trigger_run_id" | ||
| XCOM_DAG_ID = "trigger_dag_id" | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -86,21 +87,26 @@ def get_link(self, operator: BaseOperator, *, ti_key: TaskInstanceKey) -> str: | |
| if TYPE_CHECKING: | ||
| assert isinstance(operator, TriggerDagRunOperator) | ||
|
|
||
| trigger_dag_id = operator.trigger_dag_id | ||
| if not AIRFLOW_V_3_0_PLUS: | ||
| from airflow.models.renderedtifields import RenderedTaskInstanceFields | ||
| from airflow.models.taskinstancekey import TaskInstanceKey as CoreTaskInstanceKey | ||
|
|
||
| core_ti_key = CoreTaskInstanceKey( | ||
| dag_id=ti_key.dag_id, | ||
| task_id=ti_key.task_id, | ||
| run_id=ti_key.run_id, | ||
| try_number=ti_key.try_number, | ||
| map_index=ti_key.map_index, | ||
| ) | ||
| # Try to get the resolved dag_id from XCom first (for dynamic dag_ids) | ||
| trigger_dag_id = XCom.get_value(ti_key=ti_key, key=XCOM_DAG_ID) | ||
|
|
||
|
Member
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. Bug: Should be: |
||
| # Fallback to operator attribute and rendered fields if not in XCom | ||
| if not trigger_dag_id: | ||
| trigger_dag_id = operator.trigger_dag_id | ||
| if not AIRFLOW_V_3_0_PLUS: | ||
| from airflow.models.renderedtifields import RenderedTaskInstanceFields | ||
| from airflow.models.taskinstancekey import TaskInstanceKey as CoreTaskInstanceKey | ||
|
|
||
| core_ti_key = CoreTaskInstanceKey( | ||
| dag_id=ti_key.dag_id, | ||
| task_id=ti_key.task_id, | ||
| run_id=ti_key.run_id, | ||
| try_number=ti_key.try_number, | ||
| map_index=ti_key.map_index, | ||
| ) | ||
|
|
||
| if template_fields := RenderedTaskInstanceFields.get_templated_fields(core_ti_key): | ||
| trigger_dag_id: str = template_fields.get("trigger_dag_id", operator.trigger_dag_id) # type: ignore[no-redef] | ||
| if template_fields := RenderedTaskInstanceFields.get_templated_fields(core_ti_key): | ||
| trigger_dag_id: str = template_fields.get("trigger_dag_id", operator.trigger_dag_id) # type: ignore[no-redef] | ||
|
|
||
| # Fetch the correct dag_run_id for the triggerED dag which is | ||
| # stored in xcom during execution of the triggerING task. | ||
|
|
@@ -206,7 +212,7 @@ def __init__( | |
| self.note = note | ||
| self.deferrable = deferrable | ||
| self.logical_date = logical_date | ||
| if logical_date is NOTSET: | ||
| if isinstance(logical_date, ArgNotSet) or logical_date is NOTSET: | ||
| self.logical_date = NOTSET | ||
| elif logical_date is None or isinstance(logical_date, (str, datetime.datetime)): | ||
| self.logical_date = logical_date | ||
|
|
@@ -219,7 +225,7 @@ def __init__( | |
| raise NotImplementedError("Setting `fail_when_dag_is_paused` not yet supported for Airflow 3.x") | ||
|
|
||
| def execute(self, context: Context): | ||
| if self.logical_date is NOTSET: | ||
| if isinstance(self.logical_date, ArgNotSet) or self.logical_date is NOTSET: | ||
| # If no logical_date is provided we will set utcnow() | ||
| parsed_logical_date = timezone.utcnow() | ||
| elif self.logical_date is None or isinstance(self.logical_date, datetime.datetime): | ||
|
|
@@ -277,6 +283,14 @@ def execute(self, context: Context): | |
| def _trigger_dag_af_3(self, context, run_id, parsed_logical_date): | ||
| from airflow.providers.common.compat.sdk import DagRunTriggerException | ||
|
|
||
| # Store the resolved dag_id to XCom for use in the link generation | ||
| # This is important for dynamic dag_ids (from XCom or complex templates) | ||
| # In Airflow 3.x, context has both "task_instance" and "ti" keys | ||
| if "task_instance" in context: | ||
| context["task_instance"].xcom_push(key=XCOM_DAG_ID, value=self.trigger_dag_id) | ||
| elif "ti" in context: | ||
| context["ti"].xcom_push(key=XCOM_DAG_ID, value=self.trigger_dag_id) | ||
|
|
||
| kwargs_accepted = dict( | ||
| trigger_dag_id=self.trigger_dag_id, | ||
| dag_run_id=run_id, | ||
|
|
@@ -330,10 +344,11 @@ def _trigger_dag_af_2(self, context, run_id, parsed_logical_date): | |
| raise e | ||
| if dag_run is None: | ||
| raise RuntimeError("The dag_run should be set here!") | ||
| # Store the run id from the dag run (either created or found above) to | ||
| # Store the run id and dag_id from the dag run (either created or found above) to | ||
| # be used when creating the extra link on the webserver. | ||
| ti = context["task_instance"] | ||
| ti.xcom_push(key=XCOM_RUN_ID, value=dag_run.run_id) | ||
| ti.xcom_push(key=XCOM_DAG_ID, value=self.trigger_dag_id) | ||
|
|
||
| if self.wait_for_completion: | ||
| # Kick off the deferral process | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.