3 from __future__ import annotations
5 from abc import ABC, abstractmethod
6 import concurrent.futures as fut
7 from collections import defaultdict
8 from dataclasses import dataclass
17 from typing import Any, Callable, Dict, List, Optional, Set
20 import cloudpickle # type: ignore
21 from overrides import overrides
23 from ansi import bg, fg, underline, reset
26 from decorator_utils import singleton
27 from exec_utils import run_silently, cmd_in_background, cmd_with_timeout
28 import histogram as hist
29 from thread_utils import background_thread
32 logger = logging.getLogger(__name__)
34 parser = config.add_commandline_args(
35 f"Executors ({__file__})", "Args related to processing executors."
38 '--executors_threadpool_size',
41 help='Number of threads in the default threadpool, leave unset for default',
45 '--executors_processpool_size',
48 help='Number of processes in the default processpool, leave unset for default',
52 '--executors_schedule_remote_backups',
54 action=argparse_utils.ActionNoYes,
55 help='Should we schedule duplicative backup work if a remote bundle is slow',
58 '--executors_max_bundle_failures',
62 help='Maximum number of failures before giving up on a bundle',
65 SSH = '/usr/bin/ssh -oForwardX11=no'
66 SCP = '/usr/bin/scp -C'
69 def make_cloud_pickle(fun, *args, **kwargs):
70 logger.debug(f"Making cloudpickled bundle at {fun.__name__}")
71 return cloudpickle.dumps((fun, args, kwargs))
74 class BaseExecutor(ABC):
75 def __init__(self, *, title=''):
78 self.histogram = hist.SimpleHistogram(
79 hist.SimpleHistogram.n_evenly_spaced_buckets(int(0), int(500), 50)
83 def submit(self, function: Callable, *args, **kwargs) -> fut.Future:
87 def shutdown(self, wait: bool = True) -> None:
90 def adjust_task_count(self, delta: int) -> None:
91 self.task_count += delta
92 logger.debug(f'Executor current task count is {self.task_count}')
95 class ThreadExecutor(BaseExecutor):
96 def __init__(self, max_workers: Optional[int] = None):
99 if max_workers is not None:
100 workers = max_workers
101 elif 'executors_threadpool_size' in config.config:
102 workers = config.config['executors_threadpool_size']
103 logger.debug(f'Creating threadpool executor with {workers} workers')
104 self._thread_pool_executor = fut.ThreadPoolExecutor(
105 max_workers=workers, thread_name_prefix="thread_executor_helper"
108 def run_local_bundle(self, fun, *args, **kwargs):
109 logger.debug(f"Running local bundle at {fun.__name__}")
111 result = fun(*args, **kwargs)
113 self.adjust_task_count(-1)
114 duration = end - start
115 logger.debug(f"{fun.__name__} finished; used {duration:.1f}s")
116 self.histogram.add_item(duration)
120 def submit(self, function: Callable, *args, **kwargs) -> fut.Future:
121 self.adjust_task_count(+1)
123 newargs.append(function)
126 return self._thread_pool_executor.submit(
127 self.run_local_bundle, *newargs, **kwargs
131 def shutdown(self, wait=True) -> None:
132 logger.debug(f'Shutting down threadpool executor {self.title}')
133 print(self.histogram)
134 self._thread_pool_executor.shutdown(wait)
137 class ProcessExecutor(BaseExecutor):
138 def __init__(self, max_workers=None):
141 if max_workers is not None:
142 workers = max_workers
143 elif 'executors_processpool_size' in config.config:
144 workers = config.config['executors_processpool_size']
145 logger.debug(f'Creating processpool executor with {workers} workers.')
146 self._process_executor = fut.ProcessPoolExecutor(
150 def run_cloud_pickle(self, pickle):
151 fun, args, kwargs = cloudpickle.loads(pickle)
152 logger.debug(f"Running pickled bundle at {fun.__name__}")
153 result = fun(*args, **kwargs)
154 self.adjust_task_count(-1)
158 def submit(self, function: Callable, *args, **kwargs) -> fut.Future:
160 self.adjust_task_count(+1)
161 pickle = make_cloud_pickle(function, *args, **kwargs)
162 result = self._process_executor.submit(self.run_cloud_pickle, pickle)
163 result.add_done_callback(lambda _: self.histogram.add_item(time.time() - start))
167 def shutdown(self, wait=True) -> None:
168 logger.debug(f'Shutting down processpool executor {self.title}')
169 self._process_executor.shutdown(wait)
170 print(self.histogram)
172 def __getstate__(self):
173 state = self.__dict__.copy()
174 state['_process_executor'] = None
178 class RemoteExecutorException(Exception):
179 """Thrown when a bundle cannot be executed despite several retries."""
185 class RemoteWorkerRecord:
192 return hash((self.username, self.machine))
195 return f'{self.username}@{self.machine}'
203 worker: Optional[RemoteWorkerRecord]
204 username: Optional[str]
205 machine: Optional[str]
212 slower_than_local_p95: bool
213 slower_than_global_p95: bool
214 src_bundle: BundleDetails
215 is_cancelled: threading.Event
217 backup_bundles: Optional[List[BundleDetails]]
222 if uuid[-9:-2] == '_backup':
224 suffix = f'{uuid[-6:]}_b{self.uuid[-1:]}'
234 fg('marigold yellow'),
237 fg('cornflower blue'),
238 fg('turquoise blue'),
240 fg('lavender purple'),
243 c = colorz[int(uuid[-2:], 16) % len(colorz)]
244 fname = self.fname if self.fname is not None else 'nofname'
245 machine = self.machine if self.machine is not None else 'nomachine'
246 return f'{c}{suffix}/{fname}/{machine}{reset()}'
249 class RemoteExecutorStatus:
250 def __init__(self, total_worker_count: int) -> None:
251 self.worker_count: int = total_worker_count
252 self.known_workers: Set[RemoteWorkerRecord] = set()
253 self.start_time: float = time.time()
254 self.start_per_bundle: Dict[str, float] = defaultdict(float)
255 self.end_per_bundle: Dict[str, float] = defaultdict(float)
256 self.finished_bundle_timings_per_worker: Dict[
257 RemoteWorkerRecord, List[float]
259 self.in_flight_bundles_by_worker: Dict[RemoteWorkerRecord, Set[str]] = {}
260 self.bundle_details_by_uuid: Dict[str, BundleDetails] = {}
261 self.finished_bundle_timings: List[float] = []
262 self.last_periodic_dump: Optional[float] = None
263 self.total_bundles_submitted: int = 0
265 # Protects reads and modification using self. Also used
266 # as a memory fence for modifications to bundle.
267 self.lock: threading.Lock = threading.Lock()
269 def record_acquire_worker(self, worker: RemoteWorkerRecord, uuid: str) -> None:
271 self.record_acquire_worker_already_locked(worker, uuid)
273 def record_acquire_worker_already_locked(
274 self, worker: RemoteWorkerRecord, uuid: str
276 assert self.lock.locked()
277 self.known_workers.add(worker)
278 self.start_per_bundle[uuid] = None
279 x = self.in_flight_bundles_by_worker.get(worker, set())
281 self.in_flight_bundles_by_worker[worker] = x
283 def record_bundle_details(self, details: BundleDetails) -> None:
285 self.record_bundle_details_already_locked(details)
287 def record_bundle_details_already_locked(self, details: BundleDetails) -> None:
288 assert self.lock.locked()
289 self.bundle_details_by_uuid[details.uuid] = details
291 def record_release_worker(
293 worker: RemoteWorkerRecord,
298 self.record_release_worker_already_locked(worker, uuid, was_cancelled)
300 def record_release_worker_already_locked(
302 worker: RemoteWorkerRecord,
306 assert self.lock.locked()
308 self.end_per_bundle[uuid] = ts
309 self.in_flight_bundles_by_worker[worker].remove(uuid)
310 if not was_cancelled:
311 bundle_latency = ts - self.start_per_bundle[uuid]
312 x = self.finished_bundle_timings_per_worker.get(worker, list())
313 x.append(bundle_latency)
314 self.finished_bundle_timings_per_worker[worker] = x
315 self.finished_bundle_timings.append(bundle_latency)
317 def record_processing_began(self, uuid: str):
319 self.start_per_bundle[uuid] = time.time()
321 def total_in_flight(self) -> int:
322 assert self.lock.locked()
324 for worker in self.known_workers:
325 total_in_flight += len(self.in_flight_bundles_by_worker[worker])
326 return total_in_flight
328 def total_idle(self) -> int:
329 assert self.lock.locked()
330 return self.worker_count - self.total_in_flight()
333 assert self.lock.locked()
335 total_finished = len(self.finished_bundle_timings)
336 total_in_flight = self.total_in_flight()
337 ret = f'\n\n{underline()}Remote Executor Pool Status{reset()}: '
339 if len(self.finished_bundle_timings) > 1:
340 qall = numpy.quantile(self.finished_bundle_timings, [0.5, 0.95])
342 f'⏱=∀p50:{qall[0]:.1f}s, ∀p95:{qall[1]:.1f}s, total={ts-self.start_time:.1f}s, '
343 f'✅={total_finished}/{self.total_bundles_submitted}, '
344 f'💻n={total_in_flight}/{self.worker_count}\n'
348 f'⏱={ts-self.start_time:.1f}s, '
349 f'✅={total_finished}/{self.total_bundles_submitted}, '
350 f'💻n={total_in_flight}/{self.worker_count}\n'
353 for worker in self.known_workers:
354 ret += f' {fg("lightning yellow")}{worker.machine}{reset()}: '
355 timings = self.finished_bundle_timings_per_worker.get(worker, [])
359 qworker = numpy.quantile(timings, [0.5, 0.95])
360 ret += f' 💻p50: {qworker[0]:.1f}s, 💻p95: {qworker[1]:.1f}s\n'
364 ret += f' ...finished {count} total bundle(s) so far\n'
365 in_flight = len(self.in_flight_bundles_by_worker[worker])
367 ret += f' ...{in_flight} bundles currently in flight:\n'
368 for bundle_uuid in self.in_flight_bundles_by_worker[worker]:
369 details = self.bundle_details_by_uuid.get(bundle_uuid, None)
370 pid = str(details.pid) if (details and details.pid != 0) else "TBD"
371 if self.start_per_bundle[bundle_uuid] is not None:
372 sec = ts - self.start_per_bundle[bundle_uuid]
373 ret += f' (pid={pid}): {details} for {sec:.1f}s so far '
375 ret += f' {details} setting up / copying data...'
378 if qworker is not None:
380 ret += f'{bg("red")}>💻p95{reset()} '
381 if details is not None:
382 details.slower_than_local_p95 = True
384 if details is not None:
385 details.slower_than_local_p95 = False
389 ret += f'{bg("red")}>∀p95{reset()} '
390 if details is not None:
391 details.slower_than_global_p95 = True
393 details.slower_than_global_p95 = False
397 def periodic_dump(self, total_bundles_submitted: int) -> None:
398 assert self.lock.locked()
399 self.total_bundles_submitted = total_bundles_submitted
401 if self.last_periodic_dump is None or ts - self.last_periodic_dump > 5.0:
403 self.last_periodic_dump = ts
406 class RemoteWorkerSelectionPolicy(ABC):
407 def register_worker_pool(self, workers):
408 self.workers = workers
411 def is_worker_available(self) -> bool:
415 def acquire_worker(self, machine_to_avoid=None) -> Optional[RemoteWorkerRecord]:
419 class WeightedRandomRemoteWorkerSelectionPolicy(RemoteWorkerSelectionPolicy):
421 def is_worker_available(self) -> bool:
422 for worker in self.workers:
428 def acquire_worker(self, machine_to_avoid=None) -> Optional[RemoteWorkerRecord]:
430 for worker in self.workers:
431 for x in range(0, worker.count):
432 for y in range(0, worker.weight):
433 grabbag.append(worker)
435 for _ in range(0, 5):
436 random.shuffle(grabbag)
438 if worker.machine != machine_to_avoid or _ > 2:
441 logger.debug(f'Selected worker {worker}')
443 msg = 'Unexpectedly could not find a worker, retrying...'
448 class RoundRobinRemoteWorkerSelectionPolicy(RemoteWorkerSelectionPolicy):
449 def __init__(self) -> None:
453 def is_worker_available(self) -> bool:
454 for worker in self.workers:
461 self, machine_to_avoid: str = None
462 ) -> Optional[RemoteWorkerRecord]:
465 worker = self.workers[x]
469 if x >= len(self.workers):
472 logger.debug(f'Selected worker {worker}')
475 if x >= len(self.workers):
478 msg = 'Unexpectedly could not find a worker, retrying...'
483 class RemoteExecutor(BaseExecutor):
486 workers: List[RemoteWorkerRecord],
487 policy: RemoteWorkerSelectionPolicy,
490 self.workers = workers
492 self.worker_count = 0
493 for worker in self.workers:
494 self.worker_count += worker.count
495 if self.worker_count <= 0:
496 msg = f"We need somewhere to schedule work; count was {self.worker_count}"
498 raise RemoteExecutorException(msg)
499 self.policy.register_worker_pool(self.workers)
500 self.cv = threading.Condition()
502 f'Creating {self.worker_count} local threads, one per remote worker.'
504 self._helper_executor = fut.ThreadPoolExecutor(
505 thread_name_prefix="remote_executor_helper",
506 max_workers=self.worker_count,
508 self.status = RemoteExecutorStatus(self.worker_count)
509 self.total_bundles_submitted = 0
510 self.backup_lock = threading.Lock()
511 self.last_backup = None
513 self.heartbeat_thread,
514 self.heartbeat_stop_event,
515 ) = self.run_periodic_heartbeat()
518 def run_periodic_heartbeat(self, stop_event) -> None:
519 while not stop_event.is_set():
521 logger.debug('Running periodic heartbeat code...')
523 logger.debug('Periodic heartbeat thread shutting down.')
525 def heartbeat(self) -> None:
526 with self.status.lock:
527 # Dump regular progress report
528 self.status.periodic_dump(self.total_bundles_submitted)
530 # Look for bundles to reschedule via executor.submit
531 if config.config['executors_schedule_remote_backups']:
532 self.maybe_schedule_backup_bundles()
534 def maybe_schedule_backup_bundles(self):
535 assert self.status.lock.locked()
536 num_done = len(self.status.finished_bundle_timings)
537 num_idle_workers = self.worker_count - self.task_count
541 and num_idle_workers > 1
542 and (self.last_backup is None or (now - self.last_backup > 6.0))
543 and self.backup_lock.acquire(blocking=False)
546 assert self.backup_lock.locked()
548 bundle_to_backup = None
553 ) in self.status.in_flight_bundles_by_worker.items():
555 # Prefer to schedule backups of bundles running on
558 for record in self.workers:
559 if worker.machine == record.machine:
560 base_score = float(record.weight)
561 base_score = 1.0 / base_score
563 base_score = int(base_score)
566 for uuid in bundle_uuids:
567 bundle = self.status.bundle_details_by_uuid.get(uuid, None)
570 and bundle.src_bundle is None
571 and bundle.backup_bundles is not None
575 # Schedule backups of bundles running
576 # longer; especially those that are
578 start_ts = self.status.start_per_bundle[uuid]
579 if start_ts is not None:
580 runtime = now - start_ts
583 f'score[{bundle}] => {score} # latency boost'
586 if bundle.slower_than_local_p95:
589 f'score[{bundle}] => {score} # >worker p95'
592 if bundle.slower_than_global_p95:
595 f'score[{bundle}] => {score} # >global p95'
598 # Prefer backups of bundles that don't
599 # have backups already.
600 backup_count = len(bundle.backup_bundles)
601 if backup_count == 0:
603 elif backup_count == 1:
605 elif backup_count == 2:
610 f'score[{bundle}] => {score} # {backup_count} dup backup factor'
614 best_score is None or score > best_score
616 bundle_to_backup = bundle
617 assert bundle is not None
618 assert bundle.backup_bundles is not None
619 assert bundle.src_bundle is None
622 # Note: this is all still happening on the heartbeat
623 # runner thread. That's ok because
624 # schedule_backup_for_bundle uses the executor to
625 # submit the bundle again which will cause it to be
626 # picked up by a worker thread and allow this thread
627 # to return to run future heartbeats.
628 if bundle_to_backup is not None:
629 self.last_backup = now
631 f'=====> SCHEDULING BACKUP {bundle_to_backup} (score={best_score:.1f}) <====='
633 self.schedule_backup_for_bundle(bundle_to_backup)
635 self.backup_lock.release()
637 def is_worker_available(self) -> bool:
638 return self.policy.is_worker_available()
641 self, machine_to_avoid: str = None
642 ) -> Optional[RemoteWorkerRecord]:
643 return self.policy.acquire_worker(machine_to_avoid)
645 def find_available_worker_or_block(
646 self, machine_to_avoid: str = None
647 ) -> RemoteWorkerRecord:
649 while not self.is_worker_available():
651 worker = self.acquire_worker(machine_to_avoid)
652 if worker is not None:
654 msg = "We should never reach this point in the code"
658 def release_worker(self, bundle: BundleDetails, *, was_cancelled=True) -> None:
659 worker = bundle.worker
660 assert worker is not None
661 logger.debug(f'Released worker {worker}')
662 self.status.record_release_worker(
670 self.adjust_task_count(-1)
672 def check_if_cancelled(self, bundle: BundleDetails) -> bool:
673 with self.status.lock:
674 if bundle.is_cancelled.wait(timeout=0.0):
675 logger.debug(f'Bundle {bundle.uuid} is cancelled, bail out.')
676 bundle.was_cancelled = True
680 def launch(self, bundle: BundleDetails, override_avoid_machine=None) -> Any:
681 """Find a worker for bundle or block until one is available."""
682 self.adjust_task_count(+1)
684 hostname = bundle.hostname
685 avoid_machine = override_avoid_machine
686 is_original = bundle.src_bundle is None
688 # Try not to schedule a backup on the same host as the original.
689 if avoid_machine is None and bundle.src_bundle is not None:
690 avoid_machine = bundle.src_bundle.machine
692 while worker is None:
693 worker = self.find_available_worker_or_block(avoid_machine)
696 # Ok, found a worker.
697 bundle.worker = worker
698 machine = bundle.machine = worker.machine
699 username = bundle.username = worker.username
700 self.status.record_acquire_worker(worker, uuid)
701 logger.debug(f'{bundle}: Running bundle on {worker}...')
703 # Before we do any work, make sure the bundle is still viable.
704 # It may have been some time between when it was submitted and
705 # now due to lack of worker availability and someone else may
706 # have already finished it.
707 if self.check_if_cancelled(bundle):
709 return self.process_work_result(bundle)
710 except Exception as e:
712 f'{bundle}: bundle says it\'s cancelled upfront but no results?!'
714 self.release_worker(bundle)
716 # Weird. We are the original owner of this
717 # bundle. For it to have been cancelled, a backup
718 # must have already started and completed before
719 # we even for started. Moreover, the backup says
720 # it is done but we can't find the results it
721 # should have copied over. Reschedule the whole
725 f'{bundle}: We are the original owner thread and yet there are '
726 + 'no results for this bundle. This is unexpected and bad.'
728 return self.emergency_retry_nasty_bundle(bundle)
730 # Expected(?). We're a backup and our bundle is
731 # cancelled before we even got started. Something
732 # went bad in process_work_result (I acutually don't
733 # see what?) but probably not worth worrying
734 # about. Let the original thread worry about
735 # either finding the results or complaining about
739 # Send input code / data to worker machine if it's not local.
740 if hostname not in machine:
743 f'{SCP} {bundle.code_file} {username}@{machine}:{bundle.code_file}'
745 start_ts = time.time()
746 logger.info(f"{bundle}: Copying work to {worker} via {cmd}.")
748 xfer_latency = time.time() - start_ts
749 logger.debug(f"{bundle}: Copying to {worker} took {xfer_latency:.1f}s.")
750 except Exception as e:
751 self.release_worker(bundle)
753 # Weird. We tried to copy the code to the worker and it failed...
754 # And we're the original bundle. We have to retry.
757 f"{bundle}: Failed to send instructions to the worker machine?! "
758 + "This is not expected; we\'re the original bundle so this shouldn\'t "
759 + "be a race condition. Attempting an emergency retry..."
761 return self.emergency_retry_nasty_bundle(bundle)
763 # This is actually expected; we're a backup.
764 # There's a race condition where someone else
765 # already finished the work and removed the source
766 # code file before we could copy it. No biggie.
767 msg = f'{bundle}: Failed to send instructions to the worker machine... '
768 msg += 'We\'re a backup and this may be caused by the original (or some '
769 msg += 'other backup) already finishing this work. Ignoring this.'
773 # Kick off the work. Note that if this fails we let
774 # wait_for_process deal with it.
775 self.status.record_processing_began(uuid)
777 f'{SSH} {bundle.username}@{bundle.machine} '
778 f'"source py38-venv/bin/activate &&'
779 f' /home/scott/lib/python_modules/remote_worker.py'
780 f' --code_file {bundle.code_file} --result_file {bundle.result_file}"'
782 logger.debug(f'{bundle}: Executing {cmd} in the background to kick off work...')
783 p = cmd_in_background(cmd, silent=True)
786 f'{bundle}: Local ssh process pid={p.pid}; remote worker is {machine}.'
788 return self.wait_for_process(p, bundle, 0)
790 def wait_for_process(
791 self, p: subprocess.Popen, bundle: BundleDetails, depth: int
793 machine = bundle.machine
797 f"I've gotten repeated errors waiting on this bundle; giving up on pid={pid}."
800 self.release_worker(bundle)
801 return self.emergency_retry_nasty_bundle(bundle)
803 # Spin until either the ssh job we scheduled finishes the
804 # bundle or some backup worker signals that they finished it
809 except subprocess.TimeoutExpired:
810 if self.check_if_cancelled(bundle):
812 f'{bundle}: looks like another worker finished bundle...'
816 logger.info(f"{bundle}: pid {pid} ({machine}) is finished!")
820 # If we get here we believe the bundle is done; either the ssh
821 # subprocess finished (hopefully successfully) or we noticed
822 # that some other worker seems to have completed the bundle
823 # and we're bailing out.
825 ret = self.process_work_result(bundle)
826 if ret is not None and p is not None:
830 # Something went wrong; e.g. we could not copy the results
831 # back, cleanup after ourselves on the remote machine, or
832 # unpickle the results we got from the remove machine. If we
833 # still have an active ssh subprocess, keep waiting on it.
834 # Otherwise, time for an emergency reschedule.
835 except Exception as e:
837 logger.error(f'{bundle}: Something unexpected just happened...')
839 msg = f"{bundle}: Failed to wrap up \"done\" bundle, re-waiting on active ssh."
841 return self.wait_for_process(p, bundle, depth + 1)
843 self.release_worker(bundle)
844 return self.emergency_retry_nasty_bundle(bundle)
846 def process_work_result(self, bundle: BundleDetails) -> Any:
847 with self.status.lock:
848 is_original = bundle.src_bundle is None
849 was_cancelled = bundle.was_cancelled
850 username = bundle.username
851 machine = bundle.machine
852 result_file = bundle.result_file
853 code_file = bundle.code_file
855 # Whether original or backup, if we finished first we must
856 # fetch the results if the computation happened on a
858 bundle.end_ts = time.time()
859 if not was_cancelled:
860 assert bundle.machine is not None
861 if bundle.hostname not in bundle.machine:
862 cmd = f'{SCP} {username}@{machine}:{result_file} {result_file} 2>/dev/null'
864 f"{bundle}: Fetching results back from {username}@{machine} via {cmd}"
867 # If either of these throw they are handled in
873 except Exception as e:
881 f'{SSH} {username}@{machine}'
882 f' "/bin/rm -f {code_file} {result_file}"'
885 f'Fetching results back took {time.time() - bundle.end_ts:.1f}s.'
887 dur = bundle.end_ts - bundle.start_ts
888 self.histogram.add_item(dur)
890 # Only the original worker should unpickle the file contents
891 # though since it's the only one whose result matters. The
892 # original is also the only job that may delete result_file
893 # from disk. Note that the original may have been cancelled
894 # if one of the backups finished first; it still must read the
897 logger.debug(f"{bundle}: Unpickling {result_file}.")
899 with open(result_file, 'rb') as rb:
900 serialized = rb.read()
901 result = cloudpickle.loads(serialized)
902 except Exception as e:
904 msg = f'Failed to load {result_file}... this is bad news.'
906 self.release_worker(bundle)
908 # Re-raise the exception; the code in wait_for_process may
909 # decide to emergency_retry_nasty_bundle here.
911 logger.debug(f'Removing local (master) {code_file} and {result_file}.')
912 os.remove(f'{result_file}')
913 os.remove(f'{code_file}')
915 # Notify any backups that the original is done so they
916 # should stop ASAP. Do this whether or not we
917 # finished first since there could be more than one
919 if bundle.backup_bundles is not None:
920 for backup in bundle.backup_bundles:
922 f'{bundle}: Notifying backup {backup.uuid} that it\'s cancelled'
924 backup.is_cancelled.set()
926 # This is a backup job and, by now, we have already fetched
927 # the bundle results.
929 # Backup results don't matter, they just need to leave the
930 # result file in the right place for their originals to
931 # read/unpickle later.
934 # Tell the original to stop if we finished first.
935 if not was_cancelled:
937 f'{bundle}: Notifying original {bundle.src_bundle.uuid} we beat them to it.'
939 bundle.src_bundle.is_cancelled.set()
940 self.release_worker(bundle, was_cancelled=was_cancelled)
943 def create_original_bundle(self, pickle, fname: str):
944 from string_utils import generate_uuid
946 uuid = generate_uuid(omit_dashes=True)
947 code_file = f'/tmp/{uuid}.code.bin'
948 result_file = f'/tmp/{uuid}.result.bin'
950 logger.debug(f'Writing pickled code to {code_file}')
951 with open(f'{code_file}', 'wb') as wb:
954 bundle = BundleDetails(
961 hostname=platform.node(),
963 result_file=result_file,
965 start_ts=time.time(),
967 slower_than_local_p95=False,
968 slower_than_global_p95=False,
970 is_cancelled=threading.Event(),
975 self.status.record_bundle_details(bundle)
976 logger.debug(f'{bundle}: Created an original bundle')
979 def create_backup_bundle(self, src_bundle: BundleDetails):
980 assert src_bundle.backup_bundles is not None
981 n = len(src_bundle.backup_bundles)
982 uuid = src_bundle.uuid + f'_backup#{n}'
984 backup_bundle = BundleDetails(
985 pickled_code=src_bundle.pickled_code,
987 fname=src_bundle.fname,
991 hostname=src_bundle.hostname,
992 code_file=src_bundle.code_file,
993 result_file=src_bundle.result_file,
995 start_ts=time.time(),
997 slower_than_local_p95=False,
998 slower_than_global_p95=False,
999 src_bundle=src_bundle,
1000 is_cancelled=threading.Event(),
1001 was_cancelled=False,
1002 backup_bundles=None, # backup backups not allowed
1005 src_bundle.backup_bundles.append(backup_bundle)
1006 self.status.record_bundle_details_already_locked(backup_bundle)
1007 logger.debug(f'{backup_bundle}: Created a backup bundle')
1008 return backup_bundle
1010 def schedule_backup_for_bundle(self, src_bundle: BundleDetails):
1011 assert self.status.lock.locked()
1012 assert src_bundle is not None
1013 backup_bundle = self.create_backup_bundle(src_bundle)
1015 f'{backup_bundle.uuid}/{backup_bundle.fname}: Scheduling backup for execution...'
1017 self._helper_executor.submit(self.launch, backup_bundle)
1019 # Results from backups don't matter; if they finish first
1020 # they will move the result_file to this machine and let
1021 # the original pick them up and unpickle them.
1023 def emergency_retry_nasty_bundle(self, bundle: BundleDetails) -> fut.Future:
1024 is_original = bundle.src_bundle is None
1025 bundle.worker = None
1026 avoid_last_machine = bundle.machine
1027 bundle.machine = None
1028 bundle.username = None
1029 bundle.failure_count += 1
1035 if bundle.failure_count > retry_limit:
1037 f'{bundle}: Tried this bundle too many times already ({retry_limit}x); giving up.'
1040 raise RemoteExecutorException(
1041 f'{bundle}: This bundle can\'t be completed despite several backups and retries'
1045 f'{bundle}: At least it\'s only a backup; better luck with the others.'
1049 msg = f'>>> Emergency rescheduling {bundle} because of unexected errors (wtf?!) <<<'
1052 return self.launch(bundle, avoid_last_machine)
1055 def submit(self, function: Callable, *args, **kwargs) -> fut.Future:
1056 pickle = make_cloud_pickle(function, *args, **kwargs)
1057 bundle = self.create_original_bundle(pickle, function.__name__)
1058 self.total_bundles_submitted += 1
1059 return self._helper_executor.submit(self.launch, bundle)
1062 def shutdown(self, wait=True) -> None:
1063 logging.debug(f'Shutting down RemoteExecutor {self.title}')
1064 self.heartbeat_stop_event.set()
1065 self.heartbeat_thread.join()
1066 self._helper_executor.shutdown(wait)
1067 print(self.histogram)
1071 class DefaultExecutors(object):
1073 self.thread_executor: Optional[ThreadExecutor] = None
1074 self.process_executor: Optional[ProcessExecutor] = None
1075 self.remote_executor: Optional[RemoteExecutor] = None
1077 def ping(self, host) -> bool:
1078 logger.debug(f'RUN> ping -c 1 {host}')
1080 x = cmd_with_timeout(
1081 f'ping -c 1 {host} >/dev/null 2>/dev/null', timeout_seconds=1.0
1087 def thread_pool(self) -> ThreadExecutor:
1088 if self.thread_executor is None:
1089 self.thread_executor = ThreadExecutor()
1090 return self.thread_executor
1092 def process_pool(self) -> ProcessExecutor:
1093 if self.process_executor is None:
1094 self.process_executor = ProcessExecutor()
1095 return self.process_executor
1097 def remote_pool(self) -> RemoteExecutor:
1098 if self.remote_executor is None:
1099 logger.info('Looking for some helper machines...')
1100 pool: List[RemoteWorkerRecord] = []
1101 if self.ping('cheetah.house'):
1102 logger.info('Found cheetah.house')
1106 machine='cheetah.house',
1111 if self.ping('meerkat.cabin'):
1112 logger.info('Found meerkat.cabin')
1116 machine='meerkat.cabin',
1121 if self.ping('wannabe.house'):
1122 logger.info('Found wannabe.house')
1126 machine='wannabe.house',
1131 if self.ping('puma.cabin'):
1132 logger.info('Found puma.cabin')
1136 machine='puma.cabin',
1141 if self.ping('backup.house'):
1142 logger.info('Found backup.house')
1146 machine='backup.house',
1152 # The controller machine has a lot to do; go easy on it.
1154 if record.machine == platform.node() and record.count > 1:
1155 logger.info(f'Reducing workload for {record.machine}.')
1158 policy = WeightedRandomRemoteWorkerSelectionPolicy()
1159 policy.register_worker_pool(pool)
1160 self.remote_executor = RemoteExecutor(pool, policy)
1161 return self.remote_executor
1163 def shutdown(self) -> None:
1164 if self.thread_executor is not None:
1165 self.thread_executor.shutdown()
1166 self.thread_executor = None
1167 if self.process_executor is not None:
1168 self.process_executor.shutdown()
1169 self.process_executor = None
1170 if self.remote_executor is not None:
1171 self.remote_executor.shutdown()
1172 self.remote_executor = None