@@ -375,9 +375,10 @@ public int getApproximateVirtualMachineCount() {
375375 * of virtual machines we currently have available.
376376 *
377377 * @param delta Number that are desired, or if less than 0, the number we are 'returning' to the pool
378+ * @param templateLimit max agents for the template in use
378379 * @return Number that can be allocated, up to the number desired. 0 if the number desired was < 0
379380 */
380- public int adjustVirtualMachineCount (int delta ) {
381+ public int adjustVirtualMachineCount (int delta , int templateLimit ) {
381382 synchronized (this ) {
382383 if (delta < 0 ) {
383384 LOGGER .log (Level .FINE , "Current estimated VM count: {0}, reducing by {1}" ,
@@ -387,7 +388,8 @@ public int adjustVirtualMachineCount(int delta) {
387388 } else {
388389 LOGGER .log (Level .FINE , "Current estimated VM count: {0}, quantity desired {1}" ,
389390 new Object []{approximateVirtualMachineCount , delta });
390- if (approximateVirtualMachineCount + delta <= getMaxVirtualMachinesLimit ()) {
391+ int limit = determineLimit (templateLimit );
392+ if (approximateVirtualMachineCount + delta <= limit ) {
391393 // Enough available, return the desired quantity, and update the number we think we
392394 // have laying around.
393395 approximateVirtualMachineCount += delta ;
@@ -396,14 +398,25 @@ public int adjustVirtualMachineCount(int delta) {
396398 // Not enough available, return what we have. Remember we could
397399 // go negative (if for instance another Jenkins instance had
398400 // a higher limit.
399- int quantityAvailable = Math .max (0 , getMaxVirtualMachinesLimit () - approximateVirtualMachineCount );
401+ int quantityAvailable = Math .max (0 , limit - approximateVirtualMachineCount );
400402 approximateVirtualMachineCount += quantityAvailable ;
401403 return quantityAvailable ;
402404 }
403405 }
404406 }
405407 }
406408
409+ private int determineLimit (int templateLimit ) {
410+ int cloudLimit = getMaxVirtualMachinesLimit ();
411+ if (templateLimit > cloudLimit ) {
412+ return cloudLimit ;
413+ }
414+ if (templateLimit == 0 ) {
415+ return cloudLimit ;
416+ }
417+ return templateLimit ;
418+ }
419+
407420 /**
408421 * Sets the new approximate virtual machine count.
409422 * This is run by the verification task to update the VM count periodically.
@@ -670,11 +683,12 @@ public Collection<PlannedNode> provision(CloudState cloudState, int workLoad) {
670683 try {
671684 // Determine how many agents we can actually provision from here and
672685 // adjust our count (before deployment to avoid races)
673- int adjustedNumberOfAgents = adjustVirtualMachineCount (numberOfAgents );
686+ int adjustedNumberOfAgents = adjustVirtualMachineCount (numberOfAgents ,
687+ template .getMaxVirtualMachinesLimit ());
674688 if (adjustedNumberOfAgents == 0 ) {
675689 LOGGER .log (Level .INFO ,
676690 "Not able to create {0} nodes, at or above maximum VM count of {1} and already {2} VM(s)" ,
677- new Object []{numberOfAgents , getMaxVirtualMachinesLimit (),
691+ new Object []{numberOfAgents , determineLimit ( template . getMaxVirtualMachinesLimit () ),
678692 getApproximateVirtualMachineCount ()});
679693 return plannedNodes ;
680694 } else if (adjustedNumberOfAgents < numberOfAgents ) {
@@ -844,7 +858,8 @@ private void handleFailure(
844858 // Do not throw to avoid it being recorded
845859 }
846860 }
847- template .retrieveAzureCloudReference ().adjustVirtualMachineCount (-1 );
861+ template .retrieveAzureCloudReference ().adjustVirtualMachineCount (-1 ,
862+ template .getMaxVirtualMachinesLimit ());
848863 // Update the template status given this new issue.
849864 template .handleTemplateProvisioningFailure (e .getMessage (), stage );
850865 }
0 commit comments