ephraimbuddy commented on code in PR #69815:
URL: https://github.com/apache/airflow/pull/69815#discussion_r4404564613


##########
airflow-core/src/airflow/models/dagbag.py:
##########
@@ -240,6 +240,24 @@ def get_latest_version_of_dag(self, dag_id: str, *, 
session: Session) -> Seriali
 
         if not (serdag := SerializedDagModel.get(dag_id, session=session)):
             return None
+
+        with self._lock:
+            cached = self._dags.get(serdag.dag_version_id)
+            if cached is not None:
+                # The latest-row query already loaded dag_hash, so no 
revalidation query is needed.
+                if cached.dag_hash == serdag.dag_hash:
+                    self._dags[serdag.dag_version_id] = 
cached._replace(last_validated=time.monotonic())
+                else:
+                    # A serialized Dag can be updated in place without 
changing its version ID.
+                    self._dags.pop(serdag.dag_version_id, None)
+                    cached = None
+
+        if cached is not None:
+            if self._use_cache:
+                stats.incr("api_server.dag_bag.cache_hit")
+            return cached.dag
+        if self._use_cache:
+            stats.incr("api_server.dag_bag.cache_miss")
         return self._read_dag(serdag)

Review Comment:
   The DB query is intentional; re-deserializing every time was not.
   
   The cache is keyed by version ID, so a lookup by `dag_id` has to ask the DB 
which version is latest, otherwise a newly written version would never be 
picked up. When #53153 added this, the cache held only the deserialized Dag 
with no hash, so the method re-read the row and overwrote the entry. That 
overwrite was also the only way a version rewritten in place got refreshed.
   
   Since #68558 each entry carries `dag_hash`, and the latest-row query already 
returns it. Comparing the two is a sound way to skip deserialization, so I'm 
fine with the approach here.
   



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