提交 eedac7b0 编写于 作者: K kohsuke

added a mechanism to contribute SubTasks to AbstractProject.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34612 71c3de6d-444a-0410-be80-ed276b4c234a
上级 310a53b4
......@@ -41,12 +41,13 @@ import hudson.model.Cause.UserCause;
import hudson.model.Descriptor.FormException;
import hudson.model.Fingerprint.RangeSet;
import hudson.model.Queue.Executable;
import hudson.model.queue.ExecutionUnit;
import hudson.model.queue.SubTask;
import hudson.model.Queue.WaitingItem;
import hudson.model.RunMap.Constructor;
import hudson.model.labels.LabelAtom;
import hudson.model.labels.LabelExpression;
import hudson.model.queue.CauseOfBlockage;
import hudson.model.queue.SubTaskContributor;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
import hudson.scm.NullSCM;
......@@ -1002,9 +1003,13 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
return null;
}
public Collection<? extends ExecutionUnit> getMemberExecutionUnits() {
// TODO
return Collections.emptyList();
public List<SubTask> getSubTasks() {
List<SubTask> r = new ArrayList<SubTask>();
for (SubTaskContributor euc : SubTaskContributor.all())
r.addAll(euc.forProject(this));
for (JobProperty<? super P> p : properties)
r.addAll(p.getMemberExecutionUnits());
return r;
}
public R createExecutable() throws IOException {
......
......@@ -26,7 +26,7 @@ package hudson.model;
import hudson.Util;
import hudson.model.Queue.*;
import hudson.FilePath;
import hudson.model.queue.ExecutionUnit;
import hudson.model.queue.SubTask;
import hudson.model.queue.WorkUnit;
import hudson.util.TimeUnit2;
import hudson.util.InterceptingProxy;
......@@ -104,7 +104,7 @@ public class Executor extends Thread implements ModelObject {
// see issue #1583
if (Thread.interrupted()) continue;
ExecutionUnit task;
SubTask task;
try {
synchronized (queue) {// perform this state change as an atomic operation wrt other queue operations
workUnit = grabJob();
......
......@@ -26,6 +26,7 @@ package hudson.model;
import hudson.ExtensionPoint;
import hudson.Launcher;
import hudson.Plugin;
import hudson.model.queue.SubTask;
import hudson.tasks.BuildStep;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;
......@@ -163,4 +164,13 @@ public abstract class JobProperty<J extends Job<?,?>> implements Describable<Job
public final Collection<? extends Action> getProjectActions(AbstractProject<?,?> project) {
return getJobActions((J)project);
}
/**
* Contributes {@link SubTask}s to {@link AbstractProject#getSubTasks()}
*
* @since 1.FATTASK
*/
public Collection<? extends SubTask> getMemberExecutionUnits() {
return Collections.emptyList();
}
}
......@@ -35,7 +35,7 @@ import static hudson.util.Iterators.reverse;
import hudson.cli.declarative.CLIMethod;
import hudson.cli.declarative.CLIResolver;
import hudson.model.queue.AbstractQueueTask;
import hudson.model.queue.ExecutionUnit;
import hudson.model.queue.SubTask;
import hudson.model.queue.FutureImpl;
import hudson.model.queue.MatchingWorksheet;
import hudson.model.queue.MatchingWorksheet.Mapping;
......@@ -1028,8 +1028,14 @@ public class Queue extends ResourceController implements Saveable {
* Plugins are encouraged to extend from {@link AbstractQueueTask}
* instead of implementing this interface directly, to maintain
* compatibility with future changes to this interface.
*
* <p>
* For historical reasons, {@link Task} object by itself
* also represents the "primary" sub-task (and as implied by this
* design, a {@link Task} must have at least one sub-task.)
* Most of the time, the primary subtask is the only sub task.
*/
public interface Task extends ModelObject, ExecutionUnit {
public interface Task extends ModelObject, SubTask {
/**
* Returns true if the execution should be blocked
* for temporary reasons.
......@@ -1105,18 +1111,17 @@ public class Queue extends ResourceController implements Saveable {
boolean isConcurrentBuild();
/**
* Obtains the {@link ExecutionUnit}s that constitute this task.
*
* For historical reasons, {@link Task} itself implicitly forms one "main" {@link ExecutionUnit}
* (and in fact most of the time this is the only {@link Task}.)
* Obtains the {@link SubTask}s that constitute this task.
*
* The collection returned by this method doesn't contain this main {@link ExecutionUnit}.
* The returned value is read-only. Can be empty but never null.
* The collection returned by this method must also contain the primary {@link SubTask}
* represented by this {@link Task} object itself as the first element.
* The returned value is read-only.
*
* At least size 1.
*
* @since 1.FATTASK
*/
Collection<? extends ExecutionUnit> getMemberExecutionUnits();
Collection<? extends SubTask> getSubTasks();
}
/**
......@@ -1127,7 +1132,7 @@ public class Queue extends ResourceController implements Saveable {
* Task from which this executable was created.
* Never null.
*/
Task getParent();
SubTask getParent();
/**
* Called by {@link Executor} to perform the task
......
......@@ -37,7 +37,7 @@ import java.util.Collections;
* @since 1.360
*/
public abstract class AbstractQueueTask implements Queue.Task {
public Collection<? extends ExecutionUnit> getMemberExecutionUnits() {
public Collection<? extends SubTask> getSubTasks() {
return Collections.emptyList();
}
}
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, Inc.
*
* 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 hudson.model.queue;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.ResourceList;
/**
* Partial default implementation of {@link SubTask} to avoid
* {@link AbstractMethodError} with future additions to {@link SubTask}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class AbstractSubTask implements SubTask {
public Label getAssignedLabel() {
return null;
}
public Node getLastBuiltOn() {
return null;
}
public long getEstimatedDuration() {
return -1;
}
public Object getSameNodeConstraint() {
return null;
}
public ResourceList getResourceList() {
return new ResourceList();
}
}
......@@ -99,7 +99,7 @@ public class MatchingWorksheet {
}
}
public class WorkChunk extends ReadOnlyList<ExecutionUnit> {
public class WorkChunk extends ReadOnlyList<SubTask> {
public final int index;
// the main should be always at position 0
......@@ -123,7 +123,7 @@ public class MatchingWorksheet {
public final ExecutorChunk lastBuiltOn;
private WorkChunk(List<ExecutionUnit> base, int index) {
private WorkChunk(List<SubTask> base, int index) {
super(base);
assert base.size()>1;
this.index = index;
......@@ -224,26 +224,32 @@ public class MatchingWorksheet {
this.executors = Collections.unmodifiableList(executors);
// group execution units into chunks. use of LinkedHashMap ensures that the main work comes at the top
Map<Object,List<ExecutionUnit>> m = new LinkedHashMap<Object,List<ExecutionUnit>>();
build(task,m);
for (ExecutionUnit meu : task.getMemberExecutionUnits())
build(meu,m);
Map<Object,List<SubTask>> m = new LinkedHashMap<Object,List<SubTask>>();
for (SubTask meu : task.getSubTasks()) {
Object c = meu.getSameNodeConstraint();
if (c==null) c = new Object();
List<SubTask> l = m.get(c);
if (l==null)
m.put(c,l= new ArrayList<SubTask>());
l.add(meu);
}
// build into the final shape
List<WorkChunk> works = new ArrayList<WorkChunk>();
for (List<ExecutionUnit> group : m.values()) {
for (List<SubTask> group : m.values()) {
works.add(new WorkChunk(group,works.size()));
}
this.works = Collections.unmodifiableList(works);
}
private void build(ExecutionUnit eu, Map<Object, List<ExecutionUnit>> m) {
private void build(SubTask eu, Map<Object, List<SubTask>> m) {
Object c = eu.getSameNodeConstraint();
if (c==null) c = new Object();
List<ExecutionUnit> l = m.get(c);
List<SubTask> l = m.get(c);
if (l==null)
m.put(c,l= new ArrayList<ExecutionUnit>());
m.put(c,l= new ArrayList<SubTask>());
l.add(eu);
}
......
......@@ -107,7 +107,7 @@ public abstract class QueueTaskFilter implements Queue.Task {
return base.getResourceList();
}
public Collection<? extends ExecutionUnit> getMemberExecutionUnits() {
return base.getMemberExecutionUnits();
public Collection<? extends SubTask> getSubTasks() {
return base.getSubTasks();
}
}
......@@ -35,11 +35,16 @@ import java.io.IOException;
/**
* A component of {@link Task} that represents a computation carried out by a single {@link Executor}.
*
* A {@link Task} consists of a number of {@link ExecutionUnit}.
* A {@link Task} consists of a number of {@link SubTask}.
*
* <p>
* Plugins are encouraged to extend from {@link AbstractSubTask}
* instead of implementing this interface directly, to maintain
* compatibility with future changes to this interface.
*
* @since 1.FATTASK
*/
public interface ExecutionUnit extends ResourceActivity {
public interface SubTask extends ResourceActivity {
/**
* If this task needs to be run on a node with a particular label,
* return that {@link Label}. Otherwise null, indicating
......@@ -68,8 +73,8 @@ public interface ExecutionUnit extends ResourceActivity {
Executable createExecutable() throws IOException;
/**
* If a subset of {@link ExecutionUnit}s of a {@link Task} needs to be collocated with other {@link ExecutionUnit}s,
* those {@link ExecutionUnit}s should return the equal object here. If null, the execution unit isn't under a
* If a subset of {@link SubTask}s of a {@link Task} needs to be collocated with other {@link SubTask}s,
* those {@link SubTask}s should return the equal object here. If null, the execution unit isn't under a
* colocation constraint.
*
* @since 1.FATTASK
......
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, Inc.
*
* 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 hudson.model.queue;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import java.util.Collection;
import java.util.Collections;
/**
* Externally contributes {@link SubTask}s to {@link AbstractProject#getSubTasks()}.
*
* <p>
* Put @{@link Extension} on your implementation classes to register them.
*
* @author Kohsuke Kawaguchi
* @since 1.FATTASK
*/
public abstract class SubTaskContributor implements ExtensionPoint {
public Collection<? extends SubTask> forProject(AbstractProject<?,?> p) {
return Collections.emptyList();
}
/**
* All registered {@link MemberExecutionUnitContributor} instances.
*/
public static ExtensionList<SubTaskContributor> all() {
return Hudson.getInstance().getExtensionList(SubTaskContributor.class);
}
}
......@@ -31,25 +31,26 @@ import hudson.model.Queue.Task;
* Represents a unit of hand-over to {@link Executor} from {@link Queue}.
*
* @author Kohsuke Kawaguchi
* @since 1.FATTASK
*/
public final class WorkUnit {
/**
* Task to be executed.
*/
public final ExecutionUnit work;
public final SubTask work;
/**
* Shared context among {@link WorkUnit}s.
*/
public final WorkUnitContext context;
WorkUnit(WorkUnitContext context, ExecutionUnit work) {
WorkUnit(WorkUnitContext context, SubTask work) {
this.context = context;
this.work = work;
}
/**
* Is this work unit the "main work", which is the primary {@link ExecutionUnit}
* Is this work unit the "main work", which is the primary {@link SubTask}
* represented by {@link Task} itself.
*/
public boolean isMainWork() {
......
......@@ -62,7 +62,7 @@ public final class WorkUnitContext {
this.actions = item.getActions();
// +1 for the main task
workUnitSize = task.getMemberExecutionUnits().size() + 1;
workUnitSize = task.getSubTasks().size();
startLatch = new Latch(workUnitSize) {
@Override
protected void onCriteriaMet() {
......@@ -79,10 +79,10 @@ public final class WorkUnitContext {
}
/**
* Called by the executor that executes a member {@link ExecutionUnit} that belongs to this task
* Called by the executor that executes a member {@link SubTask} that belongs to this task
* to create its {@link WorkUnit}.
*/
public WorkUnit createWorkUnit(ExecutionUnit execUnit) {
public WorkUnit createWorkUnit(SubTask execUnit) {
future.addExecutor(Executor.currentExecutor());
return new WorkUnit(this,execUnit);
}
......
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, Inc.
*
* 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 hudson.model.queue;
import hudson.model.AbstractProject;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Queue.Executable;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.TestExtension;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
/**
* @author Kohsuke Kawaguchi
*/
public class WideExecutionTest extends HudsonTestCase {
boolean run = false;
@TestExtension
public class Contributer extends SubTaskContributor {
public Collection<? extends SubTask> forProject(final AbstractProject<?, ?> p) {
return Collections.singleton(new AbstractSubTask() {
private final AbstractSubTask outer = this;
public Executable createExecutable() throws IOException {
return new Executable() {
public SubTask getParent() {
return outer;
}
public void run() {
run = true;
// no-op
}
};
}
public String getDisplayName() {
return "Company of "+p.getDisplayName();
}
});
}
}
public void testRun() throws Exception {
FreeStyleProject p = createFreeStyleProject();
FreeStyleBuild b = assertBuildStatusSuccess(p.scheduleBuild2(0));
assertTrue(run);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册