Copilot commented on code in PR #76215:
URL: https://github.com/apache/airflow/pull/76215#discussion_r4744381130


##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -1356,6 +1359,7 @@ def fetch_trigger_assignments(self, trigger_ids: 
set[int]) -> dict[int, int | No
     def build_trigger_workloads(self, new_trigger_ids: set[int]) -> 
list[workloads.RunTrigger]:
         """Build workloads for new trigger IDs."""
         dag_bag = DBDagBag()
+        serdag_resolver = BatchSerializedDagResolver(dag_bag)

Review Comment:
   The new tests exercise only a single batch, so they do not protect the 
cache's batch-scoped lifetime. Please add a regression test that builds two 
batches with the same supervisor, changes the latest version or serialized Dag 
contents between them, and checks that the second batch uses the updated task 
flag/data. This is the freshness boundary that makes skipping revalidation 
safe; the current tests would also pass if the resolver were accidentally 
retained across batches.
   
   ---
   Drafted-by: GitHub Copilot (no human review before posting)



##########
airflow-core/src/airflow/models/dagbag.py:
##########
@@ -289,6 +292,55 @@ def _on_cache_size(self, *, rate: float = 1.0) -> None:
         stats.gauge(f"{self._stats_prefix}.cache_size", size, rate=rate)
 
 
+class BatchSerializedDagResolver:
+    """
+    Memoize Dag version resolution and serialized Dag loading for one batch of 
lookups.
+
+    Entries are never revalidated: a Dag's latest version is read once, and a 
version found missing
+    or rewritten in place mid-batch is not re-read, so use a new instance per 
batch.
+
+    :meta private:
+    """
+
+    def __init__(self, dag_bag: DBDagBag) -> None:
+        self._dag_bag = dag_bag
+        self._latest_version_ids: dict[str, UUID | None] = {}
+        self._serdags: dict[UUID, tuple[SerializedDagModel, SerializedDAG] | 
None] = {}
+
+    def _get_latest_version_id(self, dag_id: str, *, session: Session) -> UUID 
| None:
+        if dag_id not in self._latest_version_ids:
+            dag_version = DagVersion.get_latest_version(dag_id=dag_id, 
session=session)
+            self._latest_version_ids[dag_id] = dag_version.id if dag_version 
else None
+        return self._latest_version_ids[dag_id]
+
+    def _get_serdag(
+        self, version_id: UUID, *, session: Session
+    ) -> tuple[SerializedDagModel, SerializedDAG] | None:
+        if version_id not in self._serdags:
+            model = 
self._dag_bag.get_serialized_dag_model(version_id=version_id, session=session)
+            self._serdags[version_id] = (model, model.dag) if model is not 
None else None

Review Comment:
   Please add a regression test that resolves two pinned runs of the same Dag 
with different version IDs through one resolver. The current tests repeat one 
version or return the same mocked serialized row for every Dag, so they do not 
protect version isolation when the cache is shared across workloads. Assert 
that each run receives its own serialized model and task definition, and that 
repeated lookups fetch and deserialize each version only once.



##########
airflow-core/src/airflow/models/dagbag.py:
##########
@@ -289,6 +292,55 @@ def _on_cache_size(self, *, rate: float = 1.0) -> None:
         stats.gauge(f"{self._stats_prefix}.cache_size", size, rate=rate)
 
 
+class BatchSerializedDagResolver:
+    """
+    Memoize Dag version resolution and serialized Dag loading for one batch of 
lookups.
+
+    Entries are never revalidated: a Dag's latest version is read once, and a 
version found missing
+    or rewritten in place mid-batch is not re-read, so use a new instance per 
batch.
+
+    :meta private:
+    """
+
+    def __init__(self, dag_bag: DBDagBag) -> None:
+        self._dag_bag = dag_bag
+        self._latest_version_ids: dict[str, UUID | None] = {}
+        self._serdags: dict[UUID, tuple[SerializedDagModel, SerializedDAG] | 
None] = {}
+
+    def _get_latest_version_id(self, dag_id: str, *, session: Session) -> UUID 
| None:
+        if dag_id not in self._latest_version_ids:
+            dag_version = DagVersion.get_latest_version(dag_id=dag_id, 
session=session)
+            self._latest_version_ids[dag_id] = dag_version.id if dag_version 
else None
+        return self._latest_version_ids[dag_id]
+
+    def _get_serdag(
+        self, version_id: UUID, *, session: Session
+    ) -> tuple[SerializedDagModel, SerializedDAG] | None:
+        if version_id not in self._serdags:
+            model = 
self._dag_bag.get_serialized_dag_model(version_id=version_id, session=session)
+            self._serdags[version_id] = (model, model.dag) if model is not 
None else None

Review Comment:
   The new negative-cache behavior is not covered: 
`test_falls_back_to_ti_version` resolves only once, while the 
repeated-resolution tests always find a serialized Dag. Please add a regression 
test that resolves two task instances sharing a missing run version but having 
distinct fallback versions. Assert that the missing version is fetched only 
once and that each resolution returns its own fallback. This would catch 
repeated failed fetches or accidental reuse of the first task instance's 
fallback across the batch.



-- 
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]

Reply via email to