From 08e841eaeefd77a98de5b2ff0c74c74be27d89e5 Mon Sep 17 00:00:00 2001 From: Tom Sparrow <793763+sparrowt@users.noreply.github.com> Date: Wed, 6 Mar 2024 16:06:17 +0000 Subject: [PATCH] JENKINS-66373: idle timeout should not fight with min spare instances setting Prior to this change, if you use "Minimum number of spare instances" setting as well as "Idle termination time", then there is a constant battle going on between: - `checkForMinimumInstances` trying to maintain the minimum spare instances as requested - the idle timeout checks in this method are killing off the 'spare' instances Make this method take account of "Minimum number of _spare_ instances" in the same way that it already accounts for the main "Minimum number of instances" setting. --- .../java/hudson/plugins/ec2/EC2RetentionStrategy.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java b/src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java index 748e88fef..159a35da6 100644 --- a/src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java +++ b/src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java @@ -131,9 +131,14 @@ private long internalCheck(EC2Computer computer) { SlaveTemplate slaveTemplate = computer.getSlaveTemplate(); if (slaveTemplate != null) { long numberOfCurrentInstancesForTemplate = MinimumInstanceChecker.countCurrentNumberOfAgents(slaveTemplate); - if (numberOfCurrentInstancesForTemplate > 0 && numberOfCurrentInstancesForTemplate <= slaveTemplate.getMinimumNumberOfInstances()) { - //Check if we're in an active time-range for keeping minimum number of instances + long numberOfCurrentSpareInstancesForTemplate = MinimumInstanceChecker.countCurrentNumberOfSpareAgents(slaveTemplate); + boolean atOrBelowMinInstances = numberOfCurrentInstancesForTemplate > 0 && numberOfCurrentInstancesForTemplate <= slaveTemplate.getMinimumNumberOfInstances(); + boolean atOrBelowMinSpareInstances = numberOfCurrentSpareInstancesForTemplate > 0 && numberOfCurrentSpareInstancesForTemplate <= slaveTemplate.getMinimumNumberOfSpareInstances(); + if (atOrBelowMinInstances || atOrBelowMinSpareInstances) { + // Check if we're in an active time-range for keeping minimum number of instances if (MinimumInstanceChecker.minimumInstancesActive(slaveTemplate.getMinimumNumberOfInstancesTimeRangeConfig())) { + // Time range is active and we are at (or below) one of the "minimum number" settings, so return here + // to avoid terminating any instances seeing as MinimumInstanceChecker will immediately re-provision return 1; } }