Skip to content

Commit

Permalink
[JENKINS-44014] - Prevent NullPointerException in AdvancedQueueSorter…
Browse files Browse the repository at this point in the history
…#onLeft()
  • Loading branch information
oleg-nenashev committed Jun 17, 2017
1 parent 932179e commit 35c7a22
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/main/java/jenkins/advancedqueue/ItemTransitionLogger.java
Expand Up @@ -25,6 +25,7 @@

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import jenkins.advancedqueue.sorter.ItemInfo;
import jenkins.model.Jenkins;
Expand All @@ -37,27 +38,27 @@ public class ItemTransitionLogger {

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

static public void logNewItem(ItemInfo info) {
static public void logNewItem(@Nonnull ItemInfo info) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("New Item: " + info.toString() + "\n" + info.getDescisionLog());
} else {
LOGGER.fine("New Item: " + info.toString());
}
}

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

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

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

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

Expand Down
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import jenkins.advancedqueue.PriorityConfiguration;
import jenkins.advancedqueue.PrioritySorterConfiguration;
Expand Down Expand Up @@ -130,7 +131,7 @@ private float getCalculatedWeight(BuildableItem item) {
}
}

public void onNewItem(Item item) {
public void onNewItem(@Nonnull Item item) {
final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
ItemInfo itemInfo = new ItemInfo(item);
PriorityConfiguration.get().getPriority(item, itemInfo);
Expand All @@ -139,14 +140,20 @@ public void onNewItem(Item item) {
logNewItem(itemInfo);
}

public void onLeft(LeftItem li) {
final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
public void onLeft(@Nonnull LeftItem li) {
ItemInfo itemInfo = QueueItemCache.get().removeItem(li.id);
Float weight = itemInfo.getWeight();
if (itemInfo == null) {
LOGGER.log(Level.WARNING, "Received the onLeft() notification for the item from outside the QueueItemCache: {0}. " +
"Cannot process this item, Priority Sorter Strategy will not be invoked", li);
return;
}

final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
if (li.isCancelled()) {
prioritySorterStrategy.onCanceledItem(li);
logCanceledItem(itemInfo);
} else {
Float weight = itemInfo.getWeight();
prioritySorterStrategy.onStartedItem(li, weight);
logStartedItem(itemInfo);
}
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;

/**
* Keeps track of the Queue.Items seen by the Sorter. Uses a WeakHash to store the entries that have
Expand Down Expand Up @@ -91,6 +92,7 @@ synchronized public ItemInfo addItem(ItemInfo itemInfo) {
return itemInfo;
}

@CheckForNull
synchronized public ItemInfo removeItem(int itemId) {
return item2info.remove(itemId);
}
Expand Down
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

import jenkins.model.Jenkins;

Expand All @@ -54,21 +55,21 @@ public SorterStrategyDescriptor getDescriptor() {
* the weight before returning
* @return the {@link SorterStrategyCallback} provided to the call must be returned
*/
public abstract SorterStrategyCallback onNewItem(Queue.Item item, SorterStrategyCallback weightCallback);
public abstract SorterStrategyCallback onNewItem(@Nonnull Queue.Item item, SorterStrategyCallback weightCallback);

/**
* Called when a {@link hudson.model.Item} leaves the queue and it is started.
*
* @param item the {@link hudson.model.LeftItem}
* @param weight the weight assigned when the item entered the queue
*/
public void onStartedItem(LeftItem item, float weight) {
public void onStartedItem(@Nonnull LeftItem item, float weight) {
}

/**
* Called when a {@link hudson.model.Item} leaves the queue and it is canceled.
*/
public void onCanceledItem(LeftItem item) {
public void onCanceledItem(@Nonnull LeftItem item) {
};

/**
Expand Down

0 comments on commit 35c7a22

Please sign in to comment.