Skip to content

Commit

Permalink
Row lock TI query in SchedulerJob._process_executor_events (apache#18975
Browse files Browse the repository at this point in the history
)

Using multiple schedulers causes Deadlock in _process_executor_events.
This PR fixes it.

Co-authored-by: Tzu-ping Chung <[email protected]>
(cherry picked from commit 52cc84c)
(cherry picked from commit d161134)
  • Loading branch information
ephraimbuddy authored and jedcunningham committed Jan 13, 2022
1 parent 1609049 commit 1103c82
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions airflow/jobs/scheduler_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import time
from collections import defaultdict
from datetime import timedelta
from typing import DefaultDict, Dict, Iterable, List, Optional, Tuple
from typing import DefaultDict, Dict, Iterable, Iterator, List, Optional, Tuple

from sqlalchemy import and_, func, not_, or_, tuple_
from sqlalchemy.exc import OperationalError
Expand Down Expand Up @@ -626,7 +626,15 @@ def _process_executor_events(self, session: Session = None) -> int:

# Check state of finished tasks
filter_for_tis = TI.filter_for_tis(tis_with_right_state)
tis: List[TI] = session.query(TI).filter(filter_for_tis).options(selectinload('dag_model')).all()
query = session.query(TI).filter(filter_for_tis).options(selectinload('dag_model'))
# row lock this entire set of taskinstances to make sure the scheduler doesn't fail when we have
# multi-schedulers
tis: Iterator[TI] = with_row_locks(
query,
of=TI,
session=session,
**skip_locked(session=session),
)
for ti in tis:
try_number = ti_primary_key_to_try_number_map[ti.key.primary]
buffer_key = ti.key.with_try_number(try_number)
Expand Down

0 comments on commit 1103c82

Please sign in to comment.