-
Notifications
You must be signed in to change notification settings - Fork 58
/
WorkerPool.java
347 lines (315 loc) · 10.9 KB
/
WorkerPool.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 2022 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.ModelException;
import ai.djl.serving.wlm.util.WorkerJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* Manages the work load for a single model.
*
* @author erik.bamberg@web.de
*/
public class WorkerPool<I, O> {
private static final Logger logger = LoggerFactory.getLogger(WorkerPool.class);
private final WorkerPoolConfig<I, O> wpc;
private ExecutorService threadPool;
private Map<Device, WorkerGroup<I, O>> workerGroups;
private LinkedBlockingDeque<WorkerJob<I, O>> jobQueue;
private AtomicInteger refCnt;
/**
* Construct and initial data structure.
*
* @param wpc the model this WorkerPool belongs to
* @param threadPool the thread pool executor
*/
WorkerPool(WorkerPoolConfig<I, O> wpc, ExecutorService threadPool) {
this.wpc = wpc;
this.threadPool = threadPool;
workerGroups = new ConcurrentHashMap<>();
refCnt = new AtomicInteger(1);
}
/** Increases the reference count. */
public void increaseRef() {
refCnt.incrementAndGet();
}
/**
* Decrease the reference count and return the current count.
*
* @return the current count
*/
public int decreaseRef() {
return refCnt.decrementAndGet();
}
/**
* Returns the model of the worker pool.
*
* @return the model of the worker pool
*/
public WorkerPoolConfig<I, O> getWpc() {
return wpc;
}
ExecutorService getThreadPool() {
return threadPool;
}
/**
* Returns a map of {@code WorkerGroup}.
*
* @return a map of {@code WorkerGroup}
*/
public Map<Device, WorkerGroup<I, O>> getWorkerGroups() {
return workerGroups;
}
/**
* Returns a list of worker thread.
*
* @return the workers
*/
public List<WorkerThread<I, O>> getWorkers() {
return workerGroups.values().stream()
.flatMap(g -> g.workers.stream())
.collect(Collectors.toList());
}
/**
* Returns the {@code JobQueue} for this model.
*
* @return the jobQueue
*/
public LinkedBlockingDeque<WorkerJob<I, O>> getJobQueue() {
return jobQueue;
}
/**
* Returns the maximum number of workers for a model across all devices.
*
* @return the maximum number of workers for a model across all devices
*/
public int getMaxWorkers() {
return workerGroups.values().stream().mapToInt(g -> g.maxWorkers).reduce(0, Integer::sum);
}
/**
* Return if all workers died.
*
* @return true if all workers died
*/
public boolean isAllWorkerDied() {
for (WorkerGroup<I, O> group : workerGroups.values()) {
for (WorkerThread<?, ?> thread : group.getWorkers()) {
if (thread.isRunning()) {
return false;
}
}
}
return true;
}
/**
* Returns {@code true} if all workers are busy.
*
* @return {@code true} if all workers are busy
*/
public boolean isAllWorkerBusy() {
for (WorkerGroup<I, O> group : workerGroups.values()) {
for (WorkerThread<?, ?> thread : group.getWorkers()) {
if (thread.getState() == WorkerState.WORKER_STARTED) {
return false;
}
}
}
return true;
}
/**
* Returns if the worker groups is fully scaled.
*
* @return true if the worker groups is fully scaled
*/
public boolean isFullyScaled() {
for (WorkerGroup<I, O> group : workerGroups.values()) {
if (group.getMinWorkers() > group.getWorkers().size()) {
return false;
}
}
return true;
}
/**
* Initializes new worker capacities for this model.
*
* @param deviceName the device for the model, null for default devices
*/
public void initWorkers(String deviceName) {
initWorkers(deviceName, -1, -1);
}
/**
* Initializes new worker capacities for this model.
*
* @param deviceName the device for the model, null for default devices
* @param minWorkers minimum amount of workers (-1 for model default).
* @param maxWorkers maximum amount of workers (-1 for model default).
*/
public void initWorkers(String deviceName, int minWorkers, int maxWorkers) {
Device device;
synchronized (wpc) {
try {
wpc.initialize();
device = wpc.withDefaultDevice(deviceName);
logger.info("loading model {} on {} ...", wpc, device);
wpc.load(device);
} catch (ModelException | IOException e) {
throw new CompletionException(e);
}
if (wpc.getStatus() != WorkerPoolConfig.Status.READY) {
logger.warn("Cannot scale workers while model is not READY: {}", wpc);
}
}
// jobQueue should be initialized after model is configure
if (jobQueue == null) {
jobQueue = new LinkedBlockingDeque<>(wpc.getQueueSize());
}
cleanup();
WorkerGroup<I, O> group =
workerGroups.computeIfAbsent(device, d -> new WorkerGroup<>(this, d));
group.configureWorkers(minWorkers, maxWorkers);
doScaleWorker(group);
log();
}
/**
* Sets new worker capacities for this model.
*
* @param deviceName the device for the model, null for all loaded devices
* @param minWorkers minimum amount of workers.
* @param maxWorkers maximum amount of workers.
*/
public void scaleWorkers(String deviceName, int minWorkers, int maxWorkers) {
if (deviceName != null) {
// if the model has not been loaded on device, this will load the model
initWorkers(deviceName, minWorkers, maxWorkers);
return;
}
cleanup();
// scale for all devices
for (WorkerGroup<I, O> group : workerGroups.values()) {
group.configureWorkers(minWorkers, maxWorkers);
doScaleWorker(group);
}
log();
}
private void doScaleWorker(WorkerGroup<I, O> group) {
int minWorkers = group.getMinWorkers();
List<WorkerThread<I, O>> fixedPoolThreads = new ArrayList<>();
for (WorkerThread<I, O> threads : group.getWorkers()) {
if (threads.isFixPoolThread()) {
fixedPoolThreads.add(threads);
}
}
int activeThreads = fixedPoolThreads.size();
if (activeThreads < minWorkers) {
// scale up the fixed pool
logger.info(
"scaling up min workers by {} (from {} to {}) workers. Total range is min {} to"
+ " max {}",
minWorkers - activeThreads,
activeThreads,
minWorkers,
minWorkers,
group.getMaxWorkers());
group.addThreads(minWorkers - activeThreads, true);
} else {
// scale down the fixed pool
logger.info("scale down from workers {} to {}", activeThreads, minWorkers);
fixedPoolThreads
.subList(minWorkers, activeThreads)
.forEach(t -> t.shutdown(WorkerState.WORKER_SCALED_DOWN));
}
}
/** Shutdown all works. */
public void shutdownWorkers() {
synchronized (wpc) {
List<WorkerThread<I, O>> threads = getWorkers();
for (WorkerThread<I, O> thread : threads) {
thread.shutdown(WorkerState.WORKER_SCALED_DOWN);
}
threads.clear();
}
}
/** removes all stopped workers and workers in state error from the pool. */
public void cleanup() {
for (WorkerGroup<I, O> group : workerGroups.values()) {
group.workers.removeIf(WorkerThread::isStale);
}
}
/** Shuts down all the worker threads in the work pool. */
public void shutdown() {
wpc.close();
for (WorkerGroup<I, O> group : workerGroups.values()) {
for (WorkerThread<I, O> worker : group.workers) {
worker.shutdown(WorkerState.WORKER_STOPPED);
}
}
workerGroups.clear();
if (jobQueue != null) {
for (WorkerJob<I, O> wj : jobQueue) {
wj.getFuture().cancel(true);
}
}
}
/**
* Adds temporary threads across existing devices.
*
* <p>Only supports temporary threads because permanent threads are managed per-device, so it
* doesn't need a multi-device version.
*/
void addThreads() {
// Add threads to devices which has most room to grow
List<WorkerGroup<I, O>> sorted = new ArrayList<>(workerGroups.values());
if (sorted.isEmpty()) {
logger.warn("No worker pool available.");
return;
}
sorted.sort(Comparator.comparingInt(p -> p.getMaxWorkers() - p.workers.size()));
WorkerGroup<I, O> group = sorted.get(sorted.size() - 1);
if (group.getMaxWorkers() > group.workers.size()) {
group.addThreads(1, false);
}
}
/**
* Logs the current state of this {@code WorkerPool} when level "Debug" is enabled.
*
* <p>Logs all thread-ids in the pool.
*/
private void log() {
if (logger.isDebugEnabled()) {
StringBuffer buf = new StringBuffer();
getWorkers()
.forEach(
w -> {
buf.append(w.getWorkerId());
if (w.isFixPoolThread()) {
buf.append("-fixedPool\n");
} else {
buf.append("-tmpPool\n");
}
});
logger.debug("worker pool for model {}:\n {}", wpc, buf);
}
}
}