提交 38b75f8b 编写于 作者: H huybrechts

[FIXED HUDSON-1613] implements touchstone build for matrix projects, which...

[FIXED HUDSON-1613] implements touchstone build for matrix projects, which runs a selected number of configurations first and then all the rests if these succeed

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@20737 71c3de6d-444a-0410-be80-ed276b4c234a
上级 4f9510a0
......@@ -23,6 +23,7 @@
*/
package hudson.matrix;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
......@@ -42,6 +43,7 @@ import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.kohsuke.stapler.StaplerRequest;
......@@ -115,8 +117,10 @@ public class MatrixBuild extends AbstractBuild<MatrixProject,MatrixBuild> {
*/
public List<MatrixRun> getRuns() {
List<MatrixRun> r = new ArrayList<MatrixRun>();
for(MatrixConfiguration c : getParent().getItems())
r.add(c.getBuildByNumber(getNumber()));
for(MatrixConfiguration c : getParent().getItems()) {
MatrixRun b = c.getBuildByNumber(getNumber());
if (b != null) r.add(b);
}
return r;
}
......@@ -174,6 +178,17 @@ public class MatrixBuild extends AbstractBuild<MatrixProject,MatrixBuild> {
axes = p.getAxes();
Collection<MatrixConfiguration> activeConfigurations = p.getActiveConfigurations();
final int n = getNumber();
String touchStoneFilter = p.getTouchStoneCombinationFilter();
Collection<MatrixConfiguration> touchStoneConfigurations = new HashSet<MatrixConfiguration>();
Collection<MatrixConfiguration> delayedConfigurations = new HashSet<MatrixConfiguration>();
for (MatrixConfiguration c: activeConfigurations) {
if (touchStoneFilter != null && c.getCombination().evalGroovyExpression(p.getAxes(), p.getTouchStoneCombinationFilter())) {
touchStoneConfigurations.add(c);
} else {
delayedConfigurations.add(c);
}
}
for (MatrixAggregator a : aggregators)
if(!a.startBuild())
......@@ -181,73 +196,41 @@ public class MatrixBuild extends AbstractBuild<MatrixProject,MatrixBuild> {
try {
if(!p.isRunSequentially())
for(MatrixConfiguration c : activeConfigurations)
for(MatrixConfiguration c : touchStoneConfigurations)
scheduleConfigurationBuild(logger, c);
// this occupies an executor unnecessarily.
// it would be nice if this can be placed in a temproary executor.
Result r = Result.SUCCESS;
for (MatrixConfiguration c : activeConfigurations) {
for (MatrixConfiguration c : touchStoneConfigurations) {
if(p.isRunSequentially())
scheduleConfigurationBuild(logger, c);
String whyInQueue = "";
long startTime = System.currentTimeMillis();
// wait for the completion
int appearsCancelledCount = 0;
while(true) {
MatrixRun b = c.getBuildByNumber(n);
// two ways to get beyond this. one is that the build starts and gets done,
// or the build gets cancelled before it even started.
Result buildResult = null;
if(b!=null && !b.isBuilding())
buildResult = b.getResult();
Queue.Item qi = c.getQueueItem();
if(b==null && qi==null)
appearsCancelledCount++;
else
appearsCancelledCount = 0;
if(appearsCancelledCount>=5) {
// there's conceivably a race condition in computating b and qi, as their computation
// are not synchronized. There are indeed several reports of Hudson incorrectly assuming
// builds being cancelled. See
// http://www.nabble.com/Master-slave-problem-tt14710987.html and also
// http://www.nabble.com/Anyone-using-AccuRev-plugin--tt21634577.html#a21671389
// because of this, we really make sure that the build is cancelled by doing this 5
// times over 5 seconds
logger.println(Messages.MatrixBuild_AppearsCancelled(c.getDisplayName()));
buildResult = Result.ABORTED;
}
Result buildResult = waitForCompletion(listener, c);
r = r.combine(buildResult);
}
if (p.getTouchStoneResultCondition() != null && r.isWorseThan(p.getTouchStoneResultCondition())) {
logger.printf("Touchstone configurations resulted in %s, so aborting...\n", r);
return r;
}
if(!p.isRunSequentially())
for(MatrixConfiguration c : delayedConfigurations)
scheduleConfigurationBuild(logger, c);
if(buildResult!=null) {
r = r.combine(buildResult);
if(b!=null)
for (MatrixAggregator a : aggregators)
if(!a.endRun(b))
return Result.FAILURE;
break;
} else {
if(qi!=null) {
// if the build seems to be stuck in the queue, display why
String why = qi.getWhy();
if(!why.equals(whyInQueue) && System.currentTimeMillis()-startTime>5000) {
logger.println(c.getDisplayName()+" is still in the queue: "+why);
whyInQueue = why;
}
}
}
Thread.sleep(1000);
}
for (MatrixConfiguration c : delayedConfigurations) {
if(p.isRunSequentially())
scheduleConfigurationBuild(logger, c);
Result buildResult = waitForCompletion(listener, c);
r = r.combine(buildResult);
}
return r;
} catch( InterruptedException e ) {
logger.println("Aborted");
return Result.ABORTED;
} finally {
} catch (AggregatorFailureException e) {
return Result.FAILURE;
}
finally {
// if the build was aborted in the middle. Cancel all the configuration builds.
Queue q = Hudson.getInstance().getQueue();
synchronized(q) {// avoid micro-locking in q.cancel.
......@@ -266,6 +249,58 @@ public class MatrixBuild extends AbstractBuild<MatrixProject,MatrixBuild> {
}
}
}
private Result waitForCompletion(BuildListener listener, MatrixConfiguration c) throws InterruptedException, IOException, AggregatorFailureException {
String whyInQueue = "";
long startTime = System.currentTimeMillis();
// wait for the completion
int appearsCancelledCount = 0;
while(true) {
MatrixRun b = c.getBuildByNumber(getNumber());
// two ways to get beyond this. one is that the build starts and gets done,
// or the build gets cancelled before it even started.
Result buildResult = null;
if(b!=null && !b.isBuilding())
buildResult = b.getResult();
Queue.Item qi = c.getQueueItem();
if(b==null && qi==null)
appearsCancelledCount++;
else
appearsCancelledCount = 0;
if(appearsCancelledCount>=5) {
// there's conceivably a race condition in computating b and qi, as their computation
// are not synchronized. There are indeed several reports of Hudson incorrectly assuming
// builds being cancelled. See
// http://www.nabble.com/Master-slave-problem-tt14710987.html and also
// http://www.nabble.com/Anyone-using-AccuRev-plugin--tt21634577.html#a21671389
// because of this, we really make sure that the build is cancelled by doing this 5
// times over 5 seconds
listener.getLogger().println(Messages.MatrixBuild_AppearsCancelled(c.getDisplayName()));
buildResult = Result.ABORTED;
}
if(buildResult!=null) {
for (MatrixAggregator a : aggregators)
if(!a.endRun(b))
throw new AggregatorFailureException();
return buildResult;
}
if(qi!=null) {
// if the build seems to be stuck in the queue, display why
String why = qi.getWhy();
if(!why.equals(whyInQueue) && System.currentTimeMillis()-startTime>5000) {
listener.getLogger().println(c.getDisplayName()+" is still in the queue: "+why);
whyInQueue = why;
}
}
Thread.sleep(1000);
}
}
private void scheduleConfigurationBuild(PrintStream logger, MatrixConfiguration c) {
logger.println(Messages.MatrixBuild_Triggering(c.getDisplayName()));
......@@ -277,4 +312,10 @@ public class MatrixBuild extends AbstractBuild<MatrixProject,MatrixBuild> {
a.endBuild();
}
}
/**
* A private exception to help maintain the correct control flow after extracting the 'waitForCompletion' method
*/
private static class AggregatorFailureException extends Exception {}
}
......@@ -40,6 +40,7 @@ import hudson.model.JDK;
import hudson.model.Job;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.Result;
import hudson.model.SCMedItem;
import hudson.model.Saveable;
import hudson.model.TopLevelItem;
......@@ -134,6 +135,9 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
private transient /*final*/ Set<MatrixConfiguration> activeConfigurations = new LinkedHashSet<MatrixConfiguration>();
private boolean runSequentially;
private String touchStoneCombinationFilter;
private Result touchStoneResultCondition;
public MatrixProject(String name) {
super(Hudson.getInstance(), name);
......@@ -195,7 +199,24 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
return combinationFilter;
}
protected void updateTransientActions() {
public String getTouchStoneCombinationFilter() {
return touchStoneCombinationFilter;
}
public void setTouchStoneCombinationFilter(
String touchStoneCombinationFilter) {
this.touchStoneCombinationFilter = touchStoneCombinationFilter;
}
public Result getTouchStoneResultCondition() {
return touchStoneResultCondition;
}
public void setTouchStoneResultCondition(Result touchStoneResultCondition) {
this.touchStoneResultCondition = touchStoneResultCondition;
}
protected void updateTransientActions() {
synchronized(transientActions) {
super.updateTransientActions();
......@@ -533,10 +554,19 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
}
}
if(req.getParameter("hasCombinationFilter")!=null)
if(req.getParameter("hasCombinationFilter")!=null) {
this.combinationFilter = Util.nullify(req.getParameter("combinationFilter"));
else
} else {
this.combinationFilter = null;
}
if (req.getParameter("hasTouchStoneCombinationFilter")!=null) {
this.touchStoneCombinationFilter = Util.nullify(req.getParameter("touchStoneCombinationFilter"));
String touchStoneResultCondition = req.getParameter("touchStoneResultCondition");
this.touchStoneResultCondition = Result.fromString(touchStoneResultCondition);
} else {
this.touchStoneCombinationFilter = null;
}
// parse system axes
newAxes.add(Axis.parsePrefixed(req,"jdk"));
......
......@@ -119,6 +119,13 @@ public final class Result implements Serializable, CustomExportedBean {
public String toExportedObject() {
return name;
}
public static Result fromString(String s) {
for (Result r : all)
if (s.equals(r.name))
return r;
return FAILURE;
}
private static final long serialVersionUID = 1L;
......@@ -130,10 +137,7 @@ public final class Result implements Serializable, CustomExportedBean {
}
protected Object fromString(String s) {
for (Result r : all)
if (s.equals(r.name))
return r;
return FAILURE;
return Result.fromString(s);
}
};
}
......@@ -114,6 +114,19 @@ THE SOFTWARE.
</f:entry>
</f:optionalBlock>
<f:optionalBlock name="hasTouchStoneCombinationFilter" title="${%Execute touchstone builds first}" checked="${!empty(it.touchStoneCombinationFilter)}"
help="/help/matrix/touchstone.html">
<f:entry title="${%Filter}">
<f:textbox name="touchStoneCombinationFilter" value="${it.touchStoneCombinationFilter}" />
</f:entry>
<f:entry title="Required result" description="Execute the rest of the combinations only if the touchstone builds has (at least) the selected result.">
<select name="touchStoneResultCondition">
<f:option value="SUCCESS" selected='${it.touchStoneResultCondition.toExportedObject()=="SUCCESS"}'>Stable</f:option>
<f:option value="UNSTABLE" selected='${it.touchStoneResultCondition.toExportedObject()=="UNSTABLE"}'>Unstable</f:option>
</select>
</f:entry>
</f:optionalBlock>
</f:section>
<p:config-buildWrappers />
......
......@@ -41,6 +41,7 @@ import java.io.IOException;
* @author Kohsuke Kawaguchi
*/
public class FailureBuilder extends Builder {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().println("Simulating a failure");
build.setResult(Result.FAILURE);
......
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* 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 org.jvnet.hudson.test;
import hudson.Launcher;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import java.io.IOException;
/**
* Mock {@link Builder} that always cause a build to fail.
*
* @author Kohsuke Kawaguchi
*/
public class UnstableBuilder extends Builder {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().println("Simulating an unstable build");
build.setResult(Result.UNSTABLE);
return false;
}
@Extension
public static final class DescriptorImpl extends Descriptor<Builder> {
public Builder newInstance(StaplerRequest req, JSONObject data) {
throw new UnsupportedOperationException();
}
public String getDisplayName() {
return "Make build unstable";
}
}
}
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.
-->
<j:jelly xmlns:j="jelly:core" />
\ No newline at end of file
......@@ -33,6 +33,7 @@ import hudson.tasks.Fingerprinter;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.SingleFileSCM;
import org.jvnet.hudson.test.Email;
import org.jvnet.hudson.test.UnstableBuilder;
import java.io.IOException;
import java.util.List;
......@@ -84,6 +85,31 @@ public class MatrixProjectTest extends HudsonTestCase {
assertFalse(run.getLog().contains("-Dprop=${db}"));
}
}
/**
* Test that configuration filters work
*/
public void testConfigurationFilter() throws Exception {
MatrixProject p = createMatrixProject();
p.setCombinationFilter("db==\"mysql\"");
MatrixBuild build = p.scheduleBuild2(0).get();
assertEquals(2, build.getRuns().size());
}
/**
* Test that touch stone builds work
*/
public void testTouchStone() throws Exception {
MatrixProject p = createMatrixProject();
p.setTouchStoneCombinationFilter("db==\"mysql\"");
p.setTouchStoneResultCondition(Result.SUCCESS);
MatrixBuild build = p.scheduleBuild2(0).get();
assertEquals(4, build.getRuns().size());
p.getBuildersList().add(new UnstableBuilder());
build = p.scheduleBuild2(0).get();
assertEquals(2, build.getRuns().size());
}
@Override
protected MatrixProject createMatrixProject() throws IOException {
......
<div>
Use a touchstone build if you want to run a sanity check before building all the combinations.
You can select a number of combinations using a combination filter. These will be built first.
If the result satisfies the result condition, the rest of the combinations will be built.
</div>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册