- # Look for bundles to reschedule
- if len(self.status.finished_bundle_timings) > 7:
- for worker, bundle_uuids in self.status.in_flight_bundles_by_worker.items():
- for uuid in bundle_uuids:
- bundle = self.status.bundle_details_by_uuid.get(uuid, None)
- if (
- bundle is not None and
- bundle.too_slow and
- bundle.src_bundle is None and
- config.config['executors_schedule_remote_backups']
- ):
- self.consider_backup_for_bundle(bundle)
-
- def consider_backup_for_bundle(self, bundle: BundleDetails) -> None:
- assert self.status.lock.locked()
- if (
- bundle.too_slow
- and len(bundle.backup_bundles) == 0 # one backup per
- ):
- msg = f"*** Rescheduling {bundle.pid}/{bundle.uuid} ***"
- logger.debug(msg)
- self.schedule_backup_for_bundle(bundle)
- return
- elif (
- bundle.super_slow
- and len(bundle.backup_bundles) < 2 # two backups in dire situations
- and self.status.total_idle() > 4
- ):
- msg = f"*** Rescheduling {bundle.pid}/{bundle.uuid} ***"
- logger.debug(msg)
- self.schedule_backup_for_bundle(bundle)
- return
+ # Look for bundles to reschedule.
+ num_done = len(self.status.finished_bundle_timings)
+ num_idle_workers = self.worker_count - self.task_count
+ now = time.time()
+ if (
+ config.config['executors_schedule_remote_backups']
+ and num_done > 2
+ and num_idle_workers > 1
+ and (self.last_backup is None or (now - self.last_backup > 1.0))
+ and self.backup_lock.acquire(blocking=False)
+ ):
+ try:
+ assert self.backup_lock.locked()
+
+ bundle_to_backup = None
+ best_score = None
+ for worker, bundle_uuids in self.status.in_flight_bundles_by_worker.items():
+ # Prefer to schedule backups of bundles on slower machines.
+ base_score = 0
+ for record in self.workers:
+ if worker.machine == record.machine:
+ base_score = float(record.weight)
+ base_score = 1.0 / base_score
+ base_score *= 200.0
+ base_score = int(base_score)
+ break
+
+ for uuid in bundle_uuids:
+ bundle = self.status.bundle_details_by_uuid.get(uuid, None)
+ if (
+ bundle is not None
+ and bundle.src_bundle is None
+ and bundle.backup_bundles is not None
+ ):
+ score = base_score
+
+ # Schedule backups of bundles running longer; especially those
+ # that are unexpectedly slow.
+ start_ts = self.status.start_per_bundle[uuid]
+ if start_ts is not None:
+ runtime = now - start_ts
+ score += runtime
+ logger.debug(f'score[{bundle}] => {score} # latency boost')
+
+ if bundle.slower_than_local_p95:
+ score += runtime / 2
+ logger.debug(f'score[{bundle}] => {score} # >worker p95')
+
+ if bundle.slower_than_global_p95:
+ score += runtime / 2
+ logger.debug(f'score[{bundle}] => {score} # >global p95')
+
+ # Prefer backups of bundles that don't have backups already.
+ backup_count = len(bundle.backup_bundles)
+ if backup_count == 0:
+ score *= 2
+ elif backup_count == 1:
+ score /= 2
+ elif backup_count == 2:
+ score /= 8
+ else:
+ score = 0
+ logger.debug(f'score[{bundle}] => {score} # {backup_count} dup backup factor')
+
+ if (
+ score != 0
+ and (best_score is None or score > best_score)
+ ):
+ bundle_to_backup = bundle
+ assert bundle is not None
+ assert bundle.backup_bundles is not None
+ assert bundle.src_bundle is None
+ best_score = score
+
+ if bundle_to_backup is not None:
+ self.last_backup = now
+ logger.info(f'=====> SCHEDULING BACKUP {bundle_to_backup} (score={best_score:.1f}) <=====')
+ self.schedule_backup_for_bundle(bundle_to_backup)
+ finally:
+ self.backup_lock.release()