Navigation Menu

Skip to content

Commit

Permalink
[JENKINS-21119] Extend loggning to show assigned priority
Browse files Browse the repository at this point in the history
 * Refactored to include more information in the ItemCache.
 * Refactored to use specific transition methods rather than setter.
 * Added ItemTransitionLogger to log events in the Queue.
  • Loading branch information
emsa23 committed Dec 21, 2013
1 parent 0eb964f commit aad90ea
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 33 deletions.
58 changes: 58 additions & 0 deletions src/main/java/jenkins/advancedqueue/ItemTransitionLogger.java
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright (c) 2013, Magnus Sandberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.advancedqueue;

import java.util.logging.Logger;

import jenkins.advancedqueue.sorter.ItemInfo;

/**
* @author Magnus Sandberg
* @since 2.4
*/
public class ItemTransitionLogger {

private final static Logger LOGGER = Logger.getLogger("PrioritySorter.Queue.Items");

static public void logNewItem(ItemInfo info) {
LOGGER.fine("New Item: " + info.toString());
}

static public void logBlockedItem(ItemInfo info) {
LOGGER.fine("Blocking: " + info.toString());
}

static public void logBuilableItem(ItemInfo info) {
LOGGER.fine("Buildable: " + info.toString());
}

static public void logStartedItem(ItemInfo info) {
LOGGER.fine("Starting: " + info.toString());
}

static public void logCanceledItem(ItemInfo info) {
LOGGER.fine("Canceling: " + info.toString());
}

}
45 changes: 26 additions & 19 deletions src/main/java/jenkins/advancedqueue/PriorityConfiguration.java
Expand Up @@ -128,7 +128,7 @@ public ListBoxModel getListViewItems() {
}
return items;
}

public JobGroup getJobGroup(int id) {
return id2jobGroup.get(id);
}
Expand Down Expand Up @@ -186,11 +186,14 @@ public PriorityConfigurationCallback getPriority(Queue.Item item, PriorityConfig
if (job instanceof MatrixConfiguration) {
MatrixProject matrixProject = ((MatrixConfiguration) job).getParent();
ItemInfo itemInfo = QueueItemCache.get().getItem(matrixProject.getName());
// Can be null (for example) at startup when the MatrixBuild got lost (was running at restart)
if(itemInfo != null) {
return priorityCallback.setPrioritySelection(itemInfo.getPriority(), itemInfo.getJobGroupId());
// Can be null (for example) at startup when the MatrixBuild got lost (was running at
// restart)
if (itemInfo != null) {
return priorityCallback.setPrioritySelection(itemInfo.getPriority(), itemInfo.getJobGroupId(),
itemInfo.getPriorityStrategy());
}
return priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
return priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy()
.getDefaultPriority());
}

if (PrioritySorterConfiguration.get().getAllowPriorityOnJobs()) {
Expand All @@ -212,18 +215,17 @@ public PriorityConfigurationCallback getPriority(Queue.Item item, PriorityConfig
TopLevelItem jobItem = view.getItem(job.getName());
// Now check if the item is actually in the view
if (view.contains(jobItem)) {
int priority = PriorityCalculationsUtil.getUseDefaultPriorityPriority();
// If filtering is not used use the priority
// If filtering is used but the pattern is empty regard
// it as a match all
if (!jobGroup.isUseJobFilter() || jobGroup.getJobPattern().trim().isEmpty()) {
priority = getPriorityForJobGroup(jobGroup, item);
return getPriorityForJobGroup(priorityCallback, jobGroup, item);
} else {
// So filtering is on - use the priority if there's
// a match
try {
if (job.getName().matches(jobGroup.getJobPattern())) {
priority = getPriorityForJobGroup(jobGroup, item);
return getPriorityForJobGroup(priorityCallback, jobGroup, item);
} else {
continue nextView;
}
Expand All @@ -233,33 +235,38 @@ public PriorityConfigurationCallback getPriority(Queue.Item item, PriorityConfig
continue nextView;
}
}
if (priority == PriorityCalculationsUtil.getUseDefaultPriorityPriority()) {
priority = PrioritySorterConfiguration.get().getStrategy().getDefaultPriority();
}
return priorityCallback.setPrioritySelection(priority, jobGroup.getId());
}
}
}
}
//
return priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
return priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy()
.getDefaultPriority());
}

