-
Notifications
You must be signed in to change notification settings - Fork 58
/
WorkerThread.java
347 lines (314 loc) · 11.4 KB
/
WorkerThread.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
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.serving.wlm;
import ai.djl.Device;
import ai.djl.serving.wlm.WorkerPoolConfig.ThreadConfig;
import ai.djl.serving.wlm.util.AutoIncIdGenerator;
import ai.djl.serving.wlm.util.WlmException;
import ai.djl.serving.wlm.util.WorkerJob;
import ai.djl.translate.TranslateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/** The {@link WorkerThread} is the worker managed by the {@link WorkLoadManager}. */
public final class WorkerThread<I, O> implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(WorkerThread.class);
private static final AutoIncIdGenerator ID_GEN = new AutoIncIdGenerator("WT-");
private ThreadConfig<I, O> threadConfig;
private AtomicBoolean running = new AtomicBoolean(true);
private LinkedBlockingDeque<WorkerJob<I, O>> configJobs;
private BatchAggregator<I, O> aggregator;
private Device device;
private AtomicReference<Thread> currentThread = new AtomicReference<>();
private WorkerState state;
private String workerId;
private long startTime;
private boolean fixPoolThread;
private long stateChangeTime;
/**
* Builds a workerThread with this builder.
*
* @param builder build a new worker thread using this builder.
*/
private WorkerThread(Builder<I, O> builder) {
this.aggregator = builder.aggregator;
this.workerId = ID_GEN.generate();
this.startTime = System.currentTimeMillis();
this.fixPoolThread = builder.fixPoolThread;
this.device = builder.device;
threadConfig = builder.workerPoolConfig.newThread(device);
configJobs = new LinkedBlockingDeque<>();
logger.info(
"Starting worker thread {} for model {} on device {}",
workerId,
builder.workerPoolConfig,
device);
}
/** {@inheritDoc} */
@Override
public void run() {
Thread thread = Thread.currentThread();
thread.setName(workerId);
currentThread.set(thread);
this.state = WorkerState.WORKER_STARTED;
List<Job<I, O>> req = null;
String errorMessage = "Worker shutting down";
try {
runAllConfigJobs(); // Run initial config jobs
while (isRunning() && !aggregator.isFinished()) {
req = aggregator.getRequest();
if (req != null && !req.isEmpty()) {
state = WorkerState.WORKER_BUSY;
runAllConfigJobs(); // Run new config jobs
try {
runJobs(req);
aggregator.sendResponse();
} catch (TranslateException e) {
logger.warn("{}: Failed to predict", workerId, e);
aggregator.sendError(e);
} finally {
state = WorkerState.WORKER_STARTED;
}
}
req = null;
}
} catch (InterruptedException e) {
logger.debug("Shutting down worker thread {} .. Scaling down.", workerId);
} catch (Throwable t) {
logger.error("{}: Server error", workerId, t);
errorMessage = t.getMessage();
} finally {
logger.debug(
"Shutting down the worker thread {} .. {}",
workerId,
currentThread.get().getName());
currentThread.set(null);
shutdown(WorkerState.WORKER_STOPPED);
if (req != null) {
Exception e = new WlmException(errorMessage);
aggregator.sendError(e);
}
}
}
private void runAllConfigJobs() throws TranslateException {
while (!threadConfig.getConfigJobs().isEmpty()) {
// Run base worker pool configurations if present
runConfigJob(threadConfig.getConfigJobs().pop());
}
while (!configJobs.isEmpty()) {
// Run thread config jobs if present
runConfigJob(configJobs.pop().getJob());
}
}
private O runConfigJob(Job<I, O> configJob) throws TranslateException {
runJobs(Collections.singletonList(configJob));
return configJob.getOutput();
}
private void runJobs(List<Job<I, O>> input) throws TranslateException {
Map<Optional<JobFunction<I, O>>, List<Job<I, O>>> jobs =
input.stream().collect(Collectors.groupingBy(Job::getRunner));
for (Map.Entry<Optional<JobFunction<I, O>>, List<Job<I, O>>> fjob : jobs.entrySet()) {
if (fjob.getKey().isPresent()) {
Job.runAll(fjob.getValue(), fjob.getKey().get());
} else {
threadConfig.run(fjob.getValue());
}
}
}
/**
* Returns the worker thread ID.
*
* @return the worker thread ID
*/
public String getWorkerId() {
return workerId;
}
/**
* Returns the worker thread ID (number without prefix).
*
* @return the worker thread ID (number without prefix)
*/
public int getWorkerIdNum() {
return ID_GEN.stripPrefix(workerId);
}
/**
* Returns the {@link WorkerPoolConfig}'s {@link ThreadConfig} for this thread.
*
* @return the {@link WorkerPoolConfig}'s {@link ThreadConfig} for this thread
*/
public ThreadConfig<I, O> getThreadType() {
return threadConfig;
}
/**
* Returns true if the worker thread is running.
*
* @return true if the worker thread is running
*/
public boolean isRunning() {
return running.get();
}
/**
* Returns the device used by the thread.
*
* @return the device used by the thread
*/
public Device getDevice() {
return device;
}
/**
* Returns the thread start time.
*
* @return the thread start time
*/
public long getStartTime() {
return startTime;
}
/**
* Returns the worker state.
*
* @return the worker state
*/
public WorkerState getState() {
return state;
}
/**
* Shuts down the worker thread.
*
* @param state the state to set the thread to
*/
public void shutdown(WorkerState state) {
running.set(false);
setState(state);
Thread thread = currentThread.getAndSet(null);
if (thread != null) {
thread.interrupt();
Exception e = new WlmException("Worker shutting down");
aggregator.sendError(e);
}
logger.info("Shutting down temporary worker {}", workerId);
threadConfig.close();
}
/**
* Adds a configuration job to this thread.
*
* @param wj the configuration job to add
*/
public void addConfigJob(WorkerJob<I, O> wj) {
configJobs.add(wj);
}
void setState(WorkerState newState) {
logger.debug("Worker thread {} has state change: {} -> {}", workerId, state, newState);
if (state != WorkerState.WORKER_SCALED_DOWN) {
// Don't update the state if it was terminated on purpose.. Scaling in..
this.state = newState;
stateChangeTime = System.currentTimeMillis();
}
}
boolean isStale() {
return (state == WorkerState.WORKER_STOPPED
|| state == WorkerState.WORKER_ERROR
|| state == WorkerState.WORKER_SCALED_DOWN)
&& System.currentTimeMillis() - stateChangeTime > Duration.ofMinutes(1).toMillis();
}
/**
* check if this worker is instantiate is one of the fix threads of a pool. fix threads are not
* automatically scales down, so they are candidate for down scaling when minWorker/maxWorker
* size of a model changes.
*
* @return the fixPoolThread
*/
public boolean isFixPoolThread() {
return fixPoolThread;
}
/**
* Creates a builder to build a {@code WorkerThread}.
*
* @param <I> the workerPoolConfig input class
* @param <O> the workerPoolConfig output class
* @param wpc the {@link WorkerPoolConfig} the thread will be responsible for
* @return a new builder
*/
public static <I, O> Builder<I, O> builder(WorkerPoolConfig<I, O> wpc) {
return new Builder<>(wpc);
}
/** A Builder to construct a {@code WorkerThread}. */
public static final class Builder<I, O> {
private WorkerPoolConfig<I, O> workerPoolConfig;
private Device device;
private BatchAggregator<I, O> aggregator;
private LinkedBlockingDeque<WorkerJob<I, O>> jobQueue;
private boolean fixPoolThread;
Builder(WorkerPoolConfig<I, O> wpc) {
this.workerPoolConfig = wpc;
this.fixPoolThread = true;
}
/**
* RSets the device to run operations on.
*
* @param device the device to run operations on
* @return self-reference to this builder
*/
public Builder<I, O> setDevice(Device device) {
this.device = device;
return this;
}
/**
* Sets the jobQueue used to poll for new jobs.
*
* @param jobQueue the jobQueue to set
* @return self-reference to this builder.
*/
public Builder<I, O> setJobQueue(LinkedBlockingDeque<WorkerJob<I, O>> jobQueue) {
this.jobQueue = jobQueue;
return this;
}
/**
* Sets if the workerThread should be part of the fixed pool. Fixed Pool workers don't
* terminate themself but are managed by WorkLoadManager min/max-worker scale functionality.
*
* @param fixPoolThread the fixPoolThread to set
* @return self-reference to this builder.
*/
public Builder<I, O> optFixPoolThread(boolean fixPoolThread) {
this.fixPoolThread = fixPoolThread;
return this;
}
/**
* Builds the {@link WorkerThread} with the provided data.
*
* @return an {@link WorkerThread}
*/
public WorkerThread<I, O> build() {
if (device == null) {
throw new IllegalArgumentException("Must set device for worker thread");
}
if (jobQueue == null) {
throw new IllegalArgumentException("jobQueue has to be set.");
}
if (fixPoolThread) {
aggregator = new PermanentBatchAggregator<>(workerPoolConfig, jobQueue);
} else {
aggregator = new TemporaryBatchAggregator<>(workerPoolConfig, jobQueue);
}
return new WorkerThread<>(this);
}
}
}