Source code for mosaic.runtime.strategies



[docs] class MonitorStrategy: """ Base class for the strategies used to allocate tesserae to workers. """ def __init__(self, monitor): self._monitor = monitor
[docs] def update_node(self, updated): """ Update inner record of node state. Parameters ---------- updated : MonitoredNode Returns ------- """ pass
[docs] def update_tessera(self, updated): """ Update inner record of tesserae state. Parameters ---------- updated : MonitoredTessera Returns ------- """ pass
[docs] def update_task(self, updated): """ Update inner record of task state. Parameters ---------- updated : MonitoredTask Returns ------- """ pass
[docs] def select_worker(self, sender_id): """ Select an appropriate worker. Parameters ---------- sender_id : str Returns ------- """ pass
[docs] class RoundRobin(MonitorStrategy): """ Round robin strategy for allocating tesserae. """ def __init__(self, monitor): super().__init__(monitor) self._worker_list = set() self._num_workers = -1 self._last_worker = -1
[docs] def update_node(self, updated): for worker_id in updated.sub_resources['workers'].keys(): self._worker_list.add(worker_id) self._num_workers = len(self._worker_list)
[docs] def select_worker(self, sender_id): self._last_worker = (self._last_worker + 1) % self._num_workers return list(self._worker_list)[self._last_worker]