dstandish commented on code in PR #69565:
URL: https://github.com/apache/airflow/pull/69565#discussion_r3539413349
##########
airflow-core/src/airflow/models/taskinstance.py:
##########
@@ -203,6 +203,33 @@ def _stop_remaining_tasks(*, task_instance: TaskInstance,
task_teardown_map=None
log.info("Not skipping teardown task '%s'", ti.task_id)
+def _add_and_prime_mapped_ti(
+ ti: TaskInstance,
+ task: Operator,
+ dag_run: DagRun,
+ *,
+ session: Session,
+ context_carrier: dict | None = None,
+) -> None:
+ """
+ Attach a newly-created mapped TI to the session and prime its ``dag_run``
cache.
+
+ Used by mapped-task expansion (``TaskMap.expand_mapped_task``,
+ ``DagRun._revise_map_indexes_if_mapped``) to persist a brand-new mapped TI
without
+ the per-row SELECT that ``session.merge()`` would issue, and to prime
``dag_run`` so
+ a later ``ti.get_dagrun()`` during dependency evaluation is a cache hit
rather than
+ another per-TI SELECT.
+
+ :meta private:
+ """
+ task_instance_mutation_hook(ti, dag_run=dag_run)
+ session.add(ti)
+ if context_carrier is not None:
+ ti.context_carrier = context_carrier
+ ti.refresh_from_task(task, dag_run=dag_run)
+ set_committed_value(ti, "dag_run", dag_run)
Review Comment:
ok so in the current call sites, it just happens that there's no difference.
why: because the TIs that are passed to it happen to be transient. if this
helper were to be used for a persisted ti, then set_committed_value would avoid
an unnecessary N+1 because it bypasses the orm event system.
there is precedent for it in TI.get_dagrun
so the reason to use it is it's defensive and consistent with other
instances where we set a cached value
the reason to not use it is because it's not strictly necessary in the
usages *right now* and the other way is more idiomatic.
i lean ever so slightly to keeping the usage of set_committed_value -- but i
do *not* feel strongly about it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]