下面列出了怎么用hudson.model.LoadStatistics的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public NodeProvisioner.StrategyDecision apply(final NodeProvisioner.StrategyState strategyState) {
final Label label = strategyState.getLabel();
final LoadStatistics.LoadStatisticsSnapshot snapshot = strategyState.getSnapshot();
final 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() - availableCapacity;
LOGGER.log(Level.INFO, "Available capacity={0}, currentDemand={1}",
new Object[]{availableCapacity, currentDemand});
for (final Cloud cloud : getClouds()) {
if (currentDemand < 1) break;
if (!(cloud instanceof EC2FleetCloud)) continue;
if (!cloud.canProvision(label)) continue;
final EC2FleetCloud ec2 = (EC2FleetCloud) cloud;
if (!ec2.isNoDelayProvision()) continue;
final Collection<NodeProvisioner.PlannedNode> plannedNodes = cloud.provision(label, currentDemand);
currentDemand -= plannedNodes.size();
LOGGER.log(Level.FINE, "Planned {0} new nodes", plannedNodes.size());
strategyState.recordPendingLaunches(plannedNodes);
LOGGER.log(Level.FINE, "After provisioning, available capacity={0}, currentDemand={1}",
new Object[]{availableCapacity, currentDemand});
}
if (currentDemand < 1) {
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;
}
}
@Override public void before() throws Throwable {
clockOrig = LoadStatistics.CLOCK;
initialDelayOrig = NodeProvisionerInvoker.INITIALDELAY;
recurrencePeriodOrig = NodeProvisionerInvoker.RECURRENCEPERIOD;
if (clock != -1) {
LoadStatistics.CLOCK = clock;
}
if (initialDelay != -1) {
NodeProvisionerInvoker.INITIALDELAY = initialDelay;
}
if (recurrencePeriod != -1) {
NodeProvisionerInvoker.RECURRENCEPERIOD = recurrencePeriod;
}
super.before();
}
@Override public void after() throws Exception {
super.after();
// TODO should we really restore prior values? That makes tests using this rule not safe to run concurrently. Should rather have Configuration be per-Jenkins.
LoadStatistics.CLOCK = clockOrig;
NodeProvisionerInvoker.INITIALDELAY = initialDelayOrig;
NodeProvisionerInvoker.RECURRENCEPERIOD = recurrencePeriodOrig;
}
private StrategyDecision applyFoCloud(@Nonnull NodeProvisioner.StrategyState state, DockerCloud cloud) {
final Label label = state.getLabel();
if (!cloud.canProvision(label)) {
return CONSULT_REMAINING_STRATEGIES;
}
LoadStatistics.LoadStatisticsSnapshot snapshot = state.getSnapshot();
LOGGER.log(FINEST, "Available executors={0}, connecting={1}, planned={2}",
new Object[]{snapshot.getAvailableExecutors(), snapshot.getConnectingExecutors(), state.getPlannedCapacitySnapshot()});
int availableCapacity =
snapshot.getAvailableExecutors()
+ snapshot.getConnectingExecutors()
+ state.getPlannedCapacitySnapshot();
int currentDemand = snapshot.getQueueLength();
LOGGER.log(FINE, "Available capacity={0}, currentDemand={1}",
new Object[]{availableCapacity, currentDemand});
if (availableCapacity < currentDemand) {
Collection<NodeProvisioner.PlannedNode> plannedNodes = cloud.provision(label, currentDemand - availableCapacity);
LOGGER.log(FINE, "Planned {0} new nodes", plannedNodes.size());
state.recordPendingLaunches(plannedNodes);
availableCapacity += plannedNodes.size();
LOGGER.log(FINE, "After provisioning, available capacity={0}, currentDemand={1}",
new Object[]{availableCapacity, currentDemand});
}
if (availableCapacity >= currentDemand) {
LOGGER.log(FINE, "Provisioning completed");
return PROVISIONING_COMPLETED;
}
LOGGER.log(FINE, "Provisioning not complete, consulting remaining strategies");
return CONSULT_REMAINING_STRATEGIES;
}
@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 KubernetesCloud)) continue;
if (!cloud.canProvision(label)) 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;
}
}
/**
* Takes a provisioning decision for a single label. Determines how many ECS tasks to start based solely on
* queue length and how many agents are in the process of connecting.
*/
@Nonnull
@Override
public NodeProvisioner.StrategyDecision apply(@Nonnull NodeProvisioner.StrategyState state) {
LOGGER.log(Level.FINE, "Received {0}", new Object[]{state});
LoadStatistics.LoadStatisticsSnapshot snap = state.getSnapshot();
Label label = state.getLabel();
int excessWorkload = snap.getQueueLength() - snap.getAvailableExecutors() - snap.getConnectingExecutors();
CLOUD:
for (Cloud c : Jenkins.get().clouds) {
if (excessWorkload <= 0) {
break; // enough agents allocated
}
// Make sure this cloud actually can provision for this label.
if (!c.canProvision(label)) {
continue;
}
for (CloudProvisioningListener cl : CloudProvisioningListener.all()) {
CauseOfBlockage causeOfBlockage = cl.canProvision(c, label, excessWorkload);
if (causeOfBlockage != null) {
continue CLOUD;
}
}
Collection<NodeProvisioner.PlannedNode> additionalCapacities = c.provision(label, excessWorkload);
// compat with what the default NodeProvisioner.Strategy does
fireOnStarted(c, label, additionalCapacities);
for (NodeProvisioner.PlannedNode ac : additionalCapacities) {
excessWorkload -= ac.numExecutors;
LOGGER.log(Level.FINE, "Started provisioning {0} from {1} with {2,number,integer} "
+ "executors. Remaining excess workload: {3,number,#.###}",
new Object[]{ac.displayName, c.name, ac.numExecutors, excessWorkload});
}
state.recordPendingLaunches(additionalCapacities);
}
// we took action, only pass on to other strategies if our action was insufficient
return excessWorkload > 0 ? NodeProvisioner.StrategyDecision.CONSULT_REMAINING_STRATEGIES : NodeProvisioner.StrategyDecision.PROVISIONING_COMPLETED;
}