1 import { cpus
} from
'node:os'
2 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
3 import type { IPool
} from
'../pool'
4 import type { IWorker
} from
'../worker'
8 TaskStatisticsRequirements
,
9 WorkerChoiceStrategyOptions
10 } from
'./selection-strategies-types'
13 * Worker choice strategy abstract base class.
15 * @typeParam Worker - Type of worker which manages the strategy.
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam Response - Type of execution response. This can only be serializable data.
19 export abstract class AbstractWorkerChoiceStrategy
<
20 Worker
extends IWorker
,
23 > implements IWorkerChoiceStrategy
{
25 * Toggles finding the last free worker node key.
27 private toggleFindLastFreeWorkerNodeKey
: boolean = false
30 public readonly strategyPolicy
: StrategyPolicy
= {
31 useDynamicWorker
: false
35 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
54 * Constructs a worker choice strategy bound to the pool.
56 * @param pool - The pool instance.
57 * @param opts - The worker choice strategy options.
60 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
61 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
63 this.choose
= this.choose
.bind(this)
66 protected setTaskStatisticsRequirements (
67 opts
: WorkerChoiceStrategyOptions
70 this.taskStatisticsRequirements
.runTime
.average
&&
71 opts
.runTime
?.median
=== true
73 this.taskStatisticsRequirements
.runTime
.average
= false
74 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
78 this.taskStatisticsRequirements
.runTime
.median
&&
79 opts
.runTime
?.median
=== false
81 this.taskStatisticsRequirements
.runTime
.average
= true
82 this.taskStatisticsRequirements
.runTime
.median
= opts
.runTime
86 this.taskStatisticsRequirements
.waitTime
.average
&&
87 opts
.waitTime
?.median
=== true
89 this.taskStatisticsRequirements
.waitTime
.average
= false
90 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
94 this.taskStatisticsRequirements
.waitTime
.median
&&
95 opts
.waitTime
?.median
=== false
97 this.taskStatisticsRequirements
.waitTime
.average
= true
98 this.taskStatisticsRequirements
.waitTime
.median
= opts
.waitTime
102 this.taskStatisticsRequirements
.elu
.average
&&
103 opts
.elu
?.median
=== true
105 this.taskStatisticsRequirements
.elu
.average
= false
106 this.taskStatisticsRequirements
.elu
.median
= opts
.elu
.median
as boolean
109 this.taskStatisticsRequirements
.elu
.median
&&
110 opts
.elu
?.median
=== false
112 this.taskStatisticsRequirements
.elu
.average
= true
113 this.taskStatisticsRequirements
.elu
.median
= opts
.elu
.median
as boolean
118 public abstract reset (): boolean
121 public abstract update (workerNodeKey
: number): boolean
124 public abstract choose (): number
127 public abstract remove (workerNodeKey
: number): boolean
130 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
131 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
132 this.setTaskStatisticsRequirements(opts
)
137 * Finds a free worker node key.
139 * @returns The free worker node key or `-1` if there is no free worker node.
141 protected findFreeWorkerNodeKey (): number {
142 if (this.toggleFindLastFreeWorkerNodeKey
) {
143 this.toggleFindLastFreeWorkerNodeKey
= false
144 return this.findLastFreeWorkerNodeKey()
146 this.toggleFindLastFreeWorkerNodeKey
= true
147 return this.findFirstFreeWorkerNodeKey()
151 * Gets the worker task runtime.
152 * If the task statistics require the average runtime, the average runtime is returned.
153 * If the task statistics require the median runtime , the median runtime is returned.
155 * @param workerNodeKey - The worker node key.
156 * @returns The worker task runtime.
158 protected getWorkerTaskRunTime (workerNodeKey
: number): number {
159 return this.taskStatisticsRequirements
.runTime
.median
160 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
161 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
165 * Gets the worker task wait time.
166 * If the task statistics require the average wait time, the average wait time is returned.
167 * If the task statistics require the median wait time, the median wait time is returned.
169 * @param workerNodeKey - The worker node key.
170 * @returns The worker task wait time.
172 protected getWorkerTaskWaitTime (workerNodeKey
: number): number {
173 return this.taskStatisticsRequirements
.waitTime
.median
174 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.median
175 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.runTime
.average
179 * Gets the worker task ELU.
180 * If the task statistics require the average ELU, the average ELU is returned.
181 * If the task statistics require the median ELU, the median ELU is returned.
183 * @param workerNodeKey - The worker node key.
184 * @returns The worker task ELU.
186 protected getWorkerTaskElu (workerNodeKey
: number): number {
187 return this.taskStatisticsRequirements
.elu
.median
188 ? this.pool
.workerNodes
[workerNodeKey
].workerUsage
.elu
.active
.median
189 : this.pool
.workerNodes
[workerNodeKey
].workerUsage
.elu
.active
.average
192 protected computeDefaultWorkerWeight (): number {
193 let cpusCycleTimeWeight
= 0
194 for (const cpu
of cpus()) {
195 // CPU estimated cycle time
196 const numberOfDigits
= cpu
.speed
.toString().length
- 1
197 const cpuCycleTime
= 1 / (cpu
.speed
/ Math.pow(10, numberOfDigits
))
198 cpusCycleTimeWeight
+= cpuCycleTime
* Math.pow(10, numberOfDigits
)
200 return Math.round(cpusCycleTimeWeight
/ cpus().length
)
204 * Finds the first free worker node key based on the number of tasks the worker has applied.
206 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
208 * If no free worker is found, `-1` is returned.
210 * @returns A worker node key if there is one, `-1` otherwise.
212 private findFirstFreeWorkerNodeKey (): number {
213 return this.pool
.workerNodes
.findIndex(workerNode
=> {
214 return workerNode
.workerUsage
.tasks
.executing
=== 0
219 * Finds the last free worker node key based on the number of tasks the worker has applied.
221 * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
223 * If no free worker is found, `-1` is returned.
225 * @returns A worker node key if there is one, `-1` otherwise.
227 private findLastFreeWorkerNodeKey (): number {
228 // It requires node >= 18.0.0:
229 // return this.workerNodes.findLastIndex(workerNode => {
230 // return workerNode.workerUsage.tasks.executing === 0
233 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
238 this.pool
.workerNodes
[workerNodeKey
].workerUsage
.tasks
.executing
=== 0