-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flowscanner reimplement jenkins28119 error info #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2016 CloudBees, 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 org.jenkinsci.plugins.workflow.steps; | ||
|
||
import com.google.common.base.Predicate; | ||
import com.google.inject.Inject; | ||
import hudson.AbortException; | ||
import hudson.Extension; | ||
import hudson.Functions; | ||
import hudson.model.Result; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; | ||
import org.jenkinsci.plugins.workflow.actions.ErrorAction; | ||
import org.jenkinsci.plugins.workflow.actions.LogAction; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecution; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner; | ||
import org.jenkinsci.plugins.workflow.graph.BlockEndNode; | ||
import org.jenkinsci.plugins.workflow.graph.FlowNode; | ||
import org.jenkinsci.plugins.workflow.graph.FlowScanner; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Step to supply contextual information about an error that has been caught. | ||
*/ | ||
public class ErrorInfoStep extends AbstractStepImpl { | ||
|
||
public final Throwable error; | ||
|
||
@DataBoundConstructor public ErrorInfoStep(Throwable error) { | ||
this.error = error; | ||
} | ||
|
||
public static class Execution extends AbstractSynchronousStepExecution<ErrorInfo> { | ||
|
||
private static final long serialVersionUID = 1; | ||
@Inject private transient ErrorInfoStep step; | ||
@StepContextParameter private transient FlowExecution execution; | ||
|
||
@Override protected ErrorInfo run() throws Exception { | ||
return new ErrorInfo(step.error, execution); | ||
} | ||
|
||
} | ||
|
||
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { | ||
|
||
public DescriptorImpl() { | ||
super(Execution.class); | ||
} | ||
|
||
@Override public String getFunctionName() { | ||
return "errorInfo"; | ||
} | ||
|
||
@Override public String getDisplayName() { | ||
return "Calculate information about an error"; | ||
} | ||
|
||
// TODO blank config.jelly | ||
|
||
} | ||
|
||
public static class ErrorInfo implements Serializable { | ||
|
||
private static final long serialVersionUID = 1; | ||
private final Throwable error; | ||
private transient FlowExecution execution; | ||
private final FlowExecutionOwner executionOwner; | ||
|
||
ErrorInfo(Throwable error, FlowExecution execution) { | ||
this.error = error; | ||
this.execution = execution; | ||
executionOwner = execution.getOwner(); | ||
} | ||
|
||
private FlowExecution getExecution() throws IOException { | ||
if (execution == null) { | ||
execution = executionOwner.get(); | ||
} | ||
return execution; | ||
} | ||
|
||
/** | ||
* Finds a node which threw this exception or one of its causes. | ||
* Note that {@link Throwable#equals} is just pointer equality, | ||
* which we cannot use since we may be loading deserialized exceptions, | ||
* so we compare by stack trace instead. | ||
*/ | ||
private @CheckForNull FlowNode getNode() throws IOException { | ||
final Set<String> stackTraces = new HashSet<>(); | ||
for (Throwable t = error; t != null; t = t.getCause()) { | ||
stackTraces.add(Functions.printThrowable(t)); | ||
} | ||
Predicate<FlowNode> threwException = new Predicate<FlowNode>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would inline this variable. Just make sure it is safe for lambda conversion when we switch to |
||
@Override | ||
public boolean apply(FlowNode input) { | ||
if (input instanceof BlockEndNode) { | ||
return false; | ||
} | ||
ErrorAction a = input.getAction(ErrorAction.class); | ||
return (a != null && stackTraces.contains(Functions.printThrowable(a.getError()))); | ||
} | ||
}; | ||
return new FlowScanner.DepthFirstScanner().findFirstMatch(getExecution(), threwException); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once the ForkScanner is complete, it should be far more efficient for this case, because:
Technically the completely optimal case here is probably to jump over blocks without an ErrorAction (if we can guarantee they're attached to all the BlockEndNodes where failures happen) AND to do a proper breadth-first search. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a PR downstream of an evolving API PR, it is best to use timestamped snapshots in the Maven dependency, rather than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance is not a significant concern for this PR, because this is code run just once per step execution, typically once per build, and from the CPS VM thread. Obviously if you have some system for indexing nodes using specific action classes, that would be helpful as you could go straight to those with I did not follow the discussion about |
||
} | ||
|
||
@Whitelisted | ||
public @Nonnull Throwable getError() { | ||
return error; | ||
} | ||
|
||
/** | ||
* Gets the stack trace of the error, or just the message in the case of {@link AbortException}. | ||
*/ | ||
@Whitelisted | ||
public @Nonnull String getStackTrace() { | ||
if (error instanceof AbortException) { | ||
return error.getMessage(); | ||
} else { | ||
return Functions.printThrowable(error); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the {@link Result} of the build if the error were uncaught. | ||
* @return typically {@link Result#FAILURE} but {@link FlowInterruptedException} may override | ||
*/ | ||
@Whitelisted | ||
public @Nonnull String getResult() { | ||
Result r; | ||
if (error instanceof FlowInterruptedException) { | ||
r = ((FlowInterruptedException) error).getResult(); | ||
} else { | ||
r = Result.FAILURE; | ||
} | ||
return r.toString(); | ||
} | ||
|
||
/** | ||
* Looks for the URL of the {@link LogAction} last printed before the node which broke. | ||
*/ | ||
@Whitelisted | ||
public @CheckForNull String getLogURL() throws IOException { | ||
FlowNode n = getNode(); | ||
if (n != null) { | ||
FlowNode logNode = new FlowScanner.LinearBlockHoppingScanner() | ||
.findFirstMatch(n, | ||
FlowScanner.MATCH_HAS_LOG); | ||
if (logNode != null) { | ||
String u = Jenkins.getActiveInstance().getRootUrl(); | ||
if (u == null) { | ||
u = "http://jenkins/"; // placeholder | ||
} | ||
return u + logNode.getUrl() + logNode.getAction(LogAction.class).getUrlName(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// TODO tail of log | ||
// TODO label | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what would this look like? (Searching for |
||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2016 CloudBees, 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 org.jenkinsci.plugins.workflow.steps; | ||
|
||
import hudson.AbortException; | ||
import hudson.model.Result; | ||
import java.net.URL; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep; | ||
import static org.junit.Assert.*; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.Rule; | ||
import org.junit.runners.model.Statement; | ||
import org.jvnet.hudson.test.BuildWatcher; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.RestartableJenkinsRule; | ||
|
||
public class ErrorInfoStepTest { | ||
|
||
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
@Rule public RestartableJenkinsRule s = new RestartableJenkinsRule(); | ||
|
||
@Issue("JENKINS-28119") | ||
@Test public void smokes() { | ||
s.addStep(new Statement() { | ||
@Override public void evaluate() throws Throwable { | ||
WorkflowJob p = s.j.jenkins.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition( | ||
"try {\n" + | ||
" parallel fine: {\n" + | ||
" semaphore 'fine'\n" + | ||
" }, broken: {\n" + | ||
" echo 'erroneous step'\n" + | ||
" semaphore 'breaking'\n" + | ||
" }\n" + | ||
"} catch (e) {\n" + | ||
" def info = errorInfo(e)\n" + | ||
" semaphore 'caught'\n" + | ||
" currentBuild.result = info.result\n" + | ||
" echo \"caught an instance of ${info.error.getClass()}\"\n" + | ||
" echo info.stackTrace\n" + | ||
" echo \"browse to: ${info.logURL}\"\n" + | ||
"}", true)); | ||
WorkflowRun b = p.scheduleBuild2(0).waitForStart(); | ||
SemaphoreStep.waitForStart("fine/1", b); | ||
SemaphoreStep.failure("breaking/1", new AbortException("oops")); | ||
SemaphoreStep.success("fine/1", null); | ||
SemaphoreStep.waitForStart("caught/1", null); | ||
} | ||
}); | ||
s.addStep(new Statement() { | ||
@Override public void evaluate() throws Throwable { | ||
SemaphoreStep.success("caught/1", null); | ||
WorkflowJob p = s.j.jenkins.getItemByFullName("p", WorkflowJob.class); | ||
WorkflowRun b = p.getBuildByNumber(1); | ||
s.j.assertBuildStatus(Result.FAILURE, s.j.waitForCompletion(b)); | ||
s.j.waitForMessage("End of Pipeline", b); // TODO why does it sometimes cut off at "Resuming build"? probably because WorkflowRun.finish sets isBuilding() → false before flushing the log | ||
s.j.assertLogContains("caught an instance of class hudson.AbortException", b); | ||
s.j.assertLogContains("oops", b); | ||
s.j.assertLogNotContains("\tat ", b); | ||
String log = JenkinsRule.getLog(b); | ||
Matcher matcher = Pattern.compile("^browse to: (http.+)$", Pattern.MULTILINE).matcher(log); | ||
assertTrue(log, matcher.find()); | ||
String text = s.j.createWebClient().getPage(new URL(matcher.group(1))).getWebResponse().getContentAsString(); | ||
assertTrue(text, text.contains("erroneous step")); | ||
} | ||
}); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW this can now be switched to simply
2.2
.