From 84d80f57f2a31af44d733477efeb89db3f896021 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 18 May 2015 13:59:31 +0300 Subject: [PATCH] [JENKINS-28446] - Introduce new API method in Queue to optimize the performance of UnlabeledLoadStatistics (cherry picked from commit 7697bdb4f57d974546ec4c7e95e87fc8f2ad7f1a) --- core/src/main/java/hudson/model/Queue.java | 28 ++++++++++++++++++- .../model/UnlabeledLoadStatistics.java | 11 +------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/hudson/model/Queue.java b/core/src/main/java/hudson/model/Queue.java index ef03628060..a15b0af34b 100644 --- a/core/src/main/java/hudson/model/Queue.java +++ b/core/src/main/java/hudson/model/Queue.java @@ -938,7 +938,9 @@ public class Queue extends ResourceController implements Saveable { /** * How many {@link BuildableItem}s are assigned for the given label? - * @param l Label to be checked. If null, any label will be accepted + * @param l Label to be checked. If null, any label will be accepted. + * If you want to count {@link BuildableItem}s without assigned labels, + * use {@link #strictCountBuildableItemsFor(hudson.model.Label)}. * @return Number of {@link BuildableItem}s for the specified label. */ public @Nonnegative int countBuildableItemsFor(@CheckForNull Label l) { @@ -954,6 +956,30 @@ public class Queue extends ResourceController implements Saveable { r++; return r; } + + /** + * How many {@link BuildableItem}s are assigned for the given label? + *

+ * The implementation is quite similar to {@link #countBuildableItemsFor(hudson.model.Label)}, + * but it has another behavior for null parameters. + * @param l Label to be checked. If null, only jobs without assigned labels + * will be taken into the account. + * @return Number of {@link BuildableItem}s for the specified label. + * @since TODO + */ + public @Nonnegative int strictCountBuildableItemsFor(@CheckForNull Label l) { + Snapshot _snapshot = this.snapshot; + int r = 0; + for (BuildableItem bi : _snapshot.buildables) + for (SubTask st : bi.task.getSubTasks()) + if (bi.getAssignedLabelFor(st) == l) + r++; + for (BuildableItem bi : _snapshot.pendings) + for (SubTask st : bi.task.getSubTasks()) + if (bi.getAssignedLabelFor(st) == l) + r++; + return r; + } /** * Counts all the {@link BuildableItem}s currently in the queue. diff --git a/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java b/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java index 4f2a490a73..008e5d2c74 100644 --- a/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java +++ b/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java @@ -84,16 +84,7 @@ public class UnlabeledLoadStatistics extends LoadStatistics { if (j == null) { // Consider queue as empty when Jenkins is inactive return 0; } - - int result = 0; - final List buildableItems = j.getQueue().getBuildableItems(); - for (Queue.BuildableItem item : buildableItems) { - for (SubTask st : Tasks.getSubTasksOf(item.task)) { - if (item.getAssignedLabelFor(st) == null) - result++; - } - } - return result; + return j.getQueue().strictCountBuildableItemsFor(null); } @Override -- GitLab