Skip to content
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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>1.15</version>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>1.15</version>
<version>2.2-SNAPSHOT</version>
Copy link
Member

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.

<scope>test</scope>
</dependency>
<dependency>
Expand Down
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>() {
Copy link
Member

Choose a reason for hiding this comment

The 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 -source 8. Most likely this is satisfied by just verifying that there are no method overloads which vary only in the declared type of the SAM they accept. (This is where we screwed up with ACL.impersonate.)

@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);
Copy link
Member Author

@svanoort svanoort Apr 27, 2016

Choose a reason for hiding this comment

The 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:

  1. It doesn't have to keep a HashSet of all visited FlowNodes (just a stack with depth related to the number of levels of parallel branches).
  2. DepthFirstScanner will walk to the start of the flow, if the failure occurs in any branch branch of a parallel block beyond the first. ForkScanner will only walk the up to the start of the parallel block containing the failure (and then go through the branches there).

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DepthFirstScanner is a top-level type in the current upstream PR.

For a PR downstream of an evolving API PR, it is best to use timestamped snapshots in the Maven dependency, rather than 2.1-SNAPSHOT which is vague.

Copy link
Member

Choose a reason for hiding this comment

The 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 ErrorAction.

I did not follow the discussion about ForkScanner here. At any rate, for purposes of this PR we are looking for any FlowNode in the build with an ErrorAction, so long as it is not a BlockEndNode. Possibly we really want to check also BlockEndNodes, to handle errors thrown only during cleanup from build wrappers and the like, but give them a lower priority. The issue is that ErrorAction gets attached to end nodes of blocks which are exited by an uncaught exception, so we want to find the “first” node with the matching action: i.e., giving preference to the nested node. So it would also suffice to do a walk forward in time, rather than backward as I presume DepthFirstScanner does. Seems there is no such scanner yet?

}

@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what would this look like? (Searching for LabelAction and/or StageAction as per FlowNodeSerialWalker’s convenience API for labels.)


}

}
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"));
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.jvnet.hudson.test.RestartableJenkinsRule;

import java.util.List;
import org.jenkinsci.plugins.workflow.steps.SleepStep;
import org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution;

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -64,7 +62,7 @@ public void evaluate() throws Throwable {
+ " }\n"
+ " echo 'NotHere'\n"
+ "}\n"));
WorkflowRun b = story.j.assertBuildStatus(/* TODO JENKINS-25894 should really be ABORTED */Result.FAILURE, p.scheduleBuild2(0).get());
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());

// make sure things that are supposed to run do, and things that are NOT supposed to run do not.
story.j.assertLogNotContains("NotHere", b);
Expand Down