Repository navigation
Description
Due to network issues, overloaded workers or just bad luck, the worker-to-worker communication needed for getting task dependencies from other workers might fail (distributed.worker.get_data_from_worker). This is (in principle) successfully caught by the worker and scheduler and reacted on. During this however a race-condition can be triggered, which brings the cluster in an inconsistent and stuck state.
Minimal Complete Verifiable Example:
The following code will introduce a random failure in distributed.worker.get_data_from_worker with higher probability than a real-world use-case might have, just to demonstrate:
from unittest.mock import patch
from random import randint
import dask.bag as db
from distributed import Client
# Calling this function on the worker will temporarily replace
# the `get_data_from_worker` function with a version, which
# fails in 1 out of 4 cases. In the "real world" the
# probability of a failure due to communication problems is
# of course a lot smaller, but it might happen.
def replace_get_data_from_worker_function(dask_worker):
from distributed.worker import get_data_from_worker
dask_worker.my_counter = 0
async def get_data_from_worker_function(*args, **kwargs):
# "Fail" in 1 out of 4 cases
if randint(0, 3) == 1:
dask_worker.my_counter += 1
raise OSError
return await get_data_from_worker(*args, **kwargs)
p = patch("distributed.worker.get_data_from_worker",
get_data_from_worker_function)
p.start()
if __name__ == "__main__":
client = Client()
client.run(replace_get_data_from_worker_function)
PARTITIONS = 20
# Just some arbitrary computation, which takes
# reasonably long and (most importantly) creates tasks
# with many dependencies, so that we need a lot of communication
data = db.from_sequence(range(PARTITIONS), npartitions=PARTITIONS)\
.map_partitions(lambda x: range(100_000))\
.groupby(lambda x: x % PARTITIONS)\
.map_partitions(lambda x: len(list(x)))
data.compute()Running this code, will sometimes not finish the computation, but one (or multiple) workers are stuck while waiting in fetching the dependencies for a specific task.
Note: it is a race-condition, so you might need to run the code multiple times until it is stuck...
Here are some observations I have made using the worker's and scheduler's properties and the transition log.
Let's say worker A is stuck while processing task T1, which depends on task T2 owned by worker B.
- the scheduler task state shows that
T2is present onB Bhas the task in its memory (dask_worker.data)Ahowever thinks, that no-one owns the taskdask_worker.tasks[T1].who_owns == {}, soAdoes not even start askingBfor the data.
From the transition log, this is what I think that happens (but I am happy if someone with more knowledge on the way the worker works could confirm my observations):
- some time before that, worker
Awants to process another task,T3, which needs a dependencyT4also fromB - it calls
gather_dep, which callsget_data_from_worker. This fails (either due to a real network issue or due to our patched function above). - in the meantime/around this time, the scheduler also tells
Ato doT1, which depends onT2(owned byB). - during the error handling for an
OSErrorofgather_dep, the local state of the workerAis changed in a way, so that all tasks owned byBare marked as not owned byBanymore. In our case, that isT2andT4. - However (and I think this is where the bug is), only the initially requested dependencies are notified as missing to the scheduler, in this case
T4. (see here)
The final state is, that the worker A thinks no one would own the data for T2, while the scheduler will not re-distributed the task (as it was never marked as missing).
One last comment: using the setting DASK_DISTRIBUTED__COMM__RETRY__COUNT it is possible to make the failures
of the get_data_from_worker function less likely. But unfortunately, this will just decrease the probability, not fix the problem.
Environment:
- Dask version: 2022.2.1
- Python version: 3.8
- Operating System: macOS
- Install method (conda, pip, source): pip
Activity
nils-braun commentedon Mar 30, 2022
Just wanted to confirm that I am also seeing this with version 2022.3.0 (even though the links to the code are slightly off in the description above, but the overall function
gather_depstill looks the same).@fjetter (as I was seeing you did some changes to this logic :-)): do you think it would make sense to either not mark all of the dependencies of a worker as removed in the
except OSErrorcase ofgather_dep(here) (but instead only the ones we were asking for) or do you think it would be better to make the logic in thefinallycall more complex, so that tasks which were marked as removed during theexcept OSErrorare finally notified as missing to the scheduler?I am wondering if there is something special in our setup (large scale, many memory-hungry tasks) which triggers this bug so often.
fjetter commentedon Mar 30, 2022
Indeed, gather_dep was a very frequent source of deadlocks due to it's exception handling.
Thank you for the reproducer. I can confirm this is happening on my end as well.
Maybe. The system should be robust enough to recover from us removing too many tasks. I suspect something else is not working as intended but this may be a hotfix.
I'm a bit hesitant of introducing more complexity but if it is necessary...
We're currently investing in some larger refactoring which should allow us to test these edge cases more thoroughly, see also #5736
gjoseph92 commentedon Apr 5, 2022
Thanks for the reproducer and explanation @nils-braun. I think I've somewhat minimized this to
A
git bisectpoints to #5653 as the first failing commit (cc @fjetter). I believe this is also implicated in #5960 (comment).Interestingly, this also makes the reproducer succeed (not that I think it's a real fix though):
gjoseph92 commentedon Apr 8, 2022
A few thoughts:
There's a fundamental question here: does an
OSErrormean we can safely assume the other worker has died and will never come back (vs temporarily disconnected)? That's how the current error handling logic is acting, but I don't know if that's a valid assumption. I think the current logic is trying to add an optimization that "an OSError means the worker is probably dead, so let's preemptively stop trying to talk to it". This is a nice idea, but I don't actually know ifOSErroris even the signal we should be acting on to make that choice. (#5678 may be relevant?)But regardless that, as @nils-braun has pointed out, we're not correctly dealing with the TaskStates we mutate.
If we end up making a task's
who_hasempty, then by definition the task ismissing, so an obvious thing to do would be just transition it tomissingin that case:However, you'll then get a
Impossible transition from fetch to missingerror.Should we just add a transition function for that? Or is there a good reason why that isn't currently a valid transition?
Another thought: in
ensure_communicating, we check for tasks that are essentially missing (who_hasis empty). But we seem to just ignore them, and assume that something else is dealing with them (thefind_missingmechanism).I think these assertions are the assumptions
ensure_communicatingis making:Of course, in the reproducer, they fail. But arguably,
ensure_communicatingshould be validating this (even if just in tests). And maybe it could beensure_communicating's job to explicitly deal with tasks like this?I think it comes down to:
fetch->missingis a valid transition, then we should write that and transition tasks through itfetch->missingis not a valid transition, then we shouldn't be effectively putting tasks in a missing state but not transitioning themgjoseph92 commentedon Apr 11, 2022
For the record, @fjetter and I discussed offline, and we're going to add back a
fetch->missingtransition. We're also both skeptical about using OSError in this way, but that's a broader change that seems to throw off other things.mrocklin commentedon Apr 12, 2022
Thank you for the detailed summary @gjoseph92
Add back Worker.transition_fetch_missing
mrocklin commentedon Apr 12, 2022
This is done here: #6112
My apologies for being absent with historical context here. We went through this decision several years ago. It's not a correct assumption, but it is an assumption which prioritizes stability over performance, and so is a win. We had odd issues when we tried to get smart around this, and found that it was just better to let clusters think that workers were gone, and celebrate when they magically returned. (I may not be fully understanding the situation though)
gjoseph92 commentedon Apr 12, 2022
Maybe that was the case then, but these days I think it actually prioritizes performance over stability. It's short-circuiting other tasks from trying and failing to fetch from that worker in the future. It assumes the current error is predictive of what will happen if we try again, so we preemptively choose not to try again. This adds complexity compared to just letting those fetches fail sometime in the future, and just dealing with the failure per-key when it happens.
Add back Worker.transition_fetch_missing (#6112)
nils-braun commentedon Apr 20, 2022
Thanks @gjoseph92, @mrocklin and @fjetter!
I am happy to confirm that this has fixed the reproducer above, and I am quite confident that it will also fix the problematic large-scale use cases.