-
Notifications
You must be signed in to change notification settings - Fork 31
/
StageStepExecution.java
373 lines (343 loc) · 15 KB
/
StageStepExecution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package org.jenkinsci.plugins.workflow.support.steps;
import com.google.inject.Inject;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.XmlFile;
import hudson.model.InvisibleAction;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import jenkins.model.CauseOfInterruption;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.actions.LabelAction;
import org.jenkinsci.plugins.workflow.actions.StageAction;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
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.FlowStartNode;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.support.steps.stage.Messages;
public class StageStepExecution extends AbstractStepExecutionImpl {
private static final Logger LOGGER = Logger.getLogger(StageStepExecution.class.getName());
// only used during the start() call, so no need to be persisted
@Inject(optional=true) private transient StageStep step;
@StepContextParameter private transient Run<?,?> run;
@StepContextParameter private transient FlowNode node;
private static final class StageActionImpl extends InvisibleAction implements StageAction {
private final String stageName;
StageActionImpl(String stageName) {
this.stageName = stageName;
}
@Override public String getStageName() {
return stageName;
}
}
@Override
public boolean start() throws Exception {
if (getContext().hasBody()) { // recommended mode
if (step.concurrency != null) {
throw new AbortException(Messages.StageStepExecution_concurrency_not_supported_in_block_mode());
}
getContext().newBodyInvoker()
.withContexts(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class),
new ExpanderImpl(step.name)))
.withCallback(BodyExecutionCallback.wrap(getContext()))
.withDisplayName(step.name)
.start();
return false;
}
getContext().get(TaskListener.class).getLogger().println(Messages.StageStepExecution_non_block_mode_deprecated());
if (isInsideParallel(node)) {
throw new AbortException(Messages.StageStepExecution_the_stage_step_must_not_be_used_inside_a());
}
node.addAction(new LabelAction(step.name));
node.addAction(new StageActionImpl(step.name));
enter(run, getContext(), step.name, step.concurrency);
return false; // execute asynchronously
}
// TODO switch to FlowNodeSerialWalker or equivalent from https://github.com/jenkinsci/workflow-api-plugin/pull/2
private static boolean isInsideParallel(FlowNode n) {
while (true) {
if (n instanceof BlockEndNode) {
n = ((BlockEndNode) n).getStartNode();
}
if (n.getAction(ThreadNameAction.class) != null) {
return true;
}
List<FlowNode> parents = n.getParents();
if (parents.isEmpty()) {
assert n instanceof FlowStartNode;
return false;
}
assert parents.size() == 1;
n = parents.get(0);
}
}
@Override
public void stop(Throwable cause) throws Exception {
// TODO
throw new UnsupportedOperationException();
}
private static XmlFile getConfigFile() throws IOException {
Jenkins j = Jenkins.getInstance();
if (j == null) {
throw new IOException("Jenkins is not running"); // do not use Jenkins.getActiveInstance() as that is an ISE
}
return new XmlFile(new File(j.getRootDir(), StageStep.class.getName() + ".xml"));
}
// TODO can this be replaced with StepExecutionIterator?
private static Map<String,Map<String,Stage>> stagesByNameByJob;
// TODO or delete and make this an instance field in DescriptorImpl
public static void clear() {
stagesByNameByJob = null;
}
@SuppressWarnings("unchecked")
private static synchronized void load() {
if (stagesByNameByJob == null) {
stagesByNameByJob = new TreeMap<String,Map<String,Stage>>();
try {
XmlFile configFile = getConfigFile();
if (configFile.exists()) {
stagesByNameByJob = (Map<String,Map<String,Stage>>) configFile.read();
}
} catch (IOException x) {
LOGGER.log(WARNING, null, x);
}
LOGGER.log(Level.FINE, "load: {0}", stagesByNameByJob);
}
}
private static synchronized void save() {
try {
getConfigFile().write(stagesByNameByJob);
} catch (IOException x) {
LOGGER.log(WARNING, null, x);
}
LOGGER.log(Level.FINE, "save: {0}", stagesByNameByJob);
}
private static synchronized void enter(Run<?,?> r, StepContext context, String name, Integer concurrency) {
LOGGER.log(Level.FINE, "enter {0} {1}", new Object[] {r, name});
println(context, "Entering stage " + name);
load();
Job<?,?> job = r.getParent();
String jobName = job.getFullName();
Map<String,Stage> stagesByName = stagesByNameByJob.get(jobName);
if (stagesByName == null) {
stagesByName = new TreeMap<String,Stage>();
stagesByNameByJob.put(jobName, stagesByName);
}
Stage stage = stagesByName.get(name);
if (stage == null) {
stage = new Stage();
stagesByName.put(name, stage);
}
stage.concurrency = concurrency;
int build = r.number;
if (stage.waitingContext != null) {
// Someone has got to give up.
if (stage.waitingBuild < build) {
// Cancel the older one.
try {
cancel(stage.waitingContext, context);
} catch (Exception x) {
LOGGER.log(WARNING, "could not cancel an older flow (perhaps since deleted?)", x);
}
} else if (stage.waitingBuild > build) {
// Cancel this one. And work with the older one below, instead of the one initiating this call.
try {
cancel(context, stage.waitingContext);
} catch (Exception x) {
LOGGER.log(WARNING, "could not cancel the current flow", x);
}
build = stage.waitingBuild;
context = stage.waitingContext;
} else {
throw new IllegalStateException("the same flow is trying to reënter the stage " + name); // see 'e' with two dots, that's Jesse Glick for you! - KK
}
}
for (Map.Entry<String,Stage> entry : stagesByName.entrySet()) {
if (entry.getKey().equals(name)) {
continue;
}
Stage stage2 = entry.getValue();
// If we were holding another stage in the same job, release it, unlocking its waiter to proceed.
if (stage2.holding.remove(build)) {
if (stage2.waitingContext != null) {
stage2.unblock("Unblocked since " + r.getDisplayName() + " is moving into stage " + name);
}
}
}
stage.waitingBuild = build;
stage.waitingContext = context;
if (stage.concurrency == null || stage.holding.size() < stage.concurrency) {
stage.unblock("Proceeding");
} else {
println(context, "Waiting for builds " + stage.holding);
}
cleanUp(job, jobName);
save();
}
private static synchronized void exit(Run<?,?> r) {
load();
LOGGER.log(Level.FINE, "exit {0}: {1}", new Object[] {r, stagesByNameByJob});
Job<?,?> job = r.getParent();
String jobName = job.getFullName();
Map<String,Stage> stagesByName = stagesByNameByJob.get(jobName);
if (stagesByName == null) {
return;
}
boolean modified = false;
for (Stage stage : stagesByName.values()) {
if (stage.holding.contains(r.number)) {
stage.holding.remove(r.number); // XSTR-757: do not rely on return value of TreeSet.remove(Object)
modified = true;
if (stage.waitingContext != null) {
stage.unblock("Unblocked since " + r.getDisplayName() + " finished");
}
}
}
if (modified) {
cleanUp(job, jobName);
save();
}
}
private static void cleanUp(Job<?,?> job, String jobName) {
Map<String,Stage> stagesByName = stagesByNameByJob.get(jobName);
assert stagesByName != null;
Iterator<Entry<String,Stage>> it = stagesByName.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String,Stage> entry = it.next();
Set<Integer> holding = entry.getValue().holding;
Iterator<Integer> it2 = holding.iterator();
while (it2.hasNext()) {
Integer number = it2.next();
if (job.getBuildByNumber(number) == null) {
// Deleted at some point but did not properly clean up from exit(…).
LOGGER.log(WARNING, "Cleaning up apparently deleted {0}#{1}", new Object[] {jobName, number});
it2.remove();
}
}
if (holding.isEmpty()) {
assert entry.getValue().waitingContext == null : entry;
it.remove();
}
}
if (stagesByName.isEmpty()) {
stagesByNameByJob.remove(jobName);
}
}
private static void println(StepContext context, String message) {
if (!context.isReady()) {
LOGGER.log(Level.FINE, "cannot print message ‘{0}’ to dead {1}", new Object[] {message, context});
return;
}
try {
context.get(TaskListener.class).getLogger().println(message);
} catch (Exception x) {
LOGGER.log(WARNING, "failed to print message to dead " + context, x);
}
}
// TODO record the stage it got to and display that
private static void cancel(StepContext context, StepContext newer) throws IOException, InterruptedException {
if (context.isReady() && newer.isReady()) {
println(context, "Canceled since " + newer.get(Run.class).getDisplayName() + " got here");
println(newer, "Canceling older " + context.get(Run.class).getDisplayName());
context.onFailure(new FlowInterruptedException(Result.NOT_BUILT, new CanceledCause(newer.get(Run.class))));
} else {
LOGGER.log(WARNING, "cannot cancel dead {0} or {1}", new Object[] {context, newer});
}
}
/**
* Records that a flow was canceled while waiting in a stage step because a newer flow entered that stage instead.
*/
public static final class CanceledCause extends CauseOfInterruption {
private static final long serialVersionUID = 1;
private final String newerBuild;
CanceledCause(Run<?,?> newerBuild) {
this.newerBuild = newerBuild.getExternalizableId();
}
public Run<?,?> getNewerBuild() {
return Run.fromExternalizableId(newerBuild);
}
@Override public String getShortDescription() {
return "Superseded by " + getNewerBuild().getDisplayName();
}
}
private static final class Stage {
/** Numbers of builds currently running in this stage. */
final Set<Integer> holding = new TreeSet<Integer>();
/** Maximum permitted size of {@link #holding}, or null for unbounded. */
@CheckForNull
Integer concurrency;
/** Context of the build currently waiting to enter this stage, if any. */
@CheckForNull StepContext waitingContext;
/** Number of the build corresponding to {@link #waitingContext}, if any. */
@Nullable
Integer waitingBuild;
@Override public String toString() {
return "Stage[holding=" + holding + ",waitingBuild=" + waitingBuild + ",concurrency=" + concurrency + "]";
}
/**
* Unblocks the build currently waiting.
* @param message a message to print to the log of the unblocked build
*/
void unblock(String message) {
assert Thread.holdsLock(StageStepExecution.class);
assert waitingContext != null : message;
assert waitingBuild != null : message;
if (holding.contains(waitingBuild)) {
LOGGER.log(WARNING, "{0}: {1} already in {2}", new Object[] {message, waitingBuild, holding});
}
/* Not necessarily true, since a later build could reduce the concurrency of an existing stage; could perhaps adjust semantics to skip unblocking in this special case:
assert concurrency == null || holding.size() < concurrency;
*/
println(waitingContext, message);
waitingContext.onSuccess(null);
holding.add(waitingBuild);
waitingContext = null;
waitingBuild = null;
}
}
private static final class ExpanderImpl extends EnvironmentExpander {
private static final long serialVersionUID = 1;
private final String stageName;
private ExpanderImpl(String stageName) {
this.stageName = stageName;
}
@Override public void expand(EnvVars env) throws IOException, InterruptedException {
env.override("STAGE_NAME", stageName);
}
}
@Extension
public static final class Listener extends RunListener<Run<?,?>> {
@Override public void onCompleted(Run<?,?> r, TaskListener listener) {
if (!(r instanceof FlowExecutionOwner.Executable) || ((FlowExecutionOwner.Executable) r).asFlowExecutionOwner() == null) {
return;
}
exit(r);
}
}
private static final long serialVersionUID = 1L;
}