2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from __future__ import absolute_import, print_function
13 from . import clientactor
14 from .config import ARVADOS_ERRORS
17 class ServerCalculator(object):
18 """Generate cloud server wishlists from an Arvados job queue.
20 Instantiate this class with a list of cloud node sizes you're willing to
21 use, plus keyword overrides from the configuration. Then you can pass
22 job queues to servers_for_queue. It will return a list of node sizes
23 that would best satisfy the jobs, choosing the cheapest size that
24 satisfies each job, and ignoring jobs that can't be satisfied.
27 class CloudSizeWrapper(object):
28 def __init__(self, real_size, node_mem_scaling, **kwargs):
30 for name in ['id', 'name', 'ram', 'disk', 'bandwidth', 'price',
32 setattr(self, name, getattr(self.real, name))
33 self.cores = kwargs.pop('cores')
34 # libcloud disk sizes are in GB, Arvados/SLURM are in MB
35 # multiply by 1000 instead of 1024 to err on low side
38 self.scratch = self.disk * 1000
39 self.ram = int(self.ram * node_mem_scaling)
40 for name, override in kwargs.iteritems():
41 if not hasattr(self, name):
42 raise ValueError("unrecognized size field '%s'" % (name,))
43 setattr(self, name, override)
45 if self.price is None:
46 raise ValueError("Required field 'price' is None")
48 def meets_constraints(self, **kwargs):
49 for name, want_value in kwargs.iteritems():
50 have_value = getattr(self, name)
51 if (have_value != 0) and (have_value < want_value):
56 def __init__(self, server_list, max_nodes=None, max_price=None,
57 node_mem_scaling=0.95):
58 self.cloud_sizes = [self.CloudSizeWrapper(s, node_mem_scaling, **kws)
59 for s, kws in server_list]
60 self.cloud_sizes.sort(key=lambda s: s.price)
61 self.max_nodes = max_nodes or float('inf')
62 self.max_price = max_price or float('inf')
63 self.logger = logging.getLogger('arvnodeman.jobqueue')
65 self.logger.info("Using cloud node sizes:")
66 for s in self.cloud_sizes:
67 self.logger.info(str(s.__dict__))
70 def coerce_int(x, fallback):
73 except (TypeError, ValueError):
76 def cloud_size_for_constraints(self, constraints):
77 want_value = lambda key: self.coerce_int(constraints.get(key), 0)
78 wants = {'cores': want_value('min_cores_per_node'),
79 'ram': want_value('min_ram_mb_per_node'),
80 'scratch': want_value('min_scratch_mb_per_node')}
81 for size in self.cloud_sizes:
82 if size.meets_constraints(**wants):
86 def servers_for_queue(self, queue):
88 unsatisfiable_jobs = {}
90 constraints = job['runtime_constraints']
91 want_count = max(1, self.coerce_int(constraints.get('min_nodes'), 1))
92 cloud_size = self.cloud_size_for_constraints(constraints)
93 if cloud_size is None:
94 unsatisfiable_jobs[job['uuid']] = (
95 'Requirements for a single node exceed the available '
97 elif (want_count > self.max_nodes):
98 unsatisfiable_jobs[job['uuid']] = (
99 "Job's min_nodes constraint is greater than the configured "
100 "max_nodes (%d)" % self.max_nodes)
101 elif (want_count*cloud_size.price <= self.max_price):
102 servers.extend([cloud_size.real] * want_count)
104 unsatisfiable_jobs[job['uuid']] = (
105 "Job's price (%d) is above system's max_price "
106 "limit (%d)" % (want_count*cloud_size.price, self.max_price))
107 return (servers, unsatisfiable_jobs)
109 def cheapest_size(self):
110 return self.cloud_sizes[0]
112 def find_size(self, sizeid):
113 for s in self.cloud_sizes:
119 class JobQueueMonitorActor(clientactor.RemotePollLoopActor):
120 """Actor to generate server wishlists from the job queue.
122 This actor regularly polls Arvados' job queue, and uses the provided
123 ServerCalculator to turn that into a list of requested node sizes. That
124 list is sent to subscribers on every poll.
127 CLIENT_ERRORS = ARVADOS_ERRORS
129 def __init__(self, client, timer_actor, server_calc,
130 jobs_queue, slurm_queue, *args, **kwargs):
131 super(JobQueueMonitorActor, self).__init__(
132 client, timer_actor, *args, **kwargs)
133 self.jobs_queue = jobs_queue
134 self.slurm_queue = slurm_queue
135 self._calculator = server_calc
142 elif u in ("G", "g"):
143 return float(v) * 2**10
144 elif u in ("T", "t"):
145 return float(v) * 2**20
146 elif u in ("P", "p"):
147 return float(v) * 2**30
151 def _send_request(self):
154 # cpus, memory, tempory disk space, reason, job name
155 squeue_out = subprocess.check_output(["squeue", "--state=PENDING", "--noheader", "--format=%c|%m|%d|%r|%j"])
156 for out in squeue_out.splitlines():
158 cpu, ram, disk, reason, jobname = out.split("|", 4)
159 if ("ReqNodeNotAvail" in reason) or ("Resources" in reason) or ("Priority" in reason):
162 "runtime_constraints": {
163 "min_cores_per_node": cpu,
164 "min_ram_mb_per_node": self.coerce_to_mb(ram),
165 "min_scratch_mb_per_node": self.coerce_to_mb(disk)
172 queuelist.extend(self._client.jobs().queue().execute()['items'])
176 def _got_response(self, queue):
177 server_list, unsatisfiable_jobs = self._calculator.servers_for_queue(queue)
178 # Cancel any job/container with unsatisfiable requirements, emitting
179 # a log explaining why.
180 for job_uuid, reason in unsatisfiable_jobs.iteritems():
182 self._client.logs().create(body={
183 'object_uuid': job_uuid,
184 'event_type': 'stderr',
185 'properties': {'text': reason},
187 # Cancel the job depending on its type
188 if arvados.util.container_uuid_pattern.match(job_uuid):
189 subprocess.check_call(['scancel', '--name='+job_uuid])
190 elif arvados.util.job_uuid_pattern.match(job_uuid):
191 self._client.jobs().cancel(uuid=job_uuid).execute()
193 raise Exception('Unknown job type')
194 self._logger.debug("Cancelled unsatisfiable job '%s'", job_uuid)
195 except Exception as error:
196 self._logger.error("Trying to cancel job '%s': %s",
199 self._logger.debug("Calculated wishlist: %s",
200 ', '.join(s.name for s in server_list) or "(empty)")
201 return super(JobQueueMonitorActor, self)._got_response(server_list)