Commit 28f9e05
committed
Include job_id in order clause when fetching scheduled jobs to dispatch 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.1 parent 041b904 commit 28f9e05
File tree
3 files changed
+5
-2
lines changed- app/models/solid_queue
- execution
- test/unit
3 files changed
+5
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| |||
0 commit comments