This is an automated email from the ASF dual-hosted git repository.
vatsrahul1001 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 6196a32433a Lock only trigger rows when the triggerer claims triggers
(#76213)
6196a32433a is described below
commit 6196a32433a70e9d0da76644dfffdd47161a7420
Author: Hussein Awala <[email protected]>
AuthorDate: Mon Nov 30 19:13:37 2026 +0100
Lock only trigger rows when the triggerer claims triggers (#76213)
The triggerer's claim queries join task_instance and callback to order
triggers by priority, and the row lock taken by those queries covered
every table in the FROM clause. On PostgreSQL and MySQL 8 the triggerer
therefore held locks on the joined task instance and callback rows until
the claim committed. Scheduler writes to those rows, such as the trigger
timeout sweep, had to wait for the triggerer, and SKIP LOCKED made the
triggerer pass over any trigger whose task instance row the scheduler
happened to hold, delaying that claim by a loop.
The claim only arbitrates ownership through trigger.triggerer_id, so the
trigger row is the only one that needs to be locked. Keeping the task
instance rows out of the triggerer's lock set also removes the claim
path from the scheduler/triggerer lock graph on task_instance.
---
airflow-core/src/airflow/models/trigger.py | 6 ++++-
airflow-core/tests/unit/models/test_trigger.py | 35 ++++++++++++++++++++++++++
2 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/airflow-core/src/airflow/models/trigger.py
b/airflow-core/src/airflow/models/trigger.py
index ab8dd955f20..e5fb1d5e309 100644
--- a/airflow-core/src/airflow/models/trigger.py
+++ b/airflow-core/src/airflow/models/trigger.py
@@ -769,7 +769,11 @@ class Trigger(Base):
else:
filtered_query =
filtered_query.filter(cls.team_name.is_(None))
- locked_query =
with_row_locks(filtered_query.limit(remaining_capacity), session,
skip_locked=True)
+ # Without of=cls the joined task_instance/callback rows are locked
too, blocking scheduler
+ # writes to them until commit, and SKIP LOCKED skips triggers
whose joined rows are locked.
+ locked_query = with_row_locks(
+ filtered_query.limit(remaining_capacity), session, of=cls,
skip_locked=True
+ )
result.extend(session.execute(locked_query).all())
return result
diff --git a/airflow-core/tests/unit/models/test_trigger.py
b/airflow-core/tests/unit/models/test_trigger.py
index d078c84f408..1826005b031 100644
--- a/airflow-core/tests/unit/models/test_trigger.py
+++ b/airflow-core/tests/unit/models/test_trigger.py
@@ -29,6 +29,7 @@ import sqlalchemy as sa
import time_machine
from cryptography.fernet import Fernet
from sqlalchemy import delete, event, func, insert, select, text, update
+from sqlalchemy.dialects import mysql, postgresql
from sqlalchemy.exc import OperationalError
from airflow._shared.timezones import timezone
@@ -54,6 +55,7 @@ from airflow.triggers.base import (
TriggerEvent,
)
from airflow.utils.session import create_session
+from airflow.utils.sqlalchemy import with_row_locks
from airflow.utils.state import State, TaskInstanceState
from tests_common.test_utils.asserts import assert_queries_count,
capture_orm_selects
@@ -1466,6 +1468,39 @@ def
test_get_sorted_triggers_ignores_archived_task_instance(session, create_task
assert all("working_set" in statement for statement in statements)
+def _create_mysql8_dialect():
+ # MySQLDialect.initialize() enables FOR UPDATE OF only after detecting
MySQL 8 on connect; a
+ # dialect that never connected silently drops the OF clause.
+ dialect = mysql.dialect()
+ dialect.server_version_info = (8, 0, 36)
+ dialect.supports_for_update_of = True
+ return dialect
+
+
[email protected](
+ ("dialect", "lock_clause"),
+ [
+ pytest.param(postgresql.dialect(), "FOR NO KEY UPDATE OF trigger SKIP
LOCKED", id="postgresql"),
+ pytest.param(_create_mysql8_dialect(), "FOR UPDATE OF `trigger` SKIP
LOCKED", id="mysql8"),
+ ],
+)
+@patch("airflow.models.trigger.with_row_locks", autospec=True,
side_effect=with_row_locks)
+def test_get_sorted_triggers_locks_only_trigger_rows(mock_with_row_locks,
session, dialect, lock_clause):
+ """The claim queries join task_instance/callback but must lock only the
trigger rows."""
+ Trigger.get_sorted_triggers(capacity=10, alive_triggerer_ids=[],
queues=None, session=session)
+
+ assert mock_with_row_locks.call_count == 3
+ compiled_queries = [
+ str(with_row_locks(*lock_call.args,
**lock_call.kwargs).compile(dialect=dialect))
+ for lock_call in mock_with_row_locks.call_args_list
+ ]
+
+ assert any(" JOIN task_instance " in compiled for compiled in
compiled_queries)
+ assert any(" JOIN callback " in compiled for compiled in compiled_queries)
+ for compiled in compiled_queries:
+ assert compiled.rstrip().endswith(lock_clause)
+
+
@pytest.mark.need_serialized_dag
@pytest.mark.parametrize("use_queues", [False, True])
def test_get_sorted_triggers_same_priority_weight(session,
create_task_instance, use_queues: bool):