Skip to content

Commit e2ec8ee

Browse files
committed
[JENKINS-76200] Fix label matching in itemsInQueueForThisSlave
Changed from checking only explicit node assignment (selfLabel) to using Label.contains() which properly checks if a node can execute jobs based on label matching. This fixes the issue where stopped instances would only start for jobs explicitly tied to the node name, not for jobs that match the node's labels. Changes: - Use assignedLabel.contains(selfNode) instead of assignedLabel == selfLabel - Handle null assignedLabel (jobs that can run on any node) - Added comment explaining the label matching logic Now stopped instances will start for: - Jobs with no label requirement (assignedLabel == null) - Jobs whose labels match this node's capabilities (assignedLabel.contains(selfNode)) Before this fix, stopped instances only started for jobs explicitly tied to the specific node name.
1 parent d3a7d3c commit e2ec8ee

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ private void attemptReconnectIfOffline(EC2Computer computer) {
294294
StartInstancesRequest request = StartInstancesRequest.builder()
295295
.instanceIds(computer.getInstanceId())
296296
.build();
297-
LOGGER.info("[JENKINS-76200] Calling AWS startInstances() for " + computer.getInstanceId());
297+
LOGGER.info(
298+
"[JENKINS-76200] Calling AWS startInstances() for " + computer.getInstanceId());
298299
ec2.startInstances(request);
299300
LOGGER.info("[JENKINS-76200] Successfully called startInstances() for "
300301
+ computer.getInstanceId() + " - instance should be starting now");
@@ -309,8 +310,8 @@ private void attemptReconnectIfOffline(EC2Computer computer) {
309310
+ " - cloud not found for node " + computer.getName());
310311
}
311312
} else {
312-
LOGGER.info("[JENKINS-76200] No jobs waiting for stopped instance "
313-
+ computer.getInstanceId() + " - leaving it stopped");
313+
LOGGER.info("[JENKINS-76200] No jobs waiting for stopped instance " + computer.getInstanceId()
314+
+ " - leaving it stopped");
314315
}
315316
} else {
316317
LOGGER.info("[JENKINS-76200] Instance " + computer.getInstanceId() + " is " + state
@@ -341,11 +342,12 @@ private void attemptReconnectIfOffline(EC2Computer computer) {
341342
}
342343

343344
/*
344-
* Checks if there are any items in the queue that are waiting for this node explicitly.
345-
* This prevents a node from being taken offline while there are Ivy/Maven Modules waiting to build.
345+
* Checks if there are any items in the queue that can run on this node.
346+
* This prevents a node from being taken offline while there are jobs waiting that could use it.
346347
* Need to check entire queue as some modules may be blocked by upstream dependencies.
347348
* Accessing the queue in this way can block other threads, so only perform this check just prior
348349
* to timing out the slave.
350+
* JENKINS-76200: Check label matching, not just explicit node assignment.
349351
*/
350352
private boolean itemsInQueueForThisSlave(EC2Computer c) {
351353
final EC2AbstractSlave selfNode = c.getNode();
@@ -356,16 +358,19 @@ private boolean itemsInQueueForThisSlave(EC2Computer c) {
356358
if (selfNode == null) {
357359
return false;
358360
}
359-
final Label selfLabel = selfNode.getSelfLabel();
360361
Queue.Item[] items = Jenkins.get().getQueue().getItems();
361362
for (Queue.Item item : items) {
362363
final Label assignedLabel = item.getAssignedLabel();
363-
if (assignedLabel == selfLabel) {
364-
LOGGER.fine("Preventing idle timeout of " + c.getName()
365-
+ " as there is at least one item in the queue explicitly waiting for this slave");
364+
// JENKINS-76200: Check if this node can execute the job based on label matching
365+
// Jobs with no label requirement (null) can run on any node
366+
// Jobs with labels can run on nodes that match those labels
367+
if (assignedLabel == null || assignedLabel.contains(selfNode)) {
368+
LOGGER.fine("[JENKINS-76200] Found queued job that can run on " + c.getName()
369+
+ " (job label: " + assignedLabel + ")");
366370
return true;
367371
}
368372
}
373+
LOGGER.fine("[JENKINS-76200] No queued jobs found that can run on " + c.getName());
369374
return false;
370375
}
371376

0 commit comments

Comments
 (0)