Skip to content

Work stealing in case of job restrictions / resources would be useful #1389

Closed

Description

@m-albert

Currently jobs that have host or resource restrictions are not considered for work stealing.

The documentation (http://distributed.readthedocs.io/en/latest/work-stealing.html) states that

If a task has been specifically restricted to run on particular workers (such as is the case when special hardware is required) then we do not steal.

This probably makes sense when a task is bound to a single or few workers, however the general case of host and resource restrictions might benefit from stealing.

Consider the use case in which only a subset of workers / machines has access to data:
https://stackoverflow.com/questions/46180830/how-to-enable-proper-work-stealing-in-dask-distributed-when-using-task-restricti

Activity

  1. m-albert commented on Sep 13, 2017

    @m-albert
    Author

    I was looking into stealing.py.

    For enabling work stealing within the resource framework (which is not excluded in the documentation), might it be enough to choose out of the idle workers the first one that matches the key's resources?

    I think this would involve the following changes:

    In WorkStealing.steal_time_ratio resource limited tasks would no longer be excluded:

            if (key not in self.scheduler.loose_restrictions
                    and (key in self.scheduler.host_restrictions or
                         key in self.scheduler.worker_restrictions)):# or
                #key in self.scheduler.resource_restrictions):
                return None, None  # don't steal

    In WorkStealing.balance, this would take care of choosing a matching worker before the two calls of WorkStealing.move_task:

                            i += 1
                            # in case the key has resource_restrictions,
                            # set i to next worker providing them
                            if key in self.scheduler.resource_restrictions.keys():
                                resource_i = self.next_resource_i(
                                    idle,
                                    self.scheduler.resource_restrictions[key],
                                    start_index=i)
                                if resource_i is None:
                                    continue
                                else:
                                    i = resource_i

    with a new method of WorkStealing as such:

        def next_resource_i(self,workers,resources,start_index=0):
            """
            Increases start_index until index % len(workers)
            points to a worker which provides the resources.
            If no such worker exist, returns None.
            """
            for i in range(start_index, len(workers) + start_index):
                iworker = i % len(workers)
                suitable = True
                for resource,quantity in resources.items():
                    if not self.scheduler.worker_resources[workers[iworker]].has_key(resource) or \
                            quantity > self.scheduler.worker_resources[workers[iworker]][resource]:
                        suitable = False
                        break
                if suitable:
                    return i
            return None

    Additionally, the shift of resources would need to be handled. In WorkStealing.move_task, this would mean to add:

                self.scheduler.worker_comms[victim].send({'op': 'release-task',
                                                          'reason': 'stolen',
                                                          'key': key})
                self.scheduler.release_resources(key, victim) # new
    
                try:
                    self.scheduler.send_task_to_worker(thief, key)
                    self.scheduler.consume_resources(key, thief) # new

    Then there would be one last change in scheduler.py, since resources would need to be released from the right worker. In Scheduler.transition_processing_memory, the release of resources would need to be shifted behind w = self.rprocessing.pop(key)

                # old
                # self.release_resources(key, worker)
                assert worker
    
                self.who_has[key].add(worker)
                self.has_what[worker].add(key)
                self.worker_bytes[worker] += self.nbytes.get(key,
                                                             DEFAULT_DATA_SIZE)
    
                w = self.rprocessing.pop(key)
                # new
                self.release_resources(key, w)

    For some simple test cases this seems to be working for me. Resource restricted tasks are redistributed among the workers and scheduler.used_resources seems to be behaving as expected.

    Sorry for the long comment, but probably this is too early for a Pull Request. Thanks!

  2. mrocklin commented on Sep 13, 2017

    @mrocklin
    Member

    For scheduling on resource restrictions you'll want to both ensure that the worker still has those resources (remember that semantically resources get used up as tasks run on those workers), and you may want to choose the best worker with those resources, rather than do a round-robin. See Scheduler.worker_objective.

    Additionally we tend to only steal onto workers that are idling.

  3. m-albert commented on Sep 13, 2017

    @m-albert
    Author

    Thanks for the feedback!

    For scheduling on resource restrictions you'll want to both ensure that the worker still has those resources (remember that semantically resources get used up as tasks run on those workers)

    Yes, if I understand correctly only idle workers can be thiefs. The resource check in next_resource_i(idle..) compares to the total resources available to the idle workers (for idle workers scheduler.used_resources[w] == scheduler.worker_resources[w]).

    and you may want to choose the best worker with those resources, rather than do a round-robin. See Scheduler.worker_objective.

    In the current implementation of WorkStealing, Scheduler.worker_objective doesn't seem to be taken into account (but only for the initial worker assignment using Scheduler.decide_worker). Of course the best idle worker (for example the one with most available relevant resources) could be chosen from the idle worker list within next_resource_i.

    Related to this:

    Is it possible that currently workers are not being checked for 'still available' resources? The relevant lines seem to be 3099-3102 in scheduler.py:

            if self.resource_restrictions.get(key):
                w = {resource: {w for w, supplied in self.resources[resource].items()
                                if supplied >= required}
                     for resource, required in self.resource_restrictions[key].items()}

    Shouldn't supplied be taken from the difference between the quantities in self.resources and self.used_resources rather than only from self.resources?

  4. mrocklin commented on Sep 13, 2017

    @mrocklin
    Member

    Yes, if I understand correctly only idle workers can be thiefs. The resource check in next_resource_i(idle..) compares to the total resources available to the idle workers (for idle workers scheduler.used_resources[w] == scheduler.worker_resources[w]).

    Yes that's a valid point. If you're only taking from idling workers then we don't really care about best. Any from that set will do. Sorry for being a bit sloppy in my suggestions. My brain is cycling over a few things.

    It's technically valid to schedule a task to a worker who is running low on resources (the worker will wait until resources are freed) but you're right that it would be nicer to prefer workers who can run the task sooner.

  5. m-albert commented on Sep 13, 2017

    @m-albert
    Author

    True, so probably any idle worker with sufficient resources should be a good choice.

    Actually, related to my last point above: In scheduler.py, shoudn't self.used_resources be initialised with {r:0 for r in resources.keys()}?

        def add_resources(self, stream=None, worker=None, resources=None):
            if worker not in self.worker_resources:
                self.worker_resources[worker] = resources
                self.used_resources[worker] = resources # shoudn't this be {r:0 for r in resources.keys()}?
                for resource, quantity in resources.items():
                    self.resources[resource][worker] = quantity
            return 'OK'
  6. mrocklin commented on Sep 13, 2017

    @mrocklin
    Member
  7. vincentschut commented on Oct 20, 2017

    @vincentschut

    Apparently I am running into this (https://stackoverflow.com/questions/46849315/schedule-tasks-based-purely-on-idleness-instead-of-data-communication, I first thought the cause was different). This issue seems stalled? Is implementing this still somewhere on the agenda? Or should I not hold my breath and find a workaround...?

  8. mrocklin commented on Oct 20, 2017

    @mrocklin
    Member

    If you have time to try to resolve this issue that would be very welcome @vincentschut .

  9. mattilyra commented on Mar 22, 2018

    @mattilyra
    Contributor

    What is the motivation for host and worker restrictions blocking work stealing?

    Surely there could easily be a situation where the worker or host restrictions for a particular task is a list of say 10 hosts out of 50 hosts. Any of the hosts from the restricted host list would do just fine.

    For the worker_restrictions this would seem to be a fairly simple filter on idle before the thief worker is selected

        if ts.worker_restrictions:
            idle = [ws for ws in idle if ws.address in ts.worker_restrictions]
    

    handling the host host_restrictions seems a bit more complicated as getting the host names seems not to be straightforward.

  10. mrocklin commented on Mar 22, 2018

    @mrocklin
    Member
  11. seibert commented on Sep 11, 2019

    @seibert
    Contributor

    In case anyone stumbles on this issue, there is a stalled PR here to fix this: #2740

  12. lesteve commented on Mar 5, 2020

    @lesteve
    Member

    I guess this one can be closed since #3069 has been merged.

  13. jrbourbeau commented on Mar 5, 2020

    @jrbourbeau
    Member

    Agreed, thanks for following up @lesteve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions