fix: fix task wait time computation
[poolifier.git] / src / pools / abstract-pool.ts
1 import crypto from 'node:crypto'
2 import { performance } from 'node:perf_hooks'
3 import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
4 import {
5 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
6 EMPTY_FUNCTION,
7 isPlainObject,
8 median
9 } from '../utils'
10 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
11 import { CircularArray } from '../circular-array'
12 import { Queue } from '../queue'
13 import {
14 type IPool,
15 PoolEmitter,
16 PoolEvents,
17 type PoolInfo,
18 type PoolOptions,
19 type PoolType,
20 PoolTypes,
21 type TasksQueueOptions,
22 type WorkerType
23 } from './pool'
24 import type {
25 IWorker,
26 Task,
27 TaskStatistics,
28 WorkerNode,
29 WorkerUsage
30 } from './worker'
31 import {
32 WorkerChoiceStrategies,
33 type WorkerChoiceStrategy,
34 type WorkerChoiceStrategyOptions
35 } from './selection-strategies/selection-strategies-types'
36 import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
37
38 /**
39 * Base class that implements some shared logic for all poolifier pools.
40 *
41 * @typeParam Worker - Type of worker which manages this pool.
42 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
43 * @typeParam Response - Type of execution response. This can only be serializable data.
44 */
45 export abstract class AbstractPool<
46 Worker extends IWorker,
47 Data = unknown,
48 Response = unknown
49 > implements IPool<Worker, Data, Response> {
50 /** @inheritDoc */
51 public readonly workerNodes: Array<WorkerNode<Worker, Data>> = []
52
53 /** @inheritDoc */
54 public readonly emitter?: PoolEmitter
55
56 /**
57 * The execution response promise map.
58 *
59 * - `key`: The message id of each submitted task.
60 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
61 *
62 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
63 */
64 protected promiseResponseMap: Map<
65 string,
66 PromiseResponseWrapper<Worker, Response>
67 > = new Map<string, PromiseResponseWrapper<Worker, Response>>()
68
69 /**
70 * Worker choice strategy context referencing a worker choice algorithm implementation.
71 *
72 * Default to a round robin algorithm.
73 */
74 protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
75 Worker,
76 Data,
77 Response
78 >
79
80 /**
81 * Constructs a new poolifier pool.
82 *
83 * @param numberOfWorkers - Number of workers that this pool should manage.
84 * @param filePath - Path to the worker file.
85 * @param opts - Options for the pool.
86 */
87 public constructor (
88 protected readonly numberOfWorkers: number,
89 protected readonly filePath: string,
90 protected readonly opts: PoolOptions<Worker>
91 ) {
92 if (!this.isMain()) {
93 throw new Error('Cannot start a pool from a worker!')
94 }
95 this.checkNumberOfWorkers(this.numberOfWorkers)
96 this.checkFilePath(this.filePath)
97 this.checkPoolOptions(this.opts)
98
99 this.chooseWorkerNode = this.chooseWorkerNode.bind(this)
100 this.executeTask = this.executeTask.bind(this)
101 this.enqueueTask = this.enqueueTask.bind(this)
102 this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
103
104 if (this.opts.enableEvents === true) {
105 this.emitter = new PoolEmitter()
106 }
107 this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext<
108 Worker,
109 Data,
110 Response
111 >(
112 this,
113 this.opts.workerChoiceStrategy,
114 this.opts.workerChoiceStrategyOptions
115 )
116
117 this.setupHook()
118
119 for (let i = 1; i <= this.numberOfWorkers; i++) {
120 this.createAndSetupWorker()
121 }
122 }
123
124 private checkFilePath (filePath: string): void {
125 if (
126 filePath == null ||
127 (typeof filePath === 'string' && filePath.trim().length === 0)
128 ) {
129 throw new Error('Please specify a file with a worker implementation')
130 }
131 }
132
133 private checkNumberOfWorkers (numberOfWorkers: number): void {
134 if (numberOfWorkers == null) {
135 throw new Error(
136 'Cannot instantiate a pool without specifying the number of workers'
137 )
138 } else if (!Number.isSafeInteger(numberOfWorkers)) {
139 throw new TypeError(
140 'Cannot instantiate a pool with a non safe integer number of workers'
141 )
142 } else if (numberOfWorkers < 0) {
143 throw new RangeError(
144 'Cannot instantiate a pool with a negative number of workers'
145 )
146 } else if (this.type === PoolTypes.fixed && numberOfWorkers === 0) {
147 throw new Error('Cannot instantiate a fixed pool with no worker')
148 }
149 }
150
151 private checkPoolOptions (opts: PoolOptions<Worker>): void {
152 if (isPlainObject(opts)) {
153 this.opts.workerChoiceStrategy =
154 opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
155 this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
156 this.opts.workerChoiceStrategyOptions =
157 opts.workerChoiceStrategyOptions ??
158 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
159 this.checkValidWorkerChoiceStrategyOptions(
160 this.opts.workerChoiceStrategyOptions
161 )
162 this.opts.restartWorkerOnError = opts.restartWorkerOnError ?? true
163 this.opts.enableEvents = opts.enableEvents ?? true
164 this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
165 if (this.opts.enableTasksQueue) {
166 this.checkValidTasksQueueOptions(
167 opts.tasksQueueOptions as TasksQueueOptions
168 )
169 this.opts.tasksQueueOptions = this.buildTasksQueueOptions(
170 opts.tasksQueueOptions as TasksQueueOptions
171 )
172 }
173 } else {
174 throw new TypeError('Invalid pool options: must be a plain object')
175 }
176 }
177
178 private checkValidWorkerChoiceStrategy (
179 workerChoiceStrategy: WorkerChoiceStrategy
180 ): void {
181 if (!Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)) {
182 throw new Error(
183 `Invalid worker choice strategy '${workerChoiceStrategy}'`
184 )
185 }
186 }
187
188 private checkValidWorkerChoiceStrategyOptions (
189 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
190 ): void {
191 if (!isPlainObject(workerChoiceStrategyOptions)) {
192 throw new TypeError(
193 'Invalid worker choice strategy options: must be a plain object'
194 )
195 }
196 if (
197 workerChoiceStrategyOptions.weights != null &&
198 Object.keys(workerChoiceStrategyOptions.weights).length !== this.maxSize
199 ) {
200 throw new Error(
201 'Invalid worker choice strategy options: must have a weight for each worker node'
202 )
203 }
204 }
205
206 private checkValidTasksQueueOptions (
207 tasksQueueOptions: TasksQueueOptions
208 ): void {
209 if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
210 throw new TypeError('Invalid tasks queue options: must be a plain object')
211 }
212 if ((tasksQueueOptions?.concurrency as number) <= 0) {
213 throw new Error(
214 `Invalid worker tasks concurrency '${
215 tasksQueueOptions.concurrency as number
216 }'`
217 )
218 }
219 }
220
221 /** @inheritDoc */
222 public get info (): PoolInfo {
223 return {
224 type: this.type,
225 worker: this.worker,
226 minSize: this.minSize,
227 maxSize: this.maxSize,
228 workerNodes: this.workerNodes.length,
229 idleWorkerNodes: this.workerNodes.reduce(
230 (accumulator, workerNode) =>
231 workerNode.workerUsage.tasks.executing === 0
232 ? accumulator + 1
233 : accumulator,
234 0
235 ),
236 busyWorkerNodes: this.workerNodes.reduce(
237 (accumulator, workerNode) =>
238 workerNode.workerUsage.tasks.executing > 0
239 ? accumulator + 1
240 : accumulator,
241 0
242 ),
243 executedTasks: this.workerNodes.reduce(
244 (accumulator, workerNode) =>
245 accumulator + workerNode.workerUsage.tasks.executed,
246 0
247 ),
248 executingTasks: this.workerNodes.reduce(
249 (accumulator, workerNode) =>
250 accumulator + workerNode.workerUsage.tasks.executing,
251 0
252 ),
253 queuedTasks: this.workerNodes.reduce(
254 (accumulator, workerNode) => accumulator + workerNode.tasksQueue.size,
255 0
256 ),
257 maxQueuedTasks: this.workerNodes.reduce(
258 (accumulator, workerNode) =>
259 accumulator + workerNode.tasksQueue.maxSize,
260 0
261 ),
262 failedTasks: this.workerNodes.reduce(
263 (accumulator, workerNode) =>
264 accumulator + workerNode.workerUsage.tasks.failed,
265 0
266 )
267 }
268 }
269
270 /**
271 * Pool type.
272 *
273 * If it is `'dynamic'`, it provides the `max` property.
274 */
275 protected abstract get type (): PoolType
276
277 /**
278 * Gets the worker type.
279 */
280 protected abstract get worker (): WorkerType
281
282 /**
283 * Pool minimum size.
284 */
285 protected abstract get minSize (): number
286
287 /**
288 * Pool maximum size.
289 */
290 protected abstract get maxSize (): number
291
292 /**
293 * Gets the given worker its worker node key.
294 *
295 * @param worker - The worker.
296 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
297 */
298 private getWorkerNodeKey (worker: Worker): number {
299 return this.workerNodes.findIndex(
300 workerNode => workerNode.worker === worker
301 )
302 }
303
304 /** @inheritDoc */
305 public setWorkerChoiceStrategy (
306 workerChoiceStrategy: WorkerChoiceStrategy,
307 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
308 ): void {
309 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy)
310 this.opts.workerChoiceStrategy = workerChoiceStrategy
311 this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
312 this.opts.workerChoiceStrategy
313 )
314 if (workerChoiceStrategyOptions != null) {
315 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
316 }
317 for (const workerNode of this.workerNodes) {
318 this.setWorkerNodeTasksUsage(
319 workerNode,
320 this.getWorkerUsage(workerNode.worker)
321 )
322 this.setWorkerStatistics(workerNode.worker)
323 }
324 }
325
326 /** @inheritDoc */
327 public setWorkerChoiceStrategyOptions (
328 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
329 ): void {
330 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
331 this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions
332 this.workerChoiceStrategyContext.setOptions(
333 this.opts.workerChoiceStrategyOptions
334 )
335 }
336
337 /** @inheritDoc */
338 public enableTasksQueue (
339 enable: boolean,
340 tasksQueueOptions?: TasksQueueOptions
341 ): void {
342 if (this.opts.enableTasksQueue === true && !enable) {
343 this.flushTasksQueues()
344 }
345 this.opts.enableTasksQueue = enable
346 this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions)
347 }
348
349 /** @inheritDoc */
350 public setTasksQueueOptions (tasksQueueOptions: TasksQueueOptions): void {
351 if (this.opts.enableTasksQueue === true) {
352 this.checkValidTasksQueueOptions(tasksQueueOptions)
353 this.opts.tasksQueueOptions =
354 this.buildTasksQueueOptions(tasksQueueOptions)
355 } else if (this.opts.tasksQueueOptions != null) {
356 delete this.opts.tasksQueueOptions
357 }
358 }
359
360 private buildTasksQueueOptions (
361 tasksQueueOptions: TasksQueueOptions
362 ): TasksQueueOptions {
363 return {
364 concurrency: tasksQueueOptions?.concurrency ?? 1
365 }
366 }
367
368 /**
369 * Whether the pool is full or not.
370 *
371 * The pool filling boolean status.
372 */
373 protected get full (): boolean {
374 return this.workerNodes.length >= this.maxSize
375 }
376
377 /**
378 * Whether the pool is busy or not.
379 *
380 * The pool busyness boolean status.
381 */
382 protected abstract get busy (): boolean
383
384 protected internalBusy (): boolean {
385 return (
386 this.workerNodes.findIndex(workerNode => {
387 return workerNode.workerUsage.tasks.executing === 0
388 }) === -1
389 )
390 }
391
392 /** @inheritDoc */
393 public async execute (data?: Data, name?: string): Promise<Response> {
394 const timestamp = performance.now()
395 const workerNodeKey = this.chooseWorkerNode()
396 const submittedTask: Task<Data> = {
397 name,
398 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
399 data: data ?? ({} as Data),
400 timestamp,
401 id: crypto.randomUUID()
402 }
403 const res = new Promise<Response>((resolve, reject) => {
404 this.promiseResponseMap.set(submittedTask.id as string, {
405 resolve,
406 reject,
407 worker: this.workerNodes[workerNodeKey].worker
408 })
409 })
410 if (
411 this.opts.enableTasksQueue === true &&
412 (this.busy ||
413 this.workerNodes[workerNodeKey].workerUsage.tasks.executing >=
414 ((this.opts.tasksQueueOptions as TasksQueueOptions)
415 .concurrency as number))
416 ) {
417 this.enqueueTask(workerNodeKey, submittedTask)
418 } else {
419 this.executeTask(workerNodeKey, submittedTask)
420 }
421 this.workerChoiceStrategyContext.update(workerNodeKey)
422 this.checkAndEmitEvents()
423 // eslint-disable-next-line @typescript-eslint/return-await
424 return res
425 }
426
427 /** @inheritDoc */
428 public async destroy (): Promise<void> {
429 await Promise.all(
430 this.workerNodes.map(async (workerNode, workerNodeKey) => {
431 this.flushTasksQueue(workerNodeKey)
432 // FIXME: wait for tasks to be finished
433 await this.destroyWorker(workerNode.worker)
434 })
435 )
436 }
437
438 /**
439 * Shutdowns the given worker.
440 *
441 * @param worker - A worker within `workerNodes`.
442 */
443 protected abstract destroyWorker (worker: Worker): void | Promise<void>
444
445 /**
446 * Setup hook to execute code before worker node are created in the abstract constructor.
447 * Can be overridden
448 *
449 * @virtual
450 */
451 protected setupHook (): void {
452 // Intentionally empty
453 }
454
455 /**
456 * Should return whether the worker is the main worker or not.
457 */
458 protected abstract isMain (): boolean
459
460 /**
461 * Hook executed before the worker task execution.
462 * Can be overridden.
463 *
464 * @param workerNodeKey - The worker node key.
465 * @param task - The task to execute.
466 */
467 protected beforeTaskExecutionHook (
468 workerNodeKey: number,
469 task: Task<Data>
470 ): void {
471 const workerUsage = this.workerNodes[workerNodeKey].workerUsage
472 ++workerUsage.tasks.executing
473 this.updateWaitTimeWorkerUsage(workerUsage, task)
474 }
475
476 /**
477 * Hook executed after the worker task execution.
478 * Can be overridden.
479 *
480 * @param worker - The worker.
481 * @param message - The received message.
482 */
483 protected afterTaskExecutionHook (
484 worker: Worker,
485 message: MessageValue<Response>
486 ): void {
487 const workerUsage =
488 this.workerNodes[this.getWorkerNodeKey(worker)].workerUsage
489 const workerTaskStatistics = workerUsage.tasks
490 --workerTaskStatistics.executing
491 ++workerTaskStatistics.executed
492 if (message.taskError != null) {
493 ++workerTaskStatistics.failed
494 }
495 this.updateRunTimeWorkerUsage(workerUsage, message)
496 this.updateEluWorkerUsage(workerUsage, message)
497 }
498
499 private updateRunTimeWorkerUsage (
500 workerUsage: WorkerUsage,
501 message: MessageValue<Response>
502 ): void {
503 if (
504 this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime
505 ) {
506 workerUsage.runTime.aggregation += message.taskPerformance?.runTime ?? 0
507 if (
508 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
509 .avgRunTime &&
510 workerUsage.tasks.executed !== 0
511 ) {
512 workerUsage.runTime.average =
513 workerUsage.runTime.aggregation / workerUsage.tasks.executed
514 }
515 if (
516 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
517 .medRunTime &&
518 message.taskPerformance?.runTime != null
519 ) {
520 workerUsage.runTime.history.push(message.taskPerformance.runTime)
521 workerUsage.runTime.median = median(workerUsage.runTime.history)
522 }
523 }
524 }
525
526 private updateWaitTimeWorkerUsage (
527 workerUsage: WorkerUsage,
528 task: Task<Data>
529 ): void {
530 const timestamp = performance.now()
531 const taskWaitTime = timestamp - (task.timestamp ?? timestamp)
532 if (
533 this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime
534 ) {
535 workerUsage.waitTime.aggregation += taskWaitTime ?? 0
536 if (
537 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
538 .avgWaitTime &&
539 workerUsage.tasks.executed !== 0
540 ) {
541 workerUsage.waitTime.average =
542 workerUsage.waitTime.aggregation / workerUsage.tasks.executed
543 }
544 if (
545 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
546 .medWaitTime &&
547 taskWaitTime != null
548 ) {
549 workerUsage.waitTime.history.push(taskWaitTime)
550 workerUsage.waitTime.median = median(workerUsage.waitTime.history)
551 }
552 }
553 }
554
555 private updateEluWorkerUsage (
556 workerTasksUsage: WorkerUsage,
557 message: MessageValue<Response>
558 ): void {
559 if (this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu) {
560 if (
561 workerTasksUsage.elu != null &&
562 message.taskPerformance?.elu != null
563 ) {
564 workerTasksUsage.elu = {
565 idle: workerTasksUsage.elu.idle + message.taskPerformance.elu.idle,
566 active:
567 workerTasksUsage.elu.active + message.taskPerformance.elu.active,
568 utilization:
569 (workerTasksUsage.elu.utilization +
570 message.taskPerformance.elu.utilization) /
571 2
572 }
573 } else if (message.taskPerformance?.elu != null) {
574 workerTasksUsage.elu = message.taskPerformance.elu
575 }
576 }
577 }
578
579 /**
580 * Chooses a worker node for the next task.
581 *
582 * The default worker choice strategy uses a round robin algorithm to distribute the load.
583 *
584 * @returns The worker node key
585 */
586 protected chooseWorkerNode (): number {
587 let workerNodeKey: number
588 if (this.type === PoolTypes.dynamic && !this.full && this.internalBusy()) {
589 const workerCreated = this.createAndSetupWorker()
590 this.registerWorkerMessageListener(workerCreated, message => {
591 const currentWorkerNodeKey = this.getWorkerNodeKey(workerCreated)
592 if (
593 isKillBehavior(KillBehaviors.HARD, message.kill) ||
594 (message.kill != null &&
595 this.workerNodes[currentWorkerNodeKey].workerUsage.tasks
596 .executing === 0)
597 ) {
598 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
599 this.flushTasksQueue(currentWorkerNodeKey)
600 // FIXME: wait for tasks to be finished
601 void (this.destroyWorker(workerCreated) as Promise<void>)
602 }
603 })
604 workerNodeKey = this.getWorkerNodeKey(workerCreated)
605 } else {
606 workerNodeKey = this.workerChoiceStrategyContext.execute()
607 }
608 return workerNodeKey
609 }
610
611 /**
612 * Sends a message to the given worker.
613 *
614 * @param worker - The worker which should receive the message.
615 * @param message - The message.
616 */
617 protected abstract sendToWorker (
618 worker: Worker,
619 message: MessageValue<Data>
620 ): void
621
622 /**
623 * Registers a listener callback on the given worker.
624 *
625 * @param worker - The worker which should register a listener.
626 * @param listener - The message listener callback.
627 */
628 protected abstract registerWorkerMessageListener<
629 Message extends Data | Response
630 >(worker: Worker, listener: (message: MessageValue<Message>) => void): void
631
632 /**
633 * Returns a newly created worker.
634 */
635 protected abstract createWorker (): Worker
636
637 /**
638 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
639 *
640 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
641 *
642 * @param worker - The newly created worker.
643 */
644 protected abstract afterWorkerSetup (worker: Worker): void
645
646 /**
647 * Creates a new worker and sets it up completely in the pool worker nodes.
648 *
649 * @returns New, completely set up worker.
650 */
651 protected createAndSetupWorker (): Worker {
652 const worker = this.createWorker()
653
654 worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
655 worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
656 worker.on('error', error => {
657 if (this.emitter != null) {
658 this.emitter.emit(PoolEvents.error, error)
659 }
660 })
661 worker.on('error', () => {
662 if (this.opts.restartWorkerOnError === true) {
663 this.createAndSetupWorker()
664 }
665 })
666 worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
667 worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
668 worker.once('exit', () => {
669 this.removeWorkerNode(worker)
670 })
671
672 this.pushWorkerNode(worker)
673
674 this.setWorkerStatistics(worker)
675
676 this.afterWorkerSetup(worker)
677
678 return worker
679 }
680
681 /**
682 * This function is the listener registered for each worker message.
683 *
684 * @returns The listener function to execute when a message is received from a worker.
685 */
686 protected workerListener (): (message: MessageValue<Response>) => void {
687 return message => {
688 if (message.id != null) {
689 // Task execution response received
690 const promiseResponse = this.promiseResponseMap.get(message.id)
691 if (promiseResponse != null) {
692 if (message.taskError != null) {
693 promiseResponse.reject(message.taskError.message)
694 if (this.emitter != null) {
695 this.emitter.emit(PoolEvents.taskError, message.taskError)
696 }
697 } else {
698 promiseResponse.resolve(message.data as Response)
699 }
700 this.afterTaskExecutionHook(promiseResponse.worker, message)
701 this.promiseResponseMap.delete(message.id)
702 const workerNodeKey = this.getWorkerNodeKey(promiseResponse.worker)
703 if (
704 this.opts.enableTasksQueue === true &&
705 this.tasksQueueSize(workerNodeKey) > 0
706 ) {
707 this.executeTask(
708 workerNodeKey,
709 this.dequeueTask(workerNodeKey) as Task<Data>
710 )
711 }
712 }
713 }
714 }
715 }
716
717 private checkAndEmitEvents (): void {
718 if (this.emitter != null) {
719 if (this.busy) {
720 this.emitter?.emit(PoolEvents.busy, this.info)
721 }
722 if (this.type === PoolTypes.dynamic && this.full) {
723 this.emitter?.emit(PoolEvents.full, this.info)
724 }
725 }
726 }
727
728 /**
729 * Sets the given worker node its tasks usage in the pool.
730 *
731 * @param workerNode - The worker node.
732 * @param workerUsage - The worker usage.
733 */
734 private setWorkerNodeTasksUsage (
735 workerNode: WorkerNode<Worker, Data>,
736 workerUsage: WorkerUsage
737 ): void {
738 workerNode.workerUsage = workerUsage
739 }
740
741 /**
742 * Pushes the given worker in the pool worker nodes.
743 *
744 * @param worker - The worker.
745 * @returns The worker nodes length.
746 */
747 private pushWorkerNode (worker: Worker): number {
748 return this.workerNodes.push({
749 worker,
750 workerUsage: this.getWorkerUsage(worker),
751 tasksQueue: new Queue<Task<Data>>()
752 })
753 }
754
755 // /**
756 // * Sets the given worker in the pool worker nodes.
757 // *
758 // * @param workerNodeKey - The worker node key.
759 // * @param worker - The worker.
760 // * @param workerUsage - The worker usage.
761 // * @param tasksQueue - The worker task queue.
762 // */
763 // private setWorkerNode (
764 // workerNodeKey: number,
765 // worker: Worker,
766 // workerUsage: WorkerUsage,
767 // tasksQueue: Queue<Task<Data>>
768 // ): void {
769 // this.workerNodes[workerNodeKey] = {
770 // worker,
771 // workerUsage,
772 // tasksQueue
773 // }
774 // }
775
776 /**
777 * Removes the given worker from the pool worker nodes.
778 *
779 * @param worker - The worker.
780 */
781 private removeWorkerNode (worker: Worker): void {
782 const workerNodeKey = this.getWorkerNodeKey(worker)
783 if (workerNodeKey !== -1) {
784 this.workerNodes.splice(workerNodeKey, 1)
785 this.workerChoiceStrategyContext.remove(workerNodeKey)
786 }
787 }
788
789 private executeTask (workerNodeKey: number, task: Task<Data>): void {
790 this.beforeTaskExecutionHook(workerNodeKey, task)
791 this.sendToWorker(this.workerNodes[workerNodeKey].worker, task)
792 }
793
794 private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
795 return this.workerNodes[workerNodeKey].tasksQueue.enqueue(task)
796 }
797
798 private dequeueTask (workerNodeKey: number): Task<Data> | undefined {
799 return this.workerNodes[workerNodeKey].tasksQueue.dequeue()
800 }
801
802 private tasksQueueSize (workerNodeKey: number): number {
803 return this.workerNodes[workerNodeKey].tasksQueue.size
804 }
805
806 private flushTasksQueue (workerNodeKey: number): void {
807 if (this.tasksQueueSize(workerNodeKey) > 0) {
808 for (let i = 0; i < this.tasksQueueSize(workerNodeKey); i++) {
809 this.executeTask(
810 workerNodeKey,
811 this.dequeueTask(workerNodeKey) as Task<Data>
812 )
813 }
814 }
815 }
816
817 private flushTasksQueues (): void {
818 for (const [workerNodeKey] of this.workerNodes.entries()) {
819 this.flushTasksQueue(workerNodeKey)
820 }
821 }
822
823 private setWorkerStatistics (worker: Worker): void {
824 this.sendToWorker(worker, {
825 statistics: {
826 runTime:
827 this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
828 .runTime,
829 elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
830 .elu
831 }
832 })
833 }
834
835 private getWorkerUsage (worker: Worker): WorkerUsage {
836 return {
837 tasks: this.getTaskStatistics(worker),
838 runTime: {
839 aggregation: 0,
840 average: 0,
841 median: 0,
842 history: new CircularArray()
843 },
844 waitTime: {
845 aggregation: 0,
846 average: 0,
847 median: 0,
848 history: new CircularArray()
849 },
850 elu: undefined
851 }
852 }
853
854 private getTaskStatistics (worker: Worker): TaskStatistics {
855 const queueSize =
856 this.workerNodes[this.getWorkerNodeKey(worker)]?.tasksQueue?.size
857 return {
858 executed: 0,
859 executing: 0,
860 get queued (): number {
861 return queueSize ?? 0
862 },
863 failed: 0
864 }
865 }
866 }