Skip to content

Commit

Permalink
[JENKINS-69751] Add sorting for blocked projects (#69)
Browse files Browse the repository at this point in the history
* Add sorting for blocked projects

* [JENKINS-69751]: Wrote a test for AdvancedQueueSorter.sortBlockedItems(List<Queue.BlockedItem> blockedItems)

* Format sources before merge with master

* Format source files before merge

* Remove unused imports

* Do not lose icon impl

* Do not lose spotbugs exclusions

---------

Co-authored-by: Mark Waite <[email protected]>
Co-authored-by: Simon Sohrt <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2023
1 parent 38b7de9 commit 4459da7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ public int compare(BuildableItem o1, BuildableItem o2) {
LOGGER.info("Initialized the QueueSorter with " + items.size() + " Buildable Items");
}

@Override
public void sortBuildableItems(List<BuildableItem> items) {

Collections.sort(items, (BuildableItem o1, BuildableItem o2) -> {
public void sortNotWaitingItems(List<? extends Queue.NotWaitingItem> items) {
Collections.sort(items, (Comparator<Queue.NotWaitingItem>) (o1, o2) -> {
ItemInfo item1 = QueueItemCache.get().getItem(o1.getId());
ItemInfo item2 = QueueItemCache.get().getItem(o2.getId());
if (item1 == null || item2 == null) {
Expand All @@ -87,22 +85,22 @@ public void sortBuildableItems(List<BuildableItem> items) {
float maxWeight = QueueItemCache.get()
.getItem(items.get(items.size() - 1).getId())
.getWeight();
LOGGER.log(Level.FINE, "Sorted {0} Buildable Items with Min Weight {1} and Max Weight {2}", new Object[] {
items.size(), minWeight, maxWeight
LOGGER.log(Level.FINE, "Sorted {0} {1}s with Min Weight {2} and Max Weight {3}", new Object[] {
items.size(), items.get(0).getClass().getName(), minWeight, maxWeight
});
}
//
if (items.size() > 0 && LOGGER.isLoggable(Level.FINER)) {
StringBuilder queueStr = new StringBuilder("Queue:\n"
StringBuilder queueStr = new StringBuilder(items.get(0).getClass().getName());
queueStr.append(" Queue:\n"
+ "+----------------------------------------------------------------------+\n"
+ "| Item Id | Job Name | Priority | Weight |\n"
+ "+----------------------------------------------------------------------+\n");
for (BuildableItem item : items) {
for (Queue.NotWaitingItem item : items) {
ItemInfo itemInfo = QueueItemCache.get().getItem(item.getId());
String jobName = itemInfo.getJobName();
if (jobName.length() > 21) {
jobName =
jobName.substring(0, 9) + "..." + jobName.substring(jobName.length() - 9, jobName.length());
jobName = jobName.substring(0, 9) + "..." + jobName.substring(jobName.length() - 9);

Check warning on line 103 in src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 88-103 are not covered by tests
}
queueStr.append(String.format(
"| %10d | %20s | %8d | %20.5f |%n",
Expand All @@ -113,6 +111,16 @@ public void sortBuildableItems(List<BuildableItem> items) {
}
}

@Override
public void sortBuildableItems(List<BuildableItem> items) {
sortNotWaitingItems(items);
}

@Override
public void sortBlockedItems(List<Queue.BlockedItem> blockedItems) {
sortNotWaitingItems(blockedItems);
}

public void onNewItem(@NonNull Item item) {
final SorterStrategy prioritySorterStrategy =
PrioritySorterConfiguration.get().getStrategy();
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/jenkins/advancedqueue/test/SortBlockedItemsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package jenkins.advancedqueue.test;

import hudson.model.FreeStyleProject;
import hudson.model.Queue;
import hudson.tasks.BuildTrigger;
import jenkins.advancedqueue.testutil.ExpectedItem;
import jenkins.advancedqueue.testutil.TestRunListener;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

public class SortBlockedItemsTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
@LocalData
public void blockedItemsAreSortedByPriority() throws Exception {
// Priority value is configured in jenkins.advancedqueue.PrioritySorterConfiguration. Value is 2.
FreeStyleProject upstreamProject = j.createFreeStyleProject("upstreamProject");
// Priority value is configured in jenkins.advancedqueue.PrioritySorterConfiguration. Value is 1.
FreeStyleProject downstreamProject = j.createFreeStyleProject("downstreamProject");
upstreamProject.setBlockBuildWhenDownstreamBuilding(true);
downstreamProject.setBlockBuildWhenUpstreamBuilding(true);
upstreamProject.getPublishersList().add(new BuildTrigger(downstreamProject.getName(), true));
// Expected build sequence: downstreamProject#1 -> upstreamProject#1 -> downstreamProject#2
TestRunListener.init(
new ExpectedItem(downstreamProject.getName(), 1),
new ExpectedItem(upstreamProject.getName(), 2),
new ExpectedItem(downstreamProject.getName(), 1));

j.jenkins.rebuildDependencyGraph();

// Locking makes sure that the queue sees both builds at the same time even though upstreamProject entered the
// queue first
Queue.withLock(() -> {
j.jenkins.getQueue().schedule2(upstreamProject, 0);
j.jenkins.getQueue().schedule2(downstreamProject, 0);
j.jenkins.getQueue().maintain();
});
j.waitUntilNoActivity();

TestRunListener.assertStartedItems();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='UTF-8'?>
<jenkins.advancedqueue.PriorityConfiguration plugin="[email protected]">
<jobGroups class="linked-list">
<jenkins.advancedqueue.JobGroup>
<id>0</id>
<priority>2</priority>
<view>All</view>
<runExclusive>false</runExclusive>
<useJobFilter>true</useJobFilter>
<jobPattern>upstreamProject</jobPattern>
<usePriorityStrategies>false</usePriorityStrategies>
<priorityStrategies>
</priorityStrategies>
</jenkins.advancedqueue.JobGroup>
<jenkins.advancedqueue.JobGroup>
<id>1</id>
<priority>1</priority>
<view>All</view>
<runExclusive>false</runExclusive>
<useJobFilter>true</useJobFilter>
<jobPattern>downstreamProject</jobPattern>
<usePriorityStrategies>false</usePriorityStrategies>
<priorityStrategies>
</priorityStrategies>
</jenkins.advancedqueue.JobGroup>
</jobGroups>
</jenkins.advancedqueue.PriorityConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<jenkins.advancedqueue.PrioritySorterConfiguration>
<allowPriorityOnJobs>true</allowPriorityOnJobs>
<strategy class="jenkins.advancedqueue.sorter.strategy.AbsoluteStrategy">>
<numberOfPriorities>2</numberOfPriorities>
<defaultPriority>1</defaultPriority>
</strategy>
</jenkins.advancedqueue.PrioritySorterConfiguration>

0 comments on commit 4459da7

Please sign in to comment.