/
CpsFlowExecution.java
1319 lines (1192 loc) · 52.5 KB
/
CpsFlowExecution.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License
*
* Copyright (c) 2013-2014, 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.cps;
import com.cloudbees.groovy.cps.Continuable;
import com.cloudbees.groovy.cps.Env;
import com.cloudbees.groovy.cps.Envs;
import com.cloudbees.groovy.cps.Outcome;
import com.cloudbees.groovy.cps.impl.ConstantBlock;
import com.cloudbees.groovy.cps.impl.ThrowBlock;
import com.cloudbees.groovy.cps.sandbox.DefaultInvoker;
import com.cloudbees.groovy.cps.sandbox.SandboxInvoker;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import groovy.lang.GroovyShell;
import hudson.model.Action;
import hudson.model.Result;
import hudson.util.Iterators;
import jenkins.model.CauseOfInterruption;
import jenkins.model.Jenkins;
import org.jboss.marshalling.Unmarshaller;
import org.jenkinsci.plugins.workflow.actions.ErrorAction;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.flow.GraphListener;
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
import org.jenkinsci.plugins.workflow.graph.BlockStartNode;
import org.jenkinsci.plugins.workflow.graph.FlowEndNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graph.FlowStartNode;
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.support.concurrent.Futures;
import org.jenkinsci.plugins.workflow.support.pickles.serialization.PickleResolver;
import org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverReader;
import org.jenkinsci.plugins.workflow.support.storage.FlowNodeStorage;
import org.jenkinsci.plugins.workflow.support.storage.SimpleXStreamFlowNodeStorage;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Stack;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper.*;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.init.Terminator;
import hudson.model.Item;
import hudson.model.Queue;
import hudson.model.Saveable;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import java.beans.Introspector;
import java.lang.ref.Reference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.GuardedBy;
import jenkins.security.NotReallyRoleSensitiveCallable;
import org.acegisecurity.Authentication;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.jboss.marshalling.reflect.SerializableClassRegistry;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionList;
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker;
import org.kohsuke.accmod.restrictions.DoNotUse;
/**
* {@link FlowExecution} implemented with Groovy CPS.
*
* <h2>State Transition</h2>
* <p>
* {@link CpsFlowExecution} goes through the following states:
*
* <pre>{@code
* +----------------------+
* | |
* v |
* PERSISTED --> PREPARING --> SUSPENDED --> RUNNABLE --> RUNNING --> COMPLETE
* ^
* |
* INITIAL
* }</pre>
*
* <dl>
* <dt>INITIAL</dt>
* <dd>
* When a new {@link CpsFlowExecution} is created, it starts from here.
*
* When {@link #start()} method is called, we get one thread scheduled, and we arrive at RUNNABLE state.
* </dd>
* <dt>PERSISTED</dt>
* <dd>
* {@link CpsFlowExecution} is on disk with its owner, for example in <tt>build.xml</tt> of the workflow run.
* Nothing exists in memory. For example, Jenkins is not running.
*
* Transition from this into PREPARING is triggered outside our control by XStream using
* {@link ConverterImpl} to unmarshal {@link CpsFlowExecution}. {@link #onLoad()} is called
* at the end, and we arrive at the PREPARING state.
* </dd>
* <dt>PREPARING</dt>
* <dd>
* {@link CpsFlowExecution} is in memory, but {@link CpsThreadGroup} isn't. We are trying to
* restore all the ephemeral pickles that are necessary to get workflow going again.
* {@link #programPromise} represents a promise of completing this state.
*
* {@link PickleResolver} keeps track of this, and when it's all done, we arrive at SUSPENDED state.
* </dd>
* <dt>SUSPENDED</dt>
* <dd>
* {@link CpsThreadGroup} is in memory, but all {@link CpsThread}s are {@linkplain CpsThread#isRunnable() not runnable},
* which means they are waiting for some conditions to trigger (such as a completion of a shell script that's executing,
* human approval, etc). {@link CpsFlowExecution} and {@link CpsThreadGroup} are safe to persist.
*
* When a condition is met, {@link CpsThread#resume(Outcome)} is called, and that thread becomes runnable,
* and we move to the RUNNABLE state.
* </dd>
* <dt>RUNNABLE</dt>
* <dd>
* Some of {@link CpsThread}s are runnable, but we aren't actually running. The conditions that triggered
* {@link CpsThread} is captured in {@link CpsThread#resumeValue}.
* As we get into this state, {@link CpsThreadGroup#scheduleRun()}
* should be called to schedule the execution.
* {@link CpsFlowExecution} and {@link CpsThreadGroup} are safe to persist in this state, just like in the SUSPENDED state.
*
* When {@link CpsThreadGroup#runner} allocated a real Java thread to the execution, we move to the RUNNING state.
* </dd>
* <dt>RUNNING</dt>
* <dd>
* A thread is inside {@link CpsThreadGroup#run()} and is actively mutating the object graph inside the script.
* This state continues until no threads are runnable any more.
* Only one thread executes {@link CpsThreadGroup#run()}.
*
* In this state, {@link CpsFlowExecution} still need to be persistable (because generally we don't get to
* control when it is persisted), but {@link CpsThreadGroup} isn't safe to persist.
*
* When the Java thread leaves {@link CpsThreadGroup#run()}, we move to the SUSPENDED state.
* </dd>
* <dt>COMPLETE</dt>
* <dd>
* All the {@link CpsThread}s have terminated and there's nothing more to execute, and there's no more events to wait.
* The result is finalized and there's no further state change.
* </dd>
* </dl>
*
* @author Kohsuke Kawaguchi
*/
@PersistIn(RUN)
public class CpsFlowExecution extends FlowExecution {
/**
* Groovy script of the main source file (that the user enters in the GUI)
*/
private final String script;
/**
* Any additional scripts {@linkplain CpsGroovyShell#parse(GroovyCodeSource) parsed} afterward, keyed by
* their FQCN.
*/
/*package*/ /*final*/ Map<String,String> loadedScripts = new HashMap<String, String>();
private final boolean sandbox;
private transient /*almost final*/ FlowExecutionOwner owner;
/**
* Loading of the program is asynchronous because it requires us to re-obtain stateful objects.
* This object represents a {@link Future} for filling in {@link CpsThreadGroup}.
*
* TODO: provide a mechanism to diagnose how far along this process is.
*
* @see #runInCpsVmThread(FutureCallback)
*/
public transient volatile ListenableFuture<CpsThreadGroup> programPromise;
private transient volatile Collection<ListenableFuture<?>> pickleFutures;
/**
* Recreated from {@link #owner}
*/
/*package*/ transient /*almos final*/ FlowNodeStorage storage;
/** User ID associated with this build, or null if none specific. */
private final @CheckForNull String user;
/**
* Start nodes that have been created, whose {@link BlockEndNode} is not yet created.
*/
@GuardedBy("this")
/*package*/ /* almost final*/ Stack<BlockStartNode> startNodes = new Stack<BlockStartNode>();
@SuppressFBWarnings({"IS_FIELD_NOT_GUARDED", "IS2_INCONSISTENT_SYNC"}) // irrelevant here
private transient List<String> startNodesSerial; // used only between unmarshal and onLoad
@GuardedBy("this")
private /* almost final*/ NavigableMap<Integer,FlowHead> heads = new TreeMap<Integer,FlowHead>();
@SuppressFBWarnings({"IS_FIELD_NOT_GUARDED", "IS2_INCONSISTENT_SYNC"}) // irrelevant here
private transient Map<Integer,String> headsSerial; // used only between unmarshal and onLoad
private final AtomicInteger iota = new AtomicInteger();
/** Number of node IDs to use in lookup table, 500 covers most common flow graphs */
private static final int ID_LOOKUP_TABLE_SIZE = 500;
/** Preallocated lookup table for small ID values, used instead of interning for speed & simplicity */
private static final String[] ID_LOOKUP_TABLE = new String[ID_LOOKUP_TABLE_SIZE];
static {
for(int i = 0; i< ID_LOOKUP_TABLE.length; i++) {
ID_LOOKUP_TABLE[i] = String.valueOf(i).intern(); // Interning here allows allows us to just intern on deserialize
}
}
private transient List<GraphListener> listeners;
/**
* Result set from {@link StepContext}. Start by success and progressively gets worse.
*/
private Result result = Result.SUCCESS;
/**
* When the program is completed, set to true.
*
* {@link FlowExecution} gets loaded into memory for the build records that have been completed,
* and for those we don't want to load the program state, so that check should be efficient.
*/
private boolean done;
/**
* Groovy compiler with CPS+sandbox transformation correctly setup.
* By the time the script starts running, this field is set to non-null.
* It is reset to null after completion.
*/
private transient CpsGroovyShell shell;
/**
* Groovy compiler wih CPS transformation but not sandbox.
* Used by plugins to insert code that runs outside sandbox.
*
* By the time the script starts running, this field is set to non-null.
* It is reset to null after completion.
*/
private transient CpsGroovyShell trusted;
/** Class of the {@link CpsScript}; its loader is a {@link groovy.lang.GroovyClassLoader.InnerLoader}, not the same as {@code shell.getClassLoader()}. */
private transient Class<?> scriptClass;
/** Actions to add to the {@link FlowStartNode}. */
transient final List<Action> flowStartNodeActions = new ArrayList<Action>();
@Deprecated
public CpsFlowExecution(String script, FlowExecutionOwner owner) throws IOException {
this(script, false, owner);
}
public CpsFlowExecution(String script, boolean sandbox, FlowExecutionOwner owner) throws IOException {
this.owner = owner;
this.script = script;
this.sandbox = sandbox;
this.storage = createStorage();
Authentication auth = Jenkins.getAuthentication();
this.user = auth.equals(ACL.SYSTEM) ? null : auth.getName();
}
/**
* Perform post-deserialization state resurrection that handles version evolution
*/
private Object readResolve() {
if (loadedScripts==null)
loadedScripts = new HashMap<String,String>(); // field added later
return this;
}
/**
* Returns a groovy compiler used to load the script.
*
* @see "doc/classloader.md"
* @see GroovyShell#getClassLoader()
*/
public GroovyShell getShell() {
return shell;
}
/**
* Returns a groovy compiler used to load the trusted script.
*
* @see "doc/classloader.md"
*/
public GroovyShell getTrustedShell() {
return trusted;
}
public FlowNodeStorage getStorage() {
return storage;
}
public String getScript() {
return script;
}
public Map<String,String> getLoadedScripts() {
return ImmutableMap.copyOf(loadedScripts);
}
/**
* True if executing with groovy-sandbox, false if executing with approval.
*/
public boolean isSandbox() {
return sandbox;
}
@Override
public FlowExecutionOwner getOwner() {
return owner;
}
private SimpleXStreamFlowNodeStorage createStorage() throws IOException {
return new SimpleXStreamFlowNodeStorage(this, getStorageDir());
}
/**
* Directory where workflow stores its state.
*/
public File getStorageDir() throws IOException {
return new File(this.owner.getRootDir(),"workflow");
}
@Override
public void start() throws IOException {
final CpsScript s = parseScript();
scriptClass = s.getClass();
s.$initialize();
final FlowHead h = new FlowHead(this);
synchronized (this) {
heads.put(h.getId(), h);
}
h.newStartNode(new FlowStartNode(this, iotaStr()));
final CpsThreadGroup g = new CpsThreadGroup(this);
g.register(s);
final SettableFuture<CpsThreadGroup> f = SettableFuture.create();
programPromise = f;
g.runner.submit(new Runnable() {
@Override
public void run() {
CpsThread t = g.addThread(new Continuable(s,createInitialEnv()),h,null);
t.resume(new Outcome(null, null));
f.set(g);
}
/**
* Environment to start executing the script in.
* During sandbox execution, we need to call sandbox interceptor while executing asynchronous code.
*/
private Env createInitialEnv() {
return Envs.empty( isSandbox() ? new SandboxInvoker() : new DefaultInvoker());
}
});
}
private CpsScript parseScript() throws IOException {
// classloader hierarchy. See doc/classloader.md
trusted = new CpsGroovyShellFactory(this).forTrusted().build();
shell = new CpsGroovyShellFactory(this).withParent(trusted).build();
CpsScript s = (CpsScript) shell.reparse("WorkflowScript",script);
for (Entry<String, String> e : loadedScripts.entrySet()) {
shell.reparse(e.getKey(), e.getValue());
}
s.execution = this;
if (false) {
System.out.println("scriptName="+s.getClass().getName());
System.out.println(Arrays.asList(s.getClass().getInterfaces()));
System.out.println(Arrays.asList(s.getClass().getDeclaredFields()));
System.out.println(Arrays.asList(s.getClass().getDeclaredMethods()));
}
return s;
}
/**
* Assigns a new ID.
*/
@Restricted(NoExternalUse.class)
public String iotaStr() {
int iotaVal = iota();
// We intern this because many, many FlowNodes will have the same ID values
if ( iotaVal > 0 && iotaVal < ID_LOOKUP_TABLE_SIZE) {
return ID_LOOKUP_TABLE[iotaVal];
} else {
return String.valueOf(iota()).intern();
}
}
@Restricted(NoExternalUse.class)
public int iota() {
return iota.incrementAndGet();
}
/**
* Returns an approximate size of the flow graph, based on the heuristic that the iota is incremented once per new node.
* The exact count may be a little different due to special cases.
* ({@link FlowNodeStorage} does not currently offer a size, or a set of all nodes.
* An exact count could be obtained with {@link FlowGraphWalker}, but this could be more overhead.)
*/
int approximateNodeCount() {
return iota.get();
}
protected void initializeStorage() throws IOException {
storage = createStorage();
synchronized (this) {
// heads could not be restored in unmarshal, so doing that now:
heads = new TreeMap<Integer,FlowHead>();
for (Map.Entry<Integer,String> entry : headsSerial.entrySet()) {
FlowHead h = new FlowHead(this, entry.getKey());
h.setForDeserialize(storage.getNode(entry.getValue()));
heads.put(h.getId(), h);
}
headsSerial = null;
// Same for startNodes:
startNodes = new Stack<BlockStartNode>();
for (String id : startNodesSerial) {
startNodes.add((BlockStartNode) storage.getNode(id));
}
startNodesSerial = null;
}
}
@Override
public void onLoad(FlowExecutionOwner owner) throws IOException {
this.owner = owner;
try {
initializeStorage();
try {
if (!isComplete()) {
loadProgramAsync(getProgramDataFile());
}
} catch (IOException e) {
SettableFuture<CpsThreadGroup> p = SettableFuture.create();
programPromise = p;
loadProgramFailed(e, p);
}
} finally {
if (programPromise == null) {
programPromise = Futures.immediateFailedFuture(new IllegalStateException("completed or broken execution"));
}
}
}
/**
* Deserializes {@link CpsThreadGroup} from {@link #getProgramDataFile()} if necessary.
*
* This moves us into the PREPARING state.
* @param programDataFile
*/
public void loadProgramAsync(File programDataFile) {
final SettableFuture<CpsThreadGroup> result = SettableFuture.create();
programPromise = result;
try {
scriptClass = parseScript().getClass();
final RiverReader r = new RiverReader(programDataFile, scriptClass.getClassLoader(), owner);
Futures.addCallback(
r.restorePickles(pickleFutures = new ArrayList<>()),
new FutureCallback<Unmarshaller>() {
public void onSuccess(Unmarshaller u) {
pickleFutures = null;
try {
CpsFlowExecution old = PROGRAM_STATE_SERIALIZATION.get();
PROGRAM_STATE_SERIALIZATION.set(CpsFlowExecution.this);
try {
CpsThreadGroup g = (CpsThreadGroup) u.readObject();
result.set(g);
try {
if (g.isPaused()) {
owner.getListener().getLogger().println("Still paused");
} else {
owner.getListener().getLogger().println("Ready to run at " + new Date());
// In case we last paused execution due to Jenkins.isQuietingDown, make sure we do something after we restart.
g.scheduleRun();
}
} catch (IOException x) {
LOGGER.log(Level.WARNING, null, x);
}
} catch (Throwable t) {
onFailure(t);
} finally {
PROGRAM_STATE_SERIALIZATION.set(old);
}
} finally {
r.close();
}
}
public void onFailure(Throwable t) {
// Note: not calling result.setException(t) since loadProgramFailed in fact sets a result
try {
loadProgramFailed(t, result);
} finally {
r.close();
}
}
});
} catch (IOException e) {
loadProgramFailed(e, result);
}
}
/**
* Used by {@link #loadProgramAsync(File)} to propagate a failure to load the persisted execution state.
* <p>
* Let the workflow interrupt by throwing an exception that indicates how it failed.
* @param promise same as {@link #programPromise} but more strongly typed
*/
private void loadProgramFailed(final Throwable problem, SettableFuture<CpsThreadGroup> promise) {
FlowHead head;
synchronized(this) {
if (heads.isEmpty())
head = null;
else
head = getFirstHead();
}
if (head==null) {
// something went catastrophically wrong and there's no live head. fake one
head = new FlowHead(this);
try {
head.newStartNode(new FlowStartNode(this, iotaStr()));
} catch (IOException e) {
LOGGER.log(Level.FINE, "Failed to persist", e);
}
}
CpsThreadGroup g = new CpsThreadGroup(this);
final FlowHead head_ = head;
promise.set(g);
runInCpsVmThread(new FutureCallback<CpsThreadGroup>() {
@Override public void onSuccess(CpsThreadGroup g) {
CpsThread t = g.addThread(
new Continuable(new ThrowBlock(new ConstantBlock(
problem instanceof AbortException ? problem : new IOException("Failed to load build state", problem)))),
head_, null
);
t.resume(new Outcome(null,null));
}
@Override public void onFailure(Throwable t) {
LOGGER.log(Level.WARNING, "Failed to set program failure on " + owner, t);
onProgramEnd(new Outcome(null, t));
cleanUpHeap();
}
});
}
/**
* Where we store {@link CpsThreadGroup}.
*/
/*package*/ File getProgramDataFile() throws IOException {
return new File(owner.getRootDir(), "program.dat");
}
/**
* Execute a task in {@link CpsVmExecutorService} to safely access {@link CpsThreadGroup} internal states.
*
* <p>
* If the {@link CpsThreadGroup} deserializatoin fails, {@link FutureCallback#onFailure(Throwable)} will
* be invoked (on a random thread, since CpsVmThread doesn't exist without a valid program.)
*/
void runInCpsVmThread(final FutureCallback<CpsThreadGroup> callback) {
if (programPromise == null) {
throw new IllegalStateException("build storage unloadable, or build already finished");
}
// first we need to wait for programPromise to fullfil CpsThreadGroup, then we need to run in its runner, phew!
Futures.addCallback(programPromise, new FutureCallback<CpsThreadGroup>() {
final Exception source = new Exception(); // call stack of this object captures who called this. useful during debugging.
@Override
public void onSuccess(final CpsThreadGroup g) {
g.runner.submit(new Runnable() {
@Override
public void run() {
callback.onSuccess(g);
}
});
}
/**
* Program state failed to load.
*/
@Override
public void onFailure(Throwable t) {
callback.onFailure(t);
}
});
}
@Override public boolean blocksRestart() {
if (programPromise == null || !programPromise.isDone()) {
return true;
}
CpsThreadGroup g;
try {
g = programPromise.get();
} catch (Exception x) {
return true;
}
return g.busy;
}
/**
* Waits for the workflow to move into the SUSPENDED state.
*/
public void waitForSuspension() throws InterruptedException, ExecutionException {
if (programPromise==null)
return; // the execution has already finished and we are not loading program state anymore
CpsThreadGroup g = programPromise.get();
// TODO occasionally tests fail here with RejectedExecutionException, apparently because the runner has been shut down; should we just ignore that?
g.scheduleRun().get();
}
public synchronized FlowHead getFlowHead(int id) {
return heads.get(id);
}
@Override
public synchronized List<FlowNode> getCurrentHeads() {
List<FlowNode> r = new ArrayList<FlowNode>();
if (heads == null) {
LOGGER.log(Level.WARNING, "List of flow heads unset for {0}, perhaps due to broken storage", this);
return r;
}
for (FlowHead h : heads.values()) {
r.add(h.get());
}
return r;
}
@Override
public ListenableFuture<List<StepExecution>> getCurrentExecutions(final boolean innerMostOnly) {
if (programPromise == null || isComplete()) {
return Futures.immediateFuture(Collections.<StepExecution>emptyList());
}
final SettableFuture<List<StepExecution>> r = SettableFuture.create();
runInCpsVmThread(new FutureCallback<CpsThreadGroup>() {
@Override
public void onSuccess(CpsThreadGroup g) {
if (innerMostOnly) {
// to exclude outer StepExecutions, first build a map by FlowHead
// younger threads with their StepExecutions will overshadow old threads, leaving inner-most threads alone.
Map<FlowHead, StepExecution> m = new LinkedHashMap<FlowHead, StepExecution>();
for (CpsThread t : g.threads.values()) {
StepExecution e = t.getStep();
if (e != null) {
m.put(t.head, e);
}
}
r.set(ImmutableList.copyOf(m.values()));
} else {
List<StepExecution> es = new ArrayList<StepExecution>();
for (CpsThread t : g.threads.values()) {
StepExecution e = t.getStep();
if (e != null) {
es.add(e);
}
}
r.set(Collections.unmodifiableList(es));
}
}
@Override
public void onFailure(Throwable t) {
r.setException(t);
}
});
return r;
}
/**
* Synchronously obtain the current state of the workflow program.
*
* <p>
* The workflow can be already completed, or it can still be running.
*/
public CpsThreadDump getThreadDump() {
if (programPromise == null || isComplete()) {
return CpsThreadDump.EMPTY;
}
if (!programPromise.isDone()) {
// CpsThreadGroup state isn't ready yet, but this is probably one of the common cases
// when one wants to obtain the stack trace. Cf. JENKINS-26130.
Collection<ListenableFuture<?>> _pickleFutures = pickleFutures;
if (_pickleFutures != null) {
StringBuilder b = new StringBuilder("Program is not yet loaded");
for (ListenableFuture<?> pickleFuture : _pickleFutures) {
b.append("\n\t").append(pickleFuture);
if (pickleFuture.isCancelled()) {
b.append(" (cancelled)");
}
if (pickleFuture.isDone()) {
b.append(" (complete)");
}
}
return CpsThreadDump.fromText(b.toString());
} else {
return CpsThreadDump.fromText("Program state is unknown");
}
}
try {
return programPromise.get().getThreadDump();
} catch (InterruptedException e) {
throw new AssertionError(); // since we are checking programPromise.isDone() upfront
} catch (ExecutionException e) {
return CpsThreadDump.from(new Exception("Failed to resurrect program state",e));
}
}
@Override
public synchronized boolean isCurrentHead(FlowNode n) {
for (FlowHead h : heads.values()) {
if (h.get().equals(n))
return true;
}
return false;
}
/**
* Called by FlowHead to add a new head.
*
* The new head gets removed via {@link #subsumeHead(FlowNode)} when it's used as a parent
* of a FlowNode and thereby joining an another thread.
*/
//
synchronized void addHead(FlowHead h) {
heads.put(h.getId(), h);
}
synchronized void removeHead(FlowHead h) {
heads.remove(h.getId());
}
/**
* Removes a {@link FlowHead} that points to the given node from the 'current heads' list.
*
* This is used when a thread waits and collects the outcome of another thread.
*/
void subsumeHead(FlowNode n) {
List<FlowHead> _heads;
synchronized (this) {
_heads = new ArrayList<FlowHead>(heads.values());
}
for (FlowHead h : _heads) {
if (h.get()==n) {
h.remove();
return;
}
}
}
@Override
public void addListener(GraphListener listener) {
if (listeners == null) {
listeners = new CopyOnWriteArrayList<GraphListener>();
}
listeners.add(listener);
}
@Override public void removeListener(GraphListener listener) {
if (listeners != null) {
listeners.remove(listener);
}
}
@Override
public void interrupt(Result result, CauseOfInterruption... causes) throws IOException, InterruptedException {
setResult(result);
LOGGER.log(Level.FINE, "Interrupting {0} as {1}", new Object[] {owner, result});
final FlowInterruptedException ex = new FlowInterruptedException(result,causes);
// stop all ongoing activities
runInCpsVmThread(new FutureCallback<CpsThreadGroup>() {
@Override
public void onSuccess(CpsThreadGroup g) {
// don't touch outer ones. See JENKINS-26148
Map<FlowHead, CpsThread> m = new LinkedHashMap<>();
for (CpsThread t : g.threads.values()) {
m.put(t.head, t);
}
// for each inner most CpsThread, from young to old...
for (CpsThread t : Iterators.reverse(ImmutableList.copyOf(m.values()))) {
try {
t.stop(ex);
} catch (Exception x) {
LOGGER.log(Level.WARNING, "Failed to abort " + owner, x);
}
}
}
@Override
public void onFailure(Throwable t) {
LOGGER.log(Level.WARNING, "Failed to interrupt steps in " + owner, t);
}
});
// If we are still rehydrating pickles, try to stop that now.
Collection<ListenableFuture<?>> futures = pickleFutures;
if (futures != null) {
LOGGER.log(Level.FINE, "We are still rehydrating pickles in {0}", owner);
for (ListenableFuture<?> future : futures) {
if (!future.isDone()) {
LOGGER.log(Level.FINE, "Trying to cancel {0} for {1}", new Object[] {future, owner});
if (!future.cancel(true)) {
LOGGER.log(Level.WARNING, "Failed to cancel {0} for {1}", new Object[] {future, owner});
}
}
}
}
}
@Override
public FlowNode getNode(String id) throws IOException {
return storage.getNode(id);
}
public void setResult(Result v) {
result = result.combine(v);
}
public Result getResult() {
return result;
}
public List<Action> loadActions(FlowNode node) throws IOException {
return storage.loadActions(node);
}
public void saveActions(FlowNode node, List<Action> actions) throws IOException {
storage.saveActions(node, actions);
}
@Override
public boolean isComplete() {
return done || super.isComplete();
}
/*packgage*/ synchronized void onProgramEnd(Outcome outcome) {
// end of the program
// run till the end successfully FIXME: failure comes here, too
// TODO: if program terminates with exception, we need to record it
// TODO: in the error case, we have to close all the open nodes
FlowNode head = new FlowEndNode(this, iotaStr(), (FlowStartNode)startNodes.pop(), result, getCurrentHeads().toArray(new FlowNode[0]));
if (outcome.isFailure())
head.addAction(new ErrorAction(outcome.getAbnormal()));
// shrink everything into a single new head
done = true;
FlowHead first = getFirstHead();
first.setNewHead(head);
heads.clear();
heads.put(first.getId(),first);
}
void cleanUpHeap() {
shell = null;
trusted = null;
if (scriptClass != null) {
try {
cleanUpLoader(scriptClass.getClassLoader(), new HashSet<ClassLoader>(), new HashSet<Class<?>>());
} catch (Exception x) {
LOGGER.log(Level.WARNING, "failed to clean up memory from " + owner, x);
}
scriptClass = null;
}
// perhaps also set programPromise to null or a precompleted failure?
}
private static void cleanUpLoader(ClassLoader loader, Set<ClassLoader> encounteredLoaders, Set<Class<?>> encounteredClasses) throws Exception {
if (!(loader instanceof GroovyClassLoader)) {
return;
}
if (!encounteredLoaders.add(loader)) {
return;
}
cleanUpLoader(loader.getParent(), encounteredLoaders, encounteredClasses);
LOGGER.log(Level.FINER, "found {0}", String.valueOf(loader));
SerializableClassRegistry.getInstance().release(loader);
cleanUpGlobalClassValue(loader);
GroovyClassLoader gcl = (GroovyClassLoader) loader;
for (Class<?> clazz : gcl.getLoadedClasses()) {
if (encounteredClasses.add(clazz)) {
LOGGER.log(Level.FINER, "found {0}", clazz.getName());
Introspector.flushFromCaches(clazz);
cleanUpGlobalClassSet(clazz);
cleanUpObjectStreamClassCaches(clazz);
cleanUpLoader(clazz.getClassLoader(), encounteredLoaders, encounteredClasses);
}
}
gcl.clearCache();
}
private static void cleanUpGlobalClassValue(@Nonnull ClassLoader loader) throws Exception {
Class<?> classInfoC = Class.forName("org.codehaus.groovy.reflection.ClassInfo");
Field globalClassValueF;
try {
globalClassValueF = classInfoC.getDeclaredField("globalClassValue");
} catch (NoSuchFieldException x) {
return; // Groovy 1, fine
}
globalClassValueF.setAccessible(true);
Object globalClassValue = globalClassValueF.get(null);
Class<?> groovyClassValuePreJava7C = Class.forName("org.codehaus.groovy.reflection.GroovyClassValuePreJava7");
if (!groovyClassValuePreJava7C.isInstance(globalClassValue)) {
return; // using GroovyClassValueJava7 due to -Dgroovy.use.classvalue or on IBM J9, fine
}
Field mapF = groovyClassValuePreJava7C.getDeclaredField("map");
mapF.setAccessible(true);
Object map = mapF.get(globalClassValue);
Class<?> groovyClassValuePreJava7Map = Class.forName("org.codehaus.groovy.reflection.GroovyClassValuePreJava7$GroovyClassValuePreJava7Map");
Collection entries = (Collection) groovyClassValuePreJava7Map.getMethod("values").invoke(map);
Field klazzF = classInfoC.getDeclaredField("klazz");
klazzF.setAccessible(true);
Method removeM = groovyClassValuePreJava7Map.getMethod("remove", Object.class);
Class<?> entryC = Class.forName("org.codehaus.groovy.util.AbstractConcurrentMapBase$Entry");
Method getValueM = entryC.getMethod("getValue");
List<Class<?>> toRemove = new ArrayList<>(); // not sure if it is safe against ConcurrentModificationException or not
for (Object entry : entries) {
Object value = getValueM.invoke(entry);