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


##########
airflow-core/src/airflow/models/renderedtifields.py:
##########
@@ -363,19 +366,17 @@ def _do_delete_old_records(
                 legacy.c.run_id.not_in(run_ids_to_keep),
             )
         )
+        # rtif_v2 only stores the attempt UUID, so matching every run outside 
the retained ones
+        # would read the task's whole attempt history on each task run.
+        attempt_ids_to_clear = select(TaskInstance.id).where(
+            TaskInstance.dag_id == dag_id,
+            TaskInstance.task_id == task_id,
+            TaskInstance.run_id.in_(run_ids_to_clear),
+        )
         session.execute(
             delete(cls)
-            .where(
-                select(1)
-                .where(
-                    TaskInstance.id == cls.task_instance_id,
-                    TaskInstance.dag_id == dag_id,
-                    TaskInstance.task_id == task_id,
-                    TaskInstance.run_id.not_in(run_ids_to_keep),
-                )
-                .exists()
-            )
-            .execution_options(synchronize_session="fetch")
+            .where(cls.task_instance_id.in_(attempt_ids_to_clear))
+            .execution_options(synchronize_session="fetch", 
include_all_attempts=True)

Review Comment:
   The cleanup tests create rendered fields only for current attempts, so they 
do not exercise the new `include_all_attempts=True` behavior. Add a regression 
test with rendered fields on both archived and current attempts in the cleanup 
window, asserting that both are deleted while retained-run attempts survive. 
The test should fail when this option is removed, catching the default 
current-attempt filter leaving archived rendered fields behind.



##########
airflow-core/src/airflow/models/renderedtifields.py:
##########
@@ -318,27 +321,26 @@ def delete_old_records(
 
         from airflow.models.dagrun import DagRun
 
-        # Find run_ids from the N most recent dag runs (no RTIF table scan 
needed).
         # Use run_after instead of logical_date since logical_date can be NULL 
for manual runs.
-        run_ids_to_keep_query = (
-            select(DagRun.run_id)
-            .where(DagRun.dag_id == dag_id)
-            .order_by(DagRun.run_after.desc())
-            .limit(num_to_keep)
+        recent_run_ids = (
+            select(DagRun.run_id).where(DagRun.dag_id == 
dag_id).order_by(DagRun.run_after.desc())

Review Comment:
   When multiple Dag runs share `run_after`, the separate LIMIT and OFFSET 
queries can order tied rows differently, allowing the retained and cleared 
windows to overlap. This can delete rendered fields for a run selected for 
retention. Add the unique `DagRun.id` as a tie-breaker so both windows use the 
same ordering.



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