fix: fix pool destroying with tasks queuing enabled
[poolifier.git] / src / pools / abstract-pool.ts
1 import { randomUUID } from 'node:crypto'
2 import { performance } from 'node:perf_hooks'
3 import type { TransferListItem } from 'node:worker_threads'
4 import { EventEmitterAsyncResource } from 'node:events'
5 import { AsyncResource } from 'node:async_hooks'
6 import type {
7 MessageValue,
8 PromiseResponseWrapper,
9 Task
10 } from '../utility-types'
11 import {
12 DEFAULT_TASK_NAME,
13 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
14 EMPTY_FUNCTION,
15 average,
16 exponentialDelay,
17 isKillBehavior,
18 isPlainObject,
19 max,
20 median,
21 min,
22 round,
23 sleep
24 } from '../utils'
25 import { KillBehaviors } from '../worker/worker-options'
26 import type { TaskFunction } from '../worker/task-functions'
27 import {
28 type IPool,
29 PoolEvents,
30 type PoolInfo,
31 type PoolOptions,
32 type PoolType,
33 PoolTypes,
34 type TasksQueueOptions
35 } from './pool'
36 import type {
37 IWorker,
38 IWorkerNode,
39 TaskStatistics,
40 WorkerInfo,
41 WorkerNodeEventDetail,
42 WorkerType,
43 WorkerUsage
44 } from './worker'
45 import {
46 Measurements,
47 WorkerChoiceStrategies,
48 type WorkerChoiceStrategy,
49 type WorkerChoiceStrategyOptions
50 } from './selection-strategies/selection-strategies-types'
51 import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
52 import { version } from './version'
53 import { WorkerNode } from './worker-node'
54 import {
55 checkFilePath,
56 checkValidTasksQueueOptions,
57 checkValidWorkerChoiceStrategy,
58 getDefaultTasksQueueOptions,
59 updateEluWorkerUsage,
60 updateRunTimeWorkerUsage,
61 updateTaskStatisticsWorkerUsage,
62 updateWaitTimeWorkerUsage,
63 waitWorkerNodeEvents
64 } from './utils'
65
66 /**
67 * Base class that implements some shared logic for all poolifier pools.
68 *
69 * @typeParam Worker - Type of worker which manages this pool.
70 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
71 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
72 */
73 export abstract class AbstractPool<
74 Worker extends IWorker,
75 Data = unknown,
76 Response = unknown
77 > implements IPool<Worker, Data, Response> {
78 /** @inheritDoc */
79 public readonly workerNodes: Array<IWorkerNode<Worker, Data>> = []
80
81 /** @inheritDoc */
82 public emitter?: EventEmitterAsyncResource
83
84 /**
85 * Dynamic pool maximum size property placeholder.
86 */
87 protected readonly max?: number
88
89 /**
90 * The task execution response promise map:
91 * - `key`: The message id of each submitted task.
92 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
93 *
94 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
95 */
96 protected promiseResponseMap: Map<string, PromiseResponseWrapper<Response>> =
97 new Map<string, PromiseResponseWrapper<Response>>()
98
99 /**
100 * Worker choice strategy context referencing a worker choice algorithm implementation.
101 */
102 protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
103 Worker,
104 Data,
105 Response
106 >
107
108 /**
109 * The task functions added at runtime map:
110 * - `key`: The task function name.
111 * - `value`: The task function itself.
112 */
113 private readonly taskFunctions: Map<string, TaskFunction<Data, Response>>
114
115 /**
116 * Whether the pool is started or not.
117 */
118 private started: boolean
119 /**
120 * Whether the pool is starting or not.
121 */
122 private starting: boolean
123 /**
124 * Whether the pool is destroying or not.
125 */
126 private destroying: boolean
127 /**
128 * Whether the pool ready event has been emitted or not.
129 */
130 private readyEventEmitted: boolean
131 /**
132 * The start timestamp of the pool.
133 */
134 private readonly startTimestamp
135
136 /**
137 * Constructs a new poolifier pool.
138 *
139 * @param numberOfWorkers - Number of workers that this pool should manage.
140 * @param filePath - Path to the worker file.
141 * @param opts - Options for the pool.
142 */
143 public constructor (
144 protected readonly numberOfWorkers: number,
145 protected readonly filePath: string,
146 protected readonly opts: PoolOptions<Worker>
147 ) {
148 if (!this.isMain()) {
149 throw new Error(
150 'Cannot start a pool from a worker with the same type as the pool'
151 )
152 }
153 checkFilePath(this.filePath)
154 this.checkNumberOfWorkers(this.numberOfWorkers)
155 this.checkPoolOptions(this.opts)
156
157 this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
158 this.executeTask = this.executeTask.bind(this)
159 this.enqueueTask = this.enqueueTask.bind(this)
160
161 if (this.opts.enableEvents === true) {
162 this.initializeEventEmitter()
163 }
164 this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext<
165 Worker,
166 Data,
167 Response
168 >(
169 this,
170 this.opts.workerChoiceStrategy,
171 this.opts.workerChoiceStrategyOptions
172 )
173
174 this.setupHook()
175
176 this.taskFunctions = new Map<string, TaskFunction<Data, Response>>()
177
178 this.started = false
179 this.starting = false
180 this.destroying = false
181 this.readyEventEmitted = false
182 if (this.opts.startWorkers === true) {
183 this.start()
184 }
185
186 this.startTimestamp = performance.now()
187 }
188
189 private checkNumberOfWorkers (numberOfWorkers: number): void {
190 if (numberOfWorkers == null) {
191 throw new Error(
192 'Cannot instantiate a pool without specifying the number of workers'
193 )
194 } else if (!Number.isSafeInteger(numberOfWorkers)) {
195 throw new TypeError(
196 'Cannot instantiate a pool with a non safe integer number of workers'
197 )
198 } else if (numberOfWorkers < 0) {
199 throw new RangeError(
200 'Cannot instantiate a pool with a negative number of workers'
201 )
202 } else if (this.type === PoolTypes.fixed && numberOfWorkers === 0) {
203 throw new RangeError('Cannot instantiate a fixed pool with zero worker')
204 }
205 }
206
207 private checkPoolOptions (opts: PoolOptions<Worker>): void {
208 if (isPlainObject(opts)) {
209 this.opts.startWorkers = opts.startWorkers ?? true
210 checkValidWorkerChoiceStrategy(
211 opts.workerChoiceStrategy as WorkerChoiceStrategy
212 )
213 this.opts.workerChoiceStrategy =
214 opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
215 this.checkValidWorkerChoiceStrategyOptions(
216 opts.workerChoiceStrategyOptions as WorkerChoiceStrategyOptions
217 )
218 this.opts.workerChoiceStrategyOptions = {
219 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
220 ...opts.workerChoiceStrategyOptions
221 }
222 this.opts.restartWorkerOnError = opts.restartWorkerOnError ?? true
223 this.opts.enableEvents = opts.enableEvents ?? true
224 this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
225 if (this.opts.enableTasksQueue) {
226 checkValidTasksQueueOptions(opts.tasksQueueOptions as TasksQueueOptions)
227 this.opts.tasksQueueOptions = this.buildTasksQueueOptions(
228 opts.tasksQueueOptions as TasksQueueOptions
229 )
230 }
231 } else {
232 throw new TypeError('Invalid pool options: must be a plain object')
233 }
234 }
235
236 private checkValidWorkerChoiceStrategyOptions (
237 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
238 ): void {
239 if (
240 workerChoiceStrategyOptions != null &&
241 !isPlainObject(workerChoiceStrategyOptions)
242 ) {
243 throw new TypeError(
244 'Invalid worker choice strategy options: must be a plain object'
245 )
246 }
247 if (
248 workerChoiceStrategyOptions?.retries != null &&
249 !Number.isSafeInteger(workerChoiceStrategyOptions.retries)
250 ) {
251 throw new TypeError(
252 'Invalid worker choice strategy options: retries must be an integer'
253 )
254 }
255 if (
256 workerChoiceStrategyOptions?.retries != null &&
257 workerChoiceStrategyOptions.retries < 0
258 ) {
259 throw new RangeError(
260 `Invalid worker choice strategy options: retries '${workerChoiceStrategyOptions.retries}' must be greater or equal than zero`
261 )
262 }
263 if (
264 workerChoiceStrategyOptions?.weights != null &&
265 Object.keys(workerChoiceStrategyOptions.weights).length !== this.maxSize
266 ) {
267 throw new Error(
268 'Invalid worker choice strategy options: must have a weight for each worker node'
269 )
270 }
271 if (
272 workerChoiceStrategyOptions?.measurement != null &&
273 !Object.values(Measurements).includes(
274 workerChoiceStrategyOptions.measurement
275 )
276 ) {
277 throw new Error(
278 `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
279 )
280 }
281 }
282
283 private initializeEventEmitter (): void {
284 this.emitter = new EventEmitterAsyncResource({
285 name: `poolifier:${this.type}-${this.worker}-pool`
286 })
287 }
288
289 /** @inheritDoc */
290 public get info (): PoolInfo {
291 return {
292 version,
293 type: this.type,
294 worker: this.worker,
295 started: this.started,
296 ready: this.ready,
297 strategy: this.opts.workerChoiceStrategy as WorkerChoiceStrategy,
298 minSize: this.minSize,
299 maxSize: this.maxSize,
300 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
301 .runTime.aggregate &&
302 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
303 .waitTime.aggregate && { utilization: round(this.utilization) }),
304 workerNodes: this.workerNodes.length,
305 idleWorkerNodes: this.workerNodes.reduce(
306 (accumulator, workerNode) =>
307 workerNode.usage.tasks.executing === 0
308 ? accumulator + 1
309 : accumulator,
310 0
311 ),
312 busyWorkerNodes: this.workerNodes.reduce(
313 (accumulator, _workerNode, workerNodeKey) =>
314 this.isWorkerNodeBusy(workerNodeKey) ? accumulator + 1 : accumulator,
315 0
316 ),
317 executedTasks: this.workerNodes.reduce(
318 (accumulator, workerNode) =>
319 accumulator + workerNode.usage.tasks.executed,
320 0
321 ),
322 executingTasks: this.workerNodes.reduce(
323 (accumulator, workerNode) =>
324 accumulator + workerNode.usage.tasks.executing,
325 0
326 ),
327 ...(this.opts.enableTasksQueue === true && {
328 queuedTasks: this.workerNodes.reduce(
329 (accumulator, workerNode) =>
330 accumulator + workerNode.usage.tasks.queued,
331 0
332 )
333 }),
334 ...(this.opts.enableTasksQueue === true && {
335 maxQueuedTasks: this.workerNodes.reduce(
336 (accumulator, workerNode) =>
337 accumulator + (workerNode.usage.tasks?.maxQueued ?? 0),
338 0
339 )
340 }),
341 ...(this.opts.enableTasksQueue === true && {
342 backPressure: this.hasBackPressure()
343 }),
344 ...(this.opts.enableTasksQueue === true && {
345 stolenTasks: this.workerNodes.reduce(
346 (accumulator, workerNode) =>
347 accumulator + workerNode.usage.tasks.stolen,
348 0
349 )
350 }),
351 failedTasks: this.workerNodes.reduce(
352 (accumulator, workerNode) =>
353 accumulator + workerNode.usage.tasks.failed,
354 0
355 ),
356 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
357 .runTime.aggregate && {
358 runTime: {
359 minimum: round(
360 min(
361 ...this.workerNodes.map(
362 workerNode => workerNode.usage.runTime?.minimum ?? Infinity
363 )
364 )
365 ),
366 maximum: round(
367 max(
368 ...this.workerNodes.map(
369 workerNode => workerNode.usage.runTime?.maximum ?? -Infinity
370 )
371 )
372 ),
373 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
374 .runTime.average && {
375 average: round(
376 average(
377 this.workerNodes.reduce<number[]>(
378 (accumulator, workerNode) =>
379 accumulator.concat(workerNode.usage.runTime.history),
380 []
381 )
382 )
383 )
384 }),
385 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
386 .runTime.median && {
387 median: round(
388 median(
389 this.workerNodes.reduce<number[]>(
390 (accumulator, workerNode) =>
391 accumulator.concat(workerNode.usage.runTime.history),
392 []
393 )
394 )
395 )
396 })
397 }
398 }),
399 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
400 .waitTime.aggregate && {
401 waitTime: {
402 minimum: round(
403 min(
404 ...this.workerNodes.map(
405 workerNode => workerNode.usage.waitTime?.minimum ?? Infinity
406 )
407 )
408 ),
409 maximum: round(
410 max(
411 ...this.workerNodes.map(
412 workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity
413 )
414 )
415 ),
416 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
417 .waitTime.average && {
418 average: round(
419 average(
420 this.workerNodes.reduce<number[]>(
421 (accumulator, workerNode) =>
422 accumulator.concat(workerNode.usage.waitTime.history),
423 []
424 )
425 )
426 )
427 }),
428 ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
429 .waitTime.median && {
430 median: round(
431 median(
432 this.workerNodes.reduce<number[]>(
433 (accumulator, workerNode) =>
434 accumulator.concat(workerNode.usage.waitTime.history),
435 []
436 )
437 )
438 )
439 })
440 }
441 })
442 }
443 }
444
445 /**
446 * The pool readiness boolean status.
447 */
448 private get ready (): boolean {
449 return (
450 this.workerNodes.reduce(
451 (accumulator, workerNode) =>
452 !workerNode.info.dynamic && workerNode.info.ready
453 ? accumulator + 1
454 : accumulator,
455 0
456 ) >= this.minSize
457 )
458 }
459
460 /**
461 * The approximate pool utilization.
462 *
463 * @returns The pool utilization.
464 */
465 private get utilization (): number {
466 const poolTimeCapacity =
467 (performance.now() - this.startTimestamp) * this.maxSize
468 const totalTasksRunTime = this.workerNodes.reduce(
469 (accumulator, workerNode) =>
470 accumulator + (workerNode.usage.runTime?.aggregate ?? 0),
471 0
472 )
473 const totalTasksWaitTime = this.workerNodes.reduce(
474 (accumulator, workerNode) =>
475 accumulator + (workerNode.usage.waitTime?.aggregate ?? 0),
476 0
477 )
478 return (totalTasksRunTime + totalTasksWaitTime) / poolTimeCapacity
479 }
480
481 /**
482 * The pool type.
483 *
484 * If it is `'dynamic'`, it provides the `max` property.
485 */
486 protected abstract get type (): PoolType
487
488 /**
489 * The worker type.
490 */
491 protected abstract get worker (): WorkerType
492
493 /**
494 * The pool minimum size.
495 */
496 protected get minSize (): number {
497 return this.numberOfWorkers
498 }
499
500 /**
501 * The pool maximum size.
502 */
503 protected get maxSize (): number {
504 return this.max ?? this.numberOfWorkers
505 }
506
507 /**
508 * Checks if the worker id sent in the received message from a worker is valid.
509 *
510 * @param message - The received message.
511 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
512 */
513 private checkMessageWorkerId (message: MessageValue<Data | Response>): void {
514 if (message.workerId == null) {
515 throw new Error('Worker message received without worker id')
516 } else if (this.getWorkerNodeKeyByWorkerId(message.workerId) === -1) {
517 throw new Error(
518 `Worker message received from unknown worker '${message.workerId}'`
519 )
520 }
521 }
522
523 /**
524 * Gets the worker node key given its worker id.
525 *
526 * @param workerId - The worker id.
527 * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
528 */
529 private getWorkerNodeKeyByWorkerId (workerId: number | undefined): number {
530 return this.workerNodes.findIndex(
531 workerNode => workerNode.info.id === workerId
532 )
533 }
534
535 /** @inheritDoc */
536 public setWorkerChoiceStrategy (
537 workerChoiceStrategy: WorkerChoiceStrategy,
538 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
539 ): void {
540 checkValidWorkerChoiceStrategy(workerChoiceStrategy)
541 this.opts.workerChoiceStrategy = workerChoiceStrategy
542 this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
543 this.opts.workerChoiceStrategy
544 )
545 if (workerChoiceStrategyOptions != null) {
546 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
547 }
548 for (const [workerNodeKey, workerNode] of this.workerNodes.entries()) {
549 workerNode.resetUsage()
550 this.sendStatisticsMessageToWorker(workerNodeKey)
551 }
552 }
553
554 /** @inheritDoc */
555 public setWorkerChoiceStrategyOptions (
556 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
557 ): void {
558 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
559 this.opts.workerChoiceStrategyOptions = {
560 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
561 ...workerChoiceStrategyOptions
562 }
563 this.workerChoiceStrategyContext.setOptions(
564 this.opts.workerChoiceStrategyOptions
565 )
566 }
567
568 /** @inheritDoc */
569 public enableTasksQueue (
570 enable: boolean,
571 tasksQueueOptions?: TasksQueueOptions
572 ): void {
573 if (this.opts.enableTasksQueue === true && !enable) {
574 this.unsetTaskStealing()
575 this.unsetTasksStealingOnBackPressure()
576 this.flushTasksQueues()
577 }
578 this.opts.enableTasksQueue = enable
579 this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions)
580 }
581
582 /** @inheritDoc */
583 public setTasksQueueOptions (tasksQueueOptions: TasksQueueOptions): void {
584 if (this.opts.enableTasksQueue === true) {
585 checkValidTasksQueueOptions(tasksQueueOptions)
586 this.opts.tasksQueueOptions =
587 this.buildTasksQueueOptions(tasksQueueOptions)
588 this.setTasksQueueSize(this.opts.tasksQueueOptions.size as number)
589 if (this.opts.tasksQueueOptions.taskStealing === true) {
590 this.unsetTaskStealing()
591 this.setTaskStealing()
592 } else {
593 this.unsetTaskStealing()
594 }
595 if (this.opts.tasksQueueOptions.tasksStealingOnBackPressure === true) {
596 this.unsetTasksStealingOnBackPressure()
597 this.setTasksStealingOnBackPressure()
598 } else {
599 this.unsetTasksStealingOnBackPressure()
600 }
601 } else if (this.opts.tasksQueueOptions != null) {
602 delete this.opts.tasksQueueOptions
603 }
604 }
605
606 private buildTasksQueueOptions (
607 tasksQueueOptions: TasksQueueOptions
608 ): TasksQueueOptions {
609 return {
610 ...getDefaultTasksQueueOptions(this.maxSize),
611 ...tasksQueueOptions
612 }
613 }
614
615 private setTasksQueueSize (size: number): void {
616 for (const workerNode of this.workerNodes) {
617 workerNode.tasksQueueBackPressureSize = size
618 }
619 }
620
621 private setTaskStealing (): void {
622 for (const [workerNodeKey] of this.workerNodes.entries()) {
623 this.workerNodes[workerNodeKey].on(
624 'idleWorkerNode',
625 this.handleIdleWorkerNodeEvent
626 )
627 }
628 }
629
630 private unsetTaskStealing (): void {
631 for (const [workerNodeKey] of this.workerNodes.entries()) {
632 this.workerNodes[workerNodeKey].off(
633 'idleWorkerNode',
634 this.handleIdleWorkerNodeEvent
635 )
636 }
637 }
638
639 private setTasksStealingOnBackPressure (): void {
640 for (const [workerNodeKey] of this.workerNodes.entries()) {
641 this.workerNodes[workerNodeKey].on(
642 'backPressure',
643 this.handleBackPressureEvent
644 )
645 }
646 }
647
648 private unsetTasksStealingOnBackPressure (): void {
649 for (const [workerNodeKey] of this.workerNodes.entries()) {
650 this.workerNodes[workerNodeKey].off(
651 'backPressure',
652 this.handleBackPressureEvent
653 )
654 }
655 }
656
657 /**
658 * Whether the pool is full or not.
659 *
660 * The pool filling boolean status.
661 */
662 protected get full (): boolean {
663 return this.workerNodes.length >= this.maxSize
664 }
665
666 /**
667 * Whether the pool is busy or not.
668 *
669 * The pool busyness boolean status.
670 */
671 protected abstract get busy (): boolean
672
673 /**
674 * Whether worker nodes are executing concurrently their tasks quota or not.
675 *
676 * @returns Worker nodes busyness boolean status.
677 */
678 protected internalBusy (): boolean {
679 if (this.opts.enableTasksQueue === true) {
680 return (
681 this.workerNodes.findIndex(
682 workerNode =>
683 workerNode.info.ready &&
684 workerNode.usage.tasks.executing <
685 (this.opts.tasksQueueOptions?.concurrency as number)
686 ) === -1
687 )
688 }
689 return (
690 this.workerNodes.findIndex(
691 workerNode =>
692 workerNode.info.ready && workerNode.usage.tasks.executing === 0
693 ) === -1
694 )
695 }
696
697 private isWorkerNodeBusy (workerNodeKey: number): boolean {
698 if (this.opts.enableTasksQueue === true) {
699 return (
700 this.workerNodes[workerNodeKey].usage.tasks.executing >=
701 (this.opts.tasksQueueOptions?.concurrency as number)
702 )
703 }
704 return this.workerNodes[workerNodeKey].usage.tasks.executing > 0
705 }
706
707 private async sendTaskFunctionOperationToWorker (
708 workerNodeKey: number,
709 message: MessageValue<Data>
710 ): Promise<boolean> {
711 return await new Promise<boolean>((resolve, reject) => {
712 const taskFunctionOperationListener = (
713 message: MessageValue<Response>
714 ): void => {
715 this.checkMessageWorkerId(message)
716 const workerId = this.getWorkerInfo(workerNodeKey).id as number
717 if (
718 message.taskFunctionOperationStatus != null &&
719 message.workerId === workerId
720 ) {
721 if (message.taskFunctionOperationStatus) {
722 resolve(true)
723 } else if (!message.taskFunctionOperationStatus) {
724 reject(
725 new Error(
726 `Task function operation '${
727 message.taskFunctionOperation as string
728 }' failed on worker ${message.workerId} with error: '${
729 message.workerError?.message as string
730 }'`
731 )
732 )
733 }
734 this.deregisterWorkerMessageListener(
735 this.getWorkerNodeKeyByWorkerId(message.workerId),
736 taskFunctionOperationListener
737 )
738 }
739 }
740 this.registerWorkerMessageListener(
741 workerNodeKey,
742 taskFunctionOperationListener
743 )
744 this.sendToWorker(workerNodeKey, message)
745 })
746 }
747
748 private async sendTaskFunctionOperationToWorkers (
749 message: MessageValue<Data>
750 ): Promise<boolean> {
751 return await new Promise<boolean>((resolve, reject) => {
752 const responsesReceived = new Array<MessageValue<Response>>()
753 const taskFunctionOperationsListener = (
754 message: MessageValue<Response>
755 ): void => {
756 this.checkMessageWorkerId(message)
757 if (message.taskFunctionOperationStatus != null) {
758 responsesReceived.push(message)
759 if (responsesReceived.length === this.workerNodes.length) {
760 if (
761 responsesReceived.every(
762 message => message.taskFunctionOperationStatus === true
763 )
764 ) {
765 resolve(true)
766 } else if (
767 responsesReceived.some(
768 message => message.taskFunctionOperationStatus === false
769 )
770 ) {
771 const errorResponse = responsesReceived.find(
772 response => response.taskFunctionOperationStatus === false
773 )
774 reject(
775 new Error(
776 `Task function operation '${
777 message.taskFunctionOperation as string
778 }' failed on worker ${
779 errorResponse?.workerId as number
780 } with error: '${
781 errorResponse?.workerError?.message as string
782 }'`
783 )
784 )
785 }
786 this.deregisterWorkerMessageListener(
787 this.getWorkerNodeKeyByWorkerId(message.workerId),
788 taskFunctionOperationsListener
789 )
790 }
791 }
792 }
793 for (const [workerNodeKey] of this.workerNodes.entries()) {
794 this.registerWorkerMessageListener(
795 workerNodeKey,
796 taskFunctionOperationsListener
797 )
798 this.sendToWorker(workerNodeKey, message)
799 }
800 })
801 }
802
803 /** @inheritDoc */
804 public hasTaskFunction (name: string): boolean {
805 for (const workerNode of this.workerNodes) {
806 if (
807 Array.isArray(workerNode.info.taskFunctionNames) &&
808 workerNode.info.taskFunctionNames.includes(name)
809 ) {
810 return true
811 }
812 }
813 return false
814 }
815
816 /** @inheritDoc */
817 public async addTaskFunction (
818 name: string,
819 fn: TaskFunction<Data, Response>
820 ): Promise<boolean> {
821 if (typeof name !== 'string') {
822 throw new TypeError('name argument must be a string')
823 }
824 if (typeof name === 'string' && name.trim().length === 0) {
825 throw new TypeError('name argument must not be an empty string')
826 }
827 if (typeof fn !== 'function') {
828 throw new TypeError('fn argument must be a function')
829 }
830 const opResult = await this.sendTaskFunctionOperationToWorkers({
831 taskFunctionOperation: 'add',
832 taskFunctionName: name,
833 taskFunction: fn.toString()
834 })
835 this.taskFunctions.set(name, fn)
836 return opResult
837 }
838
839 /** @inheritDoc */
840 public async removeTaskFunction (name: string): Promise<boolean> {
841 if (!this.taskFunctions.has(name)) {
842 throw new Error(
843 'Cannot remove a task function not handled on the pool side'
844 )
845 }
846 const opResult = await this.sendTaskFunctionOperationToWorkers({
847 taskFunctionOperation: 'remove',
848 taskFunctionName: name
849 })
850 this.deleteTaskFunctionWorkerUsages(name)
851 this.taskFunctions.delete(name)
852 return opResult
853 }
854
855 /** @inheritDoc */
856 public listTaskFunctionNames (): string[] {
857 for (const workerNode of this.workerNodes) {
858 if (
859 Array.isArray(workerNode.info.taskFunctionNames) &&
860 workerNode.info.taskFunctionNames.length > 0
861 ) {
862 return workerNode.info.taskFunctionNames
863 }
864 }
865 return []
866 }
867
868 /** @inheritDoc */
869 public async setDefaultTaskFunction (name: string): Promise<boolean> {
870 return await this.sendTaskFunctionOperationToWorkers({
871 taskFunctionOperation: 'default',
872 taskFunctionName: name
873 })
874 }
875
876 private deleteTaskFunctionWorkerUsages (name: string): void {
877 for (const workerNode of this.workerNodes) {
878 workerNode.deleteTaskFunctionWorkerUsage(name)
879 }
880 }
881
882 private shallExecuteTask (workerNodeKey: number): boolean {
883 return (
884 this.tasksQueueSize(workerNodeKey) === 0 &&
885 this.workerNodes[workerNodeKey].usage.tasks.executing <
886 (this.opts.tasksQueueOptions?.concurrency as number)
887 )
888 }
889
890 /** @inheritDoc */
891 public async execute (
892 data?: Data,
893 name?: string,
894 transferList?: TransferListItem[]
895 ): Promise<Response> {
896 return await new Promise<Response>((resolve, reject) => {
897 if (!this.started) {
898 reject(new Error('Cannot execute a task on not started pool'))
899 return
900 }
901 if (this.destroying) {
902 reject(new Error('Cannot execute a task on destroying pool'))
903 return
904 }
905 if (name != null && typeof name !== 'string') {
906 reject(new TypeError('name argument must be a string'))
907 return
908 }
909 if (
910 name != null &&
911 typeof name === 'string' &&
912 name.trim().length === 0
913 ) {
914 reject(new TypeError('name argument must not be an empty string'))
915 return
916 }
917 if (transferList != null && !Array.isArray(transferList)) {
918 reject(new TypeError('transferList argument must be an array'))
919 return
920 }
921 const timestamp = performance.now()
922 const workerNodeKey = this.chooseWorkerNode()
923 const task: Task<Data> = {
924 name: name ?? DEFAULT_TASK_NAME,
925 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
926 data: data ?? ({} as Data),
927 transferList,
928 timestamp,
929 taskId: randomUUID()
930 }
931 this.promiseResponseMap.set(task.taskId as string, {
932 resolve,
933 reject,
934 workerNodeKey,
935 ...(this.emitter != null && {
936 asyncResource: new AsyncResource('poolifier:task', {
937 triggerAsyncId: this.emitter.asyncId,
938 requireManualDestroy: true
939 })
940 })
941 })
942 if (
943 this.opts.enableTasksQueue === false ||
944 (this.opts.enableTasksQueue === true &&
945 this.shallExecuteTask(workerNodeKey))
946 ) {
947 this.executeTask(workerNodeKey, task)
948 } else {
949 this.enqueueTask(workerNodeKey, task)
950 }
951 })
952 }
953
954 /** @inheritdoc */
955 public start (): void {
956 if (this.started) {
957 throw new Error('Cannot start an already started pool')
958 }
959 if (this.starting) {
960 throw new Error('Cannot start an already starting pool')
961 }
962 if (this.destroying) {
963 throw new Error('Cannot start a destroying pool')
964 }
965 this.starting = true
966 while (
967 this.workerNodes.reduce(
968 (accumulator, workerNode) =>
969 !workerNode.info.dynamic ? accumulator + 1 : accumulator,
970 0
971 ) < this.numberOfWorkers
972 ) {
973 this.createAndSetupWorkerNode()
974 }
975 this.starting = false
976 this.started = true
977 }
978
979 /** @inheritDoc */
980 public async destroy (): Promise<void> {
981 if (!this.started) {
982 throw new Error('Cannot destroy an already destroyed pool')
983 }
984 if (this.starting) {
985 throw new Error('Cannot destroy an starting pool')
986 }
987 if (this.destroying) {
988 throw new Error('Cannot destroy an already destroying pool')
989 }
990 this.destroying = true
991 await Promise.all(
992 this.workerNodes.map(async (_workerNode, workerNodeKey) => {
993 await this.destroyWorkerNode(workerNodeKey)
994 })
995 )
996 this.emitter?.emit(PoolEvents.destroy, this.info)
997 this.emitter?.emitDestroy()
998 this.emitter?.removeAllListeners()
999 this.readyEventEmitted = false
1000 this.destroying = false
1001 this.started = false
1002 }
1003
1004 protected async sendKillMessageToWorker (
1005 workerNodeKey: number
1006 ): Promise<void> {
1007 await new Promise<void>((resolve, reject) => {
1008 if (workerNodeKey < 0 || workerNodeKey >= this.workerNodes.length) {
1009 reject(new Error(`Invalid worker node key '${workerNodeKey}'`))
1010 return
1011 }
1012 const killMessageListener = (message: MessageValue<Response>): void => {
1013 this.checkMessageWorkerId(message)
1014 if (message.kill === 'success') {
1015 resolve()
1016 } else if (message.kill === 'failure') {
1017 reject(
1018 new Error(
1019 `Kill message handling failed on worker ${
1020 message.workerId as number
1021 }`
1022 )
1023 )
1024 }
1025 }
1026 // FIXME: should be registered only once
1027 this.registerWorkerMessageListener(workerNodeKey, killMessageListener)
1028 this.sendToWorker(workerNodeKey, { kill: true })
1029 })
1030 }
1031
1032 /**
1033 * Terminates the worker node given its worker node key.
1034 *
1035 * @param workerNodeKey - The worker node key.
1036 */
1037 protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
1038 this.flagWorkerNodeAsNotReady(workerNodeKey)
1039 const flushedTasks = this.flushTasksQueue(workerNodeKey)
1040 const workerNode = this.workerNodes[workerNodeKey]
1041 await waitWorkerNodeEvents(
1042 workerNode,
1043 'taskFinished',
1044 flushedTasks,
1045 this.opts.tasksQueueOptions?.tasksFinishedTimeout ??
1046 getDefaultTasksQueueOptions(this.maxSize).tasksFinishedTimeout
1047 )
1048 await this.sendKillMessageToWorker(workerNodeKey)
1049 await workerNode.terminate()
1050 }
1051
1052 /**
1053 * Setup hook to execute code before worker nodes are created in the abstract constructor.
1054 * Can be overridden.
1055 *
1056 * @virtual
1057 */
1058 protected setupHook (): void {
1059 /* Intentionally empty */
1060 }
1061
1062 /**
1063 * Should return whether the worker is the main worker or not.
1064 */
1065 protected abstract isMain (): boolean
1066
1067 /**
1068 * Hook executed before the worker task execution.
1069 * Can be overridden.
1070 *
1071 * @param workerNodeKey - The worker node key.
1072 * @param task - The task to execute.
1073 */
1074 protected beforeTaskExecutionHook (
1075 workerNodeKey: number,
1076 task: Task<Data>
1077 ): void {
1078 if (this.workerNodes[workerNodeKey]?.usage != null) {
1079 const workerUsage = this.workerNodes[workerNodeKey].usage
1080 ++workerUsage.tasks.executing
1081 updateWaitTimeWorkerUsage(
1082 this.workerChoiceStrategyContext,
1083 workerUsage,
1084 task
1085 )
1086 }
1087 if (
1088 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1089 this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage(
1090 task.name as string
1091 ) != null
1092 ) {
1093 const taskFunctionWorkerUsage = this.workerNodes[
1094 workerNodeKey
1095 ].getTaskFunctionWorkerUsage(task.name as string) as WorkerUsage
1096 ++taskFunctionWorkerUsage.tasks.executing
1097 updateWaitTimeWorkerUsage(
1098 this.workerChoiceStrategyContext,
1099 taskFunctionWorkerUsage,
1100 task
1101 )
1102 }
1103 }
1104
1105 /**
1106 * Hook executed after the worker task execution.
1107 * Can be overridden.
1108 *
1109 * @param workerNodeKey - The worker node key.
1110 * @param message - The received message.
1111 */
1112 protected afterTaskExecutionHook (
1113 workerNodeKey: number,
1114 message: MessageValue<Response>
1115 ): void {
1116 let needWorkerChoiceStrategyUpdate = false
1117 if (this.workerNodes[workerNodeKey]?.usage != null) {
1118 const workerUsage = this.workerNodes[workerNodeKey].usage
1119 updateTaskStatisticsWorkerUsage(workerUsage, message)
1120 updateRunTimeWorkerUsage(
1121 this.workerChoiceStrategyContext,
1122 workerUsage,
1123 message
1124 )
1125 updateEluWorkerUsage(
1126 this.workerChoiceStrategyContext,
1127 workerUsage,
1128 message
1129 )
1130 needWorkerChoiceStrategyUpdate = true
1131 }
1132 if (
1133 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1134 this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage(
1135 message.taskPerformance?.name as string
1136 ) != null
1137 ) {
1138 const taskFunctionWorkerUsage = this.workerNodes[
1139 workerNodeKey
1140 ].getTaskFunctionWorkerUsage(
1141 message.taskPerformance?.name as string
1142 ) as WorkerUsage
1143 updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage, message)
1144 updateRunTimeWorkerUsage(
1145 this.workerChoiceStrategyContext,
1146 taskFunctionWorkerUsage,
1147 message
1148 )
1149 updateEluWorkerUsage(
1150 this.workerChoiceStrategyContext,
1151 taskFunctionWorkerUsage,
1152 message
1153 )
1154 needWorkerChoiceStrategyUpdate = true
1155 }
1156 if (needWorkerChoiceStrategyUpdate) {
1157 this.workerChoiceStrategyContext.update(workerNodeKey)
1158 }
1159 }
1160
1161 /**
1162 * Whether the worker node shall update its task function worker usage or not.
1163 *
1164 * @param workerNodeKey - The worker node key.
1165 * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
1166 */
1167 private shallUpdateTaskFunctionWorkerUsage (workerNodeKey: number): boolean {
1168 const workerInfo = this.getWorkerInfo(workerNodeKey)
1169 return (
1170 workerInfo != null &&
1171 Array.isArray(workerInfo.taskFunctionNames) &&
1172 workerInfo.taskFunctionNames.length > 2
1173 )
1174 }
1175
1176 /**
1177 * Chooses a worker node for the next task.
1178 *
1179 * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
1180 *
1181 * @returns The chosen worker node key
1182 */
1183 private chooseWorkerNode (): number {
1184 if (this.shallCreateDynamicWorker()) {
1185 const workerNodeKey = this.createAndSetupDynamicWorkerNode()
1186 if (
1187 this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage
1188 ) {
1189 return workerNodeKey
1190 }
1191 }
1192 return this.workerChoiceStrategyContext.execute()
1193 }
1194
1195 /**
1196 * Conditions for dynamic worker creation.
1197 *
1198 * @returns Whether to create a dynamic worker or not.
1199 */
1200 private shallCreateDynamicWorker (): boolean {
1201 return this.type === PoolTypes.dynamic && !this.full && this.internalBusy()
1202 }
1203
1204 /**
1205 * Sends a message to worker given its worker node key.
1206 *
1207 * @param workerNodeKey - The worker node key.
1208 * @param message - The message.
1209 * @param transferList - The optional array of transferable objects.
1210 */
1211 protected abstract sendToWorker (
1212 workerNodeKey: number,
1213 message: MessageValue<Data>,
1214 transferList?: TransferListItem[]
1215 ): void
1216
1217 /**
1218 * Creates a new, completely set up worker node.
1219 *
1220 * @returns New, completely set up worker node key.
1221 */
1222 protected createAndSetupWorkerNode (): number {
1223 const workerNode = this.createWorkerNode()
1224 workerNode.registerWorkerEventHandler(
1225 'online',
1226 this.opts.onlineHandler ?? EMPTY_FUNCTION
1227 )
1228 workerNode.registerWorkerEventHandler(
1229 'message',
1230 this.opts.messageHandler ?? EMPTY_FUNCTION
1231 )
1232 workerNode.registerWorkerEventHandler(
1233 'error',
1234 this.opts.errorHandler ?? EMPTY_FUNCTION
1235 )
1236 workerNode.registerWorkerEventHandler('error', (error: Error) => {
1237 workerNode.info.ready = false
1238 this.emitter?.emit(PoolEvents.error, error)
1239 if (
1240 this.started &&
1241 !this.starting &&
1242 !this.destroying &&
1243 this.opts.restartWorkerOnError === true
1244 ) {
1245 if (workerNode.info.dynamic) {
1246 this.createAndSetupDynamicWorkerNode()
1247 } else {
1248 this.createAndSetupWorkerNode()
1249 }
1250 }
1251 if (this.started && this.opts.enableTasksQueue === true) {
1252 this.redistributeQueuedTasks(this.workerNodes.indexOf(workerNode))
1253 }
1254 workerNode?.terminate().catch(error => {
1255 this.emitter?.emit(PoolEvents.error, error)
1256 })
1257 })
1258 workerNode.registerWorkerEventHandler(
1259 'exit',
1260 this.opts.exitHandler ?? EMPTY_FUNCTION
1261 )
1262 workerNode.registerOnceWorkerEventHandler('exit', () => {
1263 this.removeWorkerNode(workerNode)
1264 })
1265 const workerNodeKey = this.addWorkerNode(workerNode)
1266 this.afterWorkerNodeSetup(workerNodeKey)
1267 return workerNodeKey
1268 }
1269
1270 /**
1271 * Creates a new, completely set up dynamic worker node.
1272 *
1273 * @returns New, completely set up dynamic worker node key.
1274 */
1275 protected createAndSetupDynamicWorkerNode (): number {
1276 const workerNodeKey = this.createAndSetupWorkerNode()
1277 this.registerWorkerMessageListener(workerNodeKey, message => {
1278 this.checkMessageWorkerId(message)
1279 const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
1280 message.workerId
1281 )
1282 const workerUsage = this.workerNodes[localWorkerNodeKey].usage
1283 // Kill message received from worker
1284 if (
1285 isKillBehavior(KillBehaviors.HARD, message.kill) ||
1286 (isKillBehavior(KillBehaviors.SOFT, message.kill) &&
1287 ((this.opts.enableTasksQueue === false &&
1288 workerUsage.tasks.executing === 0) ||
1289 (this.opts.enableTasksQueue === true &&
1290 workerUsage.tasks.executing === 0 &&
1291 this.tasksQueueSize(localWorkerNodeKey) === 0)))
1292 ) {
1293 // Flag the worker node as not ready immediately
1294 this.flagWorkerNodeAsNotReady(localWorkerNodeKey)
1295 this.destroyWorkerNode(localWorkerNodeKey).catch(error => {
1296 this.emitter?.emit(PoolEvents.error, error)
1297 })
1298 }
1299 })
1300 const workerInfo = this.getWorkerInfo(workerNodeKey)
1301 this.sendToWorker(workerNodeKey, {
1302 checkActive: true
1303 })
1304 if (this.taskFunctions.size > 0) {
1305 for (const [taskFunctionName, taskFunction] of this.taskFunctions) {
1306 this.sendTaskFunctionOperationToWorker(workerNodeKey, {
1307 taskFunctionOperation: 'add',
1308 taskFunctionName,
1309 taskFunction: taskFunction.toString()
1310 }).catch(error => {
1311 this.emitter?.emit(PoolEvents.error, error)
1312 })
1313 }
1314 }
1315 workerInfo.dynamic = true
1316 if (
1317 this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady ||
1318 this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage
1319 ) {
1320 workerInfo.ready = true
1321 }
1322 this.checkAndEmitDynamicWorkerCreationEvents()
1323 return workerNodeKey
1324 }
1325
1326 /**
1327 * Registers a listener callback on the worker given its worker node key.
1328 *
1329 * @param workerNodeKey - The worker node key.
1330 * @param listener - The message listener callback.
1331 */
1332 protected abstract registerWorkerMessageListener<
1333 Message extends Data | Response
1334 >(
1335 workerNodeKey: number,
1336 listener: (message: MessageValue<Message>) => void
1337 ): void
1338
1339 /**
1340 * Registers once a listener callback on the worker given its worker node key.
1341 *
1342 * @param workerNodeKey - The worker node key.
1343 * @param listener - The message listener callback.
1344 */
1345 protected abstract registerOnceWorkerMessageListener<
1346 Message extends Data | Response
1347 >(
1348 workerNodeKey: number,
1349 listener: (message: MessageValue<Message>) => void
1350 ): void
1351
1352 /**
1353 * Deregisters a listener callback on the worker given its worker node key.
1354 *
1355 * @param workerNodeKey - The worker node key.
1356 * @param listener - The message listener callback.
1357 */
1358 protected abstract deregisterWorkerMessageListener<
1359 Message extends Data | Response
1360 >(
1361 workerNodeKey: number,
1362 listener: (message: MessageValue<Message>) => void
1363 ): void
1364
1365 /**
1366 * Method hooked up after a worker node has been newly created.
1367 * Can be overridden.
1368 *
1369 * @param workerNodeKey - The newly created worker node key.
1370 */
1371 protected afterWorkerNodeSetup (workerNodeKey: number): void {
1372 // Listen to worker messages.
1373 this.registerWorkerMessageListener(
1374 workerNodeKey,
1375 this.workerMessageListener
1376 )
1377 // Send the startup message to worker.
1378 this.sendStartupMessageToWorker(workerNodeKey)
1379 // Send the statistics message to worker.
1380 this.sendStatisticsMessageToWorker(workerNodeKey)
1381 if (this.opts.enableTasksQueue === true) {
1382 if (this.opts.tasksQueueOptions?.taskStealing === true) {
1383 this.workerNodes[workerNodeKey].on(
1384 'idleWorkerNode',
1385 this.handleIdleWorkerNodeEvent
1386 )
1387 }
1388 if (this.opts.tasksQueueOptions?.tasksStealingOnBackPressure === true) {
1389 this.workerNodes[workerNodeKey].on(
1390 'backPressure',
1391 this.handleBackPressureEvent
1392 )
1393 }
1394 }
1395 }
1396
1397 /**
1398 * Sends the startup message to worker given its worker node key.
1399 *
1400 * @param workerNodeKey - The worker node key.
1401 */
1402 protected abstract sendStartupMessageToWorker (workerNodeKey: number): void
1403
1404 /**
1405 * Sends the statistics message to worker given its worker node key.
1406 *
1407 * @param workerNodeKey - The worker node key.
1408 */
1409 private sendStatisticsMessageToWorker (workerNodeKey: number): void {
1410 this.sendToWorker(workerNodeKey, {
1411 statistics: {
1412 runTime:
1413 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
1414 .runTime.aggregate,
1415 elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
1416 .elu.aggregate
1417 }
1418 })
1419 }
1420
1421 private handleTask (workerNodeKey: number, task: Task<Data>): void {
1422 if (this.shallExecuteTask(workerNodeKey)) {
1423 this.executeTask(workerNodeKey, task)
1424 } else {
1425 this.enqueueTask(workerNodeKey, task)
1426 }
1427 }
1428
1429 private redistributeQueuedTasks (workerNodeKey: number): void {
1430 if (workerNodeKey === -1) {
1431 return
1432 }
1433 if (this.workerNodes.length <= 1) {
1434 return
1435 }
1436 while (this.tasksQueueSize(workerNodeKey) > 0) {
1437 const destinationWorkerNodeKey = this.workerNodes.reduce(
1438 (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
1439 return workerNode.info.ready &&
1440 workerNode.usage.tasks.queued <
1441 workerNodes[minWorkerNodeKey].usage.tasks.queued
1442 ? workerNodeKey
1443 : minWorkerNodeKey
1444 },
1445 0
1446 )
1447 this.handleTask(
1448 destinationWorkerNodeKey,
1449 this.dequeueTask(workerNodeKey) as Task<Data>
1450 )
1451 }
1452 }
1453
1454 private updateTaskStolenStatisticsWorkerUsage (
1455 workerNodeKey: number,
1456 taskName: string
1457 ): void {
1458 const workerNode = this.workerNodes[workerNodeKey]
1459 if (workerNode?.usage != null) {
1460 ++workerNode.usage.tasks.stolen
1461 }
1462 if (
1463 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1464 workerNode.getTaskFunctionWorkerUsage(taskName) != null
1465 ) {
1466 const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage(
1467 taskName
1468 ) as WorkerUsage
1469 ++taskFunctionWorkerUsage.tasks.stolen
1470 }
1471 }
1472
1473 private updateTaskSequentiallyStolenStatisticsWorkerUsage (
1474 workerNodeKey: number
1475 ): void {
1476 const workerNode = this.workerNodes[workerNodeKey]
1477 if (workerNode?.usage != null) {
1478 ++workerNode.usage.tasks.sequentiallyStolen
1479 }
1480 }
1481
1482 private updateTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage (
1483 workerNodeKey: number,
1484 taskName: string
1485 ): void {
1486 const workerNode = this.workerNodes[workerNodeKey]
1487 if (
1488 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1489 workerNode.getTaskFunctionWorkerUsage(taskName) != null
1490 ) {
1491 const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage(
1492 taskName
1493 ) as WorkerUsage
1494 ++taskFunctionWorkerUsage.tasks.sequentiallyStolen
1495 }
1496 }
1497
1498 private resetTaskSequentiallyStolenStatisticsWorkerUsage (
1499 workerNodeKey: number
1500 ): void {
1501 const workerNode = this.workerNodes[workerNodeKey]
1502 if (workerNode?.usage != null) {
1503 workerNode.usage.tasks.sequentiallyStolen = 0
1504 }
1505 }
1506
1507 private resetTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage (
1508 workerNodeKey: number,
1509 taskName: string
1510 ): void {
1511 const workerNode = this.workerNodes[workerNodeKey]
1512 if (
1513 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1514 workerNode.getTaskFunctionWorkerUsage(taskName) != null
1515 ) {
1516 const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage(
1517 taskName
1518 ) as WorkerUsage
1519 taskFunctionWorkerUsage.tasks.sequentiallyStolen = 0
1520 }
1521 }
1522
1523 private readonly handleIdleWorkerNodeEvent = (
1524 eventDetail: WorkerNodeEventDetail,
1525 previousStolenTask?: Task<Data>
1526 ): void => {
1527 if (this.workerNodes.length <= 1) {
1528 return
1529 }
1530 const { workerNodeKey } = eventDetail
1531 if (workerNodeKey == null) {
1532 throw new Error(
1533 'WorkerNode event detail workerNodeKey attribute must be defined'
1534 )
1535 }
1536 const workerNodeTasksUsage = this.workerNodes[workerNodeKey].usage.tasks
1537 if (
1538 previousStolenTask != null &&
1539 workerNodeTasksUsage.sequentiallyStolen > 0 &&
1540 (workerNodeTasksUsage.executing > 0 ||
1541 this.tasksQueueSize(workerNodeKey) > 0)
1542 ) {
1543 for (const taskName of this.workerNodes[workerNodeKey].info
1544 .taskFunctionNames as string[]) {
1545 this.resetTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage(
1546 workerNodeKey,
1547 taskName
1548 )
1549 }
1550 this.resetTaskSequentiallyStolenStatisticsWorkerUsage(workerNodeKey)
1551 return
1552 }
1553 const stolenTask = this.workerNodeStealTask(workerNodeKey)
1554 if (
1555 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) &&
1556 stolenTask != null
1557 ) {
1558 const taskFunctionTasksWorkerUsage = this.workerNodes[
1559 workerNodeKey
1560 ].getTaskFunctionWorkerUsage(stolenTask.name as string)
1561 ?.tasks as TaskStatistics
1562 if (
1563 taskFunctionTasksWorkerUsage.sequentiallyStolen === 0 ||
1564 (previousStolenTask != null &&
1565 previousStolenTask.name === stolenTask.name &&
1566 taskFunctionTasksWorkerUsage.sequentiallyStolen > 0)
1567 ) {
1568 this.updateTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage(
1569 workerNodeKey,
1570 stolenTask.name as string
1571 )
1572 } else {
1573 this.resetTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage(
1574 workerNodeKey,
1575 stolenTask.name as string
1576 )
1577 }
1578 }
1579 sleep(exponentialDelay(workerNodeTasksUsage.sequentiallyStolen))
1580 .then(() => {
1581 this.handleIdleWorkerNodeEvent(eventDetail, stolenTask)
1582 return undefined
1583 })
1584 .catch(EMPTY_FUNCTION)
1585 }
1586
1587 private readonly workerNodeStealTask = (
1588 workerNodeKey: number
1589 ): Task<Data> | undefined => {
1590 const workerNodes = this.workerNodes
1591 .slice()
1592 .sort(
1593 (workerNodeA, workerNodeB) =>
1594 workerNodeB.usage.tasks.queued - workerNodeA.usage.tasks.queued
1595 )
1596 const sourceWorkerNode = workerNodes.find(
1597 (sourceWorkerNode, sourceWorkerNodeKey) =>
1598 sourceWorkerNode.info.ready &&
1599 sourceWorkerNodeKey !== workerNodeKey &&
1600 sourceWorkerNode.usage.tasks.queued > 0
1601 )
1602 if (sourceWorkerNode != null) {
1603 const task = sourceWorkerNode.popTask() as Task<Data>
1604 this.handleTask(workerNodeKey, task)
1605 this.updateTaskSequentiallyStolenStatisticsWorkerUsage(workerNodeKey)
1606 this.updateTaskStolenStatisticsWorkerUsage(
1607 workerNodeKey,
1608 task.name as string
1609 )
1610 return task
1611 }
1612 }
1613
1614 private readonly handleBackPressureEvent = (
1615 eventDetail: WorkerNodeEventDetail
1616 ): void => {
1617 if (this.workerNodes.length <= 1) {
1618 return
1619 }
1620 const { workerId } = eventDetail
1621 const sizeOffset = 1
1622 if ((this.opts.tasksQueueOptions?.size as number) <= sizeOffset) {
1623 return
1624 }
1625 const sourceWorkerNode =
1626 this.workerNodes[this.getWorkerNodeKeyByWorkerId(workerId)]
1627 const workerNodes = this.workerNodes
1628 .slice()
1629 .sort(
1630 (workerNodeA, workerNodeB) =>
1631 workerNodeA.usage.tasks.queued - workerNodeB.usage.tasks.queued
1632 )
1633 for (const [workerNodeKey, workerNode] of workerNodes.entries()) {
1634 if (
1635 sourceWorkerNode.usage.tasks.queued > 0 &&
1636 workerNode.info.ready &&
1637 workerNode.info.id !== workerId &&
1638 workerNode.usage.tasks.queued <
1639 (this.opts.tasksQueueOptions?.size as number) - sizeOffset
1640 ) {
1641 const task = sourceWorkerNode.popTask() as Task<Data>
1642 this.handleTask(workerNodeKey, task)
1643 this.updateTaskStolenStatisticsWorkerUsage(
1644 workerNodeKey,
1645 task.name as string
1646 )
1647 }
1648 }
1649 }
1650
1651 /**
1652 * This method is the message listener registered on each worker.
1653 */
1654 protected readonly workerMessageListener = (
1655 message: MessageValue<Response>
1656 ): void => {
1657 this.checkMessageWorkerId(message)
1658 const { workerId, ready, taskId, taskFunctionNames } = message
1659 if (ready != null && taskFunctionNames != null) {
1660 // Worker ready response received from worker
1661 this.handleWorkerReadyResponse(message)
1662 } else if (taskId != null) {
1663 // Task execution response received from worker
1664 this.handleTaskExecutionResponse(message)
1665 } else if (taskFunctionNames != null) {
1666 // Task function names message received from worker
1667 this.getWorkerInfo(
1668 this.getWorkerNodeKeyByWorkerId(workerId)
1669 ).taskFunctionNames = taskFunctionNames
1670 }
1671 }
1672
1673 private handleWorkerReadyResponse (message: MessageValue<Response>): void {
1674 const { workerId, ready, taskFunctionNames } = message
1675 if (ready === false) {
1676 throw new Error(`Worker ${workerId as number} failed to initialize`)
1677 }
1678 const workerInfo = this.getWorkerInfo(
1679 this.getWorkerNodeKeyByWorkerId(workerId)
1680 )
1681 workerInfo.ready = ready as boolean
1682 workerInfo.taskFunctionNames = taskFunctionNames
1683 if (!this.readyEventEmitted && this.ready) {
1684 this.readyEventEmitted = true
1685 this.emitter?.emit(PoolEvents.ready, this.info)
1686 }
1687 }
1688
1689 private handleTaskExecutionResponse (message: MessageValue<Response>): void {
1690 const { workerId, taskId, workerError, data } = message
1691 const promiseResponse = this.promiseResponseMap.get(taskId as string)
1692 if (promiseResponse != null) {
1693 const { resolve, reject, workerNodeKey, asyncResource } = promiseResponse
1694 const workerNode = this.workerNodes[workerNodeKey]
1695 if (workerError != null) {
1696 this.emitter?.emit(PoolEvents.taskError, workerError)
1697 asyncResource != null
1698 ? asyncResource.runInAsyncScope(
1699 reject,
1700 this.emitter,
1701 workerError.message
1702 )
1703 : reject(workerError.message)
1704 } else {
1705 asyncResource != null
1706 ? asyncResource.runInAsyncScope(resolve, this.emitter, data)
1707 : resolve(data as Response)
1708 }
1709 asyncResource?.emitDestroy()
1710 this.afterTaskExecutionHook(workerNodeKey, message)
1711 this.promiseResponseMap.delete(taskId as string)
1712 workerNode?.emit('taskFinished', taskId)
1713 if (this.opts.enableTasksQueue === true && !this.destroying) {
1714 const workerNodeTasksUsage = workerNode.usage.tasks
1715 if (
1716 this.tasksQueueSize(workerNodeKey) > 0 &&
1717 workerNodeTasksUsage.executing <
1718 (this.opts.tasksQueueOptions?.concurrency as number)
1719 ) {
1720 this.executeTask(
1721 workerNodeKey,
1722 this.dequeueTask(workerNodeKey) as Task<Data>
1723 )
1724 }
1725 if (
1726 workerNodeTasksUsage.executing === 0 &&
1727 this.tasksQueueSize(workerNodeKey) === 0 &&
1728 workerNodeTasksUsage.sequentiallyStolen === 0
1729 ) {
1730 workerNode.emit('idleWorkerNode', {
1731 workerId: workerId as number,
1732 workerNodeKey
1733 })
1734 }
1735 }
1736 }
1737 }
1738
1739 private checkAndEmitTaskExecutionEvents (): void {
1740 if (this.busy) {
1741 this.emitter?.emit(PoolEvents.busy, this.info)
1742 }
1743 }
1744
1745 private checkAndEmitTaskQueuingEvents (): void {
1746 if (this.hasBackPressure()) {
1747 this.emitter?.emit(PoolEvents.backPressure, this.info)
1748 }
1749 }
1750
1751 private checkAndEmitDynamicWorkerCreationEvents (): void {
1752 if (this.type === PoolTypes.dynamic) {
1753 if (this.full) {
1754 this.emitter?.emit(PoolEvents.full, this.info)
1755 }
1756 }
1757 }
1758
1759 /**
1760 * Gets the worker information given its worker node key.
1761 *
1762 * @param workerNodeKey - The worker node key.
1763 * @returns The worker information.
1764 */
1765 protected getWorkerInfo (workerNodeKey: number): WorkerInfo {
1766 return this.workerNodes[workerNodeKey]?.info
1767 }
1768
1769 /**
1770 * Creates a worker node.
1771 *
1772 * @returns The created worker node.
1773 */
1774 private createWorkerNode (): IWorkerNode<Worker, Data> {
1775 const workerNode = new WorkerNode<Worker, Data>(
1776 this.worker,
1777 this.filePath,
1778 {
1779 env: this.opts.env,
1780 workerOptions: this.opts.workerOptions,
1781 tasksQueueBackPressureSize:
1782 this.opts.tasksQueueOptions?.size ??
1783 getDefaultTasksQueueOptions(this.maxSize).size
1784 }
1785 )
1786 // Flag the worker node as ready at pool startup.
1787 if (this.starting) {
1788 workerNode.info.ready = true
1789 }
1790 return workerNode
1791 }
1792
1793 /**
1794 * Adds the given worker node in the pool worker nodes.
1795 *
1796 * @param workerNode - The worker node.
1797 * @returns The added worker node key.
1798 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1799 */
1800 private addWorkerNode (workerNode: IWorkerNode<Worker, Data>): number {
1801 this.workerNodes.push(workerNode)
1802 const workerNodeKey = this.workerNodes.indexOf(workerNode)
1803 if (workerNodeKey === -1) {
1804 throw new Error('Worker added not found in worker nodes')
1805 }
1806 return workerNodeKey
1807 }
1808
1809 /**
1810 * Removes the worker node from the pool worker nodes.
1811 *
1812 * @param workerNode - The worker node.
1813 */
1814 private removeWorkerNode (workerNode: IWorkerNode<Worker, Data>): void {
1815 const workerNodeKey = this.workerNodes.indexOf(workerNode)
1816 if (workerNodeKey !== -1) {
1817 this.workerNodes.splice(workerNodeKey, 1)
1818 this.workerChoiceStrategyContext.remove(workerNodeKey)
1819 }
1820 }
1821
1822 protected flagWorkerNodeAsNotReady (workerNodeKey: number): void {
1823 this.getWorkerInfo(workerNodeKey).ready = false
1824 }
1825
1826 /** @inheritDoc */
1827 public hasWorkerNodeBackPressure (workerNodeKey: number): boolean {
1828 return (
1829 this.opts.enableTasksQueue === true &&
1830 this.workerNodes[workerNodeKey].hasBackPressure()
1831 )
1832 }
1833
1834 private hasBackPressure (): boolean {
1835 return (
1836 this.opts.enableTasksQueue === true &&
1837 this.workerNodes.findIndex(
1838 workerNode => !workerNode.hasBackPressure()
1839 ) === -1
1840 )
1841 }
1842
1843 /**
1844 * Executes the given task on the worker given its worker node key.
1845 *
1846 * @param workerNodeKey - The worker node key.
1847 * @param task - The task to execute.
1848 */
1849 private executeTask (workerNodeKey: number, task: Task<Data>): void {
1850 this.beforeTaskExecutionHook(workerNodeKey, task)
1851 this.sendToWorker(workerNodeKey, task, task.transferList)
1852 this.checkAndEmitTaskExecutionEvents()
1853 }
1854
1855 private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
1856 const tasksQueueSize = this.workerNodes[workerNodeKey].enqueueTask(task)
1857 this.checkAndEmitTaskQueuingEvents()
1858 return tasksQueueSize
1859 }
1860
1861 private dequeueTask (workerNodeKey: number): Task<Data> | undefined {
1862 return this.workerNodes[workerNodeKey].dequeueTask()
1863 }
1864
1865 private tasksQueueSize (workerNodeKey: number): number {
1866 return this.workerNodes[workerNodeKey].tasksQueueSize()
1867 }
1868
1869 protected flushTasksQueue (workerNodeKey: number): number {
1870 let flushedTasks = 0
1871 while (this.tasksQueueSize(workerNodeKey) > 0) {
1872 this.executeTask(
1873 workerNodeKey,
1874 this.dequeueTask(workerNodeKey) as Task<Data>
1875 )
1876 ++flushedTasks
1877 }
1878 this.workerNodes[workerNodeKey].clearTasksQueue()
1879 return flushedTasks
1880 }
1881
1882 private flushTasksQueues (): void {
1883 for (const [workerNodeKey] of this.workerNodes.entries()) {
1884 this.flushTasksQueue(workerNodeKey)
1885 }
1886 }
1887 }