]> git.ipfire.org Git - pbs.git/commitdiff
builders: Improve autoscale algorithm
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 May 2023 14:21:06 +0000 (14:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 May 2023 14:21:06 +0000 (14:21 +0000)
We will try to avoid starting any builders that are currently shut down.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/builders.py

index f709e811dbff9c1996e7c366d7b234925685c16c..46a2eb5209eb615dab38a926cfce5f554a375b6c 100644 (file)
@@ -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