/
WorkUnitContext.java
165 lines (144 loc) · 5.87 KB
/
WorkUnitContext.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
/*
* The MIT License
*
* Copyright (c) 2010, InfraDNA, 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 hudson.model.queue;
import hudson.model.Action;
import hudson.model.Executor;
import hudson.model.Queue;
import hudson.model.Queue.BuildableItem;
import hudson.model.Queue.Task;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Holds the information shared between {@link WorkUnit}s created from the same {@link Task}.
*
* @author Kohsuke Kawaguchi
*/
public final class WorkUnitContext {
public final BuildableItem item;
public final Task task;
/**
* Once the execution starts and completes, update this future object with the outcome.
*/
public final FutureImpl future;
/**
* Associated parameters to the build.
*/
public final List<Action> actions;
private final Latch startLatch, endLatch;
private List<WorkUnit> workUnits = new ArrayList<WorkUnit>();
/**
* If the execution is aborted, set to non-null that indicates where it was aborted.
*/
private volatile Throwable aborted;
public WorkUnitContext(BuildableItem item) {
this.item = item;
this.task = item.task;
this.future = (FutureImpl)item.getFuture();
// JENKINS-51584 do not use item.getAllActions() here.
this.actions = new ArrayList<Action>(item.getActions());
// +1 for the main task
int workUnitSize = task.getSubTasks().size();
startLatch = new Latch(workUnitSize) {
@Override
protected void onCriteriaMet() {
// on behalf of the member Executors,
// the one that executes the main thing will send notifications
// Unclear if this will work with AsynchronousExecution; it *seems* this is only called from synchronize which is only called from synchronizeStart which is only called from an executor thread.
Executor e = Executor.currentExecutor();
if (e.getCurrentWorkUnit().isMainWork()) {
e.getOwner().taskAccepted(e,task);
}
}
};
endLatch = new Latch(workUnitSize);
}
/**
* Called within the queue maintenance process to create a {@link WorkUnit} for the given {@link SubTask}
*/
public WorkUnit createWorkUnit(SubTask execUnit) {
WorkUnit wu = new WorkUnit(this, execUnit);
workUnits.add(wu);
return wu;
}
public List<WorkUnit> getWorkUnits() {
return Collections.unmodifiableList(workUnits);
}
public WorkUnit getPrimaryWorkUnit() {
return workUnits.get(0);
}
/**
* All the {@link Executor}s that jointly execute a {@link Task} call this method to synchronize on the start.
*/
public void synchronizeStart() throws InterruptedException {
startLatch.synchronize();
// the main thread will send a notification
Executor e = Executor.currentExecutor();
WorkUnit wu = e.getCurrentWorkUnit();
if (wu.isMainWork()) {
future.start.set(e.getCurrentExecutable());
}
}
@Deprecated
public void synchronizeEnd(Queue.Executable executable, Throwable problems, long duration) throws InterruptedException {
synchronizeEnd(Executor.currentExecutor(), executable, problems, duration);
}
/**
* All the {@link Executor}s that jointly execute a {@link Task} call this method to synchronize on the end of the task.
*
* @throws InterruptedException
* If any of the member thread is interrupted while waiting for other threads to join, all
* the member threads will report {@link InterruptedException}.
*/
public void synchronizeEnd(Executor e, Queue.Executable executable, Throwable problems, long duration) throws InterruptedException {
endLatch.synchronize();
// the main thread will send a notification
WorkUnit wu = e.getCurrentWorkUnit();
if (wu.isMainWork()) {
if (problems == null) {
future.set(executable);
e.getOwner().taskCompleted(e, task, duration);
} else {
future.set(problems);
e.getOwner().taskCompletedWithProblems(e, task, duration, problems);
}
}
}
/**
* When one of the work unit is aborted, call this method to abort all the other work units.
*/
public synchronized void abort(Throwable cause) {
if (cause==null) throw new IllegalArgumentException();
if (aborted!=null) return; // already aborted
aborted = cause;
startLatch.abort(cause);
endLatch.abort(cause);
Thread c = Thread.currentThread();
for (WorkUnit wu : workUnits) {
Executor e = wu.getExecutor();
if (e!=null && e!=c)
e.interrupt();
}
}
}