feat: ensure task scheduling fairness and FIFO order#1032
Conversation
93590ac to
c20e868
Compare
c20e868 to
4d7761f
Compare
metcoder95
left a comment
There was a problem hiding this comment.
Thanks for the contribution! LGTM
The only concern I've is the change on the current behavior. Although we provide backwards compatibility for custom task_queues that does not provides the unshift method, the PR per-se changes the current behaviour of the queues that implementations might already have some expectations on.
Not sure if we should guard this through a flag or similar, just to preserve the current behavior and allow opt in for the new one.
|
Good point, thanks! I can add a new |
|
Sure, let's do that so it can also land on |
|
I'm gonna go ahead and send it to v6, feel free to open a pr againg |
|
lint seems failing |
|
This issue has been marked as stale because it has been opened 45 days without activity. Remove stale label or comment or this will be closed in 10 days. |
Overview
Fixes #1031
Note: I was unsure about the target branch for this change (v5 vs current), please let me know if the PR should be recreated to target a different branch.
In some code paths, a task is shifted from the tail of the queue, but then if we failed to distribute it to a worker it is pushed to the head of the queue.
This breaks the FIFO ordering of the tasks, which is not a big problem by itself, because the real order of execution is non-deterministic in a multi-threaded environment. However, this leads to the fairness issues, where some tasks are waiting in queue more that others, causing latency jitter. In some edge cases (I have consistently observed this when using 1 worker), the same task may lose its place in the queue numerous times, getting delayed by dozens of seconds.
Implementation details:
_distributeTaskis not responsible for returning a task back to the queue anymore. Now the code calling_distributeTaskshould return the task into a correct place (head or tail, skipQueue or taskQueue)unshifthas been added to theTaskQueueinterface and the 2 builtin implementations. This operation is made optional, so that clients who provide custom implementations ofTaskQueuecan continue using them without needing to write new code.Category
Contribution Checklist
DCO Sign-off