Queue.java 41.3 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly, Tom Huybrechts
 * 
 * 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.
 */
K
kohsuke 已提交
24 25
package hudson.model;

26
import hudson.BulkChange;
27
import hudson.Util;
K
kohsuke 已提交
28
import hudson.XmlFile;
K
kohsuke 已提交
29
import hudson.model.Node.Mode;
30 31
import hudson.triggers.SafeTimerTask;
import hudson.triggers.Trigger;
K
kohsuke 已提交
32
import hudson.util.OneShotEvent;
33
import hudson.util.TimeUnit2;
34
import hudson.util.XStream2;
K
kohsuke 已提交
35

36 37 38 39 40
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
41
import java.lang.ref.WeakReference;
K
kohsuke 已提交
42
import java.util.ArrayList;
43
import java.util.Arrays;
K
kohsuke 已提交
44 45 46 47 48 49
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
50
import java.util.NoSuchElementException;
K
kohsuke 已提交
51 52
import java.util.Set;
import java.util.TreeSet;
S
stephenconnolly 已提交
53
import java.util.Map.Entry;
54
import java.util.concurrent.atomic.AtomicInteger;
K
kohsuke 已提交
55 56 57
import java.util.logging.Level;
import java.util.logging.Logger;

K
kohsuke 已提交
58
import javax.management.timer.Timer;
59
import javax.servlet.ServletException;
K
kohsuke 已提交
60 61

import org.acegisecurity.AccessDeniedException;
62 63
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
K
kohsuke 已提交
64 65 66 67 68 69
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;

K
kohsuke 已提交
70 71
/**
 * Build queue.
72 73
 *
 * <p>
74 75
 * This class implements the core scheduling logic. {@link Task} represents the executable
 * task that are placed in the queue. While in the queue, it's wrapped into {@link Item}
76
 * so that we can keep track of additional data used for deciding what to exeucte when.
77 78
 *
 * <p>
79 80 81 82 83 84 85 86
 * Items in queue goes through several stages, as depicted below:
 * <pre>
 * (enter) --> waitingList --+--> blockedProjects
 *                           |        ^
 *                           |        |
 *                           |        v
 *                           +--> buildables ---> (executed)
 * </pre>
87 88
 *
 * <p>
89 90 91
 * In addition, at any stage, an item can be removed from the queue (for example, when the user
 * cancels a job in the queue.) See the corresponding field for their exact meanings.
 *
K
kohsuke 已提交
92 93
 * @author Kohsuke Kawaguchi
 */
94
@ExportedBean
95
public class Queue extends ResourceController implements Saveable {
K
kohsuke 已提交
96
    /**
97
     * Items that are waiting for its quiet period to pass.
98 99
     *
     * <p>
K
kohsuke 已提交
100 101 102
     * This consists of {@link Item}s that cannot be run yet
     * because its time has not yet come.
     */
103
    private final Set<WaitingItem> waitingList = new TreeSet<WaitingItem>();
K
kohsuke 已提交
104 105

    /**
106
     * {@link Task}s that can be built immediately
107 108 109
     * but blocked because another build is in progress,
     * required {@link Resource}s are not available, or otherwise blocked
     * by {@link Task#isBuildBlocked()}.
K
kohsuke 已提交
110
     */
111
    private final ItemList<BlockedItem> blockedProjects = new ItemList<BlockedItem>();
K
kohsuke 已提交
112 113

    /**
114
     * {@link Task}s that can be built immediately
K
kohsuke 已提交
115 116
     * that are waiting for available {@link Executor}.
     */
117
    private final ItemList<BuildableItem> buildables = new ItemList<BuildableItem>();
118

K
kohsuke 已提交
119 120 121
    /**
     * Data structure created for each idle {@link Executor}.
     * This is an offer from the queue to an executor.
122 123
     *
     * <p>
124
     * It eventually receives a {@link #item} to build.
K
kohsuke 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
     */
    private static class JobOffer {
        final Executor executor;

        /**
         * Used to wake up an executor, when it has an offered
         * {@link Project} to build.
         */
        final OneShotEvent event = new OneShotEvent();
        /**
         * The project that this {@link Executor} is going to build.
         * (Or null, in which case event is used to trigger a queue maintenance.)
         */
138
        BuildableItem item;
K
kohsuke 已提交
139 140 141 142 143

        public JobOffer(Executor executor) {
            this.executor = executor;
        }

144 145 146
        public void set(BuildableItem p) {
            assert this.item == null;
            this.item = p;
K
kohsuke 已提交
147 148 149 150
            event.signal();
        }

        public boolean isAvailable() {
151
            return item == null && !executor.getOwner().isOffline() && executor.getOwner().isAcceptingTasks();
K
kohsuke 已提交
152 153 154 155 156 157 158
        }

        public Node getNode() {
            return executor.getOwner().getNode();
        }

