Skip to content

Commit 6e2067e

Browse files
authored
Optimize MappingWorksheet to avoid calling Task.getEstimatedDuration (jenkinsci#11293)
1 parent 30f301b commit 6e2067e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

core/src/main/java/hudson/model/queue/MappingWorksheet.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import hudson.model.Queue.Task;
4141
import hudson.model.labels.LabelAssignmentAction;
4242
import hudson.security.ACL;
43+
import hudson.slaves.DumbSlave;
44+
import java.time.Duration;
4345
import java.util.AbstractList;
4446
import java.util.ArrayList;
4547
import java.util.Collection;
@@ -330,15 +332,27 @@ public MappingWorksheet(BuildableItem item, List<? extends ExecutorSlot> offers,
330332
this.item = item;
331333

332334
// group executors by their computers
335+
boolean someNonCloud = false;
333336
Map<Computer, List<ExecutorSlot>> j = new HashMap<>();
334337
for (ExecutorSlot o : offers) {
335338
Computer c = o.getExecutor().getOwner();
339+
if (!someNonCloud) {
340+
Node n = c.getNode();
341+
if (n == null || n instanceof DumbSlave) {
342+
someNonCloud = true;
343+
}
344+
}
336345
List<ExecutorSlot> l = j.computeIfAbsent(c, k -> new ArrayList<>());
337346
l.add(o);
338347
}
339348

340-
{ // take load prediction into account and reduce the available executor pool size accordingly
349+
if (j.isEmpty()) {
350+
LOGGER.fine(() -> "nothing to consider for " + item);
351+
} else if (!someNonCloud) {
352+
LOGGER.fine(() -> "only cloud agents to consider for " + item + ": " + offers);
353+
} else { // take load prediction into account and reduce the available executor pool size accordingly
341354
long duration = item.task.getEstimatedDuration();
355+
LOGGER.fine(() -> "for " + item + " taking " + Duration.ofMillis(duration) + " considering " + offers);
342356
if (duration > 0) {
343357
long now = System.currentTimeMillis();
344358
for (Map.Entry<Computer, List<ExecutorSlot>> e : j.entrySet()) {

test/src/test/java/hudson/model/queue/LoadPredictorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import hudson.model.Queue.JobOffer;
3838
import hudson.model.Queue.Task;
3939
import hudson.model.Queue.WaitingItem;
40+
import hudson.slaves.DumbSlave;
4041
import java.lang.reflect.Field;
4142
import java.util.ArrayList;
4243
import java.util.Collection;
@@ -106,7 +107,7 @@ private JobOffer createMockOffer(Executor e) {
106107
}
107108

108109
private Computer createMockComputer(int nExecutors) throws Exception {
109-
Node n = mock(Node.class);
110+
Node n = mock(DumbSlave.class);
110111
Computer c = mock(Computer.class);
111112
when(c.getNode()).thenReturn(n);
112113

0 commit comments

Comments
 (0)