hkc-8010 commented on code in PR #76908:
URL: https://github.com/apache/airflow/pull/76908#discussion_r4749389363


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4253,6 +4273,38 @@ def _handle_tasks_stuck_in_queued(self, *, session: 
Session = NEW_SESSION) -> No
         for executor, stuck_tis in 
self._executor_to_workloads(tasks_stuck_in_queued, session).items():
             try:
                 for ti in stuck_tis:
+                    # The scan takes no lock; skip TIs a worker started since, 
and hold the row locks
+                    # through the commit below. Regional TIs lock their DagRun 
first (#74349).
+                    if is_regional(ti.region_id, ti.region_index) and (

Review Comment:
   Good catch. Clears take the DagRun lock first since #74349, and with 
fail_fast, handle_failure() updates sibling rows, so an unmapped TI needs the 
same order. The DagRun lock is now taken for every stuck TI, not just regional 
ones. This path only runs for stuck TIs, so the extra SKIP LOCKED select is 
cheap. I didn't add a real two-transaction deadlock test because SQLite can't 
reproduce the lock wait; test_stuck_in_queued_locks_dag_run_before_ti now 
covers the lock order for unmapped and mapped TIs.



##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4253,6 +4273,38 @@ def _handle_tasks_stuck_in_queued(self, *, session: 
Session = NEW_SESSION) -> No
         for executor, stuck_tis in 
self._executor_to_workloads(tasks_stuck_in_queued, session).items():
             try:
                 for ti in stuck_tis:
+                    # The scan takes no lock; skip TIs a worker started since, 
and hold the row locks
+                    # through the commit below. Regional TIs lock their DagRun 
first (#74349).
+                    if is_regional(ti.region_id, ti.region_index) and (
+                        session.scalar(
+                            with_row_locks(
+                                select(DagRun.id).where(
+                                    DagRun.dag_id == ti.dag_id, DagRun.run_id 
== ti.run_id
+                                ),
+                                of=DagRun,
+                                session=session,
+                                skip_locked=True,
+                            )
+                        )
+                        is None
+                    ):
+                        self.log.info("Dag run of %s is locked; skipping 
stuck-in-queued check", ti)
+                        continue
+                    still_queued = session.scalar(
+                        with_row_locks(
+                            select(TI.id).where(TI.id == ti.id, TI.state == 
TaskInstanceState.QUEUED),
+                            of=TI,
+                            session=session,
+                            skip_locked=True,
+                        )
+                    )
+                    if still_queued is None:
+                        self.log.info(
+                            "Task instance %s is no longer queued or is 
locked; skipping stuck-in-queued check",
+                            ti,
+                        )
+                        session.commit()  # release the DagRun lock taken 
above, if any

Review Comment:
   This method owns its session in practice: the only caller is the scheduler 
timer, which passes none, so @provide_session opens one. The per-TI commit was 
already in this loop on main, and on the skip path it is what releases the 
DagRun lock before the next TI.



##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -4316,38 +4370,29 @@ def _maybe_requeue_stuck_ti(self, *, ti, session, 
executor):
                     "The DAG or task could not be found. If a failure callback 
exists, it will not be run.",
                     exc_info=True,
                 )
-            else:
-                if task.has_on_failure_callback:
-                    if inspect(ti).detached:
-                        ti = session.merge(ti)
-                    _stuck_bundle_name, _stuck_bundle_version, 
_stuck_version_data = (
-                        _resolve_ti_callback_bundle_info(ti)
-                    )
-                    # Backfill dag_version_id for legacy tasks (Pydantic 
requires uuid.UUID).
-                    # Note: we cannot use `continue` here because this method 
is not
-                    # inside a loop.  If backfilling fails we simply skip the 
callback.
-                    if _ensure_ti_has_dag_version_id(ti, session, self.log):
-                        request = TaskCallbackRequest(
-                            filepath=ti.dag_model.relative_fileloc or "",
-                            bundle_name=_stuck_bundle_name,
-                            bundle_version=_stuck_bundle_version,
-                            version_data=_stuck_version_data,
-                            ti=task_instance_to_runtime(
-                                ti, model=TIDataModel, 
map_index=coordinates.public_map_index(ti)
-                            ),
-                            msg=msg,
-                            context_from_server=TIRunContext(
-                                dag_run=ti.dag_run,
-                                max_tries=ti.max_tries,
-                                variables=[],
-                                connections=[],
-                                xcom_keys_to_clear=[],
-                            ),
-                        )
-                        executor.send_callback(request)
-            finally:
-                ti.set_state(TaskInstanceState.FAILED, session=session)
-                executor.fail(executor.get_task_key(ti))
+                task = None
+            # handle_failure() reads retries and fail_fast from ti.task.
+            ti.task = task
+            needs_callback = task is not None and 
(task.has_on_failure_callback or task.has_on_retry_callback)
+            # Unlike process_executor_events, a missing dag_version_id only 
drops the requests here; the TI
+            # is still failed so it doesn't sit in queued.
+            try:
+                _send_task_failure_requests(
+                    ti,
+                    task=task,
+                    executor=executor,
+                    msg=msg,
+                    needs_callback=needs_callback,
+                    coordinates=coordinates,
+                    session=session,
+                    log=self.log,
+                )
+            except Exception:
+                # A request that can't be built must not leave the TI queued, 
or every scan hits it again.
+                self.log.exception("Could not send failure requests for %s; 
failing it anyway", ti)
+            failed_key = executor.get_task_key(ti)
+            ti.handle_failure(error=msg, session=session)

Review Comment:
   Added test_stuck_in_queued_tries_exceeded_honours_fail_fast: the stuck TI 
fails, a running sibling is failed and a pending one is skipped.



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