        public boolean isNotExclusive() {
159
            return getNode().getMode() == Mode.NORMAL;
K
kohsuke 已提交
160 161 162
        }
    }

S
stephenconnolly 已提交
163 164 165
    /**
     * The executors that are currently parked while waiting for a job to run.
     */
166
    private final Map<Executor, JobOffer> parked = new HashMap<Executor, JobOffer>();
K
kohsuke 已提交
167

168 169 170 171 172 173
    public Queue() {
        // if all the executors are busy doing something, then the queue won't be maintained in
        // timely fashion, so use another thread to make sure it happens.
        new MaintainTask(this);
    }

K
kohsuke 已提交
174 175 176 177 178
    /**
     * Loads the queue contents that was {@link #save() saved}.
     */
    public synchronized void load() {
        try {
K
TAB->WS  
kohsuke 已提交
179
            // first try the old format
K
kohsuke 已提交
180
            File queueFile = getQueueFile();
K
kohsuke 已提交
181 182
            if (queueFile.exists()) {
                BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(queueFile)));
K
TAB->WS  
kohsuke 已提交
183 184 185 186
                String line;
                while ((line = in.readLine()) != null) {
                    AbstractProject j = Hudson.getInstance().getItemByFullName(line, AbstractProject.class);
                    if (j != null)
M
mindless 已提交
187
                        j.scheduleBuild();
K
TAB->WS  
kohsuke 已提交
188 189 190 191 192 193 194
                }
                in.close();
                // discard the queue file now that we are done
                queueFile.delete();
            } else {
                queueFile = getXMLQueueFile();
                if (queueFile.exists()) {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
                    List list = (List) new XmlFile(XSTREAM, queueFile).read();
                    if (!list.isEmpty()) {
                    	if (list.get(0) instanceof Queue.Task) {
                    		// backward compatiblity
                    		for (Task task : (List<Task>) list) {
                    			add(task, 0);
                    		}
                    	} else if (list.get(0) instanceof Item) {
                    		int maxId = 0;
                    		for (Item item: (List<Item>) list) {
                    			maxId = Math.max(maxId, item.id);
                    			if (item instanceof WaitingItem) {
                    				waitingList.add((WaitingItem) item);
                    			} else if (item instanceof BlockedItem) {
                    				blockedProjects.put(item.task, (BlockedItem) item);
                    			} else if (item instanceof BuildableItem) {
                    				buildables.add((BuildableItem) item);
                    			} else {
                    				throw new IllegalStateException("Unknown item type! " + item);
                    			}
                    		}
                    		WaitingItem.COUNTER.set(maxId);
                    	}
K
TAB->WS  
kohsuke 已提交
218
                    }
219 220 221 222 223 224 225 226 227

                    // I just had an incident where all the executors are dead at AbstractProject._getRuns()
                    // because runs is null. Debugger revealed that this is caused by a MatrixConfiguration
                    // object that doesn't appear to be de-serialized properly.
                    // I don't know how this problem happened, but to diagnose this problem better
                    // when it happens again, save the old queue file for introspection.
                    File bk = new File(queueFile.getPath() + ".bak");
                    bk.delete();
                    queueFile.renameTo(bk);
K
TAB->WS  
kohsuke 已提交
228 229 230
                    queueFile.delete();
                }
            }
231 232
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to load the queue file " + getQueueFile(), e);
K
kohsuke 已提交
233 234 235 236 237 238 239
        }
    }

    /**
     * Persists the queue contents to the disk.
     */
    public synchronized void save() {
240 241
        if(BulkChange.contains(this))  return;
        
K
kohsuke 已提交
242
        // write out the tasks on the queue
243
    	ArrayList<Queue.Item> items = new ArrayList<Queue.Item>();
K
kohsuke 已提交
244
    	for (Item item: getItems()) {
245
    	    items.add(item);
K
kohsuke 已提交
246 247
    	}
    	
K
kohsuke 已提交
248
        try {
249
            new XmlFile(XSTREAM, getXMLQueueFile()).write(items);
250 251
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to write out the queue file " + getQueueFile(), e);
K
kohsuke 已提交
252 253 254
        }
    }

255 256 257 258 259 260 261 262 263 264
    /**
     * Wipes out all the items currently in the queue, as if all of them are cancelled at once.
     */
    public synchronized void clear() {
        waitingList.clear();
        blockedProjects.clear();
        buildables.clear();
        scheduleMaintenance();
    }

K
kohsuke 已提交
265
    private File getQueueFile() {
K
TAB->WS  
kohsuke 已提交
266 267
        return new File(Hudson.getInstance().getRootDir(), "queue.txt");
    }
K
kohsuke 已提交
268

269
    /*package*/ File getXMLQueueFile() {
K
TAB->WS  
kohsuke 已提交
270 271
        return new File(Hudson.getInstance().getRootDir(), "queue.xml");
    }
K
kohsuke 已提交
272 273 274

    /**
     * Schedule a new build for this project.
275
     *
276 277 278
     * @return true if the project is actually added to the queue.
     *         false if the queue contained it and therefore the add()
     *         was noop
K
kohsuke 已提交
279
     */
280 281
    public boolean add(AbstractProject p) {
        return add(p, p.getQuietPeriod());
282 283 284 285
    }

    /**
     * Schedules a new build with a custom quiet period.
286 287
     *
     * <p>
K
kohsuke 已提交
288 289
     * Left for backward compatibility with &lt;1.114.
     *
290 291
     * @since 1.105
     */
292 293
    public synchronized boolean add(AbstractProject p, int quietPeriod) {
        return add((Task) p, quietPeriod);
K
kohsuke 已提交
294
    }
295
    
K
kohsuke 已提交
296 297 298
    /**
     * Schedules an execution of a task.
     *
299 300 301
     * @param quietPeriod Number of seconds that the task will be placed in queue.
     *                    Useful when the same task is likely scheduled for multiple
     *                    times.
302 303 304
     * @return true if the project 'p' is actually added to the queue.
     *         false if the queue contained it and therefore the add()
     *         was noop, or just changed the due date of the task.
K
kohsuke 已提交
305 306
     * @since 1.114
     */
