Commit | Line | Data |
---|---|---|
ded253e2 | 1 | import { AsyncResource } from 'node:async_hooks' |
2845f2a5 | 2 | import { randomUUID } from 'node:crypto' |
ded253e2 | 3 | import { EventEmitterAsyncResource } from 'node:events' |
62c15a68 | 4 | import { performance } from 'node:perf_hooks' |
ef083f7b | 5 | import type { TransferListItem } from 'node:worker_threads' |
ded253e2 | 6 | |
5c4d16da JB |
7 | import type { |
8 | MessageValue, | |
9 | PromiseResponseWrapper, | |
31847469 JB |
10 | Task, |
11 | TaskFunctionProperties | |
d35e5717 | 12 | } from '../utility-types.js' |
bbeadd16 | 13 | import { |
ded253e2 | 14 | average, |
31847469 | 15 | buildTaskFunctionProperties, |
ff128cc9 | 16 | DEFAULT_TASK_NAME, |
bbeadd16 | 17 | EMPTY_FUNCTION, |
463226a4 | 18 | exponentialDelay, |
59317253 | 19 | isKillBehavior, |
0d80593b | 20 | isPlainObject, |
90d6701c | 21 | max, |
afe0d5bf | 22 | median, |
90d6701c | 23 | min, |
463226a4 JB |
24 | round, |
25 | sleep | |
d35e5717 | 26 | } from '../utils.js' |
31847469 JB |
27 | import type { |
28 | TaskFunction, | |
29 | TaskFunctionObject | |
30 | } from '../worker/task-functions.js' | |
ded253e2 | 31 | import { KillBehaviors } from '../worker/worker-options.js' |
c4855468 | 32 | import { |
65d7a1c9 | 33 | type IPool, |
c4855468 | 34 | PoolEvents, |
6b27d407 | 35 | type PoolInfo, |
c4855468 | 36 | type PoolOptions, |
6b27d407 JB |
37 | type PoolType, |
38 | PoolTypes, | |
4b628b48 | 39 | type TasksQueueOptions |
d35e5717 | 40 | } from './pool.js' |
a35560ba | 41 | import { |
f0d7f803 | 42 | Measurements, |
a35560ba | 43 | WorkerChoiceStrategies, |
a20f0ba5 JB |
44 | type WorkerChoiceStrategy, |
45 | type WorkerChoiceStrategyOptions | |
d35e5717 | 46 | } from './selection-strategies/selection-strategies-types.js' |
bcfb06ce | 47 | import { WorkerChoiceStrategiesContext } from './selection-strategies/worker-choice-strategies-context.js' |
bde6b5d7 JB |
48 | import { |
49 | checkFilePath, | |
95d1a734 | 50 | checkValidPriority, |
bde6b5d7 | 51 | checkValidTasksQueueOptions, |
bfc75cca | 52 | checkValidWorkerChoiceStrategy, |
32b141fd | 53 | getDefaultTasksQueueOptions, |
c329fd41 JB |
54 | updateEluWorkerUsage, |
55 | updateRunTimeWorkerUsage, | |
56 | updateTaskStatisticsWorkerUsage, | |
57 | updateWaitTimeWorkerUsage, | |
87347ea8 | 58 | waitWorkerNodeEvents |
d35e5717 | 59 | } from './utils.js' |
ded253e2 JB |
60 | import { version } from './version.js' |
61 | import type { | |
62 | IWorker, | |
63 | IWorkerNode, | |
64 | WorkerInfo, | |
65 | WorkerNodeEventDetail, | |
66 | WorkerType | |
67 | } from './worker.js' | |
68 | import { WorkerNode } from './worker-node.js' | |
23ccf9d7 | 69 | |
729c563d | 70 | /** |
ea7a90d3 | 71 | * Base class that implements some shared logic for all poolifier pools. |
729c563d | 72 | * |
38e795c1 | 73 | * @typeParam Worker - Type of worker which manages this pool. |
e102732c JB |
74 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
75 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
729c563d | 76 | */ |
c97c7edb | 77 | export abstract class AbstractPool< |
f06e48d8 | 78 | Worker extends IWorker, |
d3c8a1a8 S |
79 | Data = unknown, |
80 | Response = unknown | |
c4855468 | 81 | > implements IPool<Worker, Data, Response> { |
afc003b2 | 82 | /** @inheritDoc */ |
4b628b48 | 83 | public readonly workerNodes: Array<IWorkerNode<Worker, Data>> = [] |
4a6952ff | 84 | |
afc003b2 | 85 | /** @inheritDoc */ |
f80125ca | 86 | public emitter?: EventEmitterAsyncResource |
7c0ba920 | 87 | |
be0676b3 | 88 | /** |
419e8121 | 89 | * The task execution response promise map: |
2740a743 | 90 | * - `key`: The message id of each submitted task. |
bcfb06ce | 91 | * - `value`: An object that contains task's worker node key, execution response promise resolve and reject callbacks, async resource. |
be0676b3 | 92 | * |
a3445496 | 93 | * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id. |
be0676b3 | 94 | */ |
84781cd6 JB |
95 | protected promiseResponseMap: Map< |
96 | `${string}-${string}-${string}-${string}-${string}`, | |
97 | PromiseResponseWrapper<Response> | |
98 | > = new Map< | |
99 | `${string}-${string}-${string}-${string}-${string}`, | |
100 | PromiseResponseWrapper<Response> | |
101 | >() | |
c97c7edb | 102 | |
a35560ba | 103 | /** |
bcfb06ce | 104 | * Worker choice strategies context referencing worker choice algorithms implementation. |
a35560ba | 105 | */ |
bcfb06ce | 106 | protected workerChoiceStrategiesContext?: WorkerChoiceStrategiesContext< |
78cea37e JB |
107 | Worker, |
108 | Data, | |
109 | Response | |
a35560ba S |
110 | > |
111 | ||
72ae84a2 JB |
112 | /** |
113 | * The task functions added at runtime map: | |
114 | * - `key`: The task function name. | |
31847469 | 115 | * - `value`: The task function object. |
72ae84a2 | 116 | */ |
31847469 JB |
117 | private readonly taskFunctions: Map< |
118 | string, | |
119 | TaskFunctionObject<Data, Response> | |
120 | > | |
72ae84a2 | 121 | |
15b176e0 JB |
122 | /** |
123 | * Whether the pool is started or not. | |
124 | */ | |
125 | private started: boolean | |
47352846 JB |
126 | /** |
127 | * Whether the pool is starting or not. | |
128 | */ | |
129 | private starting: boolean | |
711623b8 JB |
130 | /** |
131 | * Whether the pool is destroying or not. | |
132 | */ | |
133 | private destroying: boolean | |
b362a929 JB |
134 | /** |
135 | * Whether the minimum number of workers is starting or not. | |
136 | */ | |
137 | private startingMinimumNumberOfWorkers: boolean | |
55082af9 JB |
138 | /** |
139 | * Whether the pool ready event has been emitted or not. | |
140 | */ | |
141 | private readyEventEmitted: boolean | |
afe0d5bf JB |
142 | /** |
143 | * The start timestamp of the pool. | |
144 | */ | |
97dc65d9 | 145 | private startTimestamp?: number |
afe0d5bf | 146 | |
729c563d S |
147 | /** |
148 | * Constructs a new poolifier pool. | |
149 | * | |
00e1bdeb | 150 | * @param minimumNumberOfWorkers - Minimum number of workers that this pool manages. |
029715f0 | 151 | * @param filePath - Path to the worker file. |
38e795c1 | 152 | * @param opts - Options for the pool. |
00e1bdeb | 153 | * @param maximumNumberOfWorkers - Maximum number of workers that this pool manages. |
729c563d | 154 | */ |
c97c7edb | 155 | public constructor ( |
26ce26ca | 156 | protected readonly minimumNumberOfWorkers: number, |
b4213b7f | 157 | protected readonly filePath: string, |
26ce26ca JB |
158 | protected readonly opts: PoolOptions<Worker>, |
159 | protected readonly maximumNumberOfWorkers?: number | |
c97c7edb | 160 | ) { |
78cea37e | 161 | if (!this.isMain()) { |
04f45163 | 162 | throw new Error( |
8c6d4acf | 163 | 'Cannot start a pool from a worker with the same type as the pool' |
04f45163 | 164 | ) |
c97c7edb | 165 | } |
26ce26ca | 166 | this.checkPoolType() |
bde6b5d7 | 167 | checkFilePath(this.filePath) |
26ce26ca | 168 | this.checkMinimumNumberOfWorkers(this.minimumNumberOfWorkers) |
7c0ba920 | 169 | this.checkPoolOptions(this.opts) |
1086026a | 170 | |
7254e419 JB |
171 | this.chooseWorkerNode = this.chooseWorkerNode.bind(this) |
172 | this.executeTask = this.executeTask.bind(this) | |
173 | this.enqueueTask = this.enqueueTask.bind(this) | |
1086026a | 174 | |
6bd72cd0 | 175 | if (this.opts.enableEvents === true) { |
e0843544 | 176 | this.initEventEmitter() |
7c0ba920 | 177 | } |
bcfb06ce | 178 | this.workerChoiceStrategiesContext = new WorkerChoiceStrategiesContext< |
d59df138 JB |
179 | Worker, |
180 | Data, | |
181 | Response | |
da309861 JB |
182 | >( |
183 | this, | |
bcfb06ce JB |
184 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
185 | [this.opts.workerChoiceStrategy!], | |
da309861 JB |
186 | this.opts.workerChoiceStrategyOptions |
187 | ) | |
b6b32453 JB |
188 | |
189 | this.setupHook() | |
190 | ||
31847469 | 191 | this.taskFunctions = new Map<string, TaskFunctionObject<Data, Response>>() |
72ae84a2 | 192 | |
47352846 | 193 | this.started = false |
075e51d1 | 194 | this.starting = false |
711623b8 | 195 | this.destroying = false |
55082af9 | 196 | this.readyEventEmitted = false |
b362a929 | 197 | this.startingMinimumNumberOfWorkers = false |
47352846 JB |
198 | if (this.opts.startWorkers === true) { |
199 | this.start() | |
200 | } | |
c97c7edb S |
201 | } |
202 | ||
26ce26ca JB |
203 | private checkPoolType (): void { |
204 | if (this.type === PoolTypes.fixed && this.maximumNumberOfWorkers != null) { | |
205 | throw new Error( | |
206 | 'Cannot instantiate a fixed pool with a maximum number of workers specified at initialization' | |
207 | ) | |
208 | } | |
209 | } | |
210 | ||
c63a35a0 JB |
211 | private checkMinimumNumberOfWorkers ( |
212 | minimumNumberOfWorkers: number | undefined | |
213 | ): void { | |
af7f2788 | 214 | if (minimumNumberOfWorkers == null) { |
8d3782fa JB |
215 | throw new Error( |
216 | 'Cannot instantiate a pool without specifying the number of workers' | |
217 | ) | |
af7f2788 | 218 | } else if (!Number.isSafeInteger(minimumNumberOfWorkers)) { |
473c717a | 219 | throw new TypeError( |
0d80593b | 220 | 'Cannot instantiate a pool with a non safe integer number of workers' |
8d3782fa | 221 | ) |
af7f2788 | 222 | } else if (minimumNumberOfWorkers < 0) { |
473c717a | 223 | throw new RangeError( |
8d3782fa JB |
224 | 'Cannot instantiate a pool with a negative number of workers' |
225 | ) | |
af7f2788 | 226 | } else if (this.type === PoolTypes.fixed && minimumNumberOfWorkers === 0) { |
2431bdb4 JB |
227 | throw new RangeError('Cannot instantiate a fixed pool with zero worker') |
228 | } | |
229 | } | |
230 | ||
7c0ba920 | 231 | private checkPoolOptions (opts: PoolOptions<Worker>): void { |
0d80593b | 232 | if (isPlainObject(opts)) { |
47352846 | 233 | this.opts.startWorkers = opts.startWorkers ?? true |
c63a35a0 | 234 | checkValidWorkerChoiceStrategy(opts.workerChoiceStrategy) |
0d80593b JB |
235 | this.opts.workerChoiceStrategy = |
236 | opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN | |
2324f8c9 | 237 | this.checkValidWorkerChoiceStrategyOptions( |
c63a35a0 | 238 | opts.workerChoiceStrategyOptions |
2324f8c9 | 239 | ) |
26ce26ca JB |
240 | if (opts.workerChoiceStrategyOptions != null) { |
241 | this.opts.workerChoiceStrategyOptions = opts.workerChoiceStrategyOptions | |
8990357d | 242 | } |
1f68cede | 243 | this.opts.restartWorkerOnError = opts.restartWorkerOnError ?? true |
0d80593b JB |
244 | this.opts.enableEvents = opts.enableEvents ?? true |
245 | this.opts.enableTasksQueue = opts.enableTasksQueue ?? false | |
246 | if (this.opts.enableTasksQueue) { | |
c63a35a0 | 247 | checkValidTasksQueueOptions(opts.tasksQueueOptions) |
0d80593b | 248 | this.opts.tasksQueueOptions = this.buildTasksQueueOptions( |
c63a35a0 | 249 | opts.tasksQueueOptions |
0d80593b JB |
250 | ) |
251 | } | |
252 | } else { | |
253 | throw new TypeError('Invalid pool options: must be a plain object') | |
7171d33f | 254 | } |
aee46736 JB |
255 | } |
256 | ||
0d80593b | 257 | private checkValidWorkerChoiceStrategyOptions ( |
c63a35a0 | 258 | workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | undefined |
0d80593b | 259 | ): void { |
2324f8c9 JB |
260 | if ( |
261 | workerChoiceStrategyOptions != null && | |
262 | !isPlainObject(workerChoiceStrategyOptions) | |
263 | ) { | |
0d80593b JB |
264 | throw new TypeError( |
265 | 'Invalid worker choice strategy options: must be a plain object' | |
266 | ) | |
267 | } | |
49be33fe | 268 | if ( |
2324f8c9 | 269 | workerChoiceStrategyOptions?.weights != null && |
26ce26ca JB |
270 | Object.keys(workerChoiceStrategyOptions.weights).length !== |
271 | (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers) | |
49be33fe JB |
272 | ) { |
273 | throw new Error( | |
274 | 'Invalid worker choice strategy options: must have a weight for each worker node' | |
275 | ) | |
276 | } | |
f0d7f803 | 277 | if ( |
2324f8c9 | 278 | workerChoiceStrategyOptions?.measurement != null && |
f0d7f803 JB |
279 | !Object.values(Measurements).includes( |
280 | workerChoiceStrategyOptions.measurement | |
281 | ) | |
282 | ) { | |
283 | throw new Error( | |
284 | `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'` | |
285 | ) | |
286 | } | |
0d80593b JB |
287 | } |
288 | ||
e0843544 | 289 | private initEventEmitter (): void { |
5b7d00b4 | 290 | this.emitter = new EventEmitterAsyncResource({ |
b5604034 JB |
291 | name: `poolifier:${this.type}-${this.worker}-pool` |
292 | }) | |
293 | } | |
294 | ||
08f3f44c | 295 | /** @inheritDoc */ |
6b27d407 JB |
296 | public get info (): PoolInfo { |
297 | return { | |
23ccf9d7 | 298 | version, |
6b27d407 | 299 | type: this.type, |
184855e6 | 300 | worker: this.worker, |
47352846 | 301 | started: this.started, |
2431bdb4 | 302 | ready: this.ready, |
67f3f2d6 | 303 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
bcfb06ce JB |
304 | defaultStrategy: this.opts.workerChoiceStrategy!, |
305 | strategyRetries: this.workerChoiceStrategiesContext?.retriesCount ?? 0, | |
26ce26ca JB |
306 | minSize: this.minimumNumberOfWorkers, |
307 | maxSize: this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers, | |
bcfb06ce | 308 | ...(this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() |
f4d0a470 | 309 | .runTime.aggregate === true && |
bcfb06ce | 310 | this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() |
c63a35a0 JB |
311 | .waitTime.aggregate && { |
312 | utilization: round(this.utilization) | |
313 | }), | |
6b27d407 JB |
314 | workerNodes: this.workerNodes.length, |
315 | idleWorkerNodes: this.workerNodes.reduce( | |
316 | (accumulator, workerNode) => | |
f59e1027 | 317 | workerNode.usage.tasks.executing === 0 |
a4e07f72 JB |
318 | ? accumulator + 1 |
319 | : accumulator, | |
6b27d407 JB |
320 | 0 |
321 | ), | |
5eb72b9e JB |
322 | ...(this.opts.enableTasksQueue === true && { |
323 | stealingWorkerNodes: this.workerNodes.reduce( | |
324 | (accumulator, workerNode) => | |
325 | workerNode.info.stealing ? accumulator + 1 : accumulator, | |
326 | 0 | |
327 | ) | |
328 | }), | |
6b27d407 | 329 | busyWorkerNodes: this.workerNodes.reduce( |
533a8e22 | 330 | (accumulator, _, workerNodeKey) => |
42c677c1 | 331 | this.isWorkerNodeBusy(workerNodeKey) ? accumulator + 1 : accumulator, |
6b27d407 JB |
332 | 0 |
333 | ), | |
a4e07f72 | 334 | executedTasks: this.workerNodes.reduce( |
6b27d407 | 335 | (accumulator, workerNode) => |
f59e1027 | 336 | accumulator + workerNode.usage.tasks.executed, |
a4e07f72 JB |
337 | 0 |
338 | ), | |
339 | executingTasks: this.workerNodes.reduce( | |
340 | (accumulator, workerNode) => | |
f59e1027 | 341 | accumulator + workerNode.usage.tasks.executing, |
6b27d407 JB |
342 | 0 |
343 | ), | |
daf86646 JB |
344 | ...(this.opts.enableTasksQueue === true && { |
345 | queuedTasks: this.workerNodes.reduce( | |
346 | (accumulator, workerNode) => | |
347 | accumulator + workerNode.usage.tasks.queued, | |
348 | 0 | |
349 | ) | |
350 | }), | |
351 | ...(this.opts.enableTasksQueue === true && { | |
352 | maxQueuedTasks: this.workerNodes.reduce( | |
353 | (accumulator, workerNode) => | |
c63a35a0 | 354 | accumulator + (workerNode.usage.tasks.maxQueued ?? 0), |
daf86646 JB |
355 | 0 |
356 | ) | |
357 | }), | |
a1763c54 JB |
358 | ...(this.opts.enableTasksQueue === true && { |
359 | backPressure: this.hasBackPressure() | |
360 | }), | |
68cbdc84 JB |
361 | ...(this.opts.enableTasksQueue === true && { |
362 | stolenTasks: this.workerNodes.reduce( | |
363 | (accumulator, workerNode) => | |
364 | accumulator + workerNode.usage.tasks.stolen, | |
365 | 0 | |
366 | ) | |
367 | }), | |
a4e07f72 JB |
368 | failedTasks: this.workerNodes.reduce( |
369 | (accumulator, workerNode) => | |
f59e1027 | 370 | accumulator + workerNode.usage.tasks.failed, |
a4e07f72 | 371 | 0 |
1dcf8b7b | 372 | ), |
bcfb06ce | 373 | ...(this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() |
f4d0a470 | 374 | .runTime.aggregate === true && { |
1dcf8b7b | 375 | runTime: { |
98e72cda | 376 | minimum: round( |
90d6701c | 377 | min( |
98e72cda | 378 | ...this.workerNodes.map( |
80115618 JB |
379 | workerNode => |
380 | workerNode.usage.runTime.minimum ?? Number.POSITIVE_INFINITY | |
98e72cda | 381 | ) |
1dcf8b7b JB |
382 | ) |
383 | ), | |
98e72cda | 384 | maximum: round( |
90d6701c | 385 | max( |
98e72cda | 386 | ...this.workerNodes.map( |
80115618 JB |
387 | workerNode => |
388 | workerNode.usage.runTime.maximum ?? Number.NEGATIVE_INFINITY | |
98e72cda | 389 | ) |
1dcf8b7b | 390 | ) |
98e72cda | 391 | ), |
bcfb06ce | 392 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() |
3baa0837 JB |
393 | .runTime.average && { |
394 | average: round( | |
395 | average( | |
396 | this.workerNodes.reduce<number[]>( | |
397 | (accumulator, workerNode) => | |
398 | accumulator.concat(workerNode.usage.runTime.history), | |
399 | [] | |
400 | ) | |
98e72cda | 401 | ) |
dc021bcc | 402 | ) |
3baa0837 | 403 | }), |
bcfb06ce | 404 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() |
98e72cda JB |
405 | .runTime.median && { |
406 | median: round( | |
407 | median( | |
3baa0837 JB |
408 | this.workerNodes.reduce<number[]>( |
409 | (accumulator, workerNode) => | |
410 | accumulator.concat(workerNode.usage.runTime.history), | |
411 | [] | |
98e72cda JB |
412 | ) |
413 | ) | |
414 | ) | |
415 | }) | |
1dcf8b7b JB |
416 | } |
417 | }), | |
bcfb06ce | 418 | ...(this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() |
f4d0a470 | 419 | .waitTime.aggregate === true && { |
1dcf8b7b | 420 | waitTime: { |
98e72cda | 421 | minimum: round( |
90d6701c | 422 | min( |
98e72cda | 423 | ...this.workerNodes.map( |
80115618 JB |
424 | workerNode => |
425 | workerNode.usage.waitTime.minimum ?? Number.POSITIVE_INFINITY | |
98e72cda | 426 | ) |
1dcf8b7b JB |
427 | ) |
428 | ), | |
98e72cda | 429 | maximum: round( |
90d6701c | 430 | max( |
98e72cda | 431 | ...this.workerNodes.map( |
80115618 JB |
432 | workerNode => |
433 | workerNode.usage.waitTime.maximum ?? Number.NEGATIVE_INFINITY | |
98e72cda | 434 | ) |
1dcf8b7b | 435 | ) |
98e72cda | 436 | ), |
bcfb06ce | 437 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() |
3baa0837 JB |
438 | .waitTime.average && { |
439 | average: round( | |
440 | average( | |
441 | this.workerNodes.reduce<number[]>( | |
442 | (accumulator, workerNode) => | |
443 | accumulator.concat(workerNode.usage.waitTime.history), | |
444 | [] | |
445 | ) | |
98e72cda | 446 | ) |
dc021bcc | 447 | ) |
3baa0837 | 448 | }), |
bcfb06ce | 449 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() |
98e72cda JB |
450 | .waitTime.median && { |
451 | median: round( | |
452 | median( | |
3baa0837 JB |
453 | this.workerNodes.reduce<number[]>( |
454 | (accumulator, workerNode) => | |
455 | accumulator.concat(workerNode.usage.waitTime.history), | |
456 | [] | |
98e72cda JB |
457 | ) |
458 | ) | |
459 | ) | |
460 | }) | |
1dcf8b7b | 461 | } |
533a8e22 JB |
462 | }), |
463 | ...(this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() | |
464 | .elu.aggregate === true && { | |
465 | elu: { | |
466 | idle: { | |
467 | minimum: round( | |
468 | min( | |
469 | ...this.workerNodes.map( | |
80115618 JB |
470 | workerNode => |
471 | workerNode.usage.elu.idle.minimum ?? | |
472 | Number.POSITIVE_INFINITY | |
533a8e22 JB |
473 | ) |
474 | ) | |
475 | ), | |
476 | maximum: round( | |
477 | max( | |
478 | ...this.workerNodes.map( | |
80115618 JB |
479 | workerNode => |
480 | workerNode.usage.elu.idle.maximum ?? | |
481 | Number.NEGATIVE_INFINITY | |
533a8e22 JB |
482 | ) |
483 | ) | |
484 | ), | |
485 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() | |
486 | .elu.average && { | |
487 | average: round( | |
488 | average( | |
489 | this.workerNodes.reduce<number[]>( | |
490 | (accumulator, workerNode) => | |
491 | accumulator.concat(workerNode.usage.elu.idle.history), | |
492 | [] | |
493 | ) | |
494 | ) | |
495 | ) | |
496 | }), | |
497 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() | |
498 | .elu.median && { | |
499 | median: round( | |
500 | median( | |
501 | this.workerNodes.reduce<number[]>( | |
502 | (accumulator, workerNode) => | |
503 | accumulator.concat(workerNode.usage.elu.idle.history), | |
504 | [] | |
505 | ) | |
506 | ) | |
507 | ) | |
508 | }) | |
509 | }, | |
510 | active: { | |
511 | minimum: round( | |
512 | min( | |
513 | ...this.workerNodes.map( | |
80115618 JB |
514 | workerNode => |
515 | workerNode.usage.elu.active.minimum ?? | |
516 | Number.POSITIVE_INFINITY | |
533a8e22 JB |
517 | ) |
518 | ) | |
519 | ), | |
520 | maximum: round( | |
521 | max( | |
522 | ...this.workerNodes.map( | |
80115618 JB |
523 | workerNode => |
524 | workerNode.usage.elu.active.maximum ?? | |
525 | Number.NEGATIVE_INFINITY | |
533a8e22 JB |
526 | ) |
527 | ) | |
528 | ), | |
529 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() | |
530 | .elu.average && { | |
531 | average: round( | |
532 | average( | |
533 | this.workerNodes.reduce<number[]>( | |
534 | (accumulator, workerNode) => | |
535 | accumulator.concat(workerNode.usage.elu.active.history), | |
536 | [] | |
537 | ) | |
538 | ) | |
539 | ) | |
540 | }), | |
541 | ...(this.workerChoiceStrategiesContext.getTaskStatisticsRequirements() | |
542 | .elu.median && { | |
543 | median: round( | |
544 | median( | |
545 | this.workerNodes.reduce<number[]>( | |
546 | (accumulator, workerNode) => | |
547 | accumulator.concat(workerNode.usage.elu.active.history), | |
548 | [] | |
549 | ) | |
550 | ) | |
551 | ) | |
552 | }) | |
be202c2c JB |
553 | }, |
554 | utilization: { | |
555 | average: round( | |
556 | average( | |
557 | this.workerNodes.map( | |
558 | workerNode => workerNode.usage.elu.utilization ?? 0 | |
559 | ) | |
560 | ) | |
561 | ), | |
562 | median: round( | |
563 | median( | |
564 | this.workerNodes.map( | |
565 | workerNode => workerNode.usage.elu.utilization ?? 0 | |
566 | ) | |
567 | ) | |
568 | ) | |
533a8e22 JB |
569 | } |
570 | } | |
1dcf8b7b | 571 | }) |
6b27d407 JB |
572 | } |
573 | } | |
08f3f44c | 574 | |
aa9eede8 JB |
575 | /** |
576 | * The pool readiness boolean status. | |
577 | */ | |
2431bdb4 | 578 | private get ready (): boolean { |
8e8d9101 JB |
579 | if (this.empty) { |
580 | return false | |
581 | } | |
2431bdb4 | 582 | return ( |
b97d82d8 JB |
583 | this.workerNodes.reduce( |
584 | (accumulator, workerNode) => | |
585 | !workerNode.info.dynamic && workerNode.info.ready | |
586 | ? accumulator + 1 | |
587 | : accumulator, | |
588 | 0 | |
26ce26ca | 589 | ) >= this.minimumNumberOfWorkers |
2431bdb4 JB |
590 | ) |
591 | } | |
592 | ||
8e8d9101 JB |
593 | /** |
594 | * The pool emptiness boolean status. | |
595 | */ | |
596 | protected get empty (): boolean { | |
fb43035c | 597 | return this.minimumNumberOfWorkers === 0 && this.workerNodes.length === 0 |
8e8d9101 JB |
598 | } |
599 | ||
afe0d5bf | 600 | /** |
aa9eede8 | 601 | * The approximate pool utilization. |
afe0d5bf JB |
602 | * |
603 | * @returns The pool utilization. | |
604 | */ | |
605 | private get utilization (): number { | |
97dc65d9 JB |
606 | if (this.startTimestamp == null) { |
607 | return 0 | |
608 | } | |
8e5ca040 | 609 | const poolTimeCapacity = |
26ce26ca JB |
610 | (performance.now() - this.startTimestamp) * |
611 | (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers) | |
afe0d5bf JB |
612 | const totalTasksRunTime = this.workerNodes.reduce( |
613 | (accumulator, workerNode) => | |
c63a35a0 | 614 | accumulator + (workerNode.usage.runTime.aggregate ?? 0), |
afe0d5bf JB |
615 | 0 |
616 | ) | |
617 | const totalTasksWaitTime = this.workerNodes.reduce( | |
618 | (accumulator, workerNode) => | |
c63a35a0 | 619 | accumulator + (workerNode.usage.waitTime.aggregate ?? 0), |
afe0d5bf JB |
620 | 0 |
621 | ) | |
8e5ca040 | 622 | return (totalTasksRunTime + totalTasksWaitTime) / poolTimeCapacity |
afe0d5bf JB |
623 | } |
624 | ||
8881ae32 | 625 | /** |
aa9eede8 | 626 | * The pool type. |
8881ae32 JB |
627 | * |
628 | * If it is `'dynamic'`, it provides the `max` property. | |
629 | */ | |
630 | protected abstract get type (): PoolType | |
631 | ||
184855e6 | 632 | /** |
aa9eede8 | 633 | * The worker type. |
184855e6 JB |
634 | */ |
635 | protected abstract get worker (): WorkerType | |
636 | ||
6b813701 JB |
637 | /** |
638 | * Checks if the worker id sent in the received message from a worker is valid. | |
639 | * | |
640 | * @param message - The received message. | |
641 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid. | |
642 | */ | |
4d159167 | 643 | private checkMessageWorkerId (message: MessageValue<Data | Response>): void { |
310de0aa JB |
644 | if (message.workerId == null) { |
645 | throw new Error('Worker message received without worker id') | |
8e5a7cf9 | 646 | } else if (this.getWorkerNodeKeyByWorkerId(message.workerId) === -1) { |
21f710aa JB |
647 | throw new Error( |
648 | `Worker message received from unknown worker '${message.workerId}'` | |
649 | ) | |
650 | } | |
651 | } | |
652 | ||
aa9eede8 JB |
653 | /** |
654 | * Gets the worker node key given its worker id. | |
655 | * | |
656 | * @param workerId - The worker id. | |
aad6fb64 | 657 | * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise. |
aa9eede8 | 658 | */ |
72ae84a2 | 659 | private getWorkerNodeKeyByWorkerId (workerId: number | undefined): number { |
aad6fb64 | 660 | return this.workerNodes.findIndex( |
041dc05b | 661 | workerNode => workerNode.info.id === workerId |
aad6fb64 | 662 | ) |
aa9eede8 JB |
663 | } |
664 | ||
afc003b2 | 665 | /** @inheritDoc */ |
a35560ba | 666 | public setWorkerChoiceStrategy ( |
59219cbb JB |
667 | workerChoiceStrategy: WorkerChoiceStrategy, |
668 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
a35560ba | 669 | ): void { |
19b8be8b | 670 | let requireSync = false |
bde6b5d7 | 671 | checkValidWorkerChoiceStrategy(workerChoiceStrategy) |
85bbc7ab | 672 | if (workerChoiceStrategyOptions != null) { |
423f3b7a | 673 | requireSync = !this.setWorkerChoiceStrategyOptions( |
19b8be8b JB |
674 | workerChoiceStrategyOptions |
675 | ) | |
85bbc7ab JB |
676 | } |
677 | if (workerChoiceStrategy !== this.opts.workerChoiceStrategy) { | |
678 | this.opts.workerChoiceStrategy = workerChoiceStrategy | |
679 | this.workerChoiceStrategiesContext?.setDefaultWorkerChoiceStrategy( | |
680 | this.opts.workerChoiceStrategy, | |
681 | this.opts.workerChoiceStrategyOptions | |
682 | ) | |
19b8be8b JB |
683 | requireSync = true |
684 | } | |
685 | if (requireSync) { | |
85bbc7ab | 686 | this.workerChoiceStrategiesContext?.syncWorkerChoiceStrategies( |
91041638 | 687 | this.getWorkerChoiceStrategies(), |
85bbc7ab JB |
688 | this.opts.workerChoiceStrategyOptions |
689 | ) | |
19b8be8b | 690 | for (const workerNodeKey of this.workerNodes.keys()) { |
85bbc7ab JB |
691 | this.sendStatisticsMessageToWorker(workerNodeKey) |
692 | } | |
59219cbb | 693 | } |
a20f0ba5 JB |
694 | } |
695 | ||
696 | /** @inheritDoc */ | |
697 | public setWorkerChoiceStrategyOptions ( | |
c63a35a0 | 698 | workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | undefined |
19b8be8b | 699 | ): boolean { |
0d80593b | 700 | this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions) |
26ce26ca JB |
701 | if (workerChoiceStrategyOptions != null) { |
702 | this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions | |
19b8be8b JB |
703 | this.workerChoiceStrategiesContext?.setOptions( |
704 | this.opts.workerChoiceStrategyOptions | |
705 | ) | |
706 | this.workerChoiceStrategiesContext?.syncWorkerChoiceStrategies( | |
91041638 | 707 | this.getWorkerChoiceStrategies(), |
19b8be8b JB |
708 | this.opts.workerChoiceStrategyOptions |
709 | ) | |
710 | for (const workerNodeKey of this.workerNodes.keys()) { | |
711 | this.sendStatisticsMessageToWorker(workerNodeKey) | |
712 | } | |
713 | return true | |
8990357d | 714 | } |
19b8be8b | 715 | return false |
a35560ba S |
716 | } |
717 | ||
a20f0ba5 | 718 | /** @inheritDoc */ |
8f52842f JB |
719 | public enableTasksQueue ( |
720 | enable: boolean, | |
721 | tasksQueueOptions?: TasksQueueOptions | |
722 | ): void { | |
a20f0ba5 | 723 | if (this.opts.enableTasksQueue === true && !enable) { |
d6ca1416 JB |
724 | this.unsetTaskStealing() |
725 | this.unsetTasksStealingOnBackPressure() | |
ef41a6e6 | 726 | this.flushTasksQueues() |
a20f0ba5 JB |
727 | } |
728 | this.opts.enableTasksQueue = enable | |
c63a35a0 | 729 | this.setTasksQueueOptions(tasksQueueOptions) |
a20f0ba5 JB |
730 | } |
731 | ||
732 | /** @inheritDoc */ | |
c63a35a0 JB |
733 | public setTasksQueueOptions ( |
734 | tasksQueueOptions: TasksQueueOptions | undefined | |
735 | ): void { | |
a20f0ba5 | 736 | if (this.opts.enableTasksQueue === true) { |
bde6b5d7 | 737 | checkValidTasksQueueOptions(tasksQueueOptions) |
8f52842f JB |
738 | this.opts.tasksQueueOptions = |
739 | this.buildTasksQueueOptions(tasksQueueOptions) | |
67f3f2d6 JB |
740 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
741 | this.setTasksQueueSize(this.opts.tasksQueueOptions.size!) | |
d6ca1416 | 742 | if (this.opts.tasksQueueOptions.taskStealing === true) { |
9f99eb9b | 743 | this.unsetTaskStealing() |
d6ca1416 JB |
744 | this.setTaskStealing() |
745 | } else { | |
746 | this.unsetTaskStealing() | |
747 | } | |
748 | if (this.opts.tasksQueueOptions.tasksStealingOnBackPressure === true) { | |
9f99eb9b | 749 | this.unsetTasksStealingOnBackPressure() |
d6ca1416 JB |
750 | this.setTasksStealingOnBackPressure() |
751 | } else { | |
752 | this.unsetTasksStealingOnBackPressure() | |
753 | } | |
5baee0d7 | 754 | } else if (this.opts.tasksQueueOptions != null) { |
a20f0ba5 JB |
755 | delete this.opts.tasksQueueOptions |
756 | } | |
757 | } | |
758 | ||
9b38ab2d | 759 | private buildTasksQueueOptions ( |
c63a35a0 | 760 | tasksQueueOptions: TasksQueueOptions | undefined |
9b38ab2d JB |
761 | ): TasksQueueOptions { |
762 | return { | |
26ce26ca JB |
763 | ...getDefaultTasksQueueOptions( |
764 | this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers | |
765 | ), | |
9b38ab2d JB |
766 | ...tasksQueueOptions |
767 | } | |
768 | } | |
769 | ||
5b49e864 | 770 | private setTasksQueueSize (size: number): void { |
20c6f652 | 771 | for (const workerNode of this.workerNodes) { |
ff3f866a | 772 | workerNode.tasksQueueBackPressureSize = size |
20c6f652 JB |
773 | } |
774 | } | |
775 | ||
d6ca1416 | 776 | private setTaskStealing (): void { |
19b8be8b | 777 | for (const workerNodeKey of this.workerNodes.keys()) { |
e44639e9 | 778 | this.workerNodes[workerNodeKey].on('idle', this.handleWorkerNodeIdleEvent) |
d6ca1416 JB |
779 | } |
780 | } | |
781 | ||
782 | private unsetTaskStealing (): void { | |
19b8be8b | 783 | for (const workerNodeKey of this.workerNodes.keys()) { |
e1c2dba7 | 784 | this.workerNodes[workerNodeKey].off( |
e44639e9 JB |
785 | 'idle', |
786 | this.handleWorkerNodeIdleEvent | |
9f95d5eb | 787 | ) |
d6ca1416 JB |
788 | } |
789 | } | |
790 | ||
791 | private setTasksStealingOnBackPressure (): void { | |
19b8be8b | 792 | for (const workerNodeKey of this.workerNodes.keys()) { |
e1c2dba7 | 793 | this.workerNodes[workerNodeKey].on( |
b5e75be8 | 794 | 'backPressure', |
e44639e9 | 795 | this.handleWorkerNodeBackPressureEvent |
9f95d5eb | 796 | ) |
d6ca1416 JB |
797 | } |
798 | } | |
799 | ||
800 | private unsetTasksStealingOnBackPressure (): void { | |
19b8be8b | 801 | for (const workerNodeKey of this.workerNodes.keys()) { |
e1c2dba7 | 802 | this.workerNodes[workerNodeKey].off( |
b5e75be8 | 803 | 'backPressure', |
e44639e9 | 804 | this.handleWorkerNodeBackPressureEvent |
9f95d5eb | 805 | ) |
d6ca1416 JB |
806 | } |
807 | } | |
808 | ||
c319c66b JB |
809 | /** |
810 | * Whether the pool is full or not. | |
811 | * | |
812 | * The pool filling boolean status. | |
813 | */ | |
dea903a8 | 814 | protected get full (): boolean { |
26ce26ca JB |
815 | return ( |
816 | this.workerNodes.length >= | |
817 | (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers) | |
818 | ) | |
dea903a8 | 819 | } |
c2ade475 | 820 | |
c319c66b JB |
821 | /** |
822 | * Whether the pool is busy or not. | |
823 | * | |
824 | * The pool busyness boolean status. | |
825 | */ | |
826 | protected abstract get busy (): boolean | |
7c0ba920 | 827 | |
6c6afb84 | 828 | /** |
3d76750a | 829 | * Whether worker nodes are executing concurrently their tasks quota or not. |
6c6afb84 JB |
830 | * |
831 | * @returns Worker nodes busyness boolean status. | |
832 | */ | |
c2ade475 | 833 | protected internalBusy (): boolean { |
3d76750a JB |
834 | if (this.opts.enableTasksQueue === true) { |
835 | return ( | |
836 | this.workerNodes.findIndex( | |
041dc05b | 837 | workerNode => |
3d76750a JB |
838 | workerNode.info.ready && |
839 | workerNode.usage.tasks.executing < | |
67f3f2d6 JB |
840 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
841 | this.opts.tasksQueueOptions!.concurrency! | |
3d76750a JB |
842 | ) === -1 |
843 | ) | |
3d76750a | 844 | } |
419e8121 JB |
845 | return ( |
846 | this.workerNodes.findIndex( | |
847 | workerNode => | |
848 | workerNode.info.ready && workerNode.usage.tasks.executing === 0 | |
849 | ) === -1 | |
850 | ) | |
cb70b19d JB |
851 | } |
852 | ||
42c677c1 JB |
853 | private isWorkerNodeBusy (workerNodeKey: number): boolean { |
854 | if (this.opts.enableTasksQueue === true) { | |
855 | return ( | |
856 | this.workerNodes[workerNodeKey].usage.tasks.executing >= | |
67f3f2d6 JB |
857 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
858 | this.opts.tasksQueueOptions!.concurrency! | |
42c677c1 JB |
859 | ) |
860 | } | |
861 | return this.workerNodes[workerNodeKey].usage.tasks.executing > 0 | |
862 | } | |
863 | ||
e81c38f2 | 864 | private async sendTaskFunctionOperationToWorker ( |
72ae84a2 JB |
865 | workerNodeKey: number, |
866 | message: MessageValue<Data> | |
867 | ): Promise<boolean> { | |
72ae84a2 | 868 | return await new Promise<boolean>((resolve, reject) => { |
ae036c3e JB |
869 | const taskFunctionOperationListener = ( |
870 | message: MessageValue<Response> | |
871 | ): void => { | |
872 | this.checkMessageWorkerId(message) | |
1851fed0 | 873 | const workerId = this.getWorkerInfo(workerNodeKey)?.id |
72ae84a2 | 874 | if ( |
ae036c3e JB |
875 | message.taskFunctionOperationStatus != null && |
876 | message.workerId === workerId | |
72ae84a2 | 877 | ) { |
ae036c3e JB |
878 | if (message.taskFunctionOperationStatus) { |
879 | resolve(true) | |
c63a35a0 | 880 | } else { |
ae036c3e JB |
881 | reject( |
882 | new Error( | |
c63a35a0 | 883 | `Task function operation '${message.taskFunctionOperation}' failed on worker ${message.workerId} with error: '${message.workerError?.message}'` |
ae036c3e | 884 | ) |
72ae84a2 | 885 | ) |
ae036c3e JB |
886 | } |
887 | this.deregisterWorkerMessageListener( | |
888 | this.getWorkerNodeKeyByWorkerId(message.workerId), | |
889 | taskFunctionOperationListener | |
72ae84a2 JB |
890 | ) |
891 | } | |
ae036c3e JB |
892 | } |
893 | this.registerWorkerMessageListener( | |
894 | workerNodeKey, | |
895 | taskFunctionOperationListener | |
896 | ) | |
72ae84a2 JB |
897 | this.sendToWorker(workerNodeKey, message) |
898 | }) | |
899 | } | |
900 | ||
901 | private async sendTaskFunctionOperationToWorkers ( | |
adee6053 | 902 | message: MessageValue<Data> |
e81c38f2 JB |
903 | ): Promise<boolean> { |
904 | return await new Promise<boolean>((resolve, reject) => { | |
ae036c3e JB |
905 | const responsesReceived = new Array<MessageValue<Response>>() |
906 | const taskFunctionOperationsListener = ( | |
907 | message: MessageValue<Response> | |
908 | ): void => { | |
909 | this.checkMessageWorkerId(message) | |
910 | if (message.taskFunctionOperationStatus != null) { | |
911 | responsesReceived.push(message) | |
912 | if (responsesReceived.length === this.workerNodes.length) { | |
e81c38f2 | 913 | if ( |
e81c38f2 JB |
914 | responsesReceived.every( |
915 | message => message.taskFunctionOperationStatus === true | |
916 | ) | |
917 | ) { | |
918 | resolve(true) | |
919 | } else if ( | |
e81c38f2 JB |
920 | responsesReceived.some( |
921 | message => message.taskFunctionOperationStatus === false | |
922 | ) | |
923 | ) { | |
b0b55f57 JB |
924 | const errorResponse = responsesReceived.find( |
925 | response => response.taskFunctionOperationStatus === false | |
926 | ) | |
e81c38f2 JB |
927 | reject( |
928 | new Error( | |
b0b55f57 | 929 | `Task function operation '${ |
e81c38f2 | 930 | message.taskFunctionOperation as string |
c63a35a0 JB |
931 | }' failed on worker ${errorResponse?.workerId} with error: '${ |
932 | errorResponse?.workerError?.message | |
b0b55f57 | 933 | }'` |
e81c38f2 JB |
934 | ) |
935 | ) | |
936 | } | |
ae036c3e JB |
937 | this.deregisterWorkerMessageListener( |
938 | this.getWorkerNodeKeyByWorkerId(message.workerId), | |
939 | taskFunctionOperationsListener | |
940 | ) | |
e81c38f2 | 941 | } |
ae036c3e JB |
942 | } |
943 | } | |
19b8be8b | 944 | for (const workerNodeKey of this.workerNodes.keys()) { |
ae036c3e JB |
945 | this.registerWorkerMessageListener( |
946 | workerNodeKey, | |
947 | taskFunctionOperationsListener | |
948 | ) | |
72ae84a2 | 949 | this.sendToWorker(workerNodeKey, message) |
e81c38f2 JB |
950 | } |
951 | }) | |
6703b9f4 JB |
952 | } |
953 | ||
954 | /** @inheritDoc */ | |
955 | public hasTaskFunction (name: string): boolean { | |
85bbc7ab JB |
956 | return this.listTaskFunctionsProperties().some( |
957 | taskFunctionProperties => taskFunctionProperties.name === name | |
958 | ) | |
6703b9f4 JB |
959 | } |
960 | ||
961 | /** @inheritDoc */ | |
e81c38f2 JB |
962 | public async addTaskFunction ( |
963 | name: string, | |
31847469 | 964 | fn: TaskFunction<Data, Response> | TaskFunctionObject<Data, Response> |
e81c38f2 | 965 | ): Promise<boolean> { |
3feeab69 JB |
966 | if (typeof name !== 'string') { |
967 | throw new TypeError('name argument must be a string') | |
968 | } | |
969 | if (typeof name === 'string' && name.trim().length === 0) { | |
970 | throw new TypeError('name argument must not be an empty string') | |
971 | } | |
31847469 JB |
972 | if (typeof fn === 'function') { |
973 | fn = { taskFunction: fn } satisfies TaskFunctionObject<Data, Response> | |
974 | } | |
975 | if (typeof fn.taskFunction !== 'function') { | |
976 | throw new TypeError('taskFunction property must be a function') | |
3feeab69 | 977 | } |
95d1a734 JB |
978 | checkValidPriority(fn.priority) |
979 | checkValidWorkerChoiceStrategy(fn.strategy) | |
adee6053 | 980 | const opResult = await this.sendTaskFunctionOperationToWorkers({ |
6703b9f4 | 981 | taskFunctionOperation: 'add', |
31847469 JB |
982 | taskFunctionProperties: buildTaskFunctionProperties(name, fn), |
983 | taskFunction: fn.taskFunction.toString() | |
6703b9f4 | 984 | }) |
adee6053 | 985 | this.taskFunctions.set(name, fn) |
85bbc7ab | 986 | this.workerChoiceStrategiesContext?.syncWorkerChoiceStrategies( |
91041638 | 987 | this.getWorkerChoiceStrategies() |
85bbc7ab | 988 | ) |
9a55fa8c JB |
989 | for (const workerNodeKey of this.workerNodes.keys()) { |
990 | this.sendStatisticsMessageToWorker(workerNodeKey) | |
991 | } | |
adee6053 | 992 | return opResult |
6703b9f4 JB |
993 | } |
994 | ||
995 | /** @inheritDoc */ | |
e81c38f2 | 996 | public async removeTaskFunction (name: string): Promise<boolean> { |
9eae3c69 JB |
997 | if (!this.taskFunctions.has(name)) { |
998 | throw new Error( | |
16248b23 | 999 | 'Cannot remove a task function not handled on the pool side' |
9eae3c69 JB |
1000 | ) |
1001 | } | |
adee6053 | 1002 | const opResult = await this.sendTaskFunctionOperationToWorkers({ |
6703b9f4 | 1003 | taskFunctionOperation: 'remove', |
31847469 JB |
1004 | taskFunctionProperties: buildTaskFunctionProperties( |
1005 | name, | |
1006 | this.taskFunctions.get(name) | |
1007 | ) | |
6703b9f4 | 1008 | }) |
9a55fa8c JB |
1009 | for (const workerNode of this.workerNodes) { |
1010 | workerNode.deleteTaskFunctionWorkerUsage(name) | |
1011 | } | |
adee6053 | 1012 | this.taskFunctions.delete(name) |
85bbc7ab | 1013 | this.workerChoiceStrategiesContext?.syncWorkerChoiceStrategies( |
91041638 | 1014 | this.getWorkerChoiceStrategies() |
85bbc7ab | 1015 | ) |
9a55fa8c JB |
1016 | for (const workerNodeKey of this.workerNodes.keys()) { |
1017 | this.sendStatisticsMessageToWorker(workerNodeKey) | |
1018 | } | |
adee6053 | 1019 | return opResult |
6703b9f4 JB |
1020 | } |
1021 | ||
90d7d101 | 1022 | /** @inheritDoc */ |
31847469 | 1023 | public listTaskFunctionsProperties (): TaskFunctionProperties[] { |
f2dbbf95 JB |
1024 | for (const workerNode of this.workerNodes) { |
1025 | if ( | |
31847469 JB |
1026 | Array.isArray(workerNode.info.taskFunctionsProperties) && |
1027 | workerNode.info.taskFunctionsProperties.length > 0 | |
f2dbbf95 | 1028 | ) { |
31847469 | 1029 | return workerNode.info.taskFunctionsProperties |
f2dbbf95 | 1030 | } |
90d7d101 | 1031 | } |
f2dbbf95 | 1032 | return [] |
90d7d101 JB |
1033 | } |
1034 | ||
bcfb06ce | 1035 | /** |
91041638 | 1036 | * Gets task function worker choice strategy, if any. |
bcfb06ce JB |
1037 | * |
1038 | * @param name - The task function name. | |
1039 | * @returns The task function worker choice strategy if the task function worker choice strategy is defined, `undefined` otherwise. | |
1040 | */ | |
91041638 | 1041 | private readonly getTaskFunctionWorkerChoiceStrategy = ( |
bcfb06ce JB |
1042 | name?: string |
1043 | ): WorkerChoiceStrategy | undefined => { | |
91041638 JB |
1044 | name = name ?? DEFAULT_TASK_NAME |
1045 | const taskFunctionsProperties = this.listTaskFunctionsProperties() | |
1046 | if (name === DEFAULT_TASK_NAME) { | |
1047 | name = taskFunctionsProperties[1]?.name | |
bcfb06ce | 1048 | } |
91041638 JB |
1049 | return taskFunctionsProperties.find( |
1050 | (taskFunctionProperties: TaskFunctionProperties) => | |
1051 | taskFunctionProperties.name === name | |
1052 | )?.strategy | |
1053 | } | |
1054 | ||
1055 | /** | |
1056 | * Gets worker node task function worker choice strategy, if any. | |
1057 | * | |
1058 | * @param workerNodeKey - The worker node key. | |
1059 | * @param name - The task function name. | |
1060 | * @returns The worker node task function worker choice strategy if the worker node task function worker choice strategy is defined, `undefined` otherwise. | |
1061 | */ | |
1062 | private readonly getWorkerNodeTaskFunctionWorkerChoiceStrategy = ( | |
1063 | workerNodeKey: number, | |
1064 | name?: string | |
1065 | ): WorkerChoiceStrategy | undefined => { | |
1066 | const workerInfo = this.getWorkerInfo(workerNodeKey) | |
1067 | if (workerInfo == null) { | |
1068 | return | |
1069 | } | |
1070 | name = name ?? DEFAULT_TASK_NAME | |
1071 | if (name === DEFAULT_TASK_NAME) { | |
1072 | name = workerInfo.taskFunctionsProperties?.[1]?.name | |
1073 | } | |
1074 | return workerInfo.taskFunctionsProperties?.find( | |
1075 | (taskFunctionProperties: TaskFunctionProperties) => | |
1076 | taskFunctionProperties.name === name | |
1077 | )?.strategy | |
bcfb06ce JB |
1078 | } |
1079 | ||
95d1a734 JB |
1080 | /** |
1081 | * Gets worker node task function priority, if any. | |
1082 | * | |
1083 | * @param workerNodeKey - The worker node key. | |
1084 | * @param name - The task function name. | |
a20062fa | 1085 | * @returns The worker node task function priority if the worker node task function priority is defined, `undefined` otherwise. |
95d1a734 JB |
1086 | */ |
1087 | private readonly getWorkerNodeTaskFunctionPriority = ( | |
1088 | workerNodeKey: number, | |
1089 | name?: string | |
1090 | ): number | undefined => { | |
91041638 JB |
1091 | const workerInfo = this.getWorkerInfo(workerNodeKey) |
1092 | if (workerInfo == null) { | |
1093 | return | |
95d1a734 | 1094 | } |
91041638 JB |
1095 | name = name ?? DEFAULT_TASK_NAME |
1096 | if (name === DEFAULT_TASK_NAME) { | |
1097 | name = workerInfo.taskFunctionsProperties?.[1]?.name | |
1098 | } | |
1099 | return workerInfo.taskFunctionsProperties?.find( | |
1100 | (taskFunctionProperties: TaskFunctionProperties) => | |
1101 | taskFunctionProperties.name === name | |
1102 | )?.priority | |
95d1a734 JB |
1103 | } |
1104 | ||
85bbc7ab JB |
1105 | /** |
1106 | * Gets the worker choice strategies registered in this pool. | |
1107 | * | |
1108 | * @returns The worker choice strategies. | |
1109 | */ | |
91041638 | 1110 | private readonly getWorkerChoiceStrategies = |
85bbc7ab JB |
1111 | (): Set<WorkerChoiceStrategy> => { |
1112 | return new Set([ | |
1113 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1114 | this.opts.workerChoiceStrategy!, | |
1115 | ...(this.listTaskFunctionsProperties() | |
1116 | .map( | |
1117 | (taskFunctionProperties: TaskFunctionProperties) => | |
1118 | taskFunctionProperties.strategy | |
1119 | ) | |
1120 | .filter( | |
1121 | (strategy: WorkerChoiceStrategy | undefined) => strategy != null | |
1122 | ) as WorkerChoiceStrategy[]) | |
1123 | ]) | |
1124 | } | |
1125 | ||
6703b9f4 | 1126 | /** @inheritDoc */ |
e81c38f2 | 1127 | public async setDefaultTaskFunction (name: string): Promise<boolean> { |
72ae84a2 | 1128 | return await this.sendTaskFunctionOperationToWorkers({ |
6703b9f4 | 1129 | taskFunctionOperation: 'default', |
31847469 JB |
1130 | taskFunctionProperties: buildTaskFunctionProperties( |
1131 | name, | |
1132 | this.taskFunctions.get(name) | |
1133 | ) | |
6703b9f4 | 1134 | }) |
6703b9f4 JB |
1135 | } |
1136 | ||
375f7504 JB |
1137 | private shallExecuteTask (workerNodeKey: number): boolean { |
1138 | return ( | |
1139 | this.tasksQueueSize(workerNodeKey) === 0 && | |
1140 | this.workerNodes[workerNodeKey].usage.tasks.executing < | |
67f3f2d6 JB |
1141 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1142 | this.opts.tasksQueueOptions!.concurrency! | |
375f7504 JB |
1143 | ) |
1144 | } | |
1145 | ||
afc003b2 | 1146 | /** @inheritDoc */ |
7d91a8cd JB |
1147 | public async execute ( |
1148 | data?: Data, | |
1149 | name?: string, | |
6a3ecc50 | 1150 | transferList?: readonly TransferListItem[] |
7d91a8cd | 1151 | ): Promise<Response> { |
52b71763 | 1152 | return await new Promise<Response>((resolve, reject) => { |
15b176e0 | 1153 | if (!this.started) { |
47352846 | 1154 | reject(new Error('Cannot execute a task on not started pool')) |
9d2d0da1 | 1155 | return |
15b176e0 | 1156 | } |
711623b8 JB |
1157 | if (this.destroying) { |
1158 | reject(new Error('Cannot execute a task on destroying pool')) | |
1159 | return | |
1160 | } | |
7d91a8cd JB |
1161 | if (name != null && typeof name !== 'string') { |
1162 | reject(new TypeError('name argument must be a string')) | |
9d2d0da1 | 1163 | return |
7d91a8cd | 1164 | } |
90d7d101 JB |
1165 | if ( |
1166 | name != null && | |
1167 | typeof name === 'string' && | |
1168 | name.trim().length === 0 | |
1169 | ) { | |
f58b60b9 | 1170 | reject(new TypeError('name argument must not be an empty string')) |
9d2d0da1 | 1171 | return |
90d7d101 | 1172 | } |
b558f6b5 JB |
1173 | if (transferList != null && !Array.isArray(transferList)) { |
1174 | reject(new TypeError('transferList argument must be an array')) | |
9d2d0da1 | 1175 | return |
b558f6b5 JB |
1176 | } |
1177 | const timestamp = performance.now() | |
91041638 | 1178 | const workerNodeKey = this.chooseWorkerNode(name) |
501aea93 | 1179 | const task: Task<Data> = { |
52b71763 JB |
1180 | name: name ?? DEFAULT_TASK_NAME, |
1181 | // eslint-disable-next-line @typescript-eslint/consistent-type-assertions | |
1182 | data: data ?? ({} as Data), | |
95d1a734 | 1183 | priority: this.getWorkerNodeTaskFunctionPriority(workerNodeKey, name), |
91041638 JB |
1184 | strategy: this.getWorkerNodeTaskFunctionWorkerChoiceStrategy( |
1185 | workerNodeKey, | |
1186 | name | |
1187 | ), | |
7d91a8cd | 1188 | transferList, |
52b71763 | 1189 | timestamp, |
7629bdf1 | 1190 | taskId: randomUUID() |
52b71763 | 1191 | } |
67f3f2d6 JB |
1192 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1193 | this.promiseResponseMap.set(task.taskId!, { | |
2e81254d JB |
1194 | resolve, |
1195 | reject, | |
f18fd12b JB |
1196 | workerNodeKey, |
1197 | ...(this.emitter != null && { | |
1198 | asyncResource: new AsyncResource('poolifier:task', { | |
1199 | triggerAsyncId: this.emitter.asyncId, | |
1200 | requireManualDestroy: true | |
1201 | }) | |
1202 | }) | |
2e81254d | 1203 | }) |
52b71763 | 1204 | if ( |
4e377863 JB |
1205 | this.opts.enableTasksQueue === false || |
1206 | (this.opts.enableTasksQueue === true && | |
375f7504 | 1207 | this.shallExecuteTask(workerNodeKey)) |
52b71763 | 1208 | ) { |
501aea93 | 1209 | this.executeTask(workerNodeKey, task) |
4e377863 JB |
1210 | } else { |
1211 | this.enqueueTask(workerNodeKey, task) | |
52b71763 | 1212 | } |
2e81254d | 1213 | }) |
280c2a77 | 1214 | } |
c97c7edb | 1215 | |
60dc0a9c JB |
1216 | /** |
1217 | * Starts the minimum number of workers. | |
1218 | */ | |
e0843544 | 1219 | private startMinimumNumberOfWorkers (initWorkerNodeUsage = false): void { |
b362a929 | 1220 | this.startingMinimumNumberOfWorkers = true |
60dc0a9c JB |
1221 | while ( |
1222 | this.workerNodes.reduce( | |
1223 | (accumulator, workerNode) => | |
1224 | !workerNode.info.dynamic ? accumulator + 1 : accumulator, | |
1225 | 0 | |
1226 | ) < this.minimumNumberOfWorkers | |
1227 | ) { | |
e0843544 JB |
1228 | const workerNodeKey = this.createAndSetupWorkerNode() |
1229 | initWorkerNodeUsage && | |
1230 | this.initWorkerNodeUsage(this.workerNodes[workerNodeKey]) | |
60dc0a9c | 1231 | } |
b362a929 | 1232 | this.startingMinimumNumberOfWorkers = false |
60dc0a9c JB |
1233 | } |
1234 | ||
47352846 JB |
1235 | /** @inheritdoc */ |
1236 | public start (): void { | |
711623b8 JB |
1237 | if (this.started) { |
1238 | throw new Error('Cannot start an already started pool') | |
1239 | } | |
1240 | if (this.starting) { | |
1241 | throw new Error('Cannot start an already starting pool') | |
1242 | } | |
1243 | if (this.destroying) { | |
1244 | throw new Error('Cannot start a destroying pool') | |
1245 | } | |
47352846 | 1246 | this.starting = true |
60dc0a9c | 1247 | this.startMinimumNumberOfWorkers() |
2008bccd | 1248 | this.startTimestamp = performance.now() |
47352846 JB |
1249 | this.starting = false |
1250 | this.started = true | |
1251 | } | |
1252 | ||
afc003b2 | 1253 | /** @inheritDoc */ |
c97c7edb | 1254 | public async destroy (): Promise<void> { |
711623b8 JB |
1255 | if (!this.started) { |
1256 | throw new Error('Cannot destroy an already destroyed pool') | |
1257 | } | |
1258 | if (this.starting) { | |
1259 | throw new Error('Cannot destroy an starting pool') | |
1260 | } | |
1261 | if (this.destroying) { | |
1262 | throw new Error('Cannot destroy an already destroying pool') | |
1263 | } | |
1264 | this.destroying = true | |
1fbcaa7c | 1265 | await Promise.all( |
533a8e22 | 1266 | this.workerNodes.map(async (_, workerNodeKey) => { |
aa9eede8 | 1267 | await this.destroyWorkerNode(workerNodeKey) |
1fbcaa7c JB |
1268 | }) |
1269 | ) | |
33e6bb4c | 1270 | this.emitter?.emit(PoolEvents.destroy, this.info) |
f80125ca | 1271 | this.emitter?.emitDestroy() |
55082af9 | 1272 | this.readyEventEmitted = false |
2008bccd | 1273 | delete this.startTimestamp |
711623b8 | 1274 | this.destroying = false |
15b176e0 | 1275 | this.started = false |
c97c7edb S |
1276 | } |
1277 | ||
26ce26ca | 1278 | private async sendKillMessageToWorker (workerNodeKey: number): Promise<void> { |
9edb9717 | 1279 | await new Promise<void>((resolve, reject) => { |
c63a35a0 JB |
1280 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
1281 | if (this.workerNodes[workerNodeKey] == null) { | |
ad3836ed | 1282 | resolve() |
85b2561d JB |
1283 | return |
1284 | } | |
ae036c3e JB |
1285 | const killMessageListener = (message: MessageValue<Response>): void => { |
1286 | this.checkMessageWorkerId(message) | |
1e3214b6 JB |
1287 | if (message.kill === 'success') { |
1288 | resolve() | |
1289 | } else if (message.kill === 'failure') { | |
72ae84a2 JB |
1290 | reject( |
1291 | new Error( | |
c63a35a0 | 1292 | `Kill message handling failed on worker ${message.workerId}` |
72ae84a2 JB |
1293 | ) |
1294 | ) | |
1e3214b6 | 1295 | } |
ae036c3e | 1296 | } |
51d9cfbd | 1297 | // FIXME: should be registered only once |
ae036c3e | 1298 | this.registerWorkerMessageListener(workerNodeKey, killMessageListener) |
72ae84a2 | 1299 | this.sendToWorker(workerNodeKey, { kill: true }) |
1e3214b6 | 1300 | }) |
1e3214b6 JB |
1301 | } |
1302 | ||
4a6952ff | 1303 | /** |
aa9eede8 | 1304 | * Terminates the worker node given its worker node key. |
4a6952ff | 1305 | * |
aa9eede8 | 1306 | * @param workerNodeKey - The worker node key. |
4a6952ff | 1307 | */ |
07e0c9e5 JB |
1308 | protected async destroyWorkerNode (workerNodeKey: number): Promise<void> { |
1309 | this.flagWorkerNodeAsNotReady(workerNodeKey) | |
87347ea8 | 1310 | const flushedTasks = this.flushTasksQueue(workerNodeKey) |
07e0c9e5 | 1311 | const workerNode = this.workerNodes[workerNodeKey] |
32b141fd JB |
1312 | await waitWorkerNodeEvents( |
1313 | workerNode, | |
1314 | 'taskFinished', | |
1315 | flushedTasks, | |
1316 | this.opts.tasksQueueOptions?.tasksFinishedTimeout ?? | |
26ce26ca JB |
1317 | getDefaultTasksQueueOptions( |
1318 | this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers | |
1319 | ).tasksFinishedTimeout | |
32b141fd | 1320 | ) |
07e0c9e5 JB |
1321 | await this.sendKillMessageToWorker(workerNodeKey) |
1322 | await workerNode.terminate() | |
1323 | } | |
c97c7edb | 1324 | |
729c563d | 1325 | /** |
6677a3d3 JB |
1326 | * Setup hook to execute code before worker nodes are created in the abstract constructor. |
1327 | * Can be overridden. | |
afc003b2 JB |
1328 | * |
1329 | * @virtual | |
729c563d | 1330 | */ |
280c2a77 | 1331 | protected setupHook (): void { |
965df41c | 1332 | /* Intentionally empty */ |
280c2a77 | 1333 | } |
c97c7edb | 1334 | |
729c563d | 1335 | /** |
5bec72fd JB |
1336 | * Returns whether the worker is the main worker or not. |
1337 | * | |
1338 | * @returns `true` if the worker is the main worker, `false` otherwise. | |
280c2a77 S |
1339 | */ |
1340 | protected abstract isMain (): boolean | |
1341 | ||
1342 | /** | |
2e81254d | 1343 | * Hook executed before the worker task execution. |
bf9549ae | 1344 | * Can be overridden. |
729c563d | 1345 | * |
f06e48d8 | 1346 | * @param workerNodeKey - The worker node key. |
1c6fe997 | 1347 | * @param task - The task to execute. |
729c563d | 1348 | */ |
1c6fe997 JB |
1349 | protected beforeTaskExecutionHook ( |
1350 | workerNodeKey: number, | |
1351 | task: Task<Data> | |
1352 | ): void { | |
c63a35a0 | 1353 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
efabc98d | 1354 | if (this.workerNodes[workerNodeKey]?.usage != null) { |
94407def JB |
1355 | const workerUsage = this.workerNodes[workerNodeKey].usage |
1356 | ++workerUsage.tasks.executing | |
c329fd41 | 1357 | updateWaitTimeWorkerUsage( |
bcfb06ce | 1358 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1359 | workerUsage, |
1360 | task | |
1361 | ) | |
94407def JB |
1362 | } |
1363 | if ( | |
1364 | this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && | |
67f3f2d6 JB |
1365 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1366 | this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage(task.name!) != | |
1367 | null | |
94407def | 1368 | ) { |
67f3f2d6 | 1369 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
db0e38ee | 1370 | const taskFunctionWorkerUsage = this.workerNodes[ |
b558f6b5 | 1371 | workerNodeKey |
67f3f2d6 JB |
1372 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1373 | ].getTaskFunctionWorkerUsage(task.name!)! | |
5623b8d5 | 1374 | ++taskFunctionWorkerUsage.tasks.executing |
c329fd41 | 1375 | updateWaitTimeWorkerUsage( |
bcfb06ce | 1376 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1377 | taskFunctionWorkerUsage, |
1378 | task | |
1379 | ) | |
b558f6b5 | 1380 | } |
c97c7edb S |
1381 | } |
1382 | ||
c01733f1 | 1383 | /** |
2e81254d | 1384 | * Hook executed after the worker task execution. |
bf9549ae | 1385 | * Can be overridden. |
c01733f1 | 1386 | * |
501aea93 | 1387 | * @param workerNodeKey - The worker node key. |
38e795c1 | 1388 | * @param message - The received message. |
c01733f1 | 1389 | */ |
2e81254d | 1390 | protected afterTaskExecutionHook ( |
501aea93 | 1391 | workerNodeKey: number, |
2740a743 | 1392 | message: MessageValue<Response> |
bf9549ae | 1393 | ): void { |
9a55fa8c | 1394 | let needWorkerChoiceStrategiesUpdate = false |
c63a35a0 | 1395 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
efabc98d | 1396 | if (this.workerNodes[workerNodeKey]?.usage != null) { |
94407def | 1397 | const workerUsage = this.workerNodes[workerNodeKey].usage |
c329fd41 JB |
1398 | updateTaskStatisticsWorkerUsage(workerUsage, message) |
1399 | updateRunTimeWorkerUsage( | |
bcfb06ce | 1400 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1401 | workerUsage, |
1402 | message | |
1403 | ) | |
1404 | updateEluWorkerUsage( | |
bcfb06ce | 1405 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1406 | workerUsage, |
1407 | message | |
1408 | ) | |
9a55fa8c | 1409 | needWorkerChoiceStrategiesUpdate = true |
94407def JB |
1410 | } |
1411 | if ( | |
1412 | this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && | |
1413 | this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage( | |
67f3f2d6 JB |
1414 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1415 | message.taskPerformance!.name | |
94407def JB |
1416 | ) != null |
1417 | ) { | |
67f3f2d6 | 1418 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
db0e38ee | 1419 | const taskFunctionWorkerUsage = this.workerNodes[ |
b558f6b5 | 1420 | workerNodeKey |
67f3f2d6 JB |
1421 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1422 | ].getTaskFunctionWorkerUsage(message.taskPerformance!.name)! | |
c329fd41 JB |
1423 | updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage, message) |
1424 | updateRunTimeWorkerUsage( | |
bcfb06ce | 1425 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1426 | taskFunctionWorkerUsage, |
1427 | message | |
1428 | ) | |
1429 | updateEluWorkerUsage( | |
bcfb06ce | 1430 | this.workerChoiceStrategiesContext, |
c329fd41 JB |
1431 | taskFunctionWorkerUsage, |
1432 | message | |
1433 | ) | |
9a55fa8c | 1434 | needWorkerChoiceStrategiesUpdate = true |
c329fd41 | 1435 | } |
9a55fa8c | 1436 | if (needWorkerChoiceStrategiesUpdate) { |
bcfb06ce | 1437 | this.workerChoiceStrategiesContext?.update(workerNodeKey) |
b558f6b5 JB |
1438 | } |
1439 | } | |
1440 | ||
db0e38ee JB |
1441 | /** |
1442 | * Whether the worker node shall update its task function worker usage or not. | |
1443 | * | |
1444 | * @param workerNodeKey - The worker node key. | |
1445 | * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise. | |
1446 | */ | |
1447 | private shallUpdateTaskFunctionWorkerUsage (workerNodeKey: number): boolean { | |
a5d15204 | 1448 | const workerInfo = this.getWorkerInfo(workerNodeKey) |
b558f6b5 | 1449 | return ( |
1851fed0 | 1450 | workerInfo != null && |
31847469 JB |
1451 | Array.isArray(workerInfo.taskFunctionsProperties) && |
1452 | workerInfo.taskFunctionsProperties.length > 2 | |
b558f6b5 | 1453 | ) |
f1c06930 JB |
1454 | } |
1455 | ||
280c2a77 | 1456 | /** |
91041638 | 1457 | * Chooses a worker node for the next task. |
280c2a77 | 1458 | * |
91041638 | 1459 | * @param name - The task function name. |
aa9eede8 | 1460 | * @returns The chosen worker node key |
280c2a77 | 1461 | */ |
91041638 | 1462 | private chooseWorkerNode (name?: string): number { |
930dcf12 | 1463 | if (this.shallCreateDynamicWorker()) { |
aa9eede8 | 1464 | const workerNodeKey = this.createAndSetupDynamicWorkerNode() |
6c6afb84 | 1465 | if ( |
bcfb06ce JB |
1466 | this.workerChoiceStrategiesContext?.getPolicy().dynamicWorkerUsage === |
1467 | true | |
6c6afb84 | 1468 | ) { |
aa9eede8 | 1469 | return workerNodeKey |
6c6afb84 | 1470 | } |
17393ac8 | 1471 | } |
c63a35a0 | 1472 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
91041638 JB |
1473 | return this.workerChoiceStrategiesContext!.execute( |
1474 | this.getTaskFunctionWorkerChoiceStrategy(name) | |
1475 | ) | |
930dcf12 JB |
1476 | } |
1477 | ||
6c6afb84 JB |
1478 | /** |
1479 | * Conditions for dynamic worker creation. | |
1480 | * | |
1481 | * @returns Whether to create a dynamic worker or not. | |
1482 | */ | |
9d9fb7b6 | 1483 | protected abstract shallCreateDynamicWorker (): boolean |
c97c7edb | 1484 | |
280c2a77 | 1485 | /** |
aa9eede8 | 1486 | * Sends a message to worker given its worker node key. |
280c2a77 | 1487 | * |
aa9eede8 | 1488 | * @param workerNodeKey - The worker node key. |
38e795c1 | 1489 | * @param message - The message. |
7d91a8cd | 1490 | * @param transferList - The optional array of transferable objects. |
280c2a77 S |
1491 | */ |
1492 | protected abstract sendToWorker ( | |
aa9eede8 | 1493 | workerNodeKey: number, |
7d91a8cd | 1494 | message: MessageValue<Data>, |
6a3ecc50 | 1495 | transferList?: readonly TransferListItem[] |
280c2a77 S |
1496 | ): void |
1497 | ||
e0843544 JB |
1498 | /** |
1499 | * Initializes the worker node usage with sensible default values gathered during runtime. | |
1500 | * | |
1501 | * @param workerNode - The worker node. | |
1502 | */ | |
1503 | private initWorkerNodeUsage (workerNode: IWorkerNode<Worker, Data>): void { | |
1504 | if ( | |
1505 | this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() | |
1506 | .runTime.aggregate === true | |
1507 | ) { | |
1508 | workerNode.usage.runTime.aggregate = min( | |
1509 | ...this.workerNodes.map( | |
80115618 JB |
1510 | workerNode => |
1511 | workerNode.usage.runTime.aggregate ?? Number.POSITIVE_INFINITY | |
e0843544 JB |
1512 | ) |
1513 | ) | |
1514 | } | |
1515 | if ( | |
1516 | this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() | |
1517 | .waitTime.aggregate === true | |
1518 | ) { | |
1519 | workerNode.usage.waitTime.aggregate = min( | |
1520 | ...this.workerNodes.map( | |
80115618 JB |
1521 | workerNode => |
1522 | workerNode.usage.waitTime.aggregate ?? Number.POSITIVE_INFINITY | |
e0843544 JB |
1523 | ) |
1524 | ) | |
1525 | } | |
1526 | if ( | |
1527 | this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements().elu | |
1528 | .aggregate === true | |
1529 | ) { | |
1530 | workerNode.usage.elu.active.aggregate = min( | |
1531 | ...this.workerNodes.map( | |
80115618 JB |
1532 | workerNode => |
1533 | workerNode.usage.elu.active.aggregate ?? Number.POSITIVE_INFINITY | |
e0843544 JB |
1534 | ) |
1535 | ) | |
1536 | } | |
1537 | } | |
1538 | ||
4a6952ff | 1539 | /** |
aa9eede8 | 1540 | * Creates a new, completely set up worker node. |
4a6952ff | 1541 | * |
aa9eede8 | 1542 | * @returns New, completely set up worker node key. |
4a6952ff | 1543 | */ |
aa9eede8 | 1544 | protected createAndSetupWorkerNode (): number { |
c3719753 JB |
1545 | const workerNode = this.createWorkerNode() |
1546 | workerNode.registerWorkerEventHandler( | |
1547 | 'online', | |
1548 | this.opts.onlineHandler ?? EMPTY_FUNCTION | |
1549 | ) | |
1550 | workerNode.registerWorkerEventHandler( | |
1551 | 'message', | |
1552 | this.opts.messageHandler ?? EMPTY_FUNCTION | |
1553 | ) | |
1554 | workerNode.registerWorkerEventHandler( | |
1555 | 'error', | |
1556 | this.opts.errorHandler ?? EMPTY_FUNCTION | |
1557 | ) | |
b271fce0 | 1558 | workerNode.registerOnceWorkerEventHandler('error', (error: Error) => { |
07e0c9e5 | 1559 | workerNode.info.ready = false |
2a69b8c5 | 1560 | this.emitter?.emit(PoolEvents.error, error) |
15b176e0 | 1561 | if ( |
b6bfca01 | 1562 | this.started && |
711623b8 | 1563 | !this.destroying && |
9b38ab2d | 1564 | this.opts.restartWorkerOnError === true |
15b176e0 | 1565 | ) { |
07e0c9e5 | 1566 | if (workerNode.info.dynamic) { |
aa9eede8 | 1567 | this.createAndSetupDynamicWorkerNode() |
b362a929 | 1568 | } else if (!this.startingMinimumNumberOfWorkers) { |
e0843544 | 1569 | this.startMinimumNumberOfWorkers(true) |
8a1260a3 | 1570 | } |
5baee0d7 | 1571 | } |
3c9123c7 JB |
1572 | if ( |
1573 | this.started && | |
1574 | !this.destroying && | |
1575 | this.opts.enableTasksQueue === true | |
1576 | ) { | |
9974369e | 1577 | this.redistributeQueuedTasks(this.workerNodes.indexOf(workerNode)) |
19dbc45b | 1578 | } |
b4f8aca8 | 1579 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
07f6f7b1 | 1580 | workerNode?.terminate().catch((error: unknown) => { |
07e0c9e5 JB |
1581 | this.emitter?.emit(PoolEvents.error, error) |
1582 | }) | |
5baee0d7 | 1583 | }) |
c3719753 JB |
1584 | workerNode.registerWorkerEventHandler( |
1585 | 'exit', | |
1586 | this.opts.exitHandler ?? EMPTY_FUNCTION | |
1587 | ) | |
1588 | workerNode.registerOnceWorkerEventHandler('exit', () => { | |
9974369e | 1589 | this.removeWorkerNode(workerNode) |
b362a929 JB |
1590 | if ( |
1591 | this.started && | |
1592 | !this.startingMinimumNumberOfWorkers && | |
1593 | !this.destroying | |
1594 | ) { | |
e0843544 | 1595 | this.startMinimumNumberOfWorkers(true) |
60dc0a9c | 1596 | } |
a974afa6 | 1597 | }) |
c3719753 | 1598 | const workerNodeKey = this.addWorkerNode(workerNode) |
aa9eede8 | 1599 | this.afterWorkerNodeSetup(workerNodeKey) |
aa9eede8 | 1600 | return workerNodeKey |
c97c7edb | 1601 | } |
be0676b3 | 1602 | |
930dcf12 | 1603 | /** |
aa9eede8 | 1604 | * Creates a new, completely set up dynamic worker node. |
930dcf12 | 1605 | * |
aa9eede8 | 1606 | * @returns New, completely set up dynamic worker node key. |
930dcf12 | 1607 | */ |
aa9eede8 JB |
1608 | protected createAndSetupDynamicWorkerNode (): number { |
1609 | const workerNodeKey = this.createAndSetupWorkerNode() | |
041dc05b | 1610 | this.registerWorkerMessageListener(workerNodeKey, message => { |
4d159167 | 1611 | this.checkMessageWorkerId(message) |
aa9eede8 JB |
1612 | const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId( |
1613 | message.workerId | |
aad6fb64 | 1614 | ) |
152e87a8 | 1615 | const workerInfo = this.getWorkerInfo(localWorkerNodeKey) |
2b6b412f | 1616 | const workerUsage = this.workerNodes[localWorkerNodeKey]?.usage |
81c02522 | 1617 | // Kill message received from worker |
930dcf12 JB |
1618 | if ( |
1619 | isKillBehavior(KillBehaviors.HARD, message.kill) || | |
1e3214b6 | 1620 | (isKillBehavior(KillBehaviors.SOFT, message.kill) && |
7b56f532 | 1621 | ((this.opts.enableTasksQueue === false && |
aa9eede8 | 1622 | workerUsage.tasks.executing === 0) || |
7b56f532 | 1623 | (this.opts.enableTasksQueue === true && |
152e87a8 JB |
1624 | workerInfo != null && |
1625 | !workerInfo.stealing && | |
aa9eede8 JB |
1626 | workerUsage.tasks.executing === 0 && |
1627 | this.tasksQueueSize(localWorkerNodeKey) === 0))) | |
930dcf12 | 1628 | ) { |
f3827d5d | 1629 | // Flag the worker node as not ready immediately |
ae3ab61d | 1630 | this.flagWorkerNodeAsNotReady(localWorkerNodeKey) |
07f6f7b1 | 1631 | this.destroyWorkerNode(localWorkerNodeKey).catch((error: unknown) => { |
5270d253 JB |
1632 | this.emitter?.emit(PoolEvents.error, error) |
1633 | }) | |
930dcf12 JB |
1634 | } |
1635 | }) | |
aa9eede8 | 1636 | this.sendToWorker(workerNodeKey, { |
e9dd5b66 | 1637 | checkActive: true |
21f710aa | 1638 | }) |
72ae84a2 | 1639 | if (this.taskFunctions.size > 0) { |
31847469 | 1640 | for (const [taskFunctionName, taskFunctionObject] of this.taskFunctions) { |
72ae84a2 JB |
1641 | this.sendTaskFunctionOperationToWorker(workerNodeKey, { |
1642 | taskFunctionOperation: 'add', | |
31847469 JB |
1643 | taskFunctionProperties: buildTaskFunctionProperties( |
1644 | taskFunctionName, | |
1645 | taskFunctionObject | |
1646 | ), | |
1647 | taskFunction: taskFunctionObject.taskFunction.toString() | |
07f6f7b1 | 1648 | }).catch((error: unknown) => { |
72ae84a2 JB |
1649 | this.emitter?.emit(PoolEvents.error, error) |
1650 | }) | |
1651 | } | |
1652 | } | |
e44639e9 JB |
1653 | const workerNode = this.workerNodes[workerNodeKey] |
1654 | workerNode.info.dynamic = true | |
b1aae695 | 1655 | if ( |
bcfb06ce JB |
1656 | this.workerChoiceStrategiesContext?.getPolicy().dynamicWorkerReady === |
1657 | true || | |
1658 | this.workerChoiceStrategiesContext?.getPolicy().dynamicWorkerUsage === | |
1659 | true | |
b1aae695 | 1660 | ) { |
e44639e9 | 1661 | workerNode.info.ready = true |
b5e113f6 | 1662 | } |
e0843544 | 1663 | this.initWorkerNodeUsage(workerNode) |
33e6bb4c | 1664 | this.checkAndEmitDynamicWorkerCreationEvents() |
aa9eede8 | 1665 | return workerNodeKey |
930dcf12 JB |
1666 | } |
1667 | ||
a2ed5053 | 1668 | /** |
aa9eede8 | 1669 | * Registers a listener callback on the worker given its worker node key. |
a2ed5053 | 1670 | * |
aa9eede8 | 1671 | * @param workerNodeKey - The worker node key. |
a2ed5053 JB |
1672 | * @param listener - The message listener callback. |
1673 | */ | |
85aeb3f3 JB |
1674 | protected abstract registerWorkerMessageListener< |
1675 | Message extends Data | Response | |
aa9eede8 JB |
1676 | >( |
1677 | workerNodeKey: number, | |
1678 | listener: (message: MessageValue<Message>) => void | |
1679 | ): void | |
a2ed5053 | 1680 | |
ae036c3e JB |
1681 | /** |
1682 | * Registers once a listener callback on the worker given its worker node key. | |
1683 | * | |
1684 | * @param workerNodeKey - The worker node key. | |
1685 | * @param listener - The message listener callback. | |
1686 | */ | |
1687 | protected abstract registerOnceWorkerMessageListener< | |
1688 | Message extends Data | Response | |
1689 | >( | |
1690 | workerNodeKey: number, | |
1691 | listener: (message: MessageValue<Message>) => void | |
1692 | ): void | |
1693 | ||
1694 | /** | |
1695 | * Deregisters a listener callback on the worker given its worker node key. | |
1696 | * | |
1697 | * @param workerNodeKey - The worker node key. | |
1698 | * @param listener - The message listener callback. | |
1699 | */ | |
1700 | protected abstract deregisterWorkerMessageListener< | |
1701 | Message extends Data | Response | |
1702 | >( | |
1703 | workerNodeKey: number, | |
1704 | listener: (message: MessageValue<Message>) => void | |
1705 | ): void | |
1706 | ||
a2ed5053 | 1707 | /** |
aa9eede8 | 1708 | * Method hooked up after a worker node has been newly created. |
a2ed5053 JB |
1709 | * Can be overridden. |
1710 | * | |
aa9eede8 | 1711 | * @param workerNodeKey - The newly created worker node key. |
a2ed5053 | 1712 | */ |
aa9eede8 | 1713 | protected afterWorkerNodeSetup (workerNodeKey: number): void { |
a2ed5053 | 1714 | // Listen to worker messages. |
fcd39179 JB |
1715 | this.registerWorkerMessageListener( |
1716 | workerNodeKey, | |
3a776b6d | 1717 | this.workerMessageListener |
fcd39179 | 1718 | ) |
85aeb3f3 | 1719 | // Send the startup message to worker. |
aa9eede8 | 1720 | this.sendStartupMessageToWorker(workerNodeKey) |
9edb9717 JB |
1721 | // Send the statistics message to worker. |
1722 | this.sendStatisticsMessageToWorker(workerNodeKey) | |
72695f86 | 1723 | if (this.opts.enableTasksQueue === true) { |
dbd73092 | 1724 | if (this.opts.tasksQueueOptions?.taskStealing === true) { |
e1c2dba7 | 1725 | this.workerNodes[workerNodeKey].on( |
e44639e9 JB |
1726 | 'idle', |
1727 | this.handleWorkerNodeIdleEvent | |
9f95d5eb | 1728 | ) |
47352846 JB |
1729 | } |
1730 | if (this.opts.tasksQueueOptions?.tasksStealingOnBackPressure === true) { | |
e1c2dba7 | 1731 | this.workerNodes[workerNodeKey].on( |
b5e75be8 | 1732 | 'backPressure', |
e44639e9 | 1733 | this.handleWorkerNodeBackPressureEvent |
9f95d5eb | 1734 | ) |
47352846 | 1735 | } |
72695f86 | 1736 | } |
d2c73f82 JB |
1737 | } |
1738 | ||
85aeb3f3 | 1739 | /** |
aa9eede8 JB |
1740 | * Sends the startup message to worker given its worker node key. |
1741 | * | |
1742 | * @param workerNodeKey - The worker node key. | |
1743 | */ | |
1744 | protected abstract sendStartupMessageToWorker (workerNodeKey: number): void | |
1745 | ||
1746 | /** | |
9edb9717 | 1747 | * Sends the statistics message to worker given its worker node key. |
85aeb3f3 | 1748 | * |
aa9eede8 | 1749 | * @param workerNodeKey - The worker node key. |
85aeb3f3 | 1750 | */ |
9edb9717 | 1751 | private sendStatisticsMessageToWorker (workerNodeKey: number): void { |
aa9eede8 JB |
1752 | this.sendToWorker(workerNodeKey, { |
1753 | statistics: { | |
1754 | runTime: | |
bcfb06ce | 1755 | this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() |
f4d0a470 | 1756 | .runTime.aggregate ?? false, |
c63a35a0 | 1757 | elu: |
bcfb06ce JB |
1758 | this.workerChoiceStrategiesContext?.getTaskStatisticsRequirements() |
1759 | .elu.aggregate ?? false | |
72ae84a2 | 1760 | } |
aa9eede8 JB |
1761 | }) |
1762 | } | |
a2ed5053 | 1763 | |
5eb72b9e JB |
1764 | private cannotStealTask (): boolean { |
1765 | return this.workerNodes.length <= 1 || this.info.queuedTasks === 0 | |
1766 | } | |
1767 | ||
42c677c1 JB |
1768 | private handleTask (workerNodeKey: number, task: Task<Data>): void { |
1769 | if (this.shallExecuteTask(workerNodeKey)) { | |
1770 | this.executeTask(workerNodeKey, task) | |
1771 | } else { | |
1772 | this.enqueueTask(workerNodeKey, task) | |
1773 | } | |
1774 | } | |
1775 | ||
2d1d3b2d JB |
1776 | private redistributeQueuedTasks (sourceWorkerNodeKey: number): void { |
1777 | if (sourceWorkerNodeKey === -1 || this.cannotStealTask()) { | |
cb71d660 JB |
1778 | return |
1779 | } | |
2d1d3b2d | 1780 | while (this.tasksQueueSize(sourceWorkerNodeKey) > 0) { |
f201a0cd JB |
1781 | const destinationWorkerNodeKey = this.workerNodes.reduce( |
1782 | (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { | |
2d1d3b2d JB |
1783 | return sourceWorkerNodeKey !== workerNodeKey && |
1784 | workerNode.info.ready && | |
852ed3e4 JB |
1785 | workerNode.usage.tasks.queued < |
1786 | workerNodes[minWorkerNodeKey].usage.tasks.queued | |
f201a0cd JB |
1787 | ? workerNodeKey |
1788 | : minWorkerNodeKey | |
1789 | }, | |
1790 | 0 | |
1791 | ) | |
42c677c1 JB |
1792 | this.handleTask( |
1793 | destinationWorkerNodeKey, | |
67f3f2d6 | 1794 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2d1d3b2d | 1795 | this.dequeueTask(sourceWorkerNodeKey)! |
42c677c1 | 1796 | ) |
dd951876 JB |
1797 | } |
1798 | } | |
1799 | ||
b1838604 JB |
1800 | private updateTaskStolenStatisticsWorkerUsage ( |
1801 | workerNodeKey: number, | |
b1838604 JB |
1802 | taskName: string |
1803 | ): void { | |
1a880eca | 1804 | const workerNode = this.workerNodes[workerNodeKey] |
c63a35a0 | 1805 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
efabc98d | 1806 | if (workerNode?.usage != null) { |
b1838604 JB |
1807 | ++workerNode.usage.tasks.stolen |
1808 | } | |
1809 | if ( | |
1810 | this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && | |
1811 | workerNode.getTaskFunctionWorkerUsage(taskName) != null | |
1812 | ) { | |
8bd0556f JB |
1813 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1814 | ++workerNode.getTaskFunctionWorkerUsage(taskName)!.tasks.stolen | |
b1838604 JB |
1815 | } |
1816 | } | |
1817 | ||
463226a4 | 1818 | private updateTaskSequentiallyStolenStatisticsWorkerUsage ( |
2d1d3b2d JB |
1819 | workerNodeKey: number, |
1820 | taskName: string, | |
8bd0556f | 1821 | previousTaskName?: string |
463226a4 JB |
1822 | ): void { |
1823 | const workerNode = this.workerNodes[workerNodeKey] | |
c63a35a0 | 1824 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
7aa2f476 | 1825 | if (workerNode?.usage != null) { |
463226a4 JB |
1826 | ++workerNode.usage.tasks.sequentiallyStolen |
1827 | } | |
1828 | if ( | |
1829 | this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && | |
3b33cd2a JB |
1830 | workerNode.getTaskFunctionWorkerUsage(taskName) != null |
1831 | ) { | |
1832 | const taskFunctionWorkerUsage = | |
1833 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1834 | workerNode.getTaskFunctionWorkerUsage(taskName)! | |
1835 | if ( | |
1836 | taskFunctionWorkerUsage.tasks.sequentiallyStolen === 0 || | |
8bd0556f JB |
1837 | (previousTaskName != null && |
1838 | previousTaskName === taskName && | |
3b33cd2a JB |
1839 | taskFunctionWorkerUsage.tasks.sequentiallyStolen > 0) |
1840 | ) { | |
1841 | ++taskFunctionWorkerUsage.tasks.sequentiallyStolen | |
1842 | } else if (taskFunctionWorkerUsage.tasks.sequentiallyStolen > 0) { | |
1843 | taskFunctionWorkerUsage.tasks.sequentiallyStolen = 0 | |
1844 | } | |
463226a4 JB |
1845 | } |
1846 | } | |
1847 | ||
1848 | private resetTaskSequentiallyStolenStatisticsWorkerUsage ( | |
2d1d3b2d JB |
1849 | workerNodeKey: number, |
1850 | taskName: string | |
463226a4 JB |
1851 | ): void { |
1852 | const workerNode = this.workerNodes[workerNodeKey] | |
c63a35a0 | 1853 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
efabc98d | 1854 | if (workerNode?.usage != null) { |
463226a4 JB |
1855 | workerNode.usage.tasks.sequentiallyStolen = 0 |
1856 | } | |
1857 | if ( | |
1858 | this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && | |
1859 | workerNode.getTaskFunctionWorkerUsage(taskName) != null | |
1860 | ) { | |
8bd0556f JB |
1861 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1862 | workerNode.getTaskFunctionWorkerUsage( | |
1863 | taskName | |
1864 | )!.tasks.sequentiallyStolen = 0 | |
463226a4 JB |
1865 | } |
1866 | } | |
1867 | ||
e44639e9 | 1868 | private readonly handleWorkerNodeIdleEvent = ( |
e1c2dba7 | 1869 | eventDetail: WorkerNodeEventDetail, |
463226a4 | 1870 | previousStolenTask?: Task<Data> |
9f95d5eb | 1871 | ): void => { |
e1c2dba7 | 1872 | const { workerNodeKey } = eventDetail |
463226a4 JB |
1873 | if (workerNodeKey == null) { |
1874 | throw new Error( | |
c63a35a0 | 1875 | "WorkerNode event detail 'workerNodeKey' property must be defined" |
463226a4 JB |
1876 | ) |
1877 | } | |
c63a35a0 | 1878 | const workerInfo = this.getWorkerInfo(workerNodeKey) |
010f158c JB |
1879 | if (workerInfo == null) { |
1880 | throw new Error( | |
1881 | `Worker node with key '${workerNodeKey}' not found in pool` | |
1882 | ) | |
1883 | } | |
5eb72b9e JB |
1884 | if ( |
1885 | this.cannotStealTask() || | |
c63a35a0 JB |
1886 | (this.info.stealingWorkerNodes ?? 0) > |
1887 | Math.floor(this.workerNodes.length / 2) | |
5eb72b9e | 1888 | ) { |
010f158c | 1889 | if (previousStolenTask != null) { |
c63a35a0 | 1890 | workerInfo.stealing = false |
2d1d3b2d JB |
1891 | this.resetTaskSequentiallyStolenStatisticsWorkerUsage( |
1892 | workerNodeKey, | |
1893 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1894 | previousStolenTask.name! | |
1895 | ) | |
5eb72b9e JB |
1896 | } |
1897 | return | |
1898 | } | |
463226a4 JB |
1899 | const workerNodeTasksUsage = this.workerNodes[workerNodeKey].usage.tasks |
1900 | if ( | |
1901 | previousStolenTask != null && | |
463226a4 JB |
1902 | (workerNodeTasksUsage.executing > 0 || |
1903 | this.tasksQueueSize(workerNodeKey) > 0) | |
1904 | ) { | |
c63a35a0 | 1905 | workerInfo.stealing = false |
2d1d3b2d JB |
1906 | this.resetTaskSequentiallyStolenStatisticsWorkerUsage( |
1907 | workerNodeKey, | |
1908 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
1909 | previousStolenTask.name! | |
1910 | ) | |
463226a4 JB |
1911 | return |
1912 | } | |
c63a35a0 | 1913 | workerInfo.stealing = true |
463226a4 | 1914 | const stolenTask = this.workerNodeStealTask(workerNodeKey) |
8bd0556f | 1915 | if (stolenTask != null) { |
2d1d3b2d JB |
1916 | this.updateTaskSequentiallyStolenStatisticsWorkerUsage( |
1917 | workerNodeKey, | |
67f3f2d6 | 1918 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2d1d3b2d | 1919 | stolenTask.name!, |
8bd0556f | 1920 | previousStolenTask?.name |
2d1d3b2d | 1921 | ) |
f1f77f45 | 1922 | } |
463226a4 JB |
1923 | sleep(exponentialDelay(workerNodeTasksUsage.sequentiallyStolen)) |
1924 | .then(() => { | |
e44639e9 | 1925 | this.handleWorkerNodeIdleEvent(eventDetail, stolenTask) |
463226a4 JB |
1926 | return undefined |
1927 | }) | |
07f6f7b1 | 1928 | .catch((error: unknown) => { |
2e8cfbb7 JB |
1929 | this.emitter?.emit(PoolEvents.error, error) |
1930 | }) | |
463226a4 JB |
1931 | } |
1932 | ||
1933 | private readonly workerNodeStealTask = ( | |
1934 | workerNodeKey: number | |
1935 | ): Task<Data> | undefined => { | |
dd951876 | 1936 | const workerNodes = this.workerNodes |
a6b3272b | 1937 | .slice() |
dd951876 JB |
1938 | .sort( |
1939 | (workerNodeA, workerNodeB) => | |
1940 | workerNodeB.usage.tasks.queued - workerNodeA.usage.tasks.queued | |
1941 | ) | |
f201a0cd | 1942 | const sourceWorkerNode = workerNodes.find( |
463226a4 JB |
1943 | (sourceWorkerNode, sourceWorkerNodeKey) => |
1944 | sourceWorkerNode.info.ready && | |
5eb72b9e | 1945 | !sourceWorkerNode.info.stealing && |
463226a4 JB |
1946 | sourceWorkerNodeKey !== workerNodeKey && |
1947 | sourceWorkerNode.usage.tasks.queued > 0 | |
f201a0cd JB |
1948 | ) |
1949 | if (sourceWorkerNode != null) { | |
67f3f2d6 | 1950 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2eee7220 | 1951 | const task = sourceWorkerNode.dequeueLastPrioritizedTask()! |
42c677c1 | 1952 | this.handleTask(workerNodeKey, task) |
67f3f2d6 JB |
1953 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1954 | this.updateTaskStolenStatisticsWorkerUsage(workerNodeKey, task.name!) | |
463226a4 | 1955 | return task |
72695f86 JB |
1956 | } |
1957 | } | |
1958 | ||
e44639e9 | 1959 | private readonly handleWorkerNodeBackPressureEvent = ( |
e1c2dba7 | 1960 | eventDetail: WorkerNodeEventDetail |
9f95d5eb | 1961 | ): void => { |
5eb72b9e JB |
1962 | if ( |
1963 | this.cannotStealTask() || | |
2eee7220 | 1964 | this.hasBackPressure() || |
c63a35a0 JB |
1965 | (this.info.stealingWorkerNodes ?? 0) > |
1966 | Math.floor(this.workerNodes.length / 2) | |
5eb72b9e | 1967 | ) { |
cb71d660 JB |
1968 | return |
1969 | } | |
f778c355 | 1970 | const sizeOffset = 1 |
67f3f2d6 JB |
1971 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1972 | if (this.opts.tasksQueueOptions!.size! <= sizeOffset) { | |
68dbcdc0 JB |
1973 | return |
1974 | } | |
7b2c5627 | 1975 | const { workerId } = eventDetail |
72695f86 | 1976 | const sourceWorkerNode = |
b5e75be8 | 1977 | this.workerNodes[this.getWorkerNodeKeyByWorkerId(workerId)] |
72695f86 | 1978 | const workerNodes = this.workerNodes |
a6b3272b | 1979 | .slice() |
72695f86 JB |
1980 | .sort( |
1981 | (workerNodeA, workerNodeB) => | |
1982 | workerNodeA.usage.tasks.queued - workerNodeB.usage.tasks.queued | |
1983 | ) | |
1984 | for (const [workerNodeKey, workerNode] of workerNodes.entries()) { | |
1985 | if ( | |
0bc68267 | 1986 | sourceWorkerNode.usage.tasks.queued > 0 && |
a6b3272b | 1987 | workerNode.info.ready && |
5eb72b9e | 1988 | !workerNode.info.stealing && |
b5e75be8 | 1989 | workerNode.info.id !== workerId && |
0bc68267 | 1990 | workerNode.usage.tasks.queued < |
67f3f2d6 JB |
1991 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
1992 | this.opts.tasksQueueOptions!.size! - sizeOffset | |
72695f86 | 1993 | ) { |
c63a35a0 | 1994 | const workerInfo = this.getWorkerInfo(workerNodeKey) |
1851fed0 JB |
1995 | if (workerInfo == null) { |
1996 | throw new Error( | |
1997 | `Worker node with key '${workerNodeKey}' not found in pool` | |
1998 | ) | |
1999 | } | |
c63a35a0 | 2000 | workerInfo.stealing = true |
67f3f2d6 | 2001 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2eee7220 | 2002 | const task = sourceWorkerNode.dequeueLastPrioritizedTask()! |
42c677c1 | 2003 | this.handleTask(workerNodeKey, task) |
67f3f2d6 JB |
2004 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2005 | this.updateTaskStolenStatisticsWorkerUsage(workerNodeKey, task.name!) | |
c63a35a0 | 2006 | workerInfo.stealing = false |
10ecf8fd | 2007 | } |
a2ed5053 JB |
2008 | } |
2009 | } | |
2010 | ||
be0676b3 | 2011 | /** |
fcd39179 | 2012 | * This method is the message listener registered on each worker. |
be0676b3 | 2013 | */ |
3a776b6d JB |
2014 | protected readonly workerMessageListener = ( |
2015 | message: MessageValue<Response> | |
2016 | ): void => { | |
fcd39179 | 2017 | this.checkMessageWorkerId(message) |
31847469 JB |
2018 | const { workerId, ready, taskId, taskFunctionsProperties } = message |
2019 | if (ready != null && taskFunctionsProperties != null) { | |
fcd39179 JB |
2020 | // Worker ready response received from worker |
2021 | this.handleWorkerReadyResponse(message) | |
31847469 JB |
2022 | } else if (taskFunctionsProperties != null) { |
2023 | // Task function properties message received from worker | |
9a55fa8c JB |
2024 | const workerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId) |
2025 | const workerInfo = this.getWorkerInfo(workerNodeKey) | |
1851fed0 | 2026 | if (workerInfo != null) { |
31847469 | 2027 | workerInfo.taskFunctionsProperties = taskFunctionsProperties |
9a55fa8c | 2028 | this.sendStatisticsMessageToWorker(workerNodeKey) |
1851fed0 | 2029 | } |
31847469 JB |
2030 | } else if (taskId != null) { |
2031 | // Task execution response received from worker | |
2032 | this.handleTaskExecutionResponse(message) | |
6b272951 JB |
2033 | } |
2034 | } | |
2035 | ||
8e8d9101 JB |
2036 | private checkAndEmitReadyEvent (): void { |
2037 | if (!this.readyEventEmitted && this.ready) { | |
2038 | this.emitter?.emit(PoolEvents.ready, this.info) | |
2039 | this.readyEventEmitted = true | |
2040 | } | |
2041 | } | |
2042 | ||
10e2aa7e | 2043 | private handleWorkerReadyResponse (message: MessageValue<Response>): void { |
31847469 | 2044 | const { workerId, ready, taskFunctionsProperties } = message |
e44639e9 | 2045 | if (ready == null || !ready) { |
c63a35a0 | 2046 | throw new Error(`Worker ${workerId} failed to initialize`) |
f05ed93c | 2047 | } |
9a55fa8c JB |
2048 | const workerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId) |
2049 | const workerNode = this.workerNodes[workerNodeKey] | |
e44639e9 | 2050 | workerNode.info.ready = ready |
31847469 | 2051 | workerNode.info.taskFunctionsProperties = taskFunctionsProperties |
9a55fa8c | 2052 | this.sendStatisticsMessageToWorker(workerNodeKey) |
8e8d9101 | 2053 | this.checkAndEmitReadyEvent() |
6b272951 JB |
2054 | } |
2055 | ||
2056 | private handleTaskExecutionResponse (message: MessageValue<Response>): void { | |
463226a4 | 2057 | const { workerId, taskId, workerError, data } = message |
67f3f2d6 JB |
2058 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2059 | const promiseResponse = this.promiseResponseMap.get(taskId!) | |
6b272951 | 2060 | if (promiseResponse != null) { |
f18fd12b | 2061 | const { resolve, reject, workerNodeKey, asyncResource } = promiseResponse |
9b358e72 | 2062 | const workerNode = this.workerNodes[workerNodeKey] |
6703b9f4 JB |
2063 | if (workerError != null) { |
2064 | this.emitter?.emit(PoolEvents.taskError, workerError) | |
f18fd12b JB |
2065 | asyncResource != null |
2066 | ? asyncResource.runInAsyncScope( | |
2067 | reject, | |
2068 | this.emitter, | |
2069 | workerError.message | |
2070 | ) | |
2071 | : reject(workerError.message) | |
6b272951 | 2072 | } else { |
f18fd12b JB |
2073 | asyncResource != null |
2074 | ? asyncResource.runInAsyncScope(resolve, this.emitter, data) | |
2075 | : resolve(data as Response) | |
6b272951 | 2076 | } |
f18fd12b | 2077 | asyncResource?.emitDestroy() |
501aea93 | 2078 | this.afterTaskExecutionHook(workerNodeKey, message) |
67f3f2d6 JB |
2079 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2080 | this.promiseResponseMap.delete(taskId!) | |
cdaecaee JB |
2081 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
2082 | workerNode?.emit('taskFinished', taskId) | |
16d7e943 JB |
2083 | if ( |
2084 | this.opts.enableTasksQueue === true && | |
2085 | !this.destroying && | |
2086 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | |
2087 | workerNode != null | |
2088 | ) { | |
9b358e72 | 2089 | const workerNodeTasksUsage = workerNode.usage.tasks |
463226a4 JB |
2090 | if ( |
2091 | this.tasksQueueSize(workerNodeKey) > 0 && | |
2092 | workerNodeTasksUsage.executing < | |
67f3f2d6 JB |
2093 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2094 | this.opts.tasksQueueOptions!.concurrency! | |
463226a4 | 2095 | ) { |
67f3f2d6 JB |
2096 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2097 | this.executeTask(workerNodeKey, this.dequeueTask(workerNodeKey)!) | |
463226a4 JB |
2098 | } |
2099 | if ( | |
2100 | workerNodeTasksUsage.executing === 0 && | |
2101 | this.tasksQueueSize(workerNodeKey) === 0 && | |
2102 | workerNodeTasksUsage.sequentiallyStolen === 0 | |
2103 | ) { | |
e44639e9 | 2104 | workerNode.emit('idle', { |
7f0e1334 | 2105 | workerId, |
e1c2dba7 JB |
2106 | workerNodeKey |
2107 | }) | |
463226a4 | 2108 | } |
be0676b3 APA |
2109 | } |
2110 | } | |
be0676b3 | 2111 | } |
7c0ba920 | 2112 | |
a1763c54 | 2113 | private checkAndEmitTaskExecutionEvents (): void { |
33e6bb4c JB |
2114 | if (this.busy) { |
2115 | this.emitter?.emit(PoolEvents.busy, this.info) | |
a1763c54 JB |
2116 | } |
2117 | } | |
2118 | ||
2119 | private checkAndEmitTaskQueuingEvents (): void { | |
2120 | if (this.hasBackPressure()) { | |
2121 | this.emitter?.emit(PoolEvents.backPressure, this.info) | |
164d950a JB |
2122 | } |
2123 | } | |
2124 | ||
d0878034 JB |
2125 | /** |
2126 | * Emits dynamic worker creation events. | |
2127 | */ | |
2128 | protected abstract checkAndEmitDynamicWorkerCreationEvents (): void | |
33e6bb4c | 2129 | |
8a1260a3 | 2130 | /** |
aa9eede8 | 2131 | * Gets the worker information given its worker node key. |
8a1260a3 JB |
2132 | * |
2133 | * @param workerNodeKey - The worker node key. | |
3f09ed9f | 2134 | * @returns The worker information. |
8a1260a3 | 2135 | */ |
1851fed0 JB |
2136 | protected getWorkerInfo (workerNodeKey: number): WorkerInfo | undefined { |
2137 | return this.workerNodes[workerNodeKey]?.info | |
e221309a JB |
2138 | } |
2139 | ||
a05c10de | 2140 | /** |
c3719753 | 2141 | * Creates a worker node. |
ea7a90d3 | 2142 | * |
c3719753 | 2143 | * @returns The created worker node. |
ea7a90d3 | 2144 | */ |
c3719753 | 2145 | private createWorkerNode (): IWorkerNode<Worker, Data> { |
671d5154 | 2146 | const workerNode = new WorkerNode<Worker, Data>( |
c3719753 JB |
2147 | this.worker, |
2148 | this.filePath, | |
2149 | { | |
2150 | env: this.opts.env, | |
2151 | workerOptions: this.opts.workerOptions, | |
2152 | tasksQueueBackPressureSize: | |
32b141fd | 2153 | this.opts.tasksQueueOptions?.size ?? |
26ce26ca JB |
2154 | getDefaultTasksQueueOptions( |
2155 | this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers | |
95d1a734 JB |
2156 | ).size, |
2157 | tasksQueueBucketSize: | |
2158 | (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers) * 2 | |
c3719753 | 2159 | } |
671d5154 | 2160 | ) |
b97d82d8 | 2161 | // Flag the worker node as ready at pool startup. |
d2c73f82 JB |
2162 | if (this.starting) { |
2163 | workerNode.info.ready = true | |
2164 | } | |
c3719753 JB |
2165 | return workerNode |
2166 | } | |
2167 | ||
2168 | /** | |
2169 | * Adds the given worker node in the pool worker nodes. | |
2170 | * | |
2171 | * @param workerNode - The worker node. | |
2172 | * @returns The added worker node key. | |
2173 | * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found. | |
2174 | */ | |
2175 | private addWorkerNode (workerNode: IWorkerNode<Worker, Data>): number { | |
aa9eede8 | 2176 | this.workerNodes.push(workerNode) |
c3719753 | 2177 | const workerNodeKey = this.workerNodes.indexOf(workerNode) |
aa9eede8 | 2178 | if (workerNodeKey === -1) { |
86ed0598 | 2179 | throw new Error('Worker added not found in worker nodes') |
aa9eede8 JB |
2180 | } |
2181 | return workerNodeKey | |
ea7a90d3 | 2182 | } |
c923ce56 | 2183 | |
8e8d9101 JB |
2184 | private checkAndEmitEmptyEvent (): void { |
2185 | if (this.empty) { | |
2186 | this.emitter?.emit(PoolEvents.empty, this.info) | |
2187 | this.readyEventEmitted = false | |
2188 | } | |
2189 | } | |
2190 | ||
51fe3d3c | 2191 | /** |
9974369e | 2192 | * Removes the worker node from the pool worker nodes. |
51fe3d3c | 2193 | * |
9974369e | 2194 | * @param workerNode - The worker node. |
51fe3d3c | 2195 | */ |
9974369e JB |
2196 | private removeWorkerNode (workerNode: IWorkerNode<Worker, Data>): void { |
2197 | const workerNodeKey = this.workerNodes.indexOf(workerNode) | |
1f68cede JB |
2198 | if (workerNodeKey !== -1) { |
2199 | this.workerNodes.splice(workerNodeKey, 1) | |
bcfb06ce | 2200 | this.workerChoiceStrategiesContext?.remove(workerNodeKey) |
1f68cede | 2201 | } |
8e8d9101 | 2202 | this.checkAndEmitEmptyEvent() |
51fe3d3c | 2203 | } |
adc3c320 | 2204 | |
ae3ab61d | 2205 | protected flagWorkerNodeAsNotReady (workerNodeKey: number): void { |
1851fed0 JB |
2206 | const workerInfo = this.getWorkerInfo(workerNodeKey) |
2207 | if (workerInfo != null) { | |
2208 | workerInfo.ready = false | |
2209 | } | |
ae3ab61d JB |
2210 | } |
2211 | ||
9e844245 JB |
2212 | private hasBackPressure (): boolean { |
2213 | return ( | |
2214 | this.opts.enableTasksQueue === true && | |
2215 | this.workerNodes.findIndex( | |
041dc05b | 2216 | workerNode => !workerNode.hasBackPressure() |
a1763c54 | 2217 | ) === -1 |
9e844245 | 2218 | ) |
e2b31e32 JB |
2219 | } |
2220 | ||
b0a4db63 | 2221 | /** |
aa9eede8 | 2222 | * Executes the given task on the worker given its worker node key. |
b0a4db63 | 2223 | * |
aa9eede8 | 2224 | * @param workerNodeKey - The worker node key. |
b0a4db63 JB |
2225 | * @param task - The task to execute. |
2226 | */ | |
2e81254d | 2227 | private executeTask (workerNodeKey: number, task: Task<Data>): void { |
1c6fe997 | 2228 | this.beforeTaskExecutionHook(workerNodeKey, task) |
bbfa38a2 | 2229 | this.sendToWorker(workerNodeKey, task, task.transferList) |
a1763c54 | 2230 | this.checkAndEmitTaskExecutionEvents() |
2e81254d JB |
2231 | } |
2232 | ||
f9f00b5f | 2233 | private enqueueTask (workerNodeKey: number, task: Task<Data>): number { |
a1763c54 JB |
2234 | const tasksQueueSize = this.workerNodes[workerNodeKey].enqueueTask(task) |
2235 | this.checkAndEmitTaskQueuingEvents() | |
2236 | return tasksQueueSize | |
adc3c320 JB |
2237 | } |
2238 | ||
0d4e88b3 JB |
2239 | private dequeueTask (workerNodeKey: number): Task<Data> | undefined { |
2240 | return this.workerNodes[workerNodeKey].dequeueTask() | |
adc3c320 JB |
2241 | } |
2242 | ||
416fd65c | 2243 | private tasksQueueSize (workerNodeKey: number): number { |
4b628b48 | 2244 | return this.workerNodes[workerNodeKey].tasksQueueSize() |
df593701 JB |
2245 | } |
2246 | ||
87347ea8 JB |
2247 | protected flushTasksQueue (workerNodeKey: number): number { |
2248 | let flushedTasks = 0 | |
920278a2 | 2249 | while (this.tasksQueueSize(workerNodeKey) > 0) { |
67f3f2d6 JB |
2250 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
2251 | this.executeTask(workerNodeKey, this.dequeueTask(workerNodeKey)!) | |
87347ea8 | 2252 | ++flushedTasks |
ff733df7 | 2253 | } |
4b628b48 | 2254 | this.workerNodes[workerNodeKey].clearTasksQueue() |
87347ea8 | 2255 | return flushedTasks |
ff733df7 JB |
2256 | } |
2257 | ||
ef41a6e6 | 2258 | private flushTasksQueues (): void { |
19b8be8b | 2259 | for (const workerNodeKey of this.workerNodes.keys()) { |
ef41a6e6 JB |
2260 | this.flushTasksQueue(workerNodeKey) |
2261 | } | |
2262 | } | |
c97c7edb | 2263 | } |