forked from jenkinsci/google-compute-engine-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a ProvisionerStrategy to provision a node without delay (je…
…nkinsci#183) * Introduce a ProvisionerStrategy to provision a node without delay * Add ITs for the NoDelayProvisionerStrategy * fix dependencies required when using the jenkins-pipeline library as the LTS has been bumped
- Loading branch information
Showing
9 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/java/com/google/jenkins/plugins/computeengine/NoDelayProvisionerStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2020 Elastic, and a number of other of contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.jenkins.plugins.computeengine; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Label; | ||
import hudson.model.LoadStatistics; | ||
import hudson.slaves.Cloud; | ||
import hudson.slaves.CloudProvisioningListener; | ||
import hudson.slaves.NodeProvisioner; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import jenkins.model.Jenkins; | ||
|
||
/** | ||
* Implementation of {@link NodeProvisioner.Strategy} which will provision a new node immediately as | ||
* a task enter the queue. | ||
*/ | ||
@Extension(ordinal = 100) | ||
public class NoDelayProvisionerStrategy extends NodeProvisioner.Strategy { | ||
private static final Logger LOGGER = Logger.getLogger(NoDelayProvisionerStrategy.class.getName()); | ||
|
||
private static final boolean DISABLE_NODELAY_PROVISING = | ||
Boolean.valueOf( | ||
System.getProperty( | ||
"com.google.jenkins.plugins.computeengine.disableNoDelayProvisioning")); | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public NodeProvisioner.StrategyDecision apply(NodeProvisioner.StrategyState strategyState) { | ||
if (DISABLE_NODELAY_PROVISING) { | ||
LOGGER.log(Level.FINE, "Provisioning not complete, NoDelayProvisionerStrategy is disabled"); | ||
return NodeProvisioner.StrategyDecision.CONSULT_REMAINING_STRATEGIES; | ||
} | ||
final Label label = strategyState.getLabel(); | ||
|
||
LoadStatistics.LoadStatisticsSnapshot snapshot = strategyState.getSnapshot(); | ||
int availableCapacity = | ||
snapshot.getAvailableExecutors() // live executors | ||
+ snapshot.getConnectingExecutors() // executors present but not yet connected | ||
+ strategyState | ||
.getPlannedCapacitySnapshot() // capacity added by previous strategies from previous | ||
// rounds | ||
+ strategyState | ||
.getAdditionalPlannedCapacity(); // capacity added by previous strategies _this | ||
// round_ | ||
int currentDemand = snapshot.getQueueLength(); | ||
LOGGER.log( | ||
Level.FINE, | ||
"Available capacity={0}, currentDemand={1}", | ||
new Object[] {availableCapacity, currentDemand}); | ||
if (availableCapacity < currentDemand) { | ||
List<Cloud> jenkinsClouds = new ArrayList<>(Jenkins.get().clouds); | ||
Collections.shuffle(jenkinsClouds); | ||
for (Cloud cloud : jenkinsClouds) { | ||
int workloadToProvision = currentDemand - availableCapacity; | ||
if (!(cloud instanceof ComputeEngineCloud)) continue; | ||
if (!cloud.canProvision(label)) continue; | ||
ComputeEngineCloud gcp = (ComputeEngineCloud) cloud; | ||
if (!gcp.isNoDelayProvisioning()) continue; | ||
for (CloudProvisioningListener cl : CloudProvisioningListener.all()) { | ||
if (cl.canProvision(cloud, strategyState.getLabel(), workloadToProvision) != null) { | ||
continue; | ||
} | ||
} | ||
Collection<NodeProvisioner.PlannedNode> plannedNodes = | ||
cloud.provision(label, workloadToProvision); | ||
LOGGER.log(Level.FINE, "Planned {0} new nodes", plannedNodes.size()); | ||
fireOnStarted(cloud, strategyState.getLabel(), plannedNodes); | ||
strategyState.recordPendingLaunches(plannedNodes); | ||
availableCapacity += plannedNodes.size(); | ||
LOGGER.log( | ||
Level.FINE, | ||
"After provisioning, available capacity={0}, currentDemand={1}", | ||
new Object[] {availableCapacity, currentDemand}); | ||
break; | ||
} | ||
} | ||
if (availableCapacity >= currentDemand) { | ||
LOGGER.log(Level.FINE, "Provisioning completed"); | ||
return NodeProvisioner.StrategyDecision.PROVISIONING_COMPLETED; | ||
} else { | ||
LOGGER.log(Level.FINE, "Provisioning not complete, consulting remaining strategies"); | ||
return NodeProvisioner.StrategyDecision.CONSULT_REMAINING_STRATEGIES; | ||
} | ||
} | ||
|
||
private static void fireOnStarted( | ||
final Cloud cloud, | ||
final Label label, | ||
final Collection<NodeProvisioner.PlannedNode> plannedNodes) { | ||
for (CloudProvisioningListener cl : CloudProvisioningListener.all()) { | ||
try { | ||
cl.onStarted(cloud, label, plannedNodes); | ||
} catch (Error e) { | ||
throw e; | ||
} catch (Throwable e) { | ||
LOGGER.log( | ||
Level.SEVERE, | ||
"Unexpected uncaught exception encountered while " | ||
+ "processing onStarted() listener call in " | ||
+ cl | ||
+ " for label " | ||
+ label.toString(), | ||
e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...com/google/jenkins/plugins/computeengine/ComputeEngineCloud/help-noDelayProvisioning.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!-- | ||
Copyright 2020 Elastic | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
compliance with the License. You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License | ||
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
implied. See the License for the specific language governing permissions and limitations under the | ||
License. | ||
--> | ||
By default Jenkins estimates load to avoid over-provisioning of cloud nodes. | ||
With this option enabled, a new node is created on GCP as soon as NodeProvisioner detects need for more agents. | ||
In worst case scenarios, this will results in some extra nodes provisioned on GCP, which will be shortly terminated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters