From: Michael Tremer Date: Fri, 5 May 2023 14:21:06 +0000 (+0000) Subject: builders: Improve autoscale algorithm X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f447be0049beeccb1d96d3b2f98f5f5a9197b6b6;p=pbs.git builders: Improve autoscale algorithm We will try to avoid starting any builders that are currently shut down. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builders.py b/src/buildservice/builders.py index f709e811..46a2eb52 100644 --- a/src/buildservice/builders.py +++ b/src/buildservice/builders.py @@ -96,13 +96,18 @@ class Builders(base.Object): # XXX max queue length threshold = datetime.timedelta(minutes=5) - # Fetch all enabled builders and whether they are running or not - builders = { - builder : await builder.is_running() for builder in self if builder.enabled - } + # Fetch all enabled builders + builders = [b for b in self if b.enabled] + + # Fetch the priority for each builder + builders = dict( + zip(builders, await asyncio.gather( + *(b._autoscale_sort() for b in builders), + )), + ) - # Sort all running builders to the beginning of the list - builders = sorted(builders, key=lambda b: (builders[b], b)) + # Sort all builders after their priority + builders = sorted(builders, key=lambda b: (-builders[b], b)) # Store the length of the queue for each builder queue = { @@ -676,6 +681,28 @@ class Builder(base.DataObject): log.debug("%s has been stopped" % self) + async def _autoscale_sort(self): + """ + This function returns a number that tells the autoscaling algorithm + in which order a builder should be considered. + + The higher the number, the sooner the builder is considered. + """ + # Return something neutral if this isn't an AWS instance + if not self.instance: + return 0 + + # Running builds should come first + if await self.is_running(): + return 1 + + # Put builders that are shutting down at the end + elif await self.is_shutting_down(): + return -1 + + # Otherwise return a neutral preference + return 0 + # Stats @lazy_property