1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
7 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Worker choice strategy abstract base class.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam Response - Type of execution response. This can only be serializable data.
17 export abstract class AbstractWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
21 > implements IWorkerChoiceStrategy
{
23 * Toggles finding the last free worker node key.
25 private toggleFindLastFreeWorkerNodeKey
: boolean = false
27 public readonly requiredStatistics
: RequiredStatistics
= {
34 * Constructs a worker choice strategy bound to the pool.
36 * @param pool - The pool instance.
37 * @param opts - The worker choice strategy options.
40 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
41 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
43 this.choose
= this.choose
.bind(this)
46 protected setRequiredStatistics (opts
: WorkerChoiceStrategyOptions
): void {
47 if (this.requiredStatistics
.avgRunTime
&& opts
.medRunTime
=== true) {
48 this.requiredStatistics
.avgRunTime
= false
49 this.requiredStatistics
.medRunTime
= opts
.medRunTime
as boolean
51 if (this.requiredStatistics
.medRunTime
&& opts
.medRunTime
=== false) {
52 this.requiredStatistics
.avgRunTime
= true
53 this.requiredStatistics
.medRunTime
= opts
.medRunTime
as boolean
58 public abstract reset (): boolean
61 public abstract update (workerNodeKey
: number): boolean
64 public abstract choose (): number
67 public abstract remove (workerNodeKey
: number): boolean
70 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
71 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
72 this.setRequiredStatistics(opts
)
77 * Finds a free worker node key.
79 * @returns The free worker node key or `-1` if there is no free worker node.
81 protected findFreeWorkerNodeKey (): number {
82 if (this.toggleFindLastFreeWorkerNodeKey
) {
83 this.toggleFindLastFreeWorkerNodeKey
= false
84 return this.findLastFreeWorkerNodeKey()
86 this.toggleFindLastFreeWorkerNodeKey
= true
87 return this.findFirstFreeWorkerNodeKey()
91 * Gets the worker task runtime.
92 * If the required statistics are `avgRunTime`, the average runtime is returned.
93 * If the required statistics are `medRunTime`, the median runtime is returned.
95 * @param workerNodeKey - The worker node key.
96 * @returns The worker task runtime.
98 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
99 return this.requiredStatistics
.medRunTime
100 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medRunTime
101 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgRunTime
105 * Finds the first free worker node key based on the number of tasks the worker has applied.
107 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
109 * If no free worker is found, `-1` is returned.
111 * @returns A worker node key if there is one, `-1` otherwise.
113 private findFirstFreeWorkerNodeKey (): number {
114 return this.pool
.workerNodes
.findIndex(workerNode
=> {
115 return workerNode
.tasksUsage
.running
=== 0
120 * Finds the last free worker node key based on the number of tasks the worker has applied.
122 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
124 * If no free worker is found, `-1` is returned.
126 * @returns A worker node key if there is one, `-1` otherwise.
128 private findLastFreeWorkerNodeKey (): number {
129 // It requires node >= 18.0.0:
130 // return this.workerNodes.findLastIndex(workerNode => {
131 // return workerNode.tasksUsage.running === 0
134 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
138 if (this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.running
=== 0) {