Skip to content
Permalink
Browse files

Merge pull request #50 from jglick/Queue-JENKINS-44747

[JENKINS-44747] Limit the amount of time categoriesForPipeline can run
  • Loading branch information
abayer committed Jun 7, 2017
2 parents 7c61f61 + 68605b0 commit d105bb791364f3d8bfe42dcad448b14f9b0ed8b7
@@ -176,7 +176,6 @@ THE SOFTWARE.
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -236,28 +236,33 @@ public boolean isThrottleMatrixConfigurations() {
}

/**
* Get the list of categories for a given run ID (from {@link Run#getExternalizableId()}) and flow node ID (from
* {@link FlowNode#getId()}, if that run/flow node combination is recorded for one or more categories.
* Get the list of categories for a given run by flow node, if that run/flow node combination is recorded for one or more categories.
*
* @param runId The run ID
* @param flowNodeId The flow node ID
* @return A list of category names. May be empty.
* @param run the run
* @return a map (possibly empty) from {@link FlowNode#getId} to a list of category names (possibly empty)
*/
@Nonnull
static List<String> getCategoriesForRunAndFlowNode(@Nonnull String runId, @Nonnull String flowNodeId) {
List<String> categories = new ArrayList<>();
static Map<String, List<String>> getCategoriesForRunByFlowNode(@Nonnull Run<?, ?> run) {
Map<String, List<String>> categoriesByNode = new HashMap<>();

final DescriptorImpl descriptor = fetchDescriptor();

for (ThrottleCategory cat : descriptor.getCategories()) {
Map<String,List<String>> runs = descriptor.getThrottledPipelinesForCategory(cat.getCategoryName());

if (!runs.isEmpty() && runs.containsKey(runId) && runs.get(runId).contains(flowNodeId)) {
categories.add(cat.getCategoryName());
List<String> nodeIds = runs.get(run.getExternalizableId());
if (nodeIds != null) {
for (String nodeId : nodeIds) {
List<String> categories = categoriesByNode.get(nodeId);
if (categories == null) {
categories = new ArrayList<>();
categoriesByNode.put(nodeId, categories);
}
categories.add(cat.getCategoryName());
}
}
}

return categories;
return categoriesByNode;
}

/**
@@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
@@ -42,6 +43,7 @@
import org.jenkinsci.plugins.workflow.graph.StepNode;
import org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.support.concurrent.Timeout;
import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution.PlaceholderTask;

@Extension
@@ -404,20 +406,28 @@ private boolean isAnotherBuildWithSameParametersRunningOnNode(Node node, Queue.I
return paramsList;
}

@Nonnull
private List<String> categoriesForPipeline(Task task) {
if (task instanceof PlaceholderTask) {
PlaceholderTask placeholderTask = (PlaceholderTask)task;
try {
FlowNode firstThrottle = firstThrottleStartNode(placeholderTask.getNode());
Run<?,?> r = placeholderTask.run();
if (firstThrottle != null && r != null) {
return ThrottleJobProperty.getCategoriesForRunAndFlowNode(r.getExternalizableId(),
firstThrottle.getId());
Run<?, ?> r = placeholderTask.run();
if (r != null) {
Map<String, List<String>> categoriesByFlowNode = ThrottleJobProperty.getCategoriesForRunByFlowNode(r);
if (!categoriesByFlowNode.isEmpty()) {
try (Timeout t = Timeout.limit(100, TimeUnit.MILLISECONDS)) {
FlowNode firstThrottle = firstThrottleStartNode(placeholderTask.getNode());
if (firstThrottle != null) {
List<String> categories = categoriesByFlowNode.get(firstThrottle.getId());
if (categories != null) {
return categories;
}
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Error getting categories for pipeline {0}: {1}",
new Object[] {task.getDisplayName(), e});
return new ArrayList<>();
}
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Error getting categories for pipeline {0}: {1}",
new Object[] {task.getDisplayName(), e});
return new ArrayList<>();
}
}
return new ArrayList<>();

0 comments on commit d105bb7

Please sign in to comment.
You can’t perform that action at this time.