seanghaeli commented on code in PR #77086:
URL: https://github.com/apache/airflow/pull/77086#discussion_r4763270592


##########
airflow-core/src/airflow/models/trigger.py:
##########
@@ -370,20 +378,82 @@ def clean_unused(cls, *, session: Session = NEW_SESSION) 
-> None:
         cls._delete_unused_batch(batch_size, session=session)
 
     @classmethod
-    def _delete_unused_batch(cls, batch_size: int, *, session: Session) -> 
None:
-        unreferenced = (~cls.assets.any(), ~cls.callback.has(), 
~cls.task_instance.has())
-        ids = 
select(cls.id).where(*unreferenced).order_by(cls.id).limit(batch_size)
-        ids = with_row_locks(ids, session, of=cls, skip_locked=True, 
key_share=False)
+    def _failed_trigger_retention(cls) -> int:
+        """Seconds an unreferenced, failed trigger is kept (marked finished) 
before it is deleted."""
+        retention = conf.getint("triggerer", "failed_trigger_retention", 
fallback=7 * 24 * 3600)
+        if retention < 0:
+            raise ValueError("[triggerer] failed_trigger_retention must be at 
least 0")
+        return retention
+
+    @classmethod
+    def _finish_or_delete_unreferenced(
+        cls,
+        unreferenced: tuple[ColumnElement[bool], ...],
+        *,
+        session: Session,
+        batch_size: int | None = None,
+    ) -> None:
+        """
+        Keep failed unreferenced triggers for the retention window, delete the 
rest.
+
+        A trigger that recorded failures is finished on becoming unreferenced: 
it is released from
+        its triggerer and never run again, and deleted once it has been 
finished for
+        ``[triggerer] failed_trigger_retention`` seconds. Triggers that never 
failed are deleted
+        right away, so the table does not grow with every deferred task.
+        """
+        now = timezone.utcnow()
+        retention = cls._failed_trigger_retention()
+        # A failed trigger's owner has let go by the time it is unreferenced, 
so keep the reference it
+        # recorded. Watcher triggers record their asset when it is added; old 
rows may only carry this
+        # marker, which still tells the API what kind of trigger it was.
+        unfinished_failed = and_(cls.last_failed_at.is_not(None), 
cls.finished_at.is_(None))

Review Comment:
   A trigger that failed, then recovered, and later went away normally is kept 
here too, because a recovery clears `consecutive_failures` but leaves 
`last_failed_at`. In Breeze, a watcher that failed a few times, then fired an 
event and ran fine, then had its Dag paused, was kept as `finished` for the 
full retention like the one still failing, while the never-failed one was 
deleted. We should only keep it if it was failing when it was let go, i.e. key 
this on `consecutive_failures` instead.



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