307 308 309 310 311
    private synchronized boolean add(Task p, int quietPeriod, List<Action> actions) {
    	boolean taskConsumed=false;
    	List<Item> items = getItems(p);
    	Calendar due = new GregorianCalendar();
    	due.add(Calendar.SECOND, quietPeriod);
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    	List<Item> duplicatesInQueue = new ArrayList<Item>();
    	for(Item item : items) {
    		boolean shouldScheduleItem = false;
    		for (Action action: item.getActions()) {
    			if (action instanceof QueueAction)
    				shouldScheduleItem |= ((QueueAction) action).shouldSchedule(actions);
    		}
    		for (Action action: actions) {
    			if (action instanceof QueueAction) {
    				shouldScheduleItem |= ((QueueAction) action).shouldSchedule(item.getActions());
    			}
    		}
    		if(!shouldScheduleItem) {
    			duplicatesInQueue.add(item);
    		}
    	}
    	if (duplicatesInQueue.size() == 0) {
    		LOGGER.fine(p.getFullDisplayName() + " added to queue");
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    		// put the item in the queue
    		waitingList.add(new WaitingItem(due,p,actions));
    		taskConsumed=true;
    	} else {
    		// the requested build is already queued, so will not be added
    		List<WaitingItem> waitingDuplicates = new ArrayList<WaitingItem>();
    		for(Item item : duplicatesInQueue) {
    			for(Action a : actions) {
    				if(a instanceof FoldableAction) {
    					((FoldableAction)a).foldIntoExisting(item.task, item.getActions());
    				}
    			}
    			if ((item instanceof WaitingItem))
    				waitingDuplicates.add((WaitingItem)item);
    		}
    		if(duplicatesInQueue.size() == 0) {
    			// all duplicates in the queue are already in the blocked or 
    			// buildable stage no need to requeue
    			return false;
    		}
    		// TODO: avoid calling scheduleMaintenance() if none of the waiting items 
    		// actually change
    		for(WaitingItem wi : waitingDuplicates) {
    			if(quietPeriod<=0) {
    				// the user really wants to build now, and they mean NOW.
    				// so let's pull in the timestamp if we can.
    				if (wi.timestamp.before(due))
    					continue;
    			} else {
    				// otherwise we do the normal quiet period implementation
    				if (wi.timestamp.after(due))
    					continue;
    				// quiet period timer reset. start the period over again
    			}
K
kohsuke 已提交
366

367 368 369 370 371 372 373 374 375
    			// waitingList is sorted, so when we change a timestamp we need to maintain order
    			waitingList.remove(wi);
    			wi.timestamp = due;
    			waitingList.add(wi);
    		}

    	}
    	scheduleMaintenance();   // let an executor know that a new item is in the queue.
    	return taskConsumed;
K
kohsuke 已提交
376
    }
377 378 379 380 381 382 383 384
    
    public synchronized boolean add(Task p, int quietPeriod) {
    	return add(p, quietPeriod, new Action[0]);
    }

    public synchronized boolean add(Task p, int quietPeriod, Action... actions) {
    	return add(p, quietPeriod, Arrays.asList(actions));
    }
K
kohsuke 已提交
385

K
kohsuke 已提交
386
    /**
387
     * Cancels the item in the queue. If the item is scheduled more than once, cancels the first occurrence.
K
kohsuke 已提交
388
     *
389 390
     * @return true if the project was indeed in the queue and was removed.
     *         false if this was no-op.
K
kohsuke 已提交
391
     */
K
kohsuke 已提交
392
    public synchronized boolean cancel(Task p) {
K
kohsuke 已提交
393
        LOGGER.fine("Cancelling " + p.getFullDisplayName());
K
kohsuke 已提交
394 395
        for (Iterator<WaitingItem> itr = waitingList.iterator(); itr.hasNext();) {
            Item item = itr.next();
K
kohsuke 已提交
396
            if (item.task.equals(p)) {
K
kohsuke 已提交
397
                itr.remove();
K
kohsuke 已提交
398
                return true;
K
kohsuke 已提交
399 400
            }
        }
K
kohsuke 已提交
401
        // use bitwise-OR to make sure that both branches get evaluated all the time
402
        return blockedProjects.remove(p)!=null | buildables.remove(p)!=null;
K
kohsuke 已提交
403
    }
404 405 406 407 408 409
    
    public synchronized boolean cancel(Item item) {
        LOGGER.fine("Cancelling " + item.task.getFullDisplayName() + " item#" + item.id);
        // use bitwise-OR to make sure that both branches get evaluated all the time
        return (item instanceof WaitingItem && waitingList.remove(item)) | blockedProjects.remove(item) | buildables.remove(item);
    }
K
kohsuke 已提交
410 411

    public synchronized boolean isEmpty() {
412
        return waitingList.isEmpty() && blockedProjects.isEmpty() && buildables.isEmpty();
K
kohsuke 已提交
413 414
    }

415
    private synchronized WaitingItem peek() {
416
        return waitingList.iterator().next();
K
kohsuke 已提交
417 418 419 420 421
    }

    /**
     * Gets a snapshot of items in the queue.
     */
422
    @Exported(inline=true)
K
kohsuke 已提交
423
    public synchronized Item[] getItems() {
424 425 426
        Item[] r = new Item[waitingList.size() + blockedProjects.size() + buildables.size()];
        waitingList.toArray(r);
        int idx = waitingList.size();
427 428 429 430
        for (BlockedItem p : blockedProjects.values())
            r[idx++] = p;
        for (BuildableItem p : buildables.values())
            r[idx++] = p;
K
kohsuke 已提交
431 432
        return r;
    }
