1 | | | import 'dart:async'; |
2 | | | import 'dart:collection'; |
3 | | |
|
4 | | | import 'package:logger/web.dart'; |
5 | | | import 'package:using/using.dart'; |
6 | | |
|
7 | | | import '../concurrency_settings.dart'; |
8 | | | import '../exceptions/exception_manager.dart'; |
9 | | | import '../exceptions/squadron_error.dart'; |
10 | | | import '../exceptions/squadron_exception.dart'; |
11 | | | import '../exceptions/task_terminated_exception.dart'; |
12 | | | import '../exceptions/worker_exception.dart'; |
13 | | | import '../iworker.dart'; |
14 | | | import '../stats/perf_counter.dart'; |
15 | | | import '../stats/worker_stat.dart'; |
16 | | | import '../typedefs.dart'; |
17 | | | import '../worker/worker.dart'; |
18 | | | import '../worker_service.dart'; |
19 | | | import '_pool_worker.dart'; |
20 | | | import '_worker_stream_task.dart'; |
21 | | | import '_worker_task.dart'; |
22 | | | import '_worker_value_task.dart'; |
23 | | | import 'stream_task.dart'; |
24 | | | import 'task.dart'; |
25 | | | import 'value_task.dart'; |
26 | | |
|
27 | | | typedef WorkerFactory<W> = W Function(ExceptionManager); |
28 | | |
|
29 | | | /// Worker pool responsible for instantiating, starting and stopping workers running in parallel. |
30 | | | /// A [WorkerPool] is also responsible for creating and assigning [WorkerTask]s to [Worker]s. |
31 | | | abstract class WorkerPool<W extends Worker> |
32 | | | with Releasable |
33 | | | implements WorkerService, IWorker { |
34 | | | /// Create a worker pool. |
35 | | | /// |
36 | | | /// Workers are instantiated using the provided [_workerFactory]. |
37 | | | /// The pool will only instantiate workers as needed, depending on [concurrencySettings]. |
38 | | | /// The [ConcurrencySettings.minWorkers] and [ConcurrencySettings.maxWorkers] settings control |
39 | | | /// how many workers will live in the pool. The [ConcurrencySettings.maxParallel] setting |
40 | | | /// controls how many tasks can be posted to each individual worker in the pool. |
41 | | 4 | WorkerPool(this._workerFactory, |
42 | | | {ConcurrencySettings? concurrencySettings, |
43 | | | ExceptionManager? exceptionManager}) |
44 | | 0 | : concurrencySettings = concurrencySettings ?? ConcurrencySettings(), |
45 | | 3 | _exceptionManager = exceptionManager ?? ExceptionManager(); |
46 | | |
|
47 | | 4 | @override |
48 | | | void release() { |
49 | | 4 | stop(); |
50 | | 4 | super.release(); |
51 | | | } |
52 | | |
|
53 | | | final WorkerFactory<W> _workerFactory; |
54 | | |
|
55 | | | @override |
56 | | | Logger? channelLogger; |
57 | | |
|
58 | | 4 | @override |
59 | | | ExceptionManager get exceptionManager => |
60 | | 4 | (_exceptionManager ??= ExceptionManager()); |
61 | | | ExceptionManager? _exceptionManager; |
62 | | |
|
63 | | | /// Concurrency settings. |
64 | | | final ConcurrencySettings concurrencySettings; |
65 | | |
|
66 | | | final _workers = <PoolWorker<W>>[]; |
67 | | |
|
68 | | | final _deadWorkerStats = <WorkerStat>[]; |
69 | | |
|
70 | | | /// Whether this pool is scheduled for stopping. |
71 | | 2 | bool get stopped => _stopped; |
72 | | | bool _stopped = false; |
73 | | |
|
74 | | | /// Number of workers. |
75 | | 3 | int get size => _workers.length; |
76 | | |
|
77 | | | /// Maximum number of workers. |
78 | | 0 | int get maxSize => _maxSize; |
79 | | | int _maxSize = 0; |
80 | | |
|
81 | | | final _workerPoolListeners = <Object, void Function(WorkerStat, bool)>{}; |
82 | | |
|
83 | | | /// Registers a callback to be invoked when a worker thread is added or removed from the pool. |
84 | | 0 | Object registerWorkerPoolListener(void Function(WorkerStat, bool) listener) { |
85 | | 0 | final token = Object(); |
86 | | 0 | _workerPoolListeners[token] = listener; |
87 | | | return token; |
88 | | | } |
89 | | |
|
90 | | | /// Unregisters a callback. |
91 | | 0 | void unregisterWorkerPoolListener( |
92 | | | {Function(WorkerStat, bool)? listener, Object? token}) { |
93 | | | if (token != null) { |
94 | | 0 | _workerPoolListeners.remove(token); |
95 | | | } else if (listener != null) { |
96 | | 0 | _workerPoolListeners.removeWhere((key, value) => value == listener); |
97 | | | } |
98 | | | } |
99 | | |
|
100 | | | int _startingWorkers = 0; |
101 | | |
|
102 | | 4 | int _getProvisionNeeds(int workload) { |
103 | | 8 | final minWorkers = concurrencySettings.minWorkers; |
104 | | 4 | if (workload < minWorkers) { |
105 | | | // at least minWorkers |
106 | | | workload = minWorkers; |
107 | | | } |
108 | | 8 | final maxWorkers = concurrencySettings.maxWorkers; |
109 | | 8 | if (maxWorkers > 0 && workload > maxWorkers) { |
110 | | | // at most maxWorkers if > 0 |
111 | | | workload = maxWorkers; |
112 | | | } |
113 | | | // adjust by _workers.length and _startingWorkers |
114 | | 20 | return workload - _workers.length - _startingWorkers; |
115 | | | } |
116 | | |
|
117 | | 4 | Future<void> _provisionWorkers(int workload) { |
118 | | 8 | final tasks = <Future>[], errors = []; |
119 | | 8 | final maxParallel = concurrencySettings.maxParallel; |
120 | | 8 | for (var i = 0; i < workload; i++) { |
121 | | | try { |
122 | | 12 | final worker = _workerFactory(exceptionManager); |
123 | | 8 | worker.channelLogger = channelLogger; |
124 | | |
|
125 | | 4 | final poolWorker = PoolWorker(worker, maxParallel); |
126 | | 8 | _startingWorkers++; |
127 | | 20 | tasks.add(poolWorker.worker.start().whenComplete(() { |
128 | | 8 | _startingWorkers--; |
129 | | 8 | }).then((_) { |
130 | | | // start succeeded: register worker |
131 | | 4 | _addWorkerAndNotify(poolWorker); |
132 | | 5 | }).catchError((ex, st) { |
133 | | | // start failed, ensure the worker is stopped |
134 | | 2 | poolWorker.worker.stop(); |
135 | | 2 | errors.add(SquadronException.from(ex, st)); |
136 | | | })); |
137 | | | } catch (ex, st) { |
138 | | 0 | errors.add(SquadronException.from(ex, st)); |
139 | | | } |
140 | | | } |
141 | | |
|
142 | | 12 | return Future.wait(tasks).whenComplete(() { |
143 | | 16 | if (_workers.length > _maxSize) { |
144 | | 12 | _maxSize = _workers.length; |
145 | | | } |
146 | | 4 | if (errors.isNotEmpty) { |
147 | | 3 | if (errors.length < tasks.length) { |
148 | | | // some tasks failed: warn |
149 | | 0 | channelLogger?.e(() => 'Error while provisionning workers: $errors'); |
150 | | | } else { |
151 | | | // all tasks failed: throw |
152 | | 3 | throw errors.firstWhere((e) => e is SquadronError, |
153 | | 1 | orElse: () => null) ?? |
154 | | 3 | errors.firstWhere((e) => e is WorkerException, |
155 | | 0 | orElse: () => null) ?? |
156 | | 0 | errors.first; |
157 | | | } |
158 | | | } |
159 | | | }); |
160 | | | } |
161 | | |
|
162 | | | /// Ensure at least [ConcurrencySettings.minWorkers] workers are started |
163 | | | /// (defaulting to 1 if [ConcurrencySettings.minWorkers] is zero). |
164 | | 2 | @override |
165 | | | FutureOr<void> start() { |
166 | | 2 | _stopped = false; |
167 | | 6 | final needs = _getProvisionNeeds(_queue.isEmpty ? 1 : _queue.length); |
168 | | 2 | if (needs > 0) { |
169 | | 2 | return _provisionWorkers(needs); |
170 | | | } |
171 | | | } |
172 | | |
|
173 | | 4 | void _notify(W worker, {required bool removed}) { |
174 | | 8 | if (_workerPoolListeners.isNotEmpty) { |
175 | | 0 | final stats = worker.stats; |
176 | | 0 | for (var listener in _workerPoolListeners.values) { |
177 | | | try { |
178 | | 0 | listener(stats, removed); |
179 | | | } catch (ex) { |
180 | | | // swallow error from user land |
181 | | | } |
182 | | | } |
183 | | | } |
184 | | | } |
185 | | |
|
186 | | 4 | void _removeWorkerAndNotify(PoolWorker<W> poolWorker) { |
187 | | 8 | _workers.remove(poolWorker); |
188 | | 8 | _notify(poolWorker.worker, removed: true); |
189 | | | } |
190 | | |
|
191 | | 4 | void _addWorkerAndNotify(PoolWorker<W> poolWorker) { |
192 | | 8 | _workers.add(poolWorker); |
193 | | 8 | _notify(poolWorker.worker, removed: false); |
194 | | | } |
195 | | |
|
196 | | 4 | int _removeWorker(PoolWorker<W> poolWorker, bool force) { |
197 | | 5 | if (force || _workers.length > concurrencySettings.minWorkers) { |
198 | | 4 | final worker = poolWorker.worker; |
199 | | 4 | worker.stop(); |
200 | | 12 | _deadWorkerStats.add(worker.stats); |
201 | | 4 | _removeWorkerAndNotify(poolWorker); |
202 | | | return 1; |
203 | | | } else { |
204 | | | return 0; |
205 | | | } |
206 | | | } |
207 | | |
|
208 | | | /// Stop idle pool workers matching the [predicate]. |
209 | | | /// If [predicate] is null or not provided, all workers will be stopped. |
210 | | | /// Stopping a worker does not interrupt or cancel processing. Workers will |
211 | | | /// complete pending tasks before shutting down. In the meantime, they will |
212 | | | /// not receive any new workload. |
213 | | | /// Returns the number of workers that have been stopped. |
214 | | 4 | @override |
215 | | | int stop([bool Function(W worker)? predicate]) { |
216 | | | List<PoolWorker<W>> targets; |
217 | | | bool force = (predicate == null); |
218 | | | if (force) { |
219 | | | // kill workers while keeping enough workers alive to process pending tasks |
220 | | 20 | targets = _workers.skip(_queue.length).toList(); |
221 | | 4 | _stopped = true; |
222 | | | } else { |
223 | | | // kill workers that are idle and satisfy the predicate |
224 | | 7 | targets = _workers.where((w) => w.isIdle && predicate(w.worker)).toList(); |
225 | | | } |
226 | | | var stopped = 0; |
227 | | 8 | for (var poolWorker in targets) { |
228 | | 8 | stopped += _removeWorker(poolWorker, force); |
229 | | | } |
230 | | | return stopped; |
231 | | | } |
232 | | |
|
233 | | 1 | @override |
234 | | | void terminate([TaskTerminatedException? ex]) { |
235 | | 1 | _stopped = true; |
236 | | 2 | final targets = _workers.toList(); |
237 | | 2 | for (var poolWorker in targets) { |
238 | | 1 | _removeWorker(poolWorker, true); |
239 | | 2 | poolWorker.worker.terminate(ex); |
240 | | | } |
241 | | | } |
242 | | |
|
243 | | | final _queue = Queue<WorkerTask>(); |
244 | | | final _executing = <WorkerTask>{}; |
245 | | |
|
246 | | | /// Gets remaining workload |
247 | | 6 | int get pendingWorkload => _queue.length; |
248 | | |
|
249 | | 4 | WorkerTask<T, W> _enqueue<T>(WorkerTask<T, W> task) { |
250 | | 4 | if (_stopped) { |
251 | | 1 | throw SquadronErrorImpl.create( |
252 | | | 'The pool cannot accept new requests because it is stopped', |
253 | | | ); |
254 | | | } |
255 | | 8 | _queue.addLast(task); |
256 | | 4 | _schedule(); |
257 | | | return task; |
258 | | | } |
259 | | |
|
260 | | | /// Registers and schedules a [task] that returns a single value. |
261 | | | /// Returns a future that completes with the task's value. |
262 | | 4 | Future<T> execute<T>(Future<T> Function(W worker) task, |
263 | | | {PerfCounter? counter}) => |
264 | | 8 | scheduleValueTask(task, counter: counter).value; |
265 | | |
|
266 | | | /// Registers and schedules a [task] that returns a stream of values. |
267 | | | /// Returns a stream containing the task's values. |
268 | | 4 | Stream<T> stream<T>(Stream<T> Function(W worker) task, |
269 | | | {PerfCounter? counter}) => |
270 | | 8 | scheduleStreamTask(task, counter: counter).stream; |
271 | | |
|
272 | | | /// Registers and schedules a [task] that returns a single value. |
273 | | | /// Returns a [ValueTask]. |
274 | | 4 | ValueTask<T> scheduleValueTask<T>(Future<T> Function(W worker) task, |
275 | | | {PerfCounter? counter}) => |
276 | | 8 | _enqueue<T>(WorkerValueTask<T, W>(task, counter)) as ValueTask<T>; |
277 | | |
|
278 | | | /// Registers and schedules a [task] that returns a stream of values. |
279 | | | /// Returns a [StreamTask]. |
280 | | 4 | StreamTask<T> scheduleStreamTask<T>(Stream<T> Function(W worker) task, |
281 | | | {PerfCounter? counter}) => |
282 | | 8 | _enqueue<T>(WorkerStreamTask<T, W>(task, counter)) as StreamTask<T>; |
283 | | |
|
284 | | | /// Schedule tasks. |
285 | | 4 | void _schedule() { |
286 | | 16 | if (_workers.isEmpty && _startingWorkers > 0) { |
287 | | | // workers are still starting, defer |
288 | | 8 | Future(_schedule); |
289 | | | return; |
290 | | | } |
291 | | |
|
292 | | | // remove dead workers |
293 | | 4 | _workers |
294 | | 4 | .where(PoolWorker.isStopped) |
295 | | 4 | .toList() // take a copy |
296 | | 8 | .forEach(_removeWorkerAndNotify); |
297 | | |
|
298 | | | // remove canceled tasks |
299 | | 16 | _queue.removeWhere((t) => t.isCanceled); |
300 | | |
|
301 | | | // any work to do? |
302 | | 8 | if (_queue.isEmpty) { |
303 | | | // no: effectively stop the pool if needed and return |
304 | | 10 | if (_stopped && _executing.isEmpty) { |
305 | | 3 | stop(); |
306 | | | } |
307 | | | return; |
308 | | | } |
309 | | |
|
310 | | | // yes: dispatch tasks to workers |
311 | | 4 | _dispatchTasks(); |
312 | | |
|
313 | | | // and provision more workers if possible and necessary |
314 | | 12 | final needs = _getProvisionNeeds(_queue.length); |
315 | | 4 | if (needs > 0) { |
316 | | 8 | _provisionWorkers(needs).then( |
317 | | 8 | (_) => _dispatchTasks(), |
318 | | 1 | onError: (ex) { |
319 | | 1 | channelLogger?.e(() => 'Provisionning workers failed with error $ex'); |
320 | | 2 | while (_queue.isNotEmpty) { |
321 | | 3 | _queue.removeFirst().cancel('Provisionning workers failed'); |
322 | | | } |
323 | | | }, |
324 | | | ); |
325 | | | } |
326 | | | } |
327 | | |
|
328 | | 4 | int _sortAndGetMaxCapacity() { |
329 | | 8 | _workers.sort(PoolWorker.compareCapacityDesc); |
330 | | 20 | return _workers.isEmpty ? 0 : _workers.first.capacity; |
331 | | | } |
332 | | |
|
333 | | 4 | void _dispatchTasks() { |
334 | | | int maxCapacity; |
335 | | 16 | while (_queue.isNotEmpty && (maxCapacity = _sortAndGetMaxCapacity()) > 0) { |
336 | | 4 | maxCapacity -= 1; |
337 | | 16 | for (var idx = 0; idx < _workers.length; idx++) { |
338 | | 8 | final w = _workers[idx]; |
339 | | 24 | if (_queue.isEmpty || w.capacity == 0 || w.capacity < maxCapacity) { |
340 | | | break; |
341 | | | } |
342 | | 8 | final task = _queue.removeFirst(); |
343 | | 8 | _executing.add(task); |
344 | | 12 | w.run(task).whenComplete(() { |
345 | | 8 | _executing.remove(task); |
346 | | 4 | _schedule(); |
347 | | | }); |
348 | | | } |
349 | | | } |
350 | | | } |
351 | | |
|
352 | | | /// Task cancelation. If a specific [task] is provided, only this task will be canceled. |
353 | | | /// Otherwise, all tasks registered with the [WorkerPool] are canceled. |
354 | | 1 | void cancel(Task task, [String? message]) { |
355 | | 2 | _executing.remove(task); |
356 | | 4 | _queue.removeWhere((t) => t == task); |
357 | | 1 | task.cancel(message); |
358 | | | } |
359 | | |
|
360 | | | /// Task cancelation. If a specific [task] is provided, only this task will be canceled. |
361 | | | /// Otherwise, all tasks registered with the [WorkerPool] are canceled. |
362 | | 1 | void cancelAll([String? message]) { |
363 | | 4 | final toBeCanceled = _executing.followedBy(_queue).toList(); |
364 | | 2 | _executing.clear(); |
365 | | 2 | _queue.clear(); |
366 | | 2 | for (var task in toBeCanceled) { |
367 | | 1 | task.cancel(message); |
368 | | | } |
369 | | | } |
370 | | |
|
371 | | | /// Worker statistics. |
372 | | 3 | Iterable<WorkerStat> get stats => _workers.map(PoolWorker.getStats); |
373 | | |
|
374 | | | /// Full worker statistics. |
375 | | 0 | Iterable<WorkerStat> get fullStats => _deadWorkerStats.followedBy(stats); |
376 | | |
|
377 | | | /// Worker pools do not need an [operations] map. |
378 | | 0 | @override |
379 | | 0 | OperationsMap get operations => WorkerService.noOperations; |
380 | | | } |