private int getPriorityForJobGroup(JobGroup jobGroup, Queue.Item item) {
private PriorityConfigurationCallback getPriorityForJobGroup(PriorityConfigurationCallback priorityCallback,
JobGroup jobGroup, Queue.Item item) {
int priority = jobGroup.getPriority();
PriorityStrategy reason = null;
if (jobGroup.isUsePriorityStrategies()) {
List<JobGroup.PriorityStrategyHolder> priorityStrategies = jobGroup.getPriorityStrategies();
for (JobGroup.PriorityStrategyHolder priorityStrategy : priorityStrategies) {
PriorityStrategy strategy = priorityStrategy.getPriorityStrategy();
if (strategy.isApplicable(item)) {
int priority = strategy.getPriority(item);
if (priority > 0
&& priority <= PrioritySorterConfiguration.get().getStrategy().getNumberOfPriorities()) {
return priority;
int foundPriority = strategy.getPriority(item);
if (foundPriority > 0
&& foundPriority <= PrioritySorterConfiguration.get().getStrategy().getNumberOfPriorities()) {
priority = foundPriority;
reason = strategy;
break;
}
}
}
}
return jobGroup.getPriority();
if (priority == PriorityCalculationsUtil.getUseDefaultPriorityPriority()) {
priority = PrioritySorterConfiguration.get().getStrategy().getDefaultPriority();
}
return priorityCallback.setPrioritySelection(priority, jobGroup.getId(), reason);
}

static public PriorityConfiguration get() {
Expand Down
@@ -1,9 +1,11 @@
package jenkins.advancedqueue;

import jenkins.advancedqueue.priority.PriorityStrategy;

public interface PriorityConfigurationCallback {

PriorityConfigurationCallback setPrioritySelection(int priority);

PriorityConfigurationCallback setPrioritySelection(int priority, int jobGroupId);
PriorityConfigurationCallback setPrioritySelection(int priority, int jobGroupId, PriorityStrategy reason);

}
Expand Up @@ -37,6 +37,7 @@

import jenkins.advancedqueue.PriorityConfiguration;
import jenkins.advancedqueue.PrioritySorterConfiguration;
import static jenkins.advancedqueue.ItemTransitionLogger.*;

/**
* @author Magnus Sandberg
Expand All @@ -61,7 +62,7 @@ public int compare(BuildableItem o1, BuildableItem o2) {
for (BuildableItem item : items) {
advancedQueueSorter.onNewItem(item);
// Listener called before we get here so make sure we mark buildable
QueueItemCache.get().getItem(item.id).setItemStatus(ItemStatus.BUILDABLE);
QueueItemCache.get().getItem(item.id).setBuildable();
}
}

Expand Down Expand Up @@ -110,15 +111,19 @@ public void onNewItem(Item item) {
PriorityConfiguration.get().getPriority(item, itemInfo);
prioritySorterStrategy.onNewItem(item, itemInfo);
QueueItemCache.get().addItem(itemInfo);
logNewItem(itemInfo);
}

public void onLeft(LeftItem li) {
final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
Float weight = QueueItemCache.get().removeItem(li.id).getWeight();
ItemInfo itemInfo = QueueItemCache.get().removeItem(li.id);
Float weight = itemInfo.getWeight();
if (li.isCancelled()) {
prioritySorterStrategy.onCanceledItem(li);
logCanceledItem(itemInfo);
} else {
prioritySorterStrategy.onStartedItem(li, weight);
logStartedItem(itemInfo);
}
}

Expand Down
Expand Up @@ -52,13 +52,13 @@ public void onEnterBuildable(BuildableItem bi) {
ItemInfo item = QueueItemCache.get().getItem(bi.id);
// Null at startup
if(item != null) {
QueueItemCache.get().getItem(bi.id).setItemStatus(ItemStatus.BUILDABLE);
QueueItemCache.get().getItem(bi.id).setBuildable();
}
}

@Override
public void onEnterBlocked(BlockedItem bi) {
QueueItemCache.get().getItem(bi.id).setItemStatus(ItemStatus.BLOCKED);
QueueItemCache.get().getItem(bi.id).setBlocked();
}

}
38 changes: 29 additions & 9 deletions src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java
Expand Up @@ -23,12 +23,15 @@
*/
package jenkins.advancedqueue.sorter;

import static jenkins.advancedqueue.ItemTransitionLogger.logBlockedItem;
import static jenkins.advancedqueue.ItemTransitionLogger.logBuilableItem;
import hudson.model.Queue.Item;
import jenkins.advancedqueue.PriorityConfigurationCallback;
import jenkins.advancedqueue.priority.PriorityStrategy;

/**
* Used to store info about a Queue.Item and related information calculated by the Plugin
*
* Used to store info about a Queue.Item and related information calculated by the Plugin
*
* @author Magnus Sandberg
* @since 2.3
*/
Expand All @@ -38,12 +41,14 @@ public class ItemInfo implements PriorityConfigurationCallback, SorterStrategyCa

private int jobGroupId;

private PriorityStrategy priorityStrategy;

private String jobName;

private float weight;

private int priority;

private ItemStatus itemStatus;

ItemInfo(Item item) {
Expand All @@ -52,14 +57,15 @@ public class ItemInfo implements PriorityConfigurationCallback, SorterStrategyCa
this.itemStatus = ItemStatus.WAITING;
}

public PriorityConfigurationCallback setPrioritySelection(int priority, int jobGroupId) {
public PriorityConfigurationCallback setPrioritySelection(int priority, int jobGroupId, PriorityStrategy reason) {
this.priority = priority;
this.jobGroupId = jobGroupId;
this.priorityStrategy = reason;
return this;
}

public PriorityConfigurationCallback setPrioritySelection(int priority) {
setPrioritySelection(priority, -1);
setPrioritySelection(priority, -1, null);
return this;
}

Expand All @@ -68,6 +74,16 @@ public SorterStrategyCallback setWeightSelection(float weight) {
return this;
}

public void setBuildable() {
itemStatus = ItemStatus.BUILDABLE;
logBuilableItem(this);
}

public void setBlocked() {
itemStatus = ItemStatus.BLOCKED;
logBlockedItem(this);
}

public int getItemId() {
return itemId;
}
Expand All @@ -76,6 +92,10 @@ public int getJobGroupId() {
return jobGroupId;
}

public PriorityStrategy getPriorityStrategy() {
return priorityStrategy;
}

public String getJobName() {
return jobName;
}
Expand All @@ -92,10 +112,10 @@ public ItemStatus getItemStatus() {
return itemStatus;
}

public void setItemStatus(ItemStatus itemStatus) {
this.itemStatus = itemStatus;
@Override
public String toString() {
return String.format("Id: %s JobName: %s, jobGroupId: %s, priority: %s, weight: %s, status: %s", itemId,
jobName, jobGroupId, priority, weight, itemStatus);
}



}

0 comments on commit aad90ea

Please sign in to comment.