433 434 435 436 437 438 439
    
    public synchronized Item getItem(int id) {
    	for (Item item: waitingList) if (item.id == id) return item;
    	for (Item item: blockedProjects) if (item.id == id) return item;
    	for (Item item: buildables) if (item.id == id) return item;
    	return null;
    }
K
kohsuke 已提交
440

K
kohsuke 已提交
441 442 443
    /**
     * Gets all the {@link BuildableItem}s that are waiting for an executor in the given {@link Computer}.
     */
444 445 446 447
    public synchronized List<BuildableItem> getBuildableItems(Computer c) {
        List<BuildableItem> result = new ArrayList<BuildableItem>();
        for (BuildableItem p : buildables.values()) {
            Label l = p.task.getAssignedLabel();
448 449 450 451 452
            if (l != null) {
                // if a project has assigned label, it can be only built on it
                if (!l.contains(c.getNode()))
                    continue;
            }
453
            result.add(p);
454
        }
455
        return result;
456 457
    }

K
kohsuke 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    /**
     * Gets the snapshot of {@link #buildables}.
     */
    public synchronized List<BuildableItem> getBuildableItems() {
        return new ArrayList<BuildableItem>(buildables.values());
    }

    /**
     * How many {@link BuildableItem}s are assigned for the given label?
     */
    public synchronized int countBuildableItemsFor(Label l) {
        int r = 0;
        for (BuildableItem bi : buildables.values())
            if(bi.task.getAssignedLabel()==l)
                r++;
        return r;
    }

K
kohsuke 已提交
476 477 478 479 480
    /**
     * Gets the information about the queue item for the given project.
     *
     * @return null if the project is not in the queue.
     */
481 482
    public synchronized Item getItem(Task t) {
        BlockedItem bp = blockedProjects.get(t);
483
        if (bp!=null)
484 485
            return bp;
        BuildableItem bi = buildables.get(t);
486
        if(bi!=null)
487 488
            return bi;

489
        for (Item item : waitingList) {
490
            if (item.task == t)
K
kohsuke 已提交
491 492 493 494 495
                return item;
        }
        return null;
    }

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    /**
     * Gets the information about the queue item for the given project.
     *
     * @return null if the project is not in the queue.
     */
    public synchronized List<Item> getItems(Task t) {
    	List<Item> result =new ArrayList<Item>();
    	result.addAll(blockedProjects.getAll(t));
    	result.addAll(buildables.getAll(t));
        for (Item item : waitingList) {
            if (item.task == t)
                result.add(item);
        }
        return result;
    }

512 513
    /**
     * Left for backward compatibility.
514
     *
515 516
     * @see #getItem(Task)
    public synchronized Item getItem(AbstractProject p) {
517
        return getItem((Task) p);
518
    }
K
kohsuke 已提交
519
     */
520

K
kohsuke 已提交
521
    /**
K
kohsuke 已提交
522
     * Returns true if this queue contains the said project.
K
kohsuke 已提交
523
     */
524 525
    public synchronized boolean contains(Task t) {
        if (blockedProjects.containsKey(t) || buildables.containsKey(t))
K
kohsuke 已提交
526
            return true;
527
        for (Item item : waitingList) {
528
            if (item.task == t)
K
kohsuke 已提交
529 530 531 532 533 534 535
                return true;
        }
        return false;
    }

    /**
     * Called by the executor to fetch something to build next.
536
     * <p>
K
kohsuke 已提交
537 538
     * This method blocks until a next project becomes buildable.
     */
539
    public Queue.Item pop() throws InterruptedException {
K
kohsuke 已提交
540
        final Executor exec = Executor.currentExecutor();
541

K
kohsuke 已提交
542
        try {
543
            while (true) {
K
kohsuke 已提交
544 545 546
                final JobOffer offer = new JobOffer(exec);
                long sleep = -1;

547
                synchronized (this) {
K
kohsuke 已提交
548 549
                    // consider myself parked
                    assert !parked.containsKey(exec);
550
                    parked.put(exec, offer);
K
kohsuke 已提交
551

K
kohsuke 已提交
552
                    // reuse executor thread to do a queue maintenance.
K
kohsuke 已提交
553 554 555 556 557
                    // at the end of this we get all the buildable jobs
                    // in the buildables field.
                    maintain();

                    // allocate buildable jobs to executors
558
                    Iterator<BuildableItem> itr = buildables.iterator();
559
                    while (itr.hasNext()) {
560
                        BuildableItem p = itr.next();
561 562

                        // one last check to make sure this build is not blocked.
563
                        if (isBuildBlocked(p.task)) {
564
                            itr.remove();
565
                            blockedProjects.put(p.task,new BlockedItem(p));
566 567
                            continue;
                        }
568

569
                        JobOffer runner = choose(p.task);
570
                        if (runner == null)
K
kohsuke 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
                            // if we couldn't find the executor that fits,
                            // just leave it in the buildables list and
                            // check if we can execute other projects
                            continue;

                        // found a matching executor. use it.
                        runner.set(p);
                        itr.remove();
                    }

                    // we went over all the buildable projects and awaken
                    // all the executors that got work to do. now, go to sleep
                    // until this thread is awakened. If this executor assigned a job to
                    // itself above, the block method will return immediately.

586
                    if (!waitingList.isEmpty()) {
K
kohsuke 已提交
587
                        // wait until the first item in the queue is due
588 589
                        sleep = peek().timestamp.getTimeInMillis() - new GregorianCalendar().getTimeInMillis();
                        if (sleep < 100) sleep = 100;    // avoid wait(0)
K
kohsuke 已提交
590 591 592 593 594
                    }
                }

                // this needs to be done outside synchronized block,
                // so that executors can maintain a queue while others are sleeping
595
                if (sleep == -1)
K
kohsuke 已提交
596 597 598 599
                    offer.event.block();
                else
                    offer.event.block(sleep);

600
                synchronized (this) {
601
                    // retract the offer object
602
                    assert parked.get(exec) == offer;
603 604
                    parked.remove(exec);

K
kohsuke 已提交
605
                    // am I woken up because I have a project to build?
606 607
                    if (offer.item != null) {
                        LOGGER.fine("Pop returning " + offer.item + " for " + exec.getName());
K
kohsuke 已提交
608
                        // if so, just build it
609
                        return offer.item;
K
kohsuke 已提交
610 611 612 613 614
                    }
                    // otherwise run a queue maintenance
                }
            }
        } finally {
615
            synchronized (this) {
K
kohsuke 已提交
616
                // remove myself from the parked list
617
                JobOffer offer = parked.remove(exec);
618
                if (offer != null && offer.item != null) {
619 620 621 622 623
                    // we are already assigned a project,
                    // ask for someone else to build it.
                    // note that while this thread is waiting for CPU
                    // someone else can schedule this build again,
                    // so check the contains method first.
624
                    if (!contains(offer.item.task))
625
                        buildables.put(offer.item.task,offer.item);
K
kohsuke 已提交
626
                }
627 628 629 630 631 632

                // since this executor might have been chosen for
                // maintenance, schedule another one. Worst case
                // we'll just run a pointless maintenance, and that's
                // fine.
                scheduleMaintenance();
K
kohsuke 已提交
633 634 635 636 637
            }
        }
    }

    /**
K
kohsuke 已提交
638
     * Chooses the executor to carry out the build for the given project.
K
kohsuke 已提交
639
     *
640
     * @return null if no {@link Executor} can run it.
K
kohsuke 已提交
641
     */
