Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from abayer/jenkins-41243
[JENKINS-41243] Slightly faster tagging of synthetic stages
- Loading branch information
Showing
9 changed files
with
233 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...main/java/org/jenkinsci/plugins/pipeline/modeldefinition/SyntheticStageGraphListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, 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.pipeline.modeldefinition; | ||
|
||
import hudson.model.InvisibleAction; | ||
import hudson.model.Run; | ||
import jenkins.model.RunAction2; | ||
import org.jenkinsci.plugins.pipeline.SyntheticStage; | ||
import org.jenkinsci.plugins.workflow.actions.LabelAction; | ||
import org.jenkinsci.plugins.workflow.actions.TagsAction; | ||
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecution; | ||
import org.jenkinsci.plugins.workflow.flow.GraphListener; | ||
import org.jenkinsci.plugins.workflow.graph.FlowNode; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.jenkinsci.plugins.workflow.support.steps.StageStep; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
import static java.util.logging.Level.WARNING; | ||
|
||
public final class SyntheticStageGraphListener implements GraphListener { | ||
private static final Logger LOGGER = Logger.getLogger(SyntheticStageGraphListener.class.getName()); | ||
|
||
@Override | ||
public void onNewHead(FlowNode node) { | ||
if (node != null && node instanceof StepStartNode && | ||
((StepStartNode) node).getDescriptor() instanceof StageStep.DescriptorImpl) { | ||
LabelAction label = node.getPersistentAction(LabelAction.class); | ||
if (label != null && | ||
(SyntheticStageNames.preStages().contains(label.getDisplayName()) || | ||
SyntheticStageNames.postStages().contains(label.getDisplayName()))) { | ||
if (SyntheticStageNames.preStages().contains(label.getDisplayName())) { | ||
attachTag(node, SyntheticStage.getPre()); | ||
} | ||
if (SyntheticStageNames.postStages().contains(label.getDisplayName())) { | ||
attachTag(node, SyntheticStage.getPost()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void attachTag(FlowNode currentNode, String syntheticContext) { | ||
TagsAction tagsAction = currentNode.getPersistentAction(TagsAction.class); | ||
if (tagsAction == null) { | ||
tagsAction = new TagsAction(); | ||
tagsAction.addTag(SyntheticStage.TAG_NAME, syntheticContext); | ||
currentNode.addAction(tagsAction); | ||
} else if (tagsAction.getTagValue(SyntheticStage.TAG_NAME) == null) { | ||
tagsAction.addTag(SyntheticStage.TAG_NAME, syntheticContext); | ||
try { | ||
currentNode.save(); | ||
} catch (IOException e) { | ||
LOGGER.log(WARNING, "failed to save actions for FlowNode id=" + currentNode.getId(), e); | ||
} | ||
} | ||
} | ||
|
||
public static class GraphListenerAction extends InvisibleAction implements RunAction2 { | ||
@Override | ||
public void onLoad(Run<?, ?> r) { | ||
if (r != null && r instanceof WorkflowRun) { | ||
WorkflowRun run = (WorkflowRun) r; | ||
attachListener(run); | ||
} | ||
} | ||
|
||
@Override | ||
public void onAttached(Run<?, ?> r) { | ||
if (r != null && r instanceof WorkflowRun) { | ||
WorkflowRun run = (WorkflowRun) r; | ||
attachListener(run); | ||
} | ||
} | ||
|
||
private void attachListener(WorkflowRun run) { | ||
if (run != null) { | ||
FlowExecution exec = run.getExecution(); | ||
if (exec != null && !exec.isComplete()) { | ||
exec.addListener(new SyntheticStageGraphListener()); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
.../java/org/jenkinsci/plugins/pipeline/modeldefinition/SyntheticStageGraphListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, 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.pipeline.modeldefinition; | ||
|
||
import org.jenkinsci.plugins.pipeline.SyntheticStage; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecution; | ||
import org.jenkinsci.plugins.workflow.graph.FlowNode; | ||
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runners.model.Statement; | ||
import org.jvnet.hudson.test.BuildWatcher; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.RestartableJenkinsRule; | ||
|
||
import java.util.Collection; | ||
|
||
import static org.jenkinsci.plugins.pipeline.modeldefinition.BasicModelDefTest.syntheticStagePredicate; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class SyntheticStageGraphListenerTest { | ||
|
||
@ClassRule | ||
public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
|
||
@Rule | ||
public RestartableJenkinsRule rr = new RestartableJenkinsRule(); | ||
|
||
@Issue("JENKINS-41243") | ||
@Test | ||
public void resume() throws Exception { | ||
rr.addStep(new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
WorkflowJob p = rr.j.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition("pipeline {\n" + | ||
" agent any\n" + | ||
" stages {\n" + | ||
" stage('x') {\n" + | ||
" steps {\n" + | ||
" script {\n" + | ||
" semaphore 'wait'\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
" post {\n" + | ||
" always {\n" + | ||
" echo 'Hi there'\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}", true)); | ||
WorkflowRun b = p.scheduleBuild2(0).waitForStart(); | ||
SemaphoreStep.waitForStart("wait/1", b); | ||
} | ||
}); | ||
rr.addStep(new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
WorkflowJob p = rr.j.jenkins.getItemByFullName("p", WorkflowJob.class); | ||
WorkflowRun b = p.getBuildByNumber(1); | ||
SemaphoreStep.success("wait/1", null); | ||
rr.j.assertBuildStatusSuccess(rr.j.waitForCompletion(b)); | ||
rr.j.assertLogContains("[Pipeline] { (" + SyntheticStageNames.postBuild() + ")", b); | ||
FlowExecution execution = b.getExecution(); | ||
|
||
Collection<FlowNode> heads = execution.getCurrentHeads(); | ||
|
||
DepthFirstScanner scanner = new DepthFirstScanner(); | ||
|
||
assertNotNull(scanner.findFirstMatch(heads, null, syntheticStagePredicate(SyntheticStageNames.postBuild(), SyntheticStage.getPost()))); | ||
SemaphoreStep.success("wait/2", null); | ||
rr.j.buildAndAssertSuccess(p); | ||
} | ||
}); | ||
} | ||
|
||
} |