-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Prevent Concurrent TaskInstance Tries in Scheduler HA #59234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent Concurrent TaskInstance Tries in Scheduler HA #59234
Conversation
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
|
SCHEDULED state in…|
Releated to the issue reported here #57618 |
|
@ephraimbuddy can you approve the workflows ? |
d38639a to
87f6c00
Compare
|
Have you tested this with your deployment as agreed? |
Hello, yes, I have tested it with our deployment. The patch appears to resolve the race condition issue. However, I noticed another issue (here the details), which can probably be addressed independently. |
|
covered already by this one #60330 |
Fix Concurrency Issue: Prevent Concurrent TaskInstance Tries in Scheduler HA
Related issue: #57618
This PR aims to fix a sporadic but critical race condition in the HA scheduler setup that could lead to concurrent execution of the same Task Instance (TI) with sequential try numbers (e.g., Try #1 and Try #2 running simultaneously).
🐞 Problem Description
In a multi-scheduler environment, if two schedulers detected that a TI was ready to run (state$\mathbf{SCHEDULED}$ state.
None) at nearly the exact same moment, both would attempt to push the task into theThe race failure occurred due to the following two factors in the
DagRun.schedule_tis()logic:WHEREClause: TheUPDATEquery only usedTI.id.in_(id_chunk), lacking a final state check.try_numberLogic: Thetry_numberwas advanced (TI.try_number + 1) if the state was notUP_FOR_RESCHEDULE.When Scheduler B lost the race to Scheduler A:
WHEREclause matched the ID, and thescheduled_dttmfield changed, the update was successful (rowcount=1).CASElogic saw the newSCHEDULEDstate and incorrectly determined it should be advanced totry_number=2, thus corrupting the record and enabling a concurrent run.✅ Solution: Enforce Optimistic Concurrency Control
This PR enforces Optimistic Concurrency Control (OCC) by adding a restrictive
WHEREclause to the atomic update operation.The added condition is:
.where(TI.state.in_(SCHEDULEABLE_STATES))How this fixes the race:
WHEREclause holds true, and the update succeeds (rowcount=1).SCHEDULEABLE_STATES).WHEREclause fails to find a match, the query returnsThis ensures that the$\mathbf{None} \rightarrow \mathbf{SCHEDULED}$ transition, resolving the concurrent task execution bug.
try_numbercan no longer be corrupted during the