642
    private JobOffer choose(Task p) {
643
        if (Hudson.getInstance().isQuietingDown()) {
K
kohsuke 已提交
644 645 646 647 648
            // if we are quieting down, don't run anything so that
            // all executors will be free.
            return null;
        }

649
        Label l = p.getAssignedLabel();
650
        if (l != null) {
651
            // if a project has assigned label, it can be only built on it
K
kohsuke 已提交
652
            for (JobOffer offer : parked.values()) {
653
                if (offer.isAvailable() && l.contains(offer.getNode()))
K
kohsuke 已提交
654 655 656 657 658
                    return offer;
            }
            return null;
        }

659
        // if we are a large deployment, then we will favor slaves
K
kohsuke 已提交
660
        boolean isLargeHudson = Hudson.getInstance().getNodes().size() > 10;
661

662
        // otherwise let's see if the last node where this project was built is available
K
kohsuke 已提交
663 664
        // it has up-to-date workspace, so that's usually preferable.
        // (but we can't use an exclusive node)
665
        Node n = p.getLastBuiltOn();
666
        if (n != null && n.getMode() == Mode.NORMAL) {
K
kohsuke 已提交
667
            for (JobOffer offer : parked.values()) {
668 669
                if (offer.isAvailable() && offer.getNode() == n) {
                    if (isLargeHudson && offer.getNode() instanceof Slave)
S
stephenconnolly 已提交
670
                        // but if we are a large Hudson, then we really do want to keep the master free from builds
671
                        continue;
K
kohsuke 已提交
672
                    return offer;
673
                }
K
kohsuke 已提交
674 675 676 677 678 679
            }
        }

        // duration of a build on a slave tends not to have an impact on
        // the master/slave communication, so that means we should favor
        // running long jobs on slaves.
680 681
        // Similarly if we have many slaves, master should be made available
        // for HTTP requests and coordination as much as possible
682
        if (isLargeHudson || p.getEstimatedDuration() > 15 * 60 * 1000) {
K
kohsuke 已提交
683 684
            // consider a long job to be > 15 mins
            for (JobOffer offer : parked.values()) {
685
                if (offer.isAvailable() && offer.getNode() instanceof Slave && offer.isNotExclusive())
K
kohsuke 已提交
686 687 688 689 690 691
                    return offer;
            }
        }

        // lastly, just look for any idle executor
        for (JobOffer offer : parked.values()) {
692
            if (offer.isAvailable() && offer.isNotExclusive())
K
kohsuke 已提交
693 694 695 696 697 698 699 700 701
                return offer;
        }

        // nothing available
        return null;
    }

    /**
     * Checks the queue and runs anything that can be run.
702 703
     *
     * <p>
K
kohsuke 已提交
704
     * When conditions are changed, this method should be invoked.
705
     * <p>
K
kohsuke 已提交
706 707 708 709 710 711 712
     * This wakes up one {@link Executor} so that it will maintain a queue.
     */
    public synchronized void scheduleMaintenance() {
        // this code assumes that after this method is called
        // no more executors will be offered job except by
        // the pop() code.
        for (Entry<Executor, JobOffer> av : parked.entrySet()) {
713
            if (av.getValue().item == null) {
K
kohsuke 已提交
714 715 716 717 718 719
                av.getValue().event.signal();
                return;
            }
        }
    }

720 721 722
    /**
     * Checks if the given task is blocked.
     */
723
    private boolean isBuildBlocked(Task t) {
724 725 726
        return t.isBuildBlocked() || !canRun(t.getResourceList());
    }

K
kohsuke 已提交
727 728

    /**
K
kohsuke 已提交
729
     * Queue maintenance.
730
     * <p>
731
     * Move projects between {@link #waitingList}, {@link #blockedProjects}, and {@link #buildables}
K
kohsuke 已提交
732 733
     * appropriately.
     */
