jedcunningham commented on code in PR #30375:
URL: https://github.com/apache/airflow/pull/30375#discussion_r1160251186
##########
airflow/jobs/scheduler_job.py:
##########
@@ -1408,6 +1443,51 @@ def _send_sla_callbacks_to_processor(self, dag: DAG) ->
None:
)
self.executor.send_callback(request)
+ @provide_session
+ def _fail_tasks_stuck_in_queued(self, session: Session = NEW_SESSION) ->
None:
+ """
+ Mark tasks stuck in queued for longer than `task_queued_timeout` as
failed.
+ Tasks can get stuck in queued for a wide variety of reasons (e.g.
celery loses
+ track of a task, a cluster can't further scale up its workers, etc.),
but tasks
+ should not be stuck in queued for a long time. This will mark tasks
stuck in
+ queued for longer than `self._task_queued_timeout` as failed. If the
task has
+ available retries, it will be retried.
+ """
+ self.log.debug("Calling SchedulerJob._fail_tasks_stuck_in_queued
method")
+ for attempt in run_with_db_retries(logger=self.log):
+ with attempt:
+ self.log.debug(
+ "Running SchedulerJob.adopt_or_reset_orphaned_tasks with
retries. Try %d of %d",
Review Comment:
```suggestion
"Running SchedulerJob._fail_tasks_stuck_in_queued with
retries. Try %d of %d",
```
##########
airflow/jobs/scheduler_job.py:
##########
@@ -1408,6 +1443,51 @@ def _send_sla_callbacks_to_processor(self, dag: DAG) ->
None:
)
self.executor.send_callback(request)
+ @provide_session
+ def _fail_tasks_stuck_in_queued(self, session: Session = NEW_SESSION) ->
None:
+ """
+ Mark tasks stuck in queued for longer than `task_queued_timeout` as
failed.
+ Tasks can get stuck in queued for a wide variety of reasons (e.g.
celery loses
+ track of a task, a cluster can't further scale up its workers, etc.),
but tasks
+ should not be stuck in queued for a long time. This will mark tasks
stuck in
+ queued for longer than `self._task_queued_timeout` as failed. If the
task has
+ available retries, it will be retried.
+ """
+ self.log.debug("Calling SchedulerJob._fail_tasks_stuck_in_queued
method")
+ for attempt in run_with_db_retries(logger=self.log):
+ with attempt:
+ self.log.debug(
+ "Running SchedulerJob.adopt_or_reset_orphaned_tasks with
retries. Try %d of %d",
+ attempt.retry_state.attempt_number,
+ MAX_DB_RETRIES,
+ )
+ try:
+ query = session.query(TI).filter(
+ TI.state == State.QUEUED,
+ TI.queued_dttm < (timezone.utcnow() -
timedelta(seconds=self._task_queued_timeout)),
+ )
+ tasks_stuck_in_queued: list[TaskInstance] = with_row_locks(
+ query, of=TI, session=session,
**skip_locked(session=session)
+ ).all()
+ tasks_stuck_in_queued_messages = []
+ for ti in tasks_stuck_in_queued:
+ tasks_stuck_in_queued_messages.append(repr(ti))
+ msg = (
+ "TI %s was in the queued state for longer than
%s.",
+ repr(ti),
+ self._task_queued_timeout,
+ )
+ ti.handle_failure(error=msg, session=session)
Review Comment:
I'm less familiar with celery, but with KE you don't have to do this
explicitly, deleting the pod is enough. If we both fail it here and delete it,
we should double check we aren't using up multiple retries.
##########
airflow/jobs/scheduler_job.py:
##########
@@ -1408,6 +1443,51 @@ def _send_sla_callbacks_to_processor(self, dag: DAG) ->
None:
)
self.executor.send_callback(request)
+ @provide_session
+ def _fail_tasks_stuck_in_queued(self, session: Session = NEW_SESSION) ->
None:
+ """
+ Mark tasks stuck in queued for longer than `task_queued_timeout` as
failed.
+ Tasks can get stuck in queued for a wide variety of reasons (e.g.
celery loses
+ track of a task, a cluster can't further scale up its workers, etc.),
but tasks
+ should not be stuck in queued for a long time. This will mark tasks
stuck in
+ queued for longer than `self._task_queued_timeout` as failed. If the
task has
+ available retries, it will be retried.
+ """
+ self.log.debug("Calling SchedulerJob._fail_tasks_stuck_in_queued
method")
+ for attempt in run_with_db_retries(logger=self.log):
+ with attempt:
+ self.log.debug(
+ "Running SchedulerJob.adopt_or_reset_orphaned_tasks with
retries. Try %d of %d",
+ attempt.retry_state.attempt_number,
+ MAX_DB_RETRIES,
+ )
+ try:
+ query = session.query(TI).filter(
+ TI.state == State.QUEUED,
+ TI.queued_dttm < (timezone.utcnow() -
timedelta(seconds=self._task_queued_timeout)),
+ )
Review Comment:
Am I missing where we call `cleanup_stuck_queued_tasks`?
##########
tests/jobs/test_scheduler_job.py:
##########
@@ -1531,6 +1531,50 @@ def test_adopt_or_reset_orphaned_tasks(self, dag_maker):
ti2 = dr2.get_task_instance(task_id=op1.task_id, session=session)
assert ti2.state == State.QUEUED, "Tasks run by Backfill Jobs should
not be reset"
+ def test_fail_stuck_queued_tasks(self, dag_maker):
+ session = settings.Session()
+ with dag_maker("test_fail_stuck_queued_tasks"):
+ op1 = EmptyOperator(task_id="op1")
+
+ dr = dag_maker.create_dagrun()
+ ti = dr.get_task_instance(task_id=op1.task_id, session=session)
+ ti.state = State.QUEUED
+ ti.queued_dttm = timezone.utcnow() - timedelta(minutes=15)
+ session.commit()
+
+ processor = mock.MagicMock()
+
+ self.scheduler_job = SchedulerJob(num_runs=0)
+ self.scheduler_job.processor_agent = processor
+ self.scheduler_job._task_queued_timeout = 300
+
+ self.scheduler_job._fail_tasks_stuck_in_queued()
Review Comment:
Should we mock `cleanup_stuck_queued_tasks` and make sure it is called with
the TI?
--
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]