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


##########
airflow-core/src/airflow/serialization/definitions/dag.py:
##########
@@ -798,70 +879,146 @@ def _process_dagrun_deadline_alerts(
         run_id = orm_dagrun.run_id
         dagrun_id = orm_dagrun.id
         dagrun_dag_id = orm_dagrun.dag_id
+        dag_version_id = orm_dagrun.created_dag_version_id
         bundle_name = orm_dagrun.dag_model.bundle_name
+        dagrun_snapshot = _DagRunSnapshot(
+            dag_id=dagrun_dag_id,
+            logical_date=orm_dagrun.logical_date,
+            queued_at=orm_dagrun.queued_at,
+        )
         stored_alerts = [
             (alert.id, alert.name, alert.reference, alert.interval, 
alert.callback_def)
             for alert in deadline_alert_records
             if alert
         ]
 
-        for alert_id, alert_name, alert_reference, alert_interval, 
alert_callback_def in stored_alerts:
-            # Deadline creation is best-effort. Catch per alert so one bad 
alert cannot starve the rest or abort the DagRun.
+        def build_discarded_error(alert_id: UUID) -> DagRunDiscardedError:
+            return DagRunDiscardedError(
+                f"Dag run {run_id} of Dag {self.dag_id} was discarded by 
deadline alert {alert_id}: its "
+                "deadline reference rolled back, committed or broke the 
transaction the Dag run was created in",
+                dag_id=dagrun_dag_id,
+                run_id=run_id,
+                dag_version_id=dag_version_id,
+                deadline_alert_id=alert_id,
+            )
+
+        def is_dagrun_discarded() -> bool:
+            if not session.is_active:
+                # A failed flush left the caller's transaction to be rolled 
back, with the Dag run in it.
+                return True
+            # Checked on the row rather than on the session: a rollback by the 
database itself (e.g. a
+            # MySQL deadlock) leaves the Dag run in the session, while a 
commit and close() outside the
+            # scheduler's commit guard (e.g. ``dag.test``) detaches it but 
keeps its row.
             try:
-                deserialized_deadline_alert = decode_deadline_alert(
-                    {
-                        DeadlineAlertFields.NAME: alert_name,
-                        DeadlineAlertFields.REFERENCE: alert_reference,
-                        DeadlineAlertFields.INTERVAL: alert_interval,
-                        DeadlineAlertFields.CALLBACK: alert_callback_def,
-                    }
-                )
-                interval = 
resolve_deadline_alert_interval(deserialized_deadline_alert, session=session)
+                # Without autoflush, so a pending Deadline is not written 
against a row that may be gone.
+                with session.no_autoflush:
+                    return session.scalar(select(DagRun.id).where(DagRun.id == 
dagrun_id)) is None
+            except DBAPIError as error:
+                # A failed statement aborts the caller's PostgreSQL 
transaction. Any other error, such as
+                # a dropped connection, is not caused by the alert and 
propagates as before.
+                if _is_in_failed_sql_transaction(error):
+                    return True
+                raise
 
-                if isinstance(deserialized_deadline_alert.reference, 
SerializedReferenceModels.TYPES.DAGRUN):
-                    deadline_time = 
deserialized_deadline_alert.reference.evaluate_with(
-                        session=session,
-                        interval=interval,
-                        dagrun=orm_dagrun,
-                        dag_id=self.dag_id,
-                        run_id=run_id,
+        created_deadlines = 0
+
+        with contextlib.ExitStack() as exit_stack:
+            try:
+                alert_session = 
exit_stack.enter_context(_create_alert_session(session=session))

Review Comment:
   My agent flagged this one: a reference that uses the scheduler's session 
(`create_session()`, `@provide_session`, `settings.Session()`) no longer 
crashes the scheduler, but its Dag then gets no runs until a restart. In 
Breeze, pointing `settings.Session` at the alert session while the alerts run 
(`settings.Session.registry.set(alert_session)`, restored after) kept all three 
test Dags running with only the alert skipped, with two schedulers too. Then 
nothing has to be left out until a restart, so the exclusion in both creation 
queries, the stopped-Dag set and the `dag_run_creation_stopped` metric (about 
45 lines plus three tests) could go. Could we do that, so a broken alert only 
costs its deadline?



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