734
    public synchronized void maintain() {
735 736
        if (LOGGER.isLoggable(Level.FINE))
            LOGGER.fine("Queue maintenance started " + this);
737

738
        Iterator<BlockedItem> itr = blockedProjects.values().iterator();
739
        while (itr.hasNext()) {
740 741
            BlockedItem p = itr.next();
            if (!isBuildBlocked(p.task)) {
K
kohsuke 已提交
742
                // ready to be executed
K
kohsuke 已提交
743
                LOGGER.fine(p.task.getFullDisplayName() + " no longer blocked");
K
kohsuke 已提交
744
                itr.remove();
745
                buildables.put(p.task,new BuildableItem(p));
K
kohsuke 已提交
746 747 748
            }
        }

749
        while (!waitingList.isEmpty()) {
750
            WaitingItem top = peek();
K
kohsuke 已提交
751

752
            if (!top.timestamp.before(new GregorianCalendar()))
K
kohsuke 已提交
753 754
                return; // finished moving all ready items from queue

755
            Task p = top.task;
756
            if (!isBuildBlocked(p)) {
K
kohsuke 已提交
757
                // ready to be executed immediately
758
                waitingList.remove(top);
K
kohsuke 已提交
759
                LOGGER.fine(p.getFullDisplayName() + " ready to build");
760
                buildables.put(p,new BuildableItem(top));
K
kohsuke 已提交
761
            } else {
762
                // this can't be built now because another build is in progress
K
kohsuke 已提交
763
                // set this project aside.
764
                waitingList.remove(top);
K
kohsuke 已提交
765
                LOGGER.fine(p.getFullDisplayName() + " is blocked");
766
                blockedProjects.put(p,new BlockedItem(top));
K
kohsuke 已提交
767 768 769 770
            }
        }
    }

771 772 773 774
    public Api getApi() {
        return new Api(this);
    }

K
kohsuke 已提交
775 776
    /**
     * Task whose execution is controlled by the queue.
777
     *
778
     * <p>
K
kohsuke 已提交
779 780 781
     * {@link #equals(Object) Value equality} of {@link Task}s is used
     * to collapse two tasks into one. This is used to avoid infinite
     * queue backlog.
782 783 784 785
     *
     * <p>
     * Pending {@link Task}s are persisted when Hudson shuts down, so
     * it needs to be persistable.
K
kohsuke 已提交
786
     */
787
    public interface Task extends ModelObject, ResourceActivity {
788
        /**
789 790 791
         * If this task needs to be run on a node with a particular label,
         * return that {@link Label}. Otherwise null, indicating
         * it can run on anywhere.
792
         */
793
        Label getAssignedLabel();
794 795 796 797 798 799 800 801 802 803 804

        /**
         * If the previous execution of this task run on a certain node
         * and this task prefers to run on the same node, return that.
         * Otherwise null.
         */
        Node getLastBuiltOn();

        /**
         * Returns true if the execution should be blocked
         * for temporary reasons.
805 806
         *
         * <p>
K
kohsuke 已提交
807 808
         * This can be used to define mutual exclusion that goes beyond
         * {@link #getResourceList()}.
809 810 811 812 813 814 815 816 817 818 819
         */
        boolean isBuildBlocked();

        /**
         * When {@link #isBuildBlocked()} is true, this method returns
         * human readable description of why the build is blocked.
         * Used for HTML rendering.
         */
        String getWhyBlocked();

        /**
K
kohsuke 已提交
820
         * Unique name of this task.
K
kohsuke 已提交
821
         *
K
kohsuke 已提交
822 823
         * <p>
         * This method is no longer used, left here for compatibility. Just return {@link #getDisplayName()}.
824 825 826
         */
        String getName();

827 828 829 830 831
        /**
         * @see hudson.model.Item#getFullDisplayName()
         */
        String getFullDisplayName();

832 833 834 835
        /**
         * Estimate of how long will it take to execute this task.
         * Measured in milliseconds.
         *
836
         * @return -1 if it's impossible to estimate.
837 838 839
         */
        long getEstimatedDuration();

K
kohsuke 已提交
840
        /**
841
         * Creates {@link Executable}, which performs the actual execution of the task.
K
kohsuke 已提交
842
         */
843
        Executable createExecutable() throws IOException;
844 845 846 847 848 849 850 851

        /**
         * Checks the permission to see if the current user can abort this executable.
         * Returns normally from this method if it's OK.
         *
         * @throws AccessDeniedException if the permission is not granted.
         */
        void checkAbortPermission();
K
kohsuke 已提交
852 853 854 855 856 857

        /**
         * Works just like {@link #checkAbortPermission()} except it indicates the status by a return value,
         * instead of exception.
         */
        boolean hasAbortPermission();
858 859
        
        /**
K
kohsuke 已提交
860 861 862 863 864 865 866 867
         * Returns the URL of this task relative to the context root of the application.
         *
         * <p>
         * When the user clicks an item in the queue, this is the page where the user is taken to.
         * Hudson expects the current instance to be bound to the URL returned by this method.
         *
         * @return
         *      URL that ends with '/'.
868 869 870
         */
        String getUrl();
        
871 872 873 874 875
    }

    public interface Executable extends Runnable {
        /**
         * Task from which this executable was created.
K
kohsuke 已提交
876
         * Never null.
877 878 879 880 881 882 883
         */
        Task getParent();

        /**
         * Called by {@link Executor} to perform the task
         */
        void run();
884 885
    }

