Skip to content
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

Include job_id in order clause when fetching scheduled jobs to dispatch and when dispatching #164

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

rosa
Copy link
Member

@rosa rosa commented Mar 5, 2024

This was missing and makes the locking prone to deadlocks when we have a bunch of jobs scheduled at the same time with the same priority, since we'd have a non-deterministic ordering.

Besides, on the DELETE query, force ORDER BY job_id even if we don't care about that. It turns out, under certain circumstances, when the scheduled_executions table is too small, MySQL might choose not to use any indexes for the DELETE by job_id. This means it might lock other rows besides those it's going to delete. Then, when running more than one dispatcher doing this, we might end up with a deadlock because one dispatcher is deleting and locking some rows that is not going to delete, and the other is doing the same, and both are waiting for the other's lock.

This deadlock was happening consistently in CI, for MySQL only, but I didn't manage to reproduce it locally, and it has never happened in production for us. Using an INDEX hint in the DELETE query, to ensure the index on job_id was used, also avoided the deadlock.

…ch and when dispatching

This was missing and makes the locking prone to deadlocks when we have a bunch of
jobs scheduled at the same time with the same priority, since we'd have a
non-deterministic ordering.

Besides, on the DELETE query, force a order by job_id even if we don't care about that.
It turns out, under certain circumstances, when the scheduled_executions table is too
small, MySQL might choose not to use any indexes for the DELETE by job_id. This means
it might lock other rows besides those it's going to delete. Then, when running
more than one dispatcher doing this, we might end up with a deadlock because one dispatcher
is deleting and locking some rows that's not deleting, and the other is doing the
same, and both are waiting for the other's lock.

This deadlock was happening consistently in CI, for MySQL only, but I didn't manage to
reproduce it locally, and it has never happened in production for us. Using an INDEX
hint in the DELETE query, to ensure the index on job_id was used, also avoided the
deadlock.
@rosa rosa merged commit 28f9e05 into main Mar 5, 2024
6 checks passed
@rosa rosa deleted the fix-deadlock-on-scheduled-executions branch March 5, 2024 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant