Include job_id in order clause when fetching scheduled jobs to dispatch and when dispatching #164
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, forceORDER 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 theDELETE
byjob_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 theDELETE
query, to ensure the index onjob_id
was used, also avoided the deadlock.