K
kohsuke 已提交
886 887 888
    /**
     * Item in a queue.
     */
889
    @ExportedBean(defaultVisibility = 999)
890 891 892 893 894
    public static abstract class Item extends Actionable {
    	
    	public final int id;
    	
		/**
K
kohsuke 已提交
895 896
         * Project to be built.
         */
897
        @Exported
898
        public final Task task;
899
        
900
        /**
901 902 903
         * Build is blocked because another build is in progress,
         * required {@link Resource}s are not available, or otherwise blocked
         * by {@link Task#isBuildBlocked()}.
904
         */
K
kohsuke 已提交
905
        @Exported
906
        public boolean isBlocked() { return this instanceof BlockedItem; }
907 908 909 910 911 912

        /**
         * Build is waiting the executor to become available.
         * This flag is only used in {@link Queue#getItems()} for
         * 'pseudo' items that are actually not really in the queue.
         */
K
kohsuke 已提交
913
        @Exported
914
        public boolean isBuildable() { return this instanceof BuildableItem; }
915

916 917 918 919 920 921
        /**
         * True if the item is starving for an executor for too long.
         */
        @Exported
        public boolean isStuck() { return false; }

922 923 924 925 926 927 928 929
        protected Item(Task task, List<Action> actions, int id) {
            this.task = task;
            this.id = id;
            for (Action action: actions) addAction(action);
        }
        
        protected Item(Item item) {
        	this(item.task, item.getActions(), item.id);
K
kohsuke 已提交
930 931
        }

932
        /**
933
         * Gets a human-readable status message describing why it's in the queue.
934
         */
K
kohsuke 已提交
935
        @Exported
936
        public abstract String getWhy();
K
kohsuke 已提交
937

938 939 940
        public boolean hasCancelPermission() {
            return task.hasAbortPermission();
        }
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
        
        public String getDisplayName() {
			// TODO Auto-generated method stub
			return null;
		}

		public String getSearchUrl() {
			// TODO Auto-generated method stub
			return null;
		}

        /**
         * Called from queue.jelly.
         */
        public void doCancelQueue( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
        	Hudson.getInstance().getQueue().cancel(this);
            rsp.forwardToPreviousPage(req);
        }
    }
    
    /**
     * An optional interface for actions on Queue.Item.
     * Lets the action cooperate in queue management.
     */
    public interface QueueAction extends Action {
    	/**
    	 * Returns whether the new item should be scheduled. 
    	 * An action should return true if the associated task is 'different enough' to warrant a separate execution.
    	 */
    	public boolean shouldSchedule(List<Action> actions);
971
    }
972

973 974 975
    /**
     * {@link Item} in the {@link Queue#waitingList} stage.
     */
976 977 978
    public static final class WaitingItem extends Item implements Comparable<WaitingItem> {
    	private static final AtomicInteger COUNTER = new AtomicInteger(0);
    	
979 980 981 982 983
        /**
         * This item can be run after this time.
         */
        @Exported
        public Calendar timestamp;
984

985 986
        WaitingItem(Calendar timestamp, Task project, List<Action> actions) {
            super(project, actions, COUNTER.incrementAndGet());
987 988
            this.timestamp = timestamp;
        }
989
        
990 991 992 993 994 995 996 997 998
        public int compareTo(WaitingItem that) {
            int r = this.timestamp.getTime().compareTo(that.timestamp.getTime());
            if (r != 0) return r;

            return this.id - that.id;
        }

        @Override
        public String getWhy() {
999
            long diff = timestamp.getTimeInMillis() - System.currentTimeMillis();
1000
            if (diff > 0)
K
i18n  
kohsuke 已提交
1001
                return Messages.Queue_InQuietPeriod(Util.getTimeSpanString(diff));
1002 1003 1004 1005
            else
                return Messages.Queue_Unknown();
        }
    }
K
kohsuke 已提交
1006

1007 1008 1009
    /**
     * Common part between {@link BlockedItem} and {@link BuildableItem}.
     */
1010
    public static abstract class NotWaitingItem extends Item {
1011 1012 1013 1014 1015 1016 1017
        /**
         * When did this job exit the {@link Queue#waitingList} phase?
         */
        @Exported
        public final long buildableStartMilliseconds;

        protected NotWaitingItem(WaitingItem wi) {
1018
            super(wi);
1019
            buildableStartMilliseconds = System.currentTimeMillis();
K
kohsuke 已提交
1020
        }
1021

1022
        protected NotWaitingItem(NotWaitingItem ni) {
1023
            super(ni);
1024
            buildableStartMilliseconds = ni.buildableStartMilliseconds;
1025
        }
1026
    }
1027

1028 1029 1030 1031 1032 1033 1034
    /**
     * {@link Item} in the {@link Queue#blockedProjects} stage.
     */
    public final class BlockedItem extends NotWaitingItem {
        public BlockedItem(WaitingItem wi) {
            super(wi);
        }
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        public BlockedItem(NotWaitingItem ni) {
            super(ni);
        }

        @Override
        public String getWhy() {
            ResourceActivity r = getBlockingActivity(task);
            if (r != null) {
                if (r == task) // blocked by itself, meaning another build is in progress
                    return Messages.Queue_InProgress();
                return Messages.Queue_BlockedBy(r.getDisplayName());
            }
            return task.getWhyBlocked();
1049
        }
1050
    }
1051

1052 1053 1054
    /**
     * {@link Item} in the {@link Queue#buildables} stage.
     */
1055
    public final static class BuildableItem extends NotWaitingItem {
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
        public BuildableItem(WaitingItem wi) {
            super(wi);
        }

        public BuildableItem(NotWaitingItem ni) {
            super(ni);
        }

        @Override
        public String getWhy() {
1066
            Label label = task.getAssignedLabel();
1067
            Hudson hudson = Hudson.getInstance();
K
kohsuke 已提交
1068
            if (hudson.getNodes().isEmpty())
1069
                label = null;    // no master/slave. pointless to talk about nodes
1070 1071

            String name = null;
1072 1073 1074 1075
            if (label != null) {
                name = label.getName();
                if (label.isOffline()) {
                    if (label.getNodes().size() > 1)
K
i18n  
kohsuke 已提交
1076
                        return Messages.Queue_AllNodesOffline(name);
1077
                    else
K
i18n  
kohsuke 已提交
1078
                        return Messages.Queue_NodeOffline(name);
1079 1080 1081
                }
            }

K
i18n  
kohsuke 已提交
1082 1083 1084 1085
            if(name==null)
                return Messages.Queue_WaitingForNextAvailableExecutor();
            else
                return Messages.Queue_WaitingForNextAvailableExecutorOn(name);
1086
        }
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

        @Override
        public boolean isStuck() {
            Label label = task.getAssignedLabel();
            if(label!=null && label.isOffline())
                // no executor online to process this job. definitely stuck.
                return true;

            long d = task.getEstimatedDuration();
            long elapsed = System.currentTimeMillis()-buildableStartMilliseconds;
            if(d>=0) {
1098
                // if we were running elsewhere, we would have done this build ten times.
1099
                return elapsed > Math.max(d,60000L)*10;
1100 1101
            } else {
                // more than a day in the queue
1102
                return TimeUnit2.MILLISECONDS.toHours(elapsed)>24;
1103 1104
            }
        }
K
kohsuke 已提交
1105 1106 1107 1108 1109
    }

    /**
     * Unique number generator
     */
1110
    private int iota = 0;
K
kohsuke 已提交
1111 1112

    private static final Logger LOGGER = Logger.getLogger(Queue.class.getName());
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    /**
     * This {@link XStream} instance is used to persist {@link Task}s.
     */
    public static final XStream XSTREAM = new XStream2();

    static {
        XSTREAM.registerConverter(new AbstractSingleValueConverter() {

			@Override
			@SuppressWarnings("unchecked")
			public boolean canConvert(Class klazz) {
				return hudson.model.Item.class.isAssignableFrom(klazz);
			}

			@Override
			public Object fromString(String string) {
                Object item = Hudson.getInstance().getItemByFullName(string);
                if(item==null)  throw new NoSuchElementException("No such job exists: "+string);
                return item;
			}

			@Override
			public String toString(Object item) {
				return ((hudson.model.Item) item).getFullName();
			}
        });
        XSTREAM.registerConverter(new AbstractSingleValueConverter() {

			@SuppressWarnings("unchecked")
			@Override
			public boolean canConvert(Class klazz) {
				return Run.class.isAssignableFrom(klazz);
			}

			@Override
			public Object fromString(String string) {
				String[] split = string.split("#");
				String projectName = split[0];
				int buildNumber = Integer.parseInt(split[1]);
				Job<?,?> job = (Job<?,?>) Hudson.getInstance().getItemByFullName(projectName);
                if(job==null)  throw new NoSuchElementException("No such job exists: "+projectName);
				Run<?,?> run = job.getBuildByNumber(buildNumber);
K
kohsuke 已提交
1156
                if(run==null)  throw new NoSuchElementException("No such build: "+string);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
				return run;
			}

			@Override
			public String toString(Object object) {
				Run<?,?> run = (Run<?,?>) object;
				return run.getParent().getFullName() + "#" + run.getNumber();
			}
        });
    }

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
    /**
     * Regularly invokes {@link Queue#maintain()} and clean itself up when
     * {@link Queue} gets GC-ed.
     */
    private static class MaintainTask extends SafeTimerTask {
        private final WeakReference<Queue> queue;

        MaintainTask(Queue queue) {
            this.queue = new WeakReference<Queue>(queue);

            long interval = 5 * Timer.ONE_SECOND;
            Trigger.timer.schedule(this, interval, interval);
        }

        protected void doRun() {
            Queue q = queue.get();
1184
            if (q != null)
1185 1186 1187 1188 1189
                q.maintain();
            else
                cancel();
        }
    }
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    
    /**
     * A MultiMap -  LinkedMap crossover as a drop-in replacement for the previously used LinkedHashMap
     * And no, I don't care about performance ;)
     */
    private static class ItemList<T extends Item> extends ArrayList<T> {
    	public T get(Task task) {
    		for (T item: this) {
    			if (item.task == task) {
    				return item;
    			}
    		}
    		return null;
    	}
    	
    	public List<T> getAll(Task task) {
    		List<T> result = new ArrayList<T>();
    		for (T item: this) {
    			if (item.task == task) {
    				result.add(item);
    			}
    		}
    		return result;
    	}
    	
    	public boolean containsKey(Task task) {
    		return get(task) != null;
    	}
    	
    	public T remove(Task task) {
    		Iterator<T> it = iterator();
    		while (it.hasNext()) {
    			T t = it.next();
    			if (t.task == task) {
    				it.remove();
    				return t;
    			}
    		}
    		return null;
    	}
    	
    	public void put(Task task, T item) {
    		assert item.task == task;
    		add(item);
    	}
    	
    	public ItemList<T> values() {
    		return this;
    	}
    }
K
kohsuke 已